Skip to content

The agent that grows with you

37.3 KB
Raw Blame
import {
  type ChatMessage,
  type ChatMessagePart,
  chatMessageText,
  normalizeWs as normalizedText
} from '@/lib/chat-messages'
import { withoutCoveredAssistantPrefix } from '@/lib/chat-messages/coverage'
import { isLiveTailReplyId } from '@/lib/spoken-reply'

/**
 * Crash-survivable in-flight turn journal.
 *
 * While a session is busy, the visible tail of the running turn (user prompt +
 * streamed assistant rows, tool calls included) is persisted to localStorage.
 * If the renderer or the whole app dies mid-turn, session resume folds the
 * journaled tail back onto the restored transcript, so streamed progress is
 * not silently lost. The backend's own `inflight` snapshot (merged by
 * `appendLiveSessionProjection`) covers reconnects while the backend is alive;
 * this journal covers the cases where the backend died too — and it is richer,
 * because the backend snapshot carries text only while the journal keeps the
 * full part structure.
 *
 * Best-effort by design: storage failures must never break chat streaming.
 */

const LEGACY_STORAGE_KEY = 'hermes.desktop.inflightTurnJournal.v1'
const STORAGE_PREFIX = 'hermes.desktop.inflightTurnJournal.v2:'
const LEGACY_MIGRATION_KEY = 'hermes.desktop.inflightTurnJournal.v2.migrated'
const DISCARDED_SNAPSHOT_RAW = '0'
const STORE_VERSION = 1
const MAX_SESSION_STORE_CHARS = 4 * 1024 * 1024
const MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000
// Keep the worst-case v2 namespace below a conservative localStorage budget
// while retaining the 24 newest session slots for ordinary small snapshots.
const MAX_ENTRY_CHARS = 160 * 1024
const MAX_ENTRIES = Math.min(24, Math.floor(MAX_SESSION_STORE_CHARS / MAX_ENTRY_CHARS))
const MAX_LEGACY_STORE_CHARS = 2 * 1024 * 1024
const MAX_SESSION_KEY_CHARS = 512
const MAX_JOURNALED_MESSAGES = 24
const MAX_TEXT_PART_CHARS = 64 * 1024
const MAX_METADATA_CHARS = 2 * 1024
const MAX_USER_ATTACHMENT_REFS = 256
const MAX_USER_ATTACHMENT_REF_CHARS = 64 * 1024
/** Streaming repaints arrive every ~33ms; localStorage writes are synchronous.
 *  Trailing-edge throttle keeps the journal off the hot path — a crash costs at
 *  most this much of the newest tail. */
const PERSIST_THROTTLE_MS = 400

// A renderer can accumulate one entry per session over its lifetime. Sweep the
// bounded v2 namespace once on first journal access; never scan it on the
// 400ms streaming write path.
let sessionStoreSwept = false

export interface InFlightTurnSnapshot {
  messages: ChatMessage[]
  streamId: null | string
  turnStartedAt: null | number
  updatedAt: number
}

export interface JournalableSessionState {
  awaitingResponse: boolean
  busy: boolean
  messages: ChatMessage[]
  storedSessionId: null | string
  streamId: null | string
  turnStartedAt: null | number
}

interface JournalStore {
  entries: Record<string, InFlightTurnSnapshot>
  version: typeof STORE_VERSION
}

export interface InFlightRecoveryResult {
  applied: boolean
  /** The base transcript already contains the journaled turn's completed
   *  reply — the journal entry is stale and has been cleared. */
  caughtUp: boolean
  messages: ChatMessage[]
  streamId: null | string
  turnStartedAt: null | number
}

function storage(): Storage | null {
  try {
    return typeof window === 'undefined' ? null : window.localStorage
  } catch {
    return null
  }
}

function sessionStorageKey(storedSessionId: string): null | string {
  try {
    const encoded = encodeURIComponent(storedSessionId)

    return encoded.length > 0 && encoded.length <= MAX_SESSION_KEY_CHARS ? `${STORAGE_PREFIX}${encoded}` : null
  } catch {
    return null
  }
}

function readRaw(store: Storage, key: string): null | string {
  try {
    return store.getItem(key)
  } catch {
    return null
  }
}

function removeRaw(store: Storage, key: string): void {
  try {
    store.removeItem(key)
  } catch {
    // Best-effort recovery state must not interrupt chat streaming.
  }
}

function writeRaw(store: Storage, key: string, value: string): boolean {
  try {
    store.setItem(key, value)

    return true
  } catch {
    return false
  }
}

function isSnapshot(value: unknown): value is InFlightTurnSnapshot {
  if (!value || typeof value !== 'object') {
    return false
  }

  const snapshot = value as Partial<InFlightTurnSnapshot>

  return (
    Array.isArray(snapshot.messages) &&
    snapshot.messages.every(
      message =>
        Boolean(message) &&
        typeof message === 'object' &&
        typeof message.id === 'string' &&
        ['assistant', 'system', 'tool', 'user'].includes(message.role) &&
        Array.isArray(message.parts) &&
        message.parts.every(
          part =>
            Boolean(part) &&
            typeof part === 'object' &&
            typeof part.type === 'string' &&
            (part.sourceRowId === undefined ||
              (typeof part.sourceRowId === 'number' && Number.isFinite(part.sourceRowId))) &&
            (part.type !== 'text' && part.type !== 'reasoning'
              ? part.type !== 'tool-call' ||
                (typeof part.toolName === 'string' &&
                  (part.toolCallId === undefined || typeof part.toolCallId === 'string') &&
                  (part.isError === undefined || typeof part.isError === 'boolean'))
              : typeof part.text === 'string' && (part.parentId === undefined || typeof part.parentId === 'string'))
        ) &&
        (message.timestamp === undefined ||
          (typeof message.timestamp === 'number' && Number.isFinite(message.timestamp))) &&
        (message.pending === undefined || typeof message.pending === 'boolean') &&
        (message.error === undefined || typeof message.error === 'string') &&
        (message.branchGroupId === undefined || typeof message.branchGroupId === 'string') &&
        (message.hidden === undefined || typeof message.hidden === 'boolean') &&
        (message.interim === undefined || typeof message.interim === 'boolean') &&
        (message.recovered === undefined || typeof message.recovered === 'boolean') &&
        (message.durableComplete === undefined || typeof message.durableComplete === 'boolean') &&
        (message.attachmentRefs === undefined ||
          (Array.isArray(message.attachmentRefs) && message.attachmentRefs.every(ref => typeof ref === 'string'))) &&
        (message.rowId === undefined || (typeof message.rowId === 'number' && Number.isFinite(message.rowId)))
    ) &&
    (snapshot.streamId === null || typeof snapshot.streamId === 'string') &&
    (snapshot.turnStartedAt === null || typeof snapshot.turnStartedAt === 'number') &&
    typeof snapshot.updatedAt === 'number' &&
    Number.isFinite(snapshot.updatedAt)
  )
}

function parseSnapshot(raw: string): InFlightTurnSnapshot | null {
  if (raw.length > MAX_ENTRY_CHARS) {
    return null
  }

  try {
    const parsed = JSON.parse(raw)

    return isSnapshot(parsed) ? parsed : null
  } catch {
    return null
  }
}

function serializeSnapshot(snapshot: InFlightTurnSnapshot): string | null {
  let messages = snapshot.messages

  while (messages.length > 0) {
    try {
      const raw = JSON.stringify({ ...snapshot, messages })

      if (raw.length <= MAX_ENTRY_CHARS) {
        return raw
      }
    } catch {
      return null
    }

    // Keep the join-key row and newest assistant progress while dropping the
    // oldest sealed rows. If those two rows alone do not fit, the caller must
    // avoid replacing an older recoverable snapshot with a tombstone.
    if (messages.length <= 2) {
      return null
    }

    messages = [messages[0], ...messages.slice(2)]
  }

  return null
}

function sweepSessionStore(store: Storage, reserveSlot = false): void {
  if (sessionStoreSwept) {
    return
  }

  sessionStoreSwept = true

  try {
    const sessionKeys: string[] = []

    for (let index = 0; index < store.length; index += 1) {
      const key = store.key(index)

      if (key?.startsWith(STORAGE_PREFIX)) {
        sessionKeys.push(key)
      }
    }

    const liveEntries: Array<{ key: string; snapshot: InFlightTurnSnapshot }> = []
    const migrated = readRaw(store, LEGACY_MIGRATION_KEY) !== null

    for (const key of sessionKeys) {
      const raw = readRaw(store, key)

      // A tombstone is intentional state. It suppresses the stale v1
      // predecessor until the one-shot migration removes the aggregate.
      if (raw === DISCARDED_SNAPSHOT_RAW) {
        if (migrated) {
          removeRaw(store, key)
        }

        continue
      }

      const snapshot = raw ? parseSnapshot(raw) : null

      if (!snapshot || isExpired(snapshot)) {
        removeRaw(store, key)

        continue
      }

      liveEntries.push({ key, snapshot })
    }

    liveEntries
      .sort((left, right) => right.snapshot.updatedAt - left.snapshot.updatedAt)
      .slice(reserveSlot ? MAX_ENTRIES - 1 : MAX_ENTRIES)
      .forEach(entry => removeRaw(store, entry.key))
  } catch {
    // The journal is best effort; a storage enumeration failure must not
    // interrupt renderer work or turn persistence.
  }
}

function boundedString(value: string, maxChars: number): string {
  return value.length <= maxChars ? value : value.slice(0, maxChars)
}

