Name a feature's consent row in one place; tidy the router

Ai::Routes.row_key replaces the two inline copies in Availability and
doc_table. It follows shadow_of and consent_as, not route_of: routing
through embed doesn't put a feature under embed's row. The router reuses
its spec, and Ai::Consent.denial drops the spec it never read. Review
on #4774 and #4775.

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-07 18:44:41 +10:00
co-authored by Claude Opus 5.5
parent cdac9660ba
commit 77b8a8f497
5 changed files with 17 additions and 6 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ Ai::Availability = Data.define(:feature, :reason) do
return if available?
audience = user&.manages_instance_settings? ? :admin : :member
feature_name = I18n.t("ai.consent_rows.#{Ai::Routes.consent_key(Ai::Routes.fetch(feature)[:shadow_of] || feature)}")
feature_name = I18n.t("ai.consent_rows.#{Ai::Routes.row_key(feature)}")
I18n.t("ai.availability.#{reason}.#{audience}", feature: feature_name, default: I18n.t("ai.availability.#{reason}.member", feature: feature_name))
end
end
+2 -2
View File
@@ -1,13 +1,13 @@
# 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
# `spec[:consent] == :user` and adds the family's consent per feature; PR 9 adds the member
# the route spec's `consent: :user` and adds the family's consent per feature; PR 9 adds the member
# switch a family admin sets.
module Ai::Consent
USER_SWITCH = %i[chat bill_setup].freeze
# @return [Symbol, nil] the reason the feature is denied, or nil when it's allowed
def self.denial(feature, spec, family:, user:)
def self.denial(feature, family:, user:)
return :user_disabled if USER_SWITCH.include?(Ai::Routes.consent_key(feature)) && !user&.ai_enabled
nil
+2 -2
View File
@@ -14,11 +14,11 @@ class Ai::Router
def resolve(feature)
feature = feature.to_sym
spec = Ai::Routes.fetch(feature)
listed = @config.routes_for(Ai::Routes.fetch(feature)[:route_of] || feature)
listed = @config.routes_for(spec[:route_of] || feature)
.select { |route| route.agent? == agent_chat?(feature) && route.serves_user?(@user) && route.serves?(@family) }
return Ai::Availability.unavailable(feature, :not_configured) if listed.empty? # first: nothing to consent to
if (reason = Ai::Consent.denial(feature, spec, family: @family, user: @user))
if (reason = Ai::Consent.denial(feature, family: @family, user: @user))
return Ai::Availability.unavailable(feature, reason)
end
+5 -1
View File
@@ -40,6 +40,10 @@ module Ai::Routes
# A shadow has its own consent (section 8.2); `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.
def consent_keys = features.reject { fetch(_1)[:shadow_of] }.map { consent_key(_1) }.uniq
@@ -55,7 +59,7 @@ module Ai::Routes
rows = MATRIX.map do |feature, spec|
needs = Ai::Routes.needs_for(feature)
needs = needs.is_a?(Hash) ? needs.map { |kind, list| "#{kind}: #{list.join(", ")}" }.join("; ") : needs.join(", ")
row = I18n.t("ai.consent_rows.#{consent_key(spec[:shadow_of] || feature)}")
row = I18n.t("ai.consent_rows.#{row_key(feature)}")
row += " (shadow runs)" if spec[:shadow_of]
sends = Array(spec[:sends] || fetch(spec[:shadow_of])[:sends]).map { I18n.t("ai.sends.#{_1}", locale: :en) }.join("; ")
"| `#{feature}` | #{spec[:lane]} | #{needs} | #{row} (#{spec[:consent]}) | #{sends} |"
+7
View File
@@ -14,6 +14,13 @@ class Ai::RoutesTest < ActiveSupport::TestCase
assert_equal %i[chat bill_setup extract_pdf embed categorize detect_merchants], Ai::Routes.consent_keys
end
test "a shadow shows under its primary's row; routing through another feature doesn't move a row" do
assert_equal :categorize, Ai::Routes.row_key(:shadow_categorize)
assert_equal :embed, Ai::Routes.row_key(:search_documents)
assert_equal :detect_merchants, Ai::Routes.row_key(:enhance_merchants)
assert_equal :chat, Ai::Routes.row_key(:chat)
end
test "a consent row's manifest is everything its group sends" do
assert_equal %i[transaction_names merchant_names], Ai::Routes.sends_for(:detect_merchants)
assert_equal %i[depends_on_vector_store search_query], Ai::Routes.sends_for(:embed)