Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

perf(context): seed the context request by resolved id, not by name (#499)

KageBinary70919d2fad88
diff --git a/ix-cli/src/cli/__tests__/context-seeds-by-id.test.ts b/ix-cli/src/cli/__tests__/context-seeds-by-id.test.ts
new file mode 100644
index 00000000..99791c17
--- /dev/null
+++ b/ix-cli/src/cli/__tests__/context-seeds-by-id.test.ts
@@ -0,0 +1,77 @@
+import { describe, expect, it, vi } from "vitest";
+
+import { IxClient } from "../../client/api.js";
+
+/**
+ * `ix context` resolves its target and then has to ask the backend for the
+ * graph around it. It used to ask by NAME, which made the backend re-run the
+ * search the resolver had just done — the single most expensive call the CLI
+ * makes, measured at 10.6 s against 0.06 s for the id form on the same graph.
+ *
+ * Worse than slow: the re-derived seeds can land on a different node of the
+ * same name. `README.md` resolved to one id and came back seeded on
+ * `mcp/node_modules/serve-static/README.md`, with the resolved id in neither
+ * the seed list nor the returned nodes.
+ */
+describe("contextForNode", () => {
+ function capture() {
+ const calls: Array<{ url: string; body: any }> = [];
+ vi.stubGlobal("fetch", async (url: any, init: any) => {
+ calls.push({ url: String(url), body: JSON.parse(init.body) });
+ return new Response(JSON.stringify({ nodes: [], edges: [], claims: [] }), {
+ status: 200,
+ headers: { "Content-Type": "application/json" },
+ });
+ });
+ return calls;
+ }
+
+ it("sends the node id as a slice, never a name", async () => {
+ const calls = capture();
+ await new IxClient("http://x").contextForNode("11111111-2222-3333-4444-555555555555");
+ const body = calls[0].body;
+
+ expect(body.slices).toEqual([
+ {
+ type: "nodes",
+ nodeIds: ["11111111-2222-3333-4444-555555555555"],
+ expand: true,
+ hops: 1,
+ },
+ ]);
+ // The whole point: no free-text query for the backend to re-search.
+ expect(body.query).toBeUndefined();
+ vi.unstubAllGlobals();
+ });
+
+ it("passes depth through rather than pinning it to full", async () => {
+ const calls = capture();
+ await new IxClient("http://x").contextForNode("11111111-2222-3333-4444-555555555555", {
+ depth: "compact",
+ asOfRev: 2000,
+ });
+ // `--depth` has to keep meaning what it says: compact and standard return
+ // the summarized graph, full returns whole nodes plus claims. Hardcoding
+ // full would silently ignore the flag and change every bundle's size.
+ expect(calls[0].body.depth).toBe("compact");
+ expect(calls[0].body.asOfRev).toBe(2000);
+ vi.unstubAllGlobals();
+ });
+});
+
+describe("context.ts call sites", () => {
+ it("has no by-name context call left in the command", async () => {
+ const { readFileSync } = await import("node:fs");
+ const { fileURLToPath } = await import("node:url");
+ const { dirname, join } = await import("node:path");
+ const src = readFileSync(
+ join(dirname(fileURLToPath(import.meta.url)), "..", "commands", "context.ts"),
+ "utf8",
+ );
+ // Two call sites, and both must convert. Leaving one behind makes `--diff`
+ // compare a by-id bundle against a by-name one and report the entire graph
+ // as changed on the first run after upgrade.
+ expect(src).not.toContain("client.query(resolved.name");
+ expect(src.match(/client\.contextForNode\(resolved\.id/g) ?? []).toHaveLength(2);
+ });
+});
diff --git a/ix-cli/src/cli/commands/context.ts b/ix-cli/src/cli/commands/context.ts
index 7abb7806..2e0d26e8 100644
--- a/ix-cli/src/cli/commands/context.ts
+++ b/ix-cli/src/cli/commands/context.ts
@@ -314,7 +314,12 @@ export function registerContextCommand(program: Command): void {
const [facts, context, provenance] = await Promise.all([
collectFacts(client, resolved.id, resolved.name, resolved.kind),
- client.query(resolved.name, {
+ // By id, not by name. Seeding by name makes the backend re-run the
+ // search the resolver just did, and it can land on a different node of
+ // the same name. Both call sites in this file must use it: converting
+ // only one makes `--diff` compare a by-id bundle against a by-name one
+ // and report the whole graph as changed.
+ client.contextForNode(resolved.id, {
asOfRev,
depth: opts.depth,
}),
@@ -407,7 +412,7 @@ async function buildFreshBundle(
const asOfRev = opts.asOfRev;
const [facts, context, provenance] = await Promise.all([
collectFacts(client, resolved.id, resolved.name, resolved.kind),
- client.query(resolved.name, { asOfRev, depth: opts.depth }),
+ client.contextForNode(resolved.id, { asOfRev, depth: opts.depth }),
client.provenance(resolved.id),
]);
diff --git a/ix-cli/src/client/api.ts b/ix-cli/src/client/api.ts
index 3fabacd4..8261e24c 100644
--- a/ix-cli/src/client/api.ts
+++ b/ix-cli/src/client/api.ts
@@ -47,6 +47,32 @@ export class IxClient {
return this.post("/v1/context", { query: question, ...opts });
}
+ /**
+ * Context centred on a node we have already resolved.
+ *
+ * `query()` sends a NAME and makes the backend re-derive seeds by string
+ * search. That is the single most expensive call the CLI makes — measured at
+ * 10.6 s against 0.06 s for this form on the same graph — and it is not just
+ * slow, it can centre the bundle on a different node: `README.md` resolved to
+ * one id and came back seeded on `mcp/node_modules/serve-static/README.md`,
+ * with the resolved id in neither the seeds nor the returned nodes.
+ *
+ * The id is already in hand by the time this is called, so send it. `depth` is
+ * passed through rather than pinned to `full`, so `--depth` still means what it
+ * says: compact and standard return the summarized graph (which `buildBundle`
+ * reads), full returns whole nodes plus claims.
+ */
+ async contextForNode(
+ nodeId: string,
+ opts?: { asOfRev?: number; depth?: string; hops?: number }
+ ): Promise<StructuredContext> {
+ const { hops = 1, ...rest } = opts ?? {};
+ return this.post("/v1/context", {
+ slices: [{ type: "nodes", nodeIds: [nodeId], expand: true, hops }],
+ ...rest,
+ });
+ }
+
async ingest(path: string, recursive?: boolean, force?: boolean): Promise<IngestResult> {
const resp = await fetch(`${this.endpoint}/v1/ingest`, {
method: "POST",