Skip to content

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

fix(chat): a late row keeps its settled turn's claim on the timeline

M3gA-Mind7963a419618a
diff --git a/app/src/providers/ChatRuntimeProvider.tsx b/app/src/providers/ChatRuntimeProvider.tsx
index a3d12409d6..a8067c359b 100644
--- a/app/src/providers/ChatRuntimeProvider.tsx
+++ b/app/src/providers/ChatRuntimeProvider.tsx
@@ -819,6 +819,7 @@ const ChatRuntimeProvider = ({ children }: { children: React.ReactNode }) => {
dispatch(
toolCallReceived({
threadId: event.thread_id,
+ requestId: event.request_id,
round: event.round,
toolName: event.tool_name,
toolCallId: event.tool_call_id,
@@ -919,6 +920,7 @@ const ChatRuntimeProvider = ({ children }: { children: React.ReactNode }) => {
dispatch(
subagentSpawned({
threadId: event.thread_id,
+ requestId: event.request_id,
round: event.round,
rowId: `${event.thread_id}:subagent:${event.skill_id}:${event.tool_name}`,
taskId: event.skill_id,
@@ -1276,6 +1278,7 @@ const ChatRuntimeProvider = ({ children }: { children: React.ReactNode }) => {
dispatch(
toolArgsDeltaReceived({
threadId: event.thread_id,
+ requestId: event.request_id,
round: event.round,
delta: event.delta,
toolName: event.tool_name,
diff --git a/app/src/providers/__tests__/liveHistoryParity.test.tsx b/app/src/providers/__tests__/liveHistoryParity.test.tsx
index 3f814a09a7..7a797cc775 100644
--- a/app/src/providers/__tests__/liveHistoryParity.test.tsx
+++ b/app/src/providers/__tests__/liveHistoryParity.test.tsx
@@ -643,6 +643,47 @@ describe("a bare chat_done does not borrow the previous turn's trail", () => {
);
});
+ it("a nested spawn reported after its turn settled does not hand that turn's rows to the delivery", async () => {
+ // A detached child that delegates again reports the nested spawn on its
+ // parent turn's channel, minutes after that turn settled
+ // (`attach_parent` hands it the turn's progress sender).
+ vi.mocked(threadApi.getTurnState).mockResolvedValue(snapshot(TURN_REQUEST, 'running') as never);
+ const listeners = renderProvider();
+ await settleAsyncTurn(listeners);
+ fire(listeners, {
+ listener: 'onSubagentSpawned',
+ event: {
+ thread_id: TURN_THREAD,
+ request_id: TURN_REQUEST,
+ round: 2,
+ tool_name: 'researcher',
+ skill_id: 'sub-nested',
+ message: '',
+ seq: 99,
+ subagent: { mode: 'async' },
+ },
+ } as SocketStep);
+ act(() =>
+ listeners.onDone?.({
+ ...DONE_EVENT,
+ request_id: DELIVERY_REQUEST,
+ full_response: 'The research finished.',
+ })
+ );
+ await waitFor(() =>
+ expect(
+ store
+ .getState()
+ .thread.messagesByThreadId[
+ TURN_THREAD
+ ]?.some(message => message.id === `agent:${DELIVERY_REQUEST}`)
+ ).toBe(true)
+ );
+ expect(toolCards().filter(([messageId]) => messageId === `agent:${DELIVERY_REQUEST}`)).toEqual(
+ []
+ );
+ });
+
it("an older snapshot arriving after chat_done does not hide the settled turn's own trail", async () => {
// Snapshot lag: the completed-snapshot fetch returns the PREVIOUS turn's
// snapshot, so the live rows (and their owner) become that turn's.
diff --git a/app/src/store/__tests__/chatRuntimeSlice.liveParity.test.ts b/app/src/store/__tests__/chatRuntimeSlice.liveParity.test.ts
index cd7be9b963..8dc1763128 100644
--- a/app/src/store/__tests__/chatRuntimeSlice.liveParity.test.ts
+++ b/app/src/store/__tests__/chatRuntimeSlice.liveParity.test.ts
@@ -226,6 +226,80 @@ describe('turnSettled', () => {
]);
});
+ it("a late row of the settled turn keeps that turn's claim", () => {
+ // A bridge that had not drained when `chat_done` was delivered, or a
+ // detached child spawning a nested one, reports on the settled turn's
+ // request after it settled. That row names req-1, so the claim stands and
+ // the bare delivery that follows still does not adopt req-1's rows.
+ const late = [
+ toolCallReceived({
+ threadId: T,
+ requestId: 'req-1',
+ round: 2,
+ toolName: 'shell',
+ toolCallId: 'call-late',
+ }),
+ subagentSpawned({
+ threadId: T,
+ requestId: 'req-1',
+ round: 2,
+ rowId: `${T}:subagent:sub-9:researcher`,
+ taskId: 'sub-9',
+ agentId: 'researcher',
+ }),
+ ];
+ for (const action of late) {
+ const settled = run(
+ [
+ turnSettled({ threadId: T, requestId: 'req-1' }),
+ action,
+ turnSettled({ threadId: T, requestId: 'bgdeliver-1' }),
+ ],
+ live()
+ );
+ expect(settled.toolTimelineRequestByThread[T]).toBe('req-1');
+ expect(settled.settledTurnsByThread[T]?.['bgdeliver-1']).toBeUndefined();
+ }
+ });
+
+ it("a missed turn's row names its own request, which then freezes", () => {
+ const settled = run(
+ [
+ turnSettled({ threadId: T, requestId: 'req-1' }),
+ toolCallReceived({
+ threadId: T,
+ requestId: 'req-2',
+ round: 1,
+ toolName: 'shell',
+ toolCallId: 'call-b',
+ }),
+ turnSettled({ threadId: T, requestId: 'req-2' }),
+ ],
+ live()
+ );
+ expect(settled.settledTurnsByThread[T]?.['req-2']?.timeline.map(row => row.id)).toEqual([
+ 'call-a',
+ 'call-b',
+ ]);
+ });
+
+ it('a parallel request never claims the primary timeline', () => {
+ const settled = run(
+ [
+ registerParallelRequest({ threadId: T, requestId: 'fork-1' }),
+ toolCallReceived({
+ threadId: T,
+ requestId: 'fork-1',
+ round: 1,
+ toolName: 'shell',
+ toolCallId: 'call-fork',
+ }),
+ ],
+ live()
+ );
+ expect(settled.toolTimelineRequestByThread[T]).toBe('req-1');
+ });
+
it('still freezes rows whose owner is unknown, as before', () => {
// No `inference_start` was seen (e.g. a reconnect mid-turn): no claim
// either way, so the reply keeps the rows it is settling with.
diff --git a/app/src/store/chatRuntimeSlice.ts b/app/src/store/chatRuntimeSlice.ts
index b1e14c2150..60b1c40314 100644
--- a/app/src/store/chatRuntimeSlice.ts
+++ b/app/src/store/chatRuntimeSlice.ts
@@ -999,15 +999,22 @@ const initialState: ChatRuntimeState = {
* current without changing its row identity.
*/
/**
- * A row just minted by the live stream belongs to the live turn, when one is
- * known. With none (its `inference_start` was missed, e.g. a reconnect
- * mid-turn), the timeline's owner becomes unknown rather than staying the
- * turn that settled before: a stale claim would make `turnSettled` refuse to
- * freeze this turn's own rows.
+ * A row just minted by the stream belongs to the request its event names
+ * (`request_id`), else to the live turn. With neither known the owner becomes
+ * unknown rather than staying the turn that settled before: a turn whose
+ * `inference_start` was missed (a reconnect mid-turn) must still freeze its
+ * own rows. A late row of an already-settled turn names that turn, so it
+ * keeps the claim instead of erasing it. A parallel (forked) request never
+ * owns the primary timeline, as in `liveTurnStarted`.
*/
-function claimTimelineForLiveTurn(state: ChatRuntimeState, threadId: string): void {
- const live = state.liveRequestIdByThread[threadId];
- if (live) state.toolTimelineRequestByThread[threadId] = live;
+function claimTimelineForLiveTurn(
+ state: ChatRuntimeState,
+ threadId: string,
+ requestId: string | undefined
+): void {
+ if (requestId && state.parallelRequestThreads[requestId] !== undefined) return;
+ const claim = requestId ?? state.liveRequestIdByThread[threadId];
+ if (claim) state.toolTimelineRequestByThread[threadId] = claim;
else delete state.toolTimelineRequestByThread[threadId];
}
@@ -1475,6 +1482,8 @@ const chatRuntimeSlice = createSlice({
toolCallReceived: (
state,
action: PayloadAction<{
+ /** The emitting event's `request_id`; see `claimTimelineForLiveTurn`. */
+ requestId?: string;
threadId: string;
round: number;
toolName: string;
@@ -1547,7 +1556,7 @@ const chatRuntimeSlice = createSlice({
} else {
const seq = state.toolTimelineSeqByThread[threadId] ?? 0;
state.toolTimelineSeqByThread[threadId] = seq + 1;
- claimTimelineForLiveTurn(state, threadId);
+ claimTimelineForLiveTurn(state, threadId, action.payload.requestId);
entries.push(
decorateEntry({
id: rowId,
@@ -1714,6 +1723,8 @@ const chatRuntimeSlice = createSlice({
toolArgsDeltaReceived: (
state,
action: PayloadAction<{
+ /** The emitting event's `request_id`; see `claimTimelineForLiveTurn`. */
+ requestId?: string;
threadId: string;
round: number;
delta: string;
@@ -1743,7 +1754,7 @@ const chatRuntimeSlice = createSlice({
} else {
const seq = state.toolTimelineSeqByThread[threadId] ?? 0;
state.toolTimelineSeqByThread[threadId] = seq + 1;
- claimTimelineForLiveTurn(state, threadId);
+ claimTimelineForLiveTurn(state, threadId, action.payload.requestId);
entries.push(
decorateEntry({
// Same stable fallback `toolCallReceived` generates. This branch
@@ -1769,6 +1780,8 @@ const chatRuntimeSlice = createSlice({
subagentSpawned: (
state,
action: PayloadAction<{
+ /** The emitting event's `request_id`; see `claimTimelineForLiveTurn`. */
+ requestId?: string;
threadId: string;
round: number;
rowId: string;
@@ -1863,7 +1876,7 @@ const chatRuntimeSlice = createSlice({
sourceToolName = spawnEntry?.name;
seq = state.toolTimelineSeqByThread[threadId] ?? 0;
state.toolTimelineSeqByThread[threadId] = seq + 1;
- claimTimelineForLiveTurn(state, threadId);
+ claimTimelineForLiveTurn(state, threadId, action.payload.requestId);
entries.push(
decorateEntry({
id: rowId,
@@ -1898,7 +1911,7 @@ const chatRuntimeSlice = createSlice({
const spawnSeq = spawnIdx >= 0 ? entries[spawnIdx].seq : undefined;
seq = spawnSeq ?? state.toolTimelineSeqByThread[threadId] ?? 0;
if (spawnSeq === undefined) state.toolTimelineSeqByThread[threadId] = seq + 1;
- claimTimelineForLiveTurn(state, threadId);
+ claimTimelineForLiveTurn(state, threadId, action.payload.requestId);
const row = decorateEntry({
id: rowId,
name: `subagent:${agentId}`,