tryharness 0.7.36 → 0.7.38
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/activity.mjs +1 -1
- package/lib/boardFiles.mjs +49 -0
- package/lib/coordinator.mjs +6 -4
- package/lib/daemon.mjs +3 -2
- package/lib/goalCommands.mjs +21 -3
- package/lib/members.json +2 -2
- package/lib/members.mjs +3 -0
- package/lib/store.mjs +1 -1
- package/lib/subagents.mjs +332 -0
- package/lib/swarm.mjs +32 -0
- package/lib/threadRuns.mjs +4 -1
- package/package.json +1 -1
package/lib/activity.mjs
CHANGED
|
@@ -181,7 +181,7 @@ function tailRows(file, bytes) {
|
|
|
181
181
|
}
|
|
182
182
|
|
|
183
183
|
/** Whether a transcript's last turn is open, read from its tail, or from further back when the tail cannot tell. */
|
|
184
|
-
function readTurn(agent, file) {
|
|
184
|
+
export function readTurn(agent, file) {
|
|
185
185
|
const near = tailRows(file, TAIL_BYTES);
|
|
186
186
|
const turn = turnOf(agent, near.rows);
|
|
187
187
|
if (!turn.unknown || near.whole) return turn;
|
package/lib/boardFiles.mjs
CHANGED
|
@@ -45,6 +45,8 @@ export const MAX_FILE_BYTES = Math.max(
|
|
|
45
45
|
);
|
|
46
46
|
// One part of a file read through the sealed channel: 512 KiB is 683 KiB as base64, inside a sealed answer's 1 MB.
|
|
47
47
|
const FILE_PART_BYTES = 512 * 1024;
|
|
48
|
+
// The most files one search across the person's threads answers with.
|
|
49
|
+
const FOUND_FILES = 20;
|
|
48
50
|
const fileLimit = (mime, bytes) =>
|
|
49
51
|
FILE_LIMITS.find((limit) =>
|
|
50
52
|
limit.test(mime, bytes.subarray(0, 5).toString("latin1"))
|
|
@@ -339,6 +341,16 @@ export function createBoardFiles({
|
|
|
339
341
|
laterAfter: page.laterAfter,
|
|
340
342
|
};
|
|
341
343
|
}
|
|
344
|
+
// Who attached a file, and in which thread, is a record, so Harness reads it instead of guessing from how a post
|
|
345
|
+
// was worded (a file Codex made in another thread was taken for Harness's own and left for the window, which could
|
|
346
|
+
// not find it either). Records only, scrubbed as posts are: a file is read in its own thread.
|
|
347
|
+
if (input.op === "find") {
|
|
348
|
+
const found = findFiles(String(input.query || ""), place.id);
|
|
349
|
+
return {
|
|
350
|
+
ok: true,
|
|
351
|
+
files: scrubber ? await scrubDeep(scrubber, found, new Map()) : found,
|
|
352
|
+
};
|
|
353
|
+
}
|
|
342
354
|
const files = placeFiles(place);
|
|
343
355
|
if (input.op === "list")
|
|
344
356
|
return {
|
|
@@ -534,6 +546,43 @@ export function createBoardFiles({
|
|
|
534
546
|
data: bytes.subarray(offset, offset + FILE_PART_BYTES).toString("base64"),
|
|
535
547
|
};
|
|
536
548
|
};
|
|
549
|
+
// The person's files in every thread that match the words asked, best first: a word in the file's name counts
|
|
550
|
+
// double a word in its type or its thread's title. Each with where it is, who attached it and when.
|
|
551
|
+
const searchWords = (text) =>
|
|
552
|
+
String(text)
|
|
553
|
+
.toLowerCase()
|
|
554
|
+
.split(/[^\p{L}\p{N}]+/u)
|
|
555
|
+
.filter((word) => word.length > 1);
|
|
556
|
+
const findFiles = (query, hereId) => {
|
|
557
|
+
const asked = [...new Set(searchWords(query))];
|
|
558
|
+
if (!asked.length) return [];
|
|
559
|
+
const matches = store.presentFiles().flatMap((file) => {
|
|
560
|
+
const task = store.getTask(file.task_id);
|
|
561
|
+
const thread = task && (roomOf(task) ?? task);
|
|
562
|
+
if (!thread || task.removed_at || thread.removed_at) return [];
|
|
563
|
+
const named = searchWords(file.name).join(" ");
|
|
564
|
+
const about = searchWords(`${file.mime} ${thread.objective}`).join(" ");
|
|
565
|
+
const score = asked.reduce(
|
|
566
|
+
(sum, word) =>
|
|
567
|
+
sum + (named.includes(word) ? 2 : about.includes(word) ? 1 : 0),
|
|
568
|
+
file.id === query.trim() ? 100 : 0
|
|
569
|
+
);
|
|
570
|
+
return score ? [{ score, file, thread }] : [];
|
|
571
|
+
});
|
|
572
|
+
return matches
|
|
573
|
+
.sort(
|
|
574
|
+
(a, b) =>
|
|
575
|
+
b.score - a.score ||
|
|
576
|
+
b.file.created_at.localeCompare(a.file.created_at)
|
|
577
|
+
)
|
|
578
|
+
.slice(0, FOUND_FILES)
|
|
579
|
+
.map(({ file, thread }) => ({
|
|
580
|
+
...harnessFileView(file),
|
|
581
|
+
attached_at: file.created_at,
|
|
582
|
+
thread: { id: thread.id, title: thread.objective.slice(0, 200) },
|
|
583
|
+
in_this_thread: thread.id === hereId,
|
|
584
|
+
}));
|
|
585
|
+
};
|
|
537
586
|
const harnessFilesOf = (taskId) => {
|
|
538
587
|
try {
|
|
539
588
|
return JSON.parse(store.meta(`harness-files:${taskId}`) || "[]");
|
package/lib/coordinator.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import os from "node:os";
|
|
2
2
|
import { spawn } from "node:child_process";
|
|
3
|
-
import { MACHINE_RUNNERS, MEMBERS, nameOf } from "./members.mjs";
|
|
3
|
+
import { MACHINE_RUNNERS, MEMBERS, TAGGED_ONLY, nameOf } from "./members.mjs";
|
|
4
4
|
import { LEAD_TIMES } from "./schedules.mjs";
|
|
5
5
|
|
|
6
6
|
// The swarm's coordinator, run from this machine whether or not a Harness window is open. When a thread goes quiet
|
|
@@ -25,7 +25,7 @@ const REVIEWERS = new Set(
|
|
|
25
25
|
MEMBERS.filter((entry) => entry.review).map((entry) => entry.id)
|
|
26
26
|
);
|
|
27
27
|
// Agents that move money or take positions are never brought in on a guess: only once they are in the thread.
|
|
28
|
-
const
|
|
28
|
+
const TAGGED = new Set(TAGGED_ONLY);
|
|
29
29
|
const TRANSCRIPT_POSTS = 30;
|
|
30
30
|
// The goal's own updates go to the round as the goal itself (goals.mjs), not as posts. (A goal room's opening post,
|
|
31
31
|
// kind goal, is the person's request and stays.)
|
|
@@ -283,7 +283,9 @@ export function createCoordinator({
|
|
|
283
283
|
!asked?.named.includes(candidate.id) &&
|
|
284
284
|
// In every round, not only an assembly (owner, October 16: "yah", to Bankr scoring 0.33 to 0.38 as the agent
|
|
285
285
|
// to hand a production keeper fix to in the eval swarm-coordination).
|
|
286
|
-
!(
|
|
286
|
+
!(
|
|
287
|
+
TAGGED.has(candidate.id) && !thread.goal.team.includes(candidate.id)
|
|
288
|
+
)
|
|
287
289
|
)
|
|
288
290
|
.map((candidate) => ({
|
|
289
291
|
...candidate,
|
|
@@ -403,7 +405,7 @@ export function createCoordinator({
|
|
|
403
405
|
agent !== "harness" &&
|
|
404
406
|
!asked.named.includes(agent) &&
|
|
405
407
|
(!CODING.has(agent) || thread.goal.workspace) &&
|
|
406
|
-
(!
|
|
408
|
+
(!TAGGED.has(agent) || inThread.includes(agent)) &&
|
|
407
409
|
(reachable || CODING.has(agent))
|
|
408
410
|
);
|
|
409
411
|
const words = store
|
package/lib/daemon.mjs
CHANGED
|
@@ -52,6 +52,7 @@ import {
|
|
|
52
52
|
ASKED_BY_TASK,
|
|
53
53
|
CLOUD_HOMED,
|
|
54
54
|
MEMBERS,
|
|
55
|
+
TAGGED_ONLY,
|
|
55
56
|
TARGETS,
|
|
56
57
|
aboutOf,
|
|
57
58
|
member,
|
|
@@ -1291,8 +1292,7 @@ export async function startDaemon() {
|
|
|
1291
1292
|
? mentionsIn(task.result || "", ROOM_AGENTS).filter(
|
|
1292
1293
|
(agent) =>
|
|
1293
1294
|
agent !== who &&
|
|
1294
|
-
(!
|
|
1295
|
-
goal.goal.team.includes(agent))
|
|
1295
|
+
(!TAGGED_ONLY.includes(agent) || goal.goal.team.includes(agent))
|
|
1296
1296
|
)
|
|
1297
1297
|
: [];
|
|
1298
1298
|
const joining = named.filter((agent) => !goal.goal.team.includes(agent));
|
|
@@ -1431,6 +1431,7 @@ export async function startDaemon() {
|
|
|
1431
1431
|
DEFAULT_MAX_WAKES,
|
|
1432
1432
|
HELD_NOTE,
|
|
1433
1433
|
ROOM_AGENTS,
|
|
1434
|
+
available,
|
|
1434
1435
|
getActiveRuns: () => activeRuns,
|
|
1435
1436
|
endGuestThreads,
|
|
1436
1437
|
getFirewall: () => firewall,
|
package/lib/goalCommands.mjs
CHANGED
|
@@ -5,7 +5,7 @@ import { stopAgentTask } from "./runner.mjs";
|
|
|
5
5
|
import { notifyDesktop } from "./notify.mjs";
|
|
6
6
|
import { secretReason } from "./secrets.mjs";
|
|
7
7
|
import { provenanceFor } from "./provenance.mjs";
|
|
8
|
-
import { nameOf } from "./members.mjs";
|
|
8
|
+
import { TAGGED_ONLY, nameOf } from "./members.mjs";
|
|
9
9
|
import { picksIn } from "./models.mjs";
|
|
10
10
|
import { roomPage } from "./roomPage.mjs";
|
|
11
11
|
|
|
@@ -14,6 +14,7 @@ export function createGoalCommands({
|
|
|
14
14
|
DEFAULT_MAX_WAKES,
|
|
15
15
|
HELD_NOTE,
|
|
16
16
|
ROOM_AGENTS,
|
|
17
|
+
available,
|
|
17
18
|
getActiveRuns,
|
|
18
19
|
endGuestThreads,
|
|
19
20
|
getFirewall,
|
|
@@ -53,10 +54,16 @@ export function createGoalCommands({
|
|
|
53
54
|
.trim()
|
|
54
55
|
.slice(0, 500);
|
|
55
56
|
if (!objective) return { ok: false, error: "a goal needs an objective" };
|
|
57
|
+
// With no team, the agents the person named that can work here; the coordinator brings in the rest as it hands
|
|
58
|
+
// out work. Never every agent the relay knows: that put Bankr, Quotient and agents never installed into an
|
|
59
|
+
// @swarm thread (October 18).
|
|
60
|
+
const reachable = available();
|
|
56
61
|
const team =
|
|
57
62
|
Array.isArray(request.team) && request.team.length
|
|
58
63
|
? [...new Set(request.team.map(String))]
|
|
59
|
-
: ROOM_AGENTS
|
|
64
|
+
: mentionsIn(objective, ROOM_AGENTS).filter((name) =>
|
|
65
|
+
reachable.includes(name)
|
|
66
|
+
);
|
|
60
67
|
if (!team.every(validAgent)) return { ok: false, error: "invalid team" };
|
|
61
68
|
const facilitator = request.facilitator
|
|
62
69
|
? String(request.facilitator)
|
|
@@ -214,7 +221,18 @@ export function createGoalCommands({
|
|
|
214
221
|
.map(String)
|
|
215
222
|
.filter((name) => ROOM_AGENTS.includes(name) && name !== agent);
|
|
216
223
|
const named = mentionsIn(text, ROOM_AGENTS);
|
|
217
|
-
|
|
224
|
+
// An agent that joins only when tagged comes in on the person's name for it or another agent's ask, never on
|
|
225
|
+
// Harness or an agent naming it in passing; once in the thread it hears its name like anyone.
|
|
226
|
+
const askedOutright = Array.isArray(request.ask)
|
|
227
|
+
? request.ask.map(String)
|
|
228
|
+
: [];
|
|
229
|
+
const mentions = [...new Set([...asked, ...named])].filter(
|
|
230
|
+
(name) =>
|
|
231
|
+
!TAGGED_ONLY.includes(name) ||
|
|
232
|
+
agent === "user" ||
|
|
233
|
+
(agent !== "harness" && askedOutright.includes(name)) ||
|
|
234
|
+
goal.goal.team.includes(name)
|
|
235
|
+
);
|
|
218
236
|
const waitingKey = (asker, answerer) =>
|
|
219
237
|
`room-ask:${goal.id}:${asker}:${answerer}`;
|
|
220
238
|
const answered = named.filter((name) =>
|
package/lib/members.json
CHANGED
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
{ "id": "grok-bot", "name": "Grok Bot", "aliases": ["grok", "grok bot"], "road": "hosted", "home": "machine", "runs": "self", "target": true, "wake": "email",
|
|
20
20
|
"about": "research on X and the web",
|
|
21
21
|
"where": "in its own cloud", "limits": "Cannot see files on this computer unless one is attached with share_with_cloud, and cannot run anything here." },
|
|
22
|
-
{ "id": "bankr", "name": "Bankr", "aliases": [], "road": "server", "home": "cloud", "runs": "server", "target": true, "wake": "run",
|
|
22
|
+
{ "id": "bankr", "name": "Bankr", "aliases": [], "road": "server", "home": "cloud", "runs": "server", "target": true, "tagged": true, "wake": "run",
|
|
23
23
|
"about": "holds the swarm's wallet, which is the person's own, and does anything onchain the work needs: buying, selling, swapping, sending or bridging tokens, or any other transaction, always under the person's own approval and limits",
|
|
24
24
|
"where": "on Harness's server, through Bankr", "limits": "Cannot see files on this computer. Reading balances or a transaction needs no task: an agent on this computer reads them with the wallet tool in seconds." },
|
|
25
|
-
{ "id": "quotient", "name": "Quotient", "aliases": [], "road": "server", "home": "cloud", "runs": "server", "target": true, "wake": "run",
|
|
25
|
+
{ "id": "quotient", "name": "Quotient", "aliases": [], "road": "server", "home": "cloud", "runs": "server", "target": true, "tagged": true, "wake": "run",
|
|
26
26
|
"about": "prediction markets research: one search over the markets it tracks, with their odds and rules",
|
|
27
27
|
"where": "on Harness's server", "limits": "Takes no positions from a task. Cannot see files on this computer." },
|
|
28
28
|
{ "id": "muse", "name": "Muse", "aliases": [], "road": "hosted", "home": "cloud", "runs": "self", "target": true, "wake": "schedule",
|
package/lib/members.mjs
CHANGED
|
@@ -18,6 +18,9 @@ export const ids = (keep = () => true) =>
|
|
|
18
18
|
MEMBERS.filter(keep).map((entry) => entry.id);
|
|
19
19
|
/** Members others may hand work to, Harness included. */
|
|
20
20
|
export const TARGETS = ids((entry) => entry.target);
|
|
21
|
+
/** Members that join a thread only when tagged: named by the person, or asked for by another agent with `ask`. Never
|
|
22
|
+
* by @swarm, the coordinator's assembly or fallback, a default team, or Harness naming them in passing. */
|
|
23
|
+
export const TAGGED_ONLY = ids((entry) => entry.tagged);
|
|
21
24
|
/** Coding agents the daemon runs on this machine. */
|
|
22
25
|
export const MACHINE_RUNNERS = ids((entry) => entry.runs === "machine");
|
|
23
26
|
/** Targets whose tasks live on Harness's server, not the machine (Harness itself excluded). */
|
package/lib/store.mjs
CHANGED
|
@@ -1568,7 +1568,7 @@ export function openStore(file) {
|
|
|
1568
1568
|
threadRuns(goalId) {
|
|
1569
1569
|
return execute(
|
|
1570
1570
|
"all",
|
|
1571
|
-
`select id, kind, objective, target_agent, claimed_by, status, holder, result, session_id, run_workspace, run_authorized_at, usage, run_model, created_at, updated_at, reported_at
|
|
1571
|
+
`select id, kind, objective, origin_agent, target_agent, claimed_by, status, holder, result, session_id, run_workspace, run_authorized_at, usage, run_model, created_at, updated_at, reported_at
|
|
1572
1572
|
from board_tasks where parent_id = ? and kind in ('wake', 'task') order by created_at asc`,
|
|
1573
1573
|
goalId
|
|
1574
1574
|
).map((row) => ({
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
// The subagents an agent's own session spawned during a run, read from where each agent keeps them, so a thread's
|
|
2
|
+
// canvas can hang them under the agent that started them. Claude Code writes each one's transcript and a .meta.json
|
|
3
|
+
// beside its session; Codex writes each one as its own rollout naming the parent thread; OpenCode keeps each one as a
|
|
4
|
+
// child session in its database. Pi has no subagents of its own. What a finished subagent did never changes, so it is
|
|
5
|
+
// read once.
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import os from "node:os";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { spawnSync } from "node:child_process";
|
|
10
|
+
import { readJson } from "./files.mjs";
|
|
11
|
+
import { historyRoots } from "./paths.mjs";
|
|
12
|
+
import { QUIET_MS, readTurn } from "./activity.mjs";
|
|
13
|
+
import { sessionFeed } from "./live.mjs";
|
|
14
|
+
import { stepsOf } from "./runner.mjs";
|
|
15
|
+
|
|
16
|
+
const PLAIN_ID = /^[A-Za-z0-9._:-]{1,128}$/;
|
|
17
|
+
const MAX = 12;
|
|
18
|
+
const STEPS = 4;
|
|
19
|
+
const STEP_TEXT = 160;
|
|
20
|
+
// A rollout's first line names its parent thread; the base instructions that follow can run far past this.
|
|
21
|
+
const HEAD_BYTES = 16 << 10;
|
|
22
|
+
const SQLITE = "/usr/bin/sqlite3";
|
|
23
|
+
|
|
24
|
+
const iso = (ms) =>
|
|
25
|
+
Number.isFinite(ms) && ms > 0 ? new Date(ms).toISOString() : null;
|
|
26
|
+
const stamp = (value) =>
|
|
27
|
+
(typeof value === "number" ? value : Date.parse(value || "")) || null;
|
|
28
|
+
const words = (text) =>
|
|
29
|
+
String(text || "")
|
|
30
|
+
.replace(/\s+/g, " ")
|
|
31
|
+
.trim();
|
|
32
|
+
const clip = (text, limit) => {
|
|
33
|
+
const said = words(text);
|
|
34
|
+
return said.length > limit ? `${said.slice(0, limit - 1)}…` : said;
|
|
35
|
+
};
|
|
36
|
+
// Codex passes messages between its agents encrypted: nothing a person can read.
|
|
37
|
+
const sealed = (text) => /gAAAA[\w-]{24,}/.test(text);
|
|
38
|
+
const lastSteps = (items) =>
|
|
39
|
+
items
|
|
40
|
+
.filter(
|
|
41
|
+
(item) =>
|
|
42
|
+
(item.kind === "say" || item.kind === "do") && !sealed(item.text)
|
|
43
|
+
)
|
|
44
|
+
.slice(-STEPS)
|
|
45
|
+
.map((item) => ({ kind: item.kind, text: clip(item.text, STEP_TEXT) }));
|
|
46
|
+
|
|
47
|
+
function head(file, bytes = HEAD_BYTES) {
|
|
48
|
+
let fd;
|
|
49
|
+
try {
|
|
50
|
+
fd = fs.openSync(file, "r");
|
|
51
|
+
const buffer = Buffer.alloc(bytes);
|
|
52
|
+
const read = fs.readSync(fd, buffer, 0, bytes, 0);
|
|
53
|
+
return buffer.subarray(0, read).toString("utf8");
|
|
54
|
+
} catch {
|
|
55
|
+
return "";
|
|
56
|
+
} finally {
|
|
57
|
+
if (fd !== undefined) fs.closeSync(fd);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const mtimeOf = (file) => {
|
|
61
|
+
try {
|
|
62
|
+
return fs.statSync(file).mtimeMs;
|
|
63
|
+
} catch {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** OpenCode's database, where it keeps every session and its children. */
|
|
69
|
+
const opencodeDb = () =>
|
|
70
|
+
path.join(
|
|
71
|
+
process.env.XDG_DATA_HOME || path.join(os.homedir(), ".local", "share"),
|
|
72
|
+
"opencode",
|
|
73
|
+
"opencode.db"
|
|
74
|
+
);
|
|
75
|
+
function opencodeRows(sql) {
|
|
76
|
+
const db = opencodeDb();
|
|
77
|
+
if (!fs.existsSync(db) || !fs.existsSync(SQLITE)) return [];
|
|
78
|
+
const found = spawnSync(
|
|
79
|
+
SQLITE,
|
|
80
|
+
["-readonly", "-json", "-cmd", ".timeout 500", db, sql],
|
|
81
|
+
{ encoding: "utf8", timeout: 3_000 }
|
|
82
|
+
);
|
|
83
|
+
if (found.status !== 0) return [];
|
|
84
|
+
try {
|
|
85
|
+
return JSON.parse(found.stdout || "[]");
|
|
86
|
+
} catch {
|
|
87
|
+
return [];
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const quote = (value) => `'${String(value).replace(/'/g, "''")}'`;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The subagents of one run: `{ id, parent, type, task, startedAt, endedAt, live, depth, steps }`, oldest first.
|
|
94
|
+
* `parent` is the subagent that spawned it, or null for the run's own session. `now` is for tests.
|
|
95
|
+
*/
|
|
96
|
+
export function createSubagents({ now = () => Date.now() } = {}) {
|
|
97
|
+
// Finished subagents by file (or OpenCode session), and Claude Code's folder for each session.
|
|
98
|
+
const done = new Map();
|
|
99
|
+
const claudeFolders = new Map();
|
|
100
|
+
const rolloutHeads = new Map();
|
|
101
|
+
|
|
102
|
+
// A subagent counts as working while its own record says its turn is open and it wrote something lately.
|
|
103
|
+
const settled = (key, value) => {
|
|
104
|
+
if (!value.live) done.set(key, value);
|
|
105
|
+
return value;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
function claudeFolder(sessionId) {
|
|
109
|
+
if (claudeFolders.has(sessionId)) return claudeFolders.get(sessionId);
|
|
110
|
+
for (const root of historyRoots("claude-code")) {
|
|
111
|
+
let projects = [];
|
|
112
|
+
try {
|
|
113
|
+
projects = fs.readdirSync(root);
|
|
114
|
+
} catch {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
for (const project of projects) {
|
|
118
|
+
const folder = path.join(root, project, sessionId, "subagents");
|
|
119
|
+
if (fs.existsSync(folder)) {
|
|
120
|
+
claudeFolders.set(sessionId, folder);
|
|
121
|
+
return folder;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function claude(sessionId) {
|
|
129
|
+
const folder = claudeFolder(sessionId);
|
|
130
|
+
if (!folder) return [];
|
|
131
|
+
let names = [];
|
|
132
|
+
try {
|
|
133
|
+
names = fs.readdirSync(folder).filter((name) => name.endsWith(".jsonl"));
|
|
134
|
+
} catch {
|
|
135
|
+
return [];
|
|
136
|
+
}
|
|
137
|
+
return names.map((name) => {
|
|
138
|
+
const file = path.join(folder, name);
|
|
139
|
+
if (done.has(file)) return done.get(file);
|
|
140
|
+
const meta = readJson(file.replace(/\.jsonl$/, ".meta.json"), {}) ?? {};
|
|
141
|
+
let first = null;
|
|
142
|
+
try {
|
|
143
|
+
first = JSON.parse(head(file).split("\n")[0]);
|
|
144
|
+
} catch {}
|
|
145
|
+
const mtime = mtimeOf(file);
|
|
146
|
+
const open =
|
|
147
|
+
meta.finished !== true &&
|
|
148
|
+
readTurn("claude-code", file).open &&
|
|
149
|
+
now() - (mtime ?? 0) < QUIET_MS;
|
|
150
|
+
const prompt = first?.message?.content;
|
|
151
|
+
return settled(file, {
|
|
152
|
+
id: name.replace(/^agent-|\.jsonl$/g, ""),
|
|
153
|
+
parent: null,
|
|
154
|
+
type: meta.agentType || "subagent",
|
|
155
|
+
task: clip(
|
|
156
|
+
meta.description ||
|
|
157
|
+
(typeof prompt === "string" ? prompt : prompt?.[0]?.text),
|
|
158
|
+
120
|
|
159
|
+
),
|
|
160
|
+
startedAt: iso(stamp(first?.timestamp) ?? mtime),
|
|
161
|
+
endedAt: open ? null : iso(mtime),
|
|
162
|
+
live: open,
|
|
163
|
+
depth: Number(meta.spawnDepth) || 1,
|
|
164
|
+
steps: lastSteps(sessionFeed("claude-code", file, 40)),
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Codex's rollouts by day folder (YYYY/MM/DD), from the run's start to its end.
|
|
170
|
+
function codexFiles(from, to) {
|
|
171
|
+
const files = [];
|
|
172
|
+
const day = new Date(from);
|
|
173
|
+
day.setHours(0, 0, 0, 0);
|
|
174
|
+
for (; day.getTime() <= to; day.setDate(day.getDate() + 1)) {
|
|
175
|
+
const parts = [
|
|
176
|
+
String(day.getFullYear()),
|
|
177
|
+
String(day.getMonth() + 1).padStart(2, "0"),
|
|
178
|
+
String(day.getDate()).padStart(2, "0"),
|
|
179
|
+
];
|
|
180
|
+
for (const root of historyRoots("codex")) {
|
|
181
|
+
const folder = path.join(root, ...parts);
|
|
182
|
+
let names = [];
|
|
183
|
+
try {
|
|
184
|
+
names = fs.readdirSync(folder);
|
|
185
|
+
} catch {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
for (const name of names)
|
|
189
|
+
if (name.startsWith("rollout-") && name.endsWith(".jsonl"))
|
|
190
|
+
files.push(path.join(folder, name));
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return files;
|
|
194
|
+
}
|
|
195
|
+
function rolloutHead(file) {
|
|
196
|
+
if (rolloutHeads.has(file)) return rolloutHeads.get(file);
|
|
197
|
+
const line = head(file).split("\n")[0];
|
|
198
|
+
const parent = /"parent_thread_id":"([^"]+)"/.exec(line)?.[1] ?? null;
|
|
199
|
+
const found = parent
|
|
200
|
+
? {
|
|
201
|
+
id: /"payload":\{[^]*?"id":"([^"]+)"/.exec(line)?.[1] ?? null,
|
|
202
|
+
parent,
|
|
203
|
+
nickname: /"agent_nickname":"([^"]+)"/.exec(line)?.[1] ?? null,
|
|
204
|
+
role: /"agent_role":"([^"]+)"/.exec(line)?.[1] ?? null,
|
|
205
|
+
path: /"agent_path":"([^"]+)"/.exec(line)?.[1] ?? null,
|
|
206
|
+
kind: /"subagent":"([^"]+)"/.exec(line)?.[1] ?? null,
|
|
207
|
+
depth: Number(/"depth":(\d+)/.exec(line)?.[1]) || 1,
|
|
208
|
+
at: stamp(/"timestamp":"([^"]+)"/.exec(line)?.[1]),
|
|
209
|
+
}
|
|
210
|
+
: null;
|
|
211
|
+
// Only a file's first line is read, and that never changes once written.
|
|
212
|
+
if (line.endsWith("}")) rolloutHeads.set(file, found);
|
|
213
|
+
return found;
|
|
214
|
+
}
|
|
215
|
+
function codex(sessionId, startedAt, endedAt) {
|
|
216
|
+
const from = (stamp(startedAt) ?? now()) - 60_000;
|
|
217
|
+
const to = stamp(endedAt) ?? now();
|
|
218
|
+
const children = codexFiles(from, to)
|
|
219
|
+
.filter((file) => (mtimeOf(file) ?? 0) >= from)
|
|
220
|
+
.map((file) => ({ file, head: rolloutHead(file) }))
|
|
221
|
+
.filter((item) => item.head?.id);
|
|
222
|
+
// The run's own children, then theirs.
|
|
223
|
+
const found = [];
|
|
224
|
+
const parents = new Set([sessionId]);
|
|
225
|
+
for (let depth = 0; depth < 4; depth++) {
|
|
226
|
+
const next = children.filter(
|
|
227
|
+
(item) => parents.has(item.head.parent) && !found.includes(item)
|
|
228
|
+
);
|
|
229
|
+
if (!next.length) break;
|
|
230
|
+
found.push(...next);
|
|
231
|
+
for (const item of next) parents.add(item.head.id);
|
|
232
|
+
}
|
|
233
|
+
return found.map(({ file, head: meta }) => {
|
|
234
|
+
if (done.has(file)) return done.get(file);
|
|
235
|
+
const mtime = mtimeOf(file);
|
|
236
|
+
const open =
|
|
237
|
+
readTurn("codex", file).open && now() - (mtime ?? 0) < QUIET_MS;
|
|
238
|
+
const task = meta.path
|
|
239
|
+
? meta.path.split("/").at(-1).replace(/_/g, " ")
|
|
240
|
+
: meta.kind;
|
|
241
|
+
return settled(file, {
|
|
242
|
+
id: meta.id,
|
|
243
|
+
parent: meta.parent === sessionId ? null : meta.parent,
|
|
244
|
+
type: meta.role || meta.kind || "agent",
|
|
245
|
+
task: clip(
|
|
246
|
+
meta.nickname && task
|
|
247
|
+
? `${meta.nickname}: ${task}`
|
|
248
|
+
: task || meta.nickname || "",
|
|
249
|
+
120
|
|
250
|
+
),
|
|
251
|
+
startedAt: iso(meta.at ?? mtime),
|
|
252
|
+
endedAt: open ? null : iso(mtime),
|
|
253
|
+
live: open,
|
|
254
|
+
depth: meta.depth,
|
|
255
|
+
steps: lastSteps(sessionFeed("codex", file, 40)),
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function opencode(sessionId) {
|
|
261
|
+
if (!PLAIN_ID.test(sessionId)) return [];
|
|
262
|
+
const rows = opencodeRows(
|
|
263
|
+
`with recursive kids(id, parent_id, title, agent, time_created, time_updated, depth) as (
|
|
264
|
+
select id, parent_id, title, agent, time_created, time_updated, 1 from session where parent_id = ${quote(sessionId)}
|
|
265
|
+
union all select s.id, s.parent_id, s.title, s.agent, s.time_created, s.time_updated, k.depth + 1
|
|
266
|
+
from session s join kids k on s.parent_id = k.id where k.depth < 4)
|
|
267
|
+
select k.*, (select data from message m where m.session_id = k.id order by m.time_created desc limit 1) as last
|
|
268
|
+
from kids k order by time_created limit ${MAX * 2}`
|
|
269
|
+
);
|
|
270
|
+
return rows.map((row) => {
|
|
271
|
+
if (done.has(row.id)) return done.get(row.id);
|
|
272
|
+
let last = null;
|
|
273
|
+
try {
|
|
274
|
+
last = JSON.parse(row.last || "null");
|
|
275
|
+
} catch {}
|
|
276
|
+
const open =
|
|
277
|
+
!(last?.role === "assistant" && last?.time?.completed) &&
|
|
278
|
+
now() - Number(row.time_updated) < QUIET_MS;
|
|
279
|
+
const parts = opencodeRows(
|
|
280
|
+
`select data from part where session_id = ${quote(row.id)} order by time_created desc limit 30`
|
|
281
|
+
);
|
|
282
|
+
const steps = parts.reverse().flatMap((part) => {
|
|
283
|
+
try {
|
|
284
|
+
const data = JSON.parse(part.data);
|
|
285
|
+
return stepsOf({ type: data.type, part: data });
|
|
286
|
+
} catch {
|
|
287
|
+
return [];
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
return settled(row.id, {
|
|
291
|
+
id: row.id,
|
|
292
|
+
parent: row.parent_id === sessionId ? null : row.parent_id,
|
|
293
|
+
type: row.agent || "subagent",
|
|
294
|
+
task: clip(
|
|
295
|
+
String(row.title || "").replace(/\s*\(@[\w-]+ subagent\)\s*$/, ""),
|
|
296
|
+
120
|
|
297
|
+
),
|
|
298
|
+
startedAt: iso(Number(row.time_created)),
|
|
299
|
+
endedAt: open ? null : iso(Number(row.time_updated)),
|
|
300
|
+
live: open,
|
|
301
|
+
depth: Number(row.depth) || 1,
|
|
302
|
+
steps: lastSteps(steps),
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
return {
|
|
308
|
+
/** The subagents a run's session spawned while the run went on, oldest first. */
|
|
309
|
+
of({ agent, sessionId, startedAt, endedAt }) {
|
|
310
|
+
if (!sessionId || !PLAIN_ID.test(String(sessionId))) return [];
|
|
311
|
+
const id = String(sessionId);
|
|
312
|
+
const found =
|
|
313
|
+
agent === "claude-code"
|
|
314
|
+
? claude(id)
|
|
315
|
+
: agent === "codex"
|
|
316
|
+
? codex(id, startedAt, endedAt)
|
|
317
|
+
: agent === "opencode"
|
|
318
|
+
? opencode(id)
|
|
319
|
+
: [];
|
|
320
|
+
// A resumed session keeps the subagents of its earlier runs: only the ones started during this run.
|
|
321
|
+
const from = (stamp(startedAt) ?? 0) - 60_000;
|
|
322
|
+
const to = stamp(endedAt) ?? Infinity;
|
|
323
|
+
return found
|
|
324
|
+
.filter((sub) => {
|
|
325
|
+
const at = stamp(sub.startedAt) ?? 0;
|
|
326
|
+
return at >= from && at <= to;
|
|
327
|
+
})
|
|
328
|
+
.sort((a, b) => (stamp(a.startedAt) ?? 0) - (stamp(b.startedAt) ?? 0))
|
|
329
|
+
.slice(-MAX);
|
|
330
|
+
},
|
|
331
|
+
};
|
|
332
|
+
}
|
package/lib/swarm.mjs
CHANGED
|
@@ -3,6 +3,7 @@ import os from "node:os";
|
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { spawnSync } from "node:child_process";
|
|
5
5
|
import { readRunSteps } from "./runner.mjs";
|
|
6
|
+
import { createSubagents } from "./subagents.mjs";
|
|
6
7
|
import { nameOf, resolveMember } from "./members.mjs";
|
|
7
8
|
import { setNote, swarmBlock, swarmCards, swarmDigest } from "./cards.mjs";
|
|
8
9
|
import {
|
|
@@ -106,6 +107,8 @@ export function createSwarm({
|
|
|
106
107
|
// The person's post read for their reaction and for lessons (reactions.mjs, lessons.mjs), and what was learned.
|
|
107
108
|
heardPerson = () => {},
|
|
108
109
|
lessons = null,
|
|
110
|
+
// The subagents each run's own session spawned (subagents.mjs), for the thread's canvas.
|
|
111
|
+
subagents = createSubagents(),
|
|
109
112
|
}) {
|
|
110
113
|
// What the person let a thread spend of their Harness credits, in dollars, or null for no limit of its own.
|
|
111
114
|
const ceilingOf = (threadId) => {
|
|
@@ -351,10 +354,30 @@ export function createSwarm({
|
|
|
351
354
|
: [],
|
|
352
355
|
});
|
|
353
356
|
|
|
357
|
+
// Who handed a run its work and what they asked: a task names its poster; a wake, whoever woke it (threadRuns.mjs),
|
|
358
|
+
// read back from the words it was woken with for wakes from before the asker was kept.
|
|
359
|
+
const askOf = (run) => {
|
|
360
|
+
if (run.kind !== "wake")
|
|
361
|
+
return { askedBy: run.origin_agent, ask: run.objective };
|
|
362
|
+
let why = null;
|
|
363
|
+
try {
|
|
364
|
+
why = JSON.parse(store.meta(`wake-reason:${run.id}`) || "null");
|
|
365
|
+
} catch {}
|
|
366
|
+
const said = /^(\S+) (?:mentioned|asked) you: ([^]*)$/.exec(
|
|
367
|
+
why?.reason ?? ""
|
|
368
|
+
);
|
|
369
|
+
return {
|
|
370
|
+
askedBy: why?.asker ?? said?.[1] ?? run.origin_agent,
|
|
371
|
+
ask: said?.[2] ?? why?.reason ?? run.objective,
|
|
372
|
+
};
|
|
373
|
+
};
|
|
374
|
+
|
|
354
375
|
const read = (thread, hostedCaller = false) => {
|
|
355
376
|
const runs = store.threadRuns(thread.id).map((run) => ({
|
|
356
377
|
id: run.id,
|
|
357
378
|
agent: run.claimed_by || run.target_agent,
|
|
379
|
+
...askOf(run),
|
|
380
|
+
answer: run.result ? run.result.slice(0, 600) : null,
|
|
358
381
|
kind: run.kind,
|
|
359
382
|
status: run.status,
|
|
360
383
|
awaitingStart: awaitingStart(run),
|
|
@@ -368,6 +391,15 @@ export function createSwarm({
|
|
|
368
391
|
...step,
|
|
369
392
|
text: step.text.slice(0, SAY_LIMIT),
|
|
370
393
|
})),
|
|
394
|
+
// A hosted caller is told nothing about this computer's sessions.
|
|
395
|
+
subagents: hostedCaller
|
|
396
|
+
? []
|
|
397
|
+
: subagents.of({
|
|
398
|
+
agent: run.claimed_by || run.target_agent,
|
|
399
|
+
sessionId: run.session_id,
|
|
400
|
+
startedAt: run.created_at,
|
|
401
|
+
endedAt: run.reported_at,
|
|
402
|
+
}),
|
|
371
403
|
}));
|
|
372
404
|
return {
|
|
373
405
|
ok: true,
|
package/lib/threadRuns.mjs
CHANGED
|
@@ -322,7 +322,10 @@ export function createThreadRuns({
|
|
|
322
322
|
kind: "wake",
|
|
323
323
|
});
|
|
324
324
|
if (t3) store.meta(`t3-run:${wake.id}`, "1");
|
|
325
|
-
store.meta(
|
|
325
|
+
store.meta(
|
|
326
|
+
`wake-reason:${wake.id}`,
|
|
327
|
+
JSON.stringify({ reason, review, asker })
|
|
328
|
+
);
|
|
326
329
|
if (pick) setRunPick(store, wake.id, pick, asker);
|
|
327
330
|
// A review runs with the agent's own read-only switch (runner.mjs); only Claude Code and Codex have one.
|
|
328
331
|
if (review) store.meta(`review:${wake.id}`, "1");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tryharness",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.38",
|
|
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"
|