Class: Legion::Extensions::Agentic::Homeostasis::Tide::Helpers::TidalPool

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain:, capacity: 20) ⇒ TidalPool

Returns a new instance of TidalPool.

Raises:

  • (ArgumentError)


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

def initialize(domain:, capacity: 20)
  raise ArgumentError, 'capacity must be positive' unless capacity.positive?

  @id               = SecureRandom.uuid
  @domain           = domain.to_s
  @capacity         = capacity
  @items            = []
  @evaporation_count = 0
  @created_at = Time.now.utc
end

Instance Attribute Details

#capacityObject (readonly)

Returns the value of attribute capacity.



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

def capacity
  @capacity
end

#domainObject (readonly)

Returns the value of attribute domain.



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

def domain
  @domain
end

#evaporation_countObject (readonly)

Returns the value of attribute evaporation_count.



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

def evaporation_count
  @evaporation_count
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

Instance Method Details

#deposit(item) ⇒ Object

Deposit an idea item into the pool; silently drops if full



24
25
26
27
28
29
# File 'lib/legion/extensions/agentic/homeostasis/tide/helpers/tidal_pool.rb', line 24

def deposit(item)
  return false if full?

  @items << { content: item, deposited_at: Time.now.utc, id: SecureRandom.uuid }
  true
end

#depthObject

Depth as a fraction of capacity: item_count / capacity



56
57
58
59
60
# File 'lib/legion/extensions/agentic/homeostasis/tide/helpers/tidal_pool.rb', line 56

def depth
  return 0.0 if @capacity.zero?

  (@items.size.to_f / @capacity).round(10)
end

#empty?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/legion/extensions/agentic/homeostasis/tide/helpers/tidal_pool.rb', line 47

def empty?
  @items.empty?
end

#evaporate!(rate = Constants::POOL_EVAPORATION_RATE) ⇒ Object

Apply evaporation: remove a proportion of items (oldest first)



39
40
41
42
43
44
45
# File 'lib/legion/extensions/agentic/homeostasis/tide/helpers/tidal_pool.rb', line 39

def evaporate!(rate = Constants::POOL_EVAPORATION_RATE)
  clamped_rate = rate.clamp(0.0, 1.0)
  count_to_remove = (@items.size * clamped_rate).ceil
  removed = @items.shift(count_to_remove)
  @evaporation_count += removed.size
  removed.size
end

#full?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/legion/extensions/agentic/homeostasis/tide/helpers/tidal_pool.rb', line 51

def full?
  @items.size >= @capacity
end

#harvest!Object

Harvest all items from the pool, clearing it; returns the harvested items



32
33
34
35
36
# File 'lib/legion/extensions/agentic/homeostasis/tide/helpers/tidal_pool.rb', line 32

def harvest!
  harvested = @items.dup
  @items.clear
  harvested
end

#itemsObject



62
63
64
# File 'lib/legion/extensions/agentic/homeostasis/tide/helpers/tidal_pool.rb', line 62

def items
  @items.dup
end

#sizeObject



66
67
68
# File 'lib/legion/extensions/agentic/homeostasis/tide/helpers/tidal_pool.rb', line 66

def size
  @items.size
end

#to_hObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/legion/extensions/agentic/homeostasis/tide/helpers/tidal_pool.rb', line 70

def to_h
  {
    id:                @id,
    domain:            @domain,
    capacity:          @capacity,
    size:              @items.size,
    depth:             depth,
    evaporation_count: @evaporation_count,
    created_at:        @created_at
  }
end