fix(graph_trace_helper): no self-loop for a one-hop neighbor sharing our prefix; survive bad distances

Codex re-review follow-up. The one-hop shortcut wrote dd->dd both ways when the neighbor shares the bot's prefix; it now skips the update, as the multi-hop path does. A distance error (a stored infinite latitude) no longer drops the trace's edges; the edge is recorded without a distance, as before the rewrite.
This commit is contained in:
agessaman committed 2026-10-02 18:54:40 -07:00
1 parent 568b296358
commit a8f4eec3ec
2 files changed
+26 -3

No files matched your search

+10 -3
View File
@@ -100,6 +100,10 @@ def update_mesh_graph_from_trace_data(
if is_immediate_neighbor:
neighbor_prefix = path_hashes[0].lower()
if neighbor_prefix == bot_prefix:
# The graph, keyed by prefix, can't tell this neighbor from the bot
bot.logger.debug(f"Mesh graph: Neighbor {neighbor_prefix} shares the bot's prefix, skipping trace update")
return
neighbor_key = None
try:
count_query = f"""
@@ -208,9 +212,12 @@ def update_mesh_graph_from_trace_data(
geographic_distance = None
from_location, to_location = locations[i], locations[i + 1]
if from_location and to_location:
geographic_distance = calculate_distance(
from_location[0], from_location[1], to_location[0], to_location[1],
)
try:
geographic_distance = calculate_distance(
from_location[0], from_location[1], to_location[0], to_location[1],
)
except Exception as e:
bot.logger.debug(f"Could not calculate distance for trace edge {from_node}->{to_node}: {e}")
mesh_graph.add_edge(
from_prefix=from_node,
to_prefix=to_node,
+16
View File
@@ -377,3 +377,19 @@ class TestOwnTraceChain:
assert [(c["from_prefix"], c["to_prefix"]) for c in calls] == [("dd", "aa"), ("aa", "dd")]
assert calls[0]["from_public_key"] == "dd" * 32
assert calls[1]["to_public_key"] is None # the hop "dd", not the bot
def test_a_neighbor_sharing_the_bots_prefix_adds_no_self_loop(self):
bot = _make_bot(bot_prefix="dd")
update_mesh_graph_from_trace_data(bot, ["dd"], {}, is_our_trace=True)
bot.mesh_graph.add_edge.assert_not_called()
def test_a_bad_stored_location_still_records_the_edges(self, monkeypatch):
import modules.utils as utils
bot = _make_bot(bot_prefix="dd")
monkeypatch.setattr(utils, "_get_node_location_from_db", lambda *a, **k: ((float("inf"), 0.0), None))
monkeypatch.setattr(utils, "calculate_distance", Mock(side_effect=ValueError("math domain error")))
update_mesh_graph_from_trace_data(bot, ["aa", "bb"], {})
assert bot.mesh_graph.add_edge.call_count == 2
assert all(c.kwargs["geographic_distance"] is None for c in bot.mesh_graph.add_edge.call_args_list)