Class: Legion::Extensions::Agentic::Learning::Habit::Helpers::ActionSequence

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb

Constant Summary

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

#initialize(actions:, context: {}) ⇒ ActionSequence

Returns a new instance of ActionSequence.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb', line 17

def initialize(actions:, context: {})
  @id              = SecureRandom.uuid
  @actions         = actions.map(&:to_sym)
  @context         = context
  @execution_count = 0
  @success_count   = 0
  @strength        = 0.3
  @maturity        = :novel
  @last_executed   = nil
  @created_at      = Time.now.utc
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



14
15
16
# File 'lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb', line 14

def actions
  @actions
end

#contextObject (readonly)

Returns the value of attribute context.



14
15
16
# File 'lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb', line 14

def context
  @context
end

#created_atObject (readonly)

Returns the value of attribute created_at.



14
15
16
# File 'lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb', line 14

def created_at
  @created_at
end

#execution_countObject (readonly)

Returns the value of attribute execution_count.



14
15
16
# File 'lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb', line 14

def execution_count
  @execution_count
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb', line 14

def id
  @id
end

#last_executedObject (readonly)

Returns the value of attribute last_executed.



14
15
16
# File 'lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb', line 14

def last_executed
  @last_executed
end

#maturityObject (readonly)

Returns the value of attribute maturity.



14
15
16
# File 'lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb', line 14

def maturity
  @maturity
end

#strengthObject (readonly)

Returns the value of attribute strength.



14
15
16
# File 'lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb', line 14

def strength
  @strength
end

#success_countObject (readonly)

Returns the value of attribute success_count.



14
15
16
# File 'lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb', line 14

def success_count
  @success_count
end

Instance Method Details

#cognitive_costObject



43
44
45
# File 'lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb', line 43

def cognitive_cost
  Constants::COGNITIVE_COST[@maturity]
end

#decayObject



63
64
65
66
# File 'lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb', line 63

def decay
  @strength -= Constants::DECAY_RATE
  @strength >= Constants::HABIT_STRENGTH_FLOOR
end

#matches_context?(ctx) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
# File 'lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb', line 53

def matches_context?(ctx)
  return true if ctx.empty?

  relevant = Constants::CONTEXT_DIMENSIONS.select { |d| @context.key?(d) || ctx.key?(d) }
  return true if relevant.empty?

  matches = relevant.count { |d| @context[d] == ctx[d] }
  matches.to_f / relevant.size >= 0.5
end

#mature?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb', line 68

def mature?
  @maturity == :habitual || @maturity == :automatic
end

#record_execution(success:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb', line 29

def record_execution(success:)
  @execution_count += 1
  @success_count   += 1 if success
  @last_executed    = Time.now.utc

  @strength = if success
                [@strength + Constants::REINFORCEMENT_RATE, 1.0].min
              else
                [@strength - Constants::REINFORCEMENT_RATE, 0.0].max
              end

  update_maturity
end

#similarity(other) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb', line 78

def similarity(other)
  set_a = @actions.uniq
  set_b = other.actions.uniq
  return 0.0 if set_a.empty? && set_b.empty?

  intersection = (set_a & set_b).size
  union        = (set_a | set_b).size
  return 0.0 if union.zero?

  intersection.to_f / union
end

#stale?(threshold = 3600) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb', line 72

def stale?(threshold = 3600)
  return true if @last_executed.nil?

  (Time.now.utc - @last_executed) > threshold
end

#success_rateObject



47
48
49
50
51
# File 'lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb', line 47

def success_rate
  return 0.0 if @execution_count.zero?

  @success_count.to_f / @execution_count
end

#to_hObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/legion/extensions/agentic/learning/habit/helpers/action_sequence.rb', line 90

def to_h
  {
    id:              @id,
    actions:         @actions,
    context:         @context,
    execution_count: @execution_count,
    success_count:   @success_count,
    strength:        @strength,
    maturity:        @maturity,
    cognitive_cost:  cognitive_cost,
    success_rate:    success_rate,
    last_executed:   @last_executed,
    created_at:      @created_at
  }
end