Class: LLM::Function::Fork::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/function/fork/job.rb

Overview

The Job class represents a single fork-backed function call inside the child process.

It is executed in the forked process and is responsible for running the resolved tool instance, handling control messages such as interrupts, and writing the final result back to the parent process.

Instance Method Summary collapse

Constructor Details

#initialize(function, ch) ⇒ LLM::Function::Fork::Job

Parameters:



16
17
18
19
# File 'lib/llm/function/fork/job.rb', line 16

def initialize(function, ch)
  @function = function
  @ch = ch
end

Instance Method Details

#call

This method returns an undefined value.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/llm/function/fork/job.rb', line 23

def call
  runner = @function.runner
  ##
  # Before the watcher exists, because an interrupt can arrive first:
  # it is a datagram and it waits in the channel until the watcher
  # reads it.
  @window = LLM::Function::Window.new
  controller = setup(runner)
  ##
  # And everything the call needs is prepared outside the window, so
  # the distance from `running!` to the tool's first instruction is
  # the method dispatch and nothing else.
  kwargs = arguments_for(@function)
  @window.running!
  result = call!(runner, kwargs)
  ##
  # The window closes the moment the tool has returned and before the
  # result is written, so an interrupt that arrives once the work is
  # done is a no-op rather than a reason to throw the result away.
  @window.finished!
  @ch.result.write([:result, result, finished_at])
rescue LLM::Interrupt
  @ch.result.write([:interrupt, nil, finished_at])
rescue => ex
  @ch.result.write([:result, error(ex), finished_at])
ensure
  ##
  # For the paths above that did not reach the line, and nil-safe for
  # a raise before the window existed.
  @window&.finished!
  controller&.kill
  [@ch.control, @ch.result].each { _1.close unless _1.closed? }
end