Skip to content

AI, fully local on your computer.

95.1 KB
Raw Blame
import { randomUUID } from 'node:crypto'
import { t } from '../../shared/i18n'
import { type Db, prepareCached } from './db'
import {
  SEARCH_MARK_END,
  SEARCH_MARK_START,
  type ChatDepthMode,
  type Citation,
  type Conversation,
  type ConversationSearchHit,
  type ContextUsage,
  type ConversationSearchResult,
  type CoverageInfo,
  type DocumentScope,
  type EndedEarly,
  type KnowledgePackOutcome,
  type Message,
  type SkillOffer,
  type TruncationCause
} from '../../shared/types'
import { parseDocumentScope } from './collections'
import { buildFtsMatchQuery } from './fts'
import { approxTokenCount } from './ingestion/chunker'
import { getSettings } from './settings'
import { findManifestById, launchContextTokens } from './models'
import {
  approxPromptTokens,
  buildSkillFence,
  composeSystemPromptWithSkill,
  logSkillFenceReduction,
  skillFenceBudgetTokens,
  stripSkillFenceEcho
} from './skills/prompt'
import type { ChatMessage, ModelRuntime, RuntimeTimings } from './runtime'
import { requestParamsForMode } from './runtime/llama'
import { endedEarlyCause, parseEndedEarly } from './chat/ended-early'
import { ensureCompacted } from './chat/compaction'
import { log } from './logging'

/**
 * The ONE skill resolved for a turn (skills plan §10) — the minimal shape the chat/RAG
 * generators need to assemble the fence + stamp `messages.skill_id`. Built by
 * `resolveTurnSkill` (services/skills/turn.ts); structurally compatible without an import.
 */
export interface TurnSkill {
  installId: string
  title: string
  body: string
  /**
   * True only when the app AUTO-FIRED this skill (S13b/D3) — the user set no skill and the resolver
   * filled the gap. `resolveAutoFireSkill` sets it; an explicit pick / sticky default leaves it
   * undefined. Carried so the assistant row can be stamped (`messages.auto_fired`) and the per-turn
   * "answer without it" undo (S13c) shows ONLY on an auto-fired turn.
   */
  autoFired?: boolean
}

// Chat service (spec §7.6): create conversations, append user/assistant messages,
// build the system prompt + message list for the runtime, stream the response, and
// persist history. All local — no network. IDs are UUID v4, timestamps ISO-8601 UTC
// (BUILD_STATE §7 conventions). The system prompt is built per-request and NOT
// persisted; the messages table holds only user/assistant turns.

// Persist-canonical English (i18n boundary rule 1): written into conversations.title
// AND exact-matched by maybeSetTitleFromFirstMessage below, so the value must never
// localize at persist time — the renderer display map translates it (D-L4).
const DEFAULT_TITLE = t('en', 'main.chat.defaultTitle')

// Base system prompt — the PLAIN-CHAT preamble. Revised 2026-07-01 (owner-approved, from D:\ chat
// testing) from the original spec §7.6 wording, which was destroying the chat UX: small local models
// latched onto its "You do not have internet access. / You must not claim to have accessed external
// services." lines and prefaced almost every answer with offline/no-internet/training-cutoff
// disclaimers, and its DOCUMENT-grounding lines ("answer only from the context… / include citations…")
// leaked into plain chat, so the model REFUSED general-knowledge questions and pushed "upload a
// document" (see the sample transcript). Those grounding rules belong ONLY to the grounded path and
// already live, in full, in rag/index.ts `GROUNDING_RULES` (appended to this base as
// `GROUNDED_SYSTEM_PROMPT`) — so removing them here loses nothing for RAG and fixes plain chat. This
// base now tells the model to answer general questions directly from its own knowledge, stay honest
// about uncertainty WITHOUT per-turn disclaimers, and keeps the one load-bearing guardrail (never
// claim to have browsed / accessed data it wasn't given). RAG still appends `GROUNDING_RULES`; a
// more-specific, later instruction, it governs document questions.
export const BASE_SYSTEM_PROMPT = `You are HilbertRaum, a private assistant that runs locally and offline on the user's own computer.
Be helpful, accurate, and honest about uncertainty.
Answer general questions directly and fully from your own knowledge. If a specific fact is uncertain or may be out of date, note that briefly where it matters.
Do not open answers with disclaimers about being offline, lacking internet access, or a training cutoff, and do not tell the user to upload documents — the user already knows this app runs locally and privately.
Never claim to have searched the web, browsed, or opened files you were not given — you have not.
Respond in the language the user writes in.`

/**
 * Build the plain-chat system prompt (document answers compose their own in rag/). When a
 * skill fence is supplied it is bracketed AFTER the base preamble — the base rules above, the
 * fence's own guard line as the last app-authored line below — so the untrusted skill text never
 * reads as a top-level rule (skills plan §11.2). `skillFence` null/omitted ⇒ the base preamble.
 */
export function buildSystemPrompt(skillFence?: string | null): string {
  return composeSystemPromptWithSkill(BASE_SYSTEM_PROMPT, skillFence ?? null)
}

/**
 * Remove `<think>…</think>` reasoning blocks (including an unclosed trailing block
 * from a stream stopped mid-thought) from assistant text (architecture.md "Chat &
 * streaming").
 *
 * Reasoning must never persist and must never be fed back as history (Qwen guidance:
 * think blocks confuse the model when replayed). The normal path already separates
 * reasoning out of the answer stream (`--reasoning-format deepseek` →
 * `delta.reasoning_content`), so this is defense-in-depth: it catches inline tags
 * from a differently configured server and scrubs any legacy persisted rows.
 * Untouched text returns as-is; when blocks were removed the seams are trimmed.
 */
export function stripThinkBlocks(text: string): string {
  const stripped = text.replace(/<think>[\s\S]*?<\/think>/g, '').replace(/<think>[\s\S]*$/, '')
  return stripped === text ? text : stripped.trim()
}

function nowIso(): string {
  return new Date().toISOString()
}

/**
 * True when a streaming error is the result of a user Stop (an aborted signal)
 * rather than a real failure. The mock runtime returns cleanly on abort, but a real
 * `fetch`-backed runtime rejects the in-flight request with an `AbortError` — both
 * must be treated as a normal end so the partial reply is still persisted.
 */
export function isAbortError(err: unknown, signal?: AbortSignal): boolean {
  if (signal?.aborted) return true
  return err instanceof Error && err.name === 'AbortError'
}

/**
 * CB-4 — a completed (non-aborted) stream that produced ZERO answer tokens. Distinct from the
 * benign silent-empty cases (a stop before the first token, or an all-`<think>`/fence-echo reply
 * that stripped to empty) which still persist nothing without erroring: those arrived or were
 * aborted, this is a genuine empty completion. `generateAssistantMessage` throws it; `withChatStream`
 * maps it to the friendly `main.chat.emptyCompletion` copy so the user gets a retry signal instead of
 * a silently unanswered question. Rethrow-friendly (like the context-overflow mapping).
 */
export class EmptyCompletionError extends Error {
  constructor() {
    super('The model returned an empty completion.')
    this.name = 'EmptyCompletionError'
  }
}

/** True when `err` is an {@link EmptyCompletionError} (the completed-but-empty completion, CB-4). */
export function isEmptyCompletionError(err: unknown): boolean {
  return err instanceof EmptyCompletionError
}

interface ConversationRow {
  id: string
  title: string
  created_at: string
  updated_at: string
  model_id: string | null
  mode: string
  scope_json: string | null
  collection_id: string | null
  scope_v2_json: string | null
  active_skill_id: string | null
}

/** Parse a stored scope: a JSON array of document-id strings, else null (whole corpus). */
function parseScope(json: string | null): string[] | null {
  if (!json) return null
  try {
    const v = JSON.parse(json) as unknown
    if (Array.isArray(v)) {
      const ids = v.filter((x): x is string => typeof x === 'string' && x.length > 0)
      return ids.length > 0 ? ids : null
    }
  } catch {
    // Malformed scope must never break a conversation — fall through to unscoped.
  }
  return null
}

/** Normalize a caller-supplied scope: empty/non-string-bearing arrays become null. */
function normalizeScope(ids: string[] | null | undefined): string[] | null {
  if (!ids) return null
  const clean = ids.filter((x) => typeof x === 'string' && x.length > 0)
  return clean.length > 0 ? clean : null
}

/** Serialize a composite scope for `scope_v2_json` (null clears it). Empty scope persists. */
function serializeDocumentScope(scope: DocumentScope | null | undefined): string | null {
  if (!scope) return null
  const packIds = (scope.packIds ?? []).filter((x) => typeof x === 'string' && x.length > 0)
  return JSON.stringify({
    collectionIds: scope.collectionIds ?? [],
    documentIds: scope.documentIds ?? [],
    ...(scope.includeArchived ? { includeArchived: true } : {}),
    // Knowledge packs (ZIM wave): persisted only when selected, so pre-wave rows and
    // pack-less scopes serialize byte-identically to before.
    ...(packIds.length > 0 ? { packIds } : {}),
    // #301 P4 (finding M10, ruling D4): the user's EXPLICIT "answer without my documents"
    // choice. ONLY the literal `true` is ever written, so every documents-on scope — and every
    // pre-P4 row — serializes to exactly the same JSON string as before. It is NEVER derived
    // from an empty selection: `{collectionIds: [], documentIds: [], packIds: ['p']}` without
    // the flag still means "all documents AND pack p". `parseDocumentScope` (collections.ts,
    // the reading owner's explicit whitelist) reads it back.
    ...(scope.documentsOff === true ? { documentsOff: true } : {})
  })
}

function rowToConversation(r: ConversationRow): Conversation {
  return {
    id: r.id,
    title: r.title,
    createdAt: r.created_at,
    updatedAt: r.updated_at,
    modelId: r.model_id,
    mode: r.mode === 'documents' ? 'documents' : 'chat',
    scopeDocumentIds: parseScope(r.scope_json),
    collectionId: r.collection_id ?? null,
    // Composite scope (D1) — tolerant parse, malformed ⇒ null (legacy fallback applies).
    scope: parseDocumentScope(r.scope_v2_json),
    activeSkillId: r.active_skill_id ?? null
  }
}

interface MessageRow {
  id: string
  conversation_id: string
  role: string
  content: string
  created_at: string
  token_count: number | null
  citations_json: string | null
  skill_id: string | null
  /** From the LEFT JOIN on `skills`: NULL when the stamped skill no longer exists (deleted). */
  skill_title: string | null
  /** S13c — 1 when the app auto-fired the stamped skill (else NULL/0). Powers the per-turn undo. */
  auto_fired: number | null
  /** Full-doc-skills Phase 1 — JSON-serialized `CoverageInfo` (D48), or NULL (legacy/no coverage). */
  coverage_json: string | null
  /** 1 when this assistant reply was cut off at a ceiling (finish_reason 'length'); else NULL/0. */
  truncated: number | null
  /** #498 — which ceiling cut it: 'context' | 'cap', or (#994) 'repeat' when the repetition guard stopped a
   *  whole-document answer. NULL on a complete reply and on a pre-#498
   *  truncated row (read back as 'context', the flag's historical meaning). */
  truncated_cause?: string | null
  /** #600 — what ended this turn early ('model' | 'user' | 'lock'), or NULL (complete / pre-#600). */
  ended_early?: string | null
  /** Derived by listMessages (EXISTS over `result_tables`): 1 when a result table is attached
   *  (result-tables plan §4, Phase 2). Absent on other query paths — coalesced to undefined. */
  has_result_table?: number | null
  /** Issue #80 (wave R80) — JSON-serialized `SkillOffer` (id + title + provenance), or NULL (no offer). */
  skill_offer_json?: string | null
  /** #301 P4 — JSON-serialized `KnowledgePackOutcome[]` (this ask's per-pack verdicts), or NULL
   *  (a pre-migration row, or an ask whose scope selected no knowledge pack). */
  pack_outcomes_json?: string | null
}

