Skip to content

The agent that grows with you

fix(desktop): gate transformed-final settle on the interim boundary (#96465)

kshitijk4poor6cb1aa025c85
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 e3186e0c98b..e48d8fceb08 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
@@ -801,19 +801,12 @@ export function useMessageStream({
(finalText === existingText || finalText.startsWith(existingText) || existingText.startsWith(finalText))
)
- // A transform_llm_output hook rewrites the final after streaming (e.g. pseudonym
- // restore), so it may share no prefix with the streamed text yet is still this
- // turn's authoritative reply: settle it in place rather than append a duplicate.
- // Gated on sawAssistantPayload so a stale completion can't overwrite an older bubble.
- const transformedCurrentReply = Boolean(responseTransformed && state.sawAssistantPayload && finalText)
-
- if (
- existing.pending ||
- transformedCurrentReply ||
- (!interimBoundaryPending && finalText && existingText === finalText)
- ) {
+ if (existing.pending || (!interimBoundaryPending && finalText && existingText === finalText)) {
nextMessages = settleAt(index)
- } else if ((interimBoundaryPending && responsePreviewed) || finalContinuesInterim) {
+ } else if (
+ (interimBoundaryPending && (responsePreviewed || responseTransformed)) ||
+ finalContinuesInterim
+ ) {
// Settle the interim in place instead of creating a duplicate —
// the DB has one row, so the live UI must agree. Two distinct
// settle paths with different boundary requirements:
@@ -829,6 +822,10 @@ export function useMessageStream({
// complete({response_previewed: true, text: 'new'}) would
// silently destroy 'old').
//
+ // • responseTransformed (a transform_llm_output hook rewrote the
+ // final after streaming, e.g. pseudonym restore) shares the
+ // same no-continuity shape, so it takes the same boundary gate.
+ //
// • finalContinuesInterim (prefix-either-way continuity, same
// text or one a prefix of the other) is safe to settle
// flag-free within this user occurrence: a `message.start`
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/transformed-final.test.tsx b/apps/desktop/src/app/session/hooks/use-message-stream/transformed-final.test.tsx
index d2ac57729e7..dd6bff1f93e 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/transformed-final.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/transformed-final.test.tsx
@@ -23,6 +23,9 @@ const delta = (text: string) =>
const interim = (text: string) =>
act(() => stream.handleEvent({ payload: { text, already_streamed: true }, session_id: SID, type: 'message.interim' }))
+const complete = (text: string) =>
+ act(() => stream.handleEvent({ payload: { text }, session_id: SID, type: 'message.complete' }))
+
const completeTransformed = (text: string) =>
act(() =>
stream.handleEvent({ payload: { text, response_transformed: true }, session_id: SID, type: 'message.complete' })
@@ -68,4 +71,16 @@ describe('useMessageStream response_transformed settlement', () => {
expect(texts[0]).toBe('example-service.internal')
expect(texts).not.toContain('TOKEN_1')
})
+
+ it('never lets a transformed completion overwrite an already-settled reply', async () => {
+ mountStream()
+ await start()
+ await interim('old')
+ await complete('old')
+
+ // The interim boundary is spent: a late transformed final is a distinct reply.
+ await completeTransformed('new')
+
+ expect(assistantTexts()).toEqual(['old', 'new'])
+ })
})