Skip to content

Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.

Tighten crux selection: retry skipped nodes, cap over-long cruxes to leading slice

anirudhkumar-nanonets6b7894c698b3
diff --git a/src/ai/crux.ts b/src/ai/crux.ts
index 7ba70fc6..1c5a5a4d 100644
--- a/src/ai/crux.ts
+++ b/src/ai/crux.ts
@@ -53,8 +53,8 @@ Return STRICT JSON, exactly:
Rules:
- Emit exactly one entry per target id given, using that id verbatim.
- summary: ONE sentence — what the symbol is FOR at the business-logic level. Say what problem it solves or rule it enforces, not what its signature already says.
-- crux_start / crux_end: FILE line numbers (as shown), and they MUST fall inside that symbol's own line range. Pick the smallest contiguous span carrying the core branch, formula, guard, or state change — not boilerplate, logging, or plumbing.
-- If a symbol has no meaningful crux (trivial getter, data holder, one-line delegation), use "crux_start": 0 and "crux_end": 0.
+- crux_start / crux_end: FILE line numbers (as shown), inside that symbol's own line range. Pick the SINGLE most important contiguous span — the core branch, formula, guard, or state change. Keep it TIGHT: at most ~8 lines, and NEVER the whole function. If you can't narrow it below that, the symbol has no distinct crux — use 0/0.
+- Skip boilerplate, logging, and plumbing. If a symbol has no meaningful crux (trivial getter, data holder, one-line delegation, or logic spread evenly with no focal point), use "crux_start": 0 and "crux_end": 0.
- Output ONLY the JSON object. No prose, no code fences.`;
/** Cap the file text sent per request so one huge file can't blow the context. */
diff --git a/src/graph/enrich.ts b/src/graph/enrich.ts
index 7acd892f..9d91fc61 100644
--- a/src/graph/enrich.ts
+++ b/src/graph/enrich.ts
@@ -22,6 +22,9 @@
import type { CruxSummarizer, NodeCrux, NodeRef } from "../ai/crux.js";
import type { Crux, NodeV1 } from "./types.js";
+/** Cap on the stored crux: an over-long pick is trimmed to its leading slice. */
+const MAX_CRUX_LINES = 12;
+
export interface EnrichOptions {
/** When present, (re)compute meaning for stale/pending nodes. Absent → cache only. */
summarizer?: CruxSummarizer;
@@ -96,17 +99,14 @@ export async function enrichGraph(
return { id: n.id, kind: n.kind, signature: n.signature, startLine, endLine };
});
- let results: Map<string, NodeCrux>;
- try {
- const list = await opts.summarizer.describeFile({ path, source, nodes: refs });
- results = new Map(list.map((r) => [r.id, r]));
- } catch (err) {
+ const { results, error } = await collectFileCrux(opts.summarizer, path, source, refs);
+ if (error) stats.errors.push(`${path}: ${error}`);
+ if (results.size === 0) {
// whole-file call failed: leave every node as-is (stale hint or pending).
for (const node of fileNodes) {
if (node.summary_state === "stale") stats.stale++;
else stats.pending++;
}
- stats.errors.push(`${path}: ${err instanceof Error ? err.message : String(err)}`);
continue;
}
@@ -127,16 +127,50 @@ export async function enrichGraph(
return stats;
}
+/**
+ * Describe every requested definition in a file, re-asking for any the model
+ * omits (it sometimes drops entries from a batch). Returns whatever it collected
+ * plus the last error, if any — partial results are kept, not discarded.
+ */
+async function collectFileCrux(
+ summarizer: CruxSummarizer,
+ path: string,
+ source: string,
+ refs: NodeRef[],
+): Promise<{ results: Map<string, NodeCrux>; error?: string }> {
+ const results = new Map<string, NodeCrux>();
+ let missing = refs;
+ let error: string | undefined;
+ for (let attempt = 0; attempt < 2 && missing.length > 0; attempt++) {
+ try {
+ const list = await summarizer.describeFile({ path, source, nodes: missing });
+ for (const r of list) if (!results.has(r.id)) results.set(r.id, r);
+ missing = refs.filter((r) => !results.has(r.id));
+ } catch (err) {
+ error = err instanceof Error ? err.message : String(err);
+ break;
+ }
+ }
+ return { results, error };
+}
+
/**
* Cut the crux text verbatim from the file using the model's file-absolute line
* range, clamped to the node's own span. Returns null when the model reported no
* distinct crux (0/0) or gave an unusable range.
*/
function buildCrux(r: NodeCrux, node: NodeV1, source: string, lineCount: number): Crux | null {
+ // 0/0 (or an invalid range) is the model saying "nothing to highlight here".
if (r.crux_start < 1 || r.crux_end < r.crux_start) return null;
const [nodeStart, nodeEnd] = spanLines(node.span, lineCount);
const start = Math.max(nodeStart, Math.min(r.crux_start, nodeEnd));
- const end = Math.max(start, Math.min(r.crux_end, nodeEnd));
+ let end = Math.max(start, Math.min(r.crux_end, nodeEnd));
+
+ // Keep the crux readable: if the model pointed at a big region, store its
+ // leading slice (the anchor it chose) rather than the whole blob — still the
+ // most important part, and the full definition is reachable via node.span.
+ if (end - start + 1 > MAX_CRUX_LINES) end = start + MAX_CRUX_LINES - 1;
+
const code = source.split("\n").slice(start - 1, end).join("\n");
if (!code.trim()) return null;
return { code, span: `L${start}-L${end}` };