Say what the routing code does, not which PR brings what

AGENTS.md keeps PR history out of comments. Section numbers pointed at a
design document that isn't in this repository, and "until PR 7" or
"(2c)" narrated the series; both go stale. Comments and the NoAiEnv
allowlist labels now state the current behaviour or the feature served.
As done for 2a on #4773.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016zAWUV2Sxn9CgsmcUqojAZ
This commit is contained in:
josh-sanders
2026-11-08 07:50:04 +10:00
co-authored by Claude Opus 5.5
parent 8dde3a29eb
commit e967c6b587
24 changed files with 85 additions and 103 deletions
+26 -27
View File
@@ -8,9 +8,8 @@ require:
Sure/SortedList:
Enabled: true
# AI configuration isn't read from ENV (docs/proposals/v0.8/ai-plumbing-redesign.md, section 4.7).
# Ai::LegacyEnvConfig reads the deprecated variables until they're removed; migrations are history.
# The rest is today's code, which still reads them until the part named beside it replaces it.
# AI configuration isn't read from ENV. Ai::LegacyEnvConfig reads the deprecated variables, and
# migrations are history. The allowlist is code that still reads them, with the feature it serves.
# This list only shrinks: test/lib/rubocop/cop/sure/no_ai_env_test.rb fails on an entry with
# nothing left to flag.
Sure/NoAiEnv:
@@ -23,30 +22,30 @@ Sure/NoAiEnv:
- "app/models/ai/legacy_env_config.rb"
- "db/migrate/**/*"
# allowlist
- "app/controllers/settings/hostings_controller.rb" # PR 7: settings pages
- "app/models/ai_health.rb" # PR 8: health page
- "app/models/ai_health/probe.rb" # PR 3: probes as jobs
- "app/models/assistant/external.rb" # PR 6: chat
- "app/models/assistant/responder.rb" # PR 6: chat
- "app/models/assistant/token_budget.rb" # PR 6: chat
- "app/models/chat.rb" # PR 6: chat
- "app/models/eval/provider_factory.rb" # 2c: evals on the router
- "app/models/family/ai_promptable.rb" # 2c: prompt files
- "app/models/family/ai_settings.rb" # PR 7: family settings
- "app/models/provider/anthropic.rb" # PR 6: chat
- "app/models/provider/anthropic/auto_categorizer.rb" # 2c
- "app/models/provider/anthropic/auto_merchant_detector.rb" # 2c
- "app/models/provider/anthropic/bank_statement_extractor.rb" # 2e
- "app/models/provider/anthropic/bill_setup_suggester.rb" # 2c
- "app/models/provider/anthropic/pdf_processor.rb" # 2e
- "app/models/provider/anthropic/provider_merchant_enhancer.rb" # 2c
- "app/models/provider/jev.rb" # 2d
- "app/models/provider/llm_concept.rb" # 2c
- "app/models/provider/openai.rb" # PR 6: chat
- "app/models/provider/openai/concerns/json_mode.rb" # 2c
- "app/models/provider/registry.rb" # 2e, PR 6
- "app/models/setting.rb" # PR 7: settings pages
- "app/models/vector_store/pgvector.rb" # 2e
- "app/controllers/settings/hostings_controller.rb" # settings pages
- "app/models/ai_health.rb" # health page
- "app/models/ai_health/probe.rb" # health probes
- "app/models/assistant/external.rb" # chat
- "app/models/assistant/responder.rb" # chat
- "app/models/assistant/token_budget.rb" # chat
- "app/models/chat.rb" # chat
- "app/models/eval/provider_factory.rb" # evals
- "app/models/family/ai_promptable.rb" # prompt overrides
- "app/models/family/ai_settings.rb" # family settings
- "app/models/provider/anthropic.rb" # chat
- "app/models/provider/anthropic/auto_categorizer.rb" # categorization
- "app/models/provider/anthropic/auto_merchant_detector.rb" # merchant detection
- "app/models/provider/anthropic/bank_statement_extractor.rb" # PDF import
- "app/models/provider/anthropic/bill_setup_suggester.rb" # bill setup
- "app/models/provider/anthropic/pdf_processor.rb" # PDF import
- "app/models/provider/anthropic/provider_merchant_enhancer.rb" # merchant enhancement
- "app/models/provider/jev.rb" # Jev
- "app/models/provider/llm_concept.rb" # LLM calls
- "app/models/provider/openai.rb" # chat
- "app/models/provider/openai/concerns/json_mode.rb" # JSON mode
- "app/models/provider/registry.rb" # provider lookup
- "app/models/setting.rb" # settings pages
- "app/models/vector_store/pgvector.rb" # embeddings
Layout/IndentationWidth:
Enabled: true
+1 -1
View File
@@ -1,4 +1,4 @@
# One guard for controller actions that start an AI feature (ai-plumbing-redesign.md, section 15).
# One guard for controller actions that start an AI feature.
# It answers only "is this AI feature available for this family and user?", through the router:
#
# requires_ai :bill_setup
+3 -4
View File
@@ -39,10 +39,9 @@ module BillsHelper
date == Date.current ? t("bills.row_today") : bills_upcoming_date(date)
end
# The bills buttons that start a chat (the AI review, the prompt chips) keep
# today's check until chat moves onto the router in PR 6, which replaces this
# with `requires_ai :chat`. Smart-fill and smart-configure ask the router for
# `bill_setup` (ai_feature_available?).
# The bills buttons that start a chat (the AI review, the prompt chips) use
# chat's own check, since chat doesn't go through the router. Smart-fill and
# smart-configure ask the router for `bill_setup` (ai_feature_available?).
def bills_ai_chat_available?
Current.user&.ai_enabled? && Provider::Registry.preferred_llm_provider.present?
end
+1 -1
View File
@@ -1,4 +1,4 @@
# The AI layer's façade (ai-plumbing-redesign.md, PR #4440). Ask it whether a feature can run
# The AI layer's façade. Ask it whether a feature can run
# instead of looking for a provider:
#
# Ai.available?(:enhance_merchants, family: Current.family, user: Current.user)
+3 -5
View File
@@ -1,9 +1,7 @@
# Why a feature can or can't run for a family and user (section 16). The router returns one
# with a reason when nothing may run, and Ai.availability returns an available one otherwise.
# Why a feature can or can't run for a family and user. The router returns one with a reason
# when nothing may run, and Ai.availability returns an available one otherwise.
#
# Reasons in 2b: :not_configured, :capability_missing and :user_disabled. PR 7 adds
# :no_family_consent and :consent_outdated; PR 9 :endpoint_not_allowed, :key_missing,
# :cost_cap_reached and :blocked_by_admin.
# Reasons: :not_configured, :capability_missing and :user_disabled.
Ai::Availability = Data.define(:feature, :reason) do
def self.available(feature) = new(feature: feature.to_sym, reason: nil)
+4 -5
View File
@@ -1,10 +1,9 @@
# Every instance-wide AI value a process uses: endpoints, their models, and one ordered list of
# routes per feature (sections 4.1, 4.6). Every AI feature reads it through Ai::Router; nothing
# else reads AI ENV or AI `Setting` fields (cop Sure/NoAiEnv).
# routes per feature. Every AI feature reads it through Ai::Router; nothing else reads AI ENV or
# AI `Setting` fields (cop Sure/NoAiEnv).
#
# Until PR 7 it's built in each process by Ai::LegacyEnvConfig from today's ENV and `Setting`
# fields, so the web and the workers can disagree, as they do today. PR 7 builds it from the
# `ai_*` tables instead.
# Ai::LegacyEnvConfig builds it in each process from the AI ENV and `Setting` fields, so the web
# and the workers can disagree when their ENV differs.
class Ai::Config
attr_reader :endpoints, :models, :routes, :vector_store_backend
+1 -2
View File
@@ -1,6 +1,5 @@
# Holds the AI configuration snapshot for one request or job, rebuilt when one of its inputs
# changes, so a value saved in the UI applies at once, as today. PR 7 replaces this with a
# per-process cache and a version stamp (section 4.6).
# changes, so a value saved in the UI applies at once.
class Ai::Config::Current < ActiveSupport::CurrentAttributes
attribute :config, :fingerprint
end
+2 -5
View File
@@ -1,8 +1,5 @@
# Who has agreed to what (section 6). Until PR 7 this keeps today's checks (call by
# @josh-sanders during development of PR #4440): chat and bills need the user's own AI switch,
# `users.ai_enabled`, and nothing else needs any consent. PR 7 replaces USER_SWITCH with
# the route spec's `consent: :user` and adds the family's consent per feature; PR 9 adds the member
# switch a family admin sets.
# Who has agreed to what. Chat and bills need the user's own AI switch, `users.ai_enabled`, and
# nothing else needs any consent.
module Ai::Consent
USER_SWITCH = %i[chat bill_setup].freeze
+3 -5
View File
@@ -1,4 +1,4 @@
# Deprecated: reads today's AI ENV and `Setting` fields as an AI configuration (section 4.5), so
# Deprecated: reads the AI ENV and `Setting` fields as an AI configuration, so
# installs configured either way keep working while the router replaces today's lookups.
#
# Each value resolves the way today's code resolves it. Wherever today's code has a resolver
@@ -7,12 +7,10 @@
# checks that over every documented setup. The one deliberate difference is drift bug 5:
# embeddings fall back to the OpenAI values saved in Settings, as every other AI setting does.
#
# Until PR 7 it's built in every process. PR 7 loads the same structure into the `ai_*` tables
# instead, with a deprecation warning, and the release that stops reading the old variables
# deletes this file.
# It's built in every process. The release that stops reading the old variables deletes this file.
class Ai::LegacyEnvConfig
# OpenAI's vector store embeds files on its own side, so the `embed` route names this stand-in
# model on the OpenAI endpoint. PR 7's OpenAI preset names the store's embedding model.
# model on the OpenAI endpoint.
VECTOR_STORE_MODEL = "vector_store".freeze
LLM_FEATURES = %i[bill_setup extract_pdf detect_merchants enhance_merchants].freeze
EMBEDDING_TIMEOUT = 120 # VectorStore::Embeddable's client
+1 -1
View File
@@ -1,5 +1,5 @@
# What may run for a feature: its lane and the ordered routes this family and user may use
# (section 5.3). Failover and escalation (2c) move down `routes`.
# (`routes`, in order).
Ai::Plan = Data.define(:feature, :lane, :routes) do
def available? = true
+4 -4
View File
@@ -1,8 +1,8 @@
# One entry in a feature's ordered model list (section 5.2): a model on an endpoint. The first
# entry is the normal choice; later ones are for failover and escalation (2c).
# One entry in a feature's ordered model list: a model on an endpoint. The first entry is the
# normal choice; later ones are for failover and escalation.
#
# `condition` limits who an entry serves. Until 2d, a Jev entry serves only families that chose
# Jev (`:jev_family`), which is today's rule; 2d deletes it with `families.categorization_provider`.
# `condition` limits who an entry serves: a Jev entry serves only families that chose Jev
# (`:jev_family`).
Ai::Route = Data.define(:feature, :position, :model, :condition) do
def initialize(feature:, position:, model:, condition: nil)
super(feature: feature.to_sym, position:, model:, condition:)
+2 -3
View File
@@ -1,10 +1,9 @@
# THE resolution path (section 5.3): for this feature, family and user, what may run, and if
# THE resolution path: for this feature, family and user, what may run, and if
# nothing may, why. Every feature asks it, through Ai.available? and Ai.availability, instead of
# looking for a provider itself.
#
# It reads only Ai::Config, Ai::Routes and Ai::Consent, and never looks at liveness: down
# endpoints are the caller's concern at run time (2c), so availability doesn't flicker. PR 7 adds
# the family's consent to Ai::Consent; PR 9 adds the family's allowed endpoints and own keys here.
# endpoints are the caller's concern at run time, so availability doesn't flicker.
class Ai::Router
def initialize(family:, user: nil, config: Ai::Config.current)
@family, @user, @config = family, user, config
+4 -4
View File
@@ -1,4 +1,4 @@
# THE routing table (section 5.1): each AI feature's lane, what its models need, its consent
# THE routing table: each AI feature's lane, what its models need, its consent
# level and the data it sends. Changing any of these changes what data leaves the box, so it
# goes through review and the architecture tests and can't be changed at runtime.
#
@@ -11,7 +11,7 @@ module Ai::Routes
sends: %i[chat_messages looked_up_financial_data stored_document_text] },
bill_setup: { lane: :interactive, needs: %i[structured_output], consent: :user,
sends: %i[transaction_dates transaction_amounts transaction_names category_names bill_configuration] },
extract_pdf: { lane: :prompt, needs: %i[structured_output], consent: :user, # vision, documents, ocr: per way of reading (2e)
extract_pdf: { lane: :prompt, needs: %i[structured_output], consent: :user, # vision, documents, ocr: per way of reading
sends: %i[uploaded_document] },
embed: { lane: :prompt, needs: %i[embedding], consent: :user,
sends: %i[depends_on_vector_store] },
@@ -37,14 +37,14 @@ module Ai::Routes
def needs_for(feature) = fetch(source_of(feature)).fetch(:needs)
# A shadow has its own consent (section 8.2); `consent_as:` puts a feature under another's row.
# A shadow has its own consent; `consent_as:` puts a feature under another's row.
def consent_key(feature) = fetch(feature)[:consent_as] || feature.to_sym
# The consent row a person sees the feature under: a shadow shows under its primary's row.
# Routing (`route_of:`) doesn't move a feature to another row; only `consent_as:` does.
def row_key(feature) = consent_key(fetch(feature)[:shadow_of] || feature)
# The consent rows on the AI features page (PR 7), one per thing a person recognises.
# The consent rows a person sees, one per thing they recognise.
def consent_keys = features.reject { fetch(_1)[:shadow_of] }.map { consent_key(_1) }.uniq
# Everything sent under one consent key, so a row's manifest covers its whole group.
+2 -2
View File
@@ -9,8 +9,8 @@ class Assistant::Function
end
# The AI feature (a key in Ai::Routes::MATRIX) a tool calls a model for, if any. A tool that
# declares one is listed only while that feature is available (ai-plumbing-redesign.md,
# section 14), so chat and MCP clients never see a tool that can't work by setup.
# declares one is listed only while that feature is available, so chat and MCP clients never
# see a tool that can't work by setup.
def ai_feature(feature = nil)
feature ? @ai_feature = feature : @ai_feature
end
@@ -105,7 +105,7 @@ class Assistant::Function::ImportBankStatement < Assistant::Function
end
# Extract transactions from the PDF using the configured LLM provider, the
# first in extract_pdf's route, until the call itself moves onto it (2e).
# first in extract_pdf's route.
provider = Provider::Registry.preferred_llm_provider
response = provider.extract_bank_statement(
+6 -7
View File
@@ -1,7 +1,7 @@
# Every family AI setting and the methods that read them (ai-plumbing-redesign.md, section 6), kept
# out of family.rb, which many open PRs touch. Family::AiPromptable keeps the prompt overrides.
# Every family AI setting and the methods that read them, kept out of family.rb.
# Family::AiPromptable keeps the prompt overrides.
#
# Until PR 7, the ENV overrides here still win over the family's columns, as they always have.
# The ENV overrides here win over the family's columns.
module Family::AiSettings
extend ActiveSupport::Concern
@@ -46,8 +46,8 @@ module Family::AiSettings
# Credentials alone never select Jev; the family has to choose it, because
# this decides whose transaction descriptions reach a third party.
#
# The categorization run calls this until it moves onto the router (2c); the
# router's `categorize` plan names the same provider first (Ai::LegacyEnvConfig).
# The categorization run calls this; the router's `categorize` plan names the
# same provider first (Ai::LegacyEnvConfig).
# It writes nothing itself; a caller that runs categorization passes a block to
# hear why Jev could not be built.
def resolved_categorization_provider(&on_jev_error)
@@ -85,8 +85,7 @@ module Family::AiSettings
end
end
# An external assistant answers this family's chats. Reads today's assistant type until chat
# mode gets its own column (PR 7).
# An external assistant answers this family's chats, read from the assistant type.
def chat_mode_external?
effective_assistant_type == "external"
end
+1 -1
View File
@@ -215,7 +215,7 @@ class PdfImport < Import
def process_with_ai
require_ai_for_extraction!
# The first provider in extract_pdf's route, until the call moves onto it (2e).
# The first provider in extract_pdf's route.
provider = Provider::Registry.preferred_llm_provider
raise Provider::Error, I18n.t("imports.pdf_import.errors.provider_no_pdf_support") unless provider.supports_pdf_processing?
+3 -3
View File
@@ -3,9 +3,9 @@
module RuboCop
module Cop
module Sure
# Flags ENV reads of AI configuration (ai-plumbing-redesign.md, section 4.7). ENV variables
# are permanent once released and differ between containers, so AI configuration lives in
# the `ai_*` tables and the AI config file instead. Only the loaders read the old variables.
# Flags ENV reads of AI configuration. ENV variables are permanent once released and differ
# between containers, so AI code reads its configuration through Ai::Config instead. Only the
# loaders read the old variables.
#
# # bad
# ENV["OPENAI_MODEL"]
+8 -8
View File
@@ -1,25 +1,25 @@
require "test_helper"
# PR #4440's E2, kept: in every setup today's code supports, each place that shows or starts an AI
# feature gives the same answer as Ai.available? (ai-plumbing-redesign.md, section 17).
# In every supported setup, each place that shows or starts an AI feature gives the same answer
# as Ai.available?.
#
# Chat's entry points are asked too, but they move onto the router only in PR 6, so their known
# Chat's entry points are asked too, but chat doesn't go through the router, so their known
# disagreements are on CHAT_ALLOWLIST. The list may only shrink: an entry that no longer
# disagrees fails the test, and PR 6 empties it.
# disagrees fails the test.
class AiAgreementTest < ActionDispatch::IntegrationTest
include AiLegacySetups
include BillsHelper
include InsightsHelper
# [setup, entry point] => why chat's entry point and the router disagree until PR 6.
# [setup, entry point] => why chat's entry point and the router disagree.
CHAT_ALLOWLIST = {
%w[C6 chat_page] => "the chat page is offered, but no model is set, so every turn fails",
%w[C8 chat_page] => "the chat page is offered, but chat's allow-list refuses o4-mini, so every turn fails",
%w[C8 dig_in] => "Dig in asks for the batch provider, which takes o4-mini; chat refuses it",
%w[C8 bills_chat] => "the bills chat buttons ask for the batch provider, which takes o4-mini; chat refuses it",
%w[M5 chat_page] => "mcp_only turns chat off through User#ai_available?; Ai::Consent gets that line in PR 6",
%w[M5 bills_chat] => "mcp_only turns the bills chat buttons off; Ai::Consent gets that line in PR 6",
%w[M5 dig_in] => "mcp_only turns Dig in off; Ai::Consent gets that line in PR 6"
%w[M5 chat_page] => "mcp_only turns chat off through User#ai_available?, which Ai::Consent doesn't check",
%w[M5 bills_chat] => "mcp_only turns the bills chat buttons off, which Ai::Consent doesn't check",
%w[M5 dig_in] => "mcp_only turns Dig in off, which Ai::Consent doesn't check"
}.freeze
setup do
+2 -4
View File
@@ -1,9 +1,7 @@
require "test_helper"
# The deprecated ENV path resolves every setup today's code supports to the routes today's code
# would use (ai-plumbing-redesign.md, section 17), so moving a feature onto the router changes
# nothing until a PR means it to. PR 7 adds the halves for the tables, the config file and
# ai:export_config.
# The deprecated ENV path resolves every supported setup to the routes the code not yet on the
# router would use, so moving a feature onto the router changes nothing unless it means to.
class AiConfigTest < ActiveSupport::TestCase
include AiLegacySetups
+2 -3
View File
@@ -1,7 +1,7 @@
require "test_helper"
# Every controller action that starts an AI feature declares `requires_ai` for it, and the guard
# reads nothing but Ai.availability (ai-plumbing-redesign.md, section 17).
# reads nothing but Ai.availability.
class AiFeatureGuardTest < ActiveSupport::TestCase
# "Controller#action" => the feature it starts.
AI_ACTIONS = {
@@ -10,8 +10,7 @@ class AiFeatureGuardTest < ActiveSupport::TestCase
"FamilyMerchantsController#enhance" => :enhance_merchants
}.freeze
# Chat's entry points keep today's checks until PR 6 moves them to `requires_ai :chat`. The list
# may only shrink.
# Chat's entry points keep chat's own checks, not `requires_ai :chat`. The list may only shrink.
CHAT_ALLOWLIST = %w[
ChatsController#create
ChatsController#retry
+1 -2
View File
@@ -1,7 +1,6 @@
require "test_helper"
# The routing table's invariants (ai-plumbing-redesign.md, section 17). 2c adds the check that
# every generation declares a feature in the table.
# The routing table's invariants.
class AiRoutingMatrixTest < ActiveSupport::TestCase
MATRIX = Ai::Routes::MATRIX
LANES = %i[interactive prompt background].freeze
@@ -1,7 +1,7 @@
require "test_helper"
# An AI-dependent tool is listed only while its feature is available (ai-plumbing-redesign.md,
# section 14), for chat and MCP alike, since both list Assistant.function_classes.
# An AI-dependent tool is listed only while its feature is available, for chat and MCP alike,
# since both list Assistant.function_classes.
class Assistant::AiFeatureToolsTest < ActiveSupport::TestCase
setup { @user = users(:family_admin) }
+2 -3
View File
@@ -3,9 +3,8 @@
module AiTestHelper
LLM_CAPABILITIES = %i[tools vision documents streaming structured_output].freeze
# One configured setup for every test (section 17): a cloud `openai` endpoint, a local `gpu`, an
# `embed` server, a cloud `jev` and an external assistant (`agent`), with every feature routed.
# PR 7 replaces it with table fixtures.
# One configured setup for every test: a cloud `openai` endpoint, a local `gpu`, an `embed`
# server, a cloud `jev` and an external assistant (`agent`), with every feature routed.
def self.test_config
@test_config ||= begin
endpoint = ->(name, **attributes) { Ai::Config::Endpoint.new(name:, protocol: :compatible, api_key: "test-key", **attributes) }