tryharness 0.7.67 → 0.7.69

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 (54) hide show
  1. package/lib/boardFileCommands.mjs +5 -7
  2. package/lib/boardFiles.mjs +5 -0
  3. package/lib/boardProtocol.json +50 -64
  4. package/lib/boardWaits.mjs +6 -0
  5. package/lib/cards.mjs +13 -1
  6. package/lib/comfyExecution.mjs +545 -0
  7. package/lib/context.mjs +34 -20
  8. package/lib/contextWorker.mjs +199 -0
  9. package/lib/coordinator.mjs +224 -22
  10. package/lib/daemon.mjs +196 -14
  11. package/lib/dispatchContract.json +313 -0
  12. package/lib/dispatchTools.js +87 -0
  13. package/lib/dispatches.mjs +975 -0
  14. package/lib/environment.mjs +54 -69
  15. package/lib/files.mjs +35 -0
  16. package/lib/frames.mjs +5 -9
  17. package/lib/goalCommands.mjs +13 -10
  18. package/lib/kept.mjs +131 -0
  19. package/lib/landed.mjs +1 -34
  20. package/lib/library.mjs +18 -11
  21. package/lib/live.mjs +30 -11
  22. package/lib/localAgentAsk.mjs +27 -4
  23. package/lib/localStack.mjs +17 -13
  24. package/lib/machineRoutes.mjs +5 -0
  25. package/lib/machineRuns.mjs +2 -1
  26. package/lib/mcp-stdio.mjs +42 -1
  27. package/lib/openHere.mjs +36 -0
  28. package/lib/openRequests.mjs +56 -0
  29. package/lib/ownMachines.mjs +232 -24
  30. package/lib/phones.mjs +29 -4
  31. package/lib/places.mjs +44 -11
  32. package/lib/portCalls.mjs +54 -0
  33. package/lib/promptDecider.mjs +471 -0
  34. package/lib/promptFlow.mjs +562 -0
  35. package/lib/promptScopes.mjs +670 -0
  36. package/lib/provenance.mjs +3 -2
  37. package/lib/runAuthority.mjs +14 -2
  38. package/lib/runBrief.mjs +2 -2
  39. package/lib/runWorker.mjs +25 -5
  40. package/lib/runner.mjs +62 -6
  41. package/lib/sealed.mjs +37 -0
  42. package/lib/sessionControl.mjs +47 -14
  43. package/lib/sessionIndex.mjs +11 -3
  44. package/lib/store.mjs +362 -40
  45. package/lib/swarm.mjs +85 -34
  46. package/lib/swarmMemory.mjs +39 -16
  47. package/lib/taskCommands.mjs +125 -1
  48. package/lib/team.json +10 -0
  49. package/lib/team.mjs +287 -26
  50. package/lib/threadBriefs.mjs +43 -31
  51. package/lib/threadRuns.mjs +47 -3
  52. package/npm-shrinkwrap.json +14 -14
  53. package/package.json +4 -4
  54. package/release.json +55 -43
package/lib/swarm.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import fs from "node:fs";
2
2
  import os from "node:os";
3
3
  import path from "node:path";
4
- import { spawnSync } from "node:child_process";
4
+ import { kept } from "./kept.mjs";
5
5
  import { readRunSteps } from "./runner.mjs";
6
6
  import { createSubagents } from "./subagents.mjs";
7
7
  import { MACHINE_RUNNERS, member, nameOf, resolveMember } from "./members.mjs";
@@ -99,37 +99,58 @@ export function nativeSessionId(agent, sessionId) {
99
99
  : bare;
100
100
  }
101
101
 
