You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug Report] Streamed tool-call arguments with truncated JSON are persisted, then every subsequent turn fails with 400 INVALID_REQUEST: Unterminated string ... in the same session forever
#3234
When a model streams a tool call whose arguments payload arrives truncated / incomplete JSON (the delta stream stops mid-string, or a closing " / } never arrives before the block ends), the harness persists the malformed string verbatim into the append-only session log.
From then on, every request in that session rebuilds its history from the log, serializeAssistant reserializes the same broken arguments unmodified into tool_calls[].function.arguments, and the DeepSeek (Python) endpoint fails to json.loads it, returning:
400 INVALID_REQUEST: Unterminated string starting at: line 1 column N (char N)
The turn is marked failed and the GUI shows the terminal turn-error. Because the log is append-only and the bad record is never healed, the same session fails on every follow-up turn, indefinitely. Only starting a new conversation (fresh log, no bad history) recovers.
In a session, have the model issue a tool call (observed with read, grep, and pwsh; likely any tool) where the streamed arguments is cut off mid-JSON β the tool-call-delta stops before the closing "} and block-end closes the block anyway.
Observe that the truncated arguments is persisted as a tool/call event in the session log.
Send any follow-up message in the same session.
The turn fails with 400 INVALID_REQUEST: Unterminated string starting at: line 1 column N (char N).
Repeat β the same error recurs on every turn; the session never recovers.
A new conversation runs normally.
Expected behavior
A tool call whose streamed arguments do not form valid JSON should either be rejected/retried at ingest time, or persisted in a sane form (e.g. {}), so that a single truncated stream does not permanently brick the session history.
At minimum, the adapter must not resend unparseable arguments to a schema-strict upstream and turn it into a permanent 400.
Existing corrupted records should be supported: a recovery/heal path on load() for malformed persisted arguments.
Actual behavior
The ingest path deliberately tolerates malformed arguments, but the resend path is strict, and nothing validates or heals in between:
BlockAssembler concatenates tool-call-delta.argumentsDelta onto the accumulating arguments (@deepseek-ai/dsh-llm, assembler.ts) without ever checking that the result is valid JSON.
dsh-agent-loop's parseArguments swallows invalid JSON (catch { return raw }) and runs the tool with the raw truncated string β no error surfaces.
session.append("tool/call", { arguments: block.arguments }) persists the raw truncated string as the authoritative record.
On later turns, dsh-llm-deepseek's serializeAssistant writes tool_calls[].function.arguments = block.argumentsverbatim into the wire request; the DeepSeek server (Python) json.loads it and 400s.
Independently, dsh-host-apiproxy's viewFor / backscanArgs also do a bare JSON.parse(raw), throwing Unterminated string ... again β this is what emits the console noise api-proxy: presenter failed for tool/call, falling back to generic: ....
Session logs are append-only and load() only synthesizes missing tool/result/turn/end closers; it does not scan or heal malformed arguments. So the poisoned record is permanent.
Evidence (from real ~/.dsh session logs)
Example 1 β persisted tool/call with truncated read arguments:
followed by twoturn/end errors (seq 175, seq 183) with Unterminated string starting at: line 1 column 45 (char 44).
The streamed deltas confirm the truncation is upstream: for example the read call received a single partial argumentsDelta and then block-end without a closing "}", while adjacent glob calls in the same assistant message did receive their closing "}" deltas β so the harness faithfully accumulated a payload the provider never finished.
Suggested fixes
A (minimal, unblocks) β in dsh-llm-deepseek's serializeAssistant (and any other place that resends tool-call arguments), validate with JSON.parse and substitute {} for invalid values, so a poisoned history stops producing permanent 400s:
B (quiet the noise) β wrap the bare JSON.parse(raw) in dsh-host-apiproxyviewFor / backscanArgs in try/catch (mirroring parseArguments elsewhere), removing the presenter failed ... falling back to generic console spam.
C (root cause) β validate the accumulated tool-call arguments (valid JSON) before persisting the tool/call event (in BlockAssembler finalize or appendToolCall); drop or replace invalid arguments so bad history never enters the log. Optionally add a recovery/heal pass on load() for already-persisted malformed records.
Impact
A single truncated streamed tool call permanently bricks the session: every later turn 400s with INVALID_REQUEST until the user branches or starts a new conversation.
Server console pollution from the duplicated presenter failed JSON-parse errors on the same poisoned record.
Issue drafted from a diagnosis that inspected the compiled packages in the npm-cache checkout and decoded the local ~/.dsh/sessions/.../session.jsonl.zstd logs to confirm the persisted corrupt arguments and the recurring INVALID_REQUEST turn errors.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Summary
When a model streams a tool call whose
argumentspayload arrives truncated / incomplete JSON (the delta stream stops mid-string, or a closing"/}never arrives before the block ends), the harness persists the malformed string verbatim into the append-only session log.From then on, every request in that session rebuilds its history from the log,
serializeAssistantreserializes the same brokenargumentsunmodified intotool_calls[].function.arguments, and the DeepSeek (Python) endpoint fails tojson.loadsit, returning:The turn is marked failed and the GUI shows the terminal turn-error. Because the log is append-only and the bad record is never healed, the same session fails on every follow-up turn, indefinitely. Only starting a new conversation (fresh log, no bad history) recovers.
Environment
@deepseek-ai/dsh-llm-deepseek(DeepSeek chat-completions compatible endpoint, streaming)session.jsonl.zstd(append-only JSONL back-end)Steps to reproduce
read,grep, andpwsh; likely any tool) where the streamedargumentsis cut off mid-JSON β thetool-call-deltastops before the closing"}andblock-endcloses the block anyway.argumentsis persisted as atool/callevent in the session log.400 INVALID_REQUEST: Unterminated string starting at: line 1 column N (char N).Expected behavior
argumentsdo not form valid JSON should either be rejected/retried at ingest time, or persisted in a sane form (e.g.{}), so that a single truncated stream does not permanently brick the session history.argumentsto a schema-strict upstream and turn it into a permanent400.load()for malformed persistedarguments.Actual behavior
The ingest path deliberately tolerates malformed arguments, but the resend path is strict, and nothing validates or heals in between:
BlockAssemblerconcatenatestool-call-delta.argumentsDeltaonto the accumulating arguments (@deepseek-ai/dsh-llm,assembler.ts) without ever checking that the result is valid JSON.dsh-agent-loop'sparseArgumentsswallows invalid JSON (catch { return raw }) and runs the tool with the raw truncated string β no error surfaces.session.append("tool/call", { arguments: block.arguments })persists the raw truncated string as the authoritative record.dsh-llm-deepseek'sserializeAssistantwritestool_calls[].function.arguments = block.argumentsverbatim into the wire request; the DeepSeek server (Python)json.loadsit and 400s.dsh-host-apiproxy'sviewFor/backscanArgsalso do a bareJSON.parse(raw), throwingUnterminated string ...again β this is what emits the console noiseapi-proxy: presenter failed for tool/call, falling back to generic: ....load()only synthesizes missingtool/result/turn/endclosers; it does not scan or heal malformedarguments. So the poisoned record is permanent.Evidence (from real
~/.dshsession logs)Example 1 β persisted
tool/callwith truncatedreadarguments:(note: missing closing
"}), immediately followed by:{"type":"assistant/chunk","seq":696,"data":{"chunk":{"type":"finish","reason":{"kind":"error","failure":{"message":"Unterminated string starting at: line 1 column 15 (char 14)","code":"INVALID_REQUEST","status":400}}}} {"type":"turn/end","seq":698,"data":{"reason":{"kind":"error","error":{"message":"Unterminated string starting at: line 1 column 15 (char 14)","code":"INVALID_REQUEST","status":400}}}}Example 2 β persisted
tool/callwith truncatedgreparguments:followed by two
turn/enderrors (seq 175,seq 183) withUnterminated string starting at: line 1 column 45 (char 44).The streamed deltas confirm the truncation is upstream: for example the
readcall received a single partialargumentsDeltaand thenblock-endwithout a closing"}", while adjacentglobcalls in the same assistant message did receive their closing"}"deltas β so the harness faithfully accumulated a payload the provider never finished.Suggested fixes
A (minimal, unblocks) β in
dsh-llm-deepseek'sserializeAssistant(and any other place that resends tool-callarguments), validate withJSON.parseand substitute{}for invalid values, so a poisoned history stops producing permanent400s:B (quiet the noise) β wrap the bare
JSON.parse(raw)indsh-host-apiproxyviewFor/backscanArgsin try/catch (mirroringparseArgumentselsewhere), removing thepresenter failed ... falling back to genericconsole spam.C (root cause) β validate the accumulated tool-call
arguments(valid JSON) before persisting thetool/callevent (inBlockAssemblerfinalize orappendToolCall); drop or replace invalid arguments so bad history never enters the log. Optionally add a recovery/heal pass onload()for already-persisted malformed records.Impact
INVALID_REQUESTuntil the user branches or starts a new conversation.presenter failedJSON-parse errors on the same poisoned record.Issue drafted from a diagnosis that inspected the compiled packages in the npm-cache checkout and decoded the local
~/.dsh/sessions/.../session.jsonl.zstdlogs to confirm the persisted corruptargumentsand the recurringINVALID_REQUESTturn errors.All reactions