tryharness 0.7.72 → 0.7.75
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/assets/opencode-plugin.js +70 -3
- package/assets/pi-extension.ts +7 -1
- package/cli.mjs +2 -2
- package/lib/asks.mjs +2 -0
- package/lib/boardCommands.mjs +9 -1
- package/lib/boardProtocol.json +23 -19
- package/lib/bridge.mjs +2 -0
- package/lib/capabilityCheck.mjs +86 -0
- package/lib/computerAddress.mjs +6 -14
- package/lib/coordinator.mjs +220 -35
- package/lib/daemon.mjs +160 -126
- package/lib/dispatchContract.json +1 -31
- package/lib/dispatchTools.js +8 -39
- package/lib/dispatches.mjs +27 -285
- package/lib/goalCommands.mjs +7 -3
- package/lib/herdr.mjs +159 -0
- package/lib/hookDialects.mjs +36 -2
- package/lib/jobAnswers.mjs +3 -0
- package/lib/kanban.mjs +256 -0
- package/lib/library.mjs +83 -7
- package/lib/localAgentAsk.mjs +20 -29
- package/lib/localStack.mjs +19 -3
- package/lib/machineRoutes.mjs +1 -5
- package/lib/manager.mjs +254 -0
- package/lib/mcp-stdio.mjs +72 -46
- package/lib/moves.mjs +365 -0
- package/lib/nudge.mjs +18 -3
- package/lib/ownMachines.mjs +43 -6
- package/lib/places.mjs +29 -2
- package/lib/runAuthority.mjs +2 -14
- package/lib/runBrief.mjs +14 -1
- package/lib/runWorker.mjs +4 -10
- package/lib/runner.mjs +1 -49
- package/lib/sessionControl.mjs +65 -1
- package/lib/steerHook.mjs +124 -0
- package/lib/steerQueue.mjs +176 -0
- package/lib/store.mjs +20 -29
- package/lib/subagentSteering.mjs +363 -0
- package/lib/subagents.mjs +138 -29
- package/lib/swarm.mjs +16 -6
- package/lib/taskCommands.mjs +0 -62
- package/lib/threadActions.mjs +4 -2
- package/lib/threadBriefs.mjs +9 -3
- package/lib/threadRuns.mjs +13 -0
- package/npm-shrinkwrap.json +14 -14
- package/package.json +4 -4
- package/release.json +48 -44
- package/lib/comfyExecution.mjs +0 -545
- package/lib/promptDecider.mjs +0 -471
- package/lib/promptFlow.mjs +0 -597
- package/lib/promptScopes.mjs +0 -670
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { harnessHome } from "./paths.mjs";
|
|
4
|
+
import { readJson, writeJsonAtomic } from "./files.mjs";
|
|
5
|
+
import { nameOf } from "./members.mjs";
|
|
6
|
+
import { PARENT, putSteer, receiptOf } from "./steerQueue.mjs";
|
|
7
|
+
|
|
8
|
+
// The person's window into the subagents an agent's session spawned, and their word to one of them: a note, a new
|
|
9
|
+
// brief, or a stop that asks it to return what it has. The session is a thread's run or one of the person's own
|
|
10
|
+
// sessions, on this computer or another of theirs (asked there, as session reads are). Claude Code's and OpenCode's
|
|
11
|
+
// subagents are told directly (steerQueue.mjs); Codex's through Codex, as its turn ends. The agent that started the
|
|
12
|
+
// subagent is always told too, since its own answer rests on that subagent's work. Only the person steers. A subagent
|
|
13
|
+
// asking for a permission is a needs-you item that names it.
|
|
14
|
+
|
|
15
|
+
const KINDS = new Set(["note", "redirect", "stop"]);
|
|
16
|
+
const TEXT_LIMIT = 4_000;
|
|
17
|
+
const ID = /^[A-Za-z0-9._:-]{1,128}$/;
|
|
18
|
+
// Agents whose subagents hear the person directly; any other agent with subagents hears it through itself.
|
|
19
|
+
const DIRECT = new Set(["claude-code", "opencode"]);
|
|
20
|
+
// How often a run on another computer has its subagents asked for again, while something reads it.
|
|
21
|
+
const REMOTE_EVERY_MS = 3_000;
|
|
22
|
+
|
|
23
|
+
const quoted = (task) => `"${String(task || "subagent").slice(0, 120)}"`;
|
|
24
|
+
const plainKey = (text) => String(text).replace(/[^A-Za-z0-9._-]/g, "_");
|
|
25
|
+
|
|
26
|
+
// What the agent that started the subagent reads about the person's word to it. Through: it is the one to pass it on.
|
|
27
|
+
function parentWords(kind, sub, text, { cameBack, through }) {
|
|
28
|
+
const which = `your subagent ${quoted(sub.task)}`;
|
|
29
|
+
if (cameBack)
|
|
30
|
+
return `${which[0].toUpperCase()}${which.slice(1)} has already returned. They ask for more from it:\n\n${text}\n\nSend it back to work, or do this yourself.`;
|
|
31
|
+
if (through) {
|
|
32
|
+
if (kind === "stop")
|
|
33
|
+
return `They want ${which} stopped. Stop it now and use what it has done so far.`;
|
|
34
|
+
if (kind === "redirect")
|
|
35
|
+
return `They want ${which} to work to this brief instead:\n\n${text}\n\nPass it on to it now.`;
|
|
36
|
+
return `They have a note for ${which}:\n\n${text}\n\nPass it on to it now.`;
|
|
37
|
+
}
|
|
38
|
+
if (kind === "stop")
|
|
39
|
+
return `They stopped ${which}. It will return what it has done so far; finish its part or leave it, as their other words say.`;
|
|
40
|
+
if (kind === "redirect")
|
|
41
|
+
return `They gave ${which} a new brief:\n\n${text}\n\nCount on its answer serving that brief, not the one you gave it.`;
|
|
42
|
+
return `They sent ${which} this note:\n\n${text}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function createSubagentSteering({
|
|
46
|
+
store,
|
|
47
|
+
subagents,
|
|
48
|
+
home = harnessHome,
|
|
49
|
+
log = () => {},
|
|
50
|
+
// This computer's id, and a call to another of the person's computers (ownMachines.mjs askSubagents).
|
|
51
|
+
machineId = () => null,
|
|
52
|
+
remote = async () => ({ ok: false, error: "that computer is not reachable" }),
|
|
53
|
+
// The board changed: what every surface reads (a subagent asking, or another computer's subagents arriving).
|
|
54
|
+
changed = () => {},
|
|
55
|
+
// A notice on this computer (notify.mjs).
|
|
56
|
+
desktop = () => {},
|
|
57
|
+
}) {
|
|
58
|
+
const logFile = (key) =>
|
|
59
|
+
path.join(home, "steer-log", `${plainKey(key)}.json`);
|
|
60
|
+
const sentFor = (key) => readJson(logFile(key), []) ?? [];
|
|
61
|
+
|
|
62
|
+
// A thread's run, or one of the person's own sessions: the agent, its session and where it runs.
|
|
63
|
+
function targetOf(request) {
|
|
64
|
+
if (request.threadId || request.runId) {
|
|
65
|
+
if (
|
|
66
|
+
!ID.test(String(request.threadId ?? "")) ||
|
|
67
|
+
!ID.test(String(request.runId ?? ""))
|
|
68
|
+
)
|
|
69
|
+
return null;
|
|
70
|
+
const run = store
|
|
71
|
+
.threadRuns(String(request.threadId))
|
|
72
|
+
.find((row) => row.id === request.runId);
|
|
73
|
+
if (!run?.session_id) return null;
|
|
74
|
+
return {
|
|
75
|
+
key: run.id,
|
|
76
|
+
agent: run.claimed_by || run.target_agent,
|
|
77
|
+
sessionId: run.session_id,
|
|
78
|
+
startedAt: run.created_at,
|
|
79
|
+
endedAt: run.reported_at,
|
|
80
|
+
machine: run.claimed_machine ?? null,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
const agent = String(request.to ?? "");
|
|
84
|
+
const sessionId = String(request.sessionId ?? "");
|
|
85
|
+
if (!ID.test(agent) || !ID.test(sessionId)) return null;
|
|
86
|
+
return {
|
|
87
|
+
key: `${agent}-${sessionId}`,
|
|
88
|
+
agent,
|
|
89
|
+
sessionId,
|
|
90
|
+
startedAt: null,
|
|
91
|
+
endedAt: null,
|
|
92
|
+
machine: request.machine ? String(request.machine) : null,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
const elsewhere = (target) =>
|
|
96
|
+
Boolean(target.machine && machineId() && target.machine !== machineId());
|
|
97
|
+
|
|
98
|
+
// Each steer as the window shows it: waiting, or landed (when, and before which tool, or as it ended).
|
|
99
|
+
const withStatus = (entry) => {
|
|
100
|
+
const landed = receiptOf(entry.id, home);
|
|
101
|
+
const told = entry.parentId ? receiptOf(entry.parentId, home) : null;
|
|
102
|
+
return {
|
|
103
|
+
id: entry.id,
|
|
104
|
+
subagentId: entry.subagentId,
|
|
105
|
+
kind: entry.kind,
|
|
106
|
+
text: entry.text,
|
|
107
|
+
at: entry.at,
|
|
108
|
+
landed: landed
|
|
109
|
+
? {
|
|
110
|
+
at: landed.at,
|
|
111
|
+
tool: landed.tool ?? null,
|
|
112
|
+
event: landed.event ?? null,
|
|
113
|
+
}
|
|
114
|
+
: null,
|
|
115
|
+
parentTold: told
|
|
116
|
+
? { at: told.at }
|
|
117
|
+
: entry.through && landed
|
|
118
|
+
? { at: landed.at }
|
|
119
|
+
: null,
|
|
120
|
+
cameBack: entry.cameBack === true,
|
|
121
|
+
through: entry.through === true,
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
function read(target, request) {
|
|
126
|
+
const subagent = subagents.detail(target, String(request.subagentId ?? ""));
|
|
127
|
+
if (!subagent) return { ok: false, error: "subagent not found" };
|
|
128
|
+
return {
|
|
129
|
+
ok: true,
|
|
130
|
+
agent: target.agent,
|
|
131
|
+
direct: DIRECT.has(target.agent),
|
|
132
|
+
subagent: {
|
|
133
|
+
...subagent,
|
|
134
|
+
asking: askOf(target.sessionId, subagent.id)?.words ?? null,
|
|
135
|
+
},
|
|
136
|
+
sent: sentFor(target.key)
|
|
137
|
+
.filter((entry) => entry.subagentId === subagent.id)
|
|
138
|
+
.map(withStatus),
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function steer(target, request) {
|
|
143
|
+
const kind = String(request.kind ?? "");
|
|
144
|
+
if (!KINDS.has(kind))
|
|
145
|
+
return { ok: false, error: "a steer is a note, a redirect or a stop" };
|
|
146
|
+
const text = String(request.text ?? "")
|
|
147
|
+
.trim()
|
|
148
|
+
.slice(0, TEXT_LIMIT);
|
|
149
|
+
if (kind !== "stop" && !text)
|
|
150
|
+
return { ok: false, error: "say what to tell it" };
|
|
151
|
+
const sub = subagents
|
|
152
|
+
.of(target)
|
|
153
|
+
.find((found) => found.id === String(request.subagentId ?? ""));
|
|
154
|
+
if (!sub) return { ok: false, error: "subagent not found" };
|
|
155
|
+
const cameBack = !sub.live;
|
|
156
|
+
// A subagent that already returned can only be asked for more, through the agent that started it.
|
|
157
|
+
if (cameBack && kind !== "note")
|
|
158
|
+
return { ok: false, error: "it already came back" };
|
|
159
|
+
const direct = DIRECT.has(target.agent);
|
|
160
|
+
const opencode = target.agent === "opencode";
|
|
161
|
+
const parentSession = String((opencode && sub.parent) || target.sessionId);
|
|
162
|
+
const about = { key: target.key, subagentId: sub.id };
|
|
163
|
+
const sent =
|
|
164
|
+
cameBack || !direct
|
|
165
|
+
? null
|
|
166
|
+
: putSteer(
|
|
167
|
+
opencode
|
|
168
|
+
? { session: sub.id, agent: PARENT, kind, text, about }
|
|
169
|
+
: { session: target.sessionId, agent: sub.id, kind, text, about },
|
|
170
|
+
home
|
|
171
|
+
);
|
|
172
|
+
const told = putSteer(
|
|
173
|
+
{
|
|
174
|
+
session: parentSession,
|
|
175
|
+
agent: PARENT,
|
|
176
|
+
kind: "parent",
|
|
177
|
+
text: parentWords(kind, sub, text, { cameBack, through: !direct }),
|
|
178
|
+
about,
|
|
179
|
+
},
|
|
180
|
+
home
|
|
181
|
+
);
|
|
182
|
+
const entry = {
|
|
183
|
+
id: sent?.id ?? told.id,
|
|
184
|
+
parentId: sent ? told.id : null,
|
|
185
|
+
subagentId: sub.id,
|
|
186
|
+
kind,
|
|
187
|
+
text,
|
|
188
|
+
at: (sent ?? told).at,
|
|
189
|
+
cameBack,
|
|
190
|
+
through: !direct && !cameBack,
|
|
191
|
+
};
|
|
192
|
+
const file = logFile(target.key);
|
|
193
|
+
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
194
|
+
writeJsonAtomic(file, [...sentFor(target.key), entry]);
|
|
195
|
+
log(
|
|
196
|
+
`steer: ${kind} for ${target.agent} subagent ${sub.id} (${target.key})`
|
|
197
|
+
);
|
|
198
|
+
return { ok: true, sent: withStatus(entry) };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** On the computer the session runs on: one read or steer for a target named in full. */
|
|
202
|
+
function local({ target, request }) {
|
|
203
|
+
if (
|
|
204
|
+
!target ||
|
|
205
|
+
!ID.test(String(target.agent)) ||
|
|
206
|
+
!ID.test(String(target.sessionId))
|
|
207
|
+
)
|
|
208
|
+
return { ok: false, error: "subagent not found" };
|
|
209
|
+
if (request.command === "board.subagent.read") return read(target, request);
|
|
210
|
+
if (request.command === "board.subagent.steer")
|
|
211
|
+
return steer(target, request);
|
|
212
|
+
if (request.command === "board.subagent.list")
|
|
213
|
+
return { ok: true, subagents: subagents.of(target) };
|
|
214
|
+
return { ok: false, error: `unknown command ${request.command}` };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// ── A subagent asking for a permission ────────────────────────────────────────
|
|
218
|
+
// Said by Claude Code's PermissionRequest hook as the dialog opens (steerHook.mjs --ask); answered in the agent's own
|
|
219
|
+
// dialog, so it goes as soon as the subagent's transcript moves on.
|
|
220
|
+
const asks = new Map();
|
|
221
|
+
const askKey = (session, agentId) => `${session}:${agentId}`;
|
|
222
|
+
const mtime = (file) => {
|
|
223
|
+
try {
|
|
224
|
+
return fs.statSync(file).mtimeMs;
|
|
225
|
+
} catch {
|
|
226
|
+
return 0;
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
function askOf(session, agentId) {
|
|
230
|
+
const ask = asks.get(askKey(session, agentId));
|
|
231
|
+
if (!ask) return null;
|
|
232
|
+
if (mtime(ask.transcript) > ask.atMs + 1_000) {
|
|
233
|
+
asks.delete(askKey(session, agentId));
|
|
234
|
+
changed();
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
return ask;
|
|
238
|
+
}
|
|
239
|
+
function asking(request, agent) {
|
|
240
|
+
const session = String(request.sessionId ?? "");
|
|
241
|
+
const agentId = String(request.agentId ?? "");
|
|
242
|
+
if (!ID.test(session) || !ID.test(agentId))
|
|
243
|
+
return { ok: false, error: "no subagent named" };
|
|
244
|
+
const target = {
|
|
245
|
+
agent,
|
|
246
|
+
sessionId: session,
|
|
247
|
+
startedAt: null,
|
|
248
|
+
endedAt: null,
|
|
249
|
+
};
|
|
250
|
+
const sub = subagents.of(target).find((found) => found.id === agentId);
|
|
251
|
+
const tool = String(request.tool ?? "a tool").slice(0, 60);
|
|
252
|
+
const what = String(request.what ?? "")
|
|
253
|
+
.replace(/\s+/g, " ")
|
|
254
|
+
.slice(0, 160);
|
|
255
|
+
const words = `${nameOf(agent)}'s subagent ${quoted(sub?.task || sub?.type)} wants to use ${tool}${what ? `: ${what}` : ""}`;
|
|
256
|
+
const run = store.runOfSession?.(agent, session) ?? null;
|
|
257
|
+
asks.set(askKey(session, agentId), {
|
|
258
|
+
id: `subagent:${plainKey(session)}:${plainKey(agentId)}`,
|
|
259
|
+
agent,
|
|
260
|
+
session,
|
|
261
|
+
agentId,
|
|
262
|
+
words,
|
|
263
|
+
threadId: run?.parent_id ?? null,
|
|
264
|
+
runId: run?.id ?? null,
|
|
265
|
+
transcript: String(request.transcript ?? ""),
|
|
266
|
+
atMs: Date.now(),
|
|
267
|
+
at: new Date().toISOString(),
|
|
268
|
+
});
|
|
269
|
+
desktop("Your swarm", words);
|
|
270
|
+
changed();
|
|
271
|
+
return { ok: true };
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// ── Another computer's runs, read there ───────────────────────────────────────
|
|
275
|
+
const remoteSeen = new Map();
|
|
276
|
+
function remoteOf(target) {
|
|
277
|
+
const seen = remoteSeen.get(target.key);
|
|
278
|
+
if (!seen || Date.now() - seen.at > REMOTE_EVERY_MS) {
|
|
279
|
+
remoteSeen.set(target.key, {
|
|
280
|
+
at: Date.now(),
|
|
281
|
+
subagents: seen?.subagents ?? [],
|
|
282
|
+
});
|
|
283
|
+
void remote(target.machine, {
|
|
284
|
+
target,
|
|
285
|
+
request: { command: "board.subagent.list" },
|
|
286
|
+
})
|
|
287
|
+
.then((answer) => {
|
|
288
|
+
if (!answer?.ok) return;
|
|
289
|
+
const before = JSON.stringify(
|
|
290
|
+
remoteSeen.get(target.key)?.subagents ?? []
|
|
291
|
+
);
|
|
292
|
+
remoteSeen.set(target.key, {
|
|
293
|
+
at: Date.now(),
|
|
294
|
+
subagents: answer.subagents,
|
|
295
|
+
});
|
|
296
|
+
if (JSON.stringify(answer.subagents) !== before) changed();
|
|
297
|
+
})
|
|
298
|
+
.catch(() => {});
|
|
299
|
+
}
|
|
300
|
+
return remoteSeen.get(target.key)?.subagents ?? [];
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const withAsks = (target, list) =>
|
|
304
|
+
list.map((sub) => {
|
|
305
|
+
const ask = sub.live ? askOf(target.sessionId, sub.id) : null;
|
|
306
|
+
return ask ? { ...sub, asking: ask.words } : sub;
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
return {
|
|
310
|
+
/** board.subagent.read, .steer and .list (the person's), and .asking (the agent's own hook). */
|
|
311
|
+
command(request, agent) {
|
|
312
|
+
if (request.command === "board.subagent.asking")
|
|
313
|
+
return asking(request, agent);
|
|
314
|
+
if (agent !== "user")
|
|
315
|
+
return { ok: false, error: "only the person steers a subagent" };
|
|
316
|
+
const target = targetOf(request);
|
|
317
|
+
if (!target) return { ok: false, error: "that session is not found" };
|
|
318
|
+
if (elsewhere(target))
|
|
319
|
+
return {
|
|
320
|
+
pending: remote(target.machine, {
|
|
321
|
+
target,
|
|
322
|
+
request: { ...request, agent: "user" },
|
|
323
|
+
}),
|
|
324
|
+
};
|
|
325
|
+
return local({ target, request });
|
|
326
|
+
},
|
|
327
|
+
local,
|
|
328
|
+
/** A run's subagents for the thread's read: read here, or as its computer last said. */
|
|
329
|
+
of(run) {
|
|
330
|
+
const target = { ...run, key: run.id };
|
|
331
|
+
return elsewhere(target)
|
|
332
|
+
? remoteOf(target)
|
|
333
|
+
: withAsks(target, subagents.of(target));
|
|
334
|
+
},
|
|
335
|
+
/** One of the person's sessions on this computer, for the list of what runs. */
|
|
336
|
+
ofSession(session) {
|
|
337
|
+
if (!session?.agent || !session.sessionId) return [];
|
|
338
|
+
const target = {
|
|
339
|
+
agent: session.agent,
|
|
340
|
+
sessionId: session.sessionId,
|
|
341
|
+
startedAt: null,
|
|
342
|
+
endedAt: null,
|
|
343
|
+
};
|
|
344
|
+
return withAsks(target, subagents.of(target));
|
|
345
|
+
},
|
|
346
|
+
/** Subagents asking for a permission now, as needs-you items. */
|
|
347
|
+
needs() {
|
|
348
|
+
return [...asks.values()]
|
|
349
|
+
.filter((ask) => askOf(ask.session, ask.agentId))
|
|
350
|
+
.map((ask) => ({
|
|
351
|
+
id: ask.id,
|
|
352
|
+
kind: "subagent",
|
|
353
|
+
agent: ask.agent,
|
|
354
|
+
text: ask.words,
|
|
355
|
+
sessionId: ask.session,
|
|
356
|
+
subagentId: ask.agentId,
|
|
357
|
+
runId: ask.runId,
|
|
358
|
+
threadId: ask.threadId,
|
|
359
|
+
at: ask.at,
|
|
360
|
+
}));
|
|
361
|
+
},
|
|
362
|
+
};
|
|
363
|
+
}
|
package/lib/subagents.mjs
CHANGED
|
@@ -43,6 +43,49 @@ const lastSteps = (items) =>
|
|
|
43
43
|
)
|
|
44
44
|
.slice(-STEPS)
|
|
45
45
|
.map((item) => ({ kind: item.kind, text: clip(item.text, STEP_TEXT) }));
|
|
46
|
+
// What a subagent's window shows: its own words and tool calls, the latest kept.
|
|
47
|
+
const TRAIL = 80;
|
|
48
|
+
const TRAIL_TEXT = 600;
|
|
49
|
+
const BRIEF_TEXT = 4_000;
|
|
50
|
+
const trailOf = (items) =>
|
|
51
|
+
items
|
|
52
|
+
.filter(
|
|
53
|
+
(item) =>
|
|
54
|
+
(item.kind === "say" || item.kind === "do") && !sealed(item.text)
|
|
55
|
+
)
|
|
56
|
+
.slice(-TRAIL)
|
|
57
|
+
.map((item) => ({
|
|
58
|
+
kind: item.kind,
|
|
59
|
+
text: clip(item.text, TRAIL_TEXT),
|
|
60
|
+
at: item.at ?? null,
|
|
61
|
+
}));
|
|
62
|
+
// The files a subagent wrote, read off its own tool calls as stepsOf names them ("Edit: /the/file"): Claude Code's
|
|
63
|
+
// file tools and OpenCode's. A fact about what it did, never a guess at what it meant.
|
|
64
|
+
const WRITES = /^(?:Edit|Write|MultiEdit|edit|write|multiedit):\s+([^{\s].*)$/;
|
|
65
|
+
const wroteIn = (items) => [
|
|
66
|
+
...new Set(
|
|
67
|
+
items
|
|
68
|
+
.filter((item) => item.kind === "do")
|
|
69
|
+
.map((item) => WRITES.exec(item.text)?.[1]?.trim())
|
|
70
|
+
.filter(Boolean)
|
|
71
|
+
),
|
|
72
|
+
];
|
|
73
|
+
// Two subagents working at once that both wrote one file: each names the other and the files. Subagents in worktrees
|
|
74
|
+
// of their own write other paths, so they never meet here.
|
|
75
|
+
function withSameFile(subs) {
|
|
76
|
+
const working = subs.filter((sub) => sub.live && sub.wrote?.length);
|
|
77
|
+
return subs.map((sub) => {
|
|
78
|
+
if (!sub.live || !sub.wrote?.length) return sub;
|
|
79
|
+
const same = working
|
|
80
|
+
.filter((other) => other !== sub)
|
|
81
|
+
.map((other) => ({
|
|
82
|
+
id: other.id,
|
|
83
|
+
files: other.wrote.filter((file) => sub.wrote.includes(file)),
|
|
84
|
+
}))
|
|
85
|
+
.filter((other) => other.files.length);
|
|
86
|
+
return same.length ? { ...sub, sameFile: same } : sub;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
46
89
|
|
|
47
90
|
function head(file, bytes = HEAD_BYTES) {
|
|
48
91
|
let fd;
|
|
@@ -88,6 +131,19 @@ function opencodeRows(sql) {
|
|
|
88
131
|
}
|
|
89
132
|
}
|
|
90
133
|
const quote = (value) => `'${String(value).replace(/'/g, "''")}'`;
|
|
134
|
+
// OpenCode's parts, newest first as queried, as steps oldest first.
|
|
135
|
+
const opencodeSteps = (parts) =>
|
|
136
|
+
[...parts].reverse().flatMap((part) => {
|
|
137
|
+
try {
|
|
138
|
+
const data = JSON.parse(part.data);
|
|
139
|
+
return stepsOf({ type: data.type, part: data }).map((step) => ({
|
|
140
|
+
...step,
|
|
141
|
+
at: data.time?.start ? new Date(data.time.start).toISOString() : null,
|
|
142
|
+
}));
|
|
143
|
+
} catch {
|
|
144
|
+
return [];
|
|
145
|
+
}
|
|
146
|
+
});
|
|
91
147
|
|
|
92
148
|
/**
|
|
93
149
|
* The subagents of one run: `{ id, parent, type, task, startedAt, endedAt, live, depth, steps }`, oldest first.
|
|
@@ -152,6 +208,7 @@ export function createSubagents({
|
|
|
152
208
|
readTurn("claude-code", file).open &&
|
|
153
209
|
now() - (mtime ?? 0) < QUIET_MS;
|
|
154
210
|
const prompt = first?.message?.content;
|
|
211
|
+
const items = sessionFeed("claude-code", file, 200);
|
|
155
212
|
return settled(file, {
|
|
156
213
|
id: name.replace(/^agent-|\.jsonl$/g, ""),
|
|
157
214
|
parent: null,
|
|
@@ -165,7 +222,8 @@ export function createSubagents({
|
|
|
165
222
|
endedAt: open ? null : iso(mtime),
|
|
166
223
|
live: open,
|
|
167
224
|
depth: Number(meta.spawnDepth) || 1,
|
|
168
|
-
steps: scrub(lastSteps(
|
|
225
|
+
steps: scrub(lastSteps(items)),
|
|
226
|
+
wrote: scrub(wroteIn(items)),
|
|
169
227
|
});
|
|
170
228
|
});
|
|
171
229
|
}
|
|
@@ -217,7 +275,9 @@ export function createSubagents({
|
|
|
217
275
|
return found;
|
|
218
276
|
}
|
|
219
277
|
function codex(sessionId, startedAt, endedAt) {
|
|
220
|
-
|
|
278
|
+
// A session read for itself (no run) looks back a day for its children.
|
|
279
|
+
const from =
|
|
280
|
+
stamp(startedAt) != null ? stamp(startedAt) - 60_000 : now() - 86_400_000;
|
|
221
281
|
const to = stamp(endedAt) ?? now();
|
|
222
282
|
const children = codexFiles(from, to)
|
|
223
283
|
.filter((file) => (mtimeOf(file) ?? 0) >= from)
|
|
@@ -257,6 +317,7 @@ export function createSubagents({
|
|
|
257
317
|
live: open,
|
|
258
318
|
depth: meta.depth,
|
|
259
319
|
steps: scrub(lastSteps(sessionFeed("codex", file, 40))),
|
|
320
|
+
wrote: [],
|
|
260
321
|
});
|
|
261
322
|
});
|
|
262
323
|
}
|
|
@@ -281,16 +342,9 @@ export function createSubagents({
|
|
|
281
342
|
!(last?.role === "assistant" && last?.time?.completed) &&
|
|
282
343
|
now() - Number(row.time_updated) < QUIET_MS;
|
|
283
344
|
const parts = opencodeRows(
|
|
284
|
-
`select data from part where session_id = ${quote(row.id)} order by time_created desc limit
|
|
345
|
+
`select data from part where session_id = ${quote(row.id)} order by time_created desc limit 200`
|
|
285
346
|
);
|
|
286
|
-
const steps = parts
|
|
287
|
-
try {
|
|
288
|
-
const data = JSON.parse(part.data);
|
|
289
|
-
return stepsOf({ type: data.type, part: data });
|
|
290
|
-
} catch {
|
|
291
|
-
return [];
|
|
292
|
-
}
|
|
293
|
-
});
|
|
347
|
+
const steps = opencodeSteps(parts);
|
|
294
348
|
return settled(row.id, {
|
|
295
349
|
id: row.id,
|
|
296
350
|
parent: row.parent_id === sessionId ? null : row.parent_id,
|
|
@@ -304,33 +358,88 @@ export function createSubagents({
|
|
|
304
358
|
live: open,
|
|
305
359
|
depth: Number(row.depth) || 1,
|
|
306
360
|
steps: scrub(lastSteps(steps)),
|
|
361
|
+
wrote: scrub(wroteIn(steps)),
|
|
307
362
|
});
|
|
308
363
|
});
|
|
309
364
|
}
|
|
310
365
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
366
|
+
/** The subagents a run's session spawned while the run went on, oldest first. */
|
|
367
|
+
function of({ agent, sessionId, startedAt, endedAt }) {
|
|
368
|
+
if (!sessionId || !PLAIN_ID.test(String(sessionId))) return [];
|
|
369
|
+
const id = String(sessionId);
|
|
370
|
+
const found =
|
|
371
|
+
agent === "claude-code"
|
|
372
|
+
? claude(id)
|
|
373
|
+
: agent === "codex"
|
|
374
|
+
? codex(id, startedAt, endedAt)
|
|
375
|
+
: agent === "opencode"
|
|
376
|
+
? opencode(id)
|
|
377
|
+
: [];
|
|
378
|
+
// A resumed session keeps the subagents of its earlier runs: only the ones started during this run.
|
|
379
|
+
const from = (stamp(startedAt) ?? 0) - 60_000;
|
|
380
|
+
const to = stamp(endedAt) ?? Infinity;
|
|
381
|
+
return withSameFile(
|
|
382
|
+
found
|
|
328
383
|
.filter((sub) => {
|
|
329
384
|
const at = stamp(sub.startedAt) ?? 0;
|
|
330
385
|
return at >= from && at <= to;
|
|
331
386
|
})
|
|
332
387
|
.sort((a, b) => (stamp(a.startedAt) ?? 0) - (stamp(b.startedAt) ?? 0))
|
|
333
|
-
.slice(-MAX)
|
|
388
|
+
.slice(-MAX)
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// The words a subagent was started with, whole: Claude Code's first line of its transcript, OpenCode's first text.
|
|
393
|
+
function briefOf(agent, sessionId, id) {
|
|
394
|
+
if (agent === "claude-code") {
|
|
395
|
+
const folder = claudeFolder(sessionId);
|
|
396
|
+
if (!folder) return { brief: null, items: [] };
|
|
397
|
+
const file = path.join(folder, `agent-${id}.jsonl`);
|
|
398
|
+
let first = null;
|
|
399
|
+
try {
|
|
400
|
+
first = JSON.parse(head(file, 1 << 20).split("\n")[0]);
|
|
401
|
+
} catch {}
|
|
402
|
+
const prompt = first?.message?.content;
|
|
403
|
+
return {
|
|
404
|
+
brief:
|
|
405
|
+
typeof prompt === "string" ? prompt : (prompt?.[0]?.text ?? null),
|
|
406
|
+
items: sessionFeed("claude-code", file, 400),
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
if (agent === "opencode") {
|
|
410
|
+
const parts = opencodeRows(
|
|
411
|
+
`select data from part where session_id = ${quote(id)} order by time_created desc limit 300`
|
|
412
|
+
);
|
|
413
|
+
const said = opencodeRows(
|
|
414
|
+
`select data from part where session_id = ${quote(id)} order by time_created limit 1`
|
|
415
|
+
);
|
|
416
|
+
let brief = null;
|
|
417
|
+
try {
|
|
418
|
+
brief = JSON.parse(said[0]?.data ?? "null")?.text ?? null;
|
|
419
|
+
} catch {}
|
|
420
|
+
return { brief, items: opencodeSteps(parts) };
|
|
421
|
+
}
|
|
422
|
+
return { brief: null, items: [] };
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return {
|
|
426
|
+
of,
|
|
427
|
+
/**
|
|
428
|
+
* One subagent of a run, for its window: everything `of` gives, its brief whole and its trail (say and do, the
|
|
429
|
+
* latest TRAIL). Null when the run has no such subagent.
|
|
430
|
+
*/
|
|
431
|
+
detail(run, id) {
|
|
432
|
+
if (!PLAIN_ID.test(String(id ?? ""))) return null;
|
|
433
|
+
const sub = of(run).find((found) => found.id === id);
|
|
434
|
+
if (!sub) return null;
|
|
435
|
+
const { brief, items } = briefOf(run.agent, String(run.sessionId), id);
|
|
436
|
+
return {
|
|
437
|
+
...sub,
|
|
438
|
+
brief: scrub(
|
|
439
|
+
brief ? String(brief).trim().slice(0, BRIEF_TEXT) : sub.task
|
|
440
|
+
),
|
|
441
|
+
trail: scrub(trailOf(items)),
|
|
442
|
+
};
|
|
334
443
|
},
|
|
335
444
|
};
|
|
336
445
|
}
|
package/lib/swarm.mjs
CHANGED
|
@@ -206,6 +206,8 @@ export function createSwarm({
|
|
|
206
206
|
subagents = createSubagents(),
|
|
207
207
|
// Work wired to wait for other work in the thread (plans.mjs).
|
|
208
208
|
plansOf = () => [],
|
|
209
|
+
// The thread's board of cards (kanban.mjs).
|
|
210
|
+
boardOf = () => [],
|
|
209
211
|
// The thread as the person reads it on every surface (threadView.mjs).
|
|
210
212
|
threadView = null,
|
|
211
213
|
// The project a thread is the room of, shared with another person's swarm (projects.mjs).
|
|
@@ -286,12 +288,17 @@ export function createSwarm({
|
|
|
286
288
|
? agents
|
|
287
289
|
: [thread.goal.facilitator];
|
|
288
290
|
};
|
|
289
|
-
// The person's post is for the swarm when it says `@swarm`, or when it falls to the coordinator:
|
|
290
|
-
// brings in every agent with a part in it at once (coordinator.mjs heard). A message
|
|
291
|
+
// The person's post is for the swarm when it says `@swarm`, or when it names no agent and falls to the coordinator:
|
|
292
|
+
// then the coordinator brings in every agent with a part in it at once (coordinator.mjs heard). A message that names
|
|
293
|
+
// agents is theirs, `@harness` too: Harness is an agent like the others, whatever coordinates the thread.
|
|
291
294
|
const heardBy = (thread, post, text) => {
|
|
292
295
|
if (!post?.seq || post.kind !== "post") return;
|
|
293
296
|
const swarm = SWARM.test(text);
|
|
294
|
-
|
|
297
|
+
const named = mentionsIn(text, thread.goal.team).length > 0;
|
|
298
|
+
if (
|
|
299
|
+
!swarm &&
|
|
300
|
+
(named || addressed(thread, text)[0] !== thread.goal.facilitator)
|
|
301
|
+
)
|
|
295
302
|
return;
|
|
296
303
|
coordinator.heard(thread.id, {
|
|
297
304
|
seq: post.seq,
|
|
@@ -419,15 +426,15 @@ export function createSwarm({
|
|
|
419
426
|
pick: coordinator.pick(),
|
|
420
427
|
choices: coordinator.choices(),
|
|
421
428
|
rounds: coordinator.state(thread.id).rounds,
|
|
422
|
-
// Who decided this thread's rounds:
|
|
429
|
+
// Who decided this thread's rounds: Jev on the server, a backup here after Jev could not, this computer, no one.
|
|
423
430
|
decided: store.roundHealth(thread.id, 3650).reduce(
|
|
424
431
|
(sum, day) => ({
|
|
425
|
-
|
|
432
|
+
jev: sum.jev + day.jev,
|
|
426
433
|
backup: sum.backup + day.backup,
|
|
427
434
|
local: sum.local + day.local,
|
|
428
435
|
undecided: sum.undecided + day.undecided,
|
|
429
436
|
}),
|
|
430
|
-
{
|
|
437
|
+
{ jev: 0, backup: 0, local: 0, undecided: 0 }
|
|
431
438
|
),
|
|
432
439
|
},
|
|
433
440
|
// The goal as the swarm keeps it (goals.mjs): what the person wants, its checks, where it stands, every version.
|
|
@@ -549,10 +556,12 @@ export function createSwarm({
|
|
|
549
556
|
subagents: hostedCaller
|
|
550
557
|
? []
|
|
551
558
|
: subagents.of({
|
|
559
|
+
id: run.id,
|
|
552
560
|
agent: run.claimed_by || run.target_agent,
|
|
553
561
|
sessionId: run.session_id,
|
|
554
562
|
startedAt: run.created_at,
|
|
555
563
|
endedAt: run.reported_at,
|
|
564
|
+
machine: run.claimed_machine ?? null,
|
|
556
565
|
}),
|
|
557
566
|
...adoption?.decorate(thread.id, run.id),
|
|
558
567
|
}));
|
|
@@ -569,6 +578,7 @@ export function createSwarm({
|
|
|
569
578
|
kept: store.lessonsFromThread?.(thread.id) ?? [],
|
|
570
579
|
runs,
|
|
571
580
|
plans: hostedCaller ? [] : plansOf(thread.id),
|
|
581
|
+
board: boardOf(thread.id),
|
|
572
582
|
// Posts held for the person (who wrote one, when, never its words), and the post that replaced each one an agent
|
|
573
583
|
// posted again.
|
|
574
584
|
held: store.heldPosts(thread.id).map((post) => ({
|