Skip to content

armgit / Ix

Public Imported from GitHub
0

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

fix(context): centre --from-issue on an indexed file, and fail in JSON under --format json

KageBinary42ee4f4e1fc7
diff --git a/ix-cli/src/cli/__tests__/context-from-issue-centre.test.ts b/ix-cli/src/cli/__tests__/context-from-issue-centre.test.ts
new file mode 100644
index 00000000..5e3c3832
--- /dev/null
+++ b/ix-cli/src/cli/__tests__/context-from-issue-centre.test.ts
@@ -0,0 +1,62 @@
+// Copyright 2026 Ix Infrastructure Inc.
+
+import { describe, expect, it } from "vitest";
+
+import { CENTRE_FALLBACK_TRIES, chooseCentre, type StartingPoint } from "../explain/issue.js";
+
+/**
+ * Which starting point a `--from-issue` bundle is centred on.
+ *
+ * On SWE-PolyBench two svelte issues named no code; BM25's top file was a
+ * `.svelte` component the graph has no node for, and the command failed with
+ * no bundle while a lower-ranked `.js` module was indexed.
+ */
+
+const bm25 = (...paths: string[]) => paths.map((path, i) => ({ path, score: 10 - i }));
+const graph = (...indexed: string[]) => async (path: string) =>
+ indexed.includes(path) ? `node:${path}` : undefined;
+const fallbackStart = (path: string): StartingPoint =>
+ ({ token: path, name: path.split("/").pop()!, kind: "file", path, via: "bm25 fallback" });
+
+describe("chooseCentre", () => {
+ it("walks BM25 past files the graph has no node for", async () => {
+ const hits = bm25("site/src/routes/index.svelte", "src/Component.svelte", "src/compiler/render.js");
+ const { centre, starts, walked } = await chooseCentre(
+ [fallbackStart(hits[0].path)], hits, graph("src/compiler/render.js"));
+ expect(centre?.path).toBe("src/compiler/render.js");
+ expect(centre?.id).toBe("node:src/compiler/render.js");
+ expect(centre?.via).toBe("bm25 fallback");
+ expect(walked).toBe(true);
+ expect(starts.map((s) => s.path)).toEqual(["site/src/routes/index.svelte", "src/compiler/render.js"]);
+ });
+
+ it("keeps a named start that has a node, without walking", async () => {
+ const named: StartingPoint = { token: "listByKind", id: "n1", name: "listByKind", kind: "method",
+ path: "src/api.ts", via: "identifier in issue" };
+ const { centre, walked } = await chooseCentre([named], bm25("src/other.ts"), graph("src/other.ts"));
+ expect(centre).toEqual(named);
+ expect(walked).toBe(false);
+ });
+
+ it("resolves a path start to its file node before falling back", async () => {
+ const path: StartingPoint = { token: "src/a.ts", name: "a.ts", kind: "file", path: "src/a.ts", via: "path in issue" };
+ const { centre, walked } = await chooseCentre([path], bm25("src/b.ts"), graph("src/a.ts", "src/b.ts"));
+ expect(centre?.path).toBe("src/a.ts");
+ expect(walked).toBe(false);
+ });
+
+ it("gives up after a bounded number of BM25 files, and says there is no centre", async () => {
+ const hits = bm25(...Array.from({ length: CENTRE_FALLBACK_TRIES + 1 }, (_, i) => `src/f${i}.svelte`));
+ const indexed = graph(hits.at(-1)!.path); // indexed, but past the limit
+ const { centre, walked } = await chooseCentre([], hits, indexed);
+ expect(centre).toBeUndefined();
+ expect(walked).toBe(false);
+ });
+
+ it("does not try the same file twice", async () => {
+ const tried: string[] = [];
+ const find = async (path: string) => { tried.push(path); return undefined; };
+ await chooseCentre([fallbackStart("src/x.svelte")], bm25("src/x.svelte", "src/y.svelte"), find);
+ expect(tried).toEqual(["src/x.svelte", "src/y.svelte"]);
+ });
+});
diff --git a/ix-cli/src/cli/commands/context.ts b/ix-cli/src/cli/commands/context.ts
index 29d95853..008884b0 100644
--- a/ix-cli/src/cli/commands/context.ts
+++ b/ix-cli/src/cli/commands/context.ts
@@ -22,6 +22,8 @@ import type {
import { getEndpoint, resolveWorkspaceRoot } from "../config.js";
import { collectFacts, type ContextFacts, type EntityLocation } from "../explain/facts.js";
import {
+ CENTRE_FALLBACK_TRIES,
+ chooseCentre,
CLOSENESS_BOOST,
planIssue,
rankIssueFiles,
@@ -1610,7 +1612,7 @@ async function buildIssueBundle(
try {
text = await readIssueText(arg);
} catch (error) {
- reportFailure(
+ issueFailure(
"issue_unreadable",
`Cannot read the issue from ${arg === "-" ? "stdin" : `"${arg}"`}: ${(error as Error).message}`,
opts.format,
@@ -1618,7 +1620,7 @@ async function buildIssueBundle(
return undefined;
}
if (!text.trim()) {
- reportFailure("empty_issue", `The issue ${arg === "-" ? "on stdin" : `in "${arg}"`} is empty.`, opts.format);
+ issueFailure("empty_issue", `The issue ${arg === "-" ? "on stdin" : `in "${arg}"`} is empty.`, opts.format);
return undefined;
}
@@ -1630,19 +1632,16 @@ async function buildIssueBundle(
search: async (name) =>
(await client.search(name, { limit: ISSUE_SEARCH_LIMIT, nameOnly: true, ...scope })).map(symbolHit),
});
- // A path start has no node yet; the graph may not have one either.
- const starts: StartingPoint[] = await Promise.all(plan.starts.map(async (s) => {
- const id = s.id ?? (await findFileNode(client, s.path));
- return id ? { ...s, id } : s;
- }));
-
- const centre = starts.find((s) => s.id);
+ // A path start has no node yet, and the graph may not have one either: it
+ // does not index every tracked file. chooseCentre walks BM25 for one it does.
+ const { starts, centre, walked } = await chooseCentre(
+ plan.starts, plan.bm25, (path) => findFileNode(client, path));
if (!centre) {
- reportFailure(
+ issueFailure(
"issue_unresolved",
starts.length === 0
? `Nothing the issue names resolved to a definition${plan.unresolved.length > 0 ? ` (tried ${plan.unresolved.slice(0, 5).join(", ")})` : ""}, and no tracked source file matched its text. Name a symbol or file and run ix context <target>.`
- : `The issue's starting points (${starts.map((s) => s.path).join(", ")}) have no node in the graph. Run ix map if this workspace has not been mapped.`,
+ : `Neither the issue's starting points (${starts.map((s) => s.path).join(", ")}) nor the ${CENTRE_FALLBACK_TRIES} source files that best match its text are in the graph. Name a symbol or file and run ix context <target>, or run ix map if this workspace has not been mapped.`,
opts.format,
);
return undefined;
@@ -1679,13 +1678,28 @@ async function buildIssueBundle(
issue: {
startingPoints: starts,
unresolved: plan.unresolved,
- fallback: plan.fallback,
+ fallback: plan.fallback || walked,
rankedFiles: rankIssueFiles({ starts, bm25: plan.bm25, near }),
extraEntities: around.flatMap((n) => n.related),
},
});
}
+/**
+ * A `--from-issue` failure, readable by the format asked for. `reportFailure`
+ * writes nothing to stdout under `--format json`, so a JSON caller got an empty
+ * stdout and had to scrape stderr; this prints the record the other commands'
+ * JSON failures use.
+ */
+function issueFailure(code: string, message: string, format: string | undefined): void {
+ if (format === "json") {
+ printJson({ error: code, message });
+ process.exitCode = 1;
+ return;
+ }
+ reportFailure(code, message, format);
+}
+
/** A secondary starting point's one-hop facts and related files. Best-effort. */
async function startNeighbourhood(
client: IxClient,
diff --git a/ix-cli/src/cli/explain/issue.ts b/ix-cli/src/cli/explain/issue.ts
index e7811aa6..830ec4ea 100644
--- a/ix-cli/src/cli/explain/issue.ts
+++ b/ix-cli/src/cli/explain/issue.ts
@@ -317,6 +317,44 @@ export async function planIssue(
};
}
+/** BM25 files tried, best first, for one the graph has a node for. */
+export const CENTRE_FALLBACK_TRIES = 10;
+
+/**
+ * The starting point a bundle is centred on: the first start the graph has a
+ * node for, else the best BM25 file that has one.
+ *
+ * A start can have no node: the graph does not index every tracked file.
+ * On SWE-PolyBench two svelte issues named no code, BM25's top file was a
+ * `.svelte` component, and the command failed with no bundle at all while
+ * the fifth-ranked file was a perfectly good `.js` module. Walking BM25's
+ * ranking finds that one; the files the walk skipped still rank as usual.
+ * `walked` says the centre came from this walk, which is a fallback too.
+ */
+export async function chooseCentre(
+ starts: StartingPoint[],
+ bm25: Bm25Hit[],
+ findNode: (path: string) => Promise<string | undefined>,
+ tries = CENTRE_FALLBACK_TRIES,
+): Promise<{ starts: StartingPoint[]; centre?: StartingPoint; walked: boolean }> {
+ const located = await Promise.all(starts.map(async (s) => {
+ const id = s.id ?? (await findNode(s.path));
+ return id ? { ...s, id } : s;
+ }));
+ const found = located.find((s) => s.id);
+ if (found) return { starts: located, centre: found, walked: false };
+ const tried = new Set(located.map((s) => s.path));
+ for (const hit of bm25.filter((h) => !tried.has(h.path)).slice(0, tries)) {
+ const id = await findNode(hit.path);
+ if (!id) continue;
+ const centre: StartingPoint = {
+ token: hit.path, id, name: posix.basename(hit.path), kind: "file", path: hit.path, via: "bm25 fallback",
+ };
+ return { starts: [...located, centre], centre, walked: true };
+ }
+ return { starts: located, walked: false };
+}
+
export interface RankedFile {
path: string;
/** BM25 against the issue, boosted by closeness to a starting point. */