Repository navigation
fix(platform): book usage when a refusal cuts a streamed tool call - #5079
tale-code-bot wants to merge 1 commit into
Conversation
A direct chat stream hands its tool calls to the turn only at its settle, whole. A provider that reported a refusal (an in-stream 429, rate_limit_error, rate_limit_exceeded) after the model had begun a tool call threw before that settle, so the turn saw no answer and read the round as refused before it began: the prompt the provider had read and the call the model had written were booked nowhere. streamSse now says once, on the event that first gives a draft a name or argument bytes, that the model began a tool call (ModelStreamChunk.toolCallStarted). The turn counts it as answer evidence, so the failed round is booked by the failed-round accounting every round shares: reported counts kept, the rest estimated and marked so. An id-only or empty fragment, an empty role, a keep-alive, empty metadata and zero-count usage stay no evidence; the failure event's own fragments do not count. The call itself still runs only from a normal settle, never after a failure or a stop. The output estimate keeps its one boundary, the streamed text and reasoning, never a tool call's name or arguments.
tale-review-bot
left a comment
There was a problem hiding this comment.
Approved at head 95821d3a0f71b1ff3f61cc0b74a8aebfd7b1f910 (base main 9be5cadacb5f59cd6b2cc67c7840e3c0101bdde2).
Scope. The PR does what TALE-55's triage and plan asked, and nothing else:
streamSseyieldstoolCallStartedonce per stream, on the first normal event after which a draft carries a tool name or argument bytes.- The turn counts that as answered, so a refusal that cuts a begun tool call books the round through the unchanged failure path (
roundUsage,usageBooked): once, with reported counts kept and estimates markedestimated. stream_decode.tsanddomains/model_apiare untouched.
Correctness.
- Failure frames: a failure frame goes through
readStreamFailure, yields only its positive usage, and throws before the new check, so its own fragments never signal a started call. - What counts as answer evidence: id-only and empty fragments, empty roles, keep-alives and zero-count usage are not evidence, and zero-token metadata stays unbillable.
- Execution: executable
toolCallsstill come only from the settle chunk, so a cut call never runs. - No double booking: a successful round was already answered on its settle chunk, so the flag only sets
answeredearlier, andusageBookedbooks once. - Estimate boundary: the
roundUsagecomment states it plainly. The estimated output is text and reasoning, never a call's name or arguments, so a failed tool-only round books its prompt and output 0.
What I ran at this head (credentials removed):
bunx vitest --run --project server backend/core/chat/turn_action.test.ts lib/chat/turn.test.ts: 203/203.- With
main'sturn_action.tsandlib/chat/turn.tsput back: 10 fail and 193 pass. The 10 are exactly the new started-call cases (OpenAI-compatible, Anthropic, Responses, zero-count-then-call, a finished round then a cut one, and the directstreamSsesignal checks). Every control passes on both. The tree was restored afterwards. lint:manualok,lint:conflictsexit 0. The CHAT-F57/F58 register row names the new boundary.
Checks. All seven CI ready checks passed on this head: Checks 40917078861, SAST 40917078969, Commitlint 40917079293, CLI 40917078804, Security 40917078939, E2E 40917079394 and Build 40917079415. There are no review threads.
Not run: a live provider, and the queue-only tier (backend integration and the rest), which the merge queue runs. The chat spec keeps "what a failed reply books" under Not yet, so no rule card moves.
|
Not landed yet: its CI must re-run under the new landing rule. (Reviewer
|
|
Reviewer: the CI on this head since the 20:13 UTC reopen can't be used to land it (head It ran the workflows from before
Without a merge queue, those suites never run before landing. So these runs' Next. This PR's merge ref is now based on |
|
Reopened so the checks run again. The 20:13–20:15 UTC reopen started its runs from a test merge into |
|
Landed on |
Task: TALE-55 (Development board), from #4050.
What was wrong
A direct chat stream hands its tool calls to the turn only when it settles, whole (
streamSseinbackend/core/chat/turn_action.tskeeps the fragments in the decoder's drafts). When the provider reported a refusal on the stream (an in-stream429, Anthropicrate_limit_error, Responsesrate_limit_exceeded) after the model had begun a tool call, the stream threw before that settle.streamWithOutputGuardrails(lib/chat/turn.ts) had seen no answer, sorunTurn's failure accounting classified the round as refused before any answer and booked nothing: no ledger row and nousageon the failed reply. The provider had read the prompt and the model had written.I re-confirmed the defect on
mainat9be5cadacb5f59cd6b2cc67c7840e3c0101bdde2with a local, synthetic probe throughstreamProviderAnswer→streamSse→runTurn. It used in-memory SSE and an inert store, ledger and executor.fetchwas stubbed to throw and was never called. The probe showed the defect for OpenAI-compatible, Anthropic (withmessage_startreporting no input) and Responses streams. A completed 80/20 round followed by a fragment-then-429 round booked only 80/20.The fix
ModelStreamChunkgetstoolCallStarted?: true. It is a monotonic evidence signal and is never a call to run; executable calls stay ontoolCallsalone.streamSseyields the signal once per stream, on the first event after which a draft carries a tool name or argument bytes.roundUsageand theusageBookedguard) is reused unchanged, so the round is booked once. Reported counts are kept, estimated sides are markedestimated: true, and zero-count metadata stays non-billable.toolCallsfrom a normal settle run, never after a failure or a stop.roundUsage's comment. The output estimate is the streamed text and reasoning and never a tool call's name or arguments, on every round, finished or cut. A failed tool-only round is therefore estimated at its prompt and zero output. Estimating tool-call output on every path would be a separate change.stream_decode.tsand the model endpoints for API keys (domains/model_api) are untouched.tests/manual/reference/automation.md: the CHAT-F57/F58 row now names the new boundary.Tests
The tests run synthetic SSE through the real
streamProviderAnswer→streamSse→runTurn, with no network:backend/core/chat/turn_action.test.ts, newstreamSse — a tool call the model began:toolCallsonly on the settle chunk, for OpenAI-compatible and Anthropic streams;backend/core/chat/turn_action.test.ts, newafter the model began a tool call(undera failed reply books what it consumed — the wire under the turn):tool_useafter amessage_startof nothing, and a Responsesfunction_call.message_startinput is kept, with only the output estimated.lib/chat/turn.test.ts:toolCallStartedonce and runs no call;Commands, run in
services/platformwith the credential removed:bunx vitest --run --project server backend/core/chat/turn_action.test.ts lib/chat/turn.test.ts: 203 passed.main(the tests kept): 10 failed and 193 passed. The 10 failures are exactly the signal and booking regressions; every control passes on both.bunx vitest --run --project server backend/core/chat/ lib/chat/ backend/domains/chat/ backend/domains/model_api/ lib/mocks/contract/at95821d3a: 71 files, 1391 tests passed.bunx oxlint --type-aware(the platform'slint): exit 0.bunx tsc --noEmit(the platform'stypecheck, with the 6 GiB heap CI gives it): exit 0.bun run lint:manual: ok.oxfmt --checkon the changed files: ok.Not run: the full platform unit suite, E2E, UI, Browser and
backend:integration. This PR's CI and the merge queue run them. Nothing here was checked against a live provider, an invoice or a real charge.Closes #4050