Class: Legion::Extensions::Agentic::Homeostasis::Temporal::Helpers::TimePerception

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTimePerception

Returns a new instance of TimePerception.



12
13
14
15
16
17
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/time_perception.rb', line 12

def initialize
  @events = {}
  @deadlines = {}
  @subjective_rate = 1.0
  @tick_count = 0
end

Instance Attribute Details

#deadlinesObject (readonly)

Returns the value of attribute deadlines.



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

def deadlines
  @deadlines
end

#eventsObject (readonly)

Returns the value of attribute events.



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

def events
  @events
end

#subjective_rateObject (readonly)

Returns the value of attribute subjective_rate.



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

def subjective_rate
  @subjective_rate
end

Instance Method Details

#deadline_urgency(id) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/time_perception.rb', line 58

def deadline_urgency(id)
  dl = @deadlines[id]
  return nil unless dl

  remaining = dl[:at] - Time.now.utc
  return :overdue if remaining <= 0

  total = dl[:at] - dl[:created]
  return :critical if total <= 0

  fraction = remaining / total
  classify_deadline_urgency(fraction)
end

#elapsed_since(event, domain: :general) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/time_perception.rb', line 27

def elapsed_since(event, domain: :general)
  key = "#{domain}:#{event}"
  timestamps = @events[key]
  return nil unless timestamps&.any?

  Time.now.utc - timestamps.last
end

#event_count(event, domain: :general) ⇒ Object



43
44
45
46
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/time_perception.rb', line 43

def event_count(event, domain: :general)
  key = "#{domain}:#{event}"
  @events.fetch(key, []).size
end

#mark_event(event, domain: :general) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/time_perception.rb', line 19

def mark_event(event, domain: :general)
  key = "#{domain}:#{event}"
  @events[key] ||= []
  @events[key] << Time.now.utc
  trim_events(key)
  key
end

#overall_urgencyObject



97
98
99
100
101
102
103
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/time_perception.rb', line 97

def overall_urgency
  urgencies = @deadlines.keys.filter_map { |id| deadline_urgency(id) }
  return :none if urgencies.empty?

  priority = Constants::URGENCY_LEVELS
  urgencies.min_by { |u| u == :overdue ? -1 : priority.index(u) || 99 }
end

#overdue_deadlinesObject



72
73
74
75
76
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/time_perception.rb', line 72

def overdue_deadlines
  now = Time.now.utc
  @deadlines.select { |_id, dl| dl[:at] <= now }
            .map { |id, dl| { id: id, overdue_by: now - dl[:at], description: dl[:description] } }
end

#remove_deadline(id) ⇒ Object



54
55
56
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/time_perception.rb', line 54

def remove_deadline(id)
  @deadlines.delete(id)
end

#set_deadline(id, at:, description: nil) ⇒ Object



48
49
50
51
52
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/time_perception.rb', line 48

def set_deadline(id, at:, description: nil)
  @deadlines[id] = { at: at, created: Time.now.utc, description: description }
  evict_deadlines_if_needed
  id
end

#subjective_elapsed(real_seconds) ⇒ Object



93
94
95
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/time_perception.rb', line 93

def subjective_elapsed(real_seconds)
  real_seconds * @subjective_rate
end

#time_since_first(event, domain: :general) ⇒ Object



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

def time_since_first(event, domain: :general)
  key = "#{domain}:#{event}"
  timestamps = @events[key]
  return nil unless timestamps&.any?

  Time.now.utc - timestamps.first
end

#to_hObject



105
106
107
108
109
110
111
112
113
114
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/time_perception.rb', line 105

def to_h
  {
    event_domains:    @events.size,
    active_deadlines: @deadlines.size,
    overdue_count:    overdue_deadlines.size,
    subjective_rate:  @subjective_rate,
    overall_urgency:  overall_urgency,
    tick_count:       @tick_count
  }
end

#upcoming_deadlines(within: 3600) ⇒ Object



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

def upcoming_deadlines(within: 3600)
  now = Time.now.utc
  cutoff = now + within
  @deadlines.select { |_id, dl| dl[:at] > now && dl[:at] <= cutoff }
            .map { |id, dl| { id: id, remaining: dl[:at] - now, description: dl[:description] } }
            .sort_by { |d| d[:remaining] }
end

#update_subjective_time(arousal: 0.5, cognitive_load: 0.5) ⇒ Object



86
87
88
89
90
91
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/time_perception.rb', line 86

def update_subjective_time(arousal: 0.5, cognitive_load: 0.5, **)
  @tick_count += 1
  raw_dilation = compute_dilation(arousal, cognitive_load)
  @subjective_rate = ema(@subjective_rate, raw_dilation, Constants::SUBJECTIVE_ALPHA)
  @subjective_rate
end