Skip to content

fix(bin): retire task-keyed watcher markers and orphan journals at teardown - #5997

Merged
kunchenguid merged 2 commits into
kunchenguid:mainfrom
karotkriss:fm/fm-up-5252-teardown-v2
Sep 28, 2026
Merged

kunchenguid merged 2 commits into
kunchenguid:mainfrom
karotkriss:fm/fm-up-5252-teardown-v2

Conversation

@karotkriss

@karotkriss karotkriss commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Intent

Fixes #5252

After a task is torn down, some per-task state files are never removed: the turn-ended "seen" marker, a Herdr presentation journal whose workspace is already gone, and wake-drain scratch files, so a long-running home accumulates dead files that slow session start and wake drains.
Teardown should retire those task-keyed markers and orphan journals, and the drain should rotate its own scratch files.
Status and heartbeat markers are already retired on main and data// is intentionally kept (reports and briefs live there), so this completes the remaining part of upstream issue #5252.
It is complementary to open PR #5951, which closes an empty projected tab that still exists at session start; this change retires a journal whose workspace or pane is already gone at teardown.
This replaces the earlier pull request #5584, re-applied fresh on current main.

What Changed

  • Teardown now removes the watcher's per-task .seen-* signature for the task's turn-ended file across all three cleanup paths (remote_secondmate_teardown, cleanup_firstmate_home_children, and the main teardown), alongside the existing turn-ended/progress removal.
  • After the Herdr gate proves the recorded pane gone, teardown deletes an orphaned presentation journal via a new teardown_herdr_journal_orphaned check (backed by fm_backend_herdr_projection_token_workspace_gone in herdr.sh): a version 2 binding of the exact closed pane, or a version 1 attempt whose token-bearing projected workspace is confirmed gone; otherwise-bound or still-present/unreadable journals are retained for the session-start sweep with a warning.
  • fm-wake-drain.sh gains rotate_scratch_locked, run right after acquiring the queue lock, to clear scratch files (.main-eligible-rows.tmp.*, .wake-rows.consume.*, .wake-queue.retire.*, .wake-queue.ack.*, .wake-queue.actor-view.*) left by a drain that died mid-write; docs and teardown tests updated to match.

Risk Assessment

✅ Low: A tightly scoped, fail-closed cleanup change: each removed file is matched against the exact marker naming the writer uses, orphan-journal retirement is gated behind confirmed-gone pane proof with retain-on-uncertainty defaults, scratch rotation touches only drain-owned lock-scoped temps, and behavior tests cover the new v1/v2 and rotation paths.

Testing

