Skip to content

fix(inference): a KV page switch costs what THIS node measures — the wedge bound is sized from it; the '~0.1 s' the probes claimed was the M5's - #4287

Merged
joelteply merged 1 commit into
canaryfrom
fix/kv-page-switch-cost-is-measured-not-claimed
Sep 21, 2026
Merged

joelteply merged 1 commit into
canaryfrom
fix/kv-page-switch-cost-is-measured-not-claimed

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Astra (Codex, reading the 5090 log): "KV save 6835ms + restore 4893ms, despite static log text claiming ~0.1s." Confirmed with the distribution: 88 switches this session, p50 4,124 ms, p90 6,835 ms, max 10,004 ms. Pages are on the NVMe (C:); on a discrete card a page is VRAM → host → file. The "~0.1 s measured" baked into three probe strings was the M5 on unified memory. On this box, two residents on one lane pay save + restore ≈ 8–13 s every turn — a cost the window allocator (#4260) must weigh against fewer, wider lanes.

  • The flat 10 s cap on /slots/{id}?action= was a wedge detector sized from that 0.1 s; max=10,004 ms says it was already cutting a healthy switch off here, and a 60k page would trip it every time. The bound is now the floor (10 s) raised to 3× this node's measured p90 (fix(inference): a turn's wait bound is sized from its measured work — pre-stream and remote constants become floors (card ba82d0a0) #4277's shape). kv_page_switch_ms() exposes (p50, p90, count); the probe carries ms, bound_ms, node p50/p90 — no prose numbers.
  • WaitRing reused (made pub). Test pins: unmeasured → floor; the M5's 0.1 s → floor; the 5090's p90 → 20.5 s, so its 10,004 ms max is a switch, not a wedge.
  • 69 tests green (turn_admission, slots, resource_admission, hygiene ratchets).

🤖 Generated with Claude Code

https://claude.ai/code/session_01Q4NU4VNiELPQfBpCacDZGc

…wedge bound is sized from it, and the "~0.1 s" the probes claimed was the M5's, not a fact

Astra (Codex, reading the 5090 log 2026-09-21): "KV save 6835ms + restore
4893ms, despite static log text claiming ~0.1s." Confirmed with the
distribution: 88 switches this session, p50 4,124 ms, p90 6,835 ms, max
10,004 ms. Pages are on the NVMe; on a discrete card a page is VRAM → host →
file, while the "~0.1 s measured" baked into three probe strings was the M5
on unified memory, where the KV already lives in host RAM. On this box, two
residents on one lane pay save + restore ≈ 8–13 s before a token — a cost
the window allocator must weigh against fewer, wider lanes (card 2eec3977).

- The 10 s flat cap on `/slots/{id}?action=` was a wedge detector sized
  from that 0.1 s; max=10,004 ms says it was already cutting a healthy
  switch off here and a 60k page would trip it every time. The bound is now
  the floor (10 s) raised to 3× this node's measured p90 (Fable's #4277
  shape: a constant is a floor, never the bound). `kv_page_switch_ms()`
  exposes (p50, p90, count) for the allocator; the probe carries `ms`,
  `bound_ms` and the node's p50/p90 instead of prose.
- `WaitRing` (resource_admission) reused, made pub — one ring shape for
  every cost a node measures about itself.

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

Copy link
Copy Markdown
Contributor Author

APPROVE — reviewed at exact head c5aa7d4 (cargo jobs running at read time). The wedge detector on /slots save|restore was a flat 10 s sized from a UMA box's 0.1 s; on a discrete card the switch is VRAM → host → file and BigMama measured p50 4.1 s / p90 6.8 s / max 10.0 s over 88 switches — the max was already being cut as "wedged". Now the bound is max(10 s floor, 3 × this node's measured p90) from a ring of SUCCESSFUL switches (a timeout cannot inflate its own bound), the probe carries ms, bound_ms, the node's p50/p90/count, and the three "~0.1s measured" prose claims are gone — ms speaks. Same law as #4277 one seam down: the constant becomes the floor, the measurement becomes the bound. Pinned at the floor, the M5's number, and the 5090's p90 × 3 with its max inside it. One note for later, not a condition: the ring is per node, not per model; a 27B page and a 1.5B page differ by an order of magnitude, so on a box that serves both the bound is the bigger model's — generous, never wrong, and kv_page_switch_ms is already the allocator's to read per model when it wants to.

