Skip to content

fix: one tool call per id from grokv streams; headless runs exit after the turn - #161

Merged
mrnasil merged 2 commits into
mainfrom
fix/tool-call-stream-dedupe
Oct 11, 2026
Merged

mrnasil merged 2 commits into
mainfrom
fix/tool-call-stream-dedupe

Conversation

@mrnasil

@mrnasil mrnasil commented Oct 11, 2026

Copy link
Copy Markdown
Member

Summary

Found while investigating "the TUI feels frozen". Two bugs made turns slow or stuck.

1. Every tool call ran twice (grokv)

Raw SSE from grok.claude.gg (captured):

delta.tool_calls[0] = {index:0, id:"call-…-0", name:"read", arguments:""}
delta.tool_calls[0] = {index:0, id:"call-…-0", name:"read", arguments:"{\"path\":\"calc.py\"}"}

OpenAI sends id/name only on the first chunk; grokv repeats them. ToolCallAssembler::on_tool_use opened a new call per start, so each call became an empty-argument call ("Missing required parameter path") plus the real one. Auto mode then retried the failure twice, and the model got a "tool failed, correct your approach" nudge and an extra LLM step. The log had 6,257 auto mode retrying failed tool lines.

Fix: a start whose id is already open continues that call. Argument fragments append; a cumulative repeat (fragment starting with the whole buffer) replaces; an object replaces; an empty name is filled once. Parallel calls keep distinct ids.

Measured (same task, grok-4.6-cyber-high, generate): before, 6 steps / 22–24 s with failing duplicates on every step; after, 5 steps / 11 s and 0 retries.

2. whycodes generate never exited after using bash

run_headless_turn (and the parallel generate path) awaited the CI event drain until the channel closed. Without a long-lived sink, wire_background_turn_listener parks a clone of the turn sender in the background-job registry for the life of the agent (schedule moves one into a delayed task), so the channel never closed. Seen live: the process sat idle for more than 10 minutes after turn.done without printing its result.

Fix: spawn_event_drain / finish_event_drain. The drain ends on a done signal and still flushes queued events in order (biased select). Same run now exits 0 in 18 s with the JSON result.

Test plan

  • tool_stream: grokv-style repeated starts for two parallel calls → 2 calls with correct args; fragments/cumulative/object/name merges
  • helpers: drain ends on signal while a sender clone is alive, events in order; a self-closing channel still ends
  • Real model, end to end: duplicates and retries gone; generate exits 0 in 18 s, tests OK
  • agent lib 555 passed; cli bin: same 5 Windows-only failures as main; fmt, clippy, ratchets

🤖 Generated with Claude Code

…r the turn

Two bugs that made turns look stuck.

1. Tool calls split in two. grokv repeats `id` + `name` on every
   chunk of a call (`{index:0,id,name,arguments:""}` then the same id
   with the full arguments). ToolCallAssembler opened a new call per
   start, so every call ran twice: once with empty arguments ("Missing
   required parameter `path`"), auto mode retried that failure twice,
   and the model got a "tool failed" nudge and an extra step. A start
   whose id is already open now continues that call (a cumulative repeat
   replaces the buffer). Same task against grok-4.6-cyber-high: 6 steps
   / 22-24 s with failing duplicates before, 5 steps / 11 s and zero
   retries after.

2. `whycodes generate` never exited after a turn that ran bash. With no
   long-lived sink, the agent parks a clone of the turn sender in its
   background-job listener, so the CI event drain waited for a channel
   that never closed (seen: >10 min after `turn.done`). The drain now
   ends on a done signal and still flushes queued events in order.
   Same run now exits 0 in 18 s with the JSON result.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
mrnasil added a commit that referenced this pull request Oct 11, 2026
…ck (#162)

* fix: three flaky or hanging tests, two of them real bugs

1. config: write_atomic removed the destination before the rename.
   Two parallel saves raced on that remove (NotFound, flaky in Coverage
   on #161), and a crash between remove and rename left no config.toml.
   rename replaces atomically on Unix and Windows (MOVEFILE_REPLACE_
   EXISTING), so it is now rename-only with the one retry. Parallel-save
   test: 30/30 on Linux, 15/15 on Windows.

2. agent: StdinPrompter / StdinQuestionPrompter read the process stdin
   inside the async ask. The read blocks the runtime thread, so the
   tests' 2 s timeout could not cancel it; with stdin left open (a
   terminal or a pipe) the tests never ended and other tests queued on
   the stdin lock — `cargo test -p whycodes-agent` hung. The prompters
   take an AnswerInput (default: locked process stdin); tests pass an
   empty reader and still go through `ask`. With stdin held open: 3/3
   runs in 32 s, was hung on the first.

3. agent: rate_limit_retries_same_model_on_next_credential and
   failover_api_key_walks_named_credentials both set and restore the
   credential env vars; run in parallel, one restored the other's vars
   mid-turn and the 429 failover found no second key (flaky in
   Coverage on #154 and #161). Both now hold CREDENTIAL_ENV_LOCK.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(config): keep write_atomic's retry on the line floor

Rename-only saves no longer lose a remove race, so tests never took the
'first rename failed, retry succeeded' arm and config fell to 2406/2407.
The same logic as two always-run lines: let-else on the first rename,
map_err on the retry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@mrnasil
mrnasil merged commit 1e62e72 into main Oct 11, 2026
7 checks passed
@mrnasil
mrnasil deleted the fix/tool-call-stream-dedupe branch October 11, 2026 03:41
@mrnasil mrnasil mentioned this pull request Oct 11, 2026
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.

1 participant