tryharness 0.7.83 → 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 (215) 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 +191 -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 +52 -0
  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 +2426 -338
  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 +1 -1
  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/originals.mjs +216 -0
  107. package/lib/outsideEvents.mjs +383 -0
  108. package/lib/outsideRuns.mjs +230 -0
  109. package/lib/ownMachines.mjs +674 -256
  110. package/lib/pain.mjs +215 -0
  111. package/lib/pairing.mjs +91 -11
  112. package/lib/paths.mjs +42 -3
  113. package/lib/peerProcess.mjs +21 -7
  114. package/lib/peopleLink.mjs +268 -0
  115. package/lib/personNotices.mjs +3 -2
  116. package/lib/phones.mjs +252 -542
  117. package/lib/placeCards.mjs +282 -0
  118. package/lib/placeChecks.mjs +95 -0
  119. package/lib/placeCommands.mjs +8 -17
  120. package/lib/placeRequests.mjs +152 -0
  121. package/lib/placeStore.mjs +295 -0
  122. package/lib/placeTool.d.ts +12 -0
  123. package/lib/placeTool.js +40 -0
  124. package/lib/places.mjs +63 -14
  125. package/lib/plans.mjs +53 -8
  126. package/lib/presence.mjs +270 -0
  127. package/lib/privateNetwork.d.ts +2 -0
  128. package/lib/privateNetwork.js +93 -0
  129. package/lib/proposals.mjs +300 -0
  130. package/lib/publicFetch.d.ts +32 -0
  131. package/lib/publicFetch.js +248 -0
  132. package/lib/reach.mjs +128 -0
  133. package/lib/rebuildable.mjs +72 -0
  134. package/lib/recall.mjs +216 -0
  135. package/lib/record.mjs +13 -0
  136. package/lib/reflexes.mjs +392 -0
  137. package/lib/relayLimits.mjs +31 -2
  138. package/lib/releaseNotice.mjs +198 -0
  139. package/lib/removeHarness.mjs +15 -4
  140. package/lib/repoLinks.mjs +204 -35
  141. package/lib/runAuthority.mjs +102 -39
  142. package/lib/runBrief.mjs +93 -40
  143. package/lib/runRequests.mjs +52 -14
  144. package/lib/runUsage.mjs +31 -0
  145. package/lib/runWorker.mjs +74 -25
  146. package/lib/runner.mjs +289 -45
  147. package/lib/runtime-install.mjs +37 -8
  148. package/lib/schedules.mjs +171 -47
  149. package/lib/screenWatches.mjs +221 -0
  150. package/lib/sealed.mjs +12 -30
  151. package/lib/sealedThreads.mjs +198 -392
  152. package/lib/secretStore.mjs +178 -0
  153. package/lib/secrets.mjs +11 -0
  154. package/lib/seen.mjs +3 -9
  155. package/lib/selfUpdate.mjs +51 -15
  156. package/lib/senseCall.mjs +1514 -0
  157. package/lib/senseHook.mjs +114 -0
  158. package/lib/senseTools.mjs +1298 -0
  159. package/lib/sessionBriefs.mjs +87 -0
  160. package/lib/sessionControl.mjs +63 -13
  161. package/lib/sessionFiles.mjs +85 -0
  162. package/lib/setupChoices.mjs +12 -3
  163. package/lib/setupMode.mjs +70 -25
  164. package/lib/sharedKeys.mjs +166 -0
  165. package/lib/sleep.mjs +396 -0
  166. package/lib/sources.mjs +306 -0
  167. package/lib/speech.mjs +166 -411
  168. package/lib/spoken.mjs +210 -0
  169. package/lib/steerHook.mjs +3 -1
  170. package/lib/steerQueue.mjs +40 -11
  171. package/lib/store.mjs +323 -130
  172. package/lib/stuck.mjs +85 -7
  173. package/lib/swarm.mjs +157 -49
  174. package/lib/swarmDevices.mjs +25 -85
  175. package/lib/swarmMemory.mjs +93 -9
  176. package/lib/t3.mjs +73 -3
  177. package/lib/taskCommands.mjs +51 -60
  178. package/lib/taskRooms.mjs +30 -4
  179. package/lib/taskRules.json +29 -0
  180. package/lib/taskRules.mjs +21 -0
  181. package/lib/taskRuns.mjs +74 -11
  182. package/lib/team.mjs +2 -1
  183. package/lib/threadActions.mjs +37 -9
  184. package/lib/threadAuthority.mjs +4 -4
  185. package/lib/threadBriefs.mjs +173 -43
  186. package/lib/threadNotes.mjs +320 -0
  187. package/lib/threadRecord.mjs +491 -0
  188. package/lib/threadRuns.mjs +216 -108
  189. package/lib/threadView.mjs +27 -30
  190. package/lib/threadWatches.mjs +1962 -0
  191. package/lib/timeSense.mjs +839 -0
  192. package/lib/tripwires.mjs +454 -0
  193. package/lib/ui.mjs +1 -1
  194. package/lib/vault.mjs +560 -45
  195. package/lib/vaultCommands.mjs +5 -3
  196. package/lib/vaultPeers.mjs +165 -0
  197. package/lib/vaultTools.d.ts +4 -1
  198. package/lib/vaultTools.js +22 -1
  199. package/lib/verdicts.mjs +12 -26
  200. package/lib/versions.mjs +117 -0
  201. package/lib/waitTool.mjs +278 -0
  202. package/lib/wakePicture.mjs +268 -0
  203. package/lib/wakeQueue.mjs +108 -0
  204. package/lib/watchResponses.mjs +533 -0
  205. package/lib/watches.mjs +290 -145
  206. package/lib/windowAsks.mjs +80 -17
  207. package/lib/worktrees.mjs +159 -9
  208. package/npm-shrinkwrap.json +138 -631
  209. package/package.json +7 -6
  210. package/release.json +211 -129
  211. package/lib/callSessions.mjs +0 -51
  212. package/lib/directLink.mjs +0 -349
  213. package/lib/peopleBoard.mjs +0 -131
  214. package/lib/projects.mjs +0 -2917
  215. package/lib/roomChannels.mjs +0 -667
