Skip to content

fix(acp-server): surface provider errors on session/prompt #3107

Open

Description

@jangjoe

Problem

When the LLM provider returns a 4xx/5xx/rate-limit/overload/connection-error, the ACP session/prompt call resolves with { stopReason: 'end_turn' } and no JSON-RPC error. The custom ACP client (currently the JetBrains plugin) sees a normal-looking "turn ended, no content" outcome and cannot tell that the model service was actually unavailable.

Repro

  1. Point the provider config at a custom OpenAI-compatible gateway that returns 500 on a chat-completion request.
  2. Run any turn that needs an LLM call.
  3. Observe the ACP PromptResponse: { stopReason: 'end_turn', usage: {...} }. No error. No error code. No retry guidance.

Where the silent resolution happens

packages/acp-server/src/session.ts:907 onTurnEnded:

private onTurnEnded(event): void {
  const driver = this.driverFor(event.turnId);
  if (driver === undefined) return;
  const error = event.error as { code: string; message?: string } | undefined;
  this.settleDriver(driver, () => {
    if (event.reason === 'failed' && isAuthError(error)) {
      driver.reject(RequestError.authRequired(undefined, error?.message));
      return;
    }
    driver.resolve({ stopReason: turnEndReasonToStopReason(event.reason, error) });
  });
  ...
}

The isAuthError(error) check only matches the auth code set (provider.auth_error + auth.*). Anything else with reason: 'failed' falls through to turnEndReasonToStopReason, which only maps provider.filtered to refusal; everything else → end_turn.

Engine side (unchanged)

packages/agent-core-v2/src/kosong/contract/errors.ts already classifies errors as provider.api_error / provider.overloaded / provider.connection_error / provider.rate_limit / context.overflow etc. (born-coded). The loop layer surfaces these as turn.ended { reason: 'failed', error: Error2{ code } }. The classification is correct; the ACP layer just discards it.

Proposed fix

Add a sibling helper to isAuthError in packages/acp-server/src/events-map.ts:

const PROVIDER_ERROR_CODES: ReadonlySet<string> = new Set([
  'provider.api_error',
  'provider.filtered',
  'provider.rate_limit',
  'provider.connection_error',
  'provider.overloaded',
  'provider.not_found',
  'context.overflow',
  'loop.max_steps_exceeded',
]);

export function isProviderError(error?: { readonly code: string }): boolean {
  return error !== undefined && PROVIDER_ERROR_CODES.has(error.code);
}

Extend onTurnEnded in session.ts:

if (event.reason === 'failed' && isAuthError(error)) {
  driver.reject(RequestError.authRequired(undefined, error?.message));
  return;
}
if (event.reason === 'failed' && isProviderError(error)) {
  driver.reject(RequestError.internalError(
    { code: error?.code, message: error?.message, name: error?.name },
    error?.message ?? 'model provider reported an error',
  ));
  return;
}

Apply the same logic in mapPromptLaunchError (session.ts:118) so a launch-time failure that already has a provider code surfaces as internalError rather than the fixed 'session prompt failed' text.

Scope note

This change only affects the ACP server. VS Code plugin, TUI, and kap-server all consume the engine directly and are unaffected.

Test plan

  • packages/acp-server/test/convert.test.ts (or a small new events-map.test.ts if cleaner): unit-test isProviderError against every code in the set and against codes outside it.
  • packages/acp-server/test/approval.test.ts / interaction-bridge.test.ts: verify mapPromptLaunchError returns RequestError.internalError when the launch error has a provider.* / context.* code, and continues to return 'session prompt failed' otherwise.
  • packages/acp-server/test/e2e-turn.test.ts: scripted provider throws a provider.api_error mid-turn; verify the client receives a JSON-RPC error (not a successful end_turn); auth path still produces auth_required; unrelated failures still produce end_turn.

Risk

Behaviour change for any external ACP client that today treats a provider outage as "agent produced nothing". Such clients will now see a JSON-RPC error — strictly more informative, no semantic ambiguity, no protocol breakage. Mitigate by announcing the change in apps/kimi-code/CHANGELOG.md.

