Module: Lich::Common::FrontendSettings

Extended by:
SharingViolationRetry
Defined in:
documented/common/frontend_settings.rb

Overview

Owns user-configurable frontend definitions and built-in launch overrides.

The persisted schema deliberately lives outside entry.yaml so frontend configuration never enters the credential lifecycle. Callers replace the complete configuration; validation, atomic persistence, catalog cleanup, and immutable process state stay behind this module's small interface.

Defined Under Namespace

Classes: UnsupportedVersionError

Constant Summary collapse

FILE_NAME =
'frontends.yml'
SCHEMA_VERSION =
1
CUSTOM_ID_PATTERN =

Regexp that validates custom frontend identifiers.

Identifiers must start with a lowercase letter or digit, followed by zero to sixty-three characters of lowercase letters, digits, underscores, or hyphens. Total length is limited to 64 characters.

Examples:

"my-frontend" =~ CUSTOM_ID_PATTERN #=> 0
"My-Frontend" =~ CUSTOM_ID_PATTERN #=> nil
"_invalid" =~ CUSTOM_ID_PATTERN #=> nil

See Also:

/\A[a-z0-9][a-z0-9_-]{0,63}\z/
MAX_SCALAR_BYTES =
4096
MAX_ARGUMENTS =
256
MUTEX =

Synchronizes access to the current frontend configuration.

Protects @current and its mutations from concurrent reads and writes, ensuring that load!, replace!, current, and settings_for operations are atomic.

Mutex.new
SHARING_VIOLATION_RETRY_DELAYS =

Bounded backoff for publishing or removing the settings file while Windows reports a sharing violation. Shared with ActiveSessions.

SharingViolationRetry::DELAYS
EMPTY_CONFIGURATION =

Default, pristine frontend configuration with no customizations.

Contains an empty set of built-in overrides and custom definitions, frozen to prevent accidental mutation. Used as the initial state and when invalid or missing settings files are encountered.

Lich::Util.deep_freeze(
  {
    'version'  => SCHEMA_VERSION,
    'builtins' => {},
    'custom'   => {}
  }
)

Constants included from SharingViolationRetry

SharingViolationRetry::DELAYS

Class Method Summary collapse

Class Method Details

.current ⇒ Hash

Returns a detached snapshot so callers cannot mutate applied settings.

Returns:



117
118
119
# File 'documented/common/frontend_settings.rb', line 117

def current
  MUTEX.synchronize { deep_copy(@current) }
end

.load!(data_dir: nil) ⇒ Hash

Loads, validates, and applies frontends.yml from a Lich data directory. Missing, unreadable, or malformed files become an empty configuration and restore the pristine built-in catalog. A document from a newer schema is left untouched and the last known-good catalog remains active. Invalid argument lists likewise retain the previous catalog and are logged.

Parameters:

  • data_dir (String, nil) (defaults to: nil) —

    Lich data directory; defaults to DATA_DIR

Returns:

  • (Hash) —

    normalized configuration snapshot



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'documented/common/frontend_settings.rb', line 75

def load!(data_dir: nil)
  directory = resolve_data_dir(data_dir)
  MUTEX.synchronize do
    path = File.join(directory, FILE_NAME)
    raw = read_file(path)
    if unsupported_version?(raw)
      warn_unsupported_file(raw)
      return deep_copy(@current)
    end

    apply_configuration(normalize_configuration(raw))
  rescue ArgumentError => error
    log_warning("could not apply #{FILE_NAME}: #{error.message}")
    deep_copy(@current)
  end
end

.replace!(data_dir: nil, builtins: {}, custom: {}) ⇒ Hash

Validates, atomically persists, and applies a complete configuration. Replacing the whole document makes deletion explicit and prevents stale custom definitions from accumulating across settings reloads.

Parameters:

  • data_dir (String, nil) (defaults to: nil) —

    Lich data directory; defaults to DATA_DIR

  • builtins (Hash) (defaults to: {}) —

    built-in executable/argument overrides

  • custom (Hash) (defaults to: {}) —

    custom frontend definitions keyed by stable id

Returns:

  • (Hash) —

    normalized configuration snapshot



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'documented/common/frontend_settings.rb', line 100

def replace!(data_dir: nil, builtins: {}, custom: {})
  directory = resolve_data_dir(data_dir)
  MUTEX.synchronize do
    path = File.join(directory, FILE_NAME)
    refuse_unsupported_overwrite!(path)
    configuration = normalize_configuration(
      'version'  => SCHEMA_VERSION,
      'builtins' => builtins,
      'custom'   => custom
    )
    write_file(path, configuration)
    apply_configuration(configuration)
  end
end

.settings_for(frontend_id) ⇒ Hash?

Returns persisted settings for a built-in or custom frontend identifier. Aliases such as wrayth resolve to their stable built-in id.

Parameters:

  • frontend_id (String, Symbol)

Returns:



126
127
128
129
130
131
132
133
# File 'documented/common/frontend_settings.rb', line 126

def settings_for(frontend_id)
  frontend_id = Frontend.canonical_name(frontend_id)
  MUTEX.synchronize do
    settings = @current.fetch('builtins').fetch(frontend_id, nil) ||
               @current.fetch('custom').fetch(frontend_id, nil)
    settings && deep_copy(settings)
  end
end

.validate_arguments(value) ⇒ Array<String>

Validates literal argv values without trimming or dropping positions.

Parameters:

  • value (Array<String>) —

    complete additional argument list

Returns:

  • (Array<String>) —

    detached, unchanged argument values

Raises:

  • (ArgumentError) —

    when the list exceeds bounds or contains invalid values



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'documented/common/frontend_settings.rb', line 140

def validate_arguments(value)
  unless value.is_a?(Array) && value.length <= MAX_ARGUMENTS
    raise ArgumentError, "Additional arguments must be an array of at most #{MAX_ARGUMENTS} strings."
  end
  unless value.all? { |argument|
    argument.is_a?(String) && argument.valid_encoding? &&
    argument.bytesize <= MAX_SCALAR_BYTES && !argument.match?(/[\x00-\x1f\x7f]/)
  }
    raise ArgumentError, 'Arguments must be bounded strings without control characters.'
  end

  value.map(&:dup)
end