Class: Legion::Extensions::Agentic::Homeostasis::Cocoon::Helpers::Incubator

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/homeostasis/cocoon/helpers/incubator.rb

Constant Summary

Constants included from Constants

Constants::COCOON_TYPES, Constants::GESTATION_STAGES, Constants::MATURITY_LABELS, Constants::MATURITY_RATE, Constants::MAX_COCOONS, Constants::PREMATURE_PENALTY, Constants::PROTECTION_BY_TYPE

Instance Method Summary collapse

Methods included from Constants

label_for

Constructor Details

#initializeIncubator

Returns a new instance of Incubator.



12
13
14
# File 'lib/legion/extensions/agentic/homeostasis/cocoon/helpers/incubator.rb', line 12

def initialize
  @cocoons = {}
end

Instance Method Details

#by_stage(stage) ⇒ Object



52
53
54
# File 'lib/legion/extensions/agentic/homeostasis/cocoon/helpers/incubator.rb', line 52

def by_stage(stage)
  @cocoons.values.select { |c| c.stage == stage.to_sym }
end

#create_cocoon(cocoon_type:, domain:, content: '', maturity: nil, protection: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/legion/extensions/agentic/homeostasis/cocoon/helpers/incubator.rb', line 16

def create_cocoon(cocoon_type:, domain:, content: '', maturity: nil, protection: nil)
  return nil unless COCOON_TYPES.include?(cocoon_type.to_sym)

  prune_cocoons
  cocoon = Cocoon.new(
    cocoon_type: cocoon_type,
    domain:      domain,
    content:     content,
    maturity:    maturity,
    protection:  protection
  )
  @cocoons[cocoon.id] = cocoon
  cocoon
end

#force_emerge(id) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/legion/extensions/agentic/homeostasis/cocoon/helpers/incubator.rb', line 43

def force_emerge(id)
  cocoon = @cocoons[id]
  return { success: false, error: 'cocoon not found' } unless cocoon

  result = cocoon.expose!
  @cocoons.delete(id) if cocoon.stage == :emerged
  result
end

#gestate_all!(rate = MATURITY_RATE) ⇒ Object



31
32
33
34
# File 'lib/legion/extensions/agentic/homeostasis/cocoon/helpers/incubator.rb', line 31

def gestate_all!(rate = MATURITY_RATE)
  @cocoons.each_value { |c| c.gestate!(rate) unless c.stage == :emerged }
  self
end

#harvest_readyObject



36
37
38
39
40
41
# File 'lib/legion/extensions/agentic/homeostasis/cocoon/helpers/incubator.rb', line 36

def harvest_ready
  ready = @cocoons.values.select(&:ready?)
  results = ready.map(&:emerge!)
  ready.each { |c| @cocoons.delete(c.id) if c.stage == :emerged }
  results
end

#incubator_reportObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/legion/extensions/agentic/homeostasis/cocoon/helpers/incubator.rb', line 60

def incubator_report
  total    = @cocoons.size
  by_s     = Hash.new(0)
  @cocoons.each_value { |c| by_s[c.stage] += 1 }
  avg_mat  = total.zero? ? 0.0 : (@cocoons.values.sum(&:maturity) / total).round(10)
  avg_prot = total.zero? ? 0.0 : (@cocoons.values.sum(&:protection) / total).round(10)

  {
    total_cocoons:      total,
    average_maturity:   avg_mat,
    maturity_label:     Constants.label_for(MATURITY_LABELS, avg_mat),
    average_protection: avg_prot,
    stage_distribution: by_s,
    ready_count:        by_stage(:ready).size,
    most_mature:        most_mature(limit: 3).map(&:to_h)
  }
end

#most_mature(limit: 5) ⇒ Object



56
57
58
# File 'lib/legion/extensions/agentic/homeostasis/cocoon/helpers/incubator.rb', line 56

def most_mature(limit: 5)
  @cocoons.values.sort_by { |c| -c.maturity }.first(limit)
end

#to_hObject



78
79
80
81
82
83
# File 'lib/legion/extensions/agentic/homeostasis/cocoon/helpers/incubator.rb', line 78

def to_h
  {
    total_cocoons:    @cocoons.size,
    average_maturity: @cocoons.empty? ? 0.0 : (@cocoons.values.sum(&:maturity) / @cocoons.size).round(10)
  }
end