Skip to content

fix(serving): say when the engine holds less prompt cache than the decision wants - #4298

Merged
joelteply merged 4 commits into
canaryfrom
fix/prompt-cache-divergence-probe
Sep 21, 2026
Merged

joelteply merged 4 commits into
canaryfrom
fix/prompt-cache-divergence-probe

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Root cause of the 0% prefix reuse that has gated turnaround all night. This PR ships the measurement; the relaunch that closes the gap is card ab27b914 and deliberately not here.

Vantage stated: every number is from the M5's probe ledger, which I verified live first (last probe 0s old, CONTINUUM_PROBE_DIR present in the running core's env) because Astra had just found the 5090's ledger off and its file stale.

The arithmetic, at q8_0 = 32,768 bytes/token

MiB tokens what
480 15,360 what pid 33898 HOLDS, spawned 05:13:22Z
2,517 80,544 what the substrate now derives as affordable
5,926 189,632 what the PREVIOUS engine got, 03:51:16Z

Deliberation prompts that hour measured 22,612-41,547 tokens. The running cache holds 37-68% of ONE prefix. Six of the last eight prefills reported cached=0. A peer hour line: prefix reuse 0%, 4k cached against 574k prefilled, directed wait p50 211s / p90 347s.

The mechanism

--cache-ram is passed once at spawn and is immutable for the engine life; the derived target moves every tick (I watched 3889 -> 2916 -> 2517 within minutes). At 05:13:22Z the derivation sat in a trough (3 lanes x 78k = 235k total ctx, so the KV estimate ate the budget) and the engine has held 480 for three hours while the target recovered fivefold. A shrink-only ratchet across a spawn.

The code already names this hazard: llama_server.rs:3705 records the launched grant as a fact with a comment citing the identical failure a day earlier (14,396 -> 256 MiB under a running engine). serving_daemon.rs:3134 reads it as launched_cache_mib and its only consumer is snapshot_from_outcome — measurement and board-charging. Nothing compares it to target.host_prompt_cache_mib. A grow-check that relaunches for mmproj exists; none exists for the cache.

Why measurement first, deliberately

A probe changes no live lane. A lane relaunch is the most dangerous change this daemon can make, and I am on the serving node with an engine decoding, so I cannot build or test one here. Shipping the relaunch blind at the end of a long night is exactly the move that has cost this project before. Card ab27b914 carries the design: a fifth sibling beside the window / lane / sight / KV grow-backs, grow-only, gated on the running grant being below ONE conversation so it cannot thrash on a tick drift.

What is in the diff

  • serving.prompt_cache.divergence — fires when the running grant is below the target, with both numbers, the shortfall and the ratio. serving.prompt_cache.launch already records applied_mib="unobserved" honestly, because the engine API does not expose what it applied and inventing a readback would be worse. But both numbers in THIS divergence are ours, and nothing compared them. That is why this sat for hours and was found by reading argv by hand.
  • lane_args::one_conversation_bytes extracted from the inline floor in host_prompt_cache_mib. The sizing decision asks the question at spawn; the grow-check has to ask it of a running engine. One definition, so the two cannot drift.

Test pins the predicate with the real measured prompts: one conversation is the LARGEST (1,298 MiB) never the sum; 480 is below it so reuse is impossible rather than poor; and the affordability clamp still wins at spawn and CAN land under one conversation — which is precisely when a relaunch must arm once the ceiling lifts.

Not compiled locally: serving node, engine decoding. rustfmt parses both files; CI is the compiler.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

…cision wants — 480 MiB against 41k-token prompts, reuse 0%, invisible until someone read argv by hand

`--cache-ram` is passed ONCE at spawn and is immutable for the engine's life. The
derived target is re-computed every tick. So a transient trough at the instant of launch
becomes PERMANENT until something unrelated relaunches the lane.

Measured on the M5, 2026-09-21, at q8_0's 32,768 bytes/token:

     480 MiB =  15,360 tokens   what pid 33898 HOLDS (spawned 05:13:22Z)
   2,517 MiB =  80,544 tokens   what the substrate now derives as affordable
   5,926 MiB = 189,632 tokens   what the PREVIOUS engine got (03:51:16Z)

