Class: Legion::Extensions::Agentic::Social::MirrorSystem::Helpers::MirrorSystem

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb

Constant Summary

Constants included from Constants

Constants::DEFAULT_FIDELITY, Constants::DEFAULT_RESONANCE, Constants::FAMILIARITY_BOOST, Constants::FIDELITY_LEARNING_RATE, Constants::MAX_IMITATIONS, Constants::MAX_MIRRORED_AGENTS, Constants::MAX_OBSERVATIONS, Constants::MAX_REPERTOIRE, Constants::MAX_RESONANCE, Constants::MIRROR_ALPHA, Constants::REPETITION_BOOST, Constants::RESONANCE_DECAY, Constants::RESONANCE_FLOOR, Constants::RESONANCE_LABELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMirrorSystem

Returns a new instance of MirrorSystem.



14
15
16
17
18
19
# File 'lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb', line 14

def initialize
  @observations     = {}
  @repertoire       = {}
  @fidelity_scores  = {}
  @imitation_history = []
end

Instance Attribute Details

#fidelity_scoresObject (readonly)

Returns the value of attribute fidelity_scores.



12
13
14
# File 'lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb', line 12

def fidelity_scores
  @fidelity_scores
end

#imitation_historyObject (readonly)

Returns the value of attribute imitation_history.



12
13
14
# File 'lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb', line 12

def imitation_history
  @imitation_history
end

#observationsObject (readonly)

Returns the value of attribute observations.



12
13
14
# File 'lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb', line 12

def observations
  @observations
end

#repertoireObject (readonly)

Returns the value of attribute repertoire.



12
13
14
# File 'lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb', line 12

def repertoire
  @repertoire
end

Instance Method Details

#add_to_repertoire(action, domain, fidelity = DEFAULT_FIDELITY) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb', line 55

def add_to_repertoire(action, domain, fidelity = DEFAULT_FIDELITY)
  key = repertoire_key(action, domain)
  if @repertoire.key?(key)
    old = @repertoire[key][:fidelity]
    @repertoire[key][:fidelity] = ema(old, fidelity, MIRROR_ALPHA)
    @repertoire[key][:practice_count] += 1
  else
    trim_repertoire if @repertoire.size >= MAX_REPERTOIRE
    @repertoire[key] = { action: action, domain: domain, fidelity: fidelity, practice_count: 1 }
  end
end

#decay_allObject



94
95
96
97
# File 'lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb', line 94

def decay_all
  @observations.each_value(&:decay)
  @observations.reject! { |_, o| o.faded? }
end

#fidelity_for(action, domain) ⇒ Object



74
75
76
# File 'lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb', line 74

def fidelity_for(action, domain)
  @fidelity_scores.fetch(repertoire_key(action, domain), DEFAULT_FIDELITY)
end

#imitate(action:, domain:, source_agent: nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb', line 40

def imitate(action:, domain:, source_agent: nil)
  candidates = if source_agent
                 observations_for_agent(source_agent).select { |o| o.action == action && o.domain == domain }
               else
                 @observations.values.select { |o| o.action == action && o.domain == domain }
               end
  return nil if candidates.empty?

  best = candidates.max_by(&:resonance)
  fidelity = fidelity_for(action, domain)
  add_to_repertoire(action, domain, fidelity)
  record_imitation(action, domain, source_agent, fidelity)
  { observation: best, fidelity: fidelity }
end

#mirrored_agentsObject



107
108
109
# File 'lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb', line 107

def mirrored_agents
  @observations.values.map(&:agent_id).uniq
end

#observation_countObject



99
100
101
# File 'lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb', line 99

def observation_count
  @observations.size
end

#observations_for_agent(agent_id) ⇒ Object



78
79
80
# File 'lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb', line 78

def observations_for_agent(agent_id)
  @observations.values.select { |o| o.agent_id == agent_id }
end

#observations_in_domain(domain) ⇒ Object



82
83
84
# File 'lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb', line 82

def observations_in_domain(domain)
  @observations.values.select { |o| o.domain == domain }
end

#observe(agent_id:, action:, domain:, context: nil, outcome: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb', line 21

def observe(agent_id:, action:, domain:, context: nil, outcome: nil)
  key = observation_key(agent_id, action, domain)
  if @observations.key?(key)
    @observations[key].observe_again
    @observations[key]
  else
    ensure_observation_capacity
    obs = ObservedBehavior.new(
      agent_id: agent_id,
      action:   action,
      domain:   domain,
      context:  context,
      outcome:  outcome
    )
    obs.boost_familiarity if repertoire_includes?(action, domain)
    @observations[key] = obs
  end
end

#repertoire_includes?(action, domain) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb', line 90

def repertoire_includes?(action, domain)
  @repertoire.key?(repertoire_key(action, domain))
end

#repertoire_sizeObject



103
104
105
# File 'lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb', line 103

def repertoire_size
  @repertoire.size
end

#strongest_mirrors(count = 5) ⇒ Object



86
87
88
# File 'lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb', line 86

def strongest_mirrors(count = 5)
  @observations.values.sort_by { |o| -o.resonance }.first(count)
end

#to_hObject



111
112
113
114
115
116
117
118
# File 'lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb', line 111

def to_h
  {
    observations:    observation_count,
    repertoire_size: repertoire_size,
    mirrored_agents: mirrored_agents.size,
    history_size:    @imitation_history.size
  }
end

#update_fidelity(action:, domain:, success:) ⇒ Object



67
68
69
70
71
72
# File 'lib/legion/extensions/agentic/social/mirror_system/helpers/mirror_system.rb', line 67

def update_fidelity(action:, domain:, success:)
  key = repertoire_key(action, domain)
  @fidelity_scores[key] ||= DEFAULT_FIDELITY
  delta = success ? FIDELITY_LEARNING_RATE : -FIDELITY_LEARNING_RATE
  @fidelity_scores[key] = (@fidelity_scores[key] + delta).clamp(0.0, 1.0)
end