Skip to content

Commit 2525fa7

Browse files
anandgupta42claude
andcommitted
fix(analyst): preserve SQL preview contracts after rebasing on main
- Clipped column aliases set `schema_truncated`; only omitted cell data sets `output_truncated`, as on main. - Connector-reported truncation is no longer attributed to the requested limit. - Tests assert the dispatcher context main now passes (abort signal, session) and compare all saved full-text rows exactly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent aedfda0 commit 2525fa7

3 files changed

Lines changed: 68 additions & 17 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ export async function formatSqlResult(result: SqlExecuteResult, options: FormatO
529529
(excess
530530
? `Note: connector returned ${result.rows.length} rows; showing at most ${limit} (requested row limit). `
531531
: result.truncated
532-
? `Result may be row-limited (requested row limit ${limit}; total unknown). More rows may exist. `
532+
? "Result may be row-limited (connector reported a limit; total unknown). More rows may exist. "
533533
: "Row-limited: no. ") +
534534
`Output truncated: ${clipped ? "yes" : "no"}. ${spill}` +
535535
(clipped && !fullText ? " Prioritize specific cells with full_text_columns; omit limit to use the default." : "") +
@@ -563,7 +563,9 @@ export async function formatSqlResult(result: SqlExecuteResult, options: FormatO
563563
metadata.preview_rows = preview.rows
564564
metadata.preview_columns = schema.columns
565565
metadata.schema_truncated = schema.clipped
566-
metadata.output_truncated = preview.clipped || schema.clipped || notesClipped || excess
566+
// Clipped aliases use schema_truncated; omitted columns also hide cell values.
567+
metadata.output_truncated =
568+
preview.clipped || (rows.length > 0 && schema.columns < result.columns.length) || notesClipped || excess
567569
metadata.truncated = metadata.row_limited || metadata.output_truncated
568570
return {
569571
output: bounded(

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ describe("sql_execute full text selection", () => {
546546
expect(dispatch).toHaveBeenLastCalledWith(
547547
"sql.execute",
548548
expect.objectContaining({ sql: "SELECT 42", limit: limit ?? 100 }),
549+
{ signal: ctx.abort, sessionID: ctx.sessionID },
549550
)
550551
}
551552
},
@@ -554,7 +555,11 @@ describe("sql_execute full text selection", () => {
554555
test("defaults an active full text query to 20 rows", async () => {
555556
const tool = await initTool(SqlExecuteTool)
556557
const output = await tool.execute({ query: "SELECT 42", full_text_columns: ["answer"] }, ctx)
557-
expect(dispatch).toHaveBeenCalledWith("sql.execute", expect.objectContaining({ limit: 20 }))
558+
expect(dispatch).toHaveBeenCalledWith(
559+
"sql.execute",
560+
expect.objectContaining({ sql: "SELECT 42", limit: 20 }),
561+
{ signal: ctx.abort, sessionID: ctx.sessionID },
562+
)
558563
expect(output.output).toContain("42")
559564
expect(output.output).not.toContain("clamped")
560565
expect(output.metadata.error).toBeUndefined()
@@ -563,7 +568,11 @@ describe("sql_execute full text selection", () => {
563568
test.each([50, 10_001])("clamps an active full text limit of %i to 20 with a result note", async (limit) => {
564569
const tool = await initTool(SqlExecuteTool)
565570
const output = await tool.execute({ query: "SELECT 42", full_text_columns: ["answer"], limit }, ctx)
566-
expect(dispatch).toHaveBeenCalledWith("sql.execute", expect.objectContaining({ limit: 20 }))
571+
expect(dispatch).toHaveBeenCalledWith(
572+
"sql.execute",
573+
expect.objectContaining({ sql: "SELECT 42", limit: 20 }),
574+
{ signal: ctx.abort, sessionID: ctx.sessionID },
575+
)
567576
expect(output.output).toContain("42")
568577
expect(output.output).toMatch(new RegExp(`Note:.*limit.*${limit}.*clamped.*20`, "i"))
569578
expect(output.metadata.error).toBeUndefined()
@@ -591,6 +600,7 @@ describe("sql_execute full text selection", () => {
591600
expect(dispatch).toHaveBeenCalledWith(
592601
"sql.execute",
593602
expect.objectContaining({ sql: "SELECT id, title, body FROM rules", limit: 1 }),
603+
{ signal: ctx.abort, sessionID: ctx.sessionID },
594604
)
595605
})
596606

@@ -676,14 +686,23 @@ fullText.live("full text limits a connector's 40 returned rows to 20 preview row
676686
const initialized = yield* tool.init()
677687
const output = yield* initialized.execute({ query: "SELECT body FROM rules", full_text_columns: ["body"] }, ctx)
678688

679-
expect(dispatch).toHaveBeenCalledWith("sql.execute", expect.objectContaining({ limit: 20 }))
689+
expect(dispatch).toHaveBeenCalledWith(
690+
"sql.execute",
691+
expect.objectContaining({ sql: "SELECT body FROM rules", limit: 20 }),
692+
{ signal: ctx.abort, sessionID: ctx.sessionID },
693+
)
680694
expect(output.metadata).toMatchObject({ rows_returned: 40, preview_rows: 20, row_limited: true, truncated: true })
681695
expect(output.output).toContain("entry-20")
682696
expect(output.output).not.toContain("entry-21")
683697
expect(output.output).toMatch(/(?:connector.*40.*20|40.*returned.*20|20.*40.*returned)/i)
684698
const resultPath = output.metadata.result_path as string
685699
const fs = yield* FSUtil.Service
686-
expect((yield* fs.readFileString(resultPath)).trimEnd().split("\n")).toHaveLength(40)
700+
expect(
701+
(yield* fs.readFileString(resultPath))
702+
.trimEnd()
703+
.split("\n")
704+
.map((line) => JSON.parse(line)),
705+
).toEqual(rows.map((values) => ({ columns: ["body"], values })))
687706
yield* fs.remove(resultPath)
688707
}),
689708
)

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

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ describe("SQL result previews", () => {
177177
expect(Buffer.byteLength(formatted.output, "utf8")).toBeLessThanOrEqual(SQL_MAX_PREVIEW_BYTES)
178178
// Clipped names are lossy, so the full result is saved for recovery.
179179
expect(saved.writes).toHaveLength(1)
180+
expect(saved.rows()).toEqual([{ columns: [alias], values: [1] }])
180181
})
181182

182183
test("renders distinct small Buffer cells as base64 without spilling", async () => {
@@ -445,18 +446,22 @@ describe("SQL result previews", () => {
445446
expect(saved.rows()).toHaveLength(2)
446447
})
447448

448-
test("does not attribute a smaller connector cap to the requested tool limit", async () => {
449-
const formatted = await formatSqlResult(result([[1], [2]], ["id"], true), {
450-
limit: 100,
451-
sql: JSON.stringify({ command: "find", collection: "items", limit: 2 }),
452-
write: spill().write,
453-
})
449+
test.each([{ fullTextColumns: undefined }, { fullTextColumns: ["id"] }])(
450+
"does not attribute a smaller connector cap to the requested tool limit (%p)",
451+
async ({ fullTextColumns }) => {
452+
const formatted = await formatSqlResult(result([[1], [2]], ["id"], true), {
453+
limit: 100,
454+
sql: JSON.stringify({ command: "find", collection: "items", limit: 2 }),
455+
fullTextColumns: fullTextColumns?.slice(),
456+
write: spill().write,
457+
})
454458

455-
expect(formatted.output).toContain("may be row-limited")
456-
expect(formatted.output).not.toContain("row limit 100")
457-
expect(formatted.metadata.row_limited).toBe(true)
458-
expect(formatted.metadata.output_truncated).toBe(false)
459-
})
459+
expect(formatted.output).toContain("Result may be row-limited (connector reported a limit; total unknown)")
460+
expect(formatted.output).not.toContain("requested row limit")
461+
expect(formatted.metadata.row_limited).toBe(true)
462+
expect(formatted.metadata.output_truncated).toBe(false)
463+
},
464+
)
460465

461466
test("previews the first 50 rows and spills all rows even without a backend row limit", async () => {
462467
const rows = Array.from({ length: 75 }, (_, i) => [`row-${String(i).padStart(3, "0")}`])
@@ -522,6 +527,7 @@ describe("SQL result previews", () => {
522527
expect(formatted.output).not.toContain("Output truncated: yes")
523528
expect(formatted.metadata.result_path).toBeDefined()
524529
expect(saved.writes).toHaveLength(1)
530+
expect(saved.rows()).toEqual([{ columns: [alias], values: [42] }])
525531
})
526532

527533
test("caps a 500-column preview and saves the full schema and rows", async () => {
@@ -885,6 +891,30 @@ describe("SQL full text retrieval", () => {
885891
expect(saved.writes).toHaveLength(1)
886892
})
887893

894+
test.each([{ rows: [] }, { rows: [["complete"]] }])(
895+
"keeps schema clipping separate from complete full-text rows (%p)",
896+
async ({ rows }) => {
897+
const alias = "long_name".repeat(100)
898+
const saved = spill()
899+
const formatted = await formatSqlResult(result(rows.map((row) => [...row]), [alias]), {
900+
limit: 20,
901+
fullTextColumns: [alias],
902+
write: saved.write,
903+
})
904+
905+
expect(formatted.metadata).toMatchObject({
906+
preview_rows: rows.length,
907+
schema_truncated: rows.length > 0,
908+
output_truncated: false,
909+
truncated: false,
910+
result_path: resultPath,
911+
})
912+
expect(formatted.output).toContain("Output truncated: no.")
913+
expect(Buffer.byteLength(formatted.output)).toBeLessThanOrEqual(SQL_MAX_PREVIEW_BYTES)
914+
expect(saved.writes).toEqual([rows.map((values) => JSON.stringify({ columns: [alias], values }) + "\n").join("")])
915+
},
916+
)
917+
888918
test("uses the last available line for a complete named cell", async () => {
889919
const formatted = await formatSqlResult(result([["complete"]]), {
890920
limit: 20,

0 commit comments

Comments
 (0)