Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(agent-ipc): refuse duplicate compaction and keep ownership of the guard

liuzixiang2b20dc8a32db
diff --git a/apps/desktop/electron/main/ipc/agent-ipc.ts b/apps/desktop/electron/main/ipc/agent-ipc.ts
index 4256c0d5d..1b1d77a7f 100644
--- a/apps/desktop/electron/main/ipc/agent-ipc.ts
+++ b/apps/desktop/electron/main/ipc/agent-ipc.ts
@@ -692,6 +692,7 @@ export function registerAgentIpc({
// queues behind it instead of starting a turn the compaction would run
// against mid-stream (issue #2289).
const releaseSessionOperation = await acquireSessionOperation(req.sessionId);
+ let ownsCompaction = false;
let detail: { session?: any };
let launch: any;
try {
@@ -700,6 +701,14 @@ export function registerAgentIpc({
errorCode: ErrorCodes.AGENT_BUSY,
});
}
+ // A second compact behind a still-pending first one must not reach the
+ // sidecar: its failure path would clear the mark the first compaction
+ // still holds and un-guard admission (issue #2289 re-review).
+ if (compactingSessions.has(req.sessionId)) {
+ throw Object.assign(new Error("Session is already compacting"), {
+ errorCode: ErrorCodes.AGENT_BUSY,
+ });
+ }
const settings = await host.call<any>("settings.get");
detail = await host.call<{ session?: any }>("session.get", {
id: req.sessionId,
@@ -719,6 +728,7 @@ export function registerAgentIpc({
// released operation is what keeps Stop live — agentAbort acquires the
// same operation and must not wait for the summarization to finish.
compactingSessions.add(req.sessionId);
+ ownsCompaction = true;
} finally {
releaseSessionOperation();
}
@@ -749,7 +759,11 @@ export function registerAgentIpc({
});
return result;
} finally {
- compactingSessions.delete(req.sessionId);
+ // Only the call that won compaction ownership may clear the mark: a
+ // rejected duplicate must never un-guard the first compaction.
+ if (ownsCompaction) {
+ compactingSessions.delete(req.sessionId);
+ }
}
});
diff --git a/apps/desktop/test/agent-compact-session-operation.test.mjs b/apps/desktop/test/agent-compact-session-operation.test.mjs
index 79b843f2f..646f9d794 100644
--- a/apps/desktop/test/agent-compact-session-operation.test.mjs
+++ b/apps/desktop/test/agent-compact-session-operation.test.mjs
@@ -216,3 +216,57 @@ test("an idle compaction still refuses a session with an active turn", async ()
(error) => error.errorCode === ErrorCodes.AGENT_BUSY,
);
});
+
+test("a duplicate compact is refused at admission and never clears the first guard", async () => {
+ let releaseCompact;
+ let compactCalls = 0;
+ const h = buildHarness({
+ hostCall: async (method) => {
+ if (method === "session.get") return { session: { id: "s1", compactionRecordId: "c0" } };
+ return {};
+ },
+ sidecarCall: async (method) => {
+ if (method === "agent.compact") {
+ compactCalls += 1;
+ await new Promise((resolve) => { releaseCompact = resolve; });
+ return { ok: true };
+ }
+ if (method === "agent.abort") return { ok: true };
+ throw new Error(`unexpected sidecar call: ${method}`);
+ },
+ });
+
+ const first = compact(h, { sessionId: "s1" });
+ await new Promise((resolve) => setImmediate(resolve));
+ assert.equal(typeof releaseCompact, "function", "the first compaction is pending");
+
+ // The duplicate must be refused inside the locked admission — it must not
+ // reach the sidecar, and its failure path must not clear the first
+ // compaction's mark (issue #2289 re-review).
+ await assert.rejects(
+ compact(h, { sessionId: "s1" }),
+ (error) => error.errorCode === ErrorCodes.AGENT_BUSY &&
+ /already compacting/.test(error.message),
+ );
+ assert.equal(compactCalls, 1, "the sidecar saw only the first compaction");
+
+ // The first compaction still guards admission for prompts.
+ const guarded = await prompt(h, { sessionId: "s1", content: "hello" }).catch(
+ (error) => error,
+ );
+ assert.equal(guarded.errorCode, ErrorCodes.AGENT_BUSY);
+ assert.match(guarded.message, /compacting/);
+
+ // Stop still cancels the pending compaction while the mark holds.
+ const stopped = await abort(h, { sessionId: "s1" });
+ assert.deepEqual(stopped, { ok: true });
+
+ releaseCompact();
+ await first;
+
+ // Ownership released only by the owning call: prompts pass admission again.
+ const admitted = await prompt(h, { sessionId: "s1", content: "hello" }).catch(
+ (error) => error,
+ );
+ assert.match(admitted.message, /sentinel/);
+});