From 44ede43940e7fa47a42df45f88f0248c1c34696e Mon Sep 17 00:00:00 2001 From: Byron Wall Date: Sun, 22 Nov 2026 15:19:29 -0500 Subject: [PATCH 1/2] fix(flow): guard frontier trace actions and clear stale anchors --- client/e2e/dataflow.spec.ts | 16 ++++++ .../src/viz/deps/flow/FlowInspector.test.ts | 57 +++++++++++++++++++ client/src/viz/deps/flow/FlowInspector.tsx | 10 +++- client/src/viz/deps/flow/FlowView.tsx | 10 ++-- client/src/viz/deps/flow/fetchSignal.test.ts | 30 ++++++++++ client/src/viz/deps/flow/fetchSignal.ts | 4 +- client/src/viz/deps/flow/inspectorModel.ts | 5 ++ 7 files changed, 124 insertions(+), 8 deletions(-) create mode 100644 client/src/viz/deps/flow/FlowInspector.test.ts diff --git a/client/e2e/dataflow.spec.ts b/client/e2e/dataflow.spec.ts index b96d54c8..c5c536d3 100644 --- a/client/e2e/dataflow.spec.ts +++ b/client/e2e/dataflow.spec.ts @@ -22,6 +22,22 @@ const box = (page: Page, label: string) => page.locator("[data-box]").filter({ h const boxLabelled = (page: Page, label: string) => page.locator(`[data-box][data-label^="${label}"]`).first(); test.describe("Data flow on solid-route", () => { + test("a failed new anchor request does not redisplay the previous graph", async ({ page }) => { + await page.goto(url({ view: "graph", anchor: "src/components/ViewerCard.tsx@ViewerCard:3", dir: "forward" })); + await expect(page.locator("[data-box]").first()).toBeVisible({ timeout: 90_000 }); + await page.route("**/api/dataflow/anchor-graph", (route) => route.fulfill({ + status: 404, contentType: "application/json", body: JSON.stringify({ detail: "No program element test-missing-anchor" }), + })); + await page.locator('[data-trace-direction="backward"]').click(); + await expect(page.getByText(/Anchor graph failed.*test-missing-anchor/)).toBeVisible(); + await expect(page.locator("[data-box]")).toHaveCount(0); + await expect(page.getByLabel("Data flow inspector")).toHaveCount(0); + await page.unroute("**/api/dataflow/anchor-graph"); + await page.goBack(); + await expect(page).toHaveURL(/dir=forward/); + await expect(page.locator("[data-box]").first()).toBeVisible({ timeout: 90_000 }); + }); + test("switches trace direction without stale canvas work", async ({ page }) => { const errors: string[] = []; page.on("pageerror", (error) => errors.push(error.message)); diff --git a/client/src/viz/deps/flow/FlowInspector.test.ts b/client/src/viz/deps/flow/FlowInspector.test.ts new file mode 100644 index 00000000..7b6122db --- /dev/null +++ b/client/src/viz/deps/flow/FlowInspector.test.ts @@ -0,0 +1,57 @@ +// @vitest-environment jsdom +import { createComponent } from "solid-js"; +import { render } from "solid-js/web"; +import { describe, expect, it, vi } from "vitest"; +import fixture from "../../../../../engine/test/srcly/__fixtures__/solid-route.anchor-graph.json"; +import type { AnchorGraphResult } from "../../../dataflow/contract.generated"; +import { anchorGraphToViewModel } from "./anchorAdapter"; +import { canAnchorBox } from "./inspectorModel"; +import { FlowInspector } from "./FlowInspector"; + +vi.mock("lucide-solid", () => ({ ExternalLink: () => null, X: () => null })); +const graph = fixture as unknown as AnchorGraphResult; + +describe("anchor continuation actions", () => { + it("keeps frontier source recovery but never offers its synthetic id as an anchor", () => { + const frontier = { ...graph.nodes[1], id: "frontier:end:program-element:example", kind: "frontier" as const, reason: "unbound-read" }; + const withFrontier = { ...graph, nodes: [...graph.nodes, frontier] }; + expect(frontier).toBeDefined(); + const container = document.createElement("div"); + document.body.append(container); + const open = vi.fn(); + const dispose = render(() => createComponent(FlowInspector, { graph: null, model: anchorGraphToViewModel(withFrontier), rootPath: "/root", selected: frontier.id, reach: null, open: [], onOpen: open, onSelect: () => {}, onPickOrigin: () => {}, onToggleFrame: () => {}, onClose: () => {}, onTraceFrom: vi.fn(), onAddAnchor: vi.fn() }), container); + try { + expect(container.querySelector("[data-trace-from]")).toBeNull(); + expect(container.textContent).not.toContain("Add to the trace"); + expect(container.textContent).toContain("Open its source"); + (container.querySelector('[title="Open in the code viewer"]') as HTMLButtonElement).click(); + expect(open).toHaveBeenCalledWith(frontier.absPath, frontier.line); + } finally { dispose(); container.remove(); } + }); +}); + +it("only permits actual anchor-graph nodes as continuation targets", () => { + const node = graph.nodes.find((n) => n.kind !== "scope" && n.kind !== "anchor")!; + expect(canAnchorBox(node)).toBe(true); + expect(canAnchorBox({ ...node, id: "frontier:end:" + node.id, kind: "frontier" })).toBe(false); + expect(canAnchorBox(undefined)).toBe(false); +}); + +it("keeps both continuation actions for a real program element", () => { + const node = graph.nodes.find((n) => n.kind !== "scope" && n.kind !== "anchor")!; + const container = document.createElement("div"); + document.body.append(container); + const trace = vi.fn(), add = vi.fn(); + const dispose = render(() => createComponent(FlowInspector, { + graph: null, model: anchorGraphToViewModel(graph), rootPath: "/root", selected: node.id, reach: null, open: [], + onOpen: () => {}, onSelect: () => {}, onPickOrigin: () => {}, onToggleFrame: () => {}, onClose: () => {}, + onTraceFrom: trace, onAddAnchor: add, + }), container); + try { + (container.querySelector("[data-trace-from]") as HTMLButtonElement).click(); + const addButton = [...container.querySelectorAll("button")].find((b) => b.textContent?.includes("Add to the trace"))!; + addButton.click(); + expect(trace).toHaveBeenCalledWith(node.id); + expect(add).toHaveBeenCalledWith(node.id); + } finally { dispose(); container.remove(); } +}); diff --git a/client/src/viz/deps/flow/FlowInspector.tsx b/client/src/viz/deps/flow/FlowInspector.tsx index bae0f17b..a0d2c555 100644 --- a/client/src/viz/deps/flow/FlowInspector.tsx +++ b/client/src/viz/deps/flow/FlowInspector.tsx @@ -7,7 +7,7 @@ import { Button } from "../../../components/ui/Button"; import { IconButton } from "../../../components/ui/IconButton"; import type { FlowBox, FlowViewModel } from "./adapter"; import { FlowMarkGlyph } from "./FlowMark"; -import { anchorOverview, nodeDetails, routeOverview, STATUS_WORD, type AnchorOverview, type GapGroup, type GapPlace, type NeighborRow } from "./inspectorModel"; +import { canAnchorBox, anchorOverview, nodeDetails, routeOverview, STATUS_WORD, type AnchorOverview, type GapGroup, type GapPlace, type NeighborRow } from "./inspectorModel"; import { FLOW_MARKS, REPETITION_BADGE, STOP_REASONS, type FlowMark } from "./marks"; import type { Reach } from "./reach"; import { createEffectsQuery, EffectsPanel } from "../../../dataflow/EffectsPanel"; @@ -184,7 +184,7 @@ export function FlowInspector(props: { {/* A stop srcly words is already said by its detail line; any other reason shows as the engine names it. */} {(r) =>
{r()}
}
props.onOpen(d().node.absPath, d().node.line)} /> - +
{/* A folded function opens instead: its own anchor shows only its parameters and returns. */} @@ -200,6 +200,12 @@ export function FlowInspector(props: {
+ +
+ This stop is not a traceable program element. Open its source to select an expression to trace. +
+
+ {(r) => section("Reach", (
diff --git a/client/src/viz/deps/flow/FlowView.tsx b/client/src/viz/deps/flow/FlowView.tsx index 5ba8ddf2..ff3f64f2 100644 --- a/client/src/viz/deps/flow/FlowView.tsx +++ b/client/src/viz/deps/flow/FlowView.tsx @@ -25,7 +25,7 @@ import { FlowLegend } from "./FlowLegend"; import { DepthControl, DirectionSwitch, FlowModeSwitch, ModelSwitch, ReachModeSwitch, REACH_OF_DIRECTION, RecentFlowMenu, RouteLensSwitch } from "./FlowTools"; import { createFetch } from "./fetchSignal"; import { FlowProgress } from "./FlowProgress"; -import { stopsOf } from "./inspectorModel"; +import { canAnchorBox, stopsOf } from "./inspectorModel"; import { PythonApps } from "./PythonApps"; import { reachFrom, REACH_MODES, type ReachMode } from "./reach"; import { RoutePicker } from "./RoutePicker"; @@ -142,7 +142,7 @@ export default function FlowView(props: { const spec = parseAnchor(raw)!; if ("exports" in spec) throw new Error("An exports anchor has no single graph"); return watchProgress(path, fetchAnchorGraph({ path, direction: direction as Direction, ...anchorFields(spec), ...(gen ? { expectedGeneration: Number(gen) } : {}) })); - }); + }, { keepPrevious: false }); const anchorGraph = anchorFetch.value; // The other anchors (T20): each its own query, one at a time, kept by generation so a Return does not refetch. type ExtraAnchor = { raw: string; graph: AnchorGraphResult | null; outcome: string; reason: string | null }; @@ -190,7 +190,7 @@ export default function FlowView(props: { const addAnchorFrom = (id: string) => { const box = model()?.byId.get(id); const specs = anchorSpecs(); - if (!box || !specs.length || specs.length >= MAX_ANCHORS || exportsSpec()) return; + if (!box || !canAnchorBox(box) || !specs.length || specs.length >= MAX_ANCHORS || exportsSpec()) return; // By element id: the engine anchors at the very element the box stands for, so the traces share ids by construction. const next: Anchor = { file: box.file, element: id }; if (specs.some((a) => encodeAnchor(a) === encodeAnchor(next))) return; @@ -200,7 +200,7 @@ export default function FlowView(props: { /** Re-anchor on a drawn box by its element id, keeping the direction. */ const traceFrom = (id: string) => { const box = model()?.byId.get(id); - if (!box || !("side" in box) || box.kind === "scope") return; + if (!box || !canAnchorBox(box) || box.kind === "scope") return; setSelected(null); go({ anchor: encodeAnchor({ file: box.file, element: id }), open: undefined }); }; @@ -360,9 +360,9 @@ export default function FlowView(props: { let token = 0; createEffect(() => { + const mine = ++token; const m = model(); if (!m) return setLayout(null); - const mine = ++token; const rect = host?.getBoundingClientRect(); const viewport = rect && rect.height > 0 ? { width: rect.width, height: rect.height } : undefined; const aspectRatio = viewport ? viewport.width / viewport.height : 1.6; diff --git a/client/src/viz/deps/flow/fetchSignal.test.ts b/client/src/viz/deps/flow/fetchSignal.test.ts index 1eae648c..c763fa6d 100644 --- a/client/src/viz/deps/flow/fetchSignal.test.ts +++ b/client/src/viz/deps/flow/fetchSignal.test.ts @@ -22,3 +22,33 @@ describe("createFetch", () => { dispose(); }); }); + +it("clears an anchor's previous result while loading and after failure, ignoring late replies", async () => { + const pending = new Map void; reject: (error: Error) => void }>(); + const { fetch, setKey, dispose } = createRoot((dispose) => { + const [key, setKey] = createSignal("first"); + return { fetch: createFetch(key, (k) => new Promise((resolve, reject) => pending.set(k, { resolve, reject })), { keepPrevious: false }), setKey, dispose }; + }); + try { + pending.get("first")!.resolve("first graph"); + await Promise.resolve(); + expect(fetch.value()).toBe("first graph"); + setKey("second"); + expect(fetch.loading()).toBe(true); + expect(fetch.value()).toBeUndefined(); + setKey("third"); + pending.get("third")!.reject(new Error("No program element")); + await Promise.resolve(); + expect(fetch.error()?.message).toBe("No program element"); + expect(fetch.value()).toBeUndefined(); + pending.get("second")!.resolve("late second graph"); + await Promise.resolve(); + expect(fetch.value()).toBeUndefined(); + expect(fetch.error()?.message).toBe("No program element"); + fetch.retry(); + expect(fetch.error()).toBeNull(); + pending.get("third")!.resolve("retried graph"); + await Promise.resolve(); + expect(fetch.value()).toBe("retried graph"); + } finally { dispose(); } +}); diff --git a/client/src/viz/deps/flow/fetchSignal.ts b/client/src/viz/deps/flow/fetchSignal.ts index d232ae1c..dbf9c4e5 100644 --- a/client/src/viz/deps/flow/fetchSignal.ts +++ b/client/src/viz/deps/flow/fetchSignal.ts @@ -5,7 +5,7 @@ import { createEffect, createSignal, on } from "solid-js"; * reading it never suspends: the graph view sits under the treemap's * Suspense, which would swap the whole view for its fallback. */ -export function createFetch(source: () => S | null | undefined, fetcher: (key: S) => Promise) { +export function createFetch(source: () => S | null | undefined, fetcher: (key: S) => Promise, options: { keepPrevious?: boolean } = {}) { const [value, setValue] = createSignal(); const [loading, setLoading] = createSignal(false); const [error, setError] = createSignal(null); @@ -16,6 +16,8 @@ export function createFetch(source: () => S | null | undefined, fetcher: ( on([source, attempt], ([key]) => { const mine = ++token; setError(null); + // Anchor requests must not present a previous graph as the new request. + if (options.keepPrevious === false) setValue(undefined); if (key === null || key === undefined) { setValue(undefined); setLoading(false); diff --git a/client/src/viz/deps/flow/inspectorModel.ts b/client/src/viz/deps/flow/inspectorModel.ts index a6dfc98e..44e28936 100644 --- a/client/src/viz/deps/flow/inspectorModel.ts +++ b/client/src/viz/deps/flow/inspectorModel.ts @@ -6,6 +6,11 @@ import { isAssumedProof } from "../../../dataflow/outcome"; import type { FlowBox, FlowViewModel } from "./adapter"; import { humanizeDetail, MARK_OF_ANCHOR_KIND, MARK_OF_KIND, STOP_REASONS, stopWord, type FlowMark } from "./marks"; +/** Frontiers describe where a walk stopped; their display ids are not program elements. */ +export function canAnchorBox(box: FlowBox | undefined): boolean { + return !!box && "side" in box && box.kind !== "frontier"; +} + /** Route mode words for the render backbone; anchor mode names the engine's relation (`argument`, `return value`, …). */ export type EdgeKindWord = "renders" | "rendered by" | "handoff" | "field" | "structural" | (string & {}); From 1c7d14ebb7d23c5d193fa8a1e8fcf774588c63f7 Mon Sep 17 00:00:00 2001 From: Byron Wall Date: Sun, 22 Nov 2026 15:41:00 -0500 Subject: [PATCH 2/2] test(flow): verify Back after re-anchoring instead of changing direction --- client/e2e/dataflow.spec.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/client/e2e/dataflow.spec.ts b/client/e2e/dataflow.spec.ts index c5c536d3..f29db97e 100644 --- a/client/e2e/dataflow.spec.ts +++ b/client/e2e/dataflow.spec.ts @@ -23,18 +23,24 @@ const boxLabelled = (page: Page, label: string) => page.locator(`[data-box][data test.describe("Data flow on solid-route", () => { test("a failed new anchor request does not redisplay the previous graph", async ({ page }) => { - await page.goto(url({ view: "graph", anchor: "src/components/ViewerCard.tsx@ViewerCard:3", dir: "forward" })); - await expect(page.locator("[data-box]").first()).toBeVisible({ timeout: 90_000 }); + const originalAnchor = "src/components/ViewerCard.tsx@ViewerCard:3"; + await page.goto(url({ view: "graph", anchor: originalAnchor, dir: "backward" })); + await expect(status(page)).toContainText("upstream", { timeout: 90_000 }); + await page.locator('[data-box][data-label="viewer"]').first().click(); + const trace = page.getByLabel("Data flow inspector").locator("[data-trace-from]"); + await expect(trace).toBeVisible(); await page.route("**/api/dataflow/anchor-graph", (route) => route.fulfill({ status: 404, contentType: "application/json", body: JSON.stringify({ detail: "No program element test-missing-anchor" }), })); - await page.locator('[data-trace-direction="backward"]').click(); + // Re-anchoring is navigation; the direction toggle only replaces the current entry. + await trace.click(); + await expect(page).toHaveURL(/anchor=[^&]*%40el%3A/); await expect(page.getByText(/Anchor graph failed.*test-missing-anchor/)).toBeVisible(); await expect(page.locator("[data-box]")).toHaveCount(0); await expect(page.getByLabel("Data flow inspector")).toHaveCount(0); await page.unroute("**/api/dataflow/anchor-graph"); await page.goBack(); - await expect(page).toHaveURL(/dir=forward/); + await expect(page).toHaveURL((url) => url.searchParams.get("anchor") === originalAnchor && url.searchParams.get("dir") === "backward"); await expect(page.locator("[data-box]").first()).toBeVisible({ timeout: 90_000 }); });