Skip to content

fix: forbid temporary-HOME no-mistakes runs that leave a respawning launchd daemon - #5367

Closed
nodoyuna wants to merge 2 commits into
kunchenguid:mainfrom
nodoyuna:fm/fm-orph-nm-rule
Closed

nodoyuna wants to merge 2 commits into
kunchenguid:mainfrom
nodoyuna:fm/fm-orph-nm-rule

Conversation

@nodoyuna

Copy link
Copy Markdown

Problem

After a Firstmate task finished and was cleaned up, a no-mistakes daemon kept running and respawning.
It was the launchd job com.kunchenguid.no-mistakes.daemon.049534d3, installed by a real no-mistakes run that had a temporary HOME and NM_HOME.

no-mistakes daemon start installs a KeepAlive=true, RunAtLoad=true LaunchAgent labeled com.kunchenguid.no-mistakes.daemon.<sha256(realpath(root))[:8]>.
With a temporary HOME, the plist is written under <tmp>/home/Library/LaunchAgents, but launchctl bootstrap gui/<uid> loads the job into the real per-user domain, which HOME does not scope.
Deleting the temporary directory removes the plist file but leaves the loaded job in place.
Killing the daemon only makes launchd respawn it, and the new process recreates its root (a fresh config.yaml and daemon.lock).
The only way to end it is launchctl bootout gui/<uid>/<label>.

Firstmate's own bin/ never registers a no-mistakes service, and its deterministic tests replace the binary with fakes.
The leak comes from an agent-driven step, such as a worker experiment or the agent-driven no-mistakes Test step, running the real CLI with a temporary HOME/NM_HOME.

Reproduction (from the diagnosis)

  • launchctl list showed com.kunchenguid.no-mistakes.daemon.049534d3 next to the real daemon ...b870f7aa.
  • 049534d3 is the sha256 prefix of /private/var/folders/.../tmp.X2jzezfrC7/nm, and b870f7aa is the prefix of /Users/javiermoya/.no-mistakes.
  • After pid 44058 was killed, launchd respawned it as 25699, which recreated its root.
  • It stopped only after launchctl bootout.
  • The label formula in this rule gives the live label on this host:
$ NM_HOME=$HOME/.no-mistakes; printf %s "$(cd "$NM_HOME" && pwd -P)" | shasum -a 256 | cut -c1-8
b870f7aa
$ launchctl list | grep -o 'com.kunchenguid.no-mistakes.daemon[.a-z0-9]*'
com.kunchenguid.no-mistakes.daemon.b870f7aa

Fix

This is fix 4 of the orphan-process diagnosis, Firstmate side only.

  • bin/fm-brief.sh gains rule 8 in both the ship and the scout brief safety rules.
    The rule text lives in one single-quoted NM_TEMP_HOME_RULE variable, so the command renders unexpanded and has a single owner inside the script.
    It says never to run the real no-mistakes with a temporary HOME or NM_HOME.
    If that is unavoidable, the worker must boot out that temporary daemon job, never the shared one, in the same cleanup.
    The bootout has to run before the directory is deleted and while NM_HOME names the temporary root, which is $HOME/.no-mistakes when only HOME was temporary.
    The command is launchctl bootout gui/$(id -u)/com.kunchenguid.no-mistakes.daemon.$(printf %s "$(cd "$NM_HOME" && pwd -P)" | shasum -a 256 | cut -c1-8).
  • .no-mistakes.yaml test.instructions gets the same rule, because the no-mistakes Test agent never sees a worker brief.
    This is the one deliberate second copy.
  • Secondmate charters are unchanged: they supervise, and their workers get these same generated briefs.

Out of scope and not implemented: the optional read-only startup check for leftover labels, any change to the no-mistakes project, and the other fixes from the diagnosis, which ship as separate PRs.

Tests (TDD)

  • tests/fm-brief.test.sh test_ship_and_scout_forbid_temp_home_no_mistakes renders no-mistakes, direct-PR, local-only and scout briefs.
    For each one it asserts the prohibition and the exact, unexpanded bootout command, using a fixed-string match.
  • tests/fm-nm-test-contract.test.sh test_nm_test_instructions_forbid_temp_home_daemon parses .no-mistakes.yaml as YAML, the same way the existing contract test does, and asserts the same rule and command in test.instructions.

RED, before the fix:

$ bash tests/fm-brief.test.sh
...
ok - fm-brief.sh: ship and scout scaffolds teach validation-round pauses
not ok - no-mistakes brief did not forbid a temporary-HOME no-mistakes run
$ bash tests/fm-nm-test-contract.test.sh
ok - no-mistakes does not configure commands.test
not ok - test.instructions must forbid a temporary-HOME no-mistakes run (missing: 'Never run the real no-mistakes with a temporary HOME or NM_HOME')

GREEN, after the fix:

