From 01fd7b99225b0867e84cd10c479b9ce21a5fa0e0 Mon Sep 17 00:00:00 2001 From: yuez Date: Fri, 30 Oct 2026 16:16:47 +0800 Subject: [PATCH] fix(daemon): sample disk usage on the filesystems pi2d uses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The status tile read `—` on a fresh machine: the sampler ran `statfs` on the daemon's state dir, which is created lazily on the first instance log, so the call failed with ENOENT and `diskUsedBytes`/`diskTotalBytes`/`diskPercent` all came back null — `/info` then dropped `disk` altogether and the failure was indistinguishable from a full disk. `probeMount()` now walks up to the nearest existing ancestor and then to the filesystem boundary (`st_dev` change), so a sample describes a volume, not a directory. Candidates are pi2d's own write targets — `/` (the home volume on macOS, where `/` is the sealed read-only system volume), the pi session store and the visible workspace roots — never a mount-table dump: an unrelated or hung network mount must not stall the sampler. Rows are deduped by `st_dev`, sorted most-constrained first, capped at four, and reported as `system.disks[]` with the legacy scalars aliasing the worst row. A sample that yields nothing now carries `diskError` instead of silent nulls. The status tile shows the worst filesystem and, when several are in play, one quiet row per mount instead of a single percentage that hides the full volume. --- docs/protocol.md | 13 +- src/cli/wiring.ts | 13 +- src/core/sysinfo.ts | 218 ++++++++++++++++++++-- test/core/sysinfo.test.ts | 130 ++++++++++++- test/daemon/harness.ts | 3 + test/daemon/http.test.ts | 30 ++- web/src/features/status/metrics-block.tsx | 64 ++++++- web/src/lib/api.ts | 15 ++ web/src/styles/pages/status.css | 19 ++ 9 files changed, 474 insertions(+), 31 deletions(-) diff --git a/docs/protocol.md b/docs/protocol.md index 70de279..7348a4c 100644 --- a/docs/protocol.md +++ b/docs/protocol.md @@ -93,7 +93,7 @@ Plugin obligations: | Endpoint | Notes | |---|---| -| `GET /api/v1/info` | `{v, daemonId, machineId, platform, hostname, os, version, uptimeMs, pid, port, capabilities, limits}`; additive `cpu`, `mem`, `disk` (integer percent, each omitted when unknown), `cpuCores`, `loadAvg` | +| `GET /api/v1/info` | `{v, daemonId, machineId, platform, hostname, os, version, uptimeMs, pid, port, capabilities, limits}`; additive `cpu`, `mem`, `disk` (integer percent of the **most constrained** filesystem, each omitted when unknown), `cpuCores`, `loadAvg` | | `GET /api/v1/health` | `{ok, items:[{name, ok, detail}]}` | | `GET /api/v1/workspaces` | `WorkspaceInfo[]` (registry + disk index + instance state) | | `GET /api/v1/workspaces/:id/sessions` | relayed `workspace.query{kind:"sessions"}` (row fields below) | @@ -127,8 +127,15 @@ figures sampled by the daemon itself: | `cpuCores` | logical core count | | `loadAvg` | 3-tuple `[1m, 5m, 15m]`, or `null` where the OS has no load average | | `memUsedBytes`, `memTotalBytes`, `memPercent` | **host** memory; `memPercent` is an integer. `totalRssBytes` stays the soft-limit input — it is instance RSS, not a host figure | -| `diskPath` | the filesystem the sample describes: the daemon's state dir | -| `diskUsedBytes`, `diskTotalBytes`, `diskPercent` | `diskPercent` integer, nullable; the byte counts are `null` when the stat fails | +| `diskPath` | mount point of the most constrained filesystem (`disks[0]`), e.g. `/` or `/home`; the legacy scalar fields below alias that row | +| `disks` | `DiskUsage[]`, at most 4, **most constrained first**. One row per distinct filesystem (`st_dev`) among the daemon's own write targets: `/` (the home volume on macOS), the pi session store and the visible (non-`secondary`) workspace roots. Not a mount-table dump — an unrelated or hung network mount is never touched | +| `disks[].mountPoint`, `disks[].fsType?`, `disks[].totalBytes`, `disks[].usedBytes`, `disks[].percent` | `mountPoint` is the filesystem boundary (never the sampled path), `fsType` a best-effort label (`ext4`, `btrfs`, …) omitted when unknown, `percent` an integer 0–100 | +| `diskUsedBytes`, `diskTotalBytes`, `diskPercent` | the worst row, for clients that predate `disks`; `null` when nothing could be sampled | +| `diskError` | present **only** when `disks` is empty — `": "`, so a failed sample is distinguishable from a full disk instead of silently rendering `—` | + +A candidate path that does not exist yet (the state dir is created lazily) no longer +blanks the sample: the probe walks up to the nearest existing ancestor and then to the +filesystem boundary, so `diskPercent` describes the volume, not a directory. `GET /api/v1/models` no longer requires `workspaceId`. With one, the read is relayed as before and may spawn that workspace's instance — the chat picker's diff --git a/src/cli/wiring.ts b/src/cli/wiring.ts index 6566c61..969cd1f 100644 --- a/src/cli/wiring.ts +++ b/src/cli/wiring.ts @@ -145,7 +145,18 @@ export async function wireDaemon(options: WireOptions = {}): Promise