Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

delegatedTaskModes.ts

156 lines6.1 KB

1import {
2 type ModelSelection,
3 OrchestratorMcpFailure,
4 type OrchestratorMcpDelegateTaskInput,
5 type OrchestratorMcpInteractionMode,
6 type OrchestratorMcpRuntimeMode,
7 type ProviderInteractionMode,
8 type ProviderOptionDescriptor,
9 type ProviderOptionSelection,
10 type RuntimeMode,
11} from "@t3tools/contracts";
12import { ACP_SESSION_MODE_OPTION_ID } from "@t3tools/provider-acp/server/sessionConfig";
13import * as Effect from "effect/Effect";
14​
15/** How much a runtime mode allows, narrowest first. */
16export function runtimeModeRank(mode: RuntimeMode): number {
17 switch (mode) {
18 case "approval-required":
19 return 0;
20 case "auto-accept-edits":
21 return 1;
22 case "auto":
23 return 2;
24 case "full-access":
25 return 3;
26 }
28​
29/** How much an interaction mode allows: plan, then default. */
30export function interactionModeRank(mode: ProviderInteractionMode): number {
31 return mode === "plan" ? 0 : 1;
33​
34/**
35 * The mode a child runs at: the one asked for, never broader than its parent's.
36 * An inherited mode the target provider does not offer steps down to the
37 * broadest one it does; an explicitly requested one is refused instead.
38 */
39export function resolveRuntimeMode(
40 parentMode: RuntimeMode,
41 requested: OrchestratorMcpRuntimeMode | undefined,
42 target?: {
43 readonly modelSelection: ModelSelection;
44 readonly supportedRuntimeModes: ReadonlyArray<RuntimeMode> | undefined;
45 },
46): Effect.Effect<RuntimeMode, OrchestratorMcpFailure> {
47 const inherited = requested === undefined || requested === "inherit";
48 const resolved = inherited ? parentMode : requested;
49 if (runtimeModeRank(resolved) > runtimeModeRank(parentMode)) {
50 return Effect.fail(
51 new OrchestratorMcpFailure({
52 code: "runtime_mode_escalation_denied",
53 message: `Child runtime mode ${resolved} is broader than parent mode ${parentMode}.`,
54 }),
55 );
56 }
57 const supported = target?.supportedRuntimeModes;
58 if (supported === undefined || supported.length === 0 || supported.includes(resolved)) {
59 return Effect.succeed(resolved);
60 }
61 if (!inherited) {
62 return Effect.fail(
63 new OrchestratorMcpFailure({
64 code: "invalid_request",
65 message: `Provider ${target?.modelSelection.instanceId} does not offer runtime mode ${resolved}, so it would run as approval-required. Use one of: ${supported.join(", ")}.`,
66 }),
67 );
68 }
69 return Effect.succeed(
70 supported
71 .filter((mode) => runtimeModeRank(mode) <= runtimeModeRank(resolved))
72 .toSorted((left, right) => runtimeModeRank(right) - runtimeModeRank(left))[0] ??
73 "approval-required",
74 );
76​
77/** The same, for interaction modes. */
78export function resolveInteractionMode(
79 parentMode: ProviderInteractionMode,
80 requested: OrchestratorMcpInteractionMode | undefined,
81): Effect.Effect<ProviderInteractionMode, OrchestratorMcpFailure> {
82 const resolved = requested === undefined || requested === "inherit" ? parentMode : requested;
83 return interactionModeRank(resolved) > interactionModeRank(parentMode)
84 ? Effect.fail(
85 new OrchestratorMcpFailure({
86 code: "interaction_mode_escalation_denied",
87 message: `Child interaction mode ${resolved} is broader than parent mode ${parentMode}.`,
88 }),
89 )
90 : Effect.succeed(resolved);
92​
93/** True for the option that sets an agent's own session mode. */
94const isAgentModePick = (
95 selection: ProviderOptionSelection,
96 descriptors: ReadonlyArray<ProviderOptionDescriptor>,
97) =>
98 // The adapter applies the synthetic mode id whether or not the probe advertised it.
99 selection.id === ACP_SESSION_MODE_OPTION_ID ||
100 descriptors.some((descriptor) => descriptor.id === selection.id && descriptor.role === "mode");
101​
102/**
103 * An agent's own session mode can skip the approvals a runtime mode asks for, and T3 cannot rank
104 * it against one. So only a full-access caller may pick one. `held` is the selection the caller
105 * already runs with or the thread already has; repeating it grants nothing new.
106 */
107export function refuseAgentModePick(input: {
108 readonly callerRuntimeMode: RuntimeMode;
109 readonly picked: ReadonlyArray<ProviderOptionSelection> | undefined;
110 readonly descriptors: ReadonlyArray<ProviderOptionDescriptor>;
111 readonly held: ReadonlyArray<ProviderOptionSelection> | undefined;
112}): Effect.Effect<void, OrchestratorMcpFailure> {
113 if (input.callerRuntimeMode === "full-access") return Effect.void;
114 const pick = input.picked?.find(
115 (selection) =>
116 isAgentModePick(selection, input.descriptors) &&
117 !input.held?.some((held) => held.id === selection.id && held.value === selection.value),
118 );
119 return pick === undefined
120 ? Effect.void
121 : Effect.fail(
122 new OrchestratorMcpFailure({
123 code: "runtime_mode_escalation_denied",
124 message: `Option ${pick.id} sets the agent's own mode, which T3 cannot hold to ${input.callerRuntimeMode}. Only a full-access caller may set it; leave it out.`,
125 }),
126 );
128​
129/** `options` without the agent's own mode, for work that starts where only full access may pick one. */
130export const withoutAgentModePicks = (
131 options: ReadonlyArray<ProviderOptionSelection> | undefined,
132 descriptors: ReadonlyArray<ProviderOptionDescriptor>,
133): ReadonlyArray<ProviderOptionSelection> | undefined =>
134 options?.filter((selection) => !isAgentModePick(selection, descriptors));
135​
136/** Every option a provider instance advertises, across its models. */
137export const providerOptionDescriptors = (
138 provider:
139 | {
140 readonly models: ReadonlyArray<{
141 readonly capabilities?:
142 | { readonly optionDescriptors?: ReadonlyArray<ProviderOptionDescriptor> | undefined }
143 | null
144 | undefined;
145 }>;
146 }
147 | undefined,
148): ReadonlyArray<ProviderOptionDescriptor> =>
149 provider?.models.flatMap((model) => model.capabilities?.optionDescriptors ?? []) ?? [];
150​
151/** The task as its child receives it, with the role the caller asked for. */
152export function taskPrompt(input: OrchestratorMcpDelegateTaskInput): string {
153 return input.role === undefined || input.role === "general"
154 ? input.task
155 : `Act as the ${input.role} sub-agent for this task.\n\n${input.task}`;