Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion apps/desktop/src/app/session/hooks/use-prompt-actions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import type { MutableRefObject } from 'react'
import { useEffect } from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import type { ChatMessage } from '@/lib/chat-messages'
import { $sessions, setSessions } from '@/store/session'
import type { SessionInfo } from '@/types/hermes'

import { usePromptActions } from './use-prompt-actions'
import { usePromptActions, visibleUserOrdinal } from './use-prompt-actions'

vi.mock('@/hermes', () => ({
getProfiles: vi.fn(async () => ({ profiles: [] })),
Expand Down Expand Up @@ -314,3 +315,34 @@ describe('usePromptActions steerPrompt', () => {
expect(requestGateway).not.toHaveBeenCalled()
})
})

describe('visibleUserOrdinal', () => {
const user = (id: string): ChatMessage => ({ id, role: 'user', parts: [] })
const assistant = (id: string): ChatMessage => ({ id, role: 'assistant', parts: [] })
const failed = (id: string): ChatMessage => ({ id, role: 'assistant', parts: [], error: 'provider down' })

it('counts the visible user turns before the cut', () => {
const messages = [user('u1'), assistant('a1'), user('u2'), assistant('a2')]

// Two prior user turns persisted before the slice end.
expect(visibleUserOrdinal(messages, 2)).toBe(1)
expect(visibleUserOrdinal(messages, 4)).toBe(2)
})

it('skips a failed user turn so the ordinal matches the backend persisted index', () => {
// u1's prompt.submit failed (assistant error, never persisted), then u2/u3
// succeeded. The gateway only counts u2 before u3, so regenerating u3 must
// resolve to ordinal 1 — not 2 — or the backend rejects with error 4018.
const messages = [user('u1'), failed('a1'), user('u2'), assistant('a2'), user('u3'), assistant('a3')]

const u3Index = messages.findIndex(m => m.id === 'u3')

expect(visibleUserOrdinal(messages, u3Index)).toBe(1)
})

it('ignores hidden (branched-away) user turns', () => {
const messages: ChatMessage[] = [{ ...user('u1'), hidden: true }, assistant('a1'), user('u2'), assistant('a2')]

expect(visibleUserOrdinal(messages, 4)).toBe(1)
})
})
31 changes: 29 additions & 2 deletions apps/desktop/src/app/session/hooks/use-prompt-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,35 @@ function appendText(message: AppendMessage): string {
.trim()
}

function visibleUserOrdinal(messages: readonly ChatMessage[], end: number): number {
return messages.slice(0, end).filter(m => m.role === 'user' && !m.hidden).length
// Count the user turns the *backend* persisted before `end`, which is what
// `truncate_before_user_ordinal` indexes against. A user turn whose
// prompt.submit failed keeps its optimistic bubble (submitPromptText's catch
// appends an assistant error and leaves the user message in place) but never
// reaches backend history. The gateway only counts persisted user turns, so a
// failed turn must be skipped here too — otherwise every later ordinal
// overshoots the backend index and prompt.submit rejects with error 4018
// ("target user message is no longer in session history"), silently breaking
// regenerate for the rest of the session.
export function visibleUserOrdinal(messages: readonly ChatMessage[], end: number): number {
let ordinal = 0

for (let index = 0; index < end; index += 1) {
const message = messages[index]

if (message.role !== 'user' || message.hidden) {
continue
}

const next = messages[index + 1]

if (next?.role === 'assistant' && Boolean(next.error)) {
continue
}

ordinal += 1
}

return ordinal
}

export function usePromptActions({
Expand Down