Skip to content

ACP: mark tool calls still open at the end of a turn as failed - #7433

Closed
lanjak wants to merge 1 commit into
get-bb:mainfrom
lanjak:fix-6775-acp-unfinished-tools-failed
Closed

lanjak wants to merge 1 commit into
get-bb:mainfrom
lanjak:fix-6775-acp-unfinished-tools-failed

Conversation

@lanjak

@lanjak lanjak commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

When an ACP turn ends, translateTurnCompleted in packages/provider-bridge-acp/src/delta-translation.ts closes each tool call that never got a final status. It used the turn's status, so after an end_turn those calls became completed, and genericCloseFields showed the last progress text (the echoed arguments) as the result (#6775). Kimi Code 2.4.1 maps a failed turn to stopReason: "end_turn" and does not send a final tool_call_update for the interrupted call (turnEndReasonToStopReason in its published dist/main.mjs). So a turn that failed inside Kimi looked like a successful turn with successful tool calls.

What changed

  • delta-translation.ts: in translateTurnCompleted, a tool call that is still open when the turn ends closes as interrupted when the turn was cancelled, and as failed for every other stop reason. Assistant and thought streams close as before, and they take no status.
  • delta-translation.test.ts: a regression test that sends an in-progress Read call and then end_turn. One assertion in the existing Add ACP dialect hook to normalize tool call progress events #6769 test, which checks the same situation, changes from completed to failed. Its main check (no resultText) is unchanged.

What this does not change: the turn itself still shows as completed, with no error text. Kimi reports end_turn and no error, and the only sign of the failure is the unfinished call. Marking the whole turn failed from that sign would be a guess. The complete fix is in Kimi: send a JSON-RPC error, or a final failed update for pending tool calls.

No event shape or wire change.

How you verified

  • Before the fix: the new test fails with - "status": "failed" / + "status": "completed".
  • After the fix: pnpm exec turbo run lint lint:type-aware typecheck test --filter=@bb/provider-bridge-acp passes, 6 of 6 tasks, 25 test files, 0 lint warnings. oxfmt --check passes.
  • Real agents close every call before end_turn, so their normal turns do not change. A small ACP client asked each agent for a read, a listing and a shell command, then for one more read, and recorded the last status of each call:
    • OpenCode (opencode acp): read, glob and cat b.txt all completed, stopReason: "end_turn".
    • @agentclientprotocol/claude-agent-acp 0.106.0: Read a.txt, ls -la, cat b.txt and Read b.txt all completed, stopReason: "end_turn".
  • Not run against a live Kimi session. The Kimi behavior comes from its published code and the frames in the issue.

Fixes #6775

AGENT GENERATED

When an ACP prompt ended with a non-cancelled stop reason, the
translator closed every tool call that had no terminal update as
completed and showed its last progress text as the result. Kimi Code
reports a turn that failed inside the agent as stopReason end_turn, so
tool calls that never ran were shown as successful, often with their
echoed arguments as the "result".

A tool call that is still open when the agent ends the turn never
reported a result, so it now closes as failed. Cancelled turns still
close open calls as interrupted, and the turn status is unchanged.

Fixes get-bb#6775
ymichael added a commit that referenced this pull request Nov 18, 2026
#7788)

## Human comments

## What was wrong

Two ACP bridge bugs, both in `packages/provider-bridge-acp`, affecting
OpenCode, Cursor, Copilot, Gemini, Qwen and other ACP threads.

1. **Null tool call fields.** ACP SDK 1.9.0 types
`ToolCallUpdate.title`, `content` and `locations` as `T | null`.
`acpToolCallFieldsSchema` (`wire.ts`) accepted null only for `name` (and
`kind`/`status` through the enum preprocess). Any `tool_call_update`
with `title: null`, `content: null` or `locations: null` failed
`safeParse`, and `delta-translation.ts` dropped it as unhandled, even
when it carried the call's final `failed`/`completed` status and output.
The call then closed as `completed` at end of turn with no output.
2. **Lost agent session.** When the agent rejected `session/prompt`
because it no longer had the session, `runTurn` reported the error and
kept the same session. Every later message in the thread failed with the
same error until bb restarted the bridge. `ACP_RESOURCE_NOT_FOUND_CODE`
was defined but unused. Real agents don't only use `-32002`. Probing
installed agents with a prompt on an unknown session id gave:
   - OpenCode 2.0.22: `-32602 "Invalid params: session not found: <id>"`
   - Gemini: `-32602 "Session not found: <id>"`
   - Copilot: `-32602 "Session <id> not found"`
- Qwen: `-32603 "Internal error"`, `data.details: "Session not found:
<id>"`
- Cursor: `-32603 "Internal error"`, `data.details: "Session <id> not
found"`

   T3 Code adds Copilot's `session lock unavailable …` cause.

## What changed

- `wire.ts`: new `acpNullAsAbsent(schema)`, used for every SDK-nullable
tool call field (`title`, `name`, `content`, `locations`). It also
replaces the duplicate `acpOptionalString`/`acpToolCallNameSchema`. Null
means "unchanged", as the SDK documents for `name`. This also covers the
tool call inside `session/request_permission`, which shares the schema.
- `bridge/bridge.ts`: `restartAfterCancelError: boolean` becomes
`rebuildBeforeNextPrompt: "cancel-error" | "lost-session" | null`.
`isLostSessionError` matches `-32002`, or an agent error whose message
(including `data.details`) says the session was not found or its lock is
unavailable. A lost-session error marks the session for rebuild in all
three places a session-scoped request can fail: the turn prompt (and the
settings reconcile before it), the `/compact` prompt, and `turn/start`
session option changes. The next turn rebuilds through the existing
`rebuildAgentSession`: restart the agent, then
`session/load`/`session/resume`, or a fresh session with the existing
"could not restore" warning. `session/replaced` reports "The ACP agent
lost its session; the session was rebuilt before continuing." The failed
turn is not retried automatically.
- `fake-acp-agent.mjs`: `FAKE_ACP_LOST_SESSION_ERROR` +
`FAKE_ACP_LOST_SESSION_FILE` make the first launched process reject
every prompt with a given JSON-RPC error.
- Unfinished tool calls at end of turn are left to #7433.
- No server/daemon wire changes.

Credit: T3 Code [#26651](pingdotgg/t3code#26651)
(Copilot lost/locked session restart) and
[#21448](https://github.com/pingdotgg/t3code/pull/21448)/[#25559](https://github.com/pingdotgg/t3code/pull/25559)
(ACP tool calls ending as completed) pointed at these.

## How you verified

**Real repro, lost session (bb's ACP bridge + real `opencode acp`
2.0.22).** A driver sends `thread/start` and `turn/start` to the
bridge's `handleLine`, deletes the agent's session with `opencode
session delete <id>` while `opencode acp` is still running, then sends
three turns.
- `origin/main`: all three turns fail with `Invalid params: session not
found: ses_…`; no `session/replaced` notification, same session each
time.
- This branch: turn 1 fails with the same error. Turn 2 restarts the
agent, emits `session/replaced` (reason above, `contextLost: true` since
the deleted session can't load), the warning, and the prompt goes to the
new session `ses_e0f9ac9d…`. On this machine, OpenCode's only configured
provider then answered `credit_balance_exhausted` from OpenAI. That is
an agent-side billing error, so I could not show a model reply through
OpenCode; the bridge tests cover the completed reply.

**Real repro, null fields (bb's ACP bridge + an agent built on
`@agentclientprotocol/sdk` 1.9.0's `AgentSideConnection`)** sending
SDK-typed updates: a `pnpm test` call, then `tool_call_update {status:
"in_progress", title/content/locations: null}`, then `{status: "failed",
title: null, locations: null, content: "3 tests failed"}`.
- `origin/main`: both null updates dropped as `unhandled`; `call_1`
closes `completed`.
- This branch: `call_1` closes `failed`, and a read call closed with
null fields closes `completed`.

I didn't find a production agent that sends these nulls in a capture;
the SDK types allow them.

**Tests (fail on main, pass here):**
- `wire.test.ts`: null `title`/`name`/`content`/`locations` parse as
absent; a permission request tool call with nulls parses.
- `delta-translation.test.ts`: a call whose updates send nulls settles
`failed` with its output.
- `bridge.test.ts`: a table of five real error shapes (`-32002`,
OpenCode, Copilot, Cursor, Copilot lock): turn 1 fails, turn 2 completes
after one restart, `session/load` of the same id and one
`session/replaced`. Also a `/compact` failure followed by a rebuilt next
turn. A negative row (`-32603` "upstream model overloaded") keeps the
process and fails both turns, so the matcher can't widen silently.
- Against `origin/main`'s source: the three null-field tests and the six
lost-session tests fail; the negative row passes.

Commands:
- `pnpm exec turbo run typecheck lint test
--filter=@bb/provider-bridge-acp` → 621 passed, 4 skipped
- `pnpm exec turbo run typecheck lint:type-aware
--filter='...@bb/provider-bridge-acp'` → 155/155 tasks

🤖 Generated with [Claude Code](https://claude.com/claude-code)

> AGENT GENERATED

Co-authored-by: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot closed this Nov 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This is an automated message. We have closed this pull request because it has been open for more than two weeks.

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.

Kimi (ACP): turns that fail inside Kimi are shown as completed, with tool calls 'completed' and echoed arguments as results

1 participant