Module: Legion::LLM::Router::SettingsState

Extended by:
Legion::Logging::Helper
Defined in:
lib/legion/llm/router/settings_state.rb

Overview

Atomically-managed singleton that holds the current validated SettingsSnapshot generation. A single on_reload callback updates it without stalling request threads.

Class Method Summary collapse

Class Method Details

.currentObject

Return the currently installed SettingsSnapshot. Callers must capture this once per logical request and not read it again mid-flight. Lazily installs generation 1 if boot has not run (e.g. isolated unit specs), so a snapshot is always available.



48
49
50
51
52
53
54
# File 'lib/legion/llm/router/settings_state.rb', line 48

def current
  snap = ref.get
  return snap unless snap.nil?

  install!
  ref.get
end

.install!Object

Build generation 1 from the current Legion::Settings trees and register exactly one on_reload callback. Safe to call multiple times; only the first call installs the callback (tracked by @callback_registered).



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/legion/llm/router/settings_state.rb', line 24

def install!
  snap = Legion::LLM::Router::SettingsSnapshot.build(
    generation:         1,
    llm_settings:       Legion::Settings[:llm],
    extension_settings: Legion::Settings[:extensions]
  )
  ref.set(snap)
  log.debug('[llm][settings_state] action=install generation=1')

  return if @callback_registered

  Legion::Settings.on_reload do
    reload!(
      llm_settings:       Legion::Settings[:llm],
      extension_settings: Legion::Settings[:extensions]
    )
  end
  @callback_registered = true
end

.reload!(llm_settings:, extension_settings:) ⇒ Object

Build the next snapshot atomically. Increments generation only on success. If validation raises, logs at :warn, returns false, and retains the prior generation unchanged.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/legion/llm/router/settings_state.rb', line 59

def reload!(llm_settings:, extension_settings:)
  prior = ref.get
  next_gen = (prior&.generation || 0) + 1

  next_snap = Legion::LLM::Router::SettingsSnapshot.build(
    generation:         next_gen,
    llm_settings:       llm_settings,
    extension_settings: extension_settings
  )
  ref.set(next_snap)
  log.debug("[llm][settings_state] action=reload generation=#{next_gen}")
  true
rescue ArgumentError => e
  # SettingsSnapshot.build raises ArgumentError on invalid operator
  # configuration. Handle only that validation signal, retain the prior
  # generation, and return false. Any other StandardError is a
  # programming error and must propagate (never swallowed here).
  handle_exception(e, level: :warn, operation: 'llm.router.settings.reload')
  false
end

.reset!Object

RSpec-only. Clears the current snapshot and callback registration so specs start from a clean state.



82
83
84
85
# File 'lib/legion/llm/router/settings_state.rb', line 82

def reset!
  @ref = nil
  @callback_registered = false
end