From 2d3543bed4cfa10acaafb0c2fa2c2d82f26ae904 Mon Sep 17 00:00:00 2001 From: LHagfoss Date: Wed, 25 Nov 2026 22:25:41 +0100 Subject: [PATCH] fix(desktop): let a queued turn request compaction after a requested one --- .../src/state/autoCompact.store.test.ts | 30 +++++++++++++++++++ apps/desktop/src/state/autoCompactRun.ts | 8 +++++ apps/desktop/src/state/promptRun.ts | 6 +++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/apps/desktop/src/state/autoCompact.store.test.ts b/apps/desktop/src/state/autoCompact.store.test.ts index 8ce0eff0d..d4a4bf48a 100644 --- a/apps/desktop/src/state/autoCompact.store.test.ts +++ b/apps/desktop/src/state/autoCompact.store.test.ts @@ -117,6 +117,36 @@ describe("requested compaction", () => { expect(isRunning(thread)).toBe(false); }); + test("a queued turn that runs after a requested compaction can request the next one", async () => { + const id = await openClaudeThread(10); + const first = runPrompt(id, "yolo, finish this task first", emptyExtras); + expect(requestCompaction(id, false)).toBe(true); + void runPrompt(id, "yolo, the queued wake", emptyExtras); + await until( + () => + isRunning(useApp.getState().threads[id]) && + (useApp + .getState() + .threads[id]?.items.some((item) => item.kind === "user" && item.text === "yolo, the queued wake") ?? + false), + 10_000, + ); + + expect(requestCompaction(id, true)).toBe(true); + await first; + const thread = useApp.getState().threads[id]!; + const compactions = thread.items.filter( + (item) => item.kind === "notice" && item.text.startsWith("Context compacted"), + ); + expect(compactions).toHaveLength(2); + await until( + () => + useApp.getState().threads[id]?.items.some((item) => item.kind === "user" && item.continued) ?? false, + 10_000, + ); + await cancel(id); + }, 30_000); + test("continues once when requested", async () => { const id = await openClaudeThread(10); expect(requestCompaction(id, true)).toBe(true); diff --git a/apps/desktop/src/state/autoCompactRun.ts b/apps/desktop/src/state/autoCompactRun.ts index 7c08ddff3..f280f2657 100644 --- a/apps/desktop/src/state/autoCompactRun.ts +++ b/apps/desktop/src/state/autoCompactRun.ts @@ -287,6 +287,14 @@ export async function runRequestedCompaction(id: string): Promise { } } +/** + * The requested `/compact` turn itself ended. `runPrompt` goes on to start the thread's queue before it + * returns, and a turn from that queue may ask for its own compaction. + */ +export function requestedCompactionEnded(id: string): void { + requestedCompactionsRunning.delete(id); +} + export function cancelRequestedCompaction(id: string): void { requestedCompactions.delete(id); } diff --git a/apps/desktop/src/state/promptRun.ts b/apps/desktop/src/state/promptRun.ts index 602be5510..52a8b56d4 100644 --- a/apps/desktop/src/state/promptRun.ts +++ b/apps/desktop/src/state/promptRun.ts @@ -17,6 +17,7 @@ import { uploadPromptImages } from "./attachmentTransfer"; import { beginAutoCompactIdle, cancelRequestedCompaction, + requestedCompactionEnded, runRequestedCompaction, scheduleAutoCompact, } from "./autoCompactRun"; @@ -351,7 +352,10 @@ export async function runPrompt( ...(options.requestedCompaction && previousQueueHold ? { queueHeld: true } : {}), ...(rawFailure !== failure ? { errorDetail: rawFailure } : {}), })); - if (options.requestedCompaction) updateThread(id, (t) => settleCompaction(t, null)); + if (options.requestedCompaction) { + updateThread(id, (t) => settleCompaction(t, null)); + requestedCompactionEnded(id); + } // Steers the turn never read wait as turns of their own, and carry the links they came from. if (!options.autoCompact) requeueUnreadSteers(id, unused); // The stop is settled first: a turn it stopped is reported as the stop, not twice.