tryharness 0.7.64 → 0.7.67

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.
@@ -0,0 +1,308 @@
1
+ import { randomBytes } from "node:crypto";
2
+ import { apply, diff } from "./jsonPatch.mjs";
3
+
4
+ // The board as another of the person's computers shows it, answered on that computer (owner, October 28). The buddy and
5
+ // the agents there read the same few things over and over (the board, the thread list, the agents, the digest); each
6
+ // read went to home sealed, about 400 a minute from two computers, the thread list alone 362 KB, and every screen on
7
+ // them waited on home and stalled whenever home did. Now:
8
+ //
9
+ // - The first time a computer reads one of these, home answers it and keeps it as a view of that computer's: the read
10
+ // as that computer asked it, and the answer home gave.
11
+ // - When home's board moves (at most every GAP_MS), and every REFRESH_MS for what changes beside the board, home works
12
+ // out each view again and pushes only what changed in it (jsonPatch.mjs), numbered, so a change is applied only to
13
+ // the answer it was made from. A push with nothing changed still says home is there.
14
+ // - The computer answers a read from its copy while home has been heard from within FRESH_MS; otherwise, and for a copy
15
+ // a push did not fit, it asks home again. A view nobody read for IDLE_MS is let go on both sides.
16
+ // - Home's answer is the same as a read sent to it: it is the same read, answered for that computer (ownMachines.mjs).
17
+ // Claims, runs and every write still go to home and are decided there.
18
+
19
+ /** A worker asking home to keep a view of a read for it: { request } → { ok, view: { id, version, value } }. */
20
+ export const VIEW_COMMAND = "board.view";
21
+ /** A worker letting go of views it no longer reads: { ids }. */
22
+ export const VIEW_DROP_COMMAND = "board.view.drop";
23
+
24
+ // What the buddy and the agents read over and over; anything else goes to home each time.
25
+ const VIEW_READS = new Set([
26
+ "board.list",
27
+ "board.get",
28
+ "board.agents",
29
+ "board.digest",
30
+ "board.watch.list",
31
+ "board.goal.list",
32
+ "board.swarm.list",
33
+ "board.swarm.read",
34
+ "board.swarm.agents",
35
+ "board.room.read",
36
+ ]);
37
+ // The thread list's own catching up (listVersions.mjs) is not part of the view: the copy answers it whole.
38
+ const NOT_THE_VIEW = {
39
+ "board.swarm.list": ["epoch", "after"],
40
+ };
41
+
42
+ // The board moves many times a second while agents work: one pass for all of it, at most this often.
43
+ const GAP_MS = 500;
44
+ // What a view shows beside the board (runs going, presence) is worked out again this often, and says home is there.
45
+ const REFRESH_MS = 30_000;
46
+ // A copy is answered from while home was heard within this, more than two refreshes.
47
+ const FRESH_MS = 75_000;
48
+ // A view nobody read for this long is let go.
49
+ const IDLE_MS = 2 * 60_000;
50
+
51
+ const sorted = (value) =>
52
+ Array.isArray(value)
53
+ ? value.map(sorted)
54
+ : value && typeof value === "object"
55
+ ? Object.fromEntries(
56
+ Object.keys(value)
57
+ .sort()
58
+ .map((key) => [key, sorted(value[key])])
59
+ )
60
+ : value;
61
+
62
+ /** The read a view keeps: the request without what only steers a reader's own catching up. */
63
+ export function viewRequest(request) {
64
+ const command = String(request?.command ?? "");
65
+ if (!VIEW_READS.has(command)) return null;
66
+ const view = { ...request };
67
+ for (const field of NOT_THE_VIEW[command] ?? []) delete view[field];
68
+ return view;
69
+ }
70
+
71
+ /**
72
+ * On home: the views each computer reads. `answer(workerId, request)` answers a read as that computer's call would be;
73
+ * `push(workerId, body)` sends { views: [...] } there and says whether it arrived; `onMove(listener)` hears the board
74
+ * move (boardWaits.mjs).
75
+ */
76
+ export function createLiveViews({
77
+ answer,
78
+ push,
79
+ onMove,
80
+ log = () => {},
81
+ gapMs = GAP_MS,
82
+ refreshMs = REFRESH_MS,
83
+ }) {
84
+ // workerId -> Map<viewId, { request, value, version }>
85
+ const workers = new Map();
86
+ let scheduled = null;
87
+ let running = false;
88
+ let again = false;
89
+
90
+ const viewsOf = (workerId) => {
91
+ let views = workers.get(workerId);
92
+ if (!views) workers.set(workerId, (views = new Map()));
93
+ return views;
94
+ };
95
+
96
+ const subscribe = async (workerId, request) => {
97
+ const read = viewRequest(request);
98
+ if (!read)
99
+ return { ok: false, error: "that read is not one a computer keeps" };
100
+ const value = await answer(workerId, read);
101
+ const id = randomBytes(9).toString("base64url");
102
+ viewsOf(workerId).set(id, { request: read, value, version: 1 });
103
+ listening();
104
+ return { ok: true, view: { id, version: 1, value } };
105
+ };
106
+
107
+ const drop = (workerId, ids = []) => {
108
+ const views = workers.get(workerId);
109
+ for (const id of ids) views?.delete(String(id));
110
+ if (views && !views.size) workers.delete(workerId);
111
+ listening();
112
+ return { ok: true };
113
+ };
114
+
115
+ const forget = (workerId) => {
116
+ workers.delete(workerId);
117
+ listening();
118
+ };
119
+
120
+ // Each computer's views worked out again; what changed is pushed, and a computer that could not be reached is let go
121
+ // (its copies go stale, so it asks again).
122
+ const pass = async (heartbeat) => {
123
+ for (const [workerId, views] of [...workers]) {
124
+ const changes = [];
125
+ for (const [id, view] of views) {
126
+ let next;
127
+ try {
128
+ next = await answer(workerId, view.request);
129
+ } catch (error) {
130
+ log(`live views: ${view.request.command}: ${error.message}`);
131
+ continue;
132
+ }
133
+ const change = diff(view.value, next);
134
+ if (change === undefined) continue;
135
+ changes.push({
136
+ id,
137
+ base: view.version,
138
+ version: view.version + 1,
139
+ change,
140
+ });
141
+ view.value = next;
142
+ view.version += 1;
143
+ }
144
+ if (!changes.length && !heartbeat) continue;
145
+ const arrived = await push(workerId, { views: changes }).catch(
146
+ () => false
147
+ );
148
+ if (!arrived) {
149
+ log(
150
+ `live views: ${workerId} could not be reached; its copies ask again`
151
+ );
152
+ forget(workerId);
153
+ }
154
+ }
155
+ };
156
+
157
+ const run = async (heartbeat = false) => {
158
+ if (running) {
159
+ again = true;
160
+ return;
161
+ }
162
+ running = true;
163
+ try {
164
+ await pass(heartbeat);
165
+ } finally {
166
+ running = false;
167
+ if (again) {
168
+ again = false;
169
+ schedule();
170
+ }
171
+ }
172
+ };
173
+
174
+ const schedule = () => {
175
+ if (scheduled || !workers.size) return;
176
+ scheduled = setTimeout(() => {
177
+ scheduled = null;
178
+ void run();
179
+ }, gapMs);
180
+ scheduled.unref?.();
181
+ };
182
+
183
+ // The board's moves are listened to only while a computer keeps a view: listening checks the board ten times a second.
184
+ let stopMoves = null;
185
+ const listening = () => {
186
+ if (workers.size && !stopMoves) stopMoves = onMove(() => schedule());
187
+ if (!workers.size && stopMoves) {
188
+ stopMoves();
189
+ stopMoves = null;
190
+ }
191
+ };
192
+ const refresh = setInterval(() => {
193
+ if (workers.size) void run(true);
194
+ }, refreshMs);
195
+ refresh.unref?.();
196
+
197
+ const stop = () => {
198
+ stopMoves?.();
199
+ clearInterval(refresh);
200
+ clearTimeout(scheduled);
201
+ };
202
+
203
+ return { subscribe, drop, forget, stop, watched: () => workers.size > 0 };
204
+ }
205
+
206
+ /**
207
+ * On a worker: its copies of the views home keeps for it. `callHome(request)` is a sealed call to home;
208
+ * `changed()` says something shown moved (boardWaits.bump), so a surface waiting on this computer reads again.
209
+ */
210
+ export function createViewCopies({
211
+ callHome,
212
+ changed = () => {},
213
+ now = Date.now,
214
+ freshMs = FRESH_MS,
215
+ idleMs = IDLE_MS,
216
+ }) {
217
+ // key -> { id, version, value, readAt }; id -> key
218
+ const copies = new Map();
219
+ const keyOfId = new Map();
220
+ let heardAt = 0;
221
+ // Home answered that it keeps no views (an older home): every read goes to it, until home is next back.
222
+ let unsupported = false;
223
+
224
+ const remove = (key) => {
225
+ const copy = copies.get(key);
226
+ if (!copy) return;
227
+ copies.delete(key);
228
+ keyOfId.delete(copy.id);
229
+ };
230
+
231
+ const letGoIdle = () => {
232
+ const idle = [...copies].filter(([, copy]) => now() - copy.readAt > idleMs);
233
+ if (!idle.length) return;
234
+ for (const [key] of idle) remove(key);
235
+ void callHome({
236
+ command: VIEW_DROP_COMMAND,
237
+ agent: "user",
238
+ ids: idle.map(([, copy]) => copy.id),
239
+ }).catch(() => null);
240
+ };
241
+
242
+ /** The answer to `request` from this computer's copy, keeping one when there is none; null when home must answer. */
243
+ const read = async (request) => {
244
+ const view = viewRequest(request);
245
+ if (!view || unsupported) return null;
246
+ letGoIdle();
247
+ const key = JSON.stringify(sorted(view));
248
+ const copy = copies.get(key);
249
+ if (copy && now() - heardAt <= freshMs) {
250
+ copy.readAt = now();
251
+ return copy.value;
252
+ }
253
+ remove(key);
254
+ const answer = await callHome({
255
+ command: VIEW_COMMAND,
256
+ agent: request.agent,
257
+ request: view,
258
+ }).catch(() => null);
259
+ if (!answer?.ok || !answer.view?.id) {
260
+ // A home that answered but keeps no views (older than them): reads go to it as before, until it is next back.
261
+ if (answer && answer.code !== "home_offline") unsupported = true;
262
+ return null;
263
+ }
264
+ heardAt = now();
265
+ copies.set(key, {
266
+ id: answer.view.id,
267
+ version: answer.view.version,
268
+ value: answer.view.value,
269
+ readAt: now(),
270
+ });
271
+ keyOfId.set(answer.view.id, key);
272
+ return answer.view.value;
273
+ };
274
+
275
+ /** Home's push: each change applied to the copy it was made from; one that does not fit drops the copy. */
276
+ const onPush = (body) => {
277
+ heardAt = now();
278
+ let moved = false;
279
+ for (const change of Array.isArray(body?.views) ? body.views : []) {
280
+ const key = keyOfId.get(String(change?.id));
281
+ const copy = key && copies.get(key);
282
+ if (!copy) continue;
283
+ if (copy.version !== change.base) {
284
+ remove(key);
285
+ continue;
286
+ }
287
+ try {
288
+ copy.value = apply(copy.value, change.change);
289
+ copy.version = change.version;
290
+ moved = true;
291
+ } catch {
292
+ remove(key);
293
+ }
294
+ }
295
+ if (moved) changed();
296
+ return { ok: true };
297
+ };
298
+
299
+ /** Home changed or came back: every copy is asked for again. */
300
+ const reset = () => {
301
+ copies.clear();
302
+ keyOfId.clear();
303
+ heardAt = 0;
304
+ unsupported = false;
305
+ };
306
+
307
+ return { read, onPush, reset };
308
+ }
@@ -28,7 +28,19 @@ export function askLocalAgent(agent, prompt) {
28
28
  }
