tryharness 0.7.32 → 0.7.34
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/README.md +48 -0
- package/lib/actorRoster.mjs +106 -0
- package/lib/boardAccess.mjs +328 -0
- package/lib/boardCommands.mjs +28 -0
- package/lib/boardFileCommands.mjs +228 -0
- package/lib/boardFiles.mjs +584 -0
- package/lib/boardProtocol.json +470 -0
- package/lib/boardProtocol.mjs +27 -0
- package/lib/boardThreads.mjs +153 -0
- package/lib/boardTools.mjs +202 -0
- package/lib/boardWatches.mjs +92 -0
- package/lib/bridge.mjs +272 -0
- package/lib/cards.mjs +5 -83
- package/lib/coordinator.mjs +13 -3
- package/lib/daemon.mjs +809 -5459
- package/lib/goalCommands.mjs +459 -0
- package/lib/harnessModel.mjs +166 -0
- package/lib/mcp-stdio.mjs +8 -2
- package/lib/models.mjs +107 -11
- package/lib/phones.mjs +8 -121
- package/lib/roomPage.mjs +44 -0
- package/lib/runAuthority.mjs +109 -0
- package/lib/runWorker.mjs +503 -0
- package/lib/sessionControl.mjs +788 -0
- package/lib/sessionIndex.mjs +245 -0
- package/lib/store.mjs +28 -9
- package/lib/swarm.mjs +28 -9
- package/lib/taskCommands.mjs +737 -0
- package/lib/taskRuns.mjs +196 -0
- package/lib/threadRuns.mjs +487 -0
- package/package.json +1 -1
package/lib/taskRuns.mjs
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { ensureDir, readJson, writeJsonAtomic } from "./files.mjs";
|
|
4
|
+
import { paths } from "./paths.mjs";
|
|
5
|
+
import { runAgentTask } from "./runner.mjs";
|
|
6
|
+
import { t3TurnUsage } from "./places.mjs";
|
|
7
|
+
import { runInT3 } from "./t3.mjs";
|
|
8
|
+
|
|
9
|
+
const TASK_LOG_PAGE = 64_000;
|
|
10
|
+
|
|
11
|
+
export function createTaskRuns({ t3Missed }) {
|
|
12
|
+
const taskRuns = new Map();
|
|
13
|
+
const validRunId = (runId) =>
|
|
14
|
+
typeof runId === "string" && /^[a-zA-Z0-9._-]{1,128}$/.test(runId);
|
|
15
|
+
const taskFiles = (runId) => ({
|
|
16
|
+
state: path.join(paths.runs, `${runId}.json`),
|
|
17
|
+
log: path.join(paths.runs, `${runId}.log`),
|
|
18
|
+
});
|
|
19
|
+
const persistTask = (task) => {
|
|
20
|
+
ensureDir(paths.runs);
|
|
21
|
+
writeJsonAtomic(taskFiles(task.runId).state, task);
|
|
22
|
+
};
|
|
23
|
+
const readTask = (runId, requestedOffset) => {
|
|
24
|
+
if (!validRunId(runId)) return null;
|
|
25
|
+
const files = taskFiles(runId);
|
|
26
|
+
const task = taskRuns.get(runId) || readJson(files.state, null);
|
|
27
|
+
if (!task) return null;
|
|
28
|
+
let raw = "";
|
|
29
|
+
try {
|
|
30
|
+
raw = fs.readFileSync(files.log, "utf8");
|
|
31
|
+
} catch {}
|
|
32
|
+
const offset = Math.max(
|
|
33
|
+
0,
|
|
34
|
+
Math.min(raw.length, Math.floor(Number(requestedOffset) || 0))
|
|
35
|
+
);
|
|
36
|
+
const chunk = raw.slice(offset, offset + TASK_LOG_PAGE);
|
|
37
|
+
return {
|
|
38
|
+
...task,
|
|
39
|
+
log: chunk,
|
|
40
|
+
offset,
|
|
41
|
+
nextOffset: offset + chunk.length,
|
|
42
|
+
logLength: raw.length,
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
// Runs left starting/running by a previous daemon process lost their agent process
|
|
46
|
+
// with it; their outcome is unknown, never assumed finished.
|
|
47
|
+
try {
|
|
48
|
+
for (const name of fs.readdirSync(paths.runs)) {
|
|
49
|
+
if (!name.endsWith(".json")) continue;
|
|
50
|
+
const task = readJson(path.join(paths.runs, name), null);
|
|
51
|
+
if (
|
|
52
|
+
task &&
|
|
53
|
+
validRunId(task.runId) &&
|
|
54
|
+
(task.status === "starting" || task.status === "running")
|
|
55
|
+
) {
|
|
56
|
+
persistTask({
|
|
57
|
+
...task,
|
|
58
|
+
status: "interrupted",
|
|
59
|
+
updatedAt: new Date().toISOString(),
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
} catch {}
|
|
64
|
+
const taskStatus = (runId) => {
|
|
65
|
+
const task = validRunId(runId)
|
|
66
|
+
? taskRuns.get(runId) || readJson(taskFiles(runId).state, null)
|
|
67
|
+
: null;
|
|
68
|
+
if (!task) return null;
|
|
69
|
+
const message =
|
|
70
|
+
typeof task.message === "string" ? task.message.slice(0, 100_000) : null;
|
|
71
|
+
return {
|
|
72
|
+
runId: task.runId,
|
|
73
|
+
status: task.status,
|
|
74
|
+
message,
|
|
75
|
+
sessionId: task.sessionId || null,
|
|
76
|
+
cwd: task.cwd || null,
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const startTask = (message, report) => {
|
|
81
|
+
if (!validRunId(message.runId)) {
|
|
82
|
+
report("failed", "Invalid task run id.", { failure: "start" });
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
// A run id is executed at most once, even if the same task arrives again.
|
|
86
|
+
const known = taskStatus(message.runId);
|
|
87
|
+
// The daemon stopped this run itself and knows it ended: the next attempt keeps its log and record.
|
|
88
|
+
if (known && !(message.again && known.status === "requeued")) {
|
|
89
|
+
if (known.status === "done" || known.status === "failed")
|
|
90
|
+
report(
|
|
91
|
+
known.status,
|
|
92
|
+
known.message || undefined,
|
|
93
|
+
known.failure ? { failure: known.failure } : undefined
|
|
94
|
+
);
|
|
95
|
+
else if (known.status === "running") report("accepted");
|
|
96
|
+
else
|
|
97
|
+
report(
|
|
98
|
+
"failed",
|
|
99
|
+
"This run was interrupted earlier and is not started again.",
|
|
100
|
+
{ failure: "exit" }
|
|
101
|
+
);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
ensureDir(paths.runs);
|
|
105
|
+
const files = taskFiles(message.runId);
|
|
106
|
+
if (!message.again)
|
|
107
|
+
try {
|
|
108
|
+
fs.writeFileSync(files.log, "", { mode: 0o600 });
|
|
109
|
+
} catch {}
|
|
110
|
+
const task = {
|
|
111
|
+
runId: message.runId,
|
|
112
|
+
agent: message.agent,
|
|
113
|
+
title: message.title,
|
|
114
|
+
cwd: message.cwd || null,
|
|
115
|
+
status: "starting",
|
|
116
|
+
sessionId: null,
|
|
117
|
+
startedAt: new Date().toISOString(),
|
|
118
|
+
updatedAt: new Date().toISOString(),
|
|
119
|
+
message: null,
|
|
120
|
+
};
|
|
121
|
+
taskRuns.set(message.runId, task);
|
|
122
|
+
persistTask(task);
|
|
123
|
+
const emit = (kind, text, extra) => {
|
|
124
|
+
if (kind === "accepted") task.status = "running";
|
|
125
|
+
if (kind === "session") {
|
|
126
|
+
task.sessionId = text || null;
|
|
127
|
+
if (extra?.cwd) task.cwd = extra.cwd;
|
|
128
|
+
}
|
|
129
|
+
if (kind === "done" || kind === "failed") {
|
|
130
|
+
task.status = kind;
|
|
131
|
+
task.finishedAt = new Date().toISOString();
|
|
132
|
+
task.message = text || null;
|
|
133
|
+
if (extra?.usage) task.usage = extra.usage;
|
|
134
|
+
if (extra?.failure) task.failure = extra.failure;
|
|
135
|
+
}
|
|
136
|
+
task.updatedAt = new Date().toISOString();
|
|
137
|
+
persistTask(task);
|
|
138
|
+
report(kind, text, extra);
|
|
139
|
+
};
|
|
140
|
+
const observe = (stream, raw) => {
|
|
141
|
+
try {
|
|
142
|
+
fs.appendFileSync(
|
|
143
|
+
files.log,
|
|
144
|
+
stream === "stderr" ? `\n[stderr] ${raw}` : raw,
|
|
145
|
+
{ mode: 0o600 }
|
|
146
|
+
);
|
|
147
|
+
} catch {}
|
|
148
|
+
};
|
|
149
|
+
if (message.t3)
|
|
150
|
+
runInT3(
|
|
151
|
+
{
|
|
152
|
+
runId: message.runId,
|
|
153
|
+
brief: message.brief,
|
|
154
|
+
cwd: message.cwd,
|
|
155
|
+
...message.t3,
|
|
156
|
+
usageOf: (threadId, runId) => t3TurnUsage(threadId, runId),
|
|
157
|
+
// T3 Code did not answer: the turn runs here, in T3's checkout for the thread, and the thread hears it.
|
|
158
|
+
fallback: (why) => {
|
|
159
|
+
const checkout =
|
|
160
|
+
message.t3.checkout &&
|
|
161
|
+
fs
|
|
162
|
+
.statSync(message.t3.checkout, { throwIfNoEntry: false })
|
|
163
|
+
?.isDirectory()
|
|
164
|
+
? message.t3.checkout
|
|
165
|
+
: message.cwd;
|
|
166
|
+
observe(
|
|
167
|
+
"stdout",
|
|
168
|
+
`[${why} This turn runs on this computer instead, in ${checkout}.]\n`
|
|
169
|
+
);
|
|
170
|
+
t3Missed(message.runId, why);
|
|
171
|
+
runAgentTask({ ...message, cwd: checkout }, emit, observe);
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
emit,
|
|
175
|
+
observe
|
|
176
|
+
);
|
|
177
|
+
else runAgentTask(message, emit, observe);
|
|
178
|
+
};
|
|
179
|
+
const requeue = (id, reason) => {
|
|
180
|
+
const record = taskRuns.get(id);
|
|
181
|
+
if (!record) return;
|
|
182
|
+
Object.assign(record, {
|
|
183
|
+
status: "requeued",
|
|
184
|
+
message: `stopped because ${reason}`,
|
|
185
|
+
});
|
|
186
|
+
persistTask(record);
|
|
187
|
+
};
|
|
188
|
+
return {
|
|
189
|
+
validRunId,
|
|
190
|
+
taskFiles,
|
|
191
|
+
readTask,
|
|
192
|
+
taskStatus,
|
|
193
|
+
startTask,
|
|
194
|
+
requeue,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { nameOf } from "./members.mjs";
|
|
5
|
+
import { setRunPick } from "./models.mjs";
|
|
6
|
+
import { t3Access, startT3SignIn, steerT3Run } from "./t3.mjs";
|
|
7
|
+
import { t3Model, t3Open, t3Project, t3Provider } from "./places.mjs";
|
|
8
|
+
|
|
9
|
+
/** Thread execution owns queued calls, fresh briefs, worktree settlement and T3 continuity. */
|
|
10
|
+
export function createThreadRuns({
|
|
11
|
+
store,
|
|
12
|
+
log,
|
|
13
|
+
installedAgents,
|
|
14
|
+
authority,
|
|
15
|
+
worktrees,
|
|
16
|
+
notifyBoard,
|
|
17
|
+
swarmBrief,
|
|
18
|
+
threadMayRun,
|
|
19
|
+
restrictable,
|
|
20
|
+
restrictedWait,
|
|
21
|
+
pauseGoal,
|
|
22
|
+
homeOf,
|
|
23
|
+
}) {
|
|
24
|
+
const { RUNNERS } = authority;
|
|
25
|
+
const inT3 = (thread, agent, review) => {
|
|
26
|
+
if (review || !t3Provider(agent) || !thread.goal.workspace) return false;
|
|
27
|
+
const tree = worktrees.get(thread.id);
|
|
28
|
+
if (tree?.state === "t3") return true;
|
|
29
|
+
// A task's room works in place, which T3 does in its project's own checkout; a thread carrying on a terminal
|
|
30
|
+
// session in place resumes that session here.
|
|
31
|
+
const movable =
|
|
32
|
+
!tree ||
|
|
33
|
+
(tree.state === "open" && fs.existsSync(tree.path)) ||
|
|
34
|
+
(tree.state === "in_place" &&
|
|
35
|
+
Boolean(store.meta(`task-room:${thread.id}`)));
|
|
36
|
+
return (
|
|
37
|
+
movable &&
|
|
38
|
+
homeOf(agent)?.kind === "t3" &&
|
|
39
|
+
!t3Gap(agent, thread.goal.workspace)
|
|
40
|
+
);
|
|
41
|
+
};
|
|
42
|
+
// Why an agent the person uses in T3 Code would not run there in a folder; null when it would, or when its home is not T3.
|
|
43
|
+
const t3Gap = (agent, folder) => {
|
|
44
|
+
if (!t3Provider(agent) || homeOf(agent)?.kind !== "t3") return null;
|
|
45
|
+
if (t3Access()?.state !== "send")
|
|
46
|
+
return "Harness isn't approved to send into T3 Code";
|
|
47
|
+
if (!t3Open()) return "T3 Code isn't open";
|
|
48
|
+
if (!t3Project(folder))
|
|
49
|
+
return `T3 Code has no project for ${path.basename(folder)}`;
|
|
50
|
+
if (!t3Model(agent)) return `${nameOf(agent)} hasn't run in T3 Code yet`;
|
|
51
|
+
return null;
|
|
52
|
+
};
|
|
53
|
+
// Where an agent's turns in a folder run, for the composer: in T3 Code, or here, with why not T3 when it is the agent's home.
|
|
54
|
+
const runsIn = (agent, folder) => {
|
|
55
|
+
const gap = t3Gap(agent, folder);
|
|
56
|
+
return homeOf(agent)?.kind === "t3" && !gap
|
|
57
|
+
? { in: "t3", why: null }
|
|
58
|
+
: { in: "here", why: gap };
|
|
59
|
+
};
|
|
60
|
+
// A task's own run (its room's first job) goes where its room's turns go: into T3 Code when the person uses the agent
|
|
61
|
+
// there. Not when it would run restricted (work another agent asked for on a goal's authority): only this computer
|
|
62
|
+
// holds a run to that.
|
|
63
|
+
const taskInT3 = (task, agent) => {
|
|
64
|
+
const room = task.parent_id ? store.getTask(task.parent_id) : null;
|
|
65
|
+
if (
|
|
66
|
+
task.kind !== "task" ||
|
|
67
|
+
room?.kind !== "goal" ||
|
|
68
|
+
store.meta(`task-room:${room.id}`) !== task.id ||
|
|
69
|
+
!task.run_workspace
|
|
70
|
+
)
|
|
71
|
+
return false;
|
|
72
|
+
const restricted =
|
|
73
|
+
task.trust !== "user" &&
|
|
74
|
+
task.run_authorized_by === "goal" &&
|
|
75
|
+
store.meta(`person-wake:${task.id}`) !== "1";
|
|
76
|
+
return (
|
|
77
|
+
!restricted && inT3(room, agent, store.meta(`review:${task.id}`) === "1")
|
|
78
|
+
);
|
|
79
|
+
};
|
|
80
|
+
// A turn T3 carries out (t3.mjs runInT3): into the agent's T3 thread in this thread, else a new one in T3's project
|
|
81
|
+
// for the folder, on the thread's T3 checkout. The thread works in T3 from its first launch.
|
|
82
|
+
// Every turn runs there on the model and effort the run was given (what the buddy shows), whatever the thread last ran.
|
|
83
|
+
const t3Turn = (task, agent, { model, effort }) => {
|
|
84
|
+
const thread = store.getTask(task.parent_id);
|
|
85
|
+
const key = `t3-thread:${thread.id}:${agent}`;
|
|
86
|
+
const project = t3Project(thread.goal.workspace);
|
|
87
|
+
return {
|
|
88
|
+
threadId: store.meta(key) || null,
|
|
89
|
+
title: thread.objective.slice(0, 120),
|
|
90
|
+
pick: { instanceId: t3Provider(agent), model, effort },
|
|
91
|
+
launch: {
|
|
92
|
+
...(project ? { projectId: project.id } : {}),
|
|
93
|
+
workspaceStrategy: worktrees.t3Workspace(thread),
|
|
94
|
+
},
|
|
95
|
+
onThread: (threadId) => {
|
|
96
|
+
store.meta(key, threadId);
|
|
97
|
+
worktrees.inT3(thread.id);
|
|
98
|
+
},
|
|
99
|
+
onWorktree: (where, branch) => worktrees.inT3(thread.id, where, branch),
|
|
100
|
+
// Where the turn runs if T3 Code does not answer: T3's own checkout for the thread, so the work stays where T3
|
|
101
|
+
// picks it up again.
|
|
102
|
+
checkout:
|
|
103
|
+
worktrees.get(thread.id)?.state === "t3"
|
|
104
|
+
? (worktrees.get(thread.id)?.path ?? null)
|
|
105
|
+
: null,
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
// A turn that ran outside T3 Code because T3 did not answer: the thread hears it, and the agent's next turn in T3 is
|
|
109
|
+
// told what its T3 thread did not see, so the thread goes back to T3 with nothing lost (owner, October 15).
|
|
110
|
+
const ranOutsideT3 = new Set();
|
|
111
|
+
const t3Missed = (runId, why) => {
|
|
112
|
+
ranOutsideT3.add(runId);
|
|
113
|
+
const task = store.getTask(runId);
|
|
114
|
+
if (!task?.parent_id) return;
|
|
115
|
+
const agent = task.target_agent;
|
|
116
|
+
store.meta(
|
|
117
|
+
`t3-missed:${task.parent_id}:${agent}`,
|
|
118
|
+
JSON.stringify({ at: new Date().toISOString(), why, runId })
|
|
119
|
+
);
|
|
120
|
+
store.roomPost({
|
|
121
|
+
goalId: task.parent_id,
|
|
122
|
+
author: "harness",
|
|
123
|
+
kind: "note",
|
|
124
|
+
text: `${nameOf(agent)}'s turn ran on this computer, in its T3 Code checkout: ${why} Its next turn goes back to T3 Code, which is told what it missed.`,
|
|
125
|
+
});
|
|
126
|
+
notifyBoard(store.getTask(task.parent_id));
|
|
127
|
+
};
|
|
128
|
+
// What the agent's T3 thread missed, for its next turn there.
|
|
129
|
+
const t3CatchUp = (goalId, agent) => {
|
|
130
|
+
let missed = null;
|
|
131
|
+
try {
|
|
132
|
+
missed = JSON.parse(store.meta(`t3-missed:${goalId}:${agent}`) || "null");
|
|
133
|
+
} catch {}
|
|
134
|
+
return missed
|
|
135
|
+
? `Catch-up: at ${missed.at} T3 Code did not answer, so your last turn ran outside this T3 thread, on this computer in the same checkout; its result is in the thread (read_room). This T3 thread did not see that turn: look at the checkout (git status, the files it changed) before you go on.`
|
|
136
|
+
: "";
|
|
137
|
+
};
|
|
138
|
+
// Wake one teammate. A coding agent here gets a run in the goal's folder that resumes its last session on this goal;
|
|
139
|
+
// Harness and agents that cannot be started here learn of it through the board change and their own reads.
|
|
140
|
+
const pendingWakes = {
|
|
141
|
+
get(key) {
|
|
142
|
+
try {
|
|
143
|
+
const value = JSON.parse(store.meta("pending-wake:" + key) || "null");
|
|
144
|
+
return value &&
|
|
145
|
+
typeof value.reason === "string" &&
|
|
146
|
+
typeof value.asker === "string" &&
|
|
147
|
+
typeof value.go === "boolean" &&
|
|
148
|
+
typeof value.at === "string"
|
|
149
|
+
? value
|
|
150
|
+
: null;
|
|
151
|
+
} catch {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
set(key, value) {
|
|
156
|
+
store.meta("pending-wake:" + key, JSON.stringify(value));
|
|
157
|
+
},
|
|
158
|
+
delete(key) {
|
|
159
|
+
store.meta("pending-wake:" + key, "");
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
const discard = (goalId) => {
|
|
163
|
+
for (const agent of RUNNERS) pendingWakes.delete(goalId + ":" + agent);
|
|
164
|
+
};
|
|
165
|
+
// go: the person's go for this turn (personsGo), their own post naming this agent or one of their agents here asking;
|
|
166
|
+
// any other wake is work another agent asked for and runs restricted or waits (docs/member-runs.md).
|
|
167
|
+
// asker: who asked for this turn, the person or an agent, named on the card that waits for the person.
|
|
168
|
+
// pick: the model and effort the post named for this turn (models.mjs).
|
|
169
|
+
// The person's words to an agent already at work in the thread reach its turn when T3 Code carries it: T3 steers them
|
|
170
|
+
// in at the agent's next step, and the wake kept for them is dropped. A run started on this computer has no way in,
|
|
171
|
+
// so the agent reads them when its turn ends (the pending wake). Either way the thread says which happened.
|
|
172
|
+
const steerRunning = (goalId, agent, words, pending) => {
|
|
173
|
+
const running = store
|
|
174
|
+
.threadRuns(goalId)
|
|
175
|
+
.find((run) => run.target_agent === agent && run.status === "running");
|
|
176
|
+
if (!running) return;
|
|
177
|
+
const who = nameOf(agent);
|
|
178
|
+
const note = (text) => {
|
|
179
|
+
store.roomPost({ goalId, author: "harness", kind: "note", text });
|
|
180
|
+
notifyBoard(store.getTask(goalId));
|
|
181
|
+
};
|
|
182
|
+
if (store.meta(`t3-run:${running.id}`) !== "1") {
|
|
183
|
+
note(
|
|
184
|
+
`${who} reads your message when its current turn ends: a run started on this computer cannot take messages mid-turn.`
|
|
185
|
+
);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const thread = store.getTask(goalId);
|
|
189
|
+
void steerT3Run(
|
|
190
|
+
running.id,
|
|
191
|
+
`The person just wrote in the thread "${thread.objective.slice(0, 200)}" while you work:\n\n${words}\n\nTake it into account in what you are doing now.`
|
|
192
|
+
).then((delivery) => {
|
|
193
|
+
if (!delivery) {
|
|
194
|
+
note(`${who} reads your message when its current turn ends.`);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (pendingWakes.get(`${goalId}:${agent}`)?.id === pending.id)
|
|
198
|
+
pendingWakes.delete(`${goalId}:${agent}`);
|
|
199
|
+
note(
|
|
200
|
+
delivery === "restarted"
|
|
201
|
+
? `${who} restarted its turn to take your message.`
|
|
202
|
+
: `${who} has your message and reads it at its next step.`
|
|
203
|
+
);
|
|
204
|
+
});
|
|
205
|
+
};
|
|
206
|
+
// The person's go reaches a turn another agent's ask left waiting for their Start, so it starts now rather than
|
|
207
|
+
// after a turn that would never come (October 17: Grok Bot's mention held Claude Code in the person's own folder, and
|
|
208
|
+
// their @claude-code queued behind it). The pending call stays, so the turn's brief carries the person's words.
|
|
209
|
+
const startHeldWake = (goalId, agent) => {
|
|
210
|
+
const wake = store.getTask(store.openWake(goalId, agent));
|
|
211
|
+
if (wake?.status !== "open" || wake.run_authorized_at) return;
|
|
212
|
+
if (!threadMayRun(store.getTask(goalId))) return;
|
|
213
|
+
store.meta(`person-wake:${wake.id}`, "1");
|
|
214
|
+
if (store.authorizeGoalRun(wake.id)) notifyBoard(store.getTask(wake.id));
|
|
215
|
+
};
|
|
216
|
+
const wakeAgent = (
|
|
217
|
+
goalId,
|
|
218
|
+
agent,
|
|
219
|
+
reason,
|
|
220
|
+
{
|
|
221
|
+
review = false,
|
|
222
|
+
go = false,
|
|
223
|
+
asker = "harness",
|
|
224
|
+
pick = null,
|
|
225
|
+
words = null,
|
|
226
|
+
recovering = false,
|
|
227
|
+
} = {}
|
|
228
|
+
) => {
|
|
229
|
+
const task = store.getTask(goalId);
|
|
230
|
+
if (
|
|
231
|
+
!task ||
|
|
232
|
+
task.status !== "open" ||
|
|
233
|
+
!RUNNERS.has(agent) ||
|
|
234
|
+
!installedAgents.includes(agent)
|
|
235
|
+
)
|
|
236
|
+
return;
|
|
237
|
+
const goal = task.goal;
|
|
238
|
+
if (!goal.workspace) {
|
|
239
|
+
if (!store.meta(`goal-folder-note:${goalId}:${agent}`)) {
|
|
240
|
+
store.meta(`goal-folder-note:${goalId}:${agent}`, "1");
|
|
241
|
+
store.roomPost({
|
|
242
|
+
goalId,
|
|
243
|
+
author: "harness",
|
|
244
|
+
kind: "note",
|
|
245
|
+
text: `${nameOf(agent)} cannot start: this thread has no folder to work in. Choose one for the thread in Harness, or: npx tryharness goal folder ${goalId} <dir>`,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (store.openWake(goalId, agent)) {
|
|
251
|
+
const pending = {
|
|
252
|
+
id: randomUUID(),
|
|
253
|
+
reason,
|
|
254
|
+
asker,
|
|
255
|
+
go: go || Boolean(pendingWakes.get(`${goalId}:${agent}`)?.go),
|
|
256
|
+
pick: pick ?? pendingWakes.get(`${goalId}:${agent}`)?.pick ?? null,
|
|
257
|
+
at: new Date().toISOString(),
|
|
258
|
+
};
|
|
259
|
+
pendingWakes.set(`${goalId}:${agent}`, pending);
|
|
260
|
+
if (pending.go) startHeldWake(goalId, agent);
|
|
261
|
+
if (asker === "user" && words)
|
|
262
|
+
steerRunning(goalId, agent, words, pending);
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
// The limit guards against agents waking each other in a loop, so it counts from the thread's last check-in: a thread
|
|
266
|
+
// that checks in for days is not paused by the wakes its check-ins brought.
|
|
267
|
+
const counting = store.meta(`check-in-at:${goalId}`) || null;
|
|
268
|
+
if (store.wakeCount(goalId, agent, counting) >= goal.max_wakes) {
|
|
269
|
+
pauseGoal(goalId, `${agent} was woken ${goal.max_wakes} times`);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
const brief = swarmBrief(task, agent, reason, { review });
|
|
273
|
+
// A coding thread in a repository works in its own worktree, so threads never share a checkout; one whose agents the
|
|
274
|
+
// person uses in T3 Code works in T3 (inT3), where T3 makes and keeps the checkout.
|
|
275
|
+
const t3 = inT3(task, agent, review);
|
|
276
|
+
// The person uses this agent in T3 Code but this turn runs here: the thread says why, once for each reason.
|
|
277
|
+
const gap = t3 || review ? null : t3Gap(agent, goal.workspace);
|
|
278
|
+
if (gap && !store.meta(`t3-gap-note:${goalId}:${agent}:${gap}`)) {
|
|
279
|
+
store.meta(`t3-gap-note:${goalId}:${agent}:${gap}`, "1");
|
|
280
|
+
store.roomPost({
|
|
281
|
+
goalId,
|
|
282
|
+
author: "harness",
|
|
283
|
+
kind: "note",
|
|
284
|
+
text: `${nameOf(agent)} is working on this computer, not in T3 Code: ${gap}.`,
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
let folder = goal.workspace;
|
|
288
|
+
if (!t3)
|
|
289
|
+
try {
|
|
290
|
+
folder = worktrees.folderFor(task);
|
|
291
|
+
} catch (error) {
|
|
292
|
+
store.roomPost({
|
|
293
|
+
goalId,
|
|
294
|
+
author: "harness",
|
|
295
|
+
kind: "note",
|
|
296
|
+
text: `${agent} cannot start: ${error.message}`,
|
|
297
|
+
});
|
|
298
|
+
notifyBoard(store.getTask(goalId));
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
// Whose words the turn carries: whoever wrote the thread when an agent did, then the agent that asked. The person's
|
|
302
|
+
// own ask in their own thread names no one; Harness posts every wake.
|
|
303
|
+
const writers = task.trust === "agent" ? task.origin_chain : [];
|
|
304
|
+
const chain =
|
|
305
|
+
asker === "user" || writers.at(-1) === asker
|
|
306
|
+
? writers
|
|
307
|
+
: [...writers, asker];
|
|
308
|
+
const wake = store.postTask({
|
|
309
|
+
objective: `Room: ${task.objective}`.slice(0, 500),
|
|
310
|
+
brief,
|
|
311
|
+
originAgent: "harness",
|
|
312
|
+
provenance: { trust: "agent", chain: chain.slice(-16) },
|
|
313
|
+
targetAgent: agent,
|
|
314
|
+
parentId: goalId,
|
|
315
|
+
runWorkspace: folder,
|
|
316
|
+
kind: "wake",
|
|
317
|
+
});
|
|
318
|
+
if (t3) store.meta(`t3-run:${wake.id}`, "1");
|
|
319
|
+
store.meta(`wake-reason:${wake.id}`, JSON.stringify({ reason, review }));
|
|
320
|
+
if (pick) setRunPick(store, wake.id, pick, asker);
|
|
321
|
+
// A review runs with the agent's own read-only switch (runner.mjs); only Claude Code and Codex have one.
|
|
322
|
+
if (review) store.meta(`review:${wake.id}`, "1");
|
|
323
|
+
if (!recovering && threadMayRun(task)) {
|
|
324
|
+
if (go) {
|
|
325
|
+
store.meta(`person-wake:${wake.id}`, "1");
|
|
326
|
+
store.authorizeGoalRun(wake.id);
|
|
327
|
+
} else if (restrictable(agent, folder)) store.authorizeGoalRun(wake.id);
|
|
328
|
+
else if (!store.meta(`restricted-note:${goalId}:${agent}`)) {
|
|
329
|
+
store.meta(`restricted-note:${goalId}:${agent}`, "1");
|
|
330
|
+
store.roomPost({
|
|
331
|
+
goalId,
|
|
332
|
+
author: "harness",
|
|
333
|
+
kind: "note",
|
|
334
|
+
text: restrictedWait(agent),
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
notifyBoard(store.getTask(wake.id));
|
|
339
|
+
return wake.id;
|
|
340
|
+
};
|
|
341
|
+
// A wake's brief as of its start, not of when it was queued: a turn that waited for the folder reads the thread as it
|
|
342
|
+
// is now, with a call that came while it waited (October 17: Codex, queued at 07:29, started at 07:39 on a brief
|
|
343
|
+
// without Claude Code's 07:38 handoff, and settleWake dropped the handoff's own wake as already read).
|
|
344
|
+
const wakeBrief = (wake) => {
|
|
345
|
+
const thread = store.getTask(wake.parent_id);
|
|
346
|
+
let woken = null;
|
|
347
|
+
try {
|
|
348
|
+
woken = JSON.parse(store.meta(`wake-reason:${wake.id}`) || "null");
|
|
349
|
+
} catch {}
|
|
350
|
+
if (!thread || !woken) return wake.brief;
|
|
351
|
+
const later = pendingWakes.get(`${thread.id}:${wake.target_agent}`);
|
|
352
|
+
const reason =
|
|
353
|
+
later && later.at > wake.created_at
|
|
354
|
+
? `${woken.reason}\n\nSince then: ${later.reason}`
|
|
355
|
+
: woken.reason;
|
|
356
|
+
return swarmBrief(thread, wake.target_agent, reason, {
|
|
357
|
+
review: woken.review,
|
|
358
|
+
});
|
|
359
|
+
};
|
|
360
|
+
const notePeerOverlap = (goalId) => {
|
|
361
|
+
const mine = worktrees.get(goalId);
|
|
362
|
+
if (mine?.state !== "open") return;
|
|
363
|
+
const files = new Set(
|
|
364
|
+
(worktrees.diff(goalId).files || []).map((entry) => entry.file)
|
|
365
|
+
);
|
|
366
|
+
if (!files.size) return;
|
|
367
|
+
for (const other of store.goals({ all: false })) {
|
|
368
|
+
if (other.id === goalId || other.status !== "open") continue;
|
|
369
|
+
const tree = worktrees.get(other.id);
|
|
370
|
+
if (tree?.state !== "open" || tree.repo !== mine.repo) continue;
|
|
371
|
+
const shared = (worktrees.diff(other.id).files || [])
|
|
372
|
+
.map((entry) => entry.file)
|
|
373
|
+
.filter((file) => files.has(file))
|
|
374
|
+
.sort();
|
|
375
|
+
const key = `overlap:${[goalId, other.id].sort().join(":")}:${shared.join(",")}`;
|
|
376
|
+
if (!shared.length || store.meta(key)) continue;
|
|
377
|
+
store.meta(key, "1");
|
|
378
|
+
const named = `${shared.slice(0, 5).join(", ")}${shared.length > 5 ? ` and ${shared.length - 5} more` : ""}`;
|
|
379
|
+
for (const [here, there] of [
|
|
380
|
+
[goalId, other],
|
|
381
|
+
[other.id, store.getTask(goalId)],
|
|
382
|
+
]) {
|
|
383
|
+
store.roomPost({
|
|
384
|
+
goalId: here,
|
|
385
|
+
author: "harness",
|
|
386
|
+
kind: "note",
|
|
387
|
+
text: `Overlap: the thread "${there.objective.slice(0, 100)}" is also changing ${named}. Merging both will need care; say so if one should wait.`,
|
|
388
|
+
});
|
|
389
|
+
notifyBoard(store.getTask(here));
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
// The turn's changes are committed in its thread's copy as soon as it ends, before another run can start there.
|
|
394
|
+
const commitWake = (task) =>
|
|
395
|
+
worktrees.commitTurn(
|
|
396
|
+
task.parent_id,
|
|
397
|
+
task.id,
|
|
398
|
+
`${task.target_agent}: ${task.objective.replace(/^Room: /, "")}`
|
|
399
|
+
);
|
|
400
|
+
const settleWake = (task, kind, text, startedAt) => {
|
|
401
|
+
const goalId = task.parent_id;
|
|
402
|
+
try {
|
|
403
|
+
notePeerOverlap(goalId);
|
|
404
|
+
} catch (error) {
|
|
405
|
+
log(`overlap check for ${goalId}: ${error.message}`);
|
|
406
|
+
}
|
|
407
|
+
// A run the person stopped says nothing more: the thread already says it was stopped.
|
|
408
|
+
const stopped =
|
|
409
|
+
kind === "failed" && store.getTask(goalId)?.status !== "open";
|
|
410
|
+
if (
|
|
411
|
+
!stopped &&
|
|
412
|
+
!store.postsBy(goalId, task.target_agent, startedAt) &&
|
|
413
|
+
text
|
|
414
|
+
)
|
|
415
|
+
store.roomPost({
|
|
416
|
+
goalId,
|
|
417
|
+
author: task.target_agent,
|
|
418
|
+
kind: kind === "done" ? "post" : "failed",
|
|
419
|
+
text: kind === "done" ? text : `run failed: ${text}`,
|
|
420
|
+
});
|
|
421
|
+
const key = `${goalId}:${task.target_agent}`;
|
|
422
|
+
const pending = pendingWakes.get(key);
|
|
423
|
+
// A call that came before this run started was in the thread the run read: waking it again repeats its work
|
|
424
|
+
// (October 15, Codex woken a second time for an animatic it had just made). Only a later call wakes it again.
|
|
425
|
+
if (pending && startedAt && pending.at <= startedAt) {
|
|
426
|
+
pendingWakes.delete(key);
|
|
427
|
+
} else if (pending) {
|
|
428
|
+
const next = wakeAgent(goalId, task.target_agent, pending.reason, {
|
|
429
|
+
go: pending.go,
|
|
430
|
+
asker: pending.asker,
|
|
431
|
+
pick: pending.pick,
|
|
432
|
+
});
|
|
433
|
+
// Keep the accepted call durable until its replacement wake exists.
|
|
434
|
+
if (next) pendingWakes.delete(key);
|
|
435
|
+
}
|
|
436
|
+
notifyBoard(store.getTask(goalId));
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
const recover = () => {
|
|
440
|
+
for (const goal of store.goals()) {
|
|
441
|
+
if (goal.status !== "open") continue;
|
|
442
|
+
for (const agent of RUNNERS) {
|
|
443
|
+
const key = goal.id + ":" + agent;
|
|
444
|
+
const pending = pendingWakes.get(key);
|
|
445
|
+
if (!pending) continue;
|
|
446
|
+
// A waiting turn never started, so nothing of it is unknown: the person's go still starts it.
|
|
447
|
+
if (store.openWake(goal.id, agent)) {
|
|
448
|
+
if (pending.go) startHeldWake(goal.id, agent);
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
// The previous turn's effects are unknown. Preserve the new call, but let the person inspect before starting it.
|
|
452
|
+
const wakeId = wakeAgent(goal.id, agent, pending.reason, {
|
|
453
|
+
asker: pending.asker,
|
|
454
|
+
pick: pending.pick,
|
|
455
|
+
recovering: true,
|
|
456
|
+
});
|
|
457
|
+
if (!wakeId) continue;
|
|
458
|
+
pendingWakes.delete(key);
|
|
459
|
+
store.roomPost({
|
|
460
|
+
goalId: goal.id,
|
|
461
|
+
author: "harness",
|
|
462
|
+
kind: "note",
|
|
463
|
+
text:
|
|
464
|
+
nameOf(agent) +
|
|
465
|
+
"'s later call survived the restart and waits for your Start. Its interrupted turn's outcome is unknown; check the workspace before continuing.",
|
|
466
|
+
});
|
|
467
|
+
notifyBoard(store.getTask(goal.id));
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
return {
|
|
472
|
+
wake: wakeAgent,
|
|
473
|
+
discard,
|
|
474
|
+
recover,
|
|
475
|
+
where: runsIn,
|
|
476
|
+
worker: {
|
|
477
|
+
commitWake,
|
|
478
|
+
settleWake,
|
|
479
|
+
wakeBrief,
|
|
480
|
+
ranOutsideT3,
|
|
481
|
+
t3CatchUp,
|
|
482
|
+
t3Turn,
|
|
483
|
+
taskInT3,
|
|
484
|
+
},
|
|
485
|
+
missed: t3Missed,
|
|
486
|
+
};
|
|
487
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tryharness",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.34",
|
|
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"
|