Skip to content

The agent that grows with you

fix(desktop): skip failed turns in the backend-facing user ordinal space

vondelomloc2a50a86620c
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/utils.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/utils.ts
index 88880fdc8c8..459c5a15db7 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/utils.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/utils.ts
@@ -603,28 +603,45 @@ export function isVisibleUserMessage(message: ChatMessage): boolean {
return message.role === 'user' && !message.hidden
}
-export function visibleUserOrdinal(messages: readonly ChatMessage[], end: number): number {
- return messages.slice(0, end).filter(isVisibleUserMessage).length
+/**
+ * A user turn whose submit failed: the optimistic bubble stayed in the
+ * transcript (followed by an assistant error), but the turn never reached the
+ * gateway, so it does not exist in backend history. Every backend-facing
+ * user-turn count must skip these or every later ordinal overshoots the
+ * gateway's index and the rewind mis-aims / gets refused (#41275, #86573).
+ */
+export function isFailedUserTurn(messages: readonly ChatMessage[], index: number): boolean {
+ const next = messages[index + 1]
+
+ return next?.role === 'assistant' && Boolean(next.error)
}
-export function visibleUserIndexAtOrdinal(messages: readonly ChatMessage[], targetOrdinal: number): number {
- let ordinal = 0
+/**
+ * Indices of the user turns the backend also knows about — visible AND not
+ * failed. This is the ONE ordinal space shared with the gateway: truncate
+ * ordinals, ordinal→index resolution, survivor-rowId rebinding, and durable
+ * row-id resolution all iterate exactly this list.
+ */
+export function visibleUserMessageIndices(messages: readonly ChatMessage[]): number[] {
+ const indices: number[] = []
for (let index = 0; index < messages.length; index += 1) {
- const message = messages[index]
-
- if (!isVisibleUserMessage(message)) {
- continue
+ if (isVisibleUserMessage(messages[index]) && !isFailedUserTurn(messages, index)) {
+ indices.push(index)
}
+ }
- if (ordinal === targetOrdinal) {
- return index
- }
+ return indices
+}
- ordinal += 1
- }
+export function visibleUserOrdinal(messages: readonly ChatMessage[], end: number): number {
+ return visibleUserMessageIndices(messages).filter(index => index < end).length
+}
+
+export function visibleUserIndexAtOrdinal(messages: readonly ChatMessage[], targetOrdinal: number): number {
+ const indices = visibleUserMessageIndices(messages)
- return -1
+ return targetOrdinal >= 0 && targetOrdinal < indices.length ? indices[targetOrdinal] : -1
}
export interface SubmitTextOptions {