Skip to content

Commit f0ba3a0

Browse files
committed
Merge remote-tracking branch 'origin/feat/analyst-harness-dab-2026-10' into feat/analyst-v10-reliability
# Conflicts: # packages/opencode/src/altimate/tools/sql-result-format.ts
2 parents 1bd447f + c8519e8 commit f0ba3a0

3 files changed

Lines changed: 123 additions & 11 deletions

File tree

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ function clipCell(text: string, maxBytes: number, maxLines: number): string | un
4949
function previewSchema(columns: string[], maxBytes: number, maxLines: number, priority?: Set<string>) {
5050
const order = columns.map((_, index) => index)
5151
if (priority) order.sort((a, b) => Number(priority.has(columns[b]!)) - Number(priority.has(columns[a]!)))
52+
// An alias carrying a row or column delimiter is quoted so one column cannot read as several headings.
53+
const render = (name: string) => (/[\n\r]| \| /.test(name) ? JSON.stringify(name) : name)
5254
for (let count = Math.min(columns.length, SQL_MAX_PREVIEW_COLUMNS); count > 0; count--) {
5355
const omitted = count < columns.length ? `(+${columns.length - count} more columns, see spill)\n` : ""
5456
// Both the aliases and their separator consume space, along with delimiters/newlines.
@@ -58,7 +60,7 @@ function previewSchema(columns: string[], maxBytes: number, maxLines: number, pr
5860
// Retain named columns even beyond the ordinary prefix, without relaxing the
5961
// column or alias caps. Display the chosen columns in their original order.
6062
const indices = order.slice(0, count).sort((a, b) => a - b)
61-
const aliases = indices.map((index) => clipText(columns[index]!, aliasBytes, 1, "…[alias clipped]"))
63+
const aliases = indices.map((index) => clipText(render(columns[index]!), aliasBytes, 1, "…[alias clipped]"))
6264
if (aliases.some((name) => name === undefined)) continue
6365
const names = aliases as string[]
6466
const text =
@@ -68,7 +70,7 @@ function previewSchema(columns: string[], maxBytes: number, maxLines: number, pr
6870
text,
6971
indices,
7072
columns: count,
71-
clipped: count < columns.length || names.some((name, i) => name !== columns[indices[i]!]),
73+
clipped: count < columns.length || names.some((name, i) => name !== render(columns[indices[i]!])),
7274
}
7375
}
7476
return { text: "", indices: [], columns: 0, clipped: columns.length > 0 }
@@ -105,12 +107,11 @@ function displayObject(value: object, ancestors: Set<unknown>): { text: string;
105107
}
106108

107109
function displayCell(value: unknown, ancestors = new Set<unknown>()): { text: string; clipped: boolean } {
108-
// Avoid allocating a full comma-separated array or decoded BLOB just to clip it.
109-
if (value instanceof Uint8Array && value.byteLength > SQL_MAX_PREVIEW_BYTES) {
110-
const prefix = Buffer.isBuffer(value)
111-
? new TextDecoder().decode(value.subarray(0, SQL_MAX_PREVIEW_BYTES), { stream: true })
112-
: String(value.subarray(0, SQL_MAX_PREVIEW_BYTES / 4))
113-
return { text: prefix + `…[cell clipped, ${value.byteLength} bytes]`, clipped: true }
110+
// Only encode a bounded binary prefix before the preview applies its byte cap.
111+
if (value instanceof Uint8Array) {
112+
const clipped = value.byteLength > SQL_MAX_PREVIEW_BYTES
113+
const text = "base64:" + Buffer.from(value.subarray(0, SQL_MAX_PREVIEW_BYTES)).toString("base64")
114+
return { text: clipped ? text + `…[cell clipped, ${value.byteLength} bytes]` : text, clipped }
114115
}
115116
if (Array.isArray(value)) {
116117
if (ancestors.has(value)) return { text: "", clipped: false } // Array.toString omits cycles.

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

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,107 @@ describe("SQL result previews", () => {
122122
expect(saved.writes).toHaveLength(0)
123123
})
124124

125+
test("quotes column separators in aliases without spilling", async () => {
126+
const saved = spill()
127+
const formatted = await formatSqlResult(result([[1, 2]], ["a | b", "plain"]), {
128+
limit: 100,
129+
write: saved.write,
130+
})
131+
132+
expect(formatted.output).toBe('"a | b" | plain\n--------+------\n1 | 2\n\n(1 rows)')
133+
expect(formatted.metadata).toMatchObject({
134+
preview_rows: 1,
135+
preview_columns: 2,
136+
schema_truncated: false,
137+
output_truncated: false,
138+
})
139+
expect(saved.writes).toHaveLength(0)
140+
})
141+
142+
test("quotes newline and carriage return aliases without spilling", async () => {
143+
for (const alias of ["alpha\nbeta", 'alpha\r"beta"\\tail']) {
144+
const saved = spill()
145+
const formatted = await formatSqlResult(result([[1]], [alias]), { limit: 100, write: saved.write })
146+
const rendered = JSON.stringify(alias)
147+
148+
expect(formatted.output).toBe(`${rendered}\n${"-".repeat(rendered.length)}\n1\n\n(1 rows)`)
149+
expect(formatted.metadata).toMatchObject({
150+
preview_rows: 1,
151+
preview_columns: 1,
152+
schema_truncated: false,
153+
output_truncated: false,
154+
})
155+
expect(saved.writes).toHaveLength(0)
156+
}
157+
})
158+
159+
test("clips aliases after accounting for JSON escaping", async () => {
160+
const alias = "\n".repeat(64)
161+
const saved = spill()
162+
const formatted = await formatSqlResult(result([[1]], [alias]), { limit: 100, write: saved.write })
163+
164+
expect(formatted.output).toContain('"\\n\\n')
165+
expect(formatted.output).toContain("…[alias clipped]")
166+
expect(formatted.metadata).toMatchObject({ schema_truncated: true, output_truncated: true })
167+
expect(Buffer.byteLength(formatted.output, "utf8")).toBeLessThanOrEqual(SQL_MAX_PREVIEW_BYTES)
168+
expect(saved.rows()).toEqual([{ columns: [alias], values: [1] }])
169+
})
170+
171+
test("renders distinct small Buffer cells as base64 without spilling", async () => {
172+
const saved = spill()
173+
const formatted = await formatSqlResult(result([[Buffer.from([0x80])], [Buffer.from([0x81])]]), {
174+
limit: 100,
175+
write: saved.write,
176+
})
177+
178+
expect(body(formatted.output)).toBe("base64:gA==\nbase64:gQ==")
179+
expect(formatted.metadata).toMatchObject({ preview_rows: 2, output_truncated: false, truncated: false })
180+
expect(formatted.metadata.result_path).toBeUndefined()
181+
expect(saved.writes).toHaveLength(0)
182+
})
183+
184+
test("renders Uint8Array views and nested binary cells as base64 without spilling", async () => {
185+
const cell = new Uint8Array([255, 1, 128, 254]).subarray(1, 3)
186+
const saved = spill()
187+
const formatted = await formatSqlResult(result([[cell], [new Uint8Array()], [[cell, Buffer.from([0x81])]]]), {
188+
limit: 100,
189+
write: saved.write,
190+
})
191+
192+
expect(body(formatted.output)).toBe("base64:AYA=\nbase64:\nbase64:AYA=,base64:gQ==")
193+
expect(formatted.metadata.output_truncated).toBe(false)
194+
expect(saved.writes).toHaveLength(0)
195+
})
196+
197+
test("clips base64 expansion below the binary threshold and spills complete bytes", async () => {
198+
const cell = Buffer.alloc(SQL_MAX_PREVIEW_BYTES - 1, 0x80)
199+
const saved = spill()
200+
const formatted = await formatSqlResult(result([[cell]]), { limit: 100, write: saved.write })
201+
202+
expect(body(formatted.output)).toStartWith("base64:gICAgICA")
203+
expect(body(formatted.output)).toContain("…[cell clipped,")
204+
expect(formatted.metadata.output_truncated).toBe(true)
205+
expect(Buffer.byteLength(formatted.output, "utf8")).toBeLessThanOrEqual(SQL_MAX_PREVIEW_BYTES)
206+
expect(saved.rows()).toEqual([{ columns: ["value"], values: [{ $bytes: cell.toString("base64") }] }])
207+
})
208+
209+
test("renders oversized binary cells as a base64 prefix with the existing clip marker", async () => {
210+
const bytes = Buffer.alloc(SQL_MAX_PREVIEW_BYTES * 2, Buffer.from([0x80, 0x81, 0x82]))
211+
for (const cell of [bytes, new Uint8Array(bytes)]) {
212+
const saved = spill()
213+
const formatted = await formatSqlResult(result([[cell]]), { limit: 100, write: saved.write })
214+
const preview = body(formatted.output)
215+
216+
expect(preview).toStartWith("base64:gIGCgIGC")
217+
expect(preview).toContain("…[cell clipped,")
218+
expect(preview).not.toContain("\uFFFD")
219+
expect(bytes.toString("base64")).toStartWith(preview.slice("base64:".length).split("…[cell clipped,")[0]!)
220+
expect(formatted.metadata.output_truncated).toBe(true)
221+
expect(Buffer.byteLength(formatted.output, "utf8")).toBeLessThanOrEqual(SQL_MAX_PREVIEW_BYTES)
222+
expect(saved.rows()).toEqual([{ columns: ["value"], values: [{ $bytes: bytes.toString("base64") }] }])
223+
}
224+
})
225+
125226
test("quotes comma-bearing strings in array cells", async () => {
126227
const saved = spill()
127228
const formatted = await formatSqlResult(result([[["x,y", "z"]]]), { limit: 100, write: saved.write })
@@ -254,7 +355,7 @@ describe("SQL result previews", () => {
254355
const saved = spill()
255356
const formatted = await formatSqlResult(result(cells.map((cell) => [cell])), { limit: 100, write: saved.write })
256357

257-
expect(body(formatted.output)).toBe(cells.map(String).join("\n"))
358+
expect(body(formatted.output)).toBe([String(cells[0]), String(cells[1]), "base64:AYA=", "42", "true"].join("\n"))
258359
expect(saved.writes).toHaveLength(0)
259360
})
260361

‎packages/opencode/test/bus/private-trace.test.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,20 @@ test("request snapshots reach only the typed trace subscription", async () => {
3737
const global = (event: unknown) => globalEvents.push(event)
3838
GlobalBus.on("event", global)
3939
const unsubscribeAll = Bus.subscribeAll((event) => publicEvents.push(event))
40-
const unsubscribeTrace = Bus.subscribe(LLMRequestEvent, (event) => privateEvents.push(event))
40+
// Delivery runs on a forked fiber: wait for the subscription itself to signal, not for the clock.
41+
const delivered = Promise.withResolvers<void>()
42+
const unsubscribeTrace = Bus.subscribe(LLMRequestEvent, (event) => {
43+
privateEvents.push(event)
44+
delivered.resolve()
45+
})
4146
try {
4247
await Bus.publish(LLMRequestEvent, snapshot)
43-
await new Promise((resolve) => setTimeout(resolve, 10))
48+
await Promise.race([
49+
delivered.promise,
50+
new Promise((_, reject) => setTimeout(() => reject(new Error("private event was not delivered within 5000ms")), 5000)),
51+
])
52+
// Public and global listeners would have been notified by the same publish; give them one tick.
53+
await new Promise((resolve) => setTimeout(resolve, 0))
4454
expect(privateEvents).toHaveLength(1)
4555
expect(publicEvents).toEqual([])
4656
expect(globalEvents).toEqual([])

0 commit comments

Comments
 (0)