Skip to content

fix(bin): start the Herdr server in its own POSIX session - #4683

Open
MLA82 wants to merge 3 commits into
kunchenguid:mainfrom
MLA82:fm/werft-herdr-detached-session-public
Open

MLA82 wants to merge 3 commits into
kunchenguid:mainfrom
MLA82:fm/werft-herdr-detached-session-public

Conversation

@MLA82

@MLA82 MLA82 commented Sep 16, 2026 •

Copy link
Copy Markdown

Intent

A herdr server started from an interactive shell inherits that shell's POSIX session. When the
shell goes away the server is no longer a session leader, and from herdr 0.9.1 on a remote client
refuses such a host for saved machines with "remote server is not ready for saved machines",
because capabilities.detached_server_daemon is false.

Today a long-running server only satisfies that requirement by accident, when it happens to have
been started in a way that left it leading its own session. After a reboot, an unattended start
brings the server back without that property, and saved machines stop working until somebody
restarts it by hand.

This makes the server start as its own session leader, so the capability holds after an
unattended restart rather than depending on how it was launched.

What Changed

  • fm_backend_herdr_server_ensure now launches the named Herdr server through setsid when that utility is available, so the server leads its own POSIX session and Herdr advertises capabilities.detached_server_daemon (required before it accepts saved machines). Because setsid needs an executable rather than the fm_backend_herdr_cli shell helper, the detached launch resolves the session-selected client binary and reproduces the helper's HERDR_SESSION plus trailing --session <name> routing; hosts without setsid keep the previous backgrounded helper launch.
  • docs/herdr-backend.md documents the detached launch as the one path that bypasses fm_backend_herdr_cli, why the session leadership matters, and the unchanged fallback.
  • The server_ensure test's fake Herdr client records its own sid/pid, and the existing environment-scrubbing test asserts they match when setsid is present, while still checking the scrubbed environment and session routing.

Risk Assessment

✅ Low: The change is a narrow, guarded launch-path fix that preserves existing behavior on hosts without setsid, routes through the single shared server-start boundary used by every Linux caller, and is covered by a behavioral test asserting the launched process leads its own POSIX session; the only finding is a mechanical duplication of an existing selection rule.

Testing

Drove the real product: on this host (herdr 0.9.1, setsid present) fm_backend_herdr_server_ensure was run from an isolated XDG_CONFIG_HOME against a private fm-lab session, and the resulting server leads its own POSIX session and reports capabilities.detached_server_daemon=true; the same driver against the base commit's herdr.sh on the same host reproduces the reported failure (sid!=pid, capability false), so the regression fails before and passes after. Adversarial variants all passed live: launcher already a session/process-group leader still yields a session-leading server, a PATH without setsid(1) still starts a working server via the retained fallback, a session-selected client binary is used even with no herdr on PATH, and re-ensuring reuses the same server PID without restart. The environment-scrub/session-routing behaviour was only exercised by the unit suite, not driven against the live product, so it is reported as untested. The existing adapter unit suite was run targeted and passes in full (207 checks) once the shell locale is pinned to C — an unrelated de_DE decimal-comma artifact made one wait_for_working sleep-timing assertion fail in my ambient locale, untouched by this change. The doctor/systemd reboot path could not be driven because it hardcodes the live fm-remote session the intent forbids touching. No UI surface exists for this change, so evidence is CLI transcripts of the live server capability state.

  • Live validation: ✅ go - 6 of 8 scenarios driven live against the product