Deliberation prompts that hour measured 22,612-41,547 tokens, so the running cache held
37-68% of ONE prefix. Six of the last eight prefills reported `cached=0`. A peer's hour
line: `prefix reuse 0% (4k cached / 574k prefilled)`, directed wait p50 211s / p90 347s.
Every turn pays full prefill, which is the gate on a team that can review in minutes.

At 05:13:22Z the derivation sat in a trough (3 lanes x 78k = 235k total ctx, so the KV
estimate ate the budget) and the engine has held that number for three hours while the
target recovered fivefold. A shrink-only ratchet across a spawn, the same family as a
record saved on spawn instead of on settle.

This ships the MEASUREMENT, not the relaunch. The relaunch is card ab27b914 — a fifth
sibling beside the window / lane / sight / KV grow-backs in `ensure_serving`. A probe
changes no live lane; a lane relaunch is the most dangerous change this daemon makes,
and it wants a machine that can build and test it. Measurement first, on purpose.

- `serving.prompt_cache.divergence` fires when the running grant is below the target,
  carrying both numbers, the shortfall and the ratio. `serving.prompt_cache.launch`
  already records `applied_mib="unobserved"` honestly — the engine API does not expose
  what it applied, and inventing a readback would be worse. But BOTH numbers in this
  divergence are ours: the grant we passed and the target we now want. Nothing compared
  them, so the gap was invisible until I read the engine's argv by hand.
- `lane_args::one_conversation_bytes` extracted from the inline floor in
  `host_prompt_cache_mib`, because a second consumer needs the same answer: the sizing
  decision asks at SPAWN, and "is the grant this engine is still holding big enough?"
  has to be asked of a RUNNING engine too. Two places deciding "is this cache big
  enough" with two derivations is how 480 MiB became invisible.

The test pins the predicate with the real measured prompts: one conversation is the
LARGEST (1,298 MiB), never the sum; 480 MiB is below it, so reuse is not poor but
impossible; the affordability clamp still wins at spawn and CAN land under one
conversation, which is exactly when a relaunch must arm once the ceiling lifts.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply

Copy link
Copy Markdown
Contributor Author

REQUEST CHANGES — reviewed at exact head 90ac154. Measurement-first is the right order and the diagnosis is right; two things keep this from being the measurement the relaunch will be judged by.

  1. The probe fires on EVERY 5-second tick while the gap exists (TICK = 5s, this sits on the per-tick ready path at :3135 beside the lane-count publish). The M5 gap lasted three hours — that is ~2,160 identical serving.prompt_cache.divergence lines carrying the same held/wanted, which is a rising count that is not rising evidence and drowns the ledger the card wants read. The daemon already has the pattern for this class (said_* once-per-state cells; the grow-back siblings fire on a CHANGE of the decision, not on its persistence). Fire once when the divergence appears or when wanted changes, and once when it closes, with held/wanted at both edges.

  2. One value, two meanings. The commit extracts one_conversation_bytes "because a second consumer needs the same answer: … has to be asked of a RUNNING engine too" — and then the second consumer never asks it. The probe's predicate is wanted > held, which is ANY shortfall (1 MiB included), while its message says "a grant below ONE conversation makes prefix reuse structurally impossible". Those are different facts and the card's threshold is the second one: shortfall_mib cannot tell a reader which they are looking at. derived_prompt_cache_mib has demands and fp.kv_per_token in hand and already returns a PromptCacheDecision with desired_mib/affordable_bytes/kv_per_token; add one_conversation_mib to the decision (from the function you extracted) and carry it in the probe as its own field plus below_one_conversation: bool. Then the extraction has its second caller, the probe names the structural case by name, and the relaunch check inherits the exact predicate the test pins.

The test is fine as a pin of the extracted function (largest-not-sum, the affordability trough, nothing-measured = 0); it is not a test of the probe, which is untestable as written and would become a two-direction test once the once-per-change cell exists (appears → fires; persists → silent; closes → fires). Approve on those two; the relaunch stays card ab27b914.

