Skip to content

Empty assistant message after terminal crash causes 400 on session resume (DeepSeek) #1404

Closed

Description

@yanjin101

Description

After terminal crash and restart, resuming the session fails with:

400 Invalid assistant message: content or tool_calls must be set

The session cannot continue after resume. Recovery via /undo resolves the issue.

The crash occurred while the agent was in plan mode, mid-thinking with the last recorded thought:

"The plan is written. Let me call ExitPlanMode for approval."

The model had only emitted a ThinkPart (reasoning tokens) before the terminal died — no TextPart, no tool call. This incomplete assistant message was persisted to wire.jsonl. On session resume, convertMessage() in openai-legacy.ts strips the think content into reasoning_content but leaves content unset, producing:

{ "role": "assistant", "reasoning_content": "..." }

DeepSeek rejects this with 400 Invalid assistant message: content or tool_calls must be set.

Impact

  • Probability: Very low. Requires a crash during the exact thinking window before any text/tool output.
  • Recovery: Session can be restored via /undo.
  • Suggested action: None. The fix would require marking partial steps in transcript.ts, which carries risk in core session logic.

Possible Root Cause

Two code paths contribute:

  1. packages/agent-core/src/services/message/transcript.ts — step.begin immediately pushes an empty assistant message (content: [], toolCalls: []) to the transcript. If step.end never arrives (crash), this partial message stays in the transcript without partial: true.

  2. packages/kosong/src/providers/openai-legacy.ts:162-199 — convertMessage() has a if (role === 'tool') guard at line 162 for tool messages, but no corresponding if (role === 'assistant') guard for assistant messages. Assistant messages fall through to the generic else branch (line 189) which only sets content when nonThinkParts is non-empty. A think-only message produces nonThinkParts = [], leaving content unset. reasoning_content is then set at line 222-224, but DeepSeek does not accept that as a substitute for the required content or tool_calls field.

The fix would be adding a fallback in the else branch:

} else {
  const firstPart = nonThinkParts[0];
  if (nonThinkParts.length === 1 && firstPart?.type === 'text') {
    result.content = firstPart.text;
  } else if (nonThinkParts.length > 0) {
    result.content = nonThinkParts.map(...);
  }
  // missing guard:
  // else if (message.role === 'assistant' && reasoningContent) {
  //   result.content = reasoningContent;
  // }
}

Reproduction

  1. Use a DeepSeek-architecture model with thinking enabled
  2. Enter plan mode, let the agent formulate a plan and start thinking about the approval step
  3. Crash the terminal while the model is in its thinking phase (before any text or tool call output)
  4. Resume the session
  5. Observe 400 Invalid assistant message: content or tool_calls must be set on the first API call of the resumed turn

Logs

2026-07-05T20:22:23.647Z WARN  llm request failed  turnStep=136.1 attempt=1/3
  model=deepseek-v4-flash
  errorName=APIStatusError
  errorMessage="400 Invalid assistant message: content or tool_calls must be set"
  statusCode=400

The error occurs <300ms after the request is sent (immediate rejection by API), confirming invalid payload structure rather than model output truncation.

wire.jsonl evidence (line 9216-9217)

step.begin, turnId=135, step=25
content.part { type: "think", think: "The plan is written. Let me call ExitPlanMode for approval." }
-- no text, no tool.call, no step.end (crash)

Affected code

File Issue
packages/kosong/src/providers/openai-legacy.ts:162-199 if (role === 'tool') exists but no if (role === 'assistant') guard; assistant falls to else which skips content when nonThinkParts is empty
packages/agent-core/src/services/message/transcript.ts:164-172 Partial message not marked partial: true on interrupted step

Reference: Reasonix fix

esengine/DeepSeek-Reasonix/internal/provider/openai/openai.go:452-457 has the equivalent guard via an OR branch:

case m.Role != provider.RoleAssistant || len(cm.ToolCalls) == 0 || m.Content != "":
    cm.Content = m.Content

len(cm.ToolCalls) == 0 is the condition that catches this bug. When the assistant message has no tool calls, cm.Content is always set (even if m.Content is ""), ensuring the serialized JSON always includes the content field.

kimi-code's openai-legacy.ts lacks this fallback. Comparison:

Scenario esengine/DeepSeek-Reasonix kimi-code
assistant, no tool_calls, has content content: "text" content: "text"
assistant, has tool_calls, has content content: "text" + tool_calls content: "text" + tool_calls
assistant, has tool_calls, empty content tool_calls only (omit content) tool_calls only (omit content)
assistant, no tool_calls, empty content content: "" (fallback) content unset (400)

Note: Both Go encoding/json and JS JSON.stringify serialize "" identically to "content": "", so the same fix applies to both codebases.

Log files (auto-download)

Activity

  1. whyihaveyou commented on Jul 18, 2026

    @whyihaveyou

    Confirmed reproduction of the same failure chain, with two extensions beyond the reported scenario:

    1. Provider-agnostic: this also triggers on Kimi's own API (k3 model) — every subsequent resume fails with the same 400 ("assistant must not be empty").
    2. Trigger is not limited to terminal crashes: an external supervisor killing the process (SIGKILL from a 30-min per-message timeout in cc-connect) hits the exact same path — the assistant turn header is persisted with no content, and all subsequent resumes fail.

    Environment: kimi-code 0.26.0 (linux/x64), session resumed via , process killed mid-turn by an external timeout.

    Workaround confirmed: manually deleting the trailing incomplete assistant records from restores the session with full history intact.

    Fix suggestion: on resume, sanitize trailing assistant turns that have neither content nor tool calls before serializing history (drop with a warning). This avoids touching the write path and should be lower-risk than marking partial steps in transcript.ts.

  2. kaidegit commented on Aug 12, 2026

    @kaidegit

    +1,在打断ds思考再回复时非常容易复现。kimi code版本0.34,macOS

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