Class: Mistri::Spawner

Inherits:
Object
  • Object
show all
Defined in:
lib/mistri/spawner.rb

Overview

Host policy for spawning workers, as one object: the tool pool children may draw from, the curated types, the model allowlist, the headcount cap, and the dispatcher that makes background mode real. A host runtime factory reconstructs live dependencies after the dispatch boundary; model text never chooses or proves resource isolation. #tool builds the spawn_agent tool the top-level agent holds; SubAgent.spawner and SubAgent.pack are the front doors.

Every policy violation answers the model in band (unknown type, missing instructions, over capacity); only host configuration mistakes raise, and they raise at construction.

Instance Method Summary collapse

Constructor Details

#initialize(provider:, tools: [], types: {}, models: [], max_children: 4, dispatcher: nil, runtime_factory: nil, needs_approval: false, **agent_options) ⇒ Spawner

Returns a new instance of Spawner.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mistri/spawner.rb', line 16

def initialize(provider:, tools: [], types: {}, models: [], max_children: 4,
               dispatcher: nil, runtime_factory: nil, needs_approval: false,
               **agent_options)
  tools = Array.new(tools) if tools.is_a?(Array)
  models = Array.new(models) if models.is_a?(Array)
  ChildAgentOptions.validate!(agent_options)
  validate_configuration!(tools, models, dispatcher, runtime_factory)

  @provider = provider
  @pool = tools.freeze
  # Symbol keys are natural Ruby; the wire speaks strings. One
  # normalization here and lookup, schema, and menu all agree.
  @types = types.transform_keys(&:to_s).freeze
  @models = models.map { |model| String.new(model).freeze }.freeze
  @max_children = max_children
  @dispatcher = dispatcher
  @runtime_factory = runtime_factory
  @needs_approval = needs_approval
  @agent_options = agent_options
  validate_types!
end

Instance Method Details

#spawn(args, context) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mistri/spawner.rb', line 46

def spawn(args, context)
  crowded = over_capacity(context.session)
  return crowded if crowded

  worker = resolve_worker(args)
  return worker if worker.is_a?(String)

  return dispatch(args, worker, context) if args["mode"] == "background" && @dispatcher

  SubAgent.run_child(label: label_for(args), provider: provider_for(worker),
                     system: worker[:system], tools: worker[:tools],
                     task: args.fetch("task"), parent_context: context,
                     agent_options: @agent_options)
end

#toolObject



38
39
40
41
42
43
44
# File 'lib/mistri/spawner.rb', line 38

def tool
  spawner = self
  Tool.define("spawn_agent", description, needs_approval: @needs_approval,
                                          schema: schema) do |args, context|
    spawner.spawn(args, context)
  end
end