Module: Legion::LLM::Tools::Confidence

Extended by:
Cache::Helper, Legion::Logging::Helper
Defined in:
lib/legion/llm/tools/confidence.rb

Constant Summary collapse

OVERRIDE_THRESHOLD =
0.8
SHADOW_THRESHOLD =
0.5
SUCCESS_DELTA =
0.05
FAILURE_DELTA =
-0.1

Class Method Summary collapse

Class Method Details

.all_overridesObject



78
79
80
# File 'lib/legion/llm/tools/confidence.rb', line 78

def all_overrides
  @mutex.synchronize { @overrides_l0.values.map(&:dup) }
end

.cache_namespaceObject



23
# File 'lib/legion/llm/tools/confidence.rb', line 23

def cache_namespace = ''

.hydrate_from_apolloObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/legion/llm/tools/confidence.rb', line 99

def hydrate_from_apollo
  return unless defined?(Legion::Extensions::Apollo::Runners::Knowledge)

  results = Legion::Extensions::Apollo::Runners::Knowledge.handle_retrieve(
    tags:             %w[override mesh_confirmed],
    knowledge_domain: 'system',
    limit:            100
  )
  return unless results.is_a?(Array)

  results.each do |entry|
    ctx = entry[:context] || entry['context']
    next unless ctx.is_a?(Hash)

    tool = ctx[:tool] || ctx['tool']
    next unless tool

    @mutex.synchronize do
      next if @overrides_l0.key?(tool)

      @overrides_l0[tool] = {
        tool: tool,
        lex: ctx[:lex] || ctx['lex'],
        confidence: ((ctx[:confidence] || ctx['confidence']).to_f * 0.8).clamp(0.0, 1.0),
        hit_count: 0, miss_count: 0,
        created_at: Time.now, updated_at: Time.now
      }
    end
  end
rescue StandardError => e
  handle_exception(e, level: :debug, handled: true, operation: 'llm.tools.confidence.hydrate_from_apollo')
end

.hydrate_from_l2Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/legion/llm/tools/confidence.rb', line 86

def hydrate_from_l2
  return unless defined?(Legion::Data::Local)

  rows = Legion::Data::Local.query('SELECT * FROM override_confidence')
  @mutex.synchronize do
    rows.each do |row|
      @overrides_l0[row[:tool]] = row.merge(updated_at: Time.now)
    end
  end
rescue StandardError => e
  handle_exception(e, level: :debug, handled: true, operation: 'llm.tools.confidence.hydrate_from_l2')
end

.lookup(tool) ⇒ Object



62
63
64
65
66
# File 'lib/legion/llm/tools/confidence.rb', line 62

def lookup(tool)
  @mutex.synchronize { @overrides_l0[tool]&.dup } ||
    lookup_l1(tool) ||
    lookup_l2(tool)
end

.pending_l2_syncObject



82
83
84
# File 'lib/legion/llm/tools/confidence.rb', line 82

def pending_l2_sync
  @mutex.synchronize { @pending_l2_sync.keys }
end

.record(tool:, lex:, confidence:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/legion/llm/tools/confidence.rb', line 25

def record(tool:, lex:, confidence:)
  @mutex.synchronize do
    @overrides_l0[tool] = {
      tool: tool, lex: lex, confidence: confidence.clamp(0.0, 1.0),
      hit_count: 0, miss_count: 0, created_at: Time.now, updated_at: Time.now
    }
  end
  sync_to_l1(tool)
  schedule_l2_retry(tool) unless sync_to_l2(tool)
end

.record_failure(tool) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/legion/llm/tools/confidence.rb', line 49

def record_failure(tool)
  @mutex.synchronize do
    entry = @overrides_l0[tool]
    return unless entry

    entry[:confidence] = (entry[:confidence] + FAILURE_DELTA).clamp(0.0, 1.0)
    entry[:miss_count] += 1
    entry[:updated_at] = Time.now
  end
  sync_to_l1(tool)
  schedule_l2_retry(tool) unless sync_to_l2(tool)
end

.record_success(tool) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/legion/llm/tools/confidence.rb', line 36

def record_success(tool)
  @mutex.synchronize do
    entry = @overrides_l0[tool]
    return unless entry

    entry[:confidence] = (entry[:confidence] + SUCCESS_DELTA).clamp(0.0, 1.0)
    entry[:hit_count] += 1
    entry[:updated_at] = Time.now
  end
  sync_to_l1(tool)
  schedule_l2_retry(tool) unless sync_to_l2(tool)
end

.reset!Object



132
133
134
135
136
137
# File 'lib/legion/llm/tools/confidence.rb', line 132

def reset!
  @mutex.synchronize do
    @overrides_l0.clear
    @pending_l2_sync.clear
  end
end

.should_override?(tool) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/legion/llm/tools/confidence.rb', line 68

def should_override?(tool)
  entry = lookup(tool)
  entry.is_a?(Hash) && entry[:confidence] >= OVERRIDE_THRESHOLD
end

.should_shadow?(tool) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/legion/llm/tools/confidence.rb', line 73

def should_shadow?(tool)
  entry = lookup(tool)
  entry.is_a?(Hash) && entry[:confidence] >= SHADOW_THRESHOLD && entry[:confidence] < OVERRIDE_THRESHOLD
end