@@ -1,57 +1,36 @@
1
- import fs from "node:fs";
2
- import { promisify } from "node:util";
3
- import {
4
- createCipheriv,
5
- createDecipheriv,
6
- pbkdf2,
7
- randomBytes,
8
- } from "node:crypto";
1
+ import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
9
2
  import { gunzipSync, gzipSync } from "node:zlib";
10
- import { readJson, writeJsonAtomic } from "./files.mjs";
11
3
 
12
4
  // The board's encrypted backup, for Residents (Harness's server keeps it). The board survives losing a computer,
13
- // encrypted as a Resident's browser backup is. Home seals every board row (boardMirror.mjs's tables, as its pages carry
14
- // them) with a key made from a passphrase only the person knows, and Harness keeps ciphertext it cannot read. Restoring
15
- // brings the rows into the board here the way a computer joining brings its own (boardMirror.mjs bring): what is
16
- // missing comes in, nothing here is removed.
17
- //
18
- // The key is kept beside the board in Harness's folder (0600), so backups go on by themselves: the board is on that
19
- // disk unsealed anyway, and the seal is against Harness's servers. The same on every platform. Thread files' bytes
20
- // (~/.harness/files) are not in it; their records are.
5
+ // encrypted as a Resident's browser backup is: with the person's shared backup key (src/shared/backupKeys.ts), which
6
+ // every device of theirs holds and Harness never sees (device redesign 1.8f; owner, November 9: "one backup secret ...
7
+ // the board passphrase goes"). Home seals every board row (boardMirror.mjs's tables, as its pages carry them) and the
8
+ // vault's secrets (vault.mjs; 1claw's copy stays separate), opening the person's keys for that moment only
9
+ // (computerDevices.mjs sharedBackupKeys). Restoring brings the rows into the board here the way a computer joining
10
+ // brings its own (boardMirror.mjs bring): what is missing comes in, nothing here is removed; the vault comes back only
11
+ // where this computer's is empty. Thread files' bytes (~/.harness/files) are not in it; their records are.
21
12
 
22
- const MAGIC = Buffer.from("HRB1");
23
- const SALT_BYTES = 16;
13
+ const MAGIC = Buffer.from("HRB2");
24
14
  const IV_BYTES = 12;
25
15
  const TAG_BYTES = 16;
26
- // As the browser's backup client derives its key, kept in step with it.
27
- const ITERATIONS = 210_000;
28
16
  const PART_BYTES = 512 * 1024;
29
- const MIN_PASSPHRASE = 8;
30
17
  // How often home looks whether the board changed since its last backup. A product cadence, not a deadline.
