Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript
14.4 KB
Raw Blame
/**
 * `DualConversationStore` — shadow-write to two stores for canary validation.
 *
 * Hot user-visible reads prefer `memory`, the canonical store during validation.
 * Durable catalog rows and cold transcripts remain visible when RAM is empty,
 * while SQLite is also queried for parity so the canary can detect drift.
 * Writes apply to both; parity is recorded via the optional metrics callback.
 * Used when `CLERUM_SESSION_STORE=dual` to validate that the SQLite store
 * produces the same observable behavior as the in-memory store before we
 * flip the canary to `sqlite`.
 */
import { Counter } from 'prom-client'
import type { ModelSelectionWriteOutcome, ReapedSession } from '../../../db/worker/protocol'
import type { Conversation, PendingApproval, TurnToolCall } from '../../types'
import type {
  ConversationSessionMessages,
  ConversationSessionSummary,
  ConversationStore,
  EvictCallback,
  GetOrCreateOptions,
  PersistedSessionListing,
  SessionListQuery,
  SessionMessagesQuery,
  SessionTokenUsage,
} from '../conversationStore'

const parityCounter = (() => {
  try {
    return new Counter({
      name: 'clerum_conversation_store_parity_total',
      help: 'Dual-write parity outcomes between RAM and SQLite stores',
      labelNames: ['op', 'match'] as const,
    })
  } catch {
    return undefined
  }
})()

export interface DualStoreMetrics {
  recordParity?: (op: string, match: boolean) => void
}

const TOKEN_COUNTER_KEYS = new Set([
  'input_tokens',
  'output_tokens',
  'cache_read_tokens',
  'cache_write_tokens',
])

const DUAL_CATALOG_FALLBACK_OVERFETCH = 2

function normalizeParityValue(value: unknown, key?: string): unknown {
  if (value instanceof Date) return value.toISOString()
  if (Array.isArray(value)) return value.map(entry => normalizeParityValue(entry))
  if (key === 'cacheTokensReported' && value === false) return undefined
  if (key && TOKEN_COUNTER_KEYS.has(key) && value === 0) return undefined
  if (value === null || typeof value !== 'object') return value

  const normalized: Record<string, unknown> = {}
  for (const entryKey of Object.keys(value).sort()) {
    const entryValue = normalizeParityValue((value as Record<string, unknown>)[entryKey], entryKey)
    if (entryValue !== undefined) normalized[entryKey] = entryValue
  }
  return normalized
}

function pagesMatch(left: unknown, right: unknown): boolean {
  return JSON.stringify(normalizeParityValue(left)) === JSON.stringify(normalizeParityValue(right))
}

function compareSummaries(
  left: ConversationSessionSummary,
  right: ConversationSessionSummary
): number {
  const byUpdated = right.lastActivityAt.getTime() - left.lastActivityAt.getTime()
  return byUpdated || (left.key < right.key ? -1 : left.key > right.key ? 1 : 0)
}

export class DualConversationStore implements ConversationStore {
  constructor(
    private readonly memory: ConversationStore,
    private readonly sqlite: ConversationStore,
    private readonly metrics: DualStoreMetrics = {}
  ) {}

  // ─── Sync RAM-only path — reads from memory, writes to both ──────────

  get(key: string): Conversation | undefined {
    const memVal = this.memory.get(key)
    queueMicrotask(() => {
      const sqlVal = this.sqlite.get(key)
      this.recordParity('get', memVal === undefined ? sqlVal === undefined : sqlVal !== undefined)
    })
    return memVal
  }

  set(key: string, value: Conversation, opts?: { pinned?: boolean }): void {
    this.memory.set(key, value, opts)
    this.sqlite.set(key, value, opts)
  }

  has(key: string): boolean {
    const memHas = this.memory.has(key)
    queueMicrotask(() => {
      const sqlHas = this.sqlite.has(key)
      this.recordParity('has', memHas === sqlHas)
    })
    return memHas
  }

  listByPrefix(prefix: string): Array<{ key: string; conversation: Conversation }> {
    return this.memory.listByPrefix(prefix)
  }

  pin(key: string): void {
    this.memory.pin(key)
    this.sqlite.pin(key)
  }

  unpin(key: string): void {
    this.memory.unpin(key)
    this.sqlite.unpin(key)
  }

  onEvict(cb: EvictCallback): void {
    // Only the canonical (memory) store fires onEvict — sqlite eviction
    // would double-fire callbacks for the same event.
    this.memory.onEvict(cb)
  }

  // ─── Async durable path ─────────────────────────────────────────────

