Class: Legion::Extensions::Agentic::Learning::Habit::Helpers::HabitStore
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Learning::Habit::Helpers::HabitStore
show all
- Includes:
- Constants
- Defined in:
- lib/legion/extensions/agentic/learning/habit/helpers/habit_store.rb
Constant Summary
collapse
- MAX_BUFFER_SIZE =
50
Constants included
from Constants
Constants::CHUNKING_THRESHOLD, Constants::COGNITIVE_COST, Constants::CONTEXT_DIMENSIONS, Constants::DECAY_RATE, Constants::HABIT_STRENGTH_FLOOR, Constants::MATURITY_STAGES, Constants::MATURITY_THRESHOLDS, Constants::MAX_HABITS, Constants::MAX_SEQUENCE_LENGTH, Constants::MIN_SEQUENCE_LENGTH, Constants::REINFORCEMENT_RATE, Constants::SIMILARITY_THRESHOLD
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of HabitStore.
16
17
18
19
|
# File 'lib/legion/extensions/agentic/learning/habit/helpers/habit_store.rb', line 16
def initialize
@habits = {}
@action_buffer = []
end
|
Instance Attribute Details
#action_buffer ⇒ Object
Returns the value of attribute action_buffer.
14
15
16
|
# File 'lib/legion/extensions/agentic/learning/habit/helpers/habit_store.rb', line 14
def action_buffer
@action_buffer
end
|
#habits ⇒ Object
Returns the value of attribute habits.
14
15
16
|
# File 'lib/legion/extensions/agentic/learning/habit/helpers/habit_store.rb', line 14
def habits
@habits
end
|
Instance Method Details
#by_maturity(stage) ⇒ Object
76
77
78
|
# File 'lib/legion/extensions/agentic/learning/habit/helpers/habit_store.rb', line 76
def by_maturity(stage)
@habits.values.select { |h| h.maturity == stage }
end
|
#decay_all ⇒ Object
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/legion/extensions/agentic/learning/habit/helpers/habit_store.rb', line 57
def decay_all
removed = 0
@habits.each do |id, habit|
unless habit.decay
@habits.delete(id)
removed += 1
end
end
removed
end
|
#detect_patterns ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/legion/extensions/agentic/learning/habit/helpers/habit_store.rb', line 26
def detect_patterns
new_habits = []
actions = @action_buffer.map { |e| e[:action] }
ctx = @action_buffer.last&.dig(:context) || {}
(Constants::MIN_SEQUENCE_LENGTH..Constants::MAX_SEQUENCE_LENGTH).each do |len|
next if actions.size < len
process_length(len, actions, ctx, new_habits)
end
new_habits
end
|
#evict_if_needed ⇒ Object
89
90
91
92
93
94
|
# File 'lib/legion/extensions/agentic/learning/habit/helpers/habit_store.rb', line 89
def evict_if_needed
return unless @habits.size > Constants::MAX_HABITS
weakest_id = @habits.min_by { |_id, h| h.strength }.first
@habits.delete(weakest_id)
end
|
#find_matching(context: {}) ⇒ Object
40
41
42
43
44
|
# File 'lib/legion/extensions/agentic/learning/habit/helpers/habit_store.rb', line 40
def find_matching(context: {})
@habits.values
.select { |h| h.matches_context?(context) }
.sort_by { |h| -h.strength }
end
|
#get(id) ⇒ Object
46
47
48
|
# File 'lib/legion/extensions/agentic/learning/habit/helpers/habit_store.rb', line 46
def get(id)
@habits[id]
end
|
#merge_similar ⇒ Object
68
69
70
71
72
73
74
|
# File 'lib/legion/extensions/agentic/learning/habit/helpers/habit_store.rb', line 68
def merge_similar
merged = 0
@habits.keys.combination(2).each do |id_a, id_b|
merged += 1 if merge_pair(id_a, id_b)
end
merged
end
|
#record_action(action, context: {}) ⇒ Object
21
22
23
24
|
# File 'lib/legion/extensions/agentic/learning/habit/helpers/habit_store.rb', line 21
def record_action(action, context: {})
@action_buffer << { action: action.to_sym, context: context }
@action_buffer.shift if @action_buffer.size > MAX_BUFFER_SIZE
end
|
#reinforce(id, success:) ⇒ Object
50
51
52
53
54
55
|
# File 'lib/legion/extensions/agentic/learning/habit/helpers/habit_store.rb', line 50
def reinforce(id, success:)
habit = @habits[id]
return unless habit
habit.record_execution(success: success)
end
|
#stats ⇒ Object
80
81
82
83
84
85
86
87
|
# File 'lib/legion/extensions/agentic/learning/habit/helpers/habit_store.rb', line 80
def stats
habits_list = @habits.values
per_maturity = Constants::MATURITY_STAGES.to_h { |s| [s, 0] }
habits_list.each { |h| per_maturity[h.maturity] += 1 }
avg_strength = habits_list.empty? ? 0.0 : habits_list.sum(&:strength) / habits_list.size
oldest = habits_list.min_by(&:created_at)
{ total: habits_list.size, per_maturity: per_maturity, avg_strength: avg_strength, oldest: oldest&.to_h }
end
|