Skip to content

fix(agent-ipc): hold the session operation across manual compaction - #2290

Merged
vastsa merged 4 commits into
vastsa:mainfrom
SeanLiu326:fix/compact-session-operation
Nov 15, 2026
Merged

vastsa merged 4 commits into
vastsa:mainfrom
SeanLiu326:fix/compact-session-operation

Conversation

@SeanLiu326

Copy link
Copy Markdown
Contributor

Fixes #2289.

What

agentCompact held no session operation: its activeTurns busy check went stale the moment it passed, and the settings read, session detail, and launch resolution between the check and agent.compact left a window where a prompt could start a turn the compaction then ran against mid-stream.

The change

Same contract as agentPrompt and agentAbort: acquire the session operation before the busy check, release in finally. The setup body moves to a compactSession helper so the handler reads as guard → work → release; behavior is otherwise identical, including the transport-timeout reconciliation.

A compaction in flight now queues prompts and aborts for its duration, which is the existing lock semantic for every other session operation.

@vastsa vastsa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The normal-path lock closes the launch-resolution admission window, but its lifetime still does not match the runtime operation after a transport deadline. I executed the production compact handler/helper (TypeScript-transpiled, controlled Host/sidecar boundary) on an integration candidate with main 30dd376. Normal completion retains the operation until the sidecar settles. With sidecar RPC timeout while compaction is still running and session.get still returning the old checkpoint, compactSession throws and finally releases the operation; a waiting prompt immediately acquires it while compaction is still running. The real transport in packages/host-runtime/src/agent-sidecar.ts rejects/removes the pending request on timeout without cancelling runtime work.

Please make the post-timeout ownership/settlement policy explicit and keep prompt admission serialized until authoritative completion/cancellation, or provide a runtime-owned equivalent that safely queues/retries admission. Do not simply increase the RPC deadline. Add deterministic handler-level regression coverage for delayed launch, timeout-before-checkpoint, failure release, and independent sessions. This PR currently adds no behavioral tests; existing context-compaction tests pass 13/13 but do not cover this concurrency contract.

Also, the issue's claim of simultaneous streaming/context corruption is stronger than the current code supports: compactManually checks streaming/compactionInProgress and prompt's assertNotRunning checks getStatus().isRunning (including compaction). Those guards can reject the competing operation; this diagnostic establishes premature admission/AGENT_BUSY risk, not proven transcript corruption. Please describe and test the actual user-visible queue/rejection behavior. No full local Electron E2E run or approval yet.

@vastsa

vastsa commented Nov 13, 2026

Copy link
Copy Markdown
Owner

The race is real (agentCompact checks activeTurns once, then awaits settings/session/launch before agent.compact). But holding the session operation for the whole compaction regresses Stop: the renderer shows Stop while compacting (transcript-slice.ts sets isRunning), and today agentAbort → agent.abort → runtime.abort() cancels a manual compaction (runtime.ts sets compactionAborted and aborts compactionAbort). agentAbort acquires the same operation, so with this change Stop waits until the summarization finishes. Please keep admission protected without blocking abort — e.g. hold the operation through the busy check and launch resolution, mark the session as compacting in main (so agentPrompt treats it as busy), release the operation before awaiting the sidecar compaction, and clear the mark in finally. Please also add a regression test for both the prompt race and Stop-during-compaction.

agentCompact now holds the session operation across admission (busy check, settings, session read, launch resolution) so a prompt submitted during setup queues behind it instead of beginning a turn the compaction would run against mid-stream. The operation is released before awaiting the sidecar compaction and a compacting-sessions mark takes over admission: queued prompts are refused with AGENT_BUSY while the mark holds, and agentAbort acquires the released operation immediately, so Stop still cancels a running compaction instead of waiting for the summarization. The mark clears when the sidecar call settles, including the transport-timeout reconciliation path.

Review follow-up to vastsa#2290: the previous draft held the operation for the whole compaction, which made Stop wait behind the summarization. Regression tests execute the production handler: the queued prompt is refused by the mark, abort completes while the compaction is in flight, and an idle compaction still refuses a session with an active turn.
@SeanLiu326
SeanLiu326 force-pushed the fix/compact-session-operation branch from c3fbd55 to bf22dbf Compare November 15, 2026 14:04
@vastsa

vastsa commented Nov 15, 2026

Copy link
Copy Markdown
Owner

复查 bf22dbf:原 prompt setup race / Stop 三项测试已独立通过,释放锁再等待 sidecar 的方向解决了先前 Stop 阻塞。但 compact 自身没有在持锁 admission 中检查 compactingSessions:第一次压缩仍 pending 时,第二次 agentCompact 也能 add 同一个 Set 项;第二次 sidecar 报 already compacting 并进入 finally 后,会删除第一次仍持有的标记,接着 prompt 越过 admission。已在提交的 production registerAgentIpc harness 中追加此场景复现:3 原测试通过,新增断言 AGENT_BUSY 失败(拿到后续 sentinel,说明已通过 guard)。请在同一锁内同时检查 activeTurns 与 compactingSessions,在成功取得该压缩所有权后才允许 finally 清标记;补上重复 compact 被拒且不清除首次 guard 的回归,再验证 Stop 仍可取消。暂不合入。

SeanLiu326 added a commit to SeanLiu326/PI-Desktop that referenced this pull request Nov 15, 2026
sessionDelete now mirrors session.moveProject's contract: acquire the session operation, then check activeTurns, then cascade. The check-after-acquire ordering closes the queued-start window — a prompt holding the operation registers its turn and releases it, and the delete still refuses that turn (issue vastsa#2291).

