Class: Legion::Extensions::Agentic::Defense::Bias::Helpers::BiasStore
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Defense::Bias::Helpers::BiasStore
- Defined in:
- lib/legion/extensions/agentic/defense/bias/helpers/bias_store.rb
Instance Method Summary collapse
- #anchors_for(domain) ⇒ Object
- #by_domain(domain) ⇒ Object
- #by_type(bias_type) ⇒ Object
- #decay_anchors ⇒ Object
-
#initialize ⇒ BiasStore
constructor
A new instance of BiasStore.
- #recent(count = 10) ⇒ Object
- #record(event) ⇒ Object
- #register_anchor(domain, value:, influence: 1.0) ⇒ Object
- #stats ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize ⇒ BiasStore
Returns a new instance of BiasStore.
10 11 12 13 |
# File 'lib/legion/extensions/agentic/defense/bias/helpers/bias_store.rb', line 10 def initialize @events = [] @anchors = {} # domain -> array of { value:, influence:, recorded_at: } end |
Instance Method Details
#anchors_for(domain) ⇒ Object
39 40 41 |
# File 'lib/legion/extensions/agentic/defense/bias/helpers/bias_store.rb', line 39 def anchors_for(domain) @anchors.fetch(domain, []) end |
#by_domain(domain) ⇒ Object
29 30 31 |
# File 'lib/legion/extensions/agentic/defense/bias/helpers/bias_store.rb', line 29 def by_domain(domain) @events.select { |e| e.domain == domain }.map(&:to_h) end |
#by_type(bias_type) ⇒ Object
25 26 27 |
# File 'lib/legion/extensions/agentic/defense/bias/helpers/bias_store.rb', line 25 def by_type(bias_type) @events.select { |e| e.bias_type == bias_type }.map(&:to_h) end |
#decay_anchors ⇒ Object
43 44 45 46 47 48 |
# File 'lib/legion/extensions/agentic/defense/bias/helpers/bias_store.rb', line 43 def decay_anchors @anchors.each_value do |anchor_list| anchor_list.each { |a| a[:influence] = (a[:influence] - Constants::ANCHOR_DECAY).clamp(0.0, 1.0) } anchor_list.reject! { |a| a[:influence] <= 0.0 } end end |
#recent(count = 10) ⇒ Object
21 22 23 |
# File 'lib/legion/extensions/agentic/defense/bias/helpers/bias_store.rb', line 21 def recent(count = 10) @events.last(count).map(&:to_h) end |
#record(event) ⇒ Object
15 16 17 18 19 |
# File 'lib/legion/extensions/agentic/defense/bias/helpers/bias_store.rb', line 15 def record(event) @events << event @events.shift while @events.size > Constants::MAX_BIAS_EVENTS event end |
#register_anchor(domain, value:, influence: 1.0) ⇒ Object
33 34 35 36 37 |
# File 'lib/legion/extensions/agentic/defense/bias/helpers/bias_store.rb', line 33 def register_anchor(domain, value:, influence: 1.0) @anchors[domain] ||= [] @anchors[domain] << { value: value, influence: influence, recorded_at: Time.now.utc } @anchors[domain].shift while @anchors[domain].size > Constants::MAX_ANCHORS end |
#stats ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/legion/extensions/agentic/defense/bias/helpers/bias_store.rb', line 50 def stats return { total: 0, by_type: {}, by_domain: {} } if @events.empty? by_type = Constants::BIAS_TYPES.to_h do |bt| events = @events.select { |e| e.bias_type == bt } avg_mag = events.empty? ? 0.0 : events.sum(&:magnitude) / events.size [bt, { count: events.size, avg_magnitude: avg_mag.round(4) }] end domains = @events.map(&:domain).uniq by_domain = domains.to_h do |d| events = @events.select { |e| e.domain == d } [d, { count: events.size }] end { total: @events.size, by_type: by_type, by_domain: by_domain } end |
#to_h ⇒ Object
72 73 74 75 76 77 |
# File 'lib/legion/extensions/agentic/defense/bias/helpers/bias_store.rb', line 72 def to_h { total_events: @events.size, anchor_domains: @anchors.keys } end |