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
@@ -0,0 +1,189 @@
1
+ import crypto from "node:crypto";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ import { spawnSync } from "node:child_process";
5
+ import { nameOf } from "./members.mjs";
6
+
7
+ // Going in circles (docs/senses-plan.md S3.6): while an agent works, the same command run again and again with nothing
8
+ // changed in between, or a file put back to how it was before, again. What counts is the owner's ("yeah i think this is
9
+ // sensible to start", November 13): the same command 3 times in one turn with no file changed between, or a file
10
+ // returned to an earlier version twice in one turn. Harness tells the agent itself, as a reading at its next step
11
+ // (steerQueue.mjs SENSE), and keeps it on the log for the thread's record (S3.7); the thread itself says nothing and the
12
+ // person gets no notice (owner, the same day: "i think autocorrect and thread record (very subtle)").
13
+ //
14
+ // A run's steps come from its own stream (runner.mjs stepsOf): the commands it runs and the files its edit tools
15
+ // change. A thread's run also follows its folder (fileSource.mjs), for what its shell commands change. A file's version
16
+ // is its git blob id, so a file put back to its committed version counts as returned too.
17
+ const SAME_COMMAND = 3;
18
+ const RETURNS = 2;
19
+ // A larger file is not compared: reading it at each change would cost more than going in circles does.
20
+ const LARGEST_BYTES = 2_000_000;
21
+ // At most this many files of one run are compared: a turn changing more is not going in circles on one of them.
22
+ const MOST_FILES = 200;
23
+ // A command or a file is named this long in what the agent is told.
24
+ const NAMED_CHARS = 200;
25
+
26
+ /** A file's git blob id (what `git hash-object` says), or null when it is gone, not a file, or too large. */
27
+ function versionOf(file) {
28
+ try {
29
+ const stat = fs.statSync(file);
30
+ if (!stat.isFile() || stat.size > LARGEST_BYTES) return null;
31
+ const bytes = fs.readFileSync(file);
32
+ return crypto
33
+ .createHash("sha1")
34
+ .update(`blob ${bytes.length}\0`)
35
+ .update(bytes)
36
+ .digest("hex");
37
+ } catch {
38
+ return null;
39
+ }
40
+ }
41
+
42
+ /** The version a file has in its repository's index (its committed one, as a turn starts), or null outside one. */
43
+ function indexedVersionOf(file) {
44
+ const said = spawnSync(
45
+ "git",
46
+ ["-C", path.dirname(file), "ls-files", "-s", "--", path.basename(file)],
47
+ { encoding: "utf8", timeout: 2_000 }
48
+ );
49
+ if (said.status !== 0) return null;
50
+ return /^\d+ ([0-9a-f]{40}) /.exec(said.stdout)?.[1] ?? null;
51
+ }
52
+
53
+ const oneLine = (text) =>
54
+ String(text ?? "")
55
+ .split(/\s+/)
56
+ .join(" ")
57
+ .trim();
58
+
59
+ /**
60
+ * The watch on runs. `follow(folder, onChange)` follows a folder's file changes (`onChange({ at, changed })`, names
61
+ * under the folder) and returns what stops it; `tell(run, text)` gives the agent a reading at its next step;
62
+ * `events` is the log. A run is `{ runId, agent, sessionId, cwd, thread }`.
63
+ */
64
+ export function createCircling({
65
+ events,
66
+ follow = () => () => {},
67
+ tell = () => {},
68
+ log = () => {},
69
+ now = () => Date.now(),
70
+ }) {
71
+ const runs = new Map();
72
+
73
+ const found = (state, { what, text, told, data }) => {
74
+ const { run } = state;
75
+ events.append({
76
+ source: "stuck",
77
+ kind: "circling",
78
+ subject: `run:${run.runId}`,
79
+ text,
80
+ data: {
81
+ what,
82
+ agent: run.agent,
83
+ run: run.runId,
84
+ thread: run.thread ?? null,
85
+ ...data,
86
+ },
87
+ });
88
+ log(`circling: ${run.runId} ${what}`);
89
+ try {
90
+ tell(run, told);
91
+ } catch (error) {
92
+ log(`circling: telling ${run.runId}: ${error.message}`);
93
+ }
94
+ };
95
+
96
+ // A file changed: compared with every version it had in this turn; the second time it is back to one, the agent is told.
97
+ const fileChanged = (state, file) => {
98
+ state.commands.clear();
99
+ let versions = state.files.get(file);
100
+ if (!versions) {
101
+ if (state.files.size >= MOST_FILES) return;
102
+ const indexed = indexedVersionOf(file);
103
+ versions = { seen: indexed ? [indexed] : [], returns: 0, told: false };
104
+ state.files.set(file, versions);
105
+ }
106
+ const version = versionOf(file);
107
+ if (!version || version === versions.seen.at(-1)) return;
108
+ if (versions.seen.includes(version)) versions.returns++;
109
+ versions.seen.push(version);
110
+ if (versions.returns < RETURNS || versions.told) return;
111
+ versions.told = true;
112
+ const name = path.relative(state.run.cwd || "/", file) || file;
113
+ found(state, {
114
+ what: "file",
115
+ text: `${name} went back to an earlier version twice in ${nameOf(state.run.agent)}'s turn`,
116
+ told: `${name.slice(0, NAMED_CHARS)} has gone back to an earlier version of itself ${versions.returns} times in this turn.`,
117
+ data: { file: name, returns: versions.returns },
118
+ });
119
+ };
120
+
121
+ const commandRan = (state, command) => {
122
+ const times = (state.commands.get(command) ?? 0) + 1;
123
+ state.commands.set(command, times);
124
+ if (times < SAME_COMMAND || state.toldCommands.has(command)) return;
125
+ state.toldCommands.add(command);
126
+ // The command stays out of the log: it can hold anyone's words (a commit message); the agent ran it and is told.
127
+ found(state, {
128
+ what: "command",
129
+ text: `${nameOf(state.run.agent)} ran one command ${times} times with no file changed between`,
130
+ told: `You have run \`${command.slice(0, NAMED_CHARS)}\` ${times} times in this turn with no file changed in between.`,
131
+ data: { times },
132
+ });
133
+ };
134
+
135
+ const stateOf = (run) => {
136
+ let state = runs.get(run.runId);
137
+ if (state) {
138
+ // The session is known once the agent says it, after its first steps.
139
+ state.run = { ...state.run, ...run };
140
+ return state;
141
+ }
142
+ const startedAt = now();
143
+ state = {
144
+ run,
145
+ commands: new Map(),
146
+ toldCommands: new Set(),
147
+ files: new Map(),
148
+ stop: () => {},
149
+ };
150
+ runs.set(run.runId, state);
151
+ // A thread's run follows its folder, for what its shell commands change; a change from before the run is not its.
152
+ if (run.thread && run.cwd) {
153
+ const folder = run.cwd;
154
+ try {
155
+ state.stop = follow(folder, (reading) => {
156
+ if (!runs.has(run.runId) || Number(reading?.at ?? 0) < startedAt)
157
+ return;
158
+ state.commands.clear();
159
+ for (const name of reading.changed ?? [])
160
+ fileChanged(state, path.resolve(folder, name));
161
+ });
162
+ } catch (error) {
163
+ log(`circling: following ${folder}: ${error.message}`);
164
+ }
165
+ }
166
+ return state;
167
+ };
168
+
169
+ return {
170
+ /** A step a run took (runner.mjs stepsOf): `command` it ran, `files` its edit tools changed. */
171
+ step(run, step) {
172
+ if (!run?.runId || !step) return;
173
+ const state = stateOf(run);
174
+ for (const file of step.files ?? [])
175
+ if (file) fileChanged(state, path.resolve(run.cwd || "/", file));
176
+ const command = oneLine(step.command);
177
+ if (command) commandRan(state, command);
178
+ },
179
+ /** A run ended: nothing more of it is followed. */
180
+ ended(runId) {
181
+ const state = runs.get(runId);
182
+ if (!state) return;
183
+ runs.delete(runId);
184
+ try {
185
+ state.stop();
186
+ } catch {}
187
+ },
188
+ };
189
+ }
@@ -0,0 +1,234 @@
1
+ import fs from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { rebuildableUnder, sizeOf } from "./rebuildable.mjs";
5
+
6
+ // Cleanup (docs/senses-plan.md S2.6, ledger row 33): what Harness keeps falls in four groups by what losing it costs
7
+ // (owner, November 12). The person's (the board, changes never merged, attached files, what agents made): never
8
+ // deleted here; when large and old, the person is told once, with its size and place, and decides. Rebuildable
9
+ // (dependency folders, build output, caches, known by kind in rebuildable.mjs): deleted from a thread's copy once it
10
+ // is unused for 7 days, and, when the disk turns tight, from the least recently used copies first until there is
11
+ // room, never while a run works in one. History: the log keeps 30 days and at most 100 MB (events.mjs); run records
12
+ // stay until sleep can fold them (S8), since folding comes before forgetting. Scratch: the temporary folders the daemon
13
+ // makes for one piece of work, gone a day after a crash left one.
14
+ //
15
+ // Trimmed once a day while the computer is idle (the person away, no run going), and at once when the disk turns
16
+ // tight, in that order. Cleanup is not a reflex: a reflex never deletes. A thread's branch the person merged outside
17
+ // Harness counts as merged here (worktrees.mjs mergedElsewhere). Downloaded models are not deleted yet: nothing says
18
+ // when a model was last used, so one in use could go.
19
+
20
+ const DAY_MS = 86_400_000;
21
+ const UNUSED_DAYS = 7;
22
+ // The person's things are named to them once they are this large and this old.
23
+ const LARGE_BYTES = 1e9;
24
+ const OLD_DAYS = 14;
25
+ const LOOK_EVERY_MS = 30 * 60_000;
26
+ // The temporary folders the daemon makes for one piece of work and removes after it (boardFiles, cloudAssistants,
27
+ // cloudSave, files, listen, media).
28
+ const SCRATCH = [
29
+ "harness-picture-",
30
+ "harness-openclaw-",
31
+ "harness-copy-",
32
+ // A try's copy (senseTools.mjs), deleted when the try ends; a day at most if Harness stopped first.
33
+ "harness-try-",
34
+ "harness-restore-",
35
+ "harness-pdf-",
36
+ "harness-listen-",
37
+ "harness-media-",
38
+ ];
39
+ // A thread's copy is its thread's id, with a number after it when a kept one held the usual folder.
40
+ const COPY_NAME =
41
+ /^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})(?:-\d+)?$/;
42
+
43
+ const listed = (folder) => {
44
+ try {
45
+ return fs.readdirSync(folder);
46
+ } catch {
47
+ return [];
48
+ }
49
+ };
50
+ const gb = (bytes) =>
51
+ bytes >= 1e9
52
+ ? `${(bytes / 1e9).toFixed(1)} GB`
53
+ : `${Math.max(1, Math.round(bytes / 1e6))} MB`;
54
+
55
+ /**
56
+ * `trim(why)` ("daily" or "tight") clears what may go and tells what may not; `maybeDaily()` trims when a day has passed
57
+ * and the computer is idle. `lastUsed(threadId)` is when a thread last moved; `inUse(folder)` whether a run works there;
58
+ * `idle()` whether the person is away with nothing running; `hasRoom()` whether the disk has room again. `history()`
59
+ * forgets what Harness keeps for a while past its time (tripwires.mjs: what sessions read, versions kept 30 days) and
60
+ * says how many went.
61
+ */
62
+ export function createCleanup({
63
+ store,
64
+ events,
65
+ worktrees,
66
+ lastUsed = () => 0,
67
+ inUse = () => false,
68
+ idle = () => false,
69
+ hasRoom = async () => true,
70
+ notify = () => {},
71
+ history = () => 0,
72
+ now = () => Date.now(),
73
+ log = () => {},
74
+ tmp = os.tmpdir(),
75
+ }) {
76
+ let trimming = null;
77
+ let timer = null;
78
+
79
+ /** Each thread's copy Harness made: whether it is the thread's own now, or kept with what was never committed. */
80
+ const copies = () =>
81
+ listed(worktrees.root)
82
+ .map((name) => {
83
+ const folder = path.join(worktrees.root, name);
84
+ const threadId = COPY_NAME.exec(name)?.[1];
85
+ const stat = fs.statSync(folder, { throwIfNoEntry: false });
86
+ if (!threadId || !stat?.isDirectory()) return null;
87
+ const record = worktrees.record(threadId);
88
+ const current = record?.path === folder;
89
+ return {
90
+ folder,
91
+ threadId,
92
+ record: current ? record : null,
93
+ // A copy its thread moved on from, or a merged one left with files never committed.
94
+ kept: !current || record.state === "merged",
95
+ // The thread's own last move; the folder's time only for a copy whose thread is gone, since
96
+ // clearing a copy touches its folder.
97
+ lastUsed: lastUsed(threadId) || stat.mtimeMs,
98
+ };
99
+ })
100
+ .filter(Boolean);
101
+
102
+ const scratch = async (done) => {
103
+ for (const name of listed(tmp)) {
104
+ if (!SCRATCH.some((prefix) => name.startsWith(prefix))) continue;
105
+ const full = path.join(tmp, name);
106
+ const stat = fs.lstatSync(full, { throwIfNoEntry: false });
107
+ if (!stat?.isDirectory() || now() - stat.mtimeMs < DAY_MS) continue;
108
+ if (process.getuid && stat.uid !== process.getuid()) continue;
109
+ done.bytes += await sizeOf(full);
110
+ fs.rmSync(full, { recursive: true, force: true });
111
+ done.scratch++;
112
+ }
113
+ };
114
+
115
+ const rebuildable = async (why, done) => {
116
+ const ordered = copies()
117
+ .filter((copy) => !inUse(copy.folder))
118
+ .sort((a, b) => a.lastUsed - b.lastUsed);
119
+ for (const copy of ordered) {
120
+ if (why === "daily" && now() - copy.lastUsed < UNUSED_DAYS * DAY_MS)
121
+ continue;
122
+ if (why === "tight" && (await hasRoom())) break;
123
+ for (const found of rebuildableUnder(copy.folder)) {
124
+ // A run that started meanwhile keeps everything it may use.
125
+ if (inUse(copy.folder)) break;
126
+ done.bytes += await sizeOf(found.path);
127
+ fs.rmSync(found.path, { recursive: true, force: true });
128
+ done.rebuilt++;
129
+ }
130
+ }
131
+ };
132
+
133
+ const mergedElsewhere = (done) => {
134
+ for (const copy of copies()) {
135
+ if (copy.record?.state !== "open" || inUse(copy.folder)) continue;
136
+ try {
137
+ if (worktrees.mergedElsewhere(copy.threadId)) done.merged++;
138
+ } catch (error) {
139
+ log(`cleanup: ${copy.folder}: ${error.message}`);
140
+ }
141
+ }
142
+ };
143
+
144
+ // The person's: a copy kept with files never committed, once large and old, is named to them once for each
145
+ // gigabyte it holds.
146
+ const tell = async (done) => {
147
+ for (const copy of copies()) {
148
+ if (!copy.kept || now() - copy.lastUsed < OLD_DAYS * DAY_MS) continue;
149
+ const bytes = await sizeOf(copy.folder);
150
+ if (bytes < LARGE_BYTES) continue;
151
+ const key = `cleanup-told:${copy.folder}`;
152
+ const told = String(Math.floor(bytes / LARGE_BYTES));
153
+ if (store.meta(key) === told) continue;
154
+ store.meta(key, told);
155
+ const since = new Date(copy.lastUsed).toISOString().slice(0, 10);
156
+ // A notice is cut at 240 characters: what it is and what Harness does come before the folder.
157
+ notify(
158
+ `${gb(bytes)} of a thread's files were never committed and are untouched since ${since}. Harness leaves them for you to keep, move or delete: ${copy.folder}`
159
+ );
160
+ events.append({
161
+ source: "cleanup",
162
+ kind: "told",
163
+ subject: copy.folder,
164
+ text: `named ${gb(bytes)} of a thread's files never committed to the person, untouched since ${since}`,
165
+ data: { folder: copy.folder, bytes },
166
+ });
167
+ done.told++;
168
+ }
169
+ };
170
+
171
+ const trim = (why) => {
172
+ trimming ??= (async () => {
173
+ const done = {
174
+ why,
175
+ scratch: 0,
176
+ rebuilt: 0,
177
+ merged: 0,
178
+ history: 0,
179
+ told: 0,
180
+ bytes: 0,
181
+ };
182
+ try {
183
+ await scratch(done);
184
+ mergedElsewhere(done);
185
+ await rebuildable(why, done);
186
+ done.history = await history();
187
+ await tell(done);
188
+ } catch (error) {
189
+ log(`cleanup: ${error.message}`);
190
+ }
191
+ store.meta("cleanup-last", new Date(now()).toISOString());
192
+ if (done.scratch || done.rebuilt || done.merged)
193
+ events.append({
194
+ source: "cleanup",
195
+ kind: "trimmed",
196
+ text: `freed ${gb(done.bytes)}${why === "tight" ? " as the disk turned tight" : ""}: ${[
197
+ done.rebuilt &&
198
+ `${done.rebuilt} rebuildable folder${done.rebuilt === 1 ? "" : "s"} from threads' copies`,
199
+ done.scratch &&
200
+ `${done.scratch} old temporary folder${done.scratch === 1 ? "" : "s"}`,
201
+ done.merged &&
202
+ `${done.merged} copy of a branch merged outside Harness`,
203
+ ]
204
+ .filter(Boolean)
205
+ .join(", ")}`,
206
+ data: done,
207
+ });
208
+ return done;
209
+ })().finally(() => {
210
+ trimming = null;
211
+ });
212
+ return trimming;
213
+ };
214
+
215
+ const maybeDaily = () => {
216
+ const last = Date.parse(store.meta("cleanup-last") || "") || 0;
217
+ if (now() - last < DAY_MS || !idle()) return null;
218
+ return trim("daily");
219
+ };
220
+
221
+ return {
222
+ trim,
223
+ maybeDaily,
224
+ start() {
225
+ timer = setInterval(() => void maybeDaily(), LOOK_EVERY_MS);
226
+ timer.unref?.();
227
+ // A first look soon after the start, once the person has been read.
228
+ setTimeout(() => void maybeDaily(), 5_000).unref?.();
229
+ },
230
+ stop() {
231
+ clearInterval(timer);
232
+ },
233
+ };
234
+ }
package/lib/cloudBoot.mjs CHANGED
@@ -1,8 +1,10 @@
1
+ import { readConfig, writeConfig } from "./secretStore.mjs";
1
2
  import fs from "node:fs";
2
3
  import path from "node:path";
3
4
  import { randomBytes } from "node:crypto";
4
5
  import { readJson, writeJsonAtomic } from "./files.mjs";
5
6
  import { restoreHome } from "./cloudSave.mjs";
7
+ import { loadDeviceIdentity } from "./deviceIdentity.mjs";
6
8
 
7
9
  // A cloud computer's enrollment (docs/cloud-swarm.md §5.3), run by the harness-cloud service as the harness user
8
10
  // before the daemon starts (src/server/cloud/bootScript.ts). Root's part is done once, at boot: it read the box's own
@@ -31,7 +33,7 @@ export async function cloudEnroll({
31
33
  fs.mkdirSync(home, { recursive: true, mode: 0o700 });
32
34
  // The link offer Harness used to keep for every window (cloud.json); offers are minted fresh on the box now.
33
35
  removeFile(path.join(home, "cloud.json"));
34
- const config = readJson(path.join(home, "config.json"), {});
36
+ const config = readConfig(path.join(home, "config.json"), {});
35
37
  if (config.origin && config.token) {
36
38
  // The token works only until the box first connects, but it has no reason to stay on the disk once used.
37
39
  removeFile(bootFile);
@@ -44,12 +46,27 @@ export async function cloudEnroll({
44
46
  boot.instanceId && !metadataUrl
45
47
  ? String(boot.instanceId)
46
48
  : await readInstanceId(fetchImpl, metadataUrl || boot.instanceIdUrl);
49
+ // This computer as one of the person's devices: its keys are made here and never leave the box; Harness gets their
50
+ // public halves, which one of the person's devices signs onto their list. A box rebuilt from its saved copy gets its
51
+ // first keys back with the copy, and Harness keeps those.
52
+ const identity = await loadDeviceIdentity({
53
+ phoneIdentity: path.join(home, "phone-identity.json"),
54
+ deviceIdentity: path.join(home, "device-identity.json"),
55
+ });
47
56
  const response = await fetchImpl(
48
57
  `${String(boot.origin).replace(/\/$/, "")}/api/cloud/enroll`,
49
58
  {
50
59
  method: "POST",
51
60
  headers: { "content-type": "application/json" },
52
- body: JSON.stringify({ bootstrap: boot.bootstrap, instanceId }),
61
+ body: JSON.stringify({
62
+ bootstrap: boot.bootstrap,
63
+ instanceId,
64
+ device: {
65
+ deviceId: identity.deviceId,
66
+ signingKey: identity.signing.publicKey,
67
+ sealingKey: identity.sealing.publicKey,
68
+ },
69
+ }),
53
70
  }
54
71
  );
55
72
  const answer = await response.json().catch(() => ({}));
@@ -90,8 +107,8 @@ export async function cloudEnroll({
90
107
  typeof boot.ssh?.keys === "string" && typeof boot.ssh?.gate === "string"
91
108
  ? { keys: boot.ssh.keys, gate: boot.ssh.gate }
92
109
  : null;
93
- writeJsonAtomic(path.join(home, "config.json"), {
94
- ...(answer.restore ? readJson(path.join(home, "config.json"), {}) : {}),
110
+ writeConfig(path.join(home, "config.json"), {
111
+ ...(answer.restore ? readConfig(path.join(home, "config.json"), {}) : {}),
95
112
  origin: answer.origin,
96
113
  token: answer.token,
97
114
  cloud: true,
package/lib/cloudSave.mjs CHANGED
@@ -32,6 +32,18 @@ export const LEFT_OUT = [
32
32
  "./tmp",
33
33
  ];
34
34
 
35
+ // The board as the daemon is writing it: never packed as it is on disk, where a write half done (or its journal from
36
+ // another moment) would be restored as a broken board. A consistent copy of it goes instead, under its own name, and
37
+ // is put back in its place when the copy is restored.
38
+ const LIVE_BOARD = [
39
+ "./.harness/harness.db",
40
+ "./.harness/harness.db-journal",
41
+ "./.harness/harness.db-wal",
42
+ "./.harness/harness.db-shm",
43
+ "./.harness/harness.db.lock",
44
+ ];
45
+ const BOARD_COPY = path.join(".harness", "harness-copy.db");
46
+
35
47
  const NONCE = 12;
36
48
  const TAG = 16;
37
49
 
@@ -48,8 +60,23 @@ const run = (command, args, options) =>
48
60
  );
49
61
  });
50
62
 
51
- /** The home folder packed and sealed into a temporary file; its path and size. */
52
- export async function packHome({ home = os.homedir(), key }) {
63
+ /**
64
+ * The home folder packed and sealed into a temporary file; its path and size. `snapshot(file)` writes a consistent copy
65
+ * of the board's database to `file` (the store's VACUUM INTO), packed in place of the live one.
66
+ */
67
+ export async function packHome({ home = os.homedir(), key, snapshot }) {
68
+ const boardCopy = path.join(home, BOARD_COPY);
69
+ fs.rmSync(boardCopy, { force: true });
70
+ if (fs.existsSync(path.join(home, ".harness", "harness.db")))
71
+ snapshot(boardCopy);
72
+ try {
73
+ return await packAndSeal({ home, key });
74
+ } finally {
75
+ fs.rmSync(boardCopy, { force: true });
76
+ }
77
+ }
78
+
79
+ async function packAndSeal({ home, key }) {
53
80
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "harness-copy-"));
54
81
  const file = path.join(dir, "copy.enc");
55
82
  const nonce = randomBytes(NONCE);
@@ -63,7 +90,10 @@ export async function packHome({ home = os.homedir(), key }) {
63
90
  [
64
91
  "-czf",
65
92
  "-",
66
- ...LEFT_OUT.flatMap((pattern) => ["--exclude", pattern]),
93
+ ...[...LEFT_OUT, ...LIVE_BOARD].flatMap((pattern) => [
94
+ "--exclude",
95
+ pattern,
96
+ ]),
67
97
  "-C",
68
98
  home,
69
99
  ".",
@@ -74,7 +104,8 @@ export async function packHome({ home = os.homedir(), key }) {
74
104
  tar.stderr.on("data", (chunk) => (err += chunk));
75
105
  const finished = new Promise((resolve, reject) => {
76
106
  tar.on("error", reject);
77
- // 1: some file changed while it was read (GNU tar); the copy is still whole for everything else.
107
+ // 1: some file changed while it was read (GNU tar): another of the person's files, never the board, which goes as
108
+ // its own consistent copy; the copy is still whole for everything else.
78
109
  tar.on("close", (code) =>
79
110
  code === 0 || code === 1
80
111
  ? resolve()
@@ -96,6 +127,8 @@ export async function packHome({ home = os.homedir(), key }) {
96
127
  export function createCloudSave({
97
128
  origin,
98
129
  token,
130
+ // A consistent copy of the board's database written to a file (packHome).
131
+ snapshot,
99
132
  home = os.homedir(),
100
133
  fetchImpl = fetch,
101
134
  log = () => {},
@@ -121,7 +154,7 @@ export function createCloudSave({
121
154
  const saveOnce = async () => {
122
155
  const started = Date.now();
123
156
  const { url, object, key } = await call({ phase: "start" });
124
- const packed = await packHome({ home, key });
157
+ const packed = await packHome({ home, key, snapshot });
125
158
  try {
126
159
  const uploaded = await fetchImpl(url, {
127
160
  method: "PUT",
@@ -191,6 +224,14 @@ export async function restoreHome({
191
224
  await run("tar", ["-xzf", archive, "-C", home, "--no-same-owner"], {
192
225
  stdio: ["ignore", "ignore", "pipe"],
193
226
  });
227
+ // The board's consistent copy back in its place, with no journal from another moment beside it.
228
+ const boardCopy = path.join(home, BOARD_COPY);
229
+ if (fs.existsSync(boardCopy)) {
230
+ fs.rmSync(path.join(home, ".harness", "harness.db-journal"), {
231
+ force: true,
232
+ });
233
+ fs.renameSync(boardCopy, path.join(home, ".harness", "harness.db"));
234
+ }
194
235
  } finally {
195
236
  fs.rmSync(dir, { recursive: true, force: true });
196
237
  }
@@ -14,7 +14,7 @@ const QUESTIONS = [
14
14
  "3. What have you concluded without checking it?",
15
15
  ];
16
16
 
17
- export function createCompaction({ store, goals }) {
17
+ export function createCompaction({ store, goals, timelineOf = () => [] }) {
18
18
  // The work a session holds: the run working in it, else the thread that resumes it, else what its agent claimed.
19
19
  const workOf = (agent, sessionId) => {
20
20
  const run = sessionId ? store.runHoldingSession(agent, sessionId) : null;
@@ -82,6 +82,13 @@ export function createCompaction({ store, goals }) {
82
82
  `- The person's words: ${oneLine(thread.brief, FIELD_LIMIT)}`
83
83
  );
84
84
  lines.push(...goalLines(thread.id));
85
+ // When things happened, which a summary drops first (timeSense.mjs).
86
+ const story = timelineOf(thread.id);
87
+ if (story.length)
88
+ lines.push(
89
+ "- The thread so far (each time on its computer's clock):",
90
+ ...story.map((line) => ` ${line}`)
91
+ );
85
92
  }
86
93
  return [...lines, "", ...QUESTIONS].join("\n");
87
94
  };