31
18
  export const BACKUP_EVERY_MS = 60 * 60_000;
32
19
  const LAST = "board-backup-last";
20
+ const ON = "board-backup-on";
33
21
 
34
- const derive = (passphrase, salt) =>
35
- promisify(pbkdf2)(String(passphrase), salt, ITERATIONS, 32, "sha256");
36
-
37
- function seal(key, salt, plain) {
22
+ function seal(key, plain) {
38
23
  const iv = randomBytes(IV_BYTES);
39
24
  const cipher = createCipheriv("aes-256-gcm", key, iv);
40
25
  const sealed = Buffer.concat([cipher.update(plain), cipher.final()]);
41
- return Buffer.concat([MAGIC, salt, iv, cipher.getAuthTag(), sealed]);
42
- }
43
-
44
- /** The salt a sealed backup was made with, or null when it is not one. */
45
- function saltOf(blob) {
46
- if (blob.length < MAGIC.length + SALT_BYTES + IV_BYTES + TAG_BYTES)
47
- return null;
48
- if (!blob.subarray(0, MAGIC.length).equals(MAGIC)) return null;
49
- return blob.subarray(MAGIC.length, MAGIC.length + SALT_BYTES);
26
+ return Buffer.concat([MAGIC, iv, cipher.getAuthTag(), sealed]);
50
27
  }
51
28
 
52
- /** The backup opened with this key; throws when the key is not the one it was sealed with. */
29
+ /** The backup opened with this key; throws when it is not the key it was sealed with, or not a backup at all. */
53
30
  function open(key, blob) {
54
- let at = MAGIC.length + SALT_BYTES;
31
+ if (!blob.subarray(0, MAGIC.length).equals(MAGIC))
32
+ throw new Error("not a board backup");
33
+ let at = MAGIC.length;
55
34
  const iv = blob.subarray(at, (at += IV_BYTES));
56
35
  const tag = blob.subarray(at, (at += TAG_BYTES));
57
36
  const decipher = createDecipheriv("aes-256-gcm", key, iv);
@@ -59,32 +38,29 @@ function open(key, blob) {
59
38
  return Buffer.concat([decipher.update(blob.subarray(at)), decipher.final()]);
60
39
  }
61
40
 
41
+ // The keys are dropped from memory once used.
42
+ const forget = (keys) => {
43
+ keys.current?.fill(0);
44
+ for (const key of keys.previous ?? []) key.fill(0);
45
+ };
46
+
62
47
  export function createBoardBackup({
63
48
  store,
64
49
  log,
65
50
  mirror,
66
51
  // A request to Harness over this computer's connection (bridge.mjs ask).
67
52
  ask,
68
- keyFile,
53
+ // The person's shared backup keys, opened for the moment (computerDevices.mjs sharedBackupKeys): { current,
54
+ // previous }, raw 32-byte keys, newest first.
55
+ sharedBackupKeys,
56
+ // The vault, carried in the backup (vault.mjs backupCopy, restoreCopy); null leaves it out.
57
+ vault = null,
69
58
  // Whether this computer holds the board: only home backs it up.
70
59
  holdsBoard,
71
60
  label,
72
61
  everyMs = BACKUP_EVERY_MS,
73
62
  }) {
74
- const readKey = () => {
75
- const saved = readJson(keyFile, null);
76
- return saved?.salt && saved?.key
77
- ? {
78
- salt: Buffer.from(saved.salt, "base64"),
79
- key: Buffer.from(saved.key, "base64"),
80
- }
81
- : null;
82
- };
83
- const saveKey = ({ salt, key }) =>
84
- writeJsonAtomic(keyFile, {
85
- salt: salt.toString("base64"),
86
- key: key.toString("base64"),
87
- });
63
+ const isOn = () => store.meta(ON) === "1";
88
64
  const last = () => JSON.parse(store.meta(LAST) || "null");
89
65
 
90
66
  // Every row of the board, as the mirror's pages carry them.
@@ -108,7 +84,7 @@ export function createBoardBackup({
108
84
  }));
109
85
  return {
110
86
  ok: true,
111
- on: Boolean(readKey()),
87
+ on: isOn(),
112
88
  entitled:
113
89
  server.ok === true
114
90
  ? true
@@ -134,27 +110,40 @@ export function createBoardBackup({
134
110
  code: "not_home",
135
111
  error: "Your home computer backs up the board.",
136
112
  };
137
- const saved = readKey();
138
- if (!saved)
113
+ if (!isOn())
139
114
  return {
140
115
  ok: false,
141
- code: "no_passphrase",
116
+ code: "off",
142
117
  error:
143
118
  "Backups are off on this computer: npx tryharness backup on turns them on.",
144
119
  };
120
+ const keys = await sharedBackupKeys().catch((error) => ({
121
+ error: error.message,
122
+ }));
123
+ if (!keys?.current)
124
+ return {
125
+ ok: false,
126
+ code: "no_keys",
127
+ error:
128
+ keys?.error ||
129
+ "This computer does not hold your keys yet. Approve it from one of your devices.",
130
+ };
145
131
  const head = mirror.head();
146
132
  const changes = snapshot();
133
+ const vaultCopy = vault ? await vault.backupCopy().catch(() => null) : null;
147
134
  const plain = gzipSync(
148
135
  Buffer.from(
149
136
  JSON.stringify({
150
- format: 1,
137
+ format: 2,
151
138
  at: new Date().toISOString(),
152
139
  from: label(),
153
140
  changes,
141
+ ...(vaultCopy ? { vault: vaultCopy } : {}),
154
142
  })
155
143
  )
156
144
  );
157
- const blob = seal(saved.key, saved.salt, plain);
145
+ const blob = seal(keys.current, plain);
146
+ forget(keys);
158
147
  const uploadId = randomBytes(12).toString("base64url");
159
148
  const count = Math.max(1, Math.ceil(blob.length / PART_BYTES));
160
149
  let answer = null;
@@ -193,16 +182,9 @@ export function createBoardBackup({
193
182
  return { ok: true, rows: changes.length, bytes: blob.length, stored };
194
183
  };
195
184
 
196
- /** The person chose a passphrase: backups go on from now, the first one at once. */
197
- const turnOn = async (passphrase) => {
198
- if (String(passphrase ?? "").length < MIN_PASSPHRASE)
199
- return {
200
- ok: false,
201
- code: "short",
202
- error: `A backup passphrase is at least ${MIN_PASSPHRASE} characters.`,
203
- };
204
- const salt = randomBytes(SALT_BYTES);
205
- saveKey({ salt, key: await derive(passphrase, salt) });
185
+ /** The person turned backups on: they go on from now, the first one at once. */
186
+ const turnOn = async () => {
187
+ store.meta(ON, "1");
206
188
  const first = await backUp();
207
189
  // Refused (not a Resident, or not home): backups stay off rather than seem on.
208
190
  if (!first.ok && first.code !== undefined) turnOff();
@@ -210,12 +192,12 @@ export function createBoardBackup({
210
192
  };
211
193
 
212
194
  const turnOff = () => {
213
- fs.rmSync(keyFile, { force: true });
195
+ store.meta(ON, "0");
214
196
  return { ok: true };
215
197
  };
216
198
 
217
- /** The newest backup (or the version asked for), opened with the person's passphrase and brought into the board. */
218
- const restore = async (passphrase, { version } = {}) => {
199
+ /** The newest backup (or the version asked for), opened with the person's shared keys and brought in here. */
200
+ const restore = async ({ version } = {}) => {
219
201
  const parts = [];
220
202
  let count = 1;
221
203
  let found = null;
@@ -235,24 +217,42 @@ export function createBoardBackup({
235
217
  parts.push(Buffer.from(String(answer.data), "base64"));
236
218
  }
237
219
  const blob = Buffer.concat(parts);
238
- const salt = saltOf(blob);
239
- if (!salt)
240
- return { ok: false, error: "That backup is not one Harness can open." };
241
- const key = await derive(passphrase, salt);
242
- let plain;
243
- try {
244
- plain = open(key, blob);
245
- } catch {
220
+ const keys = await sharedBackupKeys().catch((error) => ({
221
+ error: error.message,
222
+ }));
223
+ if (!keys?.current)
246
224
  return {
247
225
  ok: false,
248
- code: "wrong_passphrase",
249
- error: "That is not the passphrase this backup was made with.",
226
+ code: "no_keys",
227
+ error:
228
+ keys?.error ||
229
+ "This computer does not hold your keys yet. Approve it from one of your devices.",
230
+ };
231
+ // Sealed with the keys of now or of before a device was removed: each is tried, newest first.
232
+ let plain = null;
233
+ for (const key of [keys.current, ...keys.previous])
234
+ try {
235
+ plain = open(key, blob);
236
+ break;
237
+ } catch {
238
+ // the next key
239
+ }
240
+ forget(keys);
241
+ if (!plain)
242
+ return {
243
+ ok: false,
244
+ code: "unreadable",
245
+ error:
246
+ "None of your keys opens that backup: it was made before your devices were started fresh, or with a passphrase.",
250
247
  };
251
- }
252
248
  const backup = JSON.parse(gunzipSync(plain).toString("utf8"));
253
249
  mirror.merge({ changes: backup.changes ?? [] }, { bring: true });
254
- // The same passphrase goes on backing up the board from here.
255
- saveKey({ salt, key });
250
+ const vaultBack =
251
+ backup.vault && vault
252
+ ? await vault.restoreCopy(backup.vault).catch(() => false)
253
+ : false;
254
+ // Backups go on from this computer from here.
255
+ store.meta(ON, "1");
256
256
  log(
257
257
  `brought back the board's backup from ${backup.from || "another computer"} (${backup.at}, ${backup.changes?.length ?? 0} rows)`
258
258
  );
@@ -262,12 +262,13 @@ export function createBoardBackup({
262
262
  from: backup.from ?? null,
263
263
  at: backup.at ?? null,
264
264
  rows: backup.changes?.length ?? 0,
265
+ vault: vaultBack,
265
266
  };
266
267
  };
267
268
 
268
269
  // Home looks now and then whether the board changed since its last backup.
269
270
  const timer = setInterval(() => {
270
- if (!readKey() || !holdsBoard() || last()?.head === mirror.head()) return;
271
+ if (!isOn() || !holdsBoard() || last()?.head === mirror.head()) return;
271
272
  void backUp();
272
273
  }, everyMs);
273
274
  timer.unref?.();
@@ -1,5 +1,17 @@
1
1
  import { boardCommandOwner } from "./boardProtocol.mjs";
2
2
 
3
+ // The fields a request names a task by. A friendship's thread is the person's alone (docs/jam-plan.md J2): no agent
4
+ // reads it, posts into it, claims it, files into it or names it as its work.
5
+ const TASK_FIELDS = ["id", "parentId", "thread", "threadId", "goalId", "work"];
6
+
7
+ // Harness posting a friendship on accept, to the asker's computer as the person and to the other's in their name
8
+ // (relay/threads.ts postThreadToMachines); the post itself carries no words (sealedThreads.mjs openPost).
9
+ const friendshipPost = (request, agent) =>
10
+ request.command === "board.post" &&
11
+ Boolean(request.thread) &&
12
+ typeof request.thread === "object" &&
13
+ (agent === "user" || agent.endsWith("/person"));
14
+
3
15
  // Every transport enters the same request context before a feature applies its
4
16
  // own rules. Routing selects an implementation; it grants no execution authority.
5
17
  export function createBoardCommands({
@@ -17,6 +29,12 @@ export function createBoardCommands({
17
29
  return { ok: false, error: "board requests need an agent name" };
18
30
  if (request.command === "board.post") touchListener();
19
31
  const agent = String(request.agent);
32
+ if (
33
+ agent !== "user" &&
34
+ !friendshipPost(request, agent) &&
35
+ namesFriendship(store, request)
36
+ )
37
+ return { ok: false, error: "task not found" };
20
38
  // The computer the call came from: this one, or a worker whose call home opened (ownMachines.mjs).
21
39
  const machine = machineOf(request);
22
40
  store.touchAgent(agent, machine);
@@ -36,3 +54,13 @@ export function createBoardCommands({
36
54
  : answered(request, answer);
37
55
  };
38
56
  }
57
+
58
+ function namesFriendship(store, request) {
59
+ const named = TASK_FIELDS.map((field) => request[field]).filter(
60
+ (value) => typeof value === "string" && value
61
+ );
62
+ const file =
63
+ typeof request.fileId === "string" ? store.file(request.fileId) : null;
64
+ if (file?.task_id) named.push(file.task_id);
65
+ return named.some((id) => store.getThread(id));
66
+ }
@@ -2,6 +2,17 @@ import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import { documentText, ensureDir, isDocument, mimeFor } from "./files.mjs";
4
4
  import { paths } from "./paths.mjs";
5
+ import { callerRun } from "./runAuthority.mjs";
6
+ import { deniedToRuns } from "./runner.mjs";
7
+
8
+ // The file a path names, its links followed, or null when it is not one.
9
+ const realFile = (file) => {
10
+ try {
11
+ return fs.realpathSync(file);
12
+ } catch {
13
+ return null;
14
+ }
15
+ };
5
16
 
6
17
  export function createBoardFileCommands({
7
18
  FILE_LIMITS,
@@ -11,9 +22,11 @@ export function createBoardFileCommands({
11
22
  MB,
12
23
  blobPath,
13
24
  fileLimit,
25
+ fileAdded,
14
26
  fileLine,
15
27
  fileName,
16
28
  fileView,
29
+ joinFileParts,
17
30
  keepBlob,
18
31
  namedPath,
19
32
  notifyBoard,
@@ -37,14 +50,19 @@ export function createBoardFileCommands({
37
50
  error:
38
51
  "an agent reaching this computer through Harness attaches a file's content (data), not a path",
39
52
  };
40
- const file = String(request.path);
41
- const stat = path.isAbsolute(file)
42
- ? fs.statSync(file, { throwIfNoEntry: false })
43
- : null;
53
+ const named = String(request.path);
54
+ const file = path.isAbsolute(named) ? realFile(named) : null;
55
+ const stat = file ? fs.statSync(file, { throwIfNoEntry: false }) : null;
44
56
  if (!stat?.isFile())
45
57
  return {
46
58
  ok: false,
47
- error: `${file} is not an absolute path to a file`,
59
+ error: `${named} is not an absolute path to a file`,
60
+ };
61
+ // The daemon reads what a run asks it to attach, so a run's own sandbox decides what it may name.
62
+ if (callerRun(request) && deniedToRuns(file))
63
+ return {
64
+ ok: false,
65
+ error: `${named} is where the person keeps credentials and keys, which a run does not read.`,
48
66
  };
49
67
  if (stat.size > MAX_FILE_BYTES)
50
68
  return tooBig(
@@ -59,37 +77,10 @@ export function createBoardFileCommands({
59
77
  ok: false,
60
78
  error: "give the file's path or its content as base64 data",
61
79
  };
62
- // A file sent in parts, in order (a sealed message carries about 1 MB): kept aside until its last part arrives.
63
80
  if (request.upload !== undefined) {
64
- const upload = String(request.upload),
65
- offset = Number(request.offset),
66
- total = Number(request.total);
67
- const partial = path.join(paths.files, "uploads", `${agent}-${upload}`);
68
- if (
69
- !/^[\w-]{8,64}$/.test(upload) ||
70
- !Number.isSafeInteger(total) ||
71
- total > MAX_FILE_BYTES ||
72
- offset !==
73
- (offset ? fs.statSync(partial, { throwIfNoEntry: false })?.size : 0)
74
- )
75
- return {
76
- ok: false,
77
- error:
78
- "a part must follow the one before it, within the file's size",
79
- };
80
- ensureDir(path.dirname(partial));
81
- (offset ? fs.appendFileSync : fs.writeFileSync)(partial, bytes, {
82
- mode: 0o600,
83
- });
84
- if (offset + bytes.length < total)
85
- return { ok: true, received: offset + bytes.length };
86
- bytes = fs.readFileSync(partial);
87
- fs.rmSync(partial, { force: true });
88
- if (bytes.length !== total)
89
- return {
90
- ok: false,
91
- error: `the parts came to ${bytes.length} of the file's ${total} bytes`,
92
- };
81
+ const part = joinFileParts(request, agent, bytes);
82
+ if (part.reply) return part.reply;
83
+ bytes = part.bytes;
93
84
  }
94
85
  if (!bytes.length) return { ok: false, error: "the file is empty" };
95
86
  const name = fileName(
@@ -127,6 +118,8 @@ export function createBoardFileCommands({
127
118
  });
128
119
  notifyBoard(store.getTask(place));
129
120
  readFilesSoon();
121
+ // In a jam's room the file goes to everyone in the jam (jams.mjs fileAdded).
122
+ fileAdded(place, file, machine);
130
123
  return { ok: true, file: fileView(file, request.hostedCaller) };
131
124
  } else if (request.command === "board.file.share") {
132
125
  // Whether the agents on Harness's side may read a file: the person's call, either way, on any file. A call through
@@ -141,7 +134,7 @@ export function createBoardFileCommands({
141
134
  ok: false,
142
135
  code: "sealed_only",
143
136
  error:
144
- "share a file from the buddy or a browser linked to this computer: npx tryharness link",
137
+ "share a file from the buddy, or from Harness in a browser or phone on your devices",
145
138
  };
146
139
  const file = store.file(String(request.fileId || ""));
147
140
  if (!file) return { ok: false, error: "file not found" };
@@ -3,7 +3,7 @@ import fs from "node:fs";
3
3
  import os from "node:os";
4
4
  import path from "node:path";
5
5
  import { spawnSync } from "node:child_process";
6
- import { createHash } from "node:crypto";
6
+ import { createHash, randomUUID } from "node:crypto";
7
7
  import {
8
8
  documentText,
9
9
  ensureDir,
@@ -51,7 +51,66 @@ export const FILE_PART_BYTES = 512 * 1024;
51
51
  export const RAW_FILE_PART_BYTES = 960 * 1024;
52
52
  // The most files one search across the person's threads answers with.
53
53
  const FOUND_FILES = 20;
54
- const fileLimit = (mime, bytes) =>
54
+ export const fileName = (name) =>
55
+ String(name || "")
56
+ .replace(/[\\/\u0000-\u001f]/g, "")
57
+ .replace(/^\.+/, "")
58
+ .trim()
59
+ .slice(0, 200) || "file";
60
+
61
+ /**
62
+ * A file sent in parts, in order (a sealed message carries about 1 MB): kept aside until its last part. `{ reply }` while
63
+ * more is to come or a part is refused, `{ bytes }` once the file is whole.
64
+ */
65
+ export function joinFileParts(request, owner, bytes) {
66
+ const upload = String(request.upload),
67
+ offset = Number(request.offset),
68
+ total = Number(request.total);
69
+ const partial = path.join(paths.files, "uploads", `${owner}-${upload}`);
70
+ if (
71
+ !/^[\w-]{8,64}$/.test(upload) ||
72
+ !Number.isSafeInteger(total) ||
73
+ total > MAX_FILE_BYTES ||
74
+ offset !==
75
+ (offset ? fs.statSync(partial, { throwIfNoEntry: false })?.size : 0)
76
+ )
77
+ return {
78
+ reply: {
79
+ ok: false,
80
+ error: "a part must follow the one before it, within the file's size",
81
+ },
82
+ };
83
+ ensureDir(path.dirname(partial));
84
+ (offset ? fs.appendFileSync : fs.writeFileSync)(partial, bytes, {
85
+ mode: 0o600,
86
+ });
87
+ if (offset + bytes.length < total)
88
+ return { reply: { ok: true, received: offset + bytes.length } };
89
+ const whole = fs.readFileSync(partial);
90
+ fs.rmSync(partial, { force: true });
91
+ if (whole.length !== total)
92
+ return {
93
+ reply: {
94
+ ok: false,
95
+ error: `the parts came to ${whole.length} of the file's ${total} bytes`,
96
+ },
97
+ };
98
+ return { bytes: whole };
99
+ }
100
+
101
+ // How far into a file its bytes are read to tell text from binary.
102
+ const TEXT_SNIFF_BYTES = 8_192;
103
+ /**
104
+ * Whether a file's bytes are to be read as text and checked for secrets before they leave: text by its type, or by its
105
+ * bytes (no NUL in its first part, as binary formats have). The type is whatever the attaching caller said it was, so
106
+ * a key file labelled as an image or an octet stream is checked all the same.
107
+ */
108
+ export const checkedAsText = (mime, bytes) =>
109
+ textType(String(mime)) || !bytes.subarray(0, TEXT_SNIFF_BYTES).includes(0);
110
+ const textType = (mime) =>
111
+ /^text\/|^application\/(json|xml|yaml|x-yaml)$|^image\/svg\+xml$/.test(mime);
112
+
113
+ export const fileLimit = (mime, bytes) =>
55
114
  FILE_LIMITS.find((limit) =>
56
115
  limit.test(mime, bytes.subarray(0, 5).toString("latin1"))
57
116
  );
@@ -68,13 +127,27 @@ export function createBoardFiles({
68
127
  visibleTo,
69
128
  processing,
70
129
  wakeFor,
130
+ // A file added to a thread on this computer (jams.mjs fileAdded: a jam's room shares it).
131
+ fileAdded = () => {},
71
132
  }) {
72
133
  const blobPath = (sha256) => path.join(paths.files, sha256);
134
+ // Whether the bytes kept under a hash are all there and are those bytes: a write cut short (the daemon stopped, the
135
+ // disk filled) leaves a file under the right name with the wrong bytes.
136
+ const blobIntact = (sha256, size) => {
137
+ const file = blobPath(sha256);
138
+ const stat = fs.statSync(file, { throwIfNoEntry: false });
139
+ if (!stat || stat.size !== size) return false;
140
+ const kept = createHash("sha256").update(fs.readFileSync(file));
141
+ return kept.digest("hex") === sha256;
142
+ };
143
+ // Written beside its place and renamed into it, so a stored blob is only ever whole.
73
144
  const keepBlob = (bytes) => {
74
145
  const sha256 = createHash("sha256").update(bytes).digest("hex");
75
146
  ensureDir(paths.files);
76
- if (!fs.existsSync(blobPath(sha256)))
77
- fs.writeFileSync(blobPath(sha256), bytes, { mode: 0o600 });
147
+ if (blobIntact(sha256, bytes.length)) return sha256;
148
+ const temporary = `${blobPath(sha256)}.tmp-${process.pid}-${randomUUID()}`;
149
+ fs.writeFileSync(temporary, bytes, { mode: 0o600 });
150
+ fs.renameSync(temporary, blobPath(sha256));
78
151
  return sha256;
79
152
  };
80
153
  // A caller through Harness sees a file only on a task it is part of; the person and this computer's agents see every file.
@@ -83,12 +156,6 @@ export function createBoardFiles({
83
156
  agent === "user" ||
84
157
  agent === "harness" ||
85
158
  visibleTo(store.getTask(file.task_id), agent);
86
- const fileName = (name) =>
87
- String(name || "")
88
- .replace(/[\\/\u0000-\u001f]/g, "")
89
- .replace(/^\.+/, "")
90
- .trim()
91
- .slice(0, 200) || "file";
92
159
  // For an agent here, the file under its own name (a hard link to the stored bytes), so its tools see what it is.
93
160
  const namedPath = (file) => {
94
161
  const named = path.join(paths.files, "named", file.id, file.name);
@@ -118,10 +185,7 @@ export function createBoardFiles({
118
185
  ...(file.present ? { path: namedPath(file) } : {}),
119
186
  }),
120
187
  });
121
- const textLike = (mime) =>
122
- /^text\/|^application\/(json|xml|yaml|x-yaml)$|^image\/svg\+xml$/.test(
123
- mime
124
- );
188
+ const textLike = textType;
125
189
  // The files of a thread (its goal and every task in it), or of a task on its own.
126
190
  const placeFiles = (place) =>
127
191
  [place, ...(place.kind === "goal" ? store.children(place.id) : [])].flatMap(
@@ -396,7 +460,8 @@ export function createBoardFiles({
396
460
  data: bytes.toString("base64"),
397
461
  };
398
462
  if (file.author === "harness") return partOf(answer, input);
399
- const flag = textLike(file.mime) && secretReason(bytes.toString("utf8"));
463
+ const flag =
464
+ checkedAsText(file.mime, bytes) && secretReason(bytes.toString("utf8"));
400
465
  if (flag)
401
466
  return {
402
467
  ok: false,
@@ -497,6 +562,7 @@ export function createBoardFiles({
497
562
  text: `harness attached ${fileLine(file)}`,
498
563
  source: `file:${file.id}`,
499
564
  });
565
+ fileAdded(place.id, file);
500
566
  }
501
567
  store.meta(
502
568
  `harness-files:${task.id}`,
@@ -601,6 +667,7 @@ export function createBoardFiles({
601
667
  `${file.name} (${file.mime}, ${(file.bytes / MB).toFixed(1)} MB), file ${file.id}; read it with get_file`;
602
668
 
603
669
  const command = createBoardFileCommands({
670
+ joinFileParts,
604
671
  FILE_LIMITS,
605
672
  FILE_PART_BYTES,
606
673
  RAW_FILE_PART_BYTES,
@@ -608,6 +675,7 @@ export function createBoardFiles({
608
675
  MB,
609
676
  blobPath,
610
677
  fileLimit,
678
+ fileAdded,
611
679
  fileLine,
612
680
  fileName,
613
681
  fileView,