Skip to content

fix(captain-msg): consume alert-router-relayed inbound payloads - #44

Merged
Bre77 merged 1 commit into
mainfrom
fm/fm-captain-msg-relay-consume
Jul 29, 2026
Merged

Bre77 merged 1 commit into
mainfrom
fm/fm-captain-msg-relay-consume

Conversation

@Bre77

@Bre77 Bre77 commented Jul 29, 2026

Copy link
Copy Markdown
Owner

Intent

  • Restore the two-way captain phone channel: since the Stage-7 alert-router cutover (2026-07-27), fm.ba.id.au lands on the shared alert-router, not this repo's own listener, so every inbound Twilio webhook to /captain-msg was 401'd (the router authenticates with a shared X-ClickStack-Secret header Twilio cannot send).
    • Teslemetry/tools PR fix(spawn): require a proven worktree root before ending the treehouse poll #26 fixed the router side: /captain-msg now carries no router-side auth at all and relays every POST verbatim - {"url", "signature", "body"} - into this home's state/captain-msg-inbox/, the exact directory bin/fm-captain-msg-lib.sh already pointed at. The router holds no Twilio Account Auth Token and makes no trust decision about the content; validation has to happen downstream.
    • Delivery topology is unchanged from the BetterStack precedent (docs/betterstack-webhook.md): same-host direct filesystem write into the existing inbox dir over a shared firstmate:fm-alerts group, not a new transport. That side (pm2 dotenv, directory perms) was deployed and verified live separately (srv-router-captain-msg-deploy).
  • Extracted the one Twilio HMAC-SHA1 signature implementation out of bin/fm-clickstack-listener.py into bin/fm_twilio_sig.py, imported in-process by the listener (unchanged behavior, still verified against Twilio's canonical test vector) and called out-of-process by a new bin/fm-twilio-sig-verify.py CLI (Auth Token on stdin, never argv/env) - one owner, two callers, per the direct-listener and router-relay paths.
  • bin/fm-captain-msg-poll.sh now validates a router-relayed payload at poll time before ever treating it as a pending reply: signature + From match, normalize a valid one in place into the same {received_at, params} shape the direct listener already produces (so the captain-msg-response skill and every downstream consumer never has to know which path a reply came through), and quarantine an invalid one - state/captain-msg-inbox/quarantine/, with a reason sidecar, loud output so the wake still fires. An infra failure to validate (1Password unreachable, jq/python3 missing) is reported loudly too, but the payload stays untouched for a retry - never quarantined on a hunch, and never counted as a trusted pending reply either.
  • Fixed the actual reason this home's watcher poll shim was never armed: nothing was wrong with bin/fm-bootstrap.sh's presence-gated captain_msg_setup (reproduced it correctly generating the shim from this home's real config, in isolation) - the config file was dropped mid-session, and bootstrap only runs once at session start (AGENTS.md section 3), so it never got the chance. Documented the fix in docs/captain-messaging.md Setup: run bin/fm-bootstrap.sh once after a mid-session config drop.
  • docs/captain-messaging.md "Inbound route" rewritten for both paths (router-relayed and direct-listener), with a new "Validation" subsection covering the quarantine contract; .agents/skills/captain-msg-response/SKILL.md updated for the quarantine wake and the two validation points.

Testing

  • tests/fm-captain-msg.test.sh (+18 cases, 42 total): fm-twilio-sig-verify.py standalone (valid/tampered-body/wrong-URL), and the poll's full router-relay path - valid normalization, quarantine (tampered body, missing signature, From mismatch, unrecognized shape) each with a reason sidecar and never counted pending, the auth-token-fetch-failure path (loud, untouched, not quarantined, not trusted), and confirmation an already-normalized payload is never re-validated (op tripwired on the second poll).
  • tests/fm-clickstack.test.sh / tests/fm-betterstack.test.sh (pre-existing, unmodified): still fully green - the shared-listener signature refactor left both routes' own behavior untouched.
  • bin/fm-lint.sh (shellcheck, pinned): clean. bin/fm-doc-audience-check.sh: clean.
  • Manual synthetic exercise of the exact router-relay wire shape (Teslemetry/tools PR fix(spawn): require a proven worktree root before ending the treehouse poll #26's own envelope) against the poll script, including catching and fixing a real bug along the way: an unvalidated raw payload was briefly being reported as a trusted pending reply whenever validation itself failed (e.g. 1Password unreachable) - the pending scan now only counts already-normalized ("params"-shaped) files.

Follow-up after merge

The router-side relay is already live and verified (srv-router-captain-msg-deploy, Teslemetry/tools PR #26 deployed to the pm2 alert-router). Two things remain, both outside a worktree's reach:

  1. Run bin/fm-bootstrap.sh once (or let the next session start do it) so state/captain-msg-watch.check.sh gets armed against config/captain-msg.env, which is already present.
  2. A live captain phone round-trip (bin/fm-captain-msg.sh + a real reply) to close the loop end to end with the wake actually firing - the definition of done for this feature, but not something a fork-only crewmate branch can trigger against production credentials.
Full narrative / original brief

Adapt firstmate's captain-messaging inbound path to consume the alert-router's relayed payloads, restoring the two-way phone channel. Root cause: since the Stage-7 router cutover, https://fm.ba.id.au/captain-msg lands on the alert-router, not this home's own listener; tools PR 26 added a router relay that persists each Twilio post (raw form body bytes + X-Twilio-Signature header + original URL) into a captain-msg inbox dir; a parallel ops task (srv-router-captain-msg-deploy) deployed it.

Work: extend the captain-msg inbound path to consume router-relayed payloads (validate the Twilio signature from the stored body+header+URL, reusing the listener's validation semantics, plus the From-number check), surfacing the message as a pending inbound exactly like the direct-webhook path; determine empirically how BetterStack's relay reaches state/betterstack-inbox/ and mirror the same mechanism rather than inventing transport; ensure the watcher check shim is generated by the same bootstrap owner that arms the other webhook polls, finding and fixing the generation path if broken; update docs/captain-messaging.md for the router-relay reality; colocated tests for signature validation, From mismatch, and quarantine behavior; and, once the router deploy reports live, an inbound reply payload consumed end to end with the wake firing.

The Stage-7 alert-router cutover moved fm.ba.id.au off this repo's own
listener, so every inbound Twilio webhook was 401'd. Teslemetry/tools PR #26
made the router relay /captain-msg verbatim into the existing inbox dir
instead of validating it; this teaches the poll path to validate and
normalize (or quarantine) that shape, sharing one HMAC implementation with
the direct-listener path via a new fm_twilio_sig.py module.
@Bre77
Bre77 merged commit 4c6238a into main Jul 29, 2026
10 checks passed
Bre77 added a commit that referenced this pull request Jul 29, 2026
…#45)

* feat(provenance): label fleet PRs and drop Claude's co-author trailer

The captain's commits now carry a single identity everywhere, so fleet
work needs metadata-based provenance instead: an "fm" PR label (name
configurable via config/pr-label) applied once fm-pr-check.sh records
pr=, covering every delivery mode that opens a PR (no-mistakes,
direct-PR, fork-only) from one call site, degrading to a stderr
warning rather than ever losing a PR.

Claude Code injects its own Co-Authored-By trailer regardless of
AGENTS.md's no-agent-co-author rule; fm-spawn.sh now scopes
--settings '{"attribution":{"commit":""}}' to every claude
crewmate/secondmate launch, verified empirically against a real
self-drafted commit. Codex and opencode were checked and do not
inject an equivalent trailer in firstmate's actual launch shape; pi,
grok, and kimi were unavailable to check and are left unverified
rather than guessed at.

* fix(tests): restore the executable bit on fm-captain-msg.test.sh

Committed as mode 100644 by #42/#44, so bin/fm-fork-deliver.sh's local
gate (which execs each tests/*.test.sh directly) hits Permission
denied; real CI is unaffected since bin/fm-test-run.sh invokes tests
via `bash "$script"`.
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