/**
 * Validate one parsed element against the `Citation` shape: `label`/`sourceTitle` required
 * strings, the rest optional. A structurally wrong-but-valid-JSON payload (e.g. a stale or
 * hand-edited row) must not flow untyped to the renderer (L6) — mirrors `parseScope`.
 */
function isCitation(v: unknown): v is Citation {
  if (typeof v !== 'object' || v === null) return false
  const c = v as Record<string, unknown>
  if (typeof c.label !== 'string' || typeof c.sourceTitle !== 'string') return false
  if (c.pageNumber != null && typeof c.pageNumber !== 'number') return false
  if (c.section != null && typeof c.section !== 'string') return false
  if (c.snippet != null && typeof c.snippet !== 'string') return false
  // EP-1 Phase 0 additive identity fields (plan §5 item 2): optional, tolerant — a legacy
  // row without them passes unchanged; a malformed value rejects the element like the rest.
  if (c.documentId != null && typeof c.documentId !== 'string') return false
  if (c.chunkId != null && typeof c.chunkId !== 'string') return false
  // Knowledge packs (ZIM wave, #294 review M11): the four archive fields follow the SAME
  // tolerant-but-typed rule. Without this the locator flowed untyped to consumers that
  // assume strings (the Markdown export's `.replace`, the chat/review cards) — exactly the
  // L6 hazard the guards above exist to close.
  // A non-string kind is malformed; an UNKNOWN string (a newer build's kind) is kept and reads as
  // a document downstream (every consumer tests `=== 'archive'`) — the tolerant direction.
  if (c.sourceKind != null && typeof c.sourceKind !== 'string') return false
  if (c.packId != null && typeof c.packId !== 'string') return false
  if (c.archiveTitle != null && typeof c.archiveTitle !== 'string') return false
  if (c.articlePath != null && typeof c.articlePath !== 'string') return false
  return true
}

/** Parse stored citations: a JSON array of `Citation`s, else undefined. Validates shape.
 *  Exported for EP-1 (evidence-pack/snapshot.ts): the snapshot builder reads the SAME
 *  `messages.citations_json` payload, so it must parse it with THIS validator verbatim
 *  (the `parseCoverage` export precedent). */
export function parseCitations(json: string | null): Citation[] | undefined {
  if (!json) return undefined
  try {
    const v = JSON.parse(json) as unknown
    if (!Array.isArray(v)) return undefined
    const valid = v.filter(isCitation)
    return valid.length > 0 ? valid : undefined
  } catch {
    // A malformed payload must never break rendering a conversation — drop the citations.
    return undefined
  }
}

/**
 * Parse stored coverage: a JSON object carrying at least a `mode` string, else undefined. Mirrors
 * `parseCitations` — a malformed/legacy (NULL) payload must never break rendering a conversation, so
 * it degrades to undefined and the renderer falls back to the relevance badge. Counts default to 0 so
 * a partially-shaped payload still satisfies the `CoverageInfo` contract.
 * Exported for EP-1 (evidence-reviews.ts): `evidence_reviews.coverage_snapshot_json` holds the
 * same serialized `CoverageInfo` shape, so the snapshot read reuses THIS parser verbatim.
 */
export function parseCoverage(json: string | null): CoverageInfo | undefined {
  if (!json) return undefined
  try {
    const v = JSON.parse(json) as unknown
    if (typeof v !== 'object' || v === null) return undefined
    const c = v as Record<string, unknown>
    if (typeof c.mode !== 'string') return undefined
    // Preserve any extra optional fields (treeStatus, tier, unparsedChunks, …) as-stored, but
    // guarantee the three required keys so a partial payload still satisfies the contract.
    return {
      ...(c as unknown as CoverageInfo),
      mode: c.mode as CoverageInfo['mode'],
      chunksCovered: typeof c.chunksCovered === 'number' ? c.chunksCovered : 0,
      chunksTotal: typeof c.chunksTotal === 'number' ? c.chunksTotal : 0
    }
  } catch {
    // A malformed payload must never break rendering a conversation — drop the coverage.
    return undefined
  }
}

/**
 * Serialize coverage for `messages.coverage_json` (the write side of `parseCoverage`). Null/omitted ⇒
 * NULL (the relevance fallback). Tolerant: a value that cannot stringify (cyclic/exotic) degrades to
 * NULL rather than failing the whole append — the answer itself must always persist.
 */
function serializeCoverage(coverage: CoverageInfo | null | undefined): string | null {
  if (!coverage) return null
  try {
    return JSON.stringify(coverage)
  } catch {
    return null
  }
}

/**
 * Parse a stored per-answer skill offer (issue #80): a JSON `SkillOffer` with a non-empty
 * `installId`/`title` and a known `source`, else undefined. Tolerant like `parseCoverage` — a
 * malformed payload must never break rendering a conversation.
 */
export function parseSkillOffer(json: string | null | undefined): SkillOffer | undefined {
  if (!json) return undefined
  try {
    const v = JSON.parse(json) as unknown
    if (typeof v !== 'object' || v === null) return undefined
    const o = v as Record<string, unknown>
    if (typeof o.installId !== 'string' || o.installId.length === 0) return undefined
    if (typeof o.title !== 'string' || o.title.length === 0) return undefined
    if (o.source !== 'deterministic' && o.source !== 'classifier') return undefined
    return { installId: o.installId, title: o.title, source: o.source }
  } catch {
    return undefined
  }
}

/** Serialize an offer for `messages.skill_offer_json` (the write side of `parseSkillOffer`).
 *  Null/omitted ⇒ NULL; a value that cannot stringify degrades to NULL (the answer always persists). */
function serializeSkillOffer(offer: SkillOffer | null | undefined): string | null {
  if (!offer) return null
  try {
    return JSON.stringify({ installId: offer.installId, title: offer.title, source: offer.source })
  } catch {
    return null
  }
}

/** The `KnowledgePackOutcomeStatus` values a stored row may carry (the write side's whole range). */
const PACK_OUTCOME_STATUSES = new Set<string>(['searched', 'skipped', 'failed'])
/** The `KnowledgePackOutcomeReason` codes a stored row may carry. A row with an UNKNOWN code — a
 *  newer build's reason, or a hand-edited value — is DROPPED rather than shown: the renderer maps
 *  codes to fixed copy, and an unmapped code would render as a blank or raw line. */
const PACK_OUTCOME_REASONS = new Set<string>([
  'selection-limit',
  'removed',
  'disabled',
  'file-missing',
  'identity-mismatch',
  'not-served',
  'not-searchable',
  'tools-missing',
  'mode',
  'search-failed',
  'read-failed',
  'timeout',
  'deadline',
  'server-restarted'
])

/** Coerce a stored count defensively: a finite, non-negative INTEGER, else 0. A hand-edited
 *  `-3`, `1e9.5`, `"7"` or `NaN` must never reach the renderer's plural formatting. */
function packOutcomeCount(v: unknown): number {
  if (typeof v !== 'number' || !Number.isFinite(v)) return 0
  return Math.max(0, Math.floor(v))
}

/**
 * Parse stored per-ask knowledge-pack outcomes (#301 P4, findings M6/M7): a JSON array of
 * `KnowledgePackOutcome`, else undefined. A TOLERANT WHITELIST in the `parseCitations` /
 * `parseCoverage` tradition — a malformed payload must never break rendering a conversation, and
 * an element that does not match the shape is DROPPED rather than flowed untyped to the renderer
 * (the L6 hazard). Module-private: every read of `messages.pack_outcomes_json` goes through
 * `rowToMessage`, so no second parser can drift from this one.
 */
function parsePackOutcomes(json: string | null | undefined): KnowledgePackOutcome[] | undefined {
  if (!json) return undefined
  try {
    const v = JSON.parse(json) as unknown
    if (!Array.isArray(v)) return undefined
    const valid: KnowledgePackOutcome[] = []
    for (const el of v) {
      if (typeof el !== 'object' || el === null) continue
      const o = el as Record<string, unknown>
      if (typeof o.packId !== 'string' || o.packId.length === 0) continue
      if (typeof o.status !== 'string' || !PACK_OUTCOME_STATUSES.has(o.status)) continue
      // `reason` is null exactly for `searched`; any other value must be a KNOWN code.
      const reason = o.reason ?? null
      if (reason !== null && (typeof reason !== 'string' || !PACK_OUTCOME_REASONS.has(reason))) continue
      const title = o.title ?? null
      if (title !== null && typeof title !== 'string') continue
      valid.push({
        packId: o.packId,
        title: title as string | null,
        status: o.status as KnowledgePackOutcome['status'],
        reason: reason as KnowledgePackOutcome['reason'],
        found: packOutcomeCount(o.found),
        admitted: packOutcomeCount(o.admitted)
      })
    }
    return valid.length > 0 ? valid : undefined
  } catch {
    // A malformed payload must never break rendering a conversation — drop the outcomes.
    return undefined
  }
}

/** Serialize outcomes for `messages.pack_outcomes_json` (the write side of `parsePackOutcomes`).
 *  Null/omitted/empty ⇒ NULL; a value that cannot stringify degrades to NULL — best-effort like
 *  `serializeCoverage`, because a metadata fault must never block persisting the ANSWER. */
function serializePackOutcomes(outcomes: KnowledgePackOutcome[] | null | undefined): string | null {
  if (!outcomes || outcomes.length === 0) return null
  try {
    return JSON.stringify(outcomes)
  } catch {
    return null
  }
}

/** Read `messages.truncated_cause` (#498) for a row already known to be truncated. NULL (pre-#498)
 *  or an unrecognized value ⇒ 'context', the flag's historical meaning — so a legacy cut-off reply
 *  keeps exactly the advice it used to show. Only ever called when `truncated === 1`. #994 adds
 *  'repeat' (an answer the app stopped because it repeated itself). */
function parseTruncationCause(raw: string | null | undefined): TruncationCause {
  return raw === 'cap' || raw === 'repeat' ? raw : 'context'
}

function rowToMessage(r: MessageRow): Message {
  const citations = parseCitations(r.citations_json)
  const coverage = parseCoverage(r.coverage_json)
  // Resolve the stamped skill (DS16/§22-A5). SKA-38 (skills audit 2026-07-03, U6): key the provenance
  // off the PERSISTED `messages.skill_id`, NOT the JOIN-resolved title. `skillId` is the raw stamped
  // install id (present even when the skill row was later DELETED); `skillTitle` is the JOINed title,
  // NULL only when the skill is gone. The renderer renders a localized "(removed skill)" label for a
  // NULL-title-but-stamped turn — so deleting a skill no longer erases the glyph + the "answer without
  // it" undo from an already-stamped turn (a DISABLED skill already kept both — this makes DELETED
  // consistent). Auto-fire provenance follows the stamp, not the join.
  const hasStamp = r.skill_id != null
  return {
    id: r.id,
    conversationId: r.conversation_id,
    role: r.role === 'user' || r.role === 'assistant' ? r.role : 'system',
    content: r.content,
    createdAt: r.created_at,
    tokenCount: r.token_count,
    citations,
    skillId: r.skill_id,
    skillTitle: r.skill_title,
    autoFired: hasStamp ? r.auto_fired === 1 : false,
    coverage,
    // Only surface truncation as a positive flag (undefined on complete replies / user turns), so a
    // pre-migration NULL row and a normal reply both read exactly as before.
    truncated: r.truncated === 1 ? true : undefined,
    // #498: the cause rides with the flag. A truncated row written before #498 has no cause stored —
    // it reads as 'context', which is exactly what the pre-#498 badge claimed, so old history keeps
    // its old advice. Anything unrecognized degrades the same way (tolerant read, like coverage).
    truncatedCause: r.truncated === 1 ? parseTruncationCause(r.truncated_cause) : undefined,
    // #600: undefined on every complete turn and every older row (tolerant read).
    endedEarly: parseEndedEarly(r.ended_early),
    // Positive-flag convention (Phase 2 result tables): 1 → true, anything else (incl. query paths
    // that don't compute the EXISTS) → undefined, so older rows and other readers are byte-identical.
    hasResultTable: r.has_result_table === 1 ? true : undefined,
    // #80: the per-answer actionable skill offer — undefined on every row without one (tolerant parse).
    skillOffer: parseSkillOffer(r.skill_offer_json),
    // #301 P4: the ask's per-pack outcomes — undefined on every legacy row and every turn whose
    // scope selected no pack (tolerant whitelist; a malformed payload reads as undefined).
    packOutcomes: parsePackOutcomes(r.pack_outcomes_json)
  }
}

