Skip to content

feat(heartbeat): wire emission — cmd_send --heartbeat + reminder_timer_loop hook + monitor filter (airc#644 PR-2) - #647

Merged
joelteply merged 1 commit into
canaryfrom
feat/peer-heartbeat-emit
May 17, 2026
Merged

joelteply merged 1 commit into
canaryfrom
feat/peer-heartbeat-emit

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

What

Closes airc#644 end-to-end. PR-1 (#645) landed envelope shape + helper + cmd_peers display. PR-2 wires the actual cross-peer emission so PROCESS_DOWN detection becomes real on the wire.

Changes

File Change
`lib/airc_bash/cmd_send.sh` New `--heartbeat` flag stamps `kind="heartbeat"` into payload; keeps `from=$my_name` (not "airc" like --system); implies --internal
`airc` `reminder_timer_loop` fires `cmd_send --heartbeat` every 60s (best-effort; missed sends are themselves the PROCESS_DOWN signal)
`lib/airc_core/monitor_formatter.py` Drop kind=heartbeat from UI rendering AND arm inbound watchdog (any inbound traffic = bearer alive)
`test/test_heartbeat_pr2.py` 9 tests pinning the cross-language contracts

Why The Cross-Language Drift Test Matters

The 60s cadence is hard-tied between bash (`local heartbeat_cadence=60`) and Python (`heartbeat.HEARTBEAT_CADENCE_SEC = 60`). If someone changes one without the other, `STALE_HEARTBEAT_SEC` (2× cadence = 120s) produces false PROCESS_DOWN signals during the divergence window — invisibly. `test_heartbeat_pr2.ReminderTimerLoopShapeTests` greps the bash literal and asserts it matches the Python constant so drift fails at test-time, not in production.

End-To-End State After This PR

Before PR-1 After PR-1 (#645) After PR-2 (this)
`airc peers` conflates "silent" with "process down" Envelope shape + display logic in place; no heartbeats yet so all peers still show `(silent)` Every airc emits heartbeat every 60s; after >120s of missed heartbeats a peer flips to `(PROCESS DOWN)`. Substrate-level fix live.

Validation

```
bash -n lib/airc_bash/cmd_send.sh → ok
bash -n airc → ok
python3 ast.parse monitor_formatter → ok
test/test_heartbeat_pr2.py → 9 tests passing
test/test_heartbeat.py (PR-1) → 18 tests still passing (4 skip on no-crypto)
test/test_envelope.py (existing) → 18 tests still passing
```

Live Test Path

Once this lands on canary + a peer runs `airc update --channel canary` + `airc join`:

  1. That peer starts emitting heartbeats every 60s
  2. Other peers' `airc peers` shows `last_heartbeat` separately from `last_message`
  3. If that peer's process dies, after 120s the others show `(PROCESS DOWN)`

The substrate gap airc#644 named is finally closed.

Title Prefix

`feat:` per conventional-commits; targets `canary` per the merge-to-canary-in-active-loop rule. The require-canary-or-hotfix gate is satisfied (canary-base PRs bypass the workflow entirely; `branches: [main]` filter).

…r_loop hook + monitor filter (airc#644 PR-2)

Closes airc#644 end-to-end. PR-1 (#645) landed the envelope shape,
helper module, and cmd_peers display. PR-2 wires the actual cross-peer
emission so PROCESS_DOWN detection becomes real.

## Changes

### lib/airc_bash/cmd_send.sh

New '--heartbeat' flag. Unlike '--system' which rewrites 'from' to
'airc' (system-wide notice), '--heartbeat' keeps 'from=$my_name'
because heartbeats are per-peer attribution. Stamps 'kind=heartbeat'
into the payload JSON so cmd_peers + monitor_formatter can recognize.
Implies '--internal' so a missing monitor doesn't produce noisy
error output every 60s (one missed heartbeat is fine; two missed is
the PROCESS_DOWN signal anyway).

### airc (reminder_timer_loop)

New heartbeat emit branch fires every HEARTBEAT_CADENCE_SEC (60s).
Best-effort: 'cmd_send --heartbeat --internal' in a subshell with
stdout+stderr suppressed. A transient send failure is precisely the
signal we want propagating — peer's airc process not emitting means
PROCESS_DOWN, no need to log every failure locally.

The 60s cadence is hard-tied to lib/airc_core/heartbeat.py's
HEARTBEAT_CADENCE_SEC constant. test_heartbeat_pr2.py greps the bash
literal and asserts it matches the Python constant so drift produces
a test failure rather than silent false PROCESS_DOWN signals.

### lib/airc_core/monitor_formatter.py

New filter case: 'kind == "heartbeat"' is dropped from UI rendering
AND arms the inbound-watchdog. The watchdog arming is important —
any inbound traffic (including remote heartbeats) proves the bearer
is alive, which is exactly the local-side signal we already use for
'airc_heartbeat' (a different field; that one is THIS process's
self-loop signal). Heartbeats are protocol traffic; user-invisible.

### test/test_heartbeat_pr2.py (new — 9 tests)

Four suites covering:
- CmdSendHeartbeatFlagShapeTests (2): constants pinned (kind literal
  agrees Python ↔ bash; cadence agrees Python ↔ bash)
- MonitorFormatterHeartbeatFilterTests (4): filter recognizes
  heartbeats; doesn't false-positive on chat or pre-#644 legacy
  envelopes; the constant the formatter reads (HEARTBEAT_KIND)
  matches what bash emits
- ReminderTimerLoopShapeTests (1): grep the bash for the cadence
  literal and assert it matches the Python constant — catches drift
  at test-time, not in production
- CmdSendHeartbeatFlagWiringTests (2): structural grep verifying the
  flag parser case + the payload-stamp line exist (catches bit-rot
  if someone refactors flag handling without preserving --heartbeat)

## Validation

- bash -n lib/airc_bash/cmd_send.sh  → ok
- bash -n airc                        → ok
- python3 ast.parse monitor_formatter → ok
- test/test_heartbeat_pr2.py          → 9 tests passing
- test/test_heartbeat.py (PR-1)       → 18 tests still passing (4 skip on no-crypto)
- test/test_envelope.py (existing)    → 18 tests still passing (all skip on no-crypto base env)

## Why It Works End-To-End After This PR

Before PR-1:
  airc peers shows 'last seen Nh ago (silent)' — can't tell heads-down
  from process-down.

After PR-1 (#645 merged):
  envelope.kind field + cmd_peers tracks last_heartbeat separately +
  display logic distinguishes the three cases. But no heartbeats are
  emitted yet, so all peers still show '(silent)'.

After PR-2 (this PR):
  Every running airc emits 'kind=heartbeat' every 60s through the
  existing broadcast path. Peers receive them; cmd_peers' separate
  tracking now has data. After two missed heartbeats (>120s), a
  peer's display flips from '(silent, heartbeat OK)' to
  '(PROCESS DOWN)'. The substrate-level fix is live.

Tested locally on canary: PR-1's airc peers no longer crashes (the
backtick hotfix #646 cleared the syntax error); PR-2's emission will
populate the columns once it merges + propagates.

Closes airc#644 end-to-end.
@joelteply
joelteply merged commit 9f67a3f into canary May 17, 2026
6 checks passed
@joelteply
joelteply deleted the feat/peer-heartbeat-emit branch May 17, 2026 05:35
joelteply added a commit that referenced this pull request May 17, 2026
…644 PR-2 follow-up) (#648)

PR-2 (#647) shipped the cmd_send --heartbeat path + the
reminder_timer_loop hook calling 'cmd_send --heartbeat --internal '
every 60s. Locally tested by:

  $ airc msg --heartbeat --internal ""
  ERROR: Usage: airc send <message>  or  airc send @peer <message>

The empty-body usage-check (sane default for chat) was rejecting the
heartbeat send. As a result no heartbeats reached messages.jsonl
after teardown+rejoin+75s — silent emission failure that PR-2's
unit tests didn't catch because they're structural (grep for the
flag) not behavioural (actually invoke the send).

Joel's discipline applied: 'have to see if it works.' Code review +
unit tests do NOT validate user experience. Run the actual command
a user would run. The reminder loop's invocation IS the user
surface here; teardown+rejoin+wait is the equivalent of a fresh
install path.

Fix: the usage-check now exempts heartbeats. The signal in a
heartbeat is in envelope metadata (kind/ts/from), not the msg body —
empty body is the correct shape. Other empty-body sends still die
loudly per the prior contract.

bash -n lib/airc_bash/cmd_send.sh → ok.
This was referenced May 28, 2026
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