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,523 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { BOOK_MIRRORED } from "./book.mjs";
|
|
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
|
|
7
|
+
// board tables, so its agents still read the board when home is out of reach, and a worker standing in for home
|
|
8
|
+
// already holds it (owner, October 21: "all nodes and home in a swarm stay in sync as much as possible", "home node is
|
|
9
|
+
// canonical", "entire swarm works off shared board").
|
|
10
|
+
//
|
|
11
|
+
// - Every computer numbers the changes to its board tables (triggers into board_changes: the latest change per row, so
|
|
12
|
+
// the log is never larger than the board). Its feed id names that numbering.
|
|
13
|
+
// - A worker asks home for the changes after the last one it applied (`since`, a sealed call through ownMachines.mjs)
|
|
14
|
+
// and applies each page in one transaction: a row as home has it now, or its removal. When home's feed is not the one
|
|
15
|
+
// it followed (the first time, or home moved), it starts over and, at the end, takes out the rows home does not have,
|
|
16
|
+
// writing them to a file first: a computer that was home before keeps its old board there.
|
|
17
|
+
// - Home tells connected workers when its board changed; a worker also catches up after each write it sent home and
|
|
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).
|
|
22
|
+
|
|
23
|
+
/** A worker's call for home's changes (ownMachines.mjs answers it on home, never through the board's own commands). */
|
|
24
|
+
export const MIRROR_COMMAND = "board.mirror.since";
|
|
25
|
+
/** A computer that stood in as home giving home back what it wrote meanwhile (answered on home the same way). */
|
|
26
|
+
export const MERGE_COMMAND = "board.mirror.merge";
|
|
27
|
+
|
|
28
|
+
/** The board tables every computer keeps the same as home's, by their primary key. */
|
|
29
|
+
export const MIRRORED = {
|
|
30
|
+
board_tasks: ["id"],
|
|
31
|
+
board_goals: ["goal_id"],
|
|
32
|
+
board_threads: ["task_id"],
|
|
33
|
+
board_messages: ["id"],
|
|
34
|
+
board_contributions: ["id"],
|
|
35
|
+
board_reactions: ["post_id", "agent"],
|
|
36
|
+
board_standing: ["agent"],
|
|
37
|
+
board_lessons: ["id"],
|
|
38
|
+
// What the person had forgotten or replaced, and what the dream keeps (swarmMemory.mjs), so every computer's agents
|
|
39
|
+
// recall the same.
|
|
40
|
+
board_memory_marks: ["contribution_id"],
|
|
41
|
+
board_observations: ["id"],
|
|
42
|
+
team_notes: ["id"],
|
|
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"],
|
|
53
|
+
};
|
|
54
|
+
// A page stays well under what one sealed call carries (MAX_CT, src/shared/bridge/sealedWire.ts).
|
|
55
|
+
const PAGE_BYTES = 512 * 1024;
|
|
56
|
+
const FEED = "mirror-feed";
|
|
57
|
+
const CURSOR = "mirror-cursor";
|
|
58
|
+
// On a computer standing in as home: whose board it holds and where its own changes began.
|
|
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";
|
|
62
|
+
|
|
63
|
+
const keyJson = (prefix, columns) =>
|
|
64
|
+
`json_array(${columns.map((column) => `${prefix}.${column}`).join(", ")})`;
|
|
65
|
+
|
|
66
|
+
/** The change log and its triggers, made once on every computer, and every row already there counted as changed. */
|
|
67
|
+
function install(db) {
|
|
68
|
+
db.exec(`create table if not exists board_changes (
|
|
69
|
+
seq integer primary key autoincrement,
|
|
70
|
+
tbl text not null,
|
|
71
|
+
pk text not null,
|
|
72
|
+
gone integer not null default 0,
|
|
73
|
+
unique(tbl, pk)
|
|
74
|
+
)`);
|
|
75
|
+
for (const [table, columns] of Object.entries(MIRRORED)) {
|
|
76
|
+
// A delete then an insert, never "insert or replace": the conflict rule of the statement that fired the trigger
|
|
77
|
+
// (a store's "insert or ignore") would override the trigger's own and drop the change (review, October 21).
|
|
78
|
+
const changed = (prefix, gone) =>
|
|
79
|
+
`delete from board_changes where tbl = '${table}' and pk = ${keyJson(prefix, columns)};
|
|
80
|
+
insert into board_changes(tbl, pk, gone) values('${table}', ${keyJson(prefix, columns)}, ${gone});`;
|
|
81
|
+
// Made again on every start, so a computer that has older triggers gets these.
|
|
82
|
+
db.exec(`
|
|
83
|
+
drop trigger if exists ${table}_mirror_insert;
|
|
84
|
+
drop trigger if exists ${table}_mirror_update;
|
|
85
|
+
drop trigger if exists ${table}_mirror_delete;
|
|
86
|
+
create trigger ${table}_mirror_insert after insert on ${table} begin ${changed("new", 0)} end;
|
|
87
|
+
create trigger ${table}_mirror_update after update on ${table} begin ${changed("new", 0)} end;
|
|
88
|
+
create trigger ${table}_mirror_delete after delete on ${table} begin ${changed("old", 1)} end;
|
|
89
|
+
`);
|
|
90
|
+
}
|
|
91
|
+
if (!db.get("select value from board_meta where key = ?", [FEED])) {
|
|
92
|
+
db.exec("begin immediate");
|
|
93
|
+
try {
|
|
94
|
+
for (const [table, columns] of Object.entries(MIRRORED))
|
|
95
|
+
db.run(
|
|
96
|
+
`insert or ignore into board_changes(tbl, pk, gone) select '${table}', ${keyJson(table, columns)}, 0 from ${table}`
|
|
97
|
+
);
|
|
98
|
+
db.run("insert into board_meta(key, value) values(?, ?)", [
|
|
99
|
+
FEED,
|
|
100
|
+
randomUUID(),
|
|
101
|
+
]);
|
|
102
|
+
db.exec("commit");
|
|
103
|
+
} catch (error) {
|
|
104
|
+
if (db.inTransaction) db.exec("rollback");
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const whereKey = (columns) =>
|
|
111
|
+
columns.map((column) => `${column} = ?`).join(" and ");
|
|
112
|
+
|
|
113
|
+
// A row as the wire carries it (JSON): bytes (a lesson's embedding) as base64, read back as bytes.
|
|
114
|
+
const onWire = (row) =>
|
|
115
|
+
Object.fromEntries(
|
|
116
|
+
Object.entries(row).map(([name, value]) => [
|
|
117
|
+
name,
|
|
118
|
+
value instanceof Uint8Array
|
|
119
|
+
? { $bytes: Buffer.from(value).toString("base64") }
|
|
120
|
+
: value,
|
|
121
|
+
])
|
|
122
|
+
);
|
|
123
|
+
const offWire = (value) =>
|
|
124
|
+
value && typeof value === "object" && typeof value.$bytes === "string"
|
|
125
|
+
? new Uint8Array(Buffer.from(value.$bytes, "base64"))
|
|
126
|
+
: value;
|
|
127
|
+
// The words search reads for the rows that have them (store.mjs keeps these beside the rows it writes).
|
|
128
|
+
const SEARCHED = {
|
|
129
|
+
board_contributions: {
|
|
130
|
+
table: "board_contributions_fts",
|
|
131
|
+
columns: (row) => ({ id: row.id, task_id: row.task_id, text: row.text }),
|
|
132
|
+
},
|
|
133
|
+
board_lessons: {
|
|
134
|
+
table: "board_lessons_fts",
|
|
135
|
+
columns: (row) => ({ id: row.id, text: row.text }),
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
export function createBoardMirror({
|
|
140
|
+
store,
|
|
141
|
+
log = () => {},
|
|
142
|
+
backups,
|
|
143
|
+
// On a worker: home's next page after a change (a sealed call through ownMachines.mjs), or null when unreachable.
|
|
144
|
+
fetchPage = async () => null,
|
|
145
|
+
// On a worker that stood in: one page of what it wrote, merged on home; the answer says whether home took it.
|
|
146
|
+
pushPage = async () => null,
|
|
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).
|
|
149
|
+
situation = () => "home",
|
|
150
|
+
// On a computer someone stood in for, once it has the stand-in's changes: Harness gives it home back.
|
|
151
|
+
reclaim = async () => false,
|
|
152
|
+
// Whether the computer standing in for this one can be reached (Harness has it connected, or a direct link does).
|
|
153
|
+
standInReachable = () => true,
|
|
154
|
+
// Rows another computer's changes brought in: what acts on the board hears of them.
|
|
155
|
+
merged = () => {},
|
|
156
|
+
}) {
|
|
157
|
+
const db = store.db;
|
|
158
|
+
install(db);
|
|
159
|
+
const meta = (key) =>
|
|
160
|
+
db.get("select value from board_meta where key = ?", [key])?.value ?? null;
|
|
161
|
+
const setMeta = (key, value) =>
|
|
162
|
+
db.run(
|
|
163
|
+
"insert into board_meta(key, value) values(?, ?) on conflict(key) do update set value = excluded.value",
|
|
164
|
+
[key, value]
|
|
165
|
+
);
|
|
166
|
+
const feed = () => meta(FEED);
|
|
167
|
+
// One row as the other computer has it: only the columns this board has (a newer version may have more), bytes read
|
|
168
|
+
// back from the wire, and its words kept where search reads them.
|
|
169
|
+
const writeRow = (table, row) => {
|
|
170
|
+
const known = columnsOf.get(table);
|
|
171
|
+
const names = Object.keys(row).filter((name) => known.has(name));
|
|
172
|
+
db.run(
|
|
173
|
+
`insert or replace into ${table}(${names.join(", ")}) values(${names.map(() => "?").join(", ")})`,
|
|
174
|
+
names.map((name) => offWire(row[name]))
|
|
175
|
+
);
|
|
176
|
+
const searched = SEARCHED[table];
|
|
177
|
+
if (!searched) return;
|
|
178
|
+
const words = searched.columns(row);
|
|
179
|
+
db.run(`delete from ${searched.table} where id = ?`, [words.id]);
|
|
180
|
+
db.run(
|
|
181
|
+
`insert into ${searched.table}(${Object.keys(words).join(", ")}) values(${Object.keys(
|
|
182
|
+
words
|
|
183
|
+
)
|
|
184
|
+
.map(() => "?")
|
|
185
|
+
.join(", ")})`,
|
|
186
|
+
Object.values(words)
|
|
187
|
+
);
|
|
188
|
+
};
|
|
189
|
+
const removeRow = (table, key) => {
|
|
190
|
+
const columns = MIRRORED[table];
|
|
191
|
+
db.run(`delete from ${table} where ${whereKey(columns)}`, key);
|
|
192
|
+
if (SEARCHED[table])
|
|
193
|
+
db.run(`delete from ${SEARCHED[table].table} where id = ?`, [key[0]]);
|
|
194
|
+
};
|
|
195
|
+
const head = () =>
|
|
196
|
+
Number(db.get("select max(seq) as seq from board_changes")?.seq ?? 0);
|
|
197
|
+
const columnsOf = new Map(
|
|
198
|
+
Object.keys(MIRRORED).map((table) => [
|
|
199
|
+
table,
|
|
200
|
+
new Set(db.all(`pragma table_info(${table})`).map((row) => row.name)),
|
|
201
|
+
])
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* On home: the changes after `after` in its feed, a page at a time, each a row as it is now or its removal. Asked for
|
|
206
|
+
* `standIn`, a stand-in's own changes since it stood in, for the computer it stood in for.
|
|
207
|
+
*/
|
|
208
|
+
const since = ({
|
|
209
|
+
after = 0,
|
|
210
|
+
feed: followed = null,
|
|
211
|
+
standIn = false,
|
|
212
|
+
} = {}) => {
|
|
213
|
+
const standing = standInRecord();
|
|
214
|
+
if (standIn && !standing)
|
|
215
|
+
return { ok: false, error: "this computer does not stand in" };
|
|
216
|
+
const asked = Math.max(0, Number(after) || 0);
|
|
217
|
+
// A feed numbered lower than a worker has followed (this board restored from a backup) is listed again whole.
|
|
218
|
+
const from =
|
|
219
|
+
followed === feed() && asked <= head()
|
|
220
|
+
? asked
|
|
221
|
+
: standIn
|
|
222
|
+
? standing.from
|
|
223
|
+
: 0;
|
|
224
|
+
const changes = [];
|
|
225
|
+
let bytes = 0;
|
|
226
|
+
let last = from;
|
|
227
|
+
for (const change of db.all(
|
|
228
|
+
"select seq, tbl, pk, gone from board_changes where seq > ? order by seq",
|
|
229
|
+
[from]
|
|
230
|
+
)) {
|
|
231
|
+
const columns = MIRRORED[change.tbl];
|
|
232
|
+
if (!columns) {
|
|
233
|
+
last = change.seq;
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
const key = JSON.parse(change.pk);
|
|
237
|
+
const row = change.gone
|
|
238
|
+
? null
|
|
239
|
+
: db.get(`select * from ${change.tbl} where ${whereKey(columns)}`, key);
|
|
240
|
+
const item = row
|
|
241
|
+
? { seq: change.seq, tbl: change.tbl, row: onWire(row) }
|
|
242
|
+
: { seq: change.seq, tbl: change.tbl, gone: key };
|
|
243
|
+
const size = Buffer.byteLength(JSON.stringify(item));
|
|
244
|
+
if (changes.length && bytes + size > PAGE_BYTES) break;
|
|
245
|
+
changes.push(item);
|
|
246
|
+
bytes += size;
|
|
247
|
+
last = change.seq;
|
|
248
|
+
}
|
|
249
|
+
return {
|
|
250
|
+
ok: true,
|
|
251
|
+
feed: feed(),
|
|
252
|
+
from,
|
|
253
|
+
to: last,
|
|
254
|
+
head: head(),
|
|
255
|
+
changes,
|
|
256
|
+
};
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
// Keys seen during a start-over, to take out what home does not have once it is complete.
|
|
260
|
+
let starting = null;
|
|
261
|
+
|
|
262
|
+
/** On a worker: one page from home, applied in one transaction. Returns whether more remain. */
|
|
263
|
+
const apply = (page) => {
|
|
264
|
+
// A page from the start of home's feed lists the whole board: what is not in it goes once the listing ends.
|
|
265
|
+
if (page.from === 0)
|
|
266
|
+
starting ??= new Map(
|
|
267
|
+
Object.keys(MIRRORED).map((table) => [table, new Set()])
|
|
268
|
+
);
|
|
269
|
+
db.exec("begin immediate");
|
|
270
|
+
try {
|
|
271
|
+
for (const change of page.changes) {
|
|
272
|
+
const columns = MIRRORED[change.tbl];
|
|
273
|
+
if (!columns) continue;
|
|
274
|
+
if (change.gone) {
|
|
275
|
+
removeRow(change.tbl, change.gone);
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
writeRow(change.tbl, change.row);
|
|
279
|
+
starting
|
|
280
|
+
?.get(change.tbl)
|
|
281
|
+
?.add(JSON.stringify(columns.map((column) => change.row[column])));
|
|
282
|
+
}
|
|
283
|
+
const done = page.to >= page.head;
|
|
284
|
+
if (done && starting) sweep(starting);
|
|
285
|
+
setMeta(
|
|
286
|
+
CURSOR,
|
|
287
|
+
JSON.stringify({
|
|
288
|
+
feed: page.feed,
|
|
289
|
+
seq: page.to,
|
|
290
|
+
at: new Date().toISOString(),
|
|
291
|
+
// A listing cut off by a restart starts over: the keys it had seen were in memory.
|
|
292
|
+
...(starting && !done ? { listing: true } : {}),
|
|
293
|
+
})
|
|
294
|
+
);
|
|
295
|
+
db.exec("commit");
|
|
296
|
+
if (done) starting = null;
|
|
297
|
+
return !done;
|
|
298
|
+
} catch (error) {
|
|
299
|
+
if (db.inTransaction) db.exec("rollback");
|
|
300
|
+
throw error;
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
// After a start-over: the rows home does not have go, written first to a file in Harness's folder.
|
|
305
|
+
const sweep = (seen) => {
|
|
306
|
+
const extra = [];
|
|
307
|
+
for (const [table, columns] of Object.entries(MIRRORED))
|
|
308
|
+
for (const row of db.all(`select * from ${table}`)) {
|
|
309
|
+
const key = JSON.stringify(columns.map((column) => row[column]));
|
|
310
|
+
if (!seen.get(table).has(key)) extra.push({ table, row });
|
|
311
|
+
}
|
|
312
|
+
if (!extra.length) return;
|
|
313
|
+
fs.mkdirSync(backups, { recursive: true });
|
|
314
|
+
const file = path.join(
|
|
315
|
+
backups,
|
|
316
|
+
`board-before-mirror-${new Date().toISOString().replace(/[:.]/g, "-")}.jsonl`
|
|
317
|
+
);
|
|
318
|
+
fs.writeFileSync(
|
|
319
|
+
file,
|
|
320
|
+
extra
|
|
321
|
+
.map(({ table, row }) => JSON.stringify({ table, row: onWire(row) }))
|
|
322
|
+
.join("\n") + "\n"
|
|
323
|
+
);
|
|
324
|
+
for (const { table, row } of extra)
|
|
325
|
+
removeRow(
|
|
326
|
+
table,
|
|
327
|
+
MIRRORED[table].map((column) => row[column])
|
|
328
|
+
);
|
|
329
|
+
log(
|
|
330
|
+
`the board here now follows home's: ${extra.length} row(s) it did not have are kept in ${file}`
|
|
331
|
+
);
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
/** The last change applied from home, and when: what a read from the copy says it is as of. */
|
|
335
|
+
const cursor = () => JSON.parse(meta(CURSOR) || "null");
|
|
336
|
+
const standInRecord = () => JSON.parse(meta(STAND_IN) || "null");
|
|
337
|
+
|
|
338
|
+
/** This computer stands in as home for another: its own changes from here on go back to that computer. */
|
|
339
|
+
const startStandIn = (forId) =>
|
|
340
|
+
setMeta(STAND_IN, JSON.stringify({ for: forId, from: head() }));
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Another computer's changes merged into this board, never overwriting newer work: a task by its version (every
|
|
344
|
+
* write bumps it), a lesson by when it changed, and any other row as the computer that changed it while standing in
|
|
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.
|
|
347
|
+
*/
|
|
348
|
+
const merge = (page, { bring = false } = {}) => {
|
|
349
|
+
db.exec("begin immediate");
|
|
350
|
+
try {
|
|
351
|
+
for (const change of page.changes ?? []) {
|
|
352
|
+
const columns = MIRRORED[change.tbl];
|
|
353
|
+
if (!columns) continue;
|
|
354
|
+
if (change.gone) {
|
|
355
|
+
if (!bring) removeRow(change.tbl, change.gone);
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
const here = db.get(
|
|
359
|
+
`select * from ${change.tbl} where ${whereKey(columns)}`,
|
|
360
|
+
columns.map((column) => change.row[column])
|
|
361
|
+
);
|
|
362
|
+
if (here && !(bring ? newerVersion : newer)(change.row, here)) continue;
|
|
363
|
+
writeRow(change.tbl, change.row);
|
|
364
|
+
}
|
|
365
|
+
db.exec("commit");
|
|
366
|
+
merged();
|
|
367
|
+
return { ok: true };
|
|
368
|
+
} catch (error) {
|
|
369
|
+
if (db.inTransaction) db.exec("rollback");
|
|
370
|
+
throw error;
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
// A row with no version or time of change (a thread's state, a goal's sessions, a reaction) is taken from the
|
|
374
|
+
// computer that sent it: only rows it changed while standing in come in a merge (review, October 21: these edits were
|
|
375
|
+
// dropped).
|
|
376
|
+
const newer = (incoming, here) => {
|
|
377
|
+
if ("version" in incoming && "version" in here) {
|
|
378
|
+
if (Number(incoming.version) !== Number(here.version))
|
|
379
|
+
return Number(incoming.version) > Number(here.version);
|
|
380
|
+
return String(incoming.updated_at) > String(here.updated_at);
|
|
381
|
+
}
|
|
382
|
+
if ("updated_at" in incoming && "updated_at" in here)
|
|
383
|
+
return String(incoming.updated_at) > String(here.updated_at);
|
|
384
|
+
return true;
|
|
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
|
+
};
|
|
423
|
+
|
|
424
|
+
// A stand-in that is a worker again: home gets everything it wrote while standing in, before it follows home again.
|
|
425
|
+
const handOver = async () => {
|
|
426
|
+
const standing = standInRecord();
|
|
427
|
+
if (!standing) return true;
|
|
428
|
+
let after = standing.from;
|
|
429
|
+
for (;;) {
|
|
430
|
+
const page = since({ after, feed: feed() });
|
|
431
|
+
if (!page.changes.length) break;
|
|
432
|
+
const answer = await pushPage(page);
|
|
433
|
+
if (answer?.ok !== true) return false;
|
|
434
|
+
after = page.to;
|
|
435
|
+
if (page.to >= page.head) break;
|
|
436
|
+
}
|
|
437
|
+
db.run("delete from board_meta where key = ?", [STAND_IN]);
|
|
438
|
+
log(`gave home what this computer wrote while it stood in`);
|
|
439
|
+
return true;
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
// Home again after another computer stood in for it: that computer's changes merged here, then home taken back.
|
|
443
|
+
const takeBack = async () => {
|
|
444
|
+
let after = null;
|
|
445
|
+
let followed = null;
|
|
446
|
+
for (;;) {
|
|
447
|
+
const page = await fetchPage({ standIn: true, after, feed: followed });
|
|
448
|
+
if (!page?.ok || !Array.isArray(page.changes)) {
|
|
449
|
+
// The stand-in is away too, or kept no record of standing in: home is taken back now, and the stand-in hands
|
|
450
|
+
// its changes over when it is back (handOver), rather than the swarm having no home (review, October 21).
|
|
451
|
+
if (page?.ok === false || !standInReachable()) break;
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
merge(page);
|
|
455
|
+
after = page.to;
|
|
456
|
+
followed = page.feed;
|
|
457
|
+
if (page.to >= page.head) break;
|
|
458
|
+
}
|
|
459
|
+
if (await reclaim()) log("took home back with the stand-in's changes");
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
// One catch-up at a time; asked again while it runs, it runs once more after.
|
|
463
|
+
let running = null;
|
|
464
|
+
let again = false;
|
|
465
|
+
const catchUp = async () => {
|
|
466
|
+
for (;;) {
|
|
467
|
+
const followed = cursor();
|
|
468
|
+
const fresh = followed && !(followed.listing && !starting);
|
|
469
|
+
const page = await fetchPage({
|
|
470
|
+
after: fresh ? followed.seq : 0,
|
|
471
|
+
feed: fresh ? followed.feed : null,
|
|
472
|
+
});
|
|
473
|
+
if (!page?.ok || !Array.isArray(page.changes)) return;
|
|
474
|
+
if (!apply(page)) return;
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
/** On a worker: take home's changes until this copy is up to date. */
|
|
478
|
+
const sync = () => {
|
|
479
|
+
if (running) {
|
|
480
|
+
again = true;
|
|
481
|
+
return running;
|
|
482
|
+
}
|
|
483
|
+
running = (async () => {
|
|
484
|
+
try {
|
|
485
|
+
do {
|
|
486
|
+
again = false;
|
|
487
|
+
const where = situation();
|
|
488
|
+
if (where === "home") continue;
|
|
489
|
+
if (where === "alone") {
|
|
490
|
+
markBring();
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
if (!(await handOver())) continue;
|
|
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();
|
|
501
|
+
} while (again);
|
|
502
|
+
} catch (error) {
|
|
503
|
+
log(`the board copy could not catch up with home: ${error.message}`);
|
|
504
|
+
} finally {
|
|
505
|
+
running = null;
|
|
506
|
+
}
|
|
507
|
+
})();
|
|
508
|
+
return running;
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
return {
|
|
512
|
+
since,
|
|
513
|
+
apply,
|
|
514
|
+
merge,
|
|
515
|
+
sync,
|
|
516
|
+
cursor,
|
|
517
|
+
feed,
|
|
518
|
+
head,
|
|
519
|
+
startStandIn,
|
|
520
|
+
standIn: standInRecord,
|
|
521
|
+
markBring,
|
|
522
|
+
};
|
|
523
|
+
}
|