29
29
  : {
30
30
  command: "claude",
31
- args: ["-p", prompt, "--output-format", "json", "--tools", ""],
31
+ // Only the model reads: the person's MCP servers, hooks and settings made each reading take ~15 s instead
32
+ // of ~2 s, and a question with no tools uses none of them.
33
+ args: [
34
+ "-p",
35
+ prompt,
36
+ "--output-format",
37
+ "json",
38
+ "--tools",
39
+ "",
40
+ "--strict-mcp-config",
41
+ "--setting-sources",
42
+ "",
43
+ ],
32
44
  };
33
45
  let child;
34
46
  try {
@@ -9,6 +9,8 @@ import { member } from "./members.mjs";
9
9
  // prompts. Everything else on the board is home's.
10
10
  // `repos`: a repository link is found on, and cloned onto, the computer the person is at, never home's disk.
11
11
  // `nativeRequests`: an agent's permission request lives in the memory of the computer whose run asked (runRequests.mjs).
12
+ // `waits`: a surface waits for this computer's board to move, which home's pushes and the board copy move
13
+ // (liveViews.mjs): a wait held at home cost a sealed call every 15 s and every change.
12
14
  const LOCAL_OWNERS = new Set([
13
15
  "tools",
14
16
  "live",
@@ -16,6 +18,7 @@ const LOCAL_OWNERS = new Set([
16
18
  "prompt",
17
19
  "repos",
18
20
  "nativeRequests",
21
+ "waits",
19
22
  ]);
20
23
 
21
24
  /**
@@ -2,6 +2,7 @@ import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import { within } from "./folders.mjs";
4
4
  import { provenanceLine } from "./provenance.mjs";
5
+ import { prepareScratch, threadScratch } from "./scratch.mjs";
5
6
 
6
7
  // On a worker: the work home keeps for this computer's agents, run here. The person's Start (or a thread's authority)
7
8
  // 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
@@ -34,6 +35,9 @@ export function createMachineRuns({
34
35
  taskRuns,
35
36
  agents,
36
37
  knownFolders = () => [],
38
+ // The files of a thread, by id, as this computer's copy of the board has them: a folder-less thread's turn gets them
39
+ // in its scratch folder.
40
+ threadFileIds = () => [],
37
41
  log,
38
42
  // The model and effort the run starts with, from the person's choice here (models.mjs createModelRouter): a choice
39
43
  // for an agent is kept on the computer that runs it, so it is read here, not on home.
@@ -61,8 +65,28 @@ export function createMachineRuns({
61
65
  clearTimeout(steps.get(taskId)?.timer);
62
66
  steps.delete(taskId);
63
67
  };
68
+ // A thread with no folder: its turn here works in that thread's scratch folder on this computer (scratch.mjs).
69
+ const inScratch = (task) =>
70
+ !task.run_workspace && task.kind === "wake" && Boolean(task.parent_id);
64
71
  const slotOf = (task) =>
65
- task.run_workspace ? path.resolve(task.run_workspace) : task.target_agent;
72
+ task.run_workspace
73
+ ? path.resolve(task.run_workspace)
74
+ : inScratch(task)
75
+ ? threadScratch(task.parent_id)
76
+ : task.target_agent;
77
+ // The thread's files, from home, put in the scratch folder; one home cannot send is left out.
78
+ const scratchFor = async (task) => {
79
+ const files = [];
80
+ for (const fileId of threadFileIds(task.parent_id)) {
81
+ const answer = await callHome({
82
+ command: "board.file.get",
83
+ agent: task.target_agent,
84
+ fileId,
85
+ }).catch(() => null);
86
+ if (answer?.ok && answer.file?.path) files.push(answer.file);
87
+ }
88
+ return prepareScratch(task.parent_id, files);
89
+ };
66
90
 
67
91
  const briefOf = (task) => {
68
92
  const refs = (task.context_refs ?? [])
@@ -100,6 +124,7 @@ export function createMachineRuns({
100
124
  log(`running ${agent}'s task ${task.id} from your home computer`);
101
125
  const secrets = await secretsFor(task, agent);
102
126
  const chosen = await modelFor(task, agent).catch(() => null);
127
+ const cwd = inScratch(task) ? await scratchFor(task) : task.run_workspace;
103
128
  // Sent again with the finish: the two calls go to home at once, and a finish that arrives first closes the run to
104
129
  // the session call (two-machines: home kept no session, one run in four).
105
130
  let session = null;
@@ -109,7 +134,7 @@ export function createMachineRuns({
109
134
  agent,
110
135
  title: String(task.objective || "").slice(0, 120),
111
136
  brief: briefOf(task),
112
- cwd: task.run_workspace,
137
+ cwd,
113
138
  ...(chosen?.model ? { model: chosen.model } : {}),
114
139
  ...(chosen?.effort ? { effort: chosen.effort } : {}),
115
140
  ...(chosen?.model && chosen.limits ? { limits: chosen.limits } : {}),
@@ -155,6 +180,7 @@ export function createMachineRuns({
155
180
 
156
181
  // Why this computer will not run a task where home put it, or null when it will.
157
182
  const refusalFor = (task) => {
183
+ if (inScratch(task)) return null;
158
184
  const folder =
159
185
  typeof task.run_workspace === "string" &&
160
186
  path.isAbsolute(task.run_workspace)
@@ -114,26 +114,35 @@ export const pairKey = (privateKey, peerPublicKey, workerId) =>
114
114
  export const newRequestId = () => randomBytes(16).toString("base64url");
115
115
 
116
116
  /** A worker's board call, sealed for home. */
117
+ // `session`: the session home gave this worker (callSessions.mjs), for a call through Harness that home guards in
118
+ // memory rather than by keeping its request id.
117
119
  export function sealCall(
118
120
  key,
119
121
  workerId,
120
122
  requestId,
121
123
  call,
122
- { queued = false, now = Date.now() } = {}
124
+ { queued = false, session = null, now = Date.now() } = {}
123
125
  ) {
124
- return seal(key, "w2h", workerId, requestId, { ts: now, call, queued });
126
+ return seal(key, "w2h", workerId, requestId, {
127
+ ts: now,
128
+ call,
129
+ queued,
130
+ ...(session ? { session } : {}),
131
+ });
125
132
  }
126
133
 
127
134
  const age = (ts, now) => (typeof ts === "number" ? now - ts : Infinity);
128
135
 
129
136
  /**
130
137
  * Home opening a worker's call. `from` is the worker Harness says sent it; `firstSeen(requestId)` is false for a
131
- * request id already taken. Returns { ok, call, queued } or { ok: false, code, error }.
138
+ * request id already taken. A call sealed in a session is checked by `inSession(session, requestId)` instead:
139
+ * "fresh", "replayed", or "expired" for a session home no longer holds (it restarted, or gave the worker a newer one).
140
+ * Returns { ok, call, queued, session } or { ok: false, code, error }.
132
141
  */
133
142
  export async function openCall(
134
143
  key,
135
144
  envelope,
136
- { from, firstSeen, now = Date.now() }
145
+ { from, firstSeen, inSession = () => "expired", now = Date.now() }
137
146
  ) {
138
147
  if (envelope?.deviceId !== from)
139
148
  return {
@@ -162,13 +171,33 @@ export async function openCall(
162
171
  code: "stale",
163
172
  error: "the call is too old or the computer's clock is off",
164
173
  };
165
- if (!firstSeen(envelope.requestId))
174
+ const session =
175
+ !opened.queued && typeof opened.session === "string"
176
+ ? opened.session
177
+ : null;
178
+ const seen = session
179
+ ? inSession(session, envelope.requestId)
180
+ : firstSeen(envelope.requestId)
181
+ ? "fresh"
182
+ : "replayed";
183
+ if (seen === "expired")
184
+ return {
185
+ ok: false,
186
+ code: "session_expired",
187
+ error: "this computer's session with home ended; it starts a new one",
188
+ };
189
+ if (seen !== "fresh")
166
190
  return {
167
191
  ok: false,
168
192
  code: "replayed",
169
193
  error: "this call was already taken",
170
194
  };
171
- return { ok: true, call: opened.call, queued: opened.queued === true };
195
+ return {
196
+ ok: true,
197
+ call: opened.call,
198
+ queued: opened.queued === true,
199
+ session,
200
+ };
172
201
  }
173
202
 
174
203
  /** Home's answer to a worker's call. */
package/lib/models.mjs CHANGED
@@ -78,7 +78,12 @@ const LISTS = {
78
78
  body = [];
79
79
  const flush = () => {
80
80
  if (!id) return;
81
- const entry = JSON.parse(body.join("\n"));
81
+ // An entry cut short (the list read while OpenCode was starting failed on every daemon start) keeps its model
82
+ // without details, rather than losing the whole list.
83
+ let entry = {};
84
+ try {
85
+ entry = JSON.parse(body.join("\n"));
86
+ } catch {}
82
87
  models.push({
83
88
  id,
84
89
  efforts: Object.keys(entry.variants || {}),