Skip to content

Recover one turn when tool call arrives as plain text (local MLX small models) - #304

Merged
dahai80 merged 8 commits into
mainfrom
fix/tool-call-text-recovery
Nov 21, 2026
Merged

dahai80 merged 8 commits into
mainfrom
fix/tool-call-text-recovery

Conversation

@dahai80

@dahai80 dahai80 commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Problem

Local MLX small models (Qwen3-8B class) intermittently emit a tool call as
plain text (Read({file_path: ...}), call_<id>{...}, <tools>{...})
instead of a tool_use block. The turn then looks like a finished text
answer and the session ends after one such turn — task unsolved.

Root-caused as an engine-side sampling flake (fusion-mlx; dramatically
improved in 0.10.68 but not eliminated). Controlled experiments:
bare requests always return proper tool_calls; fusion-code's real 47k-char
system prompt triggers intermittent text emission. With
tool_choice: "required" the engine returns a well-formed block
deterministically (grammar-constrained decoding) — but also returns a
non-streaming JSON body despite stream: true (separate engine ask,
filed in the fusion-mlx handoff doc).

Changes

  1. Recovery loop (src/query.ts, src/utils/queryHelpers.ts,
    src/query/transitions.ts): when the final assistant text matches a
    tool-call shape (python-call style with a known tool name, call_<id>{,
    <tools>{), feed the turn back with a format-correction meta message
    instead of ending the session. Mirrors the existing max-output-tokens
    recovery pattern. Attempt count derived from marker messages in history
    (no new State field). Capped at 2 corrections. Gated to local MLX
    (FUSION_MLX_ENABLED / FUSION_MLX_AUTO).
  2. Bench metric (benchmarks/harness-gain/run.ts): per-task
    textEmissions count so engine regressions are visible from the suite.
  3. Driver fix (benchmarks/harness-gain/run.ts): pass
    --dangerously-skip-permissions in -p mode — the suite runs in
    throwaway temp git repos with no network; without it the agent dies on
    the first Edit permission error.
  4. Debug gate: FUSION_CODE_DEBUG_RECOVERY=1 prints the recovery-gate
    variables to stderr for field diagnosis.

Verification

  • Unit: 7 tests for detectToolCallShapedText (python-call style, multiline,
    call_ prefix, wrapper, unknown-name prose rejected, lowercase rejected,
    empty) — all pass.
  • E2E (fusion-mlx 0.10.68 + Qwen3-8B, real model): recovery fires on every
    tool-shaped text turn, attempt cap engages, Read-shaped calls recover to
    real tool_use and the task completes. Edit-shaped calls re-emit as text
    after 2 corrections — 8B model limitation, not a harness bug (documented
    in the research doc).
  • Suite T01: behavior unchanged vs baseline (resolved=false, wall ~72s),
    confirming no regression on non-emitting paths.

Research context

Full survey + ranked options:
docs/research/small-model-tool-calling-2026-11.md (docs branch).

🤖 Generated with Claude Code

dahai80 and others added 8 commits November 21, 2026 10:13
Small local models served by fusion-mlx intermittently print a tool
call as plain assistant text (Read({...}), call_<id>{...}) instead of
emitting a tool_use block. The turn then looks like a finished text
answer, isResultSuccessful passes on the text block, and the session
ends after one such turn - the dominant failure mode in the
harness-gain suite on Qwen3-8B (see
docs/research/small-model-tool-calling-2026-11.md).

Add a recovery pass in the query loop before stop hooks: when the
final assistant text block matches a tool-call shape (python-call
style with a known tool name, call_<id>{ prefix, or <tools>{ wrapper)
and the provider is local MLX, feed the turn back with an explicit
format-correction meta message and continue the loop, mirroring the
existing max-output-tokens recovery pattern. Capped at 2 corrections;
attempt count derived from marker messages in history, no State field
added. Guarded by FUSION_MLX_ENABLED/FUSION_MLX_AUTO so cloud
providers are untouched.

New Transition reason 'tool_call_text_recovery' in
src/query/transitions.ts. 7 unit tests for the shape detector
(tests/utils/toolCallTextRecovery.test.ts); typecheck clean.

Co-Authored-By: Claude Code <[email protected]>
Count tool-call-shaped plain-text emissions in each harness run's
stdout (python-call style Name({...}), call_<id>{...}, <tools>{...}
wrapper). The metric is the standard instrument for the small-model
tool-calling reliability track (see
docs/research/small-model-tool-calling-2026-11.md section 4): it
already distinguishes fusion-mlx 0.10.67 (near-universal emission)
from 0.10.68 (occasional) and gives the trainer flywheel a
before/after number for MFC-style fine-tuning.

Aggregated as textEmissions in summary.json and logged per task.
Detector validated against real run logs: catches both observed
formats, zero false positives on a clean task log.

Co-Authored-By: Claude Code <[email protected]>
- Runner: add --dangerously-skip-permissions to the fusion-code driver.
  -p runs cannot grant write permissions interactively; the suite works
  in throwaway temp git repos with no network, so blanket-skip so the
  agent can actually attempt each task.
- Query loop: FUSION_CODE_DEBUG_RECOVERY=1 prints the recovery-gate
  variables (isLocalMlx, aborted, attempts, toolNames, lastText) to
  stderr for field diagnosis of the tool-call-as-text recovery loop.

E2E verified against fusion-mlx 0.10.68 + Qwen3-8B: recovery fires on
every tool-shaped text turn, attempt cap (2) engages, Read-shaped calls
recover to real tool_use; Edit-shaped calls re-emit as text (8B model
limitation, not a harness bug).

Co-Authored-By: Claude Code <[email protected]>
Qwen3-8B re-emits identical Edit-shaped text through all recovery
corrections (verified E2E) while Read-shaped calls recover on the first.
On the second correction, tell the model to prefer whole-file Write —
partial edit arguments are the hard part for small models (aider
editing-format finding: weak models do better whole than diff).

Co-Authored-By: Claude Code <[email protected]>
T13's fixture contains an unfixed infinite-retry loop; pytest on the
untouched file hangs forever (verified: killed by SIGKILL at the 60s
cap). A verifier must fail, never hang, on an untouched fixture — same
principle as the CRLF trap fixed in #297. Applies a 60s timeout to the
pytest / command-exit / command-stdout verifiers; timeout counts as
unresolved.

Found when the opencode suite wedged 4h on T13 (its agent's edit was
permission-rejected, leaving the fixture unfixed).

Co-Authored-By: Claude Code <[email protected]>
--text and -t are the same flag in goose 1.60.2; passing both ate the
prompt. OPENAI_HOST must not carry a /v1 suffix (goose appends the
path). Provider/model/key/host now wired from BENCH_MODEL and the
engine key like the fusion-code driver.

Smoke-tested live: Qwen--Qwen3-8B answers via fusion-mlx.

Co-Authored-By: Claude Code <[email protected]>
- GOOSE_MODE=approve is invalid headless (goose errors on first tool
  call); auto is the only valid non-interactive mode. Smoke-tested live.
- T08 verifier trap: dedup refactor verified by behavior tests that pass
  on the untouched fixture (opencode T08 resolved=true was a false
  positive). New count-max verifier bounds duplicate-marker occurrences;
  untouched fixture fails at 3, deduped file passes at 1.

Co-Authored-By: Claude Code <[email protected]>
@dahai80
dahai80 merged commit 1d0da17 into main Nov 21, 2026
1 check passed
@dahai80
dahai80 deleted the fix/tool-call-text-recovery branch November 21, 2026 11:56
@dahai80 dahai80 mentioned this pull request Nov 21, 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