Module: Legion::Extensions::Agentic::Inference::Coherence::Runners::CognitiveCoherence

Includes:
Helpers::Lex
Included in:
Client
Defined in:
lib/legion/extensions/agentic/inference/coherence/runners/cognitive_coherence.rb

Instance Method Summary collapse

Instance Method Details

#add_coherence_constraint(prop_a_id:, prop_b_id:, constraint_type:, positive: true) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/legion/extensions/agentic/inference/coherence/runners/cognitive_coherence.rb', line 27

def add_coherence_constraint(prop_a_id:, prop_b_id:, constraint_type:, positive: true, **)
  unless Helpers::Constants::CONSTRAINT_TYPES.include?(constraint_type)
    return { success: false, reason: :invalid_constraint_type,
             valid_types: Helpers::Constants::CONSTRAINT_TYPES }
  end

  result = engine.add_constraint(
    prop_a_id:       prop_a_id,
    prop_b_id:       prop_b_id,
    constraint_type: constraint_type,
    positive:        positive
  )

  log.debug "[cognitive_coherence] add_constraint type=#{constraint_type} " \
            "positive=#{positive} success=#{result[:success]}"
  result
end

#add_coherence_proposition(content:, domain: :general, acceptance: Helpers::Constants::DEFAULT_ACCEPTANCE) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/legion/extensions/agentic/inference/coherence/runners/cognitive_coherence.rb', line 13

def add_coherence_proposition(content:, domain: :general, acceptance: Helpers::Constants::DEFAULT_ACCEPTANCE,
                              **)
  return { success: false, reason: :missing_content } if content.nil? || content.empty?

  prop_id = engine.add_proposition(content: content, domain: domain, acceptance: acceptance)
  if prop_id
    log.debug "[cognitive_coherence] add_proposition domain=#{domain} id=#{prop_id[0..7]}"
    { success: true, proposition_id: prop_id, domain: domain, acceptance: acceptance }
  else
    log.warn '[cognitive_coherence] add_proposition failed: max propositions reached'
    { success: false, reason: :max_propositions_reached }
  end
end

#cognitive_coherence_statsObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/legion/extensions/agentic/inference/coherence/runners/cognitive_coherence.rb', line 96

def cognitive_coherence_stats(**)
  part   = engine.partition
  counts = part.transform_values(&:size)
  log.debug "[cognitive_coherence] stats propositions=#{engine.propositions.size} " \
            "coherence=#{engine.overall_coherence.round(3)}"
  {
    success:             true,
    proposition_count:   engine.propositions.size,
    overall_coherence:   engine.overall_coherence,
    coherence_label:     engine.coherence_label,
    partition_counts:    counts,
    contradiction_count: engine.find_contradictions.size,
    history_size:        engine.history.size
  }
end

#coherence_partitionObject



73
74
75
76
77
78
79
# File 'lib/legion/extensions/agentic/inference/coherence/runners/cognitive_coherence.rb', line 73

def coherence_partition(**)
  result = engine.partition
  totals = result.transform_values(&:size)
  log.debug "[cognitive_coherence] partition accepted=#{totals[:accepted]} " \
            "rejected=#{totals[:rejected]} undecided=#{totals[:undecided]}"
  { success: true, partition: result, counts: totals }
end

#compute_proposition_coherence(proposition_id:) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/legion/extensions/agentic/inference/coherence/runners/cognitive_coherence.rb', line 45

def compute_proposition_coherence(proposition_id:, **)
  score = engine.compute_coherence(proposition_id: proposition_id)
  prop  = engine.propositions[proposition_id]

  unless prop
    log.debug "[cognitive_coherence] compute_coherence: #{proposition_id[0..7]} not found"
    return { success: false, reason: :not_found }
  end

  log.debug '[cognitive_coherence] compute_coherence ' \
            "id=#{proposition_id[0..7]} score=#{score.round(3)}"
  { success: true, proposition_id: proposition_id, coherence_score: score, state: prop.state }
end

#find_contradictionsObject



67
68
69
70
71
# File 'lib/legion/extensions/agentic/inference/coherence/runners/cognitive_coherence.rb', line 67

def find_contradictions(**)
  pairs = engine.find_contradictions
  log.debug "[cognitive_coherence] find_contradictions count=#{pairs.size}"
  { success: true, contradictions: pairs, count: pairs.size }
end

#maximize_coherenceObject



59
60
61
62
63
64
65
# File 'lib/legion/extensions/agentic/inference/coherence/runners/cognitive_coherence.rb', line 59

def maximize_coherence(**)
  result = engine.maximize_coherence
  overall = result[:overall_coherence]&.round(3)
  log.info '[cognitive_coherence] maximize_coherence ' \
           "overall=#{overall} props=#{result[:proposition_count]}"
  result
end

#update_cognitive_coherenceObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/legion/extensions/agentic/inference/coherence/runners/cognitive_coherence.rb', line 81

def update_cognitive_coherence(**)
  coherence_result = engine.maximize_coherence
  decay_result     = engine.decay_all

  overall = coherence_result[:overall_coherence]&.round(3)
  log.info "[cognitive_coherence] update overall=#{overall} " \
           "decayed=#{decay_result[:decayed_count]}"
  {
    success:           true,
    overall_coherence: coherence_result[:overall_coherence],
    coherence_label:   engine.coherence_label,
    decayed_count:     decay_result[:decayed_count]
  }
end