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
26 changes: 26 additions & 0 deletions src/web-ui/src/flow_chat/services/SessionRollbackService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 };
Expand Down
7 changes: 6 additions & 1 deletion src/web-ui/src/flow_chat/services/SessionRollbackService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
type SessionMutationKind,
} from '../store/sessionMutationStore';
import {
isHostIssuedTurnCatalogRevision,
resolveMaterializedSessionTurnIdentity,
resolveStorageTurnIndex,
} from '../utils/flowChatTurnIdentity';
Expand Down Expand Up @@ -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) => {
Expand Down
19 changes: 19 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 @@ -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';
Expand Down Expand Up @@ -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');
Expand Down
9 changes: 7 additions & 2 deletions src/web-ui/src/flow_chat/store/FlowChatStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ import {
isProjectedSessionEmpty,
canonicalSessionTurns,
lastUserDialogTurn,
localTurnCatalogRevision,
projectedSessionTurnCount,
resolveDialogTurnIdentity,
resolveStorageTurnIndex,
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions src/web-ui/src/flow_chat/utils/flowChatTurnIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading