diff --git a/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts b/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts index 49663f2340f47..f46ffe32b956c 100644 --- a/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts +++ b/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts @@ -17,6 +17,7 @@ import { threadOutboxRetryDelayMs, type QueuedThreadMessage } from "./thread-out const state = vi.hoisted(() => ({ draftText: "Keep this draft separate", cleanups: [] as Array<() => void>, + hexCount: 0, delivery: null as { readonly drain: () => void; readonly dispose: () => void; @@ -122,7 +123,7 @@ vi.mock("./git", async () => ({ }, })); vi.mock("../lib/modelOptions", () => ({ isModelSelectionUnavailable: state.modelUnavailable })); -vi.mock("../lib/uuid", () => ({ uuidv4: () => "uuid", randomHex: () => "abcd" })); +vi.mock("../lib/uuid", () => ({ uuidv4: () => "uuid", randomHex: () => `hex${++state.hexCount}` })); vi.mock("../lib/attachmentUpload", () => ({ prepareTurnAttachments: state.prepare })); vi.mock("./composer-attachment-uploads", () => ({ uploadComposerAttachmentShared: vi.fn() })); vi.mock("./thread-outbox", async () => { @@ -431,6 +432,26 @@ describe("queued task operation access", () => { expect(remaining()).toEqual([queued]); }); + it("retries a refused runtime-mode change under a new command id", async () => { + setOperateGrant("secondary", true); + const queued = message({ runtimeMode: "approval-required" }); + await state.manager.enqueue(queued); + state.runtime.mockResolvedValueOnce( + AsyncResult.failure(Cause.fail(new Error("Background work is still running."))), + ); + await runDrain(); + expect(remaining()).toEqual([queued]); + + // The server keeps the refusal under the first id, so reusing it could never succeed. + state.cleanups.splice(0).forEach((cleanup) => cleanup()); + await runDrain(); + const [first, second] = state.runtime.mock.calls.map(([call]) => call.input.commandId); + expect(first).toMatch(/^command:runtime-mode:/); + expect(second).toMatch(/^command:runtime-mode:/); + expect(second).not.toBe(first); + expect(remaining()).toEqual([]); + }); + it.each([ { isCreation: false, accepted: false }, { isCreation: true, accepted: true }, diff --git a/apps/mobile/src/state/threadOutboxDelivery.ts b/apps/mobile/src/state/threadOutboxDelivery.ts index ca7fbf4fda337..db24a752c7c97 100644 --- a/apps/mobile/src/state/threadOutboxDelivery.ts +++ b/apps/mobile/src/state/threadOutboxDelivery.ts @@ -216,8 +216,10 @@ function findCreationProject( ); } +// The server keeps a refusal under its command id, and refuses a mode change while background +// work runs, so each attempt needs its own id. Both callers skip when the mode already matches. function settingsCommandId(message: QueuedThreadMessage, setting: string): CommandId { - return CommandId.make(`${message.commandId}:${setting}`); + return CommandId.make(`${message.commandId}:${setting}:${randomHex(8)}`); } /** Outbox or draft storage refused a step; the step decides what that means for the message. */ @@ -977,7 +979,7 @@ export function createThreadOutboxDelivery(): { const compactionResult = yield* deliveryCommand(threadEnvironment.startTurn, { environmentId: queuedMessage.environmentId, input: { - commandId: settingsCommandId(queuedMessage, "compact"), + commandId: CommandId.make(`${queuedMessage.commandId}:compact`), creationSource: "mobile", threadId: queuedMessage.threadId, message: { diff --git a/apps/web/src/components/BranchToolbarBranchSelector.tsx b/apps/web/src/components/BranchToolbarBranchSelector.tsx index 4bf062b27d93f..3a461129eb033 100644 --- a/apps/web/src/components/BranchToolbarBranchSelector.tsx +++ b/apps/web/src/components/BranchToolbarBranchSelector.tsx @@ -129,7 +129,6 @@ export function BranchToolbarBranchSelector({ const composerFloatingLayerProps = useComposerMenuProps(); const canWriteSourceControl = useEnvironmentScope(environmentId, AuthSourceControlWriteScope); const canOperateThread = useEnvironmentScope(environmentId, AuthOrchestrationOperateScope); - const stopThreadSession = useAtomCommand(threadEnvironment.stopSession, "thread session stop"); const updateThreadMetadata = useAtomCommand( threadEnvironment.updateMetadata, "thread metadata update", @@ -217,12 +216,6 @@ export function BranchToolbarBranchSelector({ ); return false; } - if (latest.runtime && worktreePath !== latest.worktreePath) { - void stopThreadSession({ - environmentId, - input: { threadId: activeThreadId }, - }); - } if (hasServerThread) { void (automatic ? updateEmptyThreadMetadata : updateThreadMetadata)({ environmentId,