tryharness 0.7.54 → 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.
- package/README.md +15 -16
- package/cli.mjs +377 -62
- package/lib/activity.mjs +10 -10
- package/lib/adapters.mjs +16 -0
- package/lib/agentBinaries.mjs +71 -0
- package/lib/agentRuntimeConfig.mjs +1 -1
- package/lib/agentRuntimes.mjs +9 -3
- package/lib/agentSignIn.mjs +271 -41
- package/lib/answers.mjs +5 -5
- package/lib/archetypes.d.ts +64 -0
- package/lib/archetypes.js +989 -0
- package/lib/asks.mjs +4 -4
- package/lib/audit.mjs +36 -13
- package/lib/backupSetup.mjs +4 -2
- package/lib/boardAccess.mjs +17 -11
- package/lib/boardBackup.mjs +6 -6
- package/lib/boardFileCommands.mjs +6 -6
- package/lib/boardFiles.mjs +13 -12
- package/lib/boardMirror.mjs +82 -47
- package/lib/boardProtocol.json +30 -1
- package/lib/boardWaits.mjs +2 -3
- package/lib/book.mjs +6 -6
- package/lib/bridge.mjs +5 -5
- package/lib/cards.mjs +2 -2
- package/lib/cloudAgents.mjs +216 -0
- package/lib/cloudAi.mjs +1312 -0
- package/lib/cloudAssistants.mjs +242 -0
- package/lib/cloudBoot.mjs +81 -86
- package/lib/cloudSave.mjs +197 -0
- package/lib/cloudSsh.mjs +178 -0
- package/lib/context.mjs +3 -3
- package/lib/contractGate.mjs +34 -12
- package/lib/control-client.mjs +9 -1
- package/lib/controlSocket.mjs +86 -0
- package/lib/coordinator.mjs +103 -101
- package/lib/daemon.mjs +1128 -2865
- package/lib/desk.mjs +7 -6
- package/lib/desktop.mjs +72 -68
- package/lib/directLink.mjs +16 -5
- package/lib/environment.mjs +2 -1
- package/lib/environments.mjs +14 -6
- package/lib/firewall.mjs +9 -9
- package/lib/folders.mjs +18 -14
- package/lib/frames.mjs +2 -3
- package/lib/gitHost.mjs +3 -3
- package/lib/goalCommands.mjs +25 -7
- package/lib/goals.mjs +7 -7
- package/lib/guestRequests.mjs +94 -0
- package/lib/handoffs.mjs +8 -7
- package/lib/handover.mjs +4 -4
- package/lib/harnessModel.mjs +6 -6
- package/lib/harnessOffers.mjs +266 -0
- package/lib/inference.mjs +26 -8
- package/lib/jobAnswers.mjs +190 -0
- package/lib/landed.mjs +1 -1
- package/lib/lane.mjs +42 -11
- package/lib/lessons.mjs +16 -16
- package/lib/library.mjs +3 -3
- package/lib/lineup.mjs +3 -0
- package/lib/linkRelay.mjs +574 -0
- package/lib/localAgentAsk.mjs +74 -0
- package/lib/localStack.mjs +536 -0
- package/lib/localmodels.mjs +6 -4
- package/lib/loopBackstop.mjs +4 -4
- package/lib/machineIdentity.mjs +9 -1
- package/lib/machineRoutes.mjs +181 -2
- package/lib/machineRuns.mjs +76 -1
- package/lib/machineSeal.mjs +39 -12
- package/lib/mcp-stdio.mjs +96 -26
- package/lib/mcpRelay.mjs +1 -1
- package/lib/members.json +13 -0
- package/lib/members.mjs +1 -1
- package/lib/models.mjs +77 -23
- package/lib/museSession.mjs +6 -1
- package/lib/nativeHost.mjs +35 -7
- package/lib/network.mjs +50 -5
- package/lib/notify.mjs +7 -5
- package/lib/nudge.mjs +3 -3
- package/lib/ocr.mjs +9 -9
- package/lib/onboard.mjs +26 -20
- package/lib/oneLine.mjs +7 -7
- package/lib/ownMachines.mjs +928 -149
- package/lib/pairing.mjs +33 -6
- package/lib/paths.mjs +15 -1
- package/lib/peerProcess.mjs +124 -0
- package/lib/personModel.mjs +3 -3
- package/lib/personNotices.mjs +151 -0
- package/lib/phones.mjs +490 -38
- package/lib/placeCommands.mjs +4 -4
- package/lib/places.mjs +70 -2
- package/lib/profile.mjs +67 -14
- package/lib/projectGit.mjs +158 -14
- package/lib/projectPieces.mjs +902 -0
- package/lib/projectSettings.mjs +7 -4
- package/lib/projects.mjs +384 -62
- package/lib/provenance.mjs +5 -1
- package/lib/proxyLinks.mjs +267 -0
- package/lib/reactions.mjs +7 -5
- package/lib/readiness.mjs +12 -7
- package/lib/record.mjs +19 -6
- package/lib/redact.mjs +4 -4
- package/lib/relayLimits.mjs +62 -0
- package/lib/release.mjs +170 -0
- package/lib/releaseKeys.json +6 -0
- package/lib/repoLinks.mjs +4 -4
- package/lib/roomChannels.mjs +24 -0
- package/lib/roomPage.mjs +1 -1
- package/lib/runAuthority.mjs +101 -0
- package/lib/runBrief.mjs +19 -8
- package/lib/runWorker.mjs +14 -5
- package/lib/runner.mjs +215 -25
- package/lib/runtime-install.mjs +143 -9
- package/lib/schedules.mjs +2 -2
- package/lib/screens.mjs +1 -1
- package/lib/sealed.mjs +11 -2
- package/lib/sealedThreads.mjs +671 -0
- package/lib/secretPatterns.json +1 -1
- package/lib/secrets.mjs +4 -4
- package/lib/seen.mjs +6 -6
- package/lib/serverWords.mjs +175 -0
- package/lib/service.mjs +22 -12
- package/lib/sessionControl.mjs +6 -6
- package/lib/specialties.mjs +6 -6
- package/lib/specialtyKeys.js +4 -5
- package/lib/specialtyNotice.mjs +7 -7
- package/lib/specialtySnapshot.mjs +5 -5
- package/lib/speech.mjs +5 -5
- package/lib/store.mjs +140 -39
- package/lib/stuck.mjs +1 -1
- package/lib/swarm.mjs +167 -16
- package/lib/swarmDevices.mjs +319 -0
- package/lib/swarmMemory.mjs +27 -25
- package/lib/t3.mjs +13 -13
- package/lib/taskCommands.mjs +44 -11
- package/lib/taskRooms.mjs +125 -0
- package/lib/taskRuns.mjs +36 -1
- package/lib/team.mjs +41 -18
- package/lib/threadActions.mjs +315 -0
- package/lib/threadAuthority.mjs +101 -0
- package/lib/threadBriefs.mjs +178 -0
- package/lib/threadLine.mjs +2 -2
- package/lib/threadRuns.mjs +138 -43
- package/lib/threadView.mjs +5 -5
- package/lib/usageTotals.mjs +2 -3
- package/lib/vault.mjs +101 -10
- package/lib/vaultCloud.mjs +2 -2
- package/lib/vaultCommands.mjs +15 -6
- package/lib/vaultTools.js +2 -2
- package/lib/verdicts.mjs +328 -0
- package/lib/wakes.mjs +1 -1
- package/lib/watches.mjs +2 -2
- package/lib/windowAsks.mjs +13 -5
- package/lib/worktrees.mjs +112 -18
- package/npm-shrinkwrap.json +1093 -0
- package/package.json +11 -10
- package/release.json +220 -0
|
@@ -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
|
+
}
|
package/lib/cloudBoot.mjs
CHANGED
|
@@ -1,85 +1,49 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { randomBytes } from "node:crypto";
|
|
4
|
-
import { spawnSync } from "node:child_process";
|
|
5
4
|
import { readJson, writeJsonAtomic } from "./files.mjs";
|
|
5
|
+
import { restoreHome } from "./cloudSave.mjs";
|
|
6
6
|
|
|
7
|
-
// A cloud computer's enrollment (docs/cloud-swarm.md §5.3), run
|
|
8
|
-
// starts (src/server/cloud/bootScript.ts).
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
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.
|
|
15
17
|
|
|
16
|
-
|
|
17
|
-
const METADATA_ADDRESS = "169.254.169.254";
|
|
18
|
-
|
|
19
|
-
const ownerOf = (dir) => {
|
|
20
|
-
const stat = fs.statSync(dir);
|
|
21
|
-
return { uid: stat.uid, gid: stat.gid };
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
function writeOwned(file, value, owner) {
|
|
25
|
-
writeJsonAtomic(file, value);
|
|
26
|
-
fs.chmodSync(file, 0o600);
|
|
27
|
-
if (owner && process.getuid?.() === 0)
|
|
28
|
-
fs.chownSync(file, owner.uid, owner.gid);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Best effort: a container in tests has no iptables, and the rule does not survive a reboot, so it is put back on
|
|
32
|
-
// every start (the service runs this before each daemon start).
|
|
33
|
-
function closeMetadata(owner, log) {
|
|
34
|
-
if (!owner || process.getuid?.() !== 0) return;
|
|
35
|
-
const rule = [
|
|
36
|
-
"OUTPUT",
|
|
37
|
-
"-d",
|
|
38
|
-
METADATA_ADDRESS,
|
|
39
|
-
"-m",
|
|
40
|
-
"owner",
|
|
41
|
-
"--uid-owner",
|
|
42
|
-
String(owner.uid),
|
|
43
|
-
"-j",
|
|
44
|
-
"REJECT",
|
|
45
|
-
];
|
|
46
|
-
const present = spawnSync("iptables", ["-C", ...rule], { stdio: "ignore" });
|
|
47
|
-
if (present.error)
|
|
48
|
-
return log("iptables is missing; the metadata service stays open");
|
|
49
|
-
if (present.status === 0) return;
|
|
50
|
-
const added = spawnSync("iptables", ["-I", ...rule], { stdio: "ignore" });
|
|
51
|
-
if (added.status !== 0)
|
|
52
|
-
log("could not close the metadata service to the harness user");
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Enroll this cloud computer once; later starts find it enrolled and only close the metadata service again.
|
|
57
|
-
* home is the harness user's ~/.harness; its parent directory's owner is who the files belong to.
|
|
58
|
-
*/
|
|
18
|
+
/** Enroll this cloud computer once; later starts find it enrolled. home is the harness user's ~/.harness. */
|
|
59
19
|
export async function cloudEnroll({
|
|
60
|
-
bootFile = "/etc/harness/boot.json",
|
|
61
20
|
home = "/home/harness/.harness",
|
|
21
|
+
bootFile = path.join(home, "boot.json"),
|
|
62
22
|
metadataUrl,
|
|
63
23
|
fetchImpl = fetch,
|
|
24
|
+
restore = restoreHome,
|
|
64
25
|
log = (line) => console.log(`harness cloud: ${line}`),
|
|
65
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
|
+
);
|
|
66
31
|
fs.mkdirSync(home, { recursive: true, mode: 0o700 });
|
|
67
|
-
|
|
68
|
-
|
|
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"));
|
|
69
34
|
const config = readJson(path.join(home, "config.json"), {});
|
|
70
35
|
if (config.origin && config.token) {
|
|
71
|
-
|
|
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);
|
|
72
38
|
return { ok: true, already: true };
|
|
73
39
|
}
|
|
74
40
|
const boot = readJson(bootFile, null);
|
|
75
41
|
if (!boot?.origin || !boot?.bootstrap)
|
|
76
42
|
throw new Error(`${bootFile} has no enrollment in it`);
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
throw new Error(`the metadata service answered ${metadata.status}`);
|
|
82
|
-
const instanceId = (await metadata.text()).trim();
|
|
43
|
+
const instanceId =
|
|
44
|
+
boot.instanceId && !metadataUrl
|
|
45
|
+
? String(boot.instanceId)
|
|
46
|
+
: await readInstanceId(fetchImpl, metadataUrl || boot.instanceIdUrl);
|
|
83
47
|
const response = await fetchImpl(
|
|
84
48
|
`${String(boot.origin).replace(/\/$/, "")}/api/cloud/enroll`,
|
|
85
49
|
{
|
|
@@ -93,32 +57,63 @@ export async function cloudEnroll({
|
|
|
93
57
|
throw new Error(
|
|
94
58
|
`Harness refused this computer (${response.status}): ${answer.error || "no reason given"}`
|
|
95
59
|
);
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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);
|
|
113
101
|
log(`enrolled as ${answer.role === "home" ? "home" : "a worker"}`);
|
|
114
|
-
closeMetadata(owner, log);
|
|
115
102
|
return { ok: true, role: answer.role };
|
|
116
103
|
}
|
|
117
104
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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 {}
|
|
124
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
|
+
}
|