Skip to content

Serving gives its host prompt cache back first when the authority reclaims memory (card 494c99f1) - #5969

Merged
joelteply merged 2 commits into
canaryfrom
fable/serving-gives-its-cache-back-first
Nov 13, 2026
Merged

joelteply merged 2 commits into
canaryfrom
fable/serving-gives-its-cache-back-first

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

What it serves

The four resident minds on the M5. That machine has 64 GB and serves Qwen3.6-35B-A3B to four persistent minds, and it is memory-starved: 60 MB free, 34 GB in the OS compressor, swap at 8.6 of 10 GB, and prefill at 92 tok/s against ~190. The engine was launched with a fixed host prompt cache (--cache-ram 9369 MiB) that never gives memory back. Fork PR #110 (9269444f6 on codex/s4-per-mind-integration) added a live route, POST /prompt-cache/limit, that resizes the cache on a running engine.

This is card 494c99f1 slice 2, the core side, and it advances line 3b (one live engine, its memory moved by us, no relaunch).

The change

  • Engine (continuum-engine, llama_server):
    • LlamaServerControl::set_prompt_cache_limit(NonZeroU64) -> Result<PromptCacheLimit, LlamaServerError>. The route's floor of 1 MiB is enforced by the NonZeroU64 type.
    • It returns a typed PromptCacheLimit { held_bytes, freed_bytes } receipt.
    • New variant LlamaServerError::PromptCacheRefused(PromptCacheRefusal) with three cases: Disabled (no host cache), RouteMissing (404/405: the engine predates 9269444f6), and Refused(word) (the engine named its own refusal).
    • The route is spoken in one new file, prompt_cache_limit.rs. It POSTs with the endpoint's client and PROBE_TIMEOUT, like its neighbours, and decodes the reply with resp.json::<LimitReply>() into a typed refusal enum. There is no Value walk.
    • No exhaustive match on LlamaServerError exists in the workspace. Every match site has a named arm or an error-forwarding arm, and none of the lifecycle paths (serve, idle, probes) can return the new variant.
  • Serving (continuum-serving):
    • ServingConsumer::with_prompt_cache(Arc<dyn PromptCacheControl>). The daemon wires it over its own LlamaServerControl (serving_daemon/consumer.rs, EnginePromptCache).
    • In reclaim: after the kind check, the pending-landed check and the active-model check, and before tier-down or unload. It applies only under Pressure and only when the host cache sits in serving's pool row. It then:
      1. lowers the limit by the target in whole MiB (rounded up, never below 1);
      2. returns Released when the freed bytes cover the target, Partial otherwise;
      3. confirms the receipt against the measured process drop and reports the smaller figure, saying so in detail.
    • A refusal, an unknown launch grant, or a cache at its floor falls through to the existing lever unchanged.
    • Every path emits a serving.reclaim.prompt_cache probe with the limit before and after, the target, the reported freed bytes and the measured drop.
    • Where the limit comes from:
      • The first limit is the launch grant on the serving snapshot (host_prompt_cache_mib). That is the spawn fact, or --cache-ram read off an adopted engine's argv. There is no guessed constant.
      • After that, the limit is whatever serving last set on that engine, keyed by EngineIncarnation (pid, start time, port).
      • Only a provably different engine starts again from its own grant. An engine the lever cannot tell apart is never raised by a reclaim.
    • The new lever lives in serving_consumer/prompt_cache.rs.
    • The touched files end clean on unwrap justification: three sites now carry their reason, and one unwrap_or_default is replaced by into_iter().collect().

Decisions

  • Pressure only. Shutdown and Rebalance skip the cache lever for the same reason they skip tier-down: both want the whole lease gone from this box.
  • Host-memory rows only. On a discrete GPU (the board's VRAM row), lowering a host-RAM cache frees nothing that was asked for, so that case goes straight to the heavy levers.

Tests

These extend the existing test module of serving_consumer, as a nested prompt_cache_first mod.

  • A reclaim lowers the cache first (9369 -> 9368 MiB) and reports the receipt's freed bytes. No pin, no suppress, nothing pending. The next ask lowers from the limit serving set.
  • A refused limit (RouteMissing) falls through to the tier-down.
  • A cache at its floor (1 MiB), or with an unknown grant (0), is never asked and falls through.
  • When the measured drop (3,000) is smaller than the receipt (9,000), the drop is reported as Partial.
  • current_limit: only a different engine starts again from its launch grant, and an unknown engine is never raised.
  • The two control fakes implement the new method as an engine without the route.

Nothing was compiled locally: the M5 is serving live minds, so CI is the compiler.

Live receipt to take

After the engine pin includes fork 9269444f6, record on the M5 before and after: prefill and decode tok/s, free memory, compressor size, swap used, and median act time at four residents. Also record the serving.reclaim.prompt_cache probes, showing limit before and after, reported against measured freed bytes.

Next slice (not here)

Raise the cache back toward its launch grant when pressure clears.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

…laims memory (card 494c99f1)

Under pressure, ServingConsumer::reclaim lowers the engine's host prompt-cache
limit by the request's target through the fork's live route
(POST /prompt-cache/limit, fork 9269444f6) before it tiers down or unloads.
The engine's receipt is confirmed against the measured process drop; a
refusal (no host cache, route missing, engine refusal), an unknown launch
grant, or a cache at its floor falls through to the heavy levers unchanged.

- engine: LlamaServerControl::set_prompt_cache_limit(NonZeroU64) with a typed
  PromptCacheLimit receipt and LlamaServerError::PromptCacheRefused(PromptCacheRefusal);
  the route is spoken in one file (prompt_cache_limit.rs), decoded straight
  into a typed reply.
- serving: PromptCacheLever (serving_consumer/prompt_cache.rs) keeps the limit
  serving last set per engine incarnation and never raises it on a reclaim;
  the daemon wires it over its LlamaServerControl.
- touched files end clean on unwrap justification.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
…w of #5969)

A limit far above the cache's contents freed nothing when lowered by the target
(limit 9,369 MiB holding 2 GB, asked for 4 GB: the limit walked down 4 GB per
ask, freed 0). The lever now keeps the last receipt's held bytes per engine
incarnation and plans from min(limit, held). On an engine whose contents are
not yet known, a first receipt that freed less than the target while the cache
still holds more than the shortfall is followed by ONE more limit set from what
it holds, in the same reclaim; the two receipts' freed bytes are summed. A
refused second call keeps the first's result and is probed.

Test: limit 9,369 MiB holding 2 GiB, target 1 GiB frees 1 GiB in one reclaim
(two calls), and the next reclaim plans from what is held. The fake now models
the fork's cache: held at most the limit, freed = held above the new limit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply
joelteply merged commit fd016eb into canary Nov 13, 2026
32 checks passed
@joelteply
joelteply deleted the fable/serving-gives-its-cache-back-first branch November 13, 2026 19:58
joelteply added a commit that referenced this pull request Nov 13, 2026
… live (#5970)

* The engine pin moves to fork 9269444f6: the host prompt cache can be lowered and raised live (POST /prompt-cache/limit), and context_geometry reports the slot's capped window

Fast-forward of 32 commits on codex/s4-per-mind-integration (316ad8b -> 9269444f6), including fork #110 and #111. #5969's reclaim lever needs the route; until this lands every engine answers 404 and the lever falls through to tier-down, as designed.

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

* The recorded engine pin moves with the pointer: 9269444f6

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

---------

Co-authored-by: Claude Opus 5.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