Skip to content

The agent that grows with you

25.9 KB
Raw Blame
import { useEffect } from 'react'

import {
  extendRefreshPageToOverlap,
  graftRefreshedTailOntoBackfill,
  olderPageReader
} from '@/app/chat/transcript-backfill'
import {
  fetchStoredTranscriptAcrossBackends,
  getLatestSessionMessages,
  getSession,
  PROMPT_SUBMIT_REQUEST_TIMEOUT_MS
} from '@/hermes'
import { translateNow } from '@/i18n/runtime'
import { type ChatMessage, chatMessageText, toChatMessages } from '@/lib/chat-messages'
import { markReasoningEffortPending } from '@/lib/chat-runtime'
import { type GuardedModelSwitchResult, surfaceModelSwitchConfirm } from '@/lib/guarded-model-switch'
import { noteMessageSent } from '@/store/desktop-metrics'
import { notify } from '@/store/notifications'
import {
  isReadOnlyRuntimeId,
  isStoredTranscriptReadOnly,
  readOnlyRuntimeIdFor,
  resumeWithStoredTranscriptFallback
} from '@/store/read-only-transcript'
import { knownSessionOwner, ownerLookupSessionRows } from '@/store/session'
import { assertSessionOwnerResolved } from '@/store/session-owner-resolution'
import {
  profileScopeForSessionOwner,
  requestForSessionProfile,
  type SessionOwnerScope
} from '@/store/session-request-router'
import {
  $sessionTiles,
  publishSessionState,
  sessionTileOwnerRoute,
  setSessionTileDelegate
} from '@/store/session-states'
import type { SessionResumeResult } from '@/types/hermes'

import { refreshCronRunWriteGate } from '../../cron/open-cron-run'
import type { usePromptActions } from '../../session/hooks/use-prompt-actions'
import { singleFlightSessionResume } from '../../session/hooks/use-prompt-actions/single-flight-resume'
import { markSessionRecentlyInterrupted, withSessionNotFoundResume } from '../../session/hooks/use-prompt-actions/utils'
import type { useSessionActions } from '../../session/hooks/use-session-actions'
import {
  chatMessageArraysEquivalent,
  overlayConcurrentMessageChanges,
  preserveLocalPendingTurnMessages,
  reconcileResumeMessages,
  resolveResumedBusy,
  resolveSessionOwner
} from '../../session/hooks/use-session-actions/utils'
import type { useSessionStateCache } from '../../session/hooks/use-session-state-cache'
import type { ClientSessionState } from '../../types'
import type { GatewayRequester } from '../types'

type SessionStateCache = ReturnType<typeof useSessionStateCache>

/** Runtime a stored session is bound to: the primary reverse lookup, else a retained tile. */
function boundTileRuntimeId(
  runtimeIdByStoredSessionIdRef: SessionStateCache['runtimeIdByStoredSessionIdRef'],
  storedSessionId: string
): string | undefined {
  return (
    runtimeIdByStoredSessionIdRef.current.get(storedSessionId) ??
    $sessionTiles.get().find(tile => tile.storedSessionId === storedSessionId)?.runtimeId
  )
}

/** REST read scope for a tile's resolved owner (connection-scoped owners read their target profile). */
function tileRestScope(owner: SessionOwnerScope) {
  return owner && typeof owner === 'object'
    ? { connectionId: owner.connectionId, profile: owner.targetProfile || owner.profile }
    : owner
}

/** A cached binding still carrying this session's transcript (or mid-turn). */
function isWarmTileState(
  cached: ClientSessionState | undefined,
  storedSessionId: string
): cached is ClientSessionState {
  return cached?.storedSessionId === storedSessionId && (cached.busy || cached.messages.length > 0)
}

