tryharness 0.7.54 → 0.7.57
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 +15 -16
- package/cli.mjs +377 -62
- package/lib/activity.mjs +10 -10
- package/lib/adapters.mjs +16 -0
- package/lib/agentBinaries.mjs +71 -0
- package/lib/agentRuntimeConfig.mjs +1 -1
- package/lib/agentRuntimes.mjs +9 -3
- package/lib/agentSignIn.mjs +271 -41
- package/lib/answers.mjs +5 -5
- package/lib/archetypes.d.ts +64 -0
- package/lib/archetypes.js +989 -0
- package/lib/asks.mjs +4 -4
- package/lib/audit.mjs +36 -13
- package/lib/backupSetup.mjs +4 -2
- package/lib/boardAccess.mjs +17 -11
- package/lib/boardBackup.mjs +6 -6
- package/lib/boardFileCommands.mjs +6 -6
- package/lib/boardFiles.mjs +13 -12
- package/lib/boardMirror.mjs +82 -47
- package/lib/boardProtocol.json +30 -1
- package/lib/boardWaits.mjs +2 -3
- package/lib/book.mjs +6 -6
- package/lib/bridge.mjs +5 -5
- package/lib/cards.mjs +2 -2
- package/lib/cloudAgents.mjs +216 -0
- package/lib/cloudAi.mjs +1312 -0
- package/lib/cloudAssistants.mjs +242 -0
- package/lib/cloudBoot.mjs +81 -86
- package/lib/cloudSave.mjs +197 -0
- package/lib/cloudSsh.mjs +178 -0
- package/lib/context.mjs +3 -3
- package/lib/contractGate.mjs +34 -12
- package/lib/control-client.mjs +9 -1
- package/lib/controlSocket.mjs +86 -0
- package/lib/coordinator.mjs +103 -101
- package/lib/daemon.mjs +1128 -2865
- package/lib/desk.mjs +7 -6
- package/lib/desktop.mjs +72 -68
- package/lib/directLink.mjs +16 -5
- package/lib/environment.mjs +2 -1
- package/lib/environments.mjs +14 -6
- package/lib/firewall.mjs +9 -9
- package/lib/folders.mjs +18 -14
- package/lib/frames.mjs +2 -3
- package/lib/gitHost.mjs +3 -3
- package/lib/goalCommands.mjs +25 -7
- package/lib/goals.mjs +7 -7
- package/lib/guestRequests.mjs +94 -0
- package/lib/handoffs.mjs +8 -7
- package/lib/handover.mjs +4 -4
- package/lib/harnessModel.mjs +6 -6
- package/lib/harnessOffers.mjs +266 -0
- package/lib/inference.mjs +26 -8
- package/lib/jobAnswers.mjs +190 -0
- package/lib/landed.mjs +1 -1
- package/lib/lane.mjs +42 -11
- package/lib/lessons.mjs +16 -16
- package/lib/library.mjs +3 -3
- package/lib/lineup.mjs +3 -0
- package/lib/linkRelay.mjs +574 -0
- package/lib/localAgentAsk.mjs +74 -0
- package/lib/localStack.mjs +536 -0
- package/lib/localmodels.mjs +6 -4
- package/lib/loopBackstop.mjs +4 -4
- package/lib/machineIdentity.mjs +9 -1
- package/lib/machineRoutes.mjs +181 -2
- package/lib/machineRuns.mjs +76 -1
- package/lib/machineSeal.mjs +39 -12
- package/lib/mcp-stdio.mjs +96 -26
- package/lib/mcpRelay.mjs +1 -1
- package/lib/members.json +13 -0
- package/lib/members.mjs +1 -1
- package/lib/models.mjs +77 -23
- package/lib/museSession.mjs +6 -1
- package/lib/nativeHost.mjs +35 -7
- package/lib/network.mjs +50 -5
- package/lib/notify.mjs +7 -5
- package/lib/nudge.mjs +3 -3
- package/lib/ocr.mjs +9 -9
- package/lib/onboard.mjs +26 -20
- package/lib/oneLine.mjs +7 -7
- package/lib/ownMachines.mjs +928 -149
- package/lib/pairing.mjs +33 -6
- package/lib/paths.mjs +15 -1
- package/lib/peerProcess.mjs +124 -0
- package/lib/personModel.mjs +3 -3
- package/lib/personNotices.mjs +151 -0
- package/lib/phones.mjs +490 -38
- package/lib/placeCommands.mjs +4 -4
- package/lib/places.mjs +70 -2
- package/lib/profile.mjs +67 -14
- package/lib/projectGit.mjs +158 -14
- package/lib/projectPieces.mjs +902 -0
- package/lib/projectSettings.mjs +7 -4
- package/lib/projects.mjs +384 -62
- package/lib/provenance.mjs +5 -1
- package/lib/proxyLinks.mjs +267 -0
- package/lib/reactions.mjs +7 -5
- package/lib/readiness.mjs +12 -7
- package/lib/record.mjs +19 -6
- package/lib/redact.mjs +4 -4
- package/lib/relayLimits.mjs +62 -0
- package/lib/release.mjs +170 -0
- package/lib/releaseKeys.json +6 -0
- package/lib/repoLinks.mjs +4 -4
- package/lib/roomChannels.mjs +24 -0
- package/lib/roomPage.mjs +1 -1
- package/lib/runAuthority.mjs +101 -0
- package/lib/runBrief.mjs +19 -8
- package/lib/runWorker.mjs +14 -5
- package/lib/runner.mjs +215 -25
- package/lib/runtime-install.mjs +143 -9
- package/lib/schedules.mjs +2 -2
- package/lib/screens.mjs +1 -1
- package/lib/sealed.mjs +11 -2
- package/lib/sealedThreads.mjs +671 -0
- package/lib/secretPatterns.json +1 -1
- package/lib/secrets.mjs +4 -4
- package/lib/seen.mjs +6 -6
- package/lib/serverWords.mjs +175 -0
- package/lib/service.mjs +22 -12
- package/lib/sessionControl.mjs +6 -6
- package/lib/specialties.mjs +6 -6
- package/lib/specialtyKeys.js +4 -5
- package/lib/specialtyNotice.mjs +7 -7
- package/lib/specialtySnapshot.mjs +5 -5
- package/lib/speech.mjs +5 -5
- package/lib/store.mjs +140 -39
- package/lib/stuck.mjs +1 -1
- package/lib/swarm.mjs +167 -16
- package/lib/swarmDevices.mjs +319 -0
- package/lib/swarmMemory.mjs +27 -25
- package/lib/t3.mjs +13 -13
- package/lib/taskCommands.mjs +44 -11
- package/lib/taskRooms.mjs +125 -0
- package/lib/taskRuns.mjs +36 -1
- package/lib/team.mjs +41 -18
- package/lib/threadActions.mjs +315 -0
- package/lib/threadAuthority.mjs +101 -0
- package/lib/threadBriefs.mjs +178 -0
- package/lib/threadLine.mjs +2 -2
- package/lib/threadRuns.mjs +138 -43
- package/lib/threadView.mjs +5 -5
- package/lib/usageTotals.mjs +2 -3
- package/lib/vault.mjs +101 -10
- package/lib/vaultCloud.mjs +2 -2
- package/lib/vaultCommands.mjs +15 -6
- package/lib/vaultTools.js +2 -2
- package/lib/verdicts.mjs +328 -0
- package/lib/wakes.mjs +1 -1
- package/lib/watches.mjs +2 -2
- package/lib/windowAsks.mjs +13 -5
- package/lib/worktrees.mjs +112 -18
- package/npm-shrinkwrap.json +1093 -0
- package/package.json +11 -10
- package/release.json +220 -0
package/lib/ownMachines.mjs
CHANGED
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
openNotice,
|
|
16
16
|
pairClaim,
|
|
17
17
|
pairKey,
|
|
18
|
+
QUEUED_MAX_AGE_MS,
|
|
18
19
|
sameProof,
|
|
19
20
|
sealAnswer,
|
|
20
21
|
sealCall,
|
|
@@ -24,16 +25,40 @@ import {
|
|
|
24
25
|
sealAskAnswer,
|
|
25
26
|
openAskAnswer,
|
|
26
27
|
} from "./machineSeal.mjs";
|
|
27
|
-
import {
|
|
28
|
+
import {
|
|
29
|
+
computerMay,
|
|
30
|
+
findMachine,
|
|
31
|
+
FROM_MACHINE,
|
|
32
|
+
goesHome,
|
|
33
|
+
queueable,
|
|
34
|
+
} from "./machineRoutes.mjs";
|
|
28
35
|
import { MERGE_COMMAND, MIRROR_COMMAND } from "./boardMirror.mjs";
|
|
36
|
+
import { DEVICES_COMMAND } from "./swarmDevices.mjs";
|
|
29
37
|
import { member } from "./members.mjs";
|
|
38
|
+
import { WINDOW_TOOLS } from "./windowAsks.mjs";
|
|
39
|
+
|
|
40
|
+
export { FROM_MACHINE };
|
|
30
41
|
|
|
31
42
|
// A worker's key for a direct link, swapped for home's over the sealed channel (directLink.mjs).
|
|
32
43
|
const DIRECT_COMMAND = "machine.direct";
|
|
33
44
|
// The person's model and effort for an agent live on the computer that runs it (models.mjs reads its own store), so
|
|
34
45
|
// the buddy's Swarm tab reads and sets them on each computer: a worker asks home, home asks the worker that has it.
|
|
35
46
|
const MODELS_COMMAND = "machine.models";
|
|
47
|
+
// A worker passing on the person's choice of another computer as Home (makeHome).
|
|
48
|
+
const HOME_COMMAND = "machine.home";
|
|
49
|
+
// Since when this worker has found home unreachable: { home, since, checked } (watchHome).
|
|
50
|
+
const HOME_AWAY = "home-away";
|
|
51
|
+
// Owner, October 26: a computer with Home switches only once Home has been away about a week. The count survives a
|
|
52
|
+
// restart or a night's sleep (a look at least hourly); a gap of more than a day between looks means this computer was
|
|
53
|
+
// off too and cannot say Home stayed away, so it counts again from then.
|
|
54
|
+
const AWAY_LOOK_MS = 60 * 60_000;
|
|
55
|
+
const AWAY_GAP_MS = 24 * 60 * 60_000;
|
|
36
56
|
const MODEL_COMMAND = "machine.model";
|
|
57
|
+
// A cloud computer's model call through a proxy link, to the computer that holds the link (proxyLinks.mjs).
|
|
58
|
+
const LINK_COMMAND = "machine.link";
|
|
59
|
+
// The person's SSH key let into one of their cloud computers (cloudSsh.mjs): from a computer they drive, through home,
|
|
60
|
+
// to the cloud computer, which takes it only from the home it follows.
|
|
61
|
+
const SSH_COMMAND = "machine.ssh";
|
|
37
62
|
// An agent that runs itself (Hermes, OpenClaw) is shown on a computer it called the board from this recently.
|
|
38
63
|
const SELF_RUN_SEEN_MS = 7 * 24 * 60 * 60_000;
|
|
39
64
|
|
|
@@ -44,16 +69,30 @@ const SELF_RUN_SEEN_MS = 7 * 24 * 60 * 60_000;
|
|
|
44
69
|
// worker's join code. A worker keeps what its agents write while home is out of reach and sends it when home is back.
|
|
45
70
|
// 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
71
|
// person types its code on home, then brings that board to home (boardMirror.mjs bring): it is never a computer that
|
|
47
|
-
// cannot work
|
|
72
|
+
// cannot work: a worker that could not work until home confirmed it would be broken.
|
|
48
73
|
|
|
49
|
-
//
|
|
50
|
-
//
|
|
51
|
-
|
|
74
|
+
// Each computer keeps its own limits on what another of the person's computers may ask of it, and on its own role
|
|
75
|
+
// (security audit 2026-10-25, tier 2): home answers a worker's call only as what that worker is (computerMay), a worker
|
|
76
|
+
// runs what home lists only in a folder it knows (machineRuns.mjs), and a computer moves home, follows another or forgets
|
|
77
|
+
// one only on its own person's word or a sealed statement from a computer it confirmed, never on Harness's list alone.
|
|
52
78
|
|
|
53
79
|
// Wrong join codes a worker takes before it makes a new one: Harness carries the guesses and must not get many.
|
|
54
80
|
const JOIN_TRIES = 5;
|
|
81
|
+
// How long a join code lasts, setup's included: as long as a pairing code (src/server/bridge/pairHub.ts PAIR_TTL_MS),
|
|
82
|
+
// so a code read off a screen or a web page does not open this computer days later. A waiting computer shows a new one.
|
|
83
|
+
const JOIN_CODE_TTL_MS = 10 * 60_000;
|
|
55
84
|
// How long home keeps trying a code typed for a computer that is not online yet: as long as its pairing code lasts.
|
|
56
85
|
const ADD_WAIT_MS = 10 * 60_000;
|
|
86
|
+
// The home this computer follows (its own id while it is home, "" for none): set by the person's join on that home, this
|
|
87
|
+
// computer's own move, or a sealed statement from a computer confirmed here (onRoleStatement). Harness's list names a
|
|
88
|
+
// home; a worker follows it only when it is this one.
|
|
89
|
+
const ACCEPTED = "machine-home";
|
|
90
|
+
// When this computer last changed role or home, so a statement older than that moves nothing.
|
|
91
|
+
const ROLE_AT = "machine-role-at";
|
|
92
|
+
// Sealed notices for the person's other computers that Harness could not take yet (a removal, a statement of home).
|
|
93
|
+
const QUEUED_NOTICES = "machine-notices";
|
|
94
|
+
// Removals Harness has not done yet: tried again once this computer is back on Harness.
|
|
95
|
+
const REMOVALS = "machine-removals";
|
|
57
96
|
const MB = 1024 * 1024;
|
|
58
97
|
// What get_file shows inline on a worker, as on home (boardFileCommands.mjs).
|
|
59
98
|
const INLINE_IMAGE_BYTES = 3.75 * MB;
|
|
@@ -82,10 +121,12 @@ export function createOwnMachines({
|
|
|
82
121
|
answerMirror = null,
|
|
83
122
|
// On home: a stand-in's changes, merged (boardMirror.mjs merge).
|
|
84
123
|
answerMerge = null,
|
|
124
|
+
// On home: another computer's devices, and the person's decisions on them made there (swarmDevices.mjs answer).
|
|
125
|
+
answerDevices = null,
|
|
85
126
|
// The direct links between the person's computers (directLink.mjs); null keeps every message to Harness.
|
|
86
127
|
directLink = null,
|
|
87
|
-
// How long home must be unreachable on every route before this worker stands in as home
|
|
88
|
-
//
|
|
128
|
+
// How long home must be unreachable on every route before this worker stands in as home (daemon.mjs: a week unless
|
|
129
|
+
// the person's config says otherwise); null: it never does.
|
|
89
130
|
standInAfterMs = null,
|
|
90
131
|
// This computer stood in for that one: its own changes from now on go back to it (boardMirror.mjs startStandIn).
|
|
91
132
|
onStoodIn = () => {},
|
|
@@ -105,6 +146,11 @@ export function createOwnMachines({
|
|
|
105
146
|
ok: false,
|
|
106
147
|
error: "this computer cannot list its agents' models",
|
|
107
148
|
}),
|
|
149
|
+
// A proxy link's model call from one of the person's cloud computers, on the computer holding the link
|
|
150
|
+
// (proxyLinks.mjs answer); null where none is held.
|
|
151
|
+
answerLink = null,
|
|
152
|
+
// A cloud computer's own SSH keys (cloudSsh.mjs allow): { key, by } in, null on a computer that takes none.
|
|
153
|
+
ownSsh = null,
|
|
108
154
|
}) {
|
|
109
155
|
// On home: each worker's folders and the repository each holds, as it said them.
|
|
110
156
|
const infos = new Map();
|
|
@@ -113,6 +159,7 @@ export function createOwnMachines({
|
|
|
113
159
|
let siblings = [];
|
|
114
160
|
let selfListed = null;
|
|
115
161
|
let standInTimer = null;
|
|
162
|
+
let askingToStandIn = false;
|
|
116
163
|
let flushing = false;
|
|
117
164
|
// What home last heard of this computer's folders, false until it heard them on this connection.
|
|
118
165
|
let toldHome = false;
|
|
@@ -120,12 +167,23 @@ export function createOwnMachines({
|
|
|
120
167
|
const isHome = () => machine.role !== "worker";
|
|
121
168
|
const homeListed = () =>
|
|
122
169
|
siblings.find((sibling) => sibling.role === "home") ?? null;
|
|
170
|
+
// Before this was kept, a worker followed the home it joined and a home itself.
|
|
171
|
+
const acceptedHome = () =>
|
|
172
|
+
store.meta(ACCEPTED) ??
|
|
173
|
+
(machine.role === "worker" ? store.meta("joined-home") : machine.id);
|
|
174
|
+
const accept = (id) => {
|
|
175
|
+
store.meta(ACCEPTED, id ?? "");
|
|
176
|
+
store.meta(ROLE_AT, String(Date.now()));
|
|
177
|
+
};
|
|
178
|
+
// The home Harness lists, when it is the one this computer follows and holds its key.
|
|
123
179
|
const pinnedHome = () => {
|
|
124
180
|
const home = homeListed();
|
|
125
|
-
return home
|
|
181
|
+
return home && home.id === acceptedHome()
|
|
182
|
+
? store.ownMachine(home.id)
|
|
183
|
+
: null;
|
|
126
184
|
};
|
|
127
185
|
// Whether this computer was confirmed with another of the person's computers (it joined a home, or confirmed a
|
|
128
|
-
// worker)
|
|
186
|
+
// worker). Older servers also need this in hello to preserve Home when the computer reconnects.
|
|
129
187
|
const confirmedWithAnother = () =>
|
|
130
188
|
Boolean(store.meta("joined-home")) || store.ownMachines().length > 0;
|
|
131
189
|
// Whether this worker holds home's key. Just started, before Harness lists the computers, a worker that was ever
|
|
@@ -163,17 +221,58 @@ export function createOwnMachines({
|
|
|
163
221
|
joined: confirmedWithAnother(),
|
|
164
222
|
});
|
|
165
223
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
224
|
+
// The role Harness last gave this computer, and whether it was told why it did not take it.
|
|
225
|
+
let given = null;
|
|
226
|
+
let toldKept = null;
|
|
227
|
+
/** Harness's answer to this computer's hello: its role on the account, taken as reconcileRole allows. */
|
|
228
|
+
const adopt = (answer) => {
|
|
229
|
+
if (!answer || !["home", "worker"].includes(answer.role)) return;
|
|
230
|
+
given = answer.role;
|
|
231
|
+
reconcileRole();
|
|
232
|
+
};
|
|
233
|
+
/**
|
|
234
|
+
* This computer's role, moved only where Harness's word can do no harm (S9): on a computer that trusts no other yet, or
|
|
235
|
+
* a home Harness lists under a home it never confirmed (it then trusts nobody and works on its own until the person
|
|
236
|
+
* joins it there). Otherwise a computer becomes home by its own move (makeHome, standIn) and stops being home on a
|
|
237
|
+
* sealed statement from the computer that is home now (onRoleStatement).
|
|
238
|
+
*/
|
|
239
|
+
const reconcileRole = () => {
|
|
240
|
+
if (!given || given === machine.role) return;
|
|
241
|
+
if (!machine.role || !confirmedWithAnother())
|
|
242
|
+
return setRole(given, given === "home" ? machine.id : "");
|
|
243
|
+
const home = homeListed();
|
|
244
|
+
if (
|
|
245
|
+
given === "worker" &&
|
|
246
|
+
isHome() &&
|
|
247
|
+
home &&
|
|
248
|
+
!home.standInFor &&
|
|
249
|
+
!store.ownMachine(home.id)
|
|
250
|
+
)
|
|
251
|
+
return setRole("worker", "");
|
|
252
|
+
if (toldKept === given) return;
|
|
253
|
+
toldKept = given;
|
|
254
|
+
log(
|
|
255
|
+
given === "home"
|
|
256
|
+
? "Harness lists this computer as home, but nobody here moved it: it stays a worker"
|
|
257
|
+
: `Harness lists ${home?.label || "another computer"} as home, but it has not said so itself: this computer stays home until it does`
|
|
258
|
+
);
|
|
259
|
+
};
|
|
260
|
+
const setRole = (role, followed) => {
|
|
261
|
+
if (machine.role && machine.role !== role)
|
|
170
262
|
log(
|
|
171
|
-
`this computer is now ${
|
|
263
|
+
`this computer is now ${role === "home" ? "home: it holds the board" : "a worker: its agents reach the board on your home computer"}`
|
|
172
264
|
);
|
|
173
|
-
const leftHome = machine.role === "home" &&
|
|
174
|
-
|
|
265
|
+
const leftHome = machine.role === "home" && role === "worker";
|
|
266
|
+
// A stand-in going back hands over what it wrote (boardMirror.mjs handOver); it brings no board of its own.
|
|
267
|
+
const wasStandIn = Boolean(store.meta("standing-in-for"));
|
|
268
|
+
machine.role = role;
|
|
269
|
+
// Setup's code is never a home's join code: a home is not waiting to join anything (M4).
|
|
270
|
+
if (role === "home") machine.joinCode = null;
|
|
175
271
|
saveMachine(machine);
|
|
176
|
-
|
|
272
|
+
accept(followed);
|
|
273
|
+
if (role === "worker") store.meta("standing-in-for", "");
|
|
274
|
+
toldKept = null;
|
|
275
|
+
if (leftHome && !wasStandIn) onOwnBoard();
|
|
177
276
|
changed();
|
|
178
277
|
};
|
|
179
278
|
|
|
@@ -201,7 +300,10 @@ export function createOwnMachines({
|
|
|
201
300
|
typeof listed.standInFor === "string" ? listed.standInFor : null,
|
|
202
301
|
}));
|
|
203
302
|
changed();
|
|
303
|
+
reconcileRole();
|
|
204
304
|
retryAdding();
|
|
305
|
+
void sendQueued();
|
|
306
|
+
void retryRemovals();
|
|
205
307
|
watchHome();
|
|
206
308
|
if (actsAlone()) void workAlone();
|
|
207
309
|
// Someone stood in for this computer: it takes home back, at once if the stand-in is away too (boardMirror.mjs).
|
|
@@ -238,27 +340,53 @@ export function createOwnMachines({
|
|
|
238
340
|
(link) => link.id === home.id && link.linked
|
|
239
341
|
);
|
|
240
342
|
};
|
|
241
|
-
//
|
|
242
|
-
//
|
|
343
|
+
// Since when this worker has found home unreachable, kept on disk so a restart or the computer sleeping does not start
|
|
344
|
+
// the count again. Each look at home is noted too: a gap longer than AWAY_GAP_MS between two looks means this computer
|
|
345
|
+
// was off and cannot say home stayed away meanwhile, so the count starts again from now.
|
|
346
|
+
const awaySince = (homeId) => {
|
|
347
|
+
const now = Date.now();
|
|
348
|
+
let away = null;
|
|
349
|
+
try {
|
|
350
|
+
away = JSON.parse(store.meta(HOME_AWAY) || "null");
|
|
351
|
+
} catch {}
|
|
352
|
+
const since =
|
|
353
|
+
away?.home === homeId &&
|
|
354
|
+
Number.isFinite(away.since) &&
|
|
355
|
+
now - away.checked <= AWAY_GAP_MS
|
|
356
|
+
? away.since
|
|
357
|
+
: now;
|
|
358
|
+
store.meta(
|
|
359
|
+
HOME_AWAY,
|
|
360
|
+
JSON.stringify({ home: homeId, since, checked: now })
|
|
361
|
+
);
|
|
362
|
+
return since;
|
|
363
|
+
};
|
|
364
|
+
// Home gone on every route (Harness lists it offline and no direct link answers): once it has been away for the
|
|
365
|
+
// person's deadline this worker asks Harness to let it stand in. Home back on any route first, the count ends.
|
|
243
366
|
const watchHome = () => {
|
|
367
|
+
clearTimeout(standInTimer);
|
|
368
|
+
standInTimer = null;
|
|
244
369
|
const waiting =
|
|
245
370
|
standInAfterMs !== null &&
|
|
246
371
|
!isHome() &&
|
|
247
372
|
Boolean(pinnedHome()) &&
|
|
248
373
|
homeUnreachable();
|
|
249
374
|
if (!waiting) {
|
|
250
|
-
|
|
251
|
-
standInTimer = null;
|
|
375
|
+
if (store.meta(HOME_AWAY)) store.meta(HOME_AWAY, "");
|
|
252
376
|
return;
|
|
253
377
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
378
|
+
const left = awaySince(homeListed().id) + standInAfterMs - Date.now();
|
|
379
|
+
if (left <= 0) {
|
|
380
|
+
if (!askingToStandIn) {
|
|
381
|
+
askingToStandIn = true;
|
|
382
|
+
void standIn().finally(() => (askingToStandIn = false));
|
|
383
|
+
}
|
|
384
|
+
// Refused (another computer stood in first, or Harness was out of reach): asked again at the next look.
|
|
385
|
+
standInTimer = setTimeout(watchHome, AWAY_LOOK_MS);
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
// Looked at again at least hourly, so the look is noted and a computer that slept counts from its own clock.
|
|
389
|
+
standInTimer = setTimeout(watchHome, Math.min(left, AWAY_LOOK_MS));
|
|
262
390
|
};
|
|
263
391
|
const standIn = async () => {
|
|
264
392
|
const answer = await ask("machine_role", {
|
|
@@ -266,17 +394,25 @@ export function createOwnMachines({
|
|
|
266
394
|
standIn: true,
|
|
267
395
|
}).catch(() => null);
|
|
268
396
|
if (answer?.ok !== true || typeof answer.standInFor !== "string") return;
|
|
397
|
+
store.meta(HOME_AWAY, "");
|
|
269
398
|
onStoodIn(answer.standInFor);
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
399
|
+
setRole("home", machine.id);
|
|
400
|
+
store.meta("standing-in-for", answer.standInFor);
|
|
401
|
+
// The computer it stands in for hears it from this one, sealed: only then may this one hand its writes back there.
|
|
402
|
+
queueNotice(answer.standInFor, {
|
|
403
|
+
role: {
|
|
404
|
+
home: machine.id,
|
|
405
|
+
at: Date.now(),
|
|
406
|
+
standInFor: answer.standInFor,
|
|
407
|
+
},
|
|
408
|
+
});
|
|
273
409
|
await applyKept(answer.standInFor);
|
|
274
410
|
log(
|
|
275
411
|
`your home computer${labelOf(answer.standInFor) ? ` (${labelOf(answer.standInFor)})` : ""} is unreachable; this computer stands in as home until it is back`
|
|
276
412
|
);
|
|
277
413
|
};
|
|
278
414
|
// 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 (
|
|
415
|
+
// holds, so it goes back with the rest of what it wrote (it once waited the whole stand-in).
|
|
280
416
|
const applyKept = async (formerHome) => {
|
|
281
417
|
const pinned = store.ownMachine(formerHome);
|
|
282
418
|
const kept = outbox();
|
|
@@ -318,22 +454,103 @@ export function createOwnMachines({
|
|
|
318
454
|
}
|
|
319
455
|
};
|
|
320
456
|
|
|
321
|
-
/**
|
|
457
|
+
/**
|
|
458
|
+
* Home taking its place back from the computer that stood in, once it has that computer's changes. This computer stayed
|
|
459
|
+
* home all along (reconcileRole); Harness routes the swarm here again, and the stand-in hears it from this computer,
|
|
460
|
+
* sealed, so it goes back to being a worker.
|
|
461
|
+
*/
|
|
322
462
|
const reclaim = async () => {
|
|
323
|
-
//
|
|
324
|
-
|
|
325
|
-
|
|
463
|
+
// The computer Harness lists standing in may hand back what it wrote, sealed, once (answerMergeCall): Harness's list
|
|
464
|
+
// and that computer's own sealed pages together, as with its statement (onRoleStatement), never either alone.
|
|
465
|
+
const standing = someoneStandsIn() ? homeListed().id : null;
|
|
326
466
|
const answer = await ask("machine_role", {
|
|
327
467
|
role: "home",
|
|
328
468
|
reclaim: true,
|
|
329
469
|
}).catch(() => null);
|
|
330
470
|
if (answer?.ok !== true) return false;
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
471
|
+
if (standing && store.ownMachine(standing))
|
|
472
|
+
store.meta("stood-in-by", standing);
|
|
473
|
+
setRole("home", machine.id);
|
|
474
|
+
announceHome();
|
|
334
475
|
return true;
|
|
335
476
|
};
|
|
336
477
|
|
|
478
|
+
// ---- what this computer says to the others, sealed, and keeps until Harness takes it ----
|
|
479
|
+
|
|
480
|
+
/** This computer is home: each computer confirmed here hears it from this one, not from Harness's list. */
|
|
481
|
+
const announceHome = () => {
|
|
482
|
+
const statement = { role: { home: machine.id, at: Date.now() } };
|
|
483
|
+
for (const pinned of store.ownMachines())
|
|
484
|
+
queueNotice(pinned.machine_id, statement);
|
|
485
|
+
};
|
|
486
|
+
const queuedNotices = () => {
|
|
487
|
+
try {
|
|
488
|
+
return JSON.parse(store.meta(QUEUED_NOTICES) || "[]");
|
|
489
|
+
} catch {
|
|
490
|
+
return [];
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
const queueNotice = (to, notice) => {
|
|
494
|
+
const kept = [...queuedNotices(), { to, notice, at: Date.now() }];
|
|
495
|
+
store.meta(QUEUED_NOTICES, JSON.stringify(kept.slice(-200)));
|
|
496
|
+
void sendQueued();
|
|
497
|
+
};
|
|
498
|
+
let sending = false;
|
|
499
|
+
let sendAgain = false;
|
|
500
|
+
// A notice stays here until the other computer took it, and no longer than a queued call lives.
|
|
501
|
+
const sendQueued = async () => {
|
|
502
|
+
if (sending) {
|
|
503
|
+
sendAgain = true;
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
if (!isHome() || !queuedNotices().length) return;
|
|
507
|
+
sending = true;
|
|
508
|
+
sendAgain = false;
|
|
509
|
+
try {
|
|
510
|
+
const done = new Set();
|
|
511
|
+
const parked = new Set();
|
|
512
|
+
for (const entry of queuedNotices()) {
|
|
513
|
+
const key = `${entry.to}:${entry.at}`;
|
|
514
|
+
if (
|
|
515
|
+
Date.now() - entry.at > QUEUED_MAX_AGE_MS ||
|
|
516
|
+
!store.ownMachine(entry.to)
|
|
517
|
+
) {
|
|
518
|
+
done.add(key);
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
// Harness keeps one already for that computer while it is offline: nothing more to send until it is back.
|
|
522
|
+
const away =
|
|
523
|
+
siblings.find((sibling) => sibling.id === entry.to)?.connected ===
|
|
524
|
+
false;
|
|
525
|
+
if (entry.parked && away) continue;
|
|
526
|
+
// Done once that computer took it. Kept by Harness for an offline computer, or refused (Harness still listing
|
|
527
|
+
// this computer in its old role, just before it dials again), it is sent again with the next list of computers:
|
|
528
|
+
// Harness hands a kept notice over only when that computer next says hello, which may have just passed. Each
|
|
529
|
+
// notice counts once there (onRoleStatement, removedByHome).
|
|
530
|
+
const answer = await noticeTo(entry.to, entry.notice);
|
|
531
|
+
if (answer?.ok === true && !answer.queued) done.add(key);
|
|
532
|
+
else if (answer?.queued) parked.add(key);
|
|
533
|
+
}
|
|
534
|
+
// Notices queued while these were sent stay for the next round.
|
|
535
|
+
store.meta(
|
|
536
|
+
QUEUED_NOTICES,
|
|
537
|
+
JSON.stringify(
|
|
538
|
+
queuedNotices()
|
|
539
|
+
.filter((entry) => !done.has(`${entry.to}:${entry.at}`))
|
|
540
|
+
.map((entry) =>
|
|
541
|
+
parked.has(`${entry.to}:${entry.at}`)
|
|
542
|
+
? { ...entry, parked: true }
|
|
543
|
+
: entry
|
|
544
|
+
)
|
|
545
|
+
)
|
|
546
|
+
);
|
|
547
|
+
} finally {
|
|
548
|
+
sending = false;
|
|
549
|
+
// Asked again while these went (a new list of computers, a new notice): once more, for what is left.
|
|
550
|
+
if (sendAgain) void sendQueued();
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
|
|
337
554
|
/** Uninstalled: Harness takes this computer off the account, and the pairing it connects with stops working. */
|
|
338
555
|
const leave = async () => {
|
|
339
556
|
const answer = await ask("machine_leave", {}).catch((error) => ({
|
|
@@ -345,25 +562,97 @@ export function createOwnMachines({
|
|
|
345
562
|
: { ok: false, error: answer?.error || "Harness did not answer" };
|
|
346
563
|
};
|
|
347
564
|
|
|
348
|
-
/**
|
|
349
|
-
|
|
350
|
-
|
|
565
|
+
/**
|
|
566
|
+
* The person takes another of their computers off the account (the Swarm map's Remove, npx tryharness machines remove).
|
|
567
|
+
* Never home. On home it is done here first, whatever Harness says (M5): the computer's key and direct link are
|
|
568
|
+
* forgotten, and every computer confirmed here hears it, sealed from home; Harness then takes it off the account and
|
|
569
|
+
* ends its pairing, now or once this computer is back on Harness.
|
|
570
|
+
*/
|
|
571
|
+
const remove = async (named) => {
|
|
572
|
+
const target =
|
|
573
|
+
findMachine(all(), named)?.id ??
|
|
574
|
+
store.ownMachines().find((pinned) => pinned.label === named)
|
|
575
|
+
?.machine_id ??
|
|
576
|
+
String(named || "");
|
|
577
|
+
if (!target) return { ok: false, error: "which computer?" };
|
|
578
|
+
if (target === machine.id)
|
|
579
|
+
return {
|
|
580
|
+
ok: false,
|
|
581
|
+
error: "uninstall Harness to take this computer off",
|
|
582
|
+
};
|
|
351
583
|
const listed = siblings.find((sibling) => sibling.id === target);
|
|
352
584
|
if (listed?.role === "home")
|
|
353
585
|
return {
|
|
354
586
|
ok: false,
|
|
355
587
|
error: "Home keeps the board: make another computer home first.",
|
|
356
588
|
};
|
|
589
|
+
const here = isHome() && Boolean(store.ownMachine(target));
|
|
590
|
+
if (here) {
|
|
591
|
+
const label = labelOf(target) ?? target;
|
|
592
|
+
forget(target);
|
|
593
|
+
for (const pinned of store.ownMachines())
|
|
594
|
+
queueNotice(pinned.machine_id, {
|
|
595
|
+
removed: target,
|
|
596
|
+
at: Date.now(),
|
|
597
|
+
});
|
|
598
|
+
log(`${label} was removed from your swarm on this computer`);
|
|
599
|
+
}
|
|
357
600
|
const answer = await ask("machine_remove", { machineId: target }).catch(
|
|
358
|
-
(error) => ({ ok: false, error: error.message })
|
|
601
|
+
(error) => ({ ok: false, error: error.message, unreached: true })
|
|
359
602
|
);
|
|
360
|
-
if (answer?.removed
|
|
603
|
+
if (answer?.removed === true) {
|
|
604
|
+
removed({ machineId: target });
|
|
605
|
+
return { ok: true };
|
|
606
|
+
}
|
|
607
|
+
if (!here)
|
|
361
608
|
return { ok: false, error: answer?.error || "Harness did not answer" };
|
|
362
|
-
|
|
363
|
-
return {
|
|
609
|
+
if (answer?.unreached || !answer) keepRemoval(target);
|
|
610
|
+
return {
|
|
611
|
+
ok: true,
|
|
612
|
+
note: answer?.unreached
|
|
613
|
+
? "Harness is out of reach: it takes the computer off your account once this computer is back on it."
|
|
614
|
+
: `Harness did not take it off your account (${answer?.error || "no answer"}); this computer no longer trusts it.`,
|
|
615
|
+
};
|
|
616
|
+
};
|
|
617
|
+
const forget = (id) => {
|
|
618
|
+
store.unpinOwnMachine(id);
|
|
619
|
+
directLink?.disallow?.(id);
|
|
620
|
+
infos.delete(id);
|
|
621
|
+
store.meta(`bring-armed:${id}`, "");
|
|
622
|
+
if (store.meta("stood-in-by") === id) store.meta("stood-in-by", "");
|
|
623
|
+
changed();
|
|
624
|
+
};
|
|
625
|
+
const removals = () => {
|
|
626
|
+
try {
|
|
627
|
+
return JSON.parse(store.meta(REMOVALS) || "[]");
|
|
628
|
+
} catch {
|
|
629
|
+
return [];
|
|
630
|
+
}
|
|
631
|
+
};
|
|
632
|
+
const keepRemoval = (id) =>
|
|
633
|
+
store.meta(REMOVALS, JSON.stringify([...new Set([...removals(), id])]));
|
|
634
|
+
let removing = false;
|
|
635
|
+
const retryRemovals = async () => {
|
|
636
|
+
if (removing || !removals().length) return;
|
|
637
|
+
removing = true;
|
|
638
|
+
try {
|
|
639
|
+
for (const id of removals()) {
|
|
640
|
+
const answer = await ask("machine_remove", { machineId: id }).catch(
|
|
641
|
+
() => null
|
|
642
|
+
);
|
|
643
|
+
if (!answer) return;
|
|
644
|
+
store.meta(
|
|
645
|
+
REMOVALS,
|
|
646
|
+
JSON.stringify(removals().filter((each) => each !== id))
|
|
647
|
+
);
|
|
648
|
+
if (answer.removed === true) removed({ machineId: id });
|
|
649
|
+
}
|
|
650
|
+
} finally {
|
|
651
|
+
removing = false;
|
|
652
|
+
}
|
|
364
653
|
};
|
|
365
654
|
|
|
366
|
-
/** Harness says a computer was taken off the account:
|
|
655
|
+
/** Harness says a computer was taken off the account: it is no longer listed, and its key is forgotten here. */
|
|
367
656
|
const removed = (message) => {
|
|
368
657
|
const id = String(message?.machineId || "");
|
|
369
658
|
if (id === machine.id) {
|
|
@@ -372,50 +661,171 @@ export function createOwnMachines({
|
|
|
372
661
|
);
|
|
373
662
|
return;
|
|
374
663
|
}
|
|
375
|
-
if (!store.ownMachine(id)) return;
|
|
376
|
-
store.unpinOwnMachine(id);
|
|
377
664
|
siblings = siblings.filter((sibling) => sibling.id !== id);
|
|
378
|
-
|
|
665
|
+
if (store.ownMachine(id)) {
|
|
666
|
+
forget(id);
|
|
667
|
+
log(`${labelOf(id) ?? id} was removed from your swarm`);
|
|
668
|
+
}
|
|
379
669
|
changed();
|
|
380
670
|
};
|
|
381
671
|
|
|
382
|
-
/**
|
|
383
|
-
const
|
|
672
|
+
/** On a worker: home took a computer off the swarm, sealed. This one, or another this computer trusts. */
|
|
673
|
+
const removedByHome = (id) => {
|
|
674
|
+
if (id === machine.id) {
|
|
675
|
+
const home = acceptedHome();
|
|
676
|
+
if (home) forget(home);
|
|
677
|
+
accept("");
|
|
678
|
+
log(
|
|
679
|
+
"your home computer removed this computer from your swarm: it works on its own until you add it again"
|
|
680
|
+
);
|
|
681
|
+
changed();
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
if (!store.ownMachine(id)) return;
|
|
685
|
+
forget(id);
|
|
686
|
+
log(
|
|
687
|
+
`${labelOf(id) ?? id} was removed from your swarm on your home computer`
|
|
688
|
+
);
|
|
689
|
+
};
|
|
690
|
+
|
|
691
|
+
/**
|
|
692
|
+
* This computer takes Home, as the person asked. The board stays on the computer that had it. `computers`: the
|
|
693
|
+
* person's other computers' keys, as the Home before this one pinned them, so they stay confirmed here.
|
|
694
|
+
*/
|
|
695
|
+
const becomeHome = async (computers = []) => {
|
|
696
|
+
for (const other of Array.isArray(computers) ? computers : [])
|
|
697
|
+
if (
|
|
698
|
+
typeof other?.id === "string" &&
|
|
699
|
+
other.id !== machine.id &&
|
|
700
|
+
typeof other.publicKey === "string"
|
|
701
|
+
)
|
|
702
|
+
store.pinOwnMachine(
|
|
703
|
+
other.id,
|
|
704
|
+
other.publicKey,
|
|
705
|
+
String(other.label || "")
|
|
706
|
+
);
|
|
707
|
+
if (isHome() && !standingIn()) return { ok: true, before: null };
|
|
384
708
|
const answer = await ask("machine_role", { role: "home" });
|
|
385
709
|
if (!answer?.ok)
|
|
386
710
|
return { ok: false, error: answer?.error || "Harness did not answer" };
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
711
|
+
const before = answer.before ?? null;
|
|
712
|
+
// The computer that was home brings its board here once it hears this one is home (boardMirror.mjs bring).
|
|
713
|
+
if (before) store.meta(`bring-armed:${before}`, "1");
|
|
714
|
+
setRole("home", machine.id);
|
|
715
|
+
announceHome();
|
|
716
|
+
return { ok: true, before };
|
|
717
|
+
};
|
|
718
|
+
/**
|
|
719
|
+
* The person makes one of their computers Home: this one, or another, which is asked over the sealed line between
|
|
720
|
+
* the person's computers (through home when this is a worker) and moves Home itself. Harness only records it.
|
|
721
|
+
*/
|
|
722
|
+
const makeHome = async (id = null) => {
|
|
723
|
+
const target = String(id || machine.id);
|
|
724
|
+
if (target === machine.id && isHome()) return becomeHome();
|
|
725
|
+
// A worker becoming Home asks through Home while it can, so the person's other computers come along confirmed.
|
|
726
|
+
if (target === machine.id) {
|
|
727
|
+
const pinned = homeListed()?.connected ? pinnedHome() : null;
|
|
728
|
+
const routed = pinned
|
|
729
|
+
? await callOnce(pinned, {
|
|
730
|
+
command: HOME_COMMAND,
|
|
731
|
+
agent: "user",
|
|
732
|
+
target,
|
|
733
|
+
}).catch(() => null)
|
|
734
|
+
: null;
|
|
735
|
+
return routed?.ok === true ? routed : becomeHome();
|
|
736
|
+
}
|
|
737
|
+
const listed = siblings.find((sibling) => sibling.id === target);
|
|
738
|
+
if (!listed)
|
|
739
|
+
return { ok: false, error: "that is not one of your computers" };
|
|
740
|
+
if (!store.ownMachine(target))
|
|
741
|
+
return {
|
|
742
|
+
ok: false,
|
|
743
|
+
error: `${listed.label || "That computer"} has not joined your swarm yet`,
|
|
744
|
+
};
|
|
745
|
+
if (isHome()) return handHome(target, listed.label);
|
|
746
|
+
const pinned = pinnedHome();
|
|
747
|
+
if (!pinned) return homeListed() ? notJoined() : offline();
|
|
748
|
+
return (
|
|
749
|
+
(await callOnce(pinned, {
|
|
750
|
+
command: HOME_COMMAND,
|
|
751
|
+
agent: "user",
|
|
752
|
+
target,
|
|
753
|
+
})) ?? offline()
|
|
754
|
+
);
|
|
755
|
+
};
|
|
756
|
+
|
|
757
|
+
/**
|
|
758
|
+
* On home: Home goes to another of the person's computers. The others hear its key first (once it is Home this one can
|
|
759
|
+
* no longer send them notices), then it is given their keys and takes Home, so nobody has to be added again.
|
|
760
|
+
*/
|
|
761
|
+
const handHome = async (target, label) => {
|
|
762
|
+
const pinned = store.ownMachine(target);
|
|
763
|
+
const others = store
|
|
764
|
+
.ownMachines()
|
|
765
|
+
.filter((other) => other.machine_id !== target);
|
|
766
|
+
for (const other of others)
|
|
767
|
+
await noticeTo(other.machine_id, {
|
|
768
|
+
homeNext: {
|
|
769
|
+
id: target,
|
|
770
|
+
publicKey: pinned.public_key,
|
|
771
|
+
label: pinned.label || label || "",
|
|
772
|
+
},
|
|
773
|
+
});
|
|
774
|
+
return (
|
|
775
|
+
(await askWorker(target, {
|
|
776
|
+
home: true,
|
|
777
|
+
computers: others.map((other) => ({
|
|
778
|
+
id: other.machine_id,
|
|
779
|
+
publicKey: other.public_key,
|
|
780
|
+
label: other.label,
|
|
781
|
+
})),
|
|
782
|
+
})) ?? {
|
|
783
|
+
ok: false,
|
|
784
|
+
error: `${label || "That computer"} did not answer; it may be asleep or offline`,
|
|
785
|
+
}
|
|
786
|
+
);
|
|
391
787
|
};
|
|
392
788
|
|
|
393
789
|
// ---- the join: the worker's code typed on home pins each computer's key on the other ----
|
|
394
790
|
|
|
395
|
-
// Setup's code becomes this computer's join code, in place of any made before (pairing.mjs keepJoinCode)
|
|
791
|
+
// Setup's code becomes this computer's join code, in place of any made before (pairing.mjs keepJoinCode), for what is
|
|
792
|
+
// left of its time; a home drops it, since it waits to join nothing (M4).
|
|
396
793
|
const adoptSetupCode = () => {
|
|
397
794
|
if (!machine.joinCode) return;
|
|
398
795
|
const code = normalizeJoinCode(machine.joinCode);
|
|
399
|
-
|
|
796
|
+
const made = Number(machine.joinCodeAt) || Date.now();
|
|
797
|
+
if (code && machine.role !== "home")
|
|
798
|
+
store.meta(
|
|
799
|
+
"machine-join",
|
|
800
|
+
JSON.stringify({ code, wrong: 0, until: made + JOIN_CODE_TTL_MS })
|
|
801
|
+
);
|
|
400
802
|
machine.joinCode = null;
|
|
803
|
+
machine.joinCodeAt = null;
|
|
401
804
|
saveMachine(machine);
|
|
402
805
|
};
|
|
806
|
+
// The code this computer waits to join with, while it lasts; a code kept before codes expired is spent.
|
|
403
807
|
const joinState = () => {
|
|
404
808
|
adoptSetupCode();
|
|
405
809
|
try {
|
|
406
|
-
|
|
810
|
+
const saved = JSON.parse(store.meta("machine-join") || "null");
|
|
811
|
+
return saved?.code && Date.now() < Number(saved.until) ? saved : null;
|
|
407
812
|
} catch {
|
|
408
813
|
return null;
|
|
409
814
|
}
|
|
410
815
|
};
|
|
411
816
|
const joinCode = () => {
|
|
412
|
-
if (
|
|
817
|
+
if (!waitingToJoin()) return null;
|
|
413
818
|
const saved = joinState();
|
|
414
819
|
if (saved?.code) return saved.code;
|
|
415
820
|
const code = newJoinCode();
|
|
416
|
-
store.meta(
|
|
821
|
+
store.meta(
|
|
822
|
+
"machine-join",
|
|
823
|
+
JSON.stringify({ code, wrong: 0, until: Date.now() + JOIN_CODE_TTL_MS })
|
|
824
|
+
);
|
|
417
825
|
return code;
|
|
418
826
|
};
|
|
827
|
+
/** A worker that follows no home here: the only computer a join is for. Never home, never one already joined. */
|
|
828
|
+
const waitingToJoin = () => !isHome() && !pinnedHome();
|
|
419
829
|
const confirmed = (sibling) => {
|
|
420
830
|
const pinned = store.ownMachine(sibling.id);
|
|
421
831
|
return Boolean(
|
|
@@ -433,24 +843,32 @@ export function createOwnMachines({
|
|
|
433
843
|
"That is not a code from the computer you are adding. It looks like ABCD-EFGH-JKLM-NPQR.",
|
|
434
844
|
});
|
|
435
845
|
|
|
436
|
-
/**
|
|
437
|
-
|
|
846
|
+
/**
|
|
847
|
+
* On home: the person typed a worker's join code. `hosted`: it is a computer the person does not drive from (a cloud
|
|
848
|
+
* computer, set up for agents only), so nothing it sends acts as the person (computerMay). A computer added by its
|
|
849
|
+
* code typed here is one the person drives from.
|
|
850
|
+
*/
|
|
851
|
+
const confirm = async (typed, { hosted = false } = {}) => {
|
|
438
852
|
if (!isHome()) return notHome();
|
|
439
853
|
const code = normalizeJoinCode(typed);
|
|
440
854
|
if (!code) return badCode();
|
|
441
|
-
return tryCode(code, new Set());
|
|
855
|
+
return tryCode(code, new Set(), { hosted });
|
|
442
856
|
};
|
|
443
857
|
|
|
444
858
|
// 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
|
-
|
|
859
|
+
// not online yet must not be guessed at the others again and again. `only.machine` and `only.key` name the one computer
|
|
860
|
+
// the code is for (a cloud computer, added from the person's window), so the code is tried on no other; `only.hosted`
|
|
861
|
+
// marks it as one Harness runs, kept on its pin for what home lets it do (a computer Harness runs also says so itself).
|
|
862
|
+
const tryCode = async (code, tried, only = null) => {
|
|
447
863
|
const waiting = siblings.filter(
|
|
448
864
|
(sibling) =>
|
|
449
865
|
sibling.role === "worker" &&
|
|
450
866
|
sibling.connected &&
|
|
451
867
|
sibling.publicKey &&
|
|
452
868
|
!confirmed(sibling) &&
|
|
453
|
-
!tried.has(sibling.id)
|
|
869
|
+
!tried.has(sibling.id) &&
|
|
870
|
+
(!only?.machine ||
|
|
871
|
+
(sibling.id === only.machine && sibling.publicKey === only.key))
|
|
454
872
|
);
|
|
455
873
|
const homeKey = identity().publicKey;
|
|
456
874
|
for (const worker of waiting) {
|
|
@@ -464,12 +882,12 @@ export function createOwnMachines({
|
|
|
464
882
|
const answer = await ask("machine_relay", {
|
|
465
883
|
to: worker.id,
|
|
466
884
|
kind: "join",
|
|
467
|
-
body: { homeKey, proof: joinProof(code, "home", keys) },
|
|
885
|
+
body: { homeKey, proof: await joinProof(code, "home", keys) },
|
|
468
886
|
}).catch((error) => ({ ok: false, error: error.message }));
|
|
469
887
|
if (
|
|
470
888
|
!answer?.ok ||
|
|
471
889
|
typeof answer.proof !== "string" ||
|
|
472
|
-
!sameProof(answer.proof, joinProof(code, "worker", keys))
|
|
890
|
+
!sameProof(answer.proof, await joinProof(code, "worker", keys))
|
|
473
891
|
) {
|
|
474
892
|
log(
|
|
475
893
|
`${worker.label || worker.id} did not join: ${answer?.ok ? "its proof did not match this computer's" : answer?.error}`
|
|
@@ -479,8 +897,15 @@ export function createOwnMachines({
|
|
|
479
897
|
tried.delete(worker.id);
|
|
480
898
|
continue;
|
|
481
899
|
}
|
|
482
|
-
|
|
483
|
-
|
|
900
|
+
const hosted = only?.hosted === true || answer.hosted === true;
|
|
901
|
+
store.pinOwnMachine(worker.id, worker.publicKey, worker.label, {
|
|
902
|
+
hosted,
|
|
903
|
+
});
|
|
904
|
+
// It may bring the board it kept on its own here, once (boardMirror.mjs bring, answerCall).
|
|
905
|
+
store.meta(`bring-armed:${worker.id}`, "1");
|
|
906
|
+
log(
|
|
907
|
+
`confirmed ${worker.label || worker.id} as one of your computers${hosted ? " (run by Harness, for agents only)" : ""}`
|
|
908
|
+
);
|
|
484
909
|
// Its first calls may have come before the pin and been refused: it starts over now, sealed, as one of ours.
|
|
485
910
|
void noticeTo(worker.id, { welcome: true });
|
|
486
911
|
changed();
|
|
@@ -513,7 +938,7 @@ export function createOwnMachines({
|
|
|
513
938
|
}
|
|
514
939
|
entry.trying = true;
|
|
515
940
|
try {
|
|
516
|
-
const answer = await tryCode(entry.code, entry.tried);
|
|
941
|
+
const answer = await tryCode(entry.code, entry.tried, entry.only);
|
|
517
942
|
if (answer.ok) {
|
|
518
943
|
Object.assign(entry, {
|
|
519
944
|
state: "joined",
|
|
@@ -532,12 +957,12 @@ export function createOwnMachines({
|
|
|
532
957
|
* still waits for one), and home confirms it into the swarm as soon as it is online. A code Harness had nothing
|
|
533
958
|
* waiting for is one already approved in a browser: it is tried now, on the computers waiting to join.
|
|
534
959
|
*/
|
|
535
|
-
const add = async (typed) => {
|
|
960
|
+
const add = async (typed, only = null) => {
|
|
536
961
|
const code = normalizeJoinCode(typed);
|
|
537
962
|
if (!code) return badCode();
|
|
538
963
|
let approved = null;
|
|
539
964
|
if (isPairCode(code)) {
|
|
540
|
-
const answer = await ask("machine_pair", pairClaim(code)).catch(
|
|
965
|
+
const answer = await ask("machine_pair", await pairClaim(code)).catch(
|
|
541
966
|
(error) => ({ ok: false, error: error.message })
|
|
542
967
|
);
|
|
543
968
|
if (!answer?.ok)
|
|
@@ -554,7 +979,7 @@ export function createOwnMachines({
|
|
|
554
979
|
}
|
|
555
980
|
: notHome();
|
|
556
981
|
if (approved === null) {
|
|
557
|
-
const answer = await tryCode(code, new Set());
|
|
982
|
+
const answer = await tryCode(code, new Set(), only);
|
|
558
983
|
return answer.ok ? { ...answer, state: "joined" } : answer;
|
|
559
984
|
}
|
|
560
985
|
const entry = {
|
|
@@ -564,6 +989,7 @@ export function createOwnMachines({
|
|
|
564
989
|
machine: approved,
|
|
565
990
|
until: Date.now() + ADD_WAIT_MS,
|
|
566
991
|
tried: new Set(),
|
|
992
|
+
only,
|
|
567
993
|
};
|
|
568
994
|
adding.set(entry.id, entry);
|
|
569
995
|
await tryAdding(entry);
|
|
@@ -590,10 +1016,10 @@ export function createOwnMachines({
|
|
|
590
1016
|
for (const entry of adding.values()) void tryAdding(entry);
|
|
591
1017
|
};
|
|
592
1018
|
|
|
593
|
-
/** On a worker: home asks to join with the code the person typed there. */
|
|
594
|
-
const onJoin = (body) => {
|
|
1019
|
+
/** On a worker waiting to join: home asks to join with the code the person typed there. */
|
|
1020
|
+
const onJoin = async (body) => {
|
|
595
1021
|
const home = homeListed();
|
|
596
|
-
const saved = joinState();
|
|
1022
|
+
const saved = waitingToJoin() ? joinState() : null;
|
|
597
1023
|
if (!home || !saved?.code || typeof body?.homeKey !== "string")
|
|
598
1024
|
return { ok: false, error: "this computer is not waiting to join" };
|
|
599
1025
|
const keys = {
|
|
@@ -602,13 +1028,19 @@ export function createOwnMachines({
|
|
|
602
1028
|
workerId: machine.id,
|
|
603
1029
|
workerKey: identity().publicKey,
|
|
604
1030
|
};
|
|
605
|
-
if (
|
|
1031
|
+
if (
|
|
1032
|
+
!sameProof(String(body.proof), await joinProof(saved.code, "home", keys))
|
|
1033
|
+
) {
|
|
606
1034
|
const wrong = Number(saved.wrong || 0) + 1;
|
|
607
1035
|
store.meta(
|
|
608
1036
|
"machine-join",
|
|
609
1037
|
JSON.stringify(
|
|
610
1038
|
wrong >= JOIN_TRIES
|
|
611
|
-
? {
|
|
1039
|
+
? {
|
|
1040
|
+
code: newJoinCode(),
|
|
1041
|
+
wrong: 0,
|
|
1042
|
+
until: Date.now() + JOIN_CODE_TTL_MS,
|
|
1043
|
+
}
|
|
612
1044
|
: { ...saved, wrong }
|
|
613
1045
|
)
|
|
614
1046
|
);
|
|
@@ -618,15 +1050,24 @@ export function createOwnMachines({
|
|
|
618
1050
|
error: "that is not this computer's code",
|
|
619
1051
|
};
|
|
620
1052
|
}
|
|
1053
|
+
// A new home in place of one this computer trusted before: the old one is not trusted any more (M5).
|
|
1054
|
+
const before = acceptedHome();
|
|
1055
|
+
if (before && before !== home.id && before !== machine.id) forget(before);
|
|
621
1056
|
store.pinOwnMachine(home.id, body.homeKey, home.label);
|
|
622
1057
|
store.meta("joined-home", home.id);
|
|
623
1058
|
store.meta("machine-join", "null");
|
|
1059
|
+
accept(home.id);
|
|
624
1060
|
log(
|
|
625
1061
|
`joined your swarm: ${home.label || "your home computer"} holds the board`
|
|
626
1062
|
);
|
|
627
1063
|
changed();
|
|
628
1064
|
void flush();
|
|
629
|
-
|
|
1065
|
+
// A computer Harness runs says so (cloudBoot.mjs), so home keeps it as hosted however its code was typed.
|
|
1066
|
+
return {
|
|
1067
|
+
ok: true,
|
|
1068
|
+
proof: await joinProof(saved.code, "worker", keys),
|
|
1069
|
+
...(machine.hosted === true ? { hosted: true } : {}),
|
|
1070
|
+
};
|
|
630
1071
|
};
|
|
631
1072
|
|
|
632
1073
|
// ---- a worker's board calls, sealed to home ----
|
|
@@ -663,11 +1104,12 @@ export function createOwnMachines({
|
|
|
663
1104
|
});
|
|
664
1105
|
return { key, requestId, envelope };
|
|
665
1106
|
};
|
|
666
|
-
|
|
1107
|
+
// One call's answer and whether home sealed it; null when home could not be reached.
|
|
1108
|
+
const deliverSealed = async (key, requestId, envelope, to = "home") => {
|
|
667
1109
|
let answer;
|
|
668
1110
|
try {
|
|
669
1111
|
answer = await relayTo({
|
|
670
|
-
to
|
|
1112
|
+
to,
|
|
671
1113
|
kind: "call",
|
|
672
1114
|
body: { envelope },
|
|
673
1115
|
});
|
|
@@ -677,26 +1119,41 @@ export function createOwnMachines({
|
|
|
677
1119
|
if (!answer?.envelope) {
|
|
678
1120
|
if (["offline", "timed_out"].includes(answer?.code)) return null;
|
|
679
1121
|
return {
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
1122
|
+
sealed: false,
|
|
1123
|
+
result: {
|
|
1124
|
+
ok: false,
|
|
1125
|
+
code: answer?.code,
|
|
1126
|
+
error: answer?.error || "home refused the call",
|
|
1127
|
+
},
|
|
683
1128
|
};
|
|
684
1129
|
}
|
|
685
1130
|
try {
|
|
686
|
-
return
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
1131
|
+
return {
|
|
1132
|
+
sealed: true,
|
|
1133
|
+
result: await openAnswer(key, answer.envelope, {
|
|
1134
|
+
workerId: machine.id,
|
|
1135
|
+
requestId,
|
|
1136
|
+
}),
|
|
1137
|
+
};
|
|
690
1138
|
} catch {
|
|
691
1139
|
return {
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
1140
|
+
sealed: false,
|
|
1141
|
+
result: {
|
|
1142
|
+
ok: false,
|
|
1143
|
+
error:
|
|
1144
|
+
"the answer could not be opened: it did not come from your home computer",
|
|
1145
|
+
},
|
|
695
1146
|
};
|
|
696
1147
|
}
|
|
697
1148
|
};
|
|
1149
|
+
const deliver = async (key, requestId, envelope, to) =>
|
|
1150
|
+
(await deliverSealed(key, requestId, envelope, to))?.result ?? null;
|
|
698
1151
|
|
|
699
|
-
/**
|
|
1152
|
+
/**
|
|
1153
|
+
* Writes kept while home was out of reach, in order; stops at the first that still cannot go. A write is let go only
|
|
1154
|
+
* when home answered it sealed, or said it already took it: anything else (Harness's word, an answer nobody sealed)
|
|
1155
|
+
* keeps it for the next try, so a refusal Harness makes up cannot throw the person's work away (S10).
|
|
1156
|
+
*/
|
|
700
1157
|
const flush = async () => {
|
|
701
1158
|
const pinned = pinnedHome();
|
|
702
1159
|
if (flushing || !pinned) return;
|
|
@@ -706,11 +1163,25 @@ export function createOwnMachines({
|
|
|
706
1163
|
const [next, ...rest] = outbox();
|
|
707
1164
|
if (!next) break;
|
|
708
1165
|
const key = await keyFor(pinned.public_key, machine.id);
|
|
709
|
-
const
|
|
710
|
-
|
|
1166
|
+
const answered = await deliverSealed(
|
|
1167
|
+
key,
|
|
1168
|
+
next.requestId,
|
|
1169
|
+
next.envelope
|
|
1170
|
+
);
|
|
1171
|
+
const taken =
|
|
1172
|
+
answered !== null &&
|
|
1173
|
+
(answered.sealed || answered.result?.code === "replayed");
|
|
1174
|
+
if (!taken) {
|
|
1175
|
+
if (answered)
|
|
1176
|
+
log(
|
|
1177
|
+
`kept ${next.command} for your home computer: ${answered.result?.error || "no sealed answer"}`
|
|
1178
|
+
);
|
|
1179
|
+
break;
|
|
1180
|
+
}
|
|
1181
|
+
const result = answered.result;
|
|
711
1182
|
keepOut(rest);
|
|
712
1183
|
log(
|
|
713
|
-
`sent ${next.command} to your home computer: ${result
|
|
1184
|
+
`sent ${next.command} to your home computer: ${result?.ok === false && result.code !== "replayed" ? result.error : "taken"}`
|
|
714
1185
|
);
|
|
715
1186
|
}
|
|
716
1187
|
// Once a connection: home hears which repositories are here and in which folders, so work for one can come here.
|
|
@@ -875,6 +1346,28 @@ export function createOwnMachines({
|
|
|
875
1346
|
};
|
|
876
1347
|
};
|
|
877
1348
|
|
|
1349
|
+
/**
|
|
1350
|
+
* Where this computer's copy of the board stands (boardMirror.mjs sync): "return" while another computer stands in as
|
|
1351
|
+
* home for this one (this one stayed home, and takes it back with the stand-in's changes), "home", "alone" (a worker
|
|
1352
|
+
* following no home here) or "follow".
|
|
1353
|
+
*/
|
|
1354
|
+
const situation = () =>
|
|
1355
|
+
someoneStandsIn() && store.ownMachine(homeListed().id)
|
|
1356
|
+
? "return"
|
|
1357
|
+
: isHome()
|
|
1358
|
+
? "home"
|
|
1359
|
+
: actsAlone()
|
|
1360
|
+
? "alone"
|
|
1361
|
+
: "follow";
|
|
1362
|
+
/** A call of the board copy's own (boardMirror.mjs): to the stand-in while one stands in for this computer, else home. */
|
|
1363
|
+
const mirrorCall = async (call) => {
|
|
1364
|
+
if (situation() === "return") {
|
|
1365
|
+
const standIn = store.ownMachine(homeListed().id);
|
|
1366
|
+
return (await callOnce(standIn, stripLocal(call))) ?? offline();
|
|
1367
|
+
}
|
|
1368
|
+
return isHome() ? null : callHome(call);
|
|
1369
|
+
};
|
|
1370
|
+
|
|
878
1371
|
/** Whether a board request made on this computer goes to home: not while it works on its own board. */
|
|
879
1372
|
const forwards = (request) =>
|
|
880
1373
|
!isHome() && trusted() && goesHome(request.command);
|
|
@@ -895,7 +1388,7 @@ export function createOwnMachines({
|
|
|
895
1388
|
|
|
896
1389
|
// The answers home gave lately, by the call they answer: the same sealed call arriving again (over the direct link,
|
|
897
1390
|
// then through Harness when the link was slow) gets the same answer, or waits for the first, and never runs twice
|
|
898
|
-
// (
|
|
1391
|
+
// (the second was once refused as a replay and the agent told its write failed).
|
|
899
1392
|
const answered = new Map();
|
|
900
1393
|
const ANSWERS_KEPT = 500;
|
|
901
1394
|
const once = (key, run) => {
|
|
@@ -931,42 +1424,105 @@ export function createOwnMachines({
|
|
|
931
1424
|
firstSeen: (requestId) =>
|
|
932
1425
|
store.firstPhoneRequest(`machine:${from}`, String(requestId)),
|
|
933
1426
|
});
|
|
934
|
-
|
|
1427
|
+
// A call that opened but is stale or already taken is answered sealed, so the worker can trust the refusal.
|
|
1428
|
+
if (!opened.ok)
|
|
1429
|
+
return opened.code === "unreadable"
|
|
1430
|
+
? opened
|
|
1431
|
+
: {
|
|
1432
|
+
envelope: await sealAnswer(
|
|
1433
|
+
key,
|
|
1434
|
+
from,
|
|
1435
|
+
body.envelope.requestId,
|
|
1436
|
+
opened
|
|
1437
|
+
),
|
|
1438
|
+
};
|
|
935
1439
|
const call = opened.call ?? {};
|
|
936
1440
|
const command = String(call.command || "");
|
|
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
|
-
);
|
|
1441
|
+
const result = await answerOpened(from, pinned, call, command);
|
|
966
1442
|
return {
|
|
967
1443
|
envelope: await sealAnswer(key, from, body.envelope.requestId, result),
|
|
968
1444
|
};
|
|
969
1445
|
};
|
|
1446
|
+
// What home does with a call it opened from another of the person's computers: the computers' own exchanges, else the
|
|
1447
|
+
// board, as far as that computer may ask (computerMay).
|
|
1448
|
+
const answerOpened = async (from, pinned, call, command) => {
|
|
1449
|
+
if (command === DIRECT_COMMAND) return swapDirectKeys(from, call.key);
|
|
1450
|
+
// Answered for the computer that sealed it, and only for a link allowed for that computer.
|
|
1451
|
+
if (command === LINK_COMMAND)
|
|
1452
|
+
return answerLink
|
|
1453
|
+
? answerLink(from, call.link)
|
|
1454
|
+
: { ok: false, error: "this computer holds no proxy link" };
|
|
1455
|
+
if (command === MODELS_COMMAND || command === MODEL_COMMAND)
|
|
1456
|
+
return answerModelsCall(call, from, !pinned.hosted);
|
|
1457
|
+
if (command === SSH_COMMAND)
|
|
1458
|
+
return answerSshCall(call, from, !pinned.hosted);
|
|
1459
|
+
// Only the person, from a computer they drive, moves Home (a hosted computer is Harness's to run).
|
|
1460
|
+
if (command === HOME_COMMAND)
|
|
1461
|
+
return call.agent === "user" && !pinned.hosted
|
|
1462
|
+
? makeHome(call.target)
|
|
1463
|
+
: { ok: false, error: "only the person moves Home" };
|
|
1464
|
+
if (command === MIRROR_COMMAND && answerMirror) return answerMirror(call);
|
|
1465
|
+
if (command === MERGE_COMMAND && answerMerge)
|
|
1466
|
+
return answerMergeCall(from, call);
|
|
1467
|
+
if (command === DEVICES_COMMAND && answerDevices)
|
|
1468
|
+
return answerDevices(from, call, { drives: !pinned.hosted });
|
|
1469
|
+
if (!goesHome(command))
|
|
1470
|
+
return {
|
|
1471
|
+
ok: false,
|
|
1472
|
+
error: `${command || "that"} runs on the agent's own computer`,
|
|
1473
|
+
};
|
|
1474
|
+
const refused = computerMay({
|
|
1475
|
+
command,
|
|
1476
|
+
agent: call.agent,
|
|
1477
|
+
drives: !pinned.hosted,
|
|
1478
|
+
});
|
|
1479
|
+
if (refused) return { ok: false, code: "not_allowed", error: refused };
|
|
1480
|
+
return settle(() =>
|
|
1481
|
+
boardCommand({
|
|
1482
|
+
...stripLocal(call),
|
|
1483
|
+
// A path names a file on the worker; its bytes come as data.
|
|
1484
|
+
path: undefined,
|
|
1485
|
+
command,
|
|
1486
|
+
[FROM_MACHINE]: from,
|
|
1487
|
+
})
|
|
1488
|
+
);
|
|
1489
|
+
};
|
|
1490
|
+
/**
|
|
1491
|
+
* A board merged in from another computer (M2). A worker brings the board it kept on its own once, when home has just
|
|
1492
|
+
* confirmed it (armed in tryCode) or made it home in its place (makeHome); the computer that stood in for home hands
|
|
1493
|
+
* back what it wrote, once home heard from it, sealed, that it stood in. Either ends with its last page. What a merge
|
|
1494
|
+
* writes never carries the person's Start or trust, and never starts a run (boardMirror.mjs merge).
|
|
1495
|
+
*/
|
|
1496
|
+
const answerMergeCall = (from, call) => {
|
|
1497
|
+
const bring = call.bring === true;
|
|
1498
|
+
const allowed = bring
|
|
1499
|
+
? store.meta(`bring-armed:${from}`) === "1"
|
|
1500
|
+
: store.meta("stood-in-by") === from;
|
|
1501
|
+
if (!allowed)
|
|
1502
|
+
return bring
|
|
1503
|
+
? {
|
|
1504
|
+
ok: false,
|
|
1505
|
+
code: "not_armed",
|
|
1506
|
+
error:
|
|
1507
|
+
"home takes a computer's own board only once, as it joins; this one already came or was never asked for",
|
|
1508
|
+
}
|
|
1509
|
+
: {
|
|
1510
|
+
ok: false,
|
|
1511
|
+
code: "not_stand_in",
|
|
1512
|
+
error: "this computer did not stand in for home",
|
|
1513
|
+
};
|
|
1514
|
+
const page = call.page ?? {};
|
|
1515
|
+
const answer = answerMerge(page, { bring });
|
|
1516
|
+
if (answer?.ok && Number(page.to) >= Number(page.head)) {
|
|
1517
|
+
store.meta(bring ? `bring-armed:${from}` : "stood-in-by", "");
|
|
1518
|
+
log(
|
|
1519
|
+
bring
|
|
1520
|
+
? `took ${labelOf(from) ?? from}'s own board`
|
|
1521
|
+
: `took back what ${labelOf(from) ?? from} wrote while it stood in`
|
|
1522
|
+
);
|
|
1523
|
+
}
|
|
1524
|
+
return answer;
|
|
1525
|
+
};
|
|
970
1526
|
|
|
971
1527
|
/** On home: a worker's key for a direct link, taken, and home's own given back. */
|
|
972
1528
|
const swapDirectKeys = async (workerId, workerKey) => {
|
|
@@ -997,28 +1553,91 @@ export function createOwnMachines({
|
|
|
997
1553
|
to: workerId,
|
|
998
1554
|
kind: "notice",
|
|
999
1555
|
body: { envelope },
|
|
1000
|
-
}).catch(() =>
|
|
1556
|
+
}).catch(() => null);
|
|
1001
1557
|
};
|
|
1002
1558
|
|
|
1559
|
+
// A notice is sealed by the computer that sent it as home: the home this computer follows, or another it confirmed (one
|
|
1560
|
+
// that moved home to itself, or stands in), tried in turn. Harness's word on who sent it counts for nothing.
|
|
1561
|
+
const openNoticeFromAny = async (body) => {
|
|
1562
|
+
const listed = homeListed()?.id;
|
|
1563
|
+
const candidates = store
|
|
1564
|
+
.ownMachines()
|
|
1565
|
+
.sort(
|
|
1566
|
+
(a, b) =>
|
|
1567
|
+
Number(b.machine_id === listed) - Number(a.machine_id === listed)
|
|
1568
|
+
);
|
|
1569
|
+
for (const pinned of candidates) {
|
|
1570
|
+
const key = await keyFor(pinned.public_key, machine.id);
|
|
1571
|
+
const notice = await openNotice(key, body?.envelope, {
|
|
1572
|
+
workerId: machine.id,
|
|
1573
|
+
firstSeen: (requestId) =>
|
|
1574
|
+
store.firstPhoneRequest(
|
|
1575
|
+
`machine:${pinned.machine_id}`,
|
|
1576
|
+
String(requestId)
|
|
1577
|
+
),
|
|
1578
|
+
});
|
|
1579
|
+
if (notice) return { from: pinned.machine_id, notice };
|
|
1580
|
+
}
|
|
1581
|
+
return null;
|
|
1582
|
+
};
|
|
1003
1583
|
const onNoticeFromHome = async (body) => {
|
|
1004
|
-
const
|
|
1005
|
-
if (!
|
|
1006
|
-
const
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1584
|
+
const opened = await openNoticeFromAny(body);
|
|
1585
|
+
if (!opened) return { ok: false };
|
|
1586
|
+
const { from, notice } = opened;
|
|
1587
|
+
if (notice.role) {
|
|
1588
|
+
onRoleStatement(from, notice.role);
|
|
1589
|
+
return { ok: true };
|
|
1590
|
+
}
|
|
1591
|
+
// Everything else is the word of the home this computer follows; another's is heard and set aside.
|
|
1592
|
+
if (from !== acceptedHome()) return { ok: true };
|
|
1593
|
+
if (typeof notice.removed === "string") {
|
|
1594
|
+
removedByHome(notice.removed);
|
|
1595
|
+
return { ok: true };
|
|
1596
|
+
}
|
|
1597
|
+
// Home is about to go to another of the person's computers: its key, from the Home this one follows, is pinned now,
|
|
1598
|
+
// so its sealed word that it is Home (onRoleStatement) opens here when it comes.
|
|
1599
|
+
const next = notice.homeNext;
|
|
1600
|
+
if (
|
|
1601
|
+
typeof next?.id === "string" &&
|
|
1602
|
+
next.id !== machine.id &&
|
|
1603
|
+
typeof next.publicKey === "string"
|
|
1604
|
+
) {
|
|
1605
|
+
store.pinOwnMachine(next.id, next.publicKey, String(next.label || ""));
|
|
1606
|
+
return { ok: true };
|
|
1607
|
+
}
|
|
1016
1608
|
// Home confirmed this computer: what it keeps for home, and the board it brings, go now.
|
|
1017
1609
|
if (notice.welcome === true) void flush();
|
|
1018
1610
|
onNotice(notice);
|
|
1019
1611
|
return { ok: true };
|
|
1020
1612
|
};
|
|
1021
1613
|
|
|
1614
|
+
/**
|
|
1615
|
+
* Another computer confirmed here says, sealed, that it is home now (the person moved home there, or it took its place
|
|
1616
|
+
* back from a stand-in), or that it stands in for this one. Only about itself, and only newer than this computer's
|
|
1617
|
+
* own last move. This is how a computer stops being home or follows a new home (S9); Harness's list alone never does.
|
|
1618
|
+
*/
|
|
1619
|
+
const onRoleStatement = (from, statement) => {
|
|
1620
|
+
if (statement?.home !== from) return;
|
|
1621
|
+
// A stand-in's statement moves nothing here, so it counts however late Harness delivers it (this computer may have
|
|
1622
|
+
// taken home back by then): it lets that computer hand back what it wrote meanwhile (answerMergeCall), once.
|
|
1623
|
+
if (statement.standInFor) {
|
|
1624
|
+
if (statement.standInFor !== machine.id) return;
|
|
1625
|
+
store.meta("stood-in-by", from);
|
|
1626
|
+
log(
|
|
1627
|
+
`${labelOf(from) ?? from} stands in as home while this computer is unreachable`
|
|
1628
|
+
);
|
|
1629
|
+
changed();
|
|
1630
|
+
returning();
|
|
1631
|
+
return;
|
|
1632
|
+
}
|
|
1633
|
+
if (!(Number(statement.at) > Number(store.meta(ROLE_AT) || 0))) return;
|
|
1634
|
+
if (isHome()) setRole("worker", from);
|
|
1635
|
+
else accept(from);
|
|
1636
|
+
log(`${labelOf(from) ?? from} is home now, as it said itself`);
|
|
1637
|
+
changed();
|
|
1638
|
+
void flush();
|
|
1639
|
+
};
|
|
1640
|
+
|
|
1022
1641
|
/**
|
|
1023
1642
|
* On home: each confirmed worker it can reach hears its copy of the board is behind (boardMirror.mjs): through
|
|
1024
1643
|
* Harness when Harness has it connected, or over the direct link when only that is up.
|
|
@@ -1081,6 +1700,64 @@ export function createOwnMachines({
|
|
|
1081
1700
|
);
|
|
1082
1701
|
};
|
|
1083
1702
|
|
|
1703
|
+
/**
|
|
1704
|
+
* On a cloud computer: one call through a proxy link to the computer that holds it, which is home, or (from home) a
|
|
1705
|
+
* worker it confirmed: the pair's key is the only one either holds. The opened answer, or { unreachable, error }
|
|
1706
|
+
* when none came back: "timed_out" when the line's own deadline passed (linkRelay.mjs asks again), anything else
|
|
1707
|
+
* when that computer cannot be reached.
|
|
1708
|
+
*/
|
|
1709
|
+
const linkCall = async (holderId, link) => {
|
|
1710
|
+
const home = pinnedHome();
|
|
1711
|
+
const toHome = Boolean(home) && home.machine_id === holderId;
|
|
1712
|
+
const pinned = toHome
|
|
1713
|
+
? home
|
|
1714
|
+
: isHome()
|
|
1715
|
+
? store.ownMachine(String(holderId))
|
|
1716
|
+
: null;
|
|
1717
|
+
if (!pinned)
|
|
1718
|
+
return {
|
|
1719
|
+
unreachable: "unconfirmed",
|
|
1720
|
+
error: toHome
|
|
1721
|
+
? "your home computer has not confirmed this one"
|
|
1722
|
+
: "the computer holding the link is neither this computer's home nor a computer it confirmed",
|
|
1723
|
+
};
|
|
1724
|
+
const workerId = toHome ? machine.id : pinned.machine_id;
|
|
1725
|
+
const key = await keyFor(pinned.public_key, workerId);
|
|
1726
|
+
const requestId = newRequestId();
|
|
1727
|
+
const envelope = toHome
|
|
1728
|
+
? await sealCall(key, machine.id, requestId, {
|
|
1729
|
+
command: LINK_COMMAND,
|
|
1730
|
+
agent: "user",
|
|
1731
|
+
link,
|
|
1732
|
+
})
|
|
1733
|
+
: await sealAsk(key, workerId, requestId, { link });
|
|
1734
|
+
let reply;
|
|
1735
|
+
try {
|
|
1736
|
+
reply = await relayTo({
|
|
1737
|
+
to: toHome ? "home" : workerId,
|
|
1738
|
+
kind: toHome ? "call" : "ask",
|
|
1739
|
+
body: { envelope },
|
|
1740
|
+
});
|
|
1741
|
+
} catch (error) {
|
|
1742
|
+
return { unreachable: "offline", error: error.message };
|
|
1743
|
+
}
|
|
1744
|
+
if (!reply?.envelope)
|
|
1745
|
+
return {
|
|
1746
|
+
unreachable: reply?.code || "timed_out",
|
|
1747
|
+
error: reply?.error || "no answer came back",
|
|
1748
|
+
};
|
|
1749
|
+
try {
|
|
1750
|
+
return toHome
|
|
1751
|
+
? await openAnswer(key, reply.envelope, { workerId, requestId })
|
|
1752
|
+
: await openAskAnswer(key, reply.envelope, { workerId, requestId });
|
|
1753
|
+
} catch {
|
|
1754
|
+
return {
|
|
1755
|
+
unreachable: "unsealed",
|
|
1756
|
+
error: "the answer did not come from the computer holding the link",
|
|
1757
|
+
};
|
|
1758
|
+
}
|
|
1759
|
+
};
|
|
1760
|
+
|
|
1084
1761
|
/** On a worker: home's question for the windows linked here, opened, asked and the answer sealed back. */
|
|
1085
1762
|
const onAskFromHome = (body) =>
|
|
1086
1763
|
once(`ask:${sealedKey(body?.envelope)}`, () => onAskFromHomeOnce(body));
|
|
@@ -1097,10 +1774,24 @@ export function createOwnMachines({
|
|
|
1097
1774
|
),
|
|
1098
1775
|
});
|
|
1099
1776
|
if (!call) return { ok: false, error: "the question could not be opened" };
|
|
1100
|
-
// Home asking for this computer's agents' models is answered here
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1777
|
+
// Home asking for this computer's agents' models is answered here, and Home handing itself here (handHome) is taken;
|
|
1778
|
+
// a question for the windows only from the few they answer for home (M6).
|
|
1779
|
+
// A cloud home's call through a proxy link this computer holds is answered for that home alone (proxyLinks.mjs).
|
|
1780
|
+
const answer =
|
|
1781
|
+
call.home === true
|
|
1782
|
+
? await becomeHome(call.computers)
|
|
1783
|
+
: call.link && answerLink
|
|
1784
|
+
? await answerLink(pinned.machine_id, call.link)
|
|
1785
|
+
: call.models
|
|
1786
|
+
? await ownModels(call.models)
|
|
1787
|
+
: call.ssh
|
|
1788
|
+
? ownSshAllow(call.ssh)
|
|
1789
|
+
: WINDOW_TOOLS.has(call.tool)
|
|
1790
|
+
? await askOwnWindows(call)
|
|
1791
|
+
: {
|
|
1792
|
+
ok: false,
|
|
1793
|
+
error: `home cannot ask this computer's windows for ${call.tool}`,
|
|
1794
|
+
};
|
|
1104
1795
|
return {
|
|
1105
1796
|
envelope: await sealAskAnswer(
|
|
1106
1797
|
key,
|
|
@@ -1166,6 +1857,8 @@ export function createOwnMachines({
|
|
|
1166
1857
|
connected: sibling.connected,
|
|
1167
1858
|
agents: sibling.agents,
|
|
1168
1859
|
confirmed: confirmed(sibling),
|
|
1860
|
+
// Harness runs it (a cloud computer): what home lets it do is an agents' computer's, never the person's.
|
|
1861
|
+
hosted: store.ownMachine(sibling.id)?.hosted === true,
|
|
1169
1862
|
standsInForThis: sibling.standInFor === machine.id,
|
|
1170
1863
|
direct:
|
|
1171
1864
|
directLink?.status().find((link) => link.id === sibling.id)
|
|
@@ -1296,11 +1989,18 @@ export function createOwnMachines({
|
|
|
1296
1989
|
};
|
|
1297
1990
|
};
|
|
1298
1991
|
|
|
1299
|
-
/** On home: a worker asking for every computer's models, or setting one; only as the person. */
|
|
1300
|
-
const answerModelsCall = async (call, from) => {
|
|
1992
|
+
/** On home: a worker asking for every computer's models, or setting one; only as the person, setting where they drive. */
|
|
1993
|
+
const answerModelsCall = async (call, from, drives) => {
|
|
1301
1994
|
if (call.agent !== "user")
|
|
1302
1995
|
return { ok: false, error: "only the person reads or sets models here" };
|
|
1303
|
-
if (call.command === MODEL_COMMAND)
|
|
1996
|
+
if (call.command === MODEL_COMMAND)
|
|
1997
|
+
return drives
|
|
1998
|
+
? setModel(call)
|
|
1999
|
+
: {
|
|
2000
|
+
ok: false,
|
|
2001
|
+
error:
|
|
2002
|
+
"That computer runs agents only, so nothing it sends acts as you.",
|
|
2003
|
+
};
|
|
1304
2004
|
return {
|
|
1305
2005
|
ok: true,
|
|
1306
2006
|
computers: await everyComputersModels(modelsRead(call.refresh), from),
|
|
@@ -1346,6 +2046,75 @@ export function createOwnMachines({
|
|
|
1346
2046
|
);
|
|
1347
2047
|
};
|
|
1348
2048
|
|
|
2049
|
+
// ---- the person's SSH key into a cloud computer (cloudSsh.mjs) ----
|
|
2050
|
+
|
|
2051
|
+
const ownSshAllow = (ssh) =>
|
|
2052
|
+
ownSsh
|
|
2053
|
+
? ownSsh({ key: String(ssh?.key ?? ""), by: String(ssh?.by ?? "") })
|
|
2054
|
+
: { ok: false, error: "This computer takes no SSH keys." };
|
|
2055
|
+
|
|
2056
|
+
/** On home: the key into a cloud computer that is one of the person's, confirmed here and run by Harness. */
|
|
2057
|
+
const sshOnWorker = async (computer, ssh) => {
|
|
2058
|
+
const worker = siblings.find((sibling) => sibling.id === computer);
|
|
2059
|
+
if (!worker || !confirmed(worker))
|
|
2060
|
+
return {
|
|
2061
|
+
ok: false,
|
|
2062
|
+
error: "that computer is not one of yours confirmed on home",
|
|
2063
|
+
};
|
|
2064
|
+
if (store.ownMachine(computer)?.hosted !== true)
|
|
2065
|
+
return {
|
|
2066
|
+
ok: false,
|
|
2067
|
+
error: "Only a cloud computer takes an SSH key this way.",
|
|
2068
|
+
};
|
|
2069
|
+
return (
|
|
2070
|
+
(await askWorker(computer, { ssh })) ?? {
|
|
2071
|
+
ok: false,
|
|
2072
|
+
error: `${worker.label} did not answer; it may still be waking up`,
|
|
2073
|
+
}
|
|
2074
|
+
);
|
|
2075
|
+
};
|
|
2076
|
+
|
|
2077
|
+
/** On home: a computer the person drives asks to let their key into one of their cloud computers. */
|
|
2078
|
+
const answerSshCall = (call, from, drives) => {
|
|
2079
|
+
if (call.agent !== "user" || !drives)
|
|
2080
|
+
return {
|
|
2081
|
+
ok: false,
|
|
2082
|
+
error:
|
|
2083
|
+
"That computer runs agents only, so nothing it sends acts as you.",
|
|
2084
|
+
};
|
|
2085
|
+
const ssh = { key: String(call.key ?? ""), by: labelOf(from) ?? "" };
|
|
2086
|
+
const computer = String(call.computer || "");
|
|
2087
|
+
return computer === machine.id
|
|
2088
|
+
? ownSshAllow(ssh)
|
|
2089
|
+
: sshOnWorker(computer, ssh);
|
|
2090
|
+
};
|
|
2091
|
+
|
|
2092
|
+
/**
|
|
2093
|
+
* The person, at this computer, lets their SSH key into one of their cloud computers: told to it over the sealed line
|
|
2094
|
+
* between their computers, through home. A computer Harness runs cannot do this for them.
|
|
2095
|
+
*/
|
|
2096
|
+
const allowSsh = async ({ computer, key }) => {
|
|
2097
|
+
if (machine.hosted === true)
|
|
2098
|
+
return {
|
|
2099
|
+
ok: false,
|
|
2100
|
+
error:
|
|
2101
|
+
"This computer runs agents only, so it cannot let a key in for you. Run tryharness ssh on your own computer.",
|
|
2102
|
+
};
|
|
2103
|
+
const ssh = { key: String(key ?? ""), by: machine.label ?? "" };
|
|
2104
|
+
if (computer === machine.id) return ownSshAllow(ssh);
|
|
2105
|
+
if (isHome()) return sshOnWorker(String(computer || ""), ssh);
|
|
2106
|
+
const pinned = pinnedHome();
|
|
2107
|
+
if (!pinned) return homeListed() ? notJoined() : offline();
|
|
2108
|
+
return (
|
|
2109
|
+
(await callOnce(pinned, {
|
|
2110
|
+
command: SSH_COMMAND,
|
|
2111
|
+
agent: "user",
|
|
2112
|
+
computer,
|
|
2113
|
+
key: ssh.key,
|
|
2114
|
+
})) ?? offline()
|
|
2115
|
+
);
|
|
2116
|
+
};
|
|
2117
|
+
|
|
1349
2118
|
const status = () => ({
|
|
1350
2119
|
id: machine.id,
|
|
1351
2120
|
label: machine.label,
|
|
@@ -1482,6 +2251,12 @@ export function createOwnMachines({
|
|
|
1482
2251
|
};
|
|
1483
2252
|
};
|
|
1484
2253
|
|
|
2254
|
+
if (machine.role === "home" && machine.joinCode) {
|
|
2255
|
+
machine.joinCode = null;
|
|
2256
|
+
machine.joinCodeAt = null;
|
|
2257
|
+
saveMachine(machine);
|
|
2258
|
+
}
|
|
2259
|
+
|
|
1485
2260
|
return {
|
|
1486
2261
|
hello,
|
|
1487
2262
|
adopt,
|
|
@@ -1498,10 +2273,13 @@ export function createOwnMachines({
|
|
|
1498
2273
|
relay,
|
|
1499
2274
|
noticeTo,
|
|
1500
2275
|
askWorkerWindows,
|
|
2276
|
+
linkCall,
|
|
1501
2277
|
tellWorkers,
|
|
1502
2278
|
fromLink,
|
|
1503
2279
|
standingIn,
|
|
1504
2280
|
someoneStandsIn,
|
|
2281
|
+
situation,
|
|
2282
|
+
mirrorCall,
|
|
1505
2283
|
// A worker confirmed into the swarm: it holds home's key.
|
|
1506
2284
|
joined: () => Boolean(pinnedHome()),
|
|
1507
2285
|
actsAlone,
|
|
@@ -1520,6 +2298,7 @@ export function createOwnMachines({
|
|
|
1520
2298
|
list,
|
|
1521
2299
|
models,
|
|
1522
2300
|
setModel,
|
|
2301
|
+
allowSsh,
|
|
1523
2302
|
status,
|
|
1524
2303
|
all,
|
|
1525
2304
|
learn,
|