feat: sync chat history from cloud workspaces and search chats

Merged
HA
8d ago
+33603-317

Part 1 of 2, split out of #9292. Part 2 (moving a chat between a laptop and a cloud workspace) is stacked on this branch.

Cloud boxes sync their chats to Postgres as they happen, and you can search your chats in the command palette and the CLI. This PR also carries all of the Postgres schema for chats, including the move tables part 2 uses, so part 2 has no migration.

What changes

Database: chat_items and chat_turns (latest state per item and turn; first_seq keeps display order), chat_transcripts (one row per agent session bundle, bytes in R2 as a files row) and chat_moves (every attempt to move a chat), and sync columns on chat_sessions (harness, harness_session_id, epoch, last_seq, status). chat_transcripts and chat_moves stay empty until part 2 lands. One migration, 0189_chat_storage (main took 0188), identical to #9292's 0188_chat_storage: generated statements, with two hand-written parts around them: chat_search_text (splits paths and camelCase for the generated search column) and a guarded lakebase_bm25 index that plain Postgres skips.

API: chatHistory.push (folds a host's journal events; newest version wins; session status and title follow only a newer session event; a box may only push its own workspace's chats; a push for another workspace gets CONFLICT) and chatHistory.search (owner only, one result per conversation, BM25 where available, full text otherwise). Box allowlist: chatHistory.push.

chat-runtime: onAppend reports each durable append; a local chat_sync_state table records how far each chat was pushed.

host-service: chat sync. Boxes push continuously with backoff, the server's rejected batches are split and then skipped, and unsent chats catch up at start. A machine someone owns does not push yet.

Desktop: a Chats section in Cmd+Shift+K, translated in all 17 locales.

CLI: superset sessions search <query>.

Migration safety

  • Every table is new and empty. The five chat_sessions columns are nullable with no default, so adding them changes only the catalog (about 73k rows on a production branch today).
  • Applied with drizzle-kit migrate on a fresh Neon branch of production, after main's 0188_automation_archived_at. It succeeded and created chat_items_search_bm25.

Tested

  • Unit: chat-runtime journal (13), host-service chat-sync (9), trpc chat-history (6), cli sessions search (2). Typecheck passes for db, trpc, chat-runtime, host-service and cli. bun run lint and bun run check:i18n pass.
  • chat-history.integration.ts on that Neon branch: 8/8 tests pass on this branch, and 16/16 on part 2. Its afterAll cleanup ran past bun's 5 s limit on the 0.25 CU branch, because the org/user deletes cascade over a copy of production data. The same thing happens on part 2.
  • Not run locally: desktop tsc (OOM on the 8 GB machine I used); CI covers it.

Summary by CodeRabbit

  • New Features
    • Search cloud chat history from the command palette or with superset sessions search; open results directly in their workspace.
    • Move workspaces and chats between computers and the cloud, with progress, status updates, and retry options.
    • View saved chat history while a cloud workspace is starting, and retain earlier messages alongside live chat history.
    • Chat history syncs to the cloud and supports searching across conversations.
  • Documentation
    • Added guidance and examples for the sessions search command.