Module: Legion::Extensions::Llm::Inventory::ScopedRefresher

Defined in:
lib/legion/extensions/llm/inventory/scoped_refresher.rb

Overview

Mix into a Legion::Extensions::Llm::*::Actors::DiscoveryRefresh class. The host class must include Legion::Extensions::Helpers::Lex (auto-injects log / settings / handle_exception / cache_*) and define:

- #scope_key          — Hash like { provider: :vllm, instance: instance_id }
- #compute_lanes_for_scope — Array<Hash> lane fact-sheets (no health, no
                            lane_weight — added by Inventory.write_lane).
                            Each lane MUST set :id via compose_id.
- #credential_hash    — String identifying the auth credential for this scope
                        (used by the auth-failure cooldown circuit).

Constant Summary collapse

AUTH_COOLDOWN_TTL =

Auth-failure cooldown TTL (5 minutes). Operator can fix the credential and lanes auto-recover on the next tick after expiry.

300

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compose_id(lane_fields) ⇒ Object

G22: 5-part lane id composed here and ONLY here. All gem writers MUST call this helper; Inventory.write_lane rejects any lane with a missing or malformed :id. Accepts a Hash (or keyword splat) with keys: tier, provider_family, instance_id, type, model.



24
25
26
27
28
29
30
31
# File 'lib/legion/extensions/llm/inventory/scoped_refresher.rb', line 24

def self.compose_id(lane_fields)
  t  = lane_fields[:tier]
  pf = lane_fields[:provider_family]
  ii = lane_fields[:instance_id]
  ty = lane_fields[:type]
  mo = lane_fields[:model]
  "#{t}:#{pf}:#{ii}:#{ty}:#{mo}"
end

Instance Method Details

#tickObject

G7 write-then-delete-orphans: write new lanes FIRST (eliminates zero-results race window), then delete orphans from the previous scope snapshot.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/legion/extensions/llm/inventory/scoped_refresher.rb', line 35

def tick(**)
  return if auth_cooldown_active?

  new_lanes = safe_compute
  log.info("[llm][scoped_refresher] action=tick provider=#{scope_key[:provider]} lanes_computed=#{new_lanes ? new_lanes.size : 0}")
  return unless new_lanes&.any?

  written = 0
  new_lanes.each do |lane_fact|
    written += 1 if Legion::LLM::Inventory.write_lane(lane: lane_fact)
  end
  log.info("[llm][scoped_refresher] action=tick_complete provider=#{scope_key[:provider]} lanes_computed=#{new_lanes.size} lanes_written=#{written}")

  orphans = (@prev_scope_keys || []) - new_lanes.map { it[:id] }
  orphans.each { |id| Legion::LLM::Inventory.delete_lane(id: id) }

  @prev_scope_keys = new_lanes.map { it[:id] }
end