From fa84be9f3a3c1e458c15d35e7d710db7b7254825 Mon Sep 17 00:00:00 2001 From: ShuaixinHuang <1836656514@qq.com> Date: Thu, 19 Nov 2026 19:59:24 +0800 Subject: [PATCH] fix(native): skip missing endpoints in investigation traces (#12220) * fix(native): skip missing endpoints in investigation traces * fix(native): count only eligible investigation trace edges --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com> --- .../native/nativeInvestigationGraph.test.ts | 66 +++++++++++++++++++ src/domain/native/nativeInvestigationGraph.ts | 19 ++++-- 2 files changed, 78 insertions(+), 7 deletions(-) diff --git a/src/domain/native/nativeInvestigationGraph.test.ts b/src/domain/native/nativeInvestigationGraph.test.ts index f51a022b1a..a83a8ce366 100644 --- a/src/domain/native/nativeInvestigationGraph.test.ts +++ b/src/domain/native/nativeInvestigationGraph.test.ts @@ -142,6 +142,41 @@ describe("native investigation graph traces", () => { expect(trace).toMatchObject({ reached_depth: 1, truncated: false }); }); + it.each(["forward", "backward"] as const)( + "keeps tracing after an edge to a missing node in the %s direction", + (direction) => { + const forward = direction === "forward"; + const partial = nativeInvestigationGraphSchema.parse({ + ...graph, + truncated: true, + edges: [ + { + ...graph.edges[0], + id: "missing-endpoint", + from: forward ? "b" : "missing", + to: forward ? "missing" : "b", + }, + ...graph.edges, + ], + }); + const trace = traceNativeInvestigationGraph(partial, { + start: "b", + direction, + limits: { max_edges: forward ? 2 : 1 }, + }); + expect(trace.edges.map(({ id }) => id)).toEqual([forward ? "2" : "1"]); + expect(trace.nodes.map(({ id }) => id)).toEqual([ + "b", + forward ? "c" : "a", + ]); + expect(trace).toMatchObject({ + truncated: true, + reason: "edge_target_missing_from_graph", + reached_depth: 1, + }); + }, + ); + it("retains observed and inferred route edges plus unresolved dispatch", () => { const trace = traceNativeInvestigationGraph(graph, { start: "a" }); expect(trace.nodes.map(({ id }) => id)).toEqual(["a", "b", "c"]); @@ -211,6 +246,37 @@ describe("native investigation graph traces", () => { ); }); +it.each(["forward", "backward"] as const)( + "reports a missing endpoint after the last eligible %s edge fills the budget", + (direction) => { + const forward = direction === "forward"; + const partial = nativeInvestigationGraphSchema.parse({ + ...graph, + truncated: true, + edges: [ + graph.edges[0], + { + ...graph.edges[0], + id: "missing-endpoint", + from: forward ? "b" : "missing", + to: forward ? "missing" : "a", + }, + ], + }); + const trace = traceNativeInvestigationGraph(partial, { + start: forward ? "a" : "b", + direction, + limits: { max_edges: 1 }, + }); + expect(trace.edges.map(({ id }) => id)).toEqual(["1"]); + expect(trace).toMatchObject({ + truncated: true, + reason: "edge_target_missing_from_graph", + reached_depth: 1, + }); + }, +); + describe("Interface Builder dispatch joins", () => { it.each([ { diff --git a/src/domain/native/nativeInvestigationGraph.ts b/src/domain/native/nativeInvestigationGraph.ts index db9a26362e..934207fbc7 100644 --- a/src/domain/native/nativeInvestigationGraph.ts +++ b/src/domain/native/nativeInvestigationGraph.ts @@ -242,16 +242,26 @@ export const traceNativeInvestigationGraph = ( const adjacent = adjacency.get(current.id) ?? []; for (const edge of adjacent) { + const nextId = + edge.resolution === "unresolved" + ? null + : direction === "forward" + ? edge.to + : edge.from; + if (nextId !== null && !nodeById.has(nextId)) { + truncated = true; + reason ??= "edge_target_missing_from_graph"; + continue; + } if (selectedEdges.size + unresolved.size >= limits.max_edges) { truncated = true; reason = "max_edges_reached"; break; } - if (edge.resolution === "unresolved") { + if (nextId === null) { unresolved.set(edge.id, edge); continue; } - const nextId = direction === "forward" ? edge.to : edge.from; const alreadyVisited = visited.has(nextId); if (!alreadyVisited && current.depth >= limits.max_depth) { truncated = true; @@ -267,11 +277,6 @@ export const traceNativeInvestigationGraph = ( } selectedEdges.set(edge.id, edge); if (alreadyVisited) continue; - if (!nodeById.has(nextId)) { - truncated = true; - reason ??= "edge_target_missing_from_graph"; - continue; - } visited.add(nextId); // A retained node has already been reached even if a later sibling // exhausts a budget before this node is dequeued for expansion.