Repository navigation
fix(remote): keep fm-remote Herdr servers in the Aqua session - #4090
Merged
Merged
Conversation
A herdr server keeps the macOS audit session of whatever started it, and
only the Aqua login session (gui/<uid>) can read the login keychain
without a prompt. Herdr's SSH remote attach starts the fm-remote server
as its own child when it finds none, wins the socket at boot because sshd
accepts connections before the login session exists, and every claude
pane under that server then gets `security` exit 36, falls back to a stale
plaintext credentials file, and reports "Login expired". launchd's own job
lost the socket on every KeepAlive retry and the doctor still reported the
session ready because it only asked whether any server answered.
- Add bin/fm-remote-herdr-guard.sh, the launch agent's exec target: start
the server in the foreground when nothing owns the socket, exit 0 when an
Aqua-born server does, and otherwise stop the foreign server, wait for the
socket, and exec the server at once.
- Add bin/fm-remote-herdr-owner-lib.sh, the single owner of socket-owner
discovery (lsof; pgrep cannot see herdr's argv on macOS) and the birth
markers (SSH_*, XPC_SERVICE_NAME, FM_REMOTE_JOB_ACTIVE, sshd or
remote-client-bridge ancestry matched on argv[0] and whole arguments).
- Render the agent as the login shell exec'ing the guard with
KeepAlive={SuccessfulExit=false} and ThrottleInterval=10, check the loaded
job's successful-exit semaphore, and report a session served outside the
Aqua login session as fixable so --fix retakes it through launchd; the
reload waits for an Aqua-born owner rather than any running server.
- Correct the doctor and docs: the launch shell provides environment parity,
the launchd domain provides keychain access.
- Pin the guard's decision table and the doctor's verdicts against real
marker-carrying processes, and record the dated audit-session evidence.
Intent
Captain's intent: Root-cause, from the facts below, WHY the claude agents in the mac mini's
fm-remoteHerdr session cannot authenticate ("Login expired · Please run /login" / "Not logged in") while claude in the mini'sdefaultHerdr session authenticates fine on the same machine and same user account. Deliver a report with the actual root cause, backed by evidence, and a recommended durable fix (one that keeps working across reboots). The captain does NOT want a prescribed solution handed to you - dig from the facts and find the real mechanism yourself. Do not assume any prior hypothesis is correct.FACTS (observations only - not conclusions):
mac-home, macOS, this same dotfiles setup as the MacBook you run on) hosts persistent Firstmate second-mate agents. Each runs theclaudeCLI inside a Herdr session namedfm-remote, which is kept alive across reboots by a per-user macOS LaunchAgentdev.firstmate.herdr.fm-remote. That LaunchAgent's plist is written/rewritten bybin/fm-remote-doctor.sh(functionrender_launch_agent) in the firstmate repo.kunchenguid/dotfiles-private) define a home-manager LaunchAgentlaunchd.agents.herdr-server(inhome-manager/common.nix) for the DEFAULT Herdr session. claude under that default-session server AUTHENTICATES CORRECTLY. The captain confirmed the default server is that launch-agent server - he did NOT start it by hand in a terminal.fm-remotesession reports login-expired / not-logged-in and cannot run a turn. Thedefaultsession on the same machine is fine.~/.claude/.credentials.jsonalso exists and was recently modified. (Separately and already RESOLVED:claude --versionhad been hanging earlier due to a wedgedsyspolicyd/trustdon the mini; a reboot cleared that. The current problem is ONLY the login-expired auth infm-remote, not a hang.)CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION=false CLAUDE_CODE_SEND_FEEDBACK=0 claude --dangerously-skip-permissions --settings '{...}' <brief>- with NOHOME,CLAUDE_CONFIG_DIR, orXDG_*override and noenv -i(seebin/fm-spawn.shclaude case). So the mate's claude reads the normalHOMEand keychain.defaultandfm-remoteare distinct servers/sockets.a. The
fm-remoteLaunchAgent was changed (firstmate PR fix: launch remote Herdr through the user login shell #4061, merged and deployed to the mini) so it now starts the server through a login shell:/run/current-system/sw/bin/zsh -l -c "exec /etc/profiles/per-user/kunchen/bin/herdr server --session fm-remote"- matching the login-shell pattern the dotfiles default agent uses. After deploy, login-expired PERSISTED.b. Restarting the
fm-remoteserver mid-session - viaherdr server stop --session fm-remotethenlaunchctl kickstart -k gui/501/dev.firstmate.herdr.fm-remote, and via the doctor's ownlaunchctl bootout+bootstrap+kickstart- did NOT restore auth.c. A bare
herdr server --session fm-remote(the old plist form) self-daemonizes/detaches and surviveslaunchctl bootout.d. The
defaultagent started at LOGIN (RunAtLoad when the Aqua GUI session came up). Thefm-remoteagent has, so far, only ever been (re)started MID-SESSION via kickstart since the login-shell change - it has not started fresh at a login/boot with the corrected plist.Captain's later words: given the CONFIRMED root cause (the SSH remote-client-bridge auto-spawning a keychain-less fm-remote server that pre-empts the launchd agent), should we REVERT the previous merged PR #4061 - the change to bin/fm-remote-doctor.sh render_launch_agent that made the fm-remote launch agent start the herdr server through a login shell? Assess honestly with evidence whether #4061 is (a) still necessary and correct as part of the real fix, (b) irrelevant/redundant, or (c) actively wrong; if keep, say whether any part should still be adjusted.
The captain authorized implementing the durable fix the report recommends. The substance of that recommendation, in the captain's terms: the fm-remote server that owns the session socket must be the one spawned by launchd in the user's Aqua login session (gui/), because only that birth carries login-keychain access, and Herdr's SSH remote attach (the captain's saved machine "mini" reconnecting through
herdr --session fm-remote remote-client-bridge) otherwise starts a keychain-less server first at every boot and within seconds of every stop. The mini's live datum confirmed it: the fm-remote server was a child of the SSH bridge with SSH_CONNECTION in its environment and no XPC_SERVICE_NAME, while launchd's own job sat at "spawn scheduled", 239 runs, last exit code 1, its log repeating "herdr server is already running". The durable fix: the launch agent runs a Firstmate-owned guard through the login shell (keep #4061's<login-shell> -l -c 'exec ...'launcher) with KeepAlive={SuccessfulExit=false} and ThrottleInterval=10 instead of unconditional KeepAlive; the guard finds the socket owner, execs the server in the foreground when nothing owns the socket, exits 0 when an Aqua-born server (launchd-born or the Aqua remote-job worker) already owns it, and otherwise stops the foreign server, waits for the socket to be released, and execs the server immediately so the socket is rebound before the SSH attach can start another foreign server; a takeover closes the session's panes and the parent firstmate's liveness sweep relaunches the mates. The doctor must be honest: report a session served by a server born outside the Aqua login session as fixable (naming the pid and birth) and let --fix retake it through launchd, and wait for launchd's own Aqua-born owner rather than for any server that answers. Verdict on #4061: keep it, it is neither cause nor cure - a bare Aqua LaunchAgent and the login-shell form behave identically for the keychain, the login shell only provides environment parity - but change its unconditional KeepAlive to SuccessfulExit=false, exec the guard instead of herdr directly, and correct the comments and docs that claimed the login shell grants login-keychain access. The pre-existing tests/fm-pi-primary-types.test.sh failure also fails on main and is out of scope for this change.What Changed
fm-remotesocket owner, replaces SSH-born servers, and preserves existing Aqua-born owners.Risk Assessment
✅ Low: The change is well-bounded, matches the authorized durable takeover design, and the prior Aqua-classification issue is corrected with launchd-domain evidence.
Testing
Focused fixture tests covered the guard decision table, doctor repair contract, and entrypoint behavior; live Herdr checks demonstrated foreground ownership and SSH-server takeover, while a read-only live doctor run correctly reported the ambient unproven owner as fixable. Actual launchd bootstrap/reboot validation was not performed because this phase may not modify host launchd or user state.
herdr server stop.unknown, outside the Aqua login session, and exited unready with a--fixaction.gui/<uid>anduser/<uid>, which would modify host launchd state and user LaunchAgents outside the worktree. Provide a…~/Library/LaunchAgents, alter launchd state, close ambient fm-remote panes, and require reboot authority, all prohibited in this test phase. Provide a disposab…Evidence: Live guard foreground ownership
Source: Live guard foreground ownership
Evidence: Live SSH-born Herdr server takeover
Source: Live SSH-born Herdr server takeover
Evidence: Live read-only fm-remote doctor diagnosis
Source: Live read-only fm-remote doctor diagnosis
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 2 issues found → auto-fixed ✅
bin/fm-remote-herdr-owner-lib.sh:111- Any process with XPC_SERVICE_NAME is classified as Aqua-born, but the recorded evidence in docs/verification/runtime-backends.md explicitly shows a user/501 Background LaunchAgent also has an XPC service while keychain access fails with exit 36. A Background-born fm-remote owner therefore reaches fm_remote_herdr_birth_is_aqua, is left running by the guard, and is reported ready by the doctor, preserving the authentication failure. Restrict this shared classifier to markers that actually prove the known gui/<uid> jobs, or inspect the audit session directly.bin/fm-remote-herdr-guard.sh:67- The guard merely execs the sameherdr server --sessioncommand that the supplied evidence says self-daemonizes and surviveslaunchctl bootout; exec replaces the guard but cannot prevent Herdr from detaching. Once that launcher exits successfully, KeepAlive={SuccessfulExit=false} rests permanently. If the detached Aqua server later stops, the reconnecting SSH bridge can again create a keychain-less owner with no launchd retry to retake it. This contradicts the required durable, foreground-supervised fix. The remedy needs an authorized foreground mode or additional supervision/retry lifecycle machinery, so confirm that mechanism with the user rather than relying on exec alone.🔧 Fix applied.
✅ Re-checked - no issues remain.
✅ **Test** - passed
✅ No issues found.
herdr server stop.unknown, outside the Aqua login session, and exited unready with a--fixaction.gui/<uid>anduser/<uid>, which would modify host launchd state and user LaunchAgents outside the worktree. Provide a…~/Library/LaunchAgents, alter launchd state, close ambient fm-remote panes, and require reboot authority, all prohibited in this test phase. Provide a disposab…bash tests/fm-remote-herdr-guard.test.shbash tests/fm-remote-doctor.test.shbash tests/fm-remote-entrypoint.test.shStartedbin/fm-remote-herdr-guard.shwith Herdr 0.9.0 in an isolated HOME and verified its PID owned the session socket until clean server shutdownStarted a real Herdr server carryingSSH_CONNECTION, ran the guard, and verified socket ownership transitioned from the foreign PID to the guard-exec PIDRanbin/fm-remote-doctor.shread-only against the host's running fm-remote session and captured its birth/readiness diagnosisConfirmed the temporary.nmhHOME and isolated Herdr processes were removed✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.