Skip to content

fix(acp-server): keep session/prompt open across turn continuations - #5697

Open
xuilen wants to merge 2 commits into
MoonshotAI:mainfrom
xuilen:fix/acp-turn-continuation
Open

xuilen wants to merge 2 commits into
MoonshotAI:mainfrom
xuilen:fix/acp-turn-continuation

Conversation

@xuilen

@xuilen xuilen commented Nov 29, 2026

Copy link
Copy Markdown

Summary

When a background task finishes mid-turn and steers the active turn, the engine emits turn.ended with reason: completed and continuation: turn, immediately followed by turn.started for turn N + 1. Previously, AcpSession.onTurnEnded settled the active session/prompt driver immediately with { stopReason: end_turn }, causing ACP clients to return to idle mid-task while turn N + 1 was still running.

This PR updates packages/acp-server/src/session.ts to:

  • Keep the active TurnDriver open (awaitingContinuation = true, turnId = undefined) when turn.ended has reason === 'completed', continuation === 'turn', and the driver was not cancelled, while still emitting usage_update.
  • Adopt the immediately following turn.started event into the active TurnDriver (preserving deferred cancel() and early event replay) instead of routing it to agentStartedTurnId.

Closes #5693

Verification

  • Added unit tests in packages/acp-server/test/turn-failure.test.ts covering continuation: turn streaming continuity and handoff cancellation (verified RED before fix, GREEN after fix).
  • vitest run test/turn-failure.test.ts: 11/11 passed (including existing e2e background-task and Stop-hook tests).
  • tsc --noEmit: 0 errors.
  • oxlint --type-aware --deny-warnings & check-comments.mjs: passed.
  • Added patch changeset .changeset/acp-turn-continuation.md.

@changeset-bot

changeset-bot Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 263b864

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@moonshot-ai/acp-server Patch
@moonshot-ai/kimi-code Patch

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-29T02:37:31.202203Z a4fc39c PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a4fc39c871

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +755 to +756
driver.turnId = undefined;
driver.awaitingContinuation = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Keep pre-start cancellation from stranding the driver

When cancellation arrives after this branch marks the driver as awaiting but while the next core turn is still in onBeforeSubmitPrompt (before its first public turn.started), AcpSession.cancel() sees no turnId and sends an unaddressed agent.cancel(). Agent-core treats that as a pre-start gate abort, and AgentPromptProjection.gateRejected publishes neither the turn.started nor turn.ended event this driver is now waiting for, so the cancelled session/prompt never resolves. Distinguish handoff from initial launch and defer the cancel until the continuation is adopted, or otherwise settle the driver on pre-start termination.

AGENTS.md reference: AGENTS.md:L111-L115

Useful? React with 👍 / 👎.

}

test('keeps session/prompt open across continuation: "turn" and streams the continuation turn under the same prompt', async () => {
const editor = continuationSession();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Reproduce the lifecycle through the ACP surface

Both new regressions call continuationSession(), an Object.create fake, and then manually inject the ideal turn.ended/turn.started ordering; the cancellation test even guarantees a start after cancellation. They therefore cannot detect real ACP/agent-projection races such as a pre-start cancellation suppressing that start, nor prove that an editor remains busy. Add or extend a tests/e2e scenario with a held real continuation and assert that session/prompt remains pending until the second turn completes.

AGENTS.md reference: AGENTS.md:L61-L61

Useful? React with 👍 / 👎.

"@moonshot-ai/kimi-code": patch
---

Keep ACP `session/prompt` open across immediate `continuation: "turn"` handoffs so continuation turns stream under the active prompt instead of resolving prematurely with `end_turn`.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Synchronize the ACP prompt-lifecycle documentation

This changes the public lifecycle so an immediate background-task or Stop-hook continuation remains inside the active session/prompt, but docs/en/reference/kimi-acp.md:11 and docs/zh/reference/kimi-acp.md:11 still present those turns as updates arriving after the previous prompt has ended and tell clients to retry a new prompt afterward. Update both language pages to distinguish immediate handoffs from later autonomous turns so ACP client authors are not relying on the old request boundary.

AGENTS.md reference: AGENTS.md:L95-L96

Useful? React with 👍 / 👎.

@xuilen

xuilen commented Nov 29, 2026

Copy link
Copy Markdown
Author

Addressed all three Codex review items:

  1. P1 (pre-start cancel during handoff): \AcpSession.cancel()\ now skips sending an unaddressed \�gent.cancel()\ while \driver.awaitingContinuation === true, so \cancelRequested\ is deferred until \ urn.started\ adopts the continuation turn and cancels it by \ urnId.
  2. P1 (real ACP e2e test): Added \keeps session/prompt open when a background task finishes mid-turn and steers into a continuation turn\ in \packages/acp-server/test/turn-failure.test.ts\ using a real \inAcpSession\ with a held continuation turn, verifying \session/prompt\ stays pending and rejects overlapping prompts with \ urn.agent_busy\ until the continuation finishes (12/12 passed).
  3. P2 (docs sync): Updated \docs/en/reference/kimi-acp.md\ and \docs/zh/reference/kimi-acp.md\ to distinguish immediate mid-turn continuation handoffs from post-settle autonomous turns.

This branch has not been deployed

No deployments
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.

ACP: session/prompt resolves prematurely with end_turn when turn.ended has continuation: "turn"

1 participant