Class: Legion::Extensions::Agentic::Integration::Synthesis::Helpers::SynthesisEngine
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Integration::Synthesis::Helpers::SynthesisEngine
show all
- Includes:
- Constants, Logging::Helper
- Defined in:
- lib/legion/extensions/agentic/integration/synthesis/helpers/synthesis_engine.rb
Constant Summary
Constants included
from Constants
Constants::COHERENCE_LABELS, Constants::COHERENCE_THRESHOLD, Constants::CONFIDENCE_LABELS, Constants::DEFAULT_WEIGHT, Constants::FRESHNESS_DECAY, Constants::MAX_STREAMS, Constants::MAX_SYNTHESES, Constants::MIN_STREAMS_FOR_SYNTHESIS, Constants::NOVELTY_THRESHOLD, Constants::STREAM_TYPES
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of SynthesisEngine.
15
16
17
18
|
# File 'lib/legion/extensions/agentic/integration/synthesis/helpers/synthesis_engine.rb', line 15
def initialize
@streams = {}
@syntheses = []
end
|
Instance Attribute Details
#streams ⇒ Object
Returns the value of attribute streams.
13
14
15
|
# File 'lib/legion/extensions/agentic/integration/synthesis/helpers/synthesis_engine.rb', line 13
def streams
@streams
end
|
#syntheses ⇒ Object
Returns the value of attribute syntheses.
13
14
15
|
# File 'lib/legion/extensions/agentic/integration/synthesis/helpers/synthesis_engine.rb', line 13
def syntheses
@syntheses
end
|
Instance Method Details
#add_stream(stream_type:, content:, weight: DEFAULT_WEIGHT, confidence: DEFAULT_WEIGHT) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/legion/extensions/agentic/integration/synthesis/helpers/synthesis_engine.rb', line 20
def add_stream(stream_type:, content:, weight: DEFAULT_WEIGHT, confidence: DEFAULT_WEIGHT, **)
return { success: false, error: :invalid_stream_type, valid_types: STREAM_TYPES } unless STREAM_TYPES.include?(stream_type)
stream = SynthesisStream.new(
stream_type: stream_type,
content: content,
weight: weight,
confidence: confidence
)
@streams[stream.id] = stream
prune_streams! if @streams.size > MAX_STREAMS
log.debug("[cognitive_synthesis] stream added id=#{stream.id[0..7]} " \
"type=#{stream_type} weight=#{weight.round(2)}")
{ success: true, stream_id: stream.id, stream_type: stream_type }
end
|
#average_coherence(window: 10) ⇒ Object
125
126
127
128
129
130
131
|
# File 'lib/legion/extensions/agentic/integration/synthesis/helpers/synthesis_engine.rb', line 125
def average_coherence(window: 10, **)
recent = @syntheses.last(window)
return { success: true, average_coherence: 0.0, sample_size: 0 } if recent.empty?
avg = recent.sum(&:coherence).round(10) / recent.size
{ success: true, average_coherence: avg.round(10), sample_size: recent.size }
end
|
#decay_all! ⇒ Object
80
81
82
83
84
85
86
87
88
|
# File 'lib/legion/extensions/agentic/integration/synthesis/helpers/synthesis_engine.rb', line 80
def decay_all!(**)
before = @streams.size
@streams.each_value(&:decay_freshness!)
@streams.reject! { |_, s| s.stale? }
removed = before - @streams.size
log.debug("[cognitive_synthesis] decay_all! removed=#{removed} remaining=#{@streams.size}")
{ success: true, streams_removed: removed, streams_remaining: @streams.size }
end
|
#dominant_stream ⇒ Object
111
112
113
114
115
116
117
118
|
# File 'lib/legion/extensions/agentic/integration/synthesis/helpers/synthesis_engine.rb', line 111
def dominant_stream(**)
return { success: false, error: :no_streams } if @streams.empty?
stream = @streams.values.max_by(&:effective_weight)
log.debug("[cognitive_synthesis] dominant stream id=#{stream.id[0..7]} " \
"effective_weight=#{stream.effective_weight.round(4)}")
{ success: true, stream: stream.to_h }
end
|
#remove_stream(stream_id:) ⇒ Object
39
40
41
42
43
44
45
46
47
|
# File 'lib/legion/extensions/agentic/integration/synthesis/helpers/synthesis_engine.rb', line 39
def remove_stream(stream_id:, **)
removed = @streams.delete(stream_id)
if removed
log.debug("[cognitive_synthesis] stream removed id=#{stream_id[0..7]}")
{ success: true, stream_id: stream_id }
else
{ success: false, error: :not_found }
end
end
|
#stream_conflict?(stream_id_a:, stream_id_b:) ⇒ Boolean
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/legion/extensions/agentic/integration/synthesis/helpers/synthesis_engine.rb', line 90
def stream_conflict?(stream_id_a:, stream_id_b:, **)
a = @streams[stream_id_a]
b = @streams[stream_id_b]
return { success: false, error: :stream_not_found } unless a && b
weight_opposition = opposing_weights?(a, b)
content_conflict = conflicting_content?(a, b)
conflict = weight_opposition || content_conflict
log.debug("[cognitive_synthesis] conflict check #{stream_id_a[0..7]}<>#{stream_id_b[0..7]} " \
"result=#{conflict}")
{
success: true,
conflict: conflict,
weight_opposition: weight_opposition,
content_conflict: content_conflict
}
end
|
#synthesis_history(limit: 10) ⇒ Object
120
121
122
123
|
# File 'lib/legion/extensions/agentic/integration/synthesis/helpers/synthesis_engine.rb', line 120
def synthesis_history(limit: 10, **)
recent = @syntheses.last(limit)
{ success: true, syntheses: recent.map(&:to_h), count: recent.size }
end
|
#synthesize! ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/legion/extensions/agentic/integration/synthesis/helpers/synthesis_engine.rb', line 49
def synthesize!(**)
active = @streams.values.reject(&:stale?)
if active.size < MIN_STREAMS_FOR_SYNTHESIS
log.debug("[cognitive_synthesis] synthesize! skipped: only #{active.size} active streams")
return { success: false, error: :insufficient_streams, active_count: active.size, required: MIN_STREAMS_FOR_SYNTHESIS }
end
coherence = compute_coherence(active)
novelty = compute_novelty(active)
confidence = compute_weighted_confidence(active)
content = merge_content(active)
synthesis = Synthesis.new(
streams: active.map(&:id),
coherence: coherence,
novelty: novelty,
confidence: confidence,
content: content
)
@syntheses << synthesis
@syntheses.shift while @syntheses.size > MAX_SYNTHESES
log.info("[cognitive_synthesis] synthesis id=#{synthesis.id[0..7]} " \
"coherence=#{coherence.round(2)} novelty=#{novelty.round(2)} " \
"streams=#{active.size} label=#{synthesis.coherence_label}")
{ success: true, synthesis: synthesis.to_h }
end
|
#to_h ⇒ Object
133
134
135
136
137
138
139
140
141
|
# File 'lib/legion/extensions/agentic/integration/synthesis/helpers/synthesis_engine.rb', line 133
def to_h
{
stream_count: @streams.size,
synthesis_count: @syntheses.size,
active_streams: @streams.values.count { |element| !element.stale? },
stale_streams: @streams.values.count(&:stale?),
average_coherence: @syntheses.empty? ? 0.0 : (@syntheses.sum(&:coherence).round(10) / @syntheses.size).round(6)
}
end
|