Skip to content

Write hook configs atomically, keep tests out of the real home, refresh stale Claude hooks - #147

Merged
iamnotstatic merged 5 commits into
mainfrom
iamnotstatic/safe-hook-writes
Oct 29, 2026
Merged

iamnotstatic merged 5 commits into
mainfrom
iamnotstatic/safe-hook-writes

Conversation

@iamnotstatic

@iamnotstatic iamnotstatic commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

Three fixes to how vibetime writes the desktop apps' hook configs. All three were found on the maintainer's machine on 2026-10-29: ~/.claude/settings.json and ~/.codex/hooks.json were both unparseable, and Claude Code loads nothing from a settings file it can't parse, so every hook and setting in it was off. The Codex file had been broken since Oct 21.

1. Config writes can tear

The installers and the rc-file edits used a plain writeFileSync, which truncates and then writes. When two writers hit the same file at once (here vibetime and Orca, which keeps its own hooks in the same file), the result was a short object followed by the tail of a long one. Because the session-start repair runs the installers on every session, every user is exposed, and Claude Code reads its settings while they're being written.

New src/write-file.ts, writeFileAtomic:

  • Writes a temp file next to the target and renames it into place, so readers see the old file or the new one, never part of one.
  • Follows symlinks, so a dotfiles-managed settings.json or .zshrc stays a link, and keeps the file's mode.
  • Skips the write entirely when the content is unchanged, which is the normal case for the repair.
  • On Windows, falls back to a plain write when the rename is refused because another process has the file open.

Used by the Claude, Codex, Cursor and ZCode installers and by init.ts's rc rewrites.

It doesn't stop lost updates: if vibetime and another app both rewrite the file at the same moment, one change wins whole. That's recoverable, since each app re-adds its hooks; a torn file isn't.

2. Tests rewrote the developer's real hooks

Hook tests fire session-start, which runs reconcileInstall() against the real ~/.claude, ~/.codex, ~/.cursor and ~/.zcode. Tests only redirected VIBE_DIR. So every npm test in a worktree pointed the developer's desktop hooks at that worktree, and they stopped working when the worktree was deleted. That's how the broken Codex file came to point at an old .claude/worktrees/agent-… path.

npm test now preloads test/isolate-home.mjs, which points HOME, USERPROFILE and CODEX_HOME at a scratch dir for every test file. Test files that set HOME themselves are unaffected.

3. The Claude installer never refreshed a stale path

reconcile.ts says every installer "refreshes stale pinned paths". The Codex, Cursor and ZCode installers do, but the Claude one kept any vibe hook it found. After an nvm upgrade that removes the old Node, Claude Desktop tracking fails silently, and the repair that exists for this case doesn't fix it. It now replaces a vibe hook whose command changed, and only vibe's own commands: anything else sharing that group stays.

Verified

  • Race: test/write-file.test.mjs runs six writers (200 KB and 100 B bodies, 60 writes each) and one reader, all released at a shared instant. Against a plain writeFileSync, the reader got unparseable content 89, 83 and 89 times in three runs. With this change it got 0, 0 and 0. Symlink, mode and unchanged-content tests are alongside.
  • Isolation: with a fake home whose Claude hooks point at a "global" path, test/hook-reopen.test.mjs without the preload rewrote all five hooks to the worktree's dist/cli.js. With the preload the file was untouched. Two full npm test runs left the real ~/.claude/settings.json, ~/.codex/hooks.json, ~/.cursor/hooks.json, ~/.zcode/cli/config.json and ~/.zshrc byte-identical (shasum before and after).
  • Refresh: test/claude-hooks.test.mjs (the Claude installer had no tests) fails on main with SessionStart still points at '/old/node' … and passes here. It also checks that a foreign command in the same group, a foreign event and other top-level settings survive, and that a current install isn't rewritten.
  • npm test: 201/201. Server typecheck clean.

Not covered

  • A process killed between writing the temp file and renaming it leaves a stray *.tmp next to the config.
  • The Windows fallback is untested, like the rest of Windows.
  • Running a single test file as node --test test/x.test.mjs skips the preload. CI and npm test always use it.

Changes from release QA

