Skip to content

Self-improving AI Employees for everyone

fix(chat): free the box when a resent message's run already ended

trohitga3278d4cb56d
diff --git a/client/src/features/chat/__tests__/chatPane.test.tsx b/client/src/features/chat/__tests__/chatPane.test.tsx
index 45d135d3..1bf24492 100644
--- a/client/src/features/chat/__tests__/chatPane.test.tsx
+++ b/client/src/features/chat/__tests__/chatPane.test.tsx
@@ -161,6 +161,52 @@ describe('ChatPane', () => {
expect(screen.getAllByText('Any bookings today?')).toHaveLength(1);
});
+ it('frees the box when a resent message names a run that already ended', async () => {
+ // The first send went through but its answer never arrived: the draft
+ // kept its id, and the server had already answered it.
+ const finished = { run_id: 'r1', state: 'finished', outcome: { type: 'success' } };
+ server.messages = [
+ row('m1', 'user', 'Book Saturday', { client_message_id: 'c1', run_id: 'r1', run: finished }),
+ row('a_r1', 'assistant', 'Saturday is booked.', { run_id: 'r1' }),
+ ];
+ useComposerStore.setState({ drafts: { w1: { text: 'Book Saturday', clientMessageId: 'c1' } } });
+ const answer = sendRequest.getMockImplementation()!;
+ sendRequest.mockImplementation(async (kind: string, data: Wire) => {
+ if (kind === 'send_chat_message') return { success: true, message_id: 'm1', run_id: 'r1', delivery: 'now' };
+ if (kind === 'get_chat_run') return { success: true, run: { ...finished, session_id: 'w1', seq: 4, hub_epoch: 'e1' } };
+ return answer(kind, data);
+ });
+ renderPane();
+ await waitFor(() => expect(useChatRunStore.getState().sessions.w1?.subscribed).toBe(true));
+ await waitFor(() => expect(screen.getByRole('button', { name: 'Send' })).toBeEnabled());
+ fireEvent.keyDown(box(), { key: 'Enter' });
+
+ await waitFor(() => expect(sendRequest).toHaveBeenCalledWith('get_chat_run', { run_id: 'r1' }));
+ await waitFor(() => expect(screen.getByRole('button', { name: 'Send' })).toBeInTheDocument());
+ expect(screen.queryByText('Thinking')).not.toBeInTheDocument();
+ expect(screen.getAllByText('Book Saturday')).toHaveLength(1);
+ });
+
+ it('reads no run again for a fresh message the thread still calls live', async () => {
+ renderPane();
+ await waitFor(() => expect(useChatRunStore.getState().sessions.w1?.subscribed).toBe(true));
+ const answer = sendRequest.getMockImplementation()!;
+ sendRequest.mockImplementation(async (kind: string, data: Wire) => {
+ const reply = await answer(kind, data);
+ if (kind === 'send_chat_message') {
+ server.messages = server.messages.map((message) =>
+ message.id === 'm1' ? { ...message, run: { run_id: 'r1', state: 'pending', outcome: null } } : message,
+ );
+ }
+ return reply;
+ });
+ await write('Any bookings today?');
+ fireEvent.keyDown(box(), { key: 'Enter' });
+ expect(await screen.findByText('Thinking')).toBeInTheDocument();
+ await threadUpdated();
+ expect(sendRequest).not.toHaveBeenCalledWith('get_chat_run', expect.anything());
+ });
+
it('streams the answer with a caret, then shows the saved one in its place', async () => {
server.messages = [row('m1', 'user', 'Any bookings today?', { run_id: 'r1' })];
server.activeRuns = [{ run_id: 'r1', session_id: 'w1', state: 'running', seq: 1, hub_epoch: 'e1' }];
diff --git a/client/src/features/chat/data/runs.ts b/client/src/features/chat/data/runs.ts
index e1d3e850..29c31122 100644
--- a/client/src/features/chat/data/runs.ts
+++ b/client/src/features/chat/data/runs.ts
@@ -11,7 +11,9 @@
*
* `useSessionRuns` / `useLaneRun` / `useRunsSubscribed` read the run store
* (stores/chatRunStore.ts). `useThreadRunReconcile` reads the runs a thread
- * still calls live that ended before the subscription answered.
+ * still calls live that ended before the subscription answered, and a run
+ * this tab admitted on its own (a resent message is answered with the run
+ * it started before) once the thread says it has ended.
*/
import { useEffect, useMemo, useRef, useState } from 'react';
@@ -131,7 +133,14 @@ export function useThreadRunReconcile(sessionId: string | null, messages: readon
if (!sessionId || !subscribed || !messages) return;
for (const message of messages) {
const run = message.run;
- if (!run || !isLiveRun(run) || runs[run.runId] || asked.current.has(run.runId)) continue;
+ if (!run || asked.current.has(run.runId)) continue;
+ const held = runs[run.runId];
+ // Live by the thread and never seen here; or held live only because
+ // this tab admitted it (no event or snapshot since) while the thread
+ // says it ended: no event will come for it.
+ const unseen = isLiveRun(run) && !held;
+ const endedUnseen = !isLiveRun(run) && held !== undefined && isLiveRun(held) && held.hubEpoch === null && held.seq === 0;
+ if (!unseen && !endedUnseen) continue;
asked.current.add(run.runId);
sendRequest<Reply>('get_chat_run', { run_id: run.runId })
.then((one) => {
diff --git a/client/src/features/chat/data/send.ts b/client/src/features/chat/data/send.ts
index ada335b1..494b97bb 100644
--- a/client/src/features/chat/data/send.ts
+++ b/client/src/features/chat/data/send.ts
@@ -158,11 +158,15 @@ export function useSendChatMessage(sessionId: string, scope: ThreadScope, onRefu
// The files are on the server now: the box's thumbnails can go.
for (const item of attachments ?? []) if (item.preview) URL.revokeObjectURL(item.preview);
replace((messages) =>
- messages.map((message) =>
- message.id === localId(clientMessageId)
- ? { ...message, id: result.messageId, runId: result.runId, pending: false }
- : message,
- ),
+ // A resent message the server already had is in the thread as that
+ // row: the local copy goes rather than show it twice.
+ messages.some((message) => message.id === result.messageId && !message.pending)
+ ? messages.filter((message) => message.id !== localId(clientMessageId))
+ : messages.map((message) =>
+ message.id === localId(clientMessageId)
+ ? { ...message, id: result.messageId, runId: result.runId, pending: false }
+ : message,
+ ),
);
if (result.runId) {
useChatRunStore.getState().admit(sessionId, {