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.



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

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
  @decay_factor = 0.0
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

#decay_factorObject (readonly)

Returns the value of attribute decay_factor.



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

def decay_factor
  @decay_factor
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



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

def age!(fray_rate: Constants::FRAY_RATE)
  @decay_factor = (@decay_factor + fray_rate.abs).clamp(0.0, 1.0).round(10)
  fray_rate
end

#coherence_score(threads) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 78

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



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

def completeness
  return 0.0 if @capacity.zero?

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

#completeness_labelObject



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

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

#complexity_label(threads) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @thread_ids.empty?
end

#fraying?(threads) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 64

def fraying?(threads)
  return false if empty?

  avg = average_strength(threads)
  avg < 0.3
end

#full?Boolean

Returns:

  • (Boolean)


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

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

#gap_countObject



98
99
100
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 98

def gap_count
  @capacity - @thread_ids.size
end

#masterwork?(threads) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 71

def masterwork?(threads)
  return false if empty?

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

#repair!(boost: 0.1) ⇒ Object



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

def repair!(boost: 0.1)
  @decay_factor = (@decay_factor - boost.abs).clamp(0.0, 1.0).round(10)
  boost
end

#sizeObject



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

def size
  @thread_ids.size
end

#to_h(threads = []) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb', line 102

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),
    decay_factor:       decay_factor,
    thread_ids:         @thread_ids.dup,
    created_at:         @created_at
  }
end

#unravel_thread(thread_id) ⇒ Object



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

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

#weave_thread(thread_id) ⇒ Object



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

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

  @thread_ids << thread_id
  true
end