Repository navigation
fix(dash): size the keep-alive savings window from the billed tier - #959
Conversation
eb24e0d to
6325668
Compare
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 | 6325668dd0b3 |
blocking |
Verdict: blocking
The code is correct as written. One finding, and it is a missing test, not a wrong line.
Findings
- dash/keepalive.go:208-212 — no test pins the thread boundary of the new inherit lookup
- What fails: the inherit subquery scopes by
bw.thread_id = r.thread_id. Every row inTestKaSavedWindowFollowsTheBilledTier(dash/keepalive_ttl_test.go:93) hasthread_id = '', becausekaAgent/kaCredit/kaPingnever setThreadID. So all test rows share one thread. If a later edit drops thebw.thread_idterm, no test fails. Then a read-only rescued row on thread B of a session inherits a whole-1h write from thread A. It gets the 60-minute window. That is the same over-credit class this PR removes. - Proof:
mkEvent(dash/store_test.go:12) sets noThreadID; the new test builds every event fromkaAgent/kaPing/kaCredit(dash/keepalive_ttl_test.go:117-126).Event.ThreadIDexists and is stored (dash/event.go:138, dash/store.go:585). The keeper's rule is per thread:tier1his keyed per thread (keepalive/keeper.go:471-474, 958-960). So the thread scope of the subquery is load-bearing and matches the keeper, but nothing tests it. - Fix: add one case to
TestKaSavedWindowFollowsTheBilledTier. Setw.ThreadID = "a:H"on the 1h writer and leave the rescued row on"". Ping 30 minutes before. Want: no credit. This case fails today if thebw.thread_idterm is removed.
- What fails: the inherit subquery scopes by
Non-blocking
- dash/keepalive.go:165-166 — the comment claims "the rule of keepalive.ResolveTier" but the ask half is looser.
cache_ttl = 'ephemeral_1h'means ANY breakpoint asked 1h (apply.ttlTier→bodyAsksExtendedTTL, apply/apply.go:1043-1058).ResolveTierneeds EVERY breakpoint (keepalive/keeper.go:269-271). A mixed ask whose new write lands wholly at 1h still gets the 1h window. The write-ratio check neutralizes most of this, and the PR body names the gap. But the comment here does not.factsCTEstates the same gap as "a known gap" (dash/keepalive.go:1322-1326). Add one sentence here too. - dash/keepalive.go:207 — when r itself wrote, r's own columns decide the window. That is the tier of the entry r leaves, not the entry r read before its own write. It matches
ResolveTierand test case 8, so it is stated behavior. Rare cost: a 1h-billed head read by a row whose own new write is billed 5m loses the 1h window. Under-credit, narrow case. Fine as shipped. - dash/keepalive.go:204-205 — "it never runs for an uncredited row" is true, but "once per credited row" (PR body) may not be. The
bwsubquery sits in the last AND term of the EXISTS's WHERE. It is correlated toronly. SQLite can re-evaluate it for each candidate ping rowkpthat reaches that term. So the count may be pings-per-credited-row, not 1. Each run is anidx_requests_tenant_sessionlookup, so the cost stays small. Worth one measurement on the 2.1 GB corpus before the comment's claim is trusted. - dash/keepalive.go:172 — duplicated sentence. "A one-hour thread is reachable from a ping up to an hour earlier. A 1-hour thread is pinged at 59 minutes..." Say it once.
What I checked
- The full diff (2 files) and the code around each change:
kaSavedGate,kaSavedSince,kaSaved, and every caller (Overview,KeepAliveLedger,KeepAliveNetUSDByTenant,KeepAliveSessions,KeepAliveLive,savings.go,spendexplain.go,cachestate.go,campaignsavings.go,overview_lean.go,query.go,keepalivestrategy.go). All pass alias"r."overFROM requests r, so the newr.thread_idreferences resolve everywhere. - The SQL against
keepalive.ResolveTier(keepalive/keeper.go:269-277) and the keeper'stier1hthreading (keeper.go:893-984). The write half (cache_write_1h >= cache_write) and the inherit half (latest earlier non-ping writer of the thread) match, including the "unknown is 5m" default. modelinfo.CacheLifetimeSQL(internal/modelinfo/cachelifetime.go:51-56): the newbilledHourlands in theWHENslot; the OpenAI branch stays first, so the subquery never runs for OpenAI rows.- Schema:
thread_idandcache_write_1hexist,NOT NULL DEFAULT 0(dash/schema.go:68, 154);idx_requests_tenant_sessionexists (dash/schema.go:317). Tenant scoping of the subquery is by correlation, so a crafted filter cannot widen it. - Both tests, line by line, with the fixture helpers (
kaAgent,kaPing,kaCredit,mkEvent). All 9 cases' expected outcomes follow from the new rule; I traced each window arithmetic by hand. - That the new window is never wider than the old one:
billedHourimpliescache_ttl = 'ephemeral_1h', so the change only removes credits. That is the direction issue #957 asks for. - That credited rows only exist for Anthropic-family and OpenAI GPT-5.6+ providers (
keepalive/ping.go:107-115), so thettlTierdefault branch (a non-1h-asking provider labelledephemeral_1h) cannot reach this gate. - Issue #957 and the PR description. The PR names the behavior change at the top, states data dependence (metadata only, no row changes), and checks the #930 accountant path. That satisfies the repo's behavior-change and data-dependence rules, pending the human developer's yes the issue itself calls for.
- No live tests were run (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 | 6325668dd0b3 |
blocking |
Verdict: blocking
Findings
- dash/keepalive.go:208 — the inherit lookup's thread scope and latest-writer rule have no test
- What fails: a later change that drops
AND bw.thread_id = r.thread_id, or flipsORDER BY bw.ts DESCto ASC, keeps the whole suite green while changing which rows get credit. Cross-thread inheritance would let a subagent's 1h write set the main thread's window. Earliest-writer-wins would let an early 1h write override a later 5m write. - Proof:
TestKaSavedWindowFollowsTheBilledTier(dash/keepalive_ttl_test.go:93) builds every row fromkaAgent,kaPingandkaCredit. None of these setsThreadID(dash/keepalivetab_test.go:29-63, dash/store_test.go:12), so all rows sharethread_id ''. Each case also adds at most one writer row (dash/keepalive_ttl_test.go:118-121). So both scoping rules of the new subquery are untested. The sibling feature pins exactly these two rules:TestCacheThreadsTakesEachThreadsTTLFromItsLatestWrite(dash/cachestate_test.go:213) covers per-thread tier and latest-write-wins. - Fix: add two cases to the table. One: an earlier 1h writer in a different thread of the same session — the rescued row must NOT inherit (5m window). Two: a 5m write after a 1h write in the same thread — the latest write must win (5m window), matching
ResolveTier's chain.
- What fails: a later change that drops
Non-blocking
- dash/keepalive.go:172-174: the inserted sentence duplicates the next one ("A one-hour thread is reachable from a ping up to an hour earlier. A 1-hour thread is pinged at 59 minutes"). Trim one.
- The SQL can over-credit against
ResolveTierin two narrow cases, both toward the larger window:cache_ttlrecords ANY breakpoint asking 1h whereResolveTierneeds EVERY (declared in the PR and infactsCTE), and a read-only row that asks 5m breaksResolveTier's chain while the SQL skips read-only rows and inherits from the last writer (undeclared).SavedUSDis documented as a ceiling, and the SQL is a superset ofResolveTierin every case I traced, so no rescue the keeper would call 1h loses its credit. One comment line would cover the second case. - "It runs once per credited row" (PR body): the scalar subquery sits in the EXISTS's WHERE clause, so SQLite evaluates it once per candidate ping row of a credited read-only 1h-ask row. SQLite does not cache correlated scalar subqueries. The cost stays bounded (credited rows × session pings × an indexed walk of the session), and the 314 ms figure in the comment predates the subquery. No defect claimed, but the claim is stronger than proven. A measurement on the corpus would settle it.
- dash/keepalive.go:211:
bw.ts < r.tsexcludes a writer that lands in the same millisecond as the rescued row.ResolveTier's chain is sequential, so such a write would count. The tie-break (ORDER BY bw.ts DESC, bw.id DESC) is already there; only the filter is strict. Narrow, and it errs toward the smaller credit.
What I checked
- The full diff (dash/keepalive.go, dash/keepalive_ttl_test.go) and the code around each change.
- SQL construction: paren balance of
billedHour, alias scoping (r,kp,bware distinct; every caller passes"r."), CASE branch order (openai first, thenr.cache_ttlandr.cache_write, so the subquery runs only for a read-only 1h-ask credited row),COALESCE(..., 0)for the no-writer case,cache_write_1hisNOT NULL DEFAULT 0. - Semantics against
keepalive.ResolveTier(keepalive/keeper.go:269): traced own-write, inherit, mixed-write, and no-writer cases. The SQL window is a superset ofResolveTierin every case, so the change never removes credit from a thread the keeper itself treats as 1h. - All ten inline sites of
kaSaved/kaSavedSince: same alias, nobw/kpcollisions elsewhere, and the campaign query's placeholder order is unaffected (the subquery adds no bind parameters). - Plan guards: the
SEARCH wban inTestCacheThreadsQueryIsIndexeddoes not matchSEARCH bw;idx_requests_tenant_session (tenant_id, session_id, ts)exists and fits the subquery. - Other tests: fixtures with
cache_ttl''orephemeral_5mare unchanged (the new gate narrows onlyephemeral_1hrows); the demo builder seedsCacheWrite1h = CacheWriteon 1h rows (dash/demodb_test.go:152), so demo rescued rows keep their credit. - PR body: the behavior change is named at the top with old and new behavior; data dependence is stated (metadata only, no row change), which matches the dashboard row in docs/data-dependence.md.
- Not run: builds and tests. Live tests are not approved for this review, and Go is not on this machine. This is a static review.
) kaSavedGate used the tier the rescued request asked for (cache_ttl). A 1h ask billed at 5m, or a mixed write, got a 60-minute window for a 5-minute entry. The window now follows the billed tier, with the rule of keepalive.ResolveTier. Unknown tier means 5 minutes. Closes #957 Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
6325668 to
d1be5bb
Compare
|
Round 1 answers. Head is now Reviewer 1, finding 1, and Reviewer 2, finding 1 (no test for the thread scope and latest-write rule): fixed.
Duplicated sentence at Ask half looser than Cost claim "once per credited row" (both reviewers): corrected, and measured.
r's own columns decide when r wrote (R1): left as is. This is the tier of the entry r leaves, as in Tests on the box: |
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 | d1be5bb4a83e |
approve |
Verdict: approve
All round-1 findings are fixed or rejected with a reason that holds. The new commit changes comments and adds one test. No new finding.
Findings
None.
Earlier findings
- Mine, round 1 — no test pins the thread boundary of the inherit lookup: fixed.
TestKaSavedInheritsFromTheLatestWriteOfTheSameThread(dash/keepalive_ttl_test.go:141) setsThreadIDon the writer and the rescued row. I traced all four cases by hand:- Cross-thread: a 1h writer on
a:H, rescued row on""→ the subquery finds no writer in the rescued row's thread → 5m window → ping 30 min earlier gives no credit. Droppingbw.thread_id = r.thread_idmakes the writer match → 1h window → the case fails. The pin works. - Latest-write: a 1h write then a later 5m write on the same thread → the latest (5m) wins → no credit. Flipping
ORDER BY bw.ts DESCto ASC picks the 1h write → the case fails. - Both controls (same-thread 1h writer; 5m write then later 1h write) must be credited, and the ASC mutation also breaks the second control. A broken fixture cannot pass for the wrong reason.
- Cross-thread: a 1h writer on
- Reviewer 2, round 1, finding 1 (same gap, plus the latest-writer rule): fixed by the same test. I checked it myself; the two mutations above are exactly the two rules that reviewer named.
- Non-blocking, both reviewers — duplicated sentence at keepalive.go:172: fixed. The comment now says it once.
- Non-blocking, mine — the ask half is looser than
ResolveTierand was not stated in the comment: fixed. The comment now says "The ask half is 'any breakpoint asked 1h', looser than ResolveTier's 'every' (as factsCTE notes)" (dash/keepalive.go:171-172). - Non-blocking, reviewer 2 — a read-only 5m-ask row does not break the SQL chain, undeclared: fixed. The comment now states it (dash/keepalive.go:172-173). I checked the claim: the subquery skips read-only rows (
bw.cache_write > 0), and the keeper also keeps the chain (tier1his not updated by a read-only row, keepalive/keeper.go:958-960). Both sides agree. - Non-blocking, both reviewers — "runs once per credited row" was stronger than proven: fixed. The comment now says "SQLite may run it once per candidate ping", with a measurement: 4.8 s to 5.3 s for a full-table SUM on 200k rows, and a plan that seeks
idx_requests_tenant_session. The claim is now hedged and measured. Not running the 2.1 GB corpus is fine; the comment no longer asserts what was not measured. - Non-blocking, reviewer 2 —
bw.ts < r.tsis strict: rejected by the author. The reason: a writer in the same millisecond as the rescued row is ignored, which gives the smaller window and the smaller credit. I checked the direction; it is the fail-safe way. Accept. - Non-blocking, mine — r's own columns decide the window when r wrote: rejected by the author. The reason: this is the tier of the entry r leaves, as in
ResolveTier, and test case 8 pins it. I said "fine as shipped" in round 1. Accept.
Non-blocking
- None new. The new comment line "4.8 s to 5.3 s on 200k rows" is a test-fixture measurement under
-race, not the production corpus. The comment does not claim otherwise, so nothing to fix.
What I checked
- Last review: round 1,
6325668dd0b3f86079d366af4f9d8004502f350e, 2026-11-08T15:57:22Z. - New commits read: 1 —
d1be5bb4"fix(dash): size the keep-alive savings window from the billed tier (#957)" (force-push; the round-1 head6325668dis replaced). - Read in full: 2 reviews, 1 comment, 0 inline comments (from
.review-in-reviews.txt,.review-in-comments.txt,.review-in-inline.txt). - New items since my last review: the commit
d1be5bb4(comment rewrite plusTestKaSavedInheritsFromTheLatestWriteOfTheSameThread) and the author's round-1 answer comment at 2026-11-08T16:14:41Z. I verified every claim in that comment against the code; each held. - The full diff
6325668d..d1be5bb4: onlydash/keepalive.go(comments) anddash/keepalive_ttl_test.go(the new test). No SQL or behavior changed since round 1, so my round-1 reading ofkaSavedGate,ResolveTierparity, callers, schema, and indexes still stands. - The new test, line by line: fixture helpers, thread scoping, window arithmetic for all four cases, and both mutations the author claims (drop
bw.thread_id; flipORDER BYto ASC). Each mutation fails at least one case, and the ASC mutation also fails a control. - The commit message: DCO sign-off and
Assisted-By:attribution follow the repo conventions. - No live tests were run (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 | resumed | 2 of 2 | 2 of 2 | d1be5bb4a83e |
approve |
Verdict: approve
Findings
None. The one commit since my last review fixes my finding and Reviewer 1's finding, and I found no new defect in it.
Status of earlier findings
-
Mine, round 1, and Reviewer 1's finding 1 (the same finding — I checked it myself in round 1): fixed.
TestKaSavedInheritsFromTheLatestWriteOfTheSameThread(dash/keepalive_ttl_test.go:141) setsThreadIDon the writers and on the rescued row.- I traced all four cases by hand. Cross-thread case: the 1h writer sits on
a:H, the rescued row on"", so the inherit lookup finds no writer and the window is 5 minutes; the ping 30 minutes before gives no credit. Latest-write case: a 1h write atbaseand a 5m write atbase+10minon the same thread; the latest write decides, so 5 minutes, no credit. Both controls (same-thread 1h writer; 5m then 1h write) credit through the 60-minute window. - I checked both mutations myself. Dropping
bw.thread_id = r.thread_idmakes the cross-thread case inherit and fail. FlippingORDER BY bw.ts DESCto ASC makes the latest-write case pick the early 1h writer and fail, and its control pick the early 5m writer and fail. The ping at 30 minutes before the rescue separates the two windows cleanly (30 min is inside 1h, outside 5m). - The controls are a good design: a broken fixture cannot pass for the right reason.
-
My round-1 non-blocking notes: all addressed.
- Duplicated sentence: removed. The comment says it once (dash/keepalive.go:174).
- The two divergences from
ResolveTier(any-vs-every breakpoint ask; a read-only 5m-ask row not breaking the chain): both stated in the comment now (dash/keepalive.go:173-174). The wording matches what I traced in round 1 — both err toward the larger window, the documented ceiling direction. - The cost claim: corrected and measured. The comment now says SQLite may run the lookup once per candidate ping, names the index seek, and gives 4.8 s to 5.3 s on 200k rows. The author's reply gives the halves: 4.8 s before the change, 5.3 s after, about 10% — the full-table pass dominates, not the subquery. That answers my round-1 concern.
bw.ts < r.tsstrict: the author left it as is, with the reason that a same-millisecond writer is ignored and the credit is smaller. I accept the reason. I verified the direction myself in round 1; it is the fail-safe one.
-
Reviewer 1's non-blocking notes: addressed or rightly rejected.
- Ask half looser than
ResolveTier: documented (same comment line as mine). - r's own columns decide when r wrote: left as is. The reason is right — it matches
ResolveTier's own-write branch, and test case 8 pins it. I verified the same match in round 1. - Cost claim: measured, as above.
- Ask half looser than
Non-blocking
- dash/keepalive.go:172: "4.8 s to 5.3 s on 200k rows" reads as a range of one query. It is old against new (the reply states the halves). One word ("from 4.8 s to 5.3 s") would fix the reading.
- dash/keepalive.go:174: the line runs long after the merge ("...inherits the last writer's tier. A 1-hour thread is pinged at 59 minutes..."). Cosmetic only.
What I checked
- Last review: round 1, 6325668, 2026-11-08T15:58:30Z.
- New commits read: 1 — d1be5bb "fix(dash): size the keep-alive savings window from the billed tier (#957)".
- Read in full: 2 reviews, 1 comment, 0 inline comments (from the three files in my folder).
- New items since my last review: the author's round-1 answers (comment at 2026-11-08T16:14:41Z) and commit d1be5bb. I read both in full and checked every claim in them against the code.
- The new commit's diff line by line: the new test (all four cases traced by hand, both mutations traced by hand) and the rewritten comment block (read in the file, claims checked against the author's measurement and my round-1 traces).
- Reviewer 1's round-1 review, read in full for the first time this round. Their finding 1 is the same as mine; their non-blocking items overlap with mine or are settled above. Nothing in it is unaddressed.
- Not run: builds and tests. Live tests are not approved for this review, and Go is not on this machine. This is a static review. The author reports
go test -race ./dash,go vetandgofmtclean on the box; I could not verify that myself.
|
Behavior change approved by a human developer. The repo owner (@amiddavid) approved the behavior change of #959 in the authoring session on 2026-11-08. He was asked "yes or no to #959's behavior change" and answered "yes for both". |
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]>
Behavior change — needs a human developer's yes
Summary
kaSavedGatenow uses the billed tier, askeepalive.ResolveTierdoes.What changed
cache_write_1h >= cache_write).EXISTS, behind the> 0guard. Order is unchanged. It runs only for credited rows. For a credited read-only row it can run once per candidate ping. It is an index seek onidx_requests_tenant_session.SUMwent from 4.8 s to 5.3 s (about 10%).bw, notw.TestCacheThreadsQueryIsIndexedbans aSEARCH wplan line. That ban targets a per-thread subquery, which this is not.factsCTE: "asked 1h" is the row'scache_ttl, not every breakpoint.How to test
TestKaSavedWindowFollowsTheBilledTier(9 cases). It failed 4 cases before the fix.TestKaSavedInheritsFromTheLatestWriteOfTheSameThread(4 cases, 2 controls). Droppingbw.thread_id = r.thread_idfails the cross-thread case. FlippingORDER BYto ASC fails the latest-write case.dash/keepalive.gofromorigin/main, the test fails the same 4 cases.go test -race ./dashpasses, run as^Test[A-J]and^Test[K-Z](one run hits the 10-minute limit on the box).go vetandgofmtclean.TestKaSavedCreditsAOneHourRescuenow seeds a whole 1h write, since the tier comes from the billed write.PR #930 (accountant)
keepalive_saved_usd(dash/accountant_price.go,e.KeepAliveSavedUSD = fig.KeepAliveSavedUSD).kaSavedGateis unchanged there and all readers still call it. The fix still applies. The two PRs touch different files.Out of scope
keepaliveCreditFloorMs) still usesCacheTTL.Data dependence
Metadata only (timestamps, token counts, tier). No row in
docs/data-dependence.mdchanges.🤖 Generated with Claude Code