Repository navigation
fix(bin): bind Herdr task records to the pane's terminal identity - #5435
AgardnerAU wants to merge 30 commits into
Conversation
Herdr reissues a closed workspace's id, and with it its pane ids, after a server restart, so a task record that bound only <session>:<pane> could name an unrelated live pane and read it as the task's agent. Spawn now records the pane's terminal_id as herdr_terminal_id=, and one adapter identity check gates liveness, existence, capture, input, control, and every pane close. A pane now held by another terminal reads as the task's endpoint gone: nothing reaches it and cleanup closes nothing while still running its landed-work checks. Legacy records match only while the pane works inside the recorded worktree, and an unreadable identity refuses. Cleanup also accepts a finished Herdr record whose window= binding was cleared, keeping its herdr_* lines as history without any Herdr call. A live lab regression reproduces the reissue and pins the fix.
…husk after restart
…euse-misbinds-task # Conflicts: # bin/fm-spawn.sh
…euse-misbinds-task # Conflicts: # bin/fm-teardown.sh # docs/configuration.md # docs/herdr-backend.md
…arts A refused validation left the previous record bound, so a later identity check could read a record that no longer passed validation. The endpoint safety test also read a variable name that no longer exists.
…es no record bound
Resolve conflicts with upstream's bounded, crash-isolated session-start endpoint read: bind the task record inside the isolated read so the Herdr identity check still reads this task's own record, and adapt upstream's newer session-start Herdr fakes to answer a live pane with its pane JSON.
[Medium impact] The changes affect Herdr task reads and actions, watching, and teardown within the Herdr backend. Do not merge yet: spawn abort and teardown can still close a replacement terminal. Reviews (7) · Last reviewed commit: "Merge branch 'main' of https://github.co..." · Reviewed by Greptile |
|
Merged current main into this branch (head 8623583); it is up to date with main and ready for CI. |
… finding (ci-1). A new spawn could publish a task record naming the wrong Herdr terminal, or no terminal at all. The rule now is: a Herdr task record is published only when the pane's terminal ID is confirmed, and that ID must match what Herdr returned when it created the pane. What changed: - `bin/backends/herdr.sh`: every pane-create path now keeps the terminal ID from Herdr's create response, if the response has one. That covers `fm_backend_herdr_create_task`, which now prints it as an optional third field, plus the two presentation (projection) create paths. - `bin/fm-spawn.sh`: spawn and relaunch keep that create-time ID. On relaunch, an adopted pane keeps the ID from its existing record. Just before the record is written, the pane is always read back. If that read fails, or returns a different terminal ID from the kept one, spawn stops with "refusing to publish its record" and writes nothing. The old behaviour (log a warning, write the record without an ID, and fall back to checking the working directory) is gone. - Seven Herdr test scripts that read `create_task` output now accept the optional third field. - `tests/fm-control-relaunch.test.sh`: added one mismatch test, `test_herdr_rebind_refuses_a_terminal_identity_the_create_did_not_return`. The fake create returns `term-created`, the read-back returns `term-survivor`, and the test checks that relaunch exits 1, the record is untouched, no terminal ID is stored and no agent is launched. Verification: - As you asked, I ran only the `fm-spawn*` tests that touch Herdr: `fm-spawn-compact-adviser-disable-remote` and `fm-spawn-orca-worktree`. Both pass. - `shellcheck -x` is clean on the three files I changed. - **The new test has not run.** It is in `fm-control-relaunch.test.sh`, which is outside the tests you allowed. One earlier run of that file on this machine stopped at an unrelated test, the Herdr Pi relaunch. That test calls `sed -i` in the GNU form, which macOS sed does not accept, and it then fails with "pi executable not found on PATH". My new test comes after it, so it never ran. That failure is in code I did not change. My attempt to run the file without the Pi test was denied. **Before committing:** the earlier timed-out agent left a scratch folder, `.tmp-ci/`. It is not ignored by git, so it would be committed with everything else. I tried to delete it but that was not allowed. Please remove it first
…i-2 I did not close the pane in the mismatch case, which is not what you literally asked for. The reason is below. **ci-3 (generated files committed).** I ran `git rm -r .tmp-ci`, which removed all 27 files from git and from disk. `.gitignore` did not already cover the folder, so I added `.tmp-ci/`. I made no other change for this finding. **ci-2 (flat pane survives a refusal).** The rule is: if spawn refuses to publish a record, it closes the pane it created, but never a pane that now holds another terminal. In `bin/fm-spawn.sh` the refusal now sets the existing abort-handler variables (`HERDR_PROJECTION_ABORT_*`) before it exits. The existing guarded close (`fm_backend_herdr_projection_cleanup_exact`) then does the work, so there is no new mechanism. This applies only when this spawn created the pane. Relaunch keeps a pane it adopted, because that path never sets the identity pin. There are two cases: - **The read-back fails.** The pane is still the one this spawn created, so it is closed, in both the flat and the projected (presentation) layouts. - **The read-back returns a different terminal ID.** I do not close the pane here. A different ID means Herdr gave the pane ID to someone else's terminal, and our own pane is already gone. The pin would pass the close check, so the close would kill the other terminal - the exact wrong-pane close this PR exists to prevent. In the projected layout, only the task pane is dropped from cleanup; the extra pane made with the new workspace is still closed. **Test.** I added the assertion to `test_herdr_rebind_refuses_a_terminal_identity_the_create_did_not_return`: the log must contain no `pane close`, so the live pane with the other terminal is not closed. This test would not catch a regression: in this fake, the close cannot take the presentation lock, so no close would run even if cleanup were wrongly set up. **Verification.** - `shellcheck -x` is clean on both changed files. - `fm-spawn-compact-adviser-disable-remote` and `fm-spawn-orca-worktree` pass. These are the `fm-spawn*` test files that touch Herdr. - I ran the mismatch test alone, without the rest of `fm-control-relaunch.test.sh`. It passes. - In a scratch variant (not committed) where the read-back has no terminal ID, the refusal sets up cleanup and the abort handler reaches the guarded close. The fake then gives the warning "herdr presentation focus lock unavailable" and the close does not run. **The actual pane close on refusal has not run in any test**, with the fake or with real Herdr
…nstructed. **The rule:** spawn only arms the abort-path close when the pane is pinned (this spawn created it) and its terminal identity was actually read. If the identity cannot be read, spawn never gets permission to close by pane ID alone. **Change in `bin/fm-spawn.sh`** (the refusal branch before the record is published): - **Identity read, but different from the create response:** the abort cleanup is armed (`HERDR_PROJECTION_ABORT_*`). The existing guarded close then closes the pane, flat or projected. - **Identity unreadable (read failed, or no ID returned):** the task-pane cleanup is cleared, so a projected layout does not close it either. The pane is left open, and one stderr line names it: `error: herdr pane <session>:<pane> was left open; close it by hand`. In the projected layout, the extra pane created with the new workspace is still cleaned up as before. - A pane that relaunch adopted (not pinned) is never closed, as before. **Tests in `tests/fm-control-relaunch.test.sh`:** - I extended the fake Herdr so that the guarded close can actually run in a test. It now answers `session list` with a socket so the lock works, has a focused captain workspace and tab, answers `terminal title clear` with `no_foreground_client`, and handles `pane close`. Two flag files switch these on only for the new cases. - `test_herdr_rebind_refuses_a_terminal_identity_the_create_did_not_return` now asserts that `pane close %9` runs. - New test `test_herdr_rebind_leaves_a_pane_with_an_unreadable_identity_open`: the pane read has no `terminal_id`. The test asserts that spawn refuses, runs no `pane close`, and prints the "was left open" line naming `fmlab:%9`. - Against the previous `fm-spawn.sh`, both tests fail: the first because no `pane close` runs, the second because an unexpected `pane close` runs. With the fix, both pass. - The unreadable case in the test uses a read that returns no ID, not a failed CLI call. The code treats both the same way, but the failed-call form is not tested directly. **Verification:** - I ran all nine Herdr tests in `fm-control-relaunch.test.sh`, including the two above, in a scratch runner. All pass. - `fm-spawn-compact-adviser-disable-remote` and `fm-spawn-orca-worktree` pass. - `shellcheck -x` is clean on both changed files. **Two things to note:** 1. **Your decision reverses round 4.** In the mismatch case, the pane that is closed reads back a different terminal (`term-survivor`) from the one Herdr returned at create. Round 4 left that pane open because it may belong to someone else. It is now closed, as you decided. 2. **Two scratch files must be removed before commit:** `bin/.fm-spawn-old.sh` and `tests/.scratch-one.test.sh`. They are untracked. I tried to delete them, but the deletes were denied
bin/.fm-spawn-old.sh and tests/.scratch-one.test.sh were working copies left by the pipeline's fix agent and are not part of this change.
…euse-misbinds-task # Conflicts: # bin/backends/herdr.sh # bin/fm-spawn.sh
|
Merged current main into this branch and resolved the conflicts in |
|
Speaking as Kun's firstmate: thank you @AgardnerAU — binding Herdr endpoints to create-time VISION (each rule):
Contract-class: restore. Herdr already promised task↔endpoint binding; reused pane ids broke finished records onto strangers. This restores that binding via create-time terminal identity (legacy path still covered). Live go 5/7 (two scenarios fake-Herdr only; attestation verdict go). Where it stands: attestation MATCH |
…e PR's new test tests/fm-backend-herdr-pane-reuse-e2e.test.sh called `herdr pane report-agent --source ... --agent ... --state idle "$PANE"`, with the pane id after the options. Herdr 0.7.4, which CI uses, needs the pane id positional first, so it failed with "unknown option: fm-pane-reuse-e2e" / "not ok - could not register the unrelated agent". Invariant: every report-agent call in the suite passes the pane id before the options. All the other report-agent callers (smoke, control, eventwait, respawn, presentation, afk-inject) already do this; this was the only call that did not. Fix: one-line reorder to `lab pane report-agent "$PANE" --source fm-pane-reuse-e2e --agent claude --state idle`. Verified locally: the test passes end to end against real Herdr 0.9.1 (all 5 assertions ok). Herdr 0.7.4 is not available locally, so the CI-version run is not verified. shellcheck shows only the existing SC1091 source-following info notices. ci-1 (Behavior portable parallel 1): not caused by this PR. The only failing script is tests/fm-pi-primary-types.test.sh, with TS2339 "Property 'prompt' does not exist on type 'BeforeAgentStartEvent'" in .pi/extensions/fm-branch-supervision.ts and fm-primary-pi-watch.ts. This PR does not touch those files. Upstream main CI run 38316792015, at this PR's base commit 6407052, fails with the identical errors (plus a Pi codemode failure), so this is drift in the globally installed Pi package. No code change is made for it
# Conflicts: # bin/fm-spawn.sh
|
Speaking as Kun's firstmate: thanks @AgardnerAU. Main has moved: #7172 landed and touched Contract-class: I'm revising my earlier read from restore to new-default. Binding new records to
Both are reasonable choices, but they're Kun's to make. So once this is rebased and green on a green main, it goes to Kun to merge or leave instead of auto-merging. You don't need to change anything for this. VISION.md: one captain / authority / scripts / restart / spine / vendor / scope all align. It strengthens a refusal path, a mismatch now refuses to close, and it stays inside the named Herdr adapter. Security: this is a tightening. Overlaps: #6760 (stale Herdr endpoints after session resets) has a closely related goal and touches the same |
…euse-misbinds-task # Conflicts: # bin/fm-backend.sh # bin/fm-spawn.sh # tests/fm-watch-triage.test.sh
|
Merged current upstream main into this branch and re-raised it through no-mistakes (head 64c0883). It is up to date with main and ready for CI; the workflow runs for this head are waiting for maintainer approval. |
…euse-misbinds-task # Conflicts: # bin/fm-backend.sh # bin/fm-classify-lib.sh # bin/fm-secondmate-liveness-lib.sh # bin/fm-spawn.sh # bin/fm-teardown.sh # docs/herdr-backend.md # tests/fm-teardown-endpoint-safety.test.sh
|
Speaking as Kun's firstmate: thanks @AgardnerAU for keeping this current, and sorry your 10-18 re-raise sat without a reply. What changed since my 10-13 note: two merges of main ( Gates:
Contract-class (my own read, unchanged from 10-13): new-default. New records binding to the pane's terminal identity restores an existing promise. But two default paths change, and your docs state both plainly: a legacy record without This isn't waiting on you. It's waiting on CI. Once this head is green, it goes to Kun. |
…/fm-teardown.sh:1323. The teardown script sets FM_BACKEND_HERDR_IDENTITY_PIN, but only the Herdr adapter it sources (bin/backends/herdr.sh) reads it. Rule that must hold: lint passes for an output global that a sourced library reads. The only other place that sets this variable is bin/fm-spawn.sh, which also reads it later (line ~6612), so it gets no warning. Fix: I expanded the `match)` case arm and added `# shellcheck disable=SC2034 # Read by the sourced Herdr adapter.`, the same style as bin/fm-backlog-transition-lib.sh. Behaviour does not change. Verification with ShellCheck 0.11.0 (the CI version): `shellcheck -x bin/fm-teardown.sh` exits 0, a run over every shell script this branch changes gives no warnings, and `bash -n` passes. Behavior portable serial 3/4/5/6/7/9 (not caused by this PR): the same test scripts also fail in main's own CI run at base commit 127f0f3: fm-public-followup (serial 3), fm-contributions, fm-fleet-ledger and fm-herdr-groups (serial 4), fm-record-export (serial 5), fm-remote-secondmate-lifecycle-e2e (serial 6), fm-backend-rex and fm-pr-health (serial 7), and fm-watch-checkpoint (serial 9). The failures are timing checks that break under runner load, for example "watcher checkpoint did not run" and "expected exit 124, got 137". Following the user's instruction to run only targeted tests, I ran the five fast-failing scripts locally (fm-fleet-ledger, fm-herdr-groups, fm-record-export, fm-watch-checkpoint, fm-backend-rex) and all pass. I did not run the slower scripts locally. I made no code change for these checks
…euse-misbinds-task
# Conflicts: # tests/fm-watch-triage.test.sh
| match) | ||
| # shellcheck disable=SC2034 # Read by the sourced Herdr adapter. | ||
| FM_BACKEND_HERDR_IDENTITY_PIN=$T |
There was a problem hiding this comment.
Cleanup closes a replacement pane
Teardown sets FM_BACKEND_HERDR_IDENTITY_PIN after its first successful check. Every later identity check then returns match without reading the terminal. If Herdr restarts while teardown returns the worktree, the same pane id can name a fresh terminal when cleanup closes it. Cleanup skips the recorded terminal check and can close that replacement pane.
Save the verified terminal id, not just the pane id, and compare it again before closing. This also avoids relying on the legacy working-directory check after the worktree moves.
…fm/fm-herdr-pane-id-reuse-misbinds-task # Conflicts: # bin/fm-procevent.sh # bin/fm-spawn.sh # bin/fm-teardown.sh
| if [ -n "$HERDR_READ_TERMINAL_ID" ]; then | ||
| HERDR_PROJECTION_ABORT_CLEANUP=1 | ||
| HERDR_PROJECTION_ABORT_SESSION=$HERDR_SES | ||
| HERDR_PROJECTION_ABORT_TASK_PANE=$HERDR_PANE_ID |
There was a problem hiding this comment.
Failed spawn closes replacement terminal
If Herdr reissues the pane ID before the publication read, fm-spawn.sh detects the different terminal ID and refuses the record. But this branch still arms cleanup for that pane. FM_BACKEND_HERDR_IDENTITY_PIN makes the cleanup check return match without rereading the terminal, so the abort handler can close another task's terminal.
On an identity mismatch, leave the pane untouched rather than arming cleanup.
|
Speaking as Kun's firstmate: thanks @AgardnerAU for keeping #5435 current — tip moved to Waiting on you for two things on this tip:
Unlabelled Full failure is expected. Rockets: none. Overlaps still noted with #6229/#6249/#6760. |
Intent
Update the existing pull request #5435 (branch fm/fm-herdr-pane-id-reuse-misbinds-task, from the AgardnerAU fork) onto current upstream main and push so CI re-runs. The earlier CI failures came from GitHub moving ubuntu-latest to ubuntu-26.04, fixed on main by upstream PR 7798 (an ubuntu-24.04 pin), and were not caused by this change. That PR binds Herdr task records to the pane's terminal identity so a reissued pane id is not misbound to another task. Keep updating the existing PR; do not replace it. If only the known flaky cases fail on main itself (fm-supervision-host serial 1, fm-watch-wedge-cap serial 4), report that rather than chasing them.
What Changed
herdr_terminal_id=(from the pane-create response, or from the record of an adopted endpoint). Before it publishes the record, spawn reads the pane back. If that read fails or returns a different id, spawn refuses to publish and exits nonzero. It closes a pane it created only when it could read the pane's identity.fm_backend_herdr_endpoint_identityinbin/backends/herdr.shruns before every Herdr liveness read, capture, input, control action and close. Callers that act for one task now bind that task's own record first, through the newfm_backend_bind_task_recordinbin/fm-backend.sh. These callers include the watcher, session-start, teardown, send, peek, crew-state and fleet snapshot.missing). Nothing is closed and the landed-work checks still run.window=was cleared. It makes no Herdr call for that record and still runs its landed-work checks.fm-watch.shnow walks task records rather than panes, so a live task keeps its stall detection when an old record names the same reissued pane id.docs/herdr-backend.mdanddocs/verification/runtime-backends.mddescribe the pane-id reissue behaviour, measured on Herdr 0.9.1.tests/fm-backend-herdr-pane-reuse-e2e.test.sh, and wider unit and e2e tests cover these paths.🤖 Generated with Claude Code
Risk Assessment
✅ Low: The new merge of upstream main (7da4c36) applied cleanly with no conflict resolution (empty remerge-diff). Main's overlapping changes in fm-spawn.sh (the Rex local-server ensure on new spawns), fm-test-run.sh (family and duration entries), configuration.md (the warm-compact section) and fm-watch-triage.test.sh (the FM_TEST_STOCK_BASH gate) are independent of the PR's Herdr terminal-identity binding. Main's new warm-compact path targets an unrecorded primary pane, which the identity check treats as unbound and leaves unchanged, so the earlier-reviewed PR behaviour is preserved.
Testing
I ran the branch's dedicated real-Herdr e2e at the merged commit in a throwaway fm-lab session, and all 5 assertions passed. The test first reproduces the bug: after a restart, Herdr gives pane w2:p2 to an unrelated terminal. It then shows that liveness, existence, capture, steering, interrupt and cleanup all leave that pane untouched. Bound and legacy records that own the pane still read their agent as alive. The targeted Herdr backend and teardown scripts passed with no failures. The control-relaunch script was stopped before it finished, and the watch-triage script never started, because the batch reached its time limit on a loaded host. Remote CI still runs both and covers broad regression.
Evidence: Real-Herdr pane reuse e2e transcript
Source: Real-Herdr pane reuse e2e transcript
ok - a task record matches its own live pane, bound or legacy ok - repro: after a restart Herdr reissued pane w2:p2 to an unrelated terminal ok - repro: read by pane id alone, the finished task record names a live agent ok - liveness, existence, capture, steering, interrupt, and cleanup all treat the reissued pane as the finished task endpoint gone, and leave it untouched ok - the record that owns the pane, bound or legacy, still reads its agent aliveEvidence: fm-backend-herdr test log
Source: fm-backend-herdr test log
Evidence: fm-teardown test log
Source: fm-teardown test log
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.
bash tests/fm-backend-herdr-pane-reuse-e2e.test.shagainst real herdr 0.9.1, via bin/fm-herdr-lab.sh with a disposable fm-lab session that the test tears downbash tests/fm-backend-herdr.test.sh(239 ok, 0 not ok)bash tests/fm-teardown.test.sh(44 ok, 0 not ok)bash tests/fm-control-relaunch.test.shstarted but did not finish before the background time limit;tests/fm-watch-triage.test.shdid not start✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.