Coverage for app / services / swarms / topologies / hierarchy.py: 98%

44 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-10-31 11:21 +0000

1"""A lead over managers over workers — a team hierarchy. 

2 

3The lead splits the task into parts, one for each manager; each manager 

4plans its part as pieces for its workers (or takes a piece itself), reviews 

5them, and reports up; the lead reviews the reports — sending one back once, 

6or asking for another round — and writes the answer. With three levels, a 

7manager's part goes to sub-managers in turn. Two or three levels of units 

8(``units.py``). 

9 

10Hierarchies with a reviewing manager at each level outperform flat teams on 

11the same budget in the studies behind the design, and coordinate best with 

12balanced spans of control, so the presets keep each level's span alike and 

13a run of uneven spans is told so (:meth:`Hierarchy.warnings`): 

14 

15* **Pyramid** — 1 → 3 → 9: the classic shape, three parts of three pieces. 

16* **Middle-heavy** — 1 → 4 → 8: many managers, each doing or delegating up 

17 to two pieces, so judgement sits in the middle. 

18* **Deep** — 1 → 2 → 4 → 8: three levels, two-way splits all the way down, 

19 for a task that divides cleanly into parts of parts. 

20""" 

21 

22from __future__ import annotations 

23 

24from math import prod 

25from typing import Any 

26 

27from app.services.swarms.topologies import ( 

28 Param, 

29 Preset, 

30 RoleSlot, 

31 register_topology, 

32) 

33from app.services.swarms.topologies.units import ( 

34 COST_PROFILE_CHOICES, 

35 LEAD, 

36 MANAGER, 

37 TIER_CHOICES, 

38 WORKER, 

39 Tier, 

40 Units, 

41) 

42 

43#: Planned tasks a hierarchy may hold, leaving room for redos and rounds 

44#: within the engine's ``MAX_TASKS``. 

45MAX_PLANNED_TASKS = 48 

46#: Spans this many times apart read as uneven. 

47UNEVEN_RATIO = 4 

48 

49_SPAN_KEYS = ("span_top", "span_middle", "span_bottom") 

50 

51 

52@register_topology 

53class Hierarchy(Units): 

54 key = "hierarchy" 

55 label = "Team hierarchy" 

56 description = ( 

57 "A lead splits the task between managers; each plans its part for " 

58 "its workers, checks their pieces and reports up; the lead checks " 

59 "the reports and writes the answer." 

60 ) 

61 roles = ( 

62 RoleSlot( 

63 LEAD, 

64 "Lead", 

65 "Splits the task into parts, reviews the managers' reports and " 

66 "writes the answer.", 

67 min_members=1, 

68 max_members=1, 

69 ), 

70 RoleSlot( 

71 MANAGER, 

72 "Managers", 

73 "Each plans a part for its workers, checks their pieces and " 

74 "reports up; a manager may take a piece itself. A template is " 

75 "briefed as several managers.", 

76 min_members=1, 

77 max_members=12, 

78 spawn_allowed=True, 

79 ), 

80 RoleSlot( 

81 WORKER, 

82 "Workers", 

83 "Do the pieces managers assign them. A template is briefed as " 

84 "many workers as the plans need.", 

85 min_members=0, 

86 max_members=12, 

87 spawn_allowed=True, 

88 ), 

89 ) 

