Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
fix(deep): coerce stringified concept-pass nodes and stop ticking empty batches (#395)
Frankie-Xu4c41fe47a91e
diff --git a/src/ai/synthesize.ts b/src/ai/synthesize.tsindex 59c9515d..3734be69 100644--- a/src/ai/synthesize.ts+++ b/src/ai/synthesize.ts@@ -123,6 +123,95 @@ function clean(nodes: unknown): SynthNode[] {return out;}+/**+ * A forced `record_graph` call can still deliver `nodes` as a JSON *string*+ * (double-encoded args). `clean` would then return [] and the CLI ticked+ * `✓ concepts: 0 nodes` (#383). Parse, then salvage balanced `{"name":…}`+ * objects if a stray brace breaks the string. Do not reuse recover-tool —+ * that path only runs when there is no tool-call args object.+ */+function coerceRecordGraphNodes(nodes: unknown, toolCalls: number): unknown[] {+ if (Array.isArray(nodes)) return nodes;+ if (typeof nodes !== "string") return [];+ let parsed: unknown;+ let salvaged = 0;+ try {+ parsed = JSON.parse(nodes);+ } catch {+ const objs = salvageNamedObjects(nodes);+ parsed = objs;+ salvaged = objs.length;+ }+ const values = Array.isArray(parsed) ? parsed : [];+ const salvageBit = salvaged > 0 ? ` — salvaged ${salvaged} node(s) from malformed JSON` : "";+ console.error(+ `⚠ synthesize: tool call returned "nodes" as a ${nodes.length}-char string, not an array; ${toolCalls} tool call(s)${salvageBit}`,+ );+ return values;+}++/** Pull complete `{"name":…}` objects out of a near-JSON string. */+function salvageNamedObjects(raw: string): Record<string, unknown>[] {+ const out: Record<string, unknown>[] = [];+ for (let i = 0; i < raw.length; ) {+ const start = raw.indexOf("{", i);+ if (start === -1) break;+ const end = balancedClose(raw, start);+ if (end === -1) {+ i = start + 1;+ continue;+ }+ try {+ const obj = JSON.parse(raw.slice(start, end + 1)) as unknown;+ if (obj && typeof obj === "object" && !Array.isArray(obj) && typeof (obj as { name?: unknown }).name === "string") {+ out.push(obj as Record<string, unknown>);+ }+ } catch {+ /* slice was not a complete object */+ }+ i = end + 1;+ }+ return out;+}++function balancedClose(raw: string, start: number): number {+ let depth = 0;+ let inStr = false;+ let escape = false;+ for (let i = start; i < raw.length; i++) {+ const c = raw[i]!;+ if (inStr) {+ if (escape) {+ escape = false;+ continue;+ }+ if (c === "\\") {+ escape = true;+ continue;+ }+ if (c === '"') inStr = false;+ continue;+ }+ if (c === '"') {+ inStr = true;+ continue;+ }+ if (c === "{") depth++;+ else if (c === "}") {+ depth--;+ if (depth === 0) return i;+ }+ }+ return -1;+}++function shapeOfNodes(nodes: unknown): string {+ if (typeof nodes === "string") return `"nodes" is string, ${nodes.length} chars`;+ if (Array.isArray(nodes)) return `"nodes" is array, ${nodes.length} entries`;+ if (nodes === undefined) return `"nodes" is undefined`;+ return `"nodes" is ${typeof nodes}`;+}+const RECORD_TOOL = "record_graph";/** Synthesizer backed by any {@link ChatModel} via forced tool calling. */@@ -147,7 +236,12 @@ export class ChatSynthesizer implements Synthesizer {{ role: "user", content: userContent(files) },],});- return clean(nodesFromResponse(res)?.nodes);+ const raw = nodesFromResponse(res)?.nodes;+ const out = clean(coerceRecordGraphNodes(raw, res.toolCalls.length));+ if (out.length === 0) {+ console.error(`⚠ synthesize: empty batch (${shapeOfNodes(raw)}; ${res.toolCalls.length} tool call(s))`);+ }+ return out;}}diff --git a/src/cli.ts b/src/cli.tsindex 5c82ed36..7784da7c 100644--- a/src/cli.ts+++ b/src/cli.ts@@ -640,9 +640,9 @@ grouped(program.command("build"), GROUP.setup)throw err;});process.stderr.write("\n");- console.log(- `✓ concepts: ${c.nodes} nodes, ${c.links} links from ${c.files} files (${c.summarized} read, ${c.cached} cached)`,- );+ const conceptLine = `concepts: ${c.nodes} nodes, ${c.links} links from ${c.files} files (${c.summarized} read, ${c.cached} cached)`;+ if (c.nodes === 0) console.error(`⚠ ${conceptLine}`);+ else console.log(`✓ ${conceptLine}`);for (const e of c.errors) console.error(`✗ ${e}`);conceptErrors = c.errors;conceptFatal = c.fatal;diff --git a/test/synthesize-clean.test.ts b/test/synthesize-clean.test.tsnew file mode 100644index 00000000..34e0d66c--- /dev/null+++ b/test/synthesize-clean.test.ts@@ -0,0 +1,108 @@+/**+ * #383: a forced record_graph call whose `nodes` argument is a JSON *string*+ * (double-encoded, sometimes with a stray `}`) is dropped by `clean()`'s+ * `Array.isArray` guard. warnToolChoiceIgnored does not fire — there was a+ * real tool call — so the CLI prints `✓ concepts: 0 nodes`.+ */+import { test } from "node:test";+import assert from "node:assert/strict";+import { ChatSynthesizer } from "../src/ai/synthesize.js";+import type { ChatModel, ChatRequest, ChatResponse, ToolCall } from "../src/ai/llm/types.js";++class FakeChatModel implements ChatModel {+ readonly label = "fake:synthesize-clean";+ last?: ChatRequest;+ constructor(private reply: { text?: string; toolCalls?: ToolCall[] }) {}+ async create(req: ChatRequest): Promise<ChatResponse> {+ this.last = req;+ return {+ text: this.reply.text ?? "",+ toolCalls: this.reply.toolCalls ?? [],+ usage: { input: 0, output: 0, cacheRead: 0, cacheCreate: 0 },+ stopReason: "tool_calls",+ assistant: { role: "assistant", content: this.reply.text ?? "", toolCalls: this.reply.toolCalls },+ };+ }+}++const GRAPH = {+ name: "Graph Engine",+ type: "system",+ summary: "Builds the code graph from tree-sitter.",+ sources: ["src/graph/build.ts"],+ links: [] as [],+};+const PARSE = {+ name: "Parse",+ type: "concept",+ summary: "Depth-tier extract walks one file at a time.",+ sources: ["src/graph/extract.ts"],+ links: [{ to: "Graph Engine", relation: "part_of" }],+};++/** 12k+ double-encoded payload with a stray `}` between the two objects (#383 capture). */+function stringifiedNodesWithStrayBrace(): string {+ const fat = { ...GRAPH, summary: GRAPH.summary + " " + "x".repeat(12_000) };+ const inner = JSON.stringify([fat, PARSE]);+ const seam = inner.indexOf("},{");+ if (seam < 0) throw new Error("expected two-object JSON array");+ const broken = `${inner.slice(0, seam + 1)}}${inner.slice(seam + 1)}`;+ assert.ok(broken.length >= 12_000, `fixture must be 12k+ chars, got ${broken.length}`);+ assert.throws(() => JSON.parse(broken), "stray brace must make JSON.parse fail");+ return broken;+}++test("#383: a 12k stringified nodes payload with a stray brace still yields nodes", async () => {+ const raw = stringifiedNodesWithStrayBrace();+ const m = new FakeChatModel({+ toolCalls: [{ id: "1", name: "record_graph", args: { nodes: raw } }],+ });+ const { result, err } = await withCapturedError(() =>+ new ChatSynthesizer(m).synthesize([{ path: "src/graph/build.ts", summary: "x" }]),+ );+ assert.ok(result.length >= 2, `expected salvaged nodes, got ${result.map((n) => n.name).join(",") || "(none)"}`);+ assert.ok(result.some((n) => n.name === GRAPH.name));+ assert.ok(result.some((n) => n.name === PARSE.name));+ assert.ok(+ err.some((l) => /synthesize:/.test(l) && /string/.test(l) && /12000|12\d{3}/.test(l)),+ `must warn with type and char count, got: ${err.join(" | ") || "(silence)"}`,+ );+ assert.ok(+ err.some((l) => /salvaged|2/.test(l) && /tool call/.test(l)),+ `must mention salvage count and tool-call count, got: ${err.join(" | ")}`,+ );+ assert.ok(!err.some((l) => /✓/.test(l)), "must not tick a successful empty batch");+});++test("#383: a genuine empty nodes array stays 0 but does not look like success", async () => {+ const m = new FakeChatModel({+ toolCalls: [{ id: "1", name: "record_graph", args: { nodes: [] } }],+ });+ const { result, err } = await withCapturedError(() =>+ new ChatSynthesizer(m).synthesize([{ path: "a.ts", summary: "x" }]),+ );+ assert.deepEqual(result, []);+ assert.ok(+ err.some((l) => /synthesize:/.test(l) && /empty/.test(l)),+ `empty array must warn, not stay silent, got: ${err.join(" | ") || "(silence)"}`,+ );+ assert.ok(!err.some((l) => /✓/.test(l)), "0-node batch must not print a success tick");+});++async function withCapturedError<T>(fn: () => Promise<T>): Promise<{ result: T; err: string[] }> {+ const err: string[] = [];+ const origErr = console.error;+ const origLog = console.log;+ console.error = (...args: unknown[]) => {+ err.push(args.map((a) => String(a)).join(" "));+ };+ console.log = (...args: unknown[]) => {+ err.push(args.map((a) => String(a)).join(" "));+ };+ try {+ return { result: await fn(), err };+ } finally {+ console.error = origErr;+ console.log = origLog;+ }+}