From 1403fb376cbb68eddcafada842b55c43bf0f606a Mon Sep 17 00:00:00 2001 From: Garrett Fincke Date: Fri, 6 Nov 2026 13:14:44 -0500 Subject: [PATCH 1/4] fix(server): a refused live mode switch keeps asking for approvals Co-Authored-By: Claude --- .../Adapters/ClaudeAdapterV2.test.ts | 40 ++++++++++++++++--- .../Adapters/ClaudeAdapterV2.ts | 13 ++++-- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index abb0731975..5197e8f2f2 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -5246,13 +5246,18 @@ describe("ClaudeAdapterV2 background wake turns", () => { ); it.effect.each([ - { runtimeMode: "auto", modelHasAuto: true, changes: ["auto", "plan"] }, - { runtimeMode: "full-access", modelHasAuto: true, changes: ["bypassPermissions", "plan"] }, - // Like the CLI at launch, a model without Auto runs in default. - { runtimeMode: "auto", modelHasAuto: false, changes: ["auto", "default", "plan"] }, + { runtimeMode: "auto", modelHasAuto: true, changes: ["auto", "plan"], asks: false }, + { + runtimeMode: "full-access", + modelHasAuto: true, + changes: ["bypassPermissions", "plan"], + asks: true, + }, + // Like the CLI at launch, a model without Auto runs in default, which asks. + { runtimeMode: "auto", modelHasAuto: false, changes: ["auto", "default", "plan"], asks: true }, ] as const)( "switches $runtimeMode Plan in and out on a process running a background shell (Auto: $modelHasAuto)", - ({ runtimeMode, modelHasAuto, changes }) => + ({ runtimeMode, modelHasAuto, changes, asks }) => Effect.scoped( Effect.gen(function* () { const harness = yield* makeWakeHarnessWithOptions({ @@ -5296,6 +5301,31 @@ describe("ClaudeAdapterV2 background wake turns", () => { assert.isTrue(yield* harness.hasPendingBackgroundWork); yield* harness.runtime.startTurn(turn(2, "default")); + const decision = yield* Effect.promise(() => + originalOptions!.canUseTool!( + "Bash", + { command: "git status" }, + { + signal: new AbortController().signal, + toolUseID: "tool-after-plan", + requestId: "request-after-plan", + }, + ), + ).pipe(Effect.forkScoped); + const first = yield* Effect.raceFirst( + Queue.take(harness.runtimeRequestReceipts).pipe( + Effect.map((event) => ({ type: "request", event }) as const), + ), + Fiber.join(decision).pipe(Effect.map(() => ({ type: "decision" }) as const)), + ); + assert.equal(first.type, asks ? "request" : "decision"); + if (first.type === "request") { + yield* harness.runtime.respondToRuntimeRequest({ + requestId: first.event.runtimeRequest.id, + decision: "decline", + }); + yield* Fiber.join(decision); + } yield* settle; yield* harness.runtime.startTurn(turn(3, "plan")); yield* settle; diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index b51d2ee9e2..faa25de31a 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -10460,13 +10460,20 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( // Switch Plan and the permission mode live, so a background agent survives the change. // Record the mode Claude accepted, as the init frame does for a fresh process. if (existing.queryPolicy.permissionMode !== queryPolicy.permissionMode) { - existing.permissionMode = yield* setClaudeLivePermissionMode( + const accepted = yield* setClaudeLivePermissionMode( existing, queryPolicy.permissionMode, ); - existing.openedPermissionMode = existing.permissionMode; + existing.permissionMode = accepted; + existing.openedPermissionMode = accepted; + // The default fallback forwards its approvals, so the callback must ask for them. + existing.queryPolicy = + accepted === queryPolicy.permissionMode + ? queryPolicy + : { ...queryPolicy, permissionMode: accepted, installPermissionCallback: true }; + } else { + existing.queryPolicy = queryPolicy; } - existing.queryPolicy = queryPolicy; // A denied ExitPlanMode leaves Claude in plan mode. Restore only from plan: // Auto/default may reflect a policy restriction that forbids restoring bypass. if (existing.permissionMode === "plan" && existing.openedPermissionMode !== "plan") { From 5208abe2b9c071bcd00959d538da0a3e52a83041 Mon Sep 17 00:00:00 2001 From: Garrett Fincke Date: Fri, 6 Nov 2026 13:27:44 -0500 Subject: [PATCH 2/4] fix(web): a queued message no longer hides background work from compaction Co-Authored-By: Claude --- .../web/src/components/ChatView.logic.test.ts | 64 +++++++++++++++++++ apps/web/src/components/ChatView.logic.ts | 34 +++++++--- apps/web/src/components/ChatView.tsx | 4 +- 3 files changed, 92 insertions(+), 10 deletions(-) diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index 81b6992a59..d427868aeb 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -7,6 +7,7 @@ import { snapshotComposerSend, withMessageContext, deriveAvailableRevertTurnCounts, + hasPendingProviderBackgroundWork, } from "./ChatView.logic"; import { recallCheckoutIsRepo, rememberCheckoutIsRepo } from "./ChatView.logic"; import { ProviderDriverKind, type ServerProvider } from "@t3tools/contracts"; @@ -1258,3 +1259,66 @@ describe("deriveAvailableRevertTurnCounts", () => { expect(deriveAvailableRevertTurnCounts(null).size).toBe(0); }); }); + +describe("hasPendingProviderBackgroundWork", () => { + const providerThreadId = ProviderThreadId.make("provider-thread"); + + function projection(input: { + readonly laterRun?: "queued" | "rolled_back"; + readonly agent?: boolean; + readonly command?: boolean; + readonly pullRequestWatch?: boolean; + }) { + const base = makeThreadProjectionFixture(); + const run = (ordinal: number, status: string) => ({ + id: RunId.make(`run-${ordinal}`), + ordinal, + status, + startedAt: status === "queued" ? null : "2026-10-30T14:20:00.000Z", + }); + return { + ...base, + thread: { + ...base.thread, + activeProviderThreadId: providerThreadId, + pullRequests: + input.pullRequestWatch === true + ? [{ host: "github.com", repository: "o/r", number: 1, watch: {} }] + : [], + }, + providerThreads: [ + { + id: providerThreadId, + providerInstanceId: base.thread.modelSelection.instanceId, + pendingBackgroundTasks: + input.command === true ? [{ taskId: "gate", kind: "command" }] : [], + }, + ], + runs: [ + run(1, "completed"), + ...(input.laterRun === undefined ? [] : [run(2, input.laterRun)]), + ], + turnItems: + input.agent === true + ? [{ id: "item-agent", type: "subagent", status: "running", runId: RunId.make("run-1") }] + : [], + } as unknown as ReturnType; + } + + it.each([ + { name: "a running background agent", input: { agent: true }, expected: true }, + { + name: "a background agent behind a queued message", + input: { agent: true, laterRun: "queued" }, + expected: true, + }, + { + name: "a listed command after a rollback", + input: { command: true, laterRun: "rolled_back" }, + expected: false, + }, + { name: "only a watched pull request", input: { pullRequestWatch: true }, expected: false }, + ] as const)("$name: $expected", ({ input, expected }) => { + expect(hasPendingProviderBackgroundWork(projection(input))).toBe(expected); + }); +}); diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts index eb769877e7..75bd811de7 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -1555,11 +1555,8 @@ export function presentThreadShellFromProjection( }; } -/** Background work the provider still runs after the latest run ended, plus watched pull requests. */ -export function deriveThreadPendingBackgroundTasks( - projection: ThreadStructure | null, - options?: { readonly includePullRequestWatches?: boolean }, -) { +/** Background work the provider still runs after the latest run ended. */ +export function deriveThreadPendingBackgroundTasks(projection: ThreadStructure | null) { if (projection === null) return []; const sessionError = projection.providerSessions.findLast( @@ -1575,13 +1572,34 @@ export function deriveThreadPendingBackgroundTasks( turnItems: projection.turnItems, activeProviderThreadId: projection.thread.activeProviderThreadId, runs: projection.runs, - ...(options?.includePullRequestWatches === false - ? {} - : { pullRequests: projection.thread.pullRequests }), + pullRequests: projection.thread.pullRequests, }), ]; } +/** + * Whether the provider still runs background agents or commands for the thread's latest run. + * Pull request watches do not count, and a queued run has not taken over the session yet. + */ +export function hasPendingProviderBackgroundWork( + projection: Pick, +): boolean { + const latestRun = projection.runs.reduce( + (latest, run) => + run.status === "queued" || (latest !== null && latest.ordinal > run.ordinal) ? latest : run, + null, + ); + return ( + derivePendingBackgroundWork({ + latestRun, + providerThreads: projection.providerThreads, + turnItems: projection.turnItems, + activeProviderThreadId: projection.thread.activeProviderThreadId, + runs: projection.runs, + }).length > 0 + ); +} + /** * Run ordinals the conversation can be reverted to: those whose provider turn belongs to the * active provider thread, plus 0 for the thread start. Empty without a live provider session diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 7df9aa72ea..13abfdc6ed 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -11,6 +11,7 @@ import { announcedRun, isDetailBehindShell } from "./chat/threadActivityAnnounce import { deriveAvailableRevertTurnCounts, deriveThreadPendingBackgroundTasks, + hasPendingProviderBackgroundWork, presentThreadShellFromProjection, } from "./ChatView.logic"; import { usePullRequestDraftThread } from "./chat/usePullRequestDraftThread"; @@ -2494,8 +2495,7 @@ export default function ChatView(props: ChatViewProps) { !hasHeldQueuedRuns && multipleModelSelections === null && // Background agents keep the session live and report into its next turn. - deriveThreadPendingBackgroundTasks(serverProjection, { includePullRequestWatches: false }) - .length === 0 && + (serverProjection === null || !hasPendingProviderBackgroundWork(serverProjection)) && shouldOfferResumeCompaction({ provider: selectedProvider, usedTokens: activeContextWindow.usedTokens, From 2df7edd6e717ccf64ca359de2f4e49c52ef6fc9f Mon Sep 17 00:00:00 2001 From: Garrett Fincke Date: Fri, 6 Nov 2026 13:31:39 -0500 Subject: [PATCH 3/4] test(server): Full access Plan launches with the bypass allowance Co-Authored-By: Claude --- .../src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 5197e8f2f2..0dbe37d2f3 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -5296,6 +5296,11 @@ describe("ClaudeAdapterV2 background wake turns", () => { yield* harness.runtime.startTurn(turn(1, "plan")); const originalOptions = harness.getOpenedOptions(); assert.equal(originalOptions?.permissionMode, "plan"); + // Leaving Plan for Full access needs the bypass allowance from launch. + assert.equal( + originalOptions?.allowDangerouslySkipPermissions, + runtimeMode === "full-access" ? true : undefined, + ); yield* harness.offerAndWait(wakeTaskStarted); yield* settle; assert.isTrue(yield* harness.hasPendingBackgroundWork); From 2823e11bfa7f1c48e4b7deaf44dbc588d9db4d1e Mon Sep 17 00:00:00 2001 From: Garrett Fincke Date: Fri, 6 Nov 2026 13:42:22 -0500 Subject: [PATCH 4/4] fix(server): turns follow the permission mode Claude accepted Co-Authored-By: Claude --- .../Adapters/ClaudeAdapterV2.test.ts | 84 +++++++++++++++++++ .../Adapters/ClaudeAdapterV2.ts | 28 ++++--- 2 files changed, 101 insertions(+), 11 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 0dbe37d2f3..87f6429f10 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -5352,6 +5352,90 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + it.effect("asks for approvals after restoring Claude's own Plan falls back to default", () => + Effect.scoped( + Effect.gen(function* () { + const harness = yield* makeWakeHarnessWithOptions({ + setPermissionMode: (mode) => + mode === "auto" + ? Effect.fail( + new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({ + method: "setPermissionMode", + cause: new Error("auto mode is not available for this model"), + }), + ) + : Effect.void, + }); + const now = yield* DateTime.now; + const turn = (ordinal: number) => + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now, + attemptId: RunAttemptId.make(`attempt-restore-plan:${ordinal}`), + text: `Request ${ordinal}`, + attachments: [], + providerTurnOrdinal: ordinal, + runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: "auto", + interactionMode: "default", + cwd: "/workspace", + }), + }); + yield* harness.runtime.startTurn(turn(1)); + // Claude enters Plan on its own, and the model no longer offers Auto to return to. + yield* harness.offerAndWait( + claudeSdkFrame({ + type: "system", + subtype: "status", + status: null, + permissionMode: "plan", + uuid: "restore-enter-plan", + session_id: WAKE_NATIVE_SESSION, + }), + ); + yield* harness.offerAndWait(turnOneResult); + yield* Queue.take(harness.terminalReceipts); + + yield* harness.runtime.startTurn(turn(2)); + assert.deepEqual(harness.permissionModeChanges, ["auto", "default"]); + const decision = yield* Effect.promise(() => + harness.getOpenedOptions()!.canUseTool!( + "Bash", + { command: "git status" }, + { + signal: new AbortController().signal, + toolUseID: "tool-after-restore", + requestId: "request-after-restore", + }, + ), + ).pipe(Effect.forkScoped); + const first = yield* Effect.raceFirst( + Queue.take(harness.runtimeRequestReceipts).pipe( + Effect.map((event) => ({ type: "request", event }) as const), + ), + Fiber.join(decision).pipe(Effect.map(() => ({ type: "decision" }) as const)), + ); + assert.equal(first.type, "request"); + if (first.type !== "request") return; + yield* harness.runtime.respondToRuntimeRequest({ + requestId: first.event.runtimeRequest.id, + decision: "decline", + }); + yield* Fiber.join(decision); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ), + ); + it.effect("re-sends the model on the turn after a live selection change fails", () => Effect.scoped( Effect.gen(function* () { diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index faa25de31a..d35f0cf76b 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -1466,6 +1466,18 @@ function setClaudeLivePermissionMode(live: ClaudeLiveQueryContext, mode: Permiss ); } +// Turns follow the mode Claude accepted. A fallback to default forwards its approvals, so the +// callback must ask for them even when the requested mode would not. +function claudeEffectiveLiveQueryPolicy(live: ClaudeLiveQueryContext): ClaudeRuntimeQueryPolicy { + return live.openedPermissionMode === live.queryPolicy.permissionMode + ? live.queryPolicy + : { + ...live.queryPolicy, + permissionMode: live.openedPermissionMode, + installPermissionCallback: true, + }; +} + // A delegated child is its own SDK session, so Claude Code gives it the main-conversation cache // TTL. When the user sets Claude's subagentPromptCacheTtl, the child follows it like a native // subagent would. An explicit CLAUDE_CODE_PROMPT_CACHE_TTL on the instance still wins. @@ -10460,20 +10472,13 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( // Switch Plan and the permission mode live, so a background agent survives the change. // Record the mode Claude accepted, as the init frame does for a fresh process. if (existing.queryPolicy.permissionMode !== queryPolicy.permissionMode) { - const accepted = yield* setClaudeLivePermissionMode( + existing.permissionMode = yield* setClaudeLivePermissionMode( existing, queryPolicy.permissionMode, ); - existing.permissionMode = accepted; - existing.openedPermissionMode = accepted; - // The default fallback forwards its approvals, so the callback must ask for them. - existing.queryPolicy = - accepted === queryPolicy.permissionMode - ? queryPolicy - : { ...queryPolicy, permissionMode: accepted, installPermissionCallback: true }; - } else { - existing.queryPolicy = queryPolicy; + existing.openedPermissionMode = existing.permissionMode; } + existing.queryPolicy = queryPolicy; // A denied ExitPlanMode leaves Claude in plan mode. Restore only from plan: // Auto/default may reflect a policy restriction that forbids restoring bypass. if (existing.permissionMode === "plan" && existing.openedPermissionMode !== "plan") { @@ -10481,6 +10486,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( existing, existing.openedPermissionMode, ); + existing.openedPermissionMode = existing.permissionMode; } return existing; } @@ -10792,7 +10798,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const querySession = yield* openQuery(turnInput, nativeThreadId); const context: ActiveClaudeTurnContext = { input: turnInput, - queryPolicy: querySession.queryPolicy, + queryPolicy: claudeEffectiveLiveQueryPolicy(querySession), nativeThreadId, nativeTurnId, nativeMessageCursor: null,