Skip to content

fix: agent turns end early on unoffered tool names and on long CUDA tool calls - #99

Closed
DevRico003 wants to merge 2 commits into
ashhart:mainfrom
DevRico003:fix/pass-unknown-tool-names
Closed

DevRico003 wants to merge 2 commits into
ashhart:mainfrom
DevRico003:fix/pass-unknown-tool-names

Conversation

@DevRico003

Copy link
Copy Markdown

Summary

Two things end an agent's turn early when TensorFold serves a coding harness. Both showed up on one DGX Spark (GB10) running Qwen3.8-Flash-Next (MLX 4-bit + MTP, the MiaAI-Lab recipe) on TensorFold 0.3.6.3, driven by DeepSeek Harness (DSH Desktop 2.0.16) through the OpenAI chat API.

  1. A call to a tool the request did not offer stays text. The model sometimes calls a near-miss name (kill where the request offers job_kill). The parser keeps the whole block as content, so the harness sees prose, not a call, and ends the turn. A server that returns the call lets the harness answer "unknown tool" and the model corrects itself on the next step.
  2. The CUDA server sends nothing while a tool call is being written. A reply that writes a large file in one write call streams no bytes until the call closes. Node's fetch (undici) drops a response body that is silent for 300 s, the harness retries, the same prompt regenerates the same tokens, and it fails again at the same place.

This PR adds an opt-in flag for (1) and a keepalive for (2). References are at main (1911880).

1. Unoffered tool calls: opt-in pass-through

parse_tool_calls_from_content (src/tensorfold/server/tools.py:376-380) and the CUDA parser (src/tensorfold/cuda/reply_text.py:140) both keep a well-formed call to an unoffered name as text. That is deliberate since #51, and tests pin it (tests/test_deepseek_v4_prompts.py:65, tests/test_tool_draft.py:94), so the default does not change.

With TENSORFOLD_PASS_UNKNOWN_TOOLS=1 (also true, yes, on) both parsers return such a call as a tool call under the name the model wrote. Offered names keep their declared spelling. Malformed blocks stay text either way: no name, non-object arguments, unparsable JSON. max_calls still takes the first call.

The streaming ToolCallStreamer is unchanged: it still aborts on an unknown name and the end parser decides, which now returns the call when the flag is set.

2. CUDA stream keepalive

App.run hides a tool call until it closes (src/tensorfold/cuda/server.py:302-304) and the handler emits calls only after run returns (:532-535). In between, on_tokens emits only when there is visible text (:332).

on_tokens now emits an empty delta when nothing visible has been sent for KEEPALIVE_SECONDS (15 s). An empty delta is a valid chat chunk that clients skip; for /v1/completions it is an empty text. The cancellation check runs as before, every round. Replies faster than 15 s between visible pieces stream exactly as before.

Tests

  • tests/test_tool_call_unknown_name.py, parametrised over both parsers: an unoffered call stays text by default (including one with an unclosed <parameter>), reaches the client with the flag, offered names keep spelling and order next to an unknown one, max_calls=1 takes the first, malformed blocks stay text with the flag.
  • tests/test_cuda_stream_keepalive.py, on the fake CUDA engine from test_think_call.py: with KEEPALIVE_SECONDS = 0 a hidden tool call streams empty deltas and still ends with the full call and finish_reason: tool_calls; with the default a fast reply sends none.

The new keepalive test fails on main and passes here. On macOS without mlx, the full suite gives the same 81 failures and collection errors on main and on this branch (all from the missing mlx); nothing new fails. I could not run the MLX lane.

On the GPU

One GB10, the MiaAI-Lab Qwen3.8-Flash-Next recipe with both changes applied to its image, TENSORFOLD_PASS_UNKNOWN_TOOLS=1.

Unknown names, API directly: a history where the assistant had already called kill, the request offering job_kill and bash or only bash, 3 runs each, streamed and not:

Calls returned as tool_calls
main 0 of 12 (6 streamed, 6 not)
this branch, flag on 12 of 12

Returning Error: unknown tool 'kill' as the tool result, the next reply called job_kill.

Keepalive, one write call of a 784-line file with thinking off: 108 s for the reply, longest gap between SSE lines 15.0 s, 7 empty deltas, the call arrived whole. Before, that was 108 s of silence.

Keepalive in the harness: the same DSH session prompt (a self-playing voxel football match in one HTML file, effort max) on main failed step 4 twice with terminated at 423 s, each time 301 s after the last byte, while the model was writing a full-file write (29,774 output tokens). With this branch the same prompt ran 106 minutes to completion: 160 tool calls, six full-file write calls up to 62,810 characters, 0 transport retries.

Not covered

  • The Mac lane with streaming: when ToolCallStreamer has streamed a known call and the reply then calls an unknown one, server/http.py:354 skips the end parser's calls because some were streamed, so the unknown call is dropped. That happens on main too; this PR does not change it.
  • A non-streamed reply still sends its headers only when done.
  • The keepalive covers decode. A very long prefill before the first token is still silent.

…WN_TOOLS

A well-formed call whose name the request did not offer stays text, so an
agent harness sees prose and ends its turn. With the flag set, both the Mac
lane parser and the CUDA parser return the call, the client answers "unknown
tool", and the model corrects the name. Default behaviour is unchanged.
The CUDA server hides a tool call until it closes, so a reply that writes a
large file in one call sends nothing for minutes. Clients with a read
timeout drop the connection (Node's fetch does at 300 s), retry, regenerate
the same tokens and fail again. An empty delta every 15 s while nothing
visible is ready keeps the connection open; the reply is unchanged.
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