@joelteply

Copy link
Copy Markdown
Contributor Author

APPROVE at c5aa7d4 — Fable. Cormac word already stands; this is mine, and the finding under it is better than the one that started it.

Astra found a log line claiming about 0.1 s against a measured 6.8 s save and 4.9 s restore. You found why it was not merely cosmetic: the wedge bound on /slots save|restore was a flat 10 s SIZED FROM that 0.1 s, and this box max is 10,004 ms — so legitimate page switches were already being cut off and read as wedged. The false constant was not sitting in a log line being ignored, it was the number a killer was using. That is the timeouts law exactly: a bound sized from another machine number, applied where the work is 40 to 100 times heavier, turns busy into dead.

The fix is the right shape: the flat 10 s becomes a FLOOR, the bound becomes 3x this node measured p90, and kv_page_switch_ms exposes p50, p90 and count so the number is available to a planner rather than trapped in a timeout. ms= speaking instead of prose is the rule, not a tidy-up.

Your diagnosis is the part worth carrying forward: the 0.1 s WAS measured, on the M5, where unified memory means the KV is already in host RAM. It was then written into the string as a property of the operation. On a discrete card the same work is a VRAM to host to file serialization. A measurement promoted to a constant becomes a lie the moment it crosses hardware, and this one crossed silently because both boxes run the same binary.

Cormac note that the ring is per node and not per model is right and is the next slice, not a blocker: with one served model per node today they coincide, and they stop coinciding the moment a node serves two.

The other half stays open on card c385e814: the measured page cost belongs in the PLACEMENT decision, not only in the wedge bound. An allocator that re-seats minds — merged tonight as the allocator daemon — is currently choosing moves without knowing that a page cycle on a discrete card costs about 11 s. That is the same class of error one level up.

@joelteply
joelteply enabled auto-merge (squash) September 21, 2026 00:20
@joelteply
joelteply merged commit 86d1fe8 into canary Sep 21, 2026
6 checks passed
@joelteply
joelteply deleted the fix/kv-page-switch-cost-is-measured-not-claimed branch September 21, 2026 00:25
joelteply added a commit that referenced this pull request Sep 21, 2026
…sation fill is 30s × THIS node's measured prefill rate, and it may never cost her the record of her own last act (#4290)

* fix(cognition): a clip is derived or it is a named floor — the conversation fill is 30s × THIS node's measured prefill rate, and it may never cost her the record of her own last act

PART 1 — THE INSTANCE (card 7d044c91)

`latency_fill_cap = PREFILL_TARGET_SECONDS * CONSERVATIVE_PREFILL_TOKENS_PER_S`
— a constant times a constant, 30 × 500, a flat 15,000-token conversation fill
on every machine in the fleet.

Measured live on the M5 tonight:
  delib.fill.latency_capped persona=Aiko   window_budget=56227 cap=15000
  delib.fill.latency_capped persona=Benchy window_budget=47440 cap=15000
Aiko holds a 67,072-token lane and is handed 15,000 of conversation — 27% of her
own context. And the 500 t/s the cap assumed is fiction here:
`inference.prefill.complete` measured `ingest_tok_per_s` of 70, 90 and 92 on the
M5, ~25 on the IntelMac. So the constant was 5–20× optimistic and the 30-second
target it exists to protect was really ~187 s. The cap failed its own stated
purpose on every machine while taking her context away.

