@feigi/fleet-ctl 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (245) hide show
  1. package/.claude-plugin/plugin.json +9 -0
  2. package/LICENSE +202 -0
  3. package/NOTICE +18 -0
  4. package/agents/fleet-finisher.agent.md +7 -0
  5. package/agents/fleet-implementer-alt.agent.md +210 -0
  6. package/agents/fleet-implementer.agent.md +210 -0
  7. package/agents/fleet-merge-bot.agent.md +7 -0
  8. package/agents/fleet-review-comments.agent.md +40 -0
  9. package/agents/fleet-review-correctness.agent.md +43 -0
  10. package/agents/fleet-review-runner.agent.md +73 -0
  11. package/agents/fleet-review-silent-failure.agent.md +49 -0
  12. package/agents/fleet-review-simplify.agent.md +46 -0
  13. package/agents/fleet-review-snapshot.agent.md +24 -0
  14. package/agents/fleet-review-tests.agent.md +46 -0
  15. package/agents/fleet-review-types.agent.md +41 -0
  16. package/agents/fleet-review-verifier.agent.md +29 -0
  17. package/commands/review-and-fix.md +98 -0
  18. package/commands/run-merge-bot.md +357 -0
  19. package/commands/run-team-help.md +17 -0
  20. package/package.json +23 -0
  21. package/scripts/agent-test-dir-prose.test.mjs +59 -0
  22. package/scripts/agent-test.test.mjs +1636 -0
  23. package/scripts/ambient-git-vars-mjs-prose.test.mjs +354 -0
  24. package/scripts/ambient-git-vars-prose.test.mjs +192 -0
  25. package/scripts/ancestry-check-position-prose.test.mjs +175 -0
  26. package/scripts/apply-ruleset.test.mjs +420 -0
  27. package/scripts/arg-header-probes-prose.test.mjs +393 -0
  28. package/scripts/arg.mjs +600 -0
  29. package/scripts/arg.test.mjs +1354 -0
  30. package/scripts/board-cli.test.mjs +694 -0
  31. package/scripts/board-identity.test.mjs +429 -0
  32. package/scripts/board-liveness-banner.test.mjs +119 -0
  33. package/scripts/board-prev-shape.test.mjs +229 -0
  34. package/scripts/board-tryparse-null.test.mjs +119 -0
  35. package/scripts/board.html +471 -0
  36. package/scripts/board.mjs +2037 -0
  37. package/scripts/board.test.mjs +3086 -0
  38. package/scripts/brief-after-cut-prose.test.mjs +53 -0
  39. package/scripts/candidates-exit3-prose.test.mjs +302 -0
  40. package/scripts/candidates.mjs +674 -0
  41. package/scripts/candidates.test.mjs +2101 -0
  42. package/scripts/caveat-unification-prose.test.mjs +98 -0
  43. package/scripts/ci-comment-rot-prose.test.mjs +2025 -0
  44. package/scripts/ci-completes-premise-prose.test.mjs +257 -0
  45. package/scripts/ci-state-prose.test.mjs +113 -0
  46. package/scripts/ci-state.mjs +761 -0
  47. package/scripts/ci-state.test.mjs +1640 -0
  48. package/scripts/ci-vacuous-green.test.mjs +368 -0
  49. package/scripts/citation-sweep-prose.test.mjs +1572 -0
  50. package/scripts/claim-lifecycle.test.mjs +185 -0
  51. package/scripts/claim-ticket.sh +417 -0
  52. package/scripts/claim-ticket.test.mjs +1121 -0
  53. package/scripts/closing-keyword-prose.test.mjs +193 -0
  54. package/scripts/cockpit-launch-idempotent-prose.test.mjs +165 -0
  55. package/scripts/cockpit-tick-nonblocking.test.mjs +210 -0
  56. package/scripts/compute-board.mjs +579 -0
  57. package/scripts/compute-board.test.mjs +1060 -0
  58. package/scripts/compute-spend.mjs +329 -0
  59. package/scripts/compute-spend.test.mjs +451 -0
  60. package/scripts/cross-repo-citation-prose.test.mjs +156 -0
  61. package/scripts/cwd-isolation-pins.mjs +83 -0
  62. package/scripts/dedupe-guard-prose.test.mjs +243 -0
  63. package/scripts/derive-testcmd.sh +239 -0
  64. package/scripts/derive-testcmd.test.mjs +380 -0
  65. package/scripts/diff-stats.mjs +231 -0
  66. package/scripts/diff-stats.test.mjs +408 -0
  67. package/scripts/dispatch-block-golden-prose.test.mjs +1024 -0
  68. package/scripts/dispatch-block-pins-prose.test.mjs +615 -0
  69. package/scripts/drop-merged-label.sh +173 -0
  70. package/scripts/drop-merged-label.test.mjs +480 -0
  71. package/scripts/expected-jobs-prose.test.mjs +80 -0
  72. package/scripts/filing-bar-seat-prose.test.mjs +242 -0
  73. package/scripts/filing-claim-bar-prose.test.mjs +166 -0
  74. package/scripts/finisher-dispatch-premise-prose.test.mjs +312 -0
  75. package/scripts/finisher-halt-prose.test.mjs +106 -0
  76. package/scripts/finisher-name-prose.test.mjs +134 -0
  77. package/scripts/finisher-own-tree-prose.test.mjs +285 -0
  78. package/scripts/finisher-pin-race-prose.test.mjs +339 -0
  79. package/scripts/fix-applier-correction-rules-prose.test.mjs +201 -0
  80. package/scripts/fix-applier-result-file-prose.test.mjs +100 -0
  81. package/scripts/fleet-bootstrap +155 -0
  82. package/scripts/fleet-heartbeat.mjs +272 -0
  83. package/scripts/fleet-heartbeat.test.mjs +450 -0
  84. package/scripts/fleet-provenance +207 -0
  85. package/scripts/fleet-provenance.test.mjs +64 -0
  86. package/scripts/fleet-run +148 -0
  87. package/scripts/fleet-run.test.mjs +90 -0
  88. package/scripts/fleet-state.mjs +400 -0
  89. package/scripts/fleet-state.test.mjs +373 -0
  90. package/scripts/fleet-tick-prose.test.mjs +299 -0
  91. package/scripts/fleet-tick.mjs +832 -0
  92. package/scripts/fleet-tick.test.mjs +1332 -0
  93. package/scripts/force-with-lease-reason-prose.test.mjs +184 -0
  94. package/scripts/frontmatter-allowlist.json +64 -0
  95. package/scripts/frontmatter-check.mjs +256 -0
  96. package/scripts/frontmatter-check.test.mjs +436 -0
  97. package/scripts/git-env.mjs +124 -0
  98. package/scripts/git-env.test.mjs +149 -0
  99. package/scripts/git-status-untracked-mode-sweep.test.mjs +216 -0
  100. package/scripts/gone-walk.test.mjs +213 -0
  101. package/scripts/gp-sep-invariant-prose.test.mjs +188 -0
  102. package/scripts/head-moved-after-label-prose.test.mjs +233 -0
  103. package/scripts/immutable-body-claim-prose.test.mjs +413 -0
  104. package/scripts/implementer-model-tier.test.mjs +643 -0
  105. package/scripts/inflight-citation-prose.test.mjs +144 -0
  106. package/scripts/inflight-exit2-prose.test.mjs +81 -0
  107. package/scripts/inflight-probe-guard-prose.test.mjs +109 -0
  108. package/scripts/inflight-scan-scope-prose.test.mjs +57 -0
  109. package/scripts/inflight.sh +1106 -0
  110. package/scripts/inflight.test.mjs +2984 -0
  111. package/scripts/injection-control-prose.test.mjs +404 -0
  112. package/scripts/install-root-audit.test.mjs +464 -0
  113. package/scripts/instrument-check-prose.test.mjs +235 -0
  114. package/scripts/instruments.sh +394 -0
  115. package/scripts/instruments.test.mjs +787 -0
  116. package/scripts/issue-tracker-prose.test.mjs +71 -0
  117. package/scripts/json.sh +248 -0
  118. package/scripts/json.test.mjs +364 -0
  119. package/scripts/ledger-dispatch.test.mjs +516 -0
  120. package/scripts/ledger-grammar.mjs +85 -0
  121. package/scripts/ledger-grammar.test.mjs +142 -0
  122. package/scripts/ledger-read-require-file.test.mjs +197 -0
  123. package/scripts/ledger.mjs +1753 -0
  124. package/scripts/ledger.test.mjs +3554 -0
  125. package/scripts/line-distance-prose.test.mjs +245 -0
  126. package/scripts/liveness-rationale-prose.test.mjs +83 -0
  127. package/scripts/locale-pin-prose.test.mjs +189 -0
  128. package/scripts/member-outcome-prose.test.mjs +167 -0
  129. package/scripts/member-outcomes-header.test.mjs +148 -0
  130. package/scripts/member-outcomes-prose.test.mjs +63 -0
  131. package/scripts/member-outcomes.mjs +231 -0
  132. package/scripts/member-outcomes.test.mjs +507 -0
  133. package/scripts/member-prompt-prose.test.mjs +161 -0
  134. package/scripts/member-record.mjs +605 -0
  135. package/scripts/member-record.test.mjs +516 -0
  136. package/scripts/member-write-guard.mjs +279 -0
  137. package/scripts/member-write-guard.test.mjs +211 -0
  138. package/scripts/merge-commit-probe-prose.test.mjs +122 -0
  139. package/scripts/merge-gate.mjs +262 -0
  140. package/scripts/merge-gate.test.mjs +613 -0
  141. package/scripts/muted-git-guard-sweep.test.mjs +493 -0
  142. package/scripts/net-ssh-precedence-prose.test.mjs +91 -0
  143. package/scripts/net-watchdog-prose.test.mjs +79 -0
  144. package/scripts/net.sh +409 -0
  145. package/scripts/net.test.mjs +211 -0
  146. package/scripts/no-ci-gate-prose.test.mjs +108 -0
  147. package/scripts/no-undo-audit.sh +1134 -0
  148. package/scripts/no-undo-audit.test.mjs +3965 -0
  149. package/scripts/no-wave.test.mjs +138 -0
  150. package/scripts/node-floor-sweep.test.mjs +611 -0
  151. package/scripts/pointer-target-prose.test.mjs +186 -0
  152. package/scripts/pr-overlap.mjs +417 -0
  153. package/scripts/pr-overlap.test.mjs +695 -0
  154. package/scripts/printf-die-sweep.test.mjs +384 -0
  155. package/scripts/prompt-renderer.mjs +39 -0
  156. package/scripts/prompt-renderer.test.mjs +68 -0
  157. package/scripts/prose-pin.mjs +808 -0
  158. package/scripts/prose-pin.test.mjs +1034 -0
  159. package/scripts/prove-merge.sh +228 -0
  160. package/scripts/prove-merge.test.mjs +995 -0
  161. package/scripts/pull-table-relabel-prose.test.mjs +60 -0
  162. package/scripts/quiet-payload-prose.test.mjs +222 -0
  163. package/scripts/reap.sh +1768 -0
  164. package/scripts/reap.test.mjs +4395 -0
  165. package/scripts/reaping-prose.test.mjs +159 -0
  166. package/scripts/refuter-scratch-prose.test.mjs +135 -0
  167. package/scripts/release-label-required-prose.test.mjs +71 -0
  168. package/scripts/release-label-split-prose.test.mjs +169 -0
  169. package/scripts/release-ticket.sh +1716 -0
  170. package/scripts/release-ticket.test.mjs +5106 -0
  171. package/scripts/renovate-release-contract.test.mjs +379 -0
  172. package/scripts/repo-root.mjs +544 -0
  173. package/scripts/repo-root.test.mjs +873 -0
  174. package/scripts/reporter-output-rule-prose.test.mjs +208 -0
  175. package/scripts/review-agent-report-only.test.mjs +49 -0
  176. package/scripts/review-core-cwd-isolation.test.mjs +374 -0
  177. package/scripts/review-core-reads.test.mjs +478 -0
  178. package/scripts/review-core-refuter-scratch.test.mjs +148 -0
  179. package/scripts/review-core-snapshot-path.test.mjs +777 -0
  180. package/scripts/review-core-specialist-scratch.test.mjs +189 -0
  181. package/scripts/review-core-testcmd.test.mjs +288 -0
  182. package/scripts/review-core-unrun.test.mjs +530 -0
  183. package/scripts/review-core-unverified-discriminant.test.mjs +208 -0
  184. package/scripts/review-core.mjs +1028 -0
  185. package/scripts/review-eval-resolver-path.test.mjs +55 -0
  186. package/scripts/review-eval.mjs +197 -0
  187. package/scripts/review-host-fixture.mjs +73 -0
  188. package/scripts/review-in-run-retry.test.mjs +187 -0
  189. package/scripts/review-no-vendor-dispatch.test.mjs +45 -0
  190. package/scripts/review-path-default.test.mjs +796 -0
  191. package/scripts/review-runner-spawns.test.mjs +93 -0
  192. package/scripts/review-runner.test.mjs +155 -0
  193. package/scripts/review-tier-audit.test.mjs +140 -0
  194. package/scripts/run-artifact-pr-prose.test.mjs +341 -0
  195. package/scripts/run-merge-bot-prose.test.mjs +1391 -0
  196. package/scripts/scratch-partition-invariant-prose.test.mjs +74 -0
  197. package/scripts/scripts-path-citation-sweep.test.mjs +73 -0
  198. package/scripts/section-between-guard-prose.test.mjs +378 -0
  199. package/scripts/select-dimensions.test.mjs +674 -0
  200. package/scripts/shared-refusal.test.mjs +388 -0
  201. package/scripts/shell-traps-prose.test.mjs +507 -0
  202. package/scripts/shortlist.mjs +355 -0
  203. package/scripts/shortlist.test.mjs +366 -0
  204. package/scripts/slow-transport.mjs +72 -0
  205. package/scripts/snapshot-repo.test.mjs +772 -0
  206. package/scripts/snapshot-runner-audience-prose.test.mjs +129 -0
  207. package/scripts/spend-view.test.mjs +312 -0
  208. package/scripts/staleness-qualifier-prose.test.mjs +251 -0
  209. package/scripts/staleness-verdict-prose.test.mjs +142 -0
  210. package/scripts/staleness.mjs +414 -0
  211. package/scripts/staleness.test.mjs +893 -0
  212. package/scripts/stalled-member-liveness-prose.test.mjs +219 -0
  213. package/scripts/stash-prohibition-prose.test.mjs +313 -0
  214. package/scripts/strip-comments.mjs +44 -0
  215. package/scripts/tier-check.mjs +468 -0
  216. package/scripts/tier-check.test.mjs +726 -0
  217. package/scripts/tier-guard-gate-prose.test.mjs +216 -0
  218. package/scripts/tier-outcomes-header.test.mjs +586 -0
  219. package/scripts/tier-outcomes.mjs +441 -0
  220. package/scripts/tier-outcomes.test.mjs +342 -0
  221. package/scripts/tier-roles.mjs +250 -0
  222. package/scripts/tier-roles.test.mjs +316 -0
  223. package/scripts/tracker-block-copy-prose.test.mjs +288 -0
  224. package/scripts/triage-labels-gloss-prose.test.mjs +97 -0
  225. package/scripts/unattended-git-sweep.test.mjs +142 -0
  226. package/scripts/verify-sha-exit2-prose.test.mjs +269 -0
  227. package/scripts/verify-sha.sh +117 -0
  228. package/scripts/verify-sha.test.mjs +693 -0
  229. package/scripts/watcher-degraded-block.test.mjs +372 -0
  230. package/scripts/watcher-key-prose.test.mjs +106 -0
  231. package/scripts/within-run-pair-prose.test.mjs +208 -0
  232. package/scripts/worktree-audit.sh +482 -0
  233. package/scripts/worktree-audit.test.mjs +1407 -0
  234. package/scripts/worktree-head-prose.test.mjs +276 -0
  235. package/scripts/worktree-listing-sweep.test.mjs +217 -0
  236. package/scripts/worktree-model-prose.test.mjs +216 -0
  237. package/scripts/worktree.sh +470 -0
  238. package/skills/next-ticket/SKILL.md +138 -0
  239. package/skills/run-team/SKILL.md +3603 -0
  240. package/skills/run-team/references/ci-and-staleness.md +75 -0
  241. package/skills/run-team/references/correction-tickets.md +37 -0
  242. package/skills/run-team/references/isolation.md +23 -0
  243. package/skills/run-team/references/member-lifecycle.md +73 -0
  244. package/skills/run-team/references/reaping.md +73 -0
  245. package/skills/sizing-a-ticket/SKILL.md +39 -0
