Module: Legion::DigitalWorker::Registry

Defined in:
lib/legion/digital_worker/registry.rb

Defined Under Namespace

Classes: InsufficientConsent, WorkerNotActive, WorkerNotFound

Constant Summary collapse

%w[supervised consult inform autonomous].freeze

Class Method Summary collapse

Class Method Details

.clear_local_workers!Object



19
20
21
# File 'lib/legion/digital_worker/registry.rb', line 19

def self.clear_local_workers!
  @local_workers_mutex.synchronize { @local_workers.clear }
end

Returns:

  • (Boolean)


53
54
55
# File 'lib/legion/digital_worker/registry.rb', line 53

def self.consent_sufficient?(current_tier, required_tier)
  CONSENT_HIERARCHY.index(current_tier) >= CONSENT_HIERARCHY.index(required_tier)
end

.local_worker_idsObject



15
16
17
# File 'lib/legion/digital_worker/registry.rb', line 15

def self.local_worker_ids
  @local_workers_mutex.synchronize { @local_workers.to_a }
end

.validate_execution!(worker_id:, required_consent: nil) ⇒ Object



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
# File 'lib/legion/digital_worker/registry.rb', line 23

def self.validate_execution!(worker_id:, required_consent: nil)
  Legion::Logging.debug "[Registry] validate_execution: worker_id=#{worker_id}" if defined?(Legion::Logging)
  worker = Legion::Data::Model::DigitalWorker.first(worker_id: worker_id)

  unless worker
    Legion::Logging.warn "[Registry] worker not found: #{worker_id}" if defined?(Legion::Logging)
    emit_blocked(worker_id: worker_id, reason: 'unregistered')
    raise WorkerNotFound, "no registered worker with id #{worker_id}"
  end

  unless worker.active?
    Legion::Logging.warn "[Registry] worker not active: #{worker_id} state=#{worker.lifecycle_state}" if defined?(Legion::Logging)
    emit_blocked(worker_id: worker_id, reason: "lifecycle_state=#{worker.lifecycle_state}")
    raise WorkerNotActive, "worker #{worker_id} is #{worker.lifecycle_state}, not active"
  end

  if required_consent && !consent_sufficient?(worker.consent_tier, required_consent)
    if defined?(Legion::Logging)
      Legion::Logging.warn "[Registry] insufficient consent: #{worker_id} tier=#{worker.consent_tier} required=#{required_consent}"
    end
    emit_blocked(worker_id: worker_id, reason: "consent=#{worker.consent_tier} < #{required_consent}")
    raise InsufficientConsent,
          "worker #{worker_id} consent tier #{worker.consent_tier} insufficient (needs #{required_consent})"
  end

  @local_workers_mutex.synchronize { @local_workers.add(worker_id) }
  Legion::Logging.info "[Registry] registered worker: #{worker_id}" if defined?(Legion::Logging)
  worker
end