Class: Legion::Extensions::Mesh::Helpers::Delegation
- Inherits:
-
Object
- Object
- Legion::Extensions::Mesh::Helpers::Delegation
- Defined in:
- lib/legion/extensions/mesh/helpers/delegation.rb
Constant Summary collapse
- CONSENT_LEVELS =
%i[read execute admin].freeze
Instance Attribute Summary collapse
-
#delegations ⇒ Object
readonly
Returns the value of attribute delegations.
Instance Method Summary collapse
- #chain(delegation_id) ⇒ Object
- #complete(delegation_id) ⇒ Object
- #create(from:, to:, task_context:, consent_level:, parent_delegation_id: nil) ⇒ Object
- #find(delegation_id) ⇒ Object
- #for_agent(agent_id, status: nil) ⇒ Object
-
#initialize(max_depth: 3, max_active_per_agent: 10) ⇒ Delegation
constructor
A new instance of Delegation.
- #revoke(delegation_id) ⇒ Object
- #stats ⇒ Object
Constructor Details
#initialize(max_depth: 3, max_active_per_agent: 10) ⇒ Delegation
Returns a new instance of Delegation.
14 15 16 17 18 19 |
# File 'lib/legion/extensions/mesh/helpers/delegation.rb', line 14 def initialize(max_depth: 3, max_active_per_agent: 10) @delegations = {} @agent_delegations = Hash.new { |h, k| h[k] = [] } @max_depth = max_depth @max_active_per_agent = max_active_per_agent end |
Instance Attribute Details
#delegations ⇒ Object (readonly)
Returns the value of attribute delegations.
12 13 14 |
# File 'lib/legion/extensions/mesh/helpers/delegation.rb', line 12 def delegations @delegations end |
Instance Method Details
#chain(delegation_id) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/legion/extensions/mesh/helpers/delegation.rb', line 73 def chain(delegation_id) result = [] current = @delegations[delegation_id] while current result.unshift(current) current = current[:parent_delegation_id] ? @delegations[current[:parent_delegation_id]] : nil end result end |
#complete(delegation_id) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/legion/extensions/mesh/helpers/delegation.rb', line 54 def complete(delegation_id) record = @delegations[delegation_id] return nil unless record && record[:status] == :active record[:status] = :completed record[:completed_at] = Time.now.utc record end |
#create(from:, to:, task_context:, consent_level:, parent_delegation_id: nil) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/legion/extensions/mesh/helpers/delegation.rb', line 21 def create(from:, to:, task_context:, consent_level:, parent_delegation_id: nil) depth = compute_depth(parent_delegation_id) return { error: :max_depth_exceeded } if depth >= @max_depth if parent_delegation_id parent = find(parent_delegation_id) return { error: :consent_escalation } if parent && CONSENT_LEVELS.index() > CONSENT_LEVELS.index(parent[:consent_level]) end active_count = (@agent_delegations[from] || []).count do |id| @delegations[id]&.fetch(:status, nil) == :active end return { error: :max_active_exceeded } if active_count >= @max_active_per_agent id = "del-#{SecureRandom.uuid}" record = { delegation_id: id, from_agent_id: from, to_agent_id: to, task_context: task_context, consent_level: , parent_delegation_id: parent_delegation_id, depth: depth, status: :active, created_at: Time.now.utc, completed_at: nil } @delegations[id] = record @agent_delegations[from] << id @agent_delegations[to] << id record end |
#find(delegation_id) ⇒ Object
90 91 92 |
# File 'lib/legion/extensions/mesh/helpers/delegation.rb', line 90 def find(delegation_id) @delegations[delegation_id] end |
#for_agent(agent_id, status: nil) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/legion/extensions/mesh/helpers/delegation.rb', line 83 def for_agent(agent_id, status: nil) ids = @agent_delegations[agent_id] || [] results = ids.filter_map { |id| @delegations[id] } results = results.select { |d| d[:status] == status } if status results end |
#revoke(delegation_id) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/legion/extensions/mesh/helpers/delegation.rb', line 63 def revoke(delegation_id) record = @delegations[delegation_id] return nil unless record && record[:status] == :active record[:status] = :revoked record[:completed_at] = Time.now.utc cascade_revoke(delegation_id) record end |
#stats ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/legion/extensions/mesh/helpers/delegation.rb', line 94 def stats active = @delegations.values.count { |d| d[:status] == :active } depths = @delegations.values.map { |d| d[:depth] } { total: @delegations.size, active: active, avg_depth: depths.empty? ? 0 : depths.sum.to_f / depths.size, max_depth: depths.max || 0 } end |