From d146def948b2ad8c80de18ad2d8f9e714d16cd18 Mon Sep 17 00:00:00 2001 From: Bob Lee Date: Mon, 2 Nov 2026 19:34:52 +0800 Subject: [PATCH 1/2] fix(web-ui): mark controller-minted Turn catalog revisions as local Relay replicas and optimistic truncation derive a Turn catalog on the controller and mint their own revision strings. The host computes its catalog revision independently, so these values can never match a host-issued token. Prefix them with `local:` and expose `isHostIssuedTurnCatalogRevision` so consumers can tell them apart. Co-Authored-By: Claude --- .../src/flow_chat/store/FlowChatStore.test.ts | 19 +++++++++++++++++++ .../src/flow_chat/store/FlowChatStore.ts | 9 +++++++-- .../flow_chat/utils/flowChatTurnIdentity.ts | 15 +++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) 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 5a13def3bc..46a3ed1d11 100644 --- a/src/web-ui/src/flow_chat/store/FlowChatStore.test.ts +++ b/src/web-ui/src/flow_chat/store/FlowChatStore.test.ts @@ -17,6 +17,7 @@ import { startupTrace } from '@/shared/utils/startupTrace'; import { projectEffectiveToolItem } from '../utils/toolInvocationIdentity'; import { SessionRecordReplica } from '../session-stream/SessionRecordReplica'; import { isSessionBindingLocked } from '../utils/sessionLifecycle'; +import { isHostIssuedTurnCatalogRevision } from '../utils/flowChatTurnIdentity'; import { dispatchJobStore } from '@/features/dispatch/dispatchJobStore'; import { resetLiveSessionInteractionStoreForTest } from '../services/liveSessionInteractionStore'; import { resourceBudget } from '@/shared/utils/resourceBudget'; @@ -8109,6 +8110,24 @@ describe('FlowChatStore device surfaces', () => { .toEqual(['turn-0', 'turn-2']); }); + it('marks the replica-derived Turn catalog revision as controller-local', async () => { + activateSurface(PEER_SURFACE_ID); + seedSession('peer-catalog'); + apiMocks.subscribeRelaySession.mockImplementationOnce(async () => { + apiMocks.relayRecord?.(itemRecord('peer-catalog', 0, 0)); + apiMocks.relayRecord?.(itemRecord('peer-catalog', 1, 0, 2)); + apiMocks.relayReady?.({ sessionId: 'peer-catalog', hasMore: false, oldestSeq: 1, cursor: 2 }); + return 'peer-catalog-subscription'; + }); + + await flowChatStore.loadRelaySessionHistory('peer-catalog'); + + const catalog = flowChatStore.getState().sessions.get('peer-catalog')!.turnCatalog!; + expect(catalog.entries.map(entry => entry.turnId)).toEqual(['turn-0', 'turn-1']); + // Rollback must not echo this as the host's optimistic-concurrency token. + expect(isHostIssuedTurnCatalogRevision(catalog.revision)).toBe(false); + }); + it('applies a tombstone that lands in the same batch as the turn', async () => { activateSurface(PEER_SURFACE_ID); seedSession('peer-tombstone'); diff --git a/src/web-ui/src/flow_chat/store/FlowChatStore.ts b/src/web-ui/src/flow_chat/store/FlowChatStore.ts index 0031057a50..72dbddd664 100644 --- a/src/web-ui/src/flow_chat/store/FlowChatStore.ts +++ b/src/web-ui/src/flow_chat/store/FlowChatStore.ts @@ -128,6 +128,7 @@ import { isProjectedSessionEmpty, canonicalSessionTurns, lastUserDialogTurn, + localTurnCatalogRevision, projectedSessionTurnCount, resolveDialogTurnIdentity, resolveStorageTurnIndex, @@ -5999,7 +6000,9 @@ export class FlowChatStore { ? truncateTurnCatalog( currentCatalog, clampedIndex, - `${currentCatalog.revision}:rollback:${clampedIndex}:${Date.now()}`, + localTurnCatalogRevision( + `${currentCatalog.revision}:rollback:${clampedIndex}:${Date.now()}`, + ), ) : undefined; const updatedSession = { @@ -6164,7 +6167,9 @@ export class FlowChatStore { ordinal, storageTurnIndex: turn.storageTurnIndex!, turnId: turn.id, preview: turn.userMessage.content, previewTruncated: false, })); - const revision = `relay:${entries.map(entry => entry.turnId).join('|')}`; + // The replica cannot reproduce the host's catalog revision, so mark it as + // controller-local rather than letting it pass for a host token. + const revision = localTurnCatalogRevision(`relay:${entries.map(entry => entry.turnId).join('|')}`); this.updateAuthoritativeSessionTurnCatalog(sessionId, { schemaVersion: 1, sessionId, revision, complete: true, totalTurnCount: entries.length, entries, diff --git a/src/web-ui/src/flow_chat/utils/flowChatTurnIdentity.ts b/src/web-ui/src/flow_chat/utils/flowChatTurnIdentity.ts index 47e7a1aa3f..a8c69d5c73 100644 --- a/src/web-ui/src/flow_chat/utils/flowChatTurnIdentity.ts +++ b/src/web-ui/src/flow_chat/utils/flowChatTurnIdentity.ts @@ -17,6 +17,21 @@ type TurnIdentitySession = Pick< 'sessionId' | 'dialogTurns' | 'isPartial' | 'totalTurnCount' | 'turnCatalog' >; +/** + * Catalog revisions minted by this client for catalogs it derived itself + * (relay replicas, optimistic truncation). The host never issues them, so they + * must not be sent back as optimistic-concurrency tokens. + */ +const LOCAL_TURN_CATALOG_REVISION_PREFIX = 'local:'; + +export function localTurnCatalogRevision(source: string): string { + return `${LOCAL_TURN_CATALOG_REVISION_PREFIX}${source}`; +} + +export function isHostIssuedTurnCatalogRevision(revision: string): boolean { + return !revision.startsWith(LOCAL_TURN_CATALOG_REVISION_PREFIX); +} + export function asLocalTurnIndex(value: number): LocalTurnIndex { return value as LocalTurnIndex; } From 80fe21335cc998150840bbadd1aa7e39071efcda Mon Sep 17 00:00:00 2001 From: Bob Lee Date: Mon, 2 Nov 2026 19:34:52 +0800 Subject: [PATCH 2/2] fix(web-ui): stop sending local catalog revisions on Session rollback In Peer Device Mode the controller's Turn catalog comes from relay records, and its revision was sent to the CLI host as `expectedCatalogRevision`. The host always rejected it with "Session rollback target is stale", so rollback never worked while remote-controlling a CLI host. Only send host-issued revisions. The host still validates the target Turn id and `expectedStorageTurnIndex`, the same fallback already used when the catalog does not cover the projected Turns. Co-Authored-By: Claude --- .../services/SessionRollbackService.test.ts | 26 +++++++++++++++++++ .../services/SessionRollbackService.ts | 7 ++++- 2 files changed, 32 insertions(+), 1 deletion(-) 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 6c4a752c48..023026f27f 100644 --- a/src/web-ui/src/flow_chat/services/SessionRollbackService.test.ts +++ b/src/web-ui/src/flow_chat/services/SessionRollbackService.test.ts @@ -32,6 +32,7 @@ import { stateMachineManager, } from '../state-machine'; import { pendingQueueManager } from './flow-chat-manager/PendingQueueModule'; +import { localTurnCatalogRevision } from '../utils/flowChatTurnIdentity'; describe('SessionRollbackService', () => { it('rejects detached history mutation before any local backend call', async () => { @@ -133,6 +134,31 @@ describe('SessionRollbackService', () => { })); }); + it('omits a controller-minted catalog revision the host never issued', async () => { + // Peer Device sessions build their catalog from replicated records; the + // host computes its own revision, so a local one would always look stale. + const session = sessions.get('session-1'); + session.turnCatalog.revision = localTurnCatalogRevision('relay:turn-7'); + agentApiMock.rollbackSessionToTurn.mockResolvedValue({ + status: 'completed', + restoredFiles: [], + retiredTurnIds: ['turn-7'], + composer: { kind: 'preserve' }, + }); + + await rollbackSessionToTurn({ + sessionId: 'session-1', + targetTurnId: 'turn-7', + kind: 'rollback', + }); + + expect(agentApiMock.rollbackSessionToTurn).toHaveBeenCalledWith(expect.objectContaining({ + targetTurnId: 'turn-7', + expectedStorageTurnIndex: 7, + expectedCatalogRevision: undefined, + })); + }); + it('rolls back an older Turn materialized only in a history window', async () => { const session = sessions.get('session-1'); const olderTurn = { id: 'turn-2', storageTurnIndex: 2 }; diff --git a/src/web-ui/src/flow_chat/services/SessionRollbackService.ts b/src/web-ui/src/flow_chat/services/SessionRollbackService.ts index 8e061b9c56..2f59c5f366 100644 --- a/src/web-ui/src/flow_chat/services/SessionRollbackService.ts +++ b/src/web-ui/src/flow_chat/services/SessionRollbackService.ts @@ -14,6 +14,7 @@ import { type SessionMutationKind, } from '../store/sessionMutationStore'; import { + isHostIssuedTurnCatalogRevision, resolveMaterializedSessionTurnIdentity, resolveStorageTurnIndex, } from '../utils/flowChatTurnIdentity'; @@ -63,7 +64,11 @@ function validProjectedCatalogRevision( session: Session, catalog: SessionTurnCatalog | undefined, ): string | undefined { - if (!catalog || catalog.sessionId !== session.sessionId) { + if ( + !catalog + || catalog.sessionId !== session.sessionId + || !isHostIssuedTurnCatalogRevision(catalog.revision) + ) { return undefined; } const coversProjectedTurns = session.dialogTurns.every((turn) => {