export interface CreateConversationOptions {
  title?: string
  modelId?: string | null
  mode?: 'chat' | 'documents'
  /**
   * "Ask selected documents" scope (spec §10.4): retrieval for this conversation
   * only searches these documents. Null/empty = the whole corpus. Only meaningful
   * for `mode: 'documents'`.
   */
  scopeDocumentIds?: string[] | null
  /**
   * Creation-anchor project (document-organization plan §13.4): the project a chat is
   * started inside. Persisted in `conversations.collection_id`; used for list grouping
   * and as the legacy single-project scope fallback.
   */
  collectionId?: string | null
  /**
   * The persisted composite source scope (D1). When given it is authoritative over the
   * legacy `scopeDocumentIds`/`collectionId` interpretation. An empty scope is the
   * explicit "All documents" choice.
   */
  scope?: DocumentScope | null
}

/** Create a new conversation and persist it. */
export function createConversation(db: Db, opts: CreateConversationOptions = {}): Conversation {
  const now = nowIso()
  const scope = normalizeScope(opts.scopeDocumentIds)
  const collectionId = opts.collectionId ?? null
  const compositeScope = opts.scope ?? null
  const conv: Conversation = {
    id: randomUUID(),
    title: opts.title?.trim() || DEFAULT_TITLE,
    createdAt: now,
    updatedAt: now,
    modelId: opts.modelId ?? null,
    mode: opts.mode ?? 'chat',
    scopeDocumentIds: scope,
    collectionId,
    scope: compositeScope,
    activeSkillId: null
  }
  db.prepare(
    `INSERT INTO conversations (id, title, created_at, updated_at, model_id, mode, scope_json, collection_id, scope_v2_json)
     VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
  ).run(
    conv.id,
    conv.title,
    conv.createdAt,
    conv.updatedAt,
    conv.modelId,
    conv.mode,
    scope ? JSON.stringify(scope) : null,
    collectionId,
    serializeDocumentScope(compositeScope)
  )
  return conv
}

/**
 * Persist a conversation's composite source scope (document-organization plan §8.3/D1) to
 * `scope_v2_json`. Null clears it (back to the legacy/Library interpretation); an empty
 * `DocumentScope` persists as the explicit "All documents" choice. Does NOT touch the
 * legacy `scope_json` (temp attachments never ride it — H4/C3). Returns the updated
 * conversation.
 */
export function setScope(
  db: Db,
  conversationId: string,
  scope: DocumentScope | null
): Conversation {
  const conv = getConversation(db, conversationId)
  if (!conv) throw new Error(`Unknown conversation: ${conversationId}`)
  db.prepare('UPDATE conversations SET scope_v2_json = ? WHERE id = ?').run(
    serializeDocumentScope(scope),
    conversationId
  )
  return { ...conv, scope }
}

/**
 * Persist a conversation's creation-anchor project (plan §13.4) to `conversations.
 * collection_id`. Null clears it (an unscoped/Library chat). Returns the updated
 * conversation.
 */
export function setConversationCollection(
  db: Db,
  conversationId: string,
  collectionId: string | null
): Conversation {
  const conv = getConversation(db, conversationId)
  if (!conv) throw new Error(`Unknown conversation: ${conversationId}`)
  db.prepare('UPDATE conversations SET collection_id = ? WHERE id = ?').run(
    collectionId,
    conversationId
  )
  return { ...conv, collectionId }
}

/**
 * Replace a conversation's document scope (chip removal in the UI). Null/empty clears
 * the scope back to "whole corpus". Returns the updated conversation.
 */
export function updateConversationScope(
  db: Db,
  conversationId: string,
  documentIds: string[] | null
): Conversation {
  const conv = getConversation(db, conversationId)
  if (!conv) throw new Error(`Unknown conversation: ${conversationId}`)
  const scope = normalizeScope(documentIds)
  db.prepare('UPDATE conversations SET scope_json = ? WHERE id = ?').run(
    scope ? JSON.stringify(scope) : null,
    conversationId
  )
  return { ...conv, scopeDocumentIds: scope }
}

/** List conversations, most recently updated first. */
export function listConversations(db: Db): Conversation[] {
  const rows = prepareCached(db, 'SELECT * FROM conversations ORDER BY updated_at DESC, rowid DESC')
    .all() as unknown as ConversationRow[]
  return rows.map(rowToConversation)
}

export function getConversation(db: Db, conversationId: string): Conversation | null {
  const row = prepareCached(db, 'SELECT * FROM conversations WHERE id = ?')
    .get(conversationId) as unknown as ConversationRow | undefined
  return row ? rowToConversation(row) : null
}

/**
 * List a conversation's messages in insertion order. `created_at` may collide at
 * millisecond resolution, so `rowid` is the tiebreaker that guarantees turn order.
 *
 * Compaction checkpoint rows (`kind='compaction'`, context-compaction-plan §4.4) are
 * EXCLUDED: they are machine context, not user turns, so the renderer transcript, export,
 * and skill-fence sizing all see only real user/assistant messages (R8). The summary they
 * hold reaches the model only as the synthetic pair `buildChatMessages` injects.
 */
export function listMessages(db: Db, conversationId: string): Message[] {
  const rows = prepareCached(
    db,
    `SELECT m.*, s.title AS skill_title,
            EXISTS(SELECT 1 FROM result_tables rt WHERE rt.message_id = m.id) AS has_result_table
       FROM messages m LEFT JOIN skills s ON s.install_id = m.skill_id
       WHERE m.conversation_id = ? AND m.kind IS NOT 'compaction'
       ORDER BY m.created_at ASC, m.rowid ASC`
  ).all(conversationId) as unknown as MessageRow[]
  return rows.map(rowToMessage)
}

/**
 * The latest message in a conversation as a full `Message`, or null when it holds no user/assistant
 * turns. A `LIMIT 1` twin of `listMessages` (CB-6): identical skill JOIN, `result_tables` EXISTS,
 * and `kind IS NOT 'compaction'` filter, only ordered DESC — so it is byte-identical to
 * `listMessages(db, conversationId).at(-1)` while reading a single row instead of paging the whole
 * history (used by `buildTurnFence` to size a turn against just the final turn's content).
 */
export function getLatestMessage(db: Db, conversationId: string): Message | null {
  const row = prepareCached(
    db,
    `SELECT m.*, s.title AS skill_title,
            EXISTS(SELECT 1 FROM result_tables rt WHERE rt.message_id = m.id) AS has_result_table
       FROM messages m LEFT JOIN skills s ON s.install_id = m.skill_id
       WHERE m.conversation_id = ? AND m.kind IS NOT 'compaction'
       ORDER BY m.created_at DESC, m.rowid DESC LIMIT 1`
  ).get(conversationId) as unknown as MessageRow | undefined
  return row ? rowToMessage(row) : null
}

/** One stored user/assistant turn with its `rowid` — the assembly/compaction unit (§4.4). */
export interface ConversationTurn {
  rowid: number
  role: 'user' | 'assistant'
  content: string
}

/**
 * A compaction checkpoint (context-compaction-plan §4.4): the cached summary of the older turns
 * it subsumes. `coversThroughRowid` is the max `messages.rowid` the summary replaces — assembly
 * replays only turns after it, and the next compaction folds this summary in (chained, §4.7).
 */
export interface Checkpoint {
  rowid: number
  summary: string
  coversThroughRowid: number
}

/**
 * Read a conversation's user/assistant turns (checkpoint rows excluded) in order, optionally only
 * those with `rowid > afterRowid` — the post-checkpoint replay window. Raw stored content (think
 * blocks are scrubbed by the caller at assembly/summarization time). Powers both the assembly path
 * and the compaction pre-pass; the checkpoint is always built from these STORED RAW turns (R-RAG),
 * never a transient grounded prompt.
 */
export function listConversationTurns(
  db: Db,
  conversationId: string,
  afterRowid = 0
): ConversationTurn[] {
  const rows = prepareCached(
    db,
    `SELECT m.rowid AS rowid, m.role AS role, m.content AS content
       FROM messages m
       WHERE m.conversation_id = ? AND m.rowid > ? AND m.kind IS NOT 'compaction'
       ORDER BY m.created_at ASC, m.rowid ASC`
  ).all(conversationId, afterRowid) as unknown as Array<{
    rowid: number
    role: string
    content: string
  }>
  return rows
    .filter((r) => r.role === 'user' || r.role === 'assistant')
    .map((r) => ({ rowid: r.rowid, role: r.role as 'user' | 'assistant', content: r.content }))
}

/**
 * The most recent USER message text in a conversation, or null when there is none. Used by the skill
 * run IPC to resolve a targeted-edit instruction MAIN-side from the conversation (the user's chat request
 * IS the edit instruction — Phase 8, D76), so no document/chat title crosses the run-state boundary and
 * the content stays main-side (it is scored/used, NEVER logged — the §6 posture, like scope resolution).
 * Excludes `compaction` rows (system summaries, not a user turn).
 */
export function getLatestUserMessage(db: Db, conversationId: string): string | null {
  if (!conversationId) return null
  const row = prepareCached(
    db,
    `SELECT content FROM messages
       WHERE conversation_id = ? AND role = 'user' AND kind IS NOT 'compaction'
       ORDER BY created_at DESC, rowid DESC LIMIT 1`
  ).get(conversationId) as { content: string } | undefined
  return row?.content ?? null
}

/** The latest compaction checkpoint for a conversation, or null when none has been cut. */
export function getLatestCheckpoint(db: Db, conversationId: string): Checkpoint | null {
  const row = prepareCached(
    db,
    `SELECT m.rowid AS rowid, m.content AS content, m.covers_through_rowid AS covers
       FROM messages m
       WHERE m.conversation_id = ? AND m.kind = 'compaction'
       ORDER BY m.rowid DESC LIMIT 1`
  ).get(conversationId) as unknown as
    | { rowid: number; content: string; covers: number | null }
    | undefined
  if (!row) return null
  return { rowid: row.rowid, summary: row.content, coversThroughRowid: row.covers ?? 0 }
}

/**
 * Persist a compaction checkpoint (context-compaction-plan §4.4). A dedicated `kind='compaction'`
 * row (role `system` semantics, never skill-stamped — R3) so history replay is a pure read. Does
 * NOT bump `conversations.updated_at`: a checkpoint is internal context, not a user action, and
 * must not reorder the sidebar.
 */
export function writeCheckpoint(
  db: Db,
  input: { conversationId: string; summary: string; coversThroughRowid: number }
): void {
  prepareCached(
    db,
    `INSERT INTO messages
       (id, conversation_id, role, content, created_at, token_count, citations_json,
        skill_id, auto_fired, coverage_json, kind, covers_through_rowid)
     VALUES (?, ?, 'system', ?, ?, NULL, NULL, NULL, NULL, NULL, 'compaction', ?)`
  ).run(randomUUID(), input.conversationId, input.summary, nowIso(), input.coversThroughRowid)
}

/**
 * Whether conversation compaction is enabled (context-compaction plan §5.4 / D-a, default true).
 * Gates BOTH the `ensureCompacted` pre-pass (no new checkpoints when off) and checkpoint reads in
 * assembly (`buildChatMessages` / `buildGroundedChatMessages` ignore any existing checkpoint when
 * off), so disabling reproduces the pre-feature L1-only behaviour exactly.
 */
export function compactionEnabled(db: Db): boolean {
  return getSettings(db).chatCompactionEnabled !== false
}

/**
 * Where the transcript summary marker sits (context-compaction plan §5.3, D-b). Returns the latest
 * checkpoint's summary text plus the id of the first RENDERED turn it does NOT subsume (the marker
 * renders before that message), or null when no checkpoint exists OR compaction is disabled (so the
 * marker never shows for a checkpoint the assembly is ignoring). `beforeMessageId` is null only when
 * the checkpoint covers every currently-rendered turn (degenerate; the renderer then omits it).
 */
export function getConversationSummaryMarker(
  db: Db,
  conversationId: string
): { summary: string; beforeMessageId: string | null } | null {
  if (!compactionEnabled(db)) return null
  const checkpoint = getLatestCheckpoint(db, conversationId)
  if (!checkpoint) return null
  const row = prepareCached(
    db,
    `SELECT id FROM messages
       WHERE conversation_id = ? AND kind IS NOT 'compaction' AND rowid > ?
       ORDER BY rowid ASC LIMIT 1`
  ).get(conversationId, checkpoint.coversThroughRowid) as unknown as { id: string } | undefined
  return { summary: checkpoint.summary, beforeMessageId: row?.id ?? null }
}

// §4.5 — a compaction summary is injected into the assembled prompt as a synthetic user→assistant
// pair, NOT a second leading `system` message (several local chat templates accept only one leading
// system block, and `collapseToAlternating` assumes leading-system-then-strict-alternation). The
// pair is alternation-safe and keeps the real leading system prompt BYTE-stable so its KV cache
// (`cache_prompt`, RT-2) is still reused. It is built at request time, NEVER persisted as real
// messages, and never skill-stamped (R3). Internal prompt text → English (not user-facing copy).
export const COMPACTION_SUMMARY_INTRO = 'Here is a summary of our earlier conversation so far:'
export const COMPACTION_SUMMARY_ACK = "Understood — I'll continue with that context in mind."

/** Build the synthetic summary pair (§4.5) injected at the start of the retained window. */
export function compactionSummaryPair(summary: string): ChatMessage[] {
  return [
    { role: 'user', content: `${COMPACTION_SUMMARY_INTRO}\n\n${summary}` },
    { role: 'assistant', content: COMPACTION_SUMMARY_ACK }
  ]
}

export interface AppendMessageInput {
  conversationId: string
  role: 'user' | 'assistant'
  content: string
  tokenCount?: number | null
  citations?: Citation[] | null
  /**
   * The skill that shaped THIS turn (skills plan §8.2/DS16) — stamped on the ASSISTANT row only,
   * and only when the fence was actually placed (the glyph corresponds 1:1 to a turn whose prompt
   * carried the skill — §22-A5). Omitted/null ⇒ no skill. No FK into `skills` (audit C3): a deleted
   * skill leaves this id dangling and the glyph read resolves it to NULL.
   */
  skillId?: string | null
  /**
   * S13c — true only when the app AUTO-FIRED `skillId` (the user set no skill). Stamped only alongside
   * a non-null `skillId`; surfaces the per-turn "answer without it" undo. Privacy-safe (a boolean).
   */
  autoFired?: boolean
  /**
   * The honest breadth behind an assistant answer (full-doc-skills plan §3.3/D48), persisted to
   * `messages.coverage_json`. Omitted/null ⇒ NULL, and the renderer falls back to the relevance
   * badge — so today's plain retrieval turns stay byte-identical. Counts/mode only, never content.
   */
  coverage?: CoverageInfo | null
  /**
   * True when this assistant reply was cut off at a token ceiling (finish_reason 'length',
   * §L0 honest-signal). Stamped on assistant rows only; omitted/false ⇒ NULL (complete reply). Never
   * set on a user-initiated Stop (that partial is intentional and user-known, not a length overflow).
   */
  truncated?: boolean
  /**
   * #498 — WHICH ceiling cut it: 'context' (the model ran out of context room) or 'cap' (the app's
   * own fixed per-reply cap). Stored only alongside `truncated: true`; ignored (⇒ NULL) otherwise,
   * so a complete reply can never carry a stray cause. Omitted on a truncated row ⇒ NULL, which the
   * read side resolves to 'context' (the pre-#498 meaning).
   */
  truncatedCause?: TruncationCause
  /**
   * #600 — this turn ended early: on an assistant row the partial was cut, on a user row the question
   * got no answer. Omitted ⇒ NULL. `persistAssistantMessage` stamps the cause itself from the turn's
   * abort reason (a model stop, the Stop button, lock or quit — `chat/ended-early.ts`, #612), so the
   * answer paths need not pass it.
   */
  endedEarly?: EndedEarly
  /**
   * The actionable per-answer skill OFFER (issue #80, wave R80) — persisted to
   * `messages.skill_offer_json`. Assistant rows only; omitted/null ⇒ NULL (no offer — every
   * ordinary answer stays byte-identical). Structural only (id + title + provenance), never content.
   */
  skillOffer?: SkillOffer | null
  /**
   * Knowledge packs (#301 P4, findings M6/M7; owner ruling plan §7) — ONE per-ask outcome for every
   * pack id the ask's resolved scope selected, persisted to `messages.pack_outcomes_json`. Assistant
   * rows only; omitted/null/empty ⇒ NULL, so every pack-less chat stays byte-identical. Serialized
   * BEST-EFFORT like coverage: a fault degrades to NULL and never blocks persisting the answer.
   */
  packOutcomes?: KnowledgePackOutcome[] | null
}

/** Append a message and bump the conversation's updated_at. */
export function appendMessage(db: Db, input: AppendMessageInput): Message {
  const now = nowIso()
  const tokenCount = input.tokenCount ?? null
  const citationsJson = input.citations ? JSON.stringify(input.citations) : null
  // Coverage is best-effort metadata: a serialization fault must never block persisting the answer
  // itself (the round-trip read is already tolerant). Stringify defensively → NULL on any failure.
  const coverageJson = serializeCoverage(input.coverage)
  const skillId = input.skillId ?? null
  // Stamp auto-fire provenance only when a skill is actually stamped; 1 = auto-fired, NULL otherwise.
  const autoFired = skillId != null && input.autoFired === true
  const truncated = input.truncated === true
  // #498: the cause is meaningful only on a truncated row — a complete reply always stores NULL.
  const truncatedCause = truncated ? (input.truncatedCause ?? null) : null
  const endedEarly = input.endedEarly ?? null
  // #80: best-effort like coverage — a serialization fault degrades to NULL, never blocks the answer.
  const skillOfferJson = serializeSkillOffer(input.skillOffer)
  // #301 P4: same best-effort posture — the ANSWER must persist even if the metadata cannot.
  const packOutcomesJson = serializePackOutcomes(input.packOutcomes)
  const msg: Message = {
    id: randomUUID(),
    conversationId: input.conversationId,
    role: input.role,
    content: input.content,
    createdAt: now,
    tokenCount,
    citations: input.citations ?? undefined,
    skillId,
    autoFired,
    coverage: input.coverage ?? undefined,
    truncated: truncated ? true : undefined,
    // Mirror the read side exactly: a truncated row with no stored cause reads back as 'context'.
    truncatedCause: truncated ? (truncatedCause ?? 'context') : undefined,
    endedEarly: endedEarly ?? undefined,
    skillOffer: skillOfferJson != null ? (input.skillOffer ?? undefined) : undefined,
    // Mirrors the row: undefined when nothing was written (no pack in scope, or a serialization
    // fault), so the returned Message and a later `listMessages` read agree.
    packOutcomes: packOutcomesJson != null ? (input.packOutcomes ?? undefined) : undefined
  }
  prepareCached(
    db,
    `INSERT INTO messages (id, conversation_id, role, content, created_at, token_count, citations_json, skill_id, auto_fired, coverage_json, truncated, truncated_cause, skill_offer_json, pack_outcomes_json, ended_early)
     VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
  ).run(
    msg.id,
    msg.conversationId,
    msg.role,
    msg.content,
    msg.createdAt,
    tokenCount,
    citationsJson,
    skillId,
    autoFired ? 1 : null,
    coverageJson,
    truncated ? 1 : null,
    truncatedCause,
    skillOfferJson,
    packOutcomesJson,
    endedEarly
  )
  prepareCached(db, 'UPDATE conversations SET updated_at = ? WHERE id = ?').run(
    now,
    input.conversationId
  )
  return msg
}

