Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

perf(client): keep latestTurn and checkpoint refs stable while streaming (#227)

Leob9b6d236a62f
diff --git a/packages/client-runtime/src/state/threadReducer.test.ts b/packages/client-runtime/src/state/threadReducer.test.ts
index 50d2f6f91..1860eaff2 100644
--- a/packages/client-runtime/src/state/threadReducer.test.ts
+++ b/packages/client-runtime/src/state/threadReducer.test.ts
@@ -2,6 +2,7 @@ import { assert, describe, expect, it } from "vite-plus/test";
import {
AuthSessionId,
+ BotId,
CheckpointRef,
CommandId,
EventId,
@@ -567,6 +568,143 @@ describe("applyThreadDetailEvent", () => {
}
});
+ it("keeps latestTurn and checkpoints references across a streaming delta", () => {
+ const streamingThread: OrchestrationThread = {
+ ...baseThread,
+ session: {
+ threadId: ThreadId.make("thread-1"),
+ status: "running",
+ providerName: "claude",
+ runtimeMode: "full-access",
+ activeTurnId: TurnId.make("turn-1"),
+ lastError: null,
+ updatedAt: "2026-04-01T06:59:00.000Z",
+ },
+ latestTurn: {
+ turnId: TurnId.make("turn-1"),
+ state: "running",
+ requestedAt: "2026-04-01T06:59:00.000Z",
+ startedAt: "2026-04-01T06:59:00.000Z",
+ completedAt: null,
+ assistantMessageId: MessageId.make("msg-2"),
+ requestMessageId: MessageId.make("msg-1"),
+ respondingBotId: BotId.make("bot-scout"),
+ },
+ messages: [
+ {
+ id: MessageId.make("msg-2"),
+ role: "assistant",
+ text: "Hello",
+ turnId: TurnId.make("turn-1"),
+ streaming: true,
+ createdAt: "2026-04-01T06:00:00.000Z",
+ updatedAt: "2026-04-01T06:00:00.000Z",
+ },
+ ],
+ checkpoints: [
+ {
+ turnId: TurnId.make("turn-1"),
+ checkpointTurnCount: 1,
+ checkpointRef: CheckpointRef.make("ref-1"),
+ status: "ready",
+ files: [],
+ assistantMessageId: MessageId.make("msg-2"),
+ completedAt: "2026-04-01T06:00:30.000Z",
+ },
+ ],
+ };
+
+ const result = applyThreadDetailEvent(streamingThread, {
+ ...baseEventFields,
+ sequence: 9,
+ occurredAt: "2026-04-01T07:00:00.000Z",
+ aggregateKind: "thread",
+ aggregateId: ThreadId.make("thread-1"),
+ type: "thread.message-sent",
+ payload: {
+ threadId: ThreadId.make("thread-1"),
+ messageId: MessageId.make("msg-2"),
+ role: "assistant",
+ text: ", world",
+ turnId: TurnId.make("turn-1"),
+ streaming: true,
+ createdAt: "2026-04-01T06:00:00.000Z",
+ updatedAt: "2026-04-01T07:00:00.000Z",
+ },
+ });
+
+ expect(result.kind).toBe("updated");
+ if (result.kind === "updated") {
+ expect(result.thread.messages).not.toBe(streamingThread.messages);
+ expect(result.thread.messages[0]?.text).toBe("Hello, world");
+ expect(result.thread.latestTurn).toBe(streamingThread.latestTurn);
+ expect(result.thread.latestTurn?.requestMessageId).toBe("msg-1");
+ expect(result.thread.latestTurn?.respondingBotId).toBe("bot-scout");
+ expect(result.thread.checkpoints).toBe(streamingThread.checkpoints);
+ }
+ });
+
+ it("replaces latestTurn and checkpoints when the first assistant message binds the turn", () => {
+ const unboundThread: OrchestrationThread = {
+ ...baseThread,
+ session: {
+ threadId: ThreadId.make("thread-1"),
+ status: "running",
+ providerName: "claude",
+ runtimeMode: "full-access",
+ activeTurnId: TurnId.make("turn-1"),
+ lastError: null,
+ updatedAt: "2026-04-01T06:59:00.000Z",
+ },
+ latestTurn: {
+ turnId: TurnId.make("turn-1"),
+ state: "running",
+ requestedAt: "2026-04-01T06:59:00.000Z",
+ startedAt: "2026-04-01T06:59:00.000Z",
+ completedAt: null,
+ assistantMessageId: null,
+ },
+ checkpoints: [
+ {
+ turnId: TurnId.make("turn-1"),
+ checkpointTurnCount: 1,
+ checkpointRef: CheckpointRef.make("ref-1"),
+ status: "ready",
+ files: [],
+ assistantMessageId: null,
+ completedAt: "2026-04-01T06:59:30.000Z",
+ },
+ ],
+ };
+
+ const result = applyThreadDetailEvent(unboundThread, {
+ ...baseEventFields,
+ sequence: 9,
+ occurredAt: "2026-04-01T07:00:00.000Z",
+ aggregateKind: "thread",
+ aggregateId: ThreadId.make("thread-1"),
+ type: "thread.message-sent",
+ payload: {
+ threadId: ThreadId.make("thread-1"),
+ messageId: MessageId.make("msg-2"),
+ role: "assistant",
+ text: "Hello",
+ turnId: TurnId.make("turn-1"),
+ streaming: true,
+ createdAt: "2026-04-01T07:00:00.000Z",
+ updatedAt: "2026-04-01T07:00:00.000Z",
+ },
+ });
+
+ expect(result.kind).toBe("updated");
+ if (result.kind === "updated") {
+ expect(result.thread.latestTurn).not.toBe(unboundThread.latestTurn);
+ expect(result.thread.latestTurn?.assistantMessageId).toBe("msg-2");
+ expect(result.thread.checkpoints).not.toBe(unboundThread.checkpoints);
+ expect(result.thread.checkpoints[0]?.assistantMessageId).toBe("msg-2");
+ }
+ });
+
it("keeps latestTurn running for interim assistant messages while the session runs the turn", () => {
const threadWithRunningSession: OrchestrationThread = {
...baseThread,
diff --git a/packages/client-runtime/src/state/threadReducer.ts b/packages/client-runtime/src/state/threadReducer.ts
index b6e6b2df9..20d116dad 100644
--- a/packages/client-runtime/src/state/threadReducer.ts
+++ b/packages/client-runtime/src/state/threadReducer.ts
@@ -345,16 +345,18 @@ export function applyThreadDetailEvent(
// assistant message only settles the turn once the session is no longer
// running it — providers may emit several assistant messages per turn
// (commentary between tool calls), and the turn must stay unsettled
- // until the provider reports turn end.
+ // until the provider reports turn end. Streaming deltas recompute the
+ // same record, so the previous reference is kept when nothing changed.
const turnStillRunning =
event.payload.turnId !== null &&
thread.session?.status === "running" &&
thread.session.activeTurnId === event.payload.turnId;
const settlesTurn = !event.payload.streaming && !turnStillRunning;
- const latestTurn: OrchestrationThread["latestTurn"] =
+ const latestTurn = reuseLatestTurn(
+ thread.latestTurn,
event.payload.role === "assistant" &&
- event.payload.turnId !== null &&
- (thread.latestTurn === null || thread.latestTurn.turnId === event.payload.turnId)
+ event.payload.turnId !== null &&
+ (thread.latestTurn === null || thread.latestTurn.turnId === event.payload.turnId)
? {
turnId: event.payload.turnId,
state: settlesTurn
@@ -378,8 +380,10 @@ export function applyThreadDetailEvent(
? (thread.latestTurn.completedAt ?? null)
: null,
assistantMessageId: event.payload.messageId,
+ ...copyLatestTurnIdentities(thread.latestTurn, event.payload.turnId),
}
- : thread.latestTurn;
+ : thread.latestTurn,
+ );
// Rebind checkpoint assistant message IDs for assistant messages.
const checkpoints =
@@ -445,7 +449,8 @@ export function applyThreadDetailEvent(
// Leaving the "running" session status is the turn-end signal: settle a
// still-running latest turn so its duration reflects the whole turn.
const settledTurnState = settledTurnStateForSessionStatus(event.payload.session.status);
- const latestTurn: OrchestrationLatestTurn | null =
+ const latestTurn = reuseLatestTurn(
+ thread.latestTurn,
event.payload.session.status === "running" && event.payload.session.activeTurnId !== null
? {
turnId: event.payload.session.activeTurnId,
@@ -463,6 +468,7 @@ export function applyThreadDetailEvent(
thread.latestTurn?.turnId === event.payload.session.activeTurnId
? thread.latestTurn.assistantMessageId
: null,
+ ...copyLatestTurnIdentities(thread.latestTurn, event.payload.session.activeTurnId),
}
: thread.latestTurn !== null &&
thread.latestTurn.state === "running" &&
@@ -475,7 +481,8 @@ export function applyThreadDetailEvent(
// "running" is the authoritative turn end.
completedAt: event.payload.session.updatedAt,
}
- : thread.latestTurn;
+ : thread.latestTurn,
+ );
return {
kind: "updated",
@@ -729,6 +736,52 @@ function checkpointStatusToTurnState(
}
}
+/**
+ * Returns `previous` when `next` matches it field for field, otherwise `next`.
+ * Streaming cases recompute the latest turn on every delta, and keeping the
+ * old reference lets selectors and memos keyed on `latestTurn` skip work.
+ */
+function copyLatestTurnIdentities(
+ previous: OrchestrationLatestTurn | null,
+ turnId: TurnId,
+): Pick<OrchestrationLatestTurn, "requestMessageId" | "respondingBotId" | "sourceProposedPlan"> {
+ if (previous?.turnId !== turnId) {
+ return {};
+ }
+ return {
+ ...(previous.requestMessageId !== undefined
+ ? { requestMessageId: previous.requestMessageId }
+ : {}),
+ ...(previous.respondingBotId !== undefined
+ ? { respondingBotId: previous.respondingBotId }
+ : {}),
+ ...(previous.sourceProposedPlan !== undefined
+ ? { sourceProposedPlan: previous.sourceProposedPlan }
+ : {}),
+ };
+}
+
+function reuseLatestTurn(
+ previous: OrchestrationLatestTurn | null,
+ next: OrchestrationLatestTurn | null,
+): OrchestrationLatestTurn | null {
+ if (previous === null || next === null) {
+ return next;
+ }
+ return previous.turnId === next.turnId &&
+ previous.state === next.state &&
+ previous.requestedAt === next.requestedAt &&
+ previous.startedAt === next.startedAt &&
+ previous.completedAt === next.completedAt &&
+ previous.assistantMessageId === next.assistantMessageId &&
+ previous.requestMessageId === next.requestMessageId &&
+ previous.respondingBotId === next.respondingBotId &&
+ previous.sourceProposedPlan?.threadId === next.sourceProposedPlan?.threadId &&
+ previous.sourceProposedPlan?.planId === next.sourceProposedPlan?.planId
+ ? previous
+ : next;
+}
+
function rebindCheckpointAssistantMessage(
checkpoints: ReadonlyArray<OrchestrationCheckpointSummary>,
turnId: TurnId,