tryharness 0.7.53 → 0.7.57
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 +23 -19
- package/assets/dsh-plugin.mjs +72 -0
- package/assets/opencode-plugin.js +4 -3
- package/cli.mjs +484 -90
- package/lib/activity.mjs +10 -10
- package/lib/adapters.mjs +173 -222
- package/lib/agentBinaries.mjs +71 -0
- package/lib/agentRuntimeConfig.mjs +1 -1
- package/lib/agentRuntimes.mjs +34 -18
- package/lib/agentSignIn.mjs +456 -0
- package/lib/answers.mjs +5 -5
- package/lib/archetypes.d.ts +64 -0
- package/lib/archetypes.js +989 -0
- package/lib/asks.mjs +4 -4
- package/lib/audit.mjs +666 -0
- package/lib/backupSetup.mjs +143 -0
- package/lib/boardAccess.mjs +24 -11
- package/lib/boardBackup.mjs +283 -0
- package/lib/boardFileCommands.mjs +6 -6
- package/lib/boardFiles.mjs +13 -12
- package/lib/boardMirror.mjs +124 -25
- package/lib/boardProtocol.json +88 -2
- package/lib/boardWaits.mjs +101 -0
- package/lib/book.mjs +659 -0
- package/lib/bridge.mjs +6 -6
- package/lib/cards.mjs +138 -28
- package/lib/cloudAgents.mjs +216 -0
- package/lib/cloudAi.mjs +1312 -0
- package/lib/cloudAssistants.mjs +242 -0
- package/lib/cloudBoot.mjs +119 -0
- package/lib/cloudSave.mjs +197 -0
- package/lib/cloudSsh.mjs +178 -0
- package/lib/commands.mjs +7 -2
- package/lib/context.mjs +19 -5
- package/lib/contractGate.mjs +399 -0
- package/lib/control-client.mjs +9 -1
- package/lib/controlSocket.mjs +86 -0
- package/lib/coordinator.mjs +252 -144
- package/lib/daemon.mjs +1351 -2767
- package/lib/desk.mjs +240 -0
- package/lib/desktop.mjs +178 -74
- package/lib/directLink.mjs +16 -5
- package/lib/environment.mjs +2 -1
- package/lib/environments.mjs +247 -9
- package/lib/firewall.mjs +9 -9
- package/lib/folders.mjs +20 -16
- package/lib/frames.mjs +2 -3
- package/lib/gitHost.mjs +174 -0
- package/lib/goalCommands.mjs +25 -7
- package/lib/goals.mjs +7 -7
- package/lib/guestRequests.mjs +94 -0
- package/lib/handoffs.mjs +8 -7
- package/lib/handover.mjs +4 -4
- package/lib/harnessModel.mjs +6 -6
- package/lib/harnessOffers.mjs +266 -0
- package/lib/hookDialects.mjs +298 -0
- package/lib/inference.mjs +26 -8
- package/lib/jobAnswers.mjs +190 -0
- package/lib/landed.mjs +887 -0
- package/lib/lane.mjs +42 -11
- package/lib/lessons.mjs +16 -16
- package/lib/library.mjs +3 -3
- package/lib/lineup.mjs +3 -0
- package/lib/linkRelay.mjs +574 -0
- package/lib/live.mjs +284 -13
- package/lib/localAgentAsk.mjs +74 -0
- package/lib/localStack.mjs +536 -0
- package/lib/localmodels.mjs +6 -4
- package/lib/loopBackstop.mjs +4 -4
- package/lib/machineIdentity.mjs +9 -1
- package/lib/machineRoutes.mjs +181 -2
- package/lib/machineRuns.mjs +76 -1
- package/lib/machineSeal.mjs +39 -12
- package/lib/mcp-stdio.mjs +163 -27
- package/lib/mcpRelay.mjs +1 -1
- package/lib/members.json +29 -2
- package/lib/members.mjs +1 -1
- package/lib/models.mjs +85 -31
- package/lib/museSession.mjs +21 -13
- package/lib/nativeHost.mjs +36 -8
- package/lib/neovim.mjs +126 -0
- package/lib/network.mjs +50 -5
- package/lib/notify.mjs +7 -5
- package/lib/nudge.mjs +50 -19
- package/lib/ocr.mjs +9 -9
- package/lib/onboard.mjs +26 -20
- package/lib/oneLine.mjs +7 -7
- package/lib/ownMachines.mjs +1223 -140
- package/lib/pairing.mjs +60 -16
- package/lib/paths.mjs +19 -1
- package/lib/peerProcess.mjs +124 -0
- package/lib/personModel.mjs +89 -0
- package/lib/personNotices.mjs +151 -0
- package/lib/phones.mjs +538 -31
- package/lib/placeCommands.mjs +4 -4
- package/lib/places.mjs +400 -12
- package/lib/profile.mjs +67 -14
- package/lib/projectGit.mjs +158 -14
- package/lib/projectPieces.mjs +902 -0
- package/lib/projectSettings.mjs +7 -4
- package/lib/projects.mjs +384 -62
- package/lib/provenance.mjs +5 -1
- package/lib/proxyLinks.mjs +267 -0
- package/lib/reactions.mjs +7 -5
- package/lib/readiness.mjs +145 -23
- package/lib/record.mjs +128 -0
- package/lib/redact.mjs +4 -4
- package/lib/relayLimits.mjs +62 -0
- package/lib/release.mjs +170 -0
- package/lib/releaseKeys.json +6 -0
- package/lib/repoLinks.mjs +4 -4
- package/lib/roomChannels.mjs +24 -0
- package/lib/roomPage.mjs +1 -1
- package/lib/runAuthority.mjs +101 -0
- package/lib/runBrief.mjs +19 -8
- package/lib/runWorker.mjs +31 -7
- package/lib/runner.mjs +223 -29
- package/lib/runtime-install.mjs +143 -9
- package/lib/schedules.mjs +2 -2
- package/lib/screens.mjs +1 -1
- package/lib/sealed.mjs +11 -2
- package/lib/sealedThreads.mjs +671 -0
- package/lib/secretPatterns.json +1 -1
- package/lib/secrets.mjs +4 -4
- package/lib/seen.mjs +6 -6
- package/lib/serverWords.mjs +175 -0
- package/lib/service.mjs +22 -12
- package/lib/sessionControl.mjs +378 -90
- package/lib/specialties.json +74 -0
- package/lib/specialties.mjs +477 -0
- package/lib/specialtyKeys.d.ts +3 -0
- package/lib/specialtyKeys.js +57 -0
- package/lib/specialtyNotice.mjs +147 -0
- package/lib/specialtySnapshot.mjs +138 -0
- package/lib/speech.mjs +5 -5
- package/lib/store.mjs +420 -36
- package/lib/stuck.mjs +1 -1
- package/lib/swarm.mjs +304 -37
- package/lib/swarmDevices.mjs +319 -0
- package/lib/swarmMemory.mjs +27 -25
- package/lib/t3.mjs +24 -15
- package/lib/taskCommands.mjs +80 -20
- package/lib/taskRooms.mjs +125 -0
- package/lib/taskRuns.mjs +45 -3
- package/lib/team.json +255 -148
- package/lib/team.mjs +554 -190
- package/lib/threadActions.mjs +315 -0
- package/lib/threadAuthority.mjs +101 -0
- package/lib/threadBriefs.mjs +178 -0
- package/lib/threadLine.mjs +116 -0
- package/lib/threadRuns.mjs +209 -50
- package/lib/threadView.mjs +20 -7
- package/lib/usageTotals.mjs +2 -3
- package/lib/vault.mjs +101 -10
- package/lib/vaultCloud.mjs +2 -2
- package/lib/vaultCommands.mjs +15 -6
- package/lib/vaultTools.js +2 -2
- package/lib/verdicts.mjs +328 -0
- package/lib/wakes.mjs +1 -1
- package/lib/watches.mjs +2 -2
- package/lib/windowAsks.mjs +13 -5
- package/lib/worktrees.mjs +157 -25
- package/npm-shrinkwrap.json +1093 -0
- package/package.json +12 -11
- package/release.json +220 -0
- package/lib/modelTiers.json +0 -1183
- package/lib/modelTiers.mjs +0 -133
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { speakerOf } from "./coordinator.mjs";
|
|
2
|
+
|
|
3
|
+
// The words of a task asked before they were kept apart from its brief.
|
|
4
|
+
const ASKED_WORDS =
|
|
5
|
+
/^Asked in the thread "[\s\S]*?" \(thread [0-9a-f-]+\):\n\n([\s\S]*?)\n\nRead the thread/;
|
|
6
|
+
|
|
7
|
+
// Posts that say something, as against the coordinator's notes, held posts and rounds.
|
|
8
|
+
const said = (post) => !["note", "held", "round"].includes(post.kind);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Harness's server takes a task given to Harness in a thread while no Harness window is open, only while this
|
|
12
|
+
* computer's coordinator runs on Harness: on the privacy setting the thread's words stay here and the task waits for a
|
|
13
|
+
* window. Harness holds the task meanwhile, so a window that opens does not take it too; what the server leaves for the
|
|
14
|
+
* browser goes back on the board, and the window takes it over when it next opens.
|
|
15
|
+
*
|
|
16
|
+
* `boardCommand()` and `asks()` are made after this module. `serverHas(feature)` says what the connected Harness
|
|
17
|
+
* supports; `lane` and `model` are the person's own model lane (lane.mjs) and their pick for Harness
|
|
18
|
+
* (harnessModel.mjs).
|
|
19
|
+
*/
|
|
20
|
+
export function createHarnessOffers({
|
|
21
|
+
store,
|
|
22
|
+
askHarness,
|
|
23
|
+
connected,
|
|
24
|
+
serverHas,
|
|
25
|
+
privacy,
|
|
26
|
+
boardCommand,
|
|
27
|
+
notifyBoard,
|
|
28
|
+
asks,
|
|
29
|
+
lane,
|
|
30
|
+
model,
|
|
31
|
+
files,
|
|
32
|
+
}) {
|
|
33
|
+
// Harness's tasks its server is working, by task, with their thread: an ask for Harness there goes into that turn.
|
|
34
|
+
const turns = new Map();
|
|
35
|
+
|
|
36
|
+
// What the person let a thread spend of their Harness credits, for a Harness that checks it.
|
|
37
|
+
const ceiling = (goalId) => {
|
|
38
|
+
const limit = Number(store.meta(`spend-ceiling:${goalId}`));
|
|
39
|
+
return serverHas("swarm_goal") &&
|
|
40
|
+
store.meta(`spend-ceiling:${goalId}`) &&
|
|
41
|
+
Number.isFinite(limit)
|
|
42
|
+
? { ceilingUsd: limit }
|
|
43
|
+
: {};
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// The newest 30 posts, as every agent's brief starts from the newest, and the person's first words when they fell out
|
|
47
|
+
// of them (in a long thread the roles they set were gone), each with its number for a Harness that cites posts by it.
|
|
48
|
+
const transcriptOf = (goal) => {
|
|
49
|
+
const latest = store
|
|
50
|
+
.recentRoomPosts(goal.id, { limit: 90 })
|
|
51
|
+
.posts.filter(said)
|
|
52
|
+
.slice(-30);
|
|
53
|
+
const opening = store
|
|
54
|
+
.roomPosts(goal.id, 0, 100)
|
|
55
|
+
.find((post) => said(post) && post.author === "user");
|
|
56
|
+
const numbered = serverHas("swarm_goal");
|
|
57
|
+
return [
|
|
58
|
+
...(opening && !latest.some((post) => post.seq === opening.seq)
|
|
59
|
+
? [opening]
|
|
60
|
+
: []),
|
|
61
|
+
...latest,
|
|
62
|
+
].map((post) => ({
|
|
63
|
+
...(numbered ? { seq: post.seq } : {}),
|
|
64
|
+
// The coordinator's posts read as the coordinator's, not as Harness's own.
|
|
65
|
+
author: speakerOf(store, goal, post),
|
|
66
|
+
kind: post.kind,
|
|
67
|
+
text: post.text.slice(0, 19_000),
|
|
68
|
+
}));
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const offer = (goal, taskId, asker, words) => {
|
|
72
|
+
// Not offered: the thread's waits say why; on the privacy setting no note says it.
|
|
73
|
+
const unoffered = privacy()
|
|
74
|
+
? "on the privacy setting the thread's words stay on this computer"
|
|
75
|
+
: !connected()
|
|
76
|
+
? "this computer is not connected to Harness right now"
|
|
77
|
+
: !serverHas("harness_task")
|
|
78
|
+
? "Harness's server cannot take thread tasks yet"
|
|
79
|
+
: null;
|
|
80
|
+
if (unoffered) {
|
|
81
|
+
store.meta(`harness-wait:${taskId}`, unoffered);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (
|
|
85
|
+
!boardCommand()({ command: "board.claim", agent: "harness", id: taskId })
|
|
86
|
+
?.ok
|
|
87
|
+
)
|
|
88
|
+
return;
|
|
89
|
+
const transcript = transcriptOf(goal);
|
|
90
|
+
// Back to the board for a window, as the person would put it back, with what the thread should hear.
|
|
91
|
+
const back = (post) => {
|
|
92
|
+
if (post) store.roomPost({ goalId: goal.id, author: "harness", ...post });
|
|
93
|
+
boardCommand()({ command: "board.release", agent: "user", id: taskId });
|
|
94
|
+
notifyBoard(store.getTask(goal.id));
|
|
95
|
+
};
|
|
96
|
+
// The thread hears why once for each reason, not once for each task: each @harness is its own task, and the note
|
|
97
|
+
// would come every time even when an open window took the task a second later. The thread's waits keep saying why
|
|
98
|
+
// for as long as a task waits.
|
|
99
|
+
const waits = (why) => {
|
|
100
|
+
store.meta(`harness-wait:${taskId}`, why);
|
|
101
|
+
const told = store.meta(`harness-wait-said:${goal.id}`) === why;
|
|
102
|
+
if (!told) store.meta(`harness-wait-said:${goal.id}`, why);
|
|
103
|
+
back(
|
|
104
|
+
told
|
|
105
|
+
? null
|
|
106
|
+
: {
|
|
107
|
+
kind: "note",
|
|
108
|
+
text: `This waits for your Harness window: ${why}.`,
|
|
109
|
+
}
|
|
110
|
+
);
|
|
111
|
+
};
|
|
112
|
+
const attached = serverHas("harness_files")
|
|
113
|
+
? {
|
|
114
|
+
files: files
|
|
115
|
+
.placeFiles(goal)
|
|
116
|
+
.filter((file) => file.present)
|
|
117
|
+
.slice(-50)
|
|
118
|
+
.map(files.harnessFileView),
|
|
119
|
+
}
|
|
120
|
+
: {};
|
|
121
|
+
// The person's own key or endpoint goes only when Harness's server says the task needs it, or with the model the
|
|
122
|
+
// person picked for Harness on it, and only if they let Harness Relay keep it; otherwise the thread says where to
|
|
123
|
+
// allow it.
|
|
124
|
+
const held = serverHas("harness_task_lane") ? lane.held() : null;
|
|
125
|
+
const picked = serverHas("harness_model") ? model.forTask() : null;
|
|
126
|
+
const ask = (withLane) =>
|
|
127
|
+
askHarness(
|
|
128
|
+
"harness_task",
|
|
129
|
+
{
|
|
130
|
+
taskId,
|
|
131
|
+
thread: { id: goal.id, title: goal.objective.slice(0, 500) },
|
|
132
|
+
asker,
|
|
133
|
+
words: String(words).slice(0, 20_000),
|
|
134
|
+
transcript,
|
|
135
|
+
...attached,
|
|
136
|
+
...ceiling(goal.id),
|
|
137
|
+
...(picked ? { model: picked.model } : {}),
|
|
138
|
+
},
|
|
139
|
+
null,
|
|
140
|
+
withLane
|
|
141
|
+
? {
|
|
142
|
+
lane: {
|
|
143
|
+
modelId: withLane.modelId,
|
|
144
|
+
credentialSource: withLane.credentialSource,
|
|
145
|
+
},
|
|
146
|
+
}
|
|
147
|
+
: null
|
|
148
|
+
).then((reply) =>
|
|
149
|
+
reply?.ok &&
|
|
150
|
+
!reply.taken &&
|
|
151
|
+
reply.why === "own_key" &&
|
|
152
|
+
held &&
|
|
153
|
+
!withLane
|
|
154
|
+
? ask(held)
|
|
155
|
+
: reply
|
|
156
|
+
);
|
|
157
|
+
// The asks its turn read are answered with it; whoever made them hears its answer as the task's asker does.
|
|
158
|
+
const turnEnded = (outcome) => {
|
|
159
|
+
turns.delete(taskId);
|
|
160
|
+
const read = outcome?.status === "done" ? (outcome.read ?? []) : [];
|
|
161
|
+
const askers = asks().turnEnded(goal.id, taskId, read);
|
|
162
|
+
if (askers.length)
|
|
163
|
+
store.meta(`harness-also-asked:${taskId}`, JSON.stringify(askers));
|
|
164
|
+
};
|
|
165
|
+
const settle = (outcome) => {
|
|
166
|
+
// The thread used what the person let it spend: they are asked once for each limit, and the task waits.
|
|
167
|
+
if (!outcome.taken && outcome.why === "ceiling") {
|
|
168
|
+
const limit = store.meta(`spend-ceiling:${goal.id}`);
|
|
169
|
+
const asked = store.meta(`spend-ceiling-asked:${goal.id}`) === limit;
|
|
170
|
+
store.meta(`spend-ceiling-asked:${goal.id}`, limit);
|
|
171
|
+
return back(
|
|
172
|
+
asked
|
|
173
|
+
? null
|
|
174
|
+
: {
|
|
175
|
+
kind: "question",
|
|
176
|
+
text: `Harness has reached what this thread may spend: ${outcome.detail}. Raise the limit to go on (npx tryharness swarm ceiling ${goal.id} <dollars>), or say if the swarm should go on without Harness's part.`,
|
|
177
|
+
}
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
if (!outcome.taken)
|
|
181
|
+
return outcome.why === "window_open"
|
|
182
|
+
? back(null)
|
|
183
|
+
: waits(
|
|
184
|
+
outcome.why === "own_key" && serverHas("harness_task_lane")
|
|
185
|
+
? `${outcome.detail}. To let Harness answer here while your window is closed, turn on "Harness Relay may use it" in Harness's settings, under Models`
|
|
186
|
+
: outcome.detail || "Harness could not take it here"
|
|
187
|
+
);
|
|
188
|
+
// Its words are Harness's server's, not the window's: they wake no one with the person's go (jobChanged).
|
|
189
|
+
if (outcome.status === "done")
|
|
190
|
+
store.meta(`harness-served:${taskId}`, "1");
|
|
191
|
+
if (outcome.status === "done")
|
|
192
|
+
return boardCommand()({
|
|
193
|
+
command: "board.report",
|
|
194
|
+
agent: "harness",
|
|
195
|
+
id: taskId,
|
|
196
|
+
status: "done",
|
|
197
|
+
result: outcome.text,
|
|
198
|
+
});
|
|
199
|
+
if (outcome.status === "window") {
|
|
200
|
+
// What is left needs the browser: offered to Harness's server again it would only be handed back.
|
|
201
|
+
store.meta(`harness-window:${taskId}`, "1");
|
|
202
|
+
return back({
|
|
203
|
+
kind: "post",
|
|
204
|
+
text: `${outcome.text}\n\nThe rest needs your browser: ${outcome.windowOpen ? "your Harness window is taking it over" : "Harness takes it over when your Harness window next opens"}.`,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
waits(
|
|
208
|
+
`Harness could not finish it here${outcome.text ? ` (${outcome.text.slice(0, 300)})` : ""}`
|
|
209
|
+
);
|
|
210
|
+
};
|
|
211
|
+
turns.set(taskId, goal.id);
|
|
212
|
+
ask(picked?.lane ?? null).then(
|
|
213
|
+
(reply) => {
|
|
214
|
+
// askHarness spreads Harness's answer into the reply.
|
|
215
|
+
const outcome = reply?.ok ? reply : null;
|
|
216
|
+
turnEnded(outcome);
|
|
217
|
+
if (!outcome)
|
|
218
|
+
return waits(
|
|
219
|
+
`Harness could not take it here (${reply?.error || "no answer"})`
|
|
220
|
+
);
|
|
221
|
+
settle(outcome);
|
|
222
|
+
},
|
|
223
|
+
(error) => {
|
|
224
|
+
turnEnded(null);
|
|
225
|
+
waits(`Harness could not take it here (${error.message})`);
|
|
226
|
+
}
|
|
227
|
+
);
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
/** A task just asked of Harness in a thread: its words are kept so it can be offered again, then it is offered. */
|
|
231
|
+
const offerAsk = (goal, taskId, asker, words) => {
|
|
232
|
+
store.meta(
|
|
233
|
+
`harness-ask:${taskId}`,
|
|
234
|
+
JSON.stringify({ asker, words: String(words) })
|
|
235
|
+
);
|
|
236
|
+
offer(goal, taskId, asker, words);
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
// Harness's parts left waiting for a window go back to Harness's server when what kept them may have changed: the
|
|
240
|
+
// person's next Harness turn ran on Harness credits, their Resident changed or their last Harness window closed (the
|
|
241
|
+
// server's harness_ready), or this computer reached Harness again. The server takes each one it now can; the rest
|
|
242
|
+
// keep waiting, saying nothing new. What the server already left for the browser stays for the window.
|
|
243
|
+
const reofferAll = () => {
|
|
244
|
+
for (const goal of store.goals()) {
|
|
245
|
+
if (goal.status !== "open") continue;
|
|
246
|
+
for (const run of store.threadRuns(goal.id)) {
|
|
247
|
+
if (run.target_agent !== "harness" || run.status !== "open") continue;
|
|
248
|
+
if (store.meta(`harness-window:${run.id}`)) continue;
|
|
249
|
+
const task = store.getTask(run.id);
|
|
250
|
+
let asked = null;
|
|
251
|
+
try {
|
|
252
|
+
asked = JSON.parse(store.meta(`harness-ask:${run.id}`) || "null");
|
|
253
|
+
} catch {}
|
|
254
|
+
const words = asked?.words ?? ASKED_WORDS.exec(task.brief || "")?.[1];
|
|
255
|
+
if (!words) continue;
|
|
256
|
+
offer(goal, run.id, asked?.asker ?? task.origin_agent, words);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
/** The task Harness's server is working in a thread now, if any. */
|
|
262
|
+
const turnOf = (threadId) =>
|
|
263
|
+
[...turns].find(([, thread]) => thread === threadId)?.[0] ?? null;
|
|
264
|
+
|
|
265
|
+
return { offerAsk, reofferAll, turnOf };
|
|
266
|
+
}
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { ensureDir, readJson } from "./files.mjs";
|
|
5
|
+
|
|
6
|
+
// How each agent runs session hooks: the file it reads them from, that file's shape, and what it does with a hook's
|
|
7
|
+
// output. Harness hooks three moments, by the names Claude Code gave them: a session starting (its board notice, where it
|
|
8
|
+
// runs), the person's prompt (what changed, and a message from Harness), and a turn ending (a message from Harness keeps
|
|
9
|
+
// it going). Setup writes each agent's file from here (installSessionHooks); nudge.mjs answers each agent in its own
|
|
10
|
+
// words (hookDialect). Every other setting and hook in a file is kept; setup replaces Harness's entries and uninstall
|
|
11
|
+
// takes only them out. Read from each agent's own docs and source, October 2026.
|
|
12
|
+
export const HOOK_EVENTS = ["SessionStart", "UserPromptSubmit", "Stop"];
|
|
13
|
+
|
|
14
|
+
const isHarnessHook = (entry) =>
|
|
15
|
+
JSON.stringify(entry).includes("/lib/nudge.mjs");
|
|
16
|
+
const objectOr = (value) =>
|
|
17
|
+
value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
18
|
+
const withOurs = (list, ours) => [
|
|
19
|
+
...(Array.isArray(list) ? list : []).filter((entry) => !isHarnessHook(entry)),
|
|
20
|
+
...(ours ? [ours] : []),
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
// Claude Code's shape: settings.hooks[event] holds [{ matcher?, hooks: [{ type: "command", command, timeout }] }].
|
|
24
|
+
const claudeShape = ({ start = {}, startHook = {} } = {}) => ({
|
|
25
|
+
write: (settings, commands) => {
|
|
26
|
+
const hooks = objectOr(settings.hooks);
|
|
27
|
+
const next = { ...hooks };
|
|
28
|
+
for (const event of HOOK_EVENTS) {
|
|
29
|
+
const hook = { type: "command", command: commands[event] };
|
|
30
|
+
next[event] = withOurs(
|
|
31
|
+
hooks[event],
|
|
32
|
+
event === "SessionStart"
|
|
33
|
+
? { ...start, hooks: [{ ...hook, ...startHook, timeout: 10 }] }
|
|
34
|
+
: { hooks: [{ ...hook, timeout: 10 }] }
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
return { ...settings, hooks: next };
|
|
38
|
+
},
|
|
39
|
+
erase: (settings) => {
|
|
40
|
+
if (!settings.hooks) return settings;
|
|
41
|
+
const hooks = { ...objectOr(settings.hooks) };
|
|
42
|
+
for (const event of HOOK_EVENTS)
|
|
43
|
+
if (Array.isArray(hooks[event])) hooks[event] = withOurs(hooks[event]);
|
|
44
|
+
return { ...settings, hooks };
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// A versioned file of the agent's own event names, one flat entry per hook (Copilot, Cursor).
|
|
49
|
+
const flatShape = ({ names, entry }) => ({
|
|
50
|
+
write: (settings, commands) => {
|
|
51
|
+
const hooks = objectOr(settings.hooks);
|
|
52
|
+
const next = { ...hooks };
|
|
53
|
+
for (const event of HOOK_EVENTS)
|
|
54
|
+
next[names[event]] = withOurs(
|
|
55
|
+
hooks[names[event]],
|
|
56
|
+
entry(commands[event])
|
|
57
|
+
);
|
|
58
|
+
return { ...settings, version: settings.version ?? 1, hooks: next };
|
|
59
|
+
},
|
|
60
|
+
erase: (settings) => {
|
|
61
|
+
if (!settings.hooks) return settings;
|
|
62
|
+
const hooks = { ...objectOr(settings.hooks) };
|
|
63
|
+
for (const event of HOOK_EVENTS)
|
|
64
|
+
if (Array.isArray(hooks[names[event]]))
|
|
65
|
+
hooks[names[event]] = withOurs(hooks[names[event]]);
|
|
66
|
+
return { ...settings, hooks };
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Antigravity's file holds named sets of hooks; Harness's is the set named "harness".
|
|
71
|
+
const namedShape = ({ name, names }) => ({
|
|
72
|
+
write: (settings, commands) => ({
|
|
73
|
+
...settings,
|
|
74
|
+
[name]: Object.fromEntries(
|
|
75
|
+
HOOK_EVENTS.map((event) => [
|
|
76
|
+
names[event],
|
|
77
|
+
[{ type: "command", command: commands[event], timeout: 10 }],
|
|
78
|
+
])
|
|
79
|
+
),
|
|
80
|
+
}),
|
|
81
|
+
erase: (settings) => {
|
|
82
|
+
if (!(name in settings)) return settings;
|
|
83
|
+
const { [name]: _ours, ...rest } = settings;
|
|
84
|
+
return rest;
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const asText = (text) => text;
|
|
89
|
+
const claudeBlock = (text) =>
|
|
90
|
+
JSON.stringify({ decision: "block", reason: text });
|
|
91
|
+
|
|
92
|
+
const DIALECTS = {
|
|
93
|
+
// Other agents read Claude Code's files and run its hooks as their own: --check-host (nudge.mjs startedByAnother).
|
|
94
|
+
"claude-code": {
|
|
95
|
+
file: (home) => path.join(home, ".claude", "settings.json"),
|
|
96
|
+
shape: claudeShape(),
|
|
97
|
+
flags: ["--check-host"],
|
|
98
|
+
context: { SessionStart: asText, UserPromptSubmit: asText },
|
|
99
|
+
block: claudeBlock,
|
|
100
|
+
},
|
|
101
|
+
// Codex runs the session-start hook after a compaction too, so the session hears its work again (compaction.mjs).
|
|
102
|
+
// It asks the person to trust a new or changed hook once, with /hooks in its TUI, before it runs; setup says so.
|
|
103
|
+
codex: {
|
|
104
|
+
file: (home) => path.join(home, ".codex", "hooks.json"),
|
|
105
|
+
shape: claudeShape({
|
|
106
|
+
start: { matcher: "startup|resume|compact" },
|
|
107
|
+
startHook: { statusMessage: "Reading the Harness board" },
|
|
108
|
+
}),
|
|
109
|
+
context: { SessionStart: asText, UserPromptSubmit: asText },
|
|
110
|
+
block: claudeBlock,
|
|
111
|
+
},
|
|
112
|
+
// Grok Build reads every JSON file in ~/.grok/hooks, so Harness's hooks are a file of their own. It ignores what its
|
|
113
|
+
// session-start and prompt hooks print: its notice comes in the MCP server's instructions.
|
|
114
|
+
"grok-build": {
|
|
115
|
+
file: (home, env) =>
|
|
116
|
+
path.join(
|
|
117
|
+
env.GROK_HOME || path.join(home, ".grok"),
|
|
118
|
+
"hooks",
|
|
119
|
+
"harness.json"
|
|
120
|
+
),
|
|
121
|
+
ownFile: true,
|
|
122
|
+
shape: claudeShape(),
|
|
123
|
+
context: {},
|
|
124
|
+
block: claudeBlock,
|
|
125
|
+
},
|
|
126
|
+
// Copilot's CLI reads every JSON file in ~/.copilot/hooks. Only JSON from its session-start hook reaches the model;
|
|
127
|
+
// its prompt hook's output is dropped.
|
|
128
|
+
copilot: {
|
|
129
|
+
file: (home, env) =>
|
|
130
|
+
path.join(
|
|
131
|
+
env.COPILOT_HOME || path.join(home, ".copilot"),
|
|
132
|
+
"hooks",
|
|
133
|
+
"harness.json"
|
|
134
|
+
),
|
|
135
|
+
ownFile: true,
|
|
136
|
+
shape: flatShape({
|
|
137
|
+
names: {
|
|
138
|
+
SessionStart: "sessionStart",
|
|
139
|
+
UserPromptSubmit: "userPromptSubmitted",
|
|
140
|
+
Stop: "agentStop",
|
|
141
|
+
},
|
|
142
|
+
entry: (command) => ({ type: "command", bash: command, timeoutSec: 10 }),
|
|
143
|
+
}),
|
|
144
|
+
context: {
|
|
145
|
+
SessionStart: (text) => JSON.stringify({ additionalContext: text }),
|
|
146
|
+
},
|
|
147
|
+
block: claudeBlock,
|
|
148
|
+
},
|
|
149
|
+
// Cursor (its CLI and its app) reads ~/.cursor/hooks.json, which the person's own hooks share. Its prompt hook cannot
|
|
150
|
+
// add context; its stop hook hands back a follow-up the agent sends as the person's next message. Grok Build runs
|
|
151
|
+
// this file too: --check-host.
|
|
152
|
+
cursor: {
|
|
153
|
+
file: (home) => path.join(home, ".cursor", "hooks.json"),
|
|
154
|
+
shape: flatShape({
|
|
155
|
+
names: {
|
|
156
|
+
SessionStart: "sessionStart",
|
|
157
|
+
UserPromptSubmit: "beforeSubmitPrompt",
|
|
158
|
+
Stop: "stop",
|
|
159
|
+
},
|
|
160
|
+
entry: (command) => ({ command, timeout: 10 }),
|
|
161
|
+
}),
|
|
162
|
+
flags: ["--check-host"],
|
|
163
|
+
context: {
|
|
164
|
+
SessionStart: (text) => JSON.stringify({ additional_context: text }),
|
|
165
|
+
},
|
|
166
|
+
block: (text) => JSON.stringify({ followup_message: text }),
|
|
167
|
+
},
|
|
168
|
+
// Antigravity reads ~/.gemini/config/hooks.json (CLI, app and IDE). It has no prompt event: PreInvocation runs before
|
|
169
|
+
// each model call. Its session start does not run when a conversation is resumed, and a stop that continues adds the
|
|
170
|
+
// message as a system message.
|
|
171
|
+
antigravity: {
|
|
172
|
+
file: (home) => path.join(home, ".gemini", "config", "hooks.json"),
|
|
173
|
+
shape: namedShape({
|
|
174
|
+
name: "harness",
|
|
175
|
+
names: {
|
|
176
|
+
SessionStart: "SessionStart",
|
|
177
|
+
UserPromptSubmit: "PreInvocation",
|
|
178
|
+
Stop: "Stop",
|
|
179
|
+
},
|
|
180
|
+
}),
|
|
181
|
+
context: {
|
|
182
|
+
SessionStart: (text) =>
|
|
183
|
+
JSON.stringify({
|
|
184
|
+
injectSteps: [{ systemMessage: { systemMessage: text } }],
|
|
185
|
+
}),
|
|
186
|
+
},
|
|
187
|
+
block: (text) => JSON.stringify({ decision: "continue", reason: text }),
|
|
188
|
+
},
|
|
189
|
+
// Devin's CLI keeps its hooks in its config.json (beside its other settings), in Claude Code's shape, and takes
|
|
190
|
+
// context only as JSON. It also runs Claude Code's hooks, which stand down there (--check-host).
|
|
191
|
+
devin: {
|
|
192
|
+
file: (home) => path.join(home, ".config", "devin", "config.json"),
|
|
193
|
+
shape: claudeShape(),
|
|
194
|
+
context: {
|
|
195
|
+
SessionStart: (text) =>
|
|
196
|
+
JSON.stringify({
|
|
197
|
+
hookSpecificOutput: {
|
|
198
|
+
hookEventName: "SessionStart",
|
|
199
|
+
additionalContext: text,
|
|
200
|
+
},
|
|
201
|
+
}),
|
|
202
|
+
UserPromptSubmit: (text) =>
|
|
203
|
+
JSON.stringify({
|
|
204
|
+
hookSpecificOutput: {
|
|
205
|
+
hookEventName: "UserPromptSubmit",
|
|
206
|
+
additionalContext: text,
|
|
207
|
+
},
|
|
208
|
+
}),
|
|
209
|
+
},
|
|
210
|
+
block: claudeBlock,
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
/** The agents Harness writes session hooks for. */
|
|
215
|
+
export const HOOK_AGENTS = Object.keys(DIALECTS);
|
|
216
|
+
|
|
217
|
+
/** How an agent reads a hook's output; an agent Harness does not know is answered as Claude Code is. */
|
|
218
|
+
export function hookDialect(agent) {
|
|
219
|
+
return DIALECTS[agent] ?? DIALECTS["claude-code"];
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* What a hook hands nudge.mjs, whatever the agent called it: the session (session_id, sessionId, Cursor's
|
|
224
|
+
* conversation_id, Antigravity's conversationId), its folder (cwd, or the first workspace root), and whether this is a
|
|
225
|
+
* turn ending. Grok Build also runs its Stop hooks as a session closes (reason "channel_closed" or "shutdown") and
|
|
226
|
+
* ignores their answer there.
|
|
227
|
+
*/
|
|
228
|
+
export function hookInput(raw) {
|
|
229
|
+
if (!raw || typeof raw !== "object") return null;
|
|
230
|
+
return {
|
|
231
|
+
...raw,
|
|
232
|
+
session_id:
|
|
233
|
+
raw.conversation_id ??
|
|
234
|
+
raw.conversationId ??
|
|
235
|
+
raw.session_id ??
|
|
236
|
+
raw.sessionId,
|
|
237
|
+
cwd: raw.cwd ?? raw.workspace_roots?.[0] ?? raw.workspacePaths?.[0],
|
|
238
|
+
endsTurn: raw.reason === undefined || raw.reason === "end_turn",
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** The file an agent reads its session hooks from. */
|
|
243
|
+
export function sessionHooksPath(
|
|
244
|
+
agent,
|
|
245
|
+
homeDir = os.homedir(),
|
|
246
|
+
env = process.env
|
|
247
|
+
) {
|
|
248
|
+
return DIALECTS[agent].file(homeDir, env);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export function installSessionHooks(
|
|
252
|
+
agent,
|
|
253
|
+
{
|
|
254
|
+
runtimeRoot,
|
|
255
|
+
homeDir = os.homedir(),
|
|
256
|
+
nodePath = process.execPath,
|
|
257
|
+
env = process.env,
|
|
258
|
+
}
|
|
259
|
+
) {
|
|
260
|
+
const dialect = DIALECTS[agent];
|
|
261
|
+
const file = dialect.file(homeDir, env);
|
|
262
|
+
const nudge = [
|
|
263
|
+
JSON.stringify(nodePath),
|
|
264
|
+
JSON.stringify(path.join(runtimeRoot, "lib", "nudge.mjs")),
|
|
265
|
+
"--agent",
|
|
266
|
+
agent,
|
|
267
|
+
...(dialect.flags ?? []),
|
|
268
|
+
].join(" ");
|
|
269
|
+
const commands = {
|
|
270
|
+
SessionStart: nudge,
|
|
271
|
+
// Mid-session: on every prompt, one line only when the queue changed since the session last saw it.
|
|
272
|
+
UserPromptSubmit: `${nudge} --if-changed`,
|
|
273
|
+
Stop: `${nudge} --stop`,
|
|
274
|
+
};
|
|
275
|
+
const settings = dialect.ownFile ? {} : readJson(file, {});
|
|
276
|
+
ensureDir(path.dirname(file));
|
|
277
|
+
fs.writeFileSync(
|
|
278
|
+
file,
|
|
279
|
+
JSON.stringify(dialect.shape.write(settings, commands), null, 2) + "\n"
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export function removeSessionHooks(
|
|
284
|
+
agent,
|
|
285
|
+
homeDir = os.homedir(),
|
|
286
|
+
env = process.env
|
|
287
|
+
) {
|
|
288
|
+
const dialect = DIALECTS[agent];
|
|
289
|
+
const file = dialect.file(homeDir, env);
|
|
290
|
+
if (dialect.ownFile) {
|
|
291
|
+
fs.rmSync(file, { force: true });
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
const settings = readJson(file, null);
|
|
295
|
+
const erased = settings && dialect.shape.erase(settings);
|
|
296
|
+
if (erased && erased !== settings)
|
|
297
|
+
fs.writeFileSync(file, JSON.stringify(erased, null, 2) + "\n");
|
|
298
|
+
}
|
package/lib/inference.mjs
CHANGED
|
@@ -3,6 +3,7 @@ import net from "node:net";
|
|
|
3
3
|
import os from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import { spawn, spawnSync } from "node:child_process";
|
|
6
|
+
import { randomBytes } from "node:crypto";
|
|
6
7
|
import {
|
|
7
8
|
MODELS,
|
|
8
9
|
modelById,
|
|
@@ -123,10 +124,9 @@ export function createPower({
|
|
|
123
124
|
);
|
|
124
125
|
},
|
|
125
126
|
/**
|
|
126
|
-
* Whether the dream waits. It runs whenever the computer is on
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
* should run on low power").
|
|
127
|
+
* Whether the dream waits. It runs whenever the computer is on, but not hot, below 20% on battery, or in Low Power
|
|
128
|
+
* Mode, unless Lid-Closed Mode holds the Mac awake: it turns Low Power Mode on with the lid closed, and the dream
|
|
129
|
+
* runs then, on low power.
|
|
130
130
|
*/
|
|
131
131
|
dreamWaits() {
|
|
132
132
|
const now = this.state();
|
|
@@ -152,6 +152,12 @@ export function createInference({
|
|
|
152
152
|
}) {
|
|
153
153
|
ensureDir(root);
|
|
154
154
|
const deviceFile = path.join(root, "device.json");
|
|
155
|
+
// The model server answers only this daemon: a key of its own, read from an owner-only file (not the command line,
|
|
156
|
+
// which any program here can list), and no slots endpoint, which shows what was last sent to a model. The models'
|
|
157
|
+
// own servers the router starts on other local ports take no key (llama.cpp's router does not pass one on).
|
|
158
|
+
const apiKey = randomBytes(32).toString("base64url");
|
|
159
|
+
const keyFile = path.join(root, "llama-key");
|
|
160
|
+
const keyed = { authorization: `Bearer ${apiKey}` };
|
|
155
161
|
let server = null;
|
|
156
162
|
let starting = null;
|
|
157
163
|
const queues = { llama: [], onnx: [] };
|
|
@@ -203,6 +209,7 @@ export function createInference({
|
|
|
203
209
|
const launch = async (gpu) => {
|
|
204
210
|
const binary = llamaServer(root) ?? (await ensureLlama({ root, gpu, log }));
|
|
205
211
|
const port = await freePort();
|
|
212
|
+
fs.writeFileSync(keyFile, `${apiKey}\n`, { mode: 0o600 });
|
|
206
213
|
const child = spawn(
|
|
207
214
|
binary,
|
|
208
215
|
[
|
|
@@ -215,6 +222,9 @@ export function createInference({
|
|
|
215
222
|
"--port",
|
|
216
223
|
String(port),
|
|
217
224
|
"--no-webui",
|
|
225
|
+
"--api-key-file",
|
|
226
|
+
keyFile,
|
|
227
|
+
"--no-slots",
|
|
218
228
|
],
|
|
219
229
|
{ cwd: path.dirname(binary), stdio: ["ignore", "ignore", "pipe"] }
|
|
220
230
|
);
|
|
@@ -229,7 +239,7 @@ export function createInference({
|
|
|
229
239
|
`the local model server stopped: ${tail.trim().split("\n").at(-1) || child.exitCode}`
|
|
230
240
|
);
|
|
231
241
|
try {
|
|
232
|
-
if ((await fetch(`${base}/health
|
|
242
|
+
if ((await fetch(`${base}/health`, { headers: keyed })).ok)
|
|
233
243
|
return {
|
|
234
244
|
child,
|
|
235
245
|
base,
|
|
@@ -351,7 +361,7 @@ export function createInference({
|
|
|
351
361
|
async function post(base, pathname, body) {
|
|
352
362
|
const response = await fetch(`${base}${pathname}`, {
|
|
353
363
|
method: "POST",
|
|
354
|
-
headers: { "content-type": "application/json" },
|
|
364
|
+
headers: { "content-type": "application/json", ...keyed },
|
|
355
365
|
body: JSON.stringify(body),
|
|
356
366
|
});
|
|
357
367
|
const text = await response.text();
|
|
@@ -703,7 +713,11 @@ export function createInference({
|
|
|
703
713
|
input: ["warm up"],
|
|
704
714
|
});
|
|
705
715
|
ready.marker = (
|
|
706
|
-
await (
|
|
716
|
+
await (
|
|
717
|
+
await fetch(`${ready.base}/props?model=media`, {
|
|
718
|
+
headers: keyed,
|
|
719
|
+
})
|
|
720
|
+
).json()
|
|
707
721
|
).media_marker;
|
|
708
722
|
}
|
|
709
723
|
const out = [];
|
|
@@ -752,7 +766,11 @@ export function createInference({
|
|
|
752
766
|
if (server && server.child.exitCode === null) {
|
|
753
767
|
try {
|
|
754
768
|
llama = (
|
|
755
|
-
(
|
|
769
|
+
(
|
|
770
|
+
await (
|
|
771
|
+
await fetch(`${server.base}/models`, { headers: keyed })
|
|
772
|
+
).json()
|
|
773
|
+
).data ?? []
|
|
756
774
|
).map((model) => ({
|
|
757
775
|
id: model.id,
|
|
758
776
|
state: model.status?.value ?? null,
|