Module: Legion::Extensions::Agentic::Social::Consent::Runners::Consent

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

Instance Method Summary collapse

Instance Method Details

#apply_tier_change(domain:, new_tier:) ⇒ Object



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

def apply_tier_change(domain:, new_tier:, **)
  return { error: :invalid_tier, valid_tiers: Helpers::Tiers::TIERS } unless Helpers::Tiers.valid_tier?(new_tier)

  old_tier = consent_map.get_tier(domain)
  consent_map.set_tier(domain, new_tier)
  changed = old_tier != new_tier
  log.info "[consent] tier applied: domain=#{domain} old=#{old_tier} new=#{new_tier} changed=#{changed}"
  { domain: domain, old_tier: old_tier, new_tier: new_tier, changed: changed }
end

#approve_promotion(domain:, approved_by:) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/legion/extensions/agentic/social/consent/runners/consent.rb', line 118

def approve_promotion(domain:, approved_by:, **)
  map = consent_map
  return { approved: false, error: 'no_pending_approval' } unless map.pending?(domain)

  pending_tier = map.domains[domain][:pending_tier]
  old_tier = map.get_tier(domain)
  map.clear_pending(domain)
  map.set_tier(domain, pending_tier)

  if defined?(Legion::Events)
    Legion::Events.emit('consent.promotion_approved', {
                          domain: domain, old_tier: old_tier, new_tier: pending_tier,
                          approved_by: approved_by, at: Time.now.utc
                        })
  end

  { approved: true, domain: domain, old_tier: old_tier, new_tier: pending_tier, approved_by: approved_by }
end


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

def check_consent(domain:, _action_type: :general, **)
  tier = consent_map.get_tier(domain)
  log.debug "[consent] check: domain=#{domain} tier=#{tier} allowed=#{tier == :autonomous}"

  {
    domain:        domain,
    tier:          tier,
    allowed:       tier == :autonomous,
    needs_notify:  tier == :act_notify,
    needs_consult: tier == :consult,
    human_only:    tier == :human_only
  }
end


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/legion/extensions/agentic/social/consent/runners/consent.rb', line 200

def consent_status(domain: nil, **)
  if domain
    entry = consent_map.domains[domain]
    log.debug "[consent] status: domain=#{domain} tier=#{entry[:tier]} total=#{entry[:total_actions]}"
    {
      domain:       domain,
      tier:         entry[:tier],
      success_rate: consent_map.success_rate(domain),
      total:        entry[:total_actions],
      eligible:     consent_map.eligible_for_change?(domain)
    }
  else
    log.debug "[consent] status: domains=#{consent_map.domain_count}"
    { domains: consent_map.to_h, count: consent_map.domain_count }
  end
end

#evaluate_all_tiersObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/legion/extensions/agentic/social/consent/runners/consent.rb', line 76

def evaluate_all_tiers(**)
  promotions = []
  demotions = []

  consent_map.domains.each_key do |domain|
    result = consent_map.evaluate_promotion(domain)
    promotions << domain if result == :promote
    demotions << domain if result == :demote
  end

  evaluated = consent_map.domain_count
  log.debug "[consent] tier evaluation sweep: domains=#{evaluated} " \
            "promotions=#{promotions.size} demotions=#{demotions.size}"

  { evaluated: evaluated, promotions: promotions, demotions: demotions }
end

#evaluate_and_apply_tiersObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/legion/extensions/agentic/social/consent/runners/consent.rb', line 167

def evaluate_and_apply_tiers(**)
  candidates = evaluate_all_tiers
  applied_promotions = 0
  applied_demotions = 0
  approval_requests = 0

  Array(candidates[:promotions]).each do |domain|
    current = consent_map.get_tier(domain)
    proposed = Helpers::Tiers.promote(current)

    if proposed == :autonomous
      result = request_autonomous_approval(domain: domain)
      approval_requests += 1 if result[:requested]
    else
      apply_tier_change(domain: domain, new_tier: proposed)
      applied_promotions += 1
    end
  end

  Array(candidates[:demotions]).each do |domain|
    current = consent_map.get_tier(domain)
    proposed = Helpers::Tiers.demote(current)
    apply_tier_change(domain: domain, new_tier: proposed)
    applied_demotions += 1
  end

  expired = expire_pending_approvals

  { evaluated: candidates[:evaluated], applied_promotions: applied_promotions,
    applied_demotions: applied_demotions, approval_requests: approval_requests,
    expired_approvals: expired[:expired] }
