Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(thread): upsert cached messages by id\n\nDeterministic reply ids (#5933) mean a `loadThreadMessages` fetch can land\nbetween the core's write and the frontend's same-id append; appending\nblindly left two same-id entries, which assistant-ui rejects as a\nduplicate key.\n

Shanu706c18c50b49
diff --git a/app/src/store/__tests__/threadSlice.test.ts b/app/src/store/__tests__/threadSlice.test.ts
index 6762ab74e1..8f537ad6bd 100644
--- a/app/src/store/__tests__/threadSlice.test.ts
+++ b/app/src/store/__tests__/threadSlice.test.ts
@@ -444,6 +444,26 @@ describe('threadSlice addInferenceResponse thunk', () => {
expect(state.activeThreadIds).toEqual({});
});
+ it('replaces a cached entry that already carries the persisted id instead of duplicating it (#5933)', async () => {
+ // A core-initiated turn's reply is persisted by the core under
+ // `agent:<run_id>` and announced afterwards; a thread reload can fetch that
+ // row before our own same-id append resolves.
+ const store = createStore();
+ store.dispatch(setSelectedThread('t-1'));
+ const coreRow = makeMessage({ id: 'agent:run-1', sender: 'agent', content: 'from core' });
+ mockedThreadApi.getThreadMessages.mockResolvedValueOnce({ messages: [coreRow], count: 1 });
+ await store.dispatch(loadThreadMessages('t-1'));
+
+ mockedThreadApi.appendMessage.mockResolvedValueOnce(coreRow);
+ await store.dispatch(
+ addInferenceResponse({ content: 'from core', threadId: 't-1', messageId: 'agent:run-1' })
+ );
+
+ const state = store.getState().thread;
+ expect(state.messagesByThreadId['t-1']).toEqual([coreRow]);
+ expect(state.messages).toEqual([coreRow]);
+ });
+
it('falls back to the selected thread when no threadId is supplied', async () => {
// Under parallel inference there is no single "active" thread to fall back
// to, so the legacy fallback target is now the selected thread.
diff --git a/app/src/store/threadSlice.ts b/app/src/store/threadSlice.ts
index 62f963f05e..e8387988a5 100644
--- a/app/src/store/threadSlice.ts
+++ b/app/src/store/threadSlice.ts
@@ -71,21 +71,35 @@ const initialState: ThreadState = {
createThreadRequestId: null,
};
+/**
+ * Append a persisted message to the thread cache, or replace the entry that
+ * already carries its id.
+ *
+ * Ids are no longer guaranteed fresh on append: a core-initiated turn's reply
+ * is persisted by the core under `agent:<run_id>` and then persisted again by
+ * our `chat_done` handler under the same id (the store collapses the two —
+ * #5933). If a `loadThreadMessages` fetch lands between those two writes, the
+ * cache already holds that id when the append resolves, and a second entry
+ * would hand React and assistant-ui a duplicate key (assistant-ui throws on
+ * one). `replaceExisting` keeps its narrower contract for callers that only
+ * ever update a message already in the cache (reactions).
+ */
function appendMessageToCache(
state: ThreadState,
threadId: string,
message: ThreadMessage,
replaceExisting = false
) {
- const existing = state.messagesByThreadId[threadId] ?? [];
- const next = replaceExisting
- ? existing.map(e => (e.id === message.id ? message : e))
- : [...existing, message];
- state.messagesByThreadId[threadId] = next;
+ const upsert = (list: ThreadMessage[]) => {
+ const present = list.some(e => e.id === message.id);
+ if (replaceExisting || present) {
+ return list.map(e => (e.id === message.id ? message : e));
+ }
+ return [...list, message];
+ };
+ state.messagesByThreadId[threadId] = upsert(state.messagesByThreadId[threadId] ?? []);
if (threadId === state.selectedThreadId) {
- state.messages = replaceExisting
- ? state.messages.map(e => (e.id === message.id ? message : e))
- : [...state.messages, message];
+ state.messages = upsert(state.messages);
}
}