Class: Legion::Extensions::Agentic::Memory::ImmuneMemory::Helpers::ImmuneMemoryEngine

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb

Constant Summary

Constants included from Constants

Constants::B_CELL_ACTIVATION_THRESHOLD, Constants::B_CELL_ANTIBODY_PRODUCTION, Constants::B_CELL_BOOST, Constants::B_CELL_DECAY, Constants::CELL_TYPES, Constants::HEALTH_LABELS, Constants::IMMUNITY_LABELS, Constants::MATURITY_LABELS, Constants::MAX_ANTIBODY_LIBRARY, Constants::MAX_ENCOUNTERS, Constants::MAX_MEMORY_CELLS, Constants::MEMORY_RECOGNITION_THRESHOLD, Constants::PRIMARY_RESPONSE_SPEED, Constants::RESPONSE_SPEED_LABELS, Constants::SECONDARY_RESPONSE_SPEED, Constants::THREAT_TYPES, Constants::T_CELL_ACTIVATION_THRESHOLD, Constants::T_CELL_BOOST, Constants::T_CELL_DECAY, Constants::T_CELL_LIFESPAN, Constants::VACCINATION_STRENGTH

Instance Method Summary collapse

Methods included from Constants

label_for

Constructor Details

#initializeImmuneMemoryEngine

Returns a new instance of ImmuneMemoryEngine.



12
13
14
15
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 12

def initialize
  @memory_cells = {}
  @encounters = []
end

Instance Method Details

#active_cellsObject



78
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 78

def active_cells = @memory_cells.values.reject(&:expired?)

#average_response_speedObject



102
103
104
105
106
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 102

def average_response_speed
  return PRIMARY_RESPONSE_SPEED if @encounters.empty?

  (@encounters.sum(&:response_speed) / @encounters.size).round(10)
end

#b_cellsObject



80
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 80

def b_cells = @memory_cells.values.select(&:b_cell?)

#cells_for_threat(threat_type:) ⇒ Object



67
68
69
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 67

def cells_for_threat(threat_type:)
  @memory_cells.values.select { |c| c.threat_type == threat_type.to_sym && !c.expired? }
end

#create_memory_cell(threat_type:, signature:, cell_type: :b_memory, strength: VACCINATION_STRENGTH) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 17

def create_memory_cell(threat_type:, signature:, cell_type: :b_memory, strength: VACCINATION_STRENGTH)
  prune_expired
  cell = MemoryCell.new(threat_type: threat_type, signature: signature,
                        cell_type: cell_type, strength: strength)
  @memory_cells[cell.id] = cell
  cell
end

#decay_all!Object



57
58
59
60
61
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 57

def decay_all!
  @memory_cells.each_value(&:decay!)
  prune_expired
  { cells_remaining: @memory_cells.size }
end

#encounter_threat(threat_type:, threat_signature:, severity: 0.5) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 33

def encounter_threat(threat_type:, threat_signature:, severity: 0.5)
  matching = find_by_signature(threat_signature)
  response_type = matching ? :secondary : :primary
  speed = matching ? matching.response_speed : PRIMARY_RESPONSE_SPEED
  outcome = determine_outcome(matching, severity)

  matching&.activate!

  record = Encounter.new(
    threat_type: threat_type, threat_signature: threat_signature,
    severity: severity, response_type: response_type,
    response_speed: speed, outcome: outcome
  )
  @encounters << record
  prune_encounters

  unless matching
    create_memory_cell(threat_type: threat_type, signature: threat_signature,
                       cell_type: :b_memory, strength: B_CELL_ACTIVATION_THRESHOLD)
  end

  record
end

#encounters_for(threat_type:) ⇒ Object



84
85
86
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 84

def encounters_for(threat_type:)
  @encounters.select { |e| e.threat_type == threat_type.to_sym }
end

#find_by_signature(signature) ⇒ Object



63
64
65
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 63

def find_by_signature(signature)
  @memory_cells.values.find { |c| c.signature == signature.to_s && !c.expired? }
end

#health_labelObject



121
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 121

def health_label = Constants.label_for(HEALTH_LABELS, overall_health)

#immune_reportObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 123

def immune_report
  {
    total_cells:             @memory_cells.size,
    active_cells:            active_cells.size,
    t_cells:                 t_cells.size,
    b_cells:                 b_cells.size,
    veteran_cells:           veteran_cells.size,
    total_encounters:        @encounters.size,
    secondary_response_rate: secondary_response_rate,
    neutralization_rate:     neutralization_rate,
    average_response_speed:  average_response_speed,
    threat_coverage:         threat_coverage,
    overall_health:          overall_health,
    health_label:            health_label
  }
end

#immunity_for(threat_type:) ⇒ Object



71
72
73
74
75
76
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 71

def immunity_for(threat_type:)
  cells = cells_for_threat(threat_type: threat_type)
  return 0.0 if cells.empty?

  cells.max_by(&:strength).strength
end

#naive_cellsObject



82
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 82

def naive_cells = @memory_cells.values.select(&:naive?)

#neutralization_rateObject



95
96
97
98
99
100
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 95

def neutralization_rate
  return 0.0 if @encounters.empty?

  neutralized = @encounters.count(&:neutralized?)
  (neutralized.to_f / @encounters.size).round(10)
end

#overall_healthObject



113
114
115
116
117
118
119
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 113

def overall_health
  return 0.0 if @memory_cells.empty?

  avg_strength = (@memory_cells.values.sum(&:strength) / @memory_cells.size).round(10)
  coverage_factor = threat_coverage
  ((avg_strength * 0.6) + (coverage_factor * 0.4)).clamp(0.0, 1.0).round(10)
end

#secondary_response_rateObject



88
89
90
91
92
93
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 88

def secondary_response_rate
  return 0.0 if @encounters.empty?

  secondary = @encounters.count(&:secondary?)
  (secondary.to_f / @encounters.size).round(10)
end

#t_cellsObject



79
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 79

def t_cells = @memory_cells.values.select(&:t_cell?)

#threat_coverageObject



108
109
110
111
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 108

def threat_coverage
  known_types = @memory_cells.values.map(&:threat_type).uniq
  (known_types.size.to_f / THREAT_TYPES.size).clamp(0.0, 1.0).round(10)
end

#to_hObject



140
141
142
143
144
145
146
147
148
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 140

def to_h
  {
    total_cells:     @memory_cells.size,
    active:          active_cells.size,
    encounters:      @encounters.size,
    health:          overall_health,
    threat_coverage: threat_coverage
  }
end

#vaccinate(threat_type:, signature:, strength: VACCINATION_STRENGTH) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 25

def vaccinate(threat_type:, signature:, strength: VACCINATION_STRENGTH)
  existing = find_by_signature(signature)
  return existing.activate! if existing

  create_memory_cell(threat_type: threat_type, signature: signature,
                     cell_type: :b_memory, strength: strength)
end

#veteran_cellsObject



81
# File 'lib/legion/extensions/agentic/memory/immune_memory/helpers/immune_memory_engine.rb', line 81

def veteran_cells = @memory_cells.values.select(&:veteran?)