Skip to content

The agent that grows with you

fix(desktop): anchor a pre-token steer tail on the persisted prompt

kshitijk4poord1399f206271
diff --git a/apps/desktop/src/lib/inflight-turn-journal.test.ts b/apps/desktop/src/lib/inflight-turn-journal.test.ts
index a573029d06e..e581f003499 100644
--- a/apps/desktop/src/lib/inflight-turn-journal.test.ts
+++ b/apps/desktop/src/lib/inflight-turn-journal.test.ts
@@ -1,6 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
-import type { ChatMessage } from '@/lib/chat-messages'
+import { type ChatMessage, chatMessageText } from '@/lib/chat-messages'
import {
clearInFlightTurnJournal,
type JournalableSessionState,
@@ -693,6 +693,27 @@ describe('recoverInFlightTurnJournal', () => {
expect(readInFlightTurnJournal('stored-1')).toBeNull()
})
+ // A steer typed before the first token journals `[prompt, steer, assistant]`.
+ // When only the prompt persisted, the prompt is the anchor: the steer and
+ // the partial reply are recovered under it, and the prompt is not repeated.
+ it('anchors a pre-token steer tail on the persisted prompt', () => {
+ journalEntry([
+ user('user-1', 'remove the session counts'),
+ user('user-2', 'hurry up'),
+ assistant('assistant-stream-1', 'Moving.')
+ ])
+
+ const base = [user('db-u1', 'remove the session counts')]
+ const result = recoverInFlightTurnJournal('stored-1', base, { keepPending: false })
+
+ expect(result.applied).toBe(true)
+ expect(result.messages.map(message => `${message.role}:${chatMessageText(message)}`)).toEqual([
+ 'user:remove the session counts',
+ 'user:hurry up',
+ 'assistant:Moving.'
+ ])
+ })
+
it('does not re-append committed answers when the journal tail has no user row', () => {
// 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
diff --git a/apps/desktop/src/lib/inflight-turn-journal.ts b/apps/desktop/src/lib/inflight-turn-journal.ts
index 78f1e7086f6..96ca50e23c2 100644
--- a/apps/desktop/src/lib/inflight-turn-journal.ts
+++ b/apps/desktop/src/lib/inflight-turn-journal.ts
@@ -752,11 +752,31 @@ export function mergeInFlightMessages(
return noop
}
- const tailUserIndex = tail.findLastIndex(message => message.role === 'user')
- const tailUser = tailUserIndex >= 0 ? tail[tailUserIndex] : null
+ // Anchor on the latest tail user the base already holds. A steer typed
+ // before the first token rides in the tail behind its prompt; when only the
+ // prompt persisted, the prompt is the anchor and the steer is recovered
+ // output, not a reason to append the prompt a second time.
+ let tailUserIndex = tail.findLastIndex(message => message.role === 'user')
+ let matchingUserIndex = -1
+
+ for (let index = tailUserIndex; index >= 0; index -= 1) {
+ const tailUser = tail[index]
+
+ if (tailUser.role !== 'user') {
+ continue
+ }
+
+ matchingUserIndex = baseMessages.findLastIndex(message => userMessagesMatch(message, tailUser))
+
+ if (matchingUserIndex >= 0) {
+ tailUserIndex = index
+
+ break
+ }
+ }
+
let tailAssistants = tail.slice(tailUserIndex + 1)
let lastJournalRow = tailAssistants.findLast(assistantHasRecoverableContent) ?? null
- const matchingUserIndex = tailUser ? baseMessages.findLastIndex(message => userMessagesMatch(message, tailUser)) : -1
if (matchingUserIndex < 0) {
// No base user matches the tail's user row (a projected user-inflight row