Repository navigation
Commit 7ea7080
committed
fix(file-safety, utils): give each write its own staging name, and stage exclusively at the target's mode
Three shapes in this unit's own delta, each measured against 90bbdad before and after.
safeWriteText staged into a fixed directory name, .file-safety-staging, under a path the workspace
controls. Two self-staged writes to one file therefore shared one staging place, and an entry left
there by a crashed or interrupted writer became a permanent blocker for every later write to that
file: mkdirSync could not create it, and the recovery unlink - non-recursive - could not remove a
non-empty directory. The staging directory now carries this call's generated name
(.file-safety-staging_<ts>_<rand>), the same shape safeWriteJson already uses for its staging file, so
cleanup can only ever reach this write's own directory. A caller-supplied tempPath is untouched: that
path is the caller's by contract, and the two comments that claimed the fixed name was safe are
rewritten to say what the code now guarantees.
safeWriteJson streamed the staged payload through fsSync.createWriteStream(targetPath, { encoding }) -
an ordinary create-or-truncate open of a generated path inside a directory the workspace controls. It
is now opened with openSync(targetPath, "wx", stageMode), where stageMode is the mode of the file being
replaced (0o644 when there is no target yet), plus a best-effort fchmodSync so a 0o664 or 0o666 target
is not staged as 0o644 under the common umask 022 and lose group write through the commit rename. The
descriptor is handed to the write stream, which owns closing it. This is the rule safeWriteText already
follows (openSync(tempPath, "w", targetMode) + fchmodSync, at safeWriteText.ts:264-286), and 85
call sites sit behind this writer (McpHub, TaskHistoryStore, caches, FileContextTracker), so target-mode
preservation - not a blanket 0o600 - is what can be applied without silently republishing every cached
JSON with a new mode.
Tests. safeWriteText.spec.ts gains a describe with 3 rows (generated name shape, one directory per
concurrent write, caller-supplied tempPath left at the caller's name). safeWriteJson.test.ts gains 4
rows: refusal when the generated staging path is already occupied, mode inheritance from the target,
0o644 default for a fresh target, and a real-link row - junction on win32, directory symlink elsewhere,
with a capability probe that records EPERM/ENOSYS/ENOTSUP/EOPNOTSUPP instead of asserting a resolution
it never built - which pins the lock on the referent of an operating-system alias rather than a mocked
realpath. The backup-deletion row is retitled to what it actually measures and now asserts the
committed content, the orphaned backup, and that no staging file was left behind; the comment naming
the deliberately-absent guarded-write titles is rewritten to name both json-layer rows and to record
that the rollback row is inherited from upstream main, not new here.
Red-first then controls, all on pinned node v22.23.4 (.tmp-node22; box node v25.9.0 unused), from the
worktree's own junction install:
- safeWriteText lane, head baseline 45 passed (45); new rows against unmodified production 3 failed /
45 passed (48); with the fix 48 passed (48). Negative control - staging name swapped back to the fixed
name - exits 1 with exactly those 3 rows failing and nothing else; file restored byte-identical
(18223 B, sha256 ddd301ba1e7e9b97...) and re-run 48/48.
- safeWriteJson lane, head baseline 62 passed (62), 0 skipped (the PR body's "38 passed, 1 skipped" is
stale at this head); new rows against unmodified production 3 failed / 63 passed (66); with the fix 66
passed (66). Control A (production helper reverted to the head blob) fails exactly those 3 staging
rows. Control B (make the backup-delete catch re-throw) fails 4 rows including the retitled
backup-deletion row. Control C (lock keyed on the caller-named path) fails 3 rows including the
real-link row. Each control restored byte-identical (safeWriteJson.ts sha256 ffd1d2aa208eb534...).
- guardedWrite lane: 1 new row, and its control is the point. PR Zoo-Code-Org#2359 carries an open CodeRabbit row
(PRRT_kwDOSK-bwM61HM7S) about a reserved chain promise left unhandled for an event-loop turn; this
unit has no reserved promise (enqueue composes with prev.then(fn, fn), guardedWrite.ts:87), so the
row pins that and the control injects the reported shape - a rejection with no handler for one
macrotask - into the cancellation throw: 1 failed / 40 passed (41), the recorded reason exactly the
injected error, and only that row fails. Restored byte-identical (16431 B, sha256 8cda11b309fffff9...)
and re-run 41/41.
CodeRabbit rows triaged against this head rather than carried over: safeWriteJson.test.ts:829 and :984
are answered by the two retitled/extended rows; safeWriteJson.ts:72 is answered by the wx + target-mode
staging above; safeWriteJson.ts:271 (importExport export-failure surfacing) and safeWriteJson.ts:225
(which path the lock key takes) stay open - importExport.ts:350-353 still swallows the export failure
for the UI to render and no settings_export* key exists in any of the 18 locale directories, and the
lock-key choice is an owner-level contract question with the four other copies of this writer.
Gates (worktree wt-s3-1405, node v22.23.4): prettier --check on all five touched files exit 0;
tsc --noEmit -p tsconfig.json exit 0 with 0 error lines, sensitivity control (one deliberate type error
in src) returns exit 2 with the error and back to 0 on removal; eslint --max-warnings=0 on the five
changed files exit 0 with 0 messages each, and the same run over all of src reports 1169 files 0
messages; src/eslint-suppressions.json unchanged in this commit (30910 B, 265 files / 265 rule-entries
/ 2869 counts, against merge base 73931fe 265 / 265 / 2870), and none of the five files has an entry
at the merge base or now, so no count rises.1 parent 90bbdad commit 7ea7080
5 files changed
Lines changed: 369 additions & 28 deletions
File tree
- src
- core/tools/__tests__
- services/file-safety
- __tests__
- utils
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
512 | 512 | | |
513 | 513 | | |
514 | 514 | | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
515 | 566 | | |
516 | 567 | | |
517 | 568 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
189 | 189 | | |
190 | 190 | | |
191 | 191 | | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
192 | 258 | | |
193 | 259 | | |
194 | 260 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
73 | | - | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
74 | 76 | | |
75 | | - | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
76 | 83 | | |
77 | | - | |
78 | | - | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
79 | 87 | | |
80 | 88 | | |
81 | 89 | | |
| |||
86 | 94 | | |
87 | 95 | | |
88 | 96 | | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
94 | 103 | | |
95 | 104 | | |
96 | 105 | | |
97 | 106 | | |
98 | | - | |
| 107 | + | |
99 | 108 | | |
100 | 109 | | |
101 | 110 | | |
| |||
233 | 242 | | |
234 | 243 | | |
235 | 244 | | |
236 | | - | |
| 245 | + | |
| 246 | + | |
237 | 247 | | |
238 | 248 | | |
239 | 249 | | |
| |||
399 | 409 | | |
400 | 410 | | |
401 | 411 | | |
402 | | - | |
403 | | - | |
| 412 | + | |
| 413 | + | |
404 | 414 | | |
405 | 415 | | |
406 | 416 | | |
| |||
426 | 436 | | |
427 | 437 | | |
428 | 438 | | |
429 | | - | |
430 | | - | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
431 | 442 | | |
432 | 443 | | |
433 | 444 | | |
| |||
0 commit comments