Class: SwarmSDK::V3::SubTaskAgent

Inherits:
Agent
  • Object
show all
Defined in:
lib/swarm_sdk/v3/sub_task_agent.rb

Overview

Ephemeral agent for focused subtask execution

SubTaskAgent is a lightweight copy of an Agent that shares the parent’s memory store in read-only mode. It inherits all of the parent’s capabilities (MCP connections, skills, tools) but skips memory writes, STM capture, ingestion, and eviction.

## Design

Rather than adding flags to Agent, SubTaskAgent overrides the specific lifecycle methods that differ. This keeps Agent unchanged (zero regression risk) and encapsulates all subtask behavior in one class.

## What’s inherited unchanged from Agent

  • Agent#connect_mcp_servers — full MCP connections

  • Agent#load_skills — same skill directories

  • #create_chat / #configure_chat — fresh chat, same model/params

  • Agent#build_base_system_prompt — same system prompt + skills metadata

  • Agent#ask / Agent#execute_turn — same turn flow (memory writes are no-ops)

  • Agent#interrupt! — interruption safety preserved

  • Agent#clear — proper MCP cleanup

## What’s overridden

  • #initialize_memory — uses parent’s memory store instead of creating one

  • #capture_turn — no-op (subtask is ephemeral)

  • #ingest_into_memory — no-op (read-only memory)

  • #evict_stm — no-op (no STM to evict)

  • #memory_read_only? — returns true (skips access counter updates)

  • #attach_tools — passes subtask_depth to Registry

Examples:

subtask = SubTaskAgent.new(
  definition,
  parent_memory_store: agent.memory,
  subtask_depth: 1,
)
response = subtask.ask("Analyze the auth module")
subtask.clear  # disconnects MCP

Instance Attribute Summary collapse

Attributes inherited from Agent

#definition, #id, #loaded_skills

Instance Method Summary collapse

Methods inherited from Agent

#ask, #clear, #clear_steering_queue, #defrag!, #initialized?, #interrupt!, #loop, #memory, #messages, #name, #running?, #steer, #tokens

Constructor Details

#initialize(definition, parent_memory_store:, subtask_depth:) ⇒ SubTaskAgent

Create a new subtask agent

Parameters:

  • definition (AgentDefinition)

    Agent configuration (same as parent)

  • parent_memory_store (Memory::Store, nil)

    Parent’s memory store (read-only)

  • subtask_depth (Integer)

    Current nesting depth



54
55
56
57
58
# File 'lib/swarm_sdk/v3/sub_task_agent.rb', line 54

def initialize(definition, parent_memory_store:, subtask_depth:)
  super(definition)
  @parent_memory_store = parent_memory_store
  @subtask_depth = subtask_depth
end

Instance Attribute Details

#subtask_depthInteger (readonly)

Returns Current subtask nesting depth.

Returns:

  • (Integer)

    Current subtask nesting depth



47
48
49
# File 'lib/swarm_sdk/v3/sub_task_agent.rb', line 47

def subtask_depth
  @subtask_depth
end

Instance Method Details

#memory_read_only?Boolean

Whether memory operations are read-only

Returns:

  • (Boolean)

    Always true — subtasks don’t update access counters



70
71
72
# File 'lib/swarm_sdk/v3/sub_task_agent.rb', line 70

def memory_read_only?
  true
end

#resolved_subtask_configHash

Resolve subtask model configuration with fallback chain

Resolution order:

  1. Agent-level subtask config (definition.subtask_*)

  2. Global config subtask config (Configuration.subtask_*)

  3. Parent’s model config (fallback)

Examples:

Check what model a subtask will use

subtask_agent.resolved_subtask_config[:model]
# => "claude-haiku-4"

Returns:

  • (Hash)

    Configuration hash with :model, :provider, :base_url, :headers, :parameters



86
87
88
# File 'lib/swarm_sdk/v3/sub_task_agent.rb', line 86

def resolved_subtask_config
  @resolved_subtask_config ||= build_resolved_subtask_config
end

#subtask_mode?Boolean

Whether this agent is running as a subtask

Returns:

  • (Boolean)

    Always true for SubTaskAgent



63
64
65
# File 'lib/swarm_sdk/v3/sub_task_agent.rb', line 63

def subtask_mode?
  true
end