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
This commit is contained in:
Julius Marminge committed 2026-03-29 20:03:39 -07:00
1 parent a2c1172551
commit a94819851d
4 files changed
+122 -7

No files matched your search

+6
View File
@@ -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,
+70 -2
View File
@@ -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();
});
});
+45 -5
View File
@@ -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<AppStore>((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)),
+1
View File
@@ -125,4 +125,5 @@ export interface ThreadSession {
export interface PendingThreadSend {
phase: PendingThreadSendPhase;
startedAt: string;
requestSettled: boolean;
}