feat(chat-runtime): standalone session runtime — journal, live sessions, subscriptions

Merged
SA
3mo ago
+4423-2

Stacked on #6166. The mountable chat runtime: journal with epoch-qualified replay, LiveSession adapter pump with FIFO prompt queue, SubscriptionHub (replay-then-live, delta filtering, coalescing), commands with commandId dedupe, fake harness. 50 tests incl. whole-envelope reconnect equality. Also folds the Codex desktop teardown findings into the ship plan.

https://claude.ai/code/session_01XRFCEPsfmJHu2SenDKYJ2h


Summary by cubic

Adds a standalone chat session runtime as @superset/chat-runtime with an append-only journal, epoch-qualified replay, live session pumping, and replay-then-live subscriptions. Now hardened against adapter/journal/sink failures to keep sessions consistent and recoverable; uses Drizzle-backed SQLite with committed migrations, and 50 tests verify live===replay parity.

  • New Features

    • New package @superset/chat-runtime exporting createChatRuntime({ dataDir, harnesses, openDatabase? }); SQLite chat.db via Drizzle (migrations on open, WAL) with an injectable DB seam (better-sqlite3 in prod, bun:sqlite in tests).
    • Journal: epoch minting on first open/loss, append-only writes with same-transaction projection; readSince/readPage return reset signals on epoch mismatch.
    • Live sessions: adapter-driven pump with FIFO prompt queue, runtime-minted user_message IDs, and pass-through for cancel/approval/mode.
    • Subscriptions: replay-then-live stream with per-subscriber delta filtering, 33ms coalescing (flushed before durable events), and reset frames without disconnect.
    • Commands: zod-validated inputs and commandId LRU dedupe; harness registry and a scripted fake harness for testing.
  • Bug Fixes

    • Adapter stream failures are journaled (turn → interrupted, error notice, session dead) instead of crashing; dispose waits for adapters and always closes subscriptions/DB.
    • Fixed stuck prompt queues when adapter.prompt throws.
    • Subscriptions emit reset frames for unreadable journal rows and reject invalid before-cursors; no thrown errors on subscribe/paginate.
    • Throwing sinks are dropped and closed; per-session coalescers released; deltas are dropped when no subscribers.
    • createSession validates the harness before committing; compensates the row if start fails.
    • Command dedupe keys include the command kind to avoid cross-verb collisions.
    • Epoch recovery also resets projection state (status/title/queued).

Summary by CodeRabbit

  • New Features

    • Added a standalone chat runtime with session management, durable history, replay, live updates, and harness support.
    • Added commands for creating sessions, prompting, canceling turns, responding to approvals, changing modes, listing sessions, and retrieving items.
    • Added scripted fake-harness support for simulated chat interactions.
  • Bug Fixes

    • Improved duplicate command handling, approval flows, cancellation, queued prompts, and session recovery.
    • Improved replay, pagination, reconnect behavior, and stale-session reset handling.
  • Documentation

    • Added architecture, package boundary, integration, and usage guidance.