tryharness 0.7.45 → 0.7.53
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 +232 -12
- package/cli.mjs +406 -184
- package/lib/actorRoster.mjs +28 -5
- package/lib/adapters.mjs +9 -1
- package/lib/agentRuntimeConfig.mjs +36 -0
- package/lib/agentRuntimes.mjs +560 -0
- package/lib/atlas.mjs +7 -2
- package/lib/boardCommands.mjs +5 -10
- package/lib/boardFileCommands.mjs +2 -1
- package/lib/boardMirror.mjs +459 -0
- package/lib/boardProtocol.json +206 -6
- package/lib/boardTools.mjs +2 -6
- package/lib/bridge.mjs +25 -3
- package/lib/browserWake.mjs +60 -0
- package/lib/cards.mjs +35 -2
- package/lib/commands.mjs +20 -5
- package/lib/connectionTools.d.ts +13 -0
- package/lib/connectionTools.js +45 -0
- package/lib/connectorAuth.mjs +309 -0
- package/lib/connectorCatalog.json +41 -0
- package/lib/connectorCommands.mjs +121 -0
- package/lib/connectorGoogle.mjs +513 -0
- package/lib/connectors.mjs +805 -0
- package/lib/coordinator.mjs +8 -1
- package/lib/coreModels.mjs +78 -0
- package/lib/daemon.mjs +643 -45
- package/lib/desktop.mjs +7 -2
- package/lib/digest.mjs +32 -0
- package/lib/directLink.mjs +283 -0
- package/lib/environments.mjs +595 -0
- package/lib/folders.mjs +2 -2
- package/lib/goalCommands.mjs +58 -14
- package/lib/inference.mjs +263 -152
- package/lib/lessons.mjs +6 -0
- package/lib/lineup.mjs +324 -0
- package/lib/localmodels.mjs +10 -0
- package/lib/machineIdentity.mjs +3 -0
- package/lib/machineRoutes.mjs +40 -2
- package/lib/machineRuns.mjs +30 -3
- package/lib/machineSeal.mjs +69 -10
- package/lib/mcp-stdio.mjs +121 -118
- package/lib/mcpRelay.mjs +119 -0
- package/lib/members.json +12 -0
- package/lib/members.mjs +33 -0
- package/lib/modelMemory.mjs +100 -0
- package/lib/modelTiers.json +1183 -0
- package/lib/modelTiers.mjs +133 -0
- package/lib/models.mjs +41 -8
- package/lib/museSession.mjs +119 -0
- package/lib/nativeHost.mjs +203 -0
- package/lib/network.mjs +7 -2
- package/lib/nudge.mjs +10 -2
- package/lib/onboard.mjs +430 -1
- package/lib/oneclawLocal.mjs +150 -0
- package/lib/ownMachines.mjs +521 -36
- package/lib/pairing.mjs +165 -0
- package/lib/paths.mjs +4 -0
- package/lib/phones.mjs +61 -0
- package/lib/placeCommands.mjs +27 -0
- package/lib/places.mjs +10 -2
- package/lib/plans.mjs +151 -27
- package/lib/profile.mjs +152 -0
- package/lib/projects.mjs +624 -137
- package/lib/readiness.mjs +283 -0
- package/lib/repoLinks.mjs +354 -0
- package/lib/roomChannels.mjs +641 -0
- package/lib/runBrief.mjs +32 -2
- package/lib/runRequests.mjs +157 -0
- package/lib/runWorker.mjs +20 -7
- package/lib/runner.mjs +67 -8
- package/lib/secretPatterns.json +55 -0
- package/lib/secrets.mjs +10 -27
- package/lib/seen.mjs +102 -0
- package/lib/service.mjs +11 -6
- package/lib/sessionControl.mjs +10 -8
- package/lib/sessionIndex.mjs +5 -3
- package/lib/store.mjs +546 -29
- package/lib/subagents.mjs +8 -4
- package/lib/swarm.mjs +367 -37
- package/lib/swarmMemory.mjs +947 -0
- package/lib/t3.mjs +43 -13
- package/lib/taskCommands.mjs +56 -5
- package/lib/taskRuns.mjs +61 -11
- package/lib/team.json +17 -15
- package/lib/team.mjs +68 -18
- package/lib/threadRuns.mjs +3 -1
- package/lib/threadView.mjs +51 -14
- package/lib/tokenizer.mjs +405 -0
- package/lib/ui.mjs +16 -2
- package/lib/usageTotals.mjs +148 -0
- package/lib/vault.mjs +1144 -0
- package/lib/vaultCloud.mjs +117 -0
- package/lib/vaultCommands.mjs +201 -0
- package/lib/vaultTools.d.ts +12 -0
- package/lib/vaultTools.js +42 -0
- package/lib/windowAsks.mjs +42 -0
- package/lib/workFocus.mjs +59 -0
- package/lib/workLinks.mjs +90 -0
- package/package.json +9 -4
package/lib/vault.mjs
ADDED
|
@@ -0,0 +1,1144 @@
|
|
|
1
|
+
// The swarm's secrets, kept by the relay on the person's own computer. Owner, October 21: the relay holds the secrets
|
|
2
|
+
// and agents in the swarm use them from it; "one daemon key always"; environment grants in the first version; a backup
|
|
3
|
+
// to 1claw that creates the person's 1claw account. The vault is a 1claw local vault (oneclawLocal.mjs) in
|
|
4
|
+
// <home>/vault.enc, sealed with a random key in <home>/vault-key (both owner-only), so `1claw local` and 1claw's
|
|
5
|
+
// daemon open the same file. Who may use what, the agents' requests and the backup's state live in the store
|
|
6
|
+
// (`vault:v1`); no value ever does.
|
|
7
|
+
//
|
|
8
|
+
// An agent never receives a value to read: use_secret sends the request from here, with the value in it, only to a
|
|
9
|
+
// website the person named for that secret, and the answer comes back with the value replaced by a marker. An
|
|
10
|
+
// environment grant is the exception the person chooses: the value is in that agent's run, and the run's log and
|
|
11
|
+
// result are scrubbed of it (taskRuns.mjs). A worker computer's run gets it from home, sealed to that computer, for the
|
|
12
|
+
// run (machineRuns.mjs); an agent on a worker uses a secret through home, so the value stays here.
|
|
13
|
+
import fs from "node:fs";
|
|
14
|
+
import path from "node:path";
|
|
15
|
+
import { randomBytes, randomUUID } from "node:crypto";
|
|
16
|
+
import {
|
|
17
|
+
emptyVault,
|
|
18
|
+
hostAllowed,
|
|
19
|
+
openVaultFile,
|
|
20
|
+
putSecret,
|
|
21
|
+
sealVaultFile,
|
|
22
|
+
} from "./oneclawLocal.mjs";
|
|
23
|
+
import { createOneclawCloud, OneclawError } from "./vaultCloud.mjs";
|
|
24
|
+
import { member, nameOf } from "./members.mjs";
|
|
25
|
+
|
|
26
|
+
const STATE = "vault:v1";
|
|
27
|
+
const REF = /\{\{\s*secret:([A-Za-z0-9_\-./]+)\s*\}\}/g;
|
|
28
|
+
const NAME_SHAPE = /^[A-Za-z0-9_\-.]+(\/[A-Za-z0-9_\-.]+)*$/;
|
|
29
|
+
// Names an object's own machinery answers to: never a secret's.
|
|
30
|
+
const RESERVED = new Set(["__proto__", "constructor", "prototype"]);
|
|
31
|
+
const NAME = {
|
|
32
|
+
test: (name) =>
|
|
33
|
+
NAME_SHAPE.test(name) &&
|
|
34
|
+
!name.includes("..") &&
|
|
35
|
+
!name.split("/").some((part) => RESERVED.has(part)),
|
|
36
|
+
};
|
|
37
|
+
const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
38
|
+
const HOST =
|
|
39
|
+
/^(\*\.)?[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*(:\d{1,5})?$/;
|
|
40
|
+
const TYPES = new Set([
|
|
41
|
+
"generic",
|
|
42
|
+
"password",
|
|
43
|
+
"api_key",
|
|
44
|
+
"certificate",
|
|
45
|
+
"private_key",
|
|
46
|
+
"ssh_key",
|
|
47
|
+
"env",
|
|
48
|
+
]);
|
|
49
|
+
const METHODS = new Set(["GET", "POST", "PUT", "PATCH", "DELETE"]);
|
|
50
|
+
const BODYLESS = new Set(["GET", "DELETE"]);
|
|
51
|
+
const REDIRECTS = new Set([301, 302, 303, 307, 308]);
|
|
52
|
+
const LOOPBACK = new Set(["127.0.0.1", "localhost", "[::1]"]);
|
|
53
|
+
// What use_secret gives back, from the old browser path (src/server/oneclaw/useSecret.ts): enough of an answer for an
|
|
54
|
+
// agent to act on, never a whole download.
|
|
55
|
+
const RESPONSE_CAP = 8_000;
|
|
56
|
+
const MAX_REDIRECTS = 3;
|
|
57
|
+
const OWNER_ONLY = "Only the person can do that, on their home computer.";
|
|
58
|
+
|
|
59
|
+
/** Every value in `text` (and its URL-encoded form) replaced by its name's marker. */
|
|
60
|
+
export function scrubValues(text, values) {
|
|
61
|
+
let out = String(text ?? "");
|
|
62
|
+
const variants = [];
|
|
63
|
+
for (const [name, value] of values) {
|
|
64
|
+
if (!value) continue;
|
|
65
|
+
// The value as a website or a program may write it back: plain, URL- and form-encoded, JSON- and HTML-escaped,
|
|
66
|
+
// and in base64. Base64 of the value inside a longer string (basic auth's user:password) is not caught.
|
|
67
|
+
const forms = new Set([
|
|
68
|
+
value,
|
|
69
|
+
encodeURIComponent(value),
|
|
70
|
+
encodeURIComponent(value).replace(/%20/g, "+"),
|
|
71
|
+
JSON.stringify(value).slice(1, -1),
|
|
72
|
+
value
|
|
73
|
+
.replace(/&/g, "&")
|
|
74
|
+
.replace(/</g, "<")
|
|
75
|
+
.replace(/>/g, ">")
|
|
76
|
+
.replace(/"/g, """),
|
|
77
|
+
Buffer.from(value).toString("base64").replace(/=+$/, ""),
|
|
78
|
+
Buffer.from(value).toString("base64url"),
|
|
79
|
+
]);
|
|
80
|
+
for (const form of forms) if (form.length >= 4) variants.push([name, form]);
|
|
81
|
+
}
|
|
82
|
+
variants.sort((a, b) => b[1].length - a[1].length);
|
|
83
|
+
for (const [name, variant] of variants)
|
|
84
|
+
out = out.split(variant).join(`[secret:${name}]`);
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function createVault({
|
|
89
|
+
store,
|
|
90
|
+
directory,
|
|
91
|
+
log = () => {},
|
|
92
|
+
machine,
|
|
93
|
+
cloudUrl,
|
|
94
|
+
notify = () => {},
|
|
95
|
+
// Whether another computer's run of `task` by `agent` is one it took and the person authorized (daemon.mjs).
|
|
96
|
+
runMayHaveSecrets = () => false,
|
|
97
|
+
// The run an agent's call came from (its id when the call names one it holds, else the one it is running), so the
|
|
98
|
+
// person meets its request in that run's thread (threadView.mjs); null when it is in no run.
|
|
99
|
+
runOf = () => null,
|
|
100
|
+
// A request was made or settled: the thread it waits in is read again everywhere.
|
|
101
|
+
onRequest = () => {},
|
|
102
|
+
// The person allowed what a run stopped to ask them for: the run hears it and carries on.
|
|
103
|
+
continueRun = () => {},
|
|
104
|
+
fetch = globalThis.fetch,
|
|
105
|
+
}) {
|
|
106
|
+
const file = path.join(directory, "vault.enc");
|
|
107
|
+
const keyFile = path.join(directory, "vault-key");
|
|
108
|
+
if (!fs.existsSync(keyFile))
|
|
109
|
+
fs.writeFileSync(keyFile, randomBytes(32).toString("base64url"), {
|
|
110
|
+
flag: "wx",
|
|
111
|
+
mode: 0o600,
|
|
112
|
+
});
|
|
113
|
+
const passphrase = fs.readFileSync(keyFile, "utf8").trim();
|
|
114
|
+
const cloud = createOneclawCloud({ base: cloudUrl, fetch });
|
|
115
|
+
|
|
116
|
+
const state = (() => {
|
|
117
|
+
try {
|
|
118
|
+
return {
|
|
119
|
+
grants: [],
|
|
120
|
+
requests: [],
|
|
121
|
+
shares: [],
|
|
122
|
+
backup: { status: "off" },
|
|
123
|
+
...JSON.parse(store.meta(STATE) ?? "{}"),
|
|
124
|
+
};
|
|
125
|
+
} catch {
|
|
126
|
+
return {
|
|
127
|
+
grants: [],
|
|
128
|
+
requests: [],
|
|
129
|
+
shares: [],
|
|
130
|
+
backup: { status: "off" },
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
})();
|
|
134
|
+
const saveState = () => store.meta(STATE, JSON.stringify(state));
|
|
135
|
+
|
|
136
|
+
// The vault in memory, as 1claw's daemon keeps it; every change is written before it is answered.
|
|
137
|
+
let vault = null;
|
|
138
|
+
let writing = Promise.resolve();
|
|
139
|
+
const ready = (async () => {
|
|
140
|
+
vault = fs.existsSync(file)
|
|
141
|
+
? await openVaultFile(fs.readFileSync(file), passphrase)
|
|
142
|
+
: emptyVault();
|
|
143
|
+
vault.harness ??= {};
|
|
144
|
+
vault.harness.removed ??= [];
|
|
145
|
+
})().catch((error) => {
|
|
146
|
+
log(`vault: could not open ${file}: ${error.message}`);
|
|
147
|
+
throw error;
|
|
148
|
+
});
|
|
149
|
+
const persist = () => {
|
|
150
|
+
writing = writing
|
|
151
|
+
.catch(() => {})
|
|
152
|
+
.then(async () => {
|
|
153
|
+
const sealed = await sealVaultFile(vault, passphrase);
|
|
154
|
+
const temporary = `${file}.tmp`;
|
|
155
|
+
fs.writeFileSync(temporary, sealed, { mode: 0o600 });
|
|
156
|
+
fs.renameSync(temporary, file);
|
|
157
|
+
});
|
|
158
|
+
return writing;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const now = () => new Date().toISOString();
|
|
162
|
+
const live = (grant) =>
|
|
163
|
+
!grant.expiresAt || Date.parse(grant.expiresAt) > Date.now();
|
|
164
|
+
const hostsOf = (name) => vault.secrets[name]?.allowed_hosts ?? [];
|
|
165
|
+
const publicSecret = ([name, secret]) => ({
|
|
166
|
+
name,
|
|
167
|
+
type: secret.type,
|
|
168
|
+
hosts: secret.allowed_hosts ?? [],
|
|
169
|
+
updatedAt: secret.updated_at,
|
|
170
|
+
backedUp: secret.synced_to_cloud === true,
|
|
171
|
+
});
|
|
172
|
+
const publicGrant = ({
|
|
173
|
+
id,
|
|
174
|
+
agent,
|
|
175
|
+
secret,
|
|
176
|
+
mode,
|
|
177
|
+
env,
|
|
178
|
+
hosted,
|
|
179
|
+
machines,
|
|
180
|
+
task,
|
|
181
|
+
expiresAt,
|
|
182
|
+
createdAt,
|
|
183
|
+
}) => ({
|
|
184
|
+
id,
|
|
185
|
+
agent,
|
|
186
|
+
secret,
|
|
187
|
+
mode,
|
|
188
|
+
...(env ? { env } : {}),
|
|
189
|
+
hosted,
|
|
190
|
+
machines,
|
|
191
|
+
task,
|
|
192
|
+
expiresAt,
|
|
193
|
+
createdAt,
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
// An agent asked for a secret it may not use yet: the person sees it once, and is told.
|
|
197
|
+
const ask = (agent, secret, why, host, runId) => {
|
|
198
|
+
const known = state.requests.find(
|
|
199
|
+
(request) => request.agent === agent && request.secret === secret
|
|
200
|
+
);
|
|
201
|
+
// Asked again for another website: a new request, so a card the person already sees for the first one cannot
|
|
202
|
+
// allow the second.
|
|
203
|
+
if (known && host && known.host && host !== known.host) {
|
|
204
|
+
state.requests = state.requests.filter((entry) => entry !== known);
|
|
205
|
+
onRequest(known.runId ?? null);
|
|
206
|
+
return ask(agent, secret, why, host, runId);
|
|
207
|
+
}
|
|
208
|
+
if (known) {
|
|
209
|
+
Object.assign(known, {
|
|
210
|
+
why,
|
|
211
|
+
host: host ?? known.host,
|
|
212
|
+
runId: runId ?? known.runId ?? null,
|
|
213
|
+
at: now(),
|
|
214
|
+
});
|
|
215
|
+
} else {
|
|
216
|
+
state.requests.push({
|
|
217
|
+
id: randomUUID(),
|
|
218
|
+
agent,
|
|
219
|
+
secret,
|
|
220
|
+
why,
|
|
221
|
+
host: host ?? null,
|
|
222
|
+
runId: runId ?? null,
|
|
223
|
+
at: now(),
|
|
224
|
+
});
|
|
225
|
+
notify(
|
|
226
|
+
why === "missing"
|
|
227
|
+
? `${agent} needs ${secret}${host ? ` for ${host}` : ""}. Add it in Harness's vault.`
|
|
228
|
+
: `${agent} asks to use ${secret}${host ? ` with ${host}` : ""}.`
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
saveState();
|
|
232
|
+
onRequest(runId ?? known?.runId ?? null);
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
/** What waits on the person in a run's thread: its agent's requests for secrets (threadView.mjs). */
|
|
236
|
+
const needs = (runId) =>
|
|
237
|
+
state.requests
|
|
238
|
+
.filter((request) => request.runId && request.runId === runId)
|
|
239
|
+
.map((request) => ({
|
|
240
|
+
id: `secret:${request.id}`,
|
|
241
|
+
kind: "secret",
|
|
242
|
+
agent: request.agent,
|
|
243
|
+
runId,
|
|
244
|
+
requestId: request.id,
|
|
245
|
+
secret: request.secret,
|
|
246
|
+
host: request.host,
|
|
247
|
+
missing: request.why === "missing",
|
|
248
|
+
text: `${nameOf(request.agent)} ${request.why === "missing" ? "needs" : "asks to use"} ${request.secret}${request.host ? ` for ${request.host}` : ""}.`,
|
|
249
|
+
at: request.at,
|
|
250
|
+
}));
|
|
251
|
+
|
|
252
|
+
const grantFor = (agent, name, mode, { hosted, fromElsewhere, task }) =>
|
|
253
|
+
state.grants.find(
|
|
254
|
+
(grant) =>
|
|
255
|
+
grant.agent === agent &&
|
|
256
|
+
grant.secret === name &&
|
|
257
|
+
grant.mode === mode &&
|
|
258
|
+
live(grant) &&
|
|
259
|
+
(!hosted || grant.hosted) &&
|
|
260
|
+
(!fromElsewhere || grant.machines === "own") &&
|
|
261
|
+
(!grant.task || grant.task === task)
|
|
262
|
+
);
|
|
263
|
+
|
|
264
|
+
/** The environment an agent's run gets from its grants, and the values to scrub from what the run says. */
|
|
265
|
+
const envFor = ({ agent, task = null, elsewhere = false }) => {
|
|
266
|
+
const env = {};
|
|
267
|
+
const values = new Map();
|
|
268
|
+
// Which secret each variable holds, so a computer that gets only the environment can name what it scrubs.
|
|
269
|
+
const names = {};
|
|
270
|
+
if (!vault) return { env, values, names };
|
|
271
|
+
for (const grant of state.grants) {
|
|
272
|
+
if (grant.mode !== "env" || grant.agent !== agent || !live(grant))
|
|
273
|
+
continue;
|
|
274
|
+
if (grant.task && grant.task !== task) continue;
|
|
275
|
+
if (elsewhere && grant.machines !== "own") continue;
|
|
276
|
+
const secret = vault.secrets[grant.secret];
|
|
277
|
+
if (!secret) continue;
|
|
278
|
+
env[grant.env] = secret.value;
|
|
279
|
+
values.set(grant.secret, secret.value);
|
|
280
|
+
names[grant.env] = grant.secret;
|
|
281
|
+
}
|
|
282
|
+
return { env, values, names };
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
// use_secret: the request leaves from here, with the values in it, only for websites they are allowed at.
|
|
286
|
+
const sendWithSecrets = async (
|
|
287
|
+
request,
|
|
288
|
+
agent,
|
|
289
|
+
{ hosted, fromElsewhere, from, signal }
|
|
290
|
+
) => {
|
|
291
|
+
if (request.restricted === true)
|
|
292
|
+
return {
|
|
293
|
+
ok: false,
|
|
294
|
+
error: "A restricted run cannot use the person's secrets.",
|
|
295
|
+
};
|
|
296
|
+
const url = String(request.url ?? "").trim();
|
|
297
|
+
const method = String(request.method ?? "GET").toUpperCase();
|
|
298
|
+
const headerLines = Array.isArray(request.headers)
|
|
299
|
+
? request.headers.map(String)
|
|
300
|
+
: [];
|
|
301
|
+
const body = typeof request.body === "string" ? request.body : undefined;
|
|
302
|
+
if (!METHODS.has(method))
|
|
303
|
+
return {
|
|
304
|
+
ok: false,
|
|
305
|
+
error: `${method} is not a method use_secret sends.`,
|
|
306
|
+
};
|
|
307
|
+
const names = [
|
|
308
|
+
...new Set(
|
|
309
|
+
[url, body, ...headerLines]
|
|
310
|
+
.filter(Boolean)
|
|
311
|
+
.flatMap((text) => [...text.matchAll(REF)].map((match) => match[1]))
|
|
312
|
+
),
|
|
313
|
+
];
|
|
314
|
+
if (!names.length)
|
|
315
|
+
return {
|
|
316
|
+
ok: false,
|
|
317
|
+
error:
|
|
318
|
+
"Put the secret where it goes as {{secret:name}} in the url, a header or the body.",
|
|
319
|
+
};
|
|
320
|
+
let target;
|
|
321
|
+
try {
|
|
322
|
+
target = new URL(url.replace(REF, "x"));
|
|
323
|
+
} catch {
|
|
324
|
+
return { ok: false, error: "That is not a URL." };
|
|
325
|
+
}
|
|
326
|
+
const values = new Map();
|
|
327
|
+
const runId = runOf(agent, from, request.runId);
|
|
328
|
+
for (const name of names) {
|
|
329
|
+
const secret = vault.secrets[name];
|
|
330
|
+
if (!secret) {
|
|
331
|
+
ask(agent, name, "missing", target.host, runId);
|
|
332
|
+
return {
|
|
333
|
+
ok: false,
|
|
334
|
+
error: `${name} is not in the person's vault. Harness asked them to add it; try again once they have.`,
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
if (
|
|
338
|
+
!grantFor(agent, name, "use", { hosted, fromElsewhere, task: runId })
|
|
339
|
+
) {
|
|
340
|
+
ask(agent, name, "allow", target.host, runId);
|
|
341
|
+
return {
|
|
342
|
+
ok: false,
|
|
343
|
+
error: `You may not use ${name} yet. Harness asked the person to allow it; try again once they have.`,
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
values.set(name, secret.value);
|
|
347
|
+
}
|
|
348
|
+
const fill = (text) =>
|
|
349
|
+
text.replace(REF, (whole, name) => values.get(name) ?? whole);
|
|
350
|
+
const allowedAt = (address) => {
|
|
351
|
+
let parsed;
|
|
352
|
+
try {
|
|
353
|
+
parsed = new URL(address);
|
|
354
|
+
} catch {
|
|
355
|
+
return "That is not a URL.";
|
|
356
|
+
}
|
|
357
|
+
if (parsed.username || parsed.password)
|
|
358
|
+
return "A URL with a user name or password in it is not sent.";
|
|
359
|
+
if (
|
|
360
|
+
parsed.protocol !== "https:" &&
|
|
361
|
+
!(parsed.protocol === "http:" && LOOPBACK.has(parsed.hostname))
|
|
362
|
+
)
|
|
363
|
+
return "Only https:// addresses are sent.";
|
|
364
|
+
for (const name of names) {
|
|
365
|
+
const check = hostAllowed(hostsOf(name), address);
|
|
366
|
+
if (!check.allowed) return `${name} cannot go there. ${check.reason}`;
|
|
367
|
+
}
|
|
368
|
+
return null;
|
|
369
|
+
};
|
|
370
|
+
const firstUrl = fill(url);
|
|
371
|
+
const refusal = allowedAt(firstUrl);
|
|
372
|
+
if (refusal) return { ok: false, error: refusal };
|
|
373
|
+
const headers = {};
|
|
374
|
+
for (const line of headerLines) {
|
|
375
|
+
const colon = line.indexOf(":");
|
|
376
|
+
if (colon > 0)
|
|
377
|
+
headers[line.slice(0, colon).trim()] = fill(
|
|
378
|
+
line.slice(colon + 1).trim()
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
try {
|
|
382
|
+
let address = firstUrl;
|
|
383
|
+
let verb = method;
|
|
384
|
+
let payload =
|
|
385
|
+
body !== undefined && !BODYLESS.has(method) ? fill(body) : undefined;
|
|
386
|
+
for (let redirects = 0; ; redirects += 1) {
|
|
387
|
+
const response = await fetch(address, {
|
|
388
|
+
method: verb,
|
|
389
|
+
headers,
|
|
390
|
+
...(payload === undefined ? {} : { body: payload }),
|
|
391
|
+
redirect: "manual",
|
|
392
|
+
...(signal ? { signal } : {}),
|
|
393
|
+
});
|
|
394
|
+
if (!REDIRECTS.has(response.status)) {
|
|
395
|
+
// Cleaned whole, then cut: a cut first could leave the start of a value behind.
|
|
396
|
+
const text = scrubValues(
|
|
397
|
+
`HTTP ${response.status} ${response.statusText}\n${await response.text()}`.trim(),
|
|
398
|
+
values
|
|
399
|
+
).slice(0, RESPONSE_CAP);
|
|
400
|
+
return { ok: true, status: response.status, response: text };
|
|
401
|
+
}
|
|
402
|
+
if (redirects >= MAX_REDIRECTS)
|
|
403
|
+
return { ok: false, error: "The website redirected too many times." };
|
|
404
|
+
const next = new URL(response.headers.get("location") ?? "", address);
|
|
405
|
+
if (
|
|
406
|
+
next.origin !== new URL(address).origin ||
|
|
407
|
+
allowedAt(next.toString())
|
|
408
|
+
)
|
|
409
|
+
return {
|
|
410
|
+
ok: false,
|
|
411
|
+
error:
|
|
412
|
+
"The website redirected to another address; a secret does not follow a redirect.",
|
|
413
|
+
};
|
|
414
|
+
if (
|
|
415
|
+
response.status === 303 ||
|
|
416
|
+
((response.status === 301 || response.status === 302) &&
|
|
417
|
+
verb === "POST")
|
|
418
|
+
) {
|
|
419
|
+
verb = "GET";
|
|
420
|
+
payload = undefined;
|
|
421
|
+
}
|
|
422
|
+
address = next.toString();
|
|
423
|
+
}
|
|
424
|
+
} catch (error) {
|
|
425
|
+
return {
|
|
426
|
+
ok: false,
|
|
427
|
+
error: scrubValues(`The request failed: ${error.message}`, values),
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
// ---- Backup to 1claw: the person's own account, one agent for this computer, one vault. ----
|
|
433
|
+
let agentToken = null;
|
|
434
|
+
let syncing = null;
|
|
435
|
+
let syncAgain = false;
|
|
436
|
+
const setBackup = (fields) => {
|
|
437
|
+
state.backup = { ...state.backup, ...fields };
|
|
438
|
+
saveState();
|
|
439
|
+
};
|
|
440
|
+
const tokenForAgent = async () => {
|
|
441
|
+
if (agentToken && agentToken.until > Date.now()) return agentToken.value;
|
|
442
|
+
const answer = await cloud.agentToken(vault.harness.cloud.apiKey);
|
|
443
|
+
// Renewed a minute before 1claw says it lapses, so a sync never starts with a token about to go.
|
|
444
|
+
agentToken = {
|
|
445
|
+
value: answer.access_token,
|
|
446
|
+
until: Date.now() + Math.max(0, (answer.expires_in ?? 0) - 60) * 1000,
|
|
447
|
+
};
|
|
448
|
+
return agentToken.value;
|
|
449
|
+
};
|
|
450
|
+
const stopped = (error) => {
|
|
451
|
+
if (error instanceof OneclawError && error.status === 402)
|
|
452
|
+
return "1claw asked for payment: this month's free requests on the 1claw account are used up. Nothing was paid. Sync again next month, or after upgrading in 1claw.";
|
|
453
|
+
if (
|
|
454
|
+
error instanceof OneclawError &&
|
|
455
|
+
error.type === "resource_limit_exceeded"
|
|
456
|
+
)
|
|
457
|
+
return `1claw's plan is full: ${error.message}`;
|
|
458
|
+
if (error instanceof OneclawError && error.status === 401)
|
|
459
|
+
return "1claw no longer accepts this computer's key; it may have been removed in 1claw. Turn backup on again.";
|
|
460
|
+
// 1claw answered, with an error of its own (seen October 22: a share link on a secret the agent may write).
|
|
461
|
+
if (error instanceof OneclawError)
|
|
462
|
+
return `1claw refused (${error.status}): ${error.message}`;
|
|
463
|
+
return `1claw could not be reached: ${error.message}`;
|
|
464
|
+
};
|
|
465
|
+
const upload = async () => {
|
|
466
|
+
const { vaultId } = vault.harness.cloud;
|
|
467
|
+
const token = await tokenForAgent();
|
|
468
|
+
const as = cloud.as(token);
|
|
469
|
+
for (const name of [...vault.harness.removed]) {
|
|
470
|
+
await as.deleteSecret(vaultId, name).catch((error) => {
|
|
471
|
+
if (!(error instanceof OneclawError && error.status === 404))
|
|
472
|
+
throw error;
|
|
473
|
+
});
|
|
474
|
+
vault.harness.removed = vault.harness.removed.filter(
|
|
475
|
+
(entry) => entry !== name
|
|
476
|
+
);
|
|
477
|
+
await persist();
|
|
478
|
+
}
|
|
479
|
+
for (const [name, secret] of Object.entries(vault.secrets)) {
|
|
480
|
+
if (secret.synced_to_cloud) continue;
|
|
481
|
+
const sent = secret.updated_at;
|
|
482
|
+
const stored = await as.putSecret(vaultId, name, {
|
|
483
|
+
value: secret.value,
|
|
484
|
+
type: secret.type,
|
|
485
|
+
metadata: {
|
|
486
|
+
allowed_hosts: secret.allowed_hosts ?? [],
|
|
487
|
+
saved_by: "harness",
|
|
488
|
+
},
|
|
489
|
+
});
|
|
490
|
+
Object.assign(secret, {
|
|
491
|
+
// Changed or removed while it uploaded: the next upload carries the change.
|
|
492
|
+
synced_to_cloud:
|
|
493
|
+
secret.updated_at === sent && vault.secrets[name] === secret,
|
|
494
|
+
cloud_vault_id: vaultId,
|
|
495
|
+
cloud_path: name,
|
|
496
|
+
cloud_secret_id: stored?.id ?? null,
|
|
497
|
+
});
|
|
498
|
+
if (
|
|
499
|
+
vault.secrets[name] !== secret &&
|
|
500
|
+
!vault.harness.removed.includes(name)
|
|
501
|
+
)
|
|
502
|
+
vault.harness.removed.push(name);
|
|
503
|
+
await persist();
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
// One upload at a time; a change during one is uploaded right after it.
|
|
507
|
+
const sync = () => {
|
|
508
|
+
if (vault?.harness?.cloud == null)
|
|
509
|
+
return Promise.resolve({ ok: false, error: "Backup is off." });
|
|
510
|
+
if (syncing) {
|
|
511
|
+
syncAgain = true;
|
|
512
|
+
return syncing;
|
|
513
|
+
}
|
|
514
|
+
syncing = (async () => {
|
|
515
|
+
try {
|
|
516
|
+
do {
|
|
517
|
+
syncAgain = false;
|
|
518
|
+
await upload();
|
|
519
|
+
} while (syncAgain);
|
|
520
|
+
setBackup({ status: "on", message: null, lastSync: now() });
|
|
521
|
+
return { ok: true };
|
|
522
|
+
} catch (error) {
|
|
523
|
+
log(`vault: backup stopped: ${error.message}`);
|
|
524
|
+
setBackup({ status: "paused", message: stopped(error) });
|
|
525
|
+
return { ok: false, error: state.backup.message };
|
|
526
|
+
} finally {
|
|
527
|
+
syncing = null;
|
|
528
|
+
}
|
|
529
|
+
})();
|
|
530
|
+
return syncing;
|
|
531
|
+
};
|
|
532
|
+
const changed = () => {
|
|
533
|
+
if (state.backup.status === "on") void sync();
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
// The person signed in to 1claw: their vault for Harness, this computer's agent on it, and nothing else of theirs.
|
|
537
|
+
const provision = async (humanToken, { bringIn }) => {
|
|
538
|
+
const person = cloud.as(humanToken);
|
|
539
|
+
const vaults = await person.vaults();
|
|
540
|
+
const harnessVault =
|
|
541
|
+
vaults.find((entry) => entry.name === "Harness") ??
|
|
542
|
+
(await person.createVault(
|
|
543
|
+
"Harness",
|
|
544
|
+
"The secrets your Harness relay keeps, backed up from your computer."
|
|
545
|
+
));
|
|
546
|
+
const name = `Harness on ${machine.label} (${machine.id.slice(0, 6)})`;
|
|
547
|
+
const existing = (await person.agents()).find(
|
|
548
|
+
(agent) => agent.name === name
|
|
549
|
+
);
|
|
550
|
+
let agentId, apiKey;
|
|
551
|
+
if (existing) {
|
|
552
|
+
agentId = existing.id;
|
|
553
|
+
apiKey = (await person.rotateKey(agentId)).api_key;
|
|
554
|
+
} else {
|
|
555
|
+
const created = await person.createAgent(
|
|
556
|
+
name,
|
|
557
|
+
"The Harness relay on this computer. It reads and writes only the Harness vault."
|
|
558
|
+
);
|
|
559
|
+
agentId = created.agent?.id ?? created.id;
|
|
560
|
+
apiKey = created.api_key;
|
|
561
|
+
await person.bindVault(agentId, harnessVault.id);
|
|
562
|
+
await person.grantAgent(harnessVault.id, agentId);
|
|
563
|
+
}
|
|
564
|
+
if (!apiKey) throw new Error("1claw did not give this computer a key.");
|
|
565
|
+
// Back on a computer that lost its vault, or a second one: what the backup holds and this vault lacks comes home.
|
|
566
|
+
for (const entry of await person.secrets(harnessVault.id)) {
|
|
567
|
+
if (vault.secrets[entry.path] || !NAME.test(entry.path)) continue;
|
|
568
|
+
const secret = await person.secret(harnessVault.id, entry.path);
|
|
569
|
+
if (secret.cmek_encrypted) continue;
|
|
570
|
+
putSecret(
|
|
571
|
+
vault,
|
|
572
|
+
entry.path,
|
|
573
|
+
secret.value,
|
|
574
|
+
TYPES.has(secret.type) ? secret.type : "generic",
|
|
575
|
+
{
|
|
576
|
+
allowed_hosts: (secret.metadata?.allowed_hosts ?? []).filter((host) =>
|
|
577
|
+
HOST.test(String(host))
|
|
578
|
+
),
|
|
579
|
+
}
|
|
580
|
+
);
|
|
581
|
+
Object.assign(vault.secrets[entry.path], {
|
|
582
|
+
synced_to_cloud: true,
|
|
583
|
+
cloud_vault_id: harnessVault.id,
|
|
584
|
+
cloud_path: entry.path,
|
|
585
|
+
cloud_secret_id: secret.id,
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
// What the person already kept in 1claw, outside 1claw's own vaults, comes in waiting for its websites.
|
|
589
|
+
if (bringIn)
|
|
590
|
+
for (const other of vaults) {
|
|
591
|
+
if (other.id === harnessVault.id || String(other.name).startsWith("__"))
|
|
592
|
+
continue;
|
|
593
|
+
for (const entry of await person.secrets(other.id)) {
|
|
594
|
+
const secret = await person.secret(other.id, entry.path);
|
|
595
|
+
if (secret.cmek_encrypted) continue;
|
|
596
|
+
const local = vault.secrets[entry.path]
|
|
597
|
+
? `${other.name}/${entry.path}`
|
|
598
|
+
: entry.path;
|
|
599
|
+
if (vault.secrets[local] || !NAME.test(local)) continue;
|
|
600
|
+
putSecret(
|
|
601
|
+
vault,
|
|
602
|
+
local,
|
|
603
|
+
secret.value,
|
|
604
|
+
TYPES.has(secret.type) ? secret.type : "generic",
|
|
605
|
+
{ allowed_hosts: [] }
|
|
606
|
+
);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
vault.harness.cloud = { apiKey, agentId, vaultId: harnessVault.id };
|
|
610
|
+
agentToken = null;
|
|
611
|
+
await persist();
|
|
612
|
+
};
|
|
613
|
+
let signingIn = 0;
|
|
614
|
+
const signIn = async (device, options) => {
|
|
615
|
+
// A newer sign-in replaces this one: only its approval sets the backup up.
|
|
616
|
+
const mine = ++signingIn;
|
|
617
|
+
const current = () =>
|
|
618
|
+
mine === signingIn && state.backup.status === "waiting";
|
|
619
|
+
const deadline = Date.now() + (device.expires_in ?? 0) * 1000;
|
|
620
|
+
let interval = (device.interval ?? 5) * 1000;
|
|
621
|
+
while (Date.now() < deadline && current()) {
|
|
622
|
+
await new Promise((resolve) => setTimeout(resolve, interval));
|
|
623
|
+
let answer;
|
|
624
|
+
try {
|
|
625
|
+
answer = await cloud.deviceToken(device.device_code);
|
|
626
|
+
} catch (error) {
|
|
627
|
+
// 1claw's CLI keeps polling through errors (pending is an error there); a payment, plan or rights refusal
|
|
628
|
+
// will not change by waiting.
|
|
629
|
+
if (
|
|
630
|
+
error instanceof OneclawError &&
|
|
631
|
+
[401, 402, 403].includes(error.status)
|
|
632
|
+
) {
|
|
633
|
+
if (current())
|
|
634
|
+
setBackup({
|
|
635
|
+
status: "off",
|
|
636
|
+
code: null,
|
|
637
|
+
verifyUrl: null,
|
|
638
|
+
message: stopped(error),
|
|
639
|
+
});
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
answer = { error: error.type ?? error.message };
|
|
643
|
+
}
|
|
644
|
+
if (!current()) return;
|
|
645
|
+
if (answer?.access_token) {
|
|
646
|
+
try {
|
|
647
|
+
await provision(answer.access_token, options);
|
|
648
|
+
setBackup({
|
|
649
|
+
status: "on",
|
|
650
|
+
email: answer.email ?? state.backup.email,
|
|
651
|
+
code: null,
|
|
652
|
+
verifyUrl: null,
|
|
653
|
+
message: null,
|
|
654
|
+
});
|
|
655
|
+
await sync();
|
|
656
|
+
} catch (error) {
|
|
657
|
+
log(`vault: 1claw setup failed: ${error.message}`);
|
|
658
|
+
setBackup({
|
|
659
|
+
status: "failed",
|
|
660
|
+
code: null,
|
|
661
|
+
verifyUrl: null,
|
|
662
|
+
message: stopped(error),
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
if (answer?.error === "slow_down") interval += 5_000;
|
|
668
|
+
if (
|
|
669
|
+
answer?.error === "access_denied" ||
|
|
670
|
+
answer?.error === "expired_token"
|
|
671
|
+
)
|
|
672
|
+
break;
|
|
673
|
+
}
|
|
674
|
+
if (current())
|
|
675
|
+
setBackup({
|
|
676
|
+
status: "off",
|
|
677
|
+
code: null,
|
|
678
|
+
verifyUrl: null,
|
|
679
|
+
message: "The 1claw code was not approved in time. Start again.",
|
|
680
|
+
});
|
|
681
|
+
};
|
|
682
|
+
|
|
683
|
+
// ---- Commands ----
|
|
684
|
+
const ownerCommands = {
|
|
685
|
+
"board.vault.save": async (request) => {
|
|
686
|
+
const name = String(request.name ?? "").trim();
|
|
687
|
+
const value = typeof request.value === "string" ? request.value : "";
|
|
688
|
+
const hosts = (Array.isArray(request.hosts) ? request.hosts : [])
|
|
689
|
+
.map((host) => String(host).trim().toLowerCase())
|
|
690
|
+
.filter(Boolean);
|
|
691
|
+
const type = TYPES.has(request.type)
|
|
692
|
+
? request.type
|
|
693
|
+
: (vault.secrets[name]?.type ?? "api_key");
|
|
694
|
+
if (!NAME.test(name) || name.includes(".."))
|
|
695
|
+
return {
|
|
696
|
+
ok: false,
|
|
697
|
+
error:
|
|
698
|
+
"A secret's name is letters, numbers, dashes, dots and slashes.",
|
|
699
|
+
};
|
|
700
|
+
if (!value && !vault.secrets[name])
|
|
701
|
+
return { ok: false, error: "There is no value to save." };
|
|
702
|
+
const badHost = hosts.find((host) => !HOST.test(host));
|
|
703
|
+
if (badHost)
|
|
704
|
+
return {
|
|
705
|
+
ok: false,
|
|
706
|
+
error: `${badHost} is not a website's address (for example api.fly.io).`,
|
|
707
|
+
};
|
|
708
|
+
if (value) putSecret(vault, name, value, type, { allowed_hosts: hosts });
|
|
709
|
+
else
|
|
710
|
+
Object.assign(vault.secrets[name], {
|
|
711
|
+
allowed_hosts: hosts,
|
|
712
|
+
type,
|
|
713
|
+
synced_to_cloud: false,
|
|
714
|
+
updated_at: now(),
|
|
715
|
+
});
|
|
716
|
+
vault.harness.removed = vault.harness.removed.filter(
|
|
717
|
+
(entry) => entry !== name
|
|
718
|
+
);
|
|
719
|
+
// An agent that asked for this secret before it existed now asks only to be allowed.
|
|
720
|
+
const nowAllow = state.requests.filter(
|
|
721
|
+
(entry) => entry.secret === name && entry.why === "missing"
|
|
722
|
+
);
|
|
723
|
+
for (const entry of nowAllow) entry.why = "allow";
|
|
724
|
+
saveState();
|
|
725
|
+
for (const entry of nowAllow) onRequest(entry.runId ?? null);
|
|
726
|
+
await persist();
|
|
727
|
+
changed();
|
|
728
|
+
return { ok: true, secret: publicSecret([name, vault.secrets[name]]) };
|
|
729
|
+
},
|
|
730
|
+
"board.vault.remove": async (request) => {
|
|
731
|
+
const name = String(request.name ?? "");
|
|
732
|
+
if (!vault.secrets[name])
|
|
733
|
+
return { ok: false, error: `${name} is not in the vault.` };
|
|
734
|
+
if (vault.secrets[name].cloud_path || vault.harness.cloud)
|
|
735
|
+
vault.harness.removed.push(name);
|
|
736
|
+
delete vault.secrets[name];
|
|
737
|
+
state.grants = state.grants.filter((grant) => grant.secret !== name);
|
|
738
|
+
saveState();
|
|
739
|
+
await persist();
|
|
740
|
+
changed();
|
|
741
|
+
return { ok: true };
|
|
742
|
+
},
|
|
743
|
+
"board.vault.grant": async (request) => {
|
|
744
|
+
// `agent` is the caller on every board call; the agent allowed is `forAgent`.
|
|
745
|
+
const agent = String(request.forAgent ?? "");
|
|
746
|
+
const secret = String(request.secret ?? "");
|
|
747
|
+
const mode = request.mode === "env" ? "env" : "use";
|
|
748
|
+
if (!agent || agent === "user")
|
|
749
|
+
return { ok: false, error: "Name the agent to allow." };
|
|
750
|
+
if (!vault.secrets[secret])
|
|
751
|
+
return { ok: false, error: `${secret} is not in the vault.` };
|
|
752
|
+
if (mode === "env" && !ENV_NAME.test(String(request.env ?? "")))
|
|
753
|
+
return {
|
|
754
|
+
ok: false,
|
|
755
|
+
error:
|
|
756
|
+
"An environment grant needs the variable's name (for example FLY_API_TOKEN).",
|
|
757
|
+
};
|
|
758
|
+
const grant = {
|
|
759
|
+
id: randomUUID(),
|
|
760
|
+
agent,
|
|
761
|
+
secret,
|
|
762
|
+
mode,
|
|
763
|
+
...(mode === "env" ? { env: String(request.env) } : {}),
|
|
764
|
+
// Harness, Bankr and the other agents that run on servers call from there: allowing one allows that.
|
|
765
|
+
hosted:
|
|
766
|
+
mode === "use" &&
|
|
767
|
+
(request.hosted === true ||
|
|
768
|
+
(member(agent)?.road ?? "local") !== "local"),
|
|
769
|
+
// A use grant leaves the value here wherever the agent runs; a value in a run's environment goes to another
|
|
770
|
+
// of the person's computers only when they say so.
|
|
771
|
+
machines:
|
|
772
|
+
request.machines === "home" || request.machines === "own"
|
|
773
|
+
? request.machines
|
|
774
|
+
: mode === "use"
|
|
775
|
+
? "own"
|
|
776
|
+
: "home",
|
|
777
|
+
task:
|
|
778
|
+
typeof request.task === "string" && request.task
|
|
779
|
+
? request.task
|
|
780
|
+
: null,
|
|
781
|
+
expiresAt:
|
|
782
|
+
typeof request.expiresAt === "string" && Date.parse(request.expiresAt)
|
|
783
|
+
? new Date(request.expiresAt).toISOString()
|
|
784
|
+
: null,
|
|
785
|
+
createdAt: now(),
|
|
786
|
+
};
|
|
787
|
+
state.grants = state.grants.filter(
|
|
788
|
+
(entry) =>
|
|
789
|
+
!(
|
|
790
|
+
entry.agent === agent &&
|
|
791
|
+
entry.secret === secret &&
|
|
792
|
+
entry.mode === mode &&
|
|
793
|
+
entry.task === grant.task &&
|
|
794
|
+
entry.env === grant.env
|
|
795
|
+
)
|
|
796
|
+
);
|
|
797
|
+
state.grants.push(grant);
|
|
798
|
+
const answered = state.requests.filter(
|
|
799
|
+
(entry) => entry.agent === agent && entry.secret === secret
|
|
800
|
+
);
|
|
801
|
+
state.requests = state.requests.filter(
|
|
802
|
+
(entry) => !answered.includes(entry)
|
|
803
|
+
);
|
|
804
|
+
saveState();
|
|
805
|
+
for (const entry of answered) onRequest(entry.runId ?? null);
|
|
806
|
+
return { ok: true, grant: publicGrant(grant) };
|
|
807
|
+
},
|
|
808
|
+
"board.vault.revoke": async (request) => {
|
|
809
|
+
const before = state.grants.length;
|
|
810
|
+
state.grants = state.grants.filter((grant) => grant.id !== request.id);
|
|
811
|
+
saveState();
|
|
812
|
+
return before === state.grants.length
|
|
813
|
+
? { ok: false, error: "No such permission." }
|
|
814
|
+
: { ok: true };
|
|
815
|
+
},
|
|
816
|
+
"board.vault.dismiss": async (request) => {
|
|
817
|
+
const settled = state.requests.find((entry) => entry.id === request.id);
|
|
818
|
+
state.requests = state.requests.filter(
|
|
819
|
+
(entry) => entry.id !== request.id
|
|
820
|
+
);
|
|
821
|
+
saveState();
|
|
822
|
+
onRequest(settled?.runId ?? null);
|
|
823
|
+
return { ok: true };
|
|
824
|
+
},
|
|
825
|
+
// One tap on an agent's request wherever the person meets it: the value when it was missing, then the agent may
|
|
826
|
+
// use it, and a run that stopped to ask hears so and carries on.
|
|
827
|
+
"board.vault.allow": async (request) => {
|
|
828
|
+
const asked = state.requests.find((entry) => entry.id === request.id);
|
|
829
|
+
if (!asked)
|
|
830
|
+
return { ok: false, error: "That request is already settled." };
|
|
831
|
+
// The card says which website: a request that changed since is not the one the person allowed.
|
|
832
|
+
if (
|
|
833
|
+
typeof request.host === "string" &&
|
|
834
|
+
request.host !== (asked.host ?? "")
|
|
835
|
+
)
|
|
836
|
+
return {
|
|
837
|
+
ok: false,
|
|
838
|
+
error: `The request changed: it now asks for ${asked.host}. Look again.`,
|
|
839
|
+
};
|
|
840
|
+
if (typeof request.value === "string" && request.value) {
|
|
841
|
+
const hosts = Array.isArray(request.hosts)
|
|
842
|
+
? request.hosts
|
|
843
|
+
: asked.host
|
|
844
|
+
? [asked.host]
|
|
845
|
+
: [];
|
|
846
|
+
const saved = await ownerCommands["board.vault.save"]({
|
|
847
|
+
name: asked.secret,
|
|
848
|
+
value: request.value,
|
|
849
|
+
hosts,
|
|
850
|
+
});
|
|
851
|
+
if (!saved.ok) return saved;
|
|
852
|
+
} else if (!vault.secrets[asked.secret])
|
|
853
|
+
return {
|
|
854
|
+
ok: false,
|
|
855
|
+
error: `${asked.secret} is not in the vault yet: add its value.`,
|
|
856
|
+
};
|
|
857
|
+
const granted = await ownerCommands["board.vault.grant"]({
|
|
858
|
+
forAgent: asked.agent,
|
|
859
|
+
secret: asked.secret,
|
|
860
|
+
mode: "use",
|
|
861
|
+
});
|
|
862
|
+
if (!granted.ok) return granted;
|
|
863
|
+
onRequest(asked.runId);
|
|
864
|
+
if (asked.runId)
|
|
865
|
+
continueRun(
|
|
866
|
+
asked.runId,
|
|
867
|
+
`You may use ${asked.secret} now (through use_secret). Carry on.`,
|
|
868
|
+
asked.at
|
|
869
|
+
);
|
|
870
|
+
return { ok: true, grant: granted.grant };
|
|
871
|
+
},
|
|
872
|
+
"board.vault.backup.start": async (request) => {
|
|
873
|
+
const email = String(request.email ?? "")
|
|
874
|
+
.trim()
|
|
875
|
+
.toLowerCase();
|
|
876
|
+
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email) || email.length > 254)
|
|
877
|
+
return {
|
|
878
|
+
ok: false,
|
|
879
|
+
error:
|
|
880
|
+
"Give the email of the 1claw account to back up to (a new one is made for it).",
|
|
881
|
+
};
|
|
882
|
+
if (state.backup.status === "waiting" && state.backup.email === email)
|
|
883
|
+
return {
|
|
884
|
+
ok: true,
|
|
885
|
+
code: state.backup.code,
|
|
886
|
+
verifyUrl: state.backup.verifyUrl,
|
|
887
|
+
};
|
|
888
|
+
let device;
|
|
889
|
+
try {
|
|
890
|
+
device = await cloud.deviceCode(email);
|
|
891
|
+
} catch (error) {
|
|
892
|
+
return { ok: false, error: stopped(error) };
|
|
893
|
+
}
|
|
894
|
+
const verifyUrl = cloud.verifyUrl(device.user_code);
|
|
895
|
+
setBackup({
|
|
896
|
+
status: "waiting",
|
|
897
|
+
email,
|
|
898
|
+
code: device.user_code,
|
|
899
|
+
verifyUrl,
|
|
900
|
+
message: null,
|
|
901
|
+
});
|
|
902
|
+
void signIn(device, { bringIn: request.bringIn === true });
|
|
903
|
+
return { ok: true, code: device.user_code, verifyUrl };
|
|
904
|
+
},
|
|
905
|
+
"board.vault.backup.sync": () => sync(),
|
|
906
|
+
// A 1claw agent key the person already has (the one a Harness browser kept before the vault moved here): what it
|
|
907
|
+
// can read comes in, waiting for its websites, and the key itself is used once and not kept.
|
|
908
|
+
"board.vault.import": async (request) => {
|
|
909
|
+
const apiKey = String(request.apiKey ?? "").trim();
|
|
910
|
+
if (!apiKey.startsWith("ocv_"))
|
|
911
|
+
return {
|
|
912
|
+
ok: false,
|
|
913
|
+
error: "That is not a 1claw agent key (it starts with ocv_).",
|
|
914
|
+
};
|
|
915
|
+
const imported = [];
|
|
916
|
+
try {
|
|
917
|
+
const token = await cloud.agentToken(apiKey);
|
|
918
|
+
const as = cloud.as(token.access_token);
|
|
919
|
+
const vaultIds = token.vault_ids?.length
|
|
920
|
+
? token.vault_ids
|
|
921
|
+
: (await as.vaults()).map((entry) => entry.id);
|
|
922
|
+
for (const vaultId of vaultIds)
|
|
923
|
+
for (const entry of await as.secrets(vaultId)) {
|
|
924
|
+
if (vault.secrets[entry.path] || !NAME.test(entry.path)) continue;
|
|
925
|
+
const secret = await as.secret(vaultId, entry.path);
|
|
926
|
+
if (secret.cmek_encrypted) continue;
|
|
927
|
+
putSecret(
|
|
928
|
+
vault,
|
|
929
|
+
entry.path,
|
|
930
|
+
secret.value,
|
|
931
|
+
TYPES.has(secret.type) ? secret.type : "generic",
|
|
932
|
+
{ allowed_hosts: [] }
|
|
933
|
+
);
|
|
934
|
+
imported.push(entry.path);
|
|
935
|
+
}
|
|
936
|
+
} catch (error) {
|
|
937
|
+
if (imported.length) await persist();
|
|
938
|
+
return { ok: false, imported, error: stopped(error) };
|
|
939
|
+
}
|
|
940
|
+
await persist();
|
|
941
|
+
changed();
|
|
942
|
+
return { ok: true, imported };
|
|
943
|
+
},
|
|
944
|
+
"board.vault.backup.stop": async () => {
|
|
945
|
+
if (vault.harness.cloud) delete vault.harness.cloud;
|
|
946
|
+
agentToken = null;
|
|
947
|
+
for (const secret of Object.values(vault.secrets))
|
|
948
|
+
secret.synced_to_cloud = false;
|
|
949
|
+
await persist();
|
|
950
|
+
setBackup({
|
|
951
|
+
status: "off",
|
|
952
|
+
code: null,
|
|
953
|
+
verifyUrl: null,
|
|
954
|
+
lastSync: null,
|
|
955
|
+
message:
|
|
956
|
+
"1claw still holds the last copy; remove it there if you want it gone.",
|
|
957
|
+
});
|
|
958
|
+
return { ok: true };
|
|
959
|
+
},
|
|
960
|
+
"board.vault.share": async (request) => {
|
|
961
|
+
const name = String(request.name ?? "");
|
|
962
|
+
if (!vault.secrets[name])
|
|
963
|
+
return { ok: false, error: `${name} is not in the vault.` };
|
|
964
|
+
if (state.backup.status !== "on" || !vault.harness.cloud)
|
|
965
|
+
return {
|
|
966
|
+
ok: false,
|
|
967
|
+
error: "Sharing goes through 1claw: turn backup on first.",
|
|
968
|
+
};
|
|
969
|
+
const expiresAt = Date.parse(String(request.expiresAt ?? ""));
|
|
970
|
+
if (!expiresAt || expiresAt <= Date.now())
|
|
971
|
+
return { ok: false, error: "A share needs a time it ends." };
|
|
972
|
+
const toAgent = typeof request.to === "object" && request.to?.agent;
|
|
973
|
+
if (
|
|
974
|
+
request.to !== "link" &&
|
|
975
|
+
!/^[0-9a-f-]{36}$/i.test(String(toAgent ?? ""))
|
|
976
|
+
)
|
|
977
|
+
return {
|
|
978
|
+
ok: false,
|
|
979
|
+
error: "Share with a link, or with a 1claw agent's id.",
|
|
980
|
+
};
|
|
981
|
+
const synced = await sync();
|
|
982
|
+
if (!synced.ok) return synced;
|
|
983
|
+
const secretId = vault.secrets[name].cloud_secret_id;
|
|
984
|
+
if (!secretId)
|
|
985
|
+
return {
|
|
986
|
+
ok: false,
|
|
987
|
+
error: `1claw has no copy of ${name} to share yet.`,
|
|
988
|
+
};
|
|
989
|
+
try {
|
|
990
|
+
const token = await tokenForAgent();
|
|
991
|
+
const created = await cloud.as(token).share(secretId, {
|
|
992
|
+
recipient_type: toAgent ? "agent" : "anyone_with_link",
|
|
993
|
+
...(toAgent ? { recipient_id: toAgent } : {}),
|
|
994
|
+
expires_at: new Date(expiresAt).toISOString(),
|
|
995
|
+
...(Number.isInteger(request.maxUses) && request.maxUses > 0
|
|
996
|
+
? { max_access_count: request.maxUses }
|
|
997
|
+
: {}),
|
|
998
|
+
...(typeof request.passphrase === "string" && request.passphrase
|
|
999
|
+
? { passphrase: request.passphrase }
|
|
1000
|
+
: {}),
|
|
1001
|
+
});
|
|
1002
|
+
const share = {
|
|
1003
|
+
id: created.id,
|
|
1004
|
+
name,
|
|
1005
|
+
to: toAgent ? { agent: toAgent } : "link",
|
|
1006
|
+
expiresAt: new Date(expiresAt).toISOString(),
|
|
1007
|
+
maxUses: created.max_access_count ?? null,
|
|
1008
|
+
createdAt: now(),
|
|
1009
|
+
};
|
|
1010
|
+
state.shares.push(share);
|
|
1011
|
+
saveState();
|
|
1012
|
+
// The link is the credential for whoever holds it: it is given once, here, and not kept.
|
|
1013
|
+
return {
|
|
1014
|
+
ok: true,
|
|
1015
|
+
share: {
|
|
1016
|
+
...share,
|
|
1017
|
+
url: new URL(created.share_url, `${cloud.base}/`).toString(),
|
|
1018
|
+
},
|
|
1019
|
+
};
|
|
1020
|
+
} catch (error) {
|
|
1021
|
+
return { ok: false, error: stopped(error) };
|
|
1022
|
+
}
|
|
1023
|
+
},
|
|
1024
|
+
"board.vault.unshare": async (request) => {
|
|
1025
|
+
const share = state.shares.find((entry) => entry.id === request.id);
|
|
1026
|
+
if (!share) return { ok: false, error: "No such share." };
|
|
1027
|
+
try {
|
|
1028
|
+
await cloud.as(await tokenForAgent()).unshare(share.id);
|
|
1029
|
+
} catch (error) {
|
|
1030
|
+
if (!(error instanceof OneclawError && error.status === 404))
|
|
1031
|
+
return { ok: false, error: stopped(error) };
|
|
1032
|
+
}
|
|
1033
|
+
state.shares = state.shares.filter((entry) => entry.id !== share.id);
|
|
1034
|
+
saveState();
|
|
1035
|
+
return { ok: true };
|
|
1036
|
+
},
|
|
1037
|
+
};
|
|
1038
|
+
|
|
1039
|
+
const list = (agent, { owner, hosted, fromElsewhere }) => {
|
|
1040
|
+
if (owner)
|
|
1041
|
+
return {
|
|
1042
|
+
ok: true,
|
|
1043
|
+
secrets: Object.entries(vault.secrets).map(publicSecret),
|
|
1044
|
+
grants: state.grants.filter(live).map(publicGrant),
|
|
1045
|
+
requests: state.requests,
|
|
1046
|
+
shares: state.shares.filter(
|
|
1047
|
+
(share) => Date.parse(share.expiresAt) > Date.now()
|
|
1048
|
+
),
|
|
1049
|
+
backup: {
|
|
1050
|
+
status: state.backup.status,
|
|
1051
|
+
email: state.backup.email ?? null,
|
|
1052
|
+
code: state.backup.code ?? null,
|
|
1053
|
+
verifyUrl: state.backup.verifyUrl ?? null,
|
|
1054
|
+
lastSync: state.backup.lastSync ?? null,
|
|
1055
|
+
message: state.backup.message ?? null,
|
|
1056
|
+
},
|
|
1057
|
+
};
|
|
1058
|
+
// An agent sees what it may use, and where.
|
|
1059
|
+
return {
|
|
1060
|
+
ok: true,
|
|
1061
|
+
secrets: Object.keys(vault.secrets)
|
|
1062
|
+
.filter((name) =>
|
|
1063
|
+
grantFor(agent, name, "use", { hosted, fromElsewhere })
|
|
1064
|
+
)
|
|
1065
|
+
.map((name) => ({ name, hosts: hostsOf(name) })),
|
|
1066
|
+
};
|
|
1067
|
+
};
|
|
1068
|
+
|
|
1069
|
+
/**
|
|
1070
|
+
* A board.vault.* call. `machine` is the computer it came from when not this one; the person's own changes are
|
|
1071
|
+
* made here only, by the person (not an agent, not Harness's servers, not another computer).
|
|
1072
|
+
*/
|
|
1073
|
+
const command = async (request, { agent, machine: from, signal } = {}) => {
|
|
1074
|
+
await ready;
|
|
1075
|
+
const hosted = request.hostedCaller === true;
|
|
1076
|
+
const fromElsewhere = Boolean(from) && from !== machine.id;
|
|
1077
|
+
const owner = agent === "user" && !hosted && !fromElsewhere;
|
|
1078
|
+
const name = String(request.command ?? "");
|
|
1079
|
+
if (name === "board.vault.list")
|
|
1080
|
+
return list(agent, { owner, hosted, fromElsewhere });
|
|
1081
|
+
if (name === "board.vault.use")
|
|
1082
|
+
return sendWithSecrets(request, agent, {
|
|
1083
|
+
hosted,
|
|
1084
|
+
fromElsewhere,
|
|
1085
|
+
from,
|
|
1086
|
+
signal,
|
|
1087
|
+
});
|
|
1088
|
+
// A worker computer's run, asking home for the environment its grants give it.
|
|
1089
|
+
if (name === "board.vault.env") {
|
|
1090
|
+
const runAgent = String(request.runAgent ?? "");
|
|
1091
|
+
const task = String(request.task ?? "");
|
|
1092
|
+
if (!fromElsewhere || hosted || !runMayHaveSecrets(task, runAgent, from))
|
|
1093
|
+
return {
|
|
1094
|
+
ok: false,
|
|
1095
|
+
error:
|
|
1096
|
+
"Only another of the person's computers asks for a run's environment, for a run it took.",
|
|
1097
|
+
};
|
|
1098
|
+
const { env, names } = envFor({ agent: runAgent, task, elsewhere: true });
|
|
1099
|
+
return { ok: true, env, names };
|
|
1100
|
+
}
|
|
1101
|
+
const handler = ownerCommands[name];
|
|
1102
|
+
if (!handler)
|
|
1103
|
+
return { ok: false, error: `${name} is not a vault command.` };
|
|
1104
|
+
if (!owner) return { ok: false, error: OWNER_ONLY };
|
|
1105
|
+
return handler(request);
|
|
1106
|
+
};
|
|
1107
|
+
|
|
1108
|
+
/**
|
|
1109
|
+
* Text, or anything holding text, with every value in the vault replaced by its marker: what an agent's own
|
|
1110
|
+
* session files say (a value it printed, or one the person pasted into its chat) before the index, a live read or
|
|
1111
|
+
* a raw fetch passes it on (sessionIndex.mjs, sessionControl.mjs, subagents.mjs).
|
|
1112
|
+
*/
|
|
1113
|
+
const scrubDeep = (value) => {
|
|
1114
|
+
if (!vault) return value;
|
|
1115
|
+
const values = new Map(
|
|
1116
|
+
Object.entries(vault.secrets).map(([name, secret]) => [
|
|
1117
|
+
name,
|
|
1118
|
+
secret.value,
|
|
1119
|
+
])
|
|
1120
|
+
);
|
|
1121
|
+
if (!values.size) return value;
|
|
1122
|
+
const walk = (item) => {
|
|
1123
|
+
if (typeof item === "string") return scrubValues(item, values);
|
|
1124
|
+
if (Array.isArray(item)) return item.map(walk);
|
|
1125
|
+
if (item && typeof item === "object")
|
|
1126
|
+
return Object.fromEntries(
|
|
1127
|
+
Object.entries(item).map(([key, entry]) => [key, walk(entry)])
|
|
1128
|
+
);
|
|
1129
|
+
return item;
|
|
1130
|
+
};
|
|
1131
|
+
return walk(value);
|
|
1132
|
+
};
|
|
1133
|
+
|
|
1134
|
+
return {
|
|
1135
|
+
ready,
|
|
1136
|
+
command,
|
|
1137
|
+
envFor,
|
|
1138
|
+
needs,
|
|
1139
|
+
scrubDeep,
|
|
1140
|
+
close: async () => {
|
|
1141
|
+
await writing.catch(() => {});
|
|
1142
|
+
},
|
|
1143
|
+
};
|
|
1144
|
+
}
|