moeru-ai/airi · error · Error

Failed to load the target chat session

Error message

Failed to load the target chat session

What it means

deleteMessage in the chat session store requires the target session to be hydrated first: loadSession(payload.sessionId) returning false (unknown id, deleted session, or a swallowed IndexedDB/cloud hydration error) is converted into this throw. The message deletion itself is a pure in-memory filter plus persist, so the only way to reach the error is the session not being loadable.

Source

Thrown at packages/stage-ui/src/stores/chat/session-store.ts:341

      void persistSession(sessionId)
  }

  function setSessionMessages(sessionId: string, next: ChatHistoryItem[]) {
    replaceSessionMessages(sessionId, next)
  }

  function appendSessionMessage(sessionId: string, message: ChatHistoryItem) {
    ensureSession(sessionId)
    replaceSessionMessages(sessionId, [
      ...(sessionMessages.value[sessionId] ?? []),
      message,
    ])
  }

  /** Removes one message by stable id or by its current history index. */
  async function deleteMessage(payload: DeleteChatMessagePayload): Promise<void> {
    if (!await loadSession(payload.sessionId))
      throw new Error('Failed to load the target chat session')

    const nextMessages = getSessionMessages(payload.sessionId).filter((message, messageIndex) => {
      if (payload.messageId)
        return message.id !== payload.messageId
      if (payload.index !== undefined)
        return messageIndex !== payload.index
      return true
    })

    setSessionMessages(payload.sessionId, nextMessages)
  }

  /**
   * Hydrate a single session's messages from IDB into memory. Idempotent —
   * subsequent calls for the same id are no-ops.
   *
   * Use when:
   * - The drawer is opening, the user is switching to a session, or any

View on GitHub (pinned to f679616c34)

Solutions

  1. Ensure the session is selected/loaded before showing per-message delete actions.
  2. Refresh the sessions/messages state when this error occurs and clear stale ids.
  3. Investigate storage health if the session is expected to exist.
  4. For index-based deletes, recompute indices from the freshly loaded messages.

Example fix

// before
await chatSession.deleteMessage({ sessionId, index })

// after
if (!await chatSession.loadSession(sessionId)) {
  await refreshSessions()
  toast.info('This chat no longer exists')
  return
}
await chatSession.deleteMessage({ sessionId, index })
Defensive patterns

Strategy: validation

Validate before calling

if (!await chatSession.loadSession(sessionId)) {
  await refreshSessions()
  return
}
await chatSession.deleteMessage(payload)

Try / catch

try {
  await chatSession.deleteMessage({ sessionId, messageId })
}
catch (error) {
  if (errorMessageFrom(error) === 'Failed to load the target chat session') {
    await refreshSessions()
    return
  }
  throw error
}

Prevention

When it happens

Trigger: Deleting a message from a session removed in another tab; acting on a stale sessionId retained by the messages component; IndexedDB failures (quota, corruption, restricted storage) during hydration; session list refreshed elsewhere while a delete action was pending.

Common situations: Message action menus rendered from cached history after the session was cleared; multi-tab usage; environments with flaky storage (private browsing, embedded webviews).

Related errors


AI-assisted analysis of moeru-ai/airi@f679616c34 (2026-08-18). Data as JSON: /api/errors/4dcced2b0ec0f3f4. Report an issue: GitHub.