AI plumbing Milestone 1: transports, routing, generations, lanes, gate, outbox

Merged
JO
1d ago
+32654-14526

What this is

This is Milestone 1 of the AI plumbing redesign (plan: #4440), merged here from refactor/ai-plumbing. It is an interim release. The new internals sit behind the AI configuration you already have: transports, routing and consent, generations, the capsule, lanes, the local model gate, the outbox, and DocumentFile. It is not the finished redesign. Settings in the database, the AI settings pages, allowed endpoints, own keys and the member switch (redesign PRs 7โ€“9), and the full chat rework (PR 6) come in later releases.

634 files changed. Every PR below was reviewed and squash-merged into refactor/ai-plumbing. main was then merged into the branch, so this PR merges cleanly.

What's in it

On the branch:

  • #4773 2a. Transports and instrumentation.
  • #4774 2b. Routing and consent.
  • #4775 2c. Generations.
  • #5209 CI: refuse provider HTTP outside live tests.
  • #4765 2d. Jev and shadow runs.
  • #3422 Bridge the statement vault to search, and let tools read a file.
  • #4771 2e. PDF import and document search.
  • #4892 2f. ruby_llm 2.2.3, Jev via OpenRouter, review fixes.

Stack #5504, bottom to top:

  1. #5501 3a. Capsule, shared state, and probes off the web thread.
  2. #5502 Tidy 3a and remove dead code from 2aโ€“2f.
  3. #5503 A few comprehensive AI tests per layer.
  4. #5521 Windowing: linear generation runs instead of 2c's O(Nยฒ).
  5. #5550 2c follow-up: recorded evals that run, and set-aside prompt overrides on the family.
  6. #5608 Eight #5077 follow-ups, including #5663 (nullable enums sent to Anthropic as anyOf).
  7. #5769 Down-mark lockouts fixed, and jobs queued on main keep running.
  8. #5744 Count a mixed rule's ordinary changes as modified.
  9. #5745 Outbox: record each AI job and deliver it again when lost.
  10. #5756 4a. A queue per AI lane, and chat gets threads of its own.
  11. #5758 4b. A gate per local model host, with lanes and interrupts.
  12. #5784 4c. The typing hold moves into the gate, and a chat shows what it waits for.
  13. #5892 Documents in a job.
  14. #5893 Stop a PDF import.
  15. #5894 MCP tools in a job.
  16. #5895 Bill suggestions with Stop.
  17. #5896 Probe suggestion status before catching up a waiting frame.
  18. #6086 DocumentFile: a file tool for uploads, outside Ai::, with photo cleaning.
  19. #6087 Any feature reads an upload, photos included, through Ai::PdfReading.

#5077 tracks the follow-up work deferred from these PRs.

Upgrading (release notes draft)

Configuration is unchanged. The existing AI environment variables and settings still configure everything; Ai::LegacyEnvConfig reads them (redesign ยง4.5). See docs/hosting/ai.md.

  • Jobs already queued keep running. AutoCategorizeJob, AutoDetectMerchantsJob, EnhanceProviderMerchantsJob and WorkerAiHealthCheckJob stay for one release and hand their work to the new jobs. The ai capsule drains the old ai_high_priority and ai_low_priority queues for one release. Both are removed in the release after this one.
  • A custom sidekiq.yml needs the new queues (ai_interactive, ai_prompt, ai_background) and the ai_chat capsule, so copy the shipped file. A worker started with an old file leaves chats unanswered, and System health reports that no worker is processing the AI queues.
  • Threads and the database pool. Each worker that runs AI work gets RAILS_AI_MAX_THREADS more threads (default 1) for chat. They hold a database connection only for each query, so the pool doesn't usually need to grow. RAILS_MAX_THREADS must be greater than RAILS_AI_MAX_THREADS.
  • RAILS_AI_MAX_THREADS=0 means the worker runs no AI work. Use it on every worker but one when several workers share one local model host.
  • One gate per local model host. Sure sends a local model host no more AI requests at once than it has slots. The gate is keyed by host name, whatever the port.
  • Consent. An AI run for a family without AI consent now fails instead of being skipped.
  • Migrations. 19 new migrations. One moves the statement links on family_documents into its metadata and drops family_documents.account_statement_id. Back up before upgrading.

Not in this release (tracked in #5077):

  • Planning each AI job's lane from its plan rather than its queue.
  • The rest of chat and PDF-import cancellation.
  • Enforcing capsule-only AI calls in production.
  • The remaining probes and admin diagnostics.
  • The remaining instrumentation.

Validation

  • The full suite (TZ=Etc/UTC, 8 workers) passed with 0 failures on the branch after merging main (20,787 runs) and on the stack top (20,975 runs).
  • CI passed on every stack PR before it was merged.
  • bin/rubocop, erb_lint --lint-all, Biome and Brakeman are clean on the final head.

The branch README's series notes are removed in this PR, so main's README is unchanged.

Closes #3279
Closes #5458
Closes #5546

๐Ÿค– Generated with Claude Code

https://claude.ai/code/session_016zAWUVsg9hnzXE3MTW3voE


Summary by cubic

Milestone 1 of the AI plumbing redesign lands the new AI internals โ€” transports, routing and consent, generations, per-lane queues, the local model gate, the outbox, and document reading โ€” behind the existing AI configuration. It's an interim release; the AI settings pages, own keys, the member switch, and the full chat rework come later.

Upgrading

  • Configuration is unchanged: the existing AI environment variables and settings still configure everything.
  • Jobs already queued keep running. AutoCategorizeJob, AutoDetectMerchantsJob, EnhanceProviderMerchantsJob, and WorkerAiHealthCheckJob stay for one release and hand their work to the new jobs.
  • A custom sidekiq.yml needs the new queues (ai_interactive, ai_prompt, ai_background) and the ai_chat capsule; a worker on an old file leaves chats unanswered.
  • Workers that run AI work get RAILS_AI_MAX_THREADS extra threads for chat (default 1); RAILS_AI_MAX_THREADS=0 runs no AI work.
  • One gate per local model host limits concurrent requests, keyed by host name whatever the port.
  • An AI run for a family without AI consent now fails instead of being skipped.
  • 19 new migrations; one moves statement links on family_documents into metadata. Back up before upgrading.

System Health โ†’ AI Status now shows worker-run probe results and how long they're cached (closes #3279). Rule runs record a mixed rule's ordinary changes as modified and no longer hang when a fast AI job reports before the runner counts it (closes #5458, #5546).