fix(native-chat): send queued messages after resuming a chat interrupted by a restart

This commit is contained in:
Kelvin Amoaba
2026-11-08 20:51:47 +00:00
parent 2652059825
commit ded2d0fa28
4 changed files with 34 additions and 3 deletions
@@ -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<AgentSessionJournal, 'queuedMessages' | 'appendQueueResume'>,
fence: number
): Promise<void> {
const pauses = structuredQueuePauses(journal)
if (pauses.length > 0 && pauses.every((pause) => pause.reason === 'restarted')) {
await journal.appendQueueResume(fence)
}
}
@@ -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() } })
}
@@ -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<ContinuationSubmission | undefined>
/** Attribute host continuation/failure; older clients render tone as plain text. */
note: (sessionId: string, text: string, tone?: 'error' | 'warning') => Promise<void>
/** Release cards held only by the restart once the person's continuation is accepted. */
liftRestartHold: (sessionId: string) => Promise<void>
/** 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<StructuredAgentSessionContinuationDeps, 'note' | 'logger'>,
deps: Pick<StructuredAgentSessionContinuationDeps, 'note' | 'logger' | 'liftRestartHold'>,
sessionId: string,
result: StructuredAgentSessionContinuationOutcome
): Promise<void> {
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)
@@ -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}`)