Scenario Result Live Evidence
Adapter-started herdr server leads its own POSIX session and advertises detached_server_daemon, so the host stays usable as a saved machine ✅ pass live ~/.no-mistakes/evidence/01M2P8WVC3PCR05HHZW08WS82S/herdr-detached-session-live.txt — real herdr 0.9.1: server pid=348323 sid=348323, capabilities.detached_server_daemon=true
Before the fix, the same launch on the same host produces a server that herdr refuses saved machines on (regression reproduces) ✅ pass live same transcript, BEFORE section: base 795e5e4 herdr.sh gives server pid=346841 sid=346794 and detached_server_daemon=false
Launcher that is already a session/process-group leader (setsid(2) would fail) still gets a session-leading server ✅ pass live same transcript, pgleader section run under setsid: server pid=349173 sid=349173, detached_server_daemon=true
Host without setsid(1) keeps the previous background launch and the server still comes up ✅ pass live ~/.no-mistakes/evidence/01M2P8WVC3PCR05HHZW08WS82S/herdr-detached-session-adversarial.txt — restricted PATH without setsid: server_ensure ok, status running=true
Session-selected client binary is launched even when no herdr exists on PATH ✅ pass live same adversarial transcript: PATH has no herdr, FM_BACKEND_HERDR_CLIENT_SESSION set; server started from ~/.local/bin/herdr with sid==pid and capability true
Re-running the ensure against an already-running server reuses it instead of starting or restarting one ✅ pass live ~/.no-mistakes/evidence/01M2P8WVC3PCR05HHZW08WS82S/herdr-server-ensure-idempotent.txt — identical server PID after both ensures
Startup environment scrub and explicit session routing survive the new setsid launch path ⏸️ untested no Only exercised by the unit suite (LC_ALL=C bash tests/fm-backend-herdr.test.sh, test_server_ensure_scrubs_home_and_harness_identity), which stubs the herdr client; the prior payload records no live…
A remote client accepts popos as a saved machine after an unattended reboot brings the server up via the systemd readiness unit ⏸️ untested no Requires rebooting popos and running fm-remote-doctor.sh --fix, which hardcodes HERDR_SESSION_NAME=fm-remote and would stop/replace the operator's live server (PID 2790299) that this change's intent ex…
Evidence: Live herdr 0.9.1 capability transcript (before fix / after fix / session-leader launcher)

Source: Live herdr 0.9.1 capability transcript (before fix / after fix / session-leader launcher)

--- BEFORE the fix (base 795e5e4) --- server_ensure: ok status: {"running":true,"version":"0.9.1","capabilities":{"detached_server_daemon":false,...}} server pid=346841 sid=346794 --- AFTER the fix (this branch) --- server_ensure: ok status: {"running":true,"version":"0.9.1","capabilities":{"detached_server_daemon":true,...}} server pid=348323 sid=348323 --- launcher already a session/process-group leader --- server pid=349173 sid=349173 detached_server_daemon=true

\### Live driver: real herdr 0.9.1, isolated XDG_CONFIG_HOME, private fm-lab-* session
\### The pre-existing fm-remote server (session fm-remote) is never contacted or stopped.

--- BEFORE the fix (bin/backends/herdr.sh from base 795e5e4) ---
== base: session=fm-lab-detach-base-346804 config=/tmp/fm-live-herdr-RgOwD3/cfg
server_ensure: ok
status: {"running":true,"version":"0.9.1","capabilities":{"live_handoff":true,"detached_server_daemon":false,"endpoint_protocol_generation":1,"surface_interest":true,"health_check":true}}
server pid=346841 sid=346794 ppid=346839
detached_server_daemon={"live_handoff":true,"detached_server_daemon":false,"endpoint_protocol_generation":1,"surface_interest":true,"health_check":true}

--- AFTER the fix (this branch) ---
== fixed: session=fm-lab-detach-fixed-348274 config=/tmp/fm-live-herdr-rNIyhy/cfg
server_ensure: ok
status: {"running":true,"version":"0.9.1","capabilities":{"live_handoff":true,"detached_server_daemon":true,"endpoint_protocol_generation":1,"surface_interest":true,"health_check":true}}
server pid=348323 sid=348323 ppid=1156
detached_server_daemon=true

