Coverage for app / services / swarms / topologies / supervisor.py: 93%
27 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
1"""A lead and its workers — the orchestrator-and-workers shape.
3The lead plans the work as pieces with a brief each, workers do the pieces
4in parallel, the lead reviews them all at once against their briefs —
5sending back, once, any piece that misses its brief, and asking for another
6round of work where a gap matters — and writes the answer from the pieces as
7accepted. The shape behind the best-measured gains in the literature on
8research-like, parallelisable tasks, and the one whose reviewing hub
9contains errors best. One level of units (``units.py``).
11Its presets are the shapes of the request it answers: **Orchestrator and
12workers** (a handful of pieces, each on the agent's own model), and
13**Fat-tailed** — a thin top over a wide base, one lead briefing up to ten
14workers on a lighter model, each with a small piece.
16A plan of ``one_agent`` is one piece, done alone, whose answer is the run's:
17no review and no synthesis, since splitting sequential work makes it worse.
18"""
20from __future__ import annotations
22from typing import Any
24from app.db.postgres.models import SwarmTask
25from app.services.swarms.roster import Member
26from app.services.swarms.topologies import (
27 Param,
28 Preset,
29 RoleSlot,
30 RunState,
31 register_topology,
32)
33from app.services.swarms.topologies.units import (
34 COST_PROFILE_CHOICES,
35 LEAD,
36 TIER_CHOICES,
37 WORKER,
38 Tier,
39 Units,
40 level_of,
41)
42from app.util import cost_profiles
45@register_topology
46class Supervisor(Units):
47 key = "supervisor"
48 label = "Lead and workers"
49 description = (
50 "A lead plans the work, workers do the pieces in parallel, and the "
51 "lead checks every piece against its brief before writing the answer."
52 )
53 roles = (
54 RoleSlot(
55 LEAD,
56 "Lead",
57 "Plans the work, assigns it, reviews it and writes the answer.",
58 min_members=1,
59 max_members=1,
60 ),
61 RoleSlot(
62 WORKER,
63 "Workers",
64 "Do the pieces the lead assigns them. A template is briefed as "
65 "many workers as the plan needs, each with its own specialism.",
66 min_members=0,
67 max_members=12,
68 spawn_allowed=True,
69 ),
70 )
71 params = (
72 Param(
73 "span",
74 "Pieces of work",
75 "The most pieces the lead may split the task into.",
76 "int",
77 4,
78 minimum=1,
79 maximum=12,
80 ),
81 Param(
82 "worker_tier",
83 "Workers' model",
84 "The model workers answer on.",
85 "choice",
86 Tier.as_configured.value,
87 choices=TIER_CHOICES,
88 ),
89 Param(
90 "worker_cost_profile",
91 "Workers' effort",
92 "How much checking each worker's own turn does; empty for each "
93 "agent's own setting.",
94 "choice",
95 None,
96 choices=COST_PROFILE_CHOICES,
97 ),
98 )
99 presets = (
100 Preset(
101 "orchestrator",
102 "Orchestrator and workers",
103 "A handful of pieces, each on its agent's own model.",
104 {
105 "span": 4,
106 "worker_tier": Tier.as_configured.value,
107 "worker_cost_profile": None,
108 },
109 ),
110 Preset(
111 "fat_tailed",
112 "Fat-tailed",
113 "A thin top over a wide base: one lead briefing up to ten workers "
114 "on a lighter model, each with a small piece.",
115 {
116 "span": 10,
117 "worker_tier": Tier.lighter.value,
118 "worker_cost_profile": cost_profiles.CostProfile.balanced.value,
119 },
120 ),
121 )
123 def levels(self, params: dict[str, Any]) -> int:
124 return 1
126 def span(self, params: dict[str, Any], level: int) -> int:
127 return int(params.get("span") or 1)
129 def assignable(self, state: RunState, plan: SwarmTask) -> list[Member]:
130 """Any member, the lead included: it takes a piece when no one else
131 suits it."""
132 if level_of(plan) == 0:
133 return list(state.roster.members)
134 return super().assignable(state, plan)
136 def level_settings(self, params: dict[str, Any], role: str) -> dict[str, Any]:
137 if role != WORKER:
138 return {}
139 return {
140 "tier": params.get("worker_tier") or Tier.as_configured.value,
141 "cost_profile": params.get("worker_cost_profile") or None,
142 }