Module: RubynCode::Protocols::InterruptHandler

Defined in:
lib/rubyn_code/protocols/interrupt_handler.rb

Overview

Handles SIGINT (Ctrl-C) gracefully with a two-stage interrupt protocol.

First Ctrl-C sets the interrupted flag so the current LLM call can check and abort gracefully. A second Ctrl-C within 2 seconds forces an immediate exit.

Class Method Summary collapse

Class Method Details

.clear_callbacks!void

This method returns an undefined value.

Clears all registered callbacks. Intended for test cleanup.



62
63
64
# File 'lib/rubyn_code/protocols/interrupt_handler.rb', line 62

def clear_callbacks!
  @mutex.synchronize { @callbacks.clear }
end

.interrupted?Boolean

Returns whether the interrupted flag is currently set.

Returns:

  • (Boolean)


34
35
36
# File 'lib/rubyn_code/protocols/interrupt_handler.rb', line 34

def interrupted?
  @mutex.synchronize { @interrupted }
end

.on_interrupt { ... } ⇒ void

This method returns an undefined value.

Registers a callback to be invoked on the first interrupt. Callbacks are executed in registration order.

Yields:

  • the block to run on interrupt



53
54
55
56
57
# File 'lib/rubyn_code/protocols/interrupt_handler.rb', line 53

def on_interrupt(&block)
  @mutex.synchronize do
    @callbacks << block
  end
end

.reset!void

This method returns an undefined value.

Clears the interrupted flag and resets the last interrupt timestamp.



41
42
43
44
45
46
# File 'lib/rubyn_code/protocols/interrupt_handler.rb', line 41

def reset!
  @mutex.synchronize do
    @interrupted = false
    @last_interrupt_at = nil
  end
end

.setup!void

This method returns an undefined value.

Installs the SIGINT trap with the two-stage interrupt protocol.



20
21
22
23
24
25
26
27
28
29
# File 'lib/rubyn_code/protocols/interrupt_handler.rb', line 20

def setup!
  @mutex.synchronize do
    @interrupted = false
    @last_interrupt_at = nil
  end

  trap('INT') do
    handle_interrupt
  end
end