Class: Legion::Extensions::Agentic::Defense::Friction::Helpers::StateTransition

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/defense/friction/helpers/state_transition.rb

Constant Summary

Constants included from Constants

Constants::DEFAULT_FRICTION, Constants::FRICTION_BOOST, Constants::FRICTION_DECAY, Constants::FRICTION_LABELS, Constants::INERTIA_THRESHOLD, Constants::MAX_TRANSITIONS, Constants::MOMENTUM_THRESHOLD, Constants::STATE_TYPES, Constants::TRANSITION_OUTCOMES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from_state:, to_state:, friction: DEFAULT_FRICTION) ⇒ StateTransition

Returns a new instance of StateTransition.



17
18
19
20
21
22
23
24
25
# File 'lib/legion/extensions/agentic/defense/friction/helpers/state_transition.rb', line 17

def initialize(from_state:, to_state:, friction: DEFAULT_FRICTION)
  @id            = SecureRandom.uuid
  @from_state    = from_state.to_sym
  @to_state      = to_state.to_sym
  @friction      = friction.to_f.clamp(0.0, 1.0)
  @outcome       = nil
  @force_applied = 0.0
  @created_at    = Time.now.utc
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



14
15
16
# File 'lib/legion/extensions/agentic/defense/friction/helpers/state_transition.rb', line 14

def created_at
  @created_at
end

#force_appliedObject (readonly)

Returns the value of attribute force_applied.



14
15
16
# File 'lib/legion/extensions/agentic/defense/friction/helpers/state_transition.rb', line 14

def force_applied
  @force_applied
end

#frictionObject (readonly)

Returns the value of attribute friction.



14
15
16
# File 'lib/legion/extensions/agentic/defense/friction/helpers/state_transition.rb', line 14

def friction
  @friction
end

#from_stateObject (readonly)

Returns the value of attribute from_state.



14
15
16
# File 'lib/legion/extensions/agentic/defense/friction/helpers/state_transition.rb', line 14

def from_state
  @from_state
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/legion/extensions/agentic/defense/friction/helpers/state_transition.rb', line 14

def id
  @id
end

#outcomeObject (readonly)

Returns the value of attribute outcome.



14
15
16
# File 'lib/legion/extensions/agentic/defense/friction/helpers/state_transition.rb', line 14

def outcome
  @outcome
end

#to_stateObject (readonly)

Returns the value of attribute to_state.



14
15
16
# File 'lib/legion/extensions/agentic/defense/friction/helpers/state_transition.rb', line 14

def to_state
  @to_state
end

Instance Method Details

#attempt!(force: 0.5) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/legion/extensions/agentic/defense/friction/helpers/state_transition.rb', line 27

def attempt!(force: 0.5)
  @force_applied = force.to_f.clamp(0.0, 1.0)
  @outcome = if @force_applied > @friction
               :completed
             elsif @force_applied > (@friction * 0.7)
               :deferred
             else
               :resisted
             end
  self
end

#completed?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/legion/extensions/agentic/defense/friction/helpers/state_transition.rb', line 45

def completed?
  %i[completed forced].include?(@outcome)
end

#force!(force: 1.0) ⇒ Object



39
40
41
42
43
# File 'lib/legion/extensions/agentic/defense/friction/helpers/state_transition.rb', line 39

def force!(force: 1.0)
  @force_applied = force.to_f.clamp(0.0, 1.0)
  @outcome = :forced
  self
end

#friction_labelObject



49
50
51
52
# File 'lib/legion/extensions/agentic/defense/friction/helpers/state_transition.rb', line 49

def friction_label
  match = FRICTION_LABELS.find { |range, _| range.cover?(@friction) }
  match ? match.last : :locked
end

#to_hObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/legion/extensions/agentic/defense/friction/helpers/state_transition.rb', line 54

def to_h
  {
    id:             @id,
    from_state:     @from_state,
    to_state:       @to_state,
    friction:       @friction,
    friction_label: friction_label,
    outcome:        @outcome,
    force_applied:  @force_applied,
    completed:      completed?,
    created_at:     @created_at
  }
end