The sidebar's row menu and batch delete pre-check running sessions and surface a translated notice (nav.deleteRunningSessionBlocked, all locales), and project-slice's deleteSession renders the main-side AGENT_BUSY backstop through the same message instead of the raw IPC error.

Branch rebuilt from latest main so it carries only the delete fix; the agentCompact rework lives on fix/compact-session-operation (vastsa#2290). Regression tests execute the production handler: busy refusal before any host cascade, the prompt-held-operation repro, operation held for the delete's duration, and a clean idle delete.
@SeanLiu326

Copy link
Copy Markdown
Contributor Author

Reworked per your direction — head bf22dbf3f, rebuilt from latest main.

What changed in agentCompact:

  1. The session operation is acquired before the busy check and held through the settings read, session read, and launch resolution (the admission window from agentCompact races a prompt submitted during its launch resolution (no session operation held) #2289).
  2. The session is marked compacting in main (compactingSessions, closure-local to registerAgentIpc) while the operation is still held.
  3. The operation is released before awaiting agent.compact, so agentAbort acquires it immediately — Stop now cancels a running compaction exactly as before this PR (verified: runtime.abort() still drives compactionAbort).
  4. The mark clears in a finally around the sidecar call, so it also clears on the transport-timeout reconciliation path (return { accepted: true } included).

agentPrompt sees the mark inside its own operation window: the check runs after acquiring the session operation, not before — a prompt that checks before acquiring can pass while the compaction is still in its setup window (the mark isn't set yet) and then queue behind the operation; once the compaction releases, the queued prompt would slip through unmarked. Checking after the acquire closes that.

Regression tests (agent-compact-session-operation.test.mjs, executing the production handler with a real promise-tail operation):

  • a prompt issued during the compaction's setup queues behind the operation and is then refused with AGENT_BUSY by the mark (no turn host calls behind the compaction);
  • agentAbort completes while agent.compact is still in flight, and after the compaction settles a prompt passes admission again (sentinel past the mark check);
  • an idle compaction still refuses a session with an active turn.

Desktop tsc --noEmit clean.

… guard

agentCompact now checks compactingSessions inside the same locked admission as activeTurns, so a second compact behind a still-pending first one is refused before it can reach the sidecar or re-add the mark. The clearing side of the mark is ownership-guarded: only the call that won compaction ownership clears it in its finally, so a rejected duplicate (or any other path) can never un-guard the first compaction's admission.

Re-review follow-up on vastsa#2290: the duplicate's failure path previously deleted the first compaction's mark and let prompts slip past admission. Regression: the duplicate is refused with AGENT_BUSY without a second sidecar call, the first mark still guards prompts, Stop still cancels the pending compaction, and admission reopens only when the owning call settles.
@SeanLiu326

Copy link
Copy Markdown
Contributor Author

Fixed per your re-review — head 2b20dc8a3 (rebased onto latest main including #2292's landing).

  • agentCompact now checks compactingSessions inside the same locked admission as activeTurns, so a second compact behind a still-pending first one is refused with AGENT_BUSY before it can reach the sidecar or re-add the Set item.
  • The mark's clearing side is ownership-guarded: ownsCompaction is set only where the admission succeeded, and the finally clears the mark only when it is set — a rejected duplicate (or any other failure path) can never delete the mark the first compaction still holds.

Regression added (agent-compact-session-operation.test.mjs, now 4/4): the duplicate is refused at admission with no second sidecar call, the first mark still guards prompts afterwards, Stop still cancels the pending compaction while the mark holds, and admission reopens only when the owning call settles. Desktop tsc --noEmit clean.

@vastsa

vastsa commented Nov 15, 2026

Copy link
Copy Markdown
Owner

复查 2b20dc8:重复 compact 的 locked admission 检查与 ownsCompaction 清理保护已补齐上次指出的问题。独立执行 production registerAgentIpc harness 4/4 通过;与当前 main 已合入的 session delete guard 一起执行共 8/8 通过,覆盖 setup 排队 prompt、重复 compact 不清除首个 guard、Stop 不等待压缩、settle 后恢复准入与删除锁顺序。测试仅将 typescript import 指向本机已安装版本以修复隔离 worktree 的依赖解析,没有改产品代码或断言。当前还需完成最新集成候选的相关 E2E,且远端 CI 正在运行;本条不作为最终批准或合入。

@vastsa vastsa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最终复查:原 setup admission race 与 Stop 阻塞、重复 compact 清除首个 guard 的问题均有直接回归证据;当前改动仅限主进程 admission/ownership 及测试,未改协议或持久化。当前 head 2b20dc8 已包含 main 9ff4a9a;GitHub 集成 f857cec 的 tree 50146bdc 与测试 head 完全一致。production handler compact + session delete 回归 8/8 通过;重新 bundle 后 production sidecar context-usage-compaction 3/3 通过;desktop build 通过。真实 Electron composer 测试已输出 fullComposerSubmissionAndOverflow/scratchBytesVerified 成功,含异步 compact 五种草稿场景,但整个脚本随后在 history prepare 阶段超时;main 对照同样在 line450 超时,不能宣称完整本地 Electron E2E 通过。该脚本不执行此 PR 主进程 compact handler,主进程并发保障由上述8项直接生产 handler 测试覆盖。远端五项 Actions(含 Electron E2E、Windows)全绿,Vercel 账户 blocked 属既有部署账户问题。本次据此允许 SHA-guard 合入。

@vastsa
vastsa merged commit 579f52e into vastsa:main Nov 15, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

agentCompact races a prompt submitted during its launch resolution (no session operation held)

2 participants