Skip to content

The agent that grows with you

fix(desktop): a finished tool turn stays one bubble when the switch-back hydrate races it

Brooklyn Nicholsonfeca9757b7fa
diff --git a/apps/desktop/e2e/core/harness.ts b/apps/desktop/e2e/core/harness.ts
index d9634ab3a76..3d8c3f7ccc6 100644
--- a/apps/desktop/e2e/core/harness.ts
+++ b/apps/desktop/e2e/core/harness.ts
@@ -591,6 +591,8 @@ export async function currentSessionId(page: Page): Promise<string> {
export interface PersistedMessage {
role: string
content: string
+ /** Set on synthetic rows (a process notification, a model switch) the renderer draws as notices. */
+ displayKind?: string
}
/**
@@ -640,7 +642,8 @@ export async function persistedTranscript(
return (result?.messages ?? []).map((m: any) => ({
role: String(m.role ?? ''),
- content: typeof m.content === 'string' ? m.content : JSON.stringify(m.content ?? '')
+ content: typeof m.content === 'string' ? m.content : JSON.stringify(m.content ?? ''),
+ ...(typeof m.display_kind === 'string' && m.display_kind ? { displayKind: m.display_kind } : {})
}))
}
diff --git a/apps/desktop/e2e/core/oracle.ts b/apps/desktop/e2e/core/oracle.ts
index 1ae65e29069..72ef37c1c95 100644
--- a/apps/desktop/e2e/core/oracle.ts
+++ b/apps/desktop/e2e/core/oracle.ts
@@ -388,6 +388,20 @@ function unwrapSteer(content: string): string {
return STEER_RE.exec(content)?.[1] ?? content
}
+// Rows the backend writes itself (hydration.ts NOTICE_DISPLAY_KINDS, plus
+// hidden scaffolding): drawn as a notice or not at all, never as a message.
+const NOTICE_KINDS = new Set([
+ 'model_switch',
+ 'async_delegation_complete',
+ 'process_complete',
+ 'auto_continue',
+ 'personality_switch',
+ 'failed_turn',
+ 'hidden'
+])
+
+const isNotice = (m: PersistedMessage) => m.displayKind !== undefined && NOTICE_KINDS.has(m.displayKind)
+
export interface OracleTarget {
/** Stored session id (the route id). */
sessionId: string
@@ -402,7 +416,7 @@ function transcriptViolations(persisted: PersistedMessage[], view: RenderedView,
const problems: string[] = []
const rows = persisted
- .filter(m => (m.role === 'user' || m.role === 'assistant') && norm(m.content))
+ .filter(m => (m.role === 'user' || m.role === 'assistant') && !isNotice(m) && norm(m.content))
.map(m => (m.role === 'user' ? { ...m, content: unwrapSteer(m.content) } : m))
const persistedMarkers = new Set<string>()
diff --git a/apps/desktop/e2e/core/reply-settle-paths.spec.ts b/apps/desktop/e2e/core/reply-settle-paths.spec.ts
index ef4bc204048..a03e364cd73 100644
--- a/apps/desktop/e2e/core/reply-settle-paths.spec.ts
+++ b/apps/desktop/e2e/core/reply-settle-paths.spec.ts
@@ -317,7 +317,12 @@ test('a steer at every point of a tool turn, then reload and switch back', async
await steer('tool', 5, 6, () =>
expect.poll(() => fs.existsSync(ready), { timeout: 60_000, message: 'tool started' }).toBe(true)
)
+ // The steer hands the running command to the background; finishing it
+ // starts a notification turn (a process_complete row, not a user bubble).
+ provider.scriptPrompt('process', 'Background process', [{ text: words(A(20), 'noted', 'the', 'process') }])
fs.writeFileSync(release, '')
+ await settled(page, ws, A(20))
+ await assertTranscriptOracle(page, ws, provider, session, 'background process after a steered tool')
})
await test.step('steer while the answer after a tool streams', async () => {
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 8eae9b9ec47..4433ee8add9 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
@@ -2185,6 +2185,61 @@ describe('overlayConcurrentMessageChanges', () => {
expect(overlaid.map(message => message.id)).toEqual(['3-user', '4-assistant'])
})
+ // Same race, the order the e2e hit: the store already swapped its live rows
+ // for the committed bubble, but the page was composed from the older
+ // baseline and still carries the pending live row, frozen on its first chunk.
+ it('keeps a live row retired while the switch-back hydrate was in flight retired', () => {
+ const live = msg('assistant-stream-1-3', 'assistant', 'A2 ', {
+ pending: true,
+ parts: [
+ { type: 'tool-call', toolCallId: 'call-1', toolName: 'terminal', args: {}, argsText: '{}', result: 'ok' },
+ { type: 'text', text: 'A2 ' }
+ ]
+ } as Partial<ChatMessage>)
+
+ const baseline = [
+ msg('user-optimistic', 'user', 'prompt b'),
+ msg('assistant-stream-1-2', 'assistant', 'A2i checking', { interim: true }),
+ live
+ ]
+
+ const committed = [msg('3-user', 'user', 'prompt b', { rowId: 3 }), msg('4-assistant', 'assistant', 'A2 finished')]
+
+ expect(overlayConcurrentMessageChanges([...committed, live], baseline, committed).map(m => m.id)).toEqual([
+ '3-user',
+ '4-assistant'
+ ])
+ })
+
+ it('does not project a stale snapshot over a committed tool turn', () => {
+ const snapshot = {
+ session_id: 'runtime-b',
+ turn_started_at: 100,
+ inflight: { user: 'prompt b', assistant: 'A2i checking\n\nA2 ', streaming: true }
+ }
+
+ const folded = msg('4-assistant', 'assistant', '', {
+ rowId: 4,
+ timestamp: 105,
+ parts: [
+ { type: 'text', text: 'A2i checking' },
+ { type: 'tool-call', toolCallId: 'call-1', toolName: 'terminal', args: {}, argsText: '{}', result: 'ok' },
+ { type: 'text', text: 'A2 finished while away' }
+ ]
+ } as Partial<ChatMessage>)
+
+ const persisted = [msg('3-user', 'user', 'prompt b', { rowId: 3 }), folded]
+
+ expect(appendLiveSessionProjection(persisted, snapshot).map(m => m.id)).toEqual(['3-user', '4-assistant'])
+
+ // A mid-turn commit ends on its tool: the turn is still running and projects.
+ const partial = { ...folded, parts: folded.parts.slice(0, 2) }
+
+ expect(appendLiveSessionProjection([persisted[0], partial], snapshot).map(m => m.id)).toContain(
+ 'assistant-stream-runtime-b'
+ )
+ })
+
// The same prompt sent again (from another client, so the cache lacks its
// row) streams the same opening as the previous answer. The cached-transcript
// path must keep that NEXT turn's stream, and an errored settled row keeps
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 2378d3e9839..a5145e31394 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
@@ -121,6 +121,25 @@ export function isStrictAnswerTextExtension(next: string, previous: string): boo
return n.startsWith(p)
}
+/**
+ * Whether a committed bubble already holds the reply a stale live snapshot was
+ * streaming. History folds a tool turn's narration, tools and answer into one
+ * bubble while the snapshot carries the turn's whole text, so compare the
+ * bubble's text with whitespace collapsed. The bubble must end in a reply: a
+ * mid-turn commit ends on a tool call and the turn is still running.
+ */
+function committedReplyCovers(message: ChatMessage, snapshot: string): boolean {
+ const lastTool = message.parts.findLastIndex(part => part.type === 'tool-call')
+ const reply = chatMessageText({ ...message, parts: message.parts.slice(lastTool + 1) })
+
+ const text = message.parts
+ .flatMap(part => (part.type === 'text' ? [part.text] : []))
+ .join(' ')
+ .replace(/\s+/g, ' ')
+
+ return Boolean(reply.trim()) && isStrictAnswerTextExtension(text, snapshot.replace(/\s+/g, ' '))
+}
+
/**
* Carry the durable row id and reactions from a same-turn `previous` row onto
* `next` when it lacks them — reactions are keyed by row id, so they travel
@@ -1308,8 +1327,7 @@ export function appendLiveSessionProjection(messages: ChatMessage[], projection:
liveAssistantOfCurrentTurn &&
!isLiveTailRow(liveAssistantOfCurrentTurn) &&
committedDuringTurn(turnStartedAt, committedAt) &&
- !liveAssistantOfCurrentTurn.parts.some(part => part.type === 'tool-call') &&
- isStrictAnswerTextExtension(chatMessageText(liveAssistantOfCurrentTurn), inflightAssistant)
+ committedReplyCovers(liveAssistantOfCurrentTurn, inflightAssistant)
)
const wantsAssistantRow = Boolean(
@@ -1714,6 +1732,18 @@ export function overlayConcurrentMessageChanges(
changed = true
}
+ // A live row the store retired while REST was in flight (message.complete
+ // settled it into the committed reply) stays retired: the page was composed
+ // from the older baseline, so it can still carry that row by id.
+ const currentIds = new Set(currentMessages.map(message => message.id))
+
+ const retired = (message: ChatMessage) =>
+ isLiveTailRow(message) && baselineById.has(message.id) && !currentIds.has(message.id)
+
+ if (currentMessages.length && overlaid.some(retired)) {
+ return overlaid.filter(message => !retired(message))
+ }
+
return changed ? overlaid : nextMessages
}