Repository navigation
feat(cache-warming): Backfill reply gaps from stored Turns - #2944
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
94cc287 to
ea948b5
Compare
ea948b5 to
2bcfd0f
Compare
2bcfd0f to
8cc2168
Compare
BYK
left a comment
There was a problem hiding this comment.
Will this code be useful after the first time it is used or shall we wait for the next deploy and remove it afterwards?
8cc2168 to
4db0f77
Compare
d4da481 to
0798f38
Compare
0798f38 to
89a33ef
Compare
89a33ef to
12ee131
Compare
e695d28 to
b0f9a6b
Compare
b0f9a6b to
8b46748
Compare
8b46748 to
b4323a8
Compare
Replay the stored model calls of the last 30 days with the live recording rules, so warming can start right after deploy. The heartbeat runs it once per install and fills only empty segment lists. Co-Authored-By: BYK <[email protected]>
Scan the newest Conversations first and stop after 60 seconds. A run that uses its budget writes the gaps that it has and completes, so it cannot retry each hour without finishing. Co-Authored-By: BYK <[email protected]>
State lists have no atomic replace, so live recording can append while the backfill writes a segment list. Write at most half the list, so those appends do not trim live gaps. Co-Authored-By: BYK <[email protected]>
Query 10 Conversations per batch so each query stays short and the budget check runs often. A batch that the statement timeout cancels stops the scan like the budget does, so the run still writes its gaps and completes. Co-Authored-By: BYK <[email protected]>
Take the Actor from the instruction that starts a Turn, as live recording does, so a person steering a system Turn does not move it to a person segment. Skip a stored call with no timestamp instead of reading it as 0. Co-Authored-By: BYK <[email protected]>
f2b681a to
2834180
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 2834180. Configure here.
… canceled batches Check which segment lists are empty when the run starts, so live gaps that arrive during the scan do not stop the fill. Skip a batch that the statement timeout cancels instead of ending the scan, and do not complete a run that read no history.
|
I'd keep it. After the first run completes, the key is |
…3110) Follow-up to #2944, from the [review question there](#2944 (comment)). A completed backfill set `junior:cache_warming:backfill:work_state` to `done` with no TTL. If warming stays off for more than 30 days, the gap lists expire but the key stays, so the backfill never fills them again. The backfill now uses `junior:cache_warming:backfill:work_state:v2`, and its `done` gets the gap list TTL (`GLOBAL_GAPS_TTL_MS`). The old key is ignored: a heartbeat returns when the claim fails, so an old `done` with no TTL would never be replaced. Existing installs run the backfill once more after deploy, and that run fills only segments that are empty. A fresh install and a long-lived install both check for empty segment lists again about once a month. Between checks, a heartbeat still makes only one state call. A rerun fills only lists that are empty when it starts, so lists with live gaps do not change. The existing backfill test now sets the old key to `done` with no TTL, and checks that the backfill still runs and that its new key is there just before the TTL and gone at the TTL. Without the fix, the test fails. The docs bullet in `config-and-env.md` now says the fill repeats after 30 days. `tsc --noEmit` and the full `pnpm lint` pass. The backfill, heartbeat, and heartbeat-timers tests pass. <!-- junior-request-attribution:start --> via @BYK. <!-- junior-request-attribution:end --> <!-- junior-session-footer:start --> <!-- junior-conversation-id:agent%3A95e8c282792eaed301a34c0abeedcaae --> -- [View Session](https://junior-prod.sentry.dev/conversations/agent%3A95e8c282792eaed301a34c0abeedcaae) [[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A95e8c282792eaed301a34c0abeedcaae/?project=4510944073809921) <!-- junior-session-footer:end --> --------- Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com> Co-authored-by: BYK <[email protected]>

Stacked on #2923 (stack: #2485 → #2923 → #2944). Idle warming sends no warmups for a
gapSegmentuntil that segment has recorded gaps, so each install starts from zero after deploy. This fills the empty segment lists once from stored Conversation history, so warming can start right away.What it reads
backfill.tsreplays the stored model calls of Conversations that had activity in the last 30 days (GLOBAL_GAPS_TTL_MS) and applies the live recording rules:timestampof each storedassistant_message. Pi sets it when the model request starts, so it is the same clock that the recorder'sstartedAtMsuses. A gap runs from the request that ended a Turn (stopReason: "stop") to the next recorded request. Error and aborted requests are skipped, the same asrecord()skips them.NO_REPLY_GAP_MS. An idle period that is still in its horizon records nothing.gapSegment(userActor, endedTurns).userActorcomes from the Actor of the Turn's instructionuser_message(it has auserId). Task Conversations always count as not a person, the same asagent/index.ts. The Turn count resets afterCONVERSATION_STATE_TTL_MSwith no call, the same as the expiring Conversation state. Earlier Turns of the Conversation count, including Turns before the window.usagehas the cached prefix size (cacheRead + cacheWrite), so periods belowMIN_WARMUP_PREFIX_TOKENSare skipped. The model, cache retention, and snapshot size are not checked. That is an assumption: reply timing does not depend on them.Each segment list that is empty when the run starts gets its newest gaps, at most half of
GLOBAL_GAPS_MAX. State lists have no atomic replace, so live recording can append while the backfill writes. The free half keeps those appends from trimming live gaps. A segment list that already has gaps is not changed. Per-Conversation gap lists are not filled.Scan budget
The heartbeat gives no deadline, so a run scans for at most 60 seconds (monotonic clock). It reads Conversations newest
last_activity_atfirst, 10 per query, and checks the budget before each query after the first. Small queries stay far under the SQL statement timeout. If the timeout still cancels one, the scan skips that batch and goes on. A run in which every query was canceled does not complete, and a heartbeat tries again after the claim expires. When the budget is used, it stops, writes the gaps that it has with the same rules, and marks the install as done. Some recent gaps are better than none, and a run that could never finish would otherwise try again each hour. The completion log has the number of Conversations scanned and whether the run stopped on the budget, and how many batches were canceled.Trigger: the heartbeat
runHeartbeatcallsbackfillCacheWarmingGapslast, after plugin heartbeats, in its own try/catch. I picked it over a lazy call in the warmup decision because:runCacheWarmup.warmup.tsonly exportsGLOBAL_GAPS_MAX,GLOBAL_GAPS_TTL_MS,CONVERSATION_STATE_TTL_MS, andsegmentGapsKey.waitUntil) on every install.A
setIfNotExistsclaim with a 1 hour TTL lets only one run start. A completed run sets the key todonewith no TTL, so later heartbeats make one state call and return. A run that fails or is stopped before it completes leaves the claim to expire, and a heartbeat an hour later tries again. WithJUNIOR_CACHE_WARMING_ENABLED=falsenothing runs and nothing is claimed.Tests
tests/integration/cache-warming-backfill.test.tsruns person Turns through the real Conversation path (createAgent, with only the model stream faked so the test can set the cached prefix size), adds a Task child, and runs the realrunHeartbeat.pnpm lintandtsc --noEmitpass. The backfill, cache-warming unit, and heartbeat tests pass.via @BYK.
--
View Session [Sentry]