Skip to content

The agent that grows with you

fix(desktop): scope inflight dump suppression to the live turn tail

686f6c612ecc1db27673
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts b/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts
index de03be31bdc..b34bf276430 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts
@@ -1157,4 +1157,36 @@ describe('appendLiveSessionProjection', () => {
'partial'
])
})
+
+ it('still projects inflight when only a completed historical tool reply has structure', () => {
+ // Older completed assistants keep reasoning/tool parts in the full
+ // transcript; they must not suppress a new turn's text projection.
+ const stored: ChatMessage[] = [
+ msg('old-user', 'user', 'previous task'),
+ {
+ id: 'old-assistant',
+ role: 'assistant',
+ parts: [
+ { type: 'tool-call', toolCallId: 'old', toolName: 'terminal', args: {} },
+ { type: 'text', text: 'done earlier' }
+ ]
+ },
+ msg('new-user', 'user', 'new task')
+ ]
+
+ const restored = appendLiveSessionProjection(stored, {
+ session_id: 'runtime-1',
+ inflight: {
+ user: 'new task',
+ assistant: 'working on it',
+ streaming: true
+ }
+ })
+
+ expect(restored.map(message => message.id)).toContain('assistant-stream-runtime-1')
+ expect(restored.at(-1)).toMatchObject({
+ id: 'assistant-stream-runtime-1',
+ pending: true
+ })
+ })
})
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts b/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts
index 6d88c83ed28..404b26ab2a4 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts
@@ -295,17 +295,23 @@ export function reconcileResumeMessages(nextMessages: ChatMessage[], previousMes
// the strict equality path — they reconcile a SETTLED row, and a growing
// row is by definition not settled.
//
- // Structured local assistant + text-only live projection (gateway inflight
- // dump) at the same ordinal is still the same turn even when answer text is
- // still empty — without this the dump would drop structure and reappear as
- // a plain-text sandwich (#76444).
+ // Live-tail identity: only treat structure as same-turn evidence when the
+ // cached row is still the in-flight stream (pending / stream id), not a
+ // historical completed assistant that merely shares a role ordinal after
+ // compression rewrote history (#76444 review).
+ const isLiveTailRow = (row: ChatMessage): boolean =>
+ Boolean(row.pending) ||
+ row.id.startsWith('assistant-stream-') ||
+ Boolean(row.interim)
+
const sameTurn =
sameText ||
(nextText.length > 0 && previousTrimmed.length > 0 && isStrictAnswerTextExtension(nextText, previousTrimmed)) ||
(message.role === 'assistant' &&
previous.role === 'assistant' &&
hasStructuralParts(previous) &&
- !hasStructuralParts(message))
+ !hasStructuralParts(message) &&
+ (isLiveTailRow(previous) || isLiveTailRow(message)))
if (sameTurn) {
preserved = preserveStructuralParts(preserved, previous)
@@ -690,20 +696,43 @@ export function appendLiveSessionProjection(
// Keep a pending assistant boundary even before the first delta when a
// queued user turn follows it. This preserves the two distinct turns.
//
- // When the transcript already holds a structured mid-turn assistant row
- // (reasoning / tool-call parts from the live stream or journal), do NOT
- // append a pure-text projection of `inflight.assistant` — that flat dump
- // re-renders thinking as answer text and sandwiches the structured parts
- // (#76444). Errors still force a projection so the failure is visible.
- const lastAssistant = [...messages].reverse().find(message => message.role === 'assistant')
- const turnAlreadyStructured = Boolean(lastAssistant && hasStructuralParts(lastAssistant))
+ // When the *current live turn* already holds a structured mid-turn assistant
+ // row (reasoning / tool-call from the live stream or journal), do NOT append
+ // a pure-text projection of `inflight.assistant` — that flat dump re-renders
+ // thinking as answer text and sandwiches the structured parts (#76444).
+ // Only inspect the live tail after the latest user run — never a completed
+ // historical tool-bearing reply earlier in the transcript (review feedback).
+ const liveStreamId = `assistant-stream-${sessionId}`
+ const liveAssistantOfCurrentTurn = (() => {
+ const byStreamId = messages.find(message => message.id === liveStreamId)
+ if (byStreamId) {
+ return byStreamId
+ }
+ // Assistants after the latest user row belong to this turn's tail.
+ if (latestUserIndex < 0) {
+ return null
+ }
+ for (let index = messages.length - 1; index > latestUserIndex; index -= 1) {
+ if (messages[index].role === 'assistant') {
+ return messages[index]
+ }
+ }
+ return null
+ })()
+ const turnAlreadyStructured = Boolean(
+ liveAssistantOfCurrentTurn &&
+ hasStructuralParts(liveAssistantOfCurrentTurn) &&
+ (liveAssistantOfCurrentTurn.pending ||
+ liveAssistantOfCurrentTurn.id === liveStreamId ||
+ liveAssistantOfCurrentTurn.interim)
+ )
if (inflightAssistant || inflightStreaming || inflightError || (inflightUser && queuedUser)) {
if (turnAlreadyStructured && !inflightError) {
// Structure is authoritative; skip the text-only dump row.
} else {
projected.push({
- id: `assistant-stream-${sessionId}`,
+ id: liveStreamId,
role: 'assistant',
parts: inflightAssistant ? [assistantTextPart(inflightAssistant)] : [],
pending: inflightStreaming,
diff --git a/apps/desktop/src/lib/inflight-turn-journal.test.ts b/apps/desktop/src/lib/inflight-turn-journal.test.ts
index 41d7a362d46..74338585fa6 100644
--- a/apps/desktop/src/lib/inflight-turn-journal.test.ts
+++ b/apps/desktop/src/lib/inflight-turn-journal.test.ts
@@ -179,7 +179,9 @@ describe('recoverInFlightTurnJournal', () => {
it('overlays the backend text-only projection instead of dropping local tool progress', () => {
// Sweeper regression on #44339: a backend `inflight` assistant snapshot
// (text only) used to mark the richer local tail "caught up" and delete
- // locally recorded tool calls.
+ // locally recorded tool calls. After #76444, longer text wins only when it
+ // is a strict extension of the journal answer (flat thinking dumps must
+ // not replace structured answer text).
journalEntry([
user('u1', 'do the thing'),
assistantWithTool('assistant-stream-old', 'local part', { pending: true })
@@ -187,7 +189,7 @@ describe('recoverInFlightTurnJournal', () => {
const base = [
user('db-u1', 'do the thing'),
- assistant('assistant-stream-rt9', 'longer partial text from the backend snapshot', { pending: true })
+ assistant('assistant-stream-rt9', 'local part and more from the backend snapshot', { pending: true })
]
const result = recoverInFlightTurnJournal('stored-1', base, { keepPending: true })
@@ -200,13 +202,35 @@ describe('recoverInFlightTurnJournal', () => {
// Keeps the BASE projection row id so live deltas keep landing on it.
expect(merged.id).toBe('assistant-stream-rt9')
expect(result.streamId).toBe('assistant-stream-rt9')
- // Journal structure survives; the longer backend text wins.
+ // Journal structure survives; strict-extension backend text wins.
expect(merged.parts[0]).toMatchObject({ type: 'tool-call', toolName: 'terminal' })
- expect(merged.parts[1]).toMatchObject({ type: 'text', text: 'longer partial text from the backend snapshot' })
+ expect(merged.parts[1]).toMatchObject({ type: 'text', text: 'local part and more from the backend snapshot' })
// Still in flight — the journal must NOT be cleared.
expect(readInFlightTurnJournal('stored-1')).not.toBeNull()
})
+ it('keeps journal answer text when a longer flat dump is not a strict extension (#76444)', () => {
+ journalEntry([
+ user('u1', 'do the thing'),
+ assistantWithTool('assistant-stream-old', 'partial', { pending: true })
+ ])
+
+ const base = [
+ user('db-u1', 'do the thing'),
+ assistant(
+ 'assistant-stream-rt9',
+ 'thinking chatter\nRan terminal\npartial and unrelated dump longer than answer',
+ { pending: true }
+ )
+ ]
+
+ const result = recoverInFlightTurnJournal('stored-1', base, { keepPending: true })
+ const merged = result.messages.at(-1)!
+
+ expect(merged.parts[0]).toMatchObject({ type: 'tool-call', toolName: 'terminal' })
+ expect(merged.parts[1]).toMatchObject({ type: 'text', text: 'partial' })
+ })
+
it('keeps the journal text when it is longer than the projection text', () => {
journalEntry([
user('u1', 'do the thing'),