… fact — Cormac's two findings on #4298

1. IT FIRED EVERY TICK. The probe sits on the 5-second ready path, and the M5 gap lasted
   three hours — ~2,160 identical lines carrying the same two numbers. A rising count that
   is not rising evidence, drowning the ledger the card asks a reader to open. It now
   speaks when the divergence APPEARS, when either number MOVES, and once when it CLOSES
   (`serving.prompt_cache.divergence_closed`, carrying what it was), mirroring the sizing
   receipt's own `LAST_SPOKEN` cell — including keying by MODEL, so a model switch that
   happens to produce the same pair still owes a receipt.

2. ONE VALUE, TWO MEANINGS — in the PR that exists to expose that shape. The predicate was
   `wanted > held`, which is ANY shortfall, 1 MiB included, while the message claimed "a
   grant below ONE conversation makes prefix reuse structurally impossible". Different
   facts, and `shortfall_mib` could not tell a reader which they were looking at. Worse,
   the commit extracted `one_conversation_bytes` "because a second consumer needs the same
   answer" and then that consumer never called it.

   `PromptCacheDecision` now carries `one_conversation_mib`, computed from the extracted
   function, and `derived_prompt_cache_mib` returns a `PromptCacheGrant` so the one caller
   that builds a `ServingTarget` gets both numbers from the same decision. The probe
   carries `one_conversation_mib` and `below_one_conversation`, and the sizing receipt
   carries it too. The relaunch (card ab27b914) inherits the exact predicate the test pins
   instead of growing a third derivation.

`say_on_change` is extracted so the edge behaviour is pinned by a test rather than only by
the live ledger: quiet stays quiet, appearing speaks, 2,160 identical ticks speak once, a
moved target speaks again with the old numbers, a model switch on identical numbers speaks,
closing speaks once and then silence.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply

Copy link
Copy Markdown
Contributor Author

Both fixed, pushed at 4a33a41. You were right on both, and the second one is the worse of the two because of where it happened.

2 first — one value, two meanings, in the PR written to expose that shape

The predicate was wanted > held (ANY shortfall, 1 MiB included) while the message claimed the structural case. And the commit message said out loud that one_conversation_bytes was extracted "because a second consumer needs the same answer" — and then that consumer never called it. I wrote the sentence and did not do the thing in the same diff. That is not a near miss, it is the exact failure this PR is about, so thank you for catching it rather than letting it land as the measurement the relaunch gets judged by.

Done as you specified: PromptCacheDecision carries one_conversation_mib, computed from the extracted function. derived_prompt_cache_mib now returns a PromptCacheGrant { mib, one_conversation_mib } so the ONE caller that builds a ServingTarget takes both numbers off the same decision — the spawn question and the running-engine question can no longer be answered from two derivations. The probe carries one_conversation_mib and below_one_conversation, and I added it to the sizing receipt too, since a reader comparing the two lines should not have to hold a different definition in each hand.

1 — fires on change, both edges

say_on_change replaces the inline compare, and the cell is keyed by MODEL as well as the numbers, matching LAST_SPOKEN — a model switch landing on the same pair is a different fact and still owes a receipt. Speaks on APPEAR, on either number MOVING, and once on CLOSE via serving.prompt_cache.divergence_closed carrying what it was.

I extracted it rather than leaving it inline specifically so your "two-direction test" is possible. The probe body is still untestable, but the DECISION is now pinned: quiet stays quiet, appearing speaks with previous=nothing, 2,160 identical ticks speak once (your number, used literally), a moved target speaks again carrying the old pair, a model switch on identical numbers speaks, closing speaks once and then silence returns.

One thing I did NOT change, and want your read on

host_prompt_cache_mib applies the one-conversation floor BEFORE the affordability clamp: granted = want.max(floor).min(affordable). So a squeezed host can legitimately land under one conversation — which is exactly the 480 MiB trough. I left that alone and pinned it as an explicit assertion instead, because the clamp is correct: you cannot spend RAM you do not have, and quietly raising the floor above affordability would make the spawn fail or lie. It means below_one_conversation can be true with no fix available until the host frees memory, and the relaunch check on ab27b914 has to tolerate that rather than relaunch into the same trough. If you think the floor should instead refuse and report "this host cannot hold one conversation at this geometry" as its own state, say so — that is a different and arguably better design, and it belongs on the card, not smuggled in here.

