Skip to content
23 changes: 22 additions & 1 deletion apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 },
Expand Down
6 changes: 4 additions & 2 deletions apps/mobile/src/state/threadOutboxDelivery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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: {
Expand Down
7 changes: 0 additions & 7 deletions apps/web/src/components/BranchToolbarBranchSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down
Loading