function boundedPart(part: ChatMessagePart): ChatMessagePart | null {
  if (part.type === 'text') {
    return {
      type: 'text',
      text: boundedString(part.text, MAX_TEXT_PART_CHARS),
      ...(part.sourceRowId === undefined ? {} : { sourceRowId: part.sourceRowId }),
      ...(part.parentId === undefined ? {} : { parentId: boundedString(part.parentId, MAX_METADATA_CHARS) })
    }
  }

  if (part.type === 'reasoning') {
    return {
      type: 'reasoning',
      text: boundedString(part.text, MAX_TEXT_PART_CHARS),
      ...(part.parentId === undefined ? {} : { parentId: boundedString(part.parentId, MAX_METADATA_CHARS) })
    }
  }

  if (part.type === 'tool-call') {
    // Tool payloads can contain multi-megabyte command output. Recovery only
    // needs invocation identity and failure state; args/results are available
    // from the backend transcript when it survives.
    return {
      type: 'tool-call',
      toolName: boundedString(part.toolName, MAX_METADATA_CHARS),
      args: {},
      ...(part.toolCallId === undefined ? {} : { toolCallId: boundedString(part.toolCallId, MAX_METADATA_CHARS) }),
      ...(part.result === undefined ? {} : { result: {} }),
      ...(part.isError === undefined ? {} : { isError: part.isError })
    }
  }

  // Rich file/image/data/source parts can embed large payloads. They are not
  // required for in-flight text/tool recovery and remain backend-owned.
  return null
}

function boundedMessages(messages: ChatMessage[]): ChatMessage[] | null {
  const bounded =
    messages.length <= MAX_JOURNALED_MESSAGES
      ? messages
      : [messages[0], ...messages.slice(-(MAX_JOURNALED_MESSAGES - 1))]

  // User text and attachment refs are the recovery join key. Truncating either
  // could attach a journal tail to the wrong transcript row, so pathological
  // prompts skip journaling instead of weakening the match.
  if (
    bounded.some(message => {
      if (message.role !== 'user') {
        return false
      }

      if (
        message.parts.some(
          part => (part.type === 'text' || part.type === 'reasoning') && part.text.length > MAX_TEXT_PART_CHARS
        )
      ) {
        return true
      }

      const refs = message.attachmentRefs

      if (!refs) {
        return false
      }

      if (refs.length > MAX_USER_ATTACHMENT_REFS) {
        return true
      }

      let chars = 0

      for (const ref of refs) {
        chars += ref.length

        if (chars > MAX_USER_ATTACHMENT_REF_CHARS) {
          return true
        }
      }

      return false
    })
  ) {
    return null
  }

  return bounded.map(message => ({
    id: boundedString(message.id, MAX_METADATA_CHARS),
    role: message.role,
    parts: message.parts.map(boundedPart).filter((part): part is ChatMessagePart => part !== null),
    ...(message.timestamp === undefined ? {} : { timestamp: message.timestamp }),
    ...(message.pending === undefined ? {} : { pending: message.pending }),
    ...(message.error === undefined ? {} : { error: boundedString(message.error, MAX_METADATA_CHARS) }),
    ...(message.branchGroupId === undefined
      ? {}
      : { branchGroupId: boundedString(message.branchGroupId, MAX_METADATA_CHARS) }),
    ...(message.hidden === undefined ? {} : { hidden: message.hidden }),
    ...(message.interim === undefined ? {} : { interim: message.interim }),
    ...(message.recovered === undefined ? {} : { recovered: message.recovered }),
    ...(message.durableComplete === undefined ? {} : { durableComplete: message.durableComplete }),
    ...(message.attachmentRefs === undefined
      ? {}
      : {
          attachmentRefs:
            message.role === 'user'
              ? [...message.attachmentRefs]
              : message.attachmentRefs
                  .slice(0, MAX_USER_ATTACHMENT_REFS)
                  .map(ref => boundedString(ref, MAX_METADATA_CHARS))
        }),
    ...(message.rowId === undefined ? {} : { rowId: message.rowId })
  }))
}

