tryharness 0.7.45 → 0.7.54
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 +239 -14
- package/assets/dsh-plugin.mjs +72 -0
- package/assets/opencode-plugin.js +4 -3
- package/cli.mjs +497 -196
- package/lib/actorRoster.mjs +28 -5
- package/lib/adapters.mjs +165 -222
- package/lib/agentRuntimeConfig.mjs +36 -0
- package/lib/agentRuntimes.mjs +570 -0
- package/lib/agentSignIn.mjs +226 -0
- package/lib/atlas.mjs +7 -2
- package/lib/audit.mjs +643 -0
- package/lib/backupSetup.mjs +141 -0
- package/lib/boardAccess.mjs +8 -1
- package/lib/boardBackup.mjs +283 -0
- package/lib/boardCommands.mjs +5 -10
- package/lib/boardFileCommands.mjs +2 -1
- package/lib/boardMirror.mjs +523 -0
- package/lib/boardProtocol.json +263 -6
- package/lib/boardTools.mjs +2 -6
- package/lib/boardWaits.mjs +102 -0
- package/lib/book.mjs +659 -0
- package/lib/bridge.mjs +26 -4
- package/lib/browserWake.mjs +60 -0
- package/lib/cards.mjs +159 -16
- package/lib/cloudBoot.mjs +124 -0
- package/lib/commands.mjs +25 -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/context.mjs +18 -4
- package/lib/contractGate.mjs +377 -0
- package/lib/coordinator.mjs +152 -39
- package/lib/coreModels.mjs +78 -0
- package/lib/daemon.mjs +973 -54
- package/lib/desk.mjs +239 -0
- package/lib/desktop.mjs +113 -8
- package/lib/digest.mjs +32 -0
- package/lib/directLink.mjs +283 -0
- package/lib/environments.mjs +825 -0
- package/lib/folders.mjs +4 -4
- package/lib/gitHost.mjs +174 -0
- package/lib/goalCommands.mjs +58 -14
- package/lib/hookDialects.mjs +298 -0
- package/lib/inference.mjs +263 -152
- package/lib/landed.mjs +887 -0
- package/lib/lessons.mjs +6 -0
- package/lib/lineup.mjs +324 -0
- package/lib/live.mjs +284 -13
- 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 +189 -120
- package/lib/mcpRelay.mjs +119 -0
- package/lib/members.json +26 -0
- package/lib/members.mjs +33 -0
- package/lib/modelMemory.mjs +100 -0
- package/lib/models.mjs +48 -15
- package/lib/museSession.mjs +122 -0
- package/lib/nativeHost.mjs +203 -0
- package/lib/neovim.mjs +126 -0
- package/lib/network.mjs +7 -2
- package/lib/nudge.mjs +57 -18
- package/lib/onboard.mjs +430 -1
- package/lib/oneclawLocal.mjs +150 -0
- package/lib/ownMachines.mjs +828 -39
- package/lib/pairing.mjs +182 -0
- package/lib/paths.mjs +8 -0
- package/lib/personModel.mjs +89 -0
- package/lib/phones.mjs +119 -3
- package/lib/placeCommands.mjs +27 -0
- package/lib/places.mjs +337 -9
- package/lib/plans.mjs +151 -27
- package/lib/profile.mjs +152 -0
- package/lib/projects.mjs +624 -137
- package/lib/readiness.mjs +400 -0
- package/lib/record.mjs +115 -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 +37 -9
- package/lib/runner.mjs +74 -11
- 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 +386 -96
- package/lib/sessionIndex.mjs +5 -3
- package/lib/specialties.json +74 -0
- package/lib/specialties.mjs +477 -0
- package/lib/specialtyKeys.d.ts +3 -0
- package/lib/specialtyKeys.js +58 -0
- package/lib/specialtyNotice.mjs +147 -0
- package/lib/specialtySnapshot.mjs +138 -0
- package/lib/store.mjs +829 -29
- package/lib/subagents.mjs +8 -4
- package/lib/swarm.mjs +483 -37
- package/lib/swarmMemory.mjs +947 -0
- package/lib/t3.mjs +56 -17
- package/lib/taskCommands.mjs +91 -13
- package/lib/taskRuns.mjs +68 -11
- package/lib/team.json +253 -144
- package/lib/team.mjs +538 -147
- package/lib/threadLine.mjs +116 -0
- package/lib/threadRuns.mjs +78 -12
- package/lib/threadView.mjs +66 -16
- 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/lib/worktrees.mjs +51 -13
- package/package.json +10 -5
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// What the person sees of the board's backup (boardBackup.mjs does it): one step in setup, and npx tryharness backup.
|
|
2
|
+
// A Resident coming back to a computer with an empty board is offered their backup first; a Resident without one is
|
|
3
|
+
// offered to start it; anyone else is told plainly that their board stays on this computer.
|
|
4
|
+
|
|
5
|
+
const when = (iso) => {
|
|
6
|
+
const at = new Date(iso);
|
|
7
|
+
return Number.isNaN(at.getTime())
|
|
8
|
+
? "earlier"
|
|
9
|
+
: at.toLocaleString(undefined, { dateStyle: "medium", timeStyle: "short" });
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
async function askPassphrase(ui, { twice }) {
|
|
13
|
+
const first = await ui.password({
|
|
14
|
+
message: twice
|
|
15
|
+
? "Choose a backup passphrase (at least 8 characters). Only you will have it: without it the backup cannot be opened, by anyone."
|
|
16
|
+
: "Your backup passphrase",
|
|
17
|
+
});
|
|
18
|
+
if (!first) return null;
|
|
19
|
+
if (!twice) return first;
|
|
20
|
+
const again = await ui.password({ message: "Type it again" });
|
|
21
|
+
if (again !== first) {
|
|
22
|
+
ui.warn("The two did not match, so backups stay off.");
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
return first;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Setup's step, on home only: bring a Resident's board back, or offer to start backing it up. */
|
|
29
|
+
export async function offerBackup({ ui, daemonRequest, role, boardIsEmpty }) {
|
|
30
|
+
if (role !== "home") return;
|
|
31
|
+
const status = await daemonRequest({ command: "backup.status" }).catch(
|
|
32
|
+
() => null
|
|
33
|
+
);
|
|
34
|
+
if (!status?.ok || status.entitled === null) return;
|
|
35
|
+
if (status.entitled === false) {
|
|
36
|
+
ui.info(
|
|
37
|
+
"Your board stays on this computer. With Resident, Harness also keeps an encrypted backup of it, so a new computer can bring it back."
|
|
38
|
+
);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (status.on) return;
|
|
42
|
+
if (!ui.interactive) return;
|
|
43
|
+
if (status.latest && boardIsEmpty) {
|
|
44
|
+
ui.info(
|
|
45
|
+
`You have a backup of your board from ${status.latest.fromLabel || "another computer"}, ${when(status.latest.createdAt)}.`
|
|
46
|
+
);
|
|
47
|
+
for (let tries = 0; tries < 3; tries++) {
|
|
48
|
+
const passphrase = await askPassphrase(ui, { twice: false });
|
|
49
|
+
if (!passphrase) break;
|
|
50
|
+
const restored = await daemonRequest(
|
|
51
|
+
{ command: "backup.restore", passphrase },
|
|
52
|
+
120_000
|
|
53
|
+
);
|
|
54
|
+
if (restored.ok) {
|
|
55
|
+
ui.success(
|
|
56
|
+
`Your board is back (${restored.rows} items). It goes on backing up from this computer.`
|
|
57
|
+
);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
ui.warn(restored.error || "That backup could not be opened.");
|
|
61
|
+
if (restored.code !== "wrong_passphrase") break;
|
|
62
|
+
}
|
|
63
|
+
ui.info("To try again later: npx tryharness backup restore");
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const yes = await ui.confirm({
|
|
67
|
+
message:
|
|
68
|
+
"Keep an encrypted backup of your board with Harness? A new computer can bring it back with your passphrase; Harness cannot read it.",
|
|
69
|
+
initialValue: true,
|
|
70
|
+
});
|
|
71
|
+
if (!yes) return;
|
|
72
|
+
const passphrase = await askPassphrase(ui, { twice: true });
|
|
73
|
+
if (!passphrase) return;
|
|
74
|
+
const on = await daemonRequest({ command: "backup.on", passphrase }, 120_000);
|
|
75
|
+
if (on.ok)
|
|
76
|
+
ui.success("Your board is backed up, sealed with your passphrase.");
|
|
77
|
+
else ui.warn(on.error || "The backup did not start.");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** npx tryharness backup [on | now | restore | off] */
|
|
81
|
+
export async function backupCommand({ argv, ui, daemonRequest }) {
|
|
82
|
+
const sub = argv[0];
|
|
83
|
+
const must = (reply) => {
|
|
84
|
+
if (!reply?.ok)
|
|
85
|
+
throw new Error(reply?.error || "the backup did not answer");
|
|
86
|
+
return reply;
|
|
87
|
+
};
|
|
88
|
+
if (sub === "on") {
|
|
89
|
+
const passphrase = await askPassphrase(ui, { twice: true });
|
|
90
|
+
if (!passphrase) return;
|
|
91
|
+
const on = must(
|
|
92
|
+
await daemonRequest({ command: "backup.on", passphrase }, 120_000)
|
|
93
|
+
);
|
|
94
|
+
console.log(
|
|
95
|
+
`Backed up your board, sealed with your passphrase (${on.rows} items). It backs up again by itself when the board changes.`
|
|
96
|
+
);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (sub === "now") {
|
|
100
|
+
const done = must(await daemonRequest({ command: "backup.now" }, 120_000));
|
|
101
|
+
console.log(`Backed up your board (${done.rows} items).`);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (sub === "restore") {
|
|
105
|
+
const passphrase = await askPassphrase(ui, { twice: false });
|
|
106
|
+
if (!passphrase) return;
|
|
107
|
+
const restored = must(
|
|
108
|
+
await daemonRequest({ command: "backup.restore", passphrase }, 120_000)
|
|
109
|
+
);
|
|
110
|
+
console.log(
|
|
111
|
+
`Brought back your board from ${restored.from || "another computer"} (${restored.rows} items). Nothing that was already here was removed.`
|
|
112
|
+
);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (sub === "off") {
|
|
116
|
+
must(await daemonRequest({ command: "backup.off" }));
|
|
117
|
+
console.log(
|
|
118
|
+
"Backups are off on this computer. Harness keeps the last ones until a new one replaces them."
|
|
119
|
+
);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (sub)
|
|
123
|
+
throw new Error("usage: tryharness backup [on | now | restore | off]");
|
|
124
|
+
const status = must(await daemonRequest({ command: "backup.status" }));
|
|
125
|
+
if (status.entitled === false) {
|
|
126
|
+
console.log(
|
|
127
|
+
"Your board stays on this computer. An encrypted backup of it is a Resident feature."
|
|
128
|
+
);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
console.log(
|
|
132
|
+
status.on
|
|
133
|
+
? `Backups are on${status.last ? `: last ${when(status.last.at)}, ${status.last.rows} items` : ""}.`
|
|
134
|
+
: "Backups are off on this computer: npx tryharness backup on"
|
|
135
|
+
);
|
|
136
|
+
if (status.latest)
|
|
137
|
+
console.log(
|
|
138
|
+
`Harness keeps your newest from ${status.latest.fromLabel || "a computer"}, ${when(status.latest.createdAt)} (${status.latest.rows} items).`
|
|
139
|
+
);
|
|
140
|
+
if (status.error) console.log(`Harness did not answer: ${status.error}`);
|
|
141
|
+
}
|
package/lib/boardAccess.mjs
CHANGED
|
@@ -206,11 +206,18 @@ export function createBoardAccess({
|
|
|
206
206
|
};
|
|
207
207
|
const answerPlain = (input) => {
|
|
208
208
|
const agent = String(input.agent || "");
|
|
209
|
+
const command = String(input.command || "");
|
|
210
|
+
// The desk's book and the audits are personal (book.mjs, audit.mjs): never read through Harness in the clear.
|
|
211
|
+
if (command.startsWith("board.desk.") || command.startsWith("board.audit."))
|
|
212
|
+
return {
|
|
213
|
+
ok: false,
|
|
214
|
+
error:
|
|
215
|
+
"the desk's book and the audits stay on the person's computers: only the person and agents there read them",
|
|
216
|
+
};
|
|
209
217
|
// The person's and Harness's calls are answered in full only while no browser is paired; a hosted agent is always
|
|
210
218
|
// limited to what it is part of, paired or not.
|
|
211
219
|
if (!store.sealedOnly() && (agent === "user" || agent === "harness"))
|
|
212
220
|
return boardCommand(input);
|
|
213
|
-
const command = String(input.command || "");
|
|
214
221
|
// A thread Harness posts on accept: on the asking side, only one this machine asked for (it supplies the words itself).
|
|
215
222
|
if (
|
|
216
223
|
command === "board.post" &&
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { promisify } from "node:util";
|
|
3
|
+
import {
|
|
4
|
+
createCipheriv,
|
|
5
|
+
createDecipheriv,
|
|
6
|
+
pbkdf2,
|
|
7
|
+
randomBytes,
|
|
8
|
+
} from "node:crypto";
|
|
9
|
+
import { gunzipSync, gzipSync } from "node:zlib";
|
|
10
|
+
import { readJson, writeJsonAtomic } from "./files.mjs";
|
|
11
|
+
|
|
12
|
+
// The board's encrypted backup, for Residents (src/server/bridge/boardBackup.ts keeps it). Owner, October 22: the
|
|
13
|
+
// board survives losing a computer, "same as browser with resident you can have an encrypted backup". Home seals every
|
|
14
|
+
// board row (boardMirror.mjs's tables, as its pages carry them) with a key made from a passphrase only the person
|
|
15
|
+
// knows, and Harness keeps ciphertext it cannot read. Restoring brings the rows into the board here the way a computer
|
|
16
|
+
// joining brings its own (boardMirror.mjs bring): what is missing comes in, nothing here is removed.
|
|
17
|
+
//
|
|
18
|
+
// The key is kept beside the board in Harness's folder (0600), so backups go on by themselves: the board is on that
|
|
19
|
+
// disk unsealed anyway, and the seal is against Harness's servers. The same on every platform. Thread files' bytes
|
|
20
|
+
// (~/.harness/files) are not in it; their records are.
|
|
21
|
+
|
|
22
|
+
const MAGIC = Buffer.from("HRB1");
|
|
23
|
+
const SALT_BYTES = 16;
|
|
24
|
+
const IV_BYTES = 12;
|
|
25
|
+
const TAG_BYTES = 16;
|
|
26
|
+
// As the browser's backup derives its key (src/main/backup/backupClient.ts).
|
|
27
|
+
const ITERATIONS = 210_000;
|
|
28
|
+
const PART_BYTES = 512 * 1024;
|
|
29
|
+
const MIN_PASSPHRASE = 8;
|
|
30
|
+
// How often home looks whether the board changed since its last backup. A product cadence, not a deadline.
|
|
31
|
+
export const BACKUP_EVERY_MS = 60 * 60_000;
|
|
32
|
+
const LAST = "board-backup-last";
|
|
33
|
+
|
|
34
|
+
const derive = (passphrase, salt) =>
|
|
35
|
+
promisify(pbkdf2)(String(passphrase), salt, ITERATIONS, 32, "sha256");
|
|
36
|
+
|
|
37
|
+
function seal(key, salt, plain) {
|
|
38
|
+
const iv = randomBytes(IV_BYTES);
|
|
39
|
+
const cipher = createCipheriv("aes-256-gcm", key, iv);
|
|
40
|
+
const sealed = Buffer.concat([cipher.update(plain), cipher.final()]);
|
|
41
|
+
return Buffer.concat([MAGIC, salt, iv, cipher.getAuthTag(), sealed]);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The salt a sealed backup was made with, or null when it is not one. */
|
|
45
|
+
function saltOf(blob) {
|
|
46
|
+
if (blob.length < MAGIC.length + SALT_BYTES + IV_BYTES + TAG_BYTES)
|
|
47
|
+
return null;
|
|
48
|
+
if (!blob.subarray(0, MAGIC.length).equals(MAGIC)) return null;
|
|
49
|
+
return blob.subarray(MAGIC.length, MAGIC.length + SALT_BYTES);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The backup opened with this key; throws when the key is not the one it was sealed with. */
|
|
53
|
+
function open(key, blob) {
|
|
54
|
+
let at = MAGIC.length + SALT_BYTES;
|
|
55
|
+
const iv = blob.subarray(at, (at += IV_BYTES));
|
|
56
|
+
const tag = blob.subarray(at, (at += TAG_BYTES));
|
|
57
|
+
const decipher = createDecipheriv("aes-256-gcm", key, iv);
|
|
58
|
+
decipher.setAuthTag(tag);
|
|
59
|
+
return Buffer.concat([decipher.update(blob.subarray(at)), decipher.final()]);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function createBoardBackup({
|
|
63
|
+
store,
|
|
64
|
+
log,
|
|
65
|
+
mirror,
|
|
66
|
+
// A request to Harness over this computer's connection (bridge.mjs ask).
|
|
67
|
+
ask,
|
|
68
|
+
keyFile,
|
|
69
|
+
// Whether this computer holds the board: only home backs it up.
|
|
70
|
+
holdsBoard,
|
|
71
|
+
label,
|
|
72
|
+
everyMs = BACKUP_EVERY_MS,
|
|
73
|
+
}) {
|
|
74
|
+
const readKey = () => {
|
|
75
|
+
const saved = readJson(keyFile, null);
|
|
76
|
+
return saved?.salt && saved?.key
|
|
77
|
+
? {
|
|
78
|
+
salt: Buffer.from(saved.salt, "base64"),
|
|
79
|
+
key: Buffer.from(saved.key, "base64"),
|
|
80
|
+
}
|
|
81
|
+
: null;
|
|
82
|
+
};
|
|
83
|
+
const saveKey = ({ salt, key }) =>
|
|
84
|
+
writeJsonAtomic(keyFile, {
|
|
85
|
+
salt: salt.toString("base64"),
|
|
86
|
+
key: key.toString("base64"),
|
|
87
|
+
});
|
|
88
|
+
const last = () => JSON.parse(store.meta(LAST) || "null");
|
|
89
|
+
|
|
90
|
+
// Every row of the board, as the mirror's pages carry them.
|
|
91
|
+
const snapshot = () => {
|
|
92
|
+
const changes = [];
|
|
93
|
+
let after = 0;
|
|
94
|
+
for (;;) {
|
|
95
|
+
const page = mirror.since({ after, feed: mirror.feed() });
|
|
96
|
+
changes.push(...page.changes.filter((change) => change.row));
|
|
97
|
+
if (!page.changes.length || page.to >= page.head) break;
|
|
98
|
+
after = page.to;
|
|
99
|
+
}
|
|
100
|
+
return changes;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/** Whether this account keeps a board backup, the newest Harness has, and this computer's last. */
|
|
104
|
+
const status = async () => {
|
|
105
|
+
const server = await ask("board_backup_status", {}).catch((error) => ({
|
|
106
|
+
ok: false,
|
|
107
|
+
error: error.message,
|
|
108
|
+
}));
|
|
109
|
+
return {
|
|
110
|
+
ok: true,
|
|
111
|
+
on: Boolean(readKey()),
|
|
112
|
+
entitled:
|
|
113
|
+
server.ok === true
|
|
114
|
+
? true
|
|
115
|
+
: server.code === "upgrade_required"
|
|
116
|
+
? false
|
|
117
|
+
: null,
|
|
118
|
+
latest: server.ok === true ? (server.latest ?? null) : null,
|
|
119
|
+
last: last(),
|
|
120
|
+
...(server.ok === true || server.code === "upgrade_required"
|
|
121
|
+
? {}
|
|
122
|
+
: { error: server.error || "Harness did not answer" }),
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
let running = null;
|
|
127
|
+
/** Seal the board and hand it to Harness, a part at a time. */
|
|
128
|
+
const backUp = () =>
|
|
129
|
+
(running ??= backUpOnce().finally(() => (running = null)));
|
|
130
|
+
const backUpOnce = async () => {
|
|
131
|
+
if (!holdsBoard())
|
|
132
|
+
return {
|
|
133
|
+
ok: false,
|
|
134
|
+
code: "not_home",
|
|
135
|
+
error: "Your home computer backs up the board.",
|
|
136
|
+
};
|
|
137
|
+
const saved = readKey();
|
|
138
|
+
if (!saved)
|
|
139
|
+
return {
|
|
140
|
+
ok: false,
|
|
141
|
+
code: "no_passphrase",
|
|
142
|
+
error:
|
|
143
|
+
"Backups are off on this computer: npx tryharness backup on turns them on.",
|
|
144
|
+
};
|
|
145
|
+
const head = mirror.head();
|
|
146
|
+
const changes = snapshot();
|
|
147
|
+
const plain = gzipSync(
|
|
148
|
+
Buffer.from(
|
|
149
|
+
JSON.stringify({
|
|
150
|
+
format: 1,
|
|
151
|
+
at: new Date().toISOString(),
|
|
152
|
+
from: label(),
|
|
153
|
+
changes,
|
|
154
|
+
})
|
|
155
|
+
)
|
|
156
|
+
);
|
|
157
|
+
const blob = seal(saved.key, saved.salt, plain);
|
|
158
|
+
const uploadId = randomBytes(12).toString("base64url");
|
|
159
|
+
const count = Math.max(1, Math.ceil(blob.length / PART_BYTES));
|
|
160
|
+
let answer = null;
|
|
161
|
+
for (let index = 0; index < count; index++) {
|
|
162
|
+
answer = await ask("board_backup_put", {
|
|
163
|
+
uploadId,
|
|
164
|
+
index,
|
|
165
|
+
count,
|
|
166
|
+
data: blob
|
|
167
|
+
.subarray(index * PART_BYTES, (index + 1) * PART_BYTES)
|
|
168
|
+
.toString("base64"),
|
|
169
|
+
fromLabel: label(),
|
|
170
|
+
rows: changes.length,
|
|
171
|
+
}).catch((error) => ({ ok: false, error: error.message }));
|
|
172
|
+
if (answer?.ok !== true) {
|
|
173
|
+
log(
|
|
174
|
+
`the board's backup did not go to Harness: ${answer?.error || "no answer"}`
|
|
175
|
+
);
|
|
176
|
+
return { ok: false, code: answer?.code, error: answer?.error };
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const stored = answer?.stored ?? null;
|
|
180
|
+
store.meta(
|
|
181
|
+
LAST,
|
|
182
|
+
JSON.stringify({
|
|
183
|
+
head,
|
|
184
|
+
at: new Date().toISOString(),
|
|
185
|
+
version: stored?.version ?? null,
|
|
186
|
+
rows: changes.length,
|
|
187
|
+
bytes: blob.length,
|
|
188
|
+
})
|
|
189
|
+
);
|
|
190
|
+
log(
|
|
191
|
+
`backed up the board, sealed (${changes.length} rows, ${Math.round(blob.length / 1024)} KB)`
|
|
192
|
+
);
|
|
193
|
+
return { ok: true, rows: changes.length, bytes: blob.length, stored };
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/** The person chose a passphrase: backups go on from now, the first one at once. */
|
|
197
|
+
const turnOn = async (passphrase) => {
|
|
198
|
+
if (String(passphrase ?? "").length < MIN_PASSPHRASE)
|
|
199
|
+
return {
|
|
200
|
+
ok: false,
|
|
201
|
+
code: "short",
|
|
202
|
+
error: `A backup passphrase is at least ${MIN_PASSPHRASE} characters.`,
|
|
203
|
+
};
|
|
204
|
+
const salt = randomBytes(SALT_BYTES);
|
|
205
|
+
saveKey({ salt, key: await derive(passphrase, salt) });
|
|
206
|
+
const first = await backUp();
|
|
207
|
+
// Refused (not a Resident, or not home): backups stay off rather than seem on.
|
|
208
|
+
if (!first.ok && first.code !== undefined) turnOff();
|
|
209
|
+
return first;
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const turnOff = () => {
|
|
213
|
+
fs.rmSync(keyFile, { force: true });
|
|
214
|
+
return { ok: true };
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
/** The newest backup (or the version asked for), opened with the person's passphrase and brought into the board. */
|
|
218
|
+
const restore = async (passphrase, { version } = {}) => {
|
|
219
|
+
const parts = [];
|
|
220
|
+
let count = 1;
|
|
221
|
+
let found = null;
|
|
222
|
+
for (let index = 0; index < count; index++) {
|
|
223
|
+
const answer = await ask("board_backup_get", {
|
|
224
|
+
index,
|
|
225
|
+
...(version ? { version } : {}),
|
|
226
|
+
}).catch((error) => ({ ok: false, error: error.message }));
|
|
227
|
+
if (answer?.ok !== true)
|
|
228
|
+
return {
|
|
229
|
+
ok: false,
|
|
230
|
+
code: answer?.code,
|
|
231
|
+
error: answer?.error || "Harness did not answer",
|
|
232
|
+
};
|
|
233
|
+
count = answer.count;
|
|
234
|
+
found = answer.version;
|
|
235
|
+
parts.push(Buffer.from(String(answer.data), "base64"));
|
|
236
|
+
}
|
|
237
|
+
const blob = Buffer.concat(parts);
|
|
238
|
+
const salt = saltOf(blob);
|
|
239
|
+
if (!salt)
|
|
240
|
+
return { ok: false, error: "That backup is not one Harness can open." };
|
|
241
|
+
const key = await derive(passphrase, salt);
|
|
242
|
+
let plain;
|
|
243
|
+
try {
|
|
244
|
+
plain = open(key, blob);
|
|
245
|
+
} catch {
|
|
246
|
+
return {
|
|
247
|
+
ok: false,
|
|
248
|
+
code: "wrong_passphrase",
|
|
249
|
+
error: "That is not the passphrase this backup was made with.",
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
const backup = JSON.parse(gunzipSync(plain).toString("utf8"));
|
|
253
|
+
mirror.merge({ changes: backup.changes ?? [] }, { bring: true });
|
|
254
|
+
// The same passphrase goes on backing up the board from here.
|
|
255
|
+
saveKey({ salt, key });
|
|
256
|
+
log(
|
|
257
|
+
`brought back the board's backup from ${backup.from || "another computer"} (${backup.at}, ${backup.changes?.length ?? 0} rows)`
|
|
258
|
+
);
|
|
259
|
+
return {
|
|
260
|
+
ok: true,
|
|
261
|
+
version: found,
|
|
262
|
+
from: backup.from ?? null,
|
|
263
|
+
at: backup.at ?? null,
|
|
264
|
+
rows: backup.changes?.length ?? 0,
|
|
265
|
+
};
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
// Home looks now and then whether the board changed since its last backup.
|
|
269
|
+
const timer = setInterval(() => {
|
|
270
|
+
if (!readKey() || !holdsBoard() || last()?.head === mirror.head()) return;
|
|
271
|
+
void backUp();
|
|
272
|
+
}, everyMs);
|
|
273
|
+
timer.unref?.();
|
|
274
|
+
|
|
275
|
+
return {
|
|
276
|
+
status,
|
|
277
|
+
backUp,
|
|
278
|
+
turnOn,
|
|
279
|
+
turnOff,
|
|
280
|
+
restore,
|
|
281
|
+
stop: () => clearInterval(timer),
|
|
282
|
+
};
|
|
283
|
+
}
|
package/lib/boardCommands.mjs
CHANGED
|
@@ -15,20 +15,15 @@ export function createBoardCommands({
|
|
|
15
15
|
return { ok: false, error: "board requests need an agent name" };
|
|
16
16
|
if (request.command === "board.post") touchListener();
|
|
17
17
|
const agent = String(request.agent);
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
// The computer the call came from: this one, or a worker whose call home opened (ownMachines.mjs).
|
|
19
|
+
const machine = machineOf(request);
|
|
20
|
+
const lastLooked = store.lastSeenAt(agent, machine);
|
|
21
|
+
store.touchAgent(agent, machine);
|
|
20
22
|
const id = String(request.id || "");
|
|
21
23
|
const expected = Number.isInteger(request.expectedVersion)
|
|
22
24
|
? request.expectedVersion
|
|
23
25
|
: null;
|
|
24
|
-
|
|
25
|
-
const context = {
|
|
26
|
-
agent,
|
|
27
|
-
lastLooked,
|
|
28
|
-
id,
|
|
29
|
-
expected,
|
|
30
|
-
machine: machineOf(request),
|
|
31
|
-
};
|
|
26
|
+
const context = { agent, lastLooked, id, expected, machine };
|
|
32
27
|
const owner = boardCommandOwner(String(request.command));
|
|
33
28
|
const handler = Object.hasOwn(handlers, owner) ? handlers[owner] : task;
|
|
34
29
|
return handler(request, context);
|
|
@@ -23,7 +23,7 @@ export function createBoardFileCommands({
|
|
|
23
23
|
tooBig,
|
|
24
24
|
visibleFile,
|
|
25
25
|
}) {
|
|
26
|
-
return (request, { agent, id }) => {
|
|
26
|
+
return (request, { agent, id, machine }) => {
|
|
27
27
|
if (request.command === "board.file.add") {
|
|
28
28
|
const task = store.getTask(id);
|
|
29
29
|
if (!task) return { ok: false, error: "task not found" };
|
|
@@ -122,6 +122,7 @@ export function createBoardFileCommands({
|
|
|
122
122
|
author: agent,
|
|
123
123
|
text: `${agent} attached ${fileLine(file)}`,
|
|
124
124
|
source: `file:${file.id}`,
|
|
125
|
+
machine,
|
|
125
126
|
});
|
|
126
127
|
notifyBoard(store.getTask(place));
|
|
127
128
|
readFilesSoon();
|