Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(chat): recover a saved reply after missed chat completion (#10999)

Danila Katalshov967fc4e17d5b
diff --git a/packages/core/realtime/use-realtime-sync-ws-instance.test.tsx b/packages/core/realtime/use-realtime-sync-ws-instance.test.tsx
index d491b5825f..d60ddc8b9d 100644
--- a/packages/core/realtime/use-realtime-sync-ws-instance.test.tsx
+++ b/packages/core/realtime/use-realtime-sync-ws-instance.test.tsx
@@ -328,6 +328,46 @@ describe("useRealtimeSync — ws instance change", () => {
expect(calls).toContainEqual(["issues", "tasks"]);
});
+ it.each([false, true])("refreshes chat history once on task completion (chat:done delivered: %s)", async (doneDelivered) => {
+ const ws = createMockWs();
+ const sessionId = "chat-1";
+ const taskId = "task-1";
+ const pendingKey = chatKeys.pendingTask(sessionId);
+ const messagesKey = chatKeys.messages(sessionId);
+ const reply = { id: "reply-1", task_id: taskId, role: "assistant", content: "Done" };
+ qc.setQueryData(pendingKey, { task_id: taskId, status: "running" });
+ qc.setQueryData(messagesKey, []);
+ const loadMessages = vi.fn(async () => [reply]);
+ const observer = new QueryObserver(qc, {
+ queryKey: messagesKey,
+ queryFn: loadMessages,
+ staleTime: Infinity,
+ });
+ const unsubscribe = observer.subscribe(() => {});
+ try {
+ renderHook(() => useRealtimeSync(ws, stores), { wrapper: createWrapper(qc) });
+ if (doneDelivered) {
+ const done = vi.mocked(ws.on).mock.calls.find(([event]) => event === "chat:done")?.[1];
+ expect(done).toBeDefined();
+ await act(async () => {
+ done!({
+ task_id: taskId, chat_session_id: sessionId, message_id: reply.id,
+ content: reply.content, created_at: "2026-11-19T12:00:00Z",
+ });
+ });
+ await waitFor(() => expect(observer.getCurrentResult().fetchStatus).toBe("idle"));
+ }
+ const handler = vi.mocked(ws.on).mock.calls.find(([event]) => event === "task:completed")?.[1];
+ expect(handler).toBeDefined();
+ await act(async () => { handler!({ task_id: taskId, chat_session_id: sessionId }); });
+ expect(qc.getQueryData(pendingKey)).toEqual({});
+ await waitFor(() => expect(observer.getCurrentResult().data).toEqual([reply]));
+ expect(loadMessages).toHaveBeenCalledTimes(1);
+ } finally {
+ unsubscribe();
+ }
+ });
+
it("invalidates per-chat-session caches (no wsId in key) on ws instance change", () => {
// These keys are not under the ["chat", wsId] prefix, so they need their
// own recovery invalidation when reconnecting after missed chat/task events.
diff --git a/packages/core/realtime/use-realtime-sync.ts b/packages/core/realtime/use-realtime-sync.ts
index 17db4fc4cd..e656bac7f1 100644
--- a/packages/core/realtime/use-realtime-sync.ts
+++ b/packages/core/realtime/use-realtime-sync.ts
@@ -2020,11 +2020,17 @@ export function useRealtimeSync(
task_id: payload.task_id,
chat_session_id: payload.chat_session_id,
});
- qc.setQueryData<ChatPendingTask>(
- chatKeys.pendingTask(payload.chat_session_id),
- (old) => removePendingChatTask(old, payload.task_id),
+ const pendingKey = chatKeys.pendingTask(payload.chat_session_id);
+ // chat:done normally clears this turn and refreshes its reply. If the
+ // turn is still pending locally, recover the persisted outcome here.
+ if (qc.getQueryData<ChatPendingTask>(pendingKey)?.task_id === payload.task_id) {
+ invalidateChatMessageQueries(qc, payload.chat_session_id);
+ invalidateSessionLists();
+ }
+ qc.setQueryData<ChatPendingTask>(pendingKey, (old) =>
+ removePendingChatTask(old, payload.task_id),
);
- qc.invalidateQueries({ queryKey: chatKeys.pendingTask(payload.chat_session_id) });
+ qc.invalidateQueries({ queryKey: pendingKey });
refreshWakeupTurns(payload.chat_session_id);
invalidatePendingAggregate();
});