90 params = ( 

91 Param( 

92 "levels", 

93 "Levels below the lead", 

94 "Two: managers, then workers. Three: managers, sub-managers, then workers.", 

95 "int", 

96 2, 

97 minimum=2, 

98 maximum=3, 

99 ), 

100 Param( 

101 "span_top", 

102 "Parts", 

103 "The most parts the lead splits the task into, one per manager.", 

104 "int", 

105 3, 

106 minimum=1, 

107 maximum=8, 

108 ), 

109 Param( 

110 "span_middle", 

111 "Pieces per part", 

112 "The most pieces (or, with three levels, sub-parts) each manager " 

113 "makes of its part.", 

114 "int", 

115 3, 

116 minimum=1, 

117 maximum=8, 

118 ), 

119 Param( 

120 "span_bottom", 

121 "Pieces per sub-part", 

122 "The most pieces each sub-manager makes.", 

123 "int", 

124 2, 

125 minimum=1, 

126 maximum=8, 

127 shown_when=("levels", (3,)), 

128 ), 

129 Param( 

130 "manager_tier", 

131 "Managers' model", 

132 "The model managers plan, review and report on.", 

133 "choice", 

134 Tier.as_configured.value, 

135 choices=TIER_CHOICES, 

136 ), 

137 Param( 

138 "worker_tier", 

139 "Workers' model", 

140 "The model workers answer on.", 

141 "choice", 

142 Tier.as_configured.value, 

143 choices=TIER_CHOICES, 

144 ), 

145 Param( 

146 "worker_cost_profile", 

147 "Workers' effort", 

148 "How much checking each worker's own turn does; empty for each " 

149 "agent's own setting.", 

150 "choice", 

151 None, 

152 choices=COST_PROFILE_CHOICES, 

153 ), 

154 ) 

155 presets = ( 

156 Preset( 

157 "pyramid", 

158 "Pyramid", 

159 "The classic shape: three parts, each of three pieces (1 → 3 → 9).", 

160 {"levels": 2, "span_top": 3, "span_middle": 3}, 

161 ), 

162 Preset( 

163 "middle_heavy", 

164 "Middle-heavy", 

165 "Many managers over few pieces each: four parts, each manager " 

166 "doing or delegating up to two pieces (1 → 4 → 8).", 

167 {"levels": 2, "span_top": 4, "span_middle": 2}, 

168 ), 

169 Preset( 

170 "deep", 

171 "Deep", 

172 "Three levels of two-way splits, for a task that divides into " 

173 "parts of parts (1 → 2 → 4 → 8).", 

174 {"levels": 3, "span_top": 2, "span_middle": 2, "span_bottom": 2}, 

175 ), 

176 ) 

177 

178 def levels(self, params: dict[str, Any]) -> int: 

179 return int(params.get("levels") or 2) 

180 

181 def span(self, params: dict[str, Any], level: int) -> int: 

182 key = _SPAN_KEYS[min(level, len(_SPAN_KEYS) - 1)] 

183 return int(params.get(key) or 1) 

184 

185 def spans(self, params: dict[str, Any]) -> list[int]: 

186 """The span at each level this hierarchy uses.""" 

187 return [self.span(params, level) for level in range(self.levels(params))] 

188 

189 def planned_tasks(self, params: dict[str, Any]) -> int: 

190 """The most tasks a run of these settings plans: each team's plan, 

191 review and report, and every piece at the bottom.""" 

192 spans = self.spans(params) 

193 teams = sum(prod(spans[:level]) for level in range(len(spans))) 

194 return 3 * teams + prod(spans) 

195 

196 def params_problems(self, params: dict[str, Any]) -> list[str]: 

197 planned = self.planned_tasks(params) 

198 if planned > MAX_PLANNED_TASKS: 

199 return [ 

200 ( 

201 f"These spans could make {planned} tasks; a hierarchy holds " 

202 f"{MAX_PLANNED_TASKS} at most. Narrow a level." 

203 ) 

204 ] 

205 return [] 

206 

207 def warnings(self, params: dict[str, Any]) -> list[str]: 

208 spans = self.spans(params) 

209 if max(spans) >= UNEVEN_RATIO * min(spans): 

210 shape = " → ".join(str(s) for s in [1, *spans]) 

211 return [ 

212 ( 

213 f"The levels' spans are uneven ({shape}): hierarchies with " 

214 "balanced spans coordinate better." 

215 ) 

216 ] 

217 return [] 

218 

219 def level_settings(self, params: dict[str, Any], role: str) -> dict[str, Any]: 

220 if role == MANAGER: 

221 return {"tier": params.get("manager_tier") or Tier.as_configured.value} 

222 if role == WORKER: 

223 return { 

224 "tier": params.get("worker_tier") or Tier.as_configured.value, 

225 "cost_profile": params.get("worker_cost_profile") or None, 

226 } 

227 return {}