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.
Files changed (167) hide show
  1. package/README.md +23 -19
  2. package/assets/dsh-plugin.mjs +72 -0
  3. package/assets/opencode-plugin.js +4 -3
  4. package/cli.mjs +484 -90
  5. package/lib/activity.mjs +10 -10
  6. package/lib/adapters.mjs +173 -222
  7. package/lib/agentBinaries.mjs +71 -0
  8. package/lib/agentRuntimeConfig.mjs +1 -1
  9. package/lib/agentRuntimes.mjs +34 -18
  10. package/lib/agentSignIn.mjs +456 -0
  11. package/lib/answers.mjs +5 -5
  12. package/lib/archetypes.d.ts +64 -0
  13. package/lib/archetypes.js +989 -0
  14. package/lib/asks.mjs +4 -4
  15. package/lib/audit.mjs +666 -0
  16. package/lib/backupSetup.mjs +143 -0
  17. package/lib/boardAccess.mjs +24 -11
  18. package/lib/boardBackup.mjs +283 -0
  19. package/lib/boardFileCommands.mjs +6 -6
  20. package/lib/boardFiles.mjs +13 -12
  21. package/lib/boardMirror.mjs +124 -25
  22. package/lib/boardProtocol.json +88 -2
  23. package/lib/boardWaits.mjs +101 -0
  24. package/lib/book.mjs +659 -0
  25. package/lib/bridge.mjs +6 -6
  26. package/lib/cards.mjs +138 -28
  27. package/lib/cloudAgents.mjs +216 -0
  28. package/lib/cloudAi.mjs +1312 -0
  29. package/lib/cloudAssistants.mjs +242 -0
  30. package/lib/cloudBoot.mjs +119 -0
  31. package/lib/cloudSave.mjs +197 -0
  32. package/lib/cloudSsh.mjs +178 -0
  33. package/lib/commands.mjs +7 -2
  34. package/lib/context.mjs +19 -5
  35. package/lib/contractGate.mjs +399 -0
  36. package/lib/control-client.mjs +9 -1
  37. package/lib/controlSocket.mjs +86 -0
  38. package/lib/coordinator.mjs +252 -144
  39. package/lib/daemon.mjs +1351 -2767
  40. package/lib/desk.mjs +240 -0
  41. package/lib/desktop.mjs +178 -74
  42. package/lib/directLink.mjs +16 -5
  43. package/lib/environment.mjs +2 -1
  44. package/lib/environments.mjs +247 -9
  45. package/lib/firewall.mjs +9 -9
  46. package/lib/folders.mjs +20 -16
  47. package/lib/frames.mjs +2 -3
  48. package/lib/gitHost.mjs +174 -0
  49. package/lib/goalCommands.mjs +25 -7
  50. package/lib/goals.mjs +7 -7
  51. package/lib/guestRequests.mjs +94 -0
  52. package/lib/handoffs.mjs +8 -7
  53. package/lib/handover.mjs +4 -4
  54. package/lib/harnessModel.mjs +6 -6
  55. package/lib/harnessOffers.mjs +266 -0
  56. package/lib/hookDialects.mjs +298 -0
  57. package/lib/inference.mjs +26 -8
  58. package/lib/jobAnswers.mjs +190 -0
  59. package/lib/landed.mjs +887 -0
  60. package/lib/lane.mjs +42 -11
  61. package/lib/lessons.mjs +16 -16
  62. package/lib/library.mjs +3 -3
  63. package/lib/lineup.mjs +3 -0
  64. package/lib/linkRelay.mjs +574 -0
  65. package/lib/live.mjs +284 -13
  66. package/lib/localAgentAsk.mjs +74 -0
  67. package/lib/localStack.mjs +536 -0
  68. package/lib/localmodels.mjs +6 -4
  69. package/lib/loopBackstop.mjs +4 -4
  70. package/lib/machineIdentity.mjs +9 -1
  71. package/lib/machineRoutes.mjs +181 -2
  72. package/lib/machineRuns.mjs +76 -1
  73. package/lib/machineSeal.mjs +39 -12
  74. package/lib/mcp-stdio.mjs +163 -27
  75. package/lib/mcpRelay.mjs +1 -1
  76. package/lib/members.json +29 -2
  77. package/lib/members.mjs +1 -1
  78. package/lib/models.mjs +85 -31
  79. package/lib/museSession.mjs +21 -13
  80. package/lib/nativeHost.mjs +36 -8
  81. package/lib/neovim.mjs +126 -0
  82. package/lib/network.mjs +50 -5
  83. package/lib/notify.mjs +7 -5
  84. package/lib/nudge.mjs +50 -19
  85. package/lib/ocr.mjs +9 -9
  86. package/lib/onboard.mjs +26 -20
  87. package/lib/oneLine.mjs +7 -7
  88. package/lib/ownMachines.mjs +1223 -140
  89. package/lib/pairing.mjs +60 -16
  90. package/lib/paths.mjs +19 -1
  91. package/lib/peerProcess.mjs +124 -0
  92. package/lib/personModel.mjs +89 -0
  93. package/lib/personNotices.mjs +151 -0
  94. package/lib/phones.mjs +538 -31
  95. package/lib/placeCommands.mjs +4 -4
  96. package/lib/places.mjs +400 -12
  97. package/lib/profile.mjs +67 -14
  98. package/lib/projectGit.mjs +158 -14
  99. package/lib/projectPieces.mjs +902 -0
  100. package/lib/projectSettings.mjs +7 -4
  101. package/lib/projects.mjs +384 -62
  102. package/lib/provenance.mjs +5 -1
  103. package/lib/proxyLinks.mjs +267 -0
  104. package/lib/reactions.mjs +7 -5
  105. package/lib/readiness.mjs +145 -23
  106. package/lib/record.mjs +128 -0
  107. package/lib/redact.mjs +4 -4
  108. package/lib/relayLimits.mjs +62 -0
  109. package/lib/release.mjs +170 -0
  110. package/lib/releaseKeys.json +6 -0
  111. package/lib/repoLinks.mjs +4 -4
  112. package/lib/roomChannels.mjs +24 -0
  113. package/lib/roomPage.mjs +1 -1
  114. package/lib/runAuthority.mjs +101 -0
  115. package/lib/runBrief.mjs +19 -8
  116. package/lib/runWorker.mjs +31 -7
  117. package/lib/runner.mjs +223 -29
  118. package/lib/runtime-install.mjs +143 -9
  119. package/lib/schedules.mjs +2 -2
  120. package/lib/screens.mjs +1 -1
  121. package/lib/sealed.mjs +11 -2
  122. package/lib/sealedThreads.mjs +671 -0
  123. package/lib/secretPatterns.json +1 -1
  124. package/lib/secrets.mjs +4 -4
  125. package/lib/seen.mjs +6 -6
  126. package/lib/serverWords.mjs +175 -0
  127. package/lib/service.mjs +22 -12
  128. package/lib/sessionControl.mjs +378 -90
  129. package/lib/specialties.json +74 -0
  130. package/lib/specialties.mjs +477 -0
  131. package/lib/specialtyKeys.d.ts +3 -0
  132. package/lib/specialtyKeys.js +57 -0
  133. package/lib/specialtyNotice.mjs +147 -0
  134. package/lib/specialtySnapshot.mjs +138 -0
  135. package/lib/speech.mjs +5 -5
  136. package/lib/store.mjs +420 -36
  137. package/lib/stuck.mjs +1 -1
  138. package/lib/swarm.mjs +304 -37
  139. package/lib/swarmDevices.mjs +319 -0
  140. package/lib/swarmMemory.mjs +27 -25
  141. package/lib/t3.mjs +24 -15
  142. package/lib/taskCommands.mjs +80 -20
  143. package/lib/taskRooms.mjs +125 -0
  144. package/lib/taskRuns.mjs +45 -3
  145. package/lib/team.json +255 -148
  146. package/lib/team.mjs +554 -190
  147. package/lib/threadActions.mjs +315 -0
  148. package/lib/threadAuthority.mjs +101 -0
  149. package/lib/threadBriefs.mjs +178 -0
  150. package/lib/threadLine.mjs +116 -0
  151. package/lib/threadRuns.mjs +209 -50
  152. package/lib/threadView.mjs +20 -7
  153. package/lib/usageTotals.mjs +2 -3
  154. package/lib/vault.mjs +101 -10
  155. package/lib/vaultCloud.mjs +2 -2
  156. package/lib/vaultCommands.mjs +15 -6
  157. package/lib/vaultTools.js +2 -2
  158. package/lib/verdicts.mjs +328 -0
  159. package/lib/wakes.mjs +1 -1
  160. package/lib/watches.mjs +2 -2
  161. package/lib/windowAsks.mjs +13 -5
  162. package/lib/worktrees.mjs +157 -25
  163. package/npm-shrinkwrap.json +1093 -0
  164. package/package.json +12 -11
  165. package/release.json +220 -0
  166. package/lib/modelTiers.json +0 -1183
  167. package/lib/modelTiers.mjs +0 -133
