Class: Legion::Extensions::Agentic::Homeostasis::Tempo::Helpers::TempoEngine

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTempoEngine

Returns a new instance of TempoEngine.



12
13
14
15
# File 'lib/legion/extensions/agentic/homeostasis/tempo/helpers/tempo_engine.rb', line 12

def initialize
  @records          = {}
  @domain_baselines = {}
end

Instance Attribute Details

#domain_baselinesObject (readonly)

Returns the value of attribute domain_baselines.



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

def domain_baselines
  @domain_baselines
end

#recordsObject (readonly)

Returns the value of attribute records.



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

def records
  @records
end

Instance Method Details

#adapt_tempo(domain:, target:) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/legion/extensions/agentic/homeostasis/tempo/helpers/tempo_engine.rb', line 35

def adapt_tempo(domain:, target:)
  latest = latest_record(domain)
  return nil unless latest

  latest.adapt_to!(target: target)
  latest
end

#average_mismatchObject



43
44
45
46
47
48
# File 'lib/legion/extensions/agentic/homeostasis/tempo/helpers/tempo_engine.rb', line 43

def average_mismatch
  all = @records.values.flatten
  return 0.0 if all.empty?

  all.sum(&:mismatch) / all.size.to_f
end

#domains_in_syncObject



50
51
52
53
54
55
56
57
# File 'lib/legion/extensions/agentic/homeostasis/tempo/helpers/tempo_engine.rb', line 50

def domains_in_sync
  @records.keys.select do |domain|
    latest = latest_record(domain)
    next false unless latest

    latest.mismatch <= 0.1
  end
end

#domains_mismatchedObject



59
60
61
62
63
64
65
66
# File 'lib/legion/extensions/agentic/homeostasis/tempo/helpers/tempo_engine.rb', line 59

def domains_mismatched
  @records.keys.reject do |domain|
    latest = latest_record(domain)
    next true unless latest

    latest.mismatch <= 0.1
  end
end

#record_tempo(domain:, current_tempo:, task_requirement:) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/legion/extensions/agentic/homeostasis/tempo/helpers/tempo_engine.rb', line 21

def record_tempo(domain:, current_tempo:, task_requirement:)
  entry = TempoRecord.new(
    domain:                 domain,
    baseline_tempo:         @domain_baselines.fetch(domain, Constants::DEFAULT_TEMPO),
    current_tempo:          current_tempo,
    task_tempo_requirement: task_requirement
  )

  @records[domain] ||= []
  @records[domain] << entry
  trim_domain(domain)
  entry
end

#set_baseline(domain:, tempo:) ⇒ Object



17
18
19
# File 'lib/legion/extensions/agentic/homeostasis/tempo/helpers/tempo_engine.rb', line 17

def set_baseline(domain:, tempo:)
  @domain_baselines[domain] = tempo.clamp(0.0, 1.0)
end

#tempo_reportObject



68
69
70
71
72
73
74
75
76
# File 'lib/legion/extensions/agentic/homeostasis/tempo/helpers/tempo_engine.rb', line 68

def tempo_report
  {
    domains:          @records.keys,
    average_mismatch: average_mismatch.round(10),
    in_sync:          domains_in_sync,
    mismatched:       domains_mismatched,
    total_records:    @records.values.sum(&:size)
  }
end

#to_hObject



78
79
80
81
82
83
# File 'lib/legion/extensions/agentic/homeostasis/tempo/helpers/tempo_engine.rb', line 78

def to_h
  {
    domain_baselines: @domain_baselines,
    records:          @records.transform_values { |recs| recs.map(&:to_h) }
  }
end