Skip to content

Commit 1453824

Browse files
anandgupta42claude
andcommitted
fix(analyst): bound spill memory per row and keep the routing notice under tight output limits
- The full-text spill serialiser emitted one string fragment per JSON token and held them all until the join, so a permitted wide numeric result retained millions of fragments. It now emits one bounded string per row; the tagged serialisation and the 32 MiB preflight are unchanged. - Under a tiny `tool_output.max_lines` the fail-open routing notice was clipped before its text and then overwritten. The notice is reserved first and the result and remaining notes are fitted into what is left. Tests: 2,000 × 150 numeric rows spill with identical bytes and one retained chunk per row; a full-text query under a ten-line limit keeps the routing notice. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 965f97a commit 1453824

4 files changed

Lines changed: 98 additions & 13 deletions

File tree

‎packages/opencode/src/altimate/tools/sql-execute.ts‎

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Effect } from "effect"
33
import { EffectBridge } from "../../effect/bridge"
44
import { Truncate } from "../../tool/truncate"
55
import { legacyToInit, type LegacyToolDef } from "../tool-zod-compat"
6-
import { formatSqlResult } from "./sql-result-format"
6+
import { formatSqlResult, SQL_MAX_PREVIEW_BYTES } from "./sql-result-format"
77
import { lintSqlResult } from "./sql-result-lint"
88
import { Tool } from "../../tool/tool"
99
import { Dispatcher } from "../native"
@@ -220,6 +220,25 @@ export const SqlExecuteTool = Tool.define(
220220
// altimate_change end
221221

222222
const limits = await bridge.promise(truncate.limits())
223+
const maxBytes = Math.min(limits.maxBytes, SQL_MAX_PREVIEW_BYTES)
224+
// Reserve the routing explanation before the formatter budgets optional notes.
225+
const routingNotice = fullText ? Precedence.annotate(precedence, { output: "" }).output : ""
226+
let routingPrefix = ""
227+
let routingBytes = 0
228+
let routingLines = 0
229+
for (const char of routingNotice) {
230+
const size = Buffer.byteLength(char, "utf8")
231+
if (
232+
routingBytes + size > maxBytes ||
233+
limits.maxLines < 1 ||
234+
(char === "\n" && routingLines + 1 >= limits.maxLines)
235+
)
236+
break
237+
routingPrefix += char
238+
routingBytes += size
239+
if (char === "\n") routingLines++
240+
}
241+
const routingClipped = routingPrefix !== routingNotice
223242
const suggestion = PostConnectSuggestions.getProgressiveSuggestion("sql_execute")
224243
const lint = await lintSqlResult(args.query, args.warehouse, ctx.sessionID)
225244
const formatted = await formatSqlResult(result, {
@@ -228,9 +247,14 @@ export const SqlExecuteTool = Tool.define(
228247
// The statement text lets the formatter add the empty/near-empty
229248
// result hint when a string-literal filter is present.
230249
sql: query,
231-
notes: [precedence.notice ?? "", limitNote, lint?.output ?? "", suggestion ?? ""].filter(Boolean),
232-
maxBytes: limits.maxBytes,
233-
maxLines: limits.maxLines,
250+
notes: [
251+
fullText ? "" : (precedence.notice ?? ""),
252+
limitNote,
253+
lint?.output ?? "",
254+
suggestion ?? "",
255+
].filter(Boolean),
256+
maxBytes: routingClipped ? 0 : maxBytes - routingBytes,
257+
maxLines: limits.maxLines - routingLines,
234258
write: (text) => bridge.promise(truncate.write(text)),
235259
})
236260
// altimate_change start — progressive disclosure suggestions
@@ -242,13 +266,17 @@ export const SqlExecuteTool = Tool.define(
242266
})
243267
}
244268
// altimate_change end
245-
// Preserve routing metadata while the formatter owns the bounded notice text.
269+
// The full-text routing prefix already has its own reserved output allowance.
246270
return {
247271
...Precedence.annotate(precedence, {
248272
title: `SQL: ${args.query.slice(0, 60)}${args.query.length > 60 ? "..." : ""}`,
249-
metadata: { ...formatted.metadata, ...(lint && { lint_rules: lint.rules }) },
273+
metadata: {
274+
...formatted.metadata,
275+
...(routingClipped && { output_truncated: true, truncated: true }),
276+
...(lint && { lint_rules: lint.rules }),
277+
},
250278
}),
251-
output: formatted.output,
279+
output: routingPrefix + formatted.output,
252280
}
253281
} catch (e) {
254282
const msg = e instanceof Error ? e.message : String(e)

‎packages/opencode/src/altimate/tools/sql-result-format.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,24 +339,24 @@ function* spillValue(value: unknown, ancestors = new Set<object>()): Generator<s
339339
}
340340

