Skip to content

fix: follow symlinks in atomicWrite so linked state files stay linked - #6

Open
michel-slm wants to merge 1 commit into
vincent-k2026:masterfrom
michel-slm:follow-symlinks
Open

michel-slm wants to merge 1 commit into
vincent-k2026:masterfrom
michel-slm:follow-symlinks

Conversation

@michel-slm

Copy link
Copy Markdown

Problem

Linking ~/.claude/plugins/codachi/memory.json into a synced folder (Nextcloud, Syncthing, a dotfiles repo) to carry pet progress between machines stops working after the first write. atomicWrite writes a temp file and renames it over the path, and a rename onto a symlink replaces the link itself, so the file silently becomes a plain local copy and the synced one stops updating. Seen in practice: the local count reached 20 sessions while the synced copy stayed at 10.

Fix

atomicWrite resolves the link first and writes the temp file next to the link's target, so the rename replaces the target and the link stays. A link whose target does not exist yet is followed too (the target is created) rather than replaced. Plain files and missing files are written exactly as before.

Before and after

The same check against the npm 0.3.0 package and against a build of this branch: in an isolated HOME, memory.json is linked into a "synced" folder holding totalSessions: 7, and one session renders the statusline.

== codachi 0.3.0 (npm) ==
memory.json is now a regular file (link lost)
synced copy totalSessions = 7 (7 before the session)

== follow-symlinks branch (717d68e) ==
memory.json is still a symlink
synced copy totalSessions = 8 (7 before the session)
Check script
#!/bin/bash
# Link memory.json into a "synced" folder, start one session with the given
# codachi build, and report whether the link and the synced copy survived.
set -u
entry="$1"; label="$2"
home="$(mktemp -d)"; sync="$home/Nextcloud/plugins/codachi"
mkdir -p "$sync" "$home/.claude/plugins/codachi"
echo '{"version":1,"totalSessions":7,"totalUptimeMin":0,"firstMet":1,"lastSeen":1}' > "$sync/memory.json"
ln -s "$sync/memory.json" "$home/.claude/plugins/codachi/memory.json"
printf '{"transcript_path":"/tmp/s.jsonl","model":{"display_name":"T"},"context_window":{"used_percentage":5,"context_window_size":200000}}' \
    | HOME="$home" NO_COLOR=1 node "$entry" >/dev/null 2>&1
f="$home/.claude/plugins/codachi/memory.json"
echo "== $label =="
if [ -L "$f" ]; then echo "memory.json is still a symlink"; else echo "memory.json is now a regular file (link lost)"; fi
python3 -I -c 'import json,sys; print("synced copy totalSessions =", json.load(open(sys.argv[1]))["totalSessions"], "(7 before the session)")' "$sync/memory.json"
rm -rf "$home"

Usage: ./symlink-check.sh <path to dist/index.js> <label>

Tests

  • New src/fs-utils.symlink.test.ts, 4 tests on a real temp directory (fs-utils.test.ts mocks node:fs module-wide, so link behaviour needs its own file): an absolute link, a relative link, a dangling link, and a plain file leaving no temp files behind. Against the unmodified fs-utils.ts, the three link tests fail; the plain-file one passes on both.
  • fs-utils.test.ts: its node:fs mock gains realpathSync/lstatSync/readlinkSync, defaulting to ENOENT.
  • Full suite: 365 passed (361 before), tsc clean.

Notes

Linking ~/.claude/plugins/codachi/memory.json into a synced folder, to
carry pet progress between machines, stops working after the first
write: the file becomes a plain local copy and the synced one stops
updating, with nothing to say so.

atomicWrite writes a temp file and renames it over the path, and a
rename onto a symlink replaces the link itself. It now resolves the
link first and writes the temp file next to its target, so the rename
replaces the target and the link stays. A link whose target does not
exist yet is followed too, rather than replaced. Plain files are
written exactly as before.

Assisted-by: Claude Code:claude-opus-5-5
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