Ran the two affected behavior suites end-to-end. tests/fm-teardown.test.sh passed 101/101 (exit 0) and tests/fm-wake-queue.test.sh passed (exit 0). Each new scenario invokes the real scripts against a seeded state directory and asserts observable results: the torn-down task's .seen-<id>_turn-ended / .seen-<id>_status / .hb-surfaced-<id> markers and orphaned journal are gone while a sibling task's markers survive; a journal bound to a pane the close never proved gone is retained with a stated warning; a v1 attempt journal is retired only once the session workspace list confirms its token workspace is gone and retained (with warning, no workspace-close escalation) while that workspace is still present; and a locked drain rotates away .main-eligible-rows.tmp.* / .wake-rows.consume.* / .wake-queue.{retire,ack,actor-view}.* leftovers while preserving the live main rows claim. These are the adversarial guards named in the intent (don't over-delete a live-workspace journal, don't touch other tasks' state). No UI surface exists in this shell-CLI change, so no visual artifacts apply. Worktree left clean.

  • Live validation: ✅ go - 5 of 5 scenarios driven live against the product
Scenario Result Live Evidence
Teardown removes the torn-down task's turn-ended seen marker (plus status/heartbeat markers) and its orphaned presentation journal, leaving another task's markers and a live-pane journal untouched ✅ pass live test_teardown_retires_task_watcher_markers_and_orphan_journal in bash bin/fm-test-run.sh tests/fm-teardown.test.sh - asserts .seen-task-x1_turn-ended/.seen-task-x1_status/.hb-surfaced-task-x1 and ta…
Adversarial: teardown retains a presentation journal bound to a pane it never proved gone, with a warning explaining why ✅ pass live test_teardown_retains_journal_bound_to_another_pane - asserts task-x1.herdr-presentation still present, teardown completed, and 'retaining herdr presentation journal' warning on stderr
Teardown retires a v1 attempt journal once the session workspace list confirms its token-bearing workspace is gone, without calling workspace close ✅ pass live test_teardown_retires_v1_journal_when_projected_workspace_gone - journal absent, teardown complete, log contains no 'workspace close'
Adversarial: teardown retains a v1 journal while its token workspace is still present (never strands a live workspace) and does not escalate to workspace cleanup ✅ pass live test_teardown_retains_v1_journal_when_projected_workspace_present - journal present, warning emitted, no 'workspace close' in log
A locked wake-drain rotates away scratch files a drain that died mid-write left under the queue lock, preserving the live main rows claim ✅ pass live test_drain_rotates_orphaned_scratch in bash bin/fm-test-run.sh tests/fm-wake-queue.test.sh - all five orphaned scratch files removed, .main-eligible-rows preserved, drain exit 0
Evidence: Behavior-scenario pass log and suite summaries

Source: Behavior-scenario pass log and suite summaries

PASS: teardown retires the task's own watcher markers and orphaned presentation journal, leaving other tasks' markers alone
PASS: teardown retains a presentation journal bound to a pane other than the closed endpoint
PASS: teardown retires a v1 presentation journal once its token workspace is confirmed gone
PASS: teardown retains a v1 presentation journal while its token workspace is still present
PASS: drain rotates scratch files an interrupted drain left under the queue lock

=== Suite results (real fm-teardown.sh / fm-wake-drain.sh driven against seeded state dirs) ===
FM_TEST_END 2026-09-28T09:11:10Z tests/fm-teardown.test.sh exit=0 duration_ms=148478 gate_skip=false
FM_TEST_SUMMARY total=1 failed=0 skipped_gate=0 duration_ms=148540
tests/fm-wake-queue.test.sh exit=0 (drain scratch rotation among 1 suite)

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.

  • Live validation: ✅ go - 5 of 5 scenarios driven live against the product
Scenario Result Live Evidence
Teardown removes the torn-down task's turn-ended seen marker (plus status/heartbeat markers) and its orphaned presentation journal, leaving another task's markers and a live-pane journal untouched ✅ pass live test_teardown_retires_task_watcher_markers_and_orphan_journal in bash bin/fm-test-run.sh tests/fm-teardown.test.sh - asserts .seen-task-x1_turn-ended/.seen-task-x1_status/.hb-surfaced-task-x1 and ta…
Adversarial: teardown retains a presentation journal bound to a pane it never proved gone, with a warning explaining why ✅ pass live test_teardown_retains_journal_bound_to_another_pane - asserts task-x1.herdr-presentation still present, teardown completed, and 'retaining herdr presentation journal' warning on stderr
Teardown retires a v1 attempt journal once the session workspace list confirms its token-bearing workspace is gone, without calling workspace close ✅ pass live test_teardown_retires_v1_journal_when_projected_workspace_gone - journal absent, teardown complete, log contains no 'workspace close'
Adversarial: teardown retains a v1 journal while its token workspace is still present (never strands a live workspace) and does not escalate to workspace cleanup ✅ pass live test_teardown_retains_v1_journal_when_projected_workspace_present - journal present, warning emitted, no 'workspace close' in log
A locked wake-drain rotates away scratch files a drain that died mid-write left under the queue lock, preserving the live main rows claim ✅ pass live test_drain_rotates_orphaned_scratch in bash bin/fm-test-run.sh tests/fm-wake-queue.test.sh - all five orphaned scratch files removed, .main-eligible-rows preserved, drain exit 0
  • bash bin/fm-test-run.sh tests/fm-teardown.test.sh (101 ok / 0 fail, exit 0) - drives real fm-teardown.sh against seeded state dirs
  • bash bin/fm-test-run.sh tests/fm-wake-queue.test.sh (exit 0) - includes test_drain_rotates_orphaned_scratch driving real fm-wake-drain.sh
  • New scenarios: test_teardown_retires_task_watcher_markers_and_orphan_journal, test_teardown_retains_journal_bound_to_another_pane, test_teardown_retires_v1_journal_when_projected_workspace_gone, test_teardown_retains_v1_journal_when_projected_workspace_present
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

Re-applies old PR kunchenguid#5584 on current main: teardown retires the
turn-ended .seen-* signature and an orphaned Herdr presentation
journal whose workspace is already gone, and the wake-drain rotates
its own dead scratch files. Not yet validated through no-mistakes.
@greptile-apps

greptile-apps Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Medium risk] Cleans up watcher markers and orphaned journals at task teardown.

The latest revision introduces no new blocking issue, but the previously reported sibling-marker collision remains unresolved.

Reviews (2) · Last reviewed commit: "no-mistakes(ci): Fixed the Greptile P1 f..."

