diff --git a/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-queued-pause.ts b/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-queued-pause.ts index af255f5b5f1..b3f25e8c522 100644 --- a/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-queued-pause.ts +++ b/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-queued-pause.ts @@ -40,3 +40,14 @@ export async function resumeStructuredQueue( await journal.appendQueueResume(fence, receipt) return true } + +/** A person's chosen continuation is their recovery from a restart; other pauses keep their own lift. */ +export async function liftStructuredQueueRestartHold( + journal: Pick, + fence: number +): Promise { + const pauses = structuredQueuePauses(journal) + if (pauses.length > 0 && pauses.every((pause) => pause.reason === 'restarted')) { + await journal.appendQueueResume(fence) + } +} diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-interruption-test-harness.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-interruption-test-harness.ts index fbcbaffaf00..8e40358d691 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-interruption-test-harness.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-restart-interruption-test-harness.ts @@ -134,7 +134,8 @@ export async function interruptedRestart( const fields = { body, delivery: 'queue-if-active' as const } const queued = await previous.host.send(CALLER, { envelope: envelope('agentSession.send', fields), - ...fields + ...fields, + userSend: true }) expect(queued).toMatchObject({ ok: true, value: { queued: expect.anything() } }) } diff --git a/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-restart-continuation.ts b/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-restart-continuation.ts index 9075f8218cd..a4ff1c78c62 100644 --- a/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-restart-continuation.ts +++ b/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-restart-continuation.ts @@ -32,6 +32,7 @@ import { AgentSessionPreDispatchError } from '../operations/structured-agent-ses import { restartContinuationEnvelope } from './structured-agent-session-restart-continuation-envelope' import type { AgentSessionResumeMarker } from '../../../../shared/agent-session/lifecycle/agent-session-resume-marker' import type { StructuredAgentSessionLogger } from '../host/structured-agent-session-logger' +import { liftStructuredQueueRestartHold } from '../delivery/structured-agent-session-queued-pause' /** Envelope acceptance proves only host ownership; preserve provider pending/unknown/rejected outcomes. */ export type StructuredAgentSessionContinuationOutcome = { @@ -90,7 +91,14 @@ export function continuationDeps( (await host.awaitSendHandedOver(sessionId, clientMessageId))?.value ), logger: host.logger, - note: restartNoteWriter(host) + note: restartNoteWriter(host), + liftRestartHold: async (sessionId) => { + const session = host.sessions.get(sessionId) + const fence = host.conversationFence(sessionId) + if (session && fence !== null) { + await liftStructuredQueueRestartHold(session.journal, fence) + } + } } } @@ -154,6 +162,8 @@ export type StructuredAgentSessionContinuationDeps = { ) => Promise /** Attribute host continuation/failure; older clients render tone as plain text. */ note: (sessionId: string, text: string, tone?: 'error' | 'warning') => Promise + /** Release cards held only by the restart once the person's continuation is accepted. */ + liftRestartHold: (sessionId: string) => Promise /** Required to report best-effort note failures. */ logger: StructuredAgentSessionLogger /** Answer at acceptance; caller reports pre-acceptance refusal without duplicate retry notes. */ @@ -198,11 +208,19 @@ export async function startStructuredAgentSessionContinuation( } async function noteOutcome( - deps: Pick, + deps: Pick, sessionId: string, result: StructuredAgentSessionContinuationOutcome ): Promise { if (result.outcome === 'continued') { + try { + await deps.liftRestartHold(sessionId) + } catch { + deps.logger.warn('releasing queued messages after a restart continuation failed', { + scope: 'restart-continuation-queue', + sessionId + }) + } // Attribute only accepted dispatch; note failure must not turn delivered work into failure. try { await deps.note(sessionId, AGENT_SESSION_RESTART_CONTINUATION_NOTE) diff --git a/src/main/native-chat/agent-session-wire/recovery/testing/structured-agent-session-restart-cap.test.ts b/src/main/native-chat/agent-session-wire/recovery/testing/structured-agent-session-restart-cap.test.ts index 1e722273747..0863ed3c9af 100644 --- a/src/main/native-chat/agent-session-wire/recovery/testing/structured-agent-session-restart-cap.test.ts +++ b/src/main/native-chat/agent-session-wire/recovery/testing/structured-agent-session-restart-cap.test.ts @@ -34,6 +34,7 @@ it('holds each slot from accept until handover or rejection, and frees it when t }, awaitSettlement: async () => ({ dispatchState: 'accepted' }), note: async () => undefined, + liftRestartHold: async () => undefined, logger: createStructuredAgentSessionLogger() }) const sessions = Array.from({ length: 8 }, (_, index) => `session-${index}`)