Skip to content

The agent that grows with you

fix(desktop): keep a resent prompt's live reply and an errored settled row

teknium1714e5251f63a
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 fcf080be7e2..318c3bb7c0e 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
@@ -1756,6 +1756,7 @@ describe('overlayConcurrentMessageChanges', () => {
it('keeps one reply when message.complete lands while the switch-back hydrate is in flight', () => {
const snapshot = {
session_id: 'runtime-b',
+ turn_started_at: 100,
inflight: { user: 'prompt b', assistant: 'A2 ', streaming: true }
}
@@ -1771,7 +1772,7 @@ describe('overlayConcurrentMessageChanges', () => {
const persisted = [
msg('3-user', 'user', 'prompt b', { rowId: 3 }),
- msg('4-assistant', 'assistant', 'A2 finished while away', { rowId: 4 })
+ msg('4-assistant', 'assistant', 'A2 finished while away', { rowId: 4, timestamp: 105 })
]
const hydrated = appendLiveSessionProjection(persisted, snapshot)
@@ -1794,6 +1795,40 @@ describe('overlayConcurrentMessageChanges', () => {
['assistant-stream-1-2', 'A2 finished']
])
})
+
+ // 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
+ // its failure instead of folding into identical committed text.
+ it('keeps the next turn of a resent prompt and an errored settled row', () => {
+ const cached = [
+ msg('3-user', 'user', 'prompt b', { rowId: 3 }),
+ msg('4-assistant', 'assistant', 'Same answer', { rowId: 4, timestamp: 105 })
+ ]
+
+ const snapshot = {
+ session_id: 'runtime-b',
+ turn_started_at: 200,
+ inflight: { user: 'prompt b', assistant: 'Same ', streaming: true }
+ }
+
+ const hydrated = appendLiveSessionProjection(cached, snapshot)
+
+ expect(hydrated.map(message => [message.id, chatMessageText(message)])).toEqual([
+ ['3-user', 'prompt b'],
+ ['4-assistant', 'Same answer'],
+ ['assistant-stream-runtime-b', 'Same ']
+ ])
+
+ const settledNextTurn = msg('assistant-stream-1-3', 'assistant', 'Same answer', { pending: false })
+
+ expect(overlayConcurrentMessageChanges(cached, cached, [...cached, settledNextTurn]).at(-1)).toBe(settledNextTurn)
+
+ const errored = msg('assistant-stream-1-2', 'assistant', 'A2 done', { pending: false, error: 'stream lost' })
+ const page = [msg('3-user', 'user', 'prompt b'), msg('4-assistant', 'assistant', 'A2 done')]
+
+ expect(overlayConcurrentMessageChanges(page, [page[0]], [page[0], errored]).at(-1)).toBe(errored)
+ })
})
describe('preserveEquivalentTranscript', () => {
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 dd707dc483c..ecdffe8dbab 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
@@ -877,7 +877,7 @@ export function preserveLocalPendingTurnMessages(
*/
const safelyPersistedInflightUser = Symbol('safelyPersistedInflightUser')
-type LiveSessionProjection = Pick<SessionResumeResult, 'inflight' | 'queued' | 'session_id'> & {
+type LiveSessionProjection = Pick<SessionResumeResult, 'inflight' | 'queued' | 'session_id' | 'turn_started_at'> & {
[safelyPersistedInflightUser]?: true
}
@@ -1037,12 +1037,20 @@ export function appendLiveSessionProjection(messages: ChatMessage[], projection:
// An activate snapshot taken before the turn committed goes stale once REST
// returns the committed reply after the persisted prompt: its partial
// `inflight.assistant` is a prefix of that reply, and projecting it paints
- // the answer twice (the extra row frozen on its first chunk).
+ // the answer twice (the extra row frozen on its first chunk). Text alone
+ // cannot tell this turn's reply from the previous turn's answer to the same
+ // resent prompt, so the reply must have been written after this turn began.
+ const turnStartedAt = projection.turn_started_at
+ const committedAt = liveAssistantOfCurrentTurn?.timestamp
+
const turnAlreadyCommitted = Boolean(
inflightUserAlreadyPersisted &&
!inflightError &&
liveAssistantOfCurrentTurn &&
!isLiveTailRow(liveAssistantOfCurrentTurn) &&
+ typeof turnStartedAt === 'number' &&
+ typeof committedAt === 'number' &&
+ committedAt >= turnStartedAt &&
!liveAssistantOfCurrentTurn.parts.some(part => part.type === 'tool-call') &&
isStrictAnswerTextExtension(chatMessageText(liveAssistantOfCurrentTurn), inflightAssistant)
)
@@ -1325,7 +1333,10 @@ export function overlayConcurrentMessageChanges(
// message.complete settled this stream row while REST was in flight, and
// the page already carries the same reply under its committed id (#70209).
- if (current.role === 'assistant' && current.pending !== true && isLiveTailReplyId(current.id)) {
+ // Only a row the page newly added counts: one already in the baseline is an
+ // earlier turn's answer (a resent prompt can repeat it word for word). An
+ // errored row carries a failure the committed text cannot show.
+ 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')
@@ -1333,6 +1344,7 @@ export function overlayConcurrentMessageChanges(
(message, index) =>
index > lastUser &&
message.role === 'assistant' &&
+ !baselineById.has(message.id) &&
!isLiveTailRow(message) &&
textWithoutReferenceLines(chatMessageText(message)).trim() === text
)