tryharness 0.7.53 → 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 (69) hide show
  1. package/README.md +10 -5
  2. package/assets/dsh-plugin.mjs +72 -0
  3. package/assets/opencode-plugin.js +4 -3
  4. package/cli.mjs +115 -36
  5. package/lib/adapters.mjs +157 -222
  6. package/lib/agentRuntimes.mjs +26 -16
  7. package/lib/agentSignIn.mjs +226 -0
  8. package/lib/audit.mjs +643 -0
  9. package/lib/backupSetup.mjs +141 -0
  10. package/lib/boardAccess.mjs +8 -1
  11. package/lib/boardBackup.mjs +283 -0
  12. package/lib/boardMirror.mjs +71 -7
  13. package/lib/boardProtocol.json +59 -2
  14. package/lib/boardWaits.mjs +102 -0
  15. package/lib/book.mjs +659 -0
  16. package/lib/bridge.mjs +1 -1
  17. package/lib/cards.mjs +138 -28
  18. package/lib/cloudBoot.mjs +124 -0
  19. package/lib/commands.mjs +7 -2
  20. package/lib/context.mjs +18 -4
  21. package/lib/contractGate.mjs +377 -0
  22. package/lib/coordinator.mjs +149 -43
  23. package/lib/daemon.mjs +349 -28
  24. package/lib/desk.mjs +239 -0
  25. package/lib/desktop.mjs +106 -6
  26. package/lib/environments.mjs +237 -7
  27. package/lib/folders.mjs +2 -2
  28. package/lib/gitHost.mjs +174 -0
  29. package/lib/hookDialects.mjs +298 -0
  30. package/lib/landed.mjs +887 -0
  31. package/lib/live.mjs +284 -13
  32. package/lib/mcp-stdio.mjs +68 -2
  33. package/lib/members.json +16 -2
  34. package/lib/models.mjs +8 -8
  35. package/lib/museSession.mjs +16 -13
  36. package/lib/nativeHost.mjs +1 -1
  37. package/lib/neovim.mjs +126 -0
  38. package/lib/nudge.mjs +47 -16
  39. package/lib/ownMachines.mjs +343 -39
  40. package/lib/pairing.mjs +27 -10
  41. package/lib/paths.mjs +4 -0
  42. package/lib/personModel.mjs +89 -0
  43. package/lib/phones.mjs +66 -11
  44. package/lib/places.mjs +330 -10
  45. package/lib/readiness.mjs +135 -18
  46. package/lib/record.mjs +115 -0
  47. package/lib/runWorker.mjs +17 -2
  48. package/lib/runner.mjs +8 -4
  49. package/lib/sessionControl.mjs +377 -89
  50. package/lib/specialties.json +74 -0
  51. package/lib/specialties.mjs +477 -0
  52. package/lib/specialtyKeys.d.ts +3 -0
  53. package/lib/specialtyKeys.js +58 -0
  54. package/lib/specialtyNotice.mjs +147 -0
  55. package/lib/specialtySnapshot.mjs +138 -0
  56. package/lib/store.mjs +284 -1
  57. package/lib/swarm.mjs +137 -21
  58. package/lib/t3.mjs +13 -4
  59. package/lib/taskCommands.mjs +36 -9
  60. package/lib/taskRuns.mjs +9 -2
  61. package/lib/team.json +255 -148
  62. package/lib/team.mjs +529 -188
  63. package/lib/threadLine.mjs +116 -0
  64. package/lib/threadRuns.mjs +75 -11
  65. package/lib/threadView.mjs +15 -2
  66. package/lib/worktrees.mjs +51 -13
  67. package/package.json +4 -4
  68. package/lib/modelTiers.json +0 -1183
  69. package/lib/modelTiers.mjs +0 -133
package/lib/team.mjs CHANGED
@@ -1,105 +1,188 @@
1
1
  import { readFileSync } from "node:fs";
2
2
  import { nameOf } from "./members.mjs";
3
- import { meetsFloor } from "./modelTiers.mjs";
3
+ import { specialtyName } from "./specialties.mjs";
4
+ import { namedFiles } from "./worktrees.mjs";
4
5
 
5
- // 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
6
- // shape, who holds each role, how careful the work must be, and the route the work takes between the roles. Without one,
7
- // every round handed the next piece to whichever single agent looked likeliest, and the busiest agent did most of the
8
- // work while nobody checked it or pulled parallel results together (board, October 20: 30 of 52 hand-ons to one agent,
9
- // 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).
10
12
  //
11
- // The plan is set in the thread's first assembly round (src/server/swarm/coordinator.ts teamPlan) and kept here. The
12
- // route is followed here: the round is told which step the thread is at, whether that step's agents have reported since
13
- // it began, and where the work may go next (`exits`); Jev judges only what needs judgment (did a check pass), and the
14
- // 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).
15
20
 
16
21
  export const TEAM = JSON.parse(
17
22
  readFileSync(new URL("./team.json", import.meta.url), "utf8")
18
23
  );
19
- const SHAPES = TEAM.shapes;
20
- export const SHAPE_IDS = Object.keys(SHAPES);
24
+ const ROUTE = TEAM.route;
25
+ export const STEP_IDS = ROUTE.map((step) => step.id);
21
26
  export const ROLE_IDS = Object.keys(TEAM.roles);
22
27
  export const RIGOR_LEVELS = Object.keys(TEAM.rigor);
28
+ const stepOf = (id) => ROUTE.find((step) => step.id === id);
23
29
  // Every runner's reported states that end a turn: a question to the person (needs_input) is not the step's end.
24
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;
25
33
 
26
- /** Whether an agent takes a goal and finds its own route (the brief gives it intent): a top-tier model. */
27
- export const goalLevel = (tier) => tier === "top";
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;
28
39
 
29
- const stepsOf = (plan) =>
30
- (SHAPES[plan.shape]?.steps ?? []).filter(
31
- (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")
32
50
  );
33
51
 
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
+
34
63
  /**
35
- * A plan as the round proposed it, kept only where it names a known shape, known roles and agents that may hold them:
36
- * agents of the swarm whose tier meets the role's floor (`tiers`: agent id -> tier, from its card, modelTiers.mjs). Who
37
- * was left out for the floor comes back in `leftOut`, so the thread says why.
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`).
38
70
  */
39
- export function validPlan(proposed, { members, tiers = {} }) {
40
- const shape = SHAPES[proposed?.shape];
41
- if (!shape) return null;
71
+ export function validPlan(
72
+ proposed,
73
+ { members, named = [], pins = {}, piece = false }
74
+ ) {
42
75
  const allowed = new Set(members);
43
- const roles = {};
44
- const leftOut = [];
45
- for (const role of shape.roles) {
46
- const named = [
47
- ...new Set((proposed.roles?.[role] ?? []).map(String)),
48
- ].filter((agent) => allowed.has(agent));
49
- const floor = TEAM.roles[role].floor ?? "any";
50
- const holders = named.filter((agent) => {
51
- const fits = meetsFloor(tiers[agent]?.level ?? "unknown", floor);
52
- if (!fits) leftOut.push({ role, agent, floor, tier: tiers[agent] });
53
- return fits;
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
+ ];
54
153
  });
55
- if (holders.length)
56
- roles[role] = TEAM.several.includes(role) ? holders : holders.slice(0, 1);
57
- }
58
- // The author never checks or reviews its own work.
59
- const builder = roles.builder?.[0];
60
- for (const role of ["checker", "reviewer"])
61
- if (builder && roles[role]?.[0] === builder) delete roles[role];
62
- const plan = {
63
- shape: proposed.shape,
64
- rigor: RIGOR_LEVELS.includes(proposed.rigor) ? proposed.rigor : "standard",
65
- roles,
66
- };
67
- // A role of the shape nobody holds because no agent here meets its floor: the thread says so, with who fell short.
68
- const unfilled = shape.roles
69
- .filter(
70
- (role) => !roles[role] && (TEAM.roles[role].floor ?? "any") !== "any"
71
- )
72
- .map((role) => ({
73
- role,
74
- floor: TEAM.roles[role].floor,
75
- short: members
76
- .filter(
77
- (agent) =>
78
- !meetsFloor(
79
- tiers[agent]?.level ?? "unknown",
80
- TEAM.roles[role].floor
81
- )
82
- )
83
- .map((agent) => ({ agent, level: tiers[agent]?.level ?? "unknown" })),
84
- }))
85
- .filter(
86
- (each) => each.short.length && each.short.length === members.length
87
- );
88
- return stepsOf(plan).length ? { ...plan, leftOut, unfilled } : null;
154
+ if (!kept.length) return null;
155
+ if (!split && !kept.some((step) => step.id === "make")) return null;
156
+ return { rigor, split, steps: kept, unfilled };
89
157
  }
90
158
 
91
- // Only the agents whose tier is known are named: an agent with no model on its card (Bankr, Grok Bot) says nothing here.
92
- const unfilledLine = ({ role, floor, short }) => {
93
- const known = short.filter(({ level }) => level !== "unknown");
94
- const named = known.length
95
- ? ` (${known.map(({ agent, level }) => `${nameOf(agent)} ${level}`).join(", ")})`
96
- : "";
97
- return `This team has no ${TEAM.roles[role].name}: a ${TEAM.roles[role].name} runs on a ${floor}-tier model, which no agent here does${named}. To change that: npx tryharness agents tier ${known[0]?.agent ?? "<agent>"} ${floor}.`;
98
- };
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("; ");
99
167
 
100
- // The thread's words for an agent kept off a role by its floor, with what the person can do about it.
101
- const leftOutLine = ({ role, agent, floor, tier }) =>
102
- `${nameOf(agent)} is not the ${TEAM.roles[role].name}: a ${TEAM.roles[role].name} runs on a ${floor}-tier model and ${nameOf(agent)}'s is ${tier?.level ?? "unknown"} (${tier?.because ?? "no tier"}). To change that: npx tryharness agents tier ${agent} ${floor}.`;
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);
103
186
 
104
187
  // How old a note is, in the words a brief uses.
105
188
  const age = (iso) => {
@@ -107,70 +190,127 @@ const age = (iso) => {
107
190
  return days < 1 ? "today" : days === 1 ? "yesterday" : `${days} days ago`;
108
191
  };
109
192
  const NOTE_CHARS = 600;
193
+ const BRIEF_CHARS = 1_000;
110
194
 
111
195
  export function createTeam({ store, log = () => {} }) {
112
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.
113
198
  const read = (goalId) => {
114
199
  try {
115
- 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;
116
202
  } catch {
117
203
  return null;
118
204
  }
119
205
  };
120
206
  const write = (goalId, plan) => store.meta(key(goalId), JSON.stringify(plan));
121
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
+ };
122
227
 
123
- /** The thread's plan, or null when it has none (a thread from before teams, or one not yet assembled). */
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
+ };
245
+
246
+ /** The thread's plan, or null when it has none (a thread from before routes, or one not yet triaged). */
124
247
  const plan = (goalId) => read(goalId);
125
248
 
126
- /** The roles an agent holds in the thread. */
249
+ /** The roles an agent holds in the thread's piece now. */
127
250
  const rolesOf = (goalId, agent) => {
128
251
  const current = read(goalId);
129
252
  return current
130
- ? Object.entries(current.roles)
131
- .filter(([, holders]) => holders.includes(agent))
132
- .map(([role]) => role)
253
+ ? stepsOfNow(current)
254
+ .filter((step) => step.agents.includes(agent))
255
+ .map((step) => step.role)
133
256
  : [];
134
257
  };
135
258
 
136
- /** One line naming who holds what, as the thread and the briefs say it. */
137
- const lineup = (current) =>
138
- stepsOf(current)
139
- .map((step) => step.role)
140
- .filter((role, index, all) => all.indexOf(role) === index)
141
- .map(
142
- (role) =>
143
- `${current.roles[role].map(nameOf).join(" and ")}: ${TEAM.roles[role].name}`
144
- )
145
- .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
+ };
146
272
 
147
273
  /**
148
- * 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
149
276
  * kept, or null when the proposal named nothing usable.
150
277
  */
151
- const set = (goalId, proposed, { members, tiers, by, post }) => {
152
- const checked = validPlan(proposed, { members, tiers });
278
+ const set = (
279
+ goalId,
280
+ proposed,
281
+ { members, named = [], pins = {}, by, post }
282
+ ) => {
283
+ const checked = validPlan(proposed, { members, named, pins });
153
284
  if (!checked) {
154
- log(`team plan for ${goalId} not kept: ${JSON.stringify(proposed)}`);
285
+ log(`route for ${goalId} not kept: ${JSON.stringify(proposed)}`);
155
286
  return null;
156
287
  }
157
- const { leftOut, unfilled, ...valid } = checked;
158
- const kept = {
159
- ...valid,
160
- step: stepsOf(valid)[0].id,
161
- 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,
162
296
  sendsBack: 0,
163
297
  by,
164
298
  at: now(),
165
299
  };
300
+ const kept = {
301
+ ...fresh,
302
+ stepAt: now(),
303
+ history: started(fresh, steps[0].id),
304
+ };
166
305
  write(goalId, kept);
167
306
  post?.(
168
307
  [
169
- `The team for this thread (${SHAPES[kept.shape].name}, ${kept.rigor}): ${lineup(kept)}.`,
170
- // One line per role: why it has no holder at all, else who was kept off it.
171
- ...leftOut
172
- .filter(({ role }) => !unfilled.some((each) => each.role === role))
173
- .map(leftOutLine),
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
+ : []),
174
314
  ...unfilled.map(unfilledLine),
175
315
  ].join(" ")
176
316
  );
@@ -178,35 +318,124 @@ export function createTeam({ store, log = () => {} }) {
178
318
  };
179
319
 
180
320
  /**
181
- * Move the thread to a step of its route (an id, "done"). A judged step's outcome is kept with the commit its
182
- * 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.
183
410
  */
184
411
  const moveTo = (goalId, step, { outcome = null, head = null } = {}) => {
185
412
  const current = read(goalId);
186
413
  if (!current) return null;
187
414
  const known =
188
- step === "done" || stepsOf(current).some((s) => s.id === step);
415
+ step === "done" || stepsOfNow(current).some((s) => s.id === step);
189
416
  if (!known) return current;
190
- const judged = stepsOf(current).find((s) => s.id === current.step)?.judges;
417
+ judge(goalId, current, outcome, head);
418
+ const after = read(goalId);
191
419
  const moved = {
192
- ...current,
420
+ ...after,
193
421
  step,
194
422
  stepAt: now(),
195
- sendsBack: current.sendsBack + (outcome === "sent_back" ? 1 : 0),
196
- ...(judged && (outcome === "passed" || outcome === "sent_back")
197
- ? { lastCheck: { step: current.step, outcome, head, at: now() } }
198
- : {}),
423
+ sendsBack: after.sendsBack + (outcome === "sent_back" ? 1 : 0),
199
424
  };
425
+ moved.history = started(moved, step);
200
426
  write(goalId, moved);
201
427
  return moved;
202
428
  };
203
429
 
204
- /** Whether the thread's team checks its work: its route has a step that judges. */
205
- 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
+ );
206
435
 
207
436
  /**
208
- * Whether the team's last check passed on the code as it is now (`head`, the thread's branch; null for a thread
209
- * 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.
210
439
  */
211
440
  const checkedNow = (goalId, head = null) => {
212
441
  const current = read(goalId);
@@ -219,22 +448,23 @@ export function createTeam({ store, log = () => {} }) {
219
448
 
220
449
  /**
221
450
  * Agents were just given work outside the route (the person wrote to them, or a free round picked one): the thread
222
- * 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.
223
452
  */
224
453
  const acted = (goalId, agents) => {
225
454
  const current = read(goalId);
226
455
  if (!current || !agents.length) return;
227
- const step = stepsOf(current).find((s) =>
228
- current.roles[s.role].some((holder) => agents.includes(holder))
456
+ const step = stepsOfNow(current).find((s) =>
457
+ s.agents.some((holder) => agents.includes(holder))
229
458
  );
230
459
  if (step && step.id !== current.step) moveTo(goalId, step.id);
231
- 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
+ });
232
466
  };
233
467
 
234
- /**
235
- * What the round reads about the team: the plan, the step the thread is at, whether everyone on that step has reported
236
- * since it began, and where the work may go next. `runs` are the thread's runs (store.threadRuns).
237
- */
238
468
  // When every holder of a step reported after `since`: the last of their reports, else null.
239
469
  const reportedAll = (holders, runs, since) => {
240
470
  const last = holders.map(
@@ -259,28 +489,104 @@ export function createTeam({ store, log = () => {} }) {
259
489
  * whose next step's agents have reported since moves on, up to a step that judges, whose outcome is the round's.
260
490
  */
261
491
  const catchUp = (goalId, current, runs) => {
262
- const steps = stepsOf(current);
492
+ const steps = stepsOfNow(current);
263
493
  let caught = current;
264
494
  for (;;) {
265
495
  const at = steps.findIndex((s) => s.id === caught.step);
266
496
  const step = steps[at];
267
497
  const next = steps[at + 1];
268
- if (!step || step.judges || !next) break;
269
- const done = reportedAll(current.roles[step.role], runs, caught.stepAt);
270
- 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;
271
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
+ ];
272
512
  }
273
513
  if (caught !== current) write(goalId, caught);
274
514
  return caught;
275
515
  };
276
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
+ */
277
563
  const roundView = (goalId, runs, { head = null } = {}) => {
278
564
  const kept = read(goalId);
279
565
  if (!kept) return null;
280
- const current = catchUp(goalId, kept, runs);
281
- 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
+ };
282
588
  // The route is done but the code changed after its last check passed: the work goes to the check again.
283
- const firstJudge = steps.findIndex((s) => s.judges);
589
+ const firstJudge = steps.findIndex((s) => stepOf(s.id).judges);
284
590
  const recheck =
285
591
  current.step === "done" &&
286
592
  firstJudge > 0 &&
@@ -289,41 +595,31 @@ export function createTeam({ store, log = () => {} }) {
289
595
  Boolean(current.lastCheck.head) &&
290
596
  current.lastCheck.head !== head;
291
597
  if (recheck) {
292
- const [made, judge] = [steps[firstJudge - 1], steps[firstJudge]];
598
+ const [made, judged] = [steps[firstJudge - 1], steps[firstJudge]];
293
599
  return {
294
- shape: current.shape,
295
- rigor: current.rigor,
296
- roles: current.roles,
297
- sendsBack: current.sendsBack,
600
+ ...base,
298
601
  step: {
299
602
  id: made.id,
300
603
  role: made.role,
301
- agents: current.roles[made.role],
604
+ specialty: made.specialty,
605
+ agents: made.agents,
302
606
  judges: false,
303
607
  finished: true,
304
608
  },
305
609
  exits: [
306
610
  {
307
611
  outcome: "done",
308
- step: judge.id,
309
- role: judge.role,
310
- agents: current.roles[judge.role],
612
+ step: judged.id,
613
+ role: judged.role,
614
+ agents: judged.agents,
311
615
  },
312
616
  ],
313
617
  };
314
618
  }
315
619
  const at = steps.findIndex((s) => s.id === current.step);
316
- const view = {
317
- shape: current.shape,
318
- rigor: current.rigor,
319
- roles: current.roles,
320
- sendsBack: current.sendsBack,
321
- step: null,
322
- exits: [],
323
- };
620
+ const view = { ...base, step: null, exits: [] };
324
621
  if (at === -1) return view;
325
622
  const step = steps[at];
326
- const holders = current.roles[step.role];
327
623
  const reportedSince = (agent) =>
328
624
  runs.some(
329
625
  (run) =>
@@ -331,23 +627,35 @@ export function createTeam({ store, log = () => {} }) {
331
627
  FINISHED.has(run.status) &&
332
628
  (run.reported_at ?? "") > current.stepAt
333
629
  );
334
- const finished = holders.every(reportedSince);
630
+ const finished = step.agents.every(reportedSince);
631
+ const judges = Boolean(stepOf(step.id).judges);
335
632
  view.step = {
336
633
  id: step.id,
337
634
  role: step.role,
338
- agents: holders,
339
- judges: Boolean(step.judges),
635
+ specialty: step.specialty,
636
+ agents: step.agents,
637
+ judges,
340
638
  finished,
341
639
  };
342
640
  if (!finished) return view;
641
+ if (!steps[at + 1]) current = splitInto(goalId, current, runs);
343
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
+ };
344
650
  const toStep = (s, outcome) =>
345
651
  s
346
- ? { outcome, step: s.id, role: s.role, agents: current.roles[s.role] }
347
- : { outcome, step: "done", role: null, agents: [] };
348
- 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")];
349
657
  else {
350
- const back = steps.find((s) => s.id === step.sendBack);
658
+ const back = steps.find((s) => s.id === stepOf(step.id).sendBack);
351
659
  view.exits = [
352
660
  toStep(following, "passed"),
353
661
  back && current.sendsBack < TEAM.sendBackLimit
@@ -392,52 +700,82 @@ export function createTeam({ store, log = () => {} }) {
392
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}`
393
701
  );
394
702
 
395
- /** The brief's section on the team, for an agent woken in a thread with a plan. `model`: the agent's model, if known. */
396
- const brief = (goalId, agent, { tier = 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) => {
397
730
  const current = read(goalId);
398
731
  if (!current) return "";
399
- const steps = stepsOf(current);
400
- // The role the agent acts in now comes first: the one its current step is for, when it holds several.
401
- const now = steps.find((s) => s.id === current.step)?.role;
402
- const mine = rolesOf(goalId, agent).sort(
403
- (a, b) => Number(b === now) - Number(a === now)
404
- );
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;
405
741
  const lines = [
406
- `The team (${SHAPES[current.shape].name}): ${lineup(current)}.`,
407
- `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}`,
408
747
  ];
409
748
  const folder = store.getTask(goalId)?.goal?.workspace ?? null;
410
749
  const learned = [];
411
- for (const [index, role] of mine.entries()) {
412
- const contract = TEAM.roles[role];
750
+ for (const [index, step] of mine.entries()) {
751
+ const contract = TEAM.roles[step.role];
413
752
  if (index > 0) {
414
- 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
+ );
415
756
  continue;
416
757
  }
417
- learned.push(...notesFor(role, folder));
418
- const at = steps.findIndex((s) => s.role === role);
419
- const next = steps[at + 1];
758
+ learned.push(...notesFor(step.role, folder));
759
+ const next = steps[steps.indexOf(step) + 1];
420
760
  lines.push(
421
- `Your role now: ${contract.name}. ${contract.owns}`,
761
+ `Your role now: ${contract.name}${step.specialty ? `, for ${specialtyName(step.specialty)}` : ""}. ${contract.owns}`,
422
762
  `Never: ${contract.never}`,
423
763
  `Bring back: ${contract.bringsBack}`,
764
+ ...[stepNote(current, step), uncommittedNote(goalId, step)].filter(
765
+ Boolean
766
+ ),
424
767
  next
425
- ? `Next on the team: ${current.roles[next.role].map(nameOf).join(" and ")} (${TEAM.roles[next.role].name}).`
426
- : "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."
427
770
  );
428
771
  }
429
772
  if (!mine.length)
430
773
  lines.push(
431
- "You hold no role on this team: do what you were asked and hand anything else to the agent whose role it is."
432
- );
433
- // Agents on top-tier models get the intent and find the route; others do their best work on smaller checked steps.
434
- if (!goalLevel(tier))
435
- lines.push(
436
- "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."
437
775
  );
438
776
  if (mine.length)
439
777
  lines.push(
440
- `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), add it with swarm_memory (remember, why, thread ${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.`
441
779
  );
442
780
  return [
443
781
  "Your part on the team:",
@@ -456,6 +794,7 @@ export function createTeam({ store, log = () => {} }) {
456
794
  return {
457
795
  plan,
458
796
  set,
797
+ routePiece,
459
798
  moveTo,
460
799
  acted,
461
800
  roundView,
@@ -463,5 +802,7 @@ export function createTeam({ store, log = () => {} }) {
463
802
  learn,
464
803
  brief,
465
804
  rolesOf,
805
+ stepHeld,
806
+ pinnedStep,
466
807
  };
467
808
  }