function mergeTileTranscript(
  previous: ChatMessage[],
  prefetched: ChatMessage[],
  streamId?: null | string
): ChatMessage[] {
  if (!prefetched.length) {
    return previous
  }

  const persisted = graftRefreshedTailOntoBackfill(prefetched, previous)

  // The known stream belongs to this turn even when its text repeats an older
  // answer; the generic reconnect reconciler only has text/ordinal heuristics.
  const stream = previous.find(message => message.id === streamId)

  const merged = preserveLocalPendingTurnMessages(
    reconcileResumeMessages(persisted, previous),
    stream ? previous.filter(message => message !== stream) : previous
  )

  if (!stream) {
    return merged
  }

  // Compaction shifts global assistant ordinals. Anchor this turn at its user
  // row instead; an older answer sharing the stream's prefix is not a match.
  const beforeStream = previous.slice(0, previous.indexOf(stream))
  const user = beforeStream.findLast(message => message.role === 'user')

  let anchor = user
    ? persisted.findIndex(
        message => message.id === user.id || (user.rowId !== undefined && message.rowId === user.rowId)
      )
    : -1

  if (user && anchor < 0) {
    const matches = persisted.filter(
      message => message.role === 'user' && chatMessageText(message) === chatMessageText(user)
    )

    anchor = matches.length === 1 ? persisted.indexOf(matches[0]) : -1
  }

  const turn = anchor < 0 ? [] : persisted.slice(anchor + 1)
  const nextUser = turn.findIndex(message => message.role === 'user')

  const ordinal = user
    ? beforeStream.slice(beforeStream.indexOf(user) + 1).filter(message => message.role === 'assistant').length
    : 0

  const counterpart =
    persisted.find(
      message => message.id === stream.id || (stream.rowId !== undefined && message.rowId === stream.rowId)
    ) ?? (nextUser < 0 ? turn : turn.slice(0, nextUser)).filter(message => message.role === 'assistant')[ordinal]

  const localText = chatMessageText(stream)
  const storedText = counterpart ? chatMessageText(counterpart) : ''

  if (!counterpart || !(storedText.startsWith(localText) || localText.startsWith(storedText))) {
    return [...merged, stream]
  }

  // Keep the stream id for subsequent deltas, but use REST's fuller answer.
  const reply =
    storedText.length > localText.length
      ? { ...reconcileResumeMessages([counterpart], [stream])[0], id: stream.id }
      : stream

  return merged.map((message, index) => (index === persisted.indexOf(counterpart) ? reply : message))
}

interface SessionTileDelegateParams {
  archiveSession: (storedSessionId: string) => Promise<unknown>
  branchLoadedSession: ReturnType<typeof useSessionActions>['branchLoadedSession']
  branchStoredSession: (storedSessionId: string) => Promise<unknown>
  executeSlashCommand: ReturnType<typeof usePromptActions>['executeSlashCommand']
  removeSession: (storedSessionId: string) => Promise<unknown>
  requestGateway: GatewayRequester
  runtimeIdByStoredSessionIdRef: SessionStateCache['runtimeIdByStoredSessionIdRef']
  sessionStateByRuntimeIdRef: SessionStateCache['sessionStateByRuntimeIdRef']
  updateSessionState: SessionStateCache['updateSessionState']
}

/**
 * Publishes the session-tile delegate: resume / submit / interrupt / slash for
 * tiled sessions WITHOUT touching the primary view ($activeSessionId /
 * $messages stay the main thread's). Resume reuses a live runtime binding when
 * one exists (incl. the main thread's own session); a cold tile binds +
 * hydrates the cache, which publishSessionState mirrors to the tile.
 */
