Repository navigation
Description
What happened
If a person stops a reply with Esc and quits codeaf immediately, the cut call's charge is never accounted for. After a relaunch the ledger has neither a priced row for it nor the unbilled marker. The manual promises one or the other. internal/manual/chat/models-and-cost.md, "Was I charged for a reply that got cut off", says:
When the receipt arrives, its own cost and token counts move the conversation's meter and add one late line to the machine's usage ledger.
and, when it can't be obtained:
codeaf writes an
unbilledmarker with no invented price or token count. The marker survives a restart.
This was seen on dev b8c5f2657 and on ace7888ab, in 10 alternating trials on 2026-11-10. Every call with a reported cost was booked (45 of 45). Every cut call that was caught by the quit had no row and no marker: the interrupted turn, and often a title or router helper call cut at the same moment. Its record in logs/calls.jsonl:
status: 200, error: "decode stream: context canceled (turn ended: person stopped)"
Control: the same interrupt with the window and engine left open for 70 s settles on both builds. A reconciled row arrived within 20–40 s, and an unbilled marker by 70 s. So the loss is the process stopping before the receipt arrives.
Replication
Field (real key, a cent or two).
- Use a fresh throwaway
HOMEandCODEAF_HOME, with the key from the environment, in a tiny git repo. - Plain launch.
/model deepseek/deepseek-v4-flash. AskReply with exactly: READY, and wait for idle. - Send a long request (for example, 300 numbered two-sentence tips). Once output tokens are streaming, press Esc, then Ctrl+C 0.12–0.15 s later, then
codeaf engine --stopat once. - Wait 90 s. Relaunch the same chat, wait 60 s, and quit.
- Today:
v3/usage.jsonlhas no row and nounbilledmarker for the cut turn./costcounts only the completed calls.
Control: repeat step 3 with Esc only, and keep codeaf open 70 s. The reconciled row or the unbilled marker appears.
Deterministic (no model). A provider stub that streams a few chunks and then answers the receipt or generation lookup only after a delay. Interrupt the turn, close the session and engine before the lookup answers, then reopen: today no row or marker is written. The acceptance test should be built on this.
Where
- Pending receipts are held only in memory. Search
receiptWorkininternal/provider/client.go, and the retry schedule (receiptRetrySchedule) ininternal/provider/receipt.go. The priced row or theunbilledmarker is written only when that background worker reaches its sink. No durable pending record exists while it waits. - The engine drains the usage writer after host shutdown (search the usage drain in
cmd/codeaf/engine.go). But it can't drain a receipt that hasn't arrived yet. The process exits and the queued generation is gone. - Related, also on dev and identical at v0.8.2:
internal/provider/hedge.go'sabandonhands the arms toaccountForTheAbandoned, which only drains their results forabandonGraceand books nothing.
History
The same in-memory design is in v0.8.2 (receiptWork / receiptRetrySchedule in internal/provider/receipt.go). The changes since then are credential-origin fixes (#1894, #1904) and #1887's accounting work, none of which made pending receipts durable. This predates the current release candidate. It is not a regression.
The fix
- Before the process may exit with a receipt still pending, record the pending call durably: its generation id where known, its model and its session.
- On the next start, either fetch the receipt and write the priced
reconciledrow, or write the promisedunbilledmarker. - A call cancelled before any generation id exists (before its first streamed chunk) gets the
unbilledmarker at once, so it is never silently absent.
Acceptance
- The deterministic stub test above fails today and passes after the fix.
- The field repro leaves, after the relaunch, exactly one
reconciledrow or oneunbilledmarker for every cut call. /cost, Home and the daily limit include any reconciled cost.
Activity
AbirAbbas commentedon Nov 11, 2026
Measured dollar gap. These figures are the provider's own usage figure for the test key, compared with the change in codeaf's ledger. Batches were 5 actions each, on
deepseek/deepseek-v4-flash, at devb8c5f2657(RC) andace7888ab(BASE), 2026-11-11. The provider figure was read 120 s after each batch.AbirAbbas commentedon Nov 11, 2026
Confirmed on the v0.8.2 release binary (
codeaf v0.8.2 built 2026-10-23). The same field repro, 3 times, plus one control:unbilledmarker./costcounted only the completed calls.reconciledrows.So this behaviour predates the current release candidate. Tracked here for its own fix: a pending receipt recorded durably, and retried or turned into the
unbilledmarker on the next start. The before-the-generation-id and abandoned-hedge seams are a separate provider change that references this issue.provider: a call stopped before its first streamed word keeps its cha…
Land app/v3 on dev: one trunk (#2561)