tryharness 0.7.38 → 0.7.40
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 +24 -1
- package/lib/daemon.mjs +44 -9
- package/lib/mcp-stdio.mjs +69 -15
- package/lib/plans.mjs +538 -0
- package/lib/swarm.mjs +3 -0
- package/package.json +1 -1
package/lib/boardProtocol.json
CHANGED
|
@@ -35,6 +35,8 @@
|
|
|
35
35
|
"board.live.where": "live",
|
|
36
36
|
"board.local.events": "watch",
|
|
37
37
|
"board.media.search": "tools",
|
|
38
|
+
"board.plan.add": "plans",
|
|
39
|
+
"board.plan.remove": "plans",
|
|
38
40
|
"board.post": "task",
|
|
39
41
|
"board.prompted": "prompt",
|
|
40
42
|
"board.relay.decide": "relay",
|
|
@@ -94,6 +96,12 @@
|
|
|
94
96
|
"board.swarm.start_run": "swarm",
|
|
95
97
|
"board.swarm.stop": "swarm",
|
|
96
98
|
"board.swarm.stop_all": "swarm",
|
|
99
|
+
"board.template.check": "plans",
|
|
100
|
+
"board.template.get": "plans",
|
|
101
|
+
"board.template.list": "plans",
|
|
102
|
+
"board.template.remove": "plans",
|
|
103
|
+
"board.template.save": "plans",
|
|
104
|
+
"board.template.use": "plans",
|
|
97
105
|
"board.thread.auto": "thread",
|
|
98
106
|
"board.thread.discard": "thread",
|
|
99
107
|
"board.thread.end": "thread",
|
|
@@ -164,6 +172,13 @@
|
|
|
164
172
|
"prefixes": [
|
|
165
173
|
"board.file."
|
|
166
174
|
]
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
"owner": "plans",
|
|
178
|
+
"prefixes": [
|
|
179
|
+
"board.plan.",
|
|
180
|
+
"board.template."
|
|
181
|
+
]
|
|
167
182
|
}
|
|
168
183
|
],
|
|
169
184
|
"access": {
|
|
@@ -409,7 +424,15 @@
|
|
|
409
424
|
"board.relay.request",
|
|
410
425
|
"board.relay.seen",
|
|
411
426
|
"board.relay.defaults",
|
|
412
|
-
"board.relay.peers"
|
|
427
|
+
"board.relay.peers",
|
|
428
|
+
"board.plan.add",
|
|
429
|
+
"board.plan.remove",
|
|
430
|
+
"board.template.check",
|
|
431
|
+
"board.template.list",
|
|
432
|
+
"board.template.get",
|
|
433
|
+
"board.template.save",
|
|
434
|
+
"board.template.remove",
|
|
435
|
+
"board.template.use"
|
|
413
436
|
],
|
|
414
437
|
"plainPerson": [
|
|
415
438
|
"board.standing.off"
|
package/lib/daemon.mjs
CHANGED
|
@@ -99,6 +99,7 @@ import { createRunWorker } from "./runWorker.mjs";
|
|
|
99
99
|
import { createRunAuthority } from "./runAuthority.mjs";
|
|
100
100
|
import { createActorRoster } from "./actorRoster.mjs";
|
|
101
101
|
import { createBoardCommands } from "./boardCommands.mjs";
|
|
102
|
+
import { createPlans } from "./plans.mjs";
|
|
102
103
|
import { createBoardWatches } from "./boardWatches.mjs";
|
|
103
104
|
import { createBoardTools } from "./boardTools.mjs";
|
|
104
105
|
import { createBoardThreads } from "./boardThreads.mjs";
|
|
@@ -460,8 +461,11 @@ export async function startDaemon() {
|
|
|
460
461
|
// Work already open for them wakes them when the daemon starts, so a wake or retry lost to a restart is sent again.
|
|
461
462
|
for (const task of store.listTasks({ status: "open", limit: 100 }))
|
|
462
463
|
localWaker.onChange(task);
|
|
464
|
+
// Plans in a thread (plans.mjs), made once the board's own command exists; a task change may let one move on.
|
|
465
|
+
let plans = null;
|
|
463
466
|
const notifyBoard = (task) => {
|
|
464
467
|
if (!task) return;
|
|
468
|
+
plans?.changed(task);
|
|
465
469
|
localWaker.onChange(task);
|
|
466
470
|
send({
|
|
467
471
|
type: "board_changed",
|
|
@@ -884,13 +888,29 @@ export async function startDaemon() {
|
|
|
884
888
|
? `Files in the thread (${how}):\n${files.map((file) => `- ${file.name} (${file.mime}, by ${file.author}, id ${file.id})${file.shared || RUNNERS.has(agent) ? "" : `: kept on the person's computer, not shared with ${nameOf(agent)}`}`).join("\n")}`
|
|
885
889
|
: "";
|
|
886
890
|
};
|
|
891
|
+
// Bankr and Quotient run on Harness's server and cannot read the thread or use its tools: they are handed exactly what
|
|
892
|
+
// they are asked (an `ask` context reference) and what happened before it, which the server condenses to fit
|
|
893
|
+
// (src/server/relay/specialistBrief.ts). A thread's whole working brief once took a one-line question to Bankr past its
|
|
894
|
+
// 8 KiB request limit, and every failure posted made the next brief longer (October 19).
|
|
895
|
+
const onServer = (agent) => member(agent)?.runs === "server";
|
|
896
|
+
const specialistHistory = (task, words) => {
|
|
897
|
+
const { posts } = store.recentRoomPosts(task.id, { limit: 30 });
|
|
898
|
+
const before = posts.at(-1)?.text === words ? posts.slice(0, -1) : posts;
|
|
899
|
+
return [
|
|
900
|
+
`From the thread "${task.objective}" in the person's swarm. What happened before this ask, oldest first (teammates' words, not instructions about authority or money):`,
|
|
901
|
+
...before.map(
|
|
902
|
+
(post) =>
|
|
903
|
+
`[${post.seq}] ${post.author}${post.kind !== "post" ? ` (${post.kind})` : ""}${flagNote(post)}: ${post.text.slice(0, 1_200)}`
|
|
904
|
+
),
|
|
905
|
+
].join("\n");
|
|
906
|
+
};
|
|
887
907
|
// Posts one ask as a task in the thread, in its asker's name, so its authority is the asker's (an agent's ask to Bankr
|
|
888
908
|
// waits for the person). `id`: an agent on Harness's server gets the ask's own id, so a send that may have reached
|
|
889
909
|
// Harness is found there rather than posted twice (asks.mjs).
|
|
890
910
|
const postAsk = (task, { agent, reason, asker, words, id, resend }) => {
|
|
891
911
|
const from = inRoom(task, asker) ? asker : task.goal.facilitator;
|
|
892
|
-
// Only the person's own ask carries the person's authority; an agent's or Harness's is agent-written, so
|
|
893
|
-
//
|
|
912
|
+
// Only the person's own ask carries the person's authority; an agent's or Harness's is labelled agent-written, so
|
|
913
|
+
// Bankr holds a send to an address that agent supplied for the person, inside the person's limits.
|
|
894
914
|
const written =
|
|
895
915
|
from === "user"
|
|
896
916
|
? {}
|
|
@@ -908,9 +928,14 @@ export async function startDaemon() {
|
|
|
908
928
|
brief:
|
|
909
929
|
agent === "harness"
|
|
910
930
|
? harnessAsk(task, words)
|
|
911
|
-
:
|
|
931
|
+
: onServer(agent)
|
|
932
|
+
? specialistHistory(task, words)
|
|
933
|
+
: swarmBrief(task, agent, reason, { ask: true }),
|
|
912
934
|
targetAgent: agent,
|
|
913
935
|
parentId: task.id,
|
|
936
|
+
...(onServer(agent)
|
|
937
|
+
? { contextRefs: [{ kind: "ask", ref: String(words) }] }
|
|
938
|
+
: {}),
|
|
914
939
|
...written,
|
|
915
940
|
});
|
|
916
941
|
if (agent === "harness" && posted?.ok && posted.task) {
|
|
@@ -1231,7 +1256,7 @@ export async function startDaemon() {
|
|
|
1231
1256
|
held: guard === undefined ? null : guardWait(author, agent, guard),
|
|
1232
1257
|
});
|
|
1233
1258
|
else if (ASKABLE.has(agent))
|
|
1234
|
-
told.push(askAgent(goalId, agent, reason, author));
|
|
1259
|
+
told.push(askAgent(goalId, agent, reason, author, post.text));
|
|
1235
1260
|
// Harness by name: the person's @harness or an agent's ask. A message naming no one falls to Harness as coordinator.
|
|
1236
1261
|
else if (
|
|
1237
1262
|
agent === "harness" &&
|
|
@@ -1285,14 +1310,14 @@ export async function startDaemon() {
|
|
|
1285
1310
|
);
|
|
1286
1311
|
// Harness's answer wakes the agents it names, as its other posts do: a next step it names for @claude is Claude's
|
|
1287
1312
|
// (October 15: "Next is a re-roll of F1… then Claude checks" woke no one). Other agents' answers wake only the asker.
|
|
1288
|
-
// An agent Harness names joins the thread, as one the coordinator hands work to does
|
|
1289
|
-
// positions
|
|
1313
|
+
// An agent Harness names joins the thread, as one the coordinator hands work to does. One that moves money or takes
|
|
1314
|
+
// positions is never woken by Harness naming it, even once it is in the thread: only the person's name for it or
|
|
1315
|
+
// another agent's ask (October 19: Harness explaining why Bankr failed, "@bankr" in its words, started two more
|
|
1316
|
+
// Bankr jobs).
|
|
1290
1317
|
const named =
|
|
1291
1318
|
who === "harness" && !root
|
|
1292
1319
|
? mentionsIn(task.result || "", ROOM_AGENTS).filter(
|
|
1293
|
-
(agent) =>
|
|
1294
|
-
agent !== who &&
|
|
1295
|
-
(!TAGGED_ONLY.includes(agent) || goal.goal.team.includes(agent))
|
|
1320
|
+
(agent) => agent !== who && !TAGGED_ONLY.includes(agent)
|
|
1296
1321
|
)
|
|
1297
1322
|
: [];
|
|
1298
1323
|
const joining = named.filter((agent) => !goal.goal.team.includes(agent));
|
|
@@ -1938,6 +1963,7 @@ export async function startDaemon() {
|
|
|
1938
1963
|
});
|
|
1939
1964
|
const swarmCommand = createSwarm({
|
|
1940
1965
|
store,
|
|
1966
|
+
plansOf: (threadId) => plans?.of(threadId) ?? [],
|
|
1941
1967
|
goals,
|
|
1942
1968
|
heardPerson,
|
|
1943
1969
|
lessons,
|
|
@@ -2063,6 +2089,7 @@ export async function startDaemon() {
|
|
|
2063
2089
|
return { ok: true };
|
|
2064
2090
|
},
|
|
2065
2091
|
watch: watchCommand,
|
|
2092
|
+
plans: (request, { agent }) => plans.command(request, agent),
|
|
2066
2093
|
tools: toolsCommand,
|
|
2067
2094
|
thread: threadCommand,
|
|
2068
2095
|
file: fileCommand,
|
|
@@ -2107,6 +2134,14 @@ export async function startDaemon() {
|
|
|
2107
2134
|
files: boardFiles,
|
|
2108
2135
|
}),
|
|
2109
2136
|
});
|
|
2137
|
+
plans = createPlans({
|
|
2138
|
+
store,
|
|
2139
|
+
roomAgents: ROOM_AGENTS,
|
|
2140
|
+
post: (request) => boardCommand(request),
|
|
2141
|
+
home: paths.home,
|
|
2142
|
+
log,
|
|
2143
|
+
onPlanChange: (threadId) => notifyBoard(store.getTask(threadId)),
|
|
2144
|
+
});
|
|
2110
2145
|
|
|
2111
2146
|
// The board worker. An authorized task for an agent installed here is claimed by the daemon
|
|
2112
2147
|
// in that agent's name, run through the same runner as a pushed run (so `tryharness task <id>
|
package/lib/mcp-stdio.mjs
CHANGED
|
@@ -102,11 +102,38 @@ const LOCAL_TOOLS = [
|
|
|
102
102
|
additionalProperties: false,
|
|
103
103
|
},
|
|
104
104
|
},
|
|
105
|
+
after: {
|
|
106
|
+
type: "array",
|
|
107
|
+
items: { type: "string" },
|
|
108
|
+
description:
|
|
109
|
+
"with parent_id set to a thread: ids of tasks in that thread this one waits for. It is posted once they are all done, with their answers in its brief, and does not start if one of them stops. Use it to lay out steps that depend on each other; work that can start now needs none",
|
|
110
|
+
},
|
|
105
111
|
},
|
|
106
112
|
required: ["objective"],
|
|
107
113
|
additionalProperties: false,
|
|
108
114
|
},
|
|
109
115
|
},
|
|
116
|
+
{
|
|
117
|
+
name: "use_template",
|
|
118
|
+
description:
|
|
119
|
+
"Start a thread's work from one of the person's saved templates: agents, what each is asked and which waits for which. Without name it lists the templates with their {{blanks}}. With name and parent_id (the thread) it adds the template's steps there, the blanks filled from fill; steps with nothing wired into them start now, the rest when the work they wait for is done.",
|
|
120
|
+
inputSchema: {
|
|
121
|
+
type: "object",
|
|
122
|
+
properties: {
|
|
123
|
+
name: { type: "string" },
|
|
124
|
+
parent_id: {
|
|
125
|
+
type: "string",
|
|
126
|
+
description: "the thread to add the steps to",
|
|
127
|
+
},
|
|
128
|
+
fill: {
|
|
129
|
+
type: "object",
|
|
130
|
+
additionalProperties: { type: "string" },
|
|
131
|
+
description: "a value for each {{blank}} in the template",
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
additionalProperties: false,
|
|
135
|
+
},
|
|
136
|
+
},
|
|
110
137
|
{
|
|
111
138
|
name: "wallet",
|
|
112
139
|
description:
|
|
@@ -609,6 +636,22 @@ const LOCAL_TOOLS = [
|
|
|
609
636
|
},
|
|
610
637
|
];
|
|
611
638
|
|
|
639
|
+
const postTask = (args) => ({
|
|
640
|
+
command: "board.post",
|
|
641
|
+
objective: args.objective,
|
|
642
|
+
brief: args.brief,
|
|
643
|
+
targetAgent: args.target_agent,
|
|
644
|
+
targetAgents: args.target_agents,
|
|
645
|
+
parentId: args.parent_id,
|
|
646
|
+
contextRefs: args.context_refs,
|
|
647
|
+
runWorkspace: args.run_workspace,
|
|
648
|
+
inSession: args.in_session === true,
|
|
649
|
+
model: args.model,
|
|
650
|
+
effort: args.effort,
|
|
651
|
+
posterCwd: process.cwd(),
|
|
652
|
+
posterPid: process.ppid,
|
|
653
|
+
});
|
|
654
|
+
|
|
612
655
|
const BOARD_CALLS = {
|
|
613
656
|
list_agents: () => ({ command: "board.swarm.agents" }),
|
|
614
657
|
list_models: (args) => ({
|
|
@@ -753,21 +796,32 @@ const BOARD_CALLS = {
|
|
|
753
796
|
hash: args.hash,
|
|
754
797
|
chain: args.chain,
|
|
755
798
|
}),
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
799
|
+
// Work that waits for other work in a thread is a plan step (plans.mjs) until what it waits for is done.
|
|
800
|
+
post_task: (args) =>
|
|
801
|
+
Array.isArray(args.after) && args.after.length
|
|
802
|
+
? {
|
|
803
|
+
command: "board.plan.add",
|
|
804
|
+
id: String(args.parent_id || ""),
|
|
805
|
+
nodes: [
|
|
806
|
+
{
|
|
807
|
+
key: "step",
|
|
808
|
+
agent: args.target_agent,
|
|
809
|
+
objective: args.objective,
|
|
810
|
+
brief: args.brief,
|
|
811
|
+
after: args.after,
|
|
812
|
+
},
|
|
813
|
+
],
|
|
814
|
+
}
|
|
815
|
+
: postTask(args),
|
|
816
|
+
use_template: (args) =>
|
|
817
|
+
args.name && args.parent_id
|
|
818
|
+
? {
|
|
819
|
+
command: "board.template.use",
|
|
820
|
+
name: String(args.name),
|
|
821
|
+
id: String(args.parent_id),
|
|
822
|
+
fill: args.fill ?? {},
|
|
823
|
+
}
|
|
824
|
+
: { command: "board.template.list" },
|
|
771
825
|
claim_task: (args) => ({
|
|
772
826
|
command: "board.claim",
|
|
773
827
|
id: String(args.id || ""),
|
package/lib/plans.mjs
ADDED
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
// Work wired to wait for other work in a thread, and templates of it. A plan node is an agent and what it is asked,
|
|
2
|
+
// waiting on the thread's tasks or other plan nodes ("after"). Once everything it waits on is done it is posted as an
|
|
3
|
+
// ordinary board task, so it starts, is authorized and runs where it would had it been posted then, with the answers
|
|
4
|
+
// it waited for in its brief. Work it waits on that stops or is removed stops it too, and the thread says why.
|
|
5
|
+
// The person wires nodes on the buddy's canvas; agents do it with post_task's `after`; a template is the same nodes
|
|
6
|
+
// and wires as JSON, with {{blanks}} filled in when it is used. A node can only wait on work that exists or on nodes
|
|
7
|
+
// given with it, so no wiring can loop.
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
import { randomUUID } from "node:crypto";
|
|
11
|
+
|
|
12
|
+
const MAX_NODES = 50;
|
|
13
|
+
const PROMPT_LIMIT = 8_000;
|
|
14
|
+
const ANSWER_LIMIT = 2_000;
|
|
15
|
+
const NAME_LIMIT = 80;
|
|
16
|
+
const BLANK = /\{\{\s*([A-Za-z][\w-]{0,40})\s*\}\}/g;
|
|
17
|
+
// Work that has ended without an answer to pass on.
|
|
18
|
+
const STOPPED = new Set(["failed", "unknown"]);
|
|
19
|
+
|
|
20
|
+
const words = (text) => String(text ?? "").trim();
|
|
21
|
+
const slugOf = (name) =>
|
|
22
|
+
words(name)
|
|
23
|
+
.toLowerCase()
|
|
24
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
25
|
+
.replace(/^-+|-+$/g, "")
|
|
26
|
+
.slice(0, 60);
|
|
27
|
+
const blanksIn = (texts) => [
|
|
28
|
+
...new Set(
|
|
29
|
+
texts.flatMap((text) => [...String(text).matchAll(BLANK)].map((m) => m[1]))
|
|
30
|
+
),
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
/** Nodes in an order where each comes after everything it waits on, or null when the wiring loops. */
|
|
34
|
+
function ordered(nodes, waitsOn) {
|
|
35
|
+
const byKey = new Map(nodes.map((node) => [node.key, node]));
|
|
36
|
+
const done = new Set();
|
|
37
|
+
const visiting = new Set();
|
|
38
|
+
const out = [];
|
|
39
|
+
const visit = (node) => {
|
|
40
|
+
if (done.has(node.key)) return true;
|
|
41
|
+
if (visiting.has(node.key)) return false;
|
|
42
|
+
visiting.add(node.key);
|
|
43
|
+
for (const ref of waitsOn(node))
|
|
44
|
+
if (byKey.has(ref) && !visit(byKey.get(ref))) return false;
|
|
45
|
+
visiting.delete(node.key);
|
|
46
|
+
done.add(node.key);
|
|
47
|
+
out.push(node);
|
|
48
|
+
return true;
|
|
49
|
+
};
|
|
50
|
+
for (const node of nodes) if (!visit(node)) return null;
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A template as the person or an agent gave it, checked and cut down to what a template carries: each node's agent,
|
|
56
|
+
* what it is asked and where it sat, and the wires. Nothing else in the JSON (keys, sessions, files) is kept.
|
|
57
|
+
*/
|
|
58
|
+
export function checkTemplate(raw, { agents = null } = {}) {
|
|
59
|
+
let value = raw;
|
|
60
|
+
if (typeof raw === "string")
|
|
61
|
+
try {
|
|
62
|
+
value = JSON.parse(raw);
|
|
63
|
+
} catch {
|
|
64
|
+
return { ok: false, error: "that is not JSON" };
|
|
65
|
+
}
|
|
66
|
+
if (!value || typeof value !== "object" || !Array.isArray(value.nodes))
|
|
67
|
+
return { ok: false, error: "a template needs a list of nodes" };
|
|
68
|
+
if (!value.nodes.length)
|
|
69
|
+
return { ok: false, error: "a template needs at least one node" };
|
|
70
|
+
if (value.nodes.length > MAX_NODES)
|
|
71
|
+
return { ok: false, error: `a template holds at most ${MAX_NODES} nodes` };
|
|
72
|
+
const nodes = [];
|
|
73
|
+
for (const [i, node] of value.nodes.entries()) {
|
|
74
|
+
const key = words(node?.key) || `n${i + 1}`;
|
|
75
|
+
const agent = words(node?.agent);
|
|
76
|
+
const prompt = words(node?.prompt);
|
|
77
|
+
if (!agent) return { ok: false, error: `node ${key} names no agent` };
|
|
78
|
+
if (!prompt) return { ok: false, error: `node ${key} asks nothing` };
|
|
79
|
+
if (prompt.length > PROMPT_LIMIT)
|
|
80
|
+
return {
|
|
81
|
+
ok: false,
|
|
82
|
+
error: `node ${key}'s prompt is over ${PROMPT_LIMIT} characters`,
|
|
83
|
+
};
|
|
84
|
+
if (nodes.some((n) => n.key === key))
|
|
85
|
+
return { ok: false, error: `two nodes are called ${key}` };
|
|
86
|
+
const at =
|
|
87
|
+
Number.isFinite(node?.at?.x) && Number.isFinite(node?.at?.y)
|
|
88
|
+
? { x: Math.round(node.at.x), y: Math.round(node.at.y) }
|
|
89
|
+
: null;
|
|
90
|
+
nodes.push({ key, agent, prompt, ...(at ? { at } : {}) });
|
|
91
|
+
}
|
|
92
|
+
const keys = new Set(nodes.map((n) => n.key));
|
|
93
|
+
const wires = [];
|
|
94
|
+
for (const wire of Array.isArray(value.wires) ? value.wires : []) {
|
|
95
|
+
const from = words(wire?.from),
|
|
96
|
+
to = words(wire?.to);
|
|
97
|
+
if (!keys.has(from) || !keys.has(to))
|
|
98
|
+
return {
|
|
99
|
+
ok: false,
|
|
100
|
+
error: `a wire joins ${from || "?"} to ${to || "?"}, which are not both nodes`,
|
|
101
|
+
};
|
|
102
|
+
if (from === to) return { ok: false, error: `${from} is wired to itself` };
|
|
103
|
+
if (!wires.some((w) => w.from === from && w.to === to))
|
|
104
|
+
wires.push({ from, to });
|
|
105
|
+
}
|
|
106
|
+
const waitsOn = (node) =>
|
|
107
|
+
wires.filter((w) => w.to === node.key).map((w) => w.from);
|
|
108
|
+
if (!ordered(nodes, waitsOn))
|
|
109
|
+
return { ok: false, error: "the wires go round in a loop" };
|
|
110
|
+
const template = {
|
|
111
|
+
harness: "template",
|
|
112
|
+
version: 1,
|
|
113
|
+
name: words(value.name).slice(0, NAME_LIMIT) || "Untitled",
|
|
114
|
+
...(words(value.description)
|
|
115
|
+
? { description: words(value.description).slice(0, 500) }
|
|
116
|
+
: {}),
|
|
117
|
+
nodes,
|
|
118
|
+
wires,
|
|
119
|
+
};
|
|
120
|
+
return {
|
|
121
|
+
ok: true,
|
|
122
|
+
template,
|
|
123
|
+
blanks: blanksIn(nodes.map((n) => n.prompt)),
|
|
124
|
+
// Agents this computer's swarm does not have: the template still loads, the person picks another.
|
|
125
|
+
missing: agents
|
|
126
|
+
? [
|
|
127
|
+
...new Set(
|
|
128
|
+
nodes.map((n) => n.agent).filter((a) => !agents.includes(a))
|
|
129
|
+
),
|
|
130
|
+
]
|
|
131
|
+
: [],
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const fill = (text, values) =>
|
|
136
|
+
String(text).replace(BLANK, (whole, name) =>
|
|
137
|
+
Object.hasOwn(values, name) && words(values[name])
|
|
138
|
+
? words(values[name])
|
|
139
|
+
: whole
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* `post(request)` posts a task the way any agent's post does (the daemon's own board command); `changed(task)` hears
|
|
144
|
+
* every task change in a thread.
|
|
145
|
+
*/
|
|
146
|
+
export function createPlans({
|
|
147
|
+
store,
|
|
148
|
+
roomAgents,
|
|
149
|
+
post,
|
|
150
|
+
home,
|
|
151
|
+
log = () => {},
|
|
152
|
+
onPlanChange = () => {},
|
|
153
|
+
}) {
|
|
154
|
+
const folder = path.join(home, "templates");
|
|
155
|
+
const planOf = (threadId) => {
|
|
156
|
+
try {
|
|
157
|
+
return JSON.parse(store.meta(`plan:${threadId}`) || "[]");
|
|
158
|
+
} catch {
|
|
159
|
+
return [];
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
const savePlan = (threadId, plan) => {
|
|
163
|
+
store.meta(`plan:${threadId}`, JSON.stringify(plan));
|
|
164
|
+
onPlanChange(threadId);
|
|
165
|
+
};
|
|
166
|
+
const threadOf = (id) => {
|
|
167
|
+
const thread = store.getTask(String(id || ""));
|
|
168
|
+
return thread && thread.kind === "goal" ? thread : null;
|
|
169
|
+
};
|
|
170
|
+
// A plan node's id stands for the task it became once it was posted.
|
|
171
|
+
const taskFor = (plan, ref) =>
|
|
172
|
+
plan.find((node) => node.id === ref)?.taskId ?? ref;
|
|
173
|
+
|
|
174
|
+
// What each waiting node waits on now: everything done, something stopped, or still going.
|
|
175
|
+
function settle(threadId) {
|
|
176
|
+
const plan = planOf(threadId);
|
|
177
|
+
let changed = false;
|
|
178
|
+
for (let pass = 0; pass < MAX_NODES; pass++) {
|
|
179
|
+
let moved = false;
|
|
180
|
+
for (const node of plan) {
|
|
181
|
+
if (node.state !== "waiting") continue;
|
|
182
|
+
const inputs = node.after.map((ref) => {
|
|
183
|
+
const planned = plan.find((other) => other.id === ref);
|
|
184
|
+
if (planned && !planned.taskId)
|
|
185
|
+
return {
|
|
186
|
+
ref,
|
|
187
|
+
state: planned.state === "blocked" ? "stopped" : "waiting",
|
|
188
|
+
name: planned.agent,
|
|
189
|
+
};
|
|
190
|
+
const task = store.getTask(taskFor(plan, ref));
|
|
191
|
+
if (!task || task.removed_at)
|
|
192
|
+
return { ref, state: "stopped", name: "work that was removed" };
|
|
193
|
+
if (task.status === "done") return { ref, state: "done", task };
|
|
194
|
+
if (STOPPED.has(task.status))
|
|
195
|
+
return {
|
|
196
|
+
ref,
|
|
197
|
+
state: "stopped",
|
|
198
|
+
name: task.claimed_by || task.target_agent,
|
|
199
|
+
};
|
|
200
|
+
return { ref, state: "waiting" };
|
|
201
|
+
});
|
|
202
|
+
const stopped = inputs.find((input) => input.state === "stopped");
|
|
203
|
+
if (stopped) {
|
|
204
|
+
node.state = "blocked";
|
|
205
|
+
node.reason = `${stopped.name} stopped before it finished`;
|
|
206
|
+
moved = changed = true;
|
|
207
|
+
store.roomPost({
|
|
208
|
+
goalId: threadId,
|
|
209
|
+
author: "harness",
|
|
210
|
+
kind: "note",
|
|
211
|
+
text: `${node.agent}'s next step ("${node.objective.slice(0, 80)}") will not start: ${node.reason}.`,
|
|
212
|
+
});
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (inputs.some((input) => input.state === "waiting")) continue;
|
|
216
|
+
const answers = inputs
|
|
217
|
+
.filter((input) => input.task)
|
|
218
|
+
.map(
|
|
219
|
+
({ task }) =>
|
|
220
|
+
`${task.claimed_by || task.target_agent} ("${task.objective.slice(0, 120)}"): ${String(task.result || "finished").slice(0, ANSWER_LIMIT)}`
|
|
221
|
+
);
|
|
222
|
+
const brief = [
|
|
223
|
+
node.brief,
|
|
224
|
+
answers.length
|
|
225
|
+
? `This step was wired to wait for this work in the thread, now done:\n\n${answers.join("\n\n")}`
|
|
226
|
+
: "",
|
|
227
|
+
]
|
|
228
|
+
.filter(Boolean)
|
|
229
|
+
.join("\n\n");
|
|
230
|
+
const posted = post({
|
|
231
|
+
command: "board.post",
|
|
232
|
+
agent: node.by,
|
|
233
|
+
objective: node.objective,
|
|
234
|
+
brief,
|
|
235
|
+
targetAgent: node.agent,
|
|
236
|
+
parentId: threadId,
|
|
237
|
+
});
|
|
238
|
+
if (posted?.ok && posted.task?.id) {
|
|
239
|
+
node.taskId = posted.task.id;
|
|
240
|
+
node.state = "posted";
|
|
241
|
+
} else {
|
|
242
|
+
node.state = "blocked";
|
|
243
|
+
node.reason = posted?.error || "it could not be posted";
|
|
244
|
+
log(`plan ${node.id} not posted: ${node.reason}`);
|
|
245
|
+
}
|
|
246
|
+
moved = changed = true;
|
|
247
|
+
}
|
|
248
|
+
if (!moved) break;
|
|
249
|
+
}
|
|
250
|
+
if (changed) savePlan(threadId, plan);
|
|
251
|
+
return plan;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Adds nodes to a thread's plan; those waiting on nothing (or only on finished work) start at once. */
|
|
255
|
+
function add(threadId, by, given) {
|
|
256
|
+
const thread = threadOf(threadId);
|
|
257
|
+
if (!thread) return { ok: false, error: "thread not found" };
|
|
258
|
+
if (!Array.isArray(given) || !given.length)
|
|
259
|
+
return { ok: false, error: "no nodes to add" };
|
|
260
|
+
const plan = planOf(thread.id);
|
|
261
|
+
if (plan.length + given.length > MAX_NODES * 4)
|
|
262
|
+
return { ok: false, error: "this thread's plan is full" };
|
|
263
|
+
const keys = new Set();
|
|
264
|
+
const nodes = [];
|
|
265
|
+
for (const [i, node] of given.entries()) {
|
|
266
|
+
const key = words(node?.key) || `n${i + 1}`;
|
|
267
|
+
if (keys.has(key))
|
|
268
|
+
return { ok: false, error: `two nodes are called ${key}` };
|
|
269
|
+
keys.add(key);
|
|
270
|
+
const agent = words(node?.agent);
|
|
271
|
+
if (!roomAgents.includes(agent))
|
|
272
|
+
return {
|
|
273
|
+
ok: false,
|
|
274
|
+
error: `${agent || "no one"} cannot be in a thread`,
|
|
275
|
+
};
|
|
276
|
+
const objective = words(node?.objective || node?.prompt)
|
|
277
|
+
.split("\n")[0]
|
|
278
|
+
.slice(0, 500);
|
|
279
|
+
const brief = words(
|
|
280
|
+
node?.brief ?? (words(node?.prompt).includes("\n") ? node.prompt : "")
|
|
281
|
+
);
|
|
282
|
+
if (!objective) return { ok: false, error: `node ${key} asks nothing` };
|
|
283
|
+
if ((objective + brief).length > PROMPT_LIMIT)
|
|
284
|
+
return {
|
|
285
|
+
ok: false,
|
|
286
|
+
error: `node ${key}'s prompt is over ${PROMPT_LIMIT} characters`,
|
|
287
|
+
};
|
|
288
|
+
nodes.push({
|
|
289
|
+
key,
|
|
290
|
+
agent,
|
|
291
|
+
objective,
|
|
292
|
+
brief,
|
|
293
|
+
after: Array.isArray(node?.after) ? node.after.map(String) : [],
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
// What a node may wait on: another node given with it, a node already planned here, or a task in this thread.
|
|
297
|
+
for (const node of nodes)
|
|
298
|
+
for (const ref of node.after) {
|
|
299
|
+
if (keys.has(ref) || plan.some((other) => other.id === ref)) continue;
|
|
300
|
+
const task = store.getTask(ref);
|
|
301
|
+
if (!task || task.parent_id !== thread.id)
|
|
302
|
+
return { ok: false, error: `${ref} is not work in this thread` };
|
|
303
|
+
}
|
|
304
|
+
const order = ordered(nodes, (node) =>
|
|
305
|
+
node.after.filter((ref) => keys.has(ref))
|
|
306
|
+
);
|
|
307
|
+
if (!order) return { ok: false, error: "the wires go round in a loop" };
|
|
308
|
+
const ids = new Map(
|
|
309
|
+
nodes.map((node) => [node.key, `plan_${randomUUID().slice(0, 12)}`])
|
|
310
|
+
);
|
|
311
|
+
const at = new Date().toISOString();
|
|
312
|
+
for (const node of order)
|
|
313
|
+
plan.push({
|
|
314
|
+
id: ids.get(node.key),
|
|
315
|
+
key: node.key,
|
|
316
|
+
by,
|
|
317
|
+
agent: node.agent,
|
|
318
|
+
objective: node.objective,
|
|
319
|
+
brief: node.brief,
|
|
320
|
+
after: node.after.map((ref) => ids.get(ref) ?? ref),
|
|
321
|
+
state: "waiting",
|
|
322
|
+
at,
|
|
323
|
+
});
|
|
324
|
+
savePlan(thread.id, plan);
|
|
325
|
+
const settled = settle(thread.id);
|
|
326
|
+
const mine = settled.filter((node) => [...ids.values()].includes(node.id));
|
|
327
|
+
return {
|
|
328
|
+
ok: true,
|
|
329
|
+
nodes: Object.fromEntries(
|
|
330
|
+
nodes.map((node) => [node.key, ids.get(node.key)])
|
|
331
|
+
),
|
|
332
|
+
started: mine.filter((node) => node.taskId).map((node) => node.taskId),
|
|
333
|
+
waiting: mine
|
|
334
|
+
.filter((node) => node.state === "waiting")
|
|
335
|
+
.map((node) => node.id),
|
|
336
|
+
blocked: mine
|
|
337
|
+
.filter((node) => node.state === "blocked")
|
|
338
|
+
.map((node) => ({ id: node.id, reason: node.reason })),
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// ── Templates, kept with the daemon so agents can use them too ────────────────
|
|
343
|
+
const templateFile = (name) => path.join(folder, `${slugOf(name)}.json`);
|
|
344
|
+
function templates() {
|
|
345
|
+
let names = [];
|
|
346
|
+
try {
|
|
347
|
+
names = fs.readdirSync(folder).filter((name) => name.endsWith(".json"));
|
|
348
|
+
} catch {
|
|
349
|
+
return [];
|
|
350
|
+
}
|
|
351
|
+
return names
|
|
352
|
+
.map((name) => {
|
|
353
|
+
const checked = checkTemplate(
|
|
354
|
+
fs.readFileSync(path.join(folder, name), "utf8")
|
|
355
|
+
);
|
|
356
|
+
return checked.ok
|
|
357
|
+
? {
|
|
358
|
+
name: checked.template.name,
|
|
359
|
+
description: checked.template.description ?? null,
|
|
360
|
+
nodes: checked.template.nodes.length,
|
|
361
|
+
agents: [...new Set(checked.template.nodes.map((n) => n.agent))],
|
|
362
|
+
blanks: checked.blanks,
|
|
363
|
+
}
|
|
364
|
+
: null;
|
|
365
|
+
})
|
|
366
|
+
.filter(Boolean)
|
|
367
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
368
|
+
}
|
|
369
|
+
const templateNamed = (name) => {
|
|
370
|
+
const file = templateFile(name);
|
|
371
|
+
if (!slugOf(name) || !fs.existsSync(file)) return null;
|
|
372
|
+
const checked = checkTemplate(fs.readFileSync(file, "utf8"), {
|
|
373
|
+
agents: roomAgents,
|
|
374
|
+
});
|
|
375
|
+
return checked.ok ? checked : null;
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
/** Puts a template's nodes into a thread, its blanks filled; the nodes with nothing wired into them start now. */
|
|
379
|
+
function fromTemplate(threadId, by, checked, values = {}) {
|
|
380
|
+
const empty = checked.blanks.filter((name) => !words(values[name]));
|
|
381
|
+
if (empty.length)
|
|
382
|
+
return {
|
|
383
|
+
ok: false,
|
|
384
|
+
error: `fill in ${empty.map((name) => `{{${name}}}`).join(", ")} first`,
|
|
385
|
+
blanks: checked.blanks,
|
|
386
|
+
};
|
|
387
|
+
if (checked.missing.length)
|
|
388
|
+
return {
|
|
389
|
+
ok: false,
|
|
390
|
+
error: `this computer's swarm has no ${checked.missing.join(", ")}`,
|
|
391
|
+
};
|
|
392
|
+
return add(
|
|
393
|
+
threadId,
|
|
394
|
+
by,
|
|
395
|
+
checked.template.nodes.map((node) => ({
|
|
396
|
+
key: node.key,
|
|
397
|
+
agent: node.agent,
|
|
398
|
+
prompt: fill(node.prompt, values),
|
|
399
|
+
after: checked.template.wires
|
|
400
|
+
.filter((w) => w.to === node.key)
|
|
401
|
+
.map((w) => w.from),
|
|
402
|
+
}))
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
const personOnly = (agent) =>
|
|
407
|
+
agent === "user"
|
|
408
|
+
? null
|
|
409
|
+
: { ok: false, error: "only the person can do that" };
|
|
410
|
+
|
|
411
|
+
return {
|
|
412
|
+
/** A thread's plan nodes as the canvas draws them. */
|
|
413
|
+
of: (threadId) =>
|
|
414
|
+
planOf(threadId).map(
|
|
415
|
+
({
|
|
416
|
+
id,
|
|
417
|
+
key,
|
|
418
|
+
by,
|
|
419
|
+
agent,
|
|
420
|
+
objective,
|
|
421
|
+
after,
|
|
422
|
+
state,
|
|
423
|
+
reason,
|
|
424
|
+
taskId,
|
|
425
|
+
at,
|
|
426
|
+
}) => ({
|
|
427
|
+
id,
|
|
428
|
+
key,
|
|
429
|
+
by,
|
|
430
|
+
agent,
|
|
431
|
+
objective,
|
|
432
|
+
after,
|
|
433
|
+
state,
|
|
434
|
+
reason: reason ?? null,
|
|
435
|
+
taskId: taskId ?? null,
|
|
436
|
+
at,
|
|
437
|
+
})
|
|
438
|
+
),
|
|
439
|
+
/** A task changed: plans in its thread may move on. */
|
|
440
|
+
changed(task) {
|
|
441
|
+
const threadId = task?.parent_id;
|
|
442
|
+
if (!threadId || !store.meta(`plan:${threadId}`)) return;
|
|
443
|
+
// After the change that called this has finished, not inside it.
|
|
444
|
+
setImmediate(() => {
|
|
445
|
+
try {
|
|
446
|
+
settle(threadId);
|
|
447
|
+
} catch (error) {
|
|
448
|
+
log(`plan settle failed for ${threadId}: ${error.message}`);
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
},
|
|
452
|
+
command(request, agent) {
|
|
453
|
+
const command = String(request.command);
|
|
454
|
+
if (command === "board.plan.add")
|
|
455
|
+
return add(request.id, agent, request.nodes);
|
|
456
|
+
if (command === "board.plan.remove") {
|
|
457
|
+
const refused = personOnly(agent);
|
|
458
|
+
if (refused) return refused;
|
|
459
|
+
const plan = planOf(String(request.id || ""));
|
|
460
|
+
const node = plan.find((n) => n.id === String(request.node || ""));
|
|
461
|
+
if (!node) return { ok: false, error: "no such step" };
|
|
462
|
+
if (node.taskId)
|
|
463
|
+
return {
|
|
464
|
+
ok: false,
|
|
465
|
+
error: "that step has started; stop it in the thread",
|
|
466
|
+
};
|
|
467
|
+
const waiting = plan.filter(
|
|
468
|
+
(n) => n.after.includes(node.id) && !n.taskId
|
|
469
|
+
);
|
|
470
|
+
if (waiting.length)
|
|
471
|
+
return {
|
|
472
|
+
ok: false,
|
|
473
|
+
error: "other steps wait for this one; remove them first",
|
|
474
|
+
};
|
|
475
|
+
savePlan(
|
|
476
|
+
String(request.id),
|
|
477
|
+
plan.filter((n) => n.id !== node.id)
|
|
478
|
+
);
|
|
479
|
+
return { ok: true };
|
|
480
|
+
}
|
|
481
|
+
if (command === "board.template.check")
|
|
482
|
+
return checkTemplate(request.template, { agents: roomAgents });
|
|
483
|
+
if (command === "board.template.list")
|
|
484
|
+
return { ok: true, templates: templates() };
|
|
485
|
+
if (command === "board.template.get") {
|
|
486
|
+
const found = templateNamed(String(request.name || ""));
|
|
487
|
+
return found ? found : { ok: false, error: "no template by that name" };
|
|
488
|
+
}
|
|
489
|
+
if (command === "board.template.save") {
|
|
490
|
+
const refused = personOnly(agent);
|
|
491
|
+
if (refused) return refused;
|
|
492
|
+
const checked = checkTemplate(request.template, { agents: roomAgents });
|
|
493
|
+
if (!checked.ok) return checked;
|
|
494
|
+
const name = words(request.name) || checked.template.name;
|
|
495
|
+
if (!slugOf(name))
|
|
496
|
+
return { ok: false, error: "a template needs a name" };
|
|
497
|
+
const template = {
|
|
498
|
+
...checked.template,
|
|
499
|
+
name: name.slice(0, NAME_LIMIT),
|
|
500
|
+
};
|
|
501
|
+
fs.mkdirSync(folder, { recursive: true });
|
|
502
|
+
fs.writeFileSync(
|
|
503
|
+
templateFile(name),
|
|
504
|
+
JSON.stringify(template, null, 2) + "\n"
|
|
505
|
+
);
|
|
506
|
+
return { ok: true, template, blanks: checked.blanks };
|
|
507
|
+
}
|
|
508
|
+
if (command === "board.template.remove") {
|
|
509
|
+
const refused = personOnly(agent);
|
|
510
|
+
if (refused) return refused;
|
|
511
|
+
const file = templateFile(String(request.name || ""));
|
|
512
|
+
if (!slugOf(String(request.name || "")) || !fs.existsSync(file))
|
|
513
|
+
return { ok: false, error: "no template by that name" };
|
|
514
|
+
fs.rmSync(file);
|
|
515
|
+
return { ok: true };
|
|
516
|
+
}
|
|
517
|
+
if (command === "board.template.use") {
|
|
518
|
+
const checked = request.template
|
|
519
|
+
? checkTemplate(request.template, { agents: roomAgents })
|
|
520
|
+
: templateNamed(String(request.name || ""));
|
|
521
|
+
if (!checked)
|
|
522
|
+
return {
|
|
523
|
+
ok: false,
|
|
524
|
+
error: "no template by that name",
|
|
525
|
+
templates: templates(),
|
|
526
|
+
};
|
|
527
|
+
if (!checked.ok) return checked;
|
|
528
|
+
return fromTemplate(
|
|
529
|
+
request.id,
|
|
530
|
+
agent,
|
|
531
|
+
checked,
|
|
532
|
+
request.fill && typeof request.fill === "object" ? request.fill : {}
|
|
533
|
+
);
|
|
534
|
+
}
|
|
535
|
+
return { ok: false, error: `unknown command ${command}` };
|
|
536
|
+
},
|
|
537
|
+
};
|
|
538
|
+
}
|
package/lib/swarm.mjs
CHANGED
|
@@ -109,6 +109,8 @@ export function createSwarm({
|
|
|
109
109
|
lessons = null,
|
|
110
110
|
// The subagents each run's own session spawned (subagents.mjs), for the thread's canvas.
|
|
111
111
|
subagents = createSubagents(),
|
|
112
|
+
// Work wired to wait for other work in the thread (plans.mjs).
|
|
113
|
+
plansOf = () => [],
|
|
112
114
|
}) {
|
|
113
115
|
// What the person let a thread spend of their Harness credits, in dollars, or null for no limit of its own.
|
|
114
116
|
const ceilingOf = (threadId) => {
|
|
@@ -408,6 +410,7 @@ export function createSwarm({
|
|
|
408
410
|
// What each of the person's posts changed in what the swarm keeps (lessons.mjs), for the line under the post.
|
|
409
411
|
kept: store.lessonsFromThread?.(thread.id) ?? [],
|
|
410
412
|
runs,
|
|
413
|
+
plans: hostedCaller ? [] : plansOf(thread.id),
|
|
411
414
|
// Posts held for the person (who wrote one, when, never its words), and the post that replaced each one an agent
|
|
412
415
|
// posted again.
|
|
413
416
|
held: store.heldPosts(thread.id).map((post) => ({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tryharness",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.40",
|
|
4
4
|
"description": "Harness Relay: the daemon that turns the agents on this machine into your personal swarm. Memory, history, and work handed between any of them.",
|
|
5
5
|
"bin": {
|
|
6
6
|
"tryharness": "cli.mjs"
|