Repository navigation
fix(#102): make the storage sweep fail closed on liveness (REV-33) - #109
Merged
Merged
Conversation
Post-merge audit of PR #106 found that the sweep's central safety claim — a live session's data dir is never reclaimed — did not hold, in three places. All three were the same mistake: "cannot prove alive" was read as "proved dead". - Owner marker probed with the wrong semantics. `writeOwnerMarker()` stores `process.pid`, but `siblingVerdict()` fed it to `pidAlive()`, which answers a process-GROUP question (`/proc` `pgrp === pid`, else `kill(-pid, 0)`). Task children are spawned `detached`, so their pid *is* their pgid and the group semantics are load-bearing for them (#6) — but a pi session process is usually a member of its shell's group, not a leader, so a live session was reported dead and its whole data dir removed past the age gate. New private `processAlive(pid)` (direct-pid: `kill(pid, 0)`, EPERM = alive, Linux `Z` = dead) is used for the marker only; `pidAlive()` is untouched. - An owner marker that exists but cannot be READ was treated as "no owner": the `readFileSync` catch collapsed every failure (EACCES/EIO, marker became a directory, dangling symlink) into "no marker". Marker presence is now evidence — non-plain-file or unreadable ⇒ `unreadable_owner`, and symlinks are never followed. - Snapshot pids were unvalidated, so `pid: 0` answered "alive" on macOS (`kill(-0, 0)` = own group) and "dead" on Linux. Marker and snapshot pids must now be positive integers or the directory counts as unknown. Deliberately NOT done: giving "non-terminal snapshot with no pid" a permanent protection. A delayed `pending` task has no pid and `shutdown()` leaves such snapshots on disk, so that rule (my own first patch) pinned every directory a session ever quit in — reproduced as "still refused 10 years later" in review, and reverted. Such snapshots carry no liveness evidence; the marker and the age gate decide, which is exactly why the marker exists. Also: reason vocabulary typed honestly (`not_a_data_dir`, `delete_failed`), a factually wrong phase-2 comment corrected, a drift-proof contract citation, and one vacuous test assertion (a file that was never created) replaced with real ones. Verification: storage suite 38/38 (32 at merge, +6); full `npm test` 486 / 457 pass / 16 fail / 13 skip vs baseline origin/main 480 / 451 / 16 / 13 — identical failing set (diff of names is empty; pre-existing macOS environment failures). `npm run typecheck` is unavailable in this worktree (no `typescript` installed); the patch was checked with the pinned TS 5.9.3 against a baseline copy: 0 diagnostics on both. Mutations (revert each fix ⇒ exactly one red test) are recorded in docs/reports/storage-lifecycle.md §10.
…e (REV-33) Second independent review round on the previous commit found two defects that commit itself had introduced — both in the "be stricter" direction, which is why they matter: - The pid validation was applied to *terminal* snapshots too. A finished task's pid is never probed (it is only an eviction candidate), so an odd pid there turned the whole directory into `unreadable` — its artifacts became un-evictable and the quota reported `overQuota` forever (measured: 10390 B / 6000 B, 0 evictions). The check now applies to non-terminal snapshots only. - `isPid` had no `pid_t` upper bound: `Number.isInteger(1e21)` is true, but `process.kill(1e21, 0)` throws ERR_INVALID_ARG_TYPE, which `processAlive()` read as "dead" — i.e. a garbage marker pid could still get a directory reclaimed. `isPid` is now bounded by `0x7fffffff`, and `processAlive()` answers *alive* for a pid no process can have (fail-closed, second line). Also fixed, found while probing the same code: a marker that is a FIFO made `readFileSync` block forever, hanging `configure()`. The survey now decides from the dirent and never opens a marker that is not a plain file (it lands on `unreadable_owner`). Verification: storage suite 40/40 (32 at merge, +8); full `npm test` 488 / 459 pass / 16 fail / 13 skip vs baseline 480 / 451 / 16 / 13, failing-name set identical. Mutations M8 (terminal pids re-included) and M9 (unbounded `isPid`) each turn exactly one test red; both are recorded in docs/reports/storage-lifecycle.md §10.4.
27 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #102
先说清楚:这个 PR 不是重做 #102
派发时的前提是「#102 未实现,main =
985a083」。实际情况:origin/main早已包含PR #106(
feat(#102): storage lifecycle — cross-dataDir reclamation + global byte quota,合入提交d0407d1),issue #102 也已 closed。所以本子树先 fast-forward 到origin/main,没有重写一份实现,而是对已合入的实现做一次独立、对抗式的复核,专打它自己的核心承诺:
复核结论:这条承诺在三个地方不成立,而且三个都是同一类错误——把「无法证明活着」读成「证明死了」。
本 PR 只修这三处,契约记 REV-33(
docs/CONTRACT.md§25.9)。三处 fail-open(都带复现)
1. owner marker 用错了判活语义(最严重)
writeOwnerMarker()写的是process.pid,但siblingVerdict()把它喂给了pidAlive(),而pidAlive()答的是进程组问题(
/proc里pgrp === pid,否则kill(-pid, 0))。任务子进程经
detached: true启动,pid 就是 pgid,所以组语义对任务是对的(也是 #6/REV-28c 刻意要的:组长退出但组里还有人要算活)。但 pi session 进程通常是 shell 进程组的成员,不是组长:
接到真实路径上(fixture 全在
mkdtemp):为什么合入时 32 例测试全绿:那条测试把 marker 的 pid 填成
registry.submit()的任务 pid(detached ⇒ 必然是组长),
pidAlive()恰好返回 true——理由错、结果对。生产永远不会写这种值。修法:新增私有
processAlive(pid)(进程语义:kill(pid, 0);EPERM= 存在但不是我 的权限 → 算活;Linux 上再查
/proc/<pid>/stat,Z僵尸算死),只给 marker 用;pidAlive()一字未改。2. 「marker 存在但读不出来」被吞成「没有 marker」
旧代码
try { markerRaw = readFileSync(...) } catch { markerRaw = undefined }:EACCES/EIO、marker 变成目录、悬空软链,全都被归成「无 owner」→ 目录可删。现在 marker 的存在本身就是证据:非普通文件或读不出 →
unreadable_owner并跳过(本模块从不跟随软链,即使软链指向一个能读的 JSON 也不算证据)。3. 快照的 pid 没有校验:同一个快照两个平台相反结论
pid: 0在 macOS 上是kill(-0, 0)= 「我自己的进程组」→ 判活(永不回收);在 Linux 上pgrp === 0永不匹配 → 判死(可回收)。现在 marker 与快照的 pid 都必须是正整数,否则整个目录记
unreadable_snapshot/unreadable_owner——不猜。4. 复核我自己的补丁时又抓到两处(都是我第一版引入的)
overQuota永远为真(10390 B / 6000 B,0 淘汰)isPid没有pid_t上界kill(1e21)抛ERR_INVALID_ARG_TYPE→ 读成「已死」→ 目录被回收isPid限<= 0x7fffffff;processAlive对不可探测 pid 返回 true(保留目录)附带发现并修掉:marker 若是 FIFO,
readFileSync会永久阻塞(= 挂死configure())。现在先看 dirent再决定读不读,非常规文件(FIFO/socket/目录/软链)一律不读,按
unreadable_owner处理。有意不做的事(附复核证据)
不给「非终态快照没有 pid」加永久保护。 我的第一版补丁加了这条(
unproven_task),复核把它打回来了:延迟
pending任务在 spawn 前本来就没有 pid,而shutdown()故意把这类快照留在盘上——于是每个「排过延迟任务后退出」的目录会永远无法回收(复核实测:10 年后仍然拒绝回收)。这是把一个罕见的
fail-open 换成一个常见且无界的泄漏。已回退:这类快照不提供判活证据(既不算活也不算死),
由 owner marker + 年龄闸定生死——这正是 marker 存在的理由。回退决定有回归断言钉住。
顺带修的文档/测试真实性问题
SweepSkipReason补齐not_a_data_dir/delete_failed(代码一直在发,类型与契约表格缺)。foreign_content目录的字节不进配额」对「异物 + 可解析快照」的目录是假的,已改对。CONTRACT.md里registry.ts:1374-1377的/proc判活引用早已漂移成配额记账行 → 改成按函数名引用。existsSync(join(t.current, "a.json"))里的a在该 fixture 中从未被创建,永远为真→ 改成断言真正被淘汰的
older.json+ 对照newer.json仍在。$HOME下」(原先只是注释里的承诺)。验证
tests/a-storage-sweep.test.ts:40/40(合入时 32 例 → +8)。npm test:488 / 459 pass / 16 fail / 13 skip;基线(git archive origin/main独立展开)480 / 451 / 16 / 13,失败用例名集合 diff 为空(16 条既有 macOS 环境性失败,CI 是 Ubuntu)。
npm run typecheck在本 worktree 不可用(typescript未安装,无node_modules);已用仓库锁定的TS 5.9.3 对 baseline 与 patch 各跑一次
--noEmit:两边各 0 诊断,且注入const bad: number = "x"能正常报错(证明 tsc 真的在检查)。
读不出 marker 当「无 owner」→ 红;快照 pid 回退成「任何 number」→ 红;把
pending重新当永久保护 → 红;pid 校验套到终态快照 → 红;
isPid去掉pid_t上界 → 红;配额用例改回软模式 → 红。详见报告 §10.4。第二轮抓出「我引入的配额回归」+「
pid_t上界」「FIFO 挂死」。两轮都各自给出撤掉即红的实测。git diff origin/main..HEAD只动 7 个文件(3 代码/测试 + 4 文档),无其它夹带。不做 / 无回退开关
async_status返回形状不变,§12 冻结),不改默认值(两个开关仍默认关闭),不改
prune()/reapOrphans(),不新增 env。PI_ASYNC_SWEEP_SIBLINGS/PI_ASYNC_TOTAL_BYTES置回默认0即回到 0.5.x 行为。npm publish、package.json版本不动;未/reload任何正在运行的扩展。交付报告:
docs/reports/storage-lifecycle.md§10(逐项判据、file:line、复现输出、变异记录、以及复核过但有意不改的项与理由)。