function migrateLegacyStore(store: Storage): void {
  if (readRaw(store, LEGACY_MIGRATION_KEY) !== null) {
    return
  }

  const raw = readRaw(store, LEGACY_STORAGE_KEY)

  if (raw === null) {
    return
  }

  // Claim the migration before touching the aggregate. If storage is failing,
  // skip legacy recovery rather than retrying an expensive parse on every read.
  if (!writeRaw(store, LEGACY_MIGRATION_KEY, '1')) {
    return
  }

  // Release the multi-megabyte aggregate before allocating per-session v2
  // entries. The captured string remains available for this one migration.
  removeRaw(store, LEGACY_STORAGE_KEY)

  if (!raw) {
    return
  }

  if (raw.length > MAX_LEGACY_STORE_CHARS) {
    return
  }

  try {
    const parsed = JSON.parse(raw) as Partial<JournalStore>

    if (
      parsed.version !== STORE_VERSION ||
      !parsed.entries ||
      typeof parsed.entries !== 'object' ||
      Array.isArray(parsed.entries)
    ) {
      return
    }

    const existingV2Keys = new Set<string>()

    for (let index = 0; index < store.length; index += 1) {
      const key = store.key(index)

      if (key?.startsWith(STORAGE_PREFIX)) {
        existingV2Keys.add(key)
      }
    }

    const entries = Object.entries(parsed.entries)
      .filter((entry): entry is [string, InFlightTurnSnapshot] => isSnapshot(entry[1]) && !isExpired(entry[1]))
      .sort((a, b) => b[1].updatedAt - a[1].updatedAt)
      .slice(0, Math.max(0, MAX_ENTRIES - existingV2Keys.size))

    for (const [storedSessionId, snapshot] of entries) {
      const key = sessionStorageKey(storedSessionId)
      const messages = boundedMessages(snapshot.messages)
      const value = messages ? serializeSnapshot({ ...snapshot, messages }) : null

      // A v2 snapshot may have been written before the one-shot migration ran.
      // Never replace newer per-session state with its stale v1 predecessor.
      if (key && value && readRaw(store, key) === null) {
        if (writeRaw(store, key, value)) {
          existingV2Keys.add(key)
        }
      }
    }
  } catch {
    // Malformed legacy data is discarded below.
  }
}

function discardSnapshot(store: Storage, key: string): void {
  // Migrate first so an existing v2 key suppresses its stale v1 predecessor,
  // then remove the current session. This keeps every discard path from
  // resurrecting legacy state on a later read.
  migrateLegacyStore(store)
  removeRaw(store, key)
}

function readSnapshot(storedSessionId: string): InFlightTurnSnapshot | null {
  const store = storage()
  const key = sessionStorageKey(storedSessionId)

  if (!store || !key) {
    return null
  }

  sweepSessionStore(store)

  let raw = readRaw(store, key)

  if (!raw) {
    migrateLegacyStore(store)
    raw = readRaw(store, key)
  }

  if (!raw) {
    return null
  }

  const snapshot = parseSnapshot(raw)

  if (!snapshot || isExpired(snapshot)) {
    discardSnapshot(store, key)

    return null
  }

  return snapshot
}

function removeSnapshot(storedSessionId: string): void {
  const store = storage()
  const key = sessionStorageKey(storedSessionId)

  if (store && key) {
    sweepSessionStore(store)

    // Settling a session before the one-shot migration must clear its legacy
    // entry too; otherwise a later read can migrate and resurrect stale state.
    // This aggregate parse is terminal-transition work, never a stream write.
    discardSnapshot(store, key)
  }
}

function isExpired(entry: InFlightTurnSnapshot, now = Date.now()): boolean {
  return now - entry.updatedAt > MAX_AGE_MS
}

function cloneMessages(messages: ChatMessage[]): ChatMessage[] {
  try {
    return JSON.parse(JSON.stringify(messages)) as ChatMessage[]
  } catch {
    return []
  }
}

function attachmentSignature(message: ChatMessage): string {
  return (message.attachmentRefs ?? []).join('\n')
}

function userMessagesMatch(left: ChatMessage, right: ChatMessage): boolean {
  return (
    left.role === 'user' &&
    right.role === 'user' &&
    (left.rowId === undefined || right.rowId === undefined || left.rowId === right.rowId) &&
    normalizedText(chatMessageText(left)) === normalizedText(chatMessageText(right)) &&
    attachmentSignature(left) === attachmentSignature(right)
  )
}

function partHasRecoverableContent(part: ChatMessagePart): boolean {
  if (part.type === 'text' || part.type === 'reasoning') {
    return typeof part.text === 'string' && part.text.trim().length > 0
  }

  return part.type === 'tool-call'
}

function assistantHasRecoverableContent(message: ChatMessage): boolean {
  return message.role === 'assistant' && (Boolean(message.error) || message.parts.some(partHasRecoverableContent))
}

/** A live-turn projection row (backend `inflight` via appendLiveSessionProjection,
 *  or a still-streaming local bubble) — as opposed to a completed transcript row. */
function isLiveProjectionRow(message: ChatMessage): boolean {
  return Boolean(message.pending) || isLiveTailReplyId(message.id)
}

/** A row the transcript actually holds: neither a live projection nor a
 *  journal recovery that is still waiting for its durable counterpart. */
function isCommittedRow(message: ChatMessage): boolean {
  return !isLiveProjectionRow(message) && !message.recovered
}

/** Visible tail of the running turn: the streaming assistant row (plus any
 *  interim rows sealed after it) back to the user prompt that started it. */