/**
 * Attach a per-answer skill OFFER to an already-persisted message (issue #80). Used by the
 * document-answer path when the answer was persisted deep inside `generateGroundedAnswer` and the
 * offer is decided at the call site — an update-in-place of `messages.skill_offer_json` only, so
 * the answer content and every other column stay byte-untouched. Best-effort: a null/unserializable
 * offer is a no-op (the column stays NULL).
 */
export function setMessageSkillOffer(db: Db, messageId: string, offer: SkillOffer | null | undefined): void {
  const json = serializeSkillOffer(offer)
  if (json == null) return
  prepareCached(db, 'UPDATE messages SET skill_offer_json = ? WHERE id = ?').run(json, messageId)
}

/**
 * Read a conversation's STICKY DEFAULT skill (`conversations.active_skill_id`, skills plan §10.1) —
 * the install_id pre-filled for the next turn, or null when none is set. Not a hard pin: any turn
 * can override or clear it, and past turns keep their own stamped `messages.skill_id`.
 */
export function getConversationDefaultSkill(db: Db, conversationId: string): string | null {
  const row = db
    .prepare('SELECT active_skill_id FROM conversations WHERE id = ?')
    .get(conversationId) as unknown as { active_skill_id: string | null } | undefined
  return row?.active_skill_id ?? null
}

/**
 * Persist a conversation's sticky default skill (skills plan §10.1). Null clears it. No existence
 * check on the skill here — the resolver (`resolveTurnSkill`) skips a disabled/missing default
 * gracefully, and the IPC layer validates before calling this.
 */
export function setConversationDefaultSkill(
  db: Db,
  conversationId: string,
  installId: string | null
): void {
  db.prepare('UPDATE conversations SET active_skill_id = ? WHERE id = ?').run(
    installId ?? null,
    conversationId
  )
}

/**
 * One `result_tables` row captured alongside a deleted message, column-for-column, so
 * `restoreMessage` can replay it byte-exactly: same id, same serialized columns/rows payload,
 * same row_count/source/created_at. Nothing is re-derived — the table that comes back is the
 * table that was there. Columns/rows are CONTENT (never logged or audited).
 */
export interface DeletedResultTable {
  readonly id: string
  readonly messageId: string
  readonly conversationId: string
  readonly columnsJson: string
  readonly rowsJson: string
  readonly rowCount: number
  readonly source: string | null
  readonly createdAt: string
}

/**
 * A snapshot of a deleted message, sufficient to re-insert it byte-faithfully via
 * `restoreMessage`. Powers the regenerate data-loss guard (F2, post-merge audit): the
 * destructive regenerate delete runs only after the stream slot is held, and the prior reply
 * is restored from this snapshot if generation then fails with nothing saved in its place — so a
 * failed regenerate never leaves the turn answer-less.
 *
 * It also carries the message's `result_tables` rows. `result_tables.message_id` is a foreign key
 * with ON DELETE CASCADE and the workspace runs with `PRAGMA foreign_keys = ON`, so the
 * regenerate `DELETE FROM messages` takes the answer's structured table (the artifact behind the
 * message-level "Export CSV" action) with it. Dropping it is CORRECT when a regenerate succeeds —
 * the answer is genuinely replaced and the new one brings its own table — but on the legs that
 * put the OLD answer back (a failed generation, a Stop before the first token, and since #600 a
 * model stop that cut the re-ask) the reply used to return with its table permanently gone: the derived `hasResultTable`
 * EXISTS join then read false and the Export affordance silently vanished from a turn the app had
 * just reported as fully restored, with no way to reproduce it short of re-running the model.
 * Capturing the rows here keeps those legs' "nothing is lost" contract honest.
 */