Comment thread bin/backends/herdr.sh Resolved
Comment thread bin/fm-teardown.sh
status_retire_presentation_task "$STATE" "$ID" || exit 1
fm_wake_queue_prune_task "$STATE" "$ID" "$T" 2>/dev/null || true
rm -f "$STATE/$ID.turn-ended" "$STATE/$ID.progress" \
"$(fm_wake_signal_seen_path "$STATE" "$STATE/$ID.turn-ended")" \

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Sibling marker can be deleted Valid task IDs such as a.b and a_b share the same .seen-a_b_turn-ended marker because marker naming replaces dots with underscores. This new removal, also added to the remote and home-child cleanup paths, can therefore delete a sibling task’s marker. If that sibling’s turn-ended file remains, the watcher may treat its already-seen signal as new and queue a duplicate wake.

…sh. fm_backend_herdr_projection_token_workspace_gone used `! ... jq -e ... 2>&1`, which swallowed a jq runtime error (thrown when a non-object workspace entry, e.g. a number before a live token-bearing workspace, hits `.label`) and flipped it to a "gone" verdict, causing teardown to delete a still-live v1 presentation journal. Invariant: a workspace-query error/ambiguity must never be read as token absence; only a cleanly-parsed list with no token-bearing label is "gone". Replaced the body with a single jq verdict (unknown/present/gone): a non-array list or any non-object/non-string-label entry yields "unknown", jq errors/empty output fall through `|| return 1` to unknown, and only "gone" returns 0. Sibling fm_backend_herdr_projection_endpoint_matches_journal already fails safe on jq error (empty match -> journal kept), so it needed no change, matching the author's scoping. Added test_teardown_retains_v1_journal_when_workspace_query_ambiguous driving real teardown with a malformed workspace-list entry, proving the journal is kept and no workspace close occurs. Verified the old logic returns GONE on that input (test fails before, passes after); full tests/fm-teardown.test.sh suite passes (exit 0) and shellcheck is clean. Marker-naming finding left untouched per explicit out-of-scope instruction

@kunchenguid kunchenguid left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Speaking as Kun's firstmate: tip reviewed vs main — restore teardown of task-keyed watcher markers/orphan journals; green CI+NM; approving for squash-merge.

@kunchenguid
kunchenguid merged commit 6b0f5a0 into kunchenguid:main Sep 28, 2026
20 checks passed

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: this is merged. Thank you @karotkriss — really appreciate you taking the time on this.

HEAD c3bac7c2e65d7f1b3c25b9df7807dfb73df3e928 squash-merged as 6b0f5a07fee0ac336332ccab2caa0bd07a5cf113. Tip CI 36407205048 SUCCESS; NM 36407205090 SUCCESS. Attestation MATCH. Author karotkriss not blocked.

Closes #5252: body Fixes #5252 — accurate (task-keyed .seen-* / orphan Herdr journals / wake-drain scratch never retired after teardown).

Contract-class: restore (own tip-vs-main; FM-LEARN-CLAIMS). Main already retires status/heartbeat presentation markers; tip completes the same concrete teardown path for turn-ended .seen-*, confirmed-gone orphan journals (fail-closed retain), and drain-owned scratch rotation. No new default-on observer/wake/Bearings/bootstrap surface.

VISION.md (each rule)

  1. One captain, one interface — aligns (less residue slowing session start/drain).
  2. Authority explicit — aligns (no consent widening).
  3. Scripts own mechanics — aligns (deterministic marker/journal retirement).
  4. Restart non-event — aligns (durable cleanup; retain-on-uncertainty).
  5. Delegation with a spine — aligns (no task-contract change).
  6. Fleet outlives vendor — aligns (Herdr adapter quarantine rules preserved).
  7. Scope — aligns (command-layer teardown hygiene).
    Align: peace of mind / introspectable state. Resist: none material.

Firstmate flag: no. Security tip: none.