function recoverableTail(messages: ChatMessage[], streamId: null | string): ChatMessage[] {
  const visible = messages.filter(message => !message.hidden)
  let assistantIndex = -1

  if (streamId) {
    assistantIndex = visible.findIndex(message => message.id === streamId && assistantHasRecoverableContent(message))
  }

  if (assistantIndex < 0) {
    for (let index = visible.length - 1; index >= 0; index -= 1) {
      const message = visible[index]

      if (message.role === 'user') {
        break
      }

      if (assistantHasRecoverableContent(message)) {
        assistantIndex = index

        break
      }
    }
  }

  if (assistantIndex < 0) {
    return []
  }

  let start = assistantIndex

  for (let index = assistantIndex - 1; index >= 0; index -= 1) {
    if (visible[index].role === 'user') {
      start = index

      // A mid-turn redirect inserts its correction as another user row right
      // before the live reply, so the turn can open with a RUN of user rows.
      // Keep walking back over them: stopping at the nearest one journals the
      // correction alone and loses the prompt that actually started the turn.
      while (start > 0 && visible[start - 1].role === 'user') {
        start -= 1
      }

      break
    }
  }

  return visible.slice(start)
}

function normalizeRecoveredTail(tail: ChatMessage[], keepPending: boolean): ChatMessage[] {
  return cloneMessages(tail).map(message =>
    message.role === 'assistant'
      ? {
          ...message,
          pending: keepPending ? (message.pending ?? true) : false,
          ...(keepPending ? {} : { recovered: true })
        }
      : { ...message, pending: false }
  )
}

function assistantTextLength(message: ChatMessage): number {
  return chatMessageText(message).length
}

/** Merge the journal's last assistant row into the base's live projection row.
 *
 * The journal carries structure (tool calls, reasoning) the backend snapshot
 * lacks; the backend text may be newer than the journal's last throttled
 * write. Keep the journal's parts, but let the longer text win — and keep the
 * BASE row's id so live deltas keep appending to the row the stream handler
 * already targets.
 */
function hasStructuralParts(message: ChatMessage): boolean {
  return message.parts.some(part => part.type === 'reasoning' || part.type === 'tool-call')
}

function overlayProjectionRow(projection: ChatMessage, journalRow: ChatMessage): ChatMessage {
  // A projected error (retained failed turn) must survive the overlay.
  const error = journalRow.error ?? projection.error

  const merged: ChatMessage = {
    ...journalRow,
    id: projection.id,
    pending: projection.pending,
    ...(error ? { error } : {})
  }

  if (assistantTextLength(projection) <= assistantTextLength(journalRow)) {
    return merged
  }

  // Backend text is newer than the journal's last throttled write — swap it
  // into the journal's first text part, keeping tool calls and reasoning.
  // When the journal already carries structure, only accept a *strict*
  // extension of the answer text. A longer flat dump that starts with
  // thinking chatter must not overwrite / insert as answer text (#76444).
  const projectionText = chatMessageText(projection)
  const journalText = chatMessageText(journalRow).trim()

  if (hasStructuralParts(journalRow)) {
    const next = projectionText.trim()

    if (!journalText || !next.startsWith(journalText)) {
      return merged
    }
  }

  const parts: ChatMessagePart[] = []
  let textReplaced = false

  for (const part of journalRow.parts) {
    if (part.type !== 'text') {
      parts.push(part)
    } else if (!textReplaced) {
      parts.push({ ...part, text: projectionText })
      textReplaced = true
    }
  }

  if (!textReplaced) {
    parts.push({ type: 'text', text: projectionText })
  }

  return { ...merged, parts }
}

/** Rows the base transcript doesn't already hold by id. The journal and the
 *  base can both carry the same row (a resume that replays a still-journaled
 *  turn), and appending it twice puts a duplicate id in the transcript —
 *  which assistant-ui's MessageRepository rejects by throwing. */
function withoutBaseIds(rows: ChatMessage[], baseMessages: ChatMessage[]): ChatMessage[] {
  const baseIds = new Set(baseMessages.map(message => message.id))

  return rows.filter(row => !baseIds.has(row.id))
}

/** Without a matched user interval, a journal can only be stale relative to
 *  the turn that most recently committed. A live row (no durable id yet) is
 *  covered by an equal reply in that last turn; a durable row needs identity.
 *  An older turn saying the same thing does not retire this journal. */
