Align cross-version downgrade tests with one-way conversion

A downgraded build sees the chat and records as converted and its
later writes are not carried forward. Drop tests whose subject was
this build's legacy row and outcome writes; seed newer legacy rows
before the first conversion.
This commit is contained in:
Brennan Benson
2026-11-09 03:03:10 -08:00
parent 84d7265e6c
commit def4262da2
4 changed files with 172 additions and 445 deletions
@@ -9,8 +9,7 @@ import {
} from '../../../src/shared/agent-journal-tool-call-lifecycle'
import type { AgentJournalItemIdentity } from '../../../src/shared/agent-session/journal/agent-session-journal-identity'
import { createTrackedJournalOpener } from '../../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import type { JournalRow } from '../../../src/main/native-chat/agent-session-journal/durable-rows/journal-row-schema'
import type { JournalReadRow } from '../../../src/main/native-chat/agent-session-journal/replay/journal-read-row'
import { readTestJournalItems } from '../../../src/main/native-chat/agent-session-journal/journal-observation.test-support'
import {
importReleaseCheckoutModule,
materializeReleaseCheckout,
@@ -24,7 +23,6 @@ const releaseToolModules = prepareReleaseFixtures([BASELINE_REF], async (ref) =>
const checkout = await materializeReleaseCheckout(ref)
return Promise.all(
[
'src/main/native-chat/agent-session-journal/journal-reducer.ts',
'src/shared/agent-session-journal-schemas.ts',
'src/shared/structured-agent-session-projection.ts',
'src/shared/native-chat-tool-run-outcome.ts'
@@ -45,19 +43,10 @@ type OldRunOutcome = (
options: { activeTurnIsWorking?: boolean }
) => { failedCallCount: number; succeeded: boolean }
// Old builds fold input rows; an outcome row here means the probe opened a converted chat by mistake.
function inputRows(rows: readonly JournalReadRow[]): JournalRow[] {
return rows.map((row) => {
if (row.kind === 'outcome') {
throw new Error('expected input rows')
}
return row
})
}
// Loads a real old build, including cold extraction and transforms.
test('an older host and client read a call a stop cut short as the failure they always showed', async () => {
test('an older client reads a call a stop cut short as the failure they always showed', async () => {
const directory = mkdtempSync(join(tmpdir(), 'orca-interrupted-tool-downgrade-'))
const journals = createTrackedJournalOpener({ legacyInputs: true })
const journals = createTrackedJournalOpener()
try {
// This build: a call a stop cut short, with the partial output it left.
const journal = await journals.open({
@@ -85,30 +74,13 @@ test('an older host and client read a call a stop cut short as the failure they
'unverifiable'
)
await journal.appendItem(call(1), unverified, { fence: 1, turnScope: { kind: 'thread' } })
const since = journal.readSince({ epoch: journal.epoch, sequence: 0 })
if (!since.ok) {
throw new Error(`expected rows, got reset ${since.reset}`)
}
const rows = inputRows(since.rows)
const current = journal.snapshot().items
const current = await readTestJournalItems(journal)
expect(current.map((item) => item.body)).toMatchObject([
{ state: 'failed', endedAs: 'interrupted' },
{ state: 'failed', endedAs: 'unverifiable' }
])
const [reducer, schemas, projection, outcome] = releaseToolModules(BASELINE_REF)
const createState = releaseModuleExport<(sessionId: string, epoch: string) => unknown>(
reducer,
'createJournalReducerState'
)
const applyRow = releaseModuleExport<(state: unknown, row: JournalRow) => void>(
reducer,
'applyJournalRow'
)
const render = releaseModuleExport<(state: unknown) => { items: OldRenderItem[] }>(
reducer,
'renderJournalState'
)
const [schemas, projection, outcome] = releaseToolModules(BASELINE_REF)
const renderItemSchema = releaseModuleExport<{ parse: (value: unknown) => OldRenderItem }>(
schemas,
'AgentJournalRenderItemSchema'
@@ -123,23 +95,8 @@ test('an older host and client read a call a stop cut short as the failure they
const blocksOf = (items: OldRenderItem[]) =>
items.flatMap((item) => project(item)?.blocks ?? [])
// The older host, after a downgrade, folds the rows it never wrote as failed calls.
const state = createState(SESSION, journal.epoch)
for (const row of rows) {
applyRow(state, row)
}
const folded = render(state).items
expect(folded.map((item) => item.body)).toMatchObject([
{ kind: 'tool-call', state: 'failed' },
{ kind: 'tool-call', state: 'failed' }
])
expect(runOutcome(blocksOf(folded), settled)).toEqual({
failedCallCount: 2,
succeeded: false
})
// The older client reads this host's render items the same way.
const read = current.map((item) => renderItemSchema.parse(item))
// The older client reads this host's render items, as sent on the wire, as failed calls.
const read = current.map((item) => renderItemSchema.parse(JSON.parse(JSON.stringify(item))))
expect(runOutcome(blocksOf(read), settled)).toEqual({
failedCallCount: 2,
succeeded: false
@@ -13,18 +13,17 @@ import {
closeTestJournalHostDatabase,
openTestJournalHostDatabase
} from '../../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { structuredAgentSessionLiveFence } from '../../../src/main/native-chat/agent-session-wire/observations/structured-agent-session-current-work'
import { upgradeStoredStructuredAgentSessionOwnerExitEvidence } from '../../../src/main/native-chat/agent-session-wire/host/structured-agent-session-lease-release'
import { turnVerdictFromDeathEvidence } from '../../../src/main/native-chat/agent-session-wire/termination/structured-agent-session-stale-turn-verdict'
import { releaseStoredAgentSessionOwnerAfterSurfaceClose } from '../../../src/main/runtime/agent-session/ownership/agent-session-surface-release-transition'
import { loadAgentSessionStoreRows } from '../../../src/main/runtime/agent-session/records/agent-session-record-rows'
import {
closeTestAgentSessionDatabase,
openTestAgentSessionRecordStore,
seedTestAgentSessionRecordStore
storedTestAgentSessionRecord
} from '../../../src/main/runtime/agent-session/records/agent-session-record-store-test-harness'
import { importReleaseCheckoutModule, materializeReleaseCheckout } from './release-checkout'
// The last release before Stop recorded a delivered kill; it knows three death kinds.
// The last release before Stop recorded a delivered kill.
const RELEASE_REF = 'v1.4.244'
type OldStoreMaps = {
@@ -69,15 +68,27 @@ function oldRecord(value: unknown) {
}
// Materializing the historical source checkout and importing its modules can exceed the default timeout.
test(`${RELEASE_REF} keeps a stopped chat readable through restart, resume and evidence replacement`, async () => {
// Conversion is one-way: a downgraded build keeps the records as converted, and what it writes
// later is not carried forward, so the upgraded store keeps this build's records.
test(`${RELEASE_REF} keeps the converted records readable and writable; the upgraded store keeps this build's stopped chat`, async () => {
const directory = await mkdtemp(join(tmpdir(), 'orca-kill-downgrade-'))
try {
const live = agentSessionRecordFixture()
const sessionId = live.sessionId
await seedTestAgentSessionRecordStore(directory, { records: [live] })
// The chat as an older build saved it, before this build first converts the store.
const legacy = openTestJournalHostDatabase(directory).db
legacy
.prepare('INSERT INTO agent_session_records (session_id, record_json) VALUES (?, ?)')
.run(sessionId, JSON.stringify(storedTestAgentSessionRecord(live)))
const converted = String(
legacy
.prepare('SELECT record_json FROM agent_session_records WHERE session_id = ?')
.get(sessionId)?.record_json
)
await closeTestJournalHostDatabase(directory)
const current = await openTestAgentSessionRecordStore(directory)
// Seed the host's already-attached owner; the Stop itself uses the real transition and commit.
await current.transitionHandoff(sessionId, () => live)
expect(current.getRecord(sessionId)?.lease.runtimeFence).toBe(7)
await releaseStoredAgentSessionOwnerAfterSurfaceClose(current, {
sessionId,
expectedFence: 7,
@@ -85,119 +96,76 @@ test(`${RELEASE_REF} keeps a stopped chat readable through restart, resume and e
deathEvidenceKind: 'kill-delivered',
exitReason: 'killed'
})
// Include optional proof facts in the same production serialization contract.
await current.transitionHandoff(sessionId, (record) => ({
...record,
lease: {
...record.lease,
deathEvidence: record.lease.deathEvidence && {
...record.lease.deathEvidence,
lastProvenAliveAt: 35_000,
runtimeEnd: 'quit'
}
}
}))
const evidence = current.getRecord(sessionId)?.lease.deathEvidence
expect(evidence).toEqual({
const stopped = current.getRecord(sessionId)
expect(stopped?.lease.deathEvidence).toEqual({
kind: 'kill-delivered',
detail: 'killed',
observedAt: 40_000,
ownerFence: 7,
lastProvenAliveAt: 35_000,
runtimeEnd: 'quit'
ownerFence: 7
})
const db = openTestJournalHostDatabase(directory).db
const saved = db
.prepare('SELECT record_json FROM agent_session_records WHERE session_id = ?')
.get(sessionId)
if (typeof saved?.record_json !== 'string') {
throw new Error('the current store must save the chat')
}
const raw = JSON.parse(saved.record_json)
expect(raw.lease.deathEvidence).toEqual({ ...evidence, kind: 'pid-absent', delivery: 'kill' })
await closeTestAgentSessionDatabase(directory)
const checkout = await materializeReleaseCheckout(RELEASE_REF)
const [records, rows, drafts, restart, adjudication, transitions, views] = await Promise.all([
importReleaseCheckoutModule(checkout, 'src/shared/agent-session-record.ts'),
const [rows, drafts, restart] = await Promise.all([
importReleaseCheckoutModule(checkout, 'src/main/runtime/agent-session-record-rows.ts'),
importReleaseCheckoutModule(checkout, 'src/main/runtime/agent-session-store-draft.ts'),
importReleaseCheckoutModule(
checkout,
'src/main/runtime/agent-session/ownership/agent-session-restart-lease-transitions.ts'
),
importReleaseCheckoutModule(checkout, 'src/shared/agent-session-lease-adjudication.ts'),
importReleaseCheckoutModule(
checkout,
'src/main/runtime/agent-session/ownership/agent-session-lease-transitions.ts'
),
importReleaseCheckoutModule(checkout, 'src/main/runtime/agent-session-store-state.ts')
)
])
expect(exported(records, 'isPersistedAgentSessionRecord')(raw)).toBe(true)
const loaded = exported(rows, 'loadAgentSessionStoreRows')(db)
expect(storeMaps(loaded).unreadableRecords.size).toBe(0)
const old = oldRecord(storeMaps(loaded).records.get(sessionId))
// The older build reads the records as converted, without this build's Stop.
const db = openTestJournalHostDatabase(directory).db
expect(
exported(
adjudication,
'adjudicateAgentSessionRestart'
)({
lease: old.lease,
probe: { outcome: 'indeterminate', reason: 'no child after restart' },
observedAt: 50_000
})
).toMatchObject({ disposition: 'free' })
db
.prepare('SELECT record_json FROM agent_session_records WHERE session_id = ?')
.get(sessionId)?.record_json
).toBe(converted)
const loaded = storeMaps(exported(rows, 'loadAgentSessionStoreRows')(db))
expect(loaded.unreadableRecords.size).toBe(0)
const old = oldRecord(loaded.records.get(sessionId))
expect(old.lease).toMatchObject({ runtimeFence: 7, deathEvidence: null })
// It writes its own restart reconciliation over them.
const reconciled = oldRecord(
exported(
restart,
'applyAgentSessionRestartAdjudication'
)({
record: old,
probe: { outcome: 'indeterminate', reason: 'no child after restart' },
probe: { outcome: 'pid-absent' },
now: 50_000
})
)
expect(reconciled.lease).toMatchObject({
unreconciled: false,
runtimeFence: 8,
deathEvidence: raw.lease.deathEvidence
})
const writeOld = (record: unknown) => {
const before = exported(rows, 'loadAgentSessionStoreRows')(db)
const draft = exported(drafts, 'draftAgentSessionStoreState')(before)
storeMaps(draft).records.set(sessionId, record)
exported(rows, 'writeAgentSessionStoreRows')(
db,
exported(drafts, 'agentSessionStoreDraftRowWrites')(before, draft)
)
return draft
}
const oldState = writeOld(reconciled)
expect(exported(views, 'listAgentSessionConversations')(oldState)).toMatchObject([
{ sessionId, currentFence: 8 }
])
expect(exported(views, 'getAgentSessionConversation')(oldState, sessionId)).toMatchObject({
sessionId,
ownerObservation: { unreconciled: false, claimStatus: 'released' }
})
expect(exported(views, 'requireWritableAgentSessionRecord')(oldState, sessionId)).toEqual(
reconciled
const before = exported(rows, 'loadAgentSessionStoreRows')(db)
const draft = exported(drafts, 'draftAgentSessionStoreState')(before)
storeMaps(draft).records.set(sessionId, reconciled)
exported(rows, 'writeAgentSessionStoreRows')(
db,
exported(drafts, 'agentSessionStoreDraftRowWrites')(before, draft)
)
expect(
oldRecord(storeMaps(exported(rows, 'loadAgentSessionStoreRows')(db)).records.get(sessionId))
).toEqual(reconciled)
expect(
db
.prepare('SELECT record_json FROM agent_session_records WHERE session_id = ?')
.get(sessionId)?.record_json
).not.toBe(converted)
await closeTestJournalHostDatabase(directory)
// Upgraded again over the older build's newer records: this build's stopped chat, unchanged.
const upgraded = await openTestAgentSessionRecordStore(directory)
const restored = upgraded.getRecord(sessionId)
if (!restored) {
throw new Error('the upgraded chat must remain readable')
}
expect(restored.lease.deathEvidence).toEqual(evidence)
expect(restored.lease.runtimeFence).toBe(8)
expect(restored).toEqual(stopped)
const conversation = agentSessionConversationView(restored)
expect(agentSessionLeaseOwnerVerdict(restored.lease)).toBe('exited')
expect(agentSessionLeasePossiblyWriting(restored.lease)).toBe(false)
expect(structuredAgentSessionLiveFence({ record: conversation })).toBeNull()
expect(agentModelCatalogReleasedOwner(conversation)).toBe(true)
expect(turnVerdictFromDeathEvidence(restored.lease.deathEvidence, 7)).toEqual({
state: 'interrupted',
completedAt: 35_000
expect(turnVerdictFromDeathEvidence(restored.lease.deathEvidence, 7)).toMatchObject({
state: 'interrupted'
})
await upgradeStoredStructuredAgentSessionOwnerExitEvidence(
upgraded,
@@ -212,56 +180,12 @@ test(`${RELEASE_REF} keeps a stopped chat readable through restart, resume and e
},
61_000
)
expect(loadAgentSessionStoreRows(db).records.get(sessionId)?.lease).toMatchObject({
runtimeFence: 8,
deathEvidence: { ...evidence, kind: 'exit-observed', observedAt: 60_000 }
expect(upgraded.getRecord(sessionId)?.lease.deathEvidence).toMatchObject({
kind: 'exit-observed',
detail: 'killed',
observedAt: 60_000,
ownerFence: 7
})
// Starting again in the old build ends the previous proof, including its marker.
const reservation = exported(
transitions,
'reserveAgentSessionOwner'
)({
record: reconciled,
expectedFence: 8,
probe: { outcome: 'pid-absent' },
reservation: {
spawnToken: 'next-owner',
claimKeyId: 'key-1',
handoffOperationId: null,
leaseTtlMs: 30_000,
now: 70_000
}
})
if (typeof reservation !== 'object' || reservation === null || !('record' in reservation)) {
throw new Error('the old build must reserve a successor')
}
const reserved = oldRecord(reservation.record)
writeOld(reserved)
expect(loadAgentSessionStoreRows(db).records.get(sessionId)?.lease).toMatchObject({
runtimeFence: 9,
claimStatus: 'reserved',
deathEvidence: null
})
const replaced = oldRecord(
exported(
restart,
'applyAgentSessionRestartAdjudication'
)({
record: reserved,
probe: { outcome: 'reservation-unused' },
now: 80_000
})
)
writeOld(replaced)
const replacement = loadAgentSessionStoreRows(db).records.get(sessionId)?.lease.deathEvidence
expect(replacement).toEqual({
kind: 'pid-absent',
detail: 'reservation never spawned',
observedAt: 80_000,
ownerFence: 9
})
expect(replaced.lease.deathEvidence).not.toHaveProperty('delivery')
} finally {
await closeTestJournalHostDatabase(directory)
await rm(directory, { recursive: true, force: true })
@@ -10,6 +10,7 @@ import type {
AgentSessionJournalIdentity,
AgentSessionJournalProviderHandle
} from '../../../src/shared/agent-session/journal/agent-session-journal-identity'
import { agentJournalItemKey } from '../../../src/shared/agent-session/journal/agent-session-journal-item-key'
import { codexProviderHandle } from '../../../src/shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
import type { AgentSessionRefusalReference } from '../../../src/shared/agent-session/failures/agent-session-wire-refusals'
import {
@@ -18,7 +19,8 @@ import {
insertTestJournalRowJson,
liveTestJournalRows,
openTestJournalHostDatabase,
SAVED_BY_NEWER_ORCA
SAVED_BY_NEWER_ORCA,
seedLegacyTestJournal
} from '../../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { journalOpenRefusal } from '../../../src/main/native-chat/agent-session-journal/replay/journal-open-failure'
import {
@@ -60,7 +62,7 @@ type NoticeEnglish = (parts: unknown[]) => string
type IsFinal = (refusal: AgentSessionRefusalReference | undefined) => boolean
let directory: string
const journals = createTrackedJournalOpener({ legacyInputs: true })
const journals = createTrackedJournalOpener()
beforeAll(() => {
directory = mkdtempSync(join(tmpdir(), 'orca-newer-chat-xv-'))
@@ -84,39 +86,43 @@ const NEWER_NESTED_LITERAL = {
/** A row of a kind this build does not know: the journal's open is refused as a newer Orca's. */
const NEWER_ROW_KIND = { kind: 'future-mark' }
// Write two current journal items, then a newer body or an unknown closed row.
const EPOCH = 'epoch-newer-chat'
// A saved chat of two items, then a newer body or an unknown closed row, all before this build
// first opens it: this build reads saved rows only when it first converts the chat.
async function journalWithNewerRow(
row: Record<string, unknown> = NEWER_ROW_KIND
): Promise<{ rows: string[]; newer: string }> {
const journal = await journals.open({ identity: IDENTITY, stateDirectory: directory })
await journal.appendItem(
{ provider: 'codex', threadId: 'thread-1', turnId: 'turn-1', ordinal: 0 },
{ kind: 'status', text: 'before' },
{ fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE }
)
await journal.appendItem(
{ provider: 'codex', threadId: 'thread-1', turnId: 'turn-1', ordinal: 2 },
{ kind: 'status', text: 'also before' },
{ fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE }
)
const seq = journal.cursor().sequence + 1
const newer = JSON.stringify({
// A version the older build reads, so it meets the kind rather than a newer version.
v: AGENT_SESSION_JOURNAL_TURN_ITEM_SCHEMA_VERSION,
epoch: journal.epoch,
seq,
fence: 1,
ts: 2_000,
...row
})
await journals.closeAll()
insertTestJournalRowJson(
openTestJournalHostDatabase(directory).db,
IDENTITY.sessionId,
seq,
newer,
2_000
const database = openTestJournalHostDatabase(directory)
seedLegacyTestJournal(database, { identity: IDENTITY, mintEpoch: () => EPOCH, now: () => 1_000 })
const stored = (seq: number, fields: Record<string, unknown>) =>
JSON.stringify({
// A version the older build reads, so it meets the kind rather than a newer version.
v: AGENT_SESSION_JOURNAL_TURN_ITEM_SCHEMA_VERSION,
epoch: EPOCH,
seq,
fence: 1,
ts: 1_000 + seq,
...fields
})
const before = ['before', 'also before'].map((text, index) => ({
kind: 'item',
itemId: agentJournalItemKey({
provider: 'codex',
threadId: 'thread-1',
turnId: 'turn-1',
ordinal: index * 2
}),
revision: 1,
turnScope: AGENT_JOURNAL_THREAD_SCOPE,
body: { kind: 'status', text }
}))
before.forEach((fields, index) =>
insertTestJournalRowJson(database.db, IDENTITY.sessionId, index + 2, stored(index + 2, fields))
)
const seq = before.length + 2
const newer = stored(seq, row)
insertTestJournalRowJson(database.db, IDENTITY.sessionId, seq, newer, 2_000)
return { rows: await storedRows(), newer }
}
@@ -13,6 +13,8 @@ import type {
} from '../../../src/shared/agent-session/journal/agent-session-journal-identity'
import { codexProviderHandle } from '../../../src/shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
import Database from '../../../src/main/sqlite/sync-database'
import type { AgentSessionJournal } from '../../../src/main/native-chat/agent-session-journal/journal-store'
import { readTestJournalItems } from '../../../src/main/native-chat/agent-session-journal/journal-observation.test-support'
import { journalDatabasePath } from '../../../src/main/native-chat/agent-session-journal/database/journal-host-database'
import {
closeTestJournalHostDatabase,
@@ -20,30 +22,22 @@ import {
insertTestJournalRowJson,
liveTestJournalRows,
openTestJournalHostDatabase,
SAVED_BY_NEWER_ORCA
SAVED_BY_NEWER_ORCA,
seedLegacyTestJournal
} from '../../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import type { JournalRow } from '../../../src/main/native-chat/agent-session-journal/durable-rows/journal-row-schema'
import type { JournalReadRow } from '../../../src/main/native-chat/agent-session-journal/replay/journal-read-row'
import {
importReleaseCheckoutModule,
materializeReleaseCheckout,
releaseModuleExport
} from './release-checkout'
import { agentJournalItemKey } from '../../../src/shared/agent-session/journal/agent-session-journal-item-key'
import { agentSessionFailureFact } from '../../../src/shared/agent-session/failures/agent-session-failure'
import { agentSessionFailureWords } from '../../../src/shared/agent-session/failures/agent-session-failure-words'
import {
applyJournalRow,
createJournalReducerState,
renderJournalState
} from '../../../src/main/native-chat/agent-session-journal/projection/journal-reducer'
// Old readers would truncate at unknown row kinds; encode Stop/Resume/reopen evidence in tombstones they understand.
const BASELINE_REF = 'v1.4.218'
const JOURNAL = 'src/main/native-chat/agent-session-journal'
// Pin a main build sharing this database/schema for writable downgrade; move to the first release that ships it.
const WRITABLE_BASELINE_REF = '3727100cc9dbcea6201f8a3e506676a3c4b53b18'
const EPOCH = 'epoch-saved-before-conversion'
const IDENTITY: AgentSessionJournalIdentity = {
sessionId: 'session-downgrade',
workspaceId: 'ws-1',
@@ -62,186 +56,12 @@ const OLDER_IDENTITY: OlderJournalIdentity = {
providerHandle: { kind: 'codex', threadId: 'thread-1' }
}
// Old builds fold input rows; an outcome row here means the probe opened a converted chat by mistake.
function inputRows(rows: readonly JournalReadRow[]): JournalRow[] {
return rows.map((row) => {
if (row.kind === 'outcome') {
throw new Error('expected input rows')
}
return row
})
}
test.each(['v1.4.219', 'v1.4.220', WRITABLE_BASELINE_REF])(
'%s replays current raw rows with the unconfirmed failure intact',
async (ref) => {
const directory = mkdtempSync(join(tmpdir(), 'orca-stop-note-raw-skew-'))
const journals = createTrackedJournalOpener({ legacyInputs: true })
try {
const journal = await journals.open({ identity: IDENTITY, stateDirectory: directory })
const turn = { provider: 'orca', clientMessageId: 'turn-raw-skew' } as const
const note = { provider: 'orca', clientMessageId: 'stop:turn-raw-skew' } as const
const unconfirmed = {
kind: 'status' as const,
...agentSessionFailureWords(agentSessionFailureFact('cancelUnconfirmed'), {
surface: 'row'
})
}
await journal.appendItem(
turn,
{
kind: 'status',
text: 'Interrupted',
turnLifecycle: { turnId: 'turn-raw-skew', state: 'interrupted' }
},
{ fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE }
)
await journal.appendItem(note, unconfirmed, {
fence: 1,
turnScope: { kind: 'turn', turnItemId: agentJournalItemKey(turn) }
})
const since = journal.readSince({ epoch: journal.epoch, sequence: 0 })
if (!since.ok) {
throw new Error(since.reset)
}
const rawJson = JSON.stringify(since.rows)
const checkout = await materializeReleaseCheckout(ref)
const reducer = await importReleaseCheckoutModule(checkout, `${JOURNAL}/journal-reducer.ts`)
const create = releaseModuleExport<(sessionId: string, epoch: string) => OldReplay['state']>(
reducer,
'createJournalReducerState'
)
const fold = releaseModuleExport<(state: OldReplay['state'], row: JournalRow) => void>(
reducer,
'applyJournalRow'
)
const old = create(IDENTITY.sessionId, journal.epoch)
inputRows(since.rows).forEach((row) => fold(old, row))
expect(old.items.get(agentJournalItemKey(note))).toMatchObject({ body: unconfirmed })
const current = createJournalReducerState(IDENTITY.sessionId, journal.epoch)
inputRows(since.rows).forEach((row) => applyJournalRow(current, row))
expect(current.items.get(agentJournalItemKey(note))?.body).toEqual(unconfirmed)
expect(
renderJournalState(current).items.find((item) => item.itemId === agentJournalItemKey(note))
?.body
).toEqual({ kind: 'status', text: 'Cancellation requested.' })
expect(JSON.stringify(since.rows)).toBe(rawJson)
expect(journal.itemBody(agentJournalItemKey(note))).toEqual(unconfirmed)
} finally {
await journals.closeAll()
rmSync(directory, { recursive: true, force: true })
}
},
120_000
)
function item(ordinal: number): AgentJournalItemIdentity {
return { provider: 'codex', threadId: 'thread-1', turnId: 'turn-1', ordinal }
}
type OldReplay = {
state: { items: Map<string, unknown> }
readOnly: boolean
corrupt: boolean
malformedRows: number
truncateFrom?: number
}
// Both downgrade probes load real old builds, including cold extraction and transforms.
test("an older build keeps every row around a Stop's event, a Resume and a reopen mark, and folds the rows after them", async () => {
const directory = mkdtempSync(join(tmpdir(), 'orca-stop-event-downgrade-'))
const journals = createTrackedJournalOpener({ legacyInputs: true })
try {
// This build: history, a person's Stop, a Resume, then more history.
const journal = await journals.open({ identity: IDENTITY, stateDirectory: directory })
const append = (ordinal: number, text: string) =>
journal.appendItem(
item(ordinal),
{ kind: 'status', text },
{ fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE }
)
await append(0, 'before the Stop')
const beforeMarks = journal.cursor()
await journal.appendStopEvent({ reason: 'user-stop', turnId: 'turn-1', caller: 'client-1' }, 1)
await journal.appendStopEvent({ reason: 'user-close', turnId: 'turn-1' }, 1)
await journal.appendQueueResume(1)
await journal.appendQueueReopen(1)
const afterMarks = journal.cursor()
await append(1, 'after the Stop')
// As stored: a read raises each row's `v` to the newest this build reads, which the older
// build does not.
const rows: JournalRow[] = storedRows(directory).map((json) => JSON.parse(json))
// The older build, after a downgrade, replays the same rows from its own database.
const checkout = await materializeReleaseCheckout(BASELINE_REF)
const [database, table, open, reducer, batch] = await Promise.all(
[
`${JOURNAL}/journal-database.ts`,
`${JOURNAL}/journal-row-table.ts`,
`${JOURNAL}/journal-open.ts`,
`${JOURNAL}/journal-reducer.ts`,
'src/main/native-chat/agent-session-wire/agent-session-journal-batch.ts'
].map((path) => importReleaseCheckoutModule(checkout, path))
)
const openJournalDatabase = releaseModuleExport<
(path: string) => { db: { close: () => void } }
>(database, 'openJournalDatabase')
const upsertJournalSessionRow = releaseModuleExport<
(...args: [unknown, string, string, number]) => void
>(table, 'upsertJournalSessionRow')
const insertJournalRow = releaseModuleExport<(...args: [unknown, string, JournalRow]) => void>(
table,
'insertJournalRow'
)
const replayJournal = releaseModuleExport<
(...args: [unknown, boolean, string]) => OldReplay | null
>(open, 'replayJournal')
const renderJournalState = releaseModuleExport<(state: unknown) => unknown>(
reducer,
'renderJournalState'
)
const projectJournalBatch = releaseModuleExport<
(input: { rows: readonly JournalRow[]; snapshot: unknown; afterSequence: number }) => {
ok: boolean
batch?: { items: unknown[]; removedItemIds: string[] }
}
>(batch, 'projectJournalBatch')
const { db } = openJournalDatabase(join(directory, 'older-build-journal.sqlite'))
try {
upsertJournalSessionRow(db, IDENTITY.sessionId, journal.epoch, 1)
for (const row of rows) {
insertJournalRow(db, IDENTITY.sessionId, row)
}
const replayed = replayJournal(db, false, IDENTITY.sessionId)
expect(replayed).toMatchObject({ readOnly: false, corrupt: false, malformedRows: 0 })
expect(replayed?.truncateFrom).toBeUndefined()
expect([...(replayed?.state.items.keys() ?? [])]).toHaveLength(2)
// An older client is sent only ids no item uses, removed.
const projected = projectJournalBatch({
rows: rows.filter(
(row) => row.seq > beforeMarks.sequence && row.seq <= afterMarks.sequence
),
snapshot: renderJournalState(replayed?.state),
afterSequence: beforeMarks.sequence
})
expect(projected.ok).toBe(true)
expect(projected.batch?.items).toEqual([])
expect(projected.batch?.removedItemIds).toHaveLength(3)
const liveIds = new Set(journal.snapshot().items.map((entry) => entry.itemId))
expect(projected.batch?.removedItemIds.some((id) => liveIds.has(id))).toBe(false)
} finally {
db.close()
}
} finally {
await journals.closeAll()
rmSync(directory, { recursive: true, force: true })
}
}, 120_000)
type OlderJournal = {
isReadOnly: boolean
cursor: () => { epoch: string; sequence: number }
snapshot: () => { items: { itemId: string }[] }
appendItem: (...args: [AgentJournalItemIdentity, unknown, unknown]) => Promise<unknown>
}
@@ -263,21 +83,58 @@ function storedRows(directory: string): string[] {
}
}
test("an older build opens this build's journal writable and appends to it; the pause survives the round trip", async () => {
const SCOPE = { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE }
/** A chat an older build saved, before this build first converts it; returns the row writer. */
function savedBeforeConversion(directory: string) {
const database = openTestJournalHostDatabase(directory)
seedLegacyTestJournal(database, { identity: IDENTITY, mintEpoch: () => EPOCH, now: () => 1_000 })
return (seq: number, fields: Record<string, unknown>): string => {
const row = JSON.stringify({
// A version the older build reads, so it meets the kind rather than a newer version.
v: AGENT_SESSION_JOURNAL_TURN_ITEM_SCHEMA_VERSION,
epoch: EPOCH,
seq,
fence: 1,
ts: 1_000 + seq,
...fields
})
insertTestJournalRowJson(database.db, IDENTITY.sessionId, seq, row)
return row
}
}
const statusRow = (ordinal: number, text: string) => ({
kind: 'item',
itemId: agentJournalItemKey(item(ordinal)),
revision: 1,
turnScope: AGENT_JOURNAL_THREAD_SCOPE,
body: { kind: 'status', text }
})
// Conversion is one-way: a downgraded build sees the chat as converted, and its later writes
// are not carried forward, so the upgraded chat is this build's own.
test("an older build opens and appends to the chat as converted; upgraded again, the chat and its Stop pause are this build's", async () => {
const directory = mkdtempSync(join(tmpdir(), 'orca-stop-event-writable-downgrade-'))
const journals = createTrackedJournalOpener({ legacyInputs: true })
const itemIds = (journal: Pick<OlderJournal, 'snapshot'>) =>
journal.snapshot().items.map((entry) => entry.itemId)
const journals = createTrackedJournalOpener()
const savedIds = async (journal: AgentSessionJournal) =>
(await readTestJournalItems(journal)).map((entry) => entry.itemId)
const pauses = async (journal: AgentSessionJournal) =>
(await journal.queuedMessages.pauses()).map((pause) => pause.reason)
try {
savedBeforeConversion(directory)(2, statusRow(0, 'before the upgrade'))
await closeTestJournalHostDatabase(directory)
const converted = storedRows(directory)
const journal = await journals.open({ identity: IDENTITY, stateDirectory: directory })
const scope = { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE }
await journal.appendItem(item(0), { kind: 'status', text: 'before the Stop' }, scope)
await journal.appendItem(item(1), { kind: 'status', text: 'before the Stop' }, SCOPE)
await journal.appendStopEvent({ reason: 'user-stop', turnId: 'turn-1', caller: 'client-1' }, 1)
await journal.appendItem(item(1), { kind: 'status', text: 'after the Stop' }, scope)
const wrote = { cursor: journal.cursor(), items: itemIds(journal) }
expect(journal.queuedMessages.pauses().map((pause) => pause.reason)).toEqual(['stopped'])
await journal.appendItem(item(2), { kind: 'status', text: 'after the Stop' }, SCOPE)
const wrote = { cursor: journal.cursor(), items: await savedIds(journal) }
expect(wrote.items).toEqual([0, 1, 2].map((ordinal) => agentJournalItemKey(item(ordinal))))
expect(await pauses(journal)).toEqual(['stopped'])
await journals.closeAll()
const rowsBefore = storedRows(directory)
expect(storedRows(directory)).toEqual(converted)
const checkout = await materializeReleaseCheckout(WRITABLE_BASELINE_REF)
const support = await importReleaseCheckoutModule(
@@ -288,21 +145,21 @@ test("an older build opens this build's journal writable and appends to it; the
try {
const downgraded = await older.open({ identity: OLDER_IDENTITY, stateDirectory: directory })
expect(downgraded.isReadOnly).toBe(false)
expect(downgraded.cursor()).toEqual(wrote.cursor)
expect(itemIds(downgraded)).toEqual(wrote.items)
await downgraded.appendItem(item(2), { kind: 'status', text: 'the older build' }, scope)
expect(downgraded.snapshot().items.map((entry) => entry.itemId)).toEqual([
agentJournalItemKey(item(0))
])
await downgraded.appendItem(item(3), { kind: 'status', text: 'the older build' }, SCOPE)
} finally {
await older.closeAll()
}
const rowsAfter = storedRows(directory)
expect(rowsAfter.slice(0, rowsBefore.length)).toEqual(rowsBefore)
expect(rowsAfter).toHaveLength(rowsBefore.length + 1)
expect(rowsAfter.slice(0, converted.length)).toEqual(converted)
expect(rowsAfter).toHaveLength(converted.length + 1)
// Upgraded again: the older build's row folds, and the person's Stop still pauses the queue.
const upgraded = await journals.open({ identity: IDENTITY, stateDirectory: directory })
expect(upgraded.cursor().sequence).toBe(wrote.cursor.sequence + 1)
expect(itemIds(upgraded)).toEqual([...wrote.items, 'codex:thread-1:turn-1:2'])
expect(upgraded.queuedMessages.pauses().map((pause) => pause.reason)).toEqual(['stopped'])
expect(upgraded.cursor()).toEqual(wrote.cursor)
expect(await savedIds(upgraded)).toEqual(wrote.items)
expect(await pauses(upgraded)).toEqual(['stopped'])
} finally {
await journals.closeAll()
rmSync(directory, { recursive: true, force: true })
@@ -313,30 +170,13 @@ test("an older build opens this build's journal writable and appends to it; the
// Move the pinned baseline once a released reader preserves them.
test("this build keeps a newer build's row kind and refuses the load; a build before it deletes it", async () => {
const directory = mkdtempSync(join(tmpdir(), 'orca-newer-kind-downgrade-'))
const journals = createTrackedJournalOpener({ legacyInputs: true })
const journals = createTrackedJournalOpener()
const newerKinds = () => storedRows(directory).filter((row) => row.includes('"future-mark"'))
try {
const scope = { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE }
const journal = await journals.open({ identity: IDENTITY, stateDirectory: directory })
await journal.appendItem(item(0), { kind: 'status', text: 'before' }, scope)
const at = journal.cursor().sequence + 1
const newer = JSON.stringify({
// A version the older build reads, so it meets the kind rather than a newer version.
v: AGENT_SESSION_JOURNAL_TURN_ITEM_SCHEMA_VERSION,
kind: 'future-mark',
epoch: journal.epoch,
seq: at,
fence: 1,
ts: 2_000,
payload: { said: 'by a newer build' }
})
await journals.closeAll()
insertTestJournalRowJson(
openTestJournalHostDatabase(directory).db,
IDENTITY.sessionId,
at,
newer
)
// A saved chat with an item, then a newer row, before this build first converts it.
const save = savedBeforeConversion(directory)
save(2, statusRow(0, 'before'))
const newer = save(3, { kind: 'future-mark', payload: { said: 'by a newer build' } })
await closeTestJournalHostDatabase(directory)
const rowsBefore = storedRows(directory)