Module: Legion::LLM::Router::BodyModelHintPolicy

Extended by:
Legion::Logging::Helper
Defined in:
lib/legion/llm/router/body_model_hint_policy.rb

Overview

The SOLE body-model hint decision (SSOT v3 §17.1 / D19). Given the untrusted request-body model value and any trusted explicit model, it returns one immutable Phase 1 BodyModelHintDecision. It never returns a lane, substitute model, or alias; only a honored decision carries a model constraint. PayloadBuilder/translators/executor may not independently reinterpret body.model — they consume this decision.

X-Legion-Model (the trusted channel) is intentionally EXEMPT from the whitelist/blacklist lists: a nonblank trusted model always supersedes.

Class Method Summary collapse

Class Method Details

.call(body_model:, trusted_model:, settings_snapshot:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/legion/llm/router/body_model_hint_policy.rb', line 18

def self.call(body_model:, trusted_model:, settings_snapshot:)
  requested = normalize(body_model)
  generation = settings_snapshot.generation

  # 1. missing/blank body model → absent (requested_model carries nil).
  return decision(requested_model: nil, disposition: :absent, settings_generation: generation) if requested.nil?

  # 2. body + trusted explicit model → trusted wins, body is metadata only.
  unless normalize(trusted_model).nil?
    return decision(requested_model: requested, disposition: :superseded_by_explicit_model,
                    settings_generation: generation)
  end

  # 3. auto-routing alias → auto (you-pick intent, no constraint).
  return decision(requested_model: requested, disposition: :auto, settings_generation: generation) if auto_alias?(requested, settings_snapshot.auto_routing_model_aliases)

  # 4. body hints globally disabled → ignored.
  unless settings_snapshot.allow_body_routing_hints
    return decision(requested_model: requested, disposition: :ignored_disabled,
                    settings_generation: generation)
  end

  whitelist = settings_snapshot.body_model_hint_whitelist
  blacklist = settings_snapshot.body_model_hint_blacklist

  # 5. nonempty whitelist with no match → ignored_not_whitelisted.
  if !whitelist.empty? && substring_match(requested, whitelist).nil?
    return decision(requested_model: requested, disposition: :ignored_not_whitelisted,
                    settings_generation: generation)
  end

  # 6. any blacklist match → ignored_blacklisted (blacklist wins over whitelist).
  matched_black = substring_match(requested, blacklist)
  unless matched_black.nil?
    return decision(requested_model: requested, disposition: :ignored_blacklisted,
                    matched_blacklist: matched_black, matched_whitelist: substring_match(requested, whitelist),
                    settings_generation: generation)
  end

  # 7. honored → exact body model becomes the model constraint.
  decision(requested_model: requested, disposition: :honored, model_constraint: requested,
           matched_whitelist: substring_match(requested, whitelist), settings_generation: generation)
end