Skip to content

The agent that grows with you

fix(desktop): let the last committed turn retire a stale live journal row

kshitijk4poore0f012807440
diff --git a/apps/desktop/src/lib/inflight-turn-journal.test.ts b/apps/desktop/src/lib/inflight-turn-journal.test.ts
index eba26483427..a573029d06e 100644
--- a/apps/desktop/src/lib/inflight-turn-journal.test.ts
+++ b/apps/desktop/src/lib/inflight-turn-journal.test.ts
@@ -671,11 +671,15 @@ describe('recoverInFlightTurnJournal', () => {
// desktop as "the answer was already there, but it was inputted again".
it('does not re-append committed answers when the journaled user row never persisted', () => {
- // The prompt projection did not persist, but durable assistant identity
- // proves this exact answer is already stored. Equal text alone cannot.
+ // A resume projection can journal a `user-inflight-*` row that was never
+ // written to the DB (and may even belong to a different conversation).
+ // Because no base user matches it, the fold used to treat the whole tail
+ // as unknown and append it — duplicating the assistant answers below.
+ // Live rows never carry a durable id; the last committed turn's equal
+ // reply is what proves them stale.
journalEntry([
user('user-inflight-a3c2beb1', 'a stray user bubble that never persisted'),
- assistant('assistant-stream-1', 'the committed answer', { rowId: 42 })
+ assistant('assistant-stream-1', 'the committed answer')
])
const base = [user('db-u1', 'the real prompt'), assistant('db-a1', 'the committed answer', { rowId: 42 })]
@@ -693,7 +697,7 @@ describe('recoverInFlightTurnJournal', () => {
// A tail captured after a partial hydrate can end on assistant rows with
// no user prompt before them. The old code appended them verbatim, so the
// transcript ended with a duplicate of an answer that was already settled.
- journalEntry([assistant('assistant-stream-1', 'the committed answer', { rowId: 42 })])
+ journalEntry([assistant('assistant-stream-1', 'the committed answer')])
const base = [user('db-u1', 'the real prompt'), assistant('db-a1', 'the committed answer', { rowId: 42 })]
const result = recoverInFlightTurnJournal('stored-1', base, { keepPending: false })
diff --git a/apps/desktop/src/lib/inflight-turn-journal.ts b/apps/desktop/src/lib/inflight-turn-journal.ts
index abe2098d94e..78f1e7086f6 100644
--- a/apps/desktop/src/lib/inflight-turn-journal.ts
+++ b/apps/desktop/src/lib/inflight-turn-journal.ts
@@ -702,8 +702,10 @@ function withoutBaseIds(rows: ChatMessage[], baseMessages: ChatMessage[]): ChatM
return rows.filter(row => !baseIds.has(row.id))
}
-/** Without a matched user interval, only explicit row identity can establish
- * coverage. An older turn saying the same thing does not retire this journal. */
+/** Without a matched user interval, a journal can only be stale relative to
+ * the turn that most recently committed. A live row (no durable id yet) is
+ * covered by an equal reply in that last turn; a durable row needs identity.
+ * An older turn saying the same thing does not retire this journal. */
function journalTailAlreadyCommitted(tailAssistants: ChatMessage[], baseMessages: ChatMessage[]): boolean {
const recoverable = tailAssistants.filter(assistantHasRecoverableContent)
@@ -711,9 +713,12 @@ function journalTailAlreadyCommitted(tailAssistants: ChatMessage[], baseMessages
return false
}
- return recoverable.every(message => baseMessages.some(base =>
+ const lastTurnStart = baseMessages.findLastIndex(message => message.role === 'user' && !message.hidden)
+
+ return recoverable.every(message => baseMessages.some((base, index) =>
base.role === 'assistant' && !base.hidden && !isLiveProjectionRow(base) && !base.recovered &&
- (base.id === message.id || (base.rowId !== undefined && base.rowId === message.rowId)) &&
+ (base.id === message.id ||
+ (message.rowId === undefined ? index > lastTurnStart : base.rowId !== undefined && base.rowId === message.rowId)) &&
base.error === message.error &&
normalizedText(chatMessageText(base)) === normalizedText(chatMessageText(message)) &&
message.parts.every(part => {
diff --git a/apps/desktop/src/lib/transcript-occurrences.test.ts b/apps/desktop/src/lib/transcript-occurrences.test.ts
index 2a4225b4e01..4cab9b2867a 100644
--- a/apps/desktop/src/lib/transcript-occurrences.test.ts
+++ b/apps/desktop/src/lib/transcript-occurrences.test.ts
@@ -88,10 +88,17 @@ it('recovers only the uncommitted journal suffix until the complete reply become
})
it.each(['missing prompt', 'repeated prompt', 'no prompt'])('does not clear an unknown journal occurrence by global text: %s', shape => {
- const older = toChatMessages([prompt, { id: 2, role: 'assistant', content: 'Still working.', timestamp: 2 }])
+ // The equal reply belongs to an OLDER turn; only the most recently
+ // committed turn can prove a live journal row stale.
+ const older = toChatMessages([
+ prompt,
+ { id: 2, role: 'assistant', content: 'Still working.', timestamp: 2 },
+ { id: 3, role: 'user', content: 'And then?', timestamp: 3 },
+ { id: 4, role: 'assistant', content: 'Done.', timestamp: 4 }
+ ])
const user: ChatMessage = {
- id: 'new-user', rowId: 3, role: 'user',
+ id: 'new-user', rowId: 5, role: 'user',
parts: [{ type: 'text', text: shape === 'repeated prompt' ? 'Inspect each phase' : 'A new question' }]
}
@@ -107,7 +114,7 @@ it.each(['missing prompt', 'repeated prompt', 'no prompt'])('does not clear an u
vi.advanceTimersByTime(400)
const recovered = recoverInFlightTurnJournal('repeated-session', older)
expect(recovered.caughtUp).toBe(false)
- expect(assistantTexts(recovered.messages)).toEqual(['Still working.', 'Still working.'])
+ expect(assistantTexts(recovered.messages)).toEqual(['Still working.', 'Done.', 'Still working.'])
expect(readInFlightTurnJournal('repeated-session')).not.toBeNull()
})