Class: LittleGhost::CodeMode::Ruby::Session

Inherits:
CodeMode::Session
  • Object
show all
Defined in:
lib/little_ghost/code_mode/ruby/session.rb

Overview

:nodoc:

Constant Summary collapse

OBSERVATION_SECONDS =
60

Instance Method Summary collapse

Constructor Details

#initialize(broker:, sandbox_factory:, subprocess_policy:, limits:, observation_seconds: OBSERVATION_SECONDS) ⇒ Session

Returns a new instance of Session.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/little_ghost/code_mode/ruby/session.rb', line 15

def initialize(broker:, sandbox_factory:, subprocess_policy:, limits:, observation_seconds: OBSERVATION_SECONDS)
  @broker = broker
  @task_runner = broker.task_runner
  @sandbox_factory = sandbox_factory
  @subprocess_policy = subprocess_policy
  @limits = limits
  @observation_seconds = Float(observation_seconds)
  raise ArgumentError, "observation_seconds must be positive" unless @observation_seconds.positive?
  @session = nil
  @workspace = nil
  @sandbox = nil
  @output = +""
  @closed = false
  @close_complete = false
  @control_mutex = Mutex.new
  @programs = 0
  @call_mutex = Mutex.new
  @call_tasks = []
  @call_errors = []
  @tool_calls = 0
  @closing_marker = {closing: false}
  @lifecycle_mutex = Mutex.new
  @lifecycle_condition = ConditionVariable.new
  @generation = nil
  @expiring_generation = nil
  @pending_program_error = nil
  @watchdog = nil
  @watchdog_mutex = Mutex.new
  @watchdog_condition = ConditionVariable.new
  @watchdog_generation = nil
end

Instance Method Details

#closeObject



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
# File 'lib/little_ghost/code_mode/ruby/session.rb', line 61

def close
  acquired = false
  return if @close_complete

  @closed = true
  acquired = @control_mutex.try_lock
  unless acquired
    raise ToolError, "cannot close while another code-mode control operation is active"
  end
  cleanup_error = begin
    close_process
    nil
  rescue => error
    error
  end
  pending_error = begin
    raise_program_cleanup_error!
    nil
  rescue => error
    error
  end
  @close_complete = true unless cleanup_error || pending_error
  raise(cleanup_error || pending_error) if cleanup_error || pending_error
ensure
  @control_mutex.unlock if acquired
end

#execute(source:, catalog:, frame: nil, max_output_tokens: nil, context: nil) ⇒ Object



47
48
49
50
51
# File 'lib/little_ghost/code_mode/ruby/session.rb', line 47

def execute(source:, catalog:, frame: nil, max_output_tokens: nil, context: nil)
  with_control do
    execute_program(source:, catalog:, frame:, max_output_tokens:, context:)
  end
end

#stop(max_output_tokens: nil, context: nil) ⇒ Object



57
58
59
# File 'lib/little_ghost/code_mode/ruby/session.rb', line 57

def stop(max_output_tokens: nil, context: nil)
  with_control { stop_program(max_output_tokens:, context:) }
end

#wait(max_output_tokens: nil, context: nil) ⇒ Object



53
54
55
# File 'lib/little_ghost/code_mode/ruby/session.rb', line 53

def wait(max_output_tokens: nil, context: nil)
  with_control { wait_for_program(max_output_tokens:, context:) }
end