Repository navigation
Commit 578d3b1
fix(cli): stage os skills writes and swap them in by rename, recording last (#35495)
Fixes #35079
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 改了什么:`os skills install` / `update` 的写盘改为「先写进隐藏的暂存目录 → 逐个改名换入 →
最后改名落记录」,任何一步失败都按倒序撤回已做的改名。
- 为什么改:原先先删旧目录再写新文件、最后写记录;中途失败(磁盘满、权限、文件被占用)会让旧记录指向已删或半写的目录,下一次 `update`
必须加 `--force` 才能恢复。
- 风险与回滚:只动 `applySkillsWrite` 一处写路径;残留风险是进程被强杀(SIGKILL/断电)恰好落在纯改名的换入阶段,见
Acceptance notes。回滚即还原本 PR。
- 先看:`packages/cli/src/utils/skills-install-state.ts` 的
`applySkillsWrite` try/catch 与 `undoSwap`;其次
`packages/cli/test/skills-commands.test.ts` 末尾的新 describe 与顶部的 `node:fs`
故障注入。
- 两分钟验证:`pnpm --filter @objectstack/cli exec vitest run --project unit
--maxWorkers=2 test/skills-commands.test.ts`,预期 34/34 通过(新增 4 例在修复前全红)。
- 需要你做的:无,席位落地。
## What changes
`applySkillsWrite` (`packages/cli/src/utils/skills-install-state.ts`)
now writes in three phases, and the module header documents them:
1. **Stage.** Every new skill directory is written under `new/` inside
one staging directory created in the skills directory itself
(`SKILLS_STAGING_PREFIX` = `.objectstack-skills-staging-`, via
`mkdtempSync`). Same filesystem as the targets, so every later step is a
rename.
2. **Swap.** Each dropped skill is renamed aside into the staging
directory's `old/`; each catalog skill's existing directory (recorded,
or a conflict `install --force` adopted) is renamed aside, then its
staged copy renamed into place. Rename-aside rather than rename-over,
because a directory cannot be renamed over a non-empty one (POSIX
`ENOTEMPTY`, Windows refuses outright).
3. **Record.** The record is written into the staging directory and
renamed over `.objectstack-skills.json`: a file rename over a file is
atomic on POSIX and replaces on Windows.
Any throw in 1–3 undoes the renames already done, newest first, removes
the staging directory and rethrows; the command prints the failure and
exits 1 as before. If an undo itself fails, the error says which steps
could not be undone and where the previous copies are, and the staging
directory is kept.
**What "the old skills and the record stay usable" means here: all or
nothing across the skills.** A failure partway restores every skill, not
only the one that failed, so the record never names a directory the
failure removed nor one at a version it does not record. Per-skill would
leave a mixed catalog under a record of one version, which `update` then
refuses as changed locally: the defect this card files.
**Leftovers.** The staging directory is hidden. `skillDirectories`
already skips names starting with a dot, and `SKILL_NAME` requires a
first letter or digit, so the plan, `status` and `inspectSkillsDir`
never read it as a skill and no catalog skill can collide with it. Its
contents sit one level deeper (`new/NAME`, `old/NAME`), so an agent
loading `DIR/NAME/SKILL.md` does not load them either. The next write
removes any `.objectstack-skills-staging-*` directory a killed run left
(`removeStagingLeftovers`). Removing the staging directory after a
successful write is best effort: a failure there is left for the next
write and never masks the write's outcome.
All path judgments (`underRoot`) still happen before the first write,
unchanged, and the record is computed before the first write too.
## Tests
The pin is a new top-level `describe` in the existing shared harness
`packages/cli/test/skills-commands.test.ts` (the shared-harness rule).
Failure injection: a hoisted `vi.mock('node:fs')` that passes everything
through except the one `writeFileSync` / `renameSync` call `fault.when`
matches, which throws `ENOSPC` once and disarms. Unarmed in every other
test.
- `it.each` over three failure points during `update` V1 → V2: a file of
the second skill is written; the second skill is moved into place; the
record is written after every skill is in place. Each asserts exit 1,
the fault fired exactly once, and `ENOSPC` reaches the output. It then
asserts V1 is intact (every file's bytes, record version, `status
--version V1 --check --json` exit 0 with nothing missing, modified or
extra, nothing hidden beside the record), and that a plain `update
--version V2` with no `--force` lands V2 with no leftovers.
- A staging directory a killed run left behind is not reported by
`status` and is removed by the next plain `update`.
Red before the fix: the new test commit 060bd8f, run against the
unmodified source. Ablation after the fix: source restored to the base
6d833b5 with the fix committed, then put back with `git checkout HEAD`,
the blob hash checked against HEAD, `git diff HEAD` empty.
<details><summary>Red before (060bd8f, unmodified source) and ablation
(b555879, source restored to 6d833b5): 4 failed | 30 passed (34), both
runs</summary>
```text
× the disk fails as a file of the second skill is written ... AssertionError: expected 'data v2' to be 'data v1'
× the disk fails as the second skill is moved into place ... AssertionError: expected +0 to be 1 (the old code has no rename, so the fault never fires and the update "succeeds")
× the disk fails as the record is written, after every skill is in place AssertionError: expected 'data v2' to be 'data v1'
× a staging directory a killed run left behind ... status reports the leftover as a skill
Test Files 1 failed (1) / Tests 4 failed | 30 passed (34)
os-verify-lock: VERDICT command-exit 1
```
In the first and third cases the old code had already rewritten
`objectstack-data` at V2 under the V1 record, which is exactly the state
`update` refuses without `--force`. The fourth case's red on the old
source comes from the missing `SKILLS_STAGING_PREFIX` export, which
makes the leftover's name visible: it shows that the export is needed,
not that the old code mishandled a hidden leftover.
Ablation restore proof: `git checkout HEAD --
packages/cli/src/utils/skills-install-state.ts`, then `git hash-object`
= 36b9524 = the HEAD blob, `git diff
HEAD` 0 lines, `git status --porcelain` empty.
</details>
## Acceptance notes
- **Residual window: a hard kill inside the swap phase.** A thrown
failure is fully rolled back. A process killed without unwinding
(SIGKILL, power loss; Node's default SIGINT also exits without running
the catch) can still stop between two renames. The swap phase is renames
only, after all the slow writing is done, so the window is small, but it
exists. A kill there leaves some skills swapped under the old record,
and `update` refuses those as changed locally. Closing it needs a
journal that is read and replayed before the plan is computed, in
`runSkillsWrite` (`packages/cli/src/utils/skills-command.ts`). That is
outside this card's file surface, so it is noted, not built. A kill
during the staging phase, which is where nearly all the time goes,
leaves the old catalog untouched, and the next write removes the
leftover. carrier: none.
- The fault seam is a module-wide `vi.mock('node:fs')` in the shared
harness, a pure pass-through while unarmed. No production seam was added
for it.
## Verification
All readings are on head b555879. The base is 6d833b5, and the 91
files `origin/main` has moved since then touch no skills file.
<details><summary>Tests: the pin file 34/34; the cli unit tier 430 files
/ 6150 tests, all green</summary>
```text
pnpm --filter @objectstack/cli exec vitest run --project unit --maxWorkers=2 test/skills-commands.test.ts
Test Files 1 passed (1) / Tests 34 passed (34) / VERDICT command-exit 0
pnpm --filter @objectstack/cli exec vitest run --project unit --maxWorkers=2
Test Files 430 passed (430) / Tests 6150 passed (6150) / VERDICT command-exit 0
pnpm --filter @objectstack/cli build && pnpm --filter @objectstack/cli typecheck
VERDICT command-exit 0; check:test-typecheck: OK (3 file(s) / 28 error(s) / 6 pinned signature(s) held, unchanged)
```
The `integration` tier was not run locally. CI runs it: this diff
touches no integration-tier file and no spawn entry.
</details>
<details><summary>Gates: 64 derived by dispatch-gates.mjs; 61 exit 0, 3
NOT MEASURED (exit 3, prerequisite not met)</summary>
```text
dispatch-gates --ran: 64 derived famil(ies) accounted for — 61 run, 3 NOT-MEASURED (3 DERIVED from a recorded exit 3)
NOT MEASURED: node scripts/check-plugin-teardown-shape.mjs --self-test, reason: its positive control is pinned to a commit this shallow clone cannot reach
NOT MEASURED: pnpm check:dual-build-cjs-loads, reason: it reads built output, and 14 packages outside this diff's closure have no dist/ in this worktree
NOT MEASURED: pnpm check:i18n-coverage, reason: examples/app-showcase could not load, because @objectstack/connector-slack has no build output here
overlap-check: VERDICT clean exit 0 (origin/main merges cleanly, 0 open PR heads share a file)
```
CI covers all three, in Lint and Repo Gates.
</details>
---
_Generated by [Claude
Code](https://claude.ai/code/session_01L6bhgTyMahdAdhXSzgy2nt)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent eb1f85d commit 578d3b1
3 files changed
Lines changed: 239 additions & 14 deletions
File tree
- .changeset
- packages/cli
- src/utils
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
22 | 42 | | |
23 | 43 | | |
24 | 44 | | |
25 | | - | |
| 45 | + | |
26 | 46 | | |
27 | 47 | | |
28 | 48 | | |
29 | 49 | | |
30 | 50 | | |
31 | 51 | | |
32 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
33 | 60 | | |
34 | 61 | | |
35 | 62 | | |
| |||
192 | 219 | | |
193 | 220 | | |
194 | 221 | | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
195 | 228 | | |
196 | 229 | | |
197 | 230 | | |
198 | 231 | | |
199 | | - | |
| 232 | + | |
| 233 | + | |
200 | 234 | | |
201 | 235 | | |
202 | 236 | | |
203 | | - | |
| 237 | + | |
204 | 238 | | |
205 | 239 | | |
206 | | - | |
| 240 | + | |
207 | 241 | | |
| 242 | + | |
| 243 | + | |
208 | 244 | | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
215 | 279 | | |
| 280 | + | |
216 | 281 | | |
217 | | - | |
218 | | - | |
| 282 | + | |
219 | 283 | | |
220 | 284 | | |
221 | 285 | | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
222 | 319 | | |
223 | 320 | | |
224 | 321 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
22 | 44 | | |
23 | 45 | | |
24 | 46 | | |
25 | 47 | | |
26 | | - | |
| 48 | + | |
27 | 49 | | |
28 | 50 | | |
29 | 51 | | |
| |||
458 | 480 | | |
459 | 481 | | |
460 | 482 | | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 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 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
0 commit comments