From a94819851d5facdcf7e0b08b4015d9e9d24d9894 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 29 Mar 2026 20:03:39 -0700 Subject: [PATCH] Track settled thread sends before clearing them - add `requestSettled` state to pending sends - clear sends only after the request has settled and state has caught up - update ChatView to mark sends settled at the relevant points --- apps/web/src/components/ChatView.tsx | 6 +++ apps/web/src/store.test.ts | 72 +++++++++++++++++++++++++++- apps/web/src/store.ts | 50 +++++++++++++++++-- apps/web/src/types.ts | 1 + 4 files changed, 122 insertions(+), 7 deletions(-) diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 6160c795de..76f0516f10 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -247,6 +247,7 @@ interface PendingPullRequestSetupRequest { export default function ChatView({ threadId }: ChatViewProps) { const beginThreadSend = useStore((store) => store.beginThreadSend); const clearThreadSend = useStore((store) => store.clearThreadSend); + const markThreadSendSettled = useStore((store) => store.markThreadSendSettled); const moveThreadSend = useStore((store) => store.moveThreadSend); const threads = useStore((store) => store.threads); const projects = useStore((store) => store.projects); @@ -2717,6 +2718,7 @@ export default function ChatView({ threadId }: ChatViewProps) { createdAt: messageCreatedAt, }); turnStartSucceeded = true; + markThreadSendSettled(threadIdForSend); } catch (err: unknown) { if (createdServerThreadForLocalDraft && !turnStartSucceeded) { await api.orchestration @@ -2997,6 +2999,7 @@ export default function ChatView({ threadId }: ChatViewProps) { : {}), createdAt: messageCreatedAt, }); + markThreadSendSettled(threadIdForSend); // Optimistically open the plan sidebar when implementing (not refining). // "default" mode here means the agent is executing the plan, which produces // step-tracking activities that the sidebar will display. @@ -3030,6 +3033,7 @@ export default function ChatView({ threadId }: ChatViewProps) { selectedProviderModels, beginThreadSend, clearThreadSend, + markThreadSendSettled, setComposerDraftInteractionMode, setThreadError, selectedModel, @@ -3100,6 +3104,7 @@ export default function ChatView({ threadId }: ChatViewProps) { }); moveThreadSend(activeThread.id, nextThreadId); + markThreadSendSettled(nextThreadId); const snapshot = await api.orchestration.getSnapshot(); syncServerReadModel(snapshot); // Signal that the plan sidebar should open on the new thread. @@ -3146,6 +3151,7 @@ export default function ChatView({ threadId }: ChatViewProps) { selectedProviderModels, beginThreadSend, clearThreadSend, + markThreadSendSettled, moveThreadSend, syncServerReadModel, selectedModel, diff --git a/apps/web/src/store.test.ts b/apps/web/src/store.test.ts index dbd47b8b0d..038b4d7f99 100644 --- a/apps/web/src/store.test.ts +++ b/apps/web/src/store.test.ts @@ -11,6 +11,7 @@ import { beginThreadSend, clearThreadSend, markThreadUnread, + markThreadSendSettled, reorderProjects, setThreadSendPhase, syncServerReadModel, @@ -386,6 +387,39 @@ describe("store read model sync", () => { expect(next.pendingThreadSendById[threadId]).toEqual({ phase: "sending-turn", startedAt: "2026-02-27T00:01:00.000Z", + requestSettled: false, + }); + }); + + it("does not clear pending sends when the turn appears before the request settles", () => { + const threadId = ThreadId.makeUnsafe("thread-1"); + const initialState = beginThreadSend( + makeState(makeThread()), + threadId, + "sending-turn", + "2026-02-27T00:01:00.000Z", + ); + + const next = syncServerReadModel( + initialState, + makeReadModel( + makeReadModelThread({ + latestTurn: { + turnId: TurnId.makeUnsafe("turn-1"), + state: "completed", + requestedAt: "2026-02-27T00:01:01.000Z", + startedAt: "2026-02-27T00:01:02.000Z", + completedAt: "2026-02-27T00:01:03.000Z", + assistantMessageId: null, + }, + }), + ), + ); + + expect(next.pendingThreadSendById[threadId]).toEqual({ + phase: "sending-turn", + startedAt: "2026-02-27T00:01:00.000Z", + requestSettled: false, }); }); @@ -404,7 +438,7 @@ describe("store read model sync", () => { makeReadModelThread({ session: { threadId, - status: "starting", + status: "running", providerName: "codex", runtimeMode: "full-access", activeTurnId: TurnId.makeUnsafe("turn-1"), @@ -417,6 +451,37 @@ describe("store read model sync", () => { expect(next.pendingThreadSendById[threadId]).toBeUndefined(); }); + + it("clears settled pending sends once the turn has caught up", () => { + const threadId = ThreadId.makeUnsafe("thread-1"); + const initialState = markThreadSendSettled( + beginThreadSend( + makeState(makeThread()), + threadId, + "sending-turn", + "2026-02-27T00:01:00.000Z", + ), + threadId, + ); + + const next = syncServerReadModel( + initialState, + makeReadModel( + makeReadModelThread({ + latestTurn: { + turnId: TurnId.makeUnsafe("turn-1"), + state: "completed", + requestedAt: "2026-02-27T00:01:01.000Z", + startedAt: "2026-02-27T00:01:02.000Z", + completedAt: "2026-02-27T00:01:03.000Z", + assistantMessageId: null, + }, + }), + ), + ); + + expect(next.pendingThreadSendById[threadId]).toBeUndefined(); + }); }); describe("store pending thread send helpers", () => { @@ -429,10 +494,13 @@ describe("store pending thread send helpers", () => { "2026-02-27T00:01:00.000Z", ); const sendingState = setThreadSendPhase(startedState, threadId, "sending-turn"); - const clearedState = clearThreadSend(sendingState, threadId); + const settledState = markThreadSendSettled(sendingState, threadId); + const clearedState = clearThreadSend(settledState, threadId); expect(startedState.pendingThreadSendById[threadId]?.phase).toBe("preparing-worktree"); + expect(startedState.pendingThreadSendById[threadId]?.requestSettled).toBe(false); expect(sendingState.pendingThreadSendById[threadId]?.phase).toBe("sending-turn"); + expect(settledState.pendingThreadSendById[threadId]?.requestSettled).toBe(true); expect(clearedState.pendingThreadSendById[threadId]).toBeUndefined(); }); }); diff --git a/apps/web/src/store.ts b/apps/web/src/store.ts index 628a30f897..6919533726 100644 --- a/apps/web/src/store.ts +++ b/apps/web/src/store.ts @@ -156,15 +156,18 @@ function reconcilePendingThreadSends( continue; } - const authoritativeSendStarted = - thread.session?.status === "starting" || thread.session?.status === "running"; + const authoritativeSendStarted = thread.session?.status === "running"; const authoritativeSendFailed = thread.session?.lastError != null; const latestTurnCaughtUp = hasLatestTurnCaughtUpToPendingSend( thread.latestTurn?.requestedAt ?? null, pendingSend.startedAt, ); - if (authoritativeSendStarted || authoritativeSendFailed || latestTurnCaughtUp) { + if ( + authoritativeSendStarted || + authoritativeSendFailed || + (pendingSend.requestSettled && latestTurnCaughtUp) + ) { changed = true; continue; } @@ -403,14 +406,18 @@ export function beginThreadSend( startedAt: string, ): AppState { const existing = state.pendingThreadSendById[threadId]; - if (existing?.phase === phase && existing.startedAt === startedAt) { + if ( + existing?.phase === phase && + existing.startedAt === startedAt && + existing.requestSettled === false + ) { return state; } return { ...state, pendingThreadSendById: { ...state.pendingThreadSendById, - [threadId]: { phase, startedAt }, + [threadId]: { phase, startedAt, requestSettled: false }, }, }; } @@ -436,6 +443,37 @@ export function setThreadSendPhase( }; } +export function markThreadSendSettled(state: AppState, threadId: ThreadId): AppState { + const existing = state.pendingThreadSendById[threadId]; + if (!existing) { + return state; + } + const thread = state.threads.find((candidate) => candidate.id === threadId) ?? null; + const authoritativeSendStarted = thread?.session?.orchestrationStatus === "running"; + const authoritativeSendFailed = thread?.session?.lastError != null; + const latestTurnCaughtUp = hasLatestTurnCaughtUpToPendingSend( + thread?.latestTurn?.requestedAt ?? null, + existing.startedAt, + ); + + if (authoritativeSendStarted || authoritativeSendFailed || latestTurnCaughtUp) { + return clearThreadSend(state, threadId); + } + if (existing.requestSettled) { + return state; + } + return { + ...state, + pendingThreadSendById: { + ...state.pendingThreadSendById, + [threadId]: { + ...existing, + requestSettled: true, + }, + }, + }; +} + export function clearThreadSend(state: AppState, threadId: ThreadId): AppState { if (!(threadId in state.pendingThreadSendById)) { return state; @@ -571,6 +609,7 @@ export function setThreadBranch( interface AppStore extends AppState { beginThreadSend: (threadId: ThreadId, phase: PendingThreadSendPhase, startedAt: string) => void; clearThreadSend: (threadId: ThreadId) => void; + markThreadSendSettled: (threadId: ThreadId) => void; moveThreadSend: (fromThreadId: ThreadId, toThreadId: ThreadId) => void; syncServerReadModel: (readModel: OrchestrationReadModel) => void; markThreadVisited: (threadId: ThreadId, visitedAt?: string) => void; @@ -588,6 +627,7 @@ export const useStore = create((set) => ({ beginThreadSend: (threadId, phase, startedAt) => set((state) => beginThreadSend(state, threadId, phase, startedAt)), clearThreadSend: (threadId) => set((state) => clearThreadSend(state, threadId)), + markThreadSendSettled: (threadId) => set((state) => markThreadSendSettled(state, threadId)), moveThreadSend: (fromThreadId, toThreadId) => set((state) => moveThreadSend(state, fromThreadId, toThreadId)), syncServerReadModel: (readModel) => set((state) => syncServerReadModel(state, readModel)), diff --git a/apps/web/src/types.ts b/apps/web/src/types.ts index de74107f21..e10ec374dc 100644 --- a/apps/web/src/types.ts +++ b/apps/web/src/types.ts @@ -125,4 +125,5 @@ export interface ThreadSession { export interface PendingThreadSend { phase: PendingThreadSendPhase; startedAt: string; + requestSettled: boolean; }