export interface DeletedMessage {
  readonly id: string
  readonly conversationId: string
  readonly role: string
  readonly content: string
  readonly createdAt: string
  readonly tokenCount: number | null
  readonly citationsJson: string | null
  readonly skillId: string | null
  readonly autoFired: number | null
  readonly coverageJson: string | null
  readonly kind: string | null
  readonly coversThroughRowid: number | null
  readonly truncated: number | null
  /** #498 — the truncation CAUSE, captured verbatim so a restore cannot silently downgrade a
   *  'cap' reply to the legacy 'context' reading (the `skillOfferJson` class of bug). */
  readonly truncatedCause: string | null
  /** #132/#135 (skills-pipeline audit): the persisted per-answer skill OFFER (#80). Omitting it from
   *  the snapshot silently stripped the offer row on BOTH restore legs (F2 + CB-2) — the exact class
   *  the result-table capture below exists for. */
  readonly skillOfferJson: string | null
  /** #301 P4 — the per-ask knowledge-pack outcomes, captured VERBATIM as stored (the same class as
   *  `skillOfferJson`: omit it and a regenerate that later restores the prior reply would silently
   *  strip the answer's pack record). Replayed byte-identically by `restoreMessage`. */
  readonly packOutcomesJson: string | null
  /** #600 — `ended_early`, captured verbatim like the cause above: a regenerate that restores a
   *  stopped reply must keep its "Reply stopped" marker. */
  readonly endedEarly: string | null
  /** The message's result tables, oldest-first. Empty for the common table-less answer. */
  readonly resultTables: readonly DeletedResultTable[]
}

interface DeletedMessageRow {
  id: string
  conversation_id: string
  role: string
  content: string
  created_at: string
  token_count: number | null
  citations_json: string | null
  skill_id: string | null
  auto_fired: number | null
  coverage_json: string | null
  kind: string | null
  covers_through_rowid: number | null
  truncated: number | null
  truncated_cause: string | null
  skill_offer_json: string | null
  pack_outcomes_json: string | null
  ended_early: string | null
}

interface ResultTableRow {
  id: string
  message_id: string
  conversation_id: string
  columns_json: string
  rows_json: string
  row_count: number
  source: string | null
  created_at: string
}

/**
 * The id of the message a "regenerate" would DESTROY: the conversation's last VISIBLE message
 * when — and only when — it is an assistant turn; null otherwise. Read-only.
 *
 * The single owner of the "which row does regenerate target?" question. `hasRegenerableAssistantReply`
 * (the pre-stream bail) is expressed in terms of it, and `deleteLastAssistantMessage` runs the same
 * SELECT, so the precondition, the in-stream guards and the delete cannot drift apart into
 * disagreeing about the target row. Callers that must inspect what hangs off that row before the
 * delete commits (the evidence-review guard, AUD-01) need the ID, not just a boolean.
 */
export function getRegenerableAssistantMessageId(db: Db, conversationId: string): string | null {
  // `kind IS NOT 'compaction'`: checkpoint rows are invisible transcript bookkeeping (they carry
  // role 'assistant' but are never a reply) — see deleteLastAssistantMessage (CODE-19 rider).
  const row = db
    .prepare(
      `SELECT id, role FROM messages WHERE conversation_id = ? AND kind IS NOT 'compaction'
       ORDER BY created_at DESC, rowid DESC LIMIT 1`
    )
    .get(conversationId) as unknown as { id: string; role: string } | undefined
  return row?.role === 'assistant' ? row.id : null
}

/**
 * #600 — mark the question that a turn left unanswered: the conversation's last VISIBLE message,
 * when — and only when — it is a user turn (an answer that persisted, or a restored one, makes the
 * tail an assistant row and nothing is marked). The transcript then shows "Not answered — …" under
 * it, naming the cause (a model stop; since #612 also the Stop button, lock or quit), instead of a
 * silently unanswered question. Returns whether a row was marked.
 */
export function markUnansweredQuestion(db: Db, conversationId: string, cause: EndedEarly): boolean {
  const row = db
    .prepare(
      `SELECT id, role FROM messages WHERE conversation_id = ? AND kind IS NOT 'compaction'
       ORDER BY created_at DESC, rowid DESC LIMIT 1`
    )
    .get(conversationId) as unknown as { id: string; role: string } | undefined
  if (row?.role !== 'user') return false
  db.prepare('UPDATE messages SET ended_early = ? WHERE id = ?').run(cause, row.id)
  return true
}

/**
 * #613 — the question "Send again" answers: the conversation's last VISIBLE message when it is a user
 * turn (an error, a crash or an early end left it without an answer), else null. Unlike the "Not
 * answered" note it does not need an `endedEarly` mark: an error leaves none, and its banner is gone
 * after a reload. Read-only; the same tail the regenerate and mark helpers above read.
 */
export function getUnansweredQuestion(db: Db, conversationId: string): Message | null {
  const last = getLatestMessage(db, conversationId)
  return last?.role === 'user' ? last : null
}

/**
 * #613 — drop the "Not answered" mark from the question an attempt is about to answer, so the mark always
 * describes the LATEST attempt: that attempt marks it again if it too ends early, and an error leaves the
 * question unmarked like any other failed turn. Only the last visible row, only a user turn (the tail
 * `getUnansweredQuestion` reads). Runs at the start of every turn; a plain send's fresh question carries
 * no mark and a regenerate's tail is an answer, so today it only ever clears a resent question.
 */
export function clearUnansweredMark(db: Db, conversationId: string): void {
  const question = getUnansweredQuestion(db, conversationId)
  if (question?.endedEarly == null) return
  db.prepare('UPDATE messages SET ended_early = NULL WHERE id = ?').run(question.id)
}

/**
 * Read-only precondition for "regenerate": is the conversation's last message an assistant turn
 * (so there is a prior reply to drop and re-stream)? Mirrors `deleteLastAssistantMessage`'s
 * last-message-must-be-assistant rule so the pre-stream "nothing to regenerate" bail and the
 * in-stream delete (F2) agree. Non-destructive — safe to call before the stream slot is held.
 */
export function hasRegenerableAssistantReply(db: Db, conversationId: string): boolean {
  return getRegenerableAssistantMessageId(db, conversationId) != null
}

/**
 * Remove the conversation's last message IF it is an assistant turn (used by "regenerate") and
 * return a snapshot `restoreMessage` can re-insert byte-faithfully; returns null when the last
 * message is not an assistant turn (nothing deleted).
 *
 * Deliberately scoped to the LAST message, not the last *assistant* message: after
 * a failed generation the conversation ends in a user turn — deleting the most recent
 * assistant message would then permanently destroy the answer to a *previous* question.
 * In that case regenerate just re-streams from history without deleting anything.
 *
 * CODE-19 rider (full-audit 2026-07-11, Phase-D observation): checkpoint rows are EXCLUDED
 * (`kind IS NOT 'compaction'` — the listConversationTurns predicate). A compaction checkpoint also
 * carries role 'assistant', so if generation failed right after the pre-pass persisted one, the
 * checkpoint sat at the conversation tail and a regenerate deleted IT instead of the prior answer
 * (and `hasRegenerableAssistantReply` disagreed with what the user sees — the checkpoint is
 * invisible in the transcript). Both queries now look at the last VISIBLE message.
 *
 * The snapshot also captures the message's `result_tables` rows BEFORE the delete, because that
 * delete CASCADEs into them (see `DeletedMessage`). `message_id` there carries only an index, not
 * a UNIQUE constraint, so every row is captured — ordered `created_at ASC, rowid ASC` so the
 * replay re-creates the same relative order, which is what `loadResultTable`'s newest-first read
 * resolves against.
 */
export function deleteLastAssistantMessage(db: Db, conversationId: string): DeletedMessage | null {
  const row = db
    .prepare(
      `SELECT id, conversation_id, role, content, created_at, token_count, citations_json,
              skill_id, auto_fired, coverage_json, kind, covers_through_rowid, truncated,
              truncated_cause, skill_offer_json, pack_outcomes_json, ended_early
       FROM messages WHERE conversation_id = ? AND kind IS NOT 'compaction'
       ORDER BY created_at DESC, rowid DESC LIMIT 1`
    )
    .get(conversationId) as unknown as DeletedMessageRow | undefined
  if (!row || row.role !== 'assistant') return null
  // Read the cascade-doomed rows first — after the DELETE they are unrecoverable. Indexed on
  // message_id and empty for the vast majority of answers, so this costs a single index probe.
  const tableRows = prepareCached(
    db,
    `SELECT id, message_id, conversation_id, columns_json, rows_json, row_count, source, created_at
       FROM result_tables WHERE message_id = ? ORDER BY created_at ASC, rowid ASC`
  ).all(row.id) as unknown as ResultTableRow[]
  db.prepare('DELETE FROM messages WHERE id = ?').run(row.id)
  return {
    id: row.id,
    conversationId: row.conversation_id,
    role: row.role,
    content: row.content,
    createdAt: row.created_at,
    tokenCount: row.token_count,
    citationsJson: row.citations_json,
    skillId: row.skill_id,
    autoFired: row.auto_fired,
    coverageJson: row.coverage_json,
    kind: row.kind,
    coversThroughRowid: row.covers_through_rowid,
    truncated: row.truncated,
    truncatedCause: row.truncated_cause,
    skillOfferJson: row.skill_offer_json,
    packOutcomesJson: row.pack_outcomes_json,
    endedEarly: row.ended_early,
    resultTables: tableRows.map((t) => ({
      id: t.id,
      messageId: t.message_id,
      conversationId: t.conversation_id,
      columnsJson: t.columns_json,
      rowsJson: t.rows_json,
      rowCount: t.row_count,
      source: t.source,
      createdAt: t.created_at
    }))
  }
}

/**
 * Re-insert a previously-deleted message exactly (same id, timestamp, citations, coverage, skill
 * stamp) together with any result tables that hung off it. Restores a regenerate's prior reply
 * after a failed generation (F2), a Stop before the first token (CB-2), or a model stop that cut
 * the re-ask (#600). The row keeps
 * its original `created_at`, so it sorts back to the tail of the transcript; the FTS triggers
 * re-index it on insert. A fresh rowid is assigned (rowid is identity-free here — no checkpoint
 * coverage points at a tail assistant reply).
 *
 * Ordering is load-bearing: the message row goes in FIRST so `result_tables.message_id` — a
 * foreign key under `PRAGMA foreign_keys = ON` — resolves when the table rows follow. The table
 * replay is best-effort and never allowed to fail the restore: the ANSWER is the load-bearing
 * part, and a table row that somehow refuses to re-insert must leave the user with the answer
 * back rather than with nothing (the `saveResultTable` posture, where a table that cannot persist
 * never blocks the answer either). Only ids/counts are logged if that happens — never content.
 */
export function restoreMessage(db: Db, m: DeletedMessage): void {
  prepareCached(
    db,
    `INSERT INTO messages
       (id, conversation_id, role, content, created_at, token_count, citations_json,
        skill_id, auto_fired, coverage_json, kind, covers_through_rowid, truncated,
        truncated_cause, skill_offer_json, pack_outcomes_json, ended_early)
     VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
  ).run(
    m.id,
    m.conversationId,
    m.role,
    m.content,
    m.createdAt,
    m.tokenCount,
    m.citationsJson,
    m.skillId,
    m.autoFired,
    m.coverageJson,
    m.kind,
    m.coversThroughRowid,
    m.truncated,
    m.truncatedCause,
    m.skillOfferJson,
    m.packOutcomesJson,
    m.endedEarly
  )
  if (m.resultTables.length === 0) return
  try {
    const insert = prepareCached(
      db,
      `INSERT INTO result_tables
         (id, message_id, conversation_id, columns_json, rows_json, row_count, source, created_at)
       VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
    )
    for (const t of m.resultTables) {
      insert.run(
        t.id,
        t.messageId,
        t.conversationId,
        t.columnsJson,
        t.rowsJson,
        t.rowCount,
        t.source,
        t.createdAt
      )
    }
  } catch {
    log.warn('Result-table restore failed', { messageId: m.id, tables: m.resultTables.length })
  }
}

