Skip to content

fix(self-heal): don't nuke gist when picker found no reachable scope (Bug C from #397) - #398

Merged
joelteply merged 1 commit into
canaryfrom
fix/self-heal-guard-no-nuke-when-picker-empty
May 2, 2026
Merged

joelteply merged 1 commit into
canaryfrom
fix/self-heal-guard-no-nuke-when-picker-empty

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Summary

Defense-in-depth follow-up to #397. The address-picker fix in #397 prevents the most common shape (joiner picks unreachable Tailscale on a Mac without tailnet), but a second path remained:

  1. Picker correctly returns empty (no reachable scope) → ssh_target stays at the invite-string default
  2. That fallback dial fails the same way → no route
  3. Self-heal triggers → gh deletes the gist for every peer on the substrate, not just the one with a network mismatch

Bug observed live 2026-05-02: a Mac without Tailscale joining a Windows host whose only non-localhost entry was Tailscale fell through to the invite-string ssh_target, TCP timed out, self-heal nuked the gist that 4 other peers were happily using.

Fix

Track picker outcome in _addr_picker_state:

  • no_addrs — no addresses[] in gist → tried invite-string only → self-heal allowed
  • picked — picker selected a believed-reachable address that THEN failed → self-heal allowed
  • no_match — picker found no reachable scope → self-heal SKIPPED (other peers can still reach this host)

When skipped, surface explicit message so users know it's network topology, not host-down: "Host of #room published no scope this peer can reach. Skipping self-heal (gist preserved for peers who CAN reach the host). Direct pair unavailable; gh-bearer broadcasts still work via gist."

Cumulative effect with #397

Before After #397 After this PR
Joiner without tailscale picks 100.x YES (via fallback) NO (gated) NO
no_match falls through to invite-string YES YES YES
TCP fail on invite-string nukes gist YES YES NO
Substrate damageable by one peer's mismatch YES partial NO

Test plan

  • CI green (clean-install matrix)
  • bash syntax clean (verified locally)
  • Future: multi-network integration scenario where joiner-A has scope X only, host has scope Y only — verify gist preserved + explicit "no scope this peer can reach" message

Out of scope

  • Retry-N-times-before-self-heal: orthogonal future work. This PR addresses the "1 timeout = nuke" failure for the no-match-fallthrough path; the picked-address path still nukes on first failure (which is more often correct since picker said it should be reachable).

🤖 Generated with Claude Code

… scope

Bug uncovered by the same 2026-05-02 incident as #397. The address-picker
fix in #397 prevents the most common shape (joiner picks unreachable
tailscale on a Mac without tailnet), but a second path remained: when
peer_pick_address returns empty, the caller falls through to the
invite-string ssh_target. That fallback dial fails the same way —
no route — and triggers the destructive self-heal-as-new-host chain
that DELETES the host's gist for every other peer on the substrate.

Concrete failure shape (now defended against):
  - Mac without Tailscale joins room hosted by Windows host whose
    addresses[] is [localhost, lan, tailscale].
  - Picker correctly excludes localhost (other machine) and tailscale
    (no local tailnet), and the LAN scope's subnet doesn't match (Mac
    is on a different /24). Result: empty pick.
  - ssh_target stays at the invite-string default (Windows host's
    advertised primary, often tailscale on Windows pre-#397 hosts).
  - TCP handshake fails.
  - Pre-fix: self-heal triggers, gh deletes the gist, Mac re-execs as
    new host. Three other peers still happily talking to the Windows
    host suddenly orphaned because the gist is gone.

Fix: track the picker outcome in `_addr_picker_state` ("no_addrs",
"picked", "no_match") and gate the self-heal block on it. Self-heal
only fires when:
  1. Original conditions: room context + gh CLI + handshake failed
  2. NEW: picker either succeeded OR there were no addresses[] to
     pick from (legacy gists / no multi-address protocol). Picker-
     ran-but-found-no-match means THIS peer's network mismatch, not
     host-down — nuking would damage other peers.

When skipped: surface explicit message ("published no scope this peer
can reach. Skipping self-heal. Direct pair unavailable; gh-bearer
broadcasts still work via gist") so users debugging "why didn't I
auto-pair" know it's topology, not host failure. Per the global
"evidence is for the debugger" rule.

## Tests

- bash syntax clean.
- Unit tests for peer_pick_address reachability (#397 already added
  test_gistparse.py covering the upstream picker behavior). The
  guard here is a control-flow gate; the existing integration suite's
  scenario_two_tab_localhost exercises the picked-address path. The
  no_match guard would only manifest in cross-machine + scope-
  mismatch scenarios that integration tests don't simulate today —
  worth adding once the test rig has multi-network simulation, but
  out of scope here.

## Cumulative effect with #397

#397 (Bug A): Joiner picker excludes scopes it can't route. Most
common cross-machine bad-pair shapes never reach the self-heal path.

This PR (Bug C): When picker correctly finds nothing reachable, the
fall-through to invite-string ssh_target no longer nukes the gist
on TCP failure. Defense in depth.

#397 + this = "no peer can take down the substrate for others by
joining + failing to reach the host."

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings May 2, 2026 04:02
@joelteply
joelteply merged commit edbd8a1 into canary May 2, 2026
8 checks passed
@joelteply
joelteply deleted the fix/self-heal-guard-no-nuke-when-picker-empty branch May 2, 2026 04:04

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

This PR hardens cmd_connect’s join/self-heal flow so a joiner that cannot reach any advertised host scope does not delete the shared room gist for everyone else. It fits into the room-discovery and direct-pairing logic by refining when stale-host takeover is allowed after a failed handshake.

Changes:

  • Track address-picker outcomes with _addr_picker_state to distinguish “no addresses published”, “picked a reachable-looking address”, and “no reachable scope matched”.
  • Gate the SSH-failure self-heal path so it does not delete the gist when the picker explicitly found no reachable scope for this peer.
  • Emit a clearer user-facing message for the no_match case, explaining that direct pairing failed due to topology mismatch while gist-based broadcasts still work.

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

Comment on lines 1085 to +1087
if [ -n "$resolved_room_name" ] && [ -n "$_resolved_gist_id" ] \
&& command -v gh >/dev/null 2>&1; then
&& command -v gh >/dev/null 2>&1 \
&& [ "$_addr_picker_state" != "no_match" ]; then
Comment on lines +1099 to +1110
elif [ "$_addr_picker_state" = "no_match" ]; then
# Picker found no scope this peer can reach. Surface the situation
# but do NOT nuke the gist. The host may be perfectly reachable
# for peers on the other matching scope (e.g. peers on the same
# tailnet when WE lack tailscale). Per the global "evidence is
# for the debugger, not the trash" rule — print explicit reason
# so users debugging "why didn't I auto-pair" know it's a network
# topology mismatch rather than a host-down event.
echo "" >&2
echo " ⚠ Host of #${resolved_room_name} published no scope this peer can reach." >&2
echo " Skipping self-heal (gist preserved for peers who CAN reach the host)." >&2
echo " Direct pair unavailable; gh-bearer broadcasts still work via gist." >&2
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