tryharness 0.7.54 → 0.7.58

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 (156) hide show
  1. package/README.md +15 -16
  2. package/cli.mjs +384 -62
  3. package/lib/activity.mjs +10 -10
  4. package/lib/adapters.mjs +16 -0
  5. package/lib/agentBinaries.mjs +71 -0
  6. package/lib/agentRuntimeConfig.mjs +1 -1
  7. package/lib/agentRuntimes.mjs +9 -3
  8. package/lib/agentSignIn.mjs +271 -41
  9. package/lib/answers.mjs +5 -5
  10. package/lib/archetypes.d.ts +64 -0
  11. package/lib/archetypes.js +989 -0
  12. package/lib/asks.mjs +4 -4
  13. package/lib/audit.mjs +36 -13
  14. package/lib/backupSetup.mjs +4 -2
  15. package/lib/boardAccess.mjs +17 -11
  16. package/lib/boardBackup.mjs +6 -6
  17. package/lib/boardFileCommands.mjs +6 -6
  18. package/lib/boardFiles.mjs +13 -12
  19. package/lib/boardMirror.mjs +82 -47
  20. package/lib/boardProtocol.json +30 -1
  21. package/lib/boardWaits.mjs +2 -3
  22. package/lib/book.mjs +6 -6
  23. package/lib/bridge.mjs +5 -5
  24. package/lib/cards.mjs +2 -2
  25. package/lib/cloudAgents.mjs +216 -0
  26. package/lib/cloudAi.mjs +1312 -0
  27. package/lib/cloudAssistants.mjs +242 -0
  28. package/lib/cloudBoot.mjs +81 -86
  29. package/lib/cloudSave.mjs +197 -0
  30. package/lib/cloudSsh.mjs +178 -0
  31. package/lib/context.mjs +3 -3
  32. package/lib/contractGate.mjs +34 -12
  33. package/lib/control-client.mjs +9 -1
  34. package/lib/controlSocket.mjs +86 -0
  35. package/lib/coordinator.mjs +103 -101
  36. package/lib/daemon.mjs +1128 -2865
  37. package/lib/desk.mjs +7 -6
  38. package/lib/desktop.mjs +72 -68
  39. package/lib/directLink.mjs +16 -5
  40. package/lib/environment.mjs +2 -1
  41. package/lib/environments.mjs +14 -6
  42. package/lib/firewall.mjs +9 -9
  43. package/lib/folders.mjs +18 -14
  44. package/lib/frames.mjs +2 -3
  45. package/lib/gitHost.mjs +3 -3
  46. package/lib/goalCommands.mjs +25 -7
  47. package/lib/goals.mjs +7 -7
  48. package/lib/guestRequests.mjs +94 -0
  49. package/lib/handoffs.mjs +8 -7
  50. package/lib/handover.mjs +4 -4
  51. package/lib/harnessModel.mjs +6 -6
  52. package/lib/harnessOffers.mjs +266 -0
  53. package/lib/inference.mjs +26 -8
  54. package/lib/jobAnswers.mjs +190 -0
  55. package/lib/landed.mjs +1 -1
  56. package/lib/lane.mjs +42 -11
  57. package/lib/lessons.mjs +16 -16
  58. package/lib/library.mjs +3 -3
  59. package/lib/lineup.mjs +3 -0
  60. package/lib/linkRelay.mjs +574 -0
  61. package/lib/localAgentAsk.mjs +74 -0
  62. package/lib/localStack.mjs +536 -0
  63. package/lib/localmodels.mjs +6 -4
  64. package/lib/loopBackstop.mjs +4 -4
  65. package/lib/machineIdentity.mjs +9 -1
  66. package/lib/machineRoutes.mjs +181 -2
  67. package/lib/machineRuns.mjs +76 -1
  68. package/lib/machineSeal.mjs +39 -12
  69. package/lib/mcp-stdio.mjs +96 -26
  70. package/lib/mcpRelay.mjs +1 -1
  71. package/lib/members.json +13 -0
  72. package/lib/members.mjs +1 -1
  73. package/lib/models.mjs +77 -23
  74. package/lib/museSession.mjs +6 -1
  75. package/lib/nativeHost.mjs +35 -7
  76. package/lib/network.mjs +50 -5
  77. package/lib/notify.mjs +7 -5
  78. package/lib/nudge.mjs +3 -3
  79. package/lib/ocr.mjs +9 -9
  80. package/lib/onboard.mjs +141 -50
  81. package/lib/oneLine.mjs +7 -7
  82. package/lib/ownMachines.mjs +928 -149
  83. package/lib/pairing.mjs +39 -8
  84. package/lib/paths.mjs +15 -1
  85. package/lib/peerProcess.mjs +124 -0
  86. package/lib/personModel.mjs +3 -3
  87. package/lib/personNotices.mjs +151 -0
  88. package/lib/phones.mjs +490 -38
  89. package/lib/placeCommands.mjs +4 -4
  90. package/lib/places.mjs +70 -2
  91. package/lib/profile.mjs +67 -14
  92. package/lib/projectGit.mjs +158 -14
  93. package/lib/projectPieces.mjs +902 -0
  94. package/lib/projectSettings.mjs +7 -4
  95. package/lib/projects.mjs +384 -62
  96. package/lib/provenance.mjs +5 -1
  97. package/lib/proxyLinks.mjs +267 -0
  98. package/lib/reactions.mjs +7 -5
  99. package/lib/readiness.mjs +12 -7
  100. package/lib/record.mjs +19 -6
  101. package/lib/redact.mjs +4 -4
  102. package/lib/relayLimits.mjs +62 -0
  103. package/lib/release.mjs +170 -0
  104. package/lib/releaseKeys.json +6 -0
  105. package/lib/repoLinks.mjs +4 -4
  106. package/lib/roomChannels.mjs +24 -0
  107. package/lib/roomPage.mjs +1 -1
  108. package/lib/runAuthority.mjs +101 -0
  109. package/lib/runBrief.mjs +19 -8
  110. package/lib/runWorker.mjs +14 -5
  111. package/lib/runner.mjs +215 -25
  112. package/lib/runtime-install.mjs +143 -9
  113. package/lib/schedules.mjs +2 -2
  114. package/lib/screens.mjs +1 -1
  115. package/lib/sealed.mjs +11 -2
  116. package/lib/sealedThreads.mjs +671 -0
  117. package/lib/secretPatterns.json +1 -1
  118. package/lib/secrets.mjs +4 -4
  119. package/lib/seen.mjs +6 -6
  120. package/lib/serverWords.mjs +175 -0
  121. package/lib/service.mjs +22 -12
  122. package/lib/sessionControl.mjs +6 -6
  123. package/lib/specialties.mjs +6 -6
  124. package/lib/specialtyKeys.js +4 -5
  125. package/lib/specialtyNotice.mjs +7 -7
  126. package/lib/specialtySnapshot.mjs +5 -5
  127. package/lib/speech.mjs +5 -5
  128. package/lib/store.mjs +140 -39
  129. package/lib/stuck.mjs +1 -1
  130. package/lib/swarm.mjs +167 -16
  131. package/lib/swarmDevices.mjs +319 -0
  132. package/lib/swarmMemory.mjs +27 -25
  133. package/lib/t3.mjs +13 -13
  134. package/lib/taskCommands.mjs +44 -11
  135. package/lib/taskRooms.mjs +125 -0
  136. package/lib/taskRuns.mjs +36 -1
  137. package/lib/team.mjs +41 -18
  138. package/lib/threadActions.mjs +315 -0
  139. package/lib/threadAuthority.mjs +101 -0
  140. package/lib/threadBriefs.mjs +178 -0
  141. package/lib/threadLine.mjs +2 -2
  142. package/lib/threadRuns.mjs +138 -43
  143. package/lib/threadView.mjs +5 -5
  144. package/lib/usageTotals.mjs +2 -3
  145. package/lib/vault.mjs +101 -10
  146. package/lib/vaultCloud.mjs +2 -2
  147. package/lib/vaultCommands.mjs +15 -6
  148. package/lib/vaultTools.js +2 -2
  149. package/lib/verdicts.mjs +328 -0
  150. package/lib/wakes.mjs +1 -1
  151. package/lib/watches.mjs +2 -2
  152. package/lib/windowAsks.mjs +13 -5
  153. package/lib/worktrees.mjs +112 -18
  154. package/npm-shrinkwrap.json +1093 -0
  155. package/package.json +11 -10
  156. package/release.json +220 -0
