perf(native-chat): the recovering open reads the journal once

Every conversation open now goes through the recovering open, including the
read restore of every chat at startup, which used to replay its journal once.
The recovering open replayed it twice: once to probe it and again inside the
open. The probe is now handed to the open as its load.
This commit is contained in:
Brennan Benson
2026-09-25 04:17:30 -07:00
parent f3e52d206b
commit 29a340b0b4
2 changed files with 26 additions and 3 deletions
@@ -6,14 +6,14 @@
import { mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type {
AgentJournalItemIdentity,
AgentSessionJournalIdentity
} from '../../../shared/agent-session-journal-types'
import { openJournalDatabase } from '../agent-session-journal/journal-database'
import { JOURNAL_DB_SCHEMA_VERSION } from '../agent-session-journal/journal-database-schema'
import { loadJournal } from '../agent-session-journal/journal-open'
import { loadJournal, replayJournal } from '../agent-session-journal/journal-open'
import { journalDatabaseFile } from '../agent-session-journal/journal-paths'
import { readJournalEpochRows } from '../agent-session-journal/journal-row-table'
import { createTrackedJournalOpener } from '../agent-session-journal/journal-store-test-open'
@@ -24,6 +24,12 @@ import {
recoveryJournalDir
} from './agent-session-journal-recovery'
// Only the store's own replay goes through the mock; the probe's, inside the same module, does not.
vi.mock('../agent-session-journal/journal-open', async (importOriginal) => {
const actual = await importOriginal<{ replayJournal: typeof replayJournal }>()
return { ...actual, replayJournal: vi.fn(actual.replayJournal) }
})
const CODEX_SESSION = '019fd532-7c11-7a90-b6de-4e1a2c3d5f60'
const IDENTITY: AgentSessionJournalIdentity = {
@@ -160,6 +166,21 @@ describe('openAgentSessionJournalWithRecovery', () => {
expect(opened.snapshot().items).toHaveLength(2)
})
it('reads the journal once: the probe is the open', async () => {
await seedJournal(2)
vi.mocked(replayJournal).mockClear()
const opened = journals.track(
await openAgentSessionJournalWithRecovery({
identity: IDENTITY,
journalDir,
fence: 1,
historyFilePath
}).then((result) => result.journal)
)
expect(opened.snapshot().items).toHaveLength(2)
expect(replayJournal).not.toHaveBeenCalled()
})
it('rebuilds a holed journal in place on a fresh epoch', async () => {
await seedJournal(3)
await deleteRow(3)
@@ -67,7 +67,9 @@ export async function openAgentSessionJournalWithRecovery(input: {
}
const journal = await openAgentSessionJournal({
identity: input.identity,
journalDir: input.journalDir
journalDir: input.journalDir,
// The probe is this open's replay; omitted, not `null`, when there was nothing to load.
...(probe ? { loaded: probe } : {})
})
if (!probe?.corrupt) {
return { journal, recovery: null }