Class: Rubino::Tools::ShellTailTool

Inherits:
Base
  • Object
show all
Defined in:
lib/rubino/tools/shell_tail_tool.rb

Overview

Blocking variant of shell_output: waits up to ‘timeout` seconds for new bytes to arrive on a background shell, then returns them. Returns immediately if bytes are already buffered or if the process has exited.

Lets an agent “follow” a long-running job (CI, build, watcher) without busy-polling shell_output. The polling itself is implemented internally as a short-interval loop on ShellRegistry.read_new — switching to a condition variable would shave ~50ms of jitter and is a refactor for later; for v1, 100ms polling under the agent’s tool-call latency is invisible.

Constant Summary collapse

DEFAULT_TIMEOUT =
30
MAX_TIMEOUT =
300
POLL_INTERVAL =
0.1

Instance Attribute Summary

Attributes inherited from Base

#cancel_token, #read_tracker, #stream_chunk

Instance Method Summary collapse

Methods inherited from Base

#cancellation_requested?, #config_key, #emit_chunk, #risky?, #to_tool_definition, workspace_root, workspace_roots

Instance Method Details

#call(arguments) ⇒ Object



48
49
50
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
88
89
90
91
92
93
94
95
# File 'lib/rubino/tools/shell_tail_tool.rb', line 48

def call(arguments)
  run_id  = arguments["run_id"] || arguments[:run_id]
  timeout = (arguments["timeout"] || arguments[:timeout] || DEFAULT_TIMEOUT).to_i
  timeout = timeout.clamp(1, MAX_TIMEOUT)

  return "Error: run_id is required" if run_id.nil? || run_id.to_s.empty?

  registry = ShellRegistry.instance
  entry    = registry.find(run_id)
  return "Error: no background shell with run_id=#{run_id}" unless entry

  body     = ""
  deadline = Time.now + timeout

  loop do
    body = registry.read_new(entry)
    break unless body.empty?

    # Process has exited and no bytes left to drain — return now with
    # whatever the final status says.
    break if registry.status(entry) != :running

    # User pressed Ctrl+C during a tail. Don't keep blocking — return
    # an empty body with a "cancelled" hint so the model can react.
    if cancellation_requested?
      return { output: tail_header(run_id, registry, entry, body, cancelled: true),
               error_code: :cancelled }
    end

    break if Time.now >= deadline

    sleep POLL_INTERVAL
  end

  status    = registry.status(entry)
  exit_code = registry.exit_code(entry)
  registry.remove(run_id) unless status == :running

  text = if body.empty?
           tail_header(run_id, registry, entry, body)
         else
           "#{tail_header(run_id, registry, entry, body)}\n#{body}"
         end
  { output: text,
    metrics: "#{body.bytesize}B · #{status}",
    exit_code: exit_code,
    error_code: tail_error_code(status, exit_code) }
end

#descriptionObject



24
25
26
27
28
29
30
# File 'lib/rubino/tools/shell_tail_tool.rb', line 24

def description
  "Follow a background shell — block until new stdout/stderr bytes " \
    "arrive on its run_id, the process exits, or `timeout` seconds " \
    "elapse. Default timeout #{DEFAULT_TIMEOUT}s (max #{MAX_TIMEOUT}s). " \
    "Returns the new bytes plus a status header. Use for `tail -F`-style " \
    "following; use shell_output for a one-shot read."
end

#input_schemaObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubino/tools/shell_tail_tool.rb', line 32

def input_schema
  {
    type: "object",
    properties: {
      run_id: { type: "string", description: "run_id from shell run_in_background:true" },
      timeout: { type: "integer",
                 description: "Max seconds to block (default #{DEFAULT_TIMEOUT}, max #{MAX_TIMEOUT})" }
    },
    required: %w[run_id]
  }
end

#nameObject



20
21
22
# File 'lib/rubino/tools/shell_tail_tool.rb', line 20

def name
  "shell_tail"
end

#risk_levelObject



44
45
46
# File 'lib/rubino/tools/shell_tail_tool.rb', line 44

def risk_level
  :low
end