From 7db7ea2b0168145b710bf22dcfe743329d93ebc2 Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Thu, 12 Nov 2026 05:48:47 -0800 Subject: [PATCH] fix(chat): keep the supersede trigger local to the writer connection --- ...ournal-item-compaction-worker-entry.test.ts | 13 ++++++++++++- .../outcomes/journal-outcome-format.ts | 13 +++++++++++-- .../outcomes/journal-outcome-table.ts | 18 ------------------ 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/main/native-chat/agent-session-journal/outcomes/journal-item-compaction-worker-entry.test.ts b/src/main/native-chat/agent-session-journal/outcomes/journal-item-compaction-worker-entry.test.ts index a3306b77e45..31f97bcd0d7 100644 --- a/src/main/native-chat/agent-session-journal/outcomes/journal-item-compaction-worker-entry.test.ts +++ b/src/main/native-chat/agent-session-journal/outcomes/journal-item-compaction-worker-entry.test.ts @@ -3,7 +3,8 @@ import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, beforeEach, expect, it } from 'vitest' import { AGENT_SESSION_JOURNAL_SCHEMA_VERSION } from '../../../../shared/agent-session/journal/agent-session-journal-types' -import type { JournalHostDatabase } from '../database/journal-host-database' +import { journalDatabasePath, type JournalHostDatabase } from '../database/journal-host-database' +import Database from '../../../sqlite/sync-database' import type { JournalRow } from '../durable-rows/journal-row-schema' import { restoreJournal } from '../replay/journal-open' import { @@ -75,6 +76,7 @@ it('stores only the first and newest record of a streamed item, and reopens to t function seedOldHistory(): void { database.transaction((db) => { + db.exec('DROP TRIGGER journal_outcome_item_supersede') publishTestJournalEpoch(db, sessionId, epoch) publishJournalOutcomeEpoch(db, sessionId, epoch, [ { @@ -172,6 +174,15 @@ it('removes the persistent trigger before a newer writer can insert unknown reco SELECT generation, 7, field, entry_key, 4, ordinal, value_json, deleted FROM journal_outcomes WHERE field = 'items' AND seq = 6`) expect(itemRecordSeqs()).toEqual([2, 3, 4, 5, 6, 7]) + const laterWriter = new Database(journalDatabasePath(root)) + try { + laterWriter.exec(`INSERT INTO journal_outcomes + SELECT generation, 8, field, entry_key, 3, ordinal, value_json, deleted + FROM journal_outcomes WHERE field = 'items' AND seq = 6`) + } finally { + laterWriter.close() + } + expect(itemRecordSeqs()).toEqual([2, 3, 4, 5, 6, 7, 8]) }) it('defers history deletion while the serving database is active, then resumes', () => { diff --git a/src/main/native-chat/agent-session-journal/outcomes/journal-outcome-format.ts b/src/main/native-chat/agent-session-journal/outcomes/journal-outcome-format.ts index 18e318ecef7..5d1e5171863 100644 --- a/src/main/native-chat/agent-session-journal/outcomes/journal-outcome-format.ts +++ b/src/main/native-chat/agent-session-journal/outcomes/journal-outcome-format.ts @@ -43,8 +43,17 @@ export function ensureJournalOutcomeTables(db: Database.Database): void { CREATE TABLE IF NOT EXISTS journal_maintenance ( task TEXT PRIMARY KEY, generation INTEGER NOT NULL, entry_key TEXT NOT NULL, done INTEGER NOT NULL ); - -- Pruning belongs to the versioned writer, never a trigger inherited by future builds. - DROP TRIGGER IF EXISTS journal_outcome_item_supersede`) + -- Only this connection inherits this build's whole-value replacement contract. + DROP TRIGGER IF EXISTS main.journal_outcome_item_supersede; + CREATE TEMP TRIGGER IF NOT EXISTS journal_outcome_item_supersede AFTER INSERT ON main.journal_outcomes + WHEN NEW.field = 'items' AND NEW.format IN (1, 2, 3) BEGIN + DELETE FROM journal_outcomes WHERE generation = NEW.generation AND field = 'items' + AND entry_key = NEW.entry_key + AND seq = (SELECT seq FROM journal_outcomes WHERE generation = NEW.generation + AND field = 'items' AND entry_key = NEW.entry_key AND seq < NEW.seq ORDER BY seq DESC LIMIT 1) + AND seq > (SELECT seq FROM journal_outcomes WHERE generation = NEW.generation + AND field = 'items' AND entry_key = NEW.entry_key ORDER BY seq LIMIT 1); + END`) } /** Every owning mutation calls this inside its transaction, before changing the epoch. Null means diff --git a/src/main/native-chat/agent-session-journal/outcomes/journal-outcome-table.ts b/src/main/native-chat/agent-session-journal/outcomes/journal-outcome-table.ts index 84ca1a8c6b7..719522dd147 100644 --- a/src/main/native-chat/agent-session-journal/outcomes/journal-outcome-table.ts +++ b/src/main/native-chat/agent-session-journal/outcomes/journal-outcome-table.ts @@ -88,13 +88,6 @@ export function persistJournalOutcomeRows( journalInputOrder(transition.state).introduceOverlays(transition.state) const previous = db.prepare(`SELECT ordinal, deleted FROM journal_outcomes WHERE generation = ? AND field = ? AND entry_key = ? ORDER BY seq DESC LIMIT 1`) - const supersede = db.prepare(`DELETE FROM journal_outcomes - WHERE generation = ? AND field = 'items' AND entry_key = ? - AND seq = (SELECT seq FROM journal_outcomes WHERE generation = ? AND field = 'items' - AND entry_key = ? AND seq < ? ORDER BY seq DESC LIMIT 1) - AND seq > (SELECT seq FROM journal_outcomes WHERE generation = ? AND field = 'items' - AND entry_key = ? ORDER BY seq LIMIT 1) - AND format IN (1, 2, 3)`) const insert = db.prepare(`INSERT INTO journal_outcomes (generation, seq, field, entry_key, format, ordinal, value_json, deleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`) @@ -159,17 +152,6 @@ export function persistJournalOutcomeRows( value.json, Number(value.deleted) ) - if (value.field === 'items') { - supersede.run( - source.generation, - value.key, - source.generation, - value.key, - value.seq, - source.generation, - value.key - ) - } } } return { state: transition.state, adopt: transition.adopt }