fix(coding-agent): serialize post-compaction continuation on run settlement - #1844
Closed
snimu wants to merge 3 commits into
Closed
fix(coding-agent): serialize post-compaction continuation on run settlement#1844snimu wants to merge 3 commits into
snimu wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 7ec9bfa. Configure here.
…tion and stale runners - Publish _compactionOperation from _runAutoCompaction so the post-compaction continuation runner (and other waiters) serialize against auto/threshold/overflow/requested compaction, not just manual compact(). - Identity-guard the success path of the continuation runner so a cancelled runner cannot forget continuation messages that a replacement settlement still owns (busy/error paths already did).
…ction continue A refine() call could enter its apply phase (which disconnects the agent event listener) between the runner's top-of-loop _waitForRefineIdle() and agent.continue(), losing the continuation's messages. Recheck _refineInFlight under the commit fence like pauses/_compactionOperation; the path from that recheck to agent.continue() is synchronous, mirroring the final refine barrier used by normal turn dispatch.
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

What was wrong
After an automatic compaction, the session decided whether it could continue by re-checking every 100ms on a recursive timer. That timer raced actually-starting runs: it could fire between "check" and "act", collide with a run that had just started, and it woke the process ten times a second during long work for no reason.
The fix
How it's verified
Focused queue/compaction suites green; the goal-continuation-quiescence suite (PR #1610) passes 7/7 untouched — same semantics. Full CI-style suite compared against the stack base: identical failure names (pre-existing environment noise only). Reviewer independently traced the fence/settlement flow, verified run-ownership closure, and re-ran the noisy suite at base in a throwaway worktree to confirm zero delta. Two-model implement/review loop.
Stacked on #1708 (test the whole stack at the leaf; merge base-first).
Note: intentionally no Linear ticket for this cleanup stack, so that check stays red.
Note
Medium Risk
Changes session loop timing and concurrency around compaction and
agent.continue(), which can affect autonomous runs and headless idle semantics; behavior is heavily tested but race surface is non-trivial.Overview
Removes the 100ms timer that used to defer resuming the agent after compaction, so continuation now runs as soon as readiness checks pass instead of polling ten times per second during long work.
Post-compaction continuation is reworked around a settlement token (
continueAfterSessionInputplus identity checks) so cancelled or rescheduled continuations cannot be driven by a stale runner. The runner loops until it can safely callagent.continue(), waiting on agent idle, retries, refine, queued-work pauses, and an active_compactionOperation(now tracked for auto-compaction as well as manualcompact()). Session-input pump work can still run first; an empty resume can still continue whencontinueAfterSessionInputis set.Tests drop fake-timer advancement and add settlement-driven cases (compaction in flight, refine apply, busy retry, cancellation/replacement).
Reviewed by Cursor Bugbot for commit 874a84e. Bugbot is set up for automated code reviews on this repo. Configure here.
Note
Serialize post-compaction continuation on run settlement in
AgentSessionsetTimeoutdelay for post-compaction continuation; the runner now executes immediately once all real idle conditions are met (agent idle, retry completion, refine idle, queued-work pause release, and active compaction operation completion).continueAfterSessionInputtoPostCompactionContinuationSettlementso the runner can decide whether to proceed after session input is processed or defer to the session input pump._compactionOperationpromise fencing in bothcompactand_runAutoCompactionto hold off continuation until compaction fully completes, then schedules the session input pump afterward._runScheduledPostCompactionContinueto loop on settlement identity, correctly retry after busy errors, and isolate cancellations so late completions from cancelled runs cannot forget replacement messages or reject newer waiters.agent.continue()calls; tests in agent-session-compaction.test.ts and agent-session-queue.test.ts were rewritten to use deferred-based sequencing instead of timers.Macroscope summarized 874a84e.
Linear: ENG-5651
Note: trivial textual overlap with #1859 (both add cleanup lines to the same
finallyblock in_runAutoCompaction; both lines should survive). Whichever PR merges second will be refreshed against main.