Class: Legion::Extensions::Agentic::Defense::Immunology::Helpers::ImmuneEngine

Inherits:
Object
  • Object
show all
Includes:
Helpers::Lex
Defined in:
lib/legion/extensions/agentic/defense/immunology/helpers/immune_engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeImmuneEngine

Returns a new instance of ImmuneEngine.



14
15
16
17
18
19
# File 'lib/legion/extensions/agentic/defense/immunology/helpers/immune_engine.rb', line 14

def initialize
  @threats      = {}
  @antibodies   = {}
  @resistance   = Constants::DEFAULT_RESISTANCE
  @inflammatory = false
end

Instance Attribute Details

#inflammatoryObject (readonly)

Returns the value of attribute inflammatory.



12
13
14
# File 'lib/legion/extensions/agentic/defense/immunology/helpers/immune_engine.rb', line 12

def inflammatory
  @inflammatory
end

#resistanceObject (readonly)

Returns the value of attribute resistance.



12
13
14
# File 'lib/legion/extensions/agentic/defense/immunology/helpers/immune_engine.rb', line 12

def resistance
  @resistance
end

Instance Method Details

#create_antibody(tactic:, pattern:, strength: 0.5) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/legion/extensions/agentic/defense/immunology/helpers/immune_engine.rb', line 74

def create_antibody(tactic:, pattern:, strength: 0.5)
  return nil unless Constants::MANIPULATION_TACTICS.include?(tactic.to_sym)

  prune_antibodies_if_full

  ab = Antibody.new(tactic: tactic, pattern: pattern, strength: strength)
  @antibodies[ab.id] = ab
  log.info("[cognitive_immunology] antibody created: id=#{ab.id} tactic=#{tactic} strength=#{strength}")
  ab
end

#decay_allObject



129
130
131
132
133
134
# File 'lib/legion/extensions/agentic/defense/immunology/helpers/immune_engine.rb', line 129

def decay_all
  @antibodies.each_value(&:decay!)
  @resistance = (@resistance - Constants::RESISTANCE_DECAY).clamp(0.0, 1.0).round(10)
  log.debug("[cognitive_immunology] decay cycle: resistance=#{@resistance.round(2)} antibodies=#{@antibodies.size}")
  { resistance: @resistance, antibodies_decayed: @antibodies.size }
end

#detect_threat(source:, tactic:, content_hash:, threat_level: 0.5) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/legion/extensions/agentic/defense/immunology/helpers/immune_engine.rb', line 21

def detect_threat(source:, tactic:, content_hash:, threat_level: 0.5)
  return nil unless Constants::MANIPULATION_TACTICS.include?(tactic.to_sym)

  prune_threats_if_full

  threat = Threat.new(
    source:       source,
    tactic:       tactic,
    content_hash: content_hash,
    threat_level: threat_level
  )

  matched = match_antibodies_for_tactic(tactic)
  matched.each do |ab|
    ab.match!
    reduction = (ab.strength * 0.2).round(10)
    threat.threat_level = (threat.threat_level - reduction).clamp(0.0, 1.0).round(10)
  end

  @threats[threat.id] = threat
  log.debug("[cognitive_immunology] threat detected: id=#{threat.id} tactic=#{tactic} level=#{threat.threat_level.round(2)}")
  threat
end

#immunity_labelObject



107
108
109
110
# File 'lib/legion/extensions/agentic/defense/immunology/helpers/immune_engine.rb', line 107

def immunity_label
  score = overall_immunity
  Constants::IMMUNITY_LABELS.find { |range, _| range.cover?(score) }&.last || :compromised
end

#inoculate(threat_id:) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/legion/extensions/agentic/defense/immunology/helpers/immune_engine.rb', line 63

def inoculate(threat_id:)
  threat = @threats.fetch(threat_id, nil)
  return { success: false, reason: 'not found' } unless threat

  threat.expose!
  boost = (Constants::RESISTANCE_BOOST / (threat.exposure_count + 1)).round(10)
  @resistance = (@resistance + boost).clamp(0.0, 1.0).round(10)
  log.debug("[cognitive_immunology] inoculate: id=#{threat_id} exposure=#{threat.exposure_count} resistance=#{@resistance.round(2)}")
  { success: true, threat_id: threat_id, exposure_count: threat.exposure_count, resistance: @resistance }
