Skip to content

A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.

TypeScript

fix(ui): keep the streaming turn when a mid-turn history snapshot arrives

danielgap0bebb061b938
diff --git a/packages/ui/src/store.ts b/packages/ui/src/store.ts
index 06780d4..0f8bf20 100644
--- a/packages/ui/src/store.ts
+++ b/packages/ui/src/store.ts
@@ -282,14 +282,24 @@ function reduceServer(state: DotState, message: ServerMessage): DotState {
return { ...state, asks: [...state.asks, message.ask] };
case "ask_resolved":
return { ...state, asks: state.asks.filter((a) => a.requestId !== message.requestId) };
- case "history":
+ case "history": {
+ // A snapshot taken mid-turn cannot cover the live streaming turn: the
+ // session file only gains the assistant message once it ends, and history
+ // ids (h*) never match the live bridge ids (m*). Keep this conversation's
+ // streaming messages so the remaining deltas and message_done still land
+ // on them instead of freezing a partial bubble and spawning a duplicate.
+ const streaming =
+ message.conversationId === undefined || message.conversationId === state.conversationId
+ ? state.messages.filter((m) => m.streaming)
+ : [];
return {
...state,
interrupted: false,
conversationId: message.conversationId ?? state.conversationId,
- messages: message.messages.map((m) => ({ ...m, streaming: false })),
+ messages: [...message.messages.map((m) => ({ ...m, streaming: false })), ...streaming],
hasEarlier: message.hasEarlier === true,
};
+ }
case "earlier": {
// A page for messages this window no longer starts with (a newer history replaced them) is dropped.
if (state.messages[0]?.id !== message.before) return state;
diff --git a/packages/ui/test/store.test.ts b/packages/ui/test/store.test.ts
index a64a53e..0a6770a 100644
--- a/packages/ui/test/store.test.ts
+++ b/packages/ui/test/store.test.ts
@@ -71,6 +71,51 @@ describe("reduce", () => {
expect(s).toMatchObject({ conversationId: "b.jsonl", interrupted: false });
});
+ it("keeps a streaming message when a mid-turn history snapshot arrives", () => {
+ // A reconnect during a run refetches history, but the session file only
+ // gains the assistant message when it ends, and history ids (h*) never
+ // match the live bridge ids (m*), so the snapshot cannot cover the
+ // streaming turn. The turn must survive and finish in place.
+ let s = apply(
+ initialState,
+ { type: "ready", agentState: "working", conversationId: "a.jsonl", model: "Fake" },
+ { type: "user_message", messageId: "u1", text: "hi" },
+ { type: "message_delta", messageId: "m2", delta: "Hel" },
+ );
+ s = apply(s, {
+ type: "history",
+ conversationId: "a.jsonl",
+ messages: [{ id: "h1", role: "user", text: "hi", activities: [] }],
+ });
+ expect(s.messages.map((m) => [m.id, m.role, m.text, m.streaming])).toEqual([
+ ["h1", "user", "hi", false],
+ ["m2", "assistant", "Hel", true],
+ ]);
+ s = apply(
+ s,
+ { type: "message_delta", messageId: "m2", delta: "lo!" },
+ { type: "message_done", messageId: "m2", text: "Hello!" },
+ );
+ expect(s.messages.map((m) => [m.id, m.text, m.streaming])).toEqual([
+ ["h1", "hi", false],
+ ["m2", "Hello!", false],
+ ]);
+ });
+
+ it("replaces streaming messages when the history is for another conversation", () => {
+ const s = apply(
+ initialState,
+ { type: "ready", agentState: "working", conversationId: "a.jsonl", model: "Fake" },
+ { type: "message_delta", messageId: "m2", delta: "Hel" },
+ );
+ const switched = apply(s, {
+ type: "history",
+ conversationId: "b.jsonl",
+ messages: [{ id: "h1", role: "user", text: "other", activities: [] }],
+ });
+ expect(switched.messages.map((m) => m.id)).toEqual(["h1"]);
+ });
+
it("clears the interruption when a new user message arrives", () => {
const s = apply(
initialState,