Skip to content

The agent that grows with you

fix(desktop): settle an equal no-delta completion onto the sealed pre-steer reply

kshitijk4poore4b8c30f8724
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/completion-boundaries.test.tsx b/apps/desktop/src/app/session/hooks/use-message-stream/completion-boundaries.test.tsx
index a43f080e873..2a77e18d248 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/completion-boundaries.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/completion-boundaries.test.tsx
@@ -170,7 +170,6 @@ it.each([false, true].flatMap(previousFailure => [ANSWER, ''].map(reply => ({ pr
)
it.each([
- { before: 'Answer', after: 'Answer' },
{ before: 'Answer', after: 'Answer with details' },
{ before: 'Answer with details', after: 'Answer' },
{ before: 'Answer', after: 'Rewritten reply', previewed: true },
@@ -221,6 +220,30 @@ it.each([
h.dispose()
})
+// A completion whose text IS the sealed pre-redirect reply, with nothing
+// streamed since, is that reply's own completion (rejected redirect race, or a
+// steer the model absorbed without new output): it settles the seal and the
+// correction stays the tail — see steer-arrival-order.test.tsx for the
+// streaming-side tripwire.
+it('settles an equal no-delta completion onto the sealed pre-redirect reply', async () => {
+ const h = mount()
+ await h.submit()
+ await h.send('message.start')
+ await h.send('message.delta', { text: 'Answer' })
+ await h.send('message.interim', { text: 'Answer', already_streamed: true })
+ await flush()
+ await h.redirect()
+ await h.send('message.complete', { text: 'Answer' })
+
+ expect(timeline(h.state().messages)).toEqual([
+ ['user', 'Give the answer.'],
+ ['assistant', 'Answer'],
+ ['user', 'Revise the answer.']
+ ])
+ expect(h.state().messages[1].interim).toBeFalsy()
+ h.dispose()
+})
+
it.each(['fallback', 'stream', 'tool-interim'] as const)(
'retires recovered metadata when completion settles the %s row',
async path => {
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/index.ts b/apps/desktop/src/app/session/hooks/use-message-stream/index.ts
index 4d2cbfbddf8..ae49254757f 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/index.ts
@@ -787,8 +787,31 @@ export function useMessageStream({
} else if (finalText) {
nextMessages = [...prev, newAssistantFromCompletion()]
}
- } else if (finalText) {
- nextMessages = [...prev, newAssistantFromCompletion()]
+ } else {
+ // Nothing streamed after the boundary and no `message.start` since
+ // the seal: a completion whose text IS the sealed pre-boundary reply
+ // is that reply's own completion (a redirect rejected after the row
+ // was painted, or a steer the model absorbed without new output),
+ // not a second occurrence. Anything else respects the boundary.
+ const sealedIndex =
+ !streamId && interimBoundaryPending && finalText
+ ? prev.findLastIndex(
+ (message, index) =>
+ index < lastUserIndex &&
+ message.role === 'assistant' &&
+ !message.hidden &&
+ message.interim === true &&
+ chatMessageText(message).trim() === finalText
+ )
+ : -1
+
+ if (sealedIndex >= 0) {
+ nextMessages = prev.map((message, messageIndex) =>
+ messageIndex === sealedIndex ? completeMessage(message) : message
+ )
+ } else if (finalText) {
+ nextMessages = [...prev, newAssistantFromCompletion()]
+ }
}
}
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 2703b8bc033..893d8a8c5e5 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
@@ -197,15 +197,15 @@ describe('steer mid-turn keeps arrival order (user bubble never above prior outp
expect(settled.indexOf(tail!)).toBeGreaterThan(settledSteerIndex)
})
- it('an accepted steer bounds completion even when no post-steer delta arrived', async () => {
+ it('steer with no post-steer deltas: completion settles above, bubble stays at the tail', async () => {
await mountHarness()
emit({ payload: {}, session_id: SID, type: 'message.start' })
emit({ payload: { text: 'the whole reply already streamed' }, session_id: SID, type: 'message.delta' })
await flushDeltas()
- // The gateway explicitly accepts this as redirected, not queued. Equal
- // output alone cannot place its completion back above the correction.
+ // Steer accepted during the final API call — the reply was already
+ // complete, so the correction becomes the NEXT turn's prompt.
await steer('one more thing')
emit({ payload: { text: 'the whole reply already streamed' }, session_id: SID, type: 'message.complete' })
@@ -213,10 +213,15 @@ describe('steer mid-turn keeps arrival order (user bubble never above prior outp
const messages = states.get(SID)!.messages
const steerIndex = messages.findIndex(message => message.role === 'user')
- expect(steerIndex).toBe(messages.length - 2)
- expect(messages.at(-1)?.role).toBe('assistant')
+ // The already-streamed reply settles onto its sealed bubble ABOVE the
+ // correction; the correction stays the tail, waiting for its own turn —
+ // no duplicate reply row appended below it. Load-bearing assumption: a
+ // completion settles the sealed pre-steer bubble in place and never
+ // appends/merges below the correction — if the reducer ever changes that,
+ // this test is the tripwire.
+ expect(steerIndex).toBe(messages.length - 1)
expect(messages.filter(message => chatMessageText(message).includes('whole reply already streamed'))).toHaveLength(
- 2
+ 1
)
expect(messages.every(message => message.pending !== true)).toBe(true)
})