Skip to content

provider: a call stopped before its first streamed word keeps its charge (#2566) - #2592

Merged
AbirAbbas merged 2 commits into
devfrom
fix/2566-receipt-before-id
Nov 11, 2026
Merged

AbirAbbas merged 2 commits into
devfrom
fix/2566-receipt-before-id

Conversation

@AbirAbbas

Copy link
Copy Markdown
Collaborator

What was wrong (#2566)

A paid streamed call cancelled before its first SSE frame arrived (Esc, a quit, a factory job withdrawn) lost its charge. The provider bills as soon as it accepts the request, but the socket was cut at once, so the generation id, which the first frame carries, never reached codeaf. Without that id Client.settle has nothing to ask a receipt for. The ledger got neither a priced reconciled row nor the unbilled marker.

An abandoned hedged race had a second, smaller gap. It gave back its caller's pending-spend hold as soon as it returned, before its cut arms had queued their receipts. So a closer waiting on pending spend could see nothing owed and stop waiting while a receipt was still on its way.

This was measured on land/app-v3 with TestMemoryLateReceiptReachesFactoryHeaderAfterQuit, which failed 12 of 15 runs before the fix and passed 20 of 20 after it. This PR is the same fix ported to dev.

What changes

  • internal/provider/client.go: when a streamed call has a receipt sink and its caller cancels, the call waits for its generation id before cutting the socket.
    • It keeps the socket open for at most receiptStreamIDWait (100 ms) and cuts the moment the first frame names the generation.
    • A call nobody reconciles, and a direct service (no receipt route), are still cut at once.
    • The cut carries the caller's own context.Cause, as the child context it replaces did. Rows and errors that read the cause see the same door as before.
  • internal/provider/hedge.go: abandon takes one WithReceiptPending token before it returns and hands it back only after the drain has heard from every arm, or after abandonGrace (1 s) if an arm never reports.
  • Manual (models-and-cost.md): one sentence saying that a stop before the first word keeps the stream open at most a tenth of a second.

The person's Esc does not wait on a shipped build. Every call is raced, the race answers its caller at once, and only the arm holds its socket. A build with no waiting controller returns only when the stream is cut, so there an Esc can wait up to the same 100 ms.

Not fixed here

Tests

internal/provider/receipt_cancel_test.go, one test per contract item. C1 to C4 run through both the raced and the bare stream door.

Contract
C1 A cancel after the request reached the provider and before the first frame, with the frame arriving within the hold: the receipt is asked for and booked.
C2 A cancel before the request is sent returns at once with the caller's error, and nothing reaches the provider.
C3 A cancel on a silent wire: the hold is bounded.
C4 A call nobody reconciles, or a direct service, is cut at once with no added wait.
C5 An abandoned race keeps the pending-spend token until its cut arm settles, and gives it back within abandonGrace even if an arm never reports.

Local gates

  • gofmt, go build ./... and go vet ./internal/provider/ are clean.
  • go test -count=1 ./internal/provider/ passes.
  • The new tests pass ten times in a row (-count=10).
  • go test ./internal/manual/ and make changelog-check pass.
  • make test-laws passes.
  • make pr-ready passes.

🤖 Generated with Claude Code

AbirAbbas and others added 2 commits November 10, 2026 21:59
…rge (#2566)

A paid streamed call cancelled before its first SSE frame lost its charge.
The provider bills as soon as it accepts the request, but the socket was cut
at once, so the generation id the first frame carries never arrived and
Client.settle had nothing to ask a receipt for. The ledger got neither a
priced reconciled row nor the unbilled marker.

A stream that reconciles receipts now keeps its socket open for at most
receiptStreamIDWait (100 ms) after its caller lets go, and cuts it the
moment the first frame names the generation. A call nobody reconciles, and a
direct service, are still cut at once. The cut carries the caller's own
context cause, as the child context it replaces did.

An abandoned hedged race also gave back its caller's pending-spend hold as
soon as it returned, before its cut arms queued their receipts. abandon now
holds one pending token until the drain has heard every arm, or for at most
abandonGrace.

On land/app-v3, TestMemoryLateReceiptReachesFactoryHeaderAfterQuit failed
12 of 15 runs before this fix and passed 20 of 20 with it. The new tests
C1-C5 in internal/provider/receipt_cancel_test.go fail against dev's
original code and pass with the fix.

Not fixed here: a receipt still pending only in memory when the process
exits (#2591's other half).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@AbirAbbas
AbirAbbas merged commit 9b98d51 into dev Nov 11, 2026
6 checks passed
@AbirAbbas
AbirAbbas deleted the fix/2566-receipt-before-id branch November 11, 2026 03:36
AbirAbbas added a commit that referenced this pull request Nov 11, 2026
Brings the receipt fix for a call cancelled before its first streamed
frame (#2566) onto the landing branch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
AbirAbbas added a commit that referenced this pull request Nov 11, 2026
The landing branch has a second receipt waiter that dev does not:
WithReceiptSettlement, which the factory's books wait on at close and
which also counts unmetered answers the pending token lets a job step
past. #2592 bridges the seam between an abandoned race returning and its
cut arms queueing their receipts only for the pending token, because that
is the only waiter on dev. This holds the settlement token across the
same drain, bounded by abandonGrace, as the land patch that took
TestMemoryLateReceiptReachesFactoryHeaderAfterQuit from 12 failures in 15
to 20 passes in 20 did.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
AbirAbbas added a commit that referenced this pull request Nov 11, 2026
app/v3, the desktop engine line, lands on dev, and dev is the one trunk
again. New work branches from dev and opens its pull request against dev.

This is one squash commit because dev accepts only squash merges. The 906
commits reviewed on app/v3 and on the landing branch stay reachable, and
bisectable, at the tag app-v3-final. One revert of this commit undoes all
of it.

The merge keeps every behaviour from both sides. Where they disagreed:
- one rule, session.UsageCallID, recognises a paid call already in the
  spending ledger, so reopening a chat with no receipts no longer books
  its paid turns or folded task spend a second time;
- a device's directory record is read before it is written (app/v3's
  rule);
- automatic git maintenance is switched off only in the copies codeaf
  owns (dev's rule);
- closing the desktop engine cancels and waits for everything it started,
  each within a stated bound, and a second close waits for the first.

It also carries #2592 from dev: a call stopped before its first streamed
word keeps its charge. On top of that it holds app/v3's factory
settlement token across an abandoned race's drain, so a memory receipt
that lands after quit reaches the ledger and the factory header.

For dev's CLI users, the first model call of each process now reads the
spending ledger once (about 0.6 us per row of history; a quit on a
million-row ledger can wait on it, and nothing is lost). #2593 tracks
taking it off the quit path.

Known issues that land with it: the pool's errand tracker is keyed by
profile, so in the desktop engine the first door to close cancels the
other doors' pool errands (learning rows, not money); and a receipt still
pending only in memory when the process exits is not recovered (#2591's
other half).

Fixes #2579
Fixes #2585

Co-Authored-By: Claude Fable 5.2 <[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.

1 participant