Understand any codebase instantly. System intelligence for codebases, built for humans and AI.
TypeScript aiai-memoryclaude-codeclicode-analysiscode-mappingcode-memorycodebase-understandingcodexdeveloper-toolsllmopenclawpersistent-memoryprogram-analysissoftware-architecturesystem-intelligencesystem-mapping
fix(ingest): keep a shared external node while other files still call it
KageBinaryaf3702124f53
diff --git a/ix-cli/src/cli/__tests__/helpers/fake-backend.ts b/ix-cli/src/cli/__tests__/helpers/fake-backend.tsindex 75421a4d..1e35325f 100644--- a/ix-cli/src/cli/__tests__/helpers/fake-backend.ts+++ b/ix-cli/src/cli/__tests__/helpers/fake-backend.ts@@ -122,7 +122,7 @@ export class FakeBackend {* report none, as a pre-#157 backend does. `null` reports none in any mode.*/releaseVersion: string | null | undefined = undefined;- private readonly nodes = new Map<string, { kind: unknown; name: unknown; live: boolean }>();+ private readonly nodes = new Map<string, { kind: unknown; name: unknown; attrs?: unknown; live: boolean }>();private readonly edges = new Map<string, {src: unknown; dst: unknown; predicate: unknown; workspaceId: string; uri: string; live: boolean;}>();@@ -198,7 +198,7 @@ export class FakeBackend {if (!deletesApplied) this.applyDeletes(patch);for (const op of patch.ops ?? []) {const id = String(op.id);- if (op.type === "UpsertNode") this.nodes.set(id, { kind: op.kind, name: op.name, live: true });+ if (op.type === "UpsertNode") this.nodes.set(id, { kind: op.kind, name: op.name, attrs: op.attrs, live: true });else if (op.type === "UpsertEdge") {this.edges.set(id, { src: op.src, dst: op.dst, predicate: op.predicate, workspaceId, uri, live: true });}@@ -474,7 +474,7 @@ export class FakeBackend {const edges = [...this.edges].filter(([, e]) => e.live && (e.src === id || e.dst === id)).map(([edgeId, e]) => ({ id: edgeId, predicate: e.predicate, provenance: { sourceUri: e.uri } }));- return send(200, { node: { id, kind: node.kind, name: node.name }, edges });+ return send(200, { node: { id, kind: node.kind, name: node.name, attrs: node.attrs ?? {} }, edges });}if (path === "/v1/stitch") {this.requests.push({ path, patches: 0 });diff --git a/ix-cli/src/cli/__tests__/ingest-files.test.ts b/ix-cli/src/cli/__tests__/ingest-files.test.tsindex 3ebb83d7..d86b5d98 100644--- a/ix-cli/src/cli/__tests__/ingest-files.test.ts+++ b/ix-cli/src/cli/__tests__/ingest-files.test.ts@@ -1193,6 +1193,39 @@ describe("ingestFiles against a fake backend", () => {expect(backend.lastOps.get("src/a.ts")?.some(op => op.type === "DeleteNode"), "the patch deletes").toBe(true);expect(perFile()).toBe(1);});++ it("goes to /v1/patch on its own on a release before the one that applies bulk deletes", async () => {+ backend.releaseVersion = "1.0.31";+ await removeAFunction();+ expect(perFile()).toBe(1);+ });++ // An external package node is keyed by the workspace, not the file, so+ // every caller writes the same one. One caller dropping its call deleted+ // the node and every other caller's edge to it, though their files had+ // not changed.+ it("a file that stops calling a package function leaves the other callers' edges", async () => {+ backend.semantics = "head";+ backend.releaseVersion = "1.0.32";+ mkdirSync(join(repo, "R"), { recursive: true });+ const calls = (fn: string) => `${fn} <- function(x) {\n dplyr::filter(x)\n}\n`;+ writeFileSync(join(repo, "R", "a.R"), calls("fa"), "utf8");+ writeFileSync(join(repo, "R", "b.R"), calls("fb"), "utf8");+ execFileSync("git", ["init", "-q"], { cwd: repo, stdio: "ignore" });+ execFileSync("git", ["add", "-A"], { cwd: repo, stdio: "ignore" });+ await incremental();+ const edgeNames = () => backend.graphSignature().edges+ .map(line => line.split(" "))+ .filter(([, , , predicate]) => predicate === "CALLS")+ .map(([, src, dst]) => `${backend.nodeName(src!)} -> ${backend.nodeName(dst!)}`)+ .sort();+ expect(edgeNames()).toEqual(["fa[function] -> filter[function]", "fb[function] -> filter[function]"]);++ writeFileSync(join(repo, "R", "a.R"), "fa <- function(x) {\n x\n}\n", "utf8");+ await incremental();++ expect(edgeNames()).toEqual(["fb[function] -> filter[function]"]);+ });});// ── Which workspace a path belongs to ─────────────────────────────────diff --git a/ix-cli/src/cli/__tests__/ingest-reconcile.test.ts b/ix-cli/src/cli/__tests__/ingest-reconcile.test.tsindex 4211fca8..66fcaf33 100644--- a/ix-cli/src/cli/__tests__/ingest-reconcile.test.ts+++ b/ix-cli/src/cli/__tests__/ingest-reconcile.test.ts@@ -100,6 +100,50 @@ describe("reconcileRemovedEntities", () => {expect(reconciled.ops.filter(o => o.type === "DeleteEdge").map(o => o["id"])).toEqual(ids.map(id => `e-${id}`));});+ describe("an external package node, which every caller in the workspace shares", () => {+ const ext = { type: "UpsertNode", id: "ext-filter", kind: "function", attrs: { external: true, package: "dplyr" } };+ const edges = [+ { id: "own-call", predicate: "CALLS", provenance: { sourceUri: "src/example.ts" } },+ { id: "other-call", predicate: "CALLS", provenance: { sourceUri: "src/other.ts" } },+ ];++ it("stays, with the other files' edges, when this file stops calling it", async () => {+ const getPatch = vi.fn().mockResolvedValue({ data: { ops: [ext] } });+ const entity = vi.fn().mockResolvedValue({ node: { kind: "function" }, claims: [], edges });+ const dependents = new Set<string>();++ const reconciled = await reconcileRemovedEntities({ getPatch, entity }, patchWith([]), ["prev"], dependents);++ expect(reconciled.ops.filter(o => o.type === "DeleteNode")).toEqual([]);+ expect(reconciled.ops.filter(o => o.type === "DeleteEdge").map(o => o["id"])).toEqual(["own-call"]);+ expect([...dependents], "the other file keeps its edge, so it is not a dependent").toEqual([]);+ });++ it("is recognised from the entity's attrs when the stored patch kept no ops", async () => {+ const getPatch = vi.fn().mockResolvedValue({+ data: { entityIds: ["ext-filter"], nodeOpCount: 1, edgeOpCount: 0 },+ });+ const entity = vi.fn().mockResolvedValue({+ node: { kind: "function", attrs: { external: true } }, claims: [], edges,+ });++ const reconciled = await reconcileRemovedEntities({ getPatch, entity }, patchWith([]), ["prev"]);++ expect(reconciled.ops.filter(o => o.type === "DeleteNode")).toEqual([]);+ expect(reconciled.ops.filter(o => o.type === "DeleteEdge").map(o => o["id"])).toEqual(["own-call"]);+ });++ it("goes when this file was its last caller", async () => {+ const getPatch = vi.fn().mockResolvedValue({ data: { ops: [ext] } });+ const entity = vi.fn().mockResolvedValue({ node: { kind: "function" }, claims: [], edges: [edges[0]] });++ const reconciled = await reconcileRemovedEntities({ getPatch, entity }, patchWith([]), ["prev"]);++ expect(reconciled.ops.filter(o => o.type === "DeleteNode").map(o => o["id"])).toEqual(["ext-filter"]);+ expect(reconciled.ops.filter(o => o.type === "DeleteEdge").map(o => o["id"])).toEqual(["own-call"]);+ });+ });+it("reingests surviving dependents when a deleted file returns", () => {const projectRoot = nodePath.resolve("workspace");const returnedPath = nodePath.join(projectRoot, "src", "returned.ts");diff --git a/ix-cli/src/cli/commands/ingest.ts b/ix-cli/src/cli/commands/ingest.tsindex bc04dc3e..c22ab695 100644--- a/ix-cli/src/cli/commands/ingest.ts+++ b/ix-cli/src/cli/commands/ingest.ts@@ -305,6 +305,17 @@ interface StoredPatchEntities {nodeIds: string[];/** Node kinds the stored patch wrote, when it kept its ops; empty otherwise. */kinds: Map<string, string>;+ /** External package nodes (`attrs.external`) the stored patch wrote. */+ external: Set<string>;+}++/**+ * Whether a node is an external package node: `external://<pkg>` keyed by the+ * workspace, not the file, so every file that calls into the package writes+ * the same one.+ */+function isExternalNode(entityNode: unknown): boolean {+ return (entityNode as { attrs?: { external?: unknown } } | undefined)?.attrs?.external === true;}function readStoredPatchEntities(raw: unknown): StoredPatchEntities | null {@@ -315,10 +326,13 @@ function readStoredPatchEntities(raw: unknown): StoredPatchEntities | null {} | undefined;if (!data) return null;if (Array.isArray(data.ops)) {- const ops = data.ops as Array<{ type?: unknown; id?: unknown; kind?: unknown }>;+ const ops = data.ops as Array<{ type?: unknown; id?: unknown; kind?: unknown; attrs?: unknown }>;const kinds = new Map<string, string>();+ const external = new Set<string>();for (const op of ops) {- if (op.type === 'UpsertNode' && typeof op.id === 'string' && typeof op.kind === 'string') kinds.set(op.id, op.kind);+ if (op.type !== 'UpsertNode' || typeof op.id !== 'string') continue;+ if (typeof op.kind === 'string') kinds.set(op.id, op.kind);+ if (isExternalNode(op)) external.add(op.id);}return {nodeIds: ops@@ -326,6 +340,7 @@ function readStoredPatchEntities(raw: unknown): StoredPatchEntities | null {.map(op => op.id).filter((id): id is string => typeof id === 'string'),kinds,+ external,};}if (!Array.isArray(data.entityIds)) return null;@@ -338,6 +353,7 @@ function readStoredPatchEntities(raw: unknown): StoredPatchEntities | null {return {nodeIds: entityIds.slice(0, nodeCount),kinds: new Map(),+ external: new Set(),};}@@ -418,19 +434,38 @@ export async function reconcileRemovedEntities(if (mapMode && (entity.node as { kind?: unknown } | undefined)?.kind === 'chunk') {continue;}- removedNodeIds.push(nodeId);- for (const edge of entity.edges ?? []) {+ const edges = (entity.edges ?? []).map(edge => {const edgeRecord = edge as {id?: unknown;+ predicate?: unknown;provenance?: { sourceUri?: unknown; source_uri?: unknown };};- const edgeId = edgeRecord.id;- const predicate = (edge as { predicate?: unknown }).predicate;+ return {+ id: edgeRecord.id,+ predicate: edgeRecord.predicate,+ sourceUri: edgeRecord.provenance?.sourceUri ?? edgeRecord.provenance?.source_uri,+ };+ });+ // An external package node is shared by every file in the workspace that+ // calls into the package. This file no longer writing it does not remove+ // it: while another file's edges still reach it, the node and those edges+ // stay, and only this file's own edges to it go.+ const shared = (previous.external.has(nodeId) || isExternalNode(entity.node))+ && edges.some(edge => edge.sourceUri !== patch.source.uri);+ if (shared) {+ for (const edge of edges) {+ if (typeof edge.id === 'string' && edge.sourceUri === patch.source.uri && !currentEdgeIds.has(edge.id)) {+ removedEdgeIds.add(edge.id);+ }+ }+ continue;+ }+ removedNodeIds.push(nodeId);+ for (const { id: edgeId, predicate, sourceUri } of edges) {const isChunkEdge = typeof predicate === 'string' && CHUNK_PREDICATES.has(predicate);if (typeof edgeId === 'string' && !currentEdgeIds.has(edgeId) && !(mapMode && isChunkEdge)) {removedEdgeIds.add(edgeId);}- const sourceUri = edgeRecord.provenance?.sourceUri ?? edgeRecord.provenance?.source_uri;if (dependentSourceUris &&typeof sourceUri === 'string' &&@@ -460,18 +495,18 @@ export async function reconcileRemovedEntities(};}-/**- * Whether `patch` must go to `/v1/patch` on its own. Only a patch that deletes,- * and only against a backend whose bulk route is not known to apply deletes.- * `/v1/patch` does not sweep the file's old edges, so a removed call between- * two surviving symbols stayed live there; the bulk route sweeps them.- *//*** Files reconciled at once, and entity lookups in flight across all of them:* the graph walkers' cap (`EXPAND_CONCURRENCY`), for the same reason.*/const RECONCILE_CONCURRENCY = 8;+/**+ * Whether `patch` must go to `/v1/patch` on its own. Only a patch that deletes,+ * and only against a backend whose bulk route is not known to apply deletes.+ * `/v1/patch` does not sweep the file's old edges, so a removed call between+ * two surviving symbols stayed live there; the bulk route sweeps them.+ */export function patchRequiresPerFileCommit(patch: GraphPatchPayload, bulkDeletes = false): boolean {if (bulkDeletes) return false;return patch.ops.some(op => op.type === 'DeleteNode' || op.type === 'DeleteEdge');