Merge commit 'ecc886b7693742fbe382951bf8e6498a3fa1dfb3' into brennanb2025/native-chat-sqlite-4a-harness

This commit is contained in:
Brennan Benson
2026-11-09 03:37:50 -08:00
391 changed files with 6574 additions and 13243 deletions
-1
View File
@@ -920,7 +920,6 @@
1 test-timeout-needs-reason:tests/e2e/completed-worker-retirement-resume.spec.ts
2 test-timeout-needs-reason:tests/e2e/cross-version-wire/agent-session-clear-old-client.unit.test.ts
2 test-timeout-needs-reason:tests/e2e/cross-version-wire/agent-session-newer-chat-refusal.unit.test.ts
1 test-timeout-needs-reason:tests/e2e/cross-version-wire/agent-session-stop-event-downgrade.unit.test.ts
1 test-timeout-needs-reason:tests/e2e/cross-version-wire/cross-version-agent-session-send.unit.test.ts
3 test-timeout-needs-reason:tests/e2e/cross-version-wire/cross-version-terminal-wire.unit.test.ts
4 test-timeout-needs-reason:tests/e2e/cross-version-wire/legacy-relay-bridge.unit.test.ts
@@ -443,7 +443,7 @@ describe('real phone inline composer from More through picker pruning and host b
})
// Equal Save proves no durable sender mutation or update RPC.
await act(async () => button('Save edited message').props.onPress())
expect(rig.read(before.messageId)?.body.from).toEqual(from)
expect((await rig.read(before.messageId))?.body.from).toEqual(from)
expect(
sendRequest.mock.calls.some(([method]) => method === 'agentSession.queuedMessageUpdate')
).toBe(false)
@@ -455,7 +455,7 @@ describe('real phone inline composer from More through picker pruning and host b
])
expect(error.mock.calls).toEqual([])
expect(state.hook!.queued.editor).toBeUndefined()
const accepted = rig.read(before.messageId)!
const accepted = (await rig.read(before.messageId))!
expect(accepted).toMatchObject({
position: before.position,
options: before.options,
@@ -463,7 +463,7 @@ describe('real phone inline composer from More through picker pruning and host b
})
expect(accepted.body).not.toHaveProperty('from')
await rig.restart()
const reopened = rig.read(before.messageId)!
const reopened = (await rig.read(before.messageId))!
expect(reopened).toEqual(accepted)
const publication = snapshotEvent({
queuedMessages: [await rig.published(reopened.messageId)]
@@ -29,12 +29,10 @@ import { recordingStructuredAgentSessionLogger } from '../native-chat/agent-sess
import { agentSessionRecordFixture } from '../native-chat/agent-session-record-test-fixture'
import {
openTestAgentSessionRecordStore,
seedTestAgentSessionRecordStore
seedTestAgentSessionRecordStore,
openTestAgentSessionDatabase
} from '../runtime/agent-session/records/agent-session-record-store-test-harness'
import {
openTestJournalHostDatabase,
closeTestJournalHostDatabase
} from '../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { closeTestJournalHostDatabase } from '../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
const unused = (): never => {
throw new Error('declaration fixture must not start or mutate an agent')
@@ -173,7 +171,7 @@ export async function readRecordedAcpOptions(
adapter,
agents: declaredEffortAgents(agent, adapter),
logger,
journalDatabase: openTestJournalHostDatabase(root),
journalDatabase: await openTestAgentSessionDatabase(root),
claimKeyId: 'test-key',
modelCatalog: {
captureRead: hostTestCatalogReadOperation,
@@ -9,10 +9,8 @@ import {
hostTestMessage
} from '../native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture'
import { createStructuredAgentSessionLogger } from '../native-chat/agent-session-wire/host/structured-agent-session-logger'
import {
ensureStructuredAgentSessionHost,
stopStructuredAgentSessionRuntime
} from '../runtime/agent-session/structured-agent-session-runtime'
import { stopStructuredAgentSessionRuntime } from '../runtime/agent-session/structured-agent-session-runtime'
import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from '../runtime/agent-session/records/agent-session-record-store-test-harness'
import { acpScriptedChild } from './acp-scripted-child.test-fixture'
let stateDirectory: string | undefined
@@ -21,8 +21,8 @@ it('propagates a temporary load envelope before takeover or a dead-child asserti
resume: {
key: 'saved-key',
sessionId: 'saved-id',
mayBeUnsaved: () => false,
unannouncedLosses: () => []
mayBeUnsaved: async () => false,
lossKeys: []
}
},
script: (agent) =>
@@ -52,8 +52,8 @@ it.each([
resume: {
key: 'saved-key',
sessionId: 'saved-id',
mayBeUnsaved: () => false,
unannouncedLosses: () => []
mayBeUnsaved: async () => false,
lossKeys: []
}
},
deps: {
+2 -59
View File
@@ -1,16 +1,10 @@
import { agentSessionFailureFact } from '../../shared/agent-session/failures/agent-session-failure'
import { agentSessionFailureWords } from '../../shared/agent-session/failures/agent-session-failure-words'
import type {
AgentSessionProviderHandleChain,
AgentSessionProviderHandleLink
} from '../../shared/agent-session/lifecycle/agent-session-provider-handle'
import { agentJournalItemKey } from '../../shared/agent-session/journal/agent-session-journal-item-key'
import { createLegacyProviderTimelineIdentityScheme } from '../native-chat/agent-session-timeline/provider-timeline-identity'
import type { AgentSessionProviderHandleLink } from '../../shared/agent-session/lifecycle/agent-session-provider-handle'
import type { ProviderTimelineEvent } from '../native-chat/agent-session-timeline/provider-timeline-event'
import { AcpAuthRequiredError, AcpRpcError } from './acp-errors'
import type { AcpStructuredLaunch } from './acp-structured-launch-resolution'
import { providerRequestTimeoutOf } from '../provider-process/provider-request-timeout'
import type { AcpSavedSessionReads } from './acp-structured-resume-history'
import { temporaryAcpStartupAnswer } from './acp-startup-outcome'
@@ -56,6 +50,7 @@ export function acpSessionNotRestoredRow(
{
type: 'item.update',
item: acpSessionNotRestoredItem(lostKey),
lossWarning: true,
body: {
kind: 'status',
tone: 'warning',
@@ -68,55 +63,3 @@ export function acpSessionNotRestoredRow(
}
]
}
/** Retry missing loss-warning rows on successful starts; unreadable journals suppress new warnings. */
export async function acpUnannouncedLosses(
reads: AcpSavedSessionReads,
sessionId: string,
chain: AgentSessionProviderHandleChain
): Promise<string[]> {
// Any later session in the chain may have written a lost link's warning, keyed by its own id.
const warnings = chain.flatMap((link, index) =>
link.replaces?.reason === 'restore-failed'
? [
{
key: link.replaces.key,
itemIds: warningItemIds(sessionId, link.replaces.key, chain.slice(index))
}
]
: []
)
if (warnings.length === 0) {
return []
}
let saved: ReadonlySet<string>
try {
saved = await reads.savedItemIds(
sessionId,
warnings.flatMap((warning) => warning.itemIds)
)
} catch {
return []
}
// Warnings survive even if the replacement session that wrote them is superseded.
return warnings
.filter((warning) => !warning.itemIds.some((itemId) => saved.has(itemId)))
.map((warning) => warning.key)
}
function warningItemIds(
sessionId: string,
lostKey: string,
writers: AgentSessionProviderHandleChain
): string[] {
return writers.map(({ handle }) =>
agentJournalItemKey(
createLegacyProviderTimelineIdentityScheme({ agent: handle.agent, sessionId }).item({
namespace: handle.nativeId,
family: 'item',
key: { source: 'provider', value: acpSessionNotRestoredItem(lostKey) },
thread: handle.nativeId
})
)
)
}
@@ -1,3 +1,4 @@
import { readTestHostJournal } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { afterEach, expect, it, vi } from 'vitest'
import { readAgentJournalTurn } from '../../shared/agent-session/lifecycle/agent-session-turn-record'
import { closeProviderTimelineRigs } from '../native-chat/agent-session-timeline/provider-timeline-assembler-test-support'
@@ -93,7 +94,7 @@ it('retains the first refused OMP message while later messages queue and complet
).toEqual([])
})
expect(await messages()).toEqual(['first', 'second', 'third'])
const snapshot = await host.journalSnapshot(SESSION)
const snapshot = await readTestHostJournal(host, SESSION)
expect(snapshot.submissions).toHaveLength(3)
expect(snapshot.submissions.every((row) => row.dispatchState === 'accepted')).toBe(true)
})
@@ -284,7 +284,7 @@ describe('ACP compaction settlement', () => {
})
])
// The agent's own failure, so cards waiting behind the compaction stay held.
expect(rig.rig.journal.inputOrder.newestTurn.failure()).toBeGreaterThan(0)
expect(await rig.rig.journal.queuedMessages.failureHolds()).toBe(true)
})
it('ends a stopped compaction as cancelled, with no result row', async () => {
+1 -1
View File
@@ -203,7 +203,7 @@ export async function initializeAcpStructuredSession(
}
// Include warning rows missed by earlier failed starts.
const lost = [
...((await launch.resume?.unannouncedLosses()) ?? []),
...(launch.resume?.lossKeys ?? []),
...(takeover?.replaces ? [takeover.replaces.key] : [])
]
for (const key of lost) {
@@ -1,3 +1,7 @@
import {
readTestHostJournal,
readTestJournalSubmissions
} from '../native-chat/agent-session-journal/journal-observation.test-support'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { readAgentJournalTurn } from '../../shared/agent-session/lifecycle/agent-session-turn-record'
import { readAgentSessionFailureFact } from '../../shared/agent-session/failures/agent-session-failure'
@@ -134,7 +138,7 @@ describe('a Grok crash whose exit is not proven yet', () => {
const next = await send(host, 'next')
await waitFor(async () =>
expect(
(await host.journalSnapshot(SESSION)).submissions.find(
(await readTestHostJournal(host, SESSION)).submissions.find(
(entry) => entry.clientMessageId === next
)
).toMatchObject({
@@ -216,7 +220,7 @@ describe('closing or stopping a Grok chat while it starts', () => {
expect(restarted.exited).toBe(true)
await host.flushStreamedEvents(SESSION)
const journal = host.collaboratorsForTests().sessions.get(SESSION)!.journal
expect(journal.submissions()).toMatchObject([
expect(await readTestJournalSubmissions(journal)).toMatchObject([
{ dispatchState: 'rejected', rejection: { kind: 'cancelled' } }
])
const rows = (await host.history({ sessionId: SESSION, direction: 'tail' })).page.items
@@ -286,7 +290,9 @@ describe('a Grok send whose acceptance could not be written', () => {
await waitFor(async () => expect((await host.currentWork(SESSION))?.working()).toBe(false))
// Rejected journal admission leaves the send pending.
expect(
(await host.journalSnapshot(SESSION)).submissions.find((row) => row.clientMessageId === sent)
(await readTestHostJournal(host, SESSION)).submissions.find(
(row) => row.clientMessageId === sent
)
).toMatchObject({ dispatchState: 'pending' })
await host.close(SESSION, 'user-close')
})
@@ -1,3 +1,4 @@
import { readTestJournalSubmissions } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import { afterEach, expect, it, onTestFinished, vi } from 'vitest'
@@ -162,27 +163,29 @@ async function openModels(
places: state.deliveryOrder['lanes'].get(SESSION)?.places.size ?? 0,
lanes: state.deliveryOrder['lanes'].size
})
const submissionFor = (id: string) => {
const submissionFor = async (id: string) => {
const current = journal()
return current.submission(current.queuedMessages.get(id)?.consumedAs ?? id)
return current.reads.submission((await current.queuedMessages.get(id))?.consumedAs ?? id)
}
const complete = async (id: string) => {
await waitFor(() => expect(submissionFor(id)?.dispatchState).toMatch(/admitted|accepted/))
await waitFor(async () =>
expect((await submissionFor(id))?.dispatchState).toMatch(/admitted|accepted/)
)
const frame = framesOf(rig.rig.child(), 'session/prompt').at(-1)!
const turn = journal().runningTurn()?.turnId
const turn = (await journal().reads.runningTurn())?.turnId
rig.rig.child().agent.reply(frame, { stopReason: 'end_turn' })
await rig.rig.settle()
await rig.host.flushStreamedEvents(SESSION)
await waitFor(() => expect(journal().runningTurn()?.turnId).not.toBe(turn))
await waitFor(async () => expect((await journal().reads.runningTurn())?.turnId).not.toBe(turn))
expect(
structuredAgentSessionHandingOver(journal()).has(submissionFor(id)!.clientMessageId)
structuredAgentSessionHandingOver(journal()).has((await submissionFor(id))!.clientMessageId)
).toBe(false)
}
const defer = async (path: string | false) => {
const busy = path === 'busy' ? await send(rig.host, 'busy') : undefined
if (busy) {
await waitFor(() =>
expect(journal().submission(busy)?.dispatchState).toMatch(/admitted|accepted/)
await waitFor(async () =>
expect((await journal().reads.submission(busy))?.dispatchState).toMatch(/admitted|accepted/)
)
}
if (path === 'held') {
@@ -203,7 +206,7 @@ async function openModels(
messageId: 'held'
})
).toMatchObject({ ok: true })
await complete(journal().submissions().at(-1)!.clientMessageId)
await complete((await readTestJournalSubmissions(journal())).at(-1)!.clientMessageId)
}
}
}
@@ -302,7 +305,8 @@ it.each([10, 100, 'Send', 'rejection'] as const)(
[second, PRO]
] as const) {
expect(
rig.journal().queuedMessages.get(id)?.options ?? rig.submissionFor(id)?.options
(await rig.journal().queuedMessages.get(id))?.options ??
(await rig.submissionFor(id))?.options
).toEqual({ model, effort: 'low' })
await rig.complete(id)
}
@@ -132,7 +132,7 @@ it.each(['idle', 'busy', 'resting'] as const)(
const journal = rig.host.collaboratorsForTests().sessions.get(SESSION)!.journal
const prompt = framesOf(rig.rig.child(), 'session/prompt').at(-1)!
rig.rig.child().agent.reply(prompt, { stopReason: 'end_turn' })
await waitFor(() => expect(journal.runningTurn()).toBeNull())
await waitFor(async () => expect(await journal.reads.runningTurn()).toBeNull())
await rig.rig.settle()
await rig.host.flushStreamedEvents(SESSION)
}
@@ -1,3 +1,4 @@
import { readTestJournalSubmissions } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { structuredAgentSessionHandingOver } from '../native-chat/agent-session-wire/delivery/structured-agent-session-handing-over'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { closeProviderTimelineRigs } from '../native-chat/agent-session-timeline/provider-timeline-assembler-test-support'
@@ -40,7 +41,11 @@ describe('a Grok model pick Grok never answers during send', () => {
expect(await invalid).toMatchObject({ ok: false })
expect(await host.cancel(CALLER, request)).toMatchObject({ ok: true })
expect(
host.collaboratorsForTests().sessions.get(SESSION)?.journal.submissions()[0]?.dispatchState
(
await readTestJournalSubmissions(
host.collaboratorsForTests().sessions.get(SESSION)!.journal
)
)[0]?.dispatchState
).toBe('rejected')
expect(
(await rows()).filter(
@@ -79,14 +84,14 @@ describe('a Grok model pick Grok never answers during send', () => {
rig.child().agent.reply(frame!, { configOptions: GROK_CONFIG_OPTIONS })
await rig.settle()
await host.flushStreamedEvents(SESSION)
await journal['queue'].drain()
await journal.readInOrder(() => undefined)
expect(framesOf(rig.child(), 'session/prompt')).toEqual([])
expect(original).toBeDefined()
expect(structuredAgentSessionHandingOver(journal).has(id)).toBe(false)
expect(journal.queuedMessages.list()).toMatchObject([
expect(await journal.queuedMessages.list()).toMatchObject([
{ messageId: id, body: { blocks: [{ text: 'hello' }] } }
])
expect(journal.submission(id)).toMatchObject({
expect(await journal.reads.submission(id)).toMatchObject({
keptAsQueuedMessageId: id,
rejection: { kind: 'chatClosed' }
})
@@ -107,9 +112,13 @@ describe('a Grok model pick Grok never answers during send', () => {
const child = rig.child()
const [old] = framesOf(child, 'session/set_config_option')
await vi.advanceTimersByTimeAsync(100)
await waitFor(() =>
await waitFor(async () =>
expect(
host.collaboratorsForTests().sessions.get(SESSION)?.journal.submissions()[0]?.dispatchState
(
await readTestJournalSubmissions(
host.collaboratorsForTests().sessions.get(SESSION)!.journal
)
)[0]?.dispatchState
).toBe('rejected')
)
vi.useRealTimers()
@@ -126,7 +135,11 @@ describe('a Grok model pick Grok never answers during send', () => {
await rig.settle()
expect(framesOf(child, 'session/prompt')).toHaveLength(0)
expect(
host.collaboratorsForTests().sessions.get(SESSION)?.journal.submissions()[0]?.dispatchState
(
await readTestJournalSubmissions(
host.collaboratorsForTests().sessions.get(SESSION)!.journal
)
)[0]?.dispatchState
).toBe('rejected')
expect((await host.readOptions(SESSION)).current.model).toBe(savedModel)
expect(store.getRecord(SESSION)?.options?.model).toBe(savedModel)
@@ -9,7 +9,7 @@ import { HOST_TEST_SESSION as SESSION } from '../native-chat/agent-session-wire/
import type { AcpScriptedAgent } from './acp-scripted-agent.test-support'
import { GROK, GROK_CONFIG_OPTIONS, PROVIDER_SESSION } from './acp-structured-adapter.test-support'
import { ACP_HANDLE_TRANSPORT } from './acp-structured-agent-definitions'
import { restoreJournal } from '../native-chat/agent-session-journal/replay/journal-open'
import { journalSqlReads } from '../native-chat/agent-session-journal/journal-sql-reads'
import { AgentSessionJournal } from '../native-chat/agent-session-journal/journal-store'
import { createAcpStructuredLaunchResolver } from '../runtime/agent-session/launch/structured-agent-launch-composition.test-support'
import {
@@ -27,13 +27,15 @@ afterEach(async () => {
const FRESH = 'grok-fresh-session'
const FRESH_2 = 'grok-second-fresh-session'
const FRESH_3 = 'grok-third-fresh-session'
const FRESH_4 = 'grok-fourth-fresh-session'
const keyOf = (nativeId: string) =>
agentSessionProviderHandleKey({ transport: ACP_HANDLE_TRANSPORT, agent: 'grok', nativeId })
type Lost = Map<string, { code: number; message: string }>
function sessionsLost(loads: string[], lost: Lost) {
const opened = [PROVIDER_SESSION, FRESH, FRESH_2]
const opened = [PROVIDER_SESSION, FRESH, FRESH_2, FRESH_3, FRESH_4]
return (agent: AcpScriptedAgent) => {
agent.on('session/new', (frame) => {
agent.reply(frame, {
@@ -81,7 +83,10 @@ async function openRestoreRig(code: number, message: string) {
const { rig, host, store, journalDatabase } = opened
resolver.resolve = createAcpStructuredLaunchResolver(GROK, {
store,
readJournal: (sessionId) => restoreJournal(journalDatabase.db, sessionId),
savedSession: {
hasProviderTurn: (sessionId, nativeId) =>
journalSqlReads(journalDatabase.readers, sessionId).hasProviderTurn(nativeId)
},
resolveWorkspacePath: async () => '/workspace',
resolveEnvironment: async () => ({ PATH: '/usr/bin' }),
resolveCommand: () => '/fake/grok'
@@ -197,7 +202,7 @@ describe('a replacement after its earlier attach failed to open the journal', ()
const spawnedBefore = [...rig.spawned]
const chainBefore = structuredClone(store.getRecord(SESSION)?.providerHandleChain)
// Fail before the spawned child's background handshake can prove a replacement link.
vi.spyOn(AgentSessionJournal.prototype, 'open').mockRejectedValueOnce(
vi.spyOn(AgentSessionJournal, 'open').mockRejectedValueOnce(
new Error('journal path unavailable')
)
expect(await host.attach(CALLER, attachParams(fence()))).toMatchObject({
@@ -219,7 +224,8 @@ describe('a replacement after its earlier attach failed to open the journal', ()
}
it('is written when Grok never saved the fresh session and a newer one supersedes it', async () => {
const { host, store, fence, loads, lost, warnings, ready } = await failAttachBeforeReplacement()
const { host, store, fence, loads, lost, warnings, ready, exchange } =
await failAttachBeforeReplacement()
expect(await host.attach(CALLER, attachParams(fence()))).toMatchObject({ ok: true })
await ready()
await host.close(SESSION, 'user-close')
@@ -236,11 +242,32 @@ describe('a replacement after its earlier attach failed to open the journal', ()
replaces: { key: keyOf(PROVIDER_SESSION), reason: 'restore-failed' }
})
expect(await warnings()).toHaveLength(1)
const firstWarning = (await warnings())[0]
await host.close(SESSION, 'user-close')
for (const missing of [FRESH_2, FRESH_3]) {
lost.set(missing, { code: -32002, message: 'Resource not found' })
expect(await host.attach(CALLER, attachParams(fence()))).toMatchObject({ ok: true })
await ready()
expect(await warnings()).toEqual([firstWarning])
if (missing === FRESH_2) {
await host.close(SESSION, 'user-close')
}
}
await host.close(SESSION, 'user-close')
expect(await host.attach(CALLER, attachParams(fence()))).toMatchObject({ ok: true })
await ready()
expect(loads).toEqual([PROVIDER_SESSION, FRESH, FRESH_2])
expect(await warnings()).toHaveLength(1)
expect(loads).toEqual([PROVIDER_SESSION, FRESH, FRESH_2, FRESH_3, FRESH_4])
expect(await warnings()).toEqual([firstWarning])
await exchange('remember this', 'saved', FRESH_4)
await host.close(SESSION, 'user-close')
lost.set(FRESH_4, { code: -32603, message: 'session file is corrupt' })
expect(await host.attach(CALLER, attachParams(fence()))).toMatchObject({ ok: true })
await ready()
const afterAnotherLoss = await warnings()
expect(afterAnotherLoss).toHaveLength(2)
expect(afterAnotherLoss[0]).toEqual(firstWarning)
expect(afterAnotherLoss[1]?.itemId).not.toBe(firstWarning?.itemId)
await host.close(SESSION, 'user-close')
})
})
@@ -1,3 +1,4 @@
import { readTestJournalSubmissions } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { afterEach, describe, expect, it } from 'vitest'
import { closeProviderTimelineRigs } from '../native-chat/agent-session-timeline/provider-timeline-assembler-test-support'
import type { StructuredAgentSessionHost } from '../native-chat/agent-session-wire/host/structured-agent-session-host'
@@ -88,7 +89,7 @@ describe('a Grok start the host aborts', () => {
await host.flushStreamedEvents(SESSION)
expect(rig.spawned.filter((step) => step === 'spawn')).toHaveLength(1)
const journal = host.collaboratorsForTests().sessions.get(SESSION)!.journal
expect(journal.submissions()).toMatchObject([
expect(await readTestJournalSubmissions(journal)).toMatchObject([
{ dispatchState: 'rejected', rejection: { kind: 'cancelled' } }
])
})
@@ -157,15 +158,12 @@ async function expectDeliveredAfterStop(
})
rig.child().agent.reply(prompt, { stopReason: 'end_turn' })
const journal = host.collaboratorsForTests().sessions.get(SESSION)!.journal
await waitFor(() =>
expect(
journal.submissions().find((entry) => entry.clientMessageId === secondId)?.dispatchState
).toBe('accepted')
await waitFor(async () =>
expect((await journal.reads.submission(secondId))?.dispatchState).toBe('accepted')
)
expect(journal.submissions().map((entry) => entry.rejection?.kind ?? null)).toEqual([
'cancelled',
null
])
expect(
(await readTestJournalSubmissions(journal)).map((entry) => entry.rejection?.kind ?? null)
).toEqual(['cancelled', null])
await host.flushStreamedEvents(SESSION)
const rows = (await host.history({ sessionId: SESSION, direction: 'tail' })).page.items
expect(rows.flatMap((row) => (row.body.kind === 'status' ? [row.body.text] : []))).toEqual([
@@ -1,3 +1,4 @@
import { readTestHostJournal } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { afterEach, expect, it } from 'vitest'
import { closeProviderTimelineRigs } from '../native-chat/agent-session-timeline/provider-timeline-assembler-test-support'
import { CALLER } from '../native-chat/agent-session-wire/host/testing/structured-agent-session-host.test-fixture'
@@ -17,7 +18,7 @@ it('tells queued messages the agent is signed out when its published startup fai
expect(rig.sent('session/prompt')).toEqual([])
rig.child().agent.fail(request, -32000, 'Authentication required')
await waitFor(async () => {
const snapshot = await host.journalSnapshot(SESSION)
const snapshot = await readTestHostJournal(host, SESSION)
const submission = snapshot.submissions.find((entry) => entry.clientMessageId === messageId)
expect(submission?.handedOverAt).toBeUndefined()
expect(submission).toMatchObject({
@@ -3,7 +3,6 @@ import { z } from 'zod'
import type { AgentJournalMessageItem } from '../../shared/agent-session/journal/agent-session-journal-types'
import { agentSessionProviderHandleKey } from '../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
import { readAgentJournalTurn } from '../../shared/agent-session/lifecycle/agent-session-turn-record'
import { openTestJournalHostDatabase } from '../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { messageText } from '../native-chat/agent-session-timeline/provider-timeline-assembler-test-support'
import { StructuredAgentSessionHost } from '../native-chat/agent-session-wire/host/structured-agent-session-host'
import { StructuredAgentRegistry } from '../native-chat/agent-session-wire/providers/structured-agent-registry'
@@ -19,7 +18,10 @@ import {
hostTestAttachParams
} from '../native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture'
import { AgentSessionRecoveryCapsule } from '../runtime/agent-session/records/agent-session-recovery-capsule'
import { openTestAgentSessionRecordStore } from '../runtime/agent-session/records/agent-session-record-store-test-harness'
import {
openTestAgentSessionRecordStore,
openTestAgentSessionDatabase
} from '../runtime/agent-session/records/agent-session-record-store-test-harness'
import type { AcpScriptedAgent, FakeFrame } from './acp-scripted-agent.test-support'
import {
GROK,
@@ -73,8 +75,8 @@ export function launch(resume: () => boolean, spec = GROK): () => Promise<AcpStr
agent: spec.agent,
nativeId: PROVIDER_SESSION
}),
mayBeUnsaved: () => false,
unannouncedLosses: () => []
mayBeUnsaved: async () => false,
lossKeys: []
}
: null
})
@@ -96,7 +98,7 @@ export async function openHostRig(
let generation = 0
// Forward lifecycle and late-send settlements as the runtime does.
const hosted: { host: StructuredAgentSessionHost | null } = { host: null }
const journalDatabase = openTestJournalHostDatabase(state.root)
const journalDatabase = await openTestAgentSessionDatabase(state.root)
const rig = await openAcpAdapterRig({
...options,
deps: {
@@ -1,4 +1,3 @@
import { memoryJournalMap } from '../native-chat/agent-session-journal/journal-memory-collections'
import { createAcpStructuredLaunchResolver } from '../runtime/agent-session/launch/structured-agent-launch-composition.test-support'
import { mkdtempSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
@@ -9,28 +8,16 @@ import { agentSessionProviderHandleKey } from '../../shared/agent-session/lifecy
import type { AgentSessionProviderHandle } from '../../shared/agent-session/lifecycle/agent-session-provider-handle'
import type { AgentSessionRecord } from '../../shared/agent-session/lifecycle/agent-session-record'
import { agentSessionRecordFixture } from '../../shared/agent-session/lifecycle/agent-session-record.test-fixture'
import {
replayJournal,
restoreJournal,
type JournalLoad
} from '../native-chat/agent-session-journal/replay/journal-open'
import {
openTestJournalHostDatabase,
closeTestJournalHostDatabase,
publishTestJournalEpoch,
insertTestJournalRow,
insertTestJournalRowJson
} from '../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { publishJournalOutcomeEpoch } from '../native-chat/agent-session-journal/outcomes/journal-outcome-write'
import { JOURNAL_OUTCOME_FORMAT } from '../native-chat/agent-session-journal/outcomes/journal-outcome-format'
import type { JournalRow } from '../native-chat/agent-session-journal/durable-rows/journal-row-schema'
import { createJournalReducerState } from '../native-chat/agent-session-journal/projection/journal-reducer'
import { agentJournalItemKey } from '../../shared/agent-session/journal/agent-session-journal-item-key'
import { agentJournalTurnBody } from '../../shared/agent-session/lifecycle/agent-session-turn-record'
import { createTrackedJournalOpener } from '../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { journalSqlReads } from '../native-chat/agent-session-journal/journal-sql-reads'
import { openTestAgentSessionDatabase } from '../runtime/agent-session/records/agent-session-record-store-test-harness'
import {
createLegacyProviderTimelineIdentityScheme,
spellProviderTimelineKey
} from '../native-chat/agent-session-timeline/provider-timeline-identity'
import { acpLaunchSpecFor } from './acp-launch-specs'
import type { AcpSavedSessionReads } from './acp-structured-resume-history'
const GROK = acpLaunchSpecFor('grok')!
const OMP = acpLaunchSpecFor('omp')!
@@ -71,35 +58,17 @@ function ompRecord(options: Record<string, string>, resumes: boolean): AgentSess
})
}
function journalWithTurns(...providerSessions: string[]): JournalLoad {
const state = createJournalReducerState(identity.sessionId, 'epoch-1')
providerSessions.forEach((providerSession, index) => {
const turnId = spellProviderTimelineKey(providerSession, {
source: 'provider',
value: `prompt:m${index}`
})
state.items.set(`turn-${index}`, {
itemId: `turn-${index}`,
revision: 1,
sequence: index + 2,
observedAt: 1,
body: { kind: 'turn', turnId, state: 'completed' }
})
})
return { state, newer: null, damage: null }
const NOTHING_SAVED: AcpSavedSessionReads = {
hasProviderTurn: async () => false
}
function resolver(
record: AgentSessionRecord,
readJournal: () => JournalLoad | null = () => null,
spec = GROK
) {
function resolver(record: AgentSessionRecord, savedSession = NOTHING_SAVED, spec = GROK) {
const searched: (string | null | undefined)[] = []
return {
searched,
resolve: createAcpStructuredLaunchResolver(spec, {
store: { getRecord: () => record, pinLaunchDirectory: vi.fn() },
readJournal,
savedSession,
resolveWorkspacePath: async () => '/repo/worktree',
resolveEnvironment: async () => ({ PATH: '/usr/bin', HOME: '/home/user' }),
resolveLaunchEnv: () => ({ GROK_EXTRA: '1' }),
@@ -112,9 +81,9 @@ function resolver(
}
describe('ACP launch resolution', () => {
it('preserves resume and loss-warning decisions through a restore and an unreadable tail', async () => {
it('resumes a saved exchange and carries loss candidates independently of journal readability', async () => {
const root = mkdtempSync(join(tmpdir(), 'orca-acp-saved-read-'))
const database = openTestJournalHostDatabase(root)
const journals = createTrackedJournalOpener()
try {
const handle: AgentSessionProviderHandle = {
transport: 'acp',
@@ -134,140 +103,53 @@ describe('ACP launch resolution', () => {
}
]
})
publishTestJournalEpoch(database.db, identity.sessionId, 'epoch-1')
insertTestJournalRow(database.db, identity.sessionId, {
v: 4,
epoch: 'epoch-1',
seq: 1,
fence: 1,
ts: 1,
kind: 'epoch',
reason: 'session_created',
providerHandle: { kind: 'opaque', agent: 'grok', value: 'acp-1' }
})
const turn = [...memoryJournalMap(journalWithTurns('acp-1').state.items).values()][0]!
const turnIdentity = createLegacyProviderTimelineIdentityScheme({
agent: 'grok',
sessionId: identity.sessionId
}).item({
namespace: 'acp-1',
family: 'item',
key: { source: 'provider', value: 'turn' },
thread: 'acp-1'
})
insertTestJournalRow(database.db, identity.sessionId, {
v: 4,
epoch: 'epoch-1',
seq: 2,
fence: 1,
ts: 2,
kind: 'item',
itemId: agentJournalItemKey(turnIdentity),
revision: 1,
body: turn.body
})
const readDecisions = async (readJournal: () => JournalLoad | null) => {
const launch = await resolver(record, readJournal).resolve({ identity })
const journal = await journals.open({ stateDirectory: root, identity, now: () => 1 })
const { readers } = await openTestAgentSessionDatabase(root)
const savedSession: AcpSavedSessionReads = {
hasProviderTurn: (sessionId, nativeId) =>
journalSqlReads(readers, sessionId).hasProviderTurn(nativeId)
}
const readDecisions = async (saved: AcpSavedSessionReads) => {
const launch = await resolver(record, saved).resolve({ identity })
return {
unsaved: launch.resume?.mayBeUnsaved(),
losses: launch.resume?.unannouncedLosses()
unsaved: await launch.resume?.mayBeUnsaved(),
losses: launch.resume?.lossKeys
}
}
const full = () => replayJournal(database.db, identity.sessionId)
const restored = () => restoreJournal(database.db, identity.sessionId)
expect(await readDecisions(restored)).toStrictEqual(await readDecisions(full))
expect(await readDecisions(restored)).toEqual({ unsaved: false, losses: [lostKey] })
insertTestJournalRowJson(database.db, identity.sessionId, 3, '{damaged')
expect(await readDecisions(restored)).toStrictEqual(await readDecisions(full))
expect(await readDecisions(restored)).toEqual({ unsaved: false, losses: [] })
// Nothing saved on acp-1 yet, so the created session may hold an unsaved exchange.
expect(await readDecisions(savedSession)).toEqual({ unsaved: true, losses: [lostKey] })
const turnId = spellProviderTimelineKey('acp-1', { source: 'provider', value: 'prompt:m0' })
await journal.appendItem(
createLegacyProviderTimelineIdentityScheme({
agent: 'grok',
sessionId: identity.sessionId
}).item({
namespace: 'acp-1',
family: 'item',
key: { source: 'provider', value: 'turn' },
thread: 'acp-1'
}),
agentJournalTurnBody({ turnId, state: 'completed', startedAt: 1 }),
{ fence: 1, turnScope: { kind: 'thread' } }
)
// The turn on acp-1 is found, so the exchange is saved; the loss has no warning yet.
expect(await readDecisions(savedSession)).toEqual({ unsaved: false, losses: [lostKey] })
// An unreadable chat cannot prove no exchange; its writer still owns warning admission.
const unreadable: AcpSavedSessionReads = {
hasProviderTurn: () => Promise.reject(new Error('journal unreadable'))
}
expect(await readDecisions(unreadable)).toEqual({ unsaved: false, losses: [lostKey] })
} finally {
await closeTestJournalHostDatabase(root)
await journals.closeAll()
rmSync(root, { recursive: true, force: true })
}
})
it('reads an outcome chat from its records, and a newer one as unreadable', async () => {
const root = mkdtempSync(join(tmpdir(), 'orca-acp-outcome-read-'))
const database = openTestJournalHostDatabase(root)
try {
const handle: AgentSessionProviderHandle = {
transport: 'acp',
agent: 'grok',
nativeId: 'acp-1'
}
const lostKey = agentSessionProviderHandleKey({ ...handle, nativeId: 'acp-lost' })
const record = grokRecord({
providerHandleChain: [
{
linkId: 'head',
origin: 'created',
mintedAtFence: 1,
observedAt: 1,
handle,
replaces: { key: lostKey, reason: 'restore-failed', replacedAt: 1 }
}
]
})
const turn = [...memoryJournalMap(journalWithTurns('acp-1').state.items).values()][0]!
const rows: JournalRow[] = [
{
v: 4,
epoch: 'epoch-1',
seq: 1,
fence: 1,
ts: 1,
kind: 'epoch',
reason: 'session_created',
providerHandle: { kind: 'opaque', agent: 'grok', value: 'acp-1' }
},
{
v: 4,
epoch: 'epoch-1',
seq: 2,
fence: 1,
ts: 2,
kind: 'item',
itemId: 'turn',
revision: 1,
body: turn.body
}
]
// A chat this build created holds only records.
database.transaction((db) => {
publishTestJournalEpoch(db, identity.sessionId, 'epoch-1')
publishJournalOutcomeEpoch(db, identity.sessionId, 'epoch-1', rows)
})
const readDecisions = async () => {
const launch = await resolver(record, () =>
restoreJournal(database.db, identity.sessionId)
).resolve({ identity })
return {
unsaved: launch.resume?.mayBeUnsaved(),
losses: launch.resume?.unannouncedLosses()
}
}
expect(restoreJournal(database.db, identity.sessionId)?.state.items.has('turn')).toBe(true)
// The turn on acp-1 is found, so the exchange is saved; the loss has no warning yet.
expect(await readDecisions()).toEqual({ unsaved: false, losses: [lostKey] })
// A newer build's records are unreadable here, never a missing chat.
database.db
.prepare('UPDATE journal_outcome_epochs SET format = ?')
.run(JOURNAL_OUTCOME_FORMAT + 1)
const newer = restoreJournal(database.db, identity.sessionId)
expect(newer).not.toBeNull()
expect(newer?.newer).toStrictEqual({ sequence: 1 })
expect(newer?.damage).toBeNull()
expect(await readDecisions()).toEqual({ unsaved: false, losses: [] })
} finally {
await closeTestJournalHostDatabase(root)
rmSync(root, { recursive: true, force: true })
}
})
it('starts a new OMP chat with its saved model on the launch line, where OMP resolves roles', async () => {
const start = (resumes: boolean) =>
resolver(
ompRecord({ model: '@worker_be', effort: 'medium' }, resumes),
() => null,
NOTHING_SAVED,
OMP
).resolve({ identity })
const fresh = await start(false)
@@ -292,7 +174,7 @@ describe('ACP launch folder of a floating chat', () => {
const pinLaunchDirectory = vi.fn()
const resolve = createAcpStructuredLaunchResolver(GROK, {
store: { getRecord: () => record, pinLaunchDirectory },
readJournal: () => null,
savedSession: NOTHING_SAVED,
resolveWorkspacePath,
resolveEnvironment: async () => ({ PATH: '/usr/bin', HOME: '/home/user' }),
resolveCommand: (command) => `/resolved/${command}`
@@ -31,7 +31,6 @@ import {
import type { AcpLaunchSpec } from './acp-launch-specs'
import { acpLaunchSpecForAccount, resolveAcpLaunchCommand } from './acp-launch-command'
import { nothingExchangedOn, type AcpSavedSessionReads } from './acp-structured-resume-history'
import { acpUnannouncedLosses } from './acp-session-reopen-failure'
import { loadAcpVisualsSkill, type AcpLaunchVisualsDeps } from './acp-structured-launch-visuals'
export type AcpStructuredLaunch = StructuredAgentCommand & {
@@ -50,8 +49,8 @@ export type AcpStructuredLaunch = StructuredAgentCommand & {
key: string
/** A chat-created session with no exchanged turn may never have been saved. */
mayBeUnsaved: () => Promise<boolean>
/** Lost conversations whose warning rows are missing; read after startup succeeds. */
unannouncedLosses: () => Promise<string[]>
/** The writer decides which lost conversations still need warning rows. */
lossKeys: readonly string[]
} | null
}
@@ -205,12 +204,9 @@ export function acpStructuredLaunchPart(
identity.sessionId,
head.handle.nativeId
)),
unannouncedLosses: () =>
acpUnannouncedLosses(
deps.savedSession,
identity.sessionId,
record.providerHandleChain
)
lossKeys: record.providerHandleChain.flatMap((link) =>
link.replaces?.reason === 'restore-failed' ? [link.replaces.key] : []
)
}
: null
},
@@ -1,3 +1,4 @@
import { readTestHostJournal } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { afterEach, expect, it } from 'vitest'
import { openAttachedHostRig, send, hello, framesOf } from './acp-structured-host.test-support'
import { PROVIDER_SESSION, waitFor } from './acp-structured-adapter.test-support'
@@ -35,7 +36,7 @@ it('queues mail behind an answerable ACP question without interrupting it', asyn
orchestration: { message: 'mail-notice', mailbox: 'run:r1', dispatchId: null, messages: [] }
}
}
const before = await host.journalSnapshot(SESSION)
const before = await readTestHostJournal(host, SESSION)
const questions = before.items.filter((row) => row.body.kind === 'question')
expect(questions).toHaveLength(1)
const operationId = mintAgentSessionOperationId(Date.now())
@@ -50,7 +51,7 @@ it('queues mail behind an answerable ACP question without interrupting it', asyn
stillOwed: () => true
})
).toEqual({ kind: 'queued' })
const after = await host.journalSnapshot(SESSION)
const after = await readTestHostJournal(host, SESSION)
expect(before.submissions).toHaveLength(1)
expect(after.submissions).toEqual(before.submissions)
expect(after.items.filter((row) => row.body.kind === 'question')).toEqual(questions)
@@ -45,7 +45,7 @@ it('prefers the pin even when older, else a complete installed or external copy'
}),
pinLaunchDirectory: vi.fn()
},
readJournal: () => null,
savedSession: { hasProviderTurn: async () => false },
resolveEnvironment: async () => ({ PATH: '', HOME: homePath }),
dataDirectory
})
@@ -3,7 +3,7 @@ import { describe, expect, it } from 'vitest'
import { isPersistedAgentSessionRecord } from '../../shared/agent-session/lifecycle/agent-session-record'
import { agentSessionRecordFixture } from '../../shared/agent-session/lifecycle/agent-session-record.test-fixture'
import { replaceAgentSessionRecordOptions } from '../runtime/agent-session/records/agent-session-record-options'
import { agentSessionStoreDraftRowWrites } from '../runtime/agent-session/records/agent-session-store-draft'
import { agentSessionStoreRowChanges } from '../native-chat/agent-session-journal/journal-writer-store-row-changes'
import type { AgentSessionStoreState } from '../runtime/agent-session/records/agent-session-store-state'
import { AcpStructuredOptions } from './acp-structured-options'
import {
@@ -51,7 +51,7 @@ function writtenOptions(provider: string, reader: AcpStructuredOptions) {
options,
now: 3_000
})
const writes = agentSessionStoreDraftRowWrites(published, {
const writes = agentSessionStoreRowChanges(published, {
...published,
records: new Map([[record.sessionId, changed]])
})
@@ -1,3 +1,4 @@
import { readTestHostJournal } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { afterEach, expect, it } from 'vitest'
import { closeProviderTimelineRigs } from '../native-chat/agent-session-timeline/provider-timeline-assembler-test-support'
import {
@@ -145,7 +146,7 @@ it.each(refusals)(
for (const text of attempts) {
const messageId = await send(host, text)
await waitFor(async () => {
const snapshot = await host.journalSnapshot(SESSION)
const snapshot = await readTestHostJournal(host, SESSION)
expect(
snapshot.submissions.find((entry) => entry.clientMessageId === messageId)
).toMatchObject({
@@ -165,7 +166,7 @@ it.each(refusals)(
)
expect(commands()).toHaveLength(framesOf(rig.child(), 'session/prompt').length)
}
const failed = await host.journalSnapshot(SESSION)
const failed = await readTestHostJournal(host, SESSION)
expect(
failed.submissions.filter((entry) => entry.rejection?.kind === failure.kind)
).toHaveLength(attempts.length)
@@ -1,3 +1,4 @@
import { readTestJournalItems } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { afterEach, describe, expect, it } from 'vitest'
import type { AgentJournalMessageItem } from '../../shared/agent-session/journal/agent-session-journal-types'
import { readAgentJournalTurn } from '../../shared/agent-session/lifecycle/agent-session-turn-record'
@@ -120,9 +121,9 @@ describe('OMP recordings through the adapter', () => {
await rig.adapter.closeSession(SESSION)
const savedMessages = (await rig.rig.rows()).filter((row) => row.body.kind === 'message')
const reopened = await rig.rig.reopenJournal()
expect(reopened.snapshot().items.filter((row) => row.body.kind === 'message')).toEqual(
savedMessages
)
expect(
(await readTestJournalItems(reopened)).filter((row) => row.body.kind === 'message')
).toEqual(savedMessages)
}
)
@@ -1,3 +1,4 @@
import { readTestHostJournal } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { afterEach, expect, it } from 'vitest'
import { closeProviderTimelineRigs } from '../native-chat/agent-session-timeline/provider-timeline-assembler-test-support'
import { CALLER } from '../native-chat/agent-session-wire/host/testing/structured-agent-session-host.test-fixture'
@@ -125,7 +126,7 @@ async function openModel(
const id = await send(f.host, text)
await waitFor(async () => {
expect(
(await f.host.journalSnapshot(SESSION)).submissions.find(
(await readTestHostJournal(f.host, SESSION)).submissions.find(
(entry) => entry.clientMessageId === id
)
).toMatchObject({ dispatchState: 'accepted' })
@@ -134,7 +135,9 @@ async function openModel(
messages,
JSON.stringify({ messages, controls: framesOf(f.rig.child(), 'session/set_config_option') })
).toContain(expected)
expect(f.host.collaboratorsForTests().sessions.get(SESSION)!.journal.runningTurn()).toBeNull()
expect(
await f.host.collaboratorsForTests().sessions.get(SESSION)!.journal.reads.runningTurn()
).toBeNull()
})
return id
}
@@ -159,7 +162,7 @@ it.each(['@role', '@role:high', 'retired'])(
const id = await send(f.host, 'literal choice')
await waitFor(async () =>
expect(
(await f.host.journalSnapshot(SESSION)).submissions.find(
(await readTestHostJournal(f.host, SESSION)).submissions.find(
(entry) => entry.clientMessageId === id
)
).toMatchObject({ dispatchState: 'rejected' })
@@ -261,7 +264,7 @@ it('does not send when an offered effort control is genuinely rejected', async (
const id = await send(f.host, 'use the offered effort')
await waitFor(async () =>
expect(
(await f.host.journalSnapshot(SESSION)).submissions.find(
(await readTestHostJournal(f.host, SESSION)).submissions.find(
(entry) => entry.clientMessageId === id
)
).toMatchObject({ dispatchState: 'rejected' })
@@ -2,8 +2,6 @@
export type AcpSavedSessionReads = {
/** Whether any saved turn belongs to the provider session with this native id. */
hasProviderTurn(sessionId: string, nativeId: string): Promise<boolean>
/** The subset of these item IDs the chat has saved. */
savedItemIds(sessionId: string, itemIds: readonly string[]): Promise<ReadonlySet<string>>
}
/** Unreadable journals cannot prove no exchange. */
@@ -36,8 +36,8 @@ const resume = {
resume: {
sessionId: PROVIDER_SESSION,
key: 'acp:grok:saved',
mayBeUnsaved: () => false,
unannouncedLosses: () => []
mayBeUnsaved: async () => false,
lossKeys: []
}
}
}
@@ -247,8 +247,8 @@ describe('reattaching a Grok chat the journal holds', () => {
resume: {
sessionId: 'saved-1',
key: 'acp-key-saved-1',
mayBeUnsaved: () => mayBeUnsaved,
unannouncedLosses: () => []
mayBeUnsaved: async () => mayBeUnsaved,
lossKeys: []
}
},
initialize: RESUMES,
@@ -279,8 +279,8 @@ describe('reattaching a Grok chat the journal holds', () => {
resume: {
sessionId: 'saved-1',
key: 'acp-key-saved-1',
mayBeUnsaved: () => false,
unannouncedLosses: () => []
mayBeUnsaved: async () => false,
lossKeys: []
}
},
initialize: RESUMES,
+32 -17
View File
@@ -1,3 +1,4 @@
import { readTestHostJournal } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { readAgentJournalTurn } from '../../shared/agent-session/lifecycle/agent-session-turn-record'
import { DISPATCH_REJECTED_CANCELLED } from '../../shared/agent-session/requests/structured-agent-session-dispatch-rejection'
@@ -180,12 +181,14 @@ describe('permission changes with already admitted ACP input', () => {
expect(framesOf(original, 'session/cancel')).toHaveLength(1)
expect(framesOf(original, 'session/prompt')).toHaveLength(0)
const journal = host.collaboratorsForTests().sessions.get(SESSION)!.journal
const handoffId = journal.queuedMessages.get(id)?.consumedAs ?? id
const handoffId = (await journal.queuedMessages.get(id))?.consumedAs ?? id
const prompt = await rig.frame('session/prompt')
expect(promptIdOf(prompt)).toBe(`prompt:${handoffId}`)
rig.child().agent.notify('session/update', replyChunk(promptIdOf(prompt), 'done'))
rig.child().agent.reply(prompt, { stopReason: 'end_turn' })
await waitFor(() => expect(journal.submission(handoffId)?.dispatchState).toBe('accepted'))
await waitFor(async () =>
expect((await journal.reads.submission(handoffId))?.dispatchState).toBe('accepted')
)
expect(framesOf(rig.child(), 'session/prompt')).toHaveLength(1)
await host.close(SESSION, 'user-close')
})
@@ -212,39 +215,51 @@ describe('permission changes with already admitted ACP input', () => {
).toMatchObject({ ok: true })
const c = await send(host, 'C new policy')
const journal = host.collaboratorsForTests().sessions.get(SESSION)!.journal
const handoffId = (id: string) => journal.queuedMessages.get(id)?.consumedAs ?? id
expect(journal.queuedMessages.get(c)).toMatchObject({ state: 'waiting', consumedAs: null })
expect(journal.submission(handoffId(b))?.dispatchState).toBe('pending')
const handoffId = async (id: string) =>
(await journal.queuedMessages.get(id))?.consumedAs ??
(await journal.reads.input({ kind: 'linked-to', messageId: id })).submissions[0]
?.clientMessageId ??
id
const dispatchOf = async (id: string) =>
(await journal.reads.submission(await handoffId(id)))?.dispatchState
expect(await journal.queuedMessages.get(c)).toMatchObject({
state: 'waiting',
consumedAs: null
})
expect(await dispatchOf(b)).toBe('pending')
expect(framesOf(original, 'session/prompt')).toHaveLength(1)
original.agent.reply(promptA, { stopReason: 'cancelled' })
const promptB = await rig.frame('session/prompt', 1)
expect(promptIdOf(promptB)).toBe(`prompt:${handoffId(b)}`)
expect(promptIdOf(promptB)).toBe(`prompt:${await handoffId(b)}`)
original.agent.notify('session/update', replyChunk(promptIdOf(promptB), 'answered B'))
await waitFor(() => expect(journal.submission(handoffId(b))?.dispatchState).toBe('accepted'))
await waitFor(async () => expect(await dispatchOf(b)).toBe('accepted'))
if (outcome === 'deadline') {
await waitFor(() =>
expect(journal.submission(handoffId(c))?.dispatchState).toBe('rejected')
)
expect(journal.submission(handoffId(c))?.rejection).toMatchObject({
await waitFor(async () => expect(await dispatchOf(c)).toBe('rejected'))
expect((await journal.reads.submission(await handoffId(c)))?.rejection).toMatchObject({
kind: 'restartFailed',
neverReachedAgent: true
})
expect(original.exited).toBe(false)
}
original.agent.reply(promptB, { stopReason: 'end_turn' })
expect(journal.submission(a)?.dispatchState).toBe('accepted')
expect((await journal.reads.submission(a))?.dispatchState).toBe('accepted')
expect(framesOf(original, 'session/prompt')).toHaveLength(2)
if (outcome === 'drained') {
await waitFor(() => expect(original.exited).toBe(true))
await waitFor(() => expect(rig.child()).not.toBe(original))
const promptC = await rig.frame('session/prompt')
expect(promptIdOf(promptC)).toBe(`prompt:${handoffId(c)}`)
// The restarted agent receives C as its own delivery of the queued card.
const deliveredC = promptIdOf(promptC).replace(/^prompt:/, '')
expect(await journal.reads.submissionBody(deliveredC)).toMatchObject({
blocks: [{ type: 'text', text: 'C new policy' }]
})
rig.child().agent.notify('session/update', replyChunk(promptIdOf(promptC), 'answered C'))
rig.child().agent.reply(promptC, { stopReason: 'end_turn' })
await waitFor(() =>
expect(journal.submission(handoffId(c))?.dispatchState).toBe('accepted')
await waitFor(async () =>
expect((await journal.reads.submission(deliveredC))?.dispatchState).toBe('accepted')
)
} else {
expect(journal.submission(handoffId(c))?.dispatchState).toBe('rejected')
expect(await dispatchOf(c)).toBe('rejected')
}
await host.close(SESSION, 'user-close')
}
@@ -496,7 +511,7 @@ describe('Steer on a Grok card, through the host', () => {
return history.ok ? history.page : null
}
const sends = async () =>
(await host.journalSnapshot(SESSION)).submissions
(await readTestHostJournal(host, SESSION)).submissions
.filter((entry) => entry.queuedMessageId === cardId)
.map((entry) => ({ state: entry.dispatchState, reason: entry.reason }))
return { cardId, sendNow, page, sends }
@@ -1,23 +1,18 @@
import { memoryJournalMap } from '../native-chat/agent-session-journal/journal-memory-collections'
import { afterEach, describe, expect, it } from 'vitest'
import { readAgentJournalTurn } from '../../shared/agent-session/lifecycle/agent-session-turn-record'
import { restoreJournal } from '../native-chat/agent-session-journal/replay/journal-open'
import { JournalWriteProjection } from '../native-chat/agent-session-journal/writing/journal-write-projection'
import { readTestHostJournal } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { closeProviderTimelineRigs } from '../native-chat/agent-session-timeline/provider-timeline-assembler-test-support'
import { HOST_TEST_SESSION as SESSION } from '../native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture'
import { PROVIDER_SESSION, replyChunk, waitFor } from './acp-structured-adapter.test-support'
import { openAttachedHostRig, promptIdOf, send } from './acp-structured-host.test-support'
afterEach(async () => {
JournalWriteProjection.verifyEachCommit = false
await closeProviderTimelineRigs()
})
describe('an ACP usage report with an unknown window', () => {
// The protocol allows `size: 0`; the saved reader drops that window as unusable.
it('keeps the turn running, and the live chat equals what a reload reads', async () => {
// Each commit then restores the chat from disk and fails the write if it differs from live.
JournalWriteProjection.verifyEachCommit = true
const rig = await openAttachedHostRig()
const { host } = rig
await send(host, 'hello')
@@ -38,10 +33,7 @@ describe('an ACP usage report with an unknown window', () => {
expect((await rig.rows()).filter((row) => row.body.kind === 'status')).toEqual([])
expect((await rig.turns()).at(-1)?.contextUsage).toBeUndefined()
const loaded = restoreJournal(rig.journalDatabase.db, SESSION)
expect(loaded?.damage).toBeFalsy()
expect(loaded?.newer).toBeFalsy()
const saved = [...(loaded ? memoryJournalMap(loaded.state.items).values() : [])].flatMap(
const saved = (await readTestHostJournal(host, SESSION)).items.flatMap(
(item) => readAgentJournalTurn(item.body) ?? []
)
expect(saved).toEqual(await rig.turns())
@@ -1,3 +1,7 @@
import {
readTestJournalItems,
readTestJournalSubmissions
} from '../../native-chat/agent-session-journal/journal-observation.test-support'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { agentSessionFailureFact } from '../../../shared/agent-session/failures/agent-session-failure'
import { agentSessionFailureWords } from '../../../shared/agent-session/failures/agent-session-failure-words'
@@ -74,11 +78,10 @@ describe('ACP recovered history boundaries', () => {
if (
steps.some(
(step) =>
step.kind === 'command' &&
step.step.kind === 'resolved-item' &&
'body' in step.step.resolution &&
step.step.resolution.body.kind === 'message' &&
step.step.resolution.body.role === 'assistant'
step.kind === 'resolved-item' &&
'body' in step.resolution &&
step.resolution.body.kind === 'message' &&
step.resolution.body.role === 'assistant'
)
) {
return Promise.reject(failure)
@@ -106,7 +109,7 @@ describe('ACP recovered history boundaries', () => {
exit: { ownerFence: 1, observedAt: 1200 }
})
const saved = await fixture.rig.rows()
const dispatch = structuredClone(fixture.rig.journal.submissions())
const dispatch = structuredClone(await readTestJournalSubmissions(fixture.rig.journal))
const failed = saved.find(
(row) => row.body.kind === 'status' && row.body.failure?.kind === 'hostFault'
)!
@@ -153,9 +156,9 @@ describe('ACP recovered history boundaries', () => {
dialect: OPENCODE_ACP_DIALECT,
assertCurrent: () => {}
})
const rows = journal.snapshot().items
const rows = await readTestJournalItems(journal)
expect(rows.slice(0, saved.length)).toEqual(saved)
expect(journal.submissions()).toEqual(dispatch)
expect(await readTestJournalSubmissions(journal)).toEqual(dispatch)
expect(
rows.some(
(row) =>
@@ -185,14 +188,12 @@ describe('ACP recovered history boundaries', () => {
expect(await deferred.drained()).toEqual({ ok: true })
expect((await fixture.rig.turns()).at(-1)?.state).toBe('completed')
expect(
journal
.snapshot()
.items.some(
(row) =>
row.body.kind === 'message' &&
row.body.role === 'assistant' &&
row.body.blocks.some((block) => block.type === 'text' && block.text === 'NEXT')
)
(await readTestJournalItems(journal)).some(
(row) =>
row.body.kind === 'message' &&
row.body.role === 'assistant' &&
row.body.blocks.some((block) => block.type === 'text' && block.text === 'NEXT')
)
).toBe(true)
}
} finally {
@@ -387,7 +388,7 @@ describe('ACP recovered history boundaries', () => {
}
}
plans.length = 0
const saved = new Map(journal.snapshot().items.map((row) => [row.itemId, row]))
const saved = new Map((await readTestJournalItems(journal)).map((row) => [row.itemId, row]))
try {
await catchUpAcpHistory({
frames: replay,
@@ -401,7 +402,7 @@ describe('ACP recovered history boundaries', () => {
logger: { warn, error: vi.fn() }
})
expect(await deferred.drained()).toEqual({ ok: true })
const rows = journal.snapshot().items
const rows = await readTestJournalItems(journal)
expect(rows.slice(0, native.length)).toEqual(native)
expect(dividers(rows)).toHaveLength(0)
expect(new Set(resolved).size).toBe(resolved.length)
@@ -486,21 +487,19 @@ describe('ACP recovered history boundaries', () => {
dialect: OPENCODE_ACP_DIALECT,
assertCurrent: () => {}
})
expect(dividers(journal.snapshot().items)).toHaveLength(0)
expect(dividers(await readTestJournalItems(journal))).toHaveLength(0)
expect(
journal
.snapshot()
.items.filter(
(row) =>
row.body.kind === 'message' &&
row.body.role === 'assistant' &&
row.body.blocks.some(
(block) => block.type === 'text' && block.text === 'prefix and tail'
)
)
(await readTestJournalItems(journal)).filter(
(row) =>
row.body.kind === 'message' &&
row.body.role === 'assistant' &&
row.body.blocks.some(
(block) => block.type === 'text' && block.text === 'prefix and tail'
)
)
).toHaveLength(1)
}
first = structuredClone(journal.snapshot().items)
first = structuredClone(await readTestJournalItems(journal))
} else {
expect(plans).toEqual([])
expect(resolved).toEqual([])
@@ -567,7 +566,7 @@ describe('ACP recovered history boundaries', () => {
})
expect(await deferred.drained()).toEqual({ ok: true })
expect(appendReopened).not.toHaveBeenCalled()
expect(journal.snapshot().items).toEqual(settled)
expect(await readTestJournalItems(journal)).toEqual(settled)
} finally {
deferred.close()
await journal.close()
@@ -1,3 +1,7 @@
import {
readTestJournalItems,
readTestJournalSubmissions
} from '../../native-chat/agent-session-journal/journal-observation.test-support'
// A reopened chat catches up on turns its agent replays that the chat never saw, against real
// OpenCode 2.0.14 and Grok 1.0.62 recordings: once, in order, without a notice, and never a turn
// the chat already holds.
@@ -19,7 +23,6 @@ import {
AGENT,
closeProviderTimelineRigs,
NAMESPACE,
SESSION,
providerItemId
} from '../../native-chat/agent-session-timeline/provider-timeline-assembler-test-support'
import { GROK_ACP_DIALECT } from '../acp-dialects/grok-dialect'
@@ -30,9 +33,6 @@ import {
} from '../../../shared/agent-session/lifecycle/agent-session-turn-record'
import { acpNamedTextKey } from '../acp-turn-messages'
import { isHistoricalProviderTimelineItem } from '../../native-chat/agent-session-timeline/provider-timeline-history'
import { StructuredAgentSessionCurrentWork } from '../../native-chat/agent-session-wire/observations/structured-agent-session-current-work'
import { hasUnfinishedStructuredAgentSessionWork } from '../../native-chat/agent-session-wire/termination/structured-agent-session-unfinished-work'
import { planStructuredAgentSessionDeadGeneration } from '../../native-chat/agent-session-wire/termination/structured-agent-session-dead-generation-settlement'
import { AcpLoadHistory } from './acp-history-replay'
import { asReattachHistory } from '../acp-structured-session'
import { readAcpFixture, type AcpFixtureFrame } from '../acp-timeline-fixture.test-support'
@@ -42,6 +42,11 @@ afterEach(async () => {
await closeProviderTimelineRigs()
})
function savedItem(items: readonly AgentJournalRenderItem[]) {
const byId = new Map(items.map((item) => [item.itemId, item]))
return (itemId: string) => byId.get(itemId) ?? null
}
async function recording(name: string): Promise<AcpFixtureFrame[]> {
// The rig's journal keys rows under its own provider session.
return JSON.parse(
@@ -181,7 +186,7 @@ describe('ACP history catch-up', () => {
await catchUp(partial)
const imported = await rig.rows()
const historical = imported.filter((item) =>
isHistoricalProviderTimelineItem(item, (key) => rig.journal.item(key))
isHistoricalProviderTimelineItem(item, savedItem(imported))
)
expect(historical).toHaveLength(4)
const container = historical.map((item) => readAgentJournalTurn(item.body)).find(Boolean)
@@ -191,20 +196,6 @@ describe('ACP history catch-up', () => {
expect(container).not.toHaveProperty('startedAt')
const observedTool = historical.find((item) => item.body.kind === 'tool-call')!
expect(observedTool.body).toMatchObject({ state: 'running' })
expect(
new StructuredAgentSessionCurrentWork(rig.journal, 1).isCurrentItem(observedTool.itemId)
).toBe(false)
expect(hasUnfinishedStructuredAgentSessionWork(rig.journal)).toBe(false)
expect(
planStructuredAgentSessionDeadGeneration({
journal: rig.journal,
sessionId: SESSION,
fence: 1,
settlementId: 'exit',
verdict: { state: 'interrupted' },
pendingSubmissionReason: 'ended'
}).mutations
).toEqual([])
rig.assembler.apply({ type: 'session.ended', verdict: { state: 'interrupted' }, endedAt: 9000 })
await fixture.restart()
expect((await rig.row(observedTool.itemId))?.body).toEqual(observedTool.body)
@@ -240,14 +231,9 @@ describe('ACP history catch-up', () => {
await catchUp(full)
expect(await rig.rows()).toEqual(complete)
const reopened = await rig.reopenJournal()
expect(
isHistoricalProviderTimelineItem(reopened.item(observedTool.itemId)!, (key) =>
reopened.item(key)
)
).toBe(true)
expect(isHistoricalProviderTimelineItem(nativeBefore[0]!, (key) => reopened.item(key))).toBe(
false
)
const reread = savedItem(await readTestJournalItems(reopened))
expect(isHistoricalProviderTimelineItem(reread(observedTool.itemId)!, reread)).toBe(true)
expect(isHistoricalProviderTimelineItem(nativeBefore[0]!, reread)).toBe(false)
})
it('keeps same-text outside answers and reasoning while deduplicating exact native output', async () => {
@@ -343,7 +329,7 @@ describe('ACP history catch-up', () => {
state: 'pending',
turnScope: { kind: 'thread' }
})
const submissions = journal.submissions()
const submissions = await readTestJournalSubmissions(journal)
const replay = ['handed', 'queued'].flatMap((id) => [
userReplay(id),
{
@@ -356,11 +342,11 @@ describe('ACP history catch-up', () => {
}
])
await catchUp(replay)
expect(journal.submissions()).toEqual(submissions)
expect(await readTestJournalSubmissions(journal)).toEqual(submissions)
expect(userTexts(await fixture.rig.rows())).toEqual(['repeat', 'repeat', 'repeat'])
expect(journal.receiptFor('queued')).toBeNull()
expect(journal.submission('queued')?.dispatchState).toBe('pending')
expect(journal.submission('queued')?.handedOverAt).toBeUndefined()
expect(await journal.reads.receiptFor('queued')).toBeNull()
expect((await journal.reads.submission('queued'))?.dispatchState).toBe('pending')
expect((await journal.reads.submission('queued'))?.handedOverAt).toBeUndefined()
})
it.each(['interleaved', 'adjacent', 'hidden-root', 'hidden-later'] as const)(
'keeps Grok root and later messages in one execution across restart: %s',
@@ -438,7 +424,7 @@ describe('ACP history catch-up', () => {
await catchUp([...rootBlocks, output('A')])
}
const before = await rig.rows()
const submissions = rig.journal.submissions()
const submissions = await readTestJournalSubmissions(rig.journal)
await fixture.restart()
const replay = [
...rootBlocks,
@@ -507,11 +493,9 @@ describe('ACP history catch-up', () => {
)
expect(readAgentJournalTurn(lifecycle[0]!.body)?.userItemId).toBe(opener.itemId)
expect(opener.turnScope).toEqual({ kind: 'turn', turnItemId: lifecycle[0]!.itemId })
expect(
messages.every((row) =>
isHistoricalProviderTimelineItem(row, (key) => rig.journal.item(key))
)
).toBe(true)
expect(messages.every((row) => isHistoricalProviderTimelineItem(row, savedItem(rows)))).toBe(
true
)
for (const held of before) {
const current = rows.find((row) => row.itemId === held.itemId)!
expect(current.sequence).toBe(held.sequence)
@@ -524,7 +508,7 @@ describe('ACP history catch-up', () => {
}
}
expect(dividers(rows)).toHaveLength(0)
expect(rig.journal.submissions()).toEqual(submissions)
expect(await readTestJournalSubmissions(rig.journal)).toEqual(submissions)
await catchUp(replay)
expect(await rig.rows()).toEqual(rows)
}
@@ -644,7 +628,7 @@ describe('ACP history catch-up', () => {
blocks: [{ text: 'prefix' }]
})
if (scenario === 'queued-only') {
expect(rig.journal.submission('queued')?.handedOverAt).toBeUndefined()
expect((await rig.journal.reads.submission('queued'))?.handedOverAt).toBeUndefined()
await catchUp([userReplay('outside'), answerReplay('full')])
const caughtUp = (await rig.rows()).filter(
(item) => item.body.kind === 'message' && item.body.role === 'assistant'
@@ -1,8 +1,15 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { Repo } from '../../shared/repo-types'
import type * as CommitMessageTextGenerationModule from '../text-generation/commit-message-text-generation'
import type { AgentJournalRenderItem } from '../../shared/agent-session/journal/agent-session-journal-types'
import type { AgentSessionJournal } from '../native-chat/agent-session-journal/journal-store'
import type { AgentJournalItemBody } from '../../shared/agent-session/journal/agent-session-journal-types'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { createTrackedJournalOpener } from '../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import {
claudeProviderHandle,
codexProviderHandle
} from '../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
import { StructuredAgentSessionStatusFeed } from '../native-chat/agent-session-wire/observations/structured-agent-session-status-feed'
import { maybeAutoRenameWorkspaceOnFirstStructuredTurn } from './first-work-structured-session-rename'
import '../../shared/worktree/id'
@@ -78,6 +85,40 @@ function elapseGenerationBackoff(): void {
clockMs += Math.max(...FIRST_WORK_GENERATION_RETRY_DELAYS_MS)
}
const journals = createTrackedJournalOpener()
const roots: string[] = []
afterEach(async () => {
await journals.closeAll()
await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })))
})
async function renameJournal(agent: 'claude' | 'codex', workspaceId: string) {
const root = await mkdtemp(join(tmpdir(), 'orca-first-work-sql-'))
roots.push(root)
return journals.open({
stateDirectory: root,
identity: {
sessionId: 'session',
workspaceId,
hostId: 'local',
agent,
providerHandle:
agent === 'claude' ? claudeProviderHandle('native', null) : codexProviderHandle('native')
}
})
}
const USER = {
kind: 'message',
role: 'user',
blocks: [{ type: 'text', text: 'Fix auth' }]
} satisfies AgentJournalItemBody
const TURN = {
kind: 'status',
text: 'Working',
turnLifecycle: { turnId: 'turn-1', state: 'running' }
} satisfies AgentJournalItemBody
describe('maybeAutoRenameBranchOnFirstWork', () => {
afterEach(() => {
vi.restoreAllMocks()
@@ -113,29 +154,18 @@ describe('maybeAutoRenameBranchOnFirstWork', () => {
getFolderWorkspacePath: () => '/workspace/platform',
isPendingFirstAgentMessageRename: () => true
})
const items: AgentJournalRenderItem[] = []
// Advance journal sequence so projection caching cannot hide a rename event.
let sequence = 0
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the status feed uses these held rows and provenance reads; the item lookup is checked against the real journal contract.
const journal = {
snapshot: () => ({ items, submissions: [] }),
statusRead: () => ({ items, submissions: [] }),
inputOrder: {
inputCount: () => 0,
inputOrdinal: () => Infinity,
recoverableSubmissions: () => []
},
context: { floor: () => null },
item: ((itemId) =>
items.find((item) => item.itemId === itemId) ??
null) satisfies AgentSessionJournal['item'],
stopMarks: { latest: () => null, revision: () => 0 },
lastActivityAt: () => 1,
cursor: () => ({ epoch: 1, sequence: (sequence += 1) }),
itemFence: () => undefined,
wroteBeforeOpen: () => false,
runningTurn: () => null
} as unknown as AgentSessionJournal
const journal = await renameJournal(agent, workspaceId)
const userIdentity =
agent === 'claude'
? { provider: 'claude' as const, sessionId: 'native', uuid: 'user' }
: { provider: 'codex' as const, threadId: 'native', turnId: 'turn-1', ordinal: 0 }
const turnIdentity =
agent === 'claude'
? { provider: 'claude' as const, sessionId: 'native', uuid: 'turn' }
: { provider: 'codex' as const, threadId: 'native', turnId: 'turn-1', ordinal: 1 }
const appendTurn = () =>
journal.appendItem(turnIdentity, TURN, { fence: 0, turnScope: { kind: 'thread' } })
const removeTurn = () => journal.appendTombstone(turnIdentity, { fence: 0 })
const pending: Promise<void>[] = []
const observe = vi.fn((summary, options) => {
const work = maybeAutoRenameWorkspaceOnFirstStructuredTurn(summary, options, deps)
@@ -166,40 +196,28 @@ describe('maybeAutoRenameBranchOnFirstWork', () => {
now: () => 1,
onStatusChanged: observe
})
const user = {
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'Fix auth' }] }
} as AgentJournalRenderItem
const turn = {
body: {
kind: 'status',
text: 'Working',
turnLifecycle: { turnId: 'turn-1', state: 'running' }
}
} as AgentJournalRenderItem
items.push(user, turn)
feed.publish('session', journal, { replay: true })
await journal.appendItem(userIdentity, USER, { fence: 0, turnScope: { kind: 'thread' } })
await appendTurn()
await feed.publish('session', journal, { replay: true })
await Promise.all(pending)
expect(gitExecFileAsyncMock).not.toHaveBeenCalled()
items.pop()
feed.publish('session', journal)
items.push(turn)
await removeTurn()
await feed.publish('session', journal)
await appendTurn()
generateBranchNameMock.mockResolvedValueOnce({ success: false, error: 'temporary failure' })
feed.publish('session', journal)
await feed.publish('session', journal)
await Promise.all(pending)
expect(generateBranchNameMock).toHaveBeenCalledOnce()
expect(setDisplayName).not.toHaveBeenCalled()
const callsBeforeOutput = observe.mock.calls.length
for (let index = 0; index < 100; index++) {
feed.publish('session', journal)
}
expect(observe).toHaveBeenCalledTimes(callsBeforeOutput)
await feed.publish('session', journal)
expect(generateBranchNameMock).toHaveBeenCalledOnce()
elapseGenerationBackoff()
items.pop()
feed.publish('session', journal)
items.push(turn)
feed.publish('session', journal)
await removeTurn()
await feed.publish('session', journal)
await appendTurn()
await feed.publish('session', journal)
await Promise.all(pending)
expect(generateBranchNameMock).toHaveBeenCalledTimes(2)
expect(setDisplayName).toHaveBeenCalledWith(workspaceId, 'Fix auth')
@@ -219,45 +237,14 @@ describe('maybeAutoRenameBranchOnFirstWork', () => {
const { deps, setDisplayName, setRenameError } = makeDeps({
getRepo: () => ({ id: REPO_ID, kind: 'folder', path: '/workspace/platform' }) as Repo
})
const items: AgentJournalRenderItem[] = [
{
itemId: 'user-1',
sequence: 1,
revision: 1,
observedAt: 1,
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'Fix auth' }] }
},
{
itemId: 'turn-1',
sequence: 2,
revision: 1,
observedAt: 1,
body: {
kind: 'status',
text: 'Working',
turnLifecycle: { turnId: 'turn-1', state: 'running' }
}
}
]
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the status feed uses these held rows and provenance reads; the item lookup is checked against the real journal contract.
const journal = {
lastActivityAt: () => 1,
context: { floor: () => null },
stopMarks: { latest: () => null, revision: () => 0 },
cursor: () => ({ epoch: 1, sequence: 1 }),
snapshot: () => ({ items, submissions: [] }),
statusRead: () => ({ items, submissions: [] }),
inputOrder: {
inputCount: () => 0,
inputOrdinal: () => Infinity,
recoverableSubmissions: () => []
},
item: ((itemId) =>
items.find((item) => item.itemId === itemId) ?? null) satisfies AgentSessionJournal['item'],
itemFence: () => undefined,
wroteBeforeOpen: () => false,
runningTurn: () => null
} as unknown as AgentSessionJournal
const journal = await renameJournal('codex', workspaceId)
for (const [ordinal, body] of [USER, TURN].entries()) {
await journal.appendItem(
{ provider: 'codex', threadId: 'native', turnId: 'turn-1', ordinal },
body,
{ fence: 0, turnScope: { kind: 'thread' } }
)
}
const location = {
executionHostId: 'local' as const,
wslDistro: null,
@@ -279,7 +266,7 @@ describe('maybeAutoRenameBranchOnFirstWork', () => {
}
})
feed.publish('session', journal)
await feed.publish('session', journal)
await Promise.all(pending)
expect(gitExecFileAsyncMock).not.toHaveBeenCalled()
@@ -1,268 +0,0 @@
import { memoryJournalMap } from '../native-chat/agent-session-journal/journal-memory-collections'
import { vi, type Mock, type MockInstance } from 'vitest'
import type { AgentJournalSnapshot } from '../../shared/agent-session/journal/agent-session-journal-types'
import type { AgentSessionJournal } from '../native-chat/agent-session-journal/journal-store'
import type { StructuredAgentSessionHost } from '../native-chat/agent-session-wire/host/structured-agent-session-host'
export type AutomationChatRunMeasurement = {
measure: () => number[]
full: MockInstance<AgentSessionJournal['snapshot']>
status: MockInstance<AgentSessionJournal['statusRead']>
observerReads: MockInstance<StructuredAgentSessionHost['journalCurrentRead']>
registration: MockInstance<StructuredAgentSessionHost['observeConversationPublications']>
releases: Mock<() => void>[]
keys: [
MockInstance<AgentSessionJournal['item']>,
MockInstance<AgentSessionJournal['submission']>,
MockInstance<AgentSessionJournal['queuedMessages']['get']>,
MockInstance<AgentSessionJournal['canonicalItemId']>
]
timers: MockInstance<(callback: (_: void) => void, delay?: number) => NodeJS.Timeout>
cleared: MockInstance<(timeout: NodeJS.Timeout | string | number | undefined) => void>
details: () => {
callbacks: number
committedRows: number
deltaReads: number
deltaRows: number
copied: number
iteratorYields: number
outputIterations: number
projections: number
projectedRows: number
scannedRows: number
}
}
export function measureAutomationChatRun(
journal: AgentSessionJournal,
host: StructuredAgentSessionHost
): AutomationChatRunMeasurement {
let itemReads = 0,
submissionReads = 0,
itemFinds = 0,
rootVisits = 0,
submissionFinds = 0,
submissionVisits = 0,
itemIterations = 0
const snapshots = new WeakMap<AgentJournalSnapshot, AgentJournalSnapshot>()
function wrapSnapshot(original: AgentJournalSnapshot): AgentJournalSnapshot {
const prior = snapshots.get(original)
if (prior) {
return prior
}
function rows<T>(values: T[], items: boolean): T[] {
return new Proxy(values, {
get(target, key, receiver) {
if (typeof key === 'string' && /^(0|[1-9][0-9]*)$/.test(key)) {
if (items) {
itemReads++
} else {
submissionReads++
}
}
if (key === 'find') {
return (predicate: (value: T, index: number, values: T[]) => unknown) => {
if (items) {
itemFinds++
} else {
submissionFinds++
}
return Array.prototype.find.call(receiver, (value: T, index: number, array: T[]) => {
if (items) {
rootVisits++
} else {
submissionVisits++
}
return predicate(value, index, array)
})
}
}
if (items && key === Symbol.iterator) {
return function* () {
for (let i = 0; i < target.length; i++) {
itemIterations++
yield receiver[i]
}
}
}
// oxlint-disable-next-line anti-slop/no-reflect-get -- Proxy get trap: only Reflect.get forwards a raw string|symbol key with the proxy receiver.
return Reflect.get(target, key, receiver)
}
})
}
const wrapped = {
...original,
items: rows(original.items, true),
submissions: rows(original.submissions, false)
}
snapshots.set(original, wrapped)
return wrapped
}
let projections = 0,
projectedRows = 0,
scannedRows = 0
let renderingOutput = false,
outputIterations = 0
const materialized = new WeakSet<object>()
const realPublicRead = journal.publicRead.bind(journal)
const publicReads = vi.spyOn(journal, 'publicRead').mockImplementation(() => {
const view = realPublicRead()
return {
currentSnapshot(ids) {
const result = output(() => view.currentSnapshot(ids))
if (!materialized.has(result)) {
materialized.add(result)
projections++
projectedRows += result.items.length + result.submissions.length
}
return wrapSnapshot(result)
},
snapshotForItems(ids) {
return output(() => wrapSnapshot(view.snapshotForItems(ids)))
},
get normalizationKey() {
return view.normalizationKey
},
get snapshot() {
const result = output(() => view.snapshot)
if (!materialized.has(result)) {
materialized.add(result)
projections++
projectedRows += result.items.length + result.submissions.length
}
return wrapSnapshot(result)
}
}
})
function output<T>(read: () => T): T {
const previous = renderingOutput
renderingOutput = true
try {
return read()
} finally {
renderingOutput = previous
}
}
const visitItems = journal.visitItems.bind(journal)
const visits = vi.spyOn(journal, 'visitItems').mockImplementation((visit) =>
visitItems((...args) => {
scannedRows++
visit(...args)
})
)
let deltaRows = 0
const readSince = journal.readSince.bind(journal)
const deltas = vi.spyOn(journal, 'readSince').mockImplementation((...args) => {
const value = readSince(...args)
if (value.ok) {
deltaRows += value.rows.length
}
return value
})
const timers = vi.spyOn(globalThis, 'setTimeout'),
cleared = vi.spyOn(globalThis, 'clearTimeout')
const status = vi.spyOn(journal, 'statusRead')
const realSnapshot = journal.snapshot.bind(journal)
const full = vi.spyOn(journal, 'snapshot').mockImplementation((...args) => {
const value = realSnapshot(...args)
return value
})
const releases: Mock<() => void>[] = []
let callbacks = 0,
committedRows = 0,
copied = 0,
iteratorYields = 0
const submissions = journal.submissions.bind(journal)
const submissionCopies = vi.spyOn(journal, 'submissions').mockImplementation(() => {
const rows = submissions()
copied += rows.length
return rows
})
// Count actual folded history iterators, without replacing their algorithms.
const folded = journal['state']
const values = memoryJournalMap(folded.items).values.bind(memoryJournalMap(folded.items))
vi.spyOn(memoryJournalMap(folded.items), 'values').mockImplementation(function* () {
for (const item of values()) {
iteratorYields++
if (renderingOutput) {
outputIterations++
}
yield item
}
return undefined
})
const fullApis = [
'pendingSubmissions',
'runningTurn',
'newestTurn',
'threadGoalSource',
'contextUsage',
'visitItemsWithLinkage'
] as const
const calls = fullApis.map((name) => vi.spyOn(journal, name))
const keys: AutomationChatRunMeasurement['keys'] = [
vi.spyOn(journal, 'item'),
vi.spyOn(journal, 'submission'),
vi.spyOn(journal.queuedMessages, 'get'),
vi.spyOn(journal, 'canonicalItemId')
]
const observerReads = vi.spyOn(host, 'journalCurrentRead')
const realObserve = host.observeConversationPublications.bind(host)
const registration = vi
.spyOn(host, 'observeConversationPublications')
.mockImplementation((id, observer) => {
const release = realObserve(id, {
...observer,
next: (hint) => {
callbacks++
committedRows += hint.rows?.length ?? 0
observer.next(hint)
}
})
const wrapped = vi.fn(release)
releases.push(wrapped)
return wrapped
})
const measure = () => [
copied,
iteratorYields,
projections,
projectedRows,
scannedRows,
full.mock.calls.length,
...calls.map((spy) => spy.mock.calls.length),
publicReads.mock.calls.length,
submissionCopies.mock.calls.length,
visits.mock.calls.length,
itemReads,
submissionReads,
itemFinds,
rootVisits,
submissionFinds,
submissionVisits,
itemIterations
]
return {
measure,
full,
status,
observerReads,
registration,
releases,
keys,
timers,
cleared,
details: () => ({
callbacks,
committedRows,
deltaReads: deltas.mock.calls.length,
deltaRows,
copied,
iteratorYields,
outputIterations,
projections,
projectedRows,
scannedRows
})
}
}
@@ -1,4 +1,3 @@
import { measureAutomationChatRun } from './automation-chat-run-measurement-test-harness'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type {
AgentJournalSnapshot,
@@ -29,7 +28,7 @@ const mocks = vi.hoisted(() => {
return {
...hostState,
host: {
journalCurrentRead: vi.fn(),
automationRun: vi.fn(),
observeConversationPublications: vi.fn(),
queuedSendAnswer: vi.fn(async () => null)
}
@@ -122,15 +121,15 @@ describe('restart observation and disposal', () => {
const release = vi.fn()
const read = Promise.withResolvers<AgentJournalSnapshot>()
mocks.host.observeConversationPublications.mockImplementation((_id, observer) => {
expect(mocks.host.journalCurrentRead).not.toHaveBeenCalled()
expect(mocks.host.automationRun).not.toHaveBeenCalled()
observer.next({ kind: 'reset', source: {}, journal: null })
return release
})
mocks.host.journalCurrentRead.mockReturnValue(read.promise)
mocks.host.automationRun.mockReturnValue(read.promise)
const controller = new AbortController()
const pending = observeAutomationChatRun(runtime, run, controller.signal)
const rejected = expect(pending).rejects.toThrow('request_aborted')
await vi.waitFor(() => expect(mocks.host.journalCurrentRead).toHaveBeenCalledOnce())
await vi.waitFor(() => expect(mocks.host.automationRun).toHaveBeenCalledOnce())
controller.abort()
await rejected
read.resolve(view)
@@ -150,10 +149,10 @@ describe('restart observation and disposal', () => {
})
const delayed = Promise.withResolvers<null>()
const opening = Promise.withResolvers<AgentJournalSnapshot>()
mocks.host.journalCurrentRead.mockReturnValueOnce(opening.promise).mockResolvedValue(view)
mocks.host.automationRun.mockReturnValueOnce(opening.promise).mockResolvedValue(view)
mocks.host.queuedSendAnswer.mockReturnValueOnce(delayed.promise)
const pending = observeAutomationChatRun(runtime, run, new AbortController().signal)
await vi.waitFor(() => expect(mocks.host.journalCurrentRead).toHaveBeenCalledOnce())
await vi.waitFor(() => expect(mocks.host.automationRun).toHaveBeenCalledOnce())
notify({ kind: 'reset', source: {}, journal: null })
opening.resolve({
...view,
@@ -164,275 +163,113 @@ describe('restart observation and disposal', () => {
view.items[0] = turn('own', view.submissions[0].providerItemId!)
delayed.resolve(null)
expect(await pending).toMatchObject({ status: 'completed' })
expect(mocks.host.journalCurrentRead).toHaveBeenCalledTimes(3)
expect(mocks.host.automationRun).toHaveBeenCalledTimes(3)
expect(release).toHaveBeenCalledOnce()
})
})
// The provider is scripted; queue ownership, journal, catch-up and publication are real.
describe('real host queue publication and operation bounds', () => {
// Two history sizes pin every per-history coefficient below; 1000 turns only made seeding outlast the timeout under load.
it.each([20, 100])(
'observes Q→S at %i prior turns without token-triggered history scans',
async (history) => {
const totals: number[][] = []
for (const mode of ['baseline', 'internal', 'display'] as const) {
const rig = await createQueuedMessageTestRig({ restartable: true })
mocks.actualHost = rig.host
const controller = new AbortController()
try {
const working = await rig.workingSend()
let sink = rig.providerEvents()
const journal = rig.host.collaboratorsForTests().sessions.get(HOST_TEST_SESSION)!.journal
for (let n = 0; n < history; n++) {
await journal.appendSubmission({
clientMessageId: `old-send-${n}`,
payloadFingerprint: 'a'.repeat(64),
fence: 1,
body: hostTestMessage(`old prompt ${n}`)
})
await journal.resolveDispatch({
clientMessageId: `old-send-${n}`,
state: 'accepted',
providerIdentity: null,
fence: 1
})
sink.appendItem(
{ provider: 'codex', threadId: HOST_TEST_THREAD, turnId: `old-${n}`, ordinal: 0 },
{ kind: 'turn', turnId: `old-${n}`, state: 'completed', outcome: 'success' },
{ turnScope: AGENT_JOURNAL_THREAD_SCOPE }
)
if (n % 100 === 99) {
await sink.written?.()
}
}
await sink.written?.()
const run = {
...buildProfileStateCutoverFixture('/fixture').automationRuns[0],
// ast-grep-ignore: test-no-wall-clock -- Run timestamps must match the live host expiry clock.
createdAt: Date.now(),
// ast-grep-ignore: test-no-wall-clock -- Run timestamps must match the live host expiry clock.
dispatchedAt: Date.now(),
sessionKind: 'chat' as const,
chatSessionId: HOST_TEST_SESSION
}
const q = automationRunOperationId(run, 'reuse-send')
const body = hostTestMessage('automation behind human backlog')
const fields = { body, delivery: 'queue-if-active' as const }
expect(
await rig.host.send(
{ callerKey: 'client-1' },
{
...fields,
envelope: rig.envelope(fields, 'agentSession.send', q)
}
)
).toMatchObject({ ok: true, value: { queued: { messageId: q, state: 'waiting' } } })
const metrics = measureAutomationChatRun(journal, rig.host)
const {
measure,
full,
status,
observerReads,
registration,
releases,
keys,
timers,
cleared
} = metrics
const tokenBurst = async () => {
for (let n = 0; n < 25; n++) {
sink.appendItem(
{ provider: 'codex', threadId: HOST_TEST_THREAD, turnId: 'unrelated', ordinal: 1 },
{
kind: 'message',
role: 'assistant',
blocks: [{ type: 'text', text: `token-${n}` }]
},
{ turnScope: AGENT_JOURNAL_THREAD_SCOPE }
)
sink.publish()
await sink.written?.()
await rig.host.flushStreamedEvents(HOST_TEST_SESSION)
}
}
const difference = (before: number[]) =>
measure().map((value, index) => value - before[index])
const displayRelease =
mode === 'display'
? await rig.host.subscribe({
id: 'ordinary-display',
sessionId: HOST_TEST_SESSION,
emit: () => {}
})
: () => {}
const beforeBaseline = measure()
await tokenBurst()
const baselineWork = difference(beforeBaseline)
const runtimeFacade = {
ensureStructuredAgentSessionHost: async () => {},
openAgentSessionRecordStore: async () => ({
getOperationRow: (_caller: string, id: string) =>
id === q
? { outcome: { status: 'succeeded', sessionId: HOST_TEST_SESSION } }
: undefined,
getConversation: () => ({
provider: 'codex',
location: { workspaceId: 'workspace-1' }
})
})
}
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: initialization/receipt reads are scripted; the actual host/journal/queue/observer and N/10N measurements execute.
const runtime = runtimeFacade as unknown as OrcaRuntimeService
let pending =
mode === 'internal' ? observeAutomationChatRun(runtime, run, controller.signal) : null
void pending?.catch(() => {})
if (pending) {
await eventually(() => expect(registration).toHaveBeenCalledOnce())
}
await rig.host.flushStreamedEvents(HOST_TEST_SESSION)
expect(status.mock.calls.length).toBeGreaterThan(0)
expect(full).not.toHaveBeenCalled()
const beforeTokens = measure()
const readsBeforeTokens = observerReads.mock.calls.length
const keysBeforeTokens = keys.map((spy) => spy.mock.calls.length)
await tokenBurst()
expect(observerReads.mock.calls.length).toBe(readsBeforeTokens)
const observerWork = difference(beforeTokens)
expect(observerWork).toEqual(baselineWork)
const keyedAdded = keys.map((spy, i) => spy.mock.calls.length - keysBeforeTokens[i])
expect(keyedAdded.reduce((a, b) => a + b, 0)).toBeLessThanOrEqual(25 * 12)
const unchangedReads = observerReads.mock.calls.length
await journal.resolveDispatch({
clientMessageId: 'old-send-0',
state: 'accepted',
providerIdentity: null,
fence: 1
})
await rig.host.flushStreamedEvents(HOST_TEST_SESSION)
expect(observerReads).toHaveBeenCalledTimes(unchangedReads)
if (pending) {
controller.abort()
await expect(pending).rejects.toThrow('request_aborted')
expect(releases[0]).toHaveBeenCalledOnce()
pending = observeAutomationChatRun(runtime, run, new AbortController().signal)
await eventually(() => expect(registration).toHaveBeenCalledTimes(2))
}
await rig.settleAccepted(working, 'human')
sink.appendItem(
{ provider: 'codex', threadId: HOST_TEST_THREAD, turnId: 'turn-human', ordinal: 1 },
{ kind: 'turn', turnId: 'turn-human', state: 'completed', outcome: 'success' },
{ turnScope: AGENT_JOURNAL_THREAD_SCOPE, lifecycle: true }
)
sink.publish({ lifecycle: true })
expect(await sink.written?.()).toEqual({ ok: true })
await rig.host.flushStreamedEvents(HOST_TEST_SESSION)
const handoff = () =>
journal.submissions().findLast((entry) => entry.queuedMessageId === q)
await eventually(() => expect(handoff()).toBeDefined())
const sent = handoff()!.clientMessageId
expect(sent).not.toBe(q)
await eventually(() => expect(journal.submission(sent)?.handedOverAt).toBeDefined())
await rig.settleAccepted(sent, 'automation')
await rig.host.flushStreamedEvents(HOST_TEST_SESSION)
const beforeRestatement = observerReads.mock.calls.length
await journal.resolveDispatch({
clientMessageId: sent,
state: 'accepted',
providerIdentity: null,
fence: 1
})
await rig.host.flushStreamedEvents(HOST_TEST_SESSION)
expect(observerReads).toHaveBeenCalledTimes(beforeRestatement)
const beforeWork = observerReads.mock.calls.length
rig.host['clientDelivery'].publishRestored(HOST_TEST_SESSION)
rig.host.publishWorkChanged(HOST_TEST_SESSION)
if (pending) {
await eventually(() =>
expect(observerReads.mock.calls.length).toBeGreaterThan(beforeWork)
)
}
const accepted = journal.submission(sent)
sink = rig.providerEvents()
const ownId = {
provider: 'codex' as const,
threadId: HOST_TEST_THREAD,
turnId: 'turn-automation',
ordinal: 1
}
sink.appendItem(
ownId,
{
kind: 'turn',
turnId: 'turn-automation',
userItemId: accepted!.providerItemId!,
state: 'completed',
outcome: 'success'
},
{ turnScope: AGENT_JOURNAL_THREAD_SCOPE, lifecycle: true }
)
sink.appendItem(
{ ...ownId, ordinal: 2 },
{
kind: 'message',
role: 'assistant',
blocks: [{ type: 'text', text: 'Exact automation output' }]
},
{ turnScope: { kind: 'turn', turnItemId: agentJournalItemKey(ownId) } }
)
sink.publish({ lifecycle: true })
expect(await sink.written?.()).toEqual({ ok: true })
await rig.host.flushStreamedEvents(HOST_TEST_SESSION)
expect(
pending
? await pending
: chatAutomationRunObservation(
await rig.host.journalCurrentRead(HOST_TEST_SESSION),
q,
'Codex',
await rig.host.queuedSendAnswer(HOST_TEST_SESSION, q)
)
).toMatchObject({
status: 'completed',
outputSnapshot: { content: 'Exact automation output' }
})
expect(rig.dispatch).toHaveBeenCalledTimes(2)
expect(releases).toHaveLength(mode === 'internal' ? 2 : 0)
for (const release of releases) {
expect(release).toHaveBeenCalledOnce()
}
for (const [index, args] of timers.mock.calls.entries()) {
if (Number(args[1]) > 5 * 60 * 60 * 1000) {
expect(cleared).toHaveBeenCalledWith(timers.mock.results[index].value)
}
}
displayRelease()
expect(registration).toHaveBeenCalledTimes(mode === 'internal' ? 2 : 0)
expect(observerReads.mock.calls.length).toBeLessThanOrEqual(12)
const { copied, iteratorYields, projectedRows, scannedRows } = metrics.details()
expect(copied + iteratorYields + projectedRows + scannedRows).toBeLessThan(
(history + 30) * 4000
)
const sessions = rig.host.collaboratorsForTests().sessions
expect(sessions['observers'].size).toBe(0)
expect(sessions['attachments'].size).toBe(1)
totals.push(measure())
process.stdout.write(
`${JSON.stringify({ history, mode, sourceAttachments: sessions['attachments'].size, logicalObservers: sessions['observers'].size, baselineWork, observerWork, keyedAdded, totals: measure(), ...metrics.details(), reconciliations: observerReads.mock.calls.length, registrations: registration.mock.calls.length, releases: releases.filter((r) => r.mock.calls.length === 1).length })}\n`
)
} finally {
controller.abort()
mocks.actualHost = null
await rig.dispose()
vi.restoreAllMocks()
}
describe('real host queue publication', () => {
it('follows a queued run through handover and returns its exact output', async () => {
const rig = await createQueuedMessageTestRig({ restartable: true })
mocks.actualHost = rig.host
const controller = new AbortController()
try {
const working = await rig.workingSend()
const journal = rig.host.collaboratorsForTests().sessions.get(HOST_TEST_SESSION)!.journal
const run = {
...buildProfileStateCutoverFixture('/fixture').automationRuns[0],
// ast-grep-ignore: test-no-wall-clock -- Match the observer's expiry clock.
createdAt: Date.now(),
// ast-grep-ignore: test-no-wall-clock -- Match the observer's expiry clock.
dispatchedAt: Date.now(),
sessionKind: 'chat' as const,
chatSessionId: HOST_TEST_SESSION
}
const added = totals[1].map((value, i) => value - totals[0][i])
process.stdout.write(
`${JSON.stringify({ history, fullWorkloadBaseline: totals[0], fullWorkloadInternal: totals[1], added, displayControl: totals[2] })}\n`
const q = automationRunOperationId(run, 'reuse-send')
const fields = {
body: hostTestMessage('automation behind human backlog'),
delivery: 'queue-if-active' as const
}
expect(
await rig.host.send(
{ callerKey: 'client-1' },
{
...fields,
envelope: rig.envelope(fields, 'agentSession.send', q)
}
)
).toMatchObject({ ok: true, value: { queued: { messageId: q, state: 'waiting' } } })
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: only runtime initialization and receipt lookup are scripted; the host owns delivery and observation.
const runtime = {
ensureStructuredAgentSessionHost: async () => {},
openAgentSessionRecordStore: async () => ({
getOperationRow: (_caller: string, id: string) =>
id === q
? { outcome: { status: 'succeeded', sessionId: HOST_TEST_SESSION } }
: undefined,
getConversation: () => ({ provider: 'codex', location: { workspaceId: 'workspace-1' } })
})
} as unknown as OrcaRuntimeService
const pending = observeAutomationChatRun(runtime, run, controller.signal)
void pending.catch(() => {})
await rig.settleAccepted(working, 'human')
let sink = rig.providerEvents()
sink.appendItem(
{ provider: 'codex', threadId: HOST_TEST_THREAD, turnId: 'turn-human', ordinal: 1 },
{ kind: 'turn', turnId: 'turn-human', state: 'completed', outcome: 'success' },
{ turnScope: AGENT_JOURNAL_THREAD_SCOPE, lifecycle: true }
)
sink.publish({ lifecycle: true })
expect(await sink.written?.()).toEqual({ ok: true })
await rig.host.flushStreamedEvents(HOST_TEST_SESSION)
const sent = await rig.handoffId(q)
expect(sent).not.toBe(q)
await eventually(async () =>
expect((await journal.reads.submission(sent))?.handedOverAt).toBeDefined()
)
await rig.settleAccepted(sent, 'automation')
const accepted = await journal.reads.submission(sent)
sink = rig.providerEvents()
const ownId = {
provider: 'codex' as const,
threadId: HOST_TEST_THREAD,
turnId: 'turn-automation',
ordinal: 1
}
sink.appendItem(
ownId,
{
kind: 'turn',
turnId: 'turn-automation',
userItemId: accepted!.providerItemId!,
state: 'completed',
outcome: 'success'
},
{ turnScope: AGENT_JOURNAL_THREAD_SCOPE, lifecycle: true }
)
sink.appendItem(
{ ...ownId, ordinal: 2 },
{
kind: 'message',
role: 'assistant',
blocks: [{ type: 'text', text: 'Exact automation output' }]
},
{ turnScope: { kind: 'turn', turnItemId: agentJournalItemKey(ownId) } }
)
sink.publish({ lifecycle: true })
expect(await sink.written?.()).toEqual({ ok: true })
await rig.host.flushStreamedEvents(HOST_TEST_SESSION)
expect(await pending).toMatchObject({
status: 'completed',
outputSnapshot: { content: 'Exact automation output' }
})
expect(rig.dispatch).toHaveBeenCalledTimes(2)
} finally {
controller.abort()
mocks.actualHost = null
await rig.dispose()
}
)
})
it.each(['withdraw', 'return', 'missing', 'end'] as const)(
'settles a queue-only %s without resending',
async (outcome) => {
@@ -473,12 +310,12 @@ describe('real host queue publication and operation bounds', () => {
(value) => value,
(error) => error
)
const reads = vi.spyOn(rig.host, 'journalCurrentRead')
const reads = vi.spyOn(rig.host, 'automationRun')
await eventually(() => expect(reads).toHaveBeenCalled())
expect(
chatAutomationRunObservation(
await rig.host.journalCurrentRead(HOST_TEST_SESSION),
await rig.host.automationRun(HOST_TEST_SESSION, q, []),
q,
'Codex',
await rig.host.queuedSendAnswer(HOST_TEST_SESSION, q)
@@ -513,7 +350,7 @@ describe('real host queue publication and operation bounds', () => {
expect(rig.dispatch).toHaveBeenCalledTimes(sends)
expect(
chatAutomationRunObservation(
await rig.host.journalCurrentRead(HOST_TEST_SESSION),
await rig.host.automationRun(HOST_TEST_SESSION, q, []),
q,
'Codex',
await rig.host.queuedSendAnswer(HOST_TEST_SESSION, q)
@@ -527,7 +364,6 @@ describe('real host queue publication and operation bounds', () => {
rig.host.stopDelivery()
}
expect(await liveResult).toBeInstanceOf(Error)
expect(sessions['observers'].size).toBe(0)
return
}
if (outcome === 'withdraw') {
@@ -540,7 +376,7 @@ describe('real host queue publication and operation bounds', () => {
}
expect(
chatAutomationRunObservation(
await rig.host.journalCurrentRead(HOST_TEST_SESSION),
await rig.host.automationRun(HOST_TEST_SESSION, q, []),
q,
'Codex',
await rig.host.queuedSendAnswer(HOST_TEST_SESSION, q)
+7 -9
View File
@@ -1,3 +1,5 @@
import { readTestJournalItems } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { openTestAgentSessionDatabase } from '../runtime/agent-session/records/agent-session-record-store-test-harness'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -7,10 +9,7 @@ import type { AgentSessionJournalIdentity } from '../../shared/agent-session/jou
import { openAgentSessionJournal } from '../native-chat/agent-session-journal/journal-store-factory'
import { createDeferredStructuredAgentSessionEventSink } from '../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink'
import { createClaudeJournalTranslator } from './structured/journal/claude-structured-journal-translation'
import {
closeTestJournalHostDatabases,
openTestJournalHostDatabase
} from '../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { closeTestJournalHostDatabases } 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'
import { claudeProviderHandle } from '../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
@@ -51,7 +50,7 @@ afterEach(async () => {
async function statusRowsFor(frames: Record<string, unknown>[]) {
const journal = await openAgentSessionJournal({
identity: IDENTITY,
database: openTestJournalHostDatabase(root),
database: await openTestAgentSessionDatabase(root),
now: () => 1_700_000_000_000,
mintEpoch: () => 'epoch-1'
})
@@ -62,11 +61,10 @@ async function statusRowsFor(frames: Record<string, unknown>[]) {
translator.handle({ type: 'message', sessionId: 'orca-session', message: frame })
await deferred.drained()
}
return journal
.snapshot()
.items.flatMap((item): Extract<AgentJournalItemBody, { kind: 'status' }>[] =>
return (await readTestJournalItems(journal)).flatMap(
(item): Extract<AgentJournalItemBody, { kind: 'status' }>[] =>
item.body.kind === 'status' ? [item.body] : []
)
)
}
describe('a Claude api_retry frame', () => {
@@ -7,6 +7,11 @@ import {
import type { AgentSessionRecord } from '../../shared/agent-session/lifecycle/agent-session-record'
import { agentChildWorkLiveness } from '../../shared/agent-status/child-work/agent-status-child-work-liveness'
import { conversationCommandBlocked } from '../native-chat/agent-session-wire/operations/structured-conversation-command-admission'
import type { AgentSessionJournal } from '../native-chat/agent-session-journal/journal-store'
import type {
JournalWorkAnswer,
JournalWorkRead
} from '../native-chat/agent-session-journal/journal-work-read'
import type { CapturedFrame } from './claude-captured-frame-builders.test-fixture'
import { RESUMED_BY_MESSAGE } from './claude-captured-task-frames.test-fixture'
import { hostWithParent, parent, producer } from './claude-child-work-producer-harness.test-fixture'
@@ -18,6 +23,23 @@ afterEach(() => vi.restoreAllMocks())
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: admission reads only the lease and the command/rewind records, all absent here.
const RECORD = { lease: {} } as unknown as AgentSessionRecord
/** A live generation with no turn, prompt or owed send: only child work can block. */
const IDLE_WORK: JournalWorkAnswer<'state'>['state'] = {
liveFence: 7,
allowsObservation: true,
working: false,
commandAdmission: { promptPending: false, owesSend: false, handoverOwesSend: false },
owesAnySend: false,
owedEnteredSubmissions: [],
owedSendingSubmissions: [],
activeTurn: null,
latestTurn: null,
actionablePrompts: [],
restatedSubmissions: [],
viewKey: '',
workViewKey: ''
}
/** Host records retain live work after provider roster removal. */
async function droppedFromRoster() {
const host = hostWithParent()
@@ -33,17 +55,11 @@ async function droppedFromRoster() {
{
sessionId: 'session-1',
fence: 7,
journal: {
itemsRead: () => ({ items: [] }),
cursor: () => ({ epoch: 'e', sequence: 0 }),
item: () => null,
submissions: () => [],
inputOrder: { recoverableSubmissions: () => [] },
runningTurn: () => null,
itemFence: () => undefined,
visitItems: () => undefined,
wroteBeforeOpen: () => false
},
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: admission reads only through readWork.
journal: {} as AgentSessionJournal,
readWork: async <K extends JournalWorkRead['kind']>() =>
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: command admission requests only the state answer.
({ kind: 'state', state: IDLE_WORK }) as JournalWorkAnswer<K>,
adapter: run.adapter
},
RECORD,
@@ -55,7 +71,7 @@ async function droppedFromRoster() {
{ membership: 'live', stoppable: true, providerId: 'agent-1', kind: 'agent' }
])
expect(run.adapter.backgroundTaskState('session-1')).toBeNull()
expect(blocked()?.message).toBe('Stop background tasks before using this command.')
expect((await blocked())?.message).toBe('Stop background tasks before using this command.')
// Snapshot stop targets before dispatch; settle only acknowledged records.
const stop = async (taskId?: string) => {
const targets = agentChildWorkStopTargets(records(), taskId)
@@ -87,7 +103,7 @@ describe('a background Stop acts on the host child records', () => {
expect(stopCalls()).toEqual([{ subtype: 'stop_task', params: { taskId: 'agent-1' } }])
expect(records()).toMatchObject([{ membership: 'settled', outcome: 'cancelled' }])
expect(basis()).toEqual(['stop-acknowledged'])
expect(blocked()).toBeNull()
expect(await blocked()).toBeNull()
})
it('leaves a task that ended on its own frame as it ended: its row offers no stop', async () => {
@@ -1,65 +1,59 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import type { AgentJournalSubmission } from '../../shared/agent-session/journal/agent-session-journal-types'
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 { AGENT_JOURNAL_THREAD_SCOPE } from '../../shared/agent-session/journal/agent-session-journal-types'
import { structuredRunningChildWork } from '../../shared/agent-status/child-work/agent-child-work-listing'
import type { AgentHookServer } from '../agent-hooks/server'
import type { StructuredAgentSessionJournalProjections } from '../native-chat/agent-session-wire/observations/structured-agent-session-status-journal-projection'
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 { 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'
import { StructuredAgentSessionStatusFeed } from '../native-chat/agent-session-wire/observations/structured-agent-session-status-feed'
import { indexedStatusFeedSession } from '../native-chat/agent-session-wire/observations/testing/structured-agent-session-status-feed-test-session.test-fixture'
import type { CapturedFrame } from './claude-captured-frame-builders.test-fixture'
import { backgroundWakeCapture } from './claude-captured-fold-steer-frames.test-fixture'
import { MOVED_TO_BACKGROUND } from './claude-captured-task-frames.test-fixture'
import { hostWithParent, parent, producer } from './claude-child-work-producer-harness.test-fixture'
import { PROVIDER_SESSION_ID } from './structured/claude-structured-session-test-support'
import {
identityFor,
PROVIDER_SESSION_ID
} from './structured/claude-structured-session-test-support'
import { createStructuredAgentSessionLogger } from '../native-chat/agent-session-wire/host/structured-agent-session-logger'
vi.mock('../telemetry/client', () => ({ track: vi.fn() }))
vi.mock('../telemetry/cohort-classifier', () => ({ getCohortAtEmit: vi.fn(() => ({})) }))
afterEach(() => vi.restoreAllMocks())
type Journal = Parameters<StructuredAgentSessionJournalProjections['read']>[0]
const journals = createTrackedJournalOpener()
let root: string
beforeEach(async () => {
root = await mkdtemp(join(tmpdir(), 'orca-claude-wake-retention-'))
})
afterEach(async () => {
await journals.closeAll()
await rm(root, { recursive: true, force: true })
})
async function wiredSession() {
const host: AgentHookServer = hostWithParent()
const run = await producer(host)
const submissions: AgentJournalSubmission[] = []
let sequence = 0
const journal = {
cursor: () => ({ epoch: 'epoch-1', sequence: ++sequence }),
lastActivityAt: () => 1,
submissions: (): ReturnType<Journal['submissions']> =>
submissions.map((submission) => ({ ...submission })),
item: (itemId: string): ReturnType<Journal['item']> => {
const item = run.journalItems.get(itemId)
return item ? { ...item, revision: 1, observedAt: run.now() } : null
},
statusRead(): ReturnType<Journal['statusRead']> {
return this.snapshot()
},
inputOrder: {
inputCount: () => submissions.length,
inputOrdinal: (id: string) => submissions.findIndex((s) => s.clientMessageId === id),
recoverableSubmissions: () =>
submissions.filter((s) => s.dispatchState === 'pending' || s.dispatchState === 'unknown')
},
context: { floor: () => null },
stopMarks: { latest: () => null, revision: () => 0 },
itemFence: () => undefined,
wroteBeforeOpen: () => false,
runningTurn: () => null,
snapshot: () => ({
items: [...run.journalItems.values()]
.sort((a, b) => a.sequence - b.sequence)
.map((item) => ({ ...item, revision: 1, observedAt: run.now() })),
submissions: submissions.map((submission) => ({ ...submission }))
})
}
const journal = await journals.open({
identity: identityFor(parent.sessionId),
stateDirectory: root
})
const feed = new StructuredAgentSessionStatusFeed({
logger: createStructuredAgentSessionLogger(),
sessions: new Map([
[
parent.sessionId,
{
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the fixture supplies the feed's cursor, clock, Stop marks, context floor, snapshot, submissions, item and provenance reads.
journal: journal as unknown as Journal,
...indexedStatusFeedSession({
journal,
child: { generation: 'spawn-9', fence: 7, phase: 'ready' },
provider: 'claude'
}),
params: {
location: {
executionHostId: parent.executionHostId,
@@ -82,53 +76,68 @@ async function wiredSession() {
readChildWork: (subject) => host.getStructuredChildWorkViews(subject)
})
})
const publish = () => feed.publish(parent.sessionId)
const deferred = createDeferredStructuredAgentSessionEventSink(
testEventSinkLogging(parent.sessionId)
)
deferred.bind({ journal, fence: 7, publish: () => void publish() })
const run = await producer(host, deferred.sink)
const describe = (views: ReturnType<typeof host.getStructuredChildWorkViews>) =>
views.map((view) => `${view.description}:${view.membership}:${view.outcome ?? ''}`)
const records = () => describe(host.getStructuredChildWorkViews(parent))
const running = () =>
describe(structuredRunningChildWork(host.getStructuredChildWorkViews(parent)))
const replay = (frames: readonly CapturedFrame[]) => {
const replay = async (frames: readonly CapturedFrame[]) => {
for (const captured of frames) {
run.replay([captured])
feed.publish(parent.sessionId)
expect(await deferred.drained()).toEqual({ ok: true })
await publish()
}
}
const send = (clientMessageId: string): AgentJournalSubmission => {
const submission: AgentJournalSubmission = {
const send = async (clientMessageId: string): Promise<string> => {
await journal.appendSubmission({
clientMessageId,
fence: 7,
payloadFingerprint: clientMessageId,
dispatchState: 'pending',
providerItemId: null,
reason: null,
submittedAt: run.now(),
resolvedAt: null
}
submissions.push(submission)
feed.publish(parent.sessionId)
return submission
fence: 7,
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: clientMessageId }] }
})
await journal.resolveDispatch({
clientMessageId,
state: 'pending',
fence: 7,
turnScope: AGENT_JOURNAL_THREAD_SCOPE
})
await publish()
return clientMessageId
}
const accept = (submission: AgentJournalSubmission) => {
submission.dispatchState = 'accepted'
feed.publish(parent.sessionId)
const accept = async (clientMessageId: string) => {
await journal.resolveDispatch({
clientMessageId,
state: 'accepted',
fence: 7,
providerIdentity: null
})
await publish()
}
const rootTurns = () =>
[...run.journalItems.values()].filter((item) => !item.agentId && item.body.kind === 'turn')
const rootTurns = async () =>
(await readTestJournalItems(journal)).filter(
(item) => !item.agentId && item.body.kind === 'turn'
)
return { run, feed, records, running, replay, send, accept, rootTurns }
}
describe("a finished child's record stays until the user's next turn", () => {
it("keeps a background agent through Claude's own wake turn, then retires it at the next send", async () => {
const session = await wiredSession()
session.replay(MOVED_TO_BACKGROUND)
expect(session.rootTurns()).toHaveLength(2)
await session.replay(MOVED_TO_BACKGROUND)
expect(await session.rootTurns()).toHaveLength(2)
expect(session.records()).toContain('Run 45s sleep command:settled:succeeded')
expect(session.running()).toEqual([])
const next = session.send('next-send')
const next = await session.send('next-send')
// Submitting is not acceptance; a refused send must not retire retained work.
expect(session.records()).toContain('Run 45s sleep command:settled:succeeded')
session.accept(next)
await session.accept(next)
expect(session.records()).toEqual([])
})
@@ -139,19 +148,19 @@ describe("a finished child's record stays until the user's next turn", () => {
first: 'first-uuid',
steer: 'steer-uuid'
})
session.accept(session.send('first-uuid'))
session.replay(firstTurn)
await session.accept(await session.send('first-uuid'))
await session.replay(firstTurn)
expect(session.running()).toEqual(['Sleep then print marker:live:'])
const wakeInit = wake.findIndex((captured) => captured.frame.subtype === 'init')
session.replay(wake.slice(0, wakeInit + 1))
expect(session.rootTurns()).toHaveLength(2)
await session.replay(wake.slice(0, wakeInit + 1))
expect(await session.rootTurns()).toHaveLength(2)
expect(session.records()).toEqual(['Sleep then print marker:settled:succeeded'])
expect(session.running()).toEqual([])
session.accept(session.send('steer-uuid'))
await session.accept(await session.send('steer-uuid'))
expect(session.records()).toEqual([])
session.replay(wake.slice(wakeInit + 1))
await session.replay(wake.slice(wakeInit + 1))
expect(session.records()).toEqual([])
})
})
@@ -90,8 +90,9 @@ export function hostWithParent(): AgentHookServer {
return host
}
/** With `host`, evidence goes through the host's own ingest instead of straight to reconciliation. */
export async function producer(host?: AgentHookServer) {
/** With `host`, evidence goes through the host's own ingest instead of straight to reconciliation;
* with `events`, rows go to that sink instead of the in-memory recorder. */
export async function producer(host?: AgentHookServer, events?: StructuredAgentSessionEventSink) {
const claude = fakeClaude()
const store = createAgentStatusStore({ epoch: 'epoch-1', mode: 'authority' })
expect(store.applyMutation({ parent: { subject: parent } })).not.toBeNull()
@@ -157,7 +158,7 @@ export async function producer(host?: AgentHookServer) {
identity: identityFor(),
fence: 7,
spawnToken: 'spawn-9',
events: journal
events: events ?? journal
})
const send = (message: Record<string, unknown>): Delivery[] => {
const from = deliveries.length
@@ -1,3 +1,4 @@
import { readTestJournalItems } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -141,8 +142,8 @@ function acquire(journal: AgentSessionJournal) {
return { translator, answers, settle, detach, reattach, release }
}
const ring = (journal: AgentSessionJournal) =>
selectStructuredAgentContextUsage(journal.snapshot().items)
const ring = async (journal: AgentSessionJournal) =>
selectStructuredAgentContextUsage(await readTestJournalItems(journal))
describe('context usage across a restart', () => {
it('resets the ring at a /compact that is the first thing after a restart, then reads the fresh report', async () => {
@@ -153,20 +154,20 @@ describe('context usage across a restart', () => {
before.translator.handle(assistantFrame('reply-a', 2_000, 150_000))
before.translator.handle(resultFrame(3_000))
await before.settle()
expect(ring(journal)).toMatchObject({ usedTokens: 150_000, windowTokens: 1_000_000 })
expect(await ring(journal)).toMatchObject({ usedTokens: 150_000, windowTokens: 1_000_000 })
before.release()
const after = acquire(journal)
after.translator.handle(compactBoundary(10_000))
await after.settle()
expect(ring(journal)).toBeNull()
expect(await ring(journal)).toBeNull()
after.translator.handle(resultFrame(11_000))
await after.settle()
expect(ring(journal)).toBeNull()
expect(await ring(journal)).toBeNull()
after.answers.at(-1)?.(postCompactionReport)
await after.settle()
expect(ring(journal)).toMatchObject({
expect(await ring(journal)).toMatchObject({
usedTokens: 40_000,
windowTokens: 1_000_000,
estimated: false,
@@ -193,9 +194,8 @@ describe('context usage across a restart', () => {
})
live.reattach()
await live.settle()
const turn = journal
.snapshot()
.items.map((item) => readAgentJournalTurn(item.body))
const turn = (await readTestJournalItems(journal))
.map((item) => readAgentJournalTurn(item.body))
.find(Boolean)
expect(turn).toMatchObject({
state: 'unverifiable',
@@ -1,3 +1,4 @@
import { readTestJournalItems } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { agentSessionConversationView } from '../../shared/agent-session/lifecycle/agent-session-conversation-view'
import {
agentSessionLeaseFixture,
@@ -113,8 +114,15 @@ function readOptions(journal: AgentSessionJournal, agents: StructuredAgentRegist
return readStructuredAgentSessionOptions(context, SESSION, { readsWithoutModel: true })
}
function attachTail(journal: AgentSessionJournal, limit: number): StructuredAgentSessionState {
const read = readAgentSessionHistory(journal, { sessionId: SESSION, direction: 'tail', limit })
async function attachTail(
journal: AgentSessionJournal,
limit: number
): Promise<StructuredAgentSessionState> {
const read = await readAgentSessionHistory(journal, {
sessionId: SESSION,
direction: 'tail',
limit
})
if (!read.ok) {
throw new Error('tail read failed')
}
@@ -124,11 +132,11 @@ function attachTail(journal: AgentSessionJournal, limit: number): StructuredAgen
})
}
function applyLive(
async function applyLive(
journal: AgentSessionJournal,
state: StructuredAgentSessionState
): StructuredAgentSessionState {
const read = readAgentSessionHistory(journal, {
): Promise<StructuredAgentSessionState> {
const read = await readAgentSessionHistory(journal, {
sessionId: SESSION,
direction: 'after',
cursor: state.cursor ?? undefined
@@ -155,14 +163,14 @@ function applyLive(
const hasTurnRow = (state: StructuredAgentSessionState): boolean =>
state.items.some((item) => readAgentJournalTurn(item.body) !== null)
function catchUp(
async function catchUp(
journal: AgentSessionJournal,
state: StructuredAgentSessionState
): StructuredAgentSessionState {
let next = applyLive(journal, state)
): Promise<StructuredAgentSessionState> {
let next = await applyLive(journal, state)
while (next.cursor?.sequence !== state.cursor?.sequence) {
state = next
next = applyLive(journal, state)
next = await applyLive(journal, state)
}
return next
}
@@ -175,13 +183,13 @@ describe('context usage for a turn row outside the loaded page', () => {
translator.handle(resultFrame(2_000, { 'claude-fable-5-1[1m]': { contextWindow: 1_000_000 } }))
runTurn(translator, 'turn-b', 3_000, [200_000, 210_000, 220_000, 230_000])
await settle()
const attached = attachTail(journal, 3)
const attached = await attachTail(journal, 3)
expect(hasTurnRow(attached)).toBe(false)
expect(attached.unloadedTurnRevisions).toBeUndefined()
translator.handle(assistantFrame('turn-b-reply-late', 4_000, 250_000))
await settle()
const live = applyLive(journal, attached)
const live = await applyLive(journal, attached)
expect(hasTurnRow(live)).toBe(false)
expect(live.unloadedTurnRevisions).toBe(1)
@@ -200,7 +208,7 @@ describe('context usage for a turn row outside the loaded page', () => {
await settle()
const hostAnswer = async () =>
(await readOptions(journal, claudeAndCodexAgents())).contextUsage?.current
let state = attachTail(journal, 200)
let state = await attachTail(journal, 200)
let host = await hostAnswer()
const respond = (index: number): void => {
translator.handle(assistantFrame(`turn-b-reply-${index}`, 4_000 + index, 200_000 + index))
@@ -213,7 +221,7 @@ describe('context usage for a turn row outside the loaded page', () => {
}
}
await settle()
state = catchUp(journal, state)
state = await catchUp(journal, state)
expect(hasTurnRow(state)).toBe(true)
let stepsAfterTrim = 0
@@ -221,12 +229,12 @@ describe('context usage for a turn row outside the loaded page', () => {
respond(index++)
await settle()
const seen = state.unloadedTurnRevisions
state = catchUp(journal, state)
state = await catchUp(journal, state)
if (state.unloadedTurnRevisions !== seen) {
host = await hostAnswer()
}
expect(selectStructuredAgentContextUsage(state.items, host)).toEqual(
selectStructuredAgentContextUsage(journal.snapshot().items)
selectStructuredAgentContextUsage(await readTestJournalItems(journal))
)
stepsAfterTrim += hasTurnRow(state) ? 0 : 1
}
@@ -1,3 +1,5 @@
import { readTestJournalItems } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { openTestAgentSessionDatabase } from '../runtime/agent-session/records/agent-session-record-store-test-harness'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -6,10 +8,7 @@ import type { AgentSessionJournalIdentity } from '../../shared/agent-session/jou
import { openAgentSessionJournal } from '../native-chat/agent-session-journal/journal-store-factory'
import { createDeferredStructuredAgentSessionEventSink } from '../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink'
import { createClaudeJournalTranslator } from './structured/journal/claude-structured-journal-translation'
import {
closeTestJournalHostDatabases,
openTestJournalHostDatabase
} from '../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { closeTestJournalHostDatabases } 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'
import { claudeProviderHandle } from '../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
@@ -50,7 +49,7 @@ afterEach(async () => {
async function itemsFor(frames: Record<string, unknown>[]) {
const journal = await openAgentSessionJournal({
identity: IDENTITY,
database: openTestJournalHostDatabase(root),
database: await openTestAgentSessionDatabase(root),
now: () => 1_700_000_000_000,
mintEpoch: () => 'epoch-1'
})
@@ -61,7 +60,7 @@ async function itemsFor(frames: Record<string, unknown>[]) {
translator.handle({ type: 'message', sessionId: 'orca-session', message: frame })
await deferred.drained()
}
return journal.snapshot().items.map((item) => item.body)
return (await readTestJournalItems(journal)).map((item) => item.body)
}
describe('a Claude informational frame', () => {
+3 -1
View File
@@ -151,7 +151,8 @@ export function journalClaudeResult(
journalClaudeGoal(sink, goal, turnId, observedAt, endedTurnScope)
}
const kind = claudeProviderFrameKind(message)
if (terminalFailure && endedTurnIdentity) {
const terminalShown = terminalFailure !== null && endedTurnIdentity !== null
if (terminalShown && (!stopCheck || isSettledClaudeResultKind(kind))) {
return
}
const alreadyShown = assistantFailures.resultAlreadyShown(message)
@@ -181,6 +182,7 @@ export function journalClaudeResult(
stopCheck && failure
? {
check: stopCheck,
...(terminalShown ? { ran: null } : {}),
stopped: isSettledClaudeResultKind(kind) ? null : { options: { reportsFailure: false } }
}
: undefined
@@ -1,3 +1,5 @@
import { readTestJournalItems } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { openTestAgentSessionDatabase } from '../runtime/agent-session/records/agent-session-record-store-test-harness'
import { execFileSync } from 'node:child_process'
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
@@ -17,10 +19,7 @@ import { openAgentSessionJournal } from '../native-chat/agent-session-journal/jo
import { createDeferredStructuredAgentSessionEventSink } from '../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink'
import { createClaudeJournalTranslator } from './structured/journal/claude-structured-journal-translation'
import { CLAUDE_STRUCTURED_BASE_OPTIONS } from './structured/launch/claude-structured-launch-resolution'
import {
closeTestJournalHostDatabases,
openTestJournalHostDatabase
} from '../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { closeTestJournalHostDatabases } 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'
import { claudeProviderHandle } from '../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
import { providerExecutableMissing } from '../provider-process/provider-executable-missing'
@@ -263,7 +262,7 @@ describe('Claude stream-json connection', () => {
agent: 'claude',
providerHandle: claudeProviderHandle(SESSION_ID, 'leaf-1')
},
database: openTestJournalHostDatabase(join(scenario.cwd, 'journal')),
database: await openTestAgentSessionDatabase(join(scenario.cwd, 'journal')),
now: () => 1_700_000_000_000,
mintEpoch: () => 'epoch-1'
})
@@ -280,7 +279,7 @@ describe('Claude stream-json connection', () => {
await until(() => (settled ? true : null), 'the result frame')
await deferred.drained()
const items = journal.snapshot().items
const items = await readTestJournalItems(journal)
const assistant = items.filter(
(item) => item.body.kind === 'message' && item.body.role === 'assistant'
)
@@ -1,3 +1,4 @@
import { readTestJournalItems } from '../native-chat/agent-session-journal/journal-observation.test-support'
// Real CLI captures verify waiting children have durable cards and child waits do not alter parent status.
import { readFileSync } from 'node:fs'
@@ -7,16 +8,16 @@ import { join } from 'node:path'
import { isDeepStrictEqual } from 'node:util'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { parseAgentJournalItemKey } from '../../shared/agent-session/journal/agent-session-journal-item-key'
import type {
AgentJournalRenderItem,
AgentJournalResolution
import {
AGENT_JOURNAL_THREAD_SCOPE,
type AgentJournalRenderItem,
type AgentJournalResolution
} from '../../shared/agent-session/journal/agent-session-journal-types'
import type { AgentChildWorkEvidence } from '../../shared/agent-status/child-work/agent-status-child-work-evidence'
import type { AgentStatusStructuredSessionSubject } from '../../shared/agent-status/contracts/agent-status-subject'
import { AgentHookServer } from '../agent-hooks/server'
import type { AgentSessionJournal } from '../native-chat/agent-session-journal/journal-store'
import { createTrackedJournalOpener } from '../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { StructuredAgentSessionCurrentWork } from '../native-chat/agent-session-wire/observations/structured-agent-session-current-work'
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'
import { createStructuredAgentSessionLogger } from '../native-chat/agent-session-wire/host/structured-agent-session-logger'
@@ -124,19 +125,28 @@ async function pipeline() {
const viewOf = (providerId: string) =>
published &&
server.getStructuredChildWorkViews(published).find((view) => view.providerId === providerId)
// Read the cards in command order, so each entry sees exactly the journal it published.
const askersRead: Promise<void>[] = []
const record = (subject: AgentStatusStructuredSessionSubject): void => {
published = subject
const cards = pendingCards(journal.snapshot().items)
publishes.push({
const entry: Publish = {
waiting: server
.getStructuredChildWorkViews(subject)
.flatMap((view) =>
view.state === 'waiting' && view.membership === 'live' ? [view.providerId ?? '?'] : []
),
askers: cards.flatMap((card) => (card.agentId ? [card.agentId] : [])),
askers: [],
row: parentRow(server),
unwaited: parentRow(unwaited)
})
}
publishes.push(entry)
askersRead.push(
journal.readInOrder(async (reads) => {
entry.askers = pendingCards(await readTestJournalItems({ reads })).flatMap((card) =>
card.agentId ? [card.agentId] : []
)
})
)
}
const feed = new StructuredAgentSessionStatusFeed({
logger: createStructuredAgentSessionLogger(),
@@ -216,12 +226,16 @@ async function pipeline() {
expect(await deferred.drained()).toEqual({ ok: true })
await new Promise((resolve) => setTimeout(resolve, 0))
}
await Promise.all(askersRead)
}
const hostRecords =
(resolution: Pick<AgentJournalResolution, 'state' | 'selectedOptionId'>, itemId = cardId()) =>
(resolution: Pick<AgentJournalResolution, 'state' | 'selectedOptionId'>, itemId?: string) =>
async (): Promise<void> => {
await deferred.drained()
const card = pendingCards(journal.snapshot().items).find((item) => item.itemId === itemId)
const recorded = itemId ?? (await cardId())
const card = pendingCards(await readTestJournalItems(journal)).find(
(item) => item.itemId === recorded
)
const identity = card && parseAgentJournalItemKey(card.itemId)
if (card?.body.kind !== 'approval' || !identity) {
throw new Error('no pending card to record')
@@ -229,14 +243,11 @@ async function pipeline() {
await journal.appendItem(
identity,
{ ...card.body, resolution: { ...resolution, resolvedBy: 'client-1', resolvedAt: now() } },
{
fence: FENCE,
turnScope: new StructuredAgentSessionCurrentWork(journal, FENCE).turnScope()
}
{ fence: FENCE, turnScope: card.turnScope ?? AGENT_JOURNAL_THREAD_SCOPE }
)
}
const cardId = (): string => {
const card = pendingCards(journal.snapshot().items)[0]
const cardId = async (): Promise<string> => {
const card = pendingCards(await readTestJournalItems(journal))[0]
if (!card) {
throw new Error('no pending card')
}
@@ -314,15 +325,15 @@ async function pipeline() {
type Pipeline = Awaited<ReturnType<typeof pipeline>>
function answer(
async function answer(
run: Pipeline,
optionId: 'allow' | 'deny',
commit = run.hostRecords({ state: 'resolved', selectedOptionId: optionId }),
itemId = run.cardId()
itemId?: string
) {
return run.adapter.answerPrompt({
sessionId: SESSION,
itemId,
itemId: itemId ?? (await run.cardId()),
kind: 'approval',
response: { kind: 'option', optionId },
fence: FENCE,
@@ -341,7 +352,7 @@ describe("a Claude subagent's permission request", () => {
run.deferred.unbind()
run.raise('req-late', 'toolu_late', ASKER)
await new Promise((resolve) => setTimeout(resolve, 0))
run.publishStatus()
await run.publishStatus()
expect(run.publishes.at(-1)?.waiting).toEqual([])
run.deferred.bind(run.target)
await run.settle()
@@ -373,9 +384,10 @@ describe("a Claude subagent's permission request", () => {
run.raise('req-two', 'toolu_two_bash', 'agent-two')
await run.settle()
expect([...(run.publishes.at(-1)?.waiting ?? [])].sort()).toEqual([ASKER, 'agent-two'].sort())
const cardOf = (agentId: string) =>
pendingCards(run.journal.snapshot().items).find((card) => card.agentId === agentId)?.itemId
const first = cardOf(ASKER)
const cardOf = async (agentId: string) =>
pendingCards(await readTestJournalItems(run.journal)).find((card) => card.agentId === agentId)
?.itemId
const first = await cardOf(ASKER)
await answer(
run,
'allow',
@@ -384,7 +396,7 @@ describe("a Claude subagent's permission request", () => {
)
await run.settle()
expect(run.publishes.at(-1)?.waiting).toEqual(['agent-two'])
const second = cardOf('agent-two')
const second = await cardOf('agent-two')
await answer(
run,
'allow',
@@ -1,3 +1,4 @@
import { readTestJournalItems } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -123,14 +124,15 @@ async function acquire(journal: AgentSessionJournal) {
return { translator, bind, settle, exit }
}
const proseRow = (journal: AgentSessionJournal, text: string): AgentJournalRenderItem | undefined =>
journal
.snapshot()
.items.find(
(item) =>
item.body.kind === 'message' &&
item.body.blocks.some((block) => block.type === 'text' && block.text === text)
)
const proseRow = async (
journal: AgentSessionJournal,
text: string
): Promise<AgentJournalRenderItem | undefined> =>
(await readTestJournalItems(journal)).find(
(item) =>
item.body.kind === 'message' &&
item.body.blocks.some((block) => block.type === 'text' && block.text === text)
)
describe('a Claude subagent resumed after its provider restarted', () => {
it('keeps the canonical id its first run was given, so one child stays one child', async () => {
@@ -161,7 +163,7 @@ describe('a Claude subagent resumed after its provider restarted', () => {
first.translator.handle(taskFailed('agent-a', 'toolu_spawn_a'))
first.translator.handle(result('result-a'))
await first.exit()
expect(proseRow(journal, 'A reads the rig recipes')).toMatchObject({ agentId: 'agent-a' })
expect(await proseRow(journal, 'A reads the rig recipes')).toMatchObject({ agentId: 'agent-a' })
// The acquisition's saved answers apply before its sink binds.
const second = await acquire(journal)
@@ -182,18 +184,18 @@ describe('a Claude subagent resumed after its provider restarted', () => {
second.bind()
await second.settle()
expect(proseRow(journal, 'A restarts the receiver')).toMatchObject({
expect(await proseRow(journal, 'A restarts the receiver')).toMatchObject({
agentId: 'agent-a',
providerParentRef: 'toolu_spawn_a',
producerKind: 'agent'
})
expect(proseRow(journal, 'B checks the rig')).toMatchObject({
expect(await proseRow(journal, 'B checks the rig')).toMatchObject({
agentId: 'agent-b',
providerParentRef: 'toolu_spawn_b'
})
expect(proseRow(journal, 'A reads the rig recipes')?.attempt).toBeUndefined()
expect(proseRow(journal, 'A restarts the receiver')?.attempt).toBe(2)
expect(proseRow(journal, 'B checks the rig')?.attempt).toBe(2)
expect((await proseRow(journal, 'A reads the rig recipes'))?.attempt).toBeUndefined()
expect((await proseRow(journal, 'A restarts the receiver'))?.attempt).toBe(2)
expect((await proseRow(journal, 'B checks the rig'))?.attempt).toBe(2)
await second.exit()
})
})
@@ -1,3 +1,4 @@
import { readTestJournalItems } from '../native-chat/agent-session-journal/journal-observation.test-support'
// Shared real disk/journal fixture for catch-up and historical-duration contracts.
import { appendFile, mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
@@ -122,6 +123,7 @@ export async function chat(project = '-work', cwd = '/work') {
})
const warn: Mock<(...args: unknown[]) => void> = vi.fn()
return {
root,
identity,
get journal() {
return journal
@@ -236,8 +238,8 @@ export async function chat(project = '-work', cwd = '/work') {
await deferred.drained()
return covered
},
rows: () =>
journal.snapshot().items.flatMap((item) => {
rows: async () =>
(await readTestJournalItems(journal)).flatMap((item) => {
const { body } = item
if (body.kind === 'message') {
const text = body.blocks.map((block) => (block.type === 'text' ? block.text : ''))
+118 -108
View File
@@ -1,3 +1,8 @@
import {
readTestJournalItems,
readTestJournalSnapshot,
readTestJournalSubmissions
} 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, vi } from 'vitest'
import { createClaudeJournalTranslator } from './structured/journal/claude-structured-journal-translation'
@@ -7,13 +12,13 @@ import {
} from '../native-chat/agent-session-wire/rewind/structured-rewind-cut'
import { agentJournalItemKey } from '../../shared/agent-session/journal/agent-session-journal-item-key'
import { readAgentSessionHistory } from '../native-chat/agent-session-wire/publication/history/agent-session-history-page'
import { projectJournalBatch } from '../native-chat/agent-session-wire/publication/history/agent-session-journal-batch'
import {
replacePage,
mergeItems
} from '../../shared/agent-session/view/structured-agent-session-history-state'
import { AgentJournalRenderItemSchema } from '../../shared/agent-session/journal/agent-session-journal-schemas'
import { agentSessionSendBodyFingerprint } from '../../shared/agent-session/requests/structured-agent-session-send-mutation'
import { completeTestJournalRewind } from '../native-chat/agent-session-wire/rewind/structured-rewind-completion.test-support'
import { dispatchClaudeTurn } from './structured/claude-structured-dispatch'
import { claudePromptUuidForSubmission } from './structured/claude-structured-session-id'
import {
@@ -57,7 +62,7 @@ describe('Claude chat catch-up', () => {
}
})
)
const before = c.journal.snapshot()
const before = await readTestJournalSnapshot(c.journal)
try {
const covered = await withStructuredHistoryInvocation(
owner,
@@ -72,9 +77,9 @@ describe('Claude chat catch-up', () => {
}
)
expect(covered).toBe(false)
expect(c.journal.snapshot()).toEqual(before)
expect(await readTestJournalSnapshot(c.journal)).toEqual(before)
expect(await c.open('a1')).toBe(true)
expect(c.journal.snapshot().items.length).toBeGreaterThan(before.items.length)
expect((await readTestJournalItems(c.journal)).length).toBeGreaterThan(before.items.length)
} finally {
translator.dispose()
}
@@ -104,7 +109,7 @@ describe('Claude chat catch-up', () => {
state: 'unknown',
reason: 'provider_closed_before_acknowledgement'
})
const before = c.journal.snapshot()
const before = await readTestJournalSnapshot(c.journal)
await c.journal.close()
const reopened = await c.replay()
c.deferred.bind({ journal: reopened, fence: 1, publish: () => {} })
@@ -132,26 +137,26 @@ describe('Claude chat catch-up', () => {
...(outside ? [prompt('external', 'recovered', 'same prompt', 5)] : [])
])
expect(await c.open('a1')).toBe(true)
const after = reopened.snapshot()
const after = await readTestJournalSnapshot(reopened)
expect(
after.items
.filter((item) => item.body.kind !== 'turn')
.map((item) => (item.body.kind === 'message' ? item.body.role : item.body.kind))
).toEqual(['user', 'assistant', 'user', 'status', 'assistant', ...(outside ? ['user'] : [])])
expect(after.submissions).toEqual(before.submissions)
expect(reopened.receiptFor(clientMessageId)).toBeNull()
expect(await reopened.reads.receiptFor(clientMessageId)).toBeNull()
const key = agentJournalItemKey({ provider: 'claude', sessionId: SID, uuid })
expect(reopened.canonicalItemId(key)).toBe(key)
expect(await reopened.reads.canonicalItemId(key)).toBe(key)
expect(
structuredRewindCut(reopened, {
await structuredRewindCut(reopened, {
itemId: agentJournalItemKey({ provider: 'orca', clientMessageId }),
provider: 'claude',
nativeId: SID
})
).toBeNull()
expect(await c.open('a1')).toBe(true)
expect(reopened.snapshot()).toEqual(after)
expect((await c.replay()).snapshot()).toEqual(after)
expect(await readTestJournalSnapshot(reopened)).toEqual(after)
expect(await readTestJournalSnapshot(await c.replay())).toEqual(after)
expect(c.warn).not.toHaveBeenCalled()
}
)
@@ -178,7 +183,7 @@ describe('Claude chat catch-up', () => {
fence: 1,
turnScope: AGENT_JOURNAL_THREAD_SCOPE
})
const submissions = c.journal.snapshot().submissions
const submissions = await readTestJournalSubmissions(c.journal)
const cursor = c.journal.cursor()
await c.write([
...chatTurn,
@@ -196,37 +201,42 @@ describe('Claude chat catch-up', () => {
])
const key = agentJournalItemKey({ provider: 'claude', sessionId: SID, uuid: 'cmd-1' })
expect(await c.open('a1')).toBe(true)
const held = c.journal.snapshot().items
expect(c.rows().filter((row) => row === 'user:follow up')).toHaveLength(1)
expect(c.journal.item(key)?.turnScope).toEqual({ kind: 'unknown' })
expect(isStructuredRewindTarget(c.journal, key)).toBe(false)
const held = await readTestJournalItems(c.journal)
expect((await c.rows()).filter((row) => row === 'user:follow up')).toHaveLength(1)
expect((await c.journal.reads.item(key))?.turnScope).toEqual({ kind: 'unknown' })
expect(await isStructuredRewindTarget(c.journal, key)).toBe(false)
expect(held.filter((row) => row.body.kind === 'turn')).toHaveLength(1)
expect(await c.open('a1')).toBe(true)
expect(c.journal.snapshot().items).toEqual(held)
const beforeReplay = c.journal.item(key)
expect(await readTestJournalItems(c.journal)).toEqual(held)
const beforeReplay = await c.journal.reads.item(key)
const reopened = await c.replay()
expect(reopened.item(key)).toEqual(beforeReplay)
expect(isStructuredRewindTarget(reopened, key)).toBe(false)
const page = readAgentSessionHistory(reopened, { sessionId: SESSION, direction: 'tail' })
const delta = reopened.readSince(cursor)
expect(await reopened.reads.item(key)).toEqual(beforeReplay)
expect(await isStructuredRewindTarget(reopened, key)).toBe(false)
const page = await readAgentSessionHistory(reopened, {
sessionId: SESSION,
direction: 'tail'
})
const delta = await readAgentSessionHistory(reopened, {
sessionId: SESSION,
direction: 'after',
cursor
})
if (!page.ok || !delta.ok) {
throw new Error('history unavailable')
}
const batch = projectJournalBatch({
rows: delta.rows,
snapshot: reopened.snapshot(),
afterSequence: cursor.sequence
})
if (!batch.ok) {
throw new Error('batch unavailable')
}
const snapshotClient = replacePage(page.page, 1)
const liveClient = mergeItems([], batch.batch.items, [])
expect(snapshotClient.items.find((item) => item.itemId === key)).toEqual(c.journal.item(key))
expect(liveClient.find((item) => item.itemId === key)).toEqual(c.journal.item(key))
const liveClient = mergeItems([], delta.page.items, [])
expect(snapshotClient.items.find((item) => item.itemId === key)).toEqual(
await c.journal.reads.item(key)
)
expect(liveClient.find((item) => item.itemId === key)).toEqual(
await c.journal.reads.item(key)
)
// The open wire schema used by older readers preserves the new scope and message content.
expect(
AgentJournalRenderItemSchema.parse(JSON.parse(JSON.stringify(c.journal.item(key))))
AgentJournalRenderItemSchema.parse(
JSON.parse(JSON.stringify(await c.journal.reads.item(key)))
)
).toMatchObject({ turnScope: { kind: 'unknown' }, body: { role: 'user' } })
await c.write([
@@ -235,26 +245,25 @@ describe('Claude chat catch-up', () => {
reply('a3', 'p3', 'msg_c', { type: 'text', text: 'next answer' }, 6)
])
expect(await c.open('a1')).toBe(true)
const items = c.journal.snapshot().items
expect(c.journal.item(key)).toEqual(held.find((row) => row.itemId === key))
const items = await readTestJournalItems(c.journal)
expect(await c.journal.reads.item(key)).toEqual(held.find((row) => row.itemId === key))
const resumed = items.find((row) => row.body.kind === 'turn' && row.body.turnId === 'a2')
expect(resumed?.body).toMatchObject({ userItemId: resumed?.itemId })
const opener = agentJournalItemKey({ provider: 'claude', sessionId: SID, uuid: 'p3' })
expect(isStructuredRewindTarget(c.journal, opener)).toBe(true)
expect(c.journal.snapshot().submissions).toEqual(submissions)
expect(c.rows().filter((row) => row === 'user:follow up')).toHaveLength(1)
expect(await isStructuredRewindTarget(c.journal, opener)).toBe(true)
expect(await readTestJournalSubmissions(c.journal)).toEqual(submissions)
expect((await c.rows()).filter((row) => row === 'user:follow up')).toHaveLength(1)
expect(c.warn).not.toHaveBeenCalled()
const cut = structuredRewindCut(c.journal, {
itemId: opener,
provider: 'claude',
nativeId: SID
})
if (!cut) {
throw new Error('ordinary rewind unavailable')
}
await c.journal.replaceEpochItems('handle_forked', 1, cut.kept)
expect(c.journal.item(key)?.turnScope).toEqual({ kind: 'unknown' })
expect((await c.replay()).item(key)?.turnScope).toEqual({ kind: 'unknown' })
expect(
await structuredRewindCut(c.journal, {
itemId: opener,
provider: 'claude',
nativeId: SID
})
).not.toBeNull()
await completeTestJournalRewind(c.root, c.journal, opener)
expect((await c.journal.reads.item(key))?.turnScope).toEqual({ kind: 'unknown' })
expect((await (await c.replay()).reads.item(key))?.turnScope).toEqual({ kind: 'unknown' })
}
)
@@ -268,7 +277,7 @@ describe('Claude chat catch-up', () => {
const c = await chat(project, cwd)
await c.write([...chatTurn, ...outsideTurn])
expect(await c.open('a1')).toBe(true)
expect(c.rows()).toContain('assistant:answer 2')
expect(await c.rows()).toContain('assistant:answer 2')
}
)
@@ -276,16 +285,16 @@ describe('Claude chat catch-up', () => {
// An update killed the chat after Claude saved its reply but before the prompt's echo.
const c = await chat()
await c.live({ interrupted: true })
const saved = new Set(c.journal.snapshot().items.map((item) => item.itemId))
const saved = new Set((await readTestJournalItems(c.journal)).map((item) => item.itemId))
await c.write([
prompt('leaf', null, 'earlier request', 0),
prompt('unechoed', 'leaf', 'ask 1', 1),
reply('a1', 'unechoed', 'msg_a', { type: 'text', text: 'answer 1' }, 2)
])
expect(await c.open('leaf')).toBe(true)
const imported = c.journal
.snapshot()
.items.filter((item) => item.body.kind === 'turn' && !saved.has(item.itemId))
const imported = (await readTestJournalItems(c.journal)).filter(
(item) => item.body.kind === 'turn' && !saved.has(item.itemId)
)
expect(imported).toHaveLength(1)
// It ends where it opened: in the thread, not inside the saved turn its reply matched.
expect(imported[0]).toMatchObject({
@@ -302,7 +311,7 @@ describe('Claude chat catch-up', () => {
text: 'unfinished, differently normalized',
thinking: 'retained thought'
})
const before = c.journal.snapshot().items
const before = await readTestJournalItems(c.journal)
const tool = before.find((item) => item.body.kind === 'tool-call')!
const text = before.find(
(item) => item.body.kind === 'message' && item.body.role === 'assistant'
@@ -318,8 +327,8 @@ describe('Claude chat catch-up', () => {
{ ...outsideTurn[4], parentUuid: 'thinking-final' }
])
expect(await c.open('boundary')).toBe(true)
const afterText = c.journal.item(text.itemId)!
const afterTool = c.journal.item(tool.itemId)!
const afterText = (await c.journal.reads.item(text.itemId))!
const afterTool = (await c.journal.reads.item(tool.itemId))!
expect(afterText).toMatchObject({
sequence: text.sequence,
observedAt: text.observedAt,
@@ -337,12 +346,12 @@ describe('Claude chat catch-up', () => {
output: { head: 'hello' }
}
})
expect(c.journal.item(turn.itemId)).toEqual(turn)
expect(c.journal.item(thinking.itemId)).toEqual(thinking)
expect(c.rows().some((row) => row.startsWith('(continued-outside'))).toBe(false)
const completed = c.journal.snapshot().items
expect(await c.journal.reads.item(turn.itemId)).toEqual(turn)
expect(await c.journal.reads.item(thinking.itemId)).toEqual(thinking)
expect((await c.rows()).some((row) => row.startsWith('(continued-outside'))).toBe(false)
const completed = await readTestJournalItems(c.journal)
expect(await c.open('boundary')).toBe(true)
expect(c.journal.snapshot().items).toEqual(completed)
expect(await readTestJournalItems(c.journal)).toEqual(completed)
})
it('does not place historical lifecycle after live work that arrives during its read', async () => {
@@ -367,8 +376,10 @@ describe('Claude chat catch-up', () => {
return tail
})
expect(await c.open('a1')).toBe(false)
expect(c.rows()).not.toContain('user:ask 2')
const running = c.journal.snapshot().items.findLast((item) => item.body.kind === 'turn')
expect(await c.rows()).not.toContain('user:ask 2')
const running = (await readTestJournalItems(c.journal)).findLast(
(item) => item.body.kind === 'turn'
)
expect(running?.body).toMatchObject({ state: 'running' })
live.dispose()
})
@@ -377,11 +388,11 @@ describe('Claude chat catch-up', () => {
const c = await chat()
await c.live()
await c.write(chatTurn)
const shown = c.rows()
const shown = await c.rows()
expect(shown).toEqual(['user:ask 1', 'assistant:answer 1'])
await c.open('a1')
expect(c.rows()).toEqual(shown)
expect(await c.rows()).toEqual(shown)
await c.write(outsideTurn)
await c.open('a1')
@@ -392,13 +403,13 @@ describe('Claude chat catch-up', () => {
'tool:Read:completed',
'assistant:answer 2'
]
expect(c.rows()).toEqual(caughtUp)
expect(c.journal.snapshot().submissions).toHaveLength(1)
expect(await c.rows()).toEqual(caughtUp)
expect(await readTestJournalSubmissions(c.journal)).toHaveLength(1)
// Opening again, or from a leaf left before the chat's own streamed turn by a crash, adds nothing.
await c.open('a1')
await c.open('p1')
expect(c.rows()).toEqual(caughtUp)
expect(await c.rows()).toEqual(caughtUp)
expect(c.warn).not.toHaveBeenCalled()
})
@@ -407,7 +418,7 @@ describe('Claude chat catch-up', () => {
items = 10_000
const c = await chat()
await c.live()
const expected = [...c.rows(), '(continued-outside)']
const expected = [...(await c.rows()), '(continued-outside)']
const rows: Record<string, unknown>[] = [
reply('saved-leaf', 'earlier-prompt', 'msg_saved', { type: 'text', text: 'earlier' }, 0),
prompt('p1', 'saved-leaf', 'ask 1', 1),
@@ -427,22 +438,25 @@ describe('Claude chat catch-up', () => {
expected.push(`user:ask ${n}`, `assistant:answer ${n}`)
}
const alias = agentJournalItemKey({ provider: 'claude', sessionId: SID, uuid: 'p1' })
const canonical = c.journal.canonicalItemId(alias)
const canonical = await c.journal.reads.canonicalItemId(alias)
expect(canonical).not.toBe(alias)
const submissions = c.journal.snapshot().submissions
const submissions = await readTestJournalSubmissions(c.journal)
await c.write(rows)
expect(await c.open('saved-leaf')).toBe(true)
expect(c.rows()).toEqual(expected)
expect(c.journal.snapshot().submissions).toEqual(submissions)
expect(await c.rows()).toEqual(expected)
expect(await readTestJournalSubmissions(c.journal)).toEqual(submissions)
await c.journal.appendLifecycleBatch({
settlementId: 'perf-padding',
fence: 1,
mutations: Array.from({ length: items - c.journal.snapshot().items.length }, (_, n) => ({
kind: 'item' as const,
identity: { provider: 'orca' as const, clientMessageId: `padding${n}` },
body: { kind: 'message' as const, role: 'user' as const, blocks: [] },
turnScope: { kind: 'thread' as const }
}))
mutations: Array.from(
{ length: items - (await readTestJournalItems(c.journal)).length },
(_, n) => ({
kind: 'item' as const,
identity: { provider: 'orca' as const, clientMessageId: `padding${n}` },
body: { kind: 'message' as const, role: 'user' as const, blocks: [] },
turnScope: { kind: 'thread' as const }
})
)
})
for (let n = 0; n < prompts; n++) {
const key = agentJournalItemKey({ provider: 'orca', clientMessageId: `padding${n}` })
@@ -458,12 +472,12 @@ describe('Claude chat catch-up', () => {
}
])
}
const before = c.journal.snapshot()
const before = await readTestJournalSnapshot(c.journal)
const itemCount = before.items.length
expect(itemCount).toBe(items)
expect(await c.open('saved-leaf')).toBe(true)
expect(c.journal.snapshot()).toEqual(before)
expect(c.journal.canonicalItemId(alias)).toBe(canonical)
expect(await readTestJournalSnapshot(c.journal)).toEqual(before)
expect(await c.journal.reads.canonicalItemId(alias)).toBe(canonical)
expect(c.warn).not.toHaveBeenCalled()
})
@@ -477,11 +491,9 @@ describe('Claude chat catch-up', () => {
handoverRecorded: true,
fence: 1
})
const scope = c.journal
.snapshot()
.items.find(
(item) => item.body.kind === 'message' && item.body.role === 'assistant'
)?.turnScope
const scope = (await readTestJournalItems(c.journal)).find(
(item) => item.body.kind === 'message' && item.body.role === 'assistant'
)?.turnScope
if (!scope || scope.kind !== 'turn') {
throw new Error('missing native turn')
}
@@ -497,7 +509,7 @@ describe('Claude chat catch-up', () => {
providerIdentity: { provider: 'claude', sessionId: SID, uuid: 'cmd-1' },
fence: 1
})
const before = c.journal.snapshot()
const before = await readTestJournalSnapshot(c.journal)
const end = (uuid: string, parentUuid: string) => ({
type: 'system',
subtype: 'turn_duration',
@@ -539,7 +551,7 @@ describe('Claude chat catch-up', () => {
reply('next-answer', 'next', 'msg_next', { type: 'text', text: 'next answer' }, 6)
])
expect(await c.open('p1')).toBe(true)
expect(c.rows()).toEqual([
expect(await c.rows()).toEqual([
'user:ask 1',
'assistant:answer 1',
'user:follow up',
@@ -550,29 +562,27 @@ describe('Claude chat catch-up', () => {
'user:next request',
'assistant:next answer'
])
const imported = c.journal.snapshot().items
const imported = await readTestJournalItems(c.journal)
expect(await c.open('p1')).toBe(true)
expect(c.journal.snapshot().items).toEqual(imported)
expect(c.journal.snapshot().submissions).toEqual(before.submissions)
expect(await readTestJournalItems(c.journal)).toEqual(imported)
expect(await readTestJournalSubmissions(c.journal)).toEqual(before.submissions)
for (const item of before.items) {
expect(c.journal.item(item.itemId)).toEqual(item)
expect(await c.journal.reads.item(item.itemId)).toEqual(item)
}
const unknown = agentJournalItemKey({
provider: 'claude',
sessionId: SID,
uuid: 'unknown-input'
})
expect(c.journal.item(unknown)?.turnScope).toEqual({ kind: 'unknown' })
expect(isStructuredRewindTarget(c.journal, unknown)).toBe(false)
const answer = c.journal
.snapshot()
.items.find(
(item) =>
item.body.kind === 'message' &&
item.body.role === 'assistant' &&
item.itemId !==
agentJournalItemKey({ provider: 'claude', sessionId: SID, uuid: 'block:msg_a:0' })
)
expect((await c.journal.reads.item(unknown))?.turnScope).toEqual({ kind: 'unknown' })
expect(await isStructuredRewindTarget(c.journal, unknown)).toBe(false)
const answer = (await readTestJournalItems(c.journal)).find(
(item) =>
item.body.kind === 'message' &&
item.body.role === 'assistant' &&
item.itemId !==
agentJournalItemKey({ provider: 'claude', sessionId: SID, uuid: 'block:msg_a:0' })
)
expect(answer?.turnScope).toEqual(scope)
const turns = imported.filter((item) => item.body.kind === 'turn')
expect(turns).toHaveLength(3)
@@ -595,7 +605,7 @@ describe('Claude chat catch-up', () => {
...bookkeeping
])
await c.open('a1')
expect(c.rows().slice(2)).toEqual([
expect((await c.rows()).slice(2)).toEqual([
'(continued-outside)',
'user:ask long',
`assistant:${answer}`
@@ -607,7 +617,7 @@ describe('Claude chat catch-up', () => {
await c.live()
await c.write([...chatTurn, ...outsideTurn])
await c.open('missing')
expect(c.rows()).toEqual(['user:ask 1', 'assistant:answer 1'])
expect(await c.rows()).toEqual(['user:ask 1', 'assistant:answer 1'])
expect(c.warn).toHaveBeenCalledWith(
'Claude chat did not catch up on turns made outside it',
expect.objectContaining({ reason: 'leaf-not-in-tail' })
@@ -1,3 +1,4 @@
import { readTestJournalSnapshot } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { describe, expect, it } from 'vitest'
import { agentJournalItemKey } from '../../shared/agent-session/journal/agent-session-journal-item-key'
import { selectStructuredAgentSettledTurns } from '../../shared/agent-session/view/structured-agent-session-turn-timing'
@@ -244,10 +245,10 @@ describe('Claude historical cycle duration', () => {
it('ends captured work at its boundary before later idle, next prompt and exit', async () => {
const c = await chat()
await c.live()
const before = c.journal.snapshot()
const before = await readTestJournalSnapshot(c.journal)
const handle = structuredClone(c.identity.providerHandle)
const alias = key('p1')
const canonical = c.journal.canonicalItemId(alias)
const canonical = await c.journal.reads.canonicalItemId(alias)
await c.write([
...chatTurn,
...capturedDurationRows.map((row, i) => ({
@@ -257,7 +258,7 @@ describe('Claude historical cycle duration', () => {
}))
])
expect(await c.open('a1')).toBe(true)
const snapshot = c.journal.snapshot()
const snapshot = await readTestJournalSnapshot(c.journal)
const turns = snapshot.items.filter((item) => item.body.kind === 'turn')
const first = turns.find(
(item) =>
@@ -293,7 +294,7 @@ describe('Claude historical cycle duration', () => {
expect(turn.body).not.toHaveProperty('outcome')
expect(turn.body).not.toHaveProperty('durationMs')
}
const queuedItem = c.journal.item(key('080f8253-c2a1-41e1-b5e6-b6083f15c62d'))!
const queuedItem = (await c.journal.reads.item(key('080f8253-c2a1-41e1-b5e6-b6083f15c62d')))!
expect(queuedItem).toMatchObject({
observedAt: 1792711941679,
turnScope: {
@@ -306,11 +307,11 @@ describe('Claude historical cycle duration', () => {
true
)
for (const item of before.items) {
expect(c.journal.item(item.itemId)).toEqual(item)
expect(await c.journal.reads.item(item.itemId)).toEqual(item)
}
expect(snapshot.submissions).toEqual(before.submissions)
expect(c.identity.providerHandle).toEqual(handle)
expect(c.journal.canonicalItemId(alias)).toBe(canonical)
expect(await c.journal.reads.canonicalItemId(alias)).toBe(canonical)
expect(c.rows()).toContain('user:<local-command-stdout>See ya!</local-command-stdout>')
const exits = turns.filter(
(item) =>
@@ -323,7 +324,7 @@ describe('Claude historical cycle duration', () => {
expect(item.body).not.toHaveProperty('completedAt')
}
expect(await c.open('a1')).toBe(true)
expect(c.journal.snapshot()).toEqual(snapshot)
expect(await readTestJournalSnapshot(c.journal)).toEqual(snapshot)
expect(c.warn).not.toHaveBeenCalled()
})
})
@@ -1,3 +1,4 @@
import { readTestJournalItems } from '../../native-chat/agent-session-journal/journal-observation.test-support'
import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -207,15 +208,11 @@ describe('Claude durable resume point at turn end', () => {
})
await claudeStartupSettled(adapter, 'session-1')
expect(
journal
.snapshot()
.items.some(
(item) =>
item.body.kind === 'message' &&
item.body.blocks.some(
(block) => block.type === 'text' && block.text === 'outside reply'
)
)
(await readTestJournalItems(journal)).some(
(item) =>
item.body.kind === 'message' &&
item.body.blocks.some((block) => block.type === 'text' && block.text === 'outside reply')
)
).toBe(true)
expect(store.record.providerHandleChain.at(-1)?.handle).toMatchObject({
resumeCursor: 'resumed-at'
@@ -1,3 +1,5 @@
import { readTestJournalItems } from '../../../native-chat/agent-session-journal/journal-observation.test-support'
import { openTestAgentSessionDatabase } from '../../../runtime/agent-session/records/agent-session-record-store-test-harness'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -11,10 +13,7 @@ import { openAgentSessionJournal } from '../../../native-chat/agent-session-jour
import { createDeferredStructuredAgentSessionEventSink } from '../../../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink'
import type { ClaudePendingPrompt } from '../../claude-prompt-registry'
import { createClaudeJournalTranslator } from './claude-structured-journal-translation'
import {
closeTestJournalHostDatabases,
openTestJournalHostDatabase
} from '../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { closeTestJournalHostDatabases } 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'
import { claudeProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
@@ -143,7 +142,7 @@ describe('Claude structured journal translation', () => {
it('journals a count-to-200 stream as one assistant item carrying the complete reply', async () => {
const journal = await openAgentSessionJournal({
identity: JOURNAL_IDENTITY,
database: openTestJournalHostDatabase(journalRoot),
database: await openTestAgentSessionDatabase(journalRoot),
now: () => 1_700_000_000_000,
mintEpoch: () => 'epoch-1'
})
@@ -188,7 +187,7 @@ describe('Claude structured journal translation', () => {
}
await deferred.drained()
const items: AgentJournalRenderItem[] = journal.snapshot().items
const items: AgentJournalRenderItem[] = await readTestJournalItems(journal)
const assistant = assistantMessages(items)
expect(assistant.map((item) => item.itemId)).toEqual([
'claude:claude-session:block%3Amsg_count%3A0'
@@ -204,7 +203,7 @@ describe('Claude structured journal translation', () => {
it('restores a cancelled prompt as terminal history after reopening the journal', async () => {
const journal = await openAgentSessionJournal({
identity: JOURNAL_IDENTITY,
database: openTestJournalHostDatabase(journalRoot),
database: await openTestAgentSessionDatabase(journalRoot),
now: () => 1_700_000_000_000,
mintEpoch: () => 'epoch-1'
})
@@ -233,11 +232,11 @@ describe('Claude structured journal translation', () => {
const reopened = await openAgentSessionJournal({
identity: JOURNAL_IDENTITY,
database: openTestJournalHostDatabase(journalRoot),
database: await openTestAgentSessionDatabase(journalRoot),
now: () => 1_700_000_000_000,
mintEpoch: () => 'epoch-2'
})
expect(reopened.snapshot().items).toEqual([
expect(await readTestJournalItems(reopened)).toEqual([
expect.objectContaining({
body: expect.objectContaining({
kind: 'approval',
@@ -1,3 +1,5 @@
import { readTestJournalItems } from '../../../native-chat/agent-session-journal/journal-observation.test-support'
import { openTestAgentSessionDatabase } from '../../../runtime/agent-session/records/agent-session-record-store-test-harness'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -8,10 +10,7 @@ import { openAgentSessionJournal } from '../../../native-chat/agent-session-jour
import { createDeferredStructuredAgentSessionEventSink } from '../../../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink'
import { createClaudeJournalTranslator } from '../journal/claude-structured-journal-translation'
import type { ClaudeStructuredSessionEvent } from '../claude-structured-session-state'
import {
closeTestJournalHostDatabases,
openTestJournalHostDatabase
} from '../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { closeTestJournalHostDatabases } 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'
import { claudeProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
@@ -129,7 +128,7 @@ describe('Claude provider fallback', () => {
it('keeps provider-fallback rows distinct across acquisitions', async () => {
const journal = await openAgentSessionJournal({
identity: IDENTITY,
database: openTestJournalHostDatabase(root),
database: await openTestAgentSessionDatabase(root),
now: () => 1_700_000_000_000,
mintEpoch: () => 'epoch-1'
})
@@ -150,17 +149,81 @@ describe('Claude provider fallback', () => {
)
await deferred.drained()
const fallbackRows = journal
.snapshot()
.items.filter(
(item) =>
item.body.kind === 'status' &&
item.body.providerFrame?.kind === 'message:assistant:content:future_event'
)
const fallbackRows = (await readTestJournalItems(journal)).filter(
(item) =>
item.body.kind === 'status' &&
item.body.providerFrame?.kind === 'message:assistant:content:future_event'
)
expect(fallbackRows).toHaveLength(2)
expect(fallbackRows.map(statusText)).toEqual(['first', 'second'])
})
it.each([false, true])(
'keeps unknown quota result disclosure only for a stopped turn (stopped: %s)',
async (stopped) => {
const journal = await openAgentSessionJournal({
identity: IDENTITY,
database: await openTestAgentSessionDatabase(root),
now: () => 1_000
})
const deferred = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging())
deferred.bind({ journal, fence: 1, publish: () => {} })
const translator = createClaudeJournalTranslator({ sink: deferred.sink })
const quota = message('assistant', 'quota', [{ type: 'text', text: 'Usage exhausted' }])
if (quota.type !== 'message') {
throw new Error('expected provider message')
}
translator.handle({
...quota,
observedAt: 100,
message: { ...quota.message, api_error: 'usage_limit_reached' }
})
await deferred.drained()
if (stopped) {
await journal.appendStopEvent({ reason: 'user-stop', turnId: 'quota' }, 1)
}
translator.handle({
type: 'message',
sessionId: 'orca-session',
observedAt: 2_000,
message: {
type: 'result',
subtype: 'future_error',
is_error: true,
session_id: 'provider-1',
uuid: 'quota-result',
result: 'Usage exhausted',
terminal_reason: 'api_error'
}
})
await deferred.drained()
const items = await readTestJournalItems(journal)
const disclosure = items.filter(
(item) =>
item.body.kind === 'status' &&
item.body.providerFrame?.kind === 'message:result:future_error'
)
expect(disclosure).toHaveLength(stopped ? 1 : 0)
if (stopped) {
expect(disclosure[0]?.body).not.toHaveProperty('tone')
expect(disclosure[0]?.body).not.toHaveProperty('failure')
}
expect(items.find((item) => item.body.kind === 'turn')?.body).toMatchObject({
kind: 'turn',
state: stopped ? 'interrupted' : 'completed',
...(stopped ? {} : { outcome: 'failure' })
})
deferred.close()
await journal.close()
const reopened = await openAgentSessionJournal({
identity: IDENTITY,
database: await openTestAgentSessionDatabase(root)
})
expect(await readTestJournalItems(reopened)).toEqual(items)
await reopened.close()
}
)
})
function statusText(row: { body: AgentJournalItemBody }): string {
@@ -144,6 +144,7 @@ export function createClaudeProviderFrameFallback(
/** A Stop at the write position records `stopped` instead; null writes nothing. */
stopReading?: {
check: ClaudeStopCheck
ran?: null
stopped: { options?: UnhandledProviderFrameJournalItemOptions } | null
}
) => boolean
@@ -215,7 +216,7 @@ export function createClaudeProviderFrameFallback(
)
const stopped = plain ? { identity, body: plain.body } : null
const readings = {
ran: { kind: 'literal' as const, identity, body },
ran: stopReading.ran === null ? null : { kind: 'literal' as const, identity, body },
stopped: stopped && { kind: 'literal' as const, ...stopped },
reservedBytes: estimateStructuredAgentSessionItemBytes(identity, body) * 2
}
@@ -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')
}
@@ -1,9 +1,13 @@
import { readTestJournalItems } from '../../../native-chat/agent-session-journal/journal-observation.test-support'
// Published retry rows count as activity, keeping reconnecting sessions alive.
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from 'vitest'
import { openTestAgentSessionRecordStore } from '../../../runtime/agent-session/records/agent-session-record-store-test-harness'
import {
openTestAgentSessionRecordStore,
openTestAgentSessionDatabase
} from '../../../runtime/agent-session/records/agent-session-record-store-test-harness'
import type { StructuredAgentSessionAdapter } from '../../../native-chat/agent-session-wire/providers/structured-agent-session-adapter'
import type { StructuredAgentSessionEventSink } from '../../../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink'
import { StructuredAgentSessionHost } from '../../../native-chat/agent-session-wire/host/structured-agent-session-host'
@@ -16,10 +20,7 @@ import {
resetHostTestOperationIds
} from '../../../native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture'
import { createCodexJournalTranslator } from './codex-structured-journal-translation'
import {
closeTestJournalHostDatabases,
openTestJournalHostDatabase
} from '../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { closeTestJournalHostDatabases } from '../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { createStructuredAgentSessionLogger } from '../../../native-chat/agent-session-wire/host/structured-agent-session-logger'
import { codexProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
import { NO_STRUCTURED_AGENTS } from '../../../native-chat/agent-session-wire/providers/testing/structured-agent-session-adapter-router.test-fixture'
@@ -82,7 +83,7 @@ beforeEach(async () => {
logger: createStructuredAgentSessionLogger(),
store,
adapter,
journalDatabase: openTestJournalHostDatabase(root),
journalDatabase: await openTestAgentSessionDatabase(root),
claimKeyId: 'key-1',
mintSpawnToken: () => 'spawn-1',
idleSweep: {
@@ -125,7 +126,7 @@ describe('a Codex reconnecting a dropped stream', () => {
expect(host.hasSession(SESSION)).toBe(true)
}
const items = host['sessions'].get(SESSION)?.journal.snapshot().items ?? []
const items = await readTestJournalItems(host['sessions'].get(SESSION)!.journal)
const retryRows = items.filter(
(item) => item.body.kind === 'status' && item.body.failure?.kind === 'providerRetrying'
)
@@ -1,3 +1,4 @@
import { readTestJournalItems } from '../../../native-chat/agent-session-journal/journal-observation.test-support'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -112,13 +113,12 @@ async function session() {
on('thread/tokenUsage/updated', { turnId, tokenUsage }, threadId),
contextUsage: async () => {
await expect(deferred.drained()).resolves.toEqual({ ok: true })
return selectStructuredAgentContextUsage(journal.snapshot().items)
return selectStructuredAgentContextUsage(await readTestJournalItems(journal))
},
turnRecord: async (turnId: string) => {
await expect(deferred.drained()).resolves.toEqual({ ok: true })
return journal
.snapshot()
.items.map((item) => readAgentJournalTurn(item.body))
return (await readTestJournalItems(journal))
.map((item) => readAgentJournalTurn(item.body))
.find((turn) => turn?.turnId === turnId)
}
}
@@ -1,3 +1,8 @@
import {
readTestJournalItems,
readTestJournalSnapshot,
readTestJournalSubmissions
} from '../../../native-chat/agent-session-journal/journal-observation.test-support'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -22,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 {
@@ -51,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'
@@ -64,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
}
}
@@ -149,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'],
@@ -157,7 +186,7 @@ it('settles a refused Codex exit through the host in one commit, then permits a
endedCalls: 0,
send: 'pending'
})
const before = journal.snapshot()
const before = await readTestJournalSnapshot(journal)
const commits = vi.fn()
journal.observeCommits(commits)
const admission = vi.spyOn(translator, 'handle')
@@ -210,15 +239,8 @@ it('settles a refused Codex exit through the host in one commit, then permits a
expect(events).toHaveLength(1)
await expect(deferred.drained()).resolves.toEqual({ ok: true })
expect(onFailed).not.toHaveBeenCalled()
expect(journal.snapshot()).toEqual(before)
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')
@@ -228,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(),
@@ -241,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'],
@@ -263,12 +287,12 @@ it('settles a refused Codex exit through the host in one commit, then permits a
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'again' }] }
})
).resolves.toMatchObject({ sequence })
const settled = journal.snapshot()
const submissions = journal.submissions()
const settled = await readTestJournalSnapshot(journal)
const submissions = await readTestJournalSubmissions(journal)
await journal.close()
await closeTestJournalHostDatabase(root)
const reopened = await journals.open(options)
expect(reopened.snapshot()).toEqual(settled)
expect(reopened.submissions()).toEqual(submissions)
expect(await readTestJournalSnapshot(reopened)).toEqual(settled)
expect(await readTestJournalSubmissions(reopened)).toEqual(submissions)
deferred.close()
})
@@ -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 {
@@ -1,3 +1,4 @@
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 { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
@@ -57,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,
@@ -66,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 {
@@ -82,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)
}
}
@@ -100,7 +101,7 @@ describe('codex goal accounting revisions', () => {
goals.handle({ threadId: THREAD, method: 'thread/goal/updated', params: goalFrame() })
await drained()
const [, created] = journal.snapshot().items
const [, created] = await readTestJournalItems(journal)
await journal.appendItem(
{ provider: 'orca', clientMessageId: 'later' },
{ kind: 'status', text: 'Something after the goal' },
@@ -114,8 +115,8 @@ describe('codex goal accounting revisions', () => {
params: goalFrame({ timeUsedSeconds: 10, updatedAt: 1789067998 })
})
await drained()
expect(journal.snapshot().items[1]?.revision).toBe(created?.revision)
expect(published).toEqual([])
expect((await readTestJournalItems(journal))[1]?.revision).toBe(created?.revision)
expect(await published()).toEqual([])
goals.handle({
threadId: THREAD,
@@ -124,7 +125,7 @@ describe('codex goal accounting revisions', () => {
})
await drained()
const items = journal.snapshot().items
const items = await readTestJournalItems(journal)
expect(items.map((item) => item.itemId)).toEqual([
'orca:earlier',
created?.itemId,
@@ -138,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,8 @@
import type { AgentJournalSnapshot } from '../../../../shared/agent-session/journal/agent-session-journal-types'
import {
readTestJournalItems,
readTestJournalSnapshot
} from '../../../native-chat/agent-session-journal/journal-observation.test-support'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -38,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
@@ -47,7 +52,7 @@ it.each([
journal,
fence: 7,
publish: () => {
published.push(journal.snapshot())
published.push(readTestJournalSnapshot(journal))
}
})
const translator = createCodexJournalTranslator({
@@ -81,12 +86,14 @@ it.each([
await expect(deferred.drained()).resolves.toEqual({ ok: true })
expect(onFailed).not.toHaveBeenCalled()
expect(deferred.state().failed).toBe(false)
const saved = journal.snapshot().items.filter((item) => item.body.kind === 'question')
const saved = (await readTestJournalItems(journal)).filter(
(item) => item.body.kind === 'question'
)
expect(
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(journal.snapshot())
expect(await published.at(-1)).toEqual(await readTestJournalSnapshot(journal))
translator.dispose()
deferred.close()
}
@@ -1,124 +0,0 @@
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, expect, it, vi } from 'vitest'
import { codexProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
import {
createTrackedJournalOpener,
liveTestJournalRows,
openTestJournalHostDatabase
} 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'
import { createCodexJournalTranslator } from './codex-structured-journal-translation'
import type { CodexStructuredSessionEvent } from '../session-lifecycle/codex-structured-session-state'
const SESSION = 'codex-atomic'
const journals = createTrackedJournalOpener({ legacyInputs: true })
let root: string | undefined
afterEach(async () => {
vi.restoreAllMocks()
await journals.closeAll()
if (root) {
await rm(root, { recursive: true, force: true })
}
})
it('rolls back the entire Codex exit settlement when a later row fails', async () => {
root = await mkdtemp(join(tmpdir(), 'orca-codex-atomic-'))
const options = {
identity: {
sessionId: SESSION,
workspaceId: 'folder',
hostId: 'host',
agent: 'codex' as const,
providerHandle: codexProviderHandle('thread')
},
stateDirectory: root,
now: () => 1_000
}
const journal = await journals.open(options)
const deferred = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging(SESSION))
const published: ReturnType<typeof journal.snapshot>[] = []
const publish = vi.fn(() => {
published.push(journal.snapshot())
})
const commits = vi.fn()
deferred.bind({ journal, fence: 7, publish })
const translator = createCodexJournalTranslator({
sink: deferred.sink,
primaryThreadId: () => 'thread',
now: () => 900
})
const notification = (method: string, params: unknown): CodexStructuredSessionEvent => ({
type: 'notification',
sessionId: SESSION,
threadId: 'thread',
method,
params
})
expect(translator.handle(notification('turn/started', { turn: { id: 'turn' } }))).toEqual({
accepted: true
})
for (let index = 0; index < 201; index++) {
expect(
translator.handle(
notification('item/started', {
item: {
type: 'commandExecution',
id: `call-${index}`,
command: 'run',
status: 'inProgress'
}
})
)
).toEqual({ accepted: true })
}
await expect(deferred.drained()).resolves.toEqual({ ok: true })
published.length = 0
journal.observeCommits(commits)
const before = journal.snapshot()
const database = openTestJournalHostDatabase(root)
const diskBefore = liveTestJournalRows(database.db, SESSION)
const sawEarlierInserts: number[] = []
const transaction = database.transaction.bind(database)
vi.spyOn(database, 'transaction').mockImplementation((run, committed) =>
transaction((db) => {
try {
return run(db)
} catch (error) {
sawEarlierInserts.push(liveTestJournalRows(db, SESSION).length - diskBefore.length)
expect(journal.snapshot()).toEqual(before)
expect(commits).not.toHaveBeenCalled()
expect(
published.every((snapshot) => JSON.stringify(snapshot) === JSON.stringify(before))
).toBe(true)
throw error
}
}, committed)
)
database.db.exec(`CREATE TEMP TRIGGER reject_codex_later BEFORE INSERT ON journal_rows
WHEN NEW.session_id = '${SESSION}' AND NEW.seq > ${before.cursor.sequence + 1}
BEGIN SELECT RAISE(ABORT, 'later Codex settlement row rejected'); END`)
const ended = {
type: 'ended' as const,
sessionId: SESSION,
reason: 'lost child',
cause: 'unexpected-exit' as const,
observedAt: 1_000,
fence: 7,
acquisitionGeneration: 'generation'
}
expect(translator.handle(ended)).toEqual({ accepted: true })
await expect(deferred.drained()).resolves.toMatchObject({ ok: false })
expect(sawEarlierInserts).toEqual([1])
expect(liveTestJournalRows(database.db, SESSION)).toEqual(diskBefore)
expect(journal.snapshot()).toEqual(before)
expect(commits).not.toHaveBeenCalled()
expect(published.every((snapshot) => JSON.stringify(snapshot) === JSON.stringify(before))).toBe(
true
)
translator.dispose()
deferred.close()
})
@@ -1,3 +1,5 @@
import { readTestJournalItems } from '../../../native-chat/agent-session-journal/journal-observation.test-support'
import { openTestAgentSessionDatabase } from '../../../runtime/agent-session/records/agent-session-record-store-test-harness'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -15,10 +17,7 @@ import type { AgentSessionJournal } from '../../../native-chat/agent-session-jou
import { createDeferredStructuredAgentSessionEventSink } from '../../../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink'
import { createCodexJournalTranslator } from './codex-structured-journal-translation'
import type { CodexThreadItem } from '../items/codex-thread-item-identity'
import {
closeTestJournalHostDatabases,
openTestJournalHostDatabase
} from '../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { closeTestJournalHostDatabases } 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'
import { codexProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
@@ -44,7 +43,7 @@ async function openJournal(root: string): Promise<AgentSessionJournal> {
agent: 'codex',
providerHandle: codexProviderHandle(PARENT)
},
database: openTestJournalHostDatabase(root),
database: await openTestAgentSessionDatabase(root),
now: () => 1_000
})
}
@@ -107,14 +106,14 @@ async function session() {
},
items: async (): Promise<readonly AgentJournalRenderItem[]> => {
await deferred.drained()
return journal.snapshot().items
return await readTestJournalItems(journal)
},
reopen: async (): Promise<readonly AgentJournalRenderItem[]> => {
await deferred.drained()
deferred.unbind()
await journal.close()
journal = await openJournal(root)
return journal.snapshot().items
return await readTestJournalItems(journal)
}
}
}
@@ -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?' })
])
@@ -1,3 +1,4 @@
import { readTestJournalItems } from '../../../native-chat/agent-session-journal/journal-observation.test-support'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -68,7 +69,7 @@ async function session() {
drained: () => deferred.drained(),
items: async () => {
await deferred.drained()
return journal.snapshot().items
return await readTestJournalItems(journal)
}
}
}
@@ -1,3 +1,4 @@
import { readTestJournalItems } from '../../../native-chat/agent-session-journal/journal-observation.test-support'
import { describe, expect, it } from 'vitest'
import { chat } from '../threads/codex-thread-catch-up-contract-test-fixture'
@@ -42,7 +43,9 @@ describe('codex turn lifecycle rows', () => {
].map((turn) => ({ ...turn, itemsView: 'full' }))
)
expect(c.journal.snapshot().items.map(({ itemId, body }) => ({ key: itemId, body }))).toEqual([
expect(
(await readTestJournalItems(c.journal)).map(({ itemId, body }) => ({ key: itemId, body }))
).toEqual([
{
key: 'legacy:codex:session-codex-catch-up:turn-lifecycle%3Aturn-done',
body: {
@@ -109,8 +112,8 @@ describe('codex turn lifecycle rows', () => {
}
])
expect(c.journal['state'].tombstones.size).toBe(0)
const rows = c.journal.snapshot().items
const rows = await readTestJournalItems(c.journal)
await c.reopen()
expect(c.journal.snapshot().items).toEqual(rows)
expect(await readTestJournalItems(c.journal)).toEqual(rows)
})
})
@@ -20,7 +20,7 @@ export function registerCodexHistoryBoundsTests(input: {
const c = await chat({ now: 1_000 })
await c.open([], { returnedTurns: [turn(1), turn(2)], paginate: false })
const identity = codexTurnLifecycleIdentity(SESSION, 'turn-1')
const old = c.journal.item(agentJournalItemKey(identity))
const old = await c.journal.reads.item(agentJournalItemKey(identity))
if (old?.body.kind !== 'turn') {
throw new Error('missing saved lifecycle')
}
@@ -50,8 +50,8 @@ export function registerCodexHistoryBoundsTests(input: {
],
paginate: false
})
expect(c.texts()).not.toContain('assistant:Must not commit through stale anchor')
expect(c.journal.item(old.itemId)?.body).toMatchObject({
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 } }
})
})
@@ -1,3 +1,8 @@
import {
readTestJournalItems,
readTestJournalSnapshot,
readTestJournalSubmissions
} from '../../../native-chat/agent-session-journal/journal-observation.test-support'
import { registerCodexHistorySourcesTests } from './codex-structured-thread-catch-up-sources.spec'
import { registerCodexHistoryVariantsTests } from './codex-structured-thread-catch-up-variants.spec'
import { registerCodexHistoryBoundsTests } from './codex-structured-thread-catch-up-bounds.spec'
@@ -24,7 +29,7 @@ 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'
import { completeTestJournalRewind } from '../../../native-chat/agent-session-wire/rewind/structured-rewind-completion.test-support'
export function registerCodexCatchUpEchoTests(input: {
codexWith: typeof catchUpCodexWith
@@ -49,12 +54,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 () => {
@@ -91,7 +96,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',
@@ -102,9 +107,9 @@ export function registerCodexCatchUpEchoTests(input: {
}))
}
await c.reopen()
const before = c.journal.snapshot()
const draft = c.journal.queuedMessages.get('draft')
const receipts = states.map((state) => c.journal.receiptFor(state))
const before = await readTestJournalSnapshot(c.journal)
const draft = await c.journal.queuedMessages.get('draft')
const receipts = await Promise.all(states.map((state) => c.journal.reads.receiptFor(state)))
const native = states.map((state, n) => ({
...turn(n + 2),
items: [
@@ -133,31 +138,35 @@ 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',
'assistant:outside answer'
])
expect(c.journal.snapshot().items.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(c.journal.snapshot().submissions).toEqual(before.submissions)
expect(c.journal.queuedMessages.get('draft')).toEqual(draft)
expect((await readTestJournalItems(c.journal)).slice(0, before.items.length)).toEqual(
before.items
)
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({
provider: 'codex',
threadId: THREAD,
turnId: 'turn-7',
ordinal: 0
})
expect(journalItemEchoFields(c.journal.itemProvenance(outsideKey))).toEqual({ echoOf: null })
const whole = c.journal.snapshot()
expect(journalItemEchoFields(await c.journal.reads.itemProvenance(outsideKey))).toEqual({
echoOf: null
})
const whole = await readTestJournalSnapshot(c.journal)
await c.reopen()
await c.open([...native, outside])
expect(c.journal.snapshot()).toEqual(whole)
expect(c.journal.queuedMessages.get('draft')).toEqual(draft)
const restored = restoreJournal(c.db, SESSION)
expect(restored?.state.queue.cards.get('draft')).toEqual(draft)
expect(await readTestJournalSnapshot(c.journal)).toEqual(whole)
expect(await c.journal.queuedMessages.get('draft')).toEqual(draft)
})
it('keeps native display identity after a real retained cut and disk reopen', async () => {
@@ -185,27 +194,23 @@ 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()
await completeTestJournalRewind(c.root, c.journal, target)
await c.reopen()
const prompt = c.journal.item(key)
const prompt = await c.journal.reads.item(key)
expect(prompt?.body).toEqual(body)
expect(c.journal.submission(clientMessageId)?.dispatchState).toBe('unknown')
expect((await c.journal.reads.submission(clientMessageId))?.dispatchState).toBe('unknown')
const native = {
...turn(4),
items: [
@@ -218,19 +223,21 @@ export function registerCodexCatchUpEchoTests(input: {
{ type: 'agentMessage', id: 'reply', text: 'native answer' }
]
}
const before = c.journal.snapshot()
const beforeTexts = c.texts()
const before = await readTestJournalSnapshot(c.journal)
const beforeTexts = await c.texts()
await c.open([turn(1), turn(2), native])
expect(c.journal.item(key)).toEqual(prompt)
expect(c.journal.snapshot().items.slice(0, before.items.length)).toEqual(before.items)
expect(c.texts()).toEqual([...beforeTexts, 'assistant:native answer'])
expect(c.journal.submission(clientMessageId)?.dispatchState).toBe('unknown')
expect(c.journal.receiptFor(clientMessageId)).toBeNull()
const whole = c.journal.snapshot()
expect(await c.journal.reads.item(key)).toEqual(prompt)
expect((await readTestJournalItems(c.journal)).slice(0, before.items.length)).toEqual(
before.items
)
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(c.journal.snapshot()).toEqual(whole)
await c.journal.replaceEpochItems('handle_forked', 1, [])
expect(await readTestJournalSnapshot(c.journal)).toEqual(whole)
await c.replaceEpoch('handle_forked', [])
const hidden = c.journal.cursor()
const { readCodexHistoryJournalView } =
await import('../../history/journal-views/codex-history-journal-view')
@@ -239,18 +246,23 @@ export function registerCodexCatchUpEchoTests(input: {
).toBeNull()
await c.open([native], {
onReceipt: (id, providerIdentity) =>
c.journal.acceptLate({ clientMessageId: id, providerIdentity, state: 'accepted', fence: 1 })
c.journal.settleLateDispatch({
clientMessageId: id,
providerIdentity,
state: 'accepted',
fence: 1
})
})
expect(c.journal.submission(clientMessageId)?.dispatchState).toBe('accepted')
expect(c.journal.item(key)).toBeNull()
expect(c.journal.snapshot().items).toEqual([])
expect((await c.journal.reads.submission(clientMessageId))?.dispatchState).toBe('accepted')
expect(await c.journal.reads.item(key)).toBeNull()
expect(await readTestJournalItems(c.journal)).toEqual([])
expect(
(await readCodexHistoryJournalView(c.journal, THREAD, AbortSignal.abort())).latest
).toBeNull()
expect(c.journal.cursor().epoch).toBe(hidden.epoch)
await c.reopen()
await c.open([native])
expect(c.journal.snapshot().items).toEqual([])
expect(await readTestJournalItems(c.journal)).toEqual([])
})
it('completes a represented turn and older gaps through an actual paginated open with partial returned content', async () => {
@@ -363,7 +375,7 @@ export function registerCodexCatchUpEchoTests(input: {
}
}
await open()
expect(c.texts()).toEqual([
expect(await c.texts()).toEqual([
'user:native',
'(continued-outside)',
'user:ask 1',
@@ -371,9 +383,9 @@ export function registerCodexCatchUpEchoTests(input: {
'assistant:completed answer'
])
expect(
c.journal.snapshot().items.filter((item) => item.body.kind === 'tool-call')
(await readTestJournalItems(c.journal)).filter((item) => item.body.kind === 'tool-call')
).toHaveLength(1)
expect(c.journal.item(agentJournalItemKey(detailIdentity))?.body).toMatchObject({
expect((await c.journal.reads.item(agentJournalItemKey(detailIdentity)))?.body).toMatchObject({
state: 'completed'
})
const before = c.journal.cursor()
@@ -399,7 +411,7 @@ export function registerCodexCatchUpEchoTests(input: {
// The newly selected page also acquires its known sibling's full content.
expect(fullReads).toBe(3)
expect(
c.journal.snapshot().items.filter((item) => item.body.kind === 'tool-call')
(await readTestJournalItems(c.journal)).filter((item) => item.body.kind === 'tool-call')
).toHaveLength(2)
const acquired = c.journal.cursor()
await open()
@@ -421,9 +433,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')
})
}
@@ -1,3 +1,7 @@
import {
readTestJournalItems,
readTestJournalSnapshot
} from '../../../native-chat/agent-session-journal/journal-observation.test-support'
import {
adapterFor,
fakeCodex,
@@ -104,8 +108,8 @@ export function registerCodexHistorySourcesTests(input: {
}
}
await open()
expect(c.texts()).toEqual(['user:ask 1', 'assistant:A different final answer'])
const rows = c.journal.snapshot().items
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({
kind: 'turn',
@@ -128,12 +132,12 @@ export function registerCodexHistorySourcesTests(input: {
)
)
expect(imports()).toHaveLength(1)
const snapshot = c.journal.snapshot()
const snapshot = await readTestJournalSnapshot(c.journal)
await c.reopen()
expect(c.journal.snapshot()).toEqual(snapshot)
expect(await readTestJournalSnapshot(c.journal)).toEqual(snapshot)
const repeated = vi.spyOn(c.deferred.sink, 'tryAppendTransition')
await open()
expect(c.journal.snapshot()).toEqual(snapshot)
expect(await readTestJournalSnapshot(c.journal)).toEqual(snapshot)
await c.open([], {
paginate: false,
returnedTurns: [
@@ -146,7 +150,7 @@ export function registerCodexHistorySourcesTests(input: {
}
]
})
expect(c.journal.snapshot()).toEqual(snapshot)
expect(await readTestJournalSnapshot(c.journal)).toEqual(snapshot)
expect(
repeated.mock.calls.flatMap(([call]) =>
call.steps.filter(
@@ -184,7 +188,7 @@ export function registerCodexHistorySourcesTests(input: {
},
{ fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE }
)
const legacySaved = c.journal.item(agentJournalItemKey(legacy))
const legacySaved = await c.journal.reads.item(agentJournalItemKey(legacy))
await c.reopen()
await c.open([], {
paginate: false,
@@ -208,7 +212,7 @@ export function registerCodexHistorySourcesTests(input: {
}
]
})
expect(c.journal.snapshot().items.map(({ body }) => body)).toContainEqual(
expect((await readTestJournalItems(c.journal)).map(({ body }) => body)).toContainEqual(
expect.objectContaining({ kind: 'status', text: 'omitted parent child' })
)
await c.open([], {
@@ -230,7 +234,7 @@ export function registerCodexHistorySourcesTests(input: {
}
]
})
const bodies = c.journal.snapshot().items.map(({ body }) => body)
const bodies = (await readTestJournalItems(c.journal)).map(({ body }) => body)
expect(bodies).toContainEqual(
expect.objectContaining({ kind: 'status', text: 'saved parent child' })
)
@@ -244,7 +248,7 @@ export function registerCodexHistorySourcesTests(input: {
output: journalPayload('retained')
})
)
const before = c.journal.snapshot()
const before = await readTestJournalSnapshot(c.journal)
await c.open([], {
paginate: false,
returnedTurns: [
@@ -257,10 +261,10 @@ export function registerCodexHistorySourcesTests(input: {
}
]
})
expect(c.journal.snapshot()).toEqual(before)
expect(await readTestJournalSnapshot(c.journal)).toEqual(before)
await c.reopen()
expect(c.journal.item(agentJournalItemKey(legacy))).toEqual(legacySaved)
const snapshot = c.journal.snapshot()
expect(await c.journal.reads.item(agentJournalItemKey(legacy))).toEqual(legacySaved)
const snapshot = await readTestJournalSnapshot(c.journal)
const turnKey = agentJournalItemKey(codexTurnLifecycleIdentity(SESSION, 'turn-1'))
for (const text of [
'omitted parent child',
@@ -361,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) {
@@ -372,16 +377,16 @@ export function registerCodexHistorySourcesTests(input: {
mcpIdentity: { server: 's', tool: 'lookup' },
input: null
})
const sparse = c.journal
.snapshot()
.items.find(({ body }) => body.kind === 'tool-call' && body.callId === 'sparse-command')
const sparse = (await readTestJournalItems(c.journal)).find(
({ body }) => body.kind === 'tool-call' && body.callId === 'sparse-command'
)
expect(sparse?.body).toMatchObject({
kind: 'tool-call',
input: { command: 'pwd', cwd: '/work' }
})
const originalPatch = c.journal
.snapshot()
.items.find(({ body }) => body.kind === 'tool-call' && body.callId === 'patch')
const originalPatch = (await readTestJournalItems(c.journal)).find(
({ body }) => body.kind === 'tool-call' && body.callId === 'patch'
)
expect(originalPatch?.revision).toBe(1)
await c.reopen()
const transitions = vi.spyOn(c.deferred.sink, 'tryAppendTransition')
@@ -399,10 +404,10 @@ export function registerCodexHistorySourcesTests(input: {
}
await open([incoming, turn(2)])
for (const row of saved) {
expect(row && c.journal.item(row.itemId)).toEqual(row)
expect(row && (await c.journal.reads.item(row.itemId))).toEqual(row)
}
expect(sparse && c.journal.item(sparse.itemId)).toEqual(sparse)
const snapshot = c.journal.snapshot()
expect(sparse && (await c.journal.reads.item(sparse.itemId))).toEqual(sparse)
const snapshot = await readTestJournalSnapshot(c.journal)
expect(snapshot.items.filter(({ body }) => body.kind === 'tool-call')).toEqual([
...saved,
sparse
@@ -424,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',
@@ -451,9 +456,9 @@ export function registerCodexHistorySourcesTests(input: {
revision: 2
})
await c.reopen()
expect(c.journal.snapshot()).toEqual(snapshot)
expect(await readTestJournalSnapshot(c.journal)).toEqual(snapshot)
await open([incoming, turn(2)])
expect(c.journal.snapshot()).toEqual(snapshot)
expect(await readTestJournalSnapshot(c.journal)).toEqual(snapshot)
})
it('releases whole-response groups with more than 1024 unchanged saved keys and keeps a small new turn', async () => {
@@ -470,11 +475,13 @@ export function registerCodexHistorySourcesTests(input: {
await c.open([], { paginate: false, returnedTurns: [entry] })
}
await c.reopen()
const before = c.journal.snapshot()
const before = await readTestJournalSnapshot(c.journal)
const commits = vi.spyOn(c.deferred.sink, 'tryAppendTransition')
await c.open([], { paginate: false, returnedTurns: [...old, turn(31)] })
expect(c.journal.snapshot().items.slice(0, before.items.length)).toEqual(before.items)
expect(c.texts().slice(-2)).toEqual(['user:ask 31', 'assistant:answer 31'])
expect((await readTestJournalItems(c.journal)).slice(0, before.items.length)).toEqual(
before.items
)
expect((await c.texts()).slice(-2)).toEqual(['user:ask 31', 'assistant:answer 31'])
const imports = commits.mock.calls.flatMap(([call]) =>
call.steps.filter(
(step) =>
@@ -482,9 +489,9 @@ export function registerCodexHistorySourcesTests(input: {
)
)
expect(imports).toHaveLength(1)
const snapshot = c.journal.snapshot()
const snapshot = await readTestJournalSnapshot(c.journal)
await c.open([], { paginate: false, returnedTurns: [...old, turn(31)] })
expect(c.journal.snapshot()).toEqual(snapshot)
expect(await readTestJournalSnapshot(c.journal)).toEqual(snapshot)
expect(
commits.mock.calls.flatMap(([call]) =>
call.steps.filter(
@@ -1,3 +1,7 @@
import {
readTestJournalItems,
readTestJournalSnapshot
} from '../../../native-chat/agent-session-journal/journal-observation.test-support'
import { expect, it, vi } from 'vitest'
import { codexTurnLifecycleIdentity } from '../journal/codex-structured-journal-translation-turns'
import { CodexSubagentExecutions } from '../background-work/codex-subagent-executions'
@@ -54,9 +58,9 @@ export function registerCodexHistoryVariantsTests(input: {
]
})
expect(
c.journal.snapshot().items.find(({ body }) => body.kind === 'tool-call')?.body
(await readTestJournalItems(c.journal)).find(({ body }) => body.kind === 'tool-call')?.body
).toMatchObject({ output: journalPayload(output) })
const snapshot = c.journal.snapshot()
const snapshot = await readTestJournalSnapshot(c.journal)
await c.reopen()
const serialize = vi.spyOn(JSON, 'stringify')
await c.open([], { paginate: false, returnedTurns: [large] })
@@ -80,7 +84,7 @@ export function registerCodexHistoryVariantsTests(input: {
})
).toBe(false)
serialize.mockRestore()
expect(c.journal.snapshot()).toEqual(snapshot)
expect(await readTestJournalSnapshot(c.journal)).toEqual(snapshot)
const warnings = vi.fn()
const logger = { warn: warnings, info: vi.fn(), error: vi.fn(), debug: vi.fn() }
const huge = {
@@ -96,7 +100,7 @@ export function registerCodexHistoryVariantsTests(input: {
]
}
await c.open([], { paginate: false, returnedTurns: [huge], logger })
expect(c.journal.snapshot()).toEqual(snapshot)
expect(await readTestJournalSnapshot(c.journal)).toEqual(snapshot)
expect(
warnings.mock.calls.some(
([, fields]) =>
@@ -112,7 +116,7 @@ export function registerCodexHistoryVariantsTests(input: {
],
logger
})
expect(c.journal.snapshot()).toEqual(snapshot)
expect(await readTestJournalSnapshot(c.journal)).toEqual(snapshot)
expect(
warnings.mock.calls.some(
([, fields]) => fields.error instanceof Error && fields.error.message === 'too-large'
@@ -144,7 +148,7 @@ export function registerCodexHistoryVariantsTests(input: {
turn(2)
]
})
const old = c.journal.item(agentJournalItemKey(parent))
const old = await c.journal.reads.item(agentJournalItemKey(parent))
if (old?.body.kind !== 'tool-call') {
throw new Error('missing saved parent')
}
@@ -175,11 +179,11 @@ export function registerCodexHistoryVariantsTests(input: {
]
})
expect(
c.journal
.snapshot()
.items.some(({ body }) => body.kind === 'status' && body.text === 'must fail stale parent')
(await readTestJournalItems(c.journal)).some(
({ body }) => body.kind === 'status' && body.text === 'must fail stale parent'
)
).toBe(false)
expect(c.journal.item(old.itemId)?.body).toMatchObject({
expect((await c.journal.reads.item(old.itemId))?.body).toMatchObject({
output: journalPayload('concurrent writer')
})
expect(
@@ -273,7 +277,7 @@ export function registerCodexHistoryVariantsTests(input: {
subagentExecutions: helpers,
onReceipt: async (id, providerIdentity) => {
receipts.push(id)
await c.journal.acceptLate({
await c.journal.settleLateDispatch({
clientMessageId: id,
providerIdentity,
state: 'accepted',
@@ -281,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',
@@ -291,26 +295,25 @@ export function registerCodexHistoryVariantsTests(input: {
'assistant:Proof sibling'
])
expect(receipts).toEqual(['proof'])
expect(c.journal.submission('proof')?.dispatchState).toBe('accepted')
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' }]
})
expect(
c.journal
.snapshot()
.items.filter(({ body }) => body.kind === 'tool-call')
(await readTestJournalItems(c.journal))
.filter(({ body }) => body.kind === 'tool-call')
.map(({ body }) => body)
).toEqual([
expect.objectContaining({ input: { command: 'pwd', cwd: '/workspace' }, state: 'running' }),
@@ -320,10 +323,10 @@ export function registerCodexHistoryVariantsTests(input: {
output: journalPayload('good')
})
])
const saved = c.journal.snapshot()
const saved = await readTestJournalSnapshot(c.journal)
await c.reopen()
expect(c.journal.snapshot()).toEqual(saved)
expect(c.journal.receiptFor('proof')).not.toBeNull()
expect(await readTestJournalSnapshot(c.journal)).toEqual(saved)
expect(await c.journal.reads.receiptFor('proof')).not.toBeNull()
})
it.each(['completed', 'failed', 'interrupted'] as const)(
@@ -334,7 +337,7 @@ export function registerCodexHistoryVariantsTests(input: {
await c.open([], { returnedTurns: [initial], paginate: false })
const identity = codexTurnLifecycleIdentity(SESSION, 'turn-1')
const key = agentJournalItemKey(identity)
const old = c.journal.item(key)
const old = await c.journal.reads.item(key)
if (old?.body.kind !== 'turn') {
throw new Error('missing saved lifecycle')
}
@@ -344,7 +347,7 @@ export function registerCodexHistoryVariantsTests(input: {
{ ...old.body, contextUsage, requestedAt: 500 },
{ fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE }
)
const original = c.journal.item(
const original = await c.journal.reads.item(
agentJournalItemKey({ provider: 'codex', threadId: THREAD, turnId: 'turn-1', ordinal: 0 })
)
await c.open([], { returnedTurns: [turn(2)], paginate: false })
@@ -362,7 +365,7 @@ export function registerCodexHistoryVariantsTests(input: {
],
paginate: false
})
const updated = c.journal.item(key)
const updated = await c.journal.reads.item(key)
expect(updated?.body).toEqual({
...old.body,
contextUsage,
@@ -373,30 +376,28 @@ export function registerCodexHistoryVariantsTests(input: {
completedAt: (1_700_000_000 + 150) * 1000
})
expect(updated?.sequence).toBe(old.sequence)
expect(c.journal.item(original?.itemId ?? '')).toEqual(original)
expect(await c.journal.reads.item(original?.itemId ?? '')).toEqual(original)
await c.reopen()
const extra = c.journal
.snapshot()
.items.find(
({ body }) =>
body.kind === 'message' &&
body.blocks.some(
(block) => block.type === 'text' && block.text === 'Extra same-turn answer'
)
)
const extra = (await readTestJournalItems(c.journal)).find(
({ body }) =>
body.kind === 'message' &&
body.blocks.some(
(block) => block.type === 'text' && block.text === 'Extra same-turn answer'
)
)
expect(extra?.turnScope).toEqual({ kind: 'turn', turnItemId: key })
const projected = projectStructuredItemsToNativeChat(c.journal.snapshot().items)
const projected = projectStructuredItemsToNativeChat(await readTestJournalItems(c.journal))
expect(
nativeChatTurnMembership(projected, c.journal.snapshot()).turnKeys[
nativeChatTurnMembership(projected, await readTestJournalSnapshot(c.journal)).turnKeys[
projected.findIndex((item) => item.id === extra?.itemId)
]
).toBe(original?.itemId)
const settled = c.journal.item(key)
const settled = await c.journal.reads.item(key)
await c.open([], {
returnedTurns: [{ ...turn(1), status: status === 'completed' ? 'failed' : 'completed' }],
paginate: false
})
expect(c.journal.item(key)).toEqual(settled)
expect(await c.journal.reads.item(key)).toEqual(settled)
}
)
}
@@ -1,3 +1,11 @@
import {
withTestAgentSessionDatabase,
openTestAgentSessionRecordStore
} from '../../../runtime/agent-session/records/agent-session-record-store-test-harness'
import {
readTestJournalItems,
readTestJournalSnapshot
} from '../../../native-chat/agent-session-journal/journal-observation.test-support'
import { recordedCodexRollout } from '../../__fixtures__/codex-rollout-recordings'
import {
prepareLegacyTranscriptImport,
@@ -10,7 +18,6 @@ import { writeFile } from 'node:fs/promises'
import { codexProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
import { join } from 'node:path'
import { agentJournalItemKey } from '../../../../shared/agent-session/journal/agent-session-journal-item-key'
import { openTestAgentSessionRecordStore } from '../../../runtime/agent-session/records/agent-session-record-store-test-harness'
import { recoverStructuredRewind } from '../../../native-chat/agent-session-wire/rewind/structured-rewind-recovery'
import { usableJournalHistoryOrigin } from '../../../native-chat/agent-session-journal/context-epochs/journal-history-origin'
import { describe, expect, it } from 'vitest'
@@ -97,7 +104,7 @@ describe('Codex chat catch-up', () => {
history[1_029] = proof('U')
await c.open(history, { onReceipt })
expect(settled).toEqual(['U'])
expect(c.journal.submission('U')?.dispatchState).toBe('accepted')
expect((await c.journal.reads.submission('U'))?.dispatchState).toBe('accepted')
await c.open([], {
onReceipt,
connection: {
@@ -111,7 +118,7 @@ describe('Codex chat catch-up', () => {
})
expect(settled).toEqual(['U', 'failed-import'])
await c.reopen()
expect(c.journal.submission('failed-import')?.dispatchState).toBe('accepted')
expect((await c.journal.reads.submission('failed-import'))?.dispatchState).toBe('accepted')
await c.journal.appendSubmission({
clientMessageId: 'cancelled',
body: { kind: 'message', role: 'user', blocks: [] },
@@ -168,7 +175,7 @@ describe('Codex chat catch-up', () => {
// Recent proof stops at 1024 known turns. The oldest import page also contains a known turn.
expect(settled).toEqual(['U', 'failed-import', 'old-gap', 'fetched-known'])
expect(
c.journal.item(
await c.journal.reads.item(
agentJournalItemKey({
provider: 'codex',
threadId: THREAD,
@@ -178,9 +185,9 @@ describe('Codex chat catch-up', () => {
)
).toBeNull()
await c.reopen()
expect(c.journal.submission('old-gap')?.dispatchState).toBe('accepted')
expect(c.journal.submission('fetched-known')?.dispatchState).toBe('accepted')
expect(c.journal.submission('V')?.dispatchState).toBe('rejected')
expect((await c.journal.reads.submission('old-gap'))?.dispatchState).toBe('accepted')
expect((await c.journal.reads.submission('fetched-known'))?.dispatchState).toBe('accepted')
expect((await c.journal.reads.submission('V'))?.dispatchState).toBe('rejected')
for (const n of [100, 1_000]) {
for (const id of [`tail-${n}`, `second-${n}`]) {
@@ -268,11 +275,11 @@ describe('Codex chat catch-up', () => {
})
expect(settled).toContain(first)
expect(settled).not.toContain(next)
expect(c.journal.submission(next)?.dispatchState).toBe('pending')
expect((await c.journal.reads.submission(next))?.dispatchState).toBe('pending')
await c.reopen()
expect(c.journal.submission(first)?.dispatchState).toBe('accepted')
expect(c.journal.submission(next)?.dispatchState).toBe('rejected')
expect(c.journal.submission('V')?.dispatchState).toBe('rejected')
expect((await c.journal.reads.submission(first))?.dispatchState).toBe('accepted')
expect((await c.journal.reads.submission(next))?.dispatchState).toBe('rejected')
expect((await c.journal.reads.submission('V'))?.dispatchState).toBe('rejected')
// Losing ownership or cancellation during a read forbids a new proof callback.
const blocked = new AbortController()
current = true
@@ -301,7 +308,7 @@ describe('Codex chat catch-up', () => {
const outside = { ...turn(2), items: [{ type: 'agentMessage', id: 'a-2', text: answer }] }
await c.open([turn(1), outside])
await c.reopen()
const items = c.journal.snapshot().items
const items = await readTestJournalItems(c.journal)
const stored = items.find(
(item) =>
item.body.kind === 'message' &&
@@ -343,24 +350,28 @@ describe('Codex chat catch-up', () => {
}
await c.deferred.drained()
native.dispose()
const before = c.journal.snapshot().items
const before = await readTestJournalItems(c.journal)
await c.reopen()
await c.open([turn(1), turn(2), turn(3)])
expect(c.journal.snapshot().items.slice(0, before.length)).toEqual(before)
expect(c.texts().slice(-4)).toEqual([
expect((await readTestJournalItems(c.journal)).slice(0, before.length)).toEqual(before)
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 () => {
const c = await chat({ now: 1_000 })
await c.open([turn(1)])
const before = c.journal.snapshot()
const before = await readTestJournalSnapshot(c.journal)
const large = {
...turn(2),
items: Array.from({ length: 600 }, (_, n) => ({
@@ -369,23 +380,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(c.journal.snapshot()).toEqual(before)
c.db.exec('DROP TRIGGER fail_later_partition')
expect(await readTestJournalSnapshot(c.journal)).toEqual(before)
await withTestAgentSessionDatabase(c.root, (db) => db.exec('DROP TRIGGER fail_later_partition'))
await c.reopen()
expect(c.journal.snapshot()).toEqual(before)
c.publications.length = 0
expect(await readTestJournalSnapshot(c.journal)).toEqual(before)
await c.open([turn(1), large])
expect(c.texts()).toHaveLength(603)
expect(c.publications).toEqual([before.items.length + 602])
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 () => {
@@ -398,16 +411,16 @@ describe('Codex chat catch-up', () => {
]
})
await c.open([withReasoning(1)])
const before = c.journal.snapshot().items
const before = await readTestJournalItems(c.journal)
await c.open([withReasoning(2)])
expect(c.journal.snapshot().items.slice(0, before.length)).toEqual(before)
expect(c.texts().filter((text) => text.startsWith('assistant:'))).toEqual([
expect((await readTestJournalItems(c.journal)).slice(0, before.length)).toEqual(before)
expect((await c.texts()).filter((text) => text.startsWith('assistant:'))).toEqual([
'assistant:answer 1',
'assistant:answer 2'
])
const reasoning = c.journal
.snapshot()
.items.filter((item) => item.body.kind === 'message' && item.body.role === 'reasoning')
const reasoning = (await readTestJournalItems(c.journal)).filter(
(item) => item.body.kind === 'message' && item.body.role === 'reasoning'
)
expect(new Set(reasoning.map((item) => item.itemId)).size).toBe(2)
expect(new Set(reasoning.map((item) => JSON.stringify(item.turnScope))).size).toBe(2)
})
@@ -490,7 +503,7 @@ describe('Codex chat catch-up', () => {
options: { filePath }
})
).toMatchObject({ ok: true, imported: 2 })
const original = c.journal.snapshot().items
const original = await readTestJournalItems(c.journal)
const dated = (n: number) => ({ ...turn(n), startedAt: n, completedAt: n + 10 })
await c.open([dated(100), dated(300)])
const target = agentJournalItemKey({
@@ -506,23 +519,23 @@ 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,
itemId: target,
expectedEpoch: c.journal.epoch,
expectedEpoch: c.journal.cursor().epoch,
phase: 'provider-succeeded'
}
}))
})
clock.now = 500_000
const deps = () => ({ store, logger: testEventSinkLogging().logger })
await recoverStructuredRewind(deps(), SESSION, c.journal, 1)
expect(
c.journal
.snapshot()
.items.slice(0, original.length)
(await readTestJournalItems(c.journal))
.slice(0, original.length)
.map(({ itemId, body, observedAt, turnScope }) => ({ itemId, body, observedAt, turnScope }))
).toEqual(
original.map(({ itemId, body, observedAt, turnScope }) => ({
@@ -536,17 +549,18 @@ 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'
])
expect(
c.journal
.snapshot()
.items.slice(0, original.length)
(await readTestJournalItems(c.journal))
.slice(0, original.length)
.map(({ itemId, body, observedAt, turnScope }) => ({ itemId, body, observedAt, turnScope }))
).toEqual(
original.map(({ itemId, body, observedAt, turnScope }) => ({
@@ -556,10 +570,10 @@ describe('Codex chat catch-up', () => {
turnScope
}))
)
const complete = c.journal.snapshot()
const complete = await readTestJournalSnapshot(c.journal)
await c.reopen()
await c.open([dated(100), dated(150), dated(200), dated(250), dated(600)])
expect(c.journal.snapshot()).toEqual(complete)
expect(await readTestJournalSnapshot(c.journal)).toEqual(complete)
})
it('keeps erased-origin legacy chats readable without guessing from native IDs', async () => {
@@ -573,7 +587,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 +600,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'])
})
})
@@ -630,7 +649,7 @@ it.each(['a', 'b'] as const)(
fence: 1,
turnScope: { kind: 'turn', turnItemId: agentJournalItemKey(turnIdentity) }
})
const before = c.journal.item(key)!.revision
const before = (await c.journal.reads.item(key))!.revision
// API contract derived from recorded command fields, not a captured list/read response.
const command = {
type: 'commandExecution',
@@ -652,7 +671,7 @@ it.each(['a', 'b'] as const)(
]
}
await c.open([], { returnedTurns: [supplied], paginate: false })
const saved = c.journal.item(key)!
const saved = (await c.journal.reads.item(key))!
expect(saved.revision).toBeGreaterThan(before)
expect(saved.body).toMatchObject({
kind: 'tool-call',
@@ -662,10 +681,10 @@ it.each(['a', 'b'] as const)(
})
expect(saved.body).not.toHaveProperty('exitCode')
expect(saved.body).not.toHaveProperty('durationMs')
expect(c.journal.item(agentJournalItemKey(turnIdentity))?.body).toMatchObject({
expect((await c.journal.reads.item(agentJournalItemKey(turnIdentity)))?.body).toMatchObject({
state: label === 'a' ? 'interrupted' : 'completed'
})
const calls = projectStructuredItemsToNativeChat(c.journal.snapshot().items)
const calls = projectStructuredItemsToNativeChat(await readTestJournalItems(c.journal))
.flatMap((message) => message.blocks)
.filter((block) => block.type === 'tool-call')
expect(calls.find((call) => call.callId === id)).not.toHaveProperty('exitCode')
@@ -701,14 +720,13 @@ it.each(['a', 'b'] as const)(
],
paginate: false
})
expect(c.journal.item(key)?.body).toEqual(saved.body)
expect((await c.journal.reads.item(key))?.body).toEqual(saved.body)
expect(
c.journal
.snapshot()
.items.find((row) => row.body.kind === 'tool-call' && row.body.callId === 'failed-seven')
?.body
(await readTestJournalItems(c.journal)).find(
(row) => row.body.kind === 'tool-call' && row.body.callId === 'failed-seven'
)?.body
).toMatchObject({ state: 'failed', exitCode: 7, durationMs: 11872 })
await c.reopen()
expect(c.journal.item(key)?.body).toEqual(saved.body)
expect((await c.journal.reads.item(key))?.body).toEqual(saved.body)
}
)
@@ -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()
}
@@ -1,13 +1,16 @@
import { readTestJournalItems } from '../../../native-chat/agent-session-journal/journal-observation.test-support'
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'
@@ -74,14 +77,11 @@ export async function chat(clock: { now: number }) {
}
let journal = await journals.open(journalOptions)
let deferred = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging())
const publications: number[] = []
const bind = () =>
deferred.bind({
journal,
fence: 1,
publish: () => {
publications.push(journal.snapshot().items.length)
}
publish: () => {}
})
bind()
cleanups.push(async () => {
@@ -97,15 +97,12 @@ export async function chat(clock: { now: number }) {
return deferred
},
root,
get db() {
return openTestJournalHostDatabase(root).db
},
publications,
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()
},
@@ -122,8 +119,25 @@ export async function chat(clock: { now: number }) {
})
return deferred.drained()
},
texts: () =>
journal.snapshot().items.flatMap((item) => {
/** 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') {
const text = item.body.blocks.map((block) => (block.type === 'text' ? block.text : ''))
return [`${item.body.role}:${text.join('')}`]
@@ -1,3 +1,7 @@
import {
readTestJournalItems,
readTestJournalSnapshot
} from '../../../native-chat/agent-session-journal/journal-observation.test-support'
import { writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { expect, it } from 'vitest'
@@ -89,7 +93,7 @@ it('retains the original adoption cutoff through native rewind and disk recovery
options: { filePath }
})
).toMatchObject({ ok: true, imported: 2 })
const original = c.journal.snapshot().items
const original = await readTestJournalItems(c.journal)
const dated = (n: number) => ({ ...turn(n), startedAt: n, completedAt: n + 10 })
await c.open([dated(100), dated(300)])
const target = agentJournalItemKey({
@@ -105,42 +109,42 @@ 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,
itemId: target,
expectedEpoch: c.journal.epoch,
expectedEpoch: c.journal.cursor().epoch,
phase: 'provider-succeeded'
}
}))
})
clock.now = 500_000
const deps = () => ({ store, logger: testEventSinkLogging().logger })
await recoverStructuredRewind(deps(), SESSION, c.journal, 1)
const restated = c.journal.snapshot().items.slice(0, original.length)
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'
])
expect(c.journal.snapshot().items.slice(0, original.length)).toEqual(restated)
const complete = c.journal.snapshot()
expect((await readTestJournalItems(c.journal)).slice(0, original.length)).toEqual(restated)
const complete = await readTestJournalSnapshot(c.journal)
await c.reopen()
await c.open([dated(100), dated(150), dated(200), dated(250), dated(600)])
expect(c.journal.snapshot()).toEqual(complete)
expect(await readTestJournalSnapshot(c.journal)).toEqual(complete)
})
it('keeps erased-origin legacy chats readable without guessing from native IDs', async () => {
@@ -154,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 },
@@ -162,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'])
})
@@ -1,3 +1,4 @@
import { readTestJournalItems } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { AGENT_JOURNAL_THREAD_SCOPE } from '../../shared/agent-session/journal/agent-session-journal-types'
import {
@@ -73,7 +74,7 @@ describe('Pi final tail before host eviction', () => {
expect(append('ordinary')).toEqual({ accepted: true })
return gate.promise
})
const before = journal.snapshot().items.length
const before = (await readTestJournalItems(journal)).length
vi.useFakeTimers()
const reply = `Final reply${'r'.repeat(2 * 1024 * 1024)}`
const status = `Final status${'s'.repeat(17 * 1024 * 1024)}`
@@ -99,7 +100,7 @@ describe('Pi final tail before host eviction', () => {
return result
})
await vi.advanceTimersByTimeAsync(2_501)
expect(journal.snapshot().items).toHaveLength(before)
expect(await readTestJournalItems(journal)).toHaveLength(before)
expect(landed).toBe(false)
expect(onFailed).not.toHaveBeenCalled()
gate.resolve()
@@ -115,11 +116,11 @@ describe('Pi final tail before host eviction', () => {
})
expect(acknowledgeRelease).toHaveBeenCalledOnce()
expect(
journal
.snapshot()
.items.some((item) => item.body.kind === 'status' && item.body.text === 'must not land')
(await readTestJournalItems(journal)).some(
(item) => item.body.kind === 'status' && item.body.text === 'must not land'
)
).toBe(false)
expect(journal.snapshot().items.map((item) => item.body)).toEqual(
expect((await readTestJournalItems(journal)).map((item) => item.body)).toEqual(
expect.arrayContaining([
expect.objectContaining({
kind: 'message',
@@ -4,10 +4,8 @@ import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import type { AgentJournalMessageItem } from '../../../shared/agent-session/journal/agent-session-journal-types'
import type { AgentSessionJournalIdentity } from '../../../shared/agent-session/journal/agent-session-journal-identity'
import {
createTrackedJournalOpener,
openTestJournalHostDatabase
} from '../agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { createTrackedJournalOpener } from '../agent-session-journal/store-lifecycle/journal-host-database-test-support'
import { openTestAgentSessionDatabase } from '../../runtime/agent-session/records/agent-session-record-store-test-harness'
import type { AgentSessionJournal } from '../agent-session-journal/journal-store'
import { isAgentSessionAttachmentExpiredError } from './agent-session-attachment-claims'
import {
@@ -85,22 +83,13 @@ describe('claims written with the message', () => {
return join(storeRoot, uploadId, name)
}
function claims(): { upload_id: string; session_id: string }[] {
return openTestJournalHostDatabase(stateDirectory)
.db.prepare(
'SELECT upload_id, session_id FROM agent_session_attachment_claims ORDER BY session_id'
)
.all()
.flatMap((row) =>
typeof row === 'object' &&
row !== null &&
'upload_id' in row &&
'session_id' in row &&
typeof row.upload_id === 'string' &&
typeof row.session_id === 'string'
? [{ upload_id: row.upload_id, session_id: row.session_id }]
: []
)
async function claims(): Promise<{ uploadId: string; sessionId: string }[]> {
const database = await openTestAgentSessionDatabase(stateDirectory)
const result = await database.readers.database({ kind: 'attachment-claims', after: 0 })
if (result.kind !== 'attachment-claims') {
throw new Error('expected attachment claims')
}
return result.claims.map(({ uploadId, sessionId }) => ({ uploadId, sessionId }))
}
function clientSend(journal: AgentSessionJournal, id: string, body: AgentJournalMessageItem) {
@@ -135,8 +124,8 @@ describe('claims written with the message', () => {
)
).catch((caught: unknown) => caught)
expect(isAgentSessionAttachmentExpiredError(error)).toBe(true)
expect(journal.submission('send-1')).toBeUndefined()
expect(claims()).toEqual([])
expect(await journal.reads.submission('send-1')).toBeUndefined()
expect(await claims()).toEqual([])
})
it("refuses a gone upload and keeps a waiting card's attachment claim across clear", async () => {
@@ -150,7 +139,7 @@ describe('claims written with the message', () => {
})
.catch((caught: unknown) => caught)
expect(isAgentSessionAttachmentExpiredError(refused)).toBe(true)
expect(journal.queuedMessages.list()).toHaveLength(0)
expect(await journal.queuedMessages.list()).toHaveLength(0)
const path = await storeUpload(UPLOAD_A, 'notes.txt')
const body = message({ type: 'text', text: `read @${path}` })
@@ -159,13 +148,13 @@ describe('claims written with the message', () => {
body,
requireAttachments: true
})
await journal.context.clear(
{ operationId: 'clear', afterFence: 0, clearedAt: 1000 },
{ write: () => {}, committed: () => {} }
)
await journal.write({
kind: 'context-clear',
boundary: { operationId: 'clear', afterFence: 0, clearedAt: 1000 }
})
await journals.closeAll()
const reopened = await open()
expect(reopened.queuedMessages.get('draft-1')).toMatchObject({ body, state: 'waiting' })
expect(claims()).toEqual([{ upload_id: UPLOAD_A, session_id: IDENTITY.sessionId }])
expect(await reopened.queuedMessages.get('draft-1')).toMatchObject({ body, state: 'waiting' })
expect(await claims()).toEqual([{ uploadId: UPLOAD_A, sessionId: IDENTITY.sessionId }])
})
})
@@ -39,7 +39,10 @@ import {
import {
claimAgentSessionAttachmentsInTransaction,
isAgentSessionAttachmentExpiredError,
markUnclaimedUploadForSweep
markUnclaimedUploadForSweep,
listUploadsBeingSwept,
finishUploadSweep,
pruneAgentSessionAttachmentClaims
} from './agent-session-attachment-claims'
import { agentSessionAttachmentStoreRoot } from './agent-session-attachment-references'
import { AgentSessionAttachmentStore } from './agent-session-attachment-store'
@@ -79,12 +82,25 @@ afterEach(async () => {
await rm(stateDirectory, { recursive: true, force: true })
})
function facts(overrides: Partial<AgentSessionAttachmentSweepFacts> = {}) {
function facts(
overrides: Partial<AgentSessionAttachmentSweepFacts> = {}
): AgentSessionAttachmentSweepFacts {
return {
database: () => db,
write: <T>(run: (db: Database.Database) => T) =>
overrides.database?.() === null ? undefined : journal.writeAutocommit(run),
recordedSessionIds: () => recorded,
listBeingSwept: () => listUploadsBeingSwept(db),
markForSweep: (uploadId, now) =>
journal.writeAutocommit((connection) =>
markUnclaimedUploadForSweep(connection, uploadId, now)
),
finishSweep: (uploadId) =>
journal.writeAutocommit((connection) => finishUploadSweep(connection, uploadId)),
pruneClaims: () => {
journal.writeAutocommit((connection) =>
pruneAgentSessionAttachmentClaims(connection, {
root: store.rootDir,
recordedSessionIds: recorded
})
)
},
...overrides
}
}
@@ -1,140 +0,0 @@
import { mkdtemp, rm, mkdir, writeFile } from 'node:fs/promises'
import { randomUUID } from 'node:crypto'
import { tmpdir } from 'node:os'
import { join, dirname } from 'node:path'
import { afterEach, beforeEach } from 'vitest'
import type {
AgentJournalItemIdentity,
AgentSessionJournalIdentity
} from '../../../../shared/agent-session/journal/agent-session-journal-identity'
import type { AgentJournalMessageItem } from '../../../../shared/agent-session/journal/agent-session-journal-types'
import type { JournalHostDatabase } from '../database/journal-host-database'
import {
createTrackedJournalOpener,
openTestJournalHostDatabase
} from '../store-lifecycle/journal-host-database-test-support'
import { codexProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
import { formatNativeChatFileReference } from '../../../../shared/agent-image-paste'
import { agentSessionSendBodyFingerprint } from '../../../../shared/agent-session/requests/structured-agent-session-send-mutation'
import * as attachmentReferences from '../../agent-session-attachments/agent-session-attachment-references'
export function createFixture() {
const IDENTITY: AgentSessionJournalIdentity = {
sessionId: 'session-1',
workspaceId: 'ws-1',
hostId: 'host-1',
agent: 'codex',
providerHandle: codexProviderHandle('thread-1')
}
const PEER: AgentSessionJournalIdentity = {
...IDENTITY,
sessionId: 'session-peer',
providerHandle: codexProviderHandle('thread-peer')
}
let root: string
let clock = 1_000
let database: JournalHostDatabase
const journals = createTrackedJournalOpener()
function now(): number {
clock += 1
return clock
}
function storedRows(sessionId: string, epoch: string): number {
return Number(
database.db
.prepare('SELECT count(*) AS total FROM journal_rows WHERE session_id = ? AND epoch = ?')
.get(sessionId, epoch)?.total
)
}
function item(ordinal: number): AgentJournalItemIdentity {
return { provider: 'codex', threadId: 'thread-1', turnId: 'turn-1', ordinal }
}
beforeEach(async () => {
root = await mkdtemp(join(tmpdir(), 'orca-journal-replace-'))
clock = 1_000
database = openTestJournalHostDatabase(root)
})
afterEach(async () => {
try {
await database.close()
} catch {}
await journals.closeAll()
await rm(root, { recursive: true, force: true })
})
async function legacyInputs(count: number) {
const existing = await journals.open({ identity: IDENTITY, stateDirectory: root })
await existing.close()
const files: { id: string; path: string; body: AgentJournalMessageItem }[] = []
for (let n = 0; n < count; n++) {
const id = randomUUID()
const path = join(
attachmentReferences.agentSessionAttachmentStoreRoot(root),
id,
'source.txt'
)
await mkdir(dirname(path), { recursive: true })
await writeFile(path, 'queued source')
const body: AgentJournalMessageItem = {
kind: 'message',
role: 'user',
blocks: [{ type: 'text', text: formatNativeChatFileReference(path) }]
}
database.db
.prepare(`INSERT INTO queued_messages
(session_id, message_id, position, body_json, fingerprint, created_at, host_instance, state, hold_reason, returned_reason, consumed_as)
VALUES (?, ?, ?, ?, ?, 0, 'earlier', ?, ?, ?, ?)`)
.run(
IDENTITY.sessionId,
`queue-${n}`,
n + 1,
JSON.stringify(body),
agentSessionSendBodyFingerprint(IDENTITY.sessionId, body),
n % 2 ? 'returned' : 'waiting',
n % 2 ? null : 'send_failed',
n % 2 ? 'refused before' : null,
n % 2 ? `old-${n}` : null
)
files.push({ id, path, body })
}
return files
}
return {
IDENTITY,
PEER,
get clock() {
return clock
},
set clock(value: typeof clock) {
clock = value
},
get database() {
return database
},
set database(value: typeof database) {
database = value
},
item,
journals,
legacyInputs,
now,
get root() {
return root
},
set root(value: typeof root) {
root = value
},
storedRows
}
}
@@ -37,6 +37,9 @@ CREATE INDEX journal_send_receipt_sequence ON journal_send_receipts(session_id,e
CREATE INDEX journal_queue_order ON journal_queue_cards(session_id, epoch, position, message_id)
WHERE state IN ('waiting','returned');
CREATE INDEX journal_queue_last_position ON journal_queue_cards(session_id, epoch, position);
CREATE INDEX journal_queue_source_metadata ON journal_queue_cards(session_id,epoch,message_id,
CASE WHEN json_valid(payload_json) THEN octet_length(json_extract(payload_json,'$.body')) END,
CASE WHEN json_valid(payload_json) THEN json_extract(payload_json,'$.queuedAt.sequence') END);
CREATE INDEX journal_queue_returned_commands ON journal_queue_cards(session_id, epoch, command_turn_item_id)
WHERE state='returned';
CREATE INDEX journal_edit_leases_message ON journal_edit_leases(session_id, epoch, message_id);
@@ -69,6 +72,8 @@ CREATE INDEX journal_items_provider_turn ON journal_items(session_id,epoch,provi
WHERE provider_turn_id IS NOT NULL;
CREATE INDEX journal_items_codex_turn ON journal_items(session_id,epoch,codex_thread_id,codex_turn_id,sequence,sequence_index,ordinal)
WHERE hidden=0 AND codex_turn_id IS NOT NULL;
CREATE INDEX journal_items_visible_provider_key ON journal_items(session_id,epoch,legacy_provider_item_key)
WHERE hidden=0 AND legacy_provider_item_key IS NOT NULL;
CREATE INDEX journal_submission_codex_turn ON journal_submission_attempts(session_id,epoch,codex_thread_id,codex_turn_id,item_id)
WHERE current=1 AND dispatch_state='accepted';
CREATE INDEX journal_items_opener ON journal_items(session_id, epoch, opener_item_id, ordinal)
@@ -124,6 +124,7 @@ CREATE TABLE journal_items (
provider_turn_id TEXT,
codex_thread_id TEXT,
codex_turn_id TEXT,
legacy_provider_item_key TEXT,
opener_item_id TEXT,
context_clear INTEGER NOT NULL DEFAULT 0,
goal_transition INTEGER NOT NULL DEFAULT 0,
@@ -1,3 +1,4 @@
import { readTestJournalItems } from '../journal-observation.test-support'
import {
AGENT_JOURNAL_THREAD_SCOPE,
type AgentJournalItemBody
@@ -8,17 +9,17 @@ import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import type { AgentSessionContextUsage } from '../../../../shared/agent-session/agent-session-context-usage'
import type {
AgentJournalItemIdentity,
AgentSessionJournalIdentity
} from '../../../../shared/agent-session/journal/agent-session-journal-identity'
import type { AgentSessionJournalIdentity } from '../../../../shared/agent-session/journal/agent-session-journal-identity'
import {
createTrackedJournalOpener,
openTestJournalHostDatabase,
liveTestJournalRows,
updateTestJournalRowJson
} from '../store-lifecycle/journal-host-database-test-support'
import { claudeProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
import { seedTestJournalHistoricalRows } from '../replay/journal-historical-inputs.test-support'
import {
agentSessionJournalProviderHandle,
claudeProviderHandle
} from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
export function registerJournalRowContextUsageTests(): void {
const IDENTITY: AgentSessionJournalIdentity = {
@@ -60,7 +61,7 @@ export function registerJournalRowContextUsageTests(): void {
let root: string
let clock = 1_000
const journals = createTrackedJournalOpener({ legacyInputs: true })
const journals = createTrackedJournalOpener()
const open = () =>
journals.open({
@@ -70,10 +71,6 @@ export function registerJournalRowContextUsageTests(): void {
mintEpoch: () => 'epoch-1'
})
function row(ordinal: number): AgentJournalItemIdentity {
return { provider: 'orca', clientMessageId: `row-${ordinal}` }
}
const turn = (turnId: string, contextUsage?: AgentSessionContextUsage): AgentJournalItemBody => ({
kind: 'turn',
turnId,
@@ -93,29 +90,67 @@ export function registerJournalRowContextUsageTests(): void {
describe('context facts on replayed turn rows', () => {
it('keeps a turn row whose facts it cannot read, and everything after it, minus the facts', async () => {
const journal = await open()
await journal.appendItem(row(0), turn('turn-0', FACTS[0]), {
fence: 1,
turnScope: AGENT_JOURNAL_THREAD_SCOPE
})
await journal.appendItem(
row(1),
{ kind: 'message', role: 'assistant', blocks: [{ type: 'text', text: 'after' }] },
{ fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE }
const db = openTestJournalHostDatabase(root).db
const epoch = 'saved-context'
seedTestJournalHistoricalRows(db, IDENTITY, [
{
v: 3,
kind: 'epoch',
epoch,
seq: 1,
ts: 1_000,
fence: 1,
reason: 'session_created',
providerHandle: agentSessionJournalProviderHandle(IDENTITY)
},
{
v: 3,
kind: 'item',
epoch,
seq: 2,
ts: 1_000,
fence: 1,
itemId: 'orca:row-0',
revision: 1,
body: turn('turn-0', FACTS[0]),
turnScope: AGENT_JOURNAL_THREAD_SCOPE
},
{
v: 3,
kind: 'item',
epoch,
seq: 3,
ts: 1_001,
fence: 1,
itemId: 'orca:row-1',
revision: 1,
body: { kind: 'message', role: 'assistant', blocks: [{ type: 'text', text: 'after' }] },
turnScope: AGENT_JOURNAL_THREAD_SCOPE
}
])
updateTestJournalRowJson(
db,
IDENTITY.sessionId,
2,
JSON.stringify({
v: 3,
kind: 'item',
epoch,
seq: 2,
ts: 1_000,
fence: 1,
itemId: 'orca:row-0',
revision: 1,
body: {
...turn('turn-0'),
contextUsage: { used: { kind: 'unknown', capturedAt: 'later' } }
},
turnScope: AGENT_JOURNAL_THREAD_SCOPE
})
)
await journal.close()
const opened = openTestJournalHostDatabase(root)
try {
const rowJson = liveTestJournalRows(opened.db, IDENTITY.sessionId)[1]?.rowJson ?? ''
const future = JSON.parse(rowJson)
future.body.contextUsage = { used: { kind: 'unknown', capturedAt: 'later' } }
updateTestJournalRowJson(opened.db, IDENTITY.sessionId, 2, JSON.stringify(future))
} finally {
await opened.close()
}
const reopened = await open()
expect(reopened.snapshot().items.map((item) => item.body)).toEqual([
expect((await readTestJournalItems(reopened)).map((item) => item.body)).toEqual([
turn('turn-0'),
{ kind: 'message', role: 'assistant', blocks: [{ type: 'text', text: 'after' }] }
])
@@ -1,132 +0,0 @@
import { readFileSync } from 'node:fs'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, expect, it } from 'vitest'
import { z } from 'zod'
import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../shared/agent-session/journal/agent-session-journal-types'
import type {
AgentJournalItemIdentity,
AgentSessionJournalIdentity
} from '../../../../shared/agent-session/journal/agent-session-journal-identity'
import { codexProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
import {
createTrackedJournalOpener,
openTestJournalHostDatabase,
SAVED_BY_NEWER_ORCA
} from '../store-lifecycle/journal-host-database-test-support'
import { restoreJournal } from '../replay/journal-open'
// Admission keeps the in-memory chat within what this build declares on every path a saved value
// takes into it, so restating that chat on a new epoch never drops a newer build's key.
const IDENTITY: AgentSessionJournalIdentity = {
sessionId: 'session-1',
workspaceId: 'ws-1',
hostId: 'host-1',
agent: 'codex',
providerHandle: codexProviderHandle('thread-1')
}
const SCOPE = { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE }
let root: string
let clock = 1_000
const inputs = createTrackedJournalOpener({ legacyInputs: true })
const journals = createTrackedJournalOpener()
beforeEach(async () => {
root = await mkdtemp(join(tmpdir(), 'orca-journal-storage-admission-'))
})
afterEach(async () => {
await inputs.closeAll()
await journals.closeAll()
await rm(root, { recursive: true, force: true })
})
function open(opener: typeof journals) {
return opener.open({
identity: IDENTITY,
stateDirectory: root,
now: () => (clock += 1),
mintEpoch: () => `epoch-${clock}`
})
}
function item(ordinal: number): AgentJournalItemIdentity {
return { provider: 'codex', threadId: 'thread-1', turnId: 'turn-1', ordinal }
}
async function writeItems(opener: typeof journals, ordinals: number[]): Promise<string> {
const journal = await open(opener)
for (const ordinal of ordinals) {
await journal.appendItem(
item(ordinal),
{ kind: 'message', role: 'assistant', blocks: [{ type: 'text', text: `item ${ordinal}` }] },
SCOPE
)
}
await opener.closeAll()
return journal.epoch
}
const db = () => openTestJournalHostDatabase(root).db
/** Every saved byte of the chat database: a refusal writes none of it. */
function savedBytes(): unknown {
const tables = db()
.prepare(`SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name`)
.all()
.map(({ name }) => String(name))
return tables.map((table) => [table, db().prepare(`SELECT * FROM "${table}"`).all()])
}
/** Adds a key no build declares inside an item's first text block, as a newer build would. */
function withLaterKey(json: string): string {
return json.replace('"type":"text",', '"type":"text","emphasis":"strong",')
}
it("refuses a newer build's record with an undeclared nested key before adopting it, writes nothing, and starts no epoch", async () => {
await writeItems(journals, [0, 1, 2])
const database = openTestJournalHostDatabase(root)
const source = db().prepare('SELECT generation, through_seq FROM journal_outcome_epochs').get()
const generation = Number(source?.generation)
const through = Number(source?.through_seq)
expect(restoreJournal(db(), IDENTITY.sessionId)).toMatchObject({ newer: null, damage: null })
// The newer build revises item 2 with a key this build does not declare.
const itemId = 'codex:thread-1:turn-1:2'
const latest = db()
.prepare(`SELECT value_json FROM journal_outcomes WHERE field = 'items' AND entry_key = ?
ORDER BY seq DESC LIMIT 1`)
.get(JSON.stringify(itemId))
const revised = withLaterKey(String(latest?.value_json))
const next = through + 1
const ordinal = (index: number) =>
`s${String(next).padStart(16, '0')}:${String(index).padStart(16, '0')}`
database.transaction((write) => {
const insert = write.prepare(`INSERT INTO journal_outcomes
(generation, seq, field, entry_key, format, ordinal, value_json, deleted)
VALUES (?, ?, ?, ?, 1, ?, ?, 0)`)
insert.run(generation, next, 'items', JSON.stringify(itemId), ordinal(0), revised)
write.prepare('UPDATE journal_outcome_epochs SET through_seq = ?').run(next)
})
const before = savedBytes()
expect(restoreJournal(db(), IDENTITY.sessionId)).toMatchObject({ newer: { sequence: 1 } })
// No handle exists for a refused chat, so nothing can replace, roll over or rewind its epoch.
await expect(open(journals)).rejects.toMatchObject(SAVED_BY_NEWER_ORCA)
expect(savedBytes()).toEqual(before)
})
z.object({
sessionId: z.string(),
epoch: z.string(),
rows: z.array(z.string()),
expected: z.record(z.string(), z.unknown())
}).parse(
JSON.parse(
readFileSync(
new URL('../input-order/__fixtures__/journal-input-prefix.json', import.meta.url),
'utf8'
)
)
)
@@ -1,3 +1,4 @@
import { readTestJournalItems, readTestJournalSnapshot } from '../journal-observation.test-support'
import { createCodexHistoryCollector } from './journal-codex-history'
import { readRecord } from '../../../codex/app-server/codex-item-field-readers'
import { codexCommandPresentation } from '../../../codex/history/rollouts/codex-command-presentation'
@@ -145,7 +146,7 @@ it('revises an inherited turn without inheriting segment proof (source-derived c
fence: 1
})
expect(imported.ok).toBe(true)
const rows = c.journal.snapshot().items
const rows = await readTestJournalItems(c.journal)
const opener = rows.find((row) => row.body.kind === 'message' && row.body.role === 'user')!
expect(rows[0]?.body).toMatchObject({ kind: 'turn', userItemId: opener.itemId })
const command = rows.find((row) => row.body.kind === 'tool-call')?.body
@@ -172,7 +173,7 @@ it.each(['a', 'b', 'r24'] as const)(
options: { filePath }
})
expect(imported.ok).toBe(true)
const snapshot = c.journal.snapshot()
const snapshot = await readTestJournalSnapshot(c.journal)
const calls = snapshot.items.filter((row) => row.body.kind === 'tool-call')
expect(calls).toHaveLength(label === 'a' ? 4 : label === 'b' ? 3 : 1)
const blocks = projectStructuredItemsToNativeChat(snapshot.items).flatMap(
@@ -269,7 +270,7 @@ it.each(['a', 'b', 'r24'] as const)(
expect(calls[0]?.body).not.toHaveProperty('endedAs')
}
await c.reopen()
expect(c.journal.snapshot()).toEqual(snapshot)
expect(await readTestJournalSnapshot(c.journal)).toEqual(snapshot)
}
)
@@ -1,39 +0,0 @@
import type { createFixture } from './journal-terminal-validation-test-fixture'
import { expect, it } from 'vitest'
import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../shared/agent-session/journal/agent-session-journal-types'
import { agentJournalItemKey } from '../../../../shared/agent-session/journal/agent-session-journal-item-key'
export function registerJournalApprovalRevisionValidationTests(
fixture: ReturnType<typeof createFixture>
): void {
it('admits an answer on an exhausted approval without changing the saved item', async () => {
await fixture.saveOldItems([
{
itemId: agentJournalItemKey(fixture.identity),
revision: Number.MAX_SAFE_INTEGER + 1,
body: fixture.pendingApproval()
}
])
const before = fixture.journal.snapshot().items[0]
const cursor = fixture.journal.cursor()
await expect(
fixture.journal.appendItem(
fixture.identity,
{
...fixture.pendingApproval(),
kind: 'approval',
resolution: {
state: 'resolved',
selectedOptionId: null,
resolvedBy: 'user',
resolvedAt: 1_000
}
},
{ fence: 8, turnScope: AGENT_JOURNAL_THREAD_SCOPE }
)
).resolves.toBeDefined()
expect(fixture.journal.cursor().sequence).toBe(cursor.sequence + 1)
expect(fixture.journal.snapshot().items[0]).toEqual(before)
await fixture.expectSendStillWritable()
})
}
@@ -1,3 +1,4 @@
import { readTestJournalItems, readTestJournalSnapshot } from '../journal-observation.test-support'
import { afterEach, expect, it, vi } from 'vitest'
import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../shared/agent-session/journal/agent-session-journal-types'
import { AcpToolTimeline } from '../../../acp/acp-tool-timeline'
@@ -43,7 +44,7 @@ it.each(['direct', 'transition'] as const)(
output: journalPayload(text)
}
const cursor = rig.journal.cursor()
const snapshot = rig.journal.snapshot()
const snapshot = await readTestJournalSnapshot(rig.journal)
const blocked = rig.journal.readInOrder(() => {
expect(
rig.eventSink.tryAppendItem?.(
@@ -55,7 +56,7 @@ it.each(['direct', 'transition'] as const)(
return gate.promise
})
expect(rig.journal.cursor()).toEqual(cursor)
expect(rig.journal.snapshot()).toEqual(snapshot)
expect(await readTestJournalSnapshot(rig.journal)).toEqual(snapshot)
const append = () =>
path === 'direct'
? rig.eventSink.tryAppendItem?.(identity, body, options)
@@ -79,13 +80,13 @@ it.each(['direct', 'transition'] as const)(
})
await new Promise((resolve) => setImmediate(resolve))
expect(rig.journal.cursor()).toEqual(cursor)
expect(rig.journal.snapshot()).toEqual(snapshot)
expect(await readTestJournalSnapshot(rig.journal)).toEqual(snapshot)
expect(landed).toBe(false)
gate.resolve()
await blocked
expect(await written).toEqual({ ok: true })
const reopened = await rig.reopenJournal()
expect(reopened.snapshot().items.map((item) => item.body)).toEqual([
expect((await readTestJournalItems(reopened)).map((item) => item.body)).toEqual([
{ kind: 'status', tone: 'info', text: 'before' },
body
])
@@ -178,7 +179,7 @@ it.each(['acp', 'pi'] as const)(
}
expect(onFailed).not.toHaveBeenCalled()
const reopened = await rig.reopenJournal()
expect(reopened.snapshot().items.map((row) => row.body)).toEqual(
expect((await readTestJournalItems(reopened)).map((row) => row.body)).toEqual(
['one', 'two'].map((callId) =>
expect.objectContaining({
kind: 'tool-call',
@@ -1,3 +1,4 @@
import { readTestJournalItems } from '../journal-observation.test-support'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -67,7 +68,7 @@ export function registerJournalSubagentItemTests(): void {
await live.close()
const reopened = await open()
const row = reopened.snapshot().items.at(-1)!
const row = (await readTestJournalItems(reopened)).at(-1)!
expect(row.recovered).toBe(true)
expect(row.body).toMatchObject({
blocks: [
@@ -1,7 +0,0 @@
import { createFixture } from './journal-terminal-validation-test-fixture'
import { registerJournalApprovalRevisionValidationTests } from './journal-approval-revision-validation.spec'
export function registerJournalTerminalSettlementValidationTests(): void {
const fixture = createFixture()
registerJournalApprovalRevisionValidationTests(fixture)
}
@@ -1,178 +0,0 @@
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, expect, vi } from 'vitest'
import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../shared/agent-session/journal/agent-session-journal-types'
import { codexProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
import '../../agent-session-wire/termination/structured-agent-session-dead-generation-settlement'
import { settleStaleStructuredAgentSessionState } from '../../agent-session-wire/termination/structured-agent-session-stale-state-settlement'
import {
createTrackedJournalOpener,
insertTestJournalRowJson,
liveTestJournalRows,
openTestJournalHostDatabase
} from '../store-lifecycle/journal-host-database-test-support'
import { parseJournalRow } from '../durable-rows/journal-row-schema'
import type { AgentSessionJournal } from '../journal-store'
export function createFixture() {
const journals = createTrackedJournalOpener({ legacyInputs: true })
let root: string
let journal: AgentSessionJournal
const identity = { provider: 'orca' as const, clientMessageId: 'old-turn' }
async function open() {
return journals.open({
identity: {
sessionId: 'validation',
workspaceId: 'folder',
hostId: 'host',
agent: 'codex',
providerHandle: codexProviderHandle('thread')
},
stateDirectory: root,
now: () => 1_000
})
}
beforeEach(async () => {
root = await mkdtemp(join(tmpdir(), 'orca-settlement-validation-'))
journal = await open()
})
afterEach(async () => {
vi.restoreAllMocks()
await journals.closeAll()
await rm(root, { recursive: true, force: true })
})
function settle(observedAt = 1_000, lastProvenAliveAt?: number) {
return settleStaleStructuredAgentSessionState({
journal,
sessionId: 'validation',
fence: 8,
acquisitionGeneration: 'replacement',
deathEvidence: {
kind: lastProvenAliveAt === undefined ? 'exit-observed' : 'pid-absent',
detail: '',
ownerFence: 7,
observedAt,
...(lastProvenAliveAt === undefined ? {} : { lastProvenAliveAt })
}
})
}
async function expectSendStillWritable() {
const sequence = journal.cursor().sequence + 1
await expect(
journal.appendSubmission({
clientMessageId: 'new-send',
payloadFingerprint: 'new',
fence: 8,
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'hello' }] }
})
).resolves.toMatchObject({ sequence })
}
async function saveOldItems(inputs: { itemId: string; revision?: number; body: unknown }[]) {
const { db } = openTestJournalHostDatabase(root)
for (const [index, input] of inputs.entries()) {
const json = JSON.stringify({
v: 3,
kind: 'item',
epoch: journal.epoch,
seq: index + 2,
ts: 900,
fence: 7,
revision: 1,
...input,
turnScope: AGENT_JOURNAL_THREAD_SCOPE
})
expect(parseJournalRow(json).ok).toBe(true)
insertTestJournalRowJson(db, 'validation', index + 2, json, 900)
}
await journal.close()
journal = await open()
}
async function expectUnadvanceableItemUntouched(saved: {
itemId: string
revision: number
body: unknown
}) {
await saveOldItems([
saved,
{ itemId: 'orca:advanceable', body: { kind: 'turn', turnId: 'other', state: 'running' } },
{ itemId: 'orca:approval', body: pendingApproval() }
])
const before = journal.snapshot().items.find((item) => item.itemId === saved.itemId)
const { db } = openTestJournalHostDatabase(root)
const history = liveTestJournalRows(db, 'validation')
const commits = vi.fn()
journal.observeCommits(commits)
await expect(settle()).resolves.toBeGreaterThan(0)
expect(commits).toHaveBeenCalledTimes(1)
expect(journal.snapshot().items.find((item) => item.itemId === saved.itemId)).toEqual(before)
expect(journal.itemBody('orca:advanceable')).toMatchObject({ state: 'interrupted' })
expect(journal.itemBody('orca:approval')).toMatchObject({ resolution: { state: 'cancelled' } })
expect(liveTestJournalRows(db, 'validation').slice(0, history.length)).toEqual(history)
const settled = journal.snapshot()
await settle()
expect(journal.snapshot()).toEqual(settled)
expect(commits).toHaveBeenCalledTimes(1)
await journal.close()
journal = await open()
expect(journal.snapshot()).toEqual(settled)
}
function pendingApproval() {
return {
kind: 'approval',
title: 'Allow?',
detail: null,
options: [],
resolution: { state: 'pending', selectedOptionId: null, resolvedBy: null, resolvedAt: null }
}
}
function expectRewriteTimes(value: unknown) {
if (typeof value !== 'object' || value === null) {
return
}
for (const [key, field] of Object.entries(value)) {
if (key.endsWith('At') && field !== null && field !== undefined) {
expect(typeof field).toBe('number')
expect(Number.isFinite(field)).toBe(true)
expect(field).toBeGreaterThan(0)
}
expectRewriteTimes(field)
}
}
return {
expectRewriteTimes,
expectSendStillWritable,
expectUnadvanceableItemUntouched,
identity,
get journal() {
return journal
},
set journal(value: typeof journal) {
journal = value
},
journals,
open,
pendingApproval,
get root() {
return root
},
set root(value: typeof root) {
root = value
},
saveOldItems,
settle
}
}
@@ -155,7 +155,7 @@ it('requires a ready accepting child only while steering and rolls failed conver
kind: 'item',
identity: { provider: 'orca', clientMessageId: 'turn' },
body: { kind: 'turn', turnId: 'running', state: 'completed' },
options: { fence: 1 }
options: { fence: 1, turnScope: { kind: 'thread' } }
})
f.selection.child = null
const failed = await f.claim('failed', f.prepare(), [
@@ -5,12 +5,9 @@ import { afterEach, expect, it } from 'vitest'
import { codexProviderHandle } from '../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding'
import { openStructuredAgentSessionSqlJournalDatabase } from '../../runtime/agent-session/structured-agent-session-journal-open'
import type { JournalSqlDatabase } from './database/journal-sql-database'
import { AgentSessionJournal } from './journal-store'
import type { AgentSessionJournal } from './journal-store'
import { SqlAgentSessionJournal } from './journal-sql-store'
import {
closeTestJournalHostDatabase,
openTestJournalHostDatabase
} from './store-lifecycle/journal-host-database-test-support'
import { closeTestJournalHostDatabase } from './store-lifecycle/journal-host-database-test-support'
import {
journalSqlReaderPath,
journalSqlWorkerPath
@@ -44,24 +41,6 @@ type CreationJournal = Pick<AgentSessionJournal, 'reads' | 'appendTombstone'> &
): Promise<JournalStepsResult>
}
async function memory(root: string): Promise<CreationJournal> {
const journal = new AgentSessionJournal({
database: openTestJournalHostDatabase(root),
identity,
now: () => 100
})
await journal.open()
return {
reads: journal.reads,
appendTombstone: (...args) => journal.appendTombstone(...args),
appendSteps: (steps, beforeRun) =>
journal.appendSteps(
steps.map((step) => ({ kind: 'command' as const, step })),
beforeRun
)
}
}
async function sql(root: string): Promise<CreationJournal> {
const { journalDatabase } = await openStructuredAgentSessionSqlJournalDatabase({
stateDirectory: root,
@@ -73,55 +52,49 @@ async function sql(root: string): Promise<CreationJournal> {
return SqlAgentSessionJournal.open({ database: journalDatabase, identity, now: () => 100 })
}
it.each([
['memory', memory],
['sql', sql]
])(
'records creation once at its destination on the %s writer, including removed cards',
async (_name, open) => {
const root = await mkdtemp(join(tmpdir(), 'journal-creation-fact-'))
roots.push(root)
const journal = await open(root)
const step: Extract<JournalWriterStep, { kind: 'creation-fact' }> = {
kind: 'creation-fact',
epoch: (await journal.reads.physicalCursor()).epoch,
identity: { provider: 'orca', clientMessageId: 'agent-start:dispatch' },
body: {
kind: 'agent-start',
dispatchId: 'dispatch',
worker: {
party: { address: 'term_worker', terminalHandle: 'term_worker', orcaSessionId: null },
name: null
},
task: 'Build it'
it('records creation once at its destination on the SQL writer, including removed cards', async () => {
const root = await mkdtemp(join(tmpdir(), 'journal-creation-fact-'))
roots.push(root)
const journal = await sql(root)
const step: Extract<JournalWriterStep, { kind: 'creation-fact' }> = {
kind: 'creation-fact',
epoch: (await journal.reads.physicalCursor()).epoch,
identity: { provider: 'orca', clientMessageId: 'agent-start:dispatch' },
body: {
kind: 'agent-start',
dispatchId: 'dispatch',
worker: {
party: { address: 'term_worker', terminalHandle: 'term_worker', orcaSessionId: null },
name: null
},
options: { fence: 1, turnScope: { kind: 'thread' }, observedAt: 100 },
reservedBytes: 4096
}
expect(
await journal.appendSteps([step, { ...step, body: { ...step.body, task: 'Retry' } }])
).toEqual({
outcomes: [{ kind: 'committed', wrote: true }, { kind: 'dropped' }]
})
expect(await journal.reads.item('orca:agent-start%3Adispatch')).toMatchObject({
revision: 1,
body: { task: 'Build it' }
})
const removed = journal.appendTombstone(step.identity, { fence: 1 })
const replayed = journal.appendSteps([step])
await removed
expect(await replayed).toEqual({ outcomes: [{ kind: 'dropped' }] })
expect(await journal.reads.item('orca:agent-start%3Adispatch')).toBeNull()
const other = {
...step,
identity: { provider: 'orca' as const, clientMessageId: 'agent-start:other' }
}
expect(await journal.appendSteps([{ ...other, epoch: 'retired-epoch' }])).toEqual({
outcomes: [{ kind: 'dropped' }]
})
expect(await journal.appendSteps([other], () => false)).toEqual({
outcomes: [{ kind: 'dropped' }]
})
expect(await journal.reads.item('orca:agent-start%3Aother')).toBeNull()
task: 'Build it'
},
options: { fence: 1, turnScope: { kind: 'thread' }, observedAt: 100 },
reservedBytes: 4096
}
)
expect(
await journal.appendSteps([step, { ...step, body: { ...step.body, task: 'Retry' } }])
).toMatchObject({
outcomes: [{ kind: 'committed', wrote: true }, { kind: 'dropped' }]
})
expect(await journal.reads.item('orca:agent-start%3Adispatch')).toMatchObject({
revision: 1,
body: { task: 'Build it' }
})
const removed = journal.appendTombstone(step.identity, { fence: 1 })
const replayed = journal.appendSteps([step])
await removed
expect(await replayed).toMatchObject({ outcomes: [{ kind: 'dropped' }] })
expect(await journal.reads.item('orca:agent-start%3Adispatch')).toBeNull()
const other = {
...step,
identity: { provider: 'orca' as const, clientMessageId: 'agent-start:other' }
}
expect(await journal.appendSteps([{ ...other, epoch: 'retired-epoch' }])).toMatchObject({
outcomes: [{ kind: 'dropped' }]
})
expect(await journal.appendSteps([other], () => false)).toMatchObject({
outcomes: [{ kind: 'dropped' }]
})
expect(await journal.reads.item('orca:agent-start%3Aother')).toBeNull()
})
@@ -1,7 +1,9 @@
import type { JournalStopSettleFact } from './journal-writer-host-facts'
import type Database from '../../sqlite/sync-database'
import type { AgentJournalCursor } from '../../../shared/agent-session/journal/agent-session-journal-identity'
import type { AgentSessionJournalProviderHandle } from '../../../shared/agent-session/journal/agent-session-journal-identity'
import type {
AgentJournalCursor,
AgentSessionJournalProviderHandle
} from '../../../shared/agent-session/journal/agent-session-journal-identity'
import {
journalHistoryOrigin,
sameJournalConversation,
@@ -62,16 +64,15 @@ export function readJournalDomainContextHead(db: Database.Database, target: Targ
if (!head) {
throw new Error('journal context command targets a retired chat epoch')
}
const historyOrigin: JournalHistoryOrigin | null =
head.history_origin_json === null ? null : JSON.parse(String(head.history_origin_json))
return {
lastSequence: Number(head.last_sequence),
highestFence: Number(head.highest_fence),
contextBoundary: Number(head.context_boundary),
cleared: head.queue_clear_operation_id !== null,
failureBoundary: Number(head.queue_failure_boundary),
historyOrigin:
head.history_origin_json === null
? null
: (JSON.parse(String(head.history_origin_json)) as JournalHistoryOrigin)
historyOrigin
}
}
@@ -13,9 +13,8 @@ import { agentTurnVerdict } from '../../../shared/agent-status/lifecycle/agent-t
import type { JournalWriterOperation } from './journal-writer-command'
import type { JournalWriterCoordinate } from './journal-writer-effects'
import type { JournalStopSettleFact } from './journal-writer-host-facts'
import type { JournalAppendResult } from './journal-store-contracts'
import type { JournalAppendResult, JournalStopAcceptance } from './journal-store-contracts'
import type { JournalStopEvent } from './durable-rows/journal-row-schema'
import type { JournalStopAcceptance } from './journal-store-contracts'
import {
foldJournalQueuePauseMark,
journalManualStop,
@@ -163,6 +163,10 @@ export function writeJournalDomainCurrentItem(
if (
isTurnOrClear(next) ||
isTurnOrClear(input.previous?.item) ||
next.body.kind === 'approval' ||
next.body.kind === 'question' ||
input.previous?.item.body.kind === 'approval' ||
input.previous?.item.body.kind === 'question' ||
(metadata.root && next.body.kind === 'message' && next.body.role === 'user') ||
(input.previous?.item.body.kind === 'message' && input.previous.item.body.role === 'user') ||
isStructuredAgentSessionCommandEntry(next.body) ||
@@ -268,3 +268,93 @@ it('uses the visible named opener to preserve historical replay instead of settl
endedAs: 'interrupted'
})
})
it('settles dead and older sends while retaining a continuing handover', async () => {
const f = domainLifecycleFixture(cleanups)
for (const [id, fence] of [
['old', 1],
['exited', 2],
['continuing', 2]
] as const) {
await f.run({
kind: 'submission',
input: {
clientMessageId: id,
payloadFingerprint: id,
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: id }] },
fence
}
})
}
await f.run({
kind: 'dead-generation-settle',
input: {
sessionId: 'chat',
fence: 3,
below: 2,
deathEvidence: null,
exit: {
ownerFence: 2,
settlementId: 'exit',
verdict: { state: 'interrupted', completedAt: 800 },
pendingSubmissionReason: 'provider_exited',
showUnexpectedExitOutcome: false
},
continuingInputs: ['continuing'],
finalizingInputs: []
}
})
const entries = f.db
.prepare(
'SELECT message_id,dispatch_state FROM journal_submission_attempts WHERE current=1 ORDER BY ordinal'
)
.all()
expect(entries).toEqual([
{ message_id: 'old', dispatch_state: 'unknown' },
{ message_id: 'exited', dispatch_state: 'unknown' },
{ message_id: 'continuing', dispatch_state: 'pending' }
])
})
it('keeps a continuing send pending and avoids an exit notice for an owned finalizer', async () => {
const f = domainLifecycleFixture(cleanups)
for (const id of ['continuing', 'own-finalizer']) {
await f.run({
kind: 'submission',
input: {
clientMessageId: id,
payloadFingerprint: id,
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: id }] },
fence: 1
}
})
}
const settled = await f.run({
kind: 'dead-generation-settle',
input: {
sessionId: 'chat',
fence: 2,
below: 1,
deathEvidence: null,
exit: {
ownerFence: 1,
settlementId: 'exit',
verdict: { state: 'unverifiable' },
pendingSubmissionReason: 'provider_exited'
},
continuingInputs: ['continuing'],
finalizingInputs: [{ clientMessageId: 'own-finalizer', settlementOwner: true }]
}
})
expect(
f.db
.prepare(
'SELECT message_id,dispatch_state FROM journal_submission_attempts WHERE current=1 ORDER BY ordinal'
)
.all()
).toEqual([
{ message_id: 'continuing', dispatch_state: 'pending' },
{ message_id: 'own-finalizer', dispatch_state: 'unknown' }
])
expect(settled.publication?.items).toEqual([])
})
@@ -3,13 +3,20 @@ import type { AgentSessionHistoryResult } from '../../../shared/agent-session/ag
import type { AgentJournalCursor } from '../../../shared/agent-session/journal/agent-session-journal-identity'
import type {
AgentJournalRenderItem,
AgentJournalSnapshot
AgentJournalSnapshot,
AgentJournalSubmission
} from '../../../shared/agent-session/journal/agent-session-journal-types'
import { readAgentSessionForwardHistory } from '../agent-session-wire/publication/history/agent-session-history-forward-page'
import type { HistoryReader } from '../agent-session-wire/publication/history/agent-session-history-page-bounds'
import type { QueuedImageHistoryPolicy } from '../agent-session-wire/publication/history/agent-session-queued-image-reader'
import type { JournalReadSince } from './journal-store-contracts'
import type { JournalOutcomeReadRow, JournalReadRow } from './replay/journal-read-row'
import { agentJournalSubmissionKey } from '../../../shared/agent-session/journal/agent-session-journal-item-key'
import {
readJournalDomainSourceHeaders,
readJournalDomainSourceBody
} from './journal-domain-source-items'
import { JournalSqlScope } from './journal-sql-scope'
import { JOURNAL_DOMAIN_HISTORY_ITEM_BYTES } from './journal-domain-history-window'
import { findSequenceGap, resolveJournalResume } from './replay/journal-cursor'
import type { JournalDomainEventChanges } from './journal-domain-schema'
@@ -26,19 +33,23 @@ export function readJournalDomainForwardHistory(
readSnapshot: (ids: Set<string>) => AgentJournalSnapshot,
reset: (
reason: Extract<AgentSessionHistoryResult, { ok: false }>['reset']
) => AgentSessionHistoryResult
) => AgentSessionHistoryResult,
queuedImages?: QueuedImageHistoryPolicy
): AgentSessionHistoryResult {
// One bounded identity read serves the size admission and the builder's lookahead.
const since = cursor && readJournalDomainEvents(db, head, cursor, limit + 1)
const admitted = since?.ok
? fittingEventCount(db, head, since.rows.slice(0, limit), reader.budgetBytes)
: limit
const links = queuedImages && new Map(queuedImages.links)
const since = cursor && readJournalDomainEvents(db, head, cursor, limit + 1, links)
const admitted =
since?.ok && queuedImages === undefined
? fittingEventCount(db, head, since.rows.slice(0, limit), reader.budgetBytes)
: limit
let materialized: AgentJournalSnapshot | undefined
return readAgentSessionForwardHistory(
{
readSince: (after, count = limit + 1) =>
since?.ok
? firstEvents(since, count)
: (since ?? readJournalDomainEvents(db, head, after, count)),
: (since ?? readJournalDomainEvents(db, head, after, count, links)),
canonicalItemId: (itemId) => itemId,
hasSubmission: () => false,
reset
@@ -52,7 +63,16 @@ export function readJournalDomainForwardHistory(
cursor,
admitted,
reader,
readSnapshot
(ids) => {
materialized ??= readSnapshot(ids)
return {
...materialized,
items: materialized.items.filter((item) => ids.has(item.itemId)),
submissions: materialized.submissions.filter((send) =>
ids.has(agentJournalSubmissionKey(send.clientMessageId))
)
}
}
)
}
@@ -108,7 +128,8 @@ function readJournalDomainEvents(
db: Database.Database,
head: JournalDomainHistoryHead,
cursor: AgentJournalCursor,
limit: number
limit: number,
queueLinks?: Map<string, readonly string[]>
): JournalReadSince {
const resume = resolveJournalResume(
{
@@ -143,7 +164,11 @@ function readJournalDomainEvents(
kind: 'outcome',
epoch: head.cursor.epoch,
seq: Number(row.sequence),
itemIds: [...changes.items.map((item) => item.itemId), ...(changes.removedItemIds ?? [])],
itemIds: [
...changes.items.map((item) => item.itemId),
...(changes.removedItemIds ?? []),
...(queueLinks ? changedQueueSourceKeys(db, head, changes.cards, queueLinks) : [])
],
submissionIds: changes.submissions
})
}
@@ -155,6 +180,30 @@ function readJournalDomainEvents(
}
}
function changedQueueSourceKeys(
db: Database.Database,
head: JournalDomainHistoryHead,
cards: readonly string[],
links: Map<string, readonly string[]>
): string[] {
return cards.flatMap((id) => {
const known = links.get(id)
if (known) {
return known.map(agentJournalSubmissionKey)
}
const row = db
.prepare(`SELECT json_extract(payload_json,'$.recovers') AS recovers,
json_extract(payload_json,'$.consumedAs') AS consumed_as FROM journal_queue_cards
WHERE session_id=? AND epoch=? AND message_id=?`)
.get(head.sessionId, head.epoch, id)
const keys = [id, row?.recovers, row?.consumed_as].filter(
(value): value is string => typeof value === 'string'
)
links.set(id, keys)
return keys.map(agentJournalSubmissionKey)
})
}
function eventChanges(json: string): JournalDomainEventChanges | null {
let changes: JournalDomainEventChanges
try {
@@ -171,3 +220,67 @@ function eventChanges(json: string): JournalDomainEventChanges | null {
? changes
: null
}
/** Current attempts only, in submission order then stable admission ordinal. */
export function itemSubmissions(
db: Database.Database,
head: JournalDomainHistoryHead,
itemIds: readonly string[],
present: (send: AgentJournalSubmission) => AgentJournalSubmission = (send) => send,
ordinals?: Map<string, number>
): AgentJournalSubmission[] {
return db
.prepare(`SELECT attempt.payload_json, attempt.ordinal FROM json_each(?) wanted
CROSS JOIN journal_submission_attempts attempt
WHERE attempt.session_id=? AND attempt.epoch=? AND attempt.current=1 AND attempt.item_id=wanted.value
ORDER BY attempt.submitted_at, attempt.ordinal`)
.all(JSON.stringify(itemIds), head.sessionId, head.epoch)
.map((row) => {
const send = present(JSON.parse(String(row.payload_json)))
ordinals?.set(send.clientMessageId, Number(row.ordinal))
return send
})
}
export function readJournalDomainHistorySnapshot(
db: Database.Database,
head: JournalDomainHistoryHead,
ids: ReadonlySet<string>,
sourceIds: readonly string[] = [],
present?: (send: AgentJournalSubmission) => AgentJournalSubmission
): AgentJournalSnapshot {
const wanted = [...ids]
return {
sessionId: head.sessionId,
cursor: head.cursor,
items: [
...db
.prepare(`SELECT COALESCE(item.wire_json,item.payload_json) AS payload_json FROM json_each(?) wanted
CROSS JOIN journal_items item
WHERE item.session_id=? AND item.epoch=? AND item.item_id=wanted.value AND item.hidden=0
ORDER BY item.sequence, item.sequence_index, item.ordinal`)
.all(JSON.stringify(wanted), head.sessionId, head.epoch)
.map((row) => JSON.parse(String(row.payload_json))),
...readJournalDomainSourceHeaders(
new JournalSqlScope(db, head.sessionId, head.epoch),
sourceIds.filter((id) => ids.has(agentJournalSubmissionKey(id)))
)
.toReversed()
.map((header) =>
readJournalDomainSourceBody(new JournalSqlScope(db, head.sessionId, head.epoch), header)
)
],
submissions: itemSubmissions(db, head, wanted, present)
}
}
export function latestTurnItem(
db: Database.Database,
head: JournalDomainHistoryHead
): AgentJournalRenderItem | null {
const row = db
.prepare(`SELECT COALESCE(wire_json,payload_json) AS payload_json FROM journal_items WHERE session_id=? AND epoch=? AND hidden=0
AND turn_marker=1 ORDER BY sequence DESC, sequence_index DESC, ordinal DESC LIMIT 1`)
.get(head.sessionId, head.epoch)
return row ? JSON.parse(String(row.payload_json)) : null
}
@@ -1,6 +1,8 @@
import { boundedFrameItem } from '../agent-session-wire/publication/history/agent-session-bounded-frame-item'
import { agentJournalItemForWire } from '../agent-session-wire/publication/history/agent-session-item-wire-form'
import { BOUNDED_FRAME_CONTENT_BUDGET_BYTES } from '../agent-session-wire/publication/history/agent-session-history-page-bounds'
import {
agentJournalItemForWire,
CLIENT_HISTORY_READER
} from '../agent-session-wire/publication/history/agent-session-item-wire-form'
import type Database from '../../sqlite/sync-database'
import {
AGENT_SESSION_HISTORY_MAX_LIMIT,
@@ -13,8 +15,8 @@ import type {
AgentJournalSnapshot,
AgentJournalSubmission
} from '../../../shared/agent-session/journal/agent-session-journal-types'
import type { AgentJournalCursor } from '../../../shared/agent-session/journal/agent-session-journal-identity'
import {
BOUNDED_FRAME_CONTENT_BUDGET_BYTES,
historyContentBytes,
oversizedHistoryItem,
resolveHistoryLimit,
@@ -22,12 +24,28 @@ import {
type HistoryReader
} from '../agent-session-wire/publication/history/agent-session-history-page-bounds'
import { buildAgentSessionHistoryPage } from '../agent-session-wire/publication/history/agent-session-history-page-result'
import { CLIENT_HISTORY_READER } from '../agent-session-wire/publication/history/agent-session-item-wire-form'
import {
queuedImageSubmissionPresenter,
type QueuedImageHistoryPolicy
} from '../agent-session-wire/publication/history/agent-session-queued-image-reader'
import { JournalSqlScope } from './journal-sql-scope'
import {
readJournalDomainSourceHeaders,
readJournalDomainSourceBody,
isJournalDomainSourceHeader
} from './journal-domain-source-items'
import { journalContextEpoch } from './context-epochs/journal-context-cursor'
import { readJournalDomainForwardHistory } from './journal-domain-history-after'
import {
latestTurnItem,
readJournalDomainForwardHistory,
readJournalDomainHistorySnapshot,
itemSubmissions
} from './journal-domain-history-after'
import { readJournalDomainHistoryRoster } from './journal-domain-history-roster'
import {
admitJournalDomainHistoryGroups,
readJournalDomainHistoryEnvelope,
journalDomainSequenceGroups,
selectJournalDomainHistoryWindow,
type JournalDomainHistoryHeader
} from './journal-domain-history-window'
@@ -37,18 +55,24 @@ export type JournalDomainHistoryRequest = Omit<AgentSessionHistoryRequest, 'dire
fence?: number
scope?: 'every-agent' | 'own-agent'
rowReplay?: boolean
reader?: { form: 'client' | 'bounded'; budgetBytes?: number }
reader?: {
form: 'client' | 'bounded'
budgetBytes?: number
queuedImages?: QueuedImageHistoryPolicy
}
}
export type JournalDomainHistoryHead = {
sessionId: string
epoch: string
/** Public context identity of the physical epoch. */
cursor: AgentJournalCursor
cursor: AgentJournalSnapshot['cursor']
retainedSequence: number
}
type HistoryReaderForm = Pick<HistoryReader, 'form' | 'budgetBytes'>
type HistoryReaderForm = Pick<HistoryReader, 'form' | 'budgetBytes'> & {
presentSubmission?: (send: AgentJournalSubmission) => AgentJournalSubmission
}
/** The caller owns a readonly connection; head, headers, bodies and any reset share one snapshot. */
export function readJournalDomainHistoryPage(
@@ -61,14 +85,17 @@ export function readJournalDomainHistoryPage(
}
: {
...CLIENT_HISTORY_READER,
...(request.reader?.budgetBytes !== undefined
? { budgetBytes: request.reader.budgetBytes }
: {})
budgetBytes: request.reader?.budgetBytes ?? CLIENT_HISTORY_READER.budgetBytes
}
): AgentSessionHistoryResult {
db.exec('BEGIN')
try {
const result = readHistory(db, readHead(db, request.sessionId), request, reader)
const result = readHistory(db, readHead(db, request.sessionId), request, {
...reader,
...(request.reader?.queuedImages
? { presentSubmission: queuedImageSubmissionPresenter(request.reader.queuedImages) }
: {})
})
db.exec('COMMIT')
return result
} catch (error) {
@@ -123,8 +150,16 @@ function readHistory(
resolveHistoryLimit(request.limit),
reader,
latestTurnItem(db, head),
(ids) => currentRows(db, head, ids),
reset
(ids) =>
readJournalDomainHistorySnapshot(
db,
head,
ids,
queuedImageSourceIds(request),
reader.presentSubmission
),
reset,
request.reader?.queuedImages
)
}
const cursor = request.direction === 'before' ? request.cursor : undefined
@@ -151,19 +186,39 @@ function historyPage(
request.direction === 'hydrate'
? AGENT_SESSION_HISTORY_MAX_LIMIT
: resolveHistoryLimit(request.limit)
const admitted = admitJournalDomainHistoryGroups(
selectJournalDomainHistoryWindow(db, where, prefix, end, limit),
reader.budgetBytes
)
const submissions = itemSubmissions(
db,
head,
admitted.groups.flat().map((header) => header.itemId)
)
const kept = admitted.oversized
? [admitted.groups[0]!.map((header) => oversizedHistoryItem(itemEnvelope(db, prefix, header)))]
: formHistoryGroups(db, head, admitted.groups, submissions, reader)
const items = kept.toReversed().flat()
const sourceWindow = (window: Parameters<typeof readJournalDomainSourceHeaders>[2]) =>
readJournalDomainSourceHeaders(
new JournalSqlScope(db, ...prefix),
queuedImageSourceIds(request) ?? [],
{ ...window, own: request.scope === 'own-agent' }
)
let sources = sourceWindow({ before: end, limit: 2 * limit })
if (sources.length === 2 * limit) {
const boundary = sources.at(-1)!.sequence
sources = [
...sources.filter((source) => source.sequence > boundary),
...sourceWindow({ before: boundary + 1, from: boundary })
]
}
const window = selectJournalDomainHistoryWindow(db, where, prefix, end, limit, sources)
const admitted = request.reader?.queuedImages
? { groups: [...journalDomainSequenceGroups(window)], oversized: false }
: admitJournalDomainHistoryGroups(window, reader.budgetBytes)
const { groups, submissions } = admitted.oversized
? {
groups: [
admitted.groups[0]!.map((header) =>
oversizedHistoryItem(readJournalDomainHistoryEnvelope(db, prefix, header.itemId))
)
],
submissions: itemSubmissions(
db,
head,
admitted.groups[0]!.map((header) => header.itemId)
)
}
: formHistoryGroups(db, head, admitted.groups, reader)
const items = groups.toReversed().flat()
const oldest = items[0]?.sequence ?? end
const turn = latestTurnItem(db, head)
return buildAgentSessionHistoryPage({
@@ -175,14 +230,16 @@ function historyPage(
},
direction: request.direction === 'hydrate' ? 'tail' : request.direction,
items,
hasOlder: !!db
.prepare(`SELECT 1 FROM journal_items WHERE ${where} AND sequence<? LIMIT 1`)
.get(...prefix, oldest),
hasOlder:
!!db
.prepare(`SELECT 1 FROM journal_items WHERE ${where} AND sequence<? LIMIT 1`)
.get(...prefix, oldest) || sourceWindow({ before: oldest, limit: 1 }).length > 0,
hasNewer:
cursor !== undefined &&
!!db
(!!db
.prepare(`SELECT 1 FROM journal_items WHERE ${where} AND sequence>=? LIMIT 1`)
.get(...prefix, end),
.get(...prefix, end) ||
sourceWindow({ from: end, limit: 1 }).length > 0),
fallbackCursor: cursor ?? { epoch: head.cursor.epoch, sequence: 0 },
nextCursor: items[0] ? { epoch: head.cursor.epoch, sequence: items[0].sequence } : undefined,
fence: request.fence,
@@ -190,102 +247,56 @@ function historyPage(
})
}
/** The existing byte rule, authoritative over admitted groups only. */
/** Form only positional candidates through the first exact non-fitting sequence group. */
function formHistoryGroups(
db: Database.Database,
head: JournalDomainHistoryHead,
groups: readonly JournalDomainHistoryHeader[][],
submissions: readonly AgentJournalSubmission[],
reader: HistoryReaderForm
): AgentJournalRenderItem[][] {
const sizes = submissionBytesByItemId(submissions)
): { groups: AgentJournalRenderItem[][]; submissions: AgentJournalSubmission[] } {
const body = db.prepare(
'SELECT COALESCE(wire_json,payload_json) AS wire_json FROM journal_items WHERE session_id=? AND epoch=? AND item_id=?'
)
const kept: AgentJournalRenderItem[][] = []
const submissions: AgentJournalSubmission[] = []
const ordinals = new Map<string, number>()
let total = 0
for (const group of groups) {
const sends = itemSubmissions(
db,
head,
group.map((header) => header.itemId),
reader.presentSubmission,
ordinals
)
const formed = group.map((header) =>
reader.form(
JSON.parse(String(body.get(head.sessionId, head.epoch, header.itemId)!.wire_json))
isJournalDomainSourceHeader(header)
? readJournalDomainSourceBody(new JournalSqlScope(db, head.sessionId, head.epoch), header)
: JSON.parse(String(body.get(head.sessionId, head.epoch, header.itemId)!.wire_json))
)
)
const bytes = historyContentBytes(formed, [], sizes)
const bytes = historyContentBytes(formed, [], submissionBytesByItemId(sends))
if (kept.length > 0 && total + bytes > reader.budgetBytes) {
break
}
kept.push(
formed.map(({ item }) => (bytes > reader.budgetBytes ? oversizedHistoryItem(item) : item))
)
submissions.push(...sends)
total += bytes
if (bytes > reader.budgetBytes) {
break
}
}
return kept
submissions.sort(
(a, b) =>
a.submittedAt - b.submittedAt ||
ordinals.get(a.clientMessageId)! - ordinals.get(b.clientMessageId)!
)
return { groups: kept, submissions }
}
/** An oversized group's placeholders keep each row's identity and placement, never its body. */
function itemEnvelope(
db: Database.Database,
prefix: readonly [string, string],
header: JournalDomainHistoryHeader
): AgentJournalRenderItem {
const row = db
.prepare(`SELECT placement_json AS envelope,revision,sequence,sequence_index FROM journal_items
WHERE session_id=? AND epoch=? AND item_id=?`)
.get(...prefix, header.itemId)!
// The placeholder replaces the omitted body.
return {
...JSON.parse(String(row.envelope)),
revision: Number(row.revision),
sequence: Number(row.sequence),
sequenceIndex: Number(row.sequence_index)
}
}
function latestTurnItem(
db: Database.Database,
head: JournalDomainHistoryHead
): AgentJournalRenderItem | null {
const row = db
.prepare(`SELECT COALESCE(wire_json,payload_json) AS payload_json FROM journal_items WHERE session_id=? AND epoch=? AND hidden=0
AND turn_marker=1 ORDER BY sequence DESC, sequence_index DESC, ordinal DESC LIMIT 1`)
.get(head.sessionId, head.epoch)
return row ? JSON.parse(String(row.payload_json)) : null
}
/** Current attempts only, in submission order then stable admission ordinal. */
function itemSubmissions(
db: Database.Database,
head: JournalDomainHistoryHead,
itemIds: readonly string[]
): AgentJournalSubmission[] {
return db
.prepare(`SELECT attempt.payload_json FROM json_each(?) wanted
CROSS JOIN journal_submission_attempts attempt
WHERE attempt.session_id=? AND attempt.epoch=? AND attempt.current=1 AND attempt.item_id=wanted.value
ORDER BY attempt.submitted_at, attempt.ordinal`)
.all(JSON.stringify(itemIds), head.sessionId, head.epoch)
.map((row) => JSON.parse(String(row.payload_json)))
}
function currentRows(
db: Database.Database,
head: JournalDomainHistoryHead,
ids: ReadonlySet<string>
): AgentJournalSnapshot {
const wanted = [...ids]
return {
sessionId: head.sessionId,
cursor: head.cursor,
items: db
.prepare(`SELECT COALESCE(item.wire_json,item.payload_json) AS payload_json FROM json_each(?) wanted
CROSS JOIN journal_items item
WHERE item.session_id=? AND item.epoch=? AND item.item_id=wanted.value AND item.hidden=0
ORDER BY item.sequence, item.sequence_index, item.ordinal`)
.all(JSON.stringify(wanted), head.sessionId, head.epoch)
.map((row) => JSON.parse(String(row.payload_json))),
submissions: itemSubmissions(db, head, wanted)
}
function queuedImageSourceIds(request: JournalDomainHistoryRequest): string[] | undefined {
return request.reader?.queuedImages?.ids.filter(([, active]) => active).map(([id]) => id)
}
@@ -1,3 +1,4 @@
import type { AgentJournalRenderItem } from '../../../shared/agent-session/journal/agent-session-journal-types'
import type Database from '../../sqlite/sync-database'
import type { SqliteRow } from '../../sqlite/sqlite-statement'
import { MAX_JOURNAL_LIFECYCLE_BATCH_MUTATIONS } from './durable-rows/journal-row-schema'
@@ -5,9 +6,11 @@ import { MAX_JOURNAL_LIFECYCLE_BATCH_MUTATIONS } from './durable-rows/journal-ro
export type JournalDomainHistoryHeader = {
itemId: string
sequence: number
sequenceIndex: number
ordinal: number
own: boolean
subagentGroupKey: string | null
/** Sent item plus current submission bytes, read without loading either body. */
/** Stored size without body reads; retained sources still need exact reader-form admission. */
bytes: number
}
@@ -23,9 +26,20 @@ export function selectJournalDomainHistoryWindow(
where: string,
prefix: readonly [string, string],
end: number,
limit: number
limit: number,
sources: readonly JournalDomainHistoryHeader[] = []
): JournalDomainHistoryHeader[] {
return historyWindow(selectJournalDomainHistoryHeaders(db, where, prefix, end, limit), limit)
const headers = [
...selectJournalDomainHistoryHeaders(db, where, prefix, end, limit),
...sources.filter((source) => source.sequence < end)
].sort(
(a, b) => b.sequence - a.sequence || b.sequenceIndex - a.sequenceIndex || b.ordinal - a.ordinal
)
const boundary = headers[2 * limit - 1]?.sequence
return historyWindow(
boundary === undefined ? headers : headers.filter((header) => header.sequence >= boundary),
limit
)
}
/** Newest-first keyset over every scoped row: the existing 2×limit reach also caps sparse-root chats.
@@ -37,7 +51,7 @@ function selectJournalDomainHistoryHeaders(
end: number,
limit: number
): JournalDomainHistoryHeader[] {
const columns = `item_id, sequence, root, subagent_row, subagent_group_key,
const columns = `item_id, sequence, sequence_index, ordinal, root, subagent_row, subagent_group_key,
${JOURNAL_DOMAIN_HISTORY_ITEM_BYTES} AS bytes`
const order = 'ORDER BY sequence DESC, sequence_index DESC, ordinal DESC'
const cap = 2 * limit
@@ -63,6 +77,8 @@ function selectJournalDomainHistoryHeaders(
return rows.map((row: SqliteRow) => ({
itemId: String(row.item_id),
sequence: Number(row.sequence),
sequenceIndex: Number(row.sequence_index),
ordinal: Number(row.ordinal),
own: row.root === 1 && row.subagent_row === 0,
subagentGroupKey: row.subagent_group_key === null ? null : String(row.subagent_group_key),
bytes: Number(row.bytes)
@@ -142,3 +158,22 @@ export function* journalDomainSequenceGroups<T extends { sequence: number }>(
yield group.toReversed()
}
}
/** An oversized group's placeholders keep each row's identity and placement, never its body. */
export function readJournalDomainHistoryEnvelope(
db: Database.Database,
prefix: readonly [string, string],
itemId: string
): AgentJournalRenderItem {
const row = db
.prepare(`SELECT placement_json AS envelope,revision,sequence,sequence_index FROM journal_items
WHERE session_id=? AND epoch=? AND item_id=?`)
.get(...prefix, itemId)!
// The placeholder replaces the omitted body.
return {
...JSON.parse(String(row.envelope)),
revision: Number(row.revision),
sequence: Number(row.sequence),
sequenceIndex: Number(row.sequence_index)
}
}
@@ -5,12 +5,17 @@ import { afterEach, expect, it } from 'vitest'
import Database from '../../sqlite/sync-database'
import type { AgentSessionHistoryResult } from '../../../shared/agent-session/agent-session-wire'
import type { AgentJournalRenderItem } from '../../../shared/agent-session/journal/agent-session-journal-types'
import { measureHistoryItem } from '../agent-session-wire/publication/history/agent-session-history-page-bounds'
import {
measureHistoryItem,
HISTORY_PAGE_CONTENT_BUDGET_BYTES,
BOUNDED_FRAME_CONTENT_BUDGET_BYTES
} from '../agent-session-wire/publication/history/agent-session-history-page-bounds'
import { message, messageFixture, target } from './journal-sql-message.test-fixture'
import {
readJournalDomainHistoryPage,
type JournalDomainHistoryRequest
} from './journal-domain-history-page'
import { insertJournalDomainQueueRow } from './journal-domain-queue-rows'
const cleanup: (() => void)[] = []
afterEach(() => {
@@ -214,6 +219,177 @@ it('excludes a retained body and submission by stored size before reading either
expect(forward.page).toMatchObject({ hasNewer: true, window: { nextCursor: { sequence: 1 } } })
})
it('windows retained image sources before reading their bodies and admits their exact reader form', () => {
const { db, seedItem, seedSend, head, event, read } = history()
seedSend()
db.prepare(`UPDATE journal_submission_attempts SET facts_json=json_set(facts_json,
'$.admission.sourcePlacement',json(?)), admission_body_json=? WHERE message_id='send'`).run(
JSON.stringify({
itemId: 'orca:send',
revision: 0,
sequence: 1,
observedAt: 1,
turnScope: { kind: 'thread' }
}),
JSON.stringify({
kind: 'message',
role: 'user',
blocks: [
{ type: 'text', text: 'x'.repeat(2 * HISTORY_PAGE_CONTENT_BUDGET_BYTES) },
{ type: 'image-ref', path: '/image.png' }
]
})
)
db.prepare(`UPDATE journal_submission_attempts SET payload_json=json_set(payload_json,
'$.keptAsQueuedMessageId',?) WHERE message_id='send'`).run('card'.repeat(4096))
db.exec("UPDATE journal_items SET hidden=1 WHERE item_id='orca:send'")
for (let sequence = 2; sequence <= 5; sequence++) {
seedItem(text(sequence, 'visible'))
event(sequence, sequence === 3 ? ['orca:send'] : [`orca:${sequence}:0`])
}
head(5, 1)
const reader = {
form: 'client' as const,
budgetBytes: HISTORY_PAGE_CONTENT_BUDGET_BYTES + 4096,
queuedImages: { ids: [['send', true] as const], imageIds: ['send'] }
}
const original = db
.prepare("SELECT admission_body_json FROM journal_submission_attempts WHERE message_id='send'")
.get()!.admission_body_json
db.exec(
"UPDATE journal_submission_attempts SET admission_body_json='invalid' WHERE message_id='send'"
)
const tail = read({ direction: 'tail', limit: 2, reader })
expect(sequences(tail)).toEqual([4, 5])
const middle = read({
direction: 'before',
cursor: tail.page.window.nextCursor,
limit: 2,
reader
})
expect(sequences(middle)).toEqual([2, 3])
expect(middle.page.hasOlder).toBe(true)
db.prepare(
"UPDATE journal_submission_attempts SET admission_body_json=? WHERE message_id='send'"
).run(original)
const oldest = read({
direction: 'before',
cursor: middle.page.window.nextCursor,
limit: 2,
reader
})
expect(oldest.page.items).toMatchObject([
{ itemId: 'orca:send', sequence: 1, shortened: true, body: { kind: 'message' } }
])
expect(oldest.page.hasOlder).toBe(false)
expect(oldest.page.submissions[0]).not.toHaveProperty('keptAsQueuedMessageId')
const forward = read({ direction: 'after', cursor: { epoch: 'epoch', sequence: 1 }, reader })
expect(sequences(forward)).toEqual([1, 2, 4, 5])
expect(forward.page).toMatchObject({ hasNewer: false, window: { nextCursor: { sequence: 5 } } })
const excluded = read({
direction: 'after',
cursor: { epoch: 'epoch', sequence: 1 },
limit: 2,
reader: { ...reader, budgetBytes: 4096 }
})
expect(sequences(excluded)).toEqual([2])
expect(excluded.page).toMatchObject({ hasNewer: true, window: { nextCursor: { sequence: 2 } } })
const bounded = {
...reader,
form: 'bounded' as const,
budgetBytes: BOUNDED_FRAME_CONTENT_BUDGET_BYTES
}
expect(
read({ direction: 'before', cursor: middle.page.window.nextCursor, reader: bounded }).page.items
).toMatchObject([{ itemId: 'orca:send', shortened: true, body: { kind: 'message' } }])
expect(
read({ direction: 'after', cursor: { epoch: 'epoch', sequence: 1 }, reader: bounded }).page
).toMatchObject({
items: [{ itemId: 'orca:send' }, { sequence: 2 }, { sequence: 4 }, { sequence: 5 }],
window: { nextCursor: { sequence: 5 } }
})
})
it('keeps visible rows first and retained sources in relation order across a whole boundary sequence', () => {
const { db, seedItem, seedSend, head, read } = history()
seedSend()
db.prepare(`UPDATE journal_submission_attempts SET facts_json=json_set(facts_json,
'$.admission.sourcePlacement',json(?)) WHERE message_id='send'`).run(
JSON.stringify({
itemId: 'orca:send',
revision: 0,
sequence: 7,
sequenceIndex: 0,
observedAt: 7,
turnScope: { kind: 'thread' }
})
)
db.exec("DELETE FROM journal_items WHERE item_id='orca:send'")
for (const id of ['z-card', 'a-card']) {
insertJournalDomainQueueRow(db, target, {
messageId: id,
position: 0,
state: 'waiting',
fingerprint: '',
body: { kind: 'message', role: 'user', blocks: [{ type: 'image-ref', path: '/image.png' }] },
queuedAt: { epoch: 'epoch', sequence: 7 },
holdReason: null,
returnedReason: null,
returnedRejection: null,
consumedAs: null
})
}
seedItem(text(7, 'visible'))
seedItem(text(8, 'tail'))
head(8)
const reader = {
form: 'client' as const,
queuedImages: {
ids: [
['z-card', true],
['send', true],
['a-card', true]
] as const,
imageIds: ['z-card', 'a-card']
}
}
const tail = read({ direction: 'tail', limit: 1, reader })
expect(tail.page).toMatchObject({
items: [{ itemId: 'orca:8:0' }],
submissions: [],
hasOlder: true,
hasNewer: false,
window: { nextCursor: { sequence: 8 } }
})
const before = read({
direction: 'before',
cursor: tail.page.window.nextCursor,
limit: 1,
reader
})
expect(before.page.items.map((item) => item.itemId)).toEqual([
'orca:7:0',
'orca:z-card',
'orca:send',
'orca:a-card'
])
expect(before.page).toMatchObject({
submissions: [{ clientMessageId: 'send' }],
hasOlder: false,
hasNewer: true,
window: { oldest: { sequence: 7 }, newest: { sequence: 7 }, nextCursor: { sequence: 7 } }
})
db.exec("UPDATE journal_submission_attempts SET dispatch_state='accepted'")
expect(
read({
direction: 'before',
cursor: tail.page.window.nextCursor,
limit: 1,
reader
}).page.items.map((item) => item.itemId)
).toEqual(['orca:7:0', 'orca:z-card', 'orca:a-card'])
})
it('catches up with the current revision, placement and submission, not the replayed metadata', async () => {
const { db, commit, seedSend, read } = history()
seedSend()
@@ -7,6 +7,7 @@ import type { JournalStopSettleFact } from './journal-writer-host-facts'
import type { JournalLatestStop } from './context-epochs/journal-stop-turn-end'
import { personStopDecidesTurn, turnEndAfterStop } from './context-epochs/journal-stop-turn-end'
import { boundRunningToolOutput } from './item-lifecycle/journal-payload-bounds'
import { isAdmissibleAgentSessionContextUsage } from '../../../shared/agent-session/agent-session-context-usage-schema'
export function journalDomainLatestStop(
db: Database.Database,
@@ -53,6 +54,14 @@ export function normalizeJournalDomainItemBody(
stopSettle?: JournalStopSettleFact
): AgentJournalItemBody {
let body = item.body
if (
body.kind === 'turn' &&
body.contextUsage !== undefined &&
!isAdmissibleAgentSessionContextUsage(body.contextUsage)
) {
const { contextUsage: _unusable, ...usable } = body
body = usable
}
if (body.kind === 'turn' && body.state === 'interrupted' && body.outcome === undefined) {
const latestStop = journalDomainLatestStop(db, target, stopSettle)
// A one-row policy input is temporary command data, never a reconstructed session.
@@ -15,6 +15,7 @@ import {
import { lostLiveWorkJournalBody } from './item-lifecycle/journal-subagent-liveness'
import { isUnverifiedEndAgentJournalToolCall } from '../../../shared/agent-journal-tool-call-lifecycle'
import { parseAgentJournalItemKey } from '../../../shared/agent-session/journal/agent-session-journal-item-key'
import { spelledProviderTimelineItemKey } from '../../../shared/provider-timeline-item-key'
export function journalDomainItemLifecycleColumns(
item: AgentJournalRenderItem
@@ -112,7 +113,8 @@ export const JOURNAL_DOMAIN_ITEM_COLUMNS = [
'turn_scope_kind',
'provider_turn_id',
'codex_thread_id',
'codex_turn_id'
'codex_turn_id',
'legacy_provider_item_key'
] as const
/** One payload projection supplies import, current writes and derived Stop revisions. */
@@ -140,7 +142,8 @@ export function journalDomainItemColumns(item: AgentJournalRenderItem, payload:
turnScopeKind,
turn?.providerTurnId ?? turn?.turnId ?? null,
identity?.provider === 'codex' ? identity.threadId : null,
identity?.provider === 'codex' ? identity.turnId : null
identity?.provider === 'codex' ? identity.turnId : null,
identity?.provider === 'legacy' ? spelledProviderTimelineItemKey(identity.recordId) : null
]
}
}
@@ -119,6 +119,7 @@ function chat() {
if (result.kind !== operation.kind) {
throw new Error('item read answered a different operation')
}
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the checked discriminant matches the requested kind; TypeScript cannot narrow a generic union.
return result as Extract<typeof result, { kind: K }>
}
return { db, seed, alias, tombstone, send, read }
@@ -47,7 +47,7 @@ export function removeJournalDomainItem(
const itemId = alias ? String(alias.item_id) : key
const row = db
.prepare(`SELECT CASE WHEN ? OR turn_id IS NOT NULL THEN payload_json END AS payload_json,
revision, sequence, sequence_index, turn_marker,prompt,command_entry,
revision, sequence, sequence_index, turn_marker,prompt,command_entry,kind,
queue_failure_report_turn_item_id FROM journal_items WHERE session_id=? AND epoch=? AND item_id=?`)
.get(Number(includeBody), ...prefix, itemId)
if (row?.turn_marker) {
@@ -76,6 +76,8 @@ export function removeJournalDomainItem(
if (
row.turn_marker ||
row.prompt ||
row.kind === 'approval' ||
row.kind === 'question' ||
row.command_entry ||
row.queue_failure_report_turn_item_id !== null
) {

Some files were not shown because too many files have changed in this diff Show More