Module: Lich::InternalAPI::ActiveSessions Private
- Extended by:
- Common::SharingViolationRetry
- Defined in:
- documented/internal_api/active_sessions.rb,
documented/internal_api/active_sessions/client.rb,
documented/internal_api/active_sessions/server.rb,
documented/internal_api/active_sessions/registry.rb,
documented/internal_api/active_sessions/lifecycle.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Namespace for active-sessions registry and lifecycle coordination.
Defined Under Namespace
Modules: Lifecycle Classes: Client, Registry, Server
Constant Summary collapse
- FEATURE_FLAG =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Feature flag name that enables the active sessions API.
:active_sessions_api- DEFAULT_HOST =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Loopback host used by the local TCP service.
'127.0.0.1'- EPHEMERAL_PORT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Port passed to the transport to request an OS-assigned ephemeral port. The actual bound port is read back after Server#start and published in the discovery file.
0- DISCOVERY_FILENAME =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Filename used to publish the current owner metadata for local clients.
'lich-active-sessions.json'- LOCK_FILENAME =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Filename of the advisory lock used to elect a single service owner across processes.
'lich-active-sessions.lock'- DISCOVERY_FILESYSTEM_RETRY_DELAYS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Backoff used when Windows temporarily denies rename or delete because another process still has the discovery file open. The ownership flock remains held throughout these bounded retries, so no successor can validly publish a competing generation in the meantime.
Lich::Common::SharingViolationRetry::DELAYS
Constants included from Common::SharingViolationRetry
Common::SharingViolationRetry::DELAYS
Class Method Summary collapse
-
.cleanup_discovery_if_last_session! ⇒ void
private
Removes the discovery file and stops the service when the current process still owns the service and the shared registry is now empty.
-
.enabled? ⇒ Boolean
private
Indicates whether the active sessions API is enabled.
-
.ensure_service! ⇒ Boolean
private
Starts the local service if no healthy service is already responding.
-
.query_snapshot ⇒ Hash
private
Queries the currently discovered active-sessions service without consulting the local feature flag state or attempting to bootstrap a new owner.
-
.register_session(payload) ⇒ Boolean
private
Registers or updates a session record in the local service.
-
.service_info ⇒ Hash
private
Returns sanitized metadata about the current active-sessions service.
-
.snapshot ⇒ Hash
private
Returns the current active sessions snapshot.
-
.stop_service! ⇒ void
private
Stops the in-process server if this process owns one, releasing the ownership lock and clearing the published discovery record.
-
.unregister_session(pid:) ⇒ Boolean
private
Removes a session record by pid.
Class Method Details
.cleanup_discovery_if_last_session! ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Removes the discovery file and stops the service when the current process still owns the service and the shared registry is now empty.
608 609 610 611 612 613 614 615 616 617 618 619 620 |
# File 'documented/internal_api/active_sessions.rb', line 608 def self.cleanup_discovery_if_last_session! discovery = load_discovery return unless discovery[:owner_pid].to_i == Process.pid current_snapshot = query_snapshot return if current_snapshot[:error] return unless current_snapshot[:source] == 'ActiveSessionsAPI' return unless current_snapshot[:total].to_i.zero? stop_service! rescue StandardError nil end |
.enabled? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Indicates whether the active sessions API is enabled.
An explicit --active-session-dir=PATH CLI flag implies opt-in for this process only -- passing a coordination directory is a clear enough signal of intent that requiring the persisted feature_flag: active_sessions_api row too would just be friction. This never writes to lich_settings; removing the flag on a later launch reverts to whatever the persisted flag says (false by default).
Until feature-flag plumbing is present in core, this API otherwise remains safely dormant and returns empty snapshots.
101 102 103 104 105 106 107 108 109 |
# File 'documented/internal_api/active_sessions.rb', line 101 def self.enabled? return true if defined?(ACTIVE_SESSION_DIR) && !ACTIVE_SESSION_DIR.to_s.empty? return false unless defined?(Lich::Common::FeatureFlags) Lich::Common::FeatureFlags.enabled?(FEATURE_FLAG) rescue StandardError => e Lich.log("warning: ActiveSessions feature flag check failed: #{e.class}: #{e.}") if Lich.respond_to?(:log) false end |
.ensure_service! ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Starts the local service if no healthy service is already responding.
The first process to win the ownership lock becomes the in-process server owner. All later callers reuse the same endpoint through the client adapter.
118 119 120 121 122 |
# File 'documented/internal_api/active_sessions.rb', line 118 def self.ensure_service! return false unless enabled? ensure_service_internal!(allow_bootstrap: true) end |
.query_snapshot ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Queries the currently discovered active-sessions service without consulting the local feature flag state or attempting to bootstrap a new owner.
This is intended for read-only operator tools and CLI queries that may run outside a normal Lich session process. If no service is already available, the result is a normalized fallback payload.
237 238 239 240 241 242 243 244 245 |
# File 'documented/internal_api/active_sessions.rb', line 237 def self.query_snapshot response = service_client&.snapshot return fallback_snapshot(error: 'active sessions service unavailable') unless response return fallback_snapshot(error: response[:error]) unless response[:ok] response[:payload] rescue StandardError => e fallback_snapshot(error: e.) end |
.register_session(payload) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Registers or updates a session record in the local service.
166 167 168 169 170 171 |
# File 'documented/internal_api/active_sessions.rb', line 166 def self.register_session(payload) return false unless enabled? return false unless ensure_service! service_client&.upsert(payload)&.fetch(:ok, false) || false end |
.service_info ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns sanitized metadata about the current active-sessions service.
The public shape intentionally omits the shared auth token while still exposing enough information for diagnostics and operator visibility.
253 254 255 256 257 258 259 260 261 262 |
# File 'documented/internal_api/active_sessions.rb', line 253 def self.service_info discovery = load_discovery { source: 'ActiveSessionsAPI', owner_pid: discovery[:owner_pid], port: discovery[:port], updated_at: discovery[:updated_at], service_available: service_available? }.compact end |
.snapshot ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the current active sessions snapshot.
216 217 218 219 220 221 222 223 224 225 226 |
# File 'documented/internal_api/active_sessions.rb', line 216 def self.snapshot return fallback_snapshot unless enabled? return fallback_snapshot unless ensure_service! response = service_client&.snapshot || fallback_snapshot(error: 'active sessions service unavailable') return fallback_snapshot(error: response[:error]) unless response[:ok] response[:payload] rescue StandardError => e fallback_snapshot(error: e.) end |
.stop_service! ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Stops the in-process server if this process owns one, releasing the ownership lock and clearing the published discovery record.
This is intended for explicit service shutdown paths, not ordinary lifecycle teardown for every session consumer.
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'documented/internal_api/active_sessions.rb', line 271 def self.stop_service! @mutex.synchronize do @server&.stop @server = nil @registry = nil # Keep the native flock through the ownership check and unlink. A # successor can publish as soon as we release it; checking the pid # before an unlocked unlink does not protect that new publication. delete_discovery_if_owned if own_lock? release_ownership_lock end @service_client_mutex.synchronize do @service_client = nil @service_client_token = nil @service_client_port = nil end end |
.unregister_session(pid:) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Removes a session record by pid.
194 195 196 197 198 199 |
# File 'documented/internal_api/active_sessions.rb', line 194 def self.unregister_session(pid:) return false unless enabled? return false unless ensure_service! service_client&.remove(pid)&.fetch(:ok, false) || false end |