Skip to content

fix(llm): parse emulated tool calls from content when the gateway ignores tool_choice (#129) - #216

Merged
shhdwi merged 2 commits into
trailhq:mainfrom
Frankie-Xu:fix/129-toolcall-content-fallback
Aug 27, 2026
Merged

shhdwi merged 2 commits into
trailhq:mainfrom
Frankie-Xu:fix/129-toolcall-content-fallback

Conversation

@Frankie-Xu

Copy link
Copy Markdown
Contributor

Summary

  • Failure mode 1: ChatSynthesizer (and the shared helper now used by ChatCruxSummarizer) only read res.toolCalls[0]. Gateways that ignore forced tool_choice (deepseek-v3.2) put [{"name":"emit_json","parameters":{...}}] in content with tool_calls: null, so synthesize returned [] with no warning. When toolCalls is empty, conservatively parse content as tool-call JSON (array, single object, or fenced block). Recovered args go through the same clean() / parseResults() validation as a real tool call. Unparseable or empty → one stderr line, no guessing.
  • Same retry path as fix(deep): retry pending nodes — don't cache empty meaning summaries (#172) #177: an empty synthesis batch is not cached as success (the meaning-tier trap fix(deep): retry pending nodes — don't cache empty meaning summaries (#172) #177 closed). The next --deep retries. Failed content recovery does not invent a second error channel.
  • Failure mode 2 (logging only): each synthesis batch logs nodes / links. A multi-batch run that produces 0 links gets one summary warning at the end of build. No quality scoring.

christian-jorge's #107/#111 were the adapter-level attempt at mode 1 (plus a corrective retry). They closed unmerged, and origin/main still only read toolCalls[0]. This PR redoes the remaining #129 slice at the call site, matching #177, and does not reopen those branches or change provider protocols.

#177 already recovered crux payloads from content (meaning pass). This PR shares that parser and covers synthesis. Together they address both silent failures in #129.

Test plan

  • tool_calls null + content JSON array (emit_json) → nodes
  • single-object wrapper and fenced JSON payload
  • both empty → stderr warning, []
  • prose content → warning, no throw
  • real toolCalls preferred over content JSON
  • truncated / wrong-tool JSON refused
  • empty synthesis is not cached; next build retries
  • multi-batch 0-link run logs per-batch counts and a summary warning
  • tsc --noEmit clean
  • CI npm test on Node 20 (local Node 26 cannot load tree-sitter-kotlin prebuilds — pre-existing, unrelated)

Closes #129

Made with Cursor

…ores tool_choice (trailhq#129)

DeepSeek-class gateways drop tool_calls and write the payload in content, so
ChatSynthesizer returned [] with no warning. Recover the same JSON conservatively
(array / object / fence), reuse trailhq#177's empty-result retry, and log per-batch
node/link counts so a 0-link multi-batch run is visible.

Co-authored-by: Cursor <[email protected]>
Comment thread src/ai/llm/recover-tool.ts Fixed
github-actions Bot added a commit that referenced this pull request Aug 25, 2026
@github-actions

github-actions Bot commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

🌱 graft blast radius

3 areas changed → 2 areas can be affected. 2 dependent symbols, depth 2.
Tests: no test reaches AI Synthesis, LLM Tool Recovery; 1 area updated its tests.

flowchart TB
  A0(("Workspace CLI<br/>1 symbol"))
  A1(("Engine Initialization<br/>1 symbol"))
  classDef reached fill:#D9EDF3,stroke:#3AA7C9,stroke-width:1.5px,color:#0E313C;
  class A0,A1 reached;
Loading
Can be affected Symbols Nearest hop Reached from
Workspace CLI 1 src/graph/workspace-cli.ts:L47-L65 buildChild — calls, depth 2 Build Context
Engine Initialization 1 src/engine.ts:L59-L68 init — calls, depth 1 Build Context
All 2 dependent symbols, grouped by area

Workspace CLI — 1 symbol in 1 file

  • src/graph/workspace-cli.ts:L47-L65 — buildChild (calls, depth 2)

Engine Initialization — 1 symbol in 1 file

  • src/engine.ts:L59-L68 — init (calls, depth 1)
    60: return buildContext(dir, {
Test signal per changed area — 1 ✓ · 2 ✗

Reached = a node under a test path has a resolved edge into the changed symbol. It undercounts anything called indirectly — through a CLI, a spawned process or a dynamic import — so read a low ratio as “look here”, never as a coverage gate.

  • ✓ Build Context — 1 of 1 reached · 1 test file changed here: test/context.test.ts
  • ✗ AI Synthesis — 0 of 3 reached · no test file reaches it
    • not reached: argsFromResponse, synthesize, nodesFromResponse
  • ✗ LLM Tool Recovery — 0 of 3 reached · no test file reaches it
    • not reached: asPayload, unwrapMarkdownFence, warnToolChoiceIgnored
3 test suites also reference this code

3 symbols, kept out of the diagram and the table so they cannot crowd out the areas a reviewer has to look at.

  • test/context-checkpoint.test.ts
  • test/covers.test.ts
  • test/utf16-source.test.ts

graft blast · origin/main...HEAD · depth 2 · 6 changed files

Open the interactive graph → — click an area to see its dependent symbols at file:line.

github-actions Bot added a commit that referenced this pull request Aug 25, 2026
github-actions Bot added a commit that referenced this pull request Aug 25, 2026
@shhdwi

shhdwi commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

Verified locally on top of current main (clean merge; build clean).

Fixes #129: extracts a shared, ReDoS-safe recoverToolArgsFromContent() and wires it into BOTH crux and ChatSynthesizer (which on base main did if (!call) return [] and never looked at content). Now both prefer a real tool call, else parse the JSON tool payload from message content (fenced, array, or bare object), validated through the same clean().

Empty→populated proof (PR's own tests, passing): a fake model returning the payload in text with toolCalls: [] yields nodes.length === 1 / "Auth" (base main returns [] — the deepseek-v3.2 repro). A real toolCalls payload is still preferred; unparseable prose warns and returns [] without throwing; the 8k-space ReDoS guard stays linear. Also addresses the silent-degradation half: empty batches aren't cached, so the next build retries. llm-ops/context/llm-adapters 47/0. Fixes #129. Merging.

@shhdwi
shhdwi merged commit def0cf4 into trailhq:main Aug 27, 2026
4 of 7 checks passed
github-actions Bot added a commit that referenced this pull request Aug 27, 2026
FrancescoMerenda pushed a commit to FrancescoMerenda/Graft that referenced this pull request Sep 7, 2026
…ores tool_choice (trailhq#129) (trailhq#216)

* fix(llm): parse emulated tool calls from content when the gateway ignores tool_choice (trailhq#129)

DeepSeek-class gateways drop tool_calls and write the payload in content, so
ChatSynthesizer returned [] with no warning. Recover the same JSON conservatively
(array / object / fence), reuse trailhq#177's empty-result retry, and log per-batch
node/link counts so a 0-link multi-batch run is visible.

Co-authored-by: Cursor <[email protected]>

* fix(llm): parse gateway tool-call content without backtracking regex (trailhq#129)

Co-authored-by: Cursor <[email protected]>

---------

Co-authored-by: Cursor <[email protected]>
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.

synth/crux read results only from toolCalls; gateways that ignore forced tool_choice silently produce empty/degraded graphs

3 participants