Class: Legion::Extensions::Agentic::Affect::Fatigue::Helpers::FatigueStore

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/affect/fatigue/helpers/fatigue_store.rb

Constant Summary

Constants included from Constants

Constants::ACTIVE_DRAIN_RATE, Constants::BURNOUT_THRESHOLD, Constants::COGNITIVE_DRAIN_MULTIPLIER, Constants::CRITICAL_THRESHOLD, Constants::EMOTIONAL_DRAIN_MULTIPLIER, Constants::FATIGUE_LEVELS, Constants::MAX_ENERGY, Constants::MAX_HISTORY, Constants::MIN_ENERGY, Constants::PERFORMANCE_DEGRADATION, Constants::RECOVERY_MODES, Constants::RECOVERY_RATES, Constants::RESTING_RECOVERY_RATE, Constants::REST_THRESHOLD, Constants::SECOND_WIND_CHANCE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model: nil) ⇒ FatigueStore

Returns a new instance of FatigueStore.



15
16
17
18
19
20
21
22
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/fatigue_store.rb', line 15

def initialize(model: nil)
  @model                  = model || EnergyModel.new
  @session_start          = Time.now.utc
  @peak_performance_streak = 0
  @total_rest_ticks       = 0
  @total_active_ticks     = 0
  @current_streak         = 0
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



12
13
14
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/fatigue_store.rb', line 12

def model
  @model
end

#peak_performance_streakObject (readonly)

Returns the value of attribute peak_performance_streak.



12
13
14
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/fatigue_store.rb', line 12

def peak_performance_streak
  @peak_performance_streak
end

#session_startObject (readonly)

Returns the value of attribute session_start.



12
13
14
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/fatigue_store.rb', line 12

def session_start
  @session_start
end

#total_active_ticksObject (readonly)

Returns the value of attribute total_active_ticks.



12
13
14
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/fatigue_store.rb', line 12

def total_active_ticks
  @total_active_ticks
end

#total_rest_ticksObject (readonly)

Returns the value of attribute total_rest_ticks.



12
13
14
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/fatigue_store.rb', line 12

def total_rest_ticks
  @total_rest_ticks
end

Instance Method Details

#energy_forecast(ticks:) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/fatigue_store.rb', line 78

def energy_forecast(ticks:)
  current    = @model.energy
  drain_rate = Constants::ACTIVE_DRAIN_RATE
  projected  = []

  ticks.times do |i|
    projected_energy = (current - (drain_rate * (i + 1))).clamp(Constants::MIN_ENERGY, Constants::MAX_ENERGY)
    level = classify_level(projected_energy)
    projected << {
      tick:          i + 1,
      energy:        projected_energy.round(4),
      fatigue_level: level
    }
  end

  {
    current_energy: current.round(4),
    forecast:       projected,
    ticks_to_rest:  @model.time_to_rest_threshold
  }
end

#optimal_rest_scheduleObject



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/fatigue_store.rb', line 100

def optimal_rest_schedule
  ticks_until_rest = @model.time_to_rest_threshold
  ticks_to_recover = @model.time_to_full_recovery

  {
    recommend_rest_in:   ticks_until_rest,
    full_recovery_ticks: ticks_to_recover,
    current_energy:      @model.energy.round(4),
    recommended_mode:    suggest_recovery_mode,
    trend:               @model.trend
  }
end

#recommend_actionObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/fatigue_store.rb', line 45

def recommend_action
  energy = @model.energy

  if @model.burnout?
    :emergency_shutdown
  elsif energy < Constants::CRITICAL_THRESHOLD
    :enter_rest
  elsif @model.needs_rest?
    :take_break
  elsif @model.fatigue_level == :tired
    :reduce_load
  else
    :continue
  end
end

#session_statsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/fatigue_store.rb', line 61

def session_stats
  total_ticks = @total_active_ticks + @total_rest_ticks
  duration = Time.now.utc - @session_start

  {
    duration_seconds:        duration.round(2),
    total_ticks:             total_ticks,
    active_ticks:            @total_active_ticks,
    rest_ticks:              @total_rest_ticks,
    active_ratio:            total_ticks.positive? ? (@total_active_ticks.to_f / total_ticks).round(4) : 0.0,
    current_energy:          @model.energy.round(4),
    fatigue_level:           @model.fatigue_level,
    burnout:                 @model.burnout?,
    peak_performance_streak: @peak_performance_streak
  }
end

#update(tick_results: {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/legion/extensions/agentic/affect/fatigue/helpers/fatigue_store.rb', line 24

def update(tick_results: {})
  cognitive_load    = tick_results[:cognitive_load] || 0.5
  emotional_arousal = tick_results[:emotional_arousal] || 0.5
  is_resting        = tick_results[:mode] == :resting || !@model.recovery_mode.nil?

  result = @model.tick(
    cognitive_load:    cognitive_load,
    emotional_arousal: emotional_arousal,
    is_resting:        is_resting
  )

  if is_resting
    @total_rest_ticks += 1
  else
    @total_active_ticks += 1
  end

  update_streak(result[:performance_factor])
  result
end