tryharness 0.7.83 → 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 +191 -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 +52 -0
- 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 +2426 -338
- 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 +1 -1
- 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/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 +211 -129
- 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
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
// The person's computers with each other over WebRTC (docs/one-link-plan.md L4; owner, November 12, taking the
|
|
2
|
+
// recommendation): the same interface ownMachines.mjs used for the HyperDHT direct link (allow, disallow, dial,
|
|
3
|
+
// request, status, restart, stop), now one WebRTC channel between a worker and home.
|
|
4
|
+
//
|
|
5
|
+
// The worker dials, as before. Its channel is introduced by two sealed calls to home under the pair key the two
|
|
6
|
+
// computers pinned when they joined ("machine.link.offer", "machine.link.candidates", answered in ownMachines.mjs):
|
|
7
|
+
// Harness carries them unread, and home answers only the computer that sealed them. WebRTC's own encryption is keyed by
|
|
8
|
+
// the fingerprints in that offer and answer, which only the two computers saw in the clear, so a channel that opens
|
|
9
|
+
// is between exactly them, as a pinned HyperDHT key was. What goes over it is what went over the direct link: calls,
|
|
10
|
+
// notices, asks and views, as JSON, request and answer. Harness carries only the introduction, a join, and a notice
|
|
11
|
+
// for a computer with no channel (the mailbox, which also wakes a cloud computer that sleeps).
|
|
12
|
+
|
|
13
|
+
import { randomBytes } from "node:crypto";
|
|
14
|
+
import { ASK_TIMEOUT_MS } from "./bridge.mjs";
|
|
15
|
+
import { piecesOf, wayOf, wrtc } from "./link.mjs";
|
|
16
|
+
|
|
17
|
+
// The channel's label between two computers; a page's is "harness" (link.mjs).
|
|
18
|
+
export const MACHINE_LABEL = "harness-machine";
|
|
19
|
+
// A message larger than a board page or file part with room for its fields is not one (the direct link's 4 MiB).
|
|
20
|
+
const MAX_MESSAGE_CHARS = 6_000_000;
|
|
21
|
+
// Every address found here goes to the other end: between one person's own computers nothing is hidden.
|
|
22
|
+
const shareAll = (candidate) => candidate;
|
|
23
|
+
const sdpAsIs = (sdp) => sdp;
|
|
24
|
+
const MAX_CANDIDATES = 32;
|
|
25
|
+
const SESSION = /^[A-Za-z0-9_-]{8,64}$/;
|
|
26
|
+
|
|
27
|
+
const STUN = [{ urls: "stun:stun.cloudflare.com:3478" }];
|
|
28
|
+
// How often a computer waiting for a channel it does not dial looks whether it opened.
|
|
29
|
+
const OPEN_CHECK_MS = 100;
|
|
30
|
+
// A worker whose channel to home ended, or did not open, dials again after a pause that doubles up to a minute.
|
|
31
|
+
const REDIAL_FIRST_MS = 1_000;
|
|
32
|
+
const REDIAL_MOST_MS = 60_000;
|
|
33
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* - `onChange(peerId)`: a channel opened, stalled or ended.
|
|
37
|
+
* - `onRequest(peerId, body)`: what this computer answers for the other (ownMachines.mjs fromLink).
|
|
38
|
+
* - `introduce(peerId, call)`: on a worker, one sealed introduction call to home, answered opened (ownMachines.mjs).
|
|
39
|
+
* - `iceServers()`: the servers this end uses (link.mjs createIceServers).
|
|
40
|
+
* - `timeoutMs`: how long a request waits for its answer (bridge.mjs ASK_TIMEOUT_MS, as the direct link waited).
|
|
41
|
+
* - `label`, `name`, `maxMessageChars`: the channel's label, the name its log lines carry, and its largest message.
|
|
42
|
+
* - `share(candidate)`: an address found here as the other end may see it, or null to keep it here; `shareSdp(sdp)`
|
|
43
|
+
* the same for the offer or answer (another person's computer is sent no local-network address, peopleLink.mjs).
|
|
44
|
+
* - `commands`: the introduction's two call names (`offer`, `candidates`).
|
|
45
|
+
* - `redial(peerId)`: whether a computer this one dials is dialed again when its channel ends or does not open (a
|
|
46
|
+
* worker, while Harness lists home as on); none by default.
|
|
47
|
+
*/
|
|
48
|
+
export function createMachineLink({
|
|
49
|
+
log = () => {},
|
|
50
|
+
// Told when a channel opens, stalls or ends: whether home is reachable is looked at again (ownMachines.mjs).
|
|
51
|
+
onChange = () => {},
|
|
52
|
+
onRequest,
|
|
53
|
+
introduce = async () => ({ ok: false, error: "no introductions" }),
|
|
54
|
+
iceServers = () => STUN,
|
|
55
|
+
timeoutMs = ASK_TIMEOUT_MS,
|
|
56
|
+
// With no WebRTC on this computer (link.mjs), no channel opens and the other computers cannot be reached.
|
|
57
|
+
Peer = wrtc?.RTCPeerConnection,
|
|
58
|
+
label = MACHINE_LABEL,
|
|
59
|
+
name = "machine link",
|
|
60
|
+
maxMessageChars = MAX_MESSAGE_CHARS,
|
|
61
|
+
share = shareAll,
|
|
62
|
+
shareSdp = sdpAsIs,
|
|
63
|
+
commands = {
|
|
64
|
+
offer: "machine.link.offer",
|
|
65
|
+
candidates: "machine.link.candidates",
|
|
66
|
+
},
|
|
67
|
+
redial = () => false,
|
|
68
|
+
}) {
|
|
69
|
+
const shared = (candidate) =>
|
|
70
|
+
share({
|
|
71
|
+
candidate: candidate.candidate,
|
|
72
|
+
sdpMid: candidate.sdpMid,
|
|
73
|
+
sdpMLineIndex: candidate.sdpMLineIndex,
|
|
74
|
+
});
|
|
75
|
+
// One line per other computer: its peer, its channel once open, requests waiting for an answer, pieces being
|
|
76
|
+
// joined, and (while it is being introduced) the addresses found here and the asks waiting for one.
|
|
77
|
+
const lines = new Map();
|
|
78
|
+
// Computers this one dials (a worker dials home).
|
|
79
|
+
const dialers = new Set();
|
|
80
|
+
const dialing = new Map();
|
|
81
|
+
// Per computer this one dials: the next redial's timer and its pause.
|
|
82
|
+
const redials = new Map();
|
|
83
|
+
let stopped = false;
|
|
84
|
+
|
|
85
|
+
const lineOf = (peerId) => lines.get(peerId);
|
|
86
|
+
const isOpen = (line) =>
|
|
87
|
+
line?.channel?.readyState === "open" &&
|
|
88
|
+
line.peer.connectionState === "connected";
|
|
89
|
+
|
|
90
|
+
const wake = (line) => {
|
|
91
|
+
for (const resolve of line.waiting) resolve();
|
|
92
|
+
line.waiting.clear();
|
|
93
|
+
};
|
|
94
|
+
// Nothing else carries the computers' calls (L8.3), so a worker keeps its channel to home: one that ended or did not
|
|
95
|
+
// open is dialed again while `redial` says so.
|
|
96
|
+
const redialSoon = (peerId) => {
|
|
97
|
+
if (stopped || !dialers.has(peerId) || redials.get(peerId)?.timer) return;
|
|
98
|
+
if (!redial(peerId)) return stopRedials(peerId);
|
|
99
|
+
const pause = redials.get(peerId)?.pause ?? REDIAL_FIRST_MS;
|
|
100
|
+
const timer = setTimeout(() => {
|
|
101
|
+
redials.set(peerId, {
|
|
102
|
+
timer: null,
|
|
103
|
+
pause: Math.min(pause * 2, REDIAL_MOST_MS),
|
|
104
|
+
});
|
|
105
|
+
void dial(peerId);
|
|
106
|
+
}, pause);
|
|
107
|
+
timer.unref?.();
|
|
108
|
+
redials.set(peerId, { timer, pause });
|
|
109
|
+
};
|
|
110
|
+
const stopRedials = (peerId) => {
|
|
111
|
+
clearTimeout(redials.get(peerId)?.timer);
|
|
112
|
+
redials.delete(peerId);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const drop = (peerId, line) => {
|
|
116
|
+
if (!line || lines.get(peerId) !== line) return;
|
|
117
|
+
lines.delete(peerId);
|
|
118
|
+
if (line.served) redialSoon(peerId);
|
|
119
|
+
for (const { resolve, timer } of line.pending.values()) {
|
|
120
|
+
clearTimeout(timer);
|
|
121
|
+
resolve(null);
|
|
122
|
+
}
|
|
123
|
+
line.pending.clear();
|
|
124
|
+
wake(line);
|
|
125
|
+
try {
|
|
126
|
+
line.peer.close();
|
|
127
|
+
} catch {
|
|
128
|
+
// already closed
|
|
129
|
+
}
|
|
130
|
+
// Closing it here fires no change of its own: the channel's end is said as one (nerves.mjs say the link broke).
|
|
131
|
+
onChange(peerId);
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const newLine = (peerId) => {
|
|
135
|
+
const peer = new Peer({ iceServers: iceServers() });
|
|
136
|
+
const line = {
|
|
137
|
+
peer,
|
|
138
|
+
channel: null,
|
|
139
|
+
pending: new Map(),
|
|
140
|
+
pieces: new Map(),
|
|
141
|
+
found: [],
|
|
142
|
+
waiting: new Set(),
|
|
143
|
+
};
|
|
144
|
+
peer.onconnectionstatechange = () => {
|
|
145
|
+
const state = peer.connectionState;
|
|
146
|
+
if (state === "connected")
|
|
147
|
+
void wayOf(peer).then((way) =>
|
|
148
|
+
log(`${name}: ${peerId} connected${way ? ` over ${way}` : ""}`)
|
|
149
|
+
);
|
|
150
|
+
if (state !== "new" && state !== "connecting") wake(line);
|
|
151
|
+
if (state === "failed" || state === "closed") drop(peerId, line);
|
|
152
|
+
onChange(peerId);
|
|
153
|
+
};
|
|
154
|
+
return line;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const send = (line, id, message) => {
|
|
158
|
+
for (const piece of piecesOf(id, JSON.stringify(message)))
|
|
159
|
+
line.channel.send(piece);
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const serve = (peerId, line, channel) => {
|
|
163
|
+
line.channel = channel;
|
|
164
|
+
line.served = true;
|
|
165
|
+
channel.onclose = () => drop(peerId, line);
|
|
166
|
+
// The channel opening is a change too: on the side that answers, it opens after the connection says connected.
|
|
167
|
+
if (channel.readyState === "open") onChange(peerId);
|
|
168
|
+
else
|
|
169
|
+
channel.addEventListener?.("open", () => onChange(peerId), {
|
|
170
|
+
once: true,
|
|
171
|
+
});
|
|
172
|
+
channel.onmessage = ({ data }) => receive(data);
|
|
173
|
+
const receive = async (data) => {
|
|
174
|
+
let message;
|
|
175
|
+
try {
|
|
176
|
+
message = JSON.parse(typeof data === "string" ? data : "");
|
|
177
|
+
} catch {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (!message || typeof message !== "object") return;
|
|
181
|
+
const id = String(message.id ?? message.i ?? "");
|
|
182
|
+
if (typeof message.part === "number") {
|
|
183
|
+
const parts = line.pieces.get(id) ?? [];
|
|
184
|
+
parts[message.part] = String(message.data ?? "");
|
|
185
|
+
line.pieces.set(id, parts);
|
|
186
|
+
const size = parts.reduce((sum, part) => sum + (part?.length ?? 0), 0);
|
|
187
|
+
if (size > maxMessageChars) return line.pieces.delete(id);
|
|
188
|
+
if (parts.filter((part) => part !== undefined).length < message.of)
|
|
189
|
+
return;
|
|
190
|
+
line.pieces.delete(id);
|
|
191
|
+
return receive(parts.join(""));
|
|
192
|
+
}
|
|
193
|
+
if ("a" in message) {
|
|
194
|
+
const waiting = line.pending.get(id);
|
|
195
|
+
if (!waiting) return;
|
|
196
|
+
line.pending.delete(id);
|
|
197
|
+
clearTimeout(waiting.timer);
|
|
198
|
+
return waiting.resolve(message.a ?? null);
|
|
199
|
+
}
|
|
200
|
+
if (!("q" in message)) return;
|
|
201
|
+
let answer;
|
|
202
|
+
try {
|
|
203
|
+
answer = await onRequest(peerId, message.q);
|
|
204
|
+
} catch (error) {
|
|
205
|
+
answer = { ok: false, error: `the request failed: ${error.message}` };
|
|
206
|
+
}
|
|
207
|
+
if (channel.readyState === "open") send(line, id, { i: id, a: answer });
|
|
208
|
+
};
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
// ---- the worker's side: it dials home ----
|
|
212
|
+
|
|
213
|
+
const dial = (peerId) => {
|
|
214
|
+
if (!Peer || stopped || isOpen(lineOf(peerId))) return Promise.resolve();
|
|
215
|
+
const running = dialing.get(peerId);
|
|
216
|
+
if (running) return running;
|
|
217
|
+
const started = open(peerId).finally(() => {
|
|
218
|
+
dialing.delete(peerId);
|
|
219
|
+
if (isOpen(lineOf(peerId))) stopRedials(peerId);
|
|
220
|
+
else redialSoon(peerId);
|
|
221
|
+
});
|
|
222
|
+
dialing.set(peerId, started);
|
|
223
|
+
return started;
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
const open = async (peerId) => {
|
|
227
|
+
drop(peerId, lineOf(peerId));
|
|
228
|
+
const session = randomBytes(16).toString("base64url");
|
|
229
|
+
const line = newLine(peerId);
|
|
230
|
+
lines.set(peerId, line);
|
|
231
|
+
const channel = line.peer.createDataChannel(label, {
|
|
232
|
+
ordered: true,
|
|
233
|
+
});
|
|
234
|
+
const found = [];
|
|
235
|
+
let settled = false;
|
|
236
|
+
const opened = new Promise((resolve) => {
|
|
237
|
+
const timer = setTimeout(() => resolve(false), timeoutMs);
|
|
238
|
+
channel.onopen = () => {
|
|
239
|
+
clearTimeout(timer);
|
|
240
|
+
resolve(true);
|
|
241
|
+
};
|
|
242
|
+
channel.onclose = () => {
|
|
243
|
+
clearTimeout(timer);
|
|
244
|
+
resolve(false);
|
|
245
|
+
};
|
|
246
|
+
});
|
|
247
|
+
void opened.then(() => (settled = true));
|
|
248
|
+
let asking = 0;
|
|
249
|
+
const trade = async () => {
|
|
250
|
+
asking++;
|
|
251
|
+
try {
|
|
252
|
+
const reply = await introduce(peerId, {
|
|
253
|
+
command: commands.candidates,
|
|
254
|
+
session,
|
|
255
|
+
candidates: found.splice(0),
|
|
256
|
+
});
|
|
257
|
+
if (!reply?.ok) throw new Error(reply?.error ?? "refused");
|
|
258
|
+
for (const item of reply.candidates ?? [])
|
|
259
|
+
await line.peer.addIceCandidate(item).catch(() => {});
|
|
260
|
+
return reply.state ?? "new";
|
|
261
|
+
} finally {
|
|
262
|
+
asking--;
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
line.peer.onicecandidate = ({ candidate }) => {
|
|
266
|
+
// Once open, an address found later (a fallback's) has nothing left to do.
|
|
267
|
+
if (!candidate || settled) return;
|
|
268
|
+
const given = shared(candidate);
|
|
269
|
+
if (!given) return;
|
|
270
|
+
found.push(given);
|
|
271
|
+
if (asking > 0) void trade().catch(() => {});
|
|
272
|
+
};
|
|
273
|
+
try {
|
|
274
|
+
await line.peer.setLocalDescription(await line.peer.createOffer());
|
|
275
|
+
const answer = await introduce(peerId, {
|
|
276
|
+
command: commands.offer,
|
|
277
|
+
session,
|
|
278
|
+
sdp: shareSdp(line.peer.localDescription.sdp),
|
|
279
|
+
});
|
|
280
|
+
if (!answer?.ok || typeof answer.sdp !== "string")
|
|
281
|
+
throw new Error(answer?.error ?? "no answer");
|
|
282
|
+
await line.peer.setRemoteDescription({ type: "answer", sdp: answer.sdp });
|
|
283
|
+
for (const item of answer.candidates ?? [])
|
|
284
|
+
await line.peer.addIceCandidate(item).catch(() => {});
|
|
285
|
+
while (!settled) {
|
|
286
|
+
const state = await trade();
|
|
287
|
+
if (state !== "new" && state !== "connecting") break;
|
|
288
|
+
}
|
|
289
|
+
if (!(await opened)) throw new Error("the channel did not open");
|
|
290
|
+
serve(peerId, line, channel);
|
|
291
|
+
} catch (error) {
|
|
292
|
+
log(`${name}: ${peerId} not linked (${error.message})`);
|
|
293
|
+
drop(peerId, line);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
// ---- home's side: it answers the worker's introduction (ownMachines.mjs, from the computer that sealed it) ----
|
|
298
|
+
|
|
299
|
+
/** A worker's offer, opened from its sealed call: the answer to seal back, with the addresses found so far. */
|
|
300
|
+
const offer = async (peerId, input) => {
|
|
301
|
+
if (!Peer) return { ok: false, error: "this computer has no WebRTC" };
|
|
302
|
+
const session = String(input?.session || "");
|
|
303
|
+
if (!SESSION.test(session) || typeof input?.sdp !== "string")
|
|
304
|
+
return { ok: false, error: "invalid link offer" };
|
|
305
|
+
drop(peerId, lineOf(peerId));
|
|
306
|
+
const line = newLine(peerId);
|
|
307
|
+
line.session = session;
|
|
308
|
+
lines.set(peerId, line);
|
|
309
|
+
line.peer.onicecandidate = ({ candidate }) => {
|
|
310
|
+
if (!candidate) return;
|
|
311
|
+
const given = shared(candidate);
|
|
312
|
+
if (!given) return;
|
|
313
|
+
line.found.push(given);
|
|
314
|
+
wake(line);
|
|
315
|
+
};
|
|
316
|
+
line.peer.ondatachannel = ({ channel }) => {
|
|
317
|
+
if (channel.label !== label) return channel.close();
|
|
318
|
+
serve(peerId, line, channel);
|
|
319
|
+
};
|
|
320
|
+
try {
|
|
321
|
+
await line.peer.setRemoteDescription({ type: "offer", sdp: input.sdp });
|
|
322
|
+
await line.peer.setLocalDescription(await line.peer.createAnswer());
|
|
323
|
+
} catch (error) {
|
|
324
|
+
drop(peerId, line);
|
|
325
|
+
return { ok: false, error: `the link offer failed: ${error.message}` };
|
|
326
|
+
}
|
|
327
|
+
return {
|
|
328
|
+
ok: true,
|
|
329
|
+
sdp: shareSdp(line.peer.localDescription.sdp),
|
|
330
|
+
candidates: line.found.splice(0),
|
|
331
|
+
};
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
/** The worker's addresses found since its offer, and the ones found here since, once there is one or it settled. */
|
|
335
|
+
const candidates = async (peerId, input) => {
|
|
336
|
+
const line = lineOf(peerId);
|
|
337
|
+
if (!line || line.session !== String(input?.session || ""))
|
|
338
|
+
return { ok: false, error: "no link offer for that session" };
|
|
339
|
+
const given = Array.isArray(input?.candidates)
|
|
340
|
+
? input.candidates.slice(0, MAX_CANDIDATES)
|
|
341
|
+
: [];
|
|
342
|
+
for (const item of given)
|
|
343
|
+
await line.peer
|
|
344
|
+
.addIceCandidate({
|
|
345
|
+
candidate: String(item?.candidate ?? ""),
|
|
346
|
+
sdpMid: item?.sdpMid ?? null,
|
|
347
|
+
sdpMLineIndex: item?.sdpMLineIndex ?? null,
|
|
348
|
+
})
|
|
349
|
+
.catch(() => {});
|
|
350
|
+
const settled = () =>
|
|
351
|
+
line.found.length > 0 ||
|
|
352
|
+
!["new", "connecting"].includes(line.peer.connectionState);
|
|
353
|
+
if (!settled()) await new Promise((resolve) => line.waiting.add(resolve));
|
|
354
|
+
return {
|
|
355
|
+
ok: true,
|
|
356
|
+
candidates: line.found.splice(0),
|
|
357
|
+
state: line.peer.connectionState,
|
|
358
|
+
};
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
// ---- both sides ----
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Whether the channel to that computer is open, waiting up to `ms` for it to open: a worker dials home meanwhile,
|
|
365
|
+
* home waits for the worker to dial in. Everything between two computers but its introduction and the mailbox goes
|
|
366
|
+
* over this channel (docs/one-link-plan.md L8.3), so a request waits for it rather than going round through Harness.
|
|
367
|
+
*/
|
|
368
|
+
const opened = async (peerId, ms) => {
|
|
369
|
+
const end = Date.now() + ms;
|
|
370
|
+
const left = () => Math.max(0, end - Date.now());
|
|
371
|
+
// A computer that dials waits for its own dial: one refused or failed says the channel is not coming now.
|
|
372
|
+
if (dialers.has(peerId)) {
|
|
373
|
+
if (!isOpen(lineOf(peerId)) && ms > 0)
|
|
374
|
+
await Promise.race([dial(peerId), sleep(left())]);
|
|
375
|
+
return isOpen(lineOf(peerId));
|
|
376
|
+
}
|
|
377
|
+
while (!stopped && !isOpen(lineOf(peerId)) && left() > 0)
|
|
378
|
+
await sleep(Math.min(OPEN_CHECK_MS, left()));
|
|
379
|
+
return isOpen(lineOf(peerId));
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* One request over the channel to that computer: its answer, or null with no channel or no answer in time. A request
|
|
384
|
+
* may bring its own deadline (a door call's, deviceDoor.mjs).
|
|
385
|
+
*/
|
|
386
|
+
const request = (peerId, body, { timeoutMs: wait = timeoutMs } = {}) => {
|
|
387
|
+
const line = lineOf(peerId);
|
|
388
|
+
if (!isOpen(line)) {
|
|
389
|
+
if (dialers.has(peerId)) void dial(peerId);
|
|
390
|
+
return Promise.resolve(null);
|
|
391
|
+
}
|
|
392
|
+
return new Promise((resolve) => {
|
|
393
|
+
const id = randomBytes(12).toString("hex");
|
|
394
|
+
const timer = setTimeout(() => {
|
|
395
|
+
line.pending.delete(id);
|
|
396
|
+
resolve(null);
|
|
397
|
+
}, wait);
|
|
398
|
+
line.pending.set(id, { resolve, timer });
|
|
399
|
+
try {
|
|
400
|
+
send(line, id, { i: id, q: body });
|
|
401
|
+
} catch {
|
|
402
|
+
line.pending.delete(id);
|
|
403
|
+
clearTimeout(timer);
|
|
404
|
+
resolve(null);
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
return {
|
|
410
|
+
// No key to swap: the introduction is sealed under the pair key (kept so callers that asked for one still work).
|
|
411
|
+
key: async () => null,
|
|
412
|
+
/** A computer this one may link with; a worker dials it (`dialer`). */
|
|
413
|
+
allow: (peerId, _key, { dialer = false } = {}) => {
|
|
414
|
+
if (dialer) dialers.add(peerId);
|
|
415
|
+
},
|
|
416
|
+
/** A computer no longer the person's: its channel closes and it is not dialed again. */
|
|
417
|
+
disallow: (peerId) => {
|
|
418
|
+
dialers.delete(peerId);
|
|
419
|
+
stopRedials(peerId);
|
|
420
|
+
drop(peerId, lineOf(peerId));
|
|
421
|
+
},
|
|
422
|
+
dial: (peerId) => {
|
|
423
|
+
dialers.add(peerId);
|
|
424
|
+
return dial(peerId);
|
|
425
|
+
},
|
|
426
|
+
offer,
|
|
427
|
+
candidates,
|
|
428
|
+
opened,
|
|
429
|
+
request,
|
|
430
|
+
status: () =>
|
|
431
|
+
[...new Set([...lines.keys(), ...dialers])].map((id) => ({
|
|
432
|
+
id,
|
|
433
|
+
linked: isOpen(lineOf(id)),
|
|
434
|
+
})),
|
|
435
|
+
/** Every channel closed and the ones this computer dials dialed again. */
|
|
436
|
+
restart: () => {
|
|
437
|
+
for (const [peerId, line] of [...lines]) drop(peerId, line);
|
|
438
|
+
for (const peerId of dialers) void dial(peerId);
|
|
439
|
+
},
|
|
440
|
+
stop: () => {
|
|
441
|
+
stopped = true;
|
|
442
|
+
for (const peerId of [...redials.keys()]) stopRedials(peerId);
|
|
443
|
+
for (const [peerId, line] of [...lines]) drop(peerId, line);
|
|
444
|
+
},
|
|
445
|
+
};
|
|
446
|
+
}
|
package/lib/machineRoutes.mjs
CHANGED
|
@@ -19,6 +19,12 @@ const LOCAL_OWNERS = new Set([
|
|
|
19
19
|
"repos",
|
|
20
20
|
"nativeRequests",
|
|
21
21
|
"waits",
|
|
22
|
+
// A process and what should be true are this computer's own (completion.mjs).
|
|
23
|
+
"completion",
|
|
24
|
+
// A watch is armed on the computer that reads it (threadWatches.mjs); home keeps it.
|
|
25
|
+
"watching",
|
|
26
|
+
// An outside address and its events are the computer's that made it (outsideEvents.mjs).
|
|
27
|
+
"outside",
|
|
22
28
|
]);
|
|
23
29
|
|
|
24
30
|
/**
|
|
@@ -35,6 +41,17 @@ export const fromCloud = (store, request) =>
|
|
|
35
41
|
request?.[FROM_MACHINE] !== undefined &&
|
|
36
42
|
Boolean(store.ownMachine(request[FROM_MACHINE])?.hosted);
|
|
37
43
|
|
|
44
|
+
/**
|
|
45
|
+
* A connected-app call as the caller holds its grants. A cloud computer Harness runs for the person is not one of their
|
|
46
|
+
* own computers, so it uses only grants made for hosted agents, as a call through Harness's server does; and a run uses
|
|
47
|
+
* a grant made for one task only when that task is its own (`run`), whatever task the call names.
|
|
48
|
+
*/
|
|
49
|
+
export const connectorCaller = (store, request, run) => ({
|
|
50
|
+
...request,
|
|
51
|
+
...(fromCloud(store, request) ? { hostedCaller: true } : {}),
|
|
52
|
+
...(run ? { task: run.id } : {}),
|
|
53
|
+
});
|
|
54
|
+
|
|
38
55
|
/** Whether a worker sends this board command to home. */
|
|
39
56
|
export const goesHome = (command) =>
|
|
40
57
|
String(command).startsWith("board.") &&
|
|
@@ -67,6 +84,9 @@ const FROM_COPY = new Set([
|
|
|
67
84
|
"board.room.read",
|
|
68
85
|
"board.goal.get",
|
|
69
86
|
"board.goal.list",
|
|
87
|
+
// Facts are the same on every computer (facts.mjs): a worker's copy holds home's.
|
|
88
|
+
"board.fact.get",
|
|
89
|
+
"board.watch.thread",
|
|
70
90
|
]);
|
|
71
91
|
export const readsFromCopy = (command) => FROM_COPY.has(String(command));
|
|
72
92
|
|
|
@@ -156,6 +176,11 @@ export function withComputer(post, computerOf) {
|
|
|
156
176
|
// A worker's own upkeep with home, whoever it names: telling home its folders, taking and reporting the runs home keeps
|
|
157
177
|
// for its agents, the environment grants home's vault lends a run there (vault.mjs checks those itself).
|
|
158
178
|
const UPKEEP = new Set([
|
|
179
|
+
// A watch this computer reads tripped, and its fact or wake is home's (watchResponses.mjs): home acts on the watch as
|
|
180
|
+
// it keeps it, and only for the computer that watch names.
|
|
181
|
+
"board.watch.answer",
|
|
182
|
+
// A wait it reads answered (threadWatches.mjs): home ends it, only for the computer that wait names.
|
|
183
|
+
"board.watch.end",
|
|
159
184
|
"board.machine.info",
|
|
160
185
|
"board.machine.runnable",
|
|
161
186
|
"board.machine.take",
|
|
@@ -167,6 +192,10 @@ const UPKEEP = new Set([
|
|
|
167
192
|
|
|
168
193
|
// What an agent on another computer does on home's board: what its Harness tools call there (mcp-stdio.mjs).
|
|
169
194
|
const AGENT_WORK = new Set([
|
|
195
|
+
// The step an agent's own session ended its turn with, for its thread (nextStep.mjs).
|
|
196
|
+
"board.next.keep",
|
|
197
|
+
// Harness's readings a run there left untaken, for the agent's next wake (daemon.mjs sensesLeft).
|
|
198
|
+
"board.sense.left",
|
|
170
199
|
"board.list",
|
|
171
200
|
"board.get",
|
|
172
201
|
"board.children",
|
|
@@ -184,8 +213,6 @@ const AGENT_WORK = new Set([
|
|
|
184
213
|
"board.standing.check",
|
|
185
214
|
"board.file.add",
|
|
186
215
|
"board.file.get",
|
|
187
|
-
"board.thread.read",
|
|
188
|
-
"board.thread.post",
|
|
189
216
|
"board.goal.list",
|
|
190
217
|
"board.goal.get",
|
|
191
218
|
"board.goal.finish",
|
|
@@ -215,8 +242,10 @@ const AGENT_WORK = new Set([
|
|
|
215
242
|
"board.template.get",
|
|
216
243
|
"board.template.use",
|
|
217
244
|
"board.kanban.update",
|
|
218
|
-
"board.
|
|
219
|
-
"board.
|
|
245
|
+
"board.jam.plan",
|
|
246
|
+
"board.jam.propose",
|
|
247
|
+
"board.jam.piece",
|
|
248
|
+
"board.jam.handoff",
|
|
220
249
|
"board.desk.book",
|
|
221
250
|
"board.connector.list",
|
|
222
251
|
"board.connector.tools",
|
|
@@ -225,26 +254,32 @@ const AGENT_WORK = new Set([
|
|
|
225
254
|
// A secret used through home, never handed over (vault.mjs keeps its own rules for another computer's agents).
|
|
226
255
|
"board.vault.list",
|
|
227
256
|
"board.vault.use",
|
|
257
|
+
"board.vault.pass",
|
|
258
|
+
// Facts every computer reads the same (facts.mjs): what an agent there says about its own computer or the swarm.
|
|
259
|
+
"board.fact.get",
|
|
260
|
+
"board.fact.set",
|
|
261
|
+
"board.watch.keep",
|
|
262
|
+
"board.watch.drop",
|
|
263
|
+
"board.watch.thread",
|
|
228
264
|
]);
|
|
229
265
|
|
|
230
266
|
// The person at a computer they drive from does the board's work there as on home: posts, runs, answers, stops,
|
|
231
267
|
// threads. How the swarm is set up (standing permission, spend ceilings, connectors' grants, the vault, the relay
|
|
232
|
-
// network, the desk's book,
|
|
268
|
+
// network, the desk's book, the people in jams, offices) is changed only on home itself or a phone paired with it.
|
|
233
269
|
const PERSON_WORK = new Set([
|
|
234
270
|
...AGENT_WORK,
|
|
271
|
+
"board.watch.go",
|
|
235
272
|
"board.run",
|
|
236
273
|
"board.remove",
|
|
237
274
|
"board.standing",
|
|
238
275
|
"board.file.share",
|
|
239
276
|
"board.thread.send",
|
|
240
277
|
"board.thread.discard",
|
|
241
|
-
"board.thread.auto",
|
|
242
278
|
"board.thread.end",
|
|
243
279
|
"board.goal.create",
|
|
244
280
|
"board.goal.stop",
|
|
245
281
|
"board.goal.resume",
|
|
246
282
|
"board.goal.folder",
|
|
247
|
-
"board.goal.invite",
|
|
248
283
|
"board.swarm.file",
|
|
249
284
|
"board.swarm.post",
|
|
250
285
|
"board.swarm.stop_all",
|
|
@@ -257,7 +292,6 @@ const PERSON_WORK = new Set([
|
|
|
257
292
|
"board.swarm.discard",
|
|
258
293
|
"board.swarm.start_run",
|
|
259
294
|
"board.swarm.steer",
|
|
260
|
-
"board.swarm.use_browser",
|
|
261
295
|
"board.swarm.places",
|
|
262
296
|
"board.swarm.browse",
|
|
263
297
|
"board.swarm.place",
|
|
@@ -270,6 +304,11 @@ const PERSON_WORK = new Set([
|
|
|
270
304
|
"board.swarm.lessons.edit",
|
|
271
305
|
"board.swarm.lessons.remove",
|
|
272
306
|
"board.swarm.lessons.undo",
|
|
307
|
+
"board.swarm.memory.undo",
|
|
308
|
+
"board.swarm.memory.correct",
|
|
309
|
+
"board.swarm.proposals",
|
|
310
|
+
"board.swarm.proposals.start",
|
|
311
|
+
"board.swarm.proposals.dismiss",
|
|
273
312
|
"board.swarm.specialties",
|
|
274
313
|
"board.swarm.offices",
|
|
275
314
|
"board.swarm.notice",
|
|
@@ -283,21 +322,31 @@ const PERSON_WORK = new Set([
|
|
|
283
322
|
"board.template.check",
|
|
284
323
|
"board.template.save",
|
|
285
324
|
"board.template.remove",
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
"board.
|
|
289
|
-
"board.
|
|
290
|
-
"board.
|
|
291
|
-
"board.
|
|
292
|
-
"board.
|
|
293
|
-
"board.
|
|
294
|
-
"board.
|
|
295
|
-
"board.
|
|
325
|
+
// The person's jams, from any computer they drive: home holds them (docs/jam-plan.md J3), and the folders a jam is
|
|
326
|
+
// started on or joined into are home's, as board.jam.repos lists them.
|
|
327
|
+
"board.jam.create",
|
|
328
|
+
"board.jam.join",
|
|
329
|
+
"board.jam.decline",
|
|
330
|
+
"board.jam.invite",
|
|
331
|
+
"board.jam.remove",
|
|
332
|
+
"board.jam.setting",
|
|
333
|
+
"board.jam.decide",
|
|
334
|
+
"board.jam.withdraw",
|
|
335
|
+
"board.jam.release",
|
|
336
|
+
"board.jam.events",
|
|
337
|
+
"board.jam.leave",
|
|
338
|
+
"board.jam.list",
|
|
339
|
+
"board.jam.read",
|
|
340
|
+
"board.jam.answer",
|
|
341
|
+
"board.jam.note",
|
|
342
|
+
"board.jam.work",
|
|
343
|
+
"board.jam.repos",
|
|
344
|
+
"board.jam.agents",
|
|
345
|
+
"board.jam.goal",
|
|
296
346
|
"board.landed.thread",
|
|
297
347
|
"board.landed.folder",
|
|
298
348
|
"board.environments.list",
|
|
299
349
|
"board.relay.list",
|
|
300
|
-
"board.relay.board",
|
|
301
350
|
"board.relay.seen",
|
|
302
351
|
"board.connector.operations",
|
|
303
352
|
"board.place.op",
|