Skip to content

Feat: Re-land the accountant pricing of proxy savings (#768) with the review fixes - #930

Merged
amiddavid merged 80 commits into
mainfrom
integ/batch2
Nov 9, 2026
Merged

amiddavid merged 80 commits into
mainfrom
integ/batch2

Conversation

@boazdavid

@boazdavid boazdavid commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Behavior change: the replay cap reopens after each close (#940)

This changes replay_cap_correction_usd for every session that has a close. The owner agreed on the rule in #940.

  • Before: the cap closed once and never reopened. All later replay credit was dropped.
  • Now: a close ends the replay credit of the removals made before it. A removal first made after a close earns credit until the next close. Its replays count up to the first-removal tokens seen since that close.
  • A real client compaction closes the epoch with no one-time credit.
  • A counterfactual compaction closes it too. The baseline restarts from a summary of 80,000 tokens, or half of C when C is 160,000 tokens or less (round 8: a smaller C would close an epoch on every request). That size is an assumption (median of 34 real compactions: 78,554). The new field avoided_compaction_usd holds the price of the compaction request the baseline pays and we do not. No total includes it.
  • Known limit: the rule can over-count on turns where our trimmed transcript is larger than the baseline summary.
  • Not measured on real sessions. The tests use synthetic rows.

Commits 86b2bb7b, dcf5d649 (the clamp) and dac07902 (the summary cap). JSON fields added: avoided_compaction_usd and avoided_compaction_assumed on the stats, and reopened_epochs, avoided_compaction_usd and summary_assumed on the replay ledger.

What this PR is

This is the whole accountant integration in one PR, against main:

  1. The re-land of feat(accountant): Price proxy savings with the accountant library #768. revert: Price proxy savings with the accountant library (#768) #903 reverted it from main. This PR brings it back. It needs the repo owner's yes before it merges.
  2. The fixes of the review of feat(accountant): Price proxy savings with the accountant library #768 and Fix: Wrong savings in the accountant integration (review of #768) #893 (issues Accountant integration: the default tail breakpoint makes every request fall back (check_sum) and book no saving #906 to Dashboard: the first request of a session books cost $0 (token_accounting=partial), so total cost under-counts the bill #920).
  3. The speed-up of the request path. The accountant's begin and end take 2.8 ms and 87 allocations on a 600 KB body. They took 4.8 ms and 4,905.
  4. The accountant pin 5181d50c (accountant PRs 40 to 48, 50, 51, 57 and 58). The pin brings the markPayback effect, which the host now books, and the client compaction record.

It replaces #923, #924, #925, #926, #927, #929 and #893. Their commits are in this branch. Those PRs are closed in favour of this one.

Decision for the owner

Merging this PR undoes the revert in #903. If the owner says no, none of it lands.

Issue to commit

Issue Commit
#906 c00c28d8 fix(accountant): Report the tail breakpoint mark as an operation (#906)
#907 14a5848a fix(accountant): Leave tool-section operations out of the plausibility bound (#907)
#908 a0a7f4b0 fix(accountant): Report the tool deferral names list as an operation (#908)
#909 79c676e8 fix(accountant): Credit the cachesplit saving on a short conversation (#909)
#910 883f834c fix(apply): Keep the kept-wire preference off standalone messages
#911 b843ee6c fix(accountant): Keep measured filter requests and reads on priced rows (#911)
#912 4f2a4175 fix(accountant): Trust the library's keep-alive rescue and judge the TTL by the billed tier (#912, #919)
#913 f15a7b2e fix(accountant): Pin the request after a manual ping as hot and mark the manual figure an estimate (#913)
#914 97022309 fix(accountant): Book no saved tokens on a request the provider rejected (#914)
#915 ff140deb fix(accountant): Hold a no-history estimate out of the totals (#915)
#916 2da3cb04 fix(dash): Keep since and until exact in the stats cache key (#916)
#917 6138171b fix(accountant): Version, sync and de-duplicate the state file; hash past nested cache marks (#917)
#918 a1b89cf1 fix(accountant): Recover panics in ping pricing and in accBegin (#918)
#919 4f2a4175 fix(accountant): Trust the library's keep-alive rescue and judge the TTL by the billed tier (#912, #919)
#920 ab085e8e test(dash): Pin first-request pricing; the $0 row is an unloaded price map (#920)
#938 6df3dfd4 fix(accountant): Report the cacheinject marks so the accountant prices them (#938)
#939 f5cf361b feat(accountant): Say why an event is unpriced in the acc_fallback line (#939)
#940 86b2bb7b feat(dash): Reopen the replay cap at each close and book the avoided compaction (#940)

Review

Request-path cost

  • BenchmarkBeginEndUnchanged: 2.8 ms and 87 allocations, from 4.8 ms and 4,905.
  • TestDashboardAddsNoRequestLatencyWithContentCapture adds 4.0 to 5.0 ms against a budget of about 10.4 ms on a quiet machine. It added about 6.5 ms before the speed-up.

Known open items (not in this PR)

Assisted-By: Claude (Anthropic AI) [email protected]

accBodies hashed each message's raw JSON with its cache_control. The
client moves its marks: Claude Code puts one on the newest block every
turn. The previous message then had new bytes on each request, the
library found no common prefix after the system block, and every token
it had saved earlier was priced at the write rate although the provider
read it. The booked saving grew each turn: 2.0x, 2.9x and 3.7x the real
value on turns 2 to 4 in the review of #768 (B1).

The hash now leaves out the mark, on the element and on its content
blocks. The size keeps it: the library checks the operations' bytes
against the bytes that went upstream.

Two tests cover the hash. A third drives three turns with a moving mark
through the tracker. Without the fix it prices the replay at the write
tier (54,185 tokens). With it, at the read tier.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
…ant priced

The proxy reports each toolfilter removal to the accountant library, so
on a request it priced the removal is in baseline_cost_usd and in the net
saving. The measured declaration credit added it again: one 1,000-token
removal gave a row of $0.0001 and a total of $0.0002 (review of #768, B2).

Both declaration-filter queries now leave out a request that has a
toolfilter row with priced_by = 'accountant'. A request the library could
not price (priced_by = 'none') and a row from before the switch have
nothing in the baseline, so they keep the credit.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
A manual ping avoided a cache write, and that write is booked on its own
row as ManualSavedUSD. PingSavings also stored the ping's facts, so the
next real request credited the same write again as a library rescue. The
total counted it twice: $0.0200 booked, about $0.0096 true (review of
#768, B3).

A manual ping is still priced, so its cost stays on its row. Only the
facts are not stored.

This keeps the user's estimate as the one credit for a manual ping. The
other fix, to zero ManualSavedUSD and keep the library rescue, would make
that credit a measured figure. It needs to know at ping time whether the
next request will read the entry, and it cannot.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
The reachability gate asked for cache_read > 0 on the ping. A ping that
found the entry expired re-creates it: it writes, it does not read. The
accountant library credits the next request a rescue in that case,
because that request reads the entry the ping wrote. The gate zeroed the
credit and kept the ping's cost: the session read -$0.01146 where the
hand value is -$0.00094, 12x too negative (review of #768, B4).

For a row the library priced (a request_components row with
priced_by = 'accountant'), the test is now cache_read > 0 OR
cache_write > 0. The strict test stays for every other row. The legacy
write path credited a row on keepalive_pings alone, and a ping that only
re-created the entry earned it a credit it did not deserve. The old doc
comment explains why, and it is still true of those rows.

The credited-row guard stays first. Measured on 104,000 rows: 1.64 s
before, 1.92 s after, for three Sessions calls. The comment warns of a
197x slowdown from a wrong order, and this is not that.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
DeltaUSD removed every row past the cap. Since the accountant library
prices breaks and TTL premiums, a stored saving can be negative, and a
negative row past the cap is money that was spent. Removing it raised the
net: rows +1, +1, -2 gave a correction of +1.00 (review of #768, B5).

A negative row now counts, whether the cap is open or not. Only positive
credit past the cap is removed, which is what the cap is for.

The review also asked to check creditTurn and creditOpening in
compactepisode.go for the same sign problem. They do not have it: for a
SavedUSD of -3 both book the cost in FirstRemovalCreditUSD and keep it in
the net. It is labelled "first removal credit", which is a poor name for
a cost, but the number is right. No change there.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
NetUSDFirstRemoval is documented as what the proxy's /stats reports.
/stats reports saved_usd, which nets the costs a component caused: added
tokens and the breaks it left behind. The verdict was first removal minus
the component's own LLM spend, with no costs term, so a component that
loses money read as a gain: saved_usd -$0.0302 beside +$0.0113 (review of
#768, B6).

The same function already added the costs to SavedUSDDecomposed, so the
two totals disagreed. Both the general path and the replay-cap override
now include them, and the Components tooltip says so.

The summarize override is left as it is. It sets the split from its own
ledger and has no costs term; I could not show that SavedUSDCosts is the
right number there.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
The OpenAI Responses API keeps its conversation in input. accBodies hashes
messages and apply reports no operations on that wire, so the accountant
priced nothing there: the conservation check failed and every component
read priced_by = none with acted_without_figure. A reviewer found that all
Responses savings dropped to $0 and the PR text did not say so (review of
#768, B7).

This is a guard, not support. A request on the wire opens no record, the
event is marked, and PriceMode books no saving with the reason
unsupported_wire. Chat Completions and Messages are unchanged.

Support itself means hashing input, instructions and tools, reporting
operations from the Responses write path, and an OpenAI profile in the
library (accountant#10 and #17). That is a feature, not a fix, and it
cannot be checked here against a real stream.

docs/dashboard.md now says that every saving on this wire is $0, which the
claim "every saving comes from the accountant" had left out.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
…not $0

A request the accountant could not price books no saving. Its components
carry priced_by = 'none' and a stored saved_usd of 0, and they were
counted nowhere: saved_usd_unpriced_rows stayed 0, so the Components tab
printed a confident $0 for a component that had removed 5,000 tokens
(review of #768, B8).

A row of priced_by 'none' that removed content now counts as unpriced, so
the column reads n/a. A row that removed nothing does not, and a row the
library priced at $0 is still a measured $0.

The unpriced text blamed the operator's price list every time. For a
fallback that advice is wrong: the model may be priced, and setting
MODEL_PRICES changes nothing. The three messages now give both causes and
point at acc_fallback in the proxy log.

Not done: the review also asked for a fallback_requests count on
Overview. It needs another query on both the full and the lean path,
which TestLeanStatsMatchFullForTheSameWindow and the 20,000-request budget
test pin, and it adds visibility, not correctness. The per-component
column and the acc_fallback log line already say it.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
Takes accountant 38a32264 (PR 23's branch) for four fixes the review of
#768 needed in the library: the rescue no longer depends on what the
request wrote (#9), the ping extent ignores the ping's fresh input
(#14), a split is credited only when the system section changed (#20),
and B* follows the usage when the provider read the whole previous
extent (#21).

THIS COMMIT IS NOT ON accountant main. PR 22 (the cases) is still open,
and PR 23 merged into 22's branch, not into main. The pseudo-version
resolves because Go reads the commit from the repository, but it names a
commit that a squash or a force-push would drop. Re-pin to the merge
commit on main before this branch merges.

All dash, proxy, apply, keepalive and components tests pass on it,
including dash/accountant_play_test.go, which plays the library's shared
cases through this repo's pricing.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
The review of #768 (B9) found $350.93 of cachesplit_saved_usd on $29.96 of
cost in a replay of four Claude Code sessions, where the system snapshot
never changed and the true saving was $0. Two faults made it: the body hash
included the moving cache mark (B1), and the library credited a split with
no check that the system section changed (accountant#20).

Both are fixed on this branch. The test drives the replay's shape through
the tracker: a split every turn, an unchanged system block, a mark that
moves. It books a split effect in the state #768 merged in (103 tokens on
turn 3) and none on this branch.

I checked each fault alone: with only the old hash, or only the old
library, the test passes. Either fix is enough to keep the credit out of
this scenario, so the test guards the combination. The comment says so.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
The library asks the host to wrap Compute, and this repo's rule is to fail
open. The call ran in finish, outside the function the pre-forward
recover covers, so a panic would have lost the request's cg.request log
line and its dashboard row (review of #768, major 1). The reviewer could
not make the library panic, so this is a missing guard, not a fix for a
seen failure.

computeSafe wraps it. On a panic the request is recorded with no result
and PriceMode falls back with no_result. The panic is logged at error
level. Tests keep calling compute, so a panic still fails them.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
… 20 and 21

Replaces the pin on the unmerged fix branch (38a3226) with main at d3b2135.
This is the pin the PR needed before it could merge.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
Accountant main now refuses usage outside 0.05 to 2.0 tokens per byte
(ratio_range, accountant#28) and prices a request with no client cache
mark at the read tier (accountant#24). Fixtures with a few hundred bytes
and thousands of tokens failed, and the absurd-usage cases now fall back
as "unpriced" with a failed ratio_range check, not "implausible_size".

- Scale the token counts of the savings, envelope and injection fixtures to their body sizes.
- Count the read tier when a test asks whether a change was priced.
- Re-derive the realistic-usage figure at the read rate (2e-7).
- The moving-cache-mark test now asserts that no break is reported.
  Without the B1 fix the library no longer prices the replay at the
  write rate, it reports a false break. Checked: fails without the fix.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
Takes accountant#30: Size gains optional Tokens and MediaBytes, and check 9
judges the media-adjusted ratio. The integration sets neither, so no
behavior changes. Proxy and dash tests pass.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
The library keeps each thread's active changes in memory, so every restart
dropped the replay saving of every live session until the next full
request (review of #768, major 2). Accountant#13 added Snapshot and Restore.

The handler restores from <dashboard-db>.accountant-state.json at New,
saves it every minute and at Close, and fails open: a missing, stale
(over 1h), corrupt or unwritable file starts the proxy empty. The file is
written through a temp file and a rename, mode 0600.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
OpenReadOnly shims the columns an older database lacks with a TEMP VIEW.
A view has no rowid, so Request(id) failed on such a file with "no such
column: rowid", and cg-cache-campaign opens databases this way (review of
#768, minor).

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
ReplayMultiple was (first + replay) / first and ignored the costs the
accountant nets into saved_usd, so a component that loses money printed
"1.5x". The replay cap already divides the decomposed total. Use the same
numerator here and clamp it at 0 (review of #768, minor).

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
The price lookup matched a map key against the whole id, so
claude-sonnet-5-20260101 and us.anthropic.claude-sonnet-5-v1:0 found no
price and no saving was booked. After the exact tiers, strip the vendor
prefix, the version suffix and the release date and look the bare name up
exactly. A different model (claude-sonnet-5-5) is not reached (review of
#768, minor).

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
The keeper's report carries TS, the time the ping was recorded after its
response. The library measures the entry gap from the start of a ping, as
the provider measures the TTL. Map At to TS minus the duration (review of
#768, minor).

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
An estimated request is one the accountant priced without knowing whether
a component broke the cache: a thread's first request, or the first after
a restart that lost the state. Its net can be too high, and only the
request detail said so (review of #768, minor).

Add estimated_requests to the full and the lean Overview, one SUM over the
stored price_estimated column in the aggregate that already runs, and a
line on the Preset card when it is above zero. The three stats goldens
gain the key.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
begin hashes the client body and end hashed the forwarded body again, even
when no component and no injection had changed a byte. On a 600 KB body
that is about 4 ms of the 8 ms the accountant costs on the request path
(review of #768, minor). Keep the client hash from begin to end and reuse
it when the bytes are equal. A request that was rewritten hashes both, as
before. The entry is dropped at end.

BenchmarkBeginEndUnchanged: 8.1 ms and 10.1 MB per request before, 4.0 ms
and 5.1 MB after. The rest is the library's NewBody walking each position
for media, which needs a fix in accountant.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
The price lookup reads the base rates only, so a model that charges more
above 100k or 200k prompt tokens is under-priced on those requests. Of the
bare Claude ids in LiteLLM's map, two carry such a tier. Applying it means
choosing the tier from each request's prompt size at 29 pricing call sites,
which moves every cost figure; that is left as a decision (review of #768,
major 3).

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
no_result stands for four unrelated causes: no record was opened, the
response carried no usage, an expand loop ran several upstream rounds, or
Compute panicked. The acc_fallback line gives the reason only. Add a "why"
attribute for no_result, so the first request of a session that reads a
warm cache and books nothing (review of #768, major 5) names its cause the
next time it happens. I could not reproduce that case without the trace.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
)

Both reviewers found the same three blocking problems.

- accBodies kept the library's cumulative byte table, built from the
  mark-free input, while Pos.Bytes held the wire size. A splice that removed
  a message with a cache mark failed the sum check (sawOriginal compares the
  operation's wire SizeBefore with that table), so the request booked no
  saving. The Body is now built again from its exported fields, which drops
  the table; every byte count the library reads is the wire size, as the
  operations' sizes are. The test passes with wire bytes hashed, fails with
  the old Body, and passes now.
- The client hash begin keeps for end leaked on the recovered panic before
  the forward. accDrop and finish now drop it.
- docs/data-dependence.md said the hashes are held in memory only; they are
  also saved to the state file. The row says so.

Also: the replay cap path clamps the replay multiple at 0 as the read path
does; undecorate strips a version suffix only from a vendor-prefixed id; the
long-context tier note names the models; two proxies must not share a
dashboard database.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
- undecorate also strips the au., jp. and us-gov. region prefixes of a
  Bedrock id, which LiteLLM's own keys carry.
- The long-context note says where Sonnet 4 and 4.5 carry a tier: on the
  Bedrock, Vertex and Azure listings, not on the Anthropic API ids.
- The comment of the splice test names the check that fires (conservation).

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
…768)

This reverts commit 38c00e1 (#903).
It brings back #768 on top of current main.
Conflicts in keepalive/keeper.go and keepalive/manual.go: main added touchAlias
(#904) and #768 adds gapStart to the same struct literals. Both fields are kept.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>

# Conflicts:
#	dash/overview.go
#	dash/overview_lean.go
Pin accountant at 5fa1986. It holds PR 46 (media pricing, faster extent
search) and PR 47 (TTL per breakpoint, a billed read beats the clock).

dash, proxy, apply, keepalive and components tests pass with it.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
…ws (#911)

The B2 double-count fix dropped a whole request from DeclFilterSavings when the
accountant priced its toolfilter removal. That also dropped Requests, Reads and
Since, so ToolFilterDocFor lost its Realized block and the item list in
adaptToolsSaved could sum to more than the total.

Now the row stays in the scan. An EXISTS flag column marks it and
accumulateDeclFilterRow skips only the USD. carriedCost does the same for the
items, so items and total agree.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
The default tail breakpoint (#795) adds a cache_control mark to the last
message after the pipeline ran. A string content also becomes one text
block. The message gets longer on the wire, and no operation reported
the extra bytes. The sum check failed on every request with a trailing
system message (section=messages ops=0 bodies=-63 bytes), so every
request fell back with check_sum and booked no saving.

apply now appends one rewrite operation for the last message, in wire
bytes, when markTail places a mark. Its ChangeID comes from the message
before the mark, so it is the same on every request. The operator
tailbreakpoint is an envelope operator in dash, so it gets its own row.

No accountant check or plausibility rule is weakened. The check now has
the operation it was missing.

Harness (FAKE_TOKDIV=8, tailx, 7 requests): net_saved_usd 0 -> 0.0817.
Truth (control cost - tail on cost): 0.0736. Fallbacks 7 -> 0.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
Resolve dash/keepalive.go. #959 changed the keep-alive window's tier from the
row's cache_ttl to the billed tier (billedHour). This PR turned the inline
EXISTS into the probe function with the accountant cases (#912, #919). Keep the
probe, and give it billedHour as its tier.

The requests INSERT has 95 columns, 95 placeholders and 95 bound arguments.
requestCols and the Scan of scanRequest have 88 each.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
After a counterfactual close the baseline restarted from a fixed 80,000-token
summary. With a compaction point C at or below that, every later row was already
at C, so every row closed again and booked one more avoided compaction. Claude
Code reaches it with CLAUDE_CODE_AUTO_COMPACT_WINDOW=100k (C = 67,000). On 10 such
rows the ledger booked 10 closes and $12.20, where one close is right.

The summary is now min(80,000, C/2), in the restart and in the avoided price.
A C above 160,000 keeps the 80,000 summary, so no larger C changes.

Found by both reviewers in round 8.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
@boazdavid

Copy link
Copy Markdown
Collaborator Author

Round 8 finding, fixed in dac07902. Both reviewers found it, and both were right.

Finding 1 (a small C closes an epoch on every row): fixed. I reproduced it first. 10 rows with a configured C of 67,000 gave 10 counterfactual closes and $12.20 of avoided_compaction_usd. One close is right.

  • The summary is now min(80,000, C/2), through summaryFor. It is used in the restart after a counterfactual close, and stored per close for the avoided price.
  • A C above 160,000 keeps the 80,000 summary, so no larger C changes. The older dash: the replay cap never reopens after a client compaction, so credit for removals made after it is dropped too #940 tests use C = 1,000,000 and pass unchanged.
  • Two new tests. TestReplayCapASmallCompactionPointClosesOncePerCycle: 10 rows, C = 67,000, one close, and the avoided price of one compaction with a 33,500-token summary. TestReplayCapASmallCompactionPointClosesAgainAfterItsSummaryGrowsToC: the second close comes when the summary plus the growth reaches C. Both fail on the old code (10 closes, then 4).
  • Mutations: a fixed S in the restart, a fixed S in the price, and no cap at all each fail a test.
  • docs/dashboard.md states the rule.

The reviewers noted that a changed figure for a small C needs the owner's yes. The owner asked for this fix in the author's session: "merge from main, fix the issue".

Also on this head: a merge of main (38abda79). One conflict, in dash/keepalive.go: #959 changed the keep-alive window's tier to the billed tier (billedHour), and this PR had turned that EXISTS into the probe function. I kept the probe and gave it billedHour. The requests INSERT counts 95 columns, 95 placeholders and 95 arguments. requestCols and the Scan of scanRequest count 88 each.

Assisted-By: Claude (Anthropic AI) [email protected]

@boazdavid boazdavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (claude, user of @boazdavid) Claude Code 2.1.241, claude-opus-5-5 offload-review skill, @boazdavid's session resumed 1 of 2 9 of 9 dac07902e18b approve

Verdict: approve

Findings

None.

Earlier findings

  • My round-8 finding 1 (a C at or below 80,000 closes an epoch on every row): fixed in dac07902. Reviewer 2's round-8 finding 1 is the same defect, and the same commit fixes it.
    • summaryFor(limit) = min(80,000, limit/2). The restart uses it (dash/replaycap.go:366), and addAvoided stores it for each close (summaryT, line 438). priceAvoided prices summaryT, not n × 80,000 (line 612). The restart and the price use the same S.
    • After a close, cf starts at limit/2 or less, so the next row cannot close at once. The growth since closedAt must add at least limit/2 before the next close.
    • When C is above 160,000, summaryFor gives 80,000. The two older tests that price the avoided figure (C = 1,000,000, lines 867 and 907) still expect assumedSummaryTokens, and they stay correct.
    • Behavior change: a C of 160,000 or less gets a smaller S. The author cites the owner's request ("merge from main, fix the issue"), and both reviewers asked for this rule. I accept it. Note: Bob's client default from #964 (C = 100,000 on a 200k model) now gets S = 50,000. This is the intended rule.
  • Rounds 1 to 7: all fixed or accepted. Nothing reopened.
  • Reviewer 2's round-8 note (capConversationPoint uses the newest configured row): on purpose, as its comment says. Note only.

Non-blocking

  • None.

What I checked

  • Last review: round 8, d9b1552dbf466391fbe62ce859065595b9d2a18f, 2026-11-09T09:54:15Z
  • New commits read: 2 on the PR branch: 38abda79 (Merge origin/main into integ/batch2) and dac07902 (fix(dash): Cap the counterfactual summary at half of C (#940)). The merge brings in main's ed692a71 (#964), 8be4ea05 (#965), 01fa0e93 (#958) and 354fcb32 (#959). They went through their own PRs and are in the new base 354fcb32.
  • Read in full: 15 reviews, 5 comments, 0 inline comments (from the three files)
  • New items since my last review:
    • Reviewer 2's round-8 review (2026-11-09T09:55:07Z, blocking). It has the same finding as mine, and it is now fixed. Its other checks agree with mine.
    • The author's comment of 2026-11-09T10:12:54Z. It describes the fix, the tests, the mutations and the merge. I checked each claim (see below).
    • Commits 38abda79 and dac07902. Reviewed below.

dac07902:

  • I traced TestReplayCapASmallCompactionPointClosesOncePerCycle by hand. m1 has f = 1, and C = 67,000.
    • Row 1 bills 71,000, so cf = 71,000 and the row closes. closedAt is 61,000.
    • Rows 2 to 10 give cf = 33,500 + (1,000 to 9,000), which is 42,500 at most. No close.
    • tokens_before always grows, so no real compaction fires.
    • Result: 1 close, 1 reopened epoch, and an avoided price of 67,000 × read + 33,500 × output. This matches the test. With the old code, all 10 rows close.
  • I traced TestReplayCapASmallCompactionPointClosesAgainAfterItsSummaryGrowsToC. Row 1 closes at a bill of 70,000. Row 2 gives 66,500 and stays open. Row 3 gives 67,000 and closes. Row 4 gives 34,000. The result is 2 closes, as the test expects.
  • docs/dashboard.md: the new sentence matches summaryFor. At exactly 160,000, half is 80,000, so both rules give the same S.
  • Fail open: pure arithmetic on stored metadata. No new error path.

38abda79 (merge):

  • git show --remerge-diff 38abda79: one conflict, in dash/keepalive.go. The resolution keeps this PR's probe function and gives it main's billedHour tier (#959). billedHour is defined before probe, inside the same function. Both probes (cache_read > 0 and cache_write > 0) now use the billed tier. The guard col > 0 stays first.
  • git diff-tree --cc 38abda79 lists the files that both parents changed. I read the diff d9b1552d..38abda79 of dash/replaycap.go. #964 adds ClientDefault: capConversationPoint uses IsClientSource, and a client-default conversation writes CeilingConfigured. Recorded handles both sources with abs > 0. This joins the epoch code correctly, and limit feeds summaryFor.
  • dash/keepalive_ttl_test.go: the only difference from main is the removal of TestKeepAliveCreditFloorFollowsTheRequestsTier. This PR removed that test before (it was in 2b6d739e..d9b1552d too), and it tests the legacy write path. Main's new #959 tests stay.
  • The positional SQL lists: the author reports 95/95/95 for the INSERT and 88/88 for requestCols and scanRequest. The merge did not change dash/store.go or dash/query.go (they are not in the --cc list). My round-7 script counts stand.
  • Data dependence: no change. The fix and the merge use metadata only (token counts, a C value and its source).
  • I ran no builds or tests. Live tests are not approved. The verdict rests on reading the code and on hand traces of the two new tests.

@boazdavid boazdavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (claude, user of @boazdavid) Claude Code 2.1.241, claude-opus-5-5 offload-review skill, @boazdavid's session resumed 2 of 2 9 of 9 dac07902e18b approve

Verdict: approve. The round-8 finding is fixed. The merge of main is correct. I found no new defect.

Findings

None.

Status of earlier findings

  1. Round 8, finding 1 (mine and reviewer 1's): a small C closes an epoch on every row. Fixed in dac07902.
    • summaryFor(limit) = min(80,000, limit/2) (dash/replaycap.go:167).
    • The restart after a counterfactual close uses it (dash/replaycap.go:366).
    • addAvoided stores the summary of each close in summaryT (dash/replaycap.go:438).
    • priceAvoided uses a.summaryT*pr.Output and no longer uses the fixed n*80,000 (dash/replaycap.go:612).
    • So the restart is always below C: limit/2 < limit for every limit > 0. A new close needs f*(TokensBefore-closedAt) >= limit/2, which is real growth.
    • I traced TestReplayCapASmallCompactionPointClosesOncePerCycle by hand. Model m1 has f = 1.0. Row 1: billed 71,000 ≥ 67,000, so close 1 at closedAt = 61,000. Rows 2 to 10: cf = 33,500 + (before - 61,000), at most 42,500. No more closes. The test asserts 1/1 and one priced compaction: 67,000 x 0.3e-6 + 33,500 x 15e-6. That is correct.
    • I traced TestReplayCapASmallCompactionPointClosesAgainAfterItsSummaryGrowsToC: 66,500 (open), 67,000 (close 2), 34,000 (open). 2 closes. That is correct.
    • For C > 160,000, summaryFor gives 80,000. The old #940 tests use C = 1,000,000, so their figures do not change.

Non-blocking

  1. The PR body is stale on the summary size. Line "The baseline restarts from a summary of 80,000 tokens" in Behavior change does not say "or half of C, when C ≤ 160,000". docs/dashboard.md:239 says it. Add the same sentence to the PR body, because the owner reads that section to approve the behavior change.
  2. Two comments still say the summary is fixed.
    • dash/replaycap.go:63: "S is an estimate (assumedSummaryTokens)". S is now summaryFor.
    • dash/replaycap.go:148: SummaryAssumed "rest on the fixed summary size". It is now 80,000 or C/2. Both are still assumptions, so SummaryAssumed = true stays correct. Only the words are old.
  3. The owner's approval of the C/2 rule. The author quotes the owner: "merge from main, fix the issue". The rule changes a figure only for C ≤ 160,000, inside the #940 rule that this PR adds, so main has no old behavior to compare. I do not count it as a finding. The owner may want to confirm the C/2 guess when they approve #940.

What I checked

  • Last review: round 8, d9b1552dbf466391fbe62ce859065595b9d2a18f, 2026-11-09T09:55:07Z.
  • New commits read: 2.
    • dac07902 fix(dash): Cap the counterfactual summary at half of C (#940).
    • 38abda79 Merge origin/main into integ/batch2. It brings 354fcb32 (#959), 01fa0e93 (#958), ed692a71 (#964) and 8be4ea05 (#965).
  • Read in full: 16 reviews, 5 comments, 0 inline comments.
  • New items since my last review: the author's reply of 2026-11-09T10:12:54Z on the round-8 finding and the merge. Reviewer 1's round-8 review (2026-11-09T09:54:15Z) has the same finding as mine.
  • dac07902: I read the whole diff, walkCapConversation (dash/replaycap.go:315-425), addAvoided, priceAvoided, epochRow, capTestRow and tokens.BilledDeltaFactor. I checked every use of assumedSummaryTokens. The only remaining use is inside summaryFor.
  • 38abda79: I read the --remerge-diff. The one conflict is in dash/keepalive.go. The resolution keeps this PR's probe function and gives it #959's billedHour tier (dash/keepalive.go:224). billedHour is defined before probe. Both probes, kp.cache_read > 0 and kp.cache_write > 0, now use the billed tier. That is the intent of #959.
  • The merge also brings ClientDefault (#964). capConversationPoint and the CeilingConfigured branch now use IsClientSource. The fix also covers Bob's default C (0.5 of the window, so C = 100,000 for a 200,000 window, and S = 50,000).
  • The merge does not change dash/store.go or dash/query.go. My round-8 counts stay valid: 95 INSERT columns, placeholders and args, and 88 requestCols and Scan targets.
  • Not run: Go builds and tests. This round did not approve live tests, so I read the code only.
  • Not counted, as the author asked: DCO red (5e47f96e) and TestRateFillTableAndPrice (#937).

@amiddavid
amiddavid merged commit 587a865 into main Nov 9, 2026
11 of 13 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 9, 2026
amiddavid added a commit that referenced this pull request Nov 10, 2026
…1014)

TestDashboardAddsNoRequestLatencyWithContentCapture failed on loaded
CI runners (on #930, #971, #996, #1010) because one pass of 40 paired
samples was too noisy for the 45 ms budget. It now measures the guard
in up to 3 rounds, and passes as soon as one round is in budget; a
real slowdown fails all 3. Closes #1013.

Behavior change: none. Test only (proxy/dash_test.go).
Data dependence: none.
Known limit (older than this PR): under heavy load, a 50 ms delay in
capture.finish does not fail the test; filed as its own issue.

Reviews: 2 offload reviews (GLM), round 2, both approve at 2fbb429.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
amiddavid pushed a commit that referenced this pull request Nov 11, 2026
…ge (#1001)

The first request on a warm provider cache that the proxy never saw
(a restart that lost its state, or a session begun before the proxy)
cannot be priced exactly: the reply does not give the size of the
unseen warm prefix. On main (#930) the row is held out of every total
and books 0.

- New column requests.estimated_saved_low_usd: a floor that prices
  each removed token at the cache-read rate. The true saving lies
  between this floor and estimated_saved_usd.
- The Overview note and the request detail row show the range ("$x to
  $y") in place of only the ceiling. A floor that is a cost shows too
  (estRange), with a Node test that CI runs through a Go wrapper.
- Both bounds stay out of every total. The row stays flagged estimated
  and held. No booked figure changes.

Behavior change: the dashboard shows a range for held first-request
rows; totals do not change. Owner's decision, 2026-11-11, in the
integrator session: "1001 A" (book nothing, show the range).

Data dependence: metadata only. The column is computed from token
counts and the cache-read price; no text. Row:
docs/data-dependence/features/dashboard-database.md.

Refs #915.

Reviews: 4 offload review rounds (GLM). Round 4: both approve at
4f652fd. After it: a merge of main (ff756a2) whose only hand
resolution moved the row change into its row file.

Signed-off-by: DAVID BOAZ <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
Assisted-By: Claude Opus 5.5 <[email protected]>
amiddavid pushed a commit that referenced this pull request Nov 12, 2026
…other operator took over (#1134)

Pin the accountant library to the v0.1.1 tag (was the pseudo-version
of 181c523e; v0.1.1 is 6 commits newer). It ends an in-place change
that another operator took over, and keeps one that a later operator
continued (accountant#70).

Behavior change: since v0.7.0 (#930) the accountant's sum check gates
booking, and accountant#70 made a session fail it on every later
request once a stale operation entered its state, also across a
restart. Two shapes: (A) extract or extract_llm rewrites a tool output
in place and extract_llm_sweep later cuts the same output (xhigh,
ultrahigh); (B) on subagent threads, a tailbreakpoint mark beside a
tooldefer rewrite of the same message (ultrahigh). Old: those requests
were rejected with check_sum and booked no saving (measured 2026-11-10
to 11-12: 8% to 17% of real requests on a single-user laptop proxy,
4.5% to 18% on a hosted deployment). New: the stale change ends and
only the taking-over change is priced, so they book their saving
again; an affected session recovers on its next request. Past rows are
not repriced. Turn it off: revert this one dependency line.
Owner's yes, 2026-11-12, in the integrator session: "1134 behavior
change is known and is critical".

Data dependence: none.

Reviews: 2 offload review rounds (GLM). Round 2 (same head, after the
PR text named the behavior change): both approve at f2790da.

Signed-off-by: DAVID BOAZ <[email protected]>
Assisted-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants