MoonshotAI / MoonshotAI/kimi-code

[Bug] TUI crashes on assistant.delta events with missing delta payload (Cannot read properties of undefined (reading 'trim'))

Open
#2,924 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
7.5k
Forks
1.2k
Avg merge
11h 53m
Merged PRs (30d)
350

Description

Environment

  • kimi-code TUI on the agent-core-v2 engine (current main, also reproducible on 0.34.x)
  • Model served through an Anthropic-compatible relay/proxy (not the first-party Anthropic API)

Error

The TUI throws and logs an unexpected error while streaming:

[unexpected] TypeError: Cannot read properties of undefined (reading 'trim')
    at SessionEventHandler.handleAssistantDelta (.../main.cjs)
    at SessionEventHandler.handleEvent (.../main.cjs)
    at SDKRpcClientV2.receiveEvent (.../main.cjs)

Root cause

Three small gaps line up:

  1. Parser: in packages/agent-core-v2/src/kosong/provider/bases/anthropic/anthropic.ts, the stream converter trusts the SDK types: content_block_start yields { type: 'text', text: block.text } and text_delta yields { type: 'text', text: delta.text }. Some relays omit the text field entirely, so the engine produces a text part with text: undefined.
  2. Transport: loopService.ts publishes { type: 'assistant.delta', turnId, delta: part.text }. When the event crosses JSON serialization, the undefined value silently drops the delta key, so the TUI receives an event with no delta at all.
  3. TUI: handleAssistantDelta in apps/kimi-code/src/tui/controllers/session-event-handler.ts immediately calls event.delta.trim(), which throws on undefined.

Minimal reproduction

Have an Anthropic-compatible endpoint stream:

{"type":"content_block_start","index":0,"content_block":{"type":"text"}}
{"type":"content_block_delta","index":0,"delta":{"type":"text_delta"}}

(both omit text). Every such event crashes the TUI handler.

Suggested fix

  • Parser: block.text ?? '' / delta.text ?? '' (same pattern already used for thinking ?? '' two cases below).
  • TUI: coerce a non-string event.delta to '' before use, so a malformed wire record can never crash the handler again.

Happy to submit a PR with tests if the approach looks good.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Anthropic stream converter in packages/agent-core-v2/src/kosong/provider/bases/anthropic/anthropic.ts and the event handler in apps/kimi-code/src/tui/controllers/session-event-handler.ts, using the two-event reproduction as the first check. Trace the related publishing path in loopService.ts; done means missing text fields produce safe empty values and the TUI no longer crashes on malformed assistant.delta events, with tests added for the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.