Class: Legion::Extensions::Agentic::Memory::SourceMonitoring::Helpers::SourceTracker
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Memory::SourceMonitoring::Helpers::SourceTracker
show all
- Includes:
- Constants
- Defined in:
- lib/legion/extensions/agentic/memory/source_monitoring/helpers/source_tracker.rb
Constant Summary
Constants included
from Constants
Constants::CONFIDENCE_ALPHA, Constants::CONFIDENCE_DECAY, Constants::CONFIDENCE_FLOOR, Constants::CONFIDENCE_LABELS, Constants::CONFUSION_PAIRS, Constants::DEFAULT_CONFIDENCE, Constants::MAX_ATTRIBUTIONS, Constants::MAX_HISTORY, Constants::MAX_RECORDS, Constants::REALITY_STATUS, Constants::SOURCES
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of SourceTracker.
14
15
16
17
18
19
20
|
# File 'lib/legion/extensions/agentic/memory/source_monitoring/helpers/source_tracker.rb', line 14
def initialize
@records = {}
@attribution_log = []
@record_counter = 0
@accuracy_hits = 0
@accuracy_total = 0
end
|
Instance Attribute Details
#attribution_log ⇒ Object
Returns the value of attribute attribution_log.
12
13
14
|
# File 'lib/legion/extensions/agentic/memory/source_monitoring/helpers/source_tracker.rb', line 12
def attribution_log
@attribution_log
end
|
#records ⇒ Object
Returns the value of attribute records.
12
13
14
|
# File 'lib/legion/extensions/agentic/memory/source_monitoring/helpers/source_tracker.rb', line 12
def records
@records
end
|
Instance Method Details
#attribute(content_id:) ⇒ Object
36
37
38
|
# File 'lib/legion/extensions/agentic/memory/source_monitoring/helpers/source_tracker.rb', line 36
def attribute(content_id:)
@records.values.select { |r| r.content_id == content_id }.sort_by { |r| -r.confidence }
end
|
#attribution_accuracy ⇒ Object
83
84
85
86
87
|
# File 'lib/legion/extensions/agentic/memory/source_monitoring/helpers/source_tracker.rb', line 83
def attribution_accuracy
return 0.0 if @accuracy_total.zero?
@accuracy_hits.to_f / @accuracy_total
end
|
#confused_records ⇒ Object
71
72
73
|
# File 'lib/legion/extensions/agentic/memory/source_monitoring/helpers/source_tracker.rb', line 71
def confused_records
@records.values.select(&:confused?).map(&:to_h)
end
|
#correct_source(record_id:, new_source:) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/legion/extensions/agentic/memory/source_monitoring/helpers/source_tracker.rb', line 51
def correct_source(record_id:, new_source:)
rec = @records[record_id]
return nil unless rec
return nil unless SOURCES.include?(new_source)
old = rec.source
rec.correct(new_source: new_source)
@accuracy_total += 1
log_attribution(record_id, :corrected, from: old, to: new_source)
rec
end
|
#decay_all ⇒ Object
89
90
91
92
|
# File 'lib/legion/extensions/agentic/memory/source_monitoring/helpers/source_tracker.rb', line 89
def decay_all
@records.each_value(&:decay)
@records.reject! { |_, r| r.faded? }
end
|
#reality_check(content_id:) ⇒ Object
63
64
65
66
67
68
69
|
# File 'lib/legion/extensions/agentic/memory/source_monitoring/helpers/source_tracker.rb', line 63
def reality_check(content_id:)
records = attribute(content_id: content_id)
return { status: :unknown, confidence: 0.0 } if records.empty?
best = records.first
{ status: best.reality_status, confidence: best.confidence, source: best.source, verified: best.verified }
end
|
#record_source(content_id:, source:, domain: :general, confidence: DEFAULT_CONFIDENCE, context: {}) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/legion/extensions/agentic/memory/source_monitoring/helpers/source_tracker.rb', line 22
def record_source(content_id:, source:, domain: :general, confidence: DEFAULT_CONFIDENCE, context: {})
return nil unless SOURCES.include?(source)
return nil if @records.size >= MAX_RECORDS
@record_counter += 1
rec_id = :"src_#{@record_counter}"
rec = SourceRecord.new(
id: rec_id, content_id: content_id, source: source,
domain: domain, confidence: confidence, context: context
)
@records[rec_id] = rec
rec
end
|
#records_by_source(source:) ⇒ Object
75
76
77
|
# File 'lib/legion/extensions/agentic/memory/source_monitoring/helpers/source_tracker.rb', line 75
def records_by_source(source:)
@records.values.select { |r| r.source == source }.map(&:to_h)
end
|
#records_in_domain(domain:) ⇒ Object
79
80
81
|
# File 'lib/legion/extensions/agentic/memory/source_monitoring/helpers/source_tracker.rb', line 79
def records_in_domain(domain:)
@records.values.select { |r| r.domain == domain }.map(&:to_h)
end
|
#to_h ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/legion/extensions/agentic/memory/source_monitoring/helpers/source_tracker.rb', line 94
def to_h
source_dist = SOURCES.to_h { |s| [s, @records.values.count { |r| r.source == s }] }
{
total_records: @records.size,
verified_count: @records.values.count(&:verified),
confused_count: @records.values.count(&:confused?),
accuracy: attribution_accuracy.round(4),
source_distribution: source_dist,
attribution_log_size: @attribution_log.size
}
end
|
#verify_source(record_id:) ⇒ Object
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/legion/extensions/agentic/memory/source_monitoring/helpers/source_tracker.rb', line 40
def verify_source(record_id:)
rec = @records[record_id]
return nil unless rec
rec.verify
@accuracy_hits += 1
@accuracy_total += 1
log_attribution(record_id, :verified)
rec
end
|