Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring
TypeScript diagramhomelabhomelab-infrastructure-observabilitynetworkproxmox-veself-hostedzigbeezwavezwave-js
fix(scan): never collapse a second approved row into another
Pouzore1e7b5089b16
diff --git a/backend/app/services/scanner.py b/backend/app/services/scanner.pyindex faddc38..d80bee2 100644--- a/backend/app/services/scanner.py+++ b/backend/app/services/scanner.py@@ -604,6 +604,27 @@ def _group_by_shared_ip(rows: list[InventoryDevice]) -> list[list[InventoryDevicreturn list(groups.values())+def _collapse_targets(+ group: list[InventoryDevice],+) -> tuple[InventoryDevice, list[InventoryDevice]]:+ """The row a duplicate group folds into, and the rows safe to delete for it.++ An ``approved`` row owns the canvas-link semantics — nodes and rack mounts+ point at it under ``ON DELETE SET NULL`` — so deleting one silently unlinks+ whatever drew it. With one approved row that row is the merge target and the+ rest collapse into it. With *two*, the group is two canvas-linked devices+ laying claim to one address, and a background scan is not the place to+ decide which of them loses its links: every approved row stands, and only+ the pending ones are collapsed. The pair then stays visible in the inventory+ for the user to sort out.+ """+ approved = [row for row in group if row.status == "approved"]+ keep = approved[0] if approved else group[0]+ if len(approved) > 1:+ return keep, [row for row in group if row.status != "approved"]+ return keep, [row for row in group if row is not keep]++async def _dedupe_pending_by_ip(db: AsyncSession) -> int:"""Collapse duplicate non-hidden inventory rows that share an IP into one.@@ -622,11 +643,10 @@ async def _dedupe_pending_by_ip(db: AsyncSession) -> int:for group in _group_by_shared_ip(list(rows)):if len(group) < 2:continue- keep = next((r for r in group if r.status == "approved"), group[0])- for dup in group:- if dup is not keep:- await db.delete(dup)- deleted += 1+ _, dups = _collapse_targets(group)+ for dup in dups:+ await db.delete(dup)+ deleted += 1if deleted:await db.commit()return deleted@@ -697,11 +717,11 @@ async def process_host(if existing_rows:# Prefer an approved row (it owns the canvas link semantics),# otherwise the oldest. Collapse any leftover duplicates created- # by earlier scans.- keep = next((r for r in existing_rows if r.status == "approved"), existing_rows[0])- for dup in existing_rows:- if dup is not keep:- await db.delete(dup)+ # by earlier scans — except a second approved row, which keeps its+ # own canvas links (see `_collapse_targets`).+ keep, dups = _collapse_targets(existing_rows)+ for dup in dups:+ await db.delete(dup)keep.ip = keep.ip or ip # fill an IP a Proxmox import lackedkeep.mac = norm_mac or keep.mackeep.hostname = host.get("hostname") or keep.hostnamediff --git a/backend/tests/test_scanner.py b/backend/tests/test_scanner.pyindex 6190836..ccf2202 100644--- a/backend/tests/test_scanner.py+++ b/backend/tests/test_scanner.py@@ -1614,3 +1614,70 @@ def test_group_by_shared_ip_is_transitive():groups = {frozenset(r.id for r in group) for group in _group_by_shared_ip(rows)}assert groups == {frozenset({"a", "b", "bridge"}), frozenset({"other"})}+++async def test_dedupe_never_deletes_a_second_approved_row(mem_db):+ """Two approved rows sharing an address both keep their canvas links.++ An approved row is what nodes and rack mounts point at, so collapsing one+ into the other would silently unlink whatever drew it. The scan collapses+ the pending row and leaves the pair for the user to resolve.+ """+ from app.services.scanner import _dedupe_pending_by_ip++ async with mem_db() as session:+ session.add(InventoryDevice(+ id="a", ip="10.0.0.1", status="approved",+ discovered_at=datetime(2026, 1, 1, tzinfo=timezone.utc),+ ))+ session.add(InventoryDevice(+ id="b", ip="10.0.0.1, 10.0.0.2", status="approved",+ discovered_at=datetime(2026, 2, 1, tzinfo=timezone.utc),+ ))+ session.add(InventoryDevice(+ id="pending", ip="10.0.0.2", status="pending",+ discovered_at=datetime(2026, 3, 1, tzinfo=timezone.utc),+ ))+ await session.commit()++ async with mem_db() as session:+ deleted = await _dedupe_pending_by_ip(session)++ async with mem_db() as session:+ devices = (await session.execute(sa_select(InventoryDevice))).scalars().all()++ assert deleted == 1+ assert sorted(d.id for d in devices) == ["a", "b"]+++async def test_process_host_never_deletes_a_second_approved_row(mem_db):+ """The same guard on the scan path: the merge target refreshes, no row goes."""+ from app.services.scanner import DeepScanOptions, process_host++ async with mem_db() as session:+ session.add(InventoryDevice(+ id="a", ip="10.0.0.1", status="approved",+ discovered_at=datetime(2026, 1, 1, tzinfo=timezone.utc),+ ))+ session.add(InventoryDevice(+ id="b", ip="10.0.0.1, 10.0.0.2", status="approved",+ discovered_at=datetime(2026, 2, 1, tzinfo=timezone.utc),+ ))+ await session.commit()++ async with mem_db() as session:+ outcome = await process_host(+ session, _host("10.0.0.1", hostname="claimed.lan"),+ hidden_ips=set(), deep_scan=DeepScanOptions(),+ )++ async with mem_db() as session:+ devices = (await session.execute(sa_select(InventoryDevice))).scalars().all()+ oldest = await session.get(InventoryDevice, "a")++ assert outcome == "updated"+ assert sorted(d.id for d in devices) == ["a", "b"]+ # The oldest approved row is the merge target.+ assert oldest is not None and oldest.hostname == "claimed.lan"