/**
 * Delete a conversation and all of its messages (chat and document Q&A alike — a
 * documents conversation only references documents via persisted citations, so the
 * documents/chunks/embeddings tables are untouched). Messages go first: the FK has
 * no ON DELETE CASCADE and foreign_keys is ON.
 *
 * Atomicity (REL-4, full audit 2026-06-28): both deletes run in ONE transaction so a crash /
 * lock / SQLITE_BUSY past the 5 s busy_timeout BETWEEN them rolls back instead of leaving the
 * messages gone but the conversation row surviving — an empty thread that can't be repopulated
 * (compaction checkpoint rows live in `messages` too). Mirrors `deleteDocument`'s DATA-1 wrap.
 * Returns true when a conversation row was deleted.
 */
export function deleteConversation(db: Db, conversationId: string): boolean {
  db.exec('BEGIN')
  try {
    db.prepare('DELETE FROM messages WHERE conversation_id = ?').run(conversationId)
    const result = db.prepare('DELETE FROM conversations WHERE id = ?').run(conversationId)
    db.exec('COMMIT')
    return Number(result.changes) > 0
  } catch (err) {
    try {
      db.exec('ROLLBACK')
    } catch {
      /* keep the original failure as the thrown error */
    }
    throw err
  }
}

/** Most message hits one search returns (across all conversations). */
const SEARCH_DEFAULT_LIMIT = 40

/** snippet() context: tokens shown around the matched term(s). */
const SNIPPET_CONTEXT_TOKENS = 12

interface SearchRow {
  conversationId: string
  conversationTitle: string
  messageId: string
  role: string
  snippet: string
  createdAt: string
}

/**
 * Full-text search across all conversations. The query is sanitized through the
 * shared `buildFtsMatchQuery` (FTS5 operators in user text never reach MATCH raw);
 * hits are ranked bm25 with a newest-first tie-break and grouped by conversation,
 * conversations ordered by their best hit. Snippets come from FTS5's snippet(),
 * matched terms wrapped in the SEARCH_MARK_* control characters for renderer-side
 * highlighting.
 *
 * Privacy: queries and snippets are CONTENT — callers must never log or audit them.
 */
export function searchMessages(
  db: Db,
  query: string,
  limit: number = SEARCH_DEFAULT_LIMIT
): ConversationSearchResult[] {
  if (limit <= 0) return []
  const match = buildFtsMatchQuery(query)
  if (!match) return []
  const rows = db
    .prepare(
      `SELECT m.conversation_id AS conversationId, c.title AS conversationTitle,
              m.id AS messageId, m.role AS role, m.created_at AS createdAt,
              snippet(messages_fts, 0, ?, ?, '…', ${SNIPPET_CONTEXT_TOKENS}) AS snippet
       FROM messages_fts
       JOIN messages m ON m.id = messages_fts.message_id
       JOIN conversations c ON c.id = m.conversation_id
       WHERE messages_fts MATCH ?
       ORDER BY bm25(messages_fts), m.created_at DESC, m.rowid DESC
       LIMIT ?`
    )
    .all(SEARCH_MARK_START, SEARCH_MARK_END, match, limit) as unknown as SearchRow[]

  // Group by conversation, preserving rank order: a conversation appears where its
  // best hit ranked, and hits within it stay in their own (bm25, newest-first) order.
  const byConversation = new Map<string, ConversationSearchResult>()
  for (const row of rows) {
    const hit: ConversationSearchHit = {
      messageId: row.messageId,
      role: row.role === 'user' ? 'user' : 'assistant',
      snippet: row.snippet,
      createdAt: row.createdAt
    }
    const existing = byConversation.get(row.conversationId)
    if (existing) {
      existing.hits.push(hit)
    } else {
      byConversation.set(row.conversationId, {
        conversationId: row.conversationId,
        conversationTitle: row.conversationTitle,
        hits: [hit]
      })
    }
  }
  return [...byConversation.values()]
}

/**
 * Set the conversation title from its first user message when it is still the
 * default. Keeps the sidebar readable without a separate "rename" step.
 */
export function maybeSetTitleFromFirstMessage(db: Db, conversationId: string, content: string): void {
  const conv = getConversation(db, conversationId)
  if (!conv || conv.title !== DEFAULT_TITLE) return
  // Code-point-safe cut (the truncateSnippet idiom, rag/index.ts — RAG-2; full-audit 2026-07-11
  // CODE-17): a raw `.slice(0, 60)` counts UTF-16 units and can cut inside a surrogate pair
  // (emoji, CJK ext-B), persisting a title that forever ends in `�` in the sidebar. Spreading
  // iterates whole code points, so the cut always lands on a code-point boundary.
  const normalized = content.trim().replace(/\s+/g, ' ')
  const title = [...normalized].slice(0, 60).join('') || DEFAULT_TITLE
  db.prepare('UPDATE conversations SET title = ? WHERE id = ?').run(title, conversationId)
}

/**
 * Render a conversation as a Markdown transcript for export (spec §7.6 "export chat
 * transcript"). Pure string assembly; the IPC layer handles the save dialog.
 */
export function exportTranscript(db: Db, conversationId: string): { title: string; markdown: string } {
  const conv = getConversation(db, conversationId)
  if (!conv) throw new Error(`Unknown conversation: ${conversationId}`)
  const messages = listMessages(db, conversationId)

  const lines: string[] = []
  lines.push(`# ${conv.title}`)
  lines.push('')
  lines.push(`_Exported from HilbertRaum on ${nowIso()} — local transcript, never uploaded._`)
  lines.push('')
  for (const m of messages) {
    lines.push(`## ${m.role === 'user' ? 'You' : 'Assistant'} (${m.createdAt})`)
    lines.push('')
    lines.push(m.content)
    if (m.citations && m.citations.length > 0) {
      lines.push('')
      lines.push('Sources:')
      for (const c of m.citations) {
        const where =
          c.pageNumber != null ? `, p. ${c.pageNumber}` : c.section ? `, ${c.section}` : ''
        if (c.sourceKind === 'archive') {
          // ZIM wave (#294 review M11): preserve archive provenance — the pack title and
          // the stable pack UUID/entry-path locator — even though identity stays unresolved
          // (H2). Missing locator values are OMITTED, never invented; `\r`/`\n` are stripped
          // from every untrusted locator field so a hostile value cannot break the line.
          const clean = (v: string | null | undefined): string | null => {
            const stripped = (v ?? '').replace(/[\r\n]+/g, ' ').trim()
            return stripped.length > 0 ? stripped : null
          }
          const archiveTitle = clean(c.archiveTitle)
          const packId = clean(c.packId)
          const articlePath = clean(c.articlePath)
          const locator: string[] = []
          if (archiveTitle) locator.push(`knowledge pack: ${archiveTitle}`)
          if (packId) locator.push(`pack id ${packId}`)
          if (articlePath) locator.push(`article ${articlePath}`)
          const suffix = locator.length > 0 ? ` — ${locator.join('; ')}` : ''
          lines.push(`- [${c.label}] ${c.sourceTitle}${where}${suffix}`)
        } else {
          lines.push(`- [${c.label}] ${c.sourceTitle}${where}`)
        }
      }
    }
    // Knowledge packs (#301 P4, findings M6/M7): what happened to every pack this ask selected —
    // AFTER the "Sources:" block, or on its own when the answer cited nothing (a zero-citation or
    // no-context answer is exactly where "your three packs were all unavailable" matters most).
    // English machine text like the rest of the export; `\r`/`\n` are stripped from the stored
    // title the way the M11 archive locator line strips them, so no value can break the line.
    if (m.packOutcomes && m.packOutcomes.length > 0) {
      lines.push('')
      lines.push('Knowledge packs:')
      for (const o of m.packOutcomes) {
        const title = (o.title ?? '').replace(/[\r\n]+/g, ' ').trim()
        const name = title.length > 0 ? title : `pack ${o.packId}`
        const reason = o.reason ? ` (${o.reason})` : ''
        lines.push(`- ${name}: ${o.status}${reason} — ${o.admitted} passage(s)`)
      }
    }
    lines.push('')
  }
  return { title: conv.title, markdown: lines.join('\n') }
}

// ---- Context-window budget (chat + RAG prompt assembly) -----------------------------
// The chat and grounded-answer message lists replay the WHOLE persisted history. Left
// unbounded, an accumulating conversation (or a single grounded turn carrying a large
// retrieved-chunk block) eventually assembles a prompt larger than the model's context
// window, and llama-server rejects the request with HTTP 400 `exceed_context_size_error`
// — the prompt never even reaches generation. `fitMessagesToContext` is the single owner
// of trimming the history to fit, used by both `buildChatMessages` (plain chat) and
// `buildGroundedChatMessages` (RAG). Doc-task windows have their own context budgets
// (doctasks/summary.ts); this covers the conversational path those budgets never touched.

/** Model tokens reserved for the streamed answer + chat-template chrome (so a fitted
 *  prompt still leaves room to generate; below this an answer would be truncated). */
export const CHAT_RESPONSE_RESERVE_TOKENS = 1024
/**
 * DESIRED output reserve for a whole-document ANALYSIS deliverable — the streamed map-reduce reduce
 * step (whole-doc engine, wholedoc-truncation-fix-plan §4 / Phase 2). A structured brief (a 9-section
 * contract analysis, minutes, a share-safe review) needs far more room than a conversational reply, so
 * the reduce aims for this instead of `CHAT_RESPONSE_RESERVE_TOKENS`. It is only a TARGET: the reduce
 * budget (`computeReduceBudget`, whole-doc-tree.ts) yields it back toward `CHAT_RESPONSE_RESERVE_TOKENS`
 * (never below — never worse than today) so the actual notes + this cap provably fit the launched
 * `n_ctx` at every context size. On a large window (≥ ~8 k) the deliverable gets the full reserve; on a
 * small window (4 k) it shrinks so whole-document coverage survives (the "output cut" residual — Phase 4).
 */
export const ANALYSIS_RESPONSE_RESERVE_TOKENS = 3072
/** Base real-tokens-per-whitespace-word rate for typical English/Latin prose. */
const CHAT_TOKENS_PER_WORD = 1.3
/**
 * Subword-density safety multiplier on top of the base word rate. A German machine-generated reply
 * tokenizes at ~1.5–2 real BPE tokens/word, so the 1.3 base UNDER-counts it: `fitMessagesToContext`
 * then keeps too much history, the real prompt runs larger than estimated, and the balanced path
 * (which sends no `max_tokens`) truncates the answer mid-word at the context ceiling — the exact
 * failure in the D:\ testing report (2026-07-01). Mirrors the RAG grounded-answer budget's ÷1.5
 * German safety (rag-design §15.1; this chat-side mirror is recorded in §15.7): leaning the whole
 * chat budget conservative makes German trim +
 * compact a touch sooner (and the usage meter read truthfully high) rather than overflow. English
 * reads slightly high — acceptable for a meter that is labelled approximate and designed to warn
 * before the cliff. Effective rate: 1.3 × 1.5 ≈ 1.95 real tokens/word. */
const CHAT_TOKENS_PER_WORD_SAFETY = 1.5
/** Per-message overhead for role markers / delimiters the chat template adds. */
const PER_MESSAGE_OVERHEAD_TOKENS = 8

