Skip to content

The agent that grows with you

fix: preserve newer replies across delayed redirect receipts

Tekniumac30b57acbdd
diff --git a/apps/desktop/src/app/chat/session-tile-actions.ts b/apps/desktop/src/app/chat/session-tile-actions.ts
index e138048effd..b9edd04bd0b 100644
--- a/apps/desktop/src/app/chat/session-tile-actions.ts
+++ b/apps/desktop/src/app/chat/session-tile-actions.ts
@@ -421,7 +421,11 @@ export function useSessionTileActions({ requestGateway, runtimeId, scope, stored
mutate(state => {
const message = state.messages.find(candidate => candidate.id === messageId)
- return message
+ // A newer reply may precede this ACK; it already owns its boundary.
+ const hasNewReply = state.messages.slice(state.messages.findIndex(candidate => candidate.id === messageId) + 1)
+ .some(candidate => candidate.role === 'assistant')
+
+ return message && state.streamId && !hasNewReply
? appendMidTurnUserMessage(
{ ...state, messages: state.messages.filter(candidate => candidate.id !== messageId) },
message
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/steer-arrival-order.test.tsx b/apps/desktop/src/app/session/hooks/use-message-stream/steer-arrival-order.test.tsx
index 3af728b10dc..3d09a11d2ad 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/steer-arrival-order.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/steer-arrival-order.test.tsx
@@ -319,6 +319,25 @@ describe('steer mid-turn keeps arrival order (user bubble never above prior outp
}
})
+ it.each([false, true])('a delayed accepted redirect does not move its correction past a new reply (tile=%s)', async tile => {
+ await mountHarness(tile)
+ emit({ payload: {}, session_id: SID, type: 'message.start' })
+ emit({ payload: { text: 'old reply' }, session_id: SID, type: 'message.delta' })
+ await flushDeltas()
+ let accept!: () => void
+ requestGatewayMock.mockImplementationOnce(() => new Promise(resolve => { accept = () => resolve({ status: 'redirected' }) }))
+ let pending!: Promise<unknown>
+ act(() => { pending = redirect!('correction') })
+ emit({ payload: { text: 'old reply' }, session_id: SID, type: 'message.complete' })
+ emit({ payload: {}, session_id: SID, type: 'message.start' })
+ emit({ payload: { text: 'new reply' }, session_id: SID, type: 'message.delta' })
+ await flushDeltas()
+ const newStreamId = states.get(SID)!.streamId
+ await act(async () => { accept(); await pending })
+ expect(states.get(SID)!.streamId).toBe(newStreamId)
+ expect(states.get(SID)!.messages.map(message => chatMessageText(message))).toEqual(['old reply', 'correction', 'new reply'])
+ })
+
it('a redirect the gateway rejects discards the optimistic bubble instead of stranding it', async () => {
await mountHarness()
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
index c1a7ea2efc9..97330ef224b 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
@@ -827,7 +827,11 @@ export function usePromptActions({
updateSessionState(id, state => {
const message = state.messages.find(candidate => candidate.id === messageId)
- return message
+ // A newer reply may precede this ACK; it already owns its boundary.
+ const hasNewReply = state.messages.slice(state.messages.findIndex(candidate => candidate.id === messageId) + 1)
+ .some(candidate => candidate.role === 'assistant')
+
+ return message && state.streamId && !hasNewReply
? appendMidTurnUserMessage(
{ ...state, messages: state.messages.filter(candidate => candidate.id !== messageId) },
message