Module: Legion::LLM::Pipeline::Steps::TierAssigner

Extended by:
Legion::Logging::Helper
Defined in:
lib/legion/llm/pipeline/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: :cloud, 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/pipeline/steps/tier_assigner.rb', line 22

def assign(caller:, classification:, priority:, gaia_hint:, existing_tier:, existing_intent: nil) # rubocop:disable Lint/UnusedMethodArgument
  if existing_tier
    log.debug("[llm][routing] tier_preserved tier=#{existing_tier}")
    return nil
  end

  # 1. GAIA routing hint
  recommended = gaia_hint&.dig(: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. Classification-driven: PHI/PII/restricted -> local only (fail closed)
  if classification && (classification[:contains_phi] || classification[:contains_pii] ||
                        classification[:level]&.to_sym == :restricted)
    log.info('[llm][routing] tier_assigned source=classification tier=local (phi/pii/restricted)')
    return { tier: :local, intent: { privacy: :strict }, source: :classification }
  end

  # 4. Priority-driven
  case priority&.to_sym
  when :critical, :high
    log.info("[llm][routing] tier_assigned source=priority tier=cloud priority=#{priority}")
    { tier: :cloud, 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



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/legion/llm/pipeline/steps/tier_assigner.rb', line 57

def find_role_mapping(caller)
  return nil unless caller&.dig(:requested_by, :identity)

  identity = caller[:requested_by][:identity].to_s
  tier_mappings.each do |mapping|
    next unless File.fnmatch?(mapping[:pattern], identity)

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

.tier_mappingsObject



70
71
72
73
# File 'lib/legion/llm/pipeline/steps/tier_assigner.rb', line 70

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