--- launcher already a session/process-group leader (setsid(2) would fail; util-linux forks) ---
== pgleader: session=fm-lab-detach-pgleader-349137 config=/tmp/fm-live-herdr-Y6IsEX/cfg
server_ensure: ok
status: {"running":true,"version":"0.9.1","capabilities":{"live_handoff":true,"detached_server_daemon":true,"endpoint_protocol_generation":1,"surface_interest":true,"health_check":true}}
server pid=349173 sid=349173 ppid=1156
detached_server_daemon=true
Evidence: Adversarial variants: no setsid(1) on PATH, and session-selected client with no herdr on PATH

Source: Adversarial variants: no setsid(1) on PATH, and session-selected client with no herdr on PATH

--- host WITHOUT setsid(1) --- setsid on PATH: no server_ensure: ok status: running=true, detached_server_daemon=false --- session-selected client, no 'herdr' on PATH --- selected client: ~/.local/bin/herdr server_ensure: ok status: running=true, detached_server_daemon=true server pid=353748 sid=353748

\### Adversarial variants (same live harness, real herdr 0.9.1)

--- host WITHOUT setsid(1) on PATH: plain background launch must still bring the server up ---
== mode=nosetsid session=fm-lab-detach-nosetsid3-352814
setsid on PATH: no
server_ensure: ok
status: {"running":true,"version":"0.9.1","capabilities":{"live_handoff":true,"detached_server_daemon":false,"endpoint_protocol_generation":1,"surface_interest":true,"health_check":true}}
launcher shell: pid=352814 sid=352809 pgid=352811
server pid=352936 sid=352809

--- session-selected client binary, NO 'herdr' on PATH at all ---
== mode=selected-bin session=fm-lab-detach-selbin2-353626
herdr on PATH: no; selected client: ~/.local/bin/herdr
server_ensure: ok
status: {"running":true,"version":"0.9.1","capabilities":{"live_handoff":true,"detached_server_daemon":true,"endpoint_protocol_generation":1,"surface_interest":true,"health_check":true}}
launcher shell: pid=353626 sid=352809 pgid=352811
server pid=353748 sid=353748
Evidence: Idempotence: re-ensure reuses the running detached server

Source: Idempotence: re-ensure reuses the running detached server

first ensure: ok second ensure: ok server pids after first ensure: 362518 server pids after second ensure: 362518 idempotent: same single server reused, no restart capabilities: true

\### Re-running server_ensure against an already-running detached server
first ensure: ok
second ensure: ok
server pids after first ensure: 362518 
server pids after second ensure: 362518 
idempotent: same single server reused, no restart
capabilities: true

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 1 info
  • ℹ️ bin/backends/herdr.sh:1669 - bin/backends/herdr.sh:1669-1671 re-implements the session-scoped client-binary rule that fm_backend_herdr_cli already defines once at bin/backends/herdr.sh:388-392 (client_bin=herdr, overridden by fm_backend_herdr_bin only when FM_BACKEND_HERDR_CLIENT_SESSION equals the session). The two copies agree today, but the setsid branch bypasses fm_backend_herdr_cli entirely, so a later change to client selection (for example a new selection input alongside the existing protocol_mismatch re-selection at herdr.sh:404-415) would apply only to the non-setsid fallback, and the server would launch with a different client than every other call for that session. Smallest mechanical remedy: extract the rule into a helper (e.g. fm_backend_herdr_client_bin <session>) and call it from both sites, keeping behavior identical.
✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 6 of 8 scenarios driven live against the product
Scenario Result Live Evidence
Adapter-started herdr server leads its own POSIX session and advertises detached_server_daemon, so the host stays usable as a saved machine ✅ pass live ~/.no-mistakes/evidence/01M2P8WVC3PCR05HHZW08WS82S/herdr-detached-session-live.txt — real herdr 0.9.1: server pid=348323 sid=348323, capabilities.detached_server_daemon=true
Before the fix, the same launch on the same host produces a server that herdr refuses saved machines on (regression reproduces) ✅ pass live same transcript, BEFORE section: base 795e5e4 herdr.sh gives server pid=346841 sid=346794 and detached_server_daemon=false
Launcher that is already a session/process-group leader (setsid(2) would fail) still gets a session-leading server ✅ pass live same transcript, pgleader section run under setsid: server pid=349173 sid=349173, detached_server_daemon=true
Host without setsid(1) keeps the previous background launch and the server still comes up ✅ pass live ~/.no-mistakes/evidence/01M2P8WVC3PCR05HHZW08WS82S/herdr-detached-session-adversarial.txt — restricted PATH without setsid: server_ensure ok, status running=true
Session-selected client binary is launched even when no herdr exists on PATH ✅ pass live same adversarial transcript: PATH has no herdr, FM_BACKEND_HERDR_CLIENT_SESSION set; server started from ~/.local/bin/herdr with sid==pid and capability true
Re-running the ensure against an already-running server reuses it instead of starting or restarting one ✅ pass live ~/.no-mistakes/evidence/01M2P8WVC3PCR05HHZW08WS82S/herdr-server-ensure-idempotent.txt — identical server PID after both ensures
Startup environment scrub and explicit session routing survive the new setsid launch path ⏸️ untested no Only exercised by the unit suite (LC_ALL=C bash tests/fm-backend-herdr.test.sh, test_server_ensure_scrubs_home_and_harness_identity), which stubs the herdr client; the prior payload records no live…
A remote client accepts popos as a saved machine after an unattended reboot brings the server up via the systemd readiness unit ⏸️ untested no Requires rebooting popos and running fm-remote-doctor.sh --fix, which hardcodes HERDR_SESSION_NAME=fm-remote and would stop/replace the operator's live server (PID 2790299) that this change's intent ex…
  • bash /tmp/fm-live-real.sh &lt;worktree&gt; fixed — real herdr 0.9.1 server started through fm_backend_herdr_server_ensure in an isolated XDG_CONFIG_HOME/private fm-lab session; asserted sid==pid and .server.capabilities.detached_server_daemon
  • same live driver against git show 795e5e4:bin/backends/herdr.sh (before-fix baseline) — reproduces sid!=pid and detached_server_daemon=false
  • setsid /tmp/fm-live-real.sh &lt;worktree&gt; pgleader — launcher already a session/process-group leader
  • live driver variant with a restricted PATH that omits setsid(1) — fallback launch still brings the server up
  • live driver variant with no herdr on PATH and FM_BACKEND_HERDR_CLIENT_SESSION/FM_BACKEND_HERDR_BIN set to the absolute client
  • live driver calling fm_backend_herdr_server_ensure twice against the same running server (idempotence, same PID)
  • LC_ALL=C bash tests/fm-backend-herdr.test.sh — 207 checks pass, including test_server_ensure_scrubs_home_and_harness_identity with its new sid==pid assertion
  • herdr session list --json + ps -o pid=,sid= -p 2790299 before/after — confirmed the pre-existing fm-remote server untouched and no leftover test servers
✅ **Document** - passed

✅ No issues found.

⚠️ **Lint** - 1 warning
  • ⚠️ linter found issues (exit code 1)
✅ **Push** - passed

✅ No issues found.

@MLA82
MLA82 force-pushed the fm/werft-herdr-detached-session-public branch from 812bfe7 to 992e082 Compare September 20, 2026 17:18
MLA82 added a commit to MLA82/firstmate that referenced this pull request Sep 28, 2026
…guid#4366 and PR kunchenguid#4683

- tests/fm-control-relaunch.test.sh: read the whole recorded launch command
  instead of the retired inline 'encode launch-brief' grep shape (needed by
  PR kunchenguid#5288's own new test).
- tests/fm-backend-herdr-launcher-workspace-e2e.test.sh: pass FM_HOME to the
  teardown call explicitly, kept as a precaution.

Dropped: the 10s->20s Herdr server poll-budget widening in
bin/backends/herdr.sh. It was PR kunchenguid#4683's own fix for its own setsid-wrapped
server launch; without kunchenguid#4683, bin/backends/herdr.sh reverts to the original,
unmodified 10s poll and needs no widening.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant