Module: Legion::Extensions::Agentic::Homeostasis::TemporalDiscounting::Runners::TemporalDiscounting

Includes:
Helpers::Lex
Included in:
Client
Defined in:
lib/legion/extensions/agentic/homeostasis/temporal_discounting/runners/temporal_discounting.rb

Instance Method Summary collapse

Instance Method Details

#check_worth_waiting(reward_id:, threshold: 0.3) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/runners/temporal_discounting.rb', line 33

def check_worth_waiting(reward_id:, threshold: 0.3, **)
  result = engine.worth_waiting_for?(reward_id: reward_id, threshold: threshold)
  log.debug("[temporal_discounting] worth_waiting=#{result[:worth_waiting]} id=#{reward_id[0..7]}")
  { success: !result.key?(:error), **result }
rescue StandardError => e
  log.warn("[temporal_discounting] check_worth_waiting failed: #{e.message}")
  { success: false, error: e.message }
end

#compare_temporal_rewards(reward_a_id:, reward_b_id:) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/runners/temporal_discounting.rb', line 24

def compare_temporal_rewards(reward_a_id:, reward_b_id:, **)
  result = engine.compare_rewards(reward_a_id: reward_a_id, reward_b_id: reward_b_id)
  log.debug("[temporal_discounting] compare preferred=#{result[:preferred]}")
  { success: !result.key?(:error), **result }
rescue StandardError => e
  log.warn("[temporal_discounting] compare_temporal_rewards failed: #{e.message}")
  { success: false, error: e.message }
end

#compute_optimal_delay(reward_id:, min_value: 0.5) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/runners/temporal_discounting.rb', line 56

def compute_optimal_delay(reward_id:, min_value: 0.5, **)
  result = engine.optimal_delay(reward_id: reward_id, min_value: min_value)
  log.debug("[temporal_discounting] optimal_delay=#{result[:max_delay]} id=#{reward_id[0..7]}")
  { success: !result.key?(:error), **result }
rescue StandardError => e
  log.warn("[temporal_discounting] compute_optimal_delay failed: #{e.message}")
  { success: false, error: e.message }
end

#create_temporal_reward(label:, amount:, delay:, domain: :general, discount_rate: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/runners/temporal_discounting.rb', line 13

def create_temporal_reward(label:, amount:, delay:, domain: :general, discount_rate: nil, **)
  reward = engine.create_reward(label: label, amount: amount, delay: delay,
                                domain: domain, discount_rate: discount_rate)
  log.debug("[temporal_discounting] created reward id=#{reward.id[0..7]} " \
            "sv=#{reward.subjective_value.round(4)} label=#{label}")
  { success: true, reward: reward.to_h }
rescue StandardError => e
  log.warn("[temporal_discounting] create_temporal_reward failed: #{e.message}")
  { success: false, error: e.message }
end

#immediate_vs_delayed_comparison(immediate_amount:, delayed_amount:, delay:, domain: :general) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/runners/temporal_discounting.rb', line 42

def immediate_vs_delayed_comparison(immediate_amount:, delayed_amount:, delay:, domain: :general, **)
  result = engine.immediate_vs_delayed(
    immediate_amount: immediate_amount,
    delayed_amount:   delayed_amount,
    delay:            delay,
    domain:           domain
  )
  log.debug("[temporal_discounting] imm_vs_delayed preferred=#{result[:preferred]}")
  { success: true, **result }
rescue StandardError => e
  log.warn("[temporal_discounting] immediate_vs_delayed_comparison failed: #{e.message}")
  { success: false, error: e.message }
end

#most_valuable_rewards(limit: 5) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/runners/temporal_discounting.rb', line 83

def most_valuable_rewards(limit: 5, **)
  rewards = engine.most_valuable(limit: limit)
  log.debug("[temporal_discounting] most_valuable count=#{rewards.size}")
  { success: true, rewards: rewards.map(&:to_h), count: rewards.size }
rescue StandardError => e
  log.warn("[temporal_discounting] most_valuable_rewards failed: #{e.message}")
  { success: false, error: e.message }
end

#set_domain_discount_rate(domain:, rate:) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/runners/temporal_discounting.rb', line 74

def set_domain_discount_rate(domain:, rate:, **)
  engine.set_domain_rate(domain: domain, rate: rate)
  log.debug("[temporal_discounting] set domain=#{domain} rate=#{rate}")
  { success: true, domain: domain, rate: engine.get_domain_rate(domain) }
rescue StandardError => e
  log.warn("[temporal_discounting] set_domain_discount_rate failed: #{e.message}")
  { success: false, error: e.message }
end

#temporal_discounting_statsObject



103
104
105
106
107
108
109
110
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/runners/temporal_discounting.rb', line 103

def temporal_discounting_stats(**)
  stats = engine.to_h
  log.debug("[temporal_discounting] stats total=#{stats[:total_rewards]}")
  { success: true, **stats }
rescue StandardError => e
  log.warn("[temporal_discounting] temporal_discounting_stats failed: #{e.message}")
  { success: false, error: e.message }
end

#temporal_patience_reportObject



65
66
67
68
69
70
71
72
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/runners/temporal_discounting.rb', line 65

def temporal_patience_report(**)
  report = engine.patience_report
  log.debug("[temporal_discounting] patience_report total=#{report[:total_rewards]}")
  { success: true, **report }
rescue StandardError => e
  log.warn("[temporal_discounting] temporal_patience_report failed: #{e.message}")
  { success: false, error: e.message }
end

#update_temporal_discounting(min_value: 0.05) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/legion/extensions/agentic/homeostasis/temporal_discounting/runners/temporal_discounting.rb', line 92

def update_temporal_discounting(min_value: 0.05, **)
  prune_result = engine.prune_expired(min_value: min_value)
  stats = engine.to_h
  log.debug("[temporal_discounting] update pruned=#{prune_result[:pruned]} " \
            "remaining=#{prune_result[:remaining]}")
  { success: true, pruned: prune_result[:pruned], remaining: prune_result[:remaining], stats: stats }
rescue StandardError => e
  log.warn("[temporal_discounting] update_temporal_discounting failed: #{e.message}")
  { success: false, error: e.message }
end