@@ -0,0 +1,671 @@
1
+ import fs from "node:fs";
2
+ import { randomUUID } from "node:crypto";
3
+ import { mimeFor } from "./files.mjs";
4
+ import {
5
+ fingerprint,
6
+ openBound,
7
+ openBytes,
8
+ relayBound,
9
+ relayKey,
10
+ sealBound,
11
+ sealBytes,
12
+ } from "./sealed.mjs";
13
+ import { MAX_FILE_BYTES } from "./boardFiles.mjs";
14
+ import { relayRefusal } from "./relayLimits.mjs";
15
+
16
+ // What may be referenced on a thread with another person. Harness's server applies the same rule; it is kept here too
17
+ // because a sealed request is opened only on the two computers.
18
+ const RELAY_REF_KINDS = new Set(["log", "repro", "link", "file"]);
19
+
20
+ // Marks a thread post this daemon prepared itself; nothing that arrives over the wire can carry it.
21
+ export const OPENED = Symbol("opened");
22
+
23
+ // "ana/claude-code" belongs to the person whose handle is "ana".
24
+ export const handleOf = (identityName) =>
25
+ String(identityName || "")
26
+ .split("/")[0]
27
+ .toLowerCase();
28
+
29
+ const refusedKinds = (refs) =>
30
+ (Array.isArray(refs) ? refs : [])
31
+ .filter((ref) => !RELAY_REF_KINDS.has(ref?.kind))
32
+ .map((ref) => String(ref?.kind));
33
+
34
+ const crossRefs = (refs) =>
35
+ (Array.isArray(refs) ? refs : [])
36
+ .filter(
37
+ (ref) =>
38
+ ref && RELAY_REF_KINDS.has(ref.kind) && typeof ref.ref === "string"
39
+ )
40
+ .slice(0, 20);
41
+
42
+ /**
43
+ * Threads with another person's computer, both ways. A thread whose other computer holds a key goes sealed to it:
44
+ * Harness relays only ciphertext. A thread with a computer that has no key yet goes readable, and says so.
45
+ *
46
+ * `identity()` is this computer's relay key pair, known once the phone desk has read it; `projects()` the shared
47
+ * projects, made after this module.
48
+ */
49
+ export function createSealedThreads({
50
+ store,
51
+ send,
52
+ askHarness,
53
+ connected,
54
+ identity,
55
+ projects,
56
+ notifyBoard,
57
+ notify,
58
+ files,
59
+ // The local guard's reading of words from outside (localStack.mjs readByGuard).
60
+ guard = async () => ({ unavailable: true }),
61
+ log,
62
+ }) {
63
+ const threadKey = async (thread) =>
64
+ thread?.peer_key && identity()
65
+ ? relayKey(identity().privateKey, thread.peer_key)
66
+ : null;
67
+ // What this computer seals to the other one on a thread, and what it opens from it: bound to the direction (K9).
68
+ const outward = (thread, bound) =>
69
+ relayBound(bound, identity().publicKey, thread.peer_key);
70
+ const inward = (thread, bound) =>
71
+ relayBound(bound, thread.peer_key, identity().publicKey);
72
+ // Opens what the other computer sealed on a thread, bound to its direction. Sealed the old way, without it, it is
73
+ // refused saying so; anything else did not come from that computer's key.
74
+ const openInward = async (
75
+ thread,
76
+ bound,
77
+ sealedPart,
78
+ what,
79
+ open = openBound
80
+ ) => {
81
+ const key = await threadKey(thread);
82
+ try {
83
+ return await open(key, inward(thread, bound), sealedPart);
84
+ } catch {
85
+ const old = await open(key, bound, sealedPart).then(
86
+ () => true,
87
+ () => false
88
+ );
89
+ throw new Error(
90
+ old
91
+ ? `the ${what} was refused: it was sealed by a Harness from before threads named their direction; the other person updates Harness to reach this computer`
92
+ : `the ${what} could not be opened with the other computer's key`
93
+ );
94
+ }
95
+ };
96
+ // The thread's member on this side, as the other computer knows it: the person, or one of their agents by name.
97
+ const memberHere = (taskId) => {
98
+ const agent = store.getTask(taskId)?.target_agent;
99
+ return !agent || agent === "user" ? "person" : agent;
100
+ };
101
+ // Who of the other person's sent it, as sealed: it must be the thread's member on their side, whatever label Harness
102
+ // carried beside it.
103
+ const sealedSender = (thread, from) => {
104
+ const member = String(thread.with_agent).split("/")[1] || "person";
105
+ if (String(from || "") !== member)
106
+ throw new Error("the message names another sender than the thread's");
107
+ return `${handleOf(thread.with_agent)}/${member}`;
108
+ };
109
+
110
+ // The key Harness's directory gives for another person's computer, against the one pinned here the first time.
111
+ const peerKeyFor = (handle, publicKey, trustNewKey = false) => {
112
+ const pinned = store.relayPeer(handle);
113
+ if (pinned && pinned.public_key !== publicKey && !trustNewKey) {
114
+ throw new Error(
115
+ `the key Harness gave for ${handle}'s computer is not the one this computer saw before. If they reinstalled Harness, compare fingerprints with them (npx tryharness peers), then: npx tryharness connect ${handle}/<agent> --about <text> --trust-new-key`
116
+ );
117
+ }
118
+ if (!pinned || pinned.public_key !== publicKey)
119
+ store.pinRelayPeer(handle, publicKey);
120
+ return publicKey;
121
+ };
122
+
123
+ // A message the person released (or auto-send let through) goes up the bridge; the server acknowledges it once the
124
+ // other side has it. Unacknowledged messages go again on reconnect, so a dropped socket never loses a reply.
125
+ const sendMessage = (message) =>
126
+ void (async () => {
127
+ const thread = store.getThread(message.task_id);
128
+ const key = await threadKey(thread);
129
+ const base = {
130
+ type: "thread_message",
131
+ taskId: message.task_id,
132
+ messageId: message.id,
133
+ seq: message.seq,
134
+ to: message.recipient,
135
+ sentAt: message.sent_at,
136
+ };
137
+ // A file never goes readable: a thread without the other computer's key sends the words alone.
138
+ if (!key) {
139
+ send({
140
+ ...base,
141
+ text: message.text,
142
+ refs: message.refs.filter((ref) => !ref.file),
143
+ });
144
+ return;
145
+ }
146
+ // Each file goes ahead of the message that names it, sealed on its own; the message carries its hash.
147
+ for (const ref of message.refs) {
148
+ const file =
149
+ ref.kind === "file" && ref.file ? store.file(ref.file) : null;
150
+ if (!file?.present) continue;
151
+ send({
152
+ type: "thread_file",
153
+ taskId: message.task_id,
154
+ messageId: `file:${message.id}:${file.id}`,
155
+ seq: message.seq,
156
+ to: message.recipient,
157
+ sealed: {
158
+ fileId: file.id,
159
+ ...(await sealBytes(
160
+ key,
161
+ outward(thread, `file|${message.task_id}|${file.id}`),
162
+ fs.readFileSync(files.blobPath(file.sha256))
163
+ )),
164
+ },
165
+ });
166
+ }
167
+ send({
168
+ ...base,
169
+ text: "",
170
+ refs: [],
171
+ sealed: await sealBound(
172
+ key,
173
+ outward(thread, `message|${message.task_id}|${message.id}`),
174
+ {
175
+ text: message.text,
176
+ refs: message.refs,
177
+ from: memberHere(message.task_id),
178
+ }
179
+ ),
180
+ });
181
+ })().catch((error) =>
182
+ log(`thread message ${message.id} not sent: ${error.message}`)
183
+ );
184
+
185
+ const sendEnd = (taskId, by, outcome) =>
186
+ void (async () => {
187
+ const thread = store.getThread(taskId);
188
+ const key = await threadKey(thread);
189
+ send({
190
+ type: "thread_end",
191
+ taskId,
192
+ by,
193
+ ...(key
194
+ ? {
195
+ outcome: "",
196
+ sealed: await sealBound(key, outward(thread, `end|${taskId}`), {
197
+ outcome,
198
+ }),
199
+ }
200
+ : { outcome }),
201
+ });
202
+ })().catch((error) =>
203
+ log(`thread end ${taskId} not sent: ${error.message}`)
204
+ );
205
+
206
+ // A thread that ends ends the threads of the guests it brought in, and their computers hear it.
207
+ const endGuests = (goal, outcome) => {
208
+ for (const guest of goal.goal.guests) {
209
+ const closed = store.getThread(guest.thread)
210
+ ? store.closeThread(guest.thread, outcome)
211
+ : null;
212
+ if (closed?.ok && !closed.already) sendEnd(guest.thread, "user", outcome);
213
+ }
214
+ };
215
+
216
+ // A shared project's event on the person thread under it: sealed to the other computer, never readable. Harness
217
+ // acknowledges it once it holds it, and keeps it until the other computer has it.
218
+ const sendProjectEvent = ({ projectId, to, messageId, seq, body }) =>
219
+ void (async () => {
220
+ const thread = store.getThread(projectId);
221
+ const key = await threadKey(thread);
222
+ if (!key)
223
+ throw new Error(
224
+ "the project's thread has no key for the other computer"
225
+ );
226
+ send({
227
+ type: "thread_message",
228
+ taskId: projectId,
229
+ messageId,
230
+ seq,
231
+ to,
232
+ sentAt: new Date().toISOString(),
233
+ text: "",
234
+ refs: [],
235
+ sealed: await sealBound(
236
+ key,
237
+ outward(thread, `message|${projectId}|${messageId}`),
238
+ { text: "", refs: [], from: memberHere(projectId), project: body }
239
+ ),
240
+ });
241
+ })().catch((error) =>
242
+ log(`project event ${messageId} not sent: ${error.message}`)
243
+ );
244
+
245
+ // A file the message names is this computer's own row, matched to the id the other computer gave it; only a sealed
246
+ // thread brings files.
247
+ // `sender`: who sent the message, as sealed (sealedSender), never the label Harness carried beside it.
248
+ const ownRefs = (refs, thread, input, sender) =>
249
+ crossRefs(refs).map((ref) => {
250
+ if (!ref.file) return ref;
251
+ const { file: remoteId, ...named } = ref;
252
+ if (!thread?.peer_key || !/^[0-9a-f]{64}$/.test(String(ref.sha256)))
253
+ return { kind: named.kind, ref: named.ref };
254
+ const expected = store.expectFile({
255
+ taskId: String(input.taskId),
256
+ remoteId: String(remoteId).slice(0, 128),
257
+ name: files.fileName(ref.ref),
258
+ mime: /^[a-z0-9.+-]+\/[a-z0-9.+-]+$/i.test(String(ref.mime))
259
+ ? String(ref.mime).toLowerCase()
260
+ : mimeFor(ref.ref),
261
+ sha256: String(ref.sha256),
262
+ author: sender,
263
+ });
264
+ if (!expected.ok) throw new Error(expected.error);
265
+ return {
266
+ kind: "file",
267
+ ref: expected.file.name,
268
+ file: expected.file.id,
269
+ mime: expected.file.mime,
270
+ sha256: expected.file.sha256,
271
+ };
272
+ });
273
+
274
+ const acknowledged = (outcome, input) => ({
275
+ ok: outcome.ok,
276
+ result: outcome.ok ? { acknowledged: input.messageId } : null,
277
+ ...(outcome.ok ? {} : { error: outcome.error }),
278
+ });
279
+
280
+ const receiveMessage = async (input, respond) => {
281
+ // A thread that holds the other computer's key takes only what was sealed to it.
282
+ const thread = store.getThread(String(input.taskId || ""));
283
+ let words = {
284
+ text: String(input.text || ""),
285
+ refs: Array.isArray(input.refs) ? input.refs : [],
286
+ };
287
+ let sender = String(input.from || "someone");
288
+ if (thread?.peer_key) {
289
+ if (!input.sealed)
290
+ throw new Error("a message on a sealed thread arrived unsealed");
291
+ const opened = await openInward(
292
+ thread,
293
+ `message|${input.taskId}|${input.messageId}`,
294
+ input.sealed,
295
+ "message"
296
+ );
297
+ sender = sealedSender(thread, opened.from);
298
+ words = {
299
+ text: String(opened.text || ""),
300
+ refs: Array.isArray(opened.refs) ? opened.refs : [],
301
+ };
302
+ // A shared project's event: the project keeps it; nothing lands on the thread itself.
303
+ if (opened.project) {
304
+ const kept = projects()
305
+ ? projects().receive(
306
+ String(input.taskId),
307
+ opened.project,
308
+ String(input.messageId)
309
+ )
310
+ : { ok: false, error: "the daemon is still starting" };
311
+ respond(acknowledged(kept, input));
312
+ return;
313
+ }
314
+ }
315
+ const known = store.db.all("select 1 from board_messages where id = ?", [
316
+ String(input.messageId || ""),
317
+ ]).length;
318
+ const over =
319
+ thread && !known
320
+ ? relayRefusal(store.db, {
321
+ taskId: String(input.taskId),
322
+ handle: handleOf(thread.with_agent),
323
+ text: words.text.slice(0, 50_000),
324
+ })
325
+ : null;
326
+ if (over) throw new Error(`the message was refused: ${over}`);
327
+ const received = store.receiveMessage({
328
+ id: String(input.messageId || ""),
329
+ taskId: String(input.taskId || ""),
330
+ seq: input.seq,
331
+ sender,
332
+ text: words.text.slice(0, 50_000),
333
+ refs: ownRefs(words.refs, thread, input, sender),
334
+ sentAt: input.sentAt,
335
+ });
336
+ respond(acknowledged(received, input));
337
+ if (!received.ok || received.duplicate) return;
338
+ const task = store.getTask(String(input.taskId));
339
+ notifyBoard(task);
340
+ // A guest's message lands in its goal's room as a stranger's words; it wakes the facilitator, never a run here.
341
+ const guestGoal = store.guestGoalFor(String(input.taskId));
342
+ if (guestGoal && store.getTask(guestGoal)?.status === "open") {
343
+ const post = store.roomPost({
344
+ goalId: guestGoal,
345
+ author: sender,
346
+ kind: "guest",
347
+ text: words.text.slice(0, 20_000),
348
+ });
349
+ // A stranger's words are read by the guard like any words from outside; a brief marks what it flagged.
350
+ void guard(post.text)
351
+ .then((reading) => {
352
+ if (reading?.verdict === "flag" || reading?.verdict === "hold")
353
+ store.meta(`flagged:${post.id}`, String(reading.score));
354
+ })
355
+ .catch(() => undefined);
356
+ notifyBoard(store.getTask(guestGoal));
357
+ }
358
+ notify(
359
+ sender.endsWith("/person")
360
+ ? `${handleOf(sender)} wrote to you: ${words.text.slice(0, 100)}`
361
+ : `${sender} replied on: ${String(task?.objective || "").slice(0, 80)}`
362
+ );
363
+ };
364
+
365
+ const receiveFile = async (input, respond) => {
366
+ const taskId = String(input.taskId || "");
367
+ const thread = store.getThread(taskId);
368
+ if (!thread?.peer_key || !input.sealed)
369
+ throw new Error(
370
+ "files come only sealed, on a thread with the other computer's key"
371
+ );
372
+ const remoteId = String(input.sealed.fileId || "").slice(0, 128);
373
+ const bytes = await openInward(
374
+ thread,
375
+ `file|${taskId}|${remoteId}`,
376
+ input.sealed,
377
+ "file",
378
+ openBytes
379
+ );
380
+ if (bytes.length > MAX_FILE_BYTES)
381
+ throw new Error("the file is larger than the board takes");
382
+ const arrived = store.db.all(
383
+ "select 1 from board_files where task_id = ? and remote_id = ? and present = 1",
384
+ [taskId, remoteId]
385
+ ).length;
386
+ const over = arrived
387
+ ? null
388
+ : relayRefusal(store.db, {
389
+ taskId,
390
+ handle: handleOf(thread.with_agent),
391
+ bytes: bytes.length,
392
+ });
393
+ if (over) throw new Error(`the file was refused: ${over}`);
394
+ const received = store.receiveFile({
395
+ taskId,
396
+ remoteId,
397
+ sha256: files.keepBlob(bytes),
398
+ bytes: bytes.length,
399
+ author: `${handleOf(thread.with_agent)}/someone`,
400
+ });
401
+ respond(acknowledged(received, input));
402
+ if (received.ok && !received.duplicate) notifyBoard(store.getTask(taskId));
403
+ };
404
+
405
+ const receiveEnd = async (input, respond) => {
406
+ const thread = store.getThread(String(input.taskId || ""));
407
+ let outcome = String(input.outcome || "");
408
+ if (thread?.peer_key) {
409
+ if (!input.sealed)
410
+ throw new Error("the end of a sealed thread arrived unsealed");
411
+ const opened = await openInward(
412
+ thread,
413
+ `end|${input.taskId}`,
414
+ input.sealed,
415
+ "ending"
416
+ );
417
+ outcome = String(opened.outcome || "");
418
+ }
419
+ const closed = store.closeThread(
420
+ String(input.taskId || ""),
421
+ outcome.slice(0, 20_000) || null
422
+ );
423
+ respond({
424
+ ok: closed.ok,
425
+ result: closed.ok ? { closed: input.taskId } : null,
426
+ });
427
+ if (!closed.ok || closed.already) return;
428
+ projects()?.channelEnded(String(input.taskId || ""));
429
+ notifyBoard(closed.task);
430
+ notify(
431
+ `${input.by} ended the thread on: ${String(closed.task?.objective || "").slice(0, 80)}`
432
+ );
433
+ };
434
+
435
+ const RECEIVERS = {
436
+ thread_message: receiveMessage,
437
+ thread_file: receiveFile,
438
+ thread_end: receiveEnd,
439
+ };
440
+
441
+ /**
442
+ * One message from the other computer, by its bridge type (thread_message, thread_file, thread_end). `respond` gets
443
+ * the acknowledgement Harness relays back, before the board moves; a failure is answered as one.
444
+ */
445
+ const receive = (type, input, respond) =>
446
+ RECEIVERS[type](input || {}, respond).catch((error) =>
447
+ respond({ ok: false, error: error.message })
448
+ );
449
+
450
+ // A request to another person's agent. Their computer's key comes from Harness and is pinned here; the request, its
451
+ // brief and its references go sealed to it, and this computer keeps its own copy for the thread.
452
+ const request = async ({
453
+ to,
454
+ fromAgent,
455
+ about,
456
+ brief,
457
+ refs,
458
+ trustNewKey = false,
459
+ // A shared project's offer (projects.mjs): it goes only sealed, to a computer that has a key.
460
+ project = null,
461
+ // The person agreed to a first contact Harness can read, with a computer that has no key (security audit S8).
462
+ readable = false,
463
+ }) => {
464
+ if (!connected()) throw new Error("the daemon is not connected to Harness");
465
+ const target = String(to || "").toLowerCase();
466
+ const subject = String(about || "").slice(0, 500);
467
+ const text = String(brief || "").slice(0, 200_000);
468
+ if (!subject.trim()) throw new Error("about is required");
469
+ const refused = refusedKinds(refs);
470
+ if (refused.length)
471
+ throw new Error(
472
+ `references of kind ${refused.join(", ")} cannot cross accounts`
473
+ );
474
+ const attached = crossRefs(refs);
475
+ const keyed = await askHarness("relay_key", { to: target });
476
+ if (!keyed.ok)
477
+ throw new Error(keyed.error || "Harness could not find that agent");
478
+ let targetKey = null;
479
+ let input = {
480
+ to: target,
481
+ fromAgent: String(fromAgent || ""),
482
+ about: subject,
483
+ brief: text,
484
+ refs: attached,
485
+ };
486
+ // Harness cannot quietly turn a sealed contact into a readable one: a computer that had a key keeps needing one.
487
+ if (
488
+ typeof keyed.publicKey !== "string" &&
489
+ store.relayPeer(handleOf(target))
490
+ )
491
+ throw new Error(
492
+ `Harness gave no key for ${handleOf(target)}'s computer, which had one before; the request was not sent`
493
+ );
494
+ if (project && typeof keyed.publicKey !== "string")
495
+ throw new Error(
496
+ `${handleOf(target)}'s computer can't take a sealed project yet. Ask them to update Harness.`
497
+ );
498
+ // No key from Harness for someone new: Harness could be holding it back to read the contact, so it goes readable
499
+ // only once the person says so.
500
+ if (typeof keyed.publicKey !== "string" && !readable)
501
+ throw new Error(
502
+ `Harness gave no key for ${handleOf(target)}'s computer (a Harness from before sealed threads, or one that has not connected), so Harness could read this. Ask them to update Harness, or send it readable only if you agree (tryharness connect --readable).`
503
+ );
504
+ if (typeof keyed.publicKey === "string") {
505
+ targetKey = peerKeyFor(handleOf(target), keyed.publicKey, trustNewKey);
506
+ const sealId = randomUUID();
507
+ const sealed = await sealBound(
508
+ await relayKey(identity().privateKey, targetKey),
509
+ `request|${sealId}`,
510
+ {
511
+ about: subject,
512
+ brief: text,
513
+ refs: attached,
514
+ ...(project ? { project } : {}),
515
+ }
516
+ );
517
+ input = {
518
+ to: target,
519
+ fromAgent: String(fromAgent || ""),
520
+ about: "",
521
+ brief: "",
522
+ refs: [],
523
+ sealed: {
524
+ v: 1,
525
+ sealId,
526
+ key: identity().publicKey,
527
+ targetKey,
528
+ ...sealed,
529
+ },
530
+ };
531
+ }
532
+ // No key: their computer runs a Harness from before sealed threads, and the request goes readable by Harness, as
533
+ // it says.
534
+ const response = await askHarness("relay_request", input);
535
+ if (!response.ok)
536
+ throw new Error(response.error || "the request was refused");
537
+ if (response.threadId)
538
+ store.meta(
539
+ `relay_pending:${response.threadId}`,
540
+ JSON.stringify({
541
+ about: subject,
542
+ brief: text,
543
+ refs: attached,
544
+ targetKey,
545
+ })
546
+ );
547
+ return {
548
+ ...response,
549
+ sealed: Boolean(targetKey),
550
+ ...(targetKey ? { fingerprint: await fingerprint(targetKey) } : {}),
551
+ };
552
+ };
553
+
554
+ // A thread Harness posts on accept. The asking side takes its own copy of the request; the other side opens it with
555
+ // the asker's key, pinned for that handle. Either way the thread keeps the other computer's key.
556
+ const openPost = async (post) => {
557
+ const thread = post.thread;
558
+ const id = String(post.id || "");
559
+ if (thread.role === "asker") {
560
+ const pending = JSON.parse(store.meta(`relay_pending:${id}`) || "null");
561
+ if (!pending)
562
+ throw new Error("this computer did not ask for that thread");
563
+ if ((thread.sealed?.targetKey ?? null) !== (pending.targetKey ?? null))
564
+ throw new Error(
565
+ "the thread names another key than the one this computer sealed its request to"
566
+ );
567
+ return {
568
+ ...post,
569
+ objective: pending.about,
570
+ brief: pending.brief,
571
+ contextRefs: pending.refs,
572
+ thread: {
573
+ with: thread.with,
574
+ role: "asker",
575
+ peerKey: pending.targetKey,
576
+ [OPENED]: true,
577
+ },
578
+ };
579
+ }
580
+ const sealed = thread.sealed;
581
+ // The key the person was shown when the request was listed is the one accepting pins (S8): Harness can't swap in
582
+ // another between the two.
583
+ const shown = store.meta(`relay-shown-key:${id}`);
584
+ if (shown && shown !== String(sealed.key || ""))
585
+ throw new Error(
586
+ "the request arrived with another key than the one shown before accepting"
587
+ );
588
+ const peer = peerKeyFor(handleOf(thread.with), String(sealed.key || ""));
589
+ let opened;
590
+ try {
591
+ opened = await openBound(
592
+ await relayKey(identity().privateKey, peer),
593
+ `request|${sealed.sealId}`,
594
+ sealed
595
+ );
596
+ } catch {
597
+ throw new Error(
598
+ "the request could not be opened with the key Harness gave for its sender"
599
+ );
600
+ }
601
+ return {
602
+ ...post,
603
+ objective: String(opened.about || "").slice(0, 500) || "A thread",
604
+ brief: String(opened.brief || "").slice(0, 200_000),
605
+ contextRefs: crossRefs(opened.refs),
606
+ thread: {
607
+ with: thread.with,
608
+ role: "target",
609
+ peerKey: peer,
610
+ [OPENED]: true,
611
+ },
612
+ };
613
+ };
614
+
615
+ // A sealed request shown before it is accepted: its subject, opened here for the person's own device.
616
+ // Showing a request pins nothing (security audit S8: listing alone once pinned whatever key Harness gave for a
617
+ // stranger); a key pinned before must still match, and the one shown is kept so that accepting pins that same key.
618
+ const openRequest = async ({ sealed, from, id = null }) => {
619
+ if (!sealed || typeof sealed !== "object")
620
+ throw new Error("nothing sealed to open");
621
+ const mine = sealed.key === identity().publicKey;
622
+ const pinned = store.relayPeer(handleOf(from));
623
+ if (!mine && pinned && pinned.public_key !== String(sealed.key || ""))
624
+ throw new Error(
625
+ `the key Harness gave for ${handleOf(from)}'s computer is not the one this computer saw before`
626
+ );
627
+ const peer = mine
628
+ ? String(sealed.targetKey || "")
629
+ : String(sealed.key || "");
630
+ if (!mine && id && /^[0-9a-f-]{36}$/.test(String(id)))
631
+ store.meta(`relay-shown-key:${id}`, peer);
632
+ const opened = await openBound(
633
+ await relayKey(identity().privateKey, peer),
634
+ `request|${sealed.sealId}`,
635
+ sealed
636
+ );
637
+ return {
638
+ about: String(opened.about || "").slice(0, 500),
639
+ fingerprint: await fingerprint(peer),
640
+ ...(opened.project && typeof opened.project === "object"
641
+ ? { project: opened.project }
642
+ : {}),
643
+ };
644
+ };
645
+
646
+ // The other people's computers this one has pinned a key for, and this computer's own fingerprint.
647
+ const peers = async () => ({
648
+ ok: true,
649
+ peers: await Promise.all(
650
+ store.relayPeers().map(async (peer) => ({
651
+ handle: peer.handle,
652
+ pinned_at: peer.pinned_at,
653
+ fingerprint: await fingerprint(peer.public_key),
654
+ }))
655
+ ),
656
+ mine: identity() ? await fingerprint(identity().publicKey) : null,
657
+ });
658
+
659
+ return {
660
+ sendMessage,
661
+ sendEnd,
662
+ endGuests,
663
+ sendProjectEvent,
664
+ receive,
665
+ receives: (type) => Object.hasOwn(RECEIVERS, type),
666
+ request,
667
+ openPost,
668
+ openRequest,
669
+ peers,
670
+ };
671
+ }
@@ -1,5 +1,5 @@
1
1
  {
2
- "about": "What must never leave in a message to someone else's agent or an outside place: keys and tokens by their well-known shapes, private-key blocks, env-style assignments of secrets, and anything that announces itself as a seed phrase. Read by the daemon (cli/lib/secrets.mjs) and by Harness's server (src/shared/secrets.ts), so both check the same list.",
2
+ "about": "What must never leave in a message to someone else's agent or an outside place: keys and tokens by their well-known shapes, private-key blocks, env-style assignments of secrets, and anything that announces itself as a seed phrase. Read by the daemon (cli/lib/secrets.mjs) and by Harness's server, so both check the same list.",
3
3
  "patterns": [
4
4
  {
5
5
  "reason": "a private key block",
package/lib/secrets.mjs CHANGED
@@ -1,9 +1,9 @@
1
1
  import { readFileSync } from "node:fs";
2
2
 
3
- // The outbound guard on a thread: what must never leave the machine in a reply to another
4
- // person's agent. A hit does not block forever; it holds the message for the person, with the
5
- // reason, so they can strip it or send it knowingly. The patterns live in secretPatterns.json, which
6
- // Harness's server reads too (src/shared/secrets.ts), so both check the same list.
3
+ // The outbound guard on a thread: what must never leave the machine in a reply to another person's
4
+ // agent. A hit does not block forever; it holds the message for the person, with the reason, so
5
+ // they can strip it or send it knowingly. The patterns live in secretPatterns.json, which Harness's
6
+ // server reads too, so both check the same list.
7
7
 
8
8
  const PATTERNS = JSON.parse(
9
9
  readFileSync(new URL("./secretPatterns.json", import.meta.url), "utf8")