Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

fix(client): keep AI Analysis findings on long traces; stop double-counting coding-agent cost in the dashboard (#1879)

Aman Agarwal7f8722e536c1
diff --git a/src/client/src/__tests__/clickhouse/coding-agents-dashboard-seed.test.ts b/src/client/src/__tests__/clickhouse/coding-agents-dashboard-seed.test.ts
new file mode 100644
index 00000000..50be33b7
--- /dev/null
+++ b/src/client/src/__tests__/clickhouse/coding-agents-dashboard-seed.test.ts
@@ -0,0 +1,23 @@
+import codingAgentsDashboard from "@/clickhouse/seed-data/openlit-dashboard-coding-agents-layout.json";
+
+describe("Coding Agents dashboard seed — Total cost (USD)", () => {
+ const widgets = Object.values(
+ (codingAgentsDashboard as { widgets: Record<string, { title: string; config: { query: string } }> }).widgets
+ );
+ const totalCost = widgets.find((widget) => widget.title === "Total cost (USD)");
+
+ it("exists", () => {
+ expect(totalCost).toBeDefined();
+ });
+
+ it("does not add the session root's gen_ai.usage.cost on top of its per-turn costs", () => {
+ const query = totalCost!.config.query;
+ const sessionCosts = query.match(/[\s\S]{0,400}AS session_cost/g) ?? [];
+ expect(sessionCosts.length).toBeGreaterThanOrEqual(2);
+ for (const expr of sessionCosts) {
+ expect(expr).toMatch(/SpanName = 'coding_agent\.session'/);
+ expect(expr).toMatch(/SpanName != 'coding_agent\.session', toFloat64OrZero\(SpanAttributes\['gen_ai\.usage\.cost'\]\)/);
+ expect(expr).not.toMatch(/greatest\(/);
+ }
+ });
+});
diff --git a/src/client/src/__tests__/lib/platform/chat/improvement.test.ts b/src/client/src/__tests__/lib/platform/chat/improvement.test.ts
index bb9c21ea..2d357411 100644
--- a/src/client/src/__tests__/lib/platform/chat/improvement.test.ts
+++ b/src/client/src/__tests__/lib/platform/chat/improvement.test.ts
@@ -571,6 +571,41 @@ describe("streamTraceImprovementAnalysis", () => {
expect(events.find((event) => event.type === "done")).toBeDefined();
});
+ it("keeps complete findings from a dimension response truncated mid-JSON", async () => {
+ (getHeirarchyViaSpanId as jest.Mock).mockResolvedValue({
+ record: hierarchy,
+ err: null,
+ });
+ (dataCollector as jest.Mock)
+ .mockResolvedValueOnce({ data: [], err: null })
+ .mockResolvedValueOnce({ err: null });
+ const complete = JSON.stringify({
+ summary: "Long session",
+ findings: [
+ { severity: "major", summary: "Context grew every turn", detail: "d", span_refs: ["root-span"] },
+ { severity: "minor", summary: "Second finding that gets cut", detail: "d", span_refs: ["root-span"] },
+ ],
+ });
+ const truncated = complete.slice(0, complete.indexOf("Second finding") + 6);
+ (streamText as jest.Mock).mockImplementation(({ onFinish }) => {
+ onFinish({ usage: { inputTokens: 1, outputTokens: 1 } });
+ return {
+ fullStream: (async function* () {
+ yield { type: "text-delta", delta: "```json\n" + truncated };
+ })(),
+ };
+ });
+
+ const result = await streamTraceImprovementAnalysis("root-span", "db-1");
+ const events = await readNdjson(result.response!);
+ const firstDimension = events.find((event) => event.type === "dimension");
+
+ expect(firstDimension.findings.map((finding: { summary: string }) => finding.summary)).toEqual([
+ "Context grew every turn",
+ ]);
+ expect(streamText).toHaveBeenCalledWith(expect.objectContaining({ maxOutputTokens: 4000 }));
+ });
+
it("returns the default chat-not-configured message when config is empty", async () => {
(getChatConfigWithApiKey as jest.Mock).mockResolvedValue({ data: null });
diff --git a/src/client/src/__tests__/lib/platform/chat/json-recovery.test.ts b/src/client/src/__tests__/lib/platform/chat/json-recovery.test.ts
new file mode 100644
index 00000000..ea9d4d4d
--- /dev/null
+++ b/src/client/src/__tests__/lib/platform/chat/json-recovery.test.ts
@@ -0,0 +1,47 @@
+import { recoverTruncatedJson } from "@/lib/platform/chat/json-recovery";
+
+const full = JSON.stringify({
+ summary: "Two findings",
+ findings: [
+ { severity: "major", summary: "First", span_refs: ["a", "b"] },
+ { severity: "minor", summary: "Second \"quoted\" {brace}", span_refs: ["c"] },
+ ],
+});
+
+describe("recoverTruncatedJson", () => {
+ it("returns complete JSON unchanged", () => {
+ expect(recoverTruncatedJson(full)).toEqual(JSON.parse(full));
+ });
+
+ it("keeps complete findings when cut inside a later finding's string", () => {
+ const cut = full.slice(0, full.indexOf("Second") + 4);
+ expect(recoverTruncatedJson(cut)).toEqual({
+ summary: "Two findings",
+ findings: [{ severity: "major", summary: "First", span_refs: ["a", "b"] }],
+ });
+ });
+
+ it("ignores brackets and escaped quotes inside strings", () => {
+ const cut = full.slice(0, full.lastIndexOf("span_refs"));
+ const recovered = recoverTruncatedJson(cut) as { findings: unknown[] };
+ expect(recovered.findings).toHaveLength(1);
+ });
+
+ it("returns null when the first finding is cut inside its nested array", () => {
+ const cut = full.slice(0, full.indexOf('"a"') + 3);
+ expect(recoverTruncatedJson(cut)).toBeNull();
+ });
+
+ it("returns null when nothing complete precedes the cut", () => {
+ expect(recoverTruncatedJson('{"summary": "Session consumed ~9.6M tok')).toBeNull();
+ expect(recoverTruncatedJson("not json")).toBeNull();
+ });
+
+ it("returns null on mismatched brackets", () => {
+ expect(recoverTruncatedJson('{"findings": [}]')).toBeNull();
+ });
+
+ it("ignores trailing text after a complete document", () => {
+ expect(recoverTruncatedJson('{"a": 1} trailing')).toEqual({ a: 1 });
+ });
+});
diff --git a/src/client/src/clickhouse/seed-data/openlit-dashboard-coding-agents-layout.json b/src/client/src/clickhouse/seed-data/openlit-dashboard-coding-agents-layout.json
index e324b4d3..d459bd1e 100644
--- a/src/client/src/clickhouse/seed-data/openlit-dashboard-coding-agents-layout.json
+++ b/src/client/src/clickhouse/seed-data/openlit-dashboard-coding-agents-layout.json
@@ -45,7 +45,7 @@
"9a1f1c20-3b27-4f88-9d51-1aaa11111113": {
"id": "9a1f1c20-3b27-4f88-9d51-1aaa11111113",
"title": "Total cost (USD)",
- "description": "Per-session cost rolled up: greatest of the authoritative coding_agent.session.cost_usd and the sum of per-turn gen_ai.usage.cost. Matches the Sessions list, the Users tab, and the agents-hub Coding Agents card.",
+ "description": "Per-session cost rolled up: the authoritative coding_agent.session.cost_usd when present, otherwise the sum of per-turn gen_ai.usage.cost. Matches the Sessions list, the Users tab, and the agents-hub Coding Agents card.",
"type": "STAT_CARD",
"properties": {
"value": "0.total_cost",
@@ -55,7 +55,7 @@
"prefix": "$"
},
"config": {
- "query": "WITH\n parseDateTimeBestEffort('{{filter.timeLimit.start}}') AS start_time,\n parseDateTimeBestEffort('{{filter.timeLimit.end}}') AS end_time,\n (end_time - start_time) AS duration,\n (start_time - duration) AS prev_start_time,\n (end_time - duration) AS prev_end_time,\n -- Canonical per-session cost. Same formula listSessions /\n -- listCodingUsers / the agents-hub materializer use. Without\n -- the greatest() this widget silently under-counts: sessions\n -- whose only cost signal is the session-end stamp\n -- (coding_agent.session.cost_usd, set on Stop / SessionEnd by\n -- Claude Code and Codex) would report $0 because no per-turn\n -- gen_ai.usage.cost was ever emitted.\n cost_now AS (\n SELECT\n coalesce(\n nullIf(\n if(\n notEmpty(ResourceAttributes['coding_agent.agent.parent_id'])\n AND ResourceAttributes['coding_agent.agent.parent_id'] != coalesce(\n nullIf(ResourceAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['coding_agent.session.id'], ''),\n nullIf(ResourceAttributes['coding_agent.session.id'], ''),\n ''\n ),\n ResourceAttributes['coding_agent.agent.parent_id'],\n ''\n ),\n ''\n ),\n nullIf(\n if(\n notEmpty(SpanAttributes['coding_agent.agent.parent_id'])\n AND SpanAttributes['coding_agent.agent.parent_id'] != coalesce(\n nullIf(ResourceAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['coding_agent.session.id'], ''),\n nullIf(ResourceAttributes['coding_agent.session.id'], ''),\n ''\n ),\n SpanAttributes['coding_agent.agent.parent_id'],\n ''\n ),\n ''\n ),\n nullIf(ResourceAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['coding_agent.session.id'], ''),\n nullIf(ResourceAttributes['coding_agent.session.id'], '')\n ) AS chat_id,\n coalesce( nullIf(SpanAttributes['coding_agent.client'], ''), nullIf(SpanAttributes['gen_ai.agent.name'], ''), nullIf(ResourceAttributes['gen_ai.agent.name'], ''), nullIf(ResourceAttributes['service.name'], ''), 'unknown' ) AS vendor,\n coalesce(\n nullIf(any(SpanAttributes['gen_ai.user.name']), ''),\n nullIf(any(ResourceAttributes['gen_ai.user.name']), ''),\n nullIf(any(ResourceAttributes['service.name']), ''),\n 'unknown'\n ) AS user,\n greatest(\n toFloat64OrZero(any(SpanAttributes['coding_agent.session.cost_usd'])),\n sumOrNull(toFloat64OrZero(SpanAttributes['gen_ai.usage.cost']))\n ) AS session_cost\n FROM otel_traces\n WHERE (\n notEmpty(SpanAttributes['coding_agent.session.id'])\n OR (ResourceAttributes['service.name'] = 'claude-code'\n AND (notEmpty(SpanAttributes['session.id']) OR notEmpty(ResourceAttributes['session.id'])))\n )\n AND Timestamp >= start_time AND Timestamp <= end_time\n GROUP BY chat_id, vendor\n ),\n cost_prev AS (\n SELECT\n coalesce(\n nullIf(\n if(\n notEmpty(ResourceAttributes['coding_agent.agent.parent_id'])\n AND ResourceAttributes['coding_agent.agent.parent_id'] != coalesce(\n nullIf(ResourceAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['coding_agent.session.id'], ''),\n nullIf(ResourceAttributes['coding_agent.session.id'], ''),\n ''\n ),\n ResourceAttributes['coding_agent.agent.parent_id'],\n ''\n ),\n ''\n ),\n nullIf(\n if(\n notEmpty(SpanAttributes['coding_agent.agent.parent_id'])\n AND SpanAttributes['coding_agent.agent.parent_id'] != coalesce(\n nullIf(ResourceAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['coding_agent.session.id'], ''),\n nullIf(ResourceAttributes['coding_agent.session.id'], ''),\n ''\n ),\n SpanAttributes['coding_agent.agent.parent_id'],\n ''\n ),\n ''\n ),\n nullIf(ResourceAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['coding_agent.session.id'], ''),\n nullIf(ResourceAttributes['coding_agent.session.id'], '')\n ) AS chat_id,\n coalesce( nullIf(SpanAttributes['coding_agent.client'], ''), nullIf(SpanAttributes['gen_ai.agent.name'], ''), nullIf(ResourceAttributes['gen_ai.agent.name'], ''), nullIf(ResourceAttributes['service.name'], ''), 'unknown' ) AS vendor,\n coalesce(\n nullIf(any(SpanAttributes['gen_ai.user.name']), ''),\n nullIf(any(ResourceAttributes['gen_ai.user.name']), ''),\n nullIf(any(ResourceAttributes['service.name']), ''),\n 'unknown'\n ) AS user,\n greatest(\n toFloat64OrZero(any(SpanAttributes['coding_agent.session.cost_usd'])),\n sumOrNull(toFloat64OrZero(SpanAttributes['gen_ai.usage.cost']))\n ) AS session_cost\n FROM otel_traces\n WHERE (\n notEmpty(SpanAttributes['coding_agent.session.id'])\n OR (ResourceAttributes['service.name'] = 'claude-code'\n AND (notEmpty(SpanAttributes['session.id']) OR notEmpty(ResourceAttributes['session.id'])))\n )\n AND Timestamp >= prev_start_time AND Timestamp <= prev_end_time\n GROUP BY chat_id, vendor\n ),\n totals AS (\n SELECT\n (SELECT round(sumOrNull(session_cost), 4) FROM cost_now WHERE 1=1{{#filter.vendor}} AND vendor = '{{filter.vendor}}'{{/filter.vendor}}{{#filter.user}} AND user = '{{filter.user}}'{{/filter.user}}) AS now_total,\n (SELECT round(sumOrNull(session_cost), 4) FROM cost_prev WHERE 1=1{{#filter.vendor}} AND vendor = '{{filter.vendor}}'{{/filter.vendor}}{{#filter.user}} AND user = '{{filter.user}}'{{/filter.user}}) AS prev_total\n )\n\nSELECT\n coalesce(now_total, 0) AS total_cost,\n coalesce(prev_total, 0) AS total_cost_previous,\n if(coalesce(prev_total, 0) > 0, round((coalesce(now_total, 0) - coalesce(prev_total, 0)) * 100.0 / coalesce(prev_total, 0), 1), NULL) AS rate\nFROM totals\n"
+ "query": "WITH\n parseDateTimeBestEffort('{{filter.timeLimit.start}}') AS start_time,\n parseDateTimeBestEffort('{{filter.timeLimit.end}}') AS end_time,\n (end_time - start_time) AS duration,\n (start_time - duration) AS prev_start_time,\n (end_time - duration) AS prev_end_time,\n -- Canonical per-session cost. Same formula listSessions /\n -- listCodingUsers / the agents-hub materializer use: the\n -- session-end stamp (coding_agent.session.cost_usd) wins when\n -- present, otherwise per-turn gen_ai.usage.cost from child spans.\n -- The session root also carries gen_ai.usage.cost, so it is\n -- excluded from the sum to avoid double-counting.\n cost_now AS (\n SELECT\n coalesce(\n nullIf(\n if(\n notEmpty(ResourceAttributes['coding_agent.agent.parent_id'])\n AND ResourceAttributes['coding_agent.agent.parent_id'] != coalesce(\n nullIf(ResourceAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['coding_agent.session.id'], ''),\n nullIf(ResourceAttributes['coding_agent.session.id'], ''),\n ''\n ),\n ResourceAttributes['coding_agent.agent.parent_id'],\n ''\n ),\n ''\n ),\n nullIf(\n if(\n notEmpty(SpanAttributes['coding_agent.agent.parent_id'])\n AND SpanAttributes['coding_agent.agent.parent_id'] != coalesce(\n nullIf(ResourceAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['coding_agent.session.id'], ''),\n nullIf(ResourceAttributes['coding_agent.session.id'], ''),\n ''\n ),\n SpanAttributes['coding_agent.agent.parent_id'],\n ''\n ),\n ''\n ),\n nullIf(ResourceAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['coding_agent.session.id'], ''),\n nullIf(ResourceAttributes['coding_agent.session.id'], '')\n ) AS chat_id,\n coalesce( nullIf(SpanAttributes['coding_agent.client'], ''), nullIf(SpanAttributes['gen_ai.agent.name'], ''), nullIf(ResourceAttributes['gen_ai.agent.name'], ''), nullIf(ResourceAttributes['service.name'], ''), 'unknown' ) AS vendor,\n coalesce(\n nullIf(any(SpanAttributes['gen_ai.user.name']), ''),\n nullIf(any(ResourceAttributes['gen_ai.user.name']), ''),\n nullIf(any(ResourceAttributes['service.name']), ''),\n 'unknown'\n ) AS user,\n coalesce(\n nullIf(toFloat64OrZero(anyIf(SpanAttributes['coding_agent.session.cost_usd'], SpanName = 'coding_agent.session')), 0),\n sumOrNull(if(SpanName != 'coding_agent.session', toFloat64OrZero(SpanAttributes['gen_ai.usage.cost']), 0))\n ) AS session_cost\n FROM otel_traces\n WHERE (\n notEmpty(SpanAttributes['coding_agent.session.id'])\n OR (ResourceAttributes['service.name'] = 'claude-code'\n AND (notEmpty(SpanAttributes['session.id']) OR notEmpty(ResourceAttributes['session.id'])))\n )\n AND Timestamp >= start_time AND Timestamp <= end_time\n GROUP BY chat_id, vendor\n ),\n cost_prev AS (\n SELECT\n coalesce(\n nullIf(\n if(\n notEmpty(ResourceAttributes['coding_agent.agent.parent_id'])\n AND ResourceAttributes['coding_agent.agent.parent_id'] != coalesce(\n nullIf(ResourceAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['coding_agent.session.id'], ''),\n nullIf(ResourceAttributes['coding_agent.session.id'], ''),\n ''\n ),\n ResourceAttributes['coding_agent.agent.parent_id'],\n ''\n ),\n ''\n ),\n nullIf(\n if(\n notEmpty(SpanAttributes['coding_agent.agent.parent_id'])\n AND SpanAttributes['coding_agent.agent.parent_id'] != coalesce(\n nullIf(ResourceAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['coding_agent.session.id'], ''),\n nullIf(ResourceAttributes['coding_agent.session.id'], ''),\n ''\n ),\n SpanAttributes['coding_agent.agent.parent_id'],\n ''\n ),\n ''\n ),\n nullIf(ResourceAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['gen_ai.conversation.id'], ''),\n nullIf(SpanAttributes['coding_agent.session.id'], ''),\n nullIf(ResourceAttributes['coding_agent.session.id'], '')\n ) AS chat_id,\n coalesce( nullIf(SpanAttributes['coding_agent.client'], ''), nullIf(SpanAttributes['gen_ai.agent.name'], ''), nullIf(ResourceAttributes['gen_ai.agent.name'], ''), nullIf(ResourceAttributes['service.name'], ''), 'unknown' ) AS vendor,\n coalesce(\n nullIf(any(SpanAttributes['gen_ai.user.name']), ''),\n nullIf(any(ResourceAttributes['gen_ai.user.name']), ''),\n nullIf(any(ResourceAttributes['service.name']), ''),\n 'unknown'\n ) AS user,\n coalesce(\n nullIf(toFloat64OrZero(anyIf(SpanAttributes['coding_agent.session.cost_usd'], SpanName = 'coding_agent.session')), 0),\n sumOrNull(if(SpanName != 'coding_agent.session', toFloat64OrZero(SpanAttributes['gen_ai.usage.cost']), 0))\n ) AS session_cost\n FROM otel_traces\n WHERE (\n notEmpty(SpanAttributes['coding_agent.session.id'])\n OR (ResourceAttributes['service.name'] = 'claude-code'\n AND (notEmpty(SpanAttributes['session.id']) OR notEmpty(ResourceAttributes['session.id'])))\n )\n AND Timestamp >= prev_start_time AND Timestamp <= prev_end_time\n GROUP BY chat_id, vendor\n ),\n totals AS (\n SELECT\n (SELECT round(sumOrNull(session_cost), 4) FROM cost_now WHERE 1=1{{#filter.vendor}} AND vendor = '{{filter.vendor}}'{{/filter.vendor}}{{#filter.user}} AND user = '{{filter.user}}'{{/filter.user}}) AS now_total,\n (SELECT round(sumOrNull(session_cost), 4) FROM cost_prev WHERE 1=1{{#filter.vendor}} AND vendor = '{{filter.vendor}}'{{/filter.vendor}}{{#filter.user}} AND user = '{{filter.user}}'{{/filter.user}}) AS prev_total\n )\n\nSELECT\n coalesce(now_total, 0) AS total_cost,\n coalesce(prev_total, 0) AS total_cost_previous,\n if(coalesce(prev_total, 0) > 0, round((coalesce(now_total, 0) - coalesce(prev_total, 0)) * 100.0 / coalesce(prev_total, 0), 1), NULL) AS rate\nFROM totals\n"
},
"createdAt": "2026-05-25 11:00:00",
"updatedAt": "2026-05-28 03:30:00"
diff --git a/src/client/src/lib/platform/chat/improvement.ts b/src/client/src/lib/platform/chat/improvement.ts
index 2b4dc4a3..ec4b1cb8 100644
--- a/src/client/src/lib/platform/chat/improvement.ts
+++ b/src/client/src/lib/platform/chat/improvement.ts
@@ -4,6 +4,7 @@ import { getChatConfigWithApiKey } from "./config";
import { OPENLIT_TRACE_ANALYSIS_TABLE } from "./table-details";
import { intelligenceDataCollector } from "../common";
import { getModelInstance } from "./stream";
+import { recoverTruncatedJson } from "./json-recovery";
import { getTraceHierarchy } from "../traces/read";
import { isNativeSqlChatAvailable } from "@/lib/telemetry-source";
import { TraceHeirarchySpan, TraceRow } from "@/types/trace";
@@ -105,6 +106,11 @@ type TraceAnalysisMetrics = {
potentialRetrySequences: Array<{ reason: string; spanIds: string[] }>;
};
+// Long agent sessions produce many findings; low caps cut the JSON off
+// mid-string and lose the whole dimension.
+const DIMENSION_MAX_OUTPUT_TOKENS = 4000;
+const GRADER_MAX_OUTPUT_TOKENS = 4000;
+
const TRACE_ANALYSIS_SCHEMA = `type FixPatch = {
field: 'prompt' | 'response' | 'system'; // which span attribute contains the text to edit
span_ref: string; // must be a span ID from span_refs
@@ -1218,7 +1224,18 @@ function parseDimensionAnalysis(
.replace(/```$/i, "")
.trim();
try {
- const parsed = JSON.parse(jsonText);
+ let parsed: any;
+ try {
+ parsed = JSON.parse(jsonText);
+ } catch (error) {
+ parsed = recoverTruncatedJson(jsonText);
+ if (parsed === null) throw error;
+ logTraceAnalysis("dimension_parse_recovered", {
+ dimension,
+ rootSpanId: root.SpanId,
+ rawChars: rawText.length,
+ });
+ }
const findingsSource = Array.isArray(parsed)
? parsed
: Array.isArray(parsed?.findings)
@@ -1583,7 +1600,7 @@ export async function streamTraceImprovementAnalysis(
metrics,
ruleContext,
}),
- maxOutputTokens: 900,
+ maxOutputTokens: DIMENSION_MAX_OUTPUT_TOKENS,
model: modelInstance,
onUsage: (stats) => {
dimensionStats = stats;
@@ -1643,7 +1660,7 @@ export async function streamTraceImprovementAnalysis(
ruleContext,
firstPass: dimensionAnalysis,
}),
- maxOutputTokens: 1000,
+ maxOutputTokens: GRADER_MAX_OUTPUT_TOKENS,
onUsage: (stats) => {
graderStats = stats;
addFinishStats(stats);
diff --git a/src/client/src/lib/platform/chat/json-recovery.ts b/src/client/src/lib/platform/chat/json-recovery.ts
new file mode 100644
index 00000000..f9d077dd
--- /dev/null
+++ b/src/client/src/lib/platform/chat/json-recovery.ts
@@ -0,0 +1,50 @@
+/**
+ * Best-effort recovery for model JSON that was cut off mid-stream (for
+ * example when a response hits its output-token limit).
+ *
+ * Walks the text tracking string and bracket state, remembers the last point
+ * where a value inside an array closed cleanly, and closes the remaining open
+ * brackets from there. Complete array items (e.g. findings) survive; the
+ * partial item at the cut is dropped. Returns null when nothing usable can be
+ * salvaged.
+ */
+export function recoverTruncatedJson(text: string): unknown | null {
+ const stack: string[] = [];
+ let inString = false;
+ let escaped = false;
+ let lastSafeEnd = -1;
+ let stackAtLastSafe: string[] = [];
+
+ for (let i = 0; i < text.length; i++) {
+ const ch = text[i];
+ if (inString) {
+ if (escaped) escaped = false;
+ else if (ch === "\\") escaped = true;
+ else if (ch === '"') inString = false;
+ continue;
+ }
+ if (ch === '"') {
+ inString = true;
+ } else if (ch === "{" || ch === "[") {
+ stack.push(ch === "{" ? "}" : "]");
+ } else if (ch === "}" || ch === "]") {
+ if (stack.pop() !== ch) return null;
+ if (stack.length === 0) return tryParse(text.slice(0, i + 1));
+ if (stack[stack.length - 1] === "]") {
+ lastSafeEnd = i + 1;
+ stackAtLastSafe = [...stack];
+ }
+ }
+ }
+
+ if (lastSafeEnd < 0) return null;
+ return tryParse(text.slice(0, lastSafeEnd) + [...stackAtLastSafe].reverse().join(""));
+}
+
+function tryParse(text: string): unknown | null {
+ try {
+ return JSON.parse(text);
+ } catch {
+ return null;
+ }
+}