From 84a992a28a44db30ce55923cd898f7934abdee87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BF=AE=E9=9B=A8?= <47820304+PeterGuy326@users.noreply.github.com> Date: Tue, 17 Nov 2026 18:45:22 +0800 Subject: [PATCH] fix(native-chat): recover journal writes after confirmed SQLite rollback (#43560) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A COMMIT failure always latched the journal writer as indeterminate, even when SQLite had rejected the COMMIT and the rollback was confirmed, so later chat history writes failed until restart. An SQLITE_FULL that rolled back the whole transaction also replaced the real cause with a missing-savepoint error. Only latch when the COMMIT outcome is genuinely unknown or the rollback failed, keep the original error when SQLite already ended the transaction, and carry the cause's message in the batch error for the worker transport. Fixes #43555 Co-authored-by: 修雨 Co-authored-by: Neil --- .../journal-sql-batch.test.ts | 78 +++++++++++++++++-- .../journal-sql-batch.ts | 26 ++++++- 2 files changed, 95 insertions(+), 9 deletions(-) diff --git a/src/main/native-chat/agent-session-journal/journal-sql-batch.test.ts b/src/main/native-chat/agent-session-journal/journal-sql-batch.test.ts index 0c2044ddc42..3dbcb7233b4 100644 --- a/src/main/native-chat/agent-session-journal/journal-sql-batch.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-sql-batch.test.ts @@ -109,17 +109,25 @@ it('keeps bounded snapshot prefixes and controls in enqueue order', async () => expect((await commit()).outcomes).toEqual([{ committed: true, value: 6 }]) }) -it.each([false, true])( - 'treats a lost COMMIT answer as indeterminate, whether the commit took effect: %s', - async (committed) => { +it.each([ + { ranFirst: null, failure: new Error('COMMIT answer lost') }, + { ranFirst: 'COMMIT', failure: new Error('COMMIT answer lost') }, + { + ranFirst: 'ROLLBACK', + failure: Object.assign(new Error('disk I/O error'), { code: 'ERR_SQLITE_ERROR', errcode: 10 }) + } +])( + 'treats a COMMIT failure as indeterminate after running $ranFirst: $failure.message', + async ({ ranFirst, failure: commitFailure }) => { + const committed = ranFirst === 'COMMIT' const { db, batch } = fixture() const exec = db.exec.bind(db) vi.spyOn(db, 'exec').mockImplementation((sql) => { if (sql === 'COMMIT') { - if (committed) { - exec(sql) + if (ranFirst) { + exec(ranFirst) } - throw new Error('COMMIT answer lost') + throw commitFailure } exec(sql) }) @@ -165,3 +173,61 @@ it('does not claim rollback when both command and outer rollback fail', async () exec('ROLLBACK') expect(db.prepare('SELECT id FROM writes').all()).toEqual([]) }) + +it('admits later batches after a COMMIT that SQLite rejected is rolled back', async () => { + const { db, batch } = fixture() + db.exec(`PRAGMA foreign_keys=ON; + CREATE TABLE parent (id INTEGER PRIMARY KEY); + CREATE TABLE child (id INTEGER REFERENCES parent(id) DEFERRABLE INITIALLY DEFERRED)`) + await expect( + batch.run([1], (id) => db.prepare('INSERT INTO child VALUES (?)').run(id)) + ).rejects.toThrow( + expect.objectContaining({ + outcome: 'rolled-back', + message: 'journal SQL batch failed: FOREIGN KEY constraint failed' + }) + ) + await batch.run([2], (id) => db.prepare('INSERT INTO parent VALUES (?)').run(id)) + expect(db.prepare('SELECT id FROM child').all()).toEqual([]) + expect(db.prepare('SELECT id FROM parent').all()).toEqual([{ id: 2 }]) +}) + +it('admits later batches after SQLite rolls back a full database before COMMIT', async () => { + const { db, batch } = fixture() + db.pragma(`max_page_count=${Number(db.pragma('page_count', { simple: true }))}`) + const exec = db.exec.bind(db) + vi.spyOn(db, 'exec').mockImplementation((sql) => { + if (sql === 'COMMIT') { + // A real SQLITE_FULL, so SQLite itself rolls back as when COMMIT finds the disk full. + db.prepare('INSERT INTO writes VALUES (0, ?)').run('x'.repeat(65536)) + } + exec(sql) + }) + await expect( + batch.run([1], (id) => db.prepare('INSERT INTO writes VALUES (?, ?)').run(id, 'lost')) + ).rejects.toThrow( + expect.objectContaining({ + outcome: 'rolled-back', + message: 'journal SQL batch failed: database or disk is full' + }) + ) + vi.restoreAllMocks() + await batch.run([2], (id) => db.prepare('INSERT INTO writes VALUES (?, ?)').run(id, 'written')) + expect(db.prepare('SELECT id FROM writes').all()).toEqual([{ id: 2 }]) +}) + +it('keeps the full-database cause when SQLite rolls back the batch mid-command', async () => { + const { db, batch } = fixture() + db.pragma(`max_page_count=${Number(db.pragma('page_count', { simple: true }))}`) + await expect( + batch.run([1, 2], (id) => + db.prepare('INSERT INTO writes VALUES (?, ?)').run(id, id === 1 ? 'x' : 'x'.repeat(65536)) + ) + ).rejects.toThrow( + expect.objectContaining({ + outcome: 'rolled-back', + message: 'journal SQL batch failed: database or disk is full' + }) + ) + expect(db.prepare('SELECT id FROM writes').all()).toEqual([]) +}) diff --git a/src/main/native-chat/agent-session-journal/journal-sql-batch.ts b/src/main/native-chat/agent-session-journal/journal-sql-batch.ts index e04cf410e3f..9ebc6dde498 100644 --- a/src/main/native-chat/agent-session-journal/journal-sql-batch.ts +++ b/src/main/native-chat/agent-session-journal/journal-sql-batch.ts @@ -1,4 +1,6 @@ import type Database from '../../sqlite/sync-database' +import { storageCapacityErrorCode } from '../../../shared/errors/storage-capacity-error' +import { isSqliteDriverError } from '../../sqlite/sqlite-read-failure' export type JournalSqlBatchOutcome = | { committed: true; value: Value } @@ -16,10 +18,20 @@ export class JournalSqlBatchError extends Error { cause: unknown, readonly rollback?: unknown ) { - super('journal SQL batch failed', { cause }) + super(batchFailureMessage(cause), { cause }) } } +// The worker transport keeps only the message, so carry the cause's text in it. +function batchFailureMessage(cause: unknown): string { + if (cause instanceof JournalSqlBatchError) { + return cause.message + } + return cause instanceof Error + ? `journal SQL batch failed: ${cause.message}` + : 'journal SQL batch failed' +} + /** The worker passes a fixed drain boundary; callbacks and decoded state end with each SAVEPOINT. */ export class JournalSqlBatch { private failure: unknown @@ -64,6 +76,10 @@ export class JournalSqlBatch { this.db.exec('RELEASE journal_command') outcomes.push({ committed: true, value }) } catch (error) { + // An error such as SQLITE_FULL can roll back the whole transaction, SAVEPOINT included. + if (!this.db.isTransaction) { + throw error + } // Failure here escapes to the outer rollback; no earlier success is then acknowledged. this.db.exec('ROLLBACK TO journal_command; RELEASE journal_command') outcomes.push({ committed: false, error }) @@ -73,19 +89,23 @@ export class JournalSqlBatch { this.db.exec('COMMIT') } catch (cause) { let rollback: unknown + let rolledBack = !this.db.isTransaction && storageCapacityErrorCode(cause) === 'SQLITE_FULL' if (this.db.isTransaction) { try { this.db.exec('ROLLBACK') + rolledBack = true } catch (error) { rollback = error } } + // A COMMIT is settled only when SQLite itself rejected it and the rollback is confirmed. + const indeterminate = rollback || (committing && !(rolledBack && isSqliteDriverError(cause))) failed = new JournalSqlBatchError( - committing || rollback ? 'indeterminate' : began ? 'rolled-back' : 'not-dispatched', + indeterminate ? 'indeterminate' : began ? 'rolled-back' : 'not-dispatched', cause, rollback ) - if (committing || rollback) { + if (indeterminate) { this.failure = failed } } finally {