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
56
57
58
59
60
61
# 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 }
  else
    log.debug(
      '[llm][steps][tier_assigner] action=no_assignment ' \
      "caller=#{caller&.dig(:requested_by, :identity) || 'none'} priority=#{priority || 'none'}"
    )
    nil
  end
end

.find_role_mapping(caller) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/legion/llm/inference/steps/tier_assigner.rb', line 71

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



100
101
102
103
104
105
106
# File 'lib/legion/llm/inference/steps/tier_assigner.rb', line 100

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)


63
64
65
66
67
68
69
# File 'lib/legion/llm/inference/steps/tier_assigner.rb', line 63

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



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

def tier_mappings
  configured = Legion::Settings[:llm][:routing][:tier_mappings]
  configured.nil? || configured.empty? ? DEFAULT_MAPPINGS : configured
end

.value(hash, key) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/legion/llm/inference/steps/tier_assigner.rb', line 91

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