Module: Legion::LLM::Inference::Steps::TierAssigner

Extended by:
Legion::Logging::Helper
Defined in:
lib/legion/llm/inference/steps/tier_assigner.rb

Constant Summary collapse

DEFAULT_MAPPINGS =
[
  { pattern: 'gaia:tick:*', tier: :local, intent: { cost: :minimize } },
  { pattern: 'gaia:dream:*', tier: :local, intent: { cost: :minimize } },
  { pattern: 'system:guardrails', tier: :local, intent: { cost: :minimize, capability: :basic } },
  { pattern: 'system:reflection', tier: :local, intent: { cost: :minimize, capability: :moderate } },
  { pattern: 'user:*', tier: :frontier, intent: { capability: :reasoning } }
].freeze

Class Method Summary collapse

Class Method Details

.assign(caller:, classification:, priority:, gaia_hint:, existing_tier:, existing_intent: nil) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



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
# File 'lib/legion/llm/inference/steps/tier_assigner.rb', line 22

def assign(caller:, classification:, priority:, gaia_hint:, existing_tier:, existing_intent: nil) # rubocop:disable Lint/UnusedMethodArgument
  # Privacy classifications are hard routing constraints. They must
  # override caller-supplied tier/intent and advisory signals.
  if privacy_constrained?(classification)
    log.info('[llm][routing] tier_assigned source=classification tier=local forced=true')
    return { tier: :local, intent: { privacy: :strict }, source: :classification, forced: true }
  end

  if existing_tier
    log.debug("[llm][routing] tier_preserved tier=#{existing_tier}")
    return nil
  end

  # 1. GAIA routing hint
  recommended = nested_value(gaia_hint, :data, :recommended_tier)
  if recommended
    log.info("[llm][routing] tier_assigned source=gaia tier=#{recommended}")
    return { tier: recommended.to_sym, source: :gaia }
  end

  # 2. Settings-driven role mappings
  mapping = find_role_mapping(caller)
  return mapping if mapping

  # 3. Priority-driven
  case priority&.to_sym
  when :critical, :high
    log.info("[llm][routing] tier_assigned source=priority tier=frontier priority=#{priority}")
    { tier: :frontier, intent: { capability: :reasoning }, source: :priority }
  when :low, :background
    log.info("[llm][routing] tier_assigned source=priority tier=local priority=#{priority}")
    { tier: :local, intent: { cost: :minimize }, source: :priority }
  end
end

.find_role_mapping(caller) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/legion/llm/inference/steps/tier_assigner.rb', line 65

def find_role_mapping(caller)
  identity = nested_value(caller, :requested_by, :identity)
  return nil unless identity

  identity = identity.to_s
  tier_mappings.each do |mapping|
    next unless File.fnmatch?(value(mapping, :pattern).to_s, identity)

    tier = value(mapping, :tier)
    log.info("[llm][routing] tier_mapping identity=#{identity} tier=#{tier}")
    return { tier: tier&.to_sym, intent: value(mapping, :intent), source: :role_mapping }
  end
  nil
end

.nested_value(hash, *keys) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/legion/llm/inference/steps/tier_assigner.rb', line 94

def nested_value(hash, *keys)
  keys.reduce(hash) do |current, key|
    return nil unless current.respond_to?(:key?)

    value(current, key)
  end
end

.privacy_constrained?(classification) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
# File 'lib/legion/llm/inference/steps/tier_assigner.rb', line 57

def privacy_constrained?(classification)
  return false unless classification

  value(classification, :contains_phi) ||
    value(classification, :contains_pii) ||
    value(classification, :level)&.to_sym == :restricted
end

.tier_mappingsObject



80
81
82
83
# File 'lib/legion/llm/inference/steps/tier_assigner.rb', line 80

def tier_mappings
  configured = Legion::LLM::Settings.value(:routing, :tier_mappings)
  configured.nil? || configured.empty? ? DEFAULT_MAPPINGS : configured
end

.value(hash, key) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/legion/llm/inference/steps/tier_assigner.rb', line 85

def value(hash, key)
  return nil unless hash.respond_to?(:key?)

  string_key = key.to_s
  return hash[string_key] if hash.key?(string_key)

  hash[key] if hash.key?(key)
end