Skip to content

The agent that grows with you

fix(desktop): preserve stale guard fork protection

Brooklyn Nicholsonb20281dcc028
diff --git a/apps/desktop/src/lib/stale-transcript-guard.test.ts b/apps/desktop/src/lib/stale-transcript-guard.test.ts
index eab43cd3ef8..9edbc1462ee 100644
--- a/apps/desktop/src/lib/stale-transcript-guard.test.ts
+++ b/apps/desktop/src/lib/stale-transcript-guard.test.ts
@@ -186,7 +186,7 @@ describe('surplusIsCompetingView', () => {
competingViewAssistantMessage('msg-11', '⌛ Approval timed out after 5 minutes', 11)
]
- expect(surplusIsCompetingView(local, refreshed)).toBe(false)
+ expect(surplusIsCompetingView(local, refreshed, { optimisticMessageId: 'opt-old' })).toBe(false)
})
it('a plain completed tool turn single-window surplus is not a competing view', () => {
@@ -199,7 +199,17 @@ describe('surplusIsCompetingView', () => {
competingViewAssistantMessage('msg-12', 'all green', 12)
]
- expect(surplusIsCompetingView(local, refreshed)).toBe(false)
+ expect(surplusIsCompetingView(local, refreshed, { optimisticMessageId: 'opt-old' })).toBe(false)
+ })
+
+ it('rejects a repeated prompt from another view even when the text matches', () => {
+ const local = [competingViewUserMessage('prior', 'run the tests')]
+ const refreshed = [
+ competingViewUserMessage('prior', 'run the tests', 10),
+ competingViewUserMessage('peer', 'run the tests', 12)
+ ]
+
+ expect(surplusIsCompetingView(local, refreshed)).toBe(true)
})
it('a user row with unknown text is a competing view', () => {
@@ -235,6 +245,6 @@ describe('surplusIsCompetingView', () => {
competingViewAssistantMessage('msg-12', '⌛ Approval timed out', 12)
]
- expect(surplusIsCompetingView(local, refreshed)).toBe(false)
+ expect(surplusIsCompetingView(local, refreshed, { optimisticMessageId: 'opt-old' })).toBe(false)
})
})
diff --git a/apps/desktop/src/lib/stale-transcript-guard.ts b/apps/desktop/src/lib/stale-transcript-guard.ts
index c53bdd67bf5..9d56dac447d 100644
--- a/apps/desktop/src/lib/stale-transcript-guard.ts
+++ b/apps/desktop/src/lib/stale-transcript-guard.ts
@@ -35,28 +35,41 @@ export interface TranscriptRefresh {
/**
* Whether the refreshed transcript carries a user row this window does not know.
- * A user row is known when its durable id, stored row, or text matches a local
- * one — the optimistic copy of a message this window typed keeps a local id (and
- * often no rowId) while the server's copy carries the persisted ids, so text is
- * what reconciles them. Only an unaccounted user row is another view's message;
- * tool/assistant-only surplus rows are this window's own turn residue (#124005).
+ * Durable ids and row ids are authoritative. The only text match allowed is the
+ * current optimistic prompt identified by `optimisticMessageId`; matching all
+ * user text would mistake a repeated prompt from another window for this
+ * window's own turn. Assistant/tool-only surplus rows are this window's own
+ * turn residue (#124005).
*/
-export function surplusIsCompetingView(localMessages: ChatMessage[], refreshed: ChatMessage[]): boolean {
- const localIds = new Set(localMessages.map(message => message.id))
- const localRowIds = new Set(
- localMessages.flatMap(message => (message.rowId === undefined ? [] : [message.rowId]))
- )
- const localUserTexts = new Set(
- localMessages.filter(message => message.role === 'user').map(chatMessageText)
- )
-
- return refreshed.some(
- message =>
- message.role === 'user' &&
- !localIds.has(message.id) &&
- (message.rowId === undefined || !localRowIds.has(message.rowId)) &&
- !localUserTexts.has(chatMessageText(message))
- )
+export function surplusIsCompetingView(
+ localMessages: ChatMessage[],
+ refreshed: ChatMessage[],
+ options?: { optimisticMessageId?: string }
+): boolean {
+ const localUsers = localMessages.filter(message => message.role === 'user')
+ const matchedLocalUsers = new Set<number>()
+
+ return refreshed.some(message => {
+ if (message.role !== 'user') {
+ return false
+ }
+
+ const matchIndex = localUsers.findIndex(
+ (local, index) =>
+ !matchedLocalUsers.has(index) &&
+ (local.id === message.id ||
+ (message.rowId !== undefined && local.rowId === message.rowId) ||
+ (local.id === options?.optimisticMessageId && local.rowId === undefined &&
+ chatMessageText(local) === chatMessageText(message)))
+ )
+
+ if (matchIndex < 0) {
+ return true
+ }
+
+ matchedLocalUsers.add(matchIndex)
+ return false
+ })
}
/**
@@ -209,5 +222,10 @@ export async function transcriptRefreshIfBehind(
return null
}
- return { messages, competingView: surplusIsCompetingView(localMessages, messages) }
+ return {
+ messages,
+ competingView: surplusIsCompetingView(localMessages, messages, {
+ optimisticMessageId: options?.excludeMessageId
+ })
+ }
}