tryharness 0.7.82 → 0.7.86

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.
Files changed (216) hide show
  1. package/README.md +21 -19
  2. package/assets/dsh-plugin.mjs +75 -4
  3. package/assets/herdr-mod/harness/main.js +367 -0
  4. package/assets/herdr-mod/harness/mod.json +6 -0
  5. package/assets/opencode-plugin.js +87 -22
  6. package/assets/pi-extension.ts +81 -2
  7. package/cli.mjs +544 -208
  8. package/lib/adapters.mjs +62 -27
  9. package/lib/agentBinaries.mjs +6 -8
  10. package/lib/agentClaims.mjs +168 -0
  11. package/lib/agentReadTools.json +415 -0
  12. package/lib/agentSignIn.mjs +17 -18
  13. package/lib/agentWiring.mjs +114 -0
  14. package/lib/archetypes.js +10 -11
  15. package/lib/attention.mjs +100 -0
  16. package/lib/audit.mjs +1 -1
  17. package/lib/backupSetup.mjs +6 -28
  18. package/lib/bankrApprovals.mjs +86 -0
  19. package/lib/boardAccess.mjs +40 -66
  20. package/lib/boardBackup.mjs +86 -85
  21. package/lib/boardCommands.mjs +28 -0
  22. package/lib/boardFileCommands.mjs +29 -36
  23. package/lib/boardFiles.mjs +83 -15
  24. package/lib/boardMirror.mjs +78 -11
  25. package/lib/boardProtocol.json +193 -123
  26. package/lib/boardThreads.mjs +19 -37
  27. package/lib/boardTools.mjs +22 -15
  28. package/lib/boardWatches.mjs +43 -15
  29. package/lib/bridge.mjs +7 -3
  30. package/lib/browserAgentTools.mjs +59 -0
  31. package/lib/browserWake.mjs +0 -6
  32. package/lib/cards.mjs +57 -2
  33. package/lib/circling.mjs +189 -0
  34. package/lib/cleanup.mjs +234 -0
  35. package/lib/cloudBoot.mjs +21 -4
  36. package/lib/cloudSave.mjs +46 -5
  37. package/lib/compaction.mjs +8 -1
  38. package/lib/completion.mjs +358 -0
  39. package/lib/computerDevices.mjs +853 -0
  40. package/lib/connectionStatus.mjs +34 -0
  41. package/lib/connectors.mjs +139 -1
  42. package/lib/coordinator.mjs +108 -14
  43. package/lib/coreModels.mjs +36 -12
  44. package/lib/daemon.mjs +2435 -333
  45. package/lib/daemonLock.mjs +103 -0
  46. package/lib/deviceDoor.mjs +308 -0
  47. package/lib/deviceIdentity.mjs +114 -0
  48. package/lib/deviceList.mjs +172 -0
  49. package/lib/deviceSeal.mjs +74 -0
  50. package/lib/dispatcher.mjs +497 -26
  51. package/lib/dispatches.mjs +3 -3
  52. package/lib/environment.mjs +2 -1
  53. package/lib/environments.mjs +67 -41
  54. package/lib/events.mjs +318 -0
  55. package/lib/facts.mjs +170 -0
  56. package/lib/fileSource.mjs +540 -0
  57. package/lib/files.mjs +47 -2
  58. package/lib/gitHost.mjs +39 -1
  59. package/lib/goalCommands.mjs +12 -80
  60. package/lib/handover.mjs +21 -16
  61. package/lib/harnessOffers.mjs +140 -15
  62. package/lib/herdr.mjs +100 -15
  63. package/lib/herdrAppCall.mjs +19 -0
  64. package/lib/herdrCore.mjs +59 -0
  65. package/lib/herdrRuns.mjs +1126 -0
  66. package/lib/hookDialects.mjs +235 -50
  67. package/lib/identityLink.mjs +81 -0
  68. package/lib/inference.mjs +31 -2
  69. package/lib/jamAccess.mjs +82 -0
  70. package/lib/{projectBoard.mjs → jamBoard.mjs} +5 -4
  71. package/lib/{projectGit.mjs → jamGit.mjs} +53 -15
  72. package/lib/jamKey.mjs +274 -0
  73. package/lib/{projectPieces.mjs → jamPieces.mjs} +35 -29
  74. package/lib/{projectSettings.mjs → jamSettings.mjs} +12 -22
  75. package/lib/jamStore.mjs +973 -0
  76. package/lib/jamUpgrade.mjs +112 -0
  77. package/lib/jams.mjs +3079 -0
  78. package/lib/kanban.mjs +13 -7
  79. package/lib/landed.mjs +5 -1
  80. package/lib/library.mjs +41 -22
  81. package/lib/limits.mjs +91 -0
  82. package/lib/lineDiff.mjs +97 -0
  83. package/lib/lineup.mjs +18 -13
  84. package/lib/link.mjs +584 -0
  85. package/lib/localStack.mjs +75 -16
  86. package/lib/localmodels.mjs +58 -78
  87. package/lib/loopBackstop.mjs +2 -1
  88. package/lib/machine.mjs +544 -0
  89. package/lib/machineLink.mjs +446 -0
  90. package/lib/machineRoutes.mjs +68 -19
  91. package/lib/machineRuns.mjs +85 -4
  92. package/lib/machineSeal.mjs +6 -31
  93. package/lib/manager.mjs +134 -12
  94. package/lib/mcp-stdio.mjs +492 -215
  95. package/lib/mcpRelay.mjs +34 -35
  96. package/lib/members.json +2 -2
  97. package/lib/modelRoute.mjs +162 -0
  98. package/lib/nativeHost.mjs +81 -90
  99. package/lib/nativeSenses.mjs +258 -0
  100. package/lib/nerves.mjs +523 -0
  101. package/lib/network.mjs +117 -278
  102. package/lib/networkPlaces.mjs +578 -0
  103. package/lib/nextStep.mjs +95 -0
  104. package/lib/notify.mjs +2 -2
  105. package/lib/nudge.mjs +116 -10
  106. package/lib/onboard.mjs +6 -6
  107. package/lib/originals.mjs +216 -0
  108. package/lib/outsideEvents.mjs +383 -0
  109. package/lib/outsideRuns.mjs +230 -0
  110. package/lib/ownMachines.mjs +674 -256
  111. package/lib/pain.mjs +215 -0
  112. package/lib/pairing.mjs +91 -11
  113. package/lib/paths.mjs +42 -3
  114. package/lib/peerProcess.mjs +21 -7
  115. package/lib/peopleLink.mjs +268 -0
  116. package/lib/personNotices.mjs +3 -2
  117. package/lib/phones.mjs +252 -542
  118. package/lib/placeCards.mjs +282 -0
  119. package/lib/placeChecks.mjs +95 -0
  120. package/lib/placeCommands.mjs +8 -17
  121. package/lib/placeRequests.mjs +152 -0
  122. package/lib/placeStore.mjs +295 -0
  123. package/lib/placeTool.d.ts +12 -0
  124. package/lib/placeTool.js +40 -0
  125. package/lib/places.mjs +63 -14
  126. package/lib/plans.mjs +53 -8
  127. package/lib/presence.mjs +270 -0
  128. package/lib/privateNetwork.d.ts +2 -0
  129. package/lib/privateNetwork.js +93 -0
  130. package/lib/proposals.mjs +300 -0
  131. package/lib/publicFetch.d.ts +32 -0
  132. package/lib/publicFetch.js +248 -0
  133. package/lib/reach.mjs +128 -0
  134. package/lib/rebuildable.mjs +72 -0
  135. package/lib/recall.mjs +216 -0
  136. package/lib/record.mjs +13 -0
  137. package/lib/reflexes.mjs +392 -0
  138. package/lib/relayLimits.mjs +31 -2
  139. package/lib/releaseNotice.mjs +198 -0
  140. package/lib/removeHarness.mjs +15 -4
  141. package/lib/repoLinks.mjs +204 -35
  142. package/lib/runAuthority.mjs +102 -39
  143. package/lib/runBrief.mjs +93 -40
  144. package/lib/runRequests.mjs +52 -14
  145. package/lib/runUsage.mjs +31 -0
  146. package/lib/runWorker.mjs +74 -25
  147. package/lib/runner.mjs +289 -45
  148. package/lib/runtime-install.mjs +37 -8
  149. package/lib/schedules.mjs +171 -47
  150. package/lib/screenWatches.mjs +221 -0
  151. package/lib/sealed.mjs +12 -30
  152. package/lib/sealedThreads.mjs +198 -392
  153. package/lib/secretStore.mjs +178 -0
  154. package/lib/secrets.mjs +11 -0
  155. package/lib/seen.mjs +3 -9
  156. package/lib/selfUpdate.mjs +51 -15
  157. package/lib/senseCall.mjs +1514 -0
  158. package/lib/senseHook.mjs +114 -0
  159. package/lib/senseTools.mjs +1298 -0
  160. package/lib/sessionBriefs.mjs +87 -0
  161. package/lib/sessionControl.mjs +63 -13
  162. package/lib/sessionFiles.mjs +85 -0
  163. package/lib/setupChoices.mjs +12 -3
  164. package/lib/setupMode.mjs +70 -25
  165. package/lib/sharedKeys.mjs +166 -0
  166. package/lib/sleep.mjs +396 -0
  167. package/lib/sources.mjs +306 -0
  168. package/lib/speech.mjs +166 -411
  169. package/lib/spoken.mjs +210 -0
  170. package/lib/steerHook.mjs +3 -1
  171. package/lib/steerQueue.mjs +40 -11
  172. package/lib/store.mjs +323 -130
  173. package/lib/stuck.mjs +85 -7
  174. package/lib/swarm.mjs +157 -49
  175. package/lib/swarmDevices.mjs +25 -85
  176. package/lib/swarmMemory.mjs +93 -9
  177. package/lib/t3.mjs +73 -3
  178. package/lib/taskCommands.mjs +51 -60
  179. package/lib/taskRooms.mjs +30 -4
  180. package/lib/taskRules.json +29 -0
  181. package/lib/taskRules.mjs +21 -0
  182. package/lib/taskRuns.mjs +74 -11
  183. package/lib/team.mjs +2 -1
  184. package/lib/threadActions.mjs +37 -9
  185. package/lib/threadAuthority.mjs +4 -4
  186. package/lib/threadBriefs.mjs +173 -43
  187. package/lib/threadNotes.mjs +320 -0
  188. package/lib/threadRecord.mjs +491 -0
  189. package/lib/threadRuns.mjs +216 -108
  190. package/lib/threadView.mjs +27 -30
  191. package/lib/threadWatches.mjs +1962 -0
  192. package/lib/timeSense.mjs +839 -0
  193. package/lib/tripwires.mjs +454 -0
  194. package/lib/ui.mjs +1 -1
  195. package/lib/vault.mjs +560 -45
  196. package/lib/vaultCommands.mjs +5 -3
  197. package/lib/vaultPeers.mjs +165 -0
  198. package/lib/vaultTools.d.ts +4 -1
  199. package/lib/vaultTools.js +22 -1
  200. package/lib/verdicts.mjs +12 -26
  201. package/lib/versions.mjs +117 -0
  202. package/lib/waitTool.mjs +278 -0
  203. package/lib/wakePicture.mjs +268 -0
  204. package/lib/wakeQueue.mjs +108 -0
  205. package/lib/watchResponses.mjs +533 -0
  206. package/lib/watches.mjs +290 -145
  207. package/lib/windowAsks.mjs +80 -17
  208. package/lib/worktrees.mjs +159 -9
  209. package/npm-shrinkwrap.json +138 -631
  210. package/package.json +7 -6
  211. package/release.json +212 -130
  212. package/lib/callSessions.mjs +0 -51
  213. package/lib/directLink.mjs +0 -349
  214. package/lib/peopleBoard.mjs +0 -131
  215. package/lib/projects.mjs +0 -2917
  216. package/lib/roomChannels.mjs +0 -667
