Skip to content

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

Backlog

Description

@caopulan

Summary

When a background task (such as a detached bash command) finishes mid-turn while the main agent is still executing a client-driven session/prompt, the engine steers the active turn and ends it with turn.ended { reason: "completed", continuation: "turn" } so it can immediately start turn N + 1 to process the <notification category="task"> message.

Even after #5447 (merged to main), AcpSession.onTurnEnded in packages/acp-server/src/session.ts still resolves the active session/prompt immediately with { stopReason: "end_turn" } without checking event.continuation === "turn".

Related: #5095, #5447, #5381.

Reproduction

  1. Connect any ACP client to kimi acp (observed on @moonshot-ai/[email protected], and the same onTurnEnded behavior remains on main after fix(acp): stream background task turns to editors #5447).
  2. Send a session/prompt where the agent starts a short background shell task (e.g., a command that completes or fails within ~1–15s) and continues doing another tool call (e.g., Read or Grep) in the same turn.
  3. Wait for the background task to terminate while that next step is in flight (task.terminated → prompt.steered).
  4. As soon as the tool_use step finishes (before any final assistant answer is generated), the engine emits:
    • turn.ended with reason: "completed", continuation: "turn" (for turn N)
    • turn.prompt with <notification category="task" ...> (for turn N + 1)

Observed Behavior

  • AcpSession.onTurnEnded sees event.turnId === driver.turnId and immediately settles the active session/prompt with { stopReason: "end_turn" }, even though turn N ended right after a tool_use step with continuation: "turn" and the agent has not finished the user's request.
  • Standard ACP clients mark the turn complete and return the UI to idle mid-task.
  • On 2.8.1, all output from turn N + 1 is dropped by driverFor(event.turnId), and any follow-up session/prompt sent while turn N + 1 is still running receives an immediate empty end_turn.
  • On main (after fix(acp): stream background task turns to editors #5447), turn N + 1 streams session/update notifications after session/prompt has already settled, which standard request/response ACP clients do not expect while idle — and if the user sees the idle state and sends a follow-up message, it is rejected with turn.agent_busy.

Expected Behavior

  • When turn.ended has reason: "completed" and continuation: "turn", the in-flight session/prompt should stay open and follow the immediate continuation turn (N + 1), streaming its updates under the same prompt request.
  • session/prompt should only resolve with { stopReason: "end_turn" } when the turn actually settles with continuation !== "turn" (i.e. "none" or "background"), or on error/cancellation.
  • The post-settle taskNotificationTurnId path added in fix(acp): stream background task turns to editors #5447 remains useful for truly asynchronous wakes (after a turn has already settled with continuation: "background" and a long-running task finishes later).

Root Cause & Suggested Direction

In packages/acp-server/src/session.ts, onTurnEnded does not inspect event.continuation:

private onTurnEnded(event: AgentEventPayloads['turn.ended']): void {
  if (event.turnId === this.taskNotificationTurnId) {
    this.taskNotificationTurnId = undefined;
    void this.emitUsageUpdate();
    return;
  }
  const driver = this.driverFor(event.turnId);
  if (driver === undefined) return;
  ...
  this.settleDriver(driver, () => {
    ...
    driver.resolve({ stopReason: turnEndReasonToStopReason(event.reason, error) });
  });
}

If event.reason === 'completed' && event.continuation === 'turn' (and the driver has not been cancelled), keeping this.driver active and binding it to the immediately following turn.started (with a fallback if cancellation or failure occurs) would keep the ACP session/prompt lifecycle aligned with the agent's real turn boundary across all ACP clients.

Happy to open a focused PR with regression tests if a maintainer /approves this approach.

Activity

  1. mw1986518-dot commented on Nov 30, 2026

    @mw1986518-dot

    Confirming this reproduces in real-world usage with T3 Code 0.1.13 (acpRegistry_kimi_code, model kimi-code/k3.1-pro) on macOS (darwin/arm64).

    Observed timeline

    1. Sent a multi-step prompt (code review → full test suite → git commit → remote deploy).
    2. During the turn, the agent ran a ~26s test suite while continuing to issue Read / shell checks in parallel.
    3. Shortly after the test run finished mid-turn, session/prompt resolved as completed (~73s into the run) right after a Read tool call, with no final assistant summary.
    4. The agent actually kept running unattended for another ~7 minutes, completing two git commits and a full remote production deployment with zero output streamed to the ACP client.
    5. Because the UI showed the thread as idle, I sent a follow-up "continue" message while the hidden continuation turn was still finishing — that prompt returned empty completed in 2.1s with zero assistant/tool items.

    This is particularly risky for side-effecting workflows (git commits, deployments) because the user sees the run as finished and may re-trigger the workflow while the unattached continuation turn is still mutating files or deploying. Keeping the in-flight session/prompt open when event.continuation === "turn" would directly prevent this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions