fix: write managed configs and workspace files through symlinks
Summary
writeFileIfChangednow resolves symlinks before reading or writing, so provisioning a managed config that is a symlink into a dotfiles repo updates the file it points at instead of replacing the link with a regular file.- The same resolution step, exported as
resolveWriteTarget, now guards host-service's trust seeder, the other tmp-file + rename writer for a user-owned config. - A link into a read-only or unreadable store (Nix home-manager) fails with an error naming both paths instead of a bare
EACCESon a path the user never created. - The same resolution now guards every other writer with this shape:
workspace-fs'swriteAtomically(every file an agent or the editor saves), the CLI's~/.superset/config.jsonandapp-state.json, the host service's auth config, and the default-account pointer. - Adds regression tests for the symlinked, unchanged-symlinked, dangling, cyclic, and read-only cases in every package touched.
Why / Context
Reported against ~/.claude/settings.json: installing the Artifact hook replaced a symlink into a dotfiles repo with a regular file. From then on the two copies drift silently — what Superset writes never reaches the repo, what the repo holds never reaches Claude Code — and nothing warns, because the write itself succeeds.
It recurred twice in three days for the reporter. Each restore of the symlink points at a repo copy that lacks the artifact guard entry we started managing in #7531, so the next provisioning run diffs, rewrites, and flattens the link again. It would have kept recurring until the two copies happened to match byte-for-byte.
The bug is older than that hook, which only supplied a reason to rewrite the file. Every managed config written through this helper had it latent: Claude/Gemini/Cursor/Codex/Droid settings, CLAUDE.md profile sharing, the MCP server config, and managed skills.
How It Works
The helper writes a temp file and renames it over the target. rename(2) never follows the final path component, so it replaces the symlink itself. The read side (existsSync / readFileSync) does follow it, which is why the merged content was always correct and only the link was lost.
The fix resolves the target with fs.realpathSync up front and uses that path for the read, the temp file, the rename, and the chmod. Deriving the temp path from the resolved target matters: a link pointing at another filesystem would otherwise fail EXDEV on rename.
A realpathSync failure means the path is absent or the link dangles; both fall back to the path as given, so a new file is still created and a dangling link is claimed — matching how linkSharedDir in profile-sharing.ts already treats dangling links as debris.
seed-agent-trust.ts in host-service has its own atomicWrite with the same shape, and it runs on every workspace creation against $CODEX_HOME/config.toml and ~/.claude.json. ~/.codex/config.toml is one of the configs people most often symlink into dotfiles, and seeding appends a new [projects."…"] block per worktree, so that writer flattened links more reliably than the settings.json path this PR started with. It now calls the same exported resolveWriteTarget, rather than carrying a second copy of the rule that could drift on which errno means "claim the path".
The Rest of the Sweep
writeFileIfChanged and the trust seeder were not the only writers with this shape. Every one below wrote a temp file and renamed it over a path a person may have symlinked, and each now resolves first:
| Writer | File it flattened |
|---|---|
workspace-fs writeAtomically | any file an agent or the editor saves — a CLAUDE.md linked to a shared copy, a repo config linked out of dotfiles |
cli writeConfig | ~/.superset/config.json |
cli writeThemeState | ~/.superset/app-state.json |
ConfigFileSessionTokenSource | the host auth config (SUPERSET_AUTH_CONFIG_PATH) — a refreshed token landed on a copy the CLI no longer read |
syncDefaultAccountPointer | ~/.superset/state/default-* |
workspace-fs is the one worth a careful look. Resolving there cannot escape the workspace, because writeFile already runs assertRealpathWithinRoot and rejects a link whose target leaves the root; a test pins that refusal next to the new behaviour. Its precondition read also moves from lstat to stat: readFile reports the target's revision, so once the link survives a write, comparing against the link's own lstat would return a conflict on every conditional write and the editor's read-modify-write loop would never converge.
Deliberately left alone: session-share.ts manages symlinks by design (it creates them and checks for them before moving trees); the CLI and host-service self-updaters replace a binary at a path, which is the point of an updater; Electron userData state, the pty handoff snapshot in tmpdir, and the shell-ready evidence cache are app-internal files nobody links and that regenerate when lost.
Manual QA Checklist
- New test reproduces the report against the pre-fix implementation (
lstatshows the link replaced by a regular file) and passes after - Symlinked
settings.json+ the real provisioning path (ensureClaudeManagedHooksAt, run twice): link intact, the dotfiles copy keepsmodeland gains all 11 managed hook events, second run is a no-op - Repeat with the dotfiles copy on a separate volume (HFS+ RAM disk, exercises the
EXDEVpath): link intact, hooks merged into the file on the other volume - Non-symlinked config still merged as before (existing tests plus the unchanged-content path)
- Dangling and cyclic links at the target are claimed rather than left broken
- Read-only link target (555 dir / 444 file) throws an error naming both paths, link intact
- host-service trust seeding through a symlinked
config.tomland.claude.json: both new tests fail on the pre-fixatomicWriteand pass after
Testing
bun test packages/agent-setup/src/write-file-if-changed.test.ts— 11 pass on Linux (10 pass / 1 skip on macOS:realpathSyncthere refuses an unreadable target before the read is reached, so that case cannot be set up)bun test packages/agent-setup— 370 pass, 1 skip, 0 fail (withSUPERSET_ACCOUNT_ATTRIBUTION_TOKEN/SUPERSET_AGENT_LAUNCH_IDunset;notify-hook.test.tsreads them from the ambient env and fails 5 cases inside a Superset terminal — pre-existing and unrelated, worth a separate fix)bun test packages/host-service/src/trpc/router/workspace-creation— 207 pass, 0 failbun test packages/workspace-fs— 149 pass, 0 fail;bun test packages/cli— 295 pass, 0 fail;bun test packages/host-service/src/trpc/router/usage packages/host-service/src/providers— 146 pass, 0 fail- Every new symlink test was also run against the pre-fix implementation of its writer and fails there
bun test packages/host-service/srcin full has 42 local failures (GitWatcher debounce/batching, dial timeouts, git-status-partial). They reproduce on a stashed tree with none of this branch's changes, and CI is greenbun run --filter @superset/agent-setup typecheck,bun run --filter @superset/host-service typecheck— cleanbunx @biomejs/biome checkon both packages' touched dirs — clean- Claude Code CLI 2.1.226 against a scratch
CLAUDE_CONFIG_DIRwithsettings.jsonand.claude.jsonsymlinked into a dotfiles dir (plugin marketplace add/remove, which writes user settings and the config): both links intact, both dotfiles copies updated in place. Same test at--scope projectfails withRefusing to write through symlink, leaving the link and file untouched.
Design Decisions
- realpath over
O_WRONLYon the existing path: opening the target for writing would preserve the link but give up atomicity, which the helper exists to provide — concurrent provisioners (desktop plus one host-service per org) would be able to tear a user's config. Resolving first keeps both properties. - Fall back to the literal path when realpath fails: the alternative (skipping the write) would break first-time file creation, which is the common case.
- Fail loudly on a read-only link target: a link into
/nix/storeused to be flattened silently, which also breaks home-manager's next activation. Throwing is the better of the two, andrunSetupActionalready isolates it to that one config, so boot is unaffected — but the error has to say which link points where, or the user only seesEACCESon a temp path they never created. - One resolution helper, two writers: host-service already depends on
@superset/agent-setup, so exportingresolveWriteTargetwas cheaper than a second copy of the ENOENT/ELOOP rule.
Risks / Rollout
- Risk: low, and the blast radius is the same set of files these two writers already wrote. The main behavior change is which inode receives the bytes when the target is a link — that is the bug.
- One genuinely new failure mode: a managed config symlinked into a read-only store (Nix home-manager, or any
/nix/storepath) now throws instead of being flattened.runSetupActioncatches and warns, so boot is unaffected, but those users lose the Superset hooks for that agent until they point the link at a writable file. That is deliberate: flattening left them with a config home-manager refuses to re-activate over. - Not a gap: Claude Code itself does not flatten the link. Its atomic writer takes an explicit
allowSymlinkoption — user-scopesettings.jsonand~/.claude.jsonpasstrueand write through the link (readlink→ rename onto the target), while project/local.claude/settings.jsonpassesfalsewithO_NOFOLLOWand a symlinked-parent check, and refuses withSymlinkWriteRefusedErrorrather than replacing anything. Verified against CLI 2.1.226 (see Testing). So the reporter's flattening came from our writers alone, and these two commits close it. - Rollback: revert the commits; behavior returns to flattening symlinks.
🤖 Generated with Claude Code