Class: Legion::Extensions::Agentic::Inference::RealityTesting::Helpers::Belief

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb

Constant Summary

Constants included from Constants

Constants::CONFIDENCE_BOOST, Constants::CONFIDENCE_DECAY, Constants::CONFIDENCE_LABELS, Constants::CONFIDENCE_PENALTY, Constants::DEFAULT_CONFIDENCE, Constants::EVIDENCE_TYPES, Constants::MAX_BELIEFS, Constants::MAX_EVIDENCE, Constants::VALIDITY_LABELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, claim:, domain: :general, confidence: Constants::DEFAULT_CONFIDENCE) ⇒ Belief

Returns a new instance of Belief.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb', line 15

def initialize(id:, claim:, domain: :general, confidence: Constants::DEFAULT_CONFIDENCE)
  @id                  = id
  @claim               = claim
  @domain              = domain
  @confidence          = confidence.clamp(0.0, 1.0)
  @evidence_count      = 0
  @confirming_count    = 0
  @disconfirming_count = 0
  @created_at          = Time.now.utc
  @last_tested_at      = nil
end

Instance Attribute Details

#claimObject (readonly)

Returns the value of attribute claim.



12
13
14
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb', line 12

def claim
  @claim
end

#confidenceObject (readonly)

Returns the value of attribute confidence.



12
13
14
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb', line 12

def confidence
  @confidence
end

#confirming_countObject (readonly)

Returns the value of attribute confirming_count.



12
13
14
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb', line 12

def confirming_count
  @confirming_count
end

#created_atObject (readonly)

Returns the value of attribute created_at.



12
13
14
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb', line 12

def created_at
  @created_at
end

#disconfirming_countObject (readonly)

Returns the value of attribute disconfirming_count.



12
13
14
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb', line 12

def disconfirming_count
  @disconfirming_count
end

#domainObject (readonly)

Returns the value of attribute domain.



12
13
14
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb', line 12

def domain
  @domain
end

#evidence_countObject (readonly)

Returns the value of attribute evidence_count.



12
13
14
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb', line 12

def evidence_count
  @evidence_count
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb', line 12

def id
  @id
end

#last_tested_atObject (readonly)

Returns the value of attribute last_tested_at.



12
13
14
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb', line 12

def last_tested_at
  @last_tested_at
end

Instance Method Details

#confidence_labelObject



50
51
52
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb', line 50

def confidence_label
  Constants::CONFIDENCE_LABELS.find { |entry| entry[:range].cover?(@confidence) }&.fetch(:label) || :rejected
end

#decay!Object



70
71
72
73
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb', line 70

def decay!
  @confidence = (@confidence - Constants::CONFIDENCE_DECAY).clamp(0.0, 1.0)
  self
end

#needs_testing?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb', line 66

def needs_testing?
  @confidence.between?(0.3, 0.7)
end

#test_with_evidence!(evidence_type:, weight: 0.1) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb', line 27

def test_with_evidence!(evidence_type:, weight: 0.1)
  raise ArgumentError, "unknown evidence_type: #{evidence_type}" unless Constants::EVIDENCE_TYPES.include?(evidence_type)

  @evidence_count   += 1
  @last_tested_at    = Time.now.utc

  case evidence_type
  when :confirming
    @confirming_count += 1
    @confidence = (@confidence + (Constants::CONFIDENCE_BOOST * weight * 10)).clamp(0.0, 1.0)
  when :disconfirming
    @disconfirming_count += 1
    @confidence = (@confidence - (Constants::CONFIDENCE_PENALTY * weight * 10)).clamp(0.0, 1.0)
  when :neutral
    # no confidence change
  when :ambiguous
    delta = @confidence - 0.5
    @confidence = (@confidence - (delta * 0.1)).clamp(0.0, 1.0)
  end

  self
end

#to_hObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb', line 75

def to_h
  {
    id:                  @id,
    claim:               @claim,
    domain:              @domain,
    confidence:          @confidence,
    confidence_label:    confidence_label,
    evidence_count:      @evidence_count,
    confirming_count:    @confirming_count,
    disconfirming_count: @disconfirming_count,
    validity:            validity.round(3),
    validity_label:      validity_label,
    needs_testing:       needs_testing?,
    created_at:          @created_at,
    last_tested_at:      @last_tested_at
  }
end

#validityObject



54
55
56
57
58
59
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb', line 54

def validity
  total = @confirming_count + @disconfirming_count
  return 0.5 if total.zero?

  @confirming_count.to_f / total
end

#validity_labelObject



61
62
63
64
# File 'lib/legion/extensions/agentic/inference/reality_testing/helpers/belief.rb', line 61

def validity_label
  v = validity
  Constants::VALIDITY_LABELS.find { |entry| entry[:range].cover?(v) }&.fetch(:label) || :refuted
end