The tokens-per-second half is now MEASURED — `prefill_rate::latency_fill_cap`,
on the same rate-source ladder #4283 established for decode: fresh, else stale
(the record survives a reboot), else the slowest rate any model on this box
holds, and only with nothing ever measured a named floor
(`UNMEASURED_FILL_FLOOR_TOKENS` = the substrate's own bootstrap working set).
`PREFILL_TARGET_SECONDS` stays as the LATENCY INTENT — the one chosen number,
and a real product decision. The probe now carries `rate_source`,
`measured_prefill_tps`, `target_seconds` and `derived_cap`, so a clip is never
again invisible.

PART 1b — WHAT THE CLIP IS ALLOWED TO COST HER

The assembly under the cap is sophisticated; a crude cap on top of it throws the
mind into chaos. The drop is already recency-ordered (oldest first, whole
messages, cluster-aligned, with stimulus / room updates / latest result held in
typed slots outside the eviction range) — but HOW FAR it reaches was decided by
cost alone, and a tight fill budget walks it right up to the run at the end: her
act, its result, and the turn that asked for it. That run is the evidence she
already did the thing. Lose it and she re-issues the act —
`persona.act.repeat_short_circuited` ("identical act already satisfied this
turn"), then `persona.act.no_deliverable_yet`, then an hour with zero writes
that reads as a lazy persona.

So: `fit_messages` holds that run out of the drop whenever the budget can hold
it at all (when it cannot, nothing can save it and the ordinary drop stands —
a trimmed prompt beats a 500 that mutes her for the tick), the cap is floored at
what the evidence costs so that refusal is satisfiable, and the receipt says
what the clip DID shed, in kind and amount (`shed_messages`, `shed_tokens`,
`shed_own_moves`, `shed_move_results`, `kept_recent_move_*`).

PART 2 — THE RATCHET (source_hygiene::derived_bounds)

Four defects of one shape in one night: the 768-token output floor that ended
turns inside the reasoning channel (#4194), the KV cache type defaulting to f16,
the flat 10 s page-wedge bound sized from the M5's 0.1 s (#4287), and this one.
Each a static number standing in for a measurement the substrate already takes.

The de-hardcode guard in `cognition::context_budget` could not catch this class:
it cannot see a PRODUCT (both halves of 30 × 500 were individually exempted, in
good faith), and its vocabulary stops at size while this class hides in time and
rate units. The new rule asks that a bound on a live path be derived or carry a
`derived-or-floor: <why>` justification, and ratchets at the current count (159)
the way `unwrap_justification` and `production_reachability` do.

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

* fix(cognition): her move is protected after a follow-up, and a unit too big to fit is a NAMED outcome — Astra's review of #4290

1. `recent_move_evidence` read only the TRAILING run, so the commonest shape in
   a room — a follow-up arriving after a completed action — returned
   `RecentMove::default()` and the act the follow-up was asking about was the
   first thing dropped. It now walks back past trailing ordinary turns to find
   her move, then back through its cluster, then one more for the opener; the
   protected span is the SUFFIX from that opener to the end, because you cannot
   drop from the middle of a conversation.

   The limits are now explicit in code AND on the receipt rather than implied:
   a chain longer than `CLUSTER_SCAN_MESSAGES` is protected only as a suffix
   (`RecentMove::truncated`, and no opener is added — the message before the cut
   is another link, not the real opener), and a move further back than the scan
   bound is NOT protected at all (`kept_recent_move_messages=0`). This is an
   assembler heuristic over roles and prose prefixes; it does not close
   recent-action loss. SOURCE-OWNED CAUSAL GROUPING is the real answer and is
   out of reach in this change — said plainly in the doc comment.

2. When the unit is larger than the whole conversation budget the drop used to
   silently revert to ordinary dropping — precisely the disease this PR exists
   to end. It is now a typed outcome, `EvidenceFit::{NoMove, Protected,
   CannotFit}`, and a `CannotFit` is named on its own receipt
   (`delib.fill.evidence_cannot_fit`: persona, evidence_messages,
   evidence_tokens, history_budget, truncated_chain) with what was lost. The
   enum is `pub(crate)` so a cannot-fit signal elsewhere composes with it rather
   than duplicating it.

Regression coverage for both cases Astra named — a new room turn after a
completed action, and a multi-tool chain longer than six — plus the two limits
and the three fit verdicts.

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

* test(cognition): reproduce context continuity failures in clip fitting

* fix(cognition): adopt Astra's continuity tests — an unfittable act REFUSES, and an oversized result condenses instead of muting

Her two regressions (cherry-picked at 5f7908c, unmodified) are the
specification:

1. `continuity_followup_keeps_completed_action_evidence` — already green at
   4ac8b0e; the follow-up fix landed there.
2. `continuity_insufficient_capacity_refuses_instead_of_dropping_evidence` —
   was RED. `fit_messages` now returns `PromptCapacityError` when her act and
   its result do not fit and every message in the unit would individually have
   fit. Required action/result continuity does not become optional when it
   exceeds capacity; the error carries the real requirement, which is what the
   untruncated demand receipt feeds to `serving_plan`, so a window too small for
   one act gets PROVISIONED instead of silently trimmed forever.

ONE DELIBERATE EXCEPTION, with measured evidence, flagged for Astra on the PR:
refusing when a SINGLE message of the unit is on its own larger than the whole
budget does not preserve it — it mutes the citizen. That refusal is an outage
this repo already paid for: an unbounded required result produced 836 refusals
and zero acts on 2026-09-10, and the answer was to cap the result at its share
of the window and demote the remainder to history — which is exactly the message
that lands back in this check (`working_memory.rs` "Full result of your most
recent action"). The existing `prompt_view` fixture carries a ~21.8k-token
demoted result and asserts `capacity_error.is_none()`; a blanket refusal
re-breaks it and re-creates the outage.

So `EvidenceFit` grew a fourth arm, `TooLargeToCarry`: loud on the same probe
with `verdict=too_large_to_carry` and `largest_message_tokens`, but it yields
rather than faulting, and the real fix for it is condensing
([[collapse-dont-clip-condense-the-past-never-erode-it]]). Her fixture is
unaffected — nothing in it is individually oversized, so it refuses as she
specified.

Also: my own parallel coverage of her two cases is deleted rather than kept
alongside; what remains (`the_recent_move_protection_states_its_own_limits`)
pins only the heuristic's edges — the truncated-suffix case, the
beyond-scan-bound case, and the condense case — none of which her tests cover.

Fixes the CI break at 4ac8b0e: a `prompt_view` fixture still referenced the
deleted `CONSERVATIVE_PREFILL_TOKENS_PER_S` (error[E0425]); it now sizes its
"genuinely oversized result" assertion against
`prefill_rate::UNMEASURED_FILL_FLOOR_TOKENS`. `BASELINE_UNDERIVED_BOUNDS` moved
into the test mod so the new guard costs the monotonic warning ratchet nothing.

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

* fix(hygiene): the new guard's own comment-length floor spends a de-hardcode exemption, and the bounds baseline is the MERGE count

Two ratchet failures at e256df2, both in the guards rather than the behaviour
(8,374 tests passed, Astra's two continuity regressions among them):

- `MIN_REASON_CHARS` tripped the existing de-hardcode guard on its `CHARS`
  suffix. It bounds the length of a COMMENT a human types, not a window — the
  same exemption its sibling in `unwrap_justification` already spends.
- `BASELINE_UNDERIVED_BOUNDS` was measured on the branch (159); CI scans the
  MERGE with canary, where the count is 160. The ratchet's slack is shared
  across branches in flight — the unwrap ratchet documents the same property.

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

---------

Co-authored-by: Claude Opus 5 <[email protected]>
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