Port Codex tests to SQL journals and drop the legacy row rollback test

This commit is contained in:
Brennan Benson
2026-11-09 00:16:53 -08:00
parent 5e6554f049
commit cf04da6314
15 changed files with 277 additions and 241 deletions
@@ -1,35 +1,26 @@
import type { AgentJournalItemBody } from '../../../../shared/agent-session/journal/agent-session-journal-types'
import type { AgentJournalItemIdentity } from '../../../../shared/agent-session/journal/agent-session-journal-identity'
import type { JournalLifecycleMutationInput } from '../../../native-chat/agent-session-journal/writing/journal-lifecycle-mutation'
import { codexProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
import type { TrackedJournalOpener } from '../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import type { StructuredAgentSessionEventTarget } from '../../../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink'
import { withJournalQueueMembers } from '../../../native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-journal-double.test-fixture'
export function deferredTarget(
log: AgentJournalItemBody[],
/** A deferred sink target over a real SQL journal for one Codex thread. */
export async function journalTarget(
journals: TrackedJournalOpener,
stateDirectory: string,
publishes: string[] = []
): StructuredAgentSessionEventTarget {
): Promise<StructuredAgentSessionEventTarget> {
const journal = await journals.open({
identity: {
sessionId: 'session-1',
workspaceId: 'workspace-1',
hostId: 'local',
agent: 'codex',
providerHandle: codexProviderHandle('thread-abc')
},
stateDirectory
})
return {
fence: 7,
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a double for the journal members this path calls; the helper adds the in-order ones.
journal: withJournalQueueMembers({
appendItem: async (_identity: AgentJournalItemIdentity, body: AgentJournalItemBody) => {
log.push(body)
return { cursor: { epoch: 'e', sequence: log.length } }
},
appendTombstone: async () => ({ epoch: 'e', sequence: log.length }),
// Holds no rows to read back, so a turn row's revision is its lifecycle.
itemBody: () => null,
appendLifecycleBatch: async (input: {
mutations: readonly JournalLifecycleMutationInput[]
}) => {
for (const mutation of input.mutations) {
if (mutation.kind === 'item') {
log.push(mutation.body)
}
}
return { epoch: 'e', sequence: log.length }
}
}) as unknown as StructuredAgentSessionEventTarget['journal'],
journal,
publish: () => {
publishes.push('publish')
}
@@ -126,7 +126,7 @@ describe('a Codex reconnecting a dropped stream', () => {
expect(host.hasSession(SESSION)).toBe(true)
}
const items = (await readTestJournalItems(host['sessions'].get(SESSION)?.journal)) ?? []
const items = await readTestJournalItems(host['sessions'].get(SESSION)!.journal)
const retryRows = items.filter(
(item) => item.body.kind === 'status' && item.body.failure?.kind === 'providerRetrying'
)
@@ -1,4 +1,5 @@
import {
readTestJournalItems,
readTestJournalSnapshot,
readTestJournalSubmissions
} from '../../../native-chat/agent-session-journal/journal-observation.test-support'
@@ -26,7 +27,9 @@ import {
type StructuredAgentSessionChildExitSession
} from '../../../native-chat/agent-session-wire/termination/structured-agent-session-child-exit'
import type { StructuredAgentSessionEndedEvent } from '../../../native-chat/agent-session-wire/providers/structured-agent-session-adapter'
import { StructuredAgentSessionCurrentWork } from '../../../native-chat/agent-session-wire/observations/structured-agent-session-current-work'
import { structuredAgentSessionHandoverAuthority } from '../../../native-chat/agent-session-wire/delivery/structured-agent-session-handing-over'
import { agentSessionConversationView } from '../../../../shared/agent-session/lifecycle/agent-session-conversation-view'
import { applyAgentSessionRecordCommand } from '../../../runtime/agent-session/records/agent-session-record-command'
import { createCodexJournalTranslator } from './codex-structured-journal-translation'
import { handleCodexSessionExit } from '../session-lifecycle/codex-structured-session-close'
import type {
@@ -55,12 +58,27 @@ afterEach(async () => {
}
})
function userVisible(journal: AgentSessionJournal, liveFence: number | null) {
const items = journal.snapshot().items
const work = new StructuredAgentSessionCurrentWork(journal, liveFence)
async function userVisible(
journal: AgentSessionJournal,
session: StructuredAgentSessionChildExitSession,
record: AgentSessionRecord
) {
const items = await readTestJournalItems(journal)
const { state: work } = await journal.readWork(
() => ({
handle: journal.observationHandle(),
record: agentSessionConversationView(record),
ended: session.lastEndedChild,
child: session.child && { fence: session.child.fence, heldSends: [] },
...structuredAgentSessionHandoverAuthority(journal),
openedThrough: journal.openingCursor(),
operationalRevision: 0
}),
{ kind: 'state' }
)
return {
working: work.working(),
prompt: work.hasActionablePrompt(),
working: work.working,
prompt: work.actionablePrompts.length > 0,
turnStates: items.flatMap((item) => readAgentJournalTurn(item.body)?.state ?? []),
runningCalls: items.filter(
(item) => item.body.kind === 'tool-call' && item.body.state === 'running'
@@ -68,7 +86,7 @@ function userVisible(journal: AgentSessionJournal, liveFence: number | null) {
endedCalls: items.filter(
(item) => item.body.kind === 'tool-call' && item.body.state !== 'running'
).length,
send: journal.submission('send-1')?.dispatchState
send: (await journal.reads.submission('send-1'))?.dispatchState
}
}
@@ -153,7 +171,14 @@ it('settles a refused Codex exit through the host in one commit, then permits a
).toEqual({ accepted: true })
await expect(deferred.drained()).resolves.toEqual({ ok: true })
expect(deferred.state().queuedBytes).toBe(0)
expect(userVisible(journal, 7)).toEqual({
let record: AgentSessionRecord = agentSessionRecordFixture(
agentSessionLeaseFixture({ sessionId: SESSION })
)
const hostSession: StructuredAgentSessionChildExitSession = {
child: { generation: 'generation', fence: 7, phase: 'ready' },
journal
}
expect(await userVisible(journal, hostSession, record)).toEqual({
working: true,
prompt: true,
turnStates: ['running'],
@@ -216,13 +241,6 @@ it('settles a refused Codex exit through the host in one commit, then permits a
expect(onFailed).not.toHaveBeenCalled()
expect(await readTestJournalSnapshot(journal)).toEqual(before)
expect(commits).not.toHaveBeenCalled()
let record: AgentSessionRecord = agentSessionRecordFixture(
agentSessionLeaseFixture({ sessionId: SESSION })
)
const hostSession: StructuredAgentSessionChildExitSession = {
child: { generation: 'generation', fence: 7, phase: 'ready' },
journal
}
const [event] = events
if (!event) {
throw new Error('Codex did not report its observed exit')
@@ -232,10 +250,12 @@ it('settles a refused Codex exit through the host in one commit, then permits a
logger: recording.logger,
store: {
getRecord: () => record,
transitionHandoff: async (
_sessionId: string,
transition: (current: AgentSessionRecord) => AgentSessionRecord
) => (record = transition(record))
updateRecord: async (_sessionId, command) => {
const applied = applyAgentSessionRecordCommand(record, command)
record = applied.record
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the store answers each command with its own result.
return applied.result as never
}
},
sessions: new Map([[SESSION, hostSession]]),
flushLifecycle: () => deferred.lifecycleBarrier(),
@@ -245,7 +265,7 @@ it('settles a refused Codex exit through the host in one commit, then permits a
},
event
)
expect(userVisible(journal, null)).toEqual({
expect(await userVisible(journal, hostSession, record)).toEqual({
working: false,
prompt: false,
turnStates: ['interrupted'],
@@ -1,17 +1,14 @@
import { readTestJournalItems } from '../../../native-chat/agent-session-journal/journal-observation.test-support'
import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../shared/agent-session/journal/agent-session-journal-types'
import { describe, expect, it } from 'vitest'
import { agentJournalItemKey } from '../../../../shared/agent-session/journal/agent-session-journal-item-key'
import type {
AgentJournalItemBody,
AgentJournalRenderItem
} from '../../../../shared/agent-session/journal/agent-session-journal-types'
import type { AgentJournalItemIdentity } from '../../../../shared/agent-session/journal/agent-session-journal-identity'
import {
createDeferredStructuredAgentSessionEventSink,
type StructuredAgentSessionEventTarget
} from '../../../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import type { AgentJournalItemBody } from '../../../../shared/agent-session/journal/agent-session-journal-types'
import { createDeferredStructuredAgentSessionEventSink } from '../../../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink'
import { CodexJournalGoals } from './codex-structured-journal-goals'
import { withJournalQueueMembers } from '../../../native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-journal-double.test-fixture'
import { createTrackedJournalOpener } from '../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { codexProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { testEventSinkLogging } from '../../../native-chat/agent-session-wire/host/testing/structured-agent-session-logger.test-fixture'
const THREAD = '01a08cc2-f96e-76d0-bb74-88b9bc0b03fc'
@@ -34,46 +31,31 @@ function goalFrame(goal: Record<string, unknown> = {}): Record<string, unknown>
}
}
function goalJournal() {
let rowSequence = 0
const journals = createTrackedJournalOpener()
let root: string
beforeEach(async () => {
root = await mkdtemp(join(tmpdir(), 'orca-codex-goal-resume-'))
})
afterEach(async () => {
await journals.closeAll()
await rm(root, { recursive: true, force: true })
})
async function goalJournal() {
let publishes = 0
let epochNumber = 1
const rows = new Map<string, AgentJournalRenderItem>()
const writes: string[] = []
const deferred = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging())
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a fake journal exposing only the members the goal translator and deferred sink call.
const journal = withJournalQueueMembers({
get epoch() {
return `epoch-${epochNumber}`
},
appendItem: async (identity: AgentJournalItemIdentity, body: AgentJournalItemBody) => {
rowSequence += 1
const itemId = agentJournalItemKey(identity)
const existing = rows.get(itemId)
const revision = (existing?.revision ?? 0) + 1
writes.push(itemId)
rows.set(itemId, {
itemId,
body,
revision,
sequence: existing?.sequence ?? rowSequence,
observedAt: existing?.observedAt ?? rowSequence
})
return { cursor: { epoch: `epoch-${epochNumber}`, sequence: rowSequence }, itemId, revision }
},
snapshot: () => ({
const journal = await journals.open({
identity: {
sessionId: 'session',
cursor: { epoch: `epoch-${epochNumber}`, sequence: rowSequence },
items: [...rows.values()].sort((left, right) => left.sequence - right.sequence),
submissions: []
}),
item: (itemId: string) => rows.get(itemId) ?? null,
visitItems: (visit: (itemId: string, sequence: number, body: AgentJournalItemBody) => void) => {
for (const item of rows.values()) {
visit(item.itemId, item.sequence, item.body)
}
}
}) as unknown as StructuredAgentSessionEventTarget['journal']
workspaceId: 'workspace-1',
hostId: 'local',
agent: 'codex',
providerHandle: codexProviderHandle(THREAD)
},
stateDirectory: root
})
const deferred = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging())
const target = {
journal,
fence: 1,
@@ -82,16 +64,16 @@ function goalJournal() {
}
}
deferred.bind(target)
const rows = () => readTestJournalItems(journal)
return {
sink: deferred.sink,
writes,
rows: () => journal.snapshot().items,
// Each item write adds one revision, so the sum counts the writes that changed a row.
writes: async () => (await rows()).reduce((sum, row) => sum + row.revision, 0),
rows,
goalRowId: async () => (await rows()).findLast((row) => row.body.kind === 'status')?.itemId,
publishes: () => publishes,
replaceEpoch: () => {
epochNumber += 1
rowSequence = 0
rows.clear()
},
replaceEpoch: () =>
journal.write({ kind: 'epoch-replace', reason: 'handle_forked', fence: 1, items: [] }),
rebind: () => deferred.bind(target),
unbind: deferred.unbind,
drained: deferred.drained
@@ -104,7 +86,7 @@ function texts(rows: readonly AgentJournalItemBody[]): string[] {
describe('codex goal lifecycle resume', () => {
it('rebuilds dedupe state after the journal epoch is replaced', async () => {
const journal = goalJournal()
const journal = await goalJournal()
const goals = new CodexJournalGoals(journal.sink, () => ({
turnScope: AGENT_JOURNAL_THREAD_SCOPE
}))
@@ -112,14 +94,14 @@ describe('codex goal lifecycle resume', () => {
goals.handle(event)
await journal.drained()
expect(journal.writes).toHaveLength(1)
expect(await journal.rows()).toHaveLength(1)
journal.replaceEpoch()
await journal.replaceEpoch()
expect(await journal.rows()).toEqual([])
goals.handle(event)
await journal.drained()
expect(journal.writes).toHaveLength(2)
expect(texts(journal.rows().map((row) => row.body))).toEqual([
expect(texts((await journal.rows()).map((row) => row.body))).toEqual([
'Goal set: Keep the current scratch directory tidy.'
])
goals.dispose()
@@ -145,7 +127,7 @@ describe('codex goal lifecycle resume', () => {
expected: ['Goal set', 'Goal paused', 'Goal set']
}
])('does not duplicate a $name snapshot after translator recreation', async (scenario) => {
const journal = goalJournal()
const journal = await goalJournal()
const send = (
goals: CodexJournalGoals,
state: (typeof scenario.beforeResume)[number]
@@ -164,10 +146,12 @@ describe('codex goal lifecycle resume', () => {
send(prior, state)
}
await journal.drained()
const acceptedOccurrence = journal.writes.at(-1)
const writesBeforeResume = journal.writes.length
const acceptedOccurrence = await journal.goalRowId()
const writesBeforeResume = await journal.writes()
const publishesBeforeResume = journal.publishes()
const acceptedBody = journal.rows().find((row) => row.itemId === acceptedOccurrence)?.body
const acceptedBody = (await journal.rows()).find(
(row) => row.itemId === acceptedOccurrence
)?.body
prior.dispose()
journal.unbind()
@@ -190,21 +174,21 @@ describe('codex goal lifecycle resume', () => {
turnId: null
}
})
expect(journal.writes).toHaveLength(writesBeforeResume)
expect(await journal.writes()).toBe(writesBeforeResume)
journal.rebind()
await journal.drained()
expect(texts(journal.rows().map((row) => row.body))).toEqual(
expect(texts((await journal.rows()).map((row) => row.body))).toEqual(
scenario.expected.map((prefix) =>
prefix === 'Goal cleared' ? prefix : `${prefix}: Keep the current scratch directory tidy.`
)
)
// Fresh resume accounting revises the existing row; cleared goals carry none.
const cleared = scenario.resumed.method === 'thread/goal/cleared'
expect(journal.writes).toHaveLength(writesBeforeResume + (cleared ? 0 : 1))
expect(await journal.writes()).toBe(writesBeforeResume + (cleared ? 0 : 1))
expect(journal.publishes()).toBe(publishesBeforeResume + (cleared ? 0 : 1))
expect(journal.writes.at(-1)).toBe(acceptedOccurrence)
const revised = journal.rows().find((row) => row.itemId === acceptedOccurrence)
expect(await journal.goalRowId()).toBe(acceptedOccurrence)
const revised = (await journal.rows()).find((row) => row.itemId === acceptedOccurrence)
if (cleared) {
expect(revised?.body).toEqual(acceptedBody)
} else {
@@ -58,7 +58,7 @@ function goalFrame(goal: Record<string, unknown> = {}) {
function journalSink(journal: AgentSessionJournal) {
const deferred = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging())
const published: AgentSessionHistoryPage[] = []
const pages: Promise<AgentSessionHistoryPage | null>[] = []
let subscriberCursor: AgentJournalCursor | null = null
deferred.bind({
journal,
@@ -67,14 +67,13 @@ function journalSink(journal: AgentSessionJournal) {
if (subscriberCursor === null) {
return
}
const result = readAgentSessionHistory(journal, {
sessionId: IDENTITY.sessionId,
direction: 'after',
cursor: subscriberCursor
})
if (result.ok) {
published.push(result.page)
}
pages.push(
readAgentSessionHistory(journal, {
sessionId: IDENTITY.sessionId,
direction: 'after',
cursor: subscriberCursor
}).then((result) => (result.ok ? result.page : null))
)
}
})
return {
@@ -83,7 +82,8 @@ function journalSink(journal: AgentSessionJournal) {
subscribe: () => {
subscriberCursor = journal.cursor()
},
published
published: async () =>
(await Promise.all(pages)).filter((page): page is AgentSessionHistoryPage => page !== null)
}
}
@@ -116,7 +116,7 @@ describe('codex goal accounting revisions', () => {
})
await drained()
expect((await readTestJournalItems(journal))[1]?.revision).toBe(created?.revision)
expect(published).toEqual([])
expect(await published()).toEqual([])
goals.handle({
threadId: THREAD,
@@ -139,7 +139,7 @@ describe('codex goal accounting revisions', () => {
timeUsedSeconds: 45,
updatedAt: 1789068033_000
})
expect(published).toEqual([
expect(await published()).toEqual([
expect.objectContaining({
items: [
expect.objectContaining({
@@ -1,3 +1,4 @@
import type { AgentJournalSnapshot } from '../../../../shared/agent-session/journal/agent-session-journal-types'
import {
readTestJournalItems,
readTestJournalSnapshot
@@ -42,7 +43,7 @@ it.each([
now: () => 1_000
})
const onFailed = vi.fn()
const published: ReturnType<typeof journal.snapshot>[] = []
const published: Promise<AgentJournalSnapshot>[] = []
const deferred = createDeferredStructuredAgentSessionEventSink({
...testEventSinkLogging(SESSION),
onFailed
@@ -51,7 +52,7 @@ it.each([
journal,
fence: 7,
publish: () => {
published.push(journal.snapshot())
published.push(readTestJournalSnapshot(journal))
}
})
const translator = createCodexJournalTranslator({
@@ -92,7 +93,7 @@ it.each([
saved.map((item) => (item.body.kind === 'question' ? item.body.question : null))
).toEqual(questions)
expect(saved.map((item) => item.revision)).toEqual(ids[0] === ids[1] ? [2] : [1, 1])
expect(published.at(-1)).toEqual(await readTestJournalSnapshot(journal))
expect(await published.at(-1)).toEqual(await readTestJournalSnapshot(journal))
translator.dispose()
deferred.close()
}
@@ -1,4 +1,7 @@
import { describe, expect, it, vi } from 'vitest'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { AgentJournalItemBody } from '../../../../shared/agent-session/journal/agent-session-journal-types'
import type { AgentJournalItemIdentity } from '../../../../shared/agent-session/journal/agent-session-journal-identity'
import { agentJournalItemKey } from '../../../../shared/agent-session/journal/agent-session-journal-item-key'
@@ -9,11 +12,28 @@ import {
import { createCodexJournalTranslator } from './codex-structured-journal-translation'
import { CODEX_USER_INPUT_METHOD } from '../prompts/codex-prompt-registry'
import type { CodexStructuredSessionEvent } from '../session-lifecycle/codex-structured-session-state'
import { deferredTarget } from './codex-deferred-sink-target-test-support'
import { journalTarget } from './codex-deferred-sink-target-test-support'
import { readTestJournalItems } from '../../../native-chat/agent-session-journal/journal-observation.test-support'
import { createTrackedJournalOpener } from '../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { testEventSinkLogging } from '../../../native-chat/agent-session-wire/host/testing/structured-agent-session-logger.test-fixture'
const SESSION_ID = 'session-1'
const THREAD_ID = 'thread-abc'
const journals = createTrackedJournalOpener()
let root: string
beforeEach(async () => {
root = await mkdtemp(join(tmpdir(), 'orca-codex-translation-'))
})
afterEach(async () => {
await journals.closeAll()
await rm(root, { recursive: true, force: true })
})
async function savedBodies(target: { journal: Parameters<typeof readTestJournalItems>[0] }) {
return (await readTestJournalItems(target.journal)).map((item) => item.body)
}
type Row = { key: string; body: AgentJournalItemBody }
@@ -63,7 +83,6 @@ function pausedDeferred() {
describe('codex journal translation', () => {
it('admits authoritative item completion while the deferred sink pauses reading', async () => {
const bodies: AgentJournalItemBody[] = []
const publishes: string[] = []
const readingControl = { pauseReading: vi.fn(), resumeReading: vi.fn() }
const deferred = createDeferredStructuredAgentSessionEventSink({
@@ -98,11 +117,11 @@ describe('codex journal translation', () => {
expect(deferred.state()).toMatchObject({ backpressured: true })
expect(readingControl.pauseReading).toHaveBeenCalled()
deferred.bind(deferredTarget(bodies, publishes))
const target = await journalTarget(journals, root, publishes)
deferred.bind(target)
await expect(deferred.lifecycleBarrier()).resolves.toEqual({ ok: true })
expect(bodies).toEqual([
expect.objectContaining({ kind: 'tool-call', state: 'running' }),
expect(await savedBodies(target)).toEqual([
expect.objectContaining({
kind: 'tool-call',
state: 'completed',
@@ -119,14 +138,13 @@ describe('codex journal translation', () => {
})
)
await expect(deferred.lifecycleBarrier()).resolves.toEqual({ ok: true })
expect(bodies.at(-1)).toMatchObject({
expect((await savedBodies(target)).at(-1)).toMatchObject({
kind: 'message',
blocks: [{ type: 'text', text: 'next turn still works' }]
})
})
it('admits user-input prompt questions and their publish while the sink pauses reading', async () => {
const bodies: AgentJournalItemBody[] = []
const publishes: string[] = []
const bound: [string, string, string][] = []
const deferred = pausedDeferred()
@@ -158,10 +176,11 @@ describe('codex journal translation', () => {
])
expect(deferred.state()).toMatchObject({ backpressured: true })
deferred.bind(deferredTarget(bodies, publishes))
const target = await journalTarget(journals, root, publishes)
deferred.bind(target)
await expect(deferred.lifecycleBarrier()).resolves.toEqual({ ok: true })
expect(bodies).toEqual([
expect(await savedBodies(target)).toEqual([
expect.objectContaining({ kind: 'question', question: 'Which branch?' }),
expect.objectContaining({ kind: 'question', question: 'Proceed?' })
])
@@ -50,7 +50,7 @@ export function registerCodexHistoryBoundsTests(input: {
],
paginate: false
})
expect(c.texts()).not.toContain('assistant:Must not commit through stale anchor')
expect(await c.texts()).not.toContain('assistant:Must not commit through stale anchor')
expect((await c.journal.reads.item(old.itemId))?.body).toMatchObject({
contextUsage: { window: { tokens: 500, capturedAt: 2_000 } }
})
@@ -29,7 +29,6 @@ import { agentSessionSendBodyFingerprint } from '../../../../shared/agent-sessio
import { agentSessionFailureFact } from '../../../../shared/agent-session/failures/agent-session-failure'
import { agentSessionFailureWords } from '../../../../shared/agent-session/failures/agent-session-failure-words'
import { structuredRewindCut } from '../../../native-chat/agent-session-wire/rewind/structured-rewind-cut'
import { restoreJournal } from '../../../native-chat/agent-session-journal/replay/journal-open'
export function registerCodexCatchUpEchoTests(input: {
codexWith: typeof catchUpCodexWith
@@ -54,12 +53,12 @@ export function registerCodexCatchUpEchoTests(input: {
await c.open([], { returnedTurns: [turn(2)], paginate: false })
const whole = [turn(1), turn(2)]
await c.open([], { returnedTurns: whole, paginate: false })
expect(c.texts()).toContain('assistant:answer 1')
expect(await c.texts()).toContain('assistant:answer 1')
const before = c.journal.cursor()
await c.reopen()
await c.open([], { returnedTurns: whole, paginate: false })
expect(c.journal.cursor()).toEqual(before)
expect(c.texts().filter((text) => text === 'assistant:answer 1')).toHaveLength(1)
expect((await c.texts()).filter((text) => text === 'assistant:answer 1')).toHaveLength(1)
})
it('uses saved native IDs only for display, preserving sends and drafts through outside echoes and replay', async () => {
@@ -96,7 +95,7 @@ export function registerCodexCatchUpEchoTests(input: {
turnScope: AGENT_JOURNAL_THREAD_SCOPE
})
await (state === 'unknown'
? c.journal.markPendingSubmissionsUnknown(1)
? c.journal.write({ kind: 'pending-unknown', fence: 1 })
: c.journal.resolveDispatch({
clientMessageId: state,
state: 'rejected',
@@ -109,7 +108,7 @@ export function registerCodexCatchUpEchoTests(input: {
await c.reopen()
const before = await readTestJournalSnapshot(c.journal)
const draft = await c.journal.queuedMessages.get('draft')
const receipts = states.map((state) => c.journal.receiptFor(state))
const receipts = await Promise.all(states.map((state) => c.journal.reads.receiptFor(state)))
const native = states.map((state, n) => ({
...turn(n + 2),
items: [
@@ -138,7 +137,7 @@ export function registerCodexCatchUpEchoTests(input: {
]
}
await c.open([...native, outside])
expect(c.texts().slice(-4)).toEqual([
expect((await c.texts()).slice(-4)).toEqual([
'assistant:answer 3',
'(continued-outside)',
'user:continue',
@@ -147,8 +146,10 @@ export function registerCodexCatchUpEchoTests(input: {
expect((await readTestJournalItems(c.journal)).slice(0, before.items.length)).toEqual(
before.items
)
expect(states.map((state) => c.journal.receiptFor(state))).toEqual(receipts)
expect(c.texts().filter((text) => text.includes('historical rendering'))).toEqual([])
expect(await Promise.all(states.map((state) => c.journal.reads.receiptFor(state)))).toEqual(
receipts
)
expect((await c.texts()).filter((text) => text.includes('historical rendering'))).toEqual([])
expect(await readTestJournalSubmissions(c.journal)).toEqual(before.submissions)
expect(await c.journal.queuedMessages.get('draft')).toEqual(draft)
const outsideKey = agentJournalItemKey({
@@ -165,8 +166,6 @@ export function registerCodexCatchUpEchoTests(input: {
await c.open([...native, outside])
expect(await readTestJournalSnapshot(c.journal)).toEqual(whole)
expect(await c.journal.queuedMessages.get('draft')).toEqual(draft)
const restored = restoreJournal(c.db, SESSION)
expect(restored?.state.queue.cards.get('draft')).toEqual(draft)
})
it('keeps native display identity after a real retained cut and disk reopen', async () => {
@@ -194,23 +193,24 @@ export function registerCodexCatchUpEchoTests(input: {
fence: 1,
turnScope: AGENT_JOURNAL_THREAD_SCOPE
})
await c.journal.markPendingSubmissionsUnknown(1)
await c.journal.write({ kind: 'pending-unknown', fence: 1 })
await c.open([turn(1), turn(2), turn(3)])
const key = agentJournalItemKey({ provider: 'orca', clientMessageId })
const cut = structuredRewindCut(c.journal, {
itemId: agentJournalItemKey({
provider: 'codex',
threadId: THREAD,
turnId: 'turn-3',
ordinal: 0
}),
const target = agentJournalItemKey({
provider: 'codex',
nativeId: THREAD
threadId: THREAD,
turnId: 'turn-3',
ordinal: 0
})
if (!cut) {
throw new Error('missing native retention cut')
}
await c.journal.replaceEpochItems('handle_forked', 1, cut.kept)
expect(
await structuredRewindCut(c.journal, { itemId: target, provider: 'codex', nativeId: THREAD })
).not.toBeNull()
const items = await readTestJournalItems(c.journal)
const cutAt = items.find((item) => item.itemId === target)!.sequence
await c.replaceEpoch(
'handle_forked',
items.filter((item) => item.sequence < cutAt)
)
await c.reopen()
const prompt = await c.journal.reads.item(key)
expect(prompt?.body).toEqual(body)
@@ -228,20 +228,20 @@ export function registerCodexCatchUpEchoTests(input: {
]
}
const before = await readTestJournalSnapshot(c.journal)
const beforeTexts = c.texts()
const beforeTexts = await c.texts()
await c.open([turn(1), turn(2), native])
expect(await c.journal.reads.item(key)).toEqual(prompt)
expect((await readTestJournalItems(c.journal)).slice(0, before.items.length)).toEqual(
before.items
)
expect(c.texts()).toEqual([...beforeTexts, 'assistant:native answer'])
expect(await c.texts()).toEqual([...beforeTexts, 'assistant:native answer'])
expect((await c.journal.reads.submission(clientMessageId))?.dispatchState).toBe('unknown')
expect(await c.journal.reads.receiptFor(clientMessageId)).toBeNull()
const whole = await readTestJournalSnapshot(c.journal)
await c.reopen()
await c.open([turn(1), turn(2), native])
expect(await readTestJournalSnapshot(c.journal)).toEqual(whole)
await c.journal.replaceEpochItems('handle_forked', 1, [])
await c.replaceEpoch('handle_forked', [])
const hidden = c.journal.cursor()
const { readCodexHistoryJournalView } =
await import('../../history/journal-views/codex-history-journal-view')
@@ -379,7 +379,7 @@ export function registerCodexCatchUpEchoTests(input: {
}
}
await open()
expect(c.texts()).toEqual([
expect(await c.texts()).toEqual([
'user:native',
'(continued-outside)',
'user:ask 1',
@@ -437,9 +437,11 @@ export function registerCodexCatchUpEchoTests(input: {
}
})
await open()
expect(c.texts().at(-1)).toBe('assistant:final missing tail')
expect((await c.texts()).at(-1)).toBe('assistant:final missing tail')
await c.reopen()
expect(c.texts().filter((text) => text === 'assistant:completed answer')).toHaveLength(1)
expect(c.texts().at(-1)).toBe('assistant:final missing tail')
expect((await c.texts()).filter((text) => text === 'assistant:completed answer')).toHaveLength(
1
)
expect((await c.texts()).at(-1)).toBe('assistant:final missing tail')
})
}
@@ -108,7 +108,7 @@ export function registerCodexHistorySourcesTests(input: {
}
}
await open()
expect(c.texts()).toEqual(['user:ask 1', 'assistant:A different final answer'])
expect(await c.texts()).toEqual(['user:ask 1', 'assistant:A different final answer'])
const rows = await readTestJournalItems(c.journal)
expect(new Set(rows.map((row) => row.itemId)).size).toBe(rows.length)
expect(rows.find((row) => row.body.kind === 'turn')?.body).toMatchObject({
@@ -365,8 +365,9 @@ export function registerCodexHistorySourcesTests(input: {
]
}
await open([source])
const items = await readTestJournalItems(c.journal)
const saved = collisions.map(({ id }) =>
c.journal.snapshot().items.find(({ body }) => body.kind === 'tool-call' && body.callId === id)
items.find(({ body }) => body.kind === 'tool-call' && body.callId === id)
)
expect(saved).toHaveLength(collisions.length)
for (const row of saved) {
@@ -428,7 +429,7 @@ export function registerCodexHistorySourcesTests(input: {
path: 'a',
patch: journalPayload('+a')
})
expect(c.texts()).toEqual([
expect(await c.texts()).toEqual([
'(continued-outside)',
'(plan-document)',
'user:ask 2',
@@ -480,7 +481,7 @@ export function registerCodexHistorySourcesTests(input: {
expect((await readTestJournalItems(c.journal)).slice(0, before.items.length)).toEqual(
before.items
)
expect(c.texts().slice(-2)).toEqual(['user:ask 31', 'assistant:answer 31'])
expect((await c.texts()).slice(-2)).toEqual(['user:ask 31', 'assistant:answer 31'])
const imports = commits.mock.calls.flatMap(([call]) =>
call.steps.filter(
(step) =>
@@ -285,7 +285,7 @@ export function registerCodexHistoryVariantsTests(input: {
})
}
})
expect(c.texts().filter((text) => !text.startsWith('('))).toEqual([
expect((await c.texts()).filter((text) => !text.startsWith('('))).toEqual([
'user:Captured original',
'user:Original question',
'assistant:Independent final',
@@ -298,16 +298,16 @@ export function registerCodexHistoryVariantsTests(input: {
expect((await c.journal.reads.submission('proof'))?.dispatchState).toBe('accepted')
expect(helpers.find('wrong-helper')).toBeUndefined()
const row = (ordinal: number) =>
c.journal.item(
c.journal.reads.item(
agentJournalItemKey({ provider: 'codex', threadId: THREAD, turnId: 'turn-1', ordinal })
)
expect(row(0)).toBeNull()
expect(row(1)?.body).toMatchObject({
expect(await row(0)).toBeNull()
expect((await row(1))?.body).toMatchObject({
role: 'user',
blocks: [{ type: 'text', text: 'Original question' }]
})
expect(row(3)).toBeNull()
expect(row(4)?.body).toMatchObject({
expect(await row(3)).toBeNull()
expect((await row(4))?.body).toMatchObject({
role: 'assistant',
blocks: [{ type: 'text', text: 'Good after refused slot' }]
})
@@ -1,3 +1,4 @@
import { withTestAgentSessionDatabase } from '../../../runtime/agent-session/records/agent-session-record-store-test-harness'
import {
readTestJournalItems,
readTestJournalSnapshot
@@ -351,14 +352,18 @@ describe('Codex chat catch-up', () => {
await c.reopen()
await c.open([turn(1), turn(2), turn(3)])
expect((await readTestJournalItems(c.journal)).slice(0, before.length)).toEqual(before)
expect(c.texts().slice(-4)).toEqual([
expect((await c.texts()).slice(-4)).toEqual([
'(continued-outside)',
'user:ask 2',
'assistant:answer 2',
'assistant:answer 3'
])
await c.open([turn(1), turn(2), turn(3), turn(4)])
expect(c.texts().slice(-3)).toEqual(['(continued-outside)', 'user:ask 4', 'assistant:answer 4'])
expect((await c.texts()).slice(-3)).toEqual([
'(continued-outside)',
'user:ask 4',
'assistant:answer 4'
])
})
it('commits a 600-message import once, rolls back a later partition failure, and recovers', async () => {
@@ -373,21 +378,25 @@ describe('Codex chat catch-up', () => {
text: `answer ${n}`
}))
}
const seq = c.journal.cursor().sequence
c.db.exec(
`CREATE TRIGGER fail_later_partition BEFORE INSERT ON journal_rows WHEN NEW.seq > ${seq + 1} BEGIN SELECT RAISE(ABORT, 'injected later partition failure'); END`
// Let one more item in, then fail the import's later items inside the same command.
await withTestAgentSessionDatabase(c.root, (db) =>
db.exec(`CREATE TRIGGER fail_later_partition BEFORE INSERT ON journal_items
WHEN (SELECT COUNT(*) FROM journal_items WHERE session_id = NEW.session_id) > ${
before.items.length
}
BEGIN SELECT RAISE(ABORT, 'injected later partition failure'); END`)
)
expect((await c.open([turn(1), large])).ok).toBe(false)
expect(c.deferred.state().failed).toBe(true)
expect(await readTestJournalSnapshot(c.journal)).toEqual(before)
c.db.exec('DROP TRIGGER fail_later_partition')
await withTestAgentSessionDatabase(c.root, (db) => db.exec('DROP TRIGGER fail_later_partition'))
await c.reopen()
expect(await readTestJournalSnapshot(c.journal)).toEqual(before)
await c.open([turn(1), large])
expect(c.texts()).toHaveLength(603)
expect(await c.texts()).toHaveLength(603)
await c.reopen()
await c.open([turn(1), large])
expect(c.texts()).toHaveLength(603)
expect(await c.texts()).toHaveLength(603)
})
it('keeps rollback reasoning and message identities immutable across turns', async () => {
@@ -403,7 +412,7 @@ describe('Codex chat catch-up', () => {
const before = await readTestJournalItems(c.journal)
await c.open([withReasoning(2)])
expect((await readTestJournalItems(c.journal)).slice(0, before.length)).toEqual(before)
expect(c.texts().filter((text) => text.startsWith('assistant:'))).toEqual([
expect((await c.texts()).filter((text) => text.startsWith('assistant:'))).toEqual([
'assistant:answer 1',
'assistant:answer 2'
])
@@ -508,8 +517,9 @@ describe('Codex chat catch-up', () => {
fingerprint: 'rewind',
now: 500_000
})
await store.transitionHandoff(SESSION, (record) => ({
...record,
await store.updateRecord(SESSION, {
kind: 'rewind',
fence: store.getRecord(SESSION)!.lease.runtimeFence,
rewind: {
operationId,
callerKey,
@@ -517,7 +527,7 @@ describe('Codex chat catch-up', () => {
expectedEpoch: c.journal.physicalCursor().epoch,
phase: 'provider-succeeded'
}
}))
})
clock.now = 500_000
const deps = () => ({ store, logger: testEventSinkLogging().logger })
await recoverStructuredRewind(deps(), SESSION, c.journal, 1)
@@ -537,9 +547,11 @@ describe('Codex chat catch-up', () => {
store = await openTestAgentSessionRecordStore(c.root)
await recoverStructuredRewind(deps(), SESSION, c.journal, 1)
expect(store.getRecord(SESSION)?.rewind?.phase).toBe('completed')
expect(usableJournalHistoryOrigin(c.journal.historyOrigin())?.importedAt).toBe(200_000)
expect(usableJournalHistoryOrigin(await c.journal.reads.historyOrigin())?.importedAt).toBe(
200_000
)
await c.open([dated(100), dated(150), dated(200), dated(250), dated(600)])
expect(c.texts().filter((text) => text.startsWith('assistant:'))).toEqual([
expect((await c.texts()).filter((text) => text.startsWith('assistant:'))).toEqual([
'assistant:old A answer',
'assistant:answer 250',
'assistant:answer 600'
@@ -573,7 +585,12 @@ describe('Codex chat catch-up', () => {
blocks: [{ type: 'text' as const, text: 'legacy' }]
}
}
await c.journal.replaceEpochItems('session_created', 1, [kept])
await c.journal.write({
kind: 'epoch-replace',
reason: 'session_created',
fence: 1,
items: [kept]
})
await c.reopen()
await c.journal.appendItem(
{ provider: 'codex', threadId: THREAD, turnId: 'native', ordinal: 0 },
@@ -581,7 +598,7 @@ describe('Codex chat catch-up', () => {
{ fence: 1, turnScope: { kind: 'thread' } }
)
await c.open([turn(3)])
expect(c.texts()).toEqual(['user:legacy', 'assistant:ordinary reply'])
expect(await c.texts()).toEqual(['user:legacy', 'assistant:ordinary reply'])
})
})
@@ -130,10 +130,9 @@ describe('direct goal settings application', () => {
return { codex, adapter, apply, goal, settings: state.settings, state, acquisition }
}
it('keeps literal goal settings separate from exact applied turn proof and recovers on a successful turn', async () => {
it('keeps literal goal settings separate from the applied turn settings that hold a follow-up, and recovers on a successful turn', async () => {
const { codex, adapter, apply, goal, acquisition } = await fixture()
const choices = { model: 'unlisted-model-a', effort: 'high' }
const turnProof = { model: 'unlisted-model-a', effort: 'high', interactionMode: 'default' }
const dispatch = () =>
adapter.dispatch({
sessionId: 'session-1',
@@ -164,16 +163,8 @@ describe('direct goal settings application', () => {
{ fence: 7, generation: acquisition.acquisitionGeneration, phase: 'ready' },
adapter
)
const proof = () =>
adapter.readAppliedInputOptions({
sessionId: 'session-1',
fence: 7,
acquisitionGeneration: acquisition.acquisitionGeneration,
next: choices
})
try {
expect(await dispatch()).toMatchObject({ state: 'admitted' })
expect(proof()?.active).toEqual(turnProof)
codex.routes['thread/settings/update'] = (params) => {
apply(String(params?.model), String(params?.effort))
return {}
@@ -188,14 +179,11 @@ describe('direct goal settings application', () => {
}
])
const appliedGoal = { model: 'unlisted-model-b', effort: 'low', interactionMode: 'default' }
expect(proof()).toEqual({ active: appliedGoal, next: turnProof })
apply('observation-only', 'medium')
expect(proof()).toEqual({ active: appliedGoal, next: turnProof })
codex.routes['thread/settings/update'] = () => {
throw new CodexAppServerRequestError('thread/settings/update', -32602, 'invalid model')
}
expect(await goal('refused-model', 'high')).toMatchObject({ ok: false })
expect(proof()).toEqual({ active: appliedGoal, next: turnProof })
expect(
codex.connections[0].calls.filter((call) => call.method === 'thread/goal/set')
).toHaveLength(1)
@@ -204,7 +192,6 @@ describe('direct goal settings application', () => {
(call) => call.method === 'thread/settings/update'
).length
expect(await goal('observation-only-c', 'medium')).toEqual({ ok: true })
expect(proof()).toEqual({ active: appliedGoal, next: turnProof })
expect(await held(appliedGoal)).toBe(false)
expect(await held({ model: 'observation-only-c', effort: 'medium' })).toBe(true)
expect(
@@ -214,30 +201,22 @@ describe('direct goal settings application', () => {
throw new Error('reply lost after settings enqueue')
}
expect(await goal('uncertain-model', 'low')).toMatchObject({ ok: false })
expect(proof()).toEqual({
active: { effort: 'low', interactionMode: 'default' },
next: turnProof
})
expect(await held()).toBe(true)
expect(await dispatch()).toMatchObject({ state: 'admitted' })
expect(proof()).toEqual({ active: turnProof, next: turnProof })
codex.routes['thread/settings/update'] = () => {
throw new CodexAppServerRequestError('thread/settings/update', -32601, 'method not found')
}
expect(await goal('unlisted-model-b', 'low')).toEqual({ ok: true })
expect(proof()).toEqual({ active: turnProof, next: turnProof })
const callsBeforeCached = codex.connections[0].calls.filter(
(call) => call.method === 'thread/settings/update'
).length
expect(await goal('unlisted-model-b', 'low')).toEqual({ ok: true })
expect(proof()).toEqual({ active: turnProof, next: turnProof })
expect(await held()).toBe(false)
expect(await held(appliedGoal)).toBe(true)
expect(
codex.connections[0].calls.filter((call) => call.method === 'thread/settings/update')
).toHaveLength(callsBeforeCached)
expect(await dispatch()).toMatchObject({ state: 'admitted' })
expect(proof()).toEqual({ active: turnProof, next: turnProof })
} finally {
await adapter.closeAll()
}
@@ -3,12 +3,14 @@ import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, expect } from 'vitest'
import type { AgentJournalRenderItem } from '../../../../shared/agent-session/journal/agent-session-journal-types'
import { parseAgentJournalItemKey } from '../../../../shared/agent-session/journal/agent-session-journal-item-key'
import type { JournalWriterOperation } from '../../../native-chat/agent-session-journal/journal-writer-command'
import type { AgentSessionJournalIdentity } from '../../../../shared/agent-session/journal/agent-session-journal-identity'
import { codexProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
import {
closeTestJournalHostDatabase,
createTrackedJournalOpener,
openTestJournalHostDatabase
createTrackedJournalOpener
} from '../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { createDeferredStructuredAgentSessionEventSink } from '../../../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink'
import { testEventSinkLogging } from '../../../native-chat/agent-session-wire/host/testing/structured-agent-session-logger.test-fixture'
@@ -95,14 +97,12 @@ export async function chat(clock: { now: number }) {
return deferred
},
root,
get db() {
return openTestJournalHostDatabase(root).db
},
reopen: async () => {
deferred.close()
await journal.close()
await closeTestJournalHostDatabase(root)
journal = await journals.open(journalOptions)
// A reopen recovers interrupted sends, as the host's conversation open does.
journal = await journals.open({ ...journalOptions, recover: { fence: 1 } })
deferred = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging())
bind()
},
@@ -119,6 +119,23 @@ export async function chat(clock: { now: number }) {
})
return deferred.drained()
},
/** Replace the live epoch with these saved items, as a retained cut or fork commits it. */
replaceEpoch: async (
reason: Extract<JournalWriterOperation, { kind: 'epoch-replace' }>['reason'],
kept: readonly AgentJournalRenderItem[]
) => {
const items = await Promise.all(
kept.map(async (item) => ({
identity: parseAgentJournalItemKey(item.itemId)!,
body: item.body,
observedAt: item.observedAt,
...(item.turnScope ? { turnScope: item.turnScope } : {}),
...(item.agentId ? { agentId: item.agentId } : {}),
ownerFence: (await journal.reads.itemFence(item.itemId)) ?? 1
}))
)
await journal.write({ kind: 'epoch-replace', reason, fence: 1, items })
},
texts: async () =>
(await readTestJournalItems(journal)).flatMap((item) => {
if (item.body.kind === 'message') {
@@ -109,8 +109,9 @@ it('retains the original adoption cutoff through native rewind and disk recovery
fingerprint: 'rewind',
now: 500_000
})
await store.transitionHandoff(SESSION, (record) => ({
...record,
await store.updateRecord(SESSION, {
kind: 'rewind',
fence: store.getRecord(SESSION)!.lease.runtimeFence,
rewind: {
operationId,
callerKey,
@@ -118,24 +119,23 @@ it('retains the original adoption cutoff through native rewind and disk recovery
expectedEpoch: c.journal.physicalCursor().epoch,
phase: 'provider-succeeded'
}
}))
})
clock.now = 500_000
const deps = () => ({ store, logger: testEventSinkLogging().logger })
await recoverStructuredRewind(deps(), SESSION, c.journal, 1)
const restated = (await readTestJournalItems(c.journal)).slice(0, original.length)
expect(restated).toHaveLength(original.length)
for (const [index, item] of restated.entries()) {
const previous = original[index]!
expect(item).toEqual({ ...previous, revision: previous.revision + 2, sequence: item.sequence })
expect(item.sequence).toBeGreaterThan(previous.sequence)
}
// The cut keeps the imported rows exactly as saved.
expect(restated).toEqual(original)
await c.reopen()
store = await openTestAgentSessionRecordStore(c.root)
await recoverStructuredRewind(deps(), SESSION, c.journal, 1)
expect(store.getRecord(SESSION)?.rewind?.phase).toBe('completed')
expect(usableJournalHistoryOrigin(c.journal.historyOrigin())?.importedAt).toBe(200_000)
expect(usableJournalHistoryOrigin(await c.journal.reads.historyOrigin())?.importedAt).toBe(
200_000
)
await c.open([dated(100), dated(150), dated(200), dated(250), dated(600)])
expect(c.texts().filter((text) => text.startsWith('assistant:'))).toEqual([
expect((await c.texts()).filter((text) => text.startsWith('assistant:'))).toEqual([
'assistant:old A answer',
'assistant:answer 250',
'assistant:answer 600'
@@ -158,7 +158,12 @@ it('keeps erased-origin legacy chats readable without guessing from native IDs',
blocks: [{ type: 'text' as const, text: 'legacy' }]
}
}
await c.journal.replaceEpochItems('session_created', 1, [kept])
await c.journal.write({
kind: 'epoch-replace',
reason: 'session_created',
fence: 1,
items: [kept]
})
await c.reopen()
await c.journal.appendItem(
{ provider: 'codex', threadId: THREAD, turnId: 'native', ordinal: 0 },
@@ -166,5 +171,5 @@ it('keeps erased-origin legacy chats readable without guessing from native IDs',
{ fence: 1, turnScope: { kind: 'thread' } }
)
await c.open([turn(3)])
expect(c.texts()).toEqual(['user:legacy', 'assistant:ordinary reply'])
expect(await c.texts()).toEqual(['user:legacy', 'assistant:ordinary reply'])
})