end

#overall_immunityObject



101
102
103
104
105
# File 'lib/legion/extensions/agentic/defense/immunology/helpers/immune_engine.rb', line 101

def overall_immunity
  ab_coverage = antibody_coverage_score
  score = ((@resistance * 0.6) + (ab_coverage * 0.4)).round(10)
  score.clamp(0.0, 1.0)
end

#prune_ineffectiveObject



136
137
138
139
140
141
142
# File 'lib/legion/extensions/agentic/defense/immunology/helpers/immune_engine.rb', line 136

def prune_ineffective
  before = @antibodies.size
  @antibodies.select! { |_, ab| ab.effective? }
  pruned = before - @antibodies.size
  log.debug("[cognitive_immunology] pruned #{pruned} ineffective antibodies")
  { pruned: pruned, remaining: @antibodies.size }
end

#quarantine_threat(threat_id:) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/legion/extensions/agentic/defense/immunology/helpers/immune_engine.rb', line 45

def quarantine_threat(threat_id:)
  threat = @threats.fetch(threat_id, nil)
  return { success: false, reason: 'not found' } unless threat

  threat.quarantine!
  log.info("[cognitive_immunology] quarantined: id=#{threat_id} tactic=#{threat.tactic}")
  { success: true, threat_id: threat_id }
end

#release_threat(threat_id:) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/legion/extensions/agentic/defense/immunology/helpers/immune_engine.rb', line 54

def release_threat(threat_id:)
  threat = @threats.fetch(threat_id, nil)
  return { success: false, reason: 'not found' } unless threat

  threat.release!
  log.debug("[cognitive_immunology] released: id=#{threat_id}")
  { success: true, threat_id: threat_id }
end

#resolve_inflammationObject



95
96
97
98
99
# File 'lib/legion/extensions/agentic/defense/immunology/helpers/immune_engine.rb', line 95

def resolve_inflammation
  @inflammatory = false
  log.info('[cognitive_immunology] inflammation resolved — returning to normal scrutiny')
  { inflammatory: false }
end

#scan_for_tactic(tactic:) ⇒ Object



85
86
87
# File 'lib/legion/extensions/agentic/defense/immunology/helpers/immune_engine.rb', line 85

def scan_for_tactic(tactic:)
  @threats.values.select { |t| t.tactic == tactic }
end

#threat_history(limit: 10) ⇒ Object



122
123
124
125
126
127
# File 'lib/legion/extensions/agentic/defense/immunology/helpers/immune_engine.rb', line 122

def threat_history(limit: 10)
  @threats.values
          .sort_by(&:created_at)
          .last(limit)
          .map(&:to_h)
end

#to_hObject



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/legion/extensions/agentic/defense/immunology/helpers/immune_engine.rb', line 144

def to_h
  {
    threat_count:             @threats.size,
    quarantined_count:        @threats.values.count(&:quarantined),
    antibody_count:           @antibodies.size,
    effective_antibody_count: @antibodies.values.count(&:effective?),
    resistance:               @resistance,
    inflammatory:             @inflammatory,
    overall_immunity:         overall_immunity,
    immunity_label:           immunity_label
  }
end

#trigger_inflammatory_responseObject



89
90
91
92
93
# File 'lib/legion/extensions/agentic/defense/immunology/helpers/immune_engine.rb', line 89

def trigger_inflammatory_response
  @inflammatory = true
  log.warn('[cognitive_immunology] inflammatory response triggered — heightened scrutiny mode')
  { inflammatory: true }
end

#vulnerability_reportObject



112
113
114
115
116
117
118
119
120
# File 'lib/legion/extensions/agentic/defense/immunology/helpers/immune_engine.rb', line 112

def vulnerability_report
  covered_tactics = @antibodies.values.map(&:tactic).uniq
  uncovered = Constants::MANIPULATION_TACTICS.reject { |t| covered_tactics.include?(t) }
  {
    covered:   covered_tactics,
    uncovered: uncovered,
    coverage:  (covered_tactics.size.to_f / Constants::MANIPULATION_TACTICS.size).round(10)
  }
end