diff --git a/apps/server/scripts/acp-mock-agent.ts b/apps/server/scripts/acp-mock-agent.ts index 98acb36ea6422..e14b25b9aae14 100644 --- a/apps/server/scripts/acp-mock-agent.ts +++ b/apps/server/scripts/acp-mock-agent.ts @@ -23,6 +23,12 @@ const copilotModesProfile = process.env.T3_ACP_COPILOT_MODES === "1"; const copilotModeId = (name: string) => `https://agentclientprotocol.com/protocol/session-modes#${name}`; const modeOptionCategory = process.env.T3_ACP_MODE_OPTION_CATEGORY ?? "mode"; +// Factory Droid's modes are autonomy levels behind an `autonomy_level` option. +const droidModesProfile = process.env.T3_ACP_DROID_MODES === "1"; +const modeConfigId = droidModesProfile ? "autonomy_level" : "mode"; +// A prompt containing "Always allow this session ()" makes the mock Droid raise itself to +// that level, as approving a request for the session does. +const droidGrantPattern = /Always allow this session \(([\w-]+)\)/; // Comma-separated command names the mock advertises, so `/compact` support is testable. const advertisedCommandNames = (process.env.T3_ACP_AVAILABLE_COMMANDS ?? "") .split(",") @@ -122,7 +128,9 @@ let currentModeId = antigravityProfile ? (process.env.T3_ACP_INITIAL_MODE ?? "default") : copilotModesProfile ? copilotModeId("agent") - : "ask"; + : droidModesProfile + ? "auto-high" + : "ask"; let currentModelId = antigravityProfile ? "gemini-test-low" : "default"; let parameterizedModelPicker = false; let currentReasoning = "medium"; @@ -310,7 +318,7 @@ function configOptions(): ReadonlyArray { ] : []), { - configId: "mode", + configId: modeConfigId, name: "Mode", category: modeOptionCategory, type: "select" as const, @@ -383,23 +391,30 @@ const availableModes: ReadonlyArray = antigravityProfile { id: copilotModeId("plan"), name: "Plan" }, { id: copilotModeId("autopilot"), name: "Autopilot" }, ] - : [ - { - id: "ask", - name: "Ask", - description: "Request permission before making any changes", - }, - { - id: "architect", - name: "Architect", - description: "Design and plan software systems without implementation", - }, - { - id: "code", - name: "Code", - description: "Write and modify code with full tool access", - }, - ]; + : droidModesProfile + ? [ + { id: "normal", name: "Auto (Off)" }, + { id: "auto-low", name: "Auto (Low)" }, + { id: "auto-medium", name: "Auto (Medium)" }, + { id: "auto-high", name: "Auto (High)" }, + ] + : [ + { + id: "ask", + name: "Ask", + description: "Request permission before making any changes", + }, + { + id: "architect", + name: "Architect", + description: "Design and plan software systems without implementation", + }, + { + id: "code", + name: "Code", + description: "Write and modify code with full tool access", + }, + ]; function modeState(): AcpCompat.SessionModeState { return { @@ -885,8 +900,16 @@ const program = Effect.gen(function* () { }, ); } - if (request.configId === "mode" && typeof request.value === "string") { + if (request.configId === modeConfigId && typeof request.value === "string") { + const changed = request.value !== currentModeId; currentModeId = request.value; + // Droid reports a changed level in a full config update before it answers. + if (droidModesProfile && changed) { + yield* agent.client.sessionUpdate({ + sessionId: request.sessionId, + update: { sessionUpdate: "config_option_update", configOptions: configOptions() }, + }); + } } if (request.configId === "model" && typeof request.value === "string") { currentModelId = request.value; @@ -1574,6 +1597,22 @@ const program = Effect.gen(function* () { }); } } + const grantedLevel = droidModesProfile + ? request.prompt + .map((block) => + block.type === "text" && typeof block.text === "string" + ? droidGrantPattern.exec(block.text)?.[1] + : undefined, + ) + .find((level) => level !== undefined) + : undefined; + if (grantedLevel !== undefined) { + currentModeId = grantedLevel; + yield* agent.client.sessionUpdate({ + sessionId: requestedSessionId, + update: { sessionUpdate: "config_option_update", configOptions: configOptions() }, + }); + } if (emitTrackedToolCallKindThenHang !== undefined) { yield* agent.client.sessionUpdate({ diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index 7815f8d959a2f..f5eab80e7f90d 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -760,6 +760,24 @@ function rawProtocolRequestParam( return typeof params === "object" && params !== null ? Reflect.get(params, key) : undefined; } +// The `autonomy_level` a decoded reply or session update reports in its config options. Raw +// incoming events are chunks, which can split a message. +function protocolAutonomyLevel(event: EffectAcpProtocol.AcpProtocolLogEvent): unknown { + if (event.stage !== "decoded" || !Array.isArray(event.payload)) return undefined; + const field = (value: unknown, key: string): unknown => + typeof value === "object" && value !== null ? Reflect.get(value, key) : undefined; + let level: unknown; + for (const message of event.payload) { + const options = + field(field(field(message, "exit"), "value"), "configOptions") ?? + field(field(field(message, "payload"), "update"), "configOptions"); + if (!Array.isArray(options)) continue; + const option = options.find((candidate) => field(candidate, "configId") === "autonomy_level"); + if (option !== undefined) level = field(option, "currentValue"); + } + return level; +} + function rawProtocolPromptTextBlocks( event: EffectAcpProtocol.AcpProtocolLogEvent, ): Array<{ type: "text"; text: string }> { @@ -2471,6 +2489,183 @@ describe("AcpAdapterV2", () => { }).pipe(Effect.provide(layerTest)); }); + // Each mock Droid session starts at auto-high. `writes` are T3's level writes before a turn's + // prompt; `level` is the last level Droid reported by then. + it.live.each([ + { + agent: "the registry agent", + settings: DROID_SETTINGS, + turns: [ + { runtimeMode: "approval-required", writes: ["normal"], level: "normal" }, + { + runtimeMode: "approval-required", + pick: "auto-high", + writes: ["auto-high"], + level: "auto-high", + }, + { runtimeMode: "full-access", writes: [], level: "auto-high" }, + { runtimeMode: "approval-required", writes: ["normal"], level: "normal" }, + ], + }, + { + agent: "a local droid command", + settings: DROID_LOCAL_SETTINGS, + turns: [ + { + runtimeMode: "auto-accept-edits", + otherOption: "example", + grant: "auto-low", + writes: ["normal"], + level: "normal", + }, + { + runtimeMode: "approval-required", + grant: "auto-medium", + writes: ["normal"], + level: "normal", + }, + { runtimeMode: "auto", grant: "auto-high", writes: ["normal"], level: "normal" }, + { runtimeMode: "auto", model: "composer-2", writes: [], level: "auto-high" }, + ], + }, + { + agent: "a session approval, a model change and new sessions", + settings: DROID_SETTINGS, + turns: [ + { + runtimeMode: "approval-required", + grant: "auto-low", + writes: ["normal"], + level: "normal", + }, + { runtimeMode: "approval-required", model: "composer-2", writes: [], level: "auto-low" }, + { + runtimeMode: "approval-required", + newSession: true, + pick: "auto-low", + writes: ["auto-low"], + level: "auto-low", + }, + { runtimeMode: "approval-required", newSession: true, writes: ["normal"], level: "normal" }, + ], + }, + { + agent: "a pick a managed limit drops", + settings: DROID_SETTINGS, + turns: [ + { + runtimeMode: "approval-required", + maxRuntimeMode: "approval-required", + pick: "auto-high", + writes: ["normal"], + level: "normal", + }, + ], + }, + ] as const)( + "lowers an unpicked Factory Droid level only while applying a permission mode below Full access, for $agent", + ({ settings, turns }) => + Effect.gen(function* () { + const protocolEvents = yield* Queue.unbounded(); + const instanceId = ProviderInstanceId.make("droid-autonomy-test"); + const threadId = ThreadId.make("thread-droid-autonomy"); + const adapter = yield* makeAcpRegistryAdapterV2({ + instanceId, + selfInvocation: yield* resolveSelfInvocation(), + settings, + environment: {}, + makeRuntime: makeMockRuntime({ + childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner, + mockAgentPath: yield* bundledMockAgentPath, + protocolEvents, + environment: { T3_ACP_DROID_MODES: "1", T3_ACP_EMIT_EMPTY_DEFAULT_AGENT_OPTION: "1" }, + }), + }).pipe( + Effect.provide( + Layer.mock(AcpRegistryCatalog)({ resolve: () => Effect.die("runtime is injected") }), + ), + ); + const turnInputs = turns.map((turn) => ({ + modelSelection: { + instanceId, + model: "model" in turn ? turn.model : "default", + options: [ + ...("pick" in turn ? [{ id: "autonomy_level", value: turn.pick }] : []), + ...("otherOption" in turn ? [{ id: "agent", value: turn.otherOption }] : []), + ], + }, + runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: turn.runtimeMode, + interactionMode: "default", + ...("maxRuntimeMode" in turn ? { maxRuntimeMode: turn.maxRuntimeMode } : {}), + cwd: process.cwd(), + }), + })); + const runtime = yield* adapter.openSession({ + threadId, + providerSessionId: ProviderSessionId.make("provider-session-droid-autonomy"), + ...turnInputs[0]!, + }); + let providerThread = yield* runtime.ensureThread({ threadId, ...turnInputs[0]! }); + const now = yield* DateTime.now; + const observed = []; + let level: unknown; + for (const [ordinal, turn] of turns.entries()) { + const input = turnInputs[ordinal]!; + if ("newSession" in turn) { + providerThread = yield* runtime.ensureThread({ + threadId, + ...input, + existingProviderThread: providerThread, + forceNewNativeThread: true, + }); + } + yield* runtime.startTurn( + makeTurnInput({ + threadId, + providerThread, + instanceId, + now, + ordinal, + // The mock Droid raises itself to this level, as a session approval does. + messageText: + "grant" in turn + ? `Always allow this session (${turn.grant}).` + : `Request ${ordinal}.`, + ...input, + }), + ); + const terminal = yield* runtime.events.pipe( + Stream.filter((event) => event.type === "turn.terminal"), + Stream.runHead, + ); + const writes: Array = []; + while (true) { + const event = yield* Queue.take(protocolEvents); + if (event.direction === "incoming") level = protocolAutonomyLevel(event) ?? level; + if (event.direction !== "outgoing") continue; + const method = rawProtocolMethod(event); + if (method === "session/prompt") break; + if ( + method === "session/set_config_option" && + rawProtocolRequestParam(event, "configId") === "autonomy_level" + ) { + writes.push(rawProtocolRequestParam(event, "value")); + } + } + observed.push({ writes, level, status: Option.getOrThrow(terminal).status }); + } + assert.deepEqual( + observed, + turns.map((turn) => ({ + writes: [...turn.writes], + level: turn.level, + status: "completed" as const, + })), + ); + }).pipe(Effect.provide(layerTest)), + ); + it.live.each(["failed", "recovered", "cancelled"] as const)( "projects Mistral retry notices and their %s outcome", (outcome) => diff --git a/docs/user/providers-acp.md b/docs/user/providers-acp.md index 453c777e3c8c0..1133a425786a6 100644 --- a/docs/user/providers-acp.md +++ b/docs/user/providers-acp.md @@ -112,6 +112,18 @@ approval rules. They start in their own default mode, and their mode picker in t menu switches it. How far an agent is confined depends on that mode and the agent's own settings, so use its sandbox and permission controls when that matters. +A new Factory Droid session starts at the autonomy level saved in Droid's own settings. On a thread +below **Full access** whose current model has no level picked in the menu, T3 Code switches Droid +from Auto (Low), (Medium), or (High) to Auto (Off), where Droid offers it, when it starts or reloads +the session and when the thread's permission mode changes. A session approval that had raised +Droid's level no longer covers the edits or commands that depended on it, so Droid can ask again. +Its own command rules still apply. A level picked in the menu applies in every permission mode, so +Auto (High) lets Droid act without asking even on **Supervised**, unless a +[managed policy](./managed-policy.md#permission-modes) caps the permission mode. As with any model +option, T3 Code keeps the pick with the model it was made on, so a thread carries no level after +you change to a model you have not picked one for. If a session or a turn fails while switching to +Auto (Off), update Droid to 0.236.0 or later. + When a registry agent asks for approval, T3 Code answers by the thread's [permission mode](./permission-modes.md): **Supervised** shows the request in the conversation, **Auto-accept edits** approves edits and shows the rest, and **Auto** and **Full access** approve diff --git a/packages/provider-acp-registry/src/server/adapter.ts b/packages/provider-acp-registry/src/server/adapter.ts index 8eb5dbb30b8cc..be80802efb9ee 100644 --- a/packages/provider-acp-registry/src/server/adapter.ts +++ b/packages/provider-acp-registry/src/server/adapter.ts @@ -9,7 +9,7 @@ import { extractDevinSubagentUpdate, devinAnswerablePermissionOptions, } from "./devinAcp.ts"; -import { extractDroidElicitationForm } from "./droidAcp.ts"; +import { droidSessionModeForPolicy, extractDroidElicitationForm } from "./droidAcp.ts"; import { makeKimiPromptOutcomeWatch } from "./kimiAcp.ts"; import { extractOmpElicitationForm } from "./ompAcp.ts"; import { extractOpenCodeElicitationForm } from "./opencodeAcp.ts"; @@ -277,7 +277,10 @@ export const makeAcpRegistryAdapterV2 = Effect.fn("makeAcpRegistryAdapterV2")(fu promptFailure: (cause) => acpRegistryPromptFailure(isVibe ? "mistral-vibe" : registryAgentId, cause), ...(isDroid - ? { extractElicitationForm: extractDroidElicitationForm } + ? { + extractElicitationForm: extractDroidElicitationForm, + sessionModeForPolicy: droidSessionModeForPolicy, + } : isOmp ? { extractElicitationForm: extractOmpElicitationForm } : isOpenCode diff --git a/packages/provider-acp-registry/src/server/droidAcp.ts b/packages/provider-acp-registry/src/server/droidAcp.ts index d5ad446a8c740..45b412e6625ce 100644 --- a/packages/provider-acp-registry/src/server/droidAcp.ts +++ b/packages/provider-acp-registry/src/server/droidAcp.ts @@ -82,3 +82,16 @@ export const extractDroidElicitationForm: NonNullable< }, }; }; + +// Droid's modes are its autonomy levels: `normal` is Auto (Off), and the `auto-*` levels can act +// without asking. +export const droidSessionModeForPolicy: NonNullable = ( + policy, + unpickedMode, +) => + policy.runtimeMode !== "full-access" && + unpickedMode !== undefined && + ["auto-low", "auto-medium", "auto-high"].includes(unpickedMode.currentModeId) && + unpickedMode.availableModes.some((mode) => mode.id === "normal") + ? "normal" + : undefined; diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 21fe311588906..42ba58d13e5bf 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -483,8 +483,14 @@ export interface AcpAdapterV2Flavor { */ readonly ownedModelOptionIds?: ReadonlyArray; readonly lockSessionModeToPolicy?: true; + /** + * Mode to set on each configure, or none. `unpickedMode` is the session's mode state, given only + * while applying the permission mode (a session's first configure, or a changed runtime mode) + * to a thread that picks no mode. + */ readonly sessionModeForPolicy?: ( policy: ProviderAdapter.ProviderAdapterV2RuntimePolicy, + unpickedMode: AcpSessionModeState | undefined, ) => string | undefined; /** * Native select-option values to apply for a runtime policy (e.g. Kiro's @@ -2301,6 +2307,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( rootForegroundWork = undefined; agentRequests.clear(); nativeChildren.clear(); + yield* Ref.set(configuredRuntimeMode, null); yield* Ref.set(activeSessionSetup, setup); }); const activeSelection = yield* Ref.make(null); @@ -8287,7 +8294,13 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( if (!advertisedValues.includes(selection.value)) continue; yield* runtime.setConfigOption(selection.id, selection.value); } - const policyMode = flavor.sessionModeForPolicy?.(runtimePolicy); + const policyMode = flavor.sessionModeForPolicy?.( + runtimePolicy, + (yield* Ref.get(configuredRuntimeMode)) !== runtimePolicy.runtimeMode && + !optionSelections.some((selection) => isModeChoice(selection.id)) + ? yield* runtime.getModeState + : undefined, + ); const modeLocked = flavor.lockSessionModeToPolicy && policyMode !== undefined; if (policyMode !== undefined) { yield* runtime.setMode(policyMode);