Repository navigation
fix: the installed service owns the daemon, so auto-start no longer leaves an unowned daemon with a client's settings - #332
Conversation
Review findings: reproduced, fixed, and checked by reverting each fixFor each finding, the failing test was written first and run on this branch, then the fix went in, then the source change was reverted on its own to show the test fails again.
How the fixes could have undone the original split-brain fix, and the tests that guard against it
Results
🤖 Generated with Claude Code |
…eaves an unowned daemon with a client's settings - Auto-start asks the installed service (LaunchAgent, systemd user unit, logon task) to start the daemon instead of spawning a detached child; an unloaded LaunchAgent is loaded. A direct start is the fallback only when no service exists or the service manager refuses. - A directly started daemon never inherits the client's agent name, channel, tool profile or identity material, and carries the service's own settings when a service exists. - A daemon started by its service that finds an unowned daemon holding the data folder asks it to hand over once, instead of exiting 0 behind it for good. - slm doctor reports a daemon that is not owned by its installed service and names the service settings it is missing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
launchctl list prints text plutil cannot read and never includes the job's environment, so every check of a loaded LaunchAgent failed closed: a running service looked foreign, uninstall and reinstall refused to unload it, and auto-start never used it. Read launchd's own text and take the environment from launchctl print. A job of another data folder is still refused. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s its installed definition A definition edited without a reload, or an install whose load failed, left the service manager holding an older job under the same name. Auto-start asked that job to start, running the old interpreter and settings, and doctor reported its process as owned. The loaded job is now checked against the installed file (label, program, data folder, port) before it is started or trusted; a mismatch falls back to a direct start with the file's settings, and doctor says to reinstall the service. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ut runs no daemon A successful launchctl bootstrap was taken as a started daemon. A job that was loaded but could not run (disabled, or a program that exits at once) cost the client a 60 s wait and then a failed start with no fallback. The job is now kicked after loading, and a service that shows no process for a few seconds is treated as not running, so the daemon starts directly with the service's settings. A service daemon that is still starting is never doubled. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… manager cannot be asked Without a reachable service manager (no user bus, no GUI session in a remote shell) doctor warned on macOS with a fix, slm restart, that could never clear it, while Linux skipped the same check. Both now skip it; a reachable manager that runs another process still warns. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…hecked The hand-over checked the folder's daemon against the service's jobs, then stopped whatever daemon the record named at the moment of the stop. If another daemon replaced the checked one in between, that one was stopped. The stop is now bound to the checked instance and refused on any change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The serial-thread defaults were forced after the service's settings were applied, so a service that set its own value lost it. The defaults now sit under the service's settings and still override the client's own values. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… starts nothing On Windows a logon task whose wrapper points at a removed Python accepted the start, launched nothing, and auto-start gave up after 60 s. It now starts the daemon directly with the service's settings once the wait ends, but only when no daemon record names a live process, the instance lock is free and nothing listens on the port. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A deeply nested launchctl answer raised RecursionError through the service checks. The parser now rejects it as unreadable, and the job checks and the start request treat any unexpected failure as "cannot ask", so auto-start falls back to a direct start. Co-Authored-By: Claude Opus 5.5 <[email protected]>
6547793 to
0f7c0ed
Compare
Second review: both findings reproduced, fixed, and checked by revertingRebased on
The fix. After a full wait with no daemon, auto-start now starts the daemon directly (scrubbed environment plus the service's settings) only when all of these hold: the service manager reports no process for the job (always true on Windows), no daemon record names a live process, the instance lock is free, and nothing listens on the port. This applies on every platform, so a macOS or Linux manager that stops answering after accepting the start is covered too. The daemon's own instance lock still stops two daemons if one appears after the check. Every fallback logs the service name and Ways the fallback could start a second daemon or hide a broken service, and the tests that guard against eachEach guard was removed on its own, and the listed tests failed:
Open, filed separately
Results
No real service manager was started, stopped or changed. Every launchd, systemd and Task Scheduler call in the tests goes to an in-memory fake. 🤖 Generated with Claude Code |
Symptom
On macOS with the daemon installed as a LaunchAgent (
KeepAlive {SuccessfulExit: false},ThrottleInterval 30), the service was unloaded withlaunchctl bootoutand the package upgraded. Within about 30 s, an already-runningslm mcpstdio process auto-started a new daemon itself. That daemon was detached (ppid 1), not owned by launchd, ran with the MCP client's environment (SLM_AGENT_ID,SLM_CLIENT_CHANNEL=mcp,SLM_MCP_PROFILE,SLM_SKIP_DEP_CHECK) and without the service's own settings (anSLM_RANKINGset only in the plist was silently lost). When the LaunchAgent was loaded again, its daemon found the data folder held, exited 0, and launchd never retried:launchctl listshowed- 0. The result was a long-lived daemon with the wrong configuration, attributing work through its embedded MCP endpoint to one agent, while the service manager reported nothing wrong.Root cause
cli/daemon.py::_start_daemon_subprocess(the one spawn path behindensure_daemon, every CLI/MCP auto-start andslm restart) always ranPopenwith a full copy ofos.environ. It never asked whether an installed service owns this data folder and port.server/unified_daemon.py::start_serverreturns (exit 0) when another daemon holds the instance lock or the port. That is deliberate to avoid a respawn loop, but it also meant the service's daemon could never take ownership back from an unowned one.Fix
cli/daemon_supervisor.pyfinds the service definition thatservice_inventoryalready proves belongs to this data folder and port, and asks its manager to start it: macOSlaunchctl kickstart gui/<uid>/<label>(never-k), orlaunchctl bootstrapwhen the plist is installed but not loaded; Linuxsystemctl --user start; Windowsschtasks /Run. The client then waits for owned health as before. Choice for an unloaded LaunchAgent: load it. launchd loads it at the next login anyway, and loading it now keeps one owner; spawning around it reproduces the collision. A direct start remains the fallback when there is no service or the manager refuses (for example no GUI domain in a remote shell, or a disabled job); the fallback is logged with the reason.daemon_environment()removes client-scoped variables (agent id, client/channel, session, project, MCP profile and tool lists,SLM_MCP_EMBEDDED,SLM_SKIP_DEP_CHECK,SLM_HOOK_*, and the client'sSLM_DAEMON_INSTANCE_ID/CAPABILITY), then overlays the service definition's environment when a service exists, so the fallback still runs with the service's settings. Operator settings such asSLM_RANKINGset in the starter's environment are kept when there is no service.server/supervisor_handover.pyruns instart_serverbefore the instance-lock wait. Only when this process is the service's job (its PID is the PID the service manager reports) and the folder is held by a live daemon of this data folder that no installed service runs, it asks that daemon to stop through the same authenticated/stopthatslm restartuses (graceful drain), waits for the folder to be free, and then starts normally. A holder it cannot identify, or one run by another service, is never stopped.slm doctoradds "Daemon service owner": WARN when the daemon's PID is not the service's PID, naming theSLM_*settings from the service definition that the running daemon lacks (names only, never values). Fix:slm restart, which now restarts through the service.Why hand-over for (c), and the tradeoff
Considered: (1) exit non-zero when the folder is held, so launchd/systemd retry every 30 s; (2) the current exit 0; (3) hand-over. (1) loops forever while the unowned daemon is healthy, filling the logs and never resolving: an unowned daemon has no reason to exit. (2) is the bug. (3) resolves it in one step with no loop. The tradeoff is that it stops a running daemon. That is limited to a daemon proven to belong to this data folder, proven not to be run by any installed service, and only when the service itself has been started (at login, by
launchctl kickstart, or by a client asking the service to start). The stop is graceful. It is tried once per process; if the holder refuses, the service's daemon exits 0 as before (no loop), logs that it will not retry, andslm doctorreports the unowned daemon.Reviewer concerns, and how they are handled
Restart=on-failurehas the same exit-0 semantics asKeepAlive SuccessfulExit=false, so (a), (b), (c) and doctor all apply (MainPID). On Windows the logon task is a launcher, not a supervisor: it never restarts the daemon and reports no PID. There, (a) runs the task (so the wrapper's environment applies) and (b) applies; (c) and the doctor check are skipped on purpose, because there is no restart loop to collide with.daemon.lock; the per-folder instance lock is the final arbiter.kickstartwithout-kis a no-op on a running job. A load that fails because another client loaded the job first is re-checked and followed bykickstart(unit-tested). Remaining window: during a hand-over, a client still running pre-fix code can spawn a detached daemon between the old daemon's exit and the service daemon's lock acquisition. The service daemon acquires within milliseconds while a cold spawn takes seconds to import, so this is unlikely. If the spawn wins anyway, the service daemon does not retry (no loop) and doctor reports the unowned daemon.daemon.lock. Even without it, both requests resolve to one start, because kickstart is idempotent and a second bootstrap fails with "already loaded" and falls through to kickstart. Only one daemon can take the instance lock.Out of scope: clients already running pre-fix code keep their old in-memory auto-start until they restart. (c) is what repairs the state those clients leave behind. No CHANGELOG entry here; the release commit writes it.
Tests
TDD: written first, RED before the wiring, GREEN after.
tests/test_cli/test_daemon_supervisor_autostart.py(16 tests, every service-manager call mocked): service asked and nothing spawned; failed service start falls back to a scrubbed spawn carrying the service'sSLM_RANKING; direct spawn without a service drops client identity but keeps operator settings; exactlaunchctl/systemctl/schtasksargv including load, kickstart, a concurrent-loader race and a failed load; PID parsing; port matching and definition-environment parsing.tests/test_cli/test_supervised_daemon_handover.py(10 tests): takes over only when supervised; never stops an unknown holder or one run by another service; a refused hand-over is reported, not raised; wiring test thatstart_serverruns the hand-over before the lock wait; doctor PASS/WARN/silent cases, never printing values.tests/test_integration/test_daemon_single_owner_e2e.py(2 tests, real daemons on a pytest data folder and a free port, no real service registered):ensure_daemonfrom a client environment produces a daemon whose process environment has none of the client-scoped variables; a real unowned daemon started with a client environment is handed over to a real "service" daemon (service-manager queries stubbed to name it). The unowned daemon exits, exactly one daemon remains, and it carries the service setting.Non-vacuity: with only the
daemon.pyandunified_daemon.pywiring reverted, 7 failed, 21 passed (both integration tests fail: client variables reached the daemon, and the service daemon never became the owner). Restored: 28 passed.Regression packs: every test module that references the spawn path,
start_server, service inventory/live/installer, doctor, restart or the MCP daemon proxy, plustests/test_daemon: 1254 passed, 2 failed. Neither failure comes from this change.test_doctor_health_verdicts.py::test_deadline_text_mode_names_check_and_exits_onefails identically onmainon this machine; its 1 s doctor deadline expires inside the earlier daemon probe.test_maintenance_acts_on_the_bound_workspace.py::test_observe_buffer_ingests_into_the_routed_workspacepasses alone (it is timing-sensitive under-n).tests/test_architecture,tests/test_ci_guards,tests/release: 267 passed.lint-imports: 9 kept, 0 broken.ruff: clean.🤖 Generated with Claude Code