Skip to content

The agent that grows with you

fix(desktop): a tool turn completing during the switch-back hydrate renders once

Brooklyn Nicholson3cf1b4197b10
diff --git a/apps/desktop/e2e/core/oracle.ts b/apps/desktop/e2e/core/oracle.ts
index dc208c68c85..1ae65e29069 100644
--- a/apps/desktop/e2e/core/oracle.ts
+++ b/apps/desktop/e2e/core/oracle.ts
@@ -379,6 +379,15 @@ function wireViolations(
// ─── The oracle ─────────────────────────────────────────────────────────
+// A steer that lands at a tool boundary is stored as its own user row, wrapped
+// for the model in the out-of-band marker (agent/prompt_builder.py); the user
+// sees only their words.
+const STEER_RE = /\[OUT-OF-BAND USER MESSAGE[^\]]*\]\s*([\s\S]*?)\s*\[\/OUT-OF-BAND USER MESSAGE\]/
+
+function unwrapSteer(content: string): string {
+ return STEER_RE.exec(content)?.[1] ?? content
+}
+
export interface OracleTarget {
/** Stored session id (the route id). */
sessionId: string
@@ -391,7 +400,11 @@ export interface OracleTarget {
function transcriptViolations(persisted: PersistedMessage[], view: RenderedView, target: OracleTarget): string[] {
const problems: string[] = []
- const rows = persisted.filter(m => (m.role === 'user' || m.role === 'assistant') && norm(m.content))
+
+ const rows = persisted
+ .filter(m => (m.role === 'user' || m.role === 'assistant') && norm(m.content))
+ .map(m => (m.role === 'user' ? { ...m, content: unwrapSteer(m.content) } : m))
+
const persistedMarkers = new Set<string>()
for (const marker of target.expectUserMarkers) {
diff --git a/apps/desktop/e2e/core/reply-settle-paths.spec.ts b/apps/desktop/e2e/core/reply-settle-paths.spec.ts
index 640b5f11182..ef4bc204048 100644
--- a/apps/desktop/e2e/core/reply-settle-paths.spec.ts
+++ b/apps/desktop/e2e/core/reply-settle-paths.spec.ts
@@ -11,8 +11,8 @@
*
* - an answer the model sent before a housekeeping tool, which the backend
* repeats as the final reply (interim + identical final), with and without
- * a review.summary row delivered before that turn's message.complete
- * (#131626: a slow end-of-turn flush lets the review fork's row win);
+ * a review.summary row delivered before that turn's message.complete (the
+ * #131626 report: a slow end-of-turn flush lets the review fork's row win);
* - a steer at each point of a tool turn: reasoning, the narration before a
* tool, the running tool, the answer after it, and twice in a row;
* - then a reload and a switch away and back, where the tool turns fold into
@@ -36,15 +36,9 @@ import {
writeProviderHome,
type WsRecorder
} from './harness'
-import { expectNoSymptom } from './known'
import { assertTranscriptOracle, installDuplicateSampler, type OracleTarget } from './oracle'
import { gate, type ScriptedProvider, startScriptedProvider } from './provider'
-const KNOWN: Record<string, string | undefined> = {
- reviewRow:
- '#131626 an answer repeated as the final reply renders twice when a review.summary row lands between the interim and the final'
-}
-
// Review after every turn, so a review fork is in flight during the next one.
const REVIEW_EVERY_TURN = `memory:
nudge_interval: 1
@@ -249,12 +243,9 @@ test('an answer repeated as the final reply renders once, with or without a row
await expect(rows).toHaveCount(1, { timeout: 60_000 })
})
- let live = 0
- let symptom = false
-
- await test.step('a review row lands between the interim and the final', async () => {
+ await test.step('a review row delivered before the final', async () => {
// The review fork starts before the turn's message.complete is sent; when
- // the end-of-turn flush is slow its row reaches the client first.
+ // the end-of-turn flush is slow its row reaches the client first (#131626).
frames.hold = eventOf('message.complete', A(2))
frames.until = eventOf('review.summary')
repeatedFinal(2)
@@ -265,26 +256,8 @@ test('an answer repeated as the final reply renders once, with or without a row
await expect(rows).toHaveCount(2, { timeout: 30_000 })
await settled(page, ws, A(2))
session.expectUserMarkers.push(U(2))
-
- // Converge with a deadline; a duplicate never heals, so the deadline only
- // waits out a legitimately slow settle.
- symptom = await expect
- .poll(async () => (live = (await viewport(page).innerText()).split(A(2)).length - 1), { timeout: 15_000 })
- .toBe(1)
- .then(() => false)
- .catch(() => true)
-
- if (!symptom) {
- await assertTranscriptOracle(page, ws, provider, session, 'review row between interim and final')
- }
+ await assertTranscriptOracle(page, ws, provider, session, 'review row before the final')
})
-
- expectNoSymptom(
- KNOWN.reviewRow,
- symptom,
- `the answer ${A(2)} renders twice around the review row`,
- `rendered ${live}x`
- )
})
})
diff --git a/apps/desktop/e2e/core/switch-back-race.spec.ts b/apps/desktop/e2e/core/switch-back-race.spec.ts
index b54107ad96b..a59c5961560 100644
--- a/apps/desktop/e2e/core/switch-back-race.spec.ts
+++ b/apps/desktop/e2e/core/switch-back-race.spec.ts
@@ -16,8 +16,10 @@
*
* Each order also runs with a tool turn (narration, a tool, then the held
* answer). History folds that turn into one bubble keyed by its first row
- * while the live reply is keyed by its last (#128809), so the hydrate and the
- * completion disagree on which message the answer belongs to.
+ * while the live reply is keyed by its last (#128809), and the folded text
+ * carries the narration too. complete-before-hydrate (tool turn) was red on
+ * base (the settled live reply painted beside the folded bubble); the overlay
+ * now matches the reply's stored row instead of its words.
*/
import { expect, type Page, test } from '@playwright/test'
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 98325409901..8eae9b9ec47 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
@@ -2152,6 +2152,39 @@ describe('overlayConcurrentMessageChanges', () => {
])
})
+ // The same race on a tool turn: history folds the narration, the tool and
+ // the reply into one bubble under the narration's row, so its text never
+ // equals the settled live reply. The reply's stored row still matches.
+ it('keeps one reply for a tool turn that completes while the switch-back hydrate is in flight', () => {
+ const baseline = [
+ msg('user-optimistic', 'user', 'prompt b'),
+ msg('assistant-stream-1-3', 'assistant', 'A2 ', { pending: true })
+ ]
+
+ const settled = msg('assistant-stream-1-3', 'assistant', 'A2 finished while away', {
+ pending: false,
+ rowId: 6,
+ parts: [{ type: 'text', text: 'A2 finished while away', sourceRowId: 6 }]
+ })
+
+ const folded = msg('4-assistant', 'assistant', '', {
+ rowId: 4,
+ parts: [
+ { type: 'text', text: 'A2i checking', sourceRowId: 4 },
+ { type: 'tool-call', toolCallId: 'call-1', toolName: 'terminal', args: {}, argsText: '{}', result: 'ok' },
+ { type: 'text', text: 'A2 finished while away', sourceRowId: 6 }
+ ]
+ } as Partial<ChatMessage>)
+
+ const overlaid = overlayConcurrentMessageChanges(
+ [msg('3-user', 'user', 'prompt b', { rowId: 3 }), folded],
+ baseline,
+ [baseline[0], settled]
+ )
+
+ expect(overlaid.map(message => message.id)).toEqual(['3-user', '4-assistant'])
+ })
+
// 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 640177f7baa..2378d3e9839 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
@@ -1652,6 +1652,7 @@ export function overlayConcurrentMessageChanges(
if (current.role === 'assistant' && current.pending !== true && !current.error && isLiveTailReplyId(current.id)) {
const text = textWithoutReferenceLines(chatMessageText(current)).trim()
const lastUser = overlaid.findLastIndex(message => message.role === 'user')
+ const liveRows = transcriptRowIds(current)
const committed = overlaid.some((message, index) => {
if (
@@ -1663,6 +1664,13 @@ export function overlayConcurrentMessageChanges(
return false
}
+ // The completion bound the reply to its stored row. A tool turn's
+ // history folds that row into the bubble that opened the turn, whose
+ // text also carries the narration, so match the row, not the words.
+ if (liveRows.length && transcriptRowIds(message).some(id => liveRows.includes(id))) {
+ return true
+ }
+
// The committed row and the settled live row capture the same reply
// at two moments while it kept streaming, so neither side is
// guaranteed to be textually identical: accept either as a forward