Skip to content

The host deficit reaches the engine's prompt cache first, judged by the OS's own pressure verdict (card 0f631c46) - #5981

Merged
joelteply merged 3 commits into
canaryfrom
fable/the-host-deficit-reaches-the-cache-first
Nov 14, 2026
Merged

joelteply merged 3 commits into
canaryfrom
fable/the-host-deficit-reaches-the-cache-first

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

How this serves the minds: a resident on a 64 GB Mac thinks at half speed, or not at all, while the host starves. This PR makes the one memory control work end to end: the engine's prompt cache is the first holder to give memory back, and it gets that memory back once the OS recovers. Order of work: line 3b ("one memory budget"), goal 9.

Evidence (the M5, 2026-11-13 23:40Z, canary 0ed0d23, which already includes #5969 and #5971)

Defect 1: standing starvation read as clean

core/continuum-kernel/src/system_resources/memory_pressure/host_deficit.rs (canary, HostDeficit::observe): if shortfall == 0 && !compressing && !swapping_out { self.baseline = Some(displaced); return 0; }. Under standing starvation macOS holds free pages at its small target (vm.vm_page_free_target = 4,000 pages = 64 MB) by compressing, so the compressor and swap stop growing and the shortfall reads 0. The whole 56 GB displaced became the baseline, and the deficit read 0.

Fix: only the OS's own verdict decides when the host is clean. The verdict is read in the host adapter (continuum-host/src/host_memory), and the logic has no cfg:

  • macOS: the kern.memorystatus_vm_pressure_level sysctl (1 normal, 2 warn, 4 critical). The mapping is pure (OsMemoryPressure::from_memorystatus_level) and tested on every host.
  • Linux: PSI /proc/pressure/memory (full avg10 > 0 is Critical, some avg10 > 0 is Warn, otherwise Normal). The parser is pure (pressure_from_psi).
  • A kernel with no verdict gives None, which means no deficit claim and never calm.

HostDeficit::observe now works like this:

  • OS normal: the baseline is taken (compressed + swap used) and the deficit is 0.
  • OS warn or critical: no baseline is taken. The deficit is max((displaced - baseline) + shortfall, free_target), so it is never 0 while the OS reports pressure. Without an OS-normal poll since boot, the baseline is 0.

No fixed fractions. The first-poll "nothing to compare" special case is gone.

Defect 2: the cache lever was on a path that never runs

core/continuum-serving/src/modules/serving_consumer.rs (canary):

  • reclaim_prompt_cache (line 415) was called only from ResourceConsumer::reclaim (line 726).
  • The authority (continuum-kernel/src/resources/daemon.rs) calls reclaim only when a resource kind is over its own ceiling. It never was.
  • The host deficit drives a different path, MemoryReporter::shed_load (mind results, ShedLru, Bevy).

Fix (one control):

  • The lever moves to modules/serving_prompt_cache/ (lever.rs is the old serving_consumer/prompt_cache.rs, moved and extended). It is registered as a MemoryReporter at MemoryPriority::Batch, the priority that yields first. deficit_at charges the cache before every higher priority.
  • It lowers the cache by its share_of_deficit, planned from what the cache holds (unchanged from Serving gives its host prompt cache back first when the authority reclaims memory (card 494c99f1) #5969). It acts only on the OS's deficit, never on the fixed RAM gates alone, which would fight the take-again half.
  • Take-again half: a new MemoryReporter::take_again hook. The monitor calls it after two OS-normal polls in a row (HostDeficit::take_again_due). The cache goes back to its launch grant, and the engine's /prompt-cache/limit reply is the receipt (probe serving.prompt_cache.take_again).
  • One engine step at a time, with an RAII in-flight guard. A refusal or a cache at its floor is probed once until a step runs again.
  • Deleted: the cache route through ServingConsumer (with_prompt_cache, reclaim_prompt_cache, the prompt_cache and device fields, EngineDeviceCredit::host_memory_is_the_pool) and its tests. Host RAM now has one reclaim path. The physical-measure confirmation of the engine's receipt went with it; the receipt is now the engine's own held_bytes/freed_bytes.
  • Wiring: ServingDaemonModule::give_host_memory_back_first(&pressure_monitor, rt_handle) in ipc/start_server.rs.

Simulator and tests

  • Dyno 11, standing starvation (serving_daemon/tests/dyno.rs). Built from the recorded readings (0.4 GB free, compressor flat at 42 GB, swap 14.5 GB, kernel critical, cache 16,525 MiB).
    • Before: the counters read clean (shortfall 0, nothing grows), which is all the old rule looked at.
    • After: the deficit stays > 0 across flat polls; the cache at Batch is charged before the minds (Interactive); the lever is asked and lowers the limit; after two OS-normal polls, take_again takes it back to 16,525 MiB.
  • host_deficit:
    • standing_starvation_is_owed_while_the_os_says_pressure
    • take_again_is_due_only_after_two_os_normal_polls
    • the incident and clean-box tests, rewritten on the verdict
  • Host adapter: memorystatus mapping, unread sysctl gives no verdict, PSI parse, mach and proc readings agree.
  • Lever wiring (serving_prompt_cache/tests.rs):
    • first by share
    • the RAM gate alone never lowers
    • taken back only after a lowering
    • one step at a time
    • the two-call plan from what the cache holds (migrated)
    • a refusal or a spent cache never wedges (migrated)
    • only a different engine starts again from its grant (migrated)

Lines

Diff: 954 added, 409 removed across 20 files. Production code: the consumer loses 124 lines (the cache route), and the shedder adds 271 (about 120 of them docs and probes). Most added lines are tests and the dyno (+312).

Not compiled locally

The M5 is serving live minds, so I ran no cargo build, check or test. CI is the compiler.

Live receipt to take (owner: whoever deploys this; deadline: the first hour after deploy)

Under OS pressure (sysctl kern.memorystatus_vm_pressure_level 2 or 4), check:

  • memory.pressure probe rows show os_pressure=Some(Warn|Critical) and deficit_mb > 0;
  • serving.reclaim.prompt_cache fires with held_bytes falling;
  • serving.prompt_cache.take_again fires after the OS reads normal.

Record free memory, compressor pages (vm_stat) and swap (sysctl vm.swapusage) before deploy and after one hour, plus prefill and decode tok/s.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

…he OS's own pressure verdict (card 0f631c46)

Two defects left the M5 starving at 0.4 GB free with a 16.5 GB prompt cache untouched
(2026-11-13 23:40Z, canary 0ed0d23):

1. host_deficit.rs took a "clean" baseline whenever the counters stood still and the free
   target was met. Under standing starvation macOS holds its free target by compressing, so
   the whole 42 GB compressed + 14.5 GB swap became the baseline and the deficit read 0.
   Now only the OS's own verdict (macOS kern.memorystatus_vm_pressure_level, Linux PSI, read
   in the continuum-host adapter) decides: OS normal takes the baseline; while it reports
   pressure the deficit is the displacement since the last OS-normal poll plus the shortfall,
   never less than the kernel's free target, and no baseline is taken.

2. The cache lever hung off ResourceConsumer::reclaim, which the authority calls only when a
   resource kind is over its ceiling: it never fired. The lever moves to
   modules/serving_prompt_cache as a MemoryReporter at Batch (yields first), lowered by its
   share of the OS deficit, and taken back to its launch grant through a new
   MemoryReporter::take_again hook after two OS-normal polls. The route through the
   ServingConsumer is deleted: one reclaim path for host RAM.

Dyno 11 (standing starvation) and unit tests at host_deficit, the adapters and the lever wiring.
Not compiled locally (the M5 is serving); CI is the compiler.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply
joelteply merged commit 7a29474 into canary Nov 14, 2026
84 of 87 checks passed
@joelteply
joelteply deleted the fable/the-host-deficit-reaches-the-cache-first branch November 14, 2026 00:51
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