@@ -0,0 +1,536 @@
1
+ import fs from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { execFile } from "node:child_process";
5
+ import { ATLAS_VERSION } from "./atlas.mjs";
6
+ import { createContextEngine } from "./context.mjs";
7
+ import { createCoreModels } from "./coreModels.mjs";
8
+ import { readJson, writeJsonAtomic } from "./files.mjs";
9
+ import { createFirewall } from "./firewall.mjs";
10
+ import { createInference } from "./inference.mjs";
11
+ import { listen } from "./listen.mjs";
12
+ import { ensureModels, modelsPresent } from "./localmodels.mjs";
13
+ import { createOcr, imageKind } from "./ocr.mjs";
14
+ import { createAppleSpeech, createSpeech } from "./speech.mjs";
15
+ import { fromBlob, toBlob, topK } from "./vectors.mjs";
16
+ import { watchVideo } from "./video.mjs";
17
+
18
+ // The folders searched by what is in their files, unless the person turned search off: where people keep their own.
19
+ const OWN_FOLDERS = ["Desktop", "Documents", "Downloads"];
20
+ const EMBED_EVERY_MS = 2_000;
21
+ const EMBED_BATCH = 16;
22
+ // How long the names speech is corrected against are kept before they are read again.
23
+ const SPEECH_NAMES_MS = 5 * 60_000;
24
+ // Commands this module answers on the control socket.
25
+ const COMMANDS = new Set([
26
+ "local.status",
27
+ "local.learn",
28
+ "local.on",
29
+ "local.off",
30
+ "local.add",
31
+ "local.remove",
32
+ "voice.status",
33
+ "voice.transcribe",
34
+ "image.read",
35
+ ]);
36
+
37
+ /**
38
+ * The relay's local models and everything that reads with them. Setup asks for the core set, which this daemon
39
+ * downloads into `<home>/local` in the background (coreModels.mjs); until a model is present, what needs it says so or
40
+ * goes as it did before.
41
+ *
42
+ * - The context space (search): the search model embeds it in the background, a batch at a time, so no request waits
43
+ * on it; until it is present, search answers from words and says so.
44
+ * - The context engine (context.mjs): the atlas of the folders the swarm works in, what every coding session there
45
+ * needed, a context pack for each local agent's brief, the overnight learner, and the library of the person's own
46
+ * files. Documents and PDFs need only the search model; pictures, recordings and video wait for its image and audio
47
+ * part, which comes down on the charger.
48
+ * - The firewall (firewall.mjs): reads what agents hand each other for planted instructions, and scrubs what leaves for
49
+ * a hosted agent.
50
+ * - Reading pictures (ocr.mjs), push-to-talk (speech.mjs), and watching and listening to files for agents here.
51
+ *
52
+ * Nothing here is sent anywhere. `folders()` is the folders the person works in (folders.mjs); `roomOf(task)` the
53
+ * thread a task is in and `namedPath(file)` a thread file's path under its own name, both made after this module.
54
+ */
55
+ export function createLocalStack({
56
+ store,
57
+ home,
58
+ configPath,
59
+ filesRoot,
60
+ folders,
61
+ roomOf,
62
+ namedPath,
63
+ quiet,
64
+ notify,
65
+ localEvent,
66
+ log,
67
+ }) {
68
+ const root = path.join(home, "local");
69
+ const inference = createInference({ root, log });
70
+ const ready = (ids) => modelsPresent(root, ids);
71
+ const coreModels = createCoreModels({
72
+ root,
73
+ wanted: () => readJson(configPath, {}).localModels === "download",
74
+ log,
75
+ });
76
+ const ocr = createOcr({ inference });
77
+ const firewall = createFirewall({ inference, log });
78
+
79
+ const embedderReady = () => ready(["search"]);
80
+ let embedding = false;
81
+ const embedPending = async () => {
82
+ if (embedding || quiet() || !embedderReady()) return;
83
+ embedding = true;
84
+ try {
85
+ const batch = store.contributionsToEmbed(ATLAS_VERSION, EMBED_BATCH);
86
+ if (batch.length) {
87
+ const vectors = await inference.embed(
88
+ batch.map((entry) => ({
89
+ kind: "document",
90
+ title: "none",
91
+ text: entry.text,
92
+ })),
93
+ { lane: "background" }
94
+ );
95
+ batch.forEach((entry, i) =>
96
+ store.storeVector(entry.id, ATLAS_VERSION, toBlob(vectors[i]))
97
+ );
98
+ }
99
+ } catch (error) {
100
+ log(`embedding failed: ${error.message}`);
101
+ } finally {
102
+ embedding = false;
103
+ }
104
+ };
105
+
106
+ /** Texts as the search model reads them, or null until it is here. */
107
+ const embed = async (texts, kind, { lane = "background" } = {}) =>
108
+ embedderReady()
109
+ ? inference.embed(
110
+ texts.map((text) =>
111
+ kind === "document" ? { kind, title: "none", text } : { kind, text }
112
+ ),
113
+ { lane }
114
+ )
115
+ : null;
116
+
117
+ /** A task's context closest to `query`: by vectors once the search model is here, by words until then. */
118
+ const searchContext = async (taskId, query, k) => {
119
+ const text = store.searchContextText(taskId, query, k);
120
+ const context = store.contextCount(taskId);
121
+ if (!embedderReady())
122
+ return {
123
+ retrieval: "text",
124
+ embedder: "absent",
125
+ context,
126
+ entries: text.map(({ rank, ...entry }) => ({
127
+ ...entry,
128
+ score: Number(entry.score.toFixed(3)),
129
+ })),
130
+ };
131
+ const [queryVector] = await inference.embed(
132
+ [{ kind: "query", text: query }],
133
+ { lane: "interactive" }
134
+ );
135
+ const rows = store
136
+ .vectorsFor(taskId, ATLAS_VERSION)
137
+ .map((row) => ({ id: row.id, vector: fromBlob(row.vector) }));
138
+ const best = topK(queryVector, rows, k);
139
+ const byId = new Map(best.map((hit) => [hit.id, hit.score]));
140
+ // Words still count: a text hit with no vector yet rides along below the vector hits.
141
+ for (const hit of text) if (!byId.has(hit.id)) byId.set(hit.id, 0);
142
+ const entries = [...byId.entries()]
143
+ .map(([id, score]) => ({
144
+ ...store.getContribution(id),
145
+ score: Number(score.toFixed(3)),
146
+ }))
147
+ .filter((entry) => entry.id)
148
+ .sort((a, b) => b.score - a.score)
149
+ .slice(0, k);
150
+ return {
151
+ retrieval: rows.length ? "vectors" : "text",
152
+ embedder: "ready",
153
+ context,
154
+ entries: entries.map(({ seq, ...entry }) => entry),
155
+ };
156
+ };
157
+
158
+ // Where people keep their own files and the folders they added (tryharness local add), unless they turned search off.
159
+ const libraryFolders = () => {
160
+ const config = readJson(configPath, {});
161
+ if (config.libraryOff === true) return [];
162
+ const added = Array.isArray(config.localFolders) ? config.localFolders : [];
163
+ const own = OWN_FOLDERS.map((name) => path.join(os.homedir(), name));
164
+ return [...new Set([...own, ...added])].filter(
165
+ (folder) => typeof folder === "string" && fs.existsSync(folder)
166
+ );
167
+ };
168
+
169
+ let mediaDownload = null;
170
+ const fetchMedia = () => {
171
+ if (ready(["media"])) return Promise.resolve();
172
+ mediaDownload ||= ensureModels({ root, ids: ["media"], log })
173
+ .then(
174
+ () => {
175
+ log("media: the image and audio part of the search model is ready");
176
+ },
177
+ (error) => {
178
+ log(`media: the download failed (${error.message})`);
179
+ throw error;
180
+ }
181
+ )
182
+ .finally(() => {
183
+ mediaDownload = null;
184
+ });
185
+ return mediaDownload;
186
+ };
187
+
188
+ // Every file the threads hold, for the library: read from its stored copy under its own name.
189
+ const libraryThreadFiles = () =>
190
+ store.presentFiles().map((file) => ({
191
+ id: file.id,
192
+ thread: roomOf(store.getTask(file.task_id))?.id ?? file.task_id,
193
+ name: file.name,
194
+ sha256: file.sha256,
195
+ path: namedPath(file),
196
+ }));
197
+
198
+ const contextEngine = createContextEngine({
199
+ store,
200
+ inference,
201
+ home: root,
202
+ known: () => folders().known(),
203
+ libraryFolders,
204
+ threadFiles: libraryThreadFiles,
205
+ mediaReady: () => ready(["search", "media"]),
206
+ // Pictures and sound waiting: the image and audio part comes down on the charger, then they are read.
207
+ wantMedia: () => {
208
+ if (!inference.power.onCharger() || inference.power.paused()) return;
209
+ void fetchMedia()
210
+ .then(() => contextEngine.refreshFiles())
211
+ .catch(() => {});
212
+ },
213
+ ocr,
214
+ mask: async (text) =>
215
+ (await firewall.scrub(text, { lane: "interactive" })).text,
216
+ log,
217
+ notify,
218
+ });
219
+
220
+ // A thread file is read soon after it arrives; several arriving together are read in one pass.
221
+ let filesSoon = null;
222
+ const readFilesSoon = () => {
223
+ clearTimeout(filesSoon);
224
+ filesSoon = setTimeout(() => {
225
+ if (ready(["search"])) void contextEngine.refreshFiles().catch(() => {});
226
+ }, 2_000);
227
+ filesSoon.unref?.();
228
+ };
229
+
230
+ // A library hit as an agent on this computer reads it: a path it can open, and where in the file.
231
+ const localHit = ({ vec, file, thread, ...hit }) => {
232
+ const stored = thread ? store.file(file) : null;
233
+ return {
234
+ ...hit,
235
+ path: stored ? namedPath(stored) : file,
236
+ ...(thread ? { thread, fileId: file } : {}),
237
+ };
238
+ };
239
+
240
+ /**
241
+ * The thread's own files closest to a question, beside its written context. A caller through Harness finds only the
242
+ * files shared with cloud agents, by id (get_file opens them); an agent here gets the path too.
243
+ */
244
+ const threadFileHits = async (task, query, hostedCaller) => {
245
+ if (!ready(["search"]) || !query.trim()) return [];
246
+ const thread = roomOf(task)?.id ?? task.id;
247
+ const readable = (fileId) => {
248
+ const file = store.file(fileId);
249
+ return Boolean(file) && (!hostedCaller || file.cloud);
250
+ };
251
+ try {
252
+ const hits = await contextEngine.searchFiles(query, {
253
+ threads: [thread],
254
+ allow: readable,
255
+ limit: 5,
256
+ });
257
+ return hits.map((hit) =>
258
+ hostedCaller
259
+ ? {
260
+ fileId: hit.file,
261
+ name: hit.name,
262
+ place: hit.place,
263
+ // A picture's text could name what its covered copy hides: get_file opens the covered picture.
264
+ excerpt: hit.modality === "image" ? null : hit.excerpt,
265
+ score: hit.score,
266
+ }
267
+ : localHit(hit)
268
+ );
269
+ } catch (error) {
270
+ log(`thread files search: ${error.message}`);
271
+ return [];
272
+ }
273
+ };
274
+
275
+ /** The guard's reading of words an agent hands the swarm, as a post's is read (goalCommands.mjs). */
276
+ const readByGuard = (text) =>
277
+ ready(["guard"])
278
+ ? firewall.inspect(text)
279
+ : Promise.resolve({ score: null, verdict: "clean", unavailable: true });
280
+
281
+ // Push-to-talk is heard here and never sent anywhere. Its models come down the first time the person holds to talk.
282
+ // Names are put right against the folders the person works in and the files in them (git's list, read at most every
283
+ // few minutes and only when they talk); the agents' names are speech.mjs's own.
284
+ let spoken = { at: 0, names: null };
285
+ const speechNames = async () => {
286
+ if (spoken.names && Date.now() - spoken.at < SPEECH_NAMES_MS)
287
+ return spoken.names;
288
+ const names = new Set();
289
+ for (const place of folders().known().slice(0, 12)) {
290
+ names.add(place.name);
291
+ const listed = await new Promise((resolve) =>
292
+ execFile(
293
+ "git",
294
+ ["-C", place.path, "ls-files", "-z"],
295
+ { encoding: "utf8", maxBuffer: 64 * 2 ** 20, timeout: 3_000 },
296
+ (error, stdout) => resolve(error ? "" : stdout)
297
+ )
298
+ );
299
+ for (const file of listed.split("\0"))
300
+ if (file && names.size < 5_000) names.add(path.basename(file));
301
+ }
302
+ spoken = { at: Date.now(), names: [...names] };
303
+ return spoken.names;
304
+ };
305
+ // On a Mac, Apple's transcriber through the helper in the buddy installed for this home (as desktop.mjs places it).
306
+ const appleHelper =
307
+ process.platform === "darwin"
308
+ ? path.join(
309
+ process.env.HARNESS_HOME
310
+ ? path.join(home, "desktop")
311
+ : path.join(os.homedir(), "Applications"),
312
+ "Harness.app",
313
+ "Contents",
314
+ "MacOS",
315
+ "HarnessSpeech"
316
+ )
317
+ : null;
318
+ const speech = createSpeech({
319
+ inference,
320
+ root,
321
+ vocabulary: speechNames,
322
+ apple: createAppleSpeech({ helper: appleHelper, log }),
323
+ log,
324
+ });
325
+
326
+ /**
327
+ * watch_video and listen, for agents on this computer only (video.mjs, listen.mjs). The words on screen and the words
328
+ * said come once the text reader and speech model are downloaded; asking never starts a download.
329
+ */
330
+ const watchOrListen = (request) => {
331
+ if (request.hostedCaller)
332
+ return {
333
+ ok: false,
334
+ error: "watching and listening run only for agents on this computer",
335
+ };
336
+ const file = typeof request.path === "string" ? request.path : "";
337
+ const stat = path.isAbsolute(file)
338
+ ? fs.statSync(file, { throwIfNoEntry: false })
339
+ : null;
340
+ if (!stat?.isFile())
341
+ return { ok: false, error: `${file} is not an absolute path to a file` };
342
+ const watching = request.command === "board.video.watch";
343
+ const span = { from: request.from, to: request.to };
344
+ const started = performance.now();
345
+ const work = watching
346
+ ? watchVideo(file, {
347
+ ...span,
348
+ frames: request.frames,
349
+ ocr: ready(["ocr"]) ? ocr : null,
350
+ out: path.join(filesRoot, "frames"),
351
+ })
352
+ : listen(file, { ...span, speech: speech.ready() ? speech : null });
353
+ const verb = watching ? "watched" : "listened to";
354
+ return {
355
+ ok: true,
356
+ pending: work.then((result) => {
357
+ const said = `${verb} ${path.basename(file)}: ${result.summary}`;
358
+ localEvent("media", said, performance.now() - started);
359
+ return { ok: true, ...result };
360
+ }),
361
+ };
362
+ };
363
+
364
+ // A folder of recordings and screenshots the person adds or takes away (no picker: they name it). Adding one starts
365
+ // the media part's download, then reads the folder on the background lane.
366
+ const changeFolders = (request) => {
367
+ const adding = request.command === "local.add";
368
+ const folder =
369
+ typeof request.folder === "string" && path.isAbsolute(request.folder)
370
+ ? path.resolve(request.folder)
371
+ : null;
372
+ if (
373
+ !folder ||
374
+ (adding && !fs.statSync(folder, { throwIfNoEntry: false })?.isDirectory())
375
+ )
376
+ return {
377
+ ok: false,
378
+ error: "give the full path of a folder on this computer",
379
+ };
380
+ const config = readJson(configPath, {});
381
+ const listed = (
382
+ Array.isArray(config.localFolders) ? config.localFolders : []
383
+ ).filter((entry) => entry !== folder);
384
+ writeJsonAtomic(configPath, {
385
+ ...config,
386
+ localFolders: adding ? [...listed, folder] : listed,
387
+ });
388
+ if (adding) {
389
+ const read = () =>
390
+ contextEngine
391
+ .refreshFiles()
392
+ .then((done) =>
393
+ log(
394
+ `library: ${done.added} of ${done.files} files read in the added folders`
395
+ )
396
+ );
397
+ void read()
398
+ .then(() => fetchMedia())
399
+ .then(read)
400
+ .catch(() => {});
401
+ }
402
+ return {
403
+ ok: true,
404
+ folders: libraryFolders(),
405
+ downloading: adding && !ready(["media"]),
406
+ };
407
+ };
408
+
409
+ // Push-to-talk (the buddy): a recording (base64 WAV) comes back as text for the composer, never sent.
410
+ const transcribe = (request) => {
411
+ const now = speech.ensure();
412
+ if (now.state !== "ready")
413
+ return { ok: true, ...now, text: "", heard: false };
414
+ return speech
415
+ .transcribe(Buffer.from(String(request.audio || ""), "base64"), {
416
+ lane: "interactive",
417
+ })
418
+ .then(
419
+ ({ raw, ...heard }) => ({ ok: true, state: "ready", ...heard }),
420
+ (error) => ({
421
+ ok: false,
422
+ error: `Harness could not hear that: ${error.message}`,
423
+ })
424
+ );
425
+ };
426
+
427
+ // A picture dropped on the buddy's composer: the text in it, read here for the message it goes with.
428
+ const readImage = (request) => {
429
+ const bytes = Buffer.from(String(request.data || ""), "base64");
430
+ if (!ready(["ocr"]))
431
+ return {
432
+ ok: false,
433
+ error:
434
+ "Harness's local model for reading pictures isn't on this computer yet.",
435
+ };
436
+ if (!imageKind(bytes))
437
+ return { ok: false, error: "Only PNG and JPEG pictures can be read." };
438
+ return ocr.read(bytes, { lane: "interactive" }).then(
439
+ ({ lines, ms }) => ({
440
+ ok: true,
441
+ lines: lines.map(({ text, box }) => ({ text, box })),
442
+ ms,
443
+ }),
444
+ (error) => ({
445
+ ok: false,
446
+ error: `Harness could not read that picture: ${error.message}`,
447
+ })
448
+ );
449
+ };
450
+
451
+ /**
452
+ * The local stack as the person sees it (tryharness local, the buddy): what is installed, where it runs, what is
453
+ * indexed and the overnight learner's last result; `local.learn` starts the learner now instead of waiting for the
454
+ * night; `local.on`/`local.off` turn search over the person's folders on (the default) or off, and off forgets what
455
+ * was read from them; push-to-talk says whether speech is ready, downloading (with its percent) or not downloaded,
456
+ * and `start` or the first recording starts the download.
457
+ */
458
+ const command = (request) => {
459
+ switch (request.command) {
460
+ case "local.status":
461
+ return {
462
+ ok: true,
463
+ ...contextEngine.status(),
464
+ coreModels: coreModels.status(),
465
+ learning: contextEngine.report(),
466
+ };
467
+ case "local.learn":
468
+ void contextEngine
469
+ .learn({ force: true })
470
+ .catch((error) => log(`context engine: ${error.message}`));
471
+ return { ok: true, started: true };
472
+ case "local.on":
473
+ case "local.off":
474
+ writeJsonAtomic(configPath, {
475
+ ...readJson(configPath, {}),
476
+ libraryOff: request.command === "local.off",
477
+ });
478
+ setImmediate(() => void contextEngine.refreshFiles().catch(() => {}));
479
+ return { ok: true, folders: libraryFolders() };
480
+ case "local.add":
481
+ case "local.remove":
482
+ return changeFolders(request);
483
+ case "voice.status":
484
+ return {
485
+ ok: true,
486
+ ...(request.start === true ? speech.ensure() : speech.status()),
487
+ };
488
+ case "voice.transcribe":
489
+ return transcribe(request);
490
+ case "image.read":
491
+ return readImage(request);
492
+ }
493
+ };
494
+
495
+ let embedTimer = null;
496
+ // The firewall scrubs keys and passwords from what leaves once its credentials model is here at start.
497
+ let scrubber = null;
498
+
499
+ /** Starts the downloads, the background embedding and, once its model is here, the context engine. */
500
+ const start = () => {
501
+ void coreModels.start();
502
+ embedTimer = setInterval(() => void embedPending(), EMBED_EVERY_MS);
503
+ if (ready(["credentials"])) scrubber = firewall;
504
+ if (ready(["search"])) contextEngine.start();
505
+ };
506
+
507
+ const stop = () => {
508
+ clearInterval(embedTimer);
509
+ try {
510
+ contextEngine.stop();
511
+ } catch {}
512
+ inference.stop();
513
+ };
514
+
515
+ return {
516
+ inference,
517
+ ready,
518
+ contextEngine,
519
+ ocr,
520
+ firewall,
521
+ speech,
522
+ scrubber: () => scrubber,
523
+ embed,
524
+ searchContext,
525
+ libraryFolders,
526
+ localHit,
527
+ threadFileHits,
528
+ readFilesSoon,
529
+ readByGuard,
530
+ watchOrListen,
531
+ handles: (name) => COMMANDS.has(name),
532
+ command,
533
+ start,
534
+ stop,
535
+ };
536
+ }
@@ -74,7 +74,7 @@ export const LLAMA = {
74
74
  };
