Class: Legion::Extensions::Agentic::Integration::Tapestry::Helpers::Tapestry

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, pattern:, capacity: 50) ⇒ Tapestry

Returns a new instance of Tapestry.



12
13
14
15
16
17
18
19
20
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 12

def initialize(name:, pattern:, capacity: 50)
  validate_pattern!(pattern)
  @id         = SecureRandom.uuid
  @name       = name.to_s
  @pattern    = pattern.to_sym
  @capacity   = capacity.to_i.clamp(1, 500)
  @thread_ids = []
  @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/integration/tapestry/helpers/tapestry.rb', line 10

def capacity
  @capacity
end

#created_atObject (readonly)

Returns the value of attribute created_at.



10
11
12
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 10

def created_at
  @created_at
end

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 10

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 10

def name
  @name
end

#patternObject (readonly)

Returns the value of attribute pattern.



10
11
12
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 10

def pattern
  @pattern
end

#thread_idsObject (readonly)

Returns the value of attribute thread_ids.



10
11
12
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 10

def thread_ids
  @thread_ids
end

Instance Method Details

#age!(fray_rate: Constants::FRAY_RATE) ⇒ Object



52
53
54
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 52

def age!(fray_rate: Constants::FRAY_RATE)
  fray_rate
end

#coherence_score(threads) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 74

def coherence_score(threads)
  return 0.0 if empty?

  placed = threads.select { |t| @thread_ids.include?(t.id) }
  return 0.0 if placed.empty?

  type_diversity = placed.map(&:thread_type).uniq.size.to_f / Constants::THREAD_TYPES.size
  avg_str        = placed.sum(&:strength) / placed.size
  pattern_bonus  = pattern_coherence_factor
  ((avg_str * 0.5) + (type_diversity * 0.3) + (pattern_bonus * 0.2)).clamp(0.0, 1.0).round(10)
end

#completenessObject



46
47
48
49
50
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 46

def completeness
  return 0.0 if @capacity.zero?

  (@thread_ids.size.to_f / @capacity).clamp(0.0, 1.0).round(10)
end

#completeness_labelObject



86
87
88
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 86

def completeness_label
  Constants.label_for(Constants::INTEGRITY_LABELS, completeness)
end

#complexity_label(threads) ⇒ Object



90
91
92
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 90

def complexity_label(threads)
  Constants.label_for(Constants::COMPLEXITY_LABELS, coherence_score(threads))
end

#empty?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 42

def empty?
  @thread_ids.empty?
end

#fraying?(threads) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 60

def fraying?(threads)
  return false if empty?

  avg = average_strength(threads)
  avg < 0.3
end

#full?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 38

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

#gap_countObject



94
95
96
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 94

def gap_count
  @capacity - @thread_ids.size
end

#masterwork?(threads) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 67

def masterwork?(threads)
  return false if empty?

  avg = average_strength(threads)
  avg >= 0.9 && completeness >= 0.9
end

#repair!(boost: 0.1) ⇒ Object



56
57
58
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 56

def repair!(boost: 0.1)
  boost
end

#sizeObject



34
35
36
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 34

def size
  @thread_ids.size
end

#to_h(threads = []) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 98

def to_h(threads = [])
  {
    id:                 @id,
    name:               @name,
    pattern:            @pattern,
    capacity:           @capacity,
    size:               size,
    completeness:       completeness,
    completeness_label: completeness_label,
    coherence_score:    coherence_score(threads),
    gap_count:          gap_count,
    full:               full?,
    empty:              empty?,
    fraying:            fraying?(threads),
    masterwork:         masterwork?(threads),
    thread_ids:         @thread_ids.dup,
    created_at:         @created_at
  }
end

#unravel_thread(thread_id) ⇒ Object



30
31
32
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 30

def unravel_thread(thread_id)
  @thread_ids.delete(thread_id) ? true : false
end

#weave_thread(thread_id) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 22

def weave_thread(thread_id)
  return false if @thread_ids.include?(thread_id)
  return false if full?

  @thread_ids << thread_id
  true
end