From bee96cc305947031f7b5528dc2cd993e9667fb1b Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 13 Oct 2026 19:35:47 +0000 Subject: [PATCH 01/11] docs(adr): 0026 host load is an optional module whose readings ride on status Proposed for #527. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0128DwBvv9jQ8CDpFZn7m9i8 --- ...al-module-whose-readings-ride-on-status.md | 253 ++++++++++++++++++ docs/internal/adr/README.md | 1 + 2 files changed, 254 insertions(+) create mode 100644 docs/internal/adr/0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md diff --git a/docs/internal/adr/0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md b/docs/internal/adr/0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md new file mode 100644 index 000000000..26cde8809 --- /dev/null +++ b/docs/internal/adr/0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md @@ -0,0 +1,253 @@ +# 0026. Host load is an optional module whose readings ride on status + +- **Status:** Proposed +- **Date:** 2026-10-13 +- **Issue:** [#527](https://github.com/callstackincubator/simlock/issues/527) +- **Supersedes:** nothing. Adds to [ADR + 0008](0008-the-catalog-pairs-models-with-runtimes-and-status-carries-host-facts.md): + status gains a second block about the machine beside `host`. Leaves [ADR + 0009](0009-gateway-routing-is-a-list-of-stages.md) as it is: no stage + reads the new block. +- **Depends on:** ADR 0005 (worker views, the uplink refresh), ADR 0008 + (host facts in status and in the view), ADR 0013 §4 (the console corrects + for clock skew with the `Date` header), architecture rules 9, 14 and 15. + +## Context + +#527 asks every place that shows a machine's host facts to also show its +current load: CPU busy %, available memory, swap, the macOS memory pressure +level, and free disk on the disk that holds Simlock's data folder. Readings +are for people. Nothing in Simlock acts on them. + +Four questions have answers that bind every task under #527 and any later +feature that uses the readings (a console chart, routing on load): + +- Where the code lives, and who may depend on it. +- How a reading travels from a worker to a gateway and the console. The + first sketch of #527 had a command that asks a worker to start or stop + sending readings. +- How a reading says how old it is when the worker, the gateway and the + browser have three clocks. +- What the wire shape is. It moves the protocol. + +Two facts shape the answers. A CPU percentage needs two readings some time +apart, so something has to read the machine on a timer whether or not +anyone is looking. And the gateway already calls `status.get` on every +worker, on every capacity event and on a 30 s tick, and the console +already polls `GET /v1/workers` every second. + +## Decision + +### 1. One module outside the core, wired only by the composition root + +`src/host-load/`, entered through its `index.ts`, holds the sampler and the +reading's types. It depends on ports only: `Clock`, `Logger` and the new +`HostLoadProbe` (§2). It imports nothing from `core`, `leasing`, `gateway`, +`drivers` or any frontend. A boundary test, modelled on +`src/core/warm-pool/boundary.test.ts`, keeps every file outside the +directory off everything but `index.ts`, and a lint override keeps `fs` and +`child_process` out of it. + +It sits outside `src/core/` because a gateway runs it too, and a gateway +has no core. + +`src/daemon/main.ts` builds it in both modes and hands each status +handler a function, `load: () => HostLoadReading | undefined`. No other +file imports it. Built or left out, the daemon behaves the same apart from +the `load` block (architecture rule 15). + +```mermaid +flowchart LR + main["daemon/main.ts"] --> HL["host-load/ (new)"] + main --> WD["daemon/dispatcher.ts (status.get)"] + main --> GD["gateway/dispatcher.ts (status.get)"] + HL --> P["ports/host-load.ts (new)"] + P --> FS["ports: Filesystem, ProcessRunner"] + WD -. "load()" .-> HL + GD -. "load()" .-> HL + R["gateway/routing, core capacity, warm pool, reaper"] +``` + +The last box has no arrow on purpose. Nothing that decides anything reads +the readings. + +### 2. One port, two adapters, raw counters + +`src/ports/host-load.ts` declares: + +```ts +interface HostLoadProbe { + /** Never throws. A part that cannot be read is absent. */ + read(): Promise; +} +``` + +`HostLoadCounters` holds raw figures, not percentages: cumulative CPU busy +and total time with the core count, memory total, available and swap used, +the macOS pressure level, and disk free and total. CPU and memory each say +whether a limit applies (`limited`). The composition root picks the adapter +from `process.platform`, as it does for `NodeHostInfo`, and a +`FakeHostLoadProbe` serves tests. + +- **macOS** (`DarwinHostLoadProbe`). CPU from Node's `os.cpus()` times. + Memory from one bounded `sysctl -n hw.memsize kern.memorystatus_level + kern.memorystatus_vm_pressure_level vm.swapusage` call: available is + `hw.memsize` times the free percentage `kern.memorystatus_level` reports, + the figure macOS's own `memory_pressure` tool prints. Pressure 1, 2 and 4 + are `normal`, `warn` and `critical`. Never `limited`. +- **Linux** (`LinuxHostLoadProbe`), through the `Filesystem` port so tests + seed `/proc` and `/sys` files. The cgroup comes from the `0::` line of + `/proc/self/cgroup`, and its files from under `/sys/fs/cgroup`. + - CPU: when `cpu.max` sets a quota, `cpu.stat` `usage_usec` against + quota ÷ period cores, `limited`. Otherwise the `cpu` line of + `/proc/stat` against the machine's cores. + - Memory: when `memory.max` is a number, total is that limit, available + is the limit minus `memory.current` plus `inactive_file` from + `memory.stat` (never below zero), swap is `memory.swap.current`, + `limited`. Otherwise `MemTotal`, `MemAvailable` and `SwapTotal − + SwapFree` from `/proc/meminfo`. + - No `0::` line (cgroup v1), a missing file, or `max` means no limit. +- **Disk**, both adapters: free and total from one `statfs` of the data + folder, the folder the cleanup reaper's `diskFree` reads. The + `Filesystem` port gains `diskSpace(path)`, returning both, beside + `diskFree`. + +A limit is detected, not a container. Linux has no reliable "am I in a +container" signal. A cgroup limit is both detectable and the figure +Simlock can actually use. + +### 3. The sampler reads every 15 seconds, one read at a time + +- The sampler starts when the daemon starts, before startup convergence, + so a starting daemon already has a reading. The first read is + immediate. After that, a `Clock` timer re-armed after each read fires + every 15 s. The interval is fixed, with no config key. +- One read is in flight at most. A tick that finds one still running is + skipped. Each read is bounded at 10 s, after which that read counts as + failed. The `sysctl` call has its own process timeout below that. +- Busy % is the difference between this read's CPU counters and the + previous successful read's. With no previous read, or after a change of + `limited` or core count, CPU is absent from that reading. +- A part absent from a read is absent from the reading. The sampler does + not carry an older value forward: "unavailable" is shown rather than a + stale figure. A part that starts or stops failing is logged once per + change, at warn. +- The reading records when it was taken, on the sampler's clock. +- `dispose()` cancels the timer, and the daemon's stop sequence calls it. + +### 4. On the wire, `status.get` carries the age, not a timestamp + +`status.get` gains an optional `load` block, on the starting answer and the +running one alike: + +```json +"load": { "ageMs": 4120, + "cpu": { "busyPercent": 34.2, "cores": 10, "limited": false }, + "memory": { "totalBytes": 34359738368, "availableBytes": 10522669875, + "swapUsedBytes": 1288490188, "pressure": "normal", "limited": false }, + "disk": { "freeBytes": 120689623040, "totalBytes": 530870820864 } } +``` + +- `ageMs` is how old the reading is at the moment the daemon answers. It + compares no clocks across machines. +- `cpu`, `memory` and `disk` are each optional. `pressure` is present only + where the operating system reports one. +- The block is absent when no reading has finished yet, and when the + module is not wired. +- Every number is finite and not negative, `busyPercent` is at most 100, + `cores` is at least 1, and `pressure` is one of `normal`, `warn` and + `critical`. A gateway parses every worker's answer, so the schema bounds + all of it. +- The protocol moves up by one. The CLI and the console read the block + only when it is present. + +### 5. In a worker view, the age becomes a time on the view holder's clock + +The worker view gains an optional `load` block with the same parts, plus +`readAt` in place of `ageMs`. `workerViewFields` builds it as "now, minus +`ageMs`", where "now" is when the `status.get` answer arrived, on the +clock of whoever holds the view: the gateway's, or the worker's own for +its `worker.list`. Every time in a view is then on one clock, and the +console's existing `Date`-header correction makes the age right in the +browser. + +```mermaid +sequenceDiagram + participant S as Worker sampler + participant W as Worker status.get + participant G as Gateway WorkerLink + participant C as Console + S->>S: read every 15 s + G->>W: status.get (event or 30 s tick) + W-->>G: load { ageMs: 4120, ... } + G->>G: view.load.readAt = now - 4120 + C->>G: GET /v1/workers (every 1 s) + G-->>C: view with load.readAt, Date header + C->>C: age = corrected now - readAt + Note over G,W: uplink lost + G->>G: view drops load + C->>G: GET /v1/workers + G-->>C: view without load ("no reading while disconnected") +``` + +`WorkerRegistry.disconnected` drops `load`, as it already drops `installs` +and `waiting`. That is the one place a view loses it, and a disconnected +view never carries it. A reconnect fills it in again on the next refresh. + +### 6. A gateway reports its own machine, and sums nothing + +A gateway runs its own sampler. Its `status.get` `load` is the gateway +machine's. It does not add up its workers' loads: "the fleet is 40% busy" +hides the one machine that is at 100%. Each worker's load is in its view. + +### 7. Readings go only where status goes + +There is no command to start or stop readings, no subscription, no event +per reading and no history. A reading travels only inside a `status.get` +answer someone already asked for. Per connected worker that adds about 300 +bytes to each gateway refresh. + +## Consequences + +- An operator sees each machine's load in `simlock status`, `simlock + worker list`, `--json`, the HTTP API and the console, with its age. +- A gateway shows a worker's load up to about 45 s old: 15 s of sampling + plus the 30 s refresh tick. Events that change capacity refresh sooner. +- Each daemon spawns one `sysctl` every 15 s on macOS, and reads a few + small files on Linux. +- A later feature that wants live charts, alerts or routing on load starts + from the module and the wire shape here. Routing on load needs its own + ADR: it would make a pick depend on readings up to 45 s old, which the + fixed views routing reads today are not. +- The disk pressure cleanup is unchanged. It still reads `diskFree` + itself, on its own tick, against its own threshold. The two numbers come + from the same `statfs` call, at different moments. +- An older worker sends no `load`. Its view has none, and the console and + the CLI show nothing for it. + +## Alternatives considered + +- **A command that asks a worker to start or stop sending readings.** + Rejected: it puts subscription state on the worker across a link that + drops. A gateway that restarts, or a console tab that closes, leaves + nobody to stop it, and several viewers would need reference counting. + The worker samples anyway, for the CPU delta, so asking for the latest + reading costs nothing. +- **A stream or an event per reading.** Rejected: one event every 15 s per + worker would push business events out of the ring buffer, for the same + reason `device.exec` emits none. +- **Read on demand, with no timer.** No reads while nobody looks. + Rejected: busy % needs two reads a known time apart. A delta over + whatever time passed between two status calls is a different number + every time. A load average needs no delta, but inside a container it is + the host's. +- **Put it in `HostFactsReader`.** Rejected: host facts change when someone + installs a tool and are re-read every minute. Load changes every second. + It also lives in the core, which a gateway does not run. +- **A timestamp on the wire.** Rejected: it would compare a worker's clock + with the gateway's and the browser's. `ageMs` needs no shared clock. +- **Detect containers and say `in container`.** Rejected: no reliable + signal exists across container runtimes, and a systemd service limit is + the same constraint without a container. See §2. +- **Node's `os.freemem()`.** Rejected: on macOS it counts only free pages, + so a healthy Mac looks nearly full. diff --git a/docs/internal/adr/README.md b/docs/internal/adr/README.md index 2ea0e8c36..8b4185f35 100644 --- a/docs/internal/adr/README.md +++ b/docs/internal/adr/README.md @@ -67,3 +67,4 @@ the status is stale. | [0026](0026-agent-device-through-a-gateway-relays-over-the-uplink.md) | agent-device through a gateway: workers advertise capabilities, and the host link relays over the uplink | Accepted — not yet implemented; §2 narrowed by 0028 for relayed bodies | | [0027](0027-dependency-cruiser-checks-every-component-boundary.md) | dependency-cruiser checks every component's boundary and the module graph | Accepted | | [0028](0028-the-uplink-carries-byte-channels-and-small-messages-go-first.md) | The uplink carries byte channels beside its messages, and small messages go first | Accepted — partly implemented (§1–3) | +| [0026](0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md) | Host load is an optional module whose readings ride on status | Proposed | From 8a1101154a3d7221ee7e363884ba9552f12a3974 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 13 Oct 2026 19:38:15 +0000 Subject: [PATCH 02/11] docs(adr): 0026 allows fractional cores and ticks on a fixed beat Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0128DwBvv9jQ8CDpFZn7m9i8 --- ...-optional-module-whose-readings-ride-on-status.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/docs/internal/adr/0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md b/docs/internal/adr/0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md index 26cde8809..903b2b53f 100644 --- a/docs/internal/adr/0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md +++ b/docs/internal/adr/0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md @@ -120,8 +120,8 @@ Simlock can actually use. - The sampler starts when the daemon starts, before startup convergence, so a starting daemon already has a reading. The first read is - immediate. After that, a `Clock` timer re-armed after each read fires - every 15 s. The interval is fixed, with no config key. + immediate. After that, a `Clock` timer fires every 15 s, re-armed each + time it fires. The interval is fixed, with no config key. - One read is in flight at most. A tick that finds one still running is skipped. Each read is bounded at 10 s, after which that read counts as failed. The `sysctl` call has its own process timeout below that. @@ -155,9 +155,11 @@ running one alike: - The block is absent when no reading has finished yet, and when the module is not wired. - Every number is finite and not negative, `busyPercent` is at most 100, - `cores` is at least 1, and `pressure` is one of `normal`, `warn` and - `critical`. A gateway parses every worker's answer, so the schema bounds - all of it. + `cores` is above zero (a cgroup quota can allow half a core), and + `pressure` is one of `normal`, `warn` and `critical`. A gateway parses + every worker's answer, so the schema bounds all of it. The sampler + leaves out a part whose figures break these bounds, so a bad read never + fails `status.get`. - The protocol moves up by one. The CLI and the console read the block only when it is present. From 3aa3f0e0cbede908e7ef8feafd95f0d938d51eb3 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 13 Oct 2026 19:42:15 +0000 Subject: [PATCH 03/11] docs(adr): 0026 states the registry's whole rule for load on a view Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0128DwBvv9jQ8CDpFZn7m9i8 --- ...s-an-optional-module-whose-readings-ride-on-status.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/internal/adr/0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md b/docs/internal/adr/0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md index 903b2b53f..45a000ba0 100644 --- a/docs/internal/adr/0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md +++ b/docs/internal/adr/0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md @@ -192,9 +192,12 @@ sequenceDiagram G-->>C: view without load ("no reading while disconnected") ``` -`WorkerRegistry.disconnected` drops `load`, as it already drops `installs` -and `waiting`. That is the one place a view loses it, and a disconnected -view never carries it. A reconnect fills it in again on the next refresh. +The worker registry owns the rule that a view not `connected` carries no +`load`. It drops `load` when a worker disconnects or turns incompatible, as +it already drops `installs` and `waiting`. A refresh never adds `load` to +such a view, even when a `status.get` answer lands after the disconnect. +A refresh that carries no `load` removes the old one rather than keeping +it. A reconnect fills `load` in again on the next refresh. ### 6. A gateway reports its own machine, and sums nothing From 600a4b98c64c228718b44f8515de9edbb9acea14 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 13 Oct 2026 19:44:40 +0000 Subject: [PATCH 04/11] docs(adr): 0026 treats a memory.max at or above MemTotal as no limit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0128DwBvv9jQ8CDpFZn7m9i8 --- ...load-is-an-optional-module-whose-readings-ride-on-status.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/internal/adr/0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md b/docs/internal/adr/0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md index 45a000ba0..9aa70c271 100644 --- a/docs/internal/adr/0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md +++ b/docs/internal/adr/0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md @@ -101,7 +101,8 @@ from `process.platform`, as it does for `NodeHostInfo`, and a - CPU: when `cpu.max` sets a quota, `cpu.stat` `usage_usec` against quota ÷ period cores, `limited`. Otherwise the `cpu` line of `/proc/stat` against the machine's cores. - - Memory: when `memory.max` is a number, total is that limit, available + - Memory: when `memory.max` is a number below `MemTotal`, total is that + limit, available is the limit minus `memory.current` plus `inactive_file` from `memory.stat` (never below zero), swap is `memory.swap.current`, `limited`. Otherwise `MemTotal`, `MemAvailable` and `SwapTotal − From 22788e034322795c4014700035f16a83ed88e709 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 13 Oct 2026 19:54:42 +0000 Subject: [PATCH 05/11] docs(adr): renumber host load to 0027 and close the spec check's gaps 0026 is taken by #512. Adds the lifecycle on every exit, the ageMs clamp, fitHostLoad beside the schema, the production probe factory, parent cgroup limits, and Not reported for an older worker in the console. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0128DwBvv9jQ8CDpFZn7m9i8 --- ...l-module-whose-readings-ride-on-status.md} | 142 +++++++++++------- docs/internal/adr/README.md | 2 +- 2 files changed, 92 insertions(+), 52 deletions(-) rename docs/internal/adr/{0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md => 0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md} (63%) diff --git a/docs/internal/adr/0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md b/docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md similarity index 63% rename from docs/internal/adr/0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md rename to docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md index 9aa70c271..68e00f191 100644 --- a/docs/internal/adr/0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md +++ b/docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md @@ -1,4 +1,4 @@ -# 0026. Host load is an optional module whose readings ride on status +# 0027. Host load is an optional module whose readings ride on status - **Status:** Proposed - **Date:** 2026-10-13 @@ -10,7 +10,8 @@ reads the new block. - **Depends on:** ADR 0005 (worker views, the uplink refresh), ADR 0008 (host facts in status and in the view), ADR 0013 §4 (the console corrects - for clock skew with the `Date` header), architecture rules 9, 14 and 15. + for clock skew with the `Date` header), architecture rules 9, 11, 14 + and 15. ## Context @@ -43,22 +44,29 @@ already polls `GET /v1/workers` every second. `src/host-load/`, entered through its `index.ts`, holds the sampler and the reading's types. It depends on ports only: `Clock`, `Logger` and the new `HostLoadProbe` (§2). It imports nothing from `core`, `leasing`, `gateway`, -`drivers` or any frontend. A boundary test, modelled on +`drivers`, `contract` or any frontend, and none of `core`, `leasing`, +`drivers`, `gateway` or `contract` imports it. Lint enforces both +directions. A boundary test, modelled on `src/core/warm-pool/boundary.test.ts`, keeps every file outside the -directory off everything but `index.ts`, and a lint override keeps `fs` and -`child_process` out of it. +directory off everything but `index.ts`. It sits outside `src/core/` because a gateway runs it too, and a gateway has no core. -`src/daemon/main.ts` builds it in both modes and hands each status -handler a function, `load: () => HostLoadReading | undefined`. No other -file imports it. Built or left out, the daemon behaves the same apart from -the `load` block (architecture rule 15). +`src/daemon/main.ts` builds it in both modes. `DaemonServer` starts and +disposes it (§3). Each status handler gets a function, `load: () => +StatusLoad | undefined`, which main.ts builds as `fitHostLoad(sampler.current())`. +`fitHostLoad` lives beside the schema in `src/contract/schemas.ts`, as +`fitHostFacts` does, so the wire bounds are written once. + +`startDaemon`'s `hostLoadProbe` option set to `null` leaves the module out. +The daemon then behaves the same apart from the `load` block (architecture +rule 15), and a test proves it. ```mermaid flowchart LR main["daemon/main.ts"] --> HL["host-load/ (new)"] + main --> SRV["daemon/server.ts (start, dispose)"] main --> WD["daemon/dispatcher.ts (status.get)"] main --> GD["gateway/dispatcher.ts (status.get)"] HL --> P["ports/host-load.ts (new)"] @@ -85,29 +93,43 @@ interface HostLoadProbe { `HostLoadCounters` holds raw figures, not percentages: cumulative CPU busy and total time with the core count, memory total, available and swap used, the macOS pressure level, and disk free and total. CPU and memory each say -whether a limit applies (`limited`). The composition root picks the adapter -from `process.platform`, as it does for `NodeHostInfo`, and a -`FakeHostLoadProbe` serves tests. +whether a limit applies (`limited`). Busy time is all CPU time except idle +and iowait, which is what `top` counts as used. + +One factory in the same file, `productionHostLoadProbe({ platform, +dataDirectory })`, builds the adapter for `process.platform` with its +production inputs, so the composition root holds no adapter detail and +both modes build the same probe. `FakeHostLoadProbe` serves tests. - **macOS** (`DarwinHostLoadProbe`). CPU from Node's `os.cpus()` times. Memory from one bounded `sysctl -n hw.memsize kern.memorystatus_level kern.memorystatus_vm_pressure_level vm.swapusage` call: available is `hw.memsize` times the free percentage `kern.memorystatus_level` reports, the figure macOS's own `memory_pressure` tool prints. Pressure 1, 2 and 4 - are `normal`, `warn` and `critical`. Never `limited`. + are `normal`, `warn` and `critical`. Never `limited`. The factory gives + it a process runner of its own, not the daemon's logging one, so a call + every 15 s writes nothing to the daemon log. - **Linux** (`LinuxHostLoadProbe`), through the `Filesystem` port so tests - seed `/proc` and `/sys` files. The cgroup comes from the `0::` line of - `/proc/self/cgroup`, and its files from under `/sys/fs/cgroup`. - - CPU: when `cpu.max` sets a quota, `cpu.stat` `usage_usec` against - quota ÷ period cores, `limited`. Otherwise the `cpu` line of - `/proc/stat` against the machine's cores. - - Memory: when `memory.max` is a number below `MemTotal`, total is that - limit, available - is the limit minus `memory.current` plus `inactive_file` from - `memory.stat` (never below zero), swap is `memory.swap.current`, - `limited`. Otherwise `MemTotal`, `MemAvailable` and `SwapTotal − - SwapFree` from `/proc/meminfo`. - - No `0::` line (cgroup v1), a missing file, or `max` means no limit. + seed `/proc` and `/sys` files. Without a limit, CPU is the `cpu` line of + `/proc/stat` against `os.cpus().length` cores (every CPU, as that line + covers), and memory is `MemTotal`, `MemAvailable` and `SwapTotal − + SwapFree` from `/proc/meminfo`. +- **Linux limits.** Simlock's cgroup is the `0::` line of + `/proc/self/cgroup`. Its limit files are read in that cgroup's folder + under `/sys/fs/cgroup` and in every parent folder up to `/sys/fs/cgroup`, + and the lowest limit wins, because that is the one that binds. + - CPU: a `cpu.max` quota below the machine's cores is a limit. CPU is + then `cpu.stat` `usage_usec` against elapsed time × quota ÷ period + cores, `limited`. + - Memory: a `memory.max` below `MemTotal` is a limit. Total is that + limit, available is the limit minus `memory.current` plus + `inactive_file` from `memory.stat` (never below zero), swap is + `memory.swap.current`, `limited`. + - A limit that is found, with usage that cannot be read, leaves that part + out. It does not fall back to the machine's figures, which would be + wrong under the limit. + - No `0::` line (cgroup v1), no limit file, `max`, or a limit at or above + the machine's figure means no limit. - **Disk**, both adapters: free and total from one `statfs` of the data folder, the folder the cleanup reaper's `diskFree` reads. The `Filesystem` port gains `diskSpace(path)`, returning both, beside @@ -119,27 +141,35 @@ Simlock can actually use. ### 3. The sampler reads every 15 seconds, one read at a time -- The sampler starts when the daemon starts, before startup convergence, - so a starting daemon already has a reading. The first read is - immediate. After that, a `Clock` timer fires every 15 s, re-armed each - time it fires. The interval is fixed, with no config key. -- One read is in flight at most. A tick that finds one still running is - skipped. Each read is bounded at 10 s, after which that read counts as - failed. The `sysctl` call has its own process timeout below that. +- `DaemonServer` starts the sampler once the daemon holds its socket, + before startup convergence, so a starting daemon already has a reading. + It disposes the sampler on every way out: a stop, and a start that fails + after the sampler was built. A daemon that loses the start race must + exit, and an armed timer would keep it alive. +- The first read is immediate. After that, a `Clock` timer fires every + 15 s, re-armed each time it fires. The interval is fixed, with no config + key. +- One read is in flight at most. A tick that finds one is skipped. Each + read is bounded at 10 s: after that the reading treats it as failed, but + it still counts as in flight until it settles. A read that hangs for + ever (a stalled mount) then costs one stuck call, not a new one every + 15 s, and the age of the last reading keeps growing. - Busy % is the difference between this read's CPU counters and the previous successful read's. With no previous read, or after a change of `limited` or core count, CPU is absent from that reading. - A part absent from a read is absent from the reading. The sampler does not carry an older value forward: "unavailable" is shown rather than a - stale figure. A part that starts or stops failing is logged once per - change, at warn. + stale figure. A part that stops being readable, or becomes readable + again, is logged once per change, at warn. A part that has never been + read logs nothing. - The reading records when it was taken, on the sampler's clock. -- `dispose()` cancels the timer, and the daemon's stop sequence calls it. + `current()` reports its age, clamped at zero because the wall clock can + step back. ### 4. On the wire, `status.get` carries the age, not a timestamp `status.get` gains an optional `load` block, on the starting answer and the -running one alike: +running one alike, in both modes: ```json "load": { "ageMs": 4120, @@ -155,12 +185,13 @@ running one alike: where the operating system reports one. - The block is absent when no reading has finished yet, and when the module is not wired. -- Every number is finite and not negative, `busyPercent` is at most 100, - `cores` is above zero (a cgroup quota can allow half a core), and - `pressure` is one of `normal`, `warn` and `critical`. A gateway parses - every worker's answer, so the schema bounds all of it. The sampler - leaves out a part whose figures break these bounds, so a bad read never - fails `status.get`. +- `ageMs` is a whole number from 0 to one day. Every other number is + finite and not negative, `busyPercent` is at most 100, `cores` is above + zero (a cgroup quota can allow half a core), and `pressure` is one of + `normal`, `warn` and `critical`. A gateway parses every worker's answer, + so the schema bounds all of it. `fitHostLoad` caps `ageMs` at one day and + drops a part whose figures break the bounds, so a bad read never fails + `status.get`. - The protocol moves up by one. The CLI and the console read the block only when it is present. @@ -193,12 +224,15 @@ sequenceDiagram G-->>C: view without load ("no reading while disconnected") ``` -The worker registry owns the rule that a view not `connected` carries no -`load`. It drops `load` when a worker disconnects or turns incompatible, as -it already drops `installs` and `waiting`. A refresh never adds `load` to -such a view, even when a `status.get` answer lands after the disconnect. -A refresh that carries no `load` removes the old one rather than keeping -it. A reconnect fills `load` in again on the next refresh. +The worker registry owns the rule that only a `connected` view carries +`load`, through one helper: + +- It drops `load` when a worker disconnects or turns incompatible, as it + already drops `installs` and `waiting`, and when a worker connects anew, + so a new session never shows the old session's reading. +- A refresh never adds `load` to a view that is not `connected`. +- A refresh sets `load` to exactly what the refresh carries, and so + removes it when the worker sent none. ### 6. A gateway reports its own machine, and sums nothing @@ -228,8 +262,9 @@ bytes to each gateway refresh. - The disk pressure cleanup is unchanged. It still reads `diskFree` itself, on its own tick, against its own threshold. The two numbers come from the same `statfs` call, at different moments. -- An older worker sends no `load`. Its view has none, and the console and - the CLI show nothing for it. +- An older worker sends no `load`, and its view has none. The CLI prints + no Load line for it. The console says `Not reported`, as it does for + every other fact a worker did not send. ## Alternatives considered @@ -255,5 +290,10 @@ bytes to each gateway refresh. - **Detect containers and say `in container`.** Rejected: no reliable signal exists across container runtimes, and a systemd service limit is the same constraint without a container. See §2. +- **Only the leaf cgroup's limits.** Rejected: a container running systemd + puts the container's limit on a parent of Simlock's own cgroup, and + `MemoryMax=` is often set on a slice rather than a service. - **Node's `os.freemem()`.** Rejected: on macOS it counts only free pages, - so a healthy Mac looks nearly full. + so a healthy Mac looks nearly full. **`os.availableParallelism()` for the + machine's cores**: rejected, it follows CPU affinity and, in Node 22, a + cgroup quota, while `/proc/stat` covers every CPU. diff --git a/docs/internal/adr/README.md b/docs/internal/adr/README.md index 8b4185f35..b19039db7 100644 --- a/docs/internal/adr/README.md +++ b/docs/internal/adr/README.md @@ -67,4 +67,4 @@ the status is stale. | [0026](0026-agent-device-through-a-gateway-relays-over-the-uplink.md) | agent-device through a gateway: workers advertise capabilities, and the host link relays over the uplink | Accepted — not yet implemented; §2 narrowed by 0028 for relayed bodies | | [0027](0027-dependency-cruiser-checks-every-component-boundary.md) | dependency-cruiser checks every component's boundary and the module graph | Accepted | | [0028](0028-the-uplink-carries-byte-channels-and-small-messages-go-first.md) | The uplink carries byte channels beside its messages, and small messages go first | Accepted — partly implemented (§1–3) | -| [0026](0026-host-load-is-an-optional-module-whose-readings-ride-on-status.md) | Host load is an optional module whose readings ride on status | Proposed | +| [0027](0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md) | Host load is an optional module whose readings ride on status | Proposed | From 06aaa1abbb524ef696df0bd2148528af83e66333 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 13 Oct 2026 20:05:41 +0000 Subject: [PATCH 06/11] docs(adr): 0027 reads usage where the winning cgroup limit is Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0128DwBvv9jQ8CDpFZn7m9i8 --- ...d-is-an-optional-module-whose-readings-ride-on-status.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md b/docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md index 68e00f191..6f2485f83 100644 --- a/docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md +++ b/docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md @@ -117,7 +117,11 @@ both modes build the same probe. `FakeHostLoadProbe` serves tests. - **Linux limits.** Simlock's cgroup is the `0::` line of `/proc/self/cgroup`. Its limit files are read in that cgroup's folder under `/sys/fs/cgroup` and in every parent folder up to `/sys/fs/cgroup`, - and the lowest limit wins, because that is the one that binds. + and the lowest limit wins, because that is the one that binds. CPU limits + compare as cores. Usage is read in the folder that holds the winning + limit, because the limit caps that folder's usage, siblings included. A + cgroup path outside Simlock's cgroup namespace (a `..` segment) reads + `/sys/fs/cgroup` alone. - CPU: a `cpu.max` quota below the machine's cores is a limit. CPU is then `cpu.stat` `usage_usec` against elapsed time × quota ÷ period cores, `limited`. From 9de6f87509df8d6580799a3beccb36addbae3aa2 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 13 Oct 2026 20:08:53 +0000 Subject: [PATCH 07/11] docs(adr): 0027 breaks a cgroup limit tie toward the topmost folder Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0128DwBvv9jQ8CDpFZn7m9i8 --- ...load-is-an-optional-module-whose-readings-ride-on-status.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md b/docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md index 6f2485f83..4ec599fd7 100644 --- a/docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md +++ b/docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md @@ -119,7 +119,8 @@ both modes build the same probe. `FakeHostLoadProbe` serves tests. under `/sys/fs/cgroup` and in every parent folder up to `/sys/fs/cgroup`, and the lowest limit wins, because that is the one that binds. CPU limits compare as cores. Usage is read in the folder that holds the winning - limit, because the limit caps that folder's usage, siblings included. A + limit, because the limit caps that folder's usage, siblings included. On + a tie, the topmost of the tied folders wins, for the same reason. A cgroup path outside Simlock's cgroup namespace (a `..` segment) reads `/sys/fs/cgroup` alone. - CPU: a `cpu.max` quota below the machine's cores is a limit. CPU is From fe77b2ece0b8223ed4570859bbd1c0d374681899 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 13 Oct 2026 20:10:46 +0000 Subject: [PATCH 08/11] docs(adr): accept 0027 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0128DwBvv9jQ8CDpFZn7m9i8 --- ...-load-is-an-optional-module-whose-readings-ride-on-status.md | 2 +- docs/internal/adr/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md b/docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md index 4ec599fd7..387adf6df 100644 --- a/docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md +++ b/docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md @@ -1,6 +1,6 @@ # 0027. Host load is an optional module whose readings ride on status -- **Status:** Proposed +- **Status:** Accepted — not yet implemented - **Date:** 2026-10-13 - **Issue:** [#527](https://github.com/callstackincubator/simlock/issues/527) - **Supersedes:** nothing. Adds to [ADR diff --git a/docs/internal/adr/README.md b/docs/internal/adr/README.md index b19039db7..d3412a011 100644 --- a/docs/internal/adr/README.md +++ b/docs/internal/adr/README.md @@ -67,4 +67,4 @@ the status is stale. | [0026](0026-agent-device-through-a-gateway-relays-over-the-uplink.md) | agent-device through a gateway: workers advertise capabilities, and the host link relays over the uplink | Accepted — not yet implemented; §2 narrowed by 0028 for relayed bodies | | [0027](0027-dependency-cruiser-checks-every-component-boundary.md) | dependency-cruiser checks every component's boundary and the module graph | Accepted | | [0028](0028-the-uplink-carries-byte-channels-and-small-messages-go-first.md) | The uplink carries byte channels beside its messages, and small messages go first | Accepted — partly implemented (§1–3) | -| [0027](0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md) | Host load is an optional module whose readings ride on status | Proposed | +| [0027](0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md) | Host load is an optional module whose readings ride on status | Accepted — not yet implemented | From 8b0ee42221e47257581013b78a29c79f5ac494c1 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 14 Oct 2026 16:36:33 +0200 Subject: [PATCH 09/11] docs(adr): 0028 drops the boundary-test sentence and states the protocol move truthfully --- ...al-module-whose-readings-ride-on-status.md | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md b/docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md index 387adf6df..dab9048d9 100644 --- a/docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md +++ b/docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md @@ -1,4 +1,4 @@ -# 0027. Host load is an optional module whose readings ride on status +# 0028. Host load is an optional module whose readings ride on status - **Status:** Accepted — not yet implemented - **Date:** 2026-10-13 @@ -45,10 +45,10 @@ already polls `GET /v1/workers` every second. reading's types. It depends on ports only: `Clock`, `Logger` and the new `HostLoadProbe` (§2). It imports nothing from `core`, `leasing`, `gateway`, `drivers`, `contract` or any frontend, and none of `core`, `leasing`, -`drivers`, `gateway` or `contract` imports it. Lint enforces both -directions. A boundary test, modelled on -`src/core/warm-pool/boundary.test.ts`, keeps every file outside the -directory off everything but `index.ts`. +`drivers`, `gateway` or `contract` imports it. dependency-cruiser (ADR 0027) +checks both +directions, and keeps every file outside the directory off everything but +`index.ts`. It sits outside `src/core/` because a gateway runs it too, and a gateway has no core. @@ -197,8 +197,10 @@ running one alike, in both modes: so the schema bounds all of it. `fitHostLoad` caps `ageMs` at one day and drops a part whose figures break the bounds, so a bad read never fails `status.get`. -- The protocol moves up by one. The CLI and the console read the block - only when it is present. +- The protocol version moves: `PROTOCOL_VERSION_RANGE` in + `src/contract/protocol.ts` moves both ends, because the bump leaves no + shim behind. The CLI and the console read the block only when it is + present. ### 5. In a worker view, the age becomes a time on the view holder's clock @@ -267,9 +269,12 @@ bytes to each gateway refresh. - The disk pressure cleanup is unchanged. It still reads `diskFree` itself, on its own tick, against its own threshold. The two numbers come from the same `statfs` call, at different moments. -- An older worker sends no `load`, and its view has none. The CLI prints - no Load line for it. The console says `Not reported`, as it does for - every other fact a worker did not send. +- A worker on the older protocol version is incompatible (ADR 0008 §10) + and does not connect until it is upgraded, so every connected worker + sends `load` when it has a reading. A worker with no reading yet sends + none, and its view has none. The CLI prints no Load line for it. The + console says `Not reported`, as it does for every other fact a worker + did not send. ## Alternatives considered From 61c68c18c1cdd6b595e4fbf44aecd40e6c566fd9 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 14 Oct 2026 16:45:50 +0200 Subject: [PATCH 10/11] docs(adr): number host load 0028, after main took 0027 --- ...load-is-an-optional-module-whose-readings-ride-on-status.md} | 0 docs/internal/adr/README.md | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename docs/internal/adr/{0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md => 0028-host-load-is-an-optional-module-whose-readings-ride-on-status.md} (100%) diff --git a/docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md b/docs/internal/adr/0028-host-load-is-an-optional-module-whose-readings-ride-on-status.md similarity index 100% rename from docs/internal/adr/0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md rename to docs/internal/adr/0028-host-load-is-an-optional-module-whose-readings-ride-on-status.md diff --git a/docs/internal/adr/README.md b/docs/internal/adr/README.md index d3412a011..6f56ea72c 100644 --- a/docs/internal/adr/README.md +++ b/docs/internal/adr/README.md @@ -67,4 +67,4 @@ the status is stale. | [0026](0026-agent-device-through-a-gateway-relays-over-the-uplink.md) | agent-device through a gateway: workers advertise capabilities, and the host link relays over the uplink | Accepted — not yet implemented; §2 narrowed by 0028 for relayed bodies | | [0027](0027-dependency-cruiser-checks-every-component-boundary.md) | dependency-cruiser checks every component's boundary and the module graph | Accepted | | [0028](0028-the-uplink-carries-byte-channels-and-small-messages-go-first.md) | The uplink carries byte channels beside its messages, and small messages go first | Accepted — partly implemented (§1–3) | -| [0027](0027-host-load-is-an-optional-module-whose-readings-ride-on-status.md) | Host load is an optional module whose readings ride on status | Accepted — not yet implemented | +| [0028](0028-host-load-is-an-optional-module-whose-readings-ride-on-status.md) | Host load is an optional module whose readings ride on status | Accepted — not yet implemented | From c990cef11376e5780f932b2ef808fbd08b0612b1 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Wed, 14 Oct 2026 18:13:29 +0200 Subject: [PATCH 11/11] docs(adr): number host load 0029, after main took 0028 --- ...load-is-an-optional-module-whose-readings-ride-on-status.md} | 2 +- docs/internal/adr/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename docs/internal/adr/{0028-host-load-is-an-optional-module-whose-readings-ride-on-status.md => 0029-host-load-is-an-optional-module-whose-readings-ride-on-status.md} (99%) diff --git a/docs/internal/adr/0028-host-load-is-an-optional-module-whose-readings-ride-on-status.md b/docs/internal/adr/0029-host-load-is-an-optional-module-whose-readings-ride-on-status.md similarity index 99% rename from docs/internal/adr/0028-host-load-is-an-optional-module-whose-readings-ride-on-status.md rename to docs/internal/adr/0029-host-load-is-an-optional-module-whose-readings-ride-on-status.md index dab9048d9..1a5e937b8 100644 --- a/docs/internal/adr/0028-host-load-is-an-optional-module-whose-readings-ride-on-status.md +++ b/docs/internal/adr/0029-host-load-is-an-optional-module-whose-readings-ride-on-status.md @@ -1,4 +1,4 @@ -# 0028. Host load is an optional module whose readings ride on status +# 0029. Host load is an optional module whose readings ride on status - **Status:** Accepted — not yet implemented - **Date:** 2026-10-13 diff --git a/docs/internal/adr/README.md b/docs/internal/adr/README.md index 6f56ea72c..b7d426418 100644 --- a/docs/internal/adr/README.md +++ b/docs/internal/adr/README.md @@ -67,4 +67,4 @@ the status is stale. | [0026](0026-agent-device-through-a-gateway-relays-over-the-uplink.md) | agent-device through a gateway: workers advertise capabilities, and the host link relays over the uplink | Accepted — not yet implemented; §2 narrowed by 0028 for relayed bodies | | [0027](0027-dependency-cruiser-checks-every-component-boundary.md) | dependency-cruiser checks every component's boundary and the module graph | Accepted | | [0028](0028-the-uplink-carries-byte-channels-and-small-messages-go-first.md) | The uplink carries byte channels beside its messages, and small messages go first | Accepted — partly implemented (§1–3) | -| [0028](0028-host-load-is-an-optional-module-whose-readings-ride-on-status.md) | Host load is an optional module whose readings ride on status | Accepted — not yet implemented | +| [0029](0029-host-load-is-an-optional-module-whose-readings-ride-on-status.md) | Host load is an optional module whose readings ride on status | Accepted — not yet implemented |