diff --git a/config/scripts/vitest-sqlite-runtime-files.mjs b/config/scripts/vitest-sqlite-runtime-files.mjs index f150622e61d1..84b6afa4094e 100644 --- a/config/scripts/vitest-sqlite-runtime-files.mjs +++ b/config/scripts/vitest-sqlite-runtime-files.mjs @@ -126,7 +126,10 @@ export const SQLITE_RUNTIME_INCLUDE = [ 'src/main/native-chat/agent-session-wire/structured-agent-session-provider-restore.test.ts', 'src/main/native-chat/agent-session-wire/structured-agent-session-provider-started-catalog.test.ts', 'src/main/native-chat/agent-session-wire/structured-agent-session-provider-started.test.ts', + 'src/main/native-chat/agent-session-wire/structured-agent-session-queued-gate.test.ts', 'src/main/native-chat/agent-session-wire/structured-agent-session-queued-newer-orca.test.ts', + 'src/main/native-chat/agent-session-wire/structured-agent-session-queued-pause-lift.test.ts', + 'src/main/native-chat/agent-session-wire/structured-agent-session-queued-repeat-press.test.ts', 'src/main/native-chat/agent-session-wire/structured-agent-session-read-restore.test.ts', 'src/main/native-chat/agent-session-wire/structured-agent-session-readable-restorer.test.ts', 'src/main/native-chat/agent-session-wire/structured-agent-session-reasoning-sweep.test.ts', diff --git a/src/main/native-chat/agent-session-journal/command-receipt-schema.ts b/src/main/native-chat/agent-session-journal/command-receipt-schema.ts index dfadddbb063c..2502300785a0 100644 --- a/src/main/native-chat/agent-session-journal/command-receipt-schema.ts +++ b/src/main/native-chat/agent-session-journal/command-receipt-schema.ts @@ -25,6 +25,10 @@ export const commandReceiptResultSchema = z.discriminatedUnion('kind', [ epoch: z.string().min(1), sequence: z.int().positive() }), + z.strictObject({ + kind: z.literal('queued-draft'), + messageId: z.string().min(1) + }), z.strictObject({ kind: z.literal('no-op'), outcome: z.discriminatedUnion('kind', [ @@ -75,7 +79,7 @@ export const commandReceiptSchema = z.discriminatedUnion('status', [ export type CommandReceipt = z.infer -/** Additive and inert until cutover; a version bump here would prevent older builds writing. */ +/** Additive, so no version bump: one would stop older builds writing this database. */ export function ensureCommandReceiptsTable(db: Database.Database): void { db.exec(` CREATE TABLE IF NOT EXISTS agent_session_command_receipts ( diff --git a/src/main/native-chat/agent-session-journal/command-receipt-table.test.ts b/src/main/native-chat/agent-session-journal/command-receipt-table.test.ts index 5fb26b71e1dc..4f0da005da75 100644 --- a/src/main/native-chat/agent-session-journal/command-receipt-table.test.ts +++ b/src/main/native-chat/agent-session-journal/command-receipt-table.test.ts @@ -202,6 +202,14 @@ describe('command receipt identity', () => { }) describe('command receipt outcomes and lifetime', () => { + it('round trips a queued-draft pointer without retaining its body', () => { + const receipt = commandReceiptFixture({ + result: { kind: 'queued-draft', messageId: 'draft-1' } + }) + insert(receipt) + expect(read(receipt)).toEqual({ verdict: 'readable', receipt }) + }) + it.each([ { kind: 'cancel', cancelled: false, turnId: 'turn-1' }, { kind: 'cancel', cancelled: false }, @@ -242,6 +250,7 @@ describe('unreadable command receipts', () => { ['result_json', '{'], ['result_json', '{"kind":"journal-row","epoch":"e1","sequence":0}'], ['result_json', '{"kind":"future-result"}'], + ['result_json', '{"kind":"queued-draft","messageId":""}'], ['result_json', null], ['status', 'pending'], ['method', ''], diff --git a/src/main/native-chat/agent-session-journal/command-receipt-transaction.test.ts b/src/main/native-chat/agent-session-journal/command-receipt-transaction.test.ts index e5cb81f0815c..673025d28786 100644 --- a/src/main/native-chat/agent-session-journal/command-receipt-transaction.test.ts +++ b/src/main/native-chat/agent-session-journal/command-receipt-transaction.test.ts @@ -20,7 +20,7 @@ import type { JournalRow } from './journal-row-schema' import { JournalRowWriter } from './journal-row-writer' import { JournalWriteQueue } from './journal-write-queue' import { insertCommandReceiptIfAbsent, readCommandReceipt } from './command-receipt-table' -import { commandReceiptScope, type CommandReceiptResult } from './command-receipt-schema' +import { commandReceiptScope } from './command-receipt-schema' import { commandReceiptFixture, writeCommandReceiptTestRecord @@ -95,7 +95,6 @@ describe('command receipt transaction hook', () => { it('commits through the existing row writer with a pointer to the row it assigned', async () => { const queue = new JournalWriteQueue(identity.sessionId) const committed: JournalRow[] = [] - let result: CommandReceiptResult = { kind: 'journal-row', epoch: 'epoch-0', sequence: 99 } const writer = new JournalRowWriter({ sessionId: identity.sessionId, now: () => 1000, @@ -108,10 +107,13 @@ describe('command receipt transaction hook', () => { }) await writer.enqueue( epochRow, - (_db, row) => { - result = { kind: 'journal-row', epoch: row.epoch, sequence: row.seq } - }, - buildCommandReceiptTransaction(scope, () => ({ ...receipt, result })) + undefined, + buildCommandReceiptTransaction(scope, (row) => { + if (!row) { + throw new Error('expected the committed journal row') + } + return { ...receipt, result: { kind: 'journal-row', epoch: row.epoch, sequence: row.seq } } + }) ) expect(committed).toHaveLength(1) expect(committed[0]).toEqual(epochRow(7, 1000)) diff --git a/src/main/native-chat/agent-session-journal/command-receipt-transaction.ts b/src/main/native-chat/agent-session-journal/command-receipt-transaction.ts index c4b3330cc288..cc4e228c2413 100644 --- a/src/main/native-chat/agent-session-journal/command-receipt-transaction.ts +++ b/src/main/native-chat/agent-session-journal/command-receipt-transaction.ts @@ -1,6 +1,7 @@ import type { CommandReceipt, CommandReceiptScope } from './command-receipt-schema' import { insertCommandReceiptIfAbsent, type CommandReceiptInsert } from './command-receipt-table' import type { JournalOperationReceipt } from './journal-row-writer' +import type { JournalRow } from './journal-row-schema' type CommandReceiptNotInserted = Extract @@ -14,14 +15,14 @@ export class CommandReceiptExistsError extends Error { export function buildCommandReceiptTransaction( scope: CommandReceiptScope, - receipt: CommandReceipt | (() => CommandReceipt) + receipt: CommandReceipt | ((row?: JournalRow) => CommandReceipt) ): JournalOperationReceipt { return { - write: (db) => { + write: (db, row) => { const result = insertCommandReceiptIfAbsent( db, scope, - typeof receipt === 'function' ? receipt() : receipt + typeof receipt === 'function' ? receipt(row) : receipt ) if (!result.inserted) { throw new CommandReceiptExistsError(result) diff --git a/src/main/native-chat/agent-session-journal/journal-row-writer.ts b/src/main/native-chat/agent-session-journal/journal-row-writer.ts index 3bbd9ef09ae0..627a20592a7d 100644 --- a/src/main/native-chat/agent-session-journal/journal-row-writer.ts +++ b/src/main/native-chat/agent-session-journal/journal-row-writer.ts @@ -11,14 +11,23 @@ import type { JournalWriteBody } from './journal-write-queue' * nothing can interleave inside the transaction. */ export type JournalRowTransactionHook = (db: Database.Database, row: JournalRow) => void -/** An operation's ledger answer, committed with the journal write that makes it true: `write` runs +/** An operation's receipt, committed with the journal write that makes it true: `write` runs * inside that transaction on the same connection, `committed` synchronously right after its * COMMIT and never after a rollback. */ export type JournalOperationReceipt = { - write: (db: Database.Database) => void + write: (db: Database.Database, row?: JournalRow) => void committed: () => void } +export function composeJournalOperationReceipts( + ...receipts: JournalOperationReceipt[] +): JournalOperationReceipt { + return { + write: (db, row) => receipts.forEach((receipt) => receipt.write(db, row)), + committed: () => receipts.forEach((receipt) => receipt.committed()) + } +} + export type JournalRowWriterDeps = { sessionId: string now: () => number @@ -55,7 +64,7 @@ export class JournalRowWriter { this.deps.database().transaction((db) => { insertJournalRow(db, this.deps.sessionId, row) hook?.(db, row) - receipt?.write(db) + receipt?.write(db, row) this.runBookkeeping(db, row) }) } catch (error) { diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-attachment-admission.test.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-attachment-admission.test.ts index 69cf3e904a24..fba94cd39ab1 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-attachment-admission.test.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-attachment-admission.test.ts @@ -142,3 +142,30 @@ describe('attachments at send admission', () => { expect(result).toMatchObject({ ok: true, value: { submission: expect.anything() } }) }) }) + +it.each([undefined, 'queue-if-active'] as const)( + 're-evaluates an unaccepted attachment refusal after the file returns (%s)', + async (delivery) => { + if (delivery) { + await rig.workingSend() + } + const body = textWith(storedPath('notes.txt')) + const clientOperationId = hostTestOperationId() + const fields = { body, ...(delivery ? { delivery } : {}) } + const params = { + envelope: rig.envelope(fields, 'agentSession.send', clientOperationId), + ...fields, + userSend: true as const + } + expect(await rig.host.send(CALLER, params)).toMatchObject(EXPIRED) + expect(rig.store.readCommandReceipt({ kind: 'global' }, clientOperationId)).toEqual({ + verdict: 'absent' + }) + expect( + rig.store.listOperationRows().find((row) => row.operationId === clientOperationId) + ).toBeUndefined() + await storeUpload('notes.txt') + expect(await rig.host.send(CALLER, params)).toMatchObject({ ok: true, replayed: false }) + expect(await clientSend(body, delivery)).toMatchObject({ ok: true, replayed: false }) + } +) diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-command-admission.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-command-admission.ts new file mode 100644 index 000000000000..2de6745fe0d2 --- /dev/null +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-command-admission.ts @@ -0,0 +1,181 @@ +import { parseAgentSessionOperationTimestamp } from '../../../shared/agent-session-host-authority' +import { agentSessionLedgerRefusal } from '../../../shared/agent-session-mutation-envelope' +import { + agentSessionRefusalFromReference, + isAgentSessionRefusalError +} from '../../../shared/agent-session-wire-refusals' +import type { AgentSessionOperationOutcome } from '../../../shared/agent-session-operation-ledger' +import type { AgentSessionMutationResult } from '../../../shared/agent-session-wire' +import { commandReceiptScope } from '../agent-session-journal/command-receipt-schema' +import type { CommandReceipt } from '../agent-session-journal/command-receipt-schema' +import type { ExistingCommandReceipt } from '../agent-session-journal/command-receipt-table' +import { CommandReceiptExistsError } from '../agent-session-journal/command-receipt-transaction' +import { + AGENT_SESSION_NOT_ATTACHED, + refuseAgentSessionMutation +} from './structured-agent-session-mutation-refusals' +import type { AgentSessionMutationRequest } from './structured-agent-session-mutation-admission' +import { mutationTurnContext } from './structured-agent-session-mutation-turn-context' +import { runCommandReceiptMutation } from './structured-agent-session-command-receipt' +import { + agentSessionOperationOutcomeUnknown, + resolveAgentSessionReplayOutcome +} from './structured-agent-session-replay-outcome' + +/** Submission-accepting plans use their effect's receipt, never a ledger reservation. */ +export async function admitCommandReceiptMutation( + request: AgentSessionMutationRequest, + hostFingerprint: string +): Promise> { + const { store, envelope, plan, callerKey } = request + if (parseAgentSessionOperationTimestamp(envelope.clientOperationId) === null) { + return refuseAgentSessionMutation( + agentSessionLedgerRefusal(envelope, { + decision: 'refused', + code: 'agent_session_operation_invalid', + details: { reason: 'operationIdInvalid' } + }) + ) + } + const record = store.getRecord(envelope.sessionId) + if (!record) { + return refuseAgentSessionMutation(AGENT_SESSION_NOT_ATTACHED) + } + let read + try { + read = store.readCommandReceipt( + commandReceiptScope(callerKey, plan.operationIdScope), + envelope.clientOperationId + ) + } catch (error) { + request.logger.warn('reading a command receipt failed', { + scope: 'command-receipt-read', + sessionId: envelope.sessionId, + operationId: envelope.clientOperationId, + error + }) + return unknown(request) + } + if (read.verdict !== 'absent') { + return answerCommandReceipt(request, hostFingerprint, read) + } + const prepared = await request.prepareSession?.('admit', record) + if (prepared && !prepared.ok) { + return prepared + } + const journal = request.journal() + const current = store.getRecord(envelope.sessionId) + if (!journal || !current) { + return refuseAgentSessionMutation(AGENT_SESSION_NOT_ATTACHED) + } + // These plans write only to the conversation, whose journal enforces the execution-host fence. + const context = mutationTurnContext(request, journal, current) + try { + const outcome = await runCommandReceiptMutation({ + store, + operationCallerKey: callerKey, + fingerprint: hostFingerprint, + wakeDelivery: request.wakeDelivery, + envelope, + plan, + context + }) + if ('committedReceipt' in outcome) { + return answerCommandReceipt(request, hostFingerprint, { + verdict: 'readable', + receipt: outcome.committedReceipt + }) + } + return outcome.ok + ? { + ok: true, + replayed: false, + fence: context.fence, + cursor: journal.cursor(), + value: outcome.value + } + : refuseAgentSessionMutation(outcome.refusal) + } catch (error) { + if (error instanceof CommandReceiptExistsError) { + return answerCommandReceipt(request, hostFingerprint, error.result.existing) + } + if (isAgentSessionRefusalError(error)) { + return refuseAgentSessionMutation(error.refusal) + } + throw error + } +} + +async function answerCommandReceipt( + request: AgentSessionMutationRequest, + hostFingerprint: string, + read: ExistingCommandReceipt +): Promise> { + const { envelope, plan } = request + if (read.verdict === 'unreadable') { + return unknown(request) + } + const { receipt } = read + if (receipt.fingerprint !== hostFingerprint) { + return refuseAgentSessionMutation( + agentSessionLedgerRefusal(envelope, { + decision: 'refused', + code: 'agent_session_operation_conflict', + details: { reason: 'operationIdReused' } + }) + ) + } + if (receipt.status === 'rejected') { + return refuseAgentSessionMutation( + agentSessionRefusalFromReference( + receipt.rejection.reference, + receipt.rejection.message ?? 'Operation was already refused.' + ) + ) + } + const record = request.store.getRecord(envelope.sessionId) + if (!record) { + return unknown(request) + } + const prepared = await request.prepareSession?.('replay', record) + if (prepared && !prepared.ok) { + return prepared + } + const journal = request.journal() + const current = request.store.getRecord(envelope.sessionId) + if (!journal || !current) { + return unknown(request) + } + const context = mutationTurnContext(request, journal, current) + const outcome = commandReceiptOutcome(receipt) + const replay = resolveAgentSessionReplayOutcome({ + operationId: envelope.clientOperationId, + outcome, + reconstruct: () => plan.replay(context, outcome), + rerunWhenReplayMissing: plan.rerunWhenReplayMissing?.(context), + recoverUnknownFromDurableState: plan.recoverUnknownFromDurableState + }) + // A missing result cannot undo durable acceptance, even if a later plan allows missing replays. + if (replay.decision !== 'replay') { + return replay.decision === 'refuse' + ? refuseAgentSessionMutation(replay.refusal) + : unknown(request) + } + return { + ok: true, + replayed: true, + fence: context.fence, + cursor: journal.cursor(), + value: replay.value + } +} + +function commandReceiptOutcome(receipt: CommandReceipt): AgentSessionOperationOutcome { + return { status: 'succeeded', sessionId: receipt.sessionId } +} + +function unknown(request: AgentSessionMutationRequest) { + return refuseAgentSessionMutation( + agentSessionOperationOutcomeUnknown(request.envelope.clientOperationId) + ) +} diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-command-receipt.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-command-receipt.ts new file mode 100644 index 000000000000..763506a2c319 --- /dev/null +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-command-receipt.ts @@ -0,0 +1,106 @@ +import type { AgentSessionMutationEnvelope } from '../../../shared/agent-session-wire' +import type { AgentSessionRecordStore } from '../../runtime/agent-session-record-store' +import { + commandReceiptScope, + type CommandReceipt +} from '../agent-session-journal/command-receipt-schema' +import { buildCommandReceiptTransaction } from '../agent-session-journal/command-receipt-transaction' +import { composeJournalOperationReceipts } from '../agent-session-journal/journal-row-writer' +import type { MutationPlan } from './structured-agent-session-mutation-plans' +import type { AgentSessionTurnContext, TurnOutcome } from './structured-agent-session-turns' + +/** Acceptance identity and compatibility bookkeeping share the submission or draft's commit. */ +export async function runCommandReceiptMutation(input: { + store: AgentSessionRecordStore + operationCallerKey: string + fingerprint: string + envelope: AgentSessionMutationEnvelope + plan: MutationPlan + context: AgentSessionTurnContext + wakeDelivery?: (sessionId: string) => void +}): Promise | { committedReceipt: CommandReceipt }> { + const { store, operationCallerKey, fingerprint, envelope, plan, context, wakeDelivery } = input + let acceptedReceipt: CommandReceipt | undefined + let submissionWritten = false + const receipt = composeJournalOperationReceipts( + buildCommandReceiptTransaction( + commandReceiptScope(operationCallerKey, plan.operationIdScope), + (row) => { + if (row && row.kind !== 'submission') { + throw new Error('Send acceptance requires a submission') + } + submissionWritten = row !== undefined + acceptedReceipt = { + operationId: envelope.clientOperationId, + sessionId: envelope.sessionId, + callerKey: operationCallerKey, + method: plan.method, + fingerprint, + status: 'accepted', + acceptedAt: context.now(), + result: row + ? { kind: 'journal-row', epoch: row.epoch, sequence: row.seq } + : { kind: 'queued-draft', messageId: envelope.clientOperationId } + } + return acceptedReceipt + } + ), + // TEMPORARY: ledger co-write preserves cross-family identity until every mutation uses receipts. + store.operationOutcomeReceipt({ + callerKey: operationCallerKey, + operationId: envelope.clientOperationId, + fingerprint, + now: context.now(), + ...(plan.operationIdScope ? { operationIdScope: plan.operationIdScope } : {}), + outcome: { status: 'succeeded', sessionId: envelope.sessionId } + }) + ) + let committed = false + try { + const ran = await plan.run({ + ...context, + operationReceipt: { + write: receipt.write, + committed: () => { + committed = true + receipt.committed() + } + } + }) + if (!committed || ran.ok) { + return ran + } + context.logger.warn('publishing an accepted command failed; replaying its receipt', { + scope: 'command-receipt-publication', + sessionId: envelope.sessionId, + operationId: envelope.clientOperationId, + refusal: ran.refusal.code + }) + } catch (error) { + if (!committed) { + throw error + } + context.logger.warn('publishing an accepted command failed; replaying its receipt', { + scope: 'command-receipt-publication', + sessionId: envelope.sessionId, + operationId: envelope.clientOperationId, + error + }) + } finally { + if (committed && submissionWritten) { + try { + wakeDelivery?.(envelope.sessionId) + } catch (error) { + context.logger.warn('waking delivery after acceptance failed', { + scope: 'command-receipt-delivery', + sessionId: envelope.sessionId, + error + }) + } + } + } + if (!acceptedReceipt) { + throw new Error('an accepted command requires its committed receipt') + } + return { committedReceipt: acceptedReceipt } +} diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-mutation-admission.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-mutation-admission.ts index 3fac8f4d8709..aec938b85fe6 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-mutation-admission.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-mutation-admission.ts @@ -1,9 +1,8 @@ -// The one route every mutating agent-session call takes: recompute the -// fingerprint, admit through the durable operation ledger, check the lease, then -// run the plan. It lives outside the host so that no method can quietly grow its -// own admission rules by sitting next to the call site. +// The one route every mutating agent-session call takes: recompute the fingerprint, +// dispatch submission-accepting plans to their command receipt, otherwise admit +// through the ledger and check the lease. Methods cannot grow their own admission rules. // -// Admission is two-phase for a call that brings a `prepareSession`. The ledger's +// Ledger-backed admission is two-phase when a call brings `prepareSession`. The ledger's // answer comes first and places nothing. An id it refuses is answered then, with // nothing opened; so is a recorded id that settled refused. Any other recorded id // is answered after the plan's own preparation for a replay (a send's only opens @@ -23,14 +22,12 @@ import type { AgentSessionOperationRow } from '../../../shared/agent-session-operation-ledger' import type { AgentSessionRecord } from '../../../shared/agent-session-record' -import { - refuse, - type AgentSessionMutationEnvelope, - type AgentSessionMutationResult, - type AgentSessionWireRefusal +import type { + AgentSessionMutationEnvelope, + AgentSessionMutationResult, + AgentSessionWireRefusal } from '../../../shared/agent-session-wire' import { isAgentSessionRefusalError } from '../../../shared/agent-session-wire-refusals' -import { AGENT_SESSION_UNATTACHED_REFUSAL_CODE } from '../../../shared/structured-agent-session-read-refusal' import type { AgentSessionMutationOperationAdmission, AgentSessionMutationOperationDecision @@ -52,21 +49,16 @@ import type { AgentSessionTurnContext } from './structured-agent-session-turns' import { mutationTurnContext } from './structured-agent-session-mutation-turn-context' import type { StructuredAgentSessionLogger } from './structured-agent-session-logger' import type { StructuredAgentRegistry } from './structured-agent-registry' +import { admitCommandReceiptMutation } from './structured-agent-session-command-admission' -// The code is shared with the client so a read that refuses this way can be told apart from a -// transcript that failed to load; the two must never drift apart. -export const AGENT_SESSION_NOT_ATTACHED: AgentSessionWireRefusal = refuse( - AGENT_SESSION_UNATTACHED_REFUSAL_CODE, - { reason: 'sessionNotAttached' }, - 'This host holds no attached session by that id.' -) - -export function refuseAgentSessionMutation(refusal: AgentSessionWireRefusal): { - ok: false - refusal: AgentSessionWireRefusal -} { - return { ok: false, refusal } -} +import { + AGENT_SESSION_NOT_ATTACHED, + refuseAgentSessionMutation +} from './structured-agent-session-mutation-refusals' +export { + AGENT_SESSION_NOT_ATTACHED, + refuseAgentSessionMutation +} from './structured-agent-session-mutation-refusals' export type AgentSessionMutationSessionPreparation = | { ok: true } @@ -89,6 +81,7 @@ export type AgentSessionMutationRequest = { record: AgentSessionRecord ) => Promise publish: (journal: AgentSessionJournal) => void + wakeDelivery?: (sessionId: string) => void now: () => number } @@ -105,6 +98,9 @@ export async function admitAndRunAgentSessionMutation( if (conflict) { return refuseAgentSessionMutation(conflict) } + if (plan.acceptsWithCommandReceipt) { + return admitCommandReceiptMutation(request, hostFingerprint) + } if (request.prepareSession) { const ledger = request.store.evaluateMutationOperation({ callerKey: request.callerKey, diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-mutation-context.test.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-mutation-context.test.ts new file mode 100644 index 000000000000..99120dcc23b5 --- /dev/null +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-mutation-context.test.ts @@ -0,0 +1,239 @@ +import { afterEach, beforeEach, expect, it, vi } from 'vitest' +import * as reducer from '../agent-session-journal/journal-reducer' +import { DISPATCH_DOUBT_SUBMISSION_MISSING } from '../agent-session-journal/journal-dispatch-doubt-reasons' +import { sendStructuredAgentSessionTurn } from './structured-agent-session-host-mutations' +import { mutateStructuredAgentSession } from './structured-agent-session-mutation-context' +import { sendPlan } from './structured-agent-session-mutation-plans' +import { runStructuredCompaction } from './structured-conversation-compaction' +import { + createQueuedMessageTestRig, + QUEUED_RIG_CALLER as CALLER, + type QueuedMessageTestRig +} from './structured-agent-session-queued-message-rig.test-fixture' +import { + HOST_TEST_SESSION as SESSION, + hostTestMessage, + hostTestOperationId +} from './structured-agent-session-host-test-data' + +let rig: QueuedMessageTestRig +beforeEach(async () => { + rig = await createQueuedMessageTestRig() +}) +afterEach(async () => { + vi.restoreAllMocks() + await rig.dispose() +}) + +function params(delivery?: 'queue-if-active') { + const body = hostTestMessage('accepted once') + const fields = { body, ...(delivery ? { delivery } : {}) } + return { ...fields, envelope: rig.envelope(fields, 'agentSession.send', hostTestOperationId()) } +} + +function context() { + return { ...rig.host['mutationContext'](), wakeDelivery: vi.fn() } +} + +it('wakes once for a direct send and never for its receipt replay', async () => { + const ctx = context() + const request = params() + expect(await sendStructuredAgentSessionTurn(ctx, CALLER, request)).toMatchObject({ + ok: true, + replayed: false + }) + expect(ctx.wakeDelivery).toHaveBeenCalledExactlyOnceWith(SESSION) + expect(await sendStructuredAgentSessionTurn(ctx, CALLER, request)).toMatchObject({ + ok: true, + replayed: true + }) + expect(ctx.wakeDelivery).toHaveBeenCalledOnce() + expect(rig.host['sessions'].get(SESSION)?.journal.submissions()).toHaveLength(1) +}) + +it('does not wake for a queued draft or its replay', async () => { + await rig.workingSend() + const ctx = context() + const request = params('queue-if-active') + const accepted = await sendStructuredAgentSessionTurn(ctx, CALLER, request) + expect(accepted).toMatchObject({ + ok: true, + replayed: false, + value: { queued: { state: 'waiting' } } + }) + const replay = await sendStructuredAgentSessionTurn(ctx, CALLER, request) + expect(replay).toMatchObject({ ok: true, replayed: true }) + if (!accepted.ok || !replay.ok) { + throw new Error('expected accepted drafts') + } + expect(replay.value).toEqual(accepted.value) + expect(ctx.wakeDelivery).not.toHaveBeenCalled() +}) + +it('wakes once after a direct send commits even when publication fails', async () => { + const ctx = context() + const request = params() + const error = new Error('listener failed after commit') + rig.host['sessions'].get(SESSION)?.journal.observeCommits( + vi.fn().mockImplementationOnce(() => { + throw error + }) + ) + const warn = vi.spyOn(ctx.deps.logger, 'warn') + expect(await sendStructuredAgentSessionTurn(ctx, CALLER, request)).toMatchObject({ + ok: true, + replayed: true + }) + expect(ctx.wakeDelivery).toHaveBeenCalledExactlyOnceWith(SESSION) + expect(warn).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ scope: 'send-journal-write', error }) + ) + expect(warn).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ + scope: 'command-receipt-publication', + refusal: 'agent_session_operation_invalid' + }) + ) +}) + +it('replays a queued draft after publication fails without waking delivery', async () => { + await rig.workingSend() + const ctx = context() + const request = params('queue-if-active') + rig.host['sessions'].get(SESSION)?.journal.observeCommits( + vi.fn().mockImplementationOnce(() => { + throw new Error('draft publication failed') + }) + ) + const accepted = await sendStructuredAgentSessionTurn(ctx, CALLER, request) + const replay = await sendStructuredAgentSessionTurn(ctx, CALLER, request) + expect(accepted).toMatchObject({ + ok: true, + replayed: true, + value: { queued: { state: 'waiting' } } + }) + if (!accepted.ok || !replay.ok) { + throw new Error('expected accepted drafts') + } + expect(replay.value).toEqual(accepted.value) + expect(ctx.wakeDelivery).not.toHaveBeenCalled() +}) + +it('answers the same doubt on the first reply and resend when the committed row cannot fold', async () => { + const ctx = context() + const request = params() + const fold = reducer.applyJournalRow + vi.spyOn(reducer, 'applyJournalRow').mockImplementation((state, row) => { + if (row.kind === 'submission') { + throw new Error('submission fold failed') + } + fold(state, row) + }) + const journal = rig.host['sessions'].get(SESSION)!.journal + const append = vi.spyOn(journal, 'appendSubmission') + const accepted = await sendStructuredAgentSessionTurn(ctx, CALLER, request) + const replay = await sendStructuredAgentSessionTurn(ctx, CALLER, request) + expect(accepted).toMatchObject({ + ok: true, + replayed: true, + value: { + submission: { + dispatchState: 'unknown', + reason: DISPATCH_DOUBT_SUBMISSION_MISSING, + recovered: true + } + } + }) + expect(replay).toEqual(accepted) + expect(journal.submissions()).toHaveLength(0) + expect( + rig.store.readCommandReceipt({ kind: 'global' }, request.envelope.clientOperationId) + ).toMatchObject({ verdict: 'readable', receipt: { status: 'accepted' } }) + expect(append).toHaveBeenCalledOnce() + expect(ctx.wakeDelivery).toHaveBeenCalledOnce() +}) + +it('logs a throw after commit and wakes once before answering from receipt replay', async () => { + const ctx = context() + const request = params() + const plan = sendPlan(request) + const error = new Error('plan publication failed') + const warn = vi.spyOn(ctx.deps.logger, 'warn') + const accepted = await mutateStructuredAgentSession(ctx, CALLER, request.envelope, { + ...plan, + run: async (turn) => { + await plan.run(turn) + throw error + } + }) + expect(accepted).toMatchObject({ + ok: true, + replayed: true, + value: { submission: { dispatchState: 'pending' } } + }) + expect(warn).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ scope: 'command-receipt-publication', error }) + ) + expect(ctx.wakeDelivery).toHaveBeenCalledOnce() +}) + +it('logs a wake error and still answers accepted', async () => { + const ctx = context() + const error = new Error('wake failed') + ctx.wakeDelivery.mockImplementation(() => { + throw error + }) + const warn = vi.spyOn(ctx.deps.logger, 'warn') + expect(await sendStructuredAgentSessionTurn(ctx, CALLER, params())).toMatchObject({ ok: true }) + expect(ctx.wakeDelivery).toHaveBeenCalledOnce() + expect(warn).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ scope: 'command-receipt-delivery', error }) + ) +}) + +it('logs a receipt read error and refuses unknown without accepting or waking', async () => { + const ctx = context() + const request = params() + const error = new Error('receipt read failed') + const warn = vi.spyOn(ctx.deps.logger, 'warn') + vi.spyOn(rig.store, 'readCommandReceipt').mockImplementationOnce(() => { + throw error + }) + expect(await sendStructuredAgentSessionTurn(ctx, CALLER, request)).toMatchObject({ + ok: false, + refusal: { code: 'agent_session_operation_unknown', details: { reason: 'outcomeUnknown' } } + }) + expect(warn).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ + scope: 'command-receipt-read', + operationId: request.envelope.clientOperationId, + error + }) + ) + expect(rig.host['sessions'].get(SESSION)?.journal.submissions()).toHaveLength(0) + expect(ctx.wakeDelivery).not.toHaveBeenCalled() +}) + +it('wakes once for /compact and never for its replay', async () => { + const ctx = context() + const fields = { command: 'compact' as const } + const request = { + ...fields, + envelope: rig.envelope(fields, 'agentSession.conversationCommand', hostTestOperationId()) + } + const host = { waitForSendSettlement: vi.fn(async () => undefined) } + expect(await runStructuredCompaction(ctx, host, CALLER, request)).toMatchObject({ + ok: true, + replayed: false + }) + expect(ctx.wakeDelivery).toHaveBeenCalledExactlyOnceWith(SESSION) + expect( + await runStructuredCompaction(ctx, host, { callerKey: 'second-caller' }, request) + ).toMatchObject({ ok: true, replayed: true }) + expect(ctx.wakeDelivery).toHaveBeenCalledOnce() +}) diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-mutation-context.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-mutation-context.ts index 1f543cbf19f0..f9b83c3b4f3a 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-mutation-context.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-mutation-context.ts @@ -74,6 +74,7 @@ export function mutateStructuredAgentSession( journal: () => context.sessions.get(envelope.sessionId)?.journal, prepareSession, publish: (journal) => context.publish(envelope.sessionId, journal), + wakeDelivery: (sessionId) => context.wakeDelivery(sessionId), now: () => context.now() }) ) diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-mutation-plans.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-mutation-plans.ts index 7d2a1e20067b..6d4520f9c9d8 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-mutation-plans.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-mutation-plans.ts @@ -1,11 +1,8 @@ // One plan per mutating method: what it fingerprints, what it does, and how its // answer is rebuilt on a replay. // -// The replay half matters more than it looks. The ledger records only that an -// operation happened, so the durable answer usually comes back out of the -// journal. Send is fail-closed: admission alone cannot prove non-delivery, so -// its success commits with the row that accepts it, and a row still pending is -// one that wrote nothing. +// Send and /compact prove acceptance by their command receipt, the rest by their ledger row; the +// journal projects the current answer. import type { AgentJournalMessageItem } from '../../../shared/agent-session-journal-types' import { @@ -58,14 +55,23 @@ export type MutationPlan = { rerunWhenReplayMissing?: (ctx: AgentSessionTurnContext) => boolean recoverUnknownFromDurableState?: boolean } & ( + | { + /** Accepts with a submission or draft; its command receipt commits in that write's transaction. */ + acceptsWithCommandReceipt: true + settlesWithWrite?: never + successReceipt?: never + settledOutcome?: never + } | { /** Commits success with its row; paths without a committed receipt use fallback settlement. */ settlesWithWrite: true + acceptsWithCommandReceipt?: never successReceipt?: () => JournalOperationReceipt settledOutcome?: (value: TValue) => AgentSessionOperationOutcome } | { settlesWithWrite?: never + acceptsWithCommandReceipt?: never settledOutcome?: (value: TValue) => AgentSessionOperationOutcome } ) @@ -101,7 +107,7 @@ export function sendPlan(params: { method: 'agentSession.send', operationIdScope: 'global', conversationWrite: true, - settlesWithWrite: true, + acceptsWithCommandReceipt: true, // `delivery` joins the OPERATION fingerprint only; the submission row keeps // the body-only fingerprint the reducer's echo-aliasing recomputes. fields: { @@ -184,8 +190,9 @@ export function conversationCommandPlan(params: { const clientMessageId = params.envelope.clientOperationId return { method: 'agentSession.conversationCommand', + operationIdScope: 'global', conversationWrite: true, - settlesWithWrite: true, + acceptsWithCommandReceipt: true, fields: { command: STRUCTURED_AGENT_SESSION_COMPACT_COMMAND, ...(params.delivery ? { delivery: params.delivery } : {}) @@ -202,10 +209,7 @@ export function conversationCommandPlan(params: { }) return sent.ok ? { ok: true, value: { clientMessageId } } : sent }, - replay: (ctx, outcome) => { - if (outcome.status === 'succeeded' && outcome.conversationCommand) { - return { recorded: outcome.conversationCommand } - } + replay: (ctx) => { // A card answers from itself until drained, then from the submission it became; only a // command that asked to wait can have one, as for a send. const queued = diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-mutation-refusals.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-mutation-refusals.ts new file mode 100644 index 000000000000..f59f15712253 --- /dev/null +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-mutation-refusals.ts @@ -0,0 +1,15 @@ +import { refuse, type AgentSessionWireRefusal } from '../../../shared/agent-session-wire' +import { AGENT_SESSION_UNATTACHED_REFUSAL_CODE } from '../../../shared/structured-agent-session-read-refusal' + +export const AGENT_SESSION_NOT_ATTACHED: AgentSessionWireRefusal = refuse( + AGENT_SESSION_UNATTACHED_REFUSAL_CODE, + { reason: 'sessionNotAttached' }, + 'This host holds no attached session by that id.' +) + +export function refuseAgentSessionMutation(refusal: AgentSessionWireRefusal): { + ok: false + refusal: AgentSessionWireRefusal +} { + return { ok: false, refusal } +} diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-operation-settlement.test.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-operation-settlement.test.ts index 0ed7c85e2b81..ce8e68798fbf 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-operation-settlement.test.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-operation-settlement.test.ts @@ -1,4 +1,3 @@ -import { join } from 'node:path' import { afterEach, expect, it, vi } from 'vitest' import { AgentSessionPreDispatchError, @@ -6,6 +5,7 @@ import { } from './structured-agent-session-operation-settlement' import { adapter, + attach, envelope, hostTestState, journals @@ -17,11 +17,13 @@ import { } from './structured-agent-session-host-test-data' import type { AgentSessionTurnContext } from './structured-agent-session-turns' import { sendPlan } from './structured-agent-session-mutation-plans' +import { runCommandReceiptMutation } from './structured-agent-session-command-receipt' import { createStructuredAgentSessionLogger } from './structured-agent-session-logger' import { codexProviderHandle } from '../../../shared/agent-session-provider-handle-encoding' import { NO_STRUCTURED_AGENTS } from './structured-agent-session-adapter-router-test-support' async function context(): Promise { + await attach() return { logger: createStructuredAgentSessionLogger(), sessionId: SESSION, @@ -33,7 +35,7 @@ async function context(): Promise { agent: 'codex', providerHandle: codexProviderHandle(THREAD) }, - stateDirectory: join(hostTestState().root, 'settlement') + stateDirectory: hostTestState().root }), fence: 1, agents: NO_STRUCTURED_AGENTS, @@ -57,9 +59,10 @@ it('rethrows a throw from a plan answered by its write, writing nothing and leav const writes = vi.spyOn(store, 'recordOperationOutcome') const refusal = new AgentSessionPreDispatchError('agent_session_restart_work_superseded') vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] }) - const result = await runSettledAgentSessionMutation({ + const result = await runCommandReceiptMutation({ store, operationCallerKey: 'test', + fingerprint: 'test-fingerprint', envelope: envelope('agentSession.send', {}), context: ctx, plan: { @@ -79,7 +82,7 @@ it('rethrows a throw from a plan answered by its write, writing nothing and leav }) it.each([ - { plan: 'answered by its write', settlesWithWrite: true as const, failures: 1 }, + { plan: 'answered by its write', settlesWithWrite: true as const, failures: 0 }, { plan: 'settled after its run', settlesWithWrite: undefined, failures: 2 } ])( 'preserves a proven refusal of a plan $plan through $failures failed bookkeeping writes', @@ -95,19 +98,20 @@ it.each([ refusal: { code: 'agent_session_operation_invalid' as const, message: 'Not sent.' } } const run = vi.fn(async () => refusal) - const result = await runSettledAgentSessionMutation({ + const input = { store, operationCallerKey: 'test', envelope: envelope('agentSession.send', {}), context: ctx, - plan: { - method: 'agentSession.send', - fields: {}, - ...(settlesWithWrite ? { settlesWithWrite } : {}), - run, - replay: () => null - } - }) + plan: { method: 'agentSession.send', fields: {}, run, replay: () => null } + } + const result = settlesWithWrite + ? await runCommandReceiptMutation({ + ...input, + fingerprint: 'test-fingerprint', + plan: { ...input.plan, settlesWithWrite } + }) + : await runSettledAgentSessionMutation(input) expect(result).toEqual(refusal) expect(writes).toHaveBeenCalledTimes(failures) expect(run).toHaveBeenCalledOnce() @@ -125,9 +129,10 @@ it('accepts without touching the provider', async () => { const beforeRun = vi.fn() const body = hostTestMessage('Continue the interrupted work') const operation = envelope('agentSession.send', { body }) - const result = await runSettledAgentSessionMutation({ + const result = await runCommandReceiptMutation({ store, operationCallerKey: 'test', + fingerprint: 'test-fingerprint', envelope: operation, context: ctx, plan: sendPlan({ envelope: operation, body, beforeRun }) @@ -151,9 +156,10 @@ it('refuses a superseded send at acceptance, recording and dispatching nothing', const body = hostTestMessage('Continue the interrupted work') const operation = envelope('agentSession.send', { body }) vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] }) - const result = await runSettledAgentSessionMutation({ + const result = await runCommandReceiptMutation({ store, operationCallerKey: 'test', + fingerprint: 'test-fingerprint', envelope: operation, context: ctx, plan: sendPlan({ envelope: operation, body, beforeRun }) @@ -165,7 +171,7 @@ it('refuses a superseded send at acceptance, recording and dispatching nothing', expect(vi.getTimerCount()).toBe(0) }) -it('keeps the original refusal reason and message in its settled receipt', async () => { +it('returns the pre-acceptance refusal without recording a receipt', async () => { const ctx = await context() const { store } = hostTestState() const writes = vi.spyOn(store, 'recordOperationOutcome').mockResolvedValue() @@ -177,9 +183,10 @@ it('keeps the original refusal reason and message in its settled receipt', async message: 'The message could not be saved.' } expect( - await runSettledAgentSessionMutation({ + await runCommandReceiptMutation({ store, operationCallerKey: 'test', + fingerprint: 'test-fingerprint', envelope: operation, context: ctx, plan: { @@ -188,9 +195,5 @@ it('keeps the original refusal reason and message in its settled receipt', async } }) ).toMatchObject({ ok: false, refusal }) - expect(writes).toHaveBeenCalledWith({ - callerKey: 'test', - operationId: operation.clientOperationId, - outcome: { status: 'failed', ...refusal } - }) + expect(writes).not.toHaveBeenCalled() }) diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-operation-settlement.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-operation-settlement.ts index fa9a88029ec5..56c49b42d6c0 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-operation-settlement.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-operation-settlement.ts @@ -16,7 +16,7 @@ export async function runSettledAgentSessionMutation(input: { store: AgentSessionRecordStore operationCallerKey: string envelope: AgentSessionMutationEnvelope - plan: MutationPlan + plan: Exclude, { acceptsWithCommandReceipt: true }> context: AgentSessionTurnContext }): Promise> { const operation = { diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-queued-command.test.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-queued-command.test.ts index b4efad3404e5..5c148bbe8081 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-queued-command.test.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-queued-command.test.ts @@ -10,6 +10,7 @@ import { import { HOST_TEST_SESSION as SESSION, hostTestMessage, + hostTestAttachParams, hostTestOperationId } from './structured-agent-session-host-test-data' @@ -195,3 +196,89 @@ describe('/clear', () => { expect(journal.submission(sent.clientMessageId)?.dispatchState).toBe('accepted') }) }) + +it('replays /compact across callers with one submission and one provider command', async () => { + const id = hostTestOperationId() + const fields = { command: 'compact' as const } + const params = { + ...fields, + envelope: rig.envelope(fields, 'agentSession.conversationCommand', id) + } + const admit = vi + .spyOn(rig.store, 'admitMutationOperation') + .mockRejectedValue(new Error('ledger unavailable')) + const original = await rig.host.conversationCommand(CALLER, params) + expect(original).toMatchObject({ ok: true, replayed: false }) + await eventually(() => expect(rig.compact).toHaveBeenCalledOnce()) + const replay = await rig.host.conversationCommand({ callerKey: 'second-caller' }, params) + expect(replay).toMatchObject({ ok: true, replayed: true }) + if (!original.ok || !replay.ok) { + throw new Error('expected accepted commands') + } + expect(replay.value).toEqual(original.value) + expect((await rig.host.journalSnapshot(params.envelope.sessionId)).submissions).toHaveLength(1) + expect(rig.store.readCommandReceipt({ kind: 'caller', callerKey: 'second-caller' }, id)).toEqual({ + verdict: 'absent' + }) + expect(rig.store.readCommandReceipt({ kind: 'global' }, id)).toMatchObject({ + verdict: 'readable', + receipt: { callerKey: CALLER.callerKey } + }) + expect(admit).not.toHaveBeenCalled() + expect(rig.compact).toHaveBeenCalledOnce() +}) + +it('refuses another caller reusing a send id for /compact as a receipt conflict', async () => { + const { id, result } = rig.send('a different request') + await result + const fields = { command: 'compact' as const } + await expect( + rig.host.conversationCommand( + { callerKey: 'second-caller' }, + { + ...fields, + envelope: rig.envelope(fields, 'agentSession.conversationCommand', id) + } + ) + ).resolves.toMatchObject({ + ok: false, + refusal: { code: 'agent_session_operation_conflict', details: { reason: 'operationIdReused' } } + }) + expect((await rig.host.journalSnapshot(SESSION)).submissions).toHaveLength(1) + expect(rig.compact).not.toHaveBeenCalled() +}) + +it("refuses /compact reusing another caller's id for a different conversation", async () => { + const id = hostTestOperationId() + const fields = { command: 'compact' as const } + await expect( + rig.host.conversationCommand(CALLER, { + ...fields, + envelope: rig.envelope(fields, 'agentSession.conversationCommand', id) + }) + ).resolves.toMatchObject({ ok: true }) + const otherSessionId = 'session-beta' + const caller = { callerKey: 'second-caller' } + const attach = hostTestAttachParams(null) + await expect( + rig.host.attach( + caller, + hostTestAttachParams(null, { + envelope: { ...attach.envelope, sessionId: otherSessionId }, + providerHandle: { kind: 'codex', threadId: '019fd532-7c11-7a90-b6de-4e1a2c3d5f61' } + }) + ) + ).resolves.toMatchObject({ ok: true }) + await expect( + rig.host.conversationCommand(caller, { + ...fields, + envelope: rig.envelope(fields, 'agentSession.conversationCommand', id, otherSessionId) + }) + ).resolves.toMatchObject({ + ok: false, + refusal: { code: 'agent_session_operation_conflict', details: { reason: 'operationIdReused' } } + }) + expect((await rig.host.journalSnapshot(SESSION)).submissions).toHaveLength(1) + expect((await rig.host.journalSnapshot(otherSessionId)).submissions).toHaveLength(0) + expect(rig.compact).toHaveBeenCalledOnce() +}) diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-queued-gate.test.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-queued-gate.test.ts index 09ba1797b29a..b9f6e3167920 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-queued-gate.test.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-queued-gate.test.ts @@ -6,6 +6,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../shared/agent-session-journal-types' import { structuredQueueHold } from './structured-agent-session-queued-messages' +import { openTestJournalHostDatabase } from '../agent-session-journal/journal-host-database-test-support' import { createQueuedMessageTestRig, eventually, @@ -230,7 +231,7 @@ describe('the hand-off link on answers', () => { expect(await submission(working)).not.toHaveProperty('queuedMessageId') }) - it('a queued send asked again after its ledger row is gone answers with its hand-off, never sending twice', async () => { + it('a queued send asked again after its receipt is gone answers with its hand-off, never sending twice', async () => { const working = await workingSend() const body = hostTestMessage('asked again') const clientOperationId = hostTestOperationId() @@ -247,13 +248,10 @@ describe('the hand-off link on answers', () => { await settleAccepted(working, 'a') await eventually(async () => expect(await rig.handoff(clientOperationId)).toBeDefined()) const handedOffAs = await rig.handoffId(clientOperationId) - // The ledger forgot the id, so the send runs again rather than replaying. - const operations = store['transactions'].state.operations - for (const [key, row] of operations) { - if (row.operationId === clientOperationId) { - operations.delete(key) - } - } + // As an older build accepted it, with no receipt: the send runs again rather than replaying. + openTestJournalHostDatabase(rig.root) + .db.prepare('DELETE FROM agent_session_command_receipts WHERE operation_id = ?') + .run(clientOperationId) const count = (await host.journalSnapshot(SESSION)).submissions.length expect(await host.send(CALLER, params)).toMatchObject({ ok: true, diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-queued-pause-lift.test.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-queued-pause-lift.test.ts index 418c51f5cc74..75ff328b012e 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-queued-pause-lift.test.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-queued-pause-lift.test.ts @@ -4,6 +4,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { AgentSessionJournal } from '../agent-session-journal/journal-store' +import { openTestJournalHostDatabase } from '../agent-session-journal/journal-host-database-test-support' import { AGENT_JOURNAL_THREAD_SCOPE } from '../../../shared/agent-session-journal-types' import { HOST_TEST_SESSION, @@ -168,18 +169,16 @@ describe("a Stop's queue pause", () => { await expectPaused(heldId) }) - it('an old send answered again after its ledger row is gone lifts nothing from a later Stop', async () => { + it('an old send answered again after its receipt is gone lifts nothing from a later Stop', async () => { const working = await rig.workingSend() const draftId = await queuedDraft('paused by stop') await rig.stop() await rig.settleAccepted(working, 'stopped') - // The ledger forgot the id, so the send runs again and answers with its accepted submission. - const operations = rig.store['transactions'].state.operations - for (const [key, row] of operations) { - if (row.operationId === working) { - operations.delete(key) - } - } + // As an older build accepted it, with no receipt: the send runs again and answers with its + // accepted submission. + openTestJournalHostDatabase(rig.root) + .db.prepare('DELETE FROM agent_session_command_receipts WHERE operation_id = ?') + .run(working) const body = hostTestMessage('work on this') const replayed = await rig.host.send(QUEUED_RIG_CALLER, { envelope: rig.envelope({ body }, 'agentSession.send', working), diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-queued-repeat-press.test.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-queued-repeat-press.test.ts index 82e750b908be..11084d947164 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-queued-repeat-press.test.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-queued-repeat-press.test.ts @@ -5,11 +5,17 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { createQueuedMessageTestRig, + QUEUED_RIG_CALLER as CALLER, eventually, type QueuedMessageTestRig } from './structured-agent-session-queued-message-rig.test-fixture' import { openRigTurnFor } from './structured-agent-session-queued-rig-turn.test-fixture' -import { HOST_TEST_SESSION as SESSION } from './structured-agent-session-host-test-data' +import { openTestJournalHostDatabase } from '../agent-session-journal/journal-host-database-test-support' +import { + hostTestMessage, + hostTestOperationId, + HOST_TEST_SESSION as SESSION +} from './structured-agent-session-host-test-data' let rig: QueuedMessageTestRig @@ -71,3 +77,98 @@ describe('a repeated draft press under a fresh operation id', () => { expect(await rig.queuePause()).toBeNull() }) }) + +describe('a queued Send receipt', () => { + function params(id: string, text: string) { + const body = hostTestMessage(text) + const fields = { body, delivery: 'queue-if-active' as const } + return { ...fields, envelope: rig.envelope(fields, 'agentSession.send', id) } + } + + it('accepts concurrent copies once and replays through the draft and its handoff', async () => { + const working = await rig.workingSend() + const id = hostTestOperationId() + const send = params(id, 'one queued message') + const [first, second] = await Promise.all([ + rig.host.send(CALLER, send), + rig.host.send(CALLER, send) + ]) + expect(first).toMatchObject({ ok: true, replayed: false }) + expect(second).toMatchObject({ + ok: true, + replayed: true, + value: { queued: { state: 'waiting' } } + }) + expect(await rig.drafts()).toHaveLength(1) + expect(rig.store.readCommandReceipt({ kind: 'global' }, id)).toMatchObject({ + verdict: 'readable', + receipt: { result: { kind: 'queued-draft', messageId: id } } + }) + await rig.settleAccepted(working, 'finished') + await eventually(async () => expect(await rig.handoff(id)).toBeDefined()) + expect(await rig.host.send(CALLER, send)).toMatchObject({ + ok: true, + replayed: true, + value: { submission: { queuedMessageId: id } } + }) + const submissions = (await rig.host.journalSnapshot(SESSION)).submissions + expect(submissions.filter((entry) => entry.queuedMessageId === id)).toHaveLength(1) + }) + + it('replays a withdrawn draft as spent without sending it', async () => { + await rig.workingSend() + const id = hostTestOperationId() + const send = params(id, 'withdraw me') + await rig.host.send(CALLER, send) + await rig.deleteQueued(id) + expect(await rig.host.send(CALLER, send)).toMatchObject({ + ok: true, + replayed: true, + value: { queued: { messageId: id, state: 'withdrawn' } } + }) + expect(await rig.handoff(id)).toBeUndefined() + expect(rig.dispatch).toHaveBeenCalledTimes(1) + }) + + 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 + BEGIN SELECT RAISE(ABORT, 'receipt unavailable'); END`) + await expect(rig.host.send(CALLER, params(id, 'must roll back'))).rejects.toThrow( + 'receipt unavailable' + ) + expect(await rig.drafts()).toHaveLength(0) + expect(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') + expect(await rig.host.send(CALLER, params(id, 'must roll back'))).toMatchObject({ + ok: true, + replayed: false + }) + }) +}) + +it('returns a committed queued Send after its publication fails', async () => { + await rig.workingSend() + const id = hostTestOperationId() + const body = hostTestMessage('committed queued draft') + const fields = { body, delivery: 'queue-if-active' as const } + const params = { ...fields, envelope: rig.envelope(fields, 'agentSession.send', id) } + const journal = rig.host.collaboratorsForTests().sessions.get(SESSION)!.journal + journal.observeCommits(() => { + throw new Error('draft publication failed') + }) + try { + expect(await rig.host.send(CALLER, params)).toMatchObject({ + ok: true, + replayed: true, + value: { queued: { messageId: id, position: 1, state: 'waiting' } } + }) + expect(await rig.host.send(CALLER, params)).toMatchObject({ ok: true, replayed: true }) + expect(await rig.drafts()).toHaveLength(1) + } finally { + journal.observeCommits(() => {}) + } +}) diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-queued-send.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-queued-send.ts index 1652fbca97f2..d472e159fcbc 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-queued-send.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-queued-send.ts @@ -26,9 +26,5 @@ export async function runQueueableStructuredAgentSessionSend( if (queued) { return queued } - const accepted = await immediate() - if (accepted.ok) { - context.wakeDelivery(ctx.sessionId) - } - return accepted + return immediate() } diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-refusal-retry.test.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-refusal-retry.test.ts index 2a201e94ef0a..01639485bd25 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-refusal-retry.test.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-refusal-retry.test.ts @@ -170,7 +170,11 @@ async function assertHostAgreement( } const outcome = operationState(harness, spec.operationId) let oracle: AgentSessionRefusalOperationState - if (outcome?.status === 'failed') { + if ( + harness.store.readCommandReceipt({ kind: 'global' }, spec.operationId).verdict === 'unreadable' + ) { + oracle = 'unknown' + } else if (outcome?.status === 'failed') { oracle = 'settled-rejected' } else if (outcome?.status === 'unknown') { oracle = 'unknown' @@ -211,8 +215,6 @@ const UNREACHABLE = new Set([ 'agentSession.send:agent_session_identity_required', // Only a send opens the conversation it writes to. 'agentSession.setOption:agent_session_journal_unreadable', - // Send reconstructs doubt from its global tombstone instead of refusing it. - 'agentSession.send:agent_session_operation_unknown', // Only a send restarts a lost owner. 'agentSession.setOption:agent_session_owner_restart_failed', // A write names its target, not an owner generation; only an attach compares fences. @@ -223,6 +225,8 @@ const UNREACHABLE = new Set([ 'agentSession.send:agent_session_conflict', 'agentSession.send:execution_owner_reconciling', 'agentSession.send:agent_session_owner_restart_failed', + // Send admits through its receipt, which has no age budget. + 'agentSession.send:agent_session_operation_expired', // The ledger has no count limit; only an older host still refuses with it. 'agentSession.setOption:agent_session_operation_capacity', 'agentSession.send:agent_session_operation_capacity' @@ -285,11 +289,15 @@ describe('agentSessionRefusalOperationState host oracle', () => { ) } const ledgerRefusals = await createHarness() - for (const [code, timestamp] of [ - ['agent_session_operation_expired', NOW - AGENT_SESSION_MAX_NEW_OPERATION_AGE_MS - 1], - ['agent_session_operation_invalid', null] + for (const [code, timestamp, methods] of [ + [ + 'agent_session_operation_expired', + NOW - AGENT_SESSION_MAX_NEW_OPERATION_AGE_MS - 1, + ['agentSession.setOption'] + ], + ['agent_session_operation_invalid', null, METHODS] ] as const) { - for (const method of METHODS) { + for (const method of methods) { record( await assertHostAgreement( ledgerRefusals, @@ -308,6 +316,14 @@ describe('agentSessionRefusalOperationState host oracle', () => { const optionUnknown = { method: 'agentSession.setOption' as const, operationId: operationId() } await expect(invoke(unknown, optionUnknown)).rejects.toThrow('reply lost') record(await assertHostAgreement(unknown, optionUnknown, 'agent_session_operation_unknown')) + const sendUnknown = { method: 'agentSession.send' as const, operationId: operationId() } + await expect(invoke(unknown, sendUnknown)).resolves.toMatchObject({ ok: true }) + openTestJournalHostDatabase(unknown.root) + .db.prepare( + "UPDATE agent_session_command_receipts SET result_json = '{' WHERE operation_id = ?" + ) + .run(sendUnknown.operationId) + record(await assertHostAgreement(unknown, sendUnknown, 'agent_session_operation_unknown')) const reconciling = await createHarness() for (const method of ['agentSession.setOption'] as const) { diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-resend-answer.test.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-resend-answer.test.ts index bc40f51c4661..f3737ba10865 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-resend-answer.test.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-resend-answer.test.ts @@ -17,6 +17,7 @@ import { } from './structured-agent-session-host-test-harness' import { HOST_TEST_SESSION as SESSION, + HOST_TEST_NOW as NOW, HOST_TEST_THREAD as THREAD, hostTestMessage } from './structured-agent-session-host-test-data' @@ -35,9 +36,7 @@ beforeEach(() => { afterEach(() => vi.restoreAllMocks()) function hostJournal(): AgentSessionJournal { - return ( - host as unknown as { sessions: Map } - ).sessions.get(SESSION)!.journal + return host.collaboratorsForTests().sessions.get(SESSION)!.journal } function sendParams(text: string) { @@ -50,7 +49,7 @@ async function deliveredOnce(): Promise { } describe('a resent send id', () => { - it('is answered from a refused row without opening the chat again', async () => { + it('re-evaluates a pre-acceptance failure after the chat is reopened', async () => { await attach() vi.spyOn(hostJournal(), 'appendSubmission').mockRejectedValueOnce(new Error('disk full')) const params = sendParams('refused once') @@ -64,16 +63,9 @@ describe('a resent send id', () => { const resent = await host.send(CALLER, params) - expect(resent).toMatchObject({ - ok: false, - refusal: { - code: 'agent_session_operation_invalid', - details: { reason: 'journalWriteFailed' } - } - }) - // The answer came from the ledger alone: the closed chat was not opened to give it. - expect(host.hasSession(SESSION)).toBe(false) - expect(dispatch).not.toHaveBeenCalled() + expect(resent).toMatchObject({ ok: true, replayed: false }) + expect(host.hasSession(SESSION)).toBe(true) + await deliveredOnce() }) it('answers unknown, never a refusal, when the chat holding its answer cannot be opened', async () => { @@ -242,3 +234,236 @@ describe('a send queued behind a running turn', () => { } }) }) + +describe('send receipt acceptance', () => { + it.each(['body', 'delivery'] as const)( + 'conflicts when the same id changes its %s', + async (field) => { + await attach() + const params = sendParams('original') + expect(await host.send(CALLER, params)).toMatchObject({ ok: true }) + await deliveredOnce() + const body = field === 'body' ? hostTestMessage('changed') : params.body + const fields = { + body, + ...(field === 'delivery' ? { delivery: 'queue-if-active' as const } : {}) + } + await expect( + host.send(CALLER, { + ...fields, + envelope: envelope('agentSession.send', fields, { + clientOperationId: params.envelope.clientOperationId + }) + }) + ).resolves.toMatchObject({ + ok: false, + refusal: { + code: 'agent_session_operation_conflict', + details: { reason: 'operationIdReused' } + } + }) + expect(dispatch).toHaveBeenCalledTimes(1) + } + ) + + it('conflicts with an unswitched caller ledger row even after its expiry', async () => { + await attach() + const body = hostTestMessage('reuses Stop') + const old = NOW - 3 * 24 * 60 * 60_000 + const params = { + envelope: envelope( + 'agentSession.send', + { body }, + { clientOperationId: `${old}-${'a'.repeat(32)}` } + ), + body + } + await store.admitOperation({ + callerKey: 'another-caller', + operationId: params.envelope.clientOperationId, + fingerprint: envelope('agentSession.cancel', {}).payloadFingerprint, + now: old + }) + expect( + store.listOperationRows().find((row) => row.operationId === params.envelope.clientOperationId) + ?.expiresAt + ).toBeLessThan(NOW) + const before = store.listOperationRows() + await expect(host.send(CALLER, params)).resolves.toMatchObject({ + ok: false, + refusal: { + code: 'agent_session_operation_conflict', + details: { reason: 'operationIdReused' } + } + }) + expect(store.listOperationRows()).toEqual(before) + expect(hostJournal().submissions()).toHaveLength(0) + expect(store.readCommandReceipt({ kind: 'global' }, params.envelope.clientOperationId)).toEqual( + { verdict: 'absent' } + ) + }) + + it('keeps the temporary ledger row so Stop cannot reuse a send id', async () => { + await attach() + const params = sendParams('accepted') + await host.send(CALLER, params) + await deliveredOnce() + await expect( + host.cancel(CALLER, { + envelope: envelope( + 'agentSession.cancel', + {}, + { clientOperationId: params.envelope.clientOperationId } + ) + }) + ).resolves.toMatchObject({ + ok: false, + refusal: { + code: 'agent_session_operation_conflict', + details: { reason: 'operationIdReused' } + } + }) + }) + + it.each(['agent_session_command_receipts', 'agent_session_operations'])( + 'rolls back the message when %s cannot be written, then retries the same id fresh', + async (table) => { + await attach() + const params = sendParams('receipt must commit') + const db = openTestJournalHostDatabase(root).db + db.exec(`CREATE TRIGGER fail_receipt BEFORE INSERT ON ${table} + BEGIN SELECT RAISE(ABORT, 'receipt storage failed'); END`) + await expect(host.send(CALLER, params)).resolves.toMatchObject({ + ok: false, + refusal: { details: { reason: 'journalWriteFailed' } } + }) + expect(hostJournal().submissions()).toHaveLength(0) + expect( + store + .listOperationRows() + .find((row) => row.operationId === params.envelope.clientOperationId) + ).toBeUndefined() + expect( + store.readCommandReceipt({ kind: 'global' }, params.envelope.clientOperationId) + ).toEqual({ verdict: 'absent' }) + expect(dispatch).not.toHaveBeenCalled() + db.exec('DROP TRIGGER fail_receipt') + await expect(host.send(CALLER, params)).resolves.toMatchObject({ ok: true, replayed: false }) + await deliveredOnce() + } + ) + + it('answers unknown for an unreadable receipt and never runs the message again', async () => { + await attach() + const params = sendParams('accepted before corruption') + await host.send(CALLER, params) + await deliveredOnce() + openTestJournalHostDatabase(root) + .db.prepare( + "UPDATE agent_session_command_receipts SET result_json = '{' WHERE operation_id = ?" + ) + .run(params.envelope.clientOperationId) + await expect(host.send(CALLER, params)).resolves.toMatchObject({ + ok: false, + refusal: { code: 'agent_session_operation_unknown', details: { reason: 'outcomeUnknown' } } + }) + expect(dispatch).toHaveBeenCalledTimes(1) + }) + + it('answers an in-transaction duplicate from the receipt after rolling back the second append', async () => { + await attach() + const params = sendParams('accepted once') + await host.send(CALLER, params) + await deliveredOnce() + const journal = hostJournal() + await journal.rollEpoch('schema_unreadable', store.getRecord(SESSION)!.lease.runtimeFence) + const before = journal.cursor() + vi.spyOn(store, 'readCommandReceipt').mockReturnValueOnce({ verdict: 'absent' }) + await expect(host.send(CALLER, params)).resolves.toMatchObject({ + ok: true, + replayed: true, + value: { submission: { clientMessageId: params.envelope.clientOperationId } } + }) + expect(journal.cursor()).toEqual(before) + expect(journal.submissions()).toHaveLength(0) + expect(dispatch).toHaveBeenCalledTimes(1) + }) + + it('returns the committed send through receipt replay after publication fails', async () => { + await attach() + const params = sendParams('accepted before publication failed') + const journal = hostJournal() + const error = new Error('publication failed') + journal.observeCommits( + vi.fn().mockImplementationOnce(() => { + throw error + }) + ) + await expect(host.send(CALLER, params)).resolves.toMatchObject({ + ok: true, + replayed: true, + value: { submission: { dispatchState: 'pending' } } + }) + expect(hostTestState().log.entries).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + fields: expect.objectContaining({ scope: 'send-journal-write', error }) + }), + expect.objectContaining({ + fields: expect.objectContaining({ + scope: 'command-receipt-publication', + refusal: 'agent_session_operation_invalid' + }) + }) + ]) + ) + expect(journal.submissions()).toHaveLength(1) + await expect(host.send(CALLER, params)).resolves.toMatchObject({ ok: true, replayed: true }) + journal.observeCommits(() => {}) + await deliveredOnce() + }) +}) + +it('keeps a same-fingerprint ledger row unchanged while writing the Send receipt', async () => { + await attach() + const params = sendParams('accepted with prior compatibility identity') + await store.admitGlobalOperation({ + callerKey: 'original-caller', + operationId: params.envelope.clientOperationId, + fingerprint: params.envelope.payloadFingerprint, + now: NOW + }) + const prior = store + .listOperationRows() + .find((row) => row.operationId === params.envelope.clientOperationId) + await expect(host.send(CALLER, params)).resolves.toMatchObject({ ok: true, replayed: false }) + expect( + store.listOperationRows().find((row) => row.operationId === params.envelope.clientOperationId) + ).toEqual(prior) + expect( + store.readCommandReceipt({ kind: 'global' }, params.envelope.clientOperationId) + ).toMatchObject({ + verdict: 'readable', + receipt: { status: 'accepted', fingerprint: params.envelope.payloadFingerprint } + }) + await deliveredOnce() +}) + +it('preserves global conflicts with a non-chat operation already in the ledger', async () => { + await attach() + const params = sendParams('cannot reuse a terminal launch') + await store.admitOperation({ + callerKey: 'terminal-caller', + operationId: params.envelope.clientOperationId, + fingerprint: 'different-terminal-launch', + now: NOW + }) + await expect(host.send(CALLER, params)).resolves.toMatchObject({ + ok: false, + refusal: { code: 'agent_session_operation_conflict', details: { reason: 'operationIdReused' } } + }) + expect(hostJournal().submissions()).toHaveLength(0) + expect(store.readCommandReceipt({ kind: 'global' }, params.envelope.clientOperationId)).toEqual({ + verdict: 'absent' + }) +}) diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-resend-ledger-refusal.test.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-resend-ledger-refusal.test.ts index e286c8a58ae8..0e91f3d1cf89 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-resend-ledger-refusal.test.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-resend-ledger-refusal.test.ts @@ -1,5 +1,4 @@ -// A send id the ledger refuses is answered with that refusal and nothing else: no chat opened, no -// write. A /clear in flight refuses only a send's first run, judged when the send arrived. +// Send identity outlives the ledger policy; /clear still refuses a first run at its arrival. import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from 'vitest' import { AGENT_SESSION_MAX_NEW_OPERATION_AGE_MS } from '../../../shared/agent-session-host-authority' @@ -20,14 +19,6 @@ import { hostTestMessage } from './structured-agent-session-host-test-data' -const EXPIRED = { - ok: false, - refusal: { - code: 'agent_session_operation_expired', - details: { reason: 'operationExpired' } - } -} - let root: string let store: AgentSessionRecordStore let host: StructuredAgentSessionHost @@ -72,44 +63,68 @@ function clear() { }) } -describe('an expired send id', () => { - it('is answered expired while its chat is closed, not as a chat this host lacks', async () => { +describe('send without ledger policy', () => { + it('accepts an id older than 24 hours while its chat is closed', async () => { await attach() await host.close(SESSION, 'evict') - await expect(host.send(CALLER, expiredParams('long gone'))).resolves.toMatchObject(EXPIRED) - expect(host.hasSession(SESSION)).toBe(false) + await expect(host.send(CALLER, expiredParams('long gone'))).resolves.toMatchObject({ + ok: true, + replayed: false + }) + expect(host.hasSession(SESSION)).toBe(true) }) - it('is answered expired by a store a newer Orca wrote', async () => { + it('still refuses a write to a newer database', async () => { await attach() const database = openTestJournalHostDatabase(root) Object.defineProperty(database, 'readOnly', { value: true }) expect(store.readOnly).toBe(true) try { - await expect(host.send(CALLER, expiredParams('long gone'))).resolves.toMatchObject(EXPIRED) + await expect(host.send(CALLER, expiredParams('long gone'))).resolves.toMatchObject({ + ok: false, + refusal: { details: { reason: 'journalWrittenByNewerOrca' } } + }) } finally { // Teardown stops the live child, which writes. Object.defineProperty(database, 'readOnly', { value: false }) } }) - it('is answered expired when its row lapses while the chat opens for the resend', async () => { + it('accepts a six-minute future id without evaluating or writing admission', async () => { + await attach() + const evaluate = vi.spyOn(store, 'evaluateMutationOperation').mockImplementation(() => { + throw new Error('ledger refuses admission') + }) + const admit = vi + .spyOn(store, 'admitMutationOperation') + .mockRejectedValue(new Error('ledger full')) + const params = sendParams('host clock is behind', `${NOW + 6 * 60_000}-${'f'.repeat(32)}`) + await expect(host.send(CALLER, params)).resolves.toMatchObject({ ok: true, replayed: false }) + await expect(host.send(CALLER, params)).resolves.toMatchObject({ ok: true, replayed: true }) + expect(evaluate).not.toHaveBeenCalled() + expect(admit).not.toHaveBeenCalled() + }) + + it('replays accepted ids after the compatibility ledger has expired', async () => { await attach() - const params = sendParams('recorded, then it lapsed') + const params = sendParams('receipt survives the ledger') await host.send(CALLER, params) await vi.waitFor(() => expect(dispatch).toHaveBeenCalledTimes(1)) - const evaluate = store.evaluateMutationOperation - // The second read, after the open, sees the clock past the row's whole retention. - vi.spyOn(store, 'evaluateMutationOperation') - .mockImplementationOnce(evaluate) - .mockImplementationOnce((args) => - evaluate({ ...args, now: args.now + 3 * AGENT_SESSION_MAX_NEW_OPERATION_AGE_MS }) - ) - - await expect(host.send(CALLER, params)).resolves.toMatchObject(EXPIRED) + const db = openTestJournalHostDatabase(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) }) + + it.each(['invalid', '1-a', `${NOW}-${'a'.repeat(33)}`])('refuses malformed id %s', async (id) => { + await attach() + await expect(host.send(CALLER, sendParams('invalid', id))).resolves.toMatchObject({ + ok: false, + refusal: { details: { reason: 'operationIdInvalid' } } + }) + expect(dispatch).not.toHaveBeenCalled() + }) }) describe('a /clear in flight', () => { @@ -146,12 +161,15 @@ describe('a /clear in flight', () => { ).toBeLessThanOrEqual(1) }) - it('answers an expired id expired', async () => { + it('refuses an old id only for the clear in flight', async () => { await attach() const clearing = clear() const expired = host.send(CALLER, expiredParams('typed long ago')) await clearing - await expect(expired).resolves.toMatchObject(EXPIRED) + await expect(expired).resolves.toMatchObject({ + ok: false, + refusal: { details: { reason: 'conversationCommandInFlight' } } + }) }) }) diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-restart-failure-filing.test.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-restart-failure-filing.test.ts index c99cc945d31c..be5b153ecc87 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-restart-failure-filing.test.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-restart-failure-filing.test.ts @@ -11,7 +11,7 @@ import { QUIT_CUT_NOTICE, readerNotes, statusNotes, - throwAfterContinuationAccepted + continuationCannotFold } from './structured-agent-session-restart-interruption-test-harness' import { CALLER, envelope } from './structured-agent-session-host-test-harness' import { @@ -88,13 +88,13 @@ it("keeps the quit's notice through a continuation that carries the chat on", as ]) }) -// A send that throws after Orca may have taken it cannot be proven undelivered: filed unconfirmed, +// A message Orca took but cannot read back cannot be proven undelivered: filed unconfirmed, // and it stays on record while the agent that may be carrying on keeps running. it('keeps an unconfirmed failure while the agent the continuation started keeps running', async () => { const { host } = await interruptedRestart() vi.spyOn(console, 'warn').mockImplementation(() => {}) await host.restartResume.list() - throwAfterContinuationAccepted() + continuationCannotFold() const result = await host.restartResume.continueAfterRestart([SESSION], 'modal') diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-restart-interruption-test-harness.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-restart-interruption-test-harness.ts index e17315b43e83..db9107193361 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-restart-interruption-test-harness.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-restart-interruption-test-harness.ts @@ -18,6 +18,7 @@ import { openTestAgentSessionRecordStore } from '../../runtime/agent-session-rec import { parseAgentSessionResumeMarker } from '../../../shared/agent-session-resume-marker' import type { AgentChildWorkView } from '../../../shared/agent-status-child-work-view' import { AgentSessionJournal } from '../agent-session-journal/journal-store' +import * as reducer from '../agent-session-journal/journal-reducer' import { StructuredAgentSessionHost } from './structured-agent-session-host' import type { StructuredAgentSessionHostDeps } from './structured-agent-session-host-types' import { StructuredAgentSessionResumeAdmission } from './structured-agent-session-restart-resume-runner' @@ -190,7 +191,7 @@ export async function interruptedRestart( return { ...hostTestState(), host, store, log, closeSession, marker, clock } } -/** The continuation's submission commits, then its send throws: a send Orca may have taken. +/** The continuation's submission commits and folds, then its send throws: a resend answers it. * Install it right before the continuation, the only submission written from then on. */ export function throwAfterContinuationAccepted(): void { const append = AgentSessionJournal.prototype.appendSubmission @@ -203,6 +204,22 @@ export function throwAfterContinuationAccepted(): void { }) } +/** The continuation's submission commits but cannot fold: Orca took a message it cannot vouch + * for. Install it right before the continuation, the only submission written from then on. */ +export function continuationCannotFold(): void { + const fold = reducer.applyJournalRow + vi.spyOn(reducer, 'applyJournalRow').mockImplementation((state, row) => { + if (row.kind !== 'submission') { + fold(state, row) + return + } + // As the reducer does before anything can throw, so later appends take later sequences. + state.lastSequence = Math.max(state.lastSequence, row.seq) + state.highestFence = Math.max(state.highestFence, row.fence) + throw new Error('the accepted continuation could not fold') + }) +} + export async function statusNotes(host: StructuredAgentSessionHost) { return notesOf((await host.journalSnapshot(SESSION)).items) } diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-restart-ownership.test.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-restart-ownership.test.ts index 8614f4353200..38391833ff25 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-restart-ownership.test.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-restart-ownership.test.ts @@ -19,6 +19,7 @@ import { QUIT_CUT_NOTICE, startAgent, statusNotes, + continuationCannotFold, supersededRefusal, throwAfterContinuationAccepted } from './structured-agent-session-restart-interruption-test-harness' @@ -146,11 +147,11 @@ it('replays the same logical continuation through the durable send ledger', asyn }) // A send that throws after Orca may have taken it is not proof it was not delivered. -it('keeps a continuation unconfirmed when its send throws after acceptance', async () => { +it('keeps a continuation unconfirmed when its accepted message cannot be read back', async () => { const { host, store } = await interruptedRestart() vi.spyOn(console, 'warn').mockImplementation(() => {}) expect(await host.restartResume.list()).toHaveLength(1) - throwAfterContinuationAccepted() + continuationCannotFold() const result = await host.restartResume .continueAfterRestart([SESSION], 'modal') @@ -171,6 +172,27 @@ it('keeps a continuation unconfirmed when its send throws after acceptance', asy ).toMatchObject({ outcome: { status: 'succeeded' } }) }) +// A throw after the message is in the chat is answered as a resend would be: it was delivered. +it('reports a continuation delivered when its send throws after its message is in the chat', async () => { + const { host, dispatch } = await interruptedRestart() + vi.spyOn(console, 'warn').mockImplementation(() => {}) + expect(await host.restartResume.list()).toHaveLength(1) + throwAfterContinuationAccepted() + + const result = await host.restartResume + .continueAfterRestart([SESSION], 'modal') + .finally(() => vi.restoreAllMocks()) + + expect(result.continued).toMatchObject([{ sessionId: SESSION, outcome: 'continued' }]) + expect(result.failed).toEqual([]) + expect(dispatch).toHaveBeenCalledOnce() + const notes = await statusNotes(host) + expect(notes).toContainEqual({ text: AGENT_SESSION_RESTART_CONTINUATION_NOTE, tone: undefined }) + expect(notes.map((note) => note.text)).not.toContain( + AGENT_SESSION_RESTART_CONTINUATION_UNCONFIRMED_NOTE + ) +}) + // The continuation is accepted, then its start fails: the message is rejected with the cause and // the failure is filed, and nothing is stopped because nothing started. it('rejects the continuation when its start fails, and files a retryable refusal', async () => { diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-restart-resume-progress.test.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-restart-resume-progress.test.ts index cb38b4fdce06..5ff3dc4aa651 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-restart-resume-progress.test.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-restart-resume-progress.test.ts @@ -4,8 +4,8 @@ import type { AgentSessionStatusSummary } from '../../../shared/agent-session-wi import { AgentSessionRecoveryCapsule } from '../../runtime/agent-session-recovery-capsule' import { interruptedRestart, - startAgent, - throwAfterContinuationAccepted + continuationCannotFold, + startAgent } from './structured-agent-session-restart-interruption-test-harness' import { HOST_TEST_SESSION as SESSION } from './structured-agent-session-host-test-data' import { createRestartResumeProgress } from './structured-agent-session-restart-resume-progress' @@ -178,7 +178,7 @@ it.each(['continued', 'refused', 'unconfirmed'] as const)( acquire.mockRejectedValueOnce(new Error('start refused')) } if (phase === 'unconfirmed') { - throwAfterContinuationAccepted() + continuationCannotFold() } const bookkeeping = Promise.withResolvers() const complete = AgentSessionRecoveryCapsule.prototype.completeResume diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-send-preparation.test.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-send-preparation.test.ts index 4ceae6b6c047..c1edc4766772 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-send-preparation.test.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-send-preparation.test.ts @@ -187,7 +187,7 @@ describe('a send with no live owner', () => { await accept(sendParams('accept first')) await eventually(async () => expect(dispatch).toHaveBeenCalledOnce()) - expect(order).toEqual(['admit', 'acquire']) + expect(order).toEqual(['acquire']) }) it('leaves a live owner alone', async () => { @@ -326,16 +326,16 @@ describe('a send with no live owner', () => { expect(acquire).toHaveBeenCalledOnce() }) - it("restarts nothing for an older build's unknown row the journal never saw", async () => { + it('accepts afresh when only an older ledger row exists without a command receipt', async () => { const resent = await admittedThenHostDied(sendParams('marked unknown, then died'), 'unknown') await expect(host.send(CALLER, resent)).resolves.toMatchObject({ ok: true, - replayed: true, - value: { submission: { dispatchState: 'unknown', recovered: true } } + replayed: false, + value: { submission: { dispatchState: 'pending' } } }) - expect(acquire).not.toHaveBeenCalled() - expect(dispatch).not.toHaveBeenCalled() + await eventually(async () => expect(dispatch).toHaveBeenCalledOnce()) + expect(acquire).toHaveBeenCalledOnce() }) it('accepts a send that arrives while a restart holds the queue, and hands both over in order', async () => { diff --git a/src/main/native-chat/agent-session-wire/structured-agent-session-send.test.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-send.test.ts index f382eda54e44..228bf2b66cd6 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-send.test.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-send.test.ts @@ -87,7 +87,7 @@ describe('send', () => { expect(page.providerSession).toEqual({ key: 'session_id', id: THREAD }) }) - it('settles a submission write failure as rejected before provider dispatch', async () => { + it('a submission write failure leaves no accepted operation before provider dispatch', async () => { await attach() const journal = ( host as unknown as { sessions: Map } @@ -103,7 +103,7 @@ describe('send', () => { expect(dispatch).not.toHaveBeenCalled() expect( store.listOperationRows().find((row) => row.operationId === params.envelope.clientOperationId) - ).toMatchObject({ outcome: { status: 'failed' } }) + ).toBeUndefined() }) it('settles a thrown dispatch as unknown, never as a rejection', async () => { @@ -338,10 +338,10 @@ describe('send', () => { 'host fault before the write' ) expect(hostJournal().submissions()).toHaveLength(0) - // Its success would have committed with its write, so a row still pending wrote nothing. + // A throw before acceptance leaves no receipt or compatibility ledger row. expect( store.listOperationRows().find((row) => row.operationId === clientMessageId) - ).toMatchObject({ outcome: { status: 'pending' } }) + ).toBeUndefined() await expect(host.send({ callerKey: 'client-after-recovery' }, params)).resolves.toMatchObject({ ok: true, @@ -351,7 +351,7 @@ describe('send', () => { await delivered(clientMessageId) expect( store.listOperationRows().find((row) => row.operationId === clientMessageId) - ).toMatchObject({ callerKey: CALLER.callerKey, outcome: { status: 'succeeded' } }) + ).toMatchObject({ callerKey: 'client-after-recovery', outcome: { status: 'succeeded' } }) 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/structured-agent-session-turns.ts b/src/main/native-chat/agent-session-wire/structured-agent-session-turns.ts index 17ccba7938ef..b9ca710a1fd2 100644 --- a/src/main/native-chat/agent-session-wire/structured-agent-session-turns.ts +++ b/src/main/native-chat/agent-session-wire/structured-agent-session-turns.ts @@ -43,6 +43,7 @@ import { } from '../agent-session-journal/journal-open-failure' import type { StructuredAgentSessionLogger } from './structured-agent-session-logger' import { isAgentSessionAttachmentExpiredError } from '../agent-session-attachments/agent-session-attachment-claims' +import { CommandReceiptExistsError } from '../agent-session-journal/command-receipt-transaction' import type { StructuredAgentRegistry } from './structured-agent-registry' export { performSetOption } from './structured-agent-session-turns-options' export { performPrompt } from './structured-agent-session-turns-prompt' @@ -67,7 +68,7 @@ export type AgentSessionTurnContext = { publish: () => void /** Who a Stop's refusal row names. */ failureTextContext?: AgentSessionFailureWordsContext - /** The operation's success, committed with the row that accepts it (`MutationPlan.settlesWithWrite`). */ + /** The operation's success, committed with the row that accepts it (`settlesWithWrite` or `acceptsWithCommandReceipt`). */ operationReceipt?: JournalOperationReceipt now: () => number } @@ -161,6 +162,14 @@ export async function performSend( ctx.operationReceipt ) } catch (error) { + if (error instanceof CommandReceiptExistsError) { + throw error + } + ctx.logger.warn('recording a message failed', { + scope: 'send-journal-write', + sessionId: ctx.sessionId, + error + }) if (isAgentSessionAttachmentExpiredError(error)) { return agentSessionAttachmentExpiredRefusal() } diff --git a/src/main/native-chat/agent-session-wire/structured-conversation-compaction.test.ts b/src/main/native-chat/agent-session-wire/structured-conversation-compaction.test.ts index ffaf2104b410..35e5b4fda216 100644 --- a/src/main/native-chat/agent-session-wire/structured-conversation-compaction.test.ts +++ b/src/main/native-chat/agent-session-wire/structured-conversation-compaction.test.ts @@ -739,7 +739,7 @@ it('delivers the next message after a command whose child died and whose settlem expect(readAgentJournalTurn((await commandTurn(cmid))?.body)?.state).not.toBe('running') }) -it("ignores an older build's unconfirmed compaction record, and answers its operation without rerunning it (B15)", async () => { +it('ignores a ledger-only compaction record with no command receipt (B15)', async () => { await attach() const older = compactParams() // An older build admitted this operation and died before recording its outcome. @@ -780,13 +780,10 @@ it("ignores an older build's unconfirmed compaction record, and answers its oper await expect(state.host.conversationCommand(CALLER, older)).resolves.toMatchObject({ ok: true, - value: { - state: 'unknown', - error: 'Compaction completion is unconfirmed.', - failure: { kind: 'compactionUnconfirmed' } - } + replayed: false, + value: { state: 'completed' } }) - expect(compact).toHaveBeenCalledOnce() + expect(compact).toHaveBeenCalledTimes(2) }) it('never lets a provider echo alias the command entry', async () => { diff --git a/src/main/native-chat/agent-session-wire/structured-conversation-compaction.ts b/src/main/native-chat/agent-session-wire/structured-conversation-compaction.ts index cf43da7e5269..519a22bd3b5b 100644 --- a/src/main/native-chat/agent-session-wire/structured-conversation-compaction.ts +++ b/src/main/native-chat/agent-session-wire/structured-conversation-compaction.ts @@ -190,11 +190,7 @@ function acceptStructuredConversationCommand( if (blocked) { return blocked } - const accepted = await plan.run(ctx) - if (accepted.ok) { - context.wakeDelivery(ctx.sessionId) - } - return accepted + return plan.run(ctx) } }, sendPreparation(context, params.envelope, arrival) diff --git a/src/main/runtime/agent-session-operation-admission.test.ts b/src/main/runtime/agent-session-operation-admission.test.ts index 41df221ad122..a95c7a4cfd90 100644 --- a/src/main/runtime/agent-session-operation-admission.test.ts +++ b/src/main/runtime/agent-session-operation-admission.test.ts @@ -1,8 +1,14 @@ import { describe, expect, it } from 'vitest' -import { agentSessionOperationKey } from '../../shared/agent-session-operation-ledger' +import { + agentSessionOperationKey, + agentSessionOperationExpiry, + pruneAgentSessionOperationRows, + type AgentSessionOperationRow +} from '../../shared/agent-session-operation-ledger' import { admitAgentSessionGlobalOperationRow, - admitAgentSessionOperationRow + admitAgentSessionOperationRow, + insertAcceptedAgentSessionOperationInto } from './agent-session-operation-admission' const NOW = 1_900_000_000_000 @@ -55,3 +61,24 @@ describe('global agent-session operation admission', () => { }) }) }) + +it('expires the compatibility success row from host time even for a far-future id', () => { + const operationId = `${9_999_999_999_999}-${'b'.repeat(32)}` + const state = { operations: new Map() } + insertAcceptedAgentSessionOperationInto(state, { + callerKey: 'client', + operationId, + fingerprint: 'accepted', + now: NOW, + operationIdScope: 'global', + outcome: { status: 'succeeded', sessionId: 'chat' } + }) + const expiresAt = agentSessionOperationExpiry(NOW, NOW) + expect(state.operations.get(agentSessionOperationKey('client', operationId))).toMatchObject({ + recordedAt: NOW, + expiresAt, + outcome: { status: 'succeeded' } + }) + expect(pruneAgentSessionOperationRows(state.operations, expiresAt - 1).size).toBe(1) + expect(pruneAgentSessionOperationRows(state.operations, expiresAt).size).toBe(0) +}) diff --git a/src/main/runtime/agent-session-operation-admission.ts b/src/main/runtime/agent-session-operation-admission.ts index d6d478c01900..a37b263d72a4 100644 --- a/src/main/runtime/agent-session-operation-admission.ts +++ b/src/main/runtime/agent-session-operation-admission.ts @@ -3,11 +3,13 @@ import { agentSessionOperationKey, + agentSessionOperationExpiry, claimAgentSessionOperation, evaluateAgentSessionOperation, findAgentSessionGlobalOperationRow, pruneAgentSessionOperationRows, settleAgentSessionOperation, + pendingAgentSessionOperationRow, type AgentSessionOperationClaim, type AgentSessionOperationDecision, type AgentSessionOperationOutcome, @@ -16,11 +18,13 @@ import { } from '../../shared/agent-session-operation-ledger' import { admitAgentSessionMutation, + agentSessionLedgerRefusal, type AgentSessionMutationAdmission } from '../../shared/agent-session-mutation-envelope' import type { AgentSessionMutationEnvelope } from '../../shared/agent-session-wire' import type { AgentSessionRecord } from '../../shared/agent-session-record' import type { AgentSessionStoreState } from './agent-session-store-state' +import { AgentSessionRefusalError } from '../../shared/agent-session-wire-refusals' export type AgentSessionOperationAdmission = { callerKey: string @@ -29,6 +33,46 @@ export type AgentSessionOperationAdmission = { now: number } +export type AgentSessionOperationAcceptance = AgentSessionOperationAdmission & { + operationIdScope?: 'global' +} + +// TEMPORARY: ledger co-write preserves cross-family identity until every mutation uses receipts. +export function insertAcceptedAgentSessionOperationInto( + state: Pick, + args: AgentSessionOperationAcceptance & { outcome: AgentSessionOperationOutcome } +): void { + const candidates = args.operationIdScope + ? state.operations.values() + : [state.operations.get(agentSessionOperationKey(args.callerKey, args.operationId))] + let exists = false + for (const row of candidates) { + if (!row || row.operationId !== args.operationId) { + continue + } + if (row.fingerprint !== args.fingerprint) { + throw new AgentSessionRefusalError( + agentSessionLedgerRefusal( + { clientOperationId: args.operationId }, + { + decision: 'refused', + code: 'agent_session_operation_conflict', + details: { reason: 'operationIdReused' } + } + ) + ) + } + exists = true + } + if (!exists) { + state.operations.set(agentSessionOperationKey(args.callerKey, args.operationId), { + ...pendingAgentSessionOperationRow(args), + expiresAt: agentSessionOperationExpiry(args.now, args.now), + outcome: args.outcome + }) + } +} + type OperationRows = Map export type AgentSessionMutationOperationAdmission = { diff --git a/src/main/runtime/agent-session-operation-receipt.ts b/src/main/runtime/agent-session-operation-receipt.ts new file mode 100644 index 000000000000..dff02a2e0085 --- /dev/null +++ b/src/main/runtime/agent-session-operation-receipt.ts @@ -0,0 +1,25 @@ +import type { JournalOperationReceipt } from '../native-chat/agent-session-journal/journal-row-writer' +import type { AgentSessionOperationOutcome } from '../../shared/agent-session-operation-ledger' +import { + insertAcceptedAgentSessionOperationInto, + settleAgentSessionOperationInto, + type AgentSessionOperationAcceptance +} from './agent-session-operation-admission' +import type { AgentSessionStoreTransactions } from './agent-session-store-transactions' + +export type AgentSessionOperationReceiptInput = + | Parameters[1] + | (AgentSessionOperationAcceptance & { outcome: AgentSessionOperationOutcome }) + +export function agentSessionOperationOutcomeReceipt( + transactions: AgentSessionStoreTransactions, + args: AgentSessionOperationReceiptInput +): JournalOperationReceipt { + if ('fingerprint' in args) { + // TEMPORARY: ledger co-write preserves cross-family identity until every mutation uses receipts. + return transactions.receipt((draft) => insertAcceptedAgentSessionOperationInto(draft, args), { + insertOperationsIfAbsent: true + }) + } + return transactions.receipt((draft) => settleAgentSessionOperationInto(draft, args)) +} diff --git a/src/main/runtime/agent-session-record-rows.ts b/src/main/runtime/agent-session-record-rows.ts index ecc9fd9d72c3..e819a1ebbf6c 100644 --- a/src/main/runtime/agent-session-record-rows.ts +++ b/src/main/runtime/agent-session-record-rows.ts @@ -14,6 +14,7 @@ import type Database from '../sqlite/sync-database' import type { SqliteRow } from '../sqlite/sqlite-statement' import type { AgentSessionStoreState } from './agent-session-store-state' import type { AgentSessionStoreRowWrites } from './agent-session-store-draft' +import { agentSessionRefusalError } from '../../shared/agent-session-wire-refusals' import { isReadableAgentSessionStoreOperation, isReadableAgentSessionStoreRecord, @@ -178,6 +179,23 @@ function writeTabIndex( } } +// TEMPORARY: ledger co-write preserves cross-family identity until every mutation uses receipts. +export function insertAgentSessionOperationRowsIfAbsent( + db: Database.Database, + rows: AgentSessionStoreRowWrites['operations']['upsert'] +): void { + const insert = db.prepare( + 'INSERT INTO agent_session_operations (operation_key, row_json) VALUES (?, ?) ON CONFLICT(operation_key) DO NOTHING' + ) + for (const [key, json] of rows) { + if (insert.run(key, json).changes === 0) { + throw agentSessionRefusalError('agent_session_operation_unknown', { + reason: 'outcomeUnknown' + }) + } + } +} + /** Exactly the rows a transaction changed. The caller holds the journal transaction. */ export function writeAgentSessionStoreRows( db: Database.Database, diff --git a/src/main/runtime/agent-session-record-store.ts b/src/main/runtime/agent-session-record-store.ts index 9cca024aca1b..a5c0cd728c57 100644 --- a/src/main/runtime/agent-session-record-store.ts +++ b/src/main/runtime/agent-session-record-store.ts @@ -7,7 +7,6 @@ import { commitConversationCommandRecord } from './agent-session-conversation-co import { pinAgentSessionRecordLaunchDirectory } from './agent-session-record-launch-directory' import { agentSessionOperationKey, - type AgentSessionOperationClaim, type AgentSessionOperationDecision, type AgentSessionOperationRow } from '../../shared/agent-session-operation-ledger' @@ -70,7 +69,7 @@ import { showAgentSessionTabs } from './agent-session-tab-table' import type { JournalHostDatabase } from '../native-chat/agent-session-journal/journal-host-database' -import type { JournalOperationReceipt } from '../native-chat/agent-session-journal/journal-row-writer' +import { agentSessionOperationOutcomeReceipt } from './agent-session-operation-receipt' import { loadAgentSessionStoreRows } from './agent-session-record-rows' import { AgentSessionStoreTransactions } from './agent-session-store-transactions' import { createAgentSessionFoundingStore } from './agent-session-founding-store' @@ -80,8 +79,6 @@ import { setAgentSessionRecordConversationName } from './agent-session-record-conversation-name' -type AgentSessionOperationSettlement = Parameters[1] - export const AGENT_SESSION_LEASE_TTL_MS = 30_000, AGENT_SESSION_LEASE_RENEW_INTERVAL_MS = 10_000 @@ -295,10 +292,7 @@ export class AgentSessionRecordStore { /** Durable compare-and-swap for the right to run an admitted operation's effect: two replays both * read `pending`, and only a conditional swap tells the one that may run from the one that must * replay. */ - claimOperation = (args: { - callerKey: string - operationId: string - }): Promise => + claimOperation = (args: Parameters[1]) => this.transact((draft) => claimAgentSessionOperationInto(draft, args)) /** Admission and, when `claimAfter` allows, the claim: one durable write before the effect. */ @@ -307,13 +301,17 @@ export class AgentSessionRecordStore { claimAfter: ClaimAfterAdmission ) => this.transact((draft) => admitAndClaimAgentSessionOperationInto(draft, args, claimAfter)) - recordOperationOutcome = (args: AgentSessionOperationSettlement): Promise => - this.transact((draft) => settleAgentSessionOperationInto(draft, args)) + recordOperationOutcome = ( + args: Parameters[1] + ): Promise => this.transact((draft) => settleAgentSessionOperationInto(draft, args)) + + /** That settlement, or an accepted row inserted if absent, committed by the journal write that + * makes it true. It changes only the ledger, so no record listener is owed. */ + operationOutcomeReceipt = (args: Parameters[1]) => + agentSessionOperationOutcomeReceipt(this.transactions, args) - /** The same settlement, committed by the journal write that makes it true. It changes only the - * ledger, so no record listener is owed. */ - operationOutcomeReceipt = (args: AgentSessionOperationSettlement): JournalOperationReceipt => - this.transactions.receipt((draft) => settleAgentSessionOperationInto(draft, args)) + readCommandReceipt = (...args: Parameters) => + this.transactions.readCommandReceipt(...args) replaceSessionOptions = (args: AgentSessionOptionsReplacement): Promise => this.mutate(args.sessionId, (record) => replaceAgentSessionRecordOptions(record, args)) diff --git a/src/main/runtime/agent-session-store-transactions.ts b/src/main/runtime/agent-session-store-transactions.ts index 3877da33af42..5cc0713a36bf 100644 --- a/src/main/runtime/agent-session-store-transactions.ts +++ b/src/main/runtime/agent-session-store-transactions.ts @@ -12,7 +12,12 @@ import type { JournalOperationReceipt } from '../native-chat/agent-session-journ import { journalOpenRefusalError } from '../native-chat/agent-session-journal/journal-open-failure' import { AgentSessionJournalError } from '../native-chat/agent-session-journal/journal-write-guards' import type { AgentSessionStoreState } from './agent-session-store-state' -import { writeAgentSessionStoreRows } from './agent-session-record-rows' +import { + insertAgentSessionOperationRowsIfAbsent, + writeAgentSessionStoreRows +} from './agent-session-record-rows' +import { readCommandReceipt } from '../native-chat/agent-session-journal/command-receipt-table' +import type { CommandReceiptScope } from '../native-chat/agent-session-journal/command-receipt-schema' import { agentSessionRuntimeIncarnation, attributeAgentSessionRuntime @@ -108,6 +113,10 @@ export class AgentSessionStoreTransactions { return this.journalDatabase.readOnly } + readCommandReceipt(scope: CommandReceiptScope, operationId: string) { + return readCommandReceipt(this.journalDatabase.db, scope, operationId) + } + /** * `apply` changes only the draft it is given. On a database a newer Orca wrote every transaction * is refused, except one marked `inMemoryWhenReadOnly`: it is published without a write, for a @@ -127,7 +136,10 @@ export class AgentSessionStoreTransactions { * commits. Exact without the queue: `write` and `committed` run in one synchronous step, so no * store transaction can commit between the draft's staging and its adoption. */ - receipt(apply: (draft: AgentSessionStoreState) => void): JournalOperationReceipt { + receipt( + apply: (draft: AgentSessionStoreState) => void, + options: { insertOperationsIfAbsent?: true } = {} + ): JournalOperationReceipt { let staged: StagedStoreTransaction | null = null return { write: (db) => { @@ -137,7 +149,12 @@ export class AgentSessionStoreTransactions { staged = this.stage(apply) const writes = staged.writes if (writes) { - writeAgentSessionStoreRows(db, writes) + // TEMPORARY: ledger co-write preserves cross-family identity until every mutation uses receipts. + if (options.insertOperationsIfAbsent) { + insertAgentSessionOperationRowsIfAbsent(db, writes.operations.upsert) + } else { + writeAgentSessionStoreRows(db, writes) + } } }, committed: () => { diff --git a/tests/e2e/cross-version-wire/versioned-agent-session-wire.ts b/tests/e2e/cross-version-wire/versioned-agent-session-wire.ts index 2a8f9a2fc852..a0d4c4674c95 100644 --- a/tests/e2e/cross-version-wire/versioned-agent-session-wire.ts +++ b/tests/e2e/cross-version-wire/versioned-agent-session-wire.ts @@ -136,6 +136,7 @@ type SendAdmissionModules = { plan: unknown envelope: SentMessage['envelope'] journal: () => undefined + store: { getRecord: () => null } }) => Promise } @@ -144,12 +145,13 @@ async function admitSend( admission: Record, sent: SentMessage ): Promise { - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the build's own send plan and admission; with no `prepareSession` it reads only plan, envelope and journal. A drifted export or shape fails this check. + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the build's own send plan and admission; the absent session and journal stop both admission routes after fingerprint validation. A drifted export or shape fails this check. const build = { ...plans, ...admission } as unknown as SendAdmissionModules return build.admitAndRunAgentSessionMutation({ plan: build.sendPlan(sent), envelope: sent.envelope, - journal: () => undefined + journal: () => undefined, + store: { getRecord: () => null } }) }