Repository navigation
fix(proxy): label a 1h thread's miss by its billed TTL, not 5m - #943
Conversation
The cache attribution always used the 5-minute lifetime. A miss on a 1h thread after 5-60 minutes got ttl_expiry although the entry was alive. The lifetime now follows the billed tier (cache_write_1h > 0) of the thread's previous same-model request. A read-only request keeps that tier. An unknown tier keeps 5 minutes. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 1 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.319 | offload-review skill, @amiddavid's session | fresh | 1 of 2 | 1 of 2 | b99d17bf3ccf |
approve |
Verdict: approve
Findings
None. I could not prove a defect in the changed logic.
Non-blocking
- dash/prefixcause.go:48 — a mixed write (some 5m, some 1h breakpoints) reads as fully 1h.
OneHour = e.CacheWrite1h > 0sets the tier when any part of the write was billed at 1h. For a full miss this is right: every breakpoint must have missed, so a surviving 1h part would have produced a read. For a partial-read miss (partialReadinAttributeCache) where only the 5m part expired, the label now readsunknown, notttl_expiry. The error direction is under-credit, which the pricing comments name as the safe one. No change needed. - The tier does not survive a restart.
lastFactsis in-memory only.SeedSessionsreloads refresh times but not the tier, so the first post-restart miss of a 1h thread after 5–60 minutes is again labeledttl_expiry. This is the exact bug the PR fixes, narrowed to one turn per thread per restart. The requests table holdscache_write_1h, so a later PR could seed it. - dash/keepalive.go:206 still uses the asked tier. The keep-alive coverage SQL passes
cache_ttl = '1h'(what the request asked for), not the billed tier this PR moves to. A 1h-asked, 5m-billed thread gets a too-long coverage window there. Pre-existing and out of scope, but it is the same class of issue. - Name the figure change in the PR body. The label change moves the summarizer's unique rate on a 1h thread's miss from the write rate to the read rate (
uniqueRate, dash/event.go:927). Savings figures on those turns go down. The commit message names the label; confirm the PR body names the dollar change too. - The
fromThreadpath of the tier inheritance is not tested. A read-only first request of a forked thread inherits the parent's tier (samecan come from the parent). Only the same-thread path has a test.
What I checked
- The full diff of
dash/prefixcause.go,dash/prefixcause_test.go,proxy/dash_test.go,proxy/dashcapture.go, and the commit message. - The tier plumbing end to end:
FactsOfsetsOneHour/NoWritefrom usage;ObservePrefixinherits the tier on a read-only turn and returnsprev.OneHour;cacheAttributionTTLMsmaps it throughmodelinfo.CacheLifetime. I traced every return path ofObservePrefix, including the nil-receiver,fromThread, failed-request (!commit) and model-change paths. - That
CacheWrite1his a subset ofCacheWrite(internal/providerusage/providerusage.go:15,proxy/usage.go:31,dashcapture.go:545). SoNoWrite = CacheWrite == 0really means "wrote nothing at either tier", and a 1h-only write is not misclassified.PromptTokensalso already includes the 1h part. - That
AttributeCacheuses the TTL only for thettl_expirylabel, after the cold-start case and before the cause cases ("TTL wins ties"). A longer TTL can only removettl_expirylabels, never add one. The only dollar effect isuniqueRate, in the under-credit direction. - That
cacheAttributionTTLMskeeps returning 0 for OpenAI and Gemini: thekind != Exactearly return runs before the newoneHourbranch, so the GPT-5.6+ 30-minute minimum cannot be turned into an "exact" 1h. - That all callers of the changed signatures were updated:
ObservePrefix(onlyproxy/dashcapture.goand tests),cacheAttributionTTLMs(onlydashcapture.goand tests). Test imports (timeinproxy/dash_test.go,modelinfoindash/prefixcause_test.go) are present. - The new tests by hand: all six
TestTTLExpiryUsesTheBilledTiercases,TestTTLTierSurvivesAReadOnlyTurn(fails without the inheritance fix), andTestSummarizerOnA1hThreadMissIsPricedAtTheRead. Each expected label matches my trace ofAttributeCache. - Locking (
r.muheld for the wholeObservePrefixbody) and thelastFactspruning path (pre-existing). - Data dependence: the change reads usage token counts only, already covered by the metadata rule. No new stored data.
- I did not read the PR body or any PR comments, per the round-1 rule. I did not run builds or tests (not approved; Go is not on this machine).
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 2 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.319 | offload-review skill, @amiddavid's session | fresh | 2 of 2 | 1 of 2 | b99d17bf3ccf |
blocking |
Verdict: blocking
Findings
- dash/prefixcause.go:48 —
OneHour: e.CacheWrite1h > 0calls a mixed-tier write a one-hour entry, but only part of it lives one hour- What fails: a request can bill a 1h head and a 5m tail in one write (
cache_write_1h > 0andcache_write_1h < cache_write). This is a designed shape in this repo:apply/headttl.gobuilds exactly it ("a one-hour anchor on the request's HEAD, the five-minute default on its tail"), and a client can send the same mix. The flag then says the whole entry lives 1h. On the next request, 5 to 60 minutes later, the 5m tail is gone but the 1h head is alive. With a tail-dominant prompt the row is a partial-read miss, the cause is "" (nothing differed), and the TTL test now compares 20 min against 1h. The row readsunknown. Before this PR it readttl_expiry, which was correct: the tail died of TTL. - Proof: trace — turn N rewrites the system block (head 10k billed at 1h, tail 40k billed at 5m;
CacheWrite=50k,CacheWrite1h=10k, soOneHour=true). Turn N+1 after 20 min, prefix unchanged:CacheRead=10k(live head),CacheWrite=40k(tail re-created).AttributeCache(dash/event.go:1206):partialReadholds (10k*2 < 50k, 40k > 10k), TTL = 1h so 20 min < TTL, cause = "" →CacheUnknown. The repo already answers this exact question the other way:keepalive/keeper.go:257-277(ResolveTier) rejects a mixed write withu.CacheWrite1h >= u.CacheWrite, and its comment names the reason — "A mixed ask (the proxy's own 1h head over a 5m tail) leaves a tail that expires in five minutes". The label also moves a dollar figure:uniqueRate(dash/event.go:927) prices a summarizer's first removal at the write rate only onttl_expiry/cold_start. The removed transcript is the tail, and its 5m entry expired, so the honest counterfactual is a re-write at the write rate. The new label prices it at the read rate. - Fix: demand the whole write at the 1h tier, as
ResolveTierdoes:OneHour: e.CacheWrite > 0 && e.CacheWrite1h >= e.CacheWrite. A mixed write keeps the five-minute lifetime. The new tests write 400k of 400k at 1h, so they pass unchanged. Add one case: head 1h + tail 5m, miss after 20 min, wantttl_expiry.
- What fails: a request can bill a 1h head and a 5m tail in one write (
Non-blocking
cacheAttributionTTLMs(proxy/dashcapture.go:768) now takes its 5m base fromapply.CacheLifetimeand its 1h upgrade frommodelinfo.CacheLifetime. Onemodelinfo.CacheLifetime(provider, model, oneHour)call after theExactcheck would give both. The values come from the same constants, so they cannot drift today. Style only.- Old rows keep the labels they got.
AttributeCache's comment already says so. No migration is needed for a label fix, but readers of the miss-reason trend should know the series breaks at this commit. - This change moves dashboard labels and the summarizer saving figure. CLAUDE.md asks that such a change be named at the top of the PR. I did not read the PR body (round 1 rule), so I only ask the author to confirm it is there.
What I checked
- The full diff
f43d9fa5..HEADand the code around every hunk:dash/prefixcause.go,dash/prefixcause_test.go,proxy/dashcapture.go,proxy/dash_test.go. - Every caller of the changed symbols.
ObservePrefixhas one production caller (proxy/dashcapture.go:662) and test callers, all updated for the new return value.cacheAttributionTTLMshas one production caller, updated.FactsOfhas one production caller. - The label path end to end:
FactsOf→ObservePrefix(tier inheritance on a read-only turn, parent-thread inheritance, failed-request handling) →entryAgeMs/keep-alive age cut →AttributeCachecase order →uniqueRate. - The TTL sources:
apply.CacheLifetime(nil body → 5m,Exactonly for cache-control providers),modelinfo.CacheLifetime,HonoursCacheControl(anthropic, bedrock, bedrock-mantle, vertex), so the new proxy test's bedrock case is right and the OpenAIMinimumcase still returns 0 before the 1h branch. - The new tests by hand, each case traced through
AttributeCache: all six TTL cases, the read-only tier survival, and the summarizer read-rate case give the wanted values. - The keep-alive side for consistency:
keepalive/keeper.goResolveTier(finding 1),dash/keepalive.gomissLifetime,dash/kvcache.goobservedTTL. The keeper's own tier rule and the ping paths do not touchlastFacts, so no double counting. - Data dependence: the new fact is a token count and a flag, already stored. No new data class.
docs/data-dependence.mdneeds no row. - No live tests were run (not approved). No files outside this folder were changed.
A mixed write (1h head, 5m tail) now keeps the five-minute lifetime, as keepalive ResolveTier does. cacheAttributionTTLMs makes one modelinfo.CacheLifetime call. New tests cover a mixed write and the fork inheriting its parent's tier. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
|
Round 1 replies. New head: 8499713. Reviewer 2, finding 1 (blocking, mixed write): fixed in 8499713. Reviewer 2, non-blocking:
Reviewer 1, non-blocking:
Tests: |
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 2 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.319 | offload-review skill, @amiddavid's session | resumed | 2 of 2 | 2 of 2 | 84997131b07e |
approve |
Verdict: approve
Findings
None. My round-1 finding is fixed, and I found no defect in the new commit.
Round-1 findings
-
Mixed write read as a 1h entry — fixed in 8499713.
FactsOfnow setsOneHour: e.CacheWrite > 0 && e.CacheWrite1h >= e.CacheWrite, the billed half ofResolveTier's rule. I traced the new testTestTTLExpiryOnAMixedWriteKeepsFiveMinutesby hand: turn 1 writes 50k (10k at 1h), soOneHour=false; turn 2 at +20 min reads 10k, writes 40k,partialReadholds, TTL stays 5m, 20 min > 5m →ttl_expiry. That is the exact scenario of my finding. The doc comment onPrefixFacts.OneHournow states the whole-write rule and namesResolveTier.I also checked the stricter rule for a new hole: can a pure-1h write (all written tokens at 1h) still mask a live 5m tail? No. The provider hashes head before tail, so a head write forces a tail write too. A write that is all 1h therefore covers the whole written prefix, and the shapes left are all-5m, mixed (now 5m), and all-1h. Sound.
Non-blocking
- Reviewer 1's two scoped-out items (restart seeding of the tier,
dash/keepalive.go:206using the asked tier) stay out of scope. Both are named in the PR body under Out of scope. Each deserves its own issue later, per the repo rule that a main defect does not ride a feature branch. - The author reports
go test -race ./dash/...and./proxy/...green on the shared box. I could not run them (live tests not approved). My hand traces of every new and changed test agree with the wanted values.
What I checked
- Last review: round 1, b99d17b, 2026-11-08T10:12:51Z.
- New commits read: 1 —
84997131"fix(dash): count only a whole 1h write as the one-hour tier". - Read in full: 2 reviews, 1 comment, 0 inline comments (from the three saved files), plus the PR body.
- New items since my last review, and what I did about them:
- Commit
84997131: read the full diff and the code around each hunk. TheOneHourrule, thecacheAttributionTTLMssimplification, and the two new tests all check out (details below). - Author's round-1 reply comment (2026-11-08T10:35:01Z): verified each claim against the code. "Fixed" claims are real; "out of scope" claims are named in the PR body.
- Commit
- The
cacheAttributionTTLMsrewrite: theExactgate still runs first, so OpenAIMinimumand GeminiUnknownstill return 0. The onemodelinfo.CacheLifetimecall returns the same 5m/1h values as the old two-source shape. This also closes my round-1 style note. - The
ttlTurnrefactor: it now derivesOneHour,NoWriteandPromptTokensthroughFactsOf, so the test mirrors production.PromptTokensis new and non-zero; I traced its only effect (partialRead) through every existing case — no wanted label changes. TestTTLTierIsInheritedFromTheParentThread: traced both calls. The fork's read-only first request inherits the parent's tier through thefromThreadlookup; the second request keeps it through the fork's own stored facts. This closes reviewer 1's untested-path note.- The PR body: the behavior-change block sits at the top, with who, old, new, the dollar effect, the trend break, and the data-dependence statement (metadata only). This closes my round-1 disclosure note.
- No live tests were run (not approved). No files outside this folder were changed.
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 1 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.319 | offload-review skill, @amiddavid's session | resumed | 1 of 2 | 2 of 2 | 84997131b07e |
approve |
Verdict: approve
Findings
None. The fix commit is correct, and every round-1 item is resolved or accepted as out of scope.
Earlier findings
- Reviewer 2, finding 1 (mixed write, blocking): fixed in 8499713. I checked it myself, not only the reply.
OneHouris nowCacheWrite > 0 && CacheWrite1h >= CacheWrite(dash/prefixcause.go:50).CacheWrite1his a subset ofCacheWrite, so the test says "the whole write was billed at 1h", the same billed half askeepalive.ResolveTier(keepalive/keeper.go:269). I tracedTestTTLExpiryOnAMixedWriteKeepsFiveMinutesthroughAttributeCache: head 10k at 1h, tail 40k at 5m, next turn reads 10k and re-writes 40k after 20 min.partialReadholds (20k < 50k, 40k > 10k), TTL stays 5m, 20 min > 5m →ttl_expiry. Under the old rule the tier reads 1h and the label becomesunknown, so the test guards the fix. - Reviewer 2, non-blocking (one
modelinfo.CacheLifetimecall): fixed. The refactor is behavior-identical:apply.CacheLifetime(provider, model, nil)returnsmodelinfo.CacheLifetime(provider, model, false)for Exact providers, and the non-Exact early return still runs first, so OpenAI and Gemini still get 0. - My round-1 items: mixed write — fixed by the same change. Restart seeding and
dash/keepalive.go:206— the author rejects both as out of scope and lists them in the PR body; I accept that, both are pre-existing and report-only. Dollar change named in the PR body — verified, the top block has who, old, new, dollar effect, trend break, switch ("none") and data dependence.fromThreadtest — added (TestTTLTierIsInheritedFromTheParentThread), traced and correct.
Non-blocking
- The new fork test sets
OneHourandNoWriteby hand, not throughFactsOf. It testsObservePrefixin isolation, which is what it claims, so this is fine. The production path (FactsOf→ObservePrefix) is covered byttlTurn. - The
ttlTurnhelper now also setsPromptTokensfrom the usage (it did not before). I checked the existing tests for a behavior change from that: every asserted turn hasCacheRead = 0, sopartialReadcannot change an outcome. No impact.
What I checked
- Last review: round 1,
b99d17bf3ccf65c4827c5ce7f391ae1718131e8a, 2026-11-08T10:11:59Z. - New commits read: 1 —
84997131"fix(dash): count only a whole 1h write as the one-hour tier". - Read in full: 2 reviews, 1 comment, 0 inline comments (from
.review-in-reviews.txt,.review-in-comments.txt,.review-in-inline.txt). I also read the PR body, to verify the author's claims about it. - New items since my last review, and what I did:
- Reviewer 2's round-1 review (blocking on the mixed write): checked the finding and the fix myself, as above. Its proof matches the code I read; I have nothing to add.
- The author's round-1 reply comment: checked each claim. "Tests ok on the shared box" I cannot re-run (live tests not approved, Go not on this machine); I verified the new and changed tests by hand trace instead. The PR-body claims (behavior-change block, dollar effect, trend break, out-of-scope list) I verified against the body itself: all present.
- Commit
84997131: full diff plus the code around each hunk.
- The new
OneHourrule against real usage shapes: a whole-1h write givesCacheWrite1h == CacheWrite(subset semantics,internal/providerusage/providerusage.go:15), so it still reads 1h; a 5m-only write and a missing usage block both read 5m or inherit; the round-1 tests (400k of 400k at 1h) still pass under the stricter rule. - The consumers of the
ttl_expirylabel (uniqueRate,dash/compactepisode.go:858,1019,1044) — the PR body's claims about the summarizer rate and keep-alive addressability match them, and all are report-only. ResolveTier(keepalive/keeper.go:257-277) for the claimed rule match, andapply/headttl.gofor the mixed-write shape being real.- I did not run builds or tests (not approved). No files outside this folder were changed.
|
Behavior change approved by a human developer. The repo owner (@amiddavid) approved the behavior change of #943 and #958 in the authoring session on 2026-11-08, in these words: "I approve the behavior change on both". |
SeedSessions reloaded recency and tail hash, not the billed tier that PR #943 added. After a restart, the first miss of a 1h thread fell back to the 5-minute limit and got ttl_expiry although the entry was alive. SeedSessions now reads cache_write and cache_write_1h from the latest non-ping row of each thread. It applies the FactsOf rule. ObservePrefix uses the seeded tier only while the thread has no facts, and only for a request that wrote nothing, like the read-only inheritance of #943. Closes #956 Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
Round 1 of #960, non-blocking points: - The CodeQL check run must come from GitHub's code-scanning app (github-advanced-security), so a job renamed to CodeQL cannot pass the check with no scan behind it. - A passing check run can still carry an alert. The script now also reads the open code-scanning alerts on the PR's merge ref, and a failed read stops it with an error. - The messages say what to do: the PR must target main to be scanned, and a run still in progress means wait. Tested with a stub gh: a clean PR is READY; an open alert gives NOT READY with the alert; a failed alert read gives an ERROR. On real PRs: #951 and #960 pass the scan check; #943 fails it. Assisted-By: Claude Opus 5.5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
check-ready.sh now fails a PR whose head has no passing CodeQL check run from GitHub's code-scanning app. RELEASING.md gets new rules: a PR opened before #945 needs a new push before it merges; after an --admin merge, read the merge's rule results and tell the owner of any failed rule other than the review rule; a PR's scan covers only its changed lines, so read main for all open alerts; a false positive is dismissed only with the owner's yes. Why: on 2026-11-08 #943, #937 and #944 merged past the code-scanning rule because they were opened before the CodeQL workflow (#945). CodeQL scans of main afterwards found no new alert. --admin does not say which rules it bypasses, and the script did not check code scanning. New integrator rule: an integrator talks with many sessions and with the owner, so its main session must stay free. Implementation work goes to subagents, and the main session only coordinates. The owner asked for this. Behavior change: none. Release scripts and docs only. Data dependence: none. The script reads GitHub check runs only. Reviews: 2 offload reviews (GLM), round 3 (owner granted round 3), both approve at df9d719. Assisted-By: Claude Opus 5.5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
The dashboard now seeds the billed cache tier of each thread after a proxy restart. Before, SeedSessions reloaded recency and the tail hash, but not the tier that #943 added. After a restart, the first miss of a 1h thread fell back to the 5-minute limit. It got ttl_expiry although the entry was alive. SeedSessions now reads the tier from the thread's latest successful (2xx), non-ping row with cache_write > 0. It applies the FactsOf rule: cache_write_1h >= cache_write. A failed request or a full hit does not erase the tier of the earlier write. ObservePrefix uses the seeded tier only for the first request after a restart, while the thread has no facts. A thread with no successful write in the window keeps the 5-minute limit. Closes #956. Behavior change: this affects 1h-TTL threads on Anthropic, Bedrock and Vertex, across a proxy restart. Old: the first miss after a restart, 5 to 60 minutes after the last request, got ttl_expiry. New: the label follows the billed tier of the thread's last successful cache write before the restart. A 1h thread is no longer ttl_expiry in that window. Switch: none. It is a label fix with no setting. Owner's yes, 2026-11-08, in the integrator session: "it will send u 958 and 959 both approved". Data dependence: metadata only (token counts). No change at any storage level. docs/data-dependence.md needs no update. Reviews: 2 offload reviews (GLM), round 3 (owner granted round 3), both approve at b9695d6. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
Behavior change — needs a human developer's yes
ttl_expiry. The summarizer got the write rate. The turn counted as keep-alive-addressable.prefix_changeorunknown, as for a live entry. The summarizer gets the read rate. The turn no longer counts asttl_expiry. A miss after 60 minutes is stillttl_expiry.cache_write_1handcache_writetoken counts. No stored text. No row indocs/data-dependence.mdchanges.Closes #941.
Summary
A 1h thread no longer gets
ttl_expiryfor a miss inside the hour.What changed
PrefixFactshas two new flags:OneHour(the whole write billed at 1h, as keepaliveResolveTierdoes; a mixed 1h head plus 5m tail stays 5m) andNoWrite.ObservePrefixreturns the previous same-model request's billed tier. A request that wrote nothing inherits it.cacheAttributionTTLMstakes that tier. 1h gives 3,600,000 ms. Unknown gives 5 minutes.apply.CacheLifetimeandmodelinfo.CacheLifetimeare unchanged.How it was tested
New tests cover:
ttl_expiryttl_expiryttl_expiryttl_expiryCommands, on the shared box:
go test -race ./dash/...: okgo test -race ./proxy/...: okgo vet ./dash/ ./proxy/,gofmt -l: cleanOut of scope
keepaliveCreditFloorMs(dash/event.go)Both are report-only. The accountant merge replaces them.
ttl_expiry.dash/keepalive.go:206uses the asked tier, not the billed tier.Assisted-By: Claude (Anthropic AI) [email protected]