Skip to content

fix: refuse unfinished tool arguments, end a run truncated without output, and guard repeated tool calls - #381

Open
liuedcson wants to merge 7 commits into
mainfrom
fix/reject-incomplete-tool-arguments
Open

liuedcson wants to merge 7 commits into
mainfrom
fix/reject-incomplete-tool-arguments

Conversation

@liuedcson

@liuedcson liuedcson commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A provider can cut a reply off in the middle of a tool call. The best-effort streaming parser filled in whatever arguments had arrived, so the call ran with arguments the model never finished writing: a write truncated at the output token limit reached the filesystem as a truncated file. The same reply then stayed in the transcript and was resent as history on every later request, so one bad turn could seed the next.

Three separate gaps made that possible, a purely empty truncated turn had no handling at all, and a run stuck reissuing the same calls was never noticed.

Changes

A call whose arguments never arrived whole is refused (providers)

Streamed deltas are still parsed speculatively so a UI can render a call in progress. A finished call now carries only what the provider completed: parseFinalToolArguments repairs invalid escapes, which restores the encoding the model meant, but never fills in missing structure. When the arguments cannot be read, the block keeps empty arguments and an argumentsError, and validateToolArguments refuses it, so the model gets a tool error it can re-issue.

Two provider gaps that let an unfinished call look finished close with it: a stream that stops after message_delta without closing a tool block, and a block whose scratch buffer survived finalization, are reported rather than executed on the last streamed preview.

A turn that hit the output limit with nothing to act on ends the run (coding-agent)

A length stop with no visible text and no tool call used to end as if it had finished. It is now truncated_without_output and reported as the run's failure on agent_settled, so print, JSON, subagent lanes and goal continuation all see it. It does not enter the empty-turn recovery loop: a recovery prompt would resend whatever filled the budget.

isRecoverableLength is unchanged. It answers "is a compact-and-retry worth asking for", which is a different question from "was this truncated": a reply that spent its whole budget cannot be helped by compacting, so flipping its comparison would make every max-output stop trigger a retry it cannot fix.

An oversized historical reply is bounded in the request view (agent-core)

The cap is a size bound, not a repetition detector. Repetition needs a threshold that misfires on a generated table, a file listing or a detailed report; the size of one reply is a fact. The bound follows what the model could actually have written — its output limit at four characters a token, with a 400,000-character floor — so a legitimate answer is never shortened.

A repeated or cycling tool call is noticed, and an unrecoverable repeat is stopped (coding-agent)

Two shapes are recognised, both keyed by tool name and the complete arguments:

  • One call repeating is reported at the third consecutive success and stopped at the tenth.
  • A cycle of 2 to 4 distinct calls repeating in the same order three times (ABAB, ABCABC) is reported once. It is never stopped: a cycle can be a legitimate retry loop over external state, and a notice costs a few lines while a stop costs the run.

Comparing the complete arguments is what keeps ordinary work out. An edit that changes something followed by a test is keyed by different arguments, so a real edit/test cycle is ABCB, not ABAB, and does not match. Only a cycle reissuing the exact same calls does.

At most one notice joins a request, so a stuck run cannot spend prompt space repeating it. A failed or blocked call resets both counters — a retry after an error is not a repetition — and a new user message starts a new count.

Both guard shapes are controlled by [tools] repeat_call_guard (JSON config: tools.repeatCallGuard), default on, read on every call so a reload takes effect without a new session.

Behavior and compatibility

  • A call whose JSON cannot be read is no longer executed. Arguments that only needed an invalid escape repaired still run.
  • A length stop with no output now fails the run instead of ending quietly.
  • Historical replies above the model's own output budget are shortened in the request view. The transcript, the session file and compaction boundaries keep the original, and the latest reply is untouched.
  • Session files gain an optional argumentsError on a tool-call block and a third incomplete value. Readers that ignore unknown fields are unaffected; a stored call with argumentsError is refused again on replay rather than executed.
  • The guard's notice is a hidden request-only message; it never enters the transcript. Its stop blocks the tenth identical call and ends the turn.
  • [tools] repeat_call_guard = false turns both the notice and the stop off. One switch covers both shapes; there is no finer split.

Validation

  • pnpm run check — clean, including the regenerated config schema.
  • packages/providers 527 passed / 6 skipped; packages/agent-core 583 passed / 1 skipped; packages/coding-agent 5801 passed / 59 skipped.
  • Each new test fails on main and passes here: truncated arguments, the unclosed-tool-block cases for two dialects, the truncated_without_output classification, the repeat limits, cycle detection, the guard switch in TOML and JSON, and the reply cap.

