fix: deduplicate re-delivered session sends on the client message id

Typed-3-shown-5 forensics (project 000): the WS client queues session_send
payloads while disconnected and flushes the queue after a reconnect, and the
server minted a fresh row id per delivery — so one typed message could land
as several user turns, each dispatched to the engine.

Every send now carries a client-generated ui_message_id (dispatchSessionSend
injects one when the caller didn't). The handler persists the user row under
that id and answers any later delivery in the same channel with an idempotent
ack instead of inserting and dispatching again. Because the row id now equals
the optimistic bubble's ui_message_id, the echo also merges with the local
message even after the transcript sync rewrites row metadata.

Same text intentionally sent again gets a fresh id and still starts a new turn.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
LZH-YS1998
2026-07-07 22:47:00 +08:00
parent a24cbea4d8
commit 3a2c935025
7 changed files with 166 additions and 53 deletions
@@ -10,5 +10,10 @@ assert.match(src, /makeOptimisticUserMessageId/, 'ordinary composer sends must c
assert.match(src, /chatStore\.sendMessage/, 'ordinary composer sends must echo the user message locally before backend response')
assert.match(src, /ui_message_id: uiMessageId/, 'optimistic local message and websocket metadata must share ui_message_id')
assert.match(src, /checkpointReplyId/, 'checkpoint replies must be excluded from ordinary optimistic composer echo')
assert.match(
src,
/const outgoing = metadata\?\.ui_message_id\s*\?\s*metadata\s*:\s*\{ \.\.\.\(metadata \?\? \{\}\), ui_message_id: makeOptimisticUserMessageId\(\) \}/,
'every session send must carry a client-generated ui_message_id so the backend can deduplicate re-deliveries',
)
console.log('WorkspacePage.test.ts: OK (optimistic composer echo wiring)')
@@ -943,7 +943,12 @@ export function WorkspacePage({
attachments?: OutgoingAttachmentPayload[],
metadata?: CheckpointReplyMetadata,
) => {
onSessionSend(taskId, content, attachments, metadata)
// Every send carries a client-generated ui_message_id so the backend can
// deduplicate re-deliveries (WS pending-queue flush after a reconnect).
const outgoing = metadata?.ui_message_id
? metadata
: { ...(metadata ?? {}), ui_message_id: makeOptimisticUserMessageId() }
onSessionSend(taskId, content, attachments, outgoing)
}, [onSessionSend])
// ── Composer send ──