Skip to content

fix(dash): seed seen models, per-model tier and 2xx recency on restart - #996

Merged
amiddavid merged 1 commit into
mainfrom
fix/restart-seed
Nov 10, 2026
Merged

amiddavid merged 1 commit into
mainfrom
fix/restart-seed

Conversation

@amiddavid

@amiddavid amiddavid commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Behavior change — needs a human developer's yes

  • Who: every proxy restart with --dashboard on. Only the first requests after the restart.
  • Gap 1, first request on a known model. Old: cold_start. New: hit, or the real miss reason. A model never seen in the window still reads cold_start.
  • Gap 2, tier. Old: seeded per thread, so a /model switch gave the new model the old model's tier, and a fork read 5 minutes. New: per thread and model, and a fork reads its parent's tier.
  • Gap 3, recency. Old: latest row of any status. New: latest 2xx row. A thread with only failed rows is no longer seeded, so its next request reads cold_start (no entry exists).
  • Summary saving: gap 1 lowers a too-high saving (the read rate now applies, not the write rate). Gap 3 raises a too-low saving (a real ttl_expiry is no longer hidden).
  • Switch: none. There is no flag. Revert the PR to undo it.

Closes #994

Summary

Recorder.SeedSessions now fixes three restart gaps. Data dependence: metadata only (model ids, timestamps, status, token counts). No row in docs/data-dependence.md changes.

What changed

  • seenModel is seeded from SELECT DISTINCT tenant_id, model in the window: non-ping rows with a 2xx status. A model with only failed rows stays unseeded, so it reads cold_start as in the live path.
  • seedTier is keyed by thread + model and holds the write time. ObservePrefix reads it for the request's own model, and for the parent thread when the thread is a fork. The age prune and the wholesale reset treat it like lastFacts.
  • The tier query uses ROW_NUMBER() OVER (PARTITION BY session_id, thread_id, model). The rule is the FactsOf rule: 2xx, non-ping, cache_write > 0, cache_write_1h >= cache_write.
  • The recency query adds status 2xx to both halves.
  • Other readers of the seeded recency: all want 2xx. SinceLastMs, the kaRefreshedAt age cut and ObserveThreadFrom measure from a request that wrote the entry. The grace uses its own clock (graceSeen), unchanged. No reader needs any-status recency.
  • Three existing tests stored rows with no status. They now store Status: 200.

How it was tested

  • New tests in dash/restartseed_test.go: known model reads hit and not cold_start; unknown model reads cold_start; /model switch does not leak the tier; fork inherits the 1h tier; failed request after the last write reads ttl_expiry. Each fails without its piece.
  • Mutations (piece removed, test fails): seenModel seed; per-model key; fork lookup; 2xx recency.
  • go test -race ./dash/... passes (split ^Test[A-J] and ^Test[K-Z]). go test -race ./proxy/... passes. go vet and gofmt are clean.
  • Timing, 200k synthetic rows, 4k sessions, after ANALYZE, all queries: old 1.14 s, new 1.42 s (+0.28 s once at startup).
  • Plans: recency and old tier queries: SCAN r + SEARCH r2 USING INDEX idx_requests_session. New tier query: SCAN requests USING INDEX idx_requests_session + TEMP B-TREE for the window. New model query: SCAN requests USING INDEX idx_requests_tenant + TEMP B-TREE FOR DISTINCT.

Out of scope

  • A live restart check on a real corpus. Release integration does that.
  • The seenModel wipe at 1000 entries (observe()). It is older and unchanged. New interaction: a proxy with more than 1000 tenant+model keys in the window loses the seed on its first request.
  • A seeded tier survives a request on another model only if no request ran on this thread since the restart. After one request on model B, a miss on model A reads model_changed (5 minutes), not the in-memory unknown (1h). Fixing it needs a seeded per-model write time and prompt size. This was true before the PR. Follow-up issue.

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

@amiddavid amiddavid 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 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.321 offload-review skill, @amiddavid's session fresh 1 of 2 1 of 2 347f66d9f3c3 blocking

Verdict: blocking