@@ -0,0 +1,329 @@
1
+ // Pure spend-model builder. No I/O and no clock read — board.mjs reads the
2
+ // session's subagent transcripts and calls computeSpend(); every classification
3
+ // and rollup lives here and is exercised by compute-spend.test.mjs (`node --test`).
4
+ //
5
+ // WHY cache_creation is the headline and cache_read is not: a fleet run reads
6
+ // its cache an order of magnitude more than it writes it (one measured run:
7
+ // 345M read vs 30M written), but cache reads bill at a fraction of writes. Rank
8
+ // on cache_read and every run looks identical and every role looks equally
9
+ // expensive. cache_creation is what actually tracks spend, so it is the sort key
10
+ // and the percentage base; the rest is carried for context, never for ranking.
11
+
12
+ // A fleet agent's role is not recorded anywhere as a field — it has to be
13
+ // recovered from how the controller named the work. Depth first: anything the
14
+ // controller did not spawn directly is a specialist, whatever it calls itself.
15
+ // Then description patterns, which are controller-authored and stable because
16
+ // run-team fixes them (`impl-<n>`, `fix-pr-<n>`, `review-pr-<n>`,
17
+ // `finisher-pr-<n>`, `merge-bot-<n>`).
18
+ //
19
+ // TWO IDENTITY SIGNALS, NEVER ONE (#1505). `agentDefinition` is the agent
20
+ // DEFINITION the dispatch recorded — `memory-housekeeper`, `fleet-implementer`,
21
+ // `fleet-review-verifier` — and `""` when it recorded none. `memberName` is
22
+ // what the member was CALLED (`impl-387`, `brain-housekeeping`), `""` for an
23
+ // unnamed dispatch. They used to arrive as ONE `agentType` parameter,
24
+ // documented as "the DISPATCH's identity for the member" but sometimes
25
+ // carrying the definition and sometimes the member's own name depending on
26
+ // which was available — so every rule below silently read whichever value
27
+ // happened to be supplied, and no rule could state which it meant.
28
+ // That ambiguity is what booked a memory-system member dispatched as
29
+ // `memory-housekeeper` but NAMED `brain-housekeeping` into the specialist
30
+ // bucket, moving its session's review-spend headline by 22 points (#1505).
31
+ // Each rule now names the signal it actually means and both readers fill both
32
+ // parameters from the same kind of value, so the asymmetry cannot come back by
33
+ // one reader being taught something the other was not.
34
+ //
35
+ // The finisher branch below also matches member names run-team does NOT fix.
36
+ // `finisher-pr-<n>` is the canonical spelling and the only one the naming list
37
+ // authorises; `finish-<n>`, `finish-pr-<n>` and `finisher-<n>` are spellings
38
+ // earlier runs actually dispatched. Matching those is deliberate compatibility
39
+ // with that history, NOT drift to be cleaned up: drop one and a member named
40
+ // that way books as "other" whenever its description does not happen to say
41
+ // "finish pr" too, moving the headline for runs already recorded.
42
+ // member-record.mjs matches the same four names, and its comment carries the
43
+ // measurement (#326).
44
+ export function classifyRole(signals) {
45
+ const def = String(signals?.agentDefinition ?? "");
46
+ const name = String(signals?.memberName ?? "");
47
+ const desc = String(signals?.description ?? "");
48
+
49
+ // Not fleet work at all — the memory system. Before everything else so it can
50
+ // never land in review spend.
51
+ //
52
+ // Reads the member's IDENTITY — the recorded definition and the name — and
53
+ // never `hay`. The definition is the authoritative half and the whole of
54
+ // #1505: a dispatch that recorded `memory-housekeeper` books memory whatever
55
+ // it called the member, so the eleven rows that shared one definition across
56
+ // three buckets collapse to one. The NAME stays a signal beside it because a
57
+ // missing definition is a real category here rather than a hole — an
58
+ // untyped `task` dispatch records none at all — and the memory members named
59
+ // `memory-proxy-session-review-2-3` have nothing else to be classified on;
60
+ // drop it and they fall to "other", which is the same defect wearing the
61
+ // other shoe. `desc` is excluded on purpose: every memory-adjacent fleet
62
+ // member's own prompt says "memory-proxy" somewhere, so blending the prose
63
+ // would book them all memory.
64
+ if (/memory-proxy|memory-housekeeper/.test(`${def} ${name}`)) return "memory";
65
+
66
+ // `spawnDepth` does NOT mean "review specialist". Only NAMED team members are
67
+ // depth 0; every UNNAMED agent the controller dispatches directly — the phase-0
68
+ // sizing pass, for one — is depth 1, exactly like a reviewer's fan-out.
69
+ // Classifying on depth alone swept the sizing agents into "specialist" and
70
+ // moved the review headline 83% → 87%, which is the one number anyone acts on.
71
+ // So recognise the controller's own unnamed dispatches by name FIRST. Anchored,
72
+ // so a specialist that merely mentions sizing is not swallowed.
73
+ if (/^size (candidate|ticket)/.test(desc.toLowerCase())) return "sizing";
74
+
75
+ // Everything else at depth ≥ 1 is a reviewer's fan-out. This must stay AHEAD of
76
+ // the member patterns below: specialists are named things like "Review PR 539
77
+ // correctness", which would otherwise book half the fan-out as reviewer spend.
78
+ if (Number(signals?.spawnDepth ?? 0) >= 1) return "specialist";
79
+
80
+ // Then the agent DEFINITION a dispatch named, for the members whose role the
81
+ // two signals above cannot reach. omp is where that happens: its review path
82
+ // runs `review-eval.mjs` inside the CONTROLLER's own session rather than as a
83
+ // nested review host, so its fan-out arrives at depth 0 — measured 2026-09-16
84
+ // on the live corpus, 466 of 477 `fleet-review-*` rows — and falls straight
85
+ // through to the description patterns below, which is precisely the
86
+ // "Review PR 539 correctness" misread the depth check above exists to
87
+ // prevent. Before this branch one definition, `fleet-review-verifier`, was
88
+ // split across four buckets on nothing but prompt wording: 211 other, 172
89
+ // reviewer, 20 finisher, 2 merge-bot (#1486).
90
+ //
91
+ // Matched against `def` ALONE — never the member's name, and never the `hay`
92
+ // blend the patterns below use. A fleet member's own dispatch prompt says what
93
+ // it is, so these names occur in description prose constantly, and a member
94
+ // that merely mentions a definition was not dispatched as one. `^`-anchored:
95
+ // omp's own registry is bare-name/unnamespaced (ADR 0014), so the recorded
96
+ // definition is always the bare form.
97
+ //
98
+ // The review side is a PREFIX and the implementer side is EXACT, deliberately:
99
+ // the fan-out's dimensions are an open set that `review-core.mjs` sizes per
100
+ // PR, so a dimension added tomorrow must not silently fall back to prose,
101
+ // while the implementer definitions are closed at two by the alternate-tier
102
+ // pairing that depends on exactly those two names existing.
103
+ //
104
+ // One definition under that prefix is NOT fan-out: `fleet-review-runner`
105
+ // (#1802) is the member that HOLDS an omp review — dispatched as
106
+ // `review-pr-<n>`, the reviewer the name branch below would book — so it is
107
+ // matched EXACTLY, ahead of the prefix. Its own fan-out arrives at depth ≥ 1
108
+ // and is booked "specialist" by the depth check above.
109
+ if (/^fleet-review-runner$/.test(def)) return "reviewer";
110
+ if (/^fleet-review-/.test(def)) return "specialist";
111
+ if (/^fleet-implementer(-alt)?$/.test(def)) return "implementer";
112
+
113
+ // The dispatch NAME is checked alone, in this same fixed order, before the
114
+ // prose blend below ever runs (#1506). Both readers now hand a canonical
115
+ // omp/run-team name (`impl-<n>`, `fix-pr-<n>`, `finisher-<n>`, `finish-<n>`,
116
+ // `review-pr-<n>`, `merge-bot-<n>`) straight through as `memberName`, and
117
+ // that name is a MORE PRECISE signal than the member's own free-text
118
+ // description — concatenating them into one `hay` before matching let a
119
+ // description's prose out-rank the name whenever the fixed branch order
120
+ // happened to check an earlier pattern the prose satisfied first. Measured
121
+ // live: `finisher-1380` described as "Fix PR 1380 review findings." matched
122
+ // the reviewer branch on "review" in the prose before the finisher branch
123
+ // ever saw the name that actually says what the member is; `merge-bot-12`
124
+ // described with "...review findings" misread the same way as finisher.
125
+ // Matching the name alone first, and only falling through to the blend when
126
+ // the name resolves nothing, restores the name's priority without
127
+ // disturbing the description-only fallback the tests above still need.
128
+ const byName = roleFromNamePatterns(name.toLowerCase());
129
+ if (byName) return byName;
130
+
131
+ const hay = `${name} ${desc}`.toLowerCase();
132
+ return roleFromNamePatterns(hay) ?? "other";
133
+ }
134
+
135
+ // The four name-driven branches, shared by both the name-alone pass above and
136
+ // the description-inclusive fallback: same patterns, same fixed order
137
+ // (implementer -> reviewer -> finisher -> merge-bot), just a different
138
+ // haystack. Returns `null`, never "other", so the caller can fall through to
139
+ // the next haystack instead of committing to "other" the moment the name
140
+ // alone resolves nothing.
141
+ function roleFromNamePatterns(hay) {
142
+ if (/^impl-|implement ticket/.test(hay)) return "implementer";
143
+ // Both per-PR member names book as review spend: `fix-pr-<n>` is the default
144
+ // path's applier, `review-pr-<n>` the hand-dispatch fallback's reviewer. Miss
145
+ // one and its cache writes fall through to "other", moving the review-side
146
+ // headline — the one number anyone acts on — by several points.
147
+ //
148
+ // `resolve-pr-<n>` joins the same bucket (#1250): it is a controller
149
+ // dispatch against an ALREADY-OPEN PR, not new ticket work — measured
150
+ // meta.json descriptions "Resolve conflict on PR 1232" and "Rebase and
151
+ // resolve conflicts for PR #1310", the same rebase/conflict-resolver shape
152
+ // `run-team/SKILL.md` calls "a rebase-resolver" sent into an open PR's
153
+ // worktree. It carries no ticket, no dispatch counter and no new-work verb —
154
+ // only a PR number — so it is remediation on that PR's path to merge,
155
+ // exactly the category `fix-pr-<n>`'s applier already occupies here. It is
156
+ // NOT `merge-bot`: that bucket's number is a per-run dispatch counter, never
157
+ // a PR, and merge-bot drains a pass's PRs rather than resolving one directly.
158
+ //
159
+ // Only the NAME form `resolve-pr-` joins the alternation, deliberately
160
+ // narrower than the `review pr`/`fix pr` prose forms beside it: the two
161
+ // real meta.json descriptions above ("Resolve conflict on PR 1232",
162
+ // "Rebase and resolve conflicts for PR #1310") never contain the bare
163
+ // words "resolve pr" adjacently, so a `resolve pr` prose alternative would
164
+ // be untested reach rather than a measured pattern — and "resolve" is
165
+ // common enough in unrelated prose (a finisher applying reviewer
166
+ // "resolve"-shaped findings against "pr" work) that adding it ahead of the
167
+ // finisher/merge-bot checks below risked hijacking their classification on
168
+ // words alone, with no real row to justify it.
169
+ if (/review pr|review-pr-|fix pr|fix-pr-|resolve-pr-/.test(hay)) return "reviewer";
170
+ if (/^finish-|finish pr|finisher/.test(hay)) return "finisher";
171
+ if (/merge-bot/.test(hay)) return "merge-bot";
172
+ return null;
173
+ }
174
+
175
+ // The same 5 run-team-fixed dispatch-name prefixes the branches above match,
176
+ // as a bare regex-alternation SOURCE STRING (not a compiled RegExp) — so
177
+ // member-record.mjs's OMP_CANONICAL_STEM_RE can build its own `-`-suffixed,
178
+ // anchored pattern from this one list instead of independently retyping it.
179
+ // The two encode the same run-team naming convention and must not drift
180
+ // apart the way two hand-copied lists eventually do. `finish(?:er)?`
181
+ // collapses the `finisher`/`finish` pair into the shape
182
+ // `roleFromNamePatterns`'s own bare-word check already uses.
183
+ export const CANONICAL_MEMBER_NAME_PREFIXES = "impl|fix-pr|finish(?:er)?|review-pr|merge-bot";
184
+
185
+ // Seeds the rollup buckets and breaks ties in the report, which is otherwise
186
+ // sorted by spend — so this is not the order the UI shows. Review roles lead so
187
+ // that at equal spend they still read first.
188
+ export const ROLE_ORDER = [
189
+ "specialist", "reviewer", "implementer", "merge-bot", "sizing", "memory", "finisher", "other",
190
+ ];
191
+
192
+ const empty = () => ({ agents: 0, cacheWrite: 0, output: 0, cacheRead: 0 });
193
+
194
+ export function computeSpend({ agents = [], topN = 8 } = {}) {
195
+ const byRole = new Map(ROLE_ORDER.map((r) => [r, empty()]));
196
+ const totals = empty();
197
+
198
+ for (const a of agents) {
199
+ const role = a.role ?? "other";
200
+ const b = byRole.get(role) ?? byRole.set(role, empty()).get(role);
201
+ for (const k of ["cacheWrite", "output", "cacheRead"]) {
202
+ b[k] += a[k] ?? 0;
203
+ totals[k] += a[k] ?? 0;
204
+ }
205
+ b.agents++;
206
+ totals.agents++;
207
+ }
208
+
209
+ // Percentages are of cache_creation only. A zero-total run must not produce
210
+ // NaN in the UI — an empty board is a normal state at run start.
211
+ const share = (n) => (totals.cacheWrite > 0 ? (n / totals.cacheWrite) * 100 : 0);
212
+
213
+ // Report every bucket that collected something, not just the known ones. The
214
+ // accumulator above happily creates a bucket for a role outside ROLE_ORDER,
215
+ // but mapping over ROLE_ORDER dropped it from the table while its tokens
216
+ // stayed in `totals` — so the percentage column silently stopped summing to
217
+ // 100 and the run looked cheaper than it was. Latent while classifyRole only
218
+ // emits known roles; it fails by under-reporting the moment one is added to
219
+ // the classifier and not to this array.
220
+ const roles = [...byRole.entries()]
221
+ .filter(([, b]) => b.agents > 0)
222
+ .map(([role, b]) => ({ role, ...b, pct: share(b.cacheWrite) }))
223
+ .sort((a, b) => b.cacheWrite - a.cacheWrite);
224
+
225
+ // `model` rides along only where the caller's agent object supplied one
226
+ // (#1716) — an agent with no `model` key keeps its payload exactly as it
227
+ // was rather than gaining a null column.
228
+ const top = [...agents]
229
+ .sort((a, b) => (b.cacheWrite ?? 0) - (a.cacheWrite ?? 0))
230
+ .slice(0, topN)
231
+ .map((a) => ({
232
+ label: a.label ?? "?", role: a.role ?? "other",
233
+ ...(a.model != null && { model: a.model }),
234
+ cacheWrite: a.cacheWrite ?? 0, pct: share(a.cacheWrite ?? 0),
235
+ }));
236
+
237
+ // The single number worth surfacing: review side is specialists + reviewers.
238
+ // Two independent runs measured 84% and 83%, so a large deviation is a signal
239
+ // that this run's shape changed, not that the metric is noisy.
240
+ const reviewPct = share(
241
+ (byRole.get("specialist")?.cacheWrite ?? 0) + (byRole.get("reviewer")?.cacheWrite ?? 0),
242
+ );
243
+
244
+ return { totals, roles, top, reviewPct };
245
+ }
246
+
247
+ // Per-TOOL attribution. Tokens are not billed per tool call, so this is a proxy
248
+ // and is labelled as one everywhere it surfaces: a tool result arrives in a user
249
+ // turn, and the NEXT assistant turn's cache_creation is the cost of writing that
250
+ // result into the cache. When several results land before that turn, the cost is
251
+ // split proportionally by result size, because that is what drove it.
252
+ //
253
+ // Those results arrive as CONSECUTIVE user turns, not as one turn carrying
254
+ // several blocks: across 45,062 real result-bearing turns, not one carried two
255
+ // tool_results. Parallel tool calls show up as N single-result turns in a row
256
+ // (4,087 occurrences). So `pending` must ACCUMULATE across consecutive result
257
+ // turns — replacing it dropped every batch but the last, losing 9.1% of all
258
+ // attributions, and left the proportional split below unreachable on real data.
259
+ //
260
+ // The proxy over-attributes slightly — that next turn also caches the assistant's
261
+ // own preceding output — so treat these as shares, not absolutes. `resultChars`
262
+ // is the honest raw number and needs no modelling at all; it is reported beside
263
+ // the estimate so a suspicious share can always be checked against it.
264
+ //
265
+ // `entries` is an ordered per-agent stream:
266
+ // { kind: "assistant", cacheWrite, tools: [{ id, name }] }
267
+ // { kind: "result", results: [{ id, chars }] }
268
+ export function attributeTools(entries = []) {
269
+ const nameById = new Map();
270
+ const byTool = new Map();
271
+ const bump = (name) => {
272
+ let t = byTool.get(name);
273
+ if (!t) byTool.set(name, (t = { tool: name, calls: 0, resultChars: 0, cacheWrite: 0 }));
274
+ return t;
275
+ };
276
+
277
+ let pending = null; // the most recent result batch, awaiting the next assistant turn
278
+
279
+ for (const e of entries) {
280
+ if (e.kind === "assistant") {
281
+ // Attribute this turn's cache write to whatever results preceded it.
282
+ if (pending && pending.length) {
283
+ const total = pending.reduce((n, r) => n + r.chars, 0);
284
+ for (const r of pending) {
285
+ // An all-zero batch still gets an even split rather than being dropped —
286
+ // a tool that returns nothing still costs a turn to process.
287
+ const frac = total > 0 ? r.chars / total : 1 / pending.length;
288
+ bump(r.name).cacheWrite += (e.cacheWrite ?? 0) * frac;
289
+ }
290
+ }
291
+ pending = null;
292
+ for (const t of e.tools ?? []) {
293
+ if (!t?.id) continue;
294
+ nameById.set(t.id, t.name ?? "unknown");
295
+ bump(t.name ?? "unknown").calls++;
296
+ }
297
+ } else if (e.kind === "result") {
298
+ const batch = (e.results ?? []).map((r) => {
299
+ const name = nameById.get(r.id) ?? "unknown";
300
+ bump(name).resultChars += r.chars ?? 0;
301
+ return { name, chars: r.chars ?? 0 };
302
+ });
303
+ pending = pending ? pending.concat(batch) : batch;
304
+ }
305
+ }
306
+
307
+ const tools = [...byTool.values()]
308
+ .map((t) => ({ ...t, cacheWrite: Math.round(t.cacheWrite) }))
309
+ .sort((a, b) => b.cacheWrite - a.cacheWrite);
310
+ const totalCw = tools.reduce((n, t) => n + t.cacheWrite, 0);
311
+ return tools.map((t) => ({ ...t, pct: totalCw > 0 ? (t.cacheWrite / totalCw) * 100 : 0 }));
312
+ }
313
+
314
+ // Merge per-agent tool tables into one run-level table.
315
+ export function mergeTools(tables = []) {
316
+ const acc = new Map();
317
+ for (const table of tables) {
318
+ for (const t of table) {
319
+ const cur = acc.get(t.tool) ?? { tool: t.tool, calls: 0, resultChars: 0, cacheWrite: 0 };
320
+ cur.calls += t.calls;
321
+ cur.resultChars += t.resultChars;
322
+ cur.cacheWrite += t.cacheWrite;
323
+ acc.set(t.tool, cur);
324
+ }
325
+ }
326
+ const tools = [...acc.values()].sort((a, b) => b.cacheWrite - a.cacheWrite);
327
+ const total = tools.reduce((n, t) => n + t.cacheWrite, 0);
328
+ return tools.map((t) => ({ ...t, pct: total > 0 ? (t.cacheWrite / total) * 100 : 0 }));
329
+ }