75
75
 
76
76
  // memoryMb: the model's peak added memory (phys_footprint, MallocLargeCache=0) alone through the relay's host at a
77
- // representative job, measured on an M5 Pro on October 22 (.harness-runs/relay-memory-20261022/cost-probe.mjs): what the
77
+ // representative job, measured on an M5 Pro: what the
78
78
  // memory ceiling (modelMemory.mjs) charges while it is loaded. The llama models include the llama-server router, the
79
79
  // guards their shared tokenizer.
80
80
  export const MODELS = [
@@ -136,7 +136,7 @@ export const MODELS = [
136
136
  ],
137
137
  },
138
138
  // The second guard reads short windows: in a long page or post, one planted sentence moves a 300-character window
139
- // more than an 1,800-character one, and this model separates those better than the first (handoff-firewall eval). It
139
+ // more than an 1,800-character one, and this model separates those better than the first (on a fixed set of handoffs). It
140
140
  // is built on the first guard's vocabulary, so it uses that tokenizer (about 450 MB in memory) after its own extra step.
141
141
  {
142
142
  id: "guard-short",
@@ -290,8 +290,8 @@ export const MODELS = [
290
290
  ],
291
291
  },
292
292
  // Speech where Apple's own transcriber is not on the computer (Linux, Windows, Macs before macOS 26): NVIDIA's
293
- // Parakeet TDT 0.6B v3 (CC-BY-4.0; credited in the CLI's README), the 8-bit ONNX export by istupakov. Measured in the
294
- // dictation eval against the Moonshine base it replaced: 2.8% of words wrong on real people reading against 4.9%. Two
293
+ // Parakeet TDT 0.6B v3 (CC-BY-4.0; credited in the CLI's README), the 8-bit ONNX export by istupakov. Measured on a
294
+ // fixed set of recordings against the Moonshine base it replaced: 2.8% of words wrong on real people reading against 4.9%. Two
295
295
  // threads: a 5 s recording in 130 ms here against 205 ms on one (four: 99 ms).
296
296
  {
297
297
  id: "speech",
@@ -356,6 +356,8 @@ export const unhide = (text) =>
356
356
  )
357
357
  .normalize("NFKC")
358
358
  .replace(
359
+ // Invisible and combining characters are stripped one code point at a time, on purpose.
360
+ // eslint-disable-next-line no-misleading-character-class
359
361
  /[\u00AD\u180E\u200B-\u200F\u202A-\u202E\u2060-\u2064\u2066-\u2069\uFEFF\uFE00-\uFE0F\u{E0000}-\u{E007F}\u{E0100}-\u{E01EF}\u0305\u0331-\u0338\u20D0-\u20FF]/gu,
360
362
  ""
361
363
  );
