Skip to content

armgit / Ix

Public Imported from GitHub
0

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

Merge origin/main into fix/forge-hr-01

KageBinary145f5d16274a
diff --cc ix-cli/src/cli/commands/callers.ts
index 3fa1485f,377d8da6..eab7a9ab
--- a/ix-cli/src/cli/commands/callers.ts
+++ b/ix-cli/src/cli/commands/callers.ts
@@@ -111,13 -113,20 +117,20 @@@ export function registerCallersCommand(
// Calls first and the definition out: an unranked fallback led with
// import lines and the target's own signature.
const ranked = rankTextUses(
- allTextResults.map((r) => ({ ...r, snippet: r.attrs.snippet as string })),
+ allTextResults.map((r) => ({ ...r, snippet: r.fullLine as string })),
target.name,
- ).map(({ snippet: _snippet, ...r }) => r);
+ ).map(({ snippet: _snippet, fullLine: _fullLine, ...r }) => r);
const candidatesFound = ranked.length;
- const textResults = ranked.slice(0, 10);
+ // The ranking is a stable sort of a sorted list, so pages neither
+ // overlap nor skip a row.
+ const page = sliceRanked(ranked, TEXT_FALLBACK_PAGE, undefined, offset);
+ if (page.nextOffset !== undefined) page.next = nextPageCommand(cmd, [symbol], page.nextOffset);
+ const textResults = page.rows;
+ const pastEnd = candidatesFound > 0 && textResults.length === 0;
+ const pastEndMessage = `${candidatesFound} text candidates; --offset ${offset} is past the end.`;
+ const pageHint = page.truncated ? truncationHint(page, "text candidates") : undefined;
- if (textResults.length > 0) {
+ if (candidatesFound > 0) {
if (opts.format === "llm") {
console.log(llmLine("callers", [
["target", target.name], ["source", "text"],
diff --cc ix-cli/src/cli/commands/text.ts
index c31991ad,30edd278..a6a954e8
--- a/ix-cli/src/cli/commands/text.ts
+++ b/ix-cli/src/cli/commands/text.ts
@@@ -10,74 -11,56 +11,116 @@@ import { stderr } from "../stderr.js"
import { llmError } from "../llm.js";
import { currentRunSignal } from "../../client/run-signal.js";
- type RunRipgrep = (args: string[], limit: number) => Promise<{ stdout: string }>;
+ /**
+ * Runs ripgrep and returns at most `limit` match lines. `matched`, when given,
+ * counts every match record within the per-file `--max-count`, kept or not.
+ * `capped` says some file has more matches than `--max-count` let through, so
+ * the scan left matches out. The command sorts whatever comes back, so an
+ * injected executor's order does not matter.
+ */
+ type RunRipgrep = (args: string[], limit: number) => Promise<{ stdout: string; matched?: number; capped?: boolean }>;
+/**
+ * Max characters of context kept in one `ix text` snippet. A single minified
+ * bundle or lockfile line can be tens of KB; one row must never flood the
+ * agent's context. The match itself stays whole (up to this same cap).
+ */
+export const SNIPPET_MAX_CHARS = 200;
+
+/**
+ * Trim a ripgrep line to a window of about `cap` characters around the first
+ * submatch, with `…` on each cut side. Lines at or under the cap are returned
+ * unchanged. `submatches` carry UTF-8 byte offsets (rg JSON), not string indices.
+ * A match longer than the cap keeps its first `cap` characters plus `…`.
+ */
+export function clipSnippet(
+ text: string,
+ submatches?: Array<{ start?: number; end?: number }>,
+ cap = SNIPPET_MAX_CHARS,
+): string {
+ const eol = /\r?\n$/.exec(text)?.[0] ?? "";
+ const body = eol ? text.slice(0, text.length - eol.length) : text;
+ if (body.length <= cap) return text;
+
+ const bytes = Buffer.from(text, "utf8");
+ const toIndex = (n: number) => Math.min(bytes.subarray(0, n).toString("utf8").length, body.length);
+ let start = 0;
+ let end = 0;
+ const m = submatches?.[0];
+ if (
+ m && Number.isInteger(m.start) && Number.isInteger(m.end) &&
+ m.start! >= 0 && m.start! <= m.end! && m.end! <= bytes.length
+ ) {
+ start = toIndex(m.start!);
+ end = Math.max(start, toIndex(m.end!));
+ }
+
+ let left: number;
+ let right: number;
+ if (end - start >= cap) {
+ left = start;
+ right = start + cap;
+ } else {
+ const budget = cap - (end - start);
+ const leftAvail = start;
+ const rightAvail = body.length - end;
+ const half = Math.floor(budget / 2);
+ const l = Math.min(leftAvail, half);
+ const r = Math.min(rightAvail, budget - l);
+ const l2 = Math.min(leftAvail, budget - r);
+ left = start - l2;
+ right = end + r;
+ }
+ // Do not split surrogate pairs.
+ const isLow = (i: number) => { const c = body.charCodeAt(i); return c >= 0xdc00 && c <= 0xdfff; };
+ const isHigh = (i: number) => { const c = body.charCodeAt(i); return c >= 0xd800 && c <= 0xdbff; };
+ if (left > 0 && left < body.length && isLow(left)) left++;
+ if (right < body.length && right > left && isHigh(right - 1)) right--;
+
+ return (left > 0 ? "…" : "") + body.slice(left, right) + (right < body.length ? "…" : eol);
+}
+
- async function runRipgrep(args: string[], limit: number): Promise<{ stdout: string }> {
+ /**
+ * Keep the best `k` of `items` under `compare`, in that order.
+ *
+ * The sort is stable, so items that compare equal keep the order they arrived
+ * in — which makes arrival the last tie-break without a sequence field.
+ */
+ export function keepTopK<T>(items: T[], k: number, compare: (a: T, b: T) => number): T[] {
+ const sorted = [...items].sort(compare);
+ return sorted.length > k ? sorted.slice(0, Math.max(k, 0)) : sorted;
+ }
+
+ /**
+ * The default executor. `root` is the workspace root matches are ranked
+ * against: the band regexes have to see workspace-relative paths, or a root
+ * under a `tests/` directory would put every match in the test band.
+ *
+ * ripgrep is asked for one match per file more than `--max-count` says. That
+ * extra match is never kept or counted; it only shows the file had more. A
+ * file with exactly `--max-count` matches looks the same as a cut-off one
+ * otherwise, and calling it cut off told every `--limit 1` search to narrow.
+ */
+ async function runRipgrep(args: string[], limit: number, root: string): Promise<{ stdout: string; matched: number; capped: boolean }> {
+ const { argv, perFileCap } = probePerFileCap(args);
+ const perFile = new Map<string, number>();
+ let capped = false;
return new Promise((resolve, reject) => {
// Killed when an `ix mcp` tool call that runs it times out (run-signal.ts).
- const child = spawn("rg", args, { stdio: ["ignore", "pipe", "pipe"], signal: currentRunSignal() });
+ const child = spawn("rg", argv, { stdio: ["ignore", "pipe", "pipe"], signal: currentRunSignal() });
const lines = createInterface({ input: child.stdout, crlfDelay: Infinity });
- const matches: string[] = [];
+ // The best `limit` matches seen so far, not the first `limit` to arrive:
+ // ripgrep's walker is parallel, so which matches arrive first changes from
+ // run to run. Ranking every match keeps the output byte-stable. The buffer
+ // is cut back to `limit` whenever it reaches twice that, so memory stays
+ // bounded however many matches there are, and once it has been cut a
+ // match that ranks no better than the last row kept is dropped unseen
+ // (a later arrival loses a tie, as it would in the stable sort).
+ let kept: Array<{ key: RankKey; line: string }> = [];
+ let worst: RankKey | undefined;
+ const byKey = (a: { key: RankKey }, b: { key: RankKey }) => compareRankKeys(a.key, b.key);
+ const relative = new Map<string, { path: string; band: number }>();
+ let matched = 0;
let errorOutput = "";
child.stderr.setEncoding("utf8");
@@@ -110,6 -109,118 +169,118 @@@
});
}
+ /**
+ * `args` with its own `--max-count` (the one before `--`; anything after is
+ * the caller's term and path) raised by one, and the cap it had. A cap of 0
+ * or none at all is left as it is: there is nothing to tell apart.
+ */
+ function probePerFileCap(args: string[]): { argv: string[]; perFileCap: number } {
+ const separator = args.indexOf("--");
+ const flags = separator >= 0 ? args.slice(0, separator) : args;
+ const capIndex = flags.indexOf("--max-count");
+ const cap = capIndex >= 0 ? Number(flags[capIndex + 1]) : NaN;
+ if (!Number.isSafeInteger(cap) || cap <= 0) {
+ return { argv: args, perFileCap: Number.isFinite(cap) ? cap : Infinity };
+ }
+ const argv = [...args];
+ argv[capIndex + 1] = String(cap + 1);
+ return { argv, perFileCap: cap };
+ }
+
+ /**
+ * What a match ranks by: band, then workspace-relative path, then line. `file`
+ * is ripgrep's own name for the file (see `ripgrepPath`) and only breaks ties
+ * between two files whose decoded paths are the same string.
+ */
+ interface RankKey {
+ band: number;
+ path: string;
+ line: number;
+ file?: string;
+ }
+
+ function compareRankKeys(a: RankKey, b: RankKey): number {
+ if (a.band !== b.band) return a.band - b.band;
+ if (a.path !== b.path) return a.path < b.path ? -1 : 1;
+ if (a.line !== b.line) return a.line - b.line;
+ const fileA = a.file ?? "";
+ const fileB = b.file ?? "";
+ return fileA === fileB ? 0 : fileA < fileB ? -1 : 1;
+ }
+
+ /**
+ * A path from a ripgrep `--json` record. ripgrep sends a path that is not
+ * valid UTF-8 as `{"bytes": <base64>}` instead of `{"text": ...}`. `id` tells
+ * files apart exactly (it keys the per-file `--max-count` count); `name` is
+ * the path to show and rank by, with bytes that are not UTF-8 decoded to
+ * U+FFFD. Reading only `text` gave every such file the name "", so they all
+ * counted as one file and printed as the workspace root.
+ */
+ function ripgrepPath(p: any): { id: string; name: string } {
+ if (typeof p?.text === "string") return { id: `t:${p.text}`, name: p.text };
+ if (typeof p?.bytes === "string") {
+ return { id: `b:${p.bytes}`, name: Buffer.from(p.bytes, "base64").toString("utf8") };
+ }
+ return { id: "", name: "" };
+ }
+
+ // ripgrep's `--json` match records start with these fields in this order. A
+ // line that does not fit (a non-UTF-8 path arrives as `bytes`, say) falls back
+ // to a full parse; nothing is ranked on a guess.
+ const MATCH_HEAD = /^\{"type":"match","data":\{"path":\{"text":("(?:[^"\\]|\\.)*")\}/;
+ const LINE_NUMBER = /"line_number":(\d+)/;
+
+ /**
+ * The path and line of a ripgrep `match` record, or undefined for any other
+ * line. Most records are read with two regexes instead of a full JSON.parse:
+ * ripgrep is drained to exit and every match is ranked, so on a dense term
+ * the parse was most of the cost.
+ */
+ function matchRankFields(line: string): { file: string; name: string; line: number } | undefined {
+ const head = MATCH_HEAD.exec(line);
+ const number = head ? LINE_NUMBER.exec(line) : null;
+ if (head && number) {
+ try {
+ const { id, name } = ripgrepPath({ text: JSON.parse(head[1]) });
+ return { file: id, name, line: Number(number[1]) };
+ } catch {
+ // fall through to the full parse
+ }
+ }
+ let parsed: any;
+ try {
+ parsed = JSON.parse(line);
+ } catch {
+ return undefined;
+ }
+ if (parsed?.type !== "match") return undefined;
+ const { id, name } = ripgrepPath(parsed.data?.path);
+ return { file: id, name, line: parsed.data?.line_number ?? 0 };
+ }
+
+ /** A path ripgrep printed, relative to `root` with forward slashes. */
+ function workspaceRelativePath(filePath: string, root: string): string {
+ const absoluteFilePath = path.isAbsolute(filePath)
+ ? filePath
+ : path.resolve(process.cwd(), filePath);
+ return path.relative(root, absoluteFilePath).split(path.sep).join("/");
+ }
+
-/** One ripgrep `match` record as a result, its path relative to `root`. */
++/** One ripgrep `match` record as a result, its path relative to `root` and its snippet clipped (clipSnippet). */
+ function textResultFromMatch(data: any, root: string): TextResult {
+ const filePath = ripgrepPath(data?.path).name;
+ const lineNum = data?.line_number ?? 0;
+ return {
+ path: workspaceRelativePath(filePath, root),
+ line_start: lineNum,
+ line_end: lineNum,
- snippet: data?.lines?.text ?? "",
++ snippet: clipSnippet(data?.lines?.text ?? "", data?.submatches),
+ engine: "ripgrep",
+ score: 1.0,
+ language: inferLanguage(filePath),
+ };
+ }
+
export function resolveTextSearchPath(root: string, searchPath: string): string {
return path.resolve(root, searchPath);
}