  async getOrLoad(key: string): Promise<Conversation | undefined> {
    // Memory-first cache, SQLite as cold-load backstop. Before B6 this method
    // queried both stores in parallel and returned `memVal` unconditionally;
    // a cold session present in SQLite but absent from memory was reported
    // as `undefined`, which the caller treated as "fresh session" and then
    // recreated the row via `ON CONFLICT(id) DO UPDATE` — destroying
    // `started_at`. Now: memory hit → return; memory miss + SQLite hit →
    // hydrate memory then return; both miss → undefined.
    const memVal = await this.memory.getOrLoad(key)
    if (memVal !== undefined) {
      // Parity peek is best-effort and out of the hot path.
      queueMicrotask(() => {
        this.sqlite.getOrLoad(key).then(
          sqlVal => this.recordParity('getOrLoad', sqlVal !== undefined),
          () => this.recordParity('getOrLoad', false)
        )
      })
      return memVal
    }
    const sqlVal = await this.sqlite.getOrLoad(key)
    if (sqlVal !== undefined) {
      // Hydrate the cache so subsequent sync `get()` calls hit RAM.
      this.memory.set(key, sqlVal)
      this.recordParity('getOrLoad', true)
      return sqlVal
    }
    this.recordParity('getOrLoad', true) // both stores agree the key is absent
    return undefined
  }

  async prefetchUserSessions(prefix: string): Promise<void> {
    await Promise.all([
      this.memory.prefetchUserSessions(prefix),
      this.sqlite.prefetchUserSessions(prefix),
    ])
  }

  async listSessionSummariesByPrefix(
    prefix: string,
    query: SessionListQuery = {}
  ): Promise<ConversationSessionSummary[]> {
    const memList = await this.memory.listSessionSummariesByPrefix(prefix, query)
    let sqlList: ConversationSessionSummary[]
    const durableByKey = new Map<string, ConversationSessionSummary>()
    try {
      const memoryFillsPage = query.limit !== undefined && memList.length >= query.limit
      // A full memory page is already canonical, so SQLite is sampled only for
      // parity. A partial page gets one bounded over-fetch for cold durable rows;
      // never drain cursor pages in proportion to the total mirrored catalog.
      const sqliteQuery =
        query.limit === undefined || memoryFillsPage
          ? query
          : { ...query, limit: query.limit * DUAL_CATALOG_FALLBACK_OVERFETCH }
      sqlList = await this.sqlite.listSessionSummariesByPrefix(prefix, sqliteQuery)
      for (const summary of sqlList) {
        if (!this.memory.has(summary.key)) durableByKey.set(summary.key, summary)
      }
    } catch {
      this.recordParity('listSessionSummariesByPrefix', false)
      return memList
    }

    const sqlParityPage = query.limit === undefined ? sqlList : sqlList.slice(0, query.limit)
    this.recordParity('listSessionSummariesByPrefix', pagesMatch(memList, sqlParityPage))
    if (query.limit !== undefined && memList.length >= query.limit) return memList
    // A live memory copy is canonical even when its timestamp places it outside
    // this page. Otherwise an older SQLite copy can cross a later cursor and
    // repeat a key that an earlier page already emitted from memory.
    const byKey = new Map(durableByKey)
    for (const summary of memList) byKey.set(summary.key, summary)
    const merged = Array.from(byKey.values()).sort(compareSummaries)
    return query.limit === undefined ? merged : merged.slice(0, query.limit)
  }

  async getSessionMessagesByKey(
    key: string,
    prefix: string,
    query: SessionMessagesQuery = {}
  ): Promise<ConversationSessionMessages | undefined> {
    const memPage = await this.memory.getSessionMessagesByKey(key, prefix, query)
    if (memPage !== undefined) {
      queueMicrotask(() => {
        this.sqlite.getSessionMessagesByKey(key, prefix, query).then(
          sqlPage => this.recordParity('getSessionMessagesByKey', pagesMatch(memPage, sqlPage)),
          () => this.recordParity('getSessionMessagesByKey', false)
        )
      })
      return memPage
    }

    try {
      const sqlPage = await this.sqlite.getSessionMessagesByKey(key, prefix, query)
      this.recordParity('getSessionMessagesByKey', sqlPage === undefined)
      return sqlPage
    } catch (error) {
      this.recordParity('getSessionMessagesByKey', false)
      throw error
    }
  }

  async loadAllPendingApprovals(): Promise<PersistedSessionListing[]> {
    const [memList, sqlList] = await Promise.all([
      this.memory.loadAllPendingApprovals(),
      this.sqlite.loadAllPendingApprovals(),
    ])
    this.recordParity('loadAllPendingApprovals', memList.length === sqlList.length)
    // The DURABLE store is the source of truth for cold-start rehydration: this
    // method exists precisely to repopulate pending approvals after a restart,
    // when the in-memory store boots EMPTY. Returning `memList` would silently
    // drop every persisted approval (the dual canary's whole purpose is to
    // prove cross-restart durability — returning memList makes it a no-op).
    //
    // Merge by sessionKey, durable first, then overlay the live in-RAM listing
    // so a still-hot session prefers its in-memory reference. recordParity above
    // keeps divergence observability intact.
    const bySessionKey = new Map<string, PersistedSessionListing>()
    for (const entry of sqlList) bySessionKey.set(entry.sessionKey, entry)
    for (const entry of memList) bySessionKey.set(entry.sessionKey, entry)
    return Array.from(bySessionKey.values())
  }