package/lib/watches.mjs CHANGED
@@ -1,4 +1,3 @@
1
- import { spawn } from "node:child_process";
2
1
  import { randomUUID } from "node:crypto";
3
2
 
4
3
  // Watches: something the person wants kept an eye on, read by a command on this computer on a schedule (a log, a status
@@ -121,165 +120,273 @@ export function offPattern(samples, { above = null, below = null } = {}) {
121
120
  return null;
122
121
  }
123
122
 
124
- /** Runs a check's command in its folder; its output, or an error. A check never runs longer than its own interval. */
125
- function runCheck(watch, shell = process.env.SHELL || "/bin/sh") {
126
- return new Promise((resolve) => {
127
- const started = performance.now();
128
- let out = "",
129
- err = "",
130
- done = false;
131
- const child = spawn(shell, ["-lc", watch.command], {
132
- cwd: watch.folder || undefined,
133
- stdio: ["ignore", "pipe", "pipe"],
134
- });
135
- const timer = setTimeout(() => {
136
- child.kill("SIGKILL");
137
- finish(null, `it took longer than its ${watch.everySeconds} s interval`);
138
- }, watch.everySeconds * 1000);
139
- const finish = (output, error) => {
140
- if (done) return;
141
- done = true;
142
- clearTimeout(timer);
143
- resolve({ output, error, ms: Math.round(performance.now() - started) });
144
- };
145
- child.stdout.on("data", (chunk) => {
146
- out = (out + chunk).slice(-200_000);
147
- });
148
- child.stderr.on("data", (chunk) => {
149
- err = (err + chunk).slice(-4_000);
150
- });
151
- child.on("error", (error) => finish(null, error.message));
152
- child.on("close", (code) =>
153
- finish(
154
- code === 0 || out ? out : null,
155
- code === 0 || out
156
- ? null
157
- : err.trim().split("\n").at(-1) || `it exited with code ${code}`
158
- )
159
- );
160
- });
161
- }
123
+ // The source a watch reads (sources.mjs): its command, run in its folder on its interval. Two watches on the same command
124
+ // share one run.
125
+ const sourceOf = (watch) => ({
126
+ kind: "command",
127
+ command: watch.command,
128
+ folder: watch.folder || null,
129
+ everySeconds: watch.everySeconds,
130
+ });
131
+
132
+ // A watch an agent's copy of the board brings a moment after home kept it: followed meanwhile, for at most this long.
133
+ const KEPT_GRACE_MS = 60_000;
134
+ // This computer's watches are looked at again this often, for a start or an end its copy brings from home.
135
+ const RECONCILE_MS = 10_000;
136
+
137
+ const parsed = (text, fallback) => {
138
+ try {
139
+ return JSON.parse(text) ?? fallback;
140
+ } catch {
141
+ return fallback;
142
+ }
143
+ };
144
+
145
+ /** A row of the watch table (threadWatches.mjs) as this module and the buddy read a watch. */
146
+ const watchOf = (row) => {
147
+ const source = parsed(row.source, {});
148
+ const condition = parsed(row.condition, {});
149
+ return {
150
+ id: row.id,
151
+ name: parsed(row.detail, {}).name ?? "",
152
+ command: source.command,
153
+ folder: source.folder ?? null,
154
+ everySeconds: source.everySeconds,
155
+ measure: {
156
+ ...(condition.measure ?? {}),
157
+ above: condition.above ?? null,
158
+ below: condition.below ?? null,
159
+ },
160
+ by: row.armed_by,
161
+ state: row.state === "held" ? "suggested" : row.state,
162
+ response: parsed(row.response, { do: "tell" }),
163
+ createdAt: row.created_at,
164
+ };
165
+ };
162
166
 
