mirror of
https://github.com/stablyai/orca.git
synced 2026-11-15 18:37:24 +00:00
fix(chat): keep the supersede trigger local to the writer connection
This commit is contained in:
+12
-1
@@ -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', () => {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 }
|
||||
|
||||
Reference in New Issue
Block a user