Repository navigation
feat(airc_core): host pair-handshake accept_one → airc_core.handshake (#152 Phase 1) - #172
Conversation
….accept_one (#152 Phase 1) Symmetric counterpart of PR #170 (joiner send) — the HOST'S accept- and-respond heredoc, biggest remaining bash-into-python heredoc with substituted variables. 127 lines of Python with EIGHT bash variable substitutions migrated to a clean Python module. ## Substitutions previously inline - $host_port — the listen port (numeric, but bare-substituted) - $PEERS_DIR — joiner's peer file path - $(timestamp) — bash command-substitution INTO python (highest risk) - $IDENTITY_DIR — host's ssh_key.pub source - $CONFIG — host's identity load path - $name — host's identity name - $reminder_interval — numeric reminder interval - $AIRC_WRITE_DIR — host's airc_home (sent in response) - $MESSAGES — system-event log path Each was a per-callsite silent-fail vector. Continuum traced the write-side variant (#165) earlier today. ## Migration `airc_core.handshake.accept_one()` reads all from env vars (HOST_PORT, PEERS_DIR, IDENTITY_DIR, CONFIG, HOST_NAME, REMINDER_INTERVAL, AIRC_WRITE_DIR, MESSAGES). Bash callsite shrinks from 127 lines (heredoc body) to a 9-line env-var-pass + module call. Same logic preserved verbatim — accept-with-timeout, parent-death detection (`os.getppid() == 1`), authorize joiner SSH key, write peer record (with stable-host stale cleanup), build response, write peer-joined system event. The outer `while true; do ... done &` bash loop unchanged. ## Impact - airc bash: 5647 → 5529 (-118 lines) - Cumulative today (Phase 1): ~370 lines moved out of bash to testable Python modules. ## Test posture (Mac, 89 assertions / 9 scenarios) - tabs: 19/19 (two-tab pair on localhost — exercises full accept loop end-to-end) - scope: 5/5 (multi-cwd pairing across scopes) - identity: 19/19 (identity exchange at handshake) - whois: 5/5 - kick: 12/12 (multi-peer, multiple accepts) - part_persists: 8/8 - list: 4/4 - general_sidecar_default: 12/12 - events: 5/5 (peer-joined system event emission) ## Phase 1 progress - ✓ iso_to_epoch, config CRUD, handshake parse, _whois cleanup, joiner send, monitor_formatter (PRs #166-#171) - ✓ host accept_one (this PR) - next: smaller cleanups (lan_ip resolver, identity/peer config writes, remaining gist-envelope bash heredocs)
There was a problem hiding this comment.
Pull request overview
Migrates the host-side “pair-handshake accept one joiner” logic from an inline bash python -c heredoc into airc_core.handshake.accept_one, reducing bash-variable substitution risk and centralizing handshake behavior in a Python module.
Changes:
- Added
accept_one()+ CLI subcommand wiring tolib/airc_core/handshake.pyto implement host-side accept/process/respond + peer-joined event emission. - Replaced the large inline heredoc in
aircwith an env-var pass-through and"$AIRC_PYTHON" -m airc_core.handshake accept_oneinvocation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| lib/airc_core/handshake.py | Adds host-side accept_one() implementation and exposes it via module CLI. |
| airc | Swaps the host accept-loop heredoc for a call into airc_core.handshake accept_one with env configuration. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| event. Exits 0 on success, 0 on parent-death-timeout. | ||
|
|
There was a problem hiding this comment.
The accept_one() docstring says it “Exits 0 on success, 0 on parent-death-timeout”, but the CLI wrapper returns 1 on exceptions and accept_one can also fail with other non-zero exit paths (e.g., ValueError parsing env vars, OSError binding). Please update the docstring to match the actual exit-code contract (or adjust error handling so it truly only returns 0 in the documented cases).
| event. Exits 0 on success, 0 on parent-death-timeout. | |
| event. | |
| Returns 0 on success and on parent-death-timeout. Other failures | |
| are not normalized here: invalid env values (for example while | |
| parsing HOST_PORT or REMINDER_INTERVAL), socket/bind errors, and | |
| other runtime exceptions may still propagate and be reported by | |
| the CLI wrapper as a non-zero exit status. |
| host_port = int(os.environ.get("HOST_PORT", "7547")) | ||
| peers_dir = os.path.expanduser(os.environ.get("PEERS_DIR", "")) | ||
| identity_dir = os.path.expanduser(os.environ.get("IDENTITY_DIR", "")) | ||
| config_path = os.environ.get("CONFIG", "") | ||
| host_name = os.environ.get("HOST_NAME", "") | ||
| reminder_interval = int(os.environ.get("REMINDER_INTERVAL", "300")) | ||
| airc_write_dir = os.environ.get("AIRC_WRITE_DIR", "") | ||
| messages_path = os.environ.get("MESSAGES", "") |
There was a problem hiding this comment.
accept_one() treats several path env vars (PEERS_DIR, IDENTITY_DIR, CONFIG, AIRC_WRITE_DIR, MESSAGES) as optional by defaulting them to "" and continuing. If any are accidentally unset, the handshake can fail mid-flight and the outer bash loop will silently retry forever (2>/dev/null || true), which is hard to diagnose. Consider validating the required env vars up front (similar to lib/airc_core/config.py’s CLI) and emitting a clear stderr error + nonzero exit before starting the listener.
| data = b"" | ||
| while True: | ||
| chunk = conn.recv(4096) | ||
| if not chunk: | ||
| break | ||
| data += chunk |
There was a problem hiding this comment.
The receive loop builds data via repeated data += chunk, which is quadratic in the worst case. Even though the handshake payload is usually small, switching to a bytearray() (or accumulating chunks in a list and b"".join(...)) avoids pathological behavior if a peer sends a large line.
| data = b"" | |
| while True: | |
| chunk = conn.recv(4096) | |
| if not chunk: | |
| break | |
| data += chunk | |
| data = bytearray() | |
| while True: | |
| chunk = conn.recv(4096) | |
| if not chunk: | |
| break | |
| data.extend(chunk) |
Symmetric counterpart of PR #170 (joiner send). 127-line bash-into-python heredoc with EIGHT bash variable substitutions migrated to a clean Python module.
Substitutions eliminated
`$host_port`, `$PEERS_DIR`, `$(timestamp)`, `$IDENTITY_DIR`, `$CONFIG`, `$name`, `$reminder_interval`, `$AIRC_WRITE_DIR`, `$MESSAGES`
Each was a per-callsite silent-fail vector. Now: env vars + argv. Python source is fixed bytes.
Impact
Test posture (Mac)
89 assertions / 9 scenarios green: tabs (19), scope (5), identity (19), whois (5), kick (12), part_persists (8), list (4), general_sidecar_default (12), events (5). All scenarios that exercise pair-handshake confirm identical behavior.
Phase 1 cumulative
7 architecture migrations merged today + foundation. Remaining heredocs trend smaller (lan_ip resolver, identity writes, gist envelope bits) — diminishing-returns territory but each removes one more silent-fail vector.