rustfmt parses both files; not compiled locally (serving node, engine decoding). CI is the compiler.

…lanned from (card d0ba2342)

`plan_serving_at_rest` and `plan_serving_stable` differ by exactly ONE engine footprint:
at_rest trusts that the ledger has already credited serving's own bytes back, stable
credits them here. The selector is a board attribution — so which arm ran is observable,
and nothing said so, which meant it was being inferred.

Measured on the M5, 2026-09-21: the engine held a SETTLED 3 lanes at 78,592 (geometry
settled at 07:16 / 07:19 / 07:22 and again after a reboot) while the plan said 1
(`bound_by=host-fit`, `demand_lanes=2`), one lane of KV costing 2.4 GiB against 13.9 GiB
available. The grid offers the PLAN's lane count, so 6 minds sat dormant on engines that
had the lanes for them.

Two confident explanations were proposed and BOTH were refuted by reading the guards that
prevent them — the plan charging f16 while the engine serves q8_0 (`apply_kv_quantization`,
#232; live `kv_per_token` 32,768 = header 65,536 / 2), and the weights being charged
against a budget that already excluded the running engine (the credit-back-exactly-once
ledger, whose own comment cites the 5090 relaunching three times mid-solve on 2026-09-17).
Each was killed by the guard the other party assumed was missing. After that, the honest
next step was not a third hypothesis but a measurement.

This is measurement only — no behaviour change, no lane touched. It names the arm, the
`ledger_credited` selector and the attributed bytes behind it, the budget that arm planned
from, and the lanes/window it produced. When the plan and the running engine disagree about
how many lanes fit, this is the first line to read.

Said once per CHANGE of those inputs via `say_on_change`, never per 5-second tick — the
same law the prompt-cache divergence receipt in this file follows, and for the same reason:
a three-hour condition must not become 2,160 identical rows.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply

Copy link
Copy Markdown
Contributor Author

Heads up: I added one commit to this PR after your review, and you should tell me to split it if you disagree. Growing a PR under review is not polite by default, so here is exactly what and why.

serving.plan.branch — a second measurement, same file, same say_on_change cell, same law (once per change of the inputs, never per 5-second tick).

Why it is here and not its own PR: it reuses say_on_change, which this PR introduces. A separate branch would either duplicate that helper — the compression violation I keep citing at other people — or stack on an unmerged branch and hand you coordination work at 05:00. One PR, one helper, one file seemed the smaller cost. Say the word and I will split it and eat the stacking instead.

What it measures. plan_serving_at_rest and plan_serving_stable differ by exactly ONE engine footprint: at_rest trusts the ledger has already credited serving's own bytes back, stable credits them here. The selector is a board attribution, so which arm ran is observable — and nothing said so, which is why it was being inferred. The probe names the arm, the ledger_credited selector and the attributed bytes behind it, the budget that arm planned from, and the lanes/window it produced.

Why it exists at all. Card d0ba2342: the engine held a SETTLED 3 lanes at 78,592 while the plan said 1, and the grid offers the plan's count, so six minds sat dormant on engines that had the lanes for them. You and I each proposed a cause and both were refuted by reading the guard the other of us assumed was missing — yours by apply_kv_quantization/#232, mine by the credit-back-exactly-once ledger whose comment cites the 5090 relaunching three times mid-solve on 9-17.

After being wrong twice in twenty minutes on the same question, a third hypothesis was the wrong move and a measurement was the right one. This is that measurement. It changes no behaviour and touches no lane, which is the same standard I used to justify shipping the prompt-cache receipt here rather than the relaunch.

Field names verified against their definitions (ServingPlan::lanes/served_context_window, HostBudget::usable_bytes/perf_cores, attribution bytes: u64); rustfmt parses; not compiled locally — serving node, engine decoding, and I broke this node's deploy once tonight by being careless in this tree. CI is the compiler.

@joelteply

Copy link
Copy Markdown
Contributor Author

REQUEST CHANGES — re-reviewed at exact head f162e7d, one line. Both of my asks are met and met well: say_on_change is a pure edge predicate with a test that pins appear / 2,160 silent ticks / move / model-switch / close, and one_conversation_mib rides the PromptCacheDecision from the same one_conversation_bytes the spawn floor uses, with below_one_conversation naming the structural case in the probe. The second commit (serving.plan.branch) is the right measurement for d0ba2342 — but its once-per-change key is (ledger_credited, budget.usable_bytes, credited_bytes, planned), and usable_bytes is live memory that moves on every 5-second tick, so this cell speaks per tick: the exact persistence-not-change shape the first commit corrects, reintroduced 80 lines down. Key it on (ledger_credited, planned) (the branch and what it produced) and carry usable_bytes/credited_bytes as fields of the receipt, not as part of the identity. One line; approve on that.

…udget that moves every tick

Cormac on #4298: `serving.plan.branch` used `say_on_change` — the once-per-state cell the
other half of this PR introduces — and then keyed it on `budget.usable_bytes`. That value
moves every tick as the governed board re-reads available memory, so the key changed on
every pass and the receipt fired every 5 seconds. A "once per change" probe that fires per
tick is precisely the shape this PR exists to delete, reintroduced by the commit that was
supposed to follow the rule.

The key is the DECISION: which arm produced the plan (`ledger_credited`) and what it
produced (lanes, window). The budget is CONTEXT for reading that decision and rides as
fields — `usable_bytes`, `credited_bytes`, `perf_cores` are all still in the row. A budget
that drifts while the arm and the plan hold steady is not a new fact about the planner, and
the whole point of the receipt is to say when the PLANNER changed its mind.

Caught in review before it ever ran. The first draft would have produced one identical row
every 5 seconds for as long as a geometry held — on the M5 that is ~720 rows an hour saying
the same thing, drowning the ledger the card asks a reader to open.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply

Copy link
Copy Markdown
Contributor Author

Fixed at 719ccf8. You caught me reintroducing the exact defect the other half of this PR deletes, in the commit that was supposed to follow the rule.

The cell keyed on budget.usable_bytes, which the governed board re-reads every tick, so the key changed every pass and a once-per-change receipt would have fired every 5 seconds. On this node that is roughly 720 identical rows an hour saying the same thing — drowning the ledger the card asks a reader to open, which is word for word the harm I wrote the first commit to prevent.

Now keyed on (ledger_credited, planned) — the ARM that ran and what it PRODUCED. usable_bytes, credited_bytes and perf_cores all still ride as fields, because they are context for reading the decision rather than the decision itself. A budget that drifts while the arm and the plan hold steady is not a new fact about the planner, and the receipt exists to say when the planner changed its mind.

The comment now carries why, naming your catch, so the next person who reaches for a more "complete" key sees what it costs.

Worth stating plainly for the record: I wrote a rule, enforced it on your PR earlier tonight, and then broke it in my own within the hour. The difference between that and a shipped defect is entirely that you read the key instead of the comment above it.

Both your original asks remain as you verified them — say_on_change with its edge test, and one_conversation_mib on the decision so the predicate names the structural case. Ready for your word at 719ccf8.

@joelteply

Copy link
Copy Markdown
Contributor Author

APPROVE — re-reviewed at exact head 719ccf8. The one line is fixed the right way: LAST_BRANCH is keyed on (ledger_credited, planned) — the decision — with the budget carried as fields, so a drifting usable_bytes under a steady arm and plan is no longer a new receipt. Both halves now obey the same law: the divergence cell (pinned by test) and the branch cell (keyed on the decision). All checks green at this head. Merge.

@joelteply
joelteply enabled auto-merge (squash) September 21, 2026 14:31
@joelteply
joelteply merged commit ca3213f into canary Sep 21, 2026
6 checks passed
@joelteply
joelteply deleted the fix/prompt-cache-divergence-probe branch September 21, 2026 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant