tryharness 0.7.53 → 0.7.57

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/README.md +23 -19
  2. package/assets/dsh-plugin.mjs +72 -0
  3. package/assets/opencode-plugin.js +4 -3
  4. package/cli.mjs +484 -90
  5. package/lib/activity.mjs +10 -10
  6. package/lib/adapters.mjs +173 -222
  7. package/lib/agentBinaries.mjs +71 -0
  8. package/lib/agentRuntimeConfig.mjs +1 -1
  9. package/lib/agentRuntimes.mjs +34 -18
  10. package/lib/agentSignIn.mjs +456 -0
  11. package/lib/answers.mjs +5 -5
  12. package/lib/archetypes.d.ts +64 -0
  13. package/lib/archetypes.js +989 -0
  14. package/lib/asks.mjs +4 -4
  15. package/lib/audit.mjs +666 -0
  16. package/lib/backupSetup.mjs +143 -0
  17. package/lib/boardAccess.mjs +24 -11
  18. package/lib/boardBackup.mjs +283 -0
  19. package/lib/boardFileCommands.mjs +6 -6
  20. package/lib/boardFiles.mjs +13 -12
  21. package/lib/boardMirror.mjs +124 -25
  22. package/lib/boardProtocol.json +88 -2
  23. package/lib/boardWaits.mjs +101 -0
  24. package/lib/book.mjs +659 -0
  25. package/lib/bridge.mjs +6 -6
  26. package/lib/cards.mjs +138 -28
  27. package/lib/cloudAgents.mjs +216 -0
  28. package/lib/cloudAi.mjs +1312 -0
  29. package/lib/cloudAssistants.mjs +242 -0
  30. package/lib/cloudBoot.mjs +119 -0
  31. package/lib/cloudSave.mjs +197 -0
  32. package/lib/cloudSsh.mjs +178 -0
  33. package/lib/commands.mjs +7 -2
  34. package/lib/context.mjs +19 -5
  35. package/lib/contractGate.mjs +399 -0
  36. package/lib/control-client.mjs +9 -1
  37. package/lib/controlSocket.mjs +86 -0
  38. package/lib/coordinator.mjs +252 -144
  39. package/lib/daemon.mjs +1351 -2767
  40. package/lib/desk.mjs +240 -0
  41. package/lib/desktop.mjs +178 -74
  42. package/lib/directLink.mjs +16 -5
  43. package/lib/environment.mjs +2 -1
  44. package/lib/environments.mjs +247 -9
  45. package/lib/firewall.mjs +9 -9
  46. package/lib/folders.mjs +20 -16
  47. package/lib/frames.mjs +2 -3
  48. package/lib/gitHost.mjs +174 -0
  49. package/lib/goalCommands.mjs +25 -7
  50. package/lib/goals.mjs +7 -7
  51. package/lib/guestRequests.mjs +94 -0
  52. package/lib/handoffs.mjs +8 -7
  53. package/lib/handover.mjs +4 -4
  54. package/lib/harnessModel.mjs +6 -6
  55. package/lib/harnessOffers.mjs +266 -0
  56. package/lib/hookDialects.mjs +298 -0
  57. package/lib/inference.mjs +26 -8
  58. package/lib/jobAnswers.mjs +190 -0
  59. package/lib/landed.mjs +887 -0
  60. package/lib/lane.mjs +42 -11
  61. package/lib/lessons.mjs +16 -16
  62. package/lib/library.mjs +3 -3
  63. package/lib/lineup.mjs +3 -0
  64. package/lib/linkRelay.mjs +574 -0
  65. package/lib/live.mjs +284 -13
  66. package/lib/localAgentAsk.mjs +74 -0
  67. package/lib/localStack.mjs +536 -0
  68. package/lib/localmodels.mjs +6 -4
  69. package/lib/loopBackstop.mjs +4 -4
  70. package/lib/machineIdentity.mjs +9 -1
  71. package/lib/machineRoutes.mjs +181 -2
  72. package/lib/machineRuns.mjs +76 -1
  73. package/lib/machineSeal.mjs +39 -12
  74. package/lib/mcp-stdio.mjs +163 -27
  75. package/lib/mcpRelay.mjs +1 -1
  76. package/lib/members.json +29 -2
  77. package/lib/members.mjs +1 -1
  78. package/lib/models.mjs +85 -31
  79. package/lib/museSession.mjs +21 -13
  80. package/lib/nativeHost.mjs +36 -8
  81. package/lib/neovim.mjs +126 -0
  82. package/lib/network.mjs +50 -5
  83. package/lib/notify.mjs +7 -5
  84. package/lib/nudge.mjs +50 -19
  85. package/lib/ocr.mjs +9 -9
  86. package/lib/onboard.mjs +26 -20
  87. package/lib/oneLine.mjs +7 -7
  88. package/lib/ownMachines.mjs +1223 -140
  89. package/lib/pairing.mjs +60 -16
  90. package/lib/paths.mjs +19 -1
  91. package/lib/peerProcess.mjs +124 -0
  92. package/lib/personModel.mjs +89 -0
  93. package/lib/personNotices.mjs +151 -0
  94. package/lib/phones.mjs +538 -31
  95. package/lib/placeCommands.mjs +4 -4
  96. package/lib/places.mjs +400 -12
  97. package/lib/profile.mjs +67 -14
  98. package/lib/projectGit.mjs +158 -14
  99. package/lib/projectPieces.mjs +902 -0
  100. package/lib/projectSettings.mjs +7 -4
  101. package/lib/projects.mjs +384 -62
  102. package/lib/provenance.mjs +5 -1
  103. package/lib/proxyLinks.mjs +267 -0
  104. package/lib/reactions.mjs +7 -5
  105. package/lib/readiness.mjs +145 -23
  106. package/lib/record.mjs +128 -0
  107. package/lib/redact.mjs +4 -4
  108. package/lib/relayLimits.mjs +62 -0
  109. package/lib/release.mjs +170 -0
  110. package/lib/releaseKeys.json +6 -0
  111. package/lib/repoLinks.mjs +4 -4
  112. package/lib/roomChannels.mjs +24 -0
  113. package/lib/roomPage.mjs +1 -1
  114. package/lib/runAuthority.mjs +101 -0
  115. package/lib/runBrief.mjs +19 -8
  116. package/lib/runWorker.mjs +31 -7
  117. package/lib/runner.mjs +223 -29
  118. package/lib/runtime-install.mjs +143 -9
  119. package/lib/schedules.mjs +2 -2
  120. package/lib/screens.mjs +1 -1
  121. package/lib/sealed.mjs +11 -2
  122. package/lib/sealedThreads.mjs +671 -0
  123. package/lib/secretPatterns.json +1 -1
  124. package/lib/secrets.mjs +4 -4
  125. package/lib/seen.mjs +6 -6
  126. package/lib/serverWords.mjs +175 -0
  127. package/lib/service.mjs +22 -12
  128. package/lib/sessionControl.mjs +378 -90
  129. package/lib/specialties.json +74 -0
  130. package/lib/specialties.mjs +477 -0
  131. package/lib/specialtyKeys.d.ts +3 -0
  132. package/lib/specialtyKeys.js +57 -0
  133. package/lib/specialtyNotice.mjs +147 -0
  134. package/lib/specialtySnapshot.mjs +138 -0
  135. package/lib/speech.mjs +5 -5
  136. package/lib/store.mjs +420 -36
  137. package/lib/stuck.mjs +1 -1
  138. package/lib/swarm.mjs +304 -37
  139. package/lib/swarmDevices.mjs +319 -0
  140. package/lib/swarmMemory.mjs +27 -25
  141. package/lib/t3.mjs +24 -15
  142. package/lib/taskCommands.mjs +80 -20
  143. package/lib/taskRooms.mjs +125 -0
  144. package/lib/taskRuns.mjs +45 -3
  145. package/lib/team.json +255 -148
  146. package/lib/team.mjs +554 -190
  147. package/lib/threadActions.mjs +315 -0
  148. package/lib/threadAuthority.mjs +101 -0
  149. package/lib/threadBriefs.mjs +178 -0
  150. package/lib/threadLine.mjs +116 -0
  151. package/lib/threadRuns.mjs +209 -50
  152. package/lib/threadView.mjs +20 -7
  153. package/lib/usageTotals.mjs +2 -3
  154. package/lib/vault.mjs +101 -10
  155. package/lib/vaultCloud.mjs +2 -2
  156. package/lib/vaultCommands.mjs +15 -6
  157. package/lib/vaultTools.js +2 -2
  158. package/lib/verdicts.mjs +328 -0
  159. package/lib/wakes.mjs +1 -1
  160. package/lib/watches.mjs +2 -2
  161. package/lib/windowAsks.mjs +13 -5
  162. package/lib/worktrees.mjs +157 -25
  163. package/npm-shrinkwrap.json +1093 -0
  164. package/package.json +12 -11
  165. package/release.json +220 -0
  166. package/lib/modelTiers.json +0 -1183
  167. package/lib/modelTiers.mjs +0 -133
@@ -0,0 +1,242 @@
1
+ import fs from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { spawn } from "node:child_process";
5
+
6
+ // Telegram for a chat assistant on a cloud computer, through Harness's one shared bot (docs/cloud-swarm.md §5.12).
7
+ // The computer asks Harness, with its own connection token, for the assistant's relay settings (GET /api/cloud/relay,
8
+ // src/server/assistantRelay/relay.ts credentials()), then points the assistant at Harness's server as its relay:
9
+ //
10
+ // - Hermes speaks its own documented relay contract, so it needs only settings: GATEWAY_RELAY_* in ~/.hermes/.env,
11
+ // with no Telegram token of its own. Hermes delivers through a live native adapter before the relay
12
+ // (gateway/delivery.py resolve_delivery_transport), so with its own bot on, replies to messages that came through
13
+ // Harness's bot would leave from that other bot. Then its gateway service is installed if missing and restarted to
14
+ // read the settings.
15
+ // - OpenClaw gets Harness's channel plugin (packages/openclaw-harness), installed with OpenClaw's own installer, and
16
+ // channels.harness set to the same three settings. OpenClaw applies both to its running gateway; no restart.
17
+ //
18
+ // Called by the daemon (cloudAgents.mjs afterAssistant) after the assistant is installed, only when the person chose
19
+ // Telegram for this computer. Nothing secret is logged.
20
+
21
+ // The plugin OpenClaw installs: Harness publishes it at the version cloud computers pin.
22
+ export const OPENCLAW_PLUGIN = "npm:@harness/openclaw-harness@0.1.0";
23
+
24
+ // The native bot token Hermes would otherwise answer Telegram with.
25
+ const NATIVE_TELEGRAM_KEYS = ["TELEGRAM_BOT_TOKEN"];
26
+
27
+ /** Runs one command to completion; resolves its exit code and output, never rejects. */
28
+ function runCommand(command, args, { env }) {
29
+ return new Promise((resolve) => {
30
+ let output = "";
31
+ const child = spawn(command, args, {
32
+ env,
33
+ stdio: ["ignore", "pipe", "pipe"],
34
+ });
35
+ child.stdout.on("data", (chunk) => (output += chunk));
36
+ child.stderr.on("data", (chunk) => (output += chunk));
37
+ child.on("error", (error) => resolve({ code: -1, output: error.message }));
38
+ child.on("close", (code) => resolve({ code, output }));
39
+ });
40
+ }
41
+
42
+ /** What Harness hands this computer's assistant to dial with: {url, gatewayId, secret}. */
43
+ async function relaySettings({ agent, config, fetchImpl }) {
44
+ const origin = String(config.origin).replace(/\/$/, "");
45
+ const response = await fetchImpl(
46
+ `${origin}/api/cloud/relay?agent=${encodeURIComponent(agent)}`,
47
+ { headers: { authorization: `Bearer ${config.token}` } }
48
+ );
49
+ if (!response.ok)
50
+ throw new Error(
51
+ `Harness answered ${response.status} for ${agent}'s relay settings`
52
+ );
53
+ const settings = await response.json();
54
+ if (!settings.url || !settings.gatewayId || !settings.secret)
55
+ throw new Error(`Harness sent incomplete relay settings for ${agent}`);
56
+ return settings;
57
+ }
58
+
59
+ /** The lines of a dotenv file with these keys set and those dropped; every other line kept as it was. */
60
+ export function withEnvSettings(existing, set, drop = []) {
61
+ const replaced = new Set([...Object.keys(set), ...drop]);
62
+ const kept = existing
63
+ .split("\n")
64
+ .filter((line) => {
65
+ const key = line.match(
66
+ /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)=/
67
+ )?.[1];
68
+ return !key || !replaced.has(key);
69
+ })
70
+ .join("\n")
71
+ .replace(/\n+$/, "");
72
+ const added = Object.entries(set).map(([key, value]) => `${key}=${value}`);
73
+ return `${[kept, ...added].filter(Boolean).join("\n")}\n`;
74
+ }
75
+
76
+ async function configureHermes({ settings, home, env, run, log }) {
77
+ const hermesHome = path.join(home, ".hermes");
78
+ const envFile = path.join(hermesHome, ".env");
79
+ fs.mkdirSync(hermesHome, { recursive: true });
80
+ const existing = fs.existsSync(envFile)
81
+ ? fs.readFileSync(envFile, "utf8")
82
+ : "";
83
+ const relay = {
84
+ GATEWAY_RELAY_URL: settings.url,
85
+ GATEWAY_RELAY_ID: settings.gatewayId,
86
+ GATEWAY_RELAY_SECRET: settings.secret,
87
+ // Harness fronts Telegram only; the first platform is the one Hermes says hello for.
88
+ GATEWAY_RELAY_PLATFORMS: "telegram",
89
+ };
90
+ fs.writeFileSync(
91
+ envFile,
92
+ withEnvSettings(existing, relay, NATIVE_TELEGRAM_KEYS),
93
+ { mode: 0o600 }
94
+ );
95
+ fs.chmodSync(envFile, 0o600);
96
+ log(`hermes: relay settings written to ${envFile}`);
97
+ // Messages Harness held while the computer slept arrive back to back; by default Hermes lets each new message cut
98
+ // into the turn before it, so a backlog could be answered newest first. Queued, each is its own turn, in the order
99
+ // sent (docs: messaging, busy-input mode); /stop still stops a turn.
100
+ const queued = await run(
101
+ "hermes",
102
+ ["config", "set", "display.busy_input_mode", "queue"],
103
+ { env }
104
+ );
105
+ if (queued.code !== 0)
106
+ throw new Error(`hermes config set failed (exit ${queued.code})`);
107
+ const install = await run(
108
+ "hermes",
109
+ ["gateway", "install", "--if-missing", "--start-now", "--start-on-login"],
110
+ { env }
111
+ );
112
+ if (install.code !== 0)
113
+ throw new Error(`hermes gateway install failed (exit ${install.code})`);
114
+ // An already running gateway reads .env only when it starts.
115
+ const restart = await run("hermes", ["gateway", "restart"], { env });
116
+ if (restart.code !== 0)
117
+ throw new Error(`hermes gateway restart failed (exit ${restart.code})`);
118
+ log("hermes: gateway restarted on Harness's relay");
119
+ }
120
+
121
+ async function configureOpenClaw({ settings, env, run, log, pluginSpec }) {
122
+ // Both flags are needed without a person at the keyboard: OpenClaw asks before installing anything not from its
123
+ // own catalog, and before granting the plugin its capabilities.
124
+ const install = await run(
125
+ "openclaw",
126
+ ["plugins", "install", pluginSpec, "--force", "--accept-capabilities"],
127
+ { env }
128
+ );
129
+ if (install.code !== 0)
130
+ throw new Error(`openclaw plugins install failed (exit ${install.code})`);
131
+ // After the install: OpenClaw refuses channels.harness until the plugin that owns it is there.
132
+ const channel = {
133
+ enabled: true,
134
+ relayUrl: settings.url,
135
+ gatewayId: settings.gatewayId,
136
+ secret: settings.secret,
137
+ };
138
+ // The secret goes in a file only this user can read, never on a command line, where any process on the computer
139
+ // (and the provider's process list) could read it.
140
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "harness-openclaw-"));
141
+ const batch = path.join(dir, "channel.json");
142
+ fs.writeFileSync(
143
+ batch,
144
+ JSON.stringify([{ path: "channels.harness", value: channel }]),
145
+ { mode: 0o600 }
146
+ );
147
+ let configured;
148
+ try {
149
+ configured = await run(
150
+ "openclaw",
151
+ ["config", "set", "--batch-file", batch],
152
+ { env }
153
+ );
154
+ } finally {
155
+ fs.rmSync(dir, { recursive: true, force: true });
156
+ }
157
+ if (configured.code !== 0)
158
+ throw new Error(`openclaw config set failed (exit ${configured.code})`);
159
+ log("openclaw: Harness channel installed and pointed at Harness's relay");
160
+ }
161
+
162
+ /**
163
+ * Point this computer's `agent` ("openclaw" or "hermes") at Harness's relay for Telegram. `config` is the daemon's
164
+ * config (origin, and token: the computer's connection token). Throws on any failure; the daemon logs it.
165
+ */
166
+ export async function configureAssistantRelay({
167
+ agent,
168
+ config,
169
+ log = () => {},
170
+ home = os.homedir(),
171
+ env = process.env,
172
+ fetchImpl = fetch,
173
+ run = runCommand,
174
+ pluginSpec = OPENCLAW_PLUGIN,
175
+ }) {
176
+ if (agent !== "hermes" && agent !== "openclaw")
177
+ throw new Error(`no Telegram relay for ${agent}`);
178
+ const settings = await relaySettings({ agent, config, fetchImpl });
179
+ const childEnv = { ...env, HOME: home };
180
+ if (agent === "hermes")
181
+ await configureHermes({ settings, home, env: childEnv, run, log });
182
+ else
183
+ await configureOpenClaw({ settings, env: childEnv, run, log, pluginSpec });
184
+ }
185
+
186
+ const RELAY_KEYS = [
187
+ "GATEWAY_RELAY_URL",
188
+ "GATEWAY_RELAY_ID",
189
+ "GATEWAY_RELAY_SECRET",
190
+ "GATEWAY_RELAY_PLATFORMS",
191
+ ];
192
+
193
+ /**
194
+ * Take this computer's `agent` off Harness's relay: Telegram moved to another of the person's computers, or they
195
+ * turned it off. Harness already stops sending it messages; this stops the assistant dialing a relay that refuses
196
+ * it. Nothing to undo leaves everything as it is.
197
+ */
198
+ export async function removeAssistantRelay({
199
+ agent,
200
+ log = () => {},
201
+ home = os.homedir(),
202
+ env = process.env,
203
+ run = runCommand,
204
+ }) {
205
+ const childEnv = { ...env, HOME: home };
206
+ if (agent === "hermes") {
207
+ const envFile = path.join(home, ".hermes", ".env");
208
+ const existing = fs.existsSync(envFile)
209
+ ? fs.readFileSync(envFile, "utf8")
210
+ : "";
211
+ if (!/^\s*(?:export\s+)?GATEWAY_RELAY_URL=/m.test(existing)) return;
212
+ fs.writeFileSync(envFile, withEnvSettings(existing, {}, RELAY_KEYS), {
213
+ mode: 0o600,
214
+ });
215
+ // A running gateway keeps its relay until it starts again.
216
+ const restart = await run("hermes", ["gateway", "restart"], {
217
+ env: childEnv,
218
+ });
219
+ if (restart.code !== 0)
220
+ throw new Error(`hermes gateway restart failed (exit ${restart.code})`);
221
+ log("hermes: off Harness's relay");
222
+ return;
223
+ }
224
+ if (agent === "openclaw") {
225
+ const configured = await run(
226
+ "openclaw",
227
+ ["config", "get", "channels.harness"],
228
+ { env: childEnv }
229
+ );
230
+ if (configured.code !== 0) return;
231
+ const unset = await run(
232
+ "openclaw",
233
+ ["config", "unset", "channels.harness"],
234
+ {
235
+ env: childEnv,
236
+ }
237
+ );
238
+ if (unset.code !== 0)
239
+ throw new Error(`openclaw config unset failed (exit ${unset.code})`);
240
+ log("openclaw: Harness channel removed");
241
+ }
242
+ }
@@ -0,0 +1,119 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { randomBytes } from "node:crypto";
4
+ import { readJson, writeJsonAtomic } from "./files.mjs";
5
+ import { restoreHome } from "./cloudSave.mjs";
6
+
7
+ // A cloud computer's enrollment (docs/cloud-swarm.md §5.3), run by the harness-cloud service as the harness user
8
+ // before the daemon starts (src/server/cloud/bootScript.ts). Root's part is done once, at boot: it read the box's own
9
+ // instance id and left it, with the origin, this computer's id and a single-use bootstrap token, in
10
+ // ~/.harness/boot.json, the harness user's own file. The box proves it is the server Harness made by sending that
11
+ // token with the instance id, and gets back its connection token, its role in the swarm and, as a worker, its join
12
+ // code. They are written into ~/.harness exactly as setup writes them, so from here the daemon is the same as on any
13
+ // computer.
14
+ //
15
+ // Nothing here runs as root. Agents on the box run as harness and can change anything in its home (~/.harness, a
16
+ // folder on its PATH, a symlink where a file was), so root never reads, writes or follows anything there after boot.
17
+
18
+ /** Enroll this cloud computer once; later starts find it enrolled. home is the harness user's ~/.harness. */
19
+ export async function cloudEnroll({
20
+ home = "/home/harness/.harness",
21
+ bootFile = path.join(home, "boot.json"),
22
+ metadataUrl,
23
+ fetchImpl = fetch,
24
+ restore = restoreHome,
25
+ log = (line) => console.log(`harness cloud: ${line}`),
26
+ } = {}) {
27
+ if (process.getuid?.() === 0)
28
+ throw new Error(
29
+ "cloud-enroll runs as the harness user: root never acts in that user's home"
30
+ );
31
+ fs.mkdirSync(home, { recursive: true, mode: 0o700 });
32
+ // The link offer Harness used to keep for every window (cloud.json); offers are minted fresh on the box now.
33
+ removeFile(path.join(home, "cloud.json"));
34
+ const config = readJson(path.join(home, "config.json"), {});
35
+ if (config.origin && config.token) {
36
+ // The token works only until the box first connects, but it has no reason to stay on the disk once used.
37
+ removeFile(bootFile);
38
+ return { ok: true, already: true };
39
+ }
40
+ const boot = readJson(bootFile, null);
41
+ if (!boot?.origin || !boot?.bootstrap)
42
+ throw new Error(`${bootFile} has no enrollment in it`);
43
+ const instanceId =
44
+ boot.instanceId && !metadataUrl
45
+ ? String(boot.instanceId)
46
+ : await readInstanceId(fetchImpl, metadataUrl || boot.instanceIdUrl);
47
+ const response = await fetchImpl(
48
+ `${String(boot.origin).replace(/\/$/, "")}/api/cloud/enroll`,
49
+ {
50
+ method: "POST",
51
+ headers: { "content-type": "application/json" },
52
+ body: JSON.stringify({ bootstrap: boot.bootstrap, instanceId }),
53
+ }
54
+ );
55
+ const answer = await response.json().catch(() => ({}));
56
+ if (!response.ok)
57
+ throw new Error(
58
+ `Harness refused this computer (${response.status}): ${answer.error || "no reason given"}`
59
+ );
60
+ // Leaving storage: the computer's saved copy is its home, identity and keys included (cloudSave.mjs), so it comes back
61
+ // as itself; only its connection token is new.
62
+ if (answer.restore?.url && answer.restore?.key) {
63
+ await restore({
64
+ url: answer.restore.url,
65
+ key: answer.restore.key,
66
+ home: path.dirname(home),
67
+ fetchImpl,
68
+ });
69
+ log("restored this computer's saved copy");
70
+ }
71
+ const kept = readJson(path.join(home, "machine.json"), null);
72
+ writeJsonAtomic(path.join(home, "machine.json"), {
73
+ ...(answer.restore && kept?.id ? kept : {}),
74
+ id:
75
+ answer.restore && kept?.id
76
+ ? kept.id
77
+ : randomBytes(16).toString("base64url"),
78
+ label:
79
+ answer.restore && kept?.label
80
+ ? kept.label
81
+ : answer.label || "Cloud computer",
82
+ role: answer.role,
83
+ // Harness runs this computer: the person's other computers take it as an agents' computer, never as the person.
84
+ hosted: true,
85
+ ...(answer.joinCode ? { joinCode: answer.joinCode } : {}),
86
+ });
87
+ // Written last: a config with a token is what marks this computer enrolled. A restored one keeps its own settings.
88
+ // Where the boot script put the SSH gate goes with it (cloudSsh.mjs), on a provider that lets the person in over SSH.
89
+ const ssh =
90
+ typeof boot.ssh?.keys === "string" && typeof boot.ssh?.gate === "string"
91
+ ? { keys: boot.ssh.keys, gate: boot.ssh.gate }
92
+ : null;
93
+ writeJsonAtomic(path.join(home, "config.json"), {
94
+ ...(answer.restore ? readJson(path.join(home, "config.json"), {}) : {}),
95
+ origin: answer.origin,
96
+ token: answer.token,
97
+ cloud: true,
98
+ ...(ssh ? { ssh } : {}),
99
+ });
100
+ removeFile(bootFile);
101
+ log(`enrolled as ${answer.role === "home" ? "home" : "a worker"}`);
102
+ return { ok: true, role: answer.role };
103
+ }
104
+
105
+ // A box whose boot file has no instance id (one made before root read it at boot) asks the metadata service itself,
106
+ // as does a test standing in for the boot script (metadataUrl).
107
+ async function readInstanceId(fetchImpl, url) {
108
+ if (!url) throw new Error("the boot file names no instance id");
109
+ const metadata = await fetchImpl(url);
110
+ if (!metadata.ok)
111
+ throw new Error(`the metadata service answered ${metadata.status}`);
112
+ return (await metadata.text()).trim();
113
+ }
114
+
115
+ function removeFile(file) {
116
+ try {
117
+ fs.unlinkSync(file);
118
+ } catch {}
119
+ }
@@ -0,0 +1,197 @@
1
+ import fs from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { spawn } from "node:child_process";
5
+ import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
6
+ import { pipeline } from "node:stream/promises";
7
+ import { Readable } from "node:stream";
8
+
9
+ // A cloud computer's saved copy (src/server/cloud/computers.ts, docs/cloud-swarm.md §5.11): everything that is the
10
+ // person's lives under the harness user's home, so a copy of it is the computer. Harness asks for one before the box
11
+ // sleeps; the box packs its home, seals it with the computer's own key (AES-256-GCM: a 12-byte nonce, the ciphertext,
12
+ // the 16-byte tag; src/server/cloud/copyCipher.ts reads the same) and uploads it to a link made for that one object.
13
+ // A new box built when the computer leaves storage restores the copy before its daemon starts. Runs as the harness
14
+ // user, so it reads and writes only that user's files.
15
+
16
+ // What a new box makes again or never needs: caches, the installed runtime and local models, logs, sockets, temporary
17
+ // files, and project dependencies the project reinstalls (owner, October 25: every file that differs from the base
18
+ // image, minus caches, temporary files, logs and what is tied to the machine).
19
+ export const LEFT_OUT = [
20
+ "./.cache",
21
+ "./.npm",
22
+ "./.bun/install/cache",
23
+ "./.local/share/pnpm/store",
24
+ "./.harness/runtime",
25
+ "./.harness/local",
26
+ "./.harness/models",
27
+ "./.harness/*.log",
28
+ "./.harness/*.sock",
29
+ "./.harness/boot.json",
30
+ "./.harness/restore.json",
31
+ "*/node_modules",
32
+ "./tmp",
33
+ ];
34
+
35
+ const NONCE = 12;
36
+ const TAG = 16;
37
+
38
+ const run = (command, args, options) =>
39
+ new Promise((resolve, reject) => {
40
+ const child = spawn(command, args, options);
41
+ let err = "";
42
+ child.stderr?.on("data", (chunk) => (err += chunk));
43
+ child.on("error", reject);
44
+ child.on("close", (code) =>
45
+ code === 0
46
+ ? resolve()
47
+ : reject(new Error(`${command} exited ${code}: ${err.trim()}`))
48
+ );
49
+ });
50
+
51
+ /** The home folder packed and sealed into a temporary file; its path and size. */
52
+ export async function packHome({ home = os.homedir(), key }) {
53
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "harness-copy-"));
54
+ const file = path.join(dir, "copy.enc");
55
+ const nonce = randomBytes(NONCE);
56
+ const cipher = createCipheriv(
57
+ "aes-256-gcm",
58
+ Buffer.from(key, "base64url"),
59
+ nonce
60
+ );
61
+ const tar = spawn(
62
+ "tar",
63
+ [
64
+ "-czf",
65
+ "-",
66
+ ...LEFT_OUT.flatMap((pattern) => ["--exclude", pattern]),
67
+ "-C",
68
+ home,
69
+ ".",
70
+ ],
71
+ { stdio: ["ignore", "pipe", "pipe"] }
72
+ );
73
+ let err = "";
74
+ tar.stderr.on("data", (chunk) => (err += chunk));
75
+ const finished = new Promise((resolve, reject) => {
76
+ tar.on("error", reject);
77
+ // 1: some file changed while it was read (GNU tar); the copy is still whole for everything else.
78
+ tar.on("close", (code) =>
79
+ code === 0 || code === 1
80
+ ? resolve()
81
+ : reject(new Error(`tar exited ${code}: ${err.trim()}`))
82
+ );
83
+ });
84
+ const out = fs.createWriteStream(file, { mode: 0o600 });
85
+ out.write(nonce);
86
+ await pipeline(tar.stdout, cipher, out);
87
+ await finished;
88
+ fs.appendFileSync(file, cipher.getAuthTag());
89
+ return { file, dir, bytes: fs.statSync(file).size };
90
+ }
91
+
92
+ /**
93
+ * Asked by Harness before the box sleeps (the daemon's `cloud_save`): pack, seal, upload, and say it is up. One at a
94
+ * time; a second ask while one runs waits for that one.
95
+ */
96
+ export function createCloudSave({
97
+ origin,
98
+ token,
99
+ home = os.homedir(),
100
+ fetchImpl = fetch,
101
+ log = () => {},
102
+ }) {
103
+ let running = null;
104
+ const call = async (body) => {
105
+ const response = await fetchImpl(
106
+ `${String(origin).replace(/\/$/, "")}/api/cloud/save`,
107
+ {
108
+ method: "POST",
109
+ headers: {
110
+ authorization: `Bearer ${token()}`,
111
+ "content-type": "application/json",
112
+ },
113
+ body: JSON.stringify(body),
114
+ }
115
+ );
116
+ const answer = await response.json().catch(() => ({}));
117
+ if (!response.ok)
118
+ throw new Error(answer.error || `Harness answered ${response.status}`);
119
+ return answer;
120
+ };
121
+ const saveOnce = async () => {
122
+ const started = Date.now();
123
+ const { url, object, key } = await call({ phase: "start" });
124
+ const packed = await packHome({ home, key });
125
+ try {
126
+ const uploaded = await fetchImpl(url, {
127
+ method: "PUT",
128
+ headers: { "content-length": String(packed.bytes) },
129
+ body: Readable.toWeb(fs.createReadStream(packed.file)),
130
+ duplex: "half",
131
+ });
132
+ if (!uploaded.ok)
133
+ throw new Error(`storage refused the copy: ${uploaded.status}`);
134
+ await call({ phase: "done", object, bytes: packed.bytes });
135
+ log(
136
+ `saved a copy of this computer (${packed.bytes} bytes) in ${Math.round((Date.now() - started) / 1000)} s`
137
+ );
138
+ } finally {
139
+ fs.rmSync(packed.dir, { recursive: true, force: true });
140
+ }
141
+ };
142
+ const save = () => {
143
+ running ??= saveOnce()
144
+ .catch((error) => log(`saving a copy failed: ${error.message}`))
145
+ .finally(() => (running = null));
146
+ return running;
147
+ };
148
+ return { save };
149
+ }
150
+
151
+ /** A saved copy fetched from `url`, opened with `key` and unpacked into `home`, before the daemon starts. */
152
+ export async function restoreHome({
153
+ url,
154
+ key,
155
+ home = os.homedir(),
156
+ fetchImpl = fetch,
157
+ }) {
158
+ const response = await fetchImpl(url);
159
+ if (!response.ok || !response.body)
160
+ throw new Error(`the saved copy could not be fetched: ${response.status}`);
161
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "harness-restore-"));
162
+ try {
163
+ const sealed = path.join(dir, "copy.enc");
164
+ await pipeline(
165
+ Readable.fromWeb(response.body),
166
+ fs.createWriteStream(sealed, { mode: 0o600 })
167
+ );
168
+ const size = fs.statSync(sealed).size;
169
+ if (size < NONCE + TAG) throw new Error("the saved copy is cut short");
170
+ const fd = fs.openSync(sealed, "r");
171
+ const nonce = Buffer.alloc(NONCE);
172
+ const tag = Buffer.alloc(TAG);
173
+ fs.readSync(fd, nonce, 0, NONCE, 0);
174
+ fs.readSync(fd, tag, 0, TAG, size - TAG);
175
+ fs.closeSync(fd);
176
+ const decipher = createDecipheriv(
177
+ "aes-256-gcm",
178
+ Buffer.from(key, "base64url"),
179
+ nonce
180
+ );
181
+ decipher.setAuthTag(tag);
182
+ // Opened and checked whole before anything is unpacked: a copy that was changed unpacks nothing.
183
+ const archive = path.join(dir, "copy.tar.gz");
184
+ await pipeline(
185
+ fs.createReadStream(sealed, { start: NONCE, end: size - TAG - 1 }),
186
+ decipher,
187
+ fs.createWriteStream(archive, { mode: 0o600 })
188
+ );
189
+ fs.mkdirSync(home, { recursive: true });
190
+ // As the harness user, into its own home; tar keeps every path inside it (no absolute paths, no "..").
191
+ await run("tar", ["-xzf", archive, "-C", home, "--no-same-owner"], {
192
+ stdio: ["ignore", "ignore", "pipe"],
193
+ });
194
+ } finally {
195
+ fs.rmSync(dir, { recursive: true, force: true });
196
+ }
197
+ }