function journalTailAlreadyCommitted(tailAssistants: ChatMessage[], baseMessages: ChatMessage[]): boolean {
  const recoverable = tailAssistants.filter(assistantHasRecoverableContent)

  if (recoverable.length === 0) {
    return false
  }

  // Hidden prompts (bots mode, slash commands) start turns too.
  const lastTurnStart = baseMessages.findLastIndex(message => message.role === 'user')

  const identityCovers = (base: ChatMessage, index: number, journaled: ChatMessage) =>
    base.id === journaled.id || (journaled.rowId === undefined ? index > lastTurnStart : base.rowId === journaled.rowId)

  return recoverable.every(message =>
    baseMessages.some(
      (base, index) =>
        base.role === 'assistant' &&
        !base.hidden &&
        isCommittedRow(base) &&
        identityCovers(base, index, message) &&
        base.error === message.error &&
        normalizedText(chatMessageText(base)) === normalizedText(chatMessageText(message)) &&
        message.parts.every(part => {
          if (part.type === 'tool-call') {
            return base.parts.some(
              candidate =>
                candidate.type === 'tool-call' && Boolean(part.toolCallId) && candidate.toolCallId === part.toolCallId
            )
          }

          return (
            part.type !== 'reasoning' ||
            base.parts.some(
              candidate =>
                candidate.type === 'reasoning' && normalizedText(candidate.text) === normalizedText(part.text)
            )
          )
        })
    )
  )
}

export function mergeInFlightMessages(
  baseMessages: ChatMessage[],
  tailMessages: ChatMessage[],
  options: { keepPending?: boolean } = {}
): InFlightRecoveryResult {
  const noop: InFlightRecoveryResult = {
    applied: false,
    caughtUp: false,
    messages: baseMessages,
    streamId: null,
    turnStartedAt: null
  }

  const tail = normalizeRecoveredTail(tailMessages, Boolean(options.keepPending))

  if (!tail.some(assistantHasRecoverableContent)) {
    return noop
  }

  // Anchor on the latest tail user the base already holds. A steer typed
  // before the first token rides in the tail behind its prompt; when only the
  // prompt persisted, the prompt is the anchor and the steer is recovered
  // output, not a reason to append the prompt a second time.
  let tailUserIndex = tail.findLastIndex(message => message.role === 'user')
  let matchingUserIndex = -1

  for (let index = tailUserIndex; index >= 0; index -= 1) {
    const tailUser = tail[index]

    if (tailUser.role !== 'user') {
      continue
    }

    matchingUserIndex = baseMessages.findLastIndex(message => userMessagesMatch(message, tailUser))

    if (matchingUserIndex >= 0) {
      tailUserIndex = index

      break
    }
  }

  let tailAssistants = tail.slice(tailUserIndex + 1)
  let lastJournalRow = tailAssistants.findLast(assistantHasRecoverableContent) ?? null

  if (matchingUserIndex < 0) {
    // No base user matches the tail's user row (a projected user-inflight row
    // that never persisted, or a tail captured without its user prompt). If the
    // tail's answers are already committed in the transcript, the journal is
    // stale — appending it would re-render the same replies at the end of the
    // conversation. Otherwise, the base never saw this turn at all: append the
    // whole tail (the crash-recovery path the journal exists for).
    if (journalTailAlreadyCommitted(tailAssistants, baseMessages)) {
      return { ...noop, caughtUp: true }
    }

    const streamId = lastJournalRow?.id ?? null

    return {
      applied: true,
      caughtUp: false,
      messages: [...baseMessages, ...withoutBaseIds(tail, baseMessages)],
      // Recovered output stays journaled without pretending the backend is busy.
      streamId: options.keepPending ? streamId : null,
      turnStartedAt: null
    }
  }

  const nextUserIndex = baseMessages.findIndex((message, index) => index > matchingUserIndex && message.role === 'user')
  const end = nextUserIndex < 0 ? baseMessages.length : nextUserIndex
  const afterUser = baseMessages.slice(matchingUserIndex + 1, end)

  const completedReply = afterUser.find(
    message =>
      assistantHasRecoverableContent(message) &&
      isCommittedRow(message) &&
      (message.durableComplete === true ||
        (message.durableComplete === undefined &&
          !message.interim &&
          !message.parts.some(part => part.type === 'tool-call')))
  )

  if (completedReply) {
    // A final reply, not merely a persisted tool round, supersedes this tail.
    return { ...noop, caughtUp: true }
  }

  tailAssistants = withoutCoveredAssistantPrefix(afterUser.filter(isCommittedRow), tailAssistants)
  lastJournalRow = tailAssistants.findLast(assistantHasRecoverableContent) ?? null

  const projectionIndex = baseMessages.findLastIndex(
    (message, index) =>
      index > matchingUserIndex &&
      index < end &&
      message.role === 'assistant' &&
      !message.interim &&
      isLiveProjectionRow(message)
  )

  if (projectionIndex < 0) {
    if (tailAssistants.length === 0) {
      return noop
    }

    const streamId = lastJournalRow?.id ?? null

    return {
      applied: true,
      caughtUp: false,
      messages: [
        ...baseMessages.slice(0, end),
        ...withoutBaseIds(tailAssistants, baseMessages),
        ...baseMessages.slice(end)
      ],
      // Only a running turn keeps a stream target; recovered metadata retains
      // the journal independently until durable completion is observed.
      streamId: options.keepPending ? streamId : null,
      turnStartedAt: null
    }
  }

  // Backend projection row present (text-only): overlay the journal's
  // structure onto it instead of treating it as "caught up" — that is how
  // locally recorded tool progress used to get dropped.
  const projection = baseMessages[projectionIndex]
  const merged = lastJournalRow ? overlayProjectionRow(projection, lastJournalRow) : projection

  const sealedRows = tailAssistants.filter(
    message => message !== lastJournalRow && assistantHasRecoverableContent(message)
  )

  const messages = [
    ...baseMessages.slice(0, projectionIndex),
    ...withoutBaseIds(sealedRows, baseMessages),
    merged,
    ...baseMessages.slice(projectionIndex + 1)
  ]

  return {
    applied: true,
    caughtUp: false,
    messages,
    // Idle recovered output must not reopen a streaming bubble.
    streamId: options.keepPending ? merged.id : null,
    turnStartedAt: null
  }
}