@@ -2,11 +2,11 @@
2
2
  // bring nothing new is paused, and the person is told why, however the rounds read it. Something new is a change (a
3
3
  // turn's commit or a receipt that shows changed files), a result (a job finished with words, a file), or one more of
4
4
  // the goal's checks reached. The person writing, a check-in and the backstop's own pause start the count again. Once it
5
- // pauses a thread it does not again for an hour (owner).
5
+ // pauses a thread it does not again for an hour.
6
6
  //
7
- // LOOP_TURNS, from the owner's board on October 20 (a read-only copy): in 212 stretches of turns that ended in
8
- // something new (with the person's posts and the goal's reached checks counted as new), the turns in a stretch were
9
- // p50 1, p90 2, p95 3, p99 4, at most 5. 8 is three past the longest and twice the p99.
7
+ // LOOP_TURNS, from a read-only copy of a real board: in 212 stretches of turns that ended in something new (with the
8
+ // person's posts and the goal's reached checks counted as new), the turns in a stretch were p50 1, p90 2, p95 3, p99 4,
9
+ // at most 5. 8 is three past the longest and twice the p99.
10
10
  export const LOOP_TURNS = 8;
11
11
  export const LOOP_COOLDOWN_MS = 60 * 60_000;
12
12
 
@@ -5,7 +5,9 @@ import { readJson, writeJsonAtomic } from "./files.mjs";
5
5
  // Which of the person's computers this is (machine.json, beside its key in ~/.harness): a fixed id made once, the name