knowttl pushed a commit to knowttl/firstmate that referenced this pull request Sep 29, 2026
…ardown (kunchenguid#5997)

* WIP: retire task-keyed watcher markers and orphan journals at teardown

Re-applies old PR kunchenguid#5584 on current main: teardown retires the
turn-ended .seen-* signature and an orphaned Herdr presentation
journal whose workspace is already gone, and the wake-drain rotates
its own dead scratch files. Not yet validated through no-mistakes.

* no-mistakes(ci): Fixed the Greptile P1 finding in bin/backends/herdr.sh. fm_backend_herdr_projection_token_workspace_gone used `! ... jq -e ... 2>&1`, which swallowed a jq runtime error (thrown when a non-object workspace entry, e.g. a number before a live token-bearing workspace, hits `.label`) and flipped it to a "gone" verdict, causing teardown to delete a still-live v1 presentation journal. Invariant: a workspace-query error/ambiguity must never be read as token absence; only a cleanly-parsed list with no token-bearing label is "gone". Replaced the body with a single jq verdict (unknown/present/gone): a non-array list or any non-object/non-string-label entry yields "unknown", jq errors/empty output fall through `|| return 1` to unknown, and only "gone" returns 0. Sibling fm_backend_herdr_projection_endpoint_matches_journal already fails safe on jq error (empty match -> journal kept), so it needed no change, matching the author's scoping. Added test_teardown_retains_v1_journal_when_workspace_query_ambiguous driving real teardown with a malformed workspace-list entry, proving the journal is kept and no workspace close occurs. Verified the old logic returns GONE on that input (test fails before, passes after); full tests/fm-teardown.test.sh suite passes (exit 0) and shellcheck is clean. Marker-naming finding left untouched per explicit out-of-scope instruction
RooseveltAdvisors pushed a commit to RooseveltAdvisors/firstmate that referenced this pull request Sep 29, 2026
…ardown (kunchenguid#5997)

* WIP: retire task-keyed watcher markers and orphan journals at teardown

Re-applies old PR kunchenguid#5584 on current main: teardown retires the
turn-ended .seen-* signature and an orphaned Herdr presentation
journal whose workspace is already gone, and the wake-drain rotates
its own dead scratch files. Not yet validated through no-mistakes.

* no-mistakes(ci): Fixed the Greptile P1 finding in bin/backends/herdr.sh. fm_backend_herdr_projection_token_workspace_gone used `! ... jq -e ... 2>&1`, which swallowed a jq runtime error (thrown when a non-object workspace entry, e.g. a number before a live token-bearing workspace, hits `.label`) and flipped it to a "gone" verdict, causing teardown to delete a still-live v1 presentation journal. Invariant: a workspace-query error/ambiguity must never be read as token absence; only a cleanly-parsed list with no token-bearing label is "gone". Replaced the body with a single jq verdict (unknown/present/gone): a non-array list or any non-object/non-string-label entry yields "unknown", jq errors/empty output fall through `|| return 1` to unknown, and only "gone" returns 0. Sibling fm_backend_herdr_projection_endpoint_matches_journal already fails safe on jq error (empty match -> journal kept), so it needed no change, matching the author's scoping. Added test_teardown_retains_v1_journal_when_workspace_query_ambiguous driving real teardown with a malformed workspace-list entry, proving the journal is kept and no workspace close occurs. Verified the old logic returns GONE on that input (test fails before, passes after); full tests/fm-teardown.test.sh suite passes (exit 0) and shellcheck is clean. Marker-naming finding left untouched per explicit out-of-scope instruction
@keenvc keenvc mentioned this pull request Sep 30, 2026
andrewesweet pushed a commit to andrewesweet/firstmate that referenced this pull request Sep 30, 2026
…ardown (kunchenguid#5997)

* WIP: retire task-keyed watcher markers and orphan journals at teardown

Re-applies old PR kunchenguid#5584 on current main: teardown retires the
turn-ended .seen-* signature and an orphaned Herdr presentation
journal whose workspace is already gone, and the wake-drain rotates
its own dead scratch files. Not yet validated through no-mistakes.

* no-mistakes(ci): Fixed the Greptile P1 finding in bin/backends/herdr.sh. fm_backend_herdr_projection_token_workspace_gone used `! ... jq -e ... 2>&1`, which swallowed a jq runtime error (thrown when a non-object workspace entry, e.g. a number before a live token-bearing workspace, hits `.label`) and flipped it to a "gone" verdict, causing teardown to delete a still-live v1 presentation journal. Invariant: a workspace-query error/ambiguity must never be read as token absence; only a cleanly-parsed list with no token-bearing label is "gone". Replaced the body with a single jq verdict (unknown/present/gone): a non-array list or any non-object/non-string-label entry yields "unknown", jq errors/empty output fall through `|| return 1` to unknown, and only "gone" returns 0. Sibling fm_backend_herdr_projection_endpoint_matches_journal already fails safe on jq error (empty match -> journal kept), so it needed no change, matching the author's scoping. Added test_teardown_retains_v1_journal_when_workspace_query_ambiguous driving real teardown with a malformed workspace-list entry, proving the journal is kept and no workspace close occurs. Verified the old logic returns GONE on that input (test fails before, passes after); full tests/fm-teardown.test.sh suite passes (exit 0) and shellcheck is clean. Marker-naming finding left untouched per explicit out-of-scope instruction
ironerumi added a commit to ironerumi/firstmate that referenced this pull request Oct 2, 2026