Class: Phronomy::CancellationToken
- Inherits:
-
Object
- Object
- Phronomy::CancellationToken
- Defined in:
- lib/phronomy/cancellation_token.rb
Overview
Provides cooperative cancellation for agent invocations.
Pass a token to an agent via +config: { cancellation_token: token }+. The agent checks the token before each LLM call and raises CancellationError when the token is cancelled or the optional deadline has passed.
A token may be shared across multiple agent invocations and across threads; all access to internal state is protected by a Mutex.
Instance Attribute Summary collapse
-
#deadline ⇒ Time?
readonly
The wall-clock deadline passed to #initialize, or +nil+.
Class Method Summary collapse
-
.timeout_after(seconds) ⇒ CancellationToken
Returns a new token that will expire after +seconds+ seconds, measured with the monotonic clock (+Process::CLOCK_MONOTONIC+).
Instance Method Summary collapse
-
#cancel! ⇒ self
Mark the token as cancelled.
-
#cancelled? ⇒ Boolean
Returns +true+ when the token has been explicitly cancelled via #cancel!, when the wall-clock deadline has passed, or when the monotonic deadline (set by CancellationToken.timeout_after) has elapsed.
-
#initialize(deadline: nil, monotonic_deadline: nil) ⇒ CancellationToken
constructor
A new instance of CancellationToken.
-
#raise_if_cancelled!(message = "invocation cancelled") ⇒ nil
Raises CancellationError if the token is cancelled.
Constructor Details
#initialize(deadline: nil, monotonic_deadline: nil) ⇒ CancellationToken
Returns a new instance of CancellationToken.
50 51 52 53 54 55 |
# File 'lib/phronomy/cancellation_token.rb', line 50 def initialize(deadline: nil, monotonic_deadline: nil) @cancelled = false @deadline = deadline @monotonic_deadline = monotonic_deadline @mutex = Mutex.new end |
Instance Attribute Details
#deadline ⇒ Time? (readonly)
Returns the wall-clock deadline passed to #initialize, or +nil+.
58 59 60 |
# File 'lib/phronomy/cancellation_token.rb', line 58 def deadline @deadline end |
Class Method Details
.timeout_after(seconds) ⇒ CancellationToken
Returns a new token that will expire after +seconds+ seconds, measured with the monotonic clock (+Process::CLOCK_MONOTONIC+). Unlike constructing a token with +deadline: Time.now + seconds+, this factory is immune to NTP adjustments and DST transitions.
39 40 41 42 |
# File 'lib/phronomy/cancellation_token.rb', line 39 def self.timeout_after(seconds) monotonic_deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + seconds new(monotonic_deadline: monotonic_deadline) end |
Instance Method Details
#cancel! ⇒ self
Mark the token as cancelled. Thread-safe; may be called from any thread.
63 64 65 66 |
# File 'lib/phronomy/cancellation_token.rb', line 63 def cancel! @mutex.synchronize { @cancelled = true } self end |
#cancelled? ⇒ Boolean
Returns +true+ when the token has been explicitly cancelled via #cancel!, when the wall-clock deadline has passed, or when the monotonic deadline (set by timeout_after) has elapsed. Thread-safe.
73 74 75 76 77 78 |
# File 'lib/phronomy/cancellation_token.rb', line 73 def cancelled? return true if @mutex.synchronize { @cancelled } return true if !@deadline.nil? && Time.now >= @deadline !@monotonic_deadline.nil? && Process.clock_gettime(Process::CLOCK_MONOTONIC) >= @monotonic_deadline end |
#raise_if_cancelled!(message = "invocation cancelled") ⇒ nil
Raises Phronomy::CancellationError if the token is cancelled. A convenience method for cooperative cancellation checks inside tools, RAG loaders, and hooks, replacing the +if cancelled? then raise+ pattern.
88 89 90 |
# File 'lib/phronomy/cancellation_token.rb', line 88 def raise_if_cancelled!( = "invocation cancelled") raise Phronomy::CancellationError, if cancelled? end |