167
+ /**
168
+ * The person's watches: rows of kind "watch" in the watch table (threadWatches.mjs), kept on home and copied to every
169
+ * computer, each read on the computer it was set on. Each one on reads its command through a source (sources.mjs); what
170
+ * it says out loud (off, back) is a happening in the log (events.mjs), and the person is told through the response
171
+ * table (watchResponses.mjs). Its readings and alerts stay on the computer that reads it (meta `watch-samples:<id>`,
172
+ * `watch-off:<id>`, `watch-alerts`): the copy carries what a watch is, not every reading. `atHome(request)` takes a
173
+ * keep, a start or an end to home, where the table is written.
174
+ */
163
175
  export function createWatches({
164
176
  store,
177
+ sources,
178
+ events,
179
+ machineId,
180
+ atHome,
181
+ responses = null,
165
182
  notify = () => {},
166
183
  log = () => {},
167
- run = runCheck,
184
+ now = () => Date.now(),
168
185
  }) {
169
- const list = () => {
170
- try {
171
- return JSON.parse(store.meta("watches") || "[]");
172
- } catch {
173
- return [];
174
- }
175
- };
176
- const save = (watches) => store.meta("watches", JSON.stringify(watches));
177
- const samplesOf = (id) => {
178
- try {
179
- return JSON.parse(store.meta(`watch-samples:${id}`) || "[]");
180
- } catch {
181
- return [];
182
- }
186
+ const db = store.db;
187
+ const rows = () =>
188
+ db.all(
189
+ "select * from board_watches where kind = 'watch' and machine = ? and state in ('on', 'held') order by created_at",
190
+ [machineId]
191
+ );
192
+ const rowOf = (id) =>
193
+ db.get(
194
+ "select * from board_watches where id = ? and kind = 'watch' and machine = ?",
195
+ [String(id), machineId]
196
+ );
197
+ const samplesOf = (id) => parsed(store.meta(`watch-samples:${id}`), []);
198
+ const offOf = (id) => parsed(store.meta(`watch-off:${id}`), null);
199
+ const setOff = (id, alert) =>
200
+ store.meta(`watch-off:${id}`, alert ? JSON.stringify(alert) : "");
201
+ // What each watch that is on follows: the function that stops following its source.
202
+ const following = new Map();
203
+ // Watches set here that home kept, until this computer's copy of the board brings them.
204
+ const keptHere = new Map();
205
+ let timer = null;
206
+
207
+ const tell = (watch, text, data, cause) =>
208
+ events.append({
209
+ source: "watch",
210
+ kind: "watch",
211
+ subject: watch.id,
212
+ text,
213
+ data,
214
+ cause,
215
+ });
216
+
217
+ // The person is told through the response table, which keeps the alert and tells them once.
218
+ const answer = (watch, edge, text, cause) => {
219
+ if (!responses) return notify(text);
220
+ responses.fire({
221
+ watch: { id: watch.id, thread: null, response: watch.response },
222
+ edge,
223
+ text,
224
+ cause,
225
+ });
183
226
  };
184
- const timers = new Map();
185
227
 
186
- const check = async (id) => {
187
- const watch = list().find((entry) => entry.id === id);
188
- if (!watch || watch.state !== "on") return null;
189
- const result = await run(watch);
228
+ // One reading of a watch's command: the number it holds is kept, and the watch is said out loud once when it goes
229
+ // off and once when it is back, not on every reading while it stays off.
230
+ const record = (watch, reading) => {
190
231
  const value =
191
- result.output === null ? null : readNumber(result.output, watch.measure);
232
+ reading.output === null
233
+ ? null
234
+ : readNumber(reading.output, watch.measure, reading.at);
192
235
  const samples = [
193
- ...samplesOf(id),
236
+ ...samplesOf(watch.id),
194
237
  {
195
- at: Date.now(),
238
+ at: reading.at,
196
239
  value,
197
- ms: result.ms,
198
- ...(result.error
199
- ? { error: result.error.slice(0, 300) }
240
+ ms: reading.ms,
241
+ ...(reading.error
242
+ ? { error: reading.error.slice(0, 300) }
200
243
  : value === null
201
244
  ? { error: "its output had no number to read" }
202
245
  : {}),
203
246
  },
204
247
  ].slice(-HISTORY);
205
- store.meta(`watch-samples:${id}`, JSON.stringify(samples));
248
+ store.meta(`watch-samples:${watch.id}`, JSON.stringify(samples));
206
249
  const off = offPattern(samples, watch.measure);
207
- const current = list().find((entry) => entry.id === id);
208
- if (!current) return null;
209
- // Said once when it goes off and once when it is back; not on every check while it stays off.
210
- if (off && !current.off) {
250
+ const wasOff = offOf(watch.id);
251
+ if (off && !wasOff) {
211
252
  const alert = {
212
- at: new Date().toISOString(),
213
- text: `${current.name} ${off.text}.`,
253
+ at: new Date(now()).toISOString(),
254
+ text: `${watch.name} ${off.text}.`,
214
255
  kind: off.kind,
215
256
  };
216
- save(
217
- list().map((entry) =>
218
- entry.id === id ? { ...entry, off: alert } : entry
219
- )
220
- );
257
+ setOff(watch.id, alert);
221
258
  store.meta(
222
259
  "watch-alerts",
223
260
  JSON.stringify(
224
261
  [
225
- ...JSON.parse(store.meta("watch-alerts") || "[]"),
226
- { watch: id, ...alert },
262
+ ...parsed(store.meta("watch-alerts"), []),
263
+ { watch: watch.id, ...alert },
227
264
  ].slice(-200)
228
265
  )
229
266
  );
230
- notify(alert.text, { watch: current, alert });
231
- log(`watch ${current.name}: ${off.text}`);
232
- } else if (!off && current.off) {
233
- save(
234
- list().map((entry) =>
235
- entry.id === id ? { ...entry, off: null } : entry
236
- )
267
+ const told = tell(
268
+ watch,
269
+ alert.text,
270
+ { state: "off", kind: off.kind, value },
271
+ reading.event
237
272
  );
238
- notify(`${current.name} is back to normal.`, {
239
- watch: current,
240
- alert: null,
241
- });
273
+ answer(watch, "tripped", alert.text, told?.seq ?? reading.event ?? null);
274
+ log(`watch ${watch.name}: ${off.text}`);
275
+ } else if (!off && wasOff) {
276
+ setOff(watch.id, null);
277
+ const text = `${watch.name} is back to normal.`;
278
+ const told = tell(watch, text, { state: "back", value }, reading.event);
279
+ answer(watch, "cleared", text, told?.seq ?? reading.event ?? null);
242
280
  }
243
- return { value, off, ms: result.ms };
244
281
  };
245
282
 
246
- const schedule = (watch) => {
247
- clearTimeout(timers.get(watch.id));
248
- if (watch.state !== "on") {
249
- timers.delete(watch.id);
283
+ const follow = (watch) => {
284
+ if (following.has(watch.id)) return;
285
+ following.set(
286
+ watch.id,
287
+ sources.subscribe(sourceOf(watch), (reading) => {
288
+ try {
289
+ // Read as it stands now: a watch ended a moment ago says nothing more.
290
+ const row = rowOf(watch.id);
291
+ if (row && row.state !== "on") return;
292
+ record(row ? watchOf(row) : watch, reading);
293
+ } catch (error) {
294
+ log(`watch ${watch.name}: ${error.message}`);
295
+ }
296
+ })
297
+ );
298
+ };
299
+ const unfollow = (id) => {
300
+ following.get(id)?.();
301
+ following.delete(id);
302
+ };
303
+
304
+ /** Follows this computer's watches that are on, as the table says, and stops following the rest. */
305
+ const reconcile = () => {
306
+ let on;
307
+ try {
308
+ on = rows()
309
+ .filter((row) => row.state === "on")
310
+ .map(watchOf);
311
+ } catch {
250
312
  return;
251
313
  }
252
- const tick = () => {
253
- void check(watch.id)
254
- .catch((error) => log(`watch ${watch.name}: ${error.message}`))
255
- .finally(() => {
256
- if (timers.has(watch.id))
257
- timers.set(
258
- watch.id,
259
- setTimeout(tick, watch.everySeconds * 1000).unref?.() ?? null
260
- );
261
- });
262
- };
263
- timers.set(watch.id, setTimeout(tick, 0));
314
+ const wanted = new Set(on.map((watch) => watch.id));
315
+ for (const [id, at] of keptHere)
316
+ if (wanted.has(id) || now() - at > KEPT_GRACE_MS) keptHere.delete(id);
317
+ else wanted.add(id);
318
+ for (const id of [...following.keys()]) if (!wanted.has(id)) unfollow(id);
319
+ for (const watch of on) follow(watch);
264
320
  };
265
321
 
266
- return {
267
- list: () =>
268
- list().map((watch) => ({
269
- ...watch,
270
- samples: samplesOf(watch.id).slice(-20),
271
- })),
272
- alerts: (since = "") => {
273
- try {
274
- return JSON.parse(store.meta("watch-alerts") || "[]").filter(
275
- (alert) => alert.at > since
276
- );
277
- } catch {
278
- return [];
322
+ const listed = (watch) => ({
323
+ ...watch,
324
+ off: offOf(watch.id),
325
+ samples: samplesOf(watch.id).slice(-20),
326
+ });
327
+
328
+ // Today's watches, kept in this computer's meta before they were rows: each moves in once, with its id, so its
329
+ // readings and alerts go on as they were. The meta is left as it was.
330
+ const moveIn = async () => {
331
+ if (store.meta("watches-moved") === "1") return;
332
+ const before = parsed(store.meta("watches"), []);
333
+ let all = true;
334
+ for (const watch of before) {
335
+ if (db.get("select id from board_watches where id = ?", [watch.id]))
336
+ continue;
337
+ const kept = await keepRow(
338
+ rowFor({ ...watch, state: watch.state === "on" ? "on" : "held" }),
339
+ watch.by || "user"
340
+ );
341
+ if (!kept.ok) {
342
+ all = false;
343
+ log(`watch ${watch.name}: not moved yet: ${kept.error}`);
344
+ continue;
279
345
  }
346
+ if (watch.off) setOff(watch.id, watch.off);
347
+ }
348
+ if (all) store.meta("watches-moved", "1");
349
+ };
350
+
351
+ const rowFor = (watch) => ({
352
+ id: watch.id,
353
+ thread: null,
354
+ answers: "user",
355
+ source: sourceOf(watch),
356
+ condition: {
357
+ when: "off",
358
+ measure: Object.fromEntries(
359
+ ["field", "count", "age"]
360
+ .filter((name) => watch.measure?.[name])
361
+ .map((name) => [name, watch.measure[name]])
362
+ ),
363
+ above: watch.measure?.above ?? null,
364
+ below: watch.measure?.below ?? null,
280
365
  },
366
+ kind: "watch",
367
+ response: { do: "tell" },
368
+ detail: { name: watch.name },
369
+ state: watch.state,
370
+ why: watch.state === "held" ? "an agent suggested it" : null,
371
+ });
372
+
373
+ const keepRow = async (row, by) => {
374
+ const kept = await atHome({
375
+ command: "board.watch.keep",
376
+ agent: by,
377
+ watch: row,
378
+ });
379
+ return kept?.ok ? kept : { ok: false, error: kept?.error ?? "not kept" };
380
+ };
381
+
382
+ return {
383
+ list: () => rows().map((row) => listed(watchOf(row))),
384
+ alerts: (since = "") =>
385
+ parsed(store.meta("watch-alerts"), []).filter(
386
+ (alert) => alert.at > since
387
+ ),
281
388
  /** A new watch: the person's starts at once; an agent's waits for the person's start. */
282
- add({
389
+ async add({
283
390
  name,
284
391
  command,
285
392
  folder = null,
@@ -325,38 +432,76 @@ export function createWatches({
325
432
  everySeconds: every,
326
433
  measure: clean,
327
434
  by,
328
- state: by === "user" ? "on" : "suggested",
329
- off: null,
330
- createdAt: new Date().toISOString(),
435
+ state: by === "user" ? "on" : "held",
436
+ response: { do: "tell" },
437
+ };
438
+ const kept = await keepRow(rowFor(watch), by);
439
+ if (!kept.ok) return kept;
440
+ if (watch.state === "on") {
441
+ keptHere.set(watch.id, now());
442
+ follow(watch);
443
+ }
444
+ return {
445
+ ok: true,
446
+ watch: listed({
447
+ ...watch,
448
+ state: watch.state === "held" ? "suggested" : "on",
449
+ createdAt: kept.watch?.createdAt ?? null,
450
+ }),
331
451
  };
332
- save([...list(), watch]);
333
- if (watch.state === "on") schedule(watch);
334
- return { ok: true, watch };
335
452
  },
336
- start(id) {
337
- const watch = list().find((entry) => entry.id === id);
338
- if (!watch) return { ok: false, error: "no such watch" };
339
- const on = { ...watch, state: "on" };
340
- save(list().map((entry) => (entry.id === id ? on : entry)));
341
- schedule(on);
342
- return { ok: true, watch: on };
453
+ async start(id) {
454
+ const row = rowOf(id);
455
+ if (!row || !["on", "held"].includes(row.state))
456
+ return { ok: false, error: "no such watch" };
457
+ if (row.state === "held") {
458
+ const went = await atHome({
459
+ command: "board.watch.go",
460
+ agent: "user",
461
+ id: row.id,
462
+ });
463
+ if (!went?.ok)
464
+ return { ok: false, error: went?.error ?? "not started" };
465
+ }
466
+ const on = { ...watchOf(row), state: "on" };
467
+ keptHere.set(on.id, now());
468
+ follow(on);
469
+ return { ok: true, watch: listed(on) };
343
470
  },
344
- remove(id) {
345
- if (!list().some((entry) => entry.id === id))
471
+ async remove(id) {
472
+ const row = rowOf(id);
473
+ if (!row || !["on", "held"].includes(row.state))
346
474
  return { ok: false, error: "no such watch" };
347
- clearTimeout(timers.get(id));
348
- timers.delete(id);
349
- save(list().filter((entry) => entry.id !== id));
350
- store.meta(`watch-samples:${id}`, "[]");
475
+ const ended = await atHome({
476
+ command: "board.watch.drop",
477
+ agent: "user",
478
+ id: row.id,
479
+ });
480
+ if (!ended?.ok)
481
+ return { ok: false, error: ended?.error ?? "not removed" };
482
+ keptHere.delete(row.id);
483
+ unfollow(row.id);
484
+ store.meta(`watch-samples:${row.id}`, "[]");
485
+ setOff(row.id, null);
351
486
  return { ok: true };
352
487
  },
353
- check,
354
- resume() {
355
- for (const watch of list()) if (watch.state === "on") schedule(watch);
488
+ reconcile,
489
+ /** Moves today's watches in, then follows this computer's own and looks again for what its copy brings. */
490
+ async resume() {
491
+ await moveIn().catch((error) => log(`watches: ${error.message}`));
492
+ reconcile();
493
+ clearInterval(timer);
494
+ timer = setInterval(() => {
495
+ reconcile();
496
+ if (store.meta("watches-moved") !== "1")
497
+ void moveIn().catch((error) => log(`watches: ${error.message}`));
498
+ }, RECONCILE_MS);
499
+ timer.unref?.();
356
500
  },
357
501
  stop() {
358
- for (const timer of timers.values()) clearTimeout(timer);
359
- timers.clear();
502
+ clearInterval(timer);
503
+ for (const stopFollowing of following.values()) stopFollowing();
504
+ following.clear();
360
505
  },
361
506
  };
362
507
  }
@@ -1,49 +1,112 @@
1
+ import { BROWSER_AGENT_DEADLINES } from "./browserAgentTools.mjs";
2
+
1
3
  // This computer asking the person's own Harness window for what only it holds (phones.mjs ask): this computer's own
2
- // windows first, then those linked to each of the person's other computers (ownMachines.mjs). With no Harness window
3
- // open, the browser that listens for this computer (browserWake.mjs, the extension's native host) is woken: the
4
- // extension brings up its hidden Harness page, which answers like a tab (the swarm reads what the person has seen from
5
- // the hidden page). It is asked again as it comes up, until WAKE_DEADLINE_MS.
4
+ // windows first, then those on the person's other computers, each asked by its own computer over their link
5
+ // (ownMachines.mjs askOtherWindows). A question may name one browser (`device`, by id or name, with the computer it was
6
+ // last known on), and bring its own deadline (`timeoutMs`, a door call's). With no Harness window
7
+ // open, the browser is woken: through the extension when it listens for this computer (browserWake.mjs, its native
8
+ // host brings up its hidden Harness page, out of sight), and otherwise by this computer opening Harness in the default
9
+ // browser (owner, November 12: "the extension is not load bearing in any sense at all since it is inherently opt in").
10
+ // Only an agent's own ask opens a window; a background read (the dream's, seen.mjs) never puts a browser in front of
11
+ // the person. The window is asked again as it comes up, until WAKE_DEADLINE_MS.
6
12
 
7
13
  // How long to keep asking a woken hidden page: one minute, a chosen value. Measured only against a local app on
8
14
  // loopback (an end-to-end test: closed page to answer 1.04 to 1.08 s, 5 samples on an M5 Pro), never against
9
15
  // tryharness.ai.
10
16
  export const WAKE_DEADLINE_MS = 60_000;
11
- // What home may ask the windows linked to a worker (ownMachines.mjs): the person's screen history, as its agents' tools
12
- // ask it (boardTools.mjs search_screen, seen.mjs). A worker refuses any other tool home names (security audit
17
+ // The bytes of a document Harness generated in a browser (/api/mcp/file for an outside agent).
18
+ export const FILE_BYTES_TOOL = "get_generated_file_bytes";
19
+ // The reads an agent on one of the person's computers makes of a browser through that computer (deviceDoor.mjs): the
20
+ // same list as src/shared/doorTools.ts DEVICE_READ_TOOLS, which an outside agent makes through Harness.
21
+ export const AGENT_READ_TOOLS = new Set([
22
+ "search_screen_semantic",
23
+ "search_screen_visual",
24
+ "search_screen_text",
25
+ "recall_at_time",
26
+ "get_frames",
27
+ "get_transcript",
28
+ "query_screen_history",
29
+ "read_conversation",
30
+ "search_conversations",
31
+ "recall_memories",
32
+ "list_generated_media",
33
+ "get_generated_image",
34
+ ]);
35
+ // What one of the person's computers may ask the windows on another (ownMachines.mjs): the person's screen history, as
36
+ // its agents' tools ask it (boardTools.mjs search_screen, seen.mjs, deviceDoor.mjs), and a door call Harness's server
37
+ // handed home for a browser (`door`, deviceDoor.mjs). A computer refuses any other tool another names (security audit
13
38
  // 2026-10-25, M6).
14
39
  export const WINDOW_TOOLS = new Set([
15
40
  "search_screen_text",
16
41
  "search_screen_semantic",
17
42
  "screen_seen_since",
43
+ ...AGENT_READ_TOOLS,
44
+ // A document Harness made in a browser, downloaded by an agent here (mcpRelay.mjs download, deviceDoor.mjs).
45
+ FILE_BYTES_TOOL,
46
+ // An agent's page read or page task on a browser this computer runs (browserAgentTools.mjs).
47
+ ...Object.keys(BROWSER_AGENT_DEADLINES),
18
48
  ]);
49
+ /** Whether another of the person's computers may ask this one's windows `call`. */
50
+ export const mayAskWindows = (call) =>
51
+ WINDOW_TOOLS.has(call?.tool) ||
52
+ (typeof call?.door?.op === "string" && call.door.caller != null);
19
53
  const ASK_EVERY_MS = 1_000;
20
54
 
21
55
  export function createWindowAsks({
22
56
  askOwn,
57
+ // The person's other computers' windows, each asked by its own computer (ownMachines.mjs askOtherWindows).
23
58
  askWorkers = async () => null,
24
59
  browserWake,
60
+ // Opens Harness in this computer's default browser (openHere.mjs); null where nothing can be opened.
61
+ openHarness = null,
25
62
  log = () => {},
26
63
  now = () => Date.now(),
27
64
  wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
28
65
  }) {
29
66
  const nobodyAt = (reply) =>
30
67
  reply?.ok === false && ["unpaired", "closed"].includes(reply.code);
31
- const askAnywhere = async (call) => {
32
- const here = await askOwn(call);
33
- return (nobodyAt(here) && (await askWorkers(call))) || here;
68
+ const askAnywhere = async (call, route) => {
69
+ const here = await askOwn(call, route);
70
+ return (nobodyAt(here) && (await askWorkers(call, route))) || here;
71
+ };
72
+
73
+ // One opened window is waited for, not opened again for every ask that comes meanwhile.
74
+ let openedAt = -Infinity;
75
+ const wake = (call, background) => {
76
+ if (browserWake?.listening()) {
77
+ browserWake.send({ kind: "window" });
78
+ log(`windows: none open, woke the browser for ${call.tool}`);
79
+ return true;
80
+ }
81
+ if (!openHarness || background) return false;
82
+ if (now() - openedAt >= WAKE_DEADLINE_MS) {
83
+ openedAt = now();
84
+ openHarness();
85
+ log(`windows: none open, opened Harness in the browser for ${call.tool}`);
86
+ }
87
+ return true;
34
88
  };
35
89
 
36
- /** The window's answer to `call`, waking the hidden page when no window is open and a browser listens. */
37
- return async function ask(call) {
38
- const first = await askAnywhere(call);
39
- if (first?.code !== "closed" || !browserWake?.listening()) return first;
40
- browserWake.send({ kind: "window" });
41
- log(`windows: none open, woke the browser for ${call.tool}`);
42
- const until = now() + WAKE_DEADLINE_MS;
90
+ /**
91
+ * The window's answer to `call`, waking a browser when no window is open. A question for one named browser wakes
92
+ * nothing: the browser woken here may not be the one named.
93
+ */
94
+ return async function ask({
95
+ background = false,
96
+ device = null,
97
+ computer = null,
98
+ timeoutMs = undefined,
99
+ ...call
100
+ }) {
101
+ const route = { device, computer, timeoutMs };
102
+ const first = await askAnywhere(call, route);
103
+ if (first?.code !== "closed" || device || !wake(call, background))
104
+ return first;
105
+ const until = now() + Math.min(WAKE_DEADLINE_MS, timeoutMs ?? Infinity);
43
106
  let reply = first;
44
107
  while (reply?.code === "closed" && now() < until) {
45
108
  await wait(ASK_EVERY_MS);
46
- reply = await askAnywhere(call);
109
+ reply = await askAnywhere(call, route);
47
110
  }
48
111
  return reply;
49
112
  };