Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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'],
Expand All @@ -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 () => {
Expand Down
8 changes: 8 additions & 0 deletions src/web-ui/src/flow_chat/services/SessionRollbackService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions src/web-ui/src/flow_chat/store/FlowChatStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);

Expand Down
8 changes: 8 additions & 0 deletions src/web-ui/src/flow_chat/store/FlowChatStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 ?? [];
Expand Down
Loading