Repository navigation
fix(acp-server): forward autonomous turns and hold queued prompts - #5271
nitishagar wants to merge 1 commit into
Conversation
Assign a promptId to every ACP prompt and correlate turn.started, prompt.completed and prompt.aborted with the waiting driver, so prompts queued behind engine-driven turns settle only after their own work. Forward turn events for turns no driver owns and emit a usage update when they end, instead of dropping them. Fixes MoonshotAI#5095
🦋 Changeset detectedLatest commit: 328934d The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 328934ddfd
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (driver === undefined || driver.settled) return; | ||
| if (driver.promptId === undefined || event.promptId !== driver.promptId) return; | ||
| this.settleDriver(driver, () => { | ||
| driver.resolve({ stopReason: turnEndReasonToStopReason(event.reason ?? 'completed') }); |
There was a problem hiding this comment.
Honor cancellation before settling a blocked queued prompt
When an ACP host sends session/cancel while this prompt is queued behind an autonomous turn, AcpSession.cancel() sets driver.cancelRequested; if the queued prompt is then blocked before its own turn.started, the engine emits prompt.completed(reason: 'blocked') rather than prompt.aborted, and this handler maps it to refusal without checking the flag. The canceled session/prompt therefore reports a refusal instead of cancelled; honor cancelRequested before mapping the completion reason.
AGENTS.md reference: AGENTS.md:L205-L205
Useful? React with 👍 / 👎.
|
Thanks for this. Since #5447 and #5586 landed, turns that the engine starts on its own are already forwarded to ACP editors, so that half of this PR now overlaps with main. Could you rebase it to keep only the part that queues editor prompts while such a turn is running, instead of rejecting them as busy? If we don't hear back within a week, we'll close this and pick up the queueing part in a separate PR. |
Requirement or Bug
Resolve #5095. ACP hosts never see turns the agent starts on its own from background-task notifications, and prompts sent while such a turn runs are answered immediately with an empty
end_turnwhile the text runs later invisibly (or appears lost). After this PR, hosts receive updates for every turn the agent runs, and prompts arriving during an autonomous turn wait for their own turn instead of settling empty.Bug Reproduction Steps
See linked issue. Automated coverage is in
packages/acp-server/test/autonomous-turn.test.ts(both cases fail on main, pass with this fix).Root Cause
Fundamental fix, both mechanisms in
AcpSession(packages/acp-server/src/session.ts):driverFor(turnId)matched the single ACP-owned driver. Engine-driven turns (background-task notifications, goals, cron) have no driver, so nosession/updatewas ever emitted for them.agent.prompt()returnsundefinedwhen the loop is busy (the prompt is admitted to the queue).driveLaunchtreatedundefinedas completion and resolved{ stopReason: 'end_turn' }at once. The queued prompt later ran with its driver already settled, so its output was dropped by mechanism 1 and the host never saw an answer.Code Changes
Core idea: give every ACP prompt a
promptId, correlateturn.started/prompt.completed/prompt.abortedwith the waiting driver so queued prompts settle only after their own work, and forward events for turns no driver owns.driveTurnassigns a fresh id and passes it toagent.prompt; the engine honorsattributes.promptIdas the prompt id.turn.startedsubscription claims the driver turn when thepromptIdmatches, sharing oneclaimDriverTurnhelper with the launch-result path (sets the turn id, runs a deferred cancel, flushes matching buffered events).undefinedwith apromptIdnow marks the driver queued and flushes the early buffer (every buffered event is foreign: ours has not started) instead of settling empty.shouldForwardTurnEventguard: content events are forwarded unless a driver owns a different turn, so autonomous-turn output reaches the host whether or not a prompt is waiting.turn.endedwith no matching driver now emits ausage_update.prompt.completed/prompt.abortedsettle a still-waiting driver bypromptId, covering prompts that never start their own turn (gate-blocked, steered, aborted). Ordering is safe because the engine dispatchesturn.endedbefore prompt completion, so the normal path still settles onturn.endedwith full error detail.promptIdand keep the previous behavior.Key decisions:
turn.agent_busypromptIdturn.startedReading guide:
packages/acp-server/src/session.tsholds the whole fix;packages/acp-server/test/autonomous-turn.test.tsholds the two real-engine regressions; the two.changeset/files are the library and CLI entries.Behavior Changes and Affected Users
session/updatefor engine-driven turns over ACPkimi acp) that assume updates only follow their own prompts; ACP frames no turn ownership, so extra updates are additivesession/promptsent while an autonomous turn runsend_turn; the text runs later with no visible outputturn.agent_busy, like any in-flight turnusage_updateafter autonomous turnsAffected modules and coverage:
packages/acp-server/src/session.ts: newtest/autonomous-turn.test.ts(engine-driven output forwarded with no prompt in flight; queued prompt waits for its own turn; both fail before, pass after). Existinge2e-turn,lifecycle,kap-parityand the rest of theacp-serversuite pass: 163/165, where the 2 failures are a pre-existingsession/deleteassertion that also fails on pristine main and a load-inducedconverttimeout that passes in isolation (8/8).Verification commands and results
pnpm exec vitest run --reporter=agent test/autonomous-turn.test.ts→ 2 passed (with fix); 2 failed (fix stashed)pnpm exec vitest run --reporter=agent test/(acp-server) → 163 passed, 2 failed as described abovepnpm exec tsc -p packages/acp-server/tsconfig.json --noEmit→ cleanpnpm --filter @moonshot-ai/acp-server run build→ cleanpnpm lintcomponents: check-comments, import-boundaries, guards, test-doubles, quarantine, instructions all pass; type-aware oxlint passes on the changed files (0 warnings, 0 errors). The repo-wide type-aware oxlint backend was killed by the sandbox (SIGTERM) in this environment, so CI is the final word there.Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.