const persistTimers = new Map<string, ReturnType<typeof setTimeout>>()
const persistLatest = new Map<string, JournalableSessionState>()

/** @internal Test-only reset for module-scoped throttles and sweep state. */
export function resetInFlightTurnJournalStateForTests(): void {
  for (const timer of persistTimers.values()) {
    clearTimeout(timer)
  }

  persistTimers.clear()
  persistLatest.clear()
  sessionStoreSwept = false
}

function writeSnapshot(storedSessionId: string, state: JournalableSessionState): void {
  const tail = recoverableTail(state.messages, state.streamId)

  if (tail.length === 0) {
    return
  }

  const store = storage()
  const key = sessionStorageKey(storedSessionId)

  if (!store || !key) {
    return
  }

  sweepSessionStore(store, true)

  const messages = boundedMessages(tail)

  if (!messages) {
    // Keep the timer write path free of aggregate migration. This tiny invalid
    // v2 value suppresses the stale v1 predecessor until read/settle performs
    // the one-shot migration and removes it.
    tombstoneUnlessRecoverable(store, key)

    return
  }

  const raw = serializeSnapshot({
    messages,
    streamId: state.streamId,
    turnStartedAt: state.turnStartedAt,
    updatedAt: Date.now()
  })

  if (!raw) {
    // Preserve an older bounded snapshot if the newest assistant row alone is
    // too large. A tombstone is only needed when there is no recoverable v2
    // value, so stale v1 state cannot be resurrected on a later read.
    tombstoneUnlessRecoverable(store, key)

    return
  }

  if (!writeRaw(store, key, raw)) {
    // A quota failure must not leave an older, misleading snapshot behind, or
    // let the stale v1 predecessor be resurrected on a later read.
    tombstoneUnlessRecoverable(store, key)
  }
}

function tombstoneUnlessRecoverable(store: Storage, key: string): void {
  const previous = readRaw(store, key)

  if (previous) {
    const snapshot = parseSnapshot(previous)

    if (snapshot && !isExpired(snapshot)) {
      return
    }
  }

  if (!writeRaw(store, key, DISCARDED_SNAPSHOT_RAW)) {
    removeRaw(store, key)
  }
}

/** Persist the running turn's visible tail (throttled), or clear the entry the
 *  moment the turn settles. Call on every session-state commit. */
export function persistInFlightTurnState(state: JournalableSessionState): void {
  const storedSessionId = state.storedSessionId

  if (!storedSessionId) {
    return
  }

  // `some(recovered)` is a cheap pre-check: this runs on every idle commit of
  // every cached session, and recovered rows are rare.
  if (
    !state.busy &&
    !state.awaitingResponse &&
    !state.streamId &&
    !(
      state.messages.some(message => message.recovered) &&
      recoverableTail(state.messages, null).some(message => message.recovered)
    )
  ) {
    clearInFlightTurnJournal(storedSessionId)

    return
  }

  persistLatest.set(storedSessionId, state)

  if (persistTimers.has(storedSessionId)) {
    return
  }

  persistTimers.set(
    storedSessionId,
    setTimeout(() => {
      persistTimers.delete(storedSessionId)
      const latest = persistLatest.get(storedSessionId)

      persistLatest.delete(storedSessionId)

      if (latest) {
        writeSnapshot(storedSessionId, latest)
      }
    }, PERSIST_THROTTLE_MS)
  )
}

export function readInFlightTurnJournal(storedSessionId: null | string): InFlightTurnSnapshot | null {
  if (!storedSessionId) {
    return null
  }

  return readSnapshot(storedSessionId)
}

/** Fold a journaled in-flight tail back onto a restored transcript. A no-op
 *  returns `baseMessages` by reference so callers keep their fast-path ref. */