341341
/** Ordered columns/values retain duplicate aliases; tags preserve special values. */
342-
function serializeRows(result: SqlExecuteResult): string {
343-
const chunks: string[] = []
342+
export function serializeRows(result: SqlExecuteResult, emit: (chunk: string) => void): void {
344343
let bytes = 0
345344
for (const values of result.rows) {
346345
const row = {
347346
columns: result.columns,
348347
values: result.columns.map((_, index) => (index in values ? values[index] : null)),
349348
}
350349
checkSpillSize(row, { left: SQL_MAX_SPILL_BYTES - bytes })
350+
let text = ""
351351
for (const chunk of spillValue(row)) {
352352
bytes += Buffer.byteLength(chunk, "utf8")
353353
if (bytes + 1 > SQL_MAX_SPILL_BYTES) throw new Error("result exceeds the SQL spill size limit")
354-
chunks.push(chunk)
354+
text += chunk
355355
}
356356
bytes++
357-
chunks.push("\n")
357+
// Retain one bounded string per row instead of a reference per JSON token.
358+
emit(text + "\n")
358359
}
359-
return chunks.join("")
360360
}
361361

362362
const EMPTY_RESULT_HINT =
@@ -468,7 +468,9 @@ export async function formatSqlResult(result: SqlExecuteResult, options: FormatO
468468

469469
let spill: string
470470
try {
471-
metadata.result_path = await options.write(serializeRows(result))
471+
const chunks: string[] = []
472+
serializeRows(result, (chunk) => chunks.push(chunk))
473+
metadata.result_path = await options.write(chunks.join(""))
472474
spill = `All ${result.rows.length} returned rows saved to ${metadata.result_path}. JSONL text: columns/values per row; $bigint, $date (ISO), $bytes (base64) tags; strings unchanged. Use read with char_offset/max_chars to recover any clipped text.`
473475
} catch {
474476
spill = `Spill failed (write/serialization error or ${SQL_MAX_SPILL_BYTES / 1024 / 1024} MiB size limit); returned rows were not saved.`

‎packages/opencode/test/altimate/tools/sql-execute.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,43 @@ it.live("full text includes lint and suggestion notes in the configured output b
492492
}),
493493
)
494494

495+
const shortFullText = testEffect(
496+
Layer.mergeAll(
497+
Truncate.defaultLayer,
498+
FSUtil.defaultLayer,
499+
agents,
500+
TestConfig.layer({ get: () => Effect.succeed({ tool_output: { max_bytes: 1024, max_lines: 10 } }) }),
501+
),
502+
)
503+
504+
shortFullText.live("full text preserves the routing notice with a ten-line output limit", () =>
505+
Effect.gen(function* () {
506+
const notice = "Not routed through the selected workspace: routing was unavailable, so the query ran locally."
507+
precedence.mockResolvedValue({ notice, precedence: "undetermined" })
508+
suggestion.mockReturnValue("Tip: " + "Inspect the saved result. ".repeat(1000))
509+
const lint = spyOn(SqlResultLint, "lintSqlResult").mockResolvedValue({
510+
output: "Checks:\n" + "Review this query. ".repeat(1000),
511+
rules: ["limit_without_order_by"],
512+
})
513+
try {
514+
const tool = yield* SqlExecuteTool
515+
const initialized = yield* tool.init()
516+
const output = yield* initialized.execute({ query: "SELECT 42", full_text_columns: ["answer"], limit: 50 }, ctx)
517+
518+
expect(output.output).toStartWith(notice)
519+
expect(output.output.split("\n").length).toBeLessThanOrEqual(10)
520+
expect(Buffer.byteLength(output.output, "utf8")).toBeLessThanOrEqual(1024)
521+
expect(output.metadata.precedence).toBe("undetermined")
522+
expect(output.metadata.error).toBeUndefined()
523+
expect(dispatch).toHaveBeenCalledTimes(1)
524+
const fs = yield* FSUtil.Service
525+
yield* fs.remove(output.metadata.result_path as string)
526+
} finally {
527+
lint.mockRestore()
528+
}
529+
}),
530+
)
531+
495532
test("full text selection retains write permission checks", async () => {
496533
const tool = await initTool(SqlExecuteTool)
497534
const ask = spyOn(ctx, "ask").mockImplementation(() => Effect.die(new Error("write denied")))

‎packages/opencode/test/altimate/tools/sql-result-format.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, test } from "bun:test"
22
import type { SqlExecuteResult } from "../../../src/altimate/native/types"
33
import {
44
formatSqlResult,
5+
serializeRows,
56
SQL_MAX_PREVIEW_BYTES,
67
SQL_MAX_PREVIEW_COLUMNS,
78
SQL_MAX_PREVIEW_ROWS,
@@ -217,6 +218,23 @@ describe("SQL result previews", () => {
217218
expect(saved.rows()).toEqual([{ columns, values }])
218219
})
219220

221+
test("spills 2,000 numeric rows by 150 columns with identical bytes and one retained chunk per row", async () => {
222+
const columns = Array.from({ length: 150 }, (_, i) => `column_${i}`)
223+
const rows = Array.from({ length: 2_000 }, (_, row) => columns.map((_, column) => row * columns.length + column))
224+
const input = result(rows, columns)
225+
const chunks: string[] = []
226+
serializeRows(input, (chunk) => chunks.push(chunk))
227+
228+
expect(chunks).toHaveLength(rows.length)
229+
const expected = rows.map((values) => JSON.stringify({ columns, values }) + "\n").join("")
230+
expect(chunks.join("")).toBe(expected)
231+
232+
const saved = spill()
233+
const formatted = await formatSqlResult(input, { limit: 2_000, write: saved.write })
234+
expect(formatted.metadata.result_path).toBe(resultPath)
235+
expect(saved.writes).toEqual([expected])
236+
})
237+
220238
test.each([0, 8, 64, 512, 1024])(
221239
"keeps wide schemas and a long spill envelope within a %i-byte cap",
222240
async (maxBytes) => {

0 commit comments

Comments
 (0)