Repository navigation
feat(cache-warming): Warm the prompt cache while a tool runs - #2923
Merged
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
BYK
requested changes
Nov 8, 2026
BYK
left a comment
Member
There was a problem hiding this comment.
For some reason I feel like there can be more code reuse between this PR and its parent.
BYK
marked this pull request as ready for review
November 8, 2026 19:50
sentry-junior
Bot
force-pushed
the
feat/cache-warming-tool-wait
branch
from
November 8, 2026 20:00
cdc138c to
f91fb5e
Compare
sentry-junior
Bot
force-pushed
the
feat/cache-warming-tool-wait
branch
from
November 8, 2026 20:22
db4dfd6 to
843c1c2
Compare
sentry-junior
Bot
force-pushed
the
feat/cache-warming-tool-wait
branch
from
November 8, 2026 20:26
843c1c2 to
c67c902
Compare
sentry-junior
Bot
force-pushed
the
feat/cache-warming-tool-wait
branch
from
November 8, 2026 20:34
c67c902 to
4174c91
Compare
sentry-junior
Bot
force-pushed
the
feat/cache-warming-tool-wait
branch
from
November 8, 2026 20:39
4174c91 to
4577eec
Compare
sentry-junior
Bot
force-pushed
the
feat/cache-warming-tool-wait
branch
from
November 8, 2026 20:48
4577eec to
a121496
Compare
sentry-junior
Bot
force-pushed
the
feat/cache-warming-tool-wait
branch
from
November 9, 2026 09:32
a121496 to
64c09cb
Compare
BYK
approved these changes
Nov 9, 2026
sentry-junior
Bot
force-pushed
the
feat/cache-warming-tool-wait
branch
from
November 9, 2026 09:49
64c09cb to
b405346
Compare
sentry-junior
Bot
force-pushed
the
feat/cache-warming-tool-wait
branch
from
November 9, 2026 10:09
b405346 to
179574a
Compare
sentry-junior
Bot
force-pushed
the
feat/cache-warming-tool-wait
branch
from
November 9, 2026 10:53
179574a to
a57054b
Compare
sentry-junior
Bot
force-pushed
the
feat/cache-warming-tool-wait
branch
from
November 9, 2026 11:23
a57054b to
b0d390f
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 985b112. Configure here.
sentry-junior
Bot
force-pushed
the
feat/cache-warming-tool-wait
branch
from
November 9, 2026 12:07
6b4a7ad to
0f59429
Compare
sentry-junior
Bot
force-pushed
the
feat/cache-warming-tool-wait
branch
from
November 9, 2026 12:09
0f59429 to
3ae02b7
Compare
A model request that ends with tool calls writes the full history again when its tools run longer than the cache TTL. The running agent now warms the cache with a timer while the tools run. The warmup request, miss check, eligibility, and breaker move to request.ts, so idle and tool wait warming share them. Tool wait warming uses the same cost model as idle warming: recorded tool waits give the chance that the next request comes in the next TTL, and a warmup is sent only while that chance pays for it. Co-Authored-By: BYK <[email protected]>
A long request can end after its cache expired. Then the tool wait timer fired at once and its warmup counted as a miss for the shared breaker. Both warmers now use one late-warmup check from request.ts. Co-Authored-By: BYK <[email protected]>
Co-Authored-By: BYK <[email protected]>
Co-Authored-By: BYK <[email protected]>
A run that ends during a tool never sees the tool return. Record that wait as NO_REPLY_GAP_MS, so the return chance does not count only the tools that returned. Co-Authored-By: BYK <[email protected]>
A run can end after its tools returned but before the next request, for example on a slice yield. Mark the tools as returned at turn end, and record that wait instead of a no-return gap. Co-Authored-By: BYK <[email protected]>
sentry-junior
Bot
force-pushed
the
feat/cache-warming-tool-wait
branch
from
November 9, 2026 12:21
3ae02b7 to
2d0bdeb
Compare
BYK
approved these changes
Nov 9, 2026
BYK
added a commit
that referenced
this pull request
Nov 9, 2026
Stacked on #2923 (stack: #2485 → #2923 → #2944). Idle warming sends no warmups for a `gapSegment` until 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.ts` replays the stored model calls of Conversations that had activity in the last 30 days (`GLOBAL_GAPS_TTL_MS`) and applies the live recording rules: - **Times:** the `timestamp` of each stored `assistant_message`. Pi sets it when the model request starts, so it is the same clock that the recorder's `startedAtMs` uses. 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 as `record()` skips them. - **No reply:** a gap of 55 minutes or more, or an idle period with no later call that is older than the horizon, records `NO_REPLY_GAP_MS`. An idle period that is still in its horizon records nothing. - **Segment:** `gapSegment(userActor, endedTurns)`. `userActor` comes from the Actor of the Turn's instruction `user_message` (it has a `userId`). Task Conversations always count as not a person, the same as `agent/index.ts`. The Turn count resets after `CONVERSATION_STATE_TTL_MS` with no call, the same as the expiring Conversation state. Earlier Turns of the Conversation count, including Turns before the window. - **Eligibility:** live recording records gaps only for idle periods that it could warm. The stored `usage` has the cached prefix size (`cacheRead + cacheWrite`), so periods below `MIN_WARMUP_PREFIX_TOKENS` are skipped. The model, cache retention, and snapshot size are not checked. That is an assumption: reply timing does not depend on them. - **Forks:** a fork copies its source rows with their old times, so rows from before the Conversation was created are skipped. 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_at` first, 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 `runHeartbeat` calls `backfillCacheWarmingGaps` last, after plugin heartbeats, in its own try/catch. I picked it over a lazy call in the warmup decision because: - It runs within about a minute of deploy, before the first warmup decision. A lazy call would run inside a warmup message that has seconds before its cache expires. A long first scan there would make that warmup late or make it miss. - It needs no change in `runCacheWarmup`. `warmup.ts` only exports `GLOBAL_GAPS_MAX`, `GLOBAL_GAPS_TTL_MS`, `CONVERSATION_STATE_TTL_MS`, and `segmentGapsKey`. - It already runs in background work (`waitUntil`) on every install. A `setIfNotExists` claim with a 1 hour TTL lets only one run start. A completed run sets the key to `done` with 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. With `JUNIOR_CACHE_WARMING_ENABLED=false` nothing runs and nothing is claimed. ## Tests `tests/integration/cache-warming-backfill.test.ts` runs 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 real `runHeartbeat`. - The first case checks the reply gap, the no-reply gaps, the prefix filter, the Task segment, that a non-empty segment is not changed, and that a second heartbeat does nothing. - The second case uses a zero budget. The newer Conversations fill the first batch and are written. The older Task in the second batch is not scanned, and a later heartbeat does not scan it. `pnpm lint` and `tsc --noEmit` pass. The backfill, cache-warming unit, and heartbeat 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]>
23 tasks done
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Stacked on #2485.
Sometimes a tool runs longer than the 5 minute cache TTL. Then the next model request writes the whole history to the cache again. The running agent now sends the same 1-token warmup before the cache expires.
cache-warming/request.ts: the code that idle and tool-wait warming share moves here fromwarmup.ts: eligibility (supportsWarming,SHORT_TTL_PROVIDERS), timing (warmupTiming), the warmup request (sendWarmup: core-model call, 90% read check, Sentry warning on a miss, outcome count), and the global breaker (onebreaker:openkey read per warmup).cache-warming/tool-wait.ts: when a model request ends withtoolUseon an eligible model with a large prefix, an in-process timer runs until the warmup is due. The decision uses the same cost model as idle warming (decideWarmup). It warms only while P(next request in the next TTL) ≥ read / (write − read), with the model's prices. The gaps are recorded tool waits (junior:cache_warming:gaps:tool_wait). Only waits of 2 minutes or more are recorded, because shorter waits cannot change a decision. Until waits are recorded, no tool-wait warmups are sent.cacheWarming.stop()in the agent runfinally), a warmup misses, or the breaker is open. A warmup that is still in flight when warmups stop does not schedule another one.app.cache_warming.trigger(idleortool_wait).Live Gateway check: an earlier version of this branch ran end to end on Opus 5.5. The tool-wait timer sent a warmup before the TTL ended, and the request after a 7 minute "tool" read the whole prefix from the cache instead of writing it again. The cost-based decision and the shared request code came later, and they have no live check yet.
Tests
tests/unit/chat/cache-warming-tool-wait.test.ts: warms while recorded waits make a return likely and stops when they don't, sends no warmups without recorded waits or for a small prefix, stops when the run ends (also during a warmup), and records only waits of 2 minutes or more. A test that runs the agent with a slow tool would break the test-architecture rule.via @BYK.
--
View Session [Sentry]