7 min readPost 8
Editing someone else's config file without breaking it
To wire Obituary into AI coding agents, obit init has to edit third-party files like settings.json and hooks.json while editors and running agents may be saving them too. Here is why Windows ReplaceFileW failed 68% of concurrent reads, how handle-pinned replacement brought that to zero, and how we ported the same discipline to POSIX.

On this page
To connect AI coding agents to Obituary's lifespan tracking and delete guard, obit init has to edit files Obituary does not own—Claude Code's settings.json, Codex's hooks.json, and dozens of other host configs.
The catch is concurrency. A developer may have that exact JSON open in an editor, or the running agent itself may rewrite it at any millisecond. Swallow one recent user edit or leave a half-written JSON file on disk, and you break the user's primary coding tool.
Over the afternoon and evening of 10 November, we rewrote our config-commit path three times. Every single iteration followed one move: replacing a filename-based syscall with a pinned handle.
The race hiding right after final verification
Our installer originally computed a plan, remembered the exact original bytes of each target file, re-read the file right before writing, and aborted if anything had changed.
Code review immediately exposed the gap: between that final check and the actual write, an editor performing a temp-file-plus-rename save can drop a brand-new version onto the path, and our write silently clobbers it.
Trying ReplaceFileW first: 68% of concurrent readers saw the file vanish
Windows has no single syscall for "atomically replace a file only if its identity still matches." Microsoft's documentation for updating document files points to writing a temp file and calling ReplaceFileW, which even writes a backup along the way.
Under a simple stress test—one thread looping ReplaceFileW while other threads continuously opened and read the target—ReplaceFileW failed badly: out of 16,329 concurrent reads, 11,160 got file-not-found.
ReplaceFileW briefly removes the target name during replacement. If a running host agent reads its config in that gap, it can mistake a live config for a missing one and overwrite it with empty defaults. NTFS transactions (TxF) were off the table too, as Microsoft explicitly warns against using them in new code.
Holding handles instead of names
We replaced ReplaceFileW with a four-step handle-pinned sequence on Windows:
- Inside a pinned parent directory handle, open the target denying both write and delete sharing; read its bytes and 128-bit file identity and confirm they match the plan.
- While still holding that target handle, exclusively create a numbered backup (
.bak,.bak.1, …) holding the exact bytes just verified. - Write the updated config to a temp file created exclusively with no write/delete sharing. Every subsequent write, check, and publish rename operates on that single handle (and deletes through it on failure), giving no other process a chance to swap the temp file.
- Close the target handle and immediately commit the rename through the temp handle.
Throughout verification and backup, competing in-place writes and atomic saves are blocked by Windows sharing modes. Any mismatch aborts the install without retrying over the user's version; deletions similarly dispose through the verified handle rather than by path.
Under the exact same concurrent read harness: 3,208 reads, 0 missing-file errors.

Two more windows caught in follow-up reviews
Fresh review rounds tightened two remaining seams:
- Closing the temp file before renaming: our first handle implementation created the temp file through
os.Root, closed it, and renamed by path—leaving a swap window on the temp file. We kept the temp handle open and renamed it directly viaNtSetInformationFilerelative to the pinned parent directory handle. - Junction swap on the parent directory: temp files were still created by absolute path, so swapping the parent directory to a junction after verification could redirect creation outside the folder. We switched to
NtCreateFilerelative to the pinned parent handle (read-sharing only, with delete access), moved Explorer label writes onto the same helper, and deleted the old path-based helper outright.
Tests in both packages swap the parent directory to another directory or junction right after the final check and assert zero writes escape.
One irreducible gap remains between closing the target handle and completing the rename: measured at 0.77 ms median and 2.1 ms p99. If an editor saves inside that sub-millisecond sliver, the numbered backup still preserves the verified prior version and the conflict fails the install so the user can re-run cleanly.
Strict ownership: touch only what Obituary wrote
Atomic writes are only half the contract; editing someone else's config also demands strict ownership rules:
- Only hook entries pointing to an absolute
obit/obit.exebinary withhook <agent>arguments belong to Obituary; custom compound user commands are never touched. - Editing
settings.jsonpreserves original key order, avoids escaping&&to\u0026, preserves any leading UTF-8 BOM, and formats modified files with two-space indentation. - Numbered backups (
.bak,.bak.1) are created exclusively and never clobber earlier backups; uninstall removes only Obituary's own entries. - Because Claude Code rewrites
~/.claude.jsonwholesale at runtime, Obituary only reads it to check ownership and delegates all mutations to officialclaude mcp add-jsonandclaude mcp remove.
Here is a real install-and-uninstall round-trip on a settings.json carrying a user's own PreToolUse hook: the pre-install file, the backup, and the post-uninstall file produce identical SHA-256 digests down to the last byte.

Porting the same discipline to macOS and Linux
Because Windows sharing modes have no POSIX equivalent, obit init initially refused to run on macOS and Linux. On 12 November we shipped the POSIX commit sequence:
- Read bytes and full identity (device, inode, size, mtime) inside a pinned parent directory descriptor and verify they match the plan.
- Exclusively create a numbered backup.
- Write new bytes into an exclusively created temp file in the same directory and
fsyncit. - Re-verify target identity and contents immediately before rename.
- Atomically rename inside the pinned parent and
fsyncthe directory.
Deletions never call bare unlink: they rename without overwrite into a numbered backup first, verify identity, and move the file back if anything mismatched—keeping the final check-to-rename gap on the same sub-millisecond scale as Windows.