Activity

  1. dhjy666666 commented on Sep 9, 2026

    @dhjy666666

    Additional reproduction on the installed Kimi Code 0.39.1, Linux, ACP, with a custom OpenAI-compatible BYOK provider (qwen3.7-max, medium thinking, declared 1M context). I have not rerun this on the latest release.

    This confirms the failure propagation described here, with a useful additional regression case: the turn had already emitted nonempty compaction status text before the provider failed. A client-side “empty output means failure” workaround would therefore miss it.

    Sanitized projection of the native records, in file order:

    {"type":"full_compaction.complete"}
    {"type":"llm.request","kind":"loop","messageCount":5}
    {"type":"step.end","finishReason":"error"}
    {"type":"turn.ended","reason":"failed"}

    The continuation request received HTTP 429. In the same turn, the ACP session/prompt call resolved with stopReason: "end_turn", while the host had already received compaction progress/completion text and consequently recorded the task as successful. The native failure and the successful-looking ACP result were both observed; this is not inferred solely from a gateway access log.

    The source area agrees with this result: onTurnEnded in acp-server/session.ts rejects authentication failures specially, then resolves the remaining cases through turnEndReasonToStopReason. The failure mapping was also inspected in the installed 0.39.1 program.

    Suggested acceptance coverage for the upstream fix:

    • A provider failure after partial assistant/status output still reaches the ACP client as a failure, with a sanitized code and useful retryability information where available.
    • A 429 after successful automatic compaction fails the continuation without falsely reporting task success or reapplying the completed compaction.
    • Authentication, cancellation, refusal, and normal completion keep their intended semantics.
    • An ACP host can distinguish the failed turn without matching natural-language text or examining provider traffic.

    The same sample also produced an acknowledgement-only compaction summary; that is independently tracked in #3684. The 429 occurred after the summary replacement was committed, so the summary loss and the swallowed failure are separate issues. Historical related report: #1865.

    Only sanitized event projections and synthetic test details are included. Private account, deployment, session/request identifiers, credentials, full request/response dumps, and reasoning text are omitted.

  2. imakris commented on Oct 18, 2026

    @imakris

    Title: ACP resolves max-step failures as end_turn after partial output

    An ACP prompt that fails after streaming partial assistant text resolves successfully as end_turn. An ACP client cannot tell that the agent stopped before finishing its work.

    Reproduction:

    1. Configure a local OpenAI-compatible scripted provider and set [loop_control] max_steps_per_turn = 1.
    2. Start kimi acp with an isolated home and workspace, initialize, create a session, and send a prompt.
    3. Have the provider stream assistant text followed by a Read tool call for a local fixture. The tool succeeds; the next step exceeds the configured limit.
    4. Compare the native agent wire with the ACP session/prompt response.

    Native engine event:
    {"type":"turn.ended","reason":"failed","error":{"code":"loop.max_steps_exceeded"}}

    Actual ACP response:
    {"jsonrpc":"2.0","id":3,"result":{"stopReason":"end_turn"}}

    Expected: reject session/prompt over the JSON-RPC error channel. Partial assistant output must not convert a failed turn into successful completion.

    Confirmed with the installed Windows native CLI 2.1.1 using a localhost scripted provider (one local model request, no remote inference), and with the current main real-engine ACP tests at 4a82a07. The latest published release is 2.2.0; its producer mapping and current main retain this behavior.

    The cause is in packages/acp-server: onTurnEnded rejects authentication failures, but turnEndReasonToStopReason maps other failed turns to end_turn, except provider.filtered. A downstream delegated-session client then accepts the partial text as the completed answer.

    A focused patch makes ordinary failed turns reject through RequestError.internalError, preserving the existing authentication, refusal, cancellation and successful-completion behavior. Real-engine regressions with and without partial text fail before the fix and pass afterward. Captured fixed ACP output also makes an unchanged downstream adapter reject the partial result.

    Validation: the ACP project typecheck passes. The full e2e-turn file passes 27/28 tests; its existing Windows file-location assertion compares forward-slash output with a backslash path and fails identically on pristine main. No change to that separate test is included.

    May I submit the focused fix under the approved-bug-fix contribution policy?

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