tryharness 0.7.82 → 0.7.86
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 +21 -19
- package/assets/dsh-plugin.mjs +75 -4
- package/assets/herdr-mod/harness/main.js +367 -0
- package/assets/herdr-mod/harness/mod.json +6 -0
- package/assets/opencode-plugin.js +87 -22
- package/assets/pi-extension.ts +81 -2
- package/cli.mjs +544 -208
- package/lib/adapters.mjs +62 -27
- package/lib/agentBinaries.mjs +6 -8
- package/lib/agentClaims.mjs +168 -0
- package/lib/agentReadTools.json +415 -0
- package/lib/agentSignIn.mjs +17 -18
- package/lib/agentWiring.mjs +114 -0
- package/lib/archetypes.js +10 -11
- package/lib/attention.mjs +100 -0
- package/lib/audit.mjs +1 -1
- package/lib/backupSetup.mjs +6 -28
- package/lib/bankrApprovals.mjs +86 -0
- package/lib/boardAccess.mjs +40 -66
- package/lib/boardBackup.mjs +86 -85
- package/lib/boardCommands.mjs +28 -0
- package/lib/boardFileCommands.mjs +29 -36
- package/lib/boardFiles.mjs +83 -15
- package/lib/boardMirror.mjs +78 -11
- package/lib/boardProtocol.json +193 -123
- package/lib/boardThreads.mjs +19 -37
- package/lib/boardTools.mjs +22 -15
- package/lib/boardWatches.mjs +43 -15
- package/lib/bridge.mjs +7 -3
- package/lib/browserAgentTools.mjs +59 -0
- package/lib/browserWake.mjs +0 -6
- package/lib/cards.mjs +57 -2
- package/lib/circling.mjs +189 -0
- package/lib/cleanup.mjs +234 -0
- package/lib/cloudBoot.mjs +21 -4
- package/lib/cloudSave.mjs +46 -5
- package/lib/compaction.mjs +8 -1
- package/lib/completion.mjs +358 -0
- package/lib/computerDevices.mjs +853 -0
- package/lib/connectionStatus.mjs +34 -0
- package/lib/connectors.mjs +139 -1
- package/lib/coordinator.mjs +108 -14
- package/lib/coreModels.mjs +36 -12
- package/lib/daemon.mjs +2435 -333
- package/lib/daemonLock.mjs +103 -0
- package/lib/deviceDoor.mjs +308 -0
- package/lib/deviceIdentity.mjs +114 -0
- package/lib/deviceList.mjs +172 -0
- package/lib/deviceSeal.mjs +74 -0
- package/lib/dispatcher.mjs +497 -26
- package/lib/dispatches.mjs +3 -3
- package/lib/environment.mjs +2 -1
- package/lib/environments.mjs +67 -41
- package/lib/events.mjs +318 -0
- package/lib/facts.mjs +170 -0
- package/lib/fileSource.mjs +540 -0
- package/lib/files.mjs +47 -2
- package/lib/gitHost.mjs +39 -1
- package/lib/goalCommands.mjs +12 -80
- package/lib/handover.mjs +21 -16
- package/lib/harnessOffers.mjs +140 -15
- package/lib/herdr.mjs +100 -15
- package/lib/herdrAppCall.mjs +19 -0
- package/lib/herdrCore.mjs +59 -0
- package/lib/herdrRuns.mjs +1126 -0
- package/lib/hookDialects.mjs +235 -50
- package/lib/identityLink.mjs +81 -0
- package/lib/inference.mjs +31 -2
- package/lib/jamAccess.mjs +82 -0
- package/lib/{projectBoard.mjs → jamBoard.mjs} +5 -4
- package/lib/{projectGit.mjs → jamGit.mjs} +53 -15
- package/lib/jamKey.mjs +274 -0
- package/lib/{projectPieces.mjs → jamPieces.mjs} +35 -29
- package/lib/{projectSettings.mjs → jamSettings.mjs} +12 -22
- package/lib/jamStore.mjs +973 -0
- package/lib/jamUpgrade.mjs +112 -0
- package/lib/jams.mjs +3079 -0
- package/lib/kanban.mjs +13 -7
- package/lib/landed.mjs +5 -1
- package/lib/library.mjs +41 -22
- package/lib/limits.mjs +91 -0
- package/lib/lineDiff.mjs +97 -0
- package/lib/lineup.mjs +18 -13
- package/lib/link.mjs +584 -0
- package/lib/localStack.mjs +75 -16
- package/lib/localmodels.mjs +58 -78
- package/lib/loopBackstop.mjs +2 -1
- package/lib/machine.mjs +544 -0
- package/lib/machineLink.mjs +446 -0
- package/lib/machineRoutes.mjs +68 -19
- package/lib/machineRuns.mjs +85 -4
- package/lib/machineSeal.mjs +6 -31
- package/lib/manager.mjs +134 -12
- package/lib/mcp-stdio.mjs +492 -215
- package/lib/mcpRelay.mjs +34 -35
- package/lib/members.json +2 -2
- package/lib/modelRoute.mjs +162 -0
- package/lib/nativeHost.mjs +81 -90
- package/lib/nativeSenses.mjs +258 -0
- package/lib/nerves.mjs +523 -0
- package/lib/network.mjs +117 -278
- package/lib/networkPlaces.mjs +578 -0
- package/lib/nextStep.mjs +95 -0
- package/lib/notify.mjs +2 -2
- package/lib/nudge.mjs +116 -10
- package/lib/onboard.mjs +6 -6
- package/lib/originals.mjs +216 -0
- package/lib/outsideEvents.mjs +383 -0
- package/lib/outsideRuns.mjs +230 -0
- package/lib/ownMachines.mjs +674 -256
- package/lib/pain.mjs +215 -0
- package/lib/pairing.mjs +91 -11
- package/lib/paths.mjs +42 -3
- package/lib/peerProcess.mjs +21 -7
- package/lib/peopleLink.mjs +268 -0
- package/lib/personNotices.mjs +3 -2
- package/lib/phones.mjs +252 -542
- package/lib/placeCards.mjs +282 -0
- package/lib/placeChecks.mjs +95 -0
- package/lib/placeCommands.mjs +8 -17
- package/lib/placeRequests.mjs +152 -0
- package/lib/placeStore.mjs +295 -0
- package/lib/placeTool.d.ts +12 -0
- package/lib/placeTool.js +40 -0
- package/lib/places.mjs +63 -14
- package/lib/plans.mjs +53 -8
- package/lib/presence.mjs +270 -0
- package/lib/privateNetwork.d.ts +2 -0
- package/lib/privateNetwork.js +93 -0
- package/lib/proposals.mjs +300 -0
- package/lib/publicFetch.d.ts +32 -0
- package/lib/publicFetch.js +248 -0
- package/lib/reach.mjs +128 -0
- package/lib/rebuildable.mjs +72 -0
- package/lib/recall.mjs +216 -0
- package/lib/record.mjs +13 -0
- package/lib/reflexes.mjs +392 -0
- package/lib/relayLimits.mjs +31 -2
- package/lib/releaseNotice.mjs +198 -0
- package/lib/removeHarness.mjs +15 -4
- package/lib/repoLinks.mjs +204 -35
- package/lib/runAuthority.mjs +102 -39
- package/lib/runBrief.mjs +93 -40
- package/lib/runRequests.mjs +52 -14
- package/lib/runUsage.mjs +31 -0
- package/lib/runWorker.mjs +74 -25
- package/lib/runner.mjs +289 -45
- package/lib/runtime-install.mjs +37 -8
- package/lib/schedules.mjs +171 -47
- package/lib/screenWatches.mjs +221 -0
- package/lib/sealed.mjs +12 -30
- package/lib/sealedThreads.mjs +198 -392
- package/lib/secretStore.mjs +178 -0
- package/lib/secrets.mjs +11 -0
- package/lib/seen.mjs +3 -9
- package/lib/selfUpdate.mjs +51 -15
- package/lib/senseCall.mjs +1514 -0
- package/lib/senseHook.mjs +114 -0
- package/lib/senseTools.mjs +1298 -0
- package/lib/sessionBriefs.mjs +87 -0
- package/lib/sessionControl.mjs +63 -13
- package/lib/sessionFiles.mjs +85 -0
- package/lib/setupChoices.mjs +12 -3
- package/lib/setupMode.mjs +70 -25
- package/lib/sharedKeys.mjs +166 -0
- package/lib/sleep.mjs +396 -0
- package/lib/sources.mjs +306 -0
- package/lib/speech.mjs +166 -411
- package/lib/spoken.mjs +210 -0
- package/lib/steerHook.mjs +3 -1
- package/lib/steerQueue.mjs +40 -11
- package/lib/store.mjs +323 -130
- package/lib/stuck.mjs +85 -7
- package/lib/swarm.mjs +157 -49
- package/lib/swarmDevices.mjs +25 -85
- package/lib/swarmMemory.mjs +93 -9
- package/lib/t3.mjs +73 -3
- package/lib/taskCommands.mjs +51 -60
- package/lib/taskRooms.mjs +30 -4
- package/lib/taskRules.json +29 -0
- package/lib/taskRules.mjs +21 -0
- package/lib/taskRuns.mjs +74 -11
- package/lib/team.mjs +2 -1
- package/lib/threadActions.mjs +37 -9
- package/lib/threadAuthority.mjs +4 -4
- package/lib/threadBriefs.mjs +173 -43
- package/lib/threadNotes.mjs +320 -0
- package/lib/threadRecord.mjs +491 -0
- package/lib/threadRuns.mjs +216 -108
- package/lib/threadView.mjs +27 -30
- package/lib/threadWatches.mjs +1962 -0
- package/lib/timeSense.mjs +839 -0
- package/lib/tripwires.mjs +454 -0
- package/lib/ui.mjs +1 -1
- package/lib/vault.mjs +560 -45
- package/lib/vaultCommands.mjs +5 -3
- package/lib/vaultPeers.mjs +165 -0
- package/lib/vaultTools.d.ts +4 -1
- package/lib/vaultTools.js +22 -1
- package/lib/verdicts.mjs +12 -26
- package/lib/versions.mjs +117 -0
- package/lib/waitTool.mjs +278 -0
- package/lib/wakePicture.mjs +268 -0
- package/lib/wakeQueue.mjs +108 -0
- package/lib/watchResponses.mjs +533 -0
- package/lib/watches.mjs +290 -145
- package/lib/windowAsks.mjs +80 -17
- package/lib/worktrees.mjs +159 -9
- package/npm-shrinkwrap.json +138 -631
- package/package.json +7 -6
- package/release.json +212 -130
- package/lib/callSessions.mjs +0 -51
- package/lib/directLink.mjs +0 -349
- package/lib/peopleBoard.mjs +0 -131
- package/lib/projects.mjs +0 -2917
- package/lib/roomChannels.mjs +0 -667
package/lib/ownMachines.mjs
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
2
3
|
import path from "node:path";
|
|
3
4
|
import { randomBytes, randomUUID } from "node:crypto";
|
|
4
5
|
import { documentText, ensureDir, isDocument, mimeFor } from "./files.mjs";
|
|
5
6
|
import { FILE_PART_BYTES } from "./boardFiles.mjs";
|
|
6
|
-
import {
|
|
7
|
-
fingerprint,
|
|
8
|
-
MAX_SKEW_MS,
|
|
9
|
-
open as openSealed,
|
|
10
|
-
seal,
|
|
11
|
-
} from "./sealed.mjs";
|
|
7
|
+
import { fingerprint } from "./sealed.mjs";
|
|
12
8
|
import { nameOf } from "./members.mjs";
|
|
13
9
|
import {
|
|
14
10
|
isPairCode,
|
|
@@ -41,23 +37,28 @@ import {
|
|
|
41
37
|
import { MERGE_COMMAND, MIRROR_COMMAND } from "./boardMirror.mjs";
|
|
42
38
|
import { DEVICES_COMMAND } from "./swarmDevices.mjs";
|
|
43
39
|
import { member } from "./members.mjs";
|
|
44
|
-
import {
|
|
45
|
-
import { createCallSessions } from "./callSessions.mjs";
|
|
40
|
+
import { mayAskWindows } from "./windowAsks.mjs";
|
|
46
41
|
import { VIEW_COMMAND, VIEW_DROP_COMMAND } from "./liveViews.mjs";
|
|
47
42
|
import { computerAddress } from "./computerAddress.mjs";
|
|
48
43
|
import { RUN_CALLER } from "./runAuthority.mjs";
|
|
44
|
+
import { ASK_TIMEOUT_MS } from "./bridge.mjs";
|
|
45
|
+
import { NERVE_KIND } from "./nerves.mjs";
|
|
49
46
|
|
|
50
47
|
export { FROM_MACHINE };
|
|
51
48
|
|
|
52
|
-
// A worker
|
|
53
|
-
const
|
|
54
|
-
// A worker
|
|
55
|
-
const
|
|
56
|
-
|
|
49
|
+
// A worker asking the windows on home (and home's other computers) for its agent, over their link only (windowAsks.mjs).
|
|
50
|
+
const WINDOWS_COMMAND = "machine.windows";
|
|
51
|
+
// A worker's WebRTC channel to home, introduced by these two sealed calls (machineLink.mjs).
|
|
52
|
+
const LINK_OFFER_COMMAND = "machine.link.offer";
|
|
53
|
+
const LINK_CANDIDATES_COMMAND = "machine.link.candidates";
|
|
54
|
+
const INTRODUCTIONS = new Set([LINK_OFFER_COMMAND, LINK_CANDIDATES_COMMAND]);
|
|
55
|
+
// What Harness carries of a worker's sealed calls to home: its channel's introduction (machineLink.mjs).
|
|
56
|
+
// A call over the direct link, as it is: the link's channel was introduced sealed by the pinned computer (machineLink.mjs).
|
|
57
57
|
const DIRECT_CALL = "call.direct";
|
|
58
|
-
// Home's views for a worker (liveViews.mjs): their kind on the
|
|
58
|
+
// Home's views for a worker (liveViews.mjs): their kind on the channel between the computers.
|
|
59
59
|
const VIEWS_KIND = "views";
|
|
60
|
-
|
|
60
|
+
// Home asks the worker an agent runs on for its claim that the agent wrote some words (agentClaims.mjs).
|
|
61
|
+
const CLAIM_KIND = "claim";
|
|
61
62
|
// The person's model and effort for an agent live on the computer that runs it (models.mjs reads its own store), so
|
|
62
63
|
// the buddy's Swarm tab reads and sets them on each computer: a worker asks home, home asks the worker that has it.
|
|
63
64
|
const MODELS_COMMAND = "machine.models";
|
|
@@ -133,10 +134,17 @@ export function createOwnMachines({
|
|
|
133
134
|
identity,
|
|
134
135
|
agents = [],
|
|
135
136
|
ask,
|
|
137
|
+
// This computer on the person's device list (computerDevices.mjs): how it approves another computer. Null: it adds
|
|
138
|
+
// only computers approved elsewhere.
|
|
139
|
+
devices = null,
|
|
136
140
|
boardCommand,
|
|
137
141
|
filesRoot,
|
|
138
142
|
changed = () => {},
|
|
139
143
|
onNotice = () => {},
|
|
144
|
+
// The nerves (nerves.mjs, S6): a message of what happened on another of the person's computers, its answer.
|
|
145
|
+
onNerve = () => ({ ok: false, error: "no nerves here" }),
|
|
146
|
+
// On a worker: its signed claim that one of its own agents wrote words, which home asks for ({ agent, about }).
|
|
147
|
+
onClaim = async () => ({ ok: false, error: "no claims here" }),
|
|
140
148
|
// The folders worked in here with the repository each holds: [{ path, repository }].
|
|
141
149
|
// `fresh: true` rereads their current repository identities for an inventory request.
|
|
142
150
|
folders = () => [],
|
|
@@ -149,7 +157,7 @@ export function createOwnMachines({
|
|
|
149
157
|
answerMerge = null,
|
|
150
158
|
// On home: another computer's devices, and the person's decisions on them made there (swarmDevices.mjs answer).
|
|
151
159
|
answerDevices = null,
|
|
152
|
-
// The direct links between the person's computers (
|
|
160
|
+
// The direct links between the person's computers (machineLink.mjs); null keeps every message to Harness.
|
|
153
161
|
directLink = null,
|
|
154
162
|
// How long home must be unreachable on every route before this worker stands in as home (daemon.mjs: a week unless
|
|
155
163
|
// the person's config says otherwise); null: it never does.
|
|
@@ -165,7 +173,7 @@ export function createOwnMachines({
|
|
|
165
173
|
askOwnWindows = async () => ({
|
|
166
174
|
ok: false,
|
|
167
175
|
code: "unpaired",
|
|
168
|
-
error: "no Harness window
|
|
176
|
+
error: "no Harness window has reached this computer yet",
|
|
169
177
|
}),
|
|
170
178
|
// This computer's own agents' models, read or one set, as the person (swarm.mjs board.swarm.models/model).
|
|
171
179
|
ownModels = async () => ({
|
|
@@ -199,6 +207,8 @@ export function createOwnMachines({
|
|
|
199
207
|
}),
|
|
200
208
|
// The release this computer runs, which an update never goes back from.
|
|
201
209
|
runningVersion = "0.0.0",
|
|
210
|
+
// The update this computer started: "running", "failed" or null (selfUpdate.mjs).
|
|
211
|
+
ownUpdateState = () => null,
|
|
202
212
|
}) {
|
|
203
213
|
// On home: each worker's folders and the repository each holds, as it said them.
|
|
204
214
|
const infos = new Map();
|
|
@@ -263,15 +273,41 @@ export function createOwnMachines({
|
|
|
263
273
|
const keyFor = (peerKey, workerId) =>
|
|
264
274
|
pairKey(identity().privateKey, peerKey, workerId);
|
|
265
275
|
|
|
266
|
-
// A message for another of the person's computers
|
|
267
|
-
// Harness
|
|
276
|
+
// A message for another of the person's computers goes over the channel between them (docs/one-link-plan.md L8.3),
|
|
277
|
+
// waited for while Harness lists that computer as on. Harness carries only a notice for one with no channel: its
|
|
278
|
+
// mailbox keeps it for a computer that is away and wakes a cloud computer that sleeps. Sealed the same either way.
|
|
268
279
|
const relayTo = async (input) => {
|
|
269
280
|
const peer = input.to === "home" ? homeListed()?.id : input.to;
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
281
|
+
// A notice waits for no channel: one that is not open now goes to the mailbox at once.
|
|
282
|
+
const notice = input.kind === "notice";
|
|
283
|
+
const answer = await overChannel(
|
|
284
|
+
peer,
|
|
285
|
+
{ kind: input.kind, body: input.body },
|
|
286
|
+
{ wait: !notice }
|
|
287
|
+
);
|
|
288
|
+
if (answer) return answer;
|
|
289
|
+
if (notice) return ask("machine_relay", input);
|
|
290
|
+
return peerOn(peer)
|
|
291
|
+
? {
|
|
292
|
+
ok: false,
|
|
293
|
+
code: "timed_out",
|
|
294
|
+
error: "the channel to that computer did not open in time",
|
|
295
|
+
}
|
|
296
|
+
: { ok: false, code: "offline", error: "that computer is offline" };
|
|
297
|
+
};
|
|
298
|
+
// Whether Harness lists that computer as on: only then is its channel worth waiting for.
|
|
299
|
+
const peerOn = (peer) =>
|
|
300
|
+
siblings.some((sibling) => sibling.id === peer && sibling.connected);
|
|
301
|
+
/** One request over the channel to that computer, its answer, or null with no channel or no answer in time. */
|
|
302
|
+
const overChannel = async (
|
|
303
|
+
peer,
|
|
304
|
+
message,
|
|
305
|
+
{ timeoutMs, wait = true } = {}
|
|
306
|
+
) => {
|
|
307
|
+
if (!peer || !directLink) return null;
|
|
308
|
+
const waitMs = wait && peerOn(peer) ? (timeoutMs ?? ASK_TIMEOUT_MS) : 0;
|
|
309
|
+
if (!(await directLink.opened(peer, waitMs))) return null;
|
|
310
|
+
return directLink.request(peer, message, timeoutMs ? { timeoutMs } : {});
|
|
275
311
|
};
|
|
276
312
|
|
|
277
313
|
// ---- who this computer is, and the others ----
|
|
@@ -366,6 +402,7 @@ export function createOwnMachines({
|
|
|
366
402
|
changed();
|
|
367
403
|
reconcileRole();
|
|
368
404
|
retryAdding();
|
|
405
|
+
if (waitingToJoin()) void checkListed();
|
|
369
406
|
void sendQueued();
|
|
370
407
|
void retryRemovals();
|
|
371
408
|
watchHome();
|
|
@@ -702,7 +739,6 @@ export function createOwnMachines({
|
|
|
702
739
|
const forget = (id) => {
|
|
703
740
|
store.unpinOwnMachine(id);
|
|
704
741
|
directLink?.disallow?.(id);
|
|
705
|
-
callSessions.end(id);
|
|
706
742
|
infos.delete(id);
|
|
707
743
|
store.meta(`bring-armed:${id}`, "");
|
|
708
744
|
if (store.meta("stood-in-by") === id) store.meta("stood-in-by", "");
|
|
@@ -755,6 +791,23 @@ export function createOwnMachines({
|
|
|
755
791
|
changed();
|
|
756
792
|
};
|
|
757
793
|
|
|
794
|
+
/**
|
|
795
|
+
* A computer taken off the person's signed device list (docs/one-link-plan.md L6.1), by the sealing key it had there,
|
|
796
|
+
* which is its swarm key: no longer trusted here, and its link closed now.
|
|
797
|
+
*/
|
|
798
|
+
const removedFromList = (sealingKey) => {
|
|
799
|
+
const key = Buffer.from(String(sealingKey || ""), "base64").toString(
|
|
800
|
+
"base64url"
|
|
801
|
+
);
|
|
802
|
+
const pinned = key
|
|
803
|
+
? store.ownMachines().find((each) => each.public_key === key)
|
|
804
|
+
: null;
|
|
805
|
+
if (!pinned || pinned.machine_id === machine.id) return;
|
|
806
|
+
const label = labelOf(pinned.machine_id) ?? pinned.machine_id;
|
|
807
|
+
removed({ machineId: pinned.machine_id });
|
|
808
|
+
log(`machine link: ${label} was taken off your devices; its link closed`);
|
|
809
|
+
};
|
|
810
|
+
|
|
758
811
|
/** On a worker: home took a computer off the swarm, sealed. This one, or another this computer trusts. */
|
|
759
812
|
const removedByHome = (id) => {
|
|
760
813
|
if (id === machine.id) {
|
|
@@ -899,8 +952,25 @@ export function createOwnMachines({
|
|
|
899
952
|
return null;
|
|
900
953
|
}
|
|
901
954
|
};
|
|
955
|
+
// Whether one of the person's devices signed this computer onto their list: then home brings it into the swarm by
|
|
956
|
+
// itself (joinListed) and no code is shown. Read again at most once a minute, as Harness lists the computers.
|
|
957
|
+
let onPersonsList = false;
|
|
958
|
+
let listCheckedAt = 0;
|
|
959
|
+
const checkListed = async () => {
|
|
960
|
+
if (!devices || Date.now() - listCheckedAt < 60_000) return;
|
|
961
|
+
listCheckedAt = Date.now();
|
|
962
|
+
const read = await devices.read().catch(() => null);
|
|
963
|
+
if (!read) return;
|
|
964
|
+
const listed = read.list.devices.some(
|
|
965
|
+
(device) => device.deviceId === read.deviceId
|
|
966
|
+
);
|
|
967
|
+
if (listed !== onPersonsList) {
|
|
968
|
+
onPersonsList = listed;
|
|
969
|
+
changed();
|
|
970
|
+
}
|
|
971
|
+
};
|
|
902
972
|
const joinCode = () => {
|
|
903
|
-
if (!waitingToJoin()) return null;
|
|
973
|
+
if (!waitingToJoin() || onPersonsList) return null;
|
|
904
974
|
const saved = joinState();
|
|
905
975
|
if (saved?.code) return saved.code;
|
|
906
976
|
const code = newJoinCode();
|
|
@@ -946,6 +1016,19 @@ export function createOwnMachines({
|
|
|
946
1016
|
// the code is for (a cloud computer, added from the person's window), so the code is tried on no other; `only.hosted`
|
|
947
1017
|
// marks it as one Harness runs, kept on its pin for what home lets it do (a computer Harness runs also says so itself).
|
|
948
1018
|
const tryCode = async (code, tried, only = null) => {
|
|
1019
|
+
// The one computer named is in already: it joined from the person's devices (joinListed) before the code came.
|
|
1020
|
+
const named = only?.machine
|
|
1021
|
+
? siblings.find(
|
|
1022
|
+
(sibling) =>
|
|
1023
|
+
sibling.id === only.machine && sibling.publicKey === only.key
|
|
1024
|
+
)
|
|
1025
|
+
: null;
|
|
1026
|
+
if (named && confirmed(named))
|
|
1027
|
+
return {
|
|
1028
|
+
ok: true,
|
|
1029
|
+
machine: named.label,
|
|
1030
|
+
fingerprint: await fingerprint(named.publicKey),
|
|
1031
|
+
};
|
|
949
1032
|
const waiting = siblings.filter(
|
|
950
1033
|
(sibling) =>
|
|
951
1034
|
sibling.role === "worker" &&
|
|
@@ -1043,18 +1126,62 @@ export function createOwnMachines({
|
|
|
1043
1126
|
* still waits for one), and home confirms it into the swarm as soon as it is online. A code Harness had nothing
|
|
1044
1127
|
* waiting for is one already approved in a browser: it is tried now, on the computers waiting to join.
|
|
1045
1128
|
*/
|
|
1129
|
+
// Codes typed here for a computer Harness still holds a pairing for: the comparison this computer started with it
|
|
1130
|
+
// (computerDevices.mjs), by its id, until the person says whether the codes match.
|
|
1131
|
+
const comparing = new Map();
|
|
1132
|
+
|
|
1046
1133
|
const add = async (typed, only = null) => {
|
|
1047
1134
|
const code = normalizeJoinCode(typed);
|
|
1048
1135
|
if (!code) return badCode();
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
if (
|
|
1055
|
-
|
|
1056
|
-
|
|
1136
|
+
if (isPairCode(code) && devices) {
|
|
1137
|
+
const started = await devices.startApproval(await pairClaim(code));
|
|
1138
|
+
// Nothing waiting at Harness: it was approved elsewhere already, or its setup ended. Home still confirms the code.
|
|
1139
|
+
if (!started?.ok && !/No computer is waiting/.test(started?.error ?? ""))
|
|
1140
|
+
return { ok: false, error: started?.error || "Harness did not answer" };
|
|
1141
|
+
if (started?.ok) {
|
|
1142
|
+
comparing.set(started.id, { code, only });
|
|
1143
|
+
return {
|
|
1144
|
+
ok: true,
|
|
1145
|
+
state: "compare",
|
|
1146
|
+
id: started.id,
|
|
1147
|
+
machine: started.machine,
|
|
1148
|
+
approvalCode: started.code,
|
|
1149
|
+
};
|
|
1150
|
+
}
|
|
1057
1151
|
}
|
|
1152
|
+
return addApproved(code, null, only);
|
|
1153
|
+
};
|
|
1154
|
+
|
|
1155
|
+
/**
|
|
1156
|
+
* The person compared the codes the new computer and this one show. Matching: this computer signs it onto their
|
|
1157
|
+
* device list and seals their keys to it, Harness gives it its token, and home brings it into the swarm as before.
|
|
1158
|
+
*/
|
|
1159
|
+
const match = async (id, matches) => {
|
|
1160
|
+
const typed = comparing.get(id);
|
|
1161
|
+
comparing.delete(id);
|
|
1162
|
+
const finished = devices
|
|
1163
|
+
? await devices.finishApproval(id, matches === true).catch((error) => ({
|
|
1164
|
+
ok: false,
|
|
1165
|
+
error: error.message,
|
|
1166
|
+
}))
|
|
1167
|
+
: null;
|
|
1168
|
+
if (!typed || !finished)
|
|
1169
|
+
return {
|
|
1170
|
+
ok: false,
|
|
1171
|
+
error: "That comparison ended. Type the computer's code again.",
|
|
1172
|
+
};
|
|
1173
|
+
if (!finished.ok) return finished;
|
|
1174
|
+
if (finished.declined)
|
|
1175
|
+
return { ok: true, state: "declined", machine: finished.machine };
|
|
1176
|
+
return addApproved(
|
|
1177
|
+
typed.code,
|
|
1178
|
+
finished.approved ? String(finished.machine || "") : null,
|
|
1179
|
+
typed.only
|
|
1180
|
+
);
|
|
1181
|
+
};
|
|
1182
|
+
|
|
1183
|
+
// A code whose computer Harness has let onto the account (approved names it; null: approved before, elsewhere).
|
|
1184
|
+
const addApproved = async (code, approved, only) => {
|
|
1058
1185
|
if (!isHome())
|
|
1059
1186
|
return approved !== null
|
|
1060
1187
|
? {
|
|
@@ -1100,10 +1227,158 @@ export function createOwnMachines({
|
|
|
1100
1227
|
const retryAdding = () => {
|
|
1101
1228
|
if (!isHome()) return;
|
|
1102
1229
|
for (const entry of adding.values()) void tryAdding(entry);
|
|
1230
|
+
void joinListed();
|
|
1231
|
+
};
|
|
1232
|
+
|
|
1233
|
+
// ---- on home: computers on the person's device list join with no code ----
|
|
1234
|
+
//
|
|
1235
|
+
// A computer one of the person's devices signed onto their list (computerDevices.mjs) is theirs: home asks each
|
|
1236
|
+
// connected computer that is not confirmed yet to show it, and confirms it when its device's signature over both
|
|
1237
|
+
// swarm keys holds on the signed list. One that is not on the list says so and is asked again a while later; the code
|
|
1238
|
+
// typed on home still works for it. A computer that just connected may not know its home yet, so the first tries
|
|
1239
|
+
// come quickly, then once a minute.
|
|
1240
|
+
const LIST_JOIN_QUICK_MS = 3_000;
|
|
1241
|
+
const LIST_JOIN_QUICK_TRIES = 10;
|
|
1242
|
+
const LIST_JOIN_RETRY_MS = 60_000;
|
|
1243
|
+
const listJoinTried = new Map();
|
|
1244
|
+
let listJoinTimer = null;
|
|
1245
|
+
const joinListedSoon = (ms) => {
|
|
1246
|
+
if (listJoinTimer) return;
|
|
1247
|
+
listJoinTimer = setTimeout(() => {
|
|
1248
|
+
listJoinTimer = null;
|
|
1249
|
+
void joinListed();
|
|
1250
|
+
}, ms);
|
|
1251
|
+
listJoinTimer.unref?.();
|
|
1252
|
+
};
|
|
1253
|
+
const joinListed = async () => {
|
|
1254
|
+
if (!devices || !isHome()) return;
|
|
1255
|
+
const homeKey = identity().publicKey;
|
|
1256
|
+
for (const worker of siblings) {
|
|
1257
|
+
if (
|
|
1258
|
+
worker.role !== "worker" ||
|
|
1259
|
+
!worker.connected ||
|
|
1260
|
+
!worker.publicKey ||
|
|
1261
|
+
confirmed(worker)
|
|
1262
|
+
)
|
|
1263
|
+
continue;
|
|
1264
|
+
const tried = `${worker.id}:${worker.publicKey}`;
|
|
1265
|
+
const before = listJoinTried.get(tried) ?? { at: 0, count: 0 };
|
|
1266
|
+
const wait =
|
|
1267
|
+
before.count < LIST_JOIN_QUICK_TRIES
|
|
1268
|
+
? LIST_JOIN_QUICK_MS
|
|
1269
|
+
: LIST_JOIN_RETRY_MS;
|
|
1270
|
+
if (Date.now() - before.at < wait) continue;
|
|
1271
|
+
listJoinTried.set(tried, { at: Date.now(), count: before.count + 1 });
|
|
1272
|
+
const keys = {
|
|
1273
|
+
homeId: machine.id,
|
|
1274
|
+
homeKey,
|
|
1275
|
+
workerId: worker.id,
|
|
1276
|
+
workerKey: worker.publicKey,
|
|
1277
|
+
};
|
|
1278
|
+
const proof = await devices.swarmProof("home", keys).catch((error) => {
|
|
1279
|
+
log(`could not sign a join for your devices: ${error.message}`);
|
|
1280
|
+
return null;
|
|
1281
|
+
});
|
|
1282
|
+
if (!proof) return;
|
|
1283
|
+
const answer = await ask("machine_relay", {
|
|
1284
|
+
to: worker.id,
|
|
1285
|
+
kind: "join",
|
|
1286
|
+
body: { homeKey, device: proof },
|
|
1287
|
+
}).catch((error) => ({ ok: false, error: error.message }));
|
|
1288
|
+
const listed = answer?.ok
|
|
1289
|
+
? await devices
|
|
1290
|
+
.checkSwarmProof("worker", keys, answer.device)
|
|
1291
|
+
.catch(() => null)
|
|
1292
|
+
: null;
|
|
1293
|
+
if (!listed) {
|
|
1294
|
+
log(
|
|
1295
|
+
`${worker.label || worker.id} did not join from your devices: ${answer?.ok ? "its device's signature does not hold on your list" : answer?.error}`
|
|
1296
|
+
);
|
|
1297
|
+
joinListedSoon(
|
|
1298
|
+
before.count + 1 < LIST_JOIN_QUICK_TRIES
|
|
1299
|
+
? LIST_JOIN_QUICK_MS
|
|
1300
|
+
: LIST_JOIN_RETRY_MS
|
|
1301
|
+
);
|
|
1302
|
+
continue;
|
|
1303
|
+
}
|
|
1304
|
+
listJoinTried.delete(tried);
|
|
1305
|
+
// A cloud computer is run by Harness for the person's agents: its kind on the signed list says so.
|
|
1306
|
+
const hosted = listed.kind === "cloud" || answer.hosted === true;
|
|
1307
|
+
store.pinOwnMachine(worker.id, worker.publicKey, worker.label, {
|
|
1308
|
+
hosted,
|
|
1309
|
+
});
|
|
1310
|
+
store.meta(`bring-armed:${worker.id}`, "1");
|
|
1311
|
+
log(
|
|
1312
|
+
`confirmed ${worker.label || worker.id} from your devices${hosted ? " (run by Harness, for agents only)" : ""}`
|
|
1313
|
+
);
|
|
1314
|
+
void noticeTo(worker.id, { welcome: true });
|
|
1315
|
+
for (const entry of adding.values())
|
|
1316
|
+
if (entry.state === "waiting" && entry.machine === worker.label)
|
|
1317
|
+
Object.assign(entry, {
|
|
1318
|
+
state: "joined",
|
|
1319
|
+
machine: worker.label,
|
|
1320
|
+
fingerprint: await fingerprint(worker.publicKey),
|
|
1321
|
+
});
|
|
1322
|
+
changed();
|
|
1323
|
+
}
|
|
1324
|
+
};
|
|
1325
|
+
|
|
1326
|
+
/**
|
|
1327
|
+
* On a worker: home asks it to join as one of the person's devices. Its device's signature over both swarm keys holds
|
|
1328
|
+
* on the signed list, so this computer pins it and answers with its own device's signature.
|
|
1329
|
+
*/
|
|
1330
|
+
const onListJoin = async (body) => {
|
|
1331
|
+
const home = homeListed();
|
|
1332
|
+
if (!home || !devices || typeof body?.homeKey !== "string")
|
|
1333
|
+
return { ok: false, error: "this computer has no home to join" };
|
|
1334
|
+
const keys = {
|
|
1335
|
+
homeId: home.id,
|
|
1336
|
+
homeKey: body.homeKey,
|
|
1337
|
+
workerId: machine.id,
|
|
1338
|
+
workerKey: identity().publicKey,
|
|
1339
|
+
};
|
|
1340
|
+
const listed = await devices
|
|
1341
|
+
.checkSwarmProof("home", keys, body.device)
|
|
1342
|
+
.catch(() => null);
|
|
1343
|
+
if (!listed)
|
|
1344
|
+
return {
|
|
1345
|
+
ok: false,
|
|
1346
|
+
code: "not_listed",
|
|
1347
|
+
error: "home is not one of your devices on your list",
|
|
1348
|
+
};
|
|
1349
|
+
// Only as one of the person's devices itself: home confirms this computer only then, so one not on the list yet
|
|
1350
|
+
// keeps waiting for its code rather than trusting a home that will not trust it back.
|
|
1351
|
+
const here = await devices.read().catch(() => null);
|
|
1352
|
+
if (!here?.list.devices.some((device) => device.deviceId === here.deviceId))
|
|
1353
|
+
return {
|
|
1354
|
+
ok: false,
|
|
1355
|
+
code: "not_listed",
|
|
1356
|
+
error: "this computer is not on your devices yet",
|
|
1357
|
+
};
|
|
1358
|
+
const pinned = pinnedHome();
|
|
1359
|
+
if (!pinned || pinned.public_key !== body.homeKey) {
|
|
1360
|
+
const before = acceptedHome();
|
|
1361
|
+
if (before && before !== home.id && before !== machine.id) forget(before);
|
|
1362
|
+
store.pinOwnMachine(home.id, body.homeKey, home.label);
|
|
1363
|
+
store.meta("joined-home", home.id);
|
|
1364
|
+
store.meta("machine-join", "null");
|
|
1365
|
+
accept(home.id);
|
|
1366
|
+
log(
|
|
1367
|
+
`joined your swarm from your devices: ${home.label || "your home computer"} holds the board`
|
|
1368
|
+
);
|
|
1369
|
+
changed();
|
|
1370
|
+
void flush();
|
|
1371
|
+
}
|
|
1372
|
+
return {
|
|
1373
|
+
ok: true,
|
|
1374
|
+
device: await devices.swarmProof("worker", keys),
|
|
1375
|
+
...(machine.hosted === true ? { hosted: true } : {}),
|
|
1376
|
+
};
|
|
1103
1377
|
};
|
|
1104
1378
|
|
|
1105
1379
|
/** On a worker waiting to join: home asks to join with the code the person typed there. */
|
|
1106
1380
|
const onJoin = async (body) => {
|
|
1381
|
+
if (body?.device) return onListJoin(body);
|
|
1107
1382
|
const home = homeListed();
|
|
1108
1383
|
const saved = waitingToJoin() ? joinState() : null;
|
|
1109
1384
|
if (!home || !saved?.code || typeof body?.homeKey !== "string")
|
|
@@ -1160,6 +1435,12 @@ export function createOwnMachines({
|
|
|
1160
1435
|
|
|
1161
1436
|
const notJoined = () => {
|
|
1162
1437
|
const home = homeListed();
|
|
1438
|
+
if (onPersonsList)
|
|
1439
|
+
return {
|
|
1440
|
+
ok: false,
|
|
1441
|
+
code: "unconfirmed",
|
|
1442
|
+
error: `This computer joins your swarm by itself once your home computer${home?.label ? ` (${home.label})` : ""} is on.`,
|
|
1443
|
+
};
|
|
1163
1444
|
return {
|
|
1164
1445
|
ok: false,
|
|
1165
1446
|
code: "unconfirmed",
|
|
@@ -1244,8 +1525,10 @@ export function createOwnMachines({
|
|
|
1244
1525
|
const pinned = pinnedHome();
|
|
1245
1526
|
if (flushing || !pinned) return;
|
|
1246
1527
|
flushing = true;
|
|
1247
|
-
//
|
|
1248
|
-
|
|
1528
|
+
// This computer dials home over WebRTC, introduced by sealed calls to it (machineLink.mjs, introduceLink below):
|
|
1529
|
+
// what it kept for home goes over that channel.
|
|
1530
|
+
directLink?.allow(pinned.machine_id, null, { dialer: true });
|
|
1531
|
+
void directLink?.dial(pinned.machine_id);
|
|
1249
1532
|
try {
|
|
1250
1533
|
for (;;) {
|
|
1251
1534
|
const [next] = outbox();
|
|
@@ -1282,22 +1565,10 @@ export function createOwnMachines({
|
|
|
1282
1565
|
command: "board.machine.info",
|
|
1283
1566
|
agent: "user",
|
|
1284
1567
|
folders: here,
|
|
1568
|
+
home: os.homedir(),
|
|
1285
1569
|
});
|
|
1286
1570
|
if (told?.ok === true) toldHome = said;
|
|
1287
1571
|
}
|
|
1288
|
-
// Each side learns the other's key for a direct link, sealed, so each admits only the other (directLink.mjs).
|
|
1289
|
-
const ownKey = await directLink?.key();
|
|
1290
|
-
if (ownKey) {
|
|
1291
|
-
const swapped = await callOnce(pinned, {
|
|
1292
|
-
command: DIRECT_COMMAND,
|
|
1293
|
-
agent: "user",
|
|
1294
|
-
key: ownKey,
|
|
1295
|
-
});
|
|
1296
|
-
if (typeof swapped?.key === "string") {
|
|
1297
|
-
directLink.allow(pinned.machine_id, swapped.key, { dialer: true });
|
|
1298
|
-
void directLink.dial(pinned.machine_id);
|
|
1299
|
-
}
|
|
1300
|
-
}
|
|
1301
1572
|
// Home is there for this computer: the work it keeps for it may start.
|
|
1302
1573
|
if (toldHome) homeBack();
|
|
1303
1574
|
} finally {
|
|
@@ -1329,66 +1600,15 @@ export function createOwnMachines({
|
|
|
1329
1600
|
return { ...rest, command, agent };
|
|
1330
1601
|
};
|
|
1331
1602
|
|
|
1332
|
-
// A call to home
|
|
1333
|
-
//
|
|
1603
|
+
// A call to home, over the channel between the two computers (its own encryption, opened only with the pinned
|
|
1604
|
+
// computer); null when home cannot be reached.
|
|
1334
1605
|
const callOnce = async (pinned, call) => {
|
|
1335
|
-
const
|
|
1336
|
-
return (
|
|
1337
|
-
(await directCall(pinned, requestId, call)) ??
|
|
1338
|
-
(await sessionCall(pinned, requestId, call))
|
|
1339
|
-
);
|
|
1340
|
-
};
|
|
1341
|
-
const directCall = async (pinned, requestId, call) => {
|
|
1342
|
-
if (!directLink) return null;
|
|
1343
|
-
const reply = await directLink.request(pinned.machine_id, {
|
|
1606
|
+
const reply = await overChannel(pinned.machine_id, {
|
|
1344
1607
|
kind: DIRECT_CALL,
|
|
1345
|
-
body: { requestId, call },
|
|
1608
|
+
body: { requestId: newRequestId(), call },
|
|
1346
1609
|
});
|
|
1347
1610
|
return reply && "answer" in reply ? reply.answer : null;
|
|
1348
1611
|
};
|
|
1349
|
-
const sealedCall = async (pinned, requestId, call, session = null) => {
|
|
1350
|
-
const key = await keyFor(pinned.public_key, machine.id);
|
|
1351
|
-
const envelope = await sealCall(key, machine.id, requestId, call, {
|
|
1352
|
-
session,
|
|
1353
|
-
});
|
|
1354
|
-
return deliver(key, requestId, envelope);
|
|
1355
|
-
};
|
|
1356
|
-
// The session home gave this computer: { home, id }, a handshake in flight, or "none" for a home without sessions.
|
|
1357
|
-
let session = null;
|
|
1358
|
-
const sessionWith = (pinned) => {
|
|
1359
|
-
if (session?.home === pinned.machine_id) return session.id;
|
|
1360
|
-
const asking = (async () => {
|
|
1361
|
-
const answer = await sealedCall(pinned, newRequestId(), {
|
|
1362
|
-
command: SESSION_COMMAND,
|
|
1363
|
-
agent: "user",
|
|
1364
|
-
});
|
|
1365
|
-
const id =
|
|
1366
|
-
answer?.ok === true && typeof answer.session === "string"
|
|
1367
|
-
? answer.session
|
|
1368
|
-
: answer
|
|
1369
|
-
? "none"
|
|
1370
|
-
: null;
|
|
1371
|
-
session = id ? { home: pinned.machine_id, id } : null;
|
|
1372
|
-
return id;
|
|
1373
|
-
})();
|
|
1374
|
-
session = { home: pinned.machine_id, id: asking };
|
|
1375
|
-
return asking;
|
|
1376
|
-
};
|
|
1377
|
-
const sessionCall = async (pinned, requestId, call, again = true) => {
|
|
1378
|
-
const id = await sessionWith(pinned);
|
|
1379
|
-
if (!id) return null;
|
|
1380
|
-
const answer = await sealedCall(
|
|
1381
|
-
pinned,
|
|
1382
|
-
requestId,
|
|
1383
|
-
call,
|
|
1384
|
-
id === "none" ? null : id
|
|
1385
|
-
);
|
|
1386
|
-
if (answer?.code === "session_expired" && again) {
|
|
1387
|
-
if (session?.id === id) session = null;
|
|
1388
|
-
return sessionCall(pinned, requestId, call, false);
|
|
1389
|
-
}
|
|
1390
|
-
return answer;
|
|
1391
|
-
};
|
|
1392
1612
|
|
|
1393
1613
|
// A file on this computer goes to home as its bytes, in parts a sealed call carries.
|
|
1394
1614
|
const attach = async (pinned, request) => {
|
|
@@ -1548,13 +1768,11 @@ export function createOwnMachines({
|
|
|
1548
1768
|
}
|
|
1549
1769
|
};
|
|
1550
1770
|
|
|
1551
|
-
// The answers home gave lately, by the call they answer: the same
|
|
1552
|
-
//
|
|
1553
|
-
//
|
|
1771
|
+
// The answers home gave lately, by the call they answer: the same call arriving again (sent again on a channel that
|
|
1772
|
+
// reopened) gets the same answer, or waits for the first, and never runs twice (the second was once refused as a
|
|
1773
|
+
// replay and the agent told its write failed).
|
|
1554
1774
|
const answered = new Map();
|
|
1555
1775
|
const ANSWERS_KEPT = 500;
|
|
1556
|
-
// The sessions home holds for its computers' calls through Harness (callSessions.mjs).
|
|
1557
|
-
const callSessions = createCallSessions();
|
|
1558
1776
|
const once = (key, run) => {
|
|
1559
1777
|
if (answered.has(key)) return answered.get(key);
|
|
1560
1778
|
const answer = run();
|
|
@@ -1568,11 +1786,11 @@ export function createOwnMachines({
|
|
|
1568
1786
|
// forged one under a real call's id cannot stand in for it.
|
|
1569
1787
|
const sealedKey = (envelope) =>
|
|
1570
1788
|
`${envelope?.deviceId}:${envelope?.requestId}:${envelope?.iv}`;
|
|
1571
|
-
const answerCall = (from, body) =>
|
|
1789
|
+
const answerCall = (from, body, overLink) =>
|
|
1572
1790
|
once(`call:${from}:${sealedKey(body?.envelope)}`, () =>
|
|
1573
|
-
answerCallOnce(from, body)
|
|
1791
|
+
answerCallOnce(from, body, overLink)
|
|
1574
1792
|
);
|
|
1575
|
-
const answerCallOnce = async (from, body) => {
|
|
1793
|
+
const answerCallOnce = async (from, body, overLink) => {
|
|
1576
1794
|
const pinned = store.ownMachine(String(from));
|
|
1577
1795
|
if (!isHome() || !pinned)
|
|
1578
1796
|
return {
|
|
@@ -1587,8 +1805,6 @@ export function createOwnMachines({
|
|
|
1587
1805
|
from,
|
|
1588
1806
|
firstSeen: (requestId) =>
|
|
1589
1807
|
store.firstPhoneRequest(`machine:${from}`, String(requestId)),
|
|
1590
|
-
inSession: (session, requestId) =>
|
|
1591
|
-
callSessions.take(from, session, String(requestId)),
|
|
1592
1808
|
});
|
|
1593
1809
|
// A call that opened but is stale or already taken is answered sealed, so the worker can trust the refusal.
|
|
1594
1810
|
if (!opened.ok)
|
|
@@ -1604,12 +1820,16 @@ export function createOwnMachines({
|
|
|
1604
1820
|
};
|
|
1605
1821
|
const call = opened.call ?? {};
|
|
1606
1822
|
const command = String(call.command || "");
|
|
1607
|
-
//
|
|
1608
|
-
const result =
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1823
|
+
// Through Harness comes only the channel's introduction; everything else goes over the channel (L8.3).
|
|
1824
|
+
const result =
|
|
1825
|
+
overLink || INTRODUCTIONS.has(command)
|
|
1826
|
+
? await answerOpened(from, pinned, call, command, { overLink })
|
|
1827
|
+
: {
|
|
1828
|
+
ok: false,
|
|
1829
|
+
code: "channel_only",
|
|
1830
|
+
error:
|
|
1831
|
+
"this call goes over the channel between your computers, not through Harness",
|
|
1832
|
+
};
|
|
1613
1833
|
return {
|
|
1614
1834
|
envelope: await sealAnswer(key, from, body.envelope.requestId, result),
|
|
1615
1835
|
};
|
|
@@ -1625,16 +1845,47 @@ export function createOwnMachines({
|
|
|
1625
1845
|
const call = body?.call ?? {};
|
|
1626
1846
|
return {
|
|
1627
1847
|
answer: await answerOnce(from, requestId, () =>
|
|
1628
|
-
answerOpened(from, pinned, call, String(call.command || "")
|
|
1848
|
+
answerOpened(from, pinned, call, String(call.command || ""), {
|
|
1849
|
+
overLink: true,
|
|
1850
|
+
})
|
|
1629
1851
|
),
|
|
1630
1852
|
};
|
|
1631
1853
|
};
|
|
1632
1854
|
// What home does with a call it opened from another of the person's computers: the computers' own exchanges, else the
|
|
1633
|
-
// board, as far as that computer may ask (computerMay).
|
|
1634
|
-
const answerOpened = async (
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1855
|
+
// board, as far as that computer may ask (computerMay). `overLink`: it came over their own link, not Harness's relay.
|
|
1856
|
+
const answerOpened = async (
|
|
1857
|
+
from,
|
|
1858
|
+
pinned,
|
|
1859
|
+
call,
|
|
1860
|
+
command,
|
|
1861
|
+
{ overLink = false } = {}
|
|
1862
|
+
) => {
|
|
1863
|
+
// A worker's agent asking for a browser not on that worker: home's own, then its other workers' (L4.7c), over the
|
|
1864
|
+
// computers' link alone.
|
|
1865
|
+
if (command === WINDOWS_COMMAND) {
|
|
1866
|
+
if (!overLink || !mayAskWindows(call.question))
|
|
1867
|
+
return {
|
|
1868
|
+
ok: false,
|
|
1869
|
+
error:
|
|
1870
|
+
"the person's browsers are asked only over the link between their computers",
|
|
1871
|
+
};
|
|
1872
|
+
const route = call.route ?? {};
|
|
1873
|
+
const here = await askOwnWindows(call.question, route);
|
|
1874
|
+
return (
|
|
1875
|
+
(nobodyThere(here) &&
|
|
1876
|
+
(await askOtherWindows(call.question, route, { except: from }))) ||
|
|
1877
|
+
here
|
|
1878
|
+
);
|
|
1879
|
+
}
|
|
1880
|
+
// A worker's WebRTC channel, introduced sealed by that worker, so home admits only it (machineLink.mjs).
|
|
1881
|
+
if (command === LINK_OFFER_COMMAND)
|
|
1882
|
+
return directLink
|
|
1883
|
+
? directLink.offer(from, call)
|
|
1884
|
+
: { ok: false, error: "this computer has no link" };
|
|
1885
|
+
if (command === LINK_CANDIDATES_COMMAND)
|
|
1886
|
+
return directLink
|
|
1887
|
+
? directLink.candidates(from, call)
|
|
1888
|
+
: { ok: false, error: "this computer has no link" };
|
|
1638
1889
|
if (command === VIEW_COMMAND || command === VIEW_DROP_COMMAND) {
|
|
1639
1890
|
const views = liveViews();
|
|
1640
1891
|
if (!views) return { ok: false, error: "home keeps no views" };
|
|
@@ -1729,27 +1980,40 @@ export function createOwnMachines({
|
|
|
1729
1980
|
};
|
|
1730
1981
|
|
|
1731
1982
|
/** On home: a worker's key for a direct link, taken, and home's own given back. */
|
|
1732
|
-
const swapDirectKeys = async (workerId, workerKey) => {
|
|
1733
|
-
const ownKey = await directLink?.key();
|
|
1734
|
-
if (!ownKey) return { ok: true, key: null };
|
|
1735
|
-
directLink.allow(workerId, workerKey);
|
|
1736
|
-
return { ok: true, key: ownKey };
|
|
1737
|
-
};
|
|
1738
1983
|
|
|
1739
1984
|
/** A message that came over a direct link: what Harness's machine_relay would have handed over, from that computer. */
|
|
1985
|
+
// As Harness's machine_relay would hand it over, from the computer at the other end of the channel: sealed calls,
|
|
1986
|
+
// notices and asks are checked against who sealed them by the same handlers (relay below), whatever this computer's
|
|
1987
|
+
// role is now (a home that has just moved hears from the new home on the channel it already had). What goes on the
|
|
1988
|
+
// link unsealed (a call to home, views to a worker) is taken only from the computer this one is linked with for it.
|
|
1740
1989
|
const fromLink = (peerId, body) =>
|
|
1741
|
-
|
|
1742
|
-
?
|
|
1743
|
-
|
|
1744
|
-
|
|
1990
|
+
body?.kind === NERVE_KIND
|
|
1991
|
+
? Promise.resolve(
|
|
1992
|
+
store.ownMachine(String(peerId))
|
|
1993
|
+
? onNerve(String(peerId), body.body)
|
|
1994
|
+
: { ok: false, error: "not one of the person's computers" }
|
|
1995
|
+
)
|
|
1996
|
+
: body?.kind === DIRECT_CALL
|
|
1997
|
+
? isHome()
|
|
1745
1998
|
? answerDirect(peerId, body.body)
|
|
1746
|
-
: Promise.resolve({ ok: false, error: "home takes
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1999
|
+
: Promise.resolve({ ok: false, error: "only home takes calls" })
|
|
2000
|
+
: body?.kind === VIEWS_KIND
|
|
2001
|
+
? peerId === pinnedHome()?.machine_id
|
|
2002
|
+
? Promise.resolve(onViews(body.body))
|
|
2003
|
+
: Promise.resolve({ ok: false, error: "views come only from home" })
|
|
2004
|
+
: body?.kind === CLAIM_KIND
|
|
2005
|
+
? peerId === pinnedHome()?.machine_id
|
|
2006
|
+
? onClaim(body.body)
|
|
2007
|
+
: Promise.resolve({
|
|
2008
|
+
ok: false,
|
|
2009
|
+
error: "only home asks for a claim",
|
|
2010
|
+
})
|
|
2011
|
+
: ["call", "notice", "ask"].includes(body?.kind)
|
|
2012
|
+
? relay(
|
|
2013
|
+
{ from: peerId, kind: body.kind, body: body.body },
|
|
2014
|
+
{ overLink: true }
|
|
2015
|
+
)
|
|
2016
|
+
: Promise.resolve({ ok: false, error: "unknown message" });
|
|
1753
2017
|
|
|
1754
2018
|
/** On home: a read answered as that computer's own call would be (liveViews.mjs keeps it as a view). */
|
|
1755
2019
|
const answerAs = async (workerId, request) => {
|
|
@@ -1762,30 +2026,17 @@ export function createOwnMachines({
|
|
|
1762
2026
|
String(request.command || "")
|
|
1763
2027
|
);
|
|
1764
2028
|
};
|
|
1765
|
-
/**
|
|
1766
|
-
* On home: what changed in a computer's views (liveViews.mjs). Over the direct link as it is; through Harness sealed
|
|
1767
|
-
* for that computer as a notice Harness already carries ("h2v", read only as views), with when it was sent, so an old
|
|
1768
|
-
* one is set aside. Whether it arrived.
|
|
1769
|
-
*/
|
|
2029
|
+
/** On home: what changed in a computer's views (liveViews.mjs), over the channel between them. Whether it arrived. */
|
|
1770
2030
|
const pushViews = async (workerId, body) => {
|
|
1771
2031
|
const pinned = store.ownMachine(String(workerId));
|
|
1772
2032
|
if (!isHome() || !pinned) return false;
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
views: body.views,
|
|
1781
|
-
mirror: body.mirror === true,
|
|
1782
|
-
});
|
|
1783
|
-
const answer = await ask("machine_relay", {
|
|
1784
|
-
to: workerId,
|
|
1785
|
-
kind: "notice",
|
|
1786
|
-
body: { envelope },
|
|
1787
|
-
}).catch(() => null);
|
|
1788
|
-
return answer?.ok === true && !answer.queued;
|
|
2033
|
+
// Views go on an open channel or not at all: with none, the worker's copies ask for them again (liveViews.mjs).
|
|
2034
|
+
const answer = await overChannel(
|
|
2035
|
+
workerId,
|
|
2036
|
+
{ kind: VIEWS_KIND, body },
|
|
2037
|
+
{ wait: false }
|
|
2038
|
+
);
|
|
2039
|
+
return answer?.ok === true;
|
|
1789
2040
|
};
|
|
1790
2041
|
// On a worker: home's push, applied to this computer's copies; one saying the board moved catches its copy up.
|
|
1791
2042
|
const onViews = (body) => {
|
|
@@ -1794,21 +2045,6 @@ export function createOwnMachines({
|
|
|
1794
2045
|
if (body?.mirror === true) onNotice({ mirror: true });
|
|
1795
2046
|
return copies.onPush(body);
|
|
1796
2047
|
};
|
|
1797
|
-
// On a worker: home's views, from the home this computer follows, opened and handed to its copies; null when the
|
|
1798
|
-
// envelope is not views.
|
|
1799
|
-
const openViews = async (envelope) => {
|
|
1800
|
-
const pinned = pinnedHome();
|
|
1801
|
-
if (!pinned || envelope?.deviceId !== machine.id) return null;
|
|
1802
|
-
try {
|
|
1803
|
-
const key = await keyFor(pinned.public_key, machine.id);
|
|
1804
|
-
const opened = await openSealed(key, VIEWS_SEAL, envelope);
|
|
1805
|
-
if (Math.abs(Date.now() - Number(opened.ts)) > MAX_SKEW_MS)
|
|
1806
|
-
return { ok: true };
|
|
1807
|
-
return onViews({ views: opened.views, mirror: opened.mirror === true });
|
|
1808
|
-
} catch {
|
|
1809
|
-
return null;
|
|
1810
|
-
}
|
|
1811
|
-
};
|
|
1812
2048
|
|
|
1813
2049
|
/** On home: a notice for a worker (a run to stop), kept by Harness while it is offline. */
|
|
1814
2050
|
const noticeTo = async (workerId, notice) => {
|
|
@@ -1849,7 +2085,11 @@ export function createOwnMachines({
|
|
|
1849
2085
|
if (inventoryRequests.get(workerId) !== request) break;
|
|
1850
2086
|
if (
|
|
1851
2087
|
answer?.ok === true &&
|
|
1852
|
-
learn(workerId, {
|
|
2088
|
+
learn(workerId, {
|
|
2089
|
+
request: request.id,
|
|
2090
|
+
folders: answer.folders,
|
|
2091
|
+
home: answer.home,
|
|
2092
|
+
})
|
|
1853
2093
|
)
|
|
1854
2094
|
break;
|
|
1855
2095
|
if (answer) break;
|
|
@@ -1874,6 +2114,7 @@ export function createOwnMachines({
|
|
|
1874
2114
|
command: "board.machine.info",
|
|
1875
2115
|
agent: "user",
|
|
1876
2116
|
folders: here,
|
|
2117
|
+
home: os.homedir(),
|
|
1877
2118
|
inventoryRequest: request,
|
|
1878
2119
|
});
|
|
1879
2120
|
if (reported?.ok === true) toldHome = JSON.stringify(here);
|
|
@@ -1904,8 +2145,6 @@ export function createOwnMachines({
|
|
|
1904
2145
|
return null;
|
|
1905
2146
|
};
|
|
1906
2147
|
const onNoticeFromHome = async (body) => {
|
|
1907
|
-
const views = await openViews(body?.envelope);
|
|
1908
|
-
if (views) return views;
|
|
1909
2148
|
const opened = await openNoticeFromAny(body);
|
|
1910
2149
|
if (!opened) return { ok: false };
|
|
1911
2150
|
const { from, notice } = opened;
|
|
@@ -1913,6 +2152,9 @@ export function createOwnMachines({
|
|
|
1913
2152
|
onRoleStatement(from, notice.role);
|
|
1914
2153
|
return { ok: true };
|
|
1915
2154
|
}
|
|
2155
|
+
// What happened on that computer, out of Harness's mailbox (nerves.mjs): taken in order, from any computer confirmed
|
|
2156
|
+
// here, since a worker's own stream says where it stands.
|
|
2157
|
+
if (notice.nerve) return onNerve(from, notice.nerve);
|
|
1916
2158
|
// Everything else is the word of the home this computer follows; another's is heard and set aside.
|
|
1917
2159
|
if (from !== acceptedHome()) return { ok: true };
|
|
1918
2160
|
if (!isHome() && typeof notice.inventoryRequest === "string") {
|
|
@@ -2004,17 +2246,29 @@ export function createOwnMachines({
|
|
|
2004
2246
|
answer?.ok === false && ["unpaired", "closed"].includes(answer.code);
|
|
2005
2247
|
|
|
2006
2248
|
/**
|
|
2007
|
-
*
|
|
2008
|
-
*
|
|
2009
|
-
*
|
|
2249
|
+
* A question for the person's Harness windows on their other computers (each computer asks only the browsers running
|
|
2250
|
+
* on it, docs/one-link-plan.md L4.7): on home, each confirmed worker that is connected, in turn, the one `route`
|
|
2251
|
+
* names first; on a worker, home over their link, which asks its own and its other workers'. The first that has one
|
|
2252
|
+
* answers; null when none was asked or none had a window. Only ever over the computers' own link, never Harness's
|
|
2253
|
+
* relay.
|
|
2010
2254
|
*/
|
|
2011
|
-
const
|
|
2012
|
-
if (!isHome()) return
|
|
2255
|
+
const askOtherWindows = async (call, route = {}, { except = null } = {}) => {
|
|
2256
|
+
if (!isHome()) return askHomeWindows(call, route);
|
|
2257
|
+
const workers = siblings.filter(
|
|
2258
|
+
(sibling) =>
|
|
2259
|
+
sibling.role === "worker" && sibling.connected && sibling.id !== except
|
|
2260
|
+
);
|
|
2261
|
+
const named = workers.filter((worker) => worker.id === route.computer);
|
|
2013
2262
|
let found = null;
|
|
2014
|
-
for (const worker of
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2263
|
+
for (const worker of [
|
|
2264
|
+
...named,
|
|
2265
|
+
...workers.filter((worker) => worker.id !== route.computer),
|
|
2266
|
+
]) {
|
|
2267
|
+
const answer = await askWorker(
|
|
2268
|
+
worker.id,
|
|
2269
|
+
{ ...call, route },
|
|
2270
|
+
{ timeoutMs: route.timeoutMs }
|
|
2271
|
+
);
|
|
2018
2272
|
if (!answer) continue;
|
|
2019
2273
|
if (!nobodyThere(answer)) return answer;
|
|
2020
2274
|
found ??= answer;
|
|
@@ -2022,18 +2276,44 @@ export function createOwnMachines({
|
|
|
2022
2276
|
return found;
|
|
2023
2277
|
};
|
|
2024
2278
|
|
|
2025
|
-
/** On
|
|
2026
|
-
const
|
|
2279
|
+
/** On a worker: home's answer for its windows, asked over the computers' link alone; null with no link or answer. */
|
|
2280
|
+
const askHomeWindows = async (call, route) => {
|
|
2281
|
+
const pinned = pinnedHome();
|
|
2282
|
+
if (!pinned) return null;
|
|
2283
|
+
const reply = await overChannel(
|
|
2284
|
+
pinned.machine_id,
|
|
2285
|
+
{
|
|
2286
|
+
kind: DIRECT_CALL,
|
|
2287
|
+
body: {
|
|
2288
|
+
requestId: newRequestId(),
|
|
2289
|
+
call: {
|
|
2290
|
+
command: WINDOWS_COMMAND,
|
|
2291
|
+
agent: "user",
|
|
2292
|
+
question: call,
|
|
2293
|
+
route,
|
|
2294
|
+
},
|
|
2295
|
+
},
|
|
2296
|
+
},
|
|
2297
|
+
route.timeoutMs ? { timeoutMs: route.timeoutMs } : {}
|
|
2298
|
+
);
|
|
2299
|
+
return reply && "answer" in reply ? reply.answer : null;
|
|
2300
|
+
};
|
|
2301
|
+
|
|
2302
|
+
/**
|
|
2303
|
+
* On home: one sealed question for a confirmed worker, over the channel between them, and its opened answer; null
|
|
2304
|
+
* when none came back. `timeoutMs` is the question's own deadline.
|
|
2305
|
+
*/
|
|
2306
|
+
const askWorker = async (workerId, call, { timeoutMs } = {}) => {
|
|
2027
2307
|
const pinned = store.ownMachine(workerId);
|
|
2028
2308
|
if (!pinned) return null;
|
|
2029
2309
|
const key = await keyFor(pinned.public_key, workerId);
|
|
2030
2310
|
const requestId = newRequestId();
|
|
2031
2311
|
const envelope = await sealAsk(key, workerId, requestId, call);
|
|
2032
|
-
const reply = await
|
|
2033
|
-
|
|
2034
|
-
kind: "ask",
|
|
2035
|
-
|
|
2036
|
-
|
|
2312
|
+
const reply = await overChannel(
|
|
2313
|
+
workerId,
|
|
2314
|
+
{ kind: "ask", body: { envelope } },
|
|
2315
|
+
{ timeoutMs }
|
|
2316
|
+
).catch(() => null);
|
|
2037
2317
|
if (!reply?.envelope) return null;
|
|
2038
2318
|
return openAskAnswer(key, reply.envelope, { workerId, requestId }).catch(
|
|
2039
2319
|
() => null
|
|
@@ -2098,9 +2378,20 @@ export function createOwnMachines({
|
|
|
2098
2378
|
}
|
|
2099
2379
|
};
|
|
2100
2380
|
|
|
2381
|
+
// A question as the windows are asked it, without where it was sent (windowAsks.mjs).
|
|
2382
|
+
const withoutRoute = ({ route: _route, ...question }) => question;
|
|
2383
|
+
|
|
2101
2384
|
/** On a worker: home's question for the windows linked here, opened, asked and the answer sealed back. */
|
|
2102
|
-
|
|
2103
|
-
|
|
2385
|
+
// Home's questions come over the channel between the computers alone, never through Harness (L8.3).
|
|
2386
|
+
const onAskFromHome = (body, overLink) =>
|
|
2387
|
+
overLink
|
|
2388
|
+
? once(`ask:${sealedKey(body?.envelope)}`, () => onAskFromHomeOnce(body))
|
|
2389
|
+
: Promise.resolve({
|
|
2390
|
+
ok: false,
|
|
2391
|
+
code: "channel_only",
|
|
2392
|
+
error:
|
|
2393
|
+
"home's questions come over the channel between your computers, not through Harness",
|
|
2394
|
+
});
|
|
2104
2395
|
const onAskFromHomeOnce = async (body) => {
|
|
2105
2396
|
const pinned = pinnedHome();
|
|
2106
2397
|
if (!pinned) return { ok: false, error: "this computer is not confirmed" };
|
|
@@ -2121,7 +2412,7 @@ export function createOwnMachines({
|
|
|
2121
2412
|
typeof call.capability === "string" && answerCapability
|
|
2122
2413
|
? await answerCapability(call.capability)
|
|
2123
2414
|
: call.inventory === true
|
|
2124
|
-
? { ok: true, folders: folders({ fresh: true }) }
|
|
2415
|
+
? { ok: true, folders: folders({ fresh: true }), home: os.homedir() }
|
|
2125
2416
|
: call.folders
|
|
2126
2417
|
? await ownFolders(call.folders)
|
|
2127
2418
|
: call.home === true
|
|
@@ -2136,12 +2427,17 @@ export function createOwnMachines({
|
|
|
2136
2427
|
? ownSshAllow(call.ssh)
|
|
2137
2428
|
: call.update
|
|
2138
2429
|
? ownUpdateChecked(call.update)
|
|
2139
|
-
:
|
|
2140
|
-
?
|
|
2141
|
-
:
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2430
|
+
: call.version === true
|
|
2431
|
+
? ownVersion()
|
|
2432
|
+
: mayAskWindows(call)
|
|
2433
|
+
? await askOwnWindows(
|
|
2434
|
+
withoutRoute(call),
|
|
2435
|
+
call.route ?? {}
|
|
2436
|
+
)
|
|
2437
|
+
: {
|
|
2438
|
+
ok: false,
|
|
2439
|
+
error: `home cannot ask this computer's windows for ${call.tool}`,
|
|
2440
|
+
};
|
|
2145
2441
|
return {
|
|
2146
2442
|
envelope: await sealAskAnswer(
|
|
2147
2443
|
key,
|
|
@@ -2152,16 +2448,19 @@ export function createOwnMachines({
|
|
|
2152
2448
|
};
|
|
2153
2449
|
};
|
|
2154
2450
|
|
|
2155
|
-
/**
|
|
2156
|
-
|
|
2451
|
+
/**
|
|
2452
|
+
* A message from another of the person's computers: a call (on home), a join, a notice or a question (on a worker).
|
|
2453
|
+
* `overLink`: it came over the channel between them, said only by fromLink, never by what Harness carried.
|
|
2454
|
+
*/
|
|
2455
|
+
const relay = (input, { overLink = false } = {}) =>
|
|
2157
2456
|
input?.kind === "call"
|
|
2158
|
-
? answerCall(String(input.from || ""), input.body)
|
|
2457
|
+
? answerCall(String(input.from || ""), input.body, overLink)
|
|
2159
2458
|
: input?.kind === "join"
|
|
2160
2459
|
? Promise.resolve(onJoin(input.body))
|
|
2161
2460
|
: input?.kind === "notice"
|
|
2162
2461
|
? onNoticeFromHome(input.body)
|
|
2163
2462
|
: input?.kind === "ask"
|
|
2164
|
-
? onAskFromHome(input.body)
|
|
2463
|
+
? onAskFromHome(input.body, overLink)
|
|
2165
2464
|
: Promise.resolve({ ok: false, error: "unknown message" });
|
|
2166
2465
|
|
|
2167
2466
|
// ---- what the person and status read ----
|
|
@@ -2640,7 +2939,8 @@ export function createOwnMachines({
|
|
|
2640
2939
|
store.ownMachines().find((pinned) => pinned.label === named)
|
|
2641
2940
|
?.machine_id ??
|
|
2642
2941
|
String(named || machine.id);
|
|
2643
|
-
if (target === machine.id)
|
|
2942
|
+
if (target === machine.id)
|
|
2943
|
+
return ownUpdateChecked({ version: spec }, { here: true });
|
|
2644
2944
|
if (!isHome())
|
|
2645
2945
|
return {
|
|
2646
2946
|
ok: false,
|
|
@@ -2671,7 +2971,7 @@ export function createOwnMachines({
|
|
|
2671
2971
|
return false;
|
|
2672
2972
|
};
|
|
2673
2973
|
/** On the computer updated: the same checks again, as home's word is not taken for them. */
|
|
2674
|
-
const ownUpdateChecked = ({ version } = {}) => {
|
|
2974
|
+
const ownUpdateChecked = ({ version } = {}, { here = false } = {}) => {
|
|
2675
2975
|
const spec = String(version || "latest");
|
|
2676
2976
|
if (spec !== "latest" && !/^\d+\.\d+\.\d+$/.test(spec))
|
|
2677
2977
|
return { ok: false, error: `${spec} is not a release` };
|
|
@@ -2680,7 +2980,47 @@ export function createOwnMachines({
|
|
|
2680
2980
|
ok: false,
|
|
2681
2981
|
error: `${labelOf(machine.id) ?? "This computer"} runs ${runningVersion}; it never moves back to ${spec}.`,
|
|
2682
2982
|
};
|
|
2683
|
-
return ownUpdate(spec);
|
|
2983
|
+
return ownUpdate(spec, { here });
|
|
2984
|
+
};
|
|
2985
|
+
|
|
2986
|
+
const ownVersion = () => ({
|
|
2987
|
+
ok: true,
|
|
2988
|
+
version: runningVersion,
|
|
2989
|
+
update: ownUpdateState(),
|
|
2990
|
+
});
|
|
2991
|
+
|
|
2992
|
+
/**
|
|
2993
|
+
* Every computer's release as this one can know it, this one first (releaseNotice.mjs). Home asks each of the person's
|
|
2994
|
+
* computers that is online. One that answers but does not know the question runs a release from before computers said
|
|
2995
|
+
* theirs, so it is older than any release since. A worker speaks for itself alone.
|
|
2996
|
+
*/
|
|
2997
|
+
const versions = async () => {
|
|
2998
|
+
const own = {
|
|
2999
|
+
id: machine.id,
|
|
3000
|
+
label: machine.label,
|
|
3001
|
+
here: true,
|
|
3002
|
+
connected: true,
|
|
3003
|
+
version: runningVersion,
|
|
3004
|
+
update: ownUpdateState(),
|
|
3005
|
+
};
|
|
3006
|
+
if (!isHome()) return [own];
|
|
3007
|
+
const workers = await Promise.all(
|
|
3008
|
+
siblings.filter(confirmed).map(async (worker) => {
|
|
3009
|
+
const answer = worker.connected
|
|
3010
|
+
? await askWorker(worker.id, { version: true }).catch(() => null)
|
|
3011
|
+
: null;
|
|
3012
|
+
return {
|
|
3013
|
+
id: worker.id,
|
|
3014
|
+
label: worker.label,
|
|
3015
|
+
here: false,
|
|
3016
|
+
connected: worker.connected === true,
|
|
3017
|
+
version: answer?.ok ? String(answer.version) : null,
|
|
3018
|
+
older: Boolean(answer) && !answer.ok,
|
|
3019
|
+
update: answer?.ok ? (answer.update ?? null) : null,
|
|
3020
|
+
};
|
|
3021
|
+
})
|
|
3022
|
+
);
|
|
3023
|
+
return [own, ...workers];
|
|
2684
3024
|
};
|
|
2685
3025
|
|
|
2686
3026
|
// ---- the person's SSH key into a cloud computer (cloudSsh.mjs) ----
|
|
@@ -2849,7 +3189,11 @@ export function createOwnMachines({
|
|
|
2849
3189
|
repository: folder.repository.slice(0, 300),
|
|
2850
3190
|
}));
|
|
2851
3191
|
const receivedAt = new Date().toISOString();
|
|
2852
|
-
|
|
3192
|
+
const home =
|
|
3193
|
+
typeof info.home === "string" && path.isAbsolute(info.home)
|
|
3194
|
+
? info.home
|
|
3195
|
+
: (infos.get(id)?.home ?? null);
|
|
3196
|
+
infos.set(id, { folders, home, receivedAt, current: true });
|
|
2853
3197
|
inventoryRequests.delete(id);
|
|
2854
3198
|
log(
|
|
2855
3199
|
`repository inventory received from ${labelOf(id) ?? id}: ${info.folders.length} folders`
|
|
@@ -2907,6 +3251,54 @@ export function createOwnMachines({
|
|
|
2907
3251
|
);
|
|
2908
3252
|
};
|
|
2909
3253
|
|
|
3254
|
+
const inside = (dir, root) => {
|
|
3255
|
+
const relative = path.relative(root, dir);
|
|
3256
|
+
return !relative.startsWith("..") && !path.isAbsolute(relative);
|
|
3257
|
+
};
|
|
3258
|
+
// A folder reached through a link here (macOS's /tmp) is listed under its real path.
|
|
3259
|
+
const listsFolder = (id, dir) => {
|
|
3260
|
+
const spellings = fs.existsSync(dir) ? [dir, fs.realpathSync(dir)] : [dir];
|
|
3261
|
+
return (infos.get(id)?.folders ?? []).some((each) =>
|
|
3262
|
+
spellings.some((spelling) => inside(spelling, each.path))
|
|
3263
|
+
);
|
|
3264
|
+
};
|
|
3265
|
+
|
|
3266
|
+
/**
|
|
3267
|
+
* On home: the computer and folder a task runs in, since a path means nothing without its computer. Named for another
|
|
3268
|
+
* computer: a folder it lists or home does not have is its own; one of home's becomes its checkout of that repository.
|
|
3269
|
+
* Placed here: a folder home does not have goes to the computer that lists it. What is still not there falls back to a
|
|
3270
|
+
* scratch folder where the run starts (machineRuns.mjs, runWorker.mjs). `leftOut`: nobody named this folder, it is only
|
|
3271
|
+
* where the poster works, so with no checkout of it there the work carries none (owner, November 5: a task for pop-os
|
|
3272
|
+
* asked for a Start, then failed on a Mac folder).
|
|
3273
|
+
*/
|
|
3274
|
+
const placeFolder = ({ agent, folder, target, named, leftOut = false }) => {
|
|
3275
|
+
const unchanged = { machine: target, folder };
|
|
3276
|
+
if (!folder || !isHome()) return unchanged;
|
|
3277
|
+
const hereToo = fs.existsSync(folder);
|
|
3278
|
+
if (target && target !== machine.id) {
|
|
3279
|
+
if (!hereToo || listsFolder(target, folder)) return unchanged;
|
|
3280
|
+
return {
|
|
3281
|
+
machine: target,
|
|
3282
|
+
folder: checkoutOn(target, folder) ?? (leftOut ? null : folder),
|
|
3283
|
+
};
|
|
3284
|
+
}
|
|
3285
|
+
if (hereToo || named) return unchanged;
|
|
3286
|
+
const holder = siblings.find(
|
|
3287
|
+
(each) =>
|
|
3288
|
+
each.agents.includes(agent) &&
|
|
3289
|
+
store.ownMachine(each.id) &&
|
|
3290
|
+
listsFolder(each.id, folder)
|
|
3291
|
+
);
|
|
3292
|
+
return holder ? { machine: holder.id, folder } : unchanged;
|
|
3293
|
+
};
|
|
3294
|
+
|
|
3295
|
+
/** On home: whether a folder is one that computer runs home's work in: as machineRuns.mjs refusalFor decides there. */
|
|
3296
|
+
const runsWorkIn = (id, dir) => {
|
|
3297
|
+
if (listsFolder(id, dir)) return true;
|
|
3298
|
+
const home = infos.get(id)?.home;
|
|
3299
|
+
return Boolean(home) && dir !== home && inside(dir, home);
|
|
3300
|
+
};
|
|
3301
|
+
|
|
2910
3302
|
/** On home: another computer's folders for the composer (`{ places: true }` or `{ browse }`), asked there. */
|
|
2911
3303
|
const foldersOn = async (id, question) => {
|
|
2912
3304
|
const label = labelOf(id) ?? "that computer";
|
|
@@ -2999,49 +3391,46 @@ export function createOwnMachines({
|
|
|
2999
3391
|
saveMachine(machine);
|
|
3000
3392
|
}
|
|
3001
3393
|
|
|
3002
|
-
|
|
3003
|
-
|
|
3004
|
-
|
|
3005
|
-
|
|
3006
|
-
|
|
3007
|
-
|
|
3008
|
-
|
|
3009
|
-
|
|
3010
|
-
|
|
3011
|
-
const links = new Map(directLink.status().map((link) => [link.id, link]));
|
|
3012
|
-
const now = Date.now();
|
|
3013
|
-
let stuck = false;
|
|
3014
|
-
for (const sibling of siblings) {
|
|
3015
|
-
const link = links.get(sibling.id);
|
|
3016
|
-
const waiting =
|
|
3017
|
-
sibling.role === "worker" &&
|
|
3018
|
-
sibling.connected &&
|
|
3019
|
-
store.ownMachine(sibling.id) &&
|
|
3020
|
-
link &&
|
|
3021
|
-
!link.linked;
|
|
3022
|
-
if (!waiting) {
|
|
3023
|
-
unlinkedSince.delete(sibling.id);
|
|
3024
|
-
continue;
|
|
3025
|
-
}
|
|
3026
|
-
if (!unlinkedSince.has(sibling.id)) unlinkedSince.set(sibling.id, now);
|
|
3027
|
-
if (now - unlinkedSince.get(sibling.id) >= UNLINKED_MS) stuck = true;
|
|
3028
|
-
}
|
|
3029
|
-
if (!stuck || now - relinkedAt < RELINK_EVERY_MS) return;
|
|
3030
|
-
relinkedAt = now;
|
|
3031
|
-
unlinkedSince.clear();
|
|
3032
|
-
log(
|
|
3033
|
-
"direct links: a computer online through Harness has not linked for 2 min"
|
|
3034
|
-
);
|
|
3035
|
-
void directLink
|
|
3036
|
-
.restart?.()
|
|
3037
|
-
.catch((error) =>
|
|
3038
|
-
log(`direct links: the listener did not start again (${error.message})`)
|
|
3039
|
-
);
|
|
3040
|
-
}, 30_000);
|
|
3041
|
-
watchLinks.unref?.();
|
|
3394
|
+
/** On home: the claim of the worker `workerId` that its agent wrote words, or null when it cannot give one now. */
|
|
3395
|
+
const claimFrom = async (workerId, ask) => {
|
|
3396
|
+
if (!isHome() || !store.ownMachine(String(workerId))) return null;
|
|
3397
|
+
const answer = await overChannel(String(workerId), {
|
|
3398
|
+
kind: CLAIM_KIND,
|
|
3399
|
+
body: ask,
|
|
3400
|
+
}).catch(() => null);
|
|
3401
|
+
return answer?.ok ? (answer.claim ?? null) : null;
|
|
3402
|
+
};
|
|
3042
3403
|
|
|
3043
3404
|
return {
|
|
3405
|
+
claimFrom,
|
|
3044
3406
|
hello,
|
|
3407
|
+
/** A channel to another computer opened, stalled or ended: home may have become reachable or not (watchHome). */
|
|
3408
|
+
linkChanged: () => watchHome(),
|
|
3409
|
+
/** Whether Harness lists that computer as on (machineLink.mjs dials home again only then). */
|
|
3410
|
+
listedOn: peerOn,
|
|
3411
|
+
/** On a worker: one sealed introduction call to home for its WebRTC channel, through Harness (machineLink.mjs). */
|
|
3412
|
+
introduceLink: async (call) => {
|
|
3413
|
+
const pinned = pinnedHome();
|
|
3414
|
+
if (!pinned) return null;
|
|
3415
|
+
const key = await keyFor(pinned.public_key, machine.id);
|
|
3416
|
+
const requestId = newRequestId();
|
|
3417
|
+
const envelope = await sealCall(key, machine.id, requestId, call);
|
|
3418
|
+
const answer = await ask("machine_relay", {
|
|
3419
|
+
to: "home",
|
|
3420
|
+
kind: "call",
|
|
3421
|
+
body: { envelope },
|
|
3422
|
+
}).catch(() => null);
|
|
3423
|
+
if (!answer?.envelope) return answer;
|
|
3424
|
+
return openAnswer(key, answer.envelope, {
|
|
3425
|
+
workerId: machine.id,
|
|
3426
|
+
requestId,
|
|
3427
|
+
}).catch(() => ({
|
|
3428
|
+
ok: false,
|
|
3429
|
+
error: "the answer did not come from home",
|
|
3430
|
+
}));
|
|
3431
|
+
},
|
|
3432
|
+
versions,
|
|
3433
|
+
actsAlone,
|
|
3045
3434
|
adopt,
|
|
3046
3435
|
introduce,
|
|
3047
3436
|
rename,
|
|
@@ -3049,15 +3438,17 @@ export function createOwnMachines({
|
|
|
3049
3438
|
leave,
|
|
3050
3439
|
remove,
|
|
3051
3440
|
removed,
|
|
3441
|
+
removedFromList,
|
|
3052
3442
|
confirm,
|
|
3053
3443
|
add,
|
|
3444
|
+
match,
|
|
3054
3445
|
forwards,
|
|
3055
3446
|
callHome,
|
|
3056
3447
|
relay,
|
|
3057
3448
|
noticeTo,
|
|
3058
3449
|
answerAs,
|
|
3059
3450
|
pushViews,
|
|
3060
|
-
|
|
3451
|
+
askOtherWindows,
|
|
3061
3452
|
// Whether a worker can do this work, by an agent there looking (capabilityCheck.mjs); null when it cannot be asked.
|
|
3062
3453
|
capabilityOf: (workerId, work) =>
|
|
3063
3454
|
isHome() && store.ownMachine(workerId)?.hosted !== true
|
|
@@ -3066,6 +3457,31 @@ export function createOwnMachines({
|
|
|
3066
3457
|
linkCall,
|
|
3067
3458
|
tellWorkers,
|
|
3068
3459
|
fromLink,
|
|
3460
|
+
/**
|
|
3461
|
+
* The computers this one keeps a nerve with (nerves.mjs): on home each confirmed computer, on a worker its home.
|
|
3462
|
+
* `{ id, label }`.
|
|
3463
|
+
*/
|
|
3464
|
+
nervePeers: () =>
|
|
3465
|
+
(isHome()
|
|
3466
|
+
? store.ownMachines().filter((each) => each.machine_id !== machine.id)
|
|
3467
|
+
: [pinnedHome()].filter(Boolean)
|
|
3468
|
+
).map((each) => ({
|
|
3469
|
+
id: each.machine_id,
|
|
3470
|
+
label: labelOf(each.machine_id) || each.label || "",
|
|
3471
|
+
})),
|
|
3472
|
+
/** Whether the channel to that computer is open now. */
|
|
3473
|
+
linkedTo: (id) =>
|
|
3474
|
+
(directLink?.status() ?? []).some(
|
|
3475
|
+
(link) => link.id === id && link.linked
|
|
3476
|
+
),
|
|
3477
|
+
/** One nerve message over the open channel to that computer: its answer, or null. */
|
|
3478
|
+
nerveOver: (id, message) => overChannel(id, message, { wait: false }),
|
|
3479
|
+
/** Home: one nerve message for a worker into Harness's mailbox; true once Harness took it. */
|
|
3480
|
+
nerveMail: async (id, message) => {
|
|
3481
|
+
if (!isHome()) return null;
|
|
3482
|
+
const answer = await noticeTo(id, { nerve: message.body });
|
|
3483
|
+
return answer?.ok === true;
|
|
3484
|
+
},
|
|
3069
3485
|
standingIn,
|
|
3070
3486
|
someoneStandsIn,
|
|
3071
3487
|
situation,
|
|
@@ -3099,7 +3515,9 @@ export function createOwnMachines({
|
|
|
3099
3515
|
learn,
|
|
3100
3516
|
refreshInventory,
|
|
3101
3517
|
placeElsewhere,
|
|
3518
|
+
placeFolder,
|
|
3102
3519
|
placeOn,
|
|
3520
|
+
runsWorkIn,
|
|
3103
3521
|
checkoutOn,
|
|
3104
3522
|
foldersOn,
|
|
3105
3523
|
addressIn,
|