Skip to content

test(fm-turnend-guard): make adapter tests hermetic; add env-leak regression guard - #46

Merged
trillium merged 2 commits into
mainfrom
fix/robots-qgcg-turnend-guard-hermetic-env
Aug 5, 2026
Merged

trillium merged 2 commits into
mainfrom
fix/robots-qgcg-turnend-guard-hermetic-env

Conversation

@trillium

@trillium trillium commented Aug 5, 2026 •

Copy link
Copy Markdown
Owner

Intent

Fix robots-qgcg: the test 'healthy no-supervision-needed native stop must allow' in tests/fm-turnend-guard.test.sh fails on a clean origin/main worktree on macOS (exit 2, expected 0) while passing in CI, which masks real regressions in the turn-end guard suite and forces every branch touching supervision to hand-baseline the test.

Root cause found: bin/fm-turnend-guard.sh:62-66 resolves its home as FM_HOME > FM_ROOT_OVERRIDE > the guard script's own repo root. Every fixture installs the guard into its own /bin/, so with those unset each fixture self-resolves to its own state/. run_hook (line 186) pins FM_HOME per-fixture, which is why the direct-guard tests were always immune; the harness-adapter tests (Grok/codex) invoke the hook the way the REAL adapters do, with no per-call pin, so they inherit the ambient environment. An agent session running inside a live firstmate home exports FM_HOME, so the grok adapter handed the shared guard that real home - which had an in-flight task and a fresh beacon - and the idle fixture reported '1 task(s) in flight ... last beat: 13s ago' and exited 2. CI exports no FM_HOME, so nothing leaked there.

DELIBERATE DECISION - fix the test, not the guard: FM_HOME winning over repo-root state is the guard's designed precedence, asserted by its own passing tests ('blocks from active FM_HOME state, not only repo-root state' and 'ignores stale repo-root state when FM_HOME is set'), and no adapter - Grok, codex, or Claude - pins it. So the product behavior is correct and must NOT be changed. This restores the file's own stated line-10 invariant ('All hermetic over temp dirs'). Reviewers should not propose changing bin/fm-turnend-guard.sh or making the adapters pin FM_HOME; that would break the designed precedence.

Change is test-only, +33 lines: (1) unset FM_HOME FM_ROOT_OVERRIDE FM_STATE_OVERRIDE FM_CONFIG_OVERRIDE at file scope; (2) a new test_suite_environment_is_hermetic that asserts none of those leak in, so a future regression fails loudly and names the offending variable instead of silently making every adapter verdict describe the developer's own live home.

Also noted deliberately: the sibling adapter cases that expect exit 2 were previously passing for the WRONG reason on such a box (host state also read as in-flight) and now pass for the right one.

Scope was audited: every sibling test file invoking a guard/supervision script pins FM_HOME except tests/fm-wake-queue.test.sh, which pins FM_STATE_OVERRIDE (the highest-precedence state selector) on every invocation and is therefore already hermetic - so no collateral changes were made on purpose.

Verified before pushing: 51/51 pass; 51/51 still pass under a deliberately hostile FM_HOME + FM_STATE_OVERRIDE; dropping the unset reproduces the original failure and trips the new guard first; bin/fm-lint.sh clean; bin/fm-test-run.sh --check-coverage clean (total=124 parallel=24 serial=89 herdr=11).

What Changed

  • Added unset FM_HOME FM_ROOT_OVERRIDE FM_STATE_OVERRIDE FM_CONFIG_OVERRIDE at file scope in tests/fm-turnend-guard.test.sh so harness-adapter tests (Grok/codex) no longer inherit an ambient developer home and produce verdicts about the fixture rather than the live session.
  • Added test_suite_environment_is_hermetic — a regression guard that explicitly asserts none of those four variables are set when the suite runs, naming the offending variable on failure so future leaks are caught loudly instead of silently flipping adapter exits.
  • Updated docs/turnend-guard.md regression coverage list to include suite environment hermeticity as a documented test dimension.

Risk Assessment

