Repository navigation
feat(channels): heartbeat builds channels[] from recent messages — Phase 2C - #232
Conversation
…ase 2C
Phase 2C: mesh gist's channels[] is now self-correcting. The host's
heartbeat scans the last N (default 200) lines of messages.jsonl,
extracts unique `channel` envelope fields, and writes that union as
the gist's channels[] — with the host's primary room as a fallback.
This unblocks continuum-b741's "I want #cambriantech, mesh advertises
#useideem" case from a different angle: now any joiner that sends a
message tagged #cambriantech causes the host's next heartbeat to
include #cambriantech in channels[]. Joiners discovering the mesh see
all currently-active channels, not just whatever the host happened
to be subscribed to at create time.
## Algorithm
1. Heartbeat tick fires (default every 30s).
2. Read last 200 lines of host's messages.jsonl.
3. For each line: json.loads, extract .channel, dedupe (preserve
order of first appearance).
4. If host's primary room isn't in the set, append it.
5. Embed as channels[] in the gist payload.
Window-based decay: a channel that goes silent eventually drops off
once 200 newer messages arrive without it. Fallback ensures host's
own channel is always advertised even on a fresh log.
## What's still required from joiners
Nothing! No new RPC, no special "subscribe" command, no gist contention.
Joiners just send messages tagged with their channel (cmd_send already
stamps the field per Phase 2A). Host's next heartbeat picks it up.
## Verification
bash -n: clean
Standalone python check: 4-msg fixture with channels [useideem,
cambriantech, useideem, general] + fallback cambriantech
→ emits ["useideem", "cambriantech", "general"]. Order-preserving,
deduped, fallback included.
test/integration.sh tabs: 19/0 ✓
test/integration.sh room: 14/0 ✓
test/integration.sh general_sidecar_default: 10/0 ✓
## Performance
Heartbeat fires every 30s; reads ~200 lines + json.loads per line.
On a typical messages.jsonl (~80-150 chars/line) that's ~30KB read +
parse, well under the python startup cost. No hot-path concern.
For runaway-message-volume scenarios, `AIRC_HB_RECENT` overrides the
window. Setting it to 0 effectively disables the scan and falls back
to host's primary room only.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
There was a problem hiding this comment.
Pull request overview
This PR makes the mesh gist’s channels[] field self-correcting by deriving it from recent messages.jsonl activity, allowing joiners to “advertise” channels implicitly via message envelopes.
Changes:
- Add heartbeat-time scan of the last ~200
messages.jsonllines to build a dedupedchannels[]array. - Ensure the host’s primary room is used as a fallback (and appended if missing from recent activity).
- Update the heartbeat gist payload to publish the computed
channelsarray.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| "$AIRC_PYTHON" -c ' | ||
| import json, os, sys | ||
| log = os.environ.get("AIRC_HB_MSGS", "") | ||
| fallback = os.environ.get("AIRC_HB_ROOM", "general") or "general" |
There was a problem hiding this comment.
The Python passed to python -c is indented inside the single-quoted string (e.g. import json...). At top-level this triggers IndentationError: unexpected indent, so the channel scan will always fail and fall back to ["${_hb_room}"], meaning channels[] never becomes self-correcting. Strip leading indentation from the embedded Python (or use a heredoc / dedent) so it can execute successfully.
| with open(log) as f: | ||
| # Read last N lines without slurping the full file. | ||
| lines = f.readlines()[-window:] |
There was a problem hiding this comment.
f.readlines() reads the entire messages log into memory before slicing to the last window lines, which can become expensive as messages.jsonl grows (and this runs every heartbeat). If the intent is truly “last N lines without slurping”, consider using a tail-style approach (e.g., collections.deque(maxlen=window) iteration, or seeking from EOF) so runtime/memory stay bounded.
| import json, os, sys | ||
| log = os.environ.get("AIRC_HB_MSGS", "") | ||
| fallback = os.environ.get("AIRC_HB_ROOM", "general") or "general" | ||
| window = int(os.environ.get("AIRC_HB_RECENT", "200")) |
There was a problem hiding this comment.
window = int(os.environ.get("AIRC_HB_RECENT", "200")) can yield 0/negative values (or throw on non-int). With a negative window, lines[-window:] behaves unexpectedly and can include most of the file; with 0 it yields no lines. It’d be safer to clamp window to a minimum of 1 (and default on parse failure) so heartbeats stay predictable.
| window = int(os.environ.get("AIRC_HB_RECENT", "200")) | |
| raw_window = os.environ.get("AIRC_HB_RECENT", "200") | |
| try: | |
| window = int(raw_window) | |
| except (TypeError, ValueError): | |
| window = 200 | |
| if window < 1: | |
| window = 1 |
| elif fallback not in seen: | ||
| chans.append(fallback) | ||
| print(json.dumps(chans)) | ||
| ' 2>/dev/null || echo "[\"${_hb_room}\"]") |
There was a problem hiding this comment.
This command discards all Python stderr via 2>/dev/null, which makes it hard to diagnose why channels[] fell back to just the room. Since the heartbeat already captures errors to $_hb_stderr for gist writes, consider redirecting this stderr there (or otherwise surfacing it) instead of swallowing it silently.
| ' 2>/dev/null || echo "[\"${_hb_room}\"]") | |
| ' 2>>"$_hb_stderr" || echo "[\"${_hb_room}\"]") |
| "airc": 1, | ||
| "kind": "mesh", | ||
| "channels": ["${_hb_room}"], | ||
| "channels": ${_hb_channels}, |
There was a problem hiding this comment.
New behavior builds the mesh gist channels[] from recent messages.jsonl activity, but there doesn’t appear to be an integration assertion covering this (the existing heartbeat scenario checks last_heartbeat, but not channels). Adding a test that sends messages on multiple channels and verifies the gist channels array contains the deduped union + fallback would help prevent regressions.
…te (#235) fix(update): loud warning when 'airc canary'/'airc update' runs while monitor is alive vhsm-d1f4's gotcha 2026-04-28: ran 'airc canary' to pick up #232, saw 'Updated X -> Y' confirmation, but their RUNNING monitor was still executing the pre-#232 in-memory bash. Phase 2C heartbeat scan never fired until they explicitly tore down + reconnected. The existing dim hint at the end of cmd_update was easy to miss amid install.sh's output. This makes the warning conditional + loud when it actually matters (running monitor present). ## Fix cmd_update detects `.airc/airc.pid` in the current scope and probes the PID. If alive, prints a multi-line action-shaped block with the rebar command on its own line: ⚠ A running airc monitor (PID NNNN) is still on the OLD code. Restart to pick up <new-sha>: airc teardown && airc connect Silent when nothing is running. The single-line hint that fired ALWAYS-on-update is dropped — the conditional version is more useful when it matters and quieter when it doesn't. ## What this does NOT do Auto-restart. That would interrupt active SSH sessions (DM in flight, file transfer, takeover dance). User decides when to bounce. ## Verification bash -n: clean test/integration.sh tabs: 19/0 ✓ Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Mesh gist's channels[] becomes self-correcting. Host's heartbeat scans last 200 lines of messages.jsonl, extracts unique channel envelope fields, writes that union (with host's primary room as fallback) as channels[].
Joiners no longer need to push subscriptions — they just tag messages with their channel and the host's next heartbeat picks it up. Silent channels age out via the window.
Verification: tabs 19/0, room 14/0, general_sidecar_default 10/0. Standalone fixture exercise confirms order-preserving dedupe + fallback merge.