  // D.2 — the reap only makes sense for the durable side; memory has nothing
  // to reap (it doesn't survive restarts). Delegate to sqlite.
  async reapProcessingSessions(now: number): Promise<ReapedSession[]> {
    return (await this.sqlite.reapProcessingSessions?.(now)) ?? []
  }

  // D.8 (F7) — same: only the durable side has awaiting_approval rows to reap.
  async reapExpiredAwaitingApprovalSessions(now: number): Promise<ReapedSession[]> {
    return (await this.sqlite.reapExpiredAwaitingApprovalSessions?.(now)) ?? []
  }

  // S1 — reap ALL awaiting_approval sessions (live or expired) at boot. Durable
  // side only, same as above.
  async reapAwaitingApprovalSessions(now: number): Promise<ReapedSession[]> {
    return (await this.sqlite.reapAwaitingApprovalSessions?.(now)) ?? []
  }

  // ─── Persist hooks ──────────────────────────────────────────────────

  async persistSessionCreate(conv: Conversation, opts: GetOrCreateOptions): Promise<void> {
    await Promise.all([
      Promise.resolve(this.memory.persistSessionCreate(conv, opts)),
      Promise.resolve(this.sqlite.persistSessionCreate(conv, opts)),
    ])
  }

  async persistTurnStart(conv: Conversation, userInput: string): Promise<void> {
    await Promise.all([
      Promise.resolve(this.memory.persistTurnStart(conv, userInput)),
      Promise.resolve(this.sqlite.persistTurnStart(conv, userInput)),
    ])
  }

  async persistTurnComplete(conv: Conversation, response: string): Promise<void> {
    await Promise.all([
      Promise.resolve(this.memory.persistTurnComplete(conv, response)),
      Promise.resolve(this.sqlite.persistTurnComplete(conv, response)),
    ])
  }

  async persistTurnCancel(conv: Conversation): Promise<void> {
    await Promise.all([
      Promise.resolve(this.memory.persistTurnCancel(conv)),
      Promise.resolve(this.sqlite.persistTurnCancel(conv)),
    ])
  }

  async persistTurnFail(conv: Conversation): Promise<void> {
    await Promise.all([
      Promise.resolve(this.memory.persistTurnFail(conv)),
      Promise.resolve(this.sqlite.persistTurnFail(conv)),
    ])
  }

  async persistToolCall(conv: Conversation, toolCall: TurnToolCall): Promise<void> {
    await Promise.all([
      Promise.resolve(this.memory.persistToolCall(conv, toolCall)),
      Promise.resolve(this.sqlite.persistToolCall(conv, toolCall)),
    ])
  }

  async persistSystemPromptStableHash(conv: Conversation, stableHash: string): Promise<void> {
    await Promise.all([
      Promise.resolve(this.memory.persistSystemPromptStableHash(conv, stableHash)),
      Promise.resolve(this.sqlite.persistSystemPromptStableHash(conv, stableHash)),
    ])
  }

  /**
   * #654 — the CAS arbiter is the DURABLE store, and only it. In `dual` mode the
   * memory store holds the same `Conversation` reference the manager mirrors the
   * accepted outcome into, so its own revision counter has nothing to arbitrate:
   * asking both would invent a second, weaker authority whose counter can drift
   * from the row (a cold load leaves SQLite ahead) and start rejecting writes the
   * durable store accepts. Without this method the canary mode would inherit the
   * interface default — a silent no-op that reports success while nothing is
   * persisted, which is exactly what `enqueueSync` above removes.
   */
  async applyModelSelection(
    conv: Conversation,
    provider: string,
    model: string,
    expectedRevision?: number
  ): Promise<ModelSelectionWriteOutcome> {
    return this.sqlite.applyModelSelection(conv, provider, model, expectedRevision)
  }

  async persistSessionUsage(conv: Conversation, usage: SessionTokenUsage): Promise<void> {
    await Promise.all([
      Promise.resolve(this.memory.persistSessionUsage?.(conv, usage)),
      Promise.resolve(this.sqlite.persistSessionUsage?.(conv, usage)),
    ])
  }

  async persistSuspend(conv: Conversation, approval: PendingApproval): Promise<void> {
    await Promise.all([
      this.memory.persistSuspend(conv, approval),
      this.sqlite.persistSuspend(conv, approval),
    ])
  }

  async persistApprovalResolved(
    conv: Conversation,
    requestId: string,
    decision: 'approve' | 'deny' | 'cancel'
  ): Promise<void> {
    await Promise.all([
      this.memory.persistApprovalResolved(conv, requestId, decision),
      this.sqlite.persistApprovalResolved(conv, requestId, decision),
    ])
  }

  async shutdown(): Promise<void> {
    await Promise.allSettled([this.memory.shutdown?.(), this.sqlite.shutdown?.()])
  }

  private recordParity(op: string, match: boolean): void {
    parityCounter?.labels(op, match ? 'true' : 'false').inc()
    this.metrics.recordParity?.(op, match)
  }
}