✅ Low: Test-only change (+33 lines) that fixes a real hermeticity bug: the file-scope unset clears leaked env vars at script startup, the new hermetic regression test checks observable runtime state (not source text), placement in execution order is correct, and no guard or adapter behavior is touched.

Testing

Ran tests/fm-turnend-guard.test.sh both in a normal environment and under a hostile FM_HOME=/tmp/fake-hostile-fm-home (simulating a live firstmate session); 51/51 pass in both cases. Separately reproduced the pre-fix failure by running a copy of the file without the unset line under the hostile FM_HOME — the suite correctly exits at test_suite_environment_is_hermetic with not ok - FM_HOME leaked into the suite, proving the fix is both correct and guarded by the new regression test.

Evidence: Normal run — 51/51 pass
ok - fm_supervision_unhealthy: false with no state/*.meta at all
ok - fm_supervision_unhealthy: true with in-flight task and no beacon ever
ok - fm_supervision_unhealthy: true with in-flight task and a beacon far outside the grace window
ok - fm_supervision_unhealthy: false with in-flight task and a fresh beacon
ok - fm_supervision_status: FM_SUP_QUEUE_PENDING tracks state/.wake-queue
ok - fm_supervision_needed: X-mode relay poll needs supervision without changing the task predicate
ok - fm-turnend-guard: silent no-op with nothing in flight
ok - fm-turnend-guard: blocks when a fresh beacon has no live watcher lock
ok - fm-turnend-guard: blocks on a dead watcher lock even when the beacon is fresh
ok - fm-turnend-guard: silent no-op with a live watcher lock and fresh beacon
ok - fm-turnend-guard: blocks on a live watcher lock with an ancient beacon
ok - fm-turnend-guard: blocks with the exact required reason in the primary when unhealthy
ok - fm-turnend-guard: blocks from active FM_HOME state, not only repo-root state
ok - fm-turnend-guard: X-mode repair reason sources the cadence config
ok - fm-turnend-guard: ignores stale repo-root state when FM_HOME is set
ok - fm-turnend-guard: uses FM_STATE_OVERRIDE ahead of FM_HOME/state
ok - fm-turnend-guard: stop_hook_active=true always allows the stop (never blocks twice in one turn)
ok - fm-turnend-guard: blocks a blind turn end in a secondmate's own home (.fm-secondmate-home no longer excludes it)
ok - fm-turnend-guard: idle-by-default - silent in a secondmate home with nothing in flight
ok - fm-turnend-guard: stop_hook_active=true allows the stop in a secondmate home (never blocks twice in one turn)
ok - fm-turnend-guard: secondmate deferred-death recovery - silent while watched, forces re-arm once the watcher exits
ok - fm-turnend-guard: inert in a secondmate's own child worktree (linked git worktree) even when unhealthy
ok - fm-turnend-guard: blocks a blind turn end in a treehouse-leased LINKED secondmate home (marker force-include)
ok - fm-turnend-guard: an invalid (empty) marker cannot spoof inclusion; linked worktree stays exempt
ok - fm-turnend-guard: a non-ASCII marker cannot spoof inclusion; linked worktree stays exempt
ok - fm-turnend-guard: inert in a crewmate/scout task worktree (linked git worktree) even when unhealthy
ok - fm-turnend-guard: fails open (never blocks) when jq is missing
ok - fm-turnend-guard: silent no-op on empty stdin
ok - fm-turnend-guard: runs well under the generous timing margin (1s)
ok - fm-turnend-guard: suite environment is hermetic (no FM_HOME/state overrides leak in)
ok - fm-turnend-guard-grok: forces one explicitly marked same-session resume when the shared predicate blocks
ok - fm-turnend-guard-grok: legacy environment loop guard prevents a nested resume loop
ok - fm-turnend-guard-grok: native false delegates blocking feedback with zero resume processes
ok - fm-turnend-guard-grok: native true remains bounded and starts no resume process
ok - fm-turnend-guard-grok: both spellings are typed and camelCase has deterministic precedence
ok - fm-turnend-guard-grok: malformed, invalidly typed, and missing-prerequisite payloads start neither path
ok - fm-turnend-guard-grok: missing jq and no-supervision-needed stops stay silent and bounded
ok - .codex/hooks.json: Stop hook uses hook process root when payload cwd is outside
ok - .codex/hooks.json: Stop hook ignores nested git root guard scripts
ok - .opencode primary plugin: guard path is anchored to worktree, not directory
ok - .pi primary extension: no-tool and multi-tool runs each inject exactly one guard follow-up
ok - .pi primary extension: delivery failure resets the logical-run latch
ok - fm-turnend-guard --claude: re-blocks a loop-guarded stop while unhealthy and unclaimed (incident regression)
ok - fm-turnend-guard --claude: X-mode-only homes re-block when auto-arm recovery is absent
ok - fm-turnend-guard --claude: allows the stop when the Stop auto-arm owner holds this home
ok - fm-turnend-guard --claude: fresh rewake epoch prevents a duplicate continuation for the same event
ok - fm-turnend-guard --claude: stale rewake epoch does not allow a blind stop
ok - fm-turnend-guard --claude: re-block budget stays below the 8-block cap and resets after degraded allow
ok - fm-turnend-guard --claude: any allow resets the consecutive-block budget
ok - fm-turnend-guard --claude: bounded claim wait avoids a token-consuming forced continuation
ok - fm-turnend-guard --claude: secondmate home re-blocks unclaimed and allows auto-arm-claimed stops
Evidence: Regression proof — without unset, hermetic guard catches FM_HOME leak
ok - fm_supervision_unhealthy: false with no state/*.meta at all
ok - fm_supervision_unhealthy: true with in-flight task and no beacon ever
ok - fm_supervision_unhealthy: true with in-flight task and a beacon far outside the grace window
ok - fm_supervision_unhealthy: false with in-flight task and a fresh beacon
ok - fm_supervision_status: FM_SUP_QUEUE_PENDING tracks state/.wake-queue
ok - fm_supervision_needed: X-mode relay poll needs supervision without changing the task predicate
ok - fm-turnend-guard: silent no-op with nothing in flight
ok - fm-turnend-guard: blocks when a fresh beacon has no live watcher lock
ok - fm-turnend-guard: blocks on a dead watcher lock even when the beacon is fresh
ok - fm-turnend-guard: silent no-op with a live watcher lock and fresh beacon
ok - fm-turnend-guard: blocks on a live watcher lock with an ancient beacon
ok - fm-turnend-guard: blocks with the exact required reason in the primary when unhealthy
ok - fm-turnend-guard: blocks from active FM_HOME state, not only repo-root state
ok - fm-turnend-guard: X-mode repair reason sources the cadence config
ok - fm-turnend-guard: ignores stale repo-root state when FM_HOME is set
ok - fm-turnend-guard: uses FM_STATE_OVERRIDE ahead of FM_HOME/state
ok - fm-turnend-guard: stop_hook_active=true always allows the stop (never blocks twice in one turn)
ok - fm-turnend-guard: blocks a blind turn end in a secondmate's own home (.fm-secondmate-home no longer excludes it)
ok - fm-turnend-guard: idle-by-default - silent in a secondmate home with nothing in flight
ok - fm-turnend-guard: stop_hook_active=true allows the stop in a secondmate home (never blocks twice in one turn)
ok - fm-turnend-guard: secondmate deferred-death recovery - silent while watched, forces re-arm once the watcher exits
ok - fm-turnend-guard: inert in a secondmate's own child worktree (linked git worktree) even when unhealthy
ok - fm-turnend-guard: blocks a blind turn end in a treehouse-leased LINKED secondmate home (marker force-include)
ok - fm-turnend-guard: an invalid (empty) marker cannot spoof inclusion; linked worktree stays exempt
ok - fm-turnend-guard: a non-ASCII marker cannot spoof inclusion; linked worktree stays exempt
ok - fm-turnend-guard: inert in a crewmate/scout task worktree (linked git worktree) even when unhealthy
ok - fm-turnend-guard: fails open (never blocks) when jq is missing
ok - fm-turnend-guard: silent no-op on empty stdin
ok - fm-turnend-guard: runs well under the generous timing margin (1s)
not ok - FM_HOME leaked into the suite (=/tmp/fake-hostile-fm-home); adapter verdicts would read that home, not the fixture
Evidence: Hermetic guard output (hostile env, with fix)
ok - fm-turnend-guard: suite environment is hermetic (no FM_HOME/state overrides leak in)
ok - fm-turnend-guard-grok: missing jq and no-supervision-needed stops stay silent and bounded

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

✅ **Test** - passed

✅ No issues found.

  • FM_HOME=/tmp/fake-hostile-fm-home bash tests/fm-turnend-guard.test.sh — 51/51 pass; the unset at line 27 clears the leaked var before any test runs, including the grok adapter tests that previously failed
  • bash tests/fm-turnend-guard.test.sh — 51/51 pass in a clean environment
  • bash tests/_regression-test-$$.sh (copy with unset line removed) with FM_HOME=/tmp/fake-hostile-fm-home — 29 ok then not ok - FM_HOME leaked into the suite (=/tmp/fake-hostile-fm-home) + exit 1, reproducing the original leak and confirming the new hermetic guard catches it
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

…be the fixture

'healthy no-supervision-needed native stop must allow' failed on a clean
origin/main worktree on macOS while passing in CI (robots-qgcg).

The guard resolves its home as FM_HOME > FM_ROOT_OVERRIDE > the guard
script's own repo root (bin/fm-turnend-guard.sh:62-66). Every fixture here
installs the guard into its own <fixture>/bin/, so with those unset each
fixture self-resolves to its own state/. run_hook pins FM_HOME per-fixture,
which is why the direct-guard tests were always immune - but the harness
adapter tests invoke the hook the way the real Grok/codex adapters do, with
no per-call pin, so they inherited the ambient environment.

An agent session running inside a live firstmate home exports FM_HOME. The
grok adapter then handed the shared guard that REAL home, which had an
in-flight task and a fresh beacon, so the assertion's idle fixture reported
"1 task(s) in flight ... last beat: 13s ago" and exited 2. CI exports no
FM_HOME, so nothing leaked there. The sibling adapter cases that expect exit
2 were passing for the wrong reason on such a box - the host state also read
as in-flight - and now pass for the right one.

This is a test-hermeticity defect, not a product one: FM_HOME winning over
repo-root state is the guard's designed precedence, asserted by its own
passing tests ("blocks from active FM_HOME state, not only repo-root state"
and "ignores stale repo-root state when FM_HOME is set"), and no adapter -
Grok, codex, or Claude - pins it. So the fix restores the file's stated
"all hermetic over temp dirs" invariant rather than changing the guard.

Adds test_suite_environment_is_hermetic to pin the invariant, so a future
leak fails loudly and names the variable instead of silently making every
adapter verdict describe the developer's own home.

Verified: 51/51 pass, including under a deliberately hostile
FM_HOME + FM_STATE_OVERRIDE; dropping the unset reproduces the original
failure and trips the new guard first. bin/fm-lint.sh and
bin/fm-test-run.sh --check-coverage both clean.

Fixes robots-qgcg
@coderabbitai

coderabbitai Bot commented Aug 5, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

@trillium, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 34 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 45494eb0-609f-4d01-848c-ff227dc300e9

📥 Commits

Reviewing files that changed from the base of the PR and between 0aa21d3 and d889459.

📒 Files selected for processing (2)
  • docs/turnend-guard.md
  • tests/fm-turnend-guard.test.sh

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@trillium trillium changed the title test(turnend-guard): scrub ambient FM_HOME so adapter verdicts describe the fixture test(fm-turnend-guard): make adapter tests hermetic; add env-leak regression guard Aug 5, 2026
@trillium
trillium merged commit ba51491 into main Aug 5, 2026
10 of 11 checks passed
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