Class: Lich::Common::ScriptExecutionGuard
- Inherits:
-
Object
- Object
- Lich::Common::ScriptExecutionGuard
- Defined in:
- documented/common/script_execution_guard.rb
Overview
Optional cooperative execution policy for one script. This is not a sandbox: the caller owns installing checkpoints and the guarded send.
Defined Under Namespace
Classes: Interrupted
Instance Method Summary collapse
-
#cancel!(reason = :cancelled) ⇒ self
The first cancellation wins across all threads using this guard.
-
#cancelled? ⇒ Boolean
Check whether continuation has been permanently denied for this guard.
-
#check_script_start! ⇒ true
Reject an explicitly prohibited native script start before startup admission.
-
#checking? ⇒ Boolean
Identify callback reentry without treating it as a new policy check.
-
#checkpoint!(command: nil) ⇒ true
A nil command is a cooperative checkpoint; a String is an impending send.
-
#close! ⇒ self
Permanently close this guard using the standard lifecycle reason.
-
#initialize(callback, allow_script_starts: true) ⇒ ScriptExecutionGuard
constructor
Create a policy whose first denial remains latched until disposal.
-
#interruption ⇒ Interrupted?
Return the sanitized interruption already latched by this guard.
Constructor Details
#initialize(callback, allow_script_starts: true) ⇒ ScriptExecutionGuard
Create a policy whose first denial remains latched until disposal.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'documented/common/script_execution_guard.rb', line 31 def initialize(callback, allow_script_starts: true) raise ArgumentError, 'execution guard requires a Proc' unless callback.is_a?(Proc) unless allow_script_starts.equal?(true) || allow_script_starts.equal?(false) raise ArgumentError, 'allow_script_starts must be true or false' end @callback = callback @allow_script_starts = allow_script_starts @mutex = Mutex.new @reason = nil @callbacks = {} end |
Instance Method Details
#cancel!(reason = :cancelled) ⇒ self
The first cancellation wins across all threads using this guard. Reasons are short identifiers, never raw commands or exception text.
94 95 96 97 98 99 |
# File 'documented/common/script_execution_guard.rb', line 94 def cancel!(reason = :cancelled) label = reason.to_s if reason.is_a?(String) || reason.is_a?(Symbol) normalized = label && label.match?(/\A[a-z][a-z0-9_]{0,63}\z/) ? label.to_sym : :cancelled @mutex.synchronize { @reason ||= normalized } self end |
#cancelled? ⇒ Boolean
Check whether continuation has been permanently denied for this guard.
121 122 123 |
# File 'documented/common/script_execution_guard.rb', line 121 def cancelled? @mutex.synchronize { !@reason.nil? } end |
#check_script_start! ⇒ true
Reject an explicitly prohibited native script start before startup admission. The denial also cancels future sends and cleanup attempts. Ordinary guards leave script startup behavior unchanged.
112 113 114 115 116 117 |
# File 'documented/common/script_execution_guard.rb', line 112 def check_script_start! return true if @allow_script_starts cancel!(:script_start_rejected) check_cancelled! end |
#checking? ⇒ Boolean
Identify callback reentry without treating it as a new policy check.
135 136 137 |
# File 'documented/common/script_execution_guard.rb', line 135 def checking? @mutex.synchronize { @callbacks.key?(Thread.current) } end |
#checkpoint!(command: nil) ⇒ true
A nil command is a cooperative checkpoint; a String is an impending send. Only literal true permits continuation. Callbacks may inspect Script.current (which checks again), but may not send recursively.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'documented/common/script_execution_guard.rb', line 51 def checkpoint!(command: nil) unless command.nil? || command.is_a?(String) cancel!(:invalid_command) check_cancelled! end thread = Thread.current entered = @mutex.synchronize do interrupt! if @reason if @callbacks.key?(thread) if command.nil? false else @reason = :reentrant_command interrupt! end else @callbacks[thread] = true true end end return true unless entered accepted = false begin accepted = @callback.call(command&.dup&.freeze) rescue Exception # rubocop:disable Lint/RescueException # Callback failures must latch, including script-defined exceptions; # never expose their message, backtrace, cause, or command payload. cancel!(:callback_error) ensure @mutex.synchronize { @callbacks.delete(thread) } cancel!(command.nil? ? :checkpoint_rejected : :command_rejected) unless accepted.equal?(true) end check_cancelled! true end |
#close! ⇒ self
Permanently close this guard using the standard lifecycle reason.
103 104 105 |
# File 'documented/common/script_execution_guard.rb', line 103 def close! cancel!(:closed) end |
#interruption ⇒ Interrupted?
Return the sanitized interruption already latched by this guard. This observes state only: it never invokes the policy callback and returns nil while the guard still permits execution.
129 130 131 |
# File 'documented/common/script_execution_guard.rb', line 129 def interruption @mutex.synchronize { @reason && Interrupted.new(@reason) } end |