fix(ai-vault): budget OpenCode 2 captures by decoded text, not stored tool state

OpenCode 2 charged the 64 MiB capture budget against raw session_message
rows, which are mostly tool input and metadata the transcript never keeps,
so a session with ~65 MiB of rows and a few hundred KB of text was dropped
whole. Count decoded transcript text instead, as OpenCode v1 does, and
copy truncated message text so a 256 KiB slice no longer pins its
oversized source string while the capture is held.

Fixes #28791
This commit is contained in:
Kelvin Amoaba
2026-11-11 09:30:44 +00:00
parent d0a15d824f
commit 67b70b6b75
3 changed files with 63 additions and 6 deletions
@@ -150,6 +150,47 @@ it('captures the full transcript while keeping list previews bounded', async ()
)
})
it('captures a session whose stored tool state exceeds 64 MiB when its decoded text is small (#28791)', async () => {
const { db, path } = createTempDb()
applyOpenCode2Schema(db)
insertSession(db, { id: 'heavy', directory: '/repo', timeCreated: 1000, timeUpdated: 2000 })
insertMessage(db, {
id: 'tool',
sessionId: 'heavy',
type: 'assistant',
seq: 0,
timeCreated: 1000,
data: JSON.stringify({
content: [
{
type: 'tool',
name: 'write',
state: {
status: 'completed',
input: { filePath: '/repo/a.txt', content: 'x'.repeat(65 * 1024 * 1024) },
content: [{ type: 'text', text: 'Wrote file successfully.' }]
}
}
]
})
})
insertMessage(db, {
id: 'reply',
sessionId: 'heavy',
type: 'assistant',
seq: 1,
timeCreated: 1001,
data: JSON.stringify({ text: 'Done' })
})
db.close()
const capture = await captureOpenCode2SqliteSession({
dbPath: path,
sessionId: 'heavy',
platform: 'darwin'
})
expect(capture.messages.at(-1)?.text).toBe('Done')
})
it('keeps session metadata readable but refuses a complete capture when ordering columns are missing', async () => {
const { db, path } = createTempDb()
applyOpenCode2Schema(db)
@@ -203,11 +203,9 @@ function* readCaptureRows(db: SyncDatabase, sessionId: string): Generator<Previe
WHERE session_id = ? AND type IN ('user','assistant','tool') ORDER BY time_created, seq`)
.iterate(sessionId)
let count = 0
let bytes = 0
for (const value of rows) {
const row = parseOpenCode2MessageRow(value)
bytes += Buffer.byteLength(row.data)
if (++count > OPENCODE_CAPTURE_RECORD_LIMIT || bytes > OPENCODE_CAPTURE_TEXT_LIMIT) {
if (++count > OPENCODE_CAPTURE_RECORD_LIMIT) {
throw new Error('OpenCode 2 transcript exceeds capture limits; no partial read was published')
}
yield { type: row.type, data: row.data, time_created: row.time_created }
@@ -300,7 +298,20 @@ export async function captureOpenCode2SqliteSession(args: {
platform: NodeJS.Platform
}): Promise<{ session: AiVaultSession | null; messages: TranscriptMessage[] }> {
const messages: TranscriptMessage[] = []
const sink: TranscriptMessageSink = { active: true, push: (message) => messages.push(message) }
let captured = 0
// Budget decoded text, not stored rows: tool state blobs dwarf what the transcript keeps (#28791).
const sink: TranscriptMessageSink = {
active: true,
push: (message) => {
captured += message.text.length
if (captured > OPENCODE_CAPTURE_TEXT_LIMIT) {
throw new Error(
'OpenCode 2 transcript exceeds capture limits; no partial read was published'
)
}
messages.push(message)
}
}
const session = await parseOpenCode2SqliteSession({ ...args, messages: sink })
return { session, messages }
}
@@ -5,6 +5,7 @@ import {
type DecodedToolCall
} from './session-decoded-message'
import { sliceAtCodeUnitLimit } from '../../../shared/surrogate-safe-text-slice'
import { ownRetainedString } from '../../../shared/own-retained-string'
import type { AiVaultSessionPreviewMessage } from '../../../shared/ai-vault/contracts/ai-vault-types'
import type { TranscriptMessage, TranscriptMessageRole } from './session-transcript-consumers'
@@ -141,6 +142,10 @@ export function transcriptMessageFromText(
}
export function boundedText(value: string): string | null {
const bounded = sliceAtCodeUnitLimit(value, TRANSCRIPT_MESSAGE_TEXT_LIMIT)
return bounded.trim() ? bounded : null
const sliced = sliceAtCodeUnitLimit(value, TRANSCRIPT_MESSAGE_TEXT_LIMIT)
if (!sliced.trim()) {
return null
}
// A truncated slice would pin the whole oversized source while the capture is retained.
return sliced.length === value.length ? sliced : ownRetainedString(sliced)
}