6
6
  // the person reads it by (the hostname until they rename it: npx tryharness machines name <name>), and the role
7
7
  // Harness last gave it, home or worker (src/server/bridge/machines.ts decides; this is its memory between starts).
8
- // Setup leaves the code it showed here (joinCode) for the daemon, which takes it as this computer's join code.
8
+ // Setup leaves the code it showed here (joinCode) and when (joinCodeAt) for the daemon, which takes it as this computer's
9
+ // join code while it lasts (ownMachines.mjs). A cloud computer's enrollment marks it hosted (cloudBoot.mjs): Harness
10
+ // runs it, and it says so when it joins a swarm.
9
11
 
10
12
  const ID = /^[A-Za-z0-9_-]{8,64}$/;
11
13
  const ROLES = new Set(["home", "worker"]);
@@ -30,6 +32,8 @@ export function loadMachine(file) {
30
32
  : hostname(),
31
33
  role: ROLES.has(saved.role) ? saved.role : null,
32
34
  joinCode: typeof saved.joinCode === "string" ? saved.joinCode : null,
35
+ joinCodeAt: Number.isFinite(saved.joinCodeAt) ? saved.joinCodeAt : null,
36
+ hosted: saved.hosted === true,
33
37
  };
34
38
  if (saved.id !== machine.id || saved.label !== machine.label)
35
39
  saveMachine(file, machine);
@@ -42,5 +46,9 @@ export function saveMachine(file, machine) {
42
46
  label: machine.label,
43
47
  ...(machine.role ? { role: machine.role } : {}),
44
48
  ...(machine.joinCode ? { joinCode: machine.joinCode } : {}),
49
+ ...(machine.joinCode && machine.joinCodeAt
50
+ ? { joinCodeAt: machine.joinCodeAt }
51
+ : {}),
52
+ ...(machine.hosted ? { hosted: true } : {}),
45
53
  });
46
54
  }