export function recoverInFlightTurnJournal(
  storedSessionId: null | string,
  baseMessages: ChatMessage[],
  options: { keepPending?: boolean } = {}
): InFlightRecoveryResult {
  const snapshot = readInFlightTurnJournal(storedSessionId)

  if (!snapshot) {
    return {
      applied: false,
      caughtUp: false,
      messages: baseMessages,
      streamId: null,
      turnStartedAt: null
    }
  }

  const recovered = mergeInFlightMessages(baseMessages, snapshot.messages, options)

  if (recovered.caughtUp) {
    clearInFlightTurnJournal(storedSessionId)
  }

  return {
    ...recovered,
    // Never resurrect a stale stream target on an idle resume: with
    // keepPending=false the session is not running, so the recovered rows are
    // settled history and the journal must clear on the next state update —
    // otherwise the same stale tail is folded again on every open.
    streamId: recovered.applied ? (recovered.streamId ?? (options.keepPending ? snapshot.streamId : null)) : null,
    turnStartedAt: recovered.applied ? snapshot.turnStartedAt : null
  }
}

export function clearInFlightTurnJournal(storedSessionId: null | string): void {
  if (!storedSessionId) {
    return
  }

  const timer = persistTimers.get(storedSessionId)

  if (timer) {
    clearTimeout(timer)
    persistTimers.delete(storedSessionId)
  }

  persistLatest.delete(storedSessionId)

  removeSnapshot(storedSessionId)
}

/** Purge journaled in-flight tails for a deleted session.
 *
 *  Deleting a session removes its authoritative history, but the journal
 *  kept the deleted turn's user prompt and tool calls in localStorage until
 *  the entry aged out — the delete gesture did not reach the local copy (the
 *  sibling `composer-queue` store was already cleared on delete; this one
 *  was missed, which is the #77486 journal requirement).
 *
 *  A session has more than one id, so callers pass every id they hold: the
 *  stored tip, the durable lineage root, and (the sidebar delete) the closing
 *  runtime id. The store keys on the stored id, so each id drains its own key
 *  in a single pass; entries under a stored id NO live path can still name
 *  are NOT reachable this way — those age out under `MAX_AGE_MS`, which is
 *  the retention floor this file already documents. Rotation closes that gap:
 *  `migrateInFlightTurnJournal` re-keys an entry the moment its stored id
 *  rotates, so a mid-lineage tip cannot strand its journal key. */
function migrateSnapshot(store: Storage, oldKey: string, newKey: string, raw: string): void {
  // Copy first, delete second. A crash between the two writes leaves BOTH
  // keys holding the same recoverable tail — resume folds either one — and
  // the old key is still reachable by the id the deleter holds (tip, lineage
  // root, runtime id), so nothing escapes the delete gesture. Deleting first
  // would instead trade recovery for a crash window.
  if (readRaw(store, newKey) === null && !writeRaw(store, newKey, raw)) {
    return
  }

  removeRaw(store, oldKey)
}

/** Re-key a journaled in-flight tail when its session's stored id rotates
 *  (e.g. auto-compression forks a continuation: tip A -> tip B).
 *
 *  The old stored id leaves every live index (`runtimeIdByStoredSessionId`,
 *  tiles, lineage resolution to the tip), so the key under it would otherwise
 *  be unreachable by a later permanent delete and simply age out after
 *  MAX_AGE_MS — a sensitive prompt tail surviving its session's delete.
 *  Move the entry to the new id so recovery keeps working and deletion by
 *  the new id reaches it. A null/cleared new id is a detach, not a rotation —
 *  the old id remains the session's live tip, so its key stays reachable and
 *  is left alone. */
export function migrateInFlightTurnJournal(oldStoredSessionId: string, newStoredSessionId: null | string): void {
  if (!oldStoredSessionId || !newStoredSessionId || oldStoredSessionId === newStoredSessionId) {
    return
  }

  const store = storage()
  const oldKey = sessionStorageKey(oldStoredSessionId)
  const newKey = sessionStorageKey(newStoredSessionId)

  if (!store || !oldKey || !newKey) {
    return
  }

  const raw = readRaw(store, oldKey)

  if (raw === null) {
    return
  }

  // A tombstone or expired entry carries nothing recoverable — retire it.
  const snapshot = raw === DISCARDED_SNAPSHOT_RAW ? null : parseSnapshot(raw)

  if (!snapshot || isExpired(snapshot)) {
    removeRaw(store, oldKey)

    return
  }

  migrateSnapshot(store, oldKey, newKey, raw)
}

export function purgeInFlightTurnJournals(sessionIds: readonly (null | string | undefined)[]): void {
  const store = storage()

  if (!store) {
    return
  }

  for (const storedSessionId of sessionIds) {
    if (storedSessionId) {
      clearInFlightTurnJournal(storedSessionId)
    }
  }
}