Findings

  1. dash/capture.go:860 — seedModels counts failed rows, so a model used only by failed requests is no longer a cold start after a restart
    • What fails: a thread has 2xx rows on model B and only failed rows on model A. The proxy restarts. The next request on model A is a miss. It reads unknown (or ttl_expiry), not cold_start. No cache entry was ever written on A, so cold_start is the true verdict. The live path agrees: it labels this request first_on_model → cold_start.
    • Proof: seedModels (capture.go:859-860) has no status filter, so it seeds seenModel["t1\x00A"] = true from the failed rows. AttributeCache gives cold_start only when !seenModel (event.go:1057), so the seeded flag removes it. The live path corrects this case with the failedKey marker (prefixcause.go:116-122), but that marker lives in lastFacts, which a restart clears — so the correction cannot fire. The comment at capture.go:856 claims "ObservePrefix still reads first_on_model after a failed one"; after a restart that claim does not hold. Before this PR, seenModel was not seeded at all, so the restart path read cold_start and matched the live label by accident.
    • Fix: filter seedModels to 2xx rows (status >= 200 AND status < 300), as the recency seed now does. Add a test: rows on model A all failed, restart, next request on A reads cold_start.

Non-blocking

  • seedModels can return more than 1,000 tenant+model keys on a busy multi-tenant proxy. observe() then clears the whole seenModel map on the next request (capture.go:655-657), wiping the seed for every model. The cap is pre-existing; the seed only raises the odds of hitting it.
  • No test covers two smaller new rules: a failed 1h write must not seed the tier, and a keep-alive ping must not seed seenModel. The SQL does both correctly.
  • This PR changes attribution labels across restarts (that is its purpose). Repo rules say the PR must name the behavior change at the top and have a human's approval. I did not read the PR body in round 1, so I cannot confirm it is named there.

What I checked

  • Read the full diff (5 files) and the code around each change: dash/capture.go (Recorder fields, pruneSessionsLocked, SeedSessions, seedModels, seedOneHourTiers), dash/prefixcause.go (ObservePrefix), and the new dash/restartseed_test.go.
  • Traced the key rules end to end:
    • The 2xx filter in the recency seed: a failed row no longer re-dates a thread; the thread falls back to its latest 2xx row. Matches TestRestartSeedRecencyIgnoresAFailedRequest and the live rule (ObservePrefix commits only 2xx).
    • The per-model tier key: seedTier keys are thread.Key(session, thread) + "\x00" + model on both the write side (capture.go:901) and the read side (prefixcause.go:97,108). thread.Key (internal/thread/thread.go:49) uses the same separator, so the keys agree.
    • The fork path: perModel appends cur.Model in both the own-thread and the parent lookup, so a fork on model B cannot read the parent's model-A tier. The !same && !hadAny guard keeps live facts above the seed.
    • write1h >= write with write > 0 in the seed matches FactsOf (prefixcause.go:50).
    • The ROW_NUMBER window function matches the style already used in dash/keepalive.go and kvcache.go; modernc.org/sqlite supports it. id is INTEGER PRIMARY KEY AUTOINCREMENT (schema.go:59), so the ORDER BY ts DESC, id DESC tiebreak is valid. idx_requests_ts covers the ts-bounded scans.
    • seedTier pruning by age uses the write time, which is inside the 2-hour seed window; the wholesale 20,000-entry reset still covers it.
  • Checked the callers: SeedSessions runs once at startup (cmd/context-guru-proxy/main.go:427); ObservePrefix is called from proxy/dashcapture.go:713 with commit = 2xx, and the new test helper turnOn mirrors that call.
  • Checked the updated tests (cachecost_test.go, thread_test.go, grace_test.go): each restart row that must seed now carries Status: 200, consistent with the new filter.
  • Data dependence: unchanged. All three seeds read the requests table (ids, timestamps, token counts, status) — metadata only, same as before.
  • I did not run builds or tests; live tests are not approved for this review.

@amiddavid amiddavid 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 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.321 offload-review skill, @amiddavid's session fresh 2 of 2 1 of 2 347f66d9f3c3 approve

Verdict: approve

Findings

None. I traced every changed path and found no wrong behavior introduced by this commit.

