feat(runtime): confirm a structured chat send by clientMessageId past the first journal page (#39667)

* feat(runtime): confirm a structured chat send by clientMessageId past the first journal page

* fix(mobile): ask the send's own chat before handing it back

* fix(mobile): read send-recorded support when the deadline fires and settle rows that land mid-lookup
This commit is contained in:
Kelvin Amoaba
2026-11-08 05:53:23 +00:00
committed by GitHub
parent 1ef68f5062
commit 2a62af0262
11 changed files with 248 additions and 37 deletions
@@ -10,6 +10,7 @@ import {
} from '../../../src/shared/agent-session/lifecycle/agent-session-stop-capabilities'
import {
AGENT_SESSION_ITEM_DETAIL_RUNTIME_CAPABILITY,
AGENT_SESSION_SEND_RECORDED_RUNTIME_CAPABILITY,
AGENT_SESSION_QUESTION_ANSWERS_RUNTIME_CAPABILITY,
AGENT_SESSION_QUEUED_COMMANDS_RUNTIME_CAPABILITY,
AGENT_SESSION_QUEUED_MESSAGES_RUNTIME_CAPABILITY,
@@ -69,6 +70,8 @@ export type StructuredAgentSessionHostSupport = {
permissionModes: boolean
permissionOwn?: boolean
itemDetail: boolean
/** The host says whether it recorded a send; older hosts answer only through loaded pages. */
sendRecorded?: boolean
phoneAttachments: boolean
/** Older hosts lack Continue on an Orca-stopped turn or refuse it from a phone; the phone sends an ordinary message there. */
continueInterrupted?: boolean
@@ -115,6 +118,7 @@ export function structuredAgentSessionHostSupport(
permissionOwn: capabilities.includes(AGENT_SESSION_PERMISSION_OWN_RUNTIME_CAPABILITY),
permissionModes: capabilities.includes(AGENT_SESSION_PERMISSION_MODES_RUNTIME_CAPABILITY),
itemDetail: capabilities.includes(AGENT_SESSION_ITEM_DETAIL_RUNTIME_CAPABILITY),
sendRecorded: capabilities.includes(AGENT_SESSION_SEND_RECORDED_RUNTIME_CAPABILITY),
phoneAttachments: capabilities.includes(AGENT_SESSION_PHONE_ATTACHMENTS_RUNTIME_CAPABILITY),
continueInterrupted: capabilities.includes(
AGENT_SESSION_PHONE_CONTINUE_INTERRUPTED_RUNTIME_CAPABILITY
@@ -0,0 +1,53 @@
import { useLayoutEffect, useMemo, useRef } from 'react'
import * as z from 'zod'
import { bindDeferredRpcOperation, defineRpcOperation } from '../transport/rpc-operation'
import { rpcResultVariant } from '../transport/rpc-operation-result-reader'
import type { RpcClient } from '../transport/rpc-client'
import type { StructuredAgentSessionHostSupport } from './mobile-structured-agent-session-host-support'
const sendRecordedRead = bindDeferredRpcOperation(
defineRpcOperation({
name: 'agentSession.sendRecorded',
method: 'agentSession.sendRecorded',
acceptance: 'object-result-or-null',
barrier: 'after-caller-barrier',
read: rpcResultVariant('structured-send-recorded', z.object({ recorded: z.boolean() }))
})
)
const REQUEST_OPTIONS = { timeoutMs: 10_000, budgetSpansConnect: true }
/** Whether the host recorded a send its loaded pages don't show; any failure is "not proven". */
function readMobileStructuredSendRecorded(
client: RpcClient,
sessionId: string,
clientMessageId: string
): Promise<boolean> {
return sendRecordedRead
.request(client, { sessionId, clientMessageId }, REQUEST_OPTIONS)
.then((reply) => sendRecordedRead.interpret(reply)?.recorded === true)
.catch(() => false)
}
/** Bound to the chat when a send is held; support is read when its deadline fires, since a
* relaunch claims saved sends before the capability probe answers. Null: the host can't be asked. */
export function useMobileStructuredSendRecorded(
client: RpcClient | null,
sessionId: string | null,
hostSupport: StructuredAgentSessionHostSupport | null | undefined
): ((clientMessageId: string) => Promise<boolean> | null) | null {
const supportedRef = useRef(false)
useLayoutEffect(() => {
supportedRef.current = hostSupport?.sendRecorded === true
}, [hostSupport?.sendRecorded])
return useMemo(
() =>
client && sessionId
? (clientMessageId: string) =>
supportedRef.current
? readMobileStructuredSendRecorded(client, sessionId, clientMessageId)
: null
: null,
[client, sessionId]
)
}
@@ -324,13 +324,20 @@ describe('mobile native-chat draft persistence', () => {
return null
}
function StructuredChat({ submissions }: { submissions: AgentJournalSubmission[] }): null {
function StructuredChat({
submissions,
confirmRecorded
}: {
submissions: AgentJournalSubmission[]
confirmRecorded?: (clientMessageId: string) => Promise<boolean>
}): null {
useMobileStructuredSends({
sessionKey: 'chat-a',
sessionId: 'session-a',
submissions,
queuedMessages: null,
onSendError
onSendError,
confirmRecorded
})
return null
}
@@ -432,14 +439,9 @@ describe('mobile native-chat draft persistence', () => {
expect(onSendError).not.toHaveBeenCalled()
})
it('restores a structured send the journal never recorded, and not one it did', async () => {
vi.useFakeTimers()
publishPageDeviceStore(deviceStore)
async function sendStructuredThenKill(sends: [text: string, clientMessageId: string][]) {
await mount(createElement(TerminalChat, { messages: [] }))
for (const [text, clientMessageId] of [
['landed', 'm-landed'],
['lost', 'm-lost']
]) {
for (const [text, clientMessageId] of sends) {
act(() => drafts?.setComposerText(text))
const origin = drafts?.captureSendOrigin(text)
if (!origin) {
@@ -450,6 +452,15 @@ describe('mobile native-chat draft persistence', () => {
await settle()
act(() => renderer?.unmount())
resetMobileNativeChatDraftStoreForTests()
}
it('restores a structured send the journal never recorded, and not one it did', async () => {
vi.useFakeTimers()
publishPageDeviceStore(deviceStore)
await sendStructuredThenKill([
['landed', 'm-landed'],
['lost', 'm-lost']
])
await mount(createElement(StructuredChat, { submissions: [acceptedSubmission('m-landed')] }))
await act(async () => {
@@ -460,6 +471,28 @@ describe('mobile native-chat draft persistence', () => {
expect(onSendError).toHaveBeenCalledTimes(1)
})
it('asks the host before restoring a structured send its loaded page does not show', async () => {
vi.useFakeTimers()
publishPageDeviceStore(deviceStore)
await sendStructuredThenKill([
['pages back', 'm-far'],
['lost', 'm-lost']
])
await mount(
createElement(StructuredChat, {
submissions: [],
confirmRecorded: async (clientMessageId) => clientMessageId === 'm-far'
})
)
await act(async () => {
await vi.advanceTimersByTimeAsync(20_000)
})
expect(draftText(TAB_A)).toBe('lost')
expect(onSendError).toHaveBeenCalledTimes(1)
})
it('does not bring back the chips of a sent message after a kill', async () => {
vi.useFakeTimers()
publishPageDeviceStore(deviceStore)
@@ -39,6 +39,7 @@ import { useMobileStructuredSendWithOutcome } from './use-mobile-structured-send
import { useMobileStructuredQueuedMessageControls } from './use-mobile-structured-queued-message-controls'
import { useMobileStructuredBackgroundTasks } from './use-mobile-structured-background-tasks'
import { useMobileStructuredSends } from './use-mobile-structured-sends'
import { useMobileStructuredSendRecorded } from './mobile-structured-send-recorded-read'
import { useMobileNativeChatQueueFollowUps as useQueueFollowUps } from './use-mobile-native-chat-queue-follow-ups'
import { useMobileStructuredStopOffer } from './use-mobile-structured-stop-offer'
import { useMobileStructuredTurnIndicator } from './use-mobile-structured-turn-indicator'
@@ -124,7 +125,8 @@ export function useMobileStructuredAgentSession(
cursor: state.cursor,
submissions: state.submissions,
queuedMessages,
onSendError
onSendError,
confirmRecorded: useMobileStructuredSendRecorded(client, sessionId, hostSupport)
})
const { transcriptItems, messages, receipts } = useMobileStructuredTranscript(
agent,
@@ -19,6 +19,7 @@ import { returnMobileNativeChatInFlightSend } from './native-chat/composer/mobil
import {
claimMobileNativeChatInFlightSends,
mobileNativeChatInFlightSendIds,
readMobileNativeChatInFlightSend,
releaseMobileNativeChatInFlightSends,
settleMobileNativeChatInFlightSend,
useMobileNativeChatSavedInFlightVersion
@@ -47,8 +48,11 @@ export function useMobileStructuredSends(args: {
submissions: readonly AgentJournalSubmission[]
queuedMessages: readonly AgentSessionQueuedMessage[] | null
onSendError: MobileNativeChatSendErrorReporter
/** Asks the host whether it recorded a send; null where only the loaded pages can say. */
confirmRecorded?: ((clientMessageId: string) => Promise<boolean> | null) | null
}): MobileStructuredSends {
const {
confirmRecorded = null,
connected = true,
cursor,
onSendError,
@@ -70,6 +74,8 @@ export function useMobileStructuredSends(args: {
}, [sendsBySession])
// Held sends by journal id; their deadlines pause while the host is out.
const [holds] = useState(createUnconfirmedSendDeadlines<string>)
// Expired holds whose host lookup is out: a row arriving meanwhile still settles them.
const [asking] = useState(() => new Set<string>())
useLayoutEffect(() => {
holds.setConnected(connected)
}, [connected, holds])
@@ -96,7 +102,11 @@ export function useMobileStructuredSends(args: {
const forget = useCallback(
(ids: ReadonlySet<string>) =>
setSendsBySession((previous) => {
const kept = (previous[sessionKey] ?? []).filter((send) => !ids.has(send.clientMessageId))
const current = previous[sessionKey] ?? []
const kept = current.filter((send) => !ids.has(send.clientMessageId))
if (kept.length === current.length) {
return previous
}
const next = { ...previous, [sessionKey]: kept }
if (kept.length === 0) {
delete next[sessionKey]
@@ -143,6 +153,48 @@ export function useMobileStructuredSends(args: {
(clientMessageId: string) => forget(new Set([clientMessageId])),
[forget]
)
// The journal records a send under its id: once it does, the saved copy goes.
const recorded = useCallback(
(ids: readonly string[]): Set<string> => {
const { cards, recorded } = structuredAgentSessionSettledSends(
submissions,
(queuedMessages ?? []).map((message) => message.messageId)
)
return new Set(ids.filter((id) => cards.has(id) || recorded.has(id)))
},
[queuedMessages, submissions]
)
// Before a send returns to the box, ask its chat's host: the record can sit past the pages loaded here.
const handBackUnlessRecorded = useCallback(
(
confirm: typeof confirmRecorded,
clientMessageId: string,
inFlightIds: () => string[],
handBack: () => void
): void => {
const asked = confirm?.(clientMessageId)
if (!asked) {
handBack()
return
}
asking.add(clientMessageId)
void asked.then((onHost) => {
asking.delete(clientMessageId)
if (onHost) {
for (const id of inFlightIds()) {
settleMobileNativeChatInFlightSend(id)
}
forget(new Set([clientMessageId]))
} else {
// Even after the chat closed: the hand-back it captured keeps a queued send's place.
handBack()
}
})
},
[asking, forget]
)
const isHeld = useCallback(
(clientMessageId: string) =>
(sendsBySessionRef.current[sessionKey] ?? []).some(
@@ -159,27 +211,22 @@ export function useMobileStructuredSends(args: {
)
return
}
holds.hold(clientMessageId, () => {
// The host's record can arrive before the lost reply settles, so read what is still unrecorded now.
if (isHeld(clientMessageId)) {
forget(new Set([clientMessageId]))
onUnconfirmed()
}
})
},
[forget, holds, isHeld]
)
// The journal records a send under its id: once it does, the saved copy goes.
const recorded = useCallback(
(ids: readonly string[]): Set<string> => {
const { cards, recorded } = structuredAgentSessionSettledSends(
submissions,
(queuedMessages ?? []).map((message) => message.messageId)
holds.hold(clientMessageId, () =>
handBackUnlessRecorded(
confirmRecorded,
clientMessageId,
() => mobileNativeChatInFlightSendIds(new Set([clientMessageId])),
() => {
// The host's record can arrive before the lost reply settles, so read what is still unrecorded now.
if (isHeld(clientMessageId)) {
forget(new Set([clientMessageId]))
onUnconfirmed()
}
}
)
)
return new Set(ids.filter((id) => cards.has(id) || recorded.has(id)))
},
[queuedMessages, submissions]
[confirmRecorded, forget, handBackUnlessRecorded, holds, isHeld]
)
// Sends saved before the page last closed wait like live ones once their chat opens.
@@ -195,16 +242,35 @@ export function useMobileStructuredSends(args: {
settleMobileNativeChatInFlightSend(send.id)
continue
}
holds.hold(clientMessageId, () => {
returnMobileNativeChatInFlightSend(send.id)
onSendError(send.command ? UNCONFIRMED_SEND_MESSAGE : UNCONFIRMED_SEND_RESTORED_MESSAGE)
})
holds.hold(clientMessageId, () =>
handBackUnlessRecorded(
confirmRecorded,
clientMessageId,
() => [send.id],
() => {
if (readMobileNativeChatInFlightSend(send.id)) {
returnMobileNativeChatInFlightSend(send.id)
onSendError(
send.command ? UNCONFIRMED_SEND_MESSAGE : UNCONFIRMED_SEND_RESTORED_MESSAGE
)
}
}
)
)
}
}, [holds, onSendError, recorded, savedInFlightVersion, sessionId])
}, [
confirmRecorded,
handBackUnlessRecorded,
holds,
onSendError,
recorded,
savedInFlightVersion,
sessionId
])
const sends = sendsBySession[sessionKey] ?? NO_SENDS
useEffect(() => {
const held = holds.keys()
const held = [...holds.keys(), ...asking]
if (sends.length === 0 && held.length === 0) {
return
}
@@ -226,7 +292,7 @@ export function useMobileStructuredSends(args: {
if (sends.some((send) => settled.has(send.clientMessageId))) {
forget(settled)
}
}, [cursor, forget, holds, recorded, sends])
}, [asking, cursor, forget, holds, recorded, sends])
useEffect(() => {
mountedRef.current = true
@@ -0,0 +1,36 @@
// A client that lost a send's reply asks here instead of searching the history pages it loaded.
import { SendRecordedParams } from '../../../../../../shared/rpc-contract/structured-agent-session-send-recorded-params'
import type { StructuredSendAnswer } from '../../../../../native-chat/agent-session-wire/delivery/structured-agent-session-queued-send-answer'
import { defineMethod } from '../../../core'
import { requireInstalledStructuredHost } from '../creation/structured-agent-session-gate'
/** Recorded in any state, as a loaded row or card settles a send; a rejection stays in history, as a live reply's does. */
function sendRecorded(answer: StructuredSendAnswer): boolean {
switch (answer.kind) {
case 'waiting':
case 'withdrawn':
case 'accepted':
case 'rejected':
case 'unknown':
return true
case 'refused':
case 'absent':
case 'unreadable':
return false
}
}
export const STRUCTURED_AGENT_SESSION_SEND_RECORDED_METHODS = [
defineMethod({
name: 'agentSession.sendRecorded',
permission: 'workspace',
params: SendRecordedParams,
handler: async (params, ctx): Promise<{ recorded: boolean }> => {
const host = await requireInstalledStructuredHost(ctx, params.sessionId)
return {
recorded: sendRecorded(await host.sendAnswer(params.sessionId, params.clientMessageId))
}
}
})
]
@@ -36,6 +36,7 @@ import { STRUCTURED_AGENT_SESSION_TURN_COMPLETION_METHODS } from './subscription
import { STRUCTURED_AGENT_SESSION_THREAD_GOAL_METHODS } from './conversation/structured-agent-session-thread-goal'
import { STRUCTURED_AGENT_SESSION_CONVERSATION_OUTLINE_METHODS } from './history/structured-agent-session-conversation-outline'
import { STRUCTURED_AGENT_SESSION_ITEM_METHODS } from './history/structured-agent-session-item'
import { STRUCTURED_AGENT_SESSION_SEND_RECORDED_METHODS } from './history/structured-agent-session-send-recorded'
import { STRUCTURED_AGENT_SESSION_OPTIONS_READ_METHODS } from './catalog/structured-agent-session-options-read'
import {
AttachParams,
@@ -218,6 +219,7 @@ export const STRUCTURED_AGENT_SESSION_METHODS = [
...STRUCTURED_AGENT_SESSION_THREAD_GOAL_METHODS,
...STRUCTURED_AGENT_SESSION_CONVERSATION_OUTLINE_METHODS,
...STRUCTURED_AGENT_SESSION_ITEM_METHODS,
...STRUCTURED_AGENT_SESSION_SEND_RECORDED_METHODS,
...STRUCTURED_AGENT_SESSION_CURRENT_REQUESTS_METHODS,
...STRUCTURED_AGENT_SESSION_ACCOUNT_USAGE_METHODS,
...STRUCTURED_AGENT_SESSION_OPTIONS_READ_METHODS
@@ -37,6 +37,8 @@ export const MOBILE_AGENT_SESSION_RPC_METHODS = [
'agentSession.readVisual',
// One item whole, for "Show full output" and "Show full message".
'agentSession.item',
// Whether the host recorded a send, for one the phone lost track of; gated on agent-session.send-recorded.v1.
'agentSession.sendRecorded',
// "Rewind to here"; phones offer it only where AGENT_SESSION_PHONE_REWIND_RUNTIME_CAPABILITY says so.
'agentSession.rewind',
// "Don't continue" on a usage-limit row the host will continue at its reset.
@@ -82,6 +82,9 @@ export const AGENT_SESSION_CONTINUATION_CONTEXT_RUNTIME_CAPABILITY =
'agent-session.continuation-context.v1' as const
// Probe additive item reads before offering full output; older hosts retain only heads.
export const AGENT_SESSION_ITEM_DETAIL_RUNTIME_CAPABILITY = 'agent-session.item-detail.v1' as const
// Probe before asking whether a send is recorded; older hosts answer only through the history pages a client loaded.
export const AGENT_SESSION_SEND_RECORDED_RUNTIME_CAPABILITY =
'agent-session.send-recorded.v1' as const
// The RPC is registered unconditionally; per-session rewind support is a separate check.
export const AGENT_SESSION_REWIND_RUNTIME_CAPABILITY = 'agent-session.rewind.v1' as const
// Why: the rewind UI relies on the next send settling an in-doubt rewind, even with the agent up.
@@ -163,6 +166,7 @@ export const AGENT_SESSION_RUNTIME_CAPABILITIES = /* @__PURE__ */ (() =>
AGENT_SESSION_CONVERSATION_OUTLINE_RUNTIME_CAPABILITY,
AGENT_SESSION_CONTINUATION_CONTEXT_RUNTIME_CAPABILITY,
AGENT_SESSION_ITEM_DETAIL_RUNTIME_CAPABILITY,
AGENT_SESSION_SEND_RECORDED_RUNTIME_CAPABILITY,
AGENT_SESSION_BACKGROUND_TASK_STOP_CAPABILITY,
AGENT_SESSION_QUESTION_ANSWERS_RUNTIME_CAPABILITY,
AGENT_SESSION_ATTACHMENTS_RUNTIME_CAPABILITY,
+2
View File
@@ -660,6 +660,7 @@ import {
QueuedMessageUpdateParams
} from './structured-agent-session-queued-edit-params'
import { RenameParams } from './structured-agent-session-rename-params'
import { SendRecordedParams } from './structured-agent-session-send-recorded-params'
import { TerminalDaemonRestartSeveredParams } from './terminal-daemon-params'
import { TerminalLimitUpdateParams } from './terminal-limit-params'
import { TerminalAdoptOrphans } from './terminal-orphan-params'
@@ -845,6 +846,7 @@ export const RPC_PARAMS_BY_METHOD = {
'agentSession.reveal': RevealParams,
'agentSession.rewind': RewindParams,
'agentSession.send': SendParams,
'agentSession.sendRecorded': SendRecordedParams,
'agentSession.setOption': SetOptionParams,
'agentSession.sleepWorkspace': SleepWorkspaceParams,
'agentSession.subscribe': SubscribeParams,
@@ -0,0 +1,7 @@
// Split out of structured-agent-session-params.ts, which is at its line limit.
import { Identifier, OptionsParams } from './structured-agent-session-params'
export const SendRecordedParams = OptionsParams.extend({
clientMessageId: Identifier('Invalid client message id')
}).strict()