Class: Legion::Extensions::Agentic::Defense::Extinction::Helpers::ProtocolState

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/defense/extinction/helpers/protocol_state.rb

Constant Summary collapse

MAX_HISTORY =
500

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProtocolState

Returns a new instance of ProtocolState.



16
17
18
19
20
21
# File 'lib/legion/extensions/agentic/defense/extinction/helpers/protocol_state.rb', line 16

def initialize
  @current_level = 0 # 0 = normal operation
  @active = false
  @history = []
  load_from_local
end

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



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

def active
  @active
end

#current_levelObject (readonly)

Returns the value of attribute current_level.



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

def current_level
  @current_level
end

#historyObject (readonly)

Returns the value of attribute history.



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

def history
  @history
end

Instance Method Details

#deescalate(target_level, authority:, reason:) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/legion/extensions/agentic/defense/extinction/helpers/protocol_state.rb', line 39

def deescalate(target_level, authority:, reason:)
  return :not_active unless @active
  return :invalid_target if target_level >= @current_level
  return :irreversible unless Levels.reversible?(@current_level)
  return :insufficient_authority unless authority == Levels.required_authority(@current_level)

  @current_level = target_level
  @active = target_level.positive?
  @history << {
    action: :deescalate, level: target_level, authority: authority,
    reason: reason, at: Time.now.utc
  }
  trim_history
  save_to_local
  :deescalated
end

#escalate(level, authority:, reason:) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/legion/extensions/agentic/defense/extinction/helpers/protocol_state.rb', line 23

def escalate(level, authority:, reason:)
  return :invalid_level unless Levels.valid_level?(level)
  return :already_at_or_above if level <= @current_level
  return :insufficient_authority unless authority == Levels.required_authority(level)

  @current_level = level
  @active = true
  @history << {
    action: :escalate, level: level, authority: authority,
    reason: reason, at: Time.now.utc
  }
  trim_history
  save_to_local
  :escalated
end

#save_to_localObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/legion/extensions/agentic/defense/extinction/helpers/protocol_state.rb', line 65

def save_to_local
  return unless defined?(Legion::Data::Local) && local_data_connected?

  row = {
    id:            1,
    current_level: @current_level,
    active:        @active,
    history:       ::JSON.dump(@history.map { |h| h.merge(at: h[:at].to_s) }),
    updated_at:    Time.now.utc
  }
  db = local_data_connection
  if db[:extinction_state].where(id: 1).any?
    db[:extinction_state].where(id: 1).update(row.except(:id))
  else
    db[:extinction_state].insert(row)
  end
rescue StandardError => _e
  nil
end

#to_hObject



56
57
58
59
60
61
62
63
# File 'lib/legion/extensions/agentic/defense/extinction/helpers/protocol_state.rb', line 56

def to_h
  {
    current_level: @current_level,
    active:        @active,
    level_info:    @current_level.positive? ? Levels.level_info(@current_level) : nil,
    history_size:  @history.size
  }
end