Class: Legion::Extensions::Agentic::Inference::RealityTesting::Helpers::RealityEngine
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Inference::RealityTesting::Helpers::RealityEngine
- Includes:
- Logging::Helper
- Defined in:
- lib/legion/extensions/agentic/inference/reality_testing/helpers/reality_engine.rb
Instance Method Summary collapse
- #beliefs_by_domain(domain:) ⇒ Object
- #beliefs_needing_testing ⇒ Object
- #create_belief(claim:, domain: :general, confidence: Constants::DEFAULT_CONFIDENCE) ⇒ Object
- #decay_all ⇒ Object
-
#initialize ⇒ RealityEngine
constructor
A new instance of RealityEngine.
- #overall_reality_coherence ⇒ Object
- #prune_rejected ⇒ Object
- #reality_report ⇒ Object
- #size ⇒ Object
- #strongest_beliefs(limit: 10) ⇒ Object
- #test_belief(belief_id:, evidence_type:, weight: 0.1) ⇒ Object
- #to_h ⇒ Object
- #weakest_beliefs(limit: 10) ⇒ Object
Constructor Details
#initialize ⇒ RealityEngine
Returns a new instance of RealityEngine.
12 13 14 15 |
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/reality_engine.rb', line 12 def initialize @beliefs = {} @next_id = 1 end |
Instance Method Details
#beliefs_by_domain(domain:) ⇒ Object
53 54 55 |
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/reality_engine.rb', line 53 def beliefs_by_domain(domain:) @beliefs.values.select { |b| b.domain == domain } end |
#beliefs_needing_testing ⇒ Object
37 38 39 |
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/reality_engine.rb', line 37 def beliefs_needing_testing @beliefs.values.select(&:needs_testing?) end |
#create_belief(claim:, domain: :general, confidence: Constants::DEFAULT_CONFIDENCE) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/reality_engine.rb', line 17 def create_belief(claim:, domain: :general, confidence: Constants::DEFAULT_CONFIDENCE) return { created: false, reason: :at_capacity } if @beliefs.size >= Constants::MAX_BELIEFS id = "belief_#{@next_id}" @next_id += 1 belief = Belief.new(id: id, claim: claim, domain: domain, confidence: confidence) @beliefs[id] = belief log.debug "[reality_testing] create_belief id=#{id} domain=#{domain} confidence=#{confidence.round(2)}" { created: true, belief: belief.to_h } end |
#decay_all ⇒ Object
64 65 66 67 |
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/reality_engine.rb', line 64 def decay_all @beliefs.each_value(&:decay!) @beliefs.size end |
#overall_reality_coherence ⇒ Object
57 58 59 60 61 62 |
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/reality_engine.rb', line 57 def overall_reality_coherence return 0.0 if @beliefs.empty? total = @beliefs.values.sum(&:validity) total / @beliefs.size end |
#prune_rejected ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/reality_engine.rb', line 69 def prune_rejected before = @beliefs.size @beliefs.delete_if { |_id, b| b.confidence < 0.1 } pruned = before - @beliefs.size log.debug "[reality_testing] prune_rejected pruned=#{pruned} remaining=#{@beliefs.size}" pruned end |
#reality_report ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/reality_engine.rb', line 77 def reality_report domains = @beliefs.values.group_by(&:domain) { total_beliefs: @beliefs.size, coherence: overall_reality_coherence.round(3), needing_testing: beliefs_needing_testing.size, domains: domains.transform_values(&:size), strongest_claim: @beliefs.values.max_by(&:confidence)&.claim, weakest_claim: @beliefs.values.min_by(&:confidence)&.claim } end |
#size ⇒ Object
97 98 99 |
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/reality_engine.rb', line 97 def size @beliefs.size end |
#strongest_beliefs(limit: 10) ⇒ Object
41 42 43 44 45 |
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/reality_engine.rb', line 41 def strongest_beliefs(limit: 10) @beliefs.values .sort_by { |b| -b.confidence } .first(limit) end |
#test_belief(belief_id:, evidence_type:, weight: 0.1) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/reality_engine.rb', line 28 def test_belief(belief_id:, evidence_type:, weight: 0.1) belief = @beliefs[belief_id] return { tested: false, reason: :not_found } unless belief belief.test_with_evidence!(evidence_type: evidence_type, weight: weight) log.debug "[reality_testing] test_belief id=#{belief_id} evidence=#{evidence_type} confidence=#{belief.confidence.round(2)}" { tested: true, belief: belief.to_h } end |
#to_h ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/reality_engine.rb', line 89 def to_h { belief_count: @beliefs.size, coherence: overall_reality_coherence.round(3), beliefs: @beliefs.values.map(&:to_h) } end |
#weakest_beliefs(limit: 10) ⇒ Object
47 48 49 50 51 |
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/reality_engine.rb', line 47 def weakest_beliefs(limit: 10) @beliefs.values .sort_by(&:confidence) .first(limit) end |