102
- // The folder each running coding agent works in, by agent, leaving out the runs the daemon started itself.
103
- // Null when the machine cannot say, and every session then counts as open in a terminal.
104
- export function agentProcessCwds(exclude = new Set()) {
105
- if (process.platform === "win32") return null;
106
- const ps = spawnSync("ps", ["-axo", "pid=,comm="], { encoding: "utf8" });
107
- if (ps.status !== 0) return null;
102
+ // Each running coding agent's process and the folder it works in, as kept.mjs steps; null when the machine cannot say.
103
+ function* agentProcessSteps() {
104
+ const ps = yield ["ps", ["-axo", "pid=,comm="]];
105
+ if (ps === null) return null;
108
106
  const byPid = new Map();
109
- for (const line of ps.stdout.split("\n")) {
107
+ for (const line of ps.split("\n")) {
110
108
  const match = line.trim().match(/^(\d+)\s+(.+)$/);
111
- if (!match || exclude.has(Number(match[1]))) continue;
109
+ if (!match) continue;
112
110
  const agent = Object.keys(PROCESS_NAMES).find(
113
111
  (name) => path.basename(match[2]) === PROCESS_NAMES[name]
114
112
  );
115
113
  if (agent) byPid.set(match[1], agent);
116
114
  }
117
- const cwds = new Map();
118
- if (!byPid.size) return cwds;
119
- const lsof = spawnSync(
115
+ if (!byPid.size) return [];
116
+ const lsof = yield [
120
117
  "lsof",
121
118
  ["-a", "-d", "cwd", "-Fn", "-p", [...byPid.keys()].join(",")],
122
- { encoding: "utf8" }
123
- );
124
- if (lsof.status !== 0 && !lsof.stdout) return null;
119
+ // A process gone since ps makes lsof fail, after it lists the others.
120
+ { anyStatus: true },
121
+ ];
122
+ if (lsof === null) return null;
123
+ const found = [];
125
124
  let pid = null;
126
- for (const line of lsof.stdout.split("\n")) {
125
+ for (const line of lsof.split("\n")) {
127
126
  if (line.startsWith("p")) pid = line.slice(1);
128
- else if (line.startsWith("n") && byPid.has(pid)) {
129
- const agent = byPid.get(pid);
130
- if (!cwds.has(agent)) cwds.set(agent, new Set());
131
- cwds.get(agent).add(line.slice(1));
132
- }
127
+ else if (line.startsWith("n") && byPid.has(pid))
128
+ found.push({
129
+ pid: Number(pid),
130
+ agent: byPid.get(pid),
131
+ cwd: line.slice(1),
132
+ });
133
+ }
134
+ return found;
135
+ }
136
+ // The buddy's thread list asks on every read; ps and lsof run beside the daemon's thread once this is a few seconds old.
137
+ const agentProcessesKept = kept(agentProcessSteps, {
138
+ freshMs: 3_000,
139
+ staleMs: 30_000,
140
+ keyOf: () => "all",
141
+ });
142
+
143
+ // The folder each running coding agent works in, by agent, leaving out the runs the daemon started itself.
144
+ // Null when the machine cannot say, and every session then counts as open in a terminal.
145
+ export function agentProcessCwds(exclude = new Set()) {
146
+ if (process.platform === "win32") return null;
147
+ const found = agentProcessesKept();
148
+ if (!found) return null;
149
+ const cwds = new Map();
150
+ for (const { pid, agent, cwd } of found) {
151
+ if (exclude.has(pid)) continue;
152
+ if (!cwds.has(agent)) cwds.set(agent, new Set());
153
+ cwds.get(agent).add(cwd);
133
154
  }
134
155
  return cwds;
135
156
  }
@@ -953,13 +974,16 @@ export function createSwarm({
953
974
  }
954
975
  // The swarm's shared memory, for any of the person's agents: what any agent wrote in any thread, and the coding
955
976
  // sessions on this machine. A caller limited to its own threads (a hosted agent while the machine is sealed) gets only those.
977
+ // With `thread`, that thread alone (goalCommands.mjs board.room.search), and no sessions.
956
978
  if (command === "board.swarm.search") {
957
979
  const query = String(request.query || "")
958
980
  .trim()
959
981
  .slice(0, 500);
960
982
  if (!query) return { ok: false, error: "search needs a query" };
983
+ const thread = request.thread ? String(request.thread) : null;
961
984
  const hosted = Boolean(request.teamOnly) || request.hostedCaller === true;
962
- const sessions = hosted ? [] : store.search({ query, limit: 5 });
985
+ const sessions =
986
+ hosted || thread ? [] : store.search({ query, limit: 5 });
963
987
  if (!memory)
964
988
  return { ok: true, threads: [], sessions, lessons: [], weak: true };
965
989
  return {
@@ -967,6 +991,7 @@ export function createSwarm({
967
991
  .recall(query, {
968
992
  agent,
969
993
  teamOnly: Boolean(request.teamOnly),
994
+ thread,
970
995
  hosted,
971
996
  everyFolder: true,
972
997
  })
@@ -1441,27 +1466,53 @@ export function createSwarm({
1441
1466
  );
1442
1467
  worktrees.inPlace(thread.id);
1443
1468
  }
1444
- const opened = store.getTask(thread.id);
1445
1469
  const first = store.roomPost({
1446
1470
  goalId: thread.id,
1447
1471
  author: "user",
1448
1472
  kind: "post",
1449
1473
  text,
1450
- mentions: addressed(opened, text),
1474
+ mentions: addressed(store.getTask(thread.id), text),
1451
1475
  });
1452
- wakeFor(thread.id, first, "user");
1476
+ return { id: thread.id, first };
1477
+ };
1478
+ const hear = ({ id, first }) => {
1479
+ const opened = store.getTask(id);
1480
+ wakeFor(id, first, "user");
1453
1481
  if (!to) heardBy(opened, first, text);
1454
1482
  heardPerson(opened, first);
1455
- notifyBoard(store.getTask(thread.id));
1456
- return { ok: true, thread: summary(store.getTask(thread.id)) };
1457
1483
  };
1458
- return folder || !needsFolder([...team])
1459
- ? begin(folder)
1460
- : {
1461
- pending: folders
1462
- .suggest(text, [...team])
1463
- .then((suggested) => begin(suggested.folder)),
1464
- };
1484
+ const started = ({ id }) => {
1485
+ notifyBoard(store.getTask(id));
1486
+ return { ok: true, thread: summary(store.getTask(id)) };
1487
+ };
1488
+ if (folder || !needsFolder([...team])) {
1489
+ const thread = begin(folder);
1490
+ hear(thread);
1491
+ return started(thread);
1492
+ }
1493
+ // Harness's folder pick is a model call, and Harness's server waits 10 s for this computer's answer to a window's
1494
+ // call: a start held for a slow pick reached the window as "your computer is not connected" while the thread went
1495
+ // on to start, and the person sent it again. A pick already made (the composer's preview of this message) is
1496
+ // settled before the next turn of the event loop; any other starts the thread now, its agents woken once the
1497
+ // pick lands. A folder the person chose meanwhile stands.
1498
+ const suggestion = folders.suggest(text, [...team]);
1499
+ const notYet = new Promise((resolve) => setImmediate(resolve, null));
1500
+ return {
1501
+ pending: Promise.race([suggestion, notYet]).then((suggested) => {
1502
+ const thread = begin(suggested?.folder ?? null);
1503
+ if (suggested) hear(thread);
1504
+ else
1505
+ void suggestion.then(({ folder: picked }) => {
1506
+ const now = store.getTask(thread.id);
1507
+ if (now?.status !== "open") return;
1508
+ if (picked && !now.goal.workspace)
1509
+ store.setGoalFolder(thread.id, path.resolve(picked));
1510
+ hear(thread);
1511
+ notifyBoard(store.getTask(thread.id));
1512
+ });
1513
+ return started(thread);
1514
+ }),
1515
+ };
1465
1516
  }
1466
1517
  // What the swarm learned from the person (lessons.mjs), the person's to read: only the person gets this far.
1467
1518
  if (command === "board.swarm.lessons")
@@ -111,6 +111,13 @@ export function createSwarmMemory({
111
111
  .map((thread) => thread.id)
112
112
  );
113
113
 
114
+ // The threads a search reads, or null for every one: the asker's own (`teamOnly`), one thread, or both.
115
+ const scopeOf = ({ agent, teamOnly, thread }) => {
116
+ const own = teamOnly ? threadsOf(agent) : null;
117
+ if (!thread) return own;
118
+ return !own || own.has(thread) ? new Set([thread]) : new Set();
119
+ };
120
+
114
121
  const holdsIn = (item, folder) =>
115
122
  !item.folder ||
116
123
  (folder && path.resolve(item.folder) === path.resolve(folder));
@@ -174,14 +181,16 @@ export function createSwarmMemory({
174
181
 
175
182
  /**
176
183
  * What the memory holds for a query, best first: { posts, observations, lessons, weak }. `teamOnly` limits posts to
177
- * the threads `agent` is on; `hosted` also leaves out the person's lessons and observations; `exclude(post)` drops
178
- * posts the asker already has (the room it is in).
184
+ * the threads `agent` is on; `thread` to one thread, with its jobs, and the observations and lessons that stand on
185
+ * it; `hosted` also leaves out the person's lessons and observations; `exclude(post)` drops posts the asker already
186
+ * has (the room it is in).
179
187
  */
180
188
  const recall = async (
181
189
  query,
182
190
  {
183
191
  agent = null,
184
192
  teamOnly = false,
193
+ thread = null,
185
194
  hosted = false,
186
195
  folder = null,
187
196
  // An agent's own search (swarm_memory) reads every folder, as its posts do; a brief keeps to the work's folder.
@@ -196,10 +205,11 @@ export function createSwarmMemory({
196
205
  .trim()
197
206
  .slice(0, QUERY_CHARS);
198
207
  if (!text) return { posts: [], observations: [], lessons: [], weak: true };
199
- const threads = teamOnly ? threadsOf(agent) : null;
200
- const allowed = (row) =>
201
- (!threads || (row.thread && threads.has(row.thread))) && !exclude(row);
202
- const words = store.memoryWords(text, POOL).filter(allowed);
208
+ const threads = scopeOf({ agent, teamOnly, thread });
209
+ // Both searches rank within the threads in scope, so a thread's posts are not crowded out by the whole swarm's.
210
+ const inScope = threads ? store.memoryIdsIn(threads) : null;
211
+ const allowed = (row) => !exclude(row);
212
+ const words = store.memoryWords(text, POOL, { threads }).filter(allowed);
203
213
  let meaning = [];
204
214
  let best = null;
205
215
  const [vector] = (await embed([text], "query")) ?? [];
@@ -222,7 +232,9 @@ export function createSwarmMemory({
222
232
  }
223
233
  const hits = topK(
224
234
  vector,
225
- [...cache.rows].map(([id, row]) => ({ id, vector: row })),
235
+ [...cache.rows]
236
+ .filter(([id]) => !inScope || inScope.has(id))
237
+ .map(([id, row]) => ({ id, vector: row })),
226
238
  POOL * 2
227
239
  );
228
240
  // memoryPosts drops anything marked since it was cached, and keeps the hits' order.
@@ -273,7 +285,15 @@ export function createSwarmMemory({
273
285
  }
274
286
  ordered.push(row);
275
287
  }
276
- const weak = best === null ? words.length === 0 : best < WEAK_BELOW;
288
+ // The query's own words found as they are (an error code, a commit, a file name) are a match whatever their
289
+ // meaning scores.
290
+ const verbatim = words.some((row) =>
291
+ String(row.text).toLowerCase().includes(text.toLowerCase())
292
+ );
293
+ const weak =
294
+ !verbatim && (best === null ? words.length === 0 : best < WEAK_BELOW);
295
+ const standsOnThread = (item) =>
296
+ !thread || item.evidence.some((entry) => entry.goalId === thread);
277
297
  return {
278
298
  posts: ordered.slice(0, limit).map(viewOf),
279
299
  observations: hosted
@@ -287,7 +307,7 @@ export function createSwarmMemory({
287
307
  });
288
308
  const shown = new Set(closest.map((observation) => observation.id));
289
309
  return [
290
- ...closest,
310
+ ...closest.filter(standsOnThread),
291
311
  ...(followThreads
292
312
  ? threadObservations(found, {
293
313
  folder,
@@ -300,13 +320,16 @@ export function createSwarmMemory({
300
320
  })(),
301
321
  lessons: hosted
302
322
  ? []
303
- : store.lessonsMatching(text, 5).map((lesson) => ({
304
- about: lesson.about,
305
- agent: lesson.agent,
306
- text: lesson.text,
307
- quote: lesson.evidence.at(-1)?.quote ?? null,
308
- thread: lesson.evidence.at(-1)?.goalId ?? null,
309
- })),
323
+ : store
324
+ .lessonsMatching(text, 5)
325
+ .filter(standsOnThread)
326
+ .map((lesson) => ({
327
+ about: lesson.about,
328
+ agent: lesson.agent,
329
+ text: lesson.text,
330
+ quote: lesson.evidence.at(-1)?.quote ?? null,
331
+ thread: lesson.evidence.at(-1)?.goalId ?? null,
332
+ })),
310
333
  weak,
311
334
  // The closest post's similarity, when meaning was used: what `weak` was judged on.
312
335
  nearest: best,
@@ -83,6 +83,8 @@ export function createTaskCommands({
83
83
  yourTurns,
84
84
  authority,
85
85
  actors,
86
+ dispatches = null,
87
+ promptFlow = null,
86
88
  files: boardFiles,
87
89
  // Threads mirrored from another app (environments.mjs), when the person reads them here.
88
90
  outside = () => null,
@@ -153,6 +155,14 @@ export function createTaskCommands({
153
155
  };
154
156
  };
155
157
  return (request, { agent, id, expected, lastLooked, machine }) => {
158
+ if (request.command.startsWith("board.prompt."))
159
+ return promptFlow
160
+ ? promptFlow.command(request, { agent, id, machine })
161
+ : { ok: false, error: "prompt execution is unavailable" };
162
+ if (request.command.startsWith("board.dispatch."))
163
+ return dispatches
164
+ ? dispatches.command(request, { agent, id, machine })
165
+ : { ok: false, error: "typed dispatch is unavailable" };
156
166
  let outcome;
157
167
  // A call from one of the person's other computers, which home answers on its board (ownMachines.mjs).
158
168
  const fromElsewhere = Boolean(machine) && machine !== machines.self();
@@ -180,6 +190,70 @@ export function createTaskCommands({
180
190
  };
181
191
  }
182
192
  if (request.command === "board.post") {
193
+ if (Object.hasOwn(request, "promptDispatch")) {
194
+ if (typeof request.promptDispatch !== "boolean")
195
+ return { ok: false, error: "invalid prompt dispatch mode" };
196
+ if (request.promptDispatch) {
197
+ const promptFields = new Set([
198
+ "command",
199
+ "agent",
200
+ "id",
201
+ "callerPid",
202
+ "objective",
203
+ "brief",
204
+ "runWorkspace",
205
+ "requestRef",
206
+ "promptDispatch",
207
+ ]);
208
+ if (Object.keys(request).some((key) => !promptFields.has(key)))
209
+ return {
210
+ ok: false,
211
+ error: "unsupported prompt dispatch arguments",
212
+ };
213
+ if (
214
+ agent !== "user" ||
215
+ request[RUN_CALLER] ||
216
+ request.hostedCaller ||
217
+ request.parentId ||
218
+ request.thread ||
219
+ request.targetAgent ||
220
+ request.targetAgents ||
221
+ request.targetMachine ||
222
+ request.deferDispatch ||
223
+ request.review ||
224
+ request.inSession ||
225
+ request.model
226
+ )
227
+ return {
228
+ ok: false,
229
+ error:
230
+ "prompt dispatch requires an owner prompt and its workspace context",
231
+ };
232
+ return dispatches.command(
233
+ {
234
+ command: "board.dispatch.prompt",
235
+ agent,
236
+ prompt: [
237
+ String(request.objective || ""),
238
+ String(request.brief || ""),
239
+ ]
240
+ .filter(Boolean)
241
+ .join("\n\n"),
242
+ runWorkspace: request.runWorkspace,
243
+ requestRef: request.requestRef ?? request.id,
244
+ },
245
+ { agent, id, machine }
246
+ );
247
+ }
248
+ }
249
+ if (
250
+ (Object.hasOwn(request, "deferDispatch") &&
251
+ typeof request.deferDispatch !== "boolean") ||
252
+ (Object.hasOwn(request, "dispatchStarts") &&
253
+ (request.deferDispatch !== true ||
254
+ ![0, 1].includes(request.dispatchStarts)))
255
+ )
256
+ return { ok: false, error: "invalid typed dispatch options" };
183
257
  const objective = String(request.objective || "").trim();
184
258
  if (!objective) return { ok: false, error: "a task needs an objective" };
185
259
  // A thread with another person themselves (not one of their agents) is the person's own: no agent here claims or runs it.
@@ -270,6 +344,23 @@ export function createTaskCommands({
270
344
  viaServer: request.hostedCaller === true,
271
345
  });
272
346
  const goalParent = parentTask?.kind === "goal" ? parentTask : null;
347
+ const deferred = request.deferDispatch === true;
348
+ if (
349
+ deferred &&
350
+ (agent !== "user" ||
351
+ provenance.trust !== "user" ||
352
+ request[RUN_CALLER] ||
353
+ request.hostedCaller ||
354
+ request.thread ||
355
+ request.parentId ||
356
+ request.targetAgents ||
357
+ ![0, 1].includes(request.dispatchStarts ?? 1))
358
+ )
359
+ return {
360
+ ok: false,
361
+ error:
362
+ "only the owner can stage an exact local run with a zero-or-one start limit",
363
+ };
273
364
  if (goalParent) {
274
365
  if (!inRoom(goalParent, agent))
275
366
  return { ok: false, error: `${agent} is not on this goal's team` };
@@ -509,7 +600,7 @@ export function createTaskCommands({
509
600
  // Asking for a run in a folder is the person's Start; a scratch folder they never asked for goes by standing permission.
510
601
  runWorkspace: thread ? null : runWorkspace,
511
602
  runAuthorizedBy:
512
- !thread && runWorkspace && !scratch && agent === "user"
603
+ !deferred && !thread && runWorkspace && !scratch && agent === "user"
513
604
  ? "user"
514
605
  : null,
515
606
  targetMachine,
@@ -525,6 +616,22 @@ export function createTaskCommands({
525
616
  getProjects()?.channelOpened(task.id);
526
617
  }
527
618
  if (pick) setRunPick(store, task.id, pick, agent);
619
+ if (deferred) {
620
+ try {
621
+ if (!dispatches) throw new Error("typed dispatch is unavailable");
622
+ dispatches.stage(store.getTask(task.id), request.dispatchStarts ?? 1);
623
+ notifyBoard(store.getTask(task.id));
624
+ return { ok: true, task: store.getTask(task.id) };
625
+ } catch (error) {
626
+ store.failUnstartedDispatch(task.id, error.message);
627
+ notifyBoard(store.getTask(task.id));
628
+ return {
629
+ ok: false,
630
+ error: error.message,
631
+ task: store.getTask(task.id),
632
+ };
633
+ }
634
+ }
528
635
  // A job in a goal's room runs on the goal's authority; anywhere else, standing permission decides. The goal covers
529
636
  // jobs from the person, Harness and the coding agents on this machine, with the person's go (personsGo) unless an
530
637
  // agent's words came from above the poster; a job from a member that runs elsewhere (Grok Bot, Muse, a connected
@@ -1097,7 +1204,24 @@ export function createTaskCommands({
1097
1204
  else if (request.command === "board.machine.info") {
1098
1205
  if (!fromElsewhere)
1099
1206
  return { ok: false, error: "only another computer says what it has" };
1207
+ if (
1208
+ !Array.isArray(request.folders) ||
1209
+ request.folders.some(
1210
+ (folder) =>
1211
+ typeof folder?.path !== "string" ||
1212
+ !path.isAbsolute(folder.path) ||
1213
+ typeof folder.repository !== "string"
1214
+ )
1215
+ )
1216
+ return {
1217
+ ok: false,
1218
+ error: "worker inventory needs valid folder and repository entries",
1219
+ };
1100
1220
  machines.learn(machine, {
1221
+ request:
1222
+ typeof request.inventoryRequest === "string"
1223
+ ? request.inventoryRequest
1224
+ : null,
1101
1225
  folders: (Array.isArray(request.folders) ? request.folders : [])
1102
1226
  .filter(
1103
1227
  (folder) =>
package/lib/team.json CHANGED
@@ -1,5 +1,15 @@
1
1
  {
2
2
  "version": 2,
3
+ "fileSpecialties": [
4
+ "building-features",
5
+ "debugging",
6
+ "testing",
7
+ "code-review",
8
+ "frontend",
9
+ "infrastructure",
10
+ "data-sql",
11
+ "smart-contracts"
12
+ ],
3
13
  "rules": [
4
14
  "Whoever already holds the context does the work. Hand a piece to an agent new to the thread only when it needs little reading in and its answer can be checked.",
5
15
  "The dispatcher routes and never frames, makes or judges. The framer says what good looks like, and the frame goes with the work, so the check and the judge test against it rather than the maker's own idea of done.",