Keep categorization provider resolution read-only

This commit is contained in:
sure-admin
2026-09-23 01:04:11 +00:00
parent e4fe1da5c4
commit 683ad3522c
6 changed files with 123 additions and 60 deletions
+3 -18
View File
@@ -173,7 +173,8 @@ class Family < ApplicationRecord
# this decides whose transaction descriptions reach a third party.
#
# Anything naming the provider calls this, including the rule confirmation
# screen's cost estimate, or it quotes a provider that will not run.
# screen's cost estimate, or it quotes a provider that will not run. Keep this
# side-effect free: views call it while rendering rule labels.
def resolved_categorization_provider
jev = configured_jev_provider if effective_categorization_provider == "jev"
@@ -225,23 +226,7 @@ class Family < ApplicationRecord
# which JEV_ENDPOINT can set without passing the settings form's check.
def configured_jev_provider
Provider::Registry.get_provider(:jev)
rescue Provider::Error => error
# Without this the only symptom is categorization silently running on the
# provider the family did not choose. The endpoint is redacted because a
# gateway URL can carry a key in userinfo or the query string.
DebugLogEntry.capture(
category: "auto_categorization",
level: "error",
message: "Jev is misconfigured; falling back to the LLM provider",
source: self.class.name,
family: self,
provider: "jev",
metadata: {
endpoint: Provider::Jev.redacted_endpoint(Provider::Jev.effective_endpoint),
error_class: error.class.name,
error_message: error.message
}
)
rescue Provider::Error
nil
end
private :configured_jev_provider
+34 -7
View File
@@ -122,13 +122,40 @@ class Family::AutoCategorizer
def categorization_provider
return @categorization_provider if defined?(@categorization_provider)
# Resolution lives on Family so the rule confirmation screen names the
# same provider this run will use. The preview flag gates whether the
# selector is offered at all (see
# docs/llm-guides/gating-a-preview-feature.md); it plays no part here, so
# a family that opted in and then chose the LLM provider gets the LLM
# provider.
@categorization_provider = family.resolved_categorization_provider
if family.effective_categorization_provider == "jev"
# Resolution usually lives on Family so UI and execution agree. This run
# path handles Jev directly because misconfiguration diagnostics write a
# DebugLogEntry, and view-time resolver calls must stay read-only.
jev = configured_jev_provider
end
@categorization_provider = jev || Provider::Registry.preferred_llm_provider
end
def configured_jev_provider
Provider::Registry.get_provider(:jev)
rescue Provider::Error => error
log_jev_misconfiguration(error)
nil
end
def log_jev_misconfiguration(error)
# Without this the only symptom is categorization silently running on the
# provider the family did not choose. The endpoint is redacted because a
# gateway URL can carry a key in userinfo or the query string.
DebugLogEntry.capture(
category: "auto_categorization",
level: "error",
message: "Jev is misconfigured; falling back to the LLM provider",
source: self.class.name,
family: family,
provider: "jev",
metadata: {
endpoint: Provider::Jev.redacted_endpoint(Provider::Jev.effective_endpoint),
error_class: error.class.name,
error_message: error.message
}
)
end
# Only providers reporting calibrated confidence can be gated. The LLM
@@ -35,11 +35,11 @@ class Rule::Registry::TransactionResource < Rule::Registry
Rule::ActionExecutor::SendEmailNotification.new(rule)
]
if auto_categorization_enabled?
if auto_categorization_enabled? || existing_action?("auto_categorize")
enabled_executors << Rule::ActionExecutor::AutoCategorize.new(rule)
end
if auto_merchant_detection_enabled?
if auto_merchant_detection_enabled? || existing_action?("auto_detect_merchants")
enabled_executors << Rule::ActionExecutor::AutoDetectMerchants.new(rule)
end
@@ -54,4 +54,8 @@ class Rule::Registry::TransactionResource < Rule::Registry
def auto_merchant_detection_enabled?
Provider::Registry.preferred_llm_provider.present?
end
def existing_action?(action_type)
rule.actions.any? { |action| action.action_type == action_type }
end
end
@@ -218,6 +218,40 @@ class Family::AutoCategorizerTest < ActiveSupport::TestCase
assert_equal category, txn.reload.category
end
test "logs the rejected Jev endpoint only when categorization actually runs" do
@family.update!(categorization_provider: "jev")
txn = create_transaction(account: @account, name: "Coffee shop").transaction
category = @family.categories.create!(name: "Coffee")
@llm_provider.expects(:auto_categorize).returns(provider_success_response([
AutoCategorization.new(transaction_id: txn.id, category_name: category.name)
])).once
with_env_overrides(
"JEV_API_KEY" => "test_api_key",
"JEV_ENDPOINT" => "http://someone:s3cret@gw.example.com/v1?api_key=QUERYSECRET#frag" # pipelock:ignore Credential in URL
) do
assert_difference "DebugLogEntry.count", 2 do
Family::AutoCategorizer.new(@family, transaction_ids: [ txn.id ]).auto_categorize
end
end
entry = DebugLogEntry.where(message: "Jev is misconfigured; falling back to the LLM provider").sole
assert_equal "auto_categorization", entry.category
assert_equal "error", entry.level
assert_equal "jev", entry.provider_key
assert_equal @family, entry.family
assert_equal "http://gw.example.com/v1", entry.metadata["endpoint"]
assert_equal "Provider::Jev::Error", entry.metadata["error_class"]
# error_message embeds the endpoint the constructor rejected, so it leaks
# just as readily as the endpoint field if it is not redacted at the raise.
serialized = entry.metadata.to_json
assert_not_includes serialized, "s3cret"
assert_not_includes serialized, "QUERYSECRET"
assert_not_includes serialized, "frag"
end
# Confidence has to change behaviour, not just appear in logs. Below the
# threshold the answer is withheld and the transaction is left unlocked, so a
# later run can still correct it.
+10 -24
View File
@@ -556,42 +556,28 @@ class FamilyTest < ActiveSupport::TestCase
end
end
test "resolved_categorization_provider captures the rejected endpoint for the operator" do
test "resolved_categorization_provider does not write diagnostics from view-time callers" do
family = families(:dylan_family)
family.update!(categorization_provider: "jev")
Provider::Registry.stubs(:preferred_llm_provider).returns(nil)
with_env_overrides(REJECTED_JEV_ENV) do
assert_nil family.resolved_categorization_provider
assert_no_difference "DebugLogEntry.count" do
with_env_overrides(REJECTED_JEV_ENV) do
assert_nil family.resolved_categorization_provider
end
end
entry = DebugLogEntry.order(:created_at).last
assert_equal "auto_categorization", entry.category
assert_equal "error", entry.level
assert_equal "jev", entry.provider_key
assert_equal family.id, entry.family_id
assert_equal REJECTED_JEV_ENV["JEV_ENDPOINT"], entry.metadata["endpoint"]
assert_equal "Provider::Jev::Error", entry.metadata["error_class"]
end
test "the captured diagnostic holds no endpoint credentials" do
test "categorization_model_name does not write diagnostics from view-time callers" do
family = families(:dylan_family)
family.update!(categorization_provider: "jev")
Provider::Registry.stubs(:preferred_llm_provider).returns(nil)
with_env_overrides("JEV_API_KEY" => "test_api_key", "JEV_ENDPOINT" => CREDENTIALED_JEV_ENDPOINT) do
assert_nil family.resolved_categorization_provider
assert_no_difference "DebugLogEntry.count" do
with_env_overrides(REJECTED_JEV_ENV) do
assert_nil family.categorization_model_name
end
end
entry = DebugLogEntry.order(:created_at).last
assert_equal "http://gw.example.com/v1", entry.metadata["endpoint"]
# error_message embeds the endpoint the constructor rejected, so it leaks
# just as readily as the endpoint field if it is not redacted at the raise.
serialized = entry.metadata.to_json
assert_not_includes serialized, "s3cret"
assert_not_includes serialized, "QUERYSECRET"
assert_not_includes serialized, "frag"
end
test "shadow_categorization_provider returns nil when the Jev endpoint is rejected" do
@@ -3,32 +3,59 @@ require "test_helper"
class Rule::Registry::TransactionResourceTest < ActiveSupport::TestCase
setup do
@family = families(:empty)
@rule = @family.rules.create!(
name: "Transaction registry test",
resource_type: "transaction",
effective_date: 1.day.ago.to_date,
actions: [ Rule::Action.new(action_type: "auto_categorize") ]
)
end
test "auto categorization is available when Jev is the only categorization provider" do
rule = build_rule(action_type: "auto_categorize")
@family.stubs(:resolved_categorization_provider).returns(Provider::Jev.allocate)
Provider::Registry.stubs(:preferred_llm_provider).returns(nil)
keys = @rule.registry.action_executors.map(&:key)
keys = rule.registry.action_executors.map(&:key)
assert_includes keys, "auto_categorize"
assert_not_includes keys, "auto_detect_merchants"
assert_instance_of Rule::ActionExecutor::AutoCategorize, @rule.actions.first.executor
assert_instance_of Rule::ActionExecutor::AutoCategorize, rule.actions.first.executor
end
test "existing auto categorization actions stay displayable when no provider is configured" do
rule = build_rule(action_type: "auto_categorize")
@family.stubs(:resolved_categorization_provider).returns(nil)
Provider::Registry.stubs(:preferred_llm_provider).returns(nil)
keys = rule.registry.action_executors.map(&:key)
assert_includes keys, "auto_categorize"
assert_instance_of Rule::ActionExecutor::AutoCategorize, rule.actions.first.executor
end
test "new auto categorization actions are hidden when no provider is configured" do
rule = build_rule(action_type: "exclude_transaction")
@family.stubs(:resolved_categorization_provider).returns(nil)
Provider::Registry.stubs(:preferred_llm_provider).returns(nil)
keys = rule.registry.action_executors.map(&:key)
assert_not_includes keys, "auto_categorize"
end
test "auto merchant detection is available when an LLM provider is configured" do
rule = build_rule(action_type: "exclude_transaction")
@family.stubs(:resolved_categorization_provider).returns(nil)
Provider::Registry.stubs(:preferred_llm_provider).returns(Provider::Anthropic.allocate)
keys = @rule.registry.action_executors.map(&:key)
keys = rule.registry.action_executors.map(&:key)
assert_not_includes keys, "auto_categorize"
assert_includes keys, "auto_detect_merchants"
end
private
def build_rule(action_type:)
@family.rules.create!(
name: "Transaction registry test",
resource_type: "transaction",
effective_date: 1.day.ago.to_date,
actions: [ Rule::Action.new(action_type: action_type) ]
)
end
end