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

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

Constant Summary

Constants included from Constants

Legion::Extensions::Agentic::Homeostasis::Cocoon::Helpers::Constants::COCOON_TYPES, Legion::Extensions::Agentic::Homeostasis::Cocoon::Helpers::Constants::GESTATION_STAGES, Legion::Extensions::Agentic::Homeostasis::Cocoon::Helpers::Constants::MATURITY_LABELS, Legion::Extensions::Agentic::Homeostasis::Cocoon::Helpers::Constants::MATURITY_RATE, Legion::Extensions::Agentic::Homeostasis::Cocoon::Helpers::Constants::MAX_COCOONS, Legion::Extensions::Agentic::Homeostasis::Cocoon::Helpers::Constants::PREMATURE_PENALTY, Legion::Extensions::Agentic::Homeostasis::Cocoon::Helpers::Constants::PROTECTION_BY_TYPE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Constants

label_for

Constructor Details

#initialize(cocoon_type:, domain:, content: '', maturity: nil, protection: nil) ⇒ Cocoon

Returns a new instance of Cocoon.



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

def initialize(cocoon_type:, domain:, content: '', maturity: nil, protection: nil)
  @id = SecureRandom.uuid
  @cocoon_type = cocoon_type.to_sym
  @domain     = domain.to_sym
  @content    = content
  @maturity   = (maturity || 0.0).to_f.clamp(0.0, 1.0)
  @protection = (protection || PROTECTION_BY_TYPE.fetch(@cocoon_type, 0.7)).to_f.clamp(0.0, 1.0)
  @stage      = :encapsulating
  @created_at = Time.now.utc
  advance_stage!
end

Instance Attribute Details

#cocoon_typeObject (readonly)

Returns the value of attribute cocoon_type.



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

def cocoon_type
  @cocoon_type
end

#contentObject (readonly)

Returns the value of attribute content.



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

def content
  @content
end

#created_atObject (readonly)

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#domainObject (readonly)

Returns the value of attribute domain.



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

def domain
  @domain
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#maturityObject (readonly)

Returns the value of attribute maturity.



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

def maturity
  @maturity
end

#protectionObject (readonly)

Returns the value of attribute protection.



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

def protection
  @protection
end

#stageObject (readonly)

Returns the value of attribute stage.



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

def stage
  @stage
end

Instance Method Details

#age_secondsObject



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

def age_seconds
  (Time.now.utc - @created_at).round(2)
end

#emerge!Object



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

def emerge!
  return { success: false, error: 'not ready', damaged: false } unless ready?

  @stage = :emerged
  { success: true, content: @content, damaged: false, maturity: @maturity }
end

#expose!Object



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

def expose!
  damaged = premature?
  @maturity = (@maturity * PREMATURE_PENALTY).round(10) if damaged
  @stage = :emerged
  { success: true, content: @content, damaged: damaged, maturity: @maturity }
end

#gestate!(rate = MATURITY_RATE) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/legion/extensions/agentic/homeostasis/cocoon/helpers/cocoon.rb', line 27

def gestate!(rate = MATURITY_RATE)
  return self if @stage == :emerged

  @maturity = (@maturity + rate).clamp(0.0, 1.0).round(10)
  advance_stage!
  self
end

#premature?Boolean

Returns:

  • (Boolean)


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

def premature?
  @stage != :ready && @stage != :emerged && @maturity < 1.0
end

#ready?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/legion/extensions/agentic/homeostasis/cocoon/helpers/cocoon.rb', line 49

def ready?
  @maturity >= 1.0 || @stage == :ready
end

#to_hObject



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

def to_h
  {
    id:             @id,
    cocoon_type:    @cocoon_type,
    domain:         @domain,
    content:        @content,
    maturity:       @maturity.round(10),
    stage:          @stage,
    protection:     @protection.round(10),
    ready:          ready?,
    premature:      premature?,
    maturity_label: Constants.label_for(MATURITY_LABELS, @maturity),
    age_seconds:    age_seconds,
    created_at:     @created_at.iso8601
  }
end