Skip to content

The agent that grows with you

6.5 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import { buildToolView } from '@/components/assistant-ui/tool/fallback-model'
import { createClientSessionState } from '@/lib/chat-runtime'
import { $activeSessionId, $selectedStoredSessionId, $unreadFinishedSessionIds } from '@/store/session'
import {
  $attentionSessionIds,
  $sessionStates,
  $workingSessionIds,
  clearAllSessionStates,
  publishSessionState,
  reconcileBusyStatesOnReconnect
} from '@/store/session-states'
import { isMessagingCompletion } from '@/store/session-unread'

import { rehydrateLiveSessionStatuses } from './use-background-sync'

/**
 * `session.active_list` is the authoritative snapshot of what is RUNNING in the
 * polled gateway process. A session that finished while Desktop was looking
 * elsewhere — or whose runtime id was recycled by a backend respawn — simply
 * stops appearing in the response. Absence is therefore a completion signal,
 * not "no news": if nothing reaps it, the row spins forever and the
 * busy→idle edge that paints the green "your turn" dot never fires.
 */
describe('rehydrateLiveSessionStatuses — reaping vanished runtimes', () => {
  beforeEach(() => {
    vi.useFakeTimers()
    $selectedStoredSessionId.set(null)
    $unreadFinishedSessionIds.set([])
  })

  afterEach(() => {
    vi.clearAllTimers()
    vi.useRealTimers()
    clearAllSessionStates()
    $unreadFinishedSessionIds.set([])
    $activeSessionId.set(null)
  })

  it('clears a working session that disappears from the live snapshot', () => {
    rehydrateLiveSessionStatuses({
      sessions: [{ id: 'runtime-a', session_key: 'stored-a', status: 'working' }]
    })

    expect($workingSessionIds.get()).toEqual(['stored-a'])

    // The turn finished and the gateway reaped the session between polls.
    rehydrateLiveSessionStatuses({ sessions: [] })

    expect($workingSessionIds.get()).toEqual([])
  })

  it('fires the unread "your turn" marker for a vanished background session', () => {
    rehydrateLiveSessionStatuses({
      sessions: [{ id: 'runtime-b', session_key: 'stored-b', status: 'working' }]
    })

    rehydrateLiveSessionStatuses({ sessions: [] })

    expect($unreadFinishedSessionIds.get()).toEqual(['stored-b'])
  })

  it('stays dark for a messaging turn that settles before its messaging row loads', () => {
    rehydrateLiveSessionStatuses({
      sessions: [{ id: 'runtime-tg', session_key: 'stored-tg', source: 'telegram', status: 'working' }]
    })

    rehydrateLiveSessionStatuses({ sessions: [] })

    expect($unreadFinishedSessionIds.get()).toEqual([])
  })

  it("keeps another profile's same-id local completion lit when only this profile polled a messaging source", () => {
    rehydrateLiveSessionStatuses(
      { sessions: [{ id: 'runtime-alpha', session_key: 'shared', source: 'telegram', status: 'idle' }] },
      Date.now(),
      'alpha'
    )
    expect(isMessagingCompletion('shared', 'beta')).toBe(false)
    expect(isMessagingCompletion('shared', 'alpha')).toBe(true)
  })

  // A turn that started just before the socket dropped was never polled, so
  // "seen live last poll" cannot gate its confirmation: the reconcile parked it,
  // and the first fresh snapshot that does not report it working is the
  // terminal fact that lights the dot.
  it('confirms a parked reconnect completion the poll never saw live', () => {
    publishSessionState('runtime-p', {
      ...createClientSessionState('stored-p'),
      busy: true,
      storedSessionId: 'stored-p'
    })
    reconcileBusyStatesOnReconnect()
    expect($unreadFinishedSessionIds.get()).toEqual([])

    rehydrateLiveSessionStatuses({ sessions: [] })

    expect($unreadFinishedSessionIds.get()).toEqual(['stored-p'])
  })

  it('clears a blocked session that disappears from the live snapshot', () => {
    rehydrateLiveSessionStatuses({
      sessions: [{ id: 'runtime-c', session_key: 'stored-c', status: 'waiting' }]
    })

    expect($attentionSessionIds.get()).toEqual(['stored-c'])

    rehydrateLiveSessionStatuses({ sessions: [] })

    expect($attentionSessionIds.get()).toEqual([])
  })

  it('leaves runtimes this poll never seeded alone', () => {
    // A background PROFILE's sessions are served by a different gateway and
    // never appear in this profile's active_list. Reaping them would dark out
    // every other profile's running rows.
    rehydrateLiveSessionStatuses(
      { sessions: [{ id: 'runtime-other', session_key: 'stored-other', status: 'working' }] },
      Date.now(),
      'other'
    )

    rehydrateLiveSessionStatuses({ sessions: [] }, Date.now(), 'default')

    expect($workingSessionIds.get()).toEqual(['stored-other'])
  })

  it('seals open tool parts and clears awaitingResponse when a session vanishes', () => {
    const openTool = {
      type: 'tool-call',
      toolCallId: 'call-1',
      toolName: 'patch',
      args: {},
      argsText: '{}'
    } as never

    publishSessionState('runtime-tools', {
      ...createClientSessionState('stored-tools'),
      busy: true,
      awaitingResponse: true,
      messages: [{ id: 'a1', role: 'assistant', parts: [openTool], pending: false } as never]
    })

    // Keep the runtime referenced so the settled state stays in the store
    // instead of being evicted as no-longer-needed.
    $activeSessionId.set('runtime-tools')

    rehydrateLiveSessionStatuses({
      sessions: [{ id: 'runtime-tools', session_key: 'stored-tools', status: 'working' }]
    })
    rehydrateLiveSessionStatuses({ sessions: [] })

    const state = $sessionStates.get()['runtime-tools']
    const part = state.messages[0].parts[0]

    expect(state.busy).toBe(false)
    expect(state.awaitingResponse).toBe(false)
    expect(part.type).toBe('tool-call')

    if (part.type !== 'tool-call') {
      throw new Error('Missing tool call')
    }

    // Reaping ends liveness without inventing evidence of a successful result.
    expect(part.completedAt).toBeDefined()
    expect(part.result).toBeUndefined()
    expect(buildToolView(part, '').status).toBe('warning')
  })

  it('clears a session stuck awaiting a response without the busy flag', () => {
    publishSessionState('runtime-await', {
      ...createClientSessionState('stored-await'),
      awaitingResponse: true,
      busy: false
    })

    $activeSessionId.set('runtime-await')

    rehydrateLiveSessionStatuses({
      sessions: [{ id: 'runtime-await', session_key: 'stored-await', status: 'working' }]
    })
    rehydrateLiveSessionStatuses({ sessions: [] })

    expect($sessionStates.get()['runtime-await'].awaitingResponse).toBe(false)
  })
})