Skip to content

Memory pressure: a worker works out its step and reports it #1361

Closed

Description

@V3RON

Part of #1355.

Scope

A worker works out its memory-pressure step from each host-load reading and reports it. An operator sees it in simlock status and in status.get, gets one memory.pressure-changed event per change, and sets it up through the new memoryPressure config section. Nothing acts on the step yet: admission and the warm pool are as today until #1362 and #1363. This task also adds every wire value the later tasks use, with the words each surface prints for it: the memory-pressure refusal and waiting reason, the warm pool's memory-pressure short reason, and the event in the alert list with its Slack message. So it is the only task of #1355 that bumps the protocol. Until #1362 lands, nothing refuses with the new reason.

In short

A new memoryPressure section (on by default) decides, from each 15 s reading, whether the worker is at none, warming-paused or new-devices-paused (Words used and Examples in #1355). A step comes on at the first reading that meets it. It lifts clearAfterMs (60 s) after the first reading that no longer meets it, on a Clock timer, unless a reading meets it again meanwhile: critical at 09:00 and calm from 09:00:15 lifts at 09:01:15. No usable reading is none at once. Each step keeps the time and the reading that turned it on, and the worker reports those of its current step: falling from new-devices-paused to warming-paused reports when and why warming was paused. At none there is no since and no cause. availablePercent is available ÷ total × 100 rounded down to a whole number; meeting a floor compares the exact value, so 9.9% meets a 10% floor and prints 9% available.

flowchart LR
  S[sampler reading] -->|each recorded reading| T[step tracker *new*]
  T --> P[core read port memoryPressure *new*]
  T -->|step changed| E[memory.pressure-changed *new*]
  P --> D[status.get capacity.memoryPressure *new*]
  D --> C[simlock status: Memory pressure line *new*]
Loading
# before
$ simlock status
Load: ... · memory 10.9 GiB of 32.0 GiB available · swap 14.0 GiB used · pressure warn · ...
Capacity ios: managed 2/6, running 2 + 0 reserved/6, warm 2

# after
$ simlock status
Load: ... · memory 10.9 GiB of 32.0 GiB available · swap 14.0 GiB used · pressure warn · ...
Memory pressure: warming paused since 09:12 (pressure warn)
Capacity ios: managed 2/6, running 2 + 0 reserved/6, warm 2

The line is Memory pressure: <warming paused | new devices paused> since <HH:MM> (<cause>). <HH:MM> is since in the CLI's local time, 24-hour, zero-padded. <cause> is pressure <level> when cause.trigger is level, else <availablePercent>% available, floor <floorPercent>%. A reading that meets a step by both its level and its floor is a level trigger. At none, or with no field, there is no line.

The new wire values print as:

Value Where Text
waiting reason memory-pressure simlock list --requests, the console's waiting list Full: host memory pressure (lower-cased after a request or worker name)
gateway worker entry full, capacityReason: "memory-pressure" simlock list --requests <worker>: full: host memory pressure
memory.pressure-changed to new-devices-paused Slack alert 🧠 Worker <worker> paused new devices: <cause>.
to warming-paused Slack alert 🧠 Worker <worker> paused warming: <cause>.
to none Slack alert ✅ Worker <worker> is no longer under memory pressure.

<cause> in the alert is memory pressure <level> when cause.trigger is level, else <availablePercent>% of memory available (floor <floorPercent>%). To none the event carries no cause.

If this goes wrong, an operator sees a step that does not match the Load line, or one that never lifts.

Technical spec

Facts

Commit 6674ad0.

# Fact Source
F1 macOS memory comes from one sysctl; pressure maps 1/2/4 to normal/warn/critical; available is hw.memsize × kern.memorystatus_level / 100 src/ports/host-load.ts:81-85, src/ports/host-load.ts:194
F2 Linux reads MemAvailable with no pressure; under a cgroup limit, available and total are against the limit (limited: true); Windows reads os.freemem() with no pressure src/ports/host-load.ts:268, src/ports/host-load.ts:416-417, src/ports/host-load.ts:148-153
F3 The sampler reads every 15 s; a failed read records a reading with no parts; current() answers the parts plus ageMs; it has no per-reading listener src/host-load/sampler.ts:32, src/host-load/sampler.ts:138-146, src/host-load/sampler.ts:68-71
F4 The worker builds the sampler only when hostLoadProbe is not null, then createCore, and passes hostLoad and load to the server src/daemon/worker-composition.ts:240-243, src/daemon/worker-composition.ts:257, src/daemon/worker-composition.ts:475
F5 Core, leasing, drivers, gateway and contract may not import host-load .dependency-cruiser.mjs:166-169
F6 status.get's capacity is buildCapacityFigures over the snapshot, built in the worker dispatcher; the schema is statusCapacitySchema (ios, android, global, optional ramBudget) src/daemon/dispatcher.ts:387, src/contract/status-schemas.ts:29-43
F7 capacity.changed is built from the capacity figures and emitted only when they differ src/bus/index.ts:89-94, src/core/capacity/observer.ts:39
F8 diskPressure is typed, defaulted and validated in core config, and declared again in the contract's configSchema src/core/config.ts:341, src/core/config.ts:996, src/core/config.ts:1147, src/contract/config-schemas.ts:110
F9 Unknown config keys are warned and dropped; a gateway warns on worker-only keys from lists given to warnWorkerOnlyKeys src/core/validation.ts:31-47, src/core/config.ts:729, src/core/config.ts:795-802
F10 Every key the validator accepts must be a row of the website's configuration reference src/core/config-reference.test.ts:81, website/src/content/docs/docs/reference/configuration.mdx:159
F11 An event is added in EventMap, EVENT_NAMES in the bus test, EVENTS.md and the website's events reference; a test fails when the catalog's names differ src/bus/index.ts:272, src/bus/index.test.ts:54, docs/internal/EVENTS.md:159, website/src/content/docs/docs/reference/events.mdx:197
F12 simlock status prints the Load line, then the Capacity lines with (at RAM budget), then the RAM budget line src/cli/format.ts:145-147, src/cli/format.ts:259-266
F13 The model test starts a daemon with FakeHostLoadProbe and a fake clock, and reads status.get src/daemon/main-host-load.test.ts:90, src/daemon/main-host-load.test.ts:109, src/daemon/main-host-load.test.ts:132
F14 The protocol range is one number, and a contract shape change bumps it src/contract/protocol.ts:13
F15 CapacityRefusalReason has four members; the planner maps each to a waiting reason in a Record, so a missing row fails to compile src/core/capacity/strategy.ts:30-34, src/leasing/acquisition-planner.ts:53-64
F16 The wire enums: waiting reasons, capacityReasonSchema, NO_CAPACITY details, the warm pool's short src/contract/lease-schemas.ts:94-108, src/contract/errors.ts:37-39, src/contract/status-schemas.ts:53-64
F17 The gateway's routing keeps its own CapacityReason union, and keeps a worker's refusal reason only when it parses src/gateway/routing/pipeline.ts:17-21, src/gateway/fleet-refusals.ts:141-144
F18 The CLI's and the console's waiting texts are Records over the waiting reasons; a gateway entry full uses the CLI's text for its capacityReason src/cli/commands/list.ts:50-58, src/cli/commands/list.ts:100-104, ui/src/model/waiting.ts:60-70
F19 ALERT_EVENTS lists the events a destination may name; the Slack messages are a Record over it; the alerts test emits an event and reads the text posted src/core/config.ts:95-107, src/alerts/formats/slack/messages.ts:36-37, src/alerts/alerts.test.ts:333-343
F20 The CLI test holds a reason → text table for a worker's and a gateway's entry src/cli/index.test.ts:9518, src/cli/index.test.ts:9634
F21 The load block is fitted on the worker before the daemon's own output check; a gateway's client parses status.get strictly, so an out-of-bounds field fails the whole read src/daemon/worker-composition.ts:475, src/contract/status-schemas.ts:229-234, src/simlock-client/client.ts:1019
F22 Under a cgroup limit available is limit - (used - inactive), which can pass the limit src/ports/host-load.ts:416
F23 Host-load may not import core, leasing, gateway, drivers, contract, http, cli, mcp or daemon; bus and ports are allowed .dependency-cruiser.mjs:154-158
F24 A hung read records a failed reading after 10 s, so through startDaemon a reading never gets 60 s old src/host-load/sampler.ts:33, src/host-load/sampler.ts:104-108
F25 The server starts the sampler before converge and disposes it at stop src/daemon/server.ts:414, src/daemon/server.ts:653

Modules touched

Searches: HostLoadSampler, hostLoad, diskPressure, statusCapacitySchema, buildCapacityFigures, formatLoadLine, disk.pressure-detected, EVENT_NAMES, CapacityRefusalReason, capacityReasonSchema, waitingReasonSchema, warmPoolShortSchema, CapacityReason, WAITING_REASON_TEXT, WHY_TEXT, ALERT_EVENTS across src/, ui/, website/.

flowchart LR
  DC[daemon/worker-composition *] --> HL[host-load: sampler listener *, step tracker *]
  DC --> CO[core: create-core read port *, config *]
  DD[daemon/dispatcher status.get *] --> CO
  DD --> CT[contract: status-schemas *, config-schemas *, protocol *]
  CLI[cli/format *] --> CT
  HL --> BUS[bus: EventMap *]
Loading
  • src/host-load/: the sampler calls an optional listener with each recorded reading; a new step tracker (index.ts exports it) fed by it, holding the hold and 60 s staleness timers on the Clock, emitting the event. The tracker declares its own options type for the thresholds it reads (as HostLoadSamplerOptions does) and never imports core; core owns the memoryPressure section's type, and daemon/worker-composition.ts passes config.memoryPressure in. Tests: the dependency check (host-load-imports-ports-only) stays green (decided in delivery, approved by the maintainer 2026-11-06)
  • src/core/config.ts, src/contract/config-schemas.ts: the memoryPressure section (Shared values in Admission respects the host's real memory pressure #1355), its defaults and validator, worker-only on a gateway.
  • src/core/create-core.ts, src/core/core-ports.ts: an optional memoryPressure option on createCore ({ current, onReading }); absent, current() answers undefined and onReading never calls. Held for Memory pressure: new devices wait while a worker's new devices are paused #1362 and Memory pressure: the warm pool pauses and sheds idle devices #1363.
  • src/daemon/worker-composition.ts: builds the tracker beside the sampler when host load is on; passes it to createCore and the dispatcher.
  • src/daemon/dispatcher.ts: status.get adds capacity.memoryPressure after buildCapacityFigures, so capacity.changed is unchanged (F7).
  • src/contract/status-schemas.ts: memoryPressure optional on statusCapacitySchema, bounded.
  • src/bus/index.ts, src/cli/format.ts.
  • src/daemon/server.ts: a memoryPressure option disposed with hostLoad at stop (server.ts:653).
  • The new values, with no caller acting on them yet: src/core/capacity/strategy.ts (memory-pressure in CapacityRefusalReason), src/leasing/acquisition-planner.ts (its row), src/contract/lease-schemas.ts, src/contract/errors.ts, src/http/lease-declarations.ts, src/contract/status-schemas.ts (warmPoolShortSchema), src/gateway/routing/pipeline.ts (CapacityReason), src/cli/commands/list.ts and ui/src/model/waiting.ts (the text), src/core/config.ts and src/contract/config-schemas.ts (ALERT_EVENTS), src/alerts/formats/slack/messages.ts.
  • src/core/warm-pool/policy.ts shortOf keeps mapping an unknown refusal to running-limit until Memory pressure: the warm pool pauses and sheds idle devices #1363.
  • Docs: docs/internal/EVENTS.md, website reference/events.mdx, reference/configuration.mdx, reference/errors.mdx (NO_CAPACITY reasons), gateway/alerts.mdx (the row), worker/console.mdx (the full-reason table), the simlock status output on the worker pages, src/host-load/AGENTS.md.

Surface and extension points

src/host-load/index.ts gains new MemoryPressureTracker({ clock, config, eventBus, logger }) with observe(reading) (fed by the sampler's listener), current() → { step, since, cause, readingAt } | undefined, onReading(listener) → unsubscribe (called after each observed reading, once the step is updated) and dispose(), which cancels its timers and emits nothing more; the server disposes it with the sampler (F25), before daemon.stopping's teardown ends. Core's read port is { current, onReading }; readingAt is the latest reading's time, absent before the first. Its config, step and cause types are declared in host-load (F23); the daemon hands it the memoryPressure section. No second implementation of anything.

Contract and event changes

This PR bumps the protocol by one.

  • statusCapacitySchema.memoryPressure, optional: step enum none | warming-paused | new-devices-paused; since (a finite non-negative number, epoch ms) and cause { trigger: "level" | "floor", availablePercent: integer 0-100, level?: "normal" | "warn" | "critical", floorPercent?: number 0-100 (present when triggerisfloor) }, both present unless step is none and absent then. availablePercent is clamped to 0-100 before rounding down (F22), and the block is fitted on the worker before the output check, as the load block is (F21).
  • configSchema.memoryPressure: the section as in Admission respects the host's real memory pressure #1355's Shared values.
  • memory-pressure joins waitingReasonSchema, capacityReasonSchema, NO_CAPACITY's capacityReason and warmPoolShortSchema; memory.pressure-changed joins ALERT_EVENTS and the contract's configSchema list of alert events.
  • Event memory.pressure-changed { from, to, cause? } (from, to step enums; cause as above, the new step's, absent when to is none), a row in EVENTS.md and the website's events reference; emitted by the step tracker, after the step changed, once per change.

Other code on the same state

Searches: current() on the sampler (2 hits: the server's load, the uplink's firstReading); capacity in status.get (1 hit, dispatcher.ts:387); diskPressure (5 hits, the reaper's own disk read, unrelated state).

Failure modes

Call Outcome What happens Limit, counted from
sampler → tracker listener reading with memory step worked out, event on change each reading, every 15 s
sampler → tracker listener reading without memory (failed read) step none at once, event if it changed that reading
sampler → tracker listener no reading for 60 s step none, event if it changed; unreachable through startDaemon (F24), tested on the tracker 60 s from the last reading
server stop tracker disposed timers cancelled, no event none
sampler → tracker listener listener throws logged, the sampler goes on none
status.get host load off no memoryPressure field none

The step is memory only: a restart starts with no step until the first reading; no file is written. One tracker per worker, fed one reading at a time.

Rules in play

  • ADR 0068 §1: "No reading is no step"; the tracker drops every step at once on a reading with no memory part or after 60 s with none.
  • ADR 0068 §2: "Core stays clear of host-load"; core takes a read function, never the tracker's module (F5).
  • ADR 0029 §7 as narrowed by 0068 §5: one event per step change, "never per reading".
  • Events rule (bus manual): "Emit only after the commit that made it true"; the tracker emits after it stored the new step.
  • ADR 0065 §1: createCore takes the slice it reads; the tracker takes memoryPressure only.
  • Dependency rule host-load-imports-ports-only (F23): host-load "imports no other component" among core, contract, daemon and the rest; the tracker declares its own config and step types, and takes the EventBus type from bus, which the rule allows.

Tests

The seam is startDaemon with FakeHostLoadProbe and a fake clock, modelled on src/daemon/main-host-load.test.ts; the 60 s case drives MemoryPressureTracker in a new src/host-load/memory-pressure.test.ts modelled on src/host-load/sampler.test.ts; config and CLI lines use src/core/config.test.ts and src/cli/index.test.ts.

  • An operator's worker reads memory and status.get reports the step it meets: macOS normal/warn/critical at 34% → none/warming-paused/new-devices-paused, the last two with trigger: "level"; macOS warn at 8% → new-devices-paused with trigger: "floor", floorPercent: 10; macOS critical at 8% (level and floor both) → trigger: "level"; macOS normal at 8% → new-devices-paused; no level at 25%, 15%, 9.9% → none, warming-paused, new-devices-paused; level off and floor 0 → none; enabled: false → none.
  • A step lifts only after clearAfterMs with no meeting reading: calm 45 s after the first calm reading still on; 60 s after it lifted; a meeting reading at 30 s restarts the hold; new-devices-paused falls to warming-paused while only the lower step meets, reporting warming's own since and cause; a later, different meeting reading keeps since and cause.
  • No usable reading is no step, at once: host load off (no memoryPressure field); a failed read; no reading for 60 s (at the tracker); each while a step held, since and cause absent after.
  • The worker emits memory.pressure-changed once per step change: none→warming, warming→new devices, new devices→warming, warming→none, each with from, to and the new step's cause (none to none); a second meeting reading emits nothing.
  • An operator's memoryPressure config is defaulted and checked: the defaults; level: "bogus" refused naming the key; minAvailablePercent 101 and -1 refused; clearAfterMs -1 refused; a gateway's config with the section warns it is worker-only.
  • An operator's simlock status prints the Memory pressure line: warming by level Memory pressure: warming paused since 09:12 (pressure warn); new devices by floor Memory pressure: new devices paused since 09:40 (8% available, floor 10%); 9.9% prints 9% available; macOS warn at 8% tripped by the floor prints the floor; step none prints no line; no field prints no line.
  • An operator's simlock list --requests names host memory pressure: a worker's own waiting request full: host memory pressure; a gateway request's worker entry full with capacityReason: "memory-pressure".
  • An operator's console waiting list names host memory pressure: reason memory-pressure reads Full: host memory pressure.
  • A gateway alert destination listing memory.pressure-changed gets one message per change: Slack to new devices paused by level; to warming paused by floor; to none; generic JSON envelope.

Done when

  • pnpm exec vitest run --project unit src/daemon/main-host-load.test.ts src/core/config.test.ts src/cli/index.test.ts src/bus/index.test.ts src/core/config-reference.test.ts src/alerts/alerts.test.ts ui/src/model/waiting.test.ts src/contract/schemas.test.ts src/host-load/memory-pressure.test.ts passes.
  • pnpm lint passes, the boundary rules not-host-load and host-load-imports-ports-only included.
  • The website's configuration and events references list memoryPressure.* and memory.pressure-changed.

Out of scope

Depends on

Written by an agent.

Activity

  1. added
    task:draft
    on Nov 6, 2026
  2. added
    task:ready
    and removed
    task:draft
    on Nov 6, 2026
  3. self-assigned this
    on Nov 6, 2026
  4. added 20 commits that reference this issue on Nov 6, 2026
  5. added a commit that references this issue on Nov 6, 2026
    194d9c2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

task:ready

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions