Skip to content
This repository was archived by the owner on Oct 24, 2026. It is now read-only.

feat(bin): add usage-tripwire watcher check - #3

Merged
mielyemitchell merged 6 commits into
mainfrom
fm/usage-tripwire-u2
Jul 7, 2026
Merged

mielyemitchell merged 6 commits into
mainfrom
fm/usage-tripwire-u2

Conversation

@mielyemitchell

@mielyemitchell mielyemitchell commented Jul 6, 2026 •

Copy link
Copy Markdown
Owner

Intent

Build a fast read-only token-usage tripwire for the firstmate fleet that can be armed as a watcher state/*.check.sh poll. It should scan only a recent sliding mtime window by default, not full transcript history, and alarm within the hour on two incident-backed signals: recent transcript/session-spawn count, including zero-usage killed sessions, and summed output tokens across Claude ~/.claude/projects jsonl usage plus Codex ~/.codex/sessions token_count records. The watcher contract is strict: print exactly one line only when firstmate should wake, print nothing when healthy, finish quickly before FM_CHECK_TIMEOUT, stay dependency-light with bash plus jq, degrade gracefully when transcript dirs are absent, and document thresholds plus the arming command in the script header and --help. Tests must be hermetic fixtures, cover healthy silence plus session-count and token-sum breaches, avoid real home dirs, shellcheck must pass including newer CI shellcheck, and the branch must rebase cleanly onto current origin/main after PR 2 squash-merged.

What Changed

  • Added bin/fm-usage-tripwire.sh, a read-only watcher check that scans a recent sliding mtime window (not full history) across Claude ~/.claude/projects and Codex ~/.codex/sessions transcripts, and alarms on two incident-backed signals: recent session-spawn count (including zero-usage killed sessions) and summed output tokens, printing exactly one line only when firstmate should wake, per the strict watcher-check contract.
  • Made bin/fm-guard.sh and bin/fm-supervise-daemon.sh freeze-aware so guard warnings and daemon injection correctly respect an active fleet freeze, and fixed the tripwire's signal trap to actually exit on TERM/INT/HUP instead of swallowing the signal and continuing.
  • Documented the tripwire, fleet-freeze, and fleet-map incident guardrails across AGENTS.md, README.md, docs/architecture.md, docs/configuration.md, and docs/scripts.md, and added tests/fm-usage-tripwire.test.sh covering healthy silence, session-count/output-token breaches, mtime bounding, and graceful degradation when transcript dirs are missing; also cleaned up duplicate documentation left by earlier related commits.

Risk Assessment

✅ Low: This round is a single 4-line diff that correctly fixes the previously-flagged signal-trap bug (separate HUP/INT/TERM traps now call exit with proper codes instead of a combined trap that silently continued execution); no new issues introduced and the change matches the exit-in-trap pattern already used elsewhere in the repo.

Testing

Ran the usage-tripwire unit tests plus the related freeze and daemon suites touched by this branch's review-fix commits, all passing, and manually reproduced the final commit's signal-trap fix in isolation to directly confirm SIGTERM/INT/HUP now terminate the script with the correct exit code and still clean up its temp file — no failures or missing coverage found.

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

🔧 **Rebase** - 3 issues found → auto-fixed ✅
  • ⚠️ AGENTS.md - merge conflict rebasing onto origin/main
  • ⚠️ docs/configuration.md - merge conflict rebasing onto origin/main
  • ⚠️ docs/scripts.md - merge conflict rebasing onto origin/main

🔧 Fix applied.
✅ Re-checked - no issues remain.

🔧 **Review** - 1 issue found → auto-fixed ✅
  • ⚠️ bin/fm-usage-tripwire.sh:110 - trap cleanup EXIT HUP INT TERM runs cleanup on TERM/INT/HUP but never exits, so the script does not actually terminate on those signals -- it just removes the tmp file and keeps running. Verified empirically: sending SIGTERM to a script with this exact trap shape lets execution continue past the point of interruption. This script is invoked by fm-watch.sh as timeout "$CHECK_TIMEOUT" bash "$c" (bin/fm-watch.sh:328), and timeout sends SIGTERM on expiry; because the trap swallows it without exiting, a check that runs long (e.g. slow find/jq over a large or network-mounted transcript dir) will not actually be killed at FM_CHECK_TIMEOUT as the script's own header promises ('finish quickly before FM_CHECK_TIMEOUT'), defeating the timeout and potentially stalling the watcher's check sweep. Other scripts in this repo (bin/fm-wake-drain.sh:40-41, bin/fm-watch-arm.sh:142-143, bin/fm-x-lib.sh:282) explicitly exit inside their signal traps for exactly this reason.

🔧 Fix: Fix usage-tripwire signal trap to exit on TERM/INT/HUP
✅ Re-checked - no issues remain.

✅ **Test** - passed

✅ No issues found.

  • command -v tmux >/dev/null || { echo "tmux is required for e2e tests" >&2; exit 1; }; tmux -V; rc=0; for t in tests/*.test.sh; do echo "== $t =="; bash "$t" || rc=1; done; exit "$rc"
  • bash tests/fm-usage-tripwire.test.sh (7/7 pass: healthy silence, session-count breach, output-token breach, large-old-fixture mtime bounding, missing-dir degradation, long-lived-file stale-entry exclusion, long-lived-file recent-entry counting)
  • bash tests/fm-freeze.test.sh (4/4 pass: freeze toggle, spawn/send refusal while frozen, scoped bypass)
  • bash tests/fm-daemon.test.sh (57/57 pass, including the freeze-aware inject_msg refusal case added by this branch)
  • Manual reproduction of the exact trap lines added in commit 43a9220 (trap cleanup EXIT; trap 'exit 129/130/143' HUP/INT/TERM) — sent SIGTERM to a background process using this pattern and confirmed exit code 143, cleanup ran, and the temp file was removed
  • Full configured baseline test command across all tests/*.test.sh (already reported passing before this session)
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

…tripwire incident guardrails across AGENTS.md, README, docs, and CONTRIBUTING
…or fleet-freeze/fleet-map/usage-tripwire commits: merged two overlapping README bullets, two overlapping docs/architecture.md sections, and removed duplicate CONTRIBUTING.md test-list entries, keeping the usage-tripwire additions intact.
@mielyemitchell
mielyemitchell force-pushed the fm/usage-tripwire-u2 branch from a196c13 to 207d185 Compare July 7, 2026 00:35
@mielyemitchell mielyemitchell changed the title feat(supervision): usage tripwire + freeze/guard coherence fixes feat(bin): add usage-tripwire watcher check Jul 7, 2026
@mielyemitchell
mielyemitchell merged commit ecd88ce into main Jul 7, 2026
4 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant