Repository navigation
feat(bin): add opt-in Jev shadow classifier for supervision wakes - #40
Merged
Merged
Conversation
added 8 commits
September 26, 2026 12:20
Firstmate can now ask Jev (TypeSafe's decision model on OpenRouter, typesafe/jev-1.13) one three-way question per presented wake - needs firstmate, absorbable, or urgent for the captain - and log the answer next to what firstmate actually did. It acts on nothing: the drain hands rows to a detached process, so wakes are never delayed, dropped, reordered, absorbed or altered, and no merge, destructive action or captain call reads it. - Off by default; enabled only by OPENROUTER_API_KEY in the home's private .env (the ambient environment never enables it), mirroring Relay's gate. The key reaches curl on stdin, never argv, logs or disk. - Only the wake reason line and the worker's last status line leave the machine, with URLs and paths masked. - 2 s timeout and a daily spend cap (USD 1, config/jev-daily-cap); a timeout, API error, missing cost or the cap pauses Jev until the next day. - Ground truth is recorded automatically: steers (fm-send, fm-control), decisions (--resolve-key, fm-captain-hold), acknowledgements (wake drain) and how the primary turn ended (turn-end guard, outcome only, no text). - bin/fm-jev.sh report measures agreement, wakes Jev would have wrongly absorbed, the go-live criteria, and up to 20 doubtful cases.
… config-push sends
…ts/fm-on.test.sh. All of its assertions passed ("ALL TESTS PASSED"), but the script then exited 1 because its EXIT trap failed with `rm: cannot remove .../remote-jobs/worker.lock: Directory not empty`. This is a flaky cleanup race that already existed before this PR. The PR doesn't touch fm-on, the remote job worker or that test. The trap sent the detached remote job worker a signal and ran `rm -rf` on the fixture straight away, while the worker was still writing files into worker.lock as it shut down. The fix is a small `cleanup_fixture` function used by the trap: after sending the signal, it waits up to 5 s for the worker process to exit (polling with kill -0) and only then removes the fixture. That makes the cleanup deterministic. Checked locally: `shellcheck -x tests/fm-on.test.sh` is clean, and `bash tests/fm-on.test.sh` passed 3 runs out of 3 with exit 0. The failure itself doesn't reproduce reliably, since it only happens when the timing lines up
… It was already flaky before this PR and is what failed "Behavior portable serial 2". The failing assertion was "the worker kept serving after its state root was deleted". The PR doesn't touch that test, `bin/fm-remote-job-worker.sh` or `bin/fm-remote-job-lib.sh`. Its only remote-side change is adding `FM_JEV_OBSERVE=0` to two `fm-send.sh` calls in `fm-remote-secondmate-control.sh`, which never runs the job worker. Likely cause (not reproduced locally): the test ran `rm -rf` on the state root while the worker was still running. That worker writes a heartbeat every second (a temp file created and renamed inside the state root). If a write lands in the directory while `rm -rf` is emptying it, the rm fails with "Directory not empty" and the directory stays. Later heartbeat writes then succeed, so the worker never notices the deletion, and after about 10 s the test gave up and failed. The CI timestamps match: the failure came about 10.4 s after the previous test passed, which is the length of that wait. It's the same kind of race as the earlier fm-on cleanup flake. Fix: the test now uses `mv` to rename the state root to `$TMP_ROOT/tripwire-jobs.deleted` instead of deleting it in place. A rename removes the path in one step, so a heartbeat write can't bring it back. The worker's next heartbeat fails, and the test still checks that the path is absent. The renamed directory is removed with the rest of `$TMP_ROOT` by the existing EXIT trap. Nothing outside the test changed. Checked: `shellcheck -x tests/fm-remote-job.test.sh` is clean, and `bash tests/fm-remote-job.test.sh` passed 2 runs out of 2 ("ALL TESTS PASSED", exit 0)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Intent
Add Jev (TypeSafe's decision model on OpenRouter, model id typesafe/jev-1.13) to firstmate as an ADVISORY, SHADOW-ONLY classifier of supervision wakes. This is the first of several planned uses; this PR delivers shadow mode only (switch, log, measurement). Activation (letting Jev absorb wakes) is a later, separate PR and is out of scope. This is firstmate's own shared tracked material: follow firstmate-coding-guidelines (one-owner rule, one sentence per line in Markdown, shellcheck-clean bin scripts, colocated behavior tests, no agent co-author).
Captain's binding design decisions (data/jev-integrazione/decisioni.md):
API facts (from OpenRouter docs and one allowed real smoke call): Jev is not a chat model; it is called at POST https://openrouter.ai/api/alpha/decisions with {model, state, questions}, and returns answers..choice/confidence/probabilities plus usage.cost in USD. The smoke call cost USD 0.0000196 and took 1.05 s.
Acceptance: switch off by default and verified off without the key; with the key, shadow classifications are logged privately under state/jev/ (gitignored) without changing any wake behaviour; limits enforced and tested (timeout, cap, API error) with the network mocked in tests (fake curl on PATH) - tests must never call OpenRouter; masking tested; measurement command works on a fixture log; docs updated where configuration and .env keys are documented (docs/configuration.md, AGENTS.md layout, docs/scripts.md). Known pre-existing issue out of scope: bin/fm-test-run.sh --check-coverage fails on unmodified main under a non-C locale (comm without LC_ALL=C) and passes with LC_ALL=C.
What Changed
bin/fm-jev.shandbin/fm-jev-lib.sh, an advisory, shadow-only classifier. It runs only when the home's gitignored.envsetsOPENROUTER_API_KEY(anOPENROUTER_API_KEYin the ambient environment does not count) andSTATEis that home's own state dir.typesafe/jev-1.13) one three-way question: firstmate, absorbable, or captain.config/jev-daily-cap).state/jev/.status,mask, andreportsubcommands are included.reportshows agreement %, wrongly absorbable wakes, a three-way breakdown, the go-live criteria, and up to ~20 doubtful cases.fm-wake-drain.sh: classifies the drained batch and records--ack-throughacks.fm-send.shandfm-control.sh: record steers;fm-send.sh --resolve-keyrecords decisions.fm-captain-hold.sh: records hold and answer decisions.fm-turnend-guard.sh: records the turn-end outcome (ack, message, or unknown) fromlast_assistant_message, never the text.FM_JEV_OBSERVE=0so they never count as a firstmate steer. This covers bootstrap nudges, config push/inherit, remote secondmate control, and pending-reply resends.tests/fm-jev.test.sh, which uses a fake curl on PATH, and extendedtests/fm-turnend-guard.test.sh. Documented the switch, limits,state/jev/and the new env knobs indocs/configuration.md,docs/scripts.mdandAGENTS.md, and registered the new test inbin/fm-test-run.sh.Risk Assessment
✅ Low: The latest fix round correctly adds FM_JEV_OBSERVE=0 to the remote cmd_key relay, and the new fixture steer (task y, recorded after its unacknowledged wake is presented) would make the test fail if the unbounded-end branch were reverted; the rest of the shadow-only feature is best-effort, opt-in and off the wake path.
Testing
I ran the new colocatedtests/fm-jev.test.sh(13 cases). I also ran the existing tests for the files that got hooks: turn-end guard, wake drain, send --resolve-key, captain-hold and control. All passed. I then ran a manual end-to-end demo against temporary homes with a fake curl, so nothing reached the network. It shows: - Jev stays off when the key is only in the environment: no state/jev directory and no curl call. - The drain output is identical with Jev on and off, once the per-run recovery-generation nonce is normalized. - Only the masked wake reason and the worker's last status line are sent, with<url>and<path>substituted. - The key is passed to curl on stdin (-K -) and appears nowhere in argv or under state/. - The private shadow log is mode 0600 and records the presented, classified and ack events. - The report lists the wake as doubtful because no turn end was recorded. - A timeout records a skip and a pause until the next day, and the wake is still shown. There is no UI surface, so the evidence is CLI transcripts and test logs. The overall result is a pass, and the worktree is clean.Evidence: End-to-end CLI transcript: switch off/on, identical drain output, masked request body, key not in argv, shadow log, report, timeout pause
Source: End-to-end CLI transcript: switch off/on, identical drain output, masked request body, key not in argv, shadow log, report, timeout pause
Evidence: Script that produced the end-to-end transcript (fake curl, no network)
Source: Script that produced the end-to-end transcript (fake curl, no network)
Evidence: fm-jev behavior test output
Source: fm-jev behavior test output
Evidence: Drain output is identical with Jev off and on
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 4 issues found → auto-fixed (3) ✅
bin/fm-send.sh:625- The Jev steer/decision hook fires on every fm-send.sh invocation, including automated sends that no firstmate handling turn made. One example is the watcher's pending-reply recovery (bin/fm-pending-reply-lib.sh:830 calls fm-send.sh with FM_PENDING_REPLY_EXISTING_CORR set). Another is bin/fm-bootstrap.sh's secondmate nudges. Failure path: a wake is presented, the watcher sends a recovery message to that task before the ack or turn end, and a 'steer' event lands inside the window. The report then labels the wake truth=firstmate (why=steer). If Jev said absorbable, the wake counts toward 'wrongly absorbable', the go-live blocker, even though firstmate only acknowledged. This violates the intent's ground-truth rule ('needed firstmate if its handling turn produced ... a steer'). Fix: record only sends that firstmate initiates, e.g. skip the observe when FM_PENDING_REPLY_EXISTING_CORR is set or when called from the watcher or bootstrap paths.bin/fm-jev.sh:470- Steer and decision attribution gathers every steer or decision between a wake's presentation and its turn end, whichever batch the steer belongs to. It then attributes any steer whose task has no wake in this batch to all wakes in this batch. Example: batch 1 holds a single wake A (task X, or no task). A second drain presents batch 2 with wake B for task Y. Firstmate steers Y and acks through both. For wake A, $mates=[A] has no task Y, so the steer goes to A with shared=false (the batch has one mate). A becomes truth=firstmate, unflagged, and counts as wrongly absorbable if Jev said absorbable. The same happens for a steer naming a task that had no wake at all. The intent says 'a steer/decision naming a task attributes to that task's wakes in the same drain batch, otherwise to all wakes in it'. Possible fix: when a steer names a task that has a wake in any overlapping batch, or a known task with no wake, do not attribute it to unrelated single-wake batches, or at least flag it as shared. This changes measurement semantics the captain defined, so confirm the intended rule.bin/fm-turnend-guard.sh:667- The turn-end outcome is recorded before the guard's verdict, so a Claude stop that the guard then blocks (exit 2, supervision off) still logs a turn_end even though the turn continues. The new test asserts this. The report takes the first turn_end after the ack, so a blocked stop can become that wake's 'handling turn end'. This is rare (only when supervision is broken) and it only affects measurement.bin/fm-jev.sh:463- The report's jq scans all of $all for each wake (ack, turn_end, acts), which is O(wakes × events). The per-row dedupe check in observe-drain (line 405) also rescans the whole shadow.jsonl, which is append-only and never rotated. This is fine for a one-week shadow window but grows without bound if shadow mode is left on.🔧 Fix: Credit Jev ground truth to open wakes; skip automated sends
2 issues (1 warning, 1 info) still open:
bin/fm-config-push.sh:155- The fix sets FM_JEV_OBSERVE=0 on the bootstrap remote config-reread nudge (bin/fm-bootstrap.sh:538) and on the local config-reread pointer (bin/fm-config-inherit-lib.sh:886). It misses the matching remote reread nudge in fm-config-push.sh, which sends the same $FM_REMOTE_SECOND_MATE_NUDGE_MESSAGE through fm-send.sh to fm-$id without the opt-out. Failure path: firstmate runs fm-config-push.sh while a wake for secondmate task fm-$id is open. A 'steer' naming that task is then recorded, and the wake becomes truth=firstmate (why=steer). It counts as wrongly absorbable if Jev said absorbable. That contradicts the fm-jev-lib.sh header, which says config-reread sends record nothing. Fix: add FM_JEV_OBSERVE=0 to that fm-send.sh invocation.bin/fm-jev.sh:376- $open only includes wakes with a computed end, meaning an acknowledgement exists. It also only includes wakes presented inside the report window ($since..$now). Consider a steer naming task X whose wake is still unacknowledged when the report runs (for example, a report run mid-turn, or an ack-through covering only an earlier drain's seq), or whose wake was presented before --days. That steer is treated as 'no open wake' and credited, shared=true, to every other open wake. An unrelated wake can therefore become truth=firstmate. It is flagged shared and lands in the doubtful list, so the impact is limited to measurement noise the captain will see.🔧 Fix: Keep unacked and pre-window Jev wakes open; skip config-push sends
2 warnings still open:
bin/fm-remote-secondmate-control.sh:205- This round added FM_JEV_OBSERVE=0 to the remote relay in cmd_send, because the parent home already records the steer (bin/fm-send.sh:512 runs before the remote dispatch, for text and --key sends alike). The sibling relay cmd_key was not updated. It calls fm-send.sh against TARGET_HOME/state with no opt-out. Failure path: the parent firstmate runsfm-send.sh <remote-task> --key Enter. The parent records a steer, and then the remote leg's fm-send.sh records a second steer in the secondmate home's shadow log whenever that home's .env has OPENROUTER_API_KEY. That steer is credited to the secondmate's own open wakes, and it is shared when unnamed. It can therefore mark a secondmate wake truth=firstmate and count it as wrongly absorbable, even though that secondmate took no action. The updated fm-jev-lib.sh header and the docs row now say the remote relay records nothing, so this contradicts them. Fix: add FM_JEV_OBSERVE=0 to the fm-send.sh invocation in cmd_key.tests/fm-jev.test.sh:368- The new fixture test says it covers unacknowledged wakes, but it only exercises the pre-window half of the r2-2 fix. The only unacknowledged wake (e:4, task y) is presented after both actions, and no steer or decision names task y. The test would therefore still pass if the.end == nullbranch in bin/fm-jev.sh:376 were reverted to the old.end != null and ...condition. The user asked for fixture coverage of both halves. Fix: add a steer naming the unacked wake's task after its presentation, with another Jev-absorbable wake still open. Then assert that the steer is not credited to that other wake (wrongly absorbable stays 0) and that the unacked wake stays unknown.🔧 Fix: Skip Jev observe on remote key relay; cover unacked wakes
✅ Re-checked - no issues remain.
✅ **Test** - passed
✅ No issues found.
bash tests/fm-jev.test.sh: all 13 cases pass. They cover: off by default and a key in the environment does not turn it on; a different state dir never uses the key; the drain shows the same output with Jev on or off; masking, and the key stays out of argv and off disk; timeout, API error, transport error and missing cost each pause Jev for the day; the daily spend cap; the detached request never blocks the drain; the hooks record events without the turn text; FM_JEV_OBSERVE=0 senders are not recorded; how the report attributes wakes across drains, shared cases, and unacknowledged or pre-window wakes; the report output itself.bash tests/fm-turnend-guard.test.shbash tests/fm-wake-drain-unread-status.test.shbash tests/fm-send-resolve-key.test.shbash tests/fm-captain-hold-lifecycle.test.shbash tests/fm-control.test.shManual end-to-end check:bash e2e-demo.sh <repo> <tmp>in the evidence dir, using temporary homes and a fake curl on PATH, with no network. It runsfm-jev.sh statusoff and on, drains the same wake in the off and on homes and compares the output, captures the masked request body and curl argv, acks the wake and dumps the 0600 shadow.jsonl, runsfm-jev.sh report --days 7, and forces a timeout to show the pause for the day while the wake is still shown.✅ **Document** - passed
✅ No issues found.
🔧 **Lint** - 1 issue found → auto-fixed ✅
🔧 Fix: Silence jq SC2016 and replace A && B || C test check
✅ Re-checked - no issues remain.
✅ **Push** - passed
✅ No issues found.