Class: Legion::Extensions::Agentic::Memory::CommunicationPattern::Helpers::PatternAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent_id:) ⇒ PatternAnalyzer

Returns a new instance of PatternAnalyzer.



12
13
14
15
16
17
18
19
20
21
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 12

def initialize(agent_id:)
  @agent_id      = agent_id
  @tod_histogram = Array.new(Constants::HOURS_IN_DAY, 0)
  @dow_histogram = Array.new(Constants::DAYS_IN_WEEK, 0)
  @channel_counts    = Hash.new(0)
  @direct_count      = 0
  @topic_counts      = Hash.new(0)
  @trace_count       = 0
  @dirty             = false
end

Instance Attribute Details

#agent_idObject (readonly)

Returns the value of attribute agent_id.



10
11
12
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 10

def agent_id
  @agent_id
end

#trace_countObject (readonly)

Returns the value of attribute trace_count.



10
11
12
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 10

def trace_count
  @trace_count
end

Instance Method Details

#channel_preferenceObject



36
37
38
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 36

def channel_preference
  @channel_counts.sort_by { |_k, v| -v }.map(&:first)
end

#consistencyObject



50
51
52
53
54
55
56
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 50

def consistency
  return 0.0 if @trace_count < Constants::MIN_TRACES_FOR_PATTERN

  channel_entropy = normalized_entropy(@channel_counts.values)
  tod_entropy     = normalized_entropy(@tod_histogram)
  (1.0 - ((channel_entropy + tod_entropy) / 2.0)).clamp(0.0, 1.0)
end

#day_of_week_distributionObject



32
33
34
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 32

def day_of_week_distribution
  @dow_histogram.dup
end

#direct_address_frequencyObject



40
41
42
43
44
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 40

def direct_address_frequency
  return 0.0 if @trace_count.zero?

  @direct_count.to_f / @trace_count
end

#dirty?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 58

def dirty?
  @dirty
end

#from_apollo(store:) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 72

def from_apollo(store:)
  result = store.query(text: 'communication_pattern',
                       tags: Constants::TAG_PREFIX + [@agent_id])
  return false unless result[:success] && result[:results]&.any?

  parsed = deserialize(result[:results].first[:content])
  return false unless parsed

  restore_state(parsed)
  true
rescue StandardError => e
  warn "[pattern_analyzer] from_apollo error: #{e.message}"
  false
end

#mark_clean!Object



62
63
64
65
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 62

def mark_clean!
  @dirty = false
  self
end

#time_of_day_distributionObject



28
29
30
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 28

def time_of_day_distribution
  @tod_histogram.dup
end

#to_apollo_entriesObject



67
68
69
70
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 67

def to_apollo_entries
  tags = Constants::TAG_PREFIX.dup + [@agent_id]
  [{ content: serialize(state_hash), tags: tags }]
end

#topic_clusteringObject



46
47
48
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 46

def topic_clustering
  @topic_counts.dup
end

#update_from_traces(traces) ⇒ Object



23
24
25
26
# File 'lib/legion/extensions/agentic/memory/communication_pattern/helpers/pattern_analyzer.rb', line 23

def update_from_traces(traces)
  traces.each { |t| process_trace(t) }
  @dirty = true
end