Skip to content

The agent that grows with you

fix(desktop): compare transcript tips, not counts, in the stale-send guard

liuhao102426f9fd10359c
diff --git a/apps/desktop/src/app/chat/transcript-backfill.test.ts b/apps/desktop/src/app/chat/transcript-backfill.test.ts
index 11101616335..af84a07d341 100644
--- a/apps/desktop/src/app/chat/transcript-backfill.test.ts
+++ b/apps/desktop/src/app/chat/transcript-backfill.test.ts
@@ -240,6 +240,50 @@ describe('graftRefreshedTailOntoBackfill', () => {
})
})
+describe('messagesIfTranscriptBehind retention tips (#123909)', () => {
+ it('treats a retention-trimmed store ending on the page tip as current', () => {
+ // Retention released the head: the store holds fewer messages than the
+ // latest page renders, but its last durable row IS the page's own tip.
+ // The count comparison refused every send on that shape.
+ const trimmed = [chat('old-a', 110), chat('old-b', 111), chat('tip-user', 118), chat('tip-reply', 119)]
+
+ const page = [
+ chat('old-a', 110),
+ chat('old-b', 111),
+ chat('mid-turn', 115),
+ chat('tip-user', 118),
+ chat('tip-reply', 119)
+ ]
+
+ expect(page.length).toBeGreaterThan(trimmed.length)
+
+ expect(messagesIfTranscriptBehind(trimmed, page)).toBeNull()
+ })
+
+ it('skips unpersisted tail rows when establishing the local tip', () => {
+ // The guard runs on a baseline that may still end on a live row past the
+ // last durable one; the durable tip decides, not the stream bubble.
+ const local = [chat('durable-reply', 119), chat('live-stream')]
+ const page = [chat('older', 118), chat('durable-reply', 119)]
+
+ expect(messagesIfTranscriptBehind(local, page)).toBeNull()
+ })
+
+ it("still refuses when a peer window's newer row changes the tip (#65047)", () => {
+ const local = [chat('a', 118), chat('b', 119)]
+ const page = [...local, chat('peer-turn', 120)]
+
+ expect(messagesIfTranscriptBehind(local, page)).toBe(page)
+ })
+
+ it('falls back to counts when neither side carries a durable row id', () => {
+ const local = [chat('a'), chat('b')]
+ const page = [chat('a'), chat('b'), chat('c')]
+
+ expect(messagesIfTranscriptBehind(local, page)).toBe(page)
+ })
+})
+
describe('extendRefreshPageToOverlap', () => {
it('reads older pages until a long refresh shares a durable row with the rendered transcript', async () => {
const previous = [chat('earlier', 1), chat('prompt', 2), chat('reply', 3)]
diff --git a/apps/desktop/src/lib/stale-transcript-guard.ts b/apps/desktop/src/lib/stale-transcript-guard.ts
index 9c7faccbba9..93e4a06ea75 100644
--- a/apps/desktop/src/lib/stale-transcript-guard.ts
+++ b/apps/desktop/src/lib/stale-transcript-guard.ts
@@ -31,15 +31,37 @@ function authoredMessageCount(messages: ChatMessage[]): number {
return messages.reduce((count, message) => (message.systemNotice ? count : count + 1), 0)
}
+/**
+ * Latest persisted backend row the view carries. Retention only ever releases
+ * the head (rows older than the window plus its budget — see
+ * app/chat/transcript-retention.ts), so the last durable row is always the
+ * live tail; unpersisted rows (optimistic prompts, live streams) sit past it.
+ */
+function lastDurableRowId(messages: readonly ChatMessage[]): number | undefined {
+ for (let index = messages.length - 1; index >= 0; index -= 1) {
+ const rowId = messages[index].rowId
+
+ if (typeof rowId === 'number') {
+ return rowId
+ }
+ }
+
+ return undefined
+}
+
/**
* Chat messages to install when the authoritative latest page is ahead of the
* local view. Null when the local view is current.
*
- * Authored content is compared after `toChatMessages`, so tool rows folded into
- * an assistant bubble are not "ahead", and neither is a backend-authored
- * notice. A backfilled prefix is kept when the refreshed tail anchors inside
- * it. Live stream ids that do not anchor still use the count, so the window
- * that just finished the turn is not blocked when the counts match.
+ * Tips are compared before counts: a view ending on the page's own last
+ * durable row is current however much head retention has paged out — counts
+ * never converge there (#123909), while a peer window's newer row still
+ * changes the tip. Authored content is compared after `toChatMessages`, so
+ * tool rows folded into an assistant bubble are not "ahead", and neither is a
+ * backend-authored notice. A backfilled prefix is kept when the refreshed tail
+ * anchors inside it. Live stream ids that do not anchor still use the count,
+ * so the window that just finished the turn is not blocked when the counts
+ * match.
*/
export function messagesIfTranscriptBehind(
localMessages: ChatMessage[],
@@ -53,6 +75,13 @@ export function messagesIfTranscriptBehind(
return remoteChat
}
+ const localTip = lastDurableRowId(localMessages)
+ const remoteTip = lastDurableRowId(remoteChat)
+
+ if (localTip !== undefined && localTip === remoteTip) {
+ return null
+ }
+
const grafted = graftRefreshedTailOntoBackfill(remoteChat, localMessages)
const localAuthored = authoredMessageCount(localMessages)