Skip to content

feat(triggers): receiver notices, 25h dedup, hello topics, ack outbox, and receiver module (RFC 0004 Slice 4) - #382

Merged
kombuchapunk merged 10 commits into
mainfrom
triggers/notices-slice4
Dec 4, 2026
Merged

kombuchapunk merged 10 commits into
mainfrom
triggers/notices-slice4

Conversation

@kombuchapunk

Copy link
Copy Markdown
Collaborator

Summary

Completes Triggers (RFC 0004) Slice 4 by wiring receiver notices end-to-end across packages/core, packages/cli (ControlDaemon + TriggerEngine), and packages/receiver.

Closes #339

Key Changes

  • Core (packages/core/src/triggers/receiver.ts):
    • Adds canonical receiverName(url) shared by trigger add and ControlDaemon (rejecting authority-less URIs and IPv6 brackets cleanly).
  • Daemon notices & persistence (packages/cli/src/control/notices.ts, daemon.ts, triggers.ts):
    • Implements pure boundary takeNotice() following CONTROL.md §14.7 steps 1–6 in exact order, replacing the test-only conformance shim so all 15 conformance/triggers/*.json scenarios run through production code.
    • Adds NoticeSeen (25-hour ULID dedup store in <state>/control/notices-seen.jsonl, fsyncSync on append and compaction, NOTICE_SEEN_MAX = 20_000 per receiver, torn-line tolerance).
    • Adds NoticeAckOutbox (chunks of 50, 25-hour pruning, NOTICE_ACK_MAX = 2_000 per receiver with O(1) lookup preserving ran/taken under hostile floods, writeWhole() Windows-safe atomic writes committed before in-memory mutation, safe retry on failed flush without unhandled rejections).
    • Holds seen IDs while the engine still has that notice in hand (inHand) before its first ack so slow sign-in lookups can never emit duplicate ahead of taken/ran.
    • Saves waiting runs to disk before emitting taken, and routes final acks back to the exact originating pairing even if a second pairing is added on the same host.
    • Adds single-flight helloNow() with dirty bit sending ControlHello.triggers (field 18, per-receiver topics only), stat-based triggers.json reload before each poll's notices (closing the 5s watchFile gap), pollLoop notice ingestion (≤50/poll, messages handled first), POST {control}/notices/ack flushing, and strict no-op behavior for non-claim daemons.
  • Reference Receiver Notices Module (packages/receiver/src/notices.ts) + FakeReceiver:
    • Implements pure onHello, offer, forPoll, onAcks, plus 0600 writeMachineNotices (creating parent dir 0700 + writeWhole()), enforcing topic allowlist, latest-per-(topic, subject) 200-cap queue, and ran outcome preservation.
    • Wires FakeReceiver to production packages/receiver/src/notices.ts with live HTTP integration tests covering all adversarial scenarios.
  • Changeset: adds .changeset/triggers.md (sessionpipe, sessionpipe-core, sessionpipe-receiver).

Verification

  • Architected & audited by Fable 5.2 (xhigh), built by Opus 5.5, and adversarially stress-tested (14/14 live SIGKILL/HTTP/WebAuthn probes) by Gemini 4 Neon.
  • npm run lint (0 errors, 0 new warnings), npm run typecheck, npm run build, npm run schemas -- --check (31 files), npm run conformance (510 ok), npm run fixture-scan (421 clean), and full monorepo npm run test:coverage (5,419 tests, 100% Stmts / Branch / Funcs / Lines).

kombuchapunk and others added 10 commits December 4, 2026 10:03
`trigger add` writes the name into a rule and the daemon will match a notice's
receiver and say each receiver's topics in its hello by it: one function, so
the two can never disagree. `hostOf` in the CLI's triggers.ts is gone, its
callers moved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: kombuchapunk <35040017+kombuchapunk@users.noreply.github.com>
- control/notices.ts: `takeNotice`, the id, the switch, the shape, the expiry,
  the 25-hour dedup and the match in that order; `NoticeSeen`, ids per
  receiver appended and synced before anything runs, compacted at start and
  daily, at most NOTICE_SEEN_MAX live per receiver; `NoticeAckOutbox`, one
  ack waiting per notice (a final replaces `taken`, a `duplicate` never goes
  out ahead of what waits), dropped past 25 hours.
- The engine saves a waiting or queued run before it says `taken`: a crash
  between the two can no longer leave a `taken` with no run to give the
  final ack.
- The conformance scenarios run through `takeNotice`; the test's own copy
  of steps 1 to 6 is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: kombuchapunk <35040017+kombuchapunk@users.noreply.github.com>
packages/receiver/src/notices.ts keeps one machine's notices: what its last
hello allows (`onHello`), a notice offered on a listed topic, one per topic
and subject, 200 at most (`offer`), the 50 oldest for a poll (`forPoll`),
and the machine's acks, read against the closed ack shape (`onAcks`): `taken`
pauses delivery until expiry, a final ends it, and `duplicate` never replaces
an outcome already recorded. Pure; a file pair writes the state whole, 0600.

The receiver's `@sessionpipe/core/schema` path now points at the built
declarations, as the CLI's does: nothing imported it before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: kombuchapunk <35040017+kombuchapunk@users.noreply.github.com>
…s hello

- Every hello carries `triggers` (ControlHello field 18): the engine's word
  for the pairing's name when this daemon holds the rules' claim and the
  name is that pairing's alone, off otherwise. Sent at once when the engine
  says it changed, or a pairing's name changes on reload; hellos go one
  round at a time (single flight with a dirty bit), so on, off, on ends on.
- A poll answer's notices, after its messages: `takeNotice` for each of the
  first 50, `fire` into the engine, every outcome kept in the notice-ack
  outbox and sent to `POST {control}/notices/ack` 50 at a time. A poll that
  carried notices waits for its acks (3 s at most) and then 1 s more.
- Before taking notices, the rules file's stat is compared with the one last
  read (`rulesStamp`): `trigger off` holds before the 5-second watcher looks.
- A daemon without the claim takes no notice, acks none and writes neither
  notice file. Two pairings on one host name neither triggers a rule.
- FakeReceiver keeps notices through the reference receiver's own module;
  `notify`, `rawNotices` and `noticeAckStatus` drive the live tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: kombuchapunk <35040017+kombuchapunk@users.noreply.github.com>
The `?? ""` of the ask chain and the case with no `from` were reached by no
test (handlers.ts:247, 252 on 1a5e5f8 too), which left branch coverage at
98.69% for that file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: kombuchapunk <35040017+kombuchapunk@users.noreply.github.com>
trigger-notices.md no longer says nothing runs on a notice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: kombuchapunk <35040017+kombuchapunk@users.noreply.github.com>
…pairing)

- A notice sent again while its run is still looking (a slow sign-in check
  before `taken`) is held, not acked `duplicate`: the engine says which
  notices it has in hand with nothing said yet (`inHand`), and `takeNotice`
  holds those. A live test with a 1.5 s sign-in look fails without it.
- NoticeAckOutbox: indexed by receiver and id (a new ack is one append and
  one map entry), at most NOTICE_ACK_MAX (2 000) per receiver, the oldest
  neither `ran` nor `taken` giving way first; every change on disk before it
  is made in memory, rewritten through core's writeWhole; one ack per notice
  again when the file is read back.
- A flush whose outbox write fails is said in the log and tried again in
  5 s; it never rejects into nothing.
- NoticeSeen's compaction writes a synced file beside the old one and
  renames it, so a power cut can't leave it empty.
- A notice's acks go back to the pairing it came from, kept from the poll
  until its final ack, even when a second pairing joins its host.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: kombuchapunk <35040017+kombuchapunk@users.noreply.github.com>
`receiverName("file:///etc/passwd")` was "", now null.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: kombuchapunk <35040017+kombuchapunk@users.noreply.github.com>
A second, different final after `ran` is not believed: the session it ran in
stays. writeMachineNotices makes the file's folder (0700) before writing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: kombuchapunk <35040017+kombuchapunk@users.noreply.github.com>
…fixes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: kombuchapunk <35040017+kombuchapunk@users.noreply.github.com>
@kombuchapunk
kombuchapunk merged commit 98c4f39 into main Dec 4, 2026
18 checks passed
@kombuchapunk
kombuchapunk deleted the triggers/notices-slice4 branch December 4, 2026 18:26
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.

Spec proposal: triggers — a rule made at the machine, run on a notice or a clock

1 participant