Class: Tools::SpawnSubagent

Inherits:
Base
  • Object
show all
Includes:
SubagentPrompts
Defined in:
lib/tools/spawn_subagent.rb

Overview

Spawns a generic child session that works on a task autonomously. The sub-agent inherits the parent’s viewport context at fork time, runs via AgentRequestJob, and delivers results back through ReturnResult.

For named specialists with predefined prompts and tools, see SpawnSpecialist.

Constant Summary collapse

GENERIC_PROMPT =
"You are a focused sub-agent. #{RETURN_INSTRUCTION}\n"

Constants included from SubagentPrompts

Tools::SubagentPrompts::EXPECTED_DELIVERABLE_PREFIX, Tools::SubagentPrompts::RETURN_INSTRUCTION

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

schema

Constructor Details

#initialize(session:) ⇒ SpawnSubagent

Returns a new instance of SpawnSubagent.

Parameters:

  • session (Session)

    the parent session spawning the sub-agent



48
49
50
# File 'lib/tools/spawn_subagent.rb', line 48

def initialize(session:, **)
  @session = session
end

Class Method Details

.descriptionObject



17
18
19
20
21
# File 'lib/tools/spawn_subagent.rb', line 17

def self.description
  "Spawn a generic sub-agent to work on a task autonomously. " \
    "The sub-agent inherits your conversation context, works independently, " \
    "and returns results as a tool response when done."
end

.input_schemaObject



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/tools/spawn_subagent.rb', line 23

def self.input_schema
  {
    type: "object",
    properties: {
      task: {
        type: "string",
        description: "What the sub-agent should do (emitted as its first user message)"
      },
      expected_output: {
        type: "string",
        description: "Description of the expected deliverable"
      },
      tools: {
        type: "array",
        items: {type: "string"},
        description: "Tool names to grant the sub-agent. " \
          "Omit for all standard tools. Empty array for pure reasoning (return_result only). " \
          "Valid tools: #{AgentLoop::STANDARD_TOOLS_BY_NAME.keys.join(", ")}"
      }
    },
    required: %w[task expected_output]
  }
end

.tool_nameObject



15
# File 'lib/tools/spawn_subagent.rb', line 15

def self.tool_name = "spawn_subagent"

Instance Method Details

#execute(input) ⇒ String, Hash{Symbol => String}

Creates a child session, emits the task as a user message, and queues background processing. Returns immediately (non-blocking).

Parameters:

  • input (Hash<String, Object>)

    with “task”, “expected_output”, and optional “tools”

Returns:

  • (String)

    confirmation with child session ID

  • (Hash{Symbol => String})

    with :error key on validation failure



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tools/spawn_subagent.rb', line 58

def execute(input)
  task = input["task"].to_s.strip
  expected_output = input["expected_output"].to_s.strip

  return {error: "Task cannot be blank"} if task.empty?
  return {error: "Expected output cannot be blank"} if expected_output.empty?

  tools = normalize_tools(input["tools"])

  error = validate_tools(tools)
  return error if error

  child = spawn_child(task, expected_output, tools)
  "Sub-agent spawned (session #{child.id}). Result will arrive as a tool response."
end