Class: Lich::DependencyRecovery
- Inherits:
-
Object
- Object
- Lich::DependencyRecovery
- Defined in:
- documented/dependency_recovery.rb
Overview
Restores approved runtime gems from the Ruby4Lich5 manifest when an installed runtime is incomplete or a native extension cannot load. The manifest is deliberately an allow-list: a gem name is never turned into a URL, shell command, or RubyGems request by this class.
A manifest is a release asset controlled by the same human-reviewed promotion as the gem bundle. Its SHA-256 values protect against accidental corruption and mismatched assets; they are not a substitute for signing, because the manifest and assets currently share a publisher.
Defined Under Namespace
Constant Summary collapse
- DEFAULT_MANIFEST_URL =
Default HTTPS location of the approved gem manifest for this Lich5 release.
Points to a GitHub release asset for the Ruby4Lich5 gem bundle on Windows (x64-mingw-ucrt platform). The manifest is a JSON document with schema version 1, containing approved gem units keyed by Ruby ABI and platform.
'https://github.com/elanthia-online/Ruby4Lich5/releases/download/' \ 'R4L5-gem-bundle-x64-mingw-ucrt/R4L5-gem-manifest.json'
- LOCK_FILENAME =
'.lich-dependency-recovery.lock'- HTTP_OPEN_TIMEOUT_SECONDS =
10- HTTP_READ_TIMEOUT_SECONDS =
60- NATIVE_LOAD_TIMEOUT_SECONDS =
60- SHA256_PATTERN =
Regexp that matches a validated SHA-256 digest in manifest format.
Ensures the prefix
sha256:followed by exactly 64 lowercase hexadecimal digits representing a 256-bit hash. Used to validate artifact and package digests from the manifest before any download or installation attempt. /\Asha256:[0-9a-f]{64}\z/- SAFE_FILENAME =
Regexp that matches filenames safe to extract or write within the runtime.
Allows alphanumerics, hyphens, dots, and underscores, but rejects dot-only names (like
.or..) and paths containing backslashes or forward slashes. Protects against manifest-controlled path traversal during ZIP extraction and gem installation. /\A(?!\.+\z)[^\\\/]+\z/- SAFE_NAME =
Regexp that matches gem names and unit identifiers safe to process.
Permits only ASCII alphanumerics, underscores, hyphens, and dots. Rejects control characters, slashes, and special characters to prevent gem names from being interpreted as shell metacharacters or path components during recovery.
/\A[a-zA-Z0-9_.-]+\z/
Class Method Summary collapse
- .log_helper_failure(payload_path, error) ⇒ void
-
.run_windows_replacement(payload_path) ⇒ Boolean
Runs in a detached Ruby process after the original Lich process exits.
Instance Method Summary collapse
-
#initialize(manifest_url: ENV.fetch('LICH_GEM_MANIFEST_URL', DEFAULT_MANIFEST_URL), gem_home: Gem.dir, temp_dir: (TEMP_DIR) ? TEMP_DIR : Dir.tmpdir), http_get: nil, install_gem: nil, extract_zip: nil, powershell_runner: nil, helper_launcher: nil) ⇒ DependencyRecovery
constructor
A new instance of DependencyRecovery.
-
#recover(gem_names, force: false, plan: nil) ⇒ Result
Downloads, verifies, and installs a previously approved manifest plan.
-
#recovery_plan(gem_names) ⇒ Plan
Loads and validates the manifest units covering
gem_names, without downloading gem artifacts.
Constructor Details
#initialize(manifest_url: ENV.fetch('LICH_GEM_MANIFEST_URL', DEFAULT_MANIFEST_URL), gem_home: Gem.dir, temp_dir: (TEMP_DIR) ? TEMP_DIR : Dir.tmpdir), http_get: nil, install_gem: nil, extract_zip: nil, powershell_runner: nil, helper_launcher: nil) ⇒ DependencyRecovery
Returns a new instance of DependencyRecovery.
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'documented/dependency_recovery.rb', line 115 def initialize(manifest_url: ENV.fetch('LICH_GEM_MANIFEST_URL', DEFAULT_MANIFEST_URL), gem_home: Gem.dir, temp_dir: (defined?(TEMP_DIR) ? TEMP_DIR : Dir.tmpdir), http_get: nil, install_gem: nil, extract_zip: nil, powershell_runner: nil, helper_launcher: nil) @manifest_url = manifest_url @gem_home = gem_home @temp_dir = temp_dir @http_get = http_get @install_gem = install_gem || method(:install_gem_file) @extract_zip = extract_zip || method(:extract_zip_file) @powershell_runner = powershell_runner || Open3.method(:capture3) @helper_launcher = helper_launcher || method(:launch_windows_helper) end |
Class Method Details
.log_helper_failure(payload_path, error) ⇒ void
This method returns an undefined value.
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'documented/dependency_recovery.rb', line 206 def self.log_helper_failure(payload_path, error) payload = JSON.parse(File.read(payload_path)) rescue {} log_path = File.join(payload.fetch('temp_dir', Dir.tmpdir), 'lich5-missing-gems.log') File.open(log_path, 'a') do |file| file.puts "[#{Time.now}] Lich5 native gem replacement failure" file.puts " #{error.class}: #{error.}" file.puts end return unless Gem.win_platform? require 'win32ole' WIN32OLE.new('WScript.Shell').Popup( "Lich could not update the GTK runtime. See #{log_path} for details.", 0, 'Lich5: Ruby Gem Recovery', 16 ) rescue StandardError nil end |
.run_windows_replacement(payload_path) ⇒ Boolean
Runs in a detached Ruby process after the original Lich process exits. The payload contains only already-hash-verified package paths and the original Lich invocation; artifacts are re-verified before replacement.
193 194 195 196 197 198 199 200 201 |
# File 'documented/dependency_recovery.rb', line 193 def self.run_windows_replacement(payload_path) payload = JSON.parse(File.read(payload_path)) recovery = new(gem_home: payload.fetch('gem_home'), temp_dir: payload.fetch('temp_dir')) recovery.send(:run_windows_replacement!, payload) true rescue StandardError => e log_helper_failure(payload_path, e) false end |
Instance Method Details
#recover(gem_names, force: false, plan: nil) ⇒ Result
Downloads, verifies, and installs a previously approved manifest plan.
Installation always targets Gem.dir (or the injected runtime directory),
never a user-controlled GEM_HOME.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'documented/dependency_recovery.rb', line 153 def recover(gem_names, force: false, plan: nil) planned = plan || recovery_plan(gem_names) return Result.new(installed_gems: [], error: planned.error) unless planned.success? result = with_recovery_workspace do |work_dir| artifact_cache = {} # No runtime lock or Gem.dir write occurs until every requested unit is # downloaded, extracted, and hash-verified. An unreachable release # endpoint therefore fails closed without changing the runtime. staged_units = planned.units.map { |unit| stage_unit(unit, work_dir, artifact_cache) } with_install_lock do replacement, direct = staged_units.partition { |staged| native_runtime_unit?(staged.fetch(:unit)) } installed = direct.flat_map { |staged| install_staged_unit(staged, force: force) } if replacement.empty? Result.new(installed_gems: installed) elsif replacement.length == 1 schedule_runtime_replacement(replacement.first, work_dir) Result.new(installed_gems: installed, restart_required: true) else raise Error, 'multiple native runtime replacement units are not supported in one recovery' end end end refresh_rubygems! unless result.restart_required result rescue Error, JSON::ParserError, OpenURI::HTTPError, SocketError, SystemCallError => e Result.new(installed_gems: [], error: e.) rescue StandardError => e # Recovery runs during boot. Keep an unexpected implementation failure # from turning into an unhelpful backtrace before GemCheck can report it. Result.new(installed_gems: [], error: "#{e.class}: #{e.}") end |
#recovery_plan(gem_names) ⇒ Plan
Loads and validates the manifest units covering gem_names, without
downloading gem artifacts. Callers use this to request consent first.
134 135 136 137 138 139 140 141 142 143 |
# File 'documented/dependency_recovery.rb', line 134 def recovery_plan(gem_names) requested = Array(gem_names).map(&:to_s).reject(&:empty?).uniq return Plan.new(units: []) if requested.empty? Plan.new(units: units_for(load_manifest, requested)) rescue Error, JSON::ParserError, OpenURI::HTTPError, SocketError, SystemCallError => e Plan.new(units: [], error: e.) rescue StandardError => e Plan.new(units: [], error: "#{e.class}: #{e.}") end |