tryharness 0.7.53 → 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 +10 -5
- package/assets/dsh-plugin.mjs +72 -0
- package/assets/opencode-plugin.js +4 -3
- package/cli.mjs +115 -36
- package/lib/adapters.mjs +157 -222
- package/lib/agentRuntimes.mjs +26 -16
- package/lib/agentSignIn.mjs +226 -0
- 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/boardMirror.mjs +71 -7
- package/lib/boardProtocol.json +59 -2
- package/lib/boardWaits.mjs +102 -0
- package/lib/book.mjs +659 -0
- package/lib/bridge.mjs +1 -1
- package/lib/cards.mjs +138 -28
- package/lib/cloudBoot.mjs +124 -0
- package/lib/commands.mjs +7 -2
- package/lib/context.mjs +18 -4
- package/lib/contractGate.mjs +377 -0
- package/lib/coordinator.mjs +149 -43
- package/lib/daemon.mjs +349 -28
- package/lib/desk.mjs +239 -0
- package/lib/desktop.mjs +106 -6
- package/lib/environments.mjs +237 -7
- package/lib/folders.mjs +2 -2
- package/lib/gitHost.mjs +174 -0
- package/lib/hookDialects.mjs +298 -0
- package/lib/landed.mjs +887 -0
- package/lib/live.mjs +284 -13
- package/lib/mcp-stdio.mjs +68 -2
- package/lib/members.json +16 -2
- package/lib/models.mjs +8 -8
- package/lib/museSession.mjs +16 -13
- package/lib/nativeHost.mjs +1 -1
- package/lib/neovim.mjs +126 -0
- package/lib/nudge.mjs +47 -16
- package/lib/ownMachines.mjs +343 -39
- package/lib/pairing.mjs +27 -10
- package/lib/paths.mjs +4 -0
- package/lib/personModel.mjs +89 -0
- package/lib/phones.mjs +66 -11
- package/lib/places.mjs +330 -10
- package/lib/readiness.mjs +135 -18
- package/lib/record.mjs +115 -0
- package/lib/runWorker.mjs +17 -2
- package/lib/runner.mjs +8 -4
- package/lib/sessionControl.mjs +377 -89
- 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 +284 -1
- package/lib/swarm.mjs +137 -21
- package/lib/t3.mjs +13 -4
- package/lib/taskCommands.mjs +36 -9
- package/lib/taskRuns.mjs +9 -2
- package/lib/team.json +255 -148
- package/lib/team.mjs +529 -188
- package/lib/threadLine.mjs +116 -0
- package/lib/threadRuns.mjs +75 -11
- package/lib/threadView.mjs +15 -2
- package/lib/worktrees.mjs +51 -13
- package/package.json +4 -4
- package/lib/modelTiers.json +0 -1183
- package/lib/modelTiers.mjs +0 -133
|
@@ -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/boardMirror.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { BOOK_MIRRORED } from "./book.mjs";
|
|
4
5
|
|
|
5
6
|
// Every computer of the person's swarm keeps the board. Home's is the board; each worker keeps a copy of it in its own
|
|
6
7
|
// board tables, so its agents still read the board when home is out of reach, and a worker standing in for home
|
|
@@ -15,6 +16,9 @@ import { randomUUID } from "node:crypto";
|
|
|
15
16
|
// writing them to a file first: a computer that was home before keeps its old board there.
|
|
16
17
|
// - Home tells connected workers when its board changed; a worker also catches up after each write it sent home and
|
|
17
18
|
// when home is back.
|
|
19
|
+
// - A computer that worked on a board of its own (a worker not confirmed yet, or one that was home) brings it to home
|
|
20
|
+
// once confirmed, before it follows home: home takes the rows it lacks and newer versions of its own, and nothing of
|
|
21
|
+
// home's is removed or overwritten otherwise (owner, October 22: a computer set up again must not lose the swarm).
|
|
18
22
|
|
|
19
23
|
/** A worker's call for home's changes (ownMachines.mjs answers it on home, never through the board's own commands). */
|
|
20
24
|
export const MIRROR_COMMAND = "board.mirror.since";
|
|
@@ -37,6 +41,15 @@ export const MIRRORED = {
|
|
|
37
41
|
board_observations: ["id"],
|
|
38
42
|
team_notes: ["id"],
|
|
39
43
|
board_files: ["id"],
|
|
44
|
+
// The onchain desk's book (book.mjs): personal, so it is carried only where the board is, sealed in its backup.
|
|
45
|
+
...BOOK_MIRRORED,
|
|
46
|
+
// The person's specialties, primaries, pins and appointments, and the benchmark specialties each agent was given
|
|
47
|
+
// (specialties.mjs), so the backup restores them and every computer's cards say the same.
|
|
48
|
+
board_specialty_choices: ["agent", "model", "specialty"],
|
|
49
|
+
board_specialty_primary: ["agent"],
|
|
50
|
+
board_pins: ["specialty"],
|
|
51
|
+
board_offices: ["office"],
|
|
52
|
+
board_specialty_benchmarks: ["agent", "model", "effort", "specialty"],
|
|
40
53
|
};
|
|
41
54
|
// A page stays well under what one sealed call carries (MAX_CT, src/shared/bridge/sealedWire.ts).
|
|
42
55
|
const PAGE_BYTES = 512 * 1024;
|
|
@@ -44,6 +57,8 @@ const FEED = "mirror-feed";
|
|
|
44
57
|
const CURSOR = "mirror-cursor";
|
|
45
58
|
// On a computer standing in as home: whose board it holds and where its own changes began.
|
|
46
59
|
const STAND_IN = "mirror-stand-in";
|
|
60
|
+
// On a computer that worked on a board of its own: it brings that board to home once it is confirmed.
|
|
61
|
+
const BRING = "mirror-bring";
|
|
47
62
|
|
|
48
63
|
const keyJson = (prefix, columns) =>
|
|
49
64
|
`json_array(${columns.map((column) => `${prefix}.${column}`).join(", ")})`;
|
|
@@ -129,7 +144,8 @@ export function createBoardMirror({
|
|
|
129
144
|
fetchPage = async () => null,
|
|
130
145
|
// On a worker that stood in: one page of what it wrote, merged on home; the answer says whether home took it.
|
|
131
146
|
pushPage = async () => null,
|
|
132
|
-
// Where this computer is: "home", "follow" (a worker),
|
|
147
|
+
// Where this computer is: "home", "follow" (a worker), "alone" (a worker not confirmed yet, on its own board), or
|
|
148
|
+
// "return" (home, while another computer stands in for it).
|
|
133
149
|
situation = () => "home",
|
|
134
150
|
// On a computer someone stood in for, once it has the stand-in's changes: Harness gives it home back.
|
|
135
151
|
reclaim = async () => false,
|
|
@@ -326,23 +342,24 @@ export function createBoardMirror({
|
|
|
326
342
|
/**
|
|
327
343
|
* Another computer's changes merged into this board, never overwriting newer work: a task by its version (every
|
|
328
344
|
* write bumps it), a lesson by when it changed, and any other row as the computer that changed it while standing in
|
|
329
|
-
* has it. Nothing here is taken out for being absent from the page.
|
|
345
|
+
* has it. Nothing here is taken out for being absent from the page. A board brought (`bring`) only adds: a row
|
|
346
|
+
* already here stays unless the brought one is a newer version, and nothing is removed.
|
|
330
347
|
*/
|
|
331
|
-
const merge = (page) => {
|
|
348
|
+
const merge = (page, { bring = false } = {}) => {
|
|
332
349
|
db.exec("begin immediate");
|
|
333
350
|
try {
|
|
334
351
|
for (const change of page.changes ?? []) {
|
|
335
352
|
const columns = MIRRORED[change.tbl];
|
|
336
353
|
if (!columns) continue;
|
|
337
354
|
if (change.gone) {
|
|
338
|
-
removeRow(change.tbl, change.gone);
|
|
355
|
+
if (!bring) removeRow(change.tbl, change.gone);
|
|
339
356
|
continue;
|
|
340
357
|
}
|
|
341
358
|
const here = db.get(
|
|
342
359
|
`select * from ${change.tbl} where ${whereKey(columns)}`,
|
|
343
360
|
columns.map((column) => change.row[column])
|
|
344
361
|
);
|
|
345
|
-
if (here && !newer(change.row, here)) continue;
|
|
362
|
+
if (here && !(bring ? newerVersion : newer)(change.row, here)) continue;
|
|
346
363
|
writeRow(change.tbl, change.row);
|
|
347
364
|
}
|
|
348
365
|
db.exec("commit");
|
|
@@ -366,6 +383,43 @@ export function createBoardMirror({
|
|
|
366
383
|
return String(incoming.updated_at) > String(here.updated_at);
|
|
367
384
|
return true;
|
|
368
385
|
};
|
|
386
|
+
// In a board brought to home, only a row that says when it changed may replace home's.
|
|
387
|
+
const newerVersion = (incoming, here) =>
|
|
388
|
+
("version" in incoming && "version" in here) ||
|
|
389
|
+
("updated_at" in incoming && "updated_at" in here)
|
|
390
|
+
? newer(incoming, here)
|
|
391
|
+
: false;
|
|
392
|
+
|
|
393
|
+
/** This computer works on a board of its own: once confirmed, it brings that board to home. */
|
|
394
|
+
const markBring = () => {
|
|
395
|
+
if (!meta(BRING))
|
|
396
|
+
setMeta(BRING, JSON.stringify({ at: new Date().toISOString() }));
|
|
397
|
+
};
|
|
398
|
+
// Confirmed now: the whole board here goes to home, a page at a time, before this computer follows home's.
|
|
399
|
+
const bring = async () => {
|
|
400
|
+
if (!meta(BRING)) return true;
|
|
401
|
+
let after = 0;
|
|
402
|
+
let pages = 0;
|
|
403
|
+
for (;;) {
|
|
404
|
+
const page = since({ after, feed: feed() });
|
|
405
|
+
if (!page.changes.length) break;
|
|
406
|
+
const answer = await pushPage(page, { bring: true });
|
|
407
|
+
if (answer?.ok !== true) {
|
|
408
|
+
log(
|
|
409
|
+
`this computer's board could not go to home yet: ${answer?.error || "home did not answer"}`
|
|
410
|
+
);
|
|
411
|
+
return false;
|
|
412
|
+
}
|
|
413
|
+
pages++;
|
|
414
|
+
after = page.to;
|
|
415
|
+
if (page.to >= page.head) break;
|
|
416
|
+
}
|
|
417
|
+
db.run("delete from board_meta where key = ?", [BRING]);
|
|
418
|
+
log(
|
|
419
|
+
`brought this computer's board to home${pages ? "" : " (it was empty)"}`
|
|
420
|
+
);
|
|
421
|
+
return true;
|
|
422
|
+
};
|
|
369
423
|
|
|
370
424
|
// A stand-in that is a worker again: home gets everything it wrote while standing in, before it follows home again.
|
|
371
425
|
const handOver = async () => {
|
|
@@ -432,9 +486,18 @@ export function createBoardMirror({
|
|
|
432
486
|
again = false;
|
|
433
487
|
const where = situation();
|
|
434
488
|
if (where === "home") continue;
|
|
489
|
+
if (where === "alone") {
|
|
490
|
+
markBring();
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
435
493
|
if (!(await handOver())) continue;
|
|
436
|
-
|
|
437
|
-
|
|
494
|
+
// Home for a while to a stand-in: it takes home back with the stand-in's changes, and brings nothing.
|
|
495
|
+
if (where === "return") {
|
|
496
|
+
await takeBack();
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
if (!(await bring())) continue;
|
|
500
|
+
await catchUp();
|
|
438
501
|
} while (again);
|
|
439
502
|
} catch (error) {
|
|
440
503
|
log(`the board copy could not catch up with home: ${error.message}`);
|
|
@@ -455,5 +518,6 @@ export function createBoardMirror({
|
|
|
455
518
|
head,
|
|
456
519
|
startStandIn,
|
|
457
520
|
standIn: standInRecord,
|
|
521
|
+
markBring,
|
|
458
522
|
};
|
|
459
523
|
}
|