tryharness 0.7.72 → 0.7.73

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 (47) hide show
  1. package/assets/opencode-plugin.js +70 -3
  2. package/lib/asks.mjs +2 -0
  3. package/lib/boardCommands.mjs +9 -1
  4. package/lib/boardProtocol.json +23 -19
  5. package/lib/bridge.mjs +2 -0
  6. package/lib/capabilityCheck.mjs +86 -0
  7. package/lib/computerAddress.mjs +6 -14
  8. package/lib/coordinator.mjs +207 -26
  9. package/lib/daemon.mjs +150 -125
  10. package/lib/dispatchContract.json +1 -31
  11. package/lib/dispatchTools.js +8 -39
  12. package/lib/dispatches.mjs +27 -285
  13. package/lib/goalCommands.mjs +7 -3
  14. package/lib/herdr.mjs +159 -0
  15. package/lib/hookDialects.mjs +36 -2
  16. package/lib/jobAnswers.mjs +3 -0
  17. package/lib/kanban.mjs +256 -0
  18. package/lib/localAgentAsk.mjs +20 -29
  19. package/lib/machineRoutes.mjs +1 -5
  20. package/lib/manager.mjs +254 -0
  21. package/lib/mcp-stdio.mjs +58 -39
  22. package/lib/moves.mjs +365 -0
  23. package/lib/nudge.mjs +13 -3
  24. package/lib/ownMachines.mjs +43 -6
  25. package/lib/places.mjs +29 -2
  26. package/lib/runAuthority.mjs +2 -14
  27. package/lib/runBrief.mjs +14 -1
  28. package/lib/runWorker.mjs +4 -10
  29. package/lib/runner.mjs +1 -49
  30. package/lib/sessionControl.mjs +65 -1
  31. package/lib/steerHook.mjs +124 -0
  32. package/lib/steerQueue.mjs +176 -0
  33. package/lib/store.mjs +9 -23
  34. package/lib/subagentSteering.mjs +363 -0
  35. package/lib/subagents.mjs +138 -29
  36. package/lib/swarm.mjs +13 -3
  37. package/lib/taskCommands.mjs +0 -62
  38. package/lib/threadActions.mjs +4 -2
  39. package/lib/threadBriefs.mjs +9 -3
  40. package/lib/threadRuns.mjs +13 -0
  41. package/npm-shrinkwrap.json +14 -14
  42. package/package.json +4 -4
  43. package/release.json +44 -40
  44. package/lib/comfyExecution.mjs +0 -545
  45. package/lib/promptDecider.mjs +0 -471
  46. package/lib/promptFlow.mjs +0 -597
  47. package/lib/promptScopes.mjs +0 -670
