Skip to content

bug: agentop can lose the user's original settings, breaks symlinked settings files, and has two disagreeing proxy checks #1565

Closed

Description

@huang195

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.json is left cut short. From then on readState fails and writeState refuses 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.json links into a dotfiles repo, agentop configure claude-code enable swaps 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:21 and cmd_bobshell.go:77-85 already 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.tmp is a symlink, os.WriteFile follows it. userconfig.go guards against exactly this with CreateTemp and 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 writeFileAtomic the 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 ps fails: runningPID returns 0 ("not ours"), while proxyRunning reports running (deliberately "sandbox-blind").
  • Parsing the pidfile: livePID accepts surrounding whitespace, while readPIDFile rejects anything that is not digits plus a trailing newline.
  • Finding the executable: pidExePath only falls back to ps comm, while setupPIDExePath tries lsof and then ps 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]

Activity

  1. added
    bug
    laptop
    on Oct 29, 2026
  2. self-assigned this
    on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

buglaptop

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions