tryharness 0.7.45 → 0.7.54

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 (125) hide show
  1. package/README.md +239 -14
  2. package/assets/dsh-plugin.mjs +72 -0
  3. package/assets/opencode-plugin.js +4 -3
  4. package/cli.mjs +497 -196
  5. package/lib/actorRoster.mjs +28 -5
  6. package/lib/adapters.mjs +165 -222
  7. package/lib/agentRuntimeConfig.mjs +36 -0
  8. package/lib/agentRuntimes.mjs +570 -0
  9. package/lib/agentSignIn.mjs +226 -0
  10. package/lib/atlas.mjs +7 -2
  11. package/lib/audit.mjs +643 -0
  12. package/lib/backupSetup.mjs +141 -0
  13. package/lib/boardAccess.mjs +8 -1
  14. package/lib/boardBackup.mjs +283 -0
  15. package/lib/boardCommands.mjs +5 -10
  16. package/lib/boardFileCommands.mjs +2 -1
  17. package/lib/boardMirror.mjs +523 -0
  18. package/lib/boardProtocol.json +263 -6
  19. package/lib/boardTools.mjs +2 -6
  20. package/lib/boardWaits.mjs +102 -0
  21. package/lib/book.mjs +659 -0
  22. package/lib/bridge.mjs +26 -4
  23. package/lib/browserWake.mjs +60 -0
  24. package/lib/cards.mjs +159 -16
  25. package/lib/cloudBoot.mjs +124 -0
  26. package/lib/commands.mjs +25 -5
  27. package/lib/connectionTools.d.ts +13 -0
  28. package/lib/connectionTools.js +45 -0
  29. package/lib/connectorAuth.mjs +309 -0
  30. package/lib/connectorCatalog.json +41 -0
  31. package/lib/connectorCommands.mjs +121 -0
  32. package/lib/connectorGoogle.mjs +513 -0
  33. package/lib/connectors.mjs +805 -0
  34. package/lib/context.mjs +18 -4
  35. package/lib/contractGate.mjs +377 -0
  36. package/lib/coordinator.mjs +152 -39
  37. package/lib/coreModels.mjs +78 -0
  38. package/lib/daemon.mjs +973 -54
  39. package/lib/desk.mjs +239 -0
  40. package/lib/desktop.mjs +113 -8
  41. package/lib/digest.mjs +32 -0
  42. package/lib/directLink.mjs +283 -0
  43. package/lib/environments.mjs +825 -0
  44. package/lib/folders.mjs +4 -4
  45. package/lib/gitHost.mjs +174 -0
  46. package/lib/goalCommands.mjs +58 -14
  47. package/lib/hookDialects.mjs +298 -0
  48. package/lib/inference.mjs +263 -152
  49. package/lib/landed.mjs +887 -0
  50. package/lib/lessons.mjs +6 -0
  51. package/lib/lineup.mjs +324 -0
  52. package/lib/live.mjs +284 -13
  53. package/lib/localmodels.mjs +10 -0
  54. package/lib/machineIdentity.mjs +3 -0
  55. package/lib/machineRoutes.mjs +40 -2
  56. package/lib/machineRuns.mjs +30 -3
  57. package/lib/machineSeal.mjs +69 -10
  58. package/lib/mcp-stdio.mjs +189 -120
  59. package/lib/mcpRelay.mjs +119 -0
  60. package/lib/members.json +26 -0
  61. package/lib/members.mjs +33 -0
  62. package/lib/modelMemory.mjs +100 -0
  63. package/lib/models.mjs +48 -15
  64. package/lib/museSession.mjs +122 -0
  65. package/lib/nativeHost.mjs +203 -0
  66. package/lib/neovim.mjs +126 -0
  67. package/lib/network.mjs +7 -2
  68. package/lib/nudge.mjs +57 -18
  69. package/lib/onboard.mjs +430 -1
  70. package/lib/oneclawLocal.mjs +150 -0
  71. package/lib/ownMachines.mjs +828 -39
  72. package/lib/pairing.mjs +182 -0
  73. package/lib/paths.mjs +8 -0
  74. package/lib/personModel.mjs +89 -0
  75. package/lib/phones.mjs +119 -3
  76. package/lib/placeCommands.mjs +27 -0
  77. package/lib/places.mjs +337 -9
  78. package/lib/plans.mjs +151 -27
  79. package/lib/profile.mjs +152 -0
  80. package/lib/projects.mjs +624 -137
  81. package/lib/readiness.mjs +400 -0
  82. package/lib/record.mjs +115 -0
  83. package/lib/repoLinks.mjs +354 -0
  84. package/lib/roomChannels.mjs +641 -0
  85. package/lib/runBrief.mjs +32 -2
  86. package/lib/runRequests.mjs +157 -0
  87. package/lib/runWorker.mjs +37 -9
  88. package/lib/runner.mjs +74 -11
  89. package/lib/secretPatterns.json +55 -0
  90. package/lib/secrets.mjs +10 -27
  91. package/lib/seen.mjs +102 -0
  92. package/lib/service.mjs +11 -6
  93. package/lib/sessionControl.mjs +386 -96
  94. package/lib/sessionIndex.mjs +5 -3
  95. package/lib/specialties.json +74 -0
  96. package/lib/specialties.mjs +477 -0
  97. package/lib/specialtyKeys.d.ts +3 -0
  98. package/lib/specialtyKeys.js +58 -0
  99. package/lib/specialtyNotice.mjs +147 -0
  100. package/lib/specialtySnapshot.mjs +138 -0
  101. package/lib/store.mjs +829 -29
  102. package/lib/subagents.mjs +8 -4
  103. package/lib/swarm.mjs +483 -37
  104. package/lib/swarmMemory.mjs +947 -0
  105. package/lib/t3.mjs +56 -17
  106. package/lib/taskCommands.mjs +91 -13
  107. package/lib/taskRuns.mjs +68 -11
  108. package/lib/team.json +253 -144
  109. package/lib/team.mjs +538 -147
  110. package/lib/threadLine.mjs +116 -0
  111. package/lib/threadRuns.mjs +78 -12
  112. package/lib/threadView.mjs +66 -16
  113. package/lib/tokenizer.mjs +405 -0
  114. package/lib/ui.mjs +16 -2
  115. package/lib/usageTotals.mjs +148 -0
  116. package/lib/vault.mjs +1144 -0
  117. package/lib/vaultCloud.mjs +117 -0
  118. package/lib/vaultCommands.mjs +201 -0
  119. package/lib/vaultTools.d.ts +12 -0
  120. package/lib/vaultTools.js +42 -0
  121. package/lib/windowAsks.mjs +42 -0
  122. package/lib/workFocus.mjs +59 -0
  123. package/lib/workLinks.mjs +90 -0
  124. package/lib/worktrees.mjs +51 -13
  125. package/package.json +10 -5
package/lib/team.mjs CHANGED
@@ -1,162 +1,441 @@
1
1
  import { readFileSync } from "node:fs";
2
2
  import { nameOf } from "./members.mjs";
3
+ import { specialtyName } from "./specialties.mjs";
4
+ import { namedFiles } from "./worktrees.mjs";
3
5
 
4
- // How the agents in a thread work as a team (team.json, read by the server too through src/shared/team.ts): the team's
5
- // shape, who holds each role, how careful the work must be, and the route the work takes between the roles. Without one,
6
- // every round handed the next piece to whichever single agent looked likeliest, and the busiest agent did most of the
7
- // work while nobody checked it or pulled parallel results together (board, October 20: 30 of 52 hand-ons to one agent,
8
- // 24 of 34 to the same agent as the round before, and 42 of 50 assemblies gave the whole message to one agent).
6
+ // How the agents in a thread work as a team (team.json, read by the server too through src/shared/team.ts): one route,
7
+ // frame, make, check, combine, judge, whose steps the rigor picks, each step held by the agents Jev routed it to for the
8
+ // specialty it asks for (docs/swarm-organization.md, Routes). Without it, every round handed the next piece to whichever
9
+ // single agent looked likeliest, and the busiest agent did most of the work while nobody checked it or pulled parallel
10
+ // results together (board, October 20: 30 of 52 hand-ons to one agent, and 42 of 50 assemblies gave the whole message
11
+ // to one agent).
9
12
  //
10
- // The plan is set in the thread's first assembly round (src/server/swarm/coordinator.ts teamPlan) and kept here. The
11
- // route is followed here: the round is told which step the thread is at, whether that step's agents have reported since
12
- // it began, and where the work may go next (`exits`); Jev judges only what needs judgment (did a check pass), and the
13
- // daemon moves the thread to the step the round chose.
13
+ // Jev triages the goal in the thread's first assembly round (src/server/swarm/coordinator.ts teamPlan): its rigor,
14
+ // whether a framer splits it into pieces, the specialty each step asks for and who holds each. A goal that is split
15
+ // runs its frame first; the framer's report gives the pieces, and each is routed by Jev as it starts. The route is
16
+ // followed here: the round is told which step the thread is at, whether that step's agents have reported since it
17
+ // began, and where the work may go next (`exits`); Jev judges only what needs judgment (did a check pass), and the
18
+ // daemon moves the thread to the step the round chose. Every run is marked with the step it worked and the specialty
19
+ // that step asked for, and a check's outcome is written on the runs it judged, for the record (record.mjs).
14
20
 
15
21
  export const TEAM = JSON.parse(
16
22
  readFileSync(new URL("./team.json", import.meta.url), "utf8")
17
23
  );
18
- const SHAPES = TEAM.shapes;
19
- export const SHAPE_IDS = Object.keys(SHAPES);
24
+ const ROUTE = TEAM.route;
25
+ export const STEP_IDS = ROUTE.map((step) => step.id);
20
26
  export const ROLE_IDS = Object.keys(TEAM.roles);
21
27
  export const RIGOR_LEVELS = Object.keys(TEAM.rigor);
28
+ const stepOf = (id) => ROUTE.find((step) => step.id === id);
22
29
  // Every runner's reported states that end a turn: a question to the person (needs_input) is not the step's end.
23
30
  const FINISHED = new Set(["done", "failed", "unknown"]);
31
+ // The most pieces one framer's split is routed as; more read as one list.
32
+ const MOST_PIECES = 4;
24
33
 
25
- /** Whether a model takes a goal and finds its own route (the brief gives it intent), from its name. */
26
- export const goalLevel = (model) =>
27
- Boolean(model) &&
28
- TEAM.goalLevelModels.some((family) =>
29
- String(model).toLowerCase().includes(family)
30
- );
34
+ /** The role a step is held as when it asks for this specialty: named by the step and the specialty (team.json). */
35
+ export const roleFor = (step, specialty) =>
36
+ ROLE_IDS.find((role) =>
37
+ specialty ? TEAM.roles[role].steps[step]?.includes(specialty) : false
38
+ ) ?? stepOf(step).role;
31
39
 
32
- const stepsOf = (plan) =>
33
- (SHAPES[plan.shape]?.steps ?? []).filter(
34
- (step) => (plan.roles[step.role] ?? []).length > 0
40
+ /**
41
+ * The steps a piece's route runs at a rigor: Combine below thorough only when several agents made it. A piece the
42
+ * framer split off has its frame already (its brief), and so does a goal whose makers the person tagged (their words).
43
+ */
44
+ export const stepsFor = (rigor, { makers, framed }) =>
45
+ STEP_IDS.filter(
46
+ (id) =>
47
+ (TEAM.rigor[rigor].steps.includes(id) ||
48
+ (id === "combine" && (rigor === "thorough" || makers > 1))) &&
49
+ !(framed && id === "frame")
35
50
  );
36
51
 
37
- /** A plan as the round proposed it, kept only where it names a known shape, known roles and agents that may hold them. */
38
- export function validPlan(proposed, { members }) {
39
- const shape = SHAPES[proposed?.shape];
40
- if (!shape) return null;
52
+ const holdersOf = (steps, id) =>
53
+ steps.find((step) => step.id === id)?.agents ?? [];
54
+
55
+ // A specialty on the list, else none (the route still runs; the run counts toward no specialty).
56
+ const KNOWN = new Set(
57
+ JSON.parse(
58
+ readFileSync(new URL("./specialties.json", import.meta.url), "utf8")
59
+ ).headings.flatMap((heading) => heading.specialties.map(({ id }) => id))
60
+ );
61
+ const knownSpecialty = (id) => (KNOWN.has(String(id)) ? String(id) : null);
62
+
63
+ /**
64
+ * A route as the round proposed it, kept only where it names known steps and agents of the swarm, with the person's
65
+ * choices over Jev's, step by step: an agent they tagged fills a step (Make, when Jev put it on none: "@codex fix this"
66
+ * puts Codex on Make), a pin holds the step whose specialty it is for unless a tagged agent does, and Jev's pick stands
67
+ * otherwise. The rigor then picks the steps that run. No agent checks or judges what it made, and the framer never
68
+ * judges the piece it framed: a pin that would do so gives way to Jev's pick, and a step left with no one is dropped
69
+ * and the thread says so (`unfilled`).
70
+ */
71
+ export function validPlan(
72
+ proposed,
73
+ { members, named = [], pins = {}, piece = false }
74
+ ) {
41
75
  const allowed = new Set(members);
42
- const roles = {};
43
- for (const role of shape.roles) {
44
- const holders = [
45
- ...new Set((proposed.roles?.[role] ?? []).map(String)),
46
- ].filter((agent) => allowed.has(agent));
47
- if (holders.length)
48
- roles[role] = TEAM.several.includes(role) ? holders : holders.slice(0, 1);
49
- }
50
- // The author never checks or reviews its own work.
51
- const builder = roles.builder?.[0];
52
- for (const role of ["checker", "reviewer"])
53
- if (builder && roles[role]?.[0] === builder) delete roles[role];
54
- const plan = {
55
- shape: proposed.shape,
56
- rigor: RIGOR_LEVELS.includes(proposed.rigor) ? proposed.rigor : "standard",
57
- roles,
58
- };
59
- return stepsOf(plan).length ? plan : null;
76
+ const rigor = RIGOR_LEVELS.includes(proposed?.rigor)
77
+ ? proposed.rigor
78
+ : "standard";
79
+ const split = !piece && Boolean(proposed?.split);
80
+ const given = new Map(
81
+ (Array.isArray(proposed?.steps) ? proposed.steps : [])
82
+ .filter((step) => STEP_IDS.includes(step?.id))
83
+ .map((step) => [
84
+ step.id,
85
+ {
86
+ specialty: knownSpecialty(step.specialty),
87
+ agents: [
88
+ ...new Set(
89
+ (step.agents ?? [])
90
+ .map(String)
91
+ .filter((agent) => allowed.has(agent))
92
+ ),
93
+ ],
94
+ },
95
+ ])
96
+ );
97
+ const tagged = named.filter((agent) => allowed.has(agent));
98
+ const placedByJev = new Set([...given.values()].flatMap((s) => s.agents));
99
+ const unplaced = tagged.filter((agent) => !placedByJev.has(agent));
100
+ const holders = {};
101
+ const pinned = new Set();
102
+ // Whom a step may not go to: a check or judgment never to a maker, a judgment never to the framer.
103
+ const barred = (id) =>
104
+ id === "check"
105
+ ? holders.make
106
+ : id === "judge"
107
+ ? [...holders.make, ...holders.frame]
108
+ : [];
109
+ const steps = STEP_IDS.map((id) => {
110
+ const step = given.get(id) ?? { specialty: null, agents: [] };
111
+ const taggedHere = step.agents.filter((agent) => tagged.includes(agent));
112
+ const pin = step.specialty ? pins[step.specialty] : null;
113
+ let agents = step.agents;
114
+ if (id === "make" && unplaced.length && !split)
115
+ agents = [...new Set([...taggedHere, ...unplaced])];
116
+ else if (taggedHere.length) agents = taggedHere;
117
+ else if (pin && allowed.has(pin) && !barred(id).includes(pin)) {
118
+ agents = [pin];
119
+ pinned.add(id);
120
+ }
121
+ // Make is never left empty while the person tagged someone.
122
+ if (id === "make" && !agents.length && !split) agents = tagged;
123
+ agents = stepOf(id).together
124
+ ? agents.slice(0, TEAM.mostSideBySide)
125
+ : agents.slice(0, 1);
126
+ holders[id] = agents;
127
+ return { id, specialty: step.specialty, agents };
128
+ });
129
+ const makers = holders.make;
130
+ // A tagged maker means the person framed the work themselves, unless they also named who frames it.
131
+ const framedByTag =
132
+ makers.some((agent) => tagged.includes(agent)) &&
133
+ !holders.frame.some((agent) => tagged.includes(agent));
134
+ const running = split
135
+ ? ["frame"]
136
+ : stepsFor(rigor, { makers: makers.length, framed: piece || framedByTag });
137
+ const unfilled = [];
138
+ const kept = steps
139
+ .filter((step) => running.includes(step.id))
140
+ .flatMap((step) => {
141
+ const own = step.agents.some((agent) => barred(step.id).includes(agent));
142
+ if (own || !step.agents.length) {
143
+ unfilled.push({ step: step.id, own });
144
+ return [];
145
+ }
146
+ return [
147
+ {
148
+ ...step,
149
+ role: roleFor(step.id, step.specialty),
150
+ ...(pinned.has(step.id) ? { pinned: true } : {}),
151
+ },
152
+ ];
153
+ });
154
+ if (!kept.length) return null;
155
+ if (!split && !kept.some((step) => step.id === "make")) return null;
156
+ return { rigor, split, steps: kept, unfilled };
60
157
  }
61
158
 
159
+ // The route as the thread says it: "frame: Claude Code (framer, planning and coordination); make: Codex (builder, ...)".
160
+ const lineup = (steps) =>
161
+ steps
162
+ .map(
163
+ (step) =>
164
+ `${step.id}: ${step.agents.map(nameOf).join(" and ")} (${TEAM.roles[step.role].name}${step.specialty ? `, ${specialtyName(step.specialty)}` : ""})`
165
+ )
166
+ .join("; ");
167
+
168
+ // Why a step of the route runs with nobody: the thread's words.
169
+ const unfilledLine = ({ step, own }) =>
170
+ own
171
+ ? `No agent here could ${step} this without ${step === "judge" ? "judging" : "checking"} its own work, so it goes without a ${step}.`
172
+ : `No agent here was routed to ${step} it, so it goes without that step.`;
173
+
174
+ // The pieces a framer's report splits a goal into: each on its own line as "Piece:" and its brief (the framer's
175
+ // contract in team.json asks for exactly that form).
176
+ export const piecesIn = (text) =>
177
+ String(text || "")
178
+ .split("\n")
179
+ .map((line) =>
180
+ /^\s*(?:[-*]\s*)?\**piece\b[^:]{0,12}:\**\s*(.+)$/i
181
+ .exec(line)?.[1]
182
+ ?.trim()
183
+ )
184
+ .filter(Boolean)
185
+ .slice(0, MOST_PIECES);
186
+
62
187
  // How old a note is, in the words a brief uses.
63
188
  const age = (iso) => {
64
189
  const days = Math.floor((Date.now() - Date.parse(iso)) / 86_400_000);
65
190
  return days < 1 ? "today" : days === 1 ? "yesterday" : `${days} days ago`;
66
191
  };
67
192
  const NOTE_CHARS = 600;
193
+ const BRIEF_CHARS = 1_000;
68
194
 
69
195
  export function createTeam({ store, log = () => {} }) {
70
196
  const key = (goalId) => `team:${goalId}`;
197
+ // A plan from before routes (a shape and roles) is no plan: the thread's next message to the swarm routes it anew.
71
198
  const read = (goalId) => {
72
199
  try {
73
- return JSON.parse(store.meta(key(goalId)) || "null");
200
+ const kept = JSON.parse(store.meta(key(goalId)) || "null");
201
+ return Array.isArray(kept?.pieces) ? kept : null;
74
202
  } catch {
75
203
  return null;
76
204
  }
77
205
  };
78
206
  const write = (goalId, plan) => store.meta(key(goalId), JSON.stringify(plan));
79
207
  const now = () => new Date().toISOString();
208
+ const pieceOf = (current) => current.pieces[current.piece];
209
+ const stepsOfNow = (current) => pieceOf(current)?.steps ?? [];
210
+
211
+ // Where each step began and who held it, so a run is marked with the step it worked.
212
+ const started = (current, step) => {
213
+ const entry = stepsOfNow(current).find((s) => s.id === step);
214
+ return entry
215
+ ? [
216
+ ...(current.history ?? []),
217
+ {
218
+ piece: current.piece,
219
+ step,
220
+ specialty: entry.specialty,
221
+ agents: entry.agents,
222
+ at: now(),
223
+ },
224
+ ].slice(-40)
225
+ : (current.history ?? []);
226
+ };
227
+
228
+ // Every finished run of the thread not yet marked takes the step its agent held when it reported: a run the person's
229
+ // tag started before the route began counts toward the step its agent was routed to, and a checker an agent asked by
230
+ // name while it worked counts as the check the route caught up to.
231
+ const markRuns = (current, runs) => {
232
+ const history = current.history ?? [];
233
+ for (const run of runs) {
234
+ if (run.step || !run.reported_at) continue;
235
+ const entry = history.findLast(
236
+ (each) =>
237
+ each.at < run.reported_at && each.agents.includes(run.target_agent)
238
+ );
239
+ if (!entry) continue;
240
+ store.setRunStep(run.id, entry);
241
+ run.step = entry.step;
242
+ run.specialty = entry.specialty;
243
+ }
244
+ };
80
245
 
81
- /** The thread's plan, or null when it has none (a thread from before teams, or one not yet assembled). */
246
+ /** The thread's plan, or null when it has none (a thread from before routes, or one not yet triaged). */
82
247
  const plan = (goalId) => read(goalId);
83
248
 
84
- /** The roles an agent holds in the thread. */
249
+ /** The roles an agent holds in the thread's piece now. */
85
250
  const rolesOf = (goalId, agent) => {
86
251
  const current = read(goalId);
87
252
  return current
88
- ? Object.entries(current.roles)
89
- .filter(([, holders]) => holders.includes(agent))
90
- .map(([role]) => role)
253
+ ? stepsOfNow(current)
254
+ .filter((step) => step.agents.includes(agent))
255
+ .map((step) => step.role)
91
256
  : [];
92
257
  };
93
258
 
94
- /** One line naming who holds what, as the thread and the briefs say it. */
95
- const lineup = (current) =>
96
- stepsOf(current)
97
- .map((step) => step.role)
98
- .filter((role, index, all) => all.indexOf(role) === index)
99
- .map(
100
- (role) =>
101
- `${current.roles[role].map(nameOf).join(" and ")}: ${TEAM.roles[role].name}`
102
- )
103
- .join("; ");
259
+ /** The step of the route the agent was last given in the thread, or null in a thread without a route. */
260
+ const stepHeld = (goalId, agent) =>
261
+ read(goalId)?.history?.findLast((entry) => entry.agents.includes(agent))
262
+ ?.step ?? null;
263
+
264
+ /** The step of the piece now that the agent holds because the person pinned it for the step's specialty, if any. */
265
+ const pinnedStep = (goalId, agent) => {
266
+ const current = read(goalId);
267
+ const step = current
268
+ ? stepsOfNow(current).find((s) => s.id === current.step)
269
+ : null;
270
+ return step?.pinned && step.agents.includes(agent) ? step : null;
271
+ };
104
272
 
105
273
  /**
106
- * Keep the plan the round proposed, starting at its first step. Says in the thread who holds what. Returns the plan
274
+ * Keep the route the round proposed, starting at its first step, and say in the thread who holds what. `named`: the
275
+ * agents the person tagged in the message; `pins`: specialty -> the agent the person pinned for it. Returns the plan
107
276
  * kept, or null when the proposal named nothing usable.
108
277
  */
109
- const set = (goalId, proposed, { members, by, post }) => {
110
- const valid = validPlan(proposed, { members });
111
- if (!valid) {
112
- log(`team plan for ${goalId} not kept: ${JSON.stringify(proposed)}`);
278
+ const set = (
279
+ goalId,
280
+ proposed,
281
+ { members, named = [], pins = {}, by, post }
282
+ ) => {
283
+ const checked = validPlan(proposed, { members, named, pins });
284
+ if (!checked) {
285
+ log(`route for ${goalId} not kept: ${JSON.stringify(proposed)}`);
113
286
  return null;
114
287
  }
115
- const kept = {
116
- ...valid,
117
- step: stepsOf(valid)[0].id,
118
- stepAt: now(),
288
+ const { unfilled, steps, rigor, split } = checked;
289
+ const fresh = {
290
+ version: 2,
291
+ rigor,
292
+ split,
293
+ pieces: [{ brief: null, rigor, steps }],
294
+ piece: 0,
295
+ step: steps[0].id,
119
296
  sendsBack: 0,
120
297
  by,
121
298
  at: now(),
122
299
  };
300
+ const kept = {
301
+ ...fresh,
302
+ stepAt: now(),
303
+ history: started(fresh, steps[0].id),
304
+ };
123
305
  write(goalId, kept);
124
306
  post?.(
125
- `The team for this thread (${SHAPES[kept.shape].name}, ${kept.rigor}): ${lineup(kept)}.`
307
+ [
308
+ `The route for this thread (${rigor}): ${lineup(steps)}.`,
309
+ ...(split
310
+ ? [
311
+ "The framer splits it into pieces, and each piece is routed when it starts.",
312
+ ]
313
+ : []),
314
+ ...unfilled.map(unfilledLine),
315
+ ].join(" ")
126
316
  );
127
317
  return kept;
128
318
  };
129
319
 
130
320
  /**
131
- * Move the thread to a step of its route (an id, "done"). A judged step's outcome is kept with the commit its
132
- * check saw (`head`), so a later change makes it stale; a send-back is counted.
321
+ * The next piece the framer split off, routed as the round proposed: it starts at its first step, and the thread says
322
+ * who holds what. A check that passed the piece before is kept with the commit it saw.
323
+ */
324
+ const routePiece = (
325
+ goalId,
326
+ proposed,
327
+ { members, named = [], pins = {}, outcome = null, head = null, post }
328
+ ) => {
329
+ const current = read(goalId);
330
+ const index = current ? current.piece + 1 : -1;
331
+ if (!current || !current.pieces[index]) return null;
332
+ const checked = validPlan(proposed, { members, named, pins, piece: true });
333
+ if (!checked) {
334
+ log(`route for piece ${index + 1} of ${goalId} not kept`);
335
+ return null;
336
+ }
337
+ judge(goalId, current, outcome, head);
338
+ const after = read(goalId);
339
+ const pieces = after.pieces.map((each, at) =>
340
+ at === index
341
+ ? { ...each, rigor: checked.rigor, steps: checked.steps }
342
+ : each
343
+ );
344
+ const moved = {
345
+ ...after,
346
+ pieces,
347
+ piece: index,
348
+ step: checked.steps[0].id,
349
+ stepAt: now(),
350
+ sendsBack: 0,
351
+ };
352
+ moved.history = started(moved, moved.step);
353
+ write(goalId, moved);
354
+ // The framing piece is not one of the person's pieces: the count is of the split's.
355
+ const of = pieces.length - 1;
356
+ post?.(
357
+ [
358
+ `Piece ${index} of ${of} (${checked.rigor}): ${lineup(checked.steps)}.`,
359
+ ...checked.unfilled.map(unfilledLine),
360
+ ].join(" ")
361
+ );
362
+ return moved;
363
+ };
364
+
365
+ /**
366
+ * A judged step's outcome: kept with the commit its check saw (`head`), so a later change makes it stale, and written
367
+ * on the runs made since the piece's make step last began, with each checker's model, for the record.
368
+ */
369
+ const judge = (goalId, current, outcome, head) => {
370
+ const step = stepsOfNow(current).find((s) => s.id === current.step);
371
+ if (!step || !stepOf(step.id).judges) return;
372
+ if (outcome !== "passed" && outcome !== "sent_back") return;
373
+ const runs = store.threadRuns(goalId);
374
+ markRuns(current, runs);
375
+ const latest = (agent) =>
376
+ runs.findLast(
377
+ (run) => run.target_agent === agent && run.step === current.step
378
+ );
379
+ const by = step.agents.map((agent) => ({
380
+ agent,
381
+ model: latest(agent)?.run_model?.model ?? null,
382
+ }));
383
+ // The runs this judgment is of: the piece's make runs no earlier step judged. A run a tag started before the route
384
+ // began is the piece's too; a run sent back keeps that outcome, and the run made again takes this one.
385
+ const pieceAt =
386
+ (current.history ?? []).find((entry) => entry.piece === current.piece)
387
+ ?.at ?? "";
388
+ for (const run of runs)
389
+ if (
390
+ run.step === "make" &&
391
+ !run.checked &&
392
+ (run.reported_at ?? "") > pieceAt &&
393
+ FINISHED.has(run.status)
394
+ )
395
+ store.setRunChecked(run.id, {
396
+ outcome,
397
+ step: current.step,
398
+ by,
399
+ at: now(),
400
+ });
401
+ write(goalId, {
402
+ ...current,
403
+ lastCheck: { step: current.step, outcome, head, at: now() },
404
+ });
405
+ };
406
+
407
+ /**
408
+ * Move the thread to a step of its piece's route (an id, "done"). A judged step's outcome is kept (judge); a
409
+ * send-back is counted.
133
410
  */
134
411
  const moveTo = (goalId, step, { outcome = null, head = null } = {}) => {
135
412
  const current = read(goalId);
136
413
  if (!current) return null;
137
414
  const known =
138
- step === "done" || stepsOf(current).some((s) => s.id === step);
415
+ step === "done" || stepsOfNow(current).some((s) => s.id === step);
139
416
  if (!known) return current;
140
- const judged = stepsOf(current).find((s) => s.id === current.step)?.judges;
417
+ judge(goalId, current, outcome, head);
418
+ const after = read(goalId);
141
419
  const moved = {
142
- ...current,
420
+ ...after,
143
421
  step,
144
422
  stepAt: now(),
145
- sendsBack: current.sendsBack + (outcome === "sent_back" ? 1 : 0),
146
- ...(judged && (outcome === "passed" || outcome === "sent_back")
147
- ? { lastCheck: { step: current.step, outcome, head, at: now() } }
148
- : {}),
423
+ sendsBack: after.sendsBack + (outcome === "sent_back" ? 1 : 0),
149
424
  };
425
+ moved.history = started(moved, step);
150
426
  write(goalId, moved);
151
427
  return moved;
152
428
  };
153
429
 
154
- /** Whether the thread's team checks its work: its route has a step that judges. */
155
- const checksWork = (current) => stepsOf(current).some((s) => s.judges);
430
+ /** Whether the thread's route checks its work: one of its pieces has a step that judges. */
431
+ const checksWork = (current) =>
432
+ current.pieces.some((piece) =>
433
+ piece.steps.some((step) => stepOf(step.id).judges)
434
+ );
156
435
 
157
436
  /**
158
- * Whether the team's last check passed on the code as it is now (`head`, the thread's branch; null for a thread
159
- * with no branch of its own, where the pass alone counts). True for a team that does not check its work.
437
+ * Whether the route's last check passed on the code as it is now (`head`, the thread's branch; null for a thread
438
+ * with no branch of its own, where the pass alone counts). True for a route that does not check its work.
160
439
  */
161
440
  const checkedNow = (goalId, head = null) => {
162
441
  const current = read(goalId);
@@ -169,22 +448,23 @@ export function createTeam({ store, log = () => {} }) {
169
448
 
170
449
  /**
171
450
  * Agents were just given work outside the route (the person wrote to them, or a free round picked one): the thread
172
- * moves to the earliest step one of them holds, so the route goes on from there.
451
+ * moves to the earliest step of the piece one of them holds, so the route goes on from there.
173
452
  */
174
453
  const acted = (goalId, agents) => {
175
454
  const current = read(goalId);
176
455
  if (!current || !agents.length) return;
177
- const step = stepsOf(current).find((s) =>
178
- current.roles[s.role].some((holder) => agents.includes(holder))
456
+ const step = stepsOfNow(current).find((s) =>
457
+ s.agents.some((holder) => agents.includes(holder))
179
458
  );
180
459
  if (step && step.id !== current.step) moveTo(goalId, step.id);
181
- else if (step) write(goalId, { ...current, stepAt: now() });
460
+ else if (step)
461
+ write(goalId, {
462
+ ...current,
463
+ stepAt: now(),
464
+ history: started(current, step.id),
465
+ });
182
466
  };
183
467
 
184
- /**
185
- * What the round reads about the team: the plan, the step the thread is at, whether everyone on that step has reported
186
- * since it began, and where the work may go next. `runs` are the thread's runs (store.threadRuns).
187
- */
188
468
  // When every holder of a step reported after `since`: the last of their reports, else null.
189
469
  const reportedAll = (holders, runs, since) => {
190
470
  const last = holders.map(
@@ -209,28 +489,104 @@ export function createTeam({ store, log = () => {} }) {
209
489
  * whose next step's agents have reported since moves on, up to a step that judges, whose outcome is the round's.
210
490
  */
211
491
  const catchUp = (goalId, current, runs) => {
212
- const steps = stepsOf(current);
492
+ const steps = stepsOfNow(current);
213
493
  let caught = current;
214
494
  for (;;) {
215
495
  const at = steps.findIndex((s) => s.id === caught.step);
216
496
  const step = steps[at];
217
497
  const next = steps[at + 1];
218
- if (!step || step.judges || !next) break;
219
- const done = reportedAll(current.roles[step.role], runs, caught.stepAt);
220
- if (!done || !reportedAll(current.roles[next.role], runs, done)) break;
498
+ if (!step || stepOf(step.id).judges || !next) break;
499
+ const done = reportedAll(step.agents, runs, caught.stepAt);
500
+ if (!done || !reportedAll(next.agents, runs, done)) break;
221
501
  caught = { ...caught, step: next.id, stepAt: done };
502
+ caught.history = [
503
+ ...(caught.history ?? []),
504
+ {
505
+ piece: caught.piece,
506
+ step: next.id,
507
+ specialty: next.specialty,
508
+ agents: next.agents,
509
+ at: done,
510
+ },
511
+ ];
222
512
  }
223
513
  if (caught !== current) write(goalId, caught);
224
514
  return caught;
225
515
  };
226
516
 
517
+ // The framer's report on a goal it split: its runs' results since its step began, else its posts in the thread.
518
+ const framerSaid = (goalId, current, runs) => {
519
+ const framers = stepsOfNow(current).find((s) => s.id === "frame")?.agents;
520
+ const since = current.stepAt;
521
+ const results = runs
522
+ .filter(
523
+ (run) =>
524
+ framers?.includes(run.target_agent) && (run.reported_at ?? "") > since
525
+ )
526
+ .map((run) => String(run.result || ""));
527
+ const posts = store
528
+ .roomPosts(goalId, 0, 500)
529
+ .filter(
530
+ (post) => framers?.includes(post.author) && post.created_at > since
531
+ )
532
+ .map((post) => post.text);
533
+ // Its result, else its posts: an agent's result is also posted, and its pieces are read once.
534
+ return (
535
+ results.some((text) => piecesIn(text).length) ? results : posts
536
+ ).join("\n");
537
+ };
538
+
539
+ // A split goal's frame is done: the pieces its framer gave are kept, to be routed one after another. A framer that gave
540
+ // none leaves one piece, the goal as it framed it.
541
+ const splitInto = (goalId, current, runs) => {
542
+ if (!current.split || current.pieces.length > 1) return current;
543
+ const briefs = piecesIn(framerSaid(goalId, current, runs));
544
+ const pieces = [
545
+ ...current.pieces,
546
+ ...(briefs.length ? briefs : [null]).map((brief) => ({
547
+ brief: brief ? brief.slice(0, BRIEF_CHARS) : null,
548
+ rigor: null,
549
+ steps: [],
550
+ })),
551
+ ];
552
+ const kept = { ...current, pieces };
553
+ write(goalId, kept);
554
+ return kept;
555
+ };
556
+
557
+ /**
558
+ * What the round reads about the route: the piece and its steps, the step the thread is at, whether everyone on it
559
+ * has reported since it began, and where the work may go next: a step of the piece, "done", "stop" (sent back as
560
+ * often as the route allows), or "piece" (the next piece, which the round routes at once: `nextPiece`).
561
+ * `runs` are the thread's runs (store.threadRuns).
562
+ */
227
563
  const roundView = (goalId, runs, { head = null } = {}) => {
228
564
  const kept = read(goalId);
229
565
  if (!kept) return null;
230
- const current = catchUp(goalId, kept, runs);
231
- const steps = stepsOf(current);
566
+ markRuns(kept, runs);
567
+ let current = catchUp(goalId, kept, runs);
568
+ const steps = stepsOfNow(current);
569
+ const piece = pieceOf(current);
570
+ const base = {
571
+ rigor: piece.rigor ?? current.rigor,
572
+ piece:
573
+ current.piece > 0
574
+ ? {
575
+ number: current.piece,
576
+ of: current.pieces.length - 1,
577
+ brief: piece.brief,
578
+ }
579
+ : null,
580
+ steps: steps.map(({ id, specialty, role, agents }) => ({
581
+ id,
582
+ specialty,
583
+ role,
584
+ agents,
585
+ })),
586
+ sendsBack: current.sendsBack,
587
+ };
232
588
  // The route is done but the code changed after its last check passed: the work goes to the check again.
233
- const firstJudge = steps.findIndex((s) => s.judges);
589
+ const firstJudge = steps.findIndex((s) => stepOf(s.id).judges);
234
590
  const recheck =
235
591
  current.step === "done" &&
236
592
  firstJudge > 0 &&
@@ -239,41 +595,31 @@ export function createTeam({ store, log = () => {} }) {
239
595
  Boolean(current.lastCheck.head) &&
240
596
  current.lastCheck.head !== head;
241
597
  if (recheck) {
242
- const [made, judge] = [steps[firstJudge - 1], steps[firstJudge]];
598
+ const [made, judged] = [steps[firstJudge - 1], steps[firstJudge]];
243
599
  return {
244
- shape: current.shape,
245
- rigor: current.rigor,
246
- roles: current.roles,
247
- sendsBack: current.sendsBack,
600
+ ...base,
248
601
  step: {
249
602
  id: made.id,
250
603
  role: made.role,
251
- agents: current.roles[made.role],
604
+ specialty: made.specialty,
605
+ agents: made.agents,
252
606
  judges: false,
253
607
  finished: true,
254
608
  },
255
609
  exits: [
256
610
  {
257
611
  outcome: "done",
258
- step: judge.id,
259
- role: judge.role,
260
- agents: current.roles[judge.role],
612
+ step: judged.id,
613
+ role: judged.role,
614
+ agents: judged.agents,
261
615
  },
262
616
  ],
263
617
  };
264
618
  }
265
619
  const at = steps.findIndex((s) => s.id === current.step);
266
- const view = {
267
- shape: current.shape,
268
- rigor: current.rigor,
269
- roles: current.roles,
270
- sendsBack: current.sendsBack,
271
- step: null,
272
- exits: [],
273
- };
620
+ const view = { ...base, step: null, exits: [] };
274
621
  if (at === -1) return view;
275
622
  const step = steps[at];
276
- const holders = current.roles[step.role];
277
623
  const reportedSince = (agent) =>
278
624
  runs.some(
279
625
  (run) =>
@@ -281,23 +627,35 @@ export function createTeam({ store, log = () => {} }) {
281
627
  FINISHED.has(run.status) &&
282
628
  (run.reported_at ?? "") > current.stepAt
283
629
  );
284
- const finished = holders.every(reportedSince);
630
+ const finished = step.agents.every(reportedSince);
631
+ const judges = Boolean(stepOf(step.id).judges);
285
632
  view.step = {
286
633
  id: step.id,
287
634
  role: step.role,
288
- agents: holders,
289
- judges: Boolean(step.judges),
635
+ specialty: step.specialty,
636
+ agents: step.agents,
637
+ judges,
290
638
  finished,
291
639
  };
292
640
  if (!finished) return view;
641
+ if (!steps[at + 1]) current = splitInto(goalId, current, runs);
293
642
  const following = steps[at + 1];
643
+ const nextPiece = current.pieces[current.piece + 1];
644
+ if (!following && nextPiece)
645
+ view.nextPiece = {
646
+ number: current.piece + 1,
647
+ of: current.pieces.length - 1,
648
+ brief: nextPiece.brief,
649
+ };
294
650
  const toStep = (s, outcome) =>
295
651
  s
296
- ? { outcome, step: s.id, role: s.role, agents: current.roles[s.role] }
297
- : { outcome, step: "done", role: null, agents: [] };
298
- if (!step.judges) view.exits = [toStep(following, "done")];
652
+ ? { outcome, step: s.id, role: s.role, agents: s.agents }
653
+ : nextPiece
654
+ ? { outcome, step: "piece", role: null, agents: [] }
655
+ : { outcome, step: "done", role: null, agents: [] };
656
+ if (!judges) view.exits = [toStep(following, "done")];
299
657
  else {
300
- const back = steps.find((s) => s.id === step.sendBack);
658
+ const back = steps.find((s) => s.id === stepOf(step.id).sendBack);
301
659
  view.exits = [
302
660
  toStep(following, "passed"),
303
661
  back && current.sendsBack < TEAM.sendBackLimit
@@ -342,52 +700,82 @@ export function createTeam({ store, log = () => {} }) {
342
700
  `${nameOf(note.agent)}, ${age(note.created_at)} (note ${note.id.slice(0, 8)}, unchecked: verify before relying on it): ${note.text}, because ${note.reason}`
343
701
  );
344
702
 
345
- /** The brief's section on the team, for an agent woken in a thread with a plan. `model`: the agent's model, if known. */
346
- const brief = (goalId, agent, { model = null } = {}) => {
703
+ // What a step's holder is told beyond its role's contract: the framer whether to split, a maker who checks its work.
704
+ const stepNote = (current, step) => {
705
+ const steps = stepsOfNow(current);
706
+ if (step.id === "frame")
707
+ return current.split && current.piece === 0
708
+ ? 'This goal is to be split: give each piece on its own line as "Piece:" followed by its brief, in the order they should run. Each piece is routed to its agents when it starts.'
709
+ : "This is one piece: say what it is and what good looks like; do not split it.";
710
+ if (step.id !== "make") return null;
711
+ return steps.some((s) => s.id === "check")
712
+ ? "Another agent checks your work after you: test it yourself as well, and say what you ran."
713
+ : "No other agent checks this at this rigor: test your own work before you report, and say what you ran.";
714
+ };
715
+
716
+ // A check or judgment is of the change as committed on the thread's branch: one git would not commit (threadRuns.mjs)
717
+ // is said to whoever judges next, so it is not passed as made.
718
+ const uncommittedNote = (goalId, step) => {
719
+ if (!stepOf(step.id).judges) return null;
720
+ let left = null;
721
+ try {
722
+ left = JSON.parse(store.meta(`uncommitted:${goalId}`) || "null");
723
+ } catch {}
724
+ if (!left?.files?.length) return null;
725
+ return `Not made yet: ${nameOf(left.agent)}'s change is not committed on the thread's branch (${namedFiles(left.files)} still uncommitted). Check the change as committed, and send back work that is not.`;
726
+ };
727
+
728
+ /** The brief's section on the route, for an agent woken in a thread with one. Every holder gets the same brief. */
729
+ const brief = (goalId, agent) => {
347
730
  const current = read(goalId);
348
731
  if (!current) return "";
349
- const steps = stepsOf(current);
350
- // The role the agent acts in now comes first: the one its current step is for, when it holds several.
351
- const now = steps.find((s) => s.id === current.step)?.role;
352
- const mine = rolesOf(goalId, agent).sort(
353
- (a, b) => Number(b === now) - Number(a === now)
354
- );
732
+ const steps = stepsOfNow(current);
733
+ const piece = pieceOf(current);
734
+ // The step the agent acts in now comes first: the thread's current step, when it holds several.
735
+ const mine = steps
736
+ .filter((step) => step.agents.includes(agent))
737
+ .sort(
738
+ (a, b) => Number(b.id === current.step) - Number(a.id === current.step)
739
+ );
740
+ const rigor = piece.rigor ?? current.rigor;
355
741
  const lines = [
356
- `The team (${SHAPES[current.shape].name}): ${lineup(current)}.`,
357
- `How careful this work must be: ${current.rigor}. ${TEAM.rigor[current.rigor]}`,
742
+ `The route${current.piece > 0 ? ` for piece ${current.piece} of ${current.pieces.length - 1}` : ""} (${rigor}): ${lineup(steps)}.`,
743
+ ...(current.piece > 0 && piece.brief
744
+ ? [`This piece, as the framer gave it: ${piece.brief}`]
745
+ : []),
746
+ `How careful this work must be: ${rigor}. ${TEAM.rigor[rigor].says}`,
358
747
  ];
359
748
  const folder = store.getTask(goalId)?.goal?.workspace ?? null;
360
749
  const learned = [];
361
- for (const [index, role] of mine.entries()) {
362
- const contract = TEAM.roles[role];
750
+ for (const [index, step] of mine.entries()) {
751
+ const contract = TEAM.roles[step.role];
363
752
  if (index > 0) {
364
- lines.push(`You are also its ${contract.name}, at a later step.`);
753
+ lines.push(
754
+ `You are also its ${contract.name}, at the ${step.id} step.`
755
+ );
365
756
  continue;
366
757
  }
367
- learned.push(...notesFor(role, folder));
368
- const at = steps.findIndex((s) => s.role === role);
369
- const next = steps[at + 1];
758
+ learned.push(...notesFor(step.role, folder));
759
+ const next = steps[steps.indexOf(step) + 1];
370
760
  lines.push(
371
- `Your role now: ${contract.name}. ${contract.owns}`,
761
+ `Your role now: ${contract.name}${step.specialty ? `, for ${specialtyName(step.specialty)}` : ""}. ${contract.owns}`,
372
762
  `Never: ${contract.never}`,
373
763
  `Bring back: ${contract.bringsBack}`,
764
+ ...[stepNote(current, step), uncommittedNote(goalId, step)].filter(
765
+ Boolean
766
+ ),
374
767
  next
375
- ? `Next on the team: ${current.roles[next.role].map(nameOf).join(" and ")} (${TEAM.roles[next.role].name}).`
376
- : "After you, the thread's coordinator reads your result and decides whether the goal is reached. It is not an agent: do not address it, or Harness, to hand the work back."
768
+ ? `Next on the route: ${next.agents.map(nameOf).join(" and ")} (${TEAM.roles[next.role].name}).`
769
+ : "After you, the thread's coordinator reads your result and decides what comes next. It is not an agent: do not address it, or Harness, to hand the work back."
377
770
  );
378
771
  }
379
772
  if (!mine.length)
380
773
  lines.push(
381
- "You hold no role on this team: do what you were asked and hand anything else to the agent whose role it is."
382
- );
383
- // Goal-level models get the intent and find the route; others do their best work on smaller checked steps.
384
- if (!goalLevel(model))
385
- lines.push(
386
- "Work in small steps: do one part, check that it works, then the next, and say which parts are done."
774
+ "You hold no step on this route: do what you were asked and hand anything else to the agent whose step it is."
387
775
  );
388
776
  if (mine.length)
389
777
  lines.push(
390
- `Before you stop: if you learned something about this folder that the next ${TEAM.roles[mine[0]].name} here should know (a trap, a check that matters, how the code really works), save it with save_learning (id ${goalId}): what you learned and why it holds.`
778
+ `Before you stop: if you learned something about this folder that the next ${TEAM.roles[mine[0].role].name} here should know (a trap, a check that matters, how the code really works), add it with swarm_memory (remember, why, thread ${goalId}): what you learned and why it holds.`
391
779
  );
392
780
  return [
393
781
  "Your part on the team:",
@@ -406,6 +794,7 @@ export function createTeam({ store, log = () => {} }) {
406
794
  return {
407
795
  plan,
408
796
  set,
797
+ routePiece,
409
798
  moveTo,
410
799
  acted,
411
800
  roundView,
@@ -413,5 +802,7 @@ export function createTeam({ store, log = () => {} }) {
413
802
  learn,
414
803
  brief,
415
804
  rolesOf,
805
+ stepHeld,
806
+ pinnedStep,
416
807
  };
417
808
  }