Repository navigation
feat(airc_core): collapse _whois_in_scope + resolve_name + cmd_rename heredocs (#152 Phase 1 cleanup) - #169
Conversation
… heredocs into get_config_val[_in] (#152 Phase 1) Cleanup pass following PR #167/#168. Eight more inline `python -c` heredocs collapsed into one-line calls now that airc_core.config handles the read pattern. ## Sites migrated 1. **resolve_name** (line 1228) — was duplicating the get_config_val logic inline. Now calls get_config_val. 2. **cmd_rename** (line 3369) — same. 3. **_whois_in_scope** (six sites) — host_name, host_identity, host_target (×2), host_airc_home, peer-file's identity, peer-file's host. All collapsed to get_config_val_in or airc_core.handshake get_field. ## New: get_config_val_in Like get_config_val but reads from an arbitrary config.json path. Used by _whois_in_scope's cross-scope walk (#134) which inspects sibling scope state without changing $CONFIG. Same module, same CLI; just different env var per call. ## airc_core.config: dict round-trip Extended `get` to JSON-encode dict/list values (matches handshake.get_field shape). Lets _whois_in_scope read host_identity + peer identity blobs as JSON-encoded strings that callers can re-parse. ## Test posture - whois: 5/5 - whois_cross_scope: 6/6 ← hottest path through _whois_in_scope - identity: 19/19 - kick: 12/12 - part_persists: 8/8 - list: 4/4 - general_sidecar_default: 12/12 ## Code reduction ~70 lines of inline python heredoc → ~10 lines of bash function calls. Each removed heredoc was a separate silent-fail vector (bash-substituted env var into python source code). ## Phase 1 progress - ✓ iso_to_epoch (Phase 0a) - ✓ config CRUD core (PR #167) - ✓ handshake response parse (PR #168) - ✓ _whois_in_scope + resolve_name + cmd_rename cleanup (this PR) - next: handshake/gist envelope BUILD sites, identity show/set, monitor_formatter
There was a problem hiding this comment.
Pull request overview
Phase 1 cleanup continuing the “Python truth-layer” migration by replacing additional inline python -c heredocs with stable module CLIs/helpers, and simplifying cross-scope whois lookups.
Changes:
- Extend
airc_core.config getto JSON-encode dict/list values (and treat empty-string values as defaultable). - Add
get_config_val_in <path> <key> [default]to read config-like JSON from an arbitrary file without mutating$CONFIG. - Refactor
resolve_name,cmd_rename, and_whois_in_scopeto useget_config_val(_in)andairc_core.handshake get_fieldinstead of inline Python.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/airc_core/config.py | Adjusts get() return semantics to round-trip nested objects as JSON strings for bash callers. |
| airc | Introduces get_config_val_in and migrates remaining heredoc reads in resolve_name, cmd_rename, and _whois_in_scope to the new helpers/modules. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| local remote_blob | ||
| remote_blob=$(IDENTITY_DIR="$scope/identity" relay_ssh "$host_target_addr" "cat $host_airc_home/peers/$target.json 2>/dev/null" 2>/dev/null || true) |
There was a problem hiding this comment.
The remote SSH command builds a path using $host_airc_home without remote-shell quoting. If host_airc_home ever contains spaces or shell metacharacters, the remote cat can fail (or execute unintended shell syntax). Consider quoting/escaping the remote path (e.g., build a remote_path and shell-escape it, or pass the path as an argument rather than interpolating it into the remote command string).
| local remote_blob | |
| remote_blob=$(IDENTITY_DIR="$scope/identity" relay_ssh "$host_target_addr" "cat $host_airc_home/peers/$target.json 2>/dev/null" 2>/dev/null || true) | |
| local remote_path="$host_airc_home/peers/$target.json" | |
| local remote_quoted_path; remote_quoted_path=$("$AIRC_PYTHON" -c 'import shlex, sys; print(shlex.quote(sys.argv[1]))' "$remote_path") | |
| local remote_blob | |
| remote_blob=$(IDENTITY_DIR="$scope/identity" relay_ssh "$host_target_addr" "cat $remote_quoted_path 2>/dev/null" 2>/dev/null || true) |
… Phase 1) (#170) The pair-handshake send was an inline `python -c` heredoc with FIVE bash-variable substitutions into the python source — name, host, ssh_pub, sign_pub, airc_home — plus the connect target as `('$peer_host_only', $peer_port)`. Any unusual character in any field could silently break python parsing. Specifically host_ssh_pub may contain a trailing newline (depending on how openssh-keygen wrote the .pub file); host_target may contain characters that need quoting; identity is a JSON-encoded blob of arbitrary user-set text. Each was a per-callsite silent-fail. ## Migration `airc_core.handshake.send(host, port)` reads all six fields from env: MY_NAME, MY_HOST, MY_SSH_PUB, MY_SIGN_PUB, MY_AIRC_HOME, MY_IDENTITY. Builds the JSON payload, opens TCP socket, sends, reads response, returns it as a string. Exceptions surface to stderr (matches the never-swallow-errors rule); bash captures stderr via `2>&1`. Bash callsite shrinks from 23 lines (inline python heredoc) to 8 lines (env-var pass + module call): response=$(MY_NAME="$my_name" \ MY_HOST="$(whoami)@$(get_host)" \ MY_SSH_PUB="$my_ssh_pub" \ MY_SIGN_PUB="$my_sign_pub" \ MY_AIRC_HOME="$AIRC_WRITE_DIR" \ MY_IDENTITY="$my_identity_json" \ "$AIRC_PYTHON" -m airc_core.handshake send "$peer_host_only" "$peer_port" 2>&1) || _pair_ok=0 ## Test posture Pair-handshake exercising scenarios all green: - tabs: 19/19 (two-tab pair on localhost — exercises send + receive) - identity: 19/19 (exchange identity at handshake) - whois: 5/5 (read identity from response) - kick: 12/12 (multi-peer pairing) - part_persists: 8/8 (sidecar + primary spawning) ## Phase 1 progress - ✓ iso_to_epoch (Phase 0a, PR #166) - ✓ config CRUD core (PR #167) - ✓ handshake response parse (PR #168) - ✓ _whois_in_scope cleanup (PR #169) - ✓ joiner handshake send (this PR) - next: host's response builder (line ~3236), self-heal/discovery heredocs, monitor_formatter
…-line migration, biggest single heredoc) (#171) feat(airc_core): monitor_formatter → airc_core.monitor_formatter (#152 Phase 1, biggest single migration) The biggest single heredoc in airc bash. ~250 lines of Python embedded in a `"$AIRC_PYTHON" -u -c '...'` block, complete with apostrophe- escape gymnastics like `caller'\''s` and `Joel'\''s` because bash single-quoting required them. Migrated to a proper Python module. ## Impact airc bash file: **5897 → 5647 lines** (−250 lines, ~4.2% reduction of the entire script). The migrated function had: - Inactivity watchdog (cross-platform: SIGALRM on POSIX, threading.Timer on Windows) - [rename] handler with chain-repair via stable host id - Ping/pong control message handling with auto-pong subprocess.Popen - Own-send filtering with mid-session rename support - Inbound mirror-to-local-log for joiners (avoids feedback loop on hosts) - Belt-and-suspenders error handling per line so one bad message doesn't kill the formatter All preserved verbatim — same logic, same stdin/stdout contract. The CLI shape: PEERS_DIR=<peers-dir> "$AIRC_PYTHON" -u -m airc_core.monitor_formatter <my_name> Bash function shrinks to 4 lines (was 268). ## Why a real .py file matters here The bash heredoc had: - `'\''` shell-escape sequences scattered through comments (caller's → caller'\''s) — readable Python source now restores natural apostrophes. - No editor syntax highlighting for python (it was inside a bash string). - No way to unit-test individual functions (_rename_files, _find_peer_by_host) without invoking the whole bash + airc stack. Now the module is a regular Python file: lints, syntax-highlights, unit-testable, importable from other airc_core modules if needed. ## Test posture 84 assertions pass across 8 scenarios touching monitor_formatter (every scenario that pairs + sends/receives): - tabs: 19/19 (two-tab message exchange) - identity: 19/19 (identity round-trip + rename) - whois: 5/5 (host_identity propagation) - part_persists: 8/8 (sidecar + primary monitor active) - list: 4/4 - general_sidecar_default: 12/12 - kick: 12/12 (multi-peer monitor traffic) - events: 5/5 (system-event formatting) ## Phase 1 progress - ✓ iso_to_epoch (Phase 0a, PR #166) - ✓ config CRUD core (PR #167) - ✓ handshake response parse (PR #168) - ✓ _whois_in_scope cleanup (PR #169) - ✓ joiner handshake send (PR #170) - ✓ monitor_formatter (this PR — biggest single migration) - next: host's pair-handshake handler heredocs, smaller cleanups
Cleanup pass after PR #167/#168. Eight more inline `python -c` heredocs collapsed.
Sites
New helper
`get_config_val_in [default]` — same module / same CLI as `get_config_val` but reads from an arbitrary path. Used by the cross-scope walk (#134) to inspect sibling-scope state without mutating `$CONFIG`.
airc_core.config extension
`get` now JSON-encodes dict/list values. Matches `handshake.get_field` so callers reading nested objects (host_identity, peer identity blob) get parseable strings back.
Test posture
Code reduction
~70 lines of inline python heredoc → ~10 lines of bash function calls. Each removed heredoc was a silent-fail vector class continuum-b69f traced today.