Class: Lich::Common::ScriptExecutionGuard

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(callback, allow_script_starts: true) ⇒ ScriptExecutionGuard

Create a policy whose first denial remains latched until disposal.

Parameters:

  • callback (Proc) —

    observation-only policy accepting a command or nil

  • allow_script_starts (Boolean) (defaults to: true) —

    whether guarded workers may launch scripts

Raises:

  • (ArgumentError) —

    if callback is not a Proc



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.

Parameters:

  • reason (String, Symbol) (defaults to: :cancelled) —

    identifier; unsupported values normalize to :cancelled without retaining their contents

Returns:

  • (self) —

    this guard with cancellation latched



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.

Returns:

  • (Boolean) —

    whether any cancellation reason is latched



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.

Returns:

  • (true) —

    when this guard does not restrict script starts

Raises:



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.

Returns:

  • (Boolean) —

    true only while this thread is evaluating the policy



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.

Parameters:

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

    exact wire command, or nil for a checkpoint

Returns:

  • (true) —

    when the policy permits continuation

Raises:

  • (Interrupted) —

    on cancellation, invalid input, callback failure, recursive command dispatch, or a result other than literal true



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.

Returns:

  • (self) —

    this guard with :closed latched unless already cancelled



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.

Returns:

  • (Interrupted, nil) —

    the existing cancellation, if any



129
130
131
# File 'documented/common/script_execution_guard.rb', line 129

def interruption
  @mutex.synchronize { @reason && Interrupted.new(@reason) }
end