Repository navigation
fix(ui): prevent duplicate assistant messages when session.message races with chat final - #92732
scotthuang wants to merge 2 commits into
Conversation
…ces with chat final
|
Thanks for the context here. I swept through the related work, and this is now duplicate or superseded. Close: the WebChat ordering race is real, but this branch should not remain the landing candidate because it removes current stale-run recovery and the overlapping safer work is already tracked by a proof-positive PR plus the generic duplicate-message issue. Canonical path: Close this branch as superseded, keep the safer deferred-transcript implementation on #91810, and leave broader duplicate-message follow-up on #85771. So I’m closing this here and keeping the remaining discussion on #91810 and #85771. Review detailsBest possible solution: Close this branch as superseded, keep the safer deferred-transcript implementation on #91810, and leave broader duplicate-message follow-up on #85771. Do we have a high-confidence way to reproduce the issue? No high-confidence live reproduction was established in this review. Source and existing tests show the relevant Is this the best way to solve the issue? No. The single-terminal-owner direction is plausible, but this branch removes the existing stale-run recovery path; the safer solution preserves fallback reconciliation while narrowing the duplicate/reload case as in the canonical related PR. Security review: Security review cleared: Cleared: the final PR diff only changes Control UI TypeScript and tests, with no dependency, workflow, permission, credential, package, or code-execution surface change. AGENTS.md: found and applied where relevant. What I checked:
Likely related people:
Codex review notes: model internal, reasoning high; reviewed against 45e36a241a79. |
scotthuang
left a comment
There was a problem hiding this comment.
Review found two blocking issues. The single-terminal-owner direction is correct, but this revision regresses the existing no-reload terminal path and still contains debug instrumentation that breaks required checks. Local proof: the focused changed tests pass, but ui/src/ui/app-gateway.node.test.ts fails 3 existing cases and focused oxlint reports 6 errors.
| return; | ||
| } | ||
| if (shouldReplayDeferredSessionMessageReload && !historyReloaded) { | ||
| if (shouldConsumePendingTranscriptSync && !historyReloaded) { |
There was a problem hiding this comment.
[P1] Consume the pending marker without always reloading history. A normal webchat turn publishes session.message while the run is active, so this branch now calls chat.history even when final already carried the complete assistant message. That restores the optimistic-message replacement/flicker path the surrounding code intentionally avoids. node scripts/run-vitest.mjs ui/src/ui/app-gateway.node.test.ts currently fails three existing cases, including does not reload chat history after final assistant payload reconciles an active run. Keep the pending sync clear, but only reload for aborted/error or a final that actually needs history (shouldReloadHistoryForFinalEvent(payload)).
|
|
||
| export function buildChatItems(props: BuildChatItemsProps): Array<ChatItem | MessageGroup> { | ||
| let items: ChatItem[] = []; | ||
| const debugItems = typeof (globalThis as any).window !== "undefined"; |
There was a problem hiding this comment.
[P1] Remove the leftover [DEDUPE] instrumentation before merge. This hot render path introduces explicit any values and an unused previews computation; related debug warnings also remain in src/gateway/server-chat.ts and src/agents/embedded-agent-subscribe.handlers.lifecycle.ts, contrary to the PR summary. Focused oxlint reports six errors, including no-unused-vars, three no-explicit-any/style errors here, and the now-unused scopedAgentListParamsForSession import.
|
ClawSweeper applied the proposed close for this PR.
|
Summary
When a plugin registers a
before_agent_finalizehook that blocks streaming,session.messageandchat finalcan arrive in an unexpected order, causing both paths to write tochatMessagesconcurrently — producing duplicate assistant messages in webchat.This PR establishes a single-terminal-owner model: only
chat final/error/abortedowns ending the local chat run and reconciling transcript state.session.messageduring an active run only records apendingTranscriptSyncflag; the terminal handler consumes it after the final message is committed, avoiding concurrent writes tochatMessages.chat final/error/abortedas the sole owner of local run termination and transcript reconciliationsession.messageduring an active run recordspendingTranscriptSyncand returns immediately — noloadChatHistory,loadSessions, or run reconciliationchatRunIdguards, polling retry mechanisms, and all[DEDUPE]debug logsLinked context
Closes # (no existing issue)
Related to internal design doc at
docs/duplicate-message-ordering-design.mdReal behavior proof (required for external PRs)
before_agent_finalizehook defers terminal deliverybefore_agent_finalizehook registeredsession.messagesources; transcript injection pathsTests and validation
Which commands did you run?
pnpm test ui/src/ui/app-gateway.sessions.node.test.ts— 35 tests passpnpm test ui/src/ui/controllers/chat.test.ts— 119 tests passpnpm test ui/src/ui/controllers/sessions.test.ts— 77 tests passpnpm test ui/src/ui/chat/build-chat-items.test.ts— 33 tests passpnpm test src/gateway/server-chat.agent-events.test.ts— 170 tests passpnpm test src/gateway/session-message-events.test.ts— 68 tests passpnpm build:strict-smoke— passespnpm ui:build— passesWhat regression coverage was added or updated?
app-gateway.sessions.node.test.tscovering: session.message before/after chat final, multiple session.messages coalescing, old-run terminal not consuming new-run pending sync, session-switch guardchat.test.tsverifying same-text different-turn messages are both preserved, and no text-only dedup occursWhat failed before this fix, if known?
before_agent_finalizehook deferred terminal delivery, causingsession.messageto arrive beforechat finalRisk checklist
Did user-visible behavior change? (
Yes/No)Yes — eliminates duplicate assistant messages
Did config, environment, or migration behavior change? (
Yes/No)No
Did security, auth, secrets, network, or tool execution behavior change? (
Yes/No)No
What is the highest-risk area?
Event ordering invariants. The fix assumes
chat finalalways eventually arrives as the terminal owner. If achat finalis never delivered (e.g., gateway crash mid-run),pendingTranscriptSyncwould never be consumed.How is that risk mitigated?
Gateway lifecycle broadcasts (
chat final → sessions.changed phase:end) are synchronous on the same WebSocket connection. Thesessions.changedpath still performs reconciliation as a safety net for runs without a local owner. This PR only restrictssession.messagefrom reconciling during active runs.Current review state
What is the next action?
Awaiting reviewer feedback
What is still waiting on author, maintainer, CI, or external proof?
All local tests pass; awaiting CI and maintainer review