Class: Legion::Extensions::Agentic::Homeostasis::Hourglass::Helpers::Grain

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/homeostasis/hourglass/helpers/grain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grain_type:, domain: nil, content: nil, weight: 1.0) ⇒ Grain

Returns a new instance of Grain.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/legion/extensions/agentic/homeostasis/hourglass/helpers/grain.rb', line 12

def initialize(grain_type:, domain: nil, content: nil, weight: 1.0)
  raise ArgumentError, "unknown grain_type: #{grain_type}" unless Constants::GRAIN_TYPES.include?(grain_type.to_sym)
  raise ArgumentError, 'weight must be between 0.0 and 1.0' unless (0.0..1.0).cover?(weight.to_f)

  @id         = SecureRandom.uuid
  @grain_type = grain_type.to_sym
  @domain     = domain&.to_s
  @content    = content
  @weight     = weight.to_f.clamp(0.0, 1.0).round(10)
  @created_at = Time.now.utc
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



10
11
12
# File 'lib/legion/extensions/agentic/homeostasis/hourglass/helpers/grain.rb', line 10

def content
  @content
end

#created_atObject (readonly)

Returns the value of attribute created_at.



10
11
12
# File 'lib/legion/extensions/agentic/homeostasis/hourglass/helpers/grain.rb', line 10

def created_at
  @created_at
end

#domainObject (readonly)

Returns the value of attribute domain.



10
11
12
# File 'lib/legion/extensions/agentic/homeostasis/hourglass/helpers/grain.rb', line 10

def domain
  @domain
end

#grain_typeObject (readonly)

Returns the value of attribute grain_type.



10
11
12
# File 'lib/legion/extensions/agentic/homeostasis/hourglass/helpers/grain.rb', line 10

def grain_type
  @grain_type
end

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/legion/extensions/agentic/homeostasis/hourglass/helpers/grain.rb', line 10

def id
  @id
end

#weightObject (readonly)

Returns the value of attribute weight.



10
11
12
# File 'lib/legion/extensions/agentic/homeostasis/hourglass/helpers/grain.rb', line 10

def weight
  @weight
end

Instance Method Details

#dust?Boolean

A grain is dust when its weight is below 0.05 — nearly weightless

Returns:

  • (Boolean)


37
38
39
# File 'lib/legion/extensions/agentic/homeostasis/hourglass/helpers/grain.rb', line 37

def dust?
  @weight < 0.05
end

#erode!(rate = 0.01) ⇒ Object

Erode the grain — reduce weight by rate, floors at 0.0



25
26
27
28
# File 'lib/legion/extensions/agentic/homeostasis/hourglass/helpers/grain.rb', line 25

def erode!(rate = 0.01)
  @weight = (@weight - rate.to_f.clamp(0.0, 1.0)).clamp(0.0, 1.0).round(10)
  self
end

#heavy?Boolean

A grain is heavy when its weight is above 0.75 — substantial cognitive unit

Returns:

  • (Boolean)


42
43
44
# File 'lib/legion/extensions/agentic/homeostasis/hourglass/helpers/grain.rb', line 42

def heavy?
  @weight > 0.75
end

#solidify!(rate = 0.01) ⇒ Object

Solidify the grain — increase weight by rate, caps at 1.0



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

def solidify!(rate = 0.01)
  @weight = (@weight + rate.to_f.clamp(0.0, 1.0)).clamp(0.0, 1.0).round(10)
  self
end

#to_hObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/legion/extensions/agentic/homeostasis/hourglass/helpers/grain.rb', line 46

def to_h
  {
    id:         @id,
    grain_type: @grain_type,
    domain:     @domain,
    content:    @content,
    weight:     @weight,
    created_at: @created_at,
    dust:       dust?,
    heavy:      heavy?
  }
end