diff --git a/src/web-ui/src/flow_chat/services/SessionRollbackService.test.ts b/src/web-ui/src/flow_chat/services/SessionRollbackService.test.ts index d315d8f5da..99f25f4cd7 100644 --- a/src/web-ui/src/flow_chat/services/SessionRollbackService.test.ts +++ b/src/web-ui/src/flow_chat/services/SessionRollbackService.test.ts @@ -34,6 +34,11 @@ import { stateMachineManager, } from '../state-machine'; import { pendingQueueManager } from './flow-chat-manager/PendingQueueModule'; +import { getActiveSurfaceScope } from '@/infrastructure/peer-device/deviceSurface'; +import { + beginSubmittedMessagePreview, + getSubmittedMessagePreviews, +} from './submittedMessagePresentation'; import { localTurnCatalogRevision } from '../utils/flowChatTurnIdentity'; describe('SessionRollbackService', () => { @@ -87,6 +92,8 @@ describe('SessionRollbackService', () => { }); it('submits stable persisted identity and reloads only after completion', async () => { + const scope = getActiveSurfaceScope(); + beginSubmittedMessagePreview(scope, 'session-1', 'turn-8', { id: 'user_local', content: 'later', timestamp: 1 }); agentApiMock.rollbackSessionToTurn.mockResolvedValue({ status: 'completed', restoredFiles: ['src/lib.rs'], @@ -112,6 +119,8 @@ describe('SessionRollbackService', () => { expect(isSessionTurnRetired('session-1', 'turn-7')).toBe(true); expect(isSessionTurnRetired('session-1', 'turn-8')).toBe(true); expect(retireRelaySessionTurns).toHaveBeenCalledWith('session-1', ['turn-7', 'turn-8']); + // A preview renders whenever its Turn is absent; a retired Turn's must go. + expect(getSubmittedMessagePreviews(scope, 'session-1')).toEqual([]); }); it('omits a stale catalog revision when the current Turn is not represented', async () => { diff --git a/src/web-ui/src/flow_chat/services/SessionRollbackService.ts b/src/web-ui/src/flow_chat/services/SessionRollbackService.ts index cb2b948ff3..3d9f865e1c 100644 --- a/src/web-ui/src/flow_chat/services/SessionRollbackService.ts +++ b/src/web-ui/src/flow_chat/services/SessionRollbackService.ts @@ -3,6 +3,8 @@ import { agentAPI } from '@/infrastructure/api'; import { globalEventBus } from '@/infrastructure/event-bus'; import { createLogger } from '@/shared/utils/logger'; import { flowChatStore } from '../store/FlowChatStore'; +import { getActiveSurfaceScope } from '@/infrastructure/peer-device/deviceSurface'; +import { finishSubmittedMessagePreview } from './submittedMessagePresentation'; import { assertSessionMutationLease, completeSessionMutationReconciliation, @@ -145,6 +147,12 @@ export async function rollbackSessionToTurn( markSessionTurnsRetired(request.sessionId, outcome.retiredTurnIds); // A device surface renders a host replica that reloading does not rewrite. flowChatStore.retireRelaySessionTurns(request.sessionId, outcome.retiredTurnIds); + // A send preview renders whenever its Turn is absent; never let a retired + // Turn's preview stand in for it. + const scope = getActiveSurfaceScope(); + outcome.retiredTurnIds.forEach(turnId => ( + finishSubmittedMessagePreview(scope, request.sessionId, turnId) + )); refreshFiles(outcome.restoredFiles); try { await reloadAuthoritativeSession(request.sessionId); diff --git a/src/web-ui/src/flow_chat/store/FlowChatStore.test.ts b/src/web-ui/src/flow_chat/store/FlowChatStore.test.ts index 5dbc877000..fef2dc7941 100644 --- a/src/web-ui/src/flow_chat/store/FlowChatStore.test.ts +++ b/src/web-ui/src/flow_chat/store/FlowChatStore.test.ts @@ -19,6 +19,11 @@ import { SessionRecordReplica } from '../session-stream/SessionRecordReplica'; import { resetSessionStreamsForTest, sessionStream } from '../session-stream/SessionStream'; import { isSessionBindingLocked } from '../utils/sessionLifecycle'; import { isHostIssuedTurnCatalogRevision } from '../utils/flowChatTurnIdentity'; +import { getActiveSurfaceScope } from '@/infrastructure/peer-device/deviceSurface'; +import { + beginSubmittedMessagePreview, + getSubmittedMessagePreviews, +} from '../services/submittedMessagePresentation'; import { dispatchJobStore } from '@/features/dispatch/dispatchJobStore'; import { resetLiveSessionInteractionStoreForTest } from '../services/liveSessionInteractionStore'; import { resourceBudget } from '@/shared/utils/resourceBudget'; @@ -8415,6 +8420,23 @@ describe('FlowChatStore device surfaces', () => { expect(apiMocks.subscribeRelaySession).toHaveBeenCalledTimes(subscriptions); }); + it('lets the host record of a send adopt its preview, so a rollback cannot expose it', async () => { + await openPeerSession('peer-preview', 1); + const scope = getActiveSurfaceScope(); + // The controller reserves the turn ID; the host assigns its own message ID. + beginSubmittedMessagePreview(scope, 'peer-preview', 'turn-1', { + id: 'user_local', content: 'prompt 1', timestamp: 2, + }); + + apiMocks.relayRecord?.(itemRecord('peer-preview', 1, 0, 20)); + await vi.waitFor(() => expect(turnIds('peer-preview')).toEqual(['turn-0', 'turn-1'])); + + expect(getSubmittedMessagePreviews(scope, 'peer-preview')).toEqual([]); + flowChatStore.retireRelaySessionTurns('peer-preview', ['turn-1']); + expect(turnIds('peer-preview')).toEqual(['turn-0']); + expect(getSubmittedMessagePreviews(scope, 'peer-preview')).toEqual([]); + }); + it('keeps rendered turns for a host-appended gap without a replay', async () => { await openPeerSession('peer-source-gap', 2); diff --git a/src/web-ui/src/flow_chat/store/FlowChatStore.ts b/src/web-ui/src/flow_chat/store/FlowChatStore.ts index b848c541a3..c970de5a7f 100644 --- a/src/web-ui/src/flow_chat/store/FlowChatStore.ts +++ b/src/web-ui/src/flow_chat/store/FlowChatStore.ts @@ -35,6 +35,7 @@ import { import { createLogger } from '@/shared/utils/logger'; import { canonicalAgentId } from '@/shared/agents/identity'; import { optimisticTurnAdoptionKey } from '../utils/optimisticTurnAdoption'; +import { finishSubmittedMessagePreview } from '../services/submittedMessagePresentation'; import { isRemoteTraceContext, markPhaseAfterAnimationFrames, @@ -6503,6 +6504,13 @@ export class FlowChatStore { }); if (failed.size > 0) surface.relayPendingRecordTurns.set(sessionId, new Set(failed.keys())); else surface.relayPendingRecordTurns.delete(sessionId); + // The host's record is the real Turn for a send from this window. Its user + // message carries the host's ID, so DialogTurnStarted cannot match the + // preview by message; the reserved turn ID is the join. A preview left + // behind stays hidden only while the Turn exists, and would reappear as a + // bare user message once a rollback retires it. + const scope = getActiveSurfaceScope(); + incoming.forEach(turn => finishSubmittedMessagePreview(scope, sessionId, turn.id)); const mailbox = surface.relayQuestionMailboxes.get(sessionId); if (mailbox) { const turns = this.state.sessions.get(sessionId)?.dialogTurns ?? [];