export function useSessionTileDelegate({
  archiveSession,
  branchLoadedSession,
  branchStoredSession,
  executeSlashCommand,
  removeSession,
  requestGateway,
  runtimeIdByStoredSessionIdRef,
  sessionStateByRuntimeIdRef,
  updateSessionState
}: SessionTileDelegateParams): void {
  useEffect(() => {
    // A tile's runtime binding can die the same way the foreground's does
    // (sleep/wake, backend restart). The cache maps stored -> runtime, so walk
    // it backwards to find the durable id this runtime belongs to.
    const storedSessionIdForRuntime = (runtimeId: string): null | string => {
      const cached = sessionStateByRuntimeIdRef.current.get(runtimeId)?.storedSessionId

      if (cached) {
        return cached
      }

      for (const [storedId, mapped] of runtimeIdByStoredSessionIdRef.current) {
        if (mapped === runtimeId) {
          return storedId
        }
      }

      return null
    }

    // Repoint the stored -> runtime mapping at the recovered id so subsequent
    // tile actions use the live binding instead of re-recovering every call.
    const rebindTileRuntime = (deadRuntimeId: string) => (recoveredId: string) => {
      const storedId = storedSessionIdForRuntime(deadRuntimeId)

      if (storedId) {
        runtimeIdByStoredSessionIdRef.current.set(storedId, recoveredId)
      }
    }

    // Same ladder as the window's session-RPC dispatcher: tile route → the
    // row's owner (exact when connection-tagged, else the hint / profile) →
    // the async cross-profile probe (exact when the resolved row is tagged).
    const ownerForStoredSession = async (storedSessionId: string): Promise<SessionOwnerScope> => {
      const owner =
        sessionTileOwnerRoute(storedSessionId) ??
        knownSessionOwner(ownerLookupSessionRows(), storedSessionId) ??
        (await resolveSessionOwner(storedSessionId))

      return owner
    }

    const requestForStoredSession = async <T>(
      storedSessionId: string,
      method: string,
      params: Record<string, unknown>,
      timeoutMs?: number
    ): Promise<T> => {
      const owner = await ownerForStoredSession(storedSessionId)

      return requestForSessionProfile<T>(owner, requestGateway, method, params, timeoutMs)
    }

    setSessionTileDelegate({
      archiveSession: async storedSessionId => {
        await archiveSession(storedSessionId)
      },
      branchSession: async storedSessionId => {
        await branchStoredSession(storedSessionId)
      },
      branchSessionAtMessage: async (storedSessionId, runtimeId, messageId) => {
        const state = sessionStateByRuntimeIdRef.current.get(runtimeId)

        // The tile can disappear between render and click. Without its live
        // state we cannot prove the busy flag or the transcript boundary, so
        // do not manufacture an empty, apparently-idle branch.
        if (!state) {
          return false
        }

        return await branchLoadedSession({
          busy: state.busy,
          cwd: state.cwd,
          messageId,
          messages: state.messages,
          runtimeId,
          storedSessionId
        })
      },
      deleteSession: async storedSessionId => {
        await removeSession(storedSessionId)
      },
      executeSlash: async (rawCommand, sessionId, options) => {
        await executeSlashCommand(rawCommand, { sessionId, ...options })
      },
      // The picker's wire (config.set model), always --session (never the profile default), with a
      // guarded target asked through the ONE shared confirm flow and awaited: true only once the switch
      // applied (or is queued for the next turn start), so a caller can safely re-run a turn on it;
      // false when the user kept the current model. A refused switch throws.
      switchSessionModel: async (runtimeId, model, provider) => {
        const storedSessionId = storedSessionIdForRuntime(runtimeId)

        const request = (confirmExpensiveModel: boolean) => {
          const params = {
            key: 'model',
            session_id: runtimeId,
            value: `${model} --provider ${provider} --session`,
            ...(confirmExpensiveModel ? { confirm_expensive_model: true } : {})
          }

          return storedSessionId
            ? requestForStoredSession<GuardedModelSwitchResult>(storedSessionId, 'config.set', params)
            : requestGateway<GuardedModelSwitchResult>('config.set', params)
        }

        const result = await request(false)

        if (!result?.confirm_required) {
          return true
        }

        return surfaceModelSwitchConfirm({
          confirmMessage: result.confirm_message,
          failureMessage: translateNow('desktop.modelSwitchFailed'),
          model,
          requestConfirmed: () => request(true)
        })
      },
      // Gateway reconnect (sleep/wake, backend respawn): every stored→runtime
      // binding recorded pre-reconnect points at a runtime id the respawned
      // backend no longer knows. Drop the map so resumeTile's warm path can't
      // re-bind a tile to a dead runtime; live bindings re-record from
      // post-reconnect events and fresh resumes. A binding whose runtime id IS
      // the durable id (the canonical gateway's) cannot be re-minted or
      // recycled into another chat, and the client re-attaches it on its next
      // request; dropping it made the next send re-resume the open chat, which
      // blanked its status stack and transcript until the resume answered.
      invalidateRuntimeBindings: preserveStoredSessionIds => {
        for (const [storedSessionId, runtimeId] of runtimeIdByStoredSessionIdRef.current) {
          if (runtimeId !== storedSessionId && !preserveStoredSessionIds?.has(storedSessionId)) {
            runtimeIdByStoredSessionIdRef.current.delete(storedSessionId)
          }
        }
      },
      dropRuntimeBindings: storedSessionIds => {
        for (const storedSessionId of storedSessionIds) {
          runtimeIdByStoredSessionIdRef.current.delete(storedSessionId)
        }
      },
      // Reconnect reconcile (#93059): retire an orphaned runtime's busy claim
      // through updateSessionState so the cache, focused view, busyRef and
      // tile mirrors settle together. A runtime this cache never held reports
      // false instead of minting an entry; the store downgrades its mirror.
      retireBusyClaim: runtimeId => {
        const cached = sessionStateByRuntimeIdRef.current.get(runtimeId)

        if (!cached || (!cached.busy && !cached.awaitingResponse)) {
          return false
        }

        updateSessionState(runtimeId, state => ({
          ...state,
          awaitingResponse: false,
          busy: false,
          turnLive: false,
          turnStartedAt: null
        }))

        return true
      },
      updateHeldSession: (runtimeId, updater) => {
        if (!sessionStateByRuntimeIdRef.current.has(runtimeId)) {
          return false
        }

        updateSessionState(runtimeId, updater)

        return true
      },
      interruptSession: async runtimeId => {
        // Read-only stored-transcript tiles have no live turn to interrupt.
        if (isReadOnlyRuntimeId(runtimeId)) {
          return
        }

        // Same cooldown as the primary chat's Stop (#83855): the gateway may
        // still be winding down after this interrupt, so a quick edit/resend
        // on the tile must go interrupt-first even though busy already reads
        // false. Mark the runtime id (and any recovered id) before the RPC so
        // the window covers the whole wind-down.
        markSessionRecentlyInterrupted(runtimeId)

        const storedSessionId = storedSessionIdForRuntime(runtimeId)

        const routedRequest = storedSessionId
          ? <T>(method: string, params?: Record<string, unknown>, timeoutMs?: number) =>
              requestForStoredSession<T>(storedSessionId, method, params ?? {}, timeoutMs)
          : requestGateway

        await withSessionNotFoundResume(
          runtimeId,
          storedSessionId,
          liveId => routedRequest('session.interrupt', { session_id: liveId }),
          {
            requestGateway: routedRequest,
            onRecovered: recoveredId => {
              markSessionRecentlyInterrupted(recoveredId)
              rebindTileRuntime(runtimeId)(recoveredId)
            }
          }
        )
      },
      resumeTile: async (storedSessionId, options) => {
        // A retained tile can still own its runtime after the primary view drops
        // its reverse lookup. Reconnect invalidates both bindings.
        const existing = boundTileRuntimeId(runtimeIdByStoredSessionIdRef, storedSessionId)

        const cached = existing ? sessionStateByRuntimeIdRef.current.get(existing) : undefined
        const resumeRequestBaselineMessages = cached?.messages ?? []
        const refreshTranscript = options?.refreshTranscript === true
        const authoritativeSnapshot = options?.authoritativeSnapshot === true

        // Warm path: reuse a live binding — but only when it still carries a
        // transcript (or is mid-turn, where messages legitimately stream in).
        // A binding whose cached state has no messages is either a released
        // transcript or a stale pre-reconnect survivor; reusing it painted the
        // post-sleep/wake tile permanently empty. Fall through to a real
        // resume instead — it's idempotent for a genuinely live session.
        //
        // Explicit reopen (`refreshTranscript`) must still REST-merge: the
        // warm snapshot is whatever the tile last painted, and cron bot-chat
        // deliveries that landed while the panel's WS was down never arrive
        // as realtime events (#96183).
        if (existing && isWarmTileState(cached, storedSessionId) && !refreshTranscript && !authoritativeSnapshot) {
          publishSessionState(existing, cached)

          return existing
        }

        // Resolve the owning profile before binding a runtime. A tile can open a
        // session from any profile, not just the active one; resuming (or
        // reading messages) without a profile lets the gateway fall back to the
        // launch-profile DB and fork the conversation into the wrong profile —
        // the same cross-profile bleed the recovery resumes had (#67603).
        const owner = await ownerForStoredSession(storedSessionId)

        const restScope = tileRestScope(owner)

        const prefetchPromise = authoritativeSnapshot
          ? Promise.resolve(null)
          : getLatestSessionMessages(storedSessionId, restScope).catch(() => null)

        if (!authoritativeSnapshot && existing && isWarmTileState(cached, storedSessionId)) {
          const prefetch = await prefetchPromise

          // A long turn can push every rendered row off the newest page; read
          // older pages until they overlap so the graft keeps earlier history.
          const prefetched = await extendRefreshPageToOverlap(
            toChatMessages(prefetch?.messages ?? []),
            cached.messages,
            olderPageReader(storedSessionId, restScope, prefetch)
          )

          // The overlap reads await; drop the page if the tile was rebound.
          if (sessionStateByRuntimeIdRef.current.get(existing)?.storedSessionId !== storedSessionId) {
            return existing
          }

          // Deltas and completion may land while REST is in flight.
          updateSessionState(
            existing,
            state => {
              const merged = mergeTileTranscript(state.messages, prefetched, state.streamId ?? cached.streamId)

              return chatMessageArraysEquivalent(state.messages, merged) ? state : { ...state, messages: merged }
            },
            storedSessionId
          )

          return existing
        }

        // #94724 no-owner recovery: dispatching the resume through the same
        // fail-closed gate as the window's RPC dispatcher keeps an unknown
        // owner off the ambient socket, and the wrapper opens the stored
        // transcript read-only instead of dead-ending the tile — the id-only
        // REST read routes no live session at all.
        const outcome = await resumeWithStoredTranscriptFallback(
          storedSessionId,
          () => {
            assertSessionOwnerResolved(owner, { method: 'session.resume', sessionId: storedSessionId })

            return singleFlightSessionResume(
              storedSessionId,
              () =>
                requestForSessionProfile<SessionResumeResult>(owner, requestGateway, 'session.resume', {
                  session_id: storedSessionId,
                  cols: 96,
                  omit_messages: !authoritativeSnapshot,
                  ...(owner ? { profile: typeof owner === 'string' ? owner : owner.profile } : {})
                }),
              { requiresMessages: authoritativeSnapshot, scope: owner }
            )
          },
          async () => {
            const stored =
              (await prefetchPromise) ??
              (await getLatestSessionMessages(storedSessionId, restScope).catch(() => null)) ??
              (await fetchStoredTranscriptAcrossBackends(storedSessionId))

            if (!stored) {
              throw new Error('stored transcript unavailable on every reachable backend')
            }

            return stored
          }
        )

        const prefetch = await prefetchPromise

        if (outcome.mode === 'read-only') {
          const readOnlyId = readOnlyRuntimeIdFor(storedSessionId)

          updateSessionState(
            readOnlyId,
            state => ({
              ...state,
              busy: false,
              awaitingResponse: false,
              messages: state.messages.length > 0 ? state.messages : toChatMessages(outcome.transcript?.messages ?? [])
            }),
            storedSessionId
          )

          notify({
            kind: 'info',
            title: translateNow('desktop.readOnlyTranscriptTitle'),
            message: translateNow('desktop.readOnlyTranscriptBody')
          })

          return readOnlyId
        }

        const resumed = outcome.resumed

        const runtimeId = resumed?.session_id

        if (!runtimeId) {
          throw new Error('resume returned no session id')
        }

        const currentBinding = boundTileRuntimeId(runtimeIdByStoredSessionIdRef, storedSessionId)

        // Another resume/rebind won while this request was in flight. Do not
        // publish the older response into the runtime the tile now owns.
        if (currentBinding && currentBinding !== existing && currentBinding !== runtimeId) {
          return currentBinding
        }

        const info = resumed?.info

        updateSessionState(
          runtimeId,
          state => {
            const previousMessages = state.messages.length > 0 ? state.messages : resumeRequestBaselineMessages

            const messages = authoritativeSnapshot
              ? resumed.messages.length > 0
                ? overlayConcurrentMessageChanges(
                    mergeTileTranscript(
                      resumeRequestBaselineMessages,
                      toChatMessages(resumed.messages),
                      cached?.streamId
                    ),
                    resumeRequestBaselineMessages,
                    previousMessages
                  )
                : previousMessages
              : previousMessages.length > 0
                ? previousMessages
                : toChatMessages(prefetch?.messages ?? resumed.messages ?? [])

            const busyChangedWhileResuming = cached
              ? Boolean(
                  state.busy && (state.turnStartedAt !== cached.turnStartedAt || (state.turnLive && !cached.turnLive))
                )
              : state.busy

            const running = resolveResumedBusy(resumed.running ?? info?.running, busyChangedWhileResuming)

            return {
              // The deferred build reports the session's own effort later (#79807).
              ...markReasoningEffortPending(state),
              ...(typeof info?.fast === 'boolean' ? { fast: info.fast } : {}),
              ...(typeof info?.model === 'string' ? { model: info.model } : {}),
              ...(typeof info?.provider === 'string' ? { provider: info.provider } : {}),
              ...(typeof info?.reasoning_effort === 'string'
                ? { reasoningEffort: info.reasoning_effort, reasoningEffortPending: false }
                : {}),
              ...(typeof info?.reasoning_effort_wire === 'string'
                ? { reasoningEffortWire: info.reasoning_effort_wire }
                : {}),
              awaitingResponse: running && !resumed.inflight?.assistant,
              busy: running,
              messages
            }
          },
          storedSessionId
        )

        return runtimeId
      },
      submitToSession: async (runtimeId, text) => {
        const storedSessionId = storedSessionIdForRuntime(runtimeId)

        // A cron run's write gate is re-evaluated against its authoritative
        // row before the send (#88443) — the same gate as the primary chat's
        // `submit`, so a verdict never latches and a restored tile is gated.
        if (storedSessionId && !isReadOnlyRuntimeId(runtimeId)) {
          await refreshCronRunWriteGate(storedSessionId, id =>
            ownerForStoredSession(id).then(owner => getSession(id, profileScopeForSessionOwner(owner)))
          )
        }

        // A read-only stored-transcript tile has no live runtime to submit
        // into (#94724), and a never-closed cron run the scheduler no longer
        // owns (#88443) stays closed to writes when it is opened as a tile.
        // Refuse with the explanation instead of minting a misrouted prompt.
        if (isReadOnlyRuntimeId(runtimeId) || isStoredTranscriptReadOnly(storedSessionId)) {
          notify({ kind: 'info', message: translateNow('desktop.readOnlyTranscriptSendBlocked') })

          return { runtimeSessionId: runtimeId, storedSessionId: null }
        }

        const routedRequest = storedSessionId
          ? <T>(method: string, params?: Record<string, unknown>, timeoutMs?: number) =>
              requestForStoredSession<T>(storedSessionId, method, params ?? {}, timeoutMs)
          : requestGateway

        noteMessageSent($sessionTiles.get().find(tile => tile.runtimeId === runtimeId)?.workspaceMode ?? 'sessions')
        let acceptedRuntimeId = runtimeId

        await withSessionNotFoundResume(
          runtimeId,
          storedSessionId,
          liveId => routedRequest('prompt.submit', { session_id: liveId, text }, PROMPT_SUBMIT_REQUEST_TIMEOUT_MS),
          {
            requestGateway: routedRequest,
            onRecovered: recoveredId => {
              acceptedRuntimeId = recoveredId
              rebindTileRuntime(runtimeId)(recoveredId)
            }
          }
        )

        return {
          runtimeSessionId: acceptedRuntimeId,
          // The durable binding for the accepted runtime — the only proof the
          // requested stored session is what actually took the prompt.
          storedSessionId: storedSessionIdForRuntime(acceptedRuntimeId) ?? null
        }
      },
      updateSession: (runtimeId, updater) => updateSessionState(runtimeId, updater)
    })
  }, [
    archiveSession,
    branchLoadedSession,
    branchStoredSession,
    executeSlashCommand,
    removeSession,
    requestGateway,
    runtimeIdByStoredSessionIdRef,
    sessionStateByRuntimeIdRef,
    updateSessionState
  ])
}