Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(store): correct live parity test to match updated slice logic

Steven Enamakel242e08cc2a33
diff --git a/app/src/store/__tests__/chatRuntimeSlice.liveParity.test.ts b/app/src/store/__tests__/chatRuntimeSlice.liveParity.test.ts
index 7c64de9e36..8ebc41e5ca 100644
--- a/app/src/store/__tests__/chatRuntimeSlice.liveParity.test.ts
+++ b/app/src/store/__tests__/chatRuntimeSlice.liveParity.test.ts
@@ -188,6 +188,39 @@ describe('turnSettled', () => {
});
});
+describe('turn boundaries', () => {
+ it('a newer live turn is not torn down by the previous turn settling late', () => {
+ const state = run([
+ beginInferenceTurn({ threadId: T }),
+ markInferenceTurnStreaming({ threadId: T }),
+ liveTurnStarted({ threadId: T, requestId: 'req-2' }),
+ toolCallReceived({ threadId: T, round: 1, toolName: 'shell', toolCallId: 'call-2' }),
+ turnSettled({ threadId: T, requestId: 'req-1' }),
+ ]);
+ expect(state.inferenceTurnLifecycleByThread[T]).toBe('streaming');
+ expect(state.liveRequestIdByThread[T]).toBe('req-2');
+ expect(state.toolTimelineByThread[T]?.[0]?.status).toBe('running');
+ expect(state.settledTurnsByThread[T]?.['req-1']).toBeUndefined();
+ });
+
+ it('a new send starts from an empty transcript, not the last turn’s', () => {
+ const state = run([
+ streamDeltaReceived({
+ threadId: T,
+ requestId: 'req-1',
+ round: 1,
+ delta: 'Last turn said this.',
+ channel: 'content',
+ }),
+ turnSettled({ threadId: T, requestId: 'req-1' }),
+ beginInferenceTurn({ threadId: T }),
+ ]);
+ expect(state.processingByThread[T]).toBeUndefined();
+ // …while the settled turn keeps the trail it rendered with.
+ expect(state.settledTurnsByThread[T]?.['req-1']?.transcript).toHaveLength(1);
+ });
+});
+
describe('liveTurnStarted', () => {
it('ignores a parallel (forked) request, which never owns the tail', () => {
const state = run([
diff --git a/app/src/store/chatRuntimeSlice.ts b/app/src/store/chatRuntimeSlice.ts
index a1f737317c..234b9dd875 100644
--- a/app/src/store/chatRuntimeSlice.ts
+++ b/app/src/store/chatRuntimeSlice.ts
@@ -2161,6 +2161,13 @@ const chatRuntimeSlice = createSlice({
},
beginInferenceTurn: (state, action: PayloadAction<{ threadId: string }>) => {
state.inferenceTurnLifecycleByThread[action.payload.threadId] = 'started';
+ // The live transcript belongs to the previous turn until this one's
+ // `inference_start` resets it — and the tail is minted from the moment
+ // the lifecycle is `started`, so it would draw the last turn's narration
+ // and reasoning again under the new question. That turn is settled (its
+ // trail frozen, or served by the core projection); start this one clean.
+ delete state.processingByThread[action.payload.threadId];
+ delete state.liveRequestIdByThread[action.payload.threadId];
},
markInferenceTurnStreaming: (state, action: PayloadAction<{ threadId: string }>) => {
if (state.inferenceTurnLifecycleByThread[action.payload.threadId]) {
@@ -2200,7 +2207,22 @@ const chatRuntimeSlice = createSlice({
*/
turnSettled: (state, action: PayloadAction<{ threadId: string; requestId?: string }>) => {
const { threadId } = action.payload;
- const requestId = action.payload.requestId ?? state.liveRequestIdByThread[threadId];
+ const live = state.liveRequestIdByThread[threadId];
+ // A newer turn is already live on the thread (a queued follow-up the
+ // core started while this turn's reply was still being persisted). The
+ // live state is that turn's now: freezing or clearing it would erase the
+ // turn that is streaming. This turn's reply is already in the cache and
+ // its trail comes from the core projection.
+ if (action.payload.requestId && live && live !== action.payload.requestId) {
+ turnStateLog(
+ 'turn settle superseded thread=%s request=%s live=%s',
+ threadId,
+ action.payload.requestId,
+ live
+ );
+ return;
+ }
+ const requestId = action.payload.requestId ?? live;
if (requestId) {
const timeline = (state.toolTimelineByThread[threadId] ?? []).map(entry =>
entry.status === 'running' ? { ...entry, status: 'success' as const } : entry
@@ -2251,7 +2273,9 @@ const chatRuntimeSlice = createSlice({
delete state.toolTimelineByThread[action.payload.threadId];
delete state.toolTimelineSeqByThread[action.payload.threadId];
delete state.processingByThread[action.payload.threadId];
- delete state.settledTurnsByThread[action.payload.threadId];
+ // `settledTurnsByThread` is kept: those turns are finished, and their
+ // frozen trails are what their messages render — dropping them here (a
+ // failed send, the silence timeout) would remount every settled turn.
delete state.liveRequestIdByThread[action.payload.threadId];
delete state.inferenceTurnLifecycleByThread[action.payload.threadId];
delete state.pendingApprovalByThread[action.payload.threadId];