/**
 * Estimated model tokens for one message (word estimate scaled up + template chrome).
 *
 * RAG-7 (perf audit 2026-06-18): memoized by message-object identity. The count is a pure
 * function of `m.content` (never mutated), and the same `ChatMessage` objects are summed several
 * times within one turn — `fitMessagesToContext` costs each, then `getConversationContextUsage`
 * re-sums the SAME objects it returns — so a `WeakMap` cache skips the repeated `approxTokenCount`
 * regex/split scans while staying byte-identical (and GC'ing with the messages).
 */
const messageTokenCache = new WeakMap<ChatMessage, number>()
export function messageTokens(m: ChatMessage): number {
  const cached = messageTokenCache.get(m)
  if (cached !== undefined) return cached
  const tokens =
    Math.ceil(approxTokenCount(m.content) * CHAT_TOKENS_PER_WORD * CHAT_TOKENS_PER_WORD_SAFETY) +
    PER_MESSAGE_OVERHEAD_TOKENS
  messageTokenCache.set(m, tokens)
  return tokens
}

/**
 * The REAL context-window budget for prompt assembly (§L0). The runtime is launched with
 * `launchContextTokens` (the user's `contextTokensOverride`, else
 * `manifest.recommendedContextTokens || settings.contextTokens`) as llama-server's
 * `--ctx-size`, which can DIVERGE from `settings.contextTokens` — trimming against the
 * setting alone risks an over-window HTTP 400 (window larger than the setting → we'd trim
 * too tight, wasting capacity) or, the other way, an overflow. So we budget against the
 * value the runtime actually reports, falling back to the override, then the setting, only
 * when a running runtime can't report one (a bare test stub without `contextWindow()`). With
 * no model running, callers take `contextWindowOrNextLaunch` below instead (#779).
 */
export function effectiveContextWindow(
  runtime: Pick<ModelRuntime, 'contextWindow'>,
  settings: { contextTokens: number; contextTokensOverride?: number | null }
): number {
  const reported = runtime.contextWindow?.()
  if (reported != null && reported > 0) return reported
  // No launched window to report (a bare test stub): honour the user's context-size override
  // before the legacy fallback. This runtime-only fallback has no manifest to read; a caller with
  // no model running uses `contextWindowOrNextLaunch`, which does.
  return settings.contextTokensOverride ?? settings.contextTokens
}

/**
 * The context window to budget against when a model may not be running: the running runtime's
 * launched window (`effectiveContextWindow`), else the window the NEXT start launches with —
 * `launchContextTokens` over the ACTIVE model's manifest (`settings.activeModelId`), the one
 * precedence `startModelRuntime` uses. With no manifest found (no manifests dir, a null or unknown
 * active id) that is the user's override, else the legacy setting. The ONE spelling of this rule:
 * the composer meter (`getConversationContextUsage`, #779) and the no-runtime doc-task budget
 * (main/index.ts, full-audit 2026-07-10 BE-5) both take it, so neither plans against a window the
 * next start will not use.
 */
export function contextWindowOrNextLaunch(
  runtime: Pick<ModelRuntime, 'contextWindow'> | null,
  settings: { contextTokens: number; contextTokensOverride?: number | null; activeModelId: string | null },
  manifestsDir: string | null
): number {
  if (runtime) return effectiveContextWindow(runtime, settings)
  return launchContextTokens(settings, findManifestById(manifestsDir, settings.activeModelId))
}

/**
 * Resting-state context-window usage for the composer meter (context-compaction plan §5.1). Pure
 * read, no model call: assembles the conversation exactly as a turn would (`buildChatMessages` —
 * which honours the compaction toggle + any checkpoint) over the real launched `window`, then sums
 * the per-message ESTIMATE. `usedTokens` is therefore the same over-counting estimate the budget
 * uses (labelled approximate in the UI). `runtime` may be null (no model running) — the window is
 * then the one the next start launches with (`contextWindowOrNextLaunch` over `manifestsDir`,
 * #779), so the resting meter fills against the window the next turn will really have. The skill
 * fence is intentionally omitted: at rest there is no pending turn/skill, and the meter is
 * approximate.
 */
export function getConversationContextUsage(
  db: Db,
  runtime: Pick<ModelRuntime, 'contextWindow'> | null,
  manifestsDir: string | null,
  conversationId: string
): ContextUsage {
  const settings = getSettings(db)
  const window = contextWindowOrNextLaunch(runtime, settings, manifestsDir)
  const messages = buildChatMessages(db, conversationId, window)
  const usedTokens = messages.reduce((sum, m) => sum + messageTokens(m), 0)
  return { usedTokens, window }
}

/**
 * Trim an assembled (already role-alternating) message list to fit `contextTokens`,
 * keeping a contiguous, most-recent suffix of the conversation. Invariants:
 *   - Every leading `system` message is always kept (and counted) — the instructions.
 *   - The FINAL turn is always kept, even if it alone exceeds the budget: it is the
 *     user's current question (or the grounded prompt). Dropping it would answer the
 *     wrong thing; an unavoidable overflow is left to the runtime, which surfaces the
 *     friendly "too large for this model" error (chat-stream.ts).
 *   - Older turns are dropped oldest-first until the estimate fits. A contiguous tail preserves
 *     the strict user/assistant ALTERNATION, but NOT first-role parity — an even-length trim can
 *     leave an assistant-first tail, which strict templates (Mistral-family) `raise_exception` on
 *     (HTTP 500). So after the fill loop (trimmed branch only) any leading assistant turns are
 *     dropped, normalizing the kept tail user-first (CB-1). That also prevents replaying the
 *     synthetic compaction pair's ack without its intro — a lone kept ack IS that leading assistant.
 * `CHAT_RESPONSE_RESERVE_TOKENS` holds back room for the answer.
 */
export function fitMessagesToContext(messages: ChatMessage[], contextTokens: number): ChatMessage[] {
  const budget = Math.max(256, (Math.floor(contextTokens) || 0) - CHAT_RESPONSE_RESERVE_TOKENS)
  let firstNonSystem = 0
  while (firstNonSystem < messages.length && messages[firstNonSystem].role === 'system') {
    firstNonSystem++
  }
  const system = messages.slice(0, firstNonSystem)
  const turns = messages.slice(firstNonSystem)
  if (turns.length === 0) return messages

  let used = system.reduce((sum, m) => sum + messageTokens(m), 0)
  // The final turn is mandatory — add it first, then fill older turns newest→oldest.
  const last = turns[turns.length - 1]
  used += messageTokens(last)
  const keptReversed: ChatMessage[] = [last]
  for (let i = turns.length - 2; i >= 0; i--) {
    const cost = messageTokens(turns[i])
    if (used + cost > budget) break
    used += cost
    keptReversed.push(turns[i])
  }
  // User-first normalization (CB-1): only in the trimmed branch, drop leading assistant turns so
  // the kept tail begins user-first — strict templates (Mistral-family) `raise_exception` on an
  // assistant-first message list. `while` (not `if`) is defensive; the length-1 guard never drops
  // the mandatory final user turn and never empties. In the identity (no-trim) case the oldest kept
  // turn is the conversation's first turn — always `user` (real chats + the compaction pair are
  // user-first) — so this pops nothing and the identity return below stays byte-identical.
  while (keptReversed.length > 1 && keptReversed[keptReversed.length - 1].role === 'assistant') {
    keptReversed.pop()
  }
  // Nothing was dropped → return the original array unchanged (cheap: no copy).
  if (keptReversed.length === turns.length) return messages
  return [...system, ...keptReversed.reverse()]
}

/**
 * Build the runtime message list: system prompt + persisted history in order, trimmed to fit
 * the model context (`fitMessagesToContext`).
 */
export function buildChatMessages(
  db: Db,
  conversationId: string,
  contextTokens: number,
  skillFence?: string | null,
  // The §5.4 compaction toggle. Defaults to a fresh read so every existing caller is byte-identical;
  // `generateAssistantMessage` threads its single per-turn `getSettings` read through here (CB-6) so
  // the toggle is not re-read mid-turn.
  compactionOn: boolean = compactionEnabled(db)
): ChatMessage[] {
  const messages: ChatMessage[] = [{ role: 'system', content: buildSystemPrompt(skillFence) }]
  // L2 (context compaction): when a checkpoint exists, inject its summary as a synthetic
  // user→assistant pair (§4.5) and replay only the turns AFTER it — the cached summary stands in
  // for everything older. No checkpoint ⇒ replay the whole history, byte-identical to before. The
  // checkpoint row itself is never a turn (kind-filtered by listConversationTurns). The §5.4 toggle
  // gates the READ as well as the write (compactionEnabled): when off, any existing checkpoint is
  // ignored and the FULL history replays — byte-identical to the pre-feature L1-only app.
  const checkpoint = compactionOn ? getLatestCheckpoint(db, conversationId) : null
  if (checkpoint) messages.push(...compactionSummaryPair(checkpoint.summary))
  for (const turn of listConversationTurns(db, conversationId, checkpoint?.coversThroughRowid ?? 0)) {
    // Assistant turns are scrubbed of think blocks before being replayed —
    // replayed reasoning confuses the model (see stripThinkBlocks).
    messages.push({
      role: turn.role,
      content: turn.role === 'assistant' ? stripThinkBlocks(turn.content) : turn.content
    })
  }
  return fitMessagesToContext(collapseToAlternating(messages), contextTokens)
}

/**
 * Force strict user/assistant alternation (after the leading system message) before the
 * messages reach the model. A turn whose role repeats the previous kept turn — e.g.
 * consecutive USER turns left behind when an answer failed and persisted no assistant
 * reply — collapses to the LATEST of that run (stale orphans dropped). Several chat
 * templates (Mistral, Qwen tool-style) RAISE on non-alternating roles, which surfaced as
 * an HTTP 500 on the next turn; this keeps the conversation answerable regardless.
 */
export function collapseToAlternating(messages: ChatMessage[]): ChatMessage[] {
  const out: ChatMessage[] = []
  for (const m of messages) {
    if (m.role === 'system') {
      out.push(m)
      continue
    }
    const prev = out[out.length - 1]
    if (prev && prev.role === m.role) {
      out[out.length - 1] = m
    } else {
      out.push(m)
    }
  }
  return out
}

export interface GenerateOptions {
  signal?: AbortSignal
  /** Called with each streamed token so the IPC layer can forward it to the renderer. */
  onToken?: (token: string) => void
  /** Answer-depth mode, forwarded to the runtime. Omitted = 'balanced'. */
  mode?: ChatDepthMode
  /** Called with each reasoning delta (Deep mode) — live display only, never persisted. */
  onReasoning?: (delta: string) => void
  /**
   * The skill resolved for this turn (skills plan §10/§11). When set, its instructions are
   * assembled into a budgeted, guard-bracketed fence in the system message, and the assistant row
   * is stamped with the install_id — but ONLY when the fence actually fit (omitted-for-budget ⇒ no
   * fence, no stamp, so the glyph corresponds 1:1 to a prompt that carried the skill — §22-A5/A6).
   */
  skill?: TurnSkill | null
  /**
   * Fired exactly once if the context-compaction pre-pass starts a summarization for this turn
   * (it adds latency before the first answer token). Phase 1 only plumbs the callback; the
   * `STREAM.compaction` UX channel that consumes it is Phase 2.
   */
  onCompactionStart?: () => void
  /**
   * Fired exactly once with the REAL assembled prompt's usage (the same over-counting estimate
   * the budget trims with, over the launched window) right before generation starts. The IPC
   * layer forwards it to the composer meter so the live bar reflects what the model actually
   * received — for plain chat that includes the skill fence + any checkpoint pair the renderer
   * estimate can't see; for grounded answers (rag/index.ts) it includes the whole injected
   * excerpt/document block.
   */
  onPromptUsage?: (usage: ContextUsage) => void
  /**
   * #290: fired at most once, after the stream COMPLETED (never on a user Stop — an aborted
   * request carries no final chunk), with the runtime's per-request `timings` when it sent any.
   * The mock runtime sends none, so this never fires there. Numbers only; the IPC layer turns
   * them into the ephemeral per-answer speed line. Nothing is persisted.
   */
  onTimings?: (timings: RuntimeTimings) => void
}

