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
@@ -1,5 +1,5 @@
1
+ import { aboutOf, claimHolds } from "./agentClaims.mjs";
1
2
  import fs from "node:fs";
2
- import { randomUUID } from "node:crypto";
3
3
  import { mimeFor } from "./files.mjs";
4
4
  import {
5
5
  fingerprint,
@@ -11,26 +11,28 @@ import {
11
11
  sealBytes,
12
12
  } from "./sealed.mjs";
13
13
  import { MAX_FILE_BYTES } from "./boardFiles.mjs";
14
- import { relayRefusal } from "./relayLimits.mjs";
14
+ import { createPerMinute, PER_MINUTE, relayRefusal } from "./relayLimits.mjs";
15
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.
16
+ // What may be referenced in a message to a friend. A message is opened only on the two computers, so they alone
17
+ // enforce it; Harness carries nothing it could read (docs/jam-plan.md J2).
18
18
  const RELAY_REF_KINDS = new Set(["log", "repro", "link", "file"]);
19
19
 
20
20
  // Marks a thread post this daemon prepared itself; nothing that arrives over the wire can carry it.
21
21
  export const OPENED = Symbol("opened");
22
22
 
23
- // "ana/claude-code" belongs to the person whose handle is "ana".
23
+ /**
24
+ * A person's identity key as their friends' computers seal to it: base64url, as every relay key travels (sealed.mjs).
25
+ * Harness and the person's shared keys carry it as standard base64.
26
+ */
27
+ export const relayPublicKey = (key) =>
28
+ Buffer.from(String(key), "base64").toString("base64url");
29
+
30
+ // "ana/person" is the person whose handle is "ana".
24
31
  export const handleOf = (identityName) =>
25
32
  String(identityName || "")
26
33
  .split("/")[0]
27
34
  .toLowerCase();
28
35
 
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
36
  const crossRefs = (refs) =>
35
37
  (Array.isArray(refs) ? refs : [])
36
38
  .filter(
@@ -39,27 +41,41 @@ const crossRefs = (refs) =>
39
41
  )
40
42
  .slice(0, 20);
41
43
 
44
+ const HANDLE = /^[a-z0-9][a-z0-9-]{2,19}$/;
45
+
42
46
  /**
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.
47
+ * Friendships with other people's computers, both ways (docs/jam-plan.md J1, J2). A friendship is a thread between two
48
+ * persons that Harness posts to both computers on accept; each pins the other's key as it takes it. Its messages,
49
+ * files and end go only sealed to that key, from one person to the other: nothing is sent or taken on a thread with
50
+ * an agent (one from before friends), without the other computer's key, or unsealed, and a message whose seal names
51
+ * an agent is refused. Harness relays only ciphertext.
45
52
  *
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.
53
+ * `identity()` is the person's identity toward other people (computerDevices.mjs personIdentity): the account key
54
+ * pair every one of their devices holds, with the earlier ones, so what a friend sealed before the keys changed still
55
+ * opens. Null while this computer holds no copy. `follow(handle, keyed)` takes a friend's new keys when their links
56
+ * lead to them from the keys pinned here (daemon.mjs followPeer); false when they do not.
48
57
  */
49
58
  export function createSealedThreads({
50
59
  store,
51
60
  send,
52
61
  askHarness,
53
- connected,
54
62
  identity,
55
- projects,
56
63
  notifyBoard,
57
64
  notify,
58
65
  files,
59
- // The local guard's reading of words from outside (localStack.mjs readByGuard).
60
- guard = async () => ({ unavailable: true }),
66
+ follow = async () => false,
67
+ // This computer's signed claim that it sent the person's words (agentClaims.mjs): every message carries one, and the
68
+ // other computer checks it against the identity it pinned for them.
69
+ claims = null,
70
+ // Reads the person's identity again when this computer holds none yet (it was approved after it connected).
71
+ ensureIdentity = async () => {},
61
72
  log,
62
73
  }) {
74
+ const perMinute = createPerMinute();
75
+ const tooFast = (handle) =>
76
+ perMinute.take(handle)
77
+ ? null
78
+ : `more than ${PER_MINUTE} messages a minute from ${handle}`;
63
79
  const threadKey = async (thread) =>
64
80
  thread?.peer_key && identity()
65
81
  ? relayKey(identity().privateKey, thread.peer_key)
@@ -82,6 +98,18 @@ export function createSealedThreads({
82
98
  try {
83
99
  return await open(key, inward(thread, bound), sealedPart);
84
100
  } catch {
101
+ // Sealed to the person's keys before they changed (a device left their list): the earlier keys open it.
102
+ for (const earlier of identity()?.previous ?? []) {
103
+ const opened = await open(
104
+ await relayKey(earlier.privateKey, thread.peer_key),
105
+ relayBound(bound, thread.peer_key, earlier.publicKey),
106
+ sealedPart
107
+ ).then(
108
+ (value) => ({ value }),
109
+ () => null
110
+ );
111
+ if (opened) return opened.value;
112
+ }
85
113
  const old = await open(key, bound, sealedPart).then(
86
114
  () => true,
87
115
  () => false
@@ -93,67 +121,62 @@ export function createSealedThreads({
93
121
  );
94
122
  }
95
123
  };
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;
124
+ // The friendship a message, file or end goes on: a thread with the other person themselves whose computer's key
125
+ // this one holds. A thread with another person's agent, from before friends, is none.
126
+ const friendship = (taskId) => {
127
+ const thread = store.getThread(String(taskId || ""));
128
+ const [handle, member] = String(thread?.with_agent || "").split("/");
129
+ return thread?.peer_key && HANDLE.test(handle) && member === "person"
130
+ ? thread
131
+ : null;
100
132
  };
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}`;
133
+ // The friendship something arrived on, or the refusal: only a friend's computer, only sealed.
134
+ const arrivedOn = (input, what) => {
135
+ const thread = friendship(input.taskId);
136
+ if (!thread) throw new Error(`a ${what} comes only on a friendship`);
137
+ if (!input.sealed) throw new Error(`a ${what} comes only sealed`);
138
+ // Harness's label beside it names the friend; one naming an agent did not come from a person.
139
+ const label = input.from ?? input.by;
140
+ if (label != null && String(label) !== thread.with_agent)
141
+ throw new Error(`a ${what} names only people`);
142
+ return thread;
108
143
  };
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;
144
+ // Who sent it, as sealed: the friend themselves, never one of their agents, whatever label Harness carried beside it.
145
+ const sealedSender = (thread, from) => {
146
+ if (String(from || "") !== "person")
147
+ throw new Error("a message comes only from a person, never an agent");
148
+ return thread.with_agent;
121
149
  };
122
150
 
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.
151
+ // A message the person sent goes to the other computer: over the channel with that
152
+ // person when one is open, else through Harness's mailbox (daemon.mjs sendThreadFrame), each file ahead of the message
153
+ // that names it. Unacknowledged messages go again on reconnect, so a dropped socket never loses a reply.
125
154
  const sendMessage = (message) =>
126
155
  void (async () => {
127
- const thread = store.getThread(message.task_id);
156
+ if (!identity()) await ensureIdentity();
157
+ const thread = friendship(message.task_id);
158
+ if (!thread)
159
+ throw new Error("a message goes only sealed, on a friendship");
128
160
  const key = await threadKey(thread);
129
161
  const base = {
130
162
  type: "thread_message",
131
163
  taskId: message.task_id,
132
164
  messageId: message.id,
133
165
  seq: message.seq,
134
- to: message.recipient,
166
+ to: thread.with_agent,
135
167
  sentAt: message.sent_at,
136
168
  };
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
169
  // Each file goes ahead of the message that names it, sealed on its own; the message carries its hash.
147
170
  for (const ref of message.refs) {
148
171
  const file =
149
172
  ref.kind === "file" && ref.file ? store.file(ref.file) : null;
150
173
  if (!file?.present) continue;
151
- send({
174
+ await send({
152
175
  type: "thread_file",
153
176
  taskId: message.task_id,
154
177
  messageId: `file:${message.id}:${file.id}`,
155
178
  seq: message.seq,
156
- to: message.recipient,
179
+ to: thread.with_agent,
157
180
  sealed: {
158
181
  fileId: file.id,
159
182
  ...(await sealBytes(
@@ -164,17 +187,19 @@ export function createSealedThreads({
164
187
  },
165
188
  });
166
189
  }
167
- send({
190
+ await send({
168
191
  ...base,
169
- text: "",
170
- refs: [],
171
192
  sealed: await sealBound(
172
193
  key,
173
194
  outward(thread, `message|${message.task_id}|${message.id}`),
174
195
  {
175
196
  text: message.text,
176
197
  refs: message.refs,
177
- from: memberHere(message.task_id),
198
+ from: "person",
199
+ claim: await claims?.mint(
200
+ "person",
201
+ aboutOf(String(message.text ?? ""))
202
+ ),
178
203
  }
179
204
  ),
180
205
  });
@@ -182,74 +207,30 @@ export function createSealedThreads({
182
207
  log(`thread message ${message.id} not sent: ${error.message}`)
183
208
  );
184
209
 
185
- const sendEnd = (taskId, by, outcome) =>
210
+ const sendEnd = (taskId, outcome) =>
186
211
  void (async () => {
187
- const thread = store.getThread(taskId);
212
+ if (!identity()) await ensureIdentity();
213
+ const thread = friendship(taskId);
214
+ if (!thread) throw new Error("an end goes only sealed, on a friendship");
188
215
  const key = await threadKey(thread);
189
216
  send({
190
217
  type: "thread_end",
191
218
  taskId,
192
- by,
193
- ...(key
194
- ? {
195
- outcome: "",
196
- sealed: await sealBound(key, outward(thread, `end|${taskId}`), {
197
- outcome,
198
- }),
199
- }
200
- : { outcome }),
219
+ sealed: await sealBound(key, outward(thread, `end|${taskId}`), {
220
+ outcome,
221
+ }),
201
222
  });
202
223
  })().catch((error) =>
203
224
  log(`thread end ${taskId} not sent: ${error.message}`)
204
225
  );
205
226
 
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.
227
+ // A file the message names is this computer's own row, matched to the id the other computer gave it.
247
228
  // `sender`: who sent the message, as sealed (sealedSender), never the label Harness carried beside it.
248
- const ownRefs = (refs, thread, input, sender) =>
229
+ const ownRefs = (refs, input, sender) =>
249
230
  crossRefs(refs).map((ref) => {
250
231
  if (!ref.file) return ref;
251
232
  const { file: remoteId, ...named } = ref;
252
- if (!thread?.peer_key || !/^[0-9a-f]{64}$/.test(String(ref.sha256)))
233
+ if (!/^[0-9a-f]{64}$/.test(String(ref.sha256)))
253
234
  return { kind: named.kind, ref: named.ref };
254
235
  const expected = store.expectFile({
255
236
  taskId: String(input.taskId),
@@ -278,97 +259,74 @@ export function createSealedThreads({
278
259
  });
279
260
 
280
261
  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"
262
+ const thread = arrivedOn(input, "message");
263
+ const handle = handleOf(thread.with_agent);
264
+ const opened = await openInward(
265
+ thread,
266
+ `message|${input.taskId}|${input.messageId}`,
267
+ input.sealed,
268
+ "message"
269
+ );
270
+ const sender = sealedSender(thread, opened.from);
271
+ // Which of their computers sent it, signed by that computer's key, which their identity certified.
272
+ const pinned = store.relayPeer(handle);
273
+ const claimed =
274
+ opened.claim?.agent === "person" &&
275
+ (await claimHolds(opened.claim, {
276
+ identitySigningKeys: [
277
+ pinned?.signing_key,
278
+ ...(pinned?.past_signing_keys ?? []),
279
+ ].filter(Boolean),
280
+ about: aboutOf(String(opened.text ?? "")),
281
+ }));
282
+ if (!claimed)
283
+ throw new Error(
284
+ "the message was refused: it comes without the claim of the computer that sent it"
296
285
  );
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
- }
286
+ // A use of a passed secret rides a jam now, never a friendship (docs/jam-plan.md J4.8: "agents arent in dms").
287
+ if (opened.vault) {
288
+ respond(
289
+ acknowledged(
290
+ { ok: false, error: "a secret is used only inside a jam" },
291
+ input
292
+ )
293
+ );
294
+ return;
314
295
  }
296
+ const text = String(opened.text || "").slice(0, 50_000);
315
297
  const known = store.db.all("select 1 from board_messages where id = ?", [
316
298
  String(input.messageId || ""),
317
299
  ]).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;
300
+ const over = known
301
+ ? null
302
+ : (relayRefusal(store.db, {
303
+ taskId: String(input.taskId),
304
+ handle,
305
+ text,
306
+ }) ?? tooFast(handle));
326
307
  if (over) throw new Error(`the message was refused: ${over}`);
327
308
  const received = store.receiveMessage({
328
309
  id: String(input.messageId || ""),
329
310
  taskId: String(input.taskId || ""),
330
311
  seq: input.seq,
331
312
  sender,
332
- text: words.text.slice(0, 50_000),
333
- refs: ownRefs(words.refs, thread, input, sender),
313
+ text,
314
+ refs: ownRefs(
315
+ Array.isArray(opened.refs) ? opened.refs : [],
316
+ input,
317
+ sender
318
+ ),
334
319
  sentAt: input.sentAt,
335
320
  });
336
321
  respond(acknowledged(received, input));
337
322
  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
- );
323
+ notifyBoard(store.getTask(String(input.taskId)));
324
+ notify(`${handle} wrote to you: ${text.slice(0, 100)}`);
363
325
  };
364
326
 
365
327
  const receiveFile = async (input, respond) => {
366
328
  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
- );
329
+ const thread = arrivedOn(input, "file");
372
330
  const remoteId = String(input.sealed.fileId || "").slice(0, 128);
373
331
  const bytes = await openInward(
374
332
  thread,
@@ -385,37 +343,32 @@ export function createSealedThreads({
385
343
  ).length;
386
344
  const over = arrived
387
345
  ? null
388
- : relayRefusal(store.db, {
346
+ : (relayRefusal(store.db, {
389
347
  taskId,
390
348
  handle: handleOf(thread.with_agent),
391
349
  bytes: bytes.length,
392
- });
350
+ }) ?? tooFast(handleOf(thread.with_agent)));
393
351
  if (over) throw new Error(`the file was refused: ${over}`);
394
352
  const received = store.receiveFile({
395
353
  taskId,
396
354
  remoteId,
397
355
  sha256: files.keepBlob(bytes),
398
356
  bytes: bytes.length,
399
- author: `${handleOf(thread.with_agent)}/someone`,
357
+ author: thread.with_agent,
400
358
  });
401
359
  respond(acknowledged(received, input));
402
360
  if (received.ok && !received.duplicate) notifyBoard(store.getTask(taskId));
403
361
  };
404
362
 
405
363
  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
- }
364
+ const thread = arrivedOn(input, "end");
365
+ const opened = await openInward(
366
+ thread,
367
+ `end|${input.taskId}`,
368
+ input.sealed,
369
+ "ending"
370
+ );
371
+ const outcome = String(opened.outcome || "");
419
372
  const closed = store.closeThread(
420
373
  String(input.taskId || ""),
421
374
  outcome.slice(0, 20_000) || null
@@ -425,10 +378,9 @@ export function createSealedThreads({
425
378
  result: closed.ok ? { closed: input.taskId } : null,
426
379
  });
427
380
  if (!closed.ok || closed.already) return;
428
- projects()?.channelEnded(String(input.taskId || ""));
429
381
  notifyBoard(closed.task);
430
382
  notify(
431
- `${input.by} ended the thread on: ${String(closed.task?.objective || "").slice(0, 80)}`
383
+ `${thread.with_agent} ended the thread on: ${String(closed.task?.objective || "").slice(0, 80)}`
432
384
  );
433
385
  };
434
386
 
@@ -443,206 +395,64 @@ export function createSealedThreads({
443
395
  * the acknowledgement Harness relays back, before the board moves; a failure is answered as one.
444
396
  */
445
397
  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
- };
398
+ (identity() ? Promise.resolve() : ensureIdentity())
399
+ .then(() => RECEIVERS[type](input || {}, respond))
400
+ .catch((error) => respond({ ok: false, error: error.message }));
553
401
 
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.
402
+ // A friendship Harness posts on accept, to both computers. Each takes the other's key from Harness and pins it. With
403
+ // no key yet the friendship is not taken here, so nothing can be sent; Harness posts it again on the next
404
+ // connection. A key other than the one pinned before is L5's changed key: the friendship keeps the pinned key and
405
+ // waits, sending and taking nothing, until the person checks the new one (npx tryharness peers trust).
556
406
  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 || ""))
407
+ if (!identity()) await ensureIdentity();
408
+ if (!identity())
585
409
  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
410
+ "this computer does not hold your keys yet; the friendship waits until one of your devices approves it"
595
411
  );
596
- } catch {
412
+ const thread = post.thread;
413
+ const handle = handleOf(thread.with);
414
+ const member = String(thread.with || "").split("/")[1];
415
+ if (!HANDLE.test(handle) || member !== "person")
416
+ throw new Error("a thread is only ever between two people");
417
+ let pinned = store.relayPeer(handle);
418
+ const keyed = await askHarness("relay_key", {
419
+ to: handle,
420
+ ...(pinned?.signing_key ? { since: pinned.signing_key } : {}),
421
+ });
422
+ if (!keyed.ok || typeof keyed.publicKey !== "string")
597
423
  throw new Error(
598
- "the request could not be opened with the key Harness gave for its sender"
424
+ `Harness has no keys for ${handle} yet; the friendship waits until one of their devices makes them`
599
425
  );
426
+ const publicKey = relayPublicKey(keyed.publicKey);
427
+ // Its signing key is pinned with it: what checks that a jam's event is theirs (jamKey.mjs).
428
+ if (!pinned)
429
+ store.pinRelayPeer(handle, publicKey, keyed.signingKey ?? null);
430
+ else if (pinned.public_key !== publicKey) {
431
+ // Their keys changed: followed when the links lead from the keys pinned here, else the friendship waits.
432
+ if (await follow(handle, keyed)) pinned = store.relayPeer(handle);
433
+ else {
434
+ store.meta(`relay-key-waiting:${handle}`, "replaced");
435
+ notify(
436
+ `${handle}'s keys changed. Your friendship with them waits until you check them: npx tryharness peers trust ${handle}`
437
+ );
438
+ }
600
439
  }
440
+ const peer = pinned?.public_key ?? publicKey;
601
441
  return {
602
442
  ...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),
443
+ objective: handle,
444
+ brief: "",
445
+ contextRefs: [],
446
+ targetAgent: "person",
606
447
  thread: {
607
- with: thread.with,
608
- role: "target",
448
+ with: `${handle}/person`,
449
+ role: thread.role === "asker" ? "asker" : "target",
609
450
  peerKey: peer,
610
451
  [OPENED]: true,
611
452
  },
612
453
  };
613
454
  };
614
455
 
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
456
  // The other people's computers this one has pinned a key for, and this computer's own fingerprint.
647
457
  const peers = async () => ({
648
458
  ok: true,
@@ -659,13 +469,9 @@ export function createSealedThreads({
659
469
  return {
660
470
  sendMessage,
661
471
  sendEnd,
662
- endGuests,
663
- sendProjectEvent,
664
472
  receive,
665
473
  receives: (type) => Object.hasOwn(RECEIVERS, type),
666
- request,
667
474
  openPost,
668
- openRequest,
669
475
  peers,
670
476
  };
671
477
  }