Repository navigation
Description
Problem
agentop writes the user's harness settings and its own state files in ways that can lose data or break symlinks. It also has two different "is our proxy running?" checks that disagree. Line numbers are at ab5f01f26.
#1554 already tracks the missing Sync before rename (its table lists the same writers). This issue covers the other problems with those writers, plus the in-place writes that issue does not list.
1. The ownership record is written in place, and a torn write blocks every later enable
writeState (cmd/agentop/cmd_claudecode.go:121-141) ends with os.WriteFile(path, ...), which truncates the file and then writes it. Claude Code, Codex, OpenCode and Bob all save their records through it. The record's doc comment calls it the only copy of what the user had.
- Case: agentop is killed mid-write, so
~/.cortex/claude-code-state.jsonis left cut short. From then onreadStatefails andwriteStaterefuses to overwrite ("refusing to overwrite the existing record"). Every later enable only warns, and the user's prior settings are gone. - The launchd/systemd unit file is written the same way (
cmd_service.go:473). A torn plist does not load at login, so the proxy never starts.
Fix: use the existing writeFileAtomic (setup_files.go:119) in both places.
2. Settings writers replace symlinked files and follow a fixed .tmp name
The writers are writeSettings (cmd_claudecode.go:954-983), bobWriteKeys (cmd_bob.go:986-1003) and codexEnvFile.write (cmd_codex.go:461-482). Each writes path+".tmp" with os.WriteFile, then renames it over path.
- Symlinks: if
~/.claude/settings.jsonlinks into a dotfiles repo,agentop configure claude-code enableswaps the link for a regular file, and the dotfiles copy silently goes stale. A scratch test confirmed that renaming over a symlink replaces the link.edit/local.go:21andcmd_bobshell.go:77-85already resolve symlinks for this reason.TestClaudeCodeStepUndoKeepsASymlinkedSettingsFile(setup_step_claudecode_test.go:81) only checks that undo puts the link back, so it passes while apply breaks it. - Fixed temp name: if
settings.json.tmpis a symlink,os.WriteFilefollows it.userconfig.goguards against exactly this withCreateTempand has a test for it (userconfig_test.go:307). - Duplicate backup block: the "back up once" block is copied word for word in all three writers (
cmd_claudecode.go:966-973,cmd_codex.go:462-470,cmd_bob.go:964-972). The codex comment even says "mirroring writeSettings exactly".
Atomic temp-then-rename is written about 9 times in package main alone: setup_files.go, toolscan/patch.go, cmd_bobshell.go, userconfig.go, cmd_script.go, cmd_config_migrate.go and the three above. The copies disagree on CreateTemp vs a fixed name, fsync, keeping the file's mode vs forcing 0600, and cleaning up the temp file when the rename fails.
Fix:
- Make
writeFileAtomicthe one helper. Have it resolve symlinks first, and add an option to keep the existing file mode. - Add one
backupOnce(path). - Delete the other package-main copies.
- Extend the symlink test to cover apply.
3. Two "is our proxy running?" checks give different answers
The two sets are in cmd_service_platform.go:425-468 (runningPID, livePID, adoptablePID, pidExePath) and setup_probe.go:115-260 (readPIDFile, alive, psComm, proxyRunning, setupPIDExePath). They differ in three ways:
- When
psfails:runningPIDreturns 0 ("not ours"), whileproxyRunningreports running (deliberately "sandbox-blind"). - Parsing the pidfile:
livePIDaccepts surrounding whitespace, whilereadPIDFilerejects anything that is not digits plus a trailing newline. - Finding the executable:
pidExePathonly falls back tops comm, whilesetupPIDExePathtries lsof and thenps args.
Case: in a sandbox where ps is blocked, setup_step_service.go:61 decides a background proxy is running. The install path in the same file (:282, adoptablePID) gets 0, so it does not stop the hand-started proxy. The supervised copy then fails to bind the port and crash-loops, which is the failure the comment at cmd_service.go:297 says adoption prevents. This is from reading the code; it has not been reproduced in a sandbox.
Fix: keep the setup_probe.go set, which matches install.sh, and build runningPID and adoptablePID on top of it.
Found by a code audit of main at ab5f01f.
Assisted-By: Claude (Anthropic AI) [email protected]
Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsDone
Activity
fix: restore symlink targets on undo and harden the settings writers
fix: Atomic, symlink-preserving settings writers and one proxy-runnin…