Skip to content

feat(peers): aggregate across primary + sidecar scopes with [#room] tags - #124

Merged
joelteply merged 1 commit into
canaryfrom
feat/peers-cross-scope
Apr 26, 2026
Merged

joelteply merged 1 commit into
canaryfrom
feat/peers-cross-scope

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #122. Pre-fix: `airc peers` walked only the current scope's `peers/` dir, so a tab in #useideem couldn't see who was in #general without `cd`'ing into `.airc.general/`. Multi-room presence is meaningless if the operator can't get a unified roster.

Post-fix: `cmd_peers` detects sibling sidecar scopes (any sibling dir matching `.` with `room_name` + `peers/`), merges peer records by (name, host) across scopes, tags each peer with their rooms:

```
authenticator-fd63 → [email protected] [#useideem, #general]
vhsm-d1f4 → [email protected] [#useideem]
continuum-b741 → [email protected] [#general]
```

Operator perspective stays consistent regardless of which scope invokes `airc peers`.

Test plan

  • `bash test/integration.sh peers_cross_scope` — 4/4 pass
  • Live verification on this Mac with #useideem primary + #general sidecar — output matches design
  • Convention-flexible: works for both production (`.airc`/`.airc.general`) and test (`state`/`state.general`) layouts via prefix-detection (no `.airc` literal)

🤖 Generated with Claude Code

Pre-fix: 'airc peers' walked only the current scope's peers/ dir, so a
tab in primary (#useideem) couldn't see who was in the #general lobby
without manually 'cd'ing into .airc.general/. Multi-room presence is
meaningless if the operator can't see who's in each room from one query.

Post-fix: cmd_peers detects sibling sidecar scopes by walking the parent
dir for entries matching the primary scope's prefix (.airc.<room> /
state.<room> / etc — convention-flexible). Merges peer records by
(name, host) across scopes, tagging each peer with the rooms they
appear in. Same peer in both rooms folds into one line:

  authenticator-fd63 → [email protected]   [#useideem, #general]
  vhsm-d1f4 → [email protected]         [#useideem]
  continuum-b741 → [email protected]    [#general]

Operator perspective is consistent regardless of which scope they
invoke 'airc peers' from — the aggregation walks the same set of
sibling scopes either way.

Test: scenario_peers_cross_scope synthesizes primary + sidecar scopes
with shared / primary-only / sidecar-only peers; asserts merged output
shows all three with correct room tags from both scope perspectives.
4/4 pass.

Implementation note: the '.airc' literal is NOT hardcoded; prefix
detection strips a trailing .<word> from the current scope's basename.
Works for .airc/.airc.general (production) AND state/state.general
(test ad-hoc paths) without convention coupling. Defensive guards on
room_name + peers/ presence keep unrelated sibling dirs (.airc-old,
.airc.bak) out of the listing.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings April 26, 2026 17:59
@joelteply
joelteply merged commit 0dd6b56 into canary Apr 26, 2026
@joelteply
joelteply deleted the feat/peers-cross-scope branch April 26, 2026 17:59

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds cross-scope peer aggregation so airc peers shows a unified roster across the current scope and sibling sidecar room scopes, annotating each peer with [#room] tags.

Changes:

  • Update cmd_peers to scan the primary scope plus sibling sidecar scopes and merge peer records by (name, host), rendering combined room tags.
  • Add an integration test scenario (peers_cross_scope) to verify cross-scope visibility and tagging from both primary and sidecar scopes.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
airc Reworks cmd_peers to aggregate peers across sibling scopes and print room-tag annotations.
test/integration.sh Adds scenario_peers_cross_scope and wires it into the runner/all mode.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread airc
python3 -c "
import json, os, sys, re

primary_scope = os.path.expanduser('$AIRC_WRITE_DIR')

Copilot AI Apr 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inline Python source interpolates $AIRC_WRITE_DIR directly into a quoted Python string. If the path contains a single quote (or other problematic characters), this will produce invalid Python and fail the command; it also unnecessarily expands the attack surface for code injection via crafted env vars. Prefer reading paths from os.environ (and passing them via env vars) instead of embedding shell-expanded values into the Python program text.

Suggested change
primary_scope = os.path.expanduser('$AIRC_WRITE_DIR')
primary_scope = os.path.expanduser(os.environ['AIRC_WRITE_DIR'])

Copilot uses AI. Check for mistakes.
Comment thread test/integration.sh
echo "$out" | grep -qE 'shared.*[email protected].*\[#myproject.*#general\]|shared.*[email protected].*\[#general.*#myproject\]' \
&& pass "shared peer shows tagged with BOTH rooms" \
|| fail "shared peer missing dual-room tag (got: $(echo "$out" | grep shared))"

Copilot AI Apr 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This scenario validates that the shared peer has both room tags, but it doesn’t assert that the shared peer was actually merged into a single output line (duplicates would still satisfy the grep). Consider adding an assertion that shared appears exactly once in the output to ensure the de-duplication behavior is covered.

Suggested change
[ "$(echo "$out" | grep -c 'shared')" -eq 1 ] \
&& pass "shared peer appears exactly once in merged output" \
|| fail "shared peer was not de-duplicated to a single line (got: $(echo "$out" | grep shared))"

Copilot uses AI. Check for mistakes.
Comment thread airc
Comment on lines +4246 to +4271
rn_file = os.path.join(scope, 'room_name')
room = '(?)'
if os.path.isfile(rn_file):
try: room = open(rn_file).read().strip()
except Exception: pass
for f in sorted(os.listdir(peers_dir)):
if not f.endswith('.json'): continue
try:
d = json.load(open(os.path.join(peers_dir, f)))
except Exception:
continue
key = (d.get('name', f[:-5]), d.get('host', ''))
peers_by_id.setdefault(key, []).append(room)

if not peers_by_id:
print(' No peers yet.')
sys.exit(0)

# Render. Each peer once, with room annotations sorted + deduped.
for (name, host), rooms in sorted(peers_by_id.items()):
seen = set(); ordered = []
for r in rooms:
if r not in seen:
ordered.append(r); seen.add(r)
tags = ', '.join('#' + r for r in ordered)
print(f' {name} → {host} [{tags}]')

Copilot AI Apr 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In legacy/no-room scopes where room_name is missing or empty, this prints tags like "[#(?)]" or even "[#]" (if room_name file exists but is blank). Consider omitting room tags when room is unknown/empty (or falling back to the pre-change output format) to avoid confusing/invalid room annotations and to preserve compatibility in 1:1 invite mode.

Copilot uses AI. Check for mistakes.
Comment thread airc
Comment on lines +4227 to +4256
candidates = []
if os.path.isdir(parent):
for entry in sorted(os.listdir(parent)):
if entry == prefix or entry.startswith(prefix + '.'):
candidates.append(os.path.join(parent, entry))
scopes = [s for s in candidates
if os.path.isfile(os.path.join(s, 'room_name'))
and os.path.isdir(os.path.join(s, 'peers'))]
# Always include primary even if it doesn't have room_name yet — that's
# the legacy 1:1 invite mode case (use_room=0).
if primary_scope not in scopes and os.path.isdir(os.path.join(primary_scope, 'peers')):
scopes.insert(0, primary_scope)

# Build {(name, host): [room1, room2, ...]} by walking each scope's peers/.
peers_by_id = {}
for scope in scopes:
peers_dir = os.path.join(scope, 'peers')
if not os.path.isdir(peers_dir):
continue
rn_file = os.path.join(scope, 'room_name')
room = '(?)'
if os.path.isfile(rn_file):
try: room = open(rn_file).read().strip()
except Exception: pass
for f in sorted(os.listdir(peers_dir)):
if not f.endswith('.json'): continue
try:
d = json.load(open(os.path.join(peers_dir, f)))
except Exception:
continue

Copilot AI Apr 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Python block can still raise (e.g., permission errors on parent/scopes/peers directories, transient races during listdir/open), which will make airc peers exit non-zero under set -e. Wrap directory walks (os.listdir) and file reads in try/except and skip problematic scopes/files so a single unreadable sibling scope can’t break the whole peers listing.

Copilot uses AI. Check for mistakes.
joelteply added a commit that referenced this pull request Apr 27, 2026
Pre-fix: cmd_whois only consulted the primary scope's host /
local-peer-file / cross-peer-via-host SSH lookups. A peer in the
#general sidecar but not the primary project room returned "no
record" — even after airc peers (post-#124) already showed them
across rooms. JOIN events in the sidecar emitted names the IRC-style
mental model said should be reachable, but whois 404'd.

Post-fix: cmd_whois walks sibling scopes (.airc + .airc.<room>) using
the same prefix-detection pattern cmd_peers uses. Each scope tries
its own host / local-peer-file / cross-peer-via-host lookups; first
hit wins. Per-scope logic factored into _whois_in_scope so primary
and siblings share one code path.

The cross-peer-via-host SSH path uses the SCOPE's identity/ssh_key
(not the primary scope's), so a sidecar joiner can authenticate
to its sidecar host even when the primary scope is paired with a
different host on different keys.

Test (scenario_whois_cross_scope) covers:
  - sidecar local peer found from primary scope (the vhsm bug)
  - sidecar HOST resolved from primary scope
  - primary host still resolves (no regression)
  - unknown peer still 404s after exhausting all scopes
  - symmetric: sidecar→primary lookup also works

No wire-format change; backward compatible with single-scope users.
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.

2 participants