Module: Legion::Extensions::Agentic::Learning::Habit::Runners::Habit

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

Instance Method Summary collapse

Instance Method Details

#decay_habitsObject



50
51
52
53
54
# File 'lib/legion/extensions/agentic/learning/habit/runners/habit.rb', line 50

def decay_habits(**)
  removed = habit_store.decay_all
  log.debug "[habit] decay_habits: removed=#{removed}"
  { decayed: true, removed_count: removed }
end

#execute_habit(id:, success: true) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/legion/extensions/agentic/learning/habit/runners/habit.rb', line 41

def execute_habit(id:, success: true, **)
  habit = habit_store.get(id)
  return { error: :not_found } unless habit

  habit.record_execution(success: success)
  log.debug "[habit] execute_habit: id=#{id} success=#{success} maturity=#{habit.maturity}"
  { executed: true, habit: habit.to_h, cognitive_cost: habit.cognitive_cost }
end

#habit_repertoire(maturity: nil, limit: 20) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/legion/extensions/agentic/learning/habit/runners/habit.rb', line 66

def habit_repertoire(maturity: nil, limit: 20, **)
  habits = maturity ? habit_store.by_maturity(maturity.to_sym) : habit_store.habits.values
  log.debug "[habit] habit_repertoire: maturity=#{maturity} total=#{habits.size}"
  {
    habits: habits.sort_by { |h| -h.strength }.first(limit).map(&:to_h),
    total:  habits.size
  }
end

#habit_statsObject



62
63
64
# File 'lib/legion/extensions/agentic/learning/habit/runners/habit.rb', line 62

def habit_stats(**)
  habit_store.stats
end

#merge_habitsObject



56
57
58
59
60
# File 'lib/legion/extensions/agentic/learning/habit/runners/habit.rb', line 56

def merge_habits(**)
  merged = habit_store.merge_similar
  log.debug "[habit] merge_habits: merged=#{merged}"
  { merged_count: merged }
end

#observe_action(action:, context: {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/legion/extensions/agentic/learning/habit/runners/habit.rb', line 13

def observe_action(action:, context: {}, **)
  log.debug "[habit] observe_action: action=#{action} context=#{context}"
  habit_store.record_action(action, context: context)
  detected = habit_store.detect_patterns
  log.info "[habit] patterns detected: #{detected.size}" unless detected.empty?
  {
    recorded:            true,
    action:              action,
    new_habits_detected: detected.size,
    habits:              detected.map(&:to_h)
  }
end

#suggest_habit(context: {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/legion/extensions/agentic/learning/habit/runners/habit.rb', line 26

def suggest_habit(context: {}, **)
  matches = habit_store.find_matching(context: context)
  log.debug "[habit] suggest_habit: context=#{context} matches=#{matches.size}"
  if matches.empty?
    { suggestion: nil, reason: :no_matching_habits }
  else
    best = matches.first
    {
      suggestion:        best.to_h,
      cognitive_savings: 1.0 - best.cognitive_cost,
      alternatives:      matches[1..2]&.map(&:to_h) || []
    }
  end
end