From 67b70b6b75c3d073ddcac671feef8a282a7871ee Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Wed, 11 Nov 2026 09:21:30 +0000 Subject: [PATCH] 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 --- .../session-scanner-opencode2-sqlite.test.ts | 41 +++++++++++++++++++ .../session-scanner-opencode2-sqlite.ts | 19 +++++++-- .../session-transcript-message-content.ts | 9 +++- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/main/ai-vault/scanners/opencode2/session-scanner-opencode2-sqlite.test.ts b/src/main/ai-vault/scanners/opencode2/session-scanner-opencode2-sqlite.test.ts index 8b31a656376..31efa2281ef 100644 --- a/src/main/ai-vault/scanners/opencode2/session-scanner-opencode2-sqlite.test.ts +++ b/src/main/ai-vault/scanners/opencode2/session-scanner-opencode2-sqlite.test.ts @@ -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) diff --git a/src/main/ai-vault/scanners/opencode2/session-scanner-opencode2-sqlite.ts b/src/main/ai-vault/scanners/opencode2/session-scanner-opencode2-sqlite.ts index 22100357f35..d0033162ad9 100644 --- a/src/main/ai-vault/scanners/opencode2/session-scanner-opencode2-sqlite.ts +++ b/src/main/ai-vault/scanners/opencode2/session-scanner-opencode2-sqlite.ts @@ -203,11 +203,9 @@ function* readCaptureRows(db: SyncDatabase, sessionId: string): Generator 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 } } diff --git a/src/main/ai-vault/transcripts/session-transcript-message-content.ts b/src/main/ai-vault/transcripts/session-transcript-message-content.ts index 42a8e506ae4..3ed883b69e7 100644 --- a/src/main/ai-vault/transcripts/session-transcript-message-content.ts +++ b/src/main/ai-vault/transcripts/session-transcript-message-content.ts @@ -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) }