end

#evaluate_tier_change(domain:) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/legion/extensions/agentic/social/consent/runners/consent.rb', line 41

def evaluate_tier_change(domain:, **)
  recommendation = consent_map.evaluate_promotion(domain)
  current = consent_map.get_tier(domain)

  result = {
    domain:         domain,
    current_tier:   current,
    recommendation: recommendation,
    success_rate:   consent_map.success_rate(domain)
  }

  case recommendation
  when :promote
    result[:proposed_tier] = Helpers::Tiers.promote(current)
    log.info "[consent] tier change: domain=#{domain} recommend=promote from=#{current} to=#{result[:proposed_tier]}"
  when :demote
    result[:proposed_tier] = Helpers::Tiers.demote(current)
    log.warn "[consent] tier change: domain=#{domain} recommend=demote from=#{current} to=#{result[:proposed_tier]}"
  else
    log.debug "[consent] tier eval: domain=#{domain} current=#{current} recommendation=#{recommendation}"
  end

  result
end

#expire_pending_approvals(timeout: Helpers::ConsentMap::APPROVAL_TIMEOUT) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/legion/extensions/agentic/social/consent/runners/consent.rb', line 152

def expire_pending_approvals(timeout: Helpers::ConsentMap::APPROVAL_TIMEOUT, **)
  map = consent_map
  expired = 0

  map.domains.each_key do |domain|
    next unless map.pending?(domain)
    next unless map.pending_expired?(domain, timeout: timeout)

    map.clear_pending(domain)
    expired += 1
  end

  { expired: expired }
end

#record_action(domain:, success:) ⇒ Object



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

def record_action(domain:, success:, **)
  consent_map.record_outcome(domain, success: success)
  rate = consent_map.success_rate(domain)
  total = consent_map.domains[domain][:total_actions]
  log.info "[consent] action recorded: domain=#{domain} success=#{success} rate=#{rate.round(2)} total=#{total}"

  {
    domain:       domain,
    success:      success,
    success_rate: rate,
    total:        total
  }
end

#reject_promotion(domain:, rejected_by:, reason: nil) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/legion/extensions/agentic/social/consent/runners/consent.rb', line 137

def reject_promotion(domain:, rejected_by:, reason: nil, **)
  map = consent_map
  return { rejected: false, error: 'no_pending_approval' } unless map.pending?(domain)

  map.clear_pending(domain)

  if defined?(Legion::Events)
    Legion::Events.emit('consent.promotion_rejected', {
                          domain: domain, rejected_by: rejected_by, reason: reason, at: Time.now.utc
                        })
  end

  { rejected: true, domain: domain, rejected_by: rejected_by, reason: reason }
end

#request_autonomous_approval(domain:) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/legion/extensions/agentic/social/consent/runners/consent.rb', line 93

def request_autonomous_approval(domain:, **)
  map = consent_map
  current_tier = map.get_tier(domain)

  return { requested: false, error: 'already_autonomous' } if current_tier == :autonomous
  return { requested: false, error: 'already_pending' } if map.pending?(domain)

  map.set_pending(domain, proposed_tier: :autonomous, requested_by: 'tier_evaluation')

  if defined?(Legion::Events)
    rate = map.success_rate(domain)
    total = map.domains[domain][:total_actions]
    Legion::Events.emit('consent.approval_required', {
                          domain:        domain,
                          current_tier:  current_tier,
                          proposed_tier: :autonomous,
                          success_rate:  rate,
                          total_actions: total,
                          requested_at:  Time.now.utc
                        })
  end

  { requested: true, domain: domain, current_tier: current_tier, proposed_tier: :autonomous }
end