Non-blocking

  • dash/prefixcause.go:112 — a request on another model after the restart drops the seeded tier. The mask is seededTier && !same && !hadAny. hadAny is thread-wide, so one post-restart request on model B erases the seeded 1h tier of model A. Trace: 1h write on A at T0, restart, one request on B, then a miss on A at T0+20 min. Result: model_changed with a 5-minute TTL. Without a restart the live rule keeps lastFacts[key+"\x00"+A] and reads unknown with the 1h TTL. This hole predates the PR (the old thread-level seed had the same mask) and the PR narrows it, so I do not block. Recommend a follow-up issue.
  • dash/capture.go:848 — the seed can be wiped at once on a busy proxy. SeedSessions now fills seenModel from the database. observe() clears the whole map when it passes 1000 entries. A proxy with more than 1000 distinct tenant+model pairs in the 2-hour window gets its seed discarded by the first request after the restart, so the fix silently does nothing there. The PR lists the wipe as out of scope. Note the new interaction in that issue.
  • dash/capture.go:760 — the comment "One indexed group-by at startup" is stale. SeedSessions now runs three queries (recency, tiers, models). Words only.

What I checked

  • Read CLAUDE.md and docs/design.md. The change is dash-only, metadata-only (status, timestamps, model ids, token counts). The PR states this and names the behavior change at the top, as the repo rules require.
  • Read the full diff and the code around each change: dash/capture.go, dash/prefixcause.go, and the caller in proxy/dashcapture.go (observe at line 666, ObservePrefix at line 713 with commit = 2xx).
  • Key shapes verified: seedTier keys are thread.Key(session, thread) + "\x00" + model, the same key ObservePrefix's perModel builds; seedModels keys are tenant_id + "\x00" + model, the same key observe() builds; the fork lookup mirrors the lastFacts fork fallback.
  • Traced each new test in dash/restartseed_test.go by hand against ObservePrefix and AttributeCache. Each one fails without its piece, as claimed: the per-model key (a thread-level key would leak A's 1h tier to B), the fork lookup, the seen-model seed, and the 2xx recency filter.
  • Checked that all other tests that call SeedSessions (cachecost_test.go, thread_test.go, prefixcause_test.go, grace_test.go) store rows with a 2xx status where the seed must find them. The three updated tests are exactly the ones that needed Status: 200.
  • Checked the prune paths: seedTier ages by its own write time, which is always older than the matching lastFacts timestamp, so the seed never outlives the facts it stands in for. The wholesale reset clears it with the others.
  • Checked the SQL: the window function needs SQLite 3.25+; the driver is modernc.org/sqlite v1.56.0, which bundles a newer SQLite. The ts filters can use idx_requests_ts and idx_requests_tenant.
  • The 2xx-only recency seed diverges from the live path, where observe() runs before the status is known and re-dates lastSeen on a failed request. The seed direction is the more correct one (a failed request wrote no entry), and the PR documents it. I found no reader that needs any-status recency.
  • Live tests were not approved, so I did not run them. I verified the test code by reading it, not by execution.

SeedSessions now also:
- seeds seenModel from the models in the seed window, so the first
  request on a known model is no longer a cold_start;
- seeds the billed tier per thread and model, and a fork reads its
  parent's tier;
- takes recency from the latest 2xx row, as SeedGraceSessions does.

Closes #994

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

Copy link
Copy Markdown
Collaborator Author

Round 1 replies. New head is the one this comment follows.

Reviewer 1, blocking (seedModels counts failed rows). Fixed. seedModels now reads 2xx rows only, and the comment is corrected. Test TestRestartSeedsNoModelFromFailedRows fails without the filter.

Reviewer 1, non-blocking.

  • 1000-entry wipe: named under Out of scope in the PR body. The cap is unchanged.
  • Missing tests: added TestRestartFailedOneHourWriteSeedsNoTier and TestRestartSeedsNoModelFromAPing. Each fails when its SQL filter is removed.
  • Behavior-change block: it is at the top of the PR body.

Reviewer 2, non-blocking.

  • prefixcause.go:112 (one request on model B drops model A's seeded tier): not fixed here. The mask uses the thread-wide hadAny. Reading unknown with the 1h TTL, as in memory, needs a seeded per-model write time and prompt size. That is not a small change. It was true before this PR. Named under Out of scope as a follow-up.
  • capture.go:848 wipe: see above.
  • capture.go:760 stale comment: fixed ("Three queries at startup").

@amiddavid amiddavid 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 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.321 offload-review skill, @amiddavid's session resumed 1 of 2 2 of 2 de3e0b106932 approve

Verdict: approve

Findings

None. My round-1 finding is fixed and verified. The new commit introduces no defect I can show.

Round-1 findings

  1. Fixed — seedModels counts failed rows (mine, blocking).
    • The query now filters status >= 200 AND status < 300 (capture.go:860). The comment no longer claims live parity through the failedKey marker; it states the true reason. TestRestartSeedsNoModelFromFailedRows covers it. I traced the test: with the filter, model A stays unseeded and the request reads cold_start; without it, seenModel[A] is true and the verdict falls through to unknown. The test distinguishes.

The other reviewer's round-1 findings

  • prefixcause.go:112, one request on model B drops model A's seeded tier — rejected by the author, and I accept the reason. I checked the mask myself: seededTier = seededTier && !same && !hadAny, and hadAny is thread-wide. Keeping the tier past a request on another model needs a seeded per-model write time and prompt size, which is the full lastFacts seed the PR deliberately does not build. The hole predates the PR and is named under Out of scope with a follow-up issue. Not a blocker.
  • capture.go:848, the 1000-entry seenModel wipe — out of scope, accepted. The cap is older and unchanged. The new interaction (a seed larger than 1000 keys is wiped by the first request) is named under Out of scope in the PR body.
  • capture.go:760, stale "one indexed group-by" comment — fixed. The comment now says "Three queries at startup".

Non-blocking

  • One residual label divergence remains by design: a thread with a 2xx row on model A and a later failed row on A reads first_on_model (cold_start) on the live path, but not after a restart, because the failed marker lives in lastFacts and is not seeded. The restart direction is the more accurate one (the 2xx write's entry may still exist). Same class as the out-of-scope item above; noting it for the follow-up issue.

What I checked

  • Last review: round 1, 347f66d, 2026-11-09T19:44:05Z
  • New commits read: 1 (de3e0b1 fix(dash): seed seen models, per-model tier and 2xx recency on restart — the squashed PR commit with the round-1 fixes folded in)
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from the three files)
  • New items since my last review, and what I did about each:
    • Reviewer 2's round-1 review (19:47:26Z, approve, 3 non-blocking items) — read in full; checked each item against the code and the author's reply.
    • The author's round-1 reply comment (20:10:35Z) — read in full; verified every claim it makes (see below).
    • Commit de3e0b1 — read the full diff since 347f66d9 (2 files: capture.go, restartseed_test.go) and the final state of each changed hunk.
  • Verified the fix: seedModels (capture.go:858-874) now filters to 2xx, and the corrected comment matches the code.
  • Traced each new test by hand against observe(), ObservePrefix and AttributeCache:
    • TestRestartSeedsNoModelFromFailedRows — fails without the 2xx filter, as claimed.
    • TestRestartSeedsNoModelFromAPing — the ping row gets status 200 from the helper's default, so only the keepalive = 0 filter keeps model A unseeded; the test covers that filter.
    • TestRestartFailedOneHourWriteSeedsNoTier — without the status filter in seedOneHourTiers, the failed 1h write would seed the tier and the 20-minute miss would read unknown, not ttl_expiry. The test distinguishes.
  • Verified the author's other replies against the PR body (read in full via the API): the behavior-change block is at the top with who, old and new behavior, and the switch; data dependence is stated as metadata only; the 1000-entry wipe and the model-B tier drop are named under Out of scope.
  • Confirmed the full PR diff (base 8c12384e..de3e0b1) is exactly my round-1 diff plus the round-2 delta; nothing else moved.
  • I did not run builds or tests; live tests are not approved for this review.

@amiddavid amiddavid 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 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.321 offload-review skill, @amiddavid's session resumed 2 of 2 2 of 2 de3e0b106932 approve

Verdict: approve

Findings

None. The fix for reviewer 1's blocking finding is correct, and the new tests cover it.

Earlier findings

  • Reviewer 1, finding 1 (seedModels counted failed rows) — fixed. I checked it myself. seedModels now filters status >= 200 AND status < 300 (capture.go:867). I traced why the filter is the right rule, not just a patch: live, a failed request still sets seenModel, but ObservePrefix's failedKey marker then labels the next request on that model first_on_model → cold_start (prefixcause.go:116-122, event.go:1057). A restart clears that marker, so an any-status seed would have read unknown. The 2xx seed gives the same verdict as the live path. I also traced the other row kinds: a ping never marks seenModel live (keepalive = 0 in the query, pings never call observe()), and a status-0 row sets the marker live, so cold_start is the matching seed verdict there too.
  • Reviewer 1, non-blocking (1000-entry wipe): not fixed, named under Out of scope in the PR body, with the new interaction spelled out. Accepted. The cap is older than this PR.
  • Reviewer 1, non-blocking (missing tests): fixed. TestRestartFailedOneHourWriteSeedsNoTier and TestRestartSeedsNoModelFromAPing added. I traced both below.
  • Reviewer 1, non-blocking (behavior-change block): confirmed. I read the PR body; the behavior-change block is at the top.
  • My round-1 non-blocking (prefixcause.go:112 mask): rejected by the author, and I accept the reason. The reason: returning only prevOneHour = true would not match the in-memory result, because the model_changed path also needs the per-model write time (for the gap) and the previous prompt size. That matches the code — the switch's hadAny branches return prevOneHour = false and gapMs = -1 (prefixcause.go:139-141), so a mask change alone fixes nothing. It predates the PR and is now named under Out of scope as a follow-up.
  • My round-1 non-blocking (stale "one indexed group-by" comment): fixed ("Three queries at startup").
  • My round-1 non-blocking (seed wiped at 1000 entries): not fixed, same Out-of-scope entry as reviewer 1's. Accepted.

What I checked

  • Last review: round 1, 347f66d9f3c3cfa28176fcf87411909a6570ca70, 2026-11-09T19:47:26Z.
  • New commits read: 1. The branch was rewritten: de3e0b10 replaces 347f66d9 (same title, amended content). I read git diff 347f66d9..HEAD in full. The delta is two files: the seedModels 2xx filter plus comment in dash/capture.go, and three new tests in dash/restartseed_test.go. I also re-read the full diff against the base to confirm nothing else moved.
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from the three saved files).
  • New items since my last review, and what I did:
    • Reviewer 1's round-1 review (blocking on seedModels). Checked the fix and the reasoning myself, as above.
    • The author's round-1 replies comment (2026-11-09T20:10:35Z). Verified each claim: the filter, the two new tests, the comment fix, and the two Out-of-scope entries in the PR body.
    • The rewritten head de3e0b10. Reviewed its delta as a first review, below.
  • The three new tests, traced by hand:
    • TestRestartSeedsNoModelFromFailedRows: model A has only a 500 row, so the seed skips it and the request reads cold_start. Under the round-1 query it read unknown, so the test fails without the filter.
    • TestRestartSeedsNoModelFromAPing: model A has only a ping row, excluded by keepalive = 0. Reads cold_start, matching the live rule that pings never mark a model seen.
    • TestRestartFailedOneHourWriteSeedsNoTier: the thread's 1h write is a 500, so seedOneHourTiers skips it and the tier stays 5 minutes. The miss at 20 minutes reads ttl_expiry. Without the 2xx filter in that query the tier would read 1h and the test would fail.
  • The seedModels comment's new claim (the live path labels a failed-only model first_on_model "through a marker that a restart clears") is accurate against prefixcause.go:116-122.
  • Live tests were not approved, so I did not run them. I verified the tests by reading them, not by execution.

@amiddavid

Copy link
Copy Markdown
Collaborator Author

Behavior change approved by a human developer.

The repo owner (@amiddavid) approved the behavior change of #996 in the authoring session on 2026-11-10, in these words: "I approve".

@amiddavid
amiddavid merged commit 52eea13 into main Nov 10, 2026
9 of 11 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 10, 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]>
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.

Restart seeding: seenModel, per-model tier and 2xx recency

1 participant