/**
 * Stream an assistant reply from the runtime for `conversationId`, forwarding each
 * token via `onToken`, then persist the (possibly partial, if aborted) assistant
 * message. The conversation history must already include the triggering user
 * message. Returns the persisted assistant Message.
 */
export async function generateAssistantMessage(
  db: Db,
  runtime: ModelRuntime,
  conversationId: string,
  opts: GenerateOptions = {}
): Promise<Message> {
  // Trim history to the model's context window so an accumulating conversation never
  // assembles a prompt the runtime rejects (HTTP 400 exceed_context_size_error). Budget
  // against the REAL launched window the runtime reports (§L0), not settings.contextTokens
  // (which can diverge from the model's actual --ctx-size).
  // CB-6: one settings read for the whole turn. Both the launched window and the §5.4 compaction
  // toggle derive from it (`compactionOn` mirrors compactionEnabled), and buildChatMessages takes the
  // toggle as a param below so it need not re-read — collapsing the former 3× per-turn getSettings.
  const settings = getSettings(db)
  const contextTokens = effectiveContextWindow(runtime, settings)
  const compactionOn = settings.chatCompactionEnabled !== false
  // L2 (context compaction): when the history approaches the window, summarize the older turns into
  // a cached checkpoint so assembly replays a compact summary + recent turns instead of dropping the
  // old ones outright. A no-op below threshold; any failure/abort falls back to today's L1 trim with
  // no user-visible error. Runs on the already-claimed chat slot as part of this turn (R4), honouring
  // the turn's AbortSignal, BEFORE assembly so buildChatMessages picks up any fresh checkpoint. Gated
  // by the §5.4 toggle: when off, no checkpoint is created and assembly ignores any existing one.
  // Pre-size the skill fence (§11.3/A6) BEFORE it goes in the system message so it can never
  // starve the base preamble or the final user turn — fitMessagesToContext only drops older
  // history. Stamp the assistant row only when the fence actually fit (skill shaped the answer).
  // CB-3: build it BEFORE `ensureCompacted` (free — `buildTurnFence` reads only the final turn and
  // has no checkpoint dependency) so its token cost can be handed to the compaction pre-pass as
  // `reservedTokens`, letting a fence-heavy turn cross the compaction threshold at the right point.
  const fence = buildTurnFence(db, conversationId, opts.skill, contextTokens)
  if (compactionOn) {
    await ensureCompacted(db, runtime, conversationId, contextTokens, {
      signal: opts.signal,
      onStart: opts.onCompactionStart,
      reservedTokens: fence ? approxPromptTokens(fence) : 0
    })
  }
  const messages = buildChatMessages(db, conversationId, contextTokens, fence, compactionOn)
  // Meter honesty: report what the model actually receives (fitted history + system/fence),
  // in the same estimate currency the budget uses, so the live meter never under-reads a turn.
  opts.onPromptUsage?.({
    usedTokens: messages.reduce((sum, m) => sum + messageTokens(m), 0),
    window: contextTokens
  })
  let content = ''
  // Capture the completion's finish reason so we can honestly flag a reply the model cut off at the
  // token/context ceiling ('length') instead of persisting the mid-word partial as if complete
  // (§L0 honest-signal). A user Stop aborts before any final chunk, so this stays null → not
  // truncated (the abort partial is intentional and user-known).
  let finishReason: string | null = null
  // #290: the runtime's per-request timings ride the same final chunk as the finish reason, so
  // a user Stop (no final chunk) leaves this null and the speed line is simply never emitted.
  let timings: RuntimeTimings | null = null
  const stream = runtime.chatStream(messages, {
    signal: opts.signal,
    mode: opts.mode,
    onReasoning: opts.onReasoning,
    onFinish: (reason, tm) => {
      finishReason = reason
      timings = tm ?? null
    }
  })
  let caughtAbort = false
  try {
    for await (const token of stream) {
      content += token
      opts.onToken?.(token)
    }
  } catch (err) {
    // A user Stop aborts the stream; persist the partial text and return normally.
    // Any other error is a real failure and propagates to the IPC layer.
    if (!isAbortError(err, opts.signal)) throw err
    caughtAbort = true
  }
  // #290: hand the timings up only for a COMPLETED stream — never a wrong speed on a partial.
  if (timings && !caughtAbort && opts.signal?.aborted !== true) opts.onTimings?.(timings)
  // CB-4: capture whether the model actually emitted anything BEFORE the strip passes below. A
  // reply that arrived and then stripped to empty (all-`<think>`/fence-echo) is a benign silent-empty
  // — distinct from a completed stream that produced no token at all.
  const receivedAnyToken = content !== ''
  // 'length' = the reply hit a ceiling and is cut off — but llama-server reports the SAME reason
  // for two very different ceilings: the model's context window AND a per-request `max_tokens`
  // cap (Fast mode caps at FAST_MAX_TOKENS). The badge claims "reached the model's context
  // limit", so flag ONLY the uncapped case: with a cap in effect the cap is what fired (prompt
  // fitting reserved ≥ CHAT_RESPONSE_RESERVE_TOKENS ≥ the Fast cap of answer room), and showing
  // the context-limit badge at 7% meter usage is exactly the false "context is full" signal the
  // 2026-07-04 user report described.
  const capTokens = requestParamsForMode(opts.mode).maxTokens
  const truncated = finishReason === 'length' && capTokens == null
  // Reasoning never reaches the DB: the runtime already streams it separately,
  // and any inline think block that slipped into the answer is stripped here.
  content = stripThinkBlocks(content)
  // Drop any skill-fence framing the model echoed back (e.g. a trailing "--- END LOCAL SKILL ---").
  content = stripSkillFenceEcho(content)
  // Persist whatever was produced — on a stop, that is the partial text so far. Nothing to persist:
  //  - a stop (abort) that produced nothing, OR a reply that arrived but stripped to empty
  //    (all-`<think>`/fence-echo) → the benign silent-empty: persist nothing (a permanent empty
  //    assistant bubble in the transcript otherwise) and return an unpersisted, empty message to
  //    keep the resolve contract.
  //  - CB-4: a COMPLETED, non-aborted stream that produced zero tokens is a genuine empty
  //    completion — throw so the IPC layer surfaces a friendly, retryable error instead of a
  //    silently unanswered user question.
  if (content === '') {
    const wasAborted = caughtAbort || opts.signal?.aborted === true
    if (wasAborted || receivedAnyToken) return emptyAssistantMessage(conversationId)
    throw new EmptyCompletionError()
  }
  // R1 (full-audit-2026-06-30, Phase C) defense-in-depth: the persist rides through
  // `persistAssistantMessage`, whose abort+closed-DB guard swallows a teardown that closed the DB
  // under an aborted partial-persist (the partial is lost, not a crash) — extracted to the shared
  // helper so the grounded paths behave identically (full-audit 2026-07-11 CODE-18).
  // Stamp the skill only when its fence was actually placed (DS16/§22-A5) — an omitted-for-budget
  // skill did not shape this answer, so it gets no glyph.
  return persistAssistantMessage(
    db,
    {
      conversationId,
      role: 'assistant',
      content,
      skillId: fence ? (opts.skill?.installId ?? null) : null,
      // Carry auto-fire provenance only when the fence was placed (the skill shaped the answer) — so the
      // S13c undo lines up 1:1 with the glyph (§22-A5).
      autoFired: fence ? opts.skill?.autoFired === true : false,
      // Honest-signal flag: mark a reply the model cut off at the context ceiling ('length').
      truncated,
      // #498: plain chat flags ONLY the uncapped case (see the gate above), so its flagged case is
      // the context window by construction — never the app's own cap.
      truncatedCause: truncated ? 'context' : undefined
    },
    opts.signal
  )
}

/**
 * Build the plain-chat skill fence for a turn, pre-sized to leave room for the base preamble + the
 * final user turn (audit A6). Returns the fence string, or null when there is no skill or the fence
 * was omitted for budget. Shared between the initial call site and the stamp decision so they
 * cannot disagree.
 */
function buildTurnFence(
  db: Db,
  conversationId: string,
  skill: TurnSkill | null | undefined,
  contextTokens: number
): string | null {
  if (!skill) return null
  // CB-6: read just the final turn (a LIMIT 1 tail) instead of paging the whole history to size the
  // fence — byte-identical content to the old `listMessages(...).at(-1)`.
  const finalTurn = getLatestMessage(db, conversationId)?.content ?? ''
  const fixedTokens =
    approxPromptTokens(BASE_SYSTEM_PROMPT) +
    approxPromptTokens(finalTurn) +
    2 * PER_MESSAGE_OVERHEAD_TOKENS
  const budget = skillFenceBudgetTokens({
    contextTokens,
    reserveTokens: CHAT_RESPONSE_RESERVE_TOKENS,
    fixedTokens
  })
  const fence = buildSkillFence({ title: skill.title, body: skill.body }, budget)
  // U1 (audit §3.6): log a budget-driven trim/omit (ids/counts only) — the flags were discarded before,
  // making a decapitated-rule turn undiagnosable. The SKILL.md bodies now lead with the honesty rules.
  logSkillFenceReduction(skill.installId, fence)
  return fence.text
}

/** An UNPERSISTED empty assistant message (the zero-token-stop case — see above). */
export function emptyAssistantMessage(conversationId: string): Message {
  return {
    id: randomUUID(),
    conversationId,
    role: 'assistant',
    content: '',
    createdAt: nowIso(),
    tokenCount: null
  }
}

/**
 * Persist an assistant turn with the R1 abort+closed-DB guard (full-audit-2026-06-30 Phase C;
 * extracted as a shared helper for full-audit 2026-07-11 CODE-18). The lock/quit teardown
 * deterministically awaits the stream's settle BEFORE closing the DB, so on the live path the
 * (partial) answer persists first — but if the turn's signal was aborted AND the DB is already
 * closed, this is a teardown that closed the DB under an aborted partial-persist: drop the message
 * quietly (return the unpersisted empty message, keeping the resolve contract) instead of rejecting
 * into the global unhandled-rejection handler. A genuine open-DB persistence error (constraint/disk)
 * still propagates. Content is never logged — only the conversation id. Shared by the plain-chat
 * persist and the three grounded persist sites (rag/index.ts ×2, rag/whole-doc-tree.ts) so the
 * Stop+lock race behaves identically on every answer path.
 */
export function persistAssistantMessage(db: Db, input: AppendMessageInput, signal?: AbortSignal): Message {
  // #600/#612: a partial cut by a model stop, the Stop button, lock or quit is marked "Reply stopped" —
  // here, once, for every answer path that persists through this helper (plain chat and the three
  // grounded sites), keyed on the turn's own abort reason. A bare abort carries no cause and no marker.
  // #742: never on a reply cut at the ceiling. Runtimes report a finish reason only for a stream that
  // ran to its end (and the continuation engine reports none on a Stop), so a truncated reply was cut
  // before any abort: an abort landing after it cut nothing, and the row keeps its one true note
  // ("Reply cut off", never also "Reply stopped" — design-guidelines §11.21).
  const cause = input.endedEarly == null && input.truncated !== true ? endedEarlyCause(signal) : undefined
  if (cause) input = { ...input, endedEarly: cause }
  try {
    return appendMessage(db, input)
  } catch (err) {
    if (signal?.aborted && !db.isOpen) {
      log.warn('chat: dropped partial reply — workspace locked during persist', {
        conversationId: input.conversationId
      })
      return emptyAssistantMessage(input.conversationId)
    }
    throw err
  }
}