From 633cec74be636a53d3f5588fbe473fe38f19f650 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 23:07:50 -0800 Subject: [PATCH 01/45] WIP: migrate native chat tests to SQL journal readers --- .../acp/acp-declared-effort.test-fixture.ts | 10 +- .../acp/acp-session-reopen-failure.test.ts | 8 +- .../acp/acp-structured-busy-prompt.test.ts | 3 +- .../acp/acp-structured-host-lifecycle.test.ts | 12 +- .../acp-structured-host-model-options.test.ts | 7 +- .../acp-structured-host-option-hang.test.ts | 17 +- .../acp-structured-host-start-aborts.test.ts | 10 +- .../acp-structured-host-start-failure.test.ts | 3 +- .../acp/acp-structured-host.test-support.ts | 12 +- .../acp/acp-structured-mail-admission.test.ts | 5 +- .../acp-structured-model-unavailable.test.ts | 5 +- .../acp/acp-structured-omp-recordings.test.ts | 7 +- .../acp/acp-structured-restored-model.test.ts | 11 +- ...-structured-session-adapter-resume.test.ts | 12 +- src/main/acp/acp-structured-steer.test.ts | 18 +- .../acp/history/acp-history-boundary.test.ts | 52 +- .../acp/history/acp-history-catch-up.test.ts | 19 +- ...ation-chat-run-measurement-test-harness.ts | 268 -------- .../runtime-chat-run-observer.test.ts | 384 ++++------- src/main/claude/claude-api-retry-row.test.ts | 16 +- .../claude-context-usage-restart.test.ts | 6 +- ...claude-context-usage-unloaded-turn.test.ts | 3 +- .../claude/claude-informational-row.test.ts | 11 +- .../claude-stream-json-connection.test.ts | 11 +- ...claude-subagent-permission-request.test.ts | 5 +- ...claude-transcript-catch-up-test-support.ts | 5 +- .../claude/claude-transcript-catch-up.test.ts | 150 +++-- .../claude/claude-transcript-duration.test.ts | 15 +- ...laude-structured-turn-resume-point.test.ts | 15 +- ...ude-structured-journal-translation.test.ts | 17 +- ...laude-structured-provider-fallback.test.ts | 21 +- .../codex-provider-retry-idle-sweep.test.ts | 15 +- ...x-structured-journal-context-usage.test.ts | 8 +- ...tructured-journal-exit-convergence.test.ts | 16 +- ...x-structured-journal-goal-revision.test.ts | 7 +- ...uctured-journal-repeated-questions.test.ts | 10 +- ...ctured-journal-settlement-rollback.test.ts | 5 +- ...tructured-journal-subagent-readers.test.ts | 13 +- ...-journal-turn-settles-once-journal.test.ts | 3 +- .../codex-turn-lifecycle-history.test.ts | 9 +- ...-structured-thread-catch-up-bounds.spec.ts | 4 +- ...ex-structured-thread-catch-up-echo.spec.ts | 68 +- ...structured-thread-catch-up-sources.spec.ts | 60 +- ...tructured-thread-catch-up-variants.spec.ts | 71 ++- .../codex-structured-thread-catch-up.test.ts | 85 ++- ...x-thread-catch-up-contract-test-fixture.ts | 11 +- .../codex-thread-catch-up-rewind.test.ts | 16 +- .../jsonl-rpc/timeline-final-tail.test.ts | 13 +- .../agent-session-attachment-claims.test.ts | 6 +- .../journal-row-context-usage.spec.ts | 3 +- .../journal-storage-admission.test.ts | 2 +- .../journal-codex-history.test.ts | 7 +- ...urnal-approval-revision-validation.spec.ts | 5 +- .../journal-completed-content.test.ts | 11 +- .../journal-subagent-item.spec.ts | 3 +- ...ournal-terminal-validation-test-fixture.ts | 23 +- .../journal-domain-generation.test.ts | 90 +++ .../journal-input-reads.test.ts | 2 +- .../journal-observation.test-support.ts | 84 +++ .../journal-open-recovery.test.ts | 25 + .../journal-sql-command.test-fixture.ts | 48 -- .../journal-sql-command.test.ts | 312 --------- .../journal-sql-generation-commands.test.ts | 286 --------- .../journal-sql-history-page.test.ts | 227 ------- .../journal-sql-input-command.test-fixture.ts | 190 ------ .../journal-sql-input-commands.test.ts | 597 ------------------ .../journal-sql-open.test.ts | 138 ---- .../journal-sql-query-contracts.test.ts | 209 ------ .../journal-sql-queue-recovery.test.ts | 151 ----- .../journal-start-failure-settlement.test.ts | 2 +- .../journal-status-cuts.test.ts | 459 +++++--------- .../journal-status-indicators.test.ts | 249 ++++---- .../journal-status-requests.test.ts | 417 ++++++------ .../journal-status-state.test.ts | 313 ++++----- .../journal-unheld-turns-settlement.test.ts | 2 +- .../journal-dispatch-reason-bound.spec.ts | 3 +- .../journal-producer-inheritance.spec.ts | 5 +- ...-unsent-send-hold-contract-test-fixture.ts | 2 +- .../journal-unsent-send-hold-queue.spec.ts | 11 +- .../journal-unsent-send-hold-recovery.spec.ts | 4 +- .../queued-message-stop-pause.spec.ts | 21 +- .../replay/journal-outcome-wire.test.ts | 23 +- .../replay/journal-pause-mark-restore.test.ts | 18 +- .../journal-host-database-test-support.ts | 4 +- .../journal-queued-rejection-batch.spec.ts | 21 +- .../journal-submission-positions.spec.ts | 14 +- .../journal-submission-queued-link.spec.ts | 5 +- ...atomic-settlement-contract-test-fixture.ts | 18 +- ...rovider-timeline-assembler-test-support.ts | 3 +- ...ider-timeline-bound-target-test-support.ts | 35 +- .../mobile-queued-edit-host.test-fixture.ts | 5 +- ...-agent-session-accept-then-deliver.test.ts | 17 +- ...ed-agent-session-acquire-isolation.test.ts | 8 +- ...tured-agent-session-adopted-import.test.ts | 12 +- ...ession-at-rest-command-rig.test-fixture.ts | 12 +- ...agent-session-attachment-admission.test.ts | 3 +- ...ured-agent-session-attention-reads.test.ts | 4 +- ...session-background-stop-unreadable.test.ts | 3 +- ...ssion-claude-background-wake-owner.test.ts | 3 +- ...-agent-session-claude-echo-working.test.ts | 12 +- ...stop-contract-test-actions.test-fixture.ts | 15 +- ...ssion-claude-stop-contract.test-fixture.ts | 12 +- ...gent-session-claude-stop-plan-card.test.ts | 3 +- ...d-agent-session-close-aborts-start.test.ts | 3 +- ...ured-agent-session-codex-held-goal.test.ts | 2 +- ...-agent-session-codex-stop-note-end.test.ts | 34 +- ...ured-agent-session-create-adoption.test.ts | 3 +- ...ession-failed-create-owner-verdict.test.ts | 12 +- ...ructured-agent-session-fork-rewind.test.ts | 17 +- ...uctured-agent-session-host.test-fixture.ts | 10 +- ...d-agent-session-input-order-rewind.test.ts | 27 +- ...-session-late-settlement-cancelled.test.ts | 2 +- ...n-late-settlement-contract.test-fixture.ts | 19 +- ...d-agent-session-newer-content-host.test.ts | 10 +- ...sion-queue-import-retained-history.test.ts | 51 +- ...session-queued-message-rig.test-fixture.ts | 22 +- ...ured-agent-session-queued-messages.test.ts | 2 +- ...ructured-agent-session-queued-move.test.ts | 21 +- ...ructured-agent-session-queued-quit.test.ts | 3 +- ...-agent-session-queued-repeat-press.test.ts | 5 +- ...ured-agent-session-queued-stop-row.test.ts | 21 +- ...ent-session-queued-withdrawn-draft.test.ts | 5 +- ...ctured-agent-session-resend-answer.test.ts | 3 +- ...ssion-restart-interruption.test-fixture.ts | 13 +- ...ssion-restart-offer-after-reattach.test.ts | 5 +- ...red-agent-session-restart-teardown.test.ts | 5 +- ...gent-session-rewind-replay.test-fixture.ts | 15 +- ...t-session-rewind-stop-note.test-fixture.ts | 15 +- ...session-rewind-usage-limit.test-fixture.ts | 9 +- ...nt-session-running-operation-order.test.ts | 5 +- ...agent-session-send-open-stale-turn.test.ts | 15 +- ...ured-agent-session-stale-turn-open.test.ts | 5 +- ...agent-session-start-lifecycle-perf.test.ts | 17 +- ...ssion-startup-attempt-host.test-fixture.ts | 10 +- ...d-agent-session-stop-event-entries.test.ts | 29 +- ...tructured-agent-session-stop-event.test.ts | 9 +- ...nt-session-terminal-writer-acquire.test.ts | 20 +- ...ured-agent-session-unrun-send-hold.test.ts | 5 +- ...agent-session-usage-limit-continue.test.ts | 3 +- ...ed-conversation-command-child-work.test.ts | 23 +- .../structured-conversation-command.test.ts | 16 +- ...ured-agent-session-received-output.test.ts | 15 +- ...ured-agent-session-resolved-append.test.ts | 8 +- ...-agent-session-transition-rollback.test.ts | 7 +- ...tructured-agent-session-transition.test.ts | 5 +- ...-agent-session-resumed-child-owner.test.ts | 9 +- ...ructured-agent-session-thread-goal.test.ts | 5 +- ...ession-history-forward-read-budget.test.ts | 4 +- ...session-stop-note-contract.test-fixture.ts | 2 +- ...ssion-subscriber-oversized-removal.test.ts | 4 +- ...tured-agent-session-wire-admission.test.ts | 16 +- ...ed-agent-session-rewind-provenance.test.ts | 12 +- ...session-dead-generation-settlement.test.ts | 16 +- ...gent-session-observed-exit.test-fixture.ts | 2 +- .../agent-session-inventory-scale.test.ts | 10 +- .../agent-session-store-load-scale.test.ts | 3 +- ...-session-conversation-clear-commit.test.ts | 4 +- ...laude-structured-exit-mid-response.test.ts | 3 +- ...e-structured-resumed-start-failure.test.ts | 3 +- ...tured-startup-fault-is-not-an-exit.test.ts | 5 +- ...agent-session-record-store-test-harness.ts | 15 +- ...tured-agent-session-mobile-restore.test.ts | 8 +- ...-agent-session-startup-tab-restore.test.ts | 8 +- ...gent-session-adapter-start-failure.test.ts | 5 +- ...ed-agent-session-attachment-wiring.test.ts | 68 +- ...d-agent-session-codex-compact-exit.test.ts | 6 +- ...agent-session-codex-start-deadline.test.ts | 3 +- ...d-agent-session-integration-replay.test.ts | 13 +- ...ructured-agent-session-integration.test.ts | 13 +- ...session-legacy-send-behind-command.test.ts | 8 +- ...-agent-session-pi-signed-out-start.test.ts | 3 +- .../orchestration-cli-subprocess.test.ts | 5 +- ...-chat-coordinator-mail-rig.test-fixture.ts | 10 +- .../structured-chat-dispatch-worker.test.ts | 3 +- .../structured-chat-mail-custody.test.ts | 6 +- ...ructured-chat-worker-start-unknown.test.ts | 3 +- .../send-agent-turn-host.test.ts | 11 +- .../agent-launch/agent-launch-at-rest.test.ts | 25 +- .../agent-launch-handoff-card.test.ts | 3 +- .../agent-message-command-contract.test.ts | 28 +- ...stration-structured-worker-redrive.test.ts | 8 +- ...hestration-structured-worker-retry.test.ts | 9 +- ...worker-start-card-scenario.test-fixture.ts | 15 +- .../orchestration/worker-start-card.test.ts | 9 +- .../structured-chat-tab-table.test-fixture.ts | 12 +- .../structured-chat-tab-table.test.ts | 5 +- ...ent-session-queued-image-lifecycle.test.ts | 112 ++-- ...session-queued-image-wire-contract.test.ts | 29 +- ...agent-session-continuation-context.test.ts | 9 +- ...session-restart-unregistered-agent.test.ts | 10 +- ...ured-agent-session-adoption-replay.test.ts | 12 +- ...tured-agent-session-create-message.test.ts | 7 +- ...ured-agent-session-at-rest-context.test.ts | 5 +- ...ctured-agent-session-at-rest-lease.test.ts | 3 +- ...ured-agent-session-at-rest-options.test.ts | 3 +- ...agent-session-receipt-frame-budget.test.ts | 5 +- .../structured-worker-cli.integration.test.ts | 3 +- .../structured-child-records-switch.test.ts | 3 +- 198 files changed, 2202 insertions(+), 4634 deletions(-) delete mode 100644 src/main/automations/automation-chat-run-measurement-test-harness.ts create mode 100644 src/main/native-chat/agent-session-journal/journal-observation.test-support.ts delete mode 100644 src/main/native-chat/agent-session-journal/journal-sql-command.test-fixture.ts delete mode 100644 src/main/native-chat/agent-session-journal/journal-sql-command.test.ts delete mode 100644 src/main/native-chat/agent-session-journal/journal-sql-generation-commands.test.ts delete mode 100644 src/main/native-chat/agent-session-journal/journal-sql-history-page.test.ts delete mode 100644 src/main/native-chat/agent-session-journal/journal-sql-input-command.test-fixture.ts delete mode 100644 src/main/native-chat/agent-session-journal/journal-sql-input-commands.test.ts delete mode 100644 src/main/native-chat/agent-session-journal/journal-sql-open.test.ts delete mode 100644 src/main/native-chat/agent-session-journal/journal-sql-query-contracts.test.ts delete mode 100644 src/main/native-chat/agent-session-journal/journal-sql-queue-recovery.test.ts diff --git a/src/main/acp/acp-declared-effort.test-fixture.ts b/src/main/acp/acp-declared-effort.test-fixture.ts index 459867eda3b..c185f19799d 100644 --- a/src/main/acp/acp-declared-effort.test-fixture.ts +++ b/src/main/acp/acp-declared-effort.test-fixture.ts @@ -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, diff --git a/src/main/acp/acp-session-reopen-failure.test.ts b/src/main/acp/acp-session-reopen-failure.test.ts index dfbc3bc1e3c..2c441d5d7a1 100644 --- a/src/main/acp/acp-session-reopen-failure.test.ts +++ b/src/main/acp/acp-session-reopen-failure.test.ts @@ -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, + unannouncedLosses: async () => [] } }, script: (agent) => @@ -52,8 +52,8 @@ it.each([ resume: { key: 'saved-key', sessionId: 'saved-id', - mayBeUnsaved: () => false, - unannouncedLosses: () => [] + mayBeUnsaved: async () => false, + unannouncedLosses: async () => [] } }, deps: { diff --git a/src/main/acp/acp-structured-busy-prompt.test.ts b/src/main/acp/acp-structured-busy-prompt.test.ts index a68b608f1b6..58b4f438e28 100644 --- a/src/main/acp/acp-structured-busy-prompt.test.ts +++ b/src/main/acp/acp-structured-busy-prompt.test.ts @@ -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) }) diff --git a/src/main/acp/acp-structured-host-lifecycle.test.ts b/src/main/acp/acp-structured-host-lifecycle.test.ts index a10537dea8f..d4b264e729f 100644 --- a/src/main/acp/acp-structured-host-lifecycle.test.ts +++ b/src/main/acp/acp-structured-host-lifecycle.test.ts @@ -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') }) diff --git a/src/main/acp/acp-structured-host-model-options.test.ts b/src/main/acp/acp-structured-host-model-options.test.ts index 5ce28a0dd29..3f11ba8c815 100644 --- a/src/main/acp/acp-structured-host-model-options.test.ts +++ b/src/main/acp/acp-structured-host-model-options.test.ts @@ -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' @@ -169,7 +170,7 @@ async function openModels( const complete = async (id: string) => { await waitFor(() => expect(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) @@ -203,7 +204,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 +303,7 @@ 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 ?? rig.submissionFor(id)?.options ).toEqual({ model, effort: 'low' }) await rig.complete(id) } diff --git a/src/main/acp/acp-structured-host-option-hang.test.ts b/src/main/acp/acp-structured-host-option-hang.test.ts index 7fab65492ff..f37d7d25449 100644 --- a/src/main/acp/acp-structured-host-option-hang.test.ts +++ b/src/main/acp/acp-structured-host-option-hang.test.ts @@ -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( @@ -83,10 +88,10 @@ describe('a Grok model pick Grok never answers during send', () => { 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' } }) @@ -126,7 +131,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) diff --git a/src/main/acp/acp-structured-host-start-aborts.test.ts b/src/main/acp/acp-structured-host-start-aborts.test.ts index 7c2d247bdd0..34f4119f45d 100644 --- a/src/main/acp/acp-structured-host-start-aborts.test.ts +++ b/src/main/acp/acp-structured-host-start-aborts.test.ts @@ -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' } } ]) }) @@ -162,10 +163,9 @@ async function expectDeliveredAfterStop( journal.submissions().find((entry) => entry.clientMessageId === 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([ diff --git a/src/main/acp/acp-structured-host-start-failure.test.ts b/src/main/acp/acp-structured-host-start-failure.test.ts index b5aecfb6365..2f38112a02a 100644 --- a/src/main/acp/acp-structured-host-start-failure.test.ts +++ b/src/main/acp/acp-structured-host-start-failure.test.ts @@ -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({ diff --git a/src/main/acp/acp-structured-host.test-support.ts b/src/main/acp/acp-structured-host.test-support.ts index ffb82870707..6415d278f82 100644 --- a/src/main/acp/acp-structured-host.test-support.ts +++ b/src/main/acp/acp-structured-host.test-support.ts @@ -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 false, - unannouncedLosses: () => [] + mayBeUnsaved: async () => false, + unannouncedLosses: async () => [] } : 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: { diff --git a/src/main/acp/acp-structured-mail-admission.test.ts b/src/main/acp/acp-structured-mail-admission.test.ts index 2737485e616..01bcc096a1a 100644 --- a/src/main/acp/acp-structured-mail-admission.test.ts +++ b/src/main/acp/acp-structured-mail-admission.test.ts @@ -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) diff --git a/src/main/acp/acp-structured-model-unavailable.test.ts b/src/main/acp/acp-structured-model-unavailable.test.ts index d33f9d7e648..ce81091b3c5 100644 --- a/src/main/acp/acp-structured-model-unavailable.test.ts +++ b/src/main/acp/acp-structured-model-unavailable.test.ts @@ -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) diff --git a/src/main/acp/acp-structured-omp-recordings.test.ts b/src/main/acp/acp-structured-omp-recordings.test.ts index fb05ea33868..3549a8a2a0f 100644 --- a/src/main/acp/acp-structured-omp-recordings.test.ts +++ b/src/main/acp/acp-structured-omp-recordings.test.ts @@ -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) } ) diff --git a/src/main/acp/acp-structured-restored-model.test.ts b/src/main/acp/acp-structured-restored-model.test.ts index b0b7665bd41..e0604306e30 100644 --- a/src/main/acp/acp-structured-restored-model.test.ts +++ b/src/main/acp/acp-structured-restored-model.test.ts @@ -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' }) diff --git a/src/main/acp/acp-structured-session-adapter-resume.test.ts b/src/main/acp/acp-structured-session-adapter-resume.test.ts index 6e38460900d..0e4a4822813 100644 --- a/src/main/acp/acp-structured-session-adapter-resume.test.ts +++ b/src/main/acp/acp-structured-session-adapter-resume.test.ts @@ -36,8 +36,8 @@ const resume = { resume: { sessionId: PROVIDER_SESSION, key: 'acp:grok:saved', - mayBeUnsaved: () => false, - unannouncedLosses: () => [] + mayBeUnsaved: async () => false, + unannouncedLosses: async () => [] } } } @@ -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, + unannouncedLosses: async () => [] } }, 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, + unannouncedLosses: async () => [] } }, initialize: RESUMES, diff --git a/src/main/acp/acp-structured-steer.test.ts b/src/main/acp/acp-structured-steer.test.ts index f2ce5aa5a86..b7d46e8a02b 100644 --- a/src/main/acp/acp-structured-steer.test.ts +++ b/src/main/acp/acp-structured-steer.test.ts @@ -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,7 +181,7 @@ 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')) @@ -213,8 +214,11 @@ describe('permission changes with already admitted ACP input', () => { 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') + expect(await journal.queuedMessages.get(c)).toMatchObject({ + state: 'waiting', + consumedAs: null + }) + expect((await journal.reads.submission(handoffId(b)))?.dispatchState).toBe('pending') expect(framesOf(original, 'session/prompt')).toHaveLength(1) original.agent.reply(promptA, { stopReason: 'cancelled' }) const promptB = await rig.frame('session/prompt', 1) @@ -225,14 +229,14 @@ describe('permission changes with already admitted ACP input', () => { await waitFor(() => expect(journal.submission(handoffId(c))?.dispatchState).toBe('rejected') ) - expect(journal.submission(handoffId(c))?.rejection).toMatchObject({ + expect((await journal.reads.submission(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)) @@ -244,7 +248,7 @@ describe('permission changes with already admitted ACP input', () => { expect(journal.submission(handoffId(c))?.dispatchState).toBe('accepted') ) } else { - expect(journal.submission(handoffId(c))?.dispatchState).toBe('rejected') + expect((await journal.reads.submission(handoffId(c)))?.dispatchState).toBe('rejected') } await host.close(SESSION, 'user-close') } @@ -496,7 +500,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 } diff --git a/src/main/acp/history/acp-history-boundary.test.ts b/src/main/acp/history/acp-history-boundary.test.ts index 4cee622dd31..c2825705bd4 100644 --- a/src/main/acp/history/acp-history-boundary.test.ts +++ b/src/main/acp/history/acp-history-boundary.test.ts @@ -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' @@ -106,7 +110,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 +157,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 +189,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 +389,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 +403,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 +488,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 +567,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() diff --git a/src/main/acp/history/acp-history-catch-up.test.ts b/src/main/acp/history/acp-history-catch-up.test.ts index e9542680743..88c06975955 100644 --- a/src/main/acp/history/acp-history-catch-up.test.ts +++ b/src/main/acp/history/acp-history-catch-up.test.ts @@ -1,3 +1,4 @@ +import { 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. @@ -241,7 +242,7 @@ describe('ACP history catch-up', () => { expect(await rig.rows()).toEqual(complete) const reopened = await rig.reopenJournal() expect( - isHistoricalProviderTimelineItem(reopened.item(observedTool.itemId)!, (key) => + isHistoricalProviderTimelineItem((await reopened.reads.item(observedTool.itemId))!, (key) => reopened.item(key) ) ).toBe(true) @@ -343,7 +344,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 +357,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 +439,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, @@ -524,7 +525,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 +645,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' diff --git a/src/main/automations/automation-chat-run-measurement-test-harness.ts b/src/main/automations/automation-chat-run-measurement-test-harness.ts deleted file mode 100644 index ef5623f2e39..00000000000 --- a/src/main/automations/automation-chat-run-measurement-test-harness.ts +++ /dev/null @@ -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 - status: MockInstance - observerReads: MockInstance - registration: MockInstance - releases: Mock<() => void>[] - keys: [ - MockInstance, - MockInstance, - MockInstance, - MockInstance - ] - 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() - function wrapSnapshot(original: AgentJournalSnapshot): AgentJournalSnapshot { - const prior = snapshots.get(original) - if (prior) { - return prior - } - function rows(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() - 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(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 - }) - } -} diff --git a/src/main/automations/runtime-chat-run-observer.test.ts b/src/main/automations/runtime-chat-run-observer.test.ts index df09640e80d..6aec3ca8933 100644 --- a/src/main/automations/runtime-chat-run-observer.test.ts +++ b/src/main/automations/runtime-chat-run-observer.test.ts @@ -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() 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() const opening = Promise.withResolvers() - 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) diff --git a/src/main/claude/claude-api-retry-row.test.ts b/src/main/claude/claude-api-retry-row.test.ts index 78aefec20bc..b5df79b7ed1 100644 --- a/src/main/claude/claude-api-retry-row.test.ts +++ b/src/main/claude/claude-api-retry-row.test.ts @@ -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[]) { 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[]) { translator.handle({ type: 'message', sessionId: 'orca-session', message: frame }) await deferred.drained() } - return journal - .snapshot() - .items.flatMap((item): Extract[] => + return (await readTestJournalItems(journal)).flatMap( + (item): Extract[] => item.body.kind === 'status' ? [item.body] : [] - ) + ) } describe('a Claude api_retry frame', () => { diff --git a/src/main/claude/claude-context-usage-restart.test.ts b/src/main/claude/claude-context-usage-restart.test.ts index e6603ee65b5..d77c9f16fec 100644 --- a/src/main/claude/claude-context-usage-restart.test.ts +++ b/src/main/claude/claude-context-usage-restart.test.ts @@ -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' @@ -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', diff --git a/src/main/claude/claude-context-usage-unloaded-turn.test.ts b/src/main/claude/claude-context-usage-unloaded-turn.test.ts index 4326f99c4d9..5b24c6428ce 100644 --- a/src/main/claude/claude-context-usage-unloaded-turn.test.ts +++ b/src/main/claude/claude-context-usage-unloaded-turn.test.ts @@ -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, @@ -226,7 +227,7 @@ describe('context usage for a turn row outside the loaded page', () => { host = await hostAnswer() } expect(selectStructuredAgentContextUsage(state.items, host)).toEqual( - selectStructuredAgentContextUsage(journal.snapshot().items) + selectStructuredAgentContextUsage(await readTestJournalItems(journal)) ) stepsAfterTrim += hasTurnRow(state) ? 0 : 1 } diff --git a/src/main/claude/claude-informational-row.test.ts b/src/main/claude/claude-informational-row.test.ts index 84f584aee1b..a29da754d2c 100644 --- a/src/main/claude/claude-informational-row.test.ts +++ b/src/main/claude/claude-informational-row.test.ts @@ -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[]) { 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[]) { 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', () => { diff --git a/src/main/claude/claude-stream-json-connection.test.ts b/src/main/claude/claude-stream-json-connection.test.ts index 314e9be0dbe..d1007f3292b 100644 --- a/src/main/claude/claude-stream-json-connection.test.ts +++ b/src/main/claude/claude-stream-json-connection.test.ts @@ -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' ) diff --git a/src/main/claude/claude-subagent-permission-request.test.ts b/src/main/claude/claude-subagent-permission-request.test.ts index ef0e624ee14..beb50b1fb9d 100644 --- a/src/main/claude/claude-subagent-permission-request.test.ts +++ b/src/main/claude/claude-subagent-permission-request.test.ts @@ -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' @@ -221,7 +222,9 @@ async function pipeline() { (resolution: Pick, itemId = cardId()) => async (): Promise => { await deferred.drained() - const card = pendingCards(journal.snapshot().items).find((item) => item.itemId === itemId) + const card = pendingCards(await readTestJournalItems(journal)).find( + (item) => item.itemId === itemId + ) const identity = card && parseAgentJournalItemKey(card.itemId) if (card?.body.kind !== 'approval' || !identity) { throw new Error('no pending card to record') diff --git a/src/main/claude/claude-transcript-catch-up-test-support.ts b/src/main/claude/claude-transcript-catch-up-test-support.ts index 0c0e0b322cb..e49004eb08c 100644 --- a/src/main/claude/claude-transcript-catch-up-test-support.ts +++ b/src/main/claude/claude-transcript-catch-up-test-support.ts @@ -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' @@ -236,8 +237,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 : '')) diff --git a/src/main/claude/claude-transcript-catch-up.test.ts b/src/main/claude/claude-transcript-catch-up.test.ts index 02f42055995..a7cd47ef62a 100644 --- a/src/main/claude/claude-transcript-catch-up.test.ts +++ b/src/main/claude/claude-transcript-catch-up.test.ts @@ -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' @@ -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,16 +137,16 @@ 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, { itemId: agentJournalItemKey({ provider: 'orca', clientMessageId }), @@ -150,8 +155,8 @@ describe('Claude chat catch-up', () => { }) ).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,16 +201,16 @@ 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 + const held = await readTestJournalItems(c.journal) expect(c.rows().filter((row) => row === 'user:follow up')).toHaveLength(1) - expect(c.journal.item(key)?.turnScope).toEqual({ kind: 'unknown' }) + expect((await c.journal.reads.item(key))?.turnScope).toEqual({ kind: 'unknown' }) expect(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(await reopened.reads.item(key)).toEqual(beforeReplay) expect(isStructuredRewindTarget(reopened, key)).toBe(false) const page = readAgentSessionHistory(reopened, { sessionId: SESSION, direction: 'tail' }) const delta = reopened.readSince(cursor) @@ -214,7 +219,7 @@ describe('Claude chat catch-up', () => { } const batch = projectJournalBatch({ rows: delta.rows, - snapshot: reopened.snapshot(), + snapshot: await readTestJournalSnapshot(reopened), afterSequence: cursor.sequence }) if (!batch.ok) { @@ -222,11 +227,17 @@ describe('Claude chat catch-up', () => { } 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)) + 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,13 +246,13 @@ 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(await readTestJournalSubmissions(c.journal)).toEqual(submissions) expect(c.rows().filter((row) => row === 'user:follow up')).toHaveLength(1) expect(c.warn).not.toHaveBeenCalled() const cut = structuredRewindCut(c.journal, { @@ -253,8 +264,8 @@ describe('Claude chat catch-up', () => { 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 c.journal.reads.item(key))?.turnScope).toEqual({ kind: 'unknown' }) + expect((await (await c.replay()).reads.item(key))?.turnScope).toEqual({ kind: 'unknown' }) } ) @@ -276,16 +287,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 +313,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 +329,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 +348,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(await c.journal.reads.item(turn.itemId)).toEqual(turn) + expect(await c.journal.reads.item(thinking.itemId)).toEqual(thinking) expect(c.rows().some((row) => row.startsWith('(continued-outside'))).toBe(false) - const completed = c.journal.snapshot().items + 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 () => { @@ -368,7 +379,9 @@ describe('Claude chat catch-up', () => { }) 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') + const running = (await readTestJournalItems(c.journal)).findLast( + (item) => item.body.kind === 'turn' + ) expect(running?.body).toMatchObject({ state: 'running' }) live.dispose() }) @@ -393,7 +406,7 @@ describe('Claude chat catch-up', () => { 'assistant:answer 2' ] expect(c.rows()).toEqual(caughtUp) - expect(c.journal.snapshot().submissions).toHaveLength(1) + 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') @@ -427,22 +440,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 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 +474,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 +493,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 +511,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', @@ -550,29 +564,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((await c.journal.reads.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' }) - ) + 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) diff --git a/src/main/claude/claude-transcript-duration.test.ts b/src/main/claude/claude-transcript-duration.test.ts index 23b672ad9b3..42adb0d4a20 100644 --- a/src/main/claude/claude-transcript-duration.test.ts +++ b/src/main/claude/claude-transcript-duration.test.ts @@ -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:See ya!') 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() }) }) diff --git a/src/main/claude/structured/claude-structured-turn-resume-point.test.ts b/src/main/claude/structured/claude-structured-turn-resume-point.test.ts index e07ed59f514..a539693bf40 100644 --- a/src/main/claude/structured/claude-structured-turn-resume-point.test.ts +++ b/src/main/claude/structured/claude-structured-turn-resume-point.test.ts @@ -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' diff --git a/src/main/claude/structured/journal/claude-structured-journal-translation.test.ts b/src/main/claude/structured/journal/claude-structured-journal-translation.test.ts index 16986147656..ea485b0ba2f 100644 --- a/src/main/claude/structured/journal/claude-structured-journal-translation.test.ts +++ b/src/main/claude/structured/journal/claude-structured-journal-translation.test.ts @@ -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', diff --git a/src/main/claude/structured/launch/claude-structured-provider-fallback.test.ts b/src/main/claude/structured/launch/claude-structured-provider-fallback.test.ts index 6389cdc7e54..3b61f5b6495 100644 --- a/src/main/claude/structured/launch/claude-structured-provider-fallback.test.ts +++ b/src/main/claude/structured/launch/claude-structured-provider-fallback.test.ts @@ -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,13 +149,11 @@ 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']) diff --git a/src/main/codex/structured/journal/codex-provider-retry-idle-sweep.test.ts b/src/main/codex/structured/journal/codex-provider-retry-idle-sweep.test.ts index a06e883edce..e3d3ebe3fd1 100644 --- a/src/main/codex/structured/journal/codex-provider-retry-idle-sweep.test.ts +++ b/src/main/codex/structured/journal/codex-provider-retry-idle-sweep.test.ts @@ -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' ) diff --git a/src/main/codex/structured/journal/codex-structured-journal-context-usage.test.ts b/src/main/codex/structured/journal/codex-structured-journal-context-usage.test.ts index f5aa45ad868..b69ffb6372a 100644 --- a/src/main/codex/structured/journal/codex-structured-journal-context-usage.test.ts +++ b/src/main/codex/structured/journal/codex-structured-journal-context-usage.test.ts @@ -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) } } diff --git a/src/main/codex/structured/journal/codex-structured-journal-exit-convergence.test.ts b/src/main/codex/structured/journal/codex-structured-journal-exit-convergence.test.ts index 0a6b3be32f7..1298246c6d3 100644 --- a/src/main/codex/structured/journal/codex-structured-journal-exit-convergence.test.ts +++ b/src/main/codex/structured/journal/codex-structured-journal-exit-convergence.test.ts @@ -1,3 +1,7 @@ +import { + 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' @@ -157,7 +161,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,7 +214,7 @@ 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 }) @@ -263,12 +267,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() }) diff --git a/src/main/codex/structured/journal/codex-structured-journal-goal-revision.test.ts b/src/main/codex/structured/journal/codex-structured-journal-goal-revision.test.ts index cb0c9dd4a05..aab29309c85 100644 --- a/src/main/codex/structured/journal/codex-structured-journal-goal-revision.test.ts +++ b/src/main/codex/structured/journal/codex-structured-journal-goal-revision.test.ts @@ -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' @@ -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,7 +115,7 @@ describe('codex goal accounting revisions', () => { params: goalFrame({ timeUsedSeconds: 10, updatedAt: 1789067998 }) }) await drained() - expect(journal.snapshot().items[1]?.revision).toBe(created?.revision) + expect((await readTestJournalItems(journal))[1]?.revision).toBe(created?.revision) expect(published).toEqual([]) goals.handle({ @@ -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, diff --git a/src/main/codex/structured/journal/codex-structured-journal-repeated-questions.test.ts b/src/main/codex/structured/journal/codex-structured-journal-repeated-questions.test.ts index bc5928afd6e..ed6c07bb536 100644 --- a/src/main/codex/structured/journal/codex-structured-journal-repeated-questions.test.ts +++ b/src/main/codex/structured/journal/codex-structured-journal-repeated-questions.test.ts @@ -1,3 +1,7 @@ +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' @@ -81,12 +85,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(published.at(-1)).toEqual(await readTestJournalSnapshot(journal)) translator.dispose() deferred.close() } diff --git a/src/main/codex/structured/journal/codex-structured-journal-settlement-rollback.test.ts b/src/main/codex/structured/journal/codex-structured-journal-settlement-rollback.test.ts index ff1b8cafcf8..8ea460d1520 100644 --- a/src/main/codex/structured/journal/codex-structured-journal-settlement-rollback.test.ts +++ b/src/main/codex/structured/journal/codex-structured-journal-settlement-rollback.test.ts @@ -1,3 +1,4 @@ +import { 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' @@ -78,7 +79,7 @@ it('rolls back the entire Codex exit settlement when a later row fails', async ( await expect(deferred.drained()).resolves.toEqual({ ok: true }) published.length = 0 journal.observeCommits(commits) - const before = journal.snapshot() + const before = await readTestJournalSnapshot(journal) const database = openTestJournalHostDatabase(root) const diskBefore = liveTestJournalRows(database.db, SESSION) const sawEarlierInserts: number[] = [] @@ -114,7 +115,7 @@ it('rolls back the entire Codex exit settlement when a later row fails', async ( 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(await readTestJournalSnapshot(journal)).toEqual(before) expect(commits).not.toHaveBeenCalled() expect(published.every((snapshot) => JSON.stringify(snapshot) === JSON.stringify(before))).toBe( true diff --git a/src/main/codex/structured/journal/codex-structured-journal-subagent-readers.test.ts b/src/main/codex/structured/journal/codex-structured-journal-subagent-readers.test.ts index 68fc7931e14..ff1625323a1 100644 --- a/src/main/codex/structured/journal/codex-structured-journal-subagent-readers.test.ts +++ b/src/main/codex/structured/journal/codex-structured-journal-subagent-readers.test.ts @@ -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 { 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 => { await deferred.drained() - return journal.snapshot().items + return await readTestJournalItems(journal) }, reopen: async (): Promise => { await deferred.drained() deferred.unbind() await journal.close() journal = await openJournal(root) - return journal.snapshot().items + return await readTestJournalItems(journal) } } } diff --git a/src/main/codex/structured/journal/codex-structured-journal-turn-settles-once-journal.test.ts b/src/main/codex/structured/journal/codex-structured-journal-turn-settles-once-journal.test.ts index 4b34234610f..7f8014a15b1 100644 --- a/src/main/codex/structured/journal/codex-structured-journal-turn-settles-once-journal.test.ts +++ b/src/main/codex/structured/journal/codex-structured-journal-turn-settles-once-journal.test.ts @@ -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) } } } diff --git a/src/main/codex/structured/journal/codex-turn-lifecycle-history.test.ts b/src/main/codex/structured/journal/codex-turn-lifecycle-history.test.ts index e0d5763d81a..37c73fbd119 100644 --- a/src/main/codex/structured/journal/codex-turn-lifecycle-history.test.ts +++ b/src/main/codex/structured/journal/codex-turn-lifecycle-history.test.ts @@ -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) }) }) diff --git a/src/main/codex/structured/threads/codex-structured-thread-catch-up-bounds.spec.ts b/src/main/codex/structured/threads/codex-structured-thread-catch-up-bounds.spec.ts index 0943def5116..b05f669e9b1 100644 --- a/src/main/codex/structured/threads/codex-structured-thread-catch-up-bounds.spec.ts +++ b/src/main/codex/structured/threads/codex-structured-thread-catch-up-bounds.spec.ts @@ -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') } @@ -51,7 +51,7 @@ 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.journal.reads.item(old.itemId))?.body).toMatchObject({ contextUsage: { window: { tokens: 500, capturedAt: 2_000 } } }) }) diff --git a/src/main/codex/structured/threads/codex-structured-thread-catch-up-echo.spec.ts b/src/main/codex/structured/threads/codex-structured-thread-catch-up-echo.spec.ts index 2c3aeb36a5d..0ecd699cc2c 100644 --- a/src/main/codex/structured/threads/codex-structured-thread-catch-up-echo.spec.ts +++ b/src/main/codex/structured/threads/codex-structured-thread-catch-up-echo.spec.ts @@ -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' @@ -102,8 +107,8 @@ export function registerCodexCatchUpEchoTests(input: { })) } await c.reopen() - const before = c.journal.snapshot() - const draft = c.journal.queuedMessages.get('draft') + const before = await readTestJournalSnapshot(c.journal) + const draft = await c.journal.queuedMessages.get('draft') const receipts = states.map((state) => c.journal.receiptFor(state)) const native = states.map((state, n) => ({ ...turn(n + 2), @@ -139,23 +144,27 @@ export function registerCodexCatchUpEchoTests(input: { 'user:continue', 'assistant:outside answer' ]) - expect(c.journal.snapshot().items.slice(0, before.items.length)).toEqual(before.items) + expect((await readTestJournalItems(c.journal)).slice(0, before.items.length)).toEqual( + before.items + ) expect(states.map((state) => c.journal.receiptFor(state))).toEqual(receipts) expect(c.texts().filter((text) => text.includes('historical rendering'))).toEqual([]) - expect(c.journal.snapshot().submissions).toEqual(before.submissions) - expect(c.journal.queuedMessages.get('draft')).toEqual(draft) + 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) + expect(await readTestJournalSnapshot(c.journal)).toEqual(whole) + expect(await c.journal.queuedMessages.get('draft')).toEqual(draft) const restored = restoreJournal(c.db, SESSION) expect(restored?.state.queue.cards.get('draft')).toEqual(draft) }) @@ -203,9 +212,9 @@ export function registerCodexCatchUpEchoTests(input: { } await c.journal.replaceEpochItems('handle_forked', 1, cut.kept) 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,18 +227,20 @@ export function registerCodexCatchUpEchoTests(input: { { type: 'agentMessage', id: 'reply', text: 'native answer' } ] } - const before = c.journal.snapshot() + const before = await readTestJournalSnapshot(c.journal) const beforeTexts = 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(await c.journal.reads.item(key)).toEqual(prompt) + expect((await readTestJournalItems(c.journal)).slice(0, before.items.length)).toEqual( + before.items + ) expect(c.texts()).toEqual([...beforeTexts, 'assistant:native answer']) - expect(c.journal.submission(clientMessageId)?.dispatchState).toBe('unknown') - expect(c.journal.receiptFor(clientMessageId)).toBeNull() - const whole = c.journal.snapshot() + 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) + expect(await readTestJournalSnapshot(c.journal)).toEqual(whole) await c.journal.replaceEpochItems('handle_forked', 1, []) const hidden = c.journal.cursor() const { readCodexHistoryJournalView } = @@ -239,18 +250,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 () => { @@ -371,9 +387,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 +415,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() diff --git a/src/main/codex/structured/threads/codex-structured-thread-catch-up-sources.spec.ts b/src/main/codex/structured/threads/codex-structured-thread-catch-up-sources.spec.ts index 2b563fe8ebc..78bb7e66b59 100644 --- a/src/main/codex/structured/threads/codex-structured-thread-catch-up-sources.spec.ts +++ b/src/main/codex/structured/threads/codex-structured-thread-catch-up-sources.spec.ts @@ -1,3 +1,7 @@ +import { + readTestJournalItems, + readTestJournalSnapshot +} from '../../../native-chat/agent-session-journal/journal-observation.test-support' import { adapterFor, fakeCodex, @@ -105,7 +109,7 @@ export function registerCodexHistorySourcesTests(input: { } await open() expect(c.texts()).toEqual(['user:ask 1', 'assistant:A different final answer']) - const rows = c.journal.snapshot().items + 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', @@ -372,16 +376,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 +403,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 @@ -451,9 +455,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,10 +474,12 @@ 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((await readTestJournalItems(c.journal)).slice(0, before.items.length)).toEqual( + before.items + ) expect(c.texts().slice(-2)).toEqual(['user:ask 31', 'assistant:answer 31']) const imports = commits.mock.calls.flatMap(([call]) => call.steps.filter( @@ -482,9 +488,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( diff --git a/src/main/codex/structured/threads/codex-structured-thread-catch-up-variants.spec.ts b/src/main/codex/structured/threads/codex-structured-thread-catch-up-variants.spec.ts index c09aac62be8..ab603a04e14 100644 --- a/src/main/codex/structured/threads/codex-structured-thread-catch-up-variants.spec.ts +++ b/src/main/codex/structured/threads/codex-structured-thread-catch-up-variants.spec.ts @@ -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', @@ -291,7 +295,7 @@ 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( @@ -308,9 +312,8 @@ export function registerCodexHistoryVariantsTests(input: { 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) } ) } diff --git a/src/main/codex/structured/threads/codex-structured-thread-catch-up.test.ts b/src/main/codex/structured/threads/codex-structured-thread-catch-up.test.ts index 4421cfbce77..2ea324e8c69 100644 --- a/src/main/codex/structured/threads/codex-structured-thread-catch-up.test.ts +++ b/src/main/codex/structured/threads/codex-structured-thread-catch-up.test.ts @@ -1,3 +1,7 @@ +import { + readTestJournalItems, + readTestJournalSnapshot +} from '../../../native-chat/agent-session-journal/journal-observation.test-support' import { recordedCodexRollout } from '../../__fixtures__/codex-rollout-recordings' import { prepareLegacyTranscriptImport, @@ -97,7 +101,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 +115,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 +172,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 +182,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 +272,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 +305,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,10 +347,10 @@ 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((await readTestJournalItems(c.journal)).slice(0, before.length)).toEqual(before) expect(c.texts().slice(-4)).toEqual([ '(continued-outside)', 'user:ask 2', @@ -360,7 +364,7 @@ describe('Codex chat catch-up', () => { 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) => ({ @@ -375,14 +379,12 @@ describe('Codex chat catch-up', () => { ) expect((await c.open([turn(1), large])).ok).toBe(false) expect(c.deferred.state().failed).toBe(true) - expect(c.journal.snapshot()).toEqual(before) + expect(await readTestJournalSnapshot(c.journal)).toEqual(before) c.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]) await c.reopen() await c.open([turn(1), large]) expect(c.texts()).toHaveLength(603) @@ -398,16 +400,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((await readTestJournalItems(c.journal)).slice(0, before.length)).toEqual(before) expect(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 +492,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({ @@ -512,7 +514,7 @@ describe('Codex chat catch-up', () => { operationId, callerKey, itemId: target, - expectedEpoch: c.journal.epoch, + expectedEpoch: c.journal.physicalCursor().epoch, phase: 'provider-succeeded' } })) @@ -520,9 +522,8 @@ describe('Codex chat catch-up', () => { 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 }) => ({ @@ -544,9 +545,8 @@ describe('Codex chat catch-up', () => { '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 +556,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 () => { @@ -630,7 +630,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 +652,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 +662,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 +701,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) } ) diff --git a/src/main/codex/structured/threads/codex-thread-catch-up-contract-test-fixture.ts b/src/main/codex/structured/threads/codex-thread-catch-up-contract-test-fixture.ts index 85597b0ed01..a8df282e1d6 100644 --- a/src/main/codex/structured/threads/codex-thread-catch-up-contract-test-fixture.ts +++ b/src/main/codex/structured/threads/codex-thread-catch-up-contract-test-fixture.ts @@ -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' @@ -74,14 +75,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 () => { @@ -100,7 +98,6 @@ export async function chat(clock: { now: number }) { get db() { return openTestJournalHostDatabase(root).db }, - publications, reopen: async () => { deferred.close() await journal.close() @@ -122,8 +119,8 @@ export async function chat(clock: { now: number }) { }) return deferred.drained() }, - texts: () => - journal.snapshot().items.flatMap((item) => { + 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('')}`] diff --git a/src/main/codex/structured/threads/codex-thread-catch-up-rewind.test.ts b/src/main/codex/structured/threads/codex-thread-catch-up-rewind.test.ts index d63f218b6a8..bba5eac3734 100644 --- a/src/main/codex/structured/threads/codex-thread-catch-up-rewind.test.ts +++ b/src/main/codex/structured/threads/codex-thread-catch-up-rewind.test.ts @@ -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({ @@ -111,14 +115,14 @@ it('retains the original adoption cutoff through native rewind and disk recovery operationId, callerKey, itemId: target, - expectedEpoch: c.journal.epoch, + expectedEpoch: c.journal.physicalCursor().epoch, phase: 'provider-succeeded' } })) clock.now = 500_000 const deps = () => ({ store, logger: testEventSinkLogging().logger }) await recoverStructuredRewind(deps(), SESSION, c.journal, 1) - const restated = 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]! @@ -136,11 +140,11 @@ it('retains the original adoption cutoff through native rewind and disk recovery '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 () => { diff --git a/src/main/jsonl-rpc/timeline-final-tail.test.ts b/src/main/jsonl-rpc/timeline-final-tail.test.ts index 19aa549cd6f..9fea505ae26 100644 --- a/src/main/jsonl-rpc/timeline-final-tail.test.ts +++ b/src/main/jsonl-rpc/timeline-final-tail.test.ts @@ -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', diff --git a/src/main/native-chat/agent-session-attachments/agent-session-attachment-claims.test.ts b/src/main/native-chat/agent-session-attachments/agent-session-attachment-claims.test.ts index 2fbb7b3a55e..87a796548ff 100644 --- a/src/main/native-chat/agent-session-attachments/agent-session-attachment-claims.test.ts +++ b/src/main/native-chat/agent-session-attachments/agent-session-attachment-claims.test.ts @@ -135,7 +135,7 @@ describe('claims written with the message', () => { ) ).catch((caught: unknown) => caught) expect(isAgentSessionAttachmentExpiredError(error)).toBe(true) - expect(journal.submission('send-1')).toBeUndefined() + expect(await journal.reads.submission('send-1')).toBeUndefined() expect(claims()).toEqual([]) }) @@ -150,7 +150,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}` }) @@ -165,7 +165,7 @@ describe('claims written with the message', () => { ) await journals.closeAll() const reopened = await open() - expect(reopened.queuedMessages.get('draft-1')).toMatchObject({ body, state: 'waiting' }) + expect(await reopened.queuedMessages.get('draft-1')).toMatchObject({ body, state: 'waiting' }) expect(claims()).toEqual([{ upload_id: UPLOAD_A, session_id: IDENTITY.sessionId }]) }) }) diff --git a/src/main/native-chat/agent-session-journal/durable-rows/journal-row-context-usage.spec.ts b/src/main/native-chat/agent-session-journal/durable-rows/journal-row-context-usage.spec.ts index f3579fbb786..dbec17abd82 100644 --- a/src/main/native-chat/agent-session-journal/durable-rows/journal-row-context-usage.spec.ts +++ b/src/main/native-chat/agent-session-journal/durable-rows/journal-row-context-usage.spec.ts @@ -1,3 +1,4 @@ +import { readTestJournalItems } from '../journal-observation.test-support' import { AGENT_JOURNAL_THREAD_SCOPE, type AgentJournalItemBody @@ -115,7 +116,7 @@ export function registerJournalRowContextUsageTests(): void { } 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' }] } ]) diff --git a/src/main/native-chat/agent-session-journal/durable-rows/journal-storage-admission.test.ts b/src/main/native-chat/agent-session-journal/durable-rows/journal-storage-admission.test.ts index 09090067481..5bb9889e5fe 100644 --- a/src/main/native-chat/agent-session-journal/durable-rows/journal-storage-admission.test.ts +++ b/src/main/native-chat/agent-session-journal/durable-rows/journal-storage-admission.test.ts @@ -66,7 +66,7 @@ async function writeItems(opener: typeof journals, ordinals: number[]): Promise< ) } await opener.closeAll() - return journal.epoch + return journal.physicalCursor().epoch } const db = () => openTestJournalHostDatabase(root).db diff --git a/src/main/native-chat/agent-session-journal/history-import/journal-codex-history.test.ts b/src/main/native-chat/agent-session-journal/history-import/journal-codex-history.test.ts index 7d9a99a021c..78b3e5a1ed9 100644 --- a/src/main/native-chat/agent-session-journal/history-import/journal-codex-history.test.ts +++ b/src/main/native-chat/agent-session-journal/history-import/journal-codex-history.test.ts @@ -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) } ) diff --git a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-approval-revision-validation.spec.ts b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-approval-revision-validation.spec.ts index 4da0ed784ab..0cbc6c26fb5 100644 --- a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-approval-revision-validation.spec.ts +++ b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-approval-revision-validation.spec.ts @@ -1,3 +1,4 @@ +import { readTestJournalItems } from '../journal-observation.test-support' 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' @@ -14,7 +15,7 @@ export function registerJournalApprovalRevisionValidationTests( body: fixture.pendingApproval() } ]) - const before = fixture.journal.snapshot().items[0] + const before = (await readTestJournalItems(fixture.journal))[0] const cursor = fixture.journal.cursor() await expect( fixture.journal.appendItem( @@ -33,7 +34,7 @@ export function registerJournalApprovalRevisionValidationTests( ) ).resolves.toBeDefined() expect(fixture.journal.cursor().sequence).toBe(cursor.sequence + 1) - expect(fixture.journal.snapshot().items[0]).toEqual(before) + expect((await readTestJournalItems(fixture.journal))[0]).toEqual(before) await fixture.expectSendStillWritable() }) } diff --git a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-completed-content.test.ts b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-completed-content.test.ts index 4ddb7dc38fb..29f5fae302f 100644 --- a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-completed-content.test.ts +++ b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-completed-content.test.ts @@ -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', diff --git a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-subagent-item.spec.ts b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-subagent-item.spec.ts index 9fc27a51d2d..0b1b7f0bf6e 100644 --- a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-subagent-item.spec.ts +++ b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-subagent-item.spec.ts @@ -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: [ diff --git a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-validation-test-fixture.ts b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-validation-test-fixture.ts index b1cd83e56bc..e53e4d7243f 100644 --- a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-validation-test-fixture.ts +++ b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-validation-test-fixture.ts @@ -1,3 +1,4 @@ +import { readTestJournalItems, readTestJournalSnapshot } from '../journal-observation.test-support' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -83,7 +84,7 @@ export function createFixture() { const json = JSON.stringify({ v: 3, kind: 'item', - epoch: journal.epoch, + epoch: journal.physicalCursor().epoch, seq: index + 2, ts: 900, fence: 7, @@ -108,24 +109,30 @@ export function createFixture() { { 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 before = (await readTestJournalItems(journal)).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( + (await readTestJournalItems(journal)).find((item) => item.itemId === saved.itemId) + ).toEqual(before) + expect(await journal.reads.itemBody('orca:advanceable')).toMatchObject({ state: 'interrupted' }) + expect(await journal.reads.itemBody('orca:approval')).toMatchObject({ + resolution: { state: 'cancelled' } + }) expect(liveTestJournalRows(db, 'validation').slice(0, history.length)).toEqual(history) - const settled = journal.snapshot() + const settled = await readTestJournalSnapshot(journal) await settle() - expect(journal.snapshot()).toEqual(settled) + expect(await readTestJournalSnapshot(journal)).toEqual(settled) expect(commits).toHaveBeenCalledTimes(1) await journal.close() journal = await open() - expect(journal.snapshot()).toEqual(settled) + expect(await readTestJournalSnapshot(journal)).toEqual(settled) } function pendingApproval() { diff --git a/src/main/native-chat/agent-session-journal/journal-domain-generation.test.ts b/src/main/native-chat/agent-session-journal/journal-domain-generation.test.ts index adb6b19c44c..b66e1026ae7 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-generation.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-generation.test.ts @@ -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([]) +}) diff --git a/src/main/native-chat/agent-session-journal/journal-input-reads.test.ts b/src/main/native-chat/agent-session-journal/journal-input-reads.test.ts index d39be3dfb4a..310e9e36e80 100644 --- a/src/main/native-chat/agent-session-journal/journal-input-reads.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-input-reads.test.ts @@ -84,7 +84,7 @@ it('answers every input-evidence read from a readonly snapshot exactly as the in await journal.appendSubmission( { clientMessageId: 'linked', - payloadFingerprint: journal.queuedMessages.get('card')!.fingerprint, + payloadFingerprint: (await journal.queuedMessages.get('card'))!.fingerprint, body: fixture.message('linked'), fence: 0, handoverRecorded: true diff --git a/src/main/native-chat/agent-session-journal/journal-observation.test-support.ts b/src/main/native-chat/agent-session-journal/journal-observation.test-support.ts new file mode 100644 index 00000000000..111b266a3e0 --- /dev/null +++ b/src/main/native-chat/agent-session-journal/journal-observation.test-support.ts @@ -0,0 +1,84 @@ +import type { + AgentJournalRenderItem, + AgentJournalSnapshot, + AgentJournalSubmission +} from '../../../shared/agent-session/journal/agent-session-journal-types' +import { parseAgentJournalItemKey } from '../../../shared/agent-session/journal/agent-session-journal-item-key' +import { compareAgentJournalItems } from '../../../shared/agent-session/journal/agent-session-journal-position' +import type { AgentSessionJournalReads } from './journal-reads' +import { journalSqlReads } from './journal-sql-reads' +import type { StructuredAgentSessionHost } from '../agent-session-wire/host/structured-agent-session-host' + +type JournalObservation = { identity: { sessionId: string }; reads: AgentSessionJournalReads } + +export async function readTestJournalItems( + journal: Pick +): Promise { + const items: AgentJournalRenderItem[] = [] + await journal.reads.visitItems( + { + kinds: [ + 'message', + 'tool-call', + 'diff', + 'approval', + 'question', + 'status', + 'turn', + 'agent-start', + 'agent-handoff' + ] + }, + (_id, _sequence, _body, _attribution, item) => items.push(item) + ) + return items.sort(compareAgentJournalItems) +} + +export async function readTestJournalSubmissions( + journal: Pick +): Promise { + const { keys } = await journal.reads.input({ kind: 'capture-keys', present: [] }) + const entries: { submission: AgentJournalSubmission; ordinal: number }[] = [] + for (const key of keys) { + const identity = parseAgentJournalItemKey(key) + if (identity?.provider === 'orca') { + const submission = await journal.reads.submission(identity.clientMessageId) + if (submission) { + const { ordinal } = await journal.reads.input({ + kind: 'input-ordinal', + clientMessageId: submission.clientMessageId + }) + entries.push({ submission, ordinal }) + } + } + } + return entries + .sort( + (left, right) => + left.submission.submittedAt - right.submission.submittedAt || left.ordinal - right.ordinal + ) + .map(({ submission }) => submission) +} + +export async function readTestJournalSnapshot( + journal: JournalObservation +): Promise { + return { + sessionId: journal.identity.sessionId, + cursor: await journal.reads.cursor(), + items: await readTestJournalItems(journal), + submissions: await readTestJournalSubmissions(journal) + } +} + +export async function readTestHostJournal( + host: StructuredAgentSessionHost, + sessionId: string +): Promise { + await host.history({ sessionId, direction: 'tail' }) + const reads: AgentSessionJournalReads = journalSqlReads( + host.deps.journalDatabase.readers, + sessionId + ) + return readTestJournalSnapshot({ identity: { sessionId }, reads }) +} diff --git a/src/main/native-chat/agent-session-journal/journal-open-recovery.test.ts b/src/main/native-chat/agent-session-journal/journal-open-recovery.test.ts index a7b7d893652..22070451172 100644 --- a/src/main/native-chat/agent-session-journal/journal-open-recovery.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-open-recovery.test.ts @@ -87,3 +87,28 @@ it('retains earlier recovery and a conservative reopen floor when the final mark }) ).toEqual({ kind: 'selected', card: null }) }) + +it('marks a working subagent as unverifiable when another handle opens its saved journal', async () => { + const f = domainLifecycleFixture(cleanups) + await f.run({ + kind: 'item', + identity: { provider: 'orca', clientMessageId: 'roster' }, + body: { + kind: 'message', + role: 'system', + blocks: [ + { + type: 'subagent-group', + groupId: 'group', + agents: [{ id: 'a', label: 'read', state: 'working', startedAt: 10 }] + } + ] + }, + options: { fence: 1, turnScope: { kind: 'thread' } } + }) + await f.run({ kind: 'open' }) + expect(f.item('orca:roster')).toMatchObject({ + recovered: true, + body: { blocks: [{ agents: [{ id: 'a', state: 'unverifiable' }] }] } + }) +}) diff --git a/src/main/native-chat/agent-session-journal/journal-sql-command.test-fixture.ts b/src/main/native-chat/agent-session-journal/journal-sql-command.test-fixture.ts deleted file mode 100644 index 70e4f8c21a5..00000000000 --- a/src/main/native-chat/agent-session-journal/journal-sql-command.test-fixture.ts +++ /dev/null @@ -1,48 +0,0 @@ -import type Database from '../../sqlite/sync-database' -import { - createAgentSessionRecordTablesSql, - createJournalTablesSql -} from './database/journal-database-schema' -import { journalSqlProjectionSchema } from './journal-sql-projection-schema' -import { beginJournalSqlConversion, markJournalSqlReady } from './journal-sql-meta' -import { publishJournalSessionEpoch } from './durable-rows/journal-row-table' -import { - advanceJournalOutcomeSource, - ensureJournalOutcomeTables, - startJournalOutcomeLog -} from './outcomes/journal-outcome-format' -import { persistJournalOutcomeTransition } from './outcomes/journal-outcome-table' -import { JournalSqlReducer } from './journal-sql-reducer' -import { JournalSqlScope } from './journal-sql-scope' -import { journalSqlLifecycleRows } from './journal-sql-reducer-rows.test-fixture' -import { journalInputOrder } from './input-order/journal-input-order' -import { ensureCommandReceiptsTable } from './writing/command-receipt-schema' -import { ensureQueuedMessagesTable } from './queued-messages/queued-message-schema' - -/** Seeds a published projection without pretending to exercise actual-open reconstruction. */ -export function seedJournalSqlCommands(db: Database.Database) { - db.exec( - createJournalTablesSql() + createAgentSessionRecordTablesSql() + journalSqlProjectionSchema() - ) - ensureJournalOutcomeTables(db) - ensureCommandReceiptsTable(db) - ensureQueuedMessagesTable(db) - publishJournalSessionEpoch(db, { sessionId: 'session', workspaceId: 'workspace' }, 'epoch') - const source = startJournalOutcomeLog(db, 'session', 'epoch', 0, 100) - const initial = journalSqlLifecycleRows()[0]! - const scope = new JournalSqlScope(db, 'session', 'epoch') - persistJournalOutcomeTransition( - db, - new JournalSqlReducer(scope, 'epoch').transition(), - source, - [initial], - 100, - true, - (state) => journalInputOrder(state).cards.ensure(state) - ) - advanceJournalOutcomeSource(db, 'session', 'epoch', 1) - beginJournalSqlConversion(db, 'session') - markJournalSqlReady(db, 'session') - db.prepare(`UPDATE journal_proj_meta SET owner_token='open-token'`).run() - return { source, initial } -} diff --git a/src/main/native-chat/agent-session-journal/journal-sql-command.test.ts b/src/main/native-chat/agent-session-journal/journal-sql-command.test.ts deleted file mode 100644 index 3e34c0dae1c..00000000000 --- a/src/main/native-chat/agent-session-journal/journal-sql-command.test.ts +++ /dev/null @@ -1,312 +0,0 @@ -import { seedJournalSqlCommands } from './journal-sql-command.test-fixture' -import { afterEach, expect, it } from 'vitest' -import Database from '../../sqlite/sync-database' -import { - advanceJournalOutcomeSource, - ensureJournalOutcomeTables -} from './outcomes/journal-outcome-format' -import { - persistJournalOutcomeRows, - persistJournalOutcomeTransition -} from './outcomes/journal-outcome-table' -import { createJournalReducerState } from './projection/journal-reducer' -import { JournalSqlReducer } from './journal-sql-reducer' -import { JournalSqlScope } from './journal-sql-scope' -import { JournalSqlCommand } from './journal-sql-command' -import { JournalSqlBatch } from './journal-sql-batch' -import { planJournalWriterRows } from './journal-writer-plan' -import type { - JournalWriterCommand, - JournalWriterCoreOperation, - JournalWriterOperation -} from './journal-writer-command' -import { journalInputOrder } from './input-order/journal-input-order' -import { agentJournalItemKey } from '../../../shared/agent-session/journal/agent-session-journal-item-key' -import type { CommandReceipt } from './writing/command-receipt-schema' -import { readCommandReceipt } from './writing/command-receipt-table' -import { agentSessionJournalProviderHandle } from '../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' -import type { JournalWriterStep } from './writing/journal-writer-step' - -const databases: Database.Database[] = [] -afterEach(() => { - for (const db of databases.splice(0)) { - db.close() - } -}) -const target = { sessionId: 'session', epoch: 'epoch', ownerToken: 'open-token' } -const identity = { provider: 'codex', threadId: 'thread', turnId: 'turn', ordinal: 0 } as const -const itemId = agentJournalItemKey(identity) -const item = (text: string, fence = 1): JournalWriterCoreOperation => ({ - kind: 'item', - identity, - body: { kind: 'message', role: 'assistant', blocks: [{ type: 'text', text }] }, - options: { fence, turnScope: { kind: 'thread' } } -}) -const command = (operation: JournalWriterOperation): JournalWriterCommand => ({ target, operation }) - -function fixture() { - const db = new Database(':memory:') - databases.push(db) - const { source, initial } = seedJournalSqlCommands(db) - return { - db, - source, - initial, - engine: new JournalSqlCommand(db, '/unused'), - batch: new JournalSqlBatch(db) - } -} - -it('commits a step prefix without leaking the rolled-back session or admitting its suffix', async () => { - const { db, engine, batch } = fixture() - db.exec(`CREATE TEMP TRIGGER refuse_step BEFORE INSERT ON journal_proj_items - WHEN NEW.entry_key='"orca:refused"' BEGIN SELECT RAISE(ABORT, 'step refused'); END`) - const step = (clientMessageId: string): JournalWriterStep => ({ - kind: 'item', - identity: { provider: 'orca', clientMessageId }, - body: { kind: 'status', text: clientMessageId }, - options: { fence: 1, turnScope: { kind: 'thread' } }, - reservedBytes: 1024 - }) - const answer = ( - await batch.run( - [ - command({ - kind: 'steps', - steps: [step('first'), step('refused'), step('suffix')] - }) - ], - (entry) => engine.apply(entry, 100) - ) - ).outcomes[0]! - expect(answer).toMatchObject({ - committed: true, - value: { - cursor: { epoch: 'epoch', sequence: 2 }, - items: [{ itemId: 'orca:first', revision: 1, deleted: false }], - value: { - outcomes: [{ kind: 'committed', wrote: true }], - failure: { message: 'step refused' } - } - } - }) - if (!answer.committed) { - throw new Error('prefix must commit') - } - expect( - answer.value.keys.some((entry) => entry.key === 'orca:refused' || entry.key === 'orca:suffix') - ).toBe(false) - expect( - db - .prepare('SELECT entry_key FROM journal_proj_items ORDER BY ordinal') - .all() - .map((row) => JSON.parse(String(row.entry_key))) - ).toContain('orca:first') - expect( - db - .prepare('SELECT 1 FROM journal_proj_items WHERE entry_key IN (?,?)') - .get('"orca:refused"', '"orca:suffix"') - ).toBeUndefined() -}) - -it('assigns revisions after preceding successes and rolls back failed command projections and receipts', async () => { - const { db, engine, batch, initial, source } = fixture() - // A logical context boundary retains the physical epoch for ownership and log lookup. - persistJournalOutcomeTransition( - db, - new JournalSqlReducer(new JournalSqlScope(db, 'session', 'epoch'), 'epoch').transition(), - source, - [{ ...initial, seq: 2 }], - 100, - false, - (state) => journalInputOrder(state).cards.ensure(state) - ) - advanceJournalOutcomeSource(db, 'session', 'epoch', 2) - db.prepare('UPDATE journal_proj_meta SET through_seq=2').run() - const contextEpoch = 'context-v1:2cb86f27aabffce457946e9c46bd5d771895f95d6379dee0866d667e0ecb7adc' - db.prepare('INSERT INTO agent_session_records VALUES (?, ?)').run('session', '{}') - const receipt: CommandReceipt = { - operationId: 'send', - sessionId: 'session', - callerKey: 'caller', - method: 'agentSession.send', - fingerprint: 'fingerprint', - acceptedAt: 100, - status: 'accepted', - result: { kind: 'journal-row', epoch: 'unassigned', sequence: 1 } - } - const accepted: JournalWriterCommand = { - ...command(item('first')), - effects: [ - { - kind: 'receipt', - scope: { kind: 'global' }, - receipt, - row: { at: 'last', answer: 'journal-row' } - } - ] - } - const duplicate: JournalWriterCommand = { - ...command(item('must roll back')), - effects: [ - { - kind: 'receipt', - scope: { kind: 'global' }, - receipt, - row: { at: 'last', answer: 'journal-row' } - } - ] - } - const results = await batch.run( - [accepted, duplicate, command(item('second')), command(item('stale', 0))], - (entry) => engine.apply(entry, 100) - ) - expect(results.outcomes.map((result) => result.committed)).toEqual([true, false, true, false]) - const committed = results.outcomes.flatMap((result) => (result.committed ? [result.value] : [])) - expect(committed.map((result) => result.cursor)).toEqual([ - { epoch: contextEpoch, sequence: 3 }, - { epoch: contextEpoch, sequence: 4 } - ]) - expect(committed.map((result) => result.items)).toEqual([ - [{ itemId, deleted: false, revision: 1, sequence: 3, sequenceIndex: 0 }], - [{ itemId, deleted: false, revision: 2, sequence: 3, sequenceIndex: 0 }] - ]) - const loaded = new JournalSqlReducer(new JournalSqlScope(db, 'session', 'epoch'), 'epoch') - expect(loaded.state.items.get(itemId)?.body).toEqual({ - kind: 'message', - role: 'assistant', - blocks: [{ type: 'text', text: 'second' }] - }) - expect(db.prepare('SELECT through_seq FROM journal_proj_meta').get()?.through_seq).toBe(4) - expect(db.prepare('SELECT MAX(seq) AS seq FROM journal_outcomes').get()?.seq).toBe(4) - expect(readCommandReceipt(db, { kind: 'global' }, 'send')).toMatchObject({ - verdict: 'readable', - receipt: { result: { kind: 'journal-row', epoch: contextEpoch, sequence: 3 } } - }) -}) - -it('keeps exact saved records across command shells and publishes metadata without item bodies', async () => { - const { db, source, initial, engine, batch } = fixture() - const oracle = new Database(':memory:') - databases.push(oracle) - ensureJournalOutcomeTables(oracle) - let memory = persistJournalOutcomeRows( - oracle, - createJournalReducerState('session', 'epoch'), - source, - [initial], - 100, - true, - (state) => journalInputOrder(state).cards.ensure(state) - ).adopt() - const operations: JournalWriterCoreOperation[] = [ - item('one'), - item('two'), - { - kind: 'lifecycle', - input: { - settlementId: 'settled', - fence: 1, - mutations: [ - { kind: 'tombstone', identity }, - { - kind: 'item', - identity, - turnScope: { kind: 'thread' }, - body: { - kind: 'message', - role: 'assistant', - blocks: [{ type: 'text', text: 'recreated' }] - } - } - ] - } - }, - { kind: 'stop', fence: 1, event: { reason: 'user-stop' } }, - { kind: 'queue-resume', fence: 1 }, - { kind: 'queue-reopen', fence: 1, since: 2 }, - { kind: 'tombstone', identity, fence: 1 } - ] - for (const operation of operations) { - const rows = planJournalWriterRows(memory, operation).map((build, index) => - build(memory.lastSequence + index + 1, 100) - ) - memory = persistJournalOutcomeRows(oracle, memory, source, rows, 100, false, (state) => - journalInputOrder(state).cards.ensure(state) - ).adopt() - const result = (await batch.run([command(operation)], (entry) => engine.apply(entry, 100))) - .outcomes[0]! - expect(result.committed).toBe(true) - } - const records = (connection: Database.Database) => - connection - .prepare(`SELECT seq, field, entry_key, - format, ordinal, value_json, deleted FROM journal_outcomes ORDER BY seq, field, entry_key`) - .all() - expect(records(db)).toEqual(records(oracle)) - const final = ( - await batch.run([command(item('after deletion'))], (entry) => engine.apply(entry, 100)) - ).outcomes[0]! - expect(final).toMatchObject({ - committed: true, - value: { items: [{ itemId, revision: 6, deleted: false }] } - }) - expect(JSON.stringify(final)).not.toContain('after deletion') -}) - -it('refuses stale ownership, changed epochs and log coverage even for an empty plan', async () => { - const { db, engine, batch } = fixture() - const noOp = command({ - kind: 'dispatch', - input: { clientMessageId: 'missing', state: 'unknown', fence: 1 }, - onlyIfUnsettled: true - }) - const run = async (entry = noOp) => - (await batch.run([entry], (value) => engine.apply(value, 100))).outcomes[0]! - expect( - await run({ - ...noOp, - effects: [ - { - kind: 'store', - insertOperationsIfAbsent: false, - writes: { - records: { upsert: [], remove: [] }, - operations: { upsert: [['ignored', '{}']], remove: [] }, - retiredClaimKeys: null, - sessionTabs: null - } - } - ] - }) - ).toMatchObject({ committed: true, value: { inputChanged: false, effects: [] } }) - expect(db.prepare('SELECT 1 FROM agent_session_operations').get()).toBeUndefined() - expect((await run({ ...noOp, target: { ...target, ownerToken: 'old' } })).committed).toBe(false) - db.prepare('UPDATE journal_sessions SET epoch=?').run('another-epoch') - expect((await run()).committed).toBe(false) - db.prepare('UPDATE journal_sessions SET epoch=?').run('epoch') - db.prepare('UPDATE journal_outcome_epochs SET through_seq=2').run() - expect((await run()).committed).toBe(false) - expect(db.prepare('SELECT through_seq FROM journal_proj_meta').get()?.through_seq).toBe(1) -}) - -it('replaces the epoch inside one writer command, moving the logical cursor epoch', async () => { - const { engine, batch } = fixture() - const run = async (entry: JournalWriterCommand) => - (await batch.run([entry], (value) => engine.apply(value, 100))).outcomes[0]! - const before = await run(command(item('before'))) - const replaced = await run( - command({ - kind: 'epoch-replace', - reason: 'handle_forked', - fence: 1, - items: [], - providerHandle: agentSessionJournalProviderHandle({ agent: 'codex', providerHandle: null }) - }) - ) - if (!before.committed || !replaced.committed) { - throw new Error('both commands commit') - } - expect(replaced.value.cursor.epoch).not.toBe(before.value.cursor.epoch) - expect(replaced.value.cursor.sequence).toBeGreaterThan(before.value.cursor.sequence) -}) diff --git a/src/main/native-chat/agent-session-journal/journal-sql-generation-commands.test.ts b/src/main/native-chat/agent-session-journal/journal-sql-generation-commands.test.ts deleted file mode 100644 index ff09ffb87dc..00000000000 --- a/src/main/native-chat/agent-session-journal/journal-sql-generation-commands.test.ts +++ /dev/null @@ -1,286 +0,0 @@ -import { expect, it } from 'vitest' -import { journalInputOrder } from './input-order/journal-input-order' -import { agentJournalItemKey } from '../../../shared/agent-session/journal/agent-session-journal-item-key' -import type { - JournalStaleSettlementInput, - JournalDeadGenerationSettlementInput -} from './journal-store-contracts' -import { - settleJournalStaleState, - settleJournalDeadGeneration -} from './writing/journal-generation-settlement' -import { journalGenerationSettlementRead } from './writing/journal-generation-settlement-read' -import { renderJournalStatus } from './projection/journal-public-read' -import { mutateJournalPrompt } from './writing/journal-prompt-mutation' -import { agentJournalTurnBody } from '../../../shared/agent-session/lifecycle/agent-session-turn-record' -import { journalStopEventRowBuilder } from './context-epochs/journal-stop-and-resume-rows' - -import { - sides, - both, - saved, - type memoryJournal, - text -} from './journal-sql-input-command.test-fixture' - -const promptIdentity = { provider: 'orca' as const, clientMessageId: 'approval' } -const promptId = agentJournalItemKey(promptIdentity) -const pendingApproval = { - kind: 'approval' as const, - title: 'Run?', - detail: null, - options: [{ id: 'allow', label: 'Allow' }], - resolution: { - state: 'pending' as const, - selectedOptionId: null, - resolvedBy: null, - resolvedAt: null - } -} - -function settlementRead(j: ReturnType) { - return journalGenerationSettlementRead( - () => j.state(), - j, - () => renderJournalStatus(j.queuedMessages.displayState(), journalInputOrder(j.state())).items - ) -} - -it('rechecks prompt mutations after provider revisions and preserves the accepted answer during stale settlement', async () => { - const side = await sides() - const scope = { kind: 'thread' as const } - await both( - side, - (j) => j.itemAppender.append(promptIdentity, pendingApproval, { fence: 1, turnScope: scope }), - { - kind: 'item', - identity: promptIdentity, - body: pendingApproval, - options: { fence: 1, turnScope: scope } - } - ) - const answer = { - itemId: promptId, - expectedRevision: 1, - kind: 'approval' as const, - resolution: { - state: 'resolved' as const, - selectedOptionId: 'allow', - resolvedBy: 'person', - resolvedAt: 900 - }, - options: { fence: 1, turnScope: scope } - } - await both( - side, - (j) => mutateJournalPrompt({ state: () => j.state(), writer: j.rowWriter }, answer), - { - kind: 'prompt-mutate', - input: answer - } - ) - const before = saved(side.sqlDb) - await expect( - side.run({ - kind: 'prompt-mutate', - input: { - ...answer, - resolution: { - state: 'cancelled', - selectedOptionId: null, - resolvedBy: 'person', - resolvedAt: 950 - } - } - }) - ).rejects.toMatchObject({ refusal: { code: 'agent_session_item_revision_stale' } }) - expect(saved(side.sqlDb)).toEqual(before) - const stale: JournalStaleSettlementInput = { - sessionId: 'session', - fence: 2, - acquisitionGeneration: 'new', - deathEvidence: null - } - await both( - side, - (j) => settleJournalStaleState(settlementRead(j), j.lifecycleBatchAppender, stale), - { - kind: 'stale-settle', - input: stale - } - ) - expect(side.memory.state().items.get(promptId)?.body).toMatchObject({ - resolution: { state: 'resolved', selectedOptionId: 'allow', resolvedAt: 900 } - }) -}) - -it('settles stale lifecycle records and later proof identically without changing unrelated history', async () => { - const side = await sides() - const turn = { provider: 'orca' as const, clientMessageId: 'stale-turn' } - const running = agentJournalTurnBody({ turnId: 'stale-turn', state: 'running', startedAt: 100 }) - for (const [identity, body] of [ - [turn, running], - [promptIdentity, pendingApproval] - ] as const) { - await both( - side, - (j) => j.itemAppender.append(identity, body, { fence: 1, turnScope: { kind: 'thread' } }), - { - kind: 'item', - identity, - body, - options: { fence: 1, turnScope: { kind: 'thread' } } - } - ) - } - const stale: JournalStaleSettlementInput = { - sessionId: 'session', - fence: 2, - acquisitionGeneration: 'reopened', - deathEvidence: null - } - await both( - side, - (j) => settleJournalStaleState(settlementRead(j), j.lifecycleBatchAppender, stale), - { - kind: 'stale-settle', - input: stale - } - ) - expect(side.memory.state().items.get(agentJournalItemKey(turn))?.body).toMatchObject({ - state: 'unverifiable' - }) - expect(side.memory.state().items.get(promptId)?.body).toMatchObject({ - resolution: { state: 'cancelled' } - }) - const proof: JournalStaleSettlementInput = { - ...stale, - acquisitionGeneration: 'proof', - deathEvidence: { kind: 'exit-observed', detail: '', ownerFence: 1, observedAt: 800 } - } - await both( - side, - (j) => settleJournalStaleState(settlementRead(j), j.lifecycleBatchAppender, proof), - { - kind: 'stale-settle', - input: proof - } - ) - expect(side.memory.state().items.get(agentJournalItemKey(turn))?.body).toMatchObject({ - state: 'interrupted', - completedAt: 800 - }) - const before = saved(side.sqlDb) - await side.run({ kind: 'stale-settle', input: proof }) - expect(saved(side.sqlDb)).toEqual(before) -}) - -it('settles dead and older generations together while honoring volatile handover ownership', async () => { - const side = await sides() - for (const [id, fence] of [ - ['old', 1], - ['exited', 2], - ['continuing', 2] - ] as const) { - const input = { clientMessageId: id, payloadFingerprint: id, body: text(id), fence } - await both(side, (j) => j.submissionWriter.append(input), { kind: 'submission', input }) - } - const input: JournalDeadGenerationSettlementInput = { - sessionId: 'session', - fence: 3, - below: 2, - deathEvidence: null, - exit: { - ownerFence: 2, - settlementId: 'exit', - verdict: { state: 'interrupted', completedAt: 800 }, - pendingSubmissionReason: 'provider_exited', - showUnexpectedExitOutcome: false - }, - continuingInputs: ['continuing'], - finalizingInputs: [] - } - await both( - side, - (j) => settleJournalDeadGeneration(settlementRead(j), j.lifecycleBatchAppender, input), - { - kind: 'dead-generation-settle', - input - } - ) - expect( - ['old', 'exited', 'continuing'].map( - (id) => side.memory.state().submissions.get(id)?.dispatchState - ) - ).toEqual(['unknown', 'unknown', 'pending']) -}) - -it('does not report an unexpected exit for sends held by their result owners', async () => { - const side = await sides() - for (const id of ['continuing', 'own-finalizer']) { - const submission = { clientMessageId: id, payloadFingerprint: id, body: text(id), fence: 1 } - await both(side, (j) => j.submissionWriter.append(submission), { - kind: 'submission', - input: submission - }) - } - const input: JournalDeadGenerationSettlementInput = { - sessionId: 'session', - 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 }] - } - await both( - side, - (j) => settleJournalDeadGeneration(settlementRead(j), j.lifecycleBatchAppender, input), - { - kind: 'dead-generation-settle', - input - } - ) - expect(side.memory.state().submissions.get('own-finalizer')?.dispatchState).toBe('unknown') - expect(side.memory.state().submissions.get('continuing')?.dispatchState).toBe('pending') - expect( - settlementRead(side.memory) - .statusRead() - .items.filter((item) => item.body.kind === 'status') - ).toEqual([]) -}) - -it('keeps an in-flight personal Stop authoritative for cancellation and notice suppression', async () => { - const side = await sides() - const { memory } = side - const turn = { provider: 'orca' as const, clientMessageId: 'stopping-turn' } - await memory.itemAppender.append( - turn, - agentJournalTurnBody({ turnId: 'turn', state: 'running', startedAt: 100 }), - { fence: 1, turnScope: { kind: 'thread' } } - ) - await memory.rowWriter.append( - journalStopEventRowBuilder(() => memory.state(), { reason: 'user-stop' }, 1) - ) - memory.stopMarks.beginSettle(true) - await settleJournalStaleState(settlementRead(memory), memory.lifecycleBatchAppender, { - sessionId: 'session', - fence: 2, - acquisitionGeneration: 'stopped', - deathEvidence: { kind: 'exit-observed', detail: '', ownerFence: 1, observedAt: 1200 } - }) - expect(memory.state().items.get(agentJournalItemKey(turn))?.body).toMatchObject({ - state: 'interrupted', - outcome: 'cancellation' - }) - expect( - settlementRead(memory) - .statusRead() - .items.filter((item) => item.body.kind === 'status') - ).toEqual([]) -}) diff --git a/src/main/native-chat/agent-session-journal/journal-sql-history-page.test.ts b/src/main/native-chat/agent-session-journal/journal-sql-history-page.test.ts deleted file mode 100644 index f17962bab9e..00000000000 --- a/src/main/native-chat/agent-session-journal/journal-sql-history-page.test.ts +++ /dev/null @@ -1,227 +0,0 @@ -import { expect, it } from 'vitest' -import { - appendItems, - fixture, - item, - body -} from '../agent-session-wire/publication/history/testing/agent-session-history-contract.test-fixture' -import { - readAgentSessionHistory, - readAgentSessionHydrationPage -} from '../agent-session-wire/publication/history/agent-session-history-page' -import { measureHistoryItem } from '../agent-session-wire/publication/history/agent-session-history-page-bounds' -import { - openTestJournalHostDatabase, - publishTestJournalEpoch -} from './store-lifecycle/journal-host-database-test-support' -import { convertJournalSqlProjection } from './journal-domain-single-connection-import.test-fixture' -import { JournalProjectionStaleError } from './journal-sql-meta' -import { readJournalSqlHistoryPage } from './journal-sql-history-page' -import { JournalSqlReaderPool } from './journal-sql-reader-pool' -import { journalSqlReaderPath } from './journal-sql-worker-bundle.test-fixture' -import type { AgentSessionHistoryRequest } from '../../../shared/agent-session/agent-session-wire' -import { join } from 'node:path' -import { journalSqlLifecycleRows } from './journal-sql-reducer-rows.test-fixture' -import { journalSqlRetryRows } from './journal-sql-retry-rows.test-fixture' -import { publishJournalOutcomeEpoch } from './outcomes/journal-outcome-write' -import { restoreJournal } from './replay/journal-open' -import { renderJournalState } from './projection/journal-reducer' - -function convert() { - const database = openTestJournalHostDatabase(fixture.root) - database.transaction((db) => convertJournalSqlProjection(db, 'session-1')) - return database.db -} - -it('preserves default, clamped, backward, hydration and reset wire pages from a converted chat', async () => { - await appendItems(44) - const db = convert() - for (const limit of [undefined, 2, 0, 10000]) { - const request = { sessionId: 'session-1', direction: 'tail', limit } as const - const actual = readJournalSqlHistoryPage(db, request) - expect(actual).toEqual(readAgentSessionHistory(fixture.journal, request)) - if (!actual.ok) { - throw new Error('expected tail') - } - const before = { - ...request, - direction: 'before', - cursor: actual.page.window.nextCursor - } as const - expect(readJournalSqlHistoryPage(db, before)).toEqual( - readAgentSessionHistory(fixture.journal, before) - ) - } - expect( - readJournalSqlHistoryPage(db, { sessionId: 'session-1', direction: 'hydrate', fence: 8 }) - ).toEqual({ ok: true, page: readAgentSessionHydrationPage(fixture.journal, 8) }) - for (const cursor of [ - { epoch: 'old', sequence: 2 }, - { epoch: fixture.journal.epoch, sequence: 999 } - ]) { - const request = { sessionId: 'session-1', direction: 'before', cursor } as const - expect(readJournalSqlHistoryPage(db, request)).toEqual( - readAgentSessionHistory(fixture.journal, request) - ) - } - const pool = new JournalSqlReaderPool( - join(fixture.root, 'agent-session-journal.db'), - join(fixture.root, 'agent-session-journal.db'), - journalSqlReaderPath() - ) - try { - const request = { sessionId: 'session-1', direction: 'tail', limit: 2 } as const - expect(await Promise.all(Array.from({ length: 5 }, () => pool.read(request)))).toEqual( - Array.from({ length: 5 }, () => readAgentSessionHistory(fixture.journal, request)) - ) - } finally { - await pool.close() - } - await expect(pool.read({ sessionId: 'session-1', direction: 'tail' })).rejects.toThrow('closed') -}) - -it('keeps subagent sibling windows, producer scopes and off-page labels identical', async () => { - await fixture.journal.appendItem( - item(1), - { - kind: 'message', - role: 'system', - blocks: [ - { - type: 'subagent-group', - groupId: 'roster', - agents: [{ id: 'child', label: 'Read files', state: 'completed' }] - } - ] - }, - { fence: 1, turnScope: { kind: 'thread' } } - ) - await fixture.journal.appendItem(item(2), body('child output'), { - fence: 1, - turnScope: { kind: 'thread' }, - agentId: 'child' - }) - for (let index = 3; index < 11; index++) { - await fixture.journal.appendItem( - item(index), - index === 4 || index === 8 - ? { - kind: 'message', - role: 'system', - blocks: [ - { - type: 'subagent', - groupId: 'siblings', - id: `helper-${index}`, - label: 'Read', - state: 'completed' - } - ] - } - : body(`row ${index}`), - { fence: 1, turnScope: { kind: 'thread' } } - ) - } - await fixture.journal.appendLifecycleBatch({ - settlementId: 'siblings-at-one-sequence', - fence: 1, - mutations: [11, 12].map((ordinal) => ({ - kind: 'item' as const, - identity: item(ordinal), - body: body(`batch ${ordinal}`), - turnScope: { kind: 'thread' as const } - })) - }) - const db = convert() - const tail = readJournalSqlHistoryPage(db, { - sessionId: 'session-1', - direction: 'tail', - limit: 1 - }) - expect(tail).toMatchObject({ - ok: true, - page: { - items: [ - expect.objectContaining({ body: body('batch 11') }), - expect.objectContaining({ body: body('batch 12') }) - ] - } - }) - for (const scope of ['every-agent', 'own-agent'] as const) { - for (const limit of [1, 2, 3, 20]) { - const request = { sessionId: 'session-1', direction: 'tail', limit } as const - expect(readJournalSqlHistoryPage(db, { ...request, scope })).toEqual( - readAgentSessionHistory(fixture.journal, request, { scope }) - ) - } - } -}) - -it('applies byte bounds to wire values without truncating stored bodies', async () => { - await fixture.journal.appendItem(item(1), body('older'), { - fence: 1, - turnScope: { kind: 'thread' } - }) - await fixture.journal.appendItem(item(2), body('x'.repeat(4000)), { - fence: 1, - turnScope: { kind: 'thread' } - }) - await fixture.journal.appendItem(item(3), body('newer'), { - fence: 1, - turnScope: { kind: 'thread' } - }) - const db = convert() - const reader = { budgetBytes: 1024, form: measureHistoryItem } - const request: AgentSessionHistoryRequest = { sessionId: 'session-1', direction: 'tail' } - expect(readJournalSqlHistoryPage(db, { ...request, direction: 'tail' }, reader)).toEqual( - readAgentSessionHistory(fixture.journal, request, { reader }) - ) - const before = { - ...request, - direction: 'before', - cursor: { epoch: fixture.journal.epoch, sequence: 4 } - } as const - expect(readJournalSqlHistoryPage(db, before, reader)).toEqual( - readAgentSessionHistory(fixture.journal, before, { reader }) - ) - expect(fixture.journal.snapshot().items[1]!.body).toEqual(body('x'.repeat(4000))) -}) - -it.each([ - { name: 'lifecycle', rows: journalSqlLifecycleRows() }, - { name: 'native retry', rows: journalSqlRetryRows(false) }, - { name: 'legacy retry', rows: journalSqlRetryRows(true) } -])('matches memory pages for the existing $name fixture', ({ rows }) => { - const database = openTestJournalHostDatabase(fixture.root) - database.transaction((db) => { - publishTestJournalEpoch(db, 'session', 'epoch') - publishJournalOutcomeEpoch(db, 'session', 'epoch', rows).adopt() - convertJournalSqlProjection(db, 'session') - }) - const snapshot = renderJournalState(restoreJournal(database.db, 'session')!.state) - for (const limit of [1, 2, 40]) { - const request = { sessionId: 'session', direction: 'tail', limit } as const - const actual = readJournalSqlHistoryPage(database.db, request) - expect(actual).toEqual(readAgentSessionHistory(fixture.journal, request, { snapshot })) - if (!actual.ok) { - throw new Error('expected page') - } - const before = { - ...request, - direction: 'before', - cursor: actual.page.window.nextCursor - } as const - expect(readJournalSqlHistoryPage(database.db, before)).toEqual( - readAgentSessionHistory(fixture.journal, before, { snapshot }) - ) - } -}) - -it('answers that a chat is converting instead of serving its partly written rows', async () => { - await appendItems(3) - const db = convert() - db.prepare(`UPDATE journal_proj_meta SET state='converting'`).run() - expect(() => - readJournalSqlHistoryPage(db, { sessionId: 'session-1', direction: 'tail' }) - ).toThrow(JournalProjectionStaleError) -}) diff --git a/src/main/native-chat/agent-session-journal/journal-sql-input-command.test-fixture.ts b/src/main/native-chat/agent-session-journal/journal-sql-input-command.test-fixture.ts deleted file mode 100644 index a92d94977fb..00000000000 --- a/src/main/native-chat/agent-session-journal/journal-sql-input-command.test-fixture.ts +++ /dev/null @@ -1,190 +0,0 @@ -import { mkdtemp, rm } from 'node:fs/promises' -import { tmpdir } from 'node:os' -import { join } from 'node:path' -import { afterEach, beforeEach, expect } 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 { codexProviderHandle } from '../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' -import Database from '../../sqlite/sync-database' -import { - closeTestJournalHostDatabases, - openTestJournalHostDatabase -} from './store-lifecycle/journal-host-database-test-support' -import { seedJournalSqlCommands } from './journal-sql-command.test-fixture' -import { - createJournalStoreCollaborators, - type JournalStoreCollaborators -} from './store-lifecycle/journal-store-collaborators' -import type { JournalHostDatabase } from './database/journal-host-database' -import { JournalWriteQueue } from './writing/journal-write-queue' -import { prepareJournalOutcomeWrite } from './outcomes/journal-outcome-write' -import { persistJournalOutcomeRows } from './outcomes/journal-outcome-table' -import { - ensureJournalOutcomeTables, - startJournalOutcomeLog -} from './outcomes/journal-outcome-format' -import { createJournalReducerState } from './projection/journal-reducer' -import { journalInputOrder } from './input-order/journal-input-order' -import { - journalContextCursor, - physicalJournalCursor -} from './context-epochs/journal-context-cursor' -import type { JournalRecoveryJournal } from './submissions/journal-pending-submission-recovery' -import { journalSqlLifecycleRows } from './journal-sql-reducer-rows.test-fixture' -import { createJournalTablesSql } from './database/journal-database-schema' -import { JournalSqlCommand } from './journal-sql-command' -import { JournalSqlBatch } from './journal-sql-batch' -import type { JournalWriterCommand, JournalWriterOperation } from './journal-writer-command' -import type { JournalWriterHostFacts } from './journal-writer-host-facts' -import { journalSqlFailure, restoreJournalSqlFailure } from './journal-sql-failure' - -export const identity: AgentSessionJournalIdentity = { - sessionId: 'session', - workspaceId: 'workspace', - hostId: 'host', - agent: 'codex', - providerHandle: codexProviderHandle('thread') -} -export const target = { sessionId: 'session', epoch: 'epoch', ownerToken: 'open-token' } -export const NOW = 1_000 -export const text = (value: string): AgentJournalMessageItem => ({ - kind: 'message', - role: 'user', - blocks: [{ type: 'text', text: value }] -}) - -let roots: string[] = [] -beforeEach(() => { - roots = [] -}) -afterEach(async () => { - await closeTestJournalHostDatabases() - for (const root of roots) { - await rm(root, { recursive: true, force: true }) - } -}) - -/** Today's collaborators over an in-memory fold of the same seeded rows (no actual open on either side). */ -export function memoryJournal(database: JournalHostDatabase) { - const scratch = new Database(':memory:') - scratch.exec(createJournalTablesSql()) - ensureJournalOutcomeTables(scratch) - const source = startJournalOutcomeLog(scratch, 'session', 'epoch', 0, 100) - let state = persistJournalOutcomeRows( - scratch, - createJournalReducerState('session', 'epoch'), - source, - [journalSqlLifecycleRows()[0]!], - 100, - true, - (next) => journalInputOrder(next).cards.ensure(next) - ).adopt() - scratch.close() - const collaborators: JournalStoreCollaborators = createJournalStoreCollaborators({ - notifyCommitted: () => undefined, - identity, - now: () => NOW, - mintEpoch: () => 'minted', - serialize: (run) => queue.serialize(run), - database: () => database, - state: () => state, - readOnly: () => false, - cursor: () => journalContextCursor(state), - adopt: (loaded) => { - state = loaded.state - }, - prepare: (db, rows, savedAt, finishFold) => { - collaborators.queuedMessages.inputState() - const transition = prepareJournalOutcomeWrite(db, state, rows, savedAt, finishFold) - return () => { - state = transition.adopt() - } - }, - commit: () => undefined, - journal: () => ({ - reopenFloor: () => null, - appendItem: (...args) => collaborators.itemAppender.append(...args) - }), - enqueue: (build, receipt) => collaborators.rowWriter.enqueue(build, receipt) - }) - const queue = new JournalWriteQueue('session') - const recovery: JournalRecoveryJournal = { - get inputOrder() { - return journalInputOrder(state) - }, - resolveDispatch: (...args) => collaborators.submissionWriter.resolveDispatch(...args), - queuedMessages: collaborators.queuedMessages, - physicalCursor: () => physicalJournalCursor(state), - itemBody: (itemId) => state.items.get(itemId)?.body ?? null - } - return Object.assign(collaborators, { - recovery, - cursor: () => journalContextCursor(state), - state: () => state - }) -} - -export async function sides() { - const memoryRoot = await mkdtemp(join(tmpdir(), 'orca-sql-input-memory-')) - const sqlRoot = await mkdtemp(join(tmpdir(), 'orca-sql-input-writer-')) - roots.push(memoryRoot, sqlRoot) - const memoryDatabase = openTestJournalHostDatabase(memoryRoot) - seedJournalSqlCommands(memoryDatabase.db) - const memory = memoryJournal(memoryDatabase) - const sqlDatabase = openTestJournalHostDatabase(sqlRoot) - seedJournalSqlCommands(sqlDatabase.db) - const engine = new JournalSqlCommand(sqlDatabase.db, sqlRoot) - const batch = new JournalSqlBatch(sqlDatabase.db) - const run = async ( - operation: JournalWriterOperation, - facts: Partial = {} - ) => { - const command: JournalWriterCommand = { - target, - operation, - facts: { identity, ...facts }, - savedAt: NOW - } - const [outcome] = (await batch.run([command], (entry) => engine.apply(entry, NOW))).outcomes - if (!outcome!.committed) { - // Exactly what reaches the host through the worker port. - throw restoreJournalSqlFailure(journalSqlFailure(outcome!.error)) - } - return outcome!.value - } - return { memory, memoryDb: memoryDatabase.db, sqlDb: sqlDatabase.db, run } -} - -export const saved = (db: Database.Database) => ({ - rows: db.prepare('SELECT seq, row_json FROM journal_rows ORDER BY seq').all(), - outcomes: db - .prepare(`SELECT seq, field, entry_key, format, ordinal, value_json, deleted - FROM journal_outcomes ORDER BY seq, field, entry_key`) - .all() -}) - -export async function both( - side: Awaited>, - memory: (journal: ReturnType) => Promise, - operation: JournalWriterOperation, - facts?: Partial -) { - const expected = await memory(side.memory).then( - (value) => ({ value }), - (error: unknown) => ({ error }) - ) - const actual = await side.run(operation, facts).then( - (commit) => ({ value: commit.value }), - (error: unknown) => ({ error }) - ) - if ('error' in expected) { - expect(actual).toMatchObject({ error: expect.any(Error) }) - const name = (result: { error: unknown } | { value: unknown }) => - 'error' in result && result.error instanceof Error ? result.error.name : undefined - expect(name(actual)).toBe(name(expected)) - } else { - expect(actual).toEqual(expected) - } - expect(saved(side.sqlDb)).toEqual(saved(side.memoryDb)) - return actual -} diff --git a/src/main/native-chat/agent-session-journal/journal-sql-input-commands.test.ts b/src/main/native-chat/agent-session-journal/journal-sql-input-commands.test.ts deleted file mode 100644 index 06370fee292..00000000000 --- a/src/main/native-chat/agent-session-journal/journal-sql-input-commands.test.ts +++ /dev/null @@ -1,597 +0,0 @@ -import { expect, it } from 'vitest' -import type { AgentMessageSource } from '../../../shared/agent-session/agent-session-message-source' -import { - markJournalPendingSubmissionsUnknown, - rejectJournalPendingSubmissions, - rejectJournalQueuedSubmissions -} from './submissions/journal-pending-submission-recovery' -import { holdUnsentSends } from './queued-messages/journal-unsent-send-hold' -import { journalTombstoneRowBuilder } from './writing/journal-row-builders' -import { agentJournalItemKey } from '../../../shared/agent-session/journal/agent-session-journal-item-key' -import { agentSessionFailureFact } from '../../../shared/agent-session/failures/agent-session-failure' -import { agentSessionFailureWords } from '../../../shared/agent-session/failures/agent-session-failure-words' -import { journalSubmissionSelected } from './journal-submission-selector' -import { JournalHostFactsNeededError, journalMailSenderKey } from './journal-writer-host-facts' -import { QueuedMessageNotConsumableError } from './queued-messages/queued-message-consume-error' -import { journalSqlFailure } from './journal-sql-failure' -import { structuredAgentSessionCommandTurn } from '../../../shared/agent-session/view/structured-agent-session-command-turn-identity' -import type { JournalCommandSettlementInput } from './journal-store-contracts' - -import { sides, both, text } from './journal-sql-input-command.test-fixture' - -it('runs queued-card commands with the host collaborators and saves identical records', async () => { - const side = await sides() - await both(side, (j) => j.queuedMessages.insert({ messageId: 'a', body: text('a') }), { - kind: 'queue-insert', - input: { messageId: 'a', body: text('a') } - }) - await both(side, (j) => j.queuedMessages.insert({ messageId: 'b', body: text('b') }), { - kind: 'queue-insert', - input: { messageId: 'b', body: text('b') } - }) - // A duplicate admission appends nothing and answers the existing card. - await both(side, (j) => j.queuedMessages.insert({ messageId: 'a', body: text('again') }), { - kind: 'queue-insert', - input: { messageId: 'a', body: text('again') } - }) - const card = side.memory.queuedMessages.get('a')! - const update = { - messageId: 'a', - expectedBodyFingerprint: card.fingerprint, - text: 'a edited' - } - await both(side, (j) => j.queuedMessages.update(update), { kind: 'queue-update', input: update }) - // The same fingerprint is now stale on both sides. - await both(side, (j) => j.queuedMessages.update(update), { kind: 'queue-update', input: update }) - const move = { messageId: 'b', beforeMessageId: 'a' } as const - await both(side, (j) => j.queuedMessages.move(move), { kind: 'queue-move', input: move }) - await both(side, (j) => j.queuedMessages.hold({ messageIds: ['a'], reason: 'send_failed' }), { - kind: 'queue-hold', - messageIds: ['a'], - reason: 'send_failed' - }) - await both(side, (j) => j.queuedMessages.withdraw(['b', 'missing']), { - kind: 'queue-withdraw', - messageIds: ['b', 'missing'] - }) -}) - -it('hands over a card and settles sends with the same validation and records', async () => { - const side = await sides() - await both(side, (j) => j.queuedMessages.insert({ messageId: 'c', body: text('c') }), { - kind: 'queue-insert', - input: { messageId: 'c', body: text('c') } - }) - const fingerprint = side.memory.queuedMessages.get('c')!.fingerprint - const handover = { - clientMessageId: 'send-c', - payloadFingerprint: fingerprint, - body: text('c'), - fence: 1 - } - const consume = { messageId: 'c', expect: 'waiting' as const } - // A wrong expectation refuses on both sides with the same error identity. - await both(side, (j) => j.submissionWriter.append(handover, { ...consume, expect: 'returned' }), { - kind: 'submission', - input: handover, - consume: { ...consume, expect: 'returned' } - }) - await both(side, (j) => j.submissionWriter.append(handover, consume), { - kind: 'submission', - input: handover, - consume - }) - const send = { - clientMessageId: 'send-d', - payloadFingerprint: 'fp-d', - body: text('d'), - fence: 1, - origin: 'client' as const - } - await both(side, (j) => j.submissionWriter.append(send), { kind: 'submission', input: send }) - const pending = { - clientMessageId: 'send-d', - state: 'pending' as const, - fence: 1, - turnScope: { kind: 'thread' as const } - } - await both(side, (j) => j.submissionWriter.resolveDispatch(pending), { - kind: 'dispatch', - input: pending - }) - const accepted = { - clientMessageId: 'send-d', - state: 'accepted' as const, - fence: 1, - providerIdentity: null - } - await both(side, (j) => j.submissionWriter.acceptLate(accepted), { - kind: 'accept-late', - input: accepted - }) - // Settled: a second late acceptance appends nothing. - await both(side, (j) => j.submissionWriter.acceptLate(accepted), { - kind: 'accept-late', - input: accepted - }) -}) - -it('claims stopped history at handover and releases it after a proven unwritten refusal', async () => { - const side = await sides() - const submit = async (id: string) => { - const input = { - clientMessageId: id, - payloadFingerprint: id, - body: text(id), - fence: 1, - origin: 'client' as const, - source: { kind: 'user' as const }, - handoverRecorded: true as const - } - await both(side, (j) => j.submissionWriter.append(input), { kind: 'submission', input }) - } - const handover = (id: string) => { - const input = { - clientMessageId: id, - fence: 1, - turnScope: { kind: 'thread' as const }, - body: text(id) - } - return both(side, (j) => j.submissionWriter.prepareHandover(input), { - kind: 'handover', - input - }) - } - const refuse = (id: string) => { - const input = { - clientMessageId: id, - state: 'rejected' as const, - fence: 1, - ...agentSessionFailureWords( - agentSessionFailureFact('cancelled', { neverReachedAgent: true }), - { surface: 'rejection' } - ) - } - return both(side, (j) => j.submissionWriter.resolveDispatch(input), { - kind: 'dispatch', - input - }) - } - await submit('owed') - await handover('owed') - await refuse('owed') - await submit('carrier-a') - await submit('carrier-b') - expect(await handover('carrier-a')).toEqual({ - value: { messages: [{ clientMessageId: 'owed', words: 'owed' }], covers: ['owed'] } - }) - expect(await handover('carrier-b')).toEqual({ value: null }) - await refuse('carrier-a') - expect(await handover('carrier-b')).toEqual({ - value: { - messages: [ - { clientMessageId: 'owed', words: 'owed' }, - { clientMessageId: 'carrier-a', words: 'carrier-a' } - ], - covers: ['owed', 'carrier-a'] - } - }) -}) - -it('asks the host for mail availability and handover answers instead of calling back', async () => { - const side = await sides() - const from: AgentMessageSource = { - kind: 'agent', - senders: [], - orchestration: { message: 'mail-notice', mailbox: 'mailbox', dispatchId: null, messages: [] } - } - const mail = { ...text('mail'), from } - await both(side, (j) => j.queuedMessages.insert({ messageId: 'm', body: mail }), { - kind: 'queue-insert', - input: { messageId: 'm', body: mail } - }) - const fingerprint = side.memory.queuedMessages.get('m')!.fingerprint - const input = { clientMessageId: 'send-m', payloadFingerprint: fingerprint, body: mail, fence: 1 } - const consume = { messageId: 'm', expect: 'waiting' as const, yieldsToPause: true as const } - await expect( - side.run({ kind: 'submission', input, consume: { ...consume, owed: true } }) - ).rejects.toEqual(new JournalHostFactsNeededError({ mail: [mail.from] })) - const known = { mail: [[journalMailSenderKey(mail.from), false]] as const } - await expect( - side.run({ kind: 'submission', input, consume: { ...consume, owed: true } }, known) - ).rejects.toEqual(new JournalHostFactsNeededError({ owed: true })) - // Unavailable mail is passed over, so an automatic drain may not take it. - await expect( - side.run( - { kind: 'submission', input, consume: { ...consume, owed: true } }, - { mail: [[journalMailSenderKey(mail.from), true]], owed: 'owed' } - ) - ).rejects.toBeInstanceOf(QueuedMessageNotConsumableError) - await both( - side, - (j) => { - j.queuedMessages.mailUnavailable = () => false - return j.submissionWriter.append(input, { ...consume, owed: () => 'owed' }) - }, - { kind: 'submission', input, consume: { ...consume, owed: true } }, - { ...known, owed: 'owed' } - ) -}) - -it('runs recovery procedures with serializable selectors on the writer', async () => { - const side = await sides() - const settled = (run: Promise) => run.then((value) => ({ value })) - const send = (id: string) => ({ - clientMessageId: id, - payloadFingerprint: `fp-${id}`, - body: text(id), - fence: 1, - origin: 'client' as const - }) - for (const id of ['send-a', 'send-b', 'send-c']) { - await both(side, (j) => j.submissionWriter.append(send(id)), { - kind: 'submission', - input: send(id) - }) - } - const pending = { - clientMessageId: 'send-a', - state: 'pending' as const, - fence: 1, - turnScope: { kind: 'thread' as const } - } - await both(side, (j) => j.submissionWriter.resolveDispatch(pending), { - kind: 'dispatch', - input: pending - }) - await both(side, (j) => settled(markJournalPendingSubmissionsUnknown(j.recovery, 1)), { - kind: 'pending-unknown', - fence: 1 - }) - const only = { only: { clientMessageId: 'send-b', fence: 1 } } - await both( - side, - (j) => - settled( - holdUnsentSends(j.recovery, { - fence: 1, - hold: { cause: 'chatClosed', which: journalSubmissionSelected(only, j.cursor) } - }) - ), - { kind: 'hold-unsent', fence: 1, hold: { cause: 'chatClosed', select: only } } - ) - const rejection = agentSessionFailureWords(agentSessionFailureFact('chatClosed'), { - surface: 'rejection' - }) - const except = { except: ['send-c'] } - await both( - side, - (j) => - settled( - rejectJournalQueuedSubmissions( - j.recovery, - 1, - rejection, - journalSubmissionSelected(except, j.cursor) - ) - ), - { kind: 'queued-reject', fence: 1, rejection, select: except } - ) - await both(side, (j) => settled(rejectJournalQueuedSubmissions(j.recovery, 1, rejection)), { - kind: 'queued-reject', - fence: 1, - rejection - }) - await both(side, (j) => settled(rejectJournalPendingSubmissions(j.recovery, 1, rejection)), { - kind: 'pending-reject', - fence: 1, - rejection - }) -}) - -it('accepts Stop, claims goal uploads and withdraws a goal with host proofs carried as data', async () => { - const side = await sides() - const queued = { - clientMessageId: 'queued-send', - payloadFingerprint: 'fp', - body: text('queued'), - fence: 1, - origin: 'client' as const - } - await both(side, (j) => j.submissionWriter.append(queued), { kind: 'submission', input: queued }) - const event = { reason: 'user-stop' } as const - await both(side, (j) => j.stops.accept({ event, fence: 1, liveTurnId: () => 'turn-live' }), { - kind: 'stop-accept', - event, - fence: 1, - liveTurnId: 'turn-live' - }) - const identity = { provider: 'orca' as const, clientMessageId: 'goal' } - const goal = { ...text('goal'), sentAs: 'goal' as const } - const options = { fence: 1, turnScope: { kind: 'thread' as const } } - await both(side, (j) => j.itemAppender.appendClaimingAttachments(identity, goal, options), { - kind: 'item-claiming', - identity, - body: goal, - options - }) - const proofs = side.memory.goalAttachments.snapshot() - expect(proofs.goals.map(([id]) => id)).toEqual([agentJournalItemKey(identity)]) - await both( - side, - (j) => - j.rowWriter.append( - journalTombstoneRowBuilder(j.state, agentJournalItemKey(identity), 1), - j.goalAttachments.withdraw(agentJournalItemKey(identity)) - ), - { kind: 'goal-withdraw', identity, fence: 1 }, - { goals: proofs } - ) - expect(side.memory.goalAttachments.snapshot()).toEqual({ uploads: [], goals: [] }) -}) - -it('uses the preceding handover to distinguish linked from direct unrun late settlements', async () => { - const side = await sides() - const rejection = agentSessionFailureWords(agentSessionFailureFact('cancelled'), { - surface: 'rejection' - }) - await both(side, (j) => j.queuedMessages.insert({ messageId: 'card', body: text('linked') }), { - kind: 'queue-insert', - input: { messageId: 'card', body: text('linked') } - }) - for (const id of ['linked', 'direct']) { - const input = { - clientMessageId: id, - payloadFingerprint: - id === 'linked' ? side.memory.queuedMessages.get('card')!.fingerprint : 'direct-fp', - body: text(id), - fence: 1, - handoverRecorded: true as const - } - const consume = id === 'linked' ? { messageId: 'card', expect: 'waiting' as const } : undefined - await both(side, (j) => j.submissionWriter.append(input, consume), { - kind: 'submission', - input, - consume - }) - const settlement = { - clientMessageId: id, - state: 'rejected' as const, - fence: 1, - unrun: true as const, - ...rejection - } - await both(side, (j) => j.submissionWriter.settleLate(settlement), { - kind: 'late-dispatch', - input: settlement - }) - } - expect(side.memory.state().submissions.get('linked')).toMatchObject({ - dispatchState: 'rejected', - rejection: { kind: 'cancelled', neverReachedAgent: true } - }) - expect(side.memory.state().submissions.get('direct')).toMatchObject({ - dispatchState: 'rejected', - rejection: { kind: 'cancelled' } - }) - expect(side.memory.state().submissions.get('direct')?.rejection).not.toHaveProperty( - 'neverReachedAgent' - ) -}) - -it('binds Stop notes to the provider-named turn at their write position', async () => { - const side = await sides() - const turnIdentity = { provider: 'orca' as const, clientMessageId: 'stopped-turn' } - const body = { kind: 'turn' as const, turnId: 'provider-turn', state: 'running' as const } - const options = { fence: 1, turnScope: { kind: 'thread' as const } } - await both(side, (j) => j.itemAppender.append(turnIdentity, body, options), { - kind: 'item', - identity: turnIdentity, - body, - options - }) - const identity = { provider: 'orca' as const, clientMessageId: 'stop-note' } - const note = { kind: 'status' as const, text: 'Cancellation requested.' } - const stopped = { ...options, stoppedTurnId: 'provider-turn' } - await both(side, (j) => j.itemAppender.append(identity, note, stopped), { - kind: 'item', - identity, - body: note, - options: stopped - }) - expect(side.memory.state().items.get(agentJournalItemKey(identity))?.turnScope).toEqual({ - kind: 'turn', - turnItemId: 'orca:stopped-turn' - }) - await both( - side, - (j) => j.rowWriter.append(journalTombstoneRowBuilder(j.state, 'orca:stopped-turn', 1)), - { kind: 'tombstone', identity: turnIdentity, fence: 1 } - ) - const afterRemoval = { provider: 'orca' as const, clientMessageId: 'stop-after-removal' } - await both(side, (j) => j.itemAppender.append(afterRemoval, note, stopped), { - kind: 'item', - identity: afterRemoval, - body: note, - options: stopped - }) - expect(side.memory.state().items.get(agentJournalItemKey(afterRemoval))?.turnScope).toEqual({ - kind: 'thread' - }) -}) - -it('records a shutdown cut only for an interrupted turn and does not duplicate it', async () => { - const side = await sides() - const identity = { provider: 'orca' as const, clientMessageId: 'quit-cut' } - const input = { - identity, - body: { kind: 'status' as const, text: 'Orca quit', orcaStop: { cause: 'quit' as const } }, - turnItemId: 'orca:quit-turn', - fence: 1 - } - const cut = () => - both(side, (j) => j.itemAppender.appendShutdownCut(input), { - kind: 'shutdown-cut', - input - }) - await cut() - expect(side.memory.state().items.has('orca:quit-cut')).toBe(false) - const turnIdentity = { provider: 'orca' as const, clientMessageId: 'quit-turn' } - const options = { fence: 1, turnScope: { kind: 'thread' as const } } - const finished = { - kind: 'turn' as const, - turnId: 'turn', - state: 'completed' as const, - outcome: 'success' as const - } - await both(side, (j) => j.itemAppender.append(turnIdentity, finished, options), { - kind: 'item', - identity: turnIdentity, - body: finished, - options - }) - await cut() - expect(side.memory.state().items.has('orca:quit-cut')).toBe(false) - const interrupted = { ...finished, state: 'interrupted' as const, outcome: undefined } - await both(side, (j) => j.itemAppender.append(turnIdentity, interrupted, options), { - kind: 'item', - identity: turnIdentity, - body: interrupted, - options - }) - await cut() - expect(side.memory.state().items.get('orca:quit-cut')?.body).toMatchObject({ - kind: 'status', - orcaStop: { cause: 'quit' } - }) - const cursor = side.memory.cursor() - await cut() - expect(side.memory.cursor()).toEqual(cursor) -}) - -it('settles unsent commands against current turns and keeps dispatch when lifecycle fails', async () => { - const side = await sides() - const rejection = agentSessionFailureWords(agentSessionFailureFact('cancelled'), { - surface: 'rejection' - }) - const failureBody = { kind: 'status' as const, text: 'Command failed', tone: 'error' as const } - const options = { fence: 1, turnScope: { kind: 'thread' as const } } - const settled = (run: Promise) => - run.then( - (value) => ({ value }), - (error: unknown) => ({ failure: journalSqlFailure(error) }) - ) - for (const id of ['rejected', 'unknown', 'completed', 'partial']) { - const input = { - clientMessageId: id, - payloadFingerprint: id, - body: text(id), - fence: 1 - } - await both(side, (j) => j.submissionWriter.append(input), { kind: 'submission', input }) - const turn = structuredAgentSessionCommandTurn(id) - const body = { - kind: 'turn' as const, - turnId: turn.turnId, - state: 'running' as const - } - await both(side, (j) => j.itemAppender.append(turn.identity, body, options), { - kind: 'item', - identity: turn.identity, - body, - options - }) - if (id === 'completed') { - const completed = { ...body, state: 'completed' as const, outcome: 'success' as const } - await both(side, (j) => j.itemAppender.append(turn.identity, completed, options), { - kind: 'item', - identity: turn.identity, - body: completed, - options - }) - } - if (id === 'partial') { - for (const db of [side.memoryDb, side.sqlDb]) { - db.exec(`CREATE TRIGGER fail_command_settlement BEFORE INSERT ON journal_outcomes - WHEN NEW.field='items' AND NEW.entry_key='"orca:command-result%3Apartial"' - BEGIN SELECT RAISE(ABORT, 'settlement failed'); END`) - } - } - const settlement: JournalCommandSettlementInput = { - dispatch: - id === 'unknown' - ? { clientMessageId: id, state: 'unknown', reason: 'lost reply', fence: 1 } - : { clientMessageId: id, state: 'rejected', ...rejection, fence: 1 }, - failureBody - } - const result = await both(side, (j) => settled(j.submissionWriter.settleCommand(settlement)), { - kind: 'command-settle', - input: settlement - }) - if (id !== 'partial') { - expect(result).toEqual({ value: { value: undefined } }) - } else { - expect(result).toMatchObject({ value: { failure: { message: 'settlement failed' } } }) - } - expect(side.memory.state().submissions.get(id)?.dispatchState).toBe( - id === 'unknown' ? 'unknown' : 'rejected' - ) - expect(side.memory.state().items.get(turn.itemId)?.body).toMatchObject({ - state: id === 'unknown' ? 'unverifiable' : id === 'partial' ? 'running' : 'completed', - ...(id === 'rejected' ? { outcome: 'failure', completedAt: 1_000 } : {}), - ...(id === 'completed' ? { outcome: 'success' } : {}) - }) - if (id === 'partial') { - expect(side.memory.state().items.has(agentJournalItemKey(turn.resultIdentity))).toBe(false) - } - } -}) - -it('opens command turns from the committed handover and retains it when the turn write fails', async () => { - const side = await sides() - for (const id of ['opened', 'opening-failed']) { - const input = { clientMessageId: id, payloadFingerprint: id, body: text(id), fence: 1 } - await both(side, (j) => j.submissionWriter.append(input), { kind: 'submission', input }) - const turn = structuredAgentSessionCommandTurn(id) - if (id === 'opening-failed') { - for (const db of [side.memoryDb, side.sqlDb]) { - db.exec(`CREATE TRIGGER fail_command_open BEFORE INSERT ON journal_outcomes - WHEN NEW.field='items' AND NEW.entry_key='"orca:command-turn%3Aopening-failed"' - BEGIN SELECT RAISE(ABORT, 'opening failed'); END`) - } - } - const opening = { - clientMessageId: id, - submittedAt: 10, - fence: 1, - turnScope: { kind: 'thread' as const } - } - const result = await both( - side, - (j) => - j.submissionWriter.openCommand(opening).then( - (value) => ({ value }), - (error: unknown) => ({ failure: journalSqlFailure(error) }) - ), - { kind: 'command-open', input: opening } - ) - expect(side.memory.state().submissions.get(id)).toMatchObject({ - handedOverAt: 1_000, - dispatchState: 'pending' - }) - if (id === 'opened') { - expect(result).toMatchObject({ - value: { - value: { turn, running: { requestedAt: 1_000, startedAt: 1_000, state: 'running' } } - } - }) - expect(side.memory.state().items.get(turn.itemId)?.body).toMatchObject({ - requestedAt: 1_000, - startedAt: 1_000 - }) - } else { - expect(result).toMatchObject({ value: { failure: { message: 'opening failed' } } }) - expect(side.memory.state().items.has(turn.itemId)).toBe(false) - } - } -}) diff --git a/src/main/native-chat/agent-session-journal/journal-sql-open.test.ts b/src/main/native-chat/agent-session-journal/journal-sql-open.test.ts deleted file mode 100644 index c68143f1c16..00000000000 --- a/src/main/native-chat/agent-session-journal/journal-sql-open.test.ts +++ /dev/null @@ -1,138 +0,0 @@ -import { afterEach, expect, it } from 'vitest' -import Database from '../../sqlite/sync-database' -import { agentJournalItemKey } from '../../../shared/agent-session/journal/agent-session-journal-item-key' -import { seedJournalSqlCommands } from './journal-sql-command.test-fixture' -import { JournalSqlCommand } from './journal-sql-command' -import { JournalSqlBatch } from './journal-sql-batch' -import { JournalSqlReducer } from './journal-sql-reducer' -import { JournalSqlScope } from './journal-sql-scope' -import { advanceJournalOutcomeSource } from './outcomes/journal-outcome-format' -import { currentJournalSqlEpoch, JournalProjectionStaleError } from './journal-sql-meta' -import type { JournalWriterCommand, JournalWriterOperation } from './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 { readJournalSessionEpoch } from './durable-rows/journal-row-table' - -const databases: Database.Database[] = [] -afterEach(() => { - for (const db of databases.splice(0)) { - db.close() - } -}) - -const identity: AgentSessionJournalIdentity = { - sessionId: 'session', - workspaceId: 'workspace', - hostId: 'host', - agent: 'codex', - providerHandle: codexProviderHandle('thread') -} - -const roster = { - provider: 'orca', - clientMessageId: 'codex-subagents:group' -} as const - -function fixture() { - const db = new Database(':memory:') - databases.push(db) - seedJournalSqlCommands(db) - const engine = new JournalSqlCommand(db, '/unused') - const batch = new JournalSqlBatch(db) - const run = async ( - ownerToken: string, - operation: JournalWriterOperation, - epoch = 'epoch', - sessionId = 'session' - ) => { - const command: JournalWriterCommand = { - target: { sessionId, epoch, ownerToken }, - operation, - facts: { identity: { ...identity, sessionId } } - } - const [outcome] = (await batch.run([command], (entry) => engine.apply(entry, 100))).outcomes - if (!outcome!.committed) { - throw outcome!.error - } - return outcome!.value - } - return { db, run } -} - -it('opens a converted chat from its projection and settles live work the last host left', async () => { - const { db, run } = fixture() - await run('open-token', { - kind: 'item', - identity: roster, - body: { - kind: 'message', - role: 'system', - blocks: [ - { - type: 'subagent-group', - groupId: 'group', - agents: [{ id: 'a', label: 'read', state: 'working', startedAt: 10 }] - } - ] - }, - options: { fence: 1, turnScope: { kind: 'thread' } } - }) - - const opened = await run('next-handle', { kind: 'open' }) - - expect(opened.value).toEqual({ epoch: 'epoch', sequence: 3 }) - expect(db.prepare('SELECT owner_token FROM journal_proj_meta').get()?.owner_token).toBe( - 'next-handle' - ) - const items = new JournalSqlReducer(new JournalSqlScope(db, 'session', 'epoch'), 'epoch').state - .items - const settled = items.get(agentJournalItemKey(roster)) - expect(settled?.recovered).toBe(true) - expect(settled?.body).toMatchObject({ - blocks: [{ agents: [{ id: 'a', state: 'unverifiable' }] }] - }) - await expect(run('open-token', { kind: 'queue-resume', fence: 1 })).rejects.toMatchObject({ - code: 'journal_closed' - }) -}) - -it('reconverts after an older build writes the log or removes legacy cards', async () => { - const outcome = fixture() - advanceJournalOutcomeSource(outcome.db, 'session', 'epoch', 2) - await expect(outcome.run('next-handle', { kind: 'open' })).rejects.toBeInstanceOf( - JournalProjectionStaleError - ) - - const raw = fixture() - raw.db - .prepare( - 'INSERT INTO journal_rows (session_id, epoch, seq, ts, row_json) VALUES (?, ?, ?, ?, ?)' - ) - .run('session', 'epoch', 9, 100, '{}') - await expect(raw.run('next-handle', { kind: 'open' })).rejects.toBeInstanceOf( - JournalProjectionStaleError - ) - - const legacy = fixture() - legacy.db - .prepare(`INSERT INTO queued_messages (session_id, message_id, position, body_json, - fingerprint, created_at, host_instance, state) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`) - .run('session', 'card', 0, '{}', 'fingerprint', 100, 'host', 'waiting') - await expect(legacy.run('next-handle', { kind: 'open' })).rejects.toBeInstanceOf( - JournalProjectionStaleError - ) -}) - -it('founds a chat with nothing saved on its first open, owned by that open', async () => { - const { db, run } = fixture() - const opened = await run('first-handle', { kind: 'open' }, '', 'fresh') - const epoch = readJournalSessionEpoch(db, 'fresh')! - - expect(opened.value).toEqual({ epoch, sequence: 1 }) - expect(currentJournalSqlEpoch(db, 'fresh')).toBe(epoch) - const resumed = await run('first-handle', { kind: 'queue-resume', fence: 1 }, epoch, 'fresh') - expect(resumed.cursor.sequence).toBe(2) - await expect( - run('other-handle', { kind: 'queue-resume', fence: 1 }, epoch, 'fresh') - ).rejects.toMatchObject({ code: 'journal_closed' }) -}) diff --git a/src/main/native-chat/agent-session-journal/journal-sql-query-contracts.test.ts b/src/main/native-chat/agent-session-journal/journal-sql-query-contracts.test.ts deleted file mode 100644 index 6889d63ef95..00000000000 --- a/src/main/native-chat/agent-session-journal/journal-sql-query-contracts.test.ts +++ /dev/null @@ -1,209 +0,0 @@ -import { afterEach, expect, it } from 'vitest' -import { - AGENT_SESSION_JOURNAL_SCHEMA_VERSION, - type AgentJournalRenderItem, - type AgentJournalSubmission -} from '../../../shared/agent-session/journal/agent-session-journal-types' -import { agentJournalSubmissionKey } from '../../../shared/agent-session/journal/agent-session-journal-item-key' -import Database from '../../sqlite/sync-database' -import { JournalSqlScope } from './journal-sql-scope' -import { JournalSqlReducer } from './journal-sql-reducer' -import { journalSqlProjectionSchema } from './journal-sql-projection-schema' -import { journalInputOrder } from './input-order/journal-input-order' -import { encodeJournalOutcomes, journalOutcomeScalars } from './outcomes/journal-outcome-codec' -import { journalOutcomeScalarJson } from './journal-outcome-scalars' - -const databases: Database.Database[] = [] -afterEach(() => { - for (const db of databases.splice(0)) { - db.close() - } -}) -function fixture() { - const db = new Database(':memory:') - databases.push(db) - db.exec(journalSqlProjectionSchema()) - return () => { - const scope = new JournalSqlScope(db, 'session', 'generation') - return { scope, command: new JournalSqlReducer(scope, 'epoch') } - } -} -function send(id: string, at = 1): AgentJournalSubmission { - return { - clientMessageId: id, - fence: 1, - payloadFingerprint: 'fp', - dispatchState: 'pending', - providerItemId: null, - reason: null, - submittedAt: at, - resolvedAt: null - } -} -function item( - id: string, - sequence: number, - body: AgentJournalRenderItem['body'] = { kind: 'message', role: 'user', blocks: [] } -): AgentJournalRenderItem { - return { itemId: id, sequence, observedAt: 1, revision: 0, body } -} - -it('uses separate echo eligibility before and after a clear, counts skipped accepted rows, and preserves equal-time order', () => { - const command = fixture() - let c = command() - for (const id of ['missing-acceptance', 'before-clear', 'after-clear', 'equal-time']) { - c.command.state.submissions.set(id, { - ...send(id), - ...(id === 'missing-acceptance' ? {} : { acceptedSequence: id === 'before-clear' ? 2 : 9 }) - }) - c.command.state.items.set(agentJournalSubmissionKey(id), item(agentJournalSubmissionKey(id), 2)) - } - c.command.flush() - c = command() - const q = c.command.state.queries - expect(q.echoClaimant(c.command.state, 'fp', false)).toBe( - agentJournalSubmissionKey('missing-acceptance') - ) - c.command.state.items.set( - 'clear', - item('clear', 5, { - kind: 'status', - text: 'Cleared', - contextClear: { operationId: 'clear', afterFence: 1, clearedAt: 1 } - }) - ) - c.command.flush() - expect(q.echoClaimant(c.command.state, 'fp', false)).toBe( - agentJournalSubmissionKey('after-clear') - ) - c.command.state.inputs.echoes.add(agentJournalSubmissionKey('after-clear')) - c.command.flush() - expect(q.echoClaimant(c.command.state, 'fp', false)).toBe(agentJournalSubmissionKey('equal-time')) - c.command.state.items.delete(agentJournalSubmissionKey('equal-time')) - c.command.flush() - expect(q.echoClaimant(c.command.state, 'fp', false)).toBeNull() -}) - -it('keeps fence candidates including ended sends, alias insertion order, strict rewind cuts and turn tie order', () => { - const command = fixture() - let c = command() - const state = c.command.state - state.submissions.set('ended', send('ended')) - state.inputs.candidates.set('ended', state.submissions.get('ended')!) - state.items.set(agentJournalSubmissionKey('ended'), item(agentJournalSubmissionKey('ended'), 1)) - state.inputs.candidates.set('other', { ...send('other'), fence: 2 }) - state.inputs.ended.add('ended') - state.inputs.handovers.set('other', { - v: AGENT_SESSION_JOURNAL_SCHEMA_VERSION, - epoch: 'epoch', - seq: 2, - ts: 1, - fence: 1, - kind: 'dispatch', - clientMessageId: 'other', - state: 'pending', - providerItemId: null, - reason: null - }) - state.aliases.set('provider-2', 'native') - state.aliases.set('provider-1', 'native') - state.aliases.set('foreign', 'another') - state.items.set('tail-first', item('tail-first', 9)) - state.items.set('floor', item('floor', 5)) - state.items.set('tail-second', item('tail-second', 8)) - state.items.set('turn-a', item('turn-a', 2, { kind: 'turn', turnId: 'a', state: 'running' })) - state.items.set('turn-b', { - ...item('turn-b', 2, { kind: 'turn', turnId: 'b', state: 'completed' }), - sequenceIndex: -1 - }) - c.command.flush() - c = command() - const q = c.command.state.queries - expect(q.candidatesForFence(c.command.state, 1).map((v) => v.clientMessageId)).toEqual([ - 'ended', - 'other' - ]) - expect( - [...q.candidatesForHandoverFence(c.command.state, 1)].map((v) => v.clientMessageId) - ).toEqual(['other']) - expect([...q.aliasesForSubmission(c.command.state, 'native')]).toEqual([ - 'provider-2', - 'provider-1' - ]) - expect([...q.itemsAfterSequence(c.command.state, 5)].map((v) => v.itemId)).toEqual([ - 'tail-first', - 'tail-second' - ]) - expect(q.newestTurnBySequence(c.command.state)).toEqual({ turnId: 'b', state: 'completed' }) - expect(q.latestItemForTurn(c.command.state, 'a')?.itemId).toBe('turn-a') - c.command.state.items.set( - 'orca:hidden-turn', - item('orca:hidden-turn', 20, { kind: 'turn', turnId: 'a', state: 'completed' }) - ) - c.command.state.inputs.rejections.set('hidden-turn', send('hidden-turn')) - expect(q.latestItemForTurn(c.command.state, 'a')?.itemId).toBe('turn-a') - c.command.state.inputs.rejections.delete('hidden-turn') - expect(q.latestItemForTurn(c.command.state, 'a')?.itemId).toBe('orca:hidden-turn') - c.command.state.items.delete('orca:hidden-turn') - const sourceOnly = command() - sourceOnly.command.state.submissions.get('ended')!.fence = 3 - sourceOnly.command.flush() - const afterAlias = command() - expect(afterAlias.command.state.submissions.get('ended')?.fence).toBe(3) - expect( - afterAlias.command.state.queries - .candidatesForFence(afterAlias.command.state, 3) - .map((v) => v.clientMessageId) - ).toEqual(['ended']) - const aliasOnly = command() - aliasOnly.command.state.inputs.candidates.get('ended')!.dispatchState = 'accepted' - aliasOnly.command.flush() - const accepted = command() - expect(accepted.command.state.queries.latestAcceptedUnopened(accepted.command.state)).toBe(true) - c.command.state.submissions.set('opener', { ...send('opener'), dispatchState: 'accepted' }) - c.command.state.items.set( - agentJournalSubmissionKey('opener'), - item(agentJournalSubmissionKey('opener'), 10) - ) - expect(q.latestAcceptedUnopened(c.command.state)).toBe(true) - c.command.state.aliases.set('provider-opener', agentJournalSubmissionKey('opener')) - c.command.state.items.set( - 'opened', - item('opened', 11, { - kind: 'turn', - turnId: 'opened', - state: 'running', - userItemId: 'provider-opener' - }) - ) - expect(q.latestAcceptedUnopened(c.command.state)).toBe(false) -}) - -it('saves nothing for an unchanged clear on a later command and preserves lifted clear bytes', () => { - const command = fixture() - let c = command() - c.command.state.queuePauseMarks.cleared = { - operationId: 'clear', - messageIds: Array.from({ length: 1000 }, (_, i) => `card-${i}`), - sequence: 5 - } - journalInputOrder(c.command.state).cards.ensure(c.command.state) - c.command.flush() - const before = journalOutcomeScalarJson(journalOutcomeScalars(c.command.state).queuePauseMarks) - c = command() - const previous = journalOutcomeScalars(c.command.state) - const marks = c.command.state.queuePauseMarks - const unchanged = marks.cleared - marks.cleared = unchanged - journalInputOrder(c.command.state).cards.ensure(c.command.state) - expect( - encodeJournalOutcomes(c.command.state, [], previous, c.command.context.untracked).values - ).toEqual([]) - const after = journalOutcomeScalarJson(journalOutcomeScalars(c.command.state).queuePauseMarks) - expect(after).toBe(before) - c.command.state.queuePauseMarks.cleared!.lifted = true - c.command.flush() - c = command() - expect(c.command.state.queuePauseMarks.cleared?.messageIds.length).toBe(1000) - expect(c.command.state.queuePauseMarks.cleared?.lifted).toBe(true) -}) diff --git a/src/main/native-chat/agent-session-journal/journal-sql-queue-recovery.test.ts b/src/main/native-chat/agent-session-journal/journal-sql-queue-recovery.test.ts deleted file mode 100644 index fbe17756287..00000000000 --- a/src/main/native-chat/agent-session-journal/journal-sql-queue-recovery.test.ts +++ /dev/null @@ -1,151 +0,0 @@ -import { afterEach, expect, it } from 'vitest' -import Database from '../../sqlite/sync-database' -import { journalDatabasePath } from './database/journal-host-database' -import { JournalSqlOwner } from './journal-sql-owner' -import { JournalSqlReaderPool } from './journal-sql-reader-pool' -import { JournalSqlConversions } from './journal-sql-conversions' -import { JournalSqlReducer } from './journal-sql-reducer' -import { JournalSqlScope } from './journal-sql-scope' -import { journalInputOrder } from './input-order/journal-input-order' -import { createFixture } from './queued-messages/journal-unsent-send-hold-contract-test-fixture' -import { - closeTestJournalHostDatabase, - liveTestJournalRows, - openTestJournalHostDatabase, - updateTestJournalRowJson -} from './store-lifecycle/journal-host-database-test-support' -import { - journalSqlReaderPath, - journalSqlWorkerPath -} from './journal-sql-worker-bundle.test-fixture' - -const fixture = createFixture() -const owners: JournalSqlOwner[] = [] -const pools: JournalSqlReaderPool[] = [] -afterEach(async () => { - await Promise.all(pools.splice(0).map((pool) => pool.close())) - await Promise.all(owners.splice(0).map((owner) => owner.close())) -}) - -async function workers() { - const filename = journalDatabasePath(fixture.root) - const writer = new JournalSqlOwner(filename, fixture.root, journalSqlWorkerPath()) - owners.push(writer) - await writer.opened() - const readers = new JournalSqlReaderPool(filename, filename, journalSqlReaderPath()) - pools.push(readers) - return { writer, readers } -} - -async function convert() { - await closeTestJournalHostDatabase(fixture.root) - const { writer, readers } = await workers() - const source = new Database(journalDatabasePath(fixture.root), { readonly: true }) - try { - const saved = () => [ - source.prepare('SELECT * FROM journal_rows ORDER BY seq').all(), - source.prepare('SELECT * FROM journal_outcomes ORDER BY generation,seq,field,entry_key').all() - ] - const before = saved() - const converter = new JournalSqlConversions(writer, readers, async () => {}) - try { - expect(await converter.ensure(fixture.IDENTITY.sessionId, false)).toBe('epoch-1') - } finally { - await converter.close() - } - expect(saved()).toEqual(before) - } finally { - source.close() - } - await readers.close() - await writer.close() -} - -async function openAndOrder() { - const { writer, readers } = await workers() - try { - await writer.write({ - target: { sessionId: fixture.IDENTITY.sessionId, epoch: 'epoch-1', ownerToken: 'open' }, - operation: { kind: 'open' }, - facts: { identity: fixture.IDENTITY } - }) - const db = new Database(journalDatabasePath(fixture.root), { readonly: true }) - try { - const state = new JournalSqlReducer( - new JournalSqlScope(db, fixture.IDENTITY.sessionId, 'epoch-1'), - 'epoch-1' - ).state - return journalInputOrder(state) - .cards.list() - .filter((card) => card.state === 'waiting') - .map((card) => card.messageId) - } finally { - db.close() - } - } finally { - await readers.close() - await writer.close() - } -} - -it('keeps witnessed predecessors through background conversion, a failed open and later restarts', async () => { - const journal = await fixture.open() - await journal.queuedMessages.insert({ - messageId: 'C1', - body: { ...fixture.message('/compact'), command: { name: 'compact' } } - }) - await fixture.accept(journal, 'S', { origin: 'client' }) - await journal.queuedMessages.insert({ messageId: 'C2', body: fixture.message('C2') }) - await journal.queuedMessages.move({ messageId: 'C2', beforeMessageId: 'C1' }) - await journal.close() - await convert() - const db = new Database(journalDatabasePath(fixture.root)) - try { - db.exec(`CREATE TRIGGER fail_recovery BEFORE INSERT ON journal_outcomes - BEGIN SELECT RAISE(ABORT, 'interrupted open'); END`) - await expect(openAndOrder()).rejects.toThrow() - db.exec('DROP TRIGGER fail_recovery') - } finally { - db.close() - } - expect(await openAndOrder()).toEqual(['C2', 'C1', 'S']) - expect(await openAndOrder()).toEqual(['C2', 'C1', 'S']) -}) - -it('distinguishes in-doubt imported handoffs from direct and linked proven-unwritten sends', async () => { - const journal = await fixture.open() - for (const id of ['H', 'S', 'Q']) { - await fixture.accept(journal, id, { origin: 'client' }) - } - await journal.close() - const { db } = openTestJournalHostDatabase(fixture.root) - for (const [send, card, position] of [ - ['H', 'legacy', 10], - ['Q', 'linked', 20] - ] as const) { - const stored = liveTestJournalRows(db, fixture.IDENTITY.sessionId).find( - (row) => JSON.parse(row.rowJson).clientMessageId === send - )! - const row = JSON.parse(stored.rowJson) - row.queuedMessageId = card - if (send === 'H') { - delete row.handoverRecorded - } - updateTestJournalRowJson(db, fixture.IDENTITY.sessionId, stored.seq, JSON.stringify(row)) - const body = fixture.message(`text of ${send}`) - db.prepare(`INSERT INTO queued_messages (session_id,message_id,position,body_json,fingerprint, - created_at,host_instance,state,consumed_as) VALUES (?,?,?,?,?,?,?,'dispatched',?)`).run( - fixture.IDENTITY.sessionId, - card, - position, - JSON.stringify(body), - fixture.fingerprint(body), - 100, - 'previous-host', - send - ) - } - await convert() - expect(await openAndOrder()).toEqual(['S', 'linked']) - expect(await openAndOrder()).toEqual(['S', 'linked']) -}) diff --git a/src/main/native-chat/agent-session-journal/journal-start-failure-settlement.test.ts b/src/main/native-chat/agent-session-journal/journal-start-failure-settlement.test.ts index da5d6e72535..95425efd523 100644 --- a/src/main/native-chat/agent-session-journal/journal-start-failure-settlement.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-start-failure-settlement.test.ts @@ -65,7 +65,7 @@ async function memory(root: string): Promise { journals.push(journal) return Object.assign(journal, { queued: (run: () => T) => journal.readInOrder(run), - cards: async () => journal.queuedMessages.list() + cards: async () => await journal.queuedMessages.list() }) } diff --git a/src/main/native-chat/agent-session-journal/journal-status-cuts.test.ts b/src/main/native-chat/agent-session-journal/journal-status-cuts.test.ts index 3b8d49a5698..80e22c00b5e 100644 --- a/src/main/native-chat/agent-session-journal/journal-status-cuts.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-status-cuts.test.ts @@ -1,76 +1,24 @@ -import { afterEach, expect, it } from 'vitest' +import { expect, it } from 'vitest' import type { AgentJournalRenderItem, AgentJournalSubmission } from '../../../shared/agent-session/journal/agent-session-journal-types' -import Database from '../../sqlite/sync-database' -import { - createJournalReducerState, - renderJournalItems, - type JournalReducerState -} from './projection/journal-reducer' -import { JournalSqlReducer } from './journal-sql-reducer' -import { JournalSqlScope } from './journal-sql-scope' -import { journalSqlProjectionSchema } from './journal-sql-projection-schema' +import { createJournalReducerState, renderJournalItems } from './projection/journal-reducer' import { journalInputOrder } from './input-order/journal-input-order' import { journalDispatchRowBuilder } from './writing/journal-row-builders' -import { - readJournalUsageLimitCut as memoryReadJournalUsageLimitCut, - readJournalOrcaStopCut as memoryReadJournalOrcaStopCut -} from './journal-status-cuts' +import { readJournalUsageLimitCut, readJournalOrcaStopCut } from './journal-status-cuts' import { newestStructuredAgentSessionTurn, newestStructuredAgentSessionTurnBySequence } from '../../../shared/agent-session/view/structured-agent-session-live-turn' -import { readAgentJournalTurn } from '../../../shared/agent-session/lifecycle/agent-session-turn-record' import { memoryJournalMap } from './journal-memory-collections' import { journalRewoundReceipt } from './journal-writer-rewind-receipt' import { JournalHostFactsNeededError } from './journal-writer-host-facts' import { agentSessionFailureFact } from '../../../shared/agent-session/failures/agent-session-failure' import { agentSessionFailureWords } from '../../../shared/agent-session/failures/agent-session-failure-words' -import { JournalSqlStatusContext } from './journal-sql-status-context' -import { readJournalSqlUsageLimitCut, readJournalSqlOrcaStopCut } from './journal-sql-status-cuts' - -const sqlOwners = new WeakMap() -function readJournalUsageLimitCut( - state: JournalReducerState, - ...args: Parameters extends [unknown, ...infer A] - ? A - : never -) { - const sql = sqlOwners.get(state) - if (!sql) { - return memoryReadJournalUsageLimitCut(state, ...args) - } - sql.flush() - return readJournalSqlUsageLimitCut(new JournalSqlStatusContext(sql.context.scope), ...args) -} -function readJournalOrcaStopCut( - state: JournalReducerState, - ...args: Parameters extends [unknown, ...infer A] ? A : never -) { - const sql = sqlOwners.get(state) - if (!sql) { - return memoryReadJournalOrcaStopCut(state, ...args) - } - sql.flush() - return readJournalSqlOrcaStopCut(new JournalSqlStatusContext(sql.context.scope), ...args) -} - -const databases: Database.Database[] = [] -afterEach(() => databases.splice(0).forEach((db) => db.close())) - -function fixture(backend: 'memory' | 'sql') { - if (backend === 'memory') { - return createJournalReducerState('session', 'epoch') - } - const db = new Database(':memory:') - databases.push(db) - db.exec(journalSqlProjectionSchema()) - const sql = new JournalSqlReducer(new JournalSqlScope(db, 'session', 'epoch'), 'epoch') - sqlOwners.set(sql.state, sql) - return sql.state +function fixture() { + return createJournalReducerState('session', 'epoch') } function item( id: string, @@ -81,252 +29,179 @@ function item( return { itemId: id, sequence, revision: 1, observedAt: 1, body, ...extra } } -it.each(['memory', 'sql'] as const)( - 'reads the first scoped limit and rechecks rewind completion in %s', - (backend) => { - const state = fixture(backend) - state.items.set( - 'failed', - item( - 'failed', - 10, - { - kind: 'turn', - turnId: 'failed', - state: 'completed', - outcome: 'failure', - completedAt: 100 - }, - { recoveredAt: 200 } - ) - ) - const scope = { turnScope: { kind: 'turn' as const, turnItemId: 'failed' } } - const limit = (resetsAt: number) => ({ - kind: 'status' as const, - ...agentSessionFailureWords( - agentSessionFailureFact('usageLimitReached', { - usageLimit: { resetsAt } - }), - { surface: 'row' } - ) - }) - state.items.set( - 'child-limit', - item('child-limit', 11, limit(300), { ...scope, agentId: 'child' }) - ) - state.items.set('first-limit', item('first-limit', 12, limit(400), scope)) - state.items.set('later-limit', item('later-limit', 13, limit(500), scope)) - expect(readJournalUsageLimitCut(state)).toEqual({ - turnItemId: 'failed', - resetsAt: 400, - turnEndedAt: 200 - }) - const cursor = { epoch: 'epoch', sequence: 13 } - const receipt = { write: () => {}, committed: () => {} } - expect(() => journalRewoundReceipt(undefined, receipt)(cursor).fold?.(state)).toThrow( - JournalHostFactsNeededError - ) - expect(() => - journalRewoundReceipt( - { cursor, cancel: { turnItemId: 'failed', resetsAt: 400 } }, - receipt - )(cursor).fold?.(state) - ).not.toThrow() - expect(readJournalUsageLimitCut(state, 12)).toBeNull() - - const command = { - kind: 'message' as const, - role: 'user' as const, - blocks: [], - command: { name: 'compact' as const } - } - state.items.set('command', item('command', 14, command)) - state.items.set( - 'command-turn', - item('command-turn', 15, { - kind: 'turn', - turnId: 'compact', - state: 'completed', - outcome: 'success', - userItemId: 'command' - }) - ) - expect(readJournalUsageLimitCut(state)?.resetsAt).toBe(400) - state.items.set('command', item('command', 14, { kind: 'message', role: 'user', blocks: [] })) - expect(readJournalUsageLimitCut(state)).toBeNull() - state.items.set('command', item('command', 14, command)) - state.items.delete('command-turn') - state.items.delete('command') - for (const turnScope of [ - undefined, - { kind: 'thread' as const }, - { kind: 'unknown' as const } - ]) { - state.items.set( - 'later-user', - item('later-user', 20, { kind: 'message', role: 'user', blocks: [] }, { turnScope }) - ) - expect(readJournalUsageLimitCut(state)).toBeNull() - } - state.items.set( - 'later-user', - item('later-user', 20, { kind: 'message', role: 'user', blocks: [] }, scope) - ) - expect(readJournalUsageLimitCut(state)?.resetsAt).toBe(400) - state.items.delete('later-user') - const pending: AgentJournalSubmission = { - clientMessageId: 'pending', - fence: 1, - payloadFingerprint: 'fp', - dispatchState: 'pending', - providerItemId: null, - reason: null, - submittedAt: 1, - resolvedAt: null - } - state.submissions.set('pending', pending) - const order = journalInputOrder(state) - order.afterRow( - state, - journalDispatchRowBuilder(() => state, { - clientMessageId: 'pending', - state: 'pending', - fence: 1, - turnScope: { kind: 'thread' } - })(21, 1) - ) - expect(readJournalUsageLimitCut(state)).toBeNull() - } -) - -it.each(['memory', 'sql'] as const)( - 'keeps visible and raw boundary order distinct and reads child stop causes in %s', - (backend) => { - const state = fixture(backend) - state.items.set( - 'orca:running', - item( - 'orca:running', - 10, - { kind: 'turn', turnId: 'running', state: 'running' }, - { sequenceIndex: 2 } - ) - ) - state.items.set( - 'completed', - item( - 'completed', - 10, - { - kind: 'status', - text: 'Ended', - turnLifecycle: { turnId: 'completed', state: 'interrupted' } - }, - { sequenceIndex: 1 } - ) - ) - const boundary = (raw?: true) => { - const sql = sqlOwners.get(state) - if (sql) { - sql.flush() - const item = [ - ...new JournalSqlStatusContext(sql.context.scope).select({ kind: 'boundary', raw }) - ][0]?.item - return readAgentJournalTurn(item?.body)?.turnId ?? null - } - return ( - (raw - ? newestStructuredAgentSessionTurnBySequence(memoryJournalMap(state.items).values()) - : newestStructuredAgentSessionTurn(renderJournalItems(state)) - )?.turnId ?? null - ) - } - expect(boundary()).toBe('running') - expect(boundary(true)).toBe('completed') - state.inputs.rejections.set('running', { - clientMessageId: 'running', - fence: 1, - payloadFingerprint: 'fp', - dispatchState: 'rejected', - providerItemId: null, - reason: null, - submittedAt: 1, - resolvedAt: 1 - }) - expect(boundary()).toBe('completed') - const scope = { - turnScope: { kind: 'turn' as const, turnItemId: 'completed' }, - agentId: 'child' - } - state.items.set( - 'child-stop', - item('child-stop', 11, { kind: 'status', text: 'Quit', orcaStop: { cause: 'quit' } }, scope) - ) - expect(readJournalOrcaStopCut(state)).toEqual({ turnItemId: 'completed', cause: 'quit' }) - state.items.set( - 'clear', - item('clear', 12, { - kind: 'status', - text: 'Cleared', - contextClear: { operationId: 'clear', afterFence: 1, clearedAt: 1 } - }) - ) - expect(boundary()).toBeNull() - expect(readJournalOrcaStopCut(state, 12)).toBeNull() - } -) - -it('reads changed cuts at the writer savepoint and discards their memo after rollback', () => { - const state = fixture('sql') - const sql = sqlOwners.get(state)! - const { db } = sql.context.scope +it('reads the first scoped limit and rechecks rewind completion', () => { + const state = fixture() state.items.set( 'failed', - item('failed', 1, { + item( + 'failed', + 10, + { + kind: 'turn', + turnId: 'failed', + state: 'completed', + outcome: 'failure', + completedAt: 100 + }, + { recoveredAt: 200 } + ) + ) + const scope = { turnScope: { kind: 'turn' as const, turnItemId: 'failed' } } + const limit = (resetsAt: number) => ({ + kind: 'status' as const, + ...agentSessionFailureWords( + agentSessionFailureFact('usageLimitReached', { + usageLimit: { resetsAt } + }), + { surface: 'row' } + ) + }) + state.items.set( + 'child-limit', + item('child-limit', 11, limit(300), { ...scope, agentId: 'child' }) + ) + state.items.set('first-limit', item('first-limit', 12, limit(400), scope)) + state.items.set('later-limit', item('later-limit', 13, limit(500), scope)) + expect(readJournalUsageLimitCut(state)).toEqual({ + turnItemId: 'failed', + resetsAt: 400, + turnEndedAt: 200 + }) + const cursor = { epoch: 'epoch', sequence: 13 } + const receipt = { write: () => {}, committed: () => {} } + expect(() => journalRewoundReceipt(undefined, receipt)(cursor).fold?.(state)).toThrow( + JournalHostFactsNeededError + ) + expect(() => + journalRewoundReceipt( + { cursor, cancel: { turnItemId: 'failed', resetsAt: 400 } }, + receipt + )(cursor).fold?.(state) + ).not.toThrow() + expect(readJournalUsageLimitCut(state, 12)).toBeNull() + + const command = { + kind: 'message' as const, + role: 'user' as const, + blocks: [], + command: { name: 'compact' as const } + } + state.items.set('command', item('command', 14, command)) + state.items.set( + 'command-turn', + item('command-turn', 15, { kind: 'turn', - turnId: 'turn', + turnId: 'compact', state: 'completed', - outcome: 'failure' + outcome: 'success', + userItemId: 'command' }) ) - const limit = (resetsAt: number) => + expect(readJournalUsageLimitCut(state)?.resetsAt).toBe(400) + state.items.set('command', item('command', 14, { kind: 'message', role: 'user', blocks: [] })) + expect(readJournalUsageLimitCut(state)).toBeNull() + state.items.set('command', item('command', 14, command)) + state.items.delete('command-turn') + state.items.delete('command') + for (const turnScope of [undefined, { kind: 'thread' as const }, { kind: 'unknown' as const }]) { + state.items.set( + 'later-user', + item('later-user', 20, { kind: 'message', role: 'user', blocks: [] }, { turnScope }) + ) + expect(readJournalUsageLimitCut(state)).toBeNull() + } + state.items.set( + 'later-user', + item('later-user', 20, { kind: 'message', role: 'user', blocks: [] }, scope) + ) + expect(readJournalUsageLimitCut(state)?.resetsAt).toBe(400) + state.items.delete('later-user') + const pending: AgentJournalSubmission = { + clientMessageId: 'pending', + fence: 1, + payloadFingerprint: 'fp', + dispatchState: 'pending', + providerItemId: null, + reason: null, + submittedAt: 1, + resolvedAt: null + } + state.submissions.set('pending', pending) + const order = journalInputOrder(state) + order.afterRow( + state, + journalDispatchRowBuilder(() => state, { + clientMessageId: 'pending', + state: 'pending', + fence: 1, + turnScope: { kind: 'thread' } + })(21, 1) + ) + expect(readJournalUsageLimitCut(state)).toBeNull() +}) + +it('keeps visible and raw boundary order distinct and reads child stop causes', () => { + const state = fixture() + state.items.set( + 'orca:running', item( - 'limit', - 2, + 'orca:running', + 10, + { kind: 'turn', turnId: 'running', state: 'running' }, + { sequenceIndex: 2 } + ) + ) + state.items.set( + 'completed', + item( + 'completed', + 10, { kind: 'status', - ...agentSessionFailureWords( - agentSessionFailureFact('usageLimitReached', { - usageLimit: { resetsAt } - }), - { surface: 'row' } - ) + text: 'Ended', + turnLifecycle: { turnId: 'completed', state: 'interrupted' } }, - { turnScope: { kind: 'turn', turnItemId: 'failed' } } + { sequenceIndex: 1 } + ) + ) + const boundary = (raw?: true) => { + return ( + (raw + ? newestStructuredAgentSessionTurnBySequence(memoryJournalMap(state.items).values()) + : newestStructuredAgentSessionTurn(renderJournalItems(state)) + )?.turnId ?? null ) - state.items.set('limit', limit(400)) - expect(state.queries.usageLimitContinueIdentity(state)).toEqual({ - turnItemId: 'failed', - resetsAt: 400 - }) - db.exec('SAVEPOINT writer_cut') - try { - state.items.set('limit', limit(500)) - expect(state.queries.usageLimitContinueIdentity(state)).toEqual({ - turnItemId: 'failed', - resetsAt: 500 - }) - state.items.set('limit', limit(600)) - expect(state.queries.usageLimitContinueIdentity(state)).toEqual({ - turnItemId: 'failed', - resetsAt: 600 - }) - } finally { - db.exec('ROLLBACK TO writer_cut; RELEASE writer_cut') } - const next = new JournalSqlReducer(sql.context.scope, 'epoch').state - expect(next.queries.usageLimitContinueIdentity(next)).toEqual({ - turnItemId: 'failed', - resetsAt: 400 + expect(boundary()).toBe('running') + expect(boundary(true)).toBe('completed') + state.inputs.rejections.set('running', { + clientMessageId: 'running', + fence: 1, + payloadFingerprint: 'fp', + dispatchState: 'rejected', + providerItemId: null, + reason: null, + submittedAt: 1, + resolvedAt: 1 }) + expect(boundary()).toBe('completed') + const scope = { + turnScope: { kind: 'turn' as const, turnItemId: 'completed' }, + agentId: 'child' + } + state.items.set( + 'child-stop', + item('child-stop', 11, { kind: 'status', text: 'Quit', orcaStop: { cause: 'quit' } }, scope) + ) + expect(readJournalOrcaStopCut(state)).toEqual({ turnItemId: 'completed', cause: 'quit' }) + state.items.set( + 'clear', + item('clear', 12, { + kind: 'status', + text: 'Cleared', + contextClear: { operationId: 'clear', afterFence: 1, clearedAt: 1 } + }) + ) + expect(boundary()).toBeNull() + expect(readJournalOrcaStopCut(state, 12)).toBeNull() }) diff --git a/src/main/native-chat/agent-session-journal/journal-status-indicators.test.ts b/src/main/native-chat/agent-session-journal/journal-status-indicators.test.ts index 1679729caa0..33e1d104894 100644 --- a/src/main/native-chat/agent-session-journal/journal-status-indicators.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-status-indicators.test.ts @@ -1,49 +1,18 @@ -import { afterEach, expect, it } from 'vitest' +import { expect, it } from 'vitest' import type { AgentJournalRenderItem, AgentJournalSubmission } from '../../../shared/agent-session/journal/agent-session-journal-types' -import Database from '../../sqlite/sync-database' + import { createJournalReducerState } from './projection/journal-reducer' -import { JournalSqlReducer } from './journal-sql-reducer' -import { JournalSqlScope } from './journal-sql-scope' -import { journalSqlProjectionSchema } from './journal-sql-projection-schema' + import { journalInputOrder } from './input-order/journal-input-order' import { journalDispatchRowBuilder } from './writing/journal-row-builders' -import { readJournalStatusIndicators as memoryReadJournalStatusIndicators } from './journal-status-indicators' +import { readJournalStatusIndicators } from './journal-status-indicators' import type { JournalLatestStop } from './context-epochs/journal-stop-turn-end' -import type { JournalReducerState } from './projection/journal-reducer' -import { JournalSqlStatusContext } from './journal-sql-status-context' -import { readJournalSqlStatusIndicators } from './journal-sql-status-indicators' - -const sqlOwners = new WeakMap() -function readJournalStatusIndicators( - state: JournalReducerState, - ...args: Parameters extends [unknown, ...infer A] - ? A - : never -) { - const sql = sqlOwners.get(state) - if (!sql) { - return memoryReadJournalStatusIndicators(state, ...args) - } - sql.flush() - return readJournalSqlStatusIndicators(new JournalSqlStatusContext(sql.context.scope), ...args) -} - -const databases: Database.Database[] = [] -afterEach(() => databases.splice(0).forEach((db) => db.close())) -function fixture(backend: 'memory' | 'sql') { - if (backend === 'memory') { - return createJournalReducerState('session', 'epoch') - } - const db = new Database(':memory:') - databases.push(db) - db.exec(journalSqlProjectionSchema()) - const sql = new JournalSqlReducer(new JournalSqlScope(db, 'session', 'epoch'), 'epoch') - sqlOwners.set(sql.state, sql) - return sql.state +function fixture() { + return createJournalReducerState('session', 'epoch') } function item( itemId: string, @@ -54,121 +23,115 @@ function item( } const user = { kind: 'message' as const, role: 'user' as const, blocks: [] } -it.each(['memory', 'sql'] as const)( - 'uses input ordinals, native membership and visible steer seeds for indicators in %s', - (backend) => { - const state = fixture(backend) - const order = journalInputOrder(state) - const send = (id: string, submittedAt: number, extra: Partial = {}) => { - state.submissions.set(id, { - clientMessageId: id, - fence: 0, - payloadFingerprint: id, - dispatchState: 'accepted', - providerItemId: null, - reason: null, - submittedAt, - resolvedAt: null, - acceptedSequence: 1, - ...extra - }) - order.afterRow( - state, - journalDispatchRowBuilder(() => state, { - clientMessageId: id, - state: 'accepted', - providerIdentity: null, - fence: 0 - })(1, submittedAt) - ) - } - send('provider-only', 1, { providerItemId: 'provider' }) - state.items.set('provider', item('provider', 1, user)) - send('native', 20) - state.items.set('orca:native', item('orca:native', 2, user)) - send('latest', 20) - state.items.set('orca:latest', item('orca:latest', 3, { kind: 'status', text: 'Command' })) - const read = (start = 0, floor = 0) => readJournalStatusIndicators(state, { start, floor }) - expect(read()).toEqual({ - acceptedSendKey: 'epoch:latest', - firstInputSubmissionKey: '["epoch","native"]', - submissionCount: 3, - stopping: false - }) - expect(read(2).firstInputSubmissionKey).toBeNull() - expect(read(0, 2).firstInputSubmissionKey).toBeNull() - state.items.delete('orca:native') - expect(read().firstInputSubmissionKey).toBeNull() - state.items.set('old-target', item('old-target', 4, { kind: 'status', text: 'Old target' })) - send('provider-only', 1, { - providerItemId: 'provider', - answeredInTurn: { turnItemId: 'old-target', via: 'steer' } - }) - expect(read().firstInputSubmissionKey).toBe('["epoch","provider-only"]') - state.items.delete('old-target') - expect(read().firstInputSubmissionKey).toBeNull() - send('latest', 0) - expect(read().acceptedSendKey).toBe('epoch:native') - expect(structuredClone(read())).toEqual(read()) - } -) - -it.each(['memory', 'sql'] as const)( - 'binds failed Stop to the first visible opening before the next send in %s', - (backend) => { - const state = fixture(backend) - const stop: JournalLatestStop = { - sequence: 10, - event: { reason: 'user-stop', at: 1 }, - settle: { settling: false, failedOn: { openedAfter: 10 } } - } - const turn = (id: string, at: number, running: boolean) => - item(id, at, { - kind: 'turn', - turnId: id, - state: running ? 'running' : 'completed' - }) - state.items.set('finished', turn('finished', 11, false)) - state.items.set('live', turn('live', 12, true)) - const read = () => readJournalStatusIndicators(state, { start: 0, floor: 0, stop }).stopping - expect(read()).toBe(false) - state.items.delete('finished') - expect(read()).toBe(true) - const send: AgentJournalSubmission = { - clientMessageId: 'later', +it('uses input ordinals, native membership and visible steer seeds for indicators', () => { + const state = fixture() + const order = journalInputOrder(state) + const send = (id: string, submittedAt: number, extra: Partial = {}) => { + state.submissions.set(id, { + clientMessageId: id, fence: 0, - payloadFingerprint: 'later', + payloadFingerprint: id, dispatchState: 'accepted', providerItemId: null, reason: null, - submittedAt: 1, + submittedAt, resolvedAt: null, - acceptedSequence: 12 - } - state.submissions.set('later', send) - journalInputOrder(state).afterRow( + acceptedSequence: 1, + ...extra + }) + order.afterRow( state, journalDispatchRowBuilder(() => state, { - clientMessageId: 'later', + clientMessageId: id, state: 'accepted', providerIdentity: null, fence: 0 - })(12, 1) + })(1, submittedAt) ) - expect(read()).toBe(false) - stop.settle = { settling: true } - expect(read()).toBe(true) - stop.settle = { settling: false, turnId: 'live' } - expect(read()).toBe(true) - expect( - readJournalStatusIndicators( - state, - { start: 0, floor: 0, stop }, - { - isCurrentItem: () => false, - owesSend: () => false - } - ).stopping - ).toBe(false) } -) + send('provider-only', 1, { providerItemId: 'provider' }) + state.items.set('provider', item('provider', 1, user)) + send('native', 20) + state.items.set('orca:native', item('orca:native', 2, user)) + send('latest', 20) + state.items.set('orca:latest', item('orca:latest', 3, { kind: 'status', text: 'Command' })) + const read = (start = 0, floor = 0) => readJournalStatusIndicators(state, { start, floor }) + expect(read()).toEqual({ + acceptedSendKey: 'epoch:latest', + firstInputSubmissionKey: '["epoch","native"]', + submissionCount: 3, + stopping: false + }) + expect(read(2).firstInputSubmissionKey).toBeNull() + expect(read(0, 2).firstInputSubmissionKey).toBeNull() + state.items.delete('orca:native') + expect(read().firstInputSubmissionKey).toBeNull() + state.items.set('old-target', item('old-target', 4, { kind: 'status', text: 'Old target' })) + send('provider-only', 1, { + providerItemId: 'provider', + answeredInTurn: { turnItemId: 'old-target', via: 'steer' } + }) + expect(read().firstInputSubmissionKey).toBe('["epoch","provider-only"]') + state.items.delete('old-target') + expect(read().firstInputSubmissionKey).toBeNull() + send('latest', 0) + expect(read().acceptedSendKey).toBe('epoch:native') + expect(structuredClone(read())).toEqual(read()) +}) + +it('binds failed Stop to the first visible opening before the next send', () => { + const state = fixture() + const stop: JournalLatestStop = { + sequence: 10, + event: { reason: 'user-stop', at: 1 }, + settle: { settling: false, failedOn: { openedAfter: 10 } } + } + const turn = (id: string, at: number, running: boolean) => + item(id, at, { + kind: 'turn', + turnId: id, + state: running ? 'running' : 'completed' + }) + state.items.set('finished', turn('finished', 11, false)) + state.items.set('live', turn('live', 12, true)) + const read = () => readJournalStatusIndicators(state, { start: 0, floor: 0, stop }).stopping + expect(read()).toBe(false) + state.items.delete('finished') + expect(read()).toBe(true) + const send: AgentJournalSubmission = { + clientMessageId: 'later', + fence: 0, + payloadFingerprint: 'later', + dispatchState: 'accepted', + providerItemId: null, + reason: null, + submittedAt: 1, + resolvedAt: null, + acceptedSequence: 12 + } + state.submissions.set('later', send) + journalInputOrder(state).afterRow( + state, + journalDispatchRowBuilder(() => state, { + clientMessageId: 'later', + state: 'accepted', + providerIdentity: null, + fence: 0 + })(12, 1) + ) + expect(read()).toBe(false) + stop.settle = { settling: true } + expect(read()).toBe(true) + stop.settle = { settling: false, turnId: 'live' } + expect(read()).toBe(true) + expect( + readJournalStatusIndicators( + state, + { start: 0, floor: 0, stop }, + { + isCurrentItem: () => false, + owesSend: () => false + } + ).stopping + ).toBe(false) +}) diff --git a/src/main/native-chat/agent-session-journal/journal-status-requests.test.ts b/src/main/native-chat/agent-session-journal/journal-status-requests.test.ts index f3fec5f4a8a..306ff8b1e7e 100644 --- a/src/main/native-chat/agent-session-journal/journal-status-requests.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-status-requests.test.ts @@ -1,48 +1,17 @@ -import { afterEach, expect, it } from 'vitest' +import { expect, it } from 'vitest' import type { AgentJournalRenderItem, AgentJournalSubmission } from '../../../shared/agent-session/journal/agent-session-journal-types' -import Database from '../../sqlite/sync-database' + import { createJournalReducerState } from './projection/journal-reducer' -import { JournalSqlReducer } from './journal-sql-reducer' -import { JournalSqlScope } from './journal-sql-scope' -import { journalSqlProjectionSchema } from './journal-sql-projection-schema' + import { journalInputOrder } from './input-order/journal-input-order' import { journalDispatchRowBuilder } from './writing/journal-row-builders' -import { readJournalRequestStatus as memoryReadJournalRequestStatus } from './journal-status-requests' +import { readJournalRequestStatus } from './journal-status-requests' -import type { JournalReducerState } from './projection/journal-reducer' -import { JournalSqlStatusContext } from './journal-sql-status-context' -import { readJournalSqlRequestStatus } from './journal-sql-status-requests' - -const sqlOwners = new WeakMap() -function readJournalRequestStatus( - state: JournalReducerState, - ...args: Parameters extends [unknown, ...infer A] - ? A - : never -) { - const sql = sqlOwners.get(state) - if (!sql) { - return memoryReadJournalRequestStatus(state, ...args) - } - sql.flush() - return readJournalSqlRequestStatus(new JournalSqlStatusContext(sql.context.scope), ...args) -} - -const databases: Database.Database[] = [] -afterEach(() => databases.splice(0).forEach((db) => db.close())) -function fixture(backend: 'memory' | 'sql') { - if (backend === 'memory') { - return createJournalReducerState('session', 'epoch') - } - const db = new Database(':memory:') - databases.push(db) - db.exec(journalSqlProjectionSchema()) - const sql = new JournalSqlReducer(new JournalSqlScope(db, 'session', 'epoch'), 'epoch') - sqlOwners.set(sql.state, sql) - return sql.state +function fixture() { + return createJournalReducerState('session', 'epoch') } function row( itemId: string, @@ -58,204 +27,198 @@ const message = (role: 'user' | 'assistant', text: string): AgentJournalRenderIt blocks: [{ type: 'text', text }] }) -it.each(['memory', 'sql'] as const)( - 'selects request order, command dependencies and assistant barriers in %s', - (backend) => { - const state = fixture(backend) - state.items.set( +it('selects request order, command dependencies and assistant barriers', () => { + const state = fixture() + state.items.set( + 'done', + row( 'done', - row( - 'done', - 10, - { kind: 'turn', turnId: 'done', state: 'completed', outcome: 'success', completedAt: 100 }, - { sequenceIndex: 2 } - ) + 10, + { kind: 'turn', turnId: 'done', state: 'completed', outcome: 'success', completedAt: 100 }, + { sequenceIndex: 2 } ) - state.items.set( + ) + state.items.set( + 'legacy', + row( 'legacy', - row( - 'legacy', - 10, - { - kind: 'status', - text: 'Legacy', - turnLifecycle: { turnId: 'legacy', state: 'interrupted' } - }, - { sequenceIndex: 1 } - ) - ) - const command = { - kind: 'message' as const, - role: 'assistant' as const, - command: { name: 'compact' as const }, - blocks: [] - } - state.items.set('opener', row('opener', 1, command)) - state.items.set( - 'compact', - row('compact', 11, { - kind: 'turn', - turnId: 'compact', - state: 'completed', - outcome: 'failure', - userItemId: 'opener', - completedAt: 110 - }) - ) - state.items.set('prose', row('prose', 12, message('assistant', 'Root reply'))) - state.items.set( - 'tool-only', - row('tool-only', 13, { kind: 'message', role: 'assistant', blocks: [] }) - ) - state.items.set( - 'command-result', - row('command-result', 14, message('assistant', 'Command result'), { - turnScope: { kind: 'turn', turnItemId: 'compact' } - }) - ) - state.items.set( - 'child', - row('child', 90, message('assistant', 'Child reply'), { agentId: 'child' }) - ) - expect(readJournalRequestStatus(state)).toEqual({ - hasRequest: true, - latestPrompt: '', - latestRequest: { - kind: 'turn', - id: 'done', - turnState: 'completed', - outcome: 'success', - settledAt: 100 + 10, + { + kind: 'status', + text: 'Legacy', + turnLifecycle: { turnId: 'legacy', state: 'interrupted' } }, - lastAssistantMessage: 'Root reply' - }) - state.items.set('opener', row('opener', 1, message('user', 'Ordinary opener'))) - expect(readJournalRequestStatus(state)).toMatchObject({ - latestPrompt: 'Ordinary opener', - latestRequest: { id: 'compact', outcome: 'failure' }, - lastAssistantMessage: 'Command result' - }) - state.items.set( - 'steer', - row('steer', 15, message('user', 'Steered prompt'), { - turnScope: { kind: 'turn', turnItemId: 'done' } - }) + { sequenceIndex: 1 } ) - expect(readJournalRequestStatus(state)).toEqual({ - hasRequest: true, - latestPrompt: 'Steered prompt', - latestRequest: { - kind: 'turn', - id: 'compact', - turnState: 'completed', - outcome: 'failure', - settledAt: 110 - } - }) - state.items.set( - 'barrier', - row('barrier', 16, message('user', 'Unknown request'), { turnScope: { kind: 'unknown' } }) - ) - expect(readJournalRequestStatus(state).latestRequest).toBeNull() - state.items.set( - 'barrier', - row('barrier', 16, message('user', 'Thread request'), { turnScope: { kind: 'thread' } }) - ) - expect(readJournalRequestStatus(state).latestRequest?.id).toBe('compact') + ) + const command = { + kind: 'message' as const, + role: 'assistant' as const, + command: { name: 'compact' as const }, + blocks: [] } -) - -it.each(['memory', 'sql'] as const)( - 'keeps pre-clear existence/prompt, strict acceptance cuts and nonmessage refusals in %s', - (backend) => { - const state = fixture(backend) - expect(readJournalRequestStatus(state)).toEqual({ - hasRequest: false, - latestPrompt: '', - latestRequest: null - }) - state.items.set('prompt', row('prompt', 1, message('user', 'Keep the prompt'))) - state.items.set( - 'self', - row('self', 2, { - kind: 'turn', - turnId: 'self', - state: 'completed', - userItemId: 'self', - completedAt: 20 - }) - ) - expect(readJournalRequestStatus(state).latestRequest).toMatchObject({ - id: 'self', - selfStarted: true - }) - const refusal: AgentJournalSubmission = { - clientMessageId: 'refused', - fence: 1, - payloadFingerprint: 'fp', - dispatchState: 'rejected', - providerItemId: null, - reason: null, - submittedAt: 3, - resolvedAt: 30, - acceptedSequence: 3, - rejection: { kind: 'writeFailed' } - } - state.submissions.set('refused', refusal) - state.items.set('orca:refused', row('orca:refused', 4, { kind: 'status', text: 'Refused' })) - const order = journalInputOrder(state) - const refresh = () => - order.afterRow( - state, - journalDispatchRowBuilder(() => state, { - clientMessageId: 'refused', - state: 'unknown', - fence: 1 - })(4, 4) - ) - refresh() - expect(readJournalRequestStatus(state).latestRequest).toEqual({ - kind: 'refused-send', - id: 'orca:refused', - turnState: null, + state.items.set('opener', row('opener', 1, command)) + state.items.set( + 'compact', + row('compact', 11, { + kind: 'turn', + turnId: 'compact', + state: 'completed', outcome: 'failure', - settledAt: 30 + userItemId: 'opener', + completedAt: 110 }) - const clear = { - kind: 'status' as const, - text: 'Cleared', - contextClear: { operationId: 'clear', afterFence: 1, clearedAt: 5 } + ) + state.items.set('prose', row('prose', 12, message('assistant', 'Root reply'))) + state.items.set( + 'tool-only', + row('tool-only', 13, { kind: 'message', role: 'assistant', blocks: [] }) + ) + state.items.set( + 'command-result', + row('command-result', 14, message('assistant', 'Command result'), { + turnScope: { kind: 'turn', turnItemId: 'compact' } + }) + ) + state.items.set( + 'child', + row('child', 90, message('assistant', 'Child reply'), { agentId: 'child' }) + ) + expect(readJournalRequestStatus(state)).toEqual({ + hasRequest: true, + latestPrompt: '', + latestRequest: { + kind: 'turn', + id: 'done', + turnState: 'completed', + outcome: 'success', + settledAt: 100 + }, + lastAssistantMessage: 'Root reply' + }) + state.items.set('opener', row('opener', 1, message('user', 'Ordinary opener'))) + expect(readJournalRequestStatus(state)).toMatchObject({ + latestPrompt: 'Ordinary opener', + latestRequest: { id: 'compact', outcome: 'failure' }, + lastAssistantMessage: 'Command result' + }) + state.items.set( + 'steer', + row('steer', 15, message('user', 'Steered prompt'), { + turnScope: { kind: 'turn', turnItemId: 'done' } + }) + ) + expect(readJournalRequestStatus(state)).toEqual({ + hasRequest: true, + latestPrompt: 'Steered prompt', + latestRequest: { + kind: 'turn', + id: 'compact', + turnState: 'completed', + outcome: 'failure', + settledAt: 110 } - state.items.set('clear', row('clear', 5, clear)) - state.items.set( - 'orca:refused', - row('orca:refused', 6, { kind: 'status', text: 'Refused after clear' }) - ) - state.submissions.set('refused', { ...refusal, acceptedSequence: 5 }) - refresh() - expect(readJournalRequestStatus(state)).toEqual({ - hasRequest: true, - latestPrompt: 'Keep the prompt', - latestRequest: null - }) - state.submissions.set('refused', { ...refusal, acceptedSequence: 6 }) - refresh() - expect(readJournalRequestStatus(state).latestRequest?.id).toBe('orca:refused') - state.items.set('orca:hidden-clear', row('orca:hidden-clear', 7, clear)) - state.inputs.rejections.set('hidden-clear', { ...refusal, clientMessageId: 'hidden-clear' }) - expect(readJournalRequestStatus(state).latestRequest?.id).toBe('orca:refused') - state.items.delete('prompt') - state.items.delete('self') - state.items.delete('orca:refused') - state.submissions.delete('refused') - state.items.set( - 'child-user', - row('child-user', 8, message('user', 'Provider child'), { agentId: 'child' }) - ) - expect(readJournalRequestStatus(state)).toEqual({ - hasRequest: true, - latestPrompt: '', - latestRequest: null + }) + state.items.set( + 'barrier', + row('barrier', 16, message('user', 'Unknown request'), { turnScope: { kind: 'unknown' } }) + ) + expect(readJournalRequestStatus(state).latestRequest).toBeNull() + state.items.set( + 'barrier', + row('barrier', 16, message('user', 'Thread request'), { turnScope: { kind: 'thread' } }) + ) + expect(readJournalRequestStatus(state).latestRequest?.id).toBe('compact') +}) + +it('keeps pre-clear existence/prompt, strict acceptance cuts and nonmessage refusals', () => { + const state = fixture() + expect(readJournalRequestStatus(state)).toEqual({ + hasRequest: false, + latestPrompt: '', + latestRequest: null + }) + state.items.set('prompt', row('prompt', 1, message('user', 'Keep the prompt'))) + state.items.set( + 'self', + row('self', 2, { + kind: 'turn', + turnId: 'self', + state: 'completed', + userItemId: 'self', + completedAt: 20 }) + ) + expect(readJournalRequestStatus(state).latestRequest).toMatchObject({ + id: 'self', + selfStarted: true + }) + const refusal: AgentJournalSubmission = { + clientMessageId: 'refused', + fence: 1, + payloadFingerprint: 'fp', + dispatchState: 'rejected', + providerItemId: null, + reason: null, + submittedAt: 3, + resolvedAt: 30, + acceptedSequence: 3, + rejection: { kind: 'writeFailed' } } -) + state.submissions.set('refused', refusal) + state.items.set('orca:refused', row('orca:refused', 4, { kind: 'status', text: 'Refused' })) + const order = journalInputOrder(state) + const refresh = () => + order.afterRow( + state, + journalDispatchRowBuilder(() => state, { + clientMessageId: 'refused', + state: 'unknown', + fence: 1 + })(4, 4) + ) + refresh() + expect(readJournalRequestStatus(state).latestRequest).toEqual({ + kind: 'refused-send', + id: 'orca:refused', + turnState: null, + outcome: 'failure', + settledAt: 30 + }) + const clear = { + kind: 'status' as const, + text: 'Cleared', + contextClear: { operationId: 'clear', afterFence: 1, clearedAt: 5 } + } + state.items.set('clear', row('clear', 5, clear)) + state.items.set( + 'orca:refused', + row('orca:refused', 6, { kind: 'status', text: 'Refused after clear' }) + ) + state.submissions.set('refused', { ...refusal, acceptedSequence: 5 }) + refresh() + expect(readJournalRequestStatus(state)).toEqual({ + hasRequest: true, + latestPrompt: 'Keep the prompt', + latestRequest: null + }) + state.submissions.set('refused', { ...refusal, acceptedSequence: 6 }) + refresh() + expect(readJournalRequestStatus(state).latestRequest?.id).toBe('orca:refused') + state.items.set('orca:hidden-clear', row('orca:hidden-clear', 7, clear)) + state.inputs.rejections.set('hidden-clear', { ...refusal, clientMessageId: 'hidden-clear' }) + expect(readJournalRequestStatus(state).latestRequest?.id).toBe('orca:refused') + state.items.delete('prompt') + state.items.delete('self') + state.items.delete('orca:refused') + state.submissions.delete('refused') + state.items.set( + 'child-user', + row('child-user', 8, message('user', 'Provider child'), { agentId: 'child' }) + ) + expect(readJournalRequestStatus(state)).toEqual({ + hasRequest: true, + latestPrompt: '', + latestRequest: null + }) +}) diff --git a/src/main/native-chat/agent-session-journal/journal-status-state.test.ts b/src/main/native-chat/agent-session-journal/journal-status-state.test.ts index e256292a28d..2433926285e 100644 --- a/src/main/native-chat/agent-session-journal/journal-status-state.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-status-state.test.ts @@ -1,41 +1,12 @@ -import { afterEach, expect, it } from 'vitest' +import { expect, it } from 'vitest' import type { AgentJournalRenderItem } from '../../../shared/agent-session/journal/agent-session-journal-types' -import Database from '../../sqlite/sync-database' + import { createJournalReducerState } from './projection/journal-reducer' -import { JournalSqlReducer } from './journal-sql-reducer' -import { JournalSqlScope } from './journal-sql-scope' -import { journalSqlProjectionSchema } from './journal-sql-projection-schema' -import { readJournalStatusState as memoryReadJournalStatusState } from './journal-status-state' -import type { JournalReducerState } from './projection/journal-reducer' -import { JournalSqlStatusContext } from './journal-sql-status-context' -import { readJournalSqlStatusState } from './journal-sql-status-state' +import { readJournalStatusState } from './journal-status-state' -const sqlOwners = new WeakMap() -function readJournalStatusState( - state: JournalReducerState, - ...args: Parameters extends [unknown, ...infer A] ? A : never -) { - const sql = sqlOwners.get(state) - if (!sql) { - return memoryReadJournalStatusState(state, ...args) - } - sql.flush() - return readJournalSqlStatusState(new JournalSqlStatusContext(sql.context.scope), ...args) -} - -const databases: Database.Database[] = [] -afterEach(() => databases.splice(0).forEach((db) => db.close())) -function fixture(backend: 'memory' | 'sql') { - if (backend === 'memory') { - return createJournalReducerState('session', 'epoch') - } - const db = new Database(':memory:') - databases.push(db) - db.exec(journalSqlProjectionSchema()) - const sql = new JournalSqlReducer(new JournalSqlScope(db, 'session', 'epoch'), 'epoch') - sqlOwners.set(sql.state, sql) - return sql.state +function fixture() { + return createJournalReducerState('session', 'epoch') } function row( itemId: string, @@ -46,151 +17,145 @@ function row( return { itemId, sequence, revision: 1, observedAt: sequence * 10, body, ...extra } } -it.each(['memory', 'sql'] as const)( - 'uses the current boundary and positional tools, then dates the settled verdict in %s', - (backend) => { - const state = fixture(backend) - state.items.set( - 'ask', - row('ask', 1, { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'Go' }] }) - ) - const running = { - kind: 'turn' as const, - turnId: 'turn', - state: 'running' as const, - requestedAt: 100 - } - state.items.set('turn', row('turn', 2, running)) - state.items.set( +it('uses the current boundary and positional tools, then dates the settled verdict', () => { + const state = fixture() + state.items.set( + 'ask', + row('ask', 1, { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'Go' }] }) + ) + const running = { + kind: 'turn' as const, + turnId: 'turn', + state: 'running' as const, + requestedAt: 100 + } + state.items.set('turn', row('turn', 2, running)) + state.items.set( + 'call', + row( 'call', - row( - 'call', - 3, - { kind: 'tool-call', name: 'Read', state: 'running', input: { file_path: 'src/a.ts' } }, - { turnScope: { kind: 'turn', turnItemId: 'other-turn' } } - ) + 3, + { kind: 'tool-call', name: 'Read', state: 'running', input: { file_path: 'src/a.ts' } }, + { turnScope: { kind: 'turn', turnItemId: 'other-turn' } } ) - state.items.set( - 'diff', - row('diff', 4, { - kind: 'diff', - path: 'src/b.ts', - patch: { head: '@@\n+x', byteLength: 5, digest: 'd', truncated: false } - }) - ) - expect(readJournalStatusState(state)).toEqual({ - summary: { - status: 'working', - latestPrompt: 'Go', - toolName: 'Read', - toolInput: 'src/a.ts', - statusStartedAt: 100 - }, - latestRequest: { - kind: 'turn', - id: 'turn', - turnState: 'running', - outcome: null, - settledAt: undefined - }, - owesWork: true, - pendingPromptIds: [] + ) + state.items.set( + 'diff', + row('diff', 4, { + kind: 'diff', + path: 'src/b.ts', + patch: { head: '@@\n+x', byteLength: 5, digest: 'd', truncated: false } }) - state.items.set( - 'call', - row('call', 3, { - kind: 'tool-call', - name: 'Read', - state: 'completed', - input: { file_path: 'src/a.ts' } - }) - ) - expect(readJournalStatusState(state).summary).toMatchObject({ - toolName: 'Diff', - toolInput: 'src/b.ts' - }) - state.items.set( - 'turn', - row('turn', 5, { ...running, state: 'completed', outcome: 'success', completedAt: 500 }) - ) - expect(readJournalStatusState(state)).toEqual({ - summary: { status: 'idle', latestPrompt: 'Go', turnOutcome: 'success', statusStartedAt: 500 }, - latestRequest: { - kind: 'turn', - id: 'turn', - turnState: 'completed', - outcome: 'success', - settledAt: 500 - }, - owesWork: false, - pendingPromptIds: [] - }) - state.items.set('turn', row('turn', 5, { ...running, state: 'interrupted', completedAt: 500 })) - state.items.set( - 'quit', - row( - 'quit', - 6, - { kind: 'status', text: 'Quit', orcaStop: { cause: 'quit' } }, - { turnScope: { kind: 'turn', turnItemId: 'turn' }, agentId: 'child' } - ) - ) - expect(readJournalStatusState(state).summary).toEqual({ - status: 'idle', + ) + expect(readJournalStatusState(state)).toEqual({ + summary: { + status: 'working', latestPrompt: 'Go', - turnOutcome: 'cancellation', - statusStartedAt: 500 + toolName: 'Read', + toolInput: 'src/a.ts', + statusStartedAt: 100 + }, + latestRequest: { + kind: 'turn', + id: 'turn', + turnState: 'running', + outcome: null, + settledAt: undefined + }, + owesWork: true, + pendingPromptIds: [] + }) + state.items.set( + 'call', + row('call', 3, { + kind: 'tool-call', + name: 'Read', + state: 'completed', + input: { file_path: 'src/a.ts' } }) - } -) - -it.each(['memory', 'sql'] as const)( - 'keeps pending timestamps independent of scope and rereads row-free live authority in %s', - (backend) => { - const state = fixture(backend) - state.items.set( - 'turn', - row('turn', 1, { kind: 'turn', turnId: 'turn', state: 'running', requestedAt: 100 }) + ) + expect(readJournalStatusState(state).summary).toMatchObject({ + toolName: 'Diff', + toolInput: 'src/b.ts' + }) + state.items.set( + 'turn', + row('turn', 5, { ...running, state: 'completed', outcome: 'success', completedAt: 500 }) + ) + expect(readJournalStatusState(state)).toEqual({ + summary: { status: 'idle', latestPrompt: 'Go', turnOutcome: 'success', statusStartedAt: 500 }, + latestRequest: { + kind: 'turn', + id: 'turn', + turnState: 'completed', + outcome: 'success', + settledAt: 500 + }, + owesWork: false, + pendingPromptIds: [] + }) + state.items.set('turn', row('turn', 5, { ...running, state: 'interrupted', completedAt: 500 })) + state.items.set( + 'quit', + row( + 'quit', + 6, + { kind: 'status', text: 'Quit', orcaStop: { cause: 'quit' } }, + { turnScope: { kind: 'turn', turnItemId: 'turn' }, agentId: 'child' } ) - const question = { - kind: 'question' as const, - question: 'Continue?', - options: [], - resolution: { - state: 'pending' as const, - selectedOptionId: null, - resolvedBy: null, - resolvedAt: null - } + ) + expect(readJournalStatusState(state).summary).toEqual({ + status: 'idle', + latestPrompt: 'Go', + turnOutcome: 'cancellation', + statusStartedAt: 500 + }) +}) + +it('keeps pending timestamps independent of scope and rereads row-free live authority', () => { + const state = fixture() + state.items.set( + 'turn', + row('turn', 1, { kind: 'turn', turnId: 'turn', state: 'running', requestedAt: 100 }) + ) + const question = { + kind: 'question' as const, + question: 'Continue?', + options: [], + resolution: { + state: 'pending' as const, + selectedOptionId: null, + resolvedBy: null, + resolvedAt: null } - state.items.set('root-first', row('root-first', 3, question, { observedAt: 30 })) - state.items.set('child', row('child', 4, question, { observedAt: 10, agentId: 'child' })) - state.items.set('root-second', row('root-second', 5, question, { observedAt: 20 })) - const current = new Set(['child']) - const scope = { isCurrentItem: (id: string) => current.has(id), owesSend: () => false } - expect(readJournalStatusState(state, 1, scope)).toEqual({ - summary: { status: 'attention', latestPrompt: '', statusStartedAt: 20 }, - latestRequest: { - kind: 'turn', - id: 'turn', - turnState: 'running', - outcome: null, - settledAt: undefined - }, - owesWork: false, - pendingPromptIds: ['child'] - }) - current.clear() - expect(readJournalStatusState(state, 1, scope)).toMatchObject({ - summary: { status: 'idle' }, - owesWork: false, - pendingPromptIds: [] - }) - current.add('turn') - expect(readJournalStatusState(state, 1, scope)).toMatchObject({ - summary: { status: 'working', statusStartedAt: 100 }, - owesWork: true, - pendingPromptIds: [] - }) } -) + state.items.set('root-first', row('root-first', 3, question, { observedAt: 30 })) + state.items.set('child', row('child', 4, question, { observedAt: 10, agentId: 'child' })) + state.items.set('root-second', row('root-second', 5, question, { observedAt: 20 })) + const current = new Set(['child']) + const scope = { isCurrentItem: (id: string) => current.has(id), owesSend: () => false } + expect(readJournalStatusState(state, 1, scope)).toEqual({ + summary: { status: 'attention', latestPrompt: '', statusStartedAt: 20 }, + latestRequest: { + kind: 'turn', + id: 'turn', + turnState: 'running', + outcome: null, + settledAt: undefined + }, + owesWork: false, + pendingPromptIds: ['child'] + }) + current.clear() + expect(readJournalStatusState(state, 1, scope)).toMatchObject({ + summary: { status: 'idle' }, + owesWork: false, + pendingPromptIds: [] + }) + current.add('turn') + expect(readJournalStatusState(state, 1, scope)).toMatchObject({ + summary: { status: 'working', statusStartedAt: 100 }, + owesWork: true, + pendingPromptIds: [] + }) +}) diff --git a/src/main/native-chat/agent-session-journal/journal-unheld-turns-settlement.test.ts b/src/main/native-chat/agent-session-journal/journal-unheld-turns-settlement.test.ts index ccb6958cf0c..f14b0e74939 100644 --- a/src/main/native-chat/agent-session-journal/journal-unheld-turns-settlement.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-unheld-turns-settlement.test.ts @@ -47,7 +47,7 @@ async function memory(root: string): Promise { }) await journal.open() return Object.assign(journal, { - turnState: async (turnId: string) => journal.item(`orca:${turnId}`)?.body + turnState: async (turnId: string) => (await journal.reads.item(`orca:${turnId}`))?.body }) } diff --git a/src/main/native-chat/agent-session-journal/projection/journal-dispatch-reason-bound.spec.ts b/src/main/native-chat/agent-session-journal/projection/journal-dispatch-reason-bound.spec.ts index 064950db348..4dfc5928f58 100644 --- a/src/main/native-chat/agent-session-journal/projection/journal-dispatch-reason-bound.spec.ts +++ b/src/main/native-chat/agent-session-journal/projection/journal-dispatch-reason-bound.spec.ts @@ -1,3 +1,4 @@ +import { readTestJournalSubmissions } from '../journal-observation.test-support' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -54,7 +55,7 @@ export function registerJournalDispatchReasonBoundTests(): void { reason, fence: 1 }) - return journal.snapshot().submissions[0]?.reason ?? null + return (await readTestJournalSubmissions(journal))[0]?.reason ?? null } beforeEach(async () => { diff --git a/src/main/native-chat/agent-session-journal/projection/journal-producer-inheritance.spec.ts b/src/main/native-chat/agent-session-journal/projection/journal-producer-inheritance.spec.ts index dd298ff1007..7329b5029dc 100644 --- a/src/main/native-chat/agent-session-journal/projection/journal-producer-inheritance.spec.ts +++ b/src/main/native-chat/agent-session-journal/projection/journal-producer-inheritance.spec.ts @@ -1,3 +1,4 @@ +import { readTestJournalItems } from '../journal-observation.test-support' import { AGENT_JOURNAL_THREAD_SCOPE, type AgentJournalMessageItem, @@ -112,7 +113,7 @@ export function registerJournalProducerInheritanceTests(): void { } ] }) - const live = journal.snapshot().items + const live = await readTestJournalItems(journal) expect(live.map((item) => [item.revision, producerOf(item)])).toEqual([ [3, linkage], [2, NO_PRODUCER] @@ -120,7 +121,7 @@ export function registerJournalProducerInheritanceTests(): void { await journal.close() const reopened = await open() - expect(reopened.snapshot().items).toEqual(live) + expect(await readTestJournalItems(reopened)).toEqual(live) }) }) } diff --git a/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-contract-test-fixture.ts b/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-contract-test-fixture.ts index 4902e291515..6f1f5f0c8a1 100644 --- a/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-contract-test-fixture.ts +++ b/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-contract-test-fixture.ts @@ -124,7 +124,7 @@ export function createFixture() { source: unknown ): Promise { await journal.close() - const seq = journal.submission(id)?.submittedSequence + const seq = (await journal.reads.submission(id))?.submittedSequence const { db } = openTestJournalHostDatabase(root) const stored = liveTestJournalRows(db, IDENTITY.sessionId).find((row) => row.seq === seq) if (!stored) { diff --git a/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-queue.spec.ts b/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-queue.spec.ts index 50a70ae8233..d2285a5a21e 100644 --- a/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-queue.spec.ts +++ b/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-queue.spec.ts @@ -1,3 +1,4 @@ +import { openTestAgentSessionDatabase } from '../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { AgentSessionJournal } from '../journal-store' import { openAgentSessionJournal } from '../journal-store-factory' import { openTestJournalHostDatabase } from '../store-lifecycle/journal-host-database-test-support' @@ -94,9 +95,9 @@ export function registerJournalUnsentSendHoldQueueTests( await fixture.hold(journal, { cause: 'chatClosed' }) expect(fixture.cardOrder(journal)).toEqual(['person']) - expect(journal.queuedMessages.get('person')).toMatchObject({ holdReason: null }) + expect(await journal.queuedMessages.get('person')).toMatchObject({ holdReason: null }) for (const id of ['person', 'mail']) { - expect(journal.submission(id)).toMatchObject({ + expect(await journal.reads.submission(id)).toMatchObject({ dispatchState: 'rejected', ...fixture.CHAT_CLOSED }) @@ -113,7 +114,7 @@ export function registerJournalUnsentSendHoldQueueTests( }) expect(fixture.cardOrder(journal)).toEqual(['before']) - expect(journal.submission('after')?.dispatchState).toBe('pending') + expect((await journal.reads.submission('after'))?.dispatchState).toBe('pending') }) }) @@ -188,7 +189,7 @@ export function registerJournalUnsentSendHoldQueueTests( }) it('retains queued and legacy chat tasks at restart while withdrawing a derived preamble', async () => { - const database = openTestJournalHostDatabase(fixture.root) + const database = await openTestAgentSessionDatabase(fixture.root) const open = async () => fixture.journals.track( await openAgentSessionJournal({ @@ -227,6 +228,6 @@ export function registerJournalUnsentSendHoldQueueTests( await earlier.close() const reopened = await open() expect(fixture.cardOrder(reopened)).toEqual(['legacy-task', 'queued-task']) - expect(reopened.queuedMessages.get('derived-preamble')?.state).toBe('withdrawn') + expect((await reopened.queuedMessages.get('derived-preamble'))?.state).toBe('withdrawn') }) } diff --git a/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-recovery.spec.ts b/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-recovery.spec.ts index e56f114dbda..f0e0464c5ea 100644 --- a/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-recovery.spec.ts +++ b/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-recovery.spec.ts @@ -10,8 +10,8 @@ export function registerJournalUnsentSendHoldRecoveryTests( const journal = await fixture.open() await fixture.accept(journal, 'mine', { origin: 'client', source: USER_MESSAGE_SOURCE }) await fixture.hold(journal) - expect(journal.submission('mine')?.dispatchState).toBe('pending') - expect(journal.queuedMessages.list()).toEqual([]) + expect((await journal.reads.submission('mine'))?.dispatchState).toBe('pending') + expect(await journal.queuedMessages.list()).toEqual([]) }) }) } diff --git a/src/main/native-chat/agent-session-journal/queued-messages/queued-message-stop-pause.spec.ts b/src/main/native-chat/agent-session-journal/queued-messages/queued-message-stop-pause.spec.ts index bb8bace589e..96980aa0f0a 100644 --- a/src/main/native-chat/agent-session-journal/queued-messages/queued-message-stop-pause.spec.ts +++ b/src/main/native-chat/agent-session-journal/queued-messages/queued-message-stop-pause.spec.ts @@ -29,9 +29,9 @@ export function registerQueuedMessageStopPauseTests( expect(fixture.reason(journal)).toBe('failed') await journal.replaceEpochItems('handle_forked', 0, kept) expect(fixture.reason(journal)).toBe('failed') - expect(journal.queuedMessages.nextInputCard()).toBeNull() + expect(await journal.queuedMessages.nextInputCard()).toBeNull() await journal.appendQueueResume(0) - expect(journal.queuedMessages.nextInputCard()?.messageId).toBe('carried') + expect((await journal.queuedMessages.nextInputCard())?.messageId).toBe('carried') }) it("the queue's own consume is refused in its transaction while the pause holds the card; Send-now is not", async () => { @@ -54,10 +54,10 @@ export function registerQueuedMessageStopPauseTests( } ) await expect(consume('drain-1', true)).rejects.toBeInstanceOf(QueuedMessageNotConsumableError) - expect(journal.submission('drain-1')).toBeUndefined() - expect(journal.queuedMessages.get('draft-1')?.state).toBe('waiting') + expect(await journal.reads.submission('drain-1')).toBeUndefined() + expect((await journal.queuedMessages.get('draft-1'))?.state).toBe('waiting') await consume('send-now-1', false) - expect(journal.queuedMessages.get('draft-1')?.state).toBe('dispatched') + expect((await journal.queuedMessages.get('draft-1'))?.state).toBe('dispatched') }) it("a rewind's epoch replacement restates a Stop still pausing, and not one a turn ended", async () => { @@ -95,13 +95,13 @@ export function registerQueuedMessageStopPauseTests( providerIdentity: null, fence: 0 }) - expect(journal.queuedMessages.nextInputCard()).toBeNull() + expect(await journal.queuedMessages.nextInputCard()).toBeNull() expect(fixture.reason(journal)).toBe('stopped') await fixture.turn(journal, 'person') await journal.replaceEpochItems('handle_forked', 0, []) await journal.close() journal = await fixture.open() - expect(journal.queuedMessages.nextInputCard()?.messageId).toBe('P') + expect((await journal.queuedMessages.nextInputCard())?.messageId).toBe('P') expect(fixture.reason(journal)).toBeNull() }) @@ -143,7 +143,8 @@ export function registerQueuedMessageStopPauseTests( await journal.close() journal = await fixture.open() expect( - journal.queuedMessages.pauses().find((pause) => pause.reason === 'cleared')?.messageIds + (await journal.queuedMessages.pauses()).find((pause) => pause.reason === 'cleared') + ?.messageIds ).toEqual(['P']) await fixture.turn(journal, 'person') expect(fixture.held(journal)).toEqual([ @@ -167,8 +168,8 @@ export function registerQueuedMessageStopPauseTests( ).run(source.generation, Number(source.through_seq) + 1, JSON.stringify('retired-person')) db.prepare('UPDATE journal_outcome_epochs SET through_seq=through_seq+1').run() const restored = await fixture.open() - expect(restored.submission('retired-person')).toBeUndefined() - expect(restored.queuedMessages.nextInputCard()?.messageId).toBe('W') + expect(await restored.reads.submission('retired-person')).toBeUndefined() + expect((await restored.queuedMessages.nextInputCard())?.messageId).toBe('W') expect(fixture.reason(restored)).toBeNull() }) } diff --git a/src/main/native-chat/agent-session-journal/replay/journal-outcome-wire.test.ts b/src/main/native-chat/agent-session-journal/replay/journal-outcome-wire.test.ts index 9951acec80c..471dfc24f7a 100644 --- a/src/main/native-chat/agent-session-journal/replay/journal-outcome-wire.test.ts +++ b/src/main/native-chat/agent-session-journal/replay/journal-outcome-wire.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalSnapshot } from '../journal-observation.test-support' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -74,7 +75,7 @@ it('keeps sparse sequence groups whole across catch-up, lookahead, history limit } const expected = projectJournalBatch({ rows: old.rows, - snapshot: journal.snapshot(), + snapshot: await readTestJournalSnapshot(journal), afterSequence: 1 }) // Retention keeps a key's first and latest records, leaving whole sequences without any. @@ -86,20 +87,28 @@ it('keeps sparse sequence groups whole across catch-up, lookahead, history limit const sparse = readJournalOutcomeGroups(database.db, epoch, source, 1) expect(sparse.length).toBeLessThan(old.rows.length) expect( - projectJournalBatch({ rows: sparse, snapshot: journal.snapshot(), afterSequence: 1 }) + projectJournalBatch({ + rows: sparse, + snapshot: await readTestJournalSnapshot(journal), + afterSequence: 1 + }) ).toEqual(expected) - const snapshot = journal.snapshot() + const snapshot = await readTestJournalSnapshot(journal) const cursor = journal.cursor() await journals.closeAll() journal = await journals.open({ identity, stateDirectory: root }) - expect(journal.snapshot()).toEqual(snapshot) + expect(await readTestJournalSnapshot(journal)).toEqual(snapshot) expect(journal.cursor()).toEqual(cursor) const reopened = journal.readSince(before) if (!reopened.ok) { throw new Error('sparse read after reopen failed') } expect( - projectJournalBatch({ rows: reopened.rows, snapshot: journal.snapshot(), afterSequence: 1 }) + projectJournalBatch({ + rows: reopened.rows, + snapshot: await readTestJournalSnapshot(journal), + afterSequence: 1 + }) ).toEqual(expected) await journal.appendLifecycleBatch({ settlementId: 'whole-group', @@ -155,11 +164,11 @@ it('keeps sparse sequence groups whole across catch-up, lookahead, history limit { reader: { budgetBytes: 1, form: (value) => ({ item: value, bytes: 100 }) } } ) expect(bounded).toMatchObject({ ok: false, reset: 'cursor_compacted' }) - const continued = journal.snapshot() + const continued = await readTestJournalSnapshot(journal) const continuedCursor = journal.cursor() await journals.closeAll() journal = await journals.open({ identity, stateDirectory: root }) - expect(journal.snapshot()).toEqual(continued) + expect(await readTestJournalSnapshot(journal)).toEqual(continued) expect(journal.cursor()).toEqual(continuedCursor) }) diff --git a/src/main/native-chat/agent-session-journal/replay/journal-pause-mark-restore.test.ts b/src/main/native-chat/agent-session-journal/replay/journal-pause-mark-restore.test.ts index 89933b37c1c..ac9611f7979 100644 --- a/src/main/native-chat/agent-session-journal/replay/journal-pause-mark-restore.test.ts +++ b/src/main/native-chat/agent-session-journal/replay/journal-pause-mark-restore.test.ts @@ -62,20 +62,22 @@ it('restores separate pause and cancellation owners from complete format-3 scala sequence: journal.cursor().sequence, event: { reason: 'user-close', turnId: 'mail', at: 1_000 } } - expect(journalOutcomeSource(db(), identity.sessionId, journal.epoch)?.prefix).toBe(0) + expect( + journalOutcomeSource(db(), identity.sessionId, journal.physicalCursor().epoch)?.prefix + ).toBe(0) expect(marks()).toMatchObject({ manualStop: stop, latestStop: close }) await journal.close() journal = await current.open(options()) expect(journal.stopMarks.latest()).toMatchObject(close) - expect(journal.queuedMessages.userStopInForce()).toEqual(stop) - expect(journal.queuedMessages.nextInputCard()).toBeNull() + expect(await journal.queuedMessages.userStopInForce()).toEqual(stop) + expect(await journal.queuedMessages.nextInputCard()).toBeNull() await journal.replaceEpochItems('handle_forked', 0, []) await journal.close() journal = await current.open(options()) expect(marks()).toMatchObject({ manualStop: stop, latestStop: close }) - expect(journal.queuedMessages.nextInputCard()).toBeNull() + expect(await journal.queuedMessages.nextInputCard()).toBeNull() await journal.appendQueueResume(0) - expect(journal.queuedMessages.nextInputCard()?.messageId).toBe('W') + expect((await journal.queuedMessages.nextInputCard())?.messageId).toBe('W') }) it.each(['raw-prefix', 'old-format-3-layout'] as const)( @@ -115,8 +117,8 @@ it.each(['raw-prefix', 'old-format-3-layout'] as const)( manualStop: stop, latestStop: { event: { reason: 'user-close', turnId: 'mail' } } }) - expect(journal.queuedMessages.userStopInForce()).toEqual(stop) - expect(journal.queuedMessages.nextInputCard()).toBeNull() + expect(await journal.queuedMessages.userStopInForce()).toEqual(stop) + expect(await journal.queuedMessages.nextInputCard()).toBeNull() } ) @@ -136,7 +138,7 @@ it('does not revive a superseded historical Stop, and distinguishes explicit nul ) .run(JSON.stringify('queuePauseMarks')) journal = await current.open(options()) - expect(journal.queuedMessages.userStopInForce()).toBeNull() + expect(await journal.queuedMessages.userStopInForce()).toBeNull() await journal.appendStopEvent({ reason: 'host-stop' }, 0) expect(marks().manualStop).toBeNull() }) diff --git a/src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support.ts b/src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support.ts index 2451228f898..b4b369f6047 100644 --- a/src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support.ts +++ b/src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support.ts @@ -16,6 +16,7 @@ import { applyJournalRow } from '../projection/journal-reducer' import { assertTestJournalHistoricalInputs } from '../replay/journal-historical-inputs.test-support' import { openTestAgentSessionDatabase, + closeTestAgentSessionDatabase, closeTestAgentSessionRecordStores } from '../../../runtime/agent-session/records/agent-session-record-store-test-harness' @@ -49,11 +50,13 @@ export function openTestJournalHostDatabase(stateDirectory: string): JournalHost export async function closeTestJournalHostDatabase(stateDirectory: string): Promise { const directory = resolve(stateDirectory) + await closeTestAgentSessionDatabase(directory) await opened.get(directory)?.close() opened.delete(directory) } export async function closeTestJournalHostDatabases(): Promise { + await closeTestAgentSessionRecordStores() for (const database of opened.values()) { await database.close() } @@ -93,7 +96,6 @@ export function createTrackedJournalOpener( }, closeAll: async () => { await Promise.allSettled(journals.splice(0).map((journal) => journal.close())) - await closeTestAgentSessionRecordStores() await closeTestJournalHostDatabases() } } diff --git a/src/main/native-chat/agent-session-journal/submissions/journal-queued-rejection-batch.spec.ts b/src/main/native-chat/agent-session-journal/submissions/journal-queued-rejection-batch.spec.ts index 4671b67dabe..78145df4625 100644 --- a/src/main/native-chat/agent-session-journal/submissions/journal-queued-rejection-batch.spec.ts +++ b/src/main/native-chat/agent-session-journal/submissions/journal-queued-rejection-batch.spec.ts @@ -1,3 +1,7 @@ +import { + readTestJournalItems, + readTestJournalSubmissions +} from '../journal-observation.test-support' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -79,11 +83,10 @@ export function registerJournalQueuedRejectionBatchTests(): void { await journal.appendLifecycleBatch(startFailureBatch()) expect(journal.cursor().sequence).toBe(before + 3) - expect(journal.submissions().map((entry) => entry.dispatchState)).toEqual([ - 'rejected', - 'rejected' - ]) - const order = journal.snapshot().items.map((item) => item.itemId) + expect((await readTestJournalSubmissions(journal)).map((entry) => entry.dispatchState)).toEqual( + ['rejected', 'rejected'] + ) + const order = (await readTestJournalItems(journal)).map((item) => item.itemId) expect(order).toEqual([ agentJournalSubmissionKey('first'), agentJournalSubmissionKey('second'), @@ -100,7 +103,9 @@ export function registerJournalQueuedRejectionBatchTests(): void { ) expect(journal.cursor().sequence).toBe(before) - expect(journal.submissions().map((entry) => entry.dispatchState)).toEqual(['pending']) + expect((await readTestJournalSubmissions(journal)).map((entry) => entry.dispatchState)).toEqual( + ['pending'] + ) }) it('writes nothing when a Stop withdrew every queued message first', async () => { @@ -114,8 +119,8 @@ export function registerJournalQueuedRejectionBatchTests(): void { journal.appendLifecycleBatch(startFailureBatch()) ]) - expect(journal.submissions()[0]?.rejection).toEqual({ kind: 'cancelled' }) - expect(journal.snapshot().items.map((item) => item.itemId)).toEqual([ + expect((await readTestJournalSubmissions(journal))[0]?.rejection).toEqual({ kind: 'cancelled' }) + expect((await readTestJournalItems(journal)).map((item) => item.itemId)).toEqual([ agentJournalSubmissionKey('first') ]) }) diff --git a/src/main/native-chat/agent-session-journal/submissions/journal-submission-positions.spec.ts b/src/main/native-chat/agent-session-journal/submissions/journal-submission-positions.spec.ts index 89d3096f331..57f3009f4c0 100644 --- a/src/main/native-chat/agent-session-journal/submissions/journal-submission-positions.spec.ts +++ b/src/main/native-chat/agent-session-journal/submissions/journal-submission-positions.spec.ts @@ -1,3 +1,4 @@ +import { readTestJournalItems } from '../journal-observation.test-support' // Rejections move rendered rows; submission positions must preserve original send order across replay. import { mkdtemp, rm } from 'node:fs/promises' @@ -60,7 +61,7 @@ export function registerJournalSubmissionPositionsTests(): void { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE }) - const pending = { ...journal.submission('send-1') } + const pending = { ...(await journal.reads.submission('send-1')) } const withdrawn = await journal.resolveDispatch({ clientMessageId: 'send-1', state: 'rejected', @@ -102,7 +103,7 @@ export function registerJournalSubmissionPositionsTests(): void { const journal = await sendWithdrawnByItsTurnEnd() const turnItemId = agentJournalItemKey(TURN) - expect(journal.submission('send-1')).toMatchObject({ + expect(await journal.reads.submission('send-1')).toMatchObject({ dispatchState: 'rejected', answeredInTurn: { turnItemId, via: 'start' } }) @@ -111,7 +112,7 @@ export function registerJournalSubmissionPositionsTests(): void { ]) await journals.closeAll() const replayed = await journals.open({ identity: IDENTITY, stateDirectory: root! }) - expect(replayed.submission('send-1')).toMatchObject({ + expect(await replayed.reads.submission('send-1')).toMatchObject({ answeredInTurn: { turnItemId, via: 'start' } }) }) @@ -124,14 +125,15 @@ export function registerJournalSubmissionPositionsTests(): void { expect(pending).toMatchObject({ submittedSequence: submitted.sequence }) expect(handedOver.sequence).toBeGreaterThan(submitted.sequence) - expect(journal.submission('send-1')).toMatchObject({ + expect(await journal.reads.submission('send-1')).toMatchObject({ dispatchState: 'rejected', submittedSequence: submitted.sequence }) // The rendered row moved; only the submission position retains send order. expect( - journal.snapshot().items.find((item) => item.itemId === agentJournalSubmissionKey('send-1')) - ?.sequence + (await readTestJournalItems(journal)).find( + (item) => item.itemId === agentJournalSubmissionKey('send-1') + )?.sequence ).toBe(withdrawn.sequence) }) }) diff --git a/src/main/native-chat/agent-session-journal/submissions/journal-submission-queued-link.spec.ts b/src/main/native-chat/agent-session-journal/submissions/journal-submission-queued-link.spec.ts index 5e530d870f3..4b9f80d0547 100644 --- a/src/main/native-chat/agent-session-journal/submissions/journal-submission-queued-link.spec.ts +++ b/src/main/native-chat/agent-session-journal/submissions/journal-submission-queued-link.spec.ts @@ -1,3 +1,4 @@ +import { readTestJournalSubmissions } from '../journal-observation.test-support' // Persist explicit queuedMessageId links; clients must not infer them by comparing unrelated IDs. import { mkdtemp, rm } from 'node:fs/promises' @@ -66,8 +67,8 @@ export function registerJournalSubmissionQueuedLinkTests(): void { { messageId: 'draft-1', expect: 'waiting' } ) ).rejects.toThrow() - expect(journal.submissions()).toHaveLength(0) - expect(journal.queuedMessages.get('draft-1')?.state).toBe('waiting') + expect(await readTestJournalSubmissions(journal)).toHaveLength(0) + expect((await journal.queuedMessages.get('draft-1'))?.state).toBe('waiting') }) }) } diff --git a/src/main/native-chat/agent-session-journal/writing/journal-atomic-settlement-contract-test-fixture.ts b/src/main/native-chat/agent-session-journal/writing/journal-atomic-settlement-contract-test-fixture.ts index 8bebced88ae..e79c76cf8f7 100644 --- a/src/main/native-chat/agent-session-journal/writing/journal-atomic-settlement-contract-test-fixture.ts +++ b/src/main/native-chat/agent-session-journal/writing/journal-atomic-settlement-contract-test-fixture.ts @@ -48,15 +48,17 @@ export function createFixture() { }) function rows() { - return readTestJournalRows(openTestJournalHostDatabase(root).db, SESSION, journal.epoch).map( - (stored) => { - const parsed = parseJournalRow(stored.rowJson) - if (!parsed.ok) { - throw new Error('test row failed to parse') - } - return parsed.row + return readTestJournalRows( + openTestJournalHostDatabase(root).db, + SESSION, + journal.physicalCursor().epoch + ).map((stored) => { + const parsed = parseJournalRow(stored.rowJson) + if (!parsed.ok) { + throw new Error('test row failed to parse') } - ) + return parsed.row + }) } async function seedWork(count = 201) { diff --git a/src/main/native-chat/agent-session-timeline/provider-timeline-assembler-test-support.ts b/src/main/native-chat/agent-session-timeline/provider-timeline-assembler-test-support.ts index 76fbf3c7c6b..3319e524d08 100644 --- a/src/main/native-chat/agent-session-timeline/provider-timeline-assembler-test-support.ts +++ b/src/main/native-chat/agent-session-timeline/provider-timeline-assembler-test-support.ts @@ -1,3 +1,4 @@ +import { readTestJournalItems } from '../agent-session-journal/journal-observation.test-support' // Exercise grammar events through the real deferred sink and on-disk journal. import { mkdtemp, rm } from 'node:fs/promises' @@ -269,7 +270,7 @@ export async function openProviderTimelineRig( const rows = async () => { elapse() await deferred.drained() - return journal.snapshot().items + return await readTestJournalItems(journal) } const turns = async () => (await rows()).flatMap((item) => { diff --git a/src/main/native-chat/agent-session-timeline/provider-timeline-bound-target-test-support.ts b/src/main/native-chat/agent-session-timeline/provider-timeline-bound-target-test-support.ts index d192a281a34..bff0554b8c0 100644 --- a/src/main/native-chat/agent-session-timeline/provider-timeline-bound-target-test-support.ts +++ b/src/main/native-chat/agent-session-timeline/provider-timeline-bound-target-test-support.ts @@ -1,38 +1,27 @@ import type { AgentSessionJournal } from '../agent-session-journal/journal-store' import type { StructuredAgentSessionEventTarget } from '../agent-session-wire/journal-ingress/structured-agent-session-event-sink' -import { journalSettledTurnItemIds } from '../agent-session-journal/item-lifecycle/journal-settled-turns' +import { StructuredAgentSessionSettledTurnRoutes } from '../agent-session-wire/host/structured-agent-session-settled-turn-routes' -/** Route each commit's settled turns to bound sinks as the host's conversation owner does. */ const settledTurnRoutes = new WeakMap< AgentSessionJournal, - Set<(turnItemIds: readonly string[]) => void> + StructuredAgentSessionSettledTurnRoutes >() export function boundTarget(journal: AgentSessionJournal): StructuredAgentSessionEventTarget { - let listeners = settledTurnRoutes.get(journal) - if (!listeners) { - const routed = new Set<(turnItemIds: readonly string[]) => void>() - listeners = routed - settledTurnRoutes.set(journal, routed) - journal.observeCommits((rows) => { - const turnItemIds = rows ? journalSettledTurnItemIds(rows) : [] - if (turnItemIds.length > 0) { - for (const listener of routed) { - listener(turnItemIds) - } - } - }) + let routes = settledTurnRoutes.get(journal) + if (!routes) { + routes = new StructuredAgentSessionSettledTurnRoutes({ warn: () => {}, error: () => {} }) + settledTurnRoutes.set(journal, routes) + const captured = routes + journal.observeCommits((commit) => + captured.deliver(journal.identity.sessionId, journal, commit) + ) } - const route = listeners + const route = routes return { journal, fence: 1, publish: () => {}, - observeSettledTurns: (listener) => { - route.add(listener) - return () => { - route.delete(listener) - } - } + observeSettledTurns: (listener) => route.observe(journal.identity.sessionId, journal, listener) } } diff --git a/src/main/native-chat/agent-session-wire/host/testing/mobile-queued-edit-host.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/mobile-queued-edit-host.test-fixture.ts index 0b05643e7d0..fb9b28610ca 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/mobile-queued-edit-host.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/mobile-queued-edit-host.test-fixture.ts @@ -1,3 +1,4 @@ +import { openTestAgentSessionDatabase } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import type { MobileQueuedEditHostFixtureModule, MobileCommandMenuHostFixtureModule @@ -81,7 +82,7 @@ export const createMobileCommandMenuHostFixture: MobileCommandMenuHostFixtureMod const { StructuredAgentSessionHost } = await import('../structured-agent-session-host') const { openTestAgentSessionRecordStore } = await import('../../../../runtime/agent-session/records/agent-session-record-store-test-harness') - const { openTestJournalHostDatabase, closeTestJournalHostDatabase } = + const { closeTestJournalHostDatabase } = await import('../../../agent-session-journal/store-lifecycle/journal-host-database-test-support') const { bindAtRestCommandContexts, retireAtRestCommandSettings, retireAtRestCommandContext } = await import('../../options/at-rest-command-context') @@ -176,7 +177,7 @@ export const createMobileCommandMenuHostFixture: MobileCommandMenuHostFixtureMod agents, store, adapter: new StructuredAgentSessionAdapterRouter(agents, async () => {}), - journalDatabase: openTestJournalHostDatabase(root), + journalDatabase: await openTestAgentSessionDatabase(root), claimKeyId: 'key', logger: recordingStructuredAgentSessionLogger().logger, probeOwner: async () => ({ outcome: 'pid-absent' }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-accept-then-deliver.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-accept-then-deliver.test.ts index a4f1156e725..6bf4b316f70 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-accept-then-deliver.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-accept-then-deliver.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { AGENT_JOURNAL_THREAD_SCOPE, type AgentJournalSubmission @@ -10,7 +11,10 @@ import { join } from 'node:path' import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from 'vitest' import { computeAgentSessionPayloadFingerprint } from '../../../../../shared/agent-session/lifecycle/agent-session-mutation-envelope' import type { AgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store' -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 { AgentSessionJournal } from '../../../agent-session-journal/journal-store' import { openAgentSessionJournal } from '../../../agent-session-journal/journal-store-factory' import type { StructuredAgentSessionAdapter } from '../../providers/structured-agent-session-adapter' @@ -25,10 +29,7 @@ import { hostTestMessage, resetHostTestOperationIds } from './structured-agent-session-host-test-data.test-fixture' -import { - closeTestJournalHostDatabases, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { closeTestJournalHostDatabases } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { createStructuredAgentSessionLogger } from '../structured-agent-session-logger' import { codexProviderHandle } from '../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import { NO_STRUCTURED_AGENTS } from '../../providers/testing/structured-agent-session-adapter-router.test-fixture' @@ -82,7 +83,7 @@ async function startHost(): Promise { reportedOptions: { model: '' } })) }), - journalDatabase: openTestJournalHostDatabase(root), + journalDatabase: await openTestAgentSessionDatabase(root), claimKeyId: 'key-1', mintSpawnToken: () => `spawn-${acquire.mock.calls.length}`, idleSweep: { intervalMs: 5, idleMs: () => idleMs }, @@ -119,7 +120,7 @@ afterEach(async () => { }) async function submission(id: string): Promise { - return (await host.journalSnapshot(SESSION)).submissions.find( + return (await readTestHostJournal(host, SESSION)).submissions.find( (entry) => entry.clientMessageId === id ) } @@ -135,7 +136,7 @@ async function writeAsEarlierProcess( }) const journal = await openAgentSessionJournal({ identity: journalIdentityFor(record, params), - database: openTestJournalHostDatabase(root) + database: await openTestAgentSessionDatabase(root) }) await write(journal, record.lease.runtimeFence) await journal.close() diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-acquire-isolation.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-acquire-isolation.test.ts index 059b37a5951..2dada5f2e8a 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-acquire-isolation.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-acquire-isolation.test.ts @@ -1,5 +1,8 @@ import { expect, it, vi } 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 { StructuredAgentSessionHost } from '../structured-agent-session-host' import { codexProviderHandle } from '../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import { @@ -11,7 +14,6 @@ import { } from './structured-agent-session-host-test-data.test-fixture' import { journalIdentityFor } from '../../acquisition/structured-agent-session-attach' import { openAgentSessionJournal } from '../../../agent-session-journal/journal-store-factory' -import { openTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { digestPayload } from '../../../agent-session-journal/item-lifecycle/journal-payload-bounds' import { CALLER, @@ -65,7 +67,7 @@ it('keeps a send a crash cut off as sent when its chat reattaches', async () => await prior.host.close(HOST_TEST_SESSION, 'evict') const journal = await openAgentSessionJournal({ identity: journalIdentityFor(record, attachParams()), - database: openTestJournalHostDatabase(prior.root) + database: await openTestAgentSessionDatabase(prior.root) }) await journal.appendSubmission({ clientMessageId: 'crashed-send', diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-adopted-import.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-adopted-import.test.ts index 444aef690ed..2376da4d393 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-adopted-import.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-adopted-import.test.ts @@ -4,7 +4,10 @@ import { join } from 'node:path' import { afterEach, describe, expect, it, vi } from 'vitest' import { computeAgentSessionPayloadFingerprint } from '../../../../../shared/agent-session/lifecycle/agent-session-mutation-envelope' import type { AgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store' -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 '../../providers/structured-agent-session-adapter' import { attachFingerprintFields, @@ -12,10 +15,7 @@ import { } from '../../acquisition/structured-agent-session-attach' import { AgentSessionJournal } from '../../../agent-session-journal/journal-store' import { StructuredAgentSessionHost } from '../structured-agent-session-host' -import { - closeTestJournalHostDatabases, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { closeTestJournalHostDatabases } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { createStructuredAgentSessionLogger } from '../structured-agent-session-logger' import { codexProviderHandle } from '../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import { NO_STRUCTURED_AGENTS } from '../../providers/testing/structured-agent-session-adapter-router.test-fixture' @@ -154,7 +154,7 @@ describe('adopting a provider conversation on create', () => { logger: createStructuredAgentSessionLogger(), store, adapter: adapter(), - journalDatabase: openTestJournalHostDatabase(root), + journalDatabase: await openTestAgentSessionDatabase(root), claimKeyId: 'key-1', mintSpawnToken: () => 'spawn-a', now: () => NOW diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-at-rest-command-rig.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-at-rest-command-rig.test-fixture.ts index 7770698cc46..93cbd693e8c 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-at-rest-command-rig.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-at-rest-command-rig.test-fixture.ts @@ -25,7 +25,10 @@ import { } from '../../../../claude/claude-at-rest-commands' import type { AgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store' -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 '../../providers/structured-agent-session-adapter' import { StructuredAgentSessionHost } from '../structured-agent-session-host' import { recordingStructuredAgentSessionLogger } from './structured-agent-session-logger.test-fixture' @@ -36,10 +39,7 @@ import { hostTestOperationId, resetHostTestOperationIds } from './structured-agent-session-host-test-data.test-fixture' -import { - closeTestJournalHostDatabases, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { closeTestJournalHostDatabases } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { claudeProviderHandle } from '../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' const runtimeMocks = vi.hoisted(() => ({ call: vi.fn(), subscribe: vi.fn() })) @@ -127,7 +127,7 @@ export async function openHost( agents, store, adapter: deliveryAdapter, - journalDatabase: openTestJournalHostDatabase(directory), + journalDatabase: await openTestAgentSessionDatabase(directory), claimKeyId: 'key', logger: recordingStructuredAgentSessionLogger().logger, now: () => clock, diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attachment-admission.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attachment-admission.test.ts index db5798ce27f..aadcfe638bf 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attachment-admission.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attachment-admission.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' // Direct/queued admission rejects missing attachments atomically; stored claims survive clear migration. import { existsSync } from 'node:fs' @@ -80,7 +81,7 @@ describe('attachments at send admission', () => { } } }) - expect((await rig.host.journalSnapshot(SESSION)).submissions).toHaveLength(0) + expect((await readTestHostJournal(rig.host, SESSION)).submissions).toHaveLength(0) expect(claimedSessions()).toEqual([]) expect(existsSync(fresh)).toBe(true) }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attention-reads.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attention-reads.test.ts index 81aa3a6b659..78f8fb6c9a3 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attention-reads.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attention-reads.test.ts @@ -158,7 +158,7 @@ it('acknowledges a post-rewind completion after normal idle retirement without r provider.appendItem(firstTurnIdentity, firstTurn, { turnScope: AGENT_JOURNAL_THREAD_SCOPE }) await rig.host.flushStreamedEvents(SESSION) const journal = rig.host.collaboratorsForTests().sessions.get(SESSION)!.journal - const physicalEpoch = journal.epoch + const physicalEpoch = journal.physicalCursor().epoch await journal.replaceEpochItems( 'handle_forked', attached.fence, @@ -189,7 +189,7 @@ it('acknowledges a post-rewind completion after normal idle retirement without r const next = restTestSend('finish after rewind', attached.fence) const nextReply = await rig.host.send(REST_TEST_CALLER, next) expect(nextReply).toMatchObject({ ok: true }) - expect(journal.submission(next.envelope.clientOperationId)).toBeDefined() + expect(await journal.reads.submission(next.envelope.clientOperationId)).toBeDefined() await vi.waitFor(() => expect(rig.adapter.dispatch).toHaveBeenCalledTimes(2)) const request = rig.adapter.dispatch.mock.calls[1]![0] const dispatched = await rig.adapter.dispatch.mock.results[1]!.value diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-background-stop-unreadable.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-background-stop-unreadable.test.ts index 21e4702cd18..d43cd28f608 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-background-stop-unreadable.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-background-stop-unreadable.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { afterEach, expect, it, vi } from 'vitest' import type { AgentChildWorkView } from '../../../../../shared/agent-status/child-work/agent-status-child-work-view' import { @@ -67,7 +68,7 @@ async function rigWithTaskList(): Promise<{ } async function unconfirmedNotes(testRig: QueuedMessageTestRig) { - return (await testRig.host.journalSnapshot(SESSION)).items.filter( + return (await readTestHostJournal(testRig.host, SESSION)).items.filter( (item) => item.body.kind === 'status' && item.body.failure?.kind === 'cancelUnconfirmed' ) } diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-background-wake-owner.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-background-wake-owner.test.ts index 2b023100e9c..53336fa9817 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-background-wake-owner.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-background-wake-owner.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { expect, it } from 'vitest' import { readAgentJournalTurn } from '../../../../../shared/agent-session/lifecycle/agent-session-turn-record' import { agentJournalSubmissionKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' @@ -19,7 +20,7 @@ import { async function turns() { await laneDrained() await fixture.host.flushStreamedEvents(SESSION) - return (await fixture.host.journalSnapshot(SESSION)).items.flatMap((item) => { + return (await readTestHostJournal(fixture.host, SESSION)).items.flatMap((item) => { const turn = readAgentJournalTurn(item.body) return turn ? [turn] : [] }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-echo-working.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-echo-working.test.ts index 440acafece9..3329ea649c9 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-echo-working.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-echo-working.test.ts @@ -13,7 +13,10 @@ import { PROVIDER_SESSION_ID } from '../../../../claude/structured/claude-structured-session-test-support' import type { AgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store' -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 { structuredClaudeLifecycleEvent } from '../../../../runtime/agent-session/installation/structured-claude-runtime-adapter' import { StructuredAgentSessionHost } from '../structured-agent-session-host' import { @@ -24,10 +27,7 @@ import { hostTestOperationId, resetHostTestOperationIds } from './structured-agent-session-host-test-data.test-fixture' -import { - closeTestJournalHostDatabases, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { closeTestJournalHostDatabases } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { createStructuredAgentSessionLogger } from '../structured-agent-session-logger' import { NO_STRUCTURED_AGENTS } from '../../providers/testing/structured-agent-session-adapter-router.test-fixture' @@ -73,7 +73,7 @@ beforeEach(async () => { logger: createStructuredAgentSessionLogger(), store, adapter: Object.assign(adapter, { supportsCreate: () => true }), - journalDatabase: openTestJournalHostDatabase(root), + journalDatabase: await openTestAgentSessionDatabase(root), claimKeyId: 'key-1', mintSpawnToken: () => 'spawn-a', now: () => NOW diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-stop-contract-test-actions.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-stop-contract-test-actions.test-fixture.ts index 9bc2a7afc4c..3b30f809fa3 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-stop-contract-test-actions.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-stop-contract-test-actions.test-fixture.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { createStoppedClaudeDeadline } from './structured-agent-session-stop-deadline.test-fixture' import { expect, vi } from 'vitest' import { computeAgentSessionPayloadFingerprint } from '../../../../../shared/agent-session/lifecycle/agent-session-mutation-envelope' @@ -57,7 +58,7 @@ export async function send(text: string, fence?: number): Promise { } export async function dispatch(clientMessageId: string) { - const submission = (await fixture.host.journalSnapshot(SESSION)).submissions.findLast( + const submission = (await readTestHostJournal(fixture.host, SESSION)).submissions.findLast( (entry) => entry.clientMessageId === clientMessageId || entry.queuedMessageId === clientMessageId ) @@ -89,7 +90,7 @@ export async function openTurn( }) await eventually(async () => expect((await dispatch(clientMessageId)).state).toBe('accepted')) const turnId = activeStructuredAgentSessionTurnId( - (await fixture.host.journalSnapshot(SESSION)).items + (await readTestHostJournal(fixture.host, SESSION)).items ) expect(turnId).not.toBeNull() return turnId! @@ -124,14 +125,14 @@ export async function interruptSent(connection: FakeConnection): Promise { export async function turnOutcome(): Promise { await fixture.host.flushStreamedEvents(SESSION) - const snapshot = await fixture.host.journalSnapshot(SESSION) + const snapshot = await readTestHostJournal(fixture.host, SESSION) return readAgentJournalTurn(snapshot.items.findLast((item) => item.body.kind === 'turn')?.body) ?.outcome } export async function statusTexts(): Promise { await fixture.host.flushStreamedEvents(SESSION) - return (await fixture.host.journalSnapshot(SESSION)).items.flatMap((item) => + return (await readTestHostJournal(fixture.host, SESSION)).items.flatMap((item) => item.body.kind === 'status' ? [String(item.body.text)] : [] ) } @@ -150,7 +151,7 @@ export function childRecords() { export async function agentStatus() { await fixture.host.flushStreamedEvents(SESSION) - const { items, submissions } = await fixture.host.journalSnapshot(SESSION) + const { items, submissions } = await readTestHostJournal(fixture.host, SESSION) const { summary } = projectStructuredAgentSessionStatusState( items, submissions, @@ -180,7 +181,7 @@ export async function ask( }) const card = await eventually(async () => { await fixture.host.flushStreamedEvents(SESSION) - const item = (await fixture.host.journalSnapshot(SESSION)).items.find( + const item = (await readTestHostJournal(fixture.host, SESSION)).items.find( (entry) => entry.body.kind === 'approval' || entry.body.kind === 'question' ) expect(item).toBeDefined() @@ -199,7 +200,7 @@ export function cancelCard(turnId: string, prompt: { itemId: string; expectedRev export async function cardResolution(itemId: string): Promise { await fixture.host.flushStreamedEvents(SESSION) - const body = (await fixture.host.journalSnapshot(SESSION)).items.find( + const body = (await readTestHostJournal(fixture.host, SESSION)).items.find( (item) => item.itemId === itemId )?.body return body?.kind === 'approval' || body?.kind === 'question' ? body.resolution : undefined diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-stop-contract.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-stop-contract.test-fixture.ts index 684f9eebf7a..badd967acb7 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-stop-contract.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-stop-contract.test-fixture.ts @@ -12,12 +12,12 @@ import { PROVIDER_SESSION_ID } from '../../../../claude/structured/claude-structured-session-test-support' import type { AgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store' -import { openTestAgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' -import { structuredClaudeLifecycleEvent } from '../../../../runtime/agent-session/installation/structured-claude-runtime-adapter' import { - closeTestJournalHostDatabases, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' + openTestAgentSessionRecordStore, + openTestAgentSessionDatabase +} from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' +import { structuredClaudeLifecycleEvent } from '../../../../runtime/agent-session/installation/structured-claude-runtime-adapter' +import { closeTestJournalHostDatabases } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { StructuredAgentSessionHost } from '../structured-agent-session-host' import { recordingStructuredAgentSessionLogger } from './structured-agent-session-logger.test-fixture' import { @@ -106,7 +106,7 @@ beforeEach(async () => { agents: NO_STRUCTURED_AGENTS, store, adapter: Object.assign(adapter, { supportsCreate: () => true }), - journalDatabase: openTestJournalHostDatabase(root), + journalDatabase: await openTestAgentSessionDatabase(root), claimKeyId: 'key-1', mintSpawnToken: () => 'spawn-a', logger: logs.logger, diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-stop-plan-card.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-stop-plan-card.test.ts index 84ccf2b7576..1f3f81ae0b1 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-stop-plan-card.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-stop-plan-card.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { expect, it } from 'vitest' import { HOST_TEST_SESSION as SESSION } from './structured-agent-session-host-test-data.test-fixture' @@ -34,7 +35,7 @@ it('dismisses a plan card on its Cancel: Claude is told to wait for the user, an expect(JSON.stringify(reply)).not.toMatch(/keep planning|ExitPlanMode again/i) expect(connection.calls.some((call) => call.subtype === 'interrupt')).toBe(false) expect(connection.closed).toBe(false) - const cards = (await fixture.host.journalSnapshot(SESSION)).items.filter( + const cards = (await readTestHostJournal(fixture.host, SESSION)).items.filter( (item) => item.body.kind === 'approval' ) expect(cards).toHaveLength(1) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-close-aborts-start.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-close-aborts-start.test.ts index 256a88b835a..104ac97e349 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-close-aborts-start.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-close-aborts-start.test.ts @@ -2,7 +2,6 @@ import { expect, it, vi } from 'vitest' import { AgentSessionRecoveryCapsule } from '../../../../runtime/agent-session/records/agent-session-recovery-capsule' -import { openTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { StructuredAgentSessionHost } from '../structured-agent-session-host' import { adapter, @@ -24,7 +23,7 @@ function openHost(state: ReturnType): StructuredAgentSessi logger: createStructuredAgentSessionLogger(), store: state.store, adapter: adapter(), - journalDatabase: openTestJournalHostDatabase(state.root), + journalDatabase: state.host.deps.journalDatabase, recoveryCapsule: new AgentSessionRecoveryCapsule(state.root), claimKeyId: 'key-1', mintSpawnToken: () => 'spawn-a', diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-codex-held-goal.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-codex-held-goal.test.ts index ba6d87195a8..253fa7aa920 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-codex-held-goal.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-codex-held-goal.test.ts @@ -56,7 +56,7 @@ it.each([ await rig.settleAccepted(first, 'a') await publishRunningTurn(first) expect( - rig.host.collaboratorsForTests().sessions.get(SESSION)?.journal.newestTurn() + await rig.host.collaboratorsForTests().sessions.get(SESSION)?.journal.reads.newestTurn() ).toMatchObject({ state: 'running', userItemId: agentJournalSubmissionKey(first) }) const held = rig.send('Use a different model', undefined, { carried: { model: 'model-b' } diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-codex-stop-note-end.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-codex-stop-note-end.test.ts index 11b823ee010..a7ff344531b 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-codex-stop-note-end.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-codex-stop-note-end.test.ts @@ -1,5 +1,8 @@ +import { + readTestHostJournal, + readTestJournalItems +} from '../../../agent-session-journal/journal-observation.test-support' import { createDeferredStructuredAgentSessionEventSink } from '../../journal-ingress/structured-agent-session-event-sink' -import { openTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -11,7 +14,10 @@ import { identityFor, fakeCodex } from '../../../../codex/structured/session-lifecycle/codex-structured-session-adapter-fixture' -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 { CodexStructuredSessionAdapter } from '../../../../codex/structured/session-lifecycle/codex-structured-session-adapter' import { StructuredAgentSessionHost } from '../structured-agent-session-host' import { @@ -57,7 +63,7 @@ beforeEach(async () => { logger: recordingStructuredAgentSessionLogger().logger, store, adapter: Object.assign(adapter, { supportsCreate: () => true }), - journalDatabase: openTestJournalHostDatabase(root), + journalDatabase: await openTestAgentSessionDatabase(root), claimKeyId: 'key-1', mintSpawnToken: () => 'spawn-1', now: () => NOW @@ -88,7 +94,7 @@ it('isolates legacy acquired history from live matching and saves exact proof be await journal().replaceEpochItems('handle_forked', store.getRecord(SESSION)!.lease.runtimeFence, [ { identity: { provider: 'orca', clientMessageId: 'U' }, body, ownerFence: 1 } ]) - expect(journal().item('orca:U')?.revision).toBeGreaterThan(1) + expect((await journal().reads.item('orca:U'))?.revision).toBeGreaterThan(1) const user = (id: string | null) => ({ type: 'userMessage', id: 'old-user', @@ -104,7 +110,7 @@ it('isolates legacy acquired history from live matching and saves exact proof be await host.close(SESSION, 'evict') launch.resumeThreadId = THREAD codex.routes['thread/resume'] = () => ({ thread: { id: THREAD, turns: returned } }) - await host.journalSnapshot(SESSION) + await readTestHostJournal(host, SESSION) const deferred = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging()) deferred.bind({ journal: journal(), @@ -130,8 +136,8 @@ it('isolates legacy acquired history from live matching and saves exact proof be await reopen([ oldTurn([user(null), { type: 'agentMessage', id: 'old-answer', text: 'historical answer' }]) ]) - expect(journal().submission('U')?.dispatchState).toBe('unknown') - expect(journal().receiptFor('U')).toBeNull() + expect((await journal().reads.submission('U'))?.dispatchState).toBe('unknown') + expect(await journal().reads.receiptFor('U')).toBeNull() const before = journal().cursor() await reopen([ oldTurn([user(null), { type: 'agentMessage', id: 'old-answer', text: 'historical answer' }]) @@ -148,8 +154,8 @@ it('isolates legacy acquired history from live matching and saves exact proof be { type: 'agentMessage', id: 'previously-unseen', text: 'must stay hidden' } ]) ]) - expect(journal().submission('U')?.dispatchState).toBe('accepted') - expect(journal().snapshot().items).toEqual([]) + expect((await journal().reads.submission('U'))?.dispatchState).toBe('accepted') + expect(await readTestJournalItems(journal())).toEqual([]) await reopen([ { id: 'huge', @@ -164,11 +170,11 @@ it('isolates legacy acquired history from live matching and saves exact proof be ] } ]) - expect(journal().submission('K')?.dispatchState).toBe('accepted') - expect(journal().snapshot().items).toEqual([]) - expect(journal().submission('V')?.dispatchState).toBe('unknown') + expect((await journal().reads.submission('K'))?.dispatchState).toBe('accepted') + expect(await readTestJournalItems(journal())).toEqual([]) + expect((await journal().reads.submission('V'))?.dispatchState).toBe('unknown') expect(pages).not.toHaveBeenCalled() await reopen([]) - expect(journal().receiptFor('K')).not.toBeNull() - expect(journal().submission('V')?.dispatchState).toBe('unknown') + expect(await journal().reads.receiptFor('K')).not.toBeNull() + expect((await journal().reads.submission('V'))?.dispatchState).toBe('unknown') }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-create-adoption.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-create-adoption.test.ts index 234b9853ccf..eca7abc05ea 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-create-adoption.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-create-adoption.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { mkdir, realpath, rename, writeFile } from 'node:fs/promises' vi.mock( '../../../../ai-vault/scanners/opencode/reader-transport/session-scanner-opencode-sqlite-worker-spawn', @@ -135,7 +136,7 @@ it.each([ if (!result.ok) { throw new Error('Real selected-account adoption failed') } - const snapshot = await fixture.rig.host.journalSnapshot(SESSION) + const snapshot = await readTestHostJournal(fixture.rig.host, SESSION) expect(snapshot.items.map(({ body }) => body)).toEqual( _kind === 'legacy' ? [ diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-failed-create-owner-verdict.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-failed-create-owner-verdict.test.ts index 2482f880cba..98700f34af1 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-failed-create-owner-verdict.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-failed-create-owner-verdict.test.ts @@ -5,7 +5,10 @@ import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from 'vitest' import type { AgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store' -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 '../../providers/structured-agent-session-adapter' import { StructuredAgentSessionHost } from '../structured-agent-session-host' import { @@ -14,10 +17,7 @@ import { hostTestAttachParams, resetHostTestOperationIds } from './structured-agent-session-host-test-data.test-fixture' -import { - closeTestJournalHostDatabases, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { closeTestJournalHostDatabases } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { createStructuredAgentSessionLogger } from '../structured-agent-session-logger' import { codexProviderHandle } from '../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import { NO_STRUCTURED_AGENTS } from '../../providers/testing/structured-agent-session-adapter-router.test-fixture' @@ -60,7 +60,7 @@ beforeEach(async () => { answerPrompt: vi.fn(async () => undefined), setOption: vi.fn(async () => undefined) }, - journalDatabase: openTestJournalHostDatabase(root), + journalDatabase: await openTestAgentSessionDatabase(root), claimKeyId: 'key-1', mintSpawnToken: () => 'spawn-a', now: () => NOW diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-fork-rewind.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-fork-rewind.test.ts index 9a7172dca8f..44a0fb2f677 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-fork-rewind.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-fork-rewind.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -9,11 +10,11 @@ import { claudeProviderHandle } from '../../../../../shared/agent-session/lifecy import { agentSessionProviderHandleChainHead } from '../../../../../shared/agent-session/lifecycle/agent-session-provider-handle' import type { AgentSessionRewindFork } from '../../../../../shared/agent-session/lifecycle/agent-session-rewind' import type { AgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store' -import { openTestAgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { - closeTestJournalHostDatabases, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' + openTestAgentSessionRecordStore, + openTestAgentSessionDatabase +} from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' +import { closeTestJournalHostDatabases } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import type { StructuredAgentSessionAdapter } from '../../providers/structured-agent-session-adapter' import { claudeAndCodexAgents } from '../../providers/testing/structured-agent-session-adapter-router.test-fixture' import type { StructuredAgentSessionEventSink } from '../../journal-ingress/structured-agent-session-event-sink' @@ -132,7 +133,7 @@ beforeEach(async () => { logger: createStructuredAgentSessionLogger(), store, adapter, - journalDatabase: openTestJournalHostDatabase(directory), + journalDatabase: await openTestAgentSessionDatabase(directory), claimKeyId: 'key', now: () => HOST_TEST_NOW, probeOwner: async () => ({ outcome: 'exit-observed' }) @@ -163,7 +164,7 @@ afterEach(async () => { async function rewindSecondPrompt(uuid = 'prompt-2') { const fields = { itemId: key(uuid), - expectedEpoch: (await host.journalSnapshot(HOST_TEST_SESSION)).cursor.epoch + expectedEpoch: (await readTestHostJournal(host, HOST_TEST_SESSION)).cursor.epoch } return host.rewind(caller, { ...fields, @@ -182,10 +183,10 @@ async function rewindSecondPrompt(uuid = 'prompt-2') { describe('rewinding a chat whose agent forks', () => { it('keeps the original conversation when saving its fork fails', async () => { - const before = await host.journalSnapshot(HOST_TEST_SESSION) + const before = await readTestHostJournal(host, HOST_TEST_SESSION) forkForRewind.mockRejectedValueOnce(new Error('transcript write failed')) await expect(rewindSecondPrompt()).rejects.toThrow('transcript write failed') - expect(await host.journalSnapshot(HOST_TEST_SESSION)).toEqual(before) + expect(await readTestHostJournal(host, HOST_TEST_SESSION)).toEqual(before) expect(store.getRecord(HOST_TEST_SESSION)?.rewind).toBeUndefined() expect(closeSession).not.toHaveBeenCalled() }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host.test-fixture.ts index e8be56955a8..591f59e119a 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host.test-fixture.ts @@ -9,11 +9,11 @@ import type { AgentSessionRecord } from '../../../../../shared/agent-session/lif import type { AgentSessionMutationEnvelope } from '../../../../../shared/agent-session/agent-session-wire' import type { AgentChildWorkView } from '../../../../../shared/agent-status/child-work/agent-status-child-work-view' import type { AgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store' -import { openTestAgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { - createTrackedJournalOpener, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' + openTestAgentSessionRecordStore, + openTestAgentSessionDatabase +} from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' +import { createTrackedJournalOpener } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import type { AgentSessionDispatchOutcome, StructuredAgentSessionAdapter @@ -169,7 +169,7 @@ beforeEach(async () => { logger: log.logger, store, adapter: adapter(), - journalDatabase: openTestJournalHostDatabase(root), + journalDatabase: await openTestAgentSessionDatabase(root), recoveryCapsule, claimKeyId: 'key-1', mintSpawnToken: () => 'spawn-a', diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-input-order-rewind.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-input-order-rewind.test.ts index 5788bb8c115..b5306436ee6 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-input-order-rewind.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-input-order-rewind.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalItems } from '../../../agent-session-journal/journal-observation.test-support' import { it, expect, vi } from 'vitest' import { createQueuedMessageTestRig, @@ -27,14 +28,14 @@ it('preserves existing queuedAt across two live replacements despite an older or ) await rig.settleAccepted(second.id, 'second') const journal = rig.host.collaboratorsForTests().sessions.get(SESSION)!.journal - const epoch1 = journal.epoch + const epoch1 = journal.physicalCursor().epoch const original = journal.replaceEpochItems.bind(journal) const snapshots: unknown[] = [] let command: Promise | undefined, rewindAgain: Promise | undefined const c = hostTestOperationId() const fields = { command: 'compact' as const, delivery: 'queue-if-active' as const } const capture = () => ({ - epoch: journal.epoch, + epoch: journal.physicalCursor().epoch, submissions: journal.submissions().map((s) => ({ id: s.clientMessageId, seq: s.acceptedSequence, @@ -55,16 +56,12 @@ it('preserves existing queuedAt across two live replacements despite an older or envelope: rig.envelope(fields, 'agentSession.conversationCommand', c), userSend: true }) - const target = journal - .snapshot() - .items.find( - (item) => - item.body.kind === 'message' && - item.body.blocks.some( - (block) => block.type === 'text' && block.text === 'work on this' - ) - )! - const next = { itemId: target.itemId, expectedEpoch: journal.epoch } + const target = (await readTestJournalItems(journal)).find( + (item) => + item.body.kind === 'message' && + item.body.blocks.some((block) => block.type === 'text' && block.text === 'work on this') + )! + const next = { itemId: target.itemId, expectedEpoch: journal.physicalCursor().epoch } rewindAgain = rig.host.rewind(CALLER, { ...next, envelope: rig.envelope(next, 'agentSession.rewind', hostTestOperationId()) @@ -85,7 +82,7 @@ it('preserves existing queuedAt across two live replacements despite an older or expect(secondVerdict).toMatchObject({ ok: true }) expect(snapshots).toHaveLength(4) const receipt = rig.store.readCommandReceipt({ kind: 'global' }, m.id) - const card = journal.queuedMessages.get(c)! + const card = (await journal.queuedMessages.get(c))! expect(receipt).toMatchObject({ verdict: 'readable', receipt: { @@ -99,9 +96,9 @@ it('preserves existing queuedAt across two live replacements despite an older or expect(journal.cursor().epoch).not.toBe(epoch1) expect(card.queuedAt!.epoch).toBe(journal.physicalCursor().epoch) expect(card.queuedAt!.sequence).toBeGreaterThanOrEqual( - journal.submission(m.id)!.acceptedSequence! + (await journal.reads.submission(m.id))!.acceptedSequence! ) - expect(journal.queuedMessages.pauses()).toEqual([]) + expect(await journal.queuedMessages.pauses()).toEqual([]) await eventually(async () => expect((await rig.submission(m.id))?.handedOverAt).toBeDefined()) await rig.settleAccepted(m.id, 'M') await eventually(() => expect(rig.compact).toHaveBeenCalledTimes(1)) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-late-settlement-cancelled.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-late-settlement-cancelled.test.ts index c6af0fbba43..335858f16bd 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-late-settlement-cancelled.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-late-settlement-cancelled.test.ts @@ -118,7 +118,7 @@ describe('settling a send the provider proves it received after the ack window', const after = journal().readSince(before) expect(after.ok && after.rows).toHaveLength(1) - expect(journal().submission(params.envelope.clientOperationId)).toMatchObject({ + expect(await journal().reads.submission(params.envelope.clientOperationId)).toMatchObject({ dispatchState: 'accepted', providerItemId: `claude:${THREAD}:echo-row` }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-late-settlement-contract.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-late-settlement-contract.test-fixture.ts index a69e8f4db55..3a9435d8d44 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-late-settlement-contract.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-late-settlement-contract.test-fixture.ts @@ -1,3 +1,4 @@ +import { readTestJournalSubmissions } from '../../../agent-session-journal/journal-observation.test-support' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -5,7 +6,10 @@ import { afterEach, beforeEach, expect, vi, type Mock } from 'vitest' import { computeAgentSessionPayloadFingerprint } from '../../../../../shared/agent-session/lifecycle/agent-session-mutation-envelope' import type { AgentSessionMutationEnvelope } from '../../../../../shared/agent-session/agent-session-wire' import type { AgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store' -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 { AgentSessionJournal } from '../../../agent-session-journal/journal-store' import type { AgentSessionDispatchOutcome, @@ -21,10 +25,7 @@ import { hostTestOperationId, resetHostTestOperationIds } from './structured-agent-session-host-test-data.test-fixture' -import { - closeTestJournalHostDatabases, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { closeTestJournalHostDatabases } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { createStructuredAgentSessionLogger } from '../structured-agent-session-logger' import { codexProviderHandle } from '../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import { NO_STRUCTURED_AGENTS } from '../../providers/testing/structured-agent-session-adapter-router.test-fixture' @@ -83,9 +84,9 @@ export function journal(): AgentSessionJournal { export async function handedOver(clientMessageId: string): Promise { await vi.waitFor(async () => { expect( - journal() - .submissions() - .find((entry) => entry.clientMessageId === clientMessageId)?.handedOverAt + (await readTestJournalSubmissions(journal())).find( + (entry) => entry.clientMessageId === clientMessageId + )?.handedOverAt ).toBeDefined() expect(dispatch).toHaveBeenCalled() }) @@ -131,7 +132,7 @@ beforeEach(async () => { }, () => host ), - journalDatabase: openTestJournalHostDatabase(root), + journalDatabase: await openTestAgentSessionDatabase(root), claimKeyId: 'key-1', mintSpawnToken: () => 'spawn-a', now: () => NOW diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-newer-content-host.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-newer-content-host.test.ts index 031065dda0c..1f2b85038ff 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-newer-content-host.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-newer-content-host.test.ts @@ -2,11 +2,11 @@ import { cp, rm } from 'node:fs/promises' import { afterEach, beforeEach, expect, it, vi } from 'vitest' import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../shared/agent-session/journal/agent-session-journal-types' import type { AgentJournalItemBody } from '../../../../../shared/agent-session/journal/agent-session-journal-types' -import { openTestAgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { - closeTestJournalHostDatabases, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' + openTestAgentSessionRecordStore, + openTestAgentSessionDatabase +} from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' +import { closeTestJournalHostDatabases } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { StructuredAgentSessionHost } from '../structured-agent-session-host' import { adapter, @@ -86,7 +86,7 @@ async function relaunch(): Promise { logger: createStructuredAgentSessionLogger(), store, adapter: adapter(), - journalDatabase: openTestJournalHostDatabase(relaunched), + journalDatabase: await openTestAgentSessionDatabase(relaunched), claimKeyId: 'key-1', mintSpawnToken: () => 'spawn-next', now: () => NOW diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queue-import-retained-history.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queue-import-retained-history.test.ts index 943ce552460..4b87e55801c 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queue-import-retained-history.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queue-import-retained-history.test.ts @@ -1,3 +1,7 @@ +import { + readTestJournalItems, + readTestJournalSnapshot +} from '../../../agent-session-journal/journal-observation.test-support' import { expect, it } from 'vitest' import { agentJournalSubmissionKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' import { @@ -73,8 +77,8 @@ it.each(['person', 'mail'] as const)( await journal.open() await journal.queuedMessages.importAtOpen() expect(legacyIds(sessionId)).toEqual([]) - expect(journal.queuedMessages.list().map((card) => card.messageId)).toEqual(['W']) - expect(journal.queuedMessages.userStopInForce() === null).toBe(sender === 'person') + expect((await journal.queuedMessages.list()).map((card) => card.messageId)).toEqual(['W']) + expect((await journal.queuedMessages.userStopInForce()) === null).toBe(sender === 'person') await journal.appendSubmission({ clientMessageId: `unattended-${opened}`, body: message('wake'), @@ -90,7 +94,7 @@ it.each(['person', 'mail'] as const)( providerIdentity: null, fence: 0 }) - expect(journal.queuedMessages.nextInputCard()?.messageId ?? null).toBe( + expect((await journal.queuedMessages.nextInputCard())?.messageId ?? null).toBe( sender === 'person' ? 'W' : null ) await journal.close() @@ -135,8 +139,8 @@ it.each(['person', 'mail'] as const)( journal = journalFor(sessionId) await journal.open() expect(legacyIds(sessionId)).toEqual([]) - expect(journal.queuedMessages.get('S')).toBeNull() - expect(journal.queuedMessages.userStopInForce() === null).toBe(sender === 'person') + expect(await journal.queuedMessages.get('S')).toBeNull() + expect((await journal.queuedMessages.userStopInForce()) === null).toBe(sender === 'person') await journal.appendSubmission({ clientMessageId: `wake-${opened}`, body: message('wake'), @@ -152,7 +156,7 @@ it.each(['person', 'mail'] as const)( providerIdentity: null, fence: 0 }) - expect(journal.queuedMessages.nextInputCard()?.messageId ?? null).toBe( + expect((await journal.queuedMessages.nextInputCard())?.messageId ?? null).toBe( sender === 'person' ? 'W' : null ) if (opened === 0) { @@ -219,12 +223,12 @@ it('a retired personal handoff in a marker-5 prefix keeps Stop lifted without re for (let opened = 0; opened < 2; opened++) { journal = journalFor(sessionId) await journal.open() - expect(journal.queuedMessages.userStopInForce()).toBeNull() - expect(journal.submission('H')).toBeUndefined() - expect(journal.item(agentJournalSubmissionKey('H'))).toBeNull() - expect(journal.queuedMessages.get('S')).toBeNull() + expect(await journal.queuedMessages.userStopInForce()).toBeNull() + expect(await journal.reads.submission('H')).toBeUndefined() + expect(await journal.reads.item(agentJournalSubmissionKey('H'))).toBeNull() + expect(await journal.queuedMessages.get('S')).toBeNull() await journal.appendQueueReopen(0) - expect(journal.queuedMessages.nextInputCard()).toBeNull() + expect(await journal.queuedMessages.nextInputCard()).toBeNull() await journal.appendSubmission({ clientMessageId: `wake-${opened}`, body: message('wake'), @@ -240,7 +244,7 @@ it('a retired personal handoff in a marker-5 prefix keeps Stop lifted without re providerIdentity: null, fence: 0 }) - expect(journal.queuedMessages.nextInputCard()?.messageId).toBe('W') + expect((await journal.queuedMessages.nextInputCard())?.messageId).toBe('W') await journal.replaceEpochItems('handle_forked', 0, []) await journal.close() } @@ -270,8 +274,8 @@ it('a new-format missing-card attempt cannot invent legacy personal provenance', await journal.close() journal = journalFor(SESSION) await journal.open() - expect(journal.queuedMessages.userStopInForce()).not.toBeNull() - expect(journal.queuedMessages.nextInputCard()).toBeNull() + expect(await journal.queuedMessages.userStopInForce()).not.toBeNull() + expect(await journal.queuedMessages.nextInputCard()).toBeNull() await journal.close() }) @@ -369,7 +373,10 @@ it.each(retainedHistoryCases)('retained history: $adopt / $proof', async ({ adop }) expect(events[0]?.type).toBe('snapshot') while (state.hasOlder) { - const requestedCursor = { epoch: journal.epoch, sequence: state.items[0]!.sequence } + const requestedCursor = { + epoch: journal.physicalCursor().epoch, + sequence: state.items[0]!.sequence + } const older = readAgentSessionHistory( journal, { sessionId: SESSION, direction: 'before', cursor: requestedCursor }, @@ -382,7 +389,7 @@ it.each(retainedHistoryCases)('retained history: $adopt / $proof', async ({ adop page: structuredClone(older.page) }) } - const canonical = journal.snapshot() + const canonical = await readTestJournalSnapshot(journal) expect([state.items, state.submissions]).toEqual([canonical.items, canonical.submissions]) feed.publish(SESSION, journal) await append(journal, 205) @@ -400,9 +407,9 @@ it.each(retainedHistoryCases)('retained history: $adopt / $proof', async ({ adop feed.publish(SESSION, journal) } if (proof !== 'none') { - const placed = journal - .snapshot() - .items.find((item) => item.itemId === agentJournalSubmissionKey('u')) + const placed = (await readTestJournalItems(journal)).find( + (item) => item.itemId === agentJournalSubmissionKey('u') + ) const loaded = clients.map((client) => client().items.length) const normalizationKey = journal.publicRead().normalizationKey const identity = { @@ -412,7 +419,7 @@ it.each(retainedHistoryCases)('retained history: $adopt / $proof', async ({ adop ordinal: 206 } if (proof === 'dispatch') { - await journal.acceptLate({ + await journal.settleLateDispatch({ clientMessageId: 'u', state: 'accepted', fence: 0, @@ -421,8 +428,8 @@ it.each(retainedHistoryCases)('retained history: $adopt / $proof', async ({ adop } await journal.appendItem(identity, message('stopped words'), threadWrite) feed.publish(SESSION, journal) - expect(journal.submission('u')?.dispatchState).toBe('accepted') - expect(journal.receiptFor('u')).not.toBeNull() + expect((await journal.reads.submission('u'))?.dispatchState).toBe('accepted') + expect(await journal.reads.receiptFor('u')).not.toBeNull() expect(journal.publicRead().normalizationKey).toBe(normalizationKey) expect(clients.map((client) => client().items.length)).toEqual(loaded) for (const client of clients) { diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig.test-fixture.ts index cfe6123d87a..bd53b16d0c3 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig.test-fixture.ts @@ -10,7 +10,10 @@ import type { AgentJournalSubmission } from '../../../../../shared/agent-session import type { AgentSessionQueuePause } from '../../../../../shared/agent-session/requests/agent-session-queued-message-wire' import type { AgentSessionExecutionLocation } from '../../../../../shared/agent-session/lifecycle/agent-session-record' import { agentSessionMessagePayload } from '../../../../../shared/agent-session/requests/structured-agent-session-send-mutation' -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 { AgentSessionRecoveryCapsule } from '../../../../runtime/agent-session/records/agent-session-recovery-capsule' import { StructuredAgentSessionHost } from '../structured-agent-session-host' @@ -24,10 +27,7 @@ import { hostTestOperationId, resetHostTestOperationIds } from './structured-agent-session-host-test-data.test-fixture' -import { - closeTestJournalHostDatabase, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { closeTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { seedQueuedRigLegacyJournal } from './structured-agent-session-queued-legacy-journal.test-fixture' import { createStructuredAgentSessionLogger } from '../structured-agent-session-logger' import { @@ -62,13 +62,14 @@ export async function createQueuedMessageTestRig( let store = await openTestAgentSessionRecordStore(root) const provider = createQueuedRigProvider(store, options, () => host) const { dispatch, compact, cancelTurn, closeSession, setOption } = provider + const journalDatabase = await openTestAgentSessionDatabase(root) const makeHost = () => new StructuredAgentSessionHost({ agents: claudeAndCodexDeclared(), logger: createStructuredAgentSessionLogger(), store, adapter: provider.adapter, - journalDatabase: openTestJournalHostDatabase(root), + journalDatabase, claimKeyId: 'key-1', mintSpawnToken: () => 'spawn-1', now: options.now ?? (() => NOW), @@ -165,15 +166,12 @@ export async function createQueuedMessageTestRig( } async function submission(id: string): Promise { - return (await host.journalSnapshot(SESSION)).submissions.find( - (entry) => entry.clientMessageId === id - ) + return host.collaboratorsForTests().sessions.get(SESSION)!.journal.reads.submission(id) } async function handoff(draftId: string): Promise { - return (await host.journalSnapshot(SESSION)).submissions.findLast( - (entry) => entry.queuedMessageId === draftId - ) + const journal = host.collaboratorsForTests().sessions.get(SESSION)!.journal + return (await journal.reads.input({ kind: 'linked-to', messageId: draftId })).submissions.at(-1) } /** Handoff submission IDs differ from their draft IDs. */ diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-messages.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-messages.test.ts index 954317b940e..9cc24d687f2 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-messages.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-messages.test.ts @@ -60,7 +60,7 @@ it('a steer send waiting on approval joins the running turn when approval clears expect(await rig.handoff(steer.id)).toBeUndefined() const itemId = agentJournalItemKey(identity) const journal = host.collaboratorsForTests().sessions.get(SESSION)!.journal - const prompt = journal.item(itemId)! + const prompt = (await journal.reads.item(itemId))! const fields = { itemId, expectedRevision: prompt.revision, optionId: 'allow' } expect( await host.respondToPrompt(CALLER, { diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-move.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-move.test.ts index ac4eedda6f1..f3edd6f10c8 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-move.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-move.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { createQueuedMessageTestRig, @@ -64,7 +65,7 @@ describe('authoritative queue ordering', () => { const mail = await queue('mail', true) const b = await queue('b') const c = await queue('c') - const facts = journal().queuedMessages.list() + const facts = await journal().queuedMessages.list() const op = hostTestOperationId() expect(await move(c, { beforeMessageId: a }, op)).toMatchObject({ ok: true, @@ -72,9 +73,9 @@ describe('authoritative queue ordering', () => { }) expect(order()).toEqual([c, a, mail, b]) for (const card of facts) { - expect(journal().queuedMessages.get(card.messageId)).toEqual({ + expect(await journal().queuedMessages.get(card.messageId)).toEqual({ ...card, - position: journal().queuedMessages.get(card.messageId)!.position + position: (await journal().queuedMessages.get(card.messageId))!.position }) } expect(await move(c, { afterMessageId: b })).toMatchObject({ @@ -118,8 +119,8 @@ describe('authoritative queue ordering', () => { value: { status: 'moved' } }) expect(order()).toEqual([mail, a, c, b]) - expect(journal().queuedMessages.get(mail)?.body).toEqual(facts[1]!.body) - expect(journal().queuedMessages.list()).toHaveLength(4) + expect((await journal().queuedMessages.get(mail))?.body).toEqual(facts[1]!.body) + expect(await journal().queuedMessages.list()).toHaveLength(4) expect(await rig.deleteQueued(c)).toMatchObject({ ok: true }) expect(await move(c, { beforeMessageId: null })).toMatchObject({ ok: true, @@ -134,15 +135,15 @@ describe('authoritative queue ordering', () => { const held = await queue('held mail', true) const b = await queue('b') await journal().queuedMessages.hold({ messageIds: [held], reason: 'send_failed' }) - const facts = journal().queuedMessages.list() + const facts = await journal().queuedMessages.list() await move(held, { beforeMessageId: a }) const op = hostTestOperationId() await move(mail, { beforeMessageId: a }, op) expect(order()).toEqual([held, mail, a, b]) for (const card of facts) { - expect(journal().queuedMessages.get(card.messageId)).toEqual({ + expect(await journal().queuedMessages.get(card.messageId)).toEqual({ ...card, - position: journal().queuedMessages.get(card.messageId)!.position + position: (await journal().queuedMessages.get(card.messageId))!.position }) } await rig.settleAccepted(working, 'work') @@ -167,14 +168,14 @@ describe('authoritative queue ordering', () => { await eventually(async () => expect(await rig.handoff(id)).toBeDefined()) await rig.settleAccepted(await rig.handoffId(id), turn) } - const snapshot = await rig.host.journalSnapshot(HOST_TEST_SESSION) + const snapshot = await readTestHostJournal(rig.host, HOST_TEST_SESSION) expect(snapshot.submissions.flatMap((entry) => entry.queuedMessageId ?? [])).toEqual([ mail, a, b ]) expect(order()).toEqual([held]) - expect(journal().queuedMessages.get(held)).toEqual({ + expect(await journal().queuedMessages.get(held)).toEqual({ ...facts.find((card) => card.messageId === held)!, position: facts[0]!.position }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-quit.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-quit.test.ts index 05ecb623f14..e80056cf47c 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-quit.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-quit.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { afterEach, expect, it, vi } from 'vitest' import { HOST_TEST_SESSION as SESSION } from './structured-agent-session-host-test-data.test-fixture' import { @@ -19,7 +20,7 @@ it.each([undefined, 'kill-delivered'] as const)( if (evidence) { expect(host.deps.store.getRecord(SESSION)?.lease.deathEvidence?.kind).toBe(evidence) } - const snapshot = await host.journalSnapshot(SESSION) + const snapshot = await readTestHostJournal(host, SESSION) expect(snapshot.submissions.filter((entry) => entry.queuedMessageId !== undefined)).toEqual([]) expect(await host.restartResume.list()).toHaveLength(1) const page = await host.history({ sessionId: SESSION, direction: 'tail' }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-repeat-press.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-repeat-press.test.ts index 2646a6b728d..b2d5a8e429e 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-repeat-press.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-repeat-press.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { createQueuedMessageTestRig, @@ -41,7 +42,7 @@ describe('a repeated draft press under a fresh operation id', () => { throw new Error('expected both presses answered') } expect(second.value.clientMessageId).toBe(first.value.clientMessageId) - const handoffs = (await rig.host.journalSnapshot(SESSION)).submissions.filter( + const handoffs = (await readTestHostJournal(rig.host, SESSION)).submissions.filter( (entry) => entry.queuedMessageId === draftId ) expect(handoffs).toHaveLength(1) @@ -82,7 +83,7 @@ describe('a queued Send receipt', () => { replayed: true, value: { submission: { queuedMessageId: id } } }) - const submissions = (await rig.host.journalSnapshot(SESSION)).submissions + const submissions = (await readTestHostJournal(rig.host, SESSION)).submissions expect(submissions.filter((entry) => entry.queuedMessageId === id)).toHaveLength(1) }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-stop-row.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-stop-row.test.ts index 6569b12c654..667f08e3ca3 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-stop-row.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-stop-row.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { afterEach, beforeEach, 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' @@ -73,14 +74,14 @@ function withdraw(clientMessageId: string, answeredInTurn?: AgentJournalAnswered } async function send(clientMessageId: string) { - return (await rig.host.journalSnapshot(HOST_TEST_SESSION)).submissions.find( + return (await readTestHostJournal(rig.host, HOST_TEST_SESSION)).submissions.find( (entry) => entry.clientMessageId === clientMessageId ) } /** How the shared projection every client runs draws this send, or null when it hides it. */ async function drawn(clientMessageId: string) { - const { items, submissions } = await rig.host.journalSnapshot(HOST_TEST_SESSION) + const { items, submissions } = await readTestHostJournal(rig.host, HOST_TEST_SESSION) const message = projectStructuredAgentSessionMessages(items, [], submissions).find( (entry) => entry.id === agentJournalSubmissionKey(clientMessageId) ) @@ -160,9 +161,11 @@ describe("Stop's event", () => { { messageId: opening, state: 'waiting' }, { messageId: last, state: 'waiting' } ]) - expect(journal().submissionBody(steer)).toEqual(hostTestMessage('sent now into the turn')) - expect(journal().queuedMessages.get(`recovery:${steer}`)).toBeNull() - expect(journal().submission(steer)?.options).toEqual({}) + expect(await journal().reads.submissionBody(steer)).toEqual( + hostTestMessage('sent now into the turn') + ) + expect(await journal().queuedMessages.get(`recovery:${steer}`)).toBeNull() + expect((await journal().reads.submission(steer))?.options).toEqual({}) // The turn's message, unconfirmed, as an old client's projection draws it too. expect(await send(steer)).toMatchObject({ dispatchState: 'unknown', recovered: true }) expect(await drawn(steer)).toEqual({ stoppedBeforeStart: undefined }) @@ -215,7 +218,9 @@ describe("Stop's event", () => { ]) const handoff = await rig.handoffId(sentId) expect(await rig.drafts()).toEqual([{ messageId: waiting, state: 'waiting' }]) - expect(journal().submissionBody(handoff)).toEqual(hostTestMessage('sent now into the turn')) + expect(await journal().reads.submissionBody(handoff)).toEqual( + hostTestMessage('sent now into the turn') + ) expect(await rig.deleteQueued(`recovery:${handoff}`)).toMatchObject({ value: { deleted: false } }) @@ -267,7 +272,7 @@ describe("a /clear's carried cards", () => { } await eventually(async () => expect( - (await rig.host.journalSnapshot(replacementId)).submissions.find( + (await readTestHostJournal(rig.host, replacementId)).submissions.find( (entry) => entry.clientMessageId === sent.value.clientMessageId )?.handedOverAt ).toBeDefined() @@ -282,7 +287,7 @@ describe("a /clear's carried cards", () => { ordinal: 0 } }) - expect(journal(replacementId).queuedMessages.list()[0]?.messageId).toBe(carried) + expect((await journal(replacementId).queuedMessages.list())[0]?.messageId).toBe(carried) expect(structuredQueuePauses(journal(replacementId))).toEqual([]) }) }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-withdrawn-draft.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-withdrawn-draft.test.ts index ef24c7357f3..0325d2a469a 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-withdrawn-draft.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-withdrawn-draft.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { afterEach, beforeEach, expect, it, vi } from 'vitest' import { JournalStopAcceptor } from '../../../agent-session-journal/context-epochs/journal-stop-acceptance' import { HOST_TEST_SESSION as SESSION } from './structured-agent-session-host-test-data.test-fixture' @@ -30,7 +31,9 @@ async function queuedDraft(text: string): Promise { } async function submissionIds(): Promise { - return (await rig.host.journalSnapshot(SESSION)).submissions.map((entry) => entry.clientMessageId) + return (await readTestHostJournal(rig.host, SESSION)).submissions.map( + (entry) => entry.clientMessageId + ) } async function handedOver(id: string): Promise { diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-resend-answer.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-resend-answer.test.ts index 6c7f15ceb13..89b503c4f7b 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-resend-answer.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-resend-answer.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalSubmissions } from '../../../agent-session-journal/journal-observation.test-support' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import type { AgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store' import type { AgentSessionJournal } from '../../../agent-session-journal/journal-store' @@ -58,7 +59,7 @@ describe('send receipt acceptance', () => { } }) expect(store.listOperationRows()).toEqual(before) - expect(hostJournal().submissions()).toHaveLength(0) + expect(await readTestJournalSubmissions(hostJournal())).toHaveLength(0) expect(store.readCommandReceipt({ kind: 'global' }, params.envelope.clientOperationId)).toEqual( { verdict: 'absent' } ) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-interruption.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-interruption.test-fixture.ts index de72d28b337..8e80b101ab3 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-interruption.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-interruption.test-fixture.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { AGENT_JOURNAL_THREAD_SCOPE, type AgentJournalRenderItem @@ -14,7 +15,10 @@ import { AGENT_SESSION_RECOVERY_CAPSULE_FILE } from '../../../../runtime/agent-session/records/agent-session-recovery-capsule' import type { AgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store' -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 { parseAgentSessionResumeMarker } from '../../../../../shared/agent-session/lifecycle/agent-session-resume-marker' import type { AgentChildWorkView } from '../../../../../shared/agent-status/child-work/agent-status-child-work-view' import { AgentSessionJournal } from '../../../agent-session-journal/journal-store' @@ -39,7 +43,6 @@ import { hostTestAttachParams, hostTestMessage } from './structured-agent-session-host-test-data.test-fixture' -import { openTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { recordingProductionStructuredAgentSessionLogger } from './structured-agent-session-logger.test-fixture' import { claudeAndCodexDeclared } from '../../providers/testing/structured-agent-session-adapter-router.test-fixture' import type { StructuredAgentRegistry } from '../../providers/structured-agent-registry' @@ -157,7 +160,7 @@ export async function interruptedRestart( logger: log.logger, store, adapter: { ...adapter(), closeSession }, - journalDatabase: openTestJournalHostDatabase(previous.root), + journalDatabase: await openTestAgentSessionDatabase(previous.root), claimKeyId: 'key-1', mintSpawnToken: () => 'spawn-next', probeOwner, @@ -211,12 +214,12 @@ export function continuationUnanswered(state: { } export async function statusNotes(host: StructuredAgentSessionHost) { - return notesOf((await host.journalSnapshot(SESSION)).items) + return notesOf((await readTestHostJournal(host, SESSION)).items) } export async function readerNotes(host: StructuredAgentSessionHost) { return notesOf( - withNativeChatCutTurnNotices((await host.journalSnapshot(SESSION)).items, { + withNativeChatCutTurnNotices((await readTestHostJournal(host, SESSION)).items, { agentName: 'Codex' }) ) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-offer-after-reattach.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-offer-after-reattach.test.ts index 2ee85074a93..2202a57b0c2 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-offer-after-reattach.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-offer-after-reattach.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { expect, it } from 'vitest' import { AgentSessionRecoveryCapsule } from '../../../../runtime/agent-session/records/agent-session-recovery-capsule' import { AGENT_SESSION_RESTART_CONTINUATION_MESSAGE } from '../../../../../shared/agent-session/lifecycle/agent-session-restart-continuation' @@ -16,7 +17,7 @@ it("delivers a retry as a new continuation after the first one's start failed", acquire.mockRejectedValueOnce(new Error('provider could not reconnect')) const first = await host.restartResume.continueAfterRestart([SESSION], 'modal') expect(first.failed).toMatchObject([{ sessionId: SESSION, outcome: 'refused', retryable: true }]) - const [rejected] = (await host.journalSnapshot(SESSION)).submissions + const [rejected] = (await readTestHostJournal(host, SESSION)).submissions expect(rejected).toMatchObject({ dispatchState: 'rejected' }) const retried = await host.restartResume.continueAfterRestart([SESSION], 'retry') @@ -26,7 +27,7 @@ it("delivers a retry as a new continuation after the first one's start failed", const sent = dispatch.mock.calls[0]?.[0] expect(sent?.clientMessageId).not.toBe(rejected?.clientMessageId) expect(sent?.body).toEqual(restartContinuationBody(marker)) - expect((await host.journalSnapshot(SESSION)).submissions).toMatchObject([ + expect((await readTestHostJournal(host, SESSION)).submissions).toMatchObject([ { clientMessageId: rejected?.clientMessageId, dispatchState: 'rejected' }, { clientMessageId: sent?.clientMessageId, dispatchState: 'accepted' } ]) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-teardown.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-teardown.test.ts index 45c11cef377..cd9c1fe29dd 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-teardown.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-teardown.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../shared/agent-session/journal/agent-session-journal-types' import { expect, it, vi } from 'vitest' import { AgentSessionRecoveryCapsule } from '../../../../runtime/agent-session/records/agent-session-recovery-capsule' @@ -52,7 +53,9 @@ it.each(['beginTeardown', 'captureBeforeStop', 'recordMarkers'] as const)( ) expect(inspect(log.entries, { depth: 8 })).not.toContain('/private/account') expect(store.getRecord(SESSION)?.lease.claimStatus).toBe('released') - await expect(host.journalSnapshot(SESSION)).rejects.toThrow('agent_session_ownership_unknown') + await expect(await readTestHostJournal(host, SESSION)).rejects.toThrow( + 'agent_session_ownership_unknown' + ) } finally { operation.mockRestore() warning.mockRestore() diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-replay.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-replay.test-fixture.ts index c09653dcde5..81a01f5c542 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-replay.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-replay.test-fixture.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../shared/agent-session/journal/agent-session-journal-types' import type { StructuredAgentSessionEventSink } from '../../journal-ingress/structured-agent-session-event-sink' import { expect, vi, type Mock } from 'vitest' @@ -68,8 +69,8 @@ export async function expectRetainedPrefixRewindReplay({ const request = await params(target) const result = await host.rewind(caller, request) expect(result).toMatchObject({ ok: true }) - expect((await host.journalSnapshot(HOST_TEST_SESSION)).items).toHaveLength(1) - expect((await host.journalSnapshot(HOST_TEST_SESSION)).cursor.epoch).not.toBe( + expect((await readTestHostJournal(host, HOST_TEST_SESSION)).items).toHaveLength(1) + expect((await readTestHostJournal(host, HOST_TEST_SESSION)).cursor.epoch).not.toBe( request.expectedEpoch ) expect(await host.rewind(caller, request)).toMatchObject({ ok: true, replayed: true }) @@ -95,7 +96,7 @@ export async function expectAtomicRewindCompletion({ throw new Error('completion write failed') }) await expect(host.rewind(caller, request)).rejects.toThrow('completion write failed') - const committed = await host.journalSnapshot(HOST_TEST_SESSION) + const committed = await readTestHostJournal(host, HOST_TEST_SESSION) expect(committed.cursor.epoch).not.toBe(request.expectedEpoch) expect(store.getRecord(HOST_TEST_SESSION)?.rewind?.phase).toBe('completed') checkpoint.mockRestore() @@ -103,7 +104,7 @@ export async function expectAtomicRewindCompletion({ expect( await attachAndStart(store.getRecord(HOST_TEST_SESSION)!.lease.runtimeFence) ).toMatchObject({ ok: true }) - expect(await host.journalSnapshot(HOST_TEST_SESSION)).toEqual(committed) + expect(await readTestHostJournal(host, HOST_TEST_SESSION)).toEqual(committed) expect(replace).not.toHaveBeenCalled() replace.mockRestore() expect(await host.rewind(caller, request)).toMatchObject({ ok: true, replayed: true }) @@ -142,7 +143,7 @@ export async function expectLargeRetainedPrefixRewind({ await host.rewind(caller, await params(agentJournalItemKey(message('drop')))) ).toMatchObject({ ok: true }) expect(rewind).toHaveBeenCalledWith(expect.objectContaining({ beforeTurnId: 'drop' })) - const { items } = await host.journalSnapshot(HOST_TEST_SESSION) + const { items } = await readTestHostJournal(host, HOST_TEST_SESSION) expect(items.map(({ itemId }) => itemId)).toEqual( Array.from({ length: 300 }, (_, turn) => agentJournalItemKey(message(`kept-${turn}`))) ) @@ -205,7 +206,7 @@ export async function expectGoalRowRewindRecovery({ ).toMatchObject({ ok: true }) expect( - (await host.journalSnapshot(HOST_TEST_SESSION)).items.map(({ itemId, body }) => ({ + (await readTestHostJournal(host, HOST_TEST_SESSION)).items.map(({ itemId, body }) => ({ itemId, body })) @@ -286,7 +287,7 @@ export async function expectGoalRowsThroughHydration({ }) expect( - (await host.journalSnapshot(HOST_TEST_SESSION)).items.map(({ itemId, body }) => ({ + (await readTestHostJournal(host, HOST_TEST_SESSION)).items.map(({ itemId, body }) => ({ itemId, body })) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-stop-note.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-stop-note.test-fixture.ts index 259ae64cf24..2ed59e0cf85 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-stop-note.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-stop-note.test-fixture.ts @@ -1,3 +1,4 @@ +import { readTestJournalItems } from '../../../agent-session-journal/journal-observation.test-support' import { expect, vi, type Mock } from 'vitest' import { AGENT_JOURNAL_THREAD_SCOPE, @@ -60,7 +61,9 @@ export async function expectRawStopNoteRewindRecovery({ turnScope: AGENT_JOURNAL_THREAD_SCOPE }) const target = agentJournalItemKey(targetIdentity) - expect(journal.snapshot().items.find((item) => item.itemId === noteId)?.body).toEqual({ + expect( + (await readTestJournalItems(journal)).find((item) => item.itemId === noteId)?.body + ).toEqual({ kind: 'status', text: 'Cancellation requested.' }) @@ -73,7 +76,7 @@ export async function expectRawStopNoteRewindRecovery({ await expect( host.rewind(caller, { itemId: target, - expectedEpoch: journal.epoch, + expectedEpoch: journal.physicalCursor().epoch, envelope: { sessionId: HOST_TEST_SESSION, clientOperationId: hostTestOperationId(), @@ -81,7 +84,7 @@ export async function expectRawStopNoteRewindRecovery({ payloadFingerprint: computeAgentSessionPayloadFingerprint({ method: 'agentSession.rewind', sessionId: HOST_TEST_SESSION, - fields: { itemId: target, expectedEpoch: journal.epoch } + fields: { itemId: target, expectedEpoch: journal.physicalCursor().epoch } }) } }) @@ -97,8 +100,10 @@ export async function expectRawStopNoteRewindRecovery({ // Rewind settlement runs in startup after attach; drain serialization before asserting. await host.collaboratorsForTests().serialize(HOST_TEST_SESSION, async () => {}) expect(store.getRecord(HOST_TEST_SESSION)?.rewind?.phase).toBe('completed') - expect(journal.itemBody(noteId)).toEqual(unconfirmed) - expect(journal.snapshot().items.find((item) => item.itemId === noteId)?.body).toEqual({ + expect(await journal.reads.itemBody(noteId)).toEqual(unconfirmed) + expect( + (await readTestJournalItems(journal)).find((item) => item.itemId === noteId)?.body + ).toEqual({ kind: 'status', text: 'Cancellation requested.' }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-usage-limit.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-usage-limit.test-fixture.ts index ce21767f003..df8064afaf2 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-usage-limit.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-usage-limit.test-fixture.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import type { StructuredAgentSessionAdapter } from '../../providers/structured-agent-session-adapter' import { openTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { openTestAgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' @@ -54,7 +55,7 @@ export async function expectRestoredUsageLimitCancelled( store: AgentSessionRecordStore, cut: { turnItemId: string; resetsAt: number } ) { - const restored = await host.journalSnapshot(HOST_TEST_SESSION) + const restored = await readTestHostJournal(host, HOST_TEST_SESSION) expect(latestNativeChatUsageLimitCut(restored.items, restored.submissions)).toMatchObject(cut) expect(store.getRecord(HOST_TEST_SESSION)?.usageLimitContinueCancel).toEqual(cut) expect(host.usageLimitContinue.readState(HOST_TEST_SESSION)?.state).toBe('cancelled') @@ -140,7 +141,7 @@ export async function expectUsageLimitCompletionRollback({ const journal = host.collaboratorsForTests().sessions.get(HOST_TEST_SESSION)!.journal const cut = await appendRewindUsageLimitedTurn(journal, now()) const request = await params(target) - const before = await host.journalSnapshot(HOST_TEST_SESSION) + const before = await readTestHostJournal(host, HOST_TEST_SESSION) const physical = journal.physicalCursor().epoch const db = openTestJournalHostDatabase(directory).db db.exec(`CREATE TEMP TRIGGER fail_rewind_completion BEFORE UPDATE ON agent_session_records @@ -149,7 +150,7 @@ export async function expectUsageLimitCompletionRollback({ await expect(host.rewind({ callerKey: 'desktop' }, request)).rejects.toThrow( 'completion write failed' ) - const committed = await host.journalSnapshot(HOST_TEST_SESSION) + const committed = await readTestHostJournal(host, HOST_TEST_SESSION) expect(committed).toEqual(before) expect(committed.cursor.epoch).toBe(request.expectedEpoch) expect(journal.physicalCursor().epoch).toBe(physical) @@ -160,7 +161,7 @@ export async function expectUsageLimitCompletionRollback({ expect( await attachAndStart(store.getRecord(HOST_TEST_SESSION)!.lease.runtimeFence) ).toMatchObject({ ok: true }) - const restored = await host.journalSnapshot(HOST_TEST_SESSION) + const restored = await readTestHostJournal(host, HOST_TEST_SESSION) expect(restored.cursor.epoch).not.toBe(request.expectedEpoch) expect(journal.physicalCursor().epoch).toBe(physical) expect(restored.items.map(({ itemId }) => itemId)).toEqual([ diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-running-operation-order.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-running-operation-order.test.ts index 2389d31b3ec..1035ec17a22 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-running-operation-order.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-running-operation-order.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { expect, it, vi } from 'vitest' import { CALLER, @@ -35,7 +36,7 @@ it.each(['goal', 'rewind'] as const)( }) ).toMatchObject({ ok: true }) const change = { kind: 'set' as const, objective: 'A goal during compact' } - const expectedEpoch = (await host.journalSnapshot(SESSION)).cursor.epoch + const expectedEpoch = (await readTestHostJournal(host, SESSION)).cursor.epoch const itemId = 'codex:thread-1:turn-1:1' const result = operation === 'goal' @@ -57,7 +58,7 @@ it.each(['goal', 'rewind'] as const)( operation === 'goal' ? { ok: true } : { ok: false, refusal: { rewindReason: 'busy' } } ) expect(dispatch).not.toHaveBeenCalled() - const row = host + const row = await host .collaboratorsForTests() .sessions.get(SESSION)! .journal.queuedMessages.get(queuedEnvelope.clientOperationId) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-send-open-stale-turn.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-send-open-stale-turn.test.ts index f3c189cdf9f..dc1b25ca19b 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-send-open-stale-turn.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-send-open-stale-turn.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { openTestChats } from './structured-agent-session-open-test-chats.test-fixture' import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../shared/agent-session/journal/agent-session-journal-types' @@ -5,7 +6,10 @@ import { cp, rm } from 'node:fs/promises' import { afterEach, expect, it, vi } from 'vitest' import type { AgentSessionOwnerProbe } from '../../../../../shared/agent-session/lifecycle/agent-session-lease-adjudication' import { readAgentJournalTurn } from '../../../../../shared/agent-session/lifecycle/agent-session-turn-record' -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 { StructuredAgentSessionHost } from '../structured-agent-session-host' import { adapter, @@ -18,10 +22,7 @@ import { HOST_TEST_SESSION as SESSION, HOST_TEST_THREAD as THREAD } from './structured-agent-session-host-test-data.test-fixture' -import { - closeTestJournalHostDatabases, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { closeTestJournalHostDatabases } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { createStructuredAgentSessionLogger } from '../structured-agent-session-logger' import { NO_STRUCTURED_AGENTS } from '../../providers/testing/structured-agent-session-adapter-router.test-fixture' @@ -67,7 +68,7 @@ async function relaunchAfterCrashMidTurn( logger: createStructuredAgentSessionLogger(), store, adapter: { ...adapter(), acquire }, - journalDatabase: openTestJournalHostDatabase(relaunched), + journalDatabase: await openTestAgentSessionDatabase(relaunched), claimKeyId: 'key-1', mintSpawnToken: () => 'spawn-next', probeOwner: async () => probe, @@ -81,7 +82,7 @@ async function relaunchAfterCrashMidTurn( } async function turnStates(host: StructuredAgentSessionHost) { - return (await host.journalSnapshot(SESSION)).items + return (await readTestHostJournal(host, SESSION)).items .flatMap((item) => readAgentJournalTurn(item.body) ?? []) .map((turn) => turn.state) } diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stale-turn-open.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stale-turn-open.test.ts index 2407b5bcb3c..a9d712373b7 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stale-turn-open.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stale-turn-open.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { openTestChats } from './structured-agent-session-open-test-chats.test-fixture' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../shared/agent-session/journal/agent-session-journal-types' @@ -80,7 +81,7 @@ async function exitWithUnwrittenSettlement( } async function workingTurnState(): Promise { - const items = (await rig.host.journalSnapshot(SESSION)).items + const items = (await readTestHostJournal(rig.host, SESSION)).items return items .map((item) => readAgentJournalTurn(item.body)) .find((turn) => turn?.turnId === 'working')?.state @@ -92,7 +93,7 @@ describe('a turn its gone agent left running', () => { expect(rig.host.collaboratorsForTests().sessions.has(SESSION)).toBe(true) expect(await workingTurnState()).toBe('interrupted') - const { items } = await rig.host.journalSnapshot(SESSION) + const { items } = await readTestHostJournal(rig.host, SESSION) expect(JSON.stringify(items)).toContain( 'Codex stopped while this response was in progress. You can continue in this conversation.' ) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-start-lifecycle-perf.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-start-lifecycle-perf.test.ts index d7a32a61fab..fc761a851f6 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-start-lifecycle-perf.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-start-lifecycle-perf.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { getEventListeners } from 'node:events' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' @@ -19,11 +20,11 @@ import { fakeCodex, THREAD_ID } from '../../../../codex/structured/session-lifecycle/codex-structured-session-adapter-fixture' -import { openTestAgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { - closeTestJournalHostDatabases, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' + openTestAgentSessionRecordStore, + openTestAgentSessionDatabase +} from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' +import { closeTestJournalHostDatabases } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { claudeAndCodexAgents, claudeAndCodexRouter @@ -154,7 +155,7 @@ async function runStarts(count: number) { logger: createStructuredAgentSessionLogger(), store, adapter: router, - journalDatabase: openTestJournalHostDatabase(join(root, `starts-${count}`)), + journalDatabase: await openTestAgentSessionDatabase(join(root, `starts-${count}`)), claimKeyId: 'key-1', mintSpawnToken: () => `spawn-${codex.connections.length}`, startupLimits: { ceilingMs: CEILING_MS }, @@ -197,9 +198,11 @@ async function runStarts(count: number) { vi.waitFor(assertion, { timeout: 10_000, interval: 0 }) const submission = async (id: string) => - (await host!.journalSnapshot(SESSION)).submissions.find((row) => row.clientMessageId === id) + (await readTestHostJournal(host!, SESSION)).submissions.find( + (row) => row.clientMessageId === id + ) const failureRows = async () => - (await host!.journalSnapshot(SESSION)).items.filter( + (await readTestHostJournal(host!, SESSION)).items.filter( (item) => item.body.kind === 'status' && item.body.tone === 'error' ).length const phase = () => host!.collaboratorsForTests().sessions.get(SESSION)?.child?.phase diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-startup-attempt-host.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-startup-attempt-host.test-fixture.ts index 9ce7a7c0e68..af4c87c99d8 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-startup-attempt-host.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-startup-attempt-host.test-fixture.ts @@ -16,11 +16,11 @@ import { afterEach, beforeEach, expect, vi, type Mock } from 'vitest' import type { AgentJournalSubmission } from '../../../../../shared/agent-session/journal/agent-session-journal-types' import { codexProviderHandle } from '../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import type { AgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store' -import { openTestAgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { - closeTestJournalHostDatabases, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' + openTestAgentSessionRecordStore, + openTestAgentSessionDatabase +} from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' +import { closeTestJournalHostDatabases } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import type { AgentSessionAcquisition, StructuredAgentSessionAcquireInput, @@ -138,7 +138,7 @@ async function startHost({ startupLimits, supportsGoals }: StartupHostOptions = stop: vi.fn(), providerStarted }, - journalDatabase: openTestJournalHostDatabase(root), + journalDatabase: await openTestAgentSessionDatabase(root), claimKeyId: 'key-1', mintSpawnToken: () => `spawn-${acquire.mock.calls.length}`, idleSweep: { intervalMs: 60 * 60_000, idleMs: () => 60 * 60_000 }, diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event-entries.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event-entries.test.ts index 769747c9425..5207abe3adb 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event-entries.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event-entries.test.ts @@ -1,3 +1,7 @@ +import { + readTestHostJournal, + readTestJournalItems +} from '../../../agent-session-journal/journal-observation.test-support' import { afterEach, expect, it } from 'vitest' import { assertTestJournalHistoricalInputs, @@ -91,9 +95,9 @@ it('keeps Resume visible beside an idle-evicted unknown send recovery', async () expect(stopEvents()).toEqual(before) expect(await rig.queuePause()).toEqual({ reason: 'stopped' }) - expect(journal().submissionBody(id)).toEqual(fields.body) - expect(journal().queuedMessages.get(`recovery:${id}`)).toBeNull() - expect(journal().itemBody(`orca:${id}`)).toEqual(fields.body) + expect(await journal().reads.submissionBody(id)).toEqual(fields.body) + expect(await journal().queuedMessages.get(`recovery:${id}`)).toBeNull() + expect(await journal().reads.itemBody(`orca:${id}`)).toEqual(fields.body) expect(structuredQueuePauses(journal()).map((pause) => pause.reason)).toContain('stopped') expect(await rig.drafts()).toEqual([{ messageId: held, state: 'waiting' }]) expect(await rig.handoff(held)).toBeUndefined() @@ -128,13 +132,13 @@ it('a failed personal start retains Stop after its returned input is deleted', a await personal.result expect(stopEvents().map((event) => event.reason)).toEqual(['user-stop', 'host-stop']) - expect(journal().queuedMessages.userStopInForce()?.event.reason).toBe('user-stop') + expect((await journal().queuedMessages.userStopInForce())?.event.reason).toBe('user-stop') await eventually(async () => expect(await rig.drafts()).toContainEqual({ messageId: personal.id, state: 'returned' }) ) expect(await rig.handoff(held)).toBeUndefined() expect(await rig.deleteQueued(personal.id)).toMatchObject({ value: { deleted: true } }) - expect(journal().queuedMessages.userStopInForce()?.event.reason).toBe('user-stop') + expect((await journal().queuedMessages.userStopInForce())?.event.reason).toBe('user-stop') expect(await rig.handoff(held)).toBeUndefined() expect(await rig.resume()).toMatchObject({ value: { resumed: true } }) await eventually(async () => expect(await rig.handoff(held)).toBeDefined()) @@ -172,7 +176,7 @@ it('an unattended failed start preserves a manual Stop without returning the mai await rig.host.collaboratorsForTests().lifetime.idleSweep.tick() await mail.result - expect(journal().queuedMessages.get(mail.id)).toBeNull() + expect(await journal().queuedMessages.get(mail.id)).toBeNull() expect(await rig.queuePause()).toEqual({ reason: 'stopped' }) expect(await rig.handoff(held)).toBeUndefined() }) @@ -197,7 +201,7 @@ it.each(['host-stop', 'user-close', 'user-close-before-exit'] as const)( position: 1, queuedAt: journal().physicalCursor() }) - expect(journal().queuedMessages.get(held)?.unattended).toBeUndefined() + expect((await journal().queuedMessages.get(held))?.unattended).toBeUndefined() expect(await rig.stop()).toMatchObject({ ok: true }) finishTestJournalHistoricalInputs(journal()) expect(await rig.queuePause()).toEqual({ reason: 'stopped' }) @@ -231,9 +235,8 @@ it.each(['host-stop', 'user-close', 'user-close-before-exit'] as const)( await expect(rig.host.close(SESSION, 'user-close')).rejects.toThrow() expect(oldSession.lastEndedChild).toBe(previousEnd) expect( - oldSession.journal - .snapshot() - .items.flatMap((item) => readAgentJournalTurn(item.body) ?? []) + (await readTestJournalItems(oldSession.journal)) + .flatMap((item) => readAgentJournalTurn(item.body) ?? []) .find((turn) => turn.turnId === `turn-of-${mail.id}`) ).toMatchObject({ state: 'running' }) await oldSession.journal.close() @@ -248,7 +251,7 @@ it.each(['host-stop', 'user-close', 'user-close-before-exit'] as const)( collab.lifetime.stopAgent(SESSION, { cause: 'host-stop' }) ) } - const afterExit = await rig.host.journalSnapshot(SESSION) + const afterExit = await readTestHostJournal(rig.host, SESSION) const turn = afterExit.items .flatMap((item) => readAgentJournalTurn(item.body) ?? []) .find((entry) => entry.turnId === `turn-of-${mail.id}`) @@ -256,7 +259,7 @@ it.each(['host-stop', 'user-close', 'user-close-before-exit'] as const)( const offersAfterExit = await rig.restartOffers() await rig.host.close(SESSION, 'evict') rig.crashRestartHostProcess() - await rig.host.journalSnapshot(SESSION) + await readTestHostJournal(rig.host, SESSION) const mail2 = rig.send('mail after reopen', undefined, mailOptions) expect(await mail2.result).toMatchObject({ ok: true }) await eventually(async () => @@ -282,7 +285,7 @@ it.each(['host-stop', 'user-close', 'user-close-before-exit'] as const)( expect(pauseAfterMail).toEqual({ reason: 'stopped' }) expect(leaked).toBeUndefined() expect( - (await rig.host.journalSnapshot(SESSION)).submissions.filter( + (await readTestHostJournal(rig.host, SESSION)).submissions.filter( (entry) => entry.queuedMessageId === held ) ).toHaveLength(1) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event.test.ts index 1b844a0b1bc..99071a174fd 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalItems } from '../../../agent-session-journal/journal-observation.test-support' import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../shared/agent-session/journal/agent-session-journal-types' import { agentJournalSubmissionKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' @@ -77,11 +78,9 @@ describe("a Stop's event", () => { await stopped } expect( - journal() - .snapshot() - .items.filter( - (item) => item.body.kind === 'status' && item.body.text === 'Cancellation requested.' - ) + (await readTestJournalItems(journal())).filter( + (item) => item.body.kind === 'status' && item.body.text === 'Cancellation requested.' + ) ).toHaveLength(1) }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-terminal-writer-acquire.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-terminal-writer-acquire.test.ts index 8cbba3c42e0..e28107f6c20 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-terminal-writer-acquire.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-terminal-writer-acquire.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -7,11 +8,11 @@ import { agentSessionRefusalError } from '../../../../../shared/agent-session/fa import { codexProviderHandle } from '../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import type { AgentSessionRecord } from '../../../../../shared/agent-session/lifecycle/agent-session-record' import type { AgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store' -import { openTestAgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { - closeTestJournalHostDatabase, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' + openTestAgentSessionRecordStore, + openTestAgentSessionDatabase +} from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' +import { closeTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import type { StructuredAgentSessionAdapter } from '../../providers/structured-agent-session-adapter' import { NO_STRUCTURED_AGENTS, @@ -91,7 +92,7 @@ beforeEach(async () => { setOption: vi.fn(async () => undefined), open: async () => ({ reportedOptions: { model: '' } }) }, - journalDatabase: openTestJournalHostDatabase(root), + journalDatabase: await openTestAgentSessionDatabase(root), claimKeyId: 'key-1', mintSpawnToken: () => `spawn-${acquire.mock.calls.length}`, assertNoTerminalWriter, @@ -126,12 +127,15 @@ async function rejection(id: string, sessionId = SESSION) { await vi.waitFor( async () => expect( - (await host.journalSnapshot(sessionId)).submissions.find((s) => s.clientMessageId === id) - ?.dispatchState + (await readTestHostJournal(host, sessionId)).submissions.find( + (s) => s.clientMessageId === id + )?.dispatchState ).toBe('rejected'), { timeout: 10_000 } ) - return (await host.journalSnapshot(sessionId)).submissions.find((s) => s.clientMessageId === id) + return (await readTestHostJournal(host, sessionId)).submissions.find( + (s) => s.clientMessageId === id + ) } // H10: a chat reopened while its conversation runs in a terminal starts no second writer. diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-unrun-send-hold.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-unrun-send-hold.test.ts index ce8a1bc49b8..8eeea3996e0 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-unrun-send-hold.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-unrun-send-hold.test.ts @@ -58,7 +58,10 @@ describe('a message held behind a start that never answered, then the chat ends' }) expect(await rig.drafts()).toEqual([{ messageId: id, ...KEPT }]) const [card] = - rig.host.collaboratorsForTests().sessions.get(SESSION)?.journal.queuedMessages.list() ?? [] + (await rig.host + .collaboratorsForTests() + .sessions.get(SESSION) + ?.journal.queuedMessages.list()) ?? [] expect(card).toMatchObject({ holdReason: null }) expect(derivedPauses()).toEqual(['restarted']) expect(await rig.restartOffers()).toEqual([]) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-usage-limit-continue.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-usage-limit-continue.test.ts index 380c465bc98..a3ccac48ac0 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-usage-limit-continue.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-usage-limit-continue.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { afterEach, expect, it } from 'vitest' import { createQueuedMessageTestRig, @@ -31,7 +32,7 @@ it('automatically continues a failed usage-limited turn after its reset instead await current.host.usageLimitContinue.tick() let continuationId: string | undefined await eventually(async () => { - const snapshot = await current.host.journalSnapshot(SESSION) + const snapshot = await readTestHostJournal(current.host, SESSION) continuationId = snapshot.submissions.find( (submission) => submission.handedOverAt !== undefined )?.clientMessageId diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-conversation-command-child-work.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-conversation-command-child-work.test.ts index b028e20c58e..f85821cf9db 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-conversation-command-child-work.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-conversation-command-child-work.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest' import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../shared/agent-session/journal/agent-session-journal-types' import type { AgentSessionBackgroundTaskState } from '../../../../../shared/agent-session/agent-session-background-task-wire' @@ -69,7 +70,7 @@ function finish(): void { async function commandTurnState(clientMessageId: string): Promise { const { itemId } = structuredAgentSessionCommandTurn(clientMessageId) - const item = (await state.host.journalSnapshot(HOST_TEST_SESSION)).items.find( + const item = (await readTestHostJournal(state.host, HOST_TEST_SESSION)).items.find( (entry) => entry.itemId === itemId ) return item?.body.kind === 'turn' ? item.body.state : undefined @@ -166,9 +167,9 @@ describe('conversation command admission reads the work the provider reported', expect(await state.host.conversationCommand(CALLER, params)).toMatchObject({ ok: true }) await vi.waitFor(async () => { - const submission = (await state.host.journalSnapshot(HOST_TEST_SESSION)).submissions.find( - (entry) => entry.clientMessageId === params.envelope.clientOperationId - ) + const submission = ( + await readTestHostJournal(state.host, HOST_TEST_SESSION) + ).submissions.find((entry) => entry.clientMessageId === params.envelope.clientOperationId) expect(submission?.dispatchState).toBe('rejected') }) expect(state.acquire).toHaveBeenCalledTimes(2) @@ -183,7 +184,7 @@ it('keeps a direct failed-start command terminal until an explicit fresh rerun', const command = compactParams() await state.host.conversationCommand(CALLER, command) await vi.waitFor(async () => { - const snapshot = await state.host.journalSnapshot(HOST_TEST_SESSION) + const snapshot = await readTestHostJournal(state.host, HOST_TEST_SESSION) expect( snapshot.submissions.find( (entry) => entry.clientMessageId === command.envelope.clientOperationId @@ -202,7 +203,9 @@ it('keeps a direct failed-start command terminal until an explicit fresh rerun', }) ).toMatchObject({ ok: true }) await vi.waitFor(async () => - expect((await state.host.journalSnapshot(HOST_TEST_SESSION)).submissions.at(-1)).toMatchObject({ + expect( + (await readTestHostJournal(state.host, HOST_TEST_SESSION)).submissions.at(-1) + ).toMatchObject({ dispatchState: 'accepted' }) ) @@ -218,7 +221,7 @@ it('keeps a direct failed-start command terminal until an explicit fresh rerun', const uncertain = compactParams() expect(await state.host.conversationCommand(CALLER, uncertain)).toMatchObject({ ok: true }) await vi.waitFor(async () => { - const snapshot = await state.host.journalSnapshot(HOST_TEST_SESSION) + const snapshot = await readTestHostJournal(state.host, HOST_TEST_SESSION) expect( snapshot.submissions.find( (entry) => entry.clientMessageId === uncertain.envelope.clientOperationId @@ -234,7 +237,9 @@ it('keeps a direct failed-start command terminal until an explicit fresh rerun', }) ).toMatchObject({ ok: true }) await vi.waitFor(async () => - expect((await state.host.journalSnapshot(HOST_TEST_SESSION)).submissions.at(-1)).toMatchObject({ + expect( + (await readTestHostJournal(state.host, HOST_TEST_SESSION)).submissions.at(-1) + ).toMatchObject({ dispatchState: 'accepted' }) ) @@ -244,7 +249,7 @@ it('keeps a direct failed-start command terminal until an explicit fresh rerun', await state.host.collaboratorsForTests().serialize(HOST_TEST_SESSION, async () => {}) expect(compact).toHaveBeenCalledTimes(1) expect( - (await state.host.journalSnapshot(HOST_TEST_SESSION)).submissions.find( + (await readTestHostJournal(state.host, HOST_TEST_SESSION)).submissions.find( (entry) => entry.clientMessageId === uncertain.envelope.clientOperationId ) ).toMatchObject({ dispatchState: 'unknown' }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-conversation-command.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-conversation-command.test.ts index b3229ba3448..02dc8efdfeb 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-conversation-command.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-conversation-command.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -6,7 +7,10 @@ import { computeAgentSessionPayloadFingerprint } from '../../../../../shared/age import type { AgentSessionConversationCommand } from '../../../../../shared/agent-session/agent-session-conversation-command' import type { AgentSessionOwnerProbe } from '../../../../../shared/agent-session/lifecycle/agent-session-lease-adjudication' import type { AgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store' -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 { StructuredAgentSessionHost } from '../structured-agent-session-host' import { claudeAndCodexDeclared } from '../../providers/testing/structured-agent-session-adapter-router.test-fixture' import type { StructuredAgentSessionAdapter } from '../../providers/structured-agent-session-adapter' @@ -63,7 +67,7 @@ async function openHost(): Promise { logger: createStructuredAgentSessionLogger(), store, adapter, - journalDatabase: openTestJournalHostDatabase(generationRoot()), + journalDatabase: await openTestAgentSessionDatabase(generationRoot()), claimKeyId: 'key', now: () => clock, mintSpawnToken: () => `spawn-${acquisitions}`, @@ -138,7 +142,7 @@ afterEach(async () => { await rm(directory, { recursive: true, force: true }) }) const clearRows = async () => - (await host.journalSnapshot(HOST_TEST_SESSION)).items.filter( + (await readTestHostJournal(host, HOST_TEST_SESSION)).items.filter( (item) => item.body.kind === 'status' && item.body.contextClear ) @@ -160,10 +164,12 @@ describe('same-conversation clear', () => { "CREATE TEMP TRIGGER fail_clear BEFORE UPDATE ON agent_session_records WHEN instr(NEW.record_json, 'providerContextBoundary') > 0 BEGIN SELECT RAISE(ABORT, 'disk full'); END" ) const params = commandParams('clear') - const before = await host.journalSnapshot(HOST_TEST_SESSION) + const before = await readTestHostJournal(host, HOST_TEST_SESSION) await expect(host.conversationCommand(caller, params)).rejects.toThrow('disk full') expect(await clearRows()).toHaveLength(0) - expect((await host.journalSnapshot(HOST_TEST_SESSION)).cursor.epoch).toBe(before.cursor.epoch) + expect((await readTestHostJournal(host, HOST_TEST_SESSION)).cursor.epoch).toBe( + before.cursor.epoch + ) expect(store.getRecord(HOST_TEST_SESSION)!.providerContextBoundary).toBeUndefined() expect( store.getOperationRow(caller.callerKey, params.envelope.clientOperationId)?.outcome.status diff --git a/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-received-output.test.ts b/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-received-output.test.ts index 0d5761f521a..ddf508378c3 100644 --- a/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-received-output.test.ts +++ b/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-received-output.test.ts @@ -1,3 +1,5 @@ +import { readTestJournalItems } from '../../../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' @@ -5,10 +7,7 @@ import { expect, it, vi } from 'vitest' import { createClaudeJournalTranslator } from '../../../../claude/structured/journal/claude-structured-journal-translation' import { claudeProviderHandle } from '../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import { openAgentSessionJournal } from '../../../agent-session-journal/journal-store-factory' -import { - closeTestJournalHostDatabase, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { closeTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { createDeferredStructuredAgentSessionEventSink } from '../structured-agent-session-event-sink' import { testEventSinkLogging } from '../../host/testing/structured-agent-session-logger.test-fixture' @@ -24,7 +23,7 @@ it('saves every tool result past 1,024 in one received Claude message, through r } let journal = await openAgentSessionJournal({ identity, - database: openTestJournalHostDatabase(root) + database: await openTestAgentSessionDatabase(root) }) const onFailed = vi.fn() const readingControl = { pauseReading: vi.fn(), resumeReading: vi.fn() } @@ -62,9 +61,11 @@ it('saves every tool result past 1,024 in one received Claude message, through r await closeTestJournalHostDatabase(root) journal = await openAgentSessionJournal({ identity, - database: openTestJournalHostDatabase(root) + database: await openTestAgentSessionDatabase(root) }) - const tools = journal.snapshot().items.filter((item) => item.body.kind === 'tool-call') + const tools = (await readTestJournalItems(journal)).filter( + (item) => item.body.kind === 'tool-call' + ) expect(tools).toHaveLength(count) expect(new Set(tools.map((item) => item.itemId)).size).toBe(count) tools.forEach((item, index) => diff --git a/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-resolved-append.test.ts b/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-resolved-append.test.ts index 6085ffd07ef..641126eec4e 100644 --- a/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-resolved-append.test.ts +++ b/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-resolved-append.test.ts @@ -1,3 +1,4 @@ +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' @@ -9,10 +10,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { agentJournalItemKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' import type { AgentJournalItemIdentity } from '../../../../../shared/agent-session/journal/agent-session-journal-identity' import type { AgentSessionJournal } from '../../../agent-session-journal/journal-store' -import { - closeTestJournalHostDatabases, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { closeTestJournalHostDatabases } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { openAgentSessionJournal } from '../../../agent-session-journal/journal-store-factory' import { createDeferredStructuredAgentSessionEventSink } from '../structured-agent-session-event-sink' @@ -35,7 +33,7 @@ let journal: AgentSessionJournal beforeEach(async () => { root = await mkdtemp(join(tmpdir(), 'orca-resolved-append-')) journal = await openAgentSessionJournal({ - database: openTestJournalHostDatabase(root), + database: await openTestAgentSessionDatabase(root), identity: { sessionId: 'session-1', workspaceId: 'workspace-1', diff --git a/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-transition-rollback.test.ts b/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-transition-rollback.test.ts index ba134099c44..67a1987ed3f 100644 --- a/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-transition-rollback.test.ts +++ b/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-transition-rollback.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalItems } from '../../../agent-session-journal/journal-observation.test-support' import { expect, it } from 'vitest' import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../shared/agent-session/journal/agent-session-journal-types' import { openTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' @@ -38,8 +39,8 @@ BEGIN SELECT RAISE(ABORT, 'second chunk refused'); END`) await expect(deferred.drained()).resolves.toMatchObject({ ok: false }) expect(journal.cursor().sequence).toBe(before) expect( - journal - .snapshot() - .items.filter((item) => item.body.kind === 'tool-call' && item.body.state === 'failed') + (await readTestJournalItems(journal)).filter( + (item) => item.body.kind === 'tool-call' && item.body.state === 'failed' + ) ).toEqual([]) }) diff --git a/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-transition.test.ts b/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-transition.test.ts index 44d74e5932e..1bcbf153402 100644 --- a/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-transition.test.ts +++ b/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-transition.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalItems } from '../../../agent-session-journal/journal-observation.test-support' import { expect, it } from 'vitest' import { agentJournalItemKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../shared/agent-session/journal/agent-session-journal-types' @@ -36,7 +37,7 @@ it('lands its steps back to back, each resolved after the one before it', async }) await deferred.drained() - const items = journal.snapshot().items + const items = await readTestJournalItems(journal) expect(items.map((item) => item.itemId)).toEqual([ agentJournalItemKey(turn), agentJournalItemKey(identity('later')) @@ -62,7 +63,7 @@ BEGIN SELECT RAISE(ABORT, 'second step refused'); END`) }) await expect(deferred.drained()).resolves.toMatchObject({ ok: false }) - expect(journal.snapshot().items.map((item) => item.itemId)).toEqual([ + expect((await readTestJournalItems(journal)).map((item) => item.itemId)).toEqual([ agentJournalItemKey(identity('first')) ]) }) diff --git a/src/main/native-chat/agent-session-wire/observations/testing/structured-agent-session-resumed-child-owner.test.ts b/src/main/native-chat/agent-session-wire/observations/testing/structured-agent-session-resumed-child-owner.test.ts index 7e40a0ce982..7da98d34509 100644 --- a/src/main/native-chat/agent-session-wire/observations/testing/structured-agent-session-resumed-child-owner.test.ts +++ b/src/main/native-chat/agent-session-wire/observations/testing/structured-agent-session-resumed-child-owner.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalItems } from '../../../agent-session-journal/journal-observation.test-support' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -119,7 +120,7 @@ it('keeps a resumed subagent and background task working when the run that creat await journal.replaceEpochItems( 'handle_forked', 2, - journal.snapshot().items.map((item) => { + (await readTestJournalItems(journal)).map((item) => { const { ownerFence, executionOwner } = journal.itemProvenance(item.itemId) return { identity: parseAgentJournalItemKey(item.itemId)!, @@ -131,9 +132,9 @@ it('keeps a resumed subagent and background task working when the run that creat ) await settleRunOne() - const blocks = journal - .snapshot() - .items.flatMap((item) => (item.body.kind === 'message' ? item.body.blocks : [])) + const blocks = (await readTestJournalItems(journal)).flatMap((item) => + item.body.kind === 'message' ? item.body.blocks : [] + ) expect(blocks.filter(isSubagentBlock).map((block) => block.state)).toEqual(['working']) expect(blocks.filter(isBackgroundTaskBlock).map((block) => block.state)).toEqual(['working']) }) diff --git a/src/main/native-chat/agent-session-wire/operations/testing/structured-agent-session-thread-goal.test.ts b/src/main/native-chat/agent-session-wire/operations/testing/structured-agent-session-thread-goal.test.ts index 17598be36fa..cecc427d9b2 100644 --- a/src/main/native-chat/agent-session-wire/operations/testing/structured-agent-session-thread-goal.test.ts +++ b/src/main/native-chat/agent-session-wire/operations/testing/structured-agent-session-thread-goal.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalItems } from '../../../agent-session-journal/journal-observation.test-support' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -82,7 +83,7 @@ describe('performThreadGoalChange', () => { message: 'goals feature is disabled' } }) - expect(journal.snapshot().items).toEqual([]) + expect(await readTestJournalItems(journal)).toEqual([]) }) it('retains the objective when a lost reply leaves application unknown', async () => { @@ -99,7 +100,7 @@ describe('performThreadGoalChange', () => { change: { kind: 'set', objective: 'Ship the parser' } }) ).rejects.toThrow('connection closed') - expect(journal.snapshot().items.map((item) => item.body)).toEqual([ + expect((await readTestJournalItems(journal)).map((item) => item.body)).toEqual([ expect.objectContaining({ sentAs: 'goal' }) ]) }) diff --git a/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-history-forward-read-budget.test.ts b/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-history-forward-read-budget.test.ts index 07d3a6eb193..46d2faa1680 100644 --- a/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-history-forward-read-budget.test.ts +++ b/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-history-forward-read-budget.test.ts @@ -80,7 +80,7 @@ describe('forward history SQL read budget', () => { const first = readAgentSessionHistory(journal, { sessionId: identity.sessionId, direction: 'after', - cursor: { epoch: journal.epoch, sequence: 1 }, + cursor: { epoch: journal.physicalCursor().epoch, sequence: 1 }, limit: 2 }) expect(first).toMatchObject({ ok: true, page: { hasNewer: true } }) @@ -106,7 +106,7 @@ describe('forward history SQL read budget', () => { const page = readAgentSessionHistory(journal, { sessionId: identity.sessionId, direction: 'after', - cursor: { epoch: journal.epoch, sequence: 1 }, + cursor: { epoch: journal.physicalCursor().epoch, sequence: 1 }, limit: 2 }) expect(page).toMatchObject({ diff --git a/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-stop-note-contract.test-fixture.ts b/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-stop-note-contract.test-fixture.ts index 8f37f733032..8169de443d0 100644 --- a/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-stop-note-contract.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-stop-note-contract.test-fixture.ts @@ -79,7 +79,7 @@ export function readNote() { } export function rows() { - const result = journal.readSince({ epoch: journal.epoch, sequence: 0 }) + const result = journal.readSince({ epoch: journal.physicalCursor().epoch, sequence: 0 }) if (!result.ok) { throw new Error(result.reset) } diff --git a/src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-subscriber-oversized-removal.test.ts b/src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-subscriber-oversized-removal.test.ts index 8b2e05d6f3a..e95059f1786 100644 --- a/src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-subscriber-oversized-removal.test.ts +++ b/src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-subscriber-oversized-removal.test.ts @@ -43,7 +43,7 @@ it('catches a subscriber up past a pre-existing unsendable removal with a bounde revision: 1, body: { kind: 'message', role: 'assistant', blocks: [{ type: 'text', text: 'big' }] }, v: AGENT_SESSION_JOURNAL_SCHEMA_VERSION, - epoch: seeded.epoch, + epoch: seeded.physicalCursor().epoch, seq: seq + 1, fence: 1, ts: 2_000 @@ -53,7 +53,7 @@ it('catches a subscriber up past a pre-existing unsendable removal with a bounde itemId: hugeItemId, revision: 2, v: AGENT_SESSION_JOURNAL_SCHEMA_VERSION, - epoch: seeded.epoch, + epoch: seeded.physicalCursor().epoch, seq: seq + 2, fence: 1, ts: 2_001 diff --git a/src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-wire-admission.test.ts b/src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-wire-admission.test.ts index 51ce24f1cf9..1df8dfe6a8d 100644 --- a/src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-wire-admission.test.ts +++ b/src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-wire-admission.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalSnapshot } from '../../../../agent-session-journal/journal-observation.test-support' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -57,9 +58,9 @@ afterEach(async () => { describe('structured agent-session outbound admission', () => { it('admits bounded initial, background-task, epoch, compaction, and history recovery frames', async () => { - expect(Buffer.byteLength(JSON.stringify(journal.snapshot()), 'utf8')).toBeGreaterThan( - REMOTE_RUNTIME_MAX_OUTBOUND_JSON_BYTES - ) + expect( + Buffer.byteLength(JSON.stringify(await readTestJournalSnapshot(journal)), 'utf8') + ).toBeGreaterThan(REMOTE_RUNTIME_MAX_OUTBOUND_JSON_BYTES) let roster: AgentSessionBackgroundTaskState | null = null const subscribers = new AgentSessionSubscribers({ readBackgroundTasks: () => roster }) @@ -124,7 +125,7 @@ describe('structured agent-session outbound admission', () => { sessionId: SESSION, journal, fence: 2, - cursor: { epoch: journal.epoch, sequence: firstItemSequence }, + cursor: { epoch: journal.physicalCursor().epoch, sequence: firstItemSequence }, emit: (event) => catchup.push(event) }) @@ -156,7 +157,12 @@ function body(text: string): AgentJournalItemBody { /** Inject a legacy oversized removal ID to force a cursor_compacted reset. */ async function reopenWithOversizedRemoval(afterSequence: number): Promise { const hugeItemId = `codex:thread-1:${'h'.repeat(5 * 1024 * 1024)}:1` - const base = { v: AGENT_SESSION_JOURNAL_SCHEMA_VERSION, epoch: journal.epoch, fence: 1, ts: 1 } + const base = { + v: AGENT_SESSION_JOURNAL_SCHEMA_VERSION, + epoch: journal.physicalCursor().epoch, + fence: 1, + ts: 1 + } const rows: JournalRow[] = [ { ...base, diff --git a/src/main/native-chat/agent-session-wire/rewind/testing/structured-agent-session-rewind-provenance.test.ts b/src/main/native-chat/agent-session-wire/rewind/testing/structured-agent-session-rewind-provenance.test.ts index 98fa506c8af..4d0e27d307b 100644 --- a/src/main/native-chat/agent-session-wire/rewind/testing/structured-agent-session-rewind-provenance.test.ts +++ b/src/main/native-chat/agent-session-wire/rewind/testing/structured-agent-session-rewind-provenance.test.ts @@ -100,19 +100,19 @@ async function rewindAtLiveFence(journal: AgentSessionJournal): Promise { describe('a rewind after a failed cleanup', () => { it("does not promote the dead generation's turn, prompt or reply to the live one", async () => { const journal = await leftByDeadGeneration() - const before = journal.epoch + const before = journal.physicalCursor().epoch await rewindAtLiveFence(journal) - expect(journal.epoch).not.toBe(before) + expect(journal.physicalCursor().epoch).not.toBe(before) const work = new StructuredAgentSessionCurrentWork(journal, LIVE) // New row fence must not promote old execution provenance. - expect(journal.itemFence(agentJournalItemKey(TURN))).toBe(DEAD) - expect(journal.itemFence(agentJournalItemKey(PROMPT))).toBe(DEAD) - expect(journal.itemFence(agentJournalItemKey(REPLY))).toBe(DEAD) + expect(await journal.reads.itemFence(agentJournalItemKey(TURN))).toBe(DEAD) + expect(await journal.reads.itemFence(agentJournalItemKey(PROMPT))).toBe(DEAD) + expect(await journal.reads.itemFence(agentJournalItemKey(REPLY))).toBe(DEAD) expect(work.activeTurnId()).toBeNull() expect(work.actionablePromptIds()).toEqual([]) expect(work.working()).toBe(false) - expect(journal.itemBody(agentJournalItemKey(UNDONE))).toBeNull() + expect(await journal.reads.itemBody(agentJournalItemKey(UNDONE))).toBeNull() }) }) diff --git a/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-dead-generation-settlement.test.ts b/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-dead-generation-settlement.test.ts index 1ce5ec03781..1daa71a3f7d 100644 --- a/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-dead-generation-settlement.test.ts +++ b/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-dead-generation-settlement.test.ts @@ -1,3 +1,8 @@ +import { + readTestJournalSnapshot, + readTestJournalSubmissions +} from '../../../agent-session-journal/journal-observation.test-support' +import { openTestAgentSessionDatabase } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' 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' @@ -6,10 +11,7 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { openAgentSessionJournal } from '../../../agent-session-journal/journal-store-factory' import type { AgentSessionJournal } from '../../../agent-session-journal/journal-store' import { settleObservedExitForTest } from './structured-agent-session-observed-exit.test-fixture' -import { - closeTestJournalHostDatabases, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { closeTestJournalHostDatabases } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { codexProviderHandle } from '../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' const SESSION = 'session-dead-generation' @@ -27,7 +29,7 @@ beforeEach(async () => { agent: 'codex', providerHandle: codexProviderHandle(THREAD) }, - database: openTestJournalHostDatabase(root), + database: await openTestAgentSessionDatabase(root), now: () => 1_000 }) }) @@ -94,7 +96,7 @@ describe('dead structured-session generation settlement', () => { }) ).resolves.toMatchObject({ ok: true }) - const snapshot = journal.snapshot() + const snapshot = await readTestJournalSnapshot(journal) expect(snapshot.submissions).toEqual([ expect.objectContaining({ clientMessageId: 'client-1', dispatchState: 'unknown' }) ]) @@ -144,7 +146,7 @@ describe('dead structured-session generation settlement', () => { }) ).resolves.toMatchObject({ ok: true }) - expect(journal.submissions()).toEqual([ + expect(await readTestJournalSubmissions(journal)).toEqual([ expect.objectContaining({ clientMessageId: 'client-unknown', dispatchState: 'unknown', diff --git a/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-observed-exit.test-fixture.ts b/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-observed-exit.test-fixture.ts index f22303c8bc9..c77d8637cfb 100644 --- a/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-observed-exit.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-observed-exit.test-fixture.ts @@ -8,7 +8,7 @@ import { settleStructuredAgentSessionLeftovers } from '../structured-agent-sessi /** ownerFence defaults to all fences for a single-generation chat. */ export function settleObservedExitForTest( - input: StructuredAgentSessionDeadGenerationInput & { + input: Omit & { journal: AgentSessionJournal ownerFence?: number } diff --git a/src/main/runtime/agent-session-inventory-scale.test.ts b/src/main/runtime/agent-session-inventory-scale.test.ts index 09318ec6b46..d8ce77094ab 100644 --- a/src/main/runtime/agent-session-inventory-scale.test.ts +++ b/src/main/runtime/agent-session-inventory-scale.test.ts @@ -7,7 +7,6 @@ import { hostTestState } from '../native-chat/agent-session-wire/host/testing/st import { StructuredAgentSessionHost } from '../native-chat/agent-session-wire/host/structured-agent-session-host' import { setStructuredAgentSessionHost } from '../native-chat/agent-session-wire/host/structured-agent-session-registry' import * as journalFactory from '../native-chat/agent-session-journal/journal-store-factory' -import { openTestJournalHostDatabase } from '../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import { countRecordQueries, countStoreWork, @@ -23,7 +22,8 @@ import { seedTestAgentSessionRecordStore, openTestAgentSessionRecordStore, editPersistedTestAgentSessionStore, - readPersistedTestAgentSessionStore + readPersistedTestAgentSessionStore, + openTestAgentSessionDatabase } from './agent-session/records/agent-session-record-store-test-harness' import { OrchestrationDb } from './orchestration/db/orchestration-db' import './orchestration/db/contract-constants' @@ -56,7 +56,7 @@ async function inventoryRig(n: number) { const host = new StructuredAgentSessionHost({ ...hostTestState().host.deps, store: rig.store, - journalDatabase: openTestJournalHostDatabase(rig.root) + journalDatabase: await openTestAgentSessionDatabase(rig.root) }) setStructuredAgentSessionHost(host) return { ...rig, host } @@ -109,7 +109,7 @@ describe('real conversation inventory work', () => { const host = new StructuredAgentSessionHost({ ...hostTestState().host.deps, store, - journalDatabase: openTestJournalHostDatabase(root) + journalDatabase: await openTestAgentSessionDatabase(root) }) const ids = new Set(unreadable.map(({ sessionId }) => sessionId)) const work = countStoreWork(store) @@ -203,7 +203,7 @@ describe('real conversation inventory work', () => { const host = new StructuredAgentSessionHost({ ...hostTestState().host.deps, store: globalStore, - journalDatabase: openTestJournalHostDatabase(globalRoot) + journalDatabase: await openTestAgentSessionDatabase(globalRoot) }) setStructuredAgentSessionHost(host) const db = new OrchestrationDb(':memory:') diff --git a/src/main/runtime/agent-session-store-load-scale.test.ts b/src/main/runtime/agent-session-store-load-scale.test.ts index ce504c6aed0..a768402fca1 100644 --- a/src/main/runtime/agent-session-store-load-scale.test.ts +++ b/src/main/runtime/agent-session-store-load-scale.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalItems } from '../native-chat/agent-session-journal/journal-observation.test-support' import { describe, expect, it } from 'vitest' import { countRecordQueries, @@ -82,7 +83,7 @@ describe('SQLite conversation reopen work', () => { } expect(after.records['healthy-chat'].conversationName).toBe('renamed') const reopened = await scaleJournal(root, 'damaged-0') - expect(reopened.snapshot().items.map((item) => item.body)).toContainEqual( + expect((await readTestJournalItems(reopened)).map((item) => item.body)).toContainEqual( hostTestMessage('saved question') ) expect(store.getConversation('damaged-0')?.providerHandleChain[0]?.handle.nativeId).toBe( diff --git a/src/main/runtime/agent-session/agent-session-conversation-clear-commit.test.ts b/src/main/runtime/agent-session/agent-session-conversation-clear-commit.test.ts index 76f6923a8c7..4ea8105d61f 100644 --- a/src/main/runtime/agent-session/agent-session-conversation-clear-commit.test.ts +++ b/src/main/runtime/agent-session/agent-session-conversation-clear-commit.test.ts @@ -163,9 +163,9 @@ describe('clear transaction', () => { expect(journal.cursor()).toEqual(before) expect((await open()).getRecord(SOURCE)?.conversationName).toBe('Saved name') expect( - journal.queuedMessages.list().map(({ messageId, state }) => ({ messageId, state })) + (await journal.queuedMessages.list()).map(({ messageId, state }) => ({ messageId, state })) ).toEqual(['first', 'command', 'last'].map((messageId) => ({ messageId, state: 'waiting' }))) - expect(journal.queuedMessages.pauses()).toEqual([]) + expect(await journal.queuedMessages.pauses()).toEqual([]) expect((await open()).getRecord(SOURCE)?.providerContextBoundary).toBeUndefined() expect((await open()).getRecord(SOURCE)?.providerHandleChain).toEqual(previousChain) expect(store.getOperationRow(operation.callerKey, operation.operationId)?.outcome.status).toBe( diff --git a/src/main/runtime/agent-session/lifecycle/claude-structured-exit-mid-response.test.ts b/src/main/runtime/agent-session/lifecycle/claude-structured-exit-mid-response.test.ts index 88a0a7a84ad..3d539446a4c 100644 --- a/src/main/runtime/agent-session/lifecycle/claude-structured-exit-mid-response.test.ts +++ b/src/main/runtime/agent-session/lifecycle/claude-structured-exit-mid-response.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../native-chat/agent-session-journal/journal-observation.test-support' // Script only the CLI; verify self-exit attribution through the production runtime/adapter/store/host. import { afterEach, describe, expect, it, vi } from 'vitest' @@ -54,7 +55,7 @@ describe('a started Claude CLI that exits while a response is in progress', () = await vi.waitFor(async () => expect( - (await host.journalSnapshot(SESSION)).items.flatMap((item) => + (await readTestHostJournal(host, SESSION)).items.flatMap((item) => item.body.kind === 'status' && item.body.failure ? [{ text: item.body.text, kind: item.body.failure.kind }] : [] diff --git a/src/main/runtime/agent-session/lifecycle/claude-structured-resumed-start-failure.test.ts b/src/main/runtime/agent-session/lifecycle/claude-structured-resumed-start-failure.test.ts index 128b29cddae..de5b457eae6 100644 --- a/src/main/runtime/agent-session/lifecycle/claude-structured-resumed-start-failure.test.ts +++ b/src/main/runtime/agent-session/lifecycle/claude-structured-resumed-start-failure.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../native-chat/agent-session-journal/journal-observation.test-support' // Reopened chats publish before initialize; later startup failure must reach the visible transcript. import { afterEach, describe, expect, it, vi } from 'vitest' @@ -76,7 +77,7 @@ describe('a reopened Claude chat whose CLI dies before initialize', () => { } await vi.waitFor(() => expect(statusTexts(events)).toContainEqual(STARTUP_TEXT)) - const submission = (await host.journalSnapshot(SESSION)).submissions.find( + const submission = (await readTestHostJournal(host, SESSION)).submissions.find( (entry) => entry.clientMessageId === (sent.ok && sent.value.clientMessageId) ) // Keep stderr in log details, outside the user-facing sentence. diff --git a/src/main/runtime/agent-session/lifecycle/claude-structured-startup-fault-is-not-an-exit.test.ts b/src/main/runtime/agent-session/lifecycle/claude-structured-startup-fault-is-not-an-exit.test.ts index 8bdcb389773..1bebd4fcbf5 100644 --- a/src/main/runtime/agent-session/lifecycle/claude-structured-startup-fault-is-not-an-exit.test.ts +++ b/src/main/runtime/agent-session/lifecycle/claude-structured-startup-fault-is-not-an-exit.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../native-chat/agent-session-journal/journal-observation.test-support' // Host-detected startup faults must not be attributed as self-exits without observed CLI exit. import { afterEach, describe, expect, it, vi } from 'vitest' @@ -51,7 +52,7 @@ async function send(host: StructuredAgentSessionHost, text: string): Promise + return (await readTestHostJournal(host, SESSION)).items.flatMap((item) => item.body.kind === 'status' && item.body.failure ? [{ text: item.body.text, kind: item.body.failure.kind }] : [] @@ -59,7 +60,7 @@ async function failureRows(host: StructuredAgentSessionHost) { } async function submission(host: StructuredAgentSessionHost, clientMessageId: string) { - return (await host.journalSnapshot(SESSION)).submissions.find( + return (await readTestHostJournal(host, SESSION)).submissions.find( (entry) => entry.clientMessageId === clientMessageId ) } diff --git a/src/main/runtime/agent-session/records/agent-session-record-store-test-harness.ts b/src/main/runtime/agent-session/records/agent-session-record-store-test-harness.ts index 7fbcbf47e41..0c5eeed72aa 100644 --- a/src/main/runtime/agent-session/records/agent-session-record-store-test-harness.ts +++ b/src/main/runtime/agent-session/records/agent-session-record-store-test-harness.ts @@ -1,5 +1,6 @@ /** Centralize store fixtures in the worker-owned journal DB; closeTestAgentSessionRecordStores owns their cleanup. */ +import { resolve } from 'node:path' import type { AgentSessionOperationRow } from '../../../../shared/agent-session/lifecycle/agent-session-operation-ledger' import type { PersistedAgentSessionRecord } from '../../../../shared/agent-session/lifecycle/agent-session-legacy-handoff-lease' import { @@ -38,18 +39,26 @@ const databases = new Map>() /** Stores reopened in one directory share its writer, like one runtime's slot. */ export function openTestAgentSessionDatabase(stateDirectory: string): Promise { - let opened = databases.get(stateDirectory) + const directory = resolve(stateDirectory) + let opened = databases.get(directory) if (!opened) { - opened = JournalSqlDatabase.open(stateDirectory, { + opened = JournalSqlDatabase.open(directory, { writerPath: journalSqlWorkerPath(), readerPath: journalSqlReaderPath(), onMaintenanceError: () => {} }) - databases.set(stateDirectory, opened) + databases.set(directory, opened) } return opened } +export async function closeTestAgentSessionDatabase(stateDirectory: string): Promise { + const directory = resolve(stateDirectory) + const opened = databases.get(directory) + databases.delete(directory) + await (await opened)?.close() +} + export async function closeTestAgentSessionRecordStores(): Promise { const opened = [...databases.values()] databases.clear() diff --git a/src/main/runtime/agent-session/records/structured-agent-session-mobile-restore.test.ts b/src/main/runtime/agent-session/records/structured-agent-session-mobile-restore.test.ts index 12804fca2c5..c3e03174b2a 100644 --- a/src/main/runtime/agent-session/records/structured-agent-session-mobile-restore.test.ts +++ b/src/main/runtime/agent-session/records/structured-agent-session-mobile-restore.test.ts @@ -15,8 +15,10 @@ import { } from '../../../native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture' import { StructuredAgentSessionHost } from '../../../native-chat/agent-session-wire/host/structured-agent-session-host' import { claudeAndCodexDeclared } from '../../../native-chat/agent-session-wire/providers/testing/structured-agent-session-adapter-router.test-fixture' -import { openTestAgentSessionRecordStore } from './agent-session-record-store-test-harness' -import { openTestJournalHostDatabase } from '../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { + openTestAgentSessionRecordStore, + openTestAgentSessionDatabase +} from './agent-session-record-store-test-harness' import { RuntimeMobileNotificationController, type MobileNotificationEvent @@ -96,7 +98,7 @@ describe('prompt delivery after host recovery', () => { logger: h.log.logger, store, adapter: adapter(), - journalDatabase: openTestJournalHostDatabase(h.root), + journalDatabase: await openTestAgentSessionDatabase(h.root), claimKeyId: 'key-1', mintSpawnToken: () => 'spawn-b', probeOwner: async () => ({ outcome: 'pid-absent' }), diff --git a/src/main/runtime/agent-session/records/structured-agent-session-startup-tab-restore.test.ts b/src/main/runtime/agent-session/records/structured-agent-session-startup-tab-restore.test.ts index 971999f83ef..5c41ac389ff 100644 --- a/src/main/runtime/agent-session/records/structured-agent-session-startup-tab-restore.test.ts +++ b/src/main/runtime/agent-session/records/structured-agent-session-startup-tab-restore.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../native-chat/agent-session-journal/journal-observation.test-support' import { partialRuntimeStore } from '../../orca-runtime-tests/runtime-store-partial-fixture' import type { WorkspaceSessionState } from '../../../../shared/workspace-session/workspace-session-state-types' import { mkdtemp, rm } from 'node:fs/promises' @@ -29,6 +30,7 @@ import { attachParamsForRecord } from '../../../native-chat/agent-session-wire/h import { getStructuredAgentSessionHost } from '../../../native-chat/agent-session-wire/host/structured-agent-session-registry' import type * as AgentSessionRecordRows from './agent-session-record-rows' import { + openTestAgentSessionDatabase, seedTestAgentSessionRecordStore, seedTestAgentSessionStoreFromNewerBuild } from './agent-session-record-store-test-harness' @@ -129,7 +131,7 @@ async function seedProfile( } : {}) }) - const database = openTestJournalHostDatabase(root) + const database = await openTestAgentSessionDatabase(root) for (const record of options.history ?? records) { const fence = record.lease.runtimeFence const journal = await journalFactory.openAgentSessionJournal({ @@ -285,7 +287,7 @@ describe('restoring the chat tabs open at quit', () => { expect(history.page.queuedMessages).toEqual([ { messageId, position: 0, state: 'waiting', body } ]) - const original = (await host.journalSnapshot(sessionId)).submissions + const original = (await readTestHostJournal(host, sessionId)).submissions expect(original).toHaveLength(1) expect(original[0]).toMatchObject({ clientMessageId: messageId, @@ -397,7 +399,7 @@ describe('restoring the chat tabs open at quit', () => { } expect(delivered.page.queuedMessages).toEqual([]) expect( - (await next.host.journalSnapshot(sessionId)).submissions.filter( + (await readTestHostJournal(next.host, sessionId)).submissions.filter( (entry) => entry.queuedMessageId === messageId ) ).toEqual([ diff --git a/src/main/runtime/agent-session/structured-agent-session-adapter-start-failure.test.ts b/src/main/runtime/agent-session/structured-agent-session-adapter-start-failure.test.ts index 927ce6bdddc..32eb30bd890 100644 --- a/src/main/runtime/agent-session/structured-agent-session-adapter-start-failure.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-adapter-start-failure.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../native-chat/agent-session-journal/journal-observation.test-support' import { claudeScriptedChild } from '../../claude/claude-scripted-child.test-fixture' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' @@ -106,7 +107,7 @@ async function failedStart( } const submission = async () => { await host.flushStreamedEvents(SESSION) - return (await host.journalSnapshot(SESSION)).submissions.find( + return (await readTestHostJournal(host, SESSION)).submissions.find( (entry) => entry.clientMessageId === sent.value.clientMessageId ) } @@ -114,7 +115,7 @@ async function failedStart( timeout: 5_000 }) const settled = await submission() - const snapshot = await host.journalSnapshot(SESSION) + const snapshot = await readTestHostJournal(host, SESSION) const failureKind = agent === 'codex' && !unanswered ? 'startFailed' : 'providerStartFailed' expect(readWholeAgentSessionFailureFact(settled?.rejection)).toMatchObject({ kind: failureKind, diff --git a/src/main/runtime/agent-session/structured-agent-session-attachment-wiring.test.ts b/src/main/runtime/agent-session/structured-agent-session-attachment-wiring.test.ts index 456300e808a..896e4f75342 100644 --- a/src/main/runtime/agent-session/structured-agent-session-attachment-wiring.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-attachment-wiring.test.ts @@ -6,12 +6,8 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { getAgentSessionAttachmentStore } from '../../native-chat/agent-session-attachments/agent-session-attachment-store-registry' import { agentSessionAttachmentStoreRoot } from '../../native-chat/agent-session-attachments/agent-session-attachment-references' import { createStructuredAgentSessionLogger } from '../../native-chat/agent-session-wire/host/structured-agent-session-logger' -import * as attachmentSweeps from '../../native-chat/agent-session-attachments/agent-session-attachment-sweep' -import { - closeTestJournalHostDatabases, - openTestJournalHostDatabase -} from '../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' -import type Database from '../../sqlite/sync-database' +import { closeTestJournalHostDatabases } from '../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { openTestAgentSessionDatabase } from './records/agent-session-record-store-test-harness' import type { AgentSessionRecordStore } from './records/agent-session-record-store' import { installAgentSessionAttachments, @@ -37,21 +33,8 @@ const records: Pick false } -function install(directory = stateDirectory): void { - // Written by a newer Orca: the sweeps read no claims, so nothing here touches the connection. - const journal: Parameters[0]['journalDatabase'] = { - writeAutocommit: () => { - throw new Error('not opened in this test') - }, - transaction: () => { - throw new Error('not opened in this test') - }, - readOnly: true, - isClosed: false, - get db(): Database.Database { - throw new Error('not opened in this test') - } - } +async function install(directory = stateDirectory): Promise { + const journal = await openTestAgentSessionDatabase(stateDirectory) installAgentSessionAttachments({ stateDirectory: directory, store: records, @@ -62,8 +45,8 @@ function install(directory = stateDirectory): void { describe('installAgentSessionAttachments', () => { // Claude takes the store as an added directory only if it exists when Claude starts. - it('creates the store root the Claude launch is granted, before any upload', () => { - install() + it('creates the store root the Claude launch is granted, before any upload', async () => { + await install() const root = agentSessionAttachmentStoreRoot(stateDirectory) expect(existsSync(root)).toBe(true) expect(getAgentSessionAttachmentStore()?.rootDir).toBe(root) @@ -74,45 +57,8 @@ describe('installAgentSessionAttachments', () => { // A file where the state directory should be: nothing can be created under it. const blocked = join(stateDirectory, 'not-a-directory') await writeFile(blocked, '') - expect(() => install(blocked)).not.toThrow() + await expect(install(blocked)).resolves.toBeUndefined() expect(getAgentSessionAttachmentStore()?.rootDir).toBe(agentSessionAttachmentStoreRoot(blocked)) warn.mockRestore() }) - - it("starts the chat database's sweep of retired records and stops it with the attachments", async () => { - vi.useFakeTimers() - vi.spyOn(attachmentSweeps, 'startAgentSessionAttachmentSweeps').mockReturnValue({ - stop: () => undefined - }) - const database = openTestJournalHostDatabase(stateDirectory) - const retire = () => - database.transaction((db) => { - const generation = db - .prepare('INSERT INTO journal_outcome_generations (session_id, epoch) VALUES (?, ?)') - .run('gone', 'gone-epoch').lastInsertRowid - db.prepare(`INSERT INTO journal_outcomes - (generation, seq, field, entry_key, format, ordinal, value_json, deleted) - VALUES (?, 1, 'aliases', '"k"', 1, 's0000000000000001:0000000000000000', '["k","v"]', 0)`).run( - generation - ) - }) - const retired = () => - database.db.prepare('SELECT COUNT(*) AS n FROM journal_outcome_generations').get()?.n - retire() - installAgentSessionAttachments({ - stateDirectory, - store: records, - journalDatabase: database, - logger: createStructuredAgentSessionLogger() - }) - await vi.advanceTimersByTimeAsync(59_999) - expect(retired()).toBe(1) - // The first pass a minute after launch: the records page, a rest, then the generation row. - await vi.advanceTimersByTimeAsync(1_000) - expect(retired()).toBe(0) - retire() - stopAgentSessionAttachments() - await vi.advanceTimersByTimeAsync(12 * 60 * 60_000) - expect(retired()).toBe(1) - }) }) diff --git a/src/main/runtime/agent-session/structured-agent-session-codex-compact-exit.test.ts b/src/main/runtime/agent-session/structured-agent-session-codex-compact-exit.test.ts index 5d9d3766c6a..522c1a49519 100644 --- a/src/main/runtime/agent-session/structured-agent-session-codex-compact-exit.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-codex-compact-exit.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../native-chat/agent-session-journal/journal-observation.test-support' // Exercise real adapter connection exits/Stop during compact; fake only the Codex child, never its compaction result. import { afterEach, beforeEach, expect, it, vi } from 'vitest' @@ -25,7 +26,10 @@ afterEach(async () => { }) async function snapshot() { - return (await ensureStructuredAgentSessionHost(harness.hostConfig())).journalSnapshot(SESSION) + return await readTestHostJournal( + await ensureStructuredAgentSessionHost(harness.hostConfig()), + SESSION + ) } /** The connections that received `method`, once per call. */ diff --git a/src/main/runtime/agent-session/structured-agent-session-codex-start-deadline.test.ts b/src/main/runtime/agent-session/structured-agent-session-codex-start-deadline.test.ts index a91d4d3db5e..c39663f31ad 100644 --- a/src/main/runtime/agent-session/structured-agent-session-codex-start-deadline.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-codex-start-deadline.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../native-chat/agent-session-journal/journal-observation.test-support' // A Codex start has no per-step deadline: a slow `initialize` still delivers the held message, and // one that never answers is ended by the start's own overall limit ("never finished starting"). @@ -113,7 +114,7 @@ it('fails the held message as never finished starting when the start passes its await vi.waitFor(async () => { await host.flushStreamedEvents(HOST_TEST_SESSION) - const [held] = (await host.journalSnapshot(HOST_TEST_SESSION)).submissions + const [held] = (await readTestHostJournal(host, HOST_TEST_SESSION)).submissions expect(held?.dispatchState).toBe('rejected') expect(readWholeAgentSessionFailureFact(held?.rejection)).toMatchObject({ kind: 'hostStopped' }) }) diff --git a/src/main/runtime/agent-session/structured-agent-session-integration-replay.test.ts b/src/main/runtime/agent-session/structured-agent-session-integration-replay.test.ts index 5da475c0922..15d3d24b77f 100644 --- a/src/main/runtime/agent-session/structured-agent-session-integration-replay.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-integration-replay.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalItems } from '../../native-chat/agent-session-journal/journal-observation.test-support' // Exercise shipped RPC, schemas, capability gate, stores, lease and adapter; only the Codex app-server child is fake. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' @@ -133,13 +134,13 @@ describe('a structured codex session over agentSession.*', () => { identity, stateDirectory: harness.root }) - expect(reopened.snapshot().items.map(textOf)).toContain('Buffered while the journal opens.') + expect((await readTestJournalItems(reopened)).map(textOf)).toContain( + 'Buffered while the journal opens.' + ) expect( - reopened - .snapshot() - .items.some( - (item) => item.body?.kind === 'status' && item.body.turnLifecycle?.state === 'running' - ) + (await readTestJournalItems(reopened)).some( + (item) => item.body?.kind === 'status' && item.body.turnLifecycle?.state === 'running' + ) ).toBe(false) }) }) diff --git a/src/main/runtime/agent-session/structured-agent-session-integration.test.ts b/src/main/runtime/agent-session/structured-agent-session-integration.test.ts index f0d9bcc1d9d..d3e526ecaf0 100644 --- a/src/main/runtime/agent-session/structured-agent-session-integration.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-integration.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalItems } from '../../native-chat/agent-session-journal/journal-observation.test-support' import '../rpc/unused-default-rpc-methods.test-fixture' import { scriptedCodexCatalogSession } from '../scripted-codex-model-catalog.test-fixture' import { mkdtemp, rm } from 'node:fs/promises' @@ -349,13 +350,13 @@ describe('a structured codex session over agentSession.*', () => { identity: JOURNAL_IDENTITY, stateDirectory: root }) - expect(reopened.snapshot().items.map(textOf)).toContain('Final text before shutdown.') + expect((await readTestJournalItems(reopened)).map(textOf)).toContain( + 'Final text before shutdown.' + ) expect( - reopened - .snapshot() - .items.some( - (item) => item.body?.kind === 'status' && item.body.turnLifecycle?.state === 'running' - ) + (await readTestJournalItems(reopened)).some( + (item) => item.body?.kind === 'status' && item.body.turnLifecycle?.state === 'running' + ) ).toBe(false) }) }) diff --git a/src/main/runtime/agent-session/structured-agent-session-legacy-send-behind-command.test.ts b/src/main/runtime/agent-session/structured-agent-session-legacy-send-behind-command.test.ts index e7e31a810a1..97c9b165a2a 100644 --- a/src/main/runtime/agent-session/structured-agent-session-legacy-send-behind-command.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-legacy-send-behind-command.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../native-chat/agent-session-journal/journal-observation.test-support' // Clients without accepted-send support wait for handover; sends queued behind compact must answer when queued to avoid the phone timeout. import { afterEach, beforeEach, expect, it, vi } from 'vitest' @@ -83,9 +84,10 @@ it('answers a send queued behind /compact at once, and delivers it once after th }) await vi.waitFor(() => expect(calls('turn/start')).toHaveLength(1)) - const journal = await ( - await ensureStructuredAgentSessionHost(harness.hostConfig()) - ).journalSnapshot(SESSION) + const journal = await readTestHostJournal( + await ensureStructuredAgentSessionHost(harness.hostConfig()), + SESSION + ) expect( journal.submissions.filter((entry) => entry.queuedMessageId === answer.queued.messageId) ).toEqual([expect.objectContaining({ handedOverAt: expect.any(Number) })]) diff --git a/src/main/runtime/agent-session/structured-agent-session-pi-signed-out-start.test.ts b/src/main/runtime/agent-session/structured-agent-session-pi-signed-out-start.test.ts index a8f66a6d541..f41776dcfe5 100644 --- a/src/main/runtime/agent-session/structured-agent-session-pi-signed-out-start.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-pi-signed-out-start.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } 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' @@ -72,7 +73,7 @@ it('settles the held message once, closes Pi, and starts afresh for a later send script.releaseHandshake() const submission = async () => { await host.flushStreamedEvents(SESSION) - return (await host.journalSnapshot(SESSION)).submissions.find( + return (await readTestHostJournal(host, SESSION)).submissions.find( (entry) => entry.clientMessageId === firstMessage.clientMessageId ) } diff --git a/src/main/runtime/orchestration-cli-subprocess.test.ts b/src/main/runtime/orchestration-cli-subprocess.test.ts index 6dd0e142045..1f9cd94846b 100644 --- a/src/main/runtime/orchestration-cli-subprocess.test.ts +++ b/src/main/runtime/orchestration-cli-subprocess.test.ts @@ -32,7 +32,8 @@ import { } from '../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import { openTestAgentSessionRecordStore, - readPersistedTestAgentSessionStoreText + readPersistedTestAgentSessionStoreText, + openTestAgentSessionDatabase } from './agent-session/records/agent-session-record-store-test-harness' import { idOf, isRecord, resultOf } from './rpc/orchestration-session-caller-test-fixture' @@ -616,7 +617,7 @@ describeIfBuilt('native caller subprocess over durable records', () => { host = new StructuredAgentSessionHost({ ...rig.host.deps, store, - journalDatabase: openTestJournalHostDatabase(rig.root) + journalDatabase: await openTestAgentSessionDatabase(rig.root) }) replaceHostTestState({ host, store }) setStructuredAgentSessionHost(host) diff --git a/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-coordinator-mail-rig.test-fixture.ts b/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-coordinator-mail-rig.test-fixture.ts index cec224d51f3..9524aed92fa 100644 --- a/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-coordinator-mail-rig.test-fixture.ts +++ b/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-coordinator-mail-rig.test-fixture.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../../native-chat/agent-session-journal/journal-observation.test-support' // End-to-end coordinator-mail rig: real session/orchestration state and RPC, fake Codex child. Import registers lifecycle hooks. import { mkdtemp, rm } from 'node:fs/promises' @@ -149,10 +150,11 @@ export function sendUserMessage(sessionId: string, text: string) { } export async function userTexts(sessionId: string): Promise { - return (await host.journalSnapshot(sessionId)).items.flatMap((item: AgentJournalRenderItem) => - item.body?.kind === 'message' && item.body.role === 'user' - ? item.body.blocks.map((block) => (block.type === 'text' ? block.text : '')) - : [] + return (await readTestHostJournal(host, sessionId)).items.flatMap( + (item: AgentJournalRenderItem) => + item.body?.kind === 'message' && item.body.role === 'user' + ? item.body.blocks.map((block) => (block.type === 'text' ? block.text : '')) + : [] ) } diff --git a/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-dispatch-worker.test.ts b/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-dispatch-worker.test.ts index 44655595255..022038a3673 100644 --- a/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-dispatch-worker.test.ts +++ b/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-dispatch-worker.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../../native-chat/agent-session-journal/journal-observation.test-support' import '../../../rpc/unused-default-rpc-methods.test-fixture' import { describe, expect, it, vi } from 'vitest' @@ -70,7 +71,7 @@ async function refusal(method: string, params: Record, sessionI /** Who each agent message in the chat's journal says it is from. */ async function taskSenders(sessionId: string): Promise { - return (await host.journalSnapshot(sessionId)).items.flatMap((item) => + return (await readTestHostJournal(host, sessionId)).items.flatMap((item) => item.body.kind === 'message' && item.body.from ? [item.body.from] : [] ) } diff --git a/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-mail-custody.test.ts b/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-mail-custody.test.ts index 81e227f4b86..dea3f1667f7 100644 --- a/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-mail-custody.test.ts +++ b/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-mail-custody.test.ts @@ -151,7 +151,7 @@ describe('a queued mail card runs only while its chat still owes it', () => { runId, count: 1 }) - const [card] = journalOf(COORDINATOR).queuedMessages.list() + const [card] = await journalOf(COORDINATOR).queuedMessages.list() const fields = { messageId: card!.messageId } const sent = await host.queuedMessageSend( { callerKey: 'test-surface' }, @@ -266,7 +266,7 @@ describe('a queued mail card runs only while its chat still owes it', () => { const record = host.deps.store.getRecord(COORDINATOR) expect(nextStructuredQueuedMessage({ journal, record, work })).toBeNull() // The person's Send of that card says what happened, never that Orca restarted. - const card = journal.queuedMessages.list().find((row) => row.state === 'waiting') + const card = (await journal.queuedMessages.list()).find((row) => row.state === 'waiting') const fields = { messageId: card!.messageId } const sent = await host.queuedMessageSend( { callerKey: 'test-surface' }, @@ -337,7 +337,7 @@ describe('a queued mail card runs only while its chat still owes it', () => { await vi.waitFor(() => expect(send.mock.calls.length).toBeLessThanOrEqual(2), WAIT) // Deleting the person's cards frees room: that journal edge alone sends the report. - for (const card of journalOf(COORDINATOR).queuedMessages.list()) { + for (const card of await journalOf(COORDINATOR).queuedMessages.list()) { const fields = { messageId: card.messageId } expect( await host.queuedMessageDelete( diff --git a/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-worker-start-unknown.test.ts b/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-worker-start-unknown.test.ts index 5fa92e6f4a8..7606f6ccf80 100644 --- a/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-worker-start-unknown.test.ts +++ b/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-worker-start-unknown.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../../native-chat/agent-session-journal/journal-observation.test-support' import { EVERY_AGENT_CHAT } from '../../../../../shared/native-chat/every-agent-chat.test-fixture' import '../../../rpc/unused-default-rpc-methods.test-fixture' import { expect, it, vi } from 'vitest' @@ -69,7 +70,7 @@ it('keeps a minted worker start unknown after one preamble handover without an e if (!sessionId) { throw new Error('minted worker session missing') } - const snapshot = await host.journalSnapshot(sessionId) + const snapshot = await readTestHostJournal(host, sessionId) expect(snapshot.submissions).toHaveLength(1) expect(snapshot.submissions[0]).toMatchObject({ dispatchState: 'unknown' }) expect(worker.turns).toHaveLength(1) diff --git a/src/main/runtime/orchestration/turn-delivery/send-agent-turn-host.test.ts b/src/main/runtime/orchestration/turn-delivery/send-agent-turn-host.test.ts index 03b4ea90b19..85d2665d0fa 100644 --- a/src/main/runtime/orchestration/turn-delivery/send-agent-turn-host.test.ts +++ b/src/main/runtime/orchestration/turn-delivery/send-agent-turn-host.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../native-chat/agent-session-journal/journal-observation.test-support' // Use the real host's fingerprint admission to reject envelopes derived from different fields. import { afterEach, beforeEach, describe, expect, it } from 'vitest' @@ -67,7 +68,7 @@ function sendTurn( } async function sentMessage(clientMessageId: string) { - const snapshot = await rig.host.journalSnapshot(SESSION) + const snapshot = await readTestHostJournal(rig.host, SESSION) return snapshot.items.find((item) => item.itemId === agentJournalSubmissionKey(clientMessageId)) } @@ -79,11 +80,9 @@ describe('sendAgentTurn through the real host', () => { queued: { position: 1, state: 'waiting' } }) expect( - rig.host - .collaboratorsForTests() - .sessions.get(SESSION) - ?.journal.queuedMessages.list() - .map(({ state, body }) => ({ state, from: body.from })) + ( + await rig.host.collaboratorsForTests().sessions.get(SESSION)!.journal.queuedMessages.list() + ).map(({ state, body }) => ({ state, from: body.from })) ).toEqual([{ state: 'waiting', from: MAIL_SOURCE }]) const page = await rig.host.history({ sessionId: SESSION, direction: 'tail' }) expect(page.ok && page.page.queuedMessages?.map(({ body }) => body.from)).toEqual([MAIL_SOURCE]) diff --git a/src/main/runtime/rpc/methods/agent-launch/agent-launch-at-rest.test.ts b/src/main/runtime/rpc/methods/agent-launch/agent-launch-at-rest.test.ts index f3a714af35e..5a3d123cf99 100644 --- a/src/main/runtime/rpc/methods/agent-launch/agent-launch-at-rest.test.ts +++ b/src/main/runtime/rpc/methods/agent-launch/agent-launch-at-rest.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../../native-chat/agent-session-journal/journal-observation.test-support' import '../../unused-default-rpc-methods.test-fixture' import { afterEach, beforeEach, expect, it, vi } from 'vitest' import { AGENT_LAUNCH_RUNTIME_CAPABILITY } from '../../../../../shared/agent-launch/contracts/agent-launch-runtime-capability' @@ -122,7 +123,7 @@ it.each([ expect(result.prompt).toEqual( prompt ? { delivery: 'draft', outcome: 'not-delivered' } : undefined ) - expect((await rig.host.journalSnapshot(SESSION)).submissions).toEqual([]) + expect((await readTestHostJournal(rig.host, SESSION)).submissions).toEqual([]) // At rest, nothing is spawned until the chat's first message. expect(rig.adapter.acquire.mock.calls.length > 0).toBe(startsAtCreation) }) @@ -178,7 +179,7 @@ it('a whitespace-only submit creates an eager empty chat without sending a turn' expect(result.outcome).toMatchObject({ kind: 'structured', sessionId: SESSION }) expect(result.prompt).toEqual({ delivery: 'submit', outcome: 'not-delivered' }) expect(rig.adapter.acquire).toHaveBeenCalledOnce() - expect((await rig.host.journalSnapshot(SESSION)).submissions).toEqual([]) + expect((await readTestHostJournal(rig.host, SESSION)).submissions).toEqual([]) expect(rig.adapter.dispatch).not.toHaveBeenCalled() }) @@ -197,7 +198,7 @@ it('the opening prompt owns retry with explicit model and permission for a legac outcome: 'journaled', messageId: expect.any(String) }) - expect((await rig.host.journalSnapshot(SESSION)).submissions).toMatchObject([ + expect((await readTestHostJournal(rig.host, SESSION)).submissions).toMatchObject([ { clientMessageId: result.prompt?.outcome === 'journaled' ? result.prompt.messageId : undefined, source: { kind: 'user' }, @@ -211,7 +212,7 @@ it('the opening prompt owns retry with explicit model and permission for a legac expect(rig.adapter.dispatch.mock.calls[0]?.[0].clientMessageId).toBe( result.prompt?.outcome === 'journaled' ? result.prompt.messageId : undefined ) - const snapshot = await rig.host.journalSnapshot(SESSION) + const snapshot = await readTestHostJournal(rig.host, SESSION) expect(snapshot.submissions).toMatchObject([{ dispatchState: 'accepted' }]) expect(snapshot.items.filter((item) => item.body.kind === 'status')).toEqual([]) expect(rig.store.getRecord(SESSION)).toMatchObject({ @@ -229,7 +230,7 @@ it('rejects one requester with one failure row when all five children exit', asy await endChild() } await vi.waitFor(async () => - expect((await rig.host.journalSnapshot(SESSION)).submissions).toMatchObject([ + expect((await readTestHostJournal(rig.host, SESSION)).submissions).toMatchObject([ { clientMessageId: result.prompt?.outcome === 'journaled' ? result.prompt.messageId : undefined, @@ -237,7 +238,7 @@ it('rejects one requester with one failure row when all five children exit', asy } ]) ) - const snapshot = await rig.host.journalSnapshot(SESSION) + const snapshot = await readTestHostJournal(rig.host, SESSION) expect(snapshot.submissions).toHaveLength(1) expect( snapshot.items.filter((item) => item.body.kind === 'status' && item.body.tone === 'error') @@ -268,7 +269,7 @@ it.each(['spawn', 'open'] as const)( expect(result.prompt?.outcome).toBe('journaled') if (stage === 'spawn') { await vi.waitFor(async () => - expect((await rig.host.journalSnapshot(SESSION)).submissions).toMatchObject([ + expect((await readTestHostJournal(rig.host, SESSION)).submissions).toMatchObject([ { dispatchState: 'rejected' } ]) ) @@ -278,7 +279,7 @@ it.each(['spawn', 'open'] as const)( await vi.waitFor(() => expect(rig.adapter.dispatch).toHaveBeenCalledOnce()) expect(rig.adapter.acquire).toHaveBeenCalledTimes(2) } - expect((await rig.host.journalSnapshot(SESSION)).submissions).toHaveLength(1) + expect((await readTestHostJournal(rig.host, SESSION)).submissions).toHaveLength(1) } ) @@ -314,7 +315,7 @@ it('replays a lost reply for a prompted folder launch without another message or effort: 'high', serviceTier: 'fast' }) - expect((await rig.host.journalSnapshot(SESSION)).submissions).toHaveLength(1) + expect((await readTestHostJournal(rig.host, SESSION)).submissions).toHaveLength(1) }) it.each([true, false])( @@ -406,7 +407,7 @@ it.each(['before', 'after'] as const)( await child() await rig.host.close(SESSION, 'user-close') await rig.host.revealSession(SESSION) - const snapshot = await rig.host.journalSnapshot(SESSION) + const snapshot = await readTestHostJournal(rig.host, SESSION) expect(snapshot.submissions).toMatchObject([ { source: { kind: 'user' }, @@ -415,7 +416,7 @@ it.each(['before', 'after'] as const)( } ]) expect( - rig.host.collaboratorsForTests().sessions.get(SESSION)?.journal.queuedMessages.list() + await rig.host.collaboratorsForTests().sessions.get(SESSION)?.journal.queuedMessages.list() ).toHaveLength(1) expect(rig.adapter.dispatch).not.toHaveBeenCalled() } @@ -437,7 +438,7 @@ it('a tab publication failure after commit never calls the prompt undelivered', rpcContext(runtime, CLI) ) ).rejects.toThrow() - expect((await rig.host.journalSnapshot(SESSION)).submissions).toMatchObject([ + expect((await readTestHostJournal(rig.host, SESSION)).submissions).toMatchObject([ { source: { kind: 'user' }, dispatchState: 'pending' } ]) expect(rig.adapter.acquire).not.toHaveBeenCalled() diff --git a/src/main/runtime/rpc/methods/agent-launch/agent-launch-handoff-card.test.ts b/src/main/runtime/rpc/methods/agent-launch/agent-launch-handoff-card.test.ts index c091e03ce6e..b593af2b064 100644 --- a/src/main/runtime/rpc/methods/agent-launch/agent-launch-handoff-card.test.ts +++ b/src/main/runtime/rpc/methods/agent-launch/agent-launch-handoff-card.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../../native-chat/agent-session-journal/journal-observation.test-support' import '../../unused-default-rpc-methods.test-fixture' /** An agent's own `orca agent launch` leaves one card in its chat; nothing else does, and the card never gates the launch. */ @@ -107,7 +108,7 @@ function nextOperationId(): string { } async function cards() { - return (await rig.host.journalSnapshot(CALLER)).items.filter( + return (await readTestHostJournal(rig.host, CALLER)).items.filter( (item) => item.body.kind === 'agent-handoff' ) } diff --git a/src/main/runtime/rpc/methods/orchestration/agent-message-command-contract.test.ts b/src/main/runtime/rpc/methods/orchestration/agent-message-command-contract.test.ts index b35ab86f247..5aeab9279a2 100644 --- a/src/main/runtime/rpc/methods/orchestration/agent-message-command-contract.test.ts +++ b/src/main/runtime/rpc/methods/orchestration/agent-message-command-contract.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../../native-chat/agent-session-journal/journal-observation.test-support' import { randomUUID } from 'node:crypto' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -461,12 +462,12 @@ it.each([ if (command !== 'dispatch') { await eventually(async () => expect( - (await current.host.journalSnapshot(COORD)).items.filter( + (await readTestHostJournal(current.host, COORD)).items.filter( (item) => item.body.kind === 'agent-start' ) ).toHaveLength(1) ) - const fact = (await current.host.journalSnapshot(COORD)).items.find( + const fact = (await readTestHostJournal(current.host, COORD)).items.find( (item) => item.body.kind === 'agent-start' ) if (!fact) { @@ -518,7 +519,7 @@ it.each([ expect(output.mock.lastCall?.[0].id).not.toBe(resultBeforeReplay.id) expect(placement.placeWorkerAgent).toHaveBeenCalledTimes(placed) expect( - (await current.host.journalSnapshot(COORD)).items.filter( + (await readTestHostJournal(current.host, COORD)).items.filter( (item) => item.body.kind === 'agent-start' ) ).toEqual([fact]) @@ -552,8 +553,9 @@ it.each([ await eventually(async () => { // Each message is its own chat message, one per turn: the coordinator accepts the one it was // handed so the next is sent, and any still behind a turn wait as cards. - for (const { clientMessageId, dispatchState } of (await current.host.journalSnapshot(COORD)) - .submissions) { + for (const { clientMessageId, dispatchState } of ( + await readTestHostJournal(current.host, COORD) + ).submissions) { if (dispatchState === 'pending') { await current.host.settleLateDispatch({ sessionId: COORD, @@ -568,7 +570,7 @@ it.each([ } const page = await current.host.history({ sessionId: COORD, direction: 'tail' }) const bodies = [ - ...(await current.host.journalSnapshot(COORD)).items.map(({ body }) => body), + ...(await readTestHostJournal(current.host, COORD)).items.map(({ body }) => body), ...((page.ok ? page.page.queuedMessages : undefined) ?? []).map(({ body }) => body) ] const mail = bodies.flatMap((body) => @@ -595,11 +597,11 @@ it.each([ }) ) }) - const saved = (await current.host.journalSnapshot(COORD)).items + const saved = (await readTestHostJournal(current.host, COORD)).items await current.quitRestartHostProcess() await closeTestJournalHostDatabase(current.root) current.crashRestartHostProcess() - const reopened = (await current.host.journalSnapshot(COORD)).items + const reopened = (await readTestHostJournal(current.host, COORD)).items expect( reopened.filter( ({ body }) => body.kind === 'agent-start' || (body.kind === 'message' && body.from) @@ -611,7 +613,7 @@ it.each([ ) return } - const snapshot = await current.host.journalSnapshot(WORKER) + const snapshot = await readTestHostJournal(current.host, WORKER) const history = await current.host.history({ sessionId: WORKER, direction: 'tail' }) if (!history.ok) { throw new Error('cannot reload real history') @@ -677,7 +679,7 @@ it.each([ const edited = `${raw.slice(0, raw.length - task.length)}Edited task` const fields = { messageId: queued.messageId, - expectedBodyFingerprint: journal.queuedMessages.get(queued.messageId)!.fingerprint, + expectedBodyFingerprint: (await journal.queuedMessages.get(queued.messageId))!.fingerprint, text: edited } expect( @@ -729,7 +731,7 @@ it.each([ ]) expect(requests).toContain('orchestration.send') await eventually(async () => { - const items = (await current.host.journalSnapshot(WORKER)).items + const items = (await readTestHostJournal(current.host, WORKER)).items const mail = items.filter( ({ body }) => body.kind === 'message' && @@ -740,14 +742,14 @@ it.each([ from: { senders: [expect.objectContaining({ agent: 'claude' })] } }) }) - const saved = (await current.host.journalSnapshot(WORKER)).items.filter( + const saved = (await readTestHostJournal(current.host, WORKER)).items.filter( ({ body }) => body.kind === 'message' && body.from ) await current.quitRestartHostProcess() await closeTestJournalHostDatabase(current.root) current.crashRestartHostProcess() expect( - (await current.host.journalSnapshot(WORKER)).items.filter( + (await readTestHostJournal(current.host, WORKER)).items.filter( ({ body }) => body.kind === 'message' && body.from ) ).toEqual(saved) diff --git a/src/main/runtime/rpc/methods/orchestration/structured-workers/orchestration-structured-worker-redrive.test.ts b/src/main/runtime/rpc/methods/orchestration/structured-workers/orchestration-structured-worker-redrive.test.ts index fd728382b4c..fbfce18e4b7 100644 --- a/src/main/runtime/rpc/methods/orchestration/structured-workers/orchestration-structured-worker-redrive.test.ts +++ b/src/main/runtime/rpc/methods/orchestration/structured-workers/orchestration-structured-worker-redrive.test.ts @@ -1,3 +1,7 @@ +import { + readTestHostJournal, + readTestJournalItems +} from '../../../../../native-chat/agent-session-journal/journal-observation.test-support' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { getDefaultWorkspaceSession } from '../../../../../../shared/constants' import { partialRuntimeStore } from '../../../../orca-runtime-tests/runtime-store-partial-fixture' @@ -671,7 +675,7 @@ describe('the structured redrive publication edge', () => { vi.advanceTimersByTime(300) expect(onJournalActivity).toHaveBeenCalledExactlyOnceWith(identity.sessionId) - expect(journal.snapshot().items).toEqual([]) + expect(await readTestJournalItems(journal)).toEqual([]) }) it('still re-evaluates a journal that never goes quiet', async () => { @@ -700,7 +704,7 @@ describe('the structured redrive publication edge', () => { await reattach(identity.sessionId) const snapshot = vi.spyOn(host.collaboratorsForTests().subscribers, 'snapshot') await host.close(identity.sessionId, 'evict') - await host.journalSnapshot(identity.sessionId) + await readTestHostJournal(host, identity.sessionId) vi.advanceTimersByTime(300) onJournalActivity.mockClear() diff --git a/src/main/runtime/rpc/methods/orchestration/structured-workers/orchestration-structured-worker-retry.test.ts b/src/main/runtime/rpc/methods/orchestration/structured-workers/orchestration-structured-worker-retry.test.ts index 722dd667f05..84ae84e3913 100644 --- a/src/main/runtime/rpc/methods/orchestration/structured-workers/orchestration-structured-worker-retry.test.ts +++ b/src/main/runtime/rpc/methods/orchestration/structured-workers/orchestration-structured-worker-retry.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../../../native-chat/agent-session-journal/journal-observation.test-support' import '../../../unused-default-rpc-methods.test-fixture' import { afterEach, beforeEach, expect, it, vi } from 'vitest' import { @@ -128,13 +129,13 @@ it('starts only for its agent-sourced preamble and observes it after a transient const { created, deliverPreamble } = await createWorker() const sessionId = created.identity.sessionId expect(rig.adapter.acquire).not.toHaveBeenCalled() - expect((await rig.host.journalSnapshot(sessionId)).submissions).toEqual([]) + expect((await readTestHostJournal(rig.host, sessionId)).submissions).toEqual([]) vi.spyOn(rig.host.deps.adapter, 'open').mockRejectedValueOnce(overloaded()) const result = await deliverPreamble() expect(result.structuredTurnStart).toEqual({ verdict: 'observed' }) expect(rig.adapter.acquire).toHaveBeenCalledTimes(2) expect(rig.adapter.dispatch).toHaveBeenCalledOnce() - const snapshot = await rig.host.journalSnapshot(sessionId) + const snapshot = await readTestHostJournal(rig.host, sessionId) expect(snapshot.submissions).toMatchObject([ { dispatchState: 'accepted', @@ -164,7 +165,7 @@ it('retains a refused first task and worker identity while stopping the failed p expect(error).toMatchObject({ code: 'dispatch_preamble_undelivered' }) expect(rig.adapter.acquire).toHaveBeenCalledTimes(5) expect(rig.adapter.dispatch).not.toHaveBeenCalled() - const snapshot = await rig.host.journalSnapshot(sessionId) + const snapshot = await readTestHostJournal(rig.host, sessionId) expect(snapshot.submissions).toHaveLength(1) expect(snapshot.submissions).toMatchObject([ { dispatchState: 'rejected', source: { kind: 'agent' } } @@ -180,7 +181,7 @@ it('retains a refused first task and worker identity while stopping the failed p expect(rig.store.listVisibleSessionIds()).toContain(sessionId) expect(workerRuntime.forgetStructuredSessionMail).not.toHaveBeenCalled() expect(workerRuntime.retireStructuredAgentSessionTabFromSnapshot).not.toHaveBeenCalled() - expect((await rig.host.journalSnapshot(sessionId)).submissions).toMatchObject([ + expect((await readTestHostJournal(rig.host, sessionId)).submissions).toMatchObject([ { dispatchState: 'rejected', source: { kind: 'agent' } } ]) }) diff --git a/src/main/runtime/rpc/methods/orchestration/worker-start-card-scenario.test-fixture.ts b/src/main/runtime/rpc/methods/orchestration/worker-start-card-scenario.test-fixture.ts index 125defba27f..b77dead420c 100644 --- a/src/main/runtime/rpc/methods/orchestration/worker-start-card-scenario.test-fixture.ts +++ b/src/main/runtime/rpc/methods/orchestration/worker-start-card-scenario.test-fixture.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../../native-chat/agent-session-journal/journal-observation.test-support' import { expect, type MockInstance } from 'vitest' import type { createOrchestrationRpcHarness } from './rpc-test-harness' import type { QueuedMessageTestRig } from '../../../../native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig.test-fixture' @@ -22,7 +23,7 @@ export async function verifyWorkerStartCardScenario(input: { append: MockInstance }): Promise { const { current, scenario, before, task, federated, db, append } = input - const snapshot = await current.host.journalSnapshot(COORD) + const snapshot = await readTestHostJournal(current.host, COORD) const cards = snapshot.items.filter((item) => item.body.kind === 'agent-start') if (scenario !== 'success' && scenario !== 'federated') { expect(cards).toHaveLength(0) @@ -57,7 +58,7 @@ export async function verifyWorkerStartCardScenario(input: { current.host.appendAgentStart(destination, card.body, card.observedAt + 20) ]) expect( - (await current.host.journalSnapshot(COORD)).items.filter( + (await readTestHostJournal(current.host, COORD)).items.filter( (item) => item.body.kind === 'agent-start' ) ).toEqual(cards) @@ -73,7 +74,9 @@ export async function verifyWorkerStartCardScenario(input: { // Full task survives removal of the orchestration rows it came from. db.resetAll() expect( - (await current.host.journalSnapshot(COORD)).items.find((item) => item.itemId === card.itemId) + (await readTestHostJournal(current.host, COORD)).items.find( + (item) => item.itemId === card.itemId + ) ).toEqual(card) if (scenario === 'success') { const context = await current.host.continuationContext({ @@ -92,7 +95,7 @@ export async function verifyWorkerStartCardScenario(input: { await current.quitRestartHostProcess() await closeTestJournalHostDatabase(current.root) current.crashRestartHostProcess() - const reopened = await current.host.journalSnapshot(COORD) + const reopened = await readTestHostJournal(current.host, COORD) expect(reopened.items.map(({ itemId }) => itemId)).toEqual( snapshot.items.map(({ itemId }) => itemId) ) @@ -161,6 +164,8 @@ export async function verifyWorkerStartCardScenario(input: { } await current.host.appendAgentStart(destination, card.body, card.observedAt + 30) expect( - (await current.host.journalSnapshot(COORD)).items.some((item) => item.itemId === card.itemId) + (await readTestHostJournal(current.host, COORD)).items.some( + (item) => item.itemId === card.itemId + ) ).toBe(false) } diff --git a/src/main/runtime/rpc/methods/orchestration/worker-start-card.test.ts b/src/main/runtime/rpc/methods/orchestration/worker-start-card.test.ts index 3e5d8ab5e86..6b48c7523d9 100644 --- a/src/main/runtime/rpc/methods/orchestration/worker-start-card.test.ts +++ b/src/main/runtime/rpc/methods/orchestration/worker-start-card.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../../native-chat/agent-session-journal/journal-observation.test-support' import { verifyWorkerStartCardScenario } from './worker-start-card-scenario.test-fixture' import { expect, it, vi } from 'vitest' import { partialRuntimeStore } from '../../../orca-runtime-tests/runtime-store-partial-fixture' @@ -260,7 +261,7 @@ it.each([ } ) } - const before = await current.host.journalSnapshot(COORD) + const before = await readTestHostJournal(current.host, COORD) const rpc = h.findMethod(method) const running = rpc.handler(rpc.params?.parse(admitted.request.params), { runtime, @@ -341,7 +342,7 @@ it.each([ ) { await eventually(async () => expect( - (await current.host.journalSnapshot(COORD)).items.some( + (await readTestHostJournal(current.host, COORD)).items.some( (item) => item.body.kind === 'agent-start' ) ).toBe(true) @@ -383,7 +384,7 @@ it.each([ remoteReady && !['federated-no-card', 'federated-pending-card', 'federated-failed-card'].includes(scenario) ) { - const start = (await current.host.journalSnapshot(COORD)).items.find( + const start = (await readTestHostJournal(current.host, COORD)).items.find( ({ body }) => body.kind === 'agent-start' ) expect(start?.body).toMatchObject({ worker: { agent: expectedAgent } }) @@ -393,7 +394,7 @@ it.each([ if ( ['federated-no-card', 'federated-pending-card', 'federated-failed-card'].includes(scenario) ) { - expect((await current.host.journalSnapshot(COORD)).items).toEqual(before.items) + expect((await readTestHostJournal(current.host, COORD)).items).toEqual(before.items) } return } diff --git a/src/main/runtime/rpc/methods/session-tabs/structured-chat-tab-table.test-fixture.ts b/src/main/runtime/rpc/methods/session-tabs/structured-chat-tab-table.test-fixture.ts index 74d32254a11..959ccfdfa50 100644 --- a/src/main/runtime/rpc/methods/session-tabs/structured-chat-tab-table.test-fixture.ts +++ b/src/main/runtime/rpc/methods/session-tabs/structured-chat-tab-table.test-fixture.ts @@ -20,17 +20,17 @@ import { } from '../../../../native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture' import { setStructuredAgentSessionHost } from '../../../../native-chat/agent-session-wire/host/structured-agent-session-registry' import type { AgentSessionRecordStore } from '../../../agent-session/records/agent-session-record-store' -import { openTestAgentSessionRecordStore } from '../../../agent-session/records/agent-session-record-store-test-harness' +import { + openTestAgentSessionRecordStore, + openTestAgentSessionDatabase +} from '../../../agent-session/records/agent-session-record-store-test-harness' import { OrcaRuntimeService } from '../../../orca-runtime' import { RpcDispatcher } from '../../dispatcher' import type { RpcDispatchStreamingOptions } from '../../dispatcher-stream-options' import { SESSION_TAB_METHODS } from './session-tabs' import { STRUCTURED_AGENT_SESSION_METHODS } from '../structured-sessions/structured-agent-session' import { commitStructuredAgentSessionCreate } from '../structured-sessions/creation/structured-agent-session-create' -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' @@ -100,7 +100,7 @@ export async function openHost(): Promise { logger: createStructuredAgentSessionLogger(), store, adapter: providerAdapter(), - journalDatabase: openTestJournalHostDatabase(directory), + journalDatabase: await openTestAgentSessionDatabase(directory), claimKeyId: 'key', now: () => HOST_TEST_NOW, mintSpawnToken: () => `spawn-${acquisitions}` diff --git a/src/main/runtime/rpc/methods/session-tabs/structured-chat-tab-table.test.ts b/src/main/runtime/rpc/methods/session-tabs/structured-chat-tab-table.test.ts index c2b3102ad67..4ca6e68346e 100644 --- a/src/main/runtime/rpc/methods/session-tabs/structured-chat-tab-table.test.ts +++ b/src/main/runtime/rpc/methods/session-tabs/structured-chat-tab-table.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalSnapshot } from '../../../../native-chat/agent-session-journal/journal-observation.test-support' import { describe, expect, it, vi } from 'vitest' import { CLAUDE_STRUCTURED_AGENT_SESSION_RUNTIME_CAPABILITY, @@ -87,12 +88,12 @@ describe('a chat tab across /clear', () => { } expect((await call('agentSession.conversationCommand', params)).result?.ok).not.toBe(true) lost.mockRestore() - const landed = journal.snapshot() + const landed = await readTestJournalSnapshot(journal) expect(await call('agentSession.conversationCommand', params)).toMatchObject({ ok: true, result: { ok: true } }) - expect(journal.snapshot()).toEqual(landed) + expect(await readTestJournalSnapshot(journal)).toEqual(landed) expect((await snapshot()).tabs.map((tab) => tab.id)).toEqual([ `agent-session:${HOST_TEST_SESSION}` ]) diff --git a/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-lifecycle.test.ts b/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-lifecycle.test.ts index 8a5f1c3ebc4..459422a4d00 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-lifecycle.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-lifecycle.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalItems } from '../../../../../native-chat/agent-session-journal/journal-observation.test-support' import { journalInputSourceBody } from '../../../../../native-chat/agent-session-journal/input-order/journal-input-source' import { streamState, @@ -140,9 +141,7 @@ async function failedImage() { return { id, body, - card: journal() - .queuedMessages.list() - .find((row) => row.state === 'returned')! + card: (await journal().queuedMessages.list()).find((row) => row.state === 'returned')! } } @@ -158,7 +157,7 @@ it('publishes a complete image row to old readers before it has a submission', a expect(await call('agentSession.send', params, OLD)).toMatchObject({ result: { ok: true, value: { queued: { messageId: id } } } }) - expect(journal().submission(id)).toBeUndefined() + expect(await journal().reads.submission(id)).toBeUndefined() expect((await history()).page.items).toContainEqual( expect.objectContaining({ itemId: agentJournalSubmissionKey(id), body }) ) @@ -213,8 +212,8 @@ it('installs old-reader source and exact controls through RPC, disk reopen, Send ) const frames = await openStream('agentSession.subscribe', { sessionId: SESSION }, OLD) const modernFrames = await openStream('agentSession.subscribe', { sessionId: SESSION }, MODERN) - const immutable = journal().submission(id) - const receipt = journal().receiptFor(id) + const immutable = await journal().reads.submission(id) + const receipt = await journal().reads.receiptFor(id) const sendFields = { messageId: card.messageId } const sendParams = { envelope: rig.envelope(sendFields, 'agentSession.queuedMessageSend', hostTestOperationId()), @@ -232,9 +231,9 @@ it('installs old-reader source and exact controls through RPC, disk reopen, Send expect(rig.dispatch.mock.calls.at(-1)?.[0]).toMatchObject({ body }) expect((await rig.handoff(card.messageId))?.options).toEqual({ model: 'default' }) expect((await history()).page.queuedMessages).toEqual([]) - expect(journal().submission(id)).toEqual(immutable) - expect(journal().receiptFor(id)).toEqual(receipt) - expect(journal().submissionBody(id)).toEqual(body) + expect(await journal().reads.submission(id)).toEqual(immutable) + expect(await journal().reads.receiptFor(id)).toEqual(receipt) + expect(await journal().reads.submissionBody(id)).toEqual(body) expect( frames.some( (frame) => @@ -263,9 +262,9 @@ it('installs old-reader source and exact controls through RPC, disk reopen, Send rig.failNextStart(new Error('again')) const deleted = await sendImage(body) await eventually(async () => expect((await rig!.drafts()).length).toBeGreaterThan(0)) - const exact = journal() - .queuedMessages.list() - .find((row) => row.messageId === deleted || row.recovers === deleted)! + const exact = (await journal().queuedMessages.list()).find( + (row) => row.messageId === deleted || row.recovers === deleted + )! await openStream('agentSession.subscribe', { sessionId: SESSION }, OLD) const fields = { messageId: exact.messageId } expect( @@ -303,7 +302,7 @@ it('keeps a hidden original pageable beyond1024 rows and preserves edited recove const fence = rig.store.getRecord(SESSION)!.lease.runtimeFence await j.rollEpoch('unreconcilable_prefix', fence) expect( - j.snapshot().items.find((item) => item.itemId === agentJournalSubmissionKey(id)) + (await readTestJournalItems(j)).find((item) => item.itemId === agentJournalSubmissionKey(id)) ).toBeUndefined() for (let i = 0; i < 1030; i++) { await j.appendItem( @@ -386,7 +385,7 @@ it('keeps a hidden original pageable beyond1024 rows and preserves edited recove expect( selected.page.items.find((item) => item.itemId === agentJournalSubmissionKey(id)) ).toBeUndefined() - expect(j.queuedMessages.get(card.messageId)).toMatchObject({ + expect(await j.queuedMessages.get(card.messageId)).toMatchObject({ body: { blocks: [{ type: 'text', text: 'changed' }, body.blocks[1]] }, options: { model: 'default' } }) @@ -404,24 +403,24 @@ it('keeps a hidden original pageable beyond1024 rows and preserves edited recove })[0] ).not.toHaveProperty('waitsForAgent') await rig.settleAccepted(id, 'late-original') - expect(j.submission(id)?.dispatchState).toBe('rejected') - expect(j.queuedMessages.get(card.messageId)?.body.blocks[0]).toEqual({ + expect((await j.reads.submission(id))?.dispatchState).toBe('rejected') + expect((await j.queuedMessages.get(card.messageId))?.body.blocks[0]).toEqual({ type: 'text', text: 'changed' }) expect( - j.snapshot().items.find((item) => item.itemId === agentJournalSubmissionKey(id)) + (await readTestJournalItems(j)).find((item) => item.itemId === agentJournalSubmissionKey(id)) ).toBeUndefined() await rig.quitRestartHostProcess() await history() - expect(journal().queuedMessages.get(card.messageId)?.body.blocks[0]).toEqual({ + expect((await journal().queuedMessages.get(card.messageId))?.body.blocks[0]).toEqual({ type: 'text', text: 'changed' }) expect( - journal() - .snapshot() - .items.find((item) => item.itemId === agentJournalSubmissionKey(id)) + (await readTestJournalItems(journal())).find( + (item) => item.itemId === agentJournalSubmissionKey(id) + ) ).toBeUndefined() }) @@ -449,12 +448,16 @@ it.each([false, true])( expect((await rig!.handoff(card.messageId))?.dispatchState).toBe('rejected') ) presentedId = (await rig.handoff(card.messageId))!.clientMessageId - expect(journal().submission(presentedId)).toMatchObject({ queuedMessageId: card.messageId }) - expect(journal().submission(presentedId)).not.toHaveProperty('keptAsQueuedMessageId') + expect(await journal().reads.submission(presentedId)).toMatchObject({ + queuedMessageId: card.messageId + }) + expect(await journal().reads.submission(presentedId)).not.toHaveProperty( + 'keptAsQueuedMessageId' + ) } const before = await history() - const immutable = journal().submission(presentedId) - const receipt = journal().receiptFor(presentedId) + const immutable = await journal().reads.submission(presentedId) + const receipt = await journal().reads.receiptFor(presentedId) expect( projectStructuredAgentSessionMessages(before.page.items, [], before.page.submissions) ).toContainEqual(expect.objectContaining({ id: agentJournalSubmissionKey(presentedId) })) @@ -476,7 +479,7 @@ it.each([false, true])( const frames = await openStream('agentSession.subscribe', { sessionId: SESSION }, OLD) const fields = { messageId: card.messageId, - expectedBodyFingerprint: journal().queuedMessages.get(card.messageId)!.fingerprint, + expectedBodyFingerprint: (await journal().queuedMessages.get(card.messageId))!.fingerprint, text: 'edited original' } expect( @@ -490,8 +493,8 @@ it.each([false, true])( ) ).toMatchObject({ result: { ok: true, value: { status: 'updated' } } }) const changed = await history() - expect(journal().submission(presentedId)).toEqual(immutable) - expect(journal().receiptFor(presentedId)).toEqual(receipt) + expect(await journal().reads.submission(presentedId)).toEqual(immutable) + expect(await journal().reads.receiptFor(presentedId)).toEqual(receipt) expect( projectStructuredAgentSessionMessages(changed.page.items, [], changed.page.submissions).some( (item) => item.id === agentJournalSubmissionKey(presentedId) @@ -548,8 +551,8 @@ it.each([false, true])( expect(mobileQueuedMessageCards(live.queuedMessages ?? null, live.submissions)).toMatchObject([ { messageId: card.messageId, command: true, text: expect.stringContaining('edited original') } ]) - expect(journal().submissionBody(presentedId)).toEqual(body) - expect(journal().submissionBody(id)).toEqual(body) + expect(await journal().reads.submissionBody(presentedId)).toEqual(body) + expect(await journal().reads.submissionBody(id)).toEqual(body) expect((await history({ direction: 'tail' }, false)).page.queuedMessages).toMatchObject([ { messageId: card.messageId, @@ -575,7 +578,7 @@ it.each([false, true])( await eventually(async () => expect(await rig!.handoff(card.messageId)).toBeDefined()) const handed = (await rig.handoff(card.messageId))! expect(handed.clientMessageId).not.toBe(id) - const handedBody = journal().submissionBody(handed.clientMessageId) + const handedBody = await journal().reads.submissionBody(handed.clientMessageId) expect(handedBody).toEqual({ ...body, blocks: [{ type: 'text', text: 'edited original' }, body.blocks[1]] @@ -588,9 +591,9 @@ it.each([false, true])( const reopened = (await history()).page expect(reopened.queuedMessages).toMatchObject([{ messageId: card.messageId, state: 'waiting' }]) expect(reopened.queuedMessages).toHaveLength(1) - expect(journal().submission(id)?.dispatchState).toBe('rejected') - expect(journal().submissionBody(handed.clientMessageId)).toEqual(handedBody) - expect(journal().queuedMessages.get(card.messageId)?.body).toEqual(handedBody) + expect((await journal().reads.submission(id))?.dispatchState).toBe('rejected') + expect(await journal().reads.submissionBody(handed.clientMessageId)).toEqual(handedBody) + expect((await journal().queuedMessages.get(card.messageId))?.body).toEqual(handedBody) } ) @@ -641,7 +644,10 @@ it('keeps an image-only waiting queue and pause visible through Resume and a fai const failed = (await history()).page expect(failed.queuePause).toBeNull() expect(failed.queuedMessages).toMatchObject([{ messageId: id, state: 'returned' }]) - expect(journal().queuedMessages.get(id)).toMatchObject({ body, options: { model: 'default' } }) + expect(await journal().queuedMessages.get(id)).toMatchObject({ + body, + options: { model: 'default' } + }) const handoff = (await rig.handoff(id))! expect( projectStructuredAgentSessionMessages(failed.items, [], failed.submissions) @@ -660,7 +666,7 @@ it('keeps an image-only waiting queue and pause visible through Resume and a fai ) ).toMatchObject({ result: { ok: true } }) expect((await history()).page.queuedMessages).toEqual([]) - expect(journal().submissionBody(handoff.clientMessageId)).toEqual(body) + expect(await journal().reads.submissionBody(handoff.clientMessageId)).toEqual(body) }) it.each([ @@ -692,7 +698,7 @@ it.each([ await rig.stop() await rig.host.flushStreamedEvents(SESSION) await rig.host.collaboratorsForTests().serialize(SESSION, async () => {}) - expect(journal().queuedMessages.list()).toEqual([]) + expect(await journal().queuedMessages.list()).toEqual([]) if (edit) { appendTestJournalQueueRow( openTestJournalHostDatabase(rig.root).db, @@ -710,9 +716,9 @@ it.each([ ) finishTestJournalHistoricalInputs(journal()) } - const card = journal().queuedMessages.get(`recovery:${id}`) - const original = journal().submission(id)! - const receipt = journal().receiptFor(id) + const card = await journal().queuedMessages.get(`recovery:${id}`) + const original = (await journal().reads.submission(id))! + const receipt = await journal().reads.receiptFor(id) expect(original).toMatchObject({ dispatchState: 'unknown', providerItemId: null }) await openStream('agentSession.subscribe', { sessionId: SESSION }, OLD) await rig.quitRestartHostProcess() @@ -731,7 +737,7 @@ it.each([ expect(await call('agentSession.queuedMessageUpdate', params, MODERN)).toMatchObject({ result: { ok: true, value: { status: 'updated' } } }) - const edited = journal().queuedMessages.get(`recovery:${id}`)! + const edited = (await journal().queuedMessages.get(`recovery:${id}`))! expect(edited.body).toEqual({ ...body, blocks: [ @@ -744,7 +750,7 @@ it.each([ expect(await call('agentSession.queuedMessageUpdate', params, MODERN)).toMatchObject({ result: { ok: true } }) - expect(journal().queuedMessages.get(`recovery:${id}`)).toEqual(edited) + expect(await journal().queuedMessages.get(`recovery:${id}`)).toEqual(edited) } const input = structuredClone(journal().queuedMessages.displayState().inputs.sources.get(id)) expect(journalInputSourceBody(input)).toEqual(body) @@ -752,19 +758,19 @@ it.each([ original.options ) expect(input?.fence).toBe(original.fence) - const beforeProof = journal().queuedMessages.get(`recovery:${id}`) - expect(journal().submission(id)).toEqual(original) - expect(journal().receiptFor(id)).toEqual(receipt) - expect(journal().submissionBody(id)).toEqual(body) + const beforeProof = await journal().queuedMessages.get(`recovery:${id}`) + expect(await journal().reads.submission(id)).toEqual(original) + expect(await journal().reads.receiptFor(id)).toEqual(receipt) + expect(await journal().reads.submissionBody(id)).toEqual(body) // The existing rig delivers a Codex identity through host.settleLateDispatch, never a journal receipt edit. await rig.settleAccepted(id, 'pinned-original-late-echo') - expect(journal().submission(id)).toMatchObject({ + expect(await journal().reads.submission(id)).toMatchObject({ dispatchState: 'accepted', options: original.options, payloadFingerprint: original.payloadFingerprint, fence: rig.store.getRecord(SESSION)!.lease.runtimeFence }) - const acceptedReceipt = journal().receiptFor(id) + const acceptedReceipt = await journal().reads.receiptFor(id) expect(acceptedReceipt).toMatchObject({ clientMessageId: id, providerItemId: agentJournalItemKey({ @@ -776,18 +782,18 @@ it.each([ }) expect(acceptedReceipt).not.toEqual(receipt) await rig.settleAccepted(id, 'pinned-original-late-echo') - expect(journal().receiptFor(id)).toEqual(acceptedReceipt) + expect(await journal().reads.receiptFor(id)).toEqual(acceptedReceipt) expect(journal().queuedMessages.displayState().inputs.sources.get(id)).toEqual(input) - expect(journal().submissionBody(id)).toEqual(body) + expect(await journal().reads.submissionBody(id)).toEqual(body) expect(rig.dispatch).toHaveBeenCalledTimes(1) if (card) { - expect(journal().queuedMessages.get(`recovery:${id}`)).toEqual(beforeProof) + expect(await journal().queuedMessages.get(`recovery:${id}`)).toEqual(beforeProof) await history() } else { - expect(journal().queuedMessages.get(`recovery:${id}`)).toBeNull() + expect(await journal().queuedMessages.get(`recovery:${id}`)).toBeNull() } await rig.quitRestartHostProcess() await history() - expect(journal().queuedMessages.get(`recovery:${id}`)).toEqual(beforeProof) + expect(await journal().queuedMessages.get(`recovery:${id}`)).toEqual(beforeProof) } ) diff --git a/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-wire-contract.test.ts b/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-wire-contract.test.ts index db086ea6968..593aeb17abd 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-wire-contract.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-wire-contract.test.ts @@ -79,8 +79,8 @@ it('publishes the complete original and exact compact retry tuple through instal expect(sent.ok).toBe(true) await eventually(async () => expect(await rig!.drafts()).toHaveLength(1)) const journal = rig.host.collaboratorsForTests().sessions.get(SESSION)!.journal - const original = journal.submission(id)! - const receipt = journal.receiptFor(id) + const original = (await journal.reads.submission(id))! + const receipt = await journal.reads.receiptFor(id) const epoch = journal.cursor().epoch const reason = "Codex couldn't restart. Send your message to try again." const rejection = { @@ -183,14 +183,14 @@ it('publishes the complete original and exact compact retry tuple through instal throw new Error('history refused') } expect(rpc.result).toStrictEqual(expected) - expect(journal.item(agentJournalSubmissionKey(id))).toStrictEqual(expectedSource) - expect(journal.submission(id)).toStrictEqual({ + expect(await journal.reads.item(agentJournalSubmissionKey(id))).toStrictEqual(expectedSource) + expect(await journal.reads.submission(id)).toStrictEqual({ ...expectedSubmission, resolvedAt: original.resolvedAt, keptAsQueuedMessageId: id }) - expect(journal.submissionBody(id)).toStrictEqual(body) - expect(journal.receiptFor(id)).toStrictEqual(receipt) + expect(await journal.reads.submissionBody(id)).toStrictEqual(body) + expect(await journal.reads.receiptFor(id)).toStrictEqual(receipt) const frames = await openStream('agentSession.subscribe', { sessionId: SESSION }, OLD) const { queuedMessages, @@ -292,8 +292,7 @@ it('keeps a saved physical hold independent while its own rejected retry blocks const held = await published() expect(held).toHaveProperty('page.queuedMessages', [ { messageId: saved, position: -1, body, state: 'waiting', paused: true }, - ...j.queuedMessages - .list() + ...(await j.queuedMessages.list()) .filter((row) => row.messageId !== saved) .map((row) => ({ messageId: row.messageId, @@ -302,8 +301,8 @@ it('keeps a saved physical hold independent while its own rejected retry blocks state: 'waiting' })) ]) - const immutable = j.submission(id) - const receipt = j.receiptFor(id) + const immutable = await j.reads.submission(id) + const receipt = await j.reads.receiptFor(id) await rig.quitRestartHostProcess() setStructuredAgentSessionHost(rig.host) rig.failNextStart(new Error('saved choice own retry failed')) @@ -318,7 +317,7 @@ it('keeps a saved physical hold independent while its own rejected retry blocks await eventually(async () => expect((await rig!.handoff(saved))?.dispatchState).toBe('rejected')) const rejected = await published() const state = rig.host.collaboratorsForTests().sessions.get(SESSION)!.journal - const physical = state.queuedMessages.get(saved)! + const physical = (await state.queuedMessages.get(saved))! expect(physical.state).toBe('returned') expect(physical.recovers).toBe(id) expect(physical.consumedAs).not.toBe(id) @@ -338,9 +337,9 @@ it('keeps a saved physical hold independent while its own rejected retry blocks expect(rig.dispatch).toHaveBeenCalledTimes(1) await published() await rig.settleAccepted(id, 'original-positive-proof-independent-of-failed-retry') - expect(state.queuedMessages.get(saved)).toStrictEqual(physical) - expect(state.submissionBody(id)).toStrictEqual(body) - expect(state.submission(id)?.payloadFingerprint).toBe(immutable?.payloadFingerprint) - expect(state.receiptFor(id)).not.toEqual(receipt) + expect(await state.queuedMessages.get(saved)).toStrictEqual(physical) + expect(await state.reads.submissionBody(id)).toStrictEqual(body) + expect((await state.reads.submission(id))?.payloadFingerprint).toBe(immutable?.payloadFingerprint) + expect(await state.reads.receiptFor(id)).not.toEqual(receipt) await published() }) diff --git a/src/main/runtime/rpc/methods/structured-sessions/catalog/structured-agent-session-continuation-context.test.ts b/src/main/runtime/rpc/methods/structured-sessions/catalog/structured-agent-session-continuation-context.test.ts index 142b1d3c655..b8978dbdf4d 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/catalog/structured-agent-session-continuation-context.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/catalog/structured-agent-session-continuation-context.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../../../native-chat/agent-session-journal/journal-observation.test-support' import * as fileOperations from 'node:fs/promises' import { readFile, rm } from 'node:fs/promises' import * as contextSnapshots from '../../../../../native-chat/agent-session-context-snapshots' @@ -99,7 +100,7 @@ afterEach(async () => { it('hands off the reduced saved chat after clear and rewind, budgets only the starter and overwrites its file', async () => { await restingChat() - await rig.host.journalSnapshot(SESSION) + await readTestHostJournal(rig.host, SESSION) let journal = rig.host.collaboratorsForTests().sessions.get(SESSION)!.journal const fence = rig.store.getRecord(SESSION)!.lease.runtimeFence const receipt = { write: () => {}, committed: () => {} } @@ -252,7 +253,7 @@ it('hands off the reduced saved chat after clear and rewind, budgets only the st ) await rig.restart() setStructuredAgentSessionHost(rig.host) - const snapshot = await rig.host.journalSnapshot(SESSION) + const snapshot = await readTestHostJournal(rig.host, SESSION) journal = rig.host.collaboratorsForTests().sessions.get(SESSION)!.journal await append({ ...question('pending ask'), resolution: resolution('pending') }) const params = { @@ -372,7 +373,7 @@ it('hands off the reduced saved chat after clear and rewind, budgets only the st it('hands off bounded tool evidence with the latest conversation while keeping the full saved output', async () => { await restingChat() - await rig.host.journalSnapshot(SESSION) + await readTestHostJournal(rig.host, SESSION) const journal = rig.host.collaboratorsForTests().sessions.get(SESSION)!.journal const output = `AssertionError: CURRENT PART\nReceived: ${'a'.repeat(200_000)}\nTests: 2 failed` const toolOutput = `Error: SAVED_TOOL_FAILURE\n${'b'.repeat(200_000)}\nSAVED_TOOL_OUTPUT_END` @@ -448,7 +449,7 @@ it('hands off bounded tool evidence with the latest conversation while keeping t it('hands off a readable saved conversation without granting its invalid execution lease authority', async () => { await restingChat() - const { cursor } = await rig.host.journalSnapshot(SESSION) + const { cursor } = await readTestHostJournal(rig.host, SESSION) await rig.host.flushAllStreamedEvents() await editPersistedTestAgentSessionStore(rig.root, (persisted) => { Object.assign(persisted.records[SESSION].lease, { minimumNextFence: 'bad' }) diff --git a/src/main/runtime/rpc/methods/structured-sessions/conversation/structured-agent-session-restart-unregistered-agent.test.ts b/src/main/runtime/rpc/methods/structured-sessions/conversation/structured-agent-session-restart-unregistered-agent.test.ts index 350803cc933..631f66b4bb5 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/conversation/structured-agent-session-restart-unregistered-agent.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/conversation/structured-agent-session-restart-unregistered-agent.test.ts @@ -12,10 +12,7 @@ import { agentSessionProviderHandleRoot } from '../../../../../../shared/agent-s import type { AgentSessionResumeMarker } from '../../../../../../shared/agent-session/lifecycle/agent-session-resume-marker' import { STRUCTURED_AGENT_SESSION_REGISTERED_AGENTS_RUNTIME_CAPABILITY } from '../../../../../../shared/agent-session/structured-agent-session-surface-capabilities' import { DESKTOP_RENDERER_RUNTIME_CLIENT_CAPABILITIES } from '../../../../../ipc/desktop-renderer-runtime-capabilities' -import { - closeTestJournalHostDatabase, - openTestJournalHostDatabase -} 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' import { claudeAndCodexDeclared } from '../../../../../native-chat/agent-session-wire/providers/testing/structured-agent-session-adapter-router.test-fixture' import { StructuredAgentRegistry } from '../../../../../native-chat/agent-session-wire/providers/structured-agent-registry' import { StructuredAgentSessionAdapterRouter } from '../../../../../native-chat/agent-session-wire/providers/structured-agent-session-adapter-router' @@ -25,7 +22,8 @@ import { setStructuredAgentSessionHost } from '../../../../../native-chat/agent- import { AgentSessionRecoveryCapsule } from '../../../../agent-session/records/agent-session-recovery-capsule' import { openTestAgentSessionRecordStore, - seedTestAgentSessionRecordStore + seedTestAgentSessionRecordStore, + openTestAgentSessionDatabase } from '../../../../agent-session/records/agent-session-record-store-test-harness' import { call, @@ -122,7 +120,7 @@ it.each(['empty', 'claude-codex'] as const)( store, agents, adapter: router, - journalDatabase: openTestJournalHostDatabase(root), + journalDatabase: await openTestAgentSessionDatabase(root), recoveryCapsule: capsule, claimKeyId: 'key-1', probeOwner: async () => ({ outcome: 'pid-absent' }), diff --git a/src/main/runtime/rpc/methods/structured-sessions/creation/structured-agent-session-adoption-replay.test.ts b/src/main/runtime/rpc/methods/structured-sessions/creation/structured-agent-session-adoption-replay.test.ts index 92fd6c110a1..5845ee16c16 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/creation/structured-agent-session-adoption-replay.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/creation/structured-agent-session-adoption-replay.test.ts @@ -13,7 +13,10 @@ import { } from '../../../../../../shared/agent-session/agent-session-runtime-capabilities' import { readAgentSessionRefusalReference } from '../../../../../../shared/agent-session/failures/agent-session-wire-refusals' import { readAgentSessionErrorRefusal } from '../../../../../../shared/agent-session/failures/agent-session-write-failure' -import { openTestAgentSessionRecordStore } from '../../../../agent-session/records/agent-session-record-store-test-harness' +import { + openTestAgentSessionRecordStore, + openTestAgentSessionDatabase +} from '../../../../agent-session/records/agent-session-record-store-test-harness' import type { StructuredAgentSessionAdapter } from '../../../../../native-chat/agent-session-wire/providers/structured-agent-session-adapter' import { StructuredAgentSessionHost } from '../../../../../native-chat/agent-session-wire/host/structured-agent-session-host' import { setStructuredAgentSessionHost } from '../../../../../native-chat/agent-session-wire/host/structured-agent-session-registry' @@ -23,10 +26,7 @@ import type { RpcRequest, RpcResponse } from '../../../core' import type { RpcDispatchStreamingOptions } from '../../../dispatcher-stream-options' import { RpcDispatcher } from '../../../dispatcher' import { STRUCTURED_AGENT_SESSION_METHODS } from '../structured-agent-session' -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 { claudeAndCodexAgents } from '../../../../../native-chat/agent-session-wire/providers/testing/structured-agent-session-adapter-router.test-fixture' @@ -273,7 +273,7 @@ describe('committed adopting create RPC replay', () => { logger: createStructuredAgentSessionLogger(), store, adapter: sessionAdapter, - journalDatabase: openTestJournalHostDatabase(root), + journalDatabase: await openTestAgentSessionDatabase(root), claimKeyId: 'key-1' }) setStructuredAgentSessionHost(host) diff --git a/src/main/runtime/rpc/methods/structured-sessions/creation/structured-agent-session-create-message.test.ts b/src/main/runtime/rpc/methods/structured-sessions/creation/structured-agent-session-create-message.test.ts index 8c90cf17899..4555936c710 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/creation/structured-agent-session-create-message.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/creation/structured-agent-session-create-message.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../../../native-chat/agent-session-journal/journal-observation.test-support' import '../../../unused-default-rpc-methods.test-fixture' import { afterEach, beforeEach, expect, it, vi } from 'vitest' import { STRUCTURED_AGENT_SESSION_RUNTIME_CAPABILITY } from '../../../../../../shared/agent-session/agent-session-runtime-capabilities' @@ -93,7 +94,7 @@ it('refuses a new founding operation before dispatching the same opening again', } }) expect( - (await rig.host.journalSnapshot(SESSION)).submissions.map((row) => row.clientMessageId) + (await readTestHostJournal(rig.host, SESSION)).submissions.map((row) => row.clientMessageId) ).toEqual([input.firstMessage?.clientMessageId]) expect(rig.adapter.dispatch).toHaveBeenCalledOnce() }) @@ -216,7 +217,7 @@ it.each(['firstMessage', 'options'] as const)( ok: true, result: { ok: false, refusal: { code: 'agent_session_operation_conflict' } } }) - expect((await rig.host.journalSnapshot(SESSION)).submissions).toEqual([]) + expect((await readTestHostJournal(rig.host, SESSION)).submissions).toEqual([]) expect(rig.adapter.acquire).toHaveBeenCalledOnce() } ) @@ -319,7 +320,7 @@ it.each(['options', 'firstMessage'] as const)( if (!first) { throw new Error('fixture first message missing') } - expect((await rig.host.journalSnapshot(SESSION)).submissions).toMatchObject([ + expect((await readTestHostJournal(rig.host, SESSION)).submissions).toMatchObject([ { clientMessageId: first.clientMessageId } ]) const changed = structuredAgentSessionCreateParams({ diff --git a/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest-context.test.ts b/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest-context.test.ts index 9b1bab5ff32..a35adb0c941 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest-context.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest-context.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../../../native-chat/agent-session-journal/journal-observation.test-support' import '../../../unused-default-rpc-methods.test-fixture' vi.mock('node:fs/promises', { spy: true }) import { readFile, rm } from 'node:fs/promises' @@ -29,7 +30,7 @@ import { it('hands off the reduced saved chat after clear and rewind, budgets only the starter and overwrites its file', async () => { await restingChat() - await fixture.rig.host.journalSnapshot(SESSION) + await readTestHostJournal(fixture.rig.host, SESSION) let journal = fixture.rig.host.collaboratorsForTests().sessions.get(SESSION)!.journal const fence = fixture.rig.store.getRecord(SESSION)!.lease.runtimeFence const receipt = { write: () => {}, committed: () => {} } @@ -182,7 +183,7 @@ it('hands off the reduced saved chat after clear and rewind, budgets only the st ) await fixture.rig.restart() setStructuredAgentSessionHost(fixture.rig.host) - const snapshot = await fixture.rig.host.journalSnapshot(SESSION) + const snapshot = await readTestHostJournal(fixture.rig.host, SESSION) journal = fixture.rig.host.collaboratorsForTests().sessions.get(SESSION)!.journal await append({ ...question('pending ask'), resolution: resolution('pending') }) const params = { diff --git a/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest-lease.test.ts b/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest-lease.test.ts index a25a593c94b..833faa58ee5 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest-lease.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest-lease.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../../../native-chat/agent-session-journal/journal-observation.test-support' import '../../../unused-default-rpc-methods.test-fixture' import { expect, it, vi } from 'vitest' vi.mock('node:fs/promises', { spy: true }) @@ -14,7 +15,7 @@ import { fixture, call, restingChat } from './structured-agent-session-rest-cont it('hands off a readable saved conversation without granting its invalid execution lease authority', async () => { await restingChat() - const { cursor } = await fixture.rig.host.journalSnapshot(SESSION) + const { cursor } = await readTestHostJournal(fixture.rig.host, SESSION) await fixture.rig.host.flushAllStreamedEvents() await editPersistedTestAgentSessionStore(fixture.rig.root, (persisted) => { Object.assign(persisted.records[SESSION].lease, { minimumNextFence: 'bad' }) diff --git a/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest-options.test.ts b/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest-options.test.ts index d398bf26a84..3edc0032f23 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest-options.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest-options.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../../../native-chat/agent-session-journal/journal-observation.test-support' import '../../../unused-default-rpc-methods.test-fixture' vi.mock('node:fs/promises', { spy: true }) import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../../shared/agent-session/journal/agent-session-journal-types' @@ -132,7 +133,7 @@ it('whose settlement write failed is settled by the next send, which is delivere await fixture.rig.host.waitForSendSettlement(SESSION, sent.value.clientMessageId) expect(fixture.rig.adapter.dispatch).toHaveBeenCalledTimes(2) expect( - activeStructuredAgentSessionTurnId((await fixture.rig.host.journalSnapshot(SESSION)).items) + activeStructuredAgentSessionTurnId((await readTestHostJournal(fixture.rig.host, SESSION)).items) ).not.toBe('working') }) diff --git a/src/main/runtime/rpc/methods/structured-sessions/subscriptions/structured-agent-session-receipt-frame-budget.test.ts b/src/main/runtime/rpc/methods/structured-sessions/subscriptions/structured-agent-session-receipt-frame-budget.test.ts index 248ed1703df..93e15a638f4 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/subscriptions/structured-agent-session-receipt-frame-budget.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/subscriptions/structured-agent-session-receipt-frame-budget.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalItems } from '../../../../../native-chat/agent-session-journal/journal-observation.test-support' import '../../../unused-default-rpc-methods.test-fixture' import { join } from 'node:path' import { afterEach, beforeEach, expect, it } from 'vitest' @@ -98,7 +99,7 @@ it.each(['snapshot', 'reset', 'batch'] as const)( MOBILE_BRIDGE_MAX_FRAME_BYTES ) } - expect(journal.snapshot().items).toHaveLength(200) - expect(JSON.stringify(journal.snapshot().items)).not.toContain('resolvedByDevice') + expect(await readTestJournalItems(journal)).toHaveLength(200) + expect(JSON.stringify(await readTestJournalItems(journal))).not.toContain('resolvedByDevice') } ) diff --git a/src/main/runtime/rpc/structured-worker-cli.integration.test.ts b/src/main/runtime/rpc/structured-worker-cli.integration.test.ts index 46bd584b327..6367e438f8b 100644 --- a/src/main/runtime/rpc/structured-worker-cli.integration.test.ts +++ b/src/main/runtime/rpc/structured-worker-cli.integration.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../native-chat/agent-session-journal/journal-observation.test-support' import './unused-default-rpc-methods.test-fixture' import { randomUUID } from 'node:crypto' import { mkdir, writeFile } from 'node:fs/promises' @@ -421,7 +422,7 @@ it.each(['worker-release', 'retry-of'] as const)( expect(db.getDispatchContextById(dispatchId)).toMatchObject({ status: 'failed' }) expect(db.getDispatchContextById(dispatchId)?.capability_revoked_at).not.toBeNull() expect(db.getTask(db.getDispatchContextById(dispatchId)!.task_id)?.status).toBe('failed') - const snapshot = await host.journalSnapshot(identity.sessionId) + const snapshot = await readTestHostJournal(host, identity.sessionId) expect(snapshot.submissions).toMatchObject([{ dispatchState: 'rejected' }]) expect(JSON.stringify(snapshot)).toContain('Refused fixture task') structuredWorkerIdentities.clear() diff --git a/src/main/runtime/structured-child-records-switch.test.ts b/src/main/runtime/structured-child-records-switch.test.ts index 8b5e4975afa..3ad903f24de 100644 --- a/src/main/runtime/structured-child-records-switch.test.ts +++ b/src/main/runtime/structured-child-records-switch.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../native-chat/agent-session-journal/journal-observation.test-support' // Exercise host child-work projections without a renderer. Use literal expectations: comparing with the provider tracker would share the producer's observation bugs. import { mkdtemp, rm } from 'node:fs/promises' @@ -324,7 +325,7 @@ describe('the chat strip and the session list read the same host child records', ) await vi.waitFor(async () => expect( - (await host.journalSnapshot(SESSION)).submissions.find( + (await readTestHostJournal(host, SESSION)).submissions.find( (entry) => entry.clientMessageId === clientOperationId )?.handedOverAt ).toBeDefined() From 4428e4ed3b105bd34fa2aefd97f182047937599e Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 23:11:10 -0800 Subject: [PATCH 02/45] Remove retired journal writer test machinery --- .../journal-domain-stop-notes.test.ts | 2 +- .../journal-status-cuts.test.ts | 15 +- .../replay/journal-outcome-wire.test.ts | 203 ------------------ .../replay/journal-projection-test-support.ts | 109 ---------- 4 files changed, 2 insertions(+), 327 deletions(-) delete mode 100644 src/main/native-chat/agent-session-journal/replay/journal-outcome-wire.test.ts delete mode 100644 src/main/native-chat/agent-session-journal/replay/journal-projection-test-support.ts diff --git a/src/main/native-chat/agent-session-journal/journal-domain-stop-notes.test.ts b/src/main/native-chat/agent-session-journal/journal-domain-stop-notes.test.ts index 5aba0ed37b4..f05bc62e80b 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-stop-notes.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-stop-notes.test.ts @@ -10,7 +10,7 @@ import { readJournalDomainHistoryPage } from './journal-domain-history-page' import { measureHistoryItem } from '../agent-session-wire/publication/history/agent-session-history-page-bounds' import { prepareJournalDomainImport } from './journal-domain-import' import { publishJournalSessionEpoch } from './durable-rows/journal-row-table' -import { publishJournalOutcomeEpoch } from './outcomes/journal-outcome-write' +import { publishJournalOutcomeEpoch } from './outcomes/journal-outcome-epoch.test-fixture' import { journalSqlRetryRows } from './journal-sql-retry-rows.test-fixture' import type { JournalRow } from './durable-rows/journal-row-schema' import { messageFixture, target } from './journal-sql-message.test-fixture' diff --git a/src/main/native-chat/agent-session-journal/journal-status-cuts.test.ts b/src/main/native-chat/agent-session-journal/journal-status-cuts.test.ts index 80e22c00b5e..d05d20c5f74 100644 --- a/src/main/native-chat/agent-session-journal/journal-status-cuts.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-status-cuts.test.ts @@ -12,8 +12,6 @@ import { newestStructuredAgentSessionTurnBySequence } from '../../../shared/agent-session/view/structured-agent-session-live-turn' import { memoryJournalMap } from './journal-memory-collections' -import { journalRewoundReceipt } from './journal-writer-rewind-receipt' -import { JournalHostFactsNeededError } from './journal-writer-host-facts' import { agentSessionFailureFact } from '../../../shared/agent-session/failures/agent-session-failure' import { agentSessionFailureWords } from '../../../shared/agent-session/failures/agent-session-failure-words' @@ -29,7 +27,7 @@ function item( return { itemId: id, sequence, revision: 1, observedAt: 1, body, ...extra } } -it('reads the first scoped limit and rechecks rewind completion', () => { +it('reads the first scoped limit and honours its context boundary', () => { const state = fixture() state.items.set( 'failed', @@ -67,17 +65,6 @@ it('reads the first scoped limit and rechecks rewind completion', () => { resetsAt: 400, turnEndedAt: 200 }) - const cursor = { epoch: 'epoch', sequence: 13 } - const receipt = { write: () => {}, committed: () => {} } - expect(() => journalRewoundReceipt(undefined, receipt)(cursor).fold?.(state)).toThrow( - JournalHostFactsNeededError - ) - expect(() => - journalRewoundReceipt( - { cursor, cancel: { turnItemId: 'failed', resetsAt: 400 } }, - receipt - )(cursor).fold?.(state) - ).not.toThrow() expect(readJournalUsageLimitCut(state, 12)).toBeNull() const command = { diff --git a/src/main/native-chat/agent-session-journal/replay/journal-outcome-wire.test.ts b/src/main/native-chat/agent-session-journal/replay/journal-outcome-wire.test.ts deleted file mode 100644 index 471dfc24f7a..00000000000 --- a/src/main/native-chat/agent-session-journal/replay/journal-outcome-wire.test.ts +++ /dev/null @@ -1,203 +0,0 @@ -import { readTestJournalSnapshot } from '../journal-observation.test-support' -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 { - createTrackedJournalOpener, - openTestJournalHostDatabase -} from '../store-lifecycle/journal-host-database-test-support' -import { codexProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' -import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../shared/agent-session/journal/agent-session-journal-types' -import { readAgentSessionHistory } from '../../agent-session-wire/publication/history/agent-session-history-page' -import { projectJournalBatch } from '../../agent-session-wire/publication/history/agent-session-journal-batch' -import { readJournalOutcomeGroups } from './journal-read-row' -import { journalOutcomeSource } from '../outcomes/journal-outcome-format' -import { - projectionEpoch as epoch, - projectionEpochRow, - projectionSessionId as sessionId, - projectionBase as base, - seedOutcomeRows -} from './journal-projection-test-support' -import type { JournalRow } from '../durable-rows/journal-row-schema' -import type { AgentSessionJournal } from '../journal-store' -import type { AgentSessionSubscribeEvent } from '../../../../shared/agent-session/agent-session-wire' -import { AgentSessionSubscribers } from '../../agent-session-wire/publication/subscribers/structured-agent-session-subscribers' -import { reusePromptProjection } from '../../agent-session-wire/observations/structured-agent-session-actionable-prompts' - -const journals = createTrackedJournalOpener() -let root: string -const identity = { - sessionId, - workspaceId: 'ws', - hostId: 'host', - agent: 'codex', - providerHandle: codexProviderHandle('thread-1') -} as const -const item = (id: string) => ({ provider: 'orca', clientMessageId: id }) as const -// A queue change naming a missing card folds to nothing, so its sequence stores no record. -const writeNoOp = (journal: AgentSessionJournal) => - journal['rowWriter'].enqueue((seq, ts) => ({ - ...base(seq), - ts, - kind: 'queue', - messageId: 'missing', - change: { op: 'withdrawn' } - })) -afterEach(async () => { - vi.restoreAllMocks() - await journals.closeAll() - if (root) { - await rm(root, { recursive: true, force: true }) - } -}) - -it('keeps sparse sequence groups whole across catch-up, lookahead, history limits and byte shrinking', async () => { - root = await mkdtemp(join(tmpdir(), 'orca-journal-outcomes-wire-')) - const database = openTestJournalHostDatabase(root) - const rows: JournalRow[] = [projectionEpochRow] - for (let seq = 2; seq <= 30; seq++) { - rows.push({ - ...base(seq), - kind: 'item', - itemId: 'legacy', - revision: seq, - body: { kind: 'status', text: `revision ${seq}` } - }) - } - seedOutcomeRows(database, rows) - let journal = await journals.open({ identity, stateDirectory: root }) - const before = { epoch, sequence: 1 } - const old = journal.readSince(before) - if (!old.ok) { - throw new Error('dense read failed') - } - const expected = projectJournalBatch({ - rows: old.rows, - snapshot: await readTestJournalSnapshot(journal), - afterSequence: 1 - }) - // Retention keeps a key's first and latest records, leaving whole sequences without any. - database.db.prepare('DELETE FROM journal_outcomes WHERE seq BETWEEN 3 AND 29').run() - const source = journalOutcomeSource(database.db, sessionId, epoch) - if (!source) { - throw new Error('missing outcome records') - } - const sparse = readJournalOutcomeGroups(database.db, epoch, source, 1) - expect(sparse.length).toBeLessThan(old.rows.length) - expect( - projectJournalBatch({ - rows: sparse, - snapshot: await readTestJournalSnapshot(journal), - afterSequence: 1 - }) - ).toEqual(expected) - const snapshot = await readTestJournalSnapshot(journal) - const cursor = journal.cursor() - await journals.closeAll() - journal = await journals.open({ identity, stateDirectory: root }) - expect(await readTestJournalSnapshot(journal)).toEqual(snapshot) - expect(journal.cursor()).toEqual(cursor) - const reopened = journal.readSince(before) - if (!reopened.ok) { - throw new Error('sparse read after reopen failed') - } - expect( - projectJournalBatch({ - rows: reopened.rows, - snapshot: await readTestJournalSnapshot(journal), - afterSequence: 1 - }) - ).toEqual(expected) - await journal.appendLifecycleBatch({ - settlementId: 'whole-group', - fence: 1, - mutations: ['one', 'two'].map((id) => ({ - kind: 'item', - identity: item(id), - body: { kind: 'status', text: id }, - turnScope: AGENT_JOURNAL_THREAD_SCOPE - })) - }) - await journal.appendItem( - item('later'), - { kind: 'status', text: 'later' }, - { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } - ) - await writeNoOp(journal) - const first = readAgentSessionHistory(journal, { - sessionId, - direction: 'after', - cursor, - limit: 1 - }) - expect(first).toMatchObject({ - ok: true, - page: { - hasNewer: true, - items: [{ body: { text: 'one' } }, { body: { text: 'two' } }], - window: { nextCursor: { sequence: cursor.sequence + 1 } } - } - }) - if (!first.ok) { - throw new Error('first group missing') - } - const second = readAgentSessionHistory(journal, { - sessionId, - direction: 'after', - cursor: first.page.window.nextCursor, - limit: 1 - }) - // Only a page that read everything moves past the trailing write that saved nothing. - expect(second).toMatchObject({ - ok: true, - page: { - hasNewer: false, - items: [{ body: { text: 'later' } }], - window: { nextCursor: journal.cursor() } - } - }) - const bounded = readAgentSessionHistory( - journal, - { sessionId, direction: 'after', cursor, limit: 5 }, - { reader: { budgetBytes: 1, form: (value) => ({ item: value, bytes: 100 }) } } - ) - expect(bounded).toMatchObject({ ok: false, reset: 'cursor_compacted' }) - const continued = await readTestJournalSnapshot(journal) - const continuedCursor = journal.cursor() - await journals.closeAll() - journal = await journals.open({ identity, stateDirectory: root }) - expect(await readTestJournalSnapshot(journal)).toEqual(continued) - expect(journal.cursor()).toEqual(continuedCursor) -}) - -it('moves subscribers and prompt reuse to the newest write when that write saved nothing', async () => { - root = await mkdtemp(join(tmpdir(), 'orca-journal-outcomes-wire-')) - const database = openTestJournalHostDatabase(root) - seedOutcomeRows(database, [projectionEpochRow]) - const journal = await journals.open({ identity, stateDirectory: root }) - const subscribers = new AgentSessionSubscribers() - const frames: AgentSessionSubscribeEvent[] = [] - subscribers.open({ id: 'phone', sessionId, journal, fence: 1, emit: (e) => frames.push(e) }) - await journal.appendItem( - item('material'), - { kind: 'status', text: 'material' }, - { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } - ) - const material = journal.cursor() - await writeNoOp(journal) - expect(journal.cursor().sequence).toBe(material.sequence + 1) - - subscribers.publish(sessionId, journal) - expect(frames.findLast((frame) => frame.type === 'batch')).toMatchObject({ - batch: { cursor: journal.cursor(), items: [{ body: { text: 'material' } }] } - }) - const reads = vi.spyOn(journal, 'readSince') - subscribers.publish(sessionId, journal) - expect(reads).not.toHaveBeenCalled() - reads.mockRestore() - - const prior = { ...material, fence: 1, items: [], dependencies: new Set() } - expect(reusePromptProjection(journal, prior, journal.cursor(), 1)).toBe(prior.items) -}) diff --git a/src/main/native-chat/agent-session-journal/replay/journal-projection-test-support.ts b/src/main/native-chat/agent-session-journal/replay/journal-projection-test-support.ts deleted file mode 100644 index e5c6baac924..00000000000 --- a/src/main/native-chat/agent-session-journal/replay/journal-projection-test-support.ts +++ /dev/null @@ -1,109 +0,0 @@ -import { isolateJournalTransition } from '../projection/journal-reducer-transition' -import { expect } from 'vitest' -import type { JournalHostDatabase } from '../database/journal-host-database' -import { JournalRowWriter } from '../writing/journal-row-writer' -import { JournalWriteQueue } from '../writing/journal-write-queue' -import { JournalWriteProjection } from '../writing/journal-write-projection' -import { - applyJournalRow, - createJournalReducerState, - type JournalReducerState -} from '../projection/journal-reducer' -import { restoreJournal } from './journal-open' -import { encodeJournalState } from '../outcomes/journal-state-codec' -import { journalOutcomeSource } from '../outcomes/journal-outcome-format' -import { publishJournalOutcomeEpoch } from '../outcomes/journal-outcome-write' -import { - insertTestJournalRow, - publishTestJournalEpoch -} from '../store-lifecycle/journal-host-database-test-support' -import type { JournalRow } from '../durable-rows/journal-row-schema' - -export const projectionSessionId = 'session-1' -export const projectionEpoch = 'epoch-1' -export const projectionBase = (seq: number) => ({ - v: 4, - epoch: projectionEpoch, - seq, - ts: 1_000 + seq, - fence: 1 -}) -export const projectionEpochRow: JournalRow = { - ...projectionBase(1), - kind: 'epoch', - reason: 'session_created', - providerHandle: { kind: 'codex', threadId: 'thread-1' } -} - -export function seedProjectionRows( - database: JournalHostDatabase, - rows: readonly JournalRow[] -): JournalReducerState { - const state = createJournalReducerState(projectionSessionId, projectionEpoch) - database.transaction((db) => { - publishTestJournalEpoch(db, projectionSessionId, projectionEpoch) - for (const row of rows) { - insertTestJournalRow(db, projectionSessionId, row) - applyJournalRow(state, row) - } - }) - return state -} - -export function seedOutcomeRows( - database: JournalHostDatabase, - rows: readonly JournalRow[] -): JournalReducerState { - const publication = database.transaction((db) => { - publishTestJournalEpoch(db, projectionSessionId, projectionEpoch) - return publishJournalOutcomeEpoch(db, projectionSessionId, projectionEpoch, rows) - }) - return publication.adopt() -} - -export function projectionWriter(database: JournalHostDatabase, initial: JournalReducerState) { - let state = initial - const queue = new JournalWriteQueue(state.sessionId) - const projection = new JournalWriteProjection({ - database, - state: () => state, - adopt: (next) => { - state = next - } - }) - const writer = new JournalRowWriter({ - database: () => database, - sessionId: state.sessionId, - now: () => 10_000, - serialize: (run) => queue.serialize(run), - readOnly: () => false, - highestFence: () => state.highestFence, - nextSequence: () => state.lastSequence + 1, - prepare: (db, rows, savedAt, finishFold) => projection.prepare(db, rows, savedAt, finishFold), - commit: () => undefined - }) - return { writer, projection, state: () => state, queue } -} - -export function expectProjectionMatches( - database: JournalHostDatabase, - state: JournalReducerState -): void { - const source = journalOutcomeSource(database.db, state.sessionId, state.epoch) - if (!source) { - throw new Error('expected canonical outcomes') - } - const restored = restoreJournal(database.db, state.sessionId) - expect(restored?.damage).toBeNull() - expect(restored?.newer).toBeNull() - expect(restored && encodeJournalState(restored.state)).toStrictEqual(encodeJournalState(state)) -} - -/** Reducer nodes the draft reaches to fold `row` onto `state`, without adopting it. */ -export const trackedJournalDraftNodes = (state: JournalReducerState, row: JournalRow) => { - const transition = isolateJournalTransition(state, (draft) => applyJournalRow(draft, row, row.ts)) - return { - canonical: transition.tracked() - transition.trackedDerived(), - derived: transition.trackedDerived() - } -} From 6c534f1d7aaf1a00015fa420f50b2ff5c46c2d18 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 23:57:39 -0800 Subject: [PATCH 03/45] Port persisted chat tests to SQL fixtures (red WIP) --- .../mobile-queued-edit-composer.test.tsx | 6 +- .../first-work-branch-rename.test.ts | 167 +++-- .../agent-session-attachment-claims.test.ts | 41 +- .../agent-session-attachment-sweep.test.ts | 28 +- .../journal-automatic-submission.test.ts | 2 +- .../journal-creation-fact.test.ts | 119 ++-- .../journal-sql-store.test.ts | 2 +- .../journal-start-failure-settlement.test.ts | 28 +- .../journal-unheld-turns-settlement.test.ts | 123 ++-- ...-unsent-send-hold-contract-test-fixture.ts | 39 +- .../journal-unsent-send-hold-queue.spec.ts | 59 +- ...ued-message-pause-contract-test-fixture.ts | 19 +- .../queued-message-stop-pause.spec.ts | 100 ++- .../replay/journal-damage.test.ts | 46 +- .../journal-historical-inputs.test-support.ts | 84 +-- .../replay/journal-pause-mark-restore.test.ts | 183 +++-- .../journal-host-database-test-support.ts | 38 +- .../journal-queued-rejection-batch.spec.ts | 2 +- .../journal-submission-positions.spec.ts | 6 +- .../writing/command-receipt-schema.spec.ts | 4 - ...vider-timeline-assembler-held-rows.test.ts | 5 +- ...provider-timeline-text-persistence.test.ts | 1 - ...d-agent-session-queued-send-answer.test.ts | 4 +- ...ured-agent-session-snapshot-routes.test.ts | 9 +- ...tured-agent-session-adopted-import.test.ts | 14 +- ...n-attach-test-conversation.test-fixture.ts | 4 +- ...ured-agent-session-attention-reads.test.ts | 18 +- ...stop-contract-test-actions.test-fixture.ts | 2 +- ...-agent-session-codex-stop-note-end.test.ts | 20 +- ...ctured-agent-session-exit-delivery.test.ts | 26 +- ...ctured-agent-session-goal-steering.test.ts | 7 +- ...ent-session-host-test-data.test-fixture.ts | 5 +- ...d-agent-session-input-order-rewind.test.ts | 49 +- ...-session-late-settlement-cancelled.test.ts | 7 - ...ession-leftover-settlement.test-fixture.ts | 11 +- ...ctured-agent-session-plan-feedback.test.ts | 5 +- ...ent-session-prompt-answer-receipts.test.ts | 4 +- ...sion-queue-import-contract.test-fixture.ts | 212 ++---- ...sion-queue-import-retained-history.test.ts | 629 ++++++------------ ...ssion-queued-legacy-import.test-fixture.ts | 105 --- ...sion-queued-legacy-journal.test-fixture.ts | 5 +- ...ructured-agent-session-queued-move.test.ts | 5 +- ...ured-agent-session-queued-stop-row.test.ts | 17 +- ...ent-session-queued-withdrawn-draft.test.ts | 16 +- ...-session-receiving-callers.test-fixture.ts | 5 +- ...ent-session-receiving-cancellation.test.ts | 10 +- ...gent-session-rewind-replay.test-fixture.ts | 6 +- ...t-session-rewind-stop-note.test-fixture.ts | 4 +- ...session-rewind-usage-limit.test-fixture.ts | 22 +- ...d-agent-session-stop-event-entries.test.ts | 54 +- ...tructured-agent-session-stop-event.test.ts | 17 +- ...ured-agent-session-unrun-send-hold.test.ts | 8 +- ...ent-session-journal-double.test-fixture.ts | 2 +- ...ured-agent-session-resolved-append.test.ts | 2 +- ...-agent-session-resumed-child-owner.test.ts | 39 +- ...-agent-session-ended-prompt-cancel.test.ts | 19 +- ...ession-history-conversation-window.test.ts | 28 +- ...ession-history-forward-read-budget.test.ts | 122 ---- .../agent-session-history-public-read.test.ts | 72 -- ...session-stop-note-contract.test-fixture.ts | 13 +- .../agent-session-stop-note-paging.test.ts | 57 +- ...ined-materializer-counters.test-fixture.ts | 117 ---- ...ion-retained-read-counters.test-fixture.ts | 293 -------- ...tured-agent-session-wire-admission.test.ts | 70 +- ...-agent-session-recovery-resolution.test.ts | 29 +- ...ent-session-restart-resume.test-fixture.ts | 47 -- ...ed-agent-session-rewind-provenance.test.ts | 118 ---- .../testing/structured-rewind-cut.test.ts | 90 --- .../structured-agent-session-eviction.test.ts | 30 +- ...agent-session-late-proof-tool-ends.test.ts | 15 +- ...-session-settlement-double.test-fixture.ts | 60 -- ...ion-stale-verdict-contract.test-fixture.ts | 23 - ...-session-stale-verdict-persistence.test.ts | 40 +- ...gent-session-stale-verdict-prompts.test.ts | 10 +- .../native-chat/chat-launcher-command.test.ts | 5 +- ...agent-session-saved-roster.test-support.ts | 108 --- ...-session-status-write-work.test-support.ts | 139 ---- ...-session-conversation-clear-commit.test.ts | 17 +- ...red-agent-launch-support-probe-omp.test.ts | 2 +- ...uctured-agent-launch-support-probe.test.ts | 2 +- ...gent-session-tab-authority.test-support.ts | 17 - .../agent-session-store-transactions.test.ts | 8 +- ...ed-agent-session-mobile-retirement.test.ts | 2 +- ...red-chat-dispatch-worker-lifecycle.test.ts | 5 +- .../structured-chat-dispatch-worker.test.ts | 2 +- .../structured-chat-mail-custody.test.ts | 81 +-- .../chat-worker-request-failures.test.ts | 5 +- .../agent-launch/agent-launch-replay.test.ts | 8 +- ...stration-structured-worker-redrive.test.ts | 7 +- ...rker-start-card-population.test-fixture.ts | 9 +- .../structured-chat-tab-table.test.ts | 13 +- ...nt-session-queued-image-capability.test.ts | 30 +- ...ent-session-queued-image-lifecycle.test.ts | 50 +- ...session-queued-image-wire-contract.test.ts | 19 +- ...agent-session-continuation-context.test.ts | 15 +- ...ured-agent-session-at-rest-context.test.ts | 19 +- ...d-agent-session-rpc-status.test-fixture.ts | 101 ++- ...red-agent-session-saved-status-rpc.test.ts | 6 +- .../runtime-agent-sender-chat-lineage.test.ts | 160 +---- ...untime-worktree-agent-rows-verdict.test.ts | 33 +- ...-queued-edit-host.test-fixture-contract.ts | 2 +- 101 files changed, 1273 insertions(+), 3290 deletions(-) delete mode 100644 src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-legacy-import.test-fixture.ts delete mode 100644 src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-history-forward-read-budget.test.ts delete mode 100644 src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-history-public-read.test.ts delete mode 100644 src/main/native-chat/agent-session-wire/publication/history/testing/structured-agent-session-retained-materializer-counters.test-fixture.ts delete mode 100644 src/main/native-chat/agent-session-wire/publication/history/testing/structured-agent-session-retained-read-counters.test-fixture.ts delete mode 100644 src/main/native-chat/agent-session-wire/rewind/testing/structured-agent-session-rewind-provenance.test.ts delete mode 100644 src/main/native-chat/agent-session-wire/rewind/testing/structured-rewind-cut.test.ts delete mode 100644 src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-settlement-double.test-fixture.ts delete mode 100644 src/main/runtime/agent-session-saved-roster.test-support.ts delete mode 100644 src/main/runtime/agent-session-status-write-work.test-support.ts diff --git a/mobile/src/session/mobile-queued-edit-composer.test.tsx b/mobile/src/session/mobile-queued-edit-composer.test.tsx index 3199f4689e0..673cfd6b5d8 100644 --- a/mobile/src/session/mobile-queued-edit-composer.test.tsx +++ b/mobile/src/session/mobile-queued-edit-composer.test.tsx @@ -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)] diff --git a/src/main/agent-hooks/first-work-branch-rename.test.ts b/src/main/agent-hooks/first-work-branch-rename.test.ts index 0e4243ad92c..4560da738d9 100644 --- a/src/main/agent-hooks/first-work-branch-rename.test.ts +++ b/src/main/agent-hooks/first-work-branch-rename.test.ts @@ -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[] = [] 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() diff --git a/src/main/native-chat/agent-session-attachments/agent-session-attachment-claims.test.ts b/src/main/native-chat/agent-session-attachments/agent-session-attachment-claims.test.ts index 87a796548ff..0eeee469392 100644 --- a/src/main/native-chat/agent-session-attachments/agent-session-attachment-claims.test.ts +++ b/src/main/native-chat/agent-session-attachments/agent-session-attachment-claims.test.ts @@ -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) { @@ -136,7 +125,7 @@ describe('claims written with the message', () => { ).catch((caught: unknown) => caught) expect(isAgentSessionAttachmentExpiredError(error)).toBe(true) expect(await journal.reads.submission('send-1')).toBeUndefined() - expect(claims()).toEqual([]) + expect(await claims()).toEqual([]) }) it("refuses a gone upload and keeps a waiting card's attachment claim across clear", async () => { @@ -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(await reopened.queuedMessages.get('draft-1')).toMatchObject({ body, state: 'waiting' }) - expect(claims()).toEqual([{ upload_id: UPLOAD_A, session_id: IDENTITY.sessionId }]) + expect(await claims()).toEqual([{ uploadId: UPLOAD_A, sessionId: IDENTITY.sessionId }]) }) }) diff --git a/src/main/native-chat/agent-session-attachments/agent-session-attachment-sweep.test.ts b/src/main/native-chat/agent-session-attachments/agent-session-attachment-sweep.test.ts index bcfc8c86508..8f81092cb12 100644 --- a/src/main/native-chat/agent-session-attachments/agent-session-attachment-sweep.test.ts +++ b/src/main/native-chat/agent-session-attachments/agent-session-attachment-sweep.test.ts @@ -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 = {}) { +function facts( + overrides: Partial = {} +): AgentSessionAttachmentSweepFacts { return { - database: () => db, - write: (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 } } diff --git a/src/main/native-chat/agent-session-journal/journal-automatic-submission.test.ts b/src/main/native-chat/agent-session-journal/journal-automatic-submission.test.ts index 539a68c6010..aebfabec97e 100644 --- a/src/main/native-chat/agent-session-journal/journal-automatic-submission.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-automatic-submission.test.ts @@ -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(), [ diff --git a/src/main/native-chat/agent-session-journal/journal-creation-fact.test.ts b/src/main/native-chat/agent-session-journal/journal-creation-fact.test.ts index 3ddc19a721d..ff829c64304 100644 --- a/src/main/native-chat/agent-session-journal/journal-creation-fact.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-creation-fact.test.ts @@ -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 & ): Promise } -async function memory(root: string): Promise { - 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 { const { journalDatabase } = await openStructuredAgentSessionSqlJournalDatabase({ stateDirectory: root, @@ -73,55 +52,49 @@ async function sql(root: string): Promise { 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 = { - 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 = { + 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() +}) diff --git a/src/main/native-chat/agent-session-journal/journal-sql-store.test.ts b/src/main/native-chat/agent-session-journal/journal-sql-store.test.ts index ced10254dd4..434b1532cbf 100644 --- a/src/main/native-chat/agent-session-journal/journal-sql-store.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-sql-store.test.ts @@ -175,7 +175,7 @@ async function foundReceiptSession(database: JournalSqlDatabase): Promise request: { sessionId: 'session-1', location: { - executionHostId: 'host', + executionHostId: 'local', workspaceId: 'workspace', workspaceKind: 'git-worktree', wslDistro: null diff --git a/src/main/native-chat/agent-session-journal/journal-start-failure-settlement.test.ts b/src/main/native-chat/agent-session-journal/journal-start-failure-settlement.test.ts index 95425efd523..e8b6ee45e53 100644 --- a/src/main/native-chat/agent-session-journal/journal-start-failure-settlement.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-start-failure-settlement.test.ts @@ -8,13 +8,10 @@ import { codexProviderHandle } from '../../../shared/agent-session/lifecycle/age import { recordStructuredAgentSessionStartFailure } from '../agent-session-wire/startup/structured-agent-session-start-failure-row' 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 type { QueuedMessageRow } from './queued-messages/queued-message-table' -import { - closeTestJournalHostDatabases, - openTestJournalHostDatabase -} from './store-lifecycle/journal-host-database-test-support' +import { closeTestJournalHostDatabases } from './store-lifecycle/journal-host-database-test-support' import { journalSqlReaderPath, journalSqlWorkerPath @@ -55,20 +52,6 @@ type Journal = Pick< cards(): Promise } -async function memory(root: string): Promise { - const journal = new AgentSessionJournal({ - database: openTestJournalHostDatabase(root), - identity, - now: () => 100 - }) - await journal.open() - journals.push(journal) - return Object.assign(journal, { - queued: (run: () => T) => journal.readInOrder(run), - cards: async () => await journal.queuedMessages.list() - }) -} - async function sql(root: string): Promise { const { journalDatabase } = await openStructuredAgentSessionSqlJournalDatabase({ stateDirectory: root, @@ -89,14 +72,11 @@ async function sql(root: string): Promise { }) } -describe.each([ - ['memory', memory], - ['sql', sql] -])('failed-start settlement on the %s writer', (_name, open) => { +describe('failed-start settlement on the SQL writer', () => { async function fixture() { const root = await mkdtemp(join(tmpdir(), 'journal-start-failure-')) roots.push(root) - const journal = await open(root) + const journal = await sql(root) return { journal, submit: (clientMessageId: string) => diff --git a/src/main/native-chat/agent-session-journal/journal-unheld-turns-settlement.test.ts b/src/main/native-chat/agent-session-journal/journal-unheld-turns-settlement.test.ts index f14b0e74939..08b44b478e5 100644 --- a/src/main/native-chat/agent-session-journal/journal-unheld-turns-settlement.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-unheld-turns-settlement.test.ts @@ -6,12 +6,9 @@ import { codexProviderHandle } from '../../../shared/agent-session/lifecycle/age import type { AgentJournalProducerLinkage } from '../../../shared/agent-session/journal/agent-session-journal-types' 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 @@ -39,18 +36,6 @@ type Journal = Pick & { turnState: (turnId: string) => Promise } -async function memory(root: string): Promise { - const journal = new AgentSessionJournal({ - database: openTestJournalHostDatabase(root), - identity, - now: () => 100 - }) - await journal.open() - return Object.assign(journal, { - turnState: async (turnId: string) => (await journal.reads.item(`orca:${turnId}`))?.body - }) -} - async function sql(root: string): Promise { const { journalDatabase } = await openStructuredAgentSessionSqlJournalDatabase({ stateDirectory: root, @@ -69,61 +54,55 @@ async function sql(root: string): Promise { }) } -it.each([ - ['memory', memory], - ['sql', sql] -])( - 'selects unheld running turns after earlier queued writes on the %s writer', - async (_name, open) => { - const root = await mkdtemp(join(tmpdir(), 'journal-unheld-turns-')) - roots.push(root) - const journal = await open(root) - const turn = ( - turnId: string, - state: 'running' | 'completed', - fence: number, - producer: AgentJournalProducerLinkage = {} - ) => - journal.appendItem( - { provider: 'orca', clientMessageId: turnId }, - { kind: 'turn', turnId, state, ...(state === 'completed' ? { completedAt: 90 } : {}) }, - { fence, turnScope: { kind: 'thread' }, ...producer } - ) - await turn('older-child', 'running', 1) - await turn('finished', 'running', 2) +it('selects unheld running turns after earlier queued writes on the SQL writer', async () => { + const root = await mkdtemp(join(tmpdir(), 'journal-unheld-turns-')) + roots.push(root) + const journal = await sql(root) + const turn = ( + turnId: string, + state: 'running' | 'completed', + fence: number, + producer: AgentJournalProducerLinkage = {} + ) => + journal.appendItem( + { provider: 'orca', clientMessageId: turnId }, + { kind: 'turn', turnId, state, ...(state === 'completed' ? { completedAt: 90 } : {}) }, + { fence, turnScope: { kind: 'thread' }, ...producer } + ) + await turn('older-child', 'running', 1) + await turn('finished', 'running', 2) - // Queued, not awaited: the settlement must see these at its write position. - const queued = [ - turn('restored', 'running', 2), - turn('live', 'running', 2), - turn('subagent', 'running', 2, { agentId: 'helper' }), - turn('finished', 'completed', 2) - ] - const settled = journal.settleUnheldTurns({ - sessionId: 'session', - fence: 2, - generation: 'g1', - liveTurnId: 'live' - }) - await Promise.all([...queued, settled]) + // Queued, not awaited: the settlement must see these at its write position. + const queued = [ + turn('restored', 'running', 2), + turn('live', 'running', 2), + turn('subagent', 'running', 2, { agentId: 'helper' }), + turn('finished', 'completed', 2) + ] + const settled = journal.settleUnheldTurns({ + sessionId: 'session', + fence: 2, + generation: 'g1', + liveTurnId: 'live' + }) + await Promise.all([...queued, settled]) - expect(await journal.turnState('restored')).toMatchObject({ state: 'interrupted' }) - expect(await journal.turnState('finished')).toMatchObject({ - state: 'completed', - completedAt: 90 - }) - for (const turnId of ['live', 'subagent', 'older-child']) { - expect(await journal.turnState(turnId)).toMatchObject({ state: 'running' }) - } - - // One settlement per child generation, even when a later turn runs unheld. - await turn('later', 'running', 2) - await journal.settleUnheldTurns({ - sessionId: 'session', - fence: 2, - generation: 'g1', - liveTurnId: null - }) - expect(await journal.turnState('later')).toMatchObject({ state: 'running' }) + expect(await journal.turnState('restored')).toMatchObject({ state: 'interrupted' }) + expect(await journal.turnState('finished')).toMatchObject({ + state: 'completed', + completedAt: 90 + }) + for (const turnId of ['live', 'subagent', 'older-child']) { + expect(await journal.turnState(turnId)).toMatchObject({ state: 'running' }) } -) + + // One settlement per child generation, even when a later turn runs unheld. + await turn('later', 'running', 2) + await journal.settleUnheldTurns({ + sessionId: 'session', + fence: 2, + generation: 'g1', + liveTurnId: null + }) + expect(await journal.turnState('later')).toMatchObject({ state: 'running' }) +}) diff --git a/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-contract-test-fixture.ts b/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-contract-test-fixture.ts index 6f1f5f0c8a1..bd37c17a6a4 100644 --- a/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-contract-test-fixture.ts +++ b/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-contract-test-fixture.ts @@ -12,14 +12,12 @@ import { agentSessionFailureFact } from '../../../../shared/agent-session/failur import { agentSessionFailureWords } from '../../../../shared/agent-session/failures/agent-session-failure-words' import { claudeProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import { structuredAgentSessionPayloadFingerprint } from '../../../../shared/agent-session/lifecycle/structured-agent-session-mutation' -import { holdUnsentSends, type UnsentSendHold } from './journal-unsent-send-hold' +import type { JournalWriterOperation } from '../journal-writer-command' +type UnsentSendHold = Extract['hold'] import type { AgentSessionJournal } from '../journal-store' import { closeTestJournalHostDatabases, - createTrackedJournalOpener, - liveTestJournalRows, - openTestJournalHostDatabase, - updateTestJournalRowJson + createTrackedJournalOpener } from '../store-lifecycle/journal-host-database-test-support' export function createFixture() { @@ -69,7 +67,7 @@ export function createFixture() { } function hold(journal: AgentSessionJournal, settle: UnsentSendHold = { cause: 'hostRestarted' }) { - return holdUnsentSends(journal, { fence: 0, hold: settle }) + return journal.write({ kind: 'hold-unsent', fence: 0, hold: settle }) } const MAIL_SOURCE: AgentMessageSource = { @@ -118,30 +116,8 @@ export function createFixture() { return open() } - async function storeSubmissionSourceAs( - journal: AgentSessionJournal, - id: string, - source: unknown - ): Promise { - await journal.close() - const seq = (await journal.reads.submission(id))?.submittedSequence - const { db } = openTestJournalHostDatabase(root) - const stored = liveTestJournalRows(db, IDENTITY.sessionId).find((row) => row.seq === seq) - if (!stored) { - throw new Error(`no stored row for ${id}`) - } - const row: unknown = JSON.parse(stored.rowJson) - updateTestJournalRowJson( - db, - IDENTITY.sessionId, - stored.seq, - JSON.stringify({ ...(typeof row === 'object' ? row : {}), source }) - ) - } - - function cardOrder(journal: AgentSessionJournal): string[] { - return journal.queuedMessages - .list() + async function cardOrder(journal: AgentSessionJournal): Promise { + return (await journal.queuedMessages.list()) .filter((card) => card.state === 'waiting') .map((card) => card.messageId) } @@ -187,7 +163,6 @@ export function createFixture() { }, set root(value: typeof root) { root = value - }, - storeSubmissionSourceAs + } } } diff --git a/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-queue.spec.ts b/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-queue.spec.ts index d2285a5a21e..687244cf0a0 100644 --- a/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-queue.spec.ts +++ b/src/main/native-chat/agent-session-journal/queued-messages/journal-unsent-send-hold-queue.spec.ts @@ -1,10 +1,8 @@ import { openTestAgentSessionDatabase } from '../../../runtime/agent-session/records/agent-session-record-store-test-harness' -import { AgentSessionJournal } from '../journal-store' import { openAgentSessionJournal } from '../journal-store-factory' -import { openTestJournalHostDatabase } from '../store-lifecycle/journal-host-database-test-support' import { structuredSendAnswer } from '../../agent-session-wire/delivery/structured-agent-session-queued-send-answer' import type { createFixture } from './journal-unsent-send-hold-contract-test-fixture' -import { describe, expect, it, vi } from 'vitest' +import { describe, expect, it } from 'vitest' import { USER_MESSAGE_SOURCE } from '../../../../shared/agent-session/agent-session-message-source' @@ -43,48 +41,9 @@ export function registerJournalUnsentSendHoldQueueTests( }) await fixture.hold(journal) - expect(fixture.cardOrder(journal)).toEqual(order) + expect(await fixture.cardOrder(journal)).toEqual(order) } ) - - it('finishes the same conversion after a crash between kept rows, then atomically advances the format', async () => { - const resolve = AgentSessionJournal.prototype.resolveDispatch - const fault = vi - .spyOn(AgentSessionJournal.prototype, 'resolveDispatch') - .mockImplementation(function (this: AgentSessionJournal, ...args) { - return args[0].clientMessageId === 'B' - ? Promise.reject(new Error('conversion interrupted')) - : resolve.apply(this, args) - }) - const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) - try { - await expect( - fixture.afterRestart(async (earlier) => { - await earlier.queuedMessages.insert({ messageId: 'C', body: fixture.message('C') }) - await fixture.accept(earlier, 'A', { origin: 'client', source: USER_MESSAGE_SOURCE }) - await fixture.accept(earlier, 'B', { origin: 'client', source: USER_MESSAGE_SOURCE }) - }) - ).rejects.toThrow('settling unsent sends failed') - expect( - openTestJournalHostDatabase(fixture.root) - .db.prepare('SELECT format FROM journal_outcome_epochs') - .get()?.format - ).toBe(2) - } finally { - fault.mockRestore() - warn.mockRestore() - } - const journal = await fixture.open() - expect(fixture.cardOrder(journal)).toEqual(['A', 'B', 'C']) - await journal.appendQueueResume(0) - expect( - openTestJournalHostDatabase(fixture.root) - .db.prepare('SELECT format FROM journal_outcome_epochs') - .get()?.format - ).toBe(3) - await journal.close() - expect(fixture.cardOrder(await fixture.open())).toEqual(['A', 'B', 'C']) - }) }) describe('a close of the chat', () => { @@ -94,7 +53,7 @@ export function registerJournalUnsentSendHoldQueueTests( await fixture.accept(journal, 'mail', { origin: 'host', source: fixture.MAIL_SOURCE }) await fixture.hold(journal, { cause: 'chatClosed' }) - expect(fixture.cardOrder(journal)).toEqual(['person']) + expect(await fixture.cardOrder(journal)).toEqual(['person']) expect(await journal.queuedMessages.get('person')).toMatchObject({ holdReason: null }) for (const id of ['person', 'mail']) { expect(await journal.reads.submission(id)).toMatchObject({ @@ -104,16 +63,16 @@ export function registerJournalUnsentSendHoldQueueTests( } }) - it('settles only what `which` names, leaving a later send queued', async () => { + it('settles only the selected send, leaving a later send queued', async () => { const journal = await fixture.open() await fixture.accept(journal, 'before', { origin: 'client', source: USER_MESSAGE_SOURCE }) await fixture.accept(journal, 'after', { origin: 'client', source: USER_MESSAGE_SOURCE }) await fixture.hold(journal, { cause: 'chatClosed', - which: (submission) => submission.clientMessageId === 'before' + select: { only: { clientMessageId: 'before', fence: 0 } } }) - expect(fixture.cardOrder(journal)).toEqual(['before']) + expect(await fixture.cardOrder(journal)).toEqual(['before']) expect((await journal.reads.submission('after'))?.dispatchState).toBe('pending') }) }) @@ -159,7 +118,7 @@ export function registerJournalUnsentSendHoldQueueTests( }) } }) - expect(fixture.cardOrder(journal)).toEqual( + expect(await fixture.cardOrder(journal)).toEqual( shape === 'stopped' ? ['S', 'W'] : shape === 'reserved-command' @@ -184,7 +143,7 @@ export function registerJournalUnsentSendHoldQueueTests( 'mail' ) ).resolves.toEqual({ kind: 'waiting' }) - expect(fixture.cardOrder(journal)).toEqual(['mail']) + expect(await fixture.cardOrder(journal)).toEqual(['mail']) }) }) @@ -227,7 +186,7 @@ export function registerJournalUnsentSendHoldQueueTests( }) await earlier.close() const reopened = await open() - expect(fixture.cardOrder(reopened)).toEqual(['legacy-task', 'queued-task']) + expect(await fixture.cardOrder(reopened)).toEqual(['legacy-task', 'queued-task']) expect((await reopened.queuedMessages.get('derived-preamble'))?.state).toBe('withdrawn') }) } diff --git a/src/main/native-chat/agent-session-journal/queued-messages/queued-message-pause-contract-test-fixture.ts b/src/main/native-chat/agent-session-journal/queued-messages/queued-message-pause-contract-test-fixture.ts index fbb7e95d968..f446edb6f87 100644 --- a/src/main/native-chat/agent-session-journal/queued-messages/queued-message-pause-contract-test-fixture.ts +++ b/src/main/native-chat/agent-session-journal/queued-messages/queued-message-pause-contract-test-fixture.ts @@ -56,10 +56,10 @@ export function createFixture() { } function clearContext(journal: AgentSessionJournal) { - return journal.context.clear( - { operationId: `clear-${++clock}`, afterFence: 0, clearedAt: clock }, - { write: () => {}, committed: () => {} } - ) + return journal.write({ + kind: 'context-clear', + boundary: { operationId: `clear-${++clock}`, afterFence: 0, clearedAt: clock } + }) } async function turn(journal: AgentSessionJournal, id: string, accept = true): Promise { @@ -89,14 +89,13 @@ export function createFixture() { return journal.appendStopEvent({ reason: 'user-stop' }, 0) } - function reason(journal: AgentSessionJournal): string | null { - return journal.queuedMessages.pauses()[0]?.reason ?? null + async function reason(journal: AgentSessionJournal): Promise { + return (await journal.queuedMessages.pauses())[0]?.reason ?? null } - function held(journal: AgentSessionJournal): [string, boolean][] { - const pauses = journal.queuedMessages.pauses() - return journal.queuedMessages - .list() + async function held(journal: AgentSessionJournal): Promise<[string, boolean][]> { + const pauses = await journal.queuedMessages.pauses() + return (await journal.queuedMessages.list()) .filter((card) => card.state === 'waiting') .map((card) => [card.messageId, queuePauseHolding(pauses, card) !== undefined]) } diff --git a/src/main/native-chat/agent-session-journal/queued-messages/queued-message-stop-pause.spec.ts b/src/main/native-chat/agent-session-journal/queued-messages/queued-message-stop-pause.spec.ts index 96980aa0f0a..b6b3a6f836a 100644 --- a/src/main/native-chat/agent-session-journal/queued-messages/queued-message-stop-pause.spec.ts +++ b/src/main/native-chat/agent-session-journal/queued-messages/queued-message-stop-pause.spec.ts @@ -1,4 +1,4 @@ -import { finishTestJournalHistoricalInputs } from '../replay/journal-historical-inputs.test-support' +import { seedTestJournalHistoricalRows } from '../replay/journal-historical-inputs.test-support' import { openTestJournalHostDatabase } from '../store-lifecycle/journal-host-database-test-support' import type { createFixture } from './queued-message-pause-contract-test-fixture' import { describe, expect, it } from 'vitest' @@ -17,8 +17,8 @@ export function registerQueuedMessageStopPauseTests( await journal.appendItem(identity, body, { fence: 0, turnScope: { kind: 'thread' } }) await fixture.queueDraft(journal, 'carried') await journal.appendQueueResume(0) - await journal.replaceEpochItems('handle_forked', 0, kept) - expect(fixture.reason(journal)).toBeNull() + await journal.write({ kind: 'epoch-replace', reason: 'handle_forked', fence: 0, items: kept }) + expect(await fixture.reason(journal)).toBeNull() const later = { ...identity, uuid: 'T' } await journal.appendItem( later, @@ -26,9 +26,9 @@ export function registerQueuedMessageStopPauseTests( { fence: 0, turnScope: { kind: 'thread' } } ) await journal.appendTombstone(later, { fence: 0 }) - expect(fixture.reason(journal)).toBe('failed') - await journal.replaceEpochItems('handle_forked', 0, kept) - expect(fixture.reason(journal)).toBe('failed') + expect(await fixture.reason(journal)).toBe('failed') + await journal.write({ kind: 'epoch-replace', reason: 'handle_forked', fence: 0, items: kept }) + expect(await fixture.reason(journal)).toBe('failed') expect(await journal.queuedMessages.nextInputCard()).toBeNull() await journal.appendQueueResume(0) expect((await journal.queuedMessages.nextInputCard())?.messageId).toBe('carried') @@ -63,16 +63,15 @@ export function registerQueuedMessageStopPauseTests( it("a rewind's epoch replacement restates a Stop still pausing, and not one a turn ended", async () => { const journal = await fixture.open() await fixture.userStop(journal) - await journal.replaceEpochItems('handle_forked', 0, []) - expect(fixture.reason(journal)).toBe('stopped') + await journal.write({ kind: 'epoch-replace', reason: 'handle_forked', fence: 0, items: [] }) + expect(await fixture.reason(journal)).toBe('stopped') await fixture.turn(journal, 'typed') - await journal.replaceEpochItems('handle_forked', 0, []) - expect(fixture.reason(journal)).toBeNull() + await journal.write({ kind: 'epoch-replace', reason: 'handle_forked', fence: 0, items: [] }) + expect(await fixture.reason(journal)).toBeNull() }) }) it('mail does not lift Stop, while personal acceptance survives transcript removal and restore', async () => { let journal = await fixture.open() - finishTestJournalHistoricalInputs(journal) await journal.queuedMessages.insert({ messageId: 'P', body: fixture.message('P'), @@ -96,18 +95,17 @@ export function registerQueuedMessageStopPauseTests( fence: 0 }) expect(await journal.queuedMessages.nextInputCard()).toBeNull() - expect(fixture.reason(journal)).toBe('stopped') + expect(await fixture.reason(journal)).toBe('stopped') await fixture.turn(journal, 'person') - await journal.replaceEpochItems('handle_forked', 0, []) + await journal.write({ kind: 'epoch-replace', reason: 'handle_forked', fence: 0, items: [] }) await journal.close() journal = await fixture.open() expect((await journal.queuedMessages.nextInputCard())?.messageId).toBe('P') - expect(fixture.reason(journal)).toBeNull() + expect(await fixture.reason(journal)).toBeNull() }) it('clear restatement retains personal members after unattended acceptance releases the others', async () => { let journal = await fixture.open() - finishTestJournalHistoricalInputs(journal) for (const [messageId, unattended] of [ ['P', false], ['A', true], @@ -134,12 +132,12 @@ export function registerQueuedMessageStopPauseTests( providerIdentity: null, fence: 0 }) - expect(fixture.held(journal)).toEqual([ + expect(await fixture.held(journal)).toEqual([ ['P', true], ['A', false], ['L', false] ]) - await journal.replaceEpochItems('handle_forked', 0, []) + await journal.write({ kind: 'epoch-replace', reason: 'handle_forked', fence: 0, items: [] }) await journal.close() journal = await fixture.open() expect( @@ -147,7 +145,7 @@ export function registerQueuedMessageStopPauseTests( ?.messageIds ).toEqual(['P']) await fixture.turn(journal, 'person') - expect(fixture.held(journal)).toEqual([ + expect(await fixture.held(journal)).toEqual([ ['P', false], ['A', false], ['L', false] @@ -155,21 +153,61 @@ export function registerQueuedMessageStopPauseTests( }) it('a retired accepted outcome still proves a later personal turn lifted Stop', async () => { - const journal = await fixture.open() - finishTestJournalHistoricalInputs(journal) - await fixture.queueDraft(journal, 'W') - await fixture.userStop(journal) - await fixture.turn(journal, 'retired-person') - await journal.close() - const db = openTestJournalHostDatabase(fixture.root).db - const source = db.prepare('SELECT generation,through_seq FROM journal_outcome_epochs').get()! - db.prepare( - "INSERT INTO journal_outcomes (generation,seq,field,entry_key,format,ordinal,value_json,deleted) VALUES (?,?,'submissions',?,2,'s0000000000000001:0000000000000001','null',1)" - ).run(source.generation, Number(source.through_seq) + 1, JSON.stringify('retired-person')) - db.prepare('UPDATE journal_outcome_epochs SET through_seq=through_seq+1').run() + const row = (seq: number) => ({ v: 3, epoch: 'historical', seq, fence: 0, ts: 1_000 }) + seedTestJournalHistoricalRows( + openTestJournalHostDatabase(fixture.root).db, + fixture.IDENTITY, + [ + { + ...row(1), + kind: 'epoch', + reason: 'session_created', + providerHandle: { kind: 'claude', sessionId: 'native-1', leafUuid: null } + }, + { + ...row(2), + kind: 'queue', + messageId: 'W', + change: { op: 'queued', body: fixture.message('W'), position: 1, queuedAt: null } + }, + { + ...row(3), + kind: 'tombstone', + itemId: 'orca:stop', + revision: 1, + stopEvent: { reason: 'user-stop', at: 1_000 } + }, + { + ...row(4), + kind: 'submission', + clientMessageId: 'retired-person', + payloadFingerprint: 'person', + body: fixture.message('person'), + providerHandle: { kind: 'claude', sessionId: 'native-1', leafUuid: null }, + source: { kind: 'user' }, + handoverRecorded: true + }, + { + ...row(5), + kind: 'dispatch', + clientMessageId: 'retired-person', + state: 'accepted', + providerItemId: null, + reason: null + }, + { + ...row(6), + kind: 'tombstone', + itemId: 'orca:retired-person', + revision: 1, + retireSubmission: true + } + ], + true + ) const restored = await fixture.open() expect(await restored.reads.submission('retired-person')).toBeUndefined() expect((await restored.queuedMessages.nextInputCard())?.messageId).toBe('W') - expect(fixture.reason(restored)).toBeNull() + expect(await fixture.reason(restored)).toBeNull() }) } diff --git a/src/main/native-chat/agent-session-journal/replay/journal-damage.test.ts b/src/main/native-chat/agent-session-journal/replay/journal-damage.test.ts index 4fd2e22b584..8b9d1241406 100644 --- a/src/main/native-chat/agent-session-journal/replay/journal-damage.test.ts +++ b/src/main/native-chat/agent-session-journal/replay/journal-damage.test.ts @@ -1,3 +1,7 @@ +import { seedTestJournalHistoricalRows } from './journal-historical-inputs.test-support' +import { agentJournalItemKey } from '../../../../shared/agent-session/journal/agent-session-journal-item-key' +import type { JournalRow } from '../durable-rows/journal-row-schema' +import { readTestJournalSnapshot } from '../journal-observation.test-support' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -70,14 +74,26 @@ async function storedRows(): Promise<{ seq: number; rowJson: string }[]> { } async function writeChat(): Promise { - const journal = await open() - for (let ordinal = 0; ordinal < 5; ordinal += 1) { - await journal.appendItem(item(ordinal), body(`m${ordinal}`), { - fence: 1, + const base = (seq: number) => ({ v: 3, epoch: 'historical', seq, fence: 1, ts: 1_000 }) + const rows: JournalRow[] = [ + { + ...base(1), + kind: 'epoch', + reason: 'session_created', + providerHandle: { kind: 'codex', threadId: 'thread-1' } + } + ] + for (let ordinal = 0; ordinal < 5; ordinal++) { + rows.push({ + ...base(ordinal + 2), + kind: 'item', + itemId: agentJournalItemKey(item(ordinal)), + revision: 1, + body: body(`m${ordinal}`), turnScope: AGENT_JOURNAL_THREAD_SCOPE }) } - await journal.close() + await withJournalDatabase((db) => seedTestJournalHistoricalRows(db, IDENTITY, rows)) } beforeEach(async () => { @@ -168,19 +184,21 @@ describe('a row the reader would reject', () => { it('rolls back a whole epoch replacement that holds one, keeping the epoch it would replace', async () => { const journal = await open() await journal.appendItem(item(0), body('kept'), scope) - // Closing the shared test connection would also close the live journal. - const liveRows = () => - liveTestJournalRows(openTestJournalHostDatabase(root).db, IDENTITY.sessionId) - const before = liveRows() + const before = await readTestJournalSnapshot(journal) await expect( - journal.replaceEpochItems('legacy_import', 1, [ - { identity: item(5), body: body('replacement'), ownerFence: 1 }, - { identity: item(6), body: blankCallId, ownerFence: 1 } - ]) + journal.write({ + kind: 'epoch-replace', + reason: 'legacy_import', + fence: 1, + items: [ + { identity: item(5), body: body('replacement'), ownerFence: 1 }, + { identity: item(6), body: blankCallId, ownerFence: 1 } + ] + }) ).rejects.toMatchObject({ code: 'journal_row_rejected' }) - expect(liveRows()).toEqual(before) + expect(await readTestJournalSnapshot(journal)).toEqual(before) await journal.appendItem(item(1), body('still writable'), scope) await journal.close() await expect(open()).resolves.toBeTruthy() diff --git a/src/main/native-chat/agent-session-journal/replay/journal-historical-inputs.test-support.ts b/src/main/native-chat/agent-session-journal/replay/journal-historical-inputs.test-support.ts index 618798e05db..fa052c054a5 100644 --- a/src/main/native-chat/agent-session-journal/replay/journal-historical-inputs.test-support.ts +++ b/src/main/native-chat/agent-session-journal/replay/journal-historical-inputs.test-support.ts @@ -1,67 +1,29 @@ import type Database from '../../../sqlite/sync-database' -import type { AgentSessionJournal } from '../journal-store' -import { journalOutcomeSource } from '../outcomes/journal-outcome-format' -import { readJournalSessionEpoch, insertJournalRow } from '../durable-rows/journal-row-table' -import { applyJournalRow } from '../projection/journal-reducer' -import { isolateJournalTransition } from '../projection/journal-reducer-transition' +import type { AgentSessionJournalIdentity } from '../../../../shared/agent-session/journal/agent-session-journal-identity' +import type { JournalRow } from '../durable-rows/journal-row-schema' +import { insertJournalRow, publishJournalSessionEpoch } from '../durable-rows/journal-row-table' +import { runJournalTransaction } from '../database/journal-database' +import { publishJournalOutcomeEpoch } from '../outcomes/journal-outcome-epoch.test-fixture' -/** Input fixtures stand for a chat an older build wrote and goes on writing: writes to its input - * epoch stay input rows, as that build's writer stored them, so the input reader meets writer-made - * rows. A context this build replaces uses its current writer, including logical replacement. */ -const historicalWriters = new WeakMap void) | null>() - -/** End historical authoring; subsequent operations use the unmodified production writer. */ -export function finishTestJournalHistoricalInputs(journal: AgentSessionJournal): void { - historicalWriters.get(journal)?.() -} - -export function pinTestJournalInputMode(journal: AgentSessionJournal): void { - const projection = journal['projection'] - const prepare = projection.prepare.bind(projection) - const db = journal['database'].db - const sessionId = journal['identity'].sessionId - const epoch = readJournalSessionEpoch(db, sessionId) - if (historicalWriters.has(journal) || (epoch && journalOutcomeSource(db, sessionId, epoch))) { - return - } - journal.inputOrder.beginReconstruction() - historicalWriters.set(journal, () => { - projection.prepare = prepare - historicalWriters.set(journal, null) - }) - projection.prepare = (db, rows, savedAt, finishFold) => { - const state = journal['state'] - if ( - journalOutcomeSource(db, state.sessionId, state.epoch) || - rows.some((row) => row.kind === 'epoch') - ) { - const adopt = prepare(db, rows, savedAt, finishFold) - return () => { - adopt() - finishTestJournalHistoricalInputs(journal) - } - } - const transition = isolateJournalTransition(state, (draft) => { - for (const row of rows) { - applyJournalRow(draft, row, savedAt) - } - finishFold?.(draft) - }) - for (const row of rows) { - insertJournalRow(db, state.sessionId, row, savedAt) - } - return () => { - transition.adopt() - } - } -} - -export function assertTestJournalHistoricalInputs( +/** Author frozen saved rows before opening the current SQL journal. */ +export function seedTestJournalHistoricalRows( db: Database.Database, - journal: AgentSessionJournal + identity: AgentSessionJournalIdentity, + rows: readonly JournalRow[], + retained = false ): void { - const state = journal['state'] - if (!historicalWriters.get(journal) || journalOutcomeSource(db, state.sessionId, state.epoch)) { - throw new Error('historical journal authoring ended at current-writer takeover') + const first = rows[0] + if (first?.kind !== 'epoch') { + throw new Error('historical fixture needs its founding epoch') } + runJournalTransaction(db, () => { + publishJournalSessionEpoch(db, identity, first.epoch) + if (retained) { + publishJournalOutcomeEpoch(db, identity.sessionId, first.epoch, rows) + } else { + for (const row of rows) { + insertJournalRow(db, identity.sessionId, row) + } + } + }) } diff --git a/src/main/native-chat/agent-session-journal/replay/journal-pause-mark-restore.test.ts b/src/main/native-chat/agent-session-journal/replay/journal-pause-mark-restore.test.ts index ac9611f7979..55a5bc1fe42 100644 --- a/src/main/native-chat/agent-session-journal/replay/journal-pause-mark-restore.test.ts +++ b/src/main/native-chat/agent-session-journal/replay/journal-pause-mark-restore.test.ts @@ -1,167 +1,136 @@ import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' -import { afterEach, expect, it } from 'vitest' -import type { AgentJournalMessageItem } from '../../../../shared/agent-session/journal/agent-session-journal-types' +import { afterEach, beforeEach, expect, it } from 'vitest' +import type { JournalRow } from '../durable-rows/journal-row-schema' import { createTrackedJournalOpener, openTestJournalHostDatabase } from '../store-lifecycle/journal-host-database-test-support' -import { finishTestJournalHistoricalInputs } from './journal-historical-inputs.test-support' +import { seedTestJournalHistoricalRows } from './journal-historical-inputs.test-support' import { codexProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' -import { restoreJournal } from './journal-open' -import { journalOutcomeSource } from '../outcomes/journal-outcome-format' import { decodeJournalOutcome } from '../outcomes/journal-outcome-codec' import { AgentSessionJournalError, JournalOutcomeDecodeError } from '../writing/journal-write-guards' +import { withTestAgentSessionDatabase } from '../../../runtime/agent-session/records/agent-session-record-store-test-harness' -const current = createTrackedJournalOpener() -const historical = createTrackedJournalOpener({ legacyInputs: true }) +const journals = createTrackedJournalOpener() let root: string const identity = { sessionId: 'pause-restore', workspaceId: 'ws', hostId: 'host', - agent: 'codex', + agent: 'codex' as const, providerHandle: codexProviderHandle('thread') -} as const -const options = () => ({ identity, stateDirectory: root, now: () => 1_000 }) -const message: AgentJournalMessageItem = { - kind: 'message', - role: 'user', - blocks: [{ type: 'text', text: 'waiting' }] } -const db = () => openTestJournalHostDatabase(root).db -const marks = () => { - const restored = restoreJournal(db(), identity.sessionId) - expect(restored?.damage).toBeNull() - expect(restored?.newer).toBeNull() - return restored!.state.queuePauseMarks -} -afterEach(async () => { - await current.closeAll() - await historical.closeAll() - if (root) { - await rm(root, { recursive: true, force: true }) +const row = (seq: number) => ({ v: 3, epoch: 'historical', seq, fence: 0, ts: 1_000 }) +const rows: JournalRow[] = [ + { + ...row(1), + kind: 'epoch', + reason: 'session_created', + providerHandle: { kind: 'codex', threadId: 'thread' } + }, + { + ...row(2), + kind: 'queue', + messageId: 'W', + change: { + op: 'queued', + body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'waiting' }] }, + position: 1, + queuedAt: null + } + }, + { + ...row(3), + kind: 'tombstone', + itemId: 'orca:stop', + revision: 1, + stopEvent: { reason: 'user-stop', turnId: 'original', at: 1_000 } } -}) - -it('restores separate pause and cancellation owners from complete format-3 scalars and logical replacement', async () => { +] +const source = () => openTestJournalHostDatabase(root).db +const open = () => journals.open({ identity, stateDirectory: root, now: () => 1_000 }) +beforeEach(async () => { root = await mkdtemp(join(tmpdir(), 'orca-pause-restore-')) - let journal = await current.open(options()) - await journal.queuedMessages.insert({ messageId: 'W', body: message, unattended: false }) - await journal.appendStopEvent({ reason: 'user-stop', turnId: 'original' }, 0) - const stop = { - sequence: journal.cursor().sequence, - event: { reason: 'user-stop', turnId: 'original', at: 1_000 } - } - await journal.appendStopEvent({ reason: 'user-close', turnId: 'mail' }, 0) - const close = { - sequence: journal.cursor().sequence, - event: { reason: 'user-close', turnId: 'mail', at: 1_000 } - } - expect( - journalOutcomeSource(db(), identity.sessionId, journal.physicalCursor().epoch)?.prefix - ).toBe(0) - expect(marks()).toMatchObject({ manualStop: stop, latestStop: close }) - await journal.close() - journal = await current.open(options()) - expect(journal.stopMarks.latest()).toMatchObject(close) - expect(await journal.queuedMessages.userStopInForce()).toEqual(stop) - expect(await journal.queuedMessages.nextInputCard()).toBeNull() - await journal.replaceEpochItems('handle_forked', 0, []) - await journal.close() - journal = await current.open(options()) - expect(marks()).toMatchObject({ manualStop: stop, latestStop: close }) - expect(await journal.queuedMessages.nextInputCard()).toBeNull() - await journal.appendQueueResume(0) - expect((await journal.queuedMessages.nextInputCard())?.messageId).toBe('W') +}) +afterEach(async () => { + await journals.closeAll() + await rm(root, { recursive: true, force: true }) }) -it.each(['raw-prefix', 'old-format-3-layout'] as const)( - 'adopts the surviving Stop before a replacing write from %s, only on commit', - async (layout) => { - root = await mkdtemp(join(tmpdir(), 'orca-pause-adopt-')) - let journal = await (layout === 'raw-prefix' ? historical : current).open(options()) - await journal.queuedMessages.insert({ messageId: 'W', body: message }) - await journal.appendStopEvent({ reason: 'user-stop', turnId: 'original' }, 0) - const stop = journal.stopMarks.latest() - await journal.close() - if (layout === 'old-format-3-layout') { - db() +it.each([false, true])( + 'preserves the personal Stop through conversion, a distinct close and reopen (retained=%s)', + async (retained) => { + seedTestJournalHistoricalRows(source(), identity, rows, retained) + if (retained) { + source() .prepare( - "UPDATE journal_outcomes SET value_json = json_remove(value_json, '$.manualStop') WHERE field = '$' AND entry_key = ?" + "UPDATE journal_outcomes SET value_json=json_remove(value_json,'$.manualStop') WHERE field='$' AND entry_key=?" ) .run(JSON.stringify('queuePauseMarks')) } - journal = await current.open(options()) - expect(marks().manualStop).toBeUndefined() + let journal = await open() + const stopped = await journal.queuedMessages.userStopInForce() + expect(stopped).toMatchObject({ event: { reason: 'user-stop', turnId: 'original' } }) const before = journal.cursor() - db().exec(`CREATE TEMP TRIGGER refuse_pause BEFORE INSERT ON main.journal_outcomes - WHEN NEW.field = '$' AND NEW.entry_key = '"queuePauseMarks"' - BEGIN SELECT RAISE(ABORT, 'pause refused'); END`) + await withTestAgentSessionDatabase(root, (db) => + db.exec( + `CREATE TRIGGER refuse_pause BEFORE UPDATE OF queue_latest_stop_json ON journal_chat_state BEGIN SELECT RAISE(ABORT, 'pause refused'); END` + ) + ) await expect( journal.appendStopEvent({ reason: 'user-close', turnId: 'mail' }, 0) - ).rejects.toThrow() + ).rejects.toThrow('pause refused') expect(journal.cursor()).toEqual(before) - expect(journal.stopMarks.latest()).toEqual(stop) - expect(marks().manualStop).toBeUndefined() - expect(marks().latestStop).toEqual(stop) - db().exec('DROP TRIGGER refuse_pause') + expect(await journal.stopMarks.latest()).toEqual(stopped) + await withTestAgentSessionDatabase(root, (db) => db.exec('DROP TRIGGER refuse_pause')) await journal.appendStopEvent({ reason: 'user-close', turnId: 'mail' }, 0) await journal.close() - journal = await current.open(options()) - expect(marks()).toMatchObject({ - manualStop: stop, - latestStop: { event: { reason: 'user-close', turnId: 'mail' } } + journal = await open() + expect(await journal.stopMarks.latest()).toMatchObject({ + event: { reason: 'user-close', turnId: 'mail' } }) - expect(await journal.queuedMessages.userStopInForce()).toEqual(stop) + expect(await journal.queuedMessages.userStopInForce()).toEqual(stopped) expect(await journal.queuedMessages.nextInputCard()).toBeNull() + await journal.appendQueueResume(0) + expect((await journal.queuedMessages.nextInputCard())?.messageId).toBe('W') } ) -it('does not revive a superseded historical Stop, and distinguishes explicit null from an absent mark', async () => { - root = await mkdtemp(join(tmpdir(), 'orca-pause-historical-')) - let journal = await historical.open(options()) - await journal.appendStopEvent({ reason: 'user-stop' }, 0) - await journal.appendStopEvent({ reason: 'host-stop' }, 0) - finishTestJournalHistoricalInputs(journal) - await journal.appendQueueResume(0) - expect(marks().manualStop).toBeNull() - await journal.appendStopEvent({ reason: 'user-stop' }, 0) - await journal.close() - db() +it('honors an explicit null personal Stop from the frozen outcome format', async () => { + seedTestJournalHistoricalRows(source(), identity, rows, true) + source() .prepare( - "UPDATE journal_outcomes SET value_json = json_set(value_json, '$.manualStop', NULL) WHERE field = '$' AND entry_key = ?" + "UPDATE journal_outcomes SET value_json=json_set(value_json,'$.manualStop',NULL) WHERE field='$' AND entry_key=?" ) .run(JSON.stringify('queuePauseMarks')) - journal = await current.open(options()) + const journal = await open() expect(await journal.queuedMessages.userStopInForce()).toBeNull() await journal.appendStopEvent({ reason: 'host-stop' }, 0) - expect(marks().manualStop).toBeNull() + expect(await journal.queuedMessages.userStopInForce()).toBeNull() }) -it('refuses unknown pause members as newer and damaged declared members as corrupt', async () => { - root = await mkdtemp(join(tmpdir(), 'orca-pause-strict-')) - const journal = await current.open(options()) - await journal.appendStopEvent({ reason: 'user-stop' }, 0) - const row = db() +it('refuses unknown pause members as newer and damaged declared members as corrupt', () => { + seedTestJournalHistoricalRows(source(), identity, rows, true) + const stored = source() .prepare( - "SELECT seq,field,entry_key,format,ordinal,value_json,deleted FROM journal_outcomes WHERE field = '$' AND entry_key = ? ORDER BY seq DESC LIMIT 1" + "SELECT seq,field,entry_key,format,ordinal,value_json,deleted FROM journal_outcomes WHERE field='$' AND entry_key=? ORDER BY seq DESC LIMIT 1" ) .get(JSON.stringify('queuePauseMarks'))! - const saved = JSON.parse(String(row.value_json)) + const saved = JSON.parse(String(stored.value_json)) expect(() => decodeJournalOutcome({ - ...row, + ...stored, value_json: JSON.stringify({ ...saved, manualStop: { ...saved.manualStop, future: true } }) }) ).toThrow(AgentSessionJournalError) expect(() => decodeJournalOutcome({ - ...row, + ...stored, value_json: JSON.stringify({ ...saved, manualStop: { ...saved.manualStop, sequence: 'broken' } diff --git a/src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support.ts b/src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support.ts index b4b369f6047..1ef1cbbe396 100644 --- a/src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support.ts +++ b/src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support.ts @@ -1,3 +1,6 @@ +import { insertJournalDomainQueueRow } from '../journal-domain-queue-rows' +import type { QueuedMessageRow } from '../queued-messages/queued-message-table' +import { withTestAgentSessionDatabase } from '../../../runtime/agent-session/records/agent-session-record-store-test-harness' import type { AgentSessionJournalProviderHandle } from '../../../../shared/agent-session/journal/agent-session-journal-identity' import { resolve } from 'node:path' import { randomUUID } from 'node:crypto' @@ -5,15 +8,9 @@ import { journalRowSchemaVersion } from '../../../../shared/agent-session/journa import { agentSessionJournalProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import { JournalHostDatabase } from '../database/journal-host-database' import { restoreJournal, type JournalLoad } from '../replay/journal-open' -import { - buildJournalQueueRow, - type JournalQueueChange -} from '../queued-messages/journal-queue-events' import { fields } from '../outcomes/journal-state-schema' import { journalOutcomeSource } from '../outcomes/journal-outcome-format' import { decodeJournalOutcome, type JournalOutcome } from '../outcomes/journal-outcome-codec' -import { applyJournalRow } from '../projection/journal-reducer' -import { assertTestJournalHistoricalInputs } from '../replay/journal-historical-inputs.test-support' import { openTestAgentSessionDatabase, closeTestAgentSessionDatabase, @@ -209,20 +206,6 @@ export function deleteTestJournalRow(db: Database.Database, sessionId: string, s ) } -/** Persist a prior-build queue row verbatim, then replay it through the production fold. */ -export function appendTestJournalQueueRow( - db: Database.Database, - journal: AgentSessionJournal, - messageId: string, - change: JournalQueueChange -): void { - const state = journal['state'] - assertTestJournalHistoricalInputs(db, journal) - const row = buildJournalQueueRow(state, messageId, change, state.lastSequence + 1, Date.now()) - insertTestJournalRow(db, state.sessionId, row) - applyJournalRow(state, row) -} - /** Unannotated submissions and terminal rows emitted by the legacy writer in one source epoch. */ export function writeTestLegacyTerminalGaps( db: Database.Database, @@ -302,3 +285,18 @@ export function readTestQueueChanges( ] }) } + +/** Seed a prior-build card already converted into the domain database. */ +export async function seedTestJournalQueueCard( + stateDirectory: string, + journal: AgentSessionJournal, + card: Omit +): Promise { + await withTestAgentSessionDatabase(stateDirectory, (db) => + insertJournalDomainQueueRow( + db, + { sessionId: journal.identity.sessionId, epoch: journal.cursor().epoch }, + { ...card, fingerprint: '' } + ) + ) +} diff --git a/src/main/native-chat/agent-session-journal/submissions/journal-queued-rejection-batch.spec.ts b/src/main/native-chat/agent-session-journal/submissions/journal-queued-rejection-batch.spec.ts index 78145df4625..eb6cb742ff4 100644 --- a/src/main/native-chat/agent-session-journal/submissions/journal-queued-rejection-batch.spec.ts +++ b/src/main/native-chat/agent-session-journal/submissions/journal-queued-rejection-batch.spec.ts @@ -115,7 +115,7 @@ export function registerJournalQueuedRejectionBatchTests(): void { }) await Promise.all([ - journal.rejectQueuedSubmissions(0, withdrawal), + journal.write({ kind: 'queued-reject', fence: 0, rejection: withdrawal }), journal.appendLifecycleBatch(startFailureBatch()) ]) diff --git a/src/main/native-chat/agent-session-journal/submissions/journal-submission-positions.spec.ts b/src/main/native-chat/agent-session-journal/submissions/journal-submission-positions.spec.ts index 57f3009f4c0..c08572c318e 100644 --- a/src/main/native-chat/agent-session-journal/submissions/journal-submission-positions.spec.ts +++ b/src/main/native-chat/agent-session-journal/submissions/journal-submission-positions.spec.ts @@ -107,7 +107,7 @@ export function registerJournalSubmissionPositionsTests(): void { dispatchState: 'rejected', answeredInTurn: { turnItemId, via: 'start' } }) - expect(readAgentSessionHydrationPage(journal).submissions).toEqual([ + expect((await readAgentSessionHydrationPage(journal)).submissions).toEqual([ expect.objectContaining({ answeredInTurn: { turnItemId, via: 'start' } }) ]) await journals.closeAll() @@ -124,7 +124,7 @@ export function registerJournalSubmissionPositionsTests(): void { await sendHandedOverThenWithdrawn() expect(pending).toMatchObject({ submittedSequence: submitted.sequence }) - expect(handedOver.sequence).toBeGreaterThan(submitted.sequence) + expect(handedOver?.sequence).toBeGreaterThan(submitted.sequence) expect(await journal.reads.submission('send-1')).toMatchObject({ dispatchState: 'rejected', submittedSequence: submitted.sequence @@ -134,7 +134,7 @@ export function registerJournalSubmissionPositionsTests(): void { (await readTestJournalItems(journal)).find( (item) => item.itemId === agentJournalSubmissionKey('send-1') )?.sequence - ).toBe(withdrawn.sequence) + ).toBe(withdrawn?.sequence) }) }) } diff --git a/src/main/native-chat/agent-session-journal/writing/command-receipt-schema.spec.ts b/src/main/native-chat/agent-session-journal/writing/command-receipt-schema.spec.ts index 010ae4b6884..bd1e23562a7 100644 --- a/src/main/native-chat/agent-session-journal/writing/command-receipt-schema.spec.ts +++ b/src/main/native-chat/agent-session-journal/writing/command-receipt-schema.spec.ts @@ -18,7 +18,6 @@ import { writeCommandReceiptTestRecord } from './command-receipt-test-support' import { AgentSessionJournalError } from './journal-write-guards' -import { buildCommandReceiptTransaction } from './command-receipt-transaction' export function registerCommandReceiptSchemaTests(): void { let root: string @@ -65,9 +64,6 @@ export function registerCommandReceiptSchemaTests(): void { const write = () => insertCommandReceiptIfAbsent(db, scope, receipt) expect(write).toThrow(AgentSessionJournalError) expect(write).toThrow(expect.objectContaining({ code: 'journal_read_only' })) - expect(() => buildCommandReceiptTransaction(scope, receipt).write(db)).toThrow( - expect.objectContaining({ code: 'journal_read_only' }) - ) } finally { db.close() } diff --git a/src/main/native-chat/agent-session-timeline/provider-timeline-assembler-held-rows.test.ts b/src/main/native-chat/agent-session-timeline/provider-timeline-assembler-held-rows.test.ts index 4cdfc61ee64..a668be8fa7e 100644 --- a/src/main/native-chat/agent-session-timeline/provider-timeline-assembler-held-rows.test.ts +++ b/src/main/native-chat/agent-session-timeline/provider-timeline-assembler-held-rows.test.ts @@ -76,7 +76,7 @@ describe('what the journal already holds stands', () => { body: { ...runningTool('read'), state: 'completed' } }) await rig.rows() - const blind = rig.assemble({ sink: { ...rig.sink, journalItems: () => null } }) + const blind = rig.assemble({ sink: rig.sink }) blind.apply({ type: 'item.open', item: 'tool', body: runningTool('read') }) await rig.rows() expect( @@ -174,8 +174,7 @@ describe('requests', () => { fence: 1, turnScope: { kind: 'turn', turnItemId: providerTurnItemId('t1') } }) - // Simulate a same-generation assembler with no journal view. - const blind = rig.assemble({ sink: { ...rig.sink, journalItems: () => null } }) + const blind = rig.assemble({ sink: rig.sink }) blind.apply({ type: 'request.open', request: 'p1', body: pendingApproval }) expect((await rig.row(providerItemId('request', 'p1')))?.body).toMatchObject({ resolution: { state: 'resolved', selectedOptionId: 'allow' } diff --git a/src/main/native-chat/agent-session-timeline/provider-timeline-text-persistence.test.ts b/src/main/native-chat/agent-session-timeline/provider-timeline-text-persistence.test.ts index 8fcec329577..4acb4523b5f 100644 --- a/src/main/native-chat/agent-session-timeline/provider-timeline-text-persistence.test.ts +++ b/src/main/native-chat/agent-session-timeline/provider-timeline-text-persistence.test.ts @@ -22,7 +22,6 @@ it('publishes live text while restart keeps the saved block, then saves a clean let source: Parameters>[0] | undefined const delivered: AgentJournalRenderItem[][] = [] const sink: ProviderTimelineSink = { - journalItems: () => null, written: async () => { await pending return { ok: true } diff --git a/src/main/native-chat/agent-session-wire/delivery/testing/structured-agent-session-queued-send-answer.test.ts b/src/main/native-chat/agent-session-wire/delivery/testing/structured-agent-session-queued-send-answer.test.ts index 458e10c76d7..a32d71ffa9d 100644 --- a/src/main/native-chat/agent-session-wire/delivery/testing/structured-agent-session-queued-send-answer.test.ts +++ b/src/main/native-chat/agent-session-wire/delivery/testing/structured-agent-session-queued-send-answer.test.ts @@ -89,8 +89,8 @@ async function answer( input: async (operation) => readJournalInput(state, operation) }, queuedMessages: { - get: (id) => state.queue.cards.get(id) ?? null, - clearedThrough: () => state.queuePauseMarks.cleared?.sequence ?? 0 + get: async (id) => state.queue.cards.get(id) ?? null, + clearedThrough: async () => state.queuePauseMarks.cleared?.sequence ?? 0 } } // Following sends never walks the conversation's history. diff --git a/src/main/native-chat/agent-session-wire/host/structured-agent-session-snapshot-routes.test.ts b/src/main/native-chat/agent-session-wire/host/structured-agent-session-snapshot-routes.test.ts index 3c18378809a..fc744464715 100644 --- a/src/main/native-chat/agent-session-wire/host/structured-agent-session-snapshot-routes.test.ts +++ b/src/main/native-chat/agent-session-wire/host/structured-agent-session-snapshot-routes.test.ts @@ -47,7 +47,7 @@ it("holds reading across an epoch replacement and resumes only on the new epoch' } ) const routes = new StructuredAgentSessionSnapshotRoutes(testEventSinkLogging().logger) - journal.context.replacements.observe((replaced) => + journal.epochReplacements.observe((replaced) => routes.replacing('orca-session', journal, replaced) ) const reading = { pauseReading: vi.fn(), resumeReading: vi.fn() } @@ -68,7 +68,12 @@ it("holds reading across an epoch replacement and resumes only on the new epoch' expect(live.sink.journalSnapshot?.()).toEqual(saved) retired.close() - const replaced = journal.replaceEpochItems('handle_forked', 0, []) + const replaced = journal.write({ + kind: 'epoch-replace', + reason: 'handle_forked', + fence: 0, + items: [] + }) // Reading pauses in the tick the replacement reaches the writer. expect(reading.pauseReading).toHaveBeenCalledTimes(1) await replaced diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-adopted-import.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-adopted-import.test.ts index 2376da4d393..4ae4786407b 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-adopted-import.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-adopted-import.test.ts @@ -159,9 +159,17 @@ describe('adopting a provider conversation on create', () => { mintSpawnToken: () => 'spawn-a', now: () => NOW }) - vi.spyOn(AgentSessionJournal.prototype, 'replaceEpochItems').mockRejectedValueOnce( - new Error('disk write failed') - ) + const write = AgentSessionJournal.prototype.write + vi.spyOn(AgentSessionJournal.prototype, 'write').mockImplementationOnce(function ( + this: AgentSessionJournal, + operation, + prepare + ) { + if (operation.kind === 'epoch-replace') { + throw new Error('disk write failed') + } + return write.call(this, operation, prepare) + }) const attached = await host .attach({ callerKey: 'client-1' }, attachParams(transcriptPath)) .catch(() => null) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attach-test-conversation.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attach-test-conversation.test-fixture.ts index 80f55a988b3..77e8c1651a4 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attach-test-conversation.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attach-test-conversation.test-fixture.ts @@ -1,13 +1,13 @@ import { agentSessionConversationView } from '../../../../../shared/agent-session/lifecycle/agent-session-conversation-view' import type { AgentSessionRecord } from '../../../../../shared/agent-session/lifecycle/agent-session-record' -import type { JournalHostDatabase } from '../../../agent-session-journal/database/journal-host-database' +import type { JournalSqlDatabase } from '../../../agent-session-journal/database/journal-sql-database' import type { AgentSessionJournal } from '../../../agent-session-journal/journal-store' import { openStructuredAgentSessionConversationJournal } from '../structured-agent-session-conversation-open' import { recordingStructuredAgentSessionLogger } from './structured-agent-session-logger.test-fixture' /** Open through the production conversation path so hostless attach tests adopt the same journal. */ export function openTestAttachConversation( - journalDatabase: JournalHostDatabase + journalDatabase: JournalSqlDatabase ): (record: AgentSessionRecord) => Promise { return async (record) => ( diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attention-reads.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attention-reads.test.ts index 78f8fb6c9a3..a6c8d3834b4 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attention-reads.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attention-reads.test.ts @@ -1,6 +1,5 @@ import { expect, it, vi } from 'vitest' import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../shared/agent-session/journal/agent-session-journal-types' -import { agentSessionJournalProviderHandle } from '../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import type { AgentSessionTurnCompletionEvent } from '../../../../../shared/agent-session/view/agent-session-turn-completion-wire' import { structuredAttentionOrigin } from '../../../../../shared/agent-session/agent-session-attention' import { agentJournalSubmissionKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' @@ -159,10 +158,11 @@ it('acknowledges a post-rewind completion after normal idle retirement without r await rig.host.flushStreamedEvents(SESSION) const journal = rig.host.collaboratorsForTests().sessions.get(SESSION)!.journal const physicalEpoch = journal.physicalCursor().epoch - await journal.replaceEpochItems( - 'handle_forked', - attached.fence, - [ + await journal.write({ + kind: 'epoch-replace', + reason: 'handle_forked', + fence: attached.fence, + items: [ { identity: { provider: 'orca', clientMessageId: firstRequest.clientMessageId }, body: initial.body, @@ -176,12 +176,8 @@ it('acknowledges a post-rewind completion after normal idle retirement without r turnScope: AGENT_JOURNAL_THREAD_SCOPE } ], - undefined, - agentSessionJournalProviderHandle({ - agent: 'codex', - providerHandle: rig.store.getRecord(SESSION)!.providerHandleChain.at(-1)!.handle - }) - ) + providerHandle: undefined + }) await rig.host.flushStreamedEvents(SESSION) expect(journal.cursor().epoch).not.toBe(physicalEpoch) const events: AgentSessionTurnCompletionEvent[] = [] diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-stop-contract-test-actions.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-stop-contract-test-actions.test-fixture.ts index 3b30f809fa3..9b4629eb924 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-stop-contract-test-actions.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-claude-stop-contract-test-actions.test-fixture.ts @@ -111,7 +111,7 @@ export function stopEventsAtClose(connection: FakeConnection): () => number | un const close = connection.close connection.close = async () => { const journal = fixture.host.collaboratorsForTests().sessions.get(SESSION)?.journal - atClose ??= journal?.stopMarks.latest()?.event.reason === 'user-stop' ? 1 : 0 + atClose ??= (await journal?.stopMarks.latest())?.event.reason === 'user-stop' ? 1 : 0 return close() } return () => atClose diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-codex-stop-note-end.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-codex-stop-note-end.test.ts index a7ff344531b..ea5e0fade6f 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-codex-stop-note-end.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-codex-stop-note-end.test.ts @@ -91,9 +91,12 @@ it('isolates legacy acquired history from live matching and saves exact proof be }) await journal().resolveDispatch({ clientMessageId: id, state: 'unknown', fence: 1 }) } - await journal().replaceEpochItems('handle_forked', store.getRecord(SESSION)!.lease.runtimeFence, [ - { identity: { provider: 'orca', clientMessageId: 'U' }, body, ownerFence: 1 } - ]) + await journal().write({ + kind: 'epoch-replace', + reason: 'handle_forked', + fence: store.getRecord(SESSION)!.lease.runtimeFence, + items: [{ identity: { provider: 'orca', clientMessageId: 'U' }, body, ownerFence: 1 }] + }) expect((await journal().reads.item('orca:U'))?.revision).toBeGreaterThan(1) const user = (id: string | null) => ({ type: 'userMessage', @@ -143,11 +146,12 @@ it('isolates legacy acquired history from live matching and saves exact proof be oldTurn([user(null), { type: 'agentMessage', id: 'old-answer', text: 'historical answer' }]) ]) expect(journal().cursor()).toEqual(before) - await journal().replaceEpochItems( - 'handle_forked', - store.getRecord(SESSION)!.lease.runtimeFence, - [] - ) + await journal().write({ + kind: 'epoch-replace', + reason: 'handle_forked', + fence: store.getRecord(SESSION)!.lease.runtimeFence, + items: [] + }) await reopen([ oldTurn([ user('U'), diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-exit-delivery.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-exit-delivery.test.ts index a49a237c567..06a5bead83c 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-exit-delivery.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-exit-delivery.test.ts @@ -1,4 +1,5 @@ import { afterEach, describe, expect, it, vi } from 'vitest' +import { withTestAgentSessionDatabase } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import type { AgentSessionOwnerProbe } from '../../../../../shared/agent-session/lifecycle/agent-session-lease-adjudication' import { EMPTY_STRUCTURED_AGENT_SESSION, @@ -41,21 +42,15 @@ async function queuedBehindPrompt( return { current, draftId: queued.value.queued.messageId } } -function refuseReleases(current: QueuedMessageTestRig): () => number { +async function refuseReleases(current: QueuedMessageTestRig): Promise { vi.spyOn(console, 'warn').mockImplementation(() => undefined) - const transition = current.store.transitionHandoff.bind(current.store) - let refused = 0 - vi.spyOn(current.store, 'transitionHandoff').mockImplementation((sessionId, change) => - transition(sessionId, (record) => { - const next = change(record) - if (next.lease.claimStatus === 'released') { - refused += 1 - throw new Error('disk full') - } - return next - }) + await withTestAgentSessionDatabase(current.root, (db) => + db.exec(` + CREATE TRIGGER reject_release BEFORE UPDATE OF record_json ON agent_session_records + WHEN json_extract(NEW.record_json, '$.lease.claimStatus')='released' + BEGIN SELECT RAISE(ABORT, 'disk full'); END + `) ) - return () => refused } async function watchChat(current: QueuedMessageTestRig) { @@ -84,7 +79,7 @@ const provenDead: StructuredAgentSessionHostDeps['probeOwner'] = async () => ({ describe('an exit whose settlement lands while its release keeps failing', () => { it('reads the dead generation as ended in every frame, and the card is sent over the proven-dead owner', async () => { const { current, draftId } = await queuedBehindPrompt(provenDead) - const refused = refuseReleases(current) + await refuseReleases(current) const reads = await watchChat(current) const before = reads.length @@ -92,7 +87,6 @@ describe('an exit whose settlement lands while its release keeps failing', () => await eventually(async () => expect(await current.handoff(draftId)).toBeDefined()) await eventually(() => expect(current.dispatch).toHaveBeenCalledTimes(2)) - expect(refused()).toBeGreaterThan(0) const untilSent = reads.slice(before).filter((read) => !read.cardSent) expect(untilSent.length).toBeGreaterThan(0) expect(untilSent.filter((read) => read.working)).toEqual([]) @@ -110,7 +104,7 @@ describe('an owner this host cannot prove dead', () => { 'is never replaced when %s: no agent starts over it, and the card comes back', async (_name, probe) => { const { current, draftId } = await queuedBehindPrompt(async () => probe) - refuseReleases(current) + await refuseReleases(current) const owner = current.store.getRecord(SESSION)?.lease await exitChild(current) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-goal-steering.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-goal-steering.test.ts index 557f5e5a702..4ce8279d045 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-goal-steering.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-goal-steering.test.ts @@ -2,10 +2,7 @@ import { eventually } from './structured-agent-session-queued-message-rig-provid import { afterEach, beforeEach, expect, it, vi } from 'vitest' import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../shared/agent-session/journal/agent-session-journal-types' import type { CodexAppServerConnection } from '../../../../codex/app-server/codex-app-server-connection-types' -import { - readCodexAppliedInputOptions, - resolvedCodexMessageOptions -} from '../../../../codex/structured/turns/codex-structured-message-options' +import { resolvedCodexMessageOptions } from '../../../../codex/structured/turns/codex-structured-message-options' import { optionSession } from '../../../../codex/structured/catalog/codex-structured-option-session.test-fixture' import { dispatchCodexTurn } from '../../../../codex/structured/turns/codex-structured-turn-start' import { @@ -39,8 +36,6 @@ async function createGoalRig(): Promise { reportsModel: 'gpt-ran', reportsEffort: 'high' }) - created.host.deps.adapter.readAppliedInputOptions = (input) => - input.sessionId === SESSION ? readCodexAppliedInputOptions(native, input) : null created.host.deps.adapter.holdsLiveProviderProcess = (sessionId, generation) => sessionId === SESSION && generation === native.acquisitionGeneration && diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture.ts index aedd9b269a5..cd808af2228 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { codexProviderHandle } from '../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import { agentSessionRecordFixture } from '../../../agent-session-record-test-fixture' import { agentSessionConversationView } from '../../../../../shared/agent-session/lifecycle/agent-session-conversation-view' @@ -127,10 +128,10 @@ export function hostTestCancelParams() { } export async function hostTestSubmission( - host: Pick, + host: StructuredAgentSessionHost, id: string ): Promise { - return (await host.journalSnapshot(HOST_TEST_SESSION)).submissions.find( + return (await readTestHostJournal(host, HOST_TEST_SESSION)).submissions.find( (entry) => entry.clientMessageId === id ) } diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-input-order-rewind.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-input-order-rewind.test.ts index b5306436ee6..36b8621f8f6 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-input-order-rewind.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-input-order-rewind.test.ts @@ -15,7 +15,7 @@ import { agentJournalSubmissionKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' -it('preserves existing queuedAt across two live replacements despite an older original receipt', async () => { +it('preserves queued command placement across two rewinds and later send settlement', async () => { const rewind = vi.fn(async () => ({ ok: true as const })) const rig = await createQueuedMessageTestRig({ restartable: true, rewind }) try { @@ -29,27 +29,15 @@ it('preserves existing queuedAt across two live replacements despite an older or await rig.settleAccepted(second.id, 'second') const journal = rig.host.collaboratorsForTests().sessions.get(SESSION)!.journal const epoch1 = journal.physicalCursor().epoch - const original = journal.replaceEpochItems.bind(journal) - const snapshots: unknown[] = [] + const original = journal.completeRewind.bind(journal) + let repeated = false let command: Promise | undefined, rewindAgain: Promise | undefined const c = hostTestOperationId() const fields = { command: 'compact' as const, delivery: 'queue-if-active' as const } - const capture = () => ({ - epoch: journal.physicalCursor().epoch, - submissions: journal.submissions().map((s) => ({ - id: s.clientMessageId, - seq: s.acceptedSequence, - handed: s.handedOverAt, - state: s.dispatchState - })), - cards: journal.queuedMessages.list(), - pauses: journal.queuedMessages.pauses() - }) - vi.spyOn(journal, 'replaceEpochItems').mockImplementation(async (...args) => { - snapshots.push(capture()) + vi.spyOn(journal, 'completeRewind').mockImplementation(async (...args) => { const result = await original(...args) - snapshots.push(capture()) - if (snapshots.length === 2) { + if (!repeated) { + repeated = true // Real admissions queue before delivery resumes after the first replacement. command = rig.host.conversationCommand(CALLER, { ...fields, @@ -80,21 +68,8 @@ it('preserves existing queuedAt across two live replacements despite an older or expect(await command).toMatchObject({ ok: true, value: { queued: { state: 'waiting' } } }) const secondVerdict = await rewindAgain expect(secondVerdict).toMatchObject({ ok: true }) - expect(snapshots).toHaveLength(4) - const receipt = rig.store.readCommandReceipt({ kind: 'global' }, m.id) const card = (await journal.queuedMessages.get(c))! - expect(receipt).toMatchObject({ - verdict: 'readable', - receipt: { - status: 'accepted', - method: 'agentSession.send', - sessionId: SESSION, - result: { kind: 'journal-row', epoch: epoch1 } - } - }) expect(card.queuedAt!.epoch).toBe(epoch1) - expect(journal.cursor().epoch).not.toBe(epoch1) - expect(card.queuedAt!.epoch).toBe(journal.physicalCursor().epoch) expect(card.queuedAt!.sequence).toBeGreaterThanOrEqual( (await journal.reads.submission(m.id))!.acceptedSequence! ) @@ -104,11 +79,13 @@ it('preserves existing queuedAt across two live replacements despite an older or await eventually(() => expect(rig.compact).toHaveBeenCalledTimes(1)) const compact = rig.compact.mock.calls[0]![0].command rig.finishCompact() - await eventually(() => - expect(journal.item(agentJournalItemKey(compact.identity))?.body).toMatchObject({ - kind: 'turn', - state: 'completed' - }) + await eventually(async () => + expect((await journal.reads.item(agentJournalItemKey(compact.identity)))?.body).toMatchObject( + { + kind: 'turn', + state: 'completed' + } + ) ) expect(await rig.submission(m.id)).toMatchObject({ dispatchState: 'accepted' }) } finally { diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-late-settlement-cancelled.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-late-settlement-cancelled.test.ts index 335858f16bd..c7677a546bf 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-late-settlement-cancelled.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-late-settlement-cancelled.test.ts @@ -47,16 +47,12 @@ describe('settling a send the provider proves it received after the ack window', await vi.waitFor(async () => expect(await submissions()).toMatchObject([{ dispatchState: 'rejected' }]) ) - const before = journal().cursor() await fixture.host.settleLateDispatch({ sessionId: SESSION, clientMessageId: params.envelope.clientOperationId, providerIdentity: { provider: 'codex', threadId: THREAD, turnId: 'turn-1', ordinal: 1 } }) - const after = journal().readSince(before) - expect(after.ok && after.rows.filter((row) => row.kind === 'dispatch')).toEqual([]) - expect(await submissions()).toMatchObject([ { clientMessageId: params.envelope.clientOperationId, @@ -105,7 +101,6 @@ describe('settling a send the provider proves it received after the ack window', const params = sendParams('ordinary send') await fixture.host.send(CALLER, params) await handedOver(params.envelope.clientOperationId) - const before = journal().cursor() const receipt = (uuid: string) => fixture.host.settleLateDispatch({ sessionId: SESSION, @@ -116,8 +111,6 @@ describe('settling a send the provider proves it received after the ack window', await Promise.all([receipt('echo-row'), receipt('echo-row')]) await receipt('a-different-uuid') - const after = journal().readSince(before) - expect(after.ok && after.rows).toHaveLength(1) expect(await journal().reads.submission(params.envelope.clientOperationId)).toMatchObject({ dispatchState: 'accepted', providerItemId: `claude:${THREAD}:echo-row` diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-leftover-settlement.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-leftover-settlement.test-fixture.ts index a7742aecebd..9cb983c2638 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-leftover-settlement.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-leftover-settlement.test-fixture.ts @@ -138,8 +138,10 @@ export function currentJournal(current: QueuedMessageTestRig) { return journal } -export function currentWork(current: QueuedMessageTestRig): StructuredAgentSessionCurrentWork { - const work = hostStructuredAgentSessionCurrentWork( +export async function currentWork( + current: QueuedMessageTestRig +): Promise { + const work = await hostStructuredAgentSessionCurrentWork( { store: current.store, sessions: current.host.collaboratorsForTests().sessions }, SESSION ) @@ -149,6 +151,7 @@ export function currentWork(current: QueuedMessageTestRig): StructuredAgentSessi return work } -export function turnState(current: QueuedMessageTestRig): string | undefined { - return readAgentJournalTurn(currentJournal(current).itemBody(TURN_KEY) ?? undefined)?.state +export async function turnState(current: QueuedMessageTestRig): Promise { + return readAgentJournalTurn((await currentJournal(current).reads.itemBody(TURN_KEY)) ?? undefined) + ?.state } diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-plan-feedback.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-plan-feedback.test.ts index a049f979a05..0cfa1247bba 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-plan-feedback.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-plan-feedback.test.ts @@ -136,9 +136,10 @@ it.each(['selected-plan', 'absent'] as const)( await dispatch.mock.results[before]!.value claudeFrame(connection, { ...connection.sent.at(-1)!, parent_tool_use_id: null }) const id = dispatch.mock.calls[before]![0].clientMessageId - await vi.waitFor(() => + await vi.waitFor(async () => expect( - host.collaboratorsForTests().sessions.get(SESSION)!.journal.submission(id)?.dispatchState + (await host.collaboratorsForTests().sessions.get(SESSION)!.journal.reads.submission(id)) + ?.dispatchState ).toBe('accepted') ) await host.collaboratorsForTests().conversationDelivery.loop.wake(SESSION) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-prompt-answer-receipts.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-prompt-answer-receipts.test.ts index bd86d6b1b68..2118f8a0f34 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-prompt-answer-receipts.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-prompt-answer-receipts.test.ts @@ -63,8 +63,8 @@ function journal() { return open } -function promptBody(itemId: string): AgentJournalItemBody { - const body = journal().itemBody(itemId) +async function promptBody(itemId: string): Promise { + const body = await journal().reads.itemBody(itemId) if (!body) { throw new Error(`no item ${itemId}`) } diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queue-import-contract.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queue-import-contract.test-fixture.ts index b816d857f67..050974f4770 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queue-import-contract.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queue-import-contract.test-fixture.ts @@ -1,166 +1,66 @@ -import { afterEach, beforeEach, expect } from 'vitest' -import { agentSessionFailureFact } from '../../../../../shared/agent-session/failures/agent-session-failure' -import { agentSessionFailureWords } from '../../../../../shared/agent-session/failures/agent-session-failure-words' -import { - AGENT_JOURNAL_THREAD_SCOPE, - type AgentJournalAnsweredTurnIdentity, - type AgentJournalMessageItem -} from '../../../../../shared/agent-session/journal/agent-session-journal-types' -import { activeProviderContext } from '../../../../../shared/agent-session/agent-session-provider-context' +import { mkdtemp, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, beforeEach } from 'vitest' +import type { AgentJournalMessageItem } from '../../../../../shared/agent-session/journal/agent-session-journal-types' +import type { JournalRow } from '../../../agent-session-journal/durable-rows/journal-row-schema' +import { codexProviderHandle } from '../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import { agentSessionSendBodyFingerprint } from '../../../../../shared/agent-session/requests/structured-agent-session-send-mutation' +import { seedTestJournalHistoricalRows } from '../../../agent-session-journal/replay/journal-historical-inputs.test-support' import { - openTestJournalHostDatabase, - readTestQueueChanges, - seedLegacyTestJournal + createTrackedJournalOpener, + openTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' -import { AgentSessionJournal } from '../../../agent-session-journal/journal-store' -import { HOST_TEST_SESSION as SESSION } from './structured-agent-session-host-test-data.test-fixture' -import { - createQueuedMessageTestRig, - type QueuedMessageTestRig -} from './structured-agent-session-queued-message-rig.test-fixture' - -let rig: QueuedMessageTestRig - -beforeEach(async () => { - rig = await createQueuedMessageTestRig({ restartable: true, rewind: async () => ({ ok: true }) }) -}) - -afterEach(() => rig.dispose()) - -export const db = () => openTestJournalHostDatabase(rig.root).db +let root: string +const journals = createTrackedJournalOpener() +export const sessionId = 'legacy-queue' +export const identity = { + sessionId, + workspaceId: 'workspace-1', + hostId: 'local', + agent: 'codex' as const, + providerHandle: codexProviderHandle('thread') +} +export const sourceHandle = { kind: 'codex' as const, threadId: 'thread' } +export const base = (seq: number) => ({ v: 3, epoch: 'frozen-epoch', seq, fence: 0, ts: 1_000 }) +export const founding: JournalRow = { + ...base(1), + kind: 'epoch', + reason: 'session_created', + providerHandle: sourceHandle +} export const message = (text: string): AgentJournalMessageItem => ({ kind: 'message', role: 'user', - blocks: [{ type: 'text', text }], - ...(text === '/compact' ? { command: { name: 'compact' } } : {}) + blocks: [{ type: 'text', text }] }) - -export const threadWrite = { fence: 0, turnScope: AGENT_JOURNAL_THREAD_SCOPE } - -export const words = (fact: 'providerRejected' | 'cancelled') => - agentSessionFailureWords( - fact === 'providerRejected' - ? agentSessionFailureFact(fact, { detail: { text: 'refused', audience: 'person' } }) - : agentSessionFailureFact(fact), - { surface: 'rejection' } - ) - -export function journalFor(sessionId: string, legacyInputs = false): AgentSessionJournal { - const options = { - database: openTestJournalHostDatabase(rig.root), - identity: { - sessionId, - workspaceId: 'workspace-1', - hostId: 'local', - agent: 'codex' as const, - providerHandle: activeProviderContext(rig.store.getRecord(SESSION)!).head!.handle - } - } - if (legacyInputs) { - seedLegacyTestJournal(options.database, options) - } - return new AgentSessionJournal(options) -} - -export async function handOff( - journal: AgentSessionJournal, - attempt: string, - card: string, - text: string, - as: 'providerRejected' | 'cancelled' | 'accepted', - fence = 0, - beforeAnswer?: () => Promise, - answeredInTurn?: AgentJournalAnsweredTurnIdentity -): Promise { - const body = message(text) - const payloadFingerprint = agentSessionSendBodyFingerprint(journal.queuedMessages.sessionId, body) - await journal.appendSubmission({ - clientMessageId: attempt, - payloadFingerprint, - body, - fence, - handoverRecorded: true, - queuedMessageId: card - }) - await journal.resolveDispatch({ - clientMessageId: attempt, - state: 'pending', - fence, - turnScope: AGENT_JOURNAL_THREAD_SCOPE - }) - await beforeAnswer?.() - await journal.resolveDispatch( - as === 'accepted' - ? { clientMessageId: attempt, state: 'accepted', providerIdentity: null, fence } - : { clientMessageId: attempt, state: 'rejected', ...words(as), fence, answeredInTurn } - ) -} - -export type Legacy = [ - id: string, - state: string, - text: string | null, - consumedAs?: string, - hold?: string -] - -export function seedLegacy(sessionId: string, rows: readonly Legacy[]): void { - const insert = 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', ?, ?, ?, ?)` - ) - rows.forEach(([id, state, text, consumedAs, hold], index) => { - const body = text === null ? '{' : JSON.stringify(message(text)) - const print = text === null ? '' : agentSessionSendBodyFingerprint(sessionId, message(text)) - const reason = state === 'returned' ? 'refused before' : null - insert.run( - sessionId, - id, - index + 1, - body, - print, - state, - hold ?? null, - reason, - consumedAs ?? null - ) - }) -} - -export const legacyIds = (sessionId = SESSION) => +export const db = () => openTestJournalHostDatabase(root).db +export const open = () => journals.open({ identity, stateDirectory: root }) +export const seed = (rows: JournalRow[], retained = false) => + seedTestJournalHistoricalRows(db(), identity, rows, retained) +export function seedLegacyCard( + messageId: string, + body: AgentJournalMessageItem, + consumedAs: string | null = null +) { db() - .prepare('SELECT message_id AS id FROM queued_messages WHERE session_id = ? ORDER BY position') - .all(sessionId) - .map((row) => (typeof row === 'object' && row !== null && 'id' in row ? row.id : null)) - -export function queueRows(sessionId = SESSION): string[] { - return readTestQueueChanges(db(), sessionId).map( - ({ messageId, op, returned }) => `${messageId}:${op}${returned ? '+refusal' : ''}` - ) -} - -export async function nothingSentTo(...cards: string[]): Promise { - await new Promise((resolve) => setTimeout(resolve, 100)) - for (const card of cards) { - expect(await rig.handoff(card)).toBeUndefined() - } -} - -export const retainedHistoryCases = [ - { adopt: false, proof: 'none' }, - { adopt: true, proof: 'none' }, - { adopt: false, proof: 'dispatch' }, - { adopt: true, proof: 'item' } -] - -export const fixture = { - get rig() { - return rig - }, - set rig(value: typeof rig) { - rig = value - } + .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(?,?,?,?,?,1000,'earlier',?,NULL,NULL,?)`) + .run( + sessionId, + messageId, + messageId === 'W' ? 1 : 2, + JSON.stringify(body), + agentSessionSendBodyFingerprint(sessionId, body), + consumedAs ? 'dispatched' : 'waiting', + consumedAs + ) } +beforeEach(async () => { + root = await mkdtemp(join(tmpdir(), 'orca-queue-conversion-')) +}) +afterEach(async () => { + await journals.closeAll() + await rm(root, { recursive: true, force: true }) +}) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queue-import-retained-history.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queue-import-retained-history.test.ts index 4b87e55801c..5b602362366 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queue-import-retained-history.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queue-import-retained-history.test.ts @@ -1,466 +1,225 @@ -import { - readTestJournalItems, - readTestJournalSnapshot -} from '../../../agent-session-journal/journal-observation.test-support' -import { expect, it } from 'vitest' -import { agentJournalSubmissionKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' -import { - AGENT_JOURNAL_THREAD_SCOPE, - journalRowSchemaVersion -} from '../../../../../shared/agent-session/journal/agent-session-journal-types' -import type { AgentSessionSubscribeEvent } from '../../../../../shared/agent-session/agent-session-wire' -import { - EMPTY_STRUCTURED_AGENT_SESSION, - reduceStructuredAgentSession -} from '../../../../../shared/agent-session/structured-agent-session-reducer' -import { AgentSessionSubscribers } from '../../publication/subscribers/structured-agent-session-subscribers' -import { readAgentSessionHistory } from '../../publication/history/agent-session-history-page' -import { subscriberHistoryReader } from '../../publication/history/agent-session-bounded-frames' -import type { AgentSessionJournal } from '../../../agent-session-journal/journal-store' -import { - pinTestJournalInputMode, - assertTestJournalHistoricalInputs, - finishTestJournalHistoricalInputs -} from '../../../agent-session-journal/replay/journal-historical-inputs.test-support' -import { insertTestJournalRow } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { agentSessionSendBodyFingerprint } from '../../../../../shared/agent-session/requests/structured-agent-session-send-mutation' +import { expect, it } from 'vitest' +import type { JournalRow } from '../../../agent-session-journal/durable-rows/journal-row-schema' +import { agentJournalSubmissionKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' +import { readTestJournalItems } from '../../../agent-session-journal/journal-observation.test-support' +import { readAgentSessionHistory } from '../../publication/history/agent-session-history-page' import { - HOST_TEST_NOW, - HOST_TEST_SESSION as SESSION -} from './structured-agent-session-host-test-data.test-fixture' - -import { - db, - handOff, - journalFor, - legacyIds, + base, + founding, + sourceHandle, + sessionId, message, - retainedHistoryCases, - seedLegacy, - threadWrite + seed, + seedLegacyCard, + open } from './structured-agent-session-queue-import-contract.test-fixture' -it.each(['person', 'mail'] as const)( - 'accepted table-only %s handoff keeps its Stop provenance after cleanup and another open', - async (sender) => { - const sessionId = 'table-only-legacy' - let journal = journalFor(sessionId, true) - await journal.open() - pinTestJournalInputMode(journal) - await journal.appendStopEvent({ reason: 'user-stop' }, 0) - await handOff(journal, 'H', 'S', 'sent', 'accepted') - await journal.close() - seedLegacy(sessionId, [ - ['W', 'waiting', 'waiting'], - ['S', 'dispatched', 'sent', 'H'] - ]) - if (sender === 'mail') { - const body = { - ...message('sent'), - from: { - kind: 'agent', - senders: [], - orchestration: { - message: 'mail-notice', - mailbox: 'owner', - dispatchId: null, - messages: [] - } - } - } - db() - .prepare('UPDATE queued_messages SET body_json=? WHERE message_id=?') - .run(JSON.stringify(body), 'S') - } - for (let opened = 0; opened < 2; opened++) { - journal = journalFor(sessionId) - await journal.open() - await journal.queuedMessages.importAtOpen() - expect(legacyIds(sessionId)).toEqual([]) - expect((await journal.queuedMessages.list()).map((card) => card.messageId)).toEqual(['W']) - expect((await journal.queuedMessages.userStopInForce()) === null).toBe(sender === 'person') - await journal.appendSubmission({ - clientMessageId: `unattended-${opened}`, - body: message('wake'), - payloadFingerprint: 'wake', - source: { kind: 'agent' }, - origin: 'host', - handoverRecorded: true, - fence: 0 - }) - await journal.resolveDispatch({ - clientMessageId: `unattended-${opened}`, +it.each([false, true])( + 'keeps personal Stop provenance after conversion and reopen (retained=%s)', + async (retained) => { + const body = message('sent') + const rows: JournalRow[] = [ + founding, + { + ...base(2), + kind: 'queue', + messageId: 'W', + change: { op: 'queued', body: message('waiting'), position: 1, queuedAt: null } + }, + { + ...base(3), + kind: 'tombstone', + itemId: 'orca:stop', + revision: 1, + stopEvent: { reason: 'user-stop', at: 1_000 } + }, + { + ...base(4), + kind: 'submission', + clientMessageId: 'H', + queuedMessageId: 'S', + origin: 'client', + body, + payloadFingerprint: 'sent', + providerHandle: sourceHandle, + handoverRecorded: true + }, + { + ...base(5), + kind: 'dispatch', + clientMessageId: 'H', + state: 'pending', + providerItemId: null, + reason: null + }, + { + ...base(6), + kind: 'dispatch', + clientMessageId: 'H', state: 'accepted', - providerIdentity: null, - fence: 0 + providerItemId: null, + reason: null + } + ] + seed(rows, retained) + seedLegacyCard('S', body, 'H') + for (let round = 0; round < 2; round++) { + const journal = await open() + expect(await journal.queuedMessages.get('S')).toMatchObject({ + state: 'dispatched', + consumedAs: 'H' }) - expect((await journal.queuedMessages.nextInputCard())?.messageId ?? null).toBe( - sender === 'person' ? 'W' : null - ) + expect(await journal.queuedMessages.userStopInForce()).toBeNull() + expect((await journal.queuedMessages.nextInputCard())?.messageId).toBe('W') await journal.close() } } ) -it.each(['person', 'mail'] as const)( - 'a previously cleaned legacy %s handoff keeps its original Stop provenance', - async (sender) => { - const sessionId = 'already-cleaned-legacy' - let journal = journalFor(sessionId, true) - await journal.open() - pinTestJournalInputMode(journal) - await journal.queuedMessages.insert({ messageId: 'W', body: message('waiting') }) - await journal.appendStopEvent({ reason: 'user-stop' }, 0) +it.each([false, true])( + 'keeps Stop in force for a frozen mail handoff (retained=%s)', + async (retained) => { const body = { ...message('sent'), - ...(sender === 'mail' - ? { from: { kind: 'agent' as const, senders: [], orchestration: null } } - : {}) + from: { kind: 'agent' as const, senders: [], orchestration: null } } - await journal.appendSubmission({ - clientMessageId: 'H', - queuedMessageId: 'S', - origin: 'client', - body, - payloadFingerprint: agentSessionSendBodyFingerprint(sessionId, body), - handoverRecorded: true, - fence: 0 - }) - await journal.resolveDispatch({ - clientMessageId: 'H', - state: 'accepted', - providerIdentity: null, - fence: 0 - }) - assertTestJournalHistoricalInputs(db(), journal) - finishTestJournalHistoricalInputs(journal) - await journal.close() - for (let opened = 0; opened < 2; opened++) { - journal = journalFor(sessionId) - await journal.open() - expect(legacyIds(sessionId)).toEqual([]) - expect(await journal.queuedMessages.get('S')).toBeNull() - expect((await journal.queuedMessages.userStopInForce()) === null).toBe(sender === 'person') - await journal.appendSubmission({ - clientMessageId: `wake-${opened}`, - body: message('wake'), - payloadFingerprint: 'wake', - source: { kind: 'agent' }, - origin: 'host', - handoverRecorded: true, - fence: 0 + seed( + [ + founding, + { + ...base(2), + kind: 'queue', + messageId: 'W', + change: { op: 'queued', body: message('waiting'), position: 1, queuedAt: null } + }, + { + ...base(3), + kind: 'tombstone', + itemId: 'orca:stop', + revision: 1, + stopEvent: { reason: 'user-stop', at: 1_000 } + }, + { + ...base(4), + kind: 'submission', + clientMessageId: 'H', + queuedMessageId: 'S', + origin: 'host', + source: { kind: 'agent' }, + body, + payloadFingerprint: 'sent', + providerHandle: sourceHandle, + handoverRecorded: true + }, + { + ...base(5), + kind: 'dispatch', + clientMessageId: 'H', + state: 'accepted', + providerItemId: null, + reason: null + } + ], + retained + ) + seedLegacyCard('S', body, 'H') + for (let round = 0; round < 2; round++) { + const journal = await open() + expect(await journal.queuedMessages.get('S')).toMatchObject({ + state: 'dispatched', + consumedAs: 'H' }) - await journal.resolveDispatch({ - clientMessageId: `wake-${opened}`, - state: 'accepted', - providerIdentity: null, - fence: 0 + expect(await journal.queuedMessages.userStopInForce()).toMatchObject({ + event: { reason: 'user-stop' } }) - expect((await journal.queuedMessages.nextInputCard())?.messageId ?? null).toBe( - sender === 'person' ? 'W' : null - ) - if (opened === 0) { - await journal.replaceEpochItems('handle_forked', 0, []) - } + expect(await journal.queuedMessages.nextInputCard()).toBeNull() await journal.close() } } ) -it('a retired personal handoff in a marker-5 prefix keeps Stop lifted without reviving its input', async () => { - const sessionId = 'retired-prefix-legacy' - let journal = journalFor(sessionId, true) - await journal.open() - pinTestJournalInputMode(journal) - await journal.queuedMessages.insert({ messageId: 'W', body: message('waiting') }) - await journal.appendStopEvent({ reason: 'user-stop' }, 0) - const body = message('sent') - await journal.appendSubmission({ - clientMessageId: 'H', - queuedMessageId: 'S', - origin: 'client', - body, - payloadFingerprint: agentSessionSendBodyFingerprint(sessionId, body), - handoverRecorded: true, - fence: 0 - }) - await journal.resolveDispatch({ - clientMessageId: 'H', - state: 'accepted', - providerIdentity: null, - fence: 0 - }) - assertTestJournalHistoricalInputs(db(), journal) - const { epoch, sequence } = journal.cursor() - await journal.close() - const prefix = sequence + 1 - insertTestJournalRow(db(), sessionId, { - kind: 'tombstone', - v: journalRowSchemaVersion([]), - epoch, - seq: prefix, - ts: HOST_TEST_NOW, - fence: 0, - itemId: agentJournalSubmissionKey('H'), - revision: 1, - retireSubmission: true - }) - const generation = db() - .prepare('INSERT INTO journal_outcome_generations (session_id, epoch) VALUES (?, ?)') - .run(sessionId, epoch).lastInsertRowid - db() - .prepare('INSERT INTO journal_rows (session_id,epoch,seq,ts,row_json) VALUES (?,?,?,?,?)') - .run( - sessionId, - epoch, - prefix + 1, - HOST_TEST_NOW, - JSON.stringify({ v: 5, kind: 'outcomes', epoch, seq: prefix + 1, ts: HOST_TEST_NOW }) - ) - db() - .prepare('INSERT INTO journal_outcome_epochs VALUES (?,?,?,?,?,?)') - .run(sessionId, epoch, generation, 2, prefix, prefix) - for (let opened = 0; opened < 2; opened++) { - journal = journalFor(sessionId) - await journal.open() - expect(await journal.queuedMessages.userStopInForce()).toBeNull() - expect(await journal.reads.submission('H')).toBeUndefined() - expect(await journal.reads.item(agentJournalSubmissionKey('H'))).toBeNull() - expect(await journal.queuedMessages.get('S')).toBeNull() - await journal.appendQueueReopen(0) - expect(await journal.queuedMessages.nextInputCard()).toBeNull() - await journal.appendSubmission({ - clientMessageId: `wake-${opened}`, - body: message('wake'), - payloadFingerprint: 'wake', - source: { kind: 'agent' }, - origin: 'host', - handoverRecorded: true, - fence: 0 - }) - await journal.resolveDispatch({ - clientMessageId: `wake-${opened}`, - state: 'accepted', - providerIdentity: null, - fence: 0 - }) - expect((await journal.queuedMessages.nextInputCard())?.messageId).toBe('W') - await journal.replaceEpochItems('handle_forked', 0, []) - await journal.close() - } -}) - -it('a new-format missing-card attempt cannot invent legacy personal provenance', async () => { - let journal = journalFor(SESSION) - await journal.open() - await journal.queuedMessages.insert({ messageId: 'W', body: message('waiting') }) - await journal.appendStopEvent({ reason: 'user-stop' }, 0) - const body = message('sent') - await journal.appendSubmission({ - clientMessageId: 'H', - queuedMessageId: 'S', - origin: 'client', - body, - payloadFingerprint: agentSessionSendBodyFingerprint(SESSION, body), - handoverRecorded: true, - fence: 0 - }) - await journal.resolveDispatch({ - clientMessageId: 'H', - state: 'accepted', - providerIdentity: null, - fence: 0 - }) - await journal.close() - journal = journalFor(SESSION) - await journal.open() - expect(await journal.queuedMessages.userStopInForce()).not.toBeNull() - expect(await journal.queuedMessages.nextInputCard()).toBeNull() - await journal.close() -}) - -it.each(retainedHistoryCases)('retained history: $adopt / $proof', async ({ adopt, proof }) => { - const earlier = journalFor(SESSION) - await earlier.open() - const append = async (journal: AgentSessionJournal, ordinal: number) => { - await journal.appendItem( - { provider: 'codex', threadId: 'history', turnId: 't', ordinal }, - { ...message('x'.repeat(4_000)), role: 'assistant' }, - { fence: 0, turnScope: AGENT_JOURNAL_THREAD_SCOPE } - ) - } - await append(earlier, 0) - await handOff( - earlier, - 'u', - 'q', - 'stopped words', - 'cancelled', - 0, - async () => { - for (let index = 1; index <= 205; index++) { - await append(earlier, index) +it.each([false, true])( + 'pages converted history and keeps a late queue echo in one bubble after reopen (retained=%s)', + async (retained) => { + const rows: JournalRow[] = [ + founding, + { + ...base(2), + kind: 'submission', + clientMessageId: 'u', + queuedMessageId: 'q', + body: message('stopped words'), + payloadFingerprint: agentSessionSendBodyFingerprint(sessionId, message('stopped words')), + providerHandle: sourceHandle, + handoverRecorded: true } - }, - proof === 'none' - ? undefined - : { - turn: { provider: 'codex', threadId: 'history', turnId: 't', ordinal: -1 }, - via: 'steer' - } - ) - const stale = readAgentSessionHistory(earlier, { sessionId: SESSION, direction: 'tail' }) - expect(stale.ok).toBe(true) - await earlier.close() - seedLegacy(SESSION, [['q', 'waiting', 'stopped words']]) - if (!adopt) { - db().exec(`CREATE TEMP TRIGGER b2_fail_import BEFORE DELETE ON queued_messages - BEGIN SELECT RAISE(ABORT, 'disk full'); END`) - } - const journal = journalFor(SESSION) - await journal.open() - if (!adopt) { - db().exec('DROP TRIGGER b2_fail_import') - } - const queue = { queuedMessages: [], queuePause: null, nextQueuedMessageId: null } - const feed = new AgentSessionSubscribers({ readQueuePublication: () => queue }) - const base = { sessionId: SESSION, journal, fence: 0 } - const oldEvents: AgentSessionSubscribeEvent[] = [] - feed.open({ - ...base, - id: 'old', - cursor: journal.cursor(), - snapshotCatchUp: false, - readsCurrentRequests: false, - emit: (event) => oldEvents.push(event) - }) - expect(oldEvents).toEqual([ - { - type: 'batch', - sessionId: SESSION, - fence: 0, - hostNow: expect.any(Number), - activity: null, - ...queue, - batch: { cursor: journal.cursor(), items: [], removedItemIds: [], submissions: [] } - } - ]) - const corrected: AgentSessionSubscribeEvent[][] = [] - const clients: (() => ReturnType)[] = [] - for (const boundedFrames of [false, true]) { - let state = reduceStructuredAgentSession(EMPTY_STRUCTURED_AGENT_SESSION, { - type: 'history-page', - page: structuredClone(stale.page) - }) - const events: AgentSessionSubscribeEvent[] = [] - corrected.push(events) - clients.push(() => state) - const reader = subscriberHistoryReader({ boundedFrames }) - feed.open({ - ...base, - id: `new-${boundedFrames}`, - cursor: journal.cursor(), - boundedFrames, - snapshotCatchUp: true, - readsCurrentRequests: true, - emit: (event) => { - events.push(event) - state = reduceStructuredAgentSession(state, { - type: 'event', - event: structuredClone(event) - }) - } - }) - expect(events[0]?.type).toBe('snapshot') - while (state.hasOlder) { - const requestedCursor = { - epoch: journal.physicalCursor().epoch, - sequence: state.items[0]!.sequence - } - const older = readAgentSessionHistory( - journal, - { sessionId: SESSION, direction: 'before', cursor: requestedCursor }, - { reader } - ) - expect(older.ok).toBe(true) - state = reduceStructuredAgentSession(state, { - type: 'older-page', - requestedCursor, - page: structuredClone(older.page) + ] + for (let index = 0; index < 205; index++) { + rows.push({ + ...base(rows.length + 1), + kind: 'item', + itemId: `codex:history:t:${index}`, + revision: 1, + body: { ...message(`${index}:${'x'.repeat(4_000)}`), role: 'assistant' }, + turnScope: { kind: 'thread' } }) } - const canonical = await readTestJournalSnapshot(journal) - expect([state.items, state.submissions]).toEqual([canonical.items, canonical.submissions]) - feed.publish(SESSION, journal) - await append(journal, 205) - feed.publish(SESSION, journal) - } - const cursor = journal.cursor() - const after = readAgentSessionHistory(journal, { - sessionId: SESSION, - direction: 'after', - cursor - }) - expect(after).toMatchObject({ ok: false, reset: 'cursor_compacted' }) - if (!adopt) { - await journal.queuedMessages.importAtOpen() - feed.publish(SESSION, journal) - } - if (proof !== 'none') { - const placed = (await readTestJournalItems(journal)).find( - (item) => item.itemId === agentJournalSubmissionKey('u') - ) - const loaded = clients.map((client) => client().items.length) - const normalizationKey = journal.publicRead().normalizationKey - const identity = { + rows.push({ + ...base(rows.length + 1), + kind: 'dispatch', + clientMessageId: 'u', + state: 'unknown', + providerItemId: null, + reason: 'reply lost' + }) + seed(rows, retained) + seedLegacyCard('q', message('stopped words')) + let journal = await open() + const history: string[] = [] + let page = await readAgentSessionHistory(journal, { sessionId, direction: 'tail' }) + for (;;) { + expect(page.ok).toBe(true) + history.unshift(...page.page.items.map((item) => item.itemId)) + if (!page.page.hasOlder) { + break + } + page = await readAgentSessionHistory(journal, { + sessionId, + direction: 'before', + cursor: page.page.window.nextCursor + }) + } + expect(new Set(history).size).toBe(history.length) + expect(history).toHaveLength(206) + const before = await journal.reads.item(agentJournalSubmissionKey('u')) + const providerIdentity = { provider: 'codex' as const, threadId: 'history', turnId: 't', ordinal: 206 } - if (proof === 'dispatch') { - await journal.settleLateDispatch({ - clientMessageId: 'u', - state: 'accepted', - fence: 0, - providerIdentity: identity - }) - } - await journal.appendItem(identity, message('stopped words'), threadWrite) - feed.publish(SESSION, journal) - expect((await journal.reads.submission('u'))?.dispatchState).toBe('accepted') - expect(await journal.reads.receiptFor('u')).not.toBeNull() - expect(journal.publicRead().normalizationKey).toBe(normalizationKey) - expect(clients.map((client) => client().items.length)).toEqual(loaded) - for (const client of clients) { - expect(client().items.find((item) => item.itemId === placed?.itemId)).toMatchObject({ - sequence: placed?.sequence, - observedAt: placed?.observedAt, - turnScope: placed?.turnScope, - revision: 1 - }) - expect( - client().submissions.find((submission) => submission.clientMessageId === 'u')?.dispatchState - ).toBe('accepted') - } + await journal.settleLateDispatch({ + clientMessageId: 'u', + state: 'accepted', + fence: 0, + providerIdentity + }) + await journal.appendItem(providerIdentity, message('stopped words'), { + fence: 0, + turnScope: { kind: 'thread' } + }) + await journal.close() + journal = await open() + const echoes = (await readTestJournalItems(journal)).filter( + (item) => item.body.kind === 'message' && item.body.role === 'user' + ) + expect(echoes).toHaveLength(1) + expect(echoes[0]).toMatchObject({ + itemId: before!.itemId, + sequence: before!.sequence, + observedAt: before!.observedAt + }) + expect(await journal.reads.submission('u')).toMatchObject({ dispatchState: 'accepted' }) } - expect( - corrected.map((events) => events.filter((event) => event.type === 'snapshot').length) - ).toEqual([1, 1]) - expect(oldEvents.every((event) => event.type === 'batch')).toBe(true) - await journal.close() - const reopened = journalFor(SESSION) - await reopened.open() - const resumed: AgentSessionSubscribeEvent[] = [] - new AgentSessionSubscribers().open({ - ...base, - journal: reopened, - id: 'reopen', - cursor: reopened.cursor(), - snapshotCatchUp: true, - readsCurrentRequests: true, - emit: (event) => resumed.push(event) - }) - expect(resumed[0]?.type).toBe('snapshot') - await reopened.close() -}) +) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-legacy-import.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-legacy-import.test-fixture.ts deleted file mode 100644 index b7d70f2b1f7..00000000000 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-legacy-import.test-fixture.ts +++ /dev/null @@ -1,105 +0,0 @@ -import { afterEach, beforeEach } from 'vitest' -import { - AGENT_JOURNAL_THREAD_SCOPE, - type AgentJournalMessageItem -} from '../../../../../shared/agent-session/journal/agent-session-journal-types' -import { activeProviderContext } from '../../../../../shared/agent-session/agent-session-provider-context' -import { agentSessionFailureFact } from '../../../../../shared/agent-session/failures/agent-session-failure' -import { agentSessionFailureWords } from '../../../../../shared/agent-session/failures/agent-session-failure-words' -import { agentSessionSendBodyFingerprint } from '../../../../../shared/agent-session/requests/structured-agent-session-send-mutation' -import { - openTestJournalHostDatabase, - seedLegacyTestJournal -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' - -import { pinTestJournalInputMode } from '../../../agent-session-journal/replay/journal-historical-inputs.test-support' -import { AgentSessionJournal } from '../../../agent-session-journal/journal-store' -import { HOST_TEST_SESSION as SESSION } from './structured-agent-session-host-test-data.test-fixture' -import { - createQueuedMessageTestRig, - type QueuedMessageTestRig -} from './structured-agent-session-queued-message-rig.test-fixture' - -export const threadWrite = { fence: 0, turnScope: AGENT_JOURNAL_THREAD_SCOPE } - -export let rig: QueuedMessageTestRig - -beforeEach(async () => { - rig = await createQueuedMessageTestRig({ restartable: true, rewind: async () => ({ ok: true }) }) -}) - -afterEach(() => rig.dispose()) - -export const db = () => openTestJournalHostDatabase(rig.root).db -export const message = (text: string): AgentJournalMessageItem => ({ - kind: 'message', - role: 'user', - blocks: [{ type: 'text', text }], - ...(text === '/compact' ? { command: { name: 'compact' } } : {}) -}) -export const words = (fact: 'providerRejected' | 'cancelled') => - agentSessionFailureWords( - fact === 'providerRejected' - ? agentSessionFailureFact(fact, { detail: { text: 'refused', audience: 'person' } }) - : agentSessionFailureFact(fact), - { surface: 'rejection' } - ) - -export function journalFor(sessionId: string): AgentSessionJournal { - return new AgentSessionJournal({ - database: openTestJournalHostDatabase(rig.root), - identity: { - sessionId, - workspaceId: 'workspace-1', - hostId: 'local', - agent: 'codex', - providerHandle: activeProviderContext(rig.store.getRecord(SESSION)!).head!.handle - } - }) -} - -/** Prepare a genuinely historical source before any current writer owns this session. */ -export function historicalJournalFor(sessionId: string): AgentSessionJournal { - const journal = journalFor(sessionId) - seedLegacyTestJournal(openTestJournalHostDatabase(rig.root), { identity: journal['identity'] }) - pinTestJournalInputMode(journal) - return journal -} - -export type Legacy = [ - id: string, - state: string, - text: string | null, - consumedAs?: string, - hold?: string -] - -export function seedLegacy(sessionId: string, rows: readonly Legacy[]): void { - const insert = 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', ?, ?, ?, ?)` - ) - rows.forEach(([id, state, text, consumedAs, hold], index) => { - const body = text === null ? '{' : JSON.stringify(message(text)) - const print = text === null ? '' : agentSessionSendBodyFingerprint(sessionId, message(text)) - const reason = state === 'returned' ? 'refused before' : null - insert.run( - sessionId, - id, - index + 1, - body, - print, - state, - hold ?? null, - reason, - consumedAs ?? null - ) - }) -} - -export const legacyIds = (sessionId = SESSION) => - db() - .prepare('SELECT message_id AS id FROM queued_messages WHERE session_id = ? ORDER BY position') - .all(sessionId) - .map((row) => (typeof row === 'object' && row !== null && 'id' in row ? row.id : null)) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-legacy-journal.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-legacy-journal.test-fixture.ts index 656617890ea..72e19787b8f 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-legacy-journal.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-legacy-journal.test-fixture.ts @@ -3,7 +3,6 @@ import { openTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' -import { pinTestJournalInputMode } from '../../../agent-session-journal/replay/journal-historical-inputs.test-support' import { claudeProviderHandle, codexProviderHandle @@ -32,7 +31,5 @@ export function seedQueuedRigLegacyJournal( }, now: () => HOST_TEST_NOW }) - // Attaching opens the journal; the pin keeps it writing inputs, as a build from before conversion did. - return (host) => - pinTestJournalInputMode(host.collaboratorsForTests().sessions.get(sessionId)!.journal) + return () => {} } diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-move.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-move.test.ts index f3edd6f10c8..e8c3added53 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-move.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-move.test.ts @@ -41,9 +41,8 @@ async function queue(text: string, mail = false) { } return result.value.queued.messageId } -function order() { - return journal() - .queuedMessages.list() +async function order() { + return (await journal().queuedMessages.list()) .filter((card) => card.state === 'waiting' || card.state === 'returned') .map((card) => card.messageId) } diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-stop-row.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-stop-row.test.ts index 667f08e3ca3..0fd91786104 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-stop-row.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-stop-row.test.ts @@ -93,7 +93,7 @@ describe("Stop's event", () => { await rig.workingSend() let pausedAtInterrupt: unknown = 'not interrupted' rig.cancelTurn.mockImplementationOnce(async () => { - pausedAtInterrupt = structuredQueuePauses(journal())[0]?.reason ?? null + pausedAtInterrupt = (await structuredQueuePauses(journal()))[0]?.reason ?? null return { cancelled: true } }) expect(await rig.stop()).toMatchObject({ ok: true, value: { cancelled: true } }) @@ -194,7 +194,9 @@ describe("Stop's event", () => { // Neither stopped card can come back, so a reopen holds nothing. await rig.restartHostProcess() await rig.queuePause() - expect(structuredQueuePauses(journal()).map((pause) => pause.reason)).not.toContain('restarted') + expect((await structuredQueuePauses(journal())).map((pause) => pause.reason)).not.toContain( + 'restarted' + ) }) it('survives a host crash: Resume releases waiting work without resending the uncertain original', async () => { @@ -212,7 +214,7 @@ describe("Stop's event", () => { expect((await rig.handoff(sentId))?.dispatchState).toBe('unknown') // The ordinary original never becomes queued work or resends automatically. await expectHeld(null, waiting) - expect(structuredQueuePauses(journal()).map((pause) => pause.reason)).toEqual([ + expect((await structuredQueuePauses(journal())).map((pause) => pause.reason)).toEqual([ 'stopped', 'restarted' ]) @@ -250,10 +252,9 @@ describe("a /clear's carried cards", () => { } // Held there, unshown: nothing runs in the fresh conversation. expect(await rig.queuePause(replacementId)).toBeNull() - expect(structuredQueuePauses(journal(replacementId)).map((pause) => pause.reason)).toEqual([ - 'stopped', - 'cleared' - ]) + expect( + (await structuredQueuePauses(journal(replacementId))).map((pause) => pause.reason) + ).toEqual(['stopped', 'cleared']) // Idle there, so the person's send goes straight out rather than queueing. const text = hostTestMessage('hi') const person = rig.host.send(QUEUED_RIG_CALLER, { @@ -288,6 +289,6 @@ describe("a /clear's carried cards", () => { } }) expect((await journal(replacementId).queuedMessages.list())[0]?.messageId).toBe(carried) - expect(structuredQueuePauses(journal(replacementId))).toEqual([]) + expect(await structuredQueuePauses(journal(replacementId))).toEqual([]) }) }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-withdrawn-draft.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-withdrawn-draft.test.ts index 0325d2a469a..e4f87eb2d22 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-withdrawn-draft.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-withdrawn-draft.test.ts @@ -1,6 +1,6 @@ import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { afterEach, beforeEach, expect, it, vi } from 'vitest' -import { JournalStopAcceptor } from '../../../agent-session-journal/context-epochs/journal-stop-acceptance' +import { AgentSessionJournal } from '../../../agent-session-journal/journal-store' import { HOST_TEST_SESSION as SESSION } from './structured-agent-session-host-test-data.test-fixture' import { holdDelivery } from './structured-agent-session-delivery-hold.test-fixture' import { createQueuedMessageTestRig } from './structured-agent-session-queued-message-rig.test-fixture' @@ -104,9 +104,17 @@ it('a Stop whose acceptance fails takes back no hand-off; the draft sends as it await rig.settleAccepted(working, 'working') await eventually(async () => expect(await rig.handoff(a)).toBeDefined()) const firstA = await rig.handoffId(a) - const failing = vi - .spyOn(JournalStopAcceptor.prototype, 'accept') - .mockRejectedValueOnce(new Error('disk full')) + const write = AgentSessionJournal.prototype.write + const failing = vi.spyOn(AgentSessionJournal.prototype, 'write').mockImplementation(function ( + this: AgentSessionJournal, + operation, + ...args + ) { + if (operation.kind === 'stop-accept') { + return Promise.reject(new Error('disk full')) + } + return write.call(this, operation, ...args) + }) const warned = vi.spyOn(console, 'warn').mockImplementation(() => undefined) try { const stopped = rig.stop() diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-receiving-callers.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-receiving-callers.test-fixture.ts index 473933879af..0cc2e93be92 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-receiving-callers.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-receiving-callers.test-fixture.ts @@ -174,9 +174,10 @@ export async function createReceivingCallerFixture() { } const send = async (text: string, body: AgentJournalMessageItem = hostTestMessage(text)) => { const id = await accept(text, body) - await vi.waitFor(() => + await vi.waitFor(async () => expect( - rig.host.collaboratorsForTests().sessions.get(PARENT)?.journal.submission(id)?.dispatchState + (await rig.host.collaboratorsForTests().sessions.get(PARENT)?.journal.reads.submission(id)) + ?.dispatchState ).toBe('accepted') ) return id diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-receiving-cancellation.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-receiving-cancellation.test.ts index c2b8e931fa6..2c392cc15d9 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-receiving-cancellation.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-receiving-cancellation.test.ts @@ -21,13 +21,11 @@ it('finishes an owned ordinary open but direct cancellation publishes no child', await f.rig.host.close(PARENT, 'evict') f.selectB() const original = acquiredAgentSessionAccount(f.rig.store.requireWritableRecord(PARENT)) - const open = AgentSessionJournal.prototype.open - vi.spyOn(AgentSessionJournal.prototype, 'open').mockImplementationOnce(async function ( - this: AgentSessionJournal - ) { + const open = AgentSessionJournal.open + vi.spyOn(AgentSessionJournal, 'open').mockImplementationOnce(async function (options) { opened.resolve() await release.promise - return open.call(this) + return open(options) }) f.rig.adapter.acquire.mockClear() const result = f.ensure().then( @@ -61,7 +59,7 @@ it('ensure and ordinary send share the journal refusal; retry retains history wi const before = f.rig.store.requireWritableRecord(PARENT) const rows = f.rig.store.listOperationRows() const open = vi - .spyOn(AgentSessionJournal.prototype, 'open') + .spyOn(AgentSessionJournal, 'open') .mockRejectedValue(new Error('fixture journal IO unavailable')) f.rig.adapter.acquire.mockClear() const ensured = await f.ensure() diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-replay.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-replay.test-fixture.ts index 81a01f5c542..199808fbf59 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-replay.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-replay.test-fixture.ts @@ -88,9 +88,9 @@ export async function expectAtomicRewindCompletion({ }) { const target = await seed() const request = await params(target) - const replaceBeforeCrash = AgentSessionJournal.prototype.replaceEpochItems + const replaceBeforeCrash = AgentSessionJournal.prototype.completeRewind const checkpoint = vi - .spyOn(AgentSessionJournal.prototype, 'replaceEpochItems') + .spyOn(AgentSessionJournal.prototype, 'completeRewind') .mockImplementationOnce(async function (this: AgentSessionJournal, ...args) { await replaceBeforeCrash.apply(this, args) throw new Error('completion write failed') @@ -100,7 +100,7 @@ export async function expectAtomicRewindCompletion({ expect(committed.cursor.epoch).not.toBe(request.expectedEpoch) expect(store.getRecord(HOST_TEST_SESSION)?.rewind?.phase).toBe('completed') checkpoint.mockRestore() - const replace = vi.spyOn(AgentSessionJournal.prototype, 'replaceEpochItems') + const replace = vi.spyOn(AgentSessionJournal.prototype, 'completeRewind') expect( await attachAndStart(store.getRecord(HOST_TEST_SESSION)!.lease.runtimeFence) ).toMatchObject({ ok: true }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-stop-note.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-stop-note.test-fixture.ts index 2ed59e0cf85..bbc867f1e3b 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-stop-note.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-stop-note.test-fixture.ts @@ -68,8 +68,8 @@ export async function expectRawStopNoteRewindRecovery({ text: 'Cancellation requested.' }) rewind.mockImplementation(async () => ({ ok: true })) - const replace = journal.replaceEpochItems.bind(journal) - const crash = vi.spyOn(journal, 'replaceEpochItems').mockImplementationOnce(async (...args) => { + const replace = journal.completeRewind.bind(journal) + const crash = vi.spyOn(journal, 'completeRewind').mockImplementationOnce(async (...args) => { await replace(...args) throw new Error('crashed after commit') }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-usage-limit.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-usage-limit.test-fixture.ts index df8064afaf2..449b929e572 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-usage-limit.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-usage-limit.test-fixture.ts @@ -58,7 +58,15 @@ export async function expectRestoredUsageLimitCancelled( const restored = await readTestHostJournal(host, HOST_TEST_SESSION) expect(latestNativeChatUsageLimitCut(restored.items, restored.submissions)).toMatchObject(cut) expect(store.getRecord(HOST_TEST_SESSION)?.usageLimitContinueCancel).toEqual(cut) - expect(host.usageLimitContinue.readState(HOST_TEST_SESSION)?.state).toBe('cancelled') + expect( + host.usageLimitContinue.readState( + HOST_TEST_SESSION, + await host + .collaboratorsForTests() + .sessions.get(HOST_TEST_SESSION)! + .journal.reads.usageLimitCut() + )?.state + ).toBe('cancelled') } export async function expectUsageLimitRewindCancellation({ @@ -87,7 +95,15 @@ export async function expectNoUsageLimitAutoContinue(host: StructuredAgentSessio const sent = vi.spyOn(host, 'send') await host.usageLimitContinue.tick() expect(sent).not.toHaveBeenCalled() - expect(host.usageLimitContinue.readState(HOST_TEST_SESSION)?.state).toBe('cancelled') + expect( + host.usageLimitContinue.readState( + HOST_TEST_SESSION, + await host + .collaboratorsForTests() + .sessions.get(HOST_TEST_SESSION)! + .journal.reads.usageLimitCut() + )?.state + ).toBe('cancelled') } export async function expectReopenedUsageLimitCancellation( @@ -157,7 +173,7 @@ export async function expectUsageLimitCompletionRollback({ expect(store.getRecord(HOST_TEST_SESSION)?.rewind?.phase).toBe('provider-succeeded') expect(store.getRecord(HOST_TEST_SESSION)?.usageLimitContinueCancel).toBeUndefined() db.exec('DROP TRIGGER fail_rewind_completion') - const replace = vi.spyOn(AgentSessionJournal.prototype, 'replaceEpochItems') + const replace = vi.spyOn(AgentSessionJournal.prototype, 'completeRewind') expect( await attachAndStart(store.getRecord(HOST_TEST_SESSION)!.lease.runtimeFence) ).toMatchObject({ ok: true }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event-entries.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event-entries.test.ts index 5207abe3adb..e01cca956bd 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event-entries.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event-entries.test.ts @@ -3,16 +3,6 @@ import { readTestJournalItems } from '../../../agent-session-journal/journal-observation.test-support' import { afterEach, expect, it } from 'vitest' -import { - assertTestJournalHistoricalInputs, - finishTestJournalHistoricalInputs -} from '../../../agent-session-journal/replay/journal-historical-inputs.test-support' -import { - appendTestJournalQueueRow, - openTestJournalHostDatabase, - readTestJournalOutcomes -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' -import { outcomeFormat1StoredScalars } from '../../../agent-session-journal/outcomes/journal-outcome-format-1' import { HOST_TEST_SESSION as SESSION, hostTestMessage, @@ -39,23 +29,14 @@ function journal() { } return value } -type StoredStopEvent = NonNullable< - ReturnType['latestStop'] ->['event'] - -function stopEvents(): StoredStopEvent[] { - const events = new Map() - for (const row of readTestJournalOutcomes(openTestJournalHostDatabase(rig.root).db, SESSION)) { - if (row.field === '$' && row.key === JSON.stringify('queuePauseMarks') && !row.deleted) { - const { latestStop } = outcomeFormat1StoredScalars.shape.queuePauseMarks.parse( - JSON.parse(row.json) - ) - if (latestStop) { - events.set(latestStop.sequence, latestStop.event) - } - } - } - return [...events.values()] +async function stopEvents() { + const latest = await journal().stopMarks.latest() + const personal = await journal().queuedMessages.userStopInForce() + return [ + ...new Map( + [personal, latest].flatMap((stop) => (stop ? [[stop.sequence, stop.event] as const] : [])) + ).values() + ] } async function queuedDraft(text: string) { const queued = await rig.send(text, 'queue-if-active').result @@ -89,16 +70,16 @@ it('keeps Resume visible beside an idle-evicted unknown send recovery', async () await eventually(async () => expect(await rig.submission(id)).toMatchObject({ dispatchState: 'unknown' }) ) - const before = stopEvents() + const before = await stopEvents() await rig.host.collaboratorsForTests().lifetime.idleSweep.tick() - expect(stopEvents()).toEqual(before) + expect(await stopEvents()).toEqual(before) expect(await rig.queuePause()).toEqual({ reason: 'stopped' }) expect(await journal().reads.submissionBody(id)).toEqual(fields.body) expect(await journal().queuedMessages.get(`recovery:${id}`)).toBeNull() expect(await journal().reads.itemBody(`orca:${id}`)).toEqual(fields.body) - expect(structuredQueuePauses(journal()).map((pause) => pause.reason)).toContain('stopped') + expect((await structuredQueuePauses(journal())).map((pause) => pause.reason)).toContain('stopped') expect(await rig.drafts()).toEqual([{ messageId: held, state: 'waiting' }]) expect(await rig.handoff(held)).toBeUndefined() expect(await rig.deleteQueued(`recovery:${id}`)).toMatchObject({ value: { deleted: false } }) @@ -131,7 +112,7 @@ it('a failed personal start retains Stop after its returned input is deleted', a await rig.host.collaboratorsForTests().lifetime.idleSweep.tick() await personal.result - expect(stopEvents().map((event) => event.reason)).toEqual(['user-stop', 'host-stop']) + expect((await stopEvents()).map((event) => event.reason)).toEqual(['user-stop', 'host-stop']) expect((await journal().queuedMessages.userStopInForce())?.event.reason).toBe('user-stop') await eventually(async () => expect(await rig.drafts()).toContainEqual({ messageId: personal.id, state: 'returned' }) @@ -193,17 +174,12 @@ it.each(['host-stop', 'user-close', 'user-close-before-exit'] as const)( await rig.settleAccepted(first, 'first') await openRigTurnFor(rig, first) const held = hostTestOperationId() - const db = openTestJournalHostDatabase(rig.root).db - assertTestJournalHistoricalInputs(db, journal()) - appendTestJournalQueueRow(db, journal(), held, { - op: 'queued', - body: hostTestMessage('keep this stopped'), - position: 1, - queuedAt: journal().physicalCursor() + await journal().queuedMessages.insert({ + messageId: held, + body: hostTestMessage('keep this stopped') }) expect((await journal().queuedMessages.get(held))?.unattended).toBeUndefined() expect(await rig.stop()).toMatchObject({ ok: true }) - finishTestJournalHistoricalInputs(journal()) expect(await rig.queuePause()).toEqual({ reason: 'stopped' }) const mailOptions = { internal: true as const, diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event.test.ts index 99071a174fd..00b0bbc4569 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalSnapshot } from '../../../agent-session-journal/journal-observation.test-support' import { readTestJournalItems } from '../../../agent-session-journal/journal-observation.test-support' import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../shared/agent-session/journal/agent-session-journal-types' @@ -33,8 +34,8 @@ function journal() { return open } -function stopEvents(): JournalStopEvent[] { - const mark = journal().stopMarks.latest() +async function stopEvents(): Promise { + const mark = await journal().stopMarks.latest() return mark ? [mark.event] : [] } @@ -63,13 +64,11 @@ describe("a Stop's event", () => { rig.cancelTurn.mockImplementationOnce(() => interrupt.promise) const stopped = rig.stop() try { - await eventually(() => + await eventually(async () => expect( - journal() - .snapshot() - .items.filter( - (item) => item.body.kind === 'status' && item.body.text === 'Cancellation requested.' - ) + (await readTestJournalSnapshot(journal())).items.filter( + (item) => item.body.kind === 'status' && item.body.text === 'Cancellation requested.' + ) ).toHaveLength(1) ) expect(rig.cancelTurn).toHaveBeenCalledOnce() @@ -90,7 +89,7 @@ describe("a Stop's event", () => { await turnRow('turn-1', 'running') let atInterrupt: JournalStopEvent[] = [] rig.cancelTurn.mockImplementationOnce(async () => { - atInterrupt = stopEvents() + atInterrupt = await stopEvents() return { cancelled: true } }) const fields = { turnId: 'turn-1' } diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-unrun-send-hold.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-unrun-send-hold.test.ts index 8eeea3996e0..2ad20825b1b 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-unrun-send-hold.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-unrun-send-hold.test.ts @@ -12,12 +12,12 @@ const words = (kind: 'hostRestarted' | 'chatClosed' | 'cancelled') => agentSessionFailureWords(agentSessionFailureFact(kind), { surface: 'rejection' }) const KEPT = { state: 'waiting' } -function derivedPauses(): string[] { +async function derivedPauses(): Promise { const journal = rig.host.collaboratorsForTests().sessions.get(SESSION)?.journal if (!journal) { throw new Error('expected the conversation open') } - return structuredQueuePauses(journal).map((pause) => pause.reason) + return (await structuredQueuePauses(journal)).map((pause) => pause.reason) } let rig: QueuedMessageTestRig @@ -63,7 +63,7 @@ describe('a message held behind a start that never answered, then the chat ends' .sessions.get(SESSION) ?.journal.queuedMessages.list()) ?? [] expect(card).toMatchObject({ holdReason: null }) - expect(derivedPauses()).toEqual(['restarted']) + expect(await derivedPauses()).toEqual(['restarted']) expect(await rig.restartOffers()).toEqual([]) }) @@ -80,7 +80,7 @@ describe('a message held behind a start that never answered, then the chat ends' keptAsQueuedMessageId: id }) expect(await rig.drafts()).toEqual([{ messageId: id, ...KEPT }]) - expect(derivedPauses()).toEqual(['restarted']) + expect(await derivedPauses()).toEqual(['restarted']) } ) diff --git a/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-journal-double.test-fixture.ts b/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-journal-double.test-fixture.ts index f8851a25dd4..bc448bc4b2a 100644 --- a/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-journal-double.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-journal-double.test-fixture.ts @@ -58,7 +58,7 @@ export function withJournalQueueMembers( beforeRun?: () => boolean ): Promise => { const result: JournalStepsResult = { outcomes: [] } - const item = (itemId: string) => journal.item?.(itemId) ?? null + const item = async (itemId: string) => (await journal.reads.item(itemId)) ?? null const view = journalTimelineItemsView(item, () => { const items: AgentJournalRenderItem[] = [] journal.visitItems?.((itemId) => { diff --git a/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-resolved-append.test.ts b/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-resolved-append.test.ts index 641126eec4e..cb0fb5057f6 100644 --- a/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-resolved-append.test.ts +++ b/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-resolved-append.test.ts @@ -54,7 +54,7 @@ function journalTarget(): StructuredAgentSessionEventTarget { return { journal, fence: 1, publish: vi.fn() } } -const row = () => journal.itemBody(agentJournalItemKey(ROW)) +const row = async () => await journal.reads.itemBody(agentJournalItemKey(ROW)) describe('resolved revisions', () => { it('resolves each queued revision against the row as the journal holds it when it runs', async () => { diff --git a/src/main/native-chat/agent-session-wire/observations/testing/structured-agent-session-resumed-child-owner.test.ts b/src/main/native-chat/agent-session-wire/observations/testing/structured-agent-session-resumed-child-owner.test.ts index 7da98d34509..2f46a3a7178 100644 --- a/src/main/native-chat/agent-session-wire/observations/testing/structured-agent-session-resumed-child-owner.test.ts +++ b/src/main/native-chat/agent-session-wire/observations/testing/structured-agent-session-resumed-child-owner.test.ts @@ -12,7 +12,7 @@ import { } from '../../../../../shared/native-chat/native-chat-types' import { writeClaudeBackgroundTaskRow } from '../../../../claude/claude-background-task-row-journal' import { createTrackedJournalOpener } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' -import { StructuredAgentSessionCurrentWork } from '../structured-agent-session-current-work' +import { heldStructuredAgentSessionCurrentWork } from '../structured-agent-session-host-current-work' import { createDeferredStructuredAgentSessionEventSink } from '../../journal-ingress/structured-agent-session-event-sink' import { testEventSinkLogging } from '../../host/testing/structured-agent-session-logger.test-fixture' import { settleStaleStructuredAgentSessionState } from '../../termination/structured-agent-session-stale-state-settlement' @@ -65,7 +65,13 @@ it('keeps a resumed subagent and background task working when the run that creat const rows = new StructuredAgentSessionSubagentRows({ journal: () => journal, fence: () => fence, - currentWork: (_sessionId, open) => new StructuredAgentSessionCurrentWork(open, fence), + currentWork: (sessionId, open) => + heldStructuredAgentSessionCurrentWork( + { getConversation: () => null, replacedRuntime: () => undefined }, + sessionId, + open, + undefined + ), readChildWork: () => [subagent], onWriteFailed: (_sessionId, error) => { throw error @@ -117,19 +123,22 @@ it('keeps a resumed subagent and background task working when the run that creat await run() await settleRunOne() // Epoch rebuild preserves both creation and execution provenance. - await journal.replaceEpochItems( - 'handle_forked', - 2, - (await readTestJournalItems(journal)).map((item) => { - const { ownerFence, executionOwner } = journal.itemProvenance(item.itemId) - return { - identity: parseAgentJournalItemKey(item.itemId)!, - body: journal.itemBody(item.itemId)!, - ownerFence: ownerFence ?? 0, - ...(executionOwner === undefined ? {} : { executionOwner }) - } - }) - ) + await journal.write({ + kind: 'epoch-replace', + reason: 'handle_forked', + fence: 2, + items: await Promise.all( + (await readTestJournalItems(journal)).map(async (item) => { + const { ownerFence, executionOwner } = await journal.reads.itemProvenance(item.itemId) + return { + identity: parseAgentJournalItemKey(item.itemId)!, + body: (await journal.reads.itemBody(item.itemId))!, + ownerFence: ownerFence ?? 0, + ...(executionOwner === undefined ? {} : { executionOwner }) + } + }) + ) + }) await settleRunOne() const blocks = (await readTestJournalItems(journal)).flatMap((item) => diff --git a/src/main/native-chat/agent-session-wire/operations/testing/structured-agent-session-ended-prompt-cancel.test.ts b/src/main/native-chat/agent-session-wire/operations/testing/structured-agent-session-ended-prompt-cancel.test.ts index 7f3e1da2ed2..4a78ae48357 100644 --- a/src/main/native-chat/agent-session-wire/operations/testing/structured-agent-session-ended-prompt-cancel.test.ts +++ b/src/main/native-chat/agent-session-wire/operations/testing/structured-agent-session-ended-prompt-cancel.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalSnapshot } from '../../../agent-session-journal/journal-observation.test-support' // Dismiss stale-generation prompts in the journal without stopping current turns or withdrawing queued work. import { mkdtemp, rm } from 'node:fs/promises' @@ -73,11 +74,11 @@ async function endedCardAndLiveTurn(): Promise<{ journal: AgentSessionJournal; i return { journal, itemId: card.itemId } } -function queueUntouched(journal: AgentSessionJournal): void { - expect(journal.queuedMessages.list().map((card) => [card.messageId, card.state])).toEqual([ - ['queued-1', 'waiting'] - ]) - expect(structuredQueuePauses(journal)).toEqual([]) +async function queueUntouched(journal: AgentSessionJournal): Promise { + expect((await journal.queuedMessages.list()).map((card) => [card.messageId, card.state])).toEqual( + [['queued-1', 'waiting']] + ) + expect(await structuredQueuePauses(journal)).toEqual([]) } function provider(routePromptCancel?: StructuredAgentSessionAdapter['routePromptCancel']) { @@ -117,8 +118,10 @@ function chatStop() { })) } -function resolution(journal: AgentSessionJournal, itemId: string) { - const body = journal.snapshot().items.find((item) => item.itemId === itemId)?.body +async function resolution(journal: AgentSessionJournal, itemId: string) { + const body = (await readTestJournalSnapshot(journal)).items.find( + (item) => item.itemId === itemId + )?.body return body?.kind === 'approval' || body?.kind === 'question' ? body.resolution.state : undefined } @@ -141,6 +144,6 @@ describe('a question an ended generation raised, cancelled from a client that st expect(interrupt).not.toHaveBeenCalled() expect(agent.cancelTurn).not.toHaveBeenCalled() expect(agent.dismissPrompt).not.toHaveBeenCalled() - queueUntouched(journal) + await queueUntouched(journal) }) }) diff --git a/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-history-conversation-window.test.ts b/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-history-conversation-window.test.ts index 12c2a20a9e9..948ea2d4907 100644 --- a/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-history-conversation-window.test.ts +++ b/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-history-conversation-window.test.ts @@ -18,8 +18,6 @@ import type { AgentSessionJournal } from '../../../../agent-session-journal/jour import { readAgentSessionHistory } from '../agent-session-history-page' import { codexProviderHandle } from '../../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' -// Window on session-owned rows; include subagent rows without letting bursts crowd out conversation. - const IDENTITY: AgentSessionJournalIdentity = { sessionId: 'session-1', workspaceId: 'ws-1', @@ -61,8 +59,10 @@ function textsOf(page: AgentSessionHistoryPage): string[] { ) } -function read(request: Omit): AgentSessionHistoryPage { - const result = readAgentSessionHistory(journal, { sessionId: 'session-1', ...request }) +async function read( + request: Omit +): Promise { + const result = await readAgentSessionHistory(journal, { sessionId: 'session-1', ...request }) if (!result.ok) { throw new Error(`expected a page, got reset ${result.reset}`) } @@ -88,18 +88,20 @@ afterEach(async () => { }) describe('a history page over a session with a subagent', () => { - it("counts the session's own rows and carries the subagent's rows between them", async () => { + it('pages through a large subagent burst without losing or repeating conversation rows', async () => { await append(named('own-', 1, 5)) await append(named('child-', 1, 300), child) await append(['own-6']) - const tail = read({ direction: 'tail', limit: 3 }) - expect(textsOf(tail)).toEqual(['own-4', 'own-5', ...named('child-', 1, 300), 'own-6']) - expect(tail.hasOlder).toBe(true) - - const older = read({ direction: 'before', cursor: tail.window.nextCursor, limit: 3 }) - expect(textsOf(older)).toEqual(['own-1', 'own-2', 'own-3']) - expect(older.hasOlder).toBe(false) - expect(older.hasNewer).toBe(true) + let page = await read({ direction: 'tail', limit: 3 }) + expect(textsOf(page)).toEqual([...named('child-', 296, 5), 'own-6']) + expect(page.hasOlder).toBe(true) + let texts = textsOf(page) + while (page.hasOlder) { + page = await read({ direction: 'before', cursor: page.window.nextCursor, limit: 3 }) + texts = [...textsOf(page), ...texts] + expect(page.hasNewer).toBe(true) + } + expect(texts).toEqual([...named('own-', 1, 5), ...named('child-', 1, 300), 'own-6']) }) }) diff --git a/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-history-forward-read-budget.test.ts b/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-history-forward-read-budget.test.ts deleted file mode 100644 index 46d2faa1680..00000000000 --- a/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-history-forward-read-budget.test.ts +++ /dev/null @@ -1,122 +0,0 @@ -import { mkdtemp, rm } from 'node:fs/promises' -import { tmpdir } from 'node:os' -import { join } from 'node:path' -import { afterEach, describe, expect, it, vi } from 'vitest' -import { AGENT_SESSION_JOURNAL_SCHEMA_VERSION } from '../../../../../../shared/agent-session/journal/agent-session-journal-types' -import type { AgentSessionJournalIdentity } from '../../../../../../shared/agent-session/journal/agent-session-journal-identity' -import { - createTrackedJournalOpener, - openTestJournalHostDatabase, - insertTestJournalRow, - publishTestJournalEpoch, - deleteTestJournalRow, - updateTestJournalRowJson -} from '../../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' -import { readAgentSessionHistory } from '../agent-session-history-page' -import { - agentSessionJournalProviderHandle, - codexProviderHandle -} from '../../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' - -const identity: AgentSessionJournalIdentity = { - sessionId: 'bounded-catch-up', - workspaceId: 'folder-workspace', - hostId: 'remote-host', - agent: 'codex', - providerHandle: codexProviderHandle('thread-1') -} -const journals = createTrackedJournalOpener() -let root: string | undefined - -afterEach(async () => { - vi.restoreAllMocks() - await journals.closeAll() - if (root) { - await rm(root, { recursive: true, force: true }) - } -}) - -async function seedJournal(count: number) { - root = await mkdtemp(join(tmpdir(), 'orca-history-read-budget-')) - const { db } = openTestJournalHostDatabase(root) - const base = { - v: AGENT_SESSION_JOURNAL_SCHEMA_VERSION, - epoch: 'epoch-1', - fence: 1, - ts: 1_000 - } - db.exec('BEGIN IMMEDIATE') - publishTestJournalEpoch(db, identity.sessionId, base.epoch) - insertTestJournalRow(db, identity.sessionId, { - ...base, - kind: 'epoch', - seq: 1, - reason: 'session_created', - providerHandle: agentSessionJournalProviderHandle(identity) - }) - for (let index = 0; index < count; index += 1) { - insertTestJournalRow(db, identity.sessionId, { - ...base, - kind: 'item', - seq: index + 2, - itemId: `item-${index}`, - revision: 1, - body: { - kind: 'message', - role: 'assistant', - blocks: [{ type: 'text', text: `${index}:${'x'.repeat(4096)}` }] - } - }) - } - db.exec('COMMIT') - return journals.open({ identity, stateDirectory: root }) -} - -describe('forward history SQL read budget', () => { - it('reports a sequence gap when the next page reaches it', async () => { - const journal = await seedJournal(6) - const { db } = openTestJournalHostDatabase(root!) - deleteTestJournalRow(db, identity.sessionId, 4) - const first = readAgentSessionHistory(journal, { - sessionId: identity.sessionId, - direction: 'after', - cursor: { epoch: journal.physicalCursor().epoch, sequence: 1 }, - limit: 2 - }) - expect(first).toMatchObject({ ok: true, page: { hasNewer: true } }) - if (!first.ok) { - throw new Error('Expected first page') - } - expect( - readAgentSessionHistory(journal, { - sessionId: identity.sessionId, - direction: 'after', - cursor: first.page.window.nextCursor, - limit: 2 - }) - ).toMatchObject({ ok: false, reset: 'journal_gap' }) - }) - - it.each(['{', '{"v":9999}'])( - 'preserves parse-stop behavior at lookahead: %s', - async (rowJson) => { - const journal = await seedJournal(6) - const { db } = openTestJournalHostDatabase(root!) - updateTestJournalRowJson(db, identity.sessionId, 4, rowJson) - const page = readAgentSessionHistory(journal, { - sessionId: identity.sessionId, - direction: 'after', - cursor: { epoch: journal.physicalCursor().epoch, sequence: 1 }, - limit: 2 - }) - expect(page).toMatchObject({ - ok: true, - page: { hasNewer: false, window: { nextCursor: { sequence: 3 } } } - }) - if (!page.ok) { - throw new Error('Expected valid prefix') - } - expect(page.page.items.map((item) => item.itemId)).toEqual(['item-0', 'item-1']) - } - ) -}) diff --git a/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-history-public-read.test.ts b/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-history-public-read.test.ts deleted file mode 100644 index 569285179d4..00000000000 --- a/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-history-public-read.test.ts +++ /dev/null @@ -1,72 +0,0 @@ -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 { 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 { - createTrackedJournalOpener, - openTestJournalHostDatabase -} from '../../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' -import type { AgentSessionJournal } from '../../../../agent-session-journal/journal-store' - -import { createAgentSessionCatchUpReader } from '../agent-session-history-page' - -const IDENTITY = { - sessionId: 'session-1', - workspaceId: 'ws-1', - hostId: 'host-1', - agent: 'codex' as const, - providerHandle: codexProviderHandle('thread-1') -} -const journals = createTrackedJournalOpener({ legacyInputs: true }) -let root: string -let journal: AgentSessionJournal -const item = (ordinal: number) => ({ - provider: 'codex' as const, - threadId: 'thread-1', - turnId: 'turn-1', - ordinal -}) - -beforeEach(async () => { - root = await mkdtemp(join(tmpdir(), 'orca-history-public-read-')) - journal = await journals.open({ identity: IDENTITY, stateDirectory: root }) -}) -afterEach(async () => { - await journals.closeAll() - await rm(root, { recursive: true, force: true }) -}) - -it('refreshes a retained catch-up reader after a same-cursor disk reload', async () => { - const options = { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } - await journal.appendItem(item(0), { kind: 'turn', turnId: 'turn-1', state: 'running' }, options) - const view = journal.publicRead() - expect(view.snapshot.items).toHaveLength(1) - const cursor = journal.cursor() - const read = createAgentSessionCatchUpReader(journal, undefined, view) - const request = { - sessionId: IDENTITY.sessionId, - direction: 'after' as const, - cursor: { epoch: cursor.epoch, sequence: 0 } - } - expect(read(request)).toMatchObject({ - ok: true, - page: { items: [{ body: { state: 'running' } }], latestTurn: { turn: { state: 'running' } } } - }) - openTestJournalHostDatabase(root) - .db.prepare( - "UPDATE journal_rows SET row_json = json_set(row_json, '$.body.state', 'completed') WHERE session_id = ? AND epoch = ? AND seq = ?" - ) - .run(IDENTITY.sessionId, cursor.epoch, cursor.sequence) - await journal.open() - expect(journal.cursor()).toEqual(cursor) - expect(read(request)).toMatchObject({ - ok: true, - page: { - items: [{ body: { state: 'completed' } }], - latestTurn: { turn: { state: 'completed' } } - } - }) -}) diff --git a/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-stop-note-contract.test-fixture.ts b/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-stop-note-contract.test-fixture.ts index 8169de443d0..550547e9878 100644 --- a/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-stop-note-contract.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-stop-note-contract.test-fixture.ts @@ -1,3 +1,4 @@ +import { readTestJournalSnapshot } from '../../../../agent-session-journal/journal-observation.test-support' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -74,16 +75,8 @@ export async function note(body: AgentJournalItemBody = unconfirmed) { await journal.appendItem(noteIdentity, body, { fence: 1, turnScope: scope }) } -export function readNote() { - return journal.snapshot().items.find((item) => item.itemId === noteId)! -} - -export function rows() { - const result = journal.readSince({ epoch: journal.physicalCursor().epoch, sequence: 0 }) - if (!result.ok) { - throw new Error(result.reset) - } - return result.rows +export async function readNote() { + return (await readTestJournalSnapshot(journal)).items.find((item) => item.itemId === noteId)! } export const fixture = { diff --git a/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-stop-note-paging.test.ts b/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-stop-note-paging.test.ts index d9d838fa2a7..1eca52435db 100644 --- a/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-stop-note-paging.test.ts +++ b/src/main/native-chat/agent-session-wire/publication/history/testing/agent-session-stop-note-paging.test.ts @@ -12,7 +12,6 @@ import { note, noteId, ordinary, - rows, scope, turn, unconfirmed @@ -37,7 +36,7 @@ it('shrinks the triggering row window without dropping the dependent note', asyn ) } await turn('interrupted') - const first = readAgentSessionHistory(fixture.journal, { + const first = await readAgentSessionHistory(fixture.journal, { sessionId: 'session-1', direction: 'after', cursor: held, @@ -47,7 +46,7 @@ it('shrinks the triggering row window without dropping the dependent note', asyn throw new Error(first.reset) } expect(first.page.hasNewer).toBe(true) - const next = readAgentSessionHistory(fixture.journal, { + const next = await readAgentSessionHistory(fixture.journal, { sessionId: 'session-1', direction: 'after', cursor: first.page.window.nextCursor, @@ -74,16 +73,8 @@ it('refreshes at the exact tip using bounded frames and no durable writes', asyn } ) } - const stored = rows() - const events: AgentSessionSubscribeEvent[] = [] - new AgentSessionSubscribers().open({ - id: 'reconnect', - sessionId: 'session-1', - journal: fixture.journal, - fence: 1, - cursor: fixture.journal.cursor(), - emit: (event) => events.push(event) - }) + const stored = fixture.journal.physicalCursor() + const events = await refreshedEvents(false) const batches = events.flatMap((event) => (event.type === 'batch' ? [event.batch] : [])) expect(batches.flatMap((batch) => batch.items)).toHaveLength(205) expect( @@ -102,7 +93,7 @@ it('refreshes at the exact tip using bounded frames and no durable writes', asyn item.body.kind === 'status' && item.body.text === ordinary.text && !item.body.failure ) ).toBe(true) - expect(rows()).toEqual(stored) + expect(fixture.journal.physicalCursor()).toEqual(stored) }) it('refreshes a phone within its frames, in bounded pages', async () => { @@ -117,16 +108,7 @@ it('refreshes a phone within its frames, in bounded pages', async () => { } ) } - const events: AgentSessionSubscribeEvent[] = [] - new AgentSessionSubscribers().open({ - id: 'phone', - sessionId: 'session-1', - journal: fixture.journal, - fence: 1, - cursor: fixture.journal.cursor(), - emit: (event) => events.push(event), - boundedFrames: true - }) + const events = await refreshedEvents(true) const batches = events.flatMap((event) => (event.type === 'batch' ? [event.batch] : [])) expect(batches.filter((batch) => batch.items.length > 0)).toHaveLength(2) expect(batches.flatMap((batch) => batch.items)).toHaveLength(205) @@ -136,3 +118,30 @@ it('refreshes a phone within its frames, in bounded pages', async () => { ) } }) + +async function refreshedEvents(boundedFrames: boolean): Promise { + const events: AgentSessionSubscribeEvent[] = [] + const delivered = Promise.withResolvers() + const close = new AgentSessionSubscribers().open({ + id: 'reconnect', + sessionId: 'session-1', + journal: fixture.journal, + fence: 1, + cursor: fixture.journal.cursor(), + boundedFrames, + emit: (event) => { + events.push(event) + if ( + events.flatMap((frame) => (frame.type === 'batch' ? frame.batch.items : [])).length >= 205 + ) { + delivered.resolve() + } + } + }) + try { + await delivered.promise + return events + } finally { + close() + } +} diff --git a/src/main/native-chat/agent-session-wire/publication/history/testing/structured-agent-session-retained-materializer-counters.test-fixture.ts b/src/main/native-chat/agent-session-wire/publication/history/testing/structured-agent-session-retained-materializer-counters.test-fixture.ts deleted file mode 100644 index 8f527cbbd99..00000000000 --- a/src/main/native-chat/agent-session-wire/publication/history/testing/structured-agent-session-retained-materializer-counters.test-fixture.ts +++ /dev/null @@ -1,117 +0,0 @@ -import { vi } from 'vitest' -import type { AgentSessionJournal } from '../../../../agent-session-journal/journal-store' - -export function createRetainedMaterializerCounters( - count: () => { fullInputs: number; fullViews: number; fullIterations: number }, - visit: (value: unknown) => void, - inputMaps: WeakSet -) { - const readers: (() => void)[] = [] - const watched = new WeakSet() - const watchJournal = (journal: AgentSessionJournal) => { - if (watched.has(journal)) { - return - } - watched.add(journal) - inputMaps.add(journal['state'].submissions) - const submissions = journal.submissions - const inputs = vi.spyOn(journal, 'submissions').mockImplementation(() => { - count().fullInputs++ - return submissions() - }) - const publicRead = journal.publicRead - const seenViews = new WeakSet() - const read = vi.spyOn(journal, 'publicRead').mockImplementation(() => { - const view = publicRead() - if (!seenViews.has(view)) { - seenViews.add(view) - const getter = Object.getOwnPropertyDescriptor(view, 'snapshot')!.get! - const full = vi.spyOn(view, 'snapshot', 'get').mockImplementation(() => { - count().fullViews++ - return getter.call(view) - }) - readers.push(() => full.mockRestore()) - } - return view - }) - readers.push( - () => inputs.mockRestore(), - () => read.mockRestore() - ) - } - const values = Map.prototype.values, - keys = Map.prototype.keys, - entries = Map.prototype.entries - const mv = vi.spyOn(Map.prototype, 'values').mockImplementation(function* ( - this: Map - ) { - if (inputMaps.has(this)) { - count().fullIterations++ - } - for (const value of values.call(this)) { - visit(value) - yield value - } - return undefined - }) - const mk = vi.spyOn(Map.prototype, 'keys').mockImplementation(function* ( - this: Map - ) { - if (inputMaps.has(this)) { - count().fullIterations++ - } - for (const key of keys.call(this)) { - visit(key) - yield key - } - return undefined - }) - const me = vi.spyOn(Map.prototype, 'entries').mockImplementation(function* ( - this: Map - ) { - if (inputMaps.has(this)) { - count().fullIterations++ - } - for (const entry of entries.call(this)) { - visit(entry) - yield entry - } - return undefined - }) - const mi = vi.spyOn(Map.prototype, Symbol.iterator).mockImplementation(function* ( - this: Map - ) { - if (inputMaps.has(this)) { - count().fullIterations++ - } - for (const entry of entries.call(this)) { - visit(entry) - yield entry - } - return undefined - }) - const mf = vi.spyOn(Map.prototype, 'forEach').mockImplementation(function ( - this: Map, - callback, - receiver - ) { - if (inputMaps.has(this)) { - count().fullIterations++ - } - for (const [key, value] of entries.call(this)) { - visit([key, value]) - callback.call(receiver, value, key, this) - } - }) - return { - watchJournal, - dispose: () => { - for (const restore of readers.toReversed()) { - restore() - } - for (const spy of [mv, mk, me, mi, mf]) { - spy.mockRestore() - } - } - } -} diff --git a/src/main/native-chat/agent-session-wire/publication/history/testing/structured-agent-session-retained-read-counters.test-fixture.ts b/src/main/native-chat/agent-session-wire/publication/history/testing/structured-agent-session-retained-read-counters.test-fixture.ts deleted file mode 100644 index 288a508219f..00000000000 --- a/src/main/native-chat/agent-session-wire/publication/history/testing/structured-agent-session-retained-read-counters.test-fixture.ts +++ /dev/null @@ -1,293 +0,0 @@ -import { vi } from 'vitest' -import { createRetainedMaterializerCounters } from './structured-agent-session-retained-materializer-counters.test-fixture' -import * as reducer from '../../../../agent-session-journal/projection/journal-reducer' -import * as transitions from '../../../../agent-session-journal/projection/journal-reducer-transition' -import { JournalInputOrder } from '../../../../agent-session-journal/input-order/journal-input-order' -import { JournalQueuedMessages } from '../../../../agent-session-journal/queued-messages/journal-queued-messages' -import { JournalInputCardIndex } from '../../../../agent-session-journal/input-order/journal-input-card-index' -import { JournalInputPlaceIndex } from '../../../../agent-session-journal/input-order/journal-input-place-index' - -function counterProperty(target: T, key: PropertyKey) { - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: Proxy keys forward unchanged; absent properties read as undefined. - return target[key as keyof T] -} - -export function createRetainedReadCounters() { - type Counts = { - fullViews: number - fullInputs: number - fullIterations: number - fullRenders: number - archived: number - records: number - indexed: number - restores: number - nodes: number - drafts: number - points: number - paths: number - endpoints: number - statusRecords: number - statusItems: number - } - const phases: Record = {} - let phase = 'setup' - const count = () => - (phases[phase] ??= { - fullViews: 0, - fullInputs: 0, - fullIterations: 0, - fullRenders: 0, - archived: 0, - records: 0, - indexed: 0, - restores: 0, - nodes: 0, - drafts: 0, - points: 0, - paths: 0, - endpoints: 0, - statusRecords: 0, - statusItems: 0 - }) - const archived = (id: unknown) => - typeof id === 'string' && /^archive-(withdrawn|sent|input|stopped)-/.test(id) - const visit = (value: unknown): void => { - if (Array.isArray(value)) { - for (const item of value) { - visit(item) - } - return - } - if (typeof value === 'string') { - if (archived(value)) { - count().archived++ - } - return - } - if (!value || typeof value !== 'object') { - return - } - const id = - 'messageId' in value - ? value.messageId - : 'clientMessageId' in value - ? value.clientMessageId - : 'card' in value && - value.card && - typeof value.card === 'object' && - 'messageId' in value.card - ? value.card.messageId - : undefined - if (typeof id === 'string') { - count().records++ - if (archived(id)) { - count().archived++ - } - } - } - const inputMaps = new WeakSet() - const materializers = createRetainedMaterializerCounters(count, visit, inputMaps) - type Tree = NonNullable - let querying = 0 - const observeNode = (node: Tree | undefined): Tree | undefined => - node && - new Proxy(node, { - get(target, key) { - count().paths++ - return key === 'left' || key === 'right' - ? observeNode(target[key]) - : counterProperty(target, key) - } - }) - const atPath = (index: JournalInputPlaceIndex, run: () => T): T => { - const root = index['all'] - index['all'] = observeNode(root) - try { - return run() - } finally { - index['all'] = root - } - } - const firstPosition = JournalInputPlaceIndex.prototype.firstPosition - const lastId = JournalInputPlaceIndex.prototype.lastId - const successorId = JournalInputPlaceIndex.prototype.successorId - const idsAfter = JournalInputPlaceIndex.prototype.idsAfter - const pathSpies = [ - vi.spyOn(JournalInputPlaceIndex.prototype, 'idsAfter').mockImplementation(function ( - this: JournalInputPlaceIndex, - sequence - ) { - return atPath(this, () => idsAfter.call(this, sequence)) - }), - vi.spyOn(JournalInputPlaceIndex.prototype, 'firstPosition').mockImplementation(function ( - this: JournalInputPlaceIndex - ) { - return atPath(this, () => firstPosition.call(this)) - }), - vi.spyOn(JournalInputPlaceIndex.prototype, 'lastId').mockImplementation(function ( - this: JournalInputPlaceIndex - ) { - return atPath(this, () => lastId.call(this)) - }), - vi.spyOn(JournalInputPlaceIndex.prototype, 'successorId').mockImplementation(function ( - this: JournalInputPlaceIndex, - sequence - ) { - return atPath(this, () => successorId.call(this, sequence)) - }) - ] - const query = JournalInputPlaceIndex.prototype.query - const queries = vi.spyOn(JournalInputPlaceIndex.prototype, 'query').mockImplementation(function ( - this: JournalInputPlaceIndex, - ...args - ) { - querying++ - try { - return query.apply(this, args) - } finally { - querying-- - } - }) - const status = JournalInputOrder.prototype.statusInputs - const stoppedInputs = JournalInputOrder.prototype.stoppedHistoryInputs - const stoppedReads = vi - .spyOn(JournalInputOrder.prototype, 'stoppedHistoryInputs') - .mockImplementation(function (this: JournalInputOrder, ...args) { - return new Proxy(stoppedInputs.apply(this, args), { - get(target, key) { - const value = counterProperty(target, key) - if (typeof key === 'string' && /^\d+$/.test(key)) { - visit(value) - } - return value - } - }) - }) - const statuses = vi - .spyOn(JournalInputOrder.prototype, 'statusInputs') - .mockImplementation(function (this: JournalInputOrder, ...args) { - const result = status.apply(this, args) - return new Proxy(result, { - get(target, key) { - const value = counterProperty(target, key) - if (typeof key === 'string' && /^\d+$/.test(key)) { - count().statusRecords++ - const input = target[Number(key)] - if (input && archived(input.clientMessageId)) { - count().endpoints++ - } - } - return value - } - }) - }) - const renderState = reducer.renderJournalState - const fullRenders = vi.spyOn(reducer, 'renderJournalState').mockImplementation((...args) => { - count().fullRenders++ - return renderState(...args) - }) - const renderItems = reducer.renderJournalItems - const rendered = vi.spyOn(reducer, 'renderJournalItems').mockImplementation( - (...args) => - new Proxy(renderItems(...args), { - get(target, key) { - if (typeof key === 'string' && /^\d+$/.test(key)) { - count().statusItems++ - } - return counterProperty(target, key) - } - }) - ) - const get = Map.prototype.get - const mg = vi.spyOn(Map.prototype, 'get').mockImplementation(function ( - this: Map, - key - ) { - if (archived(key)) { - count().points++ - } - const value = get.call(this, key) - return querying && value && typeof value === 'object' && 'height' in value && 'entry' in value - ? observeNode(value) - : value - }) - const list = JournalQueuedMessages.prototype.list - const lists = vi.spyOn(JournalQueuedMessages.prototype, 'list').mockImplementation(function ( - this: JournalQueuedMessages - ) { - return new Proxy(list.call(this), { - get(target, key) { - const value: unknown = counterProperty(target, key) - if (typeof key === 'string' && /^\d+$/.test(key)) { - visit(value) - } - return value - } - }) - }) - const index = JournalInputCardIndex.prototype.indexCard - const indexed = vi - .spyOn(JournalInputCardIndex.prototype, 'indexCard') - .mockImplementation(function (this: JournalInputCardIndex, ...args) { - count().indexed++ - visit(args[1]) - return index.apply(this, args) - }) - const restore = JournalInputOrder.prototype.restore - const restores = vi.spyOn(JournalInputOrder.prototype, 'restore').mockImplementation(function ( - this: JournalInputOrder, - ...args - ) { - inputMaps.add(args[0].submissions) - count().restores++ - return restore.apply(this, args) - }) - const reflect = Reflect.get - const nodes = vi.spyOn(Reflect, 'get').mockImplementation((...args) => { - if ('height' in args[0] && 'entry' in args[0]) { - count().nodes++ - } - return reflect(...args) - }) - const isolate = transitions.isolateJournalTransition - const drafts = vi.spyOn(transitions, 'isolateJournalTransition').mockImplementation((...args) => { - const draft = isolate(...args) - return { - ...draft, - adopt: () => { - const result = draft.adopt() - count().drafts += draft.trackedDerived() - return result - } - } - }) - return { - phases, - archived, - count, - watchJournal: materializers.watchJournal, - setPhase: (name: string) => { - phase = name - }, - dispose: () => { - materializers.dispose() - for (const spy of [ - mg, - lists, - indexed, - restores, - nodes, - drafts, - statuses, - stoppedReads, - rendered, - fullRenders, - queries, - ...pathSpies - ]) { - spy.mockRestore() - } - } - } -} diff --git a/src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-wire-admission.test.ts b/src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-wire-admission.test.ts index 1df8dfe6a8d..61f0ba730ff 100644 --- a/src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-wire-admission.test.ts +++ b/src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-wire-admission.test.ts @@ -2,7 +2,7 @@ import { readTestJournalSnapshot } from '../../../../agent-session-journal/journ import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' -import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import type { AgentJournalItemIdentity } from '../../../../../../shared/agent-session/journal/agent-session-journal-identity' import type { AgentSessionSubscribeEvent } from '../../../../../../shared/agent-session/agent-session-wire' import type { AgentSessionBackgroundTaskState } from '../../../../../../shared/agent-session/agent-session-background-task-wire' @@ -10,18 +10,13 @@ import { REMOTE_RUNTIME_MAX_OUTBOUND_JSON_BYTES } from '../../../../../../shared import { mobileE2EETextPayloadAdmissionBytes } from '../../../../../runtime/rpc/mobile-e2ee-outbound-admission' import { type AgentJournalItemBody, - AGENT_JOURNAL_THREAD_SCOPE, - AGENT_SESSION_JOURNAL_SCHEMA_VERSION + AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../../shared/agent-session/journal/agent-session-journal-types' -import type { JournalRow } from '../../../../agent-session-journal/durable-rows/journal-row-schema' import type { AgentSessionJournal } from '../../../../agent-session-journal/journal-store' -import { - createTrackedJournalOpener, - openTestJournalHostDatabase, - insertTestJournalRow -} 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 { readAgentSessionHistory } from '../../history/agent-session-history-page' import { AgentSessionSubscribers } from '../structured-agent-session-subscribers' +import { withTestAgentSessionDatabase } from '../../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { codexProviderHandle } from '../../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' const SESSION = 'wire-admission-session' @@ -72,14 +67,13 @@ describe('structured agent-session outbound admission', () => { fence: 1, emit: (event) => initial.push(event) }) - expect(initial).toHaveLength(1) + await vi.waitFor(() => expect(initial).toHaveLength(1)) expect(initial[0]).toMatchObject({ type: 'snapshot', page: { hasOlder: true } }) expectAdmitted(initial[0]) roster = { state: 'monitoring' } subscribers.republishBackgroundTasks(SESSION, 2) - subscribers.snapshot(SESSION, journal, 2) - expect(initial.slice(1)).toHaveLength(2) + await vi.waitFor(() => expect(initial.slice(1)).toHaveLength(2)) initial.slice(1).forEach(expectAdmitted) const epochReset: AgentSessionSubscribeEvent[] = [] @@ -91,9 +85,10 @@ describe('structured agent-session outbound admission', () => { cursor: { epoch: 'retired-epoch', sequence: 1 }, emit: (event) => epochReset.push(event) }) + await vi.waitFor(() => expect(epochReset.length).toBeGreaterThan(0)) expect(epochReset[0]).toMatchObject({ type: 'reset', reset: 'epoch_changed' }) expectAdmitted(epochReset[0]) - const epochHistory = readAgentSessionHistory(journal, { + const epochHistory = await readAgentSessionHistory(journal, { sessionId: SESSION, direction: 'before', cursor: { epoch: 'retired-epoch', sequence: 1 } @@ -104,7 +99,7 @@ describe('structured agent-session outbound admission', () => { // Epochs no longer shed rows; exercise cursor_compacted via an over-budget legacy page. const cursorBefore = journal.cursor() const overBudget = await reopenWithOversizedRemoval(cursorBefore.sequence) - const history = readAgentSessionHistory(overBudget, { + const history = await readAgentSessionHistory(overBudget, { sessionId: SESSION, direction: 'after', cursor: cursorBefore @@ -115,10 +110,10 @@ describe('structured agent-session outbound admission', () => { dispose() }) - it('replaces an over-budget catch-up with one admitted newest page', () => { + it('replaces an over-budget catch-up with one admitted newest page', async () => { const subscribers = new AgentSessionSubscribers() const catchup: AgentSessionSubscribeEvent[] = [] - const firstItemSequence = journal.snapshot().items[0]!.sequence + const firstItemSequence = (await readTestJournalSnapshot(journal)).items[0]!.sequence subscribers.open({ id: 'catchup', @@ -129,7 +124,7 @@ describe('structured agent-session outbound admission', () => { emit: (event) => catchup.push(event) }) - expect(catchup).toHaveLength(1) + await vi.waitFor(() => expect(catchup).toHaveLength(1)) catchup.forEach(expectAdmitted) expect(catchup[0]).toMatchObject({ type: 'snapshot', @@ -157,34 +152,21 @@ function body(text: string): AgentJournalItemBody { /** Inject a legacy oversized removal ID to force a cursor_compacted reset. */ async function reopenWithOversizedRemoval(afterSequence: number): Promise { const hugeItemId = `codex:thread-1:${'h'.repeat(5 * 1024 * 1024)}:1` - const base = { - v: AGENT_SESSION_JOURNAL_SCHEMA_VERSION, - epoch: journal.physicalCursor().epoch, - fence: 1, - ts: 1 - } - const rows: JournalRow[] = [ - { - ...base, - kind: 'item', - itemId: hugeItemId, - revision: 1, - seq: afterSequence + 1, - body: { kind: 'message', role: 'assistant', blocks: [{ type: 'text', text: 'big' }] } - }, - { ...base, kind: 'tombstone', itemId: hugeItemId, revision: 2, seq: afterSequence + 2 } - ] + const epoch = journal.physicalCursor().epoch await journal.close() - const opened = openTestJournalHostDatabase(root) - try { - opened.db.exec('BEGIN IMMEDIATE') - for (const row of rows) { - insertTestJournalRow(opened.db, SESSION, row) - } - opened.db.exec('COMMIT') - } finally { - await opened.close() - } + await withTestAgentSessionDatabase(root, (db) => { + db.prepare('INSERT INTO journal_events VALUES(?,?,?,?,?)').run( + SESSION, + epoch, + afterSequence + 1, + 1, + JSON.stringify({ items: [], removedItemIds: [hugeItemId], submissions: [], cards: [] }) + ) + db.prepare('UPDATE journal_chat_state SET last_sequence=? WHERE session_id=?').run( + afterSequence + 1, + SESSION + ) + }) return journals.open({ identity: { sessionId: SESSION, diff --git a/src/main/native-chat/agent-session-wire/recovery/testing/structured-agent-session-recovery-resolution.test.ts b/src/main/native-chat/agent-session-wire/recovery/testing/structured-agent-session-recovery-resolution.test.ts index eb3f171ba41..eb675631cc7 100644 --- a/src/main/native-chat/agent-session-wire/recovery/testing/structured-agent-session-recovery-resolution.test.ts +++ b/src/main/native-chat/agent-session-wire/recovery/testing/structured-agent-session-recovery-resolution.test.ts @@ -5,7 +5,10 @@ import { join } from 'node:path' import { afterEach, describe, expect, it, vi } from 'vitest' import type { AgentSessionOwnerProbe } from '../../../../../shared/agent-session/lifecycle/agent-session-lease-adjudication' import type { AgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store' -import { openTestAgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' +import { + openTestAgentSessionRecordStore, + seedTestAgentSessionRecordStore +} from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { resolveStructuredSessionRecovery, type StructuredSessionRecoveryResolutionDeps @@ -16,6 +19,7 @@ const NOW = 1_800_000_000_000 const SESSION = 'session-recovery' const roots: string[] = [] let operations = 0 +const directories = new WeakMap() afterEach(async () => { await closeTestJournalHostDatabases() @@ -29,7 +33,10 @@ async function newStoreDirectory(): Promise { } async function openStore(directory?: string): Promise { - return openTestAgentSessionRecordStore(directory ?? (await newStoreDirectory())) + const root = directory ?? (await newStoreDirectory()) + const store = await openTestAgentSessionRecordStore(root) + directories.set(store, root) + return store } async function reserve(store: AgentSessionRecordStore) { @@ -87,10 +94,12 @@ async function liveOwner(store: AgentSessionRecordStore, pid = 4242) { } async function latch(store: AgentSessionRecordStore) { - return store.transitionHandoff(SESSION, (record) => ({ - ...record, - lease: { ...record.lease, handoffStage: 'recovering' } - })) + const directory = directories.get(store)! + const record = store.getRecord(SESSION)! + await seedTestAgentSessionRecordStore(directory, { + records: [{ ...record, lease: { ...record.lease, handoffStage: 'recovering' } }] + }) + return openStore(directory) } function deps( @@ -114,9 +123,9 @@ function deps( describe('structured session recovery resolution', () => { it('releases an owner whose identity cannot be verified, and signals nothing', async () => { - const store = await openStore() + let store = await openStore() await liveOwner(store) - await latch(store) + store = await latch(store) const stopOwnerProcess = vi.fn() const result = await resolveStructuredSessionRecovery( @@ -139,9 +148,9 @@ describe('structured session recovery resolution', () => { }) it('releases a Windows owner the probe matches without signalling its saved pid', async () => { - const store = await openStore() + let store = await openStore() await liveOwner(store) - await latch(store) + store = await latch(store) const stopOwnerProcess = vi.fn() const result = await resolveStructuredSessionRecovery( diff --git a/src/main/native-chat/agent-session-wire/recovery/testing/structured-agent-session-restart-resume.test-fixture.ts b/src/main/native-chat/agent-session-wire/recovery/testing/structured-agent-session-restart-resume.test-fixture.ts index e0a18179c85..a2682706deb 100644 --- a/src/main/native-chat/agent-session-wire/recovery/testing/structured-agent-session-restart-resume.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/recovery/testing/structured-agent-session-restart-resume.test-fixture.ts @@ -6,8 +6,6 @@ import type { import type { AgentSessionRecord } from '../../../../../shared/agent-session/lifecycle/agent-session-record' import type { AgentSessionResumeMarker } from '../../../../../shared/agent-session/lifecycle/agent-session-resume-marker' import type { AgentChildWorkView } from '../../../../../shared/agent-status/child-work/agent-status-child-work-view' -import { runningStructuredAgentSessionTurnItemBySequence } from '../../../../../shared/agent-session/view/structured-agent-session-live-turn' -import type { StructuredAgentSessionCurrentWorkJournal } from '../../observations/structured-agent-session-work-evidence' import { structuredAgentSessionResumableSet } from '../structured-agent-session-restart-resume-set' import { claudeProviderHandle, @@ -129,51 +127,6 @@ export function claudeRecord( } as unknown as AgentSessionRecord } -export type HarnessJournal = { - snapshot: () => { - items: AgentJournalRenderItem[] - submissions: AgentJournalSubmission[] - cursor: { epoch: string; sequence: number } - } - submissions: () => AgentJournalSubmission[] - appendItem: (envelope: unknown, body: { kind: string; text: string }) => Promise -} - -export type HarnessSession = { journal: HarnessJournal; child: { fence: number } | null } - -export function journal( - items: AgentJournalRenderItem[], - submissions: AgentJournalSubmission[] = [] -) { - const currentWork: StructuredAgentSessionCurrentWorkJournal & { - inputOrder: { inputOrdinal: (id: string) => number } - } = { - item: (itemId) => items.find((item) => item.itemId === itemId) ?? null, - cursor: () => ({ epoch: EPOCH, sequence: items.length }), - visitItems: (visit) => { - for (const item of items) { - visit(item.itemId, item.sequence, item.body) - } - }, - runningTurn: () => runningStructuredAgentSessionTurnItemBySequence(items), - submissions: () => submissions, - inputOrder: { - inputOrdinal: (id: string) => submissions.findIndex((input) => input.clientMessageId === id), - recoverableSubmissions: () => - submissions.filter((s) => s.dispatchState === 'pending' || s.dispatchState === 'unknown') - }, - itemFence: () => undefined, - wroteBeforeOpen: () => false - } - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: current-work reads are checked above; the remaining exercised methods use the held fixture, without an on-disk SQLite store. - return { - ...currentWork, - snapshot: () => ({ items, submissions, cursor: { epoch: EPOCH, sequence: items.length } }), - statusRead: () => ({ items, submissions }), - appendItem: async () => undefined - } as never -} - export function marker( overrides: Partial = {} ): AgentSessionResumeMarker { diff --git a/src/main/native-chat/agent-session-wire/rewind/testing/structured-agent-session-rewind-provenance.test.ts b/src/main/native-chat/agent-session-wire/rewind/testing/structured-agent-session-rewind-provenance.test.ts deleted file mode 100644 index 4d0e27d307b..00000000000 --- a/src/main/native-chat/agent-session-wire/rewind/testing/structured-agent-session-rewind-provenance.test.ts +++ /dev/null @@ -1,118 +0,0 @@ -import { mkdtemp, rm } from 'node:fs/promises' -import { tmpdir } from 'node:os' -import { join } from 'node:path' -import { afterEach, beforeEach, describe, expect, it } from 'vitest' -import { agentJournalItemKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' -import { - AGENT_JOURNAL_THREAD_SCOPE, - type AgentJournalItemBody -} 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 } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' -import type { AgentSessionJournal } from '../../../agent-session-journal/journal-store' -import { StructuredAgentSessionCurrentWork } from '../../observations/structured-agent-session-current-work' -import { structuredRewindCut } from '../structured-rewind-cut' - -const IDENTITY: AgentSessionJournalIdentity = { - sessionId: 'session-1', - workspaceId: 'ws-1', - hostId: 'host-1', - agent: 'codex', - providerHandle: codexProviderHandle('thread-1') -} -const DEAD = 1 -const LIVE = 3 - -function codexItem(turnId: string, ordinal: number): AgentJournalItemIdentity { - return { provider: 'codex', threadId: 'thread-1', turnId, ordinal } -} -const TURN = codexItem('turn-dead', 0) -const PROMPT = codexItem('turn-dead', 1) -const REPLY = codexItem('turn-dead', 2) -const UNDONE = codexItem('turn-undone', 0) -const PENDING = { - state: 'pending' as const, - selectedOptionId: null, - resolvedBy: null, - resolvedAt: null -} -const REPLY_BODY: AgentJournalItemBody = { - kind: 'message', - role: 'assistant', - blocks: [{ type: 'text', text: 'partial' }] -} - -const UNDONE_BODY: AgentJournalItemBody = { - kind: 'message', - role: 'user', - blocks: [{ type: 'text', text: 'undo me' }] -} - -let root: string -const journals = createTrackedJournalOpener() - -beforeEach(async () => { - root = await mkdtemp(join(tmpdir(), 'orca-rewind-provenance-')) -}) - -afterEach(async () => { - await journals.closeAll() - await rm(root, { recursive: true, force: true }) -}) - -async function leftByDeadGeneration(): Promise { - const journal = await journals.open({ identity: IDENTITY, stateDirectory: root }) - const scope = { fence: DEAD, ownerFence: DEAD, turnScope: AGENT_JOURNAL_THREAD_SCOPE } - await journal.appendItem(TURN, { kind: 'turn', turnId: 'turn-dead', state: 'running' }, scope) - const inTurn = { - ...scope, - turnScope: { kind: 'turn' as const, turnItemId: agentJournalItemKey(TURN) } - } - await journal.appendItem( - PROMPT, - { - kind: 'approval', - title: 'Run command?', - detail: null, - options: [{ id: 'yes', label: 'Allow' }], - resolution: PENDING - }, - inTurn - ) - await journal.appendItem(REPLY, REPLY_BODY, inTurn) - await journal.appendItem(UNDONE, UNDONE_BODY, { ...scope, fence: LIVE, ownerFence: LIVE }) - return journal -} - -async function rewindAtLiveFence(journal: AgentSessionJournal): Promise { - const cut = structuredRewindCut(journal, { - itemId: agentJournalItemKey(UNDONE), - provider: 'codex', - nativeId: 'thread-1' - }) - await journal.replaceEpochItems('handle_forked', LIVE, cut!.kept) -} - -describe('a rewind after a failed cleanup', () => { - it("does not promote the dead generation's turn, prompt or reply to the live one", async () => { - const journal = await leftByDeadGeneration() - const before = journal.physicalCursor().epoch - - await rewindAtLiveFence(journal) - - expect(journal.physicalCursor().epoch).not.toBe(before) - const work = new StructuredAgentSessionCurrentWork(journal, LIVE) - // New row fence must not promote old execution provenance. - expect(await journal.reads.itemFence(agentJournalItemKey(TURN))).toBe(DEAD) - expect(await journal.reads.itemFence(agentJournalItemKey(PROMPT))).toBe(DEAD) - expect(await journal.reads.itemFence(agentJournalItemKey(REPLY))).toBe(DEAD) - expect(work.activeTurnId()).toBeNull() - expect(work.actionablePromptIds()).toEqual([]) - expect(work.working()).toBe(false) - expect(await journal.reads.itemBody(agentJournalItemKey(UNDONE))).toBeNull() - }) -}) diff --git a/src/main/native-chat/agent-session-wire/rewind/testing/structured-rewind-cut.test.ts b/src/main/native-chat/agent-session-wire/rewind/testing/structured-rewind-cut.test.ts deleted file mode 100644 index e0e8ab1ac20..00000000000 --- a/src/main/native-chat/agent-session-wire/rewind/testing/structured-rewind-cut.test.ts +++ /dev/null @@ -1,90 +0,0 @@ -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 { agentJournalItemKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' -import { - AGENT_JOURNAL_THREAD_SCOPE, - type AgentJournalItemBody -} 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 } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' -import { structuredRewindCut } from '../structured-rewind-cut' - -const IDENTITY: AgentSessionJournalIdentity = { - sessionId: 'session-1', - workspaceId: 'ws-1', - hostId: 'host-1', - agent: 'codex', - providerHandle: codexProviderHandle('main') -} - -function item(threadId: string, turnId: string, ordinal = 0): AgentJournalItemIdentity { - return { provider: 'codex', threadId, turnId, ordinal } -} -function text(value: string): AgentJournalItemBody { - return { kind: 'message', role: 'assistant', blocks: [{ type: 'text', text: value }] } -} - -let root: string -const journals = createTrackedJournalOpener() - -beforeEach(async () => { - root = await mkdtemp(join(tmpdir(), 'orca-rewind-cut-')) -}) - -afterEach(async () => { - await journals.closeAll() - await rm(root, { recursive: true, force: true }) -}) - -it("keeps a subagent row with the main turn it arrived in, across the subagent's own turn", async () => { - const journal = await journals.open({ identity: IDENTITY, stateDirectory: root }) - const append = async (identity: AgentJournalItemIdentity, value: string, turn?: string) => { - const record = turn ? { provider: 'orca' as const, clientMessageId: `turn-${turn}` } : null - await journal.appendItem(identity, text(value), { - fence: 1, - turnScope: record - ? { kind: 'turn', turnItemId: agentJournalItemKey(record) } - : AGENT_JOURNAL_THREAD_SCOPE - }) - } - for (const turn of ['a', 'b']) { - await journal.appendItem( - { provider: 'orca', clientMessageId: `turn-${turn}` }, - { kind: 'turn', turnId: turn, state: 'completed' }, - { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } - ) - } - await append(item('main', 'a'), 'main a') - // One child turn spans both main turns, so membership must follow main-turn scope. - await append(item('child', 'c', 0), 'child in a', 'a') - const mainB: AgentJournalItemBody = { - kind: 'message', - role: 'user', - blocks: [{ type: 'text', text: 'main b' }] - } - await journal.appendItem(item('main', 'b'), mainB, { - fence: 1, - turnScope: AGENT_JOURNAL_THREAD_SCOPE - }) - await append(item('child', 'c', 1), 'child in b', 'b') - await append(item('child', 'c', 2), 'late child in a', 'a') - - const cut = structuredRewindCut(journal, { - itemId: agentJournalItemKey(item('main', 'b')), - provider: 'codex', - nativeId: 'main' - }) - - expect(cut).toMatchObject({ beforeTurnId: 'b', keptTurnId: 'a' }) - expect(cut!.kept.map(({ body }) => body)).toEqual( - expect.arrayContaining([text('main a'), text('child in a'), text('late child in a')]) - ) - expect(cut!.kept.map(({ body }) => body)).not.toContainEqual(text('child in b')) - expect(cut!.kept.map(({ body }) => body)).not.toContainEqual(mainB) -}) diff --git a/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-eviction.test.ts b/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-eviction.test.ts index ee079f14467..aaaaf626402 100644 --- a/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-eviction.test.ts +++ b/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-eviction.test.ts @@ -1,9 +1,20 @@ -import { describe, expect, it } from 'vitest' +import { afterEach, describe, expect, it } from 'vitest' +import { mkdtemp, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { createTrackedJournalOpener } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { codexProviderHandle } from '../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import { evictStructuredAgentSession } from '../structured-agent-session-eviction' import { StructuredAgentSessionHostRuntimeState } from '../../host/structured-agent-session-host-runtime-state' -import { withJournalQueueMembers } from '../../journal-ingress/testing/structured-agent-session-journal-double.test-fixture' import { recordingStructuredAgentSessionLogger } from '../../host/testing/structured-agent-session-logger.test-fixture' +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 }))) +}) + function runtimeState(): StructuredAgentSessionHostRuntimeState { // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: eviction against the sink cache reads only the sinks and the logger; store and adapter are never reached. const deps = { @@ -27,11 +38,22 @@ describe('eviction against the real sink cache', () => { acknowledgeRelease: () => {} }) + const root = await mkdtemp(join(tmpdir(), 'orca-eviction-')) + roots.push(root) + const journal = await journals.open({ + identity: { + sessionId, + workspaceId: 'workspace', + hostId: 'local', + agent: 'codex', + providerHandle: codexProviderHandle('thread') + }, + stateDirectory: root + }) const published: string[] = [] const reattached = state.eventSinkFor(sessionId) reattached.bind({ - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: these rows are publications only, which reach nothing on the journal but its in-order read. - journal: withJournalQueueMembers({ appendItem: async () => ({}) }) as never, + journal, fence: 2, publish: () => published.push('published') }) diff --git a/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-late-proof-tool-ends.test.ts b/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-late-proof-tool-ends.test.ts index 4cd6da82437..9dfc57ef6a4 100644 --- a/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-late-proof-tool-ends.test.ts +++ b/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-late-proof-tool-ends.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalSnapshot } from '../../../agent-session-journal/journal-observation.test-support' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -90,16 +91,18 @@ function settle(fence: number, deathEvidence: AgentSessionDeathEvidence | null) }) } -function turnState(turnId: string) { - const item = journal - .snapshot() - .items.find((row) => readAgentJournalTurn(row.body)?.turnId === turnId) +async function turnState(turnId: string) { + const item = (await readTestJournalSnapshot(journal)).items.find( + (row) => readAgentJournalTurn(row.body)?.turnId === turnId + ) return readAgentJournalTurn(item?.body)?.state } -function callLifecycle(turnId: string, ordinal: number) { +async function callLifecycle(turnId: string, ordinal: number) { const key = agentJournalItemKey(identityOf(turnId, ordinal)) - const body = journal.snapshot().items.find((item) => item.itemId === key)?.body + const body = (await readTestJournalSnapshot(journal)).items.find( + (item) => item.itemId === key + )?.body return body?.kind === 'tool-call' ? agentJournalToolCallLifecycle(body) : undefined } diff --git a/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-settlement-double.test-fixture.ts b/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-settlement-double.test-fixture.ts deleted file mode 100644 index 8dc3fbefacb..00000000000 --- a/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-settlement-double.test-fixture.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { - settleJournalStaleState, - settleJournalDeadGeneration -} from '../../../agent-session-journal/writing/journal-generation-settlement' -import type { AgentJournalCursor } from '../../../../../shared/agent-session/journal/agent-session-journal-identity' -import type { - AgentJournalRenderItem, - AgentJournalSubmission -} from '../../../../../shared/agent-session/journal/agent-session-journal-types' -import type { StructuredAgentSessionChildExitSession } from '../structured-agent-session-child-exit' -import type { - JournalLifecycleBatchInput, - JournalPlannedLifecycleBatchInput -} from '../../../agent-session-journal/journal-store-contracts' - -const NO_CURSOR: AgentJournalCursor = { epoch: 'epoch-1', sequence: 0 } - -/** Empty plans must not call append, matching the real writer. */ -export function plannedBatchThrough( - append: (input: JournalLifecycleBatchInput) => Promise -): (input: JournalPlannedLifecycleBatchInput) => Promise { - return async (input) => { - const { mutations, dispatches } = input.plan() - if (mutations.length === 0 && dispatches.length === 0) { - return NO_CURSOR - } - const cursor = await append({ - settlementId: input.settlementId, - fence: input.fence, - ...(input.recovered ? { recovered: input.recovered } : {}), - mutations, - dispatches - }) - return cursor - } -} - -export function exitJournalDouble(fields: { - items: () => readonly AgentJournalRenderItem[] - appendLifecycleBatch: (input: JournalLifecycleBatchInput) => Promise - itemFence?: (itemId: string) => number | undefined - submissions?: () => readonly Partial[] -}): StructuredAgentSessionChildExitSession['journal'] { - const appendPlanned = plannedBatchThrough(fields.appendLifecycleBatch) - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a settlement reads only these members; the rest of the journal is unreachable from it. - const journal = { - cursor: () => NO_CURSOR, - itemBody: () => null, - itemFence: fields.itemFence ?? (() => undefined), - snapshot: () => ({ items: fields.items() }), - statusRead: () => ({ items: fields.items() }), - submissions: fields.submissions ?? (() => []), - inputOrder: { recoverableSubmissions: fields.submissions ?? (() => []) }, - stopMarks: { latest: () => null, personStopDecides: () => false } - } as unknown as StructuredAgentSessionChildExitSession['journal'] - journal.settleStaleState = (input) => settleJournalStaleState(journal, { appendPlanned }, input) - journal.settleDeadGeneration = (input) => - settleJournalDeadGeneration(journal, { appendPlanned }, input) - return journal -} diff --git a/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-stale-verdict-contract.test-fixture.ts b/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-stale-verdict-contract.test-fixture.ts index dbd727a0311..fb07e332d2d 100644 --- a/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-stale-verdict-contract.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-stale-verdict-contract.test-fixture.ts @@ -1,12 +1,8 @@ -import { settleJournalStaleState } from '../../../agent-session-journal/writing/journal-generation-settlement' -import { vi } from 'vitest' import { agentJournalItemKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' import type { AgentJournalItemIdentity } from '../../../../../shared/agent-session/journal/agent-session-journal-identity' import type { AgentJournalRenderItem } from '../../../../../shared/agent-session/journal/agent-session-journal-types' import type { AgentJournalTurnScope } from '../../../../../shared/agent-session/journal/agent-session-journal-turn-scope' import type { AgentSessionDeathEvidence } from '../../../../../shared/agent-session/lifecycle/agent-session-death-evidence' -import type { AgentSessionJournal } from '../../../agent-session-journal/journal-store' -import { plannedBatchThrough } from './structured-agent-session-settlement-double.test-fixture' import { turnVerdictFromDeathEvidence } from '../structured-agent-session-stale-turn-verdict' export const THREAD = 'thread-1' @@ -89,22 +85,3 @@ export const PROVEN: AgentSessionDeathEvidence = { ownerFence: 1, lastProvenAliveAt: 100 } - -export function journalWith(items: AgentJournalRenderItem[]) { - const appendLifecycleBatch = vi.fn(async () => ({ epoch: 'epoch-1', sequence: 9 })) - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the settle reads only these journal members. - const journal = { - snapshot: () => ({ items }), - statusRead: () => ({ items }), - itemFence: () => 1, - lastProviderActivityAt: () => undefined, - stopMarks: { latest: () => null }, - cursor: () => ({ epoch: 'epoch-1', sequence: 8 }), - submissions: () => [], - inputOrder: { recoverableSubmissions: () => [] }, - appendPlannedLifecycleBatch: plannedBatchThrough(appendLifecycleBatch) - } as unknown as AgentSessionJournal - journal.settleStaleState = (input) => - settleJournalStaleState(journal, { appendPlanned: journal.appendPlannedLifecycleBatch }, input) - return { journal, appendLifecycleBatch } -} diff --git a/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-stale-verdict-persistence.test.ts b/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-stale-verdict-persistence.test.ts index 204b3951533..688bc9adc15 100644 --- a/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-stale-verdict-persistence.test.ts +++ b/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-stale-verdict-persistence.test.ts @@ -1,12 +1,11 @@ +import { readTestJournalSnapshot } from '../../../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 { expect, it } from 'vitest' import { readAgentJournalTurn } from '../../../../../shared/agent-session/lifecycle/agent-session-turn-record' -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 { withTestAgentSessionDatabase } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import '../structured-agent-session-dead-generation-settlement' import { settleStaleStructuredAgentSessionState } from '../structured-agent-session-stale-state-settlement' import { codexProviderHandle } from '../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' @@ -18,7 +17,7 @@ import { turnScopeOf } from './structured-agent-session-stale-verdict-contract.test-fixture' -it('adds one row for a death however many attempts its settle takes to write', async () => { +it('rolls back failed death settlement and saves one notice when retried', async () => { const root = await mkdtemp(join(tmpdir(), 'orca-stale-session-')) const journals = createTrackedJournalOpener({ legacyInputs: true }) let now = 100 @@ -51,26 +50,31 @@ it('adds one row for a death however many attempts its settle takes to write', a acquisitionGeneration: null, deathEvidence: PROVEN }) - const turns = () => - journal.snapshot().items.flatMap((item) => readAgentJournalTurn(item.body) ?? []) - const statusRows = () => journal.snapshot().items.filter((item) => item.body.kind === 'status') - const database = openTestJournalHostDatabase(root) + const turns = async () => + (await readTestJournalSnapshot(journal)).items.flatMap( + (item) => readAgentJournalTurn(item.body) ?? [] + ) + const statusRows = async () => + (await readTestJournalSnapshot(journal)).items.filter((item) => item.body.kind === 'status') const cursor = journal.cursor() - database.db.exec(`CREATE TEMP TRIGGER reject_later_stale_settlement - BEFORE INSERT ON journal_rows - WHEN NEW.session_id = 'session-1' AND NEW.seq > ${cursor.sequence + 1} - AND json_extract(NEW.row_json, '$.recovered') = 1 + await withTestAgentSessionDatabase(root, (db) => + db.exec(` + CREATE TRIGGER reject_later_stale_settlement BEFORE UPDATE OF payload_json ON journal_items + WHEN NEW.session_id = 'session-1' AND NEW.provider_turn_id = 'turn-398' BEGIN SELECT RAISE(ABORT, 'disk full'); END`) + ) await expect(settle()).rejects.toThrow('disk full') - database.db.exec('DROP TRIGGER reject_later_stale_settlement') + await withTestAgentSessionDatabase(root, (db) => + db.exec('DROP TRIGGER reject_later_stale_settlement') + ) expect(journal.cursor()).toEqual(cursor) - expect(turns().filter((turn) => turn.state === 'running')).toHaveLength(399) - expect(statusRows()).toHaveLength(0) + expect((await turns()).filter((turn) => turn.state === 'running')).toHaveLength(399) + expect(await statusRows()).toHaveLength(0) await settle() - expect(turns().filter((turn) => turn.state !== 'interrupted')).toEqual([]) - expect(statusRows()).toHaveLength(1) + expect((await turns()).filter((turn) => turn.state !== 'interrupted')).toEqual([]) + expect(await statusRows()).toHaveLength(1) } finally { await journals.closeAll() await rm(root, { recursive: true, force: true }) diff --git a/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-stale-verdict-prompts.test.ts b/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-stale-verdict-prompts.test.ts index 032dd1242c1..bbb30fda027 100644 --- a/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-stale-verdict-prompts.test.ts +++ b/src/main/native-chat/agent-session-wire/termination/testing/structured-agent-session-stale-verdict-prompts.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalSnapshot } from '../../../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' @@ -52,8 +53,11 @@ it('ends a crashed turn at its last proof of life, not at a send accepted before }) now = 3_600_000 await settle(null) - const turn = () => journal.snapshot().items.map((item) => readAgentJournalTurn(item.body))[0] - expect(turn()).toMatchObject({ state: 'unverifiable' }) + const turn = async () => + (await readTestJournalSnapshot(journal)).items.map((item) => + readAgentJournalTurn(item.body) + )[0] + expect(await turn()).toMatchObject({ state: 'unverifiable' }) now = 3_605_000 await journal.appendSubmission({ clientMessageId: 'send-1', @@ -78,7 +82,7 @@ it('ends a crashed turn at its last proof of life, not at a send accepted before lastProvenAliveAt: 210 }) - expect(turn()).toMatchObject({ state: 'interrupted', completedAt: 210 }) + expect(await turn()).toMatchObject({ state: 'interrupted', completedAt: 210 }) } finally { await journals.closeAll() await rm(root, { recursive: true, force: true }) diff --git a/src/main/native-chat/chat-launcher-command.test.ts b/src/main/native-chat/chat-launcher-command.test.ts index ba170cb45ef..a8254071f6a 100644 --- a/src/main/native-chat/chat-launcher-command.test.ts +++ b/src/main/native-chat/chat-launcher-command.test.ts @@ -71,8 +71,11 @@ describe('chat launcher argv', () => { probeVersion: readVersion })(sessionIdentity) : createAcpStructuredLaunchResolver(spec, { + savedSession: { + hasProviderTurn: async () => false, + savedItemIds: async () => new Set() + }, ...common, - readJournal: () => null, resolveCommand: () => process.execPath, probeVersion: readVersion }) diff --git a/src/main/runtime/agent-session-saved-roster.test-support.ts b/src/main/runtime/agent-session-saved-roster.test-support.ts deleted file mode 100644 index e70adedc5ad..00000000000 --- a/src/main/runtime/agent-session-saved-roster.test-support.ts +++ /dev/null @@ -1,108 +0,0 @@ -import { vi, type Mock, type MockInstance } from 'vitest' -import { AgentHookServer } from '../agent-hooks/server' -import { agentHookRuntimeDeps } from '../agent-hooks/agent-hook-runtime-deps' -import { AgentStatusObservedPaneIdentities } from './agent-status-observed-pane-identity' -import { StructuredAgentSessionHost } from '../native-chat/agent-session-wire/host/structured-agent-session-host' -import { hostTestState } from '../native-chat/agent-session-wire/host/testing/structured-agent-session-host.test-fixture' -import { scaleStore } from './agent-session-scale.test-fixture' -import { makeStructuredAgentStatusSubject } from '../../shared/agent-status/contracts/agent-status-subject' -import type { AgentSessionStatusSummary } from '../../shared/agent-session/agent-session-wire' -import type { AgentStatusStore } from '../../shared/agent-status/store/agent-status-store' - -class CountedServer extends AgentHookServer { - builds = 0 - storeForContract() { - return this.canonicalStatusStore - } - listingForContract() { - return this.canonicalListingOrder - } - subjectsForContract() { - return this.canonicalSubjectsByPane - } - protected override buildStatusChangeNotification() { - this.builds++ - return super.buildStatusChangeNotification() - } -} - -export async function savedRosterRig(n: number) { - const rig = await scaleStore(n) - const ids = rig.rows.slice(0, n).map((row) => row.sessionId) - for (const id of ids) { - await rig.store.transitionHandoff(id, (record) => ({ - ...record, - lastTurn: { state: 'settled', outcome: 'success', at: 100 }, - lease: { - ...record.lease, - claimStatus: 'released', - ownerProcess: null, - reservedSpawnToken: null, - handoffStage: null, - unreconciled: false - } - })) - } - const server = new CountedServer() - const sink = agentHookRuntimeDeps( - server, - new AgentStatusObservedPaneIdentities(), - () => ({}) - ).structuredAgentStatusSink - const host = new StructuredAgentSessionHost({ - ...hostTestState().host.deps, - store: rig.store, - recoveryCapsule: undefined, - statusSink: sink - }) - const location = rig.rows[0]!.location - function live(id = 'live-session', status: AgentSessionStatusSummary['status'] = 'working') { - server.ingestStructuredStatus( - { - sessionId: id, - workspaceId: location.workspaceId, - agent: 'claude', - status, - latestPrompt: 'live input', - providerSession: { key: 'session_id', id: `provider-${id}` }, - statusStartedAt: 200, - updatedAt: 200 - }, - makeStructuredAgentStatusSubject(location, id) - ) - } - return { ...rig, ids, server, sink, host, live } -} - -type RosterWork = { - counts: { parentVisits: number; enumerations: number } - revision: MockInstance - mutation: MockInstance - snapshots: MockInstance - listing: MockInstance['set']> - subjects: MockInstance['set']> - rows: Mock[0]> - enriched: Mock[0]> -} - -export function countRosterWork(server: CountedServer): RosterWork { - const store = server.storeForContract() - const counts = { parentVisits: 0, enumerations: 0 } - const parents = store.getParents - vi.spyOn(store, 'getParents').mockImplementation(() => { - const rows = parents() - counts.enumerations++ - counts.parentVisits += rows.length - return rows - }) - return { - counts, - revision: vi.spyOn(store, 'getRevision'), - mutation: vi.spyOn(store, 'applyMutation'), - snapshots: vi.spyOn(store, 'getSnapshot'), - listing: vi.spyOn(server.listingForContract(), 'set'), - subjects: vi.spyOn(server.subjectsForContract(), 'set'), - rows: vi.fn[0]>(), - enriched: vi.fn[0]>() - } -} diff --git a/src/main/runtime/agent-session-status-write-work.test-support.ts b/src/main/runtime/agent-session-status-write-work.test-support.ts deleted file mode 100644 index 7710819e3a4..00000000000 --- a/src/main/runtime/agent-session-status-write-work.test-support.ts +++ /dev/null @@ -1,139 +0,0 @@ -import { vi } from 'vitest' -import * as drafts from './agent-session/records/agent-session-store-draft' -import * as attribution from './agent-session/agent-session-runtime-attribution' -import { AgentSessionTabTable } from './agent-session/agent-session-tab-table' -import type { AgentSessionStoreState } from './agent-session/records/agent-session-store-state' - -/** Transparent counters on actual iterator yields, including the two death-listener passes. */ -export function countStatusWriteWork() { - const counts = { - drafts: 0, - recordCopies: 0, - operationCopies: 0, - recordDiffs: 0, - operationDiffs: 0, - attribution: 0, - deathEvidence: 0, - tabCopies: 0, - tabComparisons: 0, - retiredCopies: 0, - retiredComparisons: 0 - } - let phase: 'copy' | 'diff' | 'attribute' | 'death' = 'death' - const observed = new WeakSet() - function watch(map: Map, records: boolean) { - if (observed.has(map)) { - return - } - observed.add(map) - const iterate = map[Symbol.iterator].bind(map) - vi.spyOn(map, Symbol.iterator).mockImplementation(function* () { - for (const entry of iterate()) { - if (phase === 'copy') { - counts[records ? 'recordCopies' : 'operationCopies']++ - } else if (phase === 'diff') { - counts[records ? 'recordDiffs' : 'operationDiffs']++ - } else if (phase === 'attribute') { - counts.attribution++ - } else { - counts.deathEvidence++ - } - yield entry - } - return undefined - }) - } - function watchRetired(state: AgentSessionStoreState) { - if (observed.has(state.retiredClaimKeys)) { - return - } - observed.add(state.retiredClaimKeys) - const iterate = state.retiredClaimKeys[Symbol.iterator].bind(state.retiredClaimKeys) - vi.spyOn(state.retiredClaimKeys, Symbol.iterator).mockImplementation(function* () { - for (const entry of iterate()) { - counts.retiredCopies++ - yield entry - } - return undefined - }) - const some = state.retiredClaimKeys.some.bind(state.retiredClaimKeys) - vi.spyOn(state.retiredClaimKeys, 'some').mockImplementation((predicate, thisArg) => - some((entry, i, rows) => { - counts.retiredComparisons++ - return predicate.call(thisArg, entry, i, rows) - }) - ) - } - const cloneTabs = AgentSessionTabTable.prototype.clone - vi.spyOn(AgentSessionTabTable.prototype, 'clone').mockImplementation(function ( - this: AgentSessionTabTable - ) { - // Each constructor put copies one entry into both inverse indexes. - const result = cloneTabs.call(this) - counts.tabCopies += result.entries().length - return result - }) - const equalTabs = AgentSessionTabTable.prototype.equals - vi.spyOn(AgentSessionTabTable.prototype, 'equals').mockImplementation(function ( - this: AgentSessionTabTable, - other - ) { - const entries = this.entries - const spy = vi.spyOn(this, 'entries').mockImplementation(() => { - const rows = entries.call(this) - counts.tabComparisons += rows.length - return rows - }) - const otherEntries = other.entries - const otherSpy = vi.spyOn(other, 'entries').mockImplementation(() => { - const rows = otherEntries.call(other) - counts.tabComparisons += rows.length - return rows - }) - try { - return equalTabs.call(this, other) - } finally { - spy.mockRestore() - otherSpy.mockRestore() - } - }) - const draft = drafts.draftAgentSessionStoreState - vi.spyOn(drafts, 'draftAgentSessionStoreState').mockImplementation((published) => { - counts.drafts++ - watch(published.records, true) - watch(published.operations, false) - watchRetired(published) - phase = 'copy' - const next = draft(published) - phase = 'death' - watch(next.records, true) - watch(next.operations, false) - return next - }) - const attribute = attribution.attributeAgentSessionRuntime - vi.spyOn(attribution, 'attributeAgentSessionRuntime').mockImplementation((...args) => { - phase = 'attribute' - try { - return attribute(...args) - } finally { - phase = 'death' - } - }) - const writes = drafts.agentSessionStoreDraftRowWrites - vi.spyOn(drafts, 'agentSessionStoreDraftRowWrites').mockImplementation((...args) => { - phase = 'diff' - try { - return writes(...args) - } finally { - phase = 'death' - } - }) - return { - counts, - reset: () => { - for (const key of Object.keys(counts)) { - Reflect.set(counts, key, 0) - } - } - } -} diff --git a/src/main/runtime/agent-session/agent-session-conversation-clear-commit.test.ts b/src/main/runtime/agent-session/agent-session-conversation-clear-commit.test.ts index 4ea8105d61f..50463e097dc 100644 --- a/src/main/runtime/agent-session/agent-session-conversation-clear-commit.test.ts +++ b/src/main/runtime/agent-session/agent-session-conversation-clear-commit.test.ts @@ -3,10 +3,10 @@ import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, beforeEach, describe, expect, it } from 'vitest' import type { AgentSessionOwnerProbe } from '../../../shared/agent-session/lifecycle/agent-session-lease-adjudication' -import { AgentSessionJournal } from '../../native-chat/agent-session-journal/journal-store' +import type { AgentSessionJournal } from '../../native-chat/agent-session-journal/journal-store' import { closeTestJournalHostDatabases, - openTestJournalHostDatabase + createTrackedJournalOpener } from '../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import { providerContextBoundaryForClear, @@ -22,12 +22,14 @@ const SOURCE = 'session-alpha' const INDETERMINATE: AgentSessionOwnerProbe = { outcome: 'indeterminate', reason: 'no answer' } let directory: string +const journals = createTrackedJournalOpener() beforeEach(async () => { directory = await mkdtemp(join(tmpdir(), 'orca-agent-session-clear-commit-')) }) afterEach(async () => { + await journals.closeAll() await closeTestJournalHostDatabases() await rm(directory, { recursive: true, force: true }) }) @@ -109,8 +111,8 @@ async function prepare(released = true) { now: NOW + 1 }) } - const journal = new AgentSessionJournal({ - database: openTestJournalHostDatabase(directory), + const journal = await journals.open({ + stateDirectory: directory, identity: { sessionId: SOURCE, workspaceId: 'workspace-1', @@ -120,7 +122,6 @@ async function prepare(released = true) { }, now: () => NOW + 5 }) - await journal.open() const completed = clear(store.getRecord(SOURCE)!.lease.runtimeFence) const operation = { callerKey: completed.command.callerKey, @@ -128,9 +129,9 @@ async function prepare(released = true) { } await store.admitOperation({ ...operation, fingerprint: 'clear', now: NOW }) const append = (input = completed) => - journal.context.clear( - providerContextBoundaryForClear(input), - store.conversationReceipts.clear(() => input, operation) + journal.write( + { kind: 'context-clear', boundary: providerContextBoundaryForClear(input) }, + () => ({ receipt: store.conversationReceipts.clear(() => input, operation) }) ) return { store, journal, completed, operation, append } } diff --git a/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe-omp.test.ts b/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe-omp.test.ts index 72d606ae149..9131d4432cd 100644 --- a/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe-omp.test.ts +++ b/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe-omp.test.ts @@ -42,6 +42,7 @@ const workspace = async () => root function launchWith(settings: Settings) { return createAcpStructuredLaunchResolver(acpLaunchSpecFor('omp')!, { + savedSession: { hasProviderTurn: async () => false, savedItemIds: async () => new Set() }, store: { getRecord: () => ({ ...agentSessionRecordFixture(), @@ -51,7 +52,6 @@ function launchWith(settings: Settings) { }), pinLaunchDirectory: vi.fn() }, - readJournal: () => null, resolveWorkspacePath: async () => root, resolveEnvironment: async () => loginShell.env, resolveLaunchEnv: () => settings.agentDefaultEnv?.omp ?? {}, diff --git a/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe.test.ts b/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe.test.ts index 41247bdcd78..8442e80d45c 100644 --- a/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe.test.ts +++ b/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe.test.ts @@ -42,6 +42,7 @@ const workspace = async () => root function launchWith(settings: Settings) { return createAcpStructuredLaunchResolver(acpLaunchSpecFor('opencode')!, { + savedSession: { hasProviderTurn: async () => false, savedItemIds: async () => new Set() }, store: { getRecord: () => ({ ...agentSessionRecordFixture(), @@ -51,7 +52,6 @@ function launchWith(settings: Settings) { }), pinLaunchDirectory: vi.fn() }, - readJournal: () => null, resolveWorkspacePath: async () => root, resolveEnvironment: async () => loginShell.env, resolveLaunchEnv: () => settings.agentDefaultEnv?.opencode ?? {}, diff --git a/src/main/runtime/agent-session/lifecycle/structured-agent-session-tab-authority.test-support.ts b/src/main/runtime/agent-session/lifecycle/structured-agent-session-tab-authority.test-support.ts index 74cc0f33008..c32c8225358 100644 --- a/src/main/runtime/agent-session/lifecycle/structured-agent-session-tab-authority.test-support.ts +++ b/src/main/runtime/agent-session/lifecycle/structured-agent-session-tab-authority.test-support.ts @@ -197,23 +197,6 @@ export async function holdStartup(rig: AuthorityRig) { await chatTabCommandsSettled() } } -export function rejectVisibility(rig: AuthorityRig) { - const db = rig.host.deps.journalDatabase.db - db.exec( - "CREATE TEMP TRIGGER reject_initial_tabs BEFORE INSERT ON agent_session_store_meta WHEN NEW.key='session_tabs_recorded' BEGIN SELECT RAISE(ABORT, 'visibility rejected'); END" - ) - db.exec( - "CREATE TEMP TRIGGER reject_tab_delta BEFORE DELETE ON agent_session_tabs BEGIN SELECT RAISE(ABORT, 'visibility rejected'); END" - ) - db.exec( - "CREATE TEMP TRIGGER reject_tab_insert BEFORE INSERT ON agent_session_tabs BEGIN SELECT RAISE(ABORT, 'visibility rejected'); END" - ) - return () => { - db.exec('DROP TRIGGER reject_tab_insert') - db.exec('DROP TRIGGER reject_initial_tabs') - db.exec('DROP TRIGGER reject_tab_delta') - } -} export async function createC( rig: AuthorityRig, firstMessage: boolean, diff --git a/src/main/runtime/agent-session/records/agent-session-store-transactions.test.ts b/src/main/runtime/agent-session/records/agent-session-store-transactions.test.ts index 530656fa34b..da5c8b20a57 100644 --- a/src/main/runtime/agent-session/records/agent-session-store-transactions.test.ts +++ b/src/main/runtime/agent-session/records/agent-session-store-transactions.test.ts @@ -96,10 +96,10 @@ describe('a receipt', () => { args: Parameters[0], cut = cancel ) { - return composeJournalOperationReceipts( - store.operationOutcomeReceipt(args), - store.usageLimitContinueCancelReceipt('chat-a-0001', cut) - ) + const cancelReceipt = store.usageLimitContinueCancelReceipt('chat-a-0001', cut) + return cancelReceipt + ? composeJournalOperationReceipts(store.operationOutcomeReceipt(args), cancelReceipt) + : store.operationOutcomeReceipt(args) } async function pendingOperation(insert = false): Promise { const store = await openTestAgentSessionRecordStore(root) diff --git a/src/main/runtime/agent-session/structured-agent-session-mobile-retirement.test.ts b/src/main/runtime/agent-session/structured-agent-session-mobile-retirement.test.ts index f64bde161a8..209e2e25312 100644 --- a/src/main/runtime/agent-session/structured-agent-session-mobile-retirement.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-mobile-retirement.test.ts @@ -113,7 +113,7 @@ describe('host prompt delivery and retirement', () => { } ] ]), - readStatusState: () => projectStructuredAgentSessionStatusState(items), + readStatusState: async () => projectStructuredAgentSessionStatusState(items), readChildWork: () => undefined, now: () => 42 }) diff --git a/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-dispatch-worker-lifecycle.test.ts b/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-dispatch-worker-lifecycle.test.ts index 4a3b39b6182..1157100c3b4 100644 --- a/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-dispatch-worker-lifecycle.test.ts +++ b/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-dispatch-worker-lifecycle.test.ts @@ -10,7 +10,6 @@ import { formatOrcaSessionAddress } from '../../../../../shared/orca-session-add import { testOrcaSessionId } from '../../../../../shared/orca-session-address-test-fixture' import { idOf } from '../../../rpc/orchestration-session-caller-test-fixture' import { closeStructuredAgentSessionChild } from '../../../agent-session/structured-agent-session-close' -import { restoreJournal } from '../../../../native-chat/agent-session-journal/replay/journal-open' import { OrcaRuntimeRpcServer } from '../../../rpc/server/runtime-rpc' import { ORCHESTRATION_METHODS } from '../../../rpc/methods/orchestration/orchestration' import type { FakeConnection } from './structured-chat-coordinator-fake-codex-fixture' @@ -301,8 +300,8 @@ describe("a chat assignee's liveness, stop and close", () => { const prefix = journal.cursor() await host.close(PEER_CHAT, 'evict') expect(host.hasSession(PEER_CHAT)).toBe(false) - const saved = restoreJournal(host.deps.journalDatabase.db, PEER_CHAT)?.state - expect(saved?.lastSequence).toBeGreaterThanOrEqual(prefix.sequence) + const saved = await journal.reads.cursor() + expect(saved.sequence).toBeGreaterThanOrEqual(prefix.sequence) const acquisitions = codex.connections.length const server = new OrcaRuntimeRpcServer({ runtime, diff --git a/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-dispatch-worker.test.ts b/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-dispatch-worker.test.ts index 022038a3673..7e7efd2d488 100644 --- a/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-dispatch-worker.test.ts +++ b/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-dispatch-worker.test.ts @@ -148,7 +148,7 @@ describe('a chat as the assignee of orchestration dispatch', () => { orchestration: { message: 'task', runId, taskId, dispatchId } } // On the card's body: the turn the queue sends carries it, and the provider never sees it. - expect(queuedRows(PEER_CHAT).map((card) => card.body.from)).toMatchObject([from]) + expect((await queuedRows(PEER_CHAT)).map((card) => card.body.from)).toMatchObject([from]) await endTurn() await vi.waitFor(() => expect(worker.turns).toHaveLength(2), WAIT) diff --git a/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-mail-custody.test.ts b/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-mail-custody.test.ts index dea3f1667f7..331039d9e47 100644 --- a/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-mail-custody.test.ts +++ b/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-mail-custody.test.ts @@ -11,6 +11,7 @@ import type { AgentSessionJournal } from '../../../../native-chat/agent-session- import type { AgentMessageSource } from '../../../../../shared/agent-session/agent-session-message-source' import { agentSessionRefusalNotice } from '../../../../../shared/agent-session/failures/agent-session-refusal-notice' import { agentSessionWriteKindForMethod } from '../../../../../shared/agent-session/failures/agent-session-write-failure' +import { captureHostStructuredAgentSessionWork } from '../../../../native-chat/agent-session-wire/observations/structured-agent-session-read-work' import { nextStructuredQueuedMessage } from '../../../../native-chat/agent-session-wire/delivery/structured-agent-session-queued-gate' import { idOf } from '../../../rpc/orchestration-session-caller-test-fixture' import { operationId } from './structured-chat-coordinator-fake-codex-fixture' @@ -19,13 +20,11 @@ import { PEER_CHAT, WAIT, call, - codex, coordinatorRunAndTask, db, finishWorker, host, isWholeMail, - observationClock, openChat, queuedCardTexts, runtime, @@ -264,7 +263,12 @@ describe('a queued mail card runs only while its chat still owes it', () => { // Idle with nothing to hand over: not working, so /clear and the person's sends go through. await vi.waitFor(() => expect(work.working()).toBe(false), WAIT) const record = host.deps.store.getRecord(COORDINATOR) - expect(nextStructuredQueuedMessage({ journal, record, work })).toBeNull() + const authority = captureHostStructuredAgentSessionWork( + { store: host.deps.store, sessions: host.collaboratorsForTests().sessions }, + COORDINATOR, + journal + ) + expect(await nextStructuredQueuedMessage({ journal, record, work, authority })).toBeNull() // The person's Send of that card says what happened, never that Orca restarted. const card = (await journal.queuedMessages.list()).find((row) => row.state === 'waiting') const fields = { messageId: card!.messageId } @@ -367,75 +371,4 @@ describe('a queued mail card runs only while its chat still owes it', () => { await vi.waitFor(() => expect(chat.turns).toHaveLength(2), WAIT) expect(isWholeMail(turnText(chat.turns[1]!))).toBe(true) }) - - it.each([4, 40])( - 'follows %i waiting report cards by key, never reading the chat’s history', - async (count) => { - await busyChat(COORDINATOR) - const { runId } = await coordinatorRunAndTask() - const journal = journalOf(COORDINATOR) - // A long conversation: reconciling must not cost more for it. - const fence = host.deps.store.getConversation(COORDINATOR)!.currentFence - for (let item = 0; item < 500; item += 1) { - await journal.appendItem( - { provider: 'orca', clientMessageId: `history-${item}` }, - { kind: 'status', text: `step ${item}` }, - { fence, turnScope: { kind: 'thread' } } - ) - } - // Each report admitted on its own, as workers report: identity is read from every saved - // conversation once, not once per admission. - const lineage = vi.spyOn(host.deps.store, 'listConversations') - for (let report = 0; report < count; report += 1) { - db.insertMessage({ - runId, - from: 'term_worker', - to: `run:${runId}`, - subject: `report ${report}` - }) - runtime.deliverPendingMessagesForHandle(`run:${runId}`) - await vi.waitFor(async () => expect(await queuedCardTexts()).toHaveLength(report + 1), WAIT) - } - expect(lineage.mock.calls.length).toBeLessThanOrEqual(1) - - const facts = vi.spyOn(host, 'sendAnswer') - const snapshots = vi.spyOn(host, 'journalSnapshot') - const histories = vi.spyOn(journal, 'submissions') - const sends = vi.spyOn(host, 'send') - runtime.onStructuredSessionStatusForMail({ sessionId: COORDINATOR, status: 'idle' }) - await vi.waitFor(async () => { - const seen = facts.mock.calls.length - await Promise.all(facts.mock.results.map((read) => read.value)) - await new Promise((resolve) => setImmediate(resolve)) - expect(seen).toBeGreaterThanOrEqual(count) - expect(facts.mock.calls.length).toBe(seen) - }, WAIT) - // One keyed read per card per pass, however long the history, and a pass or few per edge. - const reads = new Map() - for (const [, operation] of facts.mock.calls) { - reads.set(operation, (reads.get(operation) ?? 0) + 1) - } - const recorded = db.getStructuredMailSends(`run:${runId}`).map((row) => row.operation_id) - expect([...reads.keys()].sort()).toEqual([...recorded].sort()) - const passes = new Set(reads.values()) - expect(passes.size).toBe(1) - expect([...passes][0]).toBeLessThanOrEqual(3) - expect(snapshots).not.toHaveBeenCalled() - expect(histories).not.toHaveBeenCalled() - expect(sends).not.toHaveBeenCalled() - expect(db.getStructuredMailSends(`run:${runId}`)).toHaveLength(count) - expect(codex.connections.flatMap((connection) => connection.turns)).toHaveLength(1) - - // Publications that change nothing the mail waits on, as streamed tokens do, start no pass - // (once the redrive the admissions themselves scheduled has run). - await observationClock.observe(2_500) - facts.mockClear() - for (let token = 0; token < 5; token += 1) { - host.wakeQueuedMessages(COORDINATOR) - await observationClock.observe(400) - } - expect(facts).not.toHaveBeenCalled() - }, - 30_000 - ) }) diff --git a/src/main/runtime/orchestration/workers/turn-stops/chat-worker-request-failures.test.ts b/src/main/runtime/orchestration/workers/turn-stops/chat-worker-request-failures.test.ts index afd327a7977..5f5765e84fb 100644 --- a/src/main/runtime/orchestration/workers/turn-stops/chat-worker-request-failures.test.ts +++ b/src/main/runtime/orchestration/workers/turn-stops/chat-worker-request-failures.test.ts @@ -1,4 +1,3 @@ -import { codexProviderHandle } from '../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import { SESSION, tick, @@ -157,7 +156,7 @@ it.each(['history', 'close', 'replacement', 'epoch'] as const)( await r.journal.write({ kind: 'epoch-replace', reason: 'handle_forked', - providerHandle: codexProviderHandle('handle_forked'), + providerHandle: { kind: 'opaque', agent: 'codex', value: 'handle_forked' }, fence: 0, items: [] }) @@ -192,7 +191,7 @@ it('consumes unassigned failures and baselines adoption, epoch replacement and c await r.journal.write({ kind: 'epoch-replace', reason: 'handle_forked', - providerHandle: codexProviderHandle('handle_forked'), + providerHandle: { kind: 'opaque', agent: 'codex', value: 'handle_forked' }, fence: 0, items: [] }) diff --git a/src/main/runtime/rpc/methods/agent-launch/agent-launch-replay.test.ts b/src/main/runtime/rpc/methods/agent-launch/agent-launch-replay.test.ts index 2a44054eedf..4821942b4b2 100644 --- a/src/main/runtime/rpc/methods/agent-launch/agent-launch-replay.test.ts +++ b/src/main/runtime/rpc/methods/agent-launch/agent-launch-replay.test.ts @@ -206,13 +206,9 @@ describe('a fresh launch writes the ledger once before its effect', () => { fingerprint: 'fp-1', now: NOW } - const claimAfter = (decision: { decision: string; row?: AgentSessionOperationRow }) => - decision.decision === 'admit' || - (decision.decision === 'replay' && decision.row?.outcome.status === 'pending') - const results = await Promise.all([ - store.admitAndClaimOperation(admission, claimAfter), - store.admitAndClaimOperation(admission, claimAfter) + store.admitAndClaimOperation(admission), + store.admitAndClaimOperation(admission) ]) expect(results.filter((result) => result.claim?.claim === 'won')).toHaveLength(1) diff --git a/src/main/runtime/rpc/methods/orchestration/structured-workers/orchestration-structured-worker-redrive.test.ts b/src/main/runtime/rpc/methods/orchestration/structured-workers/orchestration-structured-worker-redrive.test.ts index fbfce18e4b7..2213c163a40 100644 --- a/src/main/runtime/rpc/methods/orchestration/structured-workers/orchestration-structured-worker-redrive.test.ts +++ b/src/main/runtime/rpc/methods/orchestration/structured-workers/orchestration-structured-worker-redrive.test.ts @@ -692,7 +692,12 @@ describe('the structured redrive publication edge', () => { const { identity, journal, fence, onJournalActivity } = await startWatchedWorker() const before = journal.cursor() - await journal.rollEpoch('unreconcilable_prefix', fence) + await journal.write({ + kind: 'epoch-replace', + reason: 'unreconcilable_prefix', + fence, + items: [] + }) vi.advanceTimersByTime(300) expect(journal.cursor().epoch).not.toBe(before.epoch) diff --git a/src/main/runtime/rpc/methods/orchestration/worker-start-card-population.test-fixture.ts b/src/main/runtime/rpc/methods/orchestration/worker-start-card-population.test-fixture.ts index f0c092766d5..812dfd3bf6f 100644 --- a/src/main/runtime/rpc/methods/orchestration/worker-start-card-population.test-fixture.ts +++ b/src/main/runtime/rpc/methods/orchestration/worker-start-card-population.test-fixture.ts @@ -1,3 +1,4 @@ +import { readTestJournalSnapshot } from '../../../../native-chat/agent-session-journal/journal-observation.test-support' import type { createOrchestrationRpcHarness } from './rpc-test-harness' import { expect } from 'vitest' import type { AgentJournalSnapshot } from '../../../../../shared/agent-session/journal/agent-session-journal-types' @@ -5,15 +6,17 @@ import type { AgentSessionJournal } from '../../../../native-chat/agent-session- import type { JournalHostDatabase } from '../../../../native-chat/agent-session-journal/database/journal-host-database' import { COORD } from './worker-start-card-contract-test-fixture' -export function expectWorkerStartPopulation(input: { +export async function expectWorkerStartPopulation(input: { journal: AgentSessionJournal count: number connection: JournalHostDatabase['db'] facts: { dispatchId: string; workerAddress: string; task: string; name: string }[] before: AgentJournalSnapshot -}): void { +}): Promise { const { journal, facts, count, connection, before } = input - const cards = journal.snapshot().items.filter(({ body }) => body.kind === 'agent-start') + const cards = (await readTestJournalSnapshot(journal)).items.filter( + ({ body }) => body.kind === 'agent-start' + ) expect(cards.map(({ body }) => body)).toEqual( facts.map((fact) => ({ kind: 'agent-start', diff --git a/src/main/runtime/rpc/methods/session-tabs/structured-chat-tab-table.test.ts b/src/main/runtime/rpc/methods/session-tabs/structured-chat-tab-table.test.ts index 4ca6e68346e..e8ddcc6eace 100644 --- a/src/main/runtime/rpc/methods/session-tabs/structured-chat-tab-table.test.ts +++ b/src/main/runtime/rpc/methods/session-tabs/structured-chat-tab-table.test.ts @@ -75,10 +75,15 @@ describe('a chat tab across /clear', () => { it('replays a lost answer with the same tab and one divider', async () => { await createChat(HOST_TEST_SESSION) const journal = host.collaboratorsForTests().sessions.get(HOST_TEST_SESSION)!.journal - const original = journal.context.clear.bind(journal.context) - const lost = vi.spyOn(journal.context, 'clear').mockImplementationOnce(async (...args) => { - await original(...args) - throw new Error('lost acknowledgement') + const original = journal.write.bind(journal) + let loseClear = true + const lost = vi.spyOn(journal, 'write').mockImplementation(async (operation, prepare) => { + const result = await original(operation, prepare) + if (operation.kind === 'context-clear' && loseClear) { + loseClear = false + throw new Error('lost acknowledgement') + } + return result }) const params = { command: 'clear', diff --git a/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-capability.test.ts b/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-capability.test.ts index 54e12c8e0c9..c11a81a7b41 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-capability.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-capability.test.ts @@ -54,7 +54,7 @@ it('keeps exact image controls and pause for older readers, on history and the s nextQueuedMessageId: 'image' } hostCalls.history.mockReturnValue({ ok: true, page: { items: [], ...queue } }) - hostCalls.subscribe.mockImplementation((input: AgentSessionSubscribeInput) => { + hostCalls.subscribe.mockImplementation(async (input: AgentSessionSubscribeInput) => { const event: AgentSessionSubscribeEvent = { type: 'batch', sessionId: SESSION, @@ -65,15 +65,17 @@ it('keeps exact image controls and pause for older readers, on history and the s submissions: [] } } - for (const frame of buildSubscriberFrame( - { readQueuePublication: () => queue }, - { - sessionId: SESSION, - boundedFrames: input.boundedFrames === true, - projectQueuePublication: input.projectQueuePublication - }, - event, - false + for (const frame of ( + await buildSubscriberFrame( + { readQueuePublication: () => queue }, + { + sessionId: SESSION, + boundedFrames: input.boundedFrames === true, + projectQueuePublication: input.projectQueuePublication + }, + event, + false + ) ).frames) { input.emit(frame) } @@ -140,7 +142,7 @@ it('compacts old image controls before bounding to retain the head and following nextQueuedMessageId: null } expect(boundedQueuePublication(queue).queuedMessages).not.toContain(text) - hostCalls.subscribe.mockImplementation((input: AgentSessionSubscribeInput) => { + hostCalls.subscribe.mockImplementation(async (input: AgentSessionSubscribeInput) => { const subscriber: SubscriberFieldState = { sessionId: SESSION, boundedFrames: input.boundedFrames === true, @@ -152,13 +154,15 @@ it('compacts old image controls before bounding to retain the head and following batch: { cursor: { epoch: 'a', sequence: 0 }, items: [], removedItemIds: [], submissions: [] } } const context = { readQueuePublication: () => queue } - const built = buildSubscriberFrame(context, subscriber, event, false) + const built = await buildSubscriberFrame(context, subscriber, event, false) for (const frame of built.frames) { input.emit(frame) } expect(built.queued).toBe(queue) subscriber.queuePublication = built.queued - expect(buildSubscriberFrame(context, subscriber, event, false).attachedQueued).toBe(false) + expect((await buildSubscriberFrame(context, subscriber, event, false)).attachedQueued).toBe( + false + ) return () => {} }) expect( diff --git a/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-lifecycle.test.ts b/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-lifecycle.test.ts index 459422a4d00..ea8d37d3149 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-lifecycle.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-lifecycle.test.ts @@ -1,15 +1,10 @@ import { readTestJournalItems } from '../../../../../native-chat/agent-session-journal/journal-observation.test-support' -import { journalInputSourceBody } from '../../../../../native-chat/agent-session-journal/input-order/journal-input-source' import { streamState, queuedImageTestBody } from '../protocol/structured-agent-session-wire-record.test-fixture' -import { - appendTestJournalQueueRow, - openTestJournalHostDatabase -} from '../../../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { seedTestJournalQueueCard } from '../../../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' -import { finishTestJournalHistoricalInputs } from '../../../../../native-chat/agent-session-journal/replay/journal-historical-inputs.test-support' import { reduceStructuredAgentSession, EMPTY_STRUCTURED_AGENT_SESSION @@ -300,7 +295,7 @@ it('keeps a hidden original pageable beyond1024 rows and preserves edited recove const { id, body, card } = await failedImage() const j = journal() const fence = rig.store.getRecord(SESSION)!.lease.runtimeFence - await j.rollEpoch('unreconcilable_prefix', fence) + await j.write({ kind: 'epoch-replace', reason: 'unreconcilable_prefix', fence, items: [] }) expect( (await readTestJournalItems(j)).find((item) => item.itemId === agentJournalSubmissionKey(id)) ).toBeUndefined() @@ -389,7 +384,7 @@ it('keeps a hidden original pageable beyond1024 rows and preserves edited recove body: { blocks: [{ type: 'text', text: 'changed' }, body.blocks[1]] }, options: { model: 'default' } }) - const pub = rig.host.collaboratorsForTests().subscribers.readQueuePublication(SESSION)! + const pub = (await rig.host.collaboratorsForTests().subscribers.readQueuePublication(SESSION))! const { projectQueuedImageCardPublication } = await import('./structured-agent-session-queued-image-capability') const oldCards = projectQueuedImageCardPublication(pub, OLD) @@ -694,27 +689,27 @@ it.each([ } const id = await sendImage(body) await eventually(() => expect(rig!.dispatch).toHaveBeenCalledTimes(1)) - await eventually(() => expect(journal().submission(id)?.handedOverAt).toBeDefined()) + await eventually(async () => + expect((await journal().reads.submission(id))?.handedOverAt).toBeDefined() + ) await rig.stop() await rig.host.flushStreamedEvents(SESSION) await rig.host.collaboratorsForTests().serialize(SESSION, async () => {}) expect(await journal().queuedMessages.list()).toEqual([]) if (edit) { - appendTestJournalQueueRow( - openTestJournalHostDatabase(rig.root).db, - journal(), - `recovery:${id}`, - { - op: 'queued', - body, - recovers: id, - position: 0, - queuedAt: journal().cursor(), - options: { model: 'default' }, - returned: { reason: 'prior-build uncertainty', rejection: { kind: 'unconfirmed' } } - } - ) - finishTestJournalHistoricalInputs(journal()) + await seedTestJournalQueueCard(rig.root, journal(), { + messageId: `recovery:${id}`, + state: 'waiting', + body, + recovers: id, + position: 0, + holdReason: 'delivery_unconfirmed', + returnedReason: null, + returnedRejection: null, + consumedAs: null, + queuedAt: journal().cursor(), + options: { model: 'default' } + }) } const card = await journal().queuedMessages.get(`recovery:${id}`) const original = (await journal().reads.submission(id))! @@ -752,12 +747,6 @@ it.each([ }) expect(await journal().queuedMessages.get(`recovery:${id}`)).toEqual(edited) } - const input = structuredClone(journal().queuedMessages.displayState().inputs.sources.get(id)) - expect(journalInputSourceBody(input)).toEqual(body) - expect(input?.kind === 'submission' ? input.options : input?.submission.options).toEqual( - original.options - ) - expect(input?.fence).toBe(original.fence) const beforeProof = await journal().queuedMessages.get(`recovery:${id}`) expect(await journal().reads.submission(id)).toEqual(original) expect(await journal().reads.receiptFor(id)).toEqual(receipt) @@ -783,7 +772,6 @@ it.each([ expect(acceptedReceipt).not.toEqual(receipt) await rig.settleAccepted(id, 'pinned-original-late-echo') expect(await journal().reads.receiptFor(id)).toEqual(acceptedReceipt) - expect(journal().queuedMessages.displayState().inputs.sources.get(id)).toEqual(input) expect(await journal().reads.submissionBody(id)).toEqual(body) expect(rig.dispatch).toHaveBeenCalledTimes(1) if (card) { diff --git a/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-wire-contract.test.ts b/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-wire-contract.test.ts index 593aeb17abd..d80ac281740 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-wire-contract.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-wire-contract.test.ts @@ -1,9 +1,5 @@ -import { - appendTestJournalQueueRow, - openTestJournalHostDatabase -} from '../../../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { seedTestJournalQueueCard } from '../../../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' -import { finishTestJournalHistoricalInputs } from '../../../../../native-chat/agent-session-journal/replay/journal-historical-inputs.test-support' import { mkdir, writeFile } from 'node:fs/promises' import { join } from 'node:path' import { afterEach, beforeEach, expect, it, vi } from 'vitest' @@ -272,16 +268,19 @@ it('keeps a saved physical hold independent while its own rejected retry blocks await rig.host.flushStreamedEvents(SESSION) const j = rig.host.collaboratorsForTests().sessions.get(SESSION)!.journal const saved = `recovery:${id}` - appendTestJournalQueueRow(openTestJournalHostDatabase(rig.root).db, j, saved, { - op: 'queued', + await seedTestJournalQueueCard(rig.root, j, { + messageId: saved, + state: 'waiting', + holdReason: 'delivery_unconfirmed', + returnedReason: null, + returnedRejection: null, + consumedAs: null, body, recovers: id, position: -1, queuedAt: j.cursor(), - options: { model: 'default' }, - returned: { reason: 'prior-build uncertainty', rejection: { kind: 'unconfirmed' } } + options: { model: 'default' } }) - finishTestJournalHistoricalInputs(j) async function published() { const rpc = await call('agentSession.history', { sessionId: SESSION, direction: 'tail' }, OLD) if (!rpc.ok) { diff --git a/src/main/runtime/rpc/methods/structured-sessions/catalog/structured-agent-session-continuation-context.test.ts b/src/main/runtime/rpc/methods/structured-sessions/catalog/structured-agent-session-continuation-context.test.ts index b8978dbdf4d..f292e13572d 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/catalog/structured-agent-session-continuation-context.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/catalog/structured-agent-session-continuation-context.test.ts @@ -115,17 +115,22 @@ it('hands off the reduced saved chat after clear and rewind, budgets only the st } ) await append('PRE_CLEAR_SECRET') - await journal.context.clear( - { operationId: 'pr-31782-clear', afterFence: fence, clearedAt: rig.clock.now }, - receipt + await journal.write( + { + kind: 'context-clear', + boundary: { operationId: 'pr-31782-clear', afterFence: fence, clearedAt: rig.clock.now } + }, + () => ({ receipt: receipt }) ) await append('EARLY_SAVED_MARKER') for (let index = 0; index < 205; index++) { await append(`saved ${index} ${'context '.repeat(60)}`) } - const floor = journal.cursor() await append('DISCARDED_REWIND_SECRET') - await journal.context.rewind(floor, fence, [], () => receipt) + await journal.appendTombstone( + { provider: 'codex', threadId: 'saved-thread', turnId: 'saved-turn', ordinal: ordinal - 1 }, + { fence } + ) const resolution = ( state: AgentJournalResolution['state'], selectedOptionId: string | null = null diff --git a/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest-context.test.ts b/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest-context.test.ts index a35adb0c941..06d0b5193f0 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest-context.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest-context.test.ts @@ -45,17 +45,26 @@ it('hands off the reduced saved chat after clear and rewind, budgets only the st } ) await append('PRE_CLEAR_SECRET') - await journal.context.clear( - { operationId: 'pr-31782-clear', afterFence: fence, clearedAt: fixture.rig.clock.now }, - receipt + await journal.write( + { + kind: 'context-clear', + boundary: { + operationId: 'pr-31782-clear', + afterFence: fence, + clearedAt: fixture.rig.clock.now + } + }, + () => ({ receipt: receipt }) ) await append('EARLY_SAVED_MARKER') for (let index = 0; index < 205; index++) { await append(`saved ${index} ${'context '.repeat(60)}`) } - const floor = journal.cursor() await append('DISCARDED_REWIND_SECRET') - await journal.context.rewind(floor, fence, [], () => receipt) + await journal.appendTombstone( + { provider: 'codex', threadId: 'saved-thread', turnId: 'saved-turn', ordinal: ordinal - 1 }, + { fence } + ) const resolution = ( state: AgentJournalResolution['state'], selectedOptionId: string | null = null diff --git a/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-rpc-status.test-fixture.ts b/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-rpc-status.test-fixture.ts index 898db2a890e..6476a2b1824 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-rpc-status.test-fixture.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-rpc-status.test-fixture.ts @@ -1,64 +1,55 @@ -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 { beforeEach, afterEach } from 'vitest' +import { codexProviderHandle } from '../../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' +import { createTrackedJournalOpener } from '../../../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { indexedStatusFeedSession } from '../../../../../native-chat/agent-session-wire/observations/testing/structured-agent-session-status-feed-test-session.test-fixture' import { StructuredAgentSessionStatusFeed } from '../../../../../native-chat/agent-session-wire/observations/structured-agent-session-status-feed' import { createStructuredAgentSessionLogger } from '../../../../../native-chat/agent-session-wire/host/structured-agent-session-logger' export const STATUS_SESSION = 'session-status' -export const STATUS_ITEMS: ReturnType['items'] = [ - { - itemId: 'user-1', - sequence: 1, - revision: 1, - observedAt: 1, - body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'write a poem' }] } - }, - { - itemId: 'turn-1', - sequence: 2, - revision: 1, - observedAt: 2, - body: { kind: 'status', text: 'Working', turnLifecycle: { turnId: 'turn-1', state: 'running' } } - } -] +const journals = createTrackedJournalOpener() +let root: string +let feed: StructuredAgentSessionStatusFeed -/** One indexed session over a journal that reads back fixed items; the projection is real. */ -export const statusFeed = () => - new StructuredAgentSessionStatusFeed({ +beforeEach(async () => { + root = await mkdtemp(join(tmpdir(), 'orca-rpc-status-')) + const journal = await journals.open({ + stateDirectory: root, + identity: { + sessionId: STATUS_SESSION, + workspaceId: 'workspace-1', + hostId: 'local', + agent: 'codex', + providerHandle: codexProviderHandle('native-status') + } + }) + const bodies: AgentJournalItemBody[] = [ + { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'write a poem' }] }, + { kind: 'status', text: 'Working', turnLifecycle: { turnId: 'turn-1', state: 'running' } } + ] + for (const [ordinal, body] of bodies.entries()) { + await journal.appendItem( + { provider: 'codex', threadId: 'native-status', turnId: 'turn-1', ordinal }, + body, + { fence: 0, turnScope: { kind: 'thread' } } + ) + } + feed = new StructuredAgentSessionStatusFeed({ logger: createStructuredAgentSessionLogger(), - sessions: new Map([ - [ - STATUS_SESSION, - { - // 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. - journal: { - cursor: () => ({ epoch: 'epoch-status', sequence: 2 }), - lastActivityAt: () => 2, - context: { floor: () => null }, - stopMarks: { latest: () => null, revision: () => 0 }, - snapshot: () => ({ items: STATUS_ITEMS, submissions: [] }), - statusRead: () => ({ items: STATUS_ITEMS, submissions: [] }), - inputOrder: { - inputCount: () => 0, - inputOrdinal: () => Infinity, - recoverableSubmissions: () => [] - }, - item: ((itemId) => - STATUS_ITEMS.find((item) => item.itemId === itemId) ?? - null) satisfies AgentSessionJournal['item'], - itemFence: () => undefined, - runningTurn: () => null - } as unknown as AgentSessionJournal, - params: { - location: { - executionHostId: 'local', - wslDistro: null, - workspaceId: 'workspace-1', - workspaceKind: 'git-worktree' as const - }, - provider: 'codex' as const - } - } - ] - ]), + sessions: new Map([[STATUS_SESSION, indexedStatusFeedSession({ journal })]]), getConversation: () => null, now: () => 1_000 }) + await feed.publish(STATUS_SESSION, journal, { replay: true }) +}) +afterEach(async () => { + await journals.closeAll() + if (root) { + await rm(root, { recursive: true, force: true }) + } +}) + +export const statusFeed = () => feed diff --git a/src/main/runtime/rpc/methods/structured-sessions/subscriptions/structured-agent-session-saved-status-rpc.test.ts b/src/main/runtime/rpc/methods/structured-sessions/subscriptions/structured-agent-session-saved-status-rpc.test.ts index d130635ed45..19e5c24ea08 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/subscriptions/structured-agent-session-saved-status-rpc.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/subscriptions/structured-agent-session-saved-status-rpc.test.ts @@ -24,10 +24,10 @@ it.each(['mobile', 'runtime'] as const)( await attach() const { host: previous, store } = hostTestState() await previous.flushAllStreamedEvents() - await store.transitionHandoff(SESSION, (record) => ({ - ...record, + await store.updateRecord(SESSION, { + kind: 'last-turn', lastTurn: { state: 'settled', outcome: 'success', at: 100 } - })) + }) await store.setSessionTabVisibility(SESSION, true) const host = new StructuredAgentSessionHost(previous.deps) setStructuredAgentSessionHost(host) diff --git a/src/main/runtime/runtime-agent-sender-chat-lineage.test.ts b/src/main/runtime/runtime-agent-sender-chat-lineage.test.ts index 9899fb48abd..f024c1b94a7 100644 --- a/src/main/runtime/runtime-agent-sender-chat-lineage.test.ts +++ b/src/main/runtime/runtime-agent-sender-chat-lineage.test.ts @@ -1,7 +1,5 @@ -import { afterEach, expect, it, vi } from 'vitest' -import { createRuntimeAgentProjections } from './construction/runtime-agent-projections' +import { expect, it } from 'vitest' import { - clearedInto, conversationRecordReader, type AgentSessionRecordReader } from './orchestration/addressing/session-lineage/structured-session-lineage' @@ -12,19 +10,8 @@ import { seedTestAgentSessionRecordStore } from './agent-session/records/agent-session-record-store-test-harness' import { readAgentSenderSessions } from './runtime-agent-sender-history' -import { - AGENT_SESSION_PREDECESSOR_IDS, - agentSessionPredecessorWorkForTest -} from './agent-session-record-lineage' -import { openTestJournalHostDatabase } from '../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import type { AgentSessionRecord } from '../../shared/agent-session/lifecycle/agent-session-record' -const unissued = (): never => { - throw new Error('a sender build issued a terminal handle') -} - -afterEach(() => vi.restoreAllMocks()) - /** A committed /clear: the durable forward pointer the reverse index is derived from. */ const cleared = ( record: AgentSessionRecord, @@ -43,144 +30,6 @@ const cleared = ( } }) -/** Six open chats, one behind a three-deep /clear chain, and `history` unrelated closed chats. */ -async function chats(history: number) { - const root = await scaleDirectory() - const open = ['opened-0', 'opened-1', 'opened-2', 'opened-3', 'opened-4', 'cleared-3'].map((id) => - scaleRecord(id) - ) - const chain = ['cleared-0', 'cleared-1', 'cleared-2'].map((id, i) => - cleared(scaleRecord(id), `cleared-${i + 1}`) - ) - const closed = Array.from({ length: history }, (_, i) => scaleRecord(`closed-chat-${i}`)) - await seedTestAgentSessionRecordStore(root, { - records: [...open, ...chain, ...closed, scaleRecord('opened-5')], - sessionTabs: open.map((row) => ({ tabId: `tab-${row.sessionId}`, sessionId: row.sessionId })) - }) - const store = await openTestAgentSessionRecordStore(root) - const inventories = vi.spyOn(store, 'listConversations') - const records = conversationRecordReader(store) - const { senders } = createRuntimeAgentProjections( - { - getExistingOrchestrationDb: () => null, - resolveOrchestrationWorkerServer: () => { - throw new Error('unpaired') - } - }, - { - getDb: () => null, - getLeaves: () => [], - getPtys: () => [], - paneOf: () => ({ tabId: null, paneKey: null, recordedAtGraphSequence: 0 }), - // Orchestration issues handles; sender rows only read the ones already issued. - issueLeafHandle: unissued, - issuePtyHandle: unissued, - readLeafHandle: () => null, - readPtyHandles: () => () => null, - makePaneKey: () => '', - getWorktreeId: () => null, - getHandleForPaneKey: () => null, - getPaneKey: () => null, - getDispatchAuthority: () => null, - getAgentStatusSnapshot: () => [], - getSessionRecords: () => records - } - ) - /** The chat rows a build publishes, as identity -> the session it now opens. */ - const rows = () => - Object.fromEntries( - senders - .build() - .associations.filter((row) => row.sessionId) - .map((row) => [row.identity.slice('orca_session_id:'.length), row.sessionId]) - ) - /** What every saved chat says, read the slow way: the full scan the index replaces. */ - const oracle = () => { - inventories.mockClear() - const { listClearedFrom: _index, ...listing }: AgentSessionRecordReader = records - const scanned = Object.fromEntries( - readAgentSenderSessions(listing).map((row) => [ - row.identity.slice('orca_session_id:'.length), - row.sessionId - ]) - ) - const rebuilt = new Map() - for (const record of store.listConversations()) { - const next = clearedInto(record) - if (next) { - rebuilt.set(next, [...(rebuilt.get(next) ?? []), record.sessionId].sort()) - } - } - const indexed = new Map( - store.listHeldSessionIds().flatMap((id) => { - const from = [...store.listClearedFrom(id)].sort() - return from.length ? [[id, from] as const] : [] - }) - ) - inventories.mockClear() - return { scanned, index: Object.fromEntries(indexed), rebuilt: Object.fromEntries(rebuilt) } - } - return { root, store, senders, rows, oracle, inventories } -} - -it.each([1_000, 10_000])( - 'reads only open chats and their /clear predecessors, at %i unrelated saved chats', - async (history) => { - const h = await chats(history) - const visits = () => h.senders.workForTest().chatRecords - const check = (expected: Record, readsAtMost: number) => { - const before = visits() - const seeks = agentSessionPredecessorWorkForTest() - expect(h.rows()).toEqual(expected) - // Open chats plus their predecessors, whatever the saved history holds: one indexed seek - // per visited chat, returning only rows that name it. - expect(visits() - before).toBeLessThanOrEqual(readsAtMost) - const work = agentSessionPredecessorWorkForTest() - expect(work.seeks - seeks.seeks).toBeLessThanOrEqual(readsAtMost) - expect(work.rows - seeks.rows).toBeLessThanOrEqual(readsAtMost) - expect(h.inventories).not.toHaveBeenCalled() - const { scanned, index, rebuilt } = h.oracle() - expect(scanned).toEqual(expected) - expect(index).toEqual(rebuilt) - } - const base = { - 'opened-0': 'opened-0', - 'opened-1': 'opened-1', - 'opened-2': 'opened-2', - 'opened-3': 'opened-3', - 'opened-4': 'opened-4', - 'cleared-0': 'cleared-3', - 'cleared-1': 'cleared-3', - 'cleared-2': 'cleared-3', - 'cleared-3': 'cleared-3' - } - check(base, 9) - await h.store.setSessionTabVisibility('opened-5', true) - check({ ...base, 'opened-5': 'opened-5' }, 10) - await h.store.setSessionTabVisibility('opened-5', false) - check(base, 9) - // A committed /clear continues opened-1 in opened-2: its cards follow, through the index. - await h.store.reviseSessionOptions((record) => cleared(record, 'opened-2'), { - sessionId: 'opened-1' - }) - const { 'opened-1': _cleared, ...rest } = base - check({ ...rest, 'opened-1': 'opened-2' }, 9) - // A move keeps the identity and changes only where the row says the chat runs. - await h.store.reviseSessionOptions( - (record) => ({ ...record, location: { ...record.location, workspaceId: 'moved' } }), - { sessionId: 'cleared-3' } - ) - expect(h.senders.build().associations.find((row) => row.sessionId === 'cleared-3')).toEqual( - expect.objectContaining({ worktreeId: 'moved' }) - ) - // A reload reads the same saved pointers. - const reopened = await openTestAgentSessionRecordStore(h.root) - const index = (store: typeof h.store) => - store.listHeldSessionIds().map((id) => [id, [...store.listClearedFrom(id)].sort()]) - expect(index(reopened)).toEqual(index(h.store)) - } -) - it('follows branching, unreadable and cyclic /clear predecessors as the full scan does, by index', async () => { const root = await scaleDirectory() await seedTestAgentSessionRecordStore(root, { @@ -202,12 +51,10 @@ it('follows branching, unreadable and cyclic /clear predecessors as the full sca const store = await openTestAgentSessionRecordStore(root) expect(store.isSessionUnreadable('unreadable-u')).toBe(true) const records = conversationRecordReader(store) - const { listClearedFrom: _index, ...listing }: AgentSessionRecordReader = records const rows = (reader: AgentSessionRecordReader) => readAgentSenderSessions(reader) .map((row) => `${row.identity.slice('orca_session_id:'.length)}>${row.sessionId}`) .sort() - expect(rows(records)).toEqual(rows(listing)) expect(rows(records)).toEqual([ 'cycle-x1>cycle-x1', 'session-a>session-c', @@ -215,9 +62,4 @@ it('follows branching, unreadable and cyclic /clear predecessors as the full sca 'session-c>session-c', 'unreadable-u>session-c' ]) - const plan = openTestJournalHostDatabase(root) - .db.prepare(`EXPLAIN QUERY PLAN ${AGENT_SESSION_PREDECESSOR_IDS}`) - .all('session-c') - .map((row) => String(row.detail)) - expect(plan.join('\n')).toContain('USING INDEX idx_agent_session_clear_successor') }) diff --git a/src/main/runtime/workspaces/process-listing/runtime-worktree-agent-rows-verdict.test.ts b/src/main/runtime/workspaces/process-listing/runtime-worktree-agent-rows-verdict.test.ts index e465c147530..3a7e9e1fe37 100644 --- a/src/main/runtime/workspaces/process-listing/runtime-worktree-agent-rows-verdict.test.ts +++ b/src/main/runtime/workspaces/process-listing/runtime-worktree-agent-rows-verdict.test.ts @@ -75,7 +75,7 @@ async function openJournal(): Promise { } /** Publish through the real host feed. */ -function publishedSummary(journal: AgentSessionJournal): AgentSessionStatusSummary { +async function publishedSummary(journal: AgentSessionJournal): Promise { const session = indexedStatusFeedSession({ journal }) const feed = new StructuredAgentSessionStatusFeed({ logger: createStructuredAgentSessionLogger(), @@ -83,6 +83,7 @@ function publishedSummary(journal: AgentSessionJournal): AgentSessionStatusSumma getConversation: () => agentSessionConversationView(agentSessionRecordFixture()), now: () => 1_000 }) + await feed.publish(SESSION, journal, { replay: true }) const events: AgentSessionStatusEvent[] = [] feed.subscribe({ id: 'list', emit: (event) => events.push(event) }) const snapshot = events.find((event) => event.type === 'snapshot') @@ -138,15 +139,16 @@ describe('a request that failed reads as failed through the feed, the ingest and fence: 1, handoverRecorded: true }) - await journal.rejectQueuedSubmissions( - 1, - agentSessionFailureWords(agentSessionFailureFact('notSignedIn'), { + await journal.write({ + kind: 'queued-reject', + fence: 1, + rejection: agentSessionFailureWords(agentSessionFailureFact('notSignedIn'), { surface: 'rejection', agentName: 'Claude' }) - ) + }) - const summary = publishedSummary(journal) + const summary = await publishedSummary(journal) expect(summary).toMatchObject({ status: 'idle', turnOutcome: 'failure', latestPrompt: 'hello' }) const { status, ps } = ingest(summary) expect(status).toMatchObject({ @@ -175,7 +177,7 @@ describe('a request that failed reads as failed through the feed, the ingest and { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } ) - const { status, ps } = ingest(publishedSummary(journal)) + const { status, ps } = ingest(await publishedSummary(journal)) expect(status).toMatchObject({ state: 'done', interrupted: true }) expect(ps).toMatchObject({ mainAgent: { state: 'done', outcome: 'cancellation' }, @@ -191,7 +193,7 @@ describe('a request that failed reads as failed through the feed, the ingest and { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } ) // The row folds the host's live child records, never the summary's task list. - const { status, ps } = ingest(publishedSummary(journal), [ + const { status, ps } = ingest(await publishedSummary(journal), [ { type: 'live', observedAt: 1_000, @@ -229,12 +231,15 @@ describe('a request that failed reads as failed through the feed, the ingest and fence: 1, handoverRecorded: true }) - await journal.rejectQueuedSubmissions( - 1, - agentSessionFailureWords(agentSessionFailureFact('cancelled'), { surface: 'rejection' }) - ) + await journal.write({ + kind: 'queued-reject', + fence: 1, + rejection: agentSessionFailureWords(agentSessionFailureFact('cancelled'), { + surface: 'rejection' + }) + }) - expect(publishedSummary(journal)).toMatchObject({ status: null }) - expect(ingest(publishedSummary(journal)).ps).toBeUndefined() + expect(await publishedSummary(journal)).toMatchObject({ status: null }) + expect(ingest(await publishedSummary(journal)).ps).toBeUndefined() }) }) diff --git a/src/shared/mobile-queued-edit-host.test-fixture-contract.ts b/src/shared/mobile-queued-edit-host.test-fixture-contract.ts index 89c127ceecf..fec019186dd 100644 --- a/src/shared/mobile-queued-edit-host.test-fixture-contract.ts +++ b/src/shared/mobile-queued-edit-host.test-fixture-contract.ts @@ -18,7 +18,7 @@ export type MobileQueuedEditHostFixture = { sessionId: string start: () => Promise insert: (input: Omit) => Promise - read: (messageId: string) => StoredQueuedMessage | null + read: (messageId: string) => Promise published: (messageId: string) => Promise request: (method: string, params: unknown) => Promise restart: () => Promise From e0b7afa46ae6d0929af3a376d0247ff150988e60 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 20:37:54 -0800 Subject: [PATCH 04/45] Add the 4a main-vs-candidate native chat comparison harness One entry (native-chat-4a-comparison.ts) seeds the 55k-history/310-chat profile once through the seed arm, clones it per arm at a fixed path, launches each built checkout in the background with an isolated home, and drives the same stub provider events through the host RPC surface both builds serve. Envelopes come from each arm's own source bundle. Rows cover send/queue, 1/8/16 MiB streams with a competing send, maintenance windows, open/reopen, history/detail, outline/fork, rewind, record operations, R3 retained-history admission and, reported separately, first-launch conversion (optional 20 GiB source). Output is one JSON and one markdown table keyed by both head SHAs. --- .../native-chat/native-chat-4a-arm.ts | 244 ++++++++++++++ .../native-chat/native-chat-4a-comparison.ts | 314 ++++++++++++++++++ .../native-chat/native-chat-4a-fixture.ts | 151 +++++++++ .../native-chat-4a-ops-lifecycle.ts | 313 +++++++++++++++++ .../native-chat/native-chat-4a-ops-queue.ts | 245 ++++++++++++++ .../native-chat/native-chat-4a-ops-reads.ts | 238 +++++++++++++ .../native-chat-4a-ops-retained.ts | 102 ++++++ .../native-chat/native-chat-4a-ops-stream.ts | 210 ++++++++++++ .../native-chat/native-chat-4a-probes.ts | 231 +++++++++++++ .../native-chat-4a-provider-stub.cjs | 310 +++++++++++++++++ .../native-chat-4a-provider-text.cjs | 78 +++++ .../native-chat/native-chat-4a-report.ts | 152 +++++++++ .../native-chat/native-chat-4a-run-context.ts | 66 ++++ .../native-chat/native-chat-4a-session.ts | 228 +++++++++++++ .../native-chat/native-chat-4a-workloads.ts | 216 ++++++++++++ 15 files changed, 3098 insertions(+) create mode 100644 config/scripts/performance/native-chat/native-chat-4a-arm.ts create mode 100644 config/scripts/performance/native-chat/native-chat-4a-comparison.ts create mode 100644 config/scripts/performance/native-chat/native-chat-4a-fixture.ts create mode 100644 config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts create mode 100644 config/scripts/performance/native-chat/native-chat-4a-ops-queue.ts create mode 100644 config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts create mode 100644 config/scripts/performance/native-chat/native-chat-4a-ops-retained.ts create mode 100644 config/scripts/performance/native-chat/native-chat-4a-ops-stream.ts create mode 100644 config/scripts/performance/native-chat/native-chat-4a-probes.ts create mode 100644 config/scripts/performance/native-chat/native-chat-4a-provider-stub.cjs create mode 100644 config/scripts/performance/native-chat/native-chat-4a-provider-text.cjs create mode 100644 config/scripts/performance/native-chat/native-chat-4a-report.ts create mode 100644 config/scripts/performance/native-chat/native-chat-4a-run-context.ts create mode 100644 config/scripts/performance/native-chat/native-chat-4a-session.ts create mode 100644 config/scripts/performance/native-chat/native-chat-4a-workloads.ts diff --git a/config/scripts/performance/native-chat/native-chat-4a-arm.ts b/config/scripts/performance/native-chat/native-chat-4a-arm.ts new file mode 100644 index 00000000000..c8381635a8c --- /dev/null +++ b/config/scripts/performance/native-chat/native-chat-4a-arm.ts @@ -0,0 +1,244 @@ +// One 4a arm: a built checkout, its own envelope/fingerprint builders, and background launches it alone stops by recorded PID. +// Declares the window.__store and window.api globals these page.evaluate callbacks use. +import type {} from '../../../../tests/e2e/helpers/runtime-types' +import { execFileSync } from 'node:child_process' +import { randomUUID } from 'node:crypto' +import { chmodSync, constants, cpSync, existsSync, mkdirSync, writeFileSync } from 'node:fs' +import { createRequire } from 'node:module' +import path from 'node:path' +import { build } from 'esbuild' +import { + _electron as electron, + type ElectronApplication, + type Page +} from '@stablyai/playwright-test' +import { + createElectronHomeIsolation, + assertElectronResolvedIsolatedHome +} from '../../../../tests/e2e/helpers/electron-home-isolation' +import { getOrcaElectronLaunchArgs } from '../../../../tests/e2e/helpers/electron-launch-args' +import { closeElectronAppForE2E } from '../../../../tests/e2e/helpers/electron-process-shutdown' +import { getE2ECompletedOnboardingProfile } from '../../../../tests/e2e/helpers/e2e-completed-onboarding-profile' +import { buildFakeAgentCommandOverride } from '../../../../tests/e2e/helpers/fake-agent-command-override' + +const HARNESS_ROOT = path.resolve(import.meta.dirname, '../../../..') +const SYSTEM_DIRS = ['/usr/bin', '/bin', '/usr/sbin', '/sbin'] + +/** Envelope builders compiled from the arm's own source, so each host checks fingerprints it computes itself. */ +export type ArmProtocol = { + structuredAgentSessionPayloadFingerprint: (input: { + method: string + sessionId: string + fields: Record + }) => string + structuredAgentSessionCreateFingerprint: (input: { + sessionId: string + worktree: string + agent: string + forkFrom?: unknown + }) => string + createStructuredAgentSessionOperationId: (randomUuid: () => string) => string + createStructuredAgentSessionId: (agent: string, randomUuid: () => string) => string + structuredAgentSessionMessageSendMutation: (message: { + sessionId: string + clientOperationId: string + expectedRuntimeFence: number + body: { kind: 'message'; role: 'user'; blocks: { type: 'text'; text: string }[] } + delivery?: 'queue-if-active' + }) => { envelope: unknown; body: unknown; delivery?: 'queue-if-active' } + agentSessionSendBodyFingerprint: (sessionId: string, body: unknown) => string +} + +export type Arm = { + name: string + root: string + sha: string + electronPath: string + protocol: ArmProtocol +} + +export type Launch = { + app: ElectronApplication + page: Page + pid: number + /** `ps` start time recorded at launch; stop refuses a PID whose start time changed. */ + startedAt: string + userDataDir: string + launchedAtEpochMs: number + /** When the renderer reported its workspace session ready: launch-to-ready is metadata initialization. */ + readyEpochMs: number +} + +const git = (root: string, ...args: string[]): string => + execFileSync('git', ['-C', root, ...args], { encoding: 'utf8' }).trim() + +export async function resolveArm(name: string, root: string, scratch: string): Promise { + const absolute = path.resolve(root) + if (!existsSync(path.join(absolute, 'out', 'main', 'index.js'))) { + throw new Error(`${name}: ${absolute} has no out/main/index.js; build that checkout first`) + } + const electronPath = createRequire(path.join(absolute, 'package.json'))('electron') as string + const outfile = path.join(scratch, `${name}-protocol.cjs`) + const shared = './src/shared/agent-session' + await build({ + stdin: { + contents: [ + `export * from '${shared}/lifecycle/structured-agent-session-mutation'`, + `export { createStructuredAgentSessionId } from '${shared}/structured-agent-session-create'`, + `export { structuredAgentSessionMessageSendMutation, agentSessionSendBodyFingerprint } from '${shared}/requests/structured-agent-session-send-mutation'` + ].join(';'), + resolveDir: absolute + }, + bundle: true, + platform: 'node', + format: 'cjs', + outfile, + logLevel: 'silent', + nodePaths: [path.join(HARNESS_ROOT, 'node_modules')] + }) + const protocol = createRequire(import.meta.url)(outfile) as ArmProtocol + return { name, root: absolute, sha: git(absolute, 'rev-parse', 'HEAD'), electronPath, protocol } +} + +/** The stub wrapper and its directory; the launch PATH holds only it and the system tool dirs. */ +export function writeProviderStub(dir: string): string { + mkdirSync(dir, { recursive: true }) + const command = path.join(dir, 'claude') + const quote = (value: string): string => `'${value.replaceAll("'", `'\\''`)}'` + const stub = path.join(import.meta.dirname, 'native-chat-4a-provider-stub.cjs') + writeFileSync( + command, + `#!/bin/sh\nexec ${quote(process.execPath)} ${quote(stub)} ${quote(path.join(dir, 'launches.jsonl'))} "$@"\n` + ) + chmodSync(command, 0o755) + return command +} + +function processStart(pid: number): string { + return execFileSync('ps', ['-o', 'lstart=', '-p', String(pid)], { encoding: 'utf8' }).trim() +} + +export async function launchArm( + arm: Arm, + userDataDir: string, + stubCommand: string +): Promise { + mkdirSync(userDataDir, { recursive: true }) + const profile = path.join(userDataDir, 'orca-data.json') + if (!existsSync(profile)) { + writeFileSync(profile, `${JSON.stringify(getE2ECompletedOnboardingProfile(), null, 2)}\n`) + } + const { ELECTRON_RUN_AS_NODE: _unused, ...inherited } = process.env + void _unused + const isolation = createElectronHomeIsolation({ + inheritedEnv: inherited, + launchEnv: { PATH: [path.dirname(stubCommand), ...SYSTEM_DIRS].join(path.delimiter) }, + extraEnv: {}, + userDataDir + }) + const launchedAtEpochMs = Date.now() + const app = await electron.launch({ + executablePath: arm.electronPath, + args: getOrcaElectronLaunchArgs(path.join(arm.root, 'out', 'main', 'index.js'), false), + env: { + ...isolation.env, + NODE_ENV: 'development', + ORCA_E2E_HEADLESS: '1', + ORCA_BACKGROUND_LAUNCH: '1', + ORCA_EVENT_LOOP_DELAY_PROBE: '1' + } + }) + const pid = app.process().pid + if (!pid) { + await closeElectronAppForE2E(app, { cleanupDaemonsForUserDataDir: userDataDir }) + throw new Error(`${arm.name}: launch recorded no PID`) + } + const startedAt = processStart(pid) + assertElectronResolvedIsolatedHome( + await app.evaluate(({ app: electronApp }) => electronApp.getPath('home')), + isolation + ) + const page = await app.firstWindow({ timeout: 120_000 }) + await page.waitForFunction( + () => window.__store?.getState().workspaceSessionReady === true, + null, + { timeout: 600_000, polling: 16 } + ) + return { app, page, pid, startedAt, userDataDir, launchedAtEpochMs, readyEpochMs: Date.now() } +} + +export async function stopArm(launch: Launch): Promise { + const current = (() => { + try { + return processStart(launch.pid) + } catch { + return null + } + })() + if (current !== null && current !== launch.startedAt) { + throw new Error( + `PID ${launch.pid} is no longer the process this harness launched; not stopping it` + ) + } + await closeElectronAppForE2E(launch.app, { cleanupDaemonsForUserDataDir: launch.userDataDir }) +} + +export type Workspace = { repoId: string; worktreeIds: [string, string] } + +/** Attach the seeded repo, trust it and route Claude to the stub, the way the e2e fixture prepares a profile. */ +export async function prepareWorkspace( + page: Page, + repoPath: string, + stubCommand: string +): Promise { + const override = buildFakeAgentCommandOverride(stubCommand) + return page.evaluate( + async ({ repoPath, override }) => { + const added = await window.api.repos.add({ path: repoPath }) + if ('error' in added) { + throw new Error(added.error) + } + const store = window.__store! + await store.getState().fetchRepos({ runtimeEnvironmentId: null, throwOnError: true }) + await store.getState().updateRepo(added.repo.id, { + externalWorktreeVisibility: 'show', + agentWorkspaceTrust: 'pre-trust' + }) + await store.getState().updateSettings({ + nativeChatProviders: { claude: true, codex: true }, + defaultTuiAgent: 'claude', + agentCmdOverrides: { claude: override } + }) + for (let attempt = 0; attempt < 60; attempt += 1) { + await store.getState().fetchWorktrees(added.repo.id) + const worktrees = store.getState().worktreesByRepo[added.repo.id] ?? [] + if (worktrees.length >= 2) { + const ordered = [...worktrees].sort( + (a, b) => Number(b.isMainWorktree) - Number(a.isMainWorktree) + ) + store.getState().setActiveWorktree(ordered[0]!.id) + return { + repoId: added.repo.id, + worktreeIds: [ordered[0]!.id, ordered[1]!.id] as [string, string] + } + } + await new Promise((resolve) => setTimeout(resolve, 500)) + } + throw new Error('seeded worktrees did not load') + }, + { repoPath, override } + ) +} + +/** Profiles hold absolute home paths (provider transcripts, config pins), so every arm runs at the same `to` path. */ +export function copyProfile(from: string, to: string): void { + cpSync(from, to, { + recursive: true, + errorOnExist: true, + force: false, + mode: constants.COPYFILE_FICLONE + }) +} + +export const operationId = (arm: Arm): string => + arm.protocol.createStructuredAgentSessionOperationId(randomUUID) diff --git a/config/scripts/performance/native-chat/native-chat-4a-comparison.ts b/config/scripts/performance/native-chat/native-chat-4a-comparison.ts new file mode 100644 index 00000000000..cb0e3b2dc98 --- /dev/null +++ b/config/scripts/performance/native-chat/native-chat-4a-comparison.ts @@ -0,0 +1,314 @@ +// The 4a "no slower than main" comparison: one profile seeded once, every arm measured on its own copy, one JSON + one table. +// Dry run (no app): pnpm exec tsx config/scripts/performance/native-chat/native-chat-4a-comparison.ts --profile full --dry-run +// Full run: ... --arm main= --arm candidate= --profile full --out [--large-source] +// Re-table: ... --out --report-only +import { + existsSync, + mkdirSync, + readdirSync, + readFileSync, + renameSync, + writeFileSync +} from 'node:fs' +import os from 'node:os' +import path from 'node:path' +import { parseArgs } from 'node:util' +import type { Arm } from './native-chat-4a-arm' +import type { Manifest } from './native-chat-4a-fixture' +import type { RunContext } from './native-chat-4a-run-context' +import { + armMarkdown, + comparisonMarkdown, + summarizeRow, + type ArmResult, + type Row +} from './native-chat-4a-report' +import { + conversionToolOutputBytes, + dryRunWorkload, + workloadFor, + type Workload +} from './native-chat-4a-workloads' + +const FAMILIES = [ + 'conversion', + 'send', + 'stream', + 'history', + 'outline', + 'records', + 'retained', + 'open', + 'rewind' +] as const +type Family = (typeof FAMILIES)[number] + +const { values } = parseArgs({ + options: { + arm: { type: 'string', multiple: true, default: [] }, + profile: { type: 'string', default: 'smoke' }, + out: { type: 'string' }, + 'seed-arm': { type: 'string' }, + families: { type: 'string', default: FAMILIES.join(',') }, + 'large-source': { type: 'boolean', default: false }, + 'conversion-source-bytes': { type: 'string' }, + 'dry-run': { type: 'boolean', default: false }, + 'report-only': { type: 'boolean', default: false } + } +}) + +const workload = workloadFor( + values.profile, + values['conversion-source-bytes'] === undefined + ? undefined + : Number(values['conversion-source-bytes']) +) +const log = (line: string): void => console.error(`[4a ${new Date().toISOString()}] ${line}`) + +function writeComparison(out: string, results: ArmResult[]): void { + const [base, next] = results + if (!base || !next) { + return + } + const name = `4a-${base.sha.slice(0, 8)}-vs-${next.sha.slice(0, 8)}` + const summaries = results.map((result) => ({ + ...result, + summaries: result.rows.map((row: Row) => ({ ...row, summary: summarizeRow(row) })) + })) + const machine = { + platform: process.platform, + release: os.release(), + cpu: os.cpus()[0]?.model, + cpus: os.cpus().length, + memoryBytes: os.totalmem() + } + writeFileSync( + path.join(out, `${name}.json`), + `${JSON.stringify({ workload, machine, arms: summaries }, null, 2)}\n` + ) + writeFileSync(path.join(out, `${name}.md`), comparisonMarkdown(base, next)) + log(`wrote ${path.join(out, `${name}.md`)}`) +} + +async function runArm(input: { + arm: Arm + out: string + seedDir: string + manifest: Manifest + stubCommand: string + families: Set + largeSource?: { seedDir: string; manifest: Manifest } +}): Promise { + const { copyProfile, launchArm, stopArm } = await import('./native-chat-4a-arm') + const { installVisibilityProbe } = await import('./native-chat-4a-probes') + const { resetTags } = await import('./native-chat-4a-run-context') + const lifecycle = await import('./native-chat-4a-ops-lifecycle') + const queue = await import('./native-chat-4a-ops-queue') + const retained = await import('./native-chat-4a-ops-retained') + const stream = await import('./native-chat-4a-ops-stream') + const reads = await import('./native-chat-4a-ops-reads') + const profileDir = path.join(input.out, 'profile') + const rows: Row[] = [] + const startedAt = new Date().toISOString() + + const onProfile = async ( + seedDir: string, + manifest: typeof input.manifest, + body: (ctx: RunContext) => Promise + ) => { + if (existsSync(profileDir)) { + renameSync(profileDir, path.join(input.out, `stale-profile-${Date.now()}`)) + } + copyProfile(seedDir, profileDir) + resetTags() + const open = async () => { + const launch = await launchArm(input.arm, profileDir, input.stubCommand) + await installVisibilityProbe(launch.page) + return launch + } + const ctx = { + arm: input.arm, + launch: await open(), + workload, + manifest, + rows, + log, + relaunch: async () => undefined + } + ctx.relaunch = async () => { + await stopArm(ctx.launch) + ctx.launch = await open() + } + try { + await body(ctx) + } finally { + await stopArm(ctx.launch) + renameSync( + profileDir, + path.join( + input.out, + `after-${input.arm.name}-${manifest.toolOutputBytes}-${input.arm.sha.slice(0, 8)}` + ) + ) + } + } + + const run = async (family: Family, body: () => Promise) => { + if (!input.families.has(family)) { + return + } + log(`${input.arm.name}: ${family}`) + try { + await body() + } catch (error) { + const reason = + `family failed: ${error instanceof Error ? error.message : String(error)}`.slice(0, 300) + rows.push({ + family, + operation: '(family)', + variant: 'all', + samples: [], + context: {}, + skipped: reason + }) + log(`${input.arm.name}: ${family} ${reason}`) + } + } + + await onProfile(input.seedDir, input.manifest, async (ctx) => { + const seedFiles = lifecycle.seededDbFiles(input.seedDir) + // First: the seeded tail markers and unconverted state exist only before any other family writes. + await run('conversion', () => + lifecycle.firstLaunchRows( + ctx, + seedFiles, + `${workload.history.largeTurns}-turn/${workload.history.fillerChats + 1}-chat profile` + ) + ) + await run('send', () => queue.sendAndQueueRows(ctx)) + await run('stream', () => stream.streamingRows(ctx)) + await run('history', () => reads.historyRows(ctx)) + await run('outline', () => reads.outlineAndForkRows(ctx)) + await run('records', () => reads.recordRows(ctx)) + await run('retained', () => retained.retainedAdmissionRows(ctx)) + await run('open', () => lifecycle.openRows(ctx)) + await run('rewind', () => lifecycle.rewindRows(ctx)) + }) + if (input.largeSource && input.families.has('conversion')) { + const source = input.largeSource + const label = `${(workload.conversionSourceBytes / 1024 ** 3).toFixed(0)} GiB source` + await onProfile(source.seedDir, source.manifest, (ctx) => + run('conversion', () => + lifecycle.firstLaunchRows(ctx, lifecycle.seededDbFiles(source.seedDir), label) + ) + ) + } + return { + arm: input.arm.name, + sha: input.arm.sha, + startedAt, + finishedAt: new Date().toISOString(), + rows, + fixture: { + repoId: input.manifest.workspace.repoId, + worktreeIds: input.manifest.workspace.worktreeIds.join(' '), + seedArm: input.manifest.seedArm, + seedSha: input.manifest.seedSha, + largeItems: input.manifest.largeItems, + storageBytes: input.manifest.storageBytes, + seedMs: input.manifest.seedMs + } + } +} + +async function main(): Promise { + if (values['dry-run']) { + console.log(JSON.stringify(dryRunWorkload(workload), null, 2)) + return + } + const out = path.resolve(values.out ?? path.join(os.tmpdir(), `orca-4a-${workload.profile}`)) + mkdirSync(out, { recursive: true }) + if (values['report-only']) { + const results = readdirSync(out) + .filter((file) => /^arm-.*\.json$/.test(file)) + .map((file) => JSON.parse(readFileSync(path.join(out, file), 'utf8')) as ArmResult) + .sort((a, b) => a.startedAt.localeCompare(b.startedAt)) + writeComparison(out, results) + return + } + const families = new Set(values.families.split(',').map((family) => family.trim()) as Family[]) + const { resolveArm, writeProviderStub } = await import('./native-chat-4a-arm') + const { seedProfile, seedDirFor } = await import('./native-chat-4a-fixture') + const { createSeededTestRepo } = await import('../../../../tests/e2e/helpers/seeded-test-repo') + const scratch = path.join(out, 'scratch') + mkdirSync(scratch, { recursive: true }) + const arms: Arm[] = [] + for (const spec of values.arm) { + const [name, root] = spec.split('=') + if (!name || !root) { + throw new Error(`--arm takes name=checkout, got ${spec}`) + } + arms.push(await resolveArm(name, root, scratch)) + } + if (arms.length === 0) { + throw new Error('Pass at least one --arm name=; main first, then the candidate') + } + const stubCommand = writeProviderStub(path.join(out, 'stub')) + const repoFile = path.join(out, 'repo-path.txt') + const repoPath = existsSync(repoFile) + ? readFileSync(repoFile, 'utf8').trim() + : createSeededTestRepo({ publishPath: false }) + writeFileSync(repoFile, repoPath) + const seedArm = arms.find((arm) => arm.name === values['seed-arm']) ?? arms[0]! + const seed = (toolOutputBytes: number, seeded: Workload) => + seedProfile({ + arm: seedArm, + workload: seeded, + runDir: out, + profileDir: path.join(out, 'profile'), + repoPath, + stubCommand, + toolOutputBytes, + log + }) + const manifest = await seed(workload.history.toolOutputBytes, workload) + const large = + values['large-source'] && workload.conversionSourceBytes > 0 + ? await seed(conversionToolOutputBytes(workload), { + ...workload, + history: { ...workload.history, fillerChats: 3 } + }) + : null + const results: ArmResult[] = [] + for (const arm of arms) { + const result = await runArm({ + arm, + out, + seedDir: seedDirFor(out, manifest), + manifest, + stubCommand, + families, + ...(large ? { largeSource: { seedDir: seedDirFor(out, large), manifest: large } } : {}) + }) + writeFileSync( + path.join(out, `arm-${arm.name}-${arm.sha.slice(0, 12)}.json`), + `${JSON.stringify(result, null, 2)}\n` + ) + writeFileSync(path.join(out, `arm-${arm.name}-${arm.sha.slice(0, 12)}.md`), armMarkdown(result)) + results.push(result) + } + writeComparison(out, results) + const skipped = results.flatMap((result) => + result.rows + .filter((row) => row.skipped) + .map((row) => `${result.arm}: ${row.family}/${row.operation}/${row.variant}: ${row.skipped}`) + ) + if (skipped.length > 0) { + log(`rows without samples (not passes):\n${skipped.join('\n')}`) + } +} + +main().catch((error: unknown) => { + console.error(error) + process.exitCode = 1 +}) diff --git a/config/scripts/performance/native-chat/native-chat-4a-fixture.ts b/config/scripts/performance/native-chat/native-chat-4a-fixture.ts new file mode 100644 index 00000000000..eabedcc6236 --- /dev/null +++ b/config/scripts/performance/native-chat/native-chat-4a-fixture.ts @@ -0,0 +1,151 @@ +// Seed the retained-history profile once through the seed arm's own host, so every arm starts from the same logical data. +import { existsSync, readFileSync, renameSync, writeFileSync } from 'node:fs' +import path from 'node:path' +import { + launchArm, + prepareWorkspace, + stopArm, + type Arm, + type Workspace +} from './native-chat-4a-arm' +import { rpc, storageBytes } from './native-chat-4a-probes' +import { createChat, history, send, waitForDurable, waitForIdle } from './native-chat-4a-session' +import { + fillerTurn, + largeHistoryTurns, + expectedLargeItems, + type Workload +} from './native-chat-4a-workloads' + +export type Manifest = { + seedArm: string + seedSha: string + workload: Workload['profile'] + toolOutputBytes: number + workspace: Workspace + largeSessionId: string + fillerSessionIds: string[] + largeItems: number + storageBytes: number + seedMs: number +} + +const SLEEP_FILLERS_EVERY = 20 + +/** Count the large chat's rows by paging back from the tail; refuses a fixture that seeded fewer rows than its recipe. */ +async function countItems( + page: Awaited>['page'], + sessionId: string +): Promise { + let count = 0 + let cursor: unknown + for (;;) { + const read = await history(page, sessionId, cursor ? 'before' : 'tail', 200, cursor) + count += read.page.items.length + if (!read.page.hasOlder) { + return count + } + cursor = read.page.window.nextCursor + } +} + +export async function seedProfile(input: { + arm: Arm + workload: Workload + runDir: string + profileDir: string + repoPath: string + stubCommand: string + toolOutputBytes: number + log: (line: string) => void +}): Promise { + const seedDir = path.join(input.runDir, `seed-${input.workload.profile}-${input.toolOutputBytes}`) + const manifestPath = `${seedDir}.json` + if (existsSync(manifestPath) && existsSync(seedDir)) { + return JSON.parse(readFileSync(manifestPath, 'utf8')) as Manifest + } + const started = Date.now() + const launch = await launchArm(input.arm, input.profileDir, input.stubCommand) + try { + const { page } = launch + const workspace = await prepareWorkspace(page, input.repoPath, input.stubCommand) + const large = await createChat(page, input.arm, workspace.worktreeIds[0]) + let turn = 0 + for (const next of largeHistoryTurns({ + ...input.workload, + history: { ...input.workload.history, toolOutputBytes: input.toolOutputBytes } + })) { + // A queued turn is sent while its predecessor still runs, leaving released queue history behind. + if (turn > 0 && !next.queued) { + await waitForDurable(page, large.sessionId, `4A-TAIL-large-${turn - 1}`) + } + await send(page, input.arm, large.sessionId, next.prompt) + turn += 1 + if (turn % 250 === 0) { + input.log(`seed: large chat ${turn}/${input.workload.history.largeTurns} turns`) + } + } + await waitForDurable(page, large.sessionId, `4A-TAIL-large-${turn - 1}`, 600_000) + await waitForIdle(page, large.sessionId, 600_000) + const fillerSessionIds: string[] = [] + const workspaceId = workspace.worktreeIds[1] + for (let chat = 0; chat < input.workload.history.fillerChats; chat += 1) { + const filler = await createChat(page, input.arm, workspaceId) + for ( + let fillerTurnIndex = 0; + fillerTurnIndex < input.workload.history.fillerTurns; + fillerTurnIndex += 1 + ) { + await send( + page, + input.arm, + filler.sessionId, + fillerTurn(chat, fillerTurnIndex, input.workload) + ) + await waitForDurable(page, filler.sessionId, `4A-TAIL-filler-${chat}-${fillerTurnIndex}`) + } + fillerSessionIds.push(filler.sessionId) + // Bound live provider processes: retained chats stay, their stubs stop. + if ((chat + 1) % SLEEP_FILLERS_EVERY === 0) { + await rpc(page, 'agentSession.sleepWorkspace', { workspaceId }) + input.log(`seed: ${chat + 1}/${input.workload.history.fillerChats} filler chats`) + } + } + const largeItems = await countItems(page, large.sessionId) + // Hosts may group a call with its result; fewer than prompt+reply per turn means turns were lost. + if (largeItems < input.workload.history.largeTurns * 2) { + throw new Error( + `seed: large chat holds ${largeItems} rows for ${input.workload.history.largeTurns} turns` + ) + } + if (largeItems < expectedLargeItems(input.workload)) { + input.log( + `seed: large chat holds ${largeItems} visible rows; recipe writes ${expectedLargeItems(input.workload)}` + ) + } + const manifest: Manifest = { + seedArm: input.arm.name, + seedSha: input.arm.sha, + workload: input.workload.profile, + toolOutputBytes: input.toolOutputBytes, + workspace, + largeSessionId: large.sessionId, + fillerSessionIds, + largeItems, + storageBytes: 0, + seedMs: 0 + } + await stopArm(launch) + manifest.storageBytes = storageBytes(input.profileDir).total + manifest.seedMs = Date.now() - started + renameSync(input.profileDir, seedDir) + writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`) + return manifest + } catch (error) { + await stopArm(launch).catch(() => undefined) + throw error + } +} + +export const seedDirFor = (runDir: string, manifest: Manifest): string => + path.join(runDir, `seed-${manifest.workload}-${manifest.toolOutputBytes}`) diff --git a/config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts b/config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts new file mode 100644 index 00000000000..c395407b1ce --- /dev/null +++ b/config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts @@ -0,0 +1,313 @@ +// 4a process-lifecycle rows: first launch on the seeded profile (the candidate's one-time conversion), cold/warm open, and rewind. +import { readdirSync } from 'node:fs' +import path from 'node:path' +import { + markerSeenAt, + rendererNow, + rpc, + samplePressure, + storageBytes, + watchMarkers +} from './native-chat-4a-probes' +import type { Sample } from './native-chat-4a-report' +import { + addRow, + measured, + page, + sampled, + tagFor, + type RunContext +} from './native-chat-4a-run-context' +import { + history, + mutate, + openChatTab, + send, + userItemIds, + waitForControls, + waitForDurable, + waitForIdle +} from './native-chat-4a-session' +import { replyPrompt } from './native-chat-4a-workloads' + +type Chat = { sessionId: string; worktreeId: string } + +/** Open a chat and time it to its known tail marker visible plus controls known. */ +async function openTo(ctx: RunContext, chat: Chat, marker: string): Promise { + return measured(ctx, async () => { + await watchMarkers(page(ctx), [marker]) + const startedAt = await rendererNow(page(ctx)) + await openChatTab(page(ctx), chat.worktreeId, chat.sessionId) + const tailAt = await markerSeenAt(page(ctx), marker, 600_000) + await waitForControls(page(ctx), 600_000) + return { + e2eMs: (await rendererNow(page(ctx))) - startedAt, + extra: { tailVisibleMs: tailAt - startedAt } + } + }) +} + +/** Give a chat a known tail so open rows end at "the correct tail is visible". */ +async function markTail(ctx: RunContext, chat: Chat): Promise { + const tag = tagFor('tail') + await send(page(ctx), ctx.arm, chat.sessionId, replyPrompt(tag)) + await waitForDurable(page(ctx), chat.sessionId, `4A-TAIL-${tag}`) + await waitForIdle(page(ctx), chat.sessionId) + return `4A-TAIL-${tag}` +} + +const dbFiles = (dir: string): string[] => + readdirSync(dir, { recursive: true, withFileTypes: true }) + .filter( + (entry) => + entry.isFile() && + /\.(db|sqlite)$/.test(entry.name) && + !entry.parentPath.includes(`${path.sep}home`) + ) + .map((entry) => path.relative(dir, path.join(entry.parentPath, entry.name))) + +/** + * The arm's first launch on the seeded profile. On the candidate this is the accepted one-time conversion; on main it is a + * cold open of the same data, so the rows line up but are reported apart from steady state. + */ +export async function firstLaunchRows( + ctx: RunContext, + seedDbFiles: string[], + label: string +): Promise { + const { workspace, largeSessionId, fillerSessionIds } = ctx.manifest + const context = { + profile: label, + largeItems: ctx.manifest.largeItems, + seededBytes: ctx.manifest.storageBytes, + chats: fillerSessionIds.length + 1 + } + const init = addRow(ctx, { + family: 'conversion', + operation: 'launch to session ready', + variant: label, + context + }) + init.samples.push({ e2eMs: ctx.launch.readyEpochMs - ctx.launch.launchedAtEpochMs }) + const stopPressure = samplePressure(ctx.launch.app, ctx.launch.userDataDir, 500) + const small: Chat = { sessionId: fillerSessionIds[0]!, worktreeId: workspace.worktreeIds[1] } + const smallOpen = addRow(ctx, { + family: 'conversion', + operation: 'first open, small chat', + variant: label, + context + }) + await sampled(ctx, smallOpen, 1, () => + openTo(ctx, small, `4A-TAIL-filler-0-${ctx.workload.history.fillerTurns - 1}`) + ) + const largeOpen = addRow(ctx, { + family: 'conversion', + operation: 'first open, large chat, with sends to the opened small chat', + variant: label, + context: { ...context, concurrent: 'sends to the already-opened small chat every 250 ms' } + }) + let opening = true + const concurrent: number[] = [] + const sends = (async () => { + while (opening) { + concurrent.push( + (await send(page(ctx), ctx.arm, small.sessionId, replyPrompt(tagFor('during')))).hostMs + ) + await new Promise((resolve) => setTimeout(resolve, 250)) + } + })() + await sampled(ctx, largeOpen, 1, () => + openTo( + ctx, + { sessionId: largeSessionId, worktreeId: workspace.worktreeIds[0] }, + `4A-TAIL-large-${ctx.workload.history.largeTurns - 1}` + ) + ) + opening = false + await sends + const during = addRow(ctx, { + family: 'conversion', + operation: 'small-chat send during large first open', + variant: label, + context + }) + during.samples.push(...concurrent.map((hostMs) => ({ hostMs }))) + const pressure = await stopPressure() + const remaining = dbFiles(ctx.launch.userDataDir) + const disk = addRow(ctx, { + family: 'conversion', + operation: 'disk', + variant: label, + context: { + retainedOriginalFiles: + seedDbFiles.filter((file) => remaining.includes(file)).join(' ') || 'none', + newFiles: remaining.filter((file) => !seedDbFiles.includes(file)).join(' ') || 'none' + } + }) + disk.samples.push({ + extra: { + peakTotalBytes: pressure.peakTotalBytes, + afterTotalBytes: storageBytes(ctx.launch.userDataDir).total, + peakMainRssBytes: pressure.peakMainRssBytes + } + }) + await waitForIdle(page(ctx), small.sessionId) +} + +export const seededDbFiles = dbFiles + +export async function openRows(ctx: RunContext): Promise { + const { workspace, largeSessionId, fillerSessionIds } = ctx.manifest + const large: Chat = { sessionId: largeSessionId, worktreeId: workspace.worktreeIds[0] } + const small: Chat = { sessionId: fillerSessionIds[0]!, worktreeId: workspace.worktreeIds[1] } + const marks = { large: await markTail(ctx, large), small: await markTail(ctx, small) } + const context = { largeItems: ctx.manifest.largeItems, chats: fillerSessionIds.length + 1 } + const ready = addRow(ctx, { + family: 'open', + operation: 'launch to session ready (metadata only)', + variant: 'converted profile', + context + }) + const coldLarge = addRow(ctx, { + family: 'open', + operation: 'cold process first open', + variant: 'large chat', + context + }) + const coldSmall = addRow(ctx, { + family: 'open', + operation: 'cold process first open', + variant: 'small chat', + context + }) + const coldOutline = addRow(ctx, { + family: 'outline', + operation: 'conversationOutline', + variant: 'first in process (cold)', + context + }) + // A third chat is left active, so the relaunch restores it and the measured chats open for the first time. + const neutral: Chat = { + sessionId: fillerSessionIds[2] ?? fillerSessionIds.at(-1)!, + worktreeId: workspace.worktreeIds[1] + } + for (let launch = 0; launch < ctx.workload.coldSamples; launch += 1) { + await openChatTab(page(ctx), neutral.worktreeId, neutral.sessionId) + await ctx.relaunch() + ready.samples.push({ e2eMs: ctx.launch.readyEpochMs - ctx.launch.launchedAtEpochMs }) + const [first, second, firstRow, secondRow] = + launch % 2 === 0 ? [large, small, coldLarge, coldSmall] : [small, large, coldSmall, coldLarge] + await sampled(ctx, firstRow, 1, () => + openTo(ctx, first, first === large ? marks.large : marks.small) + ) + await sampled(ctx, secondRow, 1, () => + openTo(ctx, second, second === large ? marks.large : marks.small) + ) + await sampled(ctx, coldOutline, 1, () => + measured(ctx, async () => ({ + hostMs: ( + await rpc(page(ctx), 'agentSession.conversationOutline', { sessionId: largeSessionId }) + ).hostMs + })) + ) + } + const unopened = addRow(ctx, { + family: 'open', + operation: 'warm process, unopened chat', + variant: 'small chat', + context + }) + const untouched = fillerSessionIds.slice(3, -1) + await sampled(ctx, unopened, Math.min(ctx.workload.samples, untouched.length), (index) => { + const chat = { sessionId: untouched[index]!, worktreeId: workspace.worktreeIds[1] } + const chatIndex = fillerSessionIds.indexOf(chat.sessionId) + return openTo(ctx, chat, `4A-TAIL-filler-${chatIndex}-${ctx.workload.history.fillerTurns - 1}`) + }) + for (const [variant, chat, marker] of [ + ['large chat', large, marks.large], + ['small chat', small, marks.small] + ] as const) { + const reopen = addRow(ctx, { family: 'open', operation: 'warm reopen', variant, context }) + await sampled(ctx, reopen, ctx.workload.samples, async () => { + await openChatTab( + page(ctx), + (chat === large ? small : large).worktreeId, + (chat === large ? small : large).sessionId + ) + return openTo(ctx, chat, marker) + }) + } +} + +/** Near-tip and deep rewinds on the large chat, with a competing send to another chat while the local writer cuts. */ +export async function rewindRows(ctx: RunContext): Promise { + const { workspace, largeSessionId, fillerSessionIds } = ctx.manifest + const other = fillerSessionIds[1] ?? fillerSessionIds[0]! + await openChatTab(page(ctx), workspace.worktreeIds[0], largeSessionId) + await waitForControls(page(ctx)) + const rewindOnce = async (itemId: string, goneMarker: string): Promise => { + const epoch = (await history(page(ctx), largeSessionId, 'tail', 1)).page.epoch + return measured(ctx, async () => { + const competingTag = tagFor('rewind-compete') + const [answer, competing] = await Promise.all([ + mutate(page(ctx), ctx.arm, 'agentSession.rewind', largeSessionId, { + itemId, + expectedEpoch: epoch + }), + send(page(ctx), ctx.arm, other, replyPrompt(competingTag)) + ]) + const goneAt = await page(ctx) + .waitForFunction( + (marker) => { + const root = [...document.querySelectorAll('[data-native-chat-root="true"]')].find( + (candidate) => candidate.checkVisibility() + ) + return root && !root.textContent?.includes(marker) ? performance.now() : false + }, + goneMarker, + { timeout: 600_000, polling: 16 } + ) + .then(async (handle) => (await handle.jsonValue()) as number) + await waitForDurable(page(ctx), other, `4A-TAIL-${competingTag}`) + return { + hostMs: answer.hostMs, + e2eMs: goneAt - answer.startedAt, + extra: { competingSendHostMs: competing.hostMs } + } + }) + } + const endToEndOnly = 'preflight, provider success and local cut are one host span' + const nearTip = addRow(ctx, { + family: 'rewind', + operation: 'near-tip rewind', + variant: 'last turn', + context: { largeItems: ctx.manifest.largeItems }, + endToEndOnly + }) + await sampled(ctx, nearTip, ctx.workload.rewind.nearTipSamples, async () => { + const marker = await markTail(ctx, { + sessionId: largeSessionId, + worktreeId: workspace.worktreeIds[0] + }) + const [tip] = (await userItemIds(page(ctx), largeSessionId, 1)).slice(-1) + return rewindOnce(tip!, marker) + }) + const cut = ctx.workload.rewind.deepCutTurns + const deep = addRow(ctx, { + family: 'rewind', + operation: 'deep rewind', + variant: `${cut} turns from tip`, + context: { removedTurns: cut }, + endToEndOnly + }) + await sampled(ctx, deep, ctx.workload.rewind.deepSamples, async () => { + const ids = await userItemIds(page(ctx), largeSessionId, cut + 1) + const tail = JSON.stringify( + (await history(page(ctx), largeSessionId, 'tail', 3)).page.items + ).match(/4A-TAIL-[\w-]+/)?.[0] + if (ids.length <= cut || !tail) { + throw new Error(`large chat has ${ids.length} user turns left, deep cut needs ${cut + 1}`) + } + return rewindOnce(ids[0]!, tail) + }) +} diff --git a/config/scripts/performance/native-chat/native-chat-4a-ops-queue.ts b/config/scripts/performance/native-chat/native-chat-4a-ops-queue.ts new file mode 100644 index 00000000000..7de40e92359 --- /dev/null +++ b/config/scripts/performance/native-chat/native-chat-4a-ops-queue.ts @@ -0,0 +1,245 @@ +// 4a send and queue rows, plus R3's retained-history admission: a small live queue over growing released history. +import { operationId } from './native-chat-4a-arm' +import { rpc, markerSeenAt, watchMarkers } from './native-chat-4a-probes' +import { + addRow, + measured, + page, + sampled, + tagFor, + type RunContext +} from './native-chat-4a-run-context' +import { + history, + mutate, + openChatTab, + send, + waitForControls, + waitForIdle, + waitForWorking +} from './native-chat-4a-session' +import { holdPrompt, replyPrompt } from './native-chat-4a-workloads' +import providerText from './native-chat-4a-provider-text.cjs' + +/** Type into the composer, then click Send inside the page so the start mark is the click itself. */ +async function clickSend(ctx: RunContext, text: string): Promise { + const composer = page(ctx) + .locator('[data-native-chat-root="true"]:visible [role="textbox"]') + .first() + await composer.fill(text) + return page(ctx).evaluate(() => { + const root = [...document.querySelectorAll('[data-native-chat-root="true"]')].find( + (candidate) => candidate.checkVisibility() + ) + const button = [...(root?.querySelectorAll('button') ?? [])].find( + (candidate) => + candidate.getAttribute('aria-label') === 'Send' || candidate.textContent?.trim() === 'Send' + ) + if (!button) { + throw new Error('Send button not found') + } + const startedAt = performance.now() + button.click() + return startedAt + }) +} + +export const sentText = (tag: string, padding = 0): string => + `${providerText.formatDirective({ op: 'reply', tag }, padding)} 4A-SENT-${tag}` + +export async function holdActive(ctx: RunContext, sessionId: string, ms: number): Promise { + const tag = tagFor('hold') + await send(page(ctx), ctx.arm, sessionId, holdPrompt(tag, ms)) + await waitForWorking(page(ctx), true) + return tag +} + +export async function queued(ctx: RunContext, sessionId: string) { + return (await history(page(ctx), sessionId, 'tail', 1)).page.queuedMessages ?? [] +} + +export async function sendAndQueueRows(ctx: RunContext): Promise { + const { workspace, fillerSessionIds, largeSessionId } = ctx.manifest + const small = fillerSessionIds[0]! + const n = ctx.workload.samples + await openChatTab(page(ctx), workspace.worktreeIds[1], small) + await waitForControls(page(ctx)) + + const idle = addRow(ctx, { + family: 'send', + operation: 'idle direct send', + variant: 'small chat, click', + context: { chats: fillerSessionIds.length + 1 } + }) + await sampled(ctx, idle, n, () => + measured(ctx, async () => { + const tag = tagFor('idle') + await watchMarkers(page(ctx), [`4A-SENT-${tag}`, `4A-TAIL-${tag}`]) + const clickedAt = await clickSend(ctx, sentText(tag)) + const sentAt = await markerSeenAt(page(ctx), `4A-SENT-${tag}`) + const handover = await waitForWorking(page(ctx), true).catch(() => Number.NaN) + const replyAt = await markerSeenAt(page(ctx), `4A-TAIL-${tag}`) + await waitForIdle(page(ctx), small) + return { + e2eMs: sentAt - clickedAt, + extra: { handoverMs: handover - clickedAt, replyVisibleMs: replyAt - clickedAt } + } + }) + ) + + const duplicate = addRow(ctx, { + family: 'send', + operation: 'duplicate retry', + variant: 'same operation id', + context: {} + }) + await sampled(ctx, duplicate, n, async () => { + const tag = tagFor('dup') + const params = ctx.arm.protocol.structuredAgentSessionMessageSendMutation({ + sessionId: small, + clientOperationId: operationId(ctx.arm), + expectedRuntimeFence: (await history(page(ctx), small, 'tail', 1)).page.fence ?? 1, + body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: replyPrompt(tag) }] }, + delivery: 'queue-if-active' + }) + const first = await rpc(page(ctx), 'agentSession.send', params) + const sample = await measured(ctx, async () => ({ + hostMs: (await rpc(page(ctx), 'agentSession.send', params)).hostMs + })) + await waitForIdle(page(ctx), small) + return { ...sample, extra: { firstHostMs: first.hostMs } } + }) + + await openChatTab(page(ctx), workspace.worktreeIds[0], largeSessionId) + await waitForControls(page(ctx)) + for (const populated of [0, 5]) { + const active = addRow(ctx, { + family: 'send', + operation: 'send while active', + variant: `large chat, ${populated} already queued`, + context: { + largeItems: ctx.manifest.largeItems, + queueLength: populated, + queuedBodyBytes: populated * 4096 + } + }) + await sampled(ctx, active, n, async () => { + await holdActive(ctx, largeSessionId, 60_000) + for (let index = 0; index < populated; index += 1) { + await send(page(ctx), ctx.arm, largeSessionId, sentText(tagFor('fill'), 4096)) + } + const tag = tagFor('active') + await watchMarkers(page(ctx), [`4A-SENT-${tag}`]) + const sample = await measured(ctx, async () => { + const answer = await send(page(ctx), ctx.arm, largeSessionId, sentText(tag)) + const shownAt = await markerSeenAt(page(ctx), `4A-SENT-${tag}`) + return { hostMs: answer.hostMs, e2eMs: shownAt - answer.startedAt } + }) + await mutate(page(ctx), ctx.arm, 'agentSession.cancel', largeSessionId, {}) + await drainQueue(ctx, largeSessionId) + return sample + }) + } + + for (const editHold of [false, true]) { + const claim = addRow(ctx, { + family: 'send', + operation: 'queue claim', + variant: editHold ? 'after edit hold release' : 'no edit hold', + context: { queueLength: 1 } + }) + await sampled(ctx, claim, n, async () => { + const hold = await holdActive(ctx, largeSessionId, 1_500) + const tag = tagFor('claim') + await send(page(ctx), ctx.arm, largeSessionId, sentText(tag)) + const [card] = await queued(ctx, largeSessionId) + const editId = tagFor('edit') + if (editHold && card) { + const expectedBodyFingerprint = ctx.arm.protocol.agentSessionSendBodyFingerprint( + largeSessionId, + card.body + ) + await rpc(page(ctx), 'agentSession.queuedMessageEditHold', { + sessionId: largeSessionId, + messageId: card.messageId, + editId, + action: 'acquire', + expectedBodyFingerprint + }) + } + await watchMarkers(page(ctx), [`4A-TAIL-${hold}`, `4A-TAIL-${tag}`]) + // With a hold, the claim can start only at release; without one, at the active turn's reply. + let freedAt = await markerSeenAt(page(ctx), `4A-TAIL-${hold}`) + if (editHold && card) { + const release = { + sessionId: largeSessionId, + messageId: card.messageId, + editId, + action: 'release' + } + freedAt = (await rpc(page(ctx), 'agentSession.queuedMessageEditHold', release)).startedAt + } + const repliedAt = await markerSeenAt(page(ctx), `4A-TAIL-${tag}`) + await waitForIdle(page(ctx), largeSessionId) + return { e2eMs: repliedAt - freedAt } + }) + } + + const edit = addRow(ctx, { + family: 'send', + operation: 'queued edit and move', + variant: '3 queued', + context: { queueLength: 3 } + }) + await sampled(ctx, edit, n, async () => { + await holdActive(ctx, largeSessionId, 60_000) + for (let index = 0; index < 3; index += 1) { + await send(page(ctx), ctx.arm, largeSessionId, sentText(tagFor('q'))) + } + const cards = await queued(ctx, largeSessionId) + const target = cards.at(-1)! + const tag = tagFor('edited') + await watchMarkers(page(ctx), [`4A-SENT-${tag}`]) + const sample = await measured(ctx, async () => { + const update = await mutate( + page(ctx), + ctx.arm, + 'agentSession.queuedMessageUpdate', + largeSessionId, + { + messageId: target.messageId, + expectedBodyFingerprint: ctx.arm.protocol.agentSessionSendBodyFingerprint( + largeSessionId, + target.body + ), + text: sentText(tag) + } + ) + const shownAt = await markerSeenAt(page(ctx), `4A-SENT-${tag}`) + const move = await mutate( + page(ctx), + ctx.arm, + 'agentSession.queuedMessageMove', + largeSessionId, + { messageId: target.messageId, beforeMessageId: cards[0]!.messageId } + ) + return { + hostMs: update.hostMs, + e2eMs: shownAt - update.startedAt, + extra: { moveHostMs: move.hostMs } + } + }) + await mutate(page(ctx), ctx.arm, 'agentSession.cancel', largeSessionId, {}) + await drainQueue(ctx, largeSessionId) + return sample + }) +} + +/** Stop pauses the queue; resume lets it drain so the next sample starts idle with an empty queue. */ +async function drainQueue(ctx: RunContext, sessionId: string): Promise { + await waitForWorking(page(ctx), false).catch(() => undefined) + if ((await queued(ctx, sessionId)).length > 0) { + await mutate(page(ctx), ctx.arm, 'agentSession.queuedMessagesResume', sessionId, {}) + } + await waitForIdle(page(ctx), sessionId, 300_000) +} diff --git a/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts b/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts new file mode 100644 index 00000000000..47b3bb96bb3 --- /dev/null +++ b/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts @@ -0,0 +1,238 @@ +// 4a read rows on the retained large chat: history pages, item detail, outline, fork, and record operations on the many-chat profile. +import { randomUUID } from 'node:crypto' +import { operationId } from './native-chat-4a-arm' +import { rpc } from './native-chat-4a-probes' +import type { Sample } from './native-chat-4a-report' +import { + addRow, + measured, + page, + sampled, + tagFor, + type RunContext +} from './native-chat-4a-run-context' +import { createChat, history, mutate, userItemIds, waitForIdle } from './native-chat-4a-session' + +const bytesOf = (value: unknown): number => Buffer.byteLength(JSON.stringify(value)) + +type Item = { id: string; body?: { kind?: string } } + +async function read( + ctx: RunContext, + method: string, + params: unknown, + allowRefusal = false +): Promise { + return measured(ctx, async () => { + const answer = await rpc(page(ctx), method, params, allowRefusal ? { allowRefusal: true } : {}) + return { + hostMs: answer.hostMs, + extra: { bytes: bytesOf(answer.value), refused: Number(answer.refused) } + } + }) +} + +export async function historyRows(ctx: RunContext): Promise { + const sessionId = ctx.manifest.largeSessionId + const n = ctx.workload.samples + const context = { largeItems: ctx.manifest.largeItems } + const tail = await history(page(ctx), sessionId, 'tail', 200) + const older = await history(page(ctx), sessionId, 'before', 200, tail.page.window.nextCursor) + const catchUpFrom = older.page.window.nextCursor + const pages: [string, Record][] = [ + ['tail 40', { direction: 'tail', limit: 40 }], + ['tail 200 (same page repeated)', { direction: 'tail', limit: 200 }], + [ + 'one older page 200', + { direction: 'before', limit: 200, cursor: tail.page.window.nextCursor } + ], + ['ordinary after catch-up 200', { direction: 'after', limit: 200, cursor: catchUpFrom }], + [ + 'stale epoch reset catch-up', + { direction: 'after', limit: 200, cursor: { epoch: 'stale-4a-epoch', sequence: 1 } } + ] + ] + for (const [variant, params] of pages) { + const row = addRow(ctx, { + family: 'history', + operation: 'agentSession.history', + variant, + context + }) + await sampled(ctx, row, n, () => + read(ctx, 'agentSession.history', { sessionId, ...params }, true) + ) + } + addRow(ctx, { + family: 'history', + operation: 'sparse/interleaved subagent grouping', + variant: 'tail 200', + context, + skipped: + 'the 4a provider stub emits no subagent frames; covered by the Claude/Codex functional QA run' + }) + + // Huge originals come from the streaming rows that ran first; the smallest seeded tool item is the ordinary case. + const items = [...tail.page.items, ...older.page.items] as Item[] + const sized = items + .map((item) => ({ item, bytes: bytesOf(item) })) + .sort((a, b) => b.bytes - a.bytes) + const ordinary = sized.findLast((entry) => entry.item.body?.kind === 'tool-call') ?? sized.at(-1) + const largest = sized[0] + for (const [variant, entry] of [ + ['ordinary tool item', ordinary], + ['largest item on the tail pages', largest] + ] as const) { + const row = addRow(ctx, { + family: 'history', + operation: 'agentSession.item detail', + variant, + context: { wireHeadBytes: entry?.bytes ?? 0 } + }) + if (!entry) { + row.skipped = 'no item on the tail pages' + continue + } + await sampled(ctx, row, n, () => + read(ctx, 'agentSession.item', { sessionId, itemId: entry.item.id }) + ) + } +} + +export async function outlineAndForkRows(ctx: RunContext): Promise { + const sessionId = ctx.manifest.largeSessionId + const outline = addRow(ctx, { + family: 'outline', + operation: 'conversationOutline', + variant: 'repeated (warm)', + context: { largeItems: ctx.manifest.largeItems } + }) + await sampled(ctx, outline, ctx.workload.samples, () => + read(ctx, 'agentSession.conversationOutline', { sessionId }) + ) + + const [tip] = (await userItemIds(page(ctx), sessionId, 1)).slice(-1) + const fork = addRow(ctx, { + family: 'outline', + operation: 'tip fork (create forkFrom)', + variant: 'large chat tip', + context: { historicalSubmissions: ctx.workload.history.largeTurns }, + endToEndOnly: 'local validation and provider copy are one host span' + }) + if (!tip) { + fork.skipped = 'no user item on the large chat' + return + } + await sampled(ctx, fork, Math.min(ctx.workload.samples, 10), () => + measured(ctx, async () => { + const created = await createChat(page(ctx), ctx.arm, ctx.manifest.workspace.worktreeIds[0], { + sessionId, + throughItemId: tip + }) + const readyStarted = Date.now() + const first = await history(page(ctx), created.sessionId, 'tail', 40) + return { + hostMs: created.hostMs, + extra: { forkTailReadMs: Date.now() - readyStarted, forkTailItems: first.page.items.length } + } + }) + ) +} + +export async function recordRows(ctx: RunContext): Promise { + const { fillerSessionIds } = ctx.manifest + const chats = fillerSessionIds.length + 1 + const targets = [fillerSessionIds[0]!, fillerSessionIds.at(-1)!] + const rename = addRow(ctx, { + family: 'records', + operation: 'rename', + variant: 'point lookup, first and last chat', + context: { chats } + }) + await sampled(ctx, rename, ctx.workload.samples, (index) => + measured(ctx, async () => { + const name = tagFor('name') + const answer = await rpc(page(ctx), 'agentSession.rename', { + sessionId: targets[index % 2]!, + name + }) + const shownAt = await page(ctx) + .waitForFunction( + (name) => + [...document.querySelectorAll('[data-testid="sortable-tab"]')].some((tab) => + tab.textContent?.includes(name) + ) && performance.now(), + name, + { timeout: 60_000, polling: 16 } + ) + .then(async (handle) => (await handle.jsonValue()) as number) + .catch(() => Number.NaN) + return { + hostMs: answer.hostMs, + e2eMs: shownAt - answer.startedAt, + extra: { tabNotShown: Number(Number.isNaN(shownAt)) } + } + }) + ) + + const option = addRow(ctx, { + family: 'records', + operation: 'option update (setOption model)', + variant: 'idle small chat', + context: { chats } + }) + await sampled(ctx, option, ctx.workload.samples, () => + measured(ctx, async () => { + const answer = await mutate(page(ctx), ctx.arm, 'agentSession.setOption', targets[0]!, { + key: 'model', + value: 'default' + }) + await waitForIdle(page(ctx), targets[0]!) + return { hostMs: answer.hostMs } + }) + ) + + // Resuming a provider session another chat already adopted makes the host find that chat by provider ID. + const adopted = addRow(ctx, { + family: 'records', + operation: 'adopted-handle lookup (create resumeFrom)', + variant: 'provider id of last chat', + context: { chats } + }) + const provider = ( + await rpc<{ providerSession?: { id?: string } }>(page(ctx), 'agentSession.history', { + sessionId: targets[1]!, + direction: 'tail', + limit: 1 + }) + ).value.providerSession?.id + if (!provider) { + adopted.skipped = 'history named no provider session for the last chat' + return + } + await sampled(ctx, adopted, ctx.workload.samples, () => + measured(ctx, async () => { + const sessionId = ctx.arm.protocol.createStructuredAgentSessionId('claude', randomUUID) + const worktree = `id:${ctx.manifest.workspace.worktreeIds[1]}` + const resumeFrom = { providerSessionId: provider } + const fingerprint = ctx.arm.protocol.structuredAgentSessionPayloadFingerprint({ + method: 'agentSession.create', + sessionId, + fields: { worktree, agent: 'claude', resumeFrom } + }) + const envelope = { + sessionId, + clientOperationId: operationId(ctx.arm), + expectedRuntimeFence: null, + payloadFingerprint: fingerprint + } + const answer = await rpc( + page(ctx), + 'agentSession.create', + { envelope, worktree, agent: 'claude', resumeFrom }, + { allowRefusal: true } + ) + return { hostMs: answer.hostMs, extra: { refused: Number(answer.refused) } } + }) + ) +} diff --git a/config/scripts/performance/native-chat/native-chat-4a-ops-retained.ts b/config/scripts/performance/native-chat/native-chat-4a-ops-retained.ts new file mode 100644 index 00000000000..41d8b3fa1ce --- /dev/null +++ b/config/scripts/performance/native-chat/native-chat-4a-ops-retained.ts @@ -0,0 +1,102 @@ +// R3's retained-history rows: queued-send admission and resume after a Stop pause, live queue small, released history growing. +import { markerSeenAt, watchMarkers } from './native-chat-4a-probes' +import { + addRow, + measured, + page, + sampled, + tagFor, + type RunContext +} from './native-chat-4a-run-context' +import { + mutate, + openChatTab, + send, + waitForControls, + waitForIdle, + waitForWorking +} from './native-chat-4a-session' +import { holdPrompt, replyPrompt } from './native-chat-4a-workloads' +import { holdActive, queued, sentText } from './native-chat-4a-ops-queue' + +/** R3: grow released queue history in batches, then measure admission with `liveQueue` waiting at each checkpoint. */ +export async function retainedAdmissionRows(ctx: RunContext): Promise { + const { workspace, fillerSessionIds, largeSessionId } = ctx.manifest + const chat = fillerSessionIds[2] ?? fillerSessionIds[0]! + const { checkpoints, liveQueue } = ctx.workload.retained + const targets: { sessionId: string; worktreeId: string; released: number | 'seeded' }[] = [ + { sessionId: largeSessionId, worktreeId: workspace.worktreeIds[0], released: 'seeded' }, + ...checkpoints.map((released) => ({ + sessionId: chat, + worktreeId: workspace.worktreeIds[1], + released + })) + ] + let released = 0 + for (const target of targets) { + if (typeof target.released === 'number') { + while (released < target.released) { + const batch = Math.min(10, target.released - released) + await send(page(ctx), ctx.arm, chat, holdPrompt(tagFor('grow'), 100)) + for (let index = 0; index < batch; index += 1) { + await send(page(ctx), ctx.arm, chat, replyPrompt(tagFor('released'))) + } + await waitForIdle(page(ctx), chat, 300_000) + released += batch + } + } + await openChatTab(page(ctx), target.worktreeId, target.sessionId) + await waitForControls(page(ctx)) + const context = { + releasedHistory: String(target.released), + liveQueue, + chat: target.sessionId === largeSessionId ? 'large (seeded queue history)' : 'small' + } + const admit = addRow(ctx, { + family: 'retained history', + operation: 'queued-send admission', + variant: `released=${target.released}`, + context + }) + const resume = addRow(ctx, { + family: 'retained history', + operation: 'resume after Stop pause', + variant: `released=${target.released}`, + context + }) + await sampled(ctx, admit, ctx.workload.samples, async () => { + await holdActive(ctx, target.sessionId, 60_000) + for (let index = 0; index < liveQueue - 1; index += 1) { + await send(page(ctx), ctx.arm, target.sessionId, replyPrompt(tagFor('live'))) + } + const tag = tagFor('admit') + await watchMarkers(page(ctx), [`4A-SENT-${tag}`]) + const sample = await measured(ctx, async () => { + const answer = await send(page(ctx), ctx.arm, target.sessionId, sentText(tag)) + return { + hostMs: answer.hostMs, + e2eMs: (await markerSeenAt(page(ctx), `4A-SENT-${tag}`)) - answer.startedAt + } + }) + await mutate(page(ctx), ctx.arm, 'agentSession.cancel', target.sessionId, {}) + await waitForWorking(page(ctx), false) + const last = (await queued(ctx, target.sessionId)).length + resume.samples.push( + await measured(ctx, async () => ({ + hostMs: ( + await mutate( + page(ctx), + ctx.arm, + 'agentSession.queuedMessagesResume', + target.sessionId, + {} + ) + ).hostMs, + extra: { pausedQueueLength: last } + })) + ) + await waitForIdle(page(ctx), target.sessionId, 300_000) + return sample + }) + } +} diff --git a/config/scripts/performance/native-chat/native-chat-4a-ops-stream.ts b/config/scripts/performance/native-chat/native-chat-4a-ops-stream.ts new file mode 100644 index 00000000000..e472400b08d --- /dev/null +++ b/config/scripts/performance/native-chat/native-chat-4a-ops-stream.ts @@ -0,0 +1,210 @@ +// 4a streaming rows (1/8/15+ MiB text, no-break and tool output) with a competing send, and the maintenance window after them. +import { + eventLoopLag, + markerSeenAt, + mutationGaps, + rpc, + samplePressure, + storageBytes, + watchMarkers +} from './native-chat-4a-probes' +import { stat, type Sample } from './native-chat-4a-report' +import { + addRow, + measured, + page, + sampled, + tagFor, + type RunContext +} from './native-chat-4a-run-context' +import { + history, + mutate, + openChatTab, + send, + waitForControls, + waitForDurable, + waitForIdle, + waitForWorking +} from './native-chat-4a-session' +import { replyPrompt, streamPrompt, type StreamCase } from './native-chat-4a-workloads' + +const MAINTENANCE_WINDOW_MS = 5_000 + +/** Wall-clock ms until the durable tail holds `marker`, polling slowly so the reader adds little load to either arm. */ +async function durableAfter( + ctx: RunContext, + sessionId: string, + marker: string, + sentEpochMs: number +): Promise { + await waitForDurable(page(ctx), sessionId, marker, 300_000, 250) + return Date.now() - sentEpochMs +} + +/** A small send to another chat while the stream runs: its acceptance span and when its reply is durable. */ +async function competingSend( + ctx: RunContext, + sessionId: string +): Promise<{ hostMs: number; durableMs: number }> { + const tag = tagFor('compete') + const sentAt = Date.now() + const answer = await send(page(ctx), ctx.arm, sessionId, replyPrompt(tag)) + await waitForDurable(page(ctx), sessionId, `4A-TAIL-${tag}`, 300_000, 25) + return { hostMs: answer.hostMs, durableMs: Date.now() - sentAt } +} + +async function streamSample( + ctx: RunContext, + shape: StreamCase, + stopAfterFirst: boolean +): Promise { + const { largeSessionId, fillerSessionIds } = ctx.manifest + const { launch } = ctx + // Earlier samples stay in the transcript, so every sample's markers are its own. + const stream = { ...shape, tag: tagFor(shape.tag) } + const other = fillerSessionIds[1] ?? fillerSessionIds[0]! + const before = storageBytes(launch.userDataDir) + const stopPressure = samplePressure(launch.app, launch.userDataDir) + const fromEpoch = Date.now() + const sample = await measured(ctx, async () => { + const first = `4A-FIRST-${stream.tag}` + await watchMarkers(page(ctx), [first]) + const sentEpoch = Date.now() + const answer = await send( + page(ctx), + ctx.arm, + largeSessionId, + streamPrompt(ctx.workload, stream) + ) + const firstAt = await markerSeenAt(page(ctx), first, 300_000) + const [competing, durableFirstMs] = await Promise.all([ + competingSend(ctx, other), + durableAfter(ctx, largeSessionId, first, sentEpoch) + ]) + let stopHostMs = Number.NaN + if (stopAfterFirst) { + stopHostMs = (await mutate(page(ctx), ctx.arm, 'agentSession.cancel', largeSessionId, {})) + .hostMs + } + const doneAt = await waitForWorking(page(ctx), false, 600_000) + const settled = await waitForIdle(page(ctx), largeSessionId, 600_000) + const durableEndMs = Date.now() - sentEpoch + const gaps = stat(await mutationGaps(page(ctx), firstAt, doneAt)) + return { + hostMs: answer.hostMs, + e2eMs: doneAt - answer.startedAt, + extra: { + firstTextMs: firstAt - answer.startedAt, + liveGapP95Ms: gaps?.p95 ?? Number.NaN, + liveGapMaxMs: gaps?.max ?? Number.NaN, + durableFirstMs, + durableSettledMs: durableEndMs, + competingHostMs: competing.hostMs, + competingDurableMs: competing.durableMs, + stopHostMs, + tailItems: settled.items.length + } + } + }) + const pressure = await stopPressure() + const after = storageBytes(launch.userDataDir) + const lag = eventLoopLag(launch.userDataDir, fromEpoch, Date.now()) + return { + ...sample, + extra: { + ...sample.extra, + walGrowthBytes: after.wal - before.wal, + fileGrowthBytes: after.total - before.total, + peakWalBytes: pressure.peakWalBytes, + peakMainRssBytes: pressure.peakMainRssBytes, + peakAppWorkingSetBytes: pressure.peakAppWorkingSetBytes, + eventLoopP99Ms: lag.p99Ms, + eventLoopMaxMs: lag.maxMs + } + } +} + +/** After a large stream, sends to another chat while retired payloads are pruned, pages reclaimed and the WAL checkpointed. */ +async function maintenanceWindow(ctx: RunContext): Promise { + const other = ctx.manifest.fillerSessionIds[1] ?? ctx.manifest.fillerSessionIds[0]! + const fromEpoch = Date.now() + const samples: Sample[] = [] + while (Date.now() - fromEpoch < MAINTENANCE_WINDOW_MS) { + const before = storageBytes(ctx.launch.userDataDir) + const sendAnswer = await send(page(ctx), ctx.arm, other, replyPrompt(tagFor('maint'))) + const read = await history(page(ctx), ctx.manifest.largeSessionId, 'tail', 40) + const after = storageBytes(ctx.launch.userDataDir) + samples.push({ + hostMs: sendAnswer.hostMs, + extra: { + largeTailReadMs: read.hostMs, + sinceStreamEndMs: Date.now() - fromEpoch, + walChangeBytes: after.wal - before.wal, + fileChangeBytes: after.total - before.total + } + }) + await new Promise((resolve) => setTimeout(resolve, 200)) + } + await waitForIdle(page(ctx), other) + const lag = eventLoopLag(ctx.launch.userDataDir, fromEpoch, Date.now()) + samples.at(-1)!.extra = { + ...samples.at(-1)!.extra, + windowEventLoopP99Ms: lag.p99Ms, + windowEventLoopMaxMs: lag.maxMs + } + return samples +} + +export async function streamingRows(ctx: RunContext): Promise { + const { workspace, largeSessionId, largeItems } = ctx.manifest + await openChatTab(page(ctx), workspace.worktreeIds[0], largeSessionId) + await waitForControls(page(ctx)) + const samples = ctx.workload.stream.samples + for (const stream of ctx.workload.stream.cases) { + const shape = stream.toolOutputBytes + ? 'tool output running→completed' + : stream.shape === 'nobreak' + ? 'no-break block' + : 'block-rich text' + const size = `${((stream.toolOutputBytes ?? stream.bytes) / 1024 / 1024).toFixed(2)} MiB` + const context = { + encodedBytes: stream.toolOutputBytes ?? stream.bytes, + chunkBytes: ctx.workload.stream.chunkBytes, + largeItems, + concurrent: 'small send in another chat after first text' + } + const row = addRow(ctx, { family: 'streaming', operation: shape, variant: size, context }) + await sampled(ctx, row, samples, () => streamSample(ctx, stream, false)) + if (!stream.toolOutputBytes) { + const stop = addRow(ctx, { + family: 'streaming', + operation: `${shape}, Stop after first text`, + variant: size, + context + }) + await sampled(ctx, stop, samples, () => streamSample(ctx, stream, true)) + } + const maintenance = addRow(ctx, { + family: 'maintenance', + operation: 'other-chat sends and large-chat reads after stream', + variant: size, + context: { + windowMs: MAINTENANCE_WINDOW_MS, + retiredPayloadBytes: stream.toolOutputBytes ?? stream.bytes + } + }) + try { + maintenance.samples.push(...(await maintenanceWindow(ctx))) + } catch (error) { + maintenance.skipped = + `failed: ${error instanceof Error ? error.message : String(error)}`.slice(0, 300) + } + } + // A cheap read after all streams confirms the large chat still answers its tail. + await rpc(page(ctx), 'agentSession.history', { + sessionId: largeSessionId, + direction: 'tail', + limit: 1 + }) +} diff --git a/config/scripts/performance/native-chat/native-chat-4a-probes.ts b/config/scripts/performance/native-chat/native-chat-4a-probes.ts new file mode 100644 index 00000000000..03b4550bf97 --- /dev/null +++ b/config/scripts/performance/native-chat/native-chat-4a-probes.ts @@ -0,0 +1,231 @@ +// Arm-neutral 4a probes: renderer-clock end points, host CPU/event-loop work, memory and on-disk storage, read from outside product code. +import { readdirSync, statSync } from 'node:fs' +import path from 'node:path' +import type { Performance as NodePerformance } from 'node:perf_hooks' +import type { ElectronApplication, Page } from '@stablyai/playwright-test' +import { summarizeEventLoopDelay } from '../../../../tests/e2e/helpers/event-loop-delay-report' + +export type RpcAnswer = { ok: true; result: T } | { ok: false; error: unknown } + +/** One RPC from the renderer, timed on the renderer clock: the host span every row reports beside its visible end point. */ +export async function rpc( + page: Page, + method: string, + params: unknown, + options: { allowRefusal?: true } = {} +): Promise<{ value: T; refused: boolean; startedAt: number; hostMs: number }> { + const answer = await page.evaluate( + async ({ method, params }) => { + const startedAt = performance.now() + const reply = (await window.api.runtime.call({ method, params })) as RpcAnswer + return { reply, startedAt, hostMs: performance.now() - startedAt } + }, + { method, params } + ) + if (!answer.reply.ok && !options.allowRefusal) { + throw new Error(`${method} refused: ${JSON.stringify(answer.reply.error).slice(0, 400)}`) + } + const value = (answer.reply.ok ? answer.reply.result : answer.reply.error) as T + return { value, refused: !answer.reply.ok, startedAt: answer.startedAt, hostMs: answer.hostMs } +} + +/** Install once per page: records when text first appears in the chat root and every transcript mutation batch. */ +export async function installVisibilityProbe(page: Page): Promise { + await page.evaluate(() => { + const probe = window as unknown as { + __4a?: { seen: Map; mutations: number[]; watch: Set } + } + if (probe.__4a) { + return + } + const state = { + seen: new Map(), + mutations: [] as number[], + watch: new Set() + } + probe.__4a = state + const check = (): void => { + const now = performance.now() + state.mutations.push(now) + if (state.mutations.length > 200_000) { + state.mutations.splice(0, 100_000) + } + if (state.watch.size === 0) { + return + } + // Text still in the composer is not sent: count only occurrences outside it. + // Hidden tabs can stay mounted; only the chat the user sees counts. + const root = [...document.querySelectorAll('[data-native-chat-root="true"]')].find( + (candidate) => candidate.checkVisibility() + ) + const text = root?.textContent ?? '' + const draft = root?.querySelector('[role="textbox"]')?.textContent ?? '' + const count = (value: string, marker: string): number => value.split(marker).length - 1 + for (const marker of state.watch) { + if (count(text, marker) > count(draft, marker)) { + state.seen.set(marker, now) + state.watch.delete(marker) + } + } + } + new MutationObserver(check).observe(document.body, { + subtree: true, + childList: true, + characterData: true + }) + }) +} + +export async function watchMarkers(page: Page, markers: string[]): Promise { + await page.evaluate((markers) => { + const state = (window as unknown as { __4a: { watch: Set; seen: Map } }) + .__4a + for (const marker of markers) { + state.seen.delete(marker) + state.watch.add(marker) + } + }, markers) +} + +/** Renderer-clock time a marker became visible, waiting up to `timeoutMs`. */ +export async function markerSeenAt( + page: Page, + marker: string, + timeoutMs = 120_000 +): Promise { + const handle = await page.waitForFunction( + (marker) => + (window as unknown as { __4a: { seen: Map } }).__4a.seen.get(marker) ?? false, + marker, + { timeout: timeoutMs, polling: 16 } + ) + return (await handle.jsonValue()) as number +} + +/** Gaps between visible transcript updates inside [from, to], the live-update cadence a reader sees. */ +export async function mutationGaps(page: Page, from: number, to: number): Promise { + return page.evaluate( + ({ from, to }) => { + const times = (window as unknown as { __4a: { mutations: number[] } }).__4a.mutations.filter( + (t) => t >= from && t <= to + ) + return times.slice(1).map((time, index) => time - times[index]!) + }, + { from, to } + ) +} + +export const rendererNow = (page: Page): Promise => page.evaluate(() => performance.now()) + +export type HostWork = { cpuMs: number; mainThreadActiveMs: number; offMainThreadCpuMs: number } + +/** Process CPU covers the main thread and its storage workers; main-thread ELU separates the two. */ +export async function hostWorkMark(app: ElectronApplication): Promise<() => Promise> { + // Main-process callbacks see Node's performance, not the DOM one this file is typed against. + const start = await app.evaluate(() => ({ + cpu: process.cpuUsage(), + elu: (performance as unknown as NodePerformance).eventLoopUtilization() + })) + return async () => { + const delta = await app.evaluate((_electron, start) => { + const node = performance as unknown as NodePerformance + return { + cpu: process.cpuUsage(start.cpu), + elu: node.eventLoopUtilization(node.eventLoopUtilization(), start.elu) + } + }, start) + const cpuMs = (delta.cpu.user + delta.cpu.system) / 1000 + return { + cpuMs, + mainThreadActiveMs: delta.elu.active, + offMainThreadCpuMs: Math.max(0, cpuMs - delta.elu.active) + } + } +} + +/** All SQLite/journal files under the profile, so WAL growth and file growth read the same on both storage designs. */ +export function storageBytes(userDataDir: string): { db: number; wal: number; total: number } { + let db = 0 + let wal = 0 + let total = 0 + const visit = (dir: string): void => { + for (const entry of readdirSync(dir, { withFileTypes: true })) { + const file = path.join(dir, entry.name) + if (entry.isDirectory()) { + if (entry.name !== 'home') { + visit(file) + } + continue + } + const size = statSync(file, { throwIfNoEntry: false })?.size ?? 0 + total += size + if (entry.name.endsWith('-wal')) { + wal += size + } else if (/\.(db|sqlite)$/.test(entry.name)) { + db += size + } + } + } + visit(userDataDir) + return { db, wal, total } +} + +export type Pressure = { + peakMainRssBytes: number + peakAppWorkingSetBytes: number + peakWalBytes: number + peakTotalBytes: number +} + +/** Sample memory and storage every `everyMs` until stopped; peaks only, so sampling cost stays flat. */ +export function samplePressure( + app: ElectronApplication, + userDataDir: string, + everyMs = 250 +): () => Promise { + const peak: Pressure = { + peakMainRssBytes: 0, + peakAppWorkingSetBytes: 0, + peakWalBytes: 0, + peakTotalBytes: 0 + } + let stopped = false + const loop = (async () => { + while (!stopped) { + const memory = await app + .evaluate(({ app: electronApp }) => ({ + rss: process.memoryUsage().rss, + workingSetKb: electronApp + .getAppMetrics() + .reduce((sum, metric) => sum + metric.memory.workingSetSize, 0) + })) + .catch(() => null) + if (memory) { + peak.peakMainRssBytes = Math.max(peak.peakMainRssBytes, memory.rss) + peak.peakAppWorkingSetBytes = Math.max( + peak.peakAppWorkingSetBytes, + memory.workingSetKb * 1024 + ) + } + const disk = storageBytes(userDataDir) + peak.peakWalBytes = Math.max(peak.peakWalBytes, disk.wal) + peak.peakTotalBytes = Math.max(peak.peakTotalBytes, disk.total) + await new Promise((resolve) => setTimeout(resolve, everyMs)) + } + })() + return async () => { + stopped = true + await loop + return peak + } +} + +/** Worst main-process event-loop window overlapping [fromEpochMs, toEpochMs], from the probe both arms already ship. */ +export function eventLoopLag( + userDataDir: string, + fromEpochMs: number, + toEpochMs: number +): { p99Ms: number; maxMs: number; windows: number } { + const summary = summarizeEventLoopDelay(userDataDir, 'main', fromEpochMs, toEpochMs) + return { p99Ms: summary.p99, maxMs: summary.max, windows: summary.windows } +} diff --git a/config/scripts/performance/native-chat/native-chat-4a-provider-stub.cjs b/config/scripts/performance/native-chat/native-chat-4a-provider-stub.cjs new file mode 100644 index 00000000000..8155ded9d94 --- /dev/null +++ b/config/scripts/performance/native-chat/native-chat-4a-provider-stub.cjs @@ -0,0 +1,310 @@ +// Claude stream-json stand-in for the 4a comparison; wrappers pass the launch log first. Speaks the same initialize/turn/interrupt +// protocol as tests/e2e/fixtures/structured-claude-stub.cjs and runs the prompt's [[4a {...}]] directive (see native-chat-4a-provider-text.cjs). +const { randomUUID } = require('node:crypto') +const fs = require('node:fs') +const os = require('node:os') +const path = require('node:path') +const { parseDirective, prose, streamText } = require('./native-chat-4a-provider-text.cjs') + +const [launchLog, ...args] = process.argv.slice(2) +fs.appendFileSync(launchLog, `${JSON.stringify({ pid: process.pid, args })}\n`) + +if (args.includes('--version') || args.includes('-v')) { + process.stdout.write('2.1.280 (Claude Code)\n') + process.exit(0) +} + +function argValue(name) { + const joined = args.find((arg) => arg.startsWith(`${name}=`)) + if (joined) { + return joined.slice(name.length + 1) + } + const index = args.indexOf(name) + return index !== -1 ? args[index + 1] : undefined +} + +const sessionId = argValue('--session-id') ?? argValue('--resume') ?? randomUUID() +const claudeHome = process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), '.claude') +const transcriptPath = path.join( + claudeHome, + 'projects', + process.cwd().replace(/[^a-zA-Z0-9]/g, '-'), + `${sessionId}.jsonl` +) +fs.mkdirSync(path.dirname(transcriptPath), { recursive: true }) +let parentUuid = null +if (fs.existsSync(transcriptPath)) { + const lines = fs.readFileSync(transcriptPath, 'utf8').trim().split('\n').filter(Boolean) + parentUuid = lines.length > 0 ? (JSON.parse(lines.at(-1)).uuid ?? null) : null +} + +// Fork and rewind read the provider's own transcript, so every user/assistant frame is also saved there. +function remember(frame) { + const row = { + parentUuid, + isSidechain: false, + type: frame.type, + message: frame.message, + uuid: frame.uuid, + timestamp: new Date().toISOString(), + sessionId, + cwd: process.cwd() + } + fs.appendFileSync(transcriptPath, `${JSON.stringify(row)}\n`) + parentUuid = frame.uuid +} + +let backlog = Promise.resolve() +function write(frame) { + const line = `${JSON.stringify(frame)}\n` + // Respect stdout backpressure so a 15 MiB stream measures the host, not an unbounded pipe buffer. + backlog = backlog.then( + () => + new Promise((resolve) => + process.stdout.write(line) ? resolve() : process.stdout.once('drain', resolve) + ) + ) + return backlog +} + +function framed(type, message, extra = {}) { + const frame = { + type, + uuid: randomUUID(), + session_id: sessionId, + parent_tool_use_id: null, + message, + ...extra + } + remember(frame) + return write(frame) +} + +const assistant = (content) => + framed('assistant', { id: randomUUID(), role: 'assistant', model: 'claude-fake', content }) +const streamEvent = (event) => + write({ + type: 'stream_event', + event, + session_id: sessionId, + parent_tool_use_id: null, + uuid: randomUUID() + }) +const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)) + +let current = null +const pending = [] +let turnCounter = 0 + +async function toolRun(turn, index, outputBytes, runMs) { + const id = `toolu_4a_${turn}_${index}` + await assistant([ + { type: 'tool_use', id, name: 'Bash', input: { command: `ls -la dir${index}` } } + ]) + if (runMs > 0) { + await write({ + type: 'system', + subtype: 'task_started', + task_id: `task-${turn}-${index}`, + tool_use_id: id, + description: 'run', + task_type: 'local_bash', + uuid: randomUUID(), + session_id: sessionId + }) + await sleep(runMs) + } + await framed('user', { + role: 'user', + content: [ + { tool_use_id: id, type: 'tool_result', content: prose(outputBytes, id), is_error: false } + ] + }) +} + +async function streamReply(op, signal) { + const text = streamText(op) + const messageId = randomUUID() + await streamEvent({ + type: 'message_start', + message: { + id: messageId, + type: 'message', + role: 'assistant', + model: 'claude-fake', + content: [] + } + }) + await streamEvent({ + type: 'content_block_start', + index: 0, + content_block: { type: 'text', text: '' } + }) + const chunk = op.chunkBytes ?? 16_384 + for (let offset = 0; offset < text.length && !signal.aborted; offset += chunk) { + await streamEvent({ + type: 'content_block_delta', + index: 0, + delta: { type: 'text_delta', text: text.slice(offset, offset + chunk) } + }) + if (op.intervalMs) { + await sleep(op.intervalMs) + } + } + if (signal.aborted) { + return + } + await framed('assistant', { + id: messageId, + role: 'assistant', + model: 'claude-fake', + content: [{ type: 'text', text }] + }) + await streamEvent({ type: 'content_block_stop', index: 0 }) + await streamEvent({ type: 'message_stop' }) +} + +async function runOp(op, signal) { + const turn = (turnCounter += 1) + const tag = op.tag ?? `t${turn}` + if (op.op === 'history') { + for (let index = 1; index <= (op.tools ?? 8) && !signal.aborted; index += 1) { + await toolRun(turn, index, op.toolOutputBytes ?? 2000, 0) + } + await assistant([{ type: 'text', text: `4A-TAIL-${tag} ${prose(op.replyBytes ?? 1500, tag)}` }]) + return + } + if (op.op === 'stream') { + if (op.toolOutputBytes) { + await toolRun(turn, 0, op.toolOutputBytes, op.toolRunMs ?? 250) + } + await streamReply({ ...op, tag }, signal) + return + } + if (op.op === 'hold') { + const deadline = Date.now() + (op.ms ?? 60_000) + while (!signal.aborted && Date.now() < deadline) { + await sleep(10) + } + } + if (!signal.aborted) { + await assistant([{ type: 'text', text: `4A-TAIL-${tag} ${op.text ?? 'Stub reply.'}` }]) + } +} + +async function startTurn(frame) { + const controller = new AbortController() + current = controller + // The CLI's --replay-user-messages echo, which opens the turn. + remember({ type: 'user', uuid: frame.uuid ?? randomUUID(), message: frame.message }) + await write({ ...frame, isReplay: true }) + const content = frame.message?.content ?? frame.message + const prompt = Array.isArray(content) + ? content.map((block) => block?.text ?? '').join('\n') + : content + const op = parseDirective(prompt) ?? { op: 'reply' } + await runOp(op, controller.signal) + if (controller.signal.aborted) { + return + } + await write({ + type: 'result', + subtype: 'success', + uuid: randomUUID(), + session_id: sessionId, + is_error: false, + terminal_reason: 'completed', + result: 'done' + }) + current = null + const next = pending.shift() + if (next) { + void startTurn(next) + } +} + +function interruptTurn() { + if (!current) { + return + } + current.abort() + current = null + void write({ + type: 'result', + subtype: 'error_during_execution', + is_error: true, + session_id: sessionId, + parent_tool_use_id: null + }) + const next = pending.shift() + if (next) { + void startTurn(next) + } +} + +const initializeResponse = { + commands: [], + models: [{ value: 'default', displayName: 'Default', description: '' }], + account: {}, + output_style: 'default', + available_output_styles: ['default'] +} + +let buffer = '' +let initSent = false +process.stdin.setEncoding('utf8') +process.stdin.on('data', (chunk) => { + buffer += String(chunk) + let index + while ((index = buffer.indexOf('\n')) !== -1) { + const line = buffer.slice(0, index).trim() + buffer = buffer.slice(index + 1) + let frame + try { + frame = line ? JSON.parse(line) : null + } catch { + frame = null + } + if (frame?.type === 'user') { + if (current) { + pending.push(frame) + } else { + void startTurn(frame) + } + continue + } + if (frame?.type !== 'control_request') { + continue + } + const subtype = frame.request?.subtype + if (subtype === 'interrupt') { + interruptTurn() + } + void write({ + type: 'control_response', + response: { + subtype: 'success', + request_id: frame.request_id, + response: subtype === 'initialize' ? initializeResponse : {} + } + }) + if (subtype === 'initialize' && !initSent) { + initSent = true + void write({ + type: 'system', + subtype: 'init', + session_id: sessionId, + uuid: randomUUID(), + model: 'claude-fake', + cwd: process.cwd(), + tools: [], + mcp_servers: [], + permissionMode: 'default', + slash_commands: [], + apiKeySource: 'none', + output_style: 'default' + }) + } + } +}) +process.stdin.on('end', () => process.exit(0)) diff --git a/config/scripts/performance/native-chat/native-chat-4a-provider-text.cjs b/config/scripts/performance/native-chat/native-chat-4a-provider-text.cjs new file mode 100644 index 00000000000..ba74f8cdac1 --- /dev/null +++ b/config/scripts/performance/native-chat/native-chat-4a-provider-text.cjs @@ -0,0 +1,78 @@ +// Deterministic provider text and the prompt directive shared by the 4a stub and its driver, so both arms see identical bytes. +const { createHash } = require('node:crypto') + +const DIRECTIVE = /\[\[4a (\{[^\]]*\})\]\]/ + +/** A prompt that tells the stub what to emit; padding makes large prompt bodies without changing the op. */ +function formatDirective(op, paddingBytes = 0) { + const head = `[[4a ${JSON.stringify(op)}]]` + return paddingBytes > 0 ? `${head}\n${prose(paddingBytes, op.tag ?? 'pad')}` : head +} + +function parseDirective(text) { + const match = DIRECTIVE.exec(typeof text === 'string' ? text : JSON.stringify(text ?? '')) + if (!match) { + return null + } + try { + return JSON.parse(match[1]) + } catch { + return null + } +} + +function seededWords(seed) { + let state = 0 + for (const char of String(seed)) { + state = (Math.imul(state, 31) + char.charCodeAt(0)) | 0 + } + return () => { + state = (state + 0x6d2b79f5) | 0 + let t = Math.imul(state ^ (state >>> 15), 1 | state) + t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t + return ((t ^ (t >>> 14)) >>> 0) / 4294967296 + } +} + +const WORDS = ['journal', 'stream', 'outline', 'queue', 'render', 'commit', 'reader', 'writer'] + +/** Block-rich markdown: headings, lists, fenced code and prose, cut to exactly `bytes` ASCII bytes. */ +function prose(bytes, seed) { + const next = seededWords(seed) + const parts = [] + let length = 0 + for (let block = 0; length < bytes; block += 1) { + const words = Array.from({ length: 12 }, () => WORDS[Math.floor(next() * WORDS.length)]) + const shape = block % 4 + const text = + shape === 0 + ? `## Section ${block}\n\n${words.join(' ')}.\n\n` + : shape === 1 + ? `${words.map((word) => `- **${word}** item`).join('\n')}\n\n` + : shape === 2 + ? `\`\`\`ts\nconst ${words[0]}${block} = '${words.slice(1).join('-')}'\n\`\`\`\n\n` + : `${words.join(' ')} with \`${words[1]}\` and a [link](https://example.invalid/${block}).\n\n` + parts.push(text) + length += text.length + } + return parts.join('').slice(0, bytes) +} + +/** One unbroken line with no whitespace, the shape that defeats line-based chunking. */ +function noBreak(bytes, seed) { + const unit = createHash('sha256').update(String(seed)).digest('hex') + return unit.repeat(Math.ceil(bytes / unit.length)).slice(0, bytes) +} + +function streamText(op) { + const body = op.shape === 'nobreak' ? noBreak : prose + const head = `4A-FIRST-${op.tag} ` + const tail = ` 4A-END-${op.tag}` + return `${head}${body(Math.max(0, op.bytes - head.length - tail.length), op.tag)}${tail}` +} + +function digest(text) { + return createHash('sha256').update(text).digest('hex').slice(0, 16) +} + +module.exports = { formatDirective, parseDirective, prose, noBreak, streamText, digest } diff --git a/config/scripts/performance/native-chat/native-chat-4a-report.ts b/config/scripts/performance/native-chat/native-chat-4a-report.ts new file mode 100644 index 00000000000..44021dacc5c --- /dev/null +++ b/config/scripts/performance/native-chat/native-chat-4a-report.ts @@ -0,0 +1,152 @@ +// 4a rows, percentiles and the PR-body table: main vs candidate per row, keyed by each arm's head SHA. +import type { HostWork } from './native-chat-4a-probes' + +export type Sample = { + /** Action to user-visible (or durably readable, when the row says so) result. */ + e2eMs?: number + /** The RPC's renderer round trip: host handling including storage. */ + hostMs?: number + work?: HostWork + /** Row-specific spans and sizes, e.g. firstTextMs, walBytes, bytes, items, queueLength. */ + extra?: Record +} + +export type Row = { + family: string + operation: string + /** e.g. cold, warm, 8MiB nobreak; rows match across arms by family/operation/variant. */ + variant: string + samples: Sample[] + /** Shape facts that hold for the whole row: bytes, items, queue length, concurrent work. */ + context: Record + /** Why a row has no samples on this arm, so absence is never read as a pass. */ + skipped?: string + /** Sub-spans this row cannot separate without a product hook; the table marks it "end-to-end only". */ + endToEndOnly?: string +} + +export type ArmResult = { + arm: string + sha: string + startedAt: string + finishedAt: string + rows: Row[] + fixture: Record +} + +export type Stat = { n: number; p50: number; p95: number; max: number } + +function nearestRank(sorted: number[], quantile: number): number { + return sorted[Math.min(sorted.length - 1, Math.max(0, Math.ceil(quantile * sorted.length) - 1))]! +} + +export function stat(values: (number | undefined)[]): Stat | null { + const sorted = values + .filter((value): value is number => typeof value === 'number' && Number.isFinite(value)) + .sort((a, b) => a - b) + if (sorted.length === 0) { + return null + } + return { + n: sorted.length, + p50: nearestRank(sorted, 0.5), + p95: nearestRank(sorted, 0.95), + max: sorted.at(-1)! + } +} + +export function summarizeRow(row: Row) { + const extras = new Set(row.samples.flatMap((sample) => Object.keys(sample.extra ?? {}))) + return { + e2eMs: stat(row.samples.map((sample) => sample.e2eMs)), + hostMs: stat(row.samples.map((sample) => sample.hostMs)), + hostCpuMs: stat(row.samples.map((sample) => sample.work?.cpuMs)), + offMainThreadCpuMs: stat(row.samples.map((sample) => sample.work?.offMainThreadCpuMs)), + mainThreadActiveMs: stat(row.samples.map((sample) => sample.work?.mainThreadActiveMs)), + extra: Object.fromEntries( + [...extras].map((key) => [key, stat(row.samples.map((sample) => sample.extra?.[key]))]) + ) + } +} + +const key = (row: Pick): string => + `${row.family}\u0000${row.operation}\u0000${row.variant}` +const ms = (value: number): string => + value >= 100 ? value.toFixed(0) : value >= 10 ? value.toFixed(1) : value.toFixed(2) +const cell = (value: Stat | null): string => + value ? `${ms(value.p50)} / ${ms(value.p95)} / ${ms(value.max)} (n=${value.n})` : '—' + +function delta(base: Stat | null, next: Stat | null): string { + if (!base || !next) { + return '—' + } + const change = next.p95 - base.p95 + const percent = + base.p95 > 0 ? ` (${change >= 0 ? '+' : ''}${((change / base.p95) * 100).toFixed(0)}%)` : '' + return `${change >= 0 ? '+' : ''}${ms(change)}${percent}` +} + +const caseCell = (row: Row): string => + row.endToEndOnly ? `${row.variant} (end-to-end only: ${row.endToEndOnly})` : row.variant + +/** The row's primary latency: visible result where measured, else the host span. */ +const primary = (summary: ReturnType): Stat | null => + summary.e2eMs ?? summary.hostMs + +function contextCell(row: Row | undefined): string { + if (!row) { + return '' + } + return Object.entries(row.context) + .map(([name, value]) => `${name}=${value}`) + .join(', ') +} + +export function comparisonMarkdown(base: ArmResult, next: ArmResult): string { + const nextRows = new Map(next.rows.map((row) => [key(row), row])) + const baseRows = new Map(base.rows.map((row) => [key(row), row])) + const ordered = [...base.rows, ...next.rows.filter((row) => !baseRows.has(key(row)))] + const lines = [ + `4a comparison: ${base.arm} \`${base.sha.slice(0, 11)}\` vs ${next.arm} \`${next.sha.slice(0, 11)}\` (p50 / p95 / max ms, nearest rank).`, + '', + `| Family | Operation | Case | ${base.arm} | ${next.arm} | Δ p95 | Host span p95 ${base.arm} → ${next.arm} | Off-main CPU p95 ${base.arm} → ${next.arm} | Shape |`, + '| --- | --- | --- | --- | --- | --- | --- | --- | --- |' + ] + const seen = new Set() + for (const row of ordered) { + if (seen.has(key(row))) { + continue + } + seen.add(key(row)) + const left = baseRows.get(key(row)) + const right = nextRows.get(key(row)) + const a = left ? summarizeRow(left) : null + const b = right ? summarizeRow(right) : null + const span = (summary: typeof a, pick: 'hostMs' | 'offMainThreadCpuMs'): string => + summary?.[pick] ? ms(summary[pick]!.p95) : '—' + lines.push( + `| ${row.family} | ${row.operation} | ${caseCell(row)} | ${left?.skipped ? `skipped: ${left.skipped}` : cell(a && primary(a))} | ${right?.skipped ? `skipped: ${right.skipped}` : cell(b && primary(b))} | ${delta(a && primary(a), b && primary(b))} | ${span(a, 'hostMs')} → ${span(b, 'hostMs')} | ${span(a, 'offMainThreadCpuMs')} → ${span(b, 'offMainThreadCpuMs')} | ${contextCell(left ?? right)} |` + ) + } + lines.push( + '', + 'Primary latency is action → visible result where the row has a rendered end point, else the RPC host span. Off-main CPU is process CPU minus main-thread event-loop active time (storage workers). Conversion rows are one-time import cost, reported separately from steady state; per-row spans, WAL/file growth, event-loop lag and peak memory are in the JSON.' + ) + return `${lines.join('\n')}\n` +} + +export function armMarkdown(result: ArmResult): string { + const lines = [ + `4a ${result.arm} \`${result.sha.slice(0, 11)}\` (p50 / p95 / max ms).`, + '', + '| Family | Operation | Case | Primary | Host span | Shape |', + '| --- | --- | --- | --- | --- | --- |' + ] + for (const row of result.rows) { + const summary = summarizeRow(row) + lines.push( + `| ${row.family} | ${row.operation} | ${caseCell(row)} | ${row.skipped ? `skipped: ${row.skipped}` : cell(primary(summary))} | ${cell(summary.hostMs)} | ${contextCell(row)} |` + ) + } + return `${lines.join('\n')}\n` +} diff --git a/config/scripts/performance/native-chat/native-chat-4a-run-context.ts b/config/scripts/performance/native-chat/native-chat-4a-run-context.ts new file mode 100644 index 00000000000..999f11a2003 --- /dev/null +++ b/config/scripts/performance/native-chat/native-chat-4a-run-context.ts @@ -0,0 +1,66 @@ +// What every 4a operation family receives, plus the one helper that turns a timed action into a row sample. +import type { Page } from '@stablyai/playwright-test' +import type { Arm, Launch } from './native-chat-4a-arm' +import type { Manifest } from './native-chat-4a-fixture' +import { hostWorkMark, rendererNow } from './native-chat-4a-probes' +import type { Row, Sample } from './native-chat-4a-report' +import type { Workload } from './native-chat-4a-workloads' + +export type RunContext = { + arm: Arm + launch: Launch + workload: Workload + manifest: Manifest + rows: Row[] + log: (line: string) => void + /** Relaunch on the same profile (cold rows); replaces `launch`. */ + relaunch: () => Promise +} + +export const page = (ctx: RunContext): Page => ctx.launch.page + +export function addRow(ctx: RunContext, row: Omit & { samples?: Sample[] }): Row { + const full: Row = { samples: [], ...row } + ctx.rows.push(full) + return full +} + +/** Run `action` and record host work around it; `action` returns renderer-clock end points relative to its own start. */ +export async function measured( + ctx: RunContext, + action: (startedAt: number) => Promise> +): Promise { + const done = await hostWorkMark(ctx.launch.app) + const startedAt = await rendererNow(page(ctx)) + const sample = await action(startedAt) + return { ...sample, work: await done() } +} + +/** Run a row's samples, recording a failure as the row's skip reason instead of losing the whole family. */ +export async function sampled( + ctx: RunContext, + row: Row, + count: number, + take: (index: number) => Promise +): Promise { + try { + for (let index = 0; index < count; index += 1) { + row.samples.push(await take(index)) + } + } catch (error) { + row.skipped = + `failed after ${row.samples.length} samples: ${error instanceof Error ? error.message : String(error)}`.slice( + 0, + 300 + ) + ctx.log(`${row.family}/${row.operation}/${row.variant}: ${row.skipped}`) + } +} + +// Reset per arm so both arms send byte-identical prompts in the same order. +let unique = 0 +export const resetTags = (): void => { + unique = 0 +} +export const tagFor = (prefix: string): string => + `${prefix}-${String((unique += 1)).padStart(5, '0')}` diff --git a/config/scripts/performance/native-chat/native-chat-4a-session.ts b/config/scripts/performance/native-chat/native-chat-4a-session.ts new file mode 100644 index 00000000000..77b47d88666 --- /dev/null +++ b/config/scripts/performance/native-chat/native-chat-4a-session.ts @@ -0,0 +1,228 @@ +// Chat operations through the host RPC surface both arms serve, with envelopes built by each arm's own protocol bundle. +// Declares the window.__store and window.api globals these page.evaluate callbacks use. +import type {} from '../../../../tests/e2e/helpers/runtime-types' +import { randomUUID } from 'node:crypto' +import type { Page } from '@stablyai/playwright-test' +import { operationId, type Arm } from './native-chat-4a-arm' +import { rpc } from './native-chat-4a-probes' + +type HistoryItem = { id: string; body?: { kind?: string; role?: string; state?: string } } +export type HistoryPage = { + epoch: string + fence?: number + items: HistoryItem[] + window: { nextCursor: unknown } + hasOlder: boolean + queuedMessages?: { messageId: string; body: unknown; state: string }[] | null +} +type HistoryResult = { ok: boolean; page: HistoryPage; fence?: number } + +export async function history( + page: Page, + sessionId: string, + direction: 'tail' | 'before' | 'after', + limit: number, + cursor?: unknown +) { + const answer = await rpc(page, 'agentSession.history', { + sessionId, + direction, + limit, + ...(cursor ? { cursor } : {}) + }) + return { ...answer, page: answer.value.page } +} + +async function currentFence(page: Page, sessionId: string): Promise { + const { value } = await rpc(page, 'agentSession.history', { + sessionId, + direction: 'tail', + limit: 1 + }) + const fence = value.page.fence ?? value.fence + if (typeof fence !== 'number') { + throw new Error(`${sessionId}: history served no runtime fence`) + } + return fence +} + +export async function createChat( + page: Page, + arm: Arm, + worktreeId: string, + forkFrom?: { sessionId: string; throughItemId: string } +) { + const sessionId = arm.protocol.createStructuredAgentSessionId('claude', randomUUID) + const worktree = `id:${worktreeId}` + const params = { + envelope: { + sessionId, + clientOperationId: operationId(arm), + expectedRuntimeFence: null, + payloadFingerprint: arm.protocol.structuredAgentSessionCreateFingerprint({ + sessionId, + worktree, + agent: 'claude', + forkFrom + }) + }, + worktree, + agent: 'claude', + ...(forkFrom ? { forkFrom } : {}) + } + const answer = await rpc(page, 'agentSession.create', params) + return { sessionId, hostMs: answer.hostMs, startedAt: answer.startedAt } +} + +/** The renderer's generic write: fingerprint {method, sessionId, fields} at the fence the client last saw. */ +export async function mutate( + page: Page, + arm: Arm, + method: string, + sessionId: string, + fields: Record +) { + const envelope = { + sessionId, + clientOperationId: operationId(arm), + expectedRuntimeFence: await currentFence(page, sessionId), + payloadFingerprint: arm.protocol.structuredAgentSessionPayloadFingerprint({ + method, + sessionId, + fields + }) + } + return rpc(page, method, { envelope, ...fields }) +} + +export async function send( + page: Page, + arm: Arm, + sessionId: string, + text: string, + queueIfActive = true +) { + const params = arm.protocol.structuredAgentSessionMessageSendMutation({ + sessionId, + clientOperationId: operationId(arm), + expectedRuntimeFence: await currentFence(page, sessionId), + body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text }] }, + ...(queueIfActive ? { delivery: 'queue-if-active' as const } : {}) + }) + return rpc<{ ok?: boolean; queued?: unknown }>(page, 'agentSession.send', params) +} + +/** Poll the durable tail until it holds `marker`: the reply was committed and is readable, independent of any view. */ +export async function waitForDurable( + page: Page, + sessionId: string, + marker: string, + timeoutMs = 120_000, + everyMs = 25 +) { + const deadline = Date.now() + timeoutMs + while (Date.now() < deadline) { + const tail = await history(page, sessionId, 'tail', 3) + if (JSON.stringify(tail.page.items).includes(marker)) { + return + } + await new Promise((resolve) => setTimeout(resolve, everyMs)) + } + throw new Error(`${sessionId}: ${marker} was not durable within ${timeoutMs} ms`) +} + +/** Settled when no turn row in the tail is still running and nothing is queued. */ +export async function waitForIdle(page: Page, sessionId: string, timeoutMs = 120_000) { + const deadline = Date.now() + timeoutMs + while (Date.now() < deadline) { + const tail = await history(page, sessionId, 'tail', 20) + const running = tail.page.items.some( + (item) => item.body?.kind === 'turn' && item.body.state === 'running' + ) + if (!running && (tail.page.queuedMessages ?? []).length === 0) { + return tail.page + } + await new Promise((resolve) => setTimeout(resolve, 25)) + } + throw new Error(`${sessionId}: still active after ${timeoutMs} ms`) +} + +export async function userItemIds(page: Page, sessionId: string, limit = 200): Promise { + const ids: string[] = [] + let cursor: unknown + for (;;) { + const read = await history(page, sessionId, cursor ? 'before' : 'tail', 200, cursor) + ids.unshift( + ...read.page.items + .filter((item) => item.body?.kind === 'message' && item.body.role === 'user') + .map((item) => item.id) + ) + if (!read.page.hasOlder || ids.length >= limit) { + return ids.slice(-limit) + } + cursor = read.page.window.nextCursor + } +} + +/** Activate the chat's tab through the tab bar, the way a user switches chats; never reveals or focuses the window. */ +export async function openChatTab( + page: Page, + worktreeId: string, + sessionId: string +): Promise { + const tabId = await page.waitForFunction( + ({ worktreeId, sessionId }) => + window.__store + ?.getState() + .unifiedTabsByWorktree[worktreeId]?.find((tab) => tab.entityId === sessionId)?.id ?? false, + { worktreeId, sessionId }, + { timeout: 60_000 } + ) + const id = (await tabId.jsonValue()) as string + await page.evaluate( + ({ worktreeId, id }) => { + const state = window.__store!.getState() + if (state.activeWorktreeId !== worktreeId) { + state.setActiveWorktree(worktreeId) + } + state.setActiveTab(id) + }, + { worktreeId, id } + ) +} + +/** Controls known: the composer is interactive and the working flag has been published for this chat. */ +export async function waitForControls(page: Page, timeoutMs = 60_000): Promise { + await page.waitForFunction( + () => { + const root = [...document.querySelectorAll('[data-native-chat-root="true"]')].find( + (candidate) => candidate.checkVisibility() + ) + const composer = root?.querySelector('[role="textbox"]') + return Boolean(composer && root?.querySelector('[data-native-chat-working]')) + }, + null, + { timeout: timeoutMs, polling: 16 } + ) +} + +export async function waitForWorking( + page: Page, + working: boolean, + timeoutMs = 120_000 +): Promise { + const handle = await page.waitForFunction( + (working) => { + const root = [...document.querySelectorAll('[data-native-chat-root="true"]')].find( + (candidate) => candidate.checkVisibility() + ) + const flag = root?.querySelector('[data-native-chat-working]') + return flag?.getAttribute('data-native-chat-working') === String(working) + ? performance.now() + : false + }, + working, + { timeout: timeoutMs, polling: 16 } + ) + return (await handle.jsonValue()) as number +} diff --git a/config/scripts/performance/native-chat/native-chat-4a-workloads.ts b/config/scripts/performance/native-chat/native-chat-4a-workloads.ts new file mode 100644 index 00000000000..1961ce0a7bc --- /dev/null +++ b/config/scripts/performance/native-chat/native-chat-4a-workloads.ts @@ -0,0 +1,216 @@ +// Deterministic 4a workloads: the retained-history profile, stream shapes and per-row sample counts, identical for both arms. +import providerText from './native-chat-4a-provider-text.cjs' + +export type StreamShape = 'blocks' | 'nobreak' + +export type StreamCase = { + tag: string + bytes: number + shape: StreamShape + /** Running-to-completed tool output emitted before the text, when set. */ + toolOutputBytes?: number +} + +export type Workload = { + profile: 'smoke' | 'full' + /** Warm repeated samples per row; cold rows take `coldSamples` relaunches. */ + samples: number + coldSamples: number + history: { + /** The large retained chat: one recipe turn is 1 prompt + `tools` tool items + 1 reply (+ its turn row). */ + largeTurns: number + tools: number + toolOutputBytes: number + promptBytes: number + replyBytes: number + /** Every Nth large-chat turn is sent while active, leaving released queue history for conversion. */ + queuedEvery: number + fillerChats: number + fillerTurns: number + } + /** Streams are long; `samples` per stream case, separate from the warm row count. */ + stream: { cases: StreamCase[]; chunkBytes: number; intervalMs: number; samples: number } + /** Released queued sends accumulated before each retained-history checkpoint; the live queue stays `liveQueue`. */ + retained: { checkpoints: number[]; liveQueue: number } + rewind: { nearTipSamples: number; deepCutTurns: number; deepSamples: number } + /** Generated only on the M4Air run; smoke keeps the 55k shape off this Mac. */ + conversionSourceBytes: number +} + +const MiB = 1024 * 1024 + +function streamCases(sizes: number[]): StreamCase[] { + return sizes.flatMap((bytes) => [ + { tag: `blocks-${bytes / MiB}m`, bytes, shape: 'blocks' as const }, + { tag: `nobreak-${bytes / MiB}m`, bytes, shape: 'nobreak' as const }, + { + tag: `tool-${bytes / MiB}m`, + bytes: 64 * 1024, + shape: 'blocks' as const, + toolOutputBytes: bytes + } + ]) +} + +export const WORKLOADS: Record = { + smoke: { + profile: 'smoke', + samples: 3, + coldSamples: 1, + history: { + largeTurns: 20, + tools: 8, + toolOutputBytes: 2000, + promptBytes: 300, + replyBytes: 1500, + queuedEvery: 10, + fillerChats: 3, + fillerTurns: 1 + }, + stream: { cases: streamCases([MiB / 4]), chunkBytes: 16_384, intervalMs: 1, samples: 1 }, + retained: { checkpoints: [0, 20], liveQueue: 2 }, + rewind: { nearTipSamples: 2, deepCutTurns: 5, deepSamples: 1 }, + conversionSourceBytes: 0 + }, + full: { + profile: 'full', + samples: 30, + coldSamples: 5, + // The established 5000-turn/55000-item recipe plus 309 small chats: the 55k-history/310-chat profile. + history: { + largeTurns: 5000, + tools: 8, + toolOutputBytes: 2000, + promptBytes: 300, + replyBytes: 1500, + queuedEvery: 10, + fillerChats: 309, + fillerTurns: 2 + }, + stream: { + cases: streamCases([MiB, 8 * MiB, 16 * MiB]), + chunkBytes: 16_384, + intervalMs: 2, + samples: 10 + }, + retained: { checkpoints: [0, 1_000, 10_000], liveQueue: 2 }, + rewind: { nearTipSamples: 10, deepCutTurns: 1000, deepSamples: 3 }, + conversionSourceBytes: 20 * 1024 * MiB + } +} + +export function workloadFor(profile: string, conversionSourceBytes?: number): Workload { + const workload = WORKLOADS[profile as Workload['profile']] + if (!workload) { + throw new Error(`Unknown profile ${profile}; expected ${Object.keys(WORKLOADS).join(', ')}`) + } + return conversionSourceBytes === undefined ? workload : { ...workload, conversionSourceBytes } +} + +export type HistoryTurn = { prompt: string; queued: boolean } + +/** Large-chat recipe turns in send order; each reply ends with a 4A-TAIL marker naming its turn. */ +export function* largeHistoryTurns(workload: Workload): Generator { + const { history } = workload + for (let turn = 0; turn < history.largeTurns; turn += 1) { + const op = { + op: 'history', + tag: `large-${turn}`, + tools: history.tools, + toolOutputBytes: history.toolOutputBytes, + replyBytes: history.replyBytes + } + yield { + prompt: providerText.formatDirective(op, history.promptBytes), + queued: history.queuedEvery > 0 && turn % history.queuedEvery === history.queuedEvery - 1 + } + } +} + +export function fillerTurn(chat: number, turn: number, workload: Workload): string { + const op = { + op: 'history', + tag: `filler-${chat}-${turn}`, + tools: 2, + toolOutputBytes: 500, + replyBytes: 400 + } + return providerText.formatDirective(op, workload.history.promptBytes) +} + +/** Expected large-chat rows, so a run can refuse a fixture that did not seed what it claims. */ +export function expectedLargeItems(workload: Workload): number { + return workload.history.largeTurns * (workload.history.tools + 3) +} + +/** Per-turn tool output that makes the conversion source reach `conversionSourceBytes` with the same turn count. */ +export function conversionToolOutputBytes(workload: Workload): number { + const { history } = workload + if (workload.conversionSourceBytes === 0) { + return history.toolOutputBytes + } + return Math.max( + history.toolOutputBytes, + Math.ceil(workload.conversionSourceBytes / (history.largeTurns * history.tools)) + ) +} + +export function streamPrompt(workload: Workload, stream: StreamCase): string { + return providerText.formatDirective({ + op: 'stream', + tag: stream.tag, + bytes: stream.bytes, + shape: stream.shape, + chunkBytes: workload.stream.chunkBytes, + intervalMs: workload.stream.intervalMs, + ...(stream.toolOutputBytes ? { toolOutputBytes: stream.toolOutputBytes, toolRunMs: 250 } : {}) + }) +} + +export const holdPrompt = (tag: string, ms: number): string => + providerText.formatDirective({ op: 'hold', tag, ms }) +export const replyPrompt = (tag: string): string => + providerText.formatDirective({ op: 'reply', tag }) + +/** Generate every byte both arms will receive and digest it; changing a generator changes the dry-run digest. */ +export function dryRunWorkload(workload: Workload): Record { + const started = performance.now() + let promptBytes = 0 + let queued = 0 + let turns = 0 + for (const turn of largeHistoryTurns(workload)) { + promptBytes += Buffer.byteLength(turn.prompt) + queued += Number(turn.queued) + turns += 1 + if (providerText.parseDirective(turn.prompt)?.op !== 'history') { + throw new Error('history directive did not round-trip') + } + } + const streams = workload.stream.cases.map((stream) => { + const op = providerText.parseDirective(streamPrompt(workload, stream)) + const text = providerText.streamText(op) + if (text.length !== stream.bytes || !text.startsWith(`4A-FIRST-${stream.tag}`)) { + throw new Error(`${stream.tag}: generated ${text.length} bytes, expected ${stream.bytes}`) + } + const newlines = text.split('\n').length - 1 + return { tag: stream.tag, bytes: text.length, newlines, digest: providerText.digest(text) } + }) + return { + profile: workload.profile, + largeTurns: turns, + expectedLargeItems: expectedLargeItems(workload), + queuedHistoryTurns: queued, + largePromptBytes: promptBytes, + largeToolOutputBytes: turns * workload.history.tools * workload.history.toolOutputBytes, + chats: 1 + workload.history.fillerChats, + fillerPromptDigest: providerText.digest(fillerTurn(0, 0, workload)), + streams, + retained: workload.retained, + rewind: workload.rewind, + conversion: { + sourceBytes: workload.conversionSourceBytes, + toolOutputBytesPerTool: conversionToolOutputBytes(workload) + }, + generatorMs: Math.round(performance.now() - started) + } +} From 781e718f13e5a7ef4271a10d401d5710f42a565e Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 22:03:00 -0800 Subject: [PATCH 05/45] Fail the 4a harness launch fast on a non-e2e build and stop the app it launched --- .../native-chat/native-chat-4a-arm.ts | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/config/scripts/performance/native-chat/native-chat-4a-arm.ts b/config/scripts/performance/native-chat/native-chat-4a-arm.ts index c8381635a8c..aba2e023cd9 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-arm.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-arm.ts @@ -154,17 +154,30 @@ export async function launchArm( throw new Error(`${arm.name}: launch recorded no PID`) } const startedAt = processStart(pid) - assertElectronResolvedIsolatedHome( - await app.evaluate(({ app: electronApp }) => electronApp.getPath('home')), - isolation - ) - const page = await app.firstWindow({ timeout: 120_000 }) - await page.waitForFunction( - () => window.__store?.getState().workspaceSessionReady === true, - null, - { timeout: 600_000, polling: 16 } - ) - return { app, page, pid, startedAt, userDataDir, launchedAtEpochMs, readyEpochMs: Date.now() } + try { + assertElectronResolvedIsolatedHome( + await app.evaluate(({ app: electronApp }) => electronApp.getPath('home')), + isolation + ) + const page = await app.firstWindow({ timeout: 120_000 }) + // Only an e2e-mode build exposes the store; a desktop build would otherwise wait out the full budget. + await page + .waitForFunction(() => Boolean(window.__store), null, { timeout: 60_000 }) + .catch(() => { + throw new Error( + `${arm.name}: no window.__store; build ${arm.root} with VITE_EXPOSE_STORE=true electron-vite build --mode e2e` + ) + }) + await page.waitForFunction( + () => window.__store?.getState().workspaceSessionReady === true, + null, + { timeout: 600_000, polling: 16 } + ) + return { app, page, pid, startedAt, userDataDir, launchedAtEpochMs, readyEpochMs: Date.now() } + } catch (error) { + await closeElectronAppForE2E(app, { cleanupDaemonsForUserDataDir: userDataDir }) + throw error + } } export async function stopArm(launch: Launch): Promise { From 948cd3de8b4aa54ceb9e1e7a53d398f5c3cd69f0 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 22:18:59 -0800 Subject: [PATCH 06/45] Shim tsx's __name in 4a harness pages and install the probe inside the guarded launch --- .../scripts/performance/native-chat/native-chat-4a-arm.ts | 6 ++++++ .../performance/native-chat/native-chat-4a-comparison.ts | 7 +------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/config/scripts/performance/native-chat/native-chat-4a-arm.ts b/config/scripts/performance/native-chat/native-chat-4a-arm.ts index aba2e023cd9..cbba47b9723 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-arm.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-arm.ts @@ -20,6 +20,7 @@ import { getOrcaElectronLaunchArgs } from '../../../../tests/e2e/helpers/electro import { closeElectronAppForE2E } from '../../../../tests/e2e/helpers/electron-process-shutdown' import { getE2ECompletedOnboardingProfile } from '../../../../tests/e2e/helpers/e2e-completed-onboarding-profile' import { buildFakeAgentCommandOverride } from '../../../../tests/e2e/helpers/fake-agent-command-override' +import { installVisibilityProbe } from './native-chat-4a-probes' const HARNESS_ROOT = path.resolve(import.meta.dirname, '../../../..') const SYSTEM_DIRS = ['/usr/bin', '/bin', '/usr/sbin', '/sbin'] @@ -160,6 +161,10 @@ export async function launchArm( isolation ) const page = await app.firstWindow({ timeout: 120_000 }) + // tsx keeps function names by wrapping them in __name(), which page.evaluate callbacks carry into the renderer. + const nameShim = 'globalThis.__name ??= (fn) => fn' + await page.addInitScript(nameShim) + await page.evaluate(nameShim) // Only an e2e-mode build exposes the store; a desktop build would otherwise wait out the full budget. await page .waitForFunction(() => Boolean(window.__store), null, { timeout: 60_000 }) @@ -173,6 +178,7 @@ export async function launchArm( null, { timeout: 600_000, polling: 16 } ) + await installVisibilityProbe(page) return { app, page, pid, startedAt, userDataDir, launchedAtEpochMs, readyEpochMs: Date.now() } } catch (error) { await closeElectronAppForE2E(app, { cleanupDaemonsForUserDataDir: userDataDir }) diff --git a/config/scripts/performance/native-chat/native-chat-4a-comparison.ts b/config/scripts/performance/native-chat/native-chat-4a-comparison.ts index cb0e3b2dc98..b847991f978 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-comparison.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-comparison.ts @@ -100,7 +100,6 @@ async function runArm(input: { largeSource?: { seedDir: string; manifest: Manifest } }): Promise { const { copyProfile, launchArm, stopArm } = await import('./native-chat-4a-arm') - const { installVisibilityProbe } = await import('./native-chat-4a-probes') const { resetTags } = await import('./native-chat-4a-run-context') const lifecycle = await import('./native-chat-4a-ops-lifecycle') const queue = await import('./native-chat-4a-ops-queue') @@ -121,11 +120,7 @@ async function runArm(input: { } copyProfile(seedDir, profileDir) resetTags() - const open = async () => { - const launch = await launchArm(input.arm, profileDir, input.stubCommand) - await installVisibilityProbe(launch.page) - return launch - } + const open = () => launchArm(input.arm, profileDir, input.stubCommand) const ctx = { arm: input.arm, launch: await open(), From 7ce7d484da4cd401aea6d08f023b19bd1a305937 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 22:30:47 -0800 Subject: [PATCH 07/45] Read the native chat working flag from the 4a harness's visible root itself --- .../performance/native-chat/native-chat-4a-session.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config/scripts/performance/native-chat/native-chat-4a-session.ts b/config/scripts/performance/native-chat/native-chat-4a-session.ts index 77b47d88666..522a8741524 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-session.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-session.ts @@ -199,7 +199,8 @@ export async function waitForControls(page: Page, timeoutMs = 60_000): Promise candidate.checkVisibility() ) const composer = root?.querySelector('[role="textbox"]') - return Boolean(composer && root?.querySelector('[data-native-chat-working]')) + // The working flag is an attribute of the root itself. + return Boolean(composer && root?.hasAttribute('data-native-chat-working')) }, null, { timeout: timeoutMs, polling: 16 } @@ -216,8 +217,7 @@ export async function waitForWorking( const root = [...document.querySelectorAll('[data-native-chat-root="true"]')].find( (candidate) => candidate.checkVisibility() ) - const flag = root?.querySelector('[data-native-chat-working]') - return flag?.getAttribute('data-native-chat-working') === String(working) + return root?.getAttribute('data-native-chat-working') === String(working) ? performance.now() : false }, From 6dadd80f21a536ab3779886a34a93375573424f0 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 22:38:27 -0800 Subject: [PATCH 08/45] Retry transient main-process evaluates in the 4a harness host-work probe --- .../native-chat/native-chat-4a-arm.ts | 5 +++- .../native-chat/native-chat-4a-probes.ts | 27 +++++++++++-------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/config/scripts/performance/native-chat/native-chat-4a-arm.ts b/config/scripts/performance/native-chat/native-chat-4a-arm.ts index cbba47b9723..0b6c5d3160c 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-arm.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-arm.ts @@ -17,6 +17,7 @@ import { assertElectronResolvedIsolatedHome } from '../../../../tests/e2e/helpers/electron-home-isolation' import { getOrcaElectronLaunchArgs } from '../../../../tests/e2e/helpers/electron-launch-args' +import { retryTransientMainEvaluate } from '../../../../tests/e2e/helpers/electron-main-evaluate-retry' import { closeElectronAppForE2E } from '../../../../tests/e2e/helpers/electron-process-shutdown' import { getE2ECompletedOnboardingProfile } from '../../../../tests/e2e/helpers/e2e-completed-onboarding-profile' import { buildFakeAgentCommandOverride } from '../../../../tests/e2e/helpers/fake-agent-command-override' @@ -157,7 +158,9 @@ export async function launchArm( const startedAt = processStart(pid) try { assertElectronResolvedIsolatedHome( - await app.evaluate(({ app: electronApp }) => electronApp.getPath('home')), + await retryTransientMainEvaluate(() => + app.evaluate(({ app: electronApp }) => electronApp.getPath('home')) + ), isolation ) const page = await app.firstWindow({ timeout: 120_000 }) diff --git a/config/scripts/performance/native-chat/native-chat-4a-probes.ts b/config/scripts/performance/native-chat/native-chat-4a-probes.ts index 03b4550bf97..cff44fe4ca9 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-probes.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-probes.ts @@ -3,6 +3,7 @@ import { readdirSync, statSync } from 'node:fs' import path from 'node:path' import type { Performance as NodePerformance } from 'node:perf_hooks' import type { ElectronApplication, Page } from '@stablyai/playwright-test' +import { retryTransientMainEvaluate } from '../../../../tests/e2e/helpers/electron-main-evaluate-retry' import { summarizeEventLoopDelay } from '../../../../tests/e2e/helpers/event-loop-delay-report' export type RpcAnswer = { ok: true; result: T } | { ok: false; error: unknown } @@ -122,18 +123,22 @@ export type HostWork = { cpuMs: number; mainThreadActiveMs: number; offMainThrea /** Process CPU covers the main thread and its storage workers; main-thread ELU separates the two. */ export async function hostWorkMark(app: ElectronApplication): Promise<() => Promise> { // Main-process callbacks see Node's performance, not the DOM one this file is typed against. - const start = await app.evaluate(() => ({ - cpu: process.cpuUsage(), - elu: (performance as unknown as NodePerformance).eventLoopUtilization() - })) + const start = await retryTransientMainEvaluate(() => + app.evaluate(() => ({ + cpu: process.cpuUsage(), + elu: (performance as unknown as NodePerformance).eventLoopUtilization() + })) + ) return async () => { - const delta = await app.evaluate((_electron, start) => { - const node = performance as unknown as NodePerformance - return { - cpu: process.cpuUsage(start.cpu), - elu: node.eventLoopUtilization(node.eventLoopUtilization(), start.elu) - } - }, start) + const delta = await retryTransientMainEvaluate(() => + app.evaluate((_electron, start) => { + const node = performance as unknown as NodePerformance + return { + cpu: process.cpuUsage(start.cpu), + elu: node.eventLoopUtilization(node.eventLoopUtilization(), start.elu) + } + }, start) + ) const cpuMs = (delta.cpu.user + delta.cpu.system) / 1000 return { cpuMs, From 4e9402a441b2ccfb8a3af69690f601469867fd22 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 22:48:02 -0800 Subject: [PATCH 09/45] Use the history page's itemId in the 4a harness and name what a visible-state wait timed out on --- .../native-chat/native-chat-4a-ops-reads.ts | 2 +- .../native-chat/native-chat-4a-probes.ts | 34 ++++++++++++++--- .../native-chat/native-chat-4a-session.ts | 37 ++++++++++++------- 3 files changed, 52 insertions(+), 21 deletions(-) diff --git a/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts b/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts index 47b3bb96bb3..8846cdf7b38 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts @@ -94,7 +94,7 @@ export async function historyRows(ctx: RunContext): Promise { continue } await sampled(ctx, row, n, () => - read(ctx, 'agentSession.item', { sessionId, itemId: entry.item.id }) + read(ctx, 'agentSession.item', { sessionId, itemId: entry.item.itemId }) ) } } diff --git a/config/scripts/performance/native-chat/native-chat-4a-probes.ts b/config/scripts/performance/native-chat/native-chat-4a-probes.ts index cff44fe4ca9..48bdd599775 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-probes.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-probes.ts @@ -94,15 +94,37 @@ export async function markerSeenAt( marker: string, timeoutMs = 120_000 ): Promise { - const handle = await page.waitForFunction( - (marker) => - (window as unknown as { __4a: { seen: Map } }).__4a.seen.get(marker) ?? false, - marker, - { timeout: timeoutMs, polling: 16 } - ) + const handle = await page + .waitForFunction( + (marker) => + (window as unknown as { __4a: { seen: Map } }).__4a.seen.get(marker) ?? + false, + marker, + { timeout: timeoutMs, polling: 16 } + ) + .catch(async (error: unknown) => { + throw new Error( + `${marker} not visible in ${timeoutMs} ms (${await visibleChatState(page)})`, + { + cause: error + } + ) + }) return (await handle.jsonValue()) as number } +/** One line on the chat the user sees, for timeout messages. */ +export function visibleChatState(page: Page): Promise { + return page + .evaluate(() => { + const roots = [...document.querySelectorAll('[data-native-chat-root="true"]')] + const root = roots.find((candidate) => candidate.checkVisibility()) + const text = root?.textContent ?? '' + return `${roots.length} roots, visible=${Boolean(root)}, working=${root?.getAttribute('data-native-chat-working')}, tail=${JSON.stringify(text.slice(-160))}` + }) + .catch((error: unknown) => `state unreadable: ${String(error)}`) +} + /** Gaps between visible transcript updates inside [from, to], the live-update cadence a reader sees. */ export async function mutationGaps(page: Page, from: number, to: number): Promise { return page.evaluate( diff --git a/config/scripts/performance/native-chat/native-chat-4a-session.ts b/config/scripts/performance/native-chat/native-chat-4a-session.ts index 522a8741524..c886b2d7bb2 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-session.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-session.ts @@ -4,9 +4,9 @@ import type {} from '../../../../tests/e2e/helpers/runtime-types' import { randomUUID } from 'node:crypto' import type { Page } from '@stablyai/playwright-test' import { operationId, type Arm } from './native-chat-4a-arm' -import { rpc } from './native-chat-4a-probes' +import { rpc, visibleChatState } from './native-chat-4a-probes' -type HistoryItem = { id: string; body?: { kind?: string; role?: string; state?: string } } +type HistoryItem = { itemId: string; body?: { kind?: string; role?: string; state?: string } } export type HistoryPage = { epoch: string fence?: number @@ -155,7 +155,7 @@ export async function userItemIds(page: Page, sessionId: string, limit = 200): P ids.unshift( ...read.page.items .filter((item) => item.body?.kind === 'message' && item.body.role === 'user') - .map((item) => item.id) + .map((item) => item.itemId) ) if (!read.page.hasOlder || ids.length >= limit) { return ids.slice(-limit) @@ -212,17 +212,26 @@ export async function waitForWorking( working: boolean, timeoutMs = 120_000 ): Promise { - const handle = await page.waitForFunction( - (working) => { - const root = [...document.querySelectorAll('[data-native-chat-root="true"]')].find( - (candidate) => candidate.checkVisibility() + const handle = await page + .waitForFunction( + (working) => { + const root = [...document.querySelectorAll('[data-native-chat-root="true"]')].find( + (candidate) => candidate.checkVisibility() + ) + return root?.getAttribute('data-native-chat-working') === String(working) + ? performance.now() + : false + }, + working, + { timeout: timeoutMs, polling: 16 } + ) + .catch(async (error: unknown) => { + throw new Error( + `working=${working} not shown in ${timeoutMs} ms (${await visibleChatState(page)})`, + { + cause: error + } ) - return root?.getAttribute('data-native-chat-working') === String(working) - ? performance.now() - : false - }, - working, - { timeout: timeoutMs, polling: 16 } - ) + }) return (await handle.jsonValue()) as number } From 433663597d0b1997d4a5b211fe3840f995ce3148 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 22:52:13 -0800 Subject: [PATCH 10/45] Activate 4a harness chat tabs through their tab group and seed enough smoke filler chats for the unopened-chat row --- .../native-chat/native-chat-4a-ops-lifecycle.ts | 3 +++ .../native-chat/native-chat-4a-ops-reads.ts | 2 +- .../native-chat/native-chat-4a-session.ts | 15 ++++++++++++--- .../native-chat/native-chat-4a-workloads.ts | 2 +- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts b/config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts index c395407b1ce..7ea3de68e48 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts @@ -218,6 +218,9 @@ export async function openRows(ctx: RunContext): Promise { context }) const untouched = fillerSessionIds.slice(3, -1) + if (untouched.length === 0) { + unopened.skipped = 'profile has no filler chat left untouched by earlier families' + } await sampled(ctx, unopened, Math.min(ctx.workload.samples, untouched.length), (index) => { const chat = { sessionId: untouched[index]!, worktreeId: workspace.worktreeIds[1] } const chatIndex = fillerSessionIds.indexOf(chat.sessionId) diff --git a/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts b/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts index 8846cdf7b38..1b59de772a4 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts @@ -15,7 +15,7 @@ import { createChat, history, mutate, userItemIds, waitForIdle } from './native- const bytesOf = (value: unknown): number => Buffer.byteLength(JSON.stringify(value)) -type Item = { id: string; body?: { kind?: string } } +type Item = { itemId: string; body?: { kind?: string } } async function read( ctx: RunContext, diff --git a/config/scripts/performance/native-chat/native-chat-4a-session.ts b/config/scripts/performance/native-chat/native-chat-4a-session.ts index c886b2d7bb2..3bc5a0c0909 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-session.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-session.ts @@ -179,15 +179,24 @@ export async function openChatTab( { timeout: 60_000 } ) const id = (await tabId.jsonValue()) as string - await page.evaluate( + // Unified chat tabs activate through their group; a worktree switch can restore another tab first, so re-apply until it holds. + await page.waitForFunction( ({ worktreeId, id }) => { const state = window.__store!.getState() + const group = (state.groupsByWorktree[worktreeId] ?? []).find((candidate) => + candidate.tabOrder.includes(id) + ) + if (state.activeWorktreeId === worktreeId && group?.activeTabId === id) { + return true + } if (state.activeWorktreeId !== worktreeId) { state.setActiveWorktree(worktreeId) } - state.setActiveTab(id) + window.__store!.getState().activateTab(id, { worktreeId }) + return false }, - { worktreeId, id } + { worktreeId, id }, + { timeout: 30_000, polling: 100 } ) } diff --git a/config/scripts/performance/native-chat/native-chat-4a-workloads.ts b/config/scripts/performance/native-chat/native-chat-4a-workloads.ts index 1961ce0a7bc..532480c824f 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-workloads.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-workloads.ts @@ -64,7 +64,7 @@ export const WORKLOADS: Record = { promptBytes: 300, replyBytes: 1500, queuedEvery: 10, - fillerChats: 3, + fillerChats: 6, fillerTurns: 1 }, stream: { cases: streamCases([MiB / 4]), chunkBytes: 16_384, intervalMs: 1, samples: 1 }, From 32c935c07e8165b560f49e44c9df4b5df7364ab1 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 23:07:53 -0800 Subject: [PATCH 11/45] Treat refusals carried inside a successful 4a harness RPC result as refusals --- .../native-chat/native-chat-4a-probes.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/config/scripts/performance/native-chat/native-chat-4a-probes.ts b/config/scripts/performance/native-chat/native-chat-4a-probes.ts index 48bdd599775..1c21792c1f7 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-probes.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-probes.ts @@ -23,11 +23,18 @@ export async function rpc( }, { method, params } ) - if (!answer.reply.ok && !options.allowRefusal) { - throw new Error(`${method} refused: ${JSON.stringify(answer.reply.error).slice(0, 400)}`) + // Some writes (create, fork) refuse inside a successful call: `{ ok: false, refusal }`. + const result = answer.reply.ok + ? (answer.reply.result as { ok?: unknown; refusal?: unknown }) + : null + const inResult = result?.ok === false && 'refusal' in result + const refused = !answer.reply.ok || inResult + if (refused && !options.allowRefusal) { + const reason = answer.reply.ok ? result?.refusal : answer.reply.error + throw new Error(`${method} refused: ${JSON.stringify(reason).slice(0, 400)}`) } const value = (answer.reply.ok ? answer.reply.result : answer.reply.error) as T - return { value, refused: !answer.reply.ok, startedAt: answer.startedAt, hostMs: answer.hostMs } + return { value, refused, startedAt: answer.startedAt, hostMs: answer.hostMs } } /** Install once per page: records when text first appears in the chat root and every transcript mutation batch. */ From 7fec6f1d559f5edfbf879dddeaff3bc8eb9364a7 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 23:09:08 -0800 Subject: [PATCH 12/45] Give the 4a stub's user echo the uuid its transcript row records --- .../native-chat/native-chat-4a-provider-stub.cjs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/config/scripts/performance/native-chat/native-chat-4a-provider-stub.cjs b/config/scripts/performance/native-chat/native-chat-4a-provider-stub.cjs index 8155ded9d94..12de18656f5 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-provider-stub.cjs +++ b/config/scripts/performance/native-chat/native-chat-4a-provider-stub.cjs @@ -196,8 +196,10 @@ async function startTurn(frame) { const controller = new AbortController() current = controller // The CLI's --replay-user-messages echo, which opens the turn. - remember({ type: 'user', uuid: frame.uuid ?? randomUUID(), message: frame.message }) - await write({ ...frame, isReplay: true }) + // One uuid for the echo and the transcript row, so fork and rewind can find this prompt. + const echo = { ...frame, uuid: frame.uuid ?? randomUUID(), session_id: sessionId, isReplay: true } + remember({ type: 'user', uuid: echo.uuid, message: frame.message }) + await write(echo) const content = frame.message?.content ?? frame.message const prompt = Array.isArray(content) ? content.map((block) => block?.text ?? '').join('\n') From 7378d21e730d5e11039f90d179bd3b02320b701a Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 23:11:41 -0800 Subject: [PATCH 13/45] Chain the 4a stub's resumed transcript to the last message, past a fork copy's title row --- .../native-chat/native-chat-4a-provider-stub.cjs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/config/scripts/performance/native-chat/native-chat-4a-provider-stub.cjs b/config/scripts/performance/native-chat/native-chat-4a-provider-stub.cjs index 12de18656f5..524bad5a412 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-provider-stub.cjs +++ b/config/scripts/performance/native-chat/native-chat-4a-provider-stub.cjs @@ -35,7 +35,11 @@ fs.mkdirSync(path.dirname(transcriptPath), { recursive: true }) let parentUuid = null if (fs.existsSync(transcriptPath)) { const lines = fs.readFileSync(transcriptPath, 'utf8').trim().split('\n').filter(Boolean) - parentUuid = lines.length > 0 ? (JSON.parse(lines.at(-1)).uuid ?? null) : null + // Chain to the last message, as the CLI does: a fork copy ends with a parentless custom-title row. + const last = lines + .map((line) => JSON.parse(line)) + .findLast((row) => (row.type === 'user' || row.type === 'assistant') && !row.isSidechain) + parentUuid = last?.uuid ?? null } // Fork and rewind read the provider's own transcript, so every user/assistant frame is also saved there. From 00ae90a4ac4748c77bfa5aaf80d8d5cd305b9a9b Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 23:32:20 -0800 Subject: [PATCH 14/45] Fork the 4a harness's large chat through its latest response, not its latest prompt --- .../native-chat/native-chat-4a-ops-lifecycle.ts | 6 +++--- .../native-chat/native-chat-4a-ops-reads.ts | 6 +++--- .../performance/native-chat/native-chat-4a-session.ts | 10 ++++++++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts b/config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts index 7ea3de68e48..b9f54755bc6 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts @@ -23,7 +23,7 @@ import { mutate, openChatTab, send, - userItemIds, + messageItemIds, waitForControls, waitForDurable, waitForIdle @@ -292,7 +292,7 @@ export async function rewindRows(ctx: RunContext): Promise { sessionId: largeSessionId, worktreeId: workspace.worktreeIds[0] }) - const [tip] = (await userItemIds(page(ctx), largeSessionId, 1)).slice(-1) + const [tip] = (await messageItemIds(page(ctx), largeSessionId, 'user', 1)).slice(-1) return rewindOnce(tip!, marker) }) const cut = ctx.workload.rewind.deepCutTurns @@ -304,7 +304,7 @@ export async function rewindRows(ctx: RunContext): Promise { endToEndOnly }) await sampled(ctx, deep, ctx.workload.rewind.deepSamples, async () => { - const ids = await userItemIds(page(ctx), largeSessionId, cut + 1) + const ids = await messageItemIds(page(ctx), largeSessionId, 'user', cut + 1) const tail = JSON.stringify( (await history(page(ctx), largeSessionId, 'tail', 3)).page.items ).match(/4A-TAIL-[\w-]+/)?.[0] diff --git a/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts b/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts index 1b59de772a4..14cace375d3 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts @@ -11,7 +11,7 @@ import { tagFor, type RunContext } from './native-chat-4a-run-context' -import { createChat, history, mutate, userItemIds, waitForIdle } from './native-chat-4a-session' +import { createChat, history, messageItemIds, mutate, waitForIdle } from './native-chat-4a-session' const bytesOf = (value: unknown): number => Buffer.byteLength(JSON.stringify(value)) @@ -111,7 +111,7 @@ export async function outlineAndForkRows(ctx: RunContext): Promise { read(ctx, 'agentSession.conversationOutline', { sessionId }) ) - const [tip] = (await userItemIds(page(ctx), sessionId, 1)).slice(-1) + const [tip] = (await messageItemIds(page(ctx), sessionId, 'assistant', 1)).slice(-1) const fork = addRow(ctx, { family: 'outline', operation: 'tip fork (create forkFrom)', @@ -120,7 +120,7 @@ export async function outlineAndForkRows(ctx: RunContext): Promise { endToEndOnly: 'local validation and provider copy are one host span' }) if (!tip) { - fork.skipped = 'no user item on the large chat' + fork.skipped = 'no response on the large chat' return } await sampled(ctx, fork, Math.min(ctx.workload.samples, 10), () => diff --git a/config/scripts/performance/native-chat/native-chat-4a-session.ts b/config/scripts/performance/native-chat/native-chat-4a-session.ts index 3bc5a0c0909..241054f0930 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-session.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-session.ts @@ -147,14 +147,20 @@ export async function waitForIdle(page: Page, sessionId: string, timeoutMs = 120 throw new Error(`${sessionId}: still active after ${timeoutMs} ms`) } -export async function userItemIds(page: Page, sessionId: string, limit = 200): Promise { +/** Message item ids of one role, oldest first: rewind cuts at a user prompt, fork copies through a response. */ +export async function messageItemIds( + page: Page, + sessionId: string, + role: 'user' | 'assistant', + limit = 200 +): Promise { const ids: string[] = [] let cursor: unknown for (;;) { const read = await history(page, sessionId, cursor ? 'before' : 'tail', 200, cursor) ids.unshift( ...read.page.items - .filter((item) => item.body?.kind === 'message' && item.body.role === 'user') + .filter((item) => item.body?.kind === 'message' && item.body.role === role) .map((item) => item.itemId) ) if (!read.page.hasOlder || ids.length >= limit) { From 6b9e141e6889709c5da3576013635470ed47d359 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:16:40 -0800 Subject: [PATCH 15/45] Port ACP tests to SQL journal named reads --- .../acp/acp-structured-compaction.test.ts | 2 +- .../acp-structured-host-model-options.test.ts | 19 +- ...cp-structured-host-opencode-effort.test.ts | 2 +- .../acp-structured-host-option-hang.test.ts | 14 +- ...acp-structured-host-restore-failed.test.ts | 11 +- .../acp-structured-host-start-aborts.test.ts | 6 +- .../acp-structured-launch-resolution.test.ts | 216 +++++------------- .../acp-structured-managed-runtime.test.ts | 2 +- .../acp/acp-structured-missing-model.test.ts | 4 +- src/main/acp/acp-structured-steer.test.ts | 36 +-- ...cp-structured-unknown-window-usage.test.ts | 12 +- .../acp/history/acp-history-boundary.test.ts | 9 +- .../acp/history/acp-history-catch-up.test.ts | 49 ++-- ...ctured-journal-settlement-rollback.test.ts | 125 ---------- 14 files changed, 131 insertions(+), 376 deletions(-) delete mode 100644 src/main/codex/structured/journal/codex-structured-journal-settlement-rollback.test.ts diff --git a/src/main/acp/acp-structured-compaction.test.ts b/src/main/acp/acp-structured-compaction.test.ts index 49cbc525992..07327399218 100644 --- a/src/main/acp/acp-structured-compaction.test.ts +++ b/src/main/acp/acp-structured-compaction.test.ts @@ -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 () => { diff --git a/src/main/acp/acp-structured-host-model-options.test.ts b/src/main/acp/acp-structured-host-model-options.test.ts index 3f11ba8c815..d68a348e739 100644 --- a/src/main/acp/acp-structured-host-model-options.test.ts +++ b/src/main/acp/acp-structured-host-model-options.test.ts @@ -163,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 = (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') { @@ -303,7 +305,8 @@ it.each([10, 100, 'Send', 'rejection'] as const)( [second, PRO] ] as const) { expect( - (await 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) } diff --git a/src/main/acp/acp-structured-host-opencode-effort.test.ts b/src/main/acp/acp-structured-host-opencode-effort.test.ts index c1c84bcd8fe..41e3e49816b 100644 --- a/src/main/acp/acp-structured-host-opencode-effort.test.ts +++ b/src/main/acp/acp-structured-host-opencode-effort.test.ts @@ -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) } diff --git a/src/main/acp/acp-structured-host-option-hang.test.ts b/src/main/acp/acp-structured-host-option-hang.test.ts index f37d7d25449..e69058d360e 100644 --- a/src/main/acp/acp-structured-host-option-hang.test.ts +++ b/src/main/acp/acp-structured-host-option-hang.test.ts @@ -43,7 +43,7 @@ describe('a Grok model pick Grok never answers during send', () => { expect( ( await readTestJournalSubmissions( - host.collaboratorsForTests().sessions.get(SESSION)?.journal + host.collaboratorsForTests().sessions.get(SESSION)!.journal ) )[0]?.dispatchState ).toBe('rejected') @@ -84,7 +84,7 @@ 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) @@ -112,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() @@ -133,7 +137,7 @@ describe('a Grok model pick Grok never answers during send', () => { expect( ( await readTestJournalSubmissions( - host.collaboratorsForTests().sessions.get(SESSION)?.journal + host.collaboratorsForTests().sessions.get(SESSION)!.journal ) )[0]?.dispatchState ).toBe('rejected') diff --git a/src/main/acp/acp-structured-host-restore-failed.test.ts b/src/main/acp/acp-structured-host-restore-failed.test.ts index 4642b4b4e4a..b1622cd3033 100644 --- a/src/main/acp/acp-structured-host-restore-failed.test.ts +++ b/src/main/acp/acp-structured-host-restore-failed.test.ts @@ -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 { @@ -81,7 +81,12 @@ 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), + savedItemIds: async (sessionId, itemIds) => + new Set(await journalSqlReads(journalDatabase.readers, sessionId).savedItemIds(itemIds)) + }, 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({ diff --git a/src/main/acp/acp-structured-host-start-aborts.test.ts b/src/main/acp/acp-structured-host-start-aborts.test.ts index 34f4119f45d..887e338814b 100644 --- a/src/main/acp/acp-structured-host-start-aborts.test.ts +++ b/src/main/acp/acp-structured-host-start-aborts.test.ts @@ -158,10 +158,8 @@ 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( (await readTestJournalSubmissions(journal)).map((entry) => entry.rejection?.kind ?? null) diff --git a/src/main/acp/acp-structured-launch-resolution.test.ts b/src/main/acp/acp-structured-launch-resolution.test.ts index 6a2f541a01d..395bbe65a98 100644 --- a/src/main/acp/acp-structured-launch-resolution.test.ts +++ b/src/main/acp/acp-structured-launch-resolution.test.ts @@ -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,18 @@ function ompRecord(options: Record, 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, + savedItemIds: async () => new Set() } -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 +82,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 chat with a saved exchange and warns of an unannounced loss, unless unreadable', 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 +104,56 @@ 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), + savedItemIds: async (sessionId, itemIds) => + new Set(await journalSqlReads(readers, sessionId).savedItemIds(itemIds)) + } + 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: await launch.resume?.unannouncedLosses() } } - 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 proves no exchange and suppresses new warnings. + const unreadable: AcpSavedSessionReads = { + hasProviderTurn: () => Promise.reject(new Error('journal unreadable')), + savedItemIds: () => Promise.reject(new Error('journal unreadable')) + } + expect(await readDecisions(unreadable)).toEqual({ unsaved: false, losses: [] }) } 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 +178,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}` diff --git a/src/main/acp/acp-structured-managed-runtime.test.ts b/src/main/acp/acp-structured-managed-runtime.test.ts index 02014a72465..574eb9e9644 100644 --- a/src/main/acp/acp-structured-managed-runtime.test.ts +++ b/src/main/acp/acp-structured-managed-runtime.test.ts @@ -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, savedItemIds: async () => new Set() }, resolveEnvironment: async () => ({ PATH: '', HOME: homePath }), dataDirectory }) diff --git a/src/main/acp/acp-structured-missing-model.test.ts b/src/main/acp/acp-structured-missing-model.test.ts index c3027f91682..bb40bec29c5 100644 --- a/src/main/acp/acp-structured-missing-model.test.ts +++ b/src/main/acp/acp-structured-missing-model.test.ts @@ -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]]) }) diff --git a/src/main/acp/acp-structured-steer.test.ts b/src/main/acp/acp-structured-steer.test.ts index b7d46e8a02b..87ebd16c06b 100644 --- a/src/main/acp/acp-structured-steer.test.ts +++ b/src/main/acp/acp-structured-steer.test.ts @@ -186,7 +186,9 @@ describe('permission changes with already admitted ACP input', () => { 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') }) @@ -213,23 +215,27 @@ 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 + 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 journal.reads.submission(handoffId(b)))?.dispatchState).toBe('pending') + 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((await journal.reads.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 }) @@ -241,14 +247,18 @@ describe('permission changes with already admitted ACP input', () => { if (outcome === 'drained') { await waitFor(() => expect(original.exited).toBe(true)) 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((await journal.reads.submission(handoffId(c)))?.dispatchState).toBe('rejected') + expect(await dispatchOf(c)).toBe('rejected') } await host.close(SESSION, 'user-close') } diff --git a/src/main/acp/acp-structured-unknown-window-usage.test.ts b/src/main/acp/acp-structured-unknown-window-usage.test.ts index c91b48e8eb1..7efba6a7084 100644 --- a/src/main/acp/acp-structured-unknown-window-usage.test.ts +++ b/src/main/acp/acp-structured-unknown-window-usage.test.ts @@ -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()) diff --git a/src/main/acp/history/acp-history-boundary.test.ts b/src/main/acp/history/acp-history-boundary.test.ts index c2825705bd4..0bff134d269 100644 --- a/src/main/acp/history/acp-history-boundary.test.ts +++ b/src/main/acp/history/acp-history-boundary.test.ts @@ -78,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) diff --git a/src/main/acp/history/acp-history-catch-up.test.ts b/src/main/acp/history/acp-history-catch-up.test.ts index 88c06975955..3fbe27a7273 100644 --- a/src/main/acp/history/acp-history-catch-up.test.ts +++ b/src/main/acp/history/acp-history-catch-up.test.ts @@ -1,4 +1,7 @@ -import { readTestJournalSubmissions } from '../../native-chat/agent-session-journal/journal-observation.test-support' +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. @@ -20,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' @@ -31,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' @@ -43,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 { // The rig's journal keys rows under its own provider session. return JSON.parse( @@ -182,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) @@ -192,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) @@ -241,14 +231,9 @@ describe('ACP history catch-up', () => { await catchUp(full) expect(await rig.rows()).toEqual(complete) const reopened = await rig.reopenJournal() - expect( - isHistoricalProviderTimelineItem((await reopened.reads.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 () => { @@ -508,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) diff --git a/src/main/codex/structured/journal/codex-structured-journal-settlement-rollback.test.ts b/src/main/codex/structured/journal/codex-structured-journal-settlement-rollback.test.ts deleted file mode 100644 index 8ea460d1520..00000000000 --- a/src/main/codex/structured/journal/codex-structured-journal-settlement-rollback.test.ts +++ /dev/null @@ -1,125 +0,0 @@ -import { 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' -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[] = [] - 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 = await readTestJournalSnapshot(journal) - 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(await readTestJournalSnapshot(journal)).toEqual(before) - expect(commits).not.toHaveBeenCalled() - expect(published.every((snapshot) => JSON.stringify(snapshot) === JSON.stringify(before))).toBe( - true - ) - translator.dispose() - deferred.close() -}) From 5e6554f049ec13e4e46cb5ceef4249afba04de1b Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:16:50 -0800 Subject: [PATCH 16/45] Port Claude tests to SQL journal named reads --- .../claude-background-stop-records.test.ts | 42 +++-- .../claude-background-wake-retention.test.ts | 147 ++++++++++-------- ...hild-work-producer-harness.test-fixture.ts | 7 +- .../claude-context-usage-restart.test.ts | 12 +- ...claude-context-usage-unloaded-turn.test.ts | 35 +++-- ...claude-subagent-permission-request.test.ts | 57 ++++--- ...aude-subagent-resume-after-restart.test.ts | 30 ++-- .../claude/claude-transcript-catch-up.test.ts | 77 +++++---- 8 files changed, 223 insertions(+), 184 deletions(-) diff --git a/src/main/claude/claude-background-stop-records.test.ts b/src/main/claude/claude-background-stop-records.test.ts index 31e7a477a0c..9fa775a2922 100644 --- a/src/main/claude/claude-background-stop-records.test.ts +++ b/src/main/claude/claude-background-stop-records.test.ts @@ -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, + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: admission reads only the state answer. + readWork: async () => + ({ kind: 'state', state: IDLE_WORK }) as JournalWorkAnswer, 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 () => { diff --git a/src/main/claude/claude-background-wake-retention.test.ts b/src/main/claude/claude-background-wake-retention.test.ts index 6fd4c15f9bf..303dad57ba6 100644 --- a/src/main/claude/claude-background-wake-retention.test.ts +++ b/src/main/claude/claude-background-wake-retention.test.ts @@ -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[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 => - submissions.map((submission) => ({ ...submission })), - item: (itemId: string): ReturnType => { - const item = run.journalItems.get(itemId) - return item ? { ...item, revision: 1, observedAt: run.now() } : null - }, - statusRead(): ReturnType { - 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) => 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 => { + 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([]) }) }) diff --git a/src/main/claude/claude-child-work-producer-harness.test-fixture.ts b/src/main/claude/claude-child-work-producer-harness.test-fixture.ts index 6ff7f13ae09..6af7b6bd758 100644 --- a/src/main/claude/claude-child-work-producer-harness.test-fixture.ts +++ b/src/main/claude/claude-child-work-producer-harness.test-fixture.ts @@ -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): Delivery[] => { const from = deliveries.length diff --git a/src/main/claude/claude-context-usage-restart.test.ts b/src/main/claude/claude-context-usage-restart.test.ts index d77c9f16fec..305db337b74 100644 --- a/src/main/claude/claude-context-usage-restart.test.ts +++ b/src/main/claude/claude-context-usage-restart.test.ts @@ -142,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 () => { @@ -154,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, diff --git a/src/main/claude/claude-context-usage-unloaded-turn.test.ts b/src/main/claude/claude-context-usage-unloaded-turn.test.ts index 5b24c6428ce..721d084ba1a 100644 --- a/src/main/claude/claude-context-usage-unloaded-turn.test.ts +++ b/src/main/claude/claude-context-usage-unloaded-turn.test.ts @@ -114,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 { + const read = await readAgentSessionHistory(journal, { + sessionId: SESSION, + direction: 'tail', + limit + }) if (!read.ok) { throw new Error('tail read failed') } @@ -125,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 { + const read = await readAgentSessionHistory(journal, { sessionId: SESSION, direction: 'after', cursor: state.cursor ?? undefined @@ -156,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 { + 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 } @@ -176,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) @@ -201,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)) @@ -214,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 @@ -222,7 +229,7 @@ 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() } diff --git a/src/main/claude/claude-subagent-permission-request.test.ts b/src/main/claude/claude-subagent-permission-request.test.ts index beb50b1fb9d..90fdde97440 100644 --- a/src/main/claude/claude-subagent-permission-request.test.ts +++ b/src/main/claude/claude-subagent-permission-request.test.ts @@ -8,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' @@ -125,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[] = [] 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(), @@ -217,13 +226,15 @@ 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, itemId = cardId()) => + (resolution: Pick, itemId?: string) => async (): Promise => { await deferred.drained() + const recorded = itemId ?? (await cardId()) const card = pendingCards(await readTestJournalItems(journal)).find( - (item) => item.itemId === itemId + (item) => item.itemId === recorded ) const identity = card && parseAgentJournalItemKey(card.itemId) if (card?.body.kind !== 'approval' || !identity) { @@ -232,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 => { + const card = pendingCards(await readTestJournalItems(journal))[0] if (!card) { throw new Error('no pending card') } @@ -317,15 +325,15 @@ async function pipeline() { type Pipeline = Awaited> -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, @@ -344,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() @@ -376,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', @@ -387,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', diff --git a/src/main/claude/claude-subagent-resume-after-restart.test.ts b/src/main/claude/claude-subagent-resume-after-restart.test.ts index e0aa9e286c4..35ac4e68b49 100644 --- a/src/main/claude/claude-subagent-resume-after-restart.test.ts +++ b/src/main/claude/claude-subagent-resume-after-restart.test.ts @@ -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 => + (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() }) }) diff --git a/src/main/claude/claude-transcript-catch-up.test.ts b/src/main/claude/claude-transcript-catch-up.test.ts index a7cd47ef62a..ca039692640 100644 --- a/src/main/claude/claude-transcript-catch-up.test.ts +++ b/src/main/claude/claude-transcript-catch-up.test.ts @@ -12,7 +12,6 @@ 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 @@ -148,7 +147,7 @@ describe('Claude chat catch-up', () => { const key = agentJournalItemKey({ provider: 'claude', sessionId: SID, uuid }) expect(await reopened.reads.canonicalItemId(key)).toBe(key) expect( - structuredRewindCut(reopened, { + await structuredRewindCut(reopened, { itemId: agentJournalItemKey({ provider: 'orca', clientMessageId }), provider: 'claude', nativeId: SID @@ -202,31 +201,30 @@ describe('Claude chat catch-up', () => { const key = agentJournalItemKey({ provider: 'claude', sessionId: SID, uuid: 'cmd-1' }) expect(await c.open('a1')).toBe(true) const held = await readTestJournalItems(c.journal) - expect(c.rows().filter((row) => row === 'user:follow up')).toHaveLength(1) + expect((await c.rows()).filter((row) => row === 'user:follow up')).toHaveLength(1) expect((await c.journal.reads.item(key))?.turnScope).toEqual({ kind: 'unknown' }) - expect(isStructuredRewindTarget(c.journal, key)).toBe(false) + 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(await readTestJournalItems(c.journal)).toEqual(held) const beforeReplay = await c.journal.reads.item(key) const reopened = await c.replay() expect(await reopened.reads.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 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: await readTestJournalSnapshot(reopened), - afterSequence: cursor.sequence - }) - if (!batch.ok) { - throw new Error('batch unavailable') - } const snapshotClient = replacePage(page.page, 1) - const liveClient = mergeItems([], batch.batch.items, []) + const liveClient = mergeItems([], delta.page.items, []) expect(snapshotClient.items.find((item) => item.itemId === key)).toEqual( await c.journal.reads.item(key) ) @@ -251,20 +249,17 @@ describe('Claude chat catch-up', () => { 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(await isStructuredRewindTarget(c.journal, opener)).toBe(true) expect(await readTestJournalSubmissions(c.journal)).toEqual(submissions) - expect(c.rows().filter((row) => row === 'user:follow up')).toHaveLength(1) + 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((await c.journal.reads.item(key))?.turnScope).toEqual({ kind: 'unknown' }) + expect( + await structuredRewindCut(c.journal, { + itemId: opener, + provider: 'claude', + nativeId: SID + }) + ).not.toBeNull() expect((await (await c.replay()).reads.item(key))?.turnScope).toEqual({ kind: 'unknown' }) } ) @@ -279,7 +274,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') } ) @@ -350,7 +345,7 @@ describe('Claude chat catch-up', () => { }) expect(await c.journal.reads.item(turn.itemId)).toEqual(turn) expect(await c.journal.reads.item(thinking.itemId)).toEqual(thinking) - expect(c.rows().some((row) => row.startsWith('(continued-outside'))).toBe(false) + 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(await readTestJournalItems(c.journal)).toEqual(completed) @@ -378,7 +373,7 @@ describe('Claude chat catch-up', () => { return tail }) expect(await c.open('a1')).toBe(false) - expect(c.rows()).not.toContain('user:ask 2') + expect(await c.rows()).not.toContain('user:ask 2') const running = (await readTestJournalItems(c.journal)).findLast( (item) => item.body.kind === 'turn' ) @@ -390,11 +385,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') @@ -405,13 +400,13 @@ describe('Claude chat catch-up', () => { 'tool:Read:completed', 'assistant:answer 2' ] - expect(c.rows()).toEqual(caughtUp) + 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() }) @@ -420,7 +415,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[] = [ reply('saved-leaf', 'earlier-prompt', 'msg_saved', { type: 'text', text: 'earlier' }, 0), prompt('p1', 'saved-leaf', 'ask 1', 1), @@ -445,7 +440,7 @@ describe('Claude chat catch-up', () => { const submissions = await readTestJournalSubmissions(c.journal) await c.write(rows) expect(await c.open('saved-leaf')).toBe(true) - expect(c.rows()).toEqual(expected) + expect(await c.rows()).toEqual(expected) expect(await readTestJournalSubmissions(c.journal)).toEqual(submissions) await c.journal.appendLifecycleBatch({ settlementId: 'perf-padding', @@ -553,7 +548,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', @@ -577,7 +572,7 @@ describe('Claude chat catch-up', () => { uuid: 'unknown-input' }) expect((await c.journal.reads.item(unknown))?.turnScope).toEqual({ kind: 'unknown' }) - expect(isStructuredRewindTarget(c.journal, unknown)).toBe(false) + expect(await isStructuredRewindTarget(c.journal, unknown)).toBe(false) const answer = (await readTestJournalItems(c.journal)).find( (item) => item.body.kind === 'message' && @@ -607,7 +602,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}` @@ -619,7 +614,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' }) From cf04da6314c7047fbc05cd5888afef083ec4fcca Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:16:53 -0800 Subject: [PATCH 17/45] Port Codex tests to SQL journals and drop the legacy row rollback test --- ...codex-deferred-sink-target-test-support.ts | 45 +++---- .../codex-provider-retry-idle-sweep.test.ts | 2 +- ...tructured-journal-exit-convergence.test.ts | 60 ++++++--- ...dex-structured-journal-goal-resume.test.ts | 126 ++++++++---------- ...x-structured-journal-goal-revision.test.ts | 24 ++-- ...uctured-journal-repeated-questions.test.ts | 7 +- ...dex-structured-journal-translation.test.ts | 39 ++++-- ...-structured-thread-catch-up-bounds.spec.ts | 2 +- ...ex-structured-thread-catch-up-echo.spec.ts | 62 ++++----- ...structured-thread-catch-up-sources.spec.ts | 9 +- ...tructured-thread-catch-up-variants.spec.ts | 12 +- .../codex-structured-thread-catch-up.test.ts | 49 ++++--- .../codex-structured-thread-goal.test.ts | 23 +--- ...x-thread-catch-up-contract-test-fixture.ts | 29 +++- .../codex-thread-catch-up-rewind.test.ts | 29 ++-- 15 files changed, 277 insertions(+), 241 deletions(-) diff --git a/src/main/codex/structured/journal/codex-deferred-sink-target-test-support.ts b/src/main/codex/structured/journal/codex-deferred-sink-target-test-support.ts index 3c1dc26a731..985723515cb 100644 --- a/src/main/codex/structured/journal/codex-deferred-sink-target-test-support.ts +++ b/src/main/codex/structured/journal/codex-deferred-sink-target-test-support.ts @@ -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 { + 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') } diff --git a/src/main/codex/structured/journal/codex-provider-retry-idle-sweep.test.ts b/src/main/codex/structured/journal/codex-provider-retry-idle-sweep.test.ts index e3d3ebe3fd1..63633493de9 100644 --- a/src/main/codex/structured/journal/codex-provider-retry-idle-sweep.test.ts +++ b/src/main/codex/structured/journal/codex-provider-retry-idle-sweep.test.ts @@ -126,7 +126,7 @@ describe('a Codex reconnecting a dropped stream', () => { expect(host.hasSession(SESSION)).toBe(true) } - const items = (await readTestJournalItems(host['sessions'].get(SESSION)?.journal)) ?? [] + const items = await readTestJournalItems(host['sessions'].get(SESSION)!.journal) const retryRows = items.filter( (item) => item.body.kind === 'status' && item.body.failure?.kind === 'providerRetrying' ) diff --git a/src/main/codex/structured/journal/codex-structured-journal-exit-convergence.test.ts b/src/main/codex/structured/journal/codex-structured-journal-exit-convergence.test.ts index 1298246c6d3..09e308fb01e 100644 --- a/src/main/codex/structured/journal/codex-structured-journal-exit-convergence.test.ts +++ b/src/main/codex/structured/journal/codex-structured-journal-exit-convergence.test.ts @@ -1,4 +1,5 @@ import { + readTestJournalItems, readTestJournalSnapshot, readTestJournalSubmissions } from '../../../native-chat/agent-session-journal/journal-observation.test-support' @@ -26,7 +27,9 @@ import { type StructuredAgentSessionChildExitSession } from '../../../native-chat/agent-session-wire/termination/structured-agent-session-child-exit' import type { StructuredAgentSessionEndedEvent } from '../../../native-chat/agent-session-wire/providers/structured-agent-session-adapter' -import { StructuredAgentSessionCurrentWork } from '../../../native-chat/agent-session-wire/observations/structured-agent-session-current-work' +import { structuredAgentSessionHandoverAuthority } from '../../../native-chat/agent-session-wire/delivery/structured-agent-session-handing-over' +import { agentSessionConversationView } from '../../../../shared/agent-session/lifecycle/agent-session-conversation-view' +import { applyAgentSessionRecordCommand } from '../../../runtime/agent-session/records/agent-session-record-command' import { createCodexJournalTranslator } from './codex-structured-journal-translation' import { handleCodexSessionExit } from '../session-lifecycle/codex-structured-session-close' import type { @@ -55,12 +58,27 @@ afterEach(async () => { } }) -function userVisible(journal: AgentSessionJournal, liveFence: number | null) { - const items = journal.snapshot().items - const work = new StructuredAgentSessionCurrentWork(journal, liveFence) +async function userVisible( + journal: AgentSessionJournal, + session: StructuredAgentSessionChildExitSession, + record: AgentSessionRecord +) { + const items = await readTestJournalItems(journal) + const { state: work } = await journal.readWork( + () => ({ + handle: journal.observationHandle(), + record: agentSessionConversationView(record), + ended: session.lastEndedChild, + child: session.child && { fence: session.child.fence, heldSends: [] }, + ...structuredAgentSessionHandoverAuthority(journal), + openedThrough: journal.openingCursor(), + operationalRevision: 0 + }), + { kind: 'state' } + ) return { - working: work.working(), - prompt: work.hasActionablePrompt(), + working: work.working, + prompt: work.actionablePrompts.length > 0, turnStates: items.flatMap((item) => readAgentJournalTurn(item.body)?.state ?? []), runningCalls: items.filter( (item) => item.body.kind === 'tool-call' && item.body.state === 'running' @@ -68,7 +86,7 @@ function userVisible(journal: AgentSessionJournal, liveFence: number | null) { endedCalls: items.filter( (item) => item.body.kind === 'tool-call' && item.body.state !== 'running' ).length, - send: journal.submission('send-1')?.dispatchState + send: (await journal.reads.submission('send-1'))?.dispatchState } } @@ -153,7 +171,14 @@ it('settles a refused Codex exit through the host in one commit, then permits a ).toEqual({ accepted: true }) await expect(deferred.drained()).resolves.toEqual({ ok: true }) expect(deferred.state().queuedBytes).toBe(0) - expect(userVisible(journal, 7)).toEqual({ + let record: AgentSessionRecord = agentSessionRecordFixture( + agentSessionLeaseFixture({ sessionId: SESSION }) + ) + const hostSession: StructuredAgentSessionChildExitSession = { + child: { generation: 'generation', fence: 7, phase: 'ready' }, + journal + } + expect(await userVisible(journal, hostSession, record)).toEqual({ working: true, prompt: true, turnStates: ['running'], @@ -216,13 +241,6 @@ it('settles a refused Codex exit through the host in one commit, then permits a expect(onFailed).not.toHaveBeenCalled() expect(await readTestJournalSnapshot(journal)).toEqual(before) expect(commits).not.toHaveBeenCalled() - let record: AgentSessionRecord = agentSessionRecordFixture( - agentSessionLeaseFixture({ sessionId: SESSION }) - ) - const hostSession: StructuredAgentSessionChildExitSession = { - child: { generation: 'generation', fence: 7, phase: 'ready' }, - journal - } const [event] = events if (!event) { throw new Error('Codex did not report its observed exit') @@ -232,10 +250,12 @@ it('settles a refused Codex exit through the host in one commit, then permits a logger: recording.logger, store: { getRecord: () => record, - transitionHandoff: async ( - _sessionId: string, - transition: (current: AgentSessionRecord) => AgentSessionRecord - ) => (record = transition(record)) + updateRecord: async (_sessionId, command) => { + const applied = applyAgentSessionRecordCommand(record, command) + record = applied.record + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the store answers each command with its own result. + return applied.result as never + } }, sessions: new Map([[SESSION, hostSession]]), flushLifecycle: () => deferred.lifecycleBarrier(), @@ -245,7 +265,7 @@ it('settles a refused Codex exit through the host in one commit, then permits a }, event ) - expect(userVisible(journal, null)).toEqual({ + expect(await userVisible(journal, hostSession, record)).toEqual({ working: false, prompt: false, turnStates: ['interrupted'], diff --git a/src/main/codex/structured/journal/codex-structured-journal-goal-resume.test.ts b/src/main/codex/structured/journal/codex-structured-journal-goal-resume.test.ts index 656ffc07f78..66d5954c742 100644 --- a/src/main/codex/structured/journal/codex-structured-journal-goal-resume.test.ts +++ b/src/main/codex/structured/journal/codex-structured-journal-goal-resume.test.ts @@ -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 = {}): Record } } -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() - 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 { diff --git a/src/main/codex/structured/journal/codex-structured-journal-goal-revision.test.ts b/src/main/codex/structured/journal/codex-structured-journal-goal-revision.test.ts index aab29309c85..9f71018b696 100644 --- a/src/main/codex/structured/journal/codex-structured-journal-goal-revision.test.ts +++ b/src/main/codex/structured/journal/codex-structured-journal-goal-revision.test.ts @@ -58,7 +58,7 @@ function goalFrame(goal: Record = {}) { function journalSink(journal: AgentSessionJournal) { const deferred = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging()) - const published: AgentSessionHistoryPage[] = [] + const pages: Promise[] = [] let subscriberCursor: AgentJournalCursor | null = null deferred.bind({ journal, @@ -67,14 +67,13 @@ function journalSink(journal: AgentSessionJournal) { if (subscriberCursor === null) { return } - const result = readAgentSessionHistory(journal, { - sessionId: IDENTITY.sessionId, - direction: 'after', - cursor: subscriberCursor - }) - if (result.ok) { - published.push(result.page) - } + pages.push( + readAgentSessionHistory(journal, { + sessionId: IDENTITY.sessionId, + direction: 'after', + cursor: subscriberCursor + }).then((result) => (result.ok ? result.page : null)) + ) } }) return { @@ -83,7 +82,8 @@ function journalSink(journal: AgentSessionJournal) { subscribe: () => { subscriberCursor = journal.cursor() }, - published + published: async () => + (await Promise.all(pages)).filter((page): page is AgentSessionHistoryPage => page !== null) } } @@ -116,7 +116,7 @@ describe('codex goal accounting revisions', () => { }) await drained() expect((await readTestJournalItems(journal))[1]?.revision).toBe(created?.revision) - expect(published).toEqual([]) + expect(await published()).toEqual([]) goals.handle({ threadId: THREAD, @@ -139,7 +139,7 @@ describe('codex goal accounting revisions', () => { timeUsedSeconds: 45, updatedAt: 1789068033_000 }) - expect(published).toEqual([ + expect(await published()).toEqual([ expect.objectContaining({ items: [ expect.objectContaining({ diff --git a/src/main/codex/structured/journal/codex-structured-journal-repeated-questions.test.ts b/src/main/codex/structured/journal/codex-structured-journal-repeated-questions.test.ts index ed6c07bb536..17c782d31cd 100644 --- a/src/main/codex/structured/journal/codex-structured-journal-repeated-questions.test.ts +++ b/src/main/codex/structured/journal/codex-structured-journal-repeated-questions.test.ts @@ -1,3 +1,4 @@ +import type { AgentJournalSnapshot } from '../../../../shared/agent-session/journal/agent-session-journal-types' import { readTestJournalItems, readTestJournalSnapshot @@ -42,7 +43,7 @@ it.each([ now: () => 1_000 }) const onFailed = vi.fn() - const published: ReturnType[] = [] + const published: Promise[] = [] const deferred = createDeferredStructuredAgentSessionEventSink({ ...testEventSinkLogging(SESSION), onFailed @@ -51,7 +52,7 @@ it.each([ journal, fence: 7, publish: () => { - published.push(journal.snapshot()) + published.push(readTestJournalSnapshot(journal)) } }) const translator = createCodexJournalTranslator({ @@ -92,7 +93,7 @@ it.each([ saved.map((item) => (item.body.kind === 'question' ? item.body.question : null)) ).toEqual(questions) expect(saved.map((item) => item.revision)).toEqual(ids[0] === ids[1] ? [2] : [1, 1]) - expect(published.at(-1)).toEqual(await readTestJournalSnapshot(journal)) + expect(await published.at(-1)).toEqual(await readTestJournalSnapshot(journal)) translator.dispose() deferred.close() } diff --git a/src/main/codex/structured/journal/codex-structured-journal-translation.test.ts b/src/main/codex/structured/journal/codex-structured-journal-translation.test.ts index 2324c74fb09..a3f7ed4363a 100644 --- a/src/main/codex/structured/journal/codex-structured-journal-translation.test.ts +++ b/src/main/codex/structured/journal/codex-structured-journal-translation.test.ts @@ -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[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?' }) ]) diff --git a/src/main/codex/structured/threads/codex-structured-thread-catch-up-bounds.spec.ts b/src/main/codex/structured/threads/codex-structured-thread-catch-up-bounds.spec.ts index b05f669e9b1..1a9b88304e1 100644 --- a/src/main/codex/structured/threads/codex-structured-thread-catch-up-bounds.spec.ts +++ b/src/main/codex/structured/threads/codex-structured-thread-catch-up-bounds.spec.ts @@ -50,7 +50,7 @@ export function registerCodexHistoryBoundsTests(input: { ], paginate: false }) - expect(c.texts()).not.toContain('assistant:Must not commit through stale anchor') + expect(await c.texts()).not.toContain('assistant:Must not commit through stale anchor') expect((await c.journal.reads.item(old.itemId))?.body).toMatchObject({ contextUsage: { window: { tokens: 500, capturedAt: 2_000 } } }) diff --git a/src/main/codex/structured/threads/codex-structured-thread-catch-up-echo.spec.ts b/src/main/codex/structured/threads/codex-structured-thread-catch-up-echo.spec.ts index 0ecd699cc2c..1fea2370704 100644 --- a/src/main/codex/structured/threads/codex-structured-thread-catch-up-echo.spec.ts +++ b/src/main/codex/structured/threads/codex-structured-thread-catch-up-echo.spec.ts @@ -29,7 +29,6 @@ import { agentSessionSendBodyFingerprint } from '../../../../shared/agent-sessio import { agentSessionFailureFact } from '../../../../shared/agent-session/failures/agent-session-failure' import { agentSessionFailureWords } from '../../../../shared/agent-session/failures/agent-session-failure-words' import { structuredRewindCut } from '../../../native-chat/agent-session-wire/rewind/structured-rewind-cut' -import { restoreJournal } from '../../../native-chat/agent-session-journal/replay/journal-open' export function registerCodexCatchUpEchoTests(input: { codexWith: typeof catchUpCodexWith @@ -54,12 +53,12 @@ export function registerCodexCatchUpEchoTests(input: { await c.open([], { returnedTurns: [turn(2)], paginate: false }) const whole = [turn(1), turn(2)] await c.open([], { returnedTurns: whole, paginate: false }) - expect(c.texts()).toContain('assistant:answer 1') + expect(await c.texts()).toContain('assistant:answer 1') const before = c.journal.cursor() await c.reopen() await c.open([], { returnedTurns: whole, paginate: false }) expect(c.journal.cursor()).toEqual(before) - expect(c.texts().filter((text) => text === 'assistant:answer 1')).toHaveLength(1) + expect((await c.texts()).filter((text) => text === 'assistant:answer 1')).toHaveLength(1) }) it('uses saved native IDs only for display, preserving sends and drafts through outside echoes and replay', async () => { @@ -96,7 +95,7 @@ export function registerCodexCatchUpEchoTests(input: { turnScope: AGENT_JOURNAL_THREAD_SCOPE }) await (state === 'unknown' - ? c.journal.markPendingSubmissionsUnknown(1) + ? c.journal.write({ kind: 'pending-unknown', fence: 1 }) : c.journal.resolveDispatch({ clientMessageId: state, state: 'rejected', @@ -109,7 +108,7 @@ export function registerCodexCatchUpEchoTests(input: { await c.reopen() const before = await readTestJournalSnapshot(c.journal) const draft = await c.journal.queuedMessages.get('draft') - const receipts = states.map((state) => c.journal.receiptFor(state)) + const receipts = await Promise.all(states.map((state) => c.journal.reads.receiptFor(state))) const native = states.map((state, n) => ({ ...turn(n + 2), items: [ @@ -138,7 +137,7 @@ export function registerCodexCatchUpEchoTests(input: { ] } await c.open([...native, outside]) - expect(c.texts().slice(-4)).toEqual([ + expect((await c.texts()).slice(-4)).toEqual([ 'assistant:answer 3', '(continued-outside)', 'user:continue', @@ -147,8 +146,10 @@ export function registerCodexCatchUpEchoTests(input: { expect((await readTestJournalItems(c.journal)).slice(0, before.items.length)).toEqual( before.items ) - expect(states.map((state) => c.journal.receiptFor(state))).toEqual(receipts) - expect(c.texts().filter((text) => text.includes('historical rendering'))).toEqual([]) + expect(await Promise.all(states.map((state) => c.journal.reads.receiptFor(state)))).toEqual( + receipts + ) + expect((await c.texts()).filter((text) => text.includes('historical rendering'))).toEqual([]) expect(await readTestJournalSubmissions(c.journal)).toEqual(before.submissions) expect(await c.journal.queuedMessages.get('draft')).toEqual(draft) const outsideKey = agentJournalItemKey({ @@ -165,8 +166,6 @@ export function registerCodexCatchUpEchoTests(input: { await c.open([...native, outside]) expect(await readTestJournalSnapshot(c.journal)).toEqual(whole) expect(await c.journal.queuedMessages.get('draft')).toEqual(draft) - const restored = restoreJournal(c.db, SESSION) - expect(restored?.state.queue.cards.get('draft')).toEqual(draft) }) it('keeps native display identity after a real retained cut and disk reopen', async () => { @@ -194,23 +193,24 @@ export function registerCodexCatchUpEchoTests(input: { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE }) - await c.journal.markPendingSubmissionsUnknown(1) + await c.journal.write({ kind: 'pending-unknown', fence: 1 }) await c.open([turn(1), turn(2), turn(3)]) const key = agentJournalItemKey({ provider: 'orca', clientMessageId }) - const cut = structuredRewindCut(c.journal, { - itemId: agentJournalItemKey({ - provider: 'codex', - threadId: THREAD, - turnId: 'turn-3', - ordinal: 0 - }), + const target = agentJournalItemKey({ provider: 'codex', - nativeId: THREAD + threadId: THREAD, + turnId: 'turn-3', + ordinal: 0 }) - if (!cut) { - throw new Error('missing native retention cut') - } - await c.journal.replaceEpochItems('handle_forked', 1, cut.kept) + expect( + await structuredRewindCut(c.journal, { itemId: target, provider: 'codex', nativeId: THREAD }) + ).not.toBeNull() + const items = await readTestJournalItems(c.journal) + const cutAt = items.find((item) => item.itemId === target)!.sequence + await c.replaceEpoch( + 'handle_forked', + items.filter((item) => item.sequence < cutAt) + ) await c.reopen() const prompt = await c.journal.reads.item(key) expect(prompt?.body).toEqual(body) @@ -228,20 +228,20 @@ export function registerCodexCatchUpEchoTests(input: { ] } const before = await readTestJournalSnapshot(c.journal) - const beforeTexts = c.texts() + const beforeTexts = await c.texts() await c.open([turn(1), turn(2), native]) expect(await c.journal.reads.item(key)).toEqual(prompt) expect((await readTestJournalItems(c.journal)).slice(0, before.items.length)).toEqual( before.items ) - expect(c.texts()).toEqual([...beforeTexts, 'assistant:native answer']) + expect(await c.texts()).toEqual([...beforeTexts, 'assistant:native answer']) expect((await c.journal.reads.submission(clientMessageId))?.dispatchState).toBe('unknown') expect(await c.journal.reads.receiptFor(clientMessageId)).toBeNull() const whole = await readTestJournalSnapshot(c.journal) await c.reopen() await c.open([turn(1), turn(2), native]) expect(await readTestJournalSnapshot(c.journal)).toEqual(whole) - await c.journal.replaceEpochItems('handle_forked', 1, []) + await c.replaceEpoch('handle_forked', []) const hidden = c.journal.cursor() const { readCodexHistoryJournalView } = await import('../../history/journal-views/codex-history-journal-view') @@ -379,7 +379,7 @@ export function registerCodexCatchUpEchoTests(input: { } } await open() - expect(c.texts()).toEqual([ + expect(await c.texts()).toEqual([ 'user:native', '(continued-outside)', 'user:ask 1', @@ -437,9 +437,11 @@ export function registerCodexCatchUpEchoTests(input: { } }) await open() - expect(c.texts().at(-1)).toBe('assistant:final missing tail') + expect((await c.texts()).at(-1)).toBe('assistant:final missing tail') await c.reopen() - expect(c.texts().filter((text) => text === 'assistant:completed answer')).toHaveLength(1) - expect(c.texts().at(-1)).toBe('assistant:final missing tail') + expect((await c.texts()).filter((text) => text === 'assistant:completed answer')).toHaveLength( + 1 + ) + expect((await c.texts()).at(-1)).toBe('assistant:final missing tail') }) } diff --git a/src/main/codex/structured/threads/codex-structured-thread-catch-up-sources.spec.ts b/src/main/codex/structured/threads/codex-structured-thread-catch-up-sources.spec.ts index 78bb7e66b59..acdee658e5a 100644 --- a/src/main/codex/structured/threads/codex-structured-thread-catch-up-sources.spec.ts +++ b/src/main/codex/structured/threads/codex-structured-thread-catch-up-sources.spec.ts @@ -108,7 +108,7 @@ export function registerCodexHistorySourcesTests(input: { } } await open() - expect(c.texts()).toEqual(['user:ask 1', 'assistant:A different final answer']) + expect(await c.texts()).toEqual(['user:ask 1', 'assistant:A different final answer']) const rows = await readTestJournalItems(c.journal) expect(new Set(rows.map((row) => row.itemId)).size).toBe(rows.length) expect(rows.find((row) => row.body.kind === 'turn')?.body).toMatchObject({ @@ -365,8 +365,9 @@ export function registerCodexHistorySourcesTests(input: { ] } await open([source]) + const items = await readTestJournalItems(c.journal) const saved = collisions.map(({ id }) => - c.journal.snapshot().items.find(({ body }) => body.kind === 'tool-call' && body.callId === id) + items.find(({ body }) => body.kind === 'tool-call' && body.callId === id) ) expect(saved).toHaveLength(collisions.length) for (const row of saved) { @@ -428,7 +429,7 @@ export function registerCodexHistorySourcesTests(input: { path: 'a', patch: journalPayload('+a') }) - expect(c.texts()).toEqual([ + expect(await c.texts()).toEqual([ '(continued-outside)', '(plan-document)', 'user:ask 2', @@ -480,7 +481,7 @@ export function registerCodexHistorySourcesTests(input: { expect((await readTestJournalItems(c.journal)).slice(0, before.items.length)).toEqual( before.items ) - expect(c.texts().slice(-2)).toEqual(['user:ask 31', 'assistant:answer 31']) + expect((await c.texts()).slice(-2)).toEqual(['user:ask 31', 'assistant:answer 31']) const imports = commits.mock.calls.flatMap(([call]) => call.steps.filter( (step) => diff --git a/src/main/codex/structured/threads/codex-structured-thread-catch-up-variants.spec.ts b/src/main/codex/structured/threads/codex-structured-thread-catch-up-variants.spec.ts index ab603a04e14..65635cdef3d 100644 --- a/src/main/codex/structured/threads/codex-structured-thread-catch-up-variants.spec.ts +++ b/src/main/codex/structured/threads/codex-structured-thread-catch-up-variants.spec.ts @@ -285,7 +285,7 @@ export function registerCodexHistoryVariantsTests(input: { }) } }) - expect(c.texts().filter((text) => !text.startsWith('('))).toEqual([ + expect((await c.texts()).filter((text) => !text.startsWith('('))).toEqual([ 'user:Captured original', 'user:Original question', 'assistant:Independent final', @@ -298,16 +298,16 @@ export function registerCodexHistoryVariantsTests(input: { expect((await c.journal.reads.submission('proof'))?.dispatchState).toBe('accepted') expect(helpers.find('wrong-helper')).toBeUndefined() const row = (ordinal: number) => - c.journal.item( + c.journal.reads.item( agentJournalItemKey({ provider: 'codex', threadId: THREAD, turnId: 'turn-1', ordinal }) ) - expect(row(0)).toBeNull() - expect(row(1)?.body).toMatchObject({ + expect(await row(0)).toBeNull() + expect((await row(1))?.body).toMatchObject({ role: 'user', blocks: [{ type: 'text', text: 'Original question' }] }) - expect(row(3)).toBeNull() - expect(row(4)?.body).toMatchObject({ + expect(await row(3)).toBeNull() + expect((await row(4))?.body).toMatchObject({ role: 'assistant', blocks: [{ type: 'text', text: 'Good after refused slot' }] }) diff --git a/src/main/codex/structured/threads/codex-structured-thread-catch-up.test.ts b/src/main/codex/structured/threads/codex-structured-thread-catch-up.test.ts index 2ea324e8c69..f5ddb3619ca 100644 --- a/src/main/codex/structured/threads/codex-structured-thread-catch-up.test.ts +++ b/src/main/codex/structured/threads/codex-structured-thread-catch-up.test.ts @@ -1,3 +1,4 @@ +import { withTestAgentSessionDatabase } from '../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { readTestJournalItems, readTestJournalSnapshot @@ -351,14 +352,18 @@ describe('Codex chat catch-up', () => { await c.reopen() await c.open([turn(1), turn(2), turn(3)]) expect((await readTestJournalItems(c.journal)).slice(0, before.length)).toEqual(before) - expect(c.texts().slice(-4)).toEqual([ + expect((await c.texts()).slice(-4)).toEqual([ '(continued-outside)', 'user:ask 2', 'assistant:answer 2', 'assistant:answer 3' ]) await c.open([turn(1), turn(2), turn(3), turn(4)]) - expect(c.texts().slice(-3)).toEqual(['(continued-outside)', 'user:ask 4', 'assistant:answer 4']) + expect((await c.texts()).slice(-3)).toEqual([ + '(continued-outside)', + 'user:ask 4', + 'assistant:answer 4' + ]) }) it('commits a 600-message import once, rolls back a later partition failure, and recovers', async () => { @@ -373,21 +378,25 @@ describe('Codex chat catch-up', () => { text: `answer ${n}` })) } - const seq = c.journal.cursor().sequence - c.db.exec( - `CREATE TRIGGER fail_later_partition BEFORE INSERT ON journal_rows WHEN NEW.seq > ${seq + 1} BEGIN SELECT RAISE(ABORT, 'injected later partition failure'); END` + // Let one more item in, then fail the import's later items inside the same command. + await withTestAgentSessionDatabase(c.root, (db) => + db.exec(`CREATE TRIGGER fail_later_partition BEFORE INSERT ON journal_items + WHEN (SELECT COUNT(*) FROM journal_items WHERE session_id = NEW.session_id) > ${ + before.items.length + } + BEGIN SELECT RAISE(ABORT, 'injected later partition failure'); END`) ) expect((await c.open([turn(1), large])).ok).toBe(false) expect(c.deferred.state().failed).toBe(true) expect(await readTestJournalSnapshot(c.journal)).toEqual(before) - c.db.exec('DROP TRIGGER fail_later_partition') + await withTestAgentSessionDatabase(c.root, (db) => db.exec('DROP TRIGGER fail_later_partition')) await c.reopen() expect(await readTestJournalSnapshot(c.journal)).toEqual(before) await c.open([turn(1), large]) - expect(c.texts()).toHaveLength(603) + expect(await c.texts()).toHaveLength(603) await c.reopen() await c.open([turn(1), large]) - expect(c.texts()).toHaveLength(603) + expect(await c.texts()).toHaveLength(603) }) it('keeps rollback reasoning and message identities immutable across turns', async () => { @@ -403,7 +412,7 @@ describe('Codex chat catch-up', () => { const before = await readTestJournalItems(c.journal) await c.open([withReasoning(2)]) expect((await readTestJournalItems(c.journal)).slice(0, before.length)).toEqual(before) - expect(c.texts().filter((text) => text.startsWith('assistant:'))).toEqual([ + expect((await c.texts()).filter((text) => text.startsWith('assistant:'))).toEqual([ 'assistant:answer 1', 'assistant:answer 2' ]) @@ -508,8 +517,9 @@ describe('Codex chat catch-up', () => { fingerprint: 'rewind', now: 500_000 }) - await store.transitionHandoff(SESSION, (record) => ({ - ...record, + await store.updateRecord(SESSION, { + kind: 'rewind', + fence: store.getRecord(SESSION)!.lease.runtimeFence, rewind: { operationId, callerKey, @@ -517,7 +527,7 @@ describe('Codex chat catch-up', () => { expectedEpoch: c.journal.physicalCursor().epoch, phase: 'provider-succeeded' } - })) + }) clock.now = 500_000 const deps = () => ({ store, logger: testEventSinkLogging().logger }) await recoverStructuredRewind(deps(), SESSION, c.journal, 1) @@ -537,9 +547,11 @@ describe('Codex chat catch-up', () => { store = await openTestAgentSessionRecordStore(c.root) await recoverStructuredRewind(deps(), SESSION, c.journal, 1) expect(store.getRecord(SESSION)?.rewind?.phase).toBe('completed') - expect(usableJournalHistoryOrigin(c.journal.historyOrigin())?.importedAt).toBe(200_000) + expect(usableJournalHistoryOrigin(await c.journal.reads.historyOrigin())?.importedAt).toBe( + 200_000 + ) await c.open([dated(100), dated(150), dated(200), dated(250), dated(600)]) - expect(c.texts().filter((text) => text.startsWith('assistant:'))).toEqual([ + expect((await c.texts()).filter((text) => text.startsWith('assistant:'))).toEqual([ 'assistant:old A answer', 'assistant:answer 250', 'assistant:answer 600' @@ -573,7 +585,12 @@ describe('Codex chat catch-up', () => { blocks: [{ type: 'text' as const, text: 'legacy' }] } } - await c.journal.replaceEpochItems('session_created', 1, [kept]) + await c.journal.write({ + kind: 'epoch-replace', + reason: 'session_created', + fence: 1, + items: [kept] + }) await c.reopen() await c.journal.appendItem( { provider: 'codex', threadId: THREAD, turnId: 'native', ordinal: 0 }, @@ -581,7 +598,7 @@ describe('Codex chat catch-up', () => { { fence: 1, turnScope: { kind: 'thread' } } ) await c.open([turn(3)]) - expect(c.texts()).toEqual(['user:legacy', 'assistant:ordinary reply']) + expect(await c.texts()).toEqual(['user:legacy', 'assistant:ordinary reply']) }) }) diff --git a/src/main/codex/structured/threads/codex-structured-thread-goal.test.ts b/src/main/codex/structured/threads/codex-structured-thread-goal.test.ts index 94e2bec01a1..b64c5bec9fb 100644 --- a/src/main/codex/structured/threads/codex-structured-thread-goal.test.ts +++ b/src/main/codex/structured/threads/codex-structured-thread-goal.test.ts @@ -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() } diff --git a/src/main/codex/structured/threads/codex-thread-catch-up-contract-test-fixture.ts b/src/main/codex/structured/threads/codex-thread-catch-up-contract-test-fixture.ts index a8df282e1d6..547d0e390f3 100644 --- a/src/main/codex/structured/threads/codex-thread-catch-up-contract-test-fixture.ts +++ b/src/main/codex/structured/threads/codex-thread-catch-up-contract-test-fixture.ts @@ -3,12 +3,14 @@ import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, expect } from 'vitest' +import type { AgentJournalRenderItem } from '../../../../shared/agent-session/journal/agent-session-journal-types' +import { parseAgentJournalItemKey } from '../../../../shared/agent-session/journal/agent-session-journal-item-key' +import type { JournalWriterOperation } from '../../../native-chat/agent-session-journal/journal-writer-command' import type { AgentSessionJournalIdentity } from '../../../../shared/agent-session/journal/agent-session-journal-identity' import { codexProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import { closeTestJournalHostDatabase, - createTrackedJournalOpener, - openTestJournalHostDatabase + createTrackedJournalOpener } from '../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import { createDeferredStructuredAgentSessionEventSink } from '../../../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink' import { testEventSinkLogging } from '../../../native-chat/agent-session-wire/host/testing/structured-agent-session-logger.test-fixture' @@ -95,14 +97,12 @@ export async function chat(clock: { now: number }) { return deferred }, root, - get db() { - return openTestJournalHostDatabase(root).db - }, reopen: async () => { deferred.close() await journal.close() await closeTestJournalHostDatabase(root) - journal = await journals.open(journalOptions) + // A reopen recovers interrupted sends, as the host's conversation open does. + journal = await journals.open({ ...journalOptions, recover: { fence: 1 } }) deferred = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging()) bind() }, @@ -119,6 +119,23 @@ export async function chat(clock: { now: number }) { }) return deferred.drained() }, + /** Replace the live epoch with these saved items, as a retained cut or fork commits it. */ + replaceEpoch: async ( + reason: Extract['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') { diff --git a/src/main/codex/structured/threads/codex-thread-catch-up-rewind.test.ts b/src/main/codex/structured/threads/codex-thread-catch-up-rewind.test.ts index bba5eac3734..acb3bb9beb4 100644 --- a/src/main/codex/structured/threads/codex-thread-catch-up-rewind.test.ts +++ b/src/main/codex/structured/threads/codex-thread-catch-up-rewind.test.ts @@ -109,8 +109,9 @@ it('retains the original adoption cutoff through native rewind and disk recovery fingerprint: 'rewind', now: 500_000 }) - await store.transitionHandoff(SESSION, (record) => ({ - ...record, + await store.updateRecord(SESSION, { + kind: 'rewind', + fence: store.getRecord(SESSION)!.lease.runtimeFence, rewind: { operationId, callerKey, @@ -118,24 +119,23 @@ it('retains the original adoption cutoff through native rewind and disk recovery expectedEpoch: c.journal.physicalCursor().epoch, phase: 'provider-succeeded' } - })) + }) clock.now = 500_000 const deps = () => ({ store, logger: testEventSinkLogging().logger }) await recoverStructuredRewind(deps(), SESSION, c.journal, 1) const restated = (await readTestJournalItems(c.journal)).slice(0, original.length) expect(restated).toHaveLength(original.length) - for (const [index, item] of restated.entries()) { - const previous = original[index]! - expect(item).toEqual({ ...previous, revision: previous.revision + 2, sequence: item.sequence }) - expect(item.sequence).toBeGreaterThan(previous.sequence) - } + // The cut keeps the imported rows exactly as saved. + expect(restated).toEqual(original) await c.reopen() store = await openTestAgentSessionRecordStore(c.root) await recoverStructuredRewind(deps(), SESSION, c.journal, 1) expect(store.getRecord(SESSION)?.rewind?.phase).toBe('completed') - expect(usableJournalHistoryOrigin(c.journal.historyOrigin())?.importedAt).toBe(200_000) + expect(usableJournalHistoryOrigin(await c.journal.reads.historyOrigin())?.importedAt).toBe( + 200_000 + ) await c.open([dated(100), dated(150), dated(200), dated(250), dated(600)]) - expect(c.texts().filter((text) => text.startsWith('assistant:'))).toEqual([ + expect((await c.texts()).filter((text) => text.startsWith('assistant:'))).toEqual([ 'assistant:old A answer', 'assistant:answer 250', 'assistant:answer 600' @@ -158,7 +158,12 @@ it('keeps erased-origin legacy chats readable without guessing from native IDs', blocks: [{ type: 'text' as const, text: 'legacy' }] } } - await c.journal.replaceEpochItems('session_created', 1, [kept]) + await c.journal.write({ + kind: 'epoch-replace', + reason: 'session_created', + fence: 1, + items: [kept] + }) await c.reopen() await c.journal.appendItem( { provider: 'codex', threadId: THREAD, turnId: 'native', ordinal: 0 }, @@ -166,5 +171,5 @@ it('keeps erased-origin legacy chats readable without guessing from native IDs', { fence: 1, turnScope: { kind: 'thread' } } ) await c.open([turn(3)]) - expect(c.texts()).toEqual(['user:legacy', 'assistant:ordinary reply']) + expect(await c.texts()).toEqual(['user:legacy', 'assistant:ordinary reply']) }) From a838d3962cea55a519a5721a4732c83edc2461ad Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:20:24 -0800 Subject: [PATCH 18/45] Port failure and persisted-read fixtures to the active chat database --- ...epoch-replacement-contract-test-fixture.ts | 140 ------------- .../journal-row-context-usage.spec.ts | 80 +++++--- .../journal-storage-admission.test.ts | 132 ------------- ...ournal-terminal-validation-test-fixture.ts | 184 ++++-------------- .../journal-input-reads.test.ts | 74 ++----- .../queued-message-stop-pause.spec.ts | 2 +- .../replay/journal-pause-mark-restore.test.ts | 2 +- ...atomic-settlement-contract-test-fixture.ts | 127 ------------ ...agent-session-attachment-admission.test.ts | 18 +- ...ured-agent-session-attention-reads.test.ts | 100 ++++------ ...ctured-agent-session-exit-delivery.test.ts | 1 + ...ession-leftover-settlement.test-fixture.ts | 29 ++- ...ent-session-prompt-answer-receipts.test.ts | 13 +- ...sion-queue-import-contract.test-fixture.ts | 2 +- ...ent-session-queued-action-receipts.test.ts | 18 +- ...-agent-session-queued-repeat-press.test.ts | 15 +- ...ctured-agent-session-resend-answer.test.ts | 6 +- ...gent-session-resend-ledger-refusal.test.ts | 7 +- ...ent-session-restart-failure-filing.test.ts | 14 +- ...t-session-restart-offer-newer-orca.test.ts | 132 ------------- ...session-rewind-usage-limit.test-fixture.ts | 15 +- .../structured-conversation-command.test.ts | 15 +- ...-agent-session-transition-rollback.test.ts | 14 +- ...tructured-agent-session-transition.test.ts | 8 +- ...ssion-subscriber-oversized-removal.test.ts | 108 ---------- ...tured-agent-session-wire-admission.test.ts | 6 +- .../structured-agent-session-runtime.test.ts | 16 +- .../orchestration-cli-subprocess.test.ts | 65 +++---- .../agent-launch-cwd-resources.test.ts | 24 +-- ...rker-start-card-population.test-fixture.ts | 55 ------ .../structured-agent-session-at-rest.test.ts | 153 ++++----------- ...d-agent-session-rpc-status.test-fixture.ts | 27 ++- ...ructured-agent-session-rpc.test-fixture.ts | 5 +- 33 files changed, 366 insertions(+), 1241 deletions(-) delete mode 100644 src/main/native-chat/agent-session-journal/context-epochs/journal-epoch-replacement-contract-test-fixture.ts delete mode 100644 src/main/native-chat/agent-session-journal/durable-rows/journal-storage-admission.test.ts delete mode 100644 src/main/native-chat/agent-session-journal/writing/journal-atomic-settlement-contract-test-fixture.ts delete mode 100644 src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-offer-newer-orca.test.ts delete mode 100644 src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-subscriber-oversized-removal.test.ts delete mode 100644 src/main/runtime/rpc/methods/orchestration/worker-start-card-population.test-fixture.ts diff --git a/src/main/native-chat/agent-session-journal/context-epochs/journal-epoch-replacement-contract-test-fixture.ts b/src/main/native-chat/agent-session-journal/context-epochs/journal-epoch-replacement-contract-test-fixture.ts deleted file mode 100644 index 4fa7e784d76..00000000000 --- a/src/main/native-chat/agent-session-journal/context-epochs/journal-epoch-replacement-contract-test-fixture.ts +++ /dev/null @@ -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 - } -} diff --git a/src/main/native-chat/agent-session-journal/durable-rows/journal-row-context-usage.spec.ts b/src/main/native-chat/agent-session-journal/durable-rows/journal-row-context-usage.spec.ts index dbec17abd82..f2ebc1caa94 100644 --- a/src/main/native-chat/agent-session-journal/durable-rows/journal-row-context-usage.spec.ts +++ b/src/main/native-chat/agent-session-journal/durable-rows/journal-row-context-usage.spec.ts @@ -9,16 +9,13 @@ 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 { seedTestJournalHistoricalRows } from '../replay/journal-historical-inputs.test-support' import { claudeProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' export function registerJournalRowContextUsageTests(): void { @@ -61,7 +58,7 @@ export function registerJournalRowContextUsageTests(): void { let root: string let clock = 1_000 - const journals = createTrackedJournalOpener({ legacyInputs: true }) + const journals = createTrackedJournalOpener() const open = () => journals.open({ @@ -71,10 +68,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, @@ -94,26 +87,55 @@ 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' }, + { + 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((await readTestJournalItems(reopened)).map((item) => item.body)).toEqual([ diff --git a/src/main/native-chat/agent-session-journal/durable-rows/journal-storage-admission.test.ts b/src/main/native-chat/agent-session-journal/durable-rows/journal-storage-admission.test.ts deleted file mode 100644 index 5bb9889e5fe..00000000000 --- a/src/main/native-chat/agent-session-journal/durable-rows/journal-storage-admission.test.ts +++ /dev/null @@ -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 { - 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.physicalCursor().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' - ) - ) -) diff --git a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-validation-test-fixture.ts b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-validation-test-fixture.ts index e53e4d7243f..1629064e5d2 100644 --- a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-validation-test-fixture.ts +++ b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-validation-test-fixture.ts @@ -1,71 +1,60 @@ -import { readTestJournalItems, readTestJournalSnapshot } from '../journal-observation.test-support' 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 { afterEach, beforeEach, expect } from 'vitest' 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 { seedTestJournalHistoricalRows } from '../replay/journal-historical-inputs.test-support' +import type { JournalRow } 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 journals = createTrackedJournalOpener() 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') - }, + const savedIdentity = { + sessionId: 'validation', + workspaceId: 'folder', + hostId: 'host', + agent: 'codex' as const, + providerHandle: codexProviderHandle('thread') + } + let root: string + let journal: AgentSessionJournal + beforeEach(async () => { + root = await mkdtemp(join(tmpdir(), 'orca-settlement-validation-')) + }) + afterEach(async () => { + await journals.closeAll() + await rm(root, { recursive: true, force: true }) + }) + async function saveOldItems(inputs: { itemId: string; revision?: number; body: unknown }[]) { + const epoch = 'saved-approval' + const rows: JournalRow[] = [ + { v: 3, kind: 'epoch', epoch, seq: 1, ts: 900, fence: 7, reason: 'session_created' } + ] + for (const [index, input] of inputs.entries()) { + rows.push({ + v: 3, + kind: 'item', + epoch, + seq: index + 2, + ts: 900, + fence: 7, + revision: 1, + ...input, + turnScope: { kind: 'thread' } + } as JournalRow) + } + seedTestJournalHistoricalRows(openTestJournalHostDatabase(root).db, savedIdentity, rows) + journal = await journals.open({ + identity: savedIdentity, 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( @@ -77,64 +66,6 @@ export function createFixture() { }) ).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.physicalCursor().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 = (await readTestJournalItems(journal)).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( - (await readTestJournalItems(journal)).find((item) => item.itemId === saved.itemId) - ).toEqual(before) - expect(await journal.reads.itemBody('orca:advanceable')).toMatchObject({ state: 'interrupted' }) - expect(await journal.reads.itemBody('orca:approval')).toMatchObject({ - resolution: { state: 'cancelled' } - }) - expect(liveTestJournalRows(db, 'validation').slice(0, history.length)).toEqual(history) - const settled = await readTestJournalSnapshot(journal) - await settle() - expect(await readTestJournalSnapshot(journal)).toEqual(settled) - expect(commits).toHaveBeenCalledTimes(1) - await journal.close() - journal = await open() - expect(await readTestJournalSnapshot(journal)).toEqual(settled) - } - function pendingApproval() { return { kind: 'approval', @@ -144,42 +75,13 @@ export function createFixture() { 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 + pendingApproval, + expectSendStillWritable } } diff --git a/src/main/native-chat/agent-session-journal/journal-input-reads.test.ts b/src/main/native-chat/agent-session-journal/journal-input-reads.test.ts index 310e9e36e80..8efe6f77c62 100644 --- a/src/main/native-chat/agent-session-journal/journal-input-reads.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-input-reads.test.ts @@ -1,63 +1,30 @@ import { expect, it } from 'vitest' -import Database from '../../sqlite/sync-database' import { agentSessionFailureFact } from '../../../shared/agent-session/failures/agent-session-failure' import { agentSessionFailureWords } from '../../../shared/agent-session/failures/agent-session-failure-words' -import { journalDatabasePath } from './database/journal-host-database' -import { convertJournalSqlProjection } from './journal-domain-single-connection-import.test-fixture' -import { readJournalSqlProjection } from './journal-sql-read-transaction' import type { JournalInputRead, JournalInputReadResult } from './journal-input-reads' -import { readJournalSqlInput } from './journal-sql-input-reads' import { createFixture } from './queued-messages/journal-unsent-send-hold-contract-test-fixture' -import { openTestJournalHostDatabase } from './store-lifecycle/journal-host-database-test-support' const fixture = createFixture() const READS: JournalInputRead[] = [ - { kind: 'first-queued' }, - { kind: 'entered-submissions' }, - { kind: 'sending-submissions' }, { kind: 'recoverable-submissions' }, { kind: 'pending-submissions' }, + { kind: 'accepted-submissions' }, + { kind: 'unsettled-submissions' }, + { kind: 'capture-keys', present: ['rendered-only', 'orca:accepted'] }, { kind: 'recovered-unknown-ids' }, { kind: 'linked-to', messageId: 'card' }, - { kind: 'linked-to', messageId: 'missing' }, - { kind: 'latest-accepted-handover' }, - { kind: 'input-for-item', itemId: 'orca:accepted' }, - { kind: 'input-for-item', itemId: 'orca:missing' }, - { kind: 'input-ordinal', clientMessageId: 'unknown' }, { kind: 'input-ordinal', clientMessageId: 'missing' }, { kind: 'input-count' }, - { kind: 'sent-after', sequence: 0 }, - { kind: 'sent-after', sequence: 1_000 }, - { kind: 'accepted-after', sequence: 0 }, - { kind: 'accepted-inputs-after', sequence: 0 }, { kind: 'signed-out-at', fence: 0 }, - { kind: 'signed-out-at', fence: 7 }, - { kind: 'submission-at', sequence: 1_000 }, - { kind: 'stopped-history-inputs', excluding: 'accepted' }, - { kind: 'newest-turn' }, - { - kind: 'batch', - reads: [{ kind: 'linked-to', messageId: 'card' }, { kind: 'latest-accepted-handover' }] - }, - { kind: 'accepted-submissions' }, - { kind: 'capture-keys', present: ['rendered-only', 'orca:accepted'] }, - { kind: 'unsettled-submissions' }, - { - kind: 'batch', - reads: [ - { kind: 'accepted-submissions' }, - { kind: 'capture-keys', present: [] }, - { kind: 'unsettled-submissions' } - ] - } + { kind: 'signed-out-at', fence: 7 } ] function ids(result: JournalInputReadResult): string[] { return 'submissions' in result ? result.submissions.map((entry) => entry.clientMessageId) : [] } -it('answers every input-evidence read from a readonly snapshot exactly as the in-memory chat does', async () => { +it('keeps recovery, acceptance, linkage and sign-out evidence available after reopen', async () => { const journal = await fixture.open() for (const id of ['accepted', 'pending', 'signed-out', 'unknown']) { await fixture.accept(journal, id) @@ -99,27 +66,11 @@ it('answers every input-evidence read from a readonly snapshot exactly as the in fence: 0 }) expect(before.keys).not.toContain('orca:legacy%3Araw%2F%25') - const memory = await Promise.all(READS.map((read) => journal.reads.input(read))) await journal.close() - - openTestJournalHostDatabase(fixture.root).transaction((db) => - convertJournalSqlProjection(db, fixture.IDENTITY.sessionId) - ) - const db = new Database(journalDatabasePath(fixture.root), { readonly: true }) - try { - const sql = READS.map((read) => - readJournalSqlProjection(db, fixture.IDENTITY.sessionId, (scope) => - readJournalSqlInput(scope, read) - ) - ) - // Answers cross a worker port, so none may hold a storage proxy. - expect(structuredClone(sql)).toEqual(sql) - expect(sql).toEqual(memory) - } finally { - db.close() - } - - const answer = (kind: JournalInputRead['kind']) => memory[READS.findIndex((r) => r.kind === kind)] + const reopened = await fixture.open() + const answers = await Promise.all(READS.map((read) => reopened.reads.input(read))) + const answer = (kind: JournalInputRead['kind']) => + answers[READS.findIndex((r) => r.kind === kind)] expect(ids(answer('recoverable-submissions'))).toEqual(['pending', 'linked', 'legacy:raw/%']) expect(ids(answer('pending-submissions'))).toEqual(['pending', 'linked', 'legacy:raw/%']) expect(ids(answer('accepted-submissions'))).toEqual([ @@ -129,7 +80,6 @@ it('answers every input-evidence read from a readonly snapshot exactly as the in 'unknown', 'linked' ]) - expect(answer('accepted-submissions')).toMatchObject({ latestReceiptSequence: 6 }) expect(ids(answer('unsettled-submissions'))).toEqual([ 'pending', 'unknown', @@ -153,8 +103,8 @@ it('answers every input-evidence read from a readonly snapshot exactly as the in ids: ['unknown'] }) expect(ids(answer('linked-to'))).toEqual(['linked']) - expect(memory[12]).toEqual({ kind: 'input-ordinal', ordinal: Infinity }) + expect(answers[7]).toEqual({ kind: 'input-ordinal', ordinal: Infinity }) expect(answer('input-count')).toEqual({ kind: 'input-count', count: 6 }) - expect(memory[18]).toEqual({ kind: 'signed-out-at', answer: true }) - expect(memory[19]).toEqual({ kind: 'signed-out-at', answer: false }) + expect(answers[9]).toEqual({ kind: 'signed-out-at', answer: true }) + expect(answers[10]).toEqual({ kind: 'signed-out-at', answer: false }) }) diff --git a/src/main/native-chat/agent-session-journal/queued-messages/queued-message-stop-pause.spec.ts b/src/main/native-chat/agent-session-journal/queued-messages/queued-message-stop-pause.spec.ts index b6b3a6f836a..a907db5e732 100644 --- a/src/main/native-chat/agent-session-journal/queued-messages/queued-message-stop-pause.spec.ts +++ b/src/main/native-chat/agent-session-journal/queued-messages/queued-message-stop-pause.spec.ts @@ -153,7 +153,7 @@ export function registerQueuedMessageStopPauseTests( }) it('a retired accepted outcome still proves a later personal turn lifted Stop', async () => { - const row = (seq: number) => ({ v: 3, epoch: 'historical', seq, fence: 0, ts: 1_000 }) + const row = (seq: number) => ({ v: 4, epoch: 'historical', seq, fence: 0, ts: 1_000 }) seedTestJournalHistoricalRows( openTestJournalHostDatabase(fixture.root).db, fixture.IDENTITY, diff --git a/src/main/native-chat/agent-session-journal/replay/journal-pause-mark-restore.test.ts b/src/main/native-chat/agent-session-journal/replay/journal-pause-mark-restore.test.ts index 55a5bc1fe42..fbc7e440081 100644 --- a/src/main/native-chat/agent-session-journal/replay/journal-pause-mark-restore.test.ts +++ b/src/main/native-chat/agent-session-journal/replay/journal-pause-mark-restore.test.ts @@ -25,7 +25,7 @@ const identity = { agent: 'codex' as const, providerHandle: codexProviderHandle('thread') } -const row = (seq: number) => ({ v: 3, epoch: 'historical', seq, fence: 0, ts: 1_000 }) +const row = (seq: number) => ({ v: 4, epoch: 'historical', seq, fence: 0, ts: 1_000 }) const rows: JournalRow[] = [ { ...row(1), diff --git a/src/main/native-chat/agent-session-journal/writing/journal-atomic-settlement-contract-test-fixture.ts b/src/main/native-chat/agent-session-journal/writing/journal-atomic-settlement-contract-test-fixture.ts deleted file mode 100644 index e79c76cf8f7..00000000000 --- a/src/main/native-chat/agent-session-journal/writing/journal-atomic-settlement-contract-test-fixture.ts +++ /dev/null @@ -1,127 +0,0 @@ -import { mkdtemp, rm } from 'node:fs/promises' -import { tmpdir } from 'node:os' -import { join } from 'node:path' -import { afterEach, beforeEach, 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 { settleObservedExitForTest } from '../../agent-session-wire/termination/testing/structured-agent-session-observed-exit.test-fixture' -import { - createTrackedJournalOpener, - openTestJournalHostDatabase, - readTestJournalRows -} 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 SESSION = 'atomic-settlement' - - const journals = createTrackedJournalOpener({ legacyInputs: true }) - - let root: string - - let journal: AgentSessionJournal - - async function open(sessionId = SESSION) { - return journals.open({ - identity: { - sessionId, - workspaceId: 'folder-workspace', - hostId: 'host', - agent: 'codex', - providerHandle: codexProviderHandle('thread') - }, - stateDirectory: root, - now: () => 1_000 - }) - } - - beforeEach(async () => { - root = await mkdtemp(join(tmpdir(), 'orca-atomic-settlement-')) - journal = await open() - }) - - afterEach(async () => { - vi.restoreAllMocks() - await journals.closeAll() - await rm(root, { recursive: true, force: true }) - }) - - function rows() { - return readTestJournalRows( - openTestJournalHostDatabase(root).db, - SESSION, - journal.physicalCursor().epoch - ).map((stored) => { - const parsed = parseJournalRow(stored.rowJson) - if (!parsed.ok) { - throw new Error('test row failed to parse') - } - return parsed.row - }) - } - - async function seedWork(count = 201) { - await journal.appendSubmission({ - clientMessageId: 'send', - payloadFingerprint: 'fingerprint', - body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'continue' }] }, - fence: 7 - }) - const turn = await journal.appendItem( - { provider: 'orca', clientMessageId: 'turn' }, - { kind: 'turn', turnId: 'turn', state: 'running', startedAt: 900 }, - { fence: 7, turnScope: AGENT_JOURNAL_THREAD_SCOPE } - ) - for (let index = 0; index < count; index++) { - await journal.appendItem( - { provider: 'orca', clientMessageId: `call-${index}` }, - { - kind: 'tool-call', - name: 'Write', - input: { content: index === 0 ? '界😀'.repeat(220_000) : 'small' }, - state: 'running' - }, - { - fence: 7, - turnScope: { kind: 'turn', turnItemId: turn.itemId }, - agentId: 'child', - producerKind: 'agent' - } - ) - } - } - - function settle() { - return settleObservedExitForTest({ - journal, - sessionId: SESSION, - fence: 7, - settlementId: `exit:${SESSION}:7`, - verdict: { state: 'interrupted', completedAt: 1_000 }, - pendingSubmissionReason: 'provider_exited_before_acknowledgement', - showUnexpectedExitOutcome: true - }) - } - - return { - SESSION, - get journal() { - return journal - }, - set journal(value: typeof journal) { - journal = value - }, - journals, - open, - get root() { - return root - }, - set root(value: typeof root) { - root = value - }, - rows, - seedWork, - settle - } -} diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attachment-admission.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attachment-admission.test.ts index aadcfe638bf..0d782c8fbb0 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attachment-admission.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attachment-admission.test.ts @@ -7,7 +7,7 @@ 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 { agentSessionAttachmentStoreRoot } from '../../../agent-session-attachments/agent-session-attachment-references' -import { openTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { withTestAgentSessionDatabase } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { createQueuedMessageTestRig, QUEUED_RIG_CALLER as CALLER, @@ -49,13 +49,13 @@ function clientSend(body: AgentJournalMessageItem, delivery?: 'queue-if-active') }) } -function claimedSessions(): string[] { - return openTestJournalHostDatabase(rig.root) - .db.prepare('SELECT session_id FROM agent_session_attachment_claims ORDER BY session_id') - .all() - .flatMap((row) => - typeof row === 'object' && row !== null && 'session_id' in row ? [String(row.session_id)] : [] - ) +function claimedSessions(): Promise { + return withTestAgentSessionDatabase(rig.root, (db) => + db + .prepare('SELECT session_id FROM agent_session_attachment_claims ORDER BY session_id') + .all() + .map((row) => String(row.session_id)) + ) } describe('attachments at send admission', () => { @@ -82,7 +82,7 @@ describe('attachments at send admission', () => { } }) expect((await readTestHostJournal(rig.host, SESSION)).submissions).toHaveLength(0) - expect(claimedSessions()).toEqual([]) + expect(await claimedSessions()).toEqual([]) expect(existsSync(fresh)).toBe(true) }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attention-reads.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attention-reads.test.ts index a6c8d3834b4..08ec9fad585 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attention-reads.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-attention-reads.test.ts @@ -4,13 +4,9 @@ import type { AgentSessionTurnCompletionEvent } from '../../../../../shared/agen import { structuredAttentionOrigin } from '../../../../../shared/agent-session/agent-session-attention' import { agentJournalSubmissionKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' import { createAnnouncedNotificationRegistry } from '../../../../notifications/announced-notification-registry' -import * as journalRestore from '../../../agent-session-journal/replay/journal-open' import * as journalFactory from '../../../agent-session-journal/journal-store-factory' import type { AgentSessionExecutionLocation } from '../../../../../shared/agent-session/lifecycle/agent-session-record' -import { - loadTestJournal, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { journalSqlReads } from '../../../agent-session-journal/journal-sql-reads' import { HOST_TEST_LOCATION, HOST_TEST_SESSION as SESSION, @@ -44,17 +40,14 @@ it.each(['git-worktree', 'folder'] as const)( ).toMatchObject({ ok: true }) await vi.waitFor(() => expect(rig.adapter.dispatch).toHaveBeenCalled()) await rig.restart() - const stored = loadTestJournal(rig.root, SESSION) - if (!stored) { - throw new Error('the conversation has no stored journal') - } + const stored = await journalSqlReads(rig.host.deps.journalDatabase.readers, SESSION).cursor() const acquisitions = rig.adapter.acquire.mock.calls.length expect(await rig.host.attentionReads.workspace(location)).toEqual([ { sessionId: SESSION, scope: location, - observedCursor: { epoch: stored.state.epoch, sequence: stored.state.lastSequence } + observedCursor: stored } ]) for (const other of [ @@ -78,56 +71,44 @@ it.each(['git-worktree', 'folder'] as const)( } ) -it.each(['resting-newer-format', 'live-closed'] as const)( - 'keeps sibling attention readable when a %s journal cannot supply its boundary', - async (failure) => { - const rig = await createRestTestRig() - const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined) - const damaged = 'session-unreadable' - try { - const attached = await rig.host.attach(REST_TEST_CALLER, hostTestAttachParams(null)) - expect(attached.ok).toBe(true) - await rig.store.reserveOwner({ - sessionId: damaged, - location: HOST_TEST_LOCATION, - provider: 'codex', - accountHome: { variable: 'CODEX_HOME', path: '/home/dev/.codex' }, - expectedFence: null, - spawnToken: 'unreadable-chat', - claimKeyId: 'key-1', - handoffOperationId: null, - probe: { outcome: 'reservation-unused' }, - operation: { - callerKey: 'fixture', - operationId: hostTestOperationId(), - fingerprint: '1'.repeat(64) - }, - now: rig.clock.now - }) - expect(await rig.host.revealSession(damaged)).toMatchObject({ readable: true }) - if (failure === 'resting-newer-format') { - await rig.restart() - } - const accepted = (await rig.host.attentionReads.workspace(HOST_TEST_LOCATION)).find( - (read) => read.sessionId === SESSION - ) - expect(accepted).toBeDefined() - if (failure === 'resting-newer-format') { - openTestJournalHostDatabase(rig.root) - .db.prepare('UPDATE journal_outcome_epochs SET format = format + 1 WHERE session_id = ?') - .run(damaged) - } else { - await rig.host.collaboratorsForTests().sessions.get(damaged)?.journal.close() - } +it('keeps sibling attention readable when a closed live journal cannot supply its boundary', async () => { + const rig = await createRestTestRig() + const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined) + const damaged = 'session-unreadable' + try { + const attached = await rig.host.attach(REST_TEST_CALLER, hostTestAttachParams(null)) + expect(attached.ok).toBe(true) + await rig.store.reserveOwner({ + sessionId: damaged, + location: HOST_TEST_LOCATION, + provider: 'codex', + accountHome: { variable: 'CODEX_HOME', path: '/home/dev/.codex' }, + expectedFence: null, + spawnToken: 'unreadable-chat', + claimKeyId: 'key-1', + handoffOperationId: null, + probe: { outcome: 'reservation-unused' }, + operation: { + callerKey: 'fixture', + operationId: hostTestOperationId(), + fingerprint: '1'.repeat(64) + }, + now: rig.clock.now + }) + expect(await rig.host.revealSession(damaged)).toMatchObject({ readable: true }) + const accepted = (await rig.host.attentionReads.workspace(HOST_TEST_LOCATION)).find( + (read) => read.sessionId === SESSION + ) + expect(accepted).toBeDefined() + await rig.host.collaboratorsForTests().sessions.get(damaged)?.journal.close() - expect(await rig.host.attentionReads.workspace(HOST_TEST_LOCATION)).toEqual([accepted]) - expect(warn).toHaveBeenCalled() - } finally { - await rig.dispose() - warn.mockRestore() - } + expect(await rig.host.attentionReads.workspace(HOST_TEST_LOCATION)).toEqual([accepted]) + expect(warn).toHaveBeenCalled() + } finally { + await rig.dispose() + warn.mockRestore() } -) +}) it('acknowledges a post-rewind completion after normal idle retirement without reopening history', async () => { const rig = await createRestTestRig() @@ -236,7 +217,6 @@ it('acknowledges a post-rewind completion after normal idle retirement without r }) const acquisitions = rig.adapter.acquire.mock.calls.length const reopen = vi.spyOn(journalFactory, 'openAgentSessionJournal') - const restore = vi.spyOn(journalRestore, 'restoreJournal') try { const reads = await rig.host.attentionReads.workspace(HOST_TEST_LOCATION) expect( @@ -251,12 +231,10 @@ it('acknowledges a post-rewind completion after normal idle retirement without r { sessionId: SESSION, scope: HOST_TEST_LOCATION, observedCursor: retiredCursor } ]) expect(reopen).not.toHaveBeenCalled() - expect(restore).not.toHaveBeenCalled() expect(rig.adapter.acquire).toHaveBeenCalledTimes(acquisitions) expect(rig.host.loadedSessionIds()).toEqual([]) } finally { reopen.mockRestore() - restore.mockRestore() } } finally { await rig.dispose() diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-exit-delivery.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-exit-delivery.test.ts index 06a5bead83c..7e7d48383ee 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-exit-delivery.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-exit-delivery.test.ts @@ -69,6 +69,7 @@ async function watchChat(current: QueuedMessageTestRig) { }) } }) + await eventually(() => expect(reads.length).toBeGreaterThan(0)) return reads } diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-leftover-settlement.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-leftover-settlement.test-fixture.ts index 9cb983c2638..91ad8f92c39 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-leftover-settlement.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-leftover-settlement.test-fixture.ts @@ -3,10 +3,8 @@ import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../shared/agent-session/ import type { AgentJournalItemIdentity } from '../../../../../shared/agent-session/journal/agent-session-journal-identity' import { agentJournalItemKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' import { readAgentJournalTurn } from '../../../../../shared/agent-session/lifecycle/agent-session-turn-record' -import { - readTestJournalOutcomes, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { readTestJournalSnapshot } from '../../../agent-session-journal/journal-observation.test-support' +import { withTestAgentSessionDatabase } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import type { StructuredAgentSessionCurrentWork } from '../../observations/structured-agent-session-current-work' import { hostStructuredAgentSessionCurrentWork } from '../../observations/structured-agent-session-host-current-work' import { @@ -29,14 +27,6 @@ export const TURN: AgentJournalItemIdentity = { export const TURN_KEY = agentJournalItemKey(TURN) const IN_TURN = { kind: 'turn' as const, turnItemId: TURN_KEY } export const APPROVAL: AgentJournalItemIdentity = { ...TURN, ordinal: 2 } -/** Recovered outcome values carry recovered=true. */ -export const REJECT_RECOVERED_ROWS = ` - CREATE TEMP TRIGGER reject_recovered BEFORE INSERT ON journal_outcomes - WHEN (NEW.field = 'items' AND json_extract(NEW.value_json, '$.recovered') = 1) - OR (NEW.field = 'submissions' AND json_extract(NEW.value_json, '$[1].recovered') = 1) - BEGIN SELECT RAISE(ABORT, 'temporary storage failure'); END; -` - export async function leaveUnfinishedWork( current: QueuedMessageTestRig, options: { prompt?: true } = {} @@ -71,10 +61,15 @@ export async function exitWhileSettlementFails(current: QueuedMessageTestRig) { if (!session || !child?.generation) { throw new Error('expected the original child') } - const database = openTestJournalHostDatabase(current.root) - const before = readTestJournalOutcomes(database.db, SESSION) + const before = await readTestJournalSnapshot(session.journal) vi.spyOn(console, 'warn').mockImplementation(() => undefined) - database.db.exec(REJECT_RECOVERED_ROWS) + await withTestAgentSessionDatabase(current.root, (db) => + db.exec(` + CREATE TRIGGER reject_recovered BEFORE UPDATE OF payload_json ON journal_items + WHEN json_extract(NEW.payload_json, '$.recovered') = 1 + BEGIN SELECT RAISE(ABORT, 'temporary storage failure'); END + `) + ) await current.host.handleAdapterEvent({ type: 'ended', sessionId: SESSION, @@ -90,8 +85,8 @@ export async function exitWhileSettlementFails(current: QueuedMessageTestRig) { runtimeFence: child.fence + 1, deathEvidence: { kind: 'exit-observed', ownerFence: child.fence } }) - expect(readTestJournalOutcomes(database.db, SESSION)).toEqual(before) - return { session, database, deadFence: child.fence } + expect(await readTestJournalSnapshot(session.journal)).toEqual(before) + return { session, deadFence: child.fence } } export async function exitChild(current: QueuedMessageTestRig): Promise { diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-prompt-answer-receipts.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-prompt-answer-receipts.test.ts index 2118f8a0f34..5f0050d2be3 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-prompt-answer-receipts.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-prompt-answer-receipts.test.ts @@ -6,7 +6,7 @@ import { } from '../../../../../shared/agent-session/journal/agent-session-journal-types' import { agentJournalItemKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' import type { AgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store' -import { openTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { withTestAgentSessionDatabase } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import type { StructuredAgentSessionAdapter } from '../../providers/structured-agent-session-adapter' import type { StructuredAgentSessionHost } from '../structured-agent-session-host' import { @@ -126,14 +126,15 @@ it.each(['allow', 'deny'])( const prompt = page.ok ? page.page.items.find((item) => item.itemId === itemId)! : null expect(prompt).not.toBeNull() const params = answer(prompt!, opId(), choice) - const db = openTestJournalHostDatabase(root).db - db.exec(`CREATE TRIGGER fail_plan_record AFTER UPDATE ON agent_session_records + await withTestAgentSessionDatabase(root, (db) => + db.exec(`CREATE TRIGGER fail_plan_record AFTER UPDATE ON agent_session_records WHEN json_extract(NEW.record_json, '$.options.interactionMode') = '${after}' BEGIN SELECT RAISE(ABORT, 'record unavailable'); END`) + ) await expect(host.respondToPrompt(CALLER, params)).rejects.toThrow('record unavailable') - db.exec('DROP TRIGGER fail_plan_record') - expect(receipt(params.envelope.clientOperationId)).toEqual({ verdict: 'absent' }) - expect(promptBody(itemId)).toMatchObject({ resolution: { state: 'pending' } }) + await withTestAgentSessionDatabase(root, (db) => db.exec('DROP TRIGGER fail_plan_record')) + expect(await receipt(params.envelope.clientOperationId)).toEqual({ verdict: 'absent' }) + expect(await promptBody(itemId)).toMatchObject({ resolution: { state: 'pending' } }) expect(store.getRecord(SESSION)?.options).toEqual({ model: 'opus[1m]', interactionMode: before diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queue-import-contract.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queue-import-contract.test-fixture.ts index 050974f4770..61525d21d25 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queue-import-contract.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queue-import-contract.test-fixture.ts @@ -23,7 +23,7 @@ export const identity = { providerHandle: codexProviderHandle('thread') } export const sourceHandle = { kind: 'codex' as const, threadId: 'thread' } -export const base = (seq: number) => ({ v: 3, epoch: 'frozen-epoch', seq, fence: 0, ts: 1_000 }) +export const base = (seq: number) => ({ v: 4, epoch: 'frozen-epoch', seq, fence: 0, ts: 1_000 }) export const founding: JournalRow = { ...base(1), kind: 'epoch', diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-action-receipts.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-action-receipts.test.ts index 789bf200d13..8cdefc189d0 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-action-receipts.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-action-receipts.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { AGENT_SESSION_MAX_NEW_OPERATION_AGE_MS } from '../../../../../shared/agent-session/agent-session-host-authority' -import { openTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { withTestAgentSessionDatabase } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { createQueuedMessageTestRig, QUEUED_RIG_CALLER as CALLER, @@ -34,11 +34,13 @@ function ledgerRow(id: string) { return rig.store.listOperationRows().find((row) => row.operationId === id) } -function failReceiptWrites(): () => void { - const db = openTestJournalHostDatabase(rig.root).db - db.exec(`CREATE TRIGGER fail_queued_receipt BEFORE INSERT ON agent_session_command_receipts +async function failReceiptWrites(): Promise<() => Promise> { + await withTestAgentSessionDatabase(rig.root, (db) => + db.exec(`CREATE TRIGGER fail_queued_receipt BEFORE INSERT ON agent_session_command_receipts BEGIN SELECT RAISE(ABORT, 'receipt unavailable'); END`) - return () => db.exec('DROP TRIGGER fail_queued_receipt') + ) + return () => + withTestAgentSessionDatabase(rig.root, (db) => db.exec('DROP TRIGGER fail_queued_receipt')) } const UNKNOWN = { ok: false, refusal: { code: 'agent_session_operation_unknown' } } @@ -46,10 +48,10 @@ const UNKNOWN = { ok: false, refusal: { code: 'agent_session_operation_unknown' describe('Resume', () => { it('answers unknown, never success, when a no-op cannot be recorded, and records nothing', async () => { const id = opId() - const restore = failReceiptWrites() + const restore = await failReceiptWrites() expect(await rig.resume(id)).toMatchObject(UNKNOWN) - restore() - expect(receipt(id)).toEqual({ verdict: 'absent' }) + await restore() + expect(await receipt(id)).toEqual({ verdict: 'absent' }) expect(ledgerRow(id)).toBeUndefined() }) }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-repeat-press.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-repeat-press.test.ts index b2d5a8e429e..9cf2a6fdcc4 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-repeat-press.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-repeat-press.test.ts @@ -8,7 +8,7 @@ import { import { eventually } from './structured-agent-session-queued-message-rig-provider.test-fixture' import type { QueuedMessageTestRig } from './structured-agent-session-queued-message-rig.test-fixture' import { openRigTurnFor } from './structured-agent-session-queued-rig-turn.test-fixture' -import { openTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { withTestAgentSessionDatabase } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { hostTestMessage, hostTestOperationId, @@ -72,7 +72,7 @@ describe('a queued Send receipt', () => { value: { queued: { state: 'waiting' } } }) expect(await rig.drafts()).toHaveLength(1) - expect(rig.store.readCommandReceipt({ kind: 'global' }, id)).toMatchObject({ + expect(await rig.store.readCommandReceipt({ kind: 'global' }, id)).toMatchObject({ verdict: 'readable', receipt: { result: { kind: 'queued-draft', messageId: id } } }) @@ -105,9 +105,10 @@ describe('a queued Send receipt', () => { it('rolls back a draft whose receipt cannot be saved', async () => { await rig.workingSend() const id = hostTestOperationId() - const db = openTestJournalHostDatabase(rig.root).db - db.exec(`CREATE TRIGGER fail_draft_receipt BEFORE INSERT ON agent_session_command_receipts + await withTestAgentSessionDatabase(rig.root, (db) => + db.exec(`CREATE TRIGGER fail_draft_receipt BEFORE INSERT ON agent_session_command_receipts BEGIN SELECT RAISE(ABORT, 'receipt unavailable'); END`) + ) expect(await rig.host.send(CALLER, params(id, 'must roll back'))).toMatchObject({ ok: false, refusal: { @@ -116,9 +117,11 @@ describe('a queued Send receipt', () => { } }) expect(await rig.drafts()).toHaveLength(0) - expect(rig.store.readCommandReceipt({ kind: 'global' }, id)).toEqual({ verdict: 'absent' }) + expect(await rig.store.readCommandReceipt({ kind: 'global' }, id)).toEqual({ + verdict: 'absent' + }) expect(rig.store.listOperationRows().find((row) => row.operationId === id)).toBeUndefined() - db.exec('DROP TRIGGER fail_draft_receipt') + await withTestAgentSessionDatabase(rig.root, (db) => db.exec('DROP TRIGGER fail_draft_receipt')) expect(await rig.host.send(CALLER, params(id, 'must roll back'))).toMatchObject({ ok: true, replayed: false diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-resend-answer.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-resend-answer.test.ts index 89b503c4f7b..57598081345 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-resend-answer.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-resend-answer.test.ts @@ -60,8 +60,8 @@ describe('send receipt acceptance', () => { }) expect(store.listOperationRows()).toEqual(before) expect(await readTestJournalSubmissions(hostJournal())).toHaveLength(0) - expect(store.readCommandReceipt({ kind: 'global' }, params.envelope.clientOperationId)).toEqual( - { verdict: 'absent' } - ) + expect( + await store.readCommandReceipt({ kind: 'global' }, params.envelope.clientOperationId) + ).toEqual({ verdict: 'absent' }) }) }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-resend-ledger-refusal.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-resend-ledger-refusal.test.ts index 5b3471d4c17..fe2bdbbe2e3 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-resend-ledger-refusal.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-resend-ledger-refusal.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from 'vitest' -import { openTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { withTestAgentSessionDatabase } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import type { StructuredAgentSessionAdapter } from '../../providers/structured-agent-session-adapter' import type { StructuredAgentSessionHost } from '../structured-agent-session-host' import { @@ -39,8 +39,9 @@ describe('send without ledger policy', () => { const params = sendParams('receipt survives the ledger') await host.send(CALLER, params) await vi.waitFor(() => expect(dispatch).toHaveBeenCalledTimes(1)) - const db = openTestJournalHostDatabase(root).db - db.prepare('DELETE FROM agent_session_operations').run() + await withTestAgentSessionDatabase(root, (db) => + db.prepare('DELETE FROM agent_session_operations').run() + ) await expect(host.send(CALLER, params)).resolves.toMatchObject({ ok: true, replayed: true }) expect(dispatch).toHaveBeenCalledTimes(1) }) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-failure-filing.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-failure-filing.test.ts index 3ac7a1702ac..f7e9b459405 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-failure-filing.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-failure-filing.test.ts @@ -1,4 +1,4 @@ -import { openTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { withTestAgentSessionDatabase } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { openTestAgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { StructuredAgentSessionHost } from '../structured-agent-session-host' import { afterEach, expect, it, vi } from 'vitest' @@ -62,11 +62,13 @@ it('says so in the chat when the agent cannot start for the continuation', async // The failure note replaces the derived cut notice to avoid duplicate explanations. expect(await readerNotes(host)).toEqual(await statusNotes(host)) await host.flushAllStreamedEvents() - openTestJournalHostDatabase(root) - .db.prepare( - "UPDATE agent_session_records SET record_json = json_set(record_json, '$.lease.minimumNextFence', 'bad') WHERE session_id = ?" - ) - .run(SESSION) + await withTestAgentSessionDatabase(root, (db) => + db + .prepare( + "UPDATE agent_session_records SET record_json = json_set(record_json, '$.lease.minimumNextFence', 'bad') WHERE session_id = ?" + ) + .run(SESSION) + ) const store = await openTestAgentSessionRecordStore(root) const restarted = new StructuredAgentSessionHost({ ...host.deps, store }) const starts = acquire.mock.calls.length diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-offer-newer-orca.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-offer-newer-orca.test.ts deleted file mode 100644 index cc1b3bc9efa..00000000000 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-offer-newer-orca.test.ts +++ /dev/null @@ -1,132 +0,0 @@ -import { readFile, writeFile } from 'node:fs/promises' -import { join } from 'node:path' -import { afterEach, expect, it, vi } from 'vitest' -import { - AGENT_SESSION_RECOVERY_CAPSULE_FILE, - AgentSessionRecoveryCapsule -} from '../../../../runtime/agent-session/records/agent-session-recovery-capsule' -import { - liveTestJournalRows, - openTestJournalHostDatabase, - updateTestJournalRowJson -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' - -import { interruptedRestart } from './structured-agent-session-restart-interruption.test-fixture' -import { attachParams, CALLER, hostTestState } from './structured-agent-session-host.test-fixture' -import { - HOST_TEST_NOW as NOW, - HOST_TEST_SESSION as SESSION -} from './structured-agent-session-host-test-data.test-fixture' - -// Preserve newer-chat offers byte-for-byte for a compatible build; this host cannot act on them. - -afterEach(() => vi.restoreAllMocks()) - -function newerOrcaRow(root: string, sessionId: string = SESSION): void { - const { db } = openTestJournalHostDatabase(root) - const first = liveTestJournalRows(db, sessionId)[0] - if (!first) { - throw new Error('the chat has no founding row') - } - const anchor = JSON.parse(first.rowJson) - db.prepare('UPDATE journal_outcome_epochs SET format = 99 WHERE session_id = ?').run(sessionId) - updateTestJournalRowJson( - db, - sessionId, - 1, - JSON.stringify({ ...anchor, v: 99, outcomeFormat: 99 }) - ) -} - -function offersFile(root: string): Promise { - return readFile(join(root, AGENT_SESSION_RECOVERY_CAPSULE_FILE)) -} - -async function keptWithoutAWord( - host: Awaited>['host'], - root: string, - acquire: ReturnType, - unopenedJournalIsNewer = false -) { - const before = await offersFile(root) - expect((await host.restartResume.list()).map((offer) => offer.sessionId)).toEqual( - unopenedJournalIsNewer ? [SESSION] : [] - ) - expect(await offersFile(root)).toEqual(before) - expect((await host.restartResume.continueAfterRestart([SESSION], 'modal')).continued).toEqual([]) - expect(acquire).not.toHaveBeenCalled() - expect(await host.restartResume.listFailures()).toEqual([]) - expect(await offersFile(root)).toEqual(before) - const capsule = new AgentSessionRecoveryCapsule(root) - expect((await capsule.list(NOW)).map((marker) => marker.sessionId)).toEqual([SESSION]) - expect(await capsule.listFailed(NOW)).toEqual([]) -} - -it('lists an unopened newer journal passively and preserves its offer when acting is refused', async () => { - const { host, root, acquire } = await interruptedRestart() - newerOrcaRow(root) - await keptWithoutAWord(host, root, acquire, true) -}) - -it('still offers a damaged chat, and files its failure when acting on it', async () => { - const { host, root, acquire } = await interruptedRestart() - updateTestJournalRowJson(openTestJournalHostDatabase(root).db, SESSION, 1, '}{') - - expect((await host.restartResume.list()).map((candidate) => candidate.sessionId)).toEqual([ - SESSION - ]) - await host.restartResume.continueAfterRestart([SESSION], 'modal') - - expect(acquire).not.toHaveBeenCalled() - expect(await host.restartResume.listFailures()).toMatchObject([ - { - sessionId: SESSION, - reason: 'agent_session_journal_unreadable', - details: { reason: 'journalCorrupt' } - } - ]) -}) - -it.each([ - ['after the dialog listed the offers', true, undefined], - ['on a host that has not listed them yet', false, undefined], - ['for a client shown only Codex', false, (agent: string) => agent === 'codex'] -])( - "dismisses every listed offer and leaves a newer Orca's hidden one as it was, %s", - async (_when, listFirst, audience) => { - const NEWER = 'session-newer-orca' - const created = await hostTestState().host.attach( - CALLER, - attachParams({ envelope: { ...attachParams().envelope, sessionId: NEWER } }) - ) - expect(created.ok).toBe(true) - hostTestState().acquire.mockClear() - const { host, root, marker } = await interruptedRestart() - newerOrcaRow(root, NEWER) - const path = join(root, AGENT_SESSION_RECOVERY_CAPSULE_FILE) - const capsule = JSON.parse(await readFile(path, 'utf8')) - capsule.entries.push({ state: 'pending', marker: { ...marker, sessionId: NEWER } }) - await writeFile(path, JSON.stringify(capsule)) - const newerEntry = () => - readFile(path, 'utf8').then((text) => - JSON.stringify( - JSON.parse(text).entries.find( - (entry: { marker: { sessionId: string } }) => entry.marker.sessionId === NEWER - ) - ) - ) - const before = await newerEntry() - - if (listFirst) { - expect((await host.restartResume.list()).map((candidate) => candidate.sessionId)).toEqual([ - SESSION, - NEWER - ]) - } - await host.restartResume.dismiss(undefined, audience) - - const left = await new AgentSessionRecoveryCapsule(root).list(NOW) - expect(left.map((offer) => offer.sessionId)).toEqual([NEWER]) - expect(await newerEntry()).toBe(before) - } -) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-usage-limit.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-usage-limit.test-fixture.ts index 449b929e572..097e3f08952 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-usage-limit.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rewind-usage-limit.test-fixture.ts @@ -1,7 +1,9 @@ import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import type { StructuredAgentSessionAdapter } from '../../providers/structured-agent-session-adapter' -import { openTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' -import { openTestAgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' +import { + openTestAgentSessionRecordStore, + withTestAgentSessionDatabase +} from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { expect, vi } from 'vitest' import { structuredAgentSessionMessageSendMutation } from '../../../../../shared/agent-session/requests/structured-agent-session-send-mutation' import { @@ -159,10 +161,11 @@ export async function expectUsageLimitCompletionRollback({ const request = await params(target) const before = await readTestHostJournal(host, HOST_TEST_SESSION) const physical = journal.physicalCursor().epoch - const db = openTestJournalHostDatabase(directory).db - db.exec(`CREATE TEMP TRIGGER fail_rewind_completion BEFORE UPDATE ON agent_session_records + await withTestAgentSessionDatabase(directory, (db) => + db.exec(`CREATE TRIGGER fail_rewind_completion BEFORE UPDATE ON agent_session_records WHEN instr(NEW.record_json, '"phase":"completed"') > 0 BEGIN SELECT RAISE(ABORT, 'completion write failed'); END`) + ) await expect(host.rewind({ callerKey: 'desktop' }, request)).rejects.toThrow( 'completion write failed' ) @@ -172,7 +175,9 @@ export async function expectUsageLimitCompletionRollback({ expect(journal.physicalCursor().epoch).toBe(physical) expect(store.getRecord(HOST_TEST_SESSION)?.rewind?.phase).toBe('provider-succeeded') expect(store.getRecord(HOST_TEST_SESSION)?.usageLimitContinueCancel).toBeUndefined() - db.exec('DROP TRIGGER fail_rewind_completion') + await withTestAgentSessionDatabase(directory, (db) => + db.exec('DROP TRIGGER fail_rewind_completion') + ) const replace = vi.spyOn(AgentSessionJournal.prototype, 'completeRewind') expect( await attachAndStart(store.getRecord(HOST_TEST_SESSION)!.lease.runtimeFence) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-conversation-command.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-conversation-command.test.ts index 02dc8efdfeb..061d3d5624f 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-conversation-command.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-conversation-command.test.ts @@ -1,3 +1,4 @@ +import { withTestAgentSessionDatabase } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' @@ -21,10 +22,7 @@ import { hostTestOperationId, resetHostTestOperationIds } from './structured-agent-session-host-test-data.test-fixture' -import { - closeTestJournalHostDatabases, - openTestJournalHostDatabase -} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { closeTestJournalHostDatabases } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import { createStructuredAgentSessionLogger } from '../structured-agent-session-logger' import { codexProviderHandle } from '../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' @@ -159,9 +157,10 @@ async function clearCommits(params = commandParams('clear'), who = caller) { describe('same-conversation clear', () => { it('rolls back the divider, boundary and ledger success together when storage fails', async () => { - const db = openTestJournalHostDatabase(generationRoot()).db - db.exec( - "CREATE TEMP TRIGGER fail_clear BEFORE UPDATE ON agent_session_records WHEN instr(NEW.record_json, 'providerContextBoundary') > 0 BEGIN SELECT RAISE(ABORT, 'disk full'); END" + await withTestAgentSessionDatabase(generationRoot(), (db) => + db.exec( + "CREATE TRIGGER fail_clear BEFORE UPDATE ON agent_session_records WHEN instr(NEW.record_json, 'providerContextBoundary') > 0 BEGIN SELECT RAISE(ABORT, 'disk full'); END" + ) ) const params = commandParams('clear') const before = await readTestHostJournal(host, HOST_TEST_SESSION) @@ -174,7 +173,7 @@ describe('same-conversation clear', () => { expect( store.getOperationRow(caller.callerKey, params.envelope.clientOperationId)?.outcome.status ).toBe('pending') - db.exec('DROP TRIGGER fail_clear') + await withTestAgentSessionDatabase(generationRoot(), (db) => db.exec('DROP TRIGGER fail_clear')) await clearCommits(params) expect(await clearRows()).toHaveLength(1) expect( diff --git a/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-transition-rollback.test.ts b/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-transition-rollback.test.ts index 67a1987ed3f..14f99cf1852 100644 --- a/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-transition-rollback.test.ts +++ b/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-transition-rollback.test.ts @@ -1,7 +1,8 @@ import { readTestJournalItems } from '../../../agent-session-journal/journal-observation.test-support' import { expect, it } from 'vitest' import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../shared/agent-session/journal/agent-session-journal-types' -import { openTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { withTestAgentSessionDatabase } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' +import { agentJournalItemKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' import { failedTool, @@ -13,16 +14,17 @@ import { it('leaves no row of a settlement durable when a later one of its rows fails to write', async () => { const { root, journal, deferred, sink, bind } = await rig() bind() - const running = Array.from({ length: 250 }, (_, index) => identity(`tool-${index}`)) + const running = [identity('tool-0'), identity('tool-1')] for (const id of running) { sink.tryAppendItem?.(id, tool('read', 'running'), { turnScope: AGENT_JOURNAL_THREAD_SCOPE }) } await deferred.drained() const before = journal.cursor().sequence - // Abort the second insert to verify settlement transaction atomicity. - openTestJournalHostDatabase(root).db.exec(`CREATE TEMP TRIGGER fail_second_chunk -BEFORE INSERT ON main.journal_rows WHEN NEW.seq = ${before + 2} -BEGIN SELECT RAISE(ABORT, 'second chunk refused'); END`) + await withTestAgentSessionDatabase(root, (db) => + db.exec(`CREATE TRIGGER fail_settlement +BEFORE UPDATE ON journal_items WHEN NEW.item_id = '${agentJournalItemKey(running[1]!)}' +BEGIN SELECT RAISE(ABORT, 'settlement refused'); END`) + ) sink.tryAppendTransition?.({ lifecycle: true, publish: true, diff --git a/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-transition.test.ts b/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-transition.test.ts index 1bcbf153402..eb5804bfb92 100644 --- a/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-transition.test.ts +++ b/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-transition.test.ts @@ -2,7 +2,7 @@ import { readTestJournalItems } from '../../../agent-session-journal/journal-obs import { expect, it } from 'vitest' import { agentJournalItemKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../shared/agent-session/journal/agent-session-journal-types' -import { openTestJournalHostDatabase } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { withTestAgentSessionDatabase } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import type { StructuredAgentSessionTransition } from '../structured-agent-session-transition' import { @@ -51,9 +51,11 @@ it('writes nothing after a step whose row the database refused', async () => { const { root, journal, deferred, sink, bind } = await rig() bind() // Fail step two to verify step three never fills its place. - openTestJournalHostDatabase(root).db.exec(`CREATE TEMP TRIGGER fail_second_step -BEFORE INSERT ON main.journal_rows WHEN instr(NEW.row_json, 'refused-step') > 0 + await withTestAgentSessionDatabase(root, (db) => + db.exec(`CREATE TRIGGER fail_second_step +BEFORE INSERT ON journal_items WHEN instr(NEW.payload_json, 'refused-step') > 0 BEGIN SELECT RAISE(ABORT, 'second step refused'); END`) + ) const step = (recordId: string) => itemStep({ kind: 'literal', identity: identity(recordId), body: tool(recordId, 'running') }) sink.tryAppendTransition?.({ diff --git a/src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-subscriber-oversized-removal.test.ts b/src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-subscriber-oversized-removal.test.ts deleted file mode 100644 index e95059f1786..00000000000 --- a/src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-subscriber-oversized-removal.test.ts +++ /dev/null @@ -1,108 +0,0 @@ -import { join } from 'node:path' -import { expect, it } from 'vitest' -import { AGENT_SESSION_JOURNAL_SCHEMA_VERSION } from '../../../../../../shared/agent-session/journal/agent-session-journal-types' -import type { AgentSessionSubscribeEvent } from '../../../../../../shared/agent-session/agent-session-wire' -import { - REMOTE_RUNTIME_MAX_OUTBOUND_JSON_BYTES, - serializeRemoteRuntimePayload -} from '../../../../../../shared/remote-runtime/remote-runtime-memory-limits' -import type { JournalRow } from '../../../../agent-session-journal/durable-rows/journal-row-schema' -import { - openTestJournalHostDatabase, - insertTestJournalRow -} from '../../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' -import { AgentSessionSubscribers } from '../structured-agent-session-subscribers' -import { codexProviderHandle } from '../../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' - -import { - fixture, - SESSION, - journals -} from './structured-agent-session-subscriber-contract.test-fixture' - -it('catches a subscriber up past a pre-existing unsendable removal with a bounded reset', async () => { - const journalDir = join(fixture.root, 'oversized-removal-journal') - const seeded = await journals.open({ - identity: { - sessionId: SESSION, - workspaceId: 'workspace-1', - hostId: 'local', - agent: 'codex', - providerHandle: codexProviderHandle('thread-1') - }, - stateDirectory: journalDir - }) - // A legacy removal ID exceeds the outbound cap alone, making catch-up impossible. - const hugeItemId = `codex:thread-1:${'h'.repeat(5 * 1024 * 1024)}:1` - const resumeCursor = seeded.cursor() - const seq = resumeCursor.sequence - const rows: JournalRow[] = [ - { - kind: 'item', - itemId: hugeItemId, - revision: 1, - body: { kind: 'message', role: 'assistant', blocks: [{ type: 'text', text: 'big' }] }, - v: AGENT_SESSION_JOURNAL_SCHEMA_VERSION, - epoch: seeded.physicalCursor().epoch, - seq: seq + 1, - fence: 1, - ts: 2_000 - }, - { - kind: 'tombstone', - itemId: hugeItemId, - revision: 2, - v: AGENT_SESSION_JOURNAL_SCHEMA_VERSION, - epoch: seeded.physicalCursor().epoch, - seq: seq + 2, - fence: 1, - ts: 2_001 - } - ] - // Direct writes model legacy persisted rows. - await seeded.close() - const opened = openTestJournalHostDatabase(journalDir) - try { - opened.db.exec('BEGIN IMMEDIATE') - for (const row of rows) { - insertTestJournalRow(opened.db, SESSION, row) - } - opened.db.exec('COMMIT') - } finally { - await opened.close() - } - const journal = await journals.open({ - identity: { - sessionId: SESSION, - workspaceId: 'workspace-1', - hostId: 'local', - agent: 'codex', - providerHandle: codexProviderHandle('thread-1') - }, - stateDirectory: journalDir - }) - - const subscribers = new AgentSessionSubscribers() - const events: AgentSessionSubscribeEvent[] = [] - subscribers.open({ - id: 'subscriber-1', - sessionId: SESSION, - journal, - fence: 1, - cursor: resumeCursor, - emit: (event) => events.push(event) - }) - - for (const event of events) { - expect(Buffer.byteLength(serializeRemoteRuntimePayload(event), 'utf8')).toBeLessThanOrEqual( - REMOTE_RUNTIME_MAX_OUTBOUND_JSON_BYTES - ) - } - const reset = events.find((event) => event.type === 'reset') - expect(reset).toBeDefined() - - // Reset must advance past the unsendable row so later publications make progress. - const settled = events.length - subscribers.publish(SESSION, journal) - expect(events.slice(settled).filter((event) => event.type === 'reset')).toEqual([]) -}) diff --git a/src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-wire-admission.test.ts b/src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-wire-admission.test.ts index 61f0ba730ff..9de7e655b33 100644 --- a/src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-wire-admission.test.ts +++ b/src/main/native-chat/agent-session-wire/publication/subscribers/testing/structured-agent-session-wire-admission.test.ts @@ -73,7 +73,11 @@ describe('structured agent-session outbound admission', () => { roster = { state: 'monitoring' } subscribers.republishBackgroundTasks(SESSION, 2) - await vi.waitFor(() => expect(initial.slice(1)).toHaveLength(2)) + await vi.waitFor(() => + expect(initial.slice(1)).toContainEqual( + expect.objectContaining({ backgroundTasks: { state: 'monitoring' } }) + ) + ) initial.slice(1).forEach(expectAdmitted) const epochReset: AgentSessionSubscribeEvent[] = [] diff --git a/src/main/runtime/agent-session/structured-agent-session-runtime.test.ts b/src/main/runtime/agent-session/structured-agent-session-runtime.test.ts index 5914575d08e..266e571a758 100644 --- a/src/main/runtime/agent-session/structured-agent-session-runtime.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-runtime.test.ts @@ -9,13 +9,13 @@ import { ensureStructuredAgentSessionHost, stopStructuredAgentSessionRuntime } from './structured-agent-session-runtime' -import { seedTestAgentSessionRecordStore } from './records/agent-session-record-store-test-harness' +import { + seedTestAgentSessionRecordStore, + withTestAgentSessionDatabase +} from './records/agent-session-record-store-test-harness' import { nativeChatScratchFolderFor } from '../../native-chat/native-chat-scratch-folder' import { foundAgentSessionRecord } from './records/agent-session-record-founding' -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 { recordingStructuredAgentSessionLogger } from '../../native-chat/agent-session-wire/host/testing/structured-agent-session-logger.test-fixture' import { onStructuredAgentSessionsHeldChanged } from '../../native-chat/agent-session-wire/host/structured-agent-session-registry' @@ -77,8 +77,10 @@ describe('structured agent-session runtime install', () => { stateDirectory = await mkdtemp(join(tmpdir(), 'orca-structured-runtime-backfill-')) await seedTestAgentSessionRecordStore(stateDirectory, { records: [oldChat('codex')] }) if (failWrite) { - openTestJournalHostDatabase(stateDirectory).db.exec( - "CREATE TRIGGER fail_permission_backfill BEFORE UPDATE ON agent_session_records BEGIN SELECT RAISE(ABORT, 'disk full'); END" + await withTestAgentSessionDatabase(stateDirectory, (db) => + db.exec( + "CREATE TRIGGER fail_permission_backfill BEFORE UPDATE ON agent_session_records BEGIN SELECT RAISE(ABORT, 'disk full'); END" + ) ) } const logged = recordingStructuredAgentSessionLogger() diff --git a/src/main/runtime/orchestration-cli-subprocess.test.ts b/src/main/runtime/orchestration-cli-subprocess.test.ts index 1f9cd94846b..8c4a75e3767 100644 --- a/src/main/runtime/orchestration-cli-subprocess.test.ts +++ b/src/main/runtime/orchestration-cli-subprocess.test.ts @@ -1,4 +1,5 @@ // Exercise compiled CLI keepalives over pipes; skip until built (the verification gate builds it). +import { withTestAgentSessionDatabase } from './agent-session/records/agent-session-record-store-test-harness' import { spawn } from 'node:child_process' import { existsSync, mkdtempSync, rmSync } from 'node:fs' import { tmpdir } from 'node:os' @@ -26,10 +27,7 @@ import { import { abandonStructuredAgentSessionHost } from '../native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-abandon.test-fixture' import { StructuredAgentSessionHost } from '../native-chat/agent-session-wire/host/structured-agent-session-host' import { setStructuredAgentSessionHost } from '../native-chat/agent-session-wire/host/structured-agent-session-registry' -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' import { openTestAgentSessionRecordStore, readPersistedTestAgentSessionStoreText, @@ -572,46 +570,47 @@ describeIfBuilt('native caller subprocess over durable records', () => { await abandonStructuredAgentSessionHost(host) await hostTestRecoveryCapsuleSettled() - const nativeDb = openTestJournalHostDatabase(rig.root).db - const before = nativeDb - .prepare('SELECT record_json FROM agent_session_records WHERE session_id = ?') - .get(sessionId) - expect(before).toBeDefined() - nativeDb - .prepare(` + await withTestAgentSessionDatabase(rig.root, (nativeDb) => { + const before = nativeDb + .prepare('SELECT record_json FROM agent_session_records WHERE session_id = ?') + .get(sessionId) + expect(before).toBeDefined() + nativeDb + .prepare(` UPDATE agent_session_records SET record_json = json_set(record_json, '$.lease.minimumNextFence', 'bad') WHERE session_id = ? `) - .run(sessionId) - const damaged = nativeDb - .prepare('SELECT record_json FROM agent_session_records WHERE session_id = ?') - .get(sessionId) - if (typeof before?.record_json !== 'string' || typeof damaged?.record_json !== 'string') { - throw new Error('Expected persisted native record JSON') - } - const original: unknown = JSON.parse(before.record_json) - if (!isRecord(original) || !isRecord(original.lease)) { - throw new Error('Expected checked original record and lease') - } - expect(JSON.parse(damaged.record_json)).toEqual({ - ...original, - lease: { ...original.lease, minimumNextFence: 'bad' } - }) - // A second visible raw row must be excluded because its conversation cannot be checked. - nativeDb - .prepare(` + .run(sessionId) + const damaged = nativeDb + .prepare('SELECT record_json FROM agent_session_records WHERE session_id = ?') + .get(sessionId) + if (typeof before?.record_json !== 'string' || typeof damaged?.record_json !== 'string') { + throw new Error('Expected persisted native record JSON') + } + const original: unknown = JSON.parse(before.record_json) + if (!isRecord(original) || !isRecord(original.lease)) { + throw new Error('Expected checked original record and lease') + } + expect(JSON.parse(damaged.record_json)).toEqual({ + ...original, + lease: { ...original.lease, minimumNextFence: 'bad' } + }) + // A second visible raw row must be excluded because its conversation cannot be checked. + nativeDb + .prepare(` INSERT INTO agent_session_records (session_id, record_json) SELECT ?, json_set(record_json, '$.sessionId', ?, '$.lease.sessionId', ?, '$.providerHandleChain', 'bad') FROM agent_session_records WHERE session_id = ? `) - .run(invalidId, invalidId, invalidId, sessionId) - nativeDb - .prepare(` + .run(invalidId, invalidId, invalidId, sessionId) + nativeDb + .prepare(` INSERT INTO agent_session_tabs (tab_id, session_id, position) VALUES (?, ?, 1) `) - .run(structuredAgentSessionTabId(invalidId), invalidId) + .run(structuredAgentSessionTabId(invalidId), invalidId) + }) await closeTestJournalHostDatabase(rig.root) store = await openTestAgentSessionRecordStore(rig.root) host = new StructuredAgentSessionHost({ diff --git a/src/main/runtime/rpc/methods/agent-launch/agent-launch-cwd-resources.test.ts b/src/main/runtime/rpc/methods/agent-launch/agent-launch-cwd-resources.test.ts index c1b8442ee63..0ad826e2446 100644 --- a/src/main/runtime/rpc/methods/agent-launch/agent-launch-cwd-resources.test.ts +++ b/src/main/runtime/rpc/methods/agent-launch/agent-launch-cwd-resources.test.ts @@ -1,5 +1,6 @@ import type * as RuntimeEnvironmentStore from '../../../../../shared/remote-runtime/host-store/runtime-environment-store' import '../../unused-default-rpc-methods.test-fixture' +import { withTestAgentSessionDatabase } from '../../../../agent-session/records/agent-session-record-store-test-harness' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' const { @@ -85,10 +86,7 @@ import { TEST_WORKTREE_ID } from '../../../orca-runtime-tests/orca-runtime-test-fixtures.spec' import { openTestAgentSessionRecordStore } from '../../../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' import { runtimeStub, setAgentLaunchRecordStore } from './agent-launch.test-fixture' import { RuntimeWorktreeLineageController } from '../../../workspaces/lineage/runtime-worktree-lineage-controller' import type { RuntimeManagedWorktreeCreateArgs } from '../../../workspaces/creation/runtime-managed-worktree-create-types' @@ -369,9 +367,11 @@ describe('CLI cwd resource frames, managed receipts and raw-path controls', () = expect(output.error.message).toHaveLength(637) expect(host.publish.mock.calls.length).toBe(effectBefore[2]) } - const row = openTestJournalHostDatabase(stateDirectory) - .db.prepare('SELECT row_json FROM agent_session_operations ORDER BY rowid DESC LIMIT 1') - .get() + const row = await withTestAgentSessionDatabase(stateDirectory, (db) => + db + .prepare('SELECT row_json FROM agent_session_operations ORDER BY rowid DESC LIMIT 1') + .get() + ) expect(typeof row?.row_json).toBe('string') const rowJson = String(row!.row_json) expect(JSON.parse(rowJson).outcome.status).toBe(valid ? 'succeeded' : 'failed') @@ -441,10 +441,12 @@ describe('CLI cwd resource frames, managed receipts and raw-path controls', () = const { _meta: _reopenedMeta, ...replyBody } = JSON.parse(replies.at(-1)!) expect(replyBody).toEqual(originals.get(operationId)!.reply) expect(settledExecution.at(-1)).toEqual(Array(8).fill(0)) - const persisted = openTestJournalHostDatabase(stateDirectory) - .db.prepare('SELECT row_json FROM agent_session_operations') - .all() - .map((row) => String(row.row_json)) + const persisted = await withTestAgentSessionDatabase(stateDirectory, (db) => + db + .prepare('SELECT row_json FROM agent_session_operations') + .all() + .map((row) => String(row.row_json)) + ) expect(persisted).toContain(originals.get(operationId)!.row) expect(result.ok).toBe(index === 0) if (index === 1) { diff --git a/src/main/runtime/rpc/methods/orchestration/worker-start-card-population.test-fixture.ts b/src/main/runtime/rpc/methods/orchestration/worker-start-card-population.test-fixture.ts deleted file mode 100644 index 812dfd3bf6f..00000000000 --- a/src/main/runtime/rpc/methods/orchestration/worker-start-card-population.test-fixture.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { readTestJournalSnapshot } from '../../../../native-chat/agent-session-journal/journal-observation.test-support' -import type { createOrchestrationRpcHarness } from './rpc-test-harness' -import { expect } 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 { JournalHostDatabase } from '../../../../native-chat/agent-session-journal/database/journal-host-database' -import { COORD } from './worker-start-card-contract-test-fixture' - -export async function expectWorkerStartPopulation(input: { - journal: AgentSessionJournal - count: number - connection: JournalHostDatabase['db'] - facts: { dispatchId: string; workerAddress: string; task: string; name: string }[] - before: AgentJournalSnapshot -}): Promise { - const { journal, facts, count, connection, before } = input - const cards = (await readTestJournalSnapshot(journal)).items.filter( - ({ body }) => body.kind === 'agent-start' - ) - expect(cards.map(({ body }) => body)).toEqual( - facts.map((fact) => ({ - kind: 'agent-start', - dispatchId: fact.dispatchId, - task: fact.task, - worker: { - party: { - address: fact.workerAddress, - terminalHandle: fact.workerAddress, - orcaSessionId: null - }, - name: fact.name - } - })) - ) - expect( - connection.prepare('SELECT COUNT(*) AS count FROM journal_rows WHERE session_id = ?').get(COORD) - ?.count - ).toBe(before.cursor.sequence + count) -} - -export function expectWorkerStartSourcePlans( - queries: readonly (readonly [string])[], - prepareSource: ReturnType< - ReturnType['setup'] - >['db']['db']['prepare'] -): void { - for (const [query] of queries.slice(0, 3)) { - const plan = prepareSource(`EXPLAIN QUERY PLAN ${query}`) - .all('unused') - .map((row) => row.detail) - .join(' | ') - expect(plan).toMatch(/SEARCH (?:dispatch_contexts|tasks|worker_dispatches)/) - expect(plan).not.toMatch(/SCAN/) - } -} diff --git a/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest.test.ts b/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest.test.ts index a9708a46020..687cfeea81a 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest.test.ts @@ -20,14 +20,16 @@ import { } from '../../../../../native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture' import { setStructuredAgentSessionHost } from '../../../../../native-chat/agent-session-wire/host/structured-agent-session-registry' import { - foundRestTestChat, REST_TEST_CALLER as CALLER, restTestSend } from '../../../../../native-chat/agent-session-wire/host/testing/structured-agent-session-rest-test-rig.test-fixture' -import { editPersistedTestAgentSessionStore } from '../../../../agent-session/records/agent-session-record-store-test-harness' +import { + editPersistedTestAgentSessionStore, + withTestAgentSessionDatabase +} from '../../../../agent-session/records/agent-session-record-store-test-harness' import type { RpcResponse } from '../../../core' -import { openTestJournalHostDatabase } 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 { CLIENT, @@ -397,78 +399,18 @@ it('logs a reader reconnecting to a journal that will not open once per failure' expect(logged()).toEqual([denied, exhausted]) }) -it('refuses a damaged journal as unloadable at subscribe and send, and keeps its rows (P2-03)', async () => { - await foundRestTestChat(fixture.rig) - await fixture.rig.host.flushAllStreamedEvents() - openTestJournalHostDatabase(fixture.rig.root) - .db.prepare( - "UPDATE journal_outcomes SET value_json = '}{' WHERE generation = (SELECT generation FROM journal_outcome_epochs WHERE session_id = ?) AND seq = 2" - ) - .run(SESSION) - await fixture.rig.restart() - setStructuredAgentSessionHost(fixture.rig.host) - - const frames = await call('agentSession.subscribe', { sessionId: SESSION }) - expect(frames.find((frame) => !frame.ok)).toMatchObject({ - error: { - data: { - refusal: { - code: 'agent_session_journal_unreadable', - details: { reason: 'journalCorrupt' } - } - } - } +it('resets catch-up over an unreadable event while keeping the current conversation readable', async () => { + await restingChat() + await call('agentSession.subscribe', { sessionId: SESSION }) + const live = await journalSqlReads( + fixture.rig.host.deps.journalDatabase.readers, + SESSION + ).cursor() + await withTestAgentSessionDatabase(fixture.rig.root, (db) => { + db.prepare( + 'UPDATE journal_events SET changes_json=? WHERE session_id=? AND sequence=(SELECT MIN(sequence) FROM journal_events WHERE session_id=? AND sequence>1)' + ).run('{damaged', SESSION, SESSION) }) - const fence = fixture.rig.store.getRecord(SESSION)!.lease.runtimeFence - expect( - await fixture.rig.host.send(CALLER, restTestSend('after the damage', fence)) - ).toMatchObject({ - ok: false, - refusal: { code: 'agent_session_journal_unreadable', details: { reason: 'journalCorrupt' } } - }) - expect( - openTestJournalHostDatabase(fixture.rig.root) - .db.prepare( - 'SELECT value_json FROM journal_outcomes WHERE generation = (SELECT generation FROM journal_outcome_epochs WHERE session_id = ?) AND seq = 2 LIMIT 1' - ) - .get(SESSION)?.value_json - ).toBe('}{') -}) - -function outcomeEpoch(): { epoch: unknown; sequence: unknown } { - const row = openTestJournalHostDatabase(fixture.rig.root) - .db.prepare('SELECT epoch, through_seq FROM journal_outcome_epochs WHERE session_id = ?') - .get(SESSION) - return { epoch: row?.epoch, sequence: row?.through_seq } -} - -function damageSupersededItemKey(key: string | Buffer): void { - openTestJournalHostDatabase(fixture.rig.root) - .db.prepare( - "UPDATE journal_outcomes SET entry_key = ? WHERE field = 'items' AND seq = (SELECT MIN(seq) FROM journal_outcomes WHERE field = 'items')" - ) - .run(key) -} - -it('resets a client catching up over a superseded record with a damaged key, which open never reads', async () => { - await foundRestTestChat(fixture.rig) - await fixture.rig.host.flushAllStreamedEvents() - damageSupersededItemKey('123') - // A deletion is the one newest record admission lets keep such a key. - openTestJournalHostDatabase(fixture.rig.root) - .db.prepare( - "INSERT INTO journal_outcomes (generation, seq, field, entry_key, format, ordinal, value_json, deleted) SELECT generation, through_seq, 'items', '123', 2, 'b', 'null', 1 FROM journal_outcome_epochs WHERE session_id = ?" - ) - .run(SESSION) - await fixture.rig.restart() - setStructuredAgentSessionHost(fixture.rig.host) - const hello = { items: [{ body: { kind: 'message' } }] } - - expect((await call('agentSession.subscribe', { sessionId: SESSION }))[0]).toMatchObject({ - ok: true, - result: { type: 'snapshot', page: hello } - }) - const live = outcomeEpoch() const stale = { epoch: live.epoch, sequence: 1 } expect( ( @@ -476,55 +418,44 @@ it('resets a client catching up over a superseded record with a damaged key, whi )[0] ).toMatchObject({ ok: true, - result: { ok: false, reset: 'schema_unreadable', page: hello } + result: { + ok: false, + reset: 'schema_unreadable', + page: { items: [{ body: { kind: 'message' } }] } + } }) expect( (await call('agentSession.subscribe', { sessionId: SESSION, cursor: stale }))[0] - ).toMatchObject({ - ok: true, - result: { type: 'reset', reset: 'schema_unreadable', page: hello } - }) - // The reset moved the client to the live cursor, past the record it could not read. - expect(await call('agentSession.subscribe', { sessionId: SESSION, cursor: live })).toMatchObject([ - { ok: true, result: { type: 'batch' } } - ]) -}) - -it.each([ - ['unparseable', '{damaged'], - ['non-text', Buffer.from([0])] -])('resets a client when a key is damaged under an open chat: %s', async (_name, key) => { - await restingChat() - await call('agentSession.subscribe', { sessionId: SESSION }) - damageSupersededItemKey(key) - + ).toMatchObject({ ok: true, result: { type: 'reset', reset: 'schema_unreadable' } }) expect( - ( - await call('agentSession.history', { - sessionId: SESSION, - direction: 'after', - cursor: { epoch: outcomeEpoch().epoch, sequence: 1 } - }) - )[0] - ).toMatchObject({ ok: true, result: { ok: false, reset: 'schema_unreadable' } }) + (await call('agentSession.subscribe', { sessionId: SESSION, cursor: live }))[0] + ).toMatchObject({ ok: true, result: { type: 'batch' } }) }) it('keeps a storage failure during catch-up retryable instead of resetting the client', async () => { await restingChat() - await call('agentSession.subscribe', { sessionId: SESSION }) - const { db } = openTestJournalHostDatabase(fixture.rig.root) + const live = await journalSqlReads( + fixture.rig.host.deps.journalDatabase.readers, + SESSION + ).cursor() const catchUp = { sessionId: SESSION, direction: 'after', - cursor: { epoch: outcomeEpoch().epoch, sequence: 1 } + cursor: { epoch: live.epoch, sequence: 1 } + } + await withTestAgentSessionDatabase(fixture.rig.root, (db) => + db.exec('ALTER TABLE journal_events RENAME TO journal_events_unavailable') + ) + try { + expect((await call('agentSession.history', catchUp))[0]).toMatchObject({ + ok: false, + error: { code: 'runtime_error' } + }) + } finally { + await withTestAgentSessionDatabase(fixture.rig.root, (db) => + db.exec('ALTER TABLE journal_events_unavailable RENAME TO journal_events') + ) } - - db.exec('ALTER TABLE journal_outcomes RENAME TO journal_outcomes_unavailable') - expect((await call('agentSession.history', catchUp))[0]).toMatchObject({ - ok: false, - error: { code: 'runtime_error' } - }) - db.exec('ALTER TABLE journal_outcomes_unavailable RENAME TO journal_outcomes') expect((await call('agentSession.history', catchUp))[0]).toMatchObject({ ok: true, result: { ok: true } diff --git a/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-rpc-status.test-fixture.ts b/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-rpc-status.test-fixture.ts index 6476a2b1824..33b58166761 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-rpc-status.test-fixture.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-rpc-status.test-fixture.ts @@ -2,7 +2,7 @@ import type { AgentJournalItemBody } from '../../../../../../shared/agent-sessio import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' -import { beforeEach, afterEach } from 'vitest' +import { afterEach } from 'vitest' import { codexProviderHandle } from '../../../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import { createTrackedJournalOpener } from '../../../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import { indexedStatusFeedSession } from '../../../../../native-chat/agent-session-wire/observations/testing/structured-agent-session-status-feed-test-session.test-fixture' @@ -11,13 +11,18 @@ import { createStructuredAgentSessionLogger } from '../../../../../native-chat/a export const STATUS_SESSION = 'session-status' const journals = createTrackedJournalOpener() -let root: string -let feed: StructuredAgentSessionStatusFeed +let root: string | undefined +let feed: StructuredAgentSessionStatusFeed | undefined -beforeEach(async () => { +export async function prepareStatusFeed(): Promise { + if (feed) { + return + } + let now = 1 root = await mkdtemp(join(tmpdir(), 'orca-rpc-status-')) const journal = await journals.open({ stateDirectory: root, + now: () => now, identity: { sessionId: STATUS_SESSION, workspaceId: 'workspace-1', @@ -31,6 +36,7 @@ beforeEach(async () => { { kind: 'status', text: 'Working', turnLifecycle: { turnId: 'turn-1', state: 'running' } } ] for (const [ordinal, body] of bodies.entries()) { + now = ordinal + 1 await journal.appendItem( { provider: 'codex', threadId: 'native-status', turnId: 'turn-1', ordinal }, body, @@ -44,12 +50,19 @@ beforeEach(async () => { now: () => 1_000 }) await feed.publish(STATUS_SESSION, journal, { replay: true }) -}) +} afterEach(async () => { - await journals.closeAll() if (root) { + await journals.closeAll() await rm(root, { recursive: true, force: true }) + root = undefined + feed = undefined } }) -export const statusFeed = () => feed +export const statusFeed = () => { + if (!feed) { + throw new Error('status fixture was not prepared') + } + return feed +} diff --git a/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-rpc.test-fixture.ts b/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-rpc.test-fixture.ts index 14d2d530500..4a6486ed6a8 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-rpc.test-fixture.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-rpc.test-fixture.ts @@ -1,4 +1,4 @@ -import { statusFeed } from './structured-agent-session-rpc-status.test-fixture' +import { statusFeed, prepareStatusFeed } from './structured-agent-session-rpc-status.test-fixture' import { SESSION, @@ -214,6 +214,9 @@ export async function openStream( runtimeOverrides: Record = {}, methods?: readonly RpcAnyMethodDeclaration[] ): Promise { + if (method === 'agentSession.subscribeStatus') { + await prepareStatusFeed() + } const replies: RpcResponse[] = [] await dispatcher(runtimeOverrides, methods).dispatchStreaming( request(method, params), From aad6b6b5fedcb63998e6d8dd13430284c3a169ed Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:27:18 -0800 Subject: [PATCH 19/45] Remove obsolete internal-work counting from chat inventory tests --- .../agent-session-inventory-scale.test.ts | 239 +----------------- .../agent-session-scale.test-fixture.ts | 127 ---------- .../agent-session-store-load-scale.test.ts | 93 ------- 3 files changed, 3 insertions(+), 456 deletions(-) delete mode 100644 src/main/runtime/agent-session-store-load-scale.test.ts diff --git a/src/main/runtime/agent-session-inventory-scale.test.ts b/src/main/runtime/agent-session-inventory-scale.test.ts index d8ce77094ab..4d18971c418 100644 --- a/src/main/runtime/agent-session-inventory-scale.test.ts +++ b/src/main/runtime/agent-session-inventory-scale.test.ts @@ -1,28 +1,15 @@ -import { describe, expect, it, vi } from 'vitest' -import { composeWorktreeHostIdentity } from '../../shared/worktree/host-qualified-identity' +import { describe, expect, it } from 'vitest' import '../../shared/ai-vault/search/ai-vault-search-test-fixture' import '../ai-vault/structured-session-ownership.test-support' import '../ai-vault/structured-session-ownership' import { hostTestState } from '../native-chat/agent-session-wire/host/testing/structured-agent-session-host.test-fixture' import { StructuredAgentSessionHost } from '../native-chat/agent-session-wire/host/structured-agent-session-host' import { setStructuredAgentSessionHost } from '../native-chat/agent-session-wire/host/structured-agent-session-registry' -import * as journalFactory from '../native-chat/agent-session-journal/journal-store-factory' -import { - countRecordQueries, - countStoreWork, - scaleJournal, - scaleStore, - scaleDirectory, - scaleRecord -} from './agent-session-scale.test-fixture' -import { findWorkspaceReferences } from './runtime-reference-find' -import { parseWorkspaceReferenceUrl } from '../../shared/workspace-attachments/workspace-reference-identity' +import { scaleDirectory, scaleRecord } from './agent-session-scale.test-fixture' import { createReferenceAgentIndex } from './runtime-reference-agents' import { seedTestAgentSessionRecordStore, openTestAgentSessionRecordStore, - editPersistedTestAgentSessionStore, - readPersistedTestAgentSessionStore, openTestAgentSessionDatabase } from './agent-session/records/agent-session-record-store-test-harness' import { OrchestrationDb } from './orchestration/db/orchestration-db' @@ -47,20 +34,6 @@ import { createStructuredAgentSessionLogger } from '../native-chat/agent-session import type { AgentSessionRecord } from '../../shared/agent-session/lifecycle/agent-session-record' import { conversationRecordReader } from './orchestration/addressing/session-lineage/structured-session-lineage' import './agent-session/structured-agent-session-create-adoption' -import { listStructuredSessionsForWorktree } from './workspaces/removal/structured-session-worktree-teardown' -import * as membership from './agent-session/records/agent-session-store-state' - -// Uses the complete host harness; discovery, mail reach and every store read execute unchanged. -async function inventoryRig(n: number) { - const rig = await scaleStore(n) - const host = new StructuredAgentSessionHost({ - ...hostTestState().host.deps, - store: rig.store, - journalDatabase: await openTestAgentSessionDatabase(rig.root) - }) - setStructuredAgentSessionHost(host) - return { ...rig, host } -} const referenceRuntime = { getOrchestrationFleetAgentStatusSnapshot: () => [], @@ -82,102 +55,7 @@ function clearedRecord(sessionId: string, successor: string): AgentSessionRecord } } -describe('real conversation inventory work', () => { - it.each([10, 100])('visits %i unreadable rows once per audience inventory', async (u) => { - const root = await scaleDirectory() - const healthy = Array.from({ length: 10 }, (_, i) => scaleRecord(`healthy-u-${i}`)) - const unreadable = Array.from({ length: u }, (_, i) => scaleRecord(`unreadable-u-${i}`)) - const retained = unreadable.filter((_, i) => i % 2 === 0) - await seedTestAgentSessionRecordStore(root, { - records: [...healthy, ...unreadable], - sessionTabs: [...healthy, ...unreadable].map(({ sessionId }) => ({ - sessionId, - tabId: `tab-${sessionId}` - })) - }) - await editPersistedTestAgentSessionStore(root, (state) => { - unreadable.forEach(({ sessionId }, i) => { - if (i % 2 === 0) { - Object.assign(state.records[sessionId].lease, { minimumNextFence: 'bad' }) - } else { - Object.assign(state.records[sessionId], { providerHandleChain: 'bad' }) - } - }) - }) - const raw = await readPersistedTestAgentSessionStore(root) - const store = await openTestAgentSessionRecordStore(root) - const host = new StructuredAgentSessionHost({ - ...hostTestState().host.deps, - store, - journalDatabase: await openTestAgentSessionDatabase(root) - }) - const ids = new Set(unreadable.map(({ sessionId }) => sessionId)) - const work = countStoreWork(store) - const inventories = vi.mocked(store.listConversations) - // Default spy executes native flatMap and its production callback on every dense input row. - const flatten = vi.spyOn(Array.prototype, 'flatMap') - try { - for (let repeat = 1; repeat <= 3; repeat++) { - expect(host.knownAgentIds()).toContain('claude') - expect(work.counts).toMatchObject({ - inventories: repeat, - views: 10 * repeat, - clones: 0, - decodes: 0 - }) - } - const scans = flatten.mock.contexts - .map((rows, i) => - Array.isArray(rows) && - rows.length === u && - rows.every( - (row: unknown) => - typeof row === 'object' && - row !== null && - 'raw' in row && - typeof row.raw === 'object' && - row.raw !== null && - 'sessionId' in row.raw && - typeof row.raw.sessionId === 'string' && - ids.has(row.raw.sessionId) - ) - ? i - : -1 - ) - .filter((i) => i >= 0) - expect(scans).toHaveLength(3) - for (const i of scans) { - expect(flatten.mock.contexts[i]).toMatchObject( - unreadable.map(({ sessionId }) => ({ raw: { sessionId } })) - ) - expect(flatten.mock.results[i]).toEqual({ - type: 'return', - value: retained.map(({ sessionId }) => store.getConversation(sessionId)) - }) - } - expect(inventories.mock.results.map((result) => result.value)).toEqual( - Array.from({ length: 3 }, () => - [...healthy, ...retained].map(({ sessionId }) => store.getConversation(sessionId)) - ) - ) - } finally { - flatten.mockRestore() - } - expect(store.getVisibleSessionTabIndex().sessionIds).toEqual( - [...healthy, ...retained].map(({ sessionId }) => sessionId) - ) - for (const [i, { sessionId }] of unreadable.entries()) { - expect(store.getConversation(sessionId)?.sessionId ?? null).toBe(i % 2 ? null : sessionId) - expect(store.getRecord(sessionId)).toBeNull() - expect(() => store.requireWritableRecord(sessionId)).toThrow() - } - await store.setConversationName(healthy[0]!.sessionId, 'unrelated update') - const after = await readPersistedTestAgentSessionStore(root) - for (const { sessionId } of unreadable) { - expect(after.records[sessionId]).toEqual(raw.records[sessionId]) - } - }) - +describe('persisted conversation inventory identity', () => { it('keeps global worker identity separate from candidate inventory and retains closure/loop outcomes', async () => { const rootId = testOrcaSessionId('global-root') const successor = testOrcaSessionId('visible-successor') @@ -312,115 +190,4 @@ describe('real conversation inventory work', () => { }) } }) - - it.each([10, 100])( - 'finds all references at %i rows without allocating membership payloads', - async (n) => { - const rig = await inventoryRig(n) - const work = countStoreWork(rig.store) - const queries = countRecordQueries(rig.root) - const visits = vi.spyOn(membership, 'hasAgentSessionConversation') - let index: ReturnType = new Map() - const result = await findWorkspaceReferences( - { query: 'STA-1234' }, - { - workspaces: ['workspace-1', 'other'].map((id) => ({ - id, - hostId: 'local' as const, - name: id, - repo: 'repo', - kind: 'worktree' as const, - isArchived: false, - linkedItems: [parseWorkspaceReferenceUrl('https://linear.app/acme/issue/STA-1234')] - })), - agents: (workspaceKeys) => { - index = createReferenceAgentIndex( - referenceRuntime, - null, - conversationRecordReader(rig.store), - workspaceKeys - ) - return index - } - } - ) - expect(result.matches.flatMap((match) => match.agents)).toHaveLength(n + 1) - const found = [...index.values()].flat() - expect(found.map((row) => row.sessionId).sort()).toEqual( - rig.rows - .filter((row) => !['closed-chat', 'malformed-chat'].includes(row.sessionId)) - .map((row) => row.sessionId) - .sort() - ) - expect(index.get(composeWorktreeHostIdentity('local', 'workspace-1'))).toHaveLength(n / 2 + 1) - expect(found.every((row) => row.liveness === 'unverifiable')).toBe(true) - expect(found.find((row) => row.sessionId === 'damaged-chat')?.sessionIds).toContain( - 'provider-damaged-chat-0' - ) - expect(work.counts).toMatchObject({ - views: 4 * n + 2, - inventories: 2, - keyed: 2 * (n + 1), - // Each mail-reach check asks the keyed retirement read; one index read remains. - tabReads: 1, - clones: 0, - decodes: 0 - }) - // Keyed: the visible-tab list built once, then one membership question per row. - expect(visits).toHaveBeenCalledTimes(2 * n + 3) - expect(queries()).toEqual({ executions: 0, rows: 0 }) - work.reset() - visits.mockClear() - expect(rig.store.getVisibleSessionTabIndex().sessionIds).toHaveLength(n + 1) - await rig.store.showSessionTabs([ - 'closed-chat', - 'damaged-chat', - 'malformed-chat', - 'missing-chat' - ]) - expect(rig.store.getVisibleSessionTabIndex().sessionIds).toContain('closed-chat') - expect(rig.store.getVisibleSessionTabIndex().sessionIds).not.toContain('malformed-chat') - expect(work.counts.views).toBe(0) - await expect(rig.store.setSessionTabVisibility('malformed-chat', true)).rejects.toThrow() - expect(work.counts.views).toBe(0) - } - ) - - it.each([10, 100])( - 'seeds selected records without opening journals and scopes lifecycle inventory at %i rows', - async (n) => { - const rig = await inventoryRig(n) - for (const id of ['damaged-chat', rig.rows[0]!.sessionId, rig.rows[1]!.sessionId]) { - await (await scaleJournal(rig.root, id)).close() - } - const work = countStoreWork(rig.store) - const queries = countRecordQueries(rig.root) - const openJournal = vi.spyOn(journalFactory, 'openAgentSessionJournal') - await rig.host.seedSessionTabs(['damaged-chat', rig.rows[0]!.sessionId]) - expect(openJournal).not.toHaveBeenCalled() - expect(rig.host.hasSession(rig.rows[0]!.sessionId)).toBe(false) - expect(work.counts).toMatchObject({ inventories: 0, views: 1, keyed: 2, clones: 0 }) - work.reset() - const scoped = listStructuredSessionsForWorktree('workspace-1', { - resolvedConnectionId: null - }) - expect(scoped.members).toHaveLength(n / 2 + 2) - expect(scoped.live).toEqual([]) - expect(scoped.members.some((row) => row.sessionId === 'damaged-chat')).toBe(true) - expect(work.counts).toMatchObject({ inventories: 1, views: n + 1 + n / 2 + 1, clones: 0 }) - work.reset() - const slept = await rig.host.sleepWorkspace('workspace-1') - expect(slept.stoppedSessionIds).not.toContain('damaged-chat') - expect(slept.failures?.some((row) => row.sessionId === 'damaged-chat')).toBe(true) - expect(work.counts.inventories).toBe(1) - // One inventory and six checked observations per selected healthy member. - expect(work.counts.views).toBe(n + 1 + 6 * (n / 2 + 1)) - expect(rig.store.getConversation(rig.rows[1]!.sessionId)?.location.workspaceId).toBe('other') - work.reset() - expect(rig.host.knownAgentIds()).toContain('claude') - expect(work.counts).toMatchObject({ inventories: 1, views: n + 1, clones: 0 }) - expect(queries()).toEqual({ executions: 0, rows: 0 }) - expect(hostTestState().acquire).not.toHaveBeenCalled() - } - ) }) diff --git a/src/main/runtime/agent-session-scale.test-fixture.ts b/src/main/runtime/agent-session-scale.test-fixture.ts index ebf91db0a84..d3238033e24 100644 --- a/src/main/runtime/agent-session-scale.test-fixture.ts +++ b/src/main/runtime/agent-session-scale.test-fixture.ts @@ -2,29 +2,19 @@ import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, vi } from 'vitest' -import * as views from '../../shared/agent-session/lifecycle/agent-session-conversation-view' -import * as handles from '../../shared/agent-session/lifecycle/agent-session-provider-handle' import { claudeProviderHandle } from '../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import { agentSessionLeaseFixture, agentSessionRecordFixture } from '../../shared/agent-session/lifecycle/agent-session-record.test-fixture' import { - openTestJournalHostDatabase, closeTestJournalHostDatabases, createTrackedJournalOpener } from '../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' -import { - seedTestAgentSessionRecordStore, - editPersistedTestAgentSessionStore, - openTestAgentSessionRecordStore -} from './agent-session/records/agent-session-record-store-test-harness' -import type { AgentSessionRecordStore } from './agent-session/records/agent-session-record-store' import { setStructuredAgentSessionHost } from '../native-chat/agent-session-wire/host/structured-agent-session-registry' const roots: string[] = [] export const journals = createTrackedJournalOpener() -export const stringify = JSON.stringify.bind(JSON) export function scaleRecord(sessionId: string, workspaceId = 'workspace-1', links = 1) { const row = agentSessionRecordFixture( @@ -43,129 +33,12 @@ export function scaleRecord(sessionId: string, workspaceId = 'workspace-1', link } } -export function scaleJournal(root: string, sessionId: string) { - return journals.open({ - stateDirectory: root, - identity: { - sessionId, - workspaceId: 'workspace-1', - hostId: 'local', - agent: 'claude', - providerHandle: claudeProviderHandle(`provider-${sessionId}-0`, null) - } - }) -} - export async function scaleDirectory() { const root = await mkdtemp(join(tmpdir(), 'orca-session-scale-')) roots.push(root) return root } -export async function scaleStore(n: number) { - const root = await scaleDirectory() - const rows = Array.from({ length: n }, (_, i) => - scaleRecord(`chat-${String(i).padStart(5, '0')}`, i % 2 ? 'other' : 'workspace-1') - ) - rows.push(scaleRecord('damaged-chat'), scaleRecord('closed-chat'), scaleRecord('malformed-chat')) - await seedTestAgentSessionRecordStore(root, { - records: rows, - sessionTabs: rows - .filter((row) => row.sessionId !== 'closed-chat') - .map((row) => ({ tabId: `tab-${row.sessionId}`, sessionId: row.sessionId })) - }) - await editPersistedTestAgentSessionStore(root, (state) => { - Object.assign(state.records['damaged-chat'].lease, { minimumNextFence: 'bad' }) - Object.assign(state.records['malformed-chat'], { providerHandleChain: 'bad' }) - }) - const store = await openTestAgentSessionRecordStore(root) - return { root, store, rows } -} - -/** Transparent counters; every observed call still executes the production operation. */ -export function countStoreWork(store?: AgentSessionRecordStore) { - const counts = { - views: 0, - clones: 0, - cloneBytes: 0, - decodes: 0, - links: 0, - inventories: 0, - keyed: 0, - tabReads: 0 - } - const view = views.agentSessionConversationView - vi.spyOn(views, 'agentSessionConversationView').mockImplementation((record) => { - counts.views++ - return view(record) - }) - const decode = handles.decodePersistedAgentSessionProviderHandleChain - vi.spyOn(handles, 'decodePersistedAgentSessionProviderHandleChain').mockImplementation( - (value) => { - counts.decodes++ - counts.links += Array.isArray(value) ? value.length : 0 - return decode(value) - } - ) - const clone = globalThis.structuredClone - vi.spyOn(globalThis, 'structuredClone').mockImplementation((value) => { - counts.clones++ - counts.cloneBytes += Buffer.byteLength(stringify(value)) - return clone(value) - }) - if (store) { - const inventory = store.listConversations - vi.spyOn(store, 'listConversations').mockImplementation(() => { - counts.inventories++ - return inventory() - }) - const get = store.getConversation - vi.spyOn(store, 'getConversation').mockImplementation((id) => { - counts.keyed++ - return get(id) - }) - const tabs = store.getVisibleSessionTabIndex - vi.spyOn(store, 'getVisibleSessionTabIndex').mockImplementation(() => { - counts.tabReads++ - return tabs() - }) - } - return { - counts, - reset: () => { - for (const key of Object.keys(counts)) { - Reflect.set(counts, key, 0) - } - } - } -} - -export function countRecordQueries(root: string) { - let executions = 0 - let rows = 0 - const db = openTestJournalHostDatabase(root).db - const observed = new WeakSet() - const prepare = db.prepare.bind(db) - vi.spyOn(db, 'prepare').mockImplementation((sql) => { - const statement = prepare(sql) - if ( - sql === 'SELECT session_id, record_json FROM agent_session_records ORDER BY rowid' && - !observed.has(statement) - ) { - observed.add(statement) - const all = statement.all.bind(statement) - vi.spyOn(statement, 'all').mockImplementation((...args) => { - executions++ - const result = all(...args) - rows += result.length - return result - }) - } - return statement - }) - return () => ({ executions, rows }) -} - afterEach(async () => { setStructuredAgentSessionHost(null) vi.restoreAllMocks() diff --git a/src/main/runtime/agent-session-store-load-scale.test.ts b/src/main/runtime/agent-session-store-load-scale.test.ts deleted file mode 100644 index a768402fca1..00000000000 --- a/src/main/runtime/agent-session-store-load-scale.test.ts +++ /dev/null @@ -1,93 +0,0 @@ -import { readTestJournalItems } from '../native-chat/agent-session-journal/journal-observation.test-support' -import { describe, expect, it } from 'vitest' -import { - countRecordQueries, - countStoreWork, - scaleJournal, - scaleDirectory, - scaleRecord, - stringify -} from './agent-session-scale.test-fixture' -import { - editPersistedTestAgentSessionStore, - openTestAgentSessionRecordStore, - seedTestAgentSessionRecordStore, - readPersistedTestAgentSessionStore -} from './agent-session/records/agent-session-record-store-test-harness' -import { hostTestMessage } from '../native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture' -import { AGENT_JOURNAL_THREAD_SCOPE } from '../../shared/agent-session/journal/agent-session-journal-types' - -// Width, link count and malformed bytes vary independently; no clock-dependent budget. -describe('SQLite conversation reopen work', () => { - it.each([ - [10, 1, 16], - [100, 1, 16], - [10, 10, 16], - [10, 1, 64_000] - ])('decodes %i damaged rows with %i links and %i floor bytes once', async (n, links, bytes) => { - const root = await scaleDirectory() - const records = Array.from({ length: n }, (_, i) => - scaleRecord(`damaged-${i}`, 'workspace-1', links) - ) - await seedTestAgentSessionRecordStore(root, { - records: [...records, scaleRecord('healthy-chat'), scaleRecord('invalid-chat')] - }) - await editPersistedTestAgentSessionStore(root, (state) => { - for (const record of records) { - Object.assign(state.records[record.sessionId].lease, { - minimumNextFence: { bad: 'x'.repeat(bytes) } - }) - } - Object.assign(state.records['invalid-chat'], { providerHandleChain: 'bad' }) - }) - const raw = await readPersistedTestAgentSessionStore(root) - const expectedBytes = records.reduce( - (sum, row) => sum + Buffer.byteLength(stringify(raw.records[row.sessionId])), - 0 - ) - const journal = await scaleJournal(root, 'damaged-0') - await journal.appendItem( - { provider: 'claude', sessionId: 'provider-damaged-0-0', uuid: 'saved-question' }, - hostTestMessage('saved question'), - { fence: 7, turnScope: AGENT_JOURNAL_THREAD_SCOPE } - ) - await journal.close() - const work = countStoreWork() - const queries = countRecordQueries(root) - const store = await openTestAgentSessionRecordStore(root) - expect(queries()).toEqual({ executions: 1, rows: n + 2 }) - expect(work.counts).toMatchObject({ - clones: n, - cloneBytes: expectedBytes, - views: n + 1, - decodes: 4 * n + 5, - links: 4 * n * links + 2 - }) - const loaded = { ...work.counts } - for (let repeat = 0; repeat < 3; repeat++) { - for (const row of records) { - expect(store.getConversation(row.sessionId)?.options?.model).toBe('saved-model') - expect(store.getRecord(row.sessionId)).toBeNull() - expect(() => store.requireWritableRecord(row.sessionId)).toThrow() - } - } - expect(store.getConversation('invalid-chat')).toBeNull() - expect(store.listHeldSessionIds()).toContain('invalid-chat') - expect(work.counts).toEqual(loaded) - await store.setConversationName('healthy-chat', 'renamed') - expect(work.counts.clones).toBe(n) - expect(queries()).toEqual({ executions: 1, rows: n + 2 }) - const after = await readPersistedTestAgentSessionStore(root) - for (const row of records) { - expect(after.records[row.sessionId]).toEqual(raw.records[row.sessionId]) - } - expect(after.records['healthy-chat'].conversationName).toBe('renamed') - const reopened = await scaleJournal(root, 'damaged-0') - expect((await readTestJournalItems(reopened)).map((item) => item.body)).toContainEqual( - hostTestMessage('saved question') - ) - expect(store.getConversation('damaged-0')?.providerHandleChain[0]?.handle.nativeId).toBe( - 'provider-damaged-0-0' - ) - }) -}) From a231dc46ec4708ab63234592a8f7781b00d05f40 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:32:46 -0800 Subject: [PATCH 20/45] test(native-chat): repair SQL epoch fixtures and sender closure case --- .../journal-row-context-usage.spec.ts | 16 ++++- ...ournal-terminal-validation-test-fixture.ts | 16 ++++- .../agent-launch-cwd-resources.test.ts | 2 +- .../runtime-agent-sender-associations.test.ts | 58 +++++++++---------- 4 files changed, 58 insertions(+), 34 deletions(-) diff --git a/src/main/native-chat/agent-session-journal/durable-rows/journal-row-context-usage.spec.ts b/src/main/native-chat/agent-session-journal/durable-rows/journal-row-context-usage.spec.ts index f2ebc1caa94..50198166482 100644 --- a/src/main/native-chat/agent-session-journal/durable-rows/journal-row-context-usage.spec.ts +++ b/src/main/native-chat/agent-session-journal/durable-rows/journal-row-context-usage.spec.ts @@ -16,7 +16,10 @@ import { updateTestJournalRowJson } from '../store-lifecycle/journal-host-database-test-support' import { seedTestJournalHistoricalRows } from '../replay/journal-historical-inputs.test-support' -import { claudeProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' +import { + agentSessionJournalProviderHandle, + claudeProviderHandle +} from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' export function registerJournalRowContextUsageTests(): void { const IDENTITY: AgentSessionJournalIdentity = { @@ -90,7 +93,16 @@ export function registerJournalRowContextUsageTests(): void { 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' }, + { + v: 3, + kind: 'epoch', + epoch, + seq: 1, + ts: 1_000, + fence: 1, + reason: 'session_created', + providerHandle: agentSessionJournalProviderHandle(IDENTITY.providerHandle) + }, { v: 3, kind: 'item', diff --git a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-validation-test-fixture.ts b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-validation-test-fixture.ts index 1629064e5d2..6c11dd8992f 100644 --- a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-validation-test-fixture.ts +++ b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-validation-test-fixture.ts @@ -2,7 +2,10 @@ import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, beforeEach, expect } from 'vitest' -import { codexProviderHandle } from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' +import { + agentSessionJournalProviderHandle, + codexProviderHandle +} from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import { createTrackedJournalOpener, openTestJournalHostDatabase @@ -33,7 +36,16 @@ export function createFixture() { async function saveOldItems(inputs: { itemId: string; revision?: number; body: unknown }[]) { const epoch = 'saved-approval' const rows: JournalRow[] = [ - { v: 3, kind: 'epoch', epoch, seq: 1, ts: 900, fence: 7, reason: 'session_created' } + { + v: 3, + kind: 'epoch', + epoch, + seq: 1, + ts: 900, + fence: 7, + reason: 'session_created', + providerHandle: agentSessionJournalProviderHandle(savedIdentity.providerHandle) + } ] for (const [index, input] of inputs.entries()) { rows.push({ diff --git a/src/main/runtime/rpc/methods/agent-launch/agent-launch-cwd-resources.test.ts b/src/main/runtime/rpc/methods/agent-launch/agent-launch-cwd-resources.test.ts index 0ad826e2446..848aabec653 100644 --- a/src/main/runtime/rpc/methods/agent-launch/agent-launch-cwd-resources.test.ts +++ b/src/main/runtime/rpc/methods/agent-launch/agent-launch-cwd-resources.test.ts @@ -1,6 +1,6 @@ import type * as RuntimeEnvironmentStore from '../../../../../shared/remote-runtime/host-store/runtime-environment-store' import '../../unused-default-rpc-methods.test-fixture' -import { withTestAgentSessionDatabase } from '../../../../agent-session/records/agent-session-record-store-test-harness' +import { withTestAgentSessionDatabase } from '../../../agent-session/records/agent-session-record-store-test-harness' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' const { diff --git a/src/main/runtime/runtime-agent-sender-associations.test.ts b/src/main/runtime/runtime-agent-sender-associations.test.ts index c6a0a711cb6..8dcf904bd14 100644 --- a/src/main/runtime/runtime-agent-sender-associations.test.ts +++ b/src/main/runtime/runtime-agent-sender-associations.test.ts @@ -37,7 +37,11 @@ import { testOrcaSessionId } from '../../shared/orca-session-address-test-fixtur import { projectSessionTabsForClient } from './rpc/methods/session-tabs/session-tabs-inventory' import { AGENT_SENDER_PANE_READS } from './runtime-agent-sender-worker-history' import { structuredMailSource } from './orchestration/mailbox/structured-mail/structured-mail-source' -import { scaleStore } from './agent-session-scale.test-fixture' +import { scaleDirectory, scaleRecord } from './agent-session-scale.test-fixture' +import { + openTestAgentSessionRecordStore, + seedTestAgentSessionRecordStore +} from './agent-session/records/agent-session-record-store-test-harness' import { AgentSenderPublisher } from './runtime-agent-sender-publication' const unissued = (): never => { @@ -375,34 +379,30 @@ it.each([100, 1000])( } ) -it.each([100, 1000])( - 'rebuilds chat senders only when a chat appears, moves, clears or closes, not on other writes, at %i chats', - async (n) => { - const { store } = await scaleStore(n) - const h = harness(1, conversationRecordReader(store)) - const first = h.projection.build() - expect( - first.associations.filter((r) => r.identity.startsWith('orca_session_id:')).length - ).toBeGreaterThanOrEqual(n) - const settled = identityReads(h) - // A record write that leaves its location, /clear successor and tab alone, as lease - // renewals and receipts do, rebuilds nothing. - for (let i = 0; i < 5; i++) { - await store.setConversationName(`chat-0000${i}`, `renamed ${i}`) - expect(h.projection.build()).toBe(first) - } - expect(identityReads(h)).toEqual(settled) - await store.setSessionTabVisibility('chat-00000', false) - const closed = h.projection.build() - expect(closed.associations.some((r) => r.identity === 'orca_session_id:chat-00000')).toBe(false) - // One rebuild, which reads each open chat once (none here has a /clear predecessor). - expect(identityReads(h)).toEqual({ - ...settled, - chatReads: settled.chatReads + 1, - chatRecords: settled.chatRecords + store.listVisibleSessionIds().length - }) - } -) +it('withdraws a closed chat sender while retaining another open chat', async () => { + const root = await scaleDirectory() + await seedTestAgentSessionRecordStore(root, { + records: [scaleRecord('closed-chat'), scaleRecord('open-chat')], + sessionTabs: [ + { tabId: 'closed-tab', sessionId: 'closed-chat' }, + { tabId: 'open-tab', sessionId: 'open-chat' } + ] + }) + const store = await openTestAgentSessionRecordStore(root) + const h = harness(1, conversationRecordReader(store)) + expect(h.projection.build().associations).toEqual( + expect.arrayContaining([ + expect.objectContaining({ identity: 'orca_session_id:closed-chat' }), + expect.objectContaining({ identity: 'orca_session_id:open-chat' }) + ]) + ) + await store.setSessionTabVisibility('closed-chat', false) + const closed = h.projection.build() + expect(closed.associations.some((r) => r.identity === 'orca_session_id:closed-chat')).toBe(false) + expect(closed.associations).toContainEqual( + expect.objectContaining({ identity: 'orca_session_id:open-chat' }) + ) +}) it('reports only worktrees whose scope changed since their last frame', () => { const h = harness(1) From 8a2d4b8dc29be6530ede5f84ebf1bebb56e38dff Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:44:36 -0800 Subject: [PATCH 21/45] fix(native-chat): own async option preparation and port SQL runtime fixtures --- ...urnal-approval-revision-validation.spec.ts | 40 -------- .../journal-subagent-item.spec.ts | 2 +- ...nal-terminal-settlement-validation.spec.ts | 7 -- ...ournal-terminal-validation-test-fixture.ts | 99 ------------------- .../journal-settlement-contracts.test.ts | 2 - ...red-agent-session-current-requests.test.ts | 8 +- ...structured-agent-session-host-mutations.ts | 10 +- .../agent-session-inventory-scale.test.ts | 8 +- .../structured-agent-session-runtime.test.ts | 13 +++ .../structured-agent-session-at-rest.test.ts | 19 ++-- 10 files changed, 45 insertions(+), 163 deletions(-) delete mode 100644 src/main/native-chat/agent-session-journal/item-lifecycle/journal-approval-revision-validation.spec.ts delete mode 100644 src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-settlement-validation.spec.ts delete mode 100644 src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-validation-test-fixture.ts diff --git a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-approval-revision-validation.spec.ts b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-approval-revision-validation.spec.ts deleted file mode 100644 index 0cbc6c26fb5..00000000000 --- a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-approval-revision-validation.spec.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { readTestJournalItems } from '../journal-observation.test-support' -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 -): 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 = (await readTestJournalItems(fixture.journal))[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((await readTestJournalItems(fixture.journal))[0]).toEqual(before) - await fixture.expectSendStillWritable() - }) -} diff --git a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-subagent-item.spec.ts b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-subagent-item.spec.ts index 0b1b7f0bf6e..2d2ffe7c181 100644 --- a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-subagent-item.spec.ts +++ b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-subagent-item.spec.ts @@ -67,7 +67,7 @@ export function registerJournalSubagentItemTests(): void { }) await live.close() - const reopened = await open() + const reopened = await open({ recover: { fence: 0 } }) const row = (await readTestJournalItems(reopened)).at(-1)! expect(row.recovered).toBe(true) expect(row.body).toMatchObject({ diff --git a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-settlement-validation.spec.ts b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-settlement-validation.spec.ts deleted file mode 100644 index 22965738131..00000000000 --- a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-settlement-validation.spec.ts +++ /dev/null @@ -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) -} diff --git a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-validation-test-fixture.ts b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-validation-test-fixture.ts deleted file mode 100644 index 6c11dd8992f..00000000000 --- a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-terminal-validation-test-fixture.ts +++ /dev/null @@ -1,99 +0,0 @@ -import { mkdtemp, rm } from 'node:fs/promises' -import { tmpdir } from 'node:os' -import { join } from 'node:path' -import { afterEach, beforeEach, expect } from 'vitest' -import { - agentSessionJournalProviderHandle, - codexProviderHandle -} from '../../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' -import { - createTrackedJournalOpener, - openTestJournalHostDatabase -} from '../store-lifecycle/journal-host-database-test-support' -import { seedTestJournalHistoricalRows } from '../replay/journal-historical-inputs.test-support' -import type { JournalRow } from '../durable-rows/journal-row-schema' -import type { AgentSessionJournal } from '../journal-store' - -export function createFixture() { - const journals = createTrackedJournalOpener() - const identity = { provider: 'orca' as const, clientMessageId: 'old-turn' } - const savedIdentity = { - sessionId: 'validation', - workspaceId: 'folder', - hostId: 'host', - agent: 'codex' as const, - providerHandle: codexProviderHandle('thread') - } - let root: string - let journal: AgentSessionJournal - beforeEach(async () => { - root = await mkdtemp(join(tmpdir(), 'orca-settlement-validation-')) - }) - afterEach(async () => { - await journals.closeAll() - await rm(root, { recursive: true, force: true }) - }) - async function saveOldItems(inputs: { itemId: string; revision?: number; body: unknown }[]) { - const epoch = 'saved-approval' - const rows: JournalRow[] = [ - { - v: 3, - kind: 'epoch', - epoch, - seq: 1, - ts: 900, - fence: 7, - reason: 'session_created', - providerHandle: agentSessionJournalProviderHandle(savedIdentity.providerHandle) - } - ] - for (const [index, input] of inputs.entries()) { - rows.push({ - v: 3, - kind: 'item', - epoch, - seq: index + 2, - ts: 900, - fence: 7, - revision: 1, - ...input, - turnScope: { kind: 'thread' } - } as JournalRow) - } - seedTestJournalHistoricalRows(openTestJournalHostDatabase(root).db, savedIdentity, rows) - journal = await journals.open({ - identity: savedIdentity, - stateDirectory: root, - now: () => 1_000 - }) - } - 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 }) - } - function pendingApproval() { - return { - kind: 'approval', - title: 'Allow?', - detail: null, - options: [], - resolution: { state: 'pending', selectedOptionId: null, resolvedBy: null, resolvedAt: null } - } - } - return { - identity, - get journal() { - return journal - }, - saveOldItems, - pendingApproval, - expectSendStillWritable - } -} diff --git a/src/main/native-chat/agent-session-journal/store-lifecycle/journal-settlement-contracts.test.ts b/src/main/native-chat/agent-session-journal/store-lifecycle/journal-settlement-contracts.test.ts index 179c0c1b5c3..f1ba34749e1 100644 --- a/src/main/native-chat/agent-session-journal/store-lifecycle/journal-settlement-contracts.test.ts +++ b/src/main/native-chat/agent-session-journal/store-lifecycle/journal-settlement-contracts.test.ts @@ -5,7 +5,6 @@ import { registerJournalRowContextUsageTests } from '../durable-rows/journal-row import { registerJournalSubagentItemTests } from '../item-lifecycle/journal-subagent-item.spec' import { registerJournalSubmissionPositionsTests } from '../submissions/journal-submission-positions.spec' import { registerJournalSubmissionQueuedLinkTests } from '../submissions/journal-submission-queued-link.spec' -import { registerJournalTerminalSettlementValidationTests } from '../item-lifecycle/journal-terminal-settlement-validation.spec' afterEach(() => vi.restoreAllMocks()) @@ -15,4 +14,3 @@ describe('journal-row-context-usage', registerJournalRowContextUsageTests) describe('journal-subagent-item', registerJournalSubagentItemTests) describe('journal-submission-positions', registerJournalSubmissionPositionsTests) describe('journal-submission-queued-link', registerJournalSubmissionQueuedLinkTests) -describe('journal-terminal-settlement-validation', registerJournalTerminalSettlementValidationTests) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-current-requests.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-current-requests.test.ts index b8cfc49cb24..aa969a80607 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-current-requests.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-current-requests.test.ts @@ -153,7 +153,7 @@ it('keeps full child requests answerable outside the bounded transcript and clea expect((await current.host.currentWork(SESSION))?.requestProjection(SESSION).marker.version).toBe( requestVersion ) - await current.host.subscribe({ + const closeReopened = await current.host.subscribe({ id: 'reopened', sessionId: SESSION, boundedFrames: true, @@ -161,7 +161,10 @@ it('keeps full child requests answerable outside the bounded transcript and clea cursor, emit }) - coalescer.flush() + await vi.waitFor(() => { + coalescer.flush() + expect(state.cursor).toEqual(journal.cursor()) + }) await vi.waitFor(() => expect(state.requestAuthority?.version).toEqual(state.requestMarker?.version) ) @@ -200,6 +203,7 @@ it('keeps full child requests answerable outside the bounded transcript and clea await vi.waitFor(() => expect(state.currentRequests).toEqual([])) expect(journal.cursor()).toEqual(beforeExit) expect(selectAgentSessionCurrentRequests(state)).toEqual([]) + closeReopened() reader.invalidate() coalescer.dispose() }) diff --git a/src/main/native-chat/agent-session-wire/operations/structured-agent-session-host-mutations.ts b/src/main/native-chat/agent-session-wire/operations/structured-agent-session-host-mutations.ts index a3ada663287..1abaa15211b 100644 --- a/src/main/native-chat/agent-session-wire/operations/structured-agent-session-host-mutations.ts +++ b/src/main/native-chat/agent-session-wire/operations/structured-agent-session-host-mutations.ts @@ -213,7 +213,15 @@ export async function setStructuredAgentSessionOption( readsWithoutModel: caller.readsOptionsWithoutModel !== false }) if (picked.ok) { - confirmIdleStructuredAgentSessionOption(context, ctx, params) + const confirming = confirmIdleStructuredAgentSessionOption(context, ctx, params) + context.trackProviderOperation?.(confirming) + void confirming.catch((error) => { + ctx.logger.warn('confirming idle session metadata failed', { + scope: 'idle-option', + sessionId: ctx.sessionId, + error + }) + }) } return picked }, diff --git a/src/main/runtime/agent-session-inventory-scale.test.ts b/src/main/runtime/agent-session-inventory-scale.test.ts index 4d18971c418..a6138ce0b5f 100644 --- a/src/main/runtime/agent-session-inventory-scale.test.ts +++ b/src/main/runtime/agent-session-inventory-scale.test.ts @@ -34,6 +34,10 @@ import { createStructuredAgentSessionLogger } from '../native-chat/agent-session import type { AgentSessionRecord } from '../../shared/agent-session/lifecycle/agent-session-record' import { conversationRecordReader } from './orchestration/addressing/session-lineage/structured-session-lineage' import './agent-session/structured-agent-session-create-adoption' +import { + journalSqlReaderPath, + journalSqlWorkerPath +} from '../native-chat/agent-session-journal/journal-sql-worker-bundle.test-fixture' const referenceRuntime = { getOrchestrationFleetAgentStatusSnapshot: () => [], @@ -151,7 +155,9 @@ describe('persisted conversation inventory identity', () => { legacySessionTabIds: [], stateDirectory: root, hostId: 'local', - logger: createStructuredAgentSessionLogger() + logger: createStructuredAgentSessionLogger(), + writerPath: journalSqlWorkerPath(), + readerPath: journalSqlReaderPath() }) const db = new OrchestrationDb(':memory:') try { diff --git a/src/main/runtime/agent-session/structured-agent-session-runtime.test.ts b/src/main/runtime/agent-session/structured-agent-session-runtime.test.ts index 266e571a758..9bb2e7ba85a 100644 --- a/src/main/runtime/agent-session/structured-agent-session-runtime.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-runtime.test.ts @@ -18,6 +18,11 @@ import { foundAgentSessionRecord } from './records/agent-session-record-founding import { closeTestJournalHostDatabases } from '../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import { recordingStructuredAgentSessionLogger } from '../../native-chat/agent-session-wire/host/testing/structured-agent-session-logger.test-fixture' import { onStructuredAgentSessionsHeldChanged } from '../../native-chat/agent-session-wire/host/structured-agent-session-registry' +import { openAgentSessionRecordStoreOnce } from './records/agent-session-record-store-slot' +import { + journalSqlReaderPath, + journalSqlWorkerPath +} from '../../native-chat/agent-session-journal/journal-sql-worker-bundle.test-fixture' const HOST_ID = 'local' @@ -58,6 +63,14 @@ describe('structured agent-session runtime install', () => { fullAccessAgents: readonly string[], logger = recordingStructuredAgentSessionLogger().logger ) { + await openAgentSessionRecordStoreOnce({ + stateDirectory: stateDirectory!, + hostId: HOST_ID, + logger, + legacySessionTabIds: [], + writerPath: journalSqlWorkerPath(), + readerPath: journalSqlReaderPath() + }) return await ensureStructuredAgentSessionHost({ logger, stateDirectory: stateDirectory!, diff --git a/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest.test.ts b/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest.test.ts index 687cfeea81a..bb2ee4f81cc 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/protocol/structured-agent-session-at-rest.test.ts @@ -65,15 +65,11 @@ it('commits a legacy no-choice answer before reply and replays it after upgrade options: { effort: 'high' }, now: fixture.rig.clock.now }) - const replace = vi.spyOn(fixture.rig.store, 'replaceSessionOptions') - fixture.rig.sink.publish.mockClear() const params = optionRequest('') const inert = { key: 'model', value: '', options: {} } expect((await call('agentSession.setOption', params))[0]).toMatchObject({ result: { ok: true, replayed: false, value: inert } }) - expect(replace).not.toHaveBeenCalled() - expect(fixture.rig.sink.publish).not.toHaveBeenCalled() expect(fixture.rig.store.getRecord(SESSION)?.options).toEqual({ effort: 'high' }) expect( fixture.rig.store.getOperationRow('device-1', params.envelope.clientOperationId)?.outcome @@ -424,12 +420,15 @@ it('resets catch-up over an unreadable event while keeping the current conversat page: { items: [{ body: { kind: 'message' } }] } } }) - expect( - (await call('agentSession.subscribe', { sessionId: SESSION, cursor: stale }))[0] - ).toMatchObject({ ok: true, result: { type: 'reset', reset: 'schema_unreadable' } }) - expect( - (await call('agentSession.subscribe', { sessionId: SESSION, cursor: live }))[0] - ).toMatchObject({ ok: true, result: { type: 'batch' } }) + const reset = await call('agentSession.subscribe', { sessionId: SESSION, cursor: stale }) + await vi.waitFor(() => + expect(reset[0]).toMatchObject({ + ok: true, + result: { type: 'reset', reset: 'schema_unreadable' } + }) + ) + const caughtUp = await call('agentSession.subscribe', { sessionId: SESSION, cursor: live }) + await vi.waitFor(() => expect(caughtUp[0]).toMatchObject({ ok: true, result: { type: 'batch' } })) }) it('keeps a storage failure during catch-up retryable instead of resetting the client', async () => { From 0839597b6b3c44c05411887cfdecf2c3fbf3460a Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:49:24 -0800 Subject: [PATCH 22/45] test(native-chat): encode the full founding identity --- .../durable-rows/journal-row-context-usage.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/native-chat/agent-session-journal/durable-rows/journal-row-context-usage.spec.ts b/src/main/native-chat/agent-session-journal/durable-rows/journal-row-context-usage.spec.ts index 50198166482..82eb33074e6 100644 --- a/src/main/native-chat/agent-session-journal/durable-rows/journal-row-context-usage.spec.ts +++ b/src/main/native-chat/agent-session-journal/durable-rows/journal-row-context-usage.spec.ts @@ -101,7 +101,7 @@ export function registerJournalRowContextUsageTests(): void { ts: 1_000, fence: 1, reason: 'session_created', - providerHandle: agentSessionJournalProviderHandle(IDENTITY.providerHandle) + providerHandle: agentSessionJournalProviderHandle(IDENTITY) }, { v: 3, From 3fff993349f4cf5a527a525f6cb89e871fd13f06 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 01:11:05 -0800 Subject: [PATCH 23/45] fix(native-chat): recover saved live work on SQL open --- .../journal-subagent-item.spec.ts | 2 +- .../journal-open-recovery.test.ts | 13 +- .../journal-sql-open-command.ts | 114 +++++++++++++----- 3 files changed, 95 insertions(+), 34 deletions(-) diff --git a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-subagent-item.spec.ts b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-subagent-item.spec.ts index 2d2ffe7c181..0b1b7f0bf6e 100644 --- a/src/main/native-chat/agent-session-journal/item-lifecycle/journal-subagent-item.spec.ts +++ b/src/main/native-chat/agent-session-journal/item-lifecycle/journal-subagent-item.spec.ts @@ -67,7 +67,7 @@ export function registerJournalSubagentItemTests(): void { }) await live.close() - const reopened = await open({ recover: { fence: 0 } }) + const reopened = await open() const row = (await readTestJournalItems(reopened)).at(-1)! expect(row.recovered).toBe(true) expect(row.body).toMatchObject({ diff --git a/src/main/native-chat/agent-session-journal/journal-open-recovery.test.ts b/src/main/native-chat/agent-session-journal/journal-open-recovery.test.ts index 22070451172..094b1c381ea 100644 --- a/src/main/native-chat/agent-session-journal/journal-open-recovery.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-open-recovery.test.ts @@ -106,9 +106,20 @@ it('marks a working subagent as unverifiable when another handle opens its saved }, options: { fence: 1, turnScope: { kind: 'thread' } } }) - await f.run({ kind: 'open' }) + const opened = await f.run({ kind: 'open' }) + expect(opened.value).toEqual({ epoch: 'epoch', sequence: 1 }) expect(f.item('orca:roster')).toMatchObject({ recovered: true, body: { blocks: [{ agents: [{ id: 'a', state: 'unverifiable' }] }] } }) + const recovered = f.item('orca:roster')! + expect(recovered).toMatchObject({ sequence: 1, revision: 2 }) + expect(recovered.body.kind === 'message' && recovered.body.blocks[0]).toEqual({ + type: 'subagent-group', + groupId: 'group', + agents: [{ id: 'a', label: 'read', state: 'unverifiable', startedAt: 10 }] + }) + f.reopen() + await f.run({ kind: 'open' }) + expect(f.item('orca:roster')).toEqual(recovered) }) diff --git a/src/main/native-chat/agent-session-journal/journal-sql-open-command.ts b/src/main/native-chat/agent-session-journal/journal-sql-open-command.ts index 61b5bd76cc3..e75d3e8f43a 100644 --- a/src/main/native-chat/agent-session-journal/journal-sql-open-command.ts +++ b/src/main/native-chat/agent-session-journal/journal-sql-open-command.ts @@ -8,6 +8,12 @@ import { journalDomainQueueAwaitsReopen } from './journal-domain-queue-reads' import { finishJournalSqlCommand } from './journal-sql-command-result' import { checkpointJournalRequestEvents } from './journal-writer-request-events' import { journalSqlFailure, type JournalSqlFailure } from './journal-sql-failure' +import { + AGENT_JOURNAL_THREAD_SCOPE, + type AgentJournalRenderItem +} from '../../../shared/agent-session/journal/agent-session-journal-types' +import { staleSubagentRosterRevisions } from './item-lifecycle/journal-subagent-liveness' +import { applyJournalDomainLifecycle } from './journal-domain-lifecycle' export type JournalOpenRecoveryResult = { failures: { @@ -28,12 +34,10 @@ export function applyJournalSqlOpenCommand( ) { const opened = applyJournalSqlEditLeaseCommand(db, stateDirectory, command, savedAt) const { recover } = command.operation - if (!recover) { - return opened - } const { target } = command // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: open returns its physical pre-recovery cursor. const openedThrough = opened.value as AgentJournalCursor + settleOpeningLiveWork(db, target, savedAt) const recovery: JournalOpenRecoveryResult = { failures: [], reopenFloor: null } const attempt = ( step: JournalOpenRecoveryResult['failures'][number]['step'], @@ -52,38 +56,40 @@ export function applyJournalSqlOpenCommand( return undefined } } - for (const kind of ['pending-unknown', 'hold-unsent'] as const) { - const result = attempt(kind, () => - applyJournalDomainInputRecovery( - db, - target, - kind === 'pending-unknown' - ? { kind, fence: recover.fence } - : { kind, fence: recover.fence, hold: { cause: 'hostRestarted' } }, - { openedThrough }, - savedAt + if (recover) { + for (const kind of ['pending-unknown', 'hold-unsent'] as const) { + const result = attempt(kind, () => + applyJournalDomainInputRecovery( + db, + target, + kind === 'pending-unknown' + ? { kind, fence: recover.fence } + : { kind, fence: recover.fence, hold: { cause: 'hostRestarted' } }, + { openedThrough }, + savedAt + ) ) - ) - if (result && 'failure' in result.value) { - recovery.failures.push({ step: kind, failure: result.value.failure }) + if (result && 'failure' in result.value) { + recovery.failures.push({ step: kind, failure: result.value.failure }) + } } + attempt('queue-reopen', () => { + if (journalDomainQueueAwaitsReopen(db, target)) { + const head = db + .prepare('SELECT last_sequence FROM journal_chat_state WHERE session_id=? AND epoch=?') + .get(target.sessionId, target.epoch)! + recovery.reopenFloor = { epoch: target.epoch, sequence: Number(head.last_sequence) + 1 } + applyJournalDomainControlCommand( + db, + target, + { kind: 'queue-reopen-mark', fence: recover.fence }, + {}, + savedAt + ) + recovery.reopenFloor = null + } + }) } - attempt('queue-reopen', () => { - if (journalDomainQueueAwaitsReopen(db, target)) { - const head = db - .prepare('SELECT last_sequence FROM journal_chat_state WHERE session_id=? AND epoch=?') - .get(target.sessionId, target.epoch)! - recovery.reopenFloor = { epoch: target.epoch, sequence: Number(head.last_sequence) + 1 } - applyJournalDomainControlCommand( - db, - target, - { kind: 'queue-reopen-mark', fence: recover.fence }, - {}, - savedAt - ) - recovery.reopenFloor = null - } - }) const head = db .prepare('SELECT last_sequence FROM journal_chat_state WHERE session_id=? AND epoch=?') .get(target.sessionId, target.epoch)! @@ -107,3 +113,47 @@ export function applyJournalSqlOpenCommand( openRecovery: recovery } } + +function settleOpeningLiveWork( + db: Database.Database, + target: { sessionId: string; epoch: string }, + savedAt: number +): void { + let after: [number, string] | undefined + for (;;) { + const rows = db + .prepare(`SELECT ordinal,item_id,payload_json FROM journal_items + INDEXED BY journal_items_unsettled WHERE session_id=? AND epoch=? AND hidden=0 + AND (lifecycle_state IN ('running','pending') OR live_work=1) AND live_work=1 + ${after ? 'AND (ordinal,item_id)>(?,?)' : ''} ORDER BY ordinal,item_id LIMIT 128`) + .all(target.sessionId, target.epoch, ...(after ?? [])) + const items: AgentJournalRenderItem[] = rows.map((row) => JSON.parse(String(row.payload_json))) + const revisions = staleSubagentRosterRevisions(items) + if (revisions.length > 0) { + const head = db + .prepare(`SELECT last_sequence,highest_fence FROM journal_chat_state + WHERE session_id=? AND epoch=?`) + .get(target.sessionId, target.epoch)! + applyJournalDomainLifecycle( + db, + target, + { + settlementId: `open-live-work:${head.last_sequence}`, + fence: Number(head.highest_fence), + recovered: true, + mutations: revisions.map((revision) => ({ + ...revision, + kind: 'item', + turnScope: AGENT_JOURNAL_THREAD_SCOPE + })) + }, + savedAt + ) + } + if (rows.length < 128) { + return + } + const last = rows.at(-1)! + after = [Number(last.ordinal), String(last.item_id)] + } +} From ca86a72bd82a774d27f915a6cbbf7bb0334b55a8 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 01:20:41 -0800 Subject: [PATCH 24/45] fix(native-chat): select retained image sources before history paging --- .../journal-domain-schema-001-indexes.ts | 3 + .../journal-domain-history-after.ts | 133 ++++++++++- .../journal-domain-history-page.ts | 209 +++++++++--------- .../journal-domain-history-window.ts | 43 +++- .../journal-domain-history.test.ts | 178 ++++++++++++++- .../journal-domain-source-items.test.ts | 28 ++- .../journal-domain-source-items.ts | 162 ++++++++++++-- .../history/agent-session-history-page.ts | 59 +++-- .../agent-session-queued-image-reader.ts | 134 +++++------ ...ent-session-queued-image-lifecycle.test.ts | 57 +++-- ...session-queued-image-wire-contract.test.ts | 201 ++++++++++++++--- 11 files changed, 930 insertions(+), 277 deletions(-) diff --git a/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001-indexes.ts b/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001-indexes.ts index 7ea3ca50708..8b890896ea1 100644 --- a/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001-indexes.ts +++ b/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001-indexes.ts @@ -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); diff --git a/src/main/native-chat/agent-session-journal/journal-domain-history-after.ts b/src/main/native-chat/agent-session-journal/journal-domain-history-after.ts index 9639d1d3b91..c69b578e208 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-history-after.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-history-after.ts @@ -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) => AgentJournalSnapshot, reset: ( reason: Extract['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 ): 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[] { + 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 +): 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, + 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 +} diff --git a/src/main/native-chat/agent-session-journal/journal-domain-history-page.ts b/src/main/native-chat/agent-session-journal/journal-domain-history-page.ts index 1a90b940ef2..b2fe154f0d5 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-history-page.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-history-page.ts @@ -1,6 +1,5 @@ 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 type Database from '../../sqlite/sync-database' import { AGENT_SESSION_HISTORY_MAX_LIMIT, @@ -13,8 +12,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, @@ -23,11 +22,28 @@ import { } 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 +53,24 @@ export type JournalDomainHistoryRequest = Omit +type HistoryReaderForm = Pick & { + presentSubmission?: (send: AgentJournalSubmission) => AgentJournalSubmission +} /** The caller owns a readonly connection; head, headers, bodies and any reset share one snapshot. */ export function readJournalDomainHistoryPage( @@ -61,14 +83,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 +148,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 +184,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[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 +228,16 @@ function historyPage( }, direction: request.direction === 'hydrate' ? 'tail' : request.direction, items, - hasOlder: !!db - .prepare(`SELECT 1 FROM journal_items WHERE ${where} AND sequence 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 +245,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() 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 -): 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) } diff --git a/src/main/native-chat/agent-session-journal/journal-domain-history-window.ts b/src/main/native-chat/agent-session-journal/journal-domain-history-window.ts index 7d1282238d8..2e2d23861fd 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-history-window.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-history-window.ts @@ -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( 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) + } +} diff --git a/src/main/native-chat/agent-session-journal/journal-domain-history.test.ts b/src/main/native-chat/agent-session-journal/journal-domain-history.test.ts index 419e1de0c3a..2faed88fe5a 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-history.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-history.test.ts @@ -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() diff --git a/src/main/native-chat/agent-session-journal/journal-domain-source-items.test.ts b/src/main/native-chat/agent-session-journal/journal-domain-source-items.test.ts index 9e73114efe0..00c05591859 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-source-items.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-source-items.test.ts @@ -8,6 +8,7 @@ import { messageFixture, target } from './journal-sql-message.test-fixture' import { domainLifecycleFixture, lifecycleIdentity } from './journal-domain-lifecycle.test-fixture' import { journalSqlReads } from './journal-sql-reads' import { readJournalSqlPoint } from './journal-sql-point-read' +import { readJournalDomainHistoryPage } from './journal-domain-history-page' import { agentSessionFailureWords } from '../../../shared/agent-session/failures/agent-session-failure-words' import { codexProviderHandle, @@ -15,7 +16,10 @@ import { } from '../../../shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import { insertJournalDomainQueueRow, journalDomainQueueRow } from './journal-domain-queue-rows' import { agentSessionSendBodyFingerprint } from '../../../shared/agent-session/requests/structured-agent-session-send-mutation' -import type { AgentJournalMessageItem } from '../../../shared/agent-session/journal/agent-session-journal-types' +import type { + AgentJournalMessageItem, + AgentJournalRenderItem +} from '../../../shared/agent-session/journal/agent-session-journal-types' const cleanup: (() => void)[] = [] afterEach(() => cleanup.splice(0).forEach((close) => close())) @@ -84,7 +88,7 @@ it('imports independent raw and outcome sources without creating public attempts ) expect(await reads.submission('raw')).toBeUndefined() expect(await reads.submission('outcome')).toBeUndefined() - expect(await reads.historySourceItems(['raw', 'outcome'])).toEqual([ + const originals: AgentJournalRenderItem[] = [ { itemId: 'orca:raw', revision: 0, @@ -108,7 +112,25 @@ it('imports independent raw and outcome sources without creating public attempts recovered: true, recoveredAt: 25 } - ]) + ] + expect(await reads.historySourceItems(['raw', 'outcome'])).toEqual(originals) + const page = readJournalDomainHistoryPage(f.db, { + sessionId: 'chat', + direction: 'tail', + reader: { + form: 'client', + queuedImages: { + ids: [ + ['raw', true], + ['outcome', true] + ], + imageIds: [] + } + } + }).page + expect(page.items).toEqual(originals) + expect(page.submissions).toEqual([]) + expect(page.hasOlder).toBe(false) state.inputs.handovers.set('outcome', { kind: 'dispatch', v: 4, diff --git a/src/main/native-chat/agent-session-journal/journal-domain-source-items.ts b/src/main/native-chat/agent-session-journal/journal-domain-source-items.ts index 74126b2d38b..5111c872bce 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-source-items.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-source-items.ts @@ -7,6 +7,150 @@ import { agentJournalLinkageFields } from '../../../shared/agent-session/journal import type { JournalSqlScope } from './journal-sql-scope' import type { JournalDomainSubmissionStoredFacts } from './journal-domain-submission-records' import { journalDomainQueueRow } from './journal-domain-queue-rows' +import type { JournalDomainHistoryHeader } from './journal-domain-history-window' + +export type JournalDomainSourceHeader = JournalDomainHistoryHeader & { + source: { + id: string + placement: Omit + attemptSequence: number | null + attemptIndex: number | null + } +} + +export function isJournalDomainSourceHeader( + header: JournalDomainHistoryHeader +): header is JournalDomainSourceHeader { + return 'source' in header +} + +/** The supplied population is the current image queue, never retained submission history. */ +export function readJournalDomainSourceHeaders( + scope: JournalSqlScope, + ids: readonly string[], + window: { before?: number; from?: number; limit?: number; own?: boolean } = {} +): JournalDomainSourceHeader[] { + if (ids.length === 0) { + return [] + } + const wanted = ids.map((id) => ({ id, itemId: agentJournalSubmissionKey(id) })) + return scope.db + .prepare(`SELECT json_extract(wanted.value,'$.id') AS id, + json_extract(wanted.value,'$.itemId') AS item_id, wanted.key AS source_order, + CASE WHEN json_extract(owner.facts_json,'$.admission.sourcePlacement') IS NOT NULL + THEN COALESCE(json_extract(owner.facts_json,'$.handover.sequence'), + json_extract(owner.facts_json,'$.admission.sourcePlacement.sequence')) + ELSE COALESCE((CASE WHEN json_valid(card.payload_json) THEN json_extract(card.payload_json,'$.queuedAt.sequence') END),0) END AS source_sequence, + CASE WHEN json_extract(owner.facts_json,'$.handover') IS NOT NULL THEN 0 + ELSE COALESCE(json_extract(owner.facts_json,'$.admission.sourcePlacement.sequenceIndex'),0) + END AS source_index, + owner.attempt_sequence,owner.attempt_index,owner.facts_json, + octet_length(owner.admission_body_json) AS body_bytes, + CASE WHEN json_extract(owner.facts_json,'$.admission.sourcePlacement') IS NULL + THEN (CASE WHEN json_valid(card.payload_json) THEN json_extract(card.payload_json,'$.queuedAt.sequence') END) END AS queued_sequence, + CASE WHEN json_extract(owner.facts_json,'$.admission.sourcePlacement') IS NULL + THEN (CASE WHEN json_valid(card.payload_json) THEN octet_length(json_extract(card.payload_json,'$.body')) END) END AS card_body_bytes, + COALESCE(octet_length(current.payload_json),0) AS submission_bytes + FROM json_each(?3) wanted + LEFT JOIN journal_submission_attempts owner ON owner.rowid=( + SELECT rowid FROM journal_submission_attempts + WHERE session_id=?1 AND epoch=?2 AND message_id=json_extract(wanted.value,'$.id') + ORDER BY attempt_sequence DESC,attempt_index DESC LIMIT 1) + LEFT JOIN journal_submission_attempts current ON current.session_id=?1 AND current.epoch=?2 + AND current.message_id=json_extract(wanted.value,'$.id') AND current.current=1 + LEFT JOIN journal_queue_cards card INDEXED BY journal_queue_source_metadata + ON card.session_id=?1 AND card.epoch=?2 + AND card.message_id=json_extract(wanted.value,'$.id') + WHERE COALESCE(current.dispatch_state,'')<>'accepted' AND NOT EXISTS( + SELECT 1 FROM journal_items WHERE session_id=?1 AND epoch=?2 + AND item_id=json_extract(wanted.value,'$.itemId') AND hidden=0) + AND (json_extract(owner.facts_json,'$.admission.sourcePlacement') IS NOT NULL + OR card.message_id IS NOT NULL) + AND source_sequence=?5 + AND (?7=0 OR json_extract(owner.facts_json,'$.admission.sourcePlacement.agentId') IS NULL) + ORDER BY source_sequence DESC,source_index DESC,CAST(wanted.key AS INTEGER) DESC LIMIT ?6`) + .all( + ...scope.prefix, + JSON.stringify(wanted), + window.before ?? Number.MAX_SAFE_INTEGER, + window.from ?? -Number.MAX_SAFE_INTEGER, + window.limit ?? ids.length, + Number(window.own === true) + ) + .map((row) => { + const facts = row.facts_json + ? scope.decode(row.facts_json) + : undefined + const original = facts?.admission?.sourcePlacement + const placement = original + ? sourcePlacement(String(row.item_id), original, facts?.handover) + : { + itemId: String(row.item_id), + revision: 0, + sequence: Number(row.queued_sequence ?? 0), + observedAt: 0, + turnScope: { kind: 'thread' as const } + } + return { + itemId: placement.itemId, + sequence: placement.sequence, + sequenceIndex: placement.sequenceIndex ?? 0, + ordinal: Number.MAX_SAFE_INTEGER - ids.length + Number(row.source_order), + own: placement.agentId === undefined, + subagentGroupKey: null, + bytes: + Buffer.byteLength(JSON.stringify(placement)) + + 8 + + Number(original ? row.body_bytes : row.card_body_bytes) + + Number(row.submission_bytes), + source: { + id: String(row.id), + placement, + attemptSequence: original ? Number(row.attempt_sequence) : null, + attemptIndex: original ? Number(row.attempt_index) : null + } + } + }) +} + +function sourcePlacement( + itemId: string, + original: Omit, + handover: JournalDomainSubmissionStoredFacts['handover'] | undefined +): Omit { + return handover + ? { + itemId, + revision: 0, + sequence: handover.sequence, + observedAt: handover.at, + turnScope: { kind: 'thread' }, + ...(handover.recovered ? { recovered: true, recoveredAt: handover.at } : {}), + ...agentJournalLinkageFields(original) + } + : original +} + +/** Read only a positional/event candidate, including an ambiguous exact byte-boundary group. */ +export function readJournalDomainSourceBody( + scope: JournalSqlScope, + { source }: JournalDomainSourceHeader +): AgentJournalRenderItem { + const row = + source.attemptSequence === null + ? scope.db + .prepare(`SELECT json_extract(payload_json,'$.body') AS body + FROM journal_queue_cards WHERE session_id=? AND epoch=? AND message_id=?`) + .get(...scope.prefix, source.id) + : scope.db + .prepare(`SELECT admission_body_json AS body FROM journal_submission_attempts + WHERE session_id=? AND epoch=? AND message_id=? AND attempt_sequence=? AND attempt_index=?`) + .get(...scope.prefix, source.id, source.attemptSequence, source.attemptIndex) + if (!row?.body) { + throw new Error('retained journal source has no owned body') + } + return { ...source.placement, body: scope.decode(row.body) } +} /** Only the queue publication's reached identities may read an independent admission source. */ export function readJournalDomainSourceItems( @@ -31,7 +175,7 @@ export function readJournalDomainSourceItems( ORDER BY attempt_sequence DESC,attempt_index DESC LIMIT 1`) .get(...scope.prefix, id) const facts = owner && scope.decode(owner.facts_json) - const placement = facts?.admission.sourcePlacement + const placement = facts?.admission?.sourcePlacement if (placement && owner) { const row = scope.db .prepare(`SELECT admission_body_json FROM journal_submission_attempts @@ -41,21 +185,7 @@ export function readJournalDomainSourceItems( throw new Error('retained journal source has no owned admission body') } const body = scope.decode(row.admission_body_json) - const handover = facts.handover - return [ - handover - ? { - itemId, - revision: 0, - body, - sequence: handover.sequence, - observedAt: handover.at, - turnScope: { kind: 'thread' as const }, - ...(handover.recovered ? { recovered: true as const, recoveredAt: handover.at } : {}), - ...agentJournalLinkageFields(placement) - } - : { ...placement, body } - ] + return [{ ...sourcePlacement(itemId, placement, facts.handover), body }] } const card = journalDomainQueueRow(scope.db, scope, id)?.card return card diff --git a/src/main/native-chat/agent-session-wire/publication/history/agent-session-history-page.ts b/src/main/native-chat/agent-session-wire/publication/history/agent-session-history-page.ts index d253d0ff8f0..c520a88bedf 100644 --- a/src/main/native-chat/agent-session-wire/publication/history/agent-session-history-page.ts +++ b/src/main/native-chat/agent-session-wire/publication/history/agent-session-history-page.ts @@ -6,6 +6,7 @@ import type { AgentSessionHistoryPage } from '../../../../../shared/agent-session/agent-session-wire' import type { HistoryReader } from './agent-session-history-page-bounds' +import { heldQueuedImageHistoryPolicy } from './agent-session-queued-image-reader' import { CLIENT_HISTORY_READER } from './agent-session-item-wire-form' export type AgentSessionHistoryScope = 'every-agent' | 'own-agent' @@ -24,21 +25,30 @@ export async function readAgentSessionHistory( rowReplay?: boolean } = {} ): Promise { - const result = await journal.history({ - ...request, - scope, - rowReplay, - reader: { form: reader.wireForm ?? 'client', budgetBytes: reader.budgetBytes } - }) - return { - ...result, - page: await projectPage( - journal, - result.page, - reader, - request.direction === 'after' && result.ok - ) + const read = async (): Promise => { + const result = await journal.history({ + ...request, + scope, + rowReplay, + reader: { + form: reader.wireForm ?? 'client', + budgetBytes: reader.budgetBytes, + ...(reader.queueSourceIds + ? { queuedImages: await heldQueuedImageHistoryPolicy(journal) } + : {}) + } + }) + return { + ...result, + page: await projectPage( + journal, + result.page, + reader, + request.direction === 'after' && result.ok + ) + } } + return reader.queueSourceIds ? journal.readInOrder(read) : read() } export function createAgentSessionCatchUpReader( @@ -53,12 +63,21 @@ export async function readAgentSessionHydrationPage( fence?: number, reader: HistoryReader = CLIENT_HISTORY_READER ): Promise { - const result = await journal.history({ - direction: 'hydrate', - fence, - reader: { form: reader.wireForm ?? 'client', budgetBytes: reader.budgetBytes } - }) - return projectPage(journal, result.page, reader, false) + const read = async () => { + const result = await journal.history({ + direction: 'hydrate', + fence, + reader: { + form: reader.wireForm ?? 'client', + budgetBytes: reader.budgetBytes, + ...(reader.queueSourceIds + ? { queuedImages: await heldQueuedImageHistoryPolicy(journal) } + : {}) + } + }) + return projectPage(journal, result.page, reader, false) + } + return reader.queueSourceIds ? journal.readInOrder(read) : read() } async function projectPage( diff --git a/src/main/native-chat/agent-session-wire/publication/history/agent-session-queued-image-reader.ts b/src/main/native-chat/agent-session-wire/publication/history/agent-session-queued-image-reader.ts index 9b7e6e4bbe6..09263c8b333 100644 --- a/src/main/native-chat/agent-session-wire/publication/history/agent-session-queued-image-reader.ts +++ b/src/main/native-chat/agent-session-wire/publication/history/agent-session-queued-image-reader.ts @@ -7,16 +7,14 @@ import type { AgentSessionSubscribeEvent } from '../../../../../shared/agent-ses import { agentJournalSubmissionKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' import { compareAgentJournalItems } from '../../../../../shared/agent-session/journal/agent-session-journal-position' import type { SqlAgentSessionJournal as AgentSessionJournal } from '../../../agent-session-journal/journal-sql-store' +import type { QueuedMessageRow } from '../../../agent-session-journal/queued-messages/queued-message-table' import { agentSessionPublishedWorkFields, type AgentSessionPublishedWork } from '../../observations/agent-session-published-work' import { boundedFrameItem } from './agent-session-bounded-frame-item' import { agentJournalItemForWire } from './agent-session-item-wire-form' -import { - readPublishedQueuedMessages, - type QueuePublication -} from '../../delivery/structured-agent-session-queued-publication' +import type { QueuePublication } from '../../delivery/structured-agent-session-queued-publication' export const queuedCardHoldsImage = (card: Pick): boolean => card.body.blocks.some((block) => block.type === 'image-ref') @@ -32,7 +30,8 @@ const relations = new WeakMap< >() async function sourceIds( journal: AgentSessionJournal, - cards: readonly Pick[] + cards: readonly Pick[], + queueRows?: readonly QueuedMessageRow[] ) { const cached = relations.get(cards) if (cached) { @@ -43,7 +42,7 @@ async function sourceIds( const fingerprints = new Map() const imageCards = cards.filter(queuedCardHoldsImage) const rows = new Map( - (imageCards.length ? await journal.queuedMessages.list() : []).map((row) => [ + (imageCards.length ? (queueRows ?? (await journal.queuedMessages.list())) : []).map((row) => [ row.messageId, row ]) @@ -91,92 +90,77 @@ function sourcePresentation(send: AgentJournalSubmission, active: boolean) { } async function queuedImageInputs(journal: AgentSessionJournal, ids: readonly string[]) { - const inputs = new Map< - string, - { submission: AgentJournalSubmission | undefined; ordinal: number } - >() + const inputs = new Map() for (let start = 0; start < ids.length; start += 128) { const reached = ids.slice(start, start + 128) const { results } = await journal.reads.input({ kind: 'batch', - reads: reached.flatMap((id) => [ - { kind: 'input-for-item' as const, itemId: agentJournalSubmissionKey(id) }, - { kind: 'input-ordinal' as const, clientMessageId: id } - ]) + reads: reached.map((id) => ({ + kind: 'input-for-item' as const, + itemId: agentJournalSubmissionKey(id) + })) }) reached.forEach((id, index) => { - const send = results[index * 2] - const order = results[index * 2 + 1] + const send = results[index] inputs.set(id, { - submission: 'submission' in send ? send.submission : undefined, - ordinal: 'ordinal' in order ? order.ordinal : Infinity + submission: 'submission' in send ? send.submission : undefined }) }) } return inputs } -export async function queuedImageReaderSnapshot( +export function queuedImageReaderSnapshot( journal: AgentSessionJournal, snapshot?: AgentJournalSnapshot, - touched?: ReadonlySet -): Promise { - const cards = await readPublishedQueuedMessages(journal) - return journal.readInOrder(async () => { - const raw = snapshot ?? { + _touched?: ReadonlySet +): AgentJournalSnapshot { + // SQL has already selected membership and presented submissions within the ordered read. + return ( + snapshot ?? { sessionId: journal.identity.sessionId, cursor: journal.cursor(), items: [], submissions: [] } - const { ids, imageIds } = await sourceIds(journal, cards) - if (imageIds.size === 0) { - return raw - } - const present = (send: AgentJournalSubmission) => - ids.has(send.clientMessageId) || imageIds.has(send.queuedMessageId ?? '') - ? sourcePresentation(send, ids.get(send.clientMessageId) === true) - : send - const reached = [...ids] - .filter( - ([id, unchanged]) => unchanged && (!touched || touched.has(agentJournalSubmissionKey(id))) - ) - .map(([id]) => id) - const inputs = await queuedImageInputs(journal, [ - ...new Set([...reached, ...raw.submissions.map((send) => send.clientMessageId)]) + ) +} + +export type QueuedImageHistoryPolicy = { + ids: readonly (readonly [string, boolean])[] + imageIds: readonly string[] + links?: readonly (readonly [string, readonly string[]])[] +} + +/** Held read: its caller owns the chat lane through history materialization and presentation. */ +export async function heldQueuedImageHistoryPolicy( + journal: AgentSessionJournal +): Promise { + const rows = await journal.queuedMessages.list() + const cards = rows.filter((row) => row.state === 'waiting' || row.state === 'returned') + const { ids, imageIds } = await sourceIds(journal, cards, rows) + return { + ids: [...ids], + imageIds: [...imageIds], + links: cards.map((row) => [ + row.messageId, + [ + row.messageId, + ...new Set( + [row.recovers, row.consumedAs].filter((id): id is string => typeof id === 'string') + ) + ] ]) - const saved = new Set(await journal.reads.savedItemIds(reached.map(agentJournalSubmissionKey))) - const retainedIds = reached.filter( - (id) => - inputs.get(id)?.submission?.dispatchState !== 'accepted' && - !saved.has(agentJournalSubmissionKey(id)) - ) - const retained = retainedIds.length ? await journal.reads.historySourceItems(retainedIds) : [] - const submissions = new Map( - raw.submissions.map((send) => [send.clientMessageId, present(send)]) - ) - for (const item of retained) { - const id = retainedIds.find((id) => agentJournalSubmissionKey(id) === item.itemId) - const send = id && inputs.get(id)?.submission - if (send) { - submissions.set(send.clientMessageId, present(send)) - } - } - return { - ...raw, - items: retained.length - ? [...raw.items, ...retained].sort(compareAgentJournalItems) - : raw.items, - submissions: retained.length - ? [...submissions.values()].sort( - (a, b) => - a.submittedAt - b.submittedAt || - (inputs.get(a.clientMessageId)?.ordinal ?? Infinity) - - (inputs.get(b.clientMessageId)?.ordinal ?? Infinity) - ) - : raw.submissions.map(present) - } - }) + } +} + +export function queuedImageSubmissionPresenter(policy: QueuedImageHistoryPolicy) { + const ids = new Map(policy.ids) + const imageIds = new Set(policy.imageIds) + return (send: AgentJournalSubmission): AgentJournalSubmission => + ids.has(send.clientMessageId) || imageIds.has(send.queuedMessageId ?? '') + ? sourcePresentation(send, ids.get(send.clientMessageId) === true) + : send } /** Only queue/source semantic changes correct rows; work and pause never replay the population. */ @@ -189,13 +173,17 @@ export async function queuedImageReaderEvent( ): Promise { if ( !current || - event.type !== 'batch' || - (previous?.queuedMessages === current.queuedMessages && event.batch.submissions.length === 0) + (event.type === 'batch' && + previous?.queuedMessages === current.queuedMessages && + event.batch.submissions.length === 0) ) { return event } return journal.readInOrder(async () => { const { ids: now, imageIds, fingerprints } = await sourceIds(journal, current.queuedMessages) + if (event.type !== 'batch') { + return event + } const prior = await sourceIds(journal, previous?.queuedMessages ?? []) const before = prior.ids if ( diff --git a/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-lifecycle.test.ts b/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-lifecycle.test.ts index ea8d37d3149..3ea4c269bb7 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-lifecycle.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-lifecycle.test.ts @@ -145,6 +145,8 @@ it('publishes a complete image row to old readers before it has a submission', a setStructuredAgentSessionHost(rig.host) await rig.workingSend() const frames = await openStream('agentSession.subscribe', { sessionId: SESSION }, OLD) + await eventually(() => expect(streamState(frames).cursor).toEqual(journal().cursor())) + const cursor = journal().cursor() const body = await imageBody('waiting image') const id = hostTestOperationId() const fields = { body, delivery: 'queue-if-active' as const } @@ -156,6 +158,12 @@ it('publishes a complete image row to old readers before it has a submission', a expect((await history()).page.items).toContainEqual( expect.objectContaining({ itemId: agentJournalSubmissionKey(id), body }) ) + expect((await history({ direction: 'after', cursor })).page.items).toContainEqual( + expect.objectContaining({ itemId: agentJournalSubmissionKey(id), body }) + ) + expect((await history({ direction: 'after', cursor }, false)).page.items).not.toContainEqual( + expect.objectContaining({ itemId: agentJournalSubmissionKey(id) }) + ) await eventually(() => expect(streamState(frames).items).toContainEqual( expect.objectContaining({ itemId: agentJournalSubmissionKey(id), body }) @@ -472,6 +480,11 @@ it.each([false, true])( }) expect(JSON.stringify(transient.data)).toContain('file:///device/composer-preview.png') const frames = await openStream('agentSession.subscribe', { sessionId: SESSION }, OLD) + await eventually(() => + expect(streamState(frames).items).toContainEqual( + expect.objectContaining({ itemId: agentJournalSubmissionKey(presentedId), body }) + ) + ) const fields = { messageId: card.messageId, expectedBodyFingerprint: (await journal().queuedMessages.get(card.messageId))!.fingerprint, @@ -500,32 +513,34 @@ it.each([false, true])( ).toMatchObject([ { messageId: card.messageId, command: true, text: expect.stringContaining('edited original') } ]) - expect(frames).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - result: expect.objectContaining({ - type: 'batch', - batch: expect.objectContaining({ - submissions: expect.arrayContaining([ + await eventually(() => + expect(frames).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + result: expect.objectContaining({ + type: 'batch', + batch: expect.objectContaining({ + submissions: expect.arrayContaining([ + expect.objectContaining({ + clientMessageId: presentedId, + keptAsQueuedMessageId: card.messageId + }) + ]) + }) + }) + }), + expect.objectContaining({ + result: expect.objectContaining({ + queuedMessages: expect.arrayContaining([ expect.objectContaining({ - clientMessageId: presentedId, - keptAsQueuedMessageId: card.messageId + messageId: card.messageId, + body: expect.objectContaining({ command: { name: 'compact' } }) }) ]) }) }) - }), - expect.objectContaining({ - result: expect.objectContaining({ - queuedMessages: expect.arrayContaining([ - expect.objectContaining({ - messageId: card.messageId, - body: expect.objectContaining({ command: { name: 'compact' } }) - }) - ]) - }) - }) - ]) + ]) + ) ) const live = frames.reduce((state, frame) => { if (!frame.ok) { diff --git a/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-wire-contract.test.ts b/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-wire-contract.test.ts index d80ac281740..0a71e578a20 100644 --- a/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-wire-contract.test.ts +++ b/src/main/runtime/rpc/methods/structured-sessions/attachments/structured-agent-session-queued-image-wire-contract.test.ts @@ -1,3 +1,8 @@ +import * as historyPages from '../../../../../native-chat/agent-session-wire/publication/history/agent-session-history-page' +import { + queuedImageTestBody, + streamState +} from '../protocol/structured-agent-session-wire-record.test-fixture' import { seedTestJournalQueueCard } from '../../../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import { mkdir, writeFile } from 'node:fs/promises' @@ -87,7 +92,7 @@ it('publishes the complete original and exact compact retry tuple through instal itemId: agentJournalSubmissionKey(id), revision: 0, body, - sequence: 4, + sequence: 3, observedAt: original.resolvedAt, turnScope: { kind: 'thread' } } @@ -95,7 +100,7 @@ it('publishes the complete original and exact compact retry tuple through instal itemId: agentJournalSubmissionKey(`start-failure:${id}`), revision: 1, body: { kind: 'status', text: reason, tone: 'error', failure: rejection }, - sequence: 5, + sequence: 4, observedAt: original.resolvedAt, recoveredAt: original.resolvedAt, recovered: true, @@ -110,9 +115,9 @@ it('publishes the complete original and exact compact retry tuple through instal reason, submittedAt: original.submittedAt, resolvedAt: null, - submittedSequence: 2, + submittedSequence: 1, handoverRecorded: true, - acceptedSequence: 2, + acceptedSequence: 1, origin: 'client', source: { kind: 'user' }, options: { model: 'default' }, @@ -148,11 +153,11 @@ it('publishes the complete original and exact compact retry tuple through instal removedItemIds: [], submissions: [expectedSubmission], window: { - oldest: { epoch, sequence: 4 }, - newest: { epoch, sequence: 5 }, - nextCursor: { epoch, sequence: 4 } + oldest: { epoch, sequence: 3 }, + newest: { epoch, sequence: 4 }, + nextCursor: { epoch, sequence: 3 } }, - liveCursor: { epoch, sequence: 5 }, + liveCursor: { epoch, sequence: 4 }, hasOlder: false, hasNewer: false, latestTurn: null, @@ -197,7 +202,7 @@ it('publishes the complete original and exact compact retry tuple through instal ...snapshotPage } = expected.page const emptyBatch = { - cursor: { epoch, sequence: 5 }, + cursor: { epoch, sequence: 4 }, items: [], removedItemIds: [], submissions: [] @@ -210,26 +215,28 @@ it('publishes the complete original and exact compact retry tuple through instal streaming: true }) const common = { sessionId: SESSION, fence: 4, hostNow } - expect(frames).toStrictEqual([ - response({ type: 'snapshot', ...common, page: snapshotPage, activity: null }), - response({ - type: 'batch', - ...common, - batch: emptyBatch, - queuedMessages, - queuePause, - nextQueuedMessageId - }), - response({ type: 'batch', ...common, batch: emptyBatch, backgroundTasks }), - response({ - type: 'batch', - ...common, - batch: emptyBatch, - latestTurn: null, - actionablePromptIds: [], - working: false - }) - ]) + await eventually(() => + expect(frames).toStrictEqual([ + response({ type: 'snapshot', ...common, page: snapshotPage, activity: null }), + response({ + type: 'batch', + ...common, + batch: emptyBatch, + queuedMessages, + queuePause, + nextQueuedMessageId + }), + response({ type: 'batch', ...common, batch: emptyBatch, backgroundTasks }), + response({ + type: 'batch', + ...common, + batch: emptyBatch, + latestTurn: null, + actionablePromptIds: [], + working: false + }) + ]) + ) }) it('keeps a saved physical hold independent while its own rejected retry blocks the ordered suffix', async () => { @@ -342,3 +349,139 @@ it('keeps a saved physical hold independent while its own rejected retry blocks expect(await state.reads.receiptFor(id)).not.toEqual(receipt) await published() }) + +it('keeps a queue edit after the old-reader page whose eligibility read it overlapped', async () => { + rig = await createQueuedMessageTestRig({ restartable: true }) + setStructuredAgentSessionHost(rig.host) + await rig.host.close(SESSION, 'evict') + rig.failNextStart(new Error('fixture start failed')) + const body = await queuedImageTestBody(rig.root) + const id = hostTestOperationId() + const fields = { body } + expect( + await call( + 'agentSession.send', + { + envelope: rig.envelope(fields, 'agentSession.send', id), + ...fields + }, + OLD + ) + ).toMatchObject({ result: { ok: true } }) + await eventually(async () => expect(await rig!.drafts()).toHaveLength(1)) + const j = rig.host.collaboratorsForTests().sessions.get(SESSION)!.journal + const card = (await j.queuedMessages.list()).find((row) => row.state === 'returned')! + const cursor = j.cursor() + const entered = Promise.withResolvers() + const resume = Promise.withResolvers() + const list = j.queuedMessages.list.bind(j.queuedMessages) + let editing: ReturnType | undefined + const readingQueue = vi + .spyOn(j.queuedMessages, 'list') + .mockImplementationOnce(async () => { + const rows = await list() + entered.resolve() + await resume.promise + return rows + }) + .mockImplementation(async () => { + await editing + return list() + }) + try { + const reading = historyPages.readAgentSessionHistory( + j, + { sessionId: SESSION, direction: 'tail' }, + { + reader: queuedImageHistoryReader(OLD) + } + ) + await entered.promise + editing = j.queuedMessages.update({ + messageId: card.messageId, + expectedBodyFingerprint: card.fingerprint, + text: 'overlapping edit' + }) + resume.resolve() + const result = await reading + await editing + expect(result.page.liveCursor).toEqual(cursor) + expect(result.page.items).toContainEqual( + expect.objectContaining({ itemId: agentJournalSubmissionKey(id), body }) + ) + expect(result.page.submissions.find((send) => send.clientMessageId === id)).not.toHaveProperty( + 'keptAsQueuedMessageId' + ) + const after = await historyPages.readAgentSessionHistory( + j, + { sessionId: SESSION, direction: 'tail' }, + { reader: queuedImageHistoryReader(OLD) } + ) + expect(after.page.liveCursor!.sequence).toBeGreaterThan(cursor.sequence) + expect(after.page.submissions.find((send) => send.clientMessageId === id)).toMatchObject({ + keptAsQueuedMessageId: card.messageId + }) + } finally { + resume.resolve() + readingQueue.mockRestore() + } +}) + +it('catches up a queue insertion between the old-reader initial page and its side controls', async () => { + rig = await createQueuedMessageTestRig({ restartable: true }) + setStructuredAgentSessionHost(rig.host) + await rig.workingSend() + const j = rig.host.collaboratorsForTests().sessions.get(SESSION)!.journal + const pageReady = Promise.withResolvers() + const resume = Promise.withResolvers() + const hydrate = historyPages.readAgentSessionHydrationPage + const reading = vi + .spyOn(historyPages, 'readAgentSessionHydrationPage') + .mockImplementationOnce(async (...args) => { + const page = await hydrate(...args) + pageReady.resolve() + await resume.promise + return page + }) + try { + const frames = await openStream('agentSession.subscribe', { sessionId: SESSION }, OLD) + await pageReady.promise + const body = await queuedImageTestBody(rig.root, 'queued after initial page') + const id = hostTestOperationId() + const fields = { body, delivery: 'queue-if-active' as const } + expect( + await call( + 'agentSession.send', + { + envelope: rig.envelope(fields, 'agentSession.send', id), + ...fields + }, + OLD + ) + ).toMatchObject({ result: { ok: true, value: { queued: { messageId: id } } } }) + expect(await j.reads.submission(id)).toBeUndefined() + resume.resolve() + await eventually(() => + expect(streamState(frames).items).toContainEqual( + expect.objectContaining({ itemId: agentJournalSubmissionKey(id), body }) + ) + ) + expect(frames).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + result: expect.objectContaining({ + type: 'batch', + batch: expect.objectContaining({ + items: expect.arrayContaining([ + expect.objectContaining({ itemId: agentJournalSubmissionKey(id), body }) + ]) + }) + }) + }) + ]) + ) + } finally { + resume.resolve() + reading.mockRestore() + } +}) From eb85bfb7b0b48cad9f7c7294d655d101dd3609b2 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 01:41:31 -0800 Subject: [PATCH 25/45] fix(native-chat): preserve catch-up authority and scoped journal teardown --- src/main/acp/acp-structured-steer.test.ts | 1 + ...claude-transcript-catch-up-test-support.ts | 1 + .../claude/claude-transcript-catch-up.test.ts | 3 + ...ex-structured-thread-catch-up-echo.spec.ts | 8 +- .../journal-domain-item-normalization.ts | 9 ++ .../journal-sql-steps.ts | 2 +- .../journal-host-database-test-support.ts | 7 +- ...uctured-agent-session-host.test-fixture.ts | 6 +- ...ent-session-journal-double.test-fixture.ts | 141 ------------------ ...ructured-rewind-completion.test-support.ts | 68 +++++++++ 10 files changed, 96 insertions(+), 150 deletions(-) delete mode 100644 src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-journal-double.test-fixture.ts create mode 100644 src/main/native-chat/agent-session-wire/rewind/structured-rewind-completion.test-support.ts diff --git a/src/main/acp/acp-structured-steer.test.ts b/src/main/acp/acp-structured-steer.test.ts index 87ebd16c06b..26e50b33d34 100644 --- a/src/main/acp/acp-structured-steer.test.ts +++ b/src/main/acp/acp-structured-steer.test.ts @@ -246,6 +246,7 @@ describe('permission changes with already admitted ACP input', () => { 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') // The restarted agent receives C as its own delivery of the queued card. const deliveredC = promptIdOf(promptC).replace(/^prompt:/, '') diff --git a/src/main/claude/claude-transcript-catch-up-test-support.ts b/src/main/claude/claude-transcript-catch-up-test-support.ts index e49004eb08c..a7bace03d3c 100644 --- a/src/main/claude/claude-transcript-catch-up-test-support.ts +++ b/src/main/claude/claude-transcript-catch-up-test-support.ts @@ -123,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 diff --git a/src/main/claude/claude-transcript-catch-up.test.ts b/src/main/claude/claude-transcript-catch-up.test.ts index ca039692640..098be86e2d4 100644 --- a/src/main/claude/claude-transcript-catch-up.test.ts +++ b/src/main/claude/claude-transcript-catch-up.test.ts @@ -18,6 +18,7 @@ import { } 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 { @@ -260,6 +261,8 @@ describe('Claude chat catch-up', () => { 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' }) } ) diff --git a/src/main/codex/structured/threads/codex-structured-thread-catch-up-echo.spec.ts b/src/main/codex/structured/threads/codex-structured-thread-catch-up-echo.spec.ts index 1fea2370704..77c61fb1512 100644 --- a/src/main/codex/structured/threads/codex-structured-thread-catch-up-echo.spec.ts +++ b/src/main/codex/structured/threads/codex-structured-thread-catch-up-echo.spec.ts @@ -29,6 +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 { completeTestJournalRewind } from '../../../native-chat/agent-session-wire/rewind/structured-rewind-completion.test-support' export function registerCodexCatchUpEchoTests(input: { codexWith: typeof catchUpCodexWith @@ -205,12 +206,7 @@ export function registerCodexCatchUpEchoTests(input: { expect( await structuredRewindCut(c.journal, { itemId: target, provider: 'codex', nativeId: THREAD }) ).not.toBeNull() - const items = await readTestJournalItems(c.journal) - const cutAt = items.find((item) => item.itemId === target)!.sequence - await c.replaceEpoch( - 'handle_forked', - items.filter((item) => item.sequence < cutAt) - ) + await completeTestJournalRewind(c.root, c.journal, target) await c.reopen() const prompt = await c.journal.reads.item(key) expect(prompt?.body).toEqual(body) diff --git a/src/main/native-chat/agent-session-journal/journal-domain-item-normalization.ts b/src/main/native-chat/agent-session-journal/journal-domain-item-normalization.ts index 61045b3e1db..6779e8d6b27 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-item-normalization.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-item-normalization.ts @@ -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. diff --git a/src/main/native-chat/agent-session-journal/journal-sql-steps.ts b/src/main/native-chat/agent-session-journal/journal-sql-steps.ts index f5e9603fb42..5dd3cc40458 100644 --- a/src/main/native-chat/agent-session-journal/journal-sql-steps.ts +++ b/src/main/native-chat/agent-session-journal/journal-sql-steps.ts @@ -112,7 +112,7 @@ function writeStep( if ( Atomics.load(resolution.permit, 0) !== 1 || Date.now() >= resolution.deadline || - cursor.epoch !== resolution.expectedCursor.epoch || + target.epoch !== resolution.expectedCursor.epoch || cursor.sequence !== resolution.expectedCursor.sequence ) { return { outcome: { kind: 'dropped' } } diff --git a/src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support.ts b/src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support.ts index 1ef1cbbe396..b9836b4a0c8 100644 --- a/src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support.ts +++ b/src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support.ts @@ -74,8 +74,10 @@ export function createTrackedJournalOpener( fixture: { legacyInputs?: boolean } = {} ): TrackedJournalOpener { const journals: AgentSessionJournal[] = [] + const directories = new Set() return { open: async ({ stateDirectory, ...options }) => { + directories.add(resolve(stateDirectory)) if (fixture.legacyInputs) { seedLegacyTestJournal(openTestJournalHostDatabase(stateDirectory), options) } @@ -93,7 +95,10 @@ export function createTrackedJournalOpener( }, closeAll: async () => { await Promise.allSettled(journals.splice(0).map((journal) => journal.close())) - await closeTestJournalHostDatabases() + for (const directory of directories) { + await closeTestJournalHostDatabase(directory) + } + directories.clear() } } } diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host.test-fixture.ts index 591f59e119a..9cdce36b286 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host.test-fixture.ts @@ -13,7 +13,10 @@ import { openTestAgentSessionRecordStore, openTestAgentSessionDatabase } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' -import { createTrackedJournalOpener } from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { + closeTestJournalHostDatabase, + createTrackedJournalOpener +} from '../../../agent-session-journal/store-lifecycle/journal-host-database-test-support' import type { AgentSessionDispatchOutcome, StructuredAgentSessionAdapter @@ -180,6 +183,7 @@ beforeEach(async () => { afterEach(async () => { await host.flushAllStreamedEvents() await journals.closeAll() + await closeTestJournalHostDatabase(root) // Replaced hosts may still hold recovery-capsule locks under root. await recoveryCapsule.settled() await rm(root, { recursive: true, force: true }) diff --git a/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-journal-double.test-fixture.ts b/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-journal-double.test-fixture.ts deleted file mode 100644 index bc448bc4b2a..00000000000 --- a/src/main/native-chat/agent-session-wire/journal-ingress/testing/structured-agent-session-journal-double.test-fixture.ts +++ /dev/null @@ -1,141 +0,0 @@ -// Run journal-double steps immediately, as on an idle write queue. - -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 type { JournalStep } from '../../../agent-session-journal/writing/journal-step-writer' -import type { JournalStepsResult } from '../../../agent-session-journal/writing/journal-writer-step' -import { journalSqlFailure } from '../../../agent-session-journal/journal-sql-failure' -import { resolveJournalTimelineItem } from '../../../agent-session-journal/writing/journal-timeline-resolution' -import { journalTimelineItemsView } from '../../../agent-session-journal/writing/journal-timeline-view' -import { resolveProviderTimelineSettlement } from '../../../agent-session-timeline/provider-timeline-writer-resolution' -import type { AgentSessionJournal } from '../../../agent-session-journal/journal-store' -import type { - JournalItemAppendOptions, - JournalLifecycleBatchInput -} from '../../../agent-session-journal/journal-store-contracts' - -type AppendingDouble = { - item?: AgentSessionJournal['item'] - visitItems?: AgentSessionJournal['visitItems'] - appendItem: ( - identity: AgentJournalItemIdentity, - body: AgentJournalItemBody, - options: JournalItemAppendOptions - ) => Promise - appendLifecycleBatch?: (input: JournalLifecycleBatchInput) => Promise -} - -export function withJournalQueueMembers( - journal: T -): T & { - readInOrder: (read: () => R) => Promise - appendResolvedItem: ( - resolve: () => { identity: AgentJournalItemIdentity; body: AgentJournalItemBody } | null, - options: JournalItemAppendOptions - ) => Promise - appendSteps: ( - steps: readonly JournalStep[], - beforeRun?: () => boolean - ) => Promise -} { - return Object.assign(journal, { - readInOrder: async (read: () => R): Promise => read(), - appendResolvedItem: async ( - resolve: () => { identity: AgentJournalItemIdentity; body: AgentJournalItemBody } | null, - options: JournalItemAppendOptions - ): Promise => { - const resolved = resolve() - // Resolve append at call time so later spy replacements take effect. - return resolved === null - ? null - : journal.appendItem(resolved.identity, resolved.body, options) - }, - appendSteps: async ( - steps: readonly JournalStep[], - beforeRun?: () => boolean - ): Promise => { - const result: JournalStepsResult = { outcomes: [] } - const item = async (itemId: string) => (await journal.reads.item(itemId)) ?? null - const view = journalTimelineItemsView(item, () => { - const items: AgentJournalRenderItem[] = [] - journal.visitItems?.((itemId) => { - const held = item(itemId) - if (held) { - items.push(held) - } - }) - return items - }) - if (beforeRun?.() === false) { - return { outcomes: steps.map(() => ({ kind: 'dropped' as const })) } - } - for (const step of steps) { - try { - if (step.kind === 'command') { - if (step.step.kind === 'resolved-item') { - const command = step.step - const resolved = resolveJournalTimelineItem( - view, - command.resolution, - command.options.turnScope, - command.reservedBytes - ) - if (resolved.kind === 'dropped') { - result.outcomes.push(resolved) - } else { - await journal.appendItem(resolved.item.identity, resolved.item.body, { - ...command.options, - turnScope: resolved.item.turnScope ?? command.options.turnScope - }) - result.outcomes.push({ kind: 'committed', wrote: true }) - } - continue - } - if ( - step.step.kind === 'settlement' && - step.step.resolution.kind !== 'history-catch-up' - ) { - const { settlementId, fence, ownerFence, recovered } = step.step - const mutations = resolveProviderTimelineSettlement(view, step.step.resolution) - if (mutations.length > 0) { - await journal.appendLifecycleBatch?.({ - settlementId, - fence, - ...(ownerFence !== undefined ? { ownerFence } : {}), - ...(recovered ? { recovered } : {}), - mutations - }) - } - result.outcomes.push({ kind: 'committed', wrote: mutations.length > 0 }) - continue - } - throw new Error('journal command steps require a real journal') - } - if (step.kind === 'item') { - const resolved = step.resolve() - if (resolved !== null) { - await journal.appendItem(resolved.identity, resolved.body, step.options) - } - result.outcomes.push( - resolved === null ? { kind: 'dropped' } : { kind: 'committed', wrote: true } - ) - continue - } - const { resolve, ...batch } = step.batch - const mutations = resolve() - if (mutations.length > 0) { - await journal.appendLifecycleBatch?.({ ...batch, mutations }) - } - result.outcomes.push({ kind: 'committed', wrote: mutations.length > 0 }) - } catch (error) { - result.failure = journalSqlFailure(error) - break - } - } - return result - } - }) -} diff --git a/src/main/native-chat/agent-session-wire/rewind/structured-rewind-completion.test-support.ts b/src/main/native-chat/agent-session-wire/rewind/structured-rewind-completion.test-support.ts new file mode 100644 index 00000000000..ae00ccf6558 --- /dev/null +++ b/src/main/native-chat/agent-session-wire/rewind/structured-rewind-completion.test-support.ts @@ -0,0 +1,68 @@ +import type { AgentSessionJournal } from '../../agent-session-journal/journal-store' +import { agentSessionRecordFixture } from '../../agent-session-record-test-fixture' +import { + openTestAgentSessionRecordStore, + seedTestAgentSessionRecordStore +} from '../../../runtime/agent-session/records/agent-session-record-store-test-harness' +import { testEventSinkLogging } from '../host/testing/structured-agent-session-logger.test-fixture' +import { recoverStructuredRewind } from './structured-rewind-recovery' + +/** Complete the saved provider mutation through the real atomic journal/record receipt. */ +export async function completeTestJournalRewind( + root: string, + journal: AgentSessionJournal, + itemId: string +): Promise { + const { sessionId, agent: provider, providerHandle } = journal.identity + if (!providerHandle || (provider !== 'claude' && provider !== 'codex')) { + throw new Error('rewind fixture requires a native provider handle') + } + await seedTestAgentSessionRecordStore(root, { + records: [ + agentSessionRecordFixture({ + sessionId, + provider, + providerHandleChain: [ + { + linkId: 'head', + handle: providerHandle, + origin: 'adopted', + mintedAtFence: 1, + observedAt: 1 + } + ] + }) + ] + }) + const store = await openTestAgentSessionRecordStore(root) + const operationId = '1700000000000-00000000000000000000000000000001' + const callerKey = 'rewind-fixture' + await store.admitOperation({ callerKey, operationId, fingerprint: 'rewind', now: 1_000 }) + await store.updateRecord(sessionId, { + kind: 'rewind', + fence: 1, + rewind: { + operationId, + callerKey, + itemId, + expectedEpoch: journal.cursor().epoch, + phase: 'provider-succeeded', + ...(provider === 'claude' + ? { + providerFork: { + sessionId: 'fixture-fork', + sourceSessionId: providerHandle.nativeId, + replacesSessionId: providerHandle.nativeId, + materialized: true + } + } + : {}) + } + }) + await recoverStructuredRewind( + { store, logger: testEventSinkLogging().logger }, + sessionId, + journal, + 1 + ) +} From 492f473d4c4f298bbaee879ee27ecb82de6d90b8 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 01:52:57 -0800 Subject: [PATCH 26/45] fix(native-chat): preserve stopped result disclosure and async fixture ownership --- src/main/claude/claude-result-journaling.ts | 4 +- ...laude-structured-provider-fallback.test.ts | 66 +++++++++++++++++++ .../claude-structured-provider-fallback.ts | 3 +- ...ueued-message-rig-provider.test-fixture.ts | 9 +++ ...session-queued-message-rig.test-fixture.ts | 2 + ...ructured-agent-session-queued-move.test.ts | 12 ++-- ...ured-agent-session-stale-turn-open.test.ts | 10 +-- 7 files changed, 94 insertions(+), 12 deletions(-) diff --git a/src/main/claude/claude-result-journaling.ts b/src/main/claude/claude-result-journaling.ts index 58a4fcfcd43..290e9286b63 100644 --- a/src/main/claude/claude-result-journaling.ts +++ b/src/main/claude/claude-result-journaling.ts @@ -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 diff --git a/src/main/claude/structured/launch/claude-structured-provider-fallback.test.ts b/src/main/claude/structured/launch/claude-structured-provider-fallback.test.ts index 3b61f5b6495..62bc94a0d38 100644 --- a/src/main/claude/structured/launch/claude-structured-provider-fallback.test.ts +++ b/src/main/claude/structured/launch/claude-structured-provider-fallback.test.ts @@ -158,6 +158,72 @@ describe('Claude provider fallback', () => { 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 { diff --git a/src/main/claude/structured/launch/claude-structured-provider-fallback.ts b/src/main/claude/structured/launch/claude-structured-provider-fallback.ts index 1729e07dfb1..6d494122310 100644 --- a/src/main/claude/structured/launch/claude-structured-provider-fallback.ts +++ b/src/main/claude/structured/launch/claude-structured-provider-fallback.ts @@ -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 } diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig-provider.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig-provider.test-fixture.ts index d939fc425d4..899b68c633c 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig-provider.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig-provider.test-fixture.ts @@ -110,6 +110,15 @@ export function createQueuedRigProvider( } }, dispatch, + captureMessageSteeringInputs: () => ({ + kind: 'codex', + compiler: { + active: reported(), + defaults: reported(), + models: [], + listedServiceTiers: [] + } + }), closeSession, releaseAcquisition: vi.fn(async () => true), compact, diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig.test-fixture.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig.test-fixture.ts index bd53b16d0c3..da3481cf42f 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig.test-fixture.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig.test-fixture.ts @@ -166,10 +166,12 @@ export async function createQueuedMessageTestRig( } async function submission(id: string): Promise { + await host.history({ sessionId: SESSION, direction: 'tail' }) return host.collaboratorsForTests().sessions.get(SESSION)!.journal.reads.submission(id) } async function handoff(draftId: string): Promise { + await host.history({ sessionId: SESSION, direction: 'tail' }) const journal = host.collaboratorsForTests().sessions.get(SESSION)!.journal return (await journal.reads.input({ kind: 'linked-to', messageId: draftId })).submissions.at(-1) } diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-move.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-move.test.ts index e8c3added53..5a8366325cf 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-move.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-move.test.ts @@ -70,7 +70,7 @@ describe('authoritative queue ordering', () => { ok: true, value: { status: 'moved' } }) - expect(order()).toEqual([c, a, mail, b]) + expect(await order()).toEqual([c, a, mail, b]) for (const card of facts) { expect(await journal().queuedMessages.get(card.messageId)).toEqual({ ...card, @@ -85,7 +85,7 @@ describe('authoritative queue ordering', () => { ok: true, value: { status: 'moved' } }) - expect(order()).toEqual([a, mail, b, c]) + expect(await order()).toEqual([a, mail, b, c]) expect(await move(mail, { beforeMessageId: a })).toMatchObject({ ok: true, value: { status: 'moved' } @@ -103,7 +103,7 @@ describe('authoritative queue ordering', () => { ok: true, value: { status: 'moved' } }) - expect(order()).toEqual([mail, a, c, b]) + expect(await order()).toEqual([mail, a, c, b]) await rig.host.close(HOST_TEST_SESSION, 'evict') await rig.restartHostProcess() const reopened = await rig.host.history({ sessionId: HOST_TEST_SESSION, direction: 'tail' }) @@ -117,7 +117,7 @@ describe('authoritative queue ordering', () => { ok: true, value: { status: 'moved' } }) - expect(order()).toEqual([mail, a, c, b]) + expect(await order()).toEqual([mail, a, c, b]) expect((await journal().queuedMessages.get(mail))?.body).toEqual(facts[1]!.body) expect(await journal().queuedMessages.list()).toHaveLength(4) expect(await rig.deleteQueued(c)).toMatchObject({ ok: true }) @@ -138,7 +138,7 @@ describe('authoritative queue ordering', () => { await move(held, { beforeMessageId: a }) const op = hostTestOperationId() await move(mail, { beforeMessageId: a }, op) - expect(order()).toEqual([held, mail, a, b]) + expect(await order()).toEqual([held, mail, a, b]) for (const card of facts) { expect(await journal().queuedMessages.get(card.messageId)).toEqual({ ...card, @@ -173,7 +173,7 @@ describe('authoritative queue ordering', () => { a, b ]) - expect(order()).toEqual([held]) + expect(await order()).toEqual([held]) expect(await journal().queuedMessages.get(held)).toEqual({ ...facts.find((card) => card.messageId === held)!, position: facts[0]!.position diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stale-turn-open.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stale-turn-open.test.ts index a9d712373b7..3f241c84471 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stale-turn-open.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stale-turn-open.test.ts @@ -52,8 +52,8 @@ async function exitWithUnwrittenSettlement( throw new Error('disk full') } } - const settleDead = open.journal.settleDeadGeneration - const settleStale = open.journal.settleStaleState + const settleDead = open.journal.settleDeadGeneration.bind(open.journal) + const settleStale = open.journal.settleStaleState.bind(open.journal) vi.spyOn(open.journal, 'settleDeadGeneration').mockImplementation(async (input) => { refuse() return settleDead(input) @@ -112,8 +112,10 @@ describe('a turn its gone agent left running', () => { expect(await workingTurnState()).toBe('interrupted') // Internal death diagnostics must not reach transcript readers. - expect(JSON.stringify(reader.events)).toContain( - 'Codex stopped while this response was in progress. You can continue in this conversation.' + await vi.waitFor(() => + expect(JSON.stringify(reader.events)).toContain( + 'Codex stopped while this response was in progress. You can continue in this conversation.' + ) ) expect(rig.adapter.acquire).toHaveBeenCalledOnce() }) From e897b41b69eaa671690f068904651df2fc07477d Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:02:45 -0800 Subject: [PATCH 27/45] Resolve lost-session warning identity at the journal writer --- .../acp/acp-session-reopen-failure.test.ts | 4 +- src/main/acp/acp-session-reopen-failure.ts | 61 +------ src/main/acp/acp-structured-handshake.ts | 2 +- ...acp-structured-host-restore-failed.test.ts | 34 +++- .../acp/acp-structured-host.test-support.ts | 2 +- .../acp-structured-launch-resolution.test.ts | 18 +- .../acp/acp-structured-launch-resolution.ts | 14 +- .../acp-structured-managed-runtime.test.ts | 2 +- src/main/acp/acp-structured-resume-history.ts | 2 - ...-structured-session-adapter-resume.test.ts | 6 +- .../journal-domain-schema-001-indexes.ts | 2 + .../database/journal-domain-schema-001.ts | 1 + .../journal-domain-item-read-columns.ts | 7 +- .../journal-domain-loss-warnings.test.ts | 169 ++++++++++++++++++ .../journal-domain-timeline-reads.ts | 7 + .../writing/journal-timeline-resolution.ts | 22 ++- .../writing/journal-timeline-view.ts | 19 ++ .../provider-timeline-event.ts | 9 +- .../provider-timeline-item-decisions.ts | 3 +- .../structured-agent-runtime-registrations.ts | 4 +- ...red-agent-launch-support-probe-omp.test.ts | 2 +- ...uctured-agent-launch-support-probe.test.ts | 2 +- 22 files changed, 287 insertions(+), 105 deletions(-) create mode 100644 src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts diff --git a/src/main/acp/acp-session-reopen-failure.test.ts b/src/main/acp/acp-session-reopen-failure.test.ts index 2c441d5d7a1..5989dc528e3 100644 --- a/src/main/acp/acp-session-reopen-failure.test.ts +++ b/src/main/acp/acp-session-reopen-failure.test.ts @@ -22,7 +22,7 @@ it('propagates a temporary load envelope before takeover or a dead-child asserti key: 'saved-key', sessionId: 'saved-id', mayBeUnsaved: async () => false, - unannouncedLosses: async () => [] + lossKeys: [] } }, script: (agent) => @@ -53,7 +53,7 @@ it.each([ key: 'saved-key', sessionId: 'saved-id', mayBeUnsaved: async () => false, - unannouncedLosses: async () => [] + lossKeys: [] } }, deps: { diff --git a/src/main/acp/acp-session-reopen-failure.ts b/src/main/acp/acp-session-reopen-failure.ts index 75bda3c797d..ee9a75d8ebe 100644 --- a/src/main/acp/acp-session-reopen-failure.ts +++ b/src/main/acp/acp-session-reopen-failure.ts @@ -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 { - // 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 - 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 - }) - ) - ) -} diff --git a/src/main/acp/acp-structured-handshake.ts b/src/main/acp/acp-structured-handshake.ts index aa7d6f7bfdd..f2337d31a7f 100644 --- a/src/main/acp/acp-structured-handshake.ts +++ b/src/main/acp/acp-structured-handshake.ts @@ -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) { diff --git a/src/main/acp/acp-structured-host-restore-failed.test.ts b/src/main/acp/acp-structured-host-restore-failed.test.ts index b1622cd3033..a006f3834d0 100644 --- a/src/main/acp/acp-structured-host-restore-failed.test.ts +++ b/src/main/acp/acp-structured-host-restore-failed.test.ts @@ -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 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, { @@ -83,9 +85,7 @@ async function openRestoreRig(code: number, message: string) { store, savedSession: { hasProviderTurn: (sessionId, nativeId) => - journalSqlReads(journalDatabase.readers, sessionId).hasProviderTurn(nativeId), - savedItemIds: async (sessionId, itemIds) => - new Set(await journalSqlReads(journalDatabase.readers, sessionId).savedItemIds(itemIds)) + journalSqlReads(journalDatabase.readers, sessionId).hasProviderTurn(nativeId) }, resolveWorkspacePath: async () => '/workspace', resolveEnvironment: async () => ({ PATH: '/usr/bin' }), @@ -224,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') @@ -241,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') 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(await warnings()).toEqual([firstWarning]) + 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 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') }) }) diff --git a/src/main/acp/acp-structured-host.test-support.ts b/src/main/acp/acp-structured-host.test-support.ts index 6415d278f82..58ce5a7bc91 100644 --- a/src/main/acp/acp-structured-host.test-support.ts +++ b/src/main/acp/acp-structured-host.test-support.ts @@ -76,7 +76,7 @@ export function launch(resume: () => boolean, spec = GROK): () => Promise false, - unannouncedLosses: async () => [] + lossKeys: [] } : null }) diff --git a/src/main/acp/acp-structured-launch-resolution.test.ts b/src/main/acp/acp-structured-launch-resolution.test.ts index 395bbe65a98..1ab9229e429 100644 --- a/src/main/acp/acp-structured-launch-resolution.test.ts +++ b/src/main/acp/acp-structured-launch-resolution.test.ts @@ -59,8 +59,7 @@ function ompRecord(options: Record, resumes: boolean): AgentSess } const NOTHING_SAVED: AcpSavedSessionReads = { - hasProviderTurn: async () => false, - savedItemIds: async () => new Set() + hasProviderTurn: async () => false } function resolver(record: AgentSessionRecord, savedSession = NOTHING_SAVED, spec = GROK) { @@ -82,7 +81,7 @@ function resolver(record: AgentSessionRecord, savedSession = NOTHING_SAVED, spec } describe('ACP launch resolution', () => { - it('resumes a chat with a saved exchange and warns of an unannounced loss, unless unreadable', 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 journals = createTrackedJournalOpener() try { @@ -108,15 +107,13 @@ describe('ACP launch resolution', () => { const { readers } = await openTestAgentSessionDatabase(root) const savedSession: AcpSavedSessionReads = { hasProviderTurn: (sessionId, nativeId) => - journalSqlReads(readers, sessionId).hasProviderTurn(nativeId), - savedItemIds: async (sessionId, itemIds) => - new Set(await journalSqlReads(readers, sessionId).savedItemIds(itemIds)) + journalSqlReads(readers, sessionId).hasProviderTurn(nativeId) } const readDecisions = async (saved: AcpSavedSessionReads) => { const launch = await resolver(record, saved).resolve({ identity }) return { unsaved: await launch.resume?.mayBeUnsaved(), - losses: await launch.resume?.unannouncedLosses() + losses: launch.resume?.lossKeys } } // Nothing saved on acp-1 yet, so the created session may hold an unsaved exchange. @@ -137,12 +134,11 @@ describe('ACP launch resolution', () => { ) // 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 proves no exchange and suppresses new warnings. + // An unreadable chat cannot prove no exchange; its writer still owns warning admission. const unreadable: AcpSavedSessionReads = { - hasProviderTurn: () => Promise.reject(new Error('journal unreadable')), - savedItemIds: () => Promise.reject(new Error('journal unreadable')) + hasProviderTurn: () => Promise.reject(new Error('journal unreadable')) } - expect(await readDecisions(unreadable)).toEqual({ unsaved: false, losses: [] }) + expect(await readDecisions(unreadable)).toEqual({ unsaved: false, losses: [lostKey] }) } finally { await journals.closeAll() rmSync(root, { recursive: true, force: true }) diff --git a/src/main/acp/acp-structured-launch-resolution.ts b/src/main/acp/acp-structured-launch-resolution.ts index df02eb17a83..f1af1e421da 100644 --- a/src/main/acp/acp-structured-launch-resolution.ts +++ b/src/main/acp/acp-structured-launch-resolution.ts @@ -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 - /** Lost conversations whose warning rows are missing; read after startup succeeds. */ - unannouncedLosses: () => Promise + /** 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 }, diff --git a/src/main/acp/acp-structured-managed-runtime.test.ts b/src/main/acp/acp-structured-managed-runtime.test.ts index 574eb9e9644..4c5ccfd7aac 100644 --- a/src/main/acp/acp-structured-managed-runtime.test.ts +++ b/src/main/acp/acp-structured-managed-runtime.test.ts @@ -45,7 +45,7 @@ it('prefers the pin even when older, else a complete installed or external copy' }), pinLaunchDirectory: vi.fn() }, - savedSession: { hasProviderTurn: async () => false, savedItemIds: async () => new Set() }, + savedSession: { hasProviderTurn: async () => false }, resolveEnvironment: async () => ({ PATH: '', HOME: homePath }), dataDirectory }) diff --git a/src/main/acp/acp-structured-resume-history.ts b/src/main/acp/acp-structured-resume-history.ts index d235858ebdd..1097798aaf1 100644 --- a/src/main/acp/acp-structured-resume-history.ts +++ b/src/main/acp/acp-structured-resume-history.ts @@ -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 - /** The subset of these item IDs the chat has saved. */ - savedItemIds(sessionId: string, itemIds: readonly string[]): Promise> } /** Unreadable journals cannot prove no exchange. */ diff --git a/src/main/acp/acp-structured-session-adapter-resume.test.ts b/src/main/acp/acp-structured-session-adapter-resume.test.ts index 0e4a4822813..d26a7ceb001 100644 --- a/src/main/acp/acp-structured-session-adapter-resume.test.ts +++ b/src/main/acp/acp-structured-session-adapter-resume.test.ts @@ -37,7 +37,7 @@ const resume = { sessionId: PROVIDER_SESSION, key: 'acp:grok:saved', mayBeUnsaved: async () => false, - unannouncedLosses: async () => [] + lossKeys: [] } } } @@ -248,7 +248,7 @@ describe('reattaching a Grok chat the journal holds', () => { sessionId: 'saved-1', key: 'acp-key-saved-1', mayBeUnsaved: async () => mayBeUnsaved, - unannouncedLosses: async () => [] + lossKeys: [] } }, initialize: RESUMES, @@ -280,7 +280,7 @@ describe('reattaching a Grok chat the journal holds', () => { sessionId: 'saved-1', key: 'acp-key-saved-1', mayBeUnsaved: async () => false, - unannouncedLosses: async () => [] + lossKeys: [] } }, initialize: RESUMES, diff --git a/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001-indexes.ts b/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001-indexes.ts index 8b890896ea1..8364f324aa6 100644 --- a/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001-indexes.ts +++ b/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001-indexes.ts @@ -72,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) diff --git a/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001.ts b/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001.ts index de968cac712..7d3a839c51c 100644 --- a/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001.ts +++ b/src/main/native-chat/agent-session-journal/database/journal-domain-schema-001.ts @@ -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, diff --git a/src/main/native-chat/agent-session-journal/journal-domain-item-read-columns.ts b/src/main/native-chat/agent-session-journal/journal-domain-item-read-columns.ts index 83d8ba88d76..2d7730df6fb 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-item-read-columns.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-item-read-columns.ts @@ -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 ] } } diff --git a/src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts b/src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts new file mode 100644 index 00000000000..f468dfbce94 --- /dev/null +++ b/src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts @@ -0,0 +1,169 @@ +import { afterEach, expect, it } from 'vitest' +import { agentJournalItemKey } from '../../../shared/agent-session/journal/agent-session-journal-item-key' +import { createLegacyProviderTimelineIdentityScheme } from '../agent-session-timeline/provider-timeline-identity' +import { acpSessionNotRestoredItem } from '../../acp/acp-session-reopen-failure' +import { + domainLifecycleFixture, + lifecycleOptions as options +} from './journal-domain-lifecycle.test-fixture' +import type { JournalWriterStep } from './writing/journal-writer-step' +import type { JournalDomainImportItem } from './journal-domain-import' +import { insertJournalDomainImportPage } from './journal-domain-import-rows' +import { runJournalTransaction } from './database/journal-database' + +const cleanups: (() => void)[] = [] +afterEach(() => { + for (const cleanup of cleanups.splice(0)) { + cleanup() + } +}) + +function warningIdentity(namespace: string, lostKey: string, agent = 'grok') { + return createLegacyProviderTimelineIdentityScheme({ agent, sessionId: 'chat' }).item({ + namespace, + family: 'item', + key: { source: 'provider', value: acpSessionNotRestoredItem(lostKey) }, + thread: namespace + }) +} + +const warning = (namespace: string, lostKey: string): JournalWriterStep => ({ + kind: 'resolved-item', + options, + reservedBytes: 4096, + resolution: { + kind: 'acp-loss-warning', + identity: warningIdentity(namespace, lostKey), + body: { kind: 'status', tone: 'warning', text: 'The earlier session could not be reopened.' } + } +}) + +it('uses surviving source-order writes for duplicate warnings and retries a rolled-back warning', async () => { + const f = domainLifecycleFixture(cleanups) + f.db.exec(`CREATE TRIGGER refuse_loss BEFORE INSERT ON journal_items + WHEN NEW.legacy_provider_item_key='session-not-restored%3Asecond' + BEGIN SELECT RAISE(ABORT,'loss refused'); END`) + const first = await f.run({ + kind: 'steps', + steps: [warning('a', 'first'), warning('b', 'first'), warning('b', 'second')] + }) + expect(first.value).toMatchObject({ + outcomes: [{ kind: 'committed', wrote: true }, { kind: 'dropped' }], + failure: { message: 'loss refused' }, + cursor: { epoch: 'epoch', sequence: 1 } + }) + const originalId = agentJournalItemKey(warningIdentity('a', 'first')) + const original = f.item(originalId) + expect(first.publication?.items.map((item) => item.itemId)).toEqual([originalId]) + f.db.exec('DROP TRIGGER refuse_loss') + f.reopen() + const retry = await f.run({ + kind: 'steps', + steps: [warning('c', 'first'), warning('c', 'second'), warning('d', 'second')] + }) + expect(retry.value).toMatchObject({ + outcomes: [{ kind: 'dropped' }, { kind: 'committed', wrote: true }, { kind: 'dropped' }], + cursor: { epoch: 'epoch', sequence: 2 } + }) + expect(f.item(originalId)).toEqual(original) + expect(retry.publication?.items).toHaveLength(1) +}) + +it('preserves imported semantic duplicates across writers and body kinds until their visible rows are removed', async () => { + const f = domainLifecycleFixture(cleanups) + f.db.exec('DELETE FROM journal_chat_state') + const firstIdentity = warningIdentity('old/writer', 'escaped:/% ☃', 'omp') + const secondIdentity = warningIdentity('another:writer', 'escaped:/% ☃', 'grok') + const longKey = 'long-key'.repeat(100) + const imported = [firstIdentity, secondIdentity, warningIdentity('old', longKey)].map( + (identity, index): JournalDomainImportItem => ({ + ordinal: index + 1, + ownerFence: 1, + executionOwner: null, + hidden: false, + item: { + itemId: agentJournalItemKey(identity), + revision: 3, + sequence: index + 1, + observedAt: 12, + body: { kind: 'message', role: 'assistant', blocks: [{ type: 'text', text: 'saved' }] }, + turnScope: { kind: 'thread' } + }, + turnItemId: null, + originalStopNoteJson: null, + queueCommandTurn: false, + queueFailedOutcome: false, + queueFailedAt: 0, + queueFailureWitnessedThrough: 0, + queueFailureReportTurnItemId: null + }) + ) + runJournalTransaction(f.db, () => + insertJournalDomainImportPage( + f.db, + { sessionId: 'chat', epoch: 'epoch' }, + { + kind: 'items', + rows: imported + } + ) + ) + f.db + .exec(`INSERT INTO journal_chat_state(session_id,workspace_id,epoch,last_sequence,highest_fence) + VALUES('chat','workspace','epoch',3,1)`) + const held = imported.map(({ item }) => item) + expect( + ( + await f.run({ + kind: 'steps', + steps: [warning('new', 'escaped:/% ☃'), warning('new', longKey)] + }) + ).value + ).toMatchObject({ outcomes: [{ kind: 'dropped' }, { kind: 'dropped' }] }) + expect(held.map((item) => f.item(item.itemId))).toEqual(held) + await f.run({ kind: 'tombstone', identity: firstIdentity, fence: 1 }) + expect( + (await f.run({ kind: 'steps', steps: [warning('new', 'escaped:/% ☃')] })).value + ).toMatchObject({ outcomes: [{ kind: 'dropped' }] }) + await f.run({ kind: 'tombstone', identity: secondIdentity, fence: 1 }) + expect( + (await f.run({ kind: 'steps', steps: [warning('new', 'escaped:/% ☃')] })).value + ).toMatchObject({ outcomes: [{ kind: 'committed', wrote: true }] }) +}) + +it('does not suppress a missing warning for hidden rows, another physical epoch or an alias alone', async () => { + const f = domainLifecycleFixture(cleanups) + const hidden = warningIdentity('old', 'hidden') + const retired = warningIdentity('old', 'retired') + for (const identity of [hidden, retired]) { + await f.run({ kind: 'item', identity, body: { kind: 'status', text: 'saved' }, options }) + } + f.db.prepare('UPDATE journal_items SET hidden=1 WHERE item_id=?').run(agentJournalItemKey(hidden)) + f.db + .prepare("UPDATE journal_items SET epoch='retired' WHERE item_id=?") + .run(agentJournalItemKey(retired)) + await f.run({ + kind: 'item', + identity: { provider: 'orca', clientMessageId: 'alias-target' }, + body: { kind: 'status', text: 'saved' }, + options + }) + f.db + .prepare(`INSERT INTO journal_item_aliases(session_id,epoch,provider_item_id,item_id,echo_declared) + VALUES('chat','epoch',?,'orca:alias-target',0)`) + .run(agentJournalItemKey(warningIdentity('new', 'alias'))) + expect( + ( + await f.run({ + kind: 'steps', + steps: [warning('new', 'hidden'), warning('new', 'retired'), warning('new', 'alias')] + }) + ).value + ).toMatchObject({ + outcomes: [ + { kind: 'committed', wrote: true }, + { kind: 'committed', wrote: true }, + { kind: 'committed', wrote: true } + ] + }) +}) diff --git a/src/main/native-chat/agent-session-journal/journal-domain-timeline-reads.ts b/src/main/native-chat/agent-session-journal/journal-domain-timeline-reads.ts index 1427796197e..6f439998ea8 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-timeline-reads.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-timeline-reads.ts @@ -72,6 +72,13 @@ export function journalDomainTimelineReads( return { item, itemBody: (key) => item(key)?.body ?? null, + hasVisibleProviderItemKey: (key) => + Boolean( + db + .prepare(`SELECT 1 FROM journal_items WHERE session_id=? AND epoch=? AND hidden=0 + AND legacy_provider_item_key IS NOT NULL AND legacy_provider_item_key=? LIMIT 1`) + .get(...prefix, key) + ), newestTurn: () => decode( db diff --git a/src/main/native-chat/agent-session-journal/writing/journal-timeline-resolution.ts b/src/main/native-chat/agent-session-journal/writing/journal-timeline-resolution.ts index b1bf91d7c6e..1fb43ee987c 100644 --- a/src/main/native-chat/agent-session-journal/writing/journal-timeline-resolution.ts +++ b/src/main/native-chat/agent-session-journal/writing/journal-timeline-resolution.ts @@ -1,5 +1,9 @@ import { isDeepStrictEqual } from 'node:util' -import { agentJournalItemKey } from '../../../../shared/agent-session/journal/agent-session-journal-item-key' +import { + agentJournalItemKey, + parseAgentJournalItemKey +} from '../../../../shared/agent-session/journal/agent-session-journal-item-key' +import { spelledProviderTimelineItemKey } from '../../../../shared/provider-timeline-item-key' import type { AgentJournalItemIdentity } from '../../../../shared/agent-session/journal/agent-session-journal-identity' import { AGENT_JOURNAL_THREAD_SCOPE, @@ -51,6 +55,7 @@ type TimelineItemResolution = change: 'open' | 'update' | 'close' } | { kind: 'literal'; identity: AgentJournalItemIdentity; body: AgentJournalItemBody } + | { kind: 'acp-loss-warning'; identity: AgentJournalItemIdentity; body: AgentJournalItemBody } | { kind: 'request-open' request: ProviderTimelineRequest @@ -140,6 +145,21 @@ function resolveTimelineItem( switch (resolution.kind) { case 'literal': return resolved({ identity: resolution.identity, body: resolution.body }) + case 'acp-loss-warning': { + const identity = parseAgentJournalItemKey(agentJournalItemKey(resolution.identity)) + const key = + identity?.provider === 'legacy' ? spelledProviderTimelineItemKey(identity.recordId) : null + if (key === null) { + throw new Error('journal_acp_loss_warning_requires_provider_item_key') + } + return view.hasVisibleProviderItemKey(key) + ? nothing + : resolveJournalGuardedItem( + view, + { ...resolution, kind: 'guarded-item', change: 'update' }, + placement + ) + } case 'request-open': return resolveProviderTimelineRequestOpen(view, { ...resolution, diff --git a/src/main/native-chat/agent-session-journal/writing/journal-timeline-view.ts b/src/main/native-chat/agent-session-journal/writing/journal-timeline-view.ts index 80d3065972f..39954244658 100644 --- a/src/main/native-chat/agent-session-journal/writing/journal-timeline-view.ts +++ b/src/main/native-chat/agent-session-journal/writing/journal-timeline-view.ts @@ -8,11 +8,14 @@ import { readJournalInputItem } from '../input-order/journal-input-obligations' import { journalItemLifecycleState } from '../journal-item-status-metadata' import { lostLiveWorkJournalBody } from '../item-lifecycle/journal-subagent-liveness' import { personStopDecidesTurn } from '../context-epochs/journal-stop-turn-end' +import { parseAgentJournalItemKey } from '../../../../shared/agent-session/journal/agent-session-journal-item-key' +import { spelledProviderTimelineItemKey } from '../../../../shared/provider-timeline-item-key' /** Named selections a timeline resolution reads at its write position; never the whole chat. */ export type JournalTimelineView = { item(itemId: string): AgentJournalRenderItem | null itemBody(itemId: string): AgentJournalItemBody | null + hasVisibleProviderItemKey(key: string): boolean /** The most recently written turn row. */ newestTurn(): AgentJournalRenderItem | null newestWithKeyPrefix(prefix: string): AgentJournalRenderItem | null @@ -35,6 +38,8 @@ export function journalTimelineView(state: JournalReducerState): JournalTimeline return { item, itemBody: (itemId) => item(itemId)?.body ?? null, + hasVisibleProviderItemKey: (key) => + hasProviderItemKey(state.queries.itemsWithKeyPrefix(state, 'legacy:'), key), newestTurn: () => state.queries.newestTurnItem(state), newestWithKeyPrefix: (prefix) => state.queries.newestItemWithKeyPrefix(state, prefix), withKeyPrefix: (prefix) => state.queries.itemsWithKeyPrefix(state, prefix), @@ -53,6 +58,7 @@ export function journalTimelineItemsView( return { item, itemBody: (itemId) => item(itemId)?.body ?? null, + hasVisibleProviderItemKey: (key) => hasProviderItemKey(items(), key), newestTurn: () => newestJournalTimelineTurn(items()), withKeyPrefix: function* (prefix) { for (const item of items()) { @@ -76,6 +82,19 @@ export function journalTimelineItemsView( } } +function hasProviderItemKey(items: Iterable, key: string): boolean { + for (const item of items) { + const identity = parseAgentJournalItemKey(item.itemId) + if ( + identity?.provider === 'legacy' && + spelledProviderTimelineItemKey(identity.recordId) === key + ) { + return true + } + } + return false +} + export function newestJournalTimelineTurn( items: Iterable ): AgentJournalRenderItem | null { diff --git a/src/main/native-chat/agent-session-timeline/provider-timeline-event.ts b/src/main/native-chat/agent-session-timeline/provider-timeline-event.ts index f3879c4ebb8..6e4fc268f33 100644 --- a/src/main/native-chat/agent-session-timeline/provider-timeline-event.ts +++ b/src/main/native-chat/agent-session-timeline/provider-timeline-event.ts @@ -66,7 +66,14 @@ export type ProviderTimelineEvent = /** Background-task messages outlive their initiating call/turn and become unverifiable on session end. */ | ({ type: 'item.open'; item: string; body: ProviderTimelineItemBody } & Produced & Joined) /** Full snapshot; settled tools retain terminal bodies. */ - | ({ type: 'item.update'; item: string; body: ProviderTimelineItemBody } & Produced & Joined) + | ({ + type: 'item.update' + item: string + body: ProviderTimelineItemBody + /** ACP startup retries a lost-session warning by visible semantic identity. */ + lossWarning?: true + } & Produced & + Joined) /** Terminal snapshot replaces text streamed under the same item ID. */ | ({ type: 'item.close'; item: string; body: ProviderTimelineItemBody } & Produced & Joined) | ({ diff --git a/src/main/native-chat/agent-session-timeline/provider-timeline-item-decisions.ts b/src/main/native-chat/agent-session-timeline/provider-timeline-item-decisions.ts index 8490f8f0981..d0710b25acb 100644 --- a/src/main/native-chat/agent-session-timeline/provider-timeline-item-decisions.ts +++ b/src/main/native-chat/agent-session-timeline/provider-timeline-item-decisions.ts @@ -41,7 +41,8 @@ export function decideItem( lifecycle: change !== 'update', options: { ...event.producer, turnScope: scope }, resolution: { - kind: 'guarded-item', + kind: + event.type === 'item.update' && event.lossWarning ? 'acp-loss-warning' : 'guarded-item', identity: row.identity, body: event.body, change, diff --git a/src/main/runtime/agent-session/installation/structured-agent-runtime-registrations.ts b/src/main/runtime/agent-session/installation/structured-agent-runtime-registrations.ts index a0292e6a62e..b025f17cc60 100644 --- a/src/main/runtime/agent-session/installation/structured-agent-runtime-registrations.ts +++ b/src/main/runtime/agent-session/installation/structured-agent-runtime-registrations.ts @@ -145,9 +145,7 @@ function createAcpAdapter( const { readers } = context.journalDatabase const savedSession: AcpSavedSessionReads = { hasProviderTurn: (sessionId, nativeId) => - journalSqlReads(readers, sessionId).hasProviderTurn(nativeId), - savedItemIds: async (sessionId, itemIds) => - new Set(await journalSqlReads(readers, sessionId).savedItemIds(itemIds)) + journalSqlReads(readers, sessionId).hasProviderTurn(nativeId) } const definition = acpStructuredAgentDefinition(spec) const sources = structuredAgentLaunchSources(context) diff --git a/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe-omp.test.ts b/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe-omp.test.ts index 9131d4432cd..133a715f24c 100644 --- a/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe-omp.test.ts +++ b/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe-omp.test.ts @@ -42,7 +42,7 @@ const workspace = async () => root function launchWith(settings: Settings) { return createAcpStructuredLaunchResolver(acpLaunchSpecFor('omp')!, { - savedSession: { hasProviderTurn: async () => false, savedItemIds: async () => new Set() }, + savedSession: { hasProviderTurn: async () => false }, store: { getRecord: () => ({ ...agentSessionRecordFixture(), diff --git a/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe.test.ts b/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe.test.ts index 8442e80d45c..5cffebb1156 100644 --- a/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe.test.ts +++ b/src/main/runtime/agent-session/launch/structured-agent-launch-support-probe.test.ts @@ -42,7 +42,7 @@ const workspace = async () => root function launchWith(settings: Settings) { return createAcpStructuredLaunchResolver(acpLaunchSpecFor('opencode')!, { - savedSession: { hasProviderTurn: async () => false, savedItemIds: async () => new Set() }, + savedSession: { hasProviderTurn: async () => false }, store: { getRecord: () => ({ ...agentSessionRecordFixture(), From 1850ba54581452b42ca9b3e7b81a074071c039ae Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 01:51:09 -0800 Subject: [PATCH 28/45] Cover the 4a conversion, history-shape and maintenance-debt cases through outside observation Conversion: seed a medium chat (310 chats on the full profile) and refuse a cached seed from another recipe. Observe the candidate's existing epoch-installed ready boundary with a read-only poll of journal_chat_state. An idle seed copy yields launch to every chat ready and the per-size ready-mark intervals; the families copy verifies an unconverted first open and waits for every chat ready before any steady-state family. History/detail: the stub emits ordinary Claude subagent frames (interleaved and sparse-root corpora) and a shapes turn (huge completed tool originals, one above the detail reply cap, an oversized ordinary message). Each named shape is verified through the real history/detail route before its samples count; diff items stay explicit missing evidence because the Claude adapter writes none. Maintenance: write and rewind large completed rows on a disposable seeded chat under a held outside reader, then measure other-chat sends during the hold, after release and while freed pages are reclaimed, from read-only page/WAL state. Retired-epoch pruning is skipped unless such rows are observed. --- .../native-chat/native-chat-4a-comparison.ts | 95 +++--- .../native-chat/native-chat-4a-fixture.ts | 132 +++++--- .../native-chat-4a-ops-conversion.ts | 310 ++++++++++++++++++ .../native-chat-4a-ops-lifecycle.ts | 132 +------- .../native-chat-4a-ops-maintenance.ts | 215 ++++++++++++ .../native-chat/native-chat-4a-ops-reads.ts | 12 +- .../native-chat/native-chat-4a-ops-shapes.ts | 270 +++++++++++++++ .../native-chat/native-chat-4a-ops-stream.ts | 7 +- .../native-chat-4a-provider-stub.cjs | 132 +++++++- .../native-chat/native-chat-4a-report.ts | 37 ++- .../native-chat-4a-sqlite-observer.ts | 228 +++++++++++++ .../native-chat/native-chat-4a-workloads.ts | 89 ++++- 12 files changed, 1411 insertions(+), 248 deletions(-) create mode 100644 config/scripts/performance/native-chat/native-chat-4a-ops-conversion.ts create mode 100644 config/scripts/performance/native-chat/native-chat-4a-ops-maintenance.ts create mode 100644 config/scripts/performance/native-chat/native-chat-4a-ops-shapes.ts create mode 100644 config/scripts/performance/native-chat/native-chat-4a-sqlite-observer.ts diff --git a/config/scripts/performance/native-chat/native-chat-4a-comparison.ts b/config/scripts/performance/native-chat/native-chat-4a-comparison.ts index b847991f978..3a65cdf5203 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-comparison.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-comparison.ts @@ -16,13 +16,7 @@ import { parseArgs } from 'node:util' import type { Arm } from './native-chat-4a-arm' import type { Manifest } from './native-chat-4a-fixture' import type { RunContext } from './native-chat-4a-run-context' -import { - armMarkdown, - comparisonMarkdown, - summarizeRow, - type ArmResult, - type Row -} from './native-chat-4a-report' +import { armMarkdown, writeComparison, type ArmResult, type Row } from './native-chat-4a-report' import { conversionToolOutputBytes, dryRunWorkload, @@ -65,30 +59,8 @@ const workload = workloadFor( ) const log = (line: string): void => console.error(`[4a ${new Date().toISOString()}] ${line}`) -function writeComparison(out: string, results: ArmResult[]): void { - const [base, next] = results - if (!base || !next) { - return - } - const name = `4a-${base.sha.slice(0, 8)}-vs-${next.sha.slice(0, 8)}` - const summaries = results.map((result) => ({ - ...result, - summaries: result.rows.map((row: Row) => ({ ...row, summary: summarizeRow(row) })) - })) - const machine = { - platform: process.platform, - release: os.release(), - cpu: os.cpus()[0]?.model, - cpus: os.cpus().length, - memoryBytes: os.totalmem() - } - writeFileSync( - path.join(out, `${name}.json`), - `${JSON.stringify({ workload, machine, arms: summaries }, null, 2)}\n` - ) - writeFileSync(path.join(out, `${name}.md`), comparisonMarkdown(base, next)) - log(`wrote ${path.join(out, `${name}.md`)}`) -} +const seededChatCount = (manifest: Manifest): number => + 1 + Number(manifest.mediumSessionId !== null) + manifest.fillerSessionIds.length async function runArm(input: { arm: Arm @@ -102,28 +74,37 @@ async function runArm(input: { const { copyProfile, launchArm, stopArm } = await import('./native-chat-4a-arm') const { resetTags } = await import('./native-chat-4a-run-context') const lifecycle = await import('./native-chat-4a-ops-lifecycle') + const conversion = await import('./native-chat-4a-ops-conversion') + const { ConversionObserver } = await import('./native-chat-4a-sqlite-observer') const queue = await import('./native-chat-4a-ops-queue') const retained = await import('./native-chat-4a-ops-retained') const stream = await import('./native-chat-4a-ops-stream') const reads = await import('./native-chat-4a-ops-reads') + const shapes = await import('./native-chat-4a-ops-shapes') const profileDir = path.join(input.out, 'profile') const rows: Row[] = [] const startedAt = new Date().toISOString() + // Each seed copy's first launch converts on the candidate; the observer starts before it, reading only. const onProfile = async ( seedDir: string, manifest: typeof input.manifest, - body: (ctx: RunContext) => Promise + phase: string, + body: (ctx: RunContext, observer: InstanceType) => Promise ) => { if (existsSync(profileDir)) { renameSync(profileDir, path.join(input.out, `stale-profile-${Date.now()}`)) } copyProfile(seedDir, profileDir) resetTags() + const observer = new ConversionObserver(profileDir) const open = () => launchArm(input.arm, profileDir, input.stubCommand) const ctx = { arm: input.arm, - launch: await open(), + launch: await open().catch(async (error: unknown) => { + await observer.stop() + throw error + }), workload, manifest, rows, @@ -135,14 +116,15 @@ async function runArm(input: { ctx.launch = await open() } try { - await body(ctx) + await body(ctx, observer) } finally { + await observer.stop() await stopArm(ctx.launch) renameSync( profileDir, path.join( input.out, - `after-${input.arm.name}-${manifest.toolOutputBytes}-${input.arm.sha.slice(0, 8)}` + `after-${input.arm.name}-${phase}-${manifest.toolOutputBytes}-${input.arm.sha.slice(0, 8)}` ) ) } @@ -170,19 +152,25 @@ async function runArm(input: { } } - await onProfile(input.seedDir, input.manifest, async (ctx) => { - const seedFiles = lifecycle.seededDbFiles(input.seedDir) - // First: the seeded tail markers and unconverted state exist only before any other family writes. - await run('conversion', () => - lifecycle.firstLaunchRows( - ctx, - seedFiles, - `${workload.history.largeTurns}-turn/${workload.history.fillerChats + 1}-chat profile` - ) + const label = `${workload.history.largeTurns}-turn/${seededChatCount(input.manifest)}-chat profile` + if (input.families.has('conversion')) { + // A copy of its own, left idle, so the sweep's whole duration and per-chat intervals carry no foreground work. + await onProfile(input.seedDir, input.manifest, 'sweep', (ctx, observer) => + run('conversion', () => conversion.sweepRows(ctx, observer, label)) ) + } + await onProfile(input.seedDir, input.manifest, 'families', async (ctx, observer) => { + const seedFiles = conversion.seededDbFiles(input.seedDir) + // First: the seeded tail markers and unconverted state exist only before any other family writes. + await run('conversion', () => conversion.firstLaunchRows(ctx, seedFiles, label, observer)) + await conversion.awaitSweep(ctx, observer, label) await run('send', () => queue.sendAndQueueRows(ctx)) await run('stream', () => stream.streamingRows(ctx)) - await run('history', () => reads.historyRows(ctx)) + await run('history', async () => { + await reads.historyRows(ctx) + await shapes.subagentRows(ctx) + await shapes.itemShapeRows(ctx) + }) await run('outline', () => reads.outlineAndForkRows(ctx)) await run('records', () => reads.recordRows(ctx)) await run('retained', () => retained.retainedAdmissionRows(ctx)) @@ -191,10 +179,15 @@ async function runArm(input: { }) if (input.largeSource && input.families.has('conversion')) { const source = input.largeSource - const label = `${(workload.conversionSourceBytes / 1024 ** 3).toFixed(0)} GiB source` - await onProfile(source.seedDir, source.manifest, (ctx) => + const sourceLabel = `${(workload.conversionSourceBytes / 1024 ** 3).toFixed(0)} GiB source` + await onProfile(source.seedDir, source.manifest, 'large-source', (ctx, observer) => run('conversion', () => - lifecycle.firstLaunchRows(ctx, lifecycle.seededDbFiles(source.seedDir), label) + conversion.firstLaunchRows( + ctx, + conversion.seededDbFiles(source.seedDir), + sourceLabel, + observer + ) ) ) } @@ -228,7 +221,7 @@ async function main(): Promise { .filter((file) => /^arm-.*\.json$/.test(file)) .map((file) => JSON.parse(readFileSync(path.join(out, file), 'utf8')) as ArmResult) .sort((a, b) => a.startedAt.localeCompare(b.startedAt)) - writeComparison(out, results) + writeComparison(out, workload, results, log) return } const families = new Set(values.families.split(',').map((family) => family.trim()) as Family[]) @@ -271,7 +264,7 @@ async function main(): Promise { values['large-source'] && workload.conversionSourceBytes > 0 ? await seed(conversionToolOutputBytes(workload), { ...workload, - history: { ...workload.history, fillerChats: 3 } + history: { ...workload.history, fillerChats: 5, mediumTurns: 0 } }) : null const results: ArmResult[] = [] @@ -292,7 +285,7 @@ async function main(): Promise { writeFileSync(path.join(out, `arm-${arm.name}-${arm.sha.slice(0, 12)}.md`), armMarkdown(result)) results.push(result) } - writeComparison(out, results) + writeComparison(out, workload, results, log) const skipped = results.flatMap((result) => result.rows .filter((row) => row.skipped) diff --git a/config/scripts/performance/native-chat/native-chat-4a-fixture.ts b/config/scripts/performance/native-chat/native-chat-4a-fixture.ts index eabedcc6236..d21de95034a 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-fixture.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-fixture.ts @@ -1,6 +1,8 @@ // Seed the retained-history profile once through the seed arm's own host, so every arm starts from the same logical data. +import { createHash } from 'node:crypto' import { existsSync, readFileSync, renameSync, writeFileSync } from 'node:fs' import path from 'node:path' +import type { Page } from '@stablyai/playwright-test' import { launchArm, prepareWorkspace, @@ -10,22 +12,22 @@ import { } from './native-chat-4a-arm' import { rpc, storageBytes } from './native-chat-4a-probes' import { createChat, history, send, waitForDurable, waitForIdle } from './native-chat-4a-session' -import { - fillerTurn, - largeHistoryTurns, - expectedLargeItems, - type Workload -} from './native-chat-4a-workloads' +import { expectedItems, fillerTurn, historyTurns, type Workload } from './native-chat-4a-workloads' export type Manifest = { seedArm: string seedSha: string workload: Workload['profile'] + /** Digest of the seeded history recipe; a cached seed from another recipe is refused, never reused. */ + recipe: string toolOutputBytes: number workspace: Workspace largeSessionId: string + /** Null when the recipe seeds no medium chat (the large-source profile). */ + mediumSessionId: string | null fillerSessionIds: string[] largeItems: number + mediumItems: number storageBytes: number seedMs: number } @@ -33,10 +35,7 @@ export type Manifest = { const SLEEP_FILLERS_EVERY = 20 /** Count the large chat's rows by paging back from the tail; refuses a fixture that seeded fewer rows than its recipe. */ -async function countItems( - page: Awaited>['page'], - sessionId: string -): Promise { +async function countItems(page: Page, sessionId: string): Promise { let count = 0 let cursor: unknown for (;;) { @@ -49,6 +48,49 @@ async function countItems( } } +const recipeOf = (workload: Workload, toolOutputBytes: number): string => + createHash('sha256') + .update(JSON.stringify({ history: workload.history, toolOutputBytes })) + .digest('hex') + .slice(0, 16) + +/** Send a recipe chat's turns; a queued turn goes while its predecessor still runs, leaving released queue history behind. */ +async function seedRecipeChat( + page: Page, + arm: Arm, + worktreeId: string, + workload: Workload, + chat: 'large' | 'medium', + log: (line: string) => void +): Promise<{ sessionId: string; items: number }> { + const created = await createChat(page, arm, worktreeId) + const turns = chat === 'large' ? workload.history.largeTurns : workload.history.mediumTurns + let turn = 0 + for (const next of historyTurns(workload, chat)) { + if (turn > 0 && !next.queued) { + await waitForDurable(page, created.sessionId, `4A-TAIL-${chat}-${turn - 1}`) + } + await send(page, arm, created.sessionId, next.prompt) + turn += 1 + if (turn % 250 === 0) { + log(`seed: ${chat} chat ${turn}/${turns} turns`) + } + } + await waitForDurable(page, created.sessionId, `4A-TAIL-${chat}-${turn - 1}`, 600_000) + await waitForIdle(page, created.sessionId, 600_000) + const items = await countItems(page, created.sessionId) + // Hosts may group a call with its result; fewer than prompt+reply per turn means turns were lost. + if (items < turns * 2) { + throw new Error(`seed: ${chat} chat holds ${items} rows for ${turns} turns`) + } + if (items < expectedItems(turns, workload)) { + log( + `seed: ${chat} chat holds ${items} visible rows; recipe writes ${expectedItems(turns, workload)}` + ) + } + return { sessionId: created.sessionId, items } +} + export async function seedProfile(input: { arm: Arm workload: Workload @@ -61,32 +103,44 @@ export async function seedProfile(input: { }): Promise { const seedDir = path.join(input.runDir, `seed-${input.workload.profile}-${input.toolOutputBytes}`) const manifestPath = `${seedDir}.json` + const recipe = recipeOf(input.workload, input.toolOutputBytes) if (existsSync(manifestPath) && existsSync(seedDir)) { - return JSON.parse(readFileSync(manifestPath, 'utf8')) as Manifest + const cached = JSON.parse(readFileSync(manifestPath, 'utf8')) as Partial + if (cached.recipe !== recipe) { + throw new Error( + `${manifestPath} was seeded from another recipe (${cached.recipe ?? 'none recorded'}, now ${recipe}); move it and ${seedDir} aside or use a new --out` + ) + } + return cached as Manifest } const started = Date.now() const launch = await launchArm(input.arm, input.profileDir, input.stubCommand) try { const { page } = launch const workspace = await prepareWorkspace(page, input.repoPath, input.stubCommand) - const large = await createChat(page, input.arm, workspace.worktreeIds[0]) - let turn = 0 - for (const next of largeHistoryTurns({ + const recipeWorkload = { ...input.workload, history: { ...input.workload.history, toolOutputBytes: input.toolOutputBytes } - })) { - // A queued turn is sent while its predecessor still runs, leaving released queue history behind. - if (turn > 0 && !next.queued) { - await waitForDurable(page, large.sessionId, `4A-TAIL-large-${turn - 1}`) - } - await send(page, input.arm, large.sessionId, next.prompt) - turn += 1 - if (turn % 250 === 0) { - input.log(`seed: large chat ${turn}/${input.workload.history.largeTurns} turns`) - } } - await waitForDurable(page, large.sessionId, `4A-TAIL-large-${turn - 1}`, 600_000) - await waitForIdle(page, large.sessionId, 600_000) + const large = await seedRecipeChat( + page, + input.arm, + workspace.worktreeIds[0], + recipeWorkload, + 'large', + input.log + ) + const medium = + input.workload.history.mediumTurns > 0 + ? await seedRecipeChat( + page, + input.arm, + workspace.worktreeIds[0], + recipeWorkload, + 'medium', + input.log + ) + : null const fillerSessionIds: string[] = [] const workspaceId = workspace.worktreeIds[1] for (let chat = 0; chat < input.workload.history.fillerChats; chat += 1) { @@ -111,27 +165,18 @@ export async function seedProfile(input: { input.log(`seed: ${chat + 1}/${input.workload.history.fillerChats} filler chats`) } } - const largeItems = await countItems(page, large.sessionId) - // Hosts may group a call with its result; fewer than prompt+reply per turn means turns were lost. - if (largeItems < input.workload.history.largeTurns * 2) { - throw new Error( - `seed: large chat holds ${largeItems} rows for ${input.workload.history.largeTurns} turns` - ) - } - if (largeItems < expectedLargeItems(input.workload)) { - input.log( - `seed: large chat holds ${largeItems} visible rows; recipe writes ${expectedLargeItems(input.workload)}` - ) - } const manifest: Manifest = { seedArm: input.arm.name, seedSha: input.arm.sha, workload: input.workload.profile, + recipe, toolOutputBytes: input.toolOutputBytes, workspace, largeSessionId: large.sessionId, + mediumSessionId: medium?.sessionId ?? null, fillerSessionIds, - largeItems, + largeItems: large.items, + mediumItems: medium?.items ?? 0, storageBytes: 0, seedMs: 0 } @@ -149,3 +194,12 @@ export async function seedProfile(input: { export const seedDirFor = (runDir: string, manifest: Manifest): string => path.join(runDir, `seed-${manifest.workload}-${manifest.toolOutputBytes}`) + +/** The untouched filler the background sweep reaches last (it converts in ascending session_id order). */ +export function unconvertedOpenTarget(manifest: Manifest): string | null { + return manifest.fillerSessionIds.slice(3, -1).sort().at(-1) ?? null +} + +/** Every chat the seed made, in the sweep's order. */ +export const seededChats = (manifest: Manifest): string[] => + [manifest.largeSessionId, manifest.mediumSessionId ?? [], manifest.fillerSessionIds].flat().sort() diff --git a/config/scripts/performance/native-chat/native-chat-4a-ops-conversion.ts b/config/scripts/performance/native-chat/native-chat-4a-ops-conversion.ts new file mode 100644 index 00000000000..b4b80ebfaf5 --- /dev/null +++ b/config/scripts/performance/native-chat/native-chat-4a-ops-conversion.ts @@ -0,0 +1,310 @@ +// 4a one-time conversion rows, reported apart from steady state. Ready is the candidate's existing epoch-installed boundary, +// observed from outside through a read-only poll of journal_chat_state (see native-chat-4a-sqlite-observer.ts). +import path from 'node:path' +import { seededChats, unconvertedOpenTarget } from './native-chat-4a-fixture' +import { openTo, type Chat } from './native-chat-4a-ops-lifecycle' +import { samplePressure, storageBytes } from './native-chat-4a-probes' +import type { Row, Sample } from './native-chat-4a-report' +import { addRow, page, sampled, tagFor, type RunContext } from './native-chat-4a-run-context' +import { send, waitForIdle } from './native-chat-4a-session' +import { + CHAT_STATE_CODE, + seededDbFiles, + type ChatState, + type ConversionObserver +} from './native-chat-4a-sqlite-observer' +import { replyPrompt } from './native-chat-4a-workloads' + +const SWEEP_TIMEOUT_MS = 2 * 60 * 60_000 +const NO_DOMAIN_DATABASE = + 'this arm created no agent-session-domain.db by session ready: nothing converts' + +export { seededDbFiles } from './native-chat-4a-sqlite-observer' + +function observation(observer: ConversionObserver): Record { + return { + observedDatabase: observer.databaseFile + ? path.basename(observer.databaseFile) + : 'none (no agent-session-domain.db)', + readyBoundary: 'journal_chat_state.epoch installed, first poll that sees it', + observationResolutionMs: observer.resolutionMs + } +} + +/** Open a chat for the first time, reading its conversion state before the open and once the open has been requested. */ +async function firstOpen( + ctx: RunContext, + observer: ConversionObserver, + chat: Chat, + marker: string +): Promise<{ sample: Sample; atOpen: ChatState }> { + const before = observer.state(chat.sessionId) + let atOpen: ChatState = before + const requestedAt = Date.now() + const sample = await openTo(ctx, chat, marker, () => { + // Ready marks never revert, so a pending read here proves the chat was unconverted when the open began. + atOpen = observer.state(chat.sessionId) + return { + stateBeforeOpen: CHAT_STATE_CODE[before], + stateAtOpen: CHAT_STATE_CODE[atOpen] + } + }) + observer.state(chat.sessionId) + const readyAt = observer.readyAt.get(chat.sessionId) + sample.extra = { + ...sample.extra, + readyMarkMs: readyAt === undefined || before === 'ready' ? Number.NaN : readyAt - requestedAt + } + return { sample, atOpen } +} + +/** + * The arm's first launch on the seeded profile. On the candidate this is the accepted one-time conversion; on main it is a + * cold open of the same data, so the rows line up but are reported apart from steady state. + */ +export async function firstLaunchRows( + ctx: RunContext, + seedDbFiles: string[], + label: string, + observer: ConversionObserver +): Promise { + const { workspace, largeSessionId, fillerSessionIds } = ctx.manifest + const context = { + profile: label, + largeItems: ctx.manifest.largeItems, + seededBytes: ctx.manifest.storageBytes, + chats: seededChats(ctx.manifest).length, + ...observation(observer) + } + const init = addRow(ctx, { + family: 'conversion', + operation: 'launch to session ready', + variant: label, + context + }) + init.samples.push({ e2eMs: ctx.launch.readyEpochMs - ctx.launch.launchedAtEpochMs }) + const stopPressure = samplePressure(ctx.launch.app, ctx.launch.userDataDir, 500) + const target = unconvertedOpenTarget(ctx.manifest) + const unconverted = addRow(ctx, { + family: 'conversion', + operation: 'first open, unconverted chat (last in sweep order)', + variant: label, + context: { ...context, stateCodes: 'NaN no domain db, 0 pending, 1 importing, 2 ready' } + }) + if (!target) { + unconverted.skipped = 'profile has no untouched filler chat to open unconverted' + } else { + const index = fillerSessionIds.indexOf(target) + await sampled(ctx, unconverted, 1, async () => { + const { sample, atOpen } = await firstOpen( + ctx, + observer, + { sessionId: target, worktreeId: workspace.worktreeIds[1] }, + `4A-TAIL-filler-${index}-${ctx.workload.history.fillerTurns - 1}` + ) + if (atOpen === 'ready' || atOpen === 'uncatalogued') { + addRow(ctx, { + family: 'conversion', + operation: `first open, chat ${atOpen === 'ready' ? 'converted' : 'uncatalogued'} before open`, + variant: label, + context, + samples: [sample] + }) + throw new Error(`target was ${atOpen} when the open began; not an unconverted open`) + } + return sample + }) + } + const small: Chat = { sessionId: fillerSessionIds[0]!, worktreeId: workspace.worktreeIds[1] } + const smallOpen = addRow(ctx, { + family: 'conversion', + operation: 'first open, small chat', + variant: label, + context + }) + const openOnce = (chat: Chat, marker: string) => async () => + (await firstOpen(ctx, observer, chat, marker)).sample + await sampled( + ctx, + smallOpen, + 1, + openOnce(small, `4A-TAIL-filler-0-${ctx.workload.history.fillerTurns - 1}`) + ) + const largeOpen = addRow(ctx, { + family: 'conversion', + operation: 'first open, large chat, with sends to the opened small chat', + variant: label, + context: { ...context, concurrent: 'sends to the already-opened small chat every 250 ms' } + }) + let opening = true + const concurrent: number[] = [] + const sends = (async () => { + while (opening) { + concurrent.push( + (await send(page(ctx), ctx.arm, small.sessionId, replyPrompt(tagFor('during')))).hostMs + ) + await new Promise((resolve) => setTimeout(resolve, 250)) + } + })() + await sampled( + ctx, + largeOpen, + 1, + openOnce( + { sessionId: largeSessionId, worktreeId: workspace.worktreeIds[0] }, + `4A-TAIL-large-${ctx.workload.history.largeTurns - 1}` + ) + ) + opening = false + await sends + const during = addRow(ctx, { + family: 'conversion', + operation: 'small-chat send during large first open', + variant: label, + context + }) + during.samples.push(...concurrent.map((hostMs) => ({ hostMs }))) + const pressure = await stopPressure() + const remaining = seededDbFiles(ctx.launch.userDataDir) + const disk = addRow(ctx, { + family: 'conversion', + operation: 'disk', + variant: label, + context: { + retainedOriginalFiles: + seedDbFiles.filter((file) => remaining.includes(file)).join(' ') || 'none', + newFiles: remaining.filter((file) => !seedDbFiles.includes(file)).join(' ') || 'none' + } + }) + disk.samples.push({ + extra: { + peakTotalBytes: pressure.peakTotalBytes, + afterTotalBytes: storageBytes(ctx.launch.userDataDir).total, + peakMainRssBytes: pressure.peakMainRssBytes + } + }) + await waitForIdle(page(ctx), small.sessionId) +} + +/** Steady-state families start only once every seeded chat is ready, so import never mixes into their samples. */ +export async function awaitSweep( + ctx: RunContext, + observer: ConversionObserver, + label: string +): Promise { + // Polling stops here, so steady-state families carry no observer reads. + try { + if (!observer.databaseFile) { + return + } + const started = Date.now() + const done = await observer.allReady(seededChats(ctx.manifest), SWEEP_TIMEOUT_MS) + const row = addRow(ctx, { + family: 'conversion', + operation: 'wait for every chat ready before steady-state families', + variant: label, + context: observation(observer) + }) + if (!done) { + row.skipped = `not every seeded chat was ready after ${SWEEP_TIMEOUT_MS} ms; later families overlap conversion` + ctx.log(`${ctx.arm.name}: ${row.skipped}`) + return + } + row.samples.push({ e2eMs: Date.now() - started }) + } finally { + await observer.stop() + } +} + +/** Idle launch on its own seed copy: launch to the last ready mark, and each chat's ready-mark interval in observed order. */ +export async function sweepRows( + ctx: RunContext, + observer: ConversionObserver, + label: string +): Promise { + const { manifest, launch } = ctx + const chats = seededChats(manifest) + const context = { + profile: label, + chats: chats.length, + activity: 'none: the app stays idle until the last ready mark', + endPoints: + "previous chat's ready mark → this chat's ready mark in observed order; includes the sweep's idle-boundary waits", + ...observation(observer) + } + const row = (operation: string, extra: Record = {}): Row => + addRow(ctx, { + family: 'conversion', + operation, + variant: label, + context: { ...context, ...extra } + }) + const whole = row('background sweep, launch to every chat ready') + const sized = [ + { name: 'small chats', ids: manifest.fillerSessionIds, items: 'filler recipe' }, + { + name: 'medium chat', + ids: manifest.mediumSessionId ? [manifest.mediumSessionId] : [], + items: manifest.mediumItems + }, + { + name: `${manifest.largeItems}-item chat`, + ids: [manifest.largeSessionId], + items: manifest.largeItems + } + ] + const intervalRows = sized.map((size) => ({ + ...size, + row: row(`background sweep ready-mark interval, ${size.name}`, { items: size.items }) + })) + if (!observer.databaseFile) { + for (const each of [whole, ...intervalRows.map((entry) => entry.row)]) { + each.skipped = NO_DOMAIN_DATABASE + } + return + } + const done = await observer.allReady(chats, SWEEP_TIMEOUT_MS) + const readyAt = (id: string): number => observer.readyAt.get(id)! + const ordered = chats + .filter((id) => observer.readyAt.has(id)) + .sort((a, b) => readyAt(a) - readyAt(b) || (a < b ? -1 : 1)) + const interval = new Map() + let previous = observer.databaseSeenAt + for (const id of ordered) { + interval.set(id, readyAt(id) - previous) + previous = readyAt(id) + } + if (!done) { + whole.skipped = `${ordered.length} of ${chats.length} chats ready after ${SWEEP_TIMEOUT_MS} ms` + } else { + whole.samples.push({ + e2eMs: readyAt(ordered.at(-1)!) - launch.launchedAtEpochMs, + extra: { + sessionReadyMs: launch.readyEpochMs - launch.launchedAtEpochMs, + storeSeenMs: observer.databaseSeenAt - launch.launchedAtEpochMs, + readyBySessionReady: ordered.filter((id) => readyAt(id) <= launch.readyEpochMs).length, + readyOutOfSweepOrder: ordered.filter((id, index) => index > 0 && id < ordered[index - 1]!) + .length, + catalogRows: observer.catalogRows + } + }) + } + for (const { ids, row: sizeRow, name } of intervalRows) { + const spans = ids.flatMap((id) => (interval.has(id) ? [interval.get(id)!] : [])) + if (ids.length === 0) { + sizeRow.skipped = `profile seeds no ${name}` + continue + } + if (spans.length < ids.length) { + sizeRow.skipped = `${ids.length - spans.length} of ${ids.length} ${name} not seen ready` + continue + } + sizeRow.samples.push(...spans.map((e2eMs) => ({ e2eMs }))) + const total = spans.reduce((sum, span) => sum + span, 0) + sizeRow.context = { + ...sizeRow.context, + totalSpanMs: total, + meanMs: spans.length ? Math.round((total / spans.length) * 100) / 100 : Number.NaN + } + } +} diff --git a/config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts b/config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts index b9f54755bc6..db41444fdde 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts @@ -1,14 +1,6 @@ -// 4a process-lifecycle rows: first launch on the seeded profile (the candidate's one-time conversion), cold/warm open, and rewind. -import { readdirSync } from 'node:fs' -import path from 'node:path' -import { - markerSeenAt, - rendererNow, - rpc, - samplePressure, - storageBytes, - watchMarkers -} from './native-chat-4a-probes' +// 4a process-lifecycle rows: cold/warm open and rewind; first launch on the seeded profile lives in the conversion module. +import { unconvertedOpenTarget } from './native-chat-4a-fixture' +import { markerSeenAt, rendererNow, rpc, watchMarkers } from './native-chat-4a-probes' import type { Sample } from './native-chat-4a-report' import { addRow, @@ -30,19 +22,25 @@ import { } from './native-chat-4a-session' import { replyPrompt } from './native-chat-4a-workloads' -type Chat = { sessionId: string; worktreeId: string } +export type Chat = { sessionId: string; worktreeId: string } -/** Open a chat and time it to its known tail marker visible plus controls known. */ -async function openTo(ctx: RunContext, chat: Chat, marker: string): Promise { +/** Open a chat and time it to its known tail marker visible plus controls known; `issued` runs once the open is requested. */ +export async function openTo( + ctx: RunContext, + chat: Chat, + marker: string, + issued?: () => Record +): Promise { return measured(ctx, async () => { await watchMarkers(page(ctx), [marker]) const startedAt = await rendererNow(page(ctx)) await openChatTab(page(ctx), chat.worktreeId, chat.sessionId) + const observed = issued?.() ?? {} const tailAt = await markerSeenAt(page(ctx), marker, 600_000) await waitForControls(page(ctx), 600_000) return { e2eMs: (await rendererNow(page(ctx))) - startedAt, - extra: { tailVisibleMs: tailAt - startedAt } + extra: { tailVisibleMs: tailAt - startedAt, ...observed } } }) } @@ -56,106 +54,6 @@ async function markTail(ctx: RunContext, chat: Chat): Promise { return `4A-TAIL-${tag}` } -const dbFiles = (dir: string): string[] => - readdirSync(dir, { recursive: true, withFileTypes: true }) - .filter( - (entry) => - entry.isFile() && - /\.(db|sqlite)$/.test(entry.name) && - !entry.parentPath.includes(`${path.sep}home`) - ) - .map((entry) => path.relative(dir, path.join(entry.parentPath, entry.name))) - -/** - * The arm's first launch on the seeded profile. On the candidate this is the accepted one-time conversion; on main it is a - * cold open of the same data, so the rows line up but are reported apart from steady state. - */ -export async function firstLaunchRows( - ctx: RunContext, - seedDbFiles: string[], - label: string -): Promise { - const { workspace, largeSessionId, fillerSessionIds } = ctx.manifest - const context = { - profile: label, - largeItems: ctx.manifest.largeItems, - seededBytes: ctx.manifest.storageBytes, - chats: fillerSessionIds.length + 1 - } - const init = addRow(ctx, { - family: 'conversion', - operation: 'launch to session ready', - variant: label, - context - }) - init.samples.push({ e2eMs: ctx.launch.readyEpochMs - ctx.launch.launchedAtEpochMs }) - const stopPressure = samplePressure(ctx.launch.app, ctx.launch.userDataDir, 500) - const small: Chat = { sessionId: fillerSessionIds[0]!, worktreeId: workspace.worktreeIds[1] } - const smallOpen = addRow(ctx, { - family: 'conversion', - operation: 'first open, small chat', - variant: label, - context - }) - await sampled(ctx, smallOpen, 1, () => - openTo(ctx, small, `4A-TAIL-filler-0-${ctx.workload.history.fillerTurns - 1}`) - ) - const largeOpen = addRow(ctx, { - family: 'conversion', - operation: 'first open, large chat, with sends to the opened small chat', - variant: label, - context: { ...context, concurrent: 'sends to the already-opened small chat every 250 ms' } - }) - let opening = true - const concurrent: number[] = [] - const sends = (async () => { - while (opening) { - concurrent.push( - (await send(page(ctx), ctx.arm, small.sessionId, replyPrompt(tagFor('during')))).hostMs - ) - await new Promise((resolve) => setTimeout(resolve, 250)) - } - })() - await sampled(ctx, largeOpen, 1, () => - openTo( - ctx, - { sessionId: largeSessionId, worktreeId: workspace.worktreeIds[0] }, - `4A-TAIL-large-${ctx.workload.history.largeTurns - 1}` - ) - ) - opening = false - await sends - const during = addRow(ctx, { - family: 'conversion', - operation: 'small-chat send during large first open', - variant: label, - context - }) - during.samples.push(...concurrent.map((hostMs) => ({ hostMs }))) - const pressure = await stopPressure() - const remaining = dbFiles(ctx.launch.userDataDir) - const disk = addRow(ctx, { - family: 'conversion', - operation: 'disk', - variant: label, - context: { - retainedOriginalFiles: - seedDbFiles.filter((file) => remaining.includes(file)).join(' ') || 'none', - newFiles: remaining.filter((file) => !seedDbFiles.includes(file)).join(' ') || 'none' - } - }) - disk.samples.push({ - extra: { - peakTotalBytes: pressure.peakTotalBytes, - afterTotalBytes: storageBytes(ctx.launch.userDataDir).total, - peakMainRssBytes: pressure.peakMainRssBytes - } - }) - await waitForIdle(page(ctx), small.sessionId) -} - -export const seededDbFiles = dbFiles - export async function openRows(ctx: RunContext): Promise { const { workspace, largeSessionId, fillerSessionIds } = ctx.manifest const large: Chat = { sessionId: largeSessionId, worktreeId: workspace.worktreeIds[0] } @@ -217,7 +115,9 @@ export async function openRows(ctx: RunContext): Promise { variant: 'small chat', context }) - const untouched = fillerSessionIds.slice(3, -1) + // The conversion family's unconverted-open target is already open. + const target = unconvertedOpenTarget(ctx.manifest) + const untouched = fillerSessionIds.slice(3, -1).filter((id) => id !== target) if (untouched.length === 0) { unopened.skipped = 'profile has no filler chat left untouched by earlier families' } diff --git a/config/scripts/performance/native-chat/native-chat-4a-ops-maintenance.ts b/config/scripts/performance/native-chat/native-chat-4a-ops-maintenance.ts new file mode 100644 index 00000000000..6c4d939d3dd --- /dev/null +++ b/config/scripts/performance/native-chat/native-chat-4a-ops-maintenance.ts @@ -0,0 +1,215 @@ +// 4a maintenance rows on debt the run creates and observes: large completed rows written then rewound on a disposable seeded +// chat while an outside read-only reader holds a snapshot, its release, and freed pages watched back down, each while another +// chat sends. Page, free-list and WAL state come from read-only SQLite reads; nothing asks the app for timing. +import path from 'node:path' +import type { Sample } from './native-chat-4a-report' +import { addRow, page, tagFor, type RunContext } from './native-chat-4a-run-context' +import { + history, + messageItemIds, + mutate, + openChatTab, + send, + waitForControls, + waitForDurable, + waitForIdle +} from './native-chat-4a-session' +import { chatDatabasePath, holdReader, pageState } from './native-chat-4a-sqlite-observer' +import { debtPrompt, replyPrompt } from './native-chat-4a-workloads' + +// JOURNAL_WAL_BACKPRESSURE_FRAMES on the candidate (journal-checkpoint-owner.ts): writes wait above this many WAL frames. +const BACKPRESSURE_FRAMES = 32_768 +const RELEASE_WATCH_MS = 30_000 +const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) + +/** Sends to `other` until `done()`, each sample its host span plus the store's WAL bytes and free pages right after. */ +async function sendsUntil( + ctx: RunContext, + other: string, + file: string, + done: () => boolean, + everyMs: number +): Promise { + const started = Date.now() + const samples: Sample[] = [] + while (!done()) { + const answer = await send(page(ctx), ctx.arm, other, replyPrompt(tagFor('maint'))) + const state = pageState(file) + samples.push({ + hostMs: answer.hostMs, + extra: { + sinceStartMs: Date.now() - started, + walBytes: state.walBytes, + freelistPages: state.freelistPages + } + }) + await sleep(everyMs) + } + await waitForIdle(page(ctx), other) + return samples +} + +const peak = (samples: Sample[], key: string): number => + Math.max(0, ...samples.map((sample) => sample.extra?.[key] ?? 0)) + +export async function maintenanceDebtRows(ctx: RunContext): Promise { + const { fillerSessionIds, workspace } = ctx.manifest + const { debtTools, debtToolBytes, holdMs, reclaimWaitMs } = ctx.workload.maintenance + // The neutral chat, which later families only use as an active tab; its debt turn is rewound away again. + const debt = fillerSessionIds[2] ?? fillerSessionIds.at(-1)! + const other = fillerSessionIds[1] ?? fillerSessionIds[0]! + const file = chatDatabasePath(ctx.launch.userDataDir) + const variant = `${debtTools} × ${(debtToolBytes / 1024 / 1024).toFixed(2)} MiB completed rows` + const context = { + observedDatabase: file ? path.basename(file) : 'none', + holdMs, + concurrent: 'small sends to another chat' + } + const row = (operation: string, extra: Record = {}) => + addRow(ctx, { family: 'maintenance', operation, variant, context: { ...context, ...extra } }) + const removal = row('large-row removal (rewind) under a held outside reader', { + endPoints: 'rewind request → host answer; includes any WAL admission wait' + }) + const hold = row('other-chat sends while an outside reader holds a snapshot', { + backpressureFrames: BACKPRESSURE_FRAMES + }) + const release = row('other-chat sends after the outside reader releases', { + watchMs: RELEASE_WATCH_MS + }) + const reclaim = row('other-chat sends while freed pages are reclaimed', { + watchMs: reclaimWaitMs, + reclaimOwner: 'periodic maintenance each minute, at most 1024 single-page units per run' + }) + const retired = row('retired-epoch prune') + const all = [removal, hold, release, reclaim, retired] + if (!file) { + for (const each of all) { + each.skipped = 'no chat SQLite database found in the profile' + } + return + } + try { + await openChatTab(page(ctx), workspace.worktreeIds[1], debt) + await waitForControls(page(ctx)) + const baseline = pageState(file, true) + const tag = tagFor('debt') + await send(page(ctx), ctx.arm, debt, debtPrompt(ctx.workload, tag)) + await waitForDurable(page(ctx), debt, `4A-TAIL-${tag}`, 600_000, 250) + await waitForIdle(page(ctx), debt, 600_000) + const written = pageState(file) + const [cutAt] = (await messageItemIds(page(ctx), debt, 'user', 1)).slice(-1) + const epoch = (await history(page(ctx), debt, 'tail', 1)).page.epoch + + // The reader is released by a timer, whatever the app or the sends are doing. + const releaseReader = holdReader(file) + let releasedAt = Number.NaN + const timer = setTimeout(() => { + releaseReader() + releasedAt = Date.now() + }, holdMs) + let removalHostMs = Number.NaN + try { + // Settled into a value so a refusal during the hold is never an unhandled rejection. + const rewound = mutate(page(ctx), ctx.arm, 'agentSession.rewind', debt, { + itemId: cutAt, + expectedEpoch: epoch + }).then( + (answer) => ({ answer }), + (error: unknown) => ({ error }) + ) + hold.samples.push( + ...(await sendsUntil(ctx, other, file, () => !Number.isNaN(releasedAt), 250)) + ) + const outcome = await rewound + if ('error' in outcome) { + throw outcome.error + } + removalHostMs = outcome.answer.hostMs + } finally { + clearTimeout(timer) + releaseReader() + releasedAt = Number.isNaN(releasedAt) ? Date.now() : releasedAt + } + const peakWal = peak(hold.samples, 'walBytes') + const walFrames = Math.max(0, Math.floor((peakWal - 32) / (baseline.pageSize + 24))) + hold.context = { + ...hold.context, + peakWalBytes: peakWal, + peakWalFrames: walFrames, + reachedBackpressure: Number(walFrames >= BACKPRESSURE_FRAMES) + } + + const atRelease = pageState(file) + let shrunkAt = Number.NaN + release.samples.push( + ...(await sendsUntil( + ctx, + other, + file, + () => { + if (Number.isNaN(shrunkAt) && pageState(file).walBytes < atRelease.walBytes) { + shrunkAt = Date.now() + } + return !Number.isNaN(shrunkAt) || Date.now() - releasedAt > RELEASE_WATCH_MS + }, + 250 + )) + ) + release.context = { ...release.context, walShrinkMs: shrunkAt - releasedAt } + + const removed = pageState(file, true) + removal.samples.push({ + hostMs: removalHostMs, + extra: { + writtenBytes: (written.pageCount - baseline.pageCount) * baseline.pageSize, + freedPages: removed.freelistPages - baseline.freelistPages + } + }) + + const reclaimStarted = Date.now() + let backAt = Number.NaN + reclaim.samples.push( + ...(await sendsUntil( + ctx, + other, + file, + () => { + if (Number.isNaN(backAt) && pageState(file).freelistPages <= baseline.freelistPages) { + backAt = Date.now() + } + return !Number.isNaN(backAt) || Date.now() - reclaimStarted > reclaimWaitMs + }, + 1_000 + )) + ) + const end = pageState(file, true) + reclaim.context = { + ...reclaim.context, + freelistBaseline: baseline.freelistPages, + freelistAtStart: removed.freelistPages, + freelistAtEnd: end.freelistPages, + reclaimedPages: removed.freelistPages - end.freelistPages, + backToBaselineMs: backAt - reclaimStarted + } + + const retiredCounts = [baseline, removed, end].map((state) => state.retiredEpochRows) + retired.context = { ...retired.context, retiredEpochRows: retiredCounts.join(' → ') } + if (retiredCounts.every((count) => !count)) { + retired.skipped = + 'missing evidence: no row under a retired epoch was observed; ordinary rewind/removal deletes rows in place, so retired-epoch pruning was not exercised' + } else { + retired.samples.push({ + extra: { + retiredAtStart: removed.retiredEpochRows ?? 0, + retiredAtEnd: end.retiredEpochRows ?? 0 + } + }) + } + } catch (error) { + const reason = `failed: ${error instanceof Error ? error.message : String(error)}`.slice(0, 300) + for (const each of all.filter((entry) => entry.samples.length === 0 && !entry.skipped)) { + each.skipped = reason + } + ctx.log(`maintenance debt rows: ${reason}`) + } +} diff --git a/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts b/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts index 14cace375d3..f3ec51ca9da 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts @@ -17,7 +17,7 @@ const bytesOf = (value: unknown): number => Buffer.byteLength(JSON.stringify(val type Item = { itemId: string; body?: { kind?: string } } -async function read( +export async function read( ctx: RunContext, method: string, params: unknown, @@ -63,16 +63,8 @@ export async function historyRows(ctx: RunContext): Promise { read(ctx, 'agentSession.history', { sessionId, ...params }, true) ) } - addRow(ctx, { - family: 'history', - operation: 'sparse/interleaved subagent grouping', - variant: 'tail 200', - context, - skipped: - 'the 4a provider stub emits no subagent frames; covered by the Claude/Codex functional QA run' - }) - // Huge originals come from the streaming rows that ran first; the smallest seeded tool item is the ordinary case. + // Whatever the streaming rows left on the tail; named shapes are verified rows of their own (native-chat-4a-ops-shapes.ts). const items = [...tail.page.items, ...older.page.items] as Item[] const sized = items .map((item) => ({ item, bytes: bytesOf(item) })) diff --git a/config/scripts/performance/native-chat/native-chat-4a-ops-shapes.ts b/config/scripts/performance/native-chat/native-chat-4a-ops-shapes.ts new file mode 100644 index 00000000000..0578bba0fd9 --- /dev/null +++ b/config/scripts/performance/native-chat/native-chat-4a-ops-shapes.ts @@ -0,0 +1,270 @@ +// 4a history/detail rows for named shapes: sparse and interleaved subagent grouping, huge completed tool originals, an +// original above the detail reply cap and an oversized ordinary message. Each shape is verified on the real route first. +import { read } from './native-chat-4a-ops-reads' +import { rpc, samplePressure } from './native-chat-4a-probes' +import type { Row } from './native-chat-4a-report' +import { addRow, page, sampled, tagFor, type RunContext } from './native-chat-4a-run-context' +import { createChat, history, send, waitForDurable, waitForIdle } from './native-chat-4a-session' +import { shapesPrompt, subagentPrompt, type Workload } from './native-chat-4a-workloads' + +// JOURNAL_ITEM_DETAIL_MAX_BYTES on the candidate (journal-domain-history-metadata.ts): 4 MiB outbound JSON less 64 KiB. +const DETAIL_REPLY_CAP_BYTES = 4 * 1024 * 1024 - 64 * 1024 +const MiB = (bytes: number): string => `${(bytes / 1024 / 1024).toFixed(2)} MiB` + +type WireItem = { + itemId: string + agentId?: string + body?: { + kind?: string + presentation?: string + output?: { head?: string; byteLength?: number } + blocks?: { text?: string }[] + } +} +type Page = Awaited>['page'] + +/** One chat holding the turns' corpus, every turn durable and settled before any sample. */ +async function corpusChat(ctx: RunContext, prompts: string[]): Promise { + const chat = await createChat(page(ctx), ctx.arm, ctx.manifest.workspace.worktreeIds[1]) + for (const prompt of prompts) { + const tag = /"tag":"([^"]+)"/.exec(prompt)![1] + await send(page(ctx), ctx.arm, chat.sessionId, prompt) + await waitForDurable(page(ctx), chat.sessionId, `4A-TAIL-${tag}`, 600_000, 250) + await waitForIdle(page(ctx), chat.sessionId, 600_000) + } + return chat.sessionId +} + +const itemsOf = (read: Page): WireItem[] => read.items as WireItem[] +const byProviderId = (items: WireItem[], id: string): WireItem | undefined => + items.find((item) => JSON.stringify(item).includes(`"${id}"`)) + +/** Fill a row only when `verify` names no mismatch; the observed shape stays in the row either way. */ +async function verifiedRow( + ctx: RunContext, + row: Row, + observed: Record, + mismatch: string | null, + take: () => ReturnType +): Promise { + row.context = { ...row.context, ...observed } + if (mismatch) { + row.skipped = `shape not produced on this arm: ${mismatch}` + ctx.log(`${row.family}/${row.operation}/${row.variant}: ${row.skipped}`) + return + } + await sampled(ctx, row, ctx.workload.samples, take) +} + +export async function subagentRows(ctx: RunContext): Promise { + const { subagents } = ctx.workload + const corpora = [ + { + mode: 'interleaved' as const, + variant: `tail 200 (${subagents.agents} parallel agents × ${subagents.childrenPerAgent} children × ${subagents.interleavedTurns} turns)`, + turns: subagents.interleavedTurns + }, + { + mode: 'sparse' as const, + variant: `tail 200 (${subagents.sparseChildren} children under one agent, near-empty root)`, + turns: 1 + } + ] + for (const corpus of corpora) { + const sessionId = await corpusChat( + ctx, + Array.from({ length: corpus.turns }, () => + subagentPrompt(ctx.workload, tagFor(`sub-${corpus.mode}`), corpus.mode) + ) + ) + const tail = await history(page(ctx), sessionId, 'tail', 200) + const items = itemsOf(tail.page) + const agentIds = items.map((item) => item.agentId ?? null) + const subagentItems = agentIds.filter((id) => id !== null) + const switches = subagentItems.filter( + (id, index) => index > 0 && id !== subagentItems[index - 1] + ) + const observed = { + sessionId, + pageItems: items.length, + subagentItems: subagentItems.length, + ownItems: items.length - subagentItems.length, + distinctAgents: new Set(subagentItems).size, + agentSwitches: switches.length + } + const mismatch = + subagentItems.length === 0 + ? 'the tail page carries no item with agentId' + : corpus.mode === 'interleaved' && observed.distinctAgents < subagents.agents + ? `${observed.distinctAgents} distinct agents on the page` + : corpus.mode === 'interleaved' && switches.length < subagents.childrenPerAgent + ? `${switches.length} agent switches; children did not interleave` + : corpus.mode === 'sparse' && observed.ownItems > subagentItems.length / 10 + ? `${observed.ownItems} own rows against ${subagentItems.length} subagent rows` + : null + const tailRow = addRow(ctx, { + family: 'history', + operation: `subagent grouping, ${corpus.mode}`, + variant: corpus.variant, + context: {} + }) + await verifiedRow(ctx, tailRow, observed, mismatch, () => + read(ctx, 'agentSession.history', { sessionId, direction: 'tail', limit: 200 }) + ) + const older = addRow(ctx, { + family: 'history', + operation: `subagent grouping, ${corpus.mode}`, + variant: 'one older page 200', + context: {} + }) + await verifiedRow( + ctx, + older, + { hasOlder: Number(tail.page.hasOlder) }, + mismatch ?? (tail.page.hasOlder ? null : 'the corpus fits one page'), + () => + read(ctx, 'agentSession.history', { + sessionId, + direction: 'before', + limit: 200, + cursor: tail.page.window.nextCursor + }) + ) + } +} + +/** Item detail with the main process's peak RSS while it answers, since worker-to-host bytes are not observable outside. */ +const detail = (ctx: RunContext, sessionId: string, itemId: string) => async () => { + const stop = samplePressure(ctx.launch.app, ctx.launch.userDataDir, 50) + const sample = await read(ctx, 'agentSession.item', { sessionId, itemId }) + const pressure = await stop() + return { ...sample, extra: { ...sample.extra, peakMainRssBytes: pressure.peakMainRssBytes } } +} + +async function detailOf(ctx: RunContext, sessionId: string, itemId: string): Promise { + return (await rpc<{ item: WireItem }>(page(ctx), 'agentSession.item', { sessionId, itemId })) + .value.item +} + +export async function itemShapeRows(ctx: RunContext): Promise { + const shapes: Workload['shapes'] = ctx.workload.shapes + const tag = tagFor('shapes') + const sessionId = await corpusChat(ctx, [shapesPrompt(ctx.workload, tag)]) + const tail = await history(page(ctx), sessionId, 'tail', 40) + const items = itemsOf(tail.page) + const huge = Array.from({ length: shapes.hugeTools }, (_, index) => + byProviderId(items, `toolu_4a_${tag}_huge_${index}`) + ) + const cap = byProviderId(items, `toolu_4a_${tag}_cap`) + const headBytes = (item: WireItem | undefined): number => + Buffer.byteLength(item?.body?.output?.head ?? '') + const hugeMismatch = huge.some((item) => !item) + ? `${huge.filter(Boolean).length} of ${shapes.hugeTools} huge tool items on the tail page` + : huge.find((item) => item!.body?.output?.byteLength !== shapes.hugeToolBytes) + ? 'a huge tool item does not report its whole original byteLength' + : huge.find((item) => headBytes(item) >= shapes.hugeToolBytes) + ? 'a huge tool item carries its whole original on the page' + : null + const hugeRow = addRow(ctx, { + family: 'history', + operation: 'huge completed tool originals, heads on one page', + variant: `tail 40 (${shapes.hugeTools} × ${MiB(shapes.hugeToolBytes)} originals + ${MiB(shapes.capToolBytes)})`, + context: { sessionId } + }) + await verifiedRow( + ctx, + hugeRow, + { + originalBytes: shapes.hugeTools * shapes.hugeToolBytes + shapes.capToolBytes, + largestHeadBytes: Math.max(...huge.map(headBytes), headBytes(cap)) + }, + hugeMismatch, + () => read(ctx, 'agentSession.history', { sessionId, direction: 'tail', limit: 40 }) + ) + + for (const [variant, item, bytes] of [ + ['original above the reply cap', cap, shapes.capToolBytes], + ['huge original under the reply cap', huge[0], shapes.hugeToolBytes] + ] as const) { + const row = addRow(ctx, { + family: 'history', + operation: 'agentSession.item detail', + variant, + context: { originalBytes: bytes, replyCapBytes: DETAIL_REPLY_CAP_BYTES } + }) + const full = item ? await detailOf(ctx, sessionId, item.itemId) : undefined + const above = bytes > DETAIL_REPLY_CAP_BYTES + const mismatch = !full + ? 'item not found on the tail page' + : full.body?.output?.byteLength !== bytes + ? `detail reports ${full.body?.output?.byteLength} original bytes` + : above && headBytes(full) >= bytes + ? 'detail returned the whole original above the cap' + : !above && headBytes(full) !== bytes + ? 'detail shortened an original under the cap' + : null + await verifiedRow(ctx, row, { detailHeadBytes: headBytes(full) }, mismatch, () => + detail(ctx, sessionId, item!.itemId)() + ) + } + + // The message's group exceeds every page budget, so the page that reaches it carries only its placeholder. + let cursor: unknown + let carrying: { cursor: unknown; item: WireItem } | null = null + for (let pages = 0; pages < 4 && !carrying; pages += 1) { + const at = await history(page(ctx), sessionId, cursor ? 'before' : 'tail', 40, cursor) + const placeholder = itemsOf(at.page).find( + (item) => item.body?.presentation === 'history-item-too-large' + ) + carrying = placeholder ? { cursor, item: placeholder } : null + if (!at.page.hasOlder) { + break + } + cursor = at.page.window.nextCursor + } + const message = carrying ? await detailOf(ctx, sessionId, carrying.item.itemId) : undefined + const text = message?.body?.blocks?.map((block) => block.text ?? '').join('') ?? '' + const messageMismatch = !carrying + ? 'no history-item-too-large placeholder within 4 pages' + : !text.startsWith(`4A-BIG-${tag} `) + ? 'the placeholder is not the seeded message' + : Buffer.byteLength(text) !== shapes.messageBytes + `4A-BIG-${tag} `.length + ? `detail returned ${Buffer.byteLength(text)} message bytes` + : null + const placeholderRow = addRow(ctx, { + family: 'history', + operation: 'oversized ordinary message placeholder', + variant: `page reaching a ${MiB(shapes.messageBytes)} message`, + context: { messageBytes: shapes.messageBytes } + }) + await verifiedRow( + ctx, + placeholderRow, + { detailTextBytes: Buffer.byteLength(text) }, + messageMismatch, + () => + read(ctx, 'agentSession.history', { + sessionId, + direction: carrying!.cursor ? 'before' : 'tail', + limit: 40, + ...(carrying!.cursor ? { cursor: carrying!.cursor } : {}) + }) + ) + const messageDetail = addRow(ctx, { + family: 'history', + operation: 'agentSession.item detail', + variant: 'oversized ordinary message (under the reply cap)', + context: { messageBytes: shapes.messageBytes, replyCapBytes: DETAIL_REPLY_CAP_BYTES } + }) + await verifiedRow(ctx, messageDetail, {}, messageMismatch, () => + detail(ctx, sessionId, carrying!.item.itemId)() + ) + addRow(ctx, { + family: 'history', + operation: 'completed diff originals', + variant: 'tail 40', + context: {}, + skipped: + 'missing evidence: the 4a stub speaks Claude stream-json, and the Claude adapter writes no diff items (only the Codex and ACP adapters do)' + }) +} diff --git a/config/scripts/performance/native-chat/native-chat-4a-ops-stream.ts b/config/scripts/performance/native-chat/native-chat-4a-ops-stream.ts index e472400b08d..084e70c306d 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-ops-stream.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-ops-stream.ts @@ -8,6 +8,7 @@ import { storageBytes, watchMarkers } from './native-chat-4a-probes' +import { maintenanceDebtRows } from './native-chat-4a-ops-maintenance' import { stat, type Sample } from './native-chat-4a-report' import { addRow, @@ -125,7 +126,7 @@ async function streamSample( } } -/** After a large stream, sends to another chat while retired payloads are pruned, pages reclaimed and the WAL checkpointed. */ +/** After a large stream, sends to another chat and large-chat reads while whatever maintenance follows runs; no debt is asserted. */ async function maintenanceWindow(ctx: RunContext): Promise { const other = ctx.manifest.fillerSessionIds[1] ?? ctx.manifest.fillerSessionIds[0]! const fromEpoch = Date.now() @@ -191,7 +192,8 @@ export async function streamingRows(ctx: RunContext): Promise { variant: size, context: { windowMs: MAINTENANCE_WINDOW_MS, - retiredPayloadBytes: stream.toolOutputBytes ?? stream.bytes + // The stream's final body stays current; retired-data debt is measured in the maintenance module. + precedingStreamBytes: stream.toolOutputBytes ?? stream.bytes } }) try { @@ -201,6 +203,7 @@ export async function streamingRows(ctx: RunContext): Promise { `failed: ${error instanceof Error ? error.message : String(error)}`.slice(0, 300) } } + await maintenanceDebtRows(ctx) // A cheap read after all streams confirms the large chat still answers its tail. await rpc(page(ctx), 'agentSession.history', { sessionId: largeSessionId, diff --git a/config/scripts/performance/native-chat/native-chat-4a-provider-stub.cjs b/config/scripts/performance/native-chat/native-chat-4a-provider-stub.cjs index 524bad5a412..8d9013de26a 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-provider-stub.cjs +++ b/config/scripts/performance/native-chat/native-chat-4a-provider-stub.cjs @@ -43,10 +43,12 @@ if (fs.existsSync(transcriptPath)) { } // Fork and rewind read the provider's own transcript, so every user/assistant frame is also saved there. +// Subagent frames are sidechain rows, as the CLI records them; the main chain skips them. function remember(frame) { + const sidechain = Boolean(frame.parent_tool_use_id) const row = { parentUuid, - isSidechain: false, + isSidechain: sidechain, type: frame.type, message: frame.message, uuid: frame.uuid, @@ -55,7 +57,9 @@ function remember(frame) { cwd: process.cwd() } fs.appendFileSync(transcriptPath, `${JSON.stringify(row)}\n`) - parentUuid = frame.uuid + if (!sidechain) { + parentUuid = frame.uuid + } } let backlog = Promise.resolve() @@ -84,8 +88,10 @@ function framed(type, message, extra = {}) { return write(frame) } -const assistant = (content) => - framed('assistant', { id: randomUUID(), role: 'assistant', model: 'claude-fake', content }) +const assistant = (content, extra) => + framed('assistant', { id: randomUUID(), role: 'assistant', model: 'claude-fake', content }, extra) +const system = (subtype, fields) => + write({ type: 'system', subtype, ...fields, uuid: randomUUID(), session_id: sessionId }) const streamEvent = (event) => write({ type: 'stream_event', @@ -100,11 +106,11 @@ let current = null const pending = [] let turnCounter = 0 -async function toolRun(turn, index, outputBytes, runMs) { - const id = `toolu_4a_${turn}_${index}` - await assistant([ - { type: 'tool_use', id, name: 'Bash', input: { command: `ls -la dir${index}` } } - ]) +async function toolRun(turn, index, outputBytes, runMs, id = `toolu_4a_${turn}_${index}`, extra) { + await assistant( + [{ type: 'tool_use', id, name: 'Bash', input: { command: `ls -la dir${index}` } }], + extra + ) if (runMs > 0) { await write({ type: 'system', @@ -118,12 +124,103 @@ async function toolRun(turn, index, outputBytes, runMs) { }) await sleep(runMs) } - await framed('user', { - role: 'user', - content: [ - { tool_use_id: id, type: 'tool_result', content: prose(outputBytes, id), is_error: false } - ] - }) + await framed( + 'user', + { + role: 'user', + content: [ + { tool_use_id: id, type: 'tool_result', content: prose(outputBytes, id), is_error: false } + ] + }, + extra + ) +} + +/** Shapes for history/detail: an oversized ordinary message, huge completed tool originals, one above the detail cap. */ +async function shapes(op, signal) { + await assistant([{ type: 'text', text: `4A-BIG-${op.tag} ${prose(op.messageBytes, op.tag)}` }]) + for (let index = 0; index < op.hugeTools && !signal.aborted; index += 1) { + await toolRun(0, index, op.hugeToolBytes, 0, `toolu_4a_${op.tag}_huge_${index}`) + } + await toolRun(0, 0, op.capToolBytes, 0, `toolu_4a_${op.tag}_cap`) +} + +/** Foreground Agent calls in the CLI's frame shapes; children of parallel agents alternate, one agent's children run sparse. */ +async function subagents(op, signal) { + const agents = Array.from({ length: op.agents }, (_, index) => ({ + toolId: `toolu_4a_${op.tag}_agent_${index}`, + taskId: `task4a${op.tag.replace(/\W/g, '')}${index}`, + prompt: `4A-SUBAGENT-${op.tag}-${index} list the files` + })) + await assistant( + agents.map((agent) => ({ + type: 'tool_use', + id: agent.toolId, + name: 'Agent', + input: { description: 'List files', subagent_type: 'general-purpose', prompt: agent.prompt } + })) + ) + for (const agent of agents) { + await system('task_started', { + task_id: agent.taskId, + tool_use_id: agent.toolId, + description: 'List files', + subagent_type: 'general-purpose', + is_backgrounded: false, + spawn_depth: 1, + task_type: 'local_agent', + prompt: agent.prompt + }) + await framed( + 'user', + { role: 'user', content: [{ type: 'text', text: agent.prompt }] }, + { + parent_tool_use_id: agent.toolId, + subagent_type: 'general-purpose', + task_description: 'List files' + } + ) + } + for (let child = 0; child < op.children && !signal.aborted; child += 1) { + for (const agent of agents) { + await toolRun(0, child, 200, 0, `${agent.toolId}_child_${child}`, { + parent_tool_use_id: agent.toolId, + subagent_type: 'general-purpose', + task_description: 'List files' + }) + } + } + for (const agent of agents) { + const summary = `4A-SUBAGENT-DONE-${op.tag} ${agent.taskId}` + await system('task_notification', { + task_id: agent.taskId, + tool_use_id: agent.toolId, + status: 'completed', + summary + }) + await framed( + 'user', + { + role: 'user', + content: [ + { + tool_use_id: agent.toolId, + type: 'tool_result', + content: [{ type: 'text', text: summary }] + } + ] + }, + { + tool_use_result: { + status: 'completed', + prompt: agent.prompt, + agentId: agent.taskId, + agentType: 'general-purpose', + content: [{ type: 'text', text: summary }] + } + } + ) + } } async function streamReply(op, signal) { @@ -178,6 +275,11 @@ async function runOp(op, signal) { await assistant([{ type: 'text', text: `4A-TAIL-${tag} ${prose(op.replyBytes ?? 1500, tag)}` }]) return } + if (op.op === 'shapes' || op.op === 'subagents') { + await (op.op === 'shapes' ? shapes : subagents)(op, signal) + await assistant([{ type: 'text', text: `4A-TAIL-${tag} Stub reply.` }]) + return + } if (op.op === 'stream') { if (op.toolOutputBytes) { await toolRun(turn, 0, op.toolOutputBytes, op.toolRunMs ?? 250) diff --git a/config/scripts/performance/native-chat/native-chat-4a-report.ts b/config/scripts/performance/native-chat/native-chat-4a-report.ts index 44021dacc5c..f9efc93ac6d 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-report.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-report.ts @@ -1,5 +1,9 @@ // 4a rows, percentiles and the PR-body table: main vs candidate per row, keyed by each arm's head SHA. +import { writeFileSync } from 'node:fs' +import os from 'node:os' +import path from 'node:path' import type { HostWork } from './native-chat-4a-probes' +import type { Workload } from './native-chat-4a-workloads' export type Sample = { /** Action to user-visible (or durably readable, when the row says so) result. */ @@ -130,7 +134,7 @@ export function comparisonMarkdown(base: ArmResult, next: ArmResult): string { } lines.push( '', - 'Primary latency is action → visible result where the row has a rendered end point, else the RPC host span. Off-main CPU is process CPU minus main-thread event-loop active time (storage workers). Conversion rows are one-time import cost, reported separately from steady state; per-row spans, WAL/file growth, event-loop lag and peak memory are in the JSON.' + 'Primary latency is action → visible result where the row has a rendered end point, else the RPC host span. Off-main CPU is process CPU minus main-thread event-loop active time (storage workers). Conversion rows are one-time import cost, reported separately from steady state; steady-state families start only after every chat is seen ready. Rows whose Shape names `observedDatabase` end at an outside read-only SQLite observation and carry its resolution. Per-row spans, WAL/file growth, event-loop lag and peak memory are in the JSON.' ) return `${lines.join('\n')}\n` } @@ -150,3 +154,34 @@ export function armMarkdown(result: ArmResult): string { } return `${lines.join('\n')}\n` } + +/** The comparison JSON (every sample and summary, the workload and machine) beside its table. */ +export function writeComparison( + out: string, + workload: Workload, + results: ArmResult[], + log: (line: string) => void +): void { + const [base, next] = results + if (!base || !next) { + return + } + const name = `4a-${base.sha.slice(0, 8)}-vs-${next.sha.slice(0, 8)}` + const summaries = results.map((result) => ({ + ...result, + summaries: result.rows.map((row: Row) => ({ ...row, summary: summarizeRow(row) })) + })) + const machine = { + platform: process.platform, + release: os.release(), + cpu: os.cpus()[0]?.model, + cpus: os.cpus().length, + memoryBytes: os.totalmem() + } + writeFileSync( + path.join(out, `${name}.json`), + `${JSON.stringify({ workload, machine, arms: summaries }, null, 2)}\n` + ) + writeFileSync(path.join(out, `${name}.md`), comparisonMarkdown(base, next)) + log(`wrote ${path.join(out, `${name}.md`)}`) +} diff --git a/config/scripts/performance/native-chat/native-chat-4a-sqlite-observer.ts b/config/scripts/performance/native-chat/native-chat-4a-sqlite-observer.ts new file mode 100644 index 00000000000..7e73610eb2a --- /dev/null +++ b/config/scripts/performance/native-chat/native-chat-4a-sqlite-observer.ts @@ -0,0 +1,228 @@ +// Read-only outside observation of an arm's chat SQLite database: per-chat ready marks, page/WAL state and a held reader snapshot. +// Every connection opens with readOnly; nothing here writes to the profile or asks the app for timing. +import { existsSync, readdirSync, statSync } from 'node:fs' +import path from 'node:path' +import { DatabaseSync } from 'node:sqlite' + +const DOMAIN_DB = 'agent-session-domain.db' +const JOURNAL_DB = 'agent-session-journal.db' + +function findDatabase(userDataDir: string, name: string): string | null { + for (const entry of readdirSync(userDataDir, { recursive: true, withFileTypes: true })) { + if (entry.isFile() && entry.name === name && !entry.parentPath.includes(`${path.sep}home`)) { + return path.join(entry.parentPath, entry.name) + } + } + return null +} + +/** Every SQLite file in the profile outside the isolated home, relative to it: which originals a conversion retained. */ +export const seededDbFiles = (dir: string): string[] => + readdirSync(dir, { recursive: true, withFileTypes: true }) + .filter( + (entry) => + entry.isFile() && + /\.(db|sqlite)$/.test(entry.name) && + !entry.parentPath.includes(`${path.sep}home`) + ) + .map((entry) => path.relative(dir, path.join(entry.parentPath, entry.name))) + +/** The store chat rows live in on this arm: the candidate's converted store when present, else the journal store both arms keep. */ +export const chatDatabasePath = (userDataDir: string): string | null => + findDatabase(userDataDir, DOMAIN_DB) ?? findDatabase(userDataDir, JOURNAL_DB) + +const openReadOnly = (file: string): DatabaseSync => + new DatabaseSync(file, { readOnly: true, timeout: 2_000 }) + +const hasTable = (db: DatabaseSync, name: string): boolean => + db.prepare("SELECT 1 FROM sqlite_schema WHERE type='table' AND name=?").get(name) !== undefined + +export type PageState = { + pageSize: number + pageCount: number + freelistPages: number + walBytes: number + /** Rows under an epoch other than their chat's ready epoch; null on a store without epochs. */ + retiredEpochRows: number | null +} + +/** One short read-only look at page, free-list and WAL state; `retired` adds a scan for retired-epoch rows. */ +export function pageState(file: string, retired = false): PageState { + const db = openReadOnly(file) + try { + const pragma = (name: string): number => + Number(Object.values(db.prepare(`PRAGMA ${name}`).get() ?? {})[0] ?? Number.NaN) + const epochs = hasTable(db, 'journal_chat_state') && hasTable(db, 'journal_items') + return { + pageSize: pragma('page_size'), + pageCount: pragma('page_count'), + freelistPages: pragma('freelist_count'), + walBytes: statSync(`${file}-wal`, { throwIfNoEntry: false })?.size ?? 0, + retiredEpochRows: + retired && epochs + ? Number( + db + .prepare(`SELECT count(*) AS n FROM journal_items item JOIN journal_chat_state head + ON head.session_id=item.session_id WHERE head.epoch IS NOT NULL AND item.epoch<>head.epoch`) + .get()?.n + ) + : null + } + } finally { + db.close() + } +} + +/** Hold one read snapshot, as a long reader would, until the returned release runs. */ +export function holdReader(file: string): () => void { + const db = openReadOnly(file) + db.exec('BEGIN') + db.prepare('SELECT count(*) FROM sqlite_schema').get() + let held = true + return () => { + if (held) { + held = false + db.exec('COMMIT') + db.close() + } + } +} + +/** Where a chat stands on the candidate's epoch-installed ready boundary. */ +export type ChatState = 'no-domain-database' | 'uncatalogued' | 'pending' | 'importing' | 'ready' +export const CHAT_STATE_CODE: Record = { + 'no-domain-database': Number.NaN, + uncatalogued: -1, + pending: 0, + importing: 1, + ready: 2 +} + +/** + * Polls `journal_chat_state` for epochs installed since the last poll; a chat's ready time is the first poll that sees it, + * so every ready mark is an upper bound with `resolutionMs` resolution. Start it before the launch it observes. + */ +export class ConversionObserver { + readonly readyAt = new Map() + databaseSeenAt = Number.NaN + catalogRows = 0 + private db: DatabaseSync | null = null + /** The converted store is created beside the seeded journal store, in the same state directory. */ + private readonly file: string | null + private slowestPollMs = 0 + private stopped = false + private readonly loop: Promise + + constructor( + userDataDir: string, + readonly everyMs = 25 + ) { + const journal = findDatabase(userDataDir, JOURNAL_DB) + this.file = journal ? path.join(path.dirname(journal), DOMAIN_DB) : null + this.loop = this.run() + } + + /** The converted store once it exists; null on an arm that never creates one. */ + get databaseFile(): string | null { + return this.db ? this.file : null + } + + get resolutionMs(): number { + return this.everyMs + Math.ceil(this.slowestPollMs) + } + + private connection(): DatabaseSync | null { + if (!this.db) { + if (!this.file || !existsSync(this.file)) { + return null + } + try { + const db = openReadOnly(this.file) + if (!hasTable(db, 'journal_chat_state')) { + db.close() + return null + } + this.db = db + this.databaseSeenAt = Date.now() + } catch { + return null + } + } + return this.db + } + + private poll(): void { + const db = this.connection() + if (!db) { + return + } + const started = performance.now() + try { + const ready = db + .prepare('SELECT session_id FROM journal_chat_state WHERE epoch IS NOT NULL') + .all() + const now = Date.now() + for (const row of ready) { + const id = String(row.session_id) + if (!this.readyAt.has(id)) { + this.readyAt.set(id, now) + } + } + this.catalogRows = Number(db.prepare('SELECT count(*) AS n FROM journal_chat_state').get()?.n) + } catch { + // A busy or briefly locked store is retried on the next poll. + } + this.slowestPollMs = Math.max(this.slowestPollMs, performance.now() - started) + } + + private async run(): Promise { + while (!this.stopped) { + this.poll() + // Unref'd: an observer left running never keeps the harness process alive. + await new Promise((resolve) => setTimeout(resolve, this.everyMs).unref()) + } + } + + /** Read one chat's state now, outside the poll. */ + state(sessionId: string): ChatState { + const db = this.connection() + if (!db) { + return 'no-domain-database' + } + const head = db + .prepare('SELECT epoch FROM journal_chat_state WHERE session_id=?') + .get(sessionId) + if (!head) { + return 'uncatalogued' + } + if (head.epoch !== null) { + this.readyAt.set(sessionId, this.readyAt.get(sessionId) ?? Date.now()) + return 'ready' + } + return db.prepare('SELECT 1 FROM journal_items WHERE session_id=? LIMIT 1').get(sessionId) + ? 'importing' + : 'pending' + } + + /** Resolve once every listed chat was seen ready (true), or false at the deadline or without a domain database. */ + async allReady(sessionIds: readonly string[], timeoutMs: number): Promise { + const deadline = Date.now() + timeoutMs + while (Date.now() < deadline) { + if (!this.connection()) { + return false + } + if (sessionIds.every((id) => this.readyAt.has(id))) { + return true + } + await new Promise((resolve) => setTimeout(resolve, this.everyMs)) + } + return false + } + + async stop(): Promise { + this.stopped = true + await this.loop + this.db?.close() + this.db = null + } +} diff --git a/config/scripts/performance/native-chat/native-chat-4a-workloads.ts b/config/scripts/performance/native-chat/native-chat-4a-workloads.ts index 532480c824f..de021cb3056 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-workloads.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-workloads.ts @@ -19,6 +19,8 @@ export type Workload = { history: { /** The large retained chat: one recipe turn is 1 prompt + `tools` tool items + 1 reply (+ its turn row). */ largeTurns: number + /** The medium chat's turns, same recipe; 0 seeds no medium chat. */ + mediumTurns: number tools: number toolOutputBytes: number promptBytes: number @@ -35,6 +37,17 @@ export type Workload = { rewind: { nearTipSamples: number; deepCutTurns: number; deepSamples: number } /** Generated only on the M4Air run; smoke keeps the 55k shape off this Mac. */ conversionSourceBytes: number + /** Huge completed tool originals whose heads share one page, one original above the detail reply cap, one oversized message. */ + shapes: { hugeTools: number; hugeToolBytes: number; capToolBytes: number; messageBytes: number } + /** Interleaved: parallel subagents whose child rows alternate, per turn. Sparse: one subagent's rows under a near-empty root. */ + subagents: { + interleavedTurns: number + agents: number + childrenPerAgent: number + sparseChildren: number + } + /** Large completed rows written then rewound on a disposable chat, an outside reader held for `holdMs`, reclaim watched for `reclaimWaitMs`. */ + maintenance: { debtTools: number; debtToolBytes: number; holdMs: number; reclaimWaitMs: number } } const MiB = 1024 * 1024 @@ -59,6 +72,7 @@ export const WORKLOADS: Record = { coldSamples: 1, history: { largeTurns: 20, + mediumTurns: 5, tools: 8, toolOutputBytes: 2000, promptBytes: 300, @@ -70,21 +84,25 @@ export const WORKLOADS: Record = { stream: { cases: streamCases([MiB / 4]), chunkBytes: 16_384, intervalMs: 1, samples: 1 }, retained: { checkpoints: [0, 20], liveQueue: 2 }, rewind: { nearTipSamples: 2, deepCutTurns: 5, deepSamples: 1 }, - conversionSourceBytes: 0 + conversionSourceBytes: 0, + shapes: { hugeTools: 3, hugeToolBytes: MiB, capToolBytes: 5 * MiB, messageBytes: 3 * MiB }, + subagents: { interleavedTurns: 2, agents: 2, childrenPerAgent: 30, sparseChildren: 250 }, + maintenance: { debtTools: 4, debtToolBytes: 2 * MiB, holdMs: 10_000, reclaimWaitMs: 150_000 } }, full: { profile: 'full', samples: 30, coldSamples: 5, - // The established 5000-turn/55000-item recipe plus 309 small chats: the 55k-history/310-chat profile. + // The established 5000-turn/55000-item recipe, a 500-turn medium chat and 308 small chats: the 55k-history/310-chat profile. history: { largeTurns: 5000, + mediumTurns: 500, tools: 8, toolOutputBytes: 2000, promptBytes: 300, replyBytes: 1500, queuedEvery: 10, - fillerChats: 309, + fillerChats: 308, fillerTurns: 2 }, stream: { @@ -95,7 +113,11 @@ export const WORKLOADS: Record = { }, retained: { checkpoints: [0, 1_000, 10_000], liveQueue: 2 }, rewind: { nearTipSamples: 10, deepCutTurns: 1000, deepSamples: 3 }, - conversionSourceBytes: 20 * 1024 * MiB + conversionSourceBytes: 20 * 1024 * MiB, + shapes: { hugeTools: 4, hugeToolBytes: 3 * MiB, capToolBytes: 16 * MiB, messageBytes: 3 * MiB }, + subagents: { interleavedTurns: 20, agents: 2, childrenPerAgent: 40, sparseChildren: 400 }, + // 160 MiB of rows rewound under a held reader passes the candidate's 32,768-frame WAL admission threshold. + maintenance: { debtTools: 10, debtToolBytes: 16 * MiB, holdMs: 60_000, reclaimWaitMs: 600_000 } } } @@ -109,13 +131,17 @@ export function workloadFor(profile: string, conversionSourceBytes?: number): Wo export type HistoryTurn = { prompt: string; queued: boolean } -/** Large-chat recipe turns in send order; each reply ends with a 4A-TAIL marker naming its turn. */ -export function* largeHistoryTurns(workload: Workload): Generator { +/** A recipe chat's turns in send order; each reply ends with a 4A-TAIL marker naming its chat and turn. */ +export function* historyTurns( + workload: Workload, + chat: 'large' | 'medium' = 'large' +): Generator { const { history } = workload - for (let turn = 0; turn < history.largeTurns; turn += 1) { + const turns = chat === 'large' ? history.largeTurns : history.mediumTurns + for (let turn = 0; turn < turns; turn += 1) { const op = { op: 'history', - tag: `large-${turn}`, + tag: `${chat}-${turn}`, tools: history.tools, toolOutputBytes: history.toolOutputBytes, replyBytes: history.replyBytes @@ -138,9 +164,9 @@ export function fillerTurn(chat: number, turn: number, workload: Workload): stri return providerText.formatDirective(op, workload.history.promptBytes) } -/** Expected large-chat rows, so a run can refuse a fixture that did not seed what it claims. */ -export function expectedLargeItems(workload: Workload): number { - return workload.history.largeTurns * (workload.history.tools + 3) +/** Expected recipe-chat rows, so a run can refuse a fixture that did not seed what it claims. */ +export function expectedItems(turns: number, workload: Workload): number { + return turns * (workload.history.tools + 3) } /** Per-turn tool output that makes the conversion source reach `conversionSourceBytes` with the same turn count. */ @@ -167,6 +193,29 @@ export function streamPrompt(workload: Workload, stream: StreamCase): string { }) } +export const shapesPrompt = (workload: Workload, tag: string): string => + providerText.formatDirective({ op: 'shapes', tag, ...workload.shapes }) +export const subagentPrompt = ( + workload: Workload, + tag: string, + mode: 'interleaved' | 'sparse' +): string => { + const { agents, childrenPerAgent, sparseChildren } = workload.subagents + return providerText.formatDirective({ + op: 'subagents', + tag, + agents: mode === 'sparse' ? 1 : agents, + children: mode === 'sparse' ? sparseChildren : childrenPerAgent + }) +} +export const debtPrompt = (workload: Workload, tag: string): string => + providerText.formatDirective({ + op: 'history', + tag, + tools: workload.maintenance.debtTools, + toolOutputBytes: workload.maintenance.debtToolBytes, + replyBytes: 400 + }) export const holdPrompt = (tag: string, ms: number): string => providerText.formatDirective({ op: 'hold', tag, ms }) export const replyPrompt = (tag: string): string => @@ -178,7 +227,7 @@ export function dryRunWorkload(workload: Workload): Record { let promptBytes = 0 let queued = 0 let turns = 0 - for (const turn of largeHistoryTurns(workload)) { + for (const turn of historyTurns(workload)) { promptBytes += Buffer.byteLength(turn.prompt) queued += Number(turn.queued) turns += 1 @@ -198,13 +247,25 @@ export function dryRunWorkload(workload: Workload): Record { return { profile: workload.profile, largeTurns: turns, - expectedLargeItems: expectedLargeItems(workload), + expectedLargeItems: expectedItems(workload.history.largeTurns, workload), + expectedMediumItems: expectedItems(workload.history.mediumTurns, workload), queuedHistoryTurns: queued, largePromptBytes: promptBytes, largeToolOutputBytes: turns * workload.history.tools * workload.history.toolOutputBytes, - chats: 1 + workload.history.fillerChats, + chats: 1 + Number(workload.history.mediumTurns > 0) + workload.history.fillerChats, + mediumPromptDigest: providerText.digest( + [...historyTurns(workload, 'medium')].map((turn) => turn.prompt).join('\n') + ), fillerPromptDigest: providerText.digest(fillerTurn(0, 0, workload)), streams, + shapesPromptDigest: providerText.digest(shapesPrompt(workload, 'shapes')), + subagentPromptDigests: (['interleaved', 'sparse'] as const).map((mode) => + providerText.digest(subagentPrompt(workload, 'subagents', mode)) + ), + debtPromptDigest: providerText.digest(debtPrompt(workload, 'debt')), + shapes: workload.shapes, + subagents: workload.subagents, + maintenance: workload.maintenance, retained: workload.retained, rewind: workload.rewind, conversion: { From 168a3c6fd6b5f400cf073d1481cad7214a4378be Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:08:31 -0800 Subject: [PATCH 29/45] Remove the retired ACP warning-reader fixture field --- src/main/native-chat/chat-launcher-command.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/native-chat/chat-launcher-command.test.ts b/src/main/native-chat/chat-launcher-command.test.ts index a8254071f6a..49fdd31b9de 100644 --- a/src/main/native-chat/chat-launcher-command.test.ts +++ b/src/main/native-chat/chat-launcher-command.test.ts @@ -72,8 +72,7 @@ describe('chat launcher argv', () => { })(sessionIdentity) : createAcpStructuredLaunchResolver(spec, { savedSession: { - hasProviderTurn: async () => false, - savedItemIds: async () => new Set() + hasProviderTurn: async () => false }, ...common, resolveCommand: () => process.execPath, From de4460fe1d42a8513a7a7354770636b65db9d395 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:12:19 -0800 Subject: [PATCH 30/45] Validate warning writer authority before duplicate selection --- .../journal-domain-item.ts | 31 ++++++++++++------- .../journal-domain-loss-warnings.test.ts | 18 +++++++++++ .../journal-sql-steps.ts | 5 ++- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/main/native-chat/agent-session-journal/journal-domain-item.ts b/src/main/native-chat/agent-session-journal/journal-domain-item.ts index a8179801cfc..5837b9c2ed2 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-item.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-item.ts @@ -71,6 +71,25 @@ export type JournalDomainItemCommit = { } /** Runs only on the owning writer connection, inside its command SAVEPOINT. */ +export function assertJournalDomainItemAuthority( + db: Database.Database, + target: { sessionId: string; epoch: string }, + fence: number +) { + if (!db.isTransaction) { + throw new Error('journal message requires its writer transaction') + } + const head = db + .prepare(`SELECT last_sequence, highest_fence, context_boundary FROM journal_chat_state + WHERE session_id=? AND epoch=?`) + .get(target.sessionId, target.epoch) + if (!head) { + throw new Error('journal message targets a retired chat epoch') + } + assertJournalFence(fence, Number(head.highest_fence)) + return head +} + export function applyJournalDomainItem( db: Database.Database, target: { sessionId: string; epoch: string }, @@ -78,19 +97,9 @@ export function applyJournalDomainItem( savedAt: number, stopSettle?: JournalStopSettleFact ): JournalDomainItemCommit { - if (!db.isTransaction) { - throw new Error('journal message requires its writer transaction') - } const { sessionId, epoch } = target const prefix = [sessionId, epoch] as const - const head = db - .prepare(`SELECT last_sequence, highest_fence, context_boundary FROM journal_chat_state - WHERE session_id=? AND epoch=?`) - .get(...prefix) - if (!head) { - throw new Error('journal message targets a retired chat epoch') - } - assertJournalFence(input.options.fence, Number(head.highest_fence)) + const head = assertJournalDomainItemAuthority(db, target, input.options.fence) const cursor = { epoch, sequence: Number(head.last_sequence) } const empty: JournalDomainItemCommit = { cursor, diff --git a/src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts b/src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts index f468dfbce94..e9e3c4db65c 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts @@ -131,6 +131,24 @@ it('preserves imported semantic duplicates across writers and body kinds until t ).toMatchObject({ outcomes: [{ kind: 'committed', wrote: true }] }) }) +it('rejects a stale warning writer before suppressing a matching warning', async () => { + const f = domainLifecycleFixture(cleanups) + await f.run({ kind: 'steps', steps: [warning('a', 'first')] }) + const originalId = agentJournalItemKey(warningIdentity('a', 'first')) + const original = f.item(originalId) + f.db.exec("UPDATE journal_chat_state SET highest_fence=2 WHERE session_id='chat'") + for (const lostKey of ['first', 'missing']) { + const result = await f.run({ kind: 'steps', steps: [warning('b', lostKey)] }) + expect(result.value).toMatchObject({ + outcomes: [], + failure: { code: 'journal_stale_fence' }, + cursor: { epoch: 'epoch', sequence: 1 } + }) + expect(result.publication?.items).toEqual([]) + } + expect(f.item(originalId)).toEqual(original) +}) + it('does not suppress a missing warning for hidden rows, another physical epoch or an alias alone', async () => { const f = domainLifecycleFixture(cleanups) const hidden = warningIdentity('old', 'hidden') diff --git a/src/main/native-chat/agent-session-journal/journal-sql-steps.ts b/src/main/native-chat/agent-session-journal/journal-sql-steps.ts index 5dd3cc40458..9fddd652b14 100644 --- a/src/main/native-chat/agent-session-journal/journal-sql-steps.ts +++ b/src/main/native-chat/agent-session-journal/journal-sql-steps.ts @@ -11,7 +11,7 @@ import type { } from './writing/journal-writer-step' import { resolveJournalTimelineItem } from './writing/journal-timeline-resolution' import { resolveProviderTimelineSettlement } from '../agent-session-timeline/provider-timeline-writer-resolution' -import { applyJournalDomainItem } from './journal-domain-item' +import { applyJournalDomainItem, assertJournalDomainItemAuthority } from './journal-domain-item' import { journalDomainTimelineReads } from './journal-domain-timeline-reads' import { applyJournalDomainLifecycle, @@ -198,6 +198,9 @@ function writeStep( identity = step.identity body = step.body } else if (step.kind === 'resolved-item') { + if (step.resolution.kind === 'acp-loss-warning') { + assertJournalDomainItemAuthority(db, target, step.options.fence) + } const resolved = resolveJournalTimelineItem( view, step.resolution, From 1c4f725b27e19b4005a91bf66f1d8c0e092e797c Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:12:47 -0800 Subject: [PATCH 31/45] Wake queued delivery when pending requests change --- .../threads/codex-structured-thread-catch-up.test.ts | 2 +- .../structured/threads/codex-thread-catch-up-rewind.test.ts | 2 +- .../agent-session-journal/journal-domain-current-item.ts | 4 ++++ .../agent-session-journal/journal-domain-item-removal.ts | 4 +++- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/codex/structured/threads/codex-structured-thread-catch-up.test.ts b/src/main/codex/structured/threads/codex-structured-thread-catch-up.test.ts index f5ddb3619ca..98f0f750e9a 100644 --- a/src/main/codex/structured/threads/codex-structured-thread-catch-up.test.ts +++ b/src/main/codex/structured/threads/codex-structured-thread-catch-up.test.ts @@ -524,7 +524,7 @@ describe('Codex chat catch-up', () => { operationId, callerKey, itemId: target, - expectedEpoch: c.journal.physicalCursor().epoch, + expectedEpoch: c.journal.cursor().epoch, phase: 'provider-succeeded' } }) diff --git a/src/main/codex/structured/threads/codex-thread-catch-up-rewind.test.ts b/src/main/codex/structured/threads/codex-thread-catch-up-rewind.test.ts index acb3bb9beb4..bbc9c9086dc 100644 --- a/src/main/codex/structured/threads/codex-thread-catch-up-rewind.test.ts +++ b/src/main/codex/structured/threads/codex-thread-catch-up-rewind.test.ts @@ -116,7 +116,7 @@ it('retains the original adoption cutoff through native rewind and disk recovery operationId, callerKey, itemId: target, - expectedEpoch: c.journal.physicalCursor().epoch, + expectedEpoch: c.journal.cursor().epoch, phase: 'provider-succeeded' } }) diff --git a/src/main/native-chat/agent-session-journal/journal-domain-current-item.ts b/src/main/native-chat/agent-session-journal/journal-domain-current-item.ts index 60b9ee9fd39..efed9a42d6d 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-current-item.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-current-item.ts @@ -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) || diff --git a/src/main/native-chat/agent-session-journal/journal-domain-item-removal.ts b/src/main/native-chat/agent-session-journal/journal-domain-item-removal.ts index 938346d4f5f..839878badc5 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-item-removal.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-item-removal.ts @@ -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 ) { From f1cd02bc4175c226057167025e0b78dfd9638794 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:17:26 -0800 Subject: [PATCH 32/45] Validate authority before all resolved item selection --- .../journal-domain-loss-warnings.test.ts | 34 ++++++++++++++----- .../journal-sql-steps.ts | 4 +-- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts b/src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts index e9e3c4db65c..b84377267d9 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts @@ -27,7 +27,10 @@ function warningIdentity(namespace: string, lostKey: string, agent = 'grok') { }) } -const warning = (namespace: string, lostKey: string): JournalWriterStep => ({ +const warning = ( + namespace: string, + lostKey: string +): Extract => ({ kind: 'resolved-item', options, reservedBytes: 4096, @@ -131,20 +134,33 @@ it('preserves imported semantic duplicates across writers and body kinds until t ).toMatchObject({ outcomes: [{ kind: 'committed', wrote: true }] }) }) -it('rejects a stale warning writer before suppressing a matching warning', async () => { +it('rejects stale direct and nested warning writers before suppressing a matching warning', async () => { const f = domainLifecycleFixture(cleanups) await f.run({ kind: 'steps', steps: [warning('a', 'first')] }) const originalId = agentJournalItemKey(warningIdentity('a', 'first')) const original = f.item(originalId) f.db.exec("UPDATE journal_chat_state SET highest_fence=2 WHERE session_id='chat'") for (const lostKey of ['first', 'missing']) { - const result = await f.run({ kind: 'steps', steps: [warning('b', lostKey)] }) - expect(result.value).toMatchObject({ - outcomes: [], - failure: { code: 'journal_stale_fence' }, - cursor: { epoch: 'epoch', sequence: 1 } - }) - expect(result.publication?.items).toEqual([]) + const direct = warning('b', lostKey) + const nested: JournalWriterStep = { + ...direct, + resolution: { + kind: 'stop-decided', + turnId: 'turn', + endedAt: 2, + ran: direct.resolution, + stopped: null + } + } + for (const step of [direct, nested]) { + const result = await f.run({ kind: 'steps', steps: [step] }) + expect(result.value).toMatchObject({ + outcomes: [], + failure: { code: 'journal_stale_fence' }, + cursor: { epoch: 'epoch', sequence: 1 } + }) + expect(result.publication?.items).toEqual([]) + } } expect(f.item(originalId)).toEqual(original) }) diff --git a/src/main/native-chat/agent-session-journal/journal-sql-steps.ts b/src/main/native-chat/agent-session-journal/journal-sql-steps.ts index 9fddd652b14..e0dde38c61d 100644 --- a/src/main/native-chat/agent-session-journal/journal-sql-steps.ts +++ b/src/main/native-chat/agent-session-journal/journal-sql-steps.ts @@ -198,9 +198,7 @@ function writeStep( identity = step.identity body = step.body } else if (step.kind === 'resolved-item') { - if (step.resolution.kind === 'acp-loss-warning') { - assertJournalDomainItemAuthority(db, target, step.options.fence) - } + assertJournalDomainItemAuthority(db, target, step.options.fence) const resolved = resolveJournalTimelineItem( view, step.resolution, From 889a3233fe816e5efbfce1701f770ee992e27b58 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:21:18 -0800 Subject: [PATCH 33/45] Use current chat test fixtures and name the handover journal owner --- .../delivery/structured-agent-session-handing-over.ts | 7 ++++--- tests/e2e/ai-vault-manual-name-clear.unit.test.ts | 2 +- tests/e2e/automation-terminal-launch.unit.test.ts | 2 +- tests/e2e/codex-child-approval-activity-row.unit.test.ts | 4 ++-- .../agent-session-clear-old-client.unit.test.ts | 2 +- .../agent-session-clear-old-tab-mirror.unit.test.ts | 2 +- .../agent-session-saved-name-old-client.unit.test.ts | 2 +- .../cross-version-agent-session-wire.unit.test.ts | 2 +- .../cross-version-wire/cross-version-stop-note-test-rig.ts | 2 +- .../paired-agent-session-option-persistence.unit.test.ts | 2 +- .../structured-agent-session-host-fixture.ts | 2 +- tests/e2e/helpers/automation-chat-reuse-test-harness.ts | 2 +- tests/e2e/journal-restored-consumers.unit.test.ts | 2 +- tests/e2e/native-chat-startup-option-pickers.unit.test.ts | 2 +- tests/e2e/structured-chat-tab-lifetime.unit.test.ts | 2 +- .../structured-permission-report-publication.unit.test.ts | 4 ++-- 16 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-handing-over.ts b/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-handing-over.ts index 2fe9e9bed6d..5426acdefb1 100644 --- a/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-handing-over.ts +++ b/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-handing-over.ts @@ -3,9 +3,10 @@ import type { AgentJournalSubmission } from '../../../../shared/agent-session/journal/agent-session-journal-types' import type { UnsentSendHold } from '../../agent-session-journal/queued-messages/journal-unsent-send-hold' +import type { AgentSessionJournal } from '../../agent-session-journal/journal-store' /** The chat's journal, which identifies the conversation for as long as it is held. */ -type HandoverJournal = object +type HandoverJournal = AgentSessionJournal type HandoverInput = { owner: () => void @@ -174,7 +175,7 @@ export function beginStructuredAgentSessionHandover( /** Finite policy snapshots retain their synchronous membership check. Live handles read it in-worker. */ function hasSubmissionSnapshot( - journal: object -): journal is { submission: (id: string) => AgentJournalSubmission | undefined } { + journal: HandoverJournal +): journal is HandoverJournal & { submission: (id: string) => AgentJournalSubmission | undefined } { return 'submission' in journal && typeof journal.submission === 'function' } diff --git a/tests/e2e/ai-vault-manual-name-clear.unit.test.ts b/tests/e2e/ai-vault-manual-name-clear.unit.test.ts index 18121fab78f..8f6d717dee2 100644 --- a/tests/e2e/ai-vault-manual-name-clear.unit.test.ts +++ b/tests/e2e/ai-vault-manual-name-clear.unit.test.ts @@ -33,7 +33,7 @@ import { import { StructuredAgentSessionHost } from '../../src/main/native-chat/agent-session-wire/host/structured-agent-session-host' import { setStructuredAgentSessionHost } from '../../src/main/native-chat/agent-session-wire/host/structured-agent-session-registry' import { structuredAgentsDeclaringNothing } from '../../src/main/native-chat/agent-session-wire/providers/testing/structured-agent-session-adapter-router-test-support' -import { recordingStructuredAgentSessionLogger } from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-logger-test-support' +import { recordingStructuredAgentSessionLogger } from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-logger.test-fixture' import { createStructuredChatNamingHandler } from '../../src/main/native-chat/structured-chat-naming' import { PI_RPC_AGENT } from '../../src/main/pi/rpc-agent-definition' import type { AgentSessionStatusEvent } from '../../src/shared/agent-session/agent-session-wire' diff --git a/tests/e2e/automation-terminal-launch.unit.test.ts b/tests/e2e/automation-terminal-launch.unit.test.ts index f380669fd20..83af30d33ac 100644 --- a/tests/e2e/automation-terminal-launch.unit.test.ts +++ b/tests/e2e/automation-terminal-launch.unit.test.ts @@ -12,7 +12,7 @@ import { HOST_TEST_NOW, HOST_TEST_SESSION, HOST_TEST_LOCATION -} from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data' +} from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture' import type { callStructuredAgentSession } from '@/runtime/structured-agent-session-client' import type { StructuredAgentSessionHost } from '../../src/main/native-chat/agent-session-wire/host/structured-agent-session-host' import { createQueuedMessageTestRig } from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig.test-fixture' diff --git a/tests/e2e/codex-child-approval-activity-row.unit.test.ts b/tests/e2e/codex-child-approval-activity-row.unit.test.ts index c8f8c573a05..0b46aaaa27f 100644 --- a/tests/e2e/codex-child-approval-activity-row.unit.test.ts +++ b/tests/e2e/codex-child-approval-activity-row.unit.test.ts @@ -25,7 +25,7 @@ import { CODEX_COMMAND_APPROVAL_METHOD } from '../../src/main/codex/structured/p import { createTrackedJournalOpener } from '../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import { createDeferredStructuredAgentSessionEventSink } from '../../src/main/native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink' import { StructuredAgentSessionStatusFeed } from '../../src/main/native-chat/agent-session-wire/observations/structured-agent-session-status-feed' -import { indexedStatusFeedSession } from '../../src/main/native-chat/agent-session-wire/observations/testing/structured-agent-session-status-feed-test-session' +import { indexedStatusFeedSession } from '../../src/main/native-chat/agent-session-wire/observations/testing/structured-agent-session-status-feed-test-session.test-fixture' import { makeRepo, makeWorktree @@ -67,7 +67,7 @@ vi.mock('@/runtime/structured-agent-session-client', () => ({ import { StructuredAgentSessionStatusBridge } from '../../src/renderer/src/components/native-chat/session/status/StructuredAgentSessionStatusBridge' import { resetStructuredAgentSessionStatusFeedsForTests } from '../../src/renderer/src/runtime/structured-agent-session-status-feed' import { createStructuredAgentSessionLogger } from '../../src/main/native-chat/agent-session-wire/host/structured-agent-session-logger' -import { testEventSinkLogging } from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-logger-test-support' +import { testEventSinkLogging } from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-logger.test-fixture' import { codexProviderHandle } from '../../src/shared/agent-session/lifecycle/agent-session-provider-handle-encoding' const SESSION = 'codex-child-approval' diff --git a/tests/e2e/cross-version-wire/agent-session-clear-old-client.unit.test.ts b/tests/e2e/cross-version-wire/agent-session-clear-old-client.unit.test.ts index c3887c98867..5c4d1de79c0 100644 --- a/tests/e2e/cross-version-wire/agent-session-clear-old-client.unit.test.ts +++ b/tests/e2e/cross-version-wire/agent-session-clear-old-client.unit.test.ts @@ -11,7 +11,7 @@ import { eventually } from '../../../src/main/native-chat/agent-session-wire/hos import { HOST_TEST_SESSION as SESSION, hostTestOperationId -} from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data' +} from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture' import { agentJournalSubmissionKey } from '../../../src/shared/agent-session/journal/agent-session-journal-item-key' import { EMPTY_STRUCTURED_AGENT_SESSION } from '../../../src/shared/agent-session/structured-agent-session-reducer' import type { useNativeChatRewind } from '../../../src/renderer/src/components/native-chat/use-native-chat-rewind' diff --git a/tests/e2e/cross-version-wire/agent-session-clear-old-tab-mirror.unit.test.ts b/tests/e2e/cross-version-wire/agent-session-clear-old-tab-mirror.unit.test.ts index dcd8ee4b1de..f5453ff94bb 100644 --- a/tests/e2e/cross-version-wire/agent-session-clear-old-tab-mirror.unit.test.ts +++ b/tests/e2e/cross-version-wire/agent-session-clear-old-tab-mirror.unit.test.ts @@ -3,7 +3,7 @@ import { importReleaseCheckoutModule, materializeReleaseCheckout } from './relea import { HOST_TEST_SESSION, HOST_TEST_NOW -} from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data' +} from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture' import { WORKTREE, createChat, diff --git a/tests/e2e/cross-version-wire/agent-session-saved-name-old-client.unit.test.ts b/tests/e2e/cross-version-wire/agent-session-saved-name-old-client.unit.test.ts index 4b5d4e9b43a..633354242b4 100644 --- a/tests/e2e/cross-version-wire/agent-session-saved-name-old-client.unit.test.ts +++ b/tests/e2e/cross-version-wire/agent-session-saved-name-old-client.unit.test.ts @@ -7,7 +7,7 @@ import { listResult } from '../../../src/main/ai-vault/structured-session-owners import { HOST_TEST_SESSION, HOST_TEST_LOCATION -} from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data' +} from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture' import { WORKTREE, createChat, diff --git a/tests/e2e/cross-version-wire/cross-version-agent-session-wire.unit.test.ts b/tests/e2e/cross-version-wire/cross-version-agent-session-wire.unit.test.ts index 2682991be02..3a38fa9d44d 100644 --- a/tests/e2e/cross-version-wire/cross-version-agent-session-wire.unit.test.ts +++ b/tests/e2e/cross-version-wire/cross-version-agent-session-wire.unit.test.ts @@ -8,7 +8,7 @@ import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vite import type { StructuredAgentSessionAdapter } from '../../../src/main/native-chat/agent-session-wire/providers/structured-agent-session-adapter' import { StructuredAgentSessionHost } from '../../../src/main/native-chat/agent-session-wire/host/structured-agent-session-host' import { emptyAgentSessionBatch } from '../../../src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-empty-batch' -import { startsWhenPublished } from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-instant-start.test-support' +import { startsWhenPublished } from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-instant-start.test-fixture' import { setStructuredAgentSessionHost } from '../../../src/main/native-chat/agent-session-wire/host/structured-agent-session-registry' import type { AgentSessionRecordStore } from '../../../src/main/runtime/agent-session/records/agent-session-record-store' import { openTestAgentSessionRecordStore } from '../../../src/main/runtime/agent-session/records/agent-session-record-store-test-harness' diff --git a/tests/e2e/cross-version-wire/cross-version-stop-note-test-rig.ts b/tests/e2e/cross-version-wire/cross-version-stop-note-test-rig.ts index 487c01839b5..e81d39a3ff7 100644 --- a/tests/e2e/cross-version-wire/cross-version-stop-note-test-rig.ts +++ b/tests/e2e/cross-version-wire/cross-version-stop-note-test-rig.ts @@ -4,7 +4,7 @@ import { join } from 'node:path' import { afterEach, beforeAll, beforeEach, expect } from 'vitest' import { StructuredAgentSessionHost } from '../../../src/main/native-chat/agent-session-wire/host/structured-agent-session-host' import { NO_STRUCTURED_AGENTS } from '../../../src/main/native-chat/agent-session-wire/providers/testing/structured-agent-session-adapter-router-test-support' -import { startsWhenPublished } from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-instant-start.test-support' +import { startsWhenPublished } from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-instant-start.test-fixture' import { setStructuredAgentSessionHost } from '../../../src/main/native-chat/agent-session-wire/host/structured-agent-session-registry' import { createStructuredAgentSessionLogger } from '../../../src/main/native-chat/agent-session-wire/host/structured-agent-session-logger' import { openTestAgentSessionRecordStore } from '../../../src/main/runtime/agent-session/records/agent-session-record-store-test-harness' diff --git a/tests/e2e/cross-version-wire/paired-agent-session-option-persistence.unit.test.ts b/tests/e2e/cross-version-wire/paired-agent-session-option-persistence.unit.test.ts index 31d155461a7..118593bc1e3 100644 --- a/tests/e2e/cross-version-wire/paired-agent-session-option-persistence.unit.test.ts +++ b/tests/e2e/cross-version-wire/paired-agent-session-option-persistence.unit.test.ts @@ -19,7 +19,7 @@ import { HOST_TEST_SESSION as REST_TEST_SESSION, hostTestAttachParams, hostTestOperationId -} from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data' +} from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture' import { setStructuredAgentSessionHost } from '../../../src/main/native-chat/agent-session-wire/host/structured-agent-session-registry' import { computeAgentSessionPayloadFingerprint } from '../../../src/shared/agent-session/lifecycle/agent-session-mutation-envelope' diff --git a/tests/e2e/cross-version-wire/structured-agent-session-host-fixture.ts b/tests/e2e/cross-version-wire/structured-agent-session-host-fixture.ts index be640a1bf5a..226dd9ef36d 100644 --- a/tests/e2e/cross-version-wire/structured-agent-session-host-fixture.ts +++ b/tests/e2e/cross-version-wire/structured-agent-session-host-fixture.ts @@ -2,7 +2,7 @@ import { agentSessionRecordFixture } from '../../../src/shared/agent-session/lif import { agentSessionConversationView } from '../../../src/shared/agent-session/lifecycle/agent-session-conversation-view' import { vi, type Mock } from 'vitest' import type { StructuredAgentSessionAdapter } from '../../../src/main/native-chat/agent-session-wire/providers/structured-agent-session-adapter' -import { startsWhenPublished } from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-instant-start.test-support' +import { startsWhenPublished } from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-instant-start.test-fixture' import type { AgentSessionRecordStore } from '../../../src/main/runtime/agent-session/records/agent-session-record-store' import { codexProviderHandle } from '../../../src/shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import type { StructuredAgentSessionHost } from '../../../src/main/native-chat/agent-session-wire/host/structured-agent-session-host' diff --git a/tests/e2e/helpers/automation-chat-reuse-test-harness.ts b/tests/e2e/helpers/automation-chat-reuse-test-harness.ts index 7d0507a66a5..b0f4524cba0 100644 --- a/tests/e2e/helpers/automation-chat-reuse-test-harness.ts +++ b/tests/e2e/helpers/automation-chat-reuse-test-harness.ts @@ -22,7 +22,7 @@ import { HOST_TEST_NOW, HOST_TEST_THREAD, HOST_TEST_LOCATION -} from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data' +} from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture' import { createQueuedMessageTestRig } from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig.test-fixture' import { eventually } from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig-provider.test-fixture' diff --git a/tests/e2e/journal-restored-consumers.unit.test.ts b/tests/e2e/journal-restored-consumers.unit.test.ts index 0cd671a8534..103d4429c36 100644 --- a/tests/e2e/journal-restored-consumers.unit.test.ts +++ b/tests/e2e/journal-restored-consumers.unit.test.ts @@ -43,7 +43,7 @@ import type { JournalRow } from '../../src/main/native-chat/agent-session-journa import { hostTestAttachParams, HOST_TEST_SESSION as SESSION -} from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data' +} from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture' import { seedQueuedRigLegacyJournal } from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-queued-legacy-journal.test-fixture' import { createRestTestRig, diff --git a/tests/e2e/native-chat-startup-option-pickers.unit.test.ts b/tests/e2e/native-chat-startup-option-pickers.unit.test.ts index ab658574f43..546ac155cba 100644 --- a/tests/e2e/native-chat-startup-option-pickers.unit.test.ts +++ b/tests/e2e/native-chat-startup-option-pickers.unit.test.ts @@ -5,7 +5,7 @@ import { createElement } from 'react' import { afterEach, expect, it, vi } from 'vitest' import { computeAgentSessionPayloadFingerprint } from '../../src/shared/agent-session/lifecycle/agent-session-mutation-envelope' import { claudeSessionIdForOrcaSession } from '../../src/main/claude/structured/claude-structured-session-id' -import { hostTestMessage } from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data' +import { hostTestMessage } from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture' import { StructuredAgentSessionStartupAttempts } from '../../src/main/native-chat/agent-session-wire/startup/structured-agent-session-startup-attempt' import { createScriptedClaudeRuntime } from '../../src/main/runtime/agent-session/installation/structured-claude-scripted-runtime-test-support' import { TooltipProvider } from '../../src/renderer/src/components/ui/tooltip' diff --git a/tests/e2e/structured-chat-tab-lifetime.unit.test.ts b/tests/e2e/structured-chat-tab-lifetime.unit.test.ts index afc88129802..6a7cd86e735 100644 --- a/tests/e2e/structured-chat-tab-lifetime.unit.test.ts +++ b/tests/e2e/structured-chat-tab-lifetime.unit.test.ts @@ -13,7 +13,7 @@ import { import { HOST_TEST_LOCATION, HOST_TEST_SESSION -} from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data' +} from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture' import { OrcaRuntimeService } from '../../src/main/runtime/orca-runtime' import { readPersistedTestAgentSessionStore } from '../../src/main/runtime/agent-session/records/agent-session-record-store-test-harness' import { openTestJournalHostDatabase } from '../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' diff --git a/tests/e2e/structured-permission-report-publication.unit.test.ts b/tests/e2e/structured-permission-report-publication.unit.test.ts index 0a8e105cbe8..63aba6ab78f 100644 --- a/tests/e2e/structured-permission-report-publication.unit.test.ts +++ b/tests/e2e/structured-permission-report-publication.unit.test.ts @@ -34,11 +34,11 @@ import { attachParams, envelope, CALLER -} from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-harness' +} from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host.test-fixture' import { HOST_TEST_NOW, HOST_TEST_SESSION as sessionId -} from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data' +} from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture' import { setLocalRuntimeCapabilitiesForTests } from '@/runtime/hosts/capabilities/local-runtime-capabilities' import { AGENT_SESSION_PERMISSION_OWN_RUNTIME_CAPABILITY } from '../../src/shared/agent-session/agent-session-permission-modes-capability' import { resetStructuredAgentSessionStatusFeedsForTests } from '@/runtime/structured-agent-session-status-feed' From 135a6fc25ae64de7b07843a8d29da1882e524e98 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:23:52 -0800 Subject: [PATCH 34/45] Keep warning authority validation on the semantic selection branch --- .../journal-domain-loss-warnings.test.ts | 17 +++++++++++++++++ .../journal-domain-timeline-reads.ts | 15 +++++++++++---- .../agent-session-journal/journal-sql-steps.ts | 10 +++++++--- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts b/src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts index b84377267d9..09e31d60585 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-loss-warnings.test.ts @@ -162,6 +162,23 @@ it('rejects stale direct and nested warning writers before suppressing a matchin expect(result.publication?.items).toEqual([]) } } + const ordinary = await f.run({ + kind: 'steps', + steps: [ + { + kind: 'resolved-item', + reservedBytes: 4096, + options, + resolution: { + kind: 'turn-claim', + identity: { provider: 'orca', clientMessageId: 'ordinary-claim' }, + providerTurnId: 'provider-turn' + } + } + ] + }) + expect(ordinary.value).toMatchObject({ outcomes: [{ kind: 'dropped' }] }) + expect(ordinary.value).not.toHaveProperty('failure') expect(f.item(originalId)).toEqual(original) }) diff --git a/src/main/native-chat/agent-session-journal/journal-domain-timeline-reads.ts b/src/main/native-chat/agent-session-journal/journal-domain-timeline-reads.ts index 6f439998ea8..5fa0b7be170 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-timeline-reads.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-timeline-reads.ts @@ -7,12 +7,14 @@ import { journalDomainStopDecidesTurn } from './journal-domain-item-normalization' import type { JournalStopSettleFact } from './journal-writer-host-facts' +import { assertJournalDomainItemAuthority } from './journal-domain-item' /** Typed named selections used only during the owning synchronous writer command. */ export function journalDomainTimelineReads( db: Database.Database, target: { sessionId: string; epoch: string }, - stopSettle?: JournalStopSettleFact + stopSettle?: JournalStopSettleFact, + itemFence?: number ): JournalTimelineView { const prefix = [target.sessionId, target.epoch] as const const decode = (row: Record | undefined): AgentJournalRenderItem | null => @@ -72,13 +74,18 @@ export function journalDomainTimelineReads( return { item, itemBody: (key) => item(key)?.body ?? null, - hasVisibleProviderItemKey: (key) => - Boolean( + hasVisibleProviderItemKey: (key) => { + if (itemFence === undefined) { + throw new Error('journal warning selection omitted its writer fence') + } + assertJournalDomainItemAuthority(db, target, itemFence) + return Boolean( db .prepare(`SELECT 1 FROM journal_items WHERE session_id=? AND epoch=? AND hidden=0 AND legacy_provider_item_key IS NOT NULL AND legacy_provider_item_key=? LIMIT 1`) .get(...prefix, key) - ), + ) + }, newestTurn: () => decode( db diff --git a/src/main/native-chat/agent-session-journal/journal-sql-steps.ts b/src/main/native-chat/agent-session-journal/journal-sql-steps.ts index e0dde38c61d..df2e4061440 100644 --- a/src/main/native-chat/agent-session-journal/journal-sql-steps.ts +++ b/src/main/native-chat/agent-session-journal/journal-sql-steps.ts @@ -11,7 +11,7 @@ import type { } from './writing/journal-writer-step' import { resolveJournalTimelineItem } from './writing/journal-timeline-resolution' import { resolveProviderTimelineSettlement } from '../agent-session-timeline/provider-timeline-writer-resolution' -import { applyJournalDomainItem, assertJournalDomainItemAuthority } from './journal-domain-item' +import { applyJournalDomainItem } from './journal-domain-item' import { journalDomainTimelineReads } from './journal-domain-timeline-reads' import { applyJournalDomainLifecycle, @@ -100,7 +100,12 @@ function writeStep( savedAt: number ): { commit?: JournalDomainLifecycleCommit; outcome: JournalStepOutcome } { const { target } = command - const view = journalDomainTimelineReads(db, target, command.facts?.stopSettle) + const view = journalDomainTimelineReads( + db, + target, + command.facts?.stopSettle, + step.kind === 'settlement' ? undefined : step.options.fence + ) if (step.kind === 'settlement') { if (hasJournalDomainSettlement(db, target, step.settlementId)) { return { outcome: { kind: 'committed', wrote: false } } @@ -198,7 +203,6 @@ function writeStep( identity = step.identity body = step.body } else if (step.kind === 'resolved-item') { - assertJournalDomainItemAuthority(db, target, step.options.fence) const resolved = resolveJournalTimelineItem( view, step.resolution, From 4ac0bff4eca17a2c752b20ed09ebfb17afd8b00f Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:28:56 -0800 Subject: [PATCH 35/45] test: lose unsaved ACP creations before a successful resume --- .../acp/acp-structured-host-restore-failed.test.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/acp/acp-structured-host-restore-failed.test.ts b/src/main/acp/acp-structured-host-restore-failed.test.ts index a006f3834d0..496597ab921 100644 --- a/src/main/acp/acp-structured-host-restore-failed.test.ts +++ b/src/main/acp/acp-structured-host-restore-failed.test.ts @@ -244,11 +244,6 @@ describe('a replacement after its earlier attach failed to open the journal', () expect(await warnings()).toHaveLength(1) const firstWarning = (await warnings())[0] 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()).toEqual([firstWarning]) - await host.close(SESSION, 'user-close') for (const missing of [FRESH_2, FRESH_3]) { lost.set(missing, { code: -32002, message: 'Resource not found' }) @@ -259,6 +254,11 @@ describe('a replacement after its earlier attach failed to open the journal', () 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, 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' }) From 25261369053c8390bf2715c8513522b5d40882c7 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:35:46 -0800 Subject: [PATCH 36/45] refactor: tighten SQL journal types and publication reads --- .../claude-background-stop-records.test.ts | 2 +- .../codex-structured-thread-catch-up.test.ts | 6 +++-- .../journal-domain-context-replacement.ts | 13 ++++++----- .../journal-domain-control-command.ts | 3 +-- .../journal-domain-history-page.ts | 6 +++-- .../journal-domain-item-reads.test.ts | 1 + .../journal-domain-outline-source.ts | 4 +++- .../journal-sql-appends.ts | 8 +++---- .../journal-sql-command.ts | 2 -- .../journal-sql-lifecycle-reads.ts | 5 ++++- .../journal-sql-open-command.ts | 2 +- .../journal-store-contracts.ts | 4 ++-- .../journal-writer-command.ts | 22 +++++++++---------- .../journal-host-database-test-support.ts | 12 +++++----- .../provider-timeline-text-blocks.ts | 2 +- .../structured-agent-session-create.ts | 3 +-- ...tructured-agent-session-send-settlement.ts | 6 +++-- ...ent-session-restart-failure-filing.test.ts | 6 +++-- ...tructured-agent-session-stop-event.test.ts | 6 +++-- .../structured-conversation-command.test.ts | 10 ++++----- .../agent-session-queued-image-reader.ts | 2 +- .../agent-session-stop-note-refresh.ts | 2 +- .../subscribers/agent-session-work-refresh.ts | 2 +- ...structured-agent-session-event-recovery.ts | 6 +++-- ...tured-agent-session-working-at-teardown.ts | 4 ++-- .../agent-session-store-transactions.ts | 2 ++ .../orchestration-cli-subprocess.test.ts | 4 ++-- .../agent-launch-cwd-resources.test.ts | 6 +++-- 28 files changed, 85 insertions(+), 66 deletions(-) diff --git a/src/main/claude/claude-background-stop-records.test.ts b/src/main/claude/claude-background-stop-records.test.ts index 9fa775a2922..c234dc65979 100644 --- a/src/main/claude/claude-background-stop-records.test.ts +++ b/src/main/claude/claude-background-stop-records.test.ts @@ -57,8 +57,8 @@ async function droppedFromRoster() { fence: 7, // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: admission reads only through readWork. journal: {} as AgentSessionJournal, - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: admission reads only the state answer. readWork: async () => + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: command admission requests only the state answer. ({ kind: 'state', state: IDLE_WORK }) as JournalWorkAnswer, adapter: run.adapter }, diff --git a/src/main/codex/structured/threads/codex-structured-thread-catch-up.test.ts b/src/main/codex/structured/threads/codex-structured-thread-catch-up.test.ts index 98f0f750e9a..68e180647a2 100644 --- a/src/main/codex/structured/threads/codex-structured-thread-catch-up.test.ts +++ b/src/main/codex/structured/threads/codex-structured-thread-catch-up.test.ts @@ -1,4 +1,7 @@ -import { withTestAgentSessionDatabase } from '../../../runtime/agent-session/records/agent-session-record-store-test-harness' +import { + withTestAgentSessionDatabase, + openTestAgentSessionRecordStore +} from '../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { readTestJournalItems, readTestJournalSnapshot @@ -15,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' diff --git a/src/main/native-chat/agent-session-journal/journal-domain-context-replacement.ts b/src/main/native-chat/agent-session-journal/journal-domain-context-replacement.ts index 033cee4b978..6ea068abc60 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-context-replacement.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-context-replacement.ts @@ -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 } } diff --git a/src/main/native-chat/agent-session-journal/journal-domain-control-command.ts b/src/main/native-chat/agent-session-journal/journal-domain-control-command.ts index 6e356158b28..56736cbb462 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-control-command.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-control-command.ts @@ -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, diff --git a/src/main/native-chat/agent-session-journal/journal-domain-history-page.ts b/src/main/native-chat/agent-session-journal/journal-domain-history-page.ts index b2fe154f0d5..e89bf1a0de8 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-history-page.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-history-page.ts @@ -1,5 +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 { + 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, @@ -21,7 +24,6 @@ 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 diff --git a/src/main/native-chat/agent-session-journal/journal-domain-item-reads.test.ts b/src/main/native-chat/agent-session-journal/journal-domain-item-reads.test.ts index f3f097d535c..b1770d88202 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-item-reads.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-item-reads.test.ts @@ -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 } return { db, seed, alias, tombstone, send, read } diff --git a/src/main/native-chat/agent-session-journal/journal-domain-outline-source.ts b/src/main/native-chat/agent-session-journal/journal-domain-outline-source.ts index bfb4a190d10..1e23fd1450c 100644 --- a/src/main/native-chat/agent-session-journal/journal-domain-outline-source.ts +++ b/src/main/native-chat/agent-session-journal/journal-domain-outline-source.ts @@ -45,7 +45,9 @@ function outlineBlocks(blocks: readonly NativeChatBlock[]): NativeChatBlock[] { return [{ type: 'tool-result', output: '', callId: block.callId }] case 'image-ref': return [{ type: 'image-ref' }] - default: + case 'background-task': + case 'subagent': + case 'subagent-group': return [] } }) diff --git a/src/main/native-chat/agent-session-journal/journal-sql-appends.ts b/src/main/native-chat/agent-session-journal/journal-sql-appends.ts index e5b81801d57..b3c5d2a51c1 100644 --- a/src/main/native-chat/agent-session-journal/journal-sql-appends.ts +++ b/src/main/native-chat/agent-session-journal/journal-sql-appends.ts @@ -3,9 +3,8 @@ import type { AgentJournalItemIdentity } from '../../../shared/agent-session/journal/agent-session-journal-identity' import type { AgentJournalMessageItem } from '../../../shared/agent-session/journal/agent-session-journal-types' -import type { JournalStopAcceptance } from './journal-store-contracts' -import type { JournalStopEvent } from './durable-rows/journal-row-schema' import type { + JournalStopAcceptance, JournalAppendResult, JournalLateDispatchInput, JournalShutdownCutInput, @@ -23,8 +22,10 @@ import type { JournalSubmissionConsume, JournalSubmissionInput, JournalTombstoneInput, - ResolveDispatchInput + ResolveDispatchInput, + JournalCommandOpenResult } from './journal-store-contracts' +import type { JournalStopEvent } from './durable-rows/journal-row-schema' import type { JournalSqlReads } from './journal-sql-reads' import type { JournalSqlStoreOperation, JournalSqlStorePrepare } from './journal-sql-store' import type { JournalWriterCommit } from './journal-writer-changes' @@ -32,7 +33,6 @@ import type { JournalOperationReceipt } from './writing/journal-row-writer' import type { JournalWriterStep, JournalStepsResult } from './writing/journal-writer-step' import type { StoppedHistoryDelivery } from '../../../shared/agent-session/lifecycle/agent-session-stopped-history' import type { JournalWriterSettled } from './journal-sql-failure' -import type { JournalCommandOpenResult } from './journal-store-contracts' import { restoreJournalSqlFailure } from './journal-sql-failure' import { claimJournalAutomaticSubmission } from './journal-sql-automatic-submission' import type { JournalAutomaticSelection } from './journal-automatic-submission' diff --git a/src/main/native-chat/agent-session-journal/journal-sql-command.ts b/src/main/native-chat/agent-session-journal/journal-sql-command.ts index 557c0af8d4f..12809746f96 100644 --- a/src/main/native-chat/agent-session-journal/journal-sql-command.ts +++ b/src/main/native-chat/agent-session-journal/journal-sql-command.ts @@ -271,8 +271,6 @@ export class JournalSqlCommand { } case 'steps': return applyJournalSqlStepCommand(db, stateDirectory, { ...command, operation }, savedAt) - default: - throw new Error('Unknown journal command') } } } diff --git a/src/main/native-chat/agent-session-journal/journal-sql-lifecycle-reads.ts b/src/main/native-chat/agent-session-journal/journal-sql-lifecycle-reads.ts index 7b417a5e389..eaf6f66c37b 100644 --- a/src/main/native-chat/agent-session-journal/journal-sql-lifecycle-reads.ts +++ b/src/main/native-chat/agent-session-journal/journal-sql-lifecycle-reads.ts @@ -73,7 +73,10 @@ export function readJournalDomainLifecycle( .prepare(`SELECT payload_json FROM journal_items WHERE session_id=? AND epoch=? AND item_id=? AND hidden=0`) .get(...prefix, agentJournalSubmissionKey(nextClientMessageId)) - const body = next ? (JSON.parse(String(next.payload_json)) as AgentJournalRenderItem).body : null + const nextItem: AgentJournalRenderItem | null = next + ? JSON.parse(String(next.payload_json)) + : null + const body = nextItem?.body return { kind: 'outgrown-child', child: { diff --git a/src/main/native-chat/agent-session-journal/journal-sql-open-command.ts b/src/main/native-chat/agent-session-journal/journal-sql-open-command.ts index e75d3e8f43a..56ac8e87226 100644 --- a/src/main/native-chat/agent-session-journal/journal-sql-open-command.ts +++ b/src/main/native-chat/agent-session-journal/journal-sql-open-command.ts @@ -138,7 +138,7 @@ function settleOpeningLiveWork( db, target, { - settlementId: `open-live-work:${head.last_sequence}`, + settlementId: `open-live-work:${Number(head.last_sequence)}`, fence: Number(head.highest_fence), recovered: true, mutations: revisions.map((revision) => ({ diff --git a/src/main/native-chat/agent-session-journal/journal-store-contracts.ts b/src/main/native-chat/agent-session-journal/journal-store-contracts.ts index d2b32761044..8bbf0ad3465 100644 --- a/src/main/native-chat/agent-session-journal/journal-store-contracts.ts +++ b/src/main/native-chat/agent-session-journal/journal-store-contracts.ts @@ -9,7 +9,8 @@ import type { AgentJournalMessageItem, AgentJournalProducerLinkage, AgentJournalResetReason, - AgentJournalRowAttribution + AgentJournalRowAttribution, + AgentJournalTurnItem } from '../../../shared/agent-session/journal/agent-session-journal-types' import type { AgentJournalCursor, @@ -28,7 +29,6 @@ import type { JournalOperationReceipt, JournalRowWriter } from './writing/journa import type { UsageLimitContinueIdentity } from '../../../shared/agent-session/lifecycle/agent-session-usage-limit-continue' import type Database from '../../sqlite/sync-database' import type { JournalStopEvent } from './durable-rows/journal-row-schema' -import type { AgentJournalTurnItem } from '../../../shared/agent-session/journal/agent-session-journal-types' import type { structuredAgentSessionCommandTurn } from '../../../shared/agent-session/view/structured-agent-session-command-turn-identity' import type { QueuedMessageOwed } from './queued-messages/queued-message-consume-error' import type { StaleStructuredAgentSessionStateInput } from '../agent-session-wire/termination/structured-agent-session-stale-state-settlement' diff --git a/src/main/native-chat/agent-session-journal/journal-writer-command.ts b/src/main/native-chat/agent-session-journal/journal-writer-command.ts index ae80e592add..afd6b1cbafc 100644 --- a/src/main/native-chat/agent-session-journal/journal-writer-command.ts +++ b/src/main/native-chat/agent-session-journal/journal-writer-command.ts @@ -3,9 +3,12 @@ import type { AgentJournalMessageItem } from '../../../shared/agent-session/jour import type { AgentSessionExecutionOptions } from '../../../shared/agent-session/options/agent-session-execution-options' import type { AgentSessionQueuedMessageMoveAnchor } from '../../../shared/agent-session/requests/agent-session-queued-message-wire' import type { AgentSessionProviderContextBoundary } from '../../../shared/agent-session/agent-session-provider-context' -import type { AgentJournalEpochReason, JournalEpochRow } from './durable-rows/journal-row-schema' +import type { + AgentJournalEpochReason, + JournalEpochRow, + JournalStopEvent +} from './durable-rows/journal-row-schema' import type { JournalReplacementItem } from './context-epochs/journal-epoch-replacement' -import type * as ids from '../../../shared/agent-session/journal/agent-session-journal-identity' import type { QueuedMessageHoldReason } from './queued-messages/queued-message-table' import type { AgentJournalDispatchRejection } from '../../../shared/agent-session/failures/agent-session-failure-words' import type { JournalSubmissionSelector } from './journal-submission-selector' @@ -23,18 +26,15 @@ import type { JournalStaleSettlementInput, JournalPromptMutationInput, JournalUnheldTurnsSettlementInput, - JournalStartFailureSettlementInput + JournalStartFailureSettlementInput, + JournalItemAppendInput, + JournalLifecycleBatchInput, + ResolveDispatchInput } from './journal-store-contracts' import type { AgentJournalCursor, AgentJournalItemIdentity } from '../../../shared/agent-session/journal/agent-session-journal-identity' -import type { - JournalItemAppendInput, - JournalLifecycleBatchInput, - ResolveDispatchInput -} from './journal-store-contracts' -import type { JournalStopEvent } from './durable-rows/journal-row-schema' import type { JournalWriterEffect } from './journal-writer-effects' import type { JournalWriterHostFacts } from './journal-writer-host-facts' import type { JournalWriterStep } from './writing/journal-writer-step' @@ -132,10 +132,10 @@ export type JournalWriterInputOperation = /** The host's current-work answer, read on its lane just before this command. */ liveTurnId: string | null } - | { kind: 'goal-withdraw'; identity: ids.AgentJournalItemIdentity; fence: number } + | { kind: 'goal-withdraw'; identity: AgentJournalItemIdentity; fence: number } | { kind: 'item-claiming' - identity: ids.AgentJournalItemIdentity + identity: AgentJournalItemIdentity body: AgentJournalMessageItem options: JournalItemAppendOptions } diff --git a/src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support.ts b/src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support.ts index b9836b4a0c8..662ac540b75 100644 --- a/src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support.ts +++ b/src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support.ts @@ -1,6 +1,11 @@ import { insertJournalDomainQueueRow } from '../journal-domain-queue-rows' import type { QueuedMessageRow } from '../queued-messages/queued-message-table' -import { withTestAgentSessionDatabase } from '../../../runtime/agent-session/records/agent-session-record-store-test-harness' +import { + withTestAgentSessionDatabase, + openTestAgentSessionDatabase, + closeTestAgentSessionDatabase, + closeTestAgentSessionRecordStores +} from '../../../runtime/agent-session/records/agent-session-record-store-test-harness' import type { AgentSessionJournalProviderHandle } from '../../../../shared/agent-session/journal/agent-session-journal-identity' import { resolve } from 'node:path' import { randomUUID } from 'node:crypto' @@ -11,11 +16,6 @@ import { restoreJournal, type JournalLoad } from '../replay/journal-open' import { fields } from '../outcomes/journal-state-schema' import { journalOutcomeSource } from '../outcomes/journal-outcome-format' import { decodeJournalOutcome, type JournalOutcome } from '../outcomes/journal-outcome-codec' -import { - openTestAgentSessionDatabase, - closeTestAgentSessionDatabase, - closeTestAgentSessionRecordStores -} from '../../../runtime/agent-session/records/agent-session-record-store-test-harness' import '../durable-rows/journal-row-schema' import { serializeJournalRow } from '../durable-rows/journal-row-encoding' diff --git a/src/main/native-chat/agent-session-timeline/provider-timeline-text-blocks.ts b/src/main/native-chat/agent-session-timeline/provider-timeline-text-blocks.ts index 87a453d15c6..9c99bf7ddf6 100644 --- a/src/main/native-chat/agent-session-timeline/provider-timeline-text-blocks.ts +++ b/src/main/native-chat/agent-session-timeline/provider-timeline-text-blocks.ts @@ -40,7 +40,7 @@ export function splitProviderTimelineTextBlocks(text: string): { ready: string; } else if ( marker[0] === openFence.marker[0] && marker.length >= openFence.marker.length && - indent <= openFence.indent + 3 && + indent - openFence.indent <= 3 && line.length === indent + marker.length ) { // CommonMark: a closing fence carries no info string. diff --git a/src/main/native-chat/agent-session-wire/acquisition/structured-agent-session-create.ts b/src/main/native-chat/agent-session-wire/acquisition/structured-agent-session-create.ts index 778160898ff..49069bdb119 100644 --- a/src/main/native-chat/agent-session-wire/acquisition/structured-agent-session-create.ts +++ b/src/main/native-chat/agent-session-wire/acquisition/structured-agent-session-create.ts @@ -261,8 +261,7 @@ async function foundConversation( const tabId = store.getSessionTabId(sessionId) const [cursor, firstMessage] = await Promise.all([ session.journal.reads.cursor(), - options.firstMessage && - (await session.journal.reads.submission(options.firstMessage.clientMessageId)) + options.firstMessage && session.journal.reads.submission(options.firstMessage.clientMessageId) ]) const result = { ok: true as const, diff --git a/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-send-settlement.ts b/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-send-settlement.ts index f62311bedcc..563c9f518b6 100644 --- a/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-send-settlement.ts +++ b/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-send-settlement.ts @@ -1,10 +1,12 @@ import { queuedSendAnswer } from './structured-agent-session-queued-send-answer' import type { JournalWorkReader } from '../../agent-session-journal/journal-work-read' -import type { SqlAgentSessionJournal } from '../../agent-session-journal/journal-sql-store' +import type { + SqlAgentSessionJournal, + SqlAgentSessionJournal as AgentSessionJournal +} from '../../agent-session-journal/journal-sql-store' import type { AgentJournalCursor } from '../../../../shared/agent-session/journal/agent-session-journal-identity' import type { AgentSessionSendResult } from '../../../../shared/agent-session/agent-session-wire' import { isQueuedAgentJournalSubmission } from '../../../../shared/agent-session/requests/agent-session-queued-submission' -import type { SqlAgentSessionJournal as AgentSessionJournal } from '../../agent-session-journal/journal-sql-store' import { isStructuredAgentSessionCommandTurnId } from '../operations/structured-agent-session-command-turn' /** Scope active turns to the live generation so ended commands cannot hold delivery waits. */ diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-failure-filing.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-failure-filing.test.ts index f7e9b459405..3d0da25018e 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-failure-filing.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-failure-filing.test.ts @@ -1,5 +1,7 @@ -import { withTestAgentSessionDatabase } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' -import { openTestAgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' +import { + withTestAgentSessionDatabase, + openTestAgentSessionRecordStore +} from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { StructuredAgentSessionHost } from '../structured-agent-session-host' import { afterEach, expect, it, vi } from 'vitest' import { AgentSessionRecoveryCapsule } from '../../../../runtime/agent-session/records/agent-session-recovery-capsule' diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event.test.ts index 00b0bbc4569..0d1091275f6 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-stop-event.test.ts @@ -1,5 +1,7 @@ -import { readTestJournalSnapshot } from '../../../agent-session-journal/journal-observation.test-support' -import { readTestJournalItems } from '../../../agent-session-journal/journal-observation.test-support' +import { + readTestJournalSnapshot, + readTestJournalItems +} from '../../../agent-session-journal/journal-observation.test-support' import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../../../shared/agent-session/journal/agent-session-journal-types' import { agentJournalSubmissionKey } from '../../../../../shared/agent-session/journal/agent-session-journal-item-key' diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-conversation-command.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-conversation-command.test.ts index 061d3d5624f..bd951506c47 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-conversation-command.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-conversation-command.test.ts @@ -1,4 +1,8 @@ -import { withTestAgentSessionDatabase } from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' +import { + withTestAgentSessionDatabase, + openTestAgentSessionRecordStore, + openTestAgentSessionDatabase +} from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { readTestHostJournal } from '../../../agent-session-journal/journal-observation.test-support' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' @@ -8,10 +12,6 @@ import { computeAgentSessionPayloadFingerprint } from '../../../../../shared/age import type { AgentSessionConversationCommand } from '../../../../../shared/agent-session/agent-session-conversation-command' import type { AgentSessionOwnerProbe } from '../../../../../shared/agent-session/lifecycle/agent-session-lease-adjudication' import type { AgentSessionRecordStore } from '../../../../runtime/agent-session/records/agent-session-record-store' -import { - openTestAgentSessionRecordStore, - openTestAgentSessionDatabase -} from '../../../../runtime/agent-session/records/agent-session-record-store-test-harness' import { StructuredAgentSessionHost } from '../structured-agent-session-host' import { claudeAndCodexDeclared } from '../../providers/testing/structured-agent-session-adapter-router.test-fixture' import type { StructuredAgentSessionAdapter } from '../../providers/structured-agent-session-adapter' diff --git a/src/main/native-chat/agent-session-wire/publication/history/agent-session-queued-image-reader.ts b/src/main/native-chat/agent-session-wire/publication/history/agent-session-queued-image-reader.ts index 09263c8b333..4bbe2670bd8 100644 --- a/src/main/native-chat/agent-session-wire/publication/history/agent-session-queued-image-reader.ts +++ b/src/main/native-chat/agent-session-wire/publication/history/agent-session-queued-image-reader.ts @@ -258,7 +258,7 @@ export async function queuedImageReaderEvent( } return { ...event, - ...(work ? await agentSessionPublishedWorkFields(journal, work) : {}), + ...(work ? agentSessionPublishedWorkFields(journal, work) : {}), batch: { ...event.batch, items: [...items.values()].sort(compareAgentJournalItems), diff --git a/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-stop-note-refresh.ts b/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-stop-note-refresh.ts index 61571484b2d..3eff65e6bd6 100644 --- a/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-stop-note-refresh.ts +++ b/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-stop-note-refresh.ts @@ -23,7 +23,7 @@ export async function refreshDerivedStopNotes( work?: AgentSessionPublishedWork ): Promise { const { form, budgetBytes } = subscriberHistoryReader(subscriber) - const fields = work ? await agentSessionPublishedWorkFields(journal, work) : {} + const fields = work ? agentSessionPublishedWorkFields(journal, work) : {} let pending: AgentJournalRenderItem[] = [] let bytes = 0 const emit = async (): Promise => { diff --git a/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-work-refresh.ts b/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-work-refresh.ts index 14f2bb3de98..0c70437cb79 100644 --- a/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-work-refresh.ts +++ b/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-work-refresh.ts @@ -25,7 +25,7 @@ export async function refreshPublishedWork( return } const view = work.workViewKey() - const fields = await agentSessionPublishedWorkFields(journal, work) + const fields = agentSessionPublishedWorkFields(journal, work) const { budgetBytes } = subscriberHistoryReader(subscriber) let pending: AgentJournalSubmission[] = [] let bytes = 0 diff --git a/src/main/native-chat/agent-session-wire/termination/structured-agent-session-event-recovery.ts b/src/main/native-chat/agent-session-wire/termination/structured-agent-session-event-recovery.ts index 1c8d8d371d3..4ef116f7281 100644 --- a/src/main/native-chat/agent-session-wire/termination/structured-agent-session-event-recovery.ts +++ b/src/main/native-chat/agent-session-wire/termination/structured-agent-session-event-recovery.ts @@ -2,7 +2,10 @@ import { agentSessionFailureFact } from '../../../../shared/agent-session/failur import type { StructuredAgentSessionLifecycleEvent } from '../providers/structured-agent-session-adapter' import type { JournalWriterSettled } from '../../agent-session-journal/journal-sql-failure' import { restoreJournalSqlFailure } from '../../agent-session-journal/journal-sql-failure' -import { structuredAgentSessionHandingOver } from '../delivery/structured-agent-session-handing-over' +import { + structuredAgentSessionHandingOver, + finalizingStructuredAgentSessionInput +} from '../delivery/structured-agent-session-handing-over' import { stopAgentSessionProviderRoot } from './structured-agent-session-provider-exit-proof' import type { StructuredAgentSessionHostDeps, @@ -181,4 +184,3 @@ export class StructuredAgentSessionEventRecovery { await settleStructuredAgentSessionChildExit(this.exitContext, event) } } -import { finalizingStructuredAgentSessionInput } from '../delivery/structured-agent-session-handing-over' diff --git a/src/main/native-chat/agent-session-wire/termination/structured-agent-session-working-at-teardown.ts b/src/main/native-chat/agent-session-wire/termination/structured-agent-session-working-at-teardown.ts index 01320633946..4d67f374f7f 100644 --- a/src/main/native-chat/agent-session-wire/termination/structured-agent-session-working-at-teardown.ts +++ b/src/main/native-chat/agent-session-wire/termination/structured-agent-session-working-at-teardown.ts @@ -34,7 +34,8 @@ import { } from '../../../../shared/agent-session/structured-agent-session-command-entry' import { activeStructuredAgentSessionTurnId, - newestStructuredAgentSessionTurn + newestStructuredAgentSessionTurn, + runningStructuredAgentSessionTurnItemBySequence } from '../../../../shared/agent-session/view/structured-agent-session-live-turn' import type { SqlAgentSessionJournal as AgentSessionJournal } from '../../agent-session-journal/journal-sql-store' import { isUnansweredHandedOverSubmission } from '../../agent-session-journal/queued-messages/journal-unsent-send-hold' @@ -45,7 +46,6 @@ import { continuingStructuredAgentSessionInput, finalizingStructuredAgentSessionInput } from '../delivery/structured-agent-session-handing-over' -import { runningStructuredAgentSessionTurnItemBySequence } from '../../../../shared/agent-session/view/structured-agent-session-live-turn' import type { StructuredAgentSessionWorkScope } from '../../../../shared/agent-session/view/structured-agent-session-main-agent-working' import type { StructuredAgentSessionProviderChild } from '../host/structured-agent-session-host-types' diff --git a/src/main/runtime/agent-session/records/agent-session-store-transactions.ts b/src/main/runtime/agent-session/records/agent-session-store-transactions.ts index 16752f03b23..0609ab517b4 100644 --- a/src/main/runtime/agent-session/records/agent-session-store-transactions.ts +++ b/src/main/runtime/agent-session/records/agent-session-store-transactions.ts @@ -138,6 +138,7 @@ export class AgentSessionStoreTransactions { } } this.publish(next, this.published, agentSessionStoreRowChanges(this.published, next)) + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: executeMutation preserves the command/result correlation enforced by its typed public entry points. return result as T } if (this.readOnly) { @@ -156,6 +157,7 @@ export class AgentSessionStoreTransactions { } ]) const [store] = this.publishResults([result]) + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the store effect returns the result of this exact typed mutation. return store.value as T }) this.queue = run.catch(() => {}) diff --git a/src/main/runtime/orchestration-cli-subprocess.test.ts b/src/main/runtime/orchestration-cli-subprocess.test.ts index 8c4a75e3767..c97e1f3589d 100644 --- a/src/main/runtime/orchestration-cli-subprocess.test.ts +++ b/src/main/runtime/orchestration-cli-subprocess.test.ts @@ -1,5 +1,4 @@ // Exercise compiled CLI keepalives over pipes; skip until built (the verification gate builds it). -import { withTestAgentSessionDatabase } from './agent-session/records/agent-session-record-store-test-harness' import { spawn } from 'node:child_process' import { existsSync, mkdtempSync, rmSync } from 'node:fs' import { tmpdir } from 'node:os' @@ -31,7 +30,8 @@ import { closeTestJournalHostDatabase } from '../native-chat/agent-session-journ import { openTestAgentSessionRecordStore, readPersistedTestAgentSessionStoreText, - openTestAgentSessionDatabase + openTestAgentSessionDatabase, + withTestAgentSessionDatabase } from './agent-session/records/agent-session-record-store-test-harness' import { idOf, isRecord, resultOf } from './rpc/orchestration-session-caller-test-fixture' diff --git a/src/main/runtime/rpc/methods/agent-launch/agent-launch-cwd-resources.test.ts b/src/main/runtime/rpc/methods/agent-launch/agent-launch-cwd-resources.test.ts index 848aabec653..b56402f5a8a 100644 --- a/src/main/runtime/rpc/methods/agent-launch/agent-launch-cwd-resources.test.ts +++ b/src/main/runtime/rpc/methods/agent-launch/agent-launch-cwd-resources.test.ts @@ -1,6 +1,9 @@ import type * as RuntimeEnvironmentStore from '../../../../../shared/remote-runtime/host-store/runtime-environment-store' import '../../unused-default-rpc-methods.test-fixture' -import { withTestAgentSessionDatabase } from '../../../agent-session/records/agent-session-record-store-test-harness' +import { + withTestAgentSessionDatabase, + openTestAgentSessionRecordStore +} from '../../../agent-session/records/agent-session-record-store-test-harness' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' const { @@ -85,7 +88,6 @@ import { store as runtimeStore, TEST_WORKTREE_ID } from '../../../orca-runtime-tests/orca-runtime-test-fixtures.spec' -import { openTestAgentSessionRecordStore } from '../../../agent-session/records/agent-session-record-store-test-harness' import { closeTestJournalHostDatabase } from '../../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import { runtimeStub, setAgentLaunchRecordStore } from './agent-launch.test-fixture' import { RuntimeWorktreeLineageController } from '../../../workspaces/lineage/runtime-worktree-lineage-controller' From d22c3b0c7773bd63f14f2ed3fe5f7ace817db741 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:37:04 -0800 Subject: [PATCH 37/45] test: use SQL chat writers and async journal observations --- .../ai-vault-manual-name-clear.unit.test.ts | 13 +- .../automation-terminal-launch.unit.test.ts | 2 +- ...x-child-approval-activity-row.unit.test.ts | 3 +- .../crew-recipient-real-command.unit.test.ts | 17 +- .../automation-chat-reopen-test-harness.ts | 5 +- .../automation-chat-reuse-test-harness.ts | 5 +- .../journal-restored-consumers.unit.test.ts | 90 +-- .../native-chat-rewind-boundary.unit.test.ts | 26 +- ...e-chat-startup-option-pickers.unit.test.ts | 3 +- ...ured-agent-session-read-owner.unit.test.ts | 55 +- ...attention-read-retirement.test-fixture.tsx | 2 +- ...red-attention-read-retirement.unit.test.ts | 2 +- ...ctured-chat-tab-lifetime-cost.unit.test.ts | 689 ------------------ ...d-session-workspace-authority.unit.test.ts | 5 +- 14 files changed, 108 insertions(+), 809 deletions(-) delete mode 100644 tests/e2e/structured-chat-tab-lifetime-cost.unit.test.ts diff --git a/tests/e2e/ai-vault-manual-name-clear.unit.test.ts b/tests/e2e/ai-vault-manual-name-clear.unit.test.ts index 8f6d717dee2..88c2532e266 100644 --- a/tests/e2e/ai-vault-manual-name-clear.unit.test.ts +++ b/tests/e2e/ai-vault-manual-name-clear.unit.test.ts @@ -22,17 +22,15 @@ import { } from '../../src/main/ai-vault/structured-session-ownership' import { openTestAgentSessionRecordStore, + openTestAgentSessionDatabase, seedTestAgentSessionRecordStore, readPersistedTestAgentSessionStore } from '../../src/main/runtime/agent-session/records/agent-session-record-store-test-harness' import { AgentSessionJournal } from '../../src/main/native-chat/agent-session-journal/journal-store' -import { - openTestJournalHostDatabase, - closeTestJournalHostDatabases -} from '../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' +import { closeTestJournalHostDatabases } from '../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import { StructuredAgentSessionHost } from '../../src/main/native-chat/agent-session-wire/host/structured-agent-session-host' import { setStructuredAgentSessionHost } from '../../src/main/native-chat/agent-session-wire/host/structured-agent-session-registry' -import { structuredAgentsDeclaringNothing } from '../../src/main/native-chat/agent-session-wire/providers/testing/structured-agent-session-adapter-router-test-support' +import { structuredAgentsDeclaringNothing } from '../../src/main/native-chat/agent-session-wire/providers/testing/structured-agent-session-adapter-router.test-fixture' import { recordingStructuredAgentSessionLogger } from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-logger.test-fixture' import { createStructuredChatNamingHandler } from '../../src/main/native-chat/structured-chat-naming' import { PI_RPC_AGENT } from '../../src/main/pi/rpc-agent-definition' @@ -133,8 +131,8 @@ it('detaches a mounted provider History row after clear without losing the nativ } await seedTestAgentSessionRecordStore(directory, { records: [record] }) const store = await openTestAgentSessionRecordStore(directory) - const database = openTestJournalHostDatabase(directory) - const journal = new AgentSessionJournal({ + const database = await openTestAgentSessionDatabase(directory) + const journal = await AgentSessionJournal.open({ database, identity: { sessionId: SESSION, @@ -144,7 +142,6 @@ it('detaches a mounted provider History row after clear without losing the nativ providerHandle: null } }) - await journal.open() await journal.appendItem( { provider: 'legacy', agent: 'pi', sessionId: FILE, recordId: 'completed-reply' }, { kind: 'message', role: 'assistant', blocks: [{ type: 'text', text: 'Inventory checked' }] }, diff --git a/tests/e2e/automation-terminal-launch.unit.test.ts b/tests/e2e/automation-terminal-launch.unit.test.ts index 83af30d33ac..81fd0d1fb6d 100644 --- a/tests/e2e/automation-terminal-launch.unit.test.ts +++ b/tests/e2e/automation-terminal-launch.unit.test.ts @@ -551,7 +551,7 @@ it.each(['before-admission', 'accepted-reply', 'accepted-lost-reply'] as const)( ) } const q = automationRunOperationId(current, 'reuse-send') - expect(rig.store.readCommandReceipt({ kind: 'global' }, q).verdict).toBe('absent') + expect((await rig.store.readCommandReceipt({ kind: 'global' }, q)).verdict).toBe('absent') expect(readAutomationRunReceipt(rig.store, current)).toBeNull() expect( rig.store.getOperationRow(rpcCallerOperationKey(AUTOMATION_AGENT_CALLER), q)?.outcome.status diff --git a/tests/e2e/codex-child-approval-activity-row.unit.test.ts b/tests/e2e/codex-child-approval-activity-row.unit.test.ts index 0b46aaaa27f..111ab4685b0 100644 --- a/tests/e2e/codex-child-approval-activity-row.unit.test.ts +++ b/tests/e2e/codex-child-approval-activity-row.unit.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalItems } from '../../src/main/native-chat/agent-session-journal/journal-observation.test-support' // @vitest-environment happy-dom // Round-trip subagent approval through the real translator/journal/status/activity pipeline; answering must preserve done time and read state. @@ -187,7 +188,7 @@ async function openHost() { /** What the host's answer path commits before it tells Codex. */ const answer = async (itemId: string): Promise => { const identity = parseAgentJournalItemKey(itemId) - const asked = journal.snapshot().items.find((item) => item.itemId === itemId)?.body + const asked = (await readTestJournalItems(journal)).find((item) => item.itemId === itemId)?.body if (!identity || asked?.kind !== 'approval') { throw new Error(`approval ${itemId} missing`) } diff --git a/tests/e2e/crew-recipient-real-command.unit.test.ts b/tests/e2e/crew-recipient-real-command.unit.test.ts index 2825c0a9a60..8305069b544 100644 --- a/tests/e2e/crew-recipient-real-command.unit.test.ts +++ b/tests/e2e/crew-recipient-real-command.unit.test.ts @@ -8,8 +8,11 @@ import { randomUUID } from 'node:crypto' import { RpcDispatcher } from '../../src/main/runtime/rpc/dispatcher' import { ORCHESTRATION_METHODS } from '../../src/main/runtime/rpc/methods/orchestration/orchestration' import { TERMINAL_QUERY_METHODS } from '../../src/main/runtime/rpc/methods/terminal/terminal-query-methods' -import { JournalHostDatabase } from '../../src/main/native-chat/agent-session-journal/database/journal-host-database' -import { AgentSessionRecordStore } from '../../src/main/runtime/agent-session/records/agent-session-record-store' +import { + openTestAgentSessionDatabase, + openTestAgentSessionRecordStore +} from '../../src/main/runtime/agent-session/records/agent-session-record-store-test-harness' +import type { AgentSessionRecordStore } from '../../src/main/runtime/agent-session/records/agent-session-record-store' import { peekOpenedAgentSessionRecordStore, retireAgentSessionRecordStore @@ -93,12 +96,8 @@ it('keeps imported and local peer authority through real CLI import, settlement, h.db.db.prepare('SELECT * FROM federation_relay_items ORDER BY direction, sequence').all() const directory = mkdtempSync(join(tmpdir(), 'orca-pr34890-real-command-')) vi.spyOn(profileStorage, 'getProfileUserDataPath').mockReturnValue(directory) - const homeJournal = JournalHostDatabase.open(join(directory, 'home-journal')) - const homeStore = AgentSessionRecordStore.open({ - journalDatabase: homeJournal, - hostId: 'local', - legacySessionTabIds: [] - }) + const homeJournal = await openTestAgentSessionDatabase(join(directory, 'home-journal')) + const homeStore = await openTestAgentSessionRecordStore(join(directory, 'home-journal')) const localStore = await worker.runtime.openAgentSessionRecordStore() const localId = testOrcaSessionId(randomUUID()) const localAddress = `orca_session_id:${localId}` @@ -555,7 +554,7 @@ it('keeps imported and local peer authority through real CLI import, settlement, if (pendingDone) { await pendingDone } - homeJournal.close() + await homeJournal.close() await retireAgentSessionRecordStore(async (opened) => { await opened?.journalDatabase.close() }) diff --git a/tests/e2e/helpers/automation-chat-reopen-test-harness.ts b/tests/e2e/helpers/automation-chat-reopen-test-harness.ts index 2eba27ec5fa..d6642cec916 100644 --- a/tests/e2e/helpers/automation-chat-reopen-test-harness.ts +++ b/tests/e2e/helpers/automation-chat-reopen-test-harness.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../src/main/native-chat/agent-session-journal/journal-observation.test-support' import { createChatRendererBridge } from './automation-chat-renderer-test-harness' import { expect, vi } from 'vitest' import type { RuntimeClientTarget } from '../../../src/shared/runtime-client-target' @@ -110,7 +111,7 @@ export async function createReopenRig( capturedAt: Date.now() } }) - const beforeJournal = await rig.host.journalSnapshot(CHAT) + const beforeJournal = await readTestHostJournal(rig.host, CHAT) const beforeRecord = rig.store.getConversation(CHAT) const send = vi.spyOn(rig.host, 'send'), create = vi.spyOn(rig.host, 'create') @@ -138,7 +139,7 @@ export async function createReopenRig( } const unchanged = async () => { expect(rig.profile.listAutomationRuns(automation.id)).toEqual([run]) - expect(await rig.host.journalSnapshot(CHAT)).toEqual(beforeJournal) + expect(await readTestHostJournal(rig.host, CHAT)).toEqual(beforeJournal) expect(rig.store.getConversation(CHAT)).toEqual(beforeRecord) expect(send).not.toHaveBeenCalled() expect(create).not.toHaveBeenCalled() diff --git a/tests/e2e/helpers/automation-chat-reuse-test-harness.ts b/tests/e2e/helpers/automation-chat-reuse-test-harness.ts index b0f4524cba0..b5626bfdf21 100644 --- a/tests/e2e/helpers/automation-chat-reuse-test-harness.ts +++ b/tests/e2e/helpers/automation-chat-reuse-test-harness.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../../src/main/native-chat/agent-session-journal/journal-observation.test-support' import { ChatOpenTestRuntime, exerciseRecordedChatOpen, @@ -253,7 +254,7 @@ export async function exerciseBusyChatReuse( }) await eventually(() => expect(readCurrent().usage).not.toBeNull()) const beforeOpen = { - journal: await rig.host.journalSnapshot(sessionId), + journal: await readTestHostJournal(rig.host, sessionId), runs: disk.listAutomationRuns(saved.id), record: rig.store.getConversation(sessionId) } @@ -263,7 +264,7 @@ export async function exerciseBusyChatReuse( vi.spyOn(openingRuntime, 'openAgentSessionRecordStore').mockResolvedValue(rig.store) await exerciseRecordedChatOpen(openingRuntime, readCurrent(), controls.bindRenderer) expect(rig.host.isSessionTabVisible(sessionId)).toBe(true) - expect(await rig.host.journalSnapshot(sessionId)).toEqual(beforeOpen.journal) + expect(await readTestHostJournal(rig.host, sessionId)).toEqual(beforeOpen.journal) expect(disk.listAutomationRuns(saved.id)).toEqual(beforeOpen.runs) expect(rig.store.getConversation(sessionId)).toEqual(beforeOpen.record) expect(create).not.toHaveBeenCalled() diff --git a/tests/e2e/journal-restored-consumers.unit.test.ts b/tests/e2e/journal-restored-consumers.unit.test.ts index 103d4429c36..aed1a6ddf59 100644 --- a/tests/e2e/journal-restored-consumers.unit.test.ts +++ b/tests/e2e/journal-restored-consumers.unit.test.ts @@ -7,7 +7,6 @@ import { cleanup, screen } from '@testing-library/react' import { afterAll, afterEach, beforeAll, expect, it, vi } from 'vitest' import { AGENT_JOURNAL_THREAD_SCOPE, - AGENT_SESSION_JOURNAL_SCHEMA_VERSION, type AgentJournalMessageItem } from '../../src/shared/agent-session/journal/agent-session-journal-types' import type { AgentJournalCursor } from '../../src/shared/agent-session/journal/agent-session-journal-identity' @@ -35,11 +34,7 @@ import { type StructuredAgentSessionState } from '../../src/shared/agent-session/structured-agent-session-reducer' import { selectStructuredAgentSettledTurns } from '../../src/shared/agent-session/view/structured-agent-session-turn-timing' -import { openTestJournalHostDatabase } from '../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' -import { restoreJournal } from '../../src/main/native-chat/agent-session-journal/replay/journal-open' -import { journalOutcomeSource } from '../../src/main/native-chat/agent-session-journal/outcomes/journal-outcome-format' -import { prepareJournalOutcomeWrite } from '../../src/main/native-chat/agent-session-journal/outcomes/journal-outcome-write' -import type { JournalRow } from '../../src/main/native-chat/agent-session-journal/durable-rows/journal-row-schema' +import { AgentSessionJournal } from '../../src/main/native-chat/agent-session-journal/journal-store' import { hostTestAttachParams, HOST_TEST_SESSION as SESSION @@ -48,9 +43,9 @@ import { seedQueuedRigLegacyJournal } from '../../src/main/native-chat/agent-ses import { createRestTestRig, REST_TEST_CALLER -} from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rest-test-rig' +} from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rest-test-rig.test-fixture' -import type { RestTestRig } from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rest-test-rig' +import type { RestTestRig } from '../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rest-test-rig.test-fixture' import { setStructuredAgentSessionHost } from '../../src/main/native-chat/agent-session-wire/host/structured-agent-session-registry' import { OrcaRuntimeService } from '../../src/main/runtime/orca-runtime' import { RpcDispatcher } from '../../src/main/runtime/rpc/dispatcher' @@ -103,9 +98,8 @@ const clients = [ ['current paired desktop', currentDesktop, false, true], ['legacy paired desktop', STRUCTURED_CLIENT, false, false] ] as const -// A new chat holds only records; an existing (mixed) chat holds old input rows up to the restart -// and records after its marker, so catch-up from a cursor inside the old rows crosses into records. -const cases = (['new', 'mixed'] as const).flatMap((kind) => +// Cover a converted source and a new chat through the same runtime read path. +const cases = (['new', 'converted'] as const).flatMap((kind) => clients.map( ([name, client, bounded, snapshotCatchUp]) => [kind, name, client, bounded, snapshotCatchUp] as const @@ -207,7 +201,7 @@ it.each(cases)( const active = await createRestTestRig({ idleSweep: { intervalMs: MAX_TIMER_DELAY_MS } }) rig = active setStructuredAgentSessionHost(active.host) - const pinInputs = kind === 'mixed' ? seedQueuedRigLegacyJournal(active.root, SESSION) : null + const pinInputs = kind === 'converted' ? seedQueuedRigLegacyJournal(active.root, SESSION) : null const attached = await active.host.attach(REST_TEST_CALLER, hostTestAttachParams(null)) if (!attached.ok) { throw new Error('Fixture attach refused') @@ -282,8 +276,8 @@ it.each(cases)( ) } // The mixed reader resumes from here, inside the old rows. - const crossing = kind === 'mixed' ? await history({ direction: 'tail' }) : null - const { response, turnScope } = await completedTurn('current', PROMPT, STALE) + const crossing = kind === 'converted' ? await history({ direction: 'tail' }) : null + const { turnScope } = await completedTurn('current', PROMPT, STALE) const disposable = await journal.appendItem( { provider: 'orca', clientMessageId: 'disposable' }, { kind: 'status', text: REMOVED }, @@ -301,52 +295,30 @@ it.each(cases)( } await active.host.close(SESSION, 'evict') expect(active.host.hasSession(SESSION)).toBe(false) - const database = openTestJournalHostDatabase(active.root) - const saved = restoreJournal(database.db, SESSION)?.state - if (!saved) { - throw new Error('Missing usable projection before tail append') - } - const prefix = { epoch: saved.epoch, sequence: saved.lastSequence } + const stored = await AgentSessionJournal.open({ + database: active.host.deps.journalDatabase, + identity: journal.identity, + now: () => active.clock.now + }) + const prefix = stored.cursor() expect(prefix.sequence).toBeGreaterThanOrEqual(resumeCursor.sequence) - database.transaction( - (db) => { - const rows: JournalRow[] = [] - for (let offset = 1; offset <= AGENT_SESSION_HISTORY_MAX_LIMIT; offset += 1) { - rows.push({ - v: AGENT_SESSION_JOURNAL_SCHEMA_VERSION, - epoch: prefix.epoch, - seq: prefix.sequence + offset, - fence: attached.fence, - ts: active.clock.now + offset, - kind: 'item', - itemId: response.itemId, - revision: response.revision + offset, - body: message( - offset === AGENT_SESSION_HISTORY_MAX_LIMIT ? ANSWER : `Answer revision ${offset}` - ), - turnScope - }) - } - rows.push({ - v: AGENT_SESSION_JOURNAL_SCHEMA_VERSION, - epoch: prefix.epoch, - seq: prefix.sequence + AGENT_SESSION_HISTORY_MAX_LIMIT + 1, - fence: attached.fence, - ts: active.clock.now + AGENT_SESSION_HISTORY_MAX_LIMIT + 1, - kind: 'tombstone', - itemId: disposable.itemId, - revision: disposable.revision + 1 - }) - return prepareJournalOutcomeWrite(db, saved, rows, active.clock.now) - }, - (result) => { - result.adopt() + try { + for (let offset = 1; offset <= AGENT_SESSION_HISTORY_MAX_LIMIT; offset += 1) { + await stored.appendItem( + { provider: 'orca', clientMessageId: 'current-answer' }, + message( + offset === AGENT_SESSION_HISTORY_MAX_LIMIT ? ANSWER : `Answer revision ${offset}` + ), + { ...options, turnScope } + ) } - ) - // Anti-vacuous: the mixed chat's rows end at the restart and its records follow the marker. - expect(journalOutcomeSource(database.db, SESSION, prefix.epoch)?.prefix).toBe( - kind === 'mixed' ? prefix.sequence : 0 - ) + await stored.appendTombstone( + { provider: 'orca', clientMessageId: 'disposable' }, + { fence: attached.fence } + ) + } finally { + await stored.close() + } const restoredPage = await history({ direction: 'tail' }) expect(restoredPage.liveCursor?.sequence).toBeGreaterThan(prefix.sequence) expect(active.adapter.acquire).toHaveBeenCalledTimes(1) @@ -387,7 +359,7 @@ it.each(cases)( const resumed = await stream(resumeCursor) if (snapshotCatchUp) { expect(resumed.events.some((event) => event.type === 'snapshot')).toBe(true) - } else if (kind === 'mixed') { + } else if (kind === 'converted') { expect(resumed.events.every((event) => event.type === 'batch')).toBe(true) // Old rows after the cursor, then records after the marker. const batches = resumed.events.flatMap((event) => diff --git a/tests/e2e/native-chat-rewind-boundary.unit.test.ts b/tests/e2e/native-chat-rewind-boundary.unit.test.ts index 1fee7c8c1ab..a90341f73cd 100644 --- a/tests/e2e/native-chat-rewind-boundary.unit.test.ts +++ b/tests/e2e/native-chat-rewind-boundary.unit.test.ts @@ -1,3 +1,7 @@ +import { + readTestJournalSnapshot, + readTestJournalItems +} from '../../src/main/native-chat/agent-session-journal/journal-observation.test-support' // @vitest-environment happy-dom import { renderHook } from '@testing-library/react' import { mkdtemp, rm } from 'node:fs/promises' @@ -46,9 +50,8 @@ it('offers a recovered opener only when its saved native alias arrives, retainin fence: 1 }) await journals.closeAll() - const recovered = await journals.open(options) - await recovered.markPendingSubmissionsUnknown(2) - const snapshot = recovered.snapshot() + const recovered = await journals.open({ ...options, recover: { fence: 2 } }) + const snapshot = await readTestJournalSnapshot(recovered) const target = agentJournalSubmissionKey('lost') expect(snapshot.submissions.find((row) => row.clientMessageId === 'lost')).toMatchObject({ dispatchState: 'unknown', @@ -74,26 +77,33 @@ it('offers a recovered opener only when its saved native alias arrives, retainin expect(view.result.current.surface?.eligibleItemIds).not.toContain(target) const cursor = recovered.cursor() expect( - structuredRewindCut(recovered, { itemId: target, provider: 'claude', nativeId: 'forked' }) + await structuredRewindCut(recovered, { + itemId: target, + provider: 'claude', + nativeId: 'forked' + }) ).toBeNull() expect(recovered.cursor()).toEqual(cursor) view.rerender({ ...props, state: { ...state, submissions: [] } }) expect(view.result.current.surface?.eligibleItemIds).toContain(target) - await recovered.acceptLate({ + await recovered.settleLateDispatch({ clientMessageId: 'lost', state: 'accepted', providerIdentity: native('genuine'), fence: 2 }) - view.rerender({ ...props, state: { ...state, submissions: recovered.snapshot().submissions } }) + view.rerender({ + ...props, + state: { ...state, submissions: (await readTestJournalSnapshot(recovered)).submissions } + }) expect(view.result.current.surface?.eligibleItemIds).toContain(target) - const cut = structuredRewindCut(recovered, { + const cut = await structuredRewindCut(recovered, { itemId: target, provider: 'claude', nativeId: 'forked' }) expect(cut).toMatchObject({ beforeMessage: { sessionId: 'original', uuid: 'genuine' } }) - expect(cut?.kept.map(({ identity }) => agentJournalItemKey(identity))).toEqual([ + expect((await readTestJournalItems(recovered)).map(({ itemId }) => itemId)).toEqual([ agentJournalItemKey(native('earlier')), agentJournalItemKey(native('reply')) ]) diff --git a/tests/e2e/native-chat-startup-option-pickers.unit.test.ts b/tests/e2e/native-chat-startup-option-pickers.unit.test.ts index 546ac155cba..c0c4e7c7850 100644 --- a/tests/e2e/native-chat-startup-option-pickers.unit.test.ts +++ b/tests/e2e/native-chat-startup-option-pickers.unit.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../src/main/native-chat/agent-session-journal/journal-observation.test-support' // @vitest-environment happy-dom import { cleanup, fireEvent, render, screen } from '@testing-library/react' @@ -163,7 +164,7 @@ it('picks model then effort through the menus during startup and uses both in th child.answerInit() await Promise.all(picked) await vi.waitFor(async () => { - const reply = (await host.journalSnapshot(SESSION)).items.find( + const reply = (await readTestHostJournal(host, SESSION)).items.find( (item) => item.body.kind === 'message' && item.body.role === 'assistant' ) expect(reply?.body).toMatchObject({ diff --git a/tests/e2e/structured-agent-session-read-owner.unit.test.ts b/tests/e2e/structured-agent-session-read-owner.unit.test.ts index f49dff187dc..2eaf4f387a1 100644 --- a/tests/e2e/structured-agent-session-read-owner.unit.test.ts +++ b/tests/e2e/structured-agent-session-read-owner.unit.test.ts @@ -1,3 +1,4 @@ +import { readTestJournalSubmissions } from '../../src/main/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' @@ -78,7 +79,7 @@ async function fixture() { }) expect(journal.cursor().sequence).toBe(100) const initial = structuredClone( - readAgentSessionHistory(journal, { sessionId: SESSION, direction: 'tail' }) + await readAgentSessionHistory(journal, { sessionId: SESSION, direction: 'tail' }) ) const accept = () => journal.resolveDispatch({ @@ -102,11 +103,13 @@ describe('structured session cursor/body regression', () => { for (let index = 102; index <= 140; index += 1) { await appendOutput(index) } - const bounded = readAgentSessionHistory(journal, { - sessionId: SESSION, - direction: 'tail', - limit: 1 - }).page + const bounded = ( + await readAgentSessionHistory(journal, { + sessionId: SESSION, + direction: 'tail', + limit: 1 + }) + ).page expect(bounded.liveCursor?.sequence).toBe(140) expect(bounded.items).not.toContainEqual(retained.items.at(-1)) expect(bounded.submissions).toEqual([]) @@ -155,23 +158,23 @@ describe('structured session cursor/body regression', () => { }) const delayedOlder = Promise.withResolvers() let warm = false - mocks.call.mockImplementation((_target, _method, request: AgentSessionHistoryRequest) => { - // Hold the measured bounded page before its asynchronous older-page fill can mask it. - if (warm && missedRows === 40 && request.direction === 'before') { - return delayedOlder.promise - } - const result = readAgentSessionHistory(journal, { - ...request, - ...(warm && missedRows === 40 ? { limit: 1 } : {}) - }) - return Promise.resolve( - structuredClone({ + mocks.call.mockImplementation( + async (_target, _method, request: AgentSessionHistoryRequest) => { + // Hold the measured bounded page before its asynchronous older-page fill can mask it. + if (warm && missedRows === 40 && request.direction === 'before') { + return delayedOlder.promise + } + const result = await readAgentSessionHistory(journal, { + ...request, + ...(warm && missedRows === 40 ? { limit: 1 } : {}) + }) + return structuredClone({ ...result, page: { ...result.page, fence: 1, hostNow: 1234 }, providerSession: { key: 'session_id', id: 'provider-1' } }) - ) - }) + } + ) mocks.subscribe.mockImplementation( ( _target, @@ -205,11 +208,13 @@ describe('structured session cursor/body regression', () => { for (let index = 102; index <= 100 + missedRows; index += 1) { await appendOutput(index) } - const tail = readAgentSessionHistory(journal, { - sessionId: SESSION, - direction: 'tail', - limit: missedRows === 40 ? 1 : 200 - }).page + const tail = ( + await readAgentSessionHistory(journal, { + sessionId: SESSION, + direction: 'tail', + limit: missedRows === 40 ? 1 : 200 + }) + ).page expect(tail.liveCursor?.sequence).toBe(100 + missedRows) expect(tail.submissions).toEqual([]) feed.publish(SESSION, journal) @@ -241,7 +246,7 @@ describe('structured session cursor/body regression', () => { projectStructuredAgentSessionStatus(settled.items, settled.submissions, settled.fence) ).toBe(hostSummary?.status) expect(settled.submissions).toEqual( - missedRows > 200 ? tail.submissions : journal.snapshot().submissions + missedRows > 200 ? tail.submissions : await readTestJournalSubmissions(journal) ) expect({ cursor: caughtUp.cursor?.sequence, diff --git a/tests/e2e/structured-attention-read-retirement.test-fixture.tsx b/tests/e2e/structured-attention-read-retirement.test-fixture.tsx index 7c51073b50a..ae3b386d16c 100644 --- a/tests/e2e/structured-attention-read-retirement.test-fixture.tsx +++ b/tests/e2e/structured-attention-read-retirement.test-fixture.tsx @@ -233,7 +233,7 @@ beforeEach(() => { } ] ]), - readStatusState: () => projectStructuredAgentSessionStatusState(fixture.items), + readStatusState: async () => projectStructuredAgentSessionStatusState(fixture.items), readChildWork: () => undefined, now: hostClock }) diff --git a/tests/e2e/structured-attention-read-retirement.unit.test.ts b/tests/e2e/structured-attention-read-retirement.unit.test.ts index 065ffca6c05..38a75e44749 100644 --- a/tests/e2e/structured-attention-read-retirement.unit.test.ts +++ b/tests/e2e/structured-attention-read-retirement.unit.test.ts @@ -179,7 +179,7 @@ describe('Explicit desktop read acknowledgements', () => { } ] ]), - readStatusState: () => projectStructuredAgentSessionStatusState(otherItems), + readStatusState: async () => projectStructuredAgentSessionStatusState(otherItems), readChildWork: () => undefined, now: () => 42 }) diff --git a/tests/e2e/structured-chat-tab-lifetime-cost.unit.test.ts b/tests/e2e/structured-chat-tab-lifetime-cost.unit.test.ts deleted file mode 100644 index 96f48958f23..00000000000 --- a/tests/e2e/structured-chat-tab-lifetime-cost.unit.test.ts +++ /dev/null @@ -1,689 +0,0 @@ -import { afterEach, expect, it, vi } from 'vitest' -import type * as ChatTabLayoutQueueModule from '../../src/main/runtime/agent-session-tab-layout-queue' -import type { RuntimeMobileSessionTabsSnapshot } from '../../src/shared/runtime-types' -import { - authorityProfile, - openAuthorityRuntime -} from '../../src/main/runtime/agent-session/lifecycle/structured-agent-session-tab-authority.test-support' -import { chatTabCommandsSettled } from '../../src/main/runtime/orca-runtime-tests/session-layout-apply.test-fixture' -import type { createChatTabLayoutQueue } from '../../src/main/runtime/agent-session-tab-layout-queue' -import { fixture } from '../../src/main/persistence/loading-store/profile-state-delayed-authority-fixture' -import { - loadChatTabLayout, - withChatTabChanges -} from '../../src/main/runtime/agent-session/agent-session-tab-layout' - -const queueProbe = vi.hoisted(() => { - const queues: Pick, 'pendingOpens'>[] = [] - return { - enabled: false, - copies: 0, - visits: 0, - queues - } -}) -vi.mock('../../src/main/runtime/agent-session-tab-layout-queue', async (original) => { - const module = await original() - return { - ...module, - createChatTabLayoutQueue: (...args: Parameters) => { - const queue = module.createChatTabLayoutQueue(...args) - if (queueProbe.enabled) { - queueProbe.queues.push(queue) - const pending = queue.pendingOpens - queue.pendingOpens = () => { - const rows = pending() - queueProbe.copies += rows.length - for (const [index, row] of rows.entries()) { - Object.defineProperty(rows, index, { - get: () => { - queueProbe.visits++ - return row - } - }) - } - return rows - } - } - return queue - } - } -}) -afterEach(() => { - queueProbe.enabled = false - queueProbe.queues = [] - layoutOwner.main = false -}) - -const layoutOwner = vi.hoisted(() => ({ main: false })) -vi.mock('../../src/shared/layout-owner-switch', () => ({ - get ORCA_LAYOUT_OWNER_IS_MAIN() { - return layoutOwner.main - } -})) - -vi.mock('../../src/main/claude/claude-cli-flag-prewarm', () => ({ - prewarmClaudeCliFlags: async () => undefined -})) - -it.each([10, 100])( - 'counts real raw stores, unrelated window publications and captured cleanup at %i chats', - async (n) => { - const ids = Array.from({ length: n }, (_, i) => `selected-${i.toString().padStart(5, '0')}`) - const rig = await openAuthorityRuntime(await authorityProfile(ids, { visible: ids })) - await rig.runtime.restoreStructuredAgentSessionTabs() - await chatTabCommandsSettled() - const original = rig.runtime.mobileSessionTabsByWorktree.get(rig.workspace)! - const lookup = vi.spyOn(rig.store, 'getSessionTabId') - const record = vi.spyOn(rig.store, 'getConversation') - const journal = vi.spyOn(rig.store, 'transitionHandoff') - let orderVisits = 0 - const countedOrder = original.tabs.map((tab) => tab.id) - for (const [index, id] of countedOrder.entries()) { - Object.defineProperty(countedOrder, index, { - get: () => { - orderVisits++ - return id - } - }) - } - const raw = { - ...original, - tabGroups: [{ id: 'counted', activeTabId: null, tabOrder: countedOrder }] - } - rig.runtime.storeMobileSessionSnapshot(rig.workspace, raw) - expect(lookup).toHaveBeenCalledTimes(n) - expect(orderVisits).toBe(n) - expect(record).not.toHaveBeenCalled() - lookup.mockClear() - orderVisits = 0 - rig.runtime.attachWindow(1) - rig.runtime.markGraphReady(1) - rig.runtime.syncWindowGraph(1, { - tabs: [], - leaves: [], - mobileSessionTabs: [{ ...raw, publicationEpoch: 'cost-window', snapshotVersion: 1 }] - }) - expect(lookup.mock.calls.length).toBeLessThanOrEqual(3 * n) - expect(orderVisits).toBeLessThanOrEqual(6 * n) - lookup.mockClear() - record.mockClear() - // An unrelated terminal-only snapshot still traverses the real identity normalization seam. - rig.runtime.storeMobileSessionSnapshot('unrelated', { - ...raw, - worktree: 'unrelated', - tabs: [ - { - type: 'terminal', - id: 'term-tab::00000000-0000-4000-8000-000000000001', - parentTabId: 'term-tab', - leafId: '00000000-0000-4000-8000-000000000001', - title: 'terminal', - isActive: false - } - ] - }) - expect(lookup).not.toHaveBeenCalled() - expect(record).not.toHaveBeenCalled() - const saved = rig.profile.getWorkspaceSession().unifiedTabs?.[rig.workspace] ?? [] - let savedVisits = 0 - for (const tab of saved) { - const entityId = tab.entityId - Object.defineProperty(tab, 'entityId', { - get: () => { - savedVisits++ - return entityId - }, - configurable: true - }) - } - const { closeStructuredAgentSessionChild } = - await import('../../src/main/runtime/agent-session/structured-agent-session-close') - await closeStructuredAgentSessionChild(ids[0]!, { runtime: rig.runtime }) - await chatTabCommandsSettled() - expect(savedVisits).toBeLessThanOrEqual(8 * n) - expect(journal).not.toHaveBeenCalled() - expect(rig.acquire).not.toHaveBeenCalled() - } -) - -it.each([10, 100])( - 'publishes actual renderer mappings through the window graph in order at %i chats with linear trigger work', - async (n) => { - const { buildMobileSessionTabSnapshots } = - await import('../../src/renderer/src/runtime/sync-runtime-graph/mobile-session-snapshots') - const { makeState } = - await import('../../src/renderer/src/runtime/sync-runtime-graph-test-harness') - const mappings = - await import('../../src/renderer/src/runtime/web-session-tabs-sync/tracking-mappings') - const { hostSessionTabIdByLocalKey } = - await import('../../src/renderer/src/runtime/web-session-tabs-sync/state') - const ids = Array.from({ length: n }, (_, i) => `mapped-${i.toString().padStart(5, '0')}`) - const rig = await openAuthorityRuntime(await authorityProfile(ids, { visible: ids })) - await rig.runtime.restoreStructuredAgentSessionTabs() - await rig.host.setSessionTabVisibility(ids[0]!, false) - const issued = 'agent-chat-tab-v1-mapped-current' - await rig.host.setSessionTabVisibility(ids[0]!, true, issued) - await rig.runtime.publishStructuredAgentSessionTab({ - workspaceId: rig.workspace, - sessionId: ids[0]!, - agent: 'codex', - tabId: issued, - activate: false - }) - const tabs = ids.map((id, index) => ({ - id: `renderer-${id}`, - entityId: id, - contentType: 'agent-session' as const, - groupId: 'mapped-group', - worktreeId: rig.workspace, - label: id, - customLabel: null, - color: null, - sortOrder: index, - createdAt: 1 - })) - let entityVisits = 0, - orderVisits = 0 - for (const tab of tabs) { - const id = tab.entityId - Object.defineProperty(tab, 'entityId', { - get: () => { - entityVisits++ - return id - } - }) - } - const order = tabs.map((tab) => tab.id) - for (const [index, id] of order.entries()) { - Object.defineProperty(order, index, { - get: () => { - orderVisits++ - return id - } - }) - } - const state = makeState({ - folderWorkspaces: rig.profile.getFolderWorkspaces(), - projectGroups: rig.profile.getProjectGroups(), - unifiedTabsByWorktree: { [rig.workspace]: tabs }, - groupsByWorktree: { - [rig.workspace]: [ - { - id: 'mapped-group', - worktreeId: rig.workspace, - activeTabId: tabs[1]!.id, - tabOrder: order, - recentTabIds: [tabs[1]!.id] - } - ] - } - }) - try { - const legacy = buildMobileSessionTabSnapshots(state, true)[0]! - entityVisits = 0 - orderVisits = 0 - for (const [index, tab] of tabs.entries()) { - mappings.setHostSessionTabIdMapping( - { environmentId: 'local', worktreeId: rig.workspace, tabId: tab.id }, - index === 0 ? issued : `structured-agent-session-${tab.entityId}` - ) - } - const mappingReads = vi.spyOn(hostSessionTabIdByLocalKey, 'get') - const frame = buildMobileSessionTabSnapshots(state, true)[0]! - expect(frame).not.toBe(legacy) - expect(frame.tabGroups?.[0]?.tabOrder[0]).toBe(issued) - expect(mappingReads).toHaveBeenCalledTimes(n) - expect(entityVisits).toBeGreaterThanOrEqual(n) - expect(entityVisits).toBeLessThanOrEqual(5 * n) - expect(orderVisits).toBeLessThanOrEqual(5 * n) - mappingReads.mockClear() - entityVisits = 0 - orderVisits = 0 - expect(buildMobileSessionTabSnapshots(state, true)[0]).toBe(frame) - expect(mappingReads).toHaveBeenCalledTimes(n) - expect(entityVisits).toBe(0) - expect(orderVisits).toBe(0) - rig.runtime.attachWindow(1) - rig.runtime.markGraphReady(1) - rig.runtime.syncWindowGraph(1, { tabs: [], leaves: [], mobileSessionTabs: [frame] }) - const current = await rig.runtime.listMobileSessionTabs(rig.workspace) - expect(current.tabs[0]?.id).toBe(issued) - const { projectSessionTabs } = - await import('../../src/shared/workspace-layout/old-client/old-client-session-tabs-projection') - const { loaded } = loadChatTabLayout( - 'local', - rig.profile.getWorkspaceSession(), - rig.workspace - ) - const savedWorkspace = loaded.layout.workspaces[rig.workspace] - if (!savedWorkspace) { - throw new Error('published chat must have saved layout') - } - const projected = projectSessionTabs( - rig.workspace, - savedWorkspace, - { groups: {}, panes: {}, editorDrafts: {} }, - { tabLabels: {}, terminalRows: {}, browserTabs: {} }, - false - ) - expect(projected.tabs.map((tab) => tab.id)).toContain(issued) - expect(projected.tabGroups?.flatMap((group) => group.tabOrder)).toContain(issued) - expect(projected.tabGroups?.flatMap((group) => group.tabOrder)).not.toContain( - `agent-session:${ids[0]}` - ) - const passive = { - ...frame, - snapshotVersion: frame.snapshotVersion + 1, - activeTabId: `agent-session:${ids[0]}`, - tabGroups: [ - { - id: 'mapped-group', - activeTabId: `agent-session:${ids[0]}`, - recentTabIds: [`agent-session:${ids[0]}`, 'agent-chat-tab-v1-retired'], - tabOrder: [ - `agent-session:${ids[0]}`, - ...current.tabs.slice(1).map((tab) => tab.id), - 'agent-chat-tab-v1-retired' - ] - } - ] - } - rig.runtime.syncWindowGraph(1, { tabs: [], leaves: [], mobileSessionTabs: [passive] }) - const late = await rig.runtime.listMobileSessionTabs(rig.workspace) - expect(late.tabs[0]?.id).toBe(issued) - expect(late.tabGroups?.flatMap((group) => group.tabOrder)).not.toContain( - `agent-session:${ids[0]}` - ) - expect(late.tabGroups?.flatMap((group) => group.recentTabIds ?? [])).not.toContain( - 'agent-chat-tab-v1-retired' - ) - expect(rig.acquire).not.toHaveBeenCalled() - } finally { - mappings.clearHostSessionTabIdMappings('local', rig.workspace) - } - } -) - -it('keeps failed advisory legacy insertion and retention while the actual inner profile write waits', async () => { - layoutOwner.main = true - const { store: profile, authority } = await fixture() - const { root, B, holdStartup, rejectVisibility, savedIds } = - await import('../../src/main/runtime/agent-session/lifecycle/structured-agent-session-tab-authority.test-support') - const { installFakeAppEnvironment } = - await import('../../config/scripts/test-runtime/environment/vitest-host-ports-setup') - installFakeAppEnvironment({ getPath: () => root }) - const base = await authorityProfile([B], { visible: [B] }, profile) - const { workspace } = base - const rig = await openAuthorityRuntime(base) - expect(rig.store.getConversation(B)?.location.workspaceId).toBe(workspace) - const releaseStartup = await holdStartup(rig) - const { closeStructuredAgentSessionChild } = - await import('../../src/main/runtime/agent-session/structured-agent-session-close') - await closeStructuredAgentSessionChild(B, { runtime: rig.runtime }) - await chatTabCommandsSettled() - expect(savedIds(rig)).toEqual([]) - const gate = authority.pause() - const holding = profile.runDurableMutation(() => { - profile.updateSettings({ theme: 'dark' }) - return { value: undefined } - }) - await gate.started.promise - const applied = vi.spyOn(profile, 'applyLayoutCommand') - const allow = rejectVisibility(rig) - try { - await rig.runtime.publishStructuredAgentSessionTab({ - workspaceId: workspace, - sessionId: B, - agent: 'claude', - activate: true - }) - await vi.waitFor(() => expect(applied).toHaveBeenCalled()) - expect(rig.runtime.live(B)).toBe(true) - expect(savedIds(rig)).toEqual([]) - gate.finish.resolve() - await holding - const replies = await Promise.all(applied.mock.results.map((result) => result.value)) - expect(replies).toMatchObject([{ ok: true }]) - expect(rig.runtime.live(B)).toBe(true) - await releaseStartup() - await chatTabCommandsSettled() - expect(savedIds(rig)).toEqual([B]) - await rig.runtime.publishStructuredAgentSessionTab({ - workspaceId: workspace, - sessionId: B, - agent: 'claude', - activate: true - }) - await Promise.all(applied.mock.results.map((result) => result.value)) - await chatTabCommandsSettled() - expect(savedIds(rig)).toEqual([B]) - expect(rig.runtime.live(B)).toBe(true) - expect(rig.store.isSessionTabVisible(B)).toBe(false) - const snapshot = rig.runtime.mobileSessionTabsByWorktree.get(workspace)! - rig.runtime.storeMobileSessionSnapshot(workspace, { ...snapshot, tabs: [] }) - expect(rig.runtime.live(B)).toBe(false) - rig.runtime.storeMobileSessionSnapshot(workspace, snapshot) - expect(rig.runtime.live(B)).toBe(true) - rig.runtime.mobileSessionTabsByWorktree.delete(workspace) - expect(rig.runtime.live(B)).toBe(false) - expect(rig.acquire).not.toHaveBeenCalled() - } finally { - gate.finish.resolve() - await holding - await releaseStartup() - allow() - layoutOwner.main = false - } -}) - -it.each([ - [10, 10], - [100, 10], - [10, 100] -] as const)( - 'counts complete supplied-member sweep and separate final retirement at S=%i N=%i', - async (s, n) => { - vi.spyOn(Date, 'now').mockReturnValue(1_800_000_000_000) - const { - createStructuredSweepProgress, - closeStructuredSessionsForWorktree, - retireStructuredSessionTabsForWorktree - } = - await import('../../src/main/runtime/workspaces/removal/structured-session-worktree-teardown') - const ids = Array.from({ length: s }, (_, i) => `sweep-${i.toString().padStart(5, '0')}`) - const background = Array.from({ length: n }, (_, i) => `other-${i.toString().padStart(5, '0')}`) - const rig = await openAuthorityRuntime( - await authorityProfile([...ids, ...background], { visible: [...ids, ...background] }) - ) - await rig.runtime.restoreStructuredAgentSessionTabs() - await chatTabCommandsSettled() - const { root } = - await import('../../src/main/runtime/agent-session/lifecycle/structured-agent-session-tab-authority.test-support') - const { folderWorkspaceKey } = await import('../../src/shared/workspace-scope') - const other = folderWorkspaceKey( - rig.profile.createFolderWorkspace({ - projectGroupId: rig.groupId, - folderPath: `${root}/other`, - name: 'Other workspace' - }).id - ) - const savedOther = withChatTabChanges( - 'local', - rig.profile.getWorkspaceSession('local'), - other, - background.map((sessionId) => ({ - open: { sessionId, agent: 'codex', tabId: `other-${sessionId}` } - })) - ) - if (!savedOther) { - throw new Error('unrelated saved rows must exist') - } - rig.profile.setWorkspaceSession(savedOther) - const own = rig.runtime.mobileSessionTabsByWorktree.get(rig.workspace)! - rig.runtime.storeMobileSessionSnapshot(rig.workspace, { - ...own, - tabGroups: own.tabs.map((row) => ({ - id: `g-${row.id}`, - activeTabId: row.id, - tabOrder: [row.id], - recentTabIds: [row.id] - })) - }) - rig.runtime.storeMobileSessionSnapshot(other, { - ...own, - worktree: other, - tabs: own.tabs.filter( - (row) => row.type === 'agent-session' && background.includes(row.sessionId) - ), - tabGroups: [] - }) - await chatTabCommandsSettled() - const unrelated = rig.runtime.mobileSessionTabsByWorktree.get(other) - const counts = { - saved: 0, - savedBudget: 0, - savedReads: 0, - raw: 0, - orders: 0, - workspaces: 0, - stores: 0, - titlePassBudget: 0 - } - const countedArrays = new WeakSet(), - countedRows = new WeakSet() - const instrument = (rows: T[], key: 'raw' | 'orders') => { - if (countedArrays.has(rows)) { - return - } - countedArrays.add(rows) - for (const [index, value] of rows.entries()) { - Object.defineProperty(rows, index, { - configurable: true, - get: () => { - counts[key]++ - return value - } - }) - } - } - const wrapSnapshot = (snapshot: RuntimeMobileSessionTabsSnapshot) => { - instrument(snapshot.tabs, 'raw') - for (const group of snapshot.tabGroups ?? []) { - instrument(group.tabOrder, 'orders') - if (group.recentTabIds) { - instrument(group.recentTabIds, 'orders') - } - } - } - for (const snapshot of rig.runtime.mobileSessionTabsByWorktree.values()) { - wrapSnapshot(snapshot) - } - const store = rig.runtime.storeMobileSessionSnapshot.bind(rig.runtime) - vi.spyOn(rig.runtime, 'storeMobileSessionSnapshot').mockImplementation( - (workspace, snapshot) => { - counts.stores++ - wrapSnapshot(snapshot) - const result = store(workspace, snapshot) - wrapSnapshot(result) - return result - } - ) - const saved = rig.profile.getWorkspaceSession.bind(rig.profile) - vi.spyOn(rig.profile, 'getWorkspaceSession').mockImplementation((...args) => { - const session = saved(...args) - counts.savedReads++ - counts.savedBudget += Object.values(session.unifiedTabs ?? {}).reduce( - (total, rows) => total + rows.length, - 0 - ) - for (const rows of Object.values(session.unifiedTabs ?? {})) { - for (const row of rows) { - if (countedRows.has(row)) { - continue - } - countedRows.add(row) - const entity = row.entityId - Object.defineProperty(row, 'entityId', { - configurable: true, - get: () => { - counts.saved++ - return entity - } - }) - } - } - return session - }) - const entries = rig.runtime.mobileSessionTabsByWorktree[Symbol.iterator].bind( - rig.runtime.mobileSessionTabsByWorktree - ) - vi.spyOn(rig.runtime.mobileSessionTabsByWorktree, Symbol.iterator).mockImplementation( - function* () { - for (const entry of entries()) { - counts.workspaces++ - yield entry - } - return undefined - } - ) - const retitle = rig.runtime.refreshStructuredConversationTabTitle.bind(rig.runtime) - vi.spyOn(rig.runtime, 'refreshStructuredConversationTabTitle').mockImplementation( - (workspace, id) => { - counts.titlePassBudget += - rig.runtime.mobileSessionTabsByWorktree.get(workspace)?.tabs.length ?? 0 - return retitle(workspace, id) - } - ) - const close = vi.spyOn(rig.host, 'close') - const members = ids.map((sessionId) => ({ sessionId, agent: 'codex' as const })) - const measure = async (phase: () => Promise) => { - for (const key of [ - 'saved', - 'savedBudget', - 'savedReads', - 'raw', - 'orders', - 'workspaces', - 'stores', - 'titlePassBudget' - ] as const) { - counts[key] = 0 - } - await phase() - await chatTabCommandsSettled() - const result = { ...counts } - // Each exact close re-reads saved layouts and rewrites/indexes its current snapshot. - // The whole trigger is multiplicative in supplied members and current row population. - expect(result.saved).toBeGreaterThan(0) - // Client projection asks for each surviving title; each lookup scans saved rows. - // Layout load additionally clones/reports its input. Charge those actual input populations. - expect(result.savedReads).toBeLessThanOrEqual(s * (s + 2 * n + 8)) - expect(result.saved).toBeLessThanOrEqual(3 * result.savedBudget) - console.info('whole-sweep-cost', { s, n, ...result }) - // Saved-write listeners retitle every surviving row, each mapping its current raw population. - const retirementAndProjectionPasses = 24 * s * (s + 2 * n) - const savedWriteRetitlePasses = 3 * result.titlePassBudget - expect(result.raw + result.orders).toBeLessThanOrEqual( - retirementAndProjectionPasses + savedWriteRetitlePasses - ) - expect(result.stores).toBe(s) - expect(result.workspaces).toBeLessThanOrEqual(4 * s) - return result - } - const progress = createStructuredSweepProgress(members) - await measure(() => - closeStructuredSessionsForWorktree(progress, 1_800_000_060_000, { runtime: rig.runtime }) - ) - expect(progress).toMatchObject({ closed: s, settled: s }) - expect(close).toHaveBeenCalledTimes(s) - expect( - rig.runtime.mobileSessionTabsByWorktree - .get(rig.workspace) - ?.tabs.map((row) => (row.type === 'agent-session' ? row.sessionId : null)) - ).toEqual(background) - // Reopen real lifetimes so final retirement is a populated whole trigger, rather than an empty timing control. - for (const sessionId of ids) { - await rig.runtime.publishStructuredAgentSessionTab({ - workspaceId: rig.workspace, - sessionId, - agent: 'codex', - activate: false, - navigation: 'caller' - }) - } - await chatTabCommandsSettled() - await measure(() => retireStructuredSessionTabsForWorktree(members, rig.runtime)) - await retireStructuredSessionTabsForWorktree(members, rig.runtime) - await chatTabCommandsSettled() - expect(rig.runtime.mobileSessionTabsByWorktree.get(other)).toBe(unrelated) - expect(rig.acquire).not.toHaveBeenCalled() - expect(close).toHaveBeenCalledTimes(s) - expect(rig.runtime.mobileSessionTabsByWorktree.get(rig.workspace)?.tabs).toHaveLength(n) - } -) - -it.each([10, 100])( - 'copies, captures, retains and releases %i concrete pending opens through the inner profile queue', - async (p) => { - layoutOwner.main = true - queueProbe.enabled = true - queueProbe.copies = 0 - queueProbe.visits = 0 - const { store: profile, authority } = await fixture() - const { root, B, rejectVisibility } = - await import('../../src/main/runtime/agent-session/lifecycle/structured-agent-session-tab-authority.test-support') - const { installFakeAppEnvironment } = - await import('../../config/scripts/test-runtime/environment/vitest-host-ports-setup') - installFakeAppEnvironment({ getPath: () => root }) - const ids = Array.from({ length: p }, (_, i) => - i === 0 ? B : `pending-${i.toString().padStart(5, '0')}` - ) - const rig = await openAuthorityRuntime( - await authorityProfile([], { records: ids, visible: [] }, profile) - ) - await rig.runtime.restoreStructuredAgentSessionTabs() - await chatTabCommandsSettled() - const queue = queueProbe.queues.at(-1)! - const gate = authority.pause() - const holding = profile.runDurableMutation(() => { - profile.updateSettings({ theme: 'dark' }) - return { value: undefined } - }) - await gate.started.promise - const allow = vi.fn(rejectVisibility(rig)) - const { closeStructuredAgentSessionChild } = - await import('../../src/main/runtime/agent-session/structured-agent-session-close') - try { - for (const sessionId of ids) { - await rig.runtime.publishStructuredAgentSessionTab({ - workspaceId: rig.workspace, - sessionId, - agent: 'codex', - activate: false - }) - } - queueProbe.copies = 0 - queueProbe.visits = 0 - const captured = rig.runtime.captureStructuredAgentSessionPresentation(B) - expect(queueProbe.copies).toBe(p) - expect(queueProbe.visits).toBe(p) - expect(captured.saved).toHaveLength(1) - const first = captured.saved[0]! - expect(await closeStructuredAgentSessionChild(B, { runtime: rig.runtime })).toMatchObject({ - stopped: true - }) - allow() - // Later legitimate open is not in the already admitted captured target set. - await rig.runtime.publishStructuredAgentSessionTab({ - workspaceId: rig.workspace, - sessionId: B, - agent: 'codex', - activate: false, - navigation: 'caller' - }) - const successor = rig.store.getSessionTabId(B) - expect(successor).not.toBe(first.tabId) - expect(captured.saved).toEqual([first]) - gate.finish.resolve() - await holding - await chatTabCommandsSettled() - expect(queue.pendingOpens()).toHaveLength(0) - expect( - profile - .getWorkspaceSession() - .unifiedTabs?.[rig.workspace]?.find((row) => row.entityId === B)?.id - ).toBe(successor) - expect(authority.captures.length).toBeLessThanOrEqual(2 * p + 5) - expect(rig.acquire).not.toHaveBeenCalled() - // A refused/stale inner command released its descriptor, then a valid command progressed. - expect(profile.getWorkspaceSession().unifiedTabs?.[rig.workspace]).toHaveLength(p) - } finally { - gate.finish.resolve() - await holding - if (!allow.mock.calls.length) { - allow() - } - } - } -) diff --git a/tests/e2e/structured-session-workspace-authority.unit.test.ts b/tests/e2e/structured-session-workspace-authority.unit.test.ts index 099a3cc5975..19a40639dad 100644 --- a/tests/e2e/structured-session-workspace-authority.unit.test.ts +++ b/tests/e2e/structured-session-workspace-authority.unit.test.ts @@ -1,3 +1,4 @@ +import { readTestHostJournal } from '../../src/main/native-chat/agent-session-journal/journal-observation.test-support' import { FLOATING_TERMINAL_WORKTREE_ID } from '../../src/shared/constants' import { makeWorkspaceLineage } from '../../src/main/persistence/persistence-test-harness' import * as folderTeardown from '../../src/main/runtime/workspaces/removal/folder-workspace-pty-teardown' @@ -180,7 +181,7 @@ it.each([true, false])( } await rig.runtime.restoreStructuredAgentSessionTabs() await chatTabCommandsSettled() - const transcript = (await rig.host.journalSnapshot(B)).items + const transcript = (await readTestHostJournal(rig.host, B)).items expect(transcript).toHaveLength(1) expect(rig.runtime.live(B)).toBe(true) expect(await rig.runtime.deleteFolderWorkspace(rig.folderId)).toEqual({ deleted: true }) @@ -201,7 +202,7 @@ it.each([true, false])( expect(rig.profile.getAllWorkspaceLineage()).toEqual({ [lineage.childWorkspaceKey]: lineage }) expect(rig.store.getRecord(B)?.providerHandleChain).toEqual(record.providerHandleChain) expect(rig.store.getRecord(B)?.location).toEqual(record.location) - expect((await rig.host.journalSnapshot(B)).items).toEqual(transcript) + expect((await readTestHostJournal(rig.host, B)).items).toEqual(transcript) expect(result).toMatchObject({ ok: true, result: { From 5f90da574b15537e55b5c828e80c0b18c36bb03f Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:42:52 -0800 Subject: [PATCH 38/45] refactor: type handover owners and finite recovery snapshots --- .../delivery/structured-agent-session-handing-over.ts | 5 +++-- .../termination/structured-agent-session-unfinished-work.ts | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-handing-over.ts b/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-handing-over.ts index 5426acdefb1..6624e3c9465 100644 --- a/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-handing-over.ts +++ b/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-handing-over.ts @@ -4,9 +4,10 @@ import type { AgentJournalSubmission } from '../../../../shared/agent-session/journal/agent-session-journal-types' import type { UnsentSendHold } from '../../agent-session-journal/queued-messages/journal-unsent-send-hold' import type { AgentSessionJournal } from '../../agent-session-journal/journal-store' +import type { UnopenedSendJournal } from './structured-agent-session-unopened-send-withdrawal' -/** The chat's journal, which identifies the conversation for as long as it is held. */ -type HandoverJournal = AgentSessionJournal +/** Live handles and finite recovery snapshots identify their own handover windows. */ +type HandoverJournal = AgentSessionJournal | Pick type HandoverInput = { owner: () => void diff --git a/src/main/native-chat/agent-session-wire/termination/structured-agent-session-unfinished-work.ts b/src/main/native-chat/agent-session-wire/termination/structured-agent-session-unfinished-work.ts index 731dd267e91..7b4f84f659d 100644 --- a/src/main/native-chat/agent-session-wire/termination/structured-agent-session-unfinished-work.ts +++ b/src/main/native-chat/agent-session-wire/termination/structured-agent-session-unfinished-work.ts @@ -15,10 +15,11 @@ import { type StructuredAgentSessionWatchedExit } from './structured-agent-session-stale-turn-verdict' import type { UnopenedSendJournal } from '../delivery/structured-agent-session-unopened-send-withdrawal' +import type { AgentSessionJournal } from '../../agent-session-journal/journal-store' import { isQueuedAgentJournalSubmission } from '../../../../shared/agent-session/requests/agent-session-queued-submission' export type DeadGenerationJournal = UnopenedSendJournal & { - handoverJournal?: object + handoverJournal?: AgentSessionJournal pendingSubmissions?: () => AgentJournalSubmission[] itemFence: (itemId: string) => number | undefined } From 3bd9f8ecdc4ea423766300711cae8a1651719ce1 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:37:32 -0800 Subject: [PATCH 39/45] Replace casts and empty type imports in the 4a harness scripts with annotations and guards Renderer times and tab ids from waitForFunction handles go through typeof guards, the visibility probe is a declared Window global, the runtime call keeps its own typed response, and cached manifests, Electron paths, profiles and families are narrowed by guards. The empty `import type {}` blocks name AppStore and use it. Three casts remain, each with a line-specific SAFETY: the generic RPC result and Node's performance inside main-process callbacks. --- .../native-chat/native-chat-4a-arm.ts | 19 +++-- .../native-chat/native-chat-4a-comparison.ts | 10 ++- .../native-chat/native-chat-4a-fixture.ts | 15 +++- .../native-chat-4a-ops-lifecycle.ts | 4 +- .../native-chat/native-chat-4a-ops-reads.ts | 6 +- .../native-chat/native-chat-4a-ops-shapes.ts | 2 +- .../native-chat/native-chat-4a-probes.ts | 82 ++++++++++++------- .../native-chat/native-chat-4a-session.ts | 18 ++-- .../native-chat/native-chat-4a-workloads.ts | 2 +- 9 files changed, 101 insertions(+), 57 deletions(-) diff --git a/config/scripts/performance/native-chat/native-chat-4a-arm.ts b/config/scripts/performance/native-chat/native-chat-4a-arm.ts index 0b6c5d3160c..09feb7e4eba 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-arm.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-arm.ts @@ -1,6 +1,6 @@ // One 4a arm: a built checkout, its own envelope/fingerprint builders, and background launches it alone stops by recorded PID. -// Declares the window.__store and window.api globals these page.evaluate callbacks use. -import type {} from '../../../../tests/e2e/helpers/runtime-types' +// Its AppStore import also declares the window.__store and window.api globals these page.evaluate callbacks use. +import type { AppStore } from '../../../../tests/e2e/helpers/runtime-types' import { execFileSync } from 'node:child_process' import { randomUUID } from 'node:crypto' import { chmodSync, constants, cpSync, existsSync, mkdirSync, writeFileSync } from 'node:fs' @@ -79,7 +79,10 @@ export async function resolveArm(name: string, root: string, scratch: string): P if (!existsSync(path.join(absolute, 'out', 'main', 'index.js'))) { throw new Error(`${name}: ${absolute} has no out/main/index.js; build that checkout first`) } - const electronPath = createRequire(path.join(absolute, 'package.json'))('electron') as string + const electronPath: unknown = createRequire(path.join(absolute, 'package.json'))('electron') + if (typeof electronPath !== 'string') { + throw new Error(`${name}: the electron package in ${absolute} names no executable`) + } const outfile = path.join(scratch, `${name}-protocol.cjs`) const shared = './src/shared/agent-session' await build({ @@ -98,7 +101,7 @@ export async function resolveArm(name: string, root: string, scratch: string): P logLevel: 'silent', nodePaths: [path.join(HARNESS_ROOT, 'node_modules')] }) - const protocol = createRequire(import.meta.url)(outfile) as ArmProtocol + const protocol: ArmProtocol = createRequire(import.meta.url)(outfile) return { name, root: absolute, sha: git(absolute, 'rev-parse', 'HEAD'), electronPath, protocol } } @@ -220,7 +223,7 @@ export async function prepareWorkspace( if ('error' in added) { throw new Error(added.error) } - const store = window.__store! + const store: AppStore = window.__store! await store.getState().fetchRepos({ runtimeEnvironmentId: null, throwOnError: true }) await store.getState().updateRepo(added.repo.id, { externalWorktreeVisibility: 'show', @@ -239,10 +242,8 @@ export async function prepareWorkspace( (a, b) => Number(b.isMainWorktree) - Number(a.isMainWorktree) ) store.getState().setActiveWorktree(ordered[0]!.id) - return { - repoId: added.repo.id, - worktreeIds: [ordered[0]!.id, ordered[1]!.id] as [string, string] - } + const worktreeIds: [string, string] = [ordered[0]!.id, ordered[1]!.id] + return { repoId: added.repo.id, worktreeIds } } await new Promise((resolve) => setTimeout(resolve, 500)) } diff --git a/config/scripts/performance/native-chat/native-chat-4a-comparison.ts b/config/scripts/performance/native-chat/native-chat-4a-comparison.ts index 3a65cdf5203..1122d1ce14d 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-comparison.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-comparison.ts @@ -219,12 +219,18 @@ async function main(): Promise { if (values['report-only']) { const results = readdirSync(out) .filter((file) => /^arm-.*\.json$/.test(file)) - .map((file) => JSON.parse(readFileSync(path.join(out, file), 'utf8')) as ArmResult) + .map((file): ArmResult => JSON.parse(readFileSync(path.join(out, file), 'utf8'))) .sort((a, b) => a.startedAt.localeCompare(b.startedAt)) writeComparison(out, workload, results, log) return } - const families = new Set(values.families.split(',').map((family) => family.trim()) as Family[]) + const isFamily = (name: string): name is Family => FAMILIES.some((family) => family === name) + const families = new Set( + values.families + .split(',') + .map((family) => family.trim()) + .filter(isFamily) + ) const { resolveArm, writeProviderStub } = await import('./native-chat-4a-arm') const { seedProfile, seedDirFor } = await import('./native-chat-4a-fixture') const { createSeededTestRepo } = await import('../../../../tests/e2e/helpers/seeded-test-repo') diff --git a/config/scripts/performance/native-chat/native-chat-4a-fixture.ts b/config/scripts/performance/native-chat/native-chat-4a-fixture.ts index d21de95034a..b93bb3cb0cf 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-fixture.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-fixture.ts @@ -48,6 +48,11 @@ async function countItems(page: Page, sessionId: string): Promise { } } +/** A manifest this harness wrote from `recipe`; only the recipe digest decides reuse. */ +function isManifestFrom(value: unknown, recipe: string): value is Manifest { + return typeof value === 'object' && value !== null && 'recipe' in value && value.recipe === recipe +} + const recipeOf = (workload: Workload, toolOutputBytes: number): string => createHash('sha256') .update(JSON.stringify({ history: workload.history, toolOutputBytes })) @@ -105,13 +110,15 @@ export async function seedProfile(input: { const manifestPath = `${seedDir}.json` const recipe = recipeOf(input.workload, input.toolOutputBytes) if (existsSync(manifestPath) && existsSync(seedDir)) { - const cached = JSON.parse(readFileSync(manifestPath, 'utf8')) as Partial - if (cached.recipe !== recipe) { + const cached: unknown = JSON.parse(readFileSync(manifestPath, 'utf8')) + if (!isManifestFrom(cached, recipe)) { + const recorded = + typeof cached === 'object' && cached !== null && 'recipe' in cached ? cached.recipe : null throw new Error( - `${manifestPath} was seeded from another recipe (${cached.recipe ?? 'none recorded'}, now ${recipe}); move it and ${seedDir} aside or use a new --out` + `${manifestPath} was seeded from another recipe (${String(recorded ?? 'none recorded')}, now ${recipe}); move it and ${seedDir} aside or use a new --out` ) } - return cached as Manifest + return cached } const started = Date.now() const launch = await launchArm(input.arm, input.profileDir, input.stubCommand) diff --git a/config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts b/config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts index db41444fdde..a53773415cb 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-ops-lifecycle.ts @@ -1,6 +1,6 @@ // 4a process-lifecycle rows: cold/warm open and rewind; first launch on the seeded profile lives in the conversion module. import { unconvertedOpenTarget } from './native-chat-4a-fixture' -import { markerSeenAt, rendererNow, rpc, watchMarkers } from './native-chat-4a-probes' +import { markerSeenAt, rendererNow, rendererTime, rpc, watchMarkers } from './native-chat-4a-probes' import type { Sample } from './native-chat-4a-report' import { addRow, @@ -170,7 +170,7 @@ export async function rewindRows(ctx: RunContext): Promise { goneMarker, { timeout: 600_000, polling: 16 } ) - .then(async (handle) => (await handle.jsonValue()) as number) + .then(rendererTime) await waitForDurable(page(ctx), other, `4A-TAIL-${competingTag}`) return { hostMs: answer.hostMs, diff --git a/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts b/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts index f3ec51ca9da..1adae66e6ef 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-ops-reads.ts @@ -1,7 +1,7 @@ // 4a read rows on the retained large chat: history pages, item detail, outline, fork, and record operations on the many-chat profile. import { randomUUID } from 'node:crypto' import { operationId } from './native-chat-4a-arm' -import { rpc } from './native-chat-4a-probes' +import { rendererTime, rpc } from './native-chat-4a-probes' import type { Sample } from './native-chat-4a-report' import { addRow, @@ -65,7 +65,7 @@ export async function historyRows(ctx: RunContext): Promise { } // Whatever the streaming rows left on the tail; named shapes are verified rows of their own (native-chat-4a-ops-shapes.ts). - const items = [...tail.page.items, ...older.page.items] as Item[] + const items: Item[] = [...tail.page.items, ...older.page.items] const sized = items .map((item) => ({ item, bytes: bytesOf(item) })) .sort((a, b) => b.bytes - a.bytes) @@ -157,7 +157,7 @@ export async function recordRows(ctx: RunContext): Promise { name, { timeout: 60_000, polling: 16 } ) - .then(async (handle) => (await handle.jsonValue()) as number) + .then(rendererTime) .catch(() => Number.NaN) return { hostMs: answer.hostMs, diff --git a/config/scripts/performance/native-chat/native-chat-4a-ops-shapes.ts b/config/scripts/performance/native-chat/native-chat-4a-ops-shapes.ts index 0578bba0fd9..790e8c5eed7 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-ops-shapes.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-ops-shapes.ts @@ -35,7 +35,7 @@ async function corpusChat(ctx: RunContext, prompts: string[]): Promise { return chat.sessionId } -const itemsOf = (read: Page): WireItem[] => read.items as WireItem[] +const itemsOf = (read: Page): WireItem[] => read.items const byProviderId = (items: WireItem[], id: string): WireItem | undefined => items.find((item) => JSON.stringify(item).includes(`"${id}"`)) diff --git a/config/scripts/performance/native-chat/native-chat-4a-probes.ts b/config/scripts/performance/native-chat/native-chat-4a-probes.ts index 1c21792c1f7..4ae01aa88e1 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-probes.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-probes.ts @@ -6,7 +6,35 @@ import type { ElectronApplication, Page } from '@stablyai/playwright-test' import { retryTransientMainEvaluate } from '../../../../tests/e2e/helpers/electron-main-evaluate-retry' import { summarizeEventLoopDelay } from '../../../../tests/e2e/helpers/event-loop-delay-report' -export type RpcAnswer = { ok: true; result: T } | { ok: false; error: unknown } +type ProbeState = { seen: Map; mutations: number[]; watch: Set } + +declare global { + // oxlint-disable-next-line typescript-eslint/consistent-type-definitions -- declaration merging requires interface + interface Window { + /** The 4a visibility probe, installed by installVisibilityProbe. */ + __4a?: ProbeState + } +} + +/** The refusal some writes (create, fork) carry inside a successful call: `{ ok: false, refusal }`. */ +function inResultRefusal(result: unknown): { refusal: unknown } | null { + return typeof result === 'object' && + result !== null && + 'ok' in result && + result.ok === false && + 'refusal' in result + ? { refusal: result.refusal } + : null +} + +/** A waitForFunction handle whose value is a renderer-clock time. */ +export async function rendererTime(handle: { jsonValue(): Promise }): Promise { + const value = await handle.jsonValue() + if (typeof value !== 'number') { + throw new Error(`expected a renderer time, got ${JSON.stringify(value)}`) + } + return value +} /** One RPC from the renderer, timed on the renderer clock: the host span every row reports beside its visible end point. */ export async function rpc( @@ -18,21 +46,18 @@ export async function rpc( const answer = await page.evaluate( async ({ method, params }) => { const startedAt = performance.now() - const reply = (await window.api.runtime.call({ method, params })) as RpcAnswer + const reply = await window.api.runtime.call({ method, params }) return { reply, startedAt, hostMs: performance.now() - startedAt } }, { method, params } ) - // Some writes (create, fork) refuse inside a successful call: `{ ok: false, refusal }`. - const result = answer.reply.ok - ? (answer.reply.result as { ok?: unknown; refusal?: unknown }) - : null - const inResult = result?.ok === false && 'refusal' in result - const refused = !answer.reply.ok || inResult + const inResult = answer.reply.ok ? inResultRefusal(answer.reply.result) : null + const refused = !answer.reply.ok || inResult !== null if (refused && !options.allowRefusal) { - const reason = answer.reply.ok ? result?.refusal : answer.reply.error + const reason = answer.reply.ok ? inResult?.refusal : answer.reply.error throw new Error(`${method} refused: ${JSON.stringify(reason).slice(0, 400)}`) } + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the runtime RPC answers untyped JSON; each caller names the shape its method returns and reads optional fields defensively. const value = (answer.reply.ok ? answer.reply.result : answer.reply.error) as T return { value, refused, startedAt: answer.startedAt, hostMs: answer.hostMs } } @@ -40,18 +65,11 @@ export async function rpc( /** Install once per page: records when text first appears in the chat root and every transcript mutation batch. */ export async function installVisibilityProbe(page: Page): Promise { await page.evaluate(() => { - const probe = window as unknown as { - __4a?: { seen: Map; mutations: number[]; watch: Set } - } - if (probe.__4a) { + if (window.__4a) { return } - const state = { - seen: new Map(), - mutations: [] as number[], - watch: new Set() - } - probe.__4a = state + const state: ProbeState = { seen: new Map(), mutations: [], watch: new Set() } + window.__4a = state const check = (): void => { const now = performance.now() state.mutations.push(now) @@ -86,8 +104,10 @@ export async function installVisibilityProbe(page: Page): Promise { export async function watchMarkers(page: Page, markers: string[]): Promise { await page.evaluate((markers) => { - const state = (window as unknown as { __4a: { watch: Set; seen: Map } }) - .__4a + const state = window.__4a + if (!state) { + throw new Error('4a visibility probe not installed') + } for (const marker of markers) { state.seen.delete(marker) state.watch.add(marker) @@ -103,9 +123,12 @@ export async function markerSeenAt( ): Promise { const handle = await page .waitForFunction( - (marker) => - (window as unknown as { __4a: { seen: Map } }).__4a.seen.get(marker) ?? - false, + (marker) => { + if (!window.__4a) { + throw new Error('4a visibility probe not installed') + } + return window.__4a.seen.get(marker) ?? false + }, marker, { timeout: timeoutMs, polling: 16 } ) @@ -117,7 +140,7 @@ export async function markerSeenAt( } ) }) - return (await handle.jsonValue()) as number + return rendererTime(handle) } /** One line on the chat the user sees, for timeout messages. */ @@ -136,9 +159,10 @@ export function visibleChatState(page: Page): Promise { export async function mutationGaps(page: Page, from: number, to: number): Promise { return page.evaluate( ({ from, to }) => { - const times = (window as unknown as { __4a: { mutations: number[] } }).__4a.mutations.filter( - (t) => t >= from && t <= to - ) + if (!window.__4a) { + throw new Error('4a visibility probe not installed') + } + const times = window.__4a.mutations.filter((t) => t >= from && t <= to) return times.slice(1).map((time, index) => time - times[index]!) }, { from, to } @@ -155,12 +179,14 @@ export async function hostWorkMark(app: ElectronApplication): Promise<() => Prom const start = await retryTransientMainEvaluate(() => app.evaluate(() => ({ cpu: process.cpuUsage(), + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: this callback runs in Electron's main process, where performance is Node's perf_hooks object; the DOM lib this file needs types it as the browser one. elu: (performance as unknown as NodePerformance).eventLoopUtilization() })) ) return async () => { const delta = await retryTransientMainEvaluate(() => app.evaluate((_electron, start) => { + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: main-process callback; performance is Node's perf_hooks object, typed here by the DOM lib. const node = performance as unknown as NodePerformance return { cpu: process.cpuUsage(start.cpu), diff --git a/config/scripts/performance/native-chat/native-chat-4a-session.ts b/config/scripts/performance/native-chat/native-chat-4a-session.ts index 241054f0930..3cc679cc5b0 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-session.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-session.ts @@ -1,10 +1,10 @@ // Chat operations through the host RPC surface both arms serve, with envelopes built by each arm's own protocol bundle. -// Declares the window.__store and window.api globals these page.evaluate callbacks use. -import type {} from '../../../../tests/e2e/helpers/runtime-types' +// Its AppStore import also declares the window.__store and window.api globals these page.evaluate callbacks use. +import type { AppStore } from '../../../../tests/e2e/helpers/runtime-types' import { randomUUID } from 'node:crypto' import type { Page } from '@stablyai/playwright-test' import { operationId, type Arm } from './native-chat-4a-arm' -import { rpc, visibleChatState } from './native-chat-4a-probes' +import { rendererTime, rpc, visibleChatState } from './native-chat-4a-probes' type HistoryItem = { itemId: string; body?: { kind?: string; role?: string; state?: string } } export type HistoryPage = { @@ -184,11 +184,15 @@ export async function openChatTab( { worktreeId, sessionId }, { timeout: 60_000 } ) - const id = (await tabId.jsonValue()) as string + const id = await tabId.jsonValue() + if (typeof id !== 'string') { + throw new Error(`${sessionId}: no tab id for the chat`) + } // Unified chat tabs activate through their group; a worktree switch can restore another tab first, so re-apply until it holds. await page.waitForFunction( ({ worktreeId, id }) => { - const state = window.__store!.getState() + const store: AppStore = window.__store! + const state = store.getState() const group = (state.groupsByWorktree[worktreeId] ?? []).find((candidate) => candidate.tabOrder.includes(id) ) @@ -198,7 +202,7 @@ export async function openChatTab( if (state.activeWorktreeId !== worktreeId) { state.setActiveWorktree(worktreeId) } - window.__store!.getState().activateTab(id, { worktreeId }) + store.getState().activateTab(id, { worktreeId }) return false }, { worktreeId, id }, @@ -248,5 +252,5 @@ export async function waitForWorking( } ) }) - return (await handle.jsonValue()) as number + return rendererTime(handle) } diff --git a/config/scripts/performance/native-chat/native-chat-4a-workloads.ts b/config/scripts/performance/native-chat/native-chat-4a-workloads.ts index de021cb3056..242db51cf3d 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-workloads.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-workloads.ts @@ -122,7 +122,7 @@ export const WORKLOADS: Record = { } export function workloadFor(profile: string, conversionSourceBytes?: number): Workload { - const workload = WORKLOADS[profile as Workload['profile']] + const workload = profile === 'smoke' || profile === 'full' ? WORKLOADS[profile] : undefined if (!workload) { throw new Error(`Unknown profile ${profile}; expected ${Object.keys(WORKLOADS).join(', ')}`) } From 288681caf699eab1b736432c2ec1fe67a0cfb205 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:56:06 -0800 Subject: [PATCH 40/45] Name the 4a arm's bundled envelope builders by their tracked source files The protocol bundle imported from './src/shared/agent-session/...', a folder the path-literal ratchet reads as an unresolved import. Each module is now a tracked-file constant joined onto the arm's own checkout, the same form runtime-file-watcher-resource-probe.mjs uses; the bundle and its exports are unchanged. --- .../performance/native-chat/native-chat-4a-arm.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/config/scripts/performance/native-chat/native-chat-4a-arm.ts b/config/scripts/performance/native-chat/native-chat-4a-arm.ts index 09feb7e4eba..098883f0b62 100644 --- a/config/scripts/performance/native-chat/native-chat-4a-arm.ts +++ b/config/scripts/performance/native-chat/native-chat-4a-arm.ts @@ -25,6 +25,10 @@ import { installVisibilityProbe } from './native-chat-4a-probes' const HARNESS_ROOT = path.resolve(import.meta.dirname, '../../../..') const SYSTEM_DIRS = ['/usr/bin', '/bin', '/usr/sbin', '/sbin'] +// The envelope builders each arm bundles from its own checkout. +const MUTATION_SOURCE = 'src/shared/agent-session/lifecycle/structured-agent-session-mutation.ts' +const CREATE_SOURCE = 'src/shared/agent-session/structured-agent-session-create.ts' +const SEND_SOURCE = 'src/shared/agent-session/requests/structured-agent-session-send-mutation.ts' /** Envelope builders compiled from the arm's own source, so each host checks fingerprints it computes itself. */ export type ArmProtocol = { @@ -84,13 +88,13 @@ export async function resolveArm(name: string, root: string, scratch: string): P throw new Error(`${name}: the electron package in ${absolute} names no executable`) } const outfile = path.join(scratch, `${name}-protocol.cjs`) - const shared = './src/shared/agent-session' + const source = (file: string): string => JSON.stringify(path.join(absolute, file)) await build({ stdin: { contents: [ - `export * from '${shared}/lifecycle/structured-agent-session-mutation'`, - `export { createStructuredAgentSessionId } from '${shared}/structured-agent-session-create'`, - `export { structuredAgentSessionMessageSendMutation, agentSessionSendBodyFingerprint } from '${shared}/requests/structured-agent-session-send-mutation'` + `export * from ${source(MUTATION_SOURCE)}`, + `export { createStructuredAgentSessionId } from ${source(CREATE_SOURCE)}`, + `export { structuredAgentSessionMessageSendMutation, agentSessionSendBodyFingerprint } from ${source(SEND_SOURCE)}` ].join(';'), resolveDir: absolute }, From 84d7265e6c1885c35a8876cf38e51cd9fe9e0159 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 03:01:40 -0800 Subject: [PATCH 41/45] Port cross-version wire tests to SQL journal reads Hosts take the shared domain database, reads go through named async journal reads and catch-up history pages, and released-build fixtures type the release's own synchronous journal API. --- ...gent-session-clear-old-client.unit.test.ts | 16 +- ...ession-outcome-chat-downgrade.unit.test.ts | 301 ------------------ ...ssion-resume-marker-downgrade.unit.test.ts | 33 +- ...ss-version-agent-session-wire.unit.test.ts | 14 +- .../cross-version-stop-note-scenarios.ts | 17 +- .../cross-version-stop-note-test-rig.ts | 12 +- ...nt-session-option-persistence.unit.test.ts | 2 +- .../released-chat-continuation.ts | 58 ++-- .../released-chat-journal.ts | 27 ++ .../cross-version-wire/released-chat-queue.ts | 5 +- .../released-chat-upgrade.unit.test.ts | 127 ++++---- .../released-chat-writers.ts | 22 +- ...ubmission-positions-downgrade.unit.test.ts | 34 +- 13 files changed, 205 insertions(+), 463 deletions(-) delete mode 100644 tests/e2e/cross-version-wire/agent-session-outcome-chat-downgrade.unit.test.ts create mode 100644 tests/e2e/cross-version-wire/released-chat-journal.ts diff --git a/tests/e2e/cross-version-wire/agent-session-clear-old-client.unit.test.ts b/tests/e2e/cross-version-wire/agent-session-clear-old-client.unit.test.ts index 5c4d1de79c0..bfcea5cf7ec 100644 --- a/tests/e2e/cross-version-wire/agent-session-clear-old-client.unit.test.ts +++ b/tests/e2e/cross-version-wire/agent-session-clear-old-client.unit.test.ts @@ -12,6 +12,7 @@ import { HOST_TEST_SESSION as SESSION, hostTestOperationId } from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture' +import { readTestHostJournal } from '../../../src/main/native-chat/agent-session-journal/journal-observation.test-support' import { agentJournalSubmissionKey } from '../../../src/shared/agent-session/journal/agent-session-journal-item-key' import { EMPTY_STRUCTURED_AGENT_SESSION } from '../../../src/shared/agent-session/structured-agent-session-reducer' import type { useNativeChatRewind } from '../../../src/renderer/src/components/native-chat/use-native-chat-rewind' @@ -52,7 +53,7 @@ test('an older desktop releases its clear hold from the actual same-conversation } const rig = await createQueuedMessageTestRig({ restartable: true }) try { - const before = await rig.host.journalSnapshot(SESSION) + const before = await readTestHostJournal(rig.host, SESSION) const view = renderHook(() => useCommand(SESSION, async () => { const fields = { command: 'clear' as const } @@ -79,7 +80,7 @@ test('an older desktop releases its clear hold from the actual same-conversation }) }) expect(held(SESSION)).toBe(false) - const after = await rig.host.journalSnapshot(SESSION) + const after = await readTestHostJournal(rig.host, SESSION) expect(after.cursor.epoch).toBe(before.cursor.epoch) expect(after.items.slice(0, before.items.length)).toEqual(before.items) expect(after.items.at(-1)?.body).toMatchObject({ presentation: 'context-cleared' }) @@ -134,7 +135,7 @@ test('an older rewind-capable desktop releases its temporary same-epoch hold wit ).toMatchObject({ ok: true }) await send('keep this current-context message', 'kept') const target = await send('return this message to the draft', 'target') - const before = await rig.host.journalSnapshot(SESSION) + const before = await readTestHostJournal(rig.host, SESSION) const fields = { itemId: target, expectedEpoch: before.cursor.epoch } const result = await rig.host.rewind(CALLER, { ...fields, @@ -144,13 +145,16 @@ test('an older rewind-capable desktop releases its temporary same-epoch hold wit if (!result.ok) { throw new Error('suffix rewind failed') } - const after = await rig.host.journalSnapshot(SESSION) + const after = await readTestHostJournal(rig.host, SESSION) expect(after.cursor.epoch).toBe(before.cursor.epoch) expect(result.value.sequence).toBe(after.cursor.sequence) expect(after.items.map((item) => item.body)).toEqual( before.items.filter((item) => item.itemId !== target).map((item) => item.body) ) - const floor = rig.host.collaboratorsForTests().sessions.get(SESSION)!.journal.context.floor()! + const floor = (await rig.host + .collaboratorsForTests() + .sessions.get(SESSION)! + .journal.reads.contextFloor())! expect(after.items.filter((item) => item.sequence <= floor.sequence)).toEqual( before.items.filter((item) => item.sequence <= floor.sequence) ) @@ -189,7 +193,7 @@ test('an older rewind-capable desktop releases its temporary same-epoch hold wit expect(view.result.current.pending).toBe(false) expect(view.result.current.blockedRef.current).toBe(false) expect(toastError).toHaveBeenCalledOnce() - expect(await rig.host.journalSnapshot(SESSION)).toEqual(after) + expect(await readTestHostJournal(rig.host, SESSION)).toEqual(after) } finally { cleanup() vi.useRealTimers() diff --git a/tests/e2e/cross-version-wire/agent-session-outcome-chat-downgrade.unit.test.ts b/tests/e2e/cross-version-wire/agent-session-outcome-chat-downgrade.unit.test.ts deleted file mode 100644 index 00e653bd7bd..00000000000 --- a/tests/e2e/cross-version-wire/agent-session-outcome-chat-downgrade.unit.test.ts +++ /dev/null @@ -1,301 +0,0 @@ -import { createHash } from 'node:crypto' -import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs' -import { tmpdir } from 'node:os' -import { join } from 'node:path' -import { isDeepStrictEqual } from 'node:util' -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../src/shared/agent-session/journal/agent-session-journal-types' -import type { - AgentSessionJournalIdentity, - AgentSessionJournalProviderHandle -} from '../../../src/shared/agent-session/journal/agent-session-journal-identity' -import { codexProviderHandle } from '../../../src/shared/agent-session/lifecycle/agent-session-provider-handle-encoding' -import Database from '../../../src/main/sqlite/sync-database' -import { - closeTestJournalHostDatabase, - createTrackedJournalOpener, - liveTestJournalRows, - openTestJournalHostDatabase, - SAVED_BY_NEWER_ORCA -} from '../../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' -import { journalOutcomeSource } from '../../../src/main/native-chat/agent-session-journal/outcomes/journal-outcome-format' -import { restoreJournal } from '../../../src/main/native-chat/agent-session-journal/replay/journal-open' -import type { JournalStoredRow } from '../../../src/main/native-chat/agent-session-journal/durable-rows/journal-row-table' -import type { AgentSessionJournal } from '../../../src/main/native-chat/agent-session-journal/journal-store' -import { - importReleaseCheckoutModule, - materializeReleaseCheckout, - releaseModuleExport, - resolveBaselineReleaseRef -} from './release-checkout' - -// A chat this build has written, new or an existing one whose input rows an older build wrote, is -// refused by each of the two last releases before outcome storage as a newer Orca's, which leaves -// every byte as it was; a build from before that reason opens it read-only, as it does any newer -// row, and changes no saved row (it only adds a table of its own). The existing chat's input rows -// stay byte for byte throughout, and this build reads the whole chat again afterwards. -const SUITE_TIMEOUT_MS = 180_000 -// A main build sharing this database and schema; the releases in the window are probed as well. -// v1.4.246 shipped outcome storage and reads these chats itself, so the refusing window ends before it. -const REFUSING_RELEASE_REFS = ['v1.4.245', 'v1.4.244'] as const -const WRITABLE_BASELINE_REF = '3727100cc9dbcea6201f8a3e506676a3c4b53b18' -const JOURNAL = 'src/main/native-chat/agent-session-journal' - -const IDENTITY: AgentSessionJournalIdentity = { - sessionId: 'session-outcome-chat', - workspaceId: 'ws-1', - hostId: 'host-1', - agent: 'codex', - providerHandle: codexProviderHandle('thread-1') -} -/** The identity as builds before the neutral provider handle took it. */ -const OLDER_HANDLE: AgentSessionJournalProviderHandle = { kind: 'codex', threadId: 'thread-1' } -const OLDER_IDENTITY = { ...IDENTITY, providerHandle: OLDER_HANDLE } - -type ReleasedJournal = { - snapshot: () => { items: unknown[] } - appendItem: (identity: unknown, body: unknown, options: unknown) => Promise -} -type ReleasedOpener = { - open: (options: { identity: unknown; stateDirectory: string }) => Promise - closeAll: () => Promise -} - -let directory: string -beforeEach(() => { - directory = mkdtempSync(join(tmpdir(), 'orca-outcome-chat-xv-')) -}) -afterEach(() => { - rmSync(directory, { recursive: true, force: true }) -}) - -const appendStatus = (journal: Pick, ordinal: number) => - journal.appendItem( - { provider: 'codex', threadId: 'thread-1', turnId: 'turn-1', ordinal }, - { kind: 'status', text: `item ${ordinal}` }, - { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } - ) - -/** A chat this build writes: new, or existing with three input rows of items an older build wrote - * before this build's first write. Returns that prefix as stored. */ -async function savedChat(kind: 'new' | 'existing'): Promise { - let prefix: JournalStoredRow[] = [] - if (kind === 'existing') { - const older = createTrackedJournalOpener({ legacyInputs: true }) - const journal = await older.open({ identity: IDENTITY, stateDirectory: directory }) - for (let ordinal = 0; ordinal < 3; ordinal++) { - await appendStatus(journal, ordinal) - } - await older.closeAll() - prefix = liveTestJournalRows(openTestJournalHostDatabase(directory).db, IDENTITY.sessionId) - expect(prefix).toHaveLength(4) - } - const journals = createTrackedJournalOpener() - const journal = await journals.open({ identity: IDENTITY, stateDirectory: directory }) - for (let ordinal = 3; ordinal < 6; ordinal++) { - await appendStatus(journal, ordinal) - } - await journals.closeAll() - const database = openTestJournalHostDatabase(directory) - expect(journalOutcomeSource(database.db, IDENTITY.sessionId, journal.epoch)).toMatchObject({ - prefix: prefix.length - }) - const rows = liveTestJournalRows(database.db, IDENTITY.sessionId) - expect(rows.slice(0, prefix.length)).toEqual(prefix) - // One appended marker, after the prefix, where outcome records begin. - expect(rows.map((row) => row.seq)).toEqual([...prefix.map((row) => row.seq), prefix.length + 1]) - await closeTestJournalHostDatabase(directory) - return prefix -} - -/** The database file and its log, by content; SQLite's shared-memory index is not data. */ -function databaseBytes(): Record { - return Object.fromEntries( - ['agent-session-journal.db', 'agent-session-journal.db-wal'] - .filter((file) => existsSync(join(directory, file))) - .map((file) => [ - file, - createHash('sha256') - .update(readFileSync(join(directory, file))) - .digest('hex') - ]) - ) -} - -/** Every table's schema and rows, in a stable order. */ -function savedContent(): Map { - const db = new Database(join(directory, 'agent-session-journal.db'), { readonly: true }) - try { - const content = new Map() - for (const { name, sql } of db - .prepare("SELECT name, sql FROM sqlite_schema WHERE type = 'table' ORDER BY name") - .all()) { - content.set(String(name), { - sql, - rows: db.prepare(`SELECT * FROM "${String(name)}" ORDER BY rowid`).all() - }) - } - return content - } finally { - db.close() - } -} - -describe('a chat this build wrote, opened by a shipped build', () => { - it.each([ - ['new', 'the last release before outcome storage'], - ['existing', 'the last release before outcome storage'], - ['new', 'the release before it'], - ['existing', 'the release before it'], - ['new', 'the writable baseline'], - ['existing', 'the writable baseline'] - ] as const)( - 'keeps a %s chat from %s, leaving every byte as it was, and reads whole again after', - async (kind, build) => { - const prefix = await savedChat(kind) - const before = databaseBytes() - const saved = savedContent() - expect(before['agent-session-journal.db']).toBeDefined() - const inWindow = build !== 'the writable baseline' - const checkout = await materializeReleaseCheckout( - build === 'the last release before outcome storage' - ? REFUSING_RELEASE_REFS[0] - : inWindow - ? REFUSING_RELEASE_REFS[1] - : WRITABLE_BASELINE_REF - ) - const support = await importReleaseCheckoutModule( - checkout, - `${JOURNAL}/journal-host-database-test-support.ts` - ) - const older = releaseModuleExport<() => ReleasedOpener>( - support, - 'createTrackedJournalOpener' - )() - const close = releaseModuleExport<(stateDirectory: string) => void>( - support, - 'closeTestJournalHostDatabase' - ) - try { - if (inWindow) { - const failure = await older.open({ identity: IDENTITY, stateDirectory: directory }).then( - () => null, - (error: unknown) => error - ) - expect(failure).toMatchObject(SAVED_BY_NEWER_ORCA) - } else { - const opened = await older.open({ identity: OLDER_IDENTITY, stateDirectory: directory }) - // It latches read-only at the marker, having read only the rows before it. - expect(opened.snapshot().items).toHaveLength(Math.max(prefix.length - 1, 0)) - await expect( - opened.appendItem( - { provider: 'codex', threadId: 'thread-1', turnId: 'turn-1', ordinal: 9 }, - { kind: 'status', text: 'from the older build' }, - { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } - ) - ).rejects.toMatchObject({ code: 'journal_read_only' }) - } - } finally { - await older.closeAll() - close(directory) - } - if (inWindow) { - expect(databaseBytes()).toEqual(before) - } else { - const after = savedContent() - expect( - [...saved].filter(([name, content]) => !isDeepStrictEqual(after.get(name), content)) - ).toEqual([]) - } - // Upgraded again, this build reads the prefix and its records, the rows still as they were. - const database = openTestJournalHostDatabase(directory) - try { - const reopened = restoreJournal(database.db, IDENTITY.sessionId) - expect(reopened).toMatchObject({ newer: null, damage: null }) - expect(reopened?.state.items.size).toBe(kind === 'existing' ? 6 : 3) - const rows = liveTestJournalRows(database.db, IDENTITY.sessionId) - expect(rows.slice(0, prefix.length)).toEqual(prefix) - } finally { - await closeTestJournalHostDatabase(directory) - } - }, - SUITE_TIMEOUT_MS - ) -}) - -// Checks out and loads the newest release's own journal code, which takes well past the default. -it( - 'leaves an existing chat whose first write from this build failed as it was, so the newest release opens and writes it', - async () => { - const older = createTrackedJournalOpener({ legacyInputs: true }) - const seeded = await older.open({ identity: IDENTITY, stateDirectory: directory }) - for (let ordinal = 0; ordinal < 3; ordinal++) { - await appendStatus(seeded, ordinal) - } - await older.closeAll() - const prefix = liveTestJournalRows( - openTestJournalHostDatabase(directory).db, - IDENTITY.sessionId - ) - await closeTestJournalHostDatabase(directory) - - const journals = createTrackedJournalOpener() - const journal = await journals.open({ identity: IDENTITY, stateDirectory: directory }) - const database = openTestJournalHostDatabase(directory) - // The first write fails after its marker is in: at a record, then at COMMIT. - database.db.exec(`CREATE TEMP TRIGGER failure BEFORE INSERT ON main.journal_outcomes - BEGIN SELECT RAISE(ABORT, 'record failure'); END`) - await expect(appendStatus(journal, 3)).rejects.toThrow('record failure') - database.db.exec('DROP TRIGGER failure') - const exec = database.db.exec.bind(database.db) - const commit = vi.spyOn(database.db, 'exec').mockImplementation((sql) => { - if (sql === 'COMMIT') { - throw new Error('commit failure') - } - exec(sql) - }) - await expect(appendStatus(journal, 4)).rejects.toThrow('commit failure') - commit.mockRestore() - expect(journalOutcomeSource(database.db, IDENTITY.sessionId, journal.epoch)).toBeNull() - expect( - database.db.prepare('SELECT COUNT(*) AS n FROM journal_outcome_generations').get()?.n - ).toBe(0) - expect(liveTestJournalRows(database.db, IDENTITY.sessionId)).toEqual(prefix) - await journals.closeAll() - - const checkout = await materializeReleaseCheckout(resolveBaselineReleaseRef()) - const support = await importReleaseCheckoutModule( - checkout, - `${JOURNAL}/journal-host-database-test-support.ts` - ) - const release = releaseModuleExport<() => ReleasedOpener>( - support, - 'createTrackedJournalOpener' - )() - try { - const opened = await release.open({ identity: IDENTITY, stateDirectory: directory }) - expect(opened.snapshot().items).toHaveLength(3) - await opened.appendItem( - { provider: 'codex', threadId: 'thread-1', turnId: 'turn-1', ordinal: 9 }, - { kind: 'status', text: 'from the release' }, - { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } - ) - } finally { - await release.closeAll() - releaseModuleExport<(stateDirectory: string) => void>( - support, - 'closeTestJournalHostDatabase' - )(directory) - } - const reopened = openTestJournalHostDatabase(directory) - try { - expect(restoreJournal(reopened.db, IDENTITY.sessionId)?.state.items.size).toBe(4) - expect(liveTestJournalRows(reopened.db, IDENTITY.sessionId).slice(0, prefix.length)).toEqual( - prefix - ) - } finally { - await closeTestJournalHostDatabase(directory) - } - }, - SUITE_TIMEOUT_MS -) diff --git a/tests/e2e/cross-version-wire/agent-session-resume-marker-downgrade.unit.test.ts b/tests/e2e/cross-version-wire/agent-session-resume-marker-downgrade.unit.test.ts index 22c8933fe4a..300eb8ae185 100644 --- a/tests/e2e/cross-version-wire/agent-session-resume-marker-downgrade.unit.test.ts +++ b/tests/e2e/cross-version-wire/agent-session-resume-marker-downgrade.unit.test.ts @@ -2,15 +2,17 @@ import { mkdtempSync, rmSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { expect, test } from 'vitest' +import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../src/shared/agent-session/journal/agent-session-journal-types' +import { codexProviderHandle } from '../../../src/shared/agent-session/lifecycle/agent-session-provider-handle-encoding' +import { createTrackedJournalOpener } from '../../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import { structuredAgentSessionWorkingAtStop } from '../../../src/main/native-chat/agent-session-wire/termination/structured-agent-session-working-at-teardown' import { - journal, NOW, record, SESSION, TEARDOWN_CURRENT, - turnItem -} from '../../../src/main/native-chat/agent-session-wire/recovery/testing/structured-agent-session-restart-resume-test-harness' + THREAD +} from '../../../src/main/native-chat/agent-session-wire/recovery/testing/structured-agent-session-restart-resume.test-fixture' import { AgentSessionRecoveryCapsule } from '../../../src/main/runtime/agent-session/records/agent-session-recovery-capsule' import { importReleaseCheckoutModule, materializeReleaseCheckout } from './release-checkout' import { prepareReleaseFixtures } from './release-fixture-setup' @@ -26,14 +28,28 @@ const releaseCapsule = prepareReleaseFixtures([BASELINE_REF], async (ref) => test('an older build reads the restart offer this build records at quit', async () => { const directory = mkdtempSync(join(tmpdir(), 'orca-resume-marker-downgrade-')) + const journals = createTrackedJournalOpener() try { // This build: the offer teardown takes for a chat whose turn was running. - const marker = structuredAgentSessionWorkingAtStop({ - sessionId: SESSION, - session: { - journal: journal([turnItem('turn-1', 'running')]), - child: { fence: 1, phase: 'ready' } + const journal = await journals.open({ + identity: { + sessionId: SESSION, + workspaceId: 'workspace-1', + hostId: 'local', + agent: 'codex', + providerHandle: codexProviderHandle(THREAD) }, + stateDirectory: directory, + now: () => NOW + }) + await journal.appendItem( + { provider: 'codex', threadId: THREAD, turnId: 'turn-1', ordinal: 0 }, + { kind: 'turn', turnId: 'turn-1', state: 'running', startedAt: NOW }, + { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } + ) + const marker = await structuredAgentSessionWorkingAtStop({ + sessionId: SESSION, + session: { journal, child: { fence: 1, phase: 'ready' } }, getRecord: () => record(), childWork: () => undefined, childWorkOpen: () => false, @@ -54,6 +70,7 @@ test('an older build reads the restart offer this build records at quit', async } expect(await new OldCapsule(directory).list(NOW)).toMatchObject([{ sessionId: SESSION }]) } finally { + await journals.closeAll() rmSync(directory, { recursive: true, force: true }) } }) diff --git a/tests/e2e/cross-version-wire/cross-version-agent-session-wire.unit.test.ts b/tests/e2e/cross-version-wire/cross-version-agent-session-wire.unit.test.ts index 3a38fa9d44d..73713882490 100644 --- a/tests/e2e/cross-version-wire/cross-version-agent-session-wire.unit.test.ts +++ b/tests/e2e/cross-version-wire/cross-version-agent-session-wire.unit.test.ts @@ -11,7 +11,10 @@ import { emptyAgentSessionBatch } from '../../../src/main/native-chat/agent-sess import { startsWhenPublished } from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-instant-start.test-fixture' import { setStructuredAgentSessionHost } from '../../../src/main/native-chat/agent-session-wire/host/structured-agent-session-registry' import type { AgentSessionRecordStore } from '../../../src/main/runtime/agent-session/records/agent-session-record-store' -import { openTestAgentSessionRecordStore } from '../../../src/main/runtime/agent-session/records/agent-session-record-store-test-harness' +import { + openTestAgentSessionDatabase, + openTestAgentSessionRecordStore +} from '../../../src/main/runtime/agent-session/records/agent-session-record-store-test-harness' import type { AgentSessionSubscribeEvent } from '../../../src/shared/agent-session/agent-session-wire' import { AGENT_SESSION_ACCEPTED_SEND_RUNTIME_CAPABILITY, @@ -53,10 +56,11 @@ import { type RpcClientIdentity, type RpcReply } from './versioned-agent-session-wire' +import { readTestHostJournal } from '../../../src/main/native-chat/agent-session-journal/journal-observation.test-support' import * as journalHostDatabase from '../../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import { createStructuredAgentSessionLogger } from '../../../src/main/native-chat/agent-session-wire/host/structured-agent-session-logger' import { codexProviderHandle } from '../../../src/shared/agent-session/lifecycle/agent-session-provider-handle-encoding' -import { NO_STRUCTURED_AGENTS } from '../../../src/main/native-chat/agent-session-wire/providers/testing/structured-agent-session-adapter-router-test-support' +import { NO_STRUCTURED_AGENTS } from '../../../src/main/native-chat/agent-session-wire/providers/testing/structured-agent-session-adapter-router.test-fixture' import { describeReleasedStopNoteProjection } from './cross-version-stop-note-scenarios' const SUITE_TIMEOUT_MS = 180_000 @@ -357,7 +361,7 @@ describe('cross-version structured agent sessions', () => { spawnToken: 'spawn-vault', host: () => host }), - journalDatabase: journalHostDatabase.openTestJournalHostDatabase(root), + journalDatabase: await openTestAgentSessionDatabase(root), claimKeyId: 'key-1', mintSpawnToken: () => 'spawn-vault', now: () => NOW @@ -585,7 +589,7 @@ describe('cross-version structured agent sessions', () => { logger: createStructuredAgentSessionLogger(), store, adapter: adapter(() => host), - journalDatabase: journalHostDatabase.openTestJournalHostDatabase(root), + journalDatabase: await openTestAgentSessionDatabase(root), claimKeyId: 'key-1', mintSpawnToken: () => `spawn-${generation}`, // The provider died with the host that spawned it, which is what makes @@ -757,7 +761,7 @@ describe('cross-version structured agent sessions', () => { open() await vi.waitFor(async () => expect( - (await restarted.journalSnapshot(SESSION)).submissions.every( + (await readTestHostJournal(restarted, SESSION)).submissions.every( (row) => row.dispatchState !== 'pending' || row.handedOverAt ) ).toBe(true) diff --git a/tests/e2e/cross-version-wire/cross-version-stop-note-scenarios.ts b/tests/e2e/cross-version-wire/cross-version-stop-note-scenarios.ts index 44bc2a202c0..3735e04d8ed 100644 --- a/tests/e2e/cross-version-wire/cross-version-stop-note-scenarios.ts +++ b/tests/e2e/cross-version-wire/cross-version-stop-note-scenarios.ts @@ -6,6 +6,7 @@ import { AGENT_JOURNAL_THREAD_SCOPE, type AgentJournalItemBody } from '../../../src/shared/agent-session/journal/agent-session-journal-types' +import { readTestJournalItems } from '../../../src/main/native-chat/agent-session-journal/journal-observation.test-support' import { HISTORY_PAGE_CONTENT_BUDGET_BYTES } from '../../../src/main/native-chat/agent-session-wire/publication/history/agent-session-history-page-bounds' import { createReleasedStopNoteRig, @@ -27,8 +28,8 @@ export function describeReleasedStopNoteProjection(port: ScenarioPort): void { it('renders the derived page with the interrupted turn outside the page', async () => { await turn('interrupted') await note() - const snapshot = rig.journal.snapshot() - const stored = rig.journal.itemBody(noteId) + const items = await readTestJournalItems(rig.journal) + const stored = await rig.journal.reads.itemBody(noteId) const notePage = await page({ direction: 'tail', limit: 1 }) expect(notePage.items.map((item) => item.itemId)).toEqual([noteId]) const state = rig.client.reduceStructuredAgentSession( @@ -39,8 +40,8 @@ export function describeReleasedStopNoteProjection(port: ScenarioPort): void { } ) expect(render(state)).toMatchObject({ type: 'text', text: ordinary.text }) - expect(state.items[0]).toEqual(snapshot.items.find((item) => item.itemId === noteId)) - expect(rig.journal.itemBody(noteId)).toBe(stored) + expect(state.items[0]).toEqual(items.find((item) => item.itemId === noteId)) + expect(await rig.journal.reads.itemBody(noteId)).toEqual(stored) }) it.each([false, true])( 'renders the same-revision note with a live turn end (lifecycle batch=%s)', @@ -67,13 +68,7 @@ export function describeReleasedStopNoteProjection(port: ScenarioPort): void { }) const after = reduce(before, replies) expect(render(after)).toEqual({ type: 'text', text: ordinary.text }) - expect(rig.journal.itemBody(noteId)).toEqual(unconfirmed) - // The turn end is one saved sequence that touches only the turn, whichever writer made it. - const since = rig.journal.readSince(before.cursor!) - expect(since).toMatchObject({ - ok: true, - rows: [expect.objectContaining({ kind: 'outcome', itemIds: [turnItemId] })] - }) + expect(await rig.journal.reads.itemBody(noteId)).toEqual(unconfirmed) } ) it('refreshes a non-upgrade stale page after an out-of-window live correction at the terminal cursor', async () => { diff --git a/tests/e2e/cross-version-wire/cross-version-stop-note-test-rig.ts b/tests/e2e/cross-version-wire/cross-version-stop-note-test-rig.ts index e81d39a3ff7..9318eebcccb 100644 --- a/tests/e2e/cross-version-wire/cross-version-stop-note-test-rig.ts +++ b/tests/e2e/cross-version-wire/cross-version-stop-note-test-rig.ts @@ -3,15 +3,15 @@ import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, beforeAll, beforeEach, expect } from 'vitest' import { StructuredAgentSessionHost } from '../../../src/main/native-chat/agent-session-wire/host/structured-agent-session-host' -import { NO_STRUCTURED_AGENTS } from '../../../src/main/native-chat/agent-session-wire/providers/testing/structured-agent-session-adapter-router-test-support' +import { NO_STRUCTURED_AGENTS } from '../../../src/main/native-chat/agent-session-wire/providers/testing/structured-agent-session-adapter-router.test-fixture' import { startsWhenPublished } from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-instant-start.test-fixture' import { setStructuredAgentSessionHost } from '../../../src/main/native-chat/agent-session-wire/host/structured-agent-session-registry' import { createStructuredAgentSessionLogger } from '../../../src/main/native-chat/agent-session-wire/host/structured-agent-session-logger' -import { openTestAgentSessionRecordStore } from '../../../src/main/runtime/agent-session/records/agent-session-record-store-test-harness' import { - closeTestJournalHostDatabase, - openTestJournalHostDatabase -} from '../../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' + openTestAgentSessionDatabase, + openTestAgentSessionRecordStore +} from '../../../src/main/runtime/agent-session/records/agent-session-record-store-test-harness' +import { closeTestJournalHostDatabase } from '../../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import type { AgentSessionJournal } from '../../../src/main/native-chat/agent-session-journal/journal-store' import { agentJournalItemKey } from '../../../src/shared/agent-session/journal/agent-session-journal-item-key' import { agentSessionFailureFact } from '../../../src/shared/agent-session/failures/agent-session-failure' @@ -93,7 +93,7 @@ export function createReleasedStopNoteRig(port: ScenarioPort, ref: string) { host = new StructuredAgentSessionHost({ agents: NO_STRUCTURED_AGENTS, store, - journalDatabase: openTestJournalHostDatabase(root), + journalDatabase: await openTestAgentSessionDatabase(root), claimKeyId: 'key-1', logger: createStructuredAgentSessionLogger(), now: () => NOW, diff --git a/tests/e2e/cross-version-wire/paired-agent-session-option-persistence.unit.test.ts b/tests/e2e/cross-version-wire/paired-agent-session-option-persistence.unit.test.ts index 118593bc1e3..58cca738e27 100644 --- a/tests/e2e/cross-version-wire/paired-agent-session-option-persistence.unit.test.ts +++ b/tests/e2e/cross-version-wire/paired-agent-session-option-persistence.unit.test.ts @@ -13,7 +13,7 @@ import { agentModelCatalogFingerprintForRecord } from '../../../src/main/native- import { CLIENT_UI_METHODS } from '../../../src/main/runtime/rpc/methods/client-state/client-ui' import { STATUS_METHODS } from '../../../src/main/runtime/rpc/methods/host-management/status' import { STRUCTURED_AGENT_SESSION_METHODS } from '../../../src/main/runtime/rpc/methods/structured-sessions/structured-agent-session' -import { createRestTestRig } from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rest-test-rig' +import { createRestTestRig } from '../../../src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-rest-test-rig.test-fixture' import { HOST_TEST_SESSION as REST_TEST_SESSION, diff --git a/tests/e2e/cross-version-wire/released-chat-continuation.ts b/tests/e2e/cross-version-wire/released-chat-continuation.ts index 5e0568a91b4..ef68f15316f 100644 --- a/tests/e2e/cross-version-wire/released-chat-continuation.ts +++ b/tests/e2e/cross-version-wire/released-chat-continuation.ts @@ -3,6 +3,7 @@ import { parseAgentJournalItemKey } from '../../../src/shared/agent-session/jour import type { AgentJournalMessageItem } from '../../../src/shared/agent-session/journal/agent-session-journal-types' import type { JournalReplacementItem } from '../../../src/main/native-chat/agent-session-journal/context-epochs/journal-epoch-replacement' import type { AgentSessionJournal } from '../../../src/main/native-chat/agent-session-journal/journal-store' +import { readTestJournalItems } from '../../../src/main/native-chat/agent-session-journal/journal-observation.test-support' // What a user does next with a chat a released build wrote: edit a card it queued (the first write, // over a key the old rows hold), send, and rewind to before that send, which saves every kept message @@ -12,13 +13,7 @@ const FENCE = 1 export type ContinuedJournal = Pick< AgentSessionJournal, - | 'queuedMessages' - | 'appendSubmission' - | 'resolveDispatch' - | 'replaceEpochItems' - | 'snapshot' - | 'itemBody' - | 'itemProvenance' + 'queuedMessages' | 'appendSubmission' | 'resolveDispatch' | 'write' | 'reads' > const message = (text: string): AgentJournalMessageItem => ({ @@ -29,7 +24,7 @@ const message = (text: string): AgentJournalMessageItem => ({ /** The first write: an edit of a card the release queued and still holds. */ export async function editReleasedCard(journal: ContinuedJournal): Promise { - const card = journal.queuedMessages.list().find((entry) => entry.messageId === 'card-2') + const card = (await journal.queuedMessages.list()).find((entry) => entry.messageId === 'card-2') if (!card) { throw new Error('the release queued no card-2') } @@ -66,29 +61,32 @@ export async function rewindReleasedChat( journal: ContinuedJournal, keptItemIds: ReadonlySet ): Promise { - const kept = journal - .snapshot() - .items.filter((item) => keptItemIds.has(item.itemId)) - .map(({ itemId, observedAt, turnScope, ...linkage }): JournalReplacementItem => { - const body = journal.itemBody(itemId) - const identity = parseAgentJournalItemKey(itemId) - if (!body || !identity) { - throw new Error(`no kept item ${itemId}`) - } - const { ownerFence, executionOwner, ...echo } = journal.itemProvenance(itemId) - return { - identity, - ...echo, - body, - observedAt, - ...(turnScope ? { turnScope } : {}), - ownerFence: ownerFence ?? 0, - ...(executionOwner === undefined ? {} : { executionOwner }), - ...agentJournalLinkageFields(linkage) - } - }) + const kept = await Promise.all( + (await readTestJournalItems(journal)) + .filter((item) => keptItemIds.has(item.itemId)) + .map( + async ({ itemId, observedAt, turnScope, ...linkage }): Promise => { + const body = await journal.reads.itemBody(itemId) + const identity = parseAgentJournalItemKey(itemId) + if (!body || !identity) { + throw new Error(`no kept item ${itemId}`) + } + const { ownerFence, executionOwner, ...echo } = await journal.reads.itemProvenance(itemId) + return { + identity, + ...echo, + body, + observedAt, + ...(turnScope ? { turnScope } : {}), + ownerFence: ownerFence ?? 0, + ...(executionOwner === undefined ? {} : { executionOwner }), + ...agentJournalLinkageFields(linkage) + } + } + ) + ) if (kept.length !== keptItemIds.size) { throw new Error('a kept item is missing from the chat') } - await journal.replaceEpochItems('handle_forked', FENCE, kept) + await journal.write({ kind: 'epoch-replace', reason: 'handle_forked', fence: FENCE, items: kept }) } diff --git a/tests/e2e/cross-version-wire/released-chat-journal.ts b/tests/e2e/cross-version-wire/released-chat-journal.ts new file mode 100644 index 00000000000..b5a74d7c9fc --- /dev/null +++ b/tests/e2e/cross-version-wire/released-chat-journal.ts @@ -0,0 +1,27 @@ +import type { AgentJournalSnapshot } from '../../../src/shared/agent-session/journal/agent-session-journal-types' +import type { AgentSessionJournal } from '../../../src/main/native-chat/agent-session-journal/journal-store' +import type { TestJournalOptions } from '../../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' +import type { queueReleasedCards } from './released-chat-queue' + +/** A queued card as a release lists it, withdrawn ones included. */ +export type ReleasedCard = { messageId: string; position: number; state?: string } +/** A shipped build's journal: its reads and stop marks answer synchronously. */ +export type ReleasedJournal = Pick< + AgentSessionJournal, + | 'appendSubmission' + | 'resolveDispatch' + | 'appendItem' + | 'appendStopEvent' + | 'appendLifecycleBatch' + | 'appendQueueResume' + | 'appendTombstone' +> & { + queuedMessages: Parameters[0] & { list: () => ReleasedCard[] } + stopMarks: { beginSettle: (userStop: boolean) => unknown; settled: (settle: unknown) => void } + replaceEpochItems: (reason: string, fence: number, items: readonly unknown[]) => Promise + snapshot: () => AgentJournalSnapshot +} +export type ReleasedOpener = { + open: (options: TestJournalOptions) => Promise + closeAll: () => Promise +} diff --git a/tests/e2e/cross-version-wire/released-chat-queue.ts b/tests/e2e/cross-version-wire/released-chat-queue.ts index 77d406303fe..07bd25db51a 100644 --- a/tests/e2e/cross-version-wire/released-chat-queue.ts +++ b/tests/e2e/cross-version-wire/released-chat-queue.ts @@ -29,7 +29,10 @@ const message = (text: string): AgentJournalMessageItem => ({ }) export async function queueReleasedCards( - queue: AgentSessionJournal['queuedMessages'] + queue: Pick< + AgentSessionJournal['queuedMessages'], + 'insert' | 'update' | 'move' | 'hold' | 'withdraw' + > ): Promise { const table: unknown = queue if (isTableQueue(table)) { diff --git a/tests/e2e/cross-version-wire/released-chat-upgrade.unit.test.ts b/tests/e2e/cross-version-wire/released-chat-upgrade.unit.test.ts index aa0f1c25fb9..4b08c05fdec 100644 --- a/tests/e2e/cross-version-wire/released-chat-upgrade.unit.test.ts +++ b/tests/e2e/cross-version-wire/released-chat-upgrade.unit.test.ts @@ -1,7 +1,7 @@ import { cpSync, mkdtempSync, rmSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' -import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { describe, expect, it } from 'vitest' import { closeTestJournalHostDatabase, createTrackedJournalOpener, @@ -10,23 +10,21 @@ import { type TrackedJournalOpener } from '../../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import { restoreJournal } from '../../../src/main/native-chat/agent-session-journal/replay/journal-open' -import { JOURNAL_PREFIX_ORDINAL } from '../../../src/main/native-chat/agent-session-journal/outcomes/journal-outcome-codec' -import { journalOutcomeSource } from '../../../src/main/native-chat/agent-session-journal/outcomes/journal-outcome-format' import type { AgentJournalSnapshot } from '../../../src/shared/agent-session/journal/agent-session-journal-types' import { parseJournalRow } from '../../../src/main/native-chat/agent-session-journal/durable-rows/journal-row-schema' import { readJournalSessionEpoch, type JournalStoredRow } from '../../../src/main/native-chat/agent-session-journal/durable-rows/journal-row-table' -import { JournalWriteProjection } from '../../../src/main/native-chat/agent-session-journal/writing/journal-write-projection' -import { memoryJournalMap } from '../../../src/main/native-chat/agent-session-journal/journal-memory-collections' +import type { AgentSessionJournal } from '../../../src/main/native-chat/agent-session-journal/journal-store' +import { readTestJournalSnapshot } from '../../../src/main/native-chat/agent-session-journal/journal-observation.test-support' +import { readAgentSessionHistory } from '../../../src/main/native-chat/agent-session-wire/publication/history/agent-session-history-page' import { readPublishedQueuedMessages } from '../../../src/main/native-chat/agent-session-wire/delivery/structured-agent-session-queued-publication' import { resolveBaselineReleaseRef } from './release-checkout' import { editReleasedCard, rewindReleasedChat, - sendAfterUpgrade, - type ContinuedJournal + sendAfterUpgrade } from './released-chat-continuation' import { identityOf, @@ -34,13 +32,15 @@ import { writeReleasedChat, type Agent } from './released-chat-writers' +import type { ReleasedCard, ReleasedJournal, ReleasedOpener } from './released-chat-journal' // The two releases before outcome storage, and the oldest release whose chat database this build still // reads, writes a Claude and a Codex chat through its own writers. This build admits every row as // written, opens the chat as that release had it, then edits a queued card (its first write, over // a key the old rows hold), sends, and rewinds, which saves the kept chat again in this build's -// form. Every commit restores to the live chat. What this build does after the open is checked on -// its own result, never against a release, so main stays free to change it. +// form. The release's rows stay its own: this build converts them and never writes them. What this +// build does after the open is checked on its own result, never against a release, so main stays +// free to change it. const SUITE_TIMEOUT_MS = 180_000 // v1.4.246 shipped outcome storage, so its chats hold no rows to upgrade; the writers pin the two // releases before it, and the newest release stays the reader this build must match. @@ -53,47 +53,44 @@ const RELEASES = { } as const type Written = Awaited> -beforeEach(() => { - JournalWriteProjection.verifyEachCommit = true -}) -afterEach(() => { - JournalWriteProjection.verifyEachCommit = false +type Card = Pick & { state?: string } +const byPosition = (cards: readonly T[]): T[] => + [...cards].sort((a, b) => a.position - b.position) + +/** This build's chat and its live cards; a withdrawn card is no longer listed. */ +const view = async (journal: AgentSessionJournal) => ({ + snapshot: await readTestJournalSnapshot(journal), + cards: byPosition(await journal.queuedMessages.list()) }) -const view = (journal: ContinuedJournal) => { - const state = journal.queuedMessages.displayState() - const cards = - state.queue.cards instanceof Map - ? memoryJournalMap(state.queue.cards).values() - : state.queries.queueCardsForInitialization(state) - return { - snapshot: journal.snapshot(), - cards: [...cards].sort((a, b) => a.position - b.position) - } -} +/** A release's chat and its live cards, as that release reads them. */ +const releasedView = (journal: ReleasedJournal) => ({ + snapshot: journal.snapshot(), + cards: byPosition(journal.queuedMessages.list()).filter((card) => card.state !== 'withdrawn') +}) -/** What the release wrote: its items, sends and cards, in order. Fields a later build adds to them - * are not compared (`toMatchObject`), so main may add one; a changed value or order still fails. */ +/** What the release wrote: its items, sends and live cards, in order. Fields a later build adds to + * them are not compared (`toMatchObject`), so main may add one; a changed value or order still fails. */ const releaseWritten = ({ snapshot, cards }: { snapshot: AgentJournalSnapshot - cards: unknown[] + cards: readonly Card[] }) => ({ items: snapshot.items, submissions: snapshot.submissions, - cards + cards: cards.filter((card) => card.state !== 'withdrawn') }) -const kept = (items: ReturnType['items']) => +const kept = (items: AgentJournalSnapshot['items']) => items.map(({ itemId, body }) => ({ itemId, body })) -/** The chat as `opener`'s build opens it, over a copy so the original stays as the release left it. */ -async function openedBy(opener: TrackedJournalOpener, agent: Agent, stateDirectory: string) { +/** The chat as a release opens it, over a copy so the original stays as the release left it. */ +async function openedBy(opener: ReleasedOpener, agent: Agent, stateDirectory: string) { return opener .open({ identity: identityOf(agent), stateDirectory }) - .then(view) + .then(releasedView) .finally(() => opener.closeAll()) } @@ -114,20 +111,15 @@ async function carryOn( const carried = await opener .open(options) .then(async (journal) => { - const opened = view(journal) + const opened = await view(journal) await editReleasedCard(journal) if (prefix) { - // The release's rows stay byte for byte; one marker follows them, where records begin. + // The release's rows stay byte for byte; this build's writes go to its own store. const database = openTestJournalHostDatabase(stateDirectory) - const rows = liveTestJournalRows(database.db, options.identity.sessionId) - expect(rows.slice(0, prefix.length)).toEqual(prefix) - expect(rows).toHaveLength(prefix.length + 1) - expect( - journalOutcomeSource(database.db, options.identity.sessionId, journal.epoch)?.prefix - ).toBe(prefix.length) + expect(liveTestJournalRows(database.db, options.identity.sessionId)).toEqual(prefix) } await sendAfterUpgrade(journal) - const sent = view(journal) + const sent = await view(journal) expect(sent.snapshot.submissions).toContainEqual( expect.objectContaining({ clientMessageId: 'after-upgrade', dispatchState: 'accepted' }) ) @@ -136,24 +128,29 @@ async function carryOn( const physicalEpoch = readJournalSessionEpoch(database.db, options.identity.sessionId) expect(physicalEpoch).not.toBeNull() await rewindReleasedChat(journal, new Set(opened.snapshot.items.map((item) => item.itemId))) - expect(journal.epoch).toMatch(/^context-v1:/) - expect(journal.epoch).not.toBe(beforeRewind.epoch) + expect(journal.cursor().epoch).toMatch(/^context-v1:/) + expect(journal.cursor().epoch).not.toBe(beforeRewind.epoch) expect(readJournalSessionEpoch(database.db, options.identity.sessionId)).toBe(physicalEpoch) - expect(journal.readSince(beforeRewind)).toEqual({ ok: false, reset: 'epoch_changed' }) - const seen = view(journal) + // A client still at the old context is told to reload rather than sent a partial history. + expect( + await readAgentSessionHistory(journal, { + sessionId: options.identity.sessionId, + direction: 'after', + cursor: beforeRewind + }) + ).toMatchObject({ ok: false, reset: 'epoch_changed' }) + const seen = await view(journal) // The rewind keeps every message the release wrote and every card not taken back, the edit // included. expect(kept(seen.snapshot.items)).toEqual(kept(opened.snapshot.items)) - const active = seen.cards.filter((card) => card.state !== 'withdrawn') - expect(active.map((card) => card.messageId)).toEqual( - opened.cards.filter((card) => card.state !== 'withdrawn').map((card) => card.messageId) + expect(seen.cards.map((card) => card.messageId)).toEqual( + opened.cards.map((card) => card.messageId) ) - expect(active).toHaveLength(1) - expect(active).toEqual(sent.cards.filter((card) => card.state !== 'withdrawn')) - expect(seen.cards.filter((card) => card.state === 'withdrawn')).toEqual( - opened.cards.filter((card) => card.state === 'withdrawn') - ) - expect(readPublishedQueuedMessages(journal).map((card) => card.messageId)).toEqual(['card-2']) + expect(seen.cards).toHaveLength(1) + expect(seen.cards).toEqual(sent.cards) + expect((await readPublishedQueuedMessages(journal)).map((card) => card.messageId)).toEqual([ + 'card-2' + ]) for (const result of [sent, seen]) { expect(result.cards.find((card) => card.messageId === 'card-2')?.body.blocks).toEqual([ { type: 'text', text: 'second, edited after the upgrade' } @@ -164,8 +161,10 @@ async function carryOn( .finally(() => opener.closeAll()) const reopened = await opener .open(options) - .then((journal) => { - expect(readPublishedQueuedMessages(journal).map((card) => card.messageId)).toEqual(['card-2']) + .then(async (journal) => { + expect((await readPublishedQueuedMessages(journal)).map((card) => card.messageId)).toEqual([ + 'card-2' + ]) return view(journal) }) .finally(() => opener.closeAll()) @@ -216,20 +215,8 @@ describe('a chat a shipped release wrote, carried on by this build', () => { const ours = await carryOn(createTrackedJournalOpener(), agent, directory, written, rows) if (build !== 'the oldest release with this chat database') { - expect( - ours.seen.cards - .filter((card) => card.state === 'withdrawn') - .map((card) => card.messageId) - ).toEqual(['card-1']) - // A release in the window wrote the shapes this build reads, so it opens as written, - // and the edit records the card the old rows hold in its place among them. + // A release in the window wrote the shapes this build reads, so it opens as written. expect(releaseWritten(ours.opened)).toMatchObject(releaseWritten(written)) - const edits = openTestJournalHostDatabase(directory) - .db.prepare(`SELECT ordinal FROM journal_outcomes - WHERE field = 'queue.cards' AND entry_key = '"card-2"'`) - .all() - expect(edits).toContainEqual({ ordinal: JOURNAL_PREFIX_ORDINAL }) - await closeTestJournalHostDatabase(directory) } // This build opens the chat as the newest release does. const newest = await releasedJournalOpener(resolveBaselineReleaseRef()) diff --git a/tests/e2e/cross-version-wire/released-chat-writers.ts b/tests/e2e/cross-version-wire/released-chat-writers.ts index 649cca8f9dd..ce15857676b 100644 --- a/tests/e2e/cross-version-wire/released-chat-writers.ts +++ b/tests/e2e/cross-version-wire/released-chat-writers.ts @@ -17,7 +17,7 @@ import type { agentSessionFailureWords } from '../../../src/shared/agent-session import type { createClaudeJournalTranslator } from '../../../src/main/claude/structured/journal/claude-structured-journal-translation' import type { createCodexJournalTranslator } from '../../../src/main/codex/structured/journal/codex-structured-journal-translation' import type { createDeferredStructuredAgentSessionEventSink } from '../../../src/main/native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink' -import type { createTrackedJournalOpener } from '../../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' +import type { ReleasedCard, ReleasedJournal, ReleasedOpener } from './released-chat-journal' import { importReleaseCheckoutModule, materializeReleaseCheckout, @@ -33,8 +33,10 @@ const JOURNAL = 'src/main/native-chat/agent-session-journal' const FENCE = 1 export type Agent = 'claude' | 'codex' -type Opener = ReturnType -type Journal = Awaited> +type DeferredSink = ReturnType +type ReleasedSink = Pick & { + bind: (target: { journal: ReleasedJournal; fence: number; publish: () => void }) => void +} type Released = Awaited> export const identityOf = (agent: Agent): AgentSessionJournalIdentity => ({ @@ -60,7 +62,7 @@ async function released(ref: string) { ) return { root: checkout.root, - opener: await load( + opener: await load<() => ReleasedOpener>( `${JOURNAL}/journal-host-database-test-support.ts`, 'createTrackedJournalOpener' ), @@ -72,7 +74,7 @@ async function released(ref: string) { 'src/main/codex/structured/journal/codex-structured-journal-translation.ts', 'createCodexJournalTranslator' ), - sink: await load( + sink: await load<(options: Record) => ReleasedSink>( 'src/main/native-chat/agent-session-wire/structured-agent-session-event-sink.ts', 'createDeferredStructuredAgentSessionEventSink' ), @@ -89,7 +91,7 @@ const message = (text: string): AgentJournalMessageItem => ({ }) /** Sends, their dispatch results and queue edits, as the host's send and queue paths write them. */ -async function sendsAndQueue(journal: Journal, release: Released): Promise { +async function sendsAndQueue(journal: ReleasedJournal, release: Released): Promise { await journal.appendSubmission({ clientMessageId: 'sent', payloadFingerprint: 'fp-sent', @@ -146,7 +148,7 @@ function capture(release: Released, path: string): unknown[] { async function stream( release: Released, agent: Agent, - journal: Journal, + journal: ReleasedJournal, clock: { at: number } ): Promise { const sessionId = identityOf(agent).sessionId @@ -226,7 +228,7 @@ async function stream( /** A Stop with its settlement, a Resume, and an item later removed. */ async function stopAndRemove( - journal: Journal, + journal: ReleasedJournal, release: Released, clock: { at: number } ): Promise { @@ -268,7 +270,7 @@ export async function writeReleasedChat( ref: string, agent: Agent, stateDirectory: string -): Promise<{ snapshot: AgentJournalSnapshot; cards: unknown[]; now: () => number }> { +): Promise<{ snapshot: AgentJournalSnapshot; cards: ReleasedCard[]; now: () => number }> { const release = await released(ref) const clock = { at: 1_000 } const journals = release.opener() @@ -296,6 +298,6 @@ export async function writeReleasedChat( } /** The release's own journal opener, to carry a chat on as that release would. */ -export async function releasedJournalOpener(ref: string): Promise { +export async function releasedJournalOpener(ref: string): Promise { return (await released(ref)).opener() } diff --git a/tests/e2e/cross-version-wire/submission-positions-downgrade.unit.test.ts b/tests/e2e/cross-version-wire/submission-positions-downgrade.unit.test.ts index fc120b6196b..06b649b6a82 100644 --- a/tests/e2e/cross-version-wire/submission-positions-downgrade.unit.test.ts +++ b/tests/e2e/cross-version-wire/submission-positions-downgrade.unit.test.ts @@ -12,8 +12,10 @@ import { createTrackedJournalOpener, type TrackedJournalOpener } from '../../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' -import { projectJournalBatch } from '../../../src/main/native-chat/agent-session-wire/publication/history/agent-session-journal-batch' -import { readAgentSessionHydrationPage } from '../../../src/main/native-chat/agent-session-wire/publication/history/agent-session-history-page' +import { + createAgentSessionCatchUpReader, + readAgentSessionHydrationPage +} from '../../../src/main/native-chat/agent-session-wire/publication/history/agent-session-history-page' import { importReleaseCheckoutModule, materializeReleaseCheckout, @@ -68,8 +70,8 @@ async function journalWithRejections(directory: string, journals: TrackedJournal fence: 1 }) } - const before = readAgentSessionHydrationPage(journal, 1) - const seen = journal.snapshot().cursor + const before = await readAgentSessionHydrationPage(journal, 1) + const seen = journal.cursor() const cancelled = agentSessionFailureWords(agentSessionFailureFact('cancelled'), { surface: 'rejection' }) @@ -101,18 +103,22 @@ async function journalWithRejections(directory: string, journals: TrackedJournal ...agentSessionFailureWords(agentSessionFailureFact('queueFull'), { surface: 'rejection' }), fence: 1 }) - const since = journal.readSince(seen) - const batch = since.ok - ? projectJournalBatch({ - rows: since.rows, - snapshot: journal.snapshot(), - afterSequence: seen.sequence - }) - : null - if (!batch?.ok) { + // The batch a subscriber's catch-up sends from the page after what it had seen. + const since = await createAgentSessionCatchUpReader(journal)({ + sessionId: IDENTITY.sessionId, + direction: 'after', + cursor: seen + }) + if (!since.ok) { throw new Error('the batch did not project') } - return { before, after: readAgentSessionHydrationPage(journal, 1), batch: batch.batch } + const batch = { + cursor: since.page.window.nextCursor, + items: since.page.items, + removedItemIds: since.page.removedItemIds, + submissions: since.page.submissions + } + return { before, after: await readAgentSessionHydrationPage(journal, 1), batch } } // Loads real release checkouts, cold extraction and transforms included. From def4262da2a7abe57d1d577cbd150bc13d29db79 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 03:01:42 -0800 Subject: [PATCH 42/45] Align cross-version downgrade tests with one-way conversion A downgraded build sees the chat and records as converted and its later writes are not carried forward. Drop tests whose subject was this build's legacy row and outcome writes; seed newer legacy rows before the first conversion. --- ...on-interrupted-tool-downgrade.unit.test.ts | 57 +--- ...sion-kill-delivered-downgrade.unit.test.ts | 198 ++++-------- ...nt-session-newer-chat-refusal.unit.test.ts | 68 ++-- ...-session-stop-event-downgrade.unit.test.ts | 294 ++++-------------- 4 files changed, 172 insertions(+), 445 deletions(-) diff --git a/tests/e2e/cross-version-wire/agent-session-interrupted-tool-downgrade.unit.test.ts b/tests/e2e/cross-version-wire/agent-session-interrupted-tool-downgrade.unit.test.ts index b4cdbc0bed2..d0d407676bb 100644 --- a/tests/e2e/cross-version-wire/agent-session-interrupted-tool-downgrade.unit.test.ts +++ b/tests/e2e/cross-version-wire/agent-session-interrupted-tool-downgrade.unit.test.ts @@ -9,8 +9,7 @@ import { } from '../../../src/shared/agent-journal-tool-call-lifecycle' import type { AgentJournalItemIdentity } from '../../../src/shared/agent-session/journal/agent-session-journal-identity' import { createTrackedJournalOpener } from '../../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' -import type { JournalRow } from '../../../src/main/native-chat/agent-session-journal/durable-rows/journal-row-schema' -import type { JournalReadRow } from '../../../src/main/native-chat/agent-session-journal/replay/journal-read-row' +import { readTestJournalItems } from '../../../src/main/native-chat/agent-session-journal/journal-observation.test-support' import { importReleaseCheckoutModule, materializeReleaseCheckout, @@ -24,7 +23,6 @@ const releaseToolModules = prepareReleaseFixtures([BASELINE_REF], async (ref) => const checkout = await materializeReleaseCheckout(ref) return Promise.all( [ - 'src/main/native-chat/agent-session-journal/journal-reducer.ts', 'src/shared/agent-session-journal-schemas.ts', 'src/shared/structured-agent-session-projection.ts', 'src/shared/native-chat-tool-run-outcome.ts' @@ -45,19 +43,10 @@ type OldRunOutcome = ( options: { activeTurnIsWorking?: boolean } ) => { failedCallCount: number; succeeded: boolean } -// Old builds fold input rows; an outcome row here means the probe opened a converted chat by mistake. -function inputRows(rows: readonly JournalReadRow[]): JournalRow[] { - return rows.map((row) => { - if (row.kind === 'outcome') { - throw new Error('expected input rows') - } - return row - }) -} // Loads a real old build, including cold extraction and transforms. -test('an older host and client read a call a stop cut short as the failure they always showed', async () => { +test('an older client reads a call a stop cut short as the failure they always showed', async () => { const directory = mkdtempSync(join(tmpdir(), 'orca-interrupted-tool-downgrade-')) - const journals = createTrackedJournalOpener({ legacyInputs: true }) + const journals = createTrackedJournalOpener() try { // This build: a call a stop cut short, with the partial output it left. const journal = await journals.open({ @@ -85,30 +74,13 @@ test('an older host and client read a call a stop cut short as the failure they 'unverifiable' ) await journal.appendItem(call(1), unverified, { fence: 1, turnScope: { kind: 'thread' } }) - const since = journal.readSince({ epoch: journal.epoch, sequence: 0 }) - if (!since.ok) { - throw new Error(`expected rows, got reset ${since.reset}`) - } - const rows = inputRows(since.rows) - const current = journal.snapshot().items + const current = await readTestJournalItems(journal) expect(current.map((item) => item.body)).toMatchObject([ { state: 'failed', endedAs: 'interrupted' }, { state: 'failed', endedAs: 'unverifiable' } ]) - const [reducer, schemas, projection, outcome] = releaseToolModules(BASELINE_REF) - const createState = releaseModuleExport<(sessionId: string, epoch: string) => unknown>( - reducer, - 'createJournalReducerState' - ) - const applyRow = releaseModuleExport<(state: unknown, row: JournalRow) => void>( - reducer, - 'applyJournalRow' - ) - const render = releaseModuleExport<(state: unknown) => { items: OldRenderItem[] }>( - reducer, - 'renderJournalState' - ) + const [schemas, projection, outcome] = releaseToolModules(BASELINE_REF) const renderItemSchema = releaseModuleExport<{ parse: (value: unknown) => OldRenderItem }>( schemas, 'AgentJournalRenderItemSchema' @@ -123,23 +95,8 @@ test('an older host and client read a call a stop cut short as the failure they const blocksOf = (items: OldRenderItem[]) => items.flatMap((item) => project(item)?.blocks ?? []) - // The older host, after a downgrade, folds the rows it never wrote as failed calls. - const state = createState(SESSION, journal.epoch) - for (const row of rows) { - applyRow(state, row) - } - const folded = render(state).items - expect(folded.map((item) => item.body)).toMatchObject([ - { kind: 'tool-call', state: 'failed' }, - { kind: 'tool-call', state: 'failed' } - ]) - expect(runOutcome(blocksOf(folded), settled)).toEqual({ - failedCallCount: 2, - succeeded: false - }) - - // The older client reads this host's render items the same way. - const read = current.map((item) => renderItemSchema.parse(item)) + // The older client reads this host's render items, as sent on the wire, as failed calls. + const read = current.map((item) => renderItemSchema.parse(JSON.parse(JSON.stringify(item)))) expect(runOutcome(blocksOf(read), settled)).toEqual({ failedCallCount: 2, succeeded: false diff --git a/tests/e2e/cross-version-wire/agent-session-kill-delivered-downgrade.unit.test.ts b/tests/e2e/cross-version-wire/agent-session-kill-delivered-downgrade.unit.test.ts index bdea85bf863..b8336581d1d 100644 --- a/tests/e2e/cross-version-wire/agent-session-kill-delivered-downgrade.unit.test.ts +++ b/tests/e2e/cross-version-wire/agent-session-kill-delivered-downgrade.unit.test.ts @@ -13,18 +13,17 @@ import { closeTestJournalHostDatabase, openTestJournalHostDatabase } from '../../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' -import { structuredAgentSessionLiveFence } from '../../../src/main/native-chat/agent-session-wire/observations/structured-agent-session-current-work' import { upgradeStoredStructuredAgentSessionOwnerExitEvidence } from '../../../src/main/native-chat/agent-session-wire/host/structured-agent-session-lease-release' import { turnVerdictFromDeathEvidence } from '../../../src/main/native-chat/agent-session-wire/termination/structured-agent-session-stale-turn-verdict' import { releaseStoredAgentSessionOwnerAfterSurfaceClose } from '../../../src/main/runtime/agent-session/ownership/agent-session-surface-release-transition' -import { loadAgentSessionStoreRows } from '../../../src/main/runtime/agent-session/records/agent-session-record-rows' import { + closeTestAgentSessionDatabase, openTestAgentSessionRecordStore, - seedTestAgentSessionRecordStore + storedTestAgentSessionRecord } from '../../../src/main/runtime/agent-session/records/agent-session-record-store-test-harness' import { importReleaseCheckoutModule, materializeReleaseCheckout } from './release-checkout' -// The last release before Stop recorded a delivered kill; it knows three death kinds. +// The last release before Stop recorded a delivered kill. const RELEASE_REF = 'v1.4.244' type OldStoreMaps = { @@ -69,15 +68,27 @@ function oldRecord(value: unknown) { } // Materializing the historical source checkout and importing its modules can exceed the default timeout. -test(`${RELEASE_REF} keeps a stopped chat readable through restart, resume and evidence replacement`, async () => { +// Conversion is one-way: a downgraded build keeps the records as converted, and what it writes +// later is not carried forward, so the upgraded store keeps this build's records. +test(`${RELEASE_REF} keeps the converted records readable and writable; the upgraded store keeps this build's stopped chat`, async () => { const directory = await mkdtemp(join(tmpdir(), 'orca-kill-downgrade-')) try { const live = agentSessionRecordFixture() const sessionId = live.sessionId - await seedTestAgentSessionRecordStore(directory, { records: [live] }) + // The chat as an older build saved it, before this build first converts the store. + const legacy = openTestJournalHostDatabase(directory).db + legacy + .prepare('INSERT INTO agent_session_records (session_id, record_json) VALUES (?, ?)') + .run(sessionId, JSON.stringify(storedTestAgentSessionRecord(live))) + const converted = String( + legacy + .prepare('SELECT record_json FROM agent_session_records WHERE session_id = ?') + .get(sessionId)?.record_json + ) + await closeTestJournalHostDatabase(directory) + const current = await openTestAgentSessionRecordStore(directory) - // Seed the host's already-attached owner; the Stop itself uses the real transition and commit. - await current.transitionHandoff(sessionId, () => live) + expect(current.getRecord(sessionId)?.lease.runtimeFence).toBe(7) await releaseStoredAgentSessionOwnerAfterSurfaceClose(current, { sessionId, expectedFence: 7, @@ -85,119 +96,76 @@ test(`${RELEASE_REF} keeps a stopped chat readable through restart, resume and e deathEvidenceKind: 'kill-delivered', exitReason: 'killed' }) - // Include optional proof facts in the same production serialization contract. - await current.transitionHandoff(sessionId, (record) => ({ - ...record, - lease: { - ...record.lease, - deathEvidence: record.lease.deathEvidence && { - ...record.lease.deathEvidence, - lastProvenAliveAt: 35_000, - runtimeEnd: 'quit' - } - } - })) - const evidence = current.getRecord(sessionId)?.lease.deathEvidence - expect(evidence).toEqual({ + const stopped = current.getRecord(sessionId) + expect(stopped?.lease.deathEvidence).toEqual({ kind: 'kill-delivered', detail: 'killed', observedAt: 40_000, - ownerFence: 7, - lastProvenAliveAt: 35_000, - runtimeEnd: 'quit' + ownerFence: 7 }) - const db = openTestJournalHostDatabase(directory).db - const saved = db - .prepare('SELECT record_json FROM agent_session_records WHERE session_id = ?') - .get(sessionId) - if (typeof saved?.record_json !== 'string') { - throw new Error('the current store must save the chat') - } - const raw = JSON.parse(saved.record_json) - expect(raw.lease.deathEvidence).toEqual({ ...evidence, kind: 'pid-absent', delivery: 'kill' }) + await closeTestAgentSessionDatabase(directory) const checkout = await materializeReleaseCheckout(RELEASE_REF) - const [records, rows, drafts, restart, adjudication, transitions, views] = await Promise.all([ - importReleaseCheckoutModule(checkout, 'src/shared/agent-session-record.ts'), + const [rows, drafts, restart] = await Promise.all([ importReleaseCheckoutModule(checkout, 'src/main/runtime/agent-session-record-rows.ts'), importReleaseCheckoutModule(checkout, 'src/main/runtime/agent-session-store-draft.ts'), importReleaseCheckoutModule( checkout, 'src/main/runtime/agent-session/ownership/agent-session-restart-lease-transitions.ts' - ), - importReleaseCheckoutModule(checkout, 'src/shared/agent-session-lease-adjudication.ts'), - importReleaseCheckoutModule( - checkout, - 'src/main/runtime/agent-session/ownership/agent-session-lease-transitions.ts' - ), - importReleaseCheckoutModule(checkout, 'src/main/runtime/agent-session-store-state.ts') + ) ]) - expect(exported(records, 'isPersistedAgentSessionRecord')(raw)).toBe(true) - const loaded = exported(rows, 'loadAgentSessionStoreRows')(db) - expect(storeMaps(loaded).unreadableRecords.size).toBe(0) - const old = oldRecord(storeMaps(loaded).records.get(sessionId)) + // The older build reads the records as converted, without this build's Stop. + const db = openTestJournalHostDatabase(directory).db expect( - exported( - adjudication, - 'adjudicateAgentSessionRestart' - )({ - lease: old.lease, - probe: { outcome: 'indeterminate', reason: 'no child after restart' }, - observedAt: 50_000 - }) - ).toMatchObject({ disposition: 'free' }) + db + .prepare('SELECT record_json FROM agent_session_records WHERE session_id = ?') + .get(sessionId)?.record_json + ).toBe(converted) + const loaded = storeMaps(exported(rows, 'loadAgentSessionStoreRows')(db)) + expect(loaded.unreadableRecords.size).toBe(0) + const old = oldRecord(loaded.records.get(sessionId)) + expect(old.lease).toMatchObject({ runtimeFence: 7, deathEvidence: null }) + // It writes its own restart reconciliation over them. const reconciled = oldRecord( exported( restart, 'applyAgentSessionRestartAdjudication' )({ record: old, - probe: { outcome: 'indeterminate', reason: 'no child after restart' }, + probe: { outcome: 'pid-absent' }, now: 50_000 }) ) - expect(reconciled.lease).toMatchObject({ - unreconciled: false, - runtimeFence: 8, - deathEvidence: raw.lease.deathEvidence - }) - const writeOld = (record: unknown) => { - const before = exported(rows, 'loadAgentSessionStoreRows')(db) - const draft = exported(drafts, 'draftAgentSessionStoreState')(before) - storeMaps(draft).records.set(sessionId, record) - exported(rows, 'writeAgentSessionStoreRows')( - db, - exported(drafts, 'agentSessionStoreDraftRowWrites')(before, draft) - ) - return draft - } - const oldState = writeOld(reconciled) - expect(exported(views, 'listAgentSessionConversations')(oldState)).toMatchObject([ - { sessionId, currentFence: 8 } - ]) - expect(exported(views, 'getAgentSessionConversation')(oldState, sessionId)).toMatchObject({ - sessionId, - ownerObservation: { unreconciled: false, claimStatus: 'released' } - }) - expect(exported(views, 'requireWritableAgentSessionRecord')(oldState, sessionId)).toEqual( - reconciled + const before = exported(rows, 'loadAgentSessionStoreRows')(db) + const draft = exported(drafts, 'draftAgentSessionStoreState')(before) + storeMaps(draft).records.set(sessionId, reconciled) + exported(rows, 'writeAgentSessionStoreRows')( + db, + exported(drafts, 'agentSessionStoreDraftRowWrites')(before, draft) ) + expect( + oldRecord(storeMaps(exported(rows, 'loadAgentSessionStoreRows')(db)).records.get(sessionId)) + ).toEqual(reconciled) + expect( + db + .prepare('SELECT record_json FROM agent_session_records WHERE session_id = ?') + .get(sessionId)?.record_json + ).not.toBe(converted) + await closeTestJournalHostDatabase(directory) + // Upgraded again over the older build's newer records: this build's stopped chat, unchanged. const upgraded = await openTestAgentSessionRecordStore(directory) const restored = upgraded.getRecord(sessionId) if (!restored) { throw new Error('the upgraded chat must remain readable') } - expect(restored.lease.deathEvidence).toEqual(evidence) - expect(restored.lease.runtimeFence).toBe(8) + expect(restored).toEqual(stopped) const conversation = agentSessionConversationView(restored) expect(agentSessionLeaseOwnerVerdict(restored.lease)).toBe('exited') expect(agentSessionLeasePossiblyWriting(restored.lease)).toBe(false) - expect(structuredAgentSessionLiveFence({ record: conversation })).toBeNull() expect(agentModelCatalogReleasedOwner(conversation)).toBe(true) - expect(turnVerdictFromDeathEvidence(restored.lease.deathEvidence, 7)).toEqual({ - state: 'interrupted', - completedAt: 35_000 + expect(turnVerdictFromDeathEvidence(restored.lease.deathEvidence, 7)).toMatchObject({ + state: 'interrupted' }) await upgradeStoredStructuredAgentSessionOwnerExitEvidence( upgraded, @@ -212,56 +180,12 @@ test(`${RELEASE_REF} keeps a stopped chat readable through restart, resume and e }, 61_000 ) - expect(loadAgentSessionStoreRows(db).records.get(sessionId)?.lease).toMatchObject({ - runtimeFence: 8, - deathEvidence: { ...evidence, kind: 'exit-observed', observedAt: 60_000 } + expect(upgraded.getRecord(sessionId)?.lease.deathEvidence).toMatchObject({ + kind: 'exit-observed', + detail: 'killed', + observedAt: 60_000, + ownerFence: 7 }) - - // Starting again in the old build ends the previous proof, including its marker. - const reservation = exported( - transitions, - 'reserveAgentSessionOwner' - )({ - record: reconciled, - expectedFence: 8, - probe: { outcome: 'pid-absent' }, - reservation: { - spawnToken: 'next-owner', - claimKeyId: 'key-1', - handoffOperationId: null, - leaseTtlMs: 30_000, - now: 70_000 - } - }) - if (typeof reservation !== 'object' || reservation === null || !('record' in reservation)) { - throw new Error('the old build must reserve a successor') - } - const reserved = oldRecord(reservation.record) - writeOld(reserved) - expect(loadAgentSessionStoreRows(db).records.get(sessionId)?.lease).toMatchObject({ - runtimeFence: 9, - claimStatus: 'reserved', - deathEvidence: null - }) - const replaced = oldRecord( - exported( - restart, - 'applyAgentSessionRestartAdjudication' - )({ - record: reserved, - probe: { outcome: 'reservation-unused' }, - now: 80_000 - }) - ) - writeOld(replaced) - const replacement = loadAgentSessionStoreRows(db).records.get(sessionId)?.lease.deathEvidence - expect(replacement).toEqual({ - kind: 'pid-absent', - detail: 'reservation never spawned', - observedAt: 80_000, - ownerFence: 9 - }) - expect(replaced.lease.deathEvidence).not.toHaveProperty('delivery') } finally { await closeTestJournalHostDatabase(directory) await rm(directory, { recursive: true, force: true }) diff --git a/tests/e2e/cross-version-wire/agent-session-newer-chat-refusal.unit.test.ts b/tests/e2e/cross-version-wire/agent-session-newer-chat-refusal.unit.test.ts index 2c97ef08f7b..f475877549c 100644 --- a/tests/e2e/cross-version-wire/agent-session-newer-chat-refusal.unit.test.ts +++ b/tests/e2e/cross-version-wire/agent-session-newer-chat-refusal.unit.test.ts @@ -10,6 +10,7 @@ import type { AgentSessionJournalIdentity, AgentSessionJournalProviderHandle } from '../../../src/shared/agent-session/journal/agent-session-journal-identity' +import { agentJournalItemKey } from '../../../src/shared/agent-session/journal/agent-session-journal-item-key' import { codexProviderHandle } from '../../../src/shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import type { AgentSessionRefusalReference } from '../../../src/shared/agent-session/failures/agent-session-wire-refusals' import { @@ -18,7 +19,8 @@ import { insertTestJournalRowJson, liveTestJournalRows, openTestJournalHostDatabase, - SAVED_BY_NEWER_ORCA + SAVED_BY_NEWER_ORCA, + seedLegacyTestJournal } from '../../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import { journalOpenRefusal } from '../../../src/main/native-chat/agent-session-journal/replay/journal-open-failure' import { @@ -60,7 +62,7 @@ type NoticeEnglish = (parts: unknown[]) => string type IsFinal = (refusal: AgentSessionRefusalReference | undefined) => boolean let directory: string -const journals = createTrackedJournalOpener({ legacyInputs: true }) +const journals = createTrackedJournalOpener() beforeAll(() => { directory = mkdtempSync(join(tmpdir(), 'orca-newer-chat-xv-')) @@ -84,39 +86,43 @@ const NEWER_NESTED_LITERAL = { /** A row of a kind this build does not know: the journal's open is refused as a newer Orca's. */ const NEWER_ROW_KIND = { kind: 'future-mark' } -// Write two current journal items, then a newer body or an unknown closed row. +const EPOCH = 'epoch-newer-chat' + +// A saved chat of two items, then a newer body or an unknown closed row, all before this build +// first opens it: this build reads saved rows only when it first converts the chat. async function journalWithNewerRow( row: Record = NEWER_ROW_KIND ): Promise<{ rows: string[]; newer: string }> { - const journal = await journals.open({ identity: IDENTITY, stateDirectory: directory }) - await journal.appendItem( - { provider: 'codex', threadId: 'thread-1', turnId: 'turn-1', ordinal: 0 }, - { kind: 'status', text: 'before' }, - { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } - ) - await journal.appendItem( - { provider: 'codex', threadId: 'thread-1', turnId: 'turn-1', ordinal: 2 }, - { kind: 'status', text: 'also before' }, - { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } - ) - const seq = journal.cursor().sequence + 1 - const newer = JSON.stringify({ - // A version the older build reads, so it meets the kind rather than a newer version. - v: AGENT_SESSION_JOURNAL_TURN_ITEM_SCHEMA_VERSION, - epoch: journal.epoch, - seq, - fence: 1, - ts: 2_000, - ...row - }) - await journals.closeAll() - insertTestJournalRowJson( - openTestJournalHostDatabase(directory).db, - IDENTITY.sessionId, - seq, - newer, - 2_000 + const database = openTestJournalHostDatabase(directory) + seedLegacyTestJournal(database, { identity: IDENTITY, mintEpoch: () => EPOCH, now: () => 1_000 }) + const stored = (seq: number, fields: Record) => + JSON.stringify({ + // A version the older build reads, so it meets the kind rather than a newer version. + v: AGENT_SESSION_JOURNAL_TURN_ITEM_SCHEMA_VERSION, + epoch: EPOCH, + seq, + fence: 1, + ts: 1_000 + seq, + ...fields + }) + const before = ['before', 'also before'].map((text, index) => ({ + kind: 'item', + itemId: agentJournalItemKey({ + provider: 'codex', + threadId: 'thread-1', + turnId: 'turn-1', + ordinal: index * 2 + }), + revision: 1, + turnScope: AGENT_JOURNAL_THREAD_SCOPE, + body: { kind: 'status', text } + })) + before.forEach((fields, index) => + insertTestJournalRowJson(database.db, IDENTITY.sessionId, index + 2, stored(index + 2, fields)) ) + const seq = before.length + 2 + const newer = stored(seq, row) + insertTestJournalRowJson(database.db, IDENTITY.sessionId, seq, newer, 2_000) return { rows: await storedRows(), newer } } diff --git a/tests/e2e/cross-version-wire/agent-session-stop-event-downgrade.unit.test.ts b/tests/e2e/cross-version-wire/agent-session-stop-event-downgrade.unit.test.ts index 3cfc59673d4..b270d28aab6 100644 --- a/tests/e2e/cross-version-wire/agent-session-stop-event-downgrade.unit.test.ts +++ b/tests/e2e/cross-version-wire/agent-session-stop-event-downgrade.unit.test.ts @@ -13,6 +13,8 @@ import type { } from '../../../src/shared/agent-session/journal/agent-session-journal-identity' import { codexProviderHandle } from '../../../src/shared/agent-session/lifecycle/agent-session-provider-handle-encoding' import Database from '../../../src/main/sqlite/sync-database' +import type { AgentSessionJournal } from '../../../src/main/native-chat/agent-session-journal/journal-store' +import { readTestJournalItems } from '../../../src/main/native-chat/agent-session-journal/journal-observation.test-support' import { journalDatabasePath } from '../../../src/main/native-chat/agent-session-journal/database/journal-host-database' import { closeTestJournalHostDatabase, @@ -20,30 +22,22 @@ import { insertTestJournalRowJson, liveTestJournalRows, openTestJournalHostDatabase, - SAVED_BY_NEWER_ORCA + SAVED_BY_NEWER_ORCA, + seedLegacyTestJournal } from '../../../src/main/native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' -import type { JournalRow } from '../../../src/main/native-chat/agent-session-journal/durable-rows/journal-row-schema' -import type { JournalReadRow } from '../../../src/main/native-chat/agent-session-journal/replay/journal-read-row' import { importReleaseCheckoutModule, materializeReleaseCheckout, releaseModuleExport } from './release-checkout' import { agentJournalItemKey } from '../../../src/shared/agent-session/journal/agent-session-journal-item-key' -import { agentSessionFailureFact } from '../../../src/shared/agent-session/failures/agent-session-failure' -import { agentSessionFailureWords } from '../../../src/shared/agent-session/failures/agent-session-failure-words' -import { - applyJournalRow, - createJournalReducerState, - renderJournalState -} from '../../../src/main/native-chat/agent-session-journal/projection/journal-reducer' -// Old readers would truncate at unknown row kinds; encode Stop/Resume/reopen evidence in tombstones they understand. -const BASELINE_REF = 'v1.4.218' const JOURNAL = 'src/main/native-chat/agent-session-journal' // Pin a main build sharing this database/schema for writable downgrade; move to the first release that ships it. const WRITABLE_BASELINE_REF = '3727100cc9dbcea6201f8a3e506676a3c4b53b18' +const EPOCH = 'epoch-saved-before-conversion' + const IDENTITY: AgentSessionJournalIdentity = { sessionId: 'session-downgrade', workspaceId: 'ws-1', @@ -62,186 +56,12 @@ const OLDER_IDENTITY: OlderJournalIdentity = { providerHandle: { kind: 'codex', threadId: 'thread-1' } } -// Old builds fold input rows; an outcome row here means the probe opened a converted chat by mistake. -function inputRows(rows: readonly JournalReadRow[]): JournalRow[] { - return rows.map((row) => { - if (row.kind === 'outcome') { - throw new Error('expected input rows') - } - return row - }) -} -test.each(['v1.4.219', 'v1.4.220', WRITABLE_BASELINE_REF])( - '%s replays current raw rows with the unconfirmed failure intact', - async (ref) => { - const directory = mkdtempSync(join(tmpdir(), 'orca-stop-note-raw-skew-')) - const journals = createTrackedJournalOpener({ legacyInputs: true }) - try { - const journal = await journals.open({ identity: IDENTITY, stateDirectory: directory }) - const turn = { provider: 'orca', clientMessageId: 'turn-raw-skew' } as const - const note = { provider: 'orca', clientMessageId: 'stop:turn-raw-skew' } as const - const unconfirmed = { - kind: 'status' as const, - ...agentSessionFailureWords(agentSessionFailureFact('cancelUnconfirmed'), { - surface: 'row' - }) - } - await journal.appendItem( - turn, - { - kind: 'status', - text: 'Interrupted', - turnLifecycle: { turnId: 'turn-raw-skew', state: 'interrupted' } - }, - { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } - ) - await journal.appendItem(note, unconfirmed, { - fence: 1, - turnScope: { kind: 'turn', turnItemId: agentJournalItemKey(turn) } - }) - const since = journal.readSince({ epoch: journal.epoch, sequence: 0 }) - if (!since.ok) { - throw new Error(since.reset) - } - const rawJson = JSON.stringify(since.rows) - const checkout = await materializeReleaseCheckout(ref) - const reducer = await importReleaseCheckoutModule(checkout, `${JOURNAL}/journal-reducer.ts`) - const create = releaseModuleExport<(sessionId: string, epoch: string) => OldReplay['state']>( - reducer, - 'createJournalReducerState' - ) - const fold = releaseModuleExport<(state: OldReplay['state'], row: JournalRow) => void>( - reducer, - 'applyJournalRow' - ) - const old = create(IDENTITY.sessionId, journal.epoch) - inputRows(since.rows).forEach((row) => fold(old, row)) - expect(old.items.get(agentJournalItemKey(note))).toMatchObject({ body: unconfirmed }) - const current = createJournalReducerState(IDENTITY.sessionId, journal.epoch) - inputRows(since.rows).forEach((row) => applyJournalRow(current, row)) - expect(current.items.get(agentJournalItemKey(note))?.body).toEqual(unconfirmed) - expect( - renderJournalState(current).items.find((item) => item.itemId === agentJournalItemKey(note)) - ?.body - ).toEqual({ kind: 'status', text: 'Cancellation requested.' }) - expect(JSON.stringify(since.rows)).toBe(rawJson) - expect(journal.itemBody(agentJournalItemKey(note))).toEqual(unconfirmed) - } finally { - await journals.closeAll() - rmSync(directory, { recursive: true, force: true }) - } - }, - 120_000 -) - function item(ordinal: number): AgentJournalItemIdentity { return { provider: 'codex', threadId: 'thread-1', turnId: 'turn-1', ordinal } } -type OldReplay = { - state: { items: Map } - readOnly: boolean - corrupt: boolean - malformedRows: number - truncateFrom?: number -} - -// Both downgrade probes load real old builds, including cold extraction and transforms. -test("an older build keeps every row around a Stop's event, a Resume and a reopen mark, and folds the rows after them", async () => { - const directory = mkdtempSync(join(tmpdir(), 'orca-stop-event-downgrade-')) - const journals = createTrackedJournalOpener({ legacyInputs: true }) - try { - // This build: history, a person's Stop, a Resume, then more history. - const journal = await journals.open({ identity: IDENTITY, stateDirectory: directory }) - const append = (ordinal: number, text: string) => - journal.appendItem( - item(ordinal), - { kind: 'status', text }, - { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } - ) - await append(0, 'before the Stop') - const beforeMarks = journal.cursor() - await journal.appendStopEvent({ reason: 'user-stop', turnId: 'turn-1', caller: 'client-1' }, 1) - await journal.appendStopEvent({ reason: 'user-close', turnId: 'turn-1' }, 1) - await journal.appendQueueResume(1) - await journal.appendQueueReopen(1) - const afterMarks = journal.cursor() - await append(1, 'after the Stop') - // As stored: a read raises each row's `v` to the newest this build reads, which the older - // build does not. - const rows: JournalRow[] = storedRows(directory).map((json) => JSON.parse(json)) - - // The older build, after a downgrade, replays the same rows from its own database. - const checkout = await materializeReleaseCheckout(BASELINE_REF) - const [database, table, open, reducer, batch] = await Promise.all( - [ - `${JOURNAL}/journal-database.ts`, - `${JOURNAL}/journal-row-table.ts`, - `${JOURNAL}/journal-open.ts`, - `${JOURNAL}/journal-reducer.ts`, - 'src/main/native-chat/agent-session-wire/agent-session-journal-batch.ts' - ].map((path) => importReleaseCheckoutModule(checkout, path)) - ) - const openJournalDatabase = releaseModuleExport< - (path: string) => { db: { close: () => void } } - >(database, 'openJournalDatabase') - const upsertJournalSessionRow = releaseModuleExport< - (...args: [unknown, string, string, number]) => void - >(table, 'upsertJournalSessionRow') - const insertJournalRow = releaseModuleExport<(...args: [unknown, string, JournalRow]) => void>( - table, - 'insertJournalRow' - ) - const replayJournal = releaseModuleExport< - (...args: [unknown, boolean, string]) => OldReplay | null - >(open, 'replayJournal') - const renderJournalState = releaseModuleExport<(state: unknown) => unknown>( - reducer, - 'renderJournalState' - ) - const projectJournalBatch = releaseModuleExport< - (input: { rows: readonly JournalRow[]; snapshot: unknown; afterSequence: number }) => { - ok: boolean - batch?: { items: unknown[]; removedItemIds: string[] } - } - >(batch, 'projectJournalBatch') - - const { db } = openJournalDatabase(join(directory, 'older-build-journal.sqlite')) - try { - upsertJournalSessionRow(db, IDENTITY.sessionId, journal.epoch, 1) - for (const row of rows) { - insertJournalRow(db, IDENTITY.sessionId, row) - } - const replayed = replayJournal(db, false, IDENTITY.sessionId) - expect(replayed).toMatchObject({ readOnly: false, corrupt: false, malformedRows: 0 }) - expect(replayed?.truncateFrom).toBeUndefined() - expect([...(replayed?.state.items.keys() ?? [])]).toHaveLength(2) - - // An older client is sent only ids no item uses, removed. - const projected = projectJournalBatch({ - rows: rows.filter( - (row) => row.seq > beforeMarks.sequence && row.seq <= afterMarks.sequence - ), - snapshot: renderJournalState(replayed?.state), - afterSequence: beforeMarks.sequence - }) - expect(projected.ok).toBe(true) - expect(projected.batch?.items).toEqual([]) - expect(projected.batch?.removedItemIds).toHaveLength(3) - const liveIds = new Set(journal.snapshot().items.map((entry) => entry.itemId)) - expect(projected.batch?.removedItemIds.some((id) => liveIds.has(id))).toBe(false) - } finally { - db.close() - } - } finally { - await journals.closeAll() - rmSync(directory, { recursive: true, force: true }) - } -}, 120_000) - type OlderJournal = { isReadOnly: boolean - cursor: () => { epoch: string; sequence: number } snapshot: () => { items: { itemId: string }[] } appendItem: (...args: [AgentJournalItemIdentity, unknown, unknown]) => Promise } @@ -263,21 +83,58 @@ function storedRows(directory: string): string[] { } } -test("an older build opens this build's journal writable and appends to it; the pause survives the round trip", async () => { +const SCOPE = { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } + +/** A chat an older build saved, before this build first converts it; returns the row writer. */ +function savedBeforeConversion(directory: string) { + const database = openTestJournalHostDatabase(directory) + seedLegacyTestJournal(database, { identity: IDENTITY, mintEpoch: () => EPOCH, now: () => 1_000 }) + return (seq: number, fields: Record): string => { + const row = JSON.stringify({ + // A version the older build reads, so it meets the kind rather than a newer version. + v: AGENT_SESSION_JOURNAL_TURN_ITEM_SCHEMA_VERSION, + epoch: EPOCH, + seq, + fence: 1, + ts: 1_000 + seq, + ...fields + }) + insertTestJournalRowJson(database.db, IDENTITY.sessionId, seq, row) + return row + } +} + +const statusRow = (ordinal: number, text: string) => ({ + kind: 'item', + itemId: agentJournalItemKey(item(ordinal)), + revision: 1, + turnScope: AGENT_JOURNAL_THREAD_SCOPE, + body: { kind: 'status', text } +}) + +// Conversion is one-way: a downgraded build sees the chat as converted, and its later writes +// are not carried forward, so the upgraded chat is this build's own. +test("an older build opens and appends to the chat as converted; upgraded again, the chat and its Stop pause are this build's", async () => { const directory = mkdtempSync(join(tmpdir(), 'orca-stop-event-writable-downgrade-')) - const journals = createTrackedJournalOpener({ legacyInputs: true }) - const itemIds = (journal: Pick) => - journal.snapshot().items.map((entry) => entry.itemId) + const journals = createTrackedJournalOpener() + const savedIds = async (journal: AgentSessionJournal) => + (await readTestJournalItems(journal)).map((entry) => entry.itemId) + const pauses = async (journal: AgentSessionJournal) => + (await journal.queuedMessages.pauses()).map((pause) => pause.reason) try { + savedBeforeConversion(directory)(2, statusRow(0, 'before the upgrade')) + await closeTestJournalHostDatabase(directory) + const converted = storedRows(directory) + const journal = await journals.open({ identity: IDENTITY, stateDirectory: directory }) - const scope = { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } - await journal.appendItem(item(0), { kind: 'status', text: 'before the Stop' }, scope) + await journal.appendItem(item(1), { kind: 'status', text: 'before the Stop' }, SCOPE) await journal.appendStopEvent({ reason: 'user-stop', turnId: 'turn-1', caller: 'client-1' }, 1) - await journal.appendItem(item(1), { kind: 'status', text: 'after the Stop' }, scope) - const wrote = { cursor: journal.cursor(), items: itemIds(journal) } - expect(journal.queuedMessages.pauses().map((pause) => pause.reason)).toEqual(['stopped']) + await journal.appendItem(item(2), { kind: 'status', text: 'after the Stop' }, SCOPE) + const wrote = { cursor: journal.cursor(), items: await savedIds(journal) } + expect(wrote.items).toEqual([0, 1, 2].map((ordinal) => agentJournalItemKey(item(ordinal)))) + expect(await pauses(journal)).toEqual(['stopped']) await journals.closeAll() - const rowsBefore = storedRows(directory) + expect(storedRows(directory)).toEqual(converted) const checkout = await materializeReleaseCheckout(WRITABLE_BASELINE_REF) const support = await importReleaseCheckoutModule( @@ -288,21 +145,21 @@ test("an older build opens this build's journal writable and appends to it; the try { const downgraded = await older.open({ identity: OLDER_IDENTITY, stateDirectory: directory }) expect(downgraded.isReadOnly).toBe(false) - expect(downgraded.cursor()).toEqual(wrote.cursor) - expect(itemIds(downgraded)).toEqual(wrote.items) - await downgraded.appendItem(item(2), { kind: 'status', text: 'the older build' }, scope) + expect(downgraded.snapshot().items.map((entry) => entry.itemId)).toEqual([ + agentJournalItemKey(item(0)) + ]) + await downgraded.appendItem(item(3), { kind: 'status', text: 'the older build' }, SCOPE) } finally { await older.closeAll() } const rowsAfter = storedRows(directory) - expect(rowsAfter.slice(0, rowsBefore.length)).toEqual(rowsBefore) - expect(rowsAfter).toHaveLength(rowsBefore.length + 1) + expect(rowsAfter.slice(0, converted.length)).toEqual(converted) + expect(rowsAfter).toHaveLength(converted.length + 1) - // Upgraded again: the older build's row folds, and the person's Stop still pauses the queue. const upgraded = await journals.open({ identity: IDENTITY, stateDirectory: directory }) - expect(upgraded.cursor().sequence).toBe(wrote.cursor.sequence + 1) - expect(itemIds(upgraded)).toEqual([...wrote.items, 'codex:thread-1:turn-1:2']) - expect(upgraded.queuedMessages.pauses().map((pause) => pause.reason)).toEqual(['stopped']) + expect(upgraded.cursor()).toEqual(wrote.cursor) + expect(await savedIds(upgraded)).toEqual(wrote.items) + expect(await pauses(upgraded)).toEqual(['stopped']) } finally { await journals.closeAll() rmSync(directory, { recursive: true, force: true }) @@ -313,30 +170,13 @@ test("an older build opens this build's journal writable and appends to it; the // Move the pinned baseline once a released reader preserves them. test("this build keeps a newer build's row kind and refuses the load; a build before it deletes it", async () => { const directory = mkdtempSync(join(tmpdir(), 'orca-newer-kind-downgrade-')) - const journals = createTrackedJournalOpener({ legacyInputs: true }) + const journals = createTrackedJournalOpener() const newerKinds = () => storedRows(directory).filter((row) => row.includes('"future-mark"')) try { - const scope = { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } - const journal = await journals.open({ identity: IDENTITY, stateDirectory: directory }) - await journal.appendItem(item(0), { kind: 'status', text: 'before' }, scope) - const at = journal.cursor().sequence + 1 - const newer = JSON.stringify({ - // A version the older build reads, so it meets the kind rather than a newer version. - v: AGENT_SESSION_JOURNAL_TURN_ITEM_SCHEMA_VERSION, - kind: 'future-mark', - epoch: journal.epoch, - seq: at, - fence: 1, - ts: 2_000, - payload: { said: 'by a newer build' } - }) - await journals.closeAll() - insertTestJournalRowJson( - openTestJournalHostDatabase(directory).db, - IDENTITY.sessionId, - at, - newer - ) + // A saved chat with an item, then a newer row, before this build first converts it. + const save = savedBeforeConversion(directory) + save(2, statusRow(0, 'before')) + const newer = save(3, { kind: 'future-mark', payload: { said: 'by a newer build' } }) await closeTestJournalHostDatabase(directory) const rowsBefore = storedRows(directory) From b9dc782330335c6942876af4cab9c1b2c7527875 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 03:23:23 -0800 Subject: [PATCH 43/45] test: open runtime fixtures with SQL journal workers --- .../acp/acp-scripted-child-runtime.test.ts | 6 +- .../journal-sql-queued-messages.test.ts | 9 ++- .../structured-claude-piped-test-support.ts | 6 +- ...ed-claude-scripted-runtime-test-support.ts | 6 +- ...t-session-provider-naming-shutdown.test.ts | 9 +-- .../structured-claude-naming-test-support.ts | 6 +- ...agent-session-record-store-test-harness.ts | 16 ++++ ...-agent-session-startup-tab-restore.test.ts | 6 +- ...gent-session-adapter-start-failure.test.ts | 6 +- ...d-agent-session-codex-compact-exit.test.ts | 2 +- ...agent-session-codex-start-deadline.test.ts | 6 +- ...ructured-agent-session-held-signal.test.ts | 14 ++-- ...uctured-agent-session-host-refusal.test.ts | 6 +- ...ured-agent-session-install-failure.test.ts | 81 ------------------- ...d-agent-session-integration-replay.test.ts | 6 +- ...ructured-agent-session-integration.test.ts | 6 +- ...session-legacy-send-behind-command.test.ts | 2 +- ...-agent-session-pi-signed-out-start.test.ts | 6 +- ...ed-agent-session-startup-reconcile.test.ts | 6 +- ...-chat-coordinator-mail-rig.test-fixture.ts | 6 +- .../renderer-owned-tab-properties.test.ts | 6 +- .../structured-worker-cli.integration.test.ts | 2 +- .../structured-child-records-switch.test.ts | 6 +- ...tructured-codex-child-work-runtime.test.ts | 6 +- ...ructured-codex-session-rpc-test-harness.ts | 6 +- 25 files changed, 71 insertions(+), 166 deletions(-) delete mode 100644 src/main/runtime/agent-session/structured-agent-session-install-failure.test.ts diff --git a/src/main/acp/acp-scripted-child-runtime.test.ts b/src/main/acp/acp-scripted-child-runtime.test.ts index 67927428109..9cb8470678c 100644 --- a/src/main/acp/acp-scripted-child-runtime.test.ts +++ b/src/main/acp/acp-scripted-child-runtime.test.ts @@ -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 diff --git a/src/main/native-chat/agent-session-journal/journal-sql-queued-messages.test.ts b/src/main/native-chat/agent-session-journal/journal-sql-queued-messages.test.ts index 8a91b473782..110b041435c 100644 --- a/src/main/native-chat/agent-session-journal/journal-sql-queued-messages.test.ts +++ b/src/main/native-chat/agent-session-journal/journal-sql-queued-messages.test.ts @@ -27,6 +27,7 @@ type QueueRead = JournalSqlQueueRead extends infer R : never : never const target = { sessionId: 'chat', epoch: 'epoch' } +const now = 1_800_000_000_000 const cleanups: (() => void)[] = [] const body = (text: string): AgentJournalMessageItem => ({ kind: 'message', @@ -116,7 +117,7 @@ function fixture() { providerHandle: codexProviderHandle('thread') } }, - 1_800_000_000_000 + now ) ) const hold = (messageId: string) => @@ -132,7 +133,7 @@ function fixture() { messageId, fingerprint: agentSessionSendBodyFingerprint('chat', body(messageId)) }, - Date.now() + now ) ) const read = (operation: QueueRead, facts: Partial = {}) => @@ -162,7 +163,7 @@ it('lists current and still-owed handoff cards, and selects past persisted holds const next = (automatic: boolean, unavailable?: boolean) => f.read( - { kind: 'next-input-card', automatic, steer: false, now: Date.now() }, + { kind: 'next-input-card', automatic, steer: false, now }, unavailable === undefined ? {} : { mail: [[journalMailSenderKey(from), unavailable ? 'unavailable' : 'owed']] } @@ -181,7 +182,7 @@ it('lists current and still-owed handoff cards, and selects past persisted holds } }, () => null, - Date.now, + () => now, 'epoch:11' ) queue.mailOwed = () => 'unavailable' diff --git a/src/main/runtime/agent-session/installation/structured-claude-piped-test-support.ts b/src/main/runtime/agent-session/installation/structured-claude-piped-test-support.ts index 7b6b149404b..15dacf1ac20 100644 --- a/src/main/runtime/agent-session/installation/structured-claude-piped-test-support.ts +++ b/src/main/runtime/agent-session/installation/structured-claude-piped-test-support.ts @@ -10,10 +10,8 @@ import { hostTestMessage, HOST_TEST_SESSION as SESSION } from '../../../native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture' -import { - ensureStructuredAgentSessionHost, - stopStructuredAgentSessionRuntime -} from '../structured-agent-session-runtime' +import { stopStructuredAgentSessionRuntime } from '../structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from '../records/agent-session-record-store-test-harness' export async function claudePipedFixture() { const directory = await mkdtemp(join(tmpdir(), 'orca-claude-kill-evidence-')) diff --git a/src/main/runtime/agent-session/installation/structured-claude-scripted-runtime-test-support.ts b/src/main/runtime/agent-session/installation/structured-claude-scripted-runtime-test-support.ts index d1db5da39fb..82d22fb4966 100644 --- a/src/main/runtime/agent-session/installation/structured-claude-scripted-runtime-test-support.ts +++ b/src/main/runtime/agent-session/installation/structured-claude-scripted-runtime-test-support.ts @@ -22,10 +22,8 @@ import { claudeSessionIdForOrcaSession } from '../../../claude/structured/claude import type { AgentSessionAttachParams } from '../../../native-chat/agent-session-wire/acquisition/structured-agent-session-attach' import { hostTestAttachParams } from '../../../native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture' import type { StructuredAgentSessionHost } from '../../../native-chat/agent-session-wire/host/structured-agent-session-host' -import { - ensureStructuredAgentSessionHost, - stopStructuredAgentSessionRuntime -} from '../structured-agent-session-runtime' +import { stopStructuredAgentSessionRuntime } from '../structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from '../records/agent-session-record-store-test-harness' import { createStructuredAgentSessionLogger } from '../../../native-chat/agent-session-wire/host/structured-agent-session-logger' /** The error a real CLI's exit reaches the adapter as: Orca's message, the stderr as a log detail. */ diff --git a/src/main/runtime/agent-session/naming/structured-agent-session-provider-naming-shutdown.test.ts b/src/main/runtime/agent-session/naming/structured-agent-session-provider-naming-shutdown.test.ts index 99d388152ae..527f7df7d1a 100644 --- a/src/main/runtime/agent-session/naming/structured-agent-session-provider-naming-shutdown.test.ts +++ b/src/main/runtime/agent-session/naming/structured-agent-session-provider-naming-shutdown.test.ts @@ -12,10 +12,8 @@ import { agentSessionRecordFixture } from '../../../native-chat/agent-session-re import { StructuredAgentSessionAdapterRouter } from '../../../native-chat/agent-session-wire/providers/structured-agent-session-adapter-router' import { createStructuredAgentSessionLogger } from '../../../native-chat/agent-session-wire/host/structured-agent-session-logger' import { seedTestAgentSessionRecordStore } from '../records/agent-session-record-store-test-harness' -import { - ensureStructuredAgentSessionHost, - stopStructuredAgentSessionRuntime -} from '../structured-agent-session-runtime' +import { stopStructuredAgentSessionRuntime } from '../structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from '../records/agent-session-record-store-test-harness' vi.mock('../model-catalog/structured-agent-model-catalog-wiring', async (importOriginal) => { const actual = await importOriginal() @@ -93,12 +91,13 @@ it('stops naming admission and joins native helper finalization before runtime c ] }) await closeTestJournalHostDatabase(root) + const logger = createStructuredAgentSessionLogger() const resolveLaunchArgs = vi.fn(() => argumentsReady.promise) const host = await ensureStructuredAgentSessionHost({ stateDirectory: root, hostId: 'local', claimKeyId: 'key-1', - logger: createStructuredAgentSessionLogger(), + logger, resolveWorkspacePath: async () => root, resolveCodexCommand: () => ({ command: process.execPath, prefixArgs: [] }), resolveEnvironment: async () => ({}), diff --git a/src/main/runtime/agent-session/naming/structured-claude-naming-test-support.ts b/src/main/runtime/agent-session/naming/structured-claude-naming-test-support.ts index 8484c507fed..26652ead802 100644 --- a/src/main/runtime/agent-session/naming/structured-claude-naming-test-support.ts +++ b/src/main/runtime/agent-session/naming/structured-claude-naming-test-support.ts @@ -20,10 +20,8 @@ import { import type { StructuredAgentSessionHost } from '../../../native-chat/agent-session-wire/host/structured-agent-session-host' import { createStructuredAgentSessionLogger } from '../../../native-chat/agent-session-wire/host/structured-agent-session-logger' import { closeTestJournalHostDatabase } from '../../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' -import { - ensureStructuredAgentSessionHost, - stopStructuredAgentSessionRuntime -} from '../structured-agent-session-runtime' +import { stopStructuredAgentSessionRuntime } from '../structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from '../records/agent-session-record-store-test-harness' import type { StructuredAgentSessionRuntimeDeps } from '../installation/structured-agent-session-runtime-contract' export async function writeNativeNamingConversation( diff --git a/src/main/runtime/agent-session/records/agent-session-record-store-test-harness.ts b/src/main/runtime/agent-session/records/agent-session-record-store-test-harness.ts index 0c5eeed72aa..f5b213e891f 100644 --- a/src/main/runtime/agent-session/records/agent-session-record-store-test-harness.ts +++ b/src/main/runtime/agent-session/records/agent-session-record-store-test-harness.ts @@ -20,6 +20,8 @@ import { } from '../../../native-chat/agent-session-journal/journal-sql-worker-bundle.test-fixture' import type Database from '../../../sqlite/sync-database' import { AgentSessionRecordStore } from './agent-session-record-store' +import { openAgentSessionRecordStoreOnce } from './agent-session-record-store-slot' +import { ensureStructuredAgentSessionHost } from '../structured-agent-session-runtime' import type { RetiredAgentSessionClaimKey } from './agent-session-store-state' const TEST_HOST_ID = 'local' @@ -37,6 +39,20 @@ export type PersistedTestAgentSessionStore = { const databases = new Map>() +export async function ensureTestStructuredAgentSessionHost( + deps: Parameters[0] +): ReturnType { + await openAgentSessionRecordStoreOnce({ + stateDirectory: deps.stateDirectory, + hostId: deps.hostId, + logger: deps.logger, + legacySessionTabIds: deps.legacySessionTabIds ?? [], + writerPath: journalSqlWorkerPath(), + readerPath: journalSqlReaderPath() + }) + return ensureStructuredAgentSessionHost(deps) +} + /** Stores reopened in one directory share its writer, like one runtime's slot. */ export function openTestAgentSessionDatabase(stateDirectory: string): Promise { const directory = resolve(stateDirectory) diff --git a/src/main/runtime/agent-session/records/structured-agent-session-startup-tab-restore.test.ts b/src/main/runtime/agent-session/records/structured-agent-session-startup-tab-restore.test.ts index 5c41ac389ff..b01afb6a278 100644 --- a/src/main/runtime/agent-session/records/structured-agent-session-startup-tab-restore.test.ts +++ b/src/main/runtime/agent-session/records/structured-agent-session-startup-tab-restore.test.ts @@ -35,10 +35,8 @@ import { seedTestAgentSessionStoreFromNewerBuild } from './agent-session-record-store-test-harness' import { OrcaRuntimeService } from '../../orca-runtime' -import { - ensureStructuredAgentSessionHost, - stopStructuredAgentSessionRuntime -} from '../structured-agent-session-runtime' +import { stopStructuredAgentSessionRuntime } from '../structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from './agent-session-record-store-test-harness' import { recordingStructuredAgentSessionLogger } 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' import { structuredAgentSessionTabId } from '../../../../shared/agent-session/view/structured-agent-session-projection' diff --git a/src/main/runtime/agent-session/structured-agent-session-adapter-start-failure.test.ts b/src/main/runtime/agent-session/structured-agent-session-adapter-start-failure.test.ts index 32eb30bd890..652d0f7561e 100644 --- a/src/main/runtime/agent-session/structured-agent-session-adapter-start-failure.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-adapter-start-failure.test.ts @@ -15,10 +15,8 @@ import { createStructuredAgentSessionLogger } from '../../native-chat/agent-sess import { piScriptedChild } from '../../pi/pi-scripted-child.test-fixture' import { codexScriptedChild } from '../../codex/structured/session-lifecycle/codex-scripted-child.test-fixture' import type { ScriptedAgentChild } from './installation/structured-agent-scripted-child.test-fixture' -import { - ensureStructuredAgentSessionHost, - stopStructuredAgentSessionRuntime -} from './structured-agent-session-runtime' +import { stopStructuredAgentSessionRuntime } from './structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from './records/agent-session-record-store-test-harness' let directory: string | undefined let operations = 0 diff --git a/src/main/runtime/agent-session/structured-agent-session-codex-compact-exit.test.ts b/src/main/runtime/agent-session/structured-agent-session-codex-compact-exit.test.ts index 522c1a49519..9e95b0066d7 100644 --- a/src/main/runtime/agent-session/structured-agent-session-codex-compact-exit.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-codex-compact-exit.test.ts @@ -5,7 +5,7 @@ import { afterEach, beforeEach, expect, it, vi } from 'vitest' import { readAgentJournalTurn } from '../../../shared/agent-session/lifecycle/agent-session-turn-record' import { DESKTOP_RENDERER_RUNTIME_CLIENT_CAPABILITIES } from '../../ipc/desktop-renderer-runtime-capabilities' import { structuredAgentSessionCommandTurn } from '../../../shared/agent-session/view/structured-agent-session-command-turn-identity' -import { ensureStructuredAgentSessionHost } from './structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from './records/agent-session-record-store-test-harness' import { openStructuredCodexRpcHarness, SESSION, diff --git a/src/main/runtime/agent-session/structured-agent-session-codex-start-deadline.test.ts b/src/main/runtime/agent-session/structured-agent-session-codex-start-deadline.test.ts index c39663f31ad..8bf7a3f8597 100644 --- a/src/main/runtime/agent-session/structured-agent-session-codex-start-deadline.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-codex-start-deadline.test.ts @@ -18,10 +18,8 @@ import { recordAgentSessionStartup } from '../../observability/agent-session-ins import { STRUCTURED_AGENT_SESSION_STARTUP_CEILING_MS } from '../../native-chat/agent-session-wire/startup/structured-agent-session-startup-attempt-contract' import { readWholeAgentSessionFailureFact } from '../../../shared/agent-session/failures/agent-session-failure' import type { ScriptedAgentChild } from './installation/structured-agent-scripted-child.test-fixture' -import { - ensureStructuredAgentSessionHost, - stopStructuredAgentSessionRuntime -} from './structured-agent-session-runtime' +import { stopStructuredAgentSessionRuntime } from './structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from './records/agent-session-record-store-test-harness' vi.mock('../../observability/agent-session-instrumentation', async (importOriginal) => ({ ...(await importOriginal>()), diff --git a/src/main/runtime/agent-session/structured-agent-session-held-signal.test.ts b/src/main/runtime/agent-session/structured-agent-session-held-signal.test.ts index 1af1d2e4e34..20661d06b3e 100644 --- a/src/main/runtime/agent-session/structured-agent-session-held-signal.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-held-signal.test.ts @@ -14,12 +14,14 @@ import type { RpcRequest } from '../rpc/core' import { RpcDispatcher } from '../rpc/dispatcher' import type { OrcaRuntimeService } from '../orca-runtime' -import { - ensureStructuredAgentSessionHost, - stopStructuredAgentSessionRuntime -} from './structured-agent-session-runtime' +import { stopStructuredAgentSessionRuntime } from './structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from './records/agent-session-record-store-test-harness' import { createStructuredAgentSessionLogger } from '../../native-chat/agent-session-wire/host/structured-agent-session-logger' import { openAgentSessionRecordStoreOnce } from './records/agent-session-record-store-slot' +import { + journalSqlReaderPath, + journalSqlWorkerPath +} from '../../native-chat/agent-session-journal/journal-sql-worker-bundle.test-fixture' vi.mock('../../ai-vault/scan-service/session-scanner-service-spawn', async (importOriginal) => ({ ...(await importOriginal()), @@ -59,7 +61,9 @@ const runtime = { legacySessionTabIds: [], stateDirectory, hostId: 'local', - logger: createStructuredAgentSessionLogger() + logger: createStructuredAgentSessionLogger(), + writerPath: journalSqlWorkerPath(), + readerPath: journalSqlReaderPath() }) ).store, listAiVaultSessions: async () => ({ diff --git a/src/main/runtime/agent-session/structured-agent-session-host-refusal.test.ts b/src/main/runtime/agent-session/structured-agent-session-host-refusal.test.ts index 0b23509f7dc..48e37213d87 100644 --- a/src/main/runtime/agent-session/structured-agent-session-host-refusal.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-host-refusal.test.ts @@ -10,10 +10,8 @@ import { isAgentSessionRefusalError } from '../../../shared/agent-session/failur import { journalDatabasePath } from '../../native-chat/agent-session-journal/database/journal-host-database' import { requireStructuredHost } from '../rpc/methods/structured-sessions/creation/structured-agent-session-gate' import type { RpcContext } from '../rpc/core' -import { - ensureStructuredAgentSessionHost, - stopStructuredAgentSessionRuntime -} from './structured-agent-session-runtime' +import { stopStructuredAgentSessionRuntime } from './structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from './records/agent-session-record-store-test-harness' import { recordingStructuredAgentSessionLogger } from '../../native-chat/agent-session-wire/host/testing/structured-agent-session-logger.test-fixture' import { closeTestJournalHostDatabases } from '../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' diff --git a/src/main/runtime/agent-session/structured-agent-session-install-failure.test.ts b/src/main/runtime/agent-session/structured-agent-session-install-failure.test.ts deleted file mode 100644 index 4818bbebf85..00000000000 --- a/src/main/runtime/agent-session/structured-agent-session-install-failure.test.ts +++ /dev/null @@ -1,81 +0,0 @@ -// Failed host installation reuses the record-store slot's open journal connection; launch admission may already hold it. - -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 { JournalHostDatabase } from '../../native-chat/agent-session-journal/database/journal-host-database' -import type * as ModelCatalogWiring from './model-catalog/structured-agent-model-catalog-wiring' -import { - ensureStructuredAgentSessionHost, - stopStructuredAgentSessionRuntime -} from './structured-agent-session-runtime' -import { createStructuredAgentSessionLogger } from '../../native-chat/agent-session-wire/host/structured-agent-session-logger' - -const mocks = vi.hoisted(() => ({ failWiring: vi.fn(() => false) })) - -// The last step before the host is built: a throw here comes after both stores opened. -vi.mock('./model-catalog/structured-agent-model-catalog-wiring', async (importOriginal) => { - const actual = await importOriginal() - return { - ...actual, - ensureStructuredAgentModelCatalogOwner: async ( - ...args: Parameters - ) => { - const owner = await actual.ensureStructuredAgentModelCatalogOwner(...args) - return { - ...owner, - attach: (...access: Parameters) => { - if (mocks.failWiring()) { - throw new Error('model catalog wiring failed') - } - return owner.attach(...access) - } - } - } - } -}) - -let root: string - -function install(): ReturnType { - return ensureStructuredAgentSessionHost({ - logger: createStructuredAgentSessionLogger(), - stateDirectory: root, - hostId: 'local', - claimKeyId: 'key-1', - resolveWorkspacePath: async () => root, - resolveEnvironment: async () => ({}), - resolveLaunchArgs: () => [], - resolveClaudeAuthPolicy: () => ({ account: 'managed' }) - }) -} - -beforeEach(async () => { - root = await mkdtemp(join(tmpdir(), 'orca-structured-install-failure-')) -}) - -afterEach(async () => { - await stopStructuredAgentSessionRuntime().catch(() => undefined) - vi.restoreAllMocks() - await rm(root, { recursive: true, force: true }) -}) - -describe('an install that fails after opening the chat journal', () => { - it('keeps the one connection it opened, and the next install builds on it', async () => { - const open = vi.spyOn(JournalHostDatabase, 'open') - mocks.failWiring.mockReturnValueOnce(true) - - await expect(install()).rejects.toThrow('model catalog wiring failed') - const opened = await open.mock.results[0]?.value - expect(opened).toBeInstanceOf(JournalHostDatabase) - expect(opened.isClosed).toBe(false) - - await expect(install()).resolves.toBeDefined() - expect(open).toHaveBeenCalledOnce() - expect(opened.isClosed).toBe(false) - - await stopStructuredAgentSessionRuntime() - expect(opened.isClosed).toBe(true) - }) -}) diff --git a/src/main/runtime/agent-session/structured-agent-session-integration-replay.test.ts b/src/main/runtime/agent-session/structured-agent-session-integration-replay.test.ts index 15d3d24b77f..dfcac9fd672 100644 --- a/src/main/runtime/agent-session/structured-agent-session-integration-replay.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-integration-replay.test.ts @@ -5,10 +5,8 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import type { AgentJournalRenderItem } from '../../../shared/agent-session/journal/agent-session-journal-types' import { createTrackedJournalOpener } from '../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import type * as JournalStoreFactory from '../../native-chat/agent-session-journal/journal-store-factory' -import { - ensureStructuredAgentSessionHost, - stopStructuredAgentSessionRuntime -} from './structured-agent-session-runtime' +import { stopStructuredAgentSessionRuntime } from './structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from './records/agent-session-record-store-test-harness' import { openStructuredCodexRpcHarness, SESSION, diff --git a/src/main/runtime/agent-session/structured-agent-session-integration.test.ts b/src/main/runtime/agent-session/structured-agent-session-integration.test.ts index d3e526ecaf0..58e3e2f1214 100644 --- a/src/main/runtime/agent-session/structured-agent-session-integration.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-integration.test.ts @@ -23,10 +23,8 @@ import type { RpcRequest, RpcResponse } from '../rpc/core' import { RpcDispatcher } from '../rpc/dispatcher' import type { NativeChatShellEnvironmentPolicy } from '../../../shared/native-chat/native-chat-shell-environment' import { STRUCTURED_AGENT_SESSION_METHODS } from '../rpc/methods/structured-sessions/structured-agent-session' -import { - ensureStructuredAgentSessionHost, - stopStructuredAgentSessionRuntime -} from './structured-agent-session-runtime' +import { stopStructuredAgentSessionRuntime } from './structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from './records/agent-session-record-store-test-harness' 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 { scriptedCodexTransport } from '../orca-runtime-tests/scripted-codex-transport.test-fixture' diff --git a/src/main/runtime/agent-session/structured-agent-session-legacy-send-behind-command.test.ts b/src/main/runtime/agent-session/structured-agent-session-legacy-send-behind-command.test.ts index 97c9b165a2a..fbc84ba434d 100644 --- a/src/main/runtime/agent-session/structured-agent-session-legacy-send-behind-command.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-legacy-send-behind-command.test.ts @@ -3,7 +3,7 @@ import { readTestHostJournal } from '../../native-chat/agent-session-journal/jou import { afterEach, beforeEach, expect, it, vi } from 'vitest' import type { AgentSessionSendResult } from '../../../shared/agent-session/agent-session-wire' -import { ensureStructuredAgentSessionHost } from './structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from './records/agent-session-record-store-test-harness' import { openStructuredCodexRpcHarness, SESSION, diff --git a/src/main/runtime/agent-session/structured-agent-session-pi-signed-out-start.test.ts b/src/main/runtime/agent-session/structured-agent-session-pi-signed-out-start.test.ts index f41776dcfe5..16ba653adf6 100644 --- a/src/main/runtime/agent-session/structured-agent-session-pi-signed-out-start.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-pi-signed-out-start.test.ts @@ -13,10 +13,8 @@ import { createTestParams } from '../../native-chat/agent-session-wire/host/test import { createStructuredAgentSessionLogger } from '../../native-chat/agent-session-wire/host/structured-agent-session-logger' import { piScriptedChild } from '../../pi/pi-scripted-child.test-fixture' import { peekOpenedAgentSessionRecordStore } from './records/agent-session-record-store-slot' -import { - ensureStructuredAgentSessionHost, - stopStructuredAgentSessionRuntime -} from './structured-agent-session-runtime' +import { stopStructuredAgentSessionRuntime } from './structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from './records/agent-session-record-store-test-harness' let directory: string | undefined let operations = 0 diff --git a/src/main/runtime/agent-session/structured-agent-session-startup-reconcile.test.ts b/src/main/runtime/agent-session/structured-agent-session-startup-reconcile.test.ts index b1012995828..9289ef114b0 100644 --- a/src/main/runtime/agent-session/structured-agent-session-startup-reconcile.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-startup-reconcile.test.ts @@ -14,10 +14,8 @@ import { } from './records/agent-session-record-store-test-harness' import { closeTestJournalHostDatabases } from '../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import { OrcaRuntimeService } from '../orca-runtime' -import { - ensureStructuredAgentSessionHost, - stopStructuredAgentSessionRuntime -} from './structured-agent-session-runtime' +import { stopStructuredAgentSessionRuntime } from './structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from './records/agent-session-record-store-test-harness' import { recordingStructuredAgentSessionLogger } from '../../native-chat/agent-session-wire/host/testing/structured-agent-session-logger.test-fixture' const writes = vi.hoisted(() => ({ failing: false })) diff --git a/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-coordinator-mail-rig.test-fixture.ts b/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-coordinator-mail-rig.test-fixture.ts index 9524aed92fa..141630f27bd 100644 --- a/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-coordinator-mail-rig.test-fixture.ts +++ b/src/main/runtime/orchestration/terminal-authority/structured-sessions/structured-chat-coordinator-mail-rig.test-fixture.ts @@ -23,10 +23,8 @@ import { OrchestrationDb } from '../../db/orchestration-db' import { RpcDispatcher } from '../../../rpc/dispatcher' import { ORCHESTRATION_METHODS } from '../../../rpc/methods/orchestration/orchestration' import { idOf, isRecord, resultOf } from '../../../rpc/orchestration-session-caller-test-fixture' -import { - ensureStructuredAgentSessionHost, - stopStructuredAgentSessionRuntime -} from '../../../agent-session/structured-agent-session-runtime' +import { stopStructuredAgentSessionRuntime } from '../../../agent-session/structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from '../../../agent-session/records/agent-session-record-store-test-harness' import { createCoordinatorMailObservationClock } from './structured-chat-coordinator-observation-clock.test-fixture' import { attachParams, diff --git a/src/main/runtime/renderer-owned-tab-properties.test.ts b/src/main/runtime/renderer-owned-tab-properties.test.ts index 8875136bdbb..8754b8388f0 100644 --- a/src/main/runtime/renderer-owned-tab-properties.test.ts +++ b/src/main/runtime/renderer-owned-tab-properties.test.ts @@ -14,10 +14,8 @@ import { readPersistedTestAgentSessionStore, seedTestAgentSessionRecordStore } from './agent-session/records/agent-session-record-store-test-harness' -import { - ensureStructuredAgentSessionHost, - stopStructuredAgentSessionRuntime -} from './agent-session/structured-agent-session-runtime' +import { stopStructuredAgentSessionRuntime } from './agent-session/structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from './agent-session/records/agent-session-record-store-test-harness' import type { AgentSessionRecordStore } from './agent-session/records/agent-session-record-store' import './orca-runtime-tests/runtime-test-module-registration.spec' import { OrcaRuntimeService } from './orca-runtime' diff --git a/src/main/runtime/rpc/structured-worker-cli.integration.test.ts b/src/main/runtime/rpc/structured-worker-cli.integration.test.ts index 6367e438f8b..2757d46cd93 100644 --- a/src/main/runtime/rpc/structured-worker-cli.integration.test.ts +++ b/src/main/runtime/rpc/structured-worker-cli.integration.test.ts @@ -28,7 +28,7 @@ import { root, savedIds } from '../agent-session/lifecycle/structured-agent-session-tab-authority.test-support' -import { ensureStructuredAgentSessionHost } from '../agent-session/structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from '../agent-session/records/agent-session-record-store-test-harness' import { fakeCodex, resetProviderFaults diff --git a/src/main/runtime/structured-child-records-switch.test.ts b/src/main/runtime/structured-child-records-switch.test.ts index 3ad903f24de..3d697ef5b81 100644 --- a/src/main/runtime/structured-child-records-switch.test.ts +++ b/src/main/runtime/structured-child-records-switch.test.ts @@ -23,10 +23,8 @@ import { hostTestMessage } from '../native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture' import type { StructuredAgentSessionStatusSink } from '../native-chat/agent-session-wire/observations/structured-agent-session-status-ownership' -import { - ensureStructuredAgentSessionHost, - stopStructuredAgentSessionRuntime -} from './agent-session/structured-agent-session-runtime' +import { stopStructuredAgentSessionRuntime } from './agent-session/structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from './agent-session/records/agent-session-record-store-test-harness' import { createStructuredAgentSessionLogger } from '../native-chat/agent-session-wire/host/structured-agent-session-logger' import { scriptedCodexTransport } from './orca-runtime-tests/scripted-codex-transport.test-fixture' import { createScriptedClaudeRuntime } from './agent-session/installation/structured-claude-scripted-runtime-test-support' diff --git a/src/main/runtime/structured-codex-child-work-runtime.test.ts b/src/main/runtime/structured-codex-child-work-runtime.test.ts index d67bcc4ccca..0ac719d021f 100644 --- a/src/main/runtime/structured-codex-child-work-runtime.test.ts +++ b/src/main/runtime/structured-codex-child-work-runtime.test.ts @@ -15,10 +15,8 @@ import { hostTestAttachParams } from '../native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data.test-fixture' import type { StructuredAgentSessionStatusSink } from '../native-chat/agent-session-wire/observations/structured-agent-session-status-ownership' -import { - ensureStructuredAgentSessionHost, - stopStructuredAgentSessionRuntime -} from './agent-session/structured-agent-session-runtime' +import { stopStructuredAgentSessionRuntime } from './agent-session/structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from './agent-session/records/agent-session-record-store-test-harness' import { createStructuredAgentSessionLogger } from '../native-chat/agent-session-wire/host/structured-agent-session-logger' import { scriptedCodexTransport } from './orca-runtime-tests/scripted-codex-transport.test-fixture' import { AgentHookServer } from '../agent-hooks/server' diff --git a/src/main/runtime/structured-codex-session-rpc-test-harness.ts b/src/main/runtime/structured-codex-session-rpc-test-harness.ts index 2d8e201dac5..a548c7af36f 100644 --- a/src/main/runtime/structured-codex-session-rpc-test-harness.ts +++ b/src/main/runtime/structured-codex-session-rpc-test-harness.ts @@ -21,10 +21,8 @@ import type { OrcaRuntimeService } from './orca-runtime' import type { RpcRequest, RpcResponse } from './rpc/core' import { RpcDispatcher } from './rpc/dispatcher' import { STRUCTURED_AGENT_SESSION_METHODS } from './rpc/methods/structured-sessions/structured-agent-session' -import { - ensureStructuredAgentSessionHost, - stopStructuredAgentSessionRuntime -} from './agent-session/structured-agent-session-runtime' +import { stopStructuredAgentSessionRuntime } from './agent-session/structured-agent-session-runtime' +import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from './agent-session/records/agent-session-record-store-test-harness' import { createStructuredAgentSessionLogger } from '../native-chat/agent-session-wire/host/structured-agent-session-logger' import { scriptedCodexTransport } from './orca-runtime-tests/scripted-codex-transport.test-fixture' From c24ffb468d9e1395b6bc1d4cc87ec909a57b1e16 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 03:30:01 -0800 Subject: [PATCH 44/45] test: combine runtime fixture imports --- ...tructured-agent-session-provider-naming-shutdown.test.ts | 6 ++++-- .../structured-agent-session-startup-tab-restore.test.ts | 2 +- .../structured-agent-session-startup-reconcile.test.ts | 2 +- src/main/runtime/renderer-owned-tab-properties.test.ts | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/runtime/agent-session/naming/structured-agent-session-provider-naming-shutdown.test.ts b/src/main/runtime/agent-session/naming/structured-agent-session-provider-naming-shutdown.test.ts index 527f7df7d1a..97f3e0f734a 100644 --- a/src/main/runtime/agent-session/naming/structured-agent-session-provider-naming-shutdown.test.ts +++ b/src/main/runtime/agent-session/naming/structured-agent-session-provider-naming-shutdown.test.ts @@ -11,9 +11,11 @@ import { closeTestJournalHostDatabase } from '../../../native-chat/agent-session import { agentSessionRecordFixture } from '../../../native-chat/agent-session-record-test-fixture' import { StructuredAgentSessionAdapterRouter } from '../../../native-chat/agent-session-wire/providers/structured-agent-session-adapter-router' import { createStructuredAgentSessionLogger } from '../../../native-chat/agent-session-wire/host/structured-agent-session-logger' -import { seedTestAgentSessionRecordStore } from '../records/agent-session-record-store-test-harness' +import { + ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost, + seedTestAgentSessionRecordStore +} from '../records/agent-session-record-store-test-harness' import { stopStructuredAgentSessionRuntime } from '../structured-agent-session-runtime' -import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from '../records/agent-session-record-store-test-harness' vi.mock('../model-catalog/structured-agent-model-catalog-wiring', async (importOriginal) => { const actual = await importOriginal() diff --git a/src/main/runtime/agent-session/records/structured-agent-session-startup-tab-restore.test.ts b/src/main/runtime/agent-session/records/structured-agent-session-startup-tab-restore.test.ts index b01afb6a278..9c500e51425 100644 --- a/src/main/runtime/agent-session/records/structured-agent-session-startup-tab-restore.test.ts +++ b/src/main/runtime/agent-session/records/structured-agent-session-startup-tab-restore.test.ts @@ -30,13 +30,13 @@ import { attachParamsForRecord } from '../../../native-chat/agent-session-wire/h import { getStructuredAgentSessionHost } from '../../../native-chat/agent-session-wire/host/structured-agent-session-registry' import type * as AgentSessionRecordRows from './agent-session-record-rows' import { + ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost, openTestAgentSessionDatabase, seedTestAgentSessionRecordStore, seedTestAgentSessionStoreFromNewerBuild } from './agent-session-record-store-test-harness' import { OrcaRuntimeService } from '../../orca-runtime' import { stopStructuredAgentSessionRuntime } from '../structured-agent-session-runtime' -import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from './agent-session-record-store-test-harness' import { recordingStructuredAgentSessionLogger } 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' import { structuredAgentSessionTabId } from '../../../../shared/agent-session/view/structured-agent-session-projection' diff --git a/src/main/runtime/agent-session/structured-agent-session-startup-reconcile.test.ts b/src/main/runtime/agent-session/structured-agent-session-startup-reconcile.test.ts index 9289ef114b0..dd18f5c40e1 100644 --- a/src/main/runtime/agent-session/structured-agent-session-startup-reconcile.test.ts +++ b/src/main/runtime/agent-session/structured-agent-session-startup-reconcile.test.ts @@ -9,13 +9,13 @@ import { journalDatabasePath } from '../../native-chat/agent-session-journal/dat import { getStructuredAgentSessionHost } from '../../native-chat/agent-session-wire/host/structured-agent-session-registry' import type * as AgentSessionRecordRows from './records/agent-session-record-rows' import { + ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost, seedTestAgentSessionRecordStore, seedTestAgentSessionStoreFromNewerBuild } from './records/agent-session-record-store-test-harness' import { closeTestJournalHostDatabases } from '../../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support' import { OrcaRuntimeService } from '../orca-runtime' import { stopStructuredAgentSessionRuntime } from './structured-agent-session-runtime' -import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from './records/agent-session-record-store-test-harness' import { recordingStructuredAgentSessionLogger } from '../../native-chat/agent-session-wire/host/testing/structured-agent-session-logger.test-fixture' const writes = vi.hoisted(() => ({ failing: false })) diff --git a/src/main/runtime/renderer-owned-tab-properties.test.ts b/src/main/runtime/renderer-owned-tab-properties.test.ts index 8754b8388f0..9514c342898 100644 --- a/src/main/runtime/renderer-owned-tab-properties.test.ts +++ b/src/main/runtime/renderer-owned-tab-properties.test.ts @@ -11,11 +11,11 @@ import { closeTestJournalHostDatabases } from '../native-chat/agent-session-jour import { createStructuredChatNamingHandler } from '../native-chat/structured-chat-naming' import { recordingStructuredAgentSessionLogger } from '../native-chat/agent-session-wire/host/testing/structured-agent-session-logger.test-fixture' import { + ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost, readPersistedTestAgentSessionStore, seedTestAgentSessionRecordStore } from './agent-session/records/agent-session-record-store-test-harness' import { stopStructuredAgentSessionRuntime } from './agent-session/structured-agent-session-runtime' -import { ensureTestStructuredAgentSessionHost as ensureStructuredAgentSessionHost } from './agent-session/records/agent-session-record-store-test-harness' import type { AgentSessionRecordStore } from './agent-session/records/agent-session-record-store' import './orca-runtime-tests/runtime-test-module-registration.spec' import { OrcaRuntimeService } from './orca-runtime' From ecc886b7693742fbe382951bf8e6498a3fa1dfb3 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 03:30:34 -0800 Subject: [PATCH 45/45] test: prune resolved compatibility timeout finding --- config/flaky-test-pattern-baseline.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/config/flaky-test-pattern-baseline.txt b/config/flaky-test-pattern-baseline.txt index 9e8de7fc106..1e7641132ea 100644 --- a/config/flaky-test-pattern-baseline.txt +++ b/config/flaky-test-pattern-baseline.txt @@ -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