Class: Legion::Extensions::Agentic::Executive::Chunking::Helpers::Chunk

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb

Constant Summary

Constants included from Constants

Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::CAPACITY_LABELS, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::CAPACITY_VARIANCE, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::CHUNK_SIZE_LABELS, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::COHERENCE_BOOST, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::COHERENCE_DECAY, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::COHERENCE_LABELS, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::DEFAULT_COHERENCE, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::MAX_CHUNKS, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::MAX_ITEMS, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::RECALL_BOOST, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::RECALL_DECAY, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::RECALL_LABELS, Legion::Extensions::Agentic::Executive::Chunking::Helpers::Constants::WORKING_MEMORY_CAPACITY

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label:, item_ids: []) ⇒ Chunk

Returns a new instance of Chunk.



15
16
17
18
19
20
21
22
23
24
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 15

def initialize(label:, item_ids: [])
  @id             = ::SecureRandom.uuid
  @label          = label
  @item_ids       = item_ids.dup
  @sub_chunk_ids  = []
  @coherence      = DEFAULT_COHERENCE
  @recall_strength = 0.8
  @access_count   = 0
  @created_at     = Time.now.utc
end

Instance Attribute Details

#access_countObject (readonly)

Returns the value of attribute access_count.



12
13
14
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 12

def access_count
  @access_count
end

#coherenceObject (readonly)

Returns the value of attribute coherence.



12
13
14
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 12

def coherence
  @coherence
end

#created_atObject (readonly)

Returns the value of attribute created_at.



12
13
14
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 12

def created_at
  @created_at
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 12

def id
  @id
end

#item_idsObject (readonly)

Returns the value of attribute item_ids.



12
13
14
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 12

def item_ids
  @item_ids
end

#labelObject (readonly)

Returns the value of attribute label.



12
13
14
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 12

def label
  @label
end

#recall_strengthObject (readonly)

Returns the value of attribute recall_strength.



12
13
14
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 12

def recall_strength
  @recall_strength
end

#sub_chunk_idsObject (readonly)

Returns the value of attribute sub_chunk_ids.



12
13
14
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 12

def sub_chunk_ids
  @sub_chunk_ids
end

Instance Method Details

#add_item!(item_id:) ⇒ Object



26
27
28
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 26

def add_item!(item_id:)
  @item_ids << item_id unless @item_ids.include?(item_id)
end

#add_sub_chunk!(chunk_id:) ⇒ Object



34
35
36
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 34

def add_sub_chunk!(chunk_id:)
  @sub_chunk_ids << chunk_id unless @sub_chunk_ids.include?(chunk_id)
end

#coherence_labelObject



57
58
59
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 57

def coherence_label
  COHERENCE_LABELS.find { |range, _| range.cover?(@coherence) }&.last || :unchunked
end

#decay!Object



44
45
46
47
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 44

def decay!
  @recall_strength = [(@recall_strength - RECALL_DECAY).round(10), 0.0].max
  @coherence       = [(@coherence - COHERENCE_DECAY).round(10), 0.0].max
end

#hierarchical?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 53

def hierarchical?
  !@sub_chunk_ids.empty?
end

#recall_labelObject



61
62
63
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 61

def recall_label
  RECALL_LABELS.find { |range, _| range.cover?(@recall_strength) }&.last || :forgotten
end

#reinforce!Object



38
39
40
41
42
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 38

def reinforce!
  @access_count   += 1
  @coherence       = [(@coherence + COHERENCE_BOOST).round(10), 1.0].min
  @recall_strength = [(@recall_strength + RECALL_BOOST).round(10), 1.0].min
end

#remove_item!(item_id:) ⇒ Object



30
31
32
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 30

def remove_item!(item_id:)
  @item_ids.delete(item_id)
end

#sizeObject



49
50
51
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 49

def size
  @item_ids.size
end

#size_labelObject



65
66
67
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 65

def size_label
  CHUNK_SIZE_LABELS.find { |range, _| range.cover?(size) }&.last || :micro
end

#to_hObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/legion/extensions/agentic/executive/chunking/helpers/chunk.rb', line 69

def to_h
  {
    id:              @id,
    label:           @label,
    item_ids:        @item_ids.dup,
    sub_chunk_ids:   @sub_chunk_ids.dup,
    coherence:       @coherence.round(10),
    recall_strength: @recall_strength.round(10),
    access_count:    @access_count,
    created_at:      @created_at,
    size:            size,
    hierarchical:    hierarchical?,
    coherence_label: coherence_label,
    recall_label:    recall_label,
    size_label:      size_label
  }
end