Repository navigation
feat: multi-address redundancy (Tailscale truly optional) - #109
Conversation
…onal)
Real fix for "agents on the same machine can't talk to each other when
Tailscale is logged out" — the underlying cause is that the gist
envelope advertised exactly one address (whichever scope get_host
returned), and joiners had to use that address even when a closer
path existed.
The gist envelope now carries:
host.machine_id — stable per-machine UUID
host.addresses[] — array of {scope, addr, port [, subnet]}
covering localhost, lan, and tailscale (when signed in)
(host.address/host.port retained for backward compat)
The heartbeat loop refreshes host.addresses every tick so network
changes mid-session (laptop moves to a new wifi, Tailscale comes
up/down/re-auths, interface flapping) propagate to peers within one
heartbeat interval.
Joiner side: if host.addresses is present, peer_pick_address selects
the cheapest reachable scope:
1. host.machine_id == my host_machine_id → localhost
2. host.lan entry with subnet covering my LAN IP → that lan
3. host.tailscale (if I have a usable tailscale myself)
4. fallback to first entry of any kind
Verified on a real Mac with two tabs on the same machine: tab B
discovered tab A's gist, picked 127.0.0.1 via machine_id match, and
SSH-paired over localhost. No Tailscale dependency in the wire path.
Bidirectional msg confirmed.
Functions added at the airc-lib boundary (per REFCONTRACT.md):
host_machine_id — stable per-machine id (mac/linux/win)
host_address_set — enumerate localhost/lan/tailscale, signed-in only
host_addresses_json — serialize for gist envelope
peer_pick_address — joiner selects best scope for this peer
Backward compat: pre-multi-address gists still work — joiners detect
the absence of host.addresses and fall back to the legacy single-address
parse from the invite string.
PS parity (airc.ps1) is the next commit.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
There was a problem hiding this comment.
Pull request overview
Adds multi-address redundancy to room gists so joiners can select the cheapest reachable address (same-machine localhost → same-LAN → Tailscale), making Tailscale optional for same-host/same-LAN connections.
Changes:
- Introduces
host_machine_id,host_address_set,host_addresses_json, andpeer_pick_addresshelpers to publish and select endpoints. - Extends room gist/heartbeat payloads with
host.machine_idandhost.addresses[]. - Updates join flow to override the invite’s host:port using
peer_pick_addresswhen the multi-address fields are present.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # 5. first entry of any kind (gives the joiner SOMETHING to try) | ||
| peer_pick_address() { | ||
| local addresses_json="${1:-}" host_machine="${2:-}" | ||
| [ -z "$addresses_json" ] || [ "$addresses_json" = "null" ] && return 0 |
There was a problem hiding this comment.
In peer_pick_address, the early-exit guard uses cmd1 || cmd2 && return 0, which in bash evaluates as (cmd1) || (cmd2 && return 0). When addresses_json is empty, [ -z "$addresses_json" ] succeeds and the return 0 does NOT run, so the function continues and later runs jq against an empty string. Split this into an explicit if (or add parentheses) so the function returns when addresses_json is empty OR null.
| [ -z "$addresses_json" ] || [ "$addresses_json" = "null" ] && return 0 | |
| if [ -z "$addresses_json" ] || [ "$addresses_json" = "null" ]; then | |
| return 0 | |
| fi |
| # LAN: enumerate interfaces, pick non-loopback IPv4s. macOS + Linux | ||
| # converge on `ifconfig`; we use that and reject loopback + CGNAT. | ||
| local lan_ips | ||
| lan_ips=$(ifconfig 2>/dev/null \ | ||
| | awk '/inet /{print $2}' \ | ||
| | grep -vE '^(127\.|169\.254\.)' \ | ||
| | grep -vE '^100\.(6[4-9]|[7-9][0-9]|1[01][0-9]|12[0-7])\.' ) |
There was a problem hiding this comment.
host_address_set relies on ifconfig to enumerate LAN IPv4s. On many modern Linux distros / minimal images (and some WSL setups), ifconfig (net-tools) is not installed by default, which will silently drop all lan|... entries and prevent the intended same-LAN optimization. Consider adding a fallback path when ifconfig is missing (e.g., ip -4 addr), or reusing the existing Python UDP-socket technique used in get_host (airc:273-306) to keep LAN detection dependency-free.
| # LAN: enumerate interfaces, pick non-loopback IPv4s. macOS + Linux | |
| # converge on `ifconfig`; we use that and reject loopback + CGNAT. | |
| local lan_ips | |
| lan_ips=$(ifconfig 2>/dev/null \ | |
| | awk '/inet /{print $2}' \ | |
| | grep -vE '^(127\.|169\.254\.)' \ | |
| | grep -vE '^100\.(6[4-9]|[7-9][0-9]|1[01][0-9]|12[0-7])\.' ) | |
| # LAN: enumerate interfaces, pick non-loopback IPv4s. Prefer `ifconfig` | |
| # when present (macOS, older Linux), but fall back to `ip -4 addr` on | |
| # modern/minimal Linux and WSL where net-tools may be absent. | |
| local lan_ips="" | |
| if command -v ifconfig >/dev/null 2>&1; then | |
| lan_ips=$(ifconfig 2>/dev/null | awk '/inet /{print $2}') | |
| elif command -v ip >/dev/null 2>&1; then | |
| lan_ips=$(ip -4 addr 2>/dev/null | awk '/inet /{sub(/\/.*/, "", $2); print $2}') | |
| fi | |
| lan_ips=$(printf '%s\n' "$lan_ips" \ | |
| | grep -vE '^(127\.|169\.254\.)' \ | |
| | grep -vE '^100\.(6[4-9]|[7-9][0-9]|1[01][0-9]|12[0-7])\.' \ | |
| || true) |
| local my_lan_ips | ||
| my_lan_ips=$(host_address_set 0 | awk -F'|' '$1=="lan"{print $2}') | ||
| if [ -n "$my_lan_ips" ]; then |
There was a problem hiding this comment.
peer_pick_address calls host_address_set 0 multiple times (for my_lan_ips and again for i_have_ts). Since host_address_set can invoke ifconfig and tailscale status, this duplicates relatively expensive work and can add noticeable latency to join. Consider calling host_address_set 0 once, caching its output, and deriving both LAN IPs and tailscale presence from that cached result.
| # Multi-address override: if the gist envelope carried host.addresses[] | ||
| # and host.machine_id, use peer_pick_address to choose the cheapest | ||
| # reachable scope (same-machine localhost > same-LAN > tailscale). | ||
| # This is what makes Tailscale truly optional — same-machine and | ||
| # same-LAN peers connect via 127.0.0.1 / LAN IP regardless of the | ||
| # invite string's host:port (which historically advertised one IP). | ||
| if [ -n "$_resolved_addresses_json" ] && [ "$_resolved_addresses_json" != "null" ]; then | ||
| local _picked; _picked=$(peer_pick_address "$_resolved_addresses_json" "$_resolved_host_machine_id") |
There was a problem hiding this comment.
This introduces a new connection path (overriding ssh_target/peer_port based on host.addresses[] + host.machine_id). The repo has an extensive test/integration.sh, but there doesn't appear to be coverage ensuring the multi-address picker is exercised (and that it prefers localhost on same-machine). Consider adding an integration scenario that stubs gh api (via a fake gh earlier on PATH) to return a room envelope containing host.addresses + host.machine_id, then asserts the joiner log includes the "Multi-address pick" line and successfully connects via 127.0.0.1.
test: add scenario_bounce + scenario_two_tab_localhost Two new integration scenarios catching real user-encountered bugs the existing tests missed: scenario_bounce — host → teardown → host. Verifies the teardown gist cleanup (#110) actually fires, AND that round-2 publishes a fresh gist without leaving an orphan from round 1. Catches the bounce-orphan class. Pre-#110: this would have failed with two competing gists. scenario_two_tab_localhost — A hosts, B joins via discovery, asserts B picked 127.0.0.1 (multi-address machine_id match) NOT the LAN or Tailscale IP. Catches multi-address regression. Pre-#109: this would have failed because B would have used whatever single address A advertised in the legacy host.address field. Both require gh + auth, skipped if unavailable. Both run quick (~10-15s each) since they use real gh round-trips but small data. Joel: "how are integration tests not actually able to test my scenario?" — they couldn't, because the existing scenarios all use --no-gist (sidesteps the actual gh substrate code path) and only spawn one joiner (no concurrent / lifecycle coverage). These two scenarios use the real gh path and exercise the lifecycle edges. Windows parity: still NONE. test/integration.sh is bash-only. A matching test/integration.ps1 with PS-native commands is needed for Windows users to verify their install. Tracked but not in this PR. Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Gist envelope now carries machine_id + addresses[] (localhost+lan+tailscale). Joiner picks cheapest reachable scope via peer_pick_address. Same-machine peers connect via 127.0.0.1 with NO Tailscale dependency. Verified e2e with two real tabs on this Mac.