Repository navigation
fix(rename): chain-repair skips when host is ambiguous (closes #180) - #256
Conversation
continuum-b741 hit this on canary dee3b6c: round-trip rename leaves a phantom peer record at sibling-host on same gh user. ## Root cause \`_find_peer_by_host\` returned the FIRST match. Same-machine peers share the \`host\` field (e.g. multiple Claudes on Joel's box all have [email protected]). When chain-repair ran with that host and the listdir returned bob first, alice's rename event renamed bob's file to alice, corrupting bob's identity record. ## Fix \`_find_peer_by_host\` collects ALL matches; returns the name only when there's EXACTLY ONE. Zero or two-plus matches → return None → chain-repair silently skips. No phantom. Cost: chain-repair is slightly less aggressive in healing genuinely-broken chains across multi-peer-on-one-host setups, but the alternative (corrupting an unrelated peer's identity) is worse. Verification: tabs 19/0; module syntax-checks.
There was a problem hiding this comment.
Pull request overview
This PR adjusts rename “chain-repair” behavior in monitor_formatter to avoid corrupting unrelated peer records when multiple peer JSONs share the same host value (issue #180).
Changes:
- Update
_find_peer_by_host()to return a match only when exactly one peer record matches the given host. - Add explanatory docstring/comments describing the ambiguous-host failure mode and the skip behavior.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| d = json.load(open(os.path.join(peers_dir, entry))) | ||
| except Exception: | ||
| continue | ||
| if d.get("host") == host: | ||
| return d.get("name") or entry[:-5] | ||
| matches.append(d.get("name") or entry[:-5]) |
There was a problem hiding this comment.
Inside this directory scan you now potentially open many peer JSON files; using json.load(open(...)) leaves file descriptors to be GC-closed and can exhaust FDs on large peer dirs. Use a context manager (with open(...) as f: json.load(f)) so each file is closed promptly, especially now that you no longer return early on first match.
| if d.get("host") == host: | ||
| return d.get("name") or entry[:-5] | ||
| matches.append(d.get("name") or entry[:-5]) | ||
| if len(matches) == 1: | ||
| return matches[0] | ||
| # 0 matches → no record to chain-repair against (probably the rename |
There was a problem hiding this comment.
This collects all matches even though the only outcomes are 0, 1, or 2+ matches. Consider short-circuiting once you find a second match (return None immediately) to avoid unnecessary file I/O when the host is ambiguous.
Same-machine peers share host field; chain-repair returning first-match corrupted an unrelated peer's record. Now returns None unless exactly one record matches the host.