Documentation
¶
Overview ¶
Package orchestrator manages the Phase 2 orchestrator + worker lifecycle: it spawns Claude Code sessions with the hive-aware system prompt, runs the mailbox router on a tick, and registers agents in the hive. See .ai/05-orchestration.md and .ai/07-orchestrator.md.
Index ¶
- Constants
- func SystemPrompt(hiveHome string) string
- type Orchestrator
- func (o *Orchestrator) AgentIDForSession(sessionID string) string
- func (o *Orchestrator) SpawnWorker(ctx context.Context, workerID string) (string, error)
- func (o *Orchestrator) Start(ctx context.Context) (string, error)
- func (o *Orchestrator) StopAll() int
- func (o *Orchestrator) TaskForAgent(agentID string) string
- type Spawner
Constants ¶
const MaxConsecutiveWakes = 10
MaxConsecutiveWakes bounds how many times in a row the router will type a kick into one agent whose inbox never clears. Past it the agent is stalled: waking it again is a Claude turn every WakeThrottle with nothing to show for it.
const OrchestratorID = hive.OrchestratorAgentID
OrchestratorID is the fixed hive agent id for the orchestrator session.
Variables ¶
This section is empty.
Functions ¶
func SystemPrompt ¶
SystemPrompt returns the orchestrator system prompt with the hive home path appended, so the session knows where its inbox/outbox and the task board live.
Types ¶
type Orchestrator ¶
type Orchestrator struct {
Hive *hive.Hive
Store *store.Store
Spawner Spawner
Log *slog.Logger
// PromptFile is the path the orchestrator system prompt is written to for
// --append-system-prompt-file (defaults to <hive>/.orchestrator-prompt.md).
PromptFile string
// Cwd is the repo the orchestrator and workers operate on.
Cwd string
// RouterTick is how often mail is delivered (default 2s).
RouterTick time.Duration
// KickDelay is how long to wait for the freshly-spawned TUI to be ready for
// input before writing the kick message (default 2500ms).
KickDelay time.Duration
// Verify runs a task's done_criteria and moves it out of `verifying`
// (→ done | in_progress | needs_you). Injected by the daemon as a closure over
// board.VerifyTask so the orchestrator need not import board. nil ⇒
// verification is driven elsewhere (or disabled in tests).
Verify func(ctx context.Context, taskID string) error
// OverBudget moves a task that outspent its budget to needs_you and mirrors
// it, with the reason attached. Injected by the daemon as a closure over
// board.OverBudget for the same reason as Verify (the board owns the mirror
// and the bus, and importing it here would cycle). nil ⇒ the router does the
// hive-side move itself, without a mirror.
OverBudget func(ctx context.Context, taskID, reason string) error
// WakeThrottle bounds how often an idle session is re-kicked to process new
// mail (default 20s), so a still-working agent is not spammed every tick.
WakeThrottle time.Duration
// MaxWakes caps how many consecutive wakes one agent gets before it is
// declared stalled and escalated (0 ⇒ MaxConsecutiveWakes). The throttle
// bounds the rate; this bounds the total, so a message the agent never clears
// cannot cost one Claude turn every WakeThrottle forever.
MaxWakes int
// Signal stops an owned session (the same verbs as POST
// /v1/agents/{id}/signal). Injected so the router can actually halt a worker
// that has outspent its budget — rewriting the task file does not stop a
// process. nil ⇒ the router can only park the task.
Signal func(sessionID, action string) error
// Now returns the current time (overridable in tests).
Now func() time.Time
// BaseCtx is the daemon-lifetime context the router and kick goroutines run
// under. It must NOT be the per-request context passed to Start — that is
// cancelled the instant the HTTP handler returns, which would kill the router
// immediately. The daemon sets this to its run context. If nil, Start falls
// back to a non-cancellable copy of the Start context.
BaseCtx context.Context
// contains filtered or unexported fields
}
Orchestrator owns the orchestrator session and the worker fleet, and runs the mailbox router on a tick.
func (*Orchestrator) AgentIDForSession ¶
func (o *Orchestrator) AgentIDForSession(sessionID string) string
AgentIDForSession maps a Caprock session id back to its hive agent id (for the Stop-loop decision, which needs the agent whose inbox to check).
func (*Orchestrator) SpawnWorker ¶
SpawnWorker registers a worker in the hive and spawns its session. Idempotent per worker id: if already running, returns the existing session.
func (*Orchestrator) Start ¶
func (o *Orchestrator) Start(ctx context.Context) (string, error)
Start registers the orchestrator agent, spawns its session with the hive-aware system prompt, and begins the router loop. Returns the session id.
func (*Orchestrator) StopAll ¶ added in v0.15.0
func (o *Orchestrator) StopAll() int
StopAll kills the orchestrator session and every worker session it spawned, in one call, and latches the router so it does not immediately respawn them from the still-assigned tasks. It is the emergency stop: an unattended fleet running with --dangerously-skip-permissions previously had no single control that halted it, only per-agent signals a user had to know the session ids for. Returns the number of sessions stopped.
Task files are left exactly as they are — the point is to stop processes, not to rewrite the board. Starting the orchestrator again clears the latch and resumes from the files.
func (*Orchestrator) TaskForAgent ¶
func (o *Orchestrator) TaskForAgent(agentID string) string
TaskForAgent returns the id of the live (non-terminal) task assigned to an agent, or "" if none. It arms the Stop-hook forced-continue guard, which is keyed per (session, task). The orchestrator itself is never assigned a task.
type Spawner ¶
type Spawner interface {
Spawn(ctx context.Context, req agents.SpawnRequest) (*agents.Agent, error)
Get(sessionID string) (*agents.Agent, bool)
ClaudeAvailable() bool
// Input writes typed bytes into an owned session's PTY (the kick message).
Input(sessionID string, data []byte) error
}
Spawner is the subset of agents.Manager the orchestrator needs.