Class: Legion::Extensions::Agentic::Self::Anchor::Helpers::AnchorEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/self/anchor/helpers/anchor_engine.rb

Instance Method Summary collapse

Constructor Details

#initializeAnchorEngine

Returns a new instance of AnchorEngine.



10
11
12
13
# File 'lib/legion/extensions/agentic/self/anchor/helpers/anchor_engine.rb', line 10

def initialize
  @anchors = {}
  @chains  = {}
end

Instance Method Details

#all_anchorsObject



104
105
106
# File 'lib/legion/extensions/agentic/self/anchor/helpers/anchor_engine.rb', line 104

def all_anchors
  @anchors.values
end

#all_chainsObject



108
109
110
# File 'lib/legion/extensions/agentic/self/anchor/helpers/anchor_engine.rb', line 108

def all_chains
  @chains.values
end

#anchor_reportObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/legion/extensions/agentic/self/anchor/helpers/anchor_engine.rb', line 92

def anchor_report
  {
    total_anchors:  @anchors.size,
    total_chains:   @chains.size,
    by_type:        anchors_by_type,
    ironclad_count: ironclad_anchors.size,
    drifting_count: drifting_anchors.size,
    broken_chains:  broken_chains.size,
    avg_grip:       avg_grip
  }
end

#anchors_by_typeObject



60
61
62
63
64
# File 'lib/legion/extensions/agentic/self/anchor/helpers/anchor_engine.rb', line 60

def anchors_by_type
  counts = Constants::ANCHOR_TYPES.to_h { |t| [t, 0] }
  @anchors.each_value { |a| counts[a.anchor_type] += 1 }
  counts
end

#apply_bias(anchor_id:, new_value:) ⇒ Object



35
36
37
38
39
40
# File 'lib/legion/extensions/agentic/self/anchor/helpers/anchor_engine.rb', line 35

def apply_bias(anchor_id:, new_value:)
  anchor = fetch_anchor(anchor_id)
  pulled = anchor.bias_pull(new_value.to_f)
  { anchor: anchor, original: new_value.to_f, biased: pulled,
    shift: (pulled - new_value.to_f).abs.round(10) }
end

#avg_gripObject



86
87
88
89
90
# File 'lib/legion/extensions/agentic/self/anchor/helpers/anchor_engine.rb', line 86

def avg_grip
  return 0.0 if @anchors.empty?

  (@anchors.values.sum(&:grip) / @anchors.size).round(10)
end

#broken_chainsObject



56
57
58
# File 'lib/legion/extensions/agentic/self/anchor/helpers/anchor_engine.rb', line 56

def broken_chains
  @chains.values.select(&:broken?)
end

#chains_for(anchor_id) ⇒ Object



82
83
84
# File 'lib/legion/extensions/agentic/self/anchor/helpers/anchor_engine.rb', line 82

def chains_for(anchor_id)
  @chains.values.select { |c| c.anchor_id == anchor_id }
end

#create_anchor(anchor_type:, domain:, content:, reference_value: nil, grip: nil, weight: nil) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
# File 'lib/legion/extensions/agentic/self/anchor/helpers/anchor_engine.rb', line 15

def create_anchor(anchor_type:, domain:, content:,
                  reference_value: nil, grip: nil, weight: nil)
  raise ArgumentError, 'anchor limit reached' if @anchors.size >= Constants::MAX_ANCHORS

  a = Anchor.new(anchor_type: anchor_type, domain: domain, content: content,
                 reference_value: reference_value, grip: grip, weight: weight)
  @anchors[a.id] = a
  a
end

#create_chain(anchor_id:, material: :steel, length: nil, flexibility: nil) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
# File 'lib/legion/extensions/agentic/self/anchor/helpers/anchor_engine.rb', line 25

def create_chain(anchor_id:, material: :steel, length: nil, flexibility: nil)
  raise ArgumentError, 'chain limit reached' if @chains.size >= Constants::MAX_CHAINS

  fetch_anchor(anchor_id)
  c = Chain.new(anchor_id: anchor_id, material: material,
                length: length, flexibility: flexibility)
  @chains[c.id] = c
  c
end

#drag_anchor(anchor_id:, rate: Constants::DRAG_RATE) ⇒ Object



42
43
44
45
46
# File 'lib/legion/extensions/agentic/self/anchor/helpers/anchor_engine.rb', line 42

def drag_anchor(anchor_id:, rate: Constants::DRAG_RATE)
  anchor = fetch_anchor(anchor_id)
  anchor.drag!(rate: rate)
  anchor
end

#drift_all!Object



48
49
50
# File 'lib/legion/extensions/agentic/self/anchor/helpers/anchor_engine.rb', line 48

def drift_all!
  @anchors.each_value(&:drift!)
end

#drifting_anchorsObject



78
79
80
# File 'lib/legion/extensions/agentic/self/anchor/helpers/anchor_engine.rb', line 78

def drifting_anchors
  @anchors.values.select(&:drifting?)
end

#ironclad_anchorsObject



74
75
76
# File 'lib/legion/extensions/agentic/self/anchor/helpers/anchor_engine.rb', line 74

def ironclad_anchors
  @anchors.values.select(&:ironclad?)
end

#strongest_anchors(limit: 5) ⇒ Object



66
67
68
# File 'lib/legion/extensions/agentic/self/anchor/helpers/anchor_engine.rb', line 66

def strongest_anchors(limit: 5)
  @anchors.values.sort_by { |a| -a.grip }.first(limit)
end

#weakest_anchors(limit: 5) ⇒ Object



70
71
72
# File 'lib/legion/extensions/agentic/self/anchor/helpers/anchor_engine.rb', line 70

def weakest_anchors(limit: 5)
  @anchors.values.sort_by(&:grip).first(limit)
end

#wear_all_chains!Object



52
53
54
# File 'lib/legion/extensions/agentic/self/anchor/helpers/anchor_engine.rb', line 52

def wear_all_chains!
  @chains.each_value(&:wear!)
end