Skip to content

The agent that grows with you

fix(desktop): fold only a partial committed during this turn; carry rowId

kshitijk4poor42457cc23378
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 0e1448b642e..62a5b05a968 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
@@ -1754,22 +1754,28 @@ describe('appendLiveSessionProjection', () => {
} as ChatMessage
]
- const restored = appendLiveSessionProjection(stored, {
- session_id: 's1',
- inflight: {
- user: 'Fais X',
- assistant: 'Tu as raison. Je les regarde vraiment cette fois. + more',
- streaming: true
- }
- })
+ const inflight = {
+ user: 'Fais X',
+ assistant: 'Tu as raison. Je les regarde vraiment cette fois. + more',
+ streaming: true
+ }
+
+ const restored = appendLiveSessionProjection(stored, { session_id: 's1', turn_started_at: 100, inflight })
const assistants = restored.filter(message => message.role === 'assistant')
expect(assistants).toHaveLength(1)
expect(assistants[0].id).toBe('assistant-stream-s1')
expect(assistants[0].pending).toBe(true)
- // The committed row's tool structure survives; the fuller live text wins.
+ // The committed row's tool structure and row id survive; the fuller live text wins.
expect(assistants[0].parts.some(part => part.type === 'tool-call')).toBe(true)
+ expect(assistants[0].rowId).toBe(13)
expect(chatMessageText(assistants[0])).toBe('Tu as raison. Je les regarde vraiment cette fois. + more')
+
+ // Without turn_started_at (older runtime) the tail may be the PREVIOUS
+ // turn's answer to a resent prompt: keep both rows rather than drop it.
+ const untimed = appendLiveSessionProjection(stored, { session_id: 's1', inflight })
+
+ expect(untimed.filter(message => message.role === 'assistant')).toHaveLength(2)
})
})
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 735fd92b338..74ced0dd221 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
@@ -112,6 +112,36 @@ export function isStrictAnswerTextExtension(next: string, previous: string): boo
return n.startsWith(p)
}
+/**
+ * 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
+ * together. Returns `next` itself when there is nothing to carry, else a NEW
+ * object (the runtime repository caches normalized messages by identity).
+ */
+function carryRowIdentity(next: ChatMessage, previous: ChatMessage): ChatMessage {
+ const rowId = next.rowId === undefined ? previous.rowId : undefined
+ const reactions = next.reactions === undefined && previous.reactions?.length ? previous.reactions : undefined
+
+ if (rowId === undefined && !reactions) {
+ return next
+ }
+
+ return {
+ ...next,
+ ...(rowId !== undefined ? { rowId } : {}),
+ ...(reactions ? { reactions: [...reactions] } : {})
+ }
+}
+
+/**
+ * True only when a row was written after the live turn began. Text alone
+ * cannot tell this turn's reply from the previous turn's answer to the same
+ * resent prompt, so a missing timestamp (older runtime) never passes.
+ */
+function committedDuringTurn(turnStartedAt: unknown, committedAt: unknown): boolean {
+ return typeof turnStartedAt === 'number' && typeof committedAt === 'number' && committedAt >= turnStartedAt
+}
+
/**
* Carry structural parts an authoritative row cannot express.
*
@@ -459,12 +489,8 @@ export function reconcileResumeMessages(nextMessages: ChatMessage[], previousMes
// neither. Carry the cached copy forward so a reaction doesn't blink off
// mid-turn. NEW object every time — the runtime repository's WeakMap
// caches normalized ThreadMessages by ChatMessage identity.
- if (sameTurn && preserved.rowId === undefined && previous.rowId !== undefined) {
- preserved = { ...preserved, rowId: previous.rowId }
- }
-
- if (sameTurn && preserved.reactions === undefined && previous.reactions?.length) {
- preserved = { ...preserved, reactions: [...previous.reactions] }
+ if (sameTurn) {
+ preserved = carryRowIdentity(preserved, previous)
}
const previousImages = embeddedImageUrls(previousText)
@@ -551,19 +577,8 @@ const localPendingSupersedes = (local: ChatMessage, authoritative: ChatMessage):
* the turn is still running and on durable row identity — so a settled shell
* must not repaint the reply as perpetually streaming.
*/
-const withAuthoritativeTurnState = (local: ChatMessage, authoritative: ChatMessage): ChatMessage => {
- const merged: ChatMessage = { ...local, pending: authoritative.pending === true }
-
- if (local.rowId === undefined && authoritative.rowId !== undefined) {
- merged.rowId = authoritative.rowId
- }
-
- if (local.reactions === undefined && authoritative.reactions?.length) {
- merged.reactions = [...authoritative.reactions]
- }
-
- return merged
-}
+const withAuthoritativeTurnState = (local: ChatMessage, authoritative: ChatMessage): ChatMessage =>
+ carryRowIdentity({ ...local, pending: authoritative.pending === true }, authoritative)
/** Text of the response that follows a folded tool round, not the commentary before it. */
function lastFoldedResponseText(message: ChatMessage): string {
@@ -1144,9 +1159,7 @@ export function appendLiveSessionProjection(messages: ChatMessage[], projection:
!inflightError &&
liveAssistantOfCurrentTurn &&
!isLiveTailRow(liveAssistantOfCurrentTurn) &&
- typeof turnStartedAt === 'number' &&
- typeof committedAt === 'number' &&
- committedAt >= turnStartedAt &&
+ committedDuringTurn(turnStartedAt, committedAt) &&
!liveAssistantOfCurrentTurn.parts.some(part => part.type === 'tool-call') &&
isStrictAnswerTextExtension(chatMessageText(liveAssistantOfCurrentTurn), inflightAssistant)
)
@@ -1177,14 +1190,15 @@ export function appendLiveSessionProjection(messages: ChatMessage[], projection:
!inflightError &&
inflightStreaming &&
committedPartial &&
- committedPartialAt >= 0 &&
inflightUserAlreadyPersisted &&
!correctionOffsetsUsable &&
- (typeof turnStartedAt !== 'number' || typeof committedAt !== 'number' || committedAt >= turnStartedAt) &&
+ committedDuringTurn(turnStartedAt, committedAt) &&
(committedPartialText.trim() === inflightAssistant.trim() ||
isStrictAnswerTextExtension(inflightAssistant, committedPartialText))
)
+ const foldTarget = turnPartiallyCommitted ? committedPartial : null
+
const pushCorrection = (correction: string, index: number): void => {
if (persistedInLatestRun(correction)) {
return
@@ -1247,17 +1261,13 @@ export function appendLiveSessionProjection(messages: ChatMessage[], projection:
...(inflightError && inflightErrorSurface ? { errorSurface: inflightErrorSurface } : {})
}
- if (turnPartiallyCommitted && committedPartial) {
+ if (foldTarget) {
// #121122: the persisted tail IS this turn's partial — replace it in
// place so the transcript holds one row that keeps streaming.
// Structure the flat dump cannot express (tool calls, reasoning)
- // carries over; reactions stay so nothing blinks off mid-turn.
- const merged = preserveStructuralParts(liveRow, committedPartial)
-
- projected.push({
- ...merged,
- ...(committedPartial.reactions?.length ? { reactions: [...committedPartial.reactions] } : {})
- })
+ // carries over; row id and reactions stay so nothing blinks off
+ // mid-turn and a reaction toggle still reaches the persisted row.
+ projected.push(carryRowIdentity(preserveStructuralParts(liveRow, foldTarget), foldTarget))
} else {
projected.push(liveRow)
}
@@ -1276,7 +1286,7 @@ export function appendLiveSessionProjection(messages: ChatMessage[], projection:
})
}
- if (turnPartiallyCommitted && committedPartialAt >= 0) {
+ if (foldTarget) {
// Splice the folded live row (plus any corrections/queued tail) into the
// committed partial's slot instead of appending beside it.
return [...messages.slice(0, committedPartialAt), ...projected, ...messages.slice(committedPartialAt + 1)]