An independent review of the combined release (#146, #147, #148) found these. Each fix has a test that fails without it.

  • The repair no longer re-pins hooks that still work. The install repair runs from whichever vibe starts: a second Node under nvm, a dev checkout, an npx cache. It used to point every app's hooks at itself. Codex then skips the changed hook until it's reviewed again, and a dev checkout's hooks die with the checkout. The released 0.15.0 does this too; it's how the maintainer's hooks ended up on a worktree. Installers now have an explicit mode:
    • repair, run at session start and in vibe status, leaves a vibe hook alone while the Node and cli.js it pins exist and its arguments are current.
    • install, run by vibe hooks install, still pins to the CLI that ran it.
  • A repaired Codex hook keeps its slot. Codex keys trust by file:event:group:handler plus a hash of the hook (codex-rs/hooks/src/engine/discovery.rs). Moving our group to the end shifted every hook after it, other tools' included. Reproduced on 0.15.0 with an Orca-style hook after ours: its own session-start repair swapped the two.
  • Only vibe's own commands are recognised. A vibe hook is now __hook followed by one of our event names. Before, any command containing __hook, such as pre_commit__hooks.py, was taken for ours, and the new Claude refresh would have replaced it. The rule lives in one place, src/hook-command.ts.
  • One app's broken config can't block the others. A Claude group with no hooks list no longer throws, and each app's repair runs on its own, so an exception can't skip the rest.
  • writeFileAtomic edge cases. It writes through a dangling symlink to its target instead of replacing the link, keeps the mode of a file it can't read, and falls back to a plain write on EACCES (a writable file in a folder we can't create files in).
  • Flaky existing test. wrap-pending-delivery.test.mjs removed its scratch folder before stopping the wrapper. The ENOTEMPTY skipped the teardown that stops it, and the file hung: 1 run in 10 on main, 2 in 10 here, 0 in 35 after the fix.
  • Linux CI. The race test now builds its 200 KB body inside each writer, because Linux caps a single argument at 128 KB.

Upgrade path, checked end to end: 0.15.0 built from its tag and installed at a fixed path with hooks for all four apps, ZCode hooks switched on, and a foreign Codex hook after ours. Then the new build was swapped in at the same path and run through vibe status, a Claude start, a Codex start, a Cursor first prompt and a ZCode first prompt:

  • All four configs stay byte-identical, so Codex trust and ZCode's switched-on state survive.
  • vibe status run from a dev checkout leaves them identical too.
  • Pinned to a deleted path, they are all repaired by vibe status, with the Codex hook staying in its slot.

…e, refresh stale Claude hooks

Hook configs and rc files are now written to a temp file and renamed into
place, through symlinks, keeping the mode, and not at all when nothing
changed. A plain write racing another app's left ~/.claude/settings.json
and ~/.codex/hooks.json unparseable, and Claude Code then loads none of it.

npm test preloads a scratch HOME and CODEX_HOME. Hook tests run the install
repair, which was repointing the developer's real desktop hooks at the
worktree under test.

The Claude installer now replaces a vibe hook whose pinned path changed,
as the Codex, Cursor and ZCode installers already did, so the session-start
repair fixes a Node that moved.
Codex keys hook trust by position as well as content and skips a hook until
it is reviewed again when either changes. Moving our group to the end on
every repair shifted the hooks after it, other tools' included.
Linux caps one argv entry at 128 KB, so passing it on the command line
failed with E2BIG in CI.
…ommands

The install repair runs from whichever vibe starts: a second Node under nvm,
a dev checkout, an npx cache. It now leaves a vibe hook alone while the Node
and CLI it pins still exist, so it stops trading working hooks for ones that
may vanish, and stops sending Codex hooks back for review. An explicit
vibe hooks install still pins to the CLI that ran it.

A vibe hook is now __hook followed by one of our events, not any command
containing __hook, so a user's pre_commit__hooks.py is never replaced. A
Claude group with no hooks list no longer throws, and each app's repair is
attempted on its own so one broken config cannot skip the rest.

writeFileAtomic writes through a dangling symlink to its target, keeps the
mode of a file it cannot read, and falls back to a plain write on EACCES.
Teardown ran in registration order, so the folder was removed while the
wrapper still wrote to it. The ENOTEMPTY skipped the hooks that close the
stub server and stop the wrapper, and the file hung: 2 runs in 10 here, 1 in
10 on main. 0 in 35 after.
@iamnotstatic iamnotstatic mentioned this pull request Oct 29, 2026
@iamnotstatic
iamnotstatic merged commit 8b1be53 into main Oct 29, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant