tryharness 0.7.42 → 0.7.43

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 (46) hide show
  1. package/cli.mjs +118 -5
  2. package/lib/actorRoster.mjs +16 -2
  3. package/lib/adapters.mjs +2 -1
  4. package/lib/answers.mjs +30 -0
  5. package/lib/boardCommands.mjs +9 -1
  6. package/lib/boardFiles.mjs +1 -1
  7. package/lib/boardProtocol.json +1 -0
  8. package/lib/bridge.mjs +49 -9
  9. package/lib/cards.mjs +22 -4
  10. package/lib/compaction.mjs +90 -0
  11. package/lib/context.mjs +19 -11
  12. package/lib/coordinator.mjs +176 -12
  13. package/lib/daemon.mjs +424 -44
  14. package/lib/fit.mjs +65 -0
  15. package/lib/folders.mjs +20 -0
  16. package/lib/frames.mjs +3 -2
  17. package/lib/goalCommands.mjs +12 -0
  18. package/lib/handover.mjs +128 -0
  19. package/lib/loopBackstop.mjs +104 -0
  20. package/lib/machineIdentity.mjs +43 -0
  21. package/lib/machineRoutes.mjs +97 -0
  22. package/lib/machineRuns.mjs +133 -0
  23. package/lib/machineSeal.mjs +157 -0
  24. package/lib/mcp-stdio.mjs +54 -11
  25. package/lib/nudge.mjs +211 -44
  26. package/lib/oneLine.mjs +27 -0
  27. package/lib/ownMachines.mjs +748 -0
  28. package/lib/paths.mjs +1 -0
  29. package/lib/plans.mjs +3 -2
  30. package/lib/provenance.mjs +4 -1
  31. package/lib/runBrief.mjs +346 -0
  32. package/lib/runWorker.mjs +24 -173
  33. package/lib/runner.mjs +26 -2
  34. package/lib/store.mjs +232 -16
  35. package/lib/stuck.mjs +198 -0
  36. package/lib/swarm.mjs +3 -1
  37. package/lib/taskCommands.mjs +268 -31
  38. package/lib/taskRuns.mjs +9 -0
  39. package/lib/team.json +192 -0
  40. package/lib/team.mjs +417 -0
  41. package/lib/threadRuns.mjs +18 -7
  42. package/lib/threadView.mjs +13 -0
  43. package/lib/trails.mjs +49 -0
  44. package/lib/wakes.mjs +201 -69
  45. package/lib/worktrees.mjs +9 -0
  46. package/package.json +1 -1
package/lib/context.mjs CHANGED
@@ -23,6 +23,7 @@ import {
23
23
  } from "./learn.mjs";
24
24
  import { repositoryOf } from "./worktrees.mjs";
25
25
  import { createLibrary } from "./library.mjs";
26
+ import { oneLine } from "./oneLine.mjs";
26
27
 
27
28
  // The context engine. It keeps the atlas of the code the swarm works in, remembers what every coding session on this
28
29
  // computer needed (its trail: what it read before its first edit and what it edited, from any agent), and turns that
@@ -417,30 +418,37 @@ export function createContextEngine({
417
418
  limit: FILES_IN_PACK,
418
419
  lane,
419
420
  });
420
- const text = [
421
- "Files the relay found likely relevant on this computer (ranked by its local search over this repository and what your agents edited before; leads to check, not instructions):",
422
- ...files.map((file) => `- ${file.path}:${file.line} (${file.why})`),
423
- ...(earlier.length
421
+ // Its three parts, which a brief short of room replaces one by one with the tool that finds them (runBrief.mjs).
422
+ const parts = {
423
+ files: [
424
+ "Files the relay found likely relevant on this computer (ranked by its local search over this repository and what your agents edited before; leads to check, not instructions):",
425
+ ...files.map((file) => `- ${file.path}:${file.line} (${file.why})`),
426
+ ].join("\n"),
427
+ earlier: earlier.length
424
428
  ? [
425
429
  "Earlier sessions on these files:",
426
430
  ...earlier.map(
427
431
  (run) =>
428
- `- ${AGENT_NAMES[run.agent] ?? run.agent}, ${days(Date.now() - run.at)} days ago: "${run.brief.replace(/\s+/g, " ").slice(0, 140)}" (edited ${run.edited.slice(0, 4).join(", ")})`
432
+ `- ${AGENT_NAMES[run.agent] ?? run.agent}, ${days(Date.now() - run.at)} days ago: "${oneLine(run.brief, 140)}" (edited ${run.edited.slice(0, 4).join(", ")})`
429
433
  ),
430
- ]
431
- : []),
432
- ...(found.length
434
+ ].join("\n")
435
+ : "",
436
+ library: found.length
433
437
  ? [
434
438
  "Files close to the request, from folders the person added and this thread's files (search_files finds more):",
435
439
  ...found.map(fileLine),
436
- ]
437
- : []),
438
- ].join("\n");
440
+ ].join("\n")
441
+ : "",
442
+ };
443
+ const text = [parts.files, parts.earlier, parts.library]
444
+ .filter(Boolean)
445
+ .join("\n");
439
446
  return {
440
447
  files,
441
448
  earlier: earlier.map(({ vector, ...run }) => run),
442
449
  library: found,
443
450
  text,
451
+ parts,
444
452
  searched: scope.length,
445
453
  ms: Math.round(performance.now() - started),
446
454
  model: model ? "learned" : "default",
@@ -80,7 +80,8 @@ Work in progress.waitingForWindow is already Harness's and only waits for the pe
80
80
  In briefs, refer to earlier posts by their seq number instead of restating figures or ids, and never say the person approved something unless you quote their words.
81
81
  The goal: statement is the whole aim in the person's terms; criteria are one to five checks a person or code can verify. A criterion is met only when a post shows the result, not when an agent only says so. goalState met only when every criterion is met.
82
82
  If the same attempt or failure keeps coming back, say so in loop and ask the next agent for a different approach in the brief, naming what was tried.
83
- A check-in brings the thread back to you at that time with a post saying what to look at; checkIns lists the thread's check-ins already set.`;
83
+ A check-in brings the thread back to you at that time with a post saying what to look at; checkIns lists the thread's check-ins already set.
84
+ The thread's team, when \`team\` is given: who holds which role and the step of its route. When \`team.step.finished\` is true and \`team.exits\` lists where the work goes next, hand on along the route unless a question to the person is open: for a step that judges (\`team.step.judges\`), read the newest report and pick the exit whose \`outcome\` it shows (passed, or sent_back when it found something to fix); hand on to that exit's agents and add "team": {"step": the exit's step, "outcome": its outcome} to your answer. An exit to step "stop" means pause and tell the person the work was sent back as often as the team allows; one to step "done" means decide as usual and add "team": {"step": "done", "outcome": "done"}. Never hand a check or a review to the agent that did the work.`;
84
85
 
85
86
  function localDecision(agent, facts) {
86
87
  return new Promise((resolve, reject) => {
@@ -146,6 +147,29 @@ function localDecision(agent, facts) {
146
147
  });
147
148
  }
148
149
 
150
+ // A question's choices as a coordinator gave them, kept only in the agreed shape: one to four, each with two to four
151
+ // options and at most one recommended.
152
+ const choicesOf = (value) => {
153
+ const choices = (Array.isArray(value) ? value : []).flatMap((entry) => {
154
+ const options = (Array.isArray(entry?.options) ? entry.options : [])
155
+ .filter((option) => typeof option === "string" && option.trim())
156
+ .map((option) => option.trim().slice(0, 120));
157
+ if (typeof entry?.question !== "string" || !entry.question.trim())
158
+ return [];
159
+ if (options.length < 2 || options.length > 4) return [];
160
+ const recommended =
161
+ Number.isInteger(entry.recommended) &&
162
+ entry.recommended >= 0 &&
163
+ entry.recommended < options.length
164
+ ? entry.recommended
165
+ : null;
166
+ return [
167
+ { question: entry.question.trim().slice(0, 200), options, recommended },
168
+ ];
169
+ });
170
+ return choices.length <= 4 ? choices : [];
171
+ };
172
+
149
173
  // An agent whose job in a thread just failed on its own terms is not offered that thread's next rounds for a while
150
174
  // (October 19: the lead handed Bankr the same over-size job three times in two minutes). The person or another agent
151
175
  // can still ask it by name.
@@ -176,6 +200,14 @@ export function createCoordinator({
176
200
  // Agents of the thread at work in T3 Code on turns Harness did not start (their own follow-ups, the person typing in
177
201
  // T3): the thread is not quiet while they are.
178
202
  busyInT3 = () => [],
203
+ // The thread's team (team.mjs): its plan, the step of its route and where the work goes next.
204
+ team = null,
205
+ // The commit a thread's own branch is at (worktrees.mjs head): what a check saw, and whether code changed since.
206
+ headOf = () => null,
207
+ // Which of the person's computers a coding agent is on, each with whether the thread's repository is there
208
+ // (ownMachines.mjs), and whether one of them can take the thread's turn at all.
209
+ machinesOf = () => undefined,
210
+ placeable = () => true,
179
211
  }) {
180
212
  const failedHereLately = (threadId, agent) => {
181
213
  const at = Date.parse(store.meta(`job-failed:${threadId}:${agent}`) || "");
@@ -191,6 +223,10 @@ export function createCoordinator({
191
223
  // Lessons go to a coordinator that reads them: this computer's, or a Harness that has swarm_lessons.
192
224
  const readsLessons = () =>
193
225
  Boolean(lessons) && (mode() === "local" || serverHas("swarm_lessons"));
226
+ // The team goes to a coordinator that keeps it: this computer's follows a route, and a Harness with swarm_team also
227
+ // proposes one in the thread's first assembly.
228
+ const keepsTeams = () =>
229
+ Boolean(team) && (mode() === "local" || serverHas("swarm_team"));
194
230
  // The backups, in the order they are tried: coding agents on this computer, most finished work first, one whose last
195
231
  // turn as a backup failed after the others.
196
232
  const backups = () => {
@@ -285,16 +321,21 @@ export function createCoordinator({
285
321
  const framesTaken = mode() === "local" || serverHas("swarm_frame");
286
322
  const causesTaken = mode() === "local" || serverHas("swarm_failure_causes");
287
323
  const verdictsTaken = mode() === "local" || serverHas("swarm_reactions");
288
- const candidates = roster()
324
+ const machinesTaken = mode() === "local" || serverHas("machines");
325
+ // Who could hold a role on the thread's team: the candidates and the agents the person named.
326
+ const eligible = roster()
289
327
  .filter(
290
328
  (candidate) =>
291
329
  (!CODING.has(candidate.id) || thread.goal.workspace) &&
292
330
  !failedHereLately(thread.id, candidate.id) &&
293
- !asked?.named.includes(candidate.id) &&
331
+ (!CODING.has(candidate.id) ||
332
+ placeable(candidate.id, thread.goal.workspace)) &&
294
333
  // In every round, not only an assembly (owner, October 16: "yah", to Bankr scoring 0.33 to 0.38 as the agent
295
334
  // to hand a production keeper fix to in the eval swarm-coordination).
296
335
  !(
297
- TAGGED.has(candidate.id) && !thread.goal.team.includes(candidate.id)
336
+ TAGGED.has(candidate.id) &&
337
+ !thread.goal.team.includes(candidate.id) &&
338
+ !asked?.named.includes(candidate.id)
298
339
  )
299
340
  )
300
341
  .map((candidate) => ({
@@ -308,7 +349,22 @@ export function createCoordinator({
308
349
  },
309
350
  }),
310
351
  lessons: readsLessons() ? candidate.lessons : undefined,
352
+ machines: machinesTaken
353
+ ? machinesOf(candidate.id, thread.goal.workspace)
354
+ : undefined,
311
355
  }));
356
+ const candidates = eligible.filter(
357
+ (candidate) => !asked?.named.includes(candidate.id)
358
+ );
359
+ // The team: where its route stands, or none yet, which the person's message to the swarm settles.
360
+ const teamView = keepsTeams()
361
+ ? team.roundView(thread.id, runs, { head: headOf(thread.id) })
362
+ : null;
363
+ const teamFacts = teamView
364
+ ? { team: teamView }
365
+ : keepsTeams() && asked && mode() !== "local"
366
+ ? { team: null, teamMembers: eligible.slice(0, 16) }
367
+ : {};
312
368
  // Earlier words close in meaning to the newest post that the transcript no longer carries, from this thread and the
313
369
  // person's sessions on this machine.
314
370
  const newest = posts
@@ -344,6 +400,7 @@ export function createCoordinator({
344
400
  const framed = frame(thread.id);
345
401
  return {
346
402
  ...(framed ? { frame: framed } : {}),
403
+ ...teamFacts,
347
404
  thread: {
348
405
  id: thread.id,
349
406
  title: thread.objective,
@@ -499,9 +556,20 @@ export function createCoordinator({
499
556
  return false;
500
557
  }
501
558
  if (!decision.met && !decision.goalState) return false;
559
+ // A team that checks its work reaches its goal, or hands it to the person, only on code its last check passed: a
560
+ // change since, or no passing check yet, leaves the goal working (October 19: "Ready for your review" was posted
561
+ // with 1 of 3 checks met).
562
+ const unchecked =
563
+ keepsTeams() && !team.checkedNow(thread.id, headOf(thread.id));
564
+ const settles =
565
+ decision.goalState === "met" ||
566
+ decision.goalState === "ready_for_review" ||
567
+ Boolean(decision.met?.length);
502
568
  const { became } = goals.progress(
503
569
  thread.id,
504
- { met: decision.met, goalState: decision.goalState },
570
+ unchecked && settles
571
+ ? { goalState: "working" }
572
+ : { met: decision.met, goalState: decision.goalState },
505
573
  { by: coordinator }
506
574
  );
507
575
  if (became !== "met") return false;
@@ -528,6 +596,81 @@ export function createCoordinator({
528
596
  });
529
597
  };
530
598
 
599
+ // Who changed the thread's code since its last review: a run whose turn committed to the thread's branch
600
+ // (worktrees.mjs commitTurn). A review by one of them would be a self-check; a run that only looked is no author.
601
+ const authorsSinceReview = (threadId) => {
602
+ const finished = store
603
+ .threadRuns(threadId)
604
+ .filter((run) => run.status === "done" && run.reported_at);
605
+ const reviewed = (run) => store.meta(`review:${run.id}`) === "1";
606
+ const lastReview = finished.filter(reviewed).at(-1)?.reported_at ?? "";
607
+ return new Set(
608
+ finished
609
+ .filter(
610
+ (run) =>
611
+ !reviewed(run) &&
612
+ run.reported_at > lastReview &&
613
+ Boolean(store.meta(`turn:${run.id}`))
614
+ )
615
+ .map((run) => run.target_agent)
616
+ );
617
+ };
618
+ // A review handed to an agent that changed the work goes to another that can review and did not; with none, the
619
+ // thread says that only a self-check was possible.
620
+ const independentReviewer = (thread, agent, candidates, coordinator) => {
621
+ const authors = authorsSinceReview(thread.id);
622
+ if (!authors.has(agent)) return agent;
623
+ const other = candidates.find(
624
+ (candidate) => REVIEWERS.has(candidate.id) && !authors.has(candidate.id)
625
+ )?.id;
626
+ postAsCoordinator({
627
+ goalId: thread.id,
628
+ author: coordinator,
629
+ kind: "note",
630
+ text: other
631
+ ? `${nameOf(agent)} changed this work, so ${nameOf(other)} reviews it instead.`
632
+ : `Only a self-check was possible: every agent here that can review changed this work.`,
633
+ });
634
+ return other ?? agent;
635
+ };
636
+ // The team's part of a decision: a plan for a thread that had none, said in the thread.
637
+ const applyTeamPlan = (thread, decision, coordinator) => {
638
+ const proposed = decision.team?.plan;
639
+ if (!proposed || !keepsTeams() || team.plan(thread.id)) return null;
640
+ return team.set(thread.id, proposed, {
641
+ members: roster().map((card) => card.id),
642
+ by: coordinator,
643
+ post: (text) =>
644
+ postAsCoordinator({
645
+ goalId: thread.id,
646
+ author: coordinator,
647
+ kind: "note",
648
+ text,
649
+ }),
650
+ });
651
+ };
652
+ // Where the work went: along the route to the step the round chose, or, handed outside it, to the step of the agents
653
+ // who now have it.
654
+ const moveTeam = (thread, decision, agents) => {
655
+ if (!keepsTeams()) return;
656
+ const step = decision.team?.step;
657
+ if (step && step !== "done")
658
+ team.moveTo(thread.id, step, {
659
+ outcome: decision.team.outcome ?? null,
660
+ head: headOf(thread.id),
661
+ });
662
+ else if (agents.length) team.acted(thread.id, agents);
663
+ };
664
+ // The route ended this round (its last check passed, or its last step was done): kept before the goal is, so a goal
665
+ // reached on the same round reads the check that passed.
666
+ const endRoute = (thread, decision) => {
667
+ if (!keepsTeams() || decision.team?.step !== "done") return;
668
+ team.moveTo(thread.id, "done", {
669
+ outcome: decision.team.outcome ?? null,
670
+ head: headOf(thread.id),
671
+ });
672
+ };
673
+
531
674
  const apply = (
532
675
  thread,
533
676
  given,
@@ -536,13 +679,19 @@ export function createCoordinator({
536
679
  asked = null,
537
680
  rules = keepsGoals()
538
681
  ) => {
682
+ endRoute(thread, given);
539
683
  if (applyGoal(thread, given, coordinator)) return;
540
684
  applyCheckIn(thread, given, coordinator);
685
+ const planned = applyTeamPlan(thread, given, coordinator);
541
686
  const decision = guardQuestion(thread, given, candidates, rules);
542
687
  // The person's message is never met with silence: a round that brings no one in and asks them nothing lets the
543
688
  // agents hear it as they wrote it, as with `@swarm` (owner, October 16: "it should never be silent if jev doesnt
544
689
  // understand thats fallback behavior"). A message naming an agent is not silent: that agent has it.
545
- const unheard = Boolean(asked) && (asked.swarm || !asked.named.length);
690
+ // A team whose first step the person's named agents hold is under way: they have the message already.
691
+ const unheard =
692
+ Boolean(asked) &&
693
+ (asked.swarm || !asked.named.length) &&
694
+ !(planned && asked.named.length);
546
695
  if (unheard && decision.action === "wait") {
547
696
  tellEveryone(thread, asked, null, { decided: true });
548
697
  notifyBoard(store.getTask(thread.id));
@@ -575,6 +724,7 @@ export function createCoordinator({
575
724
  );
576
725
  if (!parts.length) {
577
726
  if (unheard) tellEveryone(thread, asked, null, { decided: true });
727
+ else if (planned) moveTeam(thread, decision, asked?.named ?? []);
578
728
  notifyBoard(store.getTask(thread.id));
579
729
  return;
580
730
  }
@@ -604,6 +754,11 @@ export function createCoordinator({
604
754
  )
605
755
  .join("\n\n"),
606
756
  });
757
+ // The team moves first, so each brief says the step its agent now acts in.
758
+ moveTeam(thread, decision, [
759
+ ...worded.map((part) => part.agent),
760
+ ...(asked?.named ?? []),
761
+ ]);
607
762
  for (const part of worded)
608
763
  handOn(
609
764
  thread.id,
@@ -614,33 +769,42 @@ export function createCoordinator({
614
769
  notifyBoard(store.getTask(thread.id));
615
770
  return;
616
771
  }
617
- if (decision.action === "ask_person" && decision.text)
618
- postAsCoordinator({
772
+ if (decision.action === "ask_person" && decision.text) {
773
+ const asked = postAsCoordinator({
619
774
  goalId: thread.id,
620
775
  author: coordinator,
621
776
  kind: "question",
622
777
  text: String(decision.text).slice(0, 4_000),
623
778
  });
779
+ // Its decisions as choices a surface can offer as taps (threadView.mjs); the words stand alone without them.
780
+ const choices = choicesOf(decision.choices);
781
+ if (asked && choices.length)
782
+ store.meta(`question-choices:${asked.id}`, JSON.stringify(choices));
783
+ }
624
784
  if (
625
785
  decision.action === "hand_on" &&
626
786
  candidates.some((candidate) => candidate.id === decision.agent) &&
627
787
  decision.text
628
788
  ) {
629
- if (!thread.goal.team.includes(decision.agent))
630
- store.setGoalTeam(thread.id, [...thread.goal.team, decision.agent]);
631
789
  const review =
632
790
  decision.mode === "review" && REVIEWERS.has(decision.agent);
791
+ const agent = review
792
+ ? independentReviewer(thread, decision.agent, candidates, coordinator)
793
+ : decision.agent;
794
+ if (!thread.goal.team.includes(agent))
795
+ store.setGoalTeam(thread.id, [...thread.goal.team, agent]);
633
796
  const text = String(decision.text).slice(0, 4_000);
634
797
  // The brief is posted for everyone to read and the agent is woken here, so a review runs with its read-only switch.
635
798
  postAsCoordinator({
636
799
  goalId: thread.id,
637
800
  author: coordinator,
638
801
  kind: "post",
639
- text: `@${decision.agent} ${review ? "(review, no changes) " : ""}${text}`,
802
+ text: `@${agent} ${review ? "(review, no changes) " : ""}${text}`,
640
803
  });
804
+ moveTeam(thread, decision, [agent]);
641
805
  handOn(
642
806
  thread.id,
643
- decision.agent,
807
+ agent,
644
808
  `${coordinator} asked you: ${text.slice(0, 300)}`,
645
809
  { review, words: text }
646
810
  );