Repository navigation
Split Claude Code's running total by the growth each session made - #22
Merged
Merged
Conversation
…e itself After `ccli copy 1087b979 --from talcum --to default --apply`, $258 of unlogged calls moved from default to talcum: default fell from $466 to $208 and talcum rose to $278, though none of that work had happened in talcum. Claude Code's cost-state line is not one process's total. It is one running total per session, named by startTime, that Claude Code restores and carries on when the session is resumed, and that carries on in another account after a copy, in a continued session and in both branches of a fork. ccli kept only each file's newest line of a total and gave the whole total to one owner file: a file holding it as its own, the earliest created. Talcum's newest line (default's total plus talcum's growth) was copied back into default, where it replaced default's own line as an imported one, and the sync's rename made default's file the newer one, so talcum became the owner of everything. Every cost-state line is now kept, in a new snapshots table, with its offset and import flag. A session's share of a total is the growth between consecutive snapshots in its own transcript, counted at each snapshot that is its own; a copied-in snapshot only moves the base. A snapshot that several files hold as their own (a transcript copied by hand, the history a continuation or a native fork repeats) counts once, in the file created first. Each share is compared with the session's own calls in the same time windows. Opening an older index re-reads the files that have cost-state lines (177 transcripts, 3.1 GB, 50 s on this machine) once and empties the old processes table; ReconcileVersion 5 recomputes every adjustment. On a copy of the real index, 1087b979 goes back to $466 in default and $24.30 in talcum. Totals across the index rise by $374: the old rule dropped the growth of all but one branch of a forked or continued conversation, and subtracted one session's calls from another session's total. A fast-forward copy now appends the missing bytes to the target in place instead of rewriting the whole transcript through a temporary file, so the target keeps its identity and creation time and a 200 MB sync writes only what is new.
- A continued or forked session starts its transcript with the running total as it stood plus its own work, with no earlier line to measure from. Counting that first snapshot from zero counted the parent's part again (three of the user's sessions gained $189 that way). In a file created after the total began elsewhere, such a first snapshot now only sets the base. The total began in the earliest file holding a snapshot of it as its own. - A snapshot that only copies hold, because the original is not indexed, counts in the earliest copy instead of nowhere. - The migration dropped the processes rows of transcripts already gone from disk, which cannot be read again, and with them their sessions' share. Those rows now move over as one snapshot each. - The in-place append ran its check on the target before staging up to the whole tail, so a Claude Code process resuming the target meanwhile could interleave with it, and a failed append was cut back blindly. The tail is now staged next to the target first, bounded to the bytes the ledger recorded, the target is checked just before the short append, a failed append is cut back only if nothing else wrote, and if bytes may be left the ledger record stays. - A total that dips no longer lowers the base, the adjustment takes the latest counted snapshot's time, windows are merged before the query, and finding the sessions that share a total no longer joins the snapshots table with itself row by row. Tested: go test ./..., -race on store, index and transfer; on a fresh copy of the real index, 1087b979 is $466 in default and $27.82 in talcum, and the index-wide adjustment total moves by $154, all of it in sessions whose old figure subtracted another session's calls.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
After
ccli copy 1087b979 --from talcum --to default --apply, $258 of unlogged calls moved from default to talcum (default $466 → $208, talcum → $278), though that work never happened in talcum.Claude Code's
cost-stateline is one running total per session (samestartTime), restored and carried on across resumes, into another account after a copy, into continued sessions and into both branches of a fork. ccli kept only each file's newest line and gave the whole total to one owner file; the sync put talcum's newest line into default as an imported one and reset default's creation time, so talcum became the owner.snapshotstable with its offset and import flag. A session's share is the growth between consecutive snapshots in its own transcript, counted at its own snapshots; a copied-in snapshot moves the base. A snapshot several files hold as their own counts once, in the file created first; one only copies hold counts in the earliest copy. A continued or forked session's first snapshot, with nothing before it in its file, only sets the base. Each share is compared with the session's own calls in the same time windows.ReconcileVersion5 recomputes every adjustment.On a copy of the real index: 1087b979 is back to $466 in default ($27.82 in talcum, its own work). Unlogged cost index-wide rises by $154, from sessions whose old figure subtracted another session's calls from their total.
Test plan
go vet ./...,gofmt -l .,go test ./...,-raceon store, index, transfer