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