Limitations

  • The reply cap is intentionally loose. It bounds the absurd, not the merely long: at a 64k-token budget a legitimate English reply reaches roughly 256k characters, so a tighter cap would shorten real answers. A degenerate reply that fits inside the budget is not shortened by it; the truncated-arguments refusal is what stops that case from causing work.
  • Cycle detection compares call identity only. A loop that varies its arguments every round is not treated as a cycle, and is bounded only by the step limit.
  • Four tests in this environment fail identically on an unmodified main: json-mode-durability (three, from a stale local dist built before a later export existed) and subagent-invocation (one, a unix socket path over the macOS length limit). None of the files they exercise are touched here.

Streamed tool-call deltas are parsed speculatively so a UI can render a call
before it is complete. That parse was also what the finished call kept, so a
stream that ended inside the arguments left a call the model never wrote: a
write cut off at the output token limit reached the filesystem as a truncated
file instead of failing.

A finished call now carries only what the provider completed.
`parseFinalToolArguments` still repairs invalid escapes, which restores the
encoding the model meant without inventing structure, but it never fills in
missing structure. When the arguments cannot be read the block keeps empty
`arguments` and an `argumentsError`, and `validateToolArguments` refuses it, so
the call comes back to the model as a tool error it can re-issue.

Two provider gaps that let an unfinished call look finished are closed with it:
an Anthropic stream that stops after `message_delta` without closing a tool
block, and a block whose scratch buffer survived finalization, are both
reported instead of being executed on the last streamed preview.
…o act on

A provider that reported `length` with no visible text and no tool call ended
the run as if it had finished. Nothing was recorded, nothing was retried, and
the turn read as ordinary completion even though the model had spent its whole
output budget and produced no work.

The turn is now classified as `truncated_without_output` and reported as the
run's failure on `agent_settled`, so print, JSON, subagent lanes and goal
continuation all see it. It does not enter the empty-turn recovery loop: a
recovery prompt would resend whatever filled the budget, which is how a
degenerate reply becomes the input for the next one.

A truncated turn that did produce text or a tool call keeps its existing
handling — compaction owns that case, and `isRecoverableLength` still decides
whether a compact-and-retry is worth asking for.
…view

A reply that filled its whole output budget with a degenerate loop stays in the
transcript, and every later request resends it. That makes one bad reply the
input for the next one, so a single failure can seed a series of them.

The cap is a size bound, not a repetition detector. Judging whether text looks
repetitive needs a threshold that misfires on legitimate long output — a
generated table, a file listing, a detailed report. The size of one reply is a
fact, so the bound is derived from what the model could actually have written:
its output limit priced at four characters a token, with a 400,000-character
floor. An answer the model could legitimately emit is never shortened.

Only the request view changes. The transcript, the session file and compaction
boundaries keep the original, the most recent reply is left alone because it is
the current turn, and the rewrite is deterministic so a shortened prefix stays
byte-identical and the provider cache stays warm.
A model can keep issuing one call — reading the same file, running the same
command — and nothing in the loop notices. The signal is deliberately exact:
tool name plus canonical arguments, compared by length and a digest so a
written file body never has to be held in memory. That is a fact about the
request the client sent, not a guess about how repetitive the wording looks, so
no ordinary answer can trip it.

Three consecutive successful identical calls add one hidden notice to the next
request. Ten stop the call and end the turn. Stopping is far from the reminder
because repeats can be legitimate, and blocking is only for a loop that already
ignored the notice.

At most one notice joins a request, so a stuck run cannot spend prompt space on
repeating it. A failed or blocked call resets the streak, because a retry after
an error is not a repetition, and a new user message starts a new count.
…igurable

The repeat guard recognised one call repeating. A run that alternates between
two or three calls — read, test, read, test — never extended a same-call streak,
so it went unnoticed however long it ran.

A cycle is now detected as the shortest period of 2 to 4 calls that repeats
three times in a row, compared with the same key the streak uses: tool name plus
the complete arguments. That comparison is what keeps ordinary work out of it. An
`edit` that changes something followed by a `test` is keyed by different
arguments, so a real edit/test cycle is `ABCB`, not `ABAB`, and does not match.
Only a cycle reissuing the exact same calls does.

The cycle is reported, never stopped. A repeat cycle can be a legitimate retry
loop over external state, and a notice costs a few lines while a stop costs the
run. One call repeating keeps its own limits — reported at three, stopped at
ten — and is excluded from the cycle check so `AAAAAA` is not reported twice.

Both are now controlled by `[tools] repeat_call_guard` (JSON config:
`tools.repeatCallGuard`), default on. It is read on every call, so a reload takes
effect without a new session.
…-tool-arguments

# Conflicts:
#	packages/providers/src/api/anthropic-messages.ts
#	packages/providers/src/api/openai-completions.ts
Every configurable top-level key needs a row in the settings table, and the
guard added one without it.
@liuedcson liuedcson changed the title fix: refuse unfinished tool arguments and end a run truncated without output fix: refuse unfinished tool arguments, end a run truncated without output, and guard repeated tool calls Dec 2, 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