@@ -1,597 +0,0 @@
1
- import { createHash, randomUUID } from "node:crypto";
2
- import { RUN_CALLER } from "./runAuthority.mjs";
3
- import { FROM_MACHINE } from "./machineRoutes.mjs";
4
-
5
- const hash = (value) =>
6
- createHash("sha256").update(JSON.stringify(value)).digest("hex");
7
- const key = (id) => `prompt-flow:${id}`;
8
- const exact = (value, names) =>
9
- value &&
10
- typeof value === "object" &&
11
- Object.keys(value).length === names.length &&
12
- names.every((name) => Object.hasOwn(value, name));
13
- const day = () => {
14
- const value = new Date();
15
- value.setHours(0, 0, 0, 0);
16
- return value.toISOString();
17
- };
18
-
19
- /** Normal prompt admission and delivery. The model selects offered assignments;
20
- * the selected computer owns execution authority and its durable effect receipt.
21
- */
22
- export function createPromptFlow({
23
- store,
24
- machine,
25
- computers,
26
- remote,
27
- executeHere,
28
- authority,
29
- privacy,
30
- artifact,
31
- notify,
32
- dispatch,
33
- decider,
34
- }) {
35
- const active = new Map();
36
- const read = (id) => JSON.parse(store.meta(key(id)) || "null");
37
- const save = (job) => store.meta(key(job.id), JSON.stringify(job));
38
- const taskFor = (job) => ({
39
- ...store.getTask(job.task_id),
40
- files: store.taskFiles(job.task_id).map((file) => ({
41
- ...file,
42
- createdAt: file.created_at,
43
- shared: file.shared_with_cloud === true,
44
- })),
45
- });
46
- const response = (job) => ({
47
- ok: true,
48
- task: taskFor(job),
49
- phase: job.state,
50
- execution: job.execution ?? null,
51
- });
52
- const send = (id, call) =>
53
- id === machine().id ? executeHere(call, machine().id) : remote(id, call);
54
- const revokeFiles = (scopeRef) =>
55
- dispatch(
56
- { command: "board.dispatch.scope.revoke", scopeRef, agent: "user" },
57
- { agent: "user", id: "", machine: machine().id }
58
- );
59
- const held = (job, error) => {
60
- if (job.state === "cancelled") return job;
61
- const next = { ...job, state: "held", error };
62
- save(next);
63
- const task = store.getTask(job.task_id);
64
- if (task.status === "claimed")
65
- store.reportTask(task.id, "harness", {
66
- status: "needs_input",
67
- result: error,
68
- });
69
- notify(store.getTask(job.task_id));
70
- return next;
71
- };
72
- // Words no typed work here can take go to Harness as any message to it does, and wait for its window: they were held
73
- // behind a card that asked the person about the error ("Harness is asking: No registered execution capability is
74
- // available").
75
- const toHarness = (job, why) => {
76
- if (job.state === "cancelled") return job;
77
- const task = store.postTask({
78
- objective: job.prompt.slice(0, 500),
79
- brief: job.prompt,
80
- originAgent: "user",
81
- targetAgent: "harness",
82
- });
83
- store.meta(`prompt-flow-task:${task.id}`, job.id);
84
- store.removeTask(job.task_id);
85
- const next = { ...job, state: "harness", task_id: task.id, why };
86
- save(next);
87
- notify(store.getTask(task.id));
88
- return next;
89
- };
90
- const applyReceipt = async (job, receipt) => {
91
- job = read(job.id);
92
- if (job.state === "cancelled") return job;
93
- if (
94
- receipt?.operation_ref !== job.operation_ref ||
95
- receipt.computer !== job.assignment.computer ||
96
- receipt.prompt !== job.prompt ||
97
- receipt.policy_ref !== job.assignment.policy_ref ||
98
- receipt.owner_epoch !== 1 ||
99
- ![
100
- "submitting",
101
- "running",
102
- "unknown",
103
- "done",
104
- "failed",
105
- "cancelled",
106
- ].includes(receipt.state) ||
107
- (job.execution?.attempt_ref &&
108
- job.execution.attempt_ref !== receipt.attempt_ref) ||
109
- (job.execution?.prompt_id &&
110
- job.execution.prompt_id !== receipt.prompt_id)
111
- )
112
- throw new Error("The execution receipt does not match its dispatch");
113
- if (job.execution?.state === "done") return job;
114
- let next = { ...job, state: receipt.state, execution: receipt };
115
- if (receipt.state === "done") {
116
- const image = await send(job.assignment.computer, {
117
- operation: "image",
118
- operation_ref: job.operation_ref,
119
- });
120
- const bytes = Buffer.from(image?.data ?? "", "base64");
121
- const actual = createHash("sha256").update(bytes).digest("hex");
122
- if (
123
- !image?.ok ||
124
- bytes.length > 5 * 1024 * 1024 ||
125
- actual !== receipt.sha256 ||
126
- bytes.length !== receipt.bytes ||
127
- receipt.mime !== "image/png" ||
128
- !/^[\w.-]+\.png$/.test(receipt.name) ||
129
- !bytes
130
- .subarray(0, 8)
131
- .equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]))
132
- )
133
- throw new Error("The returned image does not match the worker receipt");
134
- const fresh = read(job.id);
135
- if (fresh.state === "cancelled") return fresh;
136
- const file = artifact({
137
- taskId: job.task_id,
138
- name: receipt.name,
139
- mime: "image/png",
140
- bytes,
141
- author: "harness",
142
- });
143
- next = { ...next, file_id: file.id };
144
- store.reportTask(job.task_id, "harness", {
145
- status: "done",
146
- result: `Image generated on ${receipt.computer_label}.\n\n${file.name}\nExecution: ${receipt.attempt_ref}`,
147
- });
148
- }
149
- if (["failed", "cancelled"].includes(receipt.state))
150
- store.reportTask(job.task_id, "harness", {
151
- status: "failed",
152
- result: receipt.error || "The worker cancelled this execution.",
153
- failure: "provider",
154
- });
155
- save(next);
156
- notify(store.getTask(job.task_id));
157
- return next;
158
- };
159
- const status = async (job) => {
160
- if (active.has(job.id)) return response(job);
161
- if (["done", "held", "cancelled", "harness"].includes(job.state))
162
- return response(job);
163
- if (job.state === "interpreting" && !job.assignment && !job.file_task)
164
- return response(
165
- held(
166
- job,
167
- "The interpreter was interrupted; its reserved start remains held for reconciliation."
168
- )
169
- );
170
- if (job.file_task) {
171
- const children = store.children(job.file_root);
172
- if (
173
- children.some((task) =>
174
- ["failed", "unknown", "needs_input"].includes(task.status)
175
- )
176
- )
177
- return response(
178
- held(
179
- job,
180
- "A file-work child needs reconciliation or an owner answer; its attempt will not be retried."
181
- )
182
- );
183
- if (children.length && children.every((task) => task.status === "done")) {
184
- for (const task of children)
185
- for (const file of store.taskFiles(task.id)) {
186
- // Existing blobs remain private; link each immutable file into the prompt delivery.
187
- if (
188
- store
189
- .taskFiles(job.task_id)
190
- .some(
191
- (prior) =>
192
- prior.sha256 === file.sha256 && prior.name === file.name
193
- )
194
- )
195
- continue;
196
- store.addFile({
197
- taskId: job.task_id,
198
- name: file.name,
199
- mime: file.mime,
200
- bytes: file.bytes,
201
- sha256: file.sha256,
202
- author: file.author,
203
- });
204
- }
205
- store.reportTask(job.task_id, "harness", {
206
- status: "done",
207
- result: children
208
- .map((task) => task.result)
209
- .filter(Boolean)
210
- .join("\n\n"),
211
- });
212
- save({ ...job, state: "done" });
213
- }
214
- return response(read(job.id));
215
- }
216
- if (job.assignment && !["done", "held"].includes(job.state)) {
217
- const reply = await send(job.assignment.computer, {
218
- operation: "status",
219
- operation_ref: job.operation_ref,
220
- });
221
- if (reply?.ok) job = await applyReceipt(job, reply.execution);
222
- }
223
- return response(job);
224
- };
225
- const start = async (request) => {
226
- if (
227
- typeof request.id !== "string" ||
228
- !/^[A-Za-z0-9][A-Za-z0-9_.:@-]*$/.test(request.id) ||
229
- request.id.length > 128 ||
230
- typeof request.prompt !== "string" ||
231
- !request.prompt.trim() ||
232
- request.prompt.length > 16000 ||
233
- Object.keys(request).some(
234
- (name) =>
235
- !(name === "path" && request.path === undefined) &&
236
- ![
237
- "command",
238
- "agent",
239
- "id",
240
- "prompt",
241
- "workspace",
242
- "callerPid",
243
- ].includes(name)
244
- )
245
- )
246
- return {
247
- ok: false,
248
- error: "A prompt and stable request identity are required",
249
- };
250
- const previous = read(request.id);
251
- if (previous)
252
- return previous.prompt === request.prompt &&
253
- previous.workspace === (request.workspace ?? null)
254
- ? status(previous)
255
- : {
256
- ok: false,
257
- error: "This request identity already belongs to another prompt",
258
- };
259
- const root = store.postTask({
260
- objective: request.prompt,
261
- originAgent: "user",
262
- });
263
- store.claimTask(root.id, "harness", root.version, null, machine().id);
264
- const job = {
265
- id: request.id,
266
- task_id: root.id,
267
- prompt: request.prompt,
268
- workspace: request.workspace ?? null,
269
- state: "interpreting",
270
- operation_ref: `render:${randomUUID()}`,
271
- };
272
- save(job);
273
- store.meta(`prompt-flow-task:${root.id}`, job.id);
274
- store.meta(`prompt-flow-operation:${job.operation_ref}`, job.id);
275
- const decide = async () => {
276
- try {
277
- if (privacy())
278
- throw new Error(
279
- "A verified local-only prompt interpreter is not registered"
280
- );
281
- const rule = authority
282
- .standingRules()
283
- .find((r) => r.agent === "claude-code");
284
- if (!rule || rule.mode === "off" || rule.runs_today >= rule.daily_cap)
285
- throw new Error(
286
- "The prompt interpreter has no current standing start allowance"
287
- );
288
- const candidates = [];
289
- for (const computer of computers()) {
290
- if (computer.connected === false) continue;
291
- const capability = await send(computer.id, { operation: "catalog" });
292
- if (capability?.ok && capability.computer === computer.id)
293
- candidates.push({
294
- ...capability,
295
- assignment_ref: `assignment:${hash([computer.id, capability.policy_ref])}`,
296
- label: computer.label,
297
- });
298
- }
299
- const folder =
300
- request.workspace ??
301
- (rule.workspaces.length === 1 ? rule.workspaces[0] : null);
302
- if (
303
- folder &&
304
- !authority.standingVerdict({
305
- trust: "user",
306
- target: "claude-code",
307
- workspace: folder,
308
- capacity: false,
309
- })
310
- )
311
- candidates.push({
312
- assignment_ref: `assignment:${hash([machine().id, folder, rule.set_at])}`,
313
- computer: machine().id,
314
- label: machine().label,
315
- workspace: folder,
316
- capability_ref: "capability:workspace-files@1",
317
- });
318
- // Nothing here can run typed work and the words name no computer: they are a message for Harness. Words that
319
- // name a computer that cannot take them still wait for the person, never routed elsewhere.
320
- const namesComputer = computers().some((c) =>
321
- job.prompt.toLowerCase().includes(c.label.toLowerCase())
322
- );
323
- if (!candidates.length && !namesComputer) {
324
- toHarness(read(job.id), "no registered execution capability");
325
- return;
326
- }
327
- if (!candidates.length)
328
- throw new Error("No registered execution capability is available");
329
- const modelInfo = decider.info();
330
- if (!modelInfo.ok) throw new Error(modelInfo.error);
331
- const quotaKey = `prompt-quota:${day()}:claude-code`;
332
- store.dispatchTransaction(() => {
333
- const fresh = authority
334
- .standingRules()
335
- .find((r) => r.agent === "claude-code");
336
- if (
337
- !fresh ||
338
- fresh.mode === "off" ||
339
- fresh.runs_today >= fresh.daily_cap ||
340
- fresh.set_at !== rule.set_at
341
- )
342
- throw new Error("The start permission changed");
343
- store.meta(quotaKey, String(Number(store.meta(quotaKey) || 0) + 1));
344
- });
345
- const selection = await decider.select({
346
- prompt: job.prompt,
347
- requestRef: job.id,
348
- // Model context names a computer; the assignment ref binds its durable
349
- // computer id and executor policy in the code-owned candidate table.
350
- candidates: candidates.map((candidate) => ({
351
- assignment_ref: candidate.assignment_ref,
352
- computer: candidate.label,
353
- label: candidate.workspace
354
- ? "Claude: source reads and staged text artifacts only"
355
- : "ComfyUI: one new image; no edits, service startup or installation",
356
- capability_ref: candidate.capability_ref,
357
- ...(candidate.workspace ? { workspace: candidate.workspace } : {}),
358
- })),
359
- });
360
- if (decider.info().decider_ref !== modelInfo.decider_ref)
361
- throw new Error("The prompt model changed during interpretation");
362
- if (
363
- !exact(selection, ["assignment_ref", "computer_source", "signoff"]) ||
364
- typeof selection.signoff !== "boolean"
365
- )
366
- throw new Error("Invalid prompt selection");
367
- if (selection.signoff) {
368
- held(job, "You requested signoff before work.");
369
- return;
370
- }
371
- const assignment = candidates.find(
372
- (c) => c.assignment_ref === selection.assignment_ref
373
- );
374
- if (!assignment)
375
- throw new Error(
376
- "No registered execution capability can complete this prompt"
377
- );
378
- const fresh = authority
379
- .standingRules()
380
- .find((r) => r.agent === "claude-code");
381
- if (
382
- !fresh ||
383
- fresh.mode === "off" ||
384
- fresh.set_at !== rule.set_at ||
385
- read(job.id).state === "cancelled" ||
386
- store.getTask(job.task_id).removed_at
387
- )
388
- throw new Error("The owner permission changed during interpretation");
389
- if (assignment.workspace) {
390
- if (selection.computer_source !== null)
391
- throw new Error(
392
- "A local file assignment cannot override a named computer"
393
- );
394
- if (
395
- computers().some(
396
- (c) =>
397
- c.id !== machine().id &&
398
- job.prompt.toLowerCase().includes(c.label.toLowerCase())
399
- )
400
- )
401
- throw new Error("A named remote computer cannot fall back to Home");
402
- const opened = dispatch(
403
- {
404
- command: "board.dispatch.prompt",
405
- agent: "user",
406
- prompt: job.prompt,
407
- requestRef: `flow:${job.id}`,
408
- runWorkspace: assignment.workspace,
409
- },
410
- { agent: "user", id: "", machine: machine().id }
411
- );
412
- const answer = await (opened.pending ?? opened);
413
- if (!answer.ok) throw new Error(answer.error || answer.code);
414
- if (read(job.id).state === "cancelled") {
415
- revokeFiles(answer.scope.scope_ref);
416
- return;
417
- }
418
- save({
419
- ...job,
420
- state: "running",
421
- file_task: answer.task.id,
422
- file_root: answer.scope.root_id,
423
- file_scope: answer.scope.scope_ref,
424
- });
425
- return;
426
- }
427
- const span = selection.computer_source;
428
- const named = computers().some((c) =>
429
- job.prompt.toLowerCase().includes(c.label.toLowerCase())
430
- );
431
- if (span !== null) {
432
- if (
433
- !exact(span, ["start", "end"]) ||
434
- !Number.isInteger(span.start) ||
435
- !Number.isInteger(span.end) ||
436
- span.start < 0 ||
437
- span.end > job.prompt.length ||
438
- job.prompt.slice(span.start, span.end).toLowerCase() !==
439
- assignment.label.toLowerCase()
440
- )
441
- throw new Error(
442
- "The selected computer lacks exact source evidence"
443
- );
444
- } else if (named || assignment.computer !== machine().id)
445
- throw new Error("A named remote computer cannot fall back to Home");
446
- const next = { ...job, state: "dispatching", assignment };
447
- save(next);
448
- const reply = await send(assignment.computer, {
449
- operation: "submit",
450
- operation_ref: job.operation_ref,
451
- prompt: job.prompt,
452
- policy_ref: assignment.policy_ref,
453
- computer: assignment.computer,
454
- owner_epoch: 1,
455
- });
456
- if (!reply?.ok) {
457
- const fresh = read(job.id);
458
- if (fresh.state === "cancelled" || fresh.execution) return;
459
- save({
460
- ...fresh,
461
- state: "unknown",
462
- error:
463
- reply?.error ||
464
- reply?.code ||
465
- "The submit answer is missing; reconcile before retrying",
466
- });
467
- return;
468
- }
469
- await applyReceipt(read(job.id), reply.execution);
470
- } catch (error) {
471
- held(read(job.id), error.message);
472
- } finally {
473
- active.delete(job.id);
474
- notify(store.getTask(job.task_id));
475
- }
476
- };
477
- active.set(job.id, true);
478
- void decide();
479
- return response(job);
480
- };
481
- const command = (request, { agent, machine: from }) => {
482
- if (request.command === "board.prompt.receipt") {
483
- const id = store.meta(
484
- `prompt-flow-operation:${request.execution?.operation_ref}`
485
- );
486
- const job = id && read(id);
487
- if (
488
- agent !== "user" ||
489
- request[RUN_CALLER] ||
490
- !request[FROM_MACHINE] ||
491
- !from ||
492
- !job ||
493
- job.assignment?.computer !== from
494
- )
495
- return {
496
- ok: false,
497
- error: "A receipt must come from its assigned computer",
498
- };
499
- return {
500
- pending: applyReceipt(job, request.execution)
501
- .then(() => ({ ok: true }))
502
- .catch((error) => ({ ok: false, error: error.message })),
503
- };
504
- }
505
- if (
506
- agent !== "user" ||
507
- request[RUN_CALLER] ||
508
- request.restricted ||
509
- request.review ||
510
- request.hostedCaller
511
- )
512
- return {
513
- ok: false,
514
- error: "Only an owner prompt can start or read this workflow",
515
- };
516
- if (request.command === "board.prompt.start")
517
- return {
518
- pending: start(request).catch((error) => ({
519
- ok: false,
520
- error: error.message,
521
- })),
522
- };
523
- if (request.command === "board.prompt.cancel") {
524
- const job = read(
525
- store.meta(`prompt-flow-task:${request.id}`) || request.id
526
- );
527
- if (!job) return { ok: false, error: "Prompt work not found" };
528
- if (job.state === "done") return response(job);
529
- // Handed to Harness as a plain message: taking it back removes it, as for any message to Harness.
530
- if (job.state === "harness") {
531
- store.removeTask(job.task_id);
532
- save({ ...job, state: "cancelled" });
533
- notify(store.getTask(job.task_id));
534
- return response(read(job.id));
535
- }
536
- store.dispatchTransaction(() => {
537
- const task = store.getTask(job.task_id);
538
- if (task.status === "needs_input") {
539
- const answered = store.answerTask(
540
- task.id,
541
- "user",
542
- "Cancel this prompt.",
543
- task.version
544
- );
545
- if (!answered.ok) throw new Error(answered.error);
546
- }
547
- if (store.getTask(job.task_id).status !== "failed") {
548
- const stopped = store.reportTask(job.task_id, "harness", {
549
- status: "failed",
550
- result:
551
- "You cancelled this prompt. Its reserved execution is not retried.",
552
- failure: "person",
553
- });
554
- if (!stopped.ok) throw new Error(stopped.error);
555
- }
556
- save({ ...job, state: "cancelled" });
557
- });
558
- if (job.file_scope) revokeFiles(job.file_scope);
559
- notify(store.getTask(job.task_id));
560
- return {
561
- pending: (async () => {
562
- if (job.assignment)
563
- await send(job.assignment.computer, {
564
- operation: "cancel",
565
- operation_ref: job.operation_ref,
566
- });
567
- return response(read(job.id));
568
- })(),
569
- };
570
- }
571
- if (request.command === "board.prompt.status") {
572
- const job = read(
573
- store.meta(`prompt-flow-task:${request.id}`) || request.id
574
- );
575
- return job
576
- ? {
577
- pending: status(job).catch((error) => ({
578
- ok: false,
579
- error: error.message,
580
- })),
581
- }
582
- : { ok: false, error: "Prompt work not found" };
583
- }
584
- return { ok: false, error: "Unknown prompt command" };
585
- };
586
- return {
587
- command,
588
- localReceipt: async (receipt) => {
589
- const id = store.meta(`prompt-flow-operation:${receipt.operation_ref}`);
590
- const job = id && read(id);
591
- if (!job || job.assignment?.computer !== machine().id)
592
- return { ok: false };
593
- await applyReceipt(job, receipt);
594
- return { ok: true };
595
- },
596
- };
597
- }