diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index 7815f8d959a2f..235f0f293bc61 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -101,6 +101,7 @@ import { import { appendUserInputAttachmentPaths } from "../../provider/userInputAttachments.ts"; +import { knownProviderFailureMessage } from "@t3tools/provider-core/server/failure"; import { makeGrokAcpAdapterFlavor } from "@t3tools/provider-grok/testing"; import { acpRegistryPromptFailure, @@ -2957,6 +2958,57 @@ describe("AcpAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); + it.live( + "fails the session open with a permission mode error when the agent refuses the mode", + () => + Effect.gen(function* () { + const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const mockAgentPath = yield* bundledMockAgentPath; + const instanceId = ProviderInstanceId.make("acp-test-refused-policy-mode"); + const threadId = ThreadId.make("thread-acp-refused-policy-mode"); + const adapter = yield* makeAcpAdapterV2({ + instanceId, + flavor: { + driver: ACP_TEST_DRIVER, + capabilities: AcpProviderCapabilitiesV2, + sessionModeForPolicy: () => "code", + makeRuntime: makeMockRuntime({ + childProcessSpawner, + mockAgentPath, + environment: { T3_ACP_FAIL_SET_MODE_CONFIG_OPTION: "1" }, + }), + }, + selfInvocation: yield* resolveSelfInvocation(), + }); + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: "auto", + interactionMode: "default", + cwd: process.cwd(), + }); + const modelSelection = { instanceId, model: "composer-2" } as const; + const result = yield* Effect.exit( + adapter + .openSession({ + threadId, + providerSessionId: ProviderSessionId.make("provider-session-acp-refused-mode"), + modelSelection, + runtimePolicy, + }) + .pipe( + Effect.flatMap((runtime) => + runtime.ensureThread({ threadId, modelSelection, runtimePolicy }), + ), + ), + ); + const error = Exit.isFailure(result) ? Cause.squash(result.cause) : undefined; + assert.instanceOf(error, ProviderAdapter.ProviderAdapterOpenSessionError); + assert.strictEqual( + knownProviderFailureMessage(error), + 'The agent does not offer its "code" mode in this session. Choose another permission mode, then retry.', + ); + }).pipe(Effect.provide(layerTest)), + ); + it.live("reapplies a selection after a switch away from it failed partway", () => Effect.gen(function* () { const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts index ef9bd2bb32712..a19c9c27cb5c6 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts @@ -3197,14 +3197,28 @@ effectIt.effect("fails rather than starts a fresh thread when a resume outcome i }), ); -effectIt.effect("fails the run without a retry when the provider needs sign-in", () => +const refusedModeMessage = + 'The agent does not offer its "auto" mode in this session. Choose another permission mode, then retry.'; + +effectIt.effect.each([ + { + name: "the provider needs sign-in", + failure: { requiresSignIn: true } as const, + message: "Failed to open antigravity provider session new-provider-session.", + }, + { + name: "the agent refuses the permission mode", + failure: { cause: { _tag: "AcpPermissionModeError", message: refusedModeMessage } }, + message: refusedModeMessage, + }, +])("fails the run without a retry when $name", ({ failure, message }) => Effect.gen(function* () { const harness = makeLocalCommandHarness({ text: "Continue", openFailure: new ProviderAdapter.ProviderAdapterOpenSessionError({ driver: ProviderDriverKind.make("antigravity"), providerSessionId: ProviderSessionId.make("new-provider-session"), - requiresSignIn: true, + ...failure, }), }); @@ -3216,10 +3230,7 @@ effectIt.effect("fails the run without a retry when the provider needs sign-in", { type: "error", status: "failed", - failure: { - class: "provider_error", - message: "Failed to open antigravity provider session new-provider-session.", - }, + failure: { class: "provider_error", message }, }, ]); }), diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts index 1cb77e387f950..3304787bda0f4 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts @@ -986,11 +986,14 @@ export const layer: Layer.Layer< ); if (sessionResult._tag === "Failure") { const failure = sessionResult.failure; - const requiresSignIn = - "cause" in failure && - isProviderAdapterOpenSessionError(failure.cause) && - failure.cause.requiresSignIn === true; - if (input.willRetry === true && !requiresSignIn) return yield* failure; + const openError = + "cause" in failure && isProviderAdapterOpenSessionError(failure.cause) + ? failure.cause + : undefined; + const requiresSignIn = openError?.requiresSignIn === true; + // A retry resolves the policy again and could run a mode the user did not pick. + const refusedMode = Predicate.isTagged(openError?.cause, "AcpPermissionModeError"); + if (input.willRetry === true && !requiresSignIn && !refusedMode) return yield* failure; yield* settleStartFailure({ signal: "provider-session-open-failure", title: "Provider session failed to open", diff --git a/apps/server/src/provider/Drivers/AntigravityDriver.test.ts b/apps/server/src/provider/Drivers/AntigravityDriver.test.ts index 3c0135f419c66..d109a6c4f7a66 100644 --- a/apps/server/src/provider/Drivers/AntigravityDriver.test.ts +++ b/apps/server/src/provider/Drivers/AntigravityDriver.test.ts @@ -459,6 +459,33 @@ it.layer(layerTest)("AntigravityDriver", (it) => { .map((request) => request.params?.value), ).toEqual(["default"]); } + // A session without agy's own Auto must not run an Auto thread in `default`. + const autoThreadId = ThreadId.make("thread-antigravity-cli-auto"); + const autoPolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: "auto", + interactionMode: "default", + cwd: workspace, + }); + const autoOpen = yield* h.instance.orchestrationAdapter + .openSession({ + threadId: autoThreadId, + providerSessionId: ProviderSessionId.make("provider-session-cli-auto"), + modelSelection, + runtimePolicy: autoPolicy, + }) + .pipe( + Effect.flatMap((autoSession) => + autoSession.ensureThread({ + threadId: autoThreadId, + modelSelection, + runtimePolicy: autoPolicy, + }), + ), + Effect.flip, + ); + expect(knownProviderFailureMessage(autoOpen)).toBe( + 'The agent does not offer its "auto" mode in this session. Choose another permission mode, then retry.', + ); }), ); diff --git a/apps/server/src/provider/Drivers/AntigravityDriver.ts b/apps/server/src/provider/Drivers/AntigravityDriver.ts index b0d11b453dc61..d16b7697dd129 100644 --- a/apps/server/src/provider/Drivers/AntigravityDriver.ts +++ b/apps/server/src/provider/Drivers/AntigravityDriver.ts @@ -357,12 +357,7 @@ export const AntigravityDriver: ProviderDriver< }); return { ...runtime, - setMode: (modeId: string) => - runtime.getModeState.pipe( - Effect.flatMap((modeState) => - runtime.setMode(antigravitySessionMode(modeId, modeState)), - ), - ), + setMode: (modeId: string) => runtime.setMode(antigravitySessionMode(modeId)), start: () => startCompatible().pipe( Effect.tapError((cause): Effect.Effect => diff --git a/apps/server/src/provider/acp/AntigravityAcpSupport.test.ts b/apps/server/src/provider/acp/AntigravityAcpSupport.test.ts index 7201d1edc0ba0..8055f030d774b 100644 --- a/apps/server/src/provider/acp/AntigravityAcpSupport.test.ts +++ b/apps/server/src/provider/acp/AntigravityAcpSupport.test.ts @@ -91,14 +91,11 @@ describe("Antigravity release gates", () => { describe("Antigravity Auto", () => { it.each([ - { offered: ["default", "accept-edits", "auto", "plan"], expected: "auto" }, - { offered: ["default", "accept-edits", "plan"], expected: "default" }, - ])("runs as $expected when the session offers $offered", ({ offered, expected }) => { - const modeState = { - currentModeId: "default", - availableModes: offered.map((id) => ({ id, name: id })), - }; - expect(antigravitySessionMode("auto", modeState)).toBe(expected); + { requested: "auto", expected: "auto" }, + { requested: "yolo", expected: "default" }, + { requested: "auto_edit", expected: "default" }, + ])("runs $requested as $expected", ({ requested, expected }) => { + expect(antigravitySessionMode(requested)).toBe(expected); }); it.each(["execute", "read"] as const)("asks about the %s requests agy's Auto sends", (kind) => { diff --git a/apps/server/src/provider/acp/AntigravityAcpSupport.ts b/apps/server/src/provider/acp/AntigravityAcpSupport.ts index 4d57305f7e0ec..5403c004ba8a9 100644 --- a/apps/server/src/provider/acp/AntigravityAcpSupport.ts +++ b/apps/server/src/provider/acp/AntigravityAcpSupport.ts @@ -28,7 +28,6 @@ import { unknownRecord, type AcpRuntimePolicy, } from "@t3tools/provider-acp/server/clientPolicy"; -import type { AcpSessionModeState } from "@t3tools/provider-acp/server/runtimeModel"; import { T3_MCP_TOOL_NAMES } from "@t3tools/shared/t3McpToolPresentation"; import packageJson from "../../../package.json" with { type: "json" }; import { normalizeAntigravitySessionUpdate } from "./AntigravityProtocol.ts"; @@ -167,19 +166,12 @@ export function antigravityPermissionMode(runtimeMode: RuntimeMode): string { } /** - * The agy CLI rejects `yolo` and `auto_edit`, and sessions without native Auto reject `auto`. - * Those stay in `default`, where it asks before acting and T3 answers from the live runtime - * policy, so permission changes apply on the next turn without a restart. + * The agy CLI rejects `yolo` and `auto_edit`; those stay in `default`, where it asks before acting + * and T3 answers from the live runtime policy, so permission changes apply on the next turn without + * a restart. Auto is always requested: a session without it fails to open, where `default` would + * ask about every command while the thread still says Auto. */ -export function antigravitySessionMode( - modeId: string, - modeState: AcpSessionModeState | undefined, -): string { - if (modeId === "auto") { - return modeState?.availableModes.some((mode) => mode.id === "auto") === true - ? "auto" - : "default"; - } +export function antigravitySessionMode(modeId: string): string { return modeId === "yolo" || modeId === "auto_edit" ? "default" : modeId; } diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 21fe311588906..88206e0a9bf1d 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -307,6 +307,19 @@ class AcpModelSelectionError extends Schema.TaggedError( } } +/** The agent refused the session mode the thread's permission level maps to. */ +class AcpPermissionModeError extends Schema.TaggedError()( + "AcpPermissionModeError", + { + mode: Schema.String, + cause: Schema.Defect(), + }, +) { + override get message(): string { + return `The agent does not offer its "${this.mode}" mode in this session. Choose another permission mode, then retry.`; + } +} + /** A prompt-steered turn spans several prompts, each reporting its own usage. */ function addAcpUsage( total: EffectAcpSchema.Usage | undefined, @@ -8290,7 +8303,15 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const policyMode = flavor.sessionModeForPolicy?.(runtimePolicy); const modeLocked = flavor.lockSessionModeToPolicy && policyMode !== undefined; if (policyMode !== undefined) { - yield* runtime.setMode(policyMode); + yield* runtime + .setMode(policyMode) + .pipe( + Effect.mapError((error) => + error._tag === "AcpRequestError" && error.code === -32602 + ? new AcpPermissionModeError({ mode: policyMode, cause: error }) + : error, + ), + ); } const modeState = yield* runtime.getModeState; if ( diff --git a/packages/provider-core/src/server/failure.ts b/packages/provider-core/src/server/failure.ts index e7d47cdb2175e..90155d6ca4508 100644 --- a/packages/provider-core/src/server/failure.ts +++ b/packages/provider-core/src/server/failure.ts @@ -129,6 +129,7 @@ function curatedCauseMessages(cause: unknown): { category: categoryMessage(), }; case "AcpModelSelectionError": + case "AcpPermissionModeError": case "ClaudeBackgroundWorkBlocksQueryReplacementError": case "ClaudeAppendSystemPromptFileError": case "ClaudeAppendSystemPromptMissingValueError":