Skip to content

A cut reply's charge is lost if codeaf quits before its receipt arrives (no priced row, no unbilled marker) #2591

Open

Description

@AbirAbbas

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 unbilled marker 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).

  1. Use a fresh throwaway HOME and CODEAF_HOME, with the key from the environment, in a tiny git repo.
  2. Plain launch. /model deepseek/deepseek-v4-flash. Ask Reply with exactly: READY, and wait for idle.
  3. 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 --stop at once.
  4. Wait 90 s. Relaunch the same chat, wait 60 s, and quit.
  5. Today: v3/usage.jsonl has no row and no unbilled marker for the cut turn. /cost counts 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 receiptWork in internal/provider/client.go, and the retry schedule (receiptRetrySchedule) in internal/provider/receipt.go. The priced row or the unbilled marker 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's abandon hands the arms to accountForTheAbandoned, which only drains their results for abandonGrace and 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 reconciled row, or write the promised unbilled marker.
  • A call cancelled before any generation id exists (before its first streamed chunk) gets the unbilled marker 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 reconciled row or one unbilled marker for every cut call.
  • /cost, Home and the daily limit include any reconciled cost.

Activity

  1. AbirAbbas commented on Nov 11, 2026

    @AbirAbbas
    CollaboratorAuthor

    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 dev b8c5f2657 (RC) and ace7888ab (BASE), 2026-11-11. The provider figure was read 120 s after each batch.

    batch build action ledger USD Δ provider USD Δ gap
    1 RC Esc within ~200 ms, codeaf kept running $0.00003976 $0.00003976 $0
    2 BASE same $0.00061152 $0.00061152 $0
    3 RC Esc, then quit and stop the engine within ~200 ms $0.000303899 $0.000430991 $0.000127
    4 BASE same $0.000263853 $0.000338213 $0.000074
    5 RC same, fresh session $0.000258848 $0.000492588 $0.000234
    6 BASE same, fresh session $0.0021360842 $0.003117896 $0.000982
    • When codeaf stays running, every charge is booked. When it quits right after the cancel, part of the charge never reaches the ledger.
    • Both builds behave the same. The gap is small per quit on a cheap model, but it scales with the price of whatever call was cut, including background calls (titles, memory) cut at the same moment.
    • A v0.8.2 run of the same repro follows.
  2. AbirAbbas commented on Nov 11, 2026

    @AbirAbbas
    CollaboratorAuthor

    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:

    • Quick quit (Esc, then Ctrl+C 0.12–0.15 s later, then stop the engine at once; 90 s; relaunch): 3 of 3 left their cut calls (the turn, plus a router or title call cut at the same moment) with neither a ledger row nor an unbilled marker. /cost counted only the completed calls.
    • Control (Esc only, codeaf kept open 70 s): both cut calls settled as reconciled rows.

    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 unbilled marker on the next start. The before-the-generation-id and abandoned-hedge seams are a separate provider change that references this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions