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
package/lib/ownMachines.mjs
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
import { randomBytes } from "node:crypto";
|
|
3
|
+
import { randomBytes, randomUUID } from "node:crypto";
|
|
4
4
|
import { documentText, ensureDir, isDocument, mimeFor } from "./files.mjs";
|
|
5
5
|
import { FILE_PART_BYTES } from "./boardFiles.mjs";
|
|
6
6
|
import { fingerprint } from "./sealed.mjs";
|
|
7
7
|
import {
|
|
8
|
+
isPairCode,
|
|
8
9
|
joinProof,
|
|
9
10
|
newJoinCode,
|
|
10
11
|
newRequestId,
|
|
@@ -12,20 +13,38 @@ import {
|
|
|
12
13
|
openAnswer,
|
|
13
14
|
openCall,
|
|
14
15
|
openNotice,
|
|
16
|
+
pairClaim,
|
|
15
17
|
pairKey,
|
|
16
18
|
sameProof,
|
|
17
19
|
sealAnswer,
|
|
18
20
|
sealCall,
|
|
19
21
|
sealNotice,
|
|
22
|
+
sealAsk,
|
|
23
|
+
openAsk,
|
|
24
|
+
sealAskAnswer,
|
|
25
|
+
openAskAnswer,
|
|
20
26
|
} from "./machineSeal.mjs";
|
|
21
27
|
import { findMachine, goesHome, queueable } from "./machineRoutes.mjs";
|
|
28
|
+
import { MERGE_COMMAND, MIRROR_COMMAND } from "./boardMirror.mjs";
|
|
29
|
+
import { member } from "./members.mjs";
|
|
30
|
+
|
|
31
|
+
// A worker's key for a direct link, swapped for home's over the sealed channel (directLink.mjs).
|
|
32
|
+
const DIRECT_COMMAND = "machine.direct";
|
|
33
|
+
// The person's model and effort for an agent live on the computer that runs it (models.mjs reads its own store), so
|
|
34
|
+
// the buddy's Swarm tab reads and sets them on each computer: a worker asks home, home asks the worker that has it.
|
|
35
|
+
const MODELS_COMMAND = "machine.models";
|
|
36
|
+
const MODEL_COMMAND = "machine.model";
|
|
37
|
+
// An agent that runs itself (Hermes, OpenClaw) is shown on a computer it called the board from this recently.
|
|
38
|
+
const SELF_RUN_SEEN_MS = 7 * 24 * 60 * 60_000;
|
|
22
39
|
|
|
23
40
|
// The person's other computers (one swarm across them). One computer holds the board ("home"); every other one is a
|
|
24
41
|
// worker: its agents' board calls go to home through Harness, sealed with the key the two computers share
|
|
25
42
|
// (machineSeal.mjs), and home answers them on its board as that agent on that computer. Harness says which computers
|
|
26
43
|
// there are and carries what they send; a computer trusts another only once the person confirmed it on home with the
|
|
27
44
|
// worker's join code. A worker keeps what its agents write while home is out of reach and sends it when home is back.
|
|
28
|
-
//
|
|
45
|
+
// A worker not confirmed yet (new, or its home was set up again with a new key) works on a board of its own until the
|
|
46
|
+
// person types its code on home, then brings that board to home (boardMirror.mjs bring): it is never a computer that
|
|
47
|
+
// cannot work (owner, October 22: "if this is how a worker behaves its fundamentally broken").
|
|
29
48
|
|
|
30
49
|
// Set on a board request by this module alone: the computer the call came from. JSON cannot carry it, so no caller
|
|
31
50
|
// names a computer it is not on.
|
|
@@ -33,6 +52,8 @@ export const FROM_MACHINE = Symbol("the computer a board call came from");
|
|
|
33
52
|
|
|
34
53
|
// Wrong join codes a worker takes before it makes a new one: Harness carries the guesses and must not get many.
|
|
35
54
|
const JOIN_TRIES = 5;
|
|
55
|
+
// How long home keeps trying a code typed for a computer that is not online yet: as long as its pairing code lasts.
|
|
56
|
+
const ADD_WAIT_MS = 10 * 60_000;
|
|
36
57
|
const MB = 1024 * 1024;
|
|
37
58
|
// What get_file shows inline on a worker, as on home (boardFileCommands.mjs).
|
|
38
59
|
const INLINE_IMAGE_BYTES = 3.75 * MB;
|
|
@@ -57,11 +78,41 @@ export function createOwnMachines({
|
|
|
57
78
|
folders = () => [],
|
|
58
79
|
repositoryOf = () => null,
|
|
59
80
|
homeBack = () => {},
|
|
81
|
+
// On home: a worker's call for the board's changes (boardMirror.mjs since).
|
|
82
|
+
answerMirror = null,
|
|
83
|
+
// On home: a stand-in's changes, merged (boardMirror.mjs merge).
|
|
84
|
+
answerMerge = null,
|
|
85
|
+
// The direct links between the person's computers (directLink.mjs); null keeps every message to Harness.
|
|
86
|
+
directLink = null,
|
|
87
|
+
// How long home must be unreachable on every route before this worker stands in as home; null: it never does (the
|
|
88
|
+
// owner sets it once the gaps are measured, owner October 21: "measure first").
|
|
89
|
+
standInAfterMs = null,
|
|
90
|
+
// This computer stood in for that one: its own changes from now on go back to it (boardMirror.mjs startStandIn).
|
|
91
|
+
onStoodIn = () => {},
|
|
92
|
+
// Another computer stands in for this one: it takes home back (boardMirror.mjs takeBack).
|
|
93
|
+
returning = () => {},
|
|
94
|
+
// This computer works on a board of its own (a worker not confirmed yet), or stopped being home: what it holds goes
|
|
95
|
+
// to home once it is confirmed (boardMirror.mjs bring).
|
|
96
|
+
onOwnBoard = () => {},
|
|
97
|
+
// This computer's own Harness windows, asked a question for their screen history (phones.mjs ask).
|
|
98
|
+
askOwnWindows = async () => ({
|
|
99
|
+
ok: false,
|
|
100
|
+
code: "unpaired",
|
|
101
|
+
error: "no Harness window is linked to this computer",
|
|
102
|
+
}),
|
|
103
|
+
// This computer's own agents' models, read or one set, as the person (swarm.mjs board.swarm.models/model).
|
|
104
|
+
ownModels = async () => ({
|
|
105
|
+
ok: false,
|
|
106
|
+
error: "this computer cannot list its agents' models",
|
|
107
|
+
}),
|
|
60
108
|
}) {
|
|
61
109
|
// On home: each worker's folders and the repository each holds, as it said them.
|
|
62
110
|
const infos = new Map();
|
|
63
|
-
// The account's other computers as Harness last listed them: { id, label, role, connected, agents, publicKey
|
|
111
|
+
// The account's other computers as Harness last listed them: { id, label, role, connected, agents, publicKey,
|
|
112
|
+
// standInFor }, and this one as Harness lists it.
|
|
64
113
|
let siblings = [];
|
|
114
|
+
let selfListed = null;
|
|
115
|
+
let standInTimer = null;
|
|
65
116
|
let flushing = false;
|
|
66
117
|
// What home last heard of this computer's folders, false until it heard them on this connection.
|
|
67
118
|
let toldHome = false;
|
|
@@ -73,6 +124,16 @@ export function createOwnMachines({
|
|
|
73
124
|
const home = homeListed();
|
|
74
125
|
return home ? store.ownMachine(home.id) : null;
|
|
75
126
|
};
|
|
127
|
+
// Whether this computer was confirmed with another of the person's computers (it joined a home, or confirmed a
|
|
128
|
+
// worker): such a computer never takes home from one that is away (src/server/bridge/machines.ts takesHome).
|
|
129
|
+
const confirmedWithAnother = () =>
|
|
130
|
+
Boolean(store.meta("joined-home")) || store.ownMachines().length > 0;
|
|
131
|
+
// Whether this worker holds home's key. Just started, before Harness lists the computers, a worker that was ever
|
|
132
|
+
// confirmed counts as confirmed, so it never writes on its copy of home's board.
|
|
133
|
+
const trusted = () =>
|
|
134
|
+
homeListed() ? Boolean(pinnedHome()) : confirmedWithAnother();
|
|
135
|
+
/** A worker not confirmed into the swarm yet: its agents work on this computer's own board meanwhile. */
|
|
136
|
+
const actsAlone = () => !isHome() && !trusted();
|
|
76
137
|
const labelOf = (id) =>
|
|
77
138
|
id === machine.id
|
|
78
139
|
? machine.label
|
|
@@ -82,12 +143,24 @@ export function createOwnMachines({
|
|
|
82
143
|
const keyFor = (peerKey, workerId) =>
|
|
83
144
|
pairKey(identity().privateKey, peerKey, workerId);
|
|
84
145
|
|
|
146
|
+
// A message for another of the person's computers: over the direct link when there is one and it answers, through
|
|
147
|
+
// Harness otherwise. Sealed the same either way.
|
|
148
|
+
const relayTo = async (input) => {
|
|
149
|
+
const peer = input.to === "home" ? homeListed()?.id : input.to;
|
|
150
|
+
const direct =
|
|
151
|
+
peer && directLink
|
|
152
|
+
? await directLink.request(peer, { kind: input.kind, body: input.body })
|
|
153
|
+
: null;
|
|
154
|
+
return direct ?? ask("machine_relay", input);
|
|
155
|
+
};
|
|
156
|
+
|
|
85
157
|
// ---- who this computer is, and the others ----
|
|
86
158
|
|
|
87
159
|
const hello = () => ({
|
|
88
160
|
machineId: machine.id,
|
|
89
161
|
label: machine.label,
|
|
90
162
|
...(machine.role ? { role: machine.role } : {}),
|
|
163
|
+
joined: confirmedWithAnother(),
|
|
91
164
|
});
|
|
92
165
|
|
|
93
166
|
/** Harness's answer to this computer's hello: its role on the account. */
|
|
@@ -97,8 +170,10 @@ export function createOwnMachines({
|
|
|
97
170
|
log(
|
|
98
171
|
`this computer is now ${given.role === "home" ? "home: it holds the board" : "a worker: its agents reach the board on your home computer"}`
|
|
99
172
|
);
|
|
173
|
+
const leftHome = machine.role === "home" && given.role === "worker";
|
|
100
174
|
machine.role = given.role;
|
|
101
175
|
saveMachine(machine);
|
|
176
|
+
if (leftHome) onOwnBoard();
|
|
102
177
|
changed();
|
|
103
178
|
};
|
|
104
179
|
|
|
@@ -107,6 +182,8 @@ export function createOwnMachines({
|
|
|
107
182
|
if (!Array.isArray(message?.machines)) return;
|
|
108
183
|
// Harness lists an offline computer's agents as none: they are what it had when it was last on.
|
|
109
184
|
const before = new Map(siblings.map((sibling) => [sibling.id, sibling]));
|
|
185
|
+
selfListed =
|
|
186
|
+
message.machines.find((listed) => listed?.id === machine.id) ?? null;
|
|
110
187
|
siblings = message.machines
|
|
111
188
|
.filter((listed) => listed && listed.id !== machine.id)
|
|
112
189
|
.map((listed) => ({
|
|
@@ -120,8 +197,15 @@ export function createOwnMachines({
|
|
|
120
197
|
: (before.get(String(listed.id))?.agents ?? []),
|
|
121
198
|
publicKey:
|
|
122
199
|
typeof listed.publicKey === "string" ? listed.publicKey : null,
|
|
200
|
+
standInFor:
|
|
201
|
+
typeof listed.standInFor === "string" ? listed.standInFor : null,
|
|
123
202
|
}));
|
|
124
203
|
changed();
|
|
204
|
+
retryAdding();
|
|
205
|
+
watchHome();
|
|
206
|
+
if (actsAlone()) void workAlone();
|
|
207
|
+
// Someone stood in for this computer: it takes home back, at once if the stand-in is away too (boardMirror.mjs).
|
|
208
|
+
if (someoneStandsIn()) returning();
|
|
125
209
|
if (!homeListed()?.connected) toldHome = false;
|
|
126
210
|
else if (!isHome()) void flush();
|
|
127
211
|
};
|
|
@@ -138,6 +222,163 @@ export function createOwnMachines({
|
|
|
138
222
|
return { ok: true, label: name };
|
|
139
223
|
};
|
|
140
224
|
|
|
225
|
+
// ---- standing in while home is unreachable, and home taking its place back ----
|
|
226
|
+
|
|
227
|
+
/** The computer this one stands in for as home, or null. */
|
|
228
|
+
const standingIn = () =>
|
|
229
|
+
isHome() && typeof selfListed?.standInFor === "string"
|
|
230
|
+
? selfListed.standInFor
|
|
231
|
+
: null;
|
|
232
|
+
/** Whether another computer stands in as home for this one. */
|
|
233
|
+
const someoneStandsIn = () => homeListed()?.standInFor === machine.id;
|
|
234
|
+
const homeUnreachable = () => {
|
|
235
|
+
const home = homeListed();
|
|
236
|
+
if (!home || home.connected || home.standInFor) return false;
|
|
237
|
+
return !(directLink?.status() ?? []).some(
|
|
238
|
+
(link) => link.id === home.id && link.linked
|
|
239
|
+
);
|
|
240
|
+
};
|
|
241
|
+
// Home gone on every route (Harness lists it offline and no direct link answers): after the person's deadline this
|
|
242
|
+
// worker asks Harness to let it stand in. Home back on any route first, nothing happens.
|
|
243
|
+
const watchHome = () => {
|
|
244
|
+
const waiting =
|
|
245
|
+
standInAfterMs !== null &&
|
|
246
|
+
!isHome() &&
|
|
247
|
+
Boolean(pinnedHome()) &&
|
|
248
|
+
homeUnreachable();
|
|
249
|
+
if (!waiting) {
|
|
250
|
+
clearTimeout(standInTimer);
|
|
251
|
+
standInTimer = null;
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
standInTimer ??= setTimeout(
|
|
255
|
+
() => {
|
|
256
|
+
standInTimer = null;
|
|
257
|
+
if (!isHome() && pinnedHome() && homeUnreachable()) void standIn();
|
|
258
|
+
},
|
|
259
|
+
// A timer longer than this fires at once (Node's limit, about 24.8 days).
|
|
260
|
+
Math.min(standInAfterMs, 2 ** 31 - 1)
|
|
261
|
+
);
|
|
262
|
+
};
|
|
263
|
+
const standIn = async () => {
|
|
264
|
+
const answer = await ask("machine_role", {
|
|
265
|
+
role: "home",
|
|
266
|
+
standIn: true,
|
|
267
|
+
}).catch(() => null);
|
|
268
|
+
if (answer?.ok !== true || typeof answer.standInFor !== "string") return;
|
|
269
|
+
onStoodIn(answer.standInFor);
|
|
270
|
+
machine.role = "home";
|
|
271
|
+
saveMachine(machine);
|
|
272
|
+
changed();
|
|
273
|
+
await applyKept(answer.standInFor);
|
|
274
|
+
log(
|
|
275
|
+
`your home computer${labelOf(answer.standInFor) ? ` (${labelOf(answer.standInFor)})` : ""} is unreachable; this computer stands in as home until it is back`
|
|
276
|
+
);
|
|
277
|
+
};
|
|
278
|
+
// What this computer kept for home before standing in (posts, reports) is home's work: done here now, on the board it
|
|
279
|
+
// holds, so it goes back with the rest of what it wrote (review, October 21: it waited the whole stand-in).
|
|
280
|
+
const applyKept = async (formerHome) => {
|
|
281
|
+
const pinned = store.ownMachine(formerHome);
|
|
282
|
+
const kept = outbox();
|
|
283
|
+
if (!pinned || !kept.length) return;
|
|
284
|
+
const key = await keyFor(pinned.public_key, machine.id);
|
|
285
|
+
for (const entry of kept) {
|
|
286
|
+
const opened = await openCall(key, entry.envelope, {
|
|
287
|
+
from: machine.id,
|
|
288
|
+
firstSeen: () => true,
|
|
289
|
+
});
|
|
290
|
+
if (opened.ok)
|
|
291
|
+
await settle(() =>
|
|
292
|
+
boardCommand({
|
|
293
|
+
...stripLocal(opened.call),
|
|
294
|
+
command: opened.call.command,
|
|
295
|
+
})
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
keepOut([]);
|
|
299
|
+
log(
|
|
300
|
+
`did ${kept.length} kept write(s) on the board this computer now holds`
|
|
301
|
+
);
|
|
302
|
+
};
|
|
303
|
+
// A worker whose home is one it does not trust (its home was set up again, or it is new): what it kept for the home
|
|
304
|
+
// it trusted is done on its own board, which it brings to home once confirmed.
|
|
305
|
+
let alone = false;
|
|
306
|
+
const workAlone = async () => {
|
|
307
|
+
if (alone) return;
|
|
308
|
+
alone = true;
|
|
309
|
+
try {
|
|
310
|
+
onOwnBoard();
|
|
311
|
+
const formerHome = store.meta("joined-home");
|
|
312
|
+
if (formerHome && formerHome !== homeListed()?.id)
|
|
313
|
+
await applyKept(formerHome);
|
|
314
|
+
} catch (error) {
|
|
315
|
+
log(`the writes kept for home could not be done here: ${error.message}`);
|
|
316
|
+
} finally {
|
|
317
|
+
alone = false;
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
/** Home taking its place back from the computer that stood in, once it has that computer's changes. */
|
|
322
|
+
const reclaim = async () => {
|
|
323
|
+
// Only the computer that stood in may merge its writes here afterwards (answerCall).
|
|
324
|
+
const standing = homeListed()?.id;
|
|
325
|
+
if (standing) store.meta("stood-in-by", standing);
|
|
326
|
+
const answer = await ask("machine_role", {
|
|
327
|
+
role: "home",
|
|
328
|
+
reclaim: true,
|
|
329
|
+
}).catch(() => null);
|
|
330
|
+
if (answer?.ok !== true) return false;
|
|
331
|
+
machine.role = "home";
|
|
332
|
+
saveMachine(machine);
|
|
333
|
+
changed();
|
|
334
|
+
return true;
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
/** Uninstalled: Harness takes this computer off the account, and the pairing it connects with stops working. */
|
|
338
|
+
const leave = async () => {
|
|
339
|
+
const answer = await ask("machine_leave", {}).catch((error) => ({
|
|
340
|
+
ok: false,
|
|
341
|
+
error: error.message,
|
|
342
|
+
}));
|
|
343
|
+
return answer?.left === true
|
|
344
|
+
? { ok: true }
|
|
345
|
+
: { ok: false, error: answer?.error || "Harness did not answer" };
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
/** The person takes another of their computers off the account from the Swarm map. Never home. */
|
|
349
|
+
const remove = async (id) => {
|
|
350
|
+
const target = String(id || "");
|
|
351
|
+
const listed = siblings.find((sibling) => sibling.id === target);
|
|
352
|
+
if (listed?.role === "home")
|
|
353
|
+
return {
|
|
354
|
+
ok: false,
|
|
355
|
+
error: "Home keeps the board: make another computer home first.",
|
|
356
|
+
};
|
|
357
|
+
const answer = await ask("machine_remove", { machineId: target }).catch(
|
|
358
|
+
(error) => ({ ok: false, error: error.message })
|
|
359
|
+
);
|
|
360
|
+
if (answer?.removed !== true)
|
|
361
|
+
return { ok: false, error: answer?.error || "Harness did not answer" };
|
|
362
|
+
removed({ machineId: target });
|
|
363
|
+
return { ok: true };
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
/** Harness says a computer was taken off the account: its key is forgotten here, so it must be added again. */
|
|
367
|
+
const removed = (message) => {
|
|
368
|
+
const id = String(message?.machineId || "");
|
|
369
|
+
if (id === machine.id) {
|
|
370
|
+
log(
|
|
371
|
+
"this computer was removed from your swarm on another of your computers: run npx tryharness setup to add it again"
|
|
372
|
+
);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
if (!store.ownMachine(id)) return;
|
|
376
|
+
store.unpinOwnMachine(id);
|
|
377
|
+
siblings = siblings.filter((sibling) => sibling.id !== id);
|
|
378
|
+
log(`${labelOf(id) ?? id} was removed from your swarm`);
|
|
379
|
+
changed();
|
|
380
|
+
};
|
|
381
|
+
|
|
141
382
|
/** The person makes this computer home. The board stays on the computer that had it. */
|
|
142
383
|
const makeHome = async () => {
|
|
143
384
|
const answer = await ask("machine_role", { role: "home" });
|
|
@@ -151,7 +392,16 @@ export function createOwnMachines({
|
|
|
151
392
|
|
|
152
393
|
// ---- the join: the worker's code typed on home pins each computer's key on the other ----
|
|
153
394
|
|
|
395
|
+
// Setup's code becomes this computer's join code, in place of any made before (pairing.mjs keepJoinCode).
|
|
396
|
+
const adoptSetupCode = () => {
|
|
397
|
+
if (!machine.joinCode) return;
|
|
398
|
+
const code = normalizeJoinCode(machine.joinCode);
|
|
399
|
+
if (code) store.meta("machine-join", JSON.stringify({ code, wrong: 0 }));
|
|
400
|
+
machine.joinCode = null;
|
|
401
|
+
saveMachine(machine);
|
|
402
|
+
};
|
|
154
403
|
const joinState = () => {
|
|
404
|
+
adoptSetupCode();
|
|
155
405
|
try {
|
|
156
406
|
return JSON.parse(store.meta("machine-join") || "null");
|
|
157
407
|
} catch {
|
|
@@ -173,28 +423,38 @@ export function createOwnMachines({
|
|
|
173
423
|
);
|
|
174
424
|
};
|
|
175
425
|
|
|
426
|
+
const notHome = () => ({
|
|
427
|
+
ok: false,
|
|
428
|
+
error: `Add a computer on your home computer${homeListed() ? ` (${homeListed().label})` : ""}, not here.`,
|
|
429
|
+
});
|
|
430
|
+
const badCode = () => ({
|
|
431
|
+
ok: false,
|
|
432
|
+
error:
|
|
433
|
+
"That is not a code from the computer you are adding. It looks like ABCD-EFGH-JKLM-NPQR.",
|
|
434
|
+
});
|
|
435
|
+
|
|
176
436
|
/** On home: the person typed a worker's join code. */
|
|
177
437
|
const confirm = async (typed) => {
|
|
178
|
-
if (!isHome())
|
|
179
|
-
return {
|
|
180
|
-
ok: false,
|
|
181
|
-
error: `Confirm a computer on your home computer${homeListed() ? ` (${homeListed().label})` : ""}, not here.`,
|
|
182
|
-
};
|
|
438
|
+
if (!isHome()) return notHome();
|
|
183
439
|
const code = normalizeJoinCode(typed);
|
|
184
|
-
if (!code)
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
440
|
+
if (!code) return badCode();
|
|
441
|
+
return tryCode(code, new Set());
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
// Each waiting worker hears a code once: a wrong one counts against its tries (onJoin), so a code kept for a computer
|
|
445
|
+
// not online yet must not be guessed at the others again and again.
|
|
446
|
+
const tryCode = async (code, tried) => {
|
|
189
447
|
const waiting = siblings.filter(
|
|
190
448
|
(sibling) =>
|
|
191
449
|
sibling.role === "worker" &&
|
|
192
450
|
sibling.connected &&
|
|
193
451
|
sibling.publicKey &&
|
|
194
|
-
!confirmed(sibling)
|
|
452
|
+
!confirmed(sibling) &&
|
|
453
|
+
!tried.has(sibling.id)
|
|
195
454
|
);
|
|
196
455
|
const homeKey = identity().publicKey;
|
|
197
456
|
for (const worker of waiting) {
|
|
457
|
+
tried.add(worker.id);
|
|
198
458
|
const keys = {
|
|
199
459
|
homeId: machine.id,
|
|
200
460
|
homeKey,
|
|
@@ -214,10 +474,15 @@ export function createOwnMachines({
|
|
|
214
474
|
log(
|
|
215
475
|
`${worker.label || worker.id} did not join: ${answer?.ok ? "its proof did not match this computer's" : answer?.error}`
|
|
216
476
|
);
|
|
477
|
+
// Only a code it refused (or a proof that did not match) cost it a try; one it never heard is tried again.
|
|
478
|
+
if (!answer?.ok && answer?.code !== "wrong_code")
|
|
479
|
+
tried.delete(worker.id);
|
|
217
480
|
continue;
|
|
218
481
|
}
|
|
219
482
|
store.pinOwnMachine(worker.id, worker.publicKey, worker.label);
|
|
220
483
|
log(`confirmed ${worker.label || worker.id} as one of your computers`);
|
|
484
|
+
// Its first calls may have come before the pin and been refused: it starts over now, sealed, as one of ours.
|
|
485
|
+
void noticeTo(worker.id, { welcome: true });
|
|
221
486
|
changed();
|
|
222
487
|
return {
|
|
223
488
|
ok: true,
|
|
@@ -227,12 +492,104 @@ export function createOwnMachines({
|
|
|
227
492
|
}
|
|
228
493
|
return {
|
|
229
494
|
ok: false,
|
|
495
|
+
code: waiting.length ? "no_match" : "none_waiting",
|
|
230
496
|
error: waiting.length
|
|
231
497
|
? "No computer waiting to join has that code. On the computer you are adding, npx tryharness machines shows its code."
|
|
232
498
|
: "No computer is waiting to join. Start Harness on the computer you are adding (npx tryharness), then type the code it shows.",
|
|
233
499
|
};
|
|
234
500
|
};
|
|
235
501
|
|
|
502
|
+
// ---- adding a computer with setup's one code (the buddy's Add a Computer, npx tryharness machines add) ----
|
|
503
|
+
|
|
504
|
+
// Codes typed here, kept while the computer they are for comes online: id → { code, state, machine, until, tried }.
|
|
505
|
+
const adding = new Map();
|
|
506
|
+
|
|
507
|
+
const tryAdding = async (entry) => {
|
|
508
|
+
if (entry.state !== "waiting" || entry.trying) return;
|
|
509
|
+
if (Date.now() > entry.until) {
|
|
510
|
+
entry.state = "expired";
|
|
511
|
+
changed();
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
entry.trying = true;
|
|
515
|
+
try {
|
|
516
|
+
const answer = await tryCode(entry.code, entry.tried);
|
|
517
|
+
if (answer.ok) {
|
|
518
|
+
Object.assign(entry, {
|
|
519
|
+
state: "joined",
|
|
520
|
+
machine: answer.machine,
|
|
521
|
+
fingerprint: answer.fingerprint,
|
|
522
|
+
});
|
|
523
|
+
changed();
|
|
524
|
+
}
|
|
525
|
+
} finally {
|
|
526
|
+
entry.trying = false;
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* The person typed the code a new computer shows. Harness releases that computer's connection to the account (when it
|
|
532
|
+
* still waits for one), and home confirms it into the swarm as soon as it is online. A code Harness had nothing
|
|
533
|
+
* waiting for is one already approved in a browser: it is tried now, on the computers waiting to join.
|
|
534
|
+
*/
|
|
535
|
+
const add = async (typed) => {
|
|
536
|
+
const code = normalizeJoinCode(typed);
|
|
537
|
+
if (!code) return badCode();
|
|
538
|
+
let approved = null;
|
|
539
|
+
if (isPairCode(code)) {
|
|
540
|
+
const answer = await ask("machine_pair", pairClaim(code)).catch(
|
|
541
|
+
(error) => ({ ok: false, error: error.message })
|
|
542
|
+
);
|
|
543
|
+
if (!answer?.ok)
|
|
544
|
+
return { ok: false, error: answer?.error || "Harness did not answer" };
|
|
545
|
+
approved = answer.approved ? String(answer.hostname || "") : null;
|
|
546
|
+
}
|
|
547
|
+
if (!isHome())
|
|
548
|
+
return approved !== null
|
|
549
|
+
? {
|
|
550
|
+
ok: true,
|
|
551
|
+
state: "approved",
|
|
552
|
+
machine: approved,
|
|
553
|
+
note: `It is connected to your account. To bring it into your swarm, type the same code on your home computer${homeListed() ? ` (${homeListed().label})` : ""}.`,
|
|
554
|
+
}
|
|
555
|
+
: notHome();
|
|
556
|
+
if (approved === null) {
|
|
557
|
+
const answer = await tryCode(code, new Set());
|
|
558
|
+
return answer.ok ? { ...answer, state: "joined" } : answer;
|
|
559
|
+
}
|
|
560
|
+
const entry = {
|
|
561
|
+
id: randomBytes(6).toString("hex"),
|
|
562
|
+
code,
|
|
563
|
+
state: "waiting",
|
|
564
|
+
machine: approved,
|
|
565
|
+
until: Date.now() + ADD_WAIT_MS,
|
|
566
|
+
tried: new Set(),
|
|
567
|
+
};
|
|
568
|
+
adding.set(entry.id, entry);
|
|
569
|
+
await tryAdding(entry);
|
|
570
|
+
return { ok: true, ...addingView(entry) };
|
|
571
|
+
};
|
|
572
|
+
const addingView = ({ id, state, machine: label, fingerprint: key }) => ({
|
|
573
|
+
id,
|
|
574
|
+
state,
|
|
575
|
+
machine: label || null,
|
|
576
|
+
...(key ? { fingerprint: key } : {}),
|
|
577
|
+
});
|
|
578
|
+
// What the buddy shows while it waits: codes past their time read expired, and are let go an hour later.
|
|
579
|
+
const currentAdding = () => {
|
|
580
|
+
const now = Date.now();
|
|
581
|
+
for (const [id, entry] of adding) {
|
|
582
|
+
if (entry.state === "waiting" && now > entry.until)
|
|
583
|
+
entry.state = "expired";
|
|
584
|
+
if (now > entry.until + 60 * 60_000) adding.delete(id);
|
|
585
|
+
}
|
|
586
|
+
return [...adding.values()].map(addingView);
|
|
587
|
+
};
|
|
588
|
+
const retryAdding = () => {
|
|
589
|
+
if (!isHome()) return;
|
|
590
|
+
for (const entry of adding.values()) void tryAdding(entry);
|
|
591
|
+
};
|
|
592
|
+
|
|
236
593
|
/** On a worker: home asks to join with the code the person typed there. */
|
|
237
594
|
const onJoin = (body) => {
|
|
238
595
|
const home = homeListed();
|
|
@@ -255,9 +612,14 @@ export function createOwnMachines({
|
|
|
255
612
|
: { ...saved, wrong }
|
|
256
613
|
)
|
|
257
614
|
);
|
|
258
|
-
return {
|
|
615
|
+
return {
|
|
616
|
+
ok: false,
|
|
617
|
+
code: "wrong_code",
|
|
618
|
+
error: "that is not this computer's code",
|
|
619
|
+
};
|
|
259
620
|
}
|
|
260
621
|
store.pinOwnMachine(home.id, body.homeKey, home.label);
|
|
622
|
+
store.meta("joined-home", home.id);
|
|
261
623
|
store.meta("machine-join", "null");
|
|
262
624
|
log(
|
|
263
625
|
`joined your swarm: ${home.label || "your home computer"} holds the board`
|
|
@@ -274,7 +636,7 @@ export function createOwnMachines({
|
|
|
274
636
|
return {
|
|
275
637
|
ok: false,
|
|
276
638
|
code: "unconfirmed",
|
|
277
|
-
error: `This computer is not confirmed for your swarm yet. On your home computer${home?.label ? ` (${home.label})` : ""},
|
|
639
|
+
error: `This computer is not confirmed for your swarm yet. On your home computer${home?.label ? ` (${home.label})` : ""}, choose Add a Computer in the Harness menu and type ${joinCode() ?? "the code npx tryharness machines shows here"}, or run: npx tryharness machines add ${joinCode() ?? "<code>"}`,
|
|
278
640
|
};
|
|
279
641
|
};
|
|
280
642
|
const offline = () => ({
|
|
@@ -304,7 +666,7 @@ export function createOwnMachines({
|
|
|
304
666
|
const deliver = async (key, requestId, envelope) => {
|
|
305
667
|
let answer;
|
|
306
668
|
try {
|
|
307
|
-
answer = await
|
|
669
|
+
answer = await relayTo({
|
|
308
670
|
to: "home",
|
|
309
671
|
kind: "call",
|
|
310
672
|
body: { envelope },
|
|
@@ -362,6 +724,19 @@ export function createOwnMachines({
|
|
|
362
724
|
});
|
|
363
725
|
if (told?.ok === true) toldHome = said;
|
|
364
726
|
}
|
|
727
|
+
// Each side learns the other's key for a direct link, sealed, so each admits only the other (directLink.mjs).
|
|
728
|
+
const ownKey = await directLink?.key();
|
|
729
|
+
if (ownKey) {
|
|
730
|
+
const swapped = await callOnce(pinned, {
|
|
731
|
+
command: DIRECT_COMMAND,
|
|
732
|
+
agent: "user",
|
|
733
|
+
key: ownKey,
|
|
734
|
+
});
|
|
735
|
+
if (typeof swapped?.key === "string") {
|
|
736
|
+
directLink.allow(pinned.machine_id, swapped.key, { dialer: true });
|
|
737
|
+
void directLink.dial(pinned.machine_id);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
365
740
|
// Home is there for this computer: the work it keeps for it may start.
|
|
366
741
|
if (toldHome) homeBack();
|
|
367
742
|
} finally {
|
|
@@ -467,7 +842,12 @@ export function createOwnMachines({
|
|
|
467
842
|
const callHome = async (request) => {
|
|
468
843
|
const pinned = pinnedHome();
|
|
469
844
|
if (!pinned) return homeListed() ? notJoined() : offline();
|
|
470
|
-
|
|
845
|
+
// A task started here gets its id here, so it is the same task whether home takes it now or once it is back.
|
|
846
|
+
const call = stripLocal(
|
|
847
|
+
request.command === "board.post" && request.id == null
|
|
848
|
+
? { ...request, id: randomUUID() }
|
|
849
|
+
: request
|
|
850
|
+
);
|
|
471
851
|
if (call.command === "board.file.add" && call.path != null)
|
|
472
852
|
return (await attach(pinned, call)) ?? offline();
|
|
473
853
|
if (call.command === "board.file.get" && call.offset === undefined)
|
|
@@ -481,12 +861,23 @@ export function createOwnMachines({
|
|
|
481
861
|
return {
|
|
482
862
|
ok: true,
|
|
483
863
|
queued: true,
|
|
864
|
+
...(call.command === "board.post"
|
|
865
|
+
? {
|
|
866
|
+
task: {
|
|
867
|
+
id: call.id,
|
|
868
|
+
objective: call.objective,
|
|
869
|
+
target_agent: call.targetAgent ?? null,
|
|
870
|
+
status: "open",
|
|
871
|
+
},
|
|
872
|
+
}
|
|
873
|
+
: {}),
|
|
484
874
|
note: `Your home computer${homeListed()?.label ? ` (${homeListed().label})` : ""} is out of reach; this goes to it as soon as it is back.`,
|
|
485
875
|
};
|
|
486
876
|
};
|
|
487
877
|
|
|
488
|
-
/** Whether a board request made on this computer goes to home. */
|
|
489
|
-
const forwards = (request) =>
|
|
878
|
+
/** Whether a board request made on this computer goes to home: not while it works on its own board. */
|
|
879
|
+
const forwards = (request) =>
|
|
880
|
+
!isHome() && trusted() && goesHome(request.command);
|
|
490
881
|
|
|
491
882
|
// ---- on home: a worker's call, opened, answered on the board and sealed back ----
|
|
492
883
|
|
|
@@ -502,14 +893,36 @@ export function createOwnMachines({
|
|
|
502
893
|
}
|
|
503
894
|
};
|
|
504
895
|
|
|
505
|
-
|
|
896
|
+
// The answers home gave lately, by the call they answer: the same sealed call arriving again (over the direct link,
|
|
897
|
+
// then through Harness when the link was slow) gets the same answer, or waits for the first, and never runs twice
|
|
898
|
+
// (review, October 21: the second was refused as a replay and the agent told its write failed).
|
|
899
|
+
const answered = new Map();
|
|
900
|
+
const ANSWERS_KEPT = 500;
|
|
901
|
+
const once = (key, run) => {
|
|
902
|
+
if (answered.has(key)) return answered.get(key);
|
|
903
|
+
const answer = run();
|
|
904
|
+
answered.set(key, answer);
|
|
905
|
+
if (answered.size > ANSWERS_KEPT)
|
|
906
|
+
answered.delete(answered.keys().next().value);
|
|
907
|
+
return answer;
|
|
908
|
+
};
|
|
909
|
+
|
|
910
|
+
// Keyed by the sealed envelope itself (its random iv with its request id): only the very same call matches, so a
|
|
911
|
+
// forged one under a real call's id cannot stand in for it.
|
|
912
|
+
const sealedKey = (envelope) =>
|
|
913
|
+
`${envelope?.deviceId}:${envelope?.requestId}:${envelope?.iv}`;
|
|
914
|
+
const answerCall = (from, body) =>
|
|
915
|
+
once(`call:${from}:${sealedKey(body?.envelope)}`, () =>
|
|
916
|
+
answerCallOnce(from, body)
|
|
917
|
+
);
|
|
918
|
+
const answerCallOnce = async (from, body) => {
|
|
506
919
|
const pinned = store.ownMachine(String(from));
|
|
507
920
|
if (!isHome() || !pinned)
|
|
508
921
|
return {
|
|
509
922
|
ok: false,
|
|
510
923
|
code: "unconfirmed",
|
|
511
924
|
error: isHome()
|
|
512
|
-
? "This computer is not confirmed on home. On home,
|
|
925
|
+
? "This computer is not confirmed on home. On home, choose Add a Computer in the Harness menu and type the code npx tryharness machines shows on this computer."
|
|
513
926
|
: "This computer is not home.",
|
|
514
927
|
};
|
|
515
928
|
const key = await keyFor(pinned.public_key, from);
|
|
@@ -521,32 +934,66 @@ export function createOwnMachines({
|
|
|
521
934
|
if (!opened.ok) return opened;
|
|
522
935
|
const call = opened.call ?? {};
|
|
523
936
|
const command = String(call.command || "");
|
|
524
|
-
const result =
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
937
|
+
const result =
|
|
938
|
+
command === DIRECT_COMMAND
|
|
939
|
+
? await swapDirectKeys(from, call.key)
|
|
940
|
+
: command === MODELS_COMMAND || command === MODEL_COMMAND
|
|
941
|
+
? await answerModelsCall(call, from)
|
|
942
|
+
: command === MIRROR_COMMAND && answerMirror
|
|
943
|
+
? answerMirror(call)
|
|
944
|
+
: command === MERGE_COMMAND && answerMerge
|
|
945
|
+
? // A confirmed worker brings the board it kept on its own (union only); a stand-in hands back its writes.
|
|
946
|
+
call.bring === true || store.meta("stood-in-by") === from
|
|
947
|
+
? answerMerge(call)
|
|
948
|
+
: {
|
|
949
|
+
ok: false,
|
|
950
|
+
error: "this computer did not stand in for home",
|
|
951
|
+
}
|
|
952
|
+
: !goesHome(command)
|
|
953
|
+
? {
|
|
954
|
+
ok: false,
|
|
955
|
+
error: `${command || "that"} runs on the agent's own computer`,
|
|
956
|
+
}
|
|
957
|
+
: await settle(() =>
|
|
958
|
+
boardCommand({
|
|
959
|
+
...stripLocal(call),
|
|
960
|
+
// A path names a file on the worker; its bytes come as data.
|
|
961
|
+
path: undefined,
|
|
962
|
+
command,
|
|
963
|
+
[FROM_MACHINE]: from,
|
|
964
|
+
})
|
|
965
|
+
);
|
|
538
966
|
return {
|
|
539
967
|
envelope: await sealAnswer(key, from, body.envelope.requestId, result),
|
|
540
968
|
};
|
|
541
969
|
};
|
|
542
970
|
|
|
971
|
+
/** On home: a worker's key for a direct link, taken, and home's own given back. */
|
|
972
|
+
const swapDirectKeys = async (workerId, workerKey) => {
|
|
973
|
+
const ownKey = await directLink?.key();
|
|
974
|
+
if (!ownKey) return { ok: true, key: null };
|
|
975
|
+
directLink.allow(workerId, workerKey);
|
|
976
|
+
return { ok: true, key: ownKey };
|
|
977
|
+
};
|
|
978
|
+
|
|
979
|
+
/** A message that came over a direct link: what Harness's machine_relay would have handed over, from that computer. */
|
|
980
|
+
const fromLink = (peerId, body) =>
|
|
981
|
+
isHome()
|
|
982
|
+
? body?.kind === "call"
|
|
983
|
+
? answerCall(peerId, body.body)
|
|
984
|
+
: Promise.resolve({ ok: false, error: "home takes only calls" })
|
|
985
|
+
: ["notice", "ask"].includes(body?.kind) &&
|
|
986
|
+
peerId === pinnedHome()?.machine_id
|
|
987
|
+
? relay({ from: "home", kind: body.kind, body: body.body })
|
|
988
|
+
: Promise.resolve({ ok: false, error: "a worker takes only home's" });
|
|
989
|
+
|
|
543
990
|
/** On home: a notice for a worker (a run to stop), kept by Harness while it is offline. */
|
|
544
991
|
const noticeTo = async (workerId, notice) => {
|
|
545
992
|
const pinned = store.ownMachine(workerId);
|
|
546
993
|
if (!isHome() || !pinned) return { ok: false };
|
|
547
994
|
const key = await keyFor(pinned.public_key, workerId);
|
|
548
995
|
const envelope = await sealNotice(key, workerId, newRequestId(), notice);
|
|
549
|
-
return
|
|
996
|
+
return relayTo({
|
|
550
997
|
to: workerId,
|
|
551
998
|
kind: "notice",
|
|
552
999
|
body: { envelope },
|
|
@@ -566,11 +1013,105 @@ export function createOwnMachines({
|
|
|
566
1013
|
),
|
|
567
1014
|
});
|
|
568
1015
|
if (!notice) return { ok: false };
|
|
1016
|
+
// Home confirmed this computer: what it keeps for home, and the board it brings, go now.
|
|
1017
|
+
if (notice.welcome === true) void flush();
|
|
569
1018
|
onNotice(notice);
|
|
570
1019
|
return { ok: true };
|
|
571
1020
|
};
|
|
572
1021
|
|
|
573
|
-
/**
|
|
1022
|
+
/**
|
|
1023
|
+
* On home: each confirmed worker it can reach hears its copy of the board is behind (boardMirror.mjs): through
|
|
1024
|
+
* Harness when Harness has it connected, or over the direct link when only that is up.
|
|
1025
|
+
*/
|
|
1026
|
+
const tellWorkers = (notice) => {
|
|
1027
|
+
if (!isHome()) return;
|
|
1028
|
+
const linked = new Set(
|
|
1029
|
+
(directLink?.status() ?? [])
|
|
1030
|
+
.filter((link) => link.linked)
|
|
1031
|
+
.map((link) => link.id)
|
|
1032
|
+
);
|
|
1033
|
+
for (const worker of siblings.filter(
|
|
1034
|
+
(sibling) =>
|
|
1035
|
+
sibling.role === "worker" &&
|
|
1036
|
+
(sibling.connected || linked.has(sibling.id))
|
|
1037
|
+
))
|
|
1038
|
+
if (store.ownMachine(worker.id)) void noticeTo(worker.id, notice);
|
|
1039
|
+
};
|
|
1040
|
+
|
|
1041
|
+
// ---- the person's Harness windows on the other computers ----
|
|
1042
|
+
|
|
1043
|
+
// The windows a question finds nothing in: none linked, or none open. Any other answer, a refusal included, is final.
|
|
1044
|
+
const nobodyThere = (answer) =>
|
|
1045
|
+
answer?.ok === false && ["unpaired", "closed"].includes(answer.code);
|
|
1046
|
+
|
|
1047
|
+
/**
|
|
1048
|
+
* On home: a question for the person's Harness windows linked to the workers (each window links to the computer it
|
|
1049
|
+
* runs on), asked of each confirmed worker that is connected, in turn. The first that has one answers; null when none
|
|
1050
|
+
* was asked or none had a window.
|
|
1051
|
+
*/
|
|
1052
|
+
const askWorkerWindows = async (call) => {
|
|
1053
|
+
if (!isHome()) return null;
|
|
1054
|
+
let found = null;
|
|
1055
|
+
for (const worker of siblings.filter(
|
|
1056
|
+
(sibling) => sibling.role === "worker" && sibling.connected
|
|
1057
|
+
)) {
|
|
1058
|
+
const answer = await askWorker(worker.id, call);
|
|
1059
|
+
if (!answer) continue;
|
|
1060
|
+
if (!nobodyThere(answer)) return answer;
|
|
1061
|
+
found ??= answer;
|
|
1062
|
+
}
|
|
1063
|
+
return found;
|
|
1064
|
+
};
|
|
1065
|
+
|
|
1066
|
+
/** On home: one sealed question for a confirmed worker and its opened answer; null when none came back. */
|
|
1067
|
+
const askWorker = async (workerId, call) => {
|
|
1068
|
+
const pinned = store.ownMachine(workerId);
|
|
1069
|
+
if (!pinned) return null;
|
|
1070
|
+
const key = await keyFor(pinned.public_key, workerId);
|
|
1071
|
+
const requestId = newRequestId();
|
|
1072
|
+
const envelope = await sealAsk(key, workerId, requestId, call);
|
|
1073
|
+
const reply = await relayTo({
|
|
1074
|
+
to: workerId,
|
|
1075
|
+
kind: "ask",
|
|
1076
|
+
body: { envelope },
|
|
1077
|
+
}).catch(() => null);
|
|
1078
|
+
if (!reply?.envelope) return null;
|
|
1079
|
+
return openAskAnswer(key, reply.envelope, { workerId, requestId }).catch(
|
|
1080
|
+
() => null
|
|
1081
|
+
);
|
|
1082
|
+
};
|
|
1083
|
+
|
|
1084
|
+
/** On a worker: home's question for the windows linked here, opened, asked and the answer sealed back. */
|
|
1085
|
+
const onAskFromHome = (body) =>
|
|
1086
|
+
once(`ask:${sealedKey(body?.envelope)}`, () => onAskFromHomeOnce(body));
|
|
1087
|
+
const onAskFromHomeOnce = async (body) => {
|
|
1088
|
+
const pinned = pinnedHome();
|
|
1089
|
+
if (!pinned) return { ok: false, error: "this computer is not confirmed" };
|
|
1090
|
+
const key = await keyFor(pinned.public_key, machine.id);
|
|
1091
|
+
const call = await openAsk(key, body?.envelope, {
|
|
1092
|
+
workerId: machine.id,
|
|
1093
|
+
firstSeen: (requestId) =>
|
|
1094
|
+
store.firstPhoneRequest(
|
|
1095
|
+
`machine:${pinned.machine_id}`,
|
|
1096
|
+
String(requestId)
|
|
1097
|
+
),
|
|
1098
|
+
});
|
|
1099
|
+
if (!call) return { ok: false, error: "the question could not be opened" };
|
|
1100
|
+
// Home asking for this computer's agents' models is answered here; anything else goes to the windows.
|
|
1101
|
+
const answer = call.models
|
|
1102
|
+
? await ownModels(call.models)
|
|
1103
|
+
: await askOwnWindows(call);
|
|
1104
|
+
return {
|
|
1105
|
+
envelope: await sealAskAnswer(
|
|
1106
|
+
key,
|
|
1107
|
+
machine.id,
|
|
1108
|
+
body.envelope.requestId,
|
|
1109
|
+
answer
|
|
1110
|
+
),
|
|
1111
|
+
};
|
|
1112
|
+
};
|
|
1113
|
+
|
|
1114
|
+
/** Harness's machine_relay for this computer: a call (on home), a join, a notice or a question (on a worker). */
|
|
574
1115
|
const relay = (input) =>
|
|
575
1116
|
input?.kind === "call"
|
|
576
1117
|
? answerCall(String(input.from || ""), input.body)
|
|
@@ -578,7 +1119,9 @@ export function createOwnMachines({
|
|
|
578
1119
|
? Promise.resolve(onJoin(input.body))
|
|
579
1120
|
: input?.kind === "notice"
|
|
580
1121
|
? onNoticeFromHome(input.body)
|
|
581
|
-
:
|
|
1122
|
+
: input?.kind === "ask"
|
|
1123
|
+
? onAskFromHome(input.body)
|
|
1124
|
+
: Promise.resolve({ ok: false, error: "unknown message" });
|
|
582
1125
|
|
|
583
1126
|
// ---- what the person and status read ----
|
|
584
1127
|
|
|
@@ -605,11 +1148,16 @@ export function createOwnMachines({
|
|
|
605
1148
|
id: machine.id,
|
|
606
1149
|
label: machine.label,
|
|
607
1150
|
role: machine.role ?? "home",
|
|
1151
|
+
alone: actsAlone(),
|
|
1152
|
+
standingInFor: standingIn()
|
|
1153
|
+
? (labelOf(standingIn()) ?? standingIn())
|
|
1154
|
+
: null,
|
|
608
1155
|
fingerprint: identity()
|
|
609
1156
|
? await fingerprint(identity().publicKey)
|
|
610
1157
|
: null,
|
|
611
1158
|
},
|
|
612
1159
|
joinCode: joinCode(),
|
|
1160
|
+
adding: currentAdding(),
|
|
613
1161
|
machines: await Promise.all(
|
|
614
1162
|
listed.map(async (sibling) => ({
|
|
615
1163
|
id: sibling.id,
|
|
@@ -618,6 +1166,10 @@ export function createOwnMachines({
|
|
|
618
1166
|
connected: sibling.connected,
|
|
619
1167
|
agents: sibling.agents,
|
|
620
1168
|
confirmed: confirmed(sibling),
|
|
1169
|
+
standsInForThis: sibling.standInFor === machine.id,
|
|
1170
|
+
direct:
|
|
1171
|
+
directLink?.status().find((link) => link.id === sibling.id)
|
|
1172
|
+
?.linked === true,
|
|
621
1173
|
fingerprint: sibling.publicKey
|
|
622
1174
|
? await fingerprint(sibling.publicKey)
|
|
623
1175
|
: null,
|
|
@@ -626,10 +1178,181 @@ export function createOwnMachines({
|
|
|
626
1178
|
};
|
|
627
1179
|
};
|
|
628
1180
|
|
|
1181
|
+
// ---- every computer's agents and the models they run, for the buddy's Swarm tab ----
|
|
1182
|
+
|
|
1183
|
+
const modelsRead = (refresh) => ({
|
|
1184
|
+
command: "board.swarm.models",
|
|
1185
|
+
...(refresh === true ? { refresh: true } : {}),
|
|
1186
|
+
});
|
|
1187
|
+
|
|
1188
|
+
/** One computer as the Swarm tab shows it: its agents' models as it answered, else the agents Harness last listed. */
|
|
1189
|
+
const computerEntry = (computer, answer) => ({
|
|
1190
|
+
id: computer.id,
|
|
1191
|
+
label: computer.label,
|
|
1192
|
+
role: computer.role ?? "home",
|
|
1193
|
+
online: computer.online,
|
|
1194
|
+
models: answer?.ok ? (answer.agents ?? []) : [],
|
|
1195
|
+
agents: answer?.ok ? (answer.installed ?? []) : (computer.agents ?? []),
|
|
1196
|
+
...(answer?.ok === false ? { error: answer.error } : {}),
|
|
1197
|
+
});
|
|
1198
|
+
|
|
1199
|
+
/** On home: the agents that run themselves, by the computer they last called the board from (home's records). */
|
|
1200
|
+
const selfRunByComputer = () => {
|
|
1201
|
+
const since = new Date(Date.now() - SELF_RUN_SEEN_MS).toISOString();
|
|
1202
|
+
const found = {};
|
|
1203
|
+
for (const { agent, machine: computer } of store.agentsSeenOnComputers(
|
|
1204
|
+
since
|
|
1205
|
+
)) {
|
|
1206
|
+
if (member(agent)?.runs !== "self") continue;
|
|
1207
|
+
(found[computer] ??= []).push(agent);
|
|
1208
|
+
}
|
|
1209
|
+
return found;
|
|
1210
|
+
};
|
|
1211
|
+
const withSelfRun = (computers, selfRun) =>
|
|
1212
|
+
computers.map((computer) => ({
|
|
1213
|
+
...computer,
|
|
1214
|
+
selfRun: selfRun[computer.id] ?? [],
|
|
1215
|
+
}));
|
|
1216
|
+
|
|
1217
|
+
/** On home: home and each confirmed worker but `except` (the worker asking), each read on its own computer. */
|
|
1218
|
+
const everyComputersModels = async (read, except = null) => {
|
|
1219
|
+
const workers = siblings.filter(
|
|
1220
|
+
(sibling) => sibling.id !== except && confirmed(sibling)
|
|
1221
|
+
);
|
|
1222
|
+
const [own, ...answers] = await Promise.all([
|
|
1223
|
+
ownModels(read),
|
|
1224
|
+
...workers.map((worker) =>
|
|
1225
|
+
worker.connected
|
|
1226
|
+
? askWorker(worker.id, { models: read }).then(
|
|
1227
|
+
(answer) =>
|
|
1228
|
+
answer ?? { ok: false, error: `${worker.label} did not answer` }
|
|
1229
|
+
)
|
|
1230
|
+
: null
|
|
1231
|
+
),
|
|
1232
|
+
]);
|
|
1233
|
+
return [
|
|
1234
|
+
computerEntry(
|
|
1235
|
+
{
|
|
1236
|
+
id: machine.id,
|
|
1237
|
+
label: machine.label,
|
|
1238
|
+
role: machine.role ?? "home",
|
|
1239
|
+
online: true,
|
|
1240
|
+
agents,
|
|
1241
|
+
},
|
|
1242
|
+
own
|
|
1243
|
+
),
|
|
1244
|
+
...workers.map((worker, index) =>
|
|
1245
|
+
computerEntry({ ...worker, online: worker.connected }, answers[index])
|
|
1246
|
+
),
|
|
1247
|
+
];
|
|
1248
|
+
};
|
|
1249
|
+
|
|
1250
|
+
// Why the other computers' agents are missing: this one is not joined yet and works on its own, or home is away.
|
|
1251
|
+
const unreachableNote = (answer) =>
|
|
1252
|
+
actsAlone()
|
|
1253
|
+
? "This computer isn't joined to your home computer yet, so only its own agents show here."
|
|
1254
|
+
: (answer ?? offline()).error;
|
|
1255
|
+
|
|
1256
|
+
/** The person's computers, this one first, each with its agents and the model and effort each runs on. */
|
|
1257
|
+
const models = async ({ refresh = false } = {}) => {
|
|
1258
|
+
const read = modelsRead(refresh);
|
|
1259
|
+
if (isHome())
|
|
1260
|
+
return {
|
|
1261
|
+
ok: true,
|
|
1262
|
+
here: machine.id,
|
|
1263
|
+
computers: withSelfRun(
|
|
1264
|
+
await everyComputersModels(read),
|
|
1265
|
+
selfRunByComputer()
|
|
1266
|
+
),
|
|
1267
|
+
};
|
|
1268
|
+
const own = computerEntry(
|
|
1269
|
+
{
|
|
1270
|
+
id: machine.id,
|
|
1271
|
+
label: machine.label,
|
|
1272
|
+
role: "worker",
|
|
1273
|
+
online: true,
|
|
1274
|
+
agents,
|
|
1275
|
+
},
|
|
1276
|
+
await ownModels(read)
|
|
1277
|
+
);
|
|
1278
|
+
const pinned = pinnedHome();
|
|
1279
|
+
const fromHome = pinned
|
|
1280
|
+
? await callOnce(pinned, {
|
|
1281
|
+
command: MODELS_COMMAND,
|
|
1282
|
+
agent: "user",
|
|
1283
|
+
refresh: refresh === true,
|
|
1284
|
+
})
|
|
1285
|
+
: null;
|
|
1286
|
+
const others = fromHome?.ok
|
|
1287
|
+
? fromHome.computers.filter((computer) => computer.id !== machine.id)
|
|
1288
|
+
: siblings.map((sibling) =>
|
|
1289
|
+
computerEntry({ ...sibling, online: false }, null)
|
|
1290
|
+
);
|
|
1291
|
+
return {
|
|
1292
|
+
ok: true,
|
|
1293
|
+
here: machine.id,
|
|
1294
|
+
computers: withSelfRun([own, ...others], fromHome?.selfRun ?? {}),
|
|
1295
|
+
...(fromHome?.ok ? {} : { note: unreachableNote(fromHome) }),
|
|
1296
|
+
};
|
|
1297
|
+
};
|
|
1298
|
+
|
|
1299
|
+
/** On home: a worker asking for every computer's models, or setting one; only as the person. */
|
|
1300
|
+
const answerModelsCall = async (call, from) => {
|
|
1301
|
+
if (call.agent !== "user")
|
|
1302
|
+
return { ok: false, error: "only the person reads or sets models here" };
|
|
1303
|
+
if (call.command === MODEL_COMMAND) return setModel(call);
|
|
1304
|
+
return {
|
|
1305
|
+
ok: true,
|
|
1306
|
+
computers: await everyComputersModels(modelsRead(call.refresh), from),
|
|
1307
|
+
selfRun: selfRunByComputer(),
|
|
1308
|
+
};
|
|
1309
|
+
};
|
|
1310
|
+
|
|
1311
|
+
/** The person's model and effort for one agent on one computer, set on that computer. */
|
|
1312
|
+
const setModel = async (request) => {
|
|
1313
|
+
const pick = {
|
|
1314
|
+
command: "board.swarm.model",
|
|
1315
|
+
target: request.target,
|
|
1316
|
+
model: request.model,
|
|
1317
|
+
effort: request.effort ?? null,
|
|
1318
|
+
};
|
|
1319
|
+
const computer = String(request.computer || machine.id);
|
|
1320
|
+
if (computer === machine.id) return ownModels(pick);
|
|
1321
|
+
if (isHome()) {
|
|
1322
|
+
const worker = siblings.find((sibling) => sibling.id === computer);
|
|
1323
|
+
if (!worker || !confirmed(worker))
|
|
1324
|
+
return {
|
|
1325
|
+
ok: false,
|
|
1326
|
+
error: "that computer is not one of yours confirmed on home",
|
|
1327
|
+
};
|
|
1328
|
+
return (
|
|
1329
|
+
(await askWorker(computer, { models: pick })) ?? {
|
|
1330
|
+
ok: false,
|
|
1331
|
+
error: `${worker.label} did not answer; it may be asleep or offline`,
|
|
1332
|
+
}
|
|
1333
|
+
);
|
|
1334
|
+
}
|
|
1335
|
+
const pinned = pinnedHome();
|
|
1336
|
+
if (!pinned) return homeListed() ? notJoined() : offline();
|
|
1337
|
+
return (
|
|
1338
|
+
(await callOnce(pinned, {
|
|
1339
|
+
command: MODEL_COMMAND,
|
|
1340
|
+
agent: "user",
|
|
1341
|
+
computer,
|
|
1342
|
+
target: pick.target,
|
|
1343
|
+
model: pick.model,
|
|
1344
|
+
effort: pick.effort,
|
|
1345
|
+
})) ?? offline()
|
|
1346
|
+
);
|
|
1347
|
+
};
|
|
1348
|
+
|
|
629
1349
|
const status = () => ({
|
|
630
1350
|
id: machine.id,
|
|
631
1351
|
label: machine.label,
|
|
632
1352
|
role: machine.role ?? null,
|
|
1353
|
+
...(standingIn()
|
|
1354
|
+
? { standingInFor: labelOf(standingIn()) ?? standingIn() }
|
|
1355
|
+
: {}),
|
|
633
1356
|
...(isHome()
|
|
634
1357
|
? {}
|
|
635
1358
|
: {
|
|
@@ -641,7 +1364,23 @@ export function createOwnMachines({
|
|
|
641
1364
|
}
|
|
642
1365
|
: null,
|
|
643
1366
|
waiting: outbox().length,
|
|
1367
|
+
// Working on its own board until the person types this code on home.
|
|
1368
|
+
...(actsAlone() && joinCode() ? { joinCode: joinCode() } : {}),
|
|
644
1369
|
}),
|
|
1370
|
+
// On home: the computers connected and waiting for their code to be typed here.
|
|
1371
|
+
...(isHome()
|
|
1372
|
+
? {
|
|
1373
|
+
waitingToJoin: siblings
|
|
1374
|
+
.filter(
|
|
1375
|
+
(sibling) =>
|
|
1376
|
+
sibling.role === "worker" &&
|
|
1377
|
+
sibling.connected &&
|
|
1378
|
+
sibling.publicKey &&
|
|
1379
|
+
!confirmed(sibling)
|
|
1380
|
+
)
|
|
1381
|
+
.map((sibling) => sibling.label),
|
|
1382
|
+
}
|
|
1383
|
+
: {}),
|
|
645
1384
|
});
|
|
646
1385
|
|
|
647
1386
|
/** The person's computers that have this agent, this one first: what its card says. Only confirmed ones. */
|
|
@@ -721,18 +1460,66 @@ export function createOwnMachines({
|
|
|
721
1460
|
})),
|
|
722
1461
|
];
|
|
723
1462
|
|
|
1463
|
+
/** The name beside an agent's words from that computer ("Codex (linux)"): only once the person has more than one. */
|
|
1464
|
+
const shownLabel = (id) => (all().length > 1 ? labelOf(id) : null);
|
|
1465
|
+
|
|
1466
|
+
/**
|
|
1467
|
+
* Where an agent calling from that computer is: its name, whether it is home (which keeps the board), home's name and
|
|
1468
|
+
* the person's other computers. What an agent is told of itself (cards.mjs youLine); null for a computer not known here.
|
|
1469
|
+
*/
|
|
1470
|
+
const whereIs = (id) => {
|
|
1471
|
+
const computers = all();
|
|
1472
|
+
const here = computers.find((computer) => computer.id === id);
|
|
1473
|
+
if (!labelOf(id)) return null;
|
|
1474
|
+
return {
|
|
1475
|
+
computer: labelOf(id),
|
|
1476
|
+
home: (here?.role ?? "home") === "home",
|
|
1477
|
+
homeLabel:
|
|
1478
|
+
computers.find((computer) => computer.role === "home")?.label ?? null,
|
|
1479
|
+
others: computers
|
|
1480
|
+
.filter((computer) => computer.id !== id)
|
|
1481
|
+
.map((computer) => computer.label),
|
|
1482
|
+
};
|
|
1483
|
+
};
|
|
1484
|
+
|
|
724
1485
|
return {
|
|
725
1486
|
hello,
|
|
726
1487
|
adopt,
|
|
727
1488
|
introduce,
|
|
728
1489
|
rename,
|
|
729
1490
|
makeHome,
|
|
1491
|
+
leave,
|
|
1492
|
+
remove,
|
|
1493
|
+
removed,
|
|
730
1494
|
confirm,
|
|
1495
|
+
add,
|
|
731
1496
|
forwards,
|
|
732
1497
|
callHome,
|
|
733
1498
|
relay,
|
|
734
1499
|
noticeTo,
|
|
1500
|
+
askWorkerWindows,
|
|
1501
|
+
tellWorkers,
|
|
1502
|
+
fromLink,
|
|
1503
|
+
standingIn,
|
|
1504
|
+
someoneStandsIn,
|
|
1505
|
+
// A worker confirmed into the swarm: it holds home's key.
|
|
1506
|
+
joined: () => Boolean(pinnedHome()),
|
|
1507
|
+
actsAlone,
|
|
1508
|
+
standInReachable: () => {
|
|
1509
|
+
const home = homeListed();
|
|
1510
|
+
return Boolean(
|
|
1511
|
+
home &&
|
|
1512
|
+
(home.connected ||
|
|
1513
|
+
(directLink?.status() ?? []).some(
|
|
1514
|
+
(link) => link.id === home.id && link.linked
|
|
1515
|
+
))
|
|
1516
|
+
);
|
|
1517
|
+
},
|
|
1518
|
+
reclaim,
|
|
1519
|
+
directStatus: () => directLink?.status() ?? [],
|
|
735
1520
|
list,
|
|
1521
|
+
models,
|
|
1522
|
+
setModel,
|
|
736
1523
|
status,
|
|
737
1524
|
all,
|
|
738
1525
|
learn,
|
|
@@ -740,6 +1527,8 @@ export function createOwnMachines({
|
|
|
740
1527
|
computersWith,
|
|
741
1528
|
computersFor,
|
|
742
1529
|
labelOf,
|
|
1530
|
+
shownLabel,
|
|
1531
|
+
whereIs,
|
|
743
1532
|
find: (named) => findMachine(all(), named),
|
|
744
1533
|
isHome,
|
|
745
1534
|
self: () => machine.id,
|