tryharness 0.7.42 → 0.7.44
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/cli.mjs +118 -5
- package/lib/actorRoster.mjs +16 -2
- package/lib/adapters.mjs +2 -1
- package/lib/answers.mjs +30 -0
- package/lib/boardCommands.mjs +9 -1
- package/lib/boardFiles.mjs +1 -1
- package/lib/boardProtocol.json +1 -0
- package/lib/bridge.mjs +49 -9
- package/lib/cards.mjs +22 -4
- package/lib/compaction.mjs +90 -0
- package/lib/context.mjs +19 -11
- package/lib/coordinator.mjs +176 -12
- package/lib/daemon.mjs +435 -49
- package/lib/fit.mjs +65 -0
- package/lib/folders.mjs +20 -0
- package/lib/frames.mjs +3 -2
- package/lib/goalCommands.mjs +12 -0
- package/lib/handover.mjs +128 -0
- package/lib/loopBackstop.mjs +104 -0
- package/lib/machineIdentity.mjs +43 -0
- package/lib/machineRoutes.mjs +97 -0
- package/lib/machineRuns.mjs +133 -0
- package/lib/machineSeal.mjs +157 -0
- package/lib/mcp-stdio.mjs +54 -11
- package/lib/nudge.mjs +211 -44
- package/lib/oneLine.mjs +27 -0
- package/lib/ownMachines.mjs +748 -0
- package/lib/paths.mjs +1 -0
- package/lib/plans.mjs +3 -2
- package/lib/provenance.mjs +4 -1
- package/lib/runBrief.mjs +346 -0
- package/lib/runWorker.mjs +24 -173
- package/lib/runner.mjs +26 -2
- package/lib/store.mjs +232 -16
- package/lib/stuck.mjs +198 -0
- package/lib/swarm.mjs +3 -1
- package/lib/taskCommands.mjs +268 -31
- package/lib/taskRuns.mjs +9 -0
- package/lib/team.json +192 -0
- package/lib/team.mjs +417 -0
- package/lib/threadRuns.mjs +24 -9
- package/lib/threadView.mjs +13 -0
- package/lib/trails.mjs +49 -0
- package/lib/wakes.mjs +201 -69
- package/lib/worktrees.mjs +9 -0
- package/package.json +1 -1
package/lib/fit.mjs
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Text an agent is handed whole (a run's brief, a session's start) made of named sections that must fit a budget. Each
|
|
2
|
+
// section has a priority and, besides its whole text, shorter forms ending in a pointer: a line that says what was left
|
|
3
|
+
// out and which tool reads it. Over budget, the lowest-priority section, the largest first, steps down one form at a
|
|
4
|
+
// time, so nothing is cut mid-way and nothing is lost silently. A core section has no pointer and is always sent.
|
|
5
|
+
|
|
6
|
+
const RANK = { optional: 0, recommended: 1, core: 2 };
|
|
7
|
+
const JOIN = "\n\n";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* sections: [{ name, priority: "core" | "recommended" | "optional", source, text, shorter?: [text], pointer?: text }].
|
|
11
|
+
* Returns the text as sent and, for each section, its whole size, what was sent and in which form.
|
|
12
|
+
*/
|
|
13
|
+
export function fitSections(sections, budget) {
|
|
14
|
+
const entries = sections
|
|
15
|
+
.map((section) => ({ ...section, text: String(section.text ?? "").trim() }))
|
|
16
|
+
.filter((section) => section.text)
|
|
17
|
+
.map((section) => ({
|
|
18
|
+
...section,
|
|
19
|
+
forms: [
|
|
20
|
+
section.text,
|
|
21
|
+
...(section.shorter ?? []),
|
|
22
|
+
...(section.pointer ? [section.pointer] : []),
|
|
23
|
+
].filter(Boolean),
|
|
24
|
+
level: 0,
|
|
25
|
+
}));
|
|
26
|
+
const current = (entry) => entry.forms[entry.level];
|
|
27
|
+
const total = () =>
|
|
28
|
+
entries.reduce((sum, entry) => sum + current(entry).length, 0) +
|
|
29
|
+
JOIN.length * Math.max(0, entries.length - 1);
|
|
30
|
+
const canStep = (entry) => entry.level < entry.forms.length - 1;
|
|
31
|
+
while (total() > budget) {
|
|
32
|
+
const next = entries
|
|
33
|
+
.filter(canStep)
|
|
34
|
+
.sort(
|
|
35
|
+
(a, b) =>
|
|
36
|
+
RANK[a.priority] - RANK[b.priority] ||
|
|
37
|
+
current(b).length - current(a).length
|
|
38
|
+
)[0];
|
|
39
|
+
if (!next) break;
|
|
40
|
+
next.level += 1;
|
|
41
|
+
}
|
|
42
|
+
const sent = total();
|
|
43
|
+
return {
|
|
44
|
+
text: entries.map(current).join(JOIN),
|
|
45
|
+
budget,
|
|
46
|
+
whole:
|
|
47
|
+
entries.reduce((sum, entry) => sum + entry.text.length, 0) +
|
|
48
|
+
JOIN.length * Math.max(0, entries.length - 1),
|
|
49
|
+
sent,
|
|
50
|
+
over: sent > budget,
|
|
51
|
+
sections: entries.map((entry) => ({
|
|
52
|
+
name: entry.name,
|
|
53
|
+
priority: entry.priority,
|
|
54
|
+
source: entry.source,
|
|
55
|
+
chars: entry.text.length,
|
|
56
|
+
sent: current(entry).length,
|
|
57
|
+
form:
|
|
58
|
+
entry.level === 0
|
|
59
|
+
? "whole"
|
|
60
|
+
: entry.pointer && entry.level === entry.forms.length - 1
|
|
61
|
+
? "pointer"
|
|
62
|
+
: "shortened",
|
|
63
|
+
})),
|
|
64
|
+
};
|
|
65
|
+
}
|
package/lib/folders.mjs
CHANGED
|
@@ -78,6 +78,26 @@ function repositoryOf(dir) {
|
|
|
78
78
|
return root;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
+
/**
|
|
82
|
+
* The repository a folder holds, named the same on every computer that has it: its origin's host and path, without
|
|
83
|
+
* credentials, scheme or ".git" (git@github.com:a/b.git and https://github.com/a/b are both github.com/a/b). Null for a
|
|
84
|
+
* folder with no origin.
|
|
85
|
+
*/
|
|
86
|
+
export function repositoryName(dir) {
|
|
87
|
+
if (!isDir(dir)) return null;
|
|
88
|
+
const remote = git(dir, ["remote", "get-url", "origin"]);
|
|
89
|
+
if (!remote) return null;
|
|
90
|
+
return (
|
|
91
|
+
remote
|
|
92
|
+
.replace(/^[a-z+]+:\/\//i, "")
|
|
93
|
+
.replace(/^[^@/]+@/, "")
|
|
94
|
+
.replace(/^([^/:]+):(?!\d)/, "$1/")
|
|
95
|
+
.replace(/\.git\/?$/, "")
|
|
96
|
+
.replace(/\/+$/, "")
|
|
97
|
+
.toLowerCase() || null
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
81
101
|
/**
|
|
82
102
|
* What Jev reads of a folder: its name, its repository, who worked there and when, and the titles of the sessions there
|
|
83
103
|
* (each the start of its first message), newest first. What was worked on in a folder says where new work goes; READMEs
|
package/lib/frames.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import path from "node:path";
|
|
|
2
2
|
import { createHash } from "node:crypto";
|
|
3
3
|
import { repositoryOf } from "./worktrees.mjs";
|
|
4
4
|
import { nameOf } from "./members.mjs";
|
|
5
|
+
import { oneLine } from "./oneLine.mjs";
|
|
5
6
|
|
|
6
7
|
// The task frame: one record per thread, built by the relay from what is on this computer and refreshed at every
|
|
7
8
|
// handoff. What's asked: the files and code the person named, resolved to where they are (a function named in the words
|
|
@@ -278,12 +279,12 @@ export function createFrames({
|
|
|
278
279
|
`${entry.path} (${entry.state}${entry.minutesAgo !== null ? `, ${entry.minutesAgo} min ago` : ""})`
|
|
279
280
|
);
|
|
280
281
|
lines.push(
|
|
281
|
-
`- Repository: ${repo.branch ? `branch ${repo.branch}` : "no branch"}${repo.own ? ", this thread's own copy" : ""}; ${repo.uncommitted ? `${repo.uncommitted} file${repo.uncommitted === 1 ? "" : "s"} changed and not committed${changed.length ? `: ${changed.join(", ")}${repo.uncommitted > changed.length ? ", ..." : ""}` : ""}` : "nothing uncommitted"}${repo.lastCommit ? `; last commit ${repo.lastCommit.minutesAgo} min ago ("${repo.lastCommit.subject
|
|
282
|
+
`- Repository: ${repo.branch ? `branch ${repo.branch}` : "no branch"}${repo.own ? ", this thread's own copy" : ""}; ${repo.uncommitted ? `${repo.uncommitted} file${repo.uncommitted === 1 ? "" : "s"} changed and not committed${changed.length ? `: ${changed.join(", ")}${repo.uncommitted > changed.length ? ", ..." : ""}` : ""}` : "nothing uncommitted"}${repo.lastCommit ? `; last commit ${repo.lastCommit.minutesAgo} min ago ("${oneLine(repo.lastCommit.subject, 80)}")` : ""}.`
|
|
282
283
|
);
|
|
283
284
|
}
|
|
284
285
|
for (const peer of frame.peers)
|
|
285
286
|
lines.push(
|
|
286
|
-
`- Also working in this repository: the thread "${peer.title
|
|
287
|
+
`- Also working in this repository: the thread "${oneLine(peer.title, 80)}"${peer.running.length ? ` (${peer.running.map(nameOf).join(", ")} running now)` : ""}${peer.files.length ? `, changing ${peer.files.slice(0, 5).join(", ")}${peer.files.length > 5 ? ` and ${peer.files.length - 5} more` : ""}` : ""}.`
|
|
287
288
|
);
|
|
288
289
|
for (const check of frame.checks
|
|
289
290
|
.filter((entry) => entry.agent !== agent || frame.checks.length > 1)
|
package/lib/goalCommands.mjs
CHANGED
|
@@ -39,6 +39,8 @@ export function createGoalCommands({
|
|
|
39
39
|
wakeFor,
|
|
40
40
|
resumed,
|
|
41
41
|
files: boardFiles,
|
|
42
|
+
// The thread's team (team.mjs): an agent's note on what it learned in its role.
|
|
43
|
+
teams = null,
|
|
42
44
|
}) {
|
|
43
45
|
return (request, agent) => {
|
|
44
46
|
const id = String(request.id || "");
|
|
@@ -147,6 +149,16 @@ export function createGoalCommands({
|
|
|
147
149
|
const page = roomPage(store, goal.id, request);
|
|
148
150
|
return { ...view, ...page, posts: page.posts.map(flagged), files };
|
|
149
151
|
}
|
|
152
|
+
if (request.command === "board.room.learn") {
|
|
153
|
+
if (!inRoom(goal, agent) || agent === "user")
|
|
154
|
+
return { ok: false, error: `${agent} is not on this goal's team` };
|
|
155
|
+
if (!teams) return { ok: false, error: "the daemon is still starting" };
|
|
156
|
+
return teams.learn(goal.id, agent, {
|
|
157
|
+
text: request.text,
|
|
158
|
+
reason: request.reason,
|
|
159
|
+
replaces: request.replaces,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
150
162
|
if (request.command === "board.room.search") {
|
|
151
163
|
if (!inRoom(goal, agent))
|
|
152
164
|
return { ok: false, error: `${agent} is not on this goal's team` };
|
package/lib/handover.mjs
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { readTrail } from "./trails.mjs";
|
|
2
|
+
import { sessionFile } from "./live.mjs";
|
|
3
|
+
import { catalogOf } from "./models.mjs";
|
|
4
|
+
import { nameOf } from "./members.mjs";
|
|
5
|
+
import { oneLine } from "./oneLine.mjs";
|
|
6
|
+
|
|
7
|
+
// A thread's agent resumes its own session each turn. A session close to its model's window would compact within the
|
|
8
|
+
// turn and lose its record to a summary, so before resuming, the relay reads the session's last model call from the
|
|
9
|
+
// agent's own file and, past the line, starts a fresh session that is told where the old one is and what was said last.
|
|
10
|
+
|
|
11
|
+
// Where the line sits, measured on the owner's machine, October 20, 2026:
|
|
12
|
+
// - Codex compacted 90 times since September 15; the last call before each carried a median 87% of the window Codex
|
|
13
|
+
// reported, and 77% or more in 9 of 10 (the lowest 58%). Claude Code's automatic compactions of 1M-token sessions came
|
|
14
|
+
// at 96.6% or more.
|
|
15
|
+
// - A thread turn that resumed a session grew it by up to 39,469 tokens before its last call (13 turns of Claude Code and
|
|
16
|
+
// Codex on the board with their session files still on disk; median 9,773 to 13,309).
|
|
17
|
+
// So a session is resumed only while its last call plus one such turn stays under 77% of the window.
|
|
18
|
+
export const COMPACTS_AT = 0.77;
|
|
19
|
+
export const TURN_GROWTH = 40_000;
|
|
20
|
+
export const lineFor = (window) =>
|
|
21
|
+
Math.floor(window * COMPACTS_AT) - TURN_GROWTH;
|
|
22
|
+
|
|
23
|
+
const count = (value) => Number(value).toLocaleString("en-US");
|
|
24
|
+
|
|
25
|
+
export function createHandover({ store, log, sync }) {
|
|
26
|
+
const fileOf = async (agent, sessionId, folder) => {
|
|
27
|
+
const found = () =>
|
|
28
|
+
sessionFile(agent, sessionId, folder, store.recentSessions(100));
|
|
29
|
+
if (found()) return found();
|
|
30
|
+
await sync(agent);
|
|
31
|
+
return found();
|
|
32
|
+
};
|
|
33
|
+
// The window the agent itself reported for the session (Codex), else its model's in the lists the agents keep: Claude
|
|
34
|
+
// Code lists none of its own, so its model is found among the Anthropic models Pi and OpenCode list.
|
|
35
|
+
const windowOf = (agent, trail) => {
|
|
36
|
+
if (trail.window) return { window: trail.window, from: "the session" };
|
|
37
|
+
const model = String(trail.model || "").toLowerCase();
|
|
38
|
+
if (!model) return null;
|
|
39
|
+
for (const lister of [agent, "pi", "opencode"]) {
|
|
40
|
+
const entry = catalogOf(store, lister).find((each) => {
|
|
41
|
+
const id = each.id.toLowerCase();
|
|
42
|
+
return id === model || id.endsWith(`/${model}`);
|
|
43
|
+
});
|
|
44
|
+
if (entry?.context)
|
|
45
|
+
return { window: entry.context, from: `${nameOf(lister)}'s list` };
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
};
|
|
49
|
+
const replayed = (said) =>
|
|
50
|
+
said.map(
|
|
51
|
+
(entry) =>
|
|
52
|
+
`- ${entry.by === "agent" ? "You" : "To you"}: ${entry.text}${entry.cut ? " [cut short: the transcript has all of it]" : ""}`
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Whether an agent's session is too full to resume: null to resume it, else what the fresh session is told (its
|
|
57
|
+
* predecessor section) and the record of why. Never throws; anything unreadable resumes as before, logged.
|
|
58
|
+
*/
|
|
59
|
+
const check = async ({ agent, sessionId, folder }) => {
|
|
60
|
+
try {
|
|
61
|
+
const file = await fileOf(agent, sessionId, folder);
|
|
62
|
+
if (!file) {
|
|
63
|
+
log(
|
|
64
|
+
`handover: ${agent} session ${sessionId} has no file here; resumed`
|
|
65
|
+
);
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
const trail = await readTrail(agent, file);
|
|
69
|
+
const input = trail?.usage.at(-1)?.input ?? null;
|
|
70
|
+
if (!trail || input === null) return null;
|
|
71
|
+
const known = windowOf(agent, trail);
|
|
72
|
+
if (!known) {
|
|
73
|
+
log(
|
|
74
|
+
`handover: no window known for ${agent}'s model ${trail.model ?? "(none)"}; session ${sessionId} resumed`
|
|
75
|
+
);
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
const line = lineFor(known.window);
|
|
79
|
+
if (input <= line) return null;
|
|
80
|
+
const model = oneLine(trail.model || "its model", 80);
|
|
81
|
+
const why = `its last model call carried ${count(input)} tokens, past the line of ${count(line)} for ${model}'s ${count(known.window)}-token window`;
|
|
82
|
+
const said = trail.said.slice(-6);
|
|
83
|
+
const text = [
|
|
84
|
+
`Your earlier session in this thread (${sessionId}) was too full to continue, so this turn starts a fresh one: ${why}.`,
|
|
85
|
+
`Its whole transcript: ${file}`,
|
|
86
|
+
said.length
|
|
87
|
+
? [
|
|
88
|
+
"Its last exchanges, replayed from the record (said in that session, not this one):",
|
|
89
|
+
...replayed(said),
|
|
90
|
+
].join("\n")
|
|
91
|
+
: "No exchanges are replayed: the record of that session holds no messages.",
|
|
92
|
+
].join("\n");
|
|
93
|
+
return {
|
|
94
|
+
record: {
|
|
95
|
+
from: sessionId,
|
|
96
|
+
file,
|
|
97
|
+
input,
|
|
98
|
+
line,
|
|
99
|
+
window: known.window,
|
|
100
|
+
windowFrom: known.from,
|
|
101
|
+
model: trail.model,
|
|
102
|
+
replayed: said.length,
|
|
103
|
+
},
|
|
104
|
+
why,
|
|
105
|
+
section: {
|
|
106
|
+
text,
|
|
107
|
+
pointer: `Your earlier session in this thread (${sessionId}) was too full to continue, so this turn starts a fresh one. Its whole transcript: ${file}`,
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
} catch (error) {
|
|
111
|
+
log(`handover for ${agent} session ${sessionId}: ${error.message}`);
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/** The thread hears which session was set aside and why; the run keeps the record. */
|
|
117
|
+
const announce = ({ threadId, taskId, agent, handed }) => {
|
|
118
|
+
store.meta(`handover:${taskId}`, JSON.stringify(handed.record));
|
|
119
|
+
store.roomPost({
|
|
120
|
+
goalId: threadId,
|
|
121
|
+
author: "harness",
|
|
122
|
+
kind: "note",
|
|
123
|
+
text: `${nameOf(agent)} starts a fresh session here: its last one (${handed.record.from}) was too full to continue, as ${handed.why}. The new session's brief replays its last exchanges from the record and names the old transcript.`,
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
return { check, announce };
|
|
128
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// A counted backstop beside the coordinator's own judgement of a loop: a thread whose agents keep taking turns that
|
|
2
|
+
// bring nothing new is paused, and the person is told why, however the rounds read it. Something new is a change (a
|
|
3
|
+
// turn's commit or a receipt that shows changed files), a result (a job finished with words, a file), or one more of
|
|
4
|
+
// the goal's checks reached. The person writing, a check-in and the backstop's own pause start the count again. Once it
|
|
5
|
+
// pauses a thread it does not again for an hour (owner).
|
|
6
|
+
//
|
|
7
|
+
// LOOP_TURNS, from the owner's board on October 20 (a read-only copy): in 212 stretches of turns that ended in
|
|
8
|
+
// something new (with the person's posts and the goal's reached checks counted as new), the turns in a stretch were
|
|
9
|
+
// p50 1, p90 2, p95 3, p99 4, at most 5. 8 is three past the longest and twice the p99.
|
|
10
|
+
export const LOOP_TURNS = 8;
|
|
11
|
+
export const LOOP_COOLDOWN_MS = 60 * 60_000;
|
|
12
|
+
|
|
13
|
+
const ENDED = ["done", "failed", "unknown"];
|
|
14
|
+
// Turns that never reached their agent: the person stopped them, or they could not start.
|
|
15
|
+
const NEVER_RAN = ["person", "start"];
|
|
16
|
+
|
|
17
|
+
const latest = (times) =>
|
|
18
|
+
times.filter(Boolean).reduce((a, b) => (a > b ? a : b), "");
|
|
19
|
+
|
|
20
|
+
export function createLoopBackstop({
|
|
21
|
+
store,
|
|
22
|
+
goals,
|
|
23
|
+
pauseGoal,
|
|
24
|
+
log = () => {},
|
|
25
|
+
limit = LOOP_TURNS,
|
|
26
|
+
cooldownMs = LOOP_COOLDOWN_MS,
|
|
27
|
+
now = Date.now,
|
|
28
|
+
}) {
|
|
29
|
+
const key = (threadId) => `loop-backstop:${threadId}`;
|
|
30
|
+
const stateOf = (threadId) => {
|
|
31
|
+
try {
|
|
32
|
+
return JSON.parse(store.meta(key(threadId)) || "{}");
|
|
33
|
+
} catch {
|
|
34
|
+
return {};
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const changed = (run) => {
|
|
38
|
+
if (store.meta(`turn:${run.id}`)) return true;
|
|
39
|
+
try {
|
|
40
|
+
return (
|
|
41
|
+
JSON.parse(store.meta(`receipt:${run.id}`) || "null")?.changed?.length >
|
|
42
|
+
0
|
|
43
|
+
);
|
|
44
|
+
} catch {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const resulted = (run) =>
|
|
49
|
+
run.kind === "task" &&
|
|
50
|
+
run.status === "done" &&
|
|
51
|
+
Boolean(String(run.result || "").trim());
|
|
52
|
+
|
|
53
|
+
// When the thread last had something new, or its count last began again.
|
|
54
|
+
const lastNew = (thread, runs, state) =>
|
|
55
|
+
latest([
|
|
56
|
+
thread.created_at,
|
|
57
|
+
store.lastPostAt(thread.id, "user"),
|
|
58
|
+
store.meta(`check-in-at:${thread.id}`),
|
|
59
|
+
...runs
|
|
60
|
+
.filter((run) => run.reported_at && (changed(run) || resulted(run)))
|
|
61
|
+
.map((run) => run.reported_at),
|
|
62
|
+
...store.taskFiles(thread.id).map((file) => file?.created_at),
|
|
63
|
+
state.metAt,
|
|
64
|
+
state.pausedAt,
|
|
65
|
+
]);
|
|
66
|
+
|
|
67
|
+
/** Looks at a thread after a turn in it ended; pauses it when the count is reached. True when it paused it. */
|
|
68
|
+
const check = (threadId) => {
|
|
69
|
+
const thread = store.getTask(threadId);
|
|
70
|
+
if (thread?.kind !== "goal" || thread.status !== "open") return false;
|
|
71
|
+
const at = new Date(now()).toISOString();
|
|
72
|
+
let state = stateOf(threadId);
|
|
73
|
+
const met =
|
|
74
|
+
goals
|
|
75
|
+
.view(threadId)
|
|
76
|
+
?.criteria.filter((criterion) => criterion.status === "met").length ??
|
|
77
|
+
0;
|
|
78
|
+
if (met !== (state.met ?? 0)) {
|
|
79
|
+
state = { ...state, met, ...(met > (state.met ?? 0) && { metAt: at }) };
|
|
80
|
+
store.meta(key(threadId), JSON.stringify(state));
|
|
81
|
+
}
|
|
82
|
+
const runs = store.threadRuns(threadId);
|
|
83
|
+
const since = lastNew(thread, runs, state);
|
|
84
|
+
const idle = runs.filter(
|
|
85
|
+
(run) =>
|
|
86
|
+
run.created_at > since &&
|
|
87
|
+
ENDED.includes(run.status) &&
|
|
88
|
+
!NEVER_RAN.includes(run.failure)
|
|
89
|
+
).length;
|
|
90
|
+
if (idle < limit) return false;
|
|
91
|
+
const cooling =
|
|
92
|
+
state.pausedAt && now() - Date.parse(state.pausedAt) < cooldownMs;
|
|
93
|
+
if (cooling) return false;
|
|
94
|
+
store.meta(key(threadId), JSON.stringify({ ...state, pausedAt: at }));
|
|
95
|
+
log(`loop backstop: paused ${threadId} after ${idle} turns`);
|
|
96
|
+
pauseGoal(
|
|
97
|
+
threadId,
|
|
98
|
+
`${idle} turns in a row brought no new change, result or reached check`
|
|
99
|
+
);
|
|
100
|
+
return true;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
return { check };
|
|
104
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import { randomBytes } from "node:crypto";
|
|
3
|
+
import { readJson, writeJsonAtomic } from "./files.mjs";
|
|
4
|
+
|
|
5
|
+
// Which of the person's computers this is (machine.json, beside its key in ~/.harness): a fixed id made once, the name
|
|
6
|
+
// the person reads it by (the hostname until they rename it: npx tryharness machines name <name>), and the role
|
|
7
|
+
// Harness last gave it, home or worker (src/server/bridge/machines.ts decides; this is its memory between starts).
|
|
8
|
+
|
|
9
|
+
const ID = /^[A-Za-z0-9_-]{8,64}$/;
|
|
10
|
+
const ROLES = new Set(["home", "worker"]);
|
|
11
|
+
|
|
12
|
+
const hostname = () =>
|
|
13
|
+
os
|
|
14
|
+
.hostname()
|
|
15
|
+
.replace(/\.local$/, "")
|
|
16
|
+
.slice(0, 64) || "computer";
|
|
17
|
+
|
|
18
|
+
/** This computer's id, name and role, the id made the first time. */
|
|
19
|
+
export function loadMachine(file) {
|
|
20
|
+
const saved = readJson(file, {});
|
|
21
|
+
const machine = {
|
|
22
|
+
id:
|
|
23
|
+
typeof saved.id === "string" && ID.test(saved.id)
|
|
24
|
+
? saved.id
|
|
25
|
+
: randomBytes(16).toString("base64url"),
|
|
26
|
+
label:
|
|
27
|
+
typeof saved.label === "string" && saved.label.trim()
|
|
28
|
+
? saved.label.trim().slice(0, 64)
|
|
29
|
+
: hostname(),
|
|
30
|
+
role: ROLES.has(saved.role) ? saved.role : null,
|
|
31
|
+
};
|
|
32
|
+
if (saved.id !== machine.id || saved.label !== machine.label)
|
|
33
|
+
saveMachine(file, machine);
|
|
34
|
+
return machine;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function saveMachine(file, machine) {
|
|
38
|
+
writeJsonAtomic(file, {
|
|
39
|
+
id: machine.id,
|
|
40
|
+
label: machine.label,
|
|
41
|
+
...(machine.role ? { role: machine.role } : {}),
|
|
42
|
+
});
|
|
43
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { boardCommandOwner } from "./boardProtocol.mjs";
|
|
2
|
+
|
|
3
|
+
// Where board work goes when the person's agents live on several computers. The board is home's; an agent keeps its
|
|
4
|
+
// id and a task names the computer it is for (target_machine) and the one that took it (claimed_machine). People and
|
|
5
|
+
// agents read "Codex (linux)". Pure decisions, so they read the same on every computer.
|
|
6
|
+
|
|
7
|
+
// What an agent does on its own computer: its tools on local files, its live session, the watches set there, its
|
|
8
|
+
// prompts. Everything else on the board is home's.
|
|
9
|
+
const LOCAL_OWNERS = new Set(["tools", "live", "watch", "prompt"]);
|
|
10
|
+
|
|
11
|
+
/** Whether a worker sends this board command to home. */
|
|
12
|
+
export const goesHome = (command) =>
|
|
13
|
+
String(command).startsWith("board.") &&
|
|
14
|
+
!LOCAL_OWNERS.has(boardCommandOwner(String(command)));
|
|
15
|
+
|
|
16
|
+
// Writes an agent need not wait on: while home is out of reach the worker keeps them, sealed, and sends them when it is
|
|
17
|
+
// back. A claim or a post waits for its answer instead.
|
|
18
|
+
const QUEUEABLE = new Set([
|
|
19
|
+
"board.report",
|
|
20
|
+
"board.answer",
|
|
21
|
+
"board.release",
|
|
22
|
+
"board.room.post",
|
|
23
|
+
"board.thread.post",
|
|
24
|
+
"board.context.add",
|
|
25
|
+
"board.machine.finish",
|
|
26
|
+
]);
|
|
27
|
+
export const queueable = (command) => QUEUEABLE.has(String(command));
|
|
28
|
+
|
|
29
|
+
/** Whether a caller on `machine` may take this task: work meant for one computer is taken only there. */
|
|
30
|
+
export const mayClaim = (task, machine) =>
|
|
31
|
+
!task?.target_machine || task.target_machine === machine;
|
|
32
|
+
|
|
33
|
+
/** Whether a caller on `machine` may report on this task: only the computer that took it. */
|
|
34
|
+
export const mayReport = (task, machine) =>
|
|
35
|
+
!task?.claimed_machine || task.claimed_machine === machine;
|
|
36
|
+
|
|
37
|
+
/** Whether this computer runs a task itself: one meant for it, or for no computer in particular when it is home. */
|
|
38
|
+
export const runsHere = (task, { machine, home }) =>
|
|
39
|
+
task?.target_machine ? task.target_machine === machine : home;
|
|
40
|
+
|
|
41
|
+
/** A computer named by its id or its name, any case. */
|
|
42
|
+
export function findMachine(machines, named) {
|
|
43
|
+
const wanted = String(named ?? "")
|
|
44
|
+
.trim()
|
|
45
|
+
.toLowerCase();
|
|
46
|
+
if (!wanted) return null;
|
|
47
|
+
return (
|
|
48
|
+
machines.find((machine) => machine.id === named) ??
|
|
49
|
+
machines.find(
|
|
50
|
+
(machine) => String(machine.label || "").toLowerCase() === wanted
|
|
51
|
+
) ??
|
|
52
|
+
null
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The computer a task for `agent` goes to. Named: that one, or a refusal naming the computers there are. Not named:
|
|
58
|
+
* the poster's own computer when the poster works there in a folder for an agent installed there; else whichever
|
|
59
|
+
* computer has the agent and the thread's repository; else the only computer with the agent; else none (home, or
|
|
60
|
+
* whichever the coordinator hands it to).
|
|
61
|
+
* machines: [{ id, label, role, agents: [..], repositories: [..] }]
|
|
62
|
+
*/
|
|
63
|
+
export function placeTask({
|
|
64
|
+
agent,
|
|
65
|
+
named = null,
|
|
66
|
+
machines,
|
|
67
|
+
poster = null,
|
|
68
|
+
repository = null,
|
|
69
|
+
}) {
|
|
70
|
+
if (named != null && String(named).trim()) {
|
|
71
|
+
const machine = findMachine(machines, named);
|
|
72
|
+
if (!machine)
|
|
73
|
+
return {
|
|
74
|
+
ok: false,
|
|
75
|
+
error: `No computer is called "${named}". Your computers: ${machines.map((each) => each.label || each.id).join(", ")}.`,
|
|
76
|
+
};
|
|
77
|
+
return { ok: true, machine: machine.id };
|
|
78
|
+
}
|
|
79
|
+
if (!agent) return { ok: true, machine: null };
|
|
80
|
+
const holders = machines.filter((machine) =>
|
|
81
|
+
(machine.agents ?? []).includes(agent)
|
|
82
|
+
);
|
|
83
|
+
if (poster && holders.some((machine) => machine.id === poster))
|
|
84
|
+
return { ok: true, machine: poster };
|
|
85
|
+
const withRepository = repository
|
|
86
|
+
? holders.filter((machine) =>
|
|
87
|
+
(machine.repositories ?? []).includes(repository)
|
|
88
|
+
)
|
|
89
|
+
: [];
|
|
90
|
+
if (withRepository.length === 1)
|
|
91
|
+
return { ok: true, machine: withRepository[0].id };
|
|
92
|
+
if (holders.length === 1) return { ok: true, machine: holders[0].id };
|
|
93
|
+
return { ok: true, machine: null };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** How people and agents read an agent on a computer: "Codex (linux)". */
|
|
97
|
+
export const onMachine = (name, label) => (label ? `${name} (${label})` : name);
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { provenanceLine } from "./provenance.mjs";
|
|
3
|
+
|
|
4
|
+
// On a worker: the work home keeps for this computer's agents, run here. The person's Start (or a thread's authority)
|
|
5
|
+
// is home's to give and stays on home's board; a worker only asks home for what is authorized for it, takes each task
|
|
6
|
+
// in its agent's name, runs it here through the same runner as any run (taskRuns.mjs), and reports back to home, which
|
|
7
|
+
// keeps the report while home is out of reach. Asked when home is back, when home says there is work, and when a run
|
|
8
|
+
// ends; never on a timer. One run per folder at a time, as on home.
|
|
9
|
+
//
|
|
10
|
+
// Home's stuck sweep (stuck.mjs) reads when each run last took a step. A run here has no record on home, so each new
|
|
11
|
+
// step is told to home, sealed like every call: the first at once, then at most one a second per run (the rate home's
|
|
12
|
+
// own run worker tells its steps, runWorker.mjs), the latest standing for those between. Nothing is told while a run
|
|
13
|
+
// takes no step, and a step home could not hear is not kept: the last one it heard stands, as for a computer offline.
|
|
14
|
+
const STEP_NOTICE_MS = 1_000;
|
|
15
|
+
|
|
16
|
+
export function createMachineRuns({ callHome, taskRuns, agents, log }) {
|
|
17
|
+
const active = new Map();
|
|
18
|
+
const steps = new Map();
|
|
19
|
+
let asking = false;
|
|
20
|
+
let askAgain = false;
|
|
21
|
+
|
|
22
|
+
const tellStep = (taskId, agent) => {
|
|
23
|
+
const step = steps.get(taskId) ?? { toldAt: 0, timer: null };
|
|
24
|
+
steps.set(taskId, step);
|
|
25
|
+
if (step.timer) return;
|
|
26
|
+
step.timer = setTimeout(
|
|
27
|
+
() => {
|
|
28
|
+
step.timer = null;
|
|
29
|
+
step.toldAt = Date.now();
|
|
30
|
+
void callHome({ command: "board.machine.step", agent, id: taskId });
|
|
31
|
+
},
|
|
32
|
+
Math.max(0, step.toldAt + STEP_NOTICE_MS - Date.now())
|
|
33
|
+
);
|
|
34
|
+
};
|
|
35
|
+
const stopSteps = (taskId) => {
|
|
36
|
+
clearTimeout(steps.get(taskId)?.timer);
|
|
37
|
+
steps.delete(taskId);
|
|
38
|
+
};
|
|
39
|
+
const slotOf = (task) =>
|
|
40
|
+
task.run_workspace ? path.resolve(task.run_workspace) : task.target_agent;
|
|
41
|
+
|
|
42
|
+
const briefOf = (task) => {
|
|
43
|
+
const refs = (task.context_refs ?? [])
|
|
44
|
+
.map(
|
|
45
|
+
(ref) => `- ${ref.kind}: ${ref.ref}${ref.note ? ` (${ref.note})` : ""}`
|
|
46
|
+
)
|
|
47
|
+
.join("\n");
|
|
48
|
+
const notice = provenanceLine(task);
|
|
49
|
+
return [notice, task.objective, task.brief, refs ? `Context:\n${refs}` : ""]
|
|
50
|
+
.filter(Boolean)
|
|
51
|
+
.join("\n\n");
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const start = (task, slot) => {
|
|
55
|
+
const agent = task.target_agent;
|
|
56
|
+
log(`running ${agent}'s task ${task.id} from your home computer`);
|
|
57
|
+
taskRuns.startTask(
|
|
58
|
+
{
|
|
59
|
+
runId: task.id,
|
|
60
|
+
agent,
|
|
61
|
+
title: String(task.objective || "").slice(0, 120),
|
|
62
|
+
brief: briefOf(task),
|
|
63
|
+
cwd: task.run_workspace,
|
|
64
|
+
},
|
|
65
|
+
(kind, text, extra) => {
|
|
66
|
+
if (kind === "session" && text)
|
|
67
|
+
void callHome({
|
|
68
|
+
command: "board.machine.session",
|
|
69
|
+
agent,
|
|
70
|
+
id: task.id,
|
|
71
|
+
session: text,
|
|
72
|
+
});
|
|
73
|
+
if (kind === "progress") tellStep(task.id, agent);
|
|
74
|
+
if (kind !== "done" && kind !== "failed") return;
|
|
75
|
+
stopSteps(task.id);
|
|
76
|
+
active.delete(slot);
|
|
77
|
+
void callHome({
|
|
78
|
+
command: "board.machine.finish",
|
|
79
|
+
agent,
|
|
80
|
+
id: task.id,
|
|
81
|
+
status: kind,
|
|
82
|
+
result: text || "",
|
|
83
|
+
failure: extra?.failure ?? "exit",
|
|
84
|
+
...(extra?.usage ? { usage: extra.usage } : {}),
|
|
85
|
+
}).then((answer) => {
|
|
86
|
+
if (answer?.ok === false)
|
|
87
|
+
log(
|
|
88
|
+
`task ${task.id} ended ${kind} but home did not take it: ${answer.error}`
|
|
89
|
+
);
|
|
90
|
+
void check();
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/** Ask home for this computer's authorized work and start what has a free folder. */
|
|
97
|
+
const check = async () => {
|
|
98
|
+
if (!agents.length) return;
|
|
99
|
+
if (asking) {
|
|
100
|
+
askAgain = true;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
asking = true;
|
|
104
|
+
try {
|
|
105
|
+
const answer = await callHome({
|
|
106
|
+
command: "board.machine.runnable",
|
|
107
|
+
agent: "user",
|
|
108
|
+
agents,
|
|
109
|
+
});
|
|
110
|
+
for (const task of answer?.ok ? answer.tasks : []) {
|
|
111
|
+
const slot = slotOf(task);
|
|
112
|
+
if (active.has(slot)) continue;
|
|
113
|
+
const taken = await callHome({
|
|
114
|
+
command: "board.machine.take",
|
|
115
|
+
agent: task.target_agent,
|
|
116
|
+
id: task.id,
|
|
117
|
+
expectedVersion: task.version,
|
|
118
|
+
});
|
|
119
|
+
if (!taken?.ok) continue;
|
|
120
|
+
active.set(slot, task.id);
|
|
121
|
+
start(taken.task, slot);
|
|
122
|
+
}
|
|
123
|
+
} finally {
|
|
124
|
+
asking = false;
|
|
125
|
+
if (askAgain) {
|
|
126
|
+
askAgain = false;
|
|
127
|
+
void check();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
return { check, running: () => [...active.values()] };
|
|
133
|
+
}
|