Skip to content

Commit 5df7cfa

Browse files
committed
Merge remote-tracking branch 'origin/feat/analyst-harness-dab-2026-10' into feat/analyst-v10-probe
2 parents d152ddd + c8519e8 commit 5df7cfa

3 files changed

Lines changed: 124 additions & 11 deletions

File tree

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,22 @@ function clipCell(text: string, maxBytes: number, maxLines: number): string | un
4545
}
4646

4747
function previewSchema(columns: string[], maxBytes: number, maxLines: number) {
48+
const rendered = columns
49+
.slice(0, SQL_MAX_PREVIEW_COLUMNS)
50+
.map((name) => (/[\n\r]| \| /.test(name) ? JSON.stringify(name) : name))
4851
for (let count = Math.min(columns.length, SQL_MAX_PREVIEW_COLUMNS); count > 0; count--) {
4952
const omitted = count < columns.length ? `(+${columns.length - count} more columns, see spill)\n` : ""
5053
// Both the aliases and their separator consume space, along with delimiters/newlines.
5154
const available = maxBytes - Buffer.byteLength(omitted, "utf8") - 2 - (count - 1) * 6
5255
const aliasBytes = Math.min(128, Math.floor(available / (2 * count)))
5356
if (aliasBytes < 4) continue
54-
const aliases = columns.slice(0, count).map((name) => clipText(name, aliasBytes, 1, "…[alias clipped]"))
57+
const aliases = rendered.slice(0, count).map((name) => clipText(name, aliasBytes, 1, "…[alias clipped]"))
5558
if (aliases.some((name) => name === undefined)) continue
5659
const names = aliases as string[]
5760
const text =
5861
names.join(" | ") + "\n" + names.map((name) => "-".repeat(Math.max(name.length, 4))).join("-+-") + "\n" + omitted
5962
if (Buffer.byteLength(text, "utf8") > maxBytes || lineCount(text) > maxLines) continue
60-
return { text, columns: count, clipped: count < columns.length || names.some((name, i) => name !== columns[i]) }
63+
return { text, columns: count, clipped: count < columns.length || names.some((name, i) => name !== rendered[i]) }
6164
}
6265
return { text: "", columns: 0, clipped: columns.length > 0 }
6366
}
@@ -93,12 +96,11 @@ function displayObject(value: object, ancestors: Set<unknown>): { text: string;
9396
}
9497

9598
function displayCell(value: unknown, ancestors = new Set<unknown>()): { text: string; clipped: boolean } {
96-
// Avoid allocating a full comma-separated array or decoded BLOB just to clip it.
97-
if (value instanceof Uint8Array && value.byteLength > SQL_MAX_PREVIEW_BYTES) {
98-
const prefix = Buffer.isBuffer(value)
99-
? new TextDecoder().decode(value.subarray(0, SQL_MAX_PREVIEW_BYTES), { stream: true })
100-
: String(value.subarray(0, SQL_MAX_PREVIEW_BYTES / 4))
101-
return { text: prefix + `…[cell clipped, ${value.byteLength} bytes]`, clipped: true }
99+
// Only encode a bounded binary prefix before the preview applies its byte cap.
100+
if (value instanceof Uint8Array) {
101+
const clipped = value.byteLength > SQL_MAX_PREVIEW_BYTES
102+
const text = "base64:" + Buffer.from(value.subarray(0, SQL_MAX_PREVIEW_BYTES)).toString("base64")
103+
return { text: clipped ? text + `…[cell clipped, ${value.byteLength} bytes]` : text, clipped }
102104
}
103105
if (Array.isArray(value)) {
104106
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
@@ -121,6 +121,107 @@ describe("SQL result previews", () => {
121121
expect(saved.writes).toHaveLength(0)
122122
})
123123

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

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

‎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)