$ bash tests/fm-brief.test.sh      # rc=0, 29 ok
ok - fm-brief.sh: ship and scout briefs forbid temporary-HOME no-mistakes runs
$ bash tests/fm-nm-test-contract.test.sh
ok - no-mistakes does not configure commands.test
ok - no-mistakes Test instructions forbid a temporary-HOME daemon
$ bin/fm-test-run.sh tests/fm-brief.test.sh tests/fm-nm-test-contract.test.sh tests/fm-ask-user-authority.test.sh tests/fm-classify-corr-token.test.sh tests/fm-control-relaunch.test.sh tests/fm-secondmate-safety.test.sh tests/fm-spawn-dispatch-profile.test.sh tests/fm-subagent-pretool-check.test.sh tests/fm-tangle-guard.test.sh tests/fm-task-delivery.test.sh tests/fm-voice-relay.test.sh tests/fm-lint.test.sh tests/fm-gate-refuse.test.sh
FM_TEST_SUMMARY total=13 failed=0 skipped_gate=0 duration_ms=930206
$ bin/fm-lint.sh                  # rc=0
fm-lint.sh: ShellCheck 0.11.0 (pinned 0.11.0)
fm-lint-workflows.sh: actionlint 1.7.12 (pinned 1.7.12)
fm-lint-workflows.sh: 3 workflow files valid
$ bin/fm-doc-audience-check.sh    # rc=0

That run covers every script that consumes fm-brief.sh, plus the tests that read .no-mistakes.yaml.
The live Herdr e2e scripts that --changed selects were deliberately not run, because this task is not authorized to drive Herdr lifecycle.
After the review rewording, fm-brief.test.sh, fm-nm-test-contract.test.sh and fm-lint.sh were re-run and passed.

Independent review

A separate Claude subagent reviewed the change read-only from four angles: functional tests, architecture and isolation, anti-slop simplification, and requirement alignment.
It found no blockers.

  • It confirmed both new tests fail on the origin/main sources and pass with the fix.
  • It confirmed the command renders unexpanded in every ship mode and in the scout brief.
  • It confirmed the formula matches the live label and that the variable is safe on Bash 3.2.
  • Addressed: it said the case where only HOME is temporary was implicit, where an unset NM_HOME would make cd "" hash the wrong directory. The rule now says $HOME/.no-mistakes.
  • Addressed: it said the bootout must run while NM_HOME still names the temporary root. The rule now says so and adds "never the shared one", so it cannot be read as conflicting with rule 7.
  • Addressed: it flagged mismatched "respawns forever" wording between the two copies, and suggested collapsing the shellcheck comment.
  • Not addressed, noted as a follow-up: when only NM_HOME is temporary and HOME is real, the temporary label's plist probably stays in the real ~/Library/LaunchAgents after bootout and could reload at the next login.
    The reviewer inferred this and did not reproduce it.
    The rule's main line (never do this) already prevents it, and the task asked for exactly the bootout command, so removing the plist is left to the upstream fix below.

Suggested upstream issue for no-mistakes

daemon start under a temporary HOME/NM_HOME installs a KeepAlive LaunchAgent into the real gui/<uid> domain that outlives its root

On macOS, no-mistakes daemon start with a non-default root (temporary HOME and/or NM_HOME) bootstraps com.kunchenguid.no-mistakes.daemon.<hash> (KeepAlive=true, RunAtLoad=true) into the real per-user launchd domain, which HOME does not scope.
Deleting the temporary root removes neither the loaded job nor, when HOME is real, the plist; killing the daemon makes launchd respawn it, and it recreates the deleted root.
Only launchctl bootout gui/<uid>/<label> stops it.
Suggested behavior: do not install a managed service when the root is non-default or under a temporary directory (or offer an opt-out such as --no-service / an env var), and make daemon stop (or a new daemon uninstall) boot out and remove the service it installed.
A daemon whose root disappears could also exit instead of recreating it.

Instructions acknowledged

  • I read /Users/javiermoya/.claude/CLAUDE.md. Applied: everything is in English, TDD was followed (RED recorded before GREEN), new code is within the size limits, and there are no filler abstractions. The independent multi-angle review above was run. No merge is requested or performed here.
  • /ssd/datos-mac/workspace/config/development-policy.md is absent (No such file or directory).
  • I read this repo's AGENTS.md and CONTRIBUTING.md, and loaded .agents/skills/firstmate-coding-guidelines/SKILL.md before editing. Applied: the one-owner rule (one variable for both scaffolds, with the YAML copy justified above), tests that exercise the public scaffold and parsed config, shellcheck and bin/fm-lint.sh clean, and no commands.test added.
  • Per the task's delivery instructions, this PR was raised directly, without the no-mistakes pipeline.
    The PR must be raised via no-mistakes check may therefore report its expected result for a non-pipeline PR.

…aunchd daemon

A real no-mistakes run with a temporary HOME or NM_HOME registers a
KeepAlive launchd job in the real gui/<uid> domain; deleting the temp
directory removes the plist file but not the loaded job, so the daemon
respawns forever and recreates its root. Add the rule, with the exact
bootout command, to the ship and scout brief safety rules and to the
repo's no-mistakes Test-agent instructions.
@nodoyuna

Copy link
Copy Markdown
Author

[AGENT] Closing as stale (no activity for more than 5 days) during the backlog cleanup Javi requested on 2026-10-16. The branch is kept; reopen this PR if the work is still needed.

@nodoyuna nodoyuna closed this Oct 16, 2026
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