@parcha/agentrun-serve 0.1.0-beta.56 → 0.1.0-beta.58
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/CHANGELOG.md +281 -0
- package/README.md +246 -66
- package/dist/acp-package.d.ts +2 -0
- package/dist/adapters.d.ts +15 -3
- package/dist/adapters.js +17 -6
- package/dist/agent-package.d.ts +3 -3
- package/dist/agent-package.js +17 -8
- package/dist/api-keys.d.ts +76 -0
- package/dist/api-keys.js +217 -0
- package/dist/api-only.d.ts +15 -5
- package/dist/api-only.js +32 -8
- package/dist/box-paths.d.ts +4 -3
- package/dist/box-paths.js +7 -4
- package/dist/browser-hold.d.ts +23 -0
- package/dist/browser-hold.js +48 -0
- package/dist/browser-person.d.ts +3 -0
- package/dist/browser-person.js +4 -1
- package/dist/chatgpt-plan.d.ts +18 -8
- package/dist/chatgpt-plan.js +91 -34
- package/dist/claude-login-token.d.ts +117 -0
- package/dist/claude-login-token.js +737 -0
- package/dist/claude-login.d.ts +120 -22
- package/dist/claude-login.js +347 -63
- package/dist/claude-sign-in.d.ts +17 -6
- package/dist/claude-sign-in.js +42 -39
- package/dist/cli.js +33 -10
- package/dist/composio-made.d.ts +4 -0
- package/dist/composio-made.js +4 -0
- package/dist/composio-preset.d.ts +43 -2
- package/dist/composio-preset.js +92 -4
- package/dist/computer-attach.d.ts +35 -17
- package/dist/computer-attach.js +301 -49
- package/dist/computer-held-routes.d.ts +5 -4
- package/dist/computer-held-routes.js +29 -8
- package/dist/computer-held.d.ts +6 -1
- package/dist/computer-held.js +14 -0
- package/dist/computer-record.js +4 -2
- package/dist/computer-viewer.js +4 -1
- package/dist/computers.d.ts +10 -10
- package/dist/computers.js +5 -5
- package/dist/console/assets/{backend-Boh7tC4i.js → backend-CSAWUqtZ.js} +19 -19
- package/dist/console/assets/index-Bwn1oTem.css +1 -0
- package/dist/console/assets/index-DPh29cyz.js +747 -0
- package/dist/console/index.html +2 -2
- package/dist/decisions.d.ts +1 -1
- package/dist/decisions.js +1 -1
- package/dist/dial-relay.d.ts +25 -4
- package/dist/dial-relay.js +129 -1
- package/dist/docker-host-addresses.d.ts +22 -11
- package/dist/docker-host-addresses.js +38 -26
- package/dist/env-switch.d.ts +8 -0
- package/dist/env-switch.js +31 -2
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +2 -0
- package/dist/fal-price.d.ts +56 -20
- package/dist/fal-price.js +400 -90
- package/dist/handed-values.d.ts +43 -1
- package/dist/handed-values.js +119 -2
- package/dist/harness-archive.d.ts +13 -10
- package/dist/harness-archive.js +60 -34
- package/dist/harness-check.d.ts +3 -1
- package/dist/harness-check.js +25 -46
- package/dist/harness-keys.d.ts +73 -27
- package/dist/harness-keys.js +194 -61
- package/dist/harness-recipe-entry.d.ts +8 -7
- package/dist/harness-recipe-entry.js +21 -32
- package/dist/harness-recipe.d.ts +38 -25
- package/dist/harness-recipe.js +49 -36
- package/dist/harness-sign-in.js +12 -23
- package/dist/harness-table.d.ts +10 -3
- package/dist/harness-table.js +26 -21
- package/dist/harness.d.ts +1 -0
- package/dist/harness.js +17 -5
- package/dist/hosted-search.d.ts +1 -1
- package/dist/hosted-search.js +1 -2
- package/dist/import-worker.d.ts +4 -2
- package/dist/import-worker.js +9 -2
- package/dist/in-flight.d.ts +26 -0
- package/dist/in-flight.js +52 -0
- package/dist/key-refusal.d.ts +9 -4
- package/dist/key-refusal.js +107 -25
- package/dist/learn/ledger.d.ts +170 -0
- package/dist/learn/ledger.js +218 -0
- package/dist/learn/promote.d.ts +3 -3
- package/dist/learn/promote.js +1 -1
- package/dist/learn/secrets.d.ts +13 -3
- package/dist/learn/secrets.js +118 -17
- package/dist/learning-cost.d.ts +28 -23
- package/dist/learning-cost.js +19 -23
- package/dist/link-check.js +1 -1
- package/dist/live-reach.d.ts +2 -0
- package/dist/live-reach.js +2 -0
- package/dist/llms.txt +1 -1
- package/dist/marketplace.d.ts +77 -0
- package/dist/marketplace.js +224 -0
- package/dist/mcp-api-only-origins.d.ts +35 -0
- package/dist/mcp-api-only-origins.js +57 -0
- package/dist/mcp-gateway.d.ts +7 -5
- package/dist/mcp-gateway.js +9 -12
- package/dist/mcp-secret-writes.js +5 -3
- package/dist/mcp-servers-store.d.ts +28 -6
- package/dist/mcp-servers-store.js +53 -4
- package/dist/media-duration.d.ts +3 -0
- package/dist/media-duration.js +90 -9
- package/dist/micro.d.ts +2 -0
- package/dist/micro.js +2 -0
- package/dist/model-form.d.ts +5 -4
- package/dist/model-form.js +11 -7
- package/dist/model.d.ts +10 -6
- package/dist/model.js +16 -11
- package/dist/monid.d.ts +14 -1
- package/dist/monid.js +103 -26
- package/dist/ollama-provider.d.ts +7 -1
- package/dist/ollama-provider.js +14 -2
- package/dist/openapi.js +210 -37
- package/dist/otel/genai.d.ts +65 -0
- package/dist/otel/genai.js +116 -0
- package/dist/otel/tracer.d.ts +55 -0
- package/dist/otel/tracer.js +308 -0
- package/dist/page.d.ts +2 -0
- package/dist/page.js +3 -1
- package/dist/paid-calls.d.ts +1 -32
- package/dist/paid-calls.js +4 -39
- package/dist/place-offers.d.ts +4 -0
- package/dist/place-offers.js +7 -4
- package/dist/provider.d.ts +11 -2
- package/dist/provider.js +59 -8
- package/dist/redact/match.d.ts +30 -3
- package/dist/redact/match.js +422 -41
- package/dist/refusal-words.d.ts +11 -0
- package/dist/refusal-words.js +11 -0
- package/dist/run-census.d.ts +67 -0
- package/dist/run-census.js +240 -0
- package/dist/run-computer.d.ts +6 -3
- package/dist/run-computer.js +4 -1
- package/dist/run-cut.d.ts +15 -6
- package/dist/run-cut.js +30 -9
- package/dist/run-files.d.ts +3 -0
- package/dist/run-files.js +3 -0
- package/dist/run-launch.d.ts +12 -1
- package/dist/run-launch.js +9 -10
- package/dist/run-learning-served.d.ts +4 -2
- package/dist/run-model-key.d.ts +8 -0
- package/dist/run-model-key.js +31 -1
- package/dist/run-reads-over-store.d.ts +24 -6
- package/dist/run-reads-over-store.js +67 -23
- package/dist/run-recordings.d.ts +5 -1
- package/dist/run-recordings.js +39 -14
- package/dist/run-timeline-served.d.ts +2 -0
- package/dist/run-timeline-served.js +2 -2
- package/dist/run-usage.d.ts +1 -0
- package/dist/run-view.d.ts +1 -2
- package/dist/run-view.js +2 -1
- package/dist/runnable.d.ts +10 -1
- package/dist/runnable.js +20 -8
- package/dist/runner/adapter-loop.d.ts +11 -0
- package/dist/runner/adapter-loop.js +58 -26
- package/dist/runner/adapter-run-spec.d.ts +55 -11
- package/dist/runner/adapter-run-spec.js +183 -52
- package/dist/runner/adapters/pi.js +25 -3
- package/dist/runner/beside.d.ts +9 -2
- package/dist/runner/beside.js +27 -9
- package/dist/runner/big-writes.js +1 -10
- package/dist/runner/claim-secrets.d.ts +27 -10
- package/dist/runner/claim-secrets.js +49 -22
- package/dist/runner/cli-run-spec.d.ts +6 -0
- package/dist/runner/cli-run-spec.js +97 -38
- package/dist/runner/computer-journal.d.ts +9 -1
- package/dist/runner/computer-journal.js +26 -0
- package/dist/runner/conformance/harness.js +1 -1
- package/dist/runner/feedback-round.js +3 -0
- package/dist/runner/harness-loops.d.ts +6 -5
- package/dist/runner/harness-loops.js +7 -7
- package/dist/runner/held.d.ts +24 -6
- package/dist/runner/journal-kinds.d.ts +5 -0
- package/dist/runner/journal-kinds.js +5 -0
- package/dist/runner/main.d.ts +11 -6
- package/dist/runner/main.js +19 -9
- package/dist/runner/mcp-claim.js +31 -3
- package/dist/runner/mcp-run.d.ts +9 -0
- package/dist/runner/mcp-run.js +11 -5
- package/dist/runner/model-route.d.ts +17 -0
- package/dist/runner/model-route.js +120 -2
- package/dist/runner/presence.d.ts +11 -1
- package/dist/runner/presence.js +7 -0
- package/dist/runner/reflect-hook.js +1 -1
- package/dist/runner/run-child-host.d.ts +22 -8
- package/dist/runner/run-child-host.js +128 -41
- package/dist/runner/run-child.js +5 -2
- package/dist/runner/runner.d.ts +17 -3
- package/dist/runner/runner.js +130 -34
- package/dist/runner/save-slots.js +2 -1
- package/dist/runner/store-port.d.ts +1 -1
- package/dist/runner/store-secrets.d.ts +6 -0
- package/dist/runner/store-secrets.js +49 -7
- package/dist/runner/subagent.d.ts +11 -0
- package/dist/runner/subagent.js +16 -38
- package/dist/runner/workflow-first.d.ts +35 -15
- package/dist/runner/workflow-first.js +38 -25
- package/dist/runner/workflow-spec.js +4 -3
- package/dist/runs-over-store.d.ts +15 -6
- package/dist/runs-over-store.js +33 -21
- package/dist/scrub-command.d.ts +73 -0
- package/dist/scrub-command.js +307 -0
- package/dist/secret-cut.d.ts +73 -0
- package/dist/secret-cut.js +291 -4
- package/dist/secret-rules.d.ts +143 -0
- package/dist/secret-rules.js +2564 -0
- package/dist/secrets.d.ts +7 -0
- package/dist/secrets.js +20 -0
- package/dist/server-keys.d.ts +20 -0
- package/dist/server-keys.js +25 -6
- package/dist/server.d.ts +65 -13
- package/dist/server.js +470 -157
- package/dist/skills/agentrun-serve/SKILL.md +4 -0
- package/dist/slots.d.ts +9 -0
- package/dist/slots.js +45 -0
- package/dist/store/index.d.ts +1 -1
- package/dist/store/index.js +1 -1
- package/dist/store/journal/storage.js +1 -1
- package/dist/store/postgres/migrations/0027_run_launch_census.sql +31 -0
- package/dist/store/postgres/migrations/0028_learning_ledger.sql +44 -0
- package/dist/store/schema.d.ts +23 -0
- package/dist/store/schema.js +58 -2
- package/dist/store/seal.d.ts +6 -1
- package/dist/store/seal.js +5 -3
- package/dist/store/sqlite/run-launch.d.ts +9 -1
- package/dist/store/sqlite/run-launch.js +24 -13
- package/dist/store/sqlite/run-patch.d.ts +12 -2
- package/dist/store/sqlite/run-patch.js +42 -5
- package/dist/store/sqlite/store.js +105 -12
- package/dist/store/store.d.ts +57 -2
- package/dist/store/store.js +20 -0
- package/dist/store-reads/learning.d.ts +84 -124
- package/dist/store-reads/learning.js +210 -274
- package/dist/store-view.d.ts +3 -0
- package/dist/store-view.js +3 -3
- package/dist/suggestions.js +3 -2
- package/dist/template-eval.d.ts +39 -9
- package/dist/template-eval.js +191 -60
- package/dist/template-import.js +6 -2
- package/dist/templates.d.ts +10 -2
- package/dist/templates.js +28 -8
- package/dist/timeline/labels.d.ts +6 -4
- package/dist/timeline/labels.js +18 -6
- package/dist/timeline/timeline.d.ts +5 -0
- package/dist/timeline/timeline.js +14 -8
- package/dist/timeline/util.js +1 -1
- package/dist/timeline/web-calls.d.ts +22 -7
- package/dist/timeline/web-calls.js +106 -35
- package/dist/tool-gate.d.ts +42 -3
- package/dist/tool-gate.js +66 -5
- package/dist/tool-keys-store.d.ts +2 -4
- package/dist/tool-keys-store.js +5 -21
- package/dist/tool-keys.d.ts +2 -2
- package/dist/tool-keys.js +2 -2
- package/dist/tool-list.js +1 -1
- package/dist/tool-plane.d.ts +2 -2
- package/dist/tool-plane.js +35 -33
- package/dist/tools.js +13 -7
- package/dist/trigger-targets.d.ts +4 -0
- package/dist/trigger-targets.js +9 -2
- package/dist/triggers.d.ts +67 -4
- package/dist/triggers.js +171 -22
- package/dist/until.d.ts +3 -0
- package/dist/until.js +11 -0
- package/dist/web-fake.d.ts +1 -3
- package/dist/web-fake.js +7 -18
- package/dist/web-fetch.d.ts +6 -72
- package/dist/web-fetch.js +11 -265
- package/dist/web-run.d.ts +31 -0
- package/dist/web-run.js +90 -0
- package/dist/web-save.d.ts +10 -13
- package/dist/web-save.js +13 -55
- package/dist/web-search.d.ts +9 -127
- package/dist/web-search.js +23 -259
- package/dist/wires.d.ts +13 -0
- package/dist/wires.js +11 -0
- package/dist/workflow-run.d.ts +5 -4
- package/dist/workflow-run.js +5 -4
- package/dist/workspace/attached-computers.d.ts +32 -9
- package/dist/workspace/attached-computers.js +77 -13
- package/dist/workspace/computer-deps.d.ts +8 -2
- package/dist/workspace/computer-deps.js +10 -3
- package/dist/workspace/computer-for-claim.d.ts +12 -1
- package/dist/workspace/computer-for-claim.js +83 -11
- package/dist/workspace/computer-kept.d.ts +3 -0
- package/dist/workspace/computer-kept.js +11 -2
- package/dist/workspace/index.d.ts +1 -1
- package/dist/workspace/index.js +1 -1
- package/dist/workspace/kind-skills.d.ts +4 -0
- package/dist/workspace/kind-skills.js +9 -0
- package/dist/workspace/paired-route.d.ts +91 -0
- package/dist/workspace/paired-route.js +153 -0
- package/dist/workspace/synced-folder.js +3 -4
- package/dist/wrapped.d.ts +20 -6
- package/dist/wrapped.js +66 -25
- package/package.json +17 -11
- package/skills/agentrun-serve/SKILL.md +4 -0
- package/templates/README.md +32 -10
- package/dist/console/assets/index-CQ3cJRzI.js +0 -746
- package/dist/console/assets/index-DKkaS3a0.css +0 -1
- package/dist/exa-keyless.d.ts +0 -34
- package/dist/exa-keyless.js +0 -118
- package/dist/firecrawl-keyless.d.ts +0 -41
- package/dist/firecrawl-keyless.js +0 -152
- package/dist/keyless-mcp.d.ts +0 -36
- package/dist/keyless-mcp.js +0 -127
- package/dist/text-urls.d.ts +0 -38
- package/dist/text-urls.js +0 -170
- package/dist/web-fetch-direct.d.ts +0 -119
- package/dist/web-fetch-direct.js +0 -190
package/dist/computer-attach.js
CHANGED
|
@@ -14,24 +14,36 @@
|
|
|
14
14
|
// the machine's name, and it fetches this server's computerd build with the same token (GET /v2/computers/daemon,
|
|
15
15
|
// DAEMON_FETCHES_PER_MINUTE a minute per machine).
|
|
16
16
|
//
|
|
17
|
-
// One process: the hub is this process's memory, so a run takes a paired computer
|
|
18
|
-
//
|
|
19
|
-
//
|
|
17
|
+
// One process: the hub is this process's memory, so a run takes a paired computer from a runner in the process that
|
|
18
|
+
// holds its socket (the CLI's start, with its runner beside the server), or from a runner in a process of its own that
|
|
19
|
+
// relays (below). A runner of its own process with no relay refuses one in words (workspace/attached-computers.ts
|
|
20
|
+
// ONE_PROCESS_WORDS); a second server on the same store is refused at its start.
|
|
20
21
|
//
|
|
21
22
|
// A hosted machine a runner in a process of its own made (dial-relay.ts) dials this server the same way, and its token's
|
|
22
23
|
// row names that runner: the server relays its socket to the runner's attach listener (`relay`), where these same routes
|
|
23
24
|
// (`owner`, the runner's) take it into the runner's hub. The relay is two sockets, each with this module's limits (a frame
|
|
24
25
|
// over MAX_LINE_BYTES, a binary frame, ATTACH_IDLE_MS of silence), joined frame by frame: a close of either closes the
|
|
25
26
|
// other with its code.
|
|
27
|
+
//
|
|
28
|
+
// A paired computer goes the same way while a run holds it (workspace/paired-route.ts): its route, a server setting the
|
|
29
|
+
// run's claim takes, names the runner whose hub holds the box for the run. The server's upgrade reads the route: none
|
|
30
|
+
// keeps the socket in this hub; a live one relays it to that runner's listener, whose routes take it only while the route
|
|
31
|
+
// still names that runner and incarnation; a stale one (its runner gone or restarted) is 503 in words, the route dropped
|
|
32
|
+
// and its words kept on the row. The server's tick (every PAIRED_ROUTE_MS) compares each route with where the box's
|
|
33
|
+
// socket is, and closes a socket that is in the wrong place with 1001: the box's own redial lands where its route says.
|
|
26
34
|
import { createHash, randomBytes, randomInt } from "node:crypto";
|
|
27
35
|
import { ATTACH_CLOSE, ATTACH_HEARTBEAT_MS, ATTACH_IDLE_MS, ATTACH_PATH, ATTACH_SUBPROTOCOL, hostedDaemonPath, MAX_LINE_BYTES, mintedComputer, newMachineToken, PAIRING_CODE_PATTERN, PAIRING_CODE_TTL_MS, PROTOCOL_VERSION, RUN_LABEL } from "@parcha/agentrun-computer";
|
|
28
36
|
import { WebSocket, WebSocketServer } from "ws";
|
|
29
37
|
import { ApiError, invalid } from "./errors.js";
|
|
30
|
-
import { dialHost, LIVE_PROOF_HEADER, relayProof } from "./live-reach.js";
|
|
38
|
+
import { dialHost, LIVE_PROOF_HEADER, RELAY_CONNECT_MS, relayProof } from "./live-reach.js";
|
|
31
39
|
import { peerKey } from "./owner.js";
|
|
32
40
|
import { registerNotSecretWrite } from "./secret-writes.js";
|
|
33
|
-
import { PRESENCE_STALE_MS } from "./runner/presence.js";
|
|
41
|
+
import { liveRelays, PRESENCE_STALE_MS } from "./runner/presence.js";
|
|
34
42
|
import { pairedOf, PAIRED_ID_PREFIX, RUNNER_APART_WORDS, servesNoKind } from "./workspace/attached-computers.js";
|
|
43
|
+
import { allRoutes, EMPTY_ROUTE, notAttachedWords, PAIRED_BRING_MS, replaceRoute, ROUTE_COMES_BACK, routeKeyOf, routeRestartedWords, seeRoute } from "./workspace/paired-route.js";
|
|
44
|
+
// Where they were first defined, for the readers that import them from here.
|
|
45
|
+
export { liveRelays } from "./runner/presence.js";
|
|
46
|
+
export { RELAY_CONNECT_MS } from "./live-reach.js";
|
|
35
47
|
// Routes under /computers, where a provider's keys are a secret write (computers.ts): these three write none of the
|
|
36
48
|
// owner's secrets, so the API token may call them as the console's session does.
|
|
37
49
|
registerNotSecretWrite("createPairing", "A pairing code is shown once and kept as its hash: it is a one-time code, not a secret of the owner's.");
|
|
@@ -180,12 +192,17 @@ function featuresOf(features) {
|
|
|
180
192
|
return {};
|
|
181
193
|
return Object.fromEntries(Object.entries(features).flatMap(([name, value]) => (value === true ? [[name, true]] : isObject(value) && typeof value.off === "string" ? [[name, { off: value.off }]] : [])));
|
|
182
194
|
}
|
|
183
|
-
|
|
195
|
+
/** `row` as GET /v2/computers lists it. `runsRefused`: why no run here can use it (RUNNER_APART_WORDS), or null. `route`:
|
|
196
|
+
* its route (workspace/paired-route.ts), which names the run that holds it and the runner whose hub does while a run's
|
|
197
|
+
* claim routed it there; `relayed`: its socket is relayed to that runner now (createAttachRoutes relayedPaired). */
|
|
198
|
+
export function pairedView(row, hub, serves, runsRefused = null, route = EMPTY_ROUTE, relayed = false) {
|
|
184
199
|
const paired = row.hello;
|
|
185
200
|
// Kind and os stay what it paired as (another is refused at hello); the rest follows its driver's live hello.
|
|
186
201
|
const hello = hub.hello(row.id) ?? paired;
|
|
187
|
-
|
|
188
|
-
const
|
|
202
|
+
// A run's hold on a runner's hub is the route's; on this server's hub, the hub's own.
|
|
203
|
+
const routed = route.runner !== null;
|
|
204
|
+
const holder = routed ? route.run : (hub.claimed(row.id)?.[RUN_LABEL] ?? null);
|
|
205
|
+
const until = routed ? (route.until === null ? undefined : new Date(route.until)) : holder === null ? undefined : hub.deadline(row.id);
|
|
189
206
|
// The hub has the socket live before the store keeps its hello (attach's accept, then seen), so a list between the
|
|
190
207
|
// two reads the hub's time: an online computer is never listed as never seen.
|
|
191
208
|
const seen = hub.since(row.id)?.getTime() ?? row.seenAt;
|
|
@@ -195,7 +212,7 @@ export function pairedView(row, hub, serves, runnerApart = false) {
|
|
|
195
212
|
kind: String(paired.kind ?? ""),
|
|
196
213
|
serves: [...serves],
|
|
197
214
|
unserved: serves.length === 0 ? (servesNoKind(row.name, paired) ?? null) : null,
|
|
198
|
-
runs_refused:
|
|
215
|
+
runs_refused: runsRefused,
|
|
199
216
|
os: String(paired.os ?? ""),
|
|
200
217
|
os_version: typeof hello.osVersion === "string" ? hello.osVersion : null,
|
|
201
218
|
device: hello.device && typeof hello.device.maker === "string" && typeof hello.device.model === "string" ? { maker: hello.device.maker, model: hello.device.model } : null,
|
|
@@ -204,9 +221,11 @@ export function pairedView(row, hub, serves, runnerApart = false) {
|
|
|
204
221
|
groups: Array.isArray(hello.groups) ? hello.groups.map(String) : [],
|
|
205
222
|
features: featuresOf(hello.features),
|
|
206
223
|
driver: hub.hello(row.id)?.version ?? row.driver,
|
|
207
|
-
online: hub.online(row.id),
|
|
224
|
+
online: hub.online(row.id) || relayed,
|
|
208
225
|
held_by_run: holder,
|
|
209
226
|
held_until: until === undefined ? null : until.toISOString(),
|
|
227
|
+
held_on: route.runner,
|
|
228
|
+
hold_note: route.refused,
|
|
210
229
|
pairing: row.pairingId,
|
|
211
230
|
created_at: new Date(row.createdAt).toISOString(),
|
|
212
231
|
seen_at: seen === null ? null : new Date(seen).toISOString(),
|
|
@@ -271,14 +290,6 @@ export function attachSocketOf(ws, timing = { heartbeatMs: ATTACH_HEARTBEAT_MS,
|
|
|
271
290
|
}
|
|
272
291
|
/** A request's Bearer token, or undefined. */
|
|
273
292
|
export const bearerOf = (authorization) => /^Bearer (\S+)$/.exec(String(authorization ?? ""))?.[1];
|
|
274
|
-
/** The runners whose relay takes dials now: their rows seen within PRESENCE_STALE_MS on the store's clock (Runners.live,
|
|
275
|
-
* the database's on Postgres), never one process's, that say where their relay answers. The server relays a dial only
|
|
276
|
-
* to one of these, and says a dial provider is served while one answers at a host it dials (server.ts relayed). */
|
|
277
|
-
export async function liveRelays(runners) {
|
|
278
|
-
return (await runners.live(PRESENCE_STALE_MS)).filter((row) => row.host !== null && row.port !== null);
|
|
279
|
-
}
|
|
280
|
-
/** How long the server waits for a runner's attach listener to take a relayed dial. */
|
|
281
|
-
export const RELAY_CONNECT_MS = 10_000;
|
|
282
293
|
/** A close code one side of a relay may send the other: its own, or 1001 for one a peer may not send (1005, 1006). */
|
|
283
294
|
const sendable = (code) => ((code >= 1000 && code <= 1014 && code !== 1004 && code !== 1005 && code !== 1006) || (code >= 3000 && code <= 4999) ? code : ATTACH_CLOSE.going);
|
|
284
295
|
/** Two WebSockets joined frame by frame, each an attach socket with `timing` (attachSocketOf: its idle close, its binary
|
|
@@ -306,21 +317,78 @@ const machineGone = (machine) => `${machine} was deleted: its token ends`;
|
|
|
306
317
|
* provider mints. */
|
|
307
318
|
export function createAttachRoutes(options) {
|
|
308
319
|
const { store, hub, log = () => { }, now = Date.now } = options;
|
|
309
|
-
|
|
320
|
+
/** Why no run here can use a paired computer, read at each answer: on a server whose runner runs apart, while no
|
|
321
|
+
* runner relays. */
|
|
322
|
+
const runsRefused = async () => (options.runnerApart !== true || (await options.relayed?.()) === true ? null : RUNNER_APART_WORDS);
|
|
310
323
|
const paired = store.pairedComputers;
|
|
311
324
|
const minted = store.mintedTokens;
|
|
325
|
+
const settings = store.settings;
|
|
312
326
|
const tries = pairTries(now);
|
|
313
327
|
const fetches = new MinuteLimit(DAEMON_FETCHES_PER_MINUTE, undefined, now);
|
|
314
328
|
const sockets = new Set();
|
|
315
329
|
let closing = false;
|
|
316
330
|
const wss = new WebSocketServer({ noServer: true, maxPayload: MAX_LINE_BYTES, clientTracking: false, handleProtocols: () => ATTACH_SUBPROTOCOL });
|
|
331
|
+
// The server's side of the paired computers' routes (no owner: this is the server, not a runner's listener).
|
|
332
|
+
/** Each paired computer whose socket is in this hub, by its id: the socket the tick closes for a move. */
|
|
333
|
+
const home = new Map();
|
|
334
|
+
/** Each paired computer whose socket this server relays, by its id: the box's socket and the runner it goes to. */
|
|
335
|
+
const relayedPaired = new Map();
|
|
336
|
+
/** Each paired computer the tick closed for a move, and when: mid-redial until PAIRED_BRING_MS after, never refused
|
|
337
|
+
* as not attached meanwhile. */
|
|
338
|
+
const moving = new Map();
|
|
339
|
+
/** Each paired computer whose relayed dial is opening now. */
|
|
340
|
+
const dialing = new Set();
|
|
341
|
+
/** The paired computers (not removed) by id, with their names: read once at the first tick, then kept by this
|
|
342
|
+
* server's own pair and remove answers (only the server writes paired rows). */
|
|
343
|
+
let known;
|
|
344
|
+
/** The pair (its name) and remove (null) answers given while that first read is pending, by computer id: its snapshot
|
|
345
|
+
* may predate them (on Postgres a read and a write are separate connections), so they go over it before it is
|
|
346
|
+
* `known`. A read that starts after an answer's commit sees it, so an answer given while no read is pending needs no
|
|
347
|
+
* keeping, and a read that fails leaves the answers to the next one. */
|
|
348
|
+
let early;
|
|
349
|
+
/** Whether a tick is running: the interval does not wait on one, and a tick then does nothing. */
|
|
350
|
+
let ticking = false;
|
|
351
|
+
/** A pair (`name`) or a remove (null) answered: told to `known`, or kept for the first read while it is pending. */
|
|
352
|
+
function note(id, name) {
|
|
353
|
+
if (known === undefined)
|
|
354
|
+
return void early?.set(id, name);
|
|
355
|
+
if (name === null)
|
|
356
|
+
known.delete(id);
|
|
357
|
+
else
|
|
358
|
+
known.set(id, name);
|
|
359
|
+
}
|
|
360
|
+
/** The paired computers by id: `known`, or the first read, with the answers given meanwhile over its snapshot. */
|
|
361
|
+
async function knownNow() {
|
|
362
|
+
if (known !== undefined)
|
|
363
|
+
return known;
|
|
364
|
+
early = new Map();
|
|
365
|
+
try {
|
|
366
|
+
const read = new Map((await paired.list()).map((row) => [row.id, row.name]));
|
|
367
|
+
for (const [id, name] of early) {
|
|
368
|
+
if (name === null)
|
|
369
|
+
read.delete(id);
|
|
370
|
+
else
|
|
371
|
+
read.set(id, name);
|
|
372
|
+
}
|
|
373
|
+
// In the same step as the answers' merge, so none given after it misses `known`.
|
|
374
|
+
known = read;
|
|
375
|
+
return read;
|
|
376
|
+
}
|
|
377
|
+
finally {
|
|
378
|
+
early = undefined;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
/** Whether `route` is a run's of the runner beside this server, in this process (`beside`): the box's place is this
|
|
382
|
+
* hub. A route of an earlier server's runner beside it names another runner (a new name and incarnation at each
|
|
383
|
+
* start), so it is stale as any runner's that is gone. */
|
|
384
|
+
const besideRoute = (route) => options.beside !== undefined && route.runner === options.beside.runner && route.incarnation === options.beside.incarnation;
|
|
317
385
|
/** POST /v2/computers/pairings: a new code, good once until it expires. The store keeps its hash. */
|
|
318
386
|
async function createPairing() {
|
|
319
387
|
const code = newPairingCode();
|
|
320
388
|
const id = `pairing-${randomBytes(9).toString("base64url")}`;
|
|
321
389
|
const expiresAt = now() + PAIRING_CODE_TTL_MS;
|
|
322
390
|
await paired.addCode(id, sha256Hex(code), expiresAt);
|
|
323
|
-
return { id, code, expires_at: new Date(expiresAt).toISOString(), runs_refused: runsRefused };
|
|
391
|
+
return { id, code, expires_at: new Date(expiresAt).toISOString(), runs_refused: await runsRefused() };
|
|
324
392
|
}
|
|
325
393
|
/** POST /v2/computers/pair: the code traded for the computer's id and token, once; `address` the try's source. */
|
|
326
394
|
async function pair(body, address) {
|
|
@@ -341,14 +409,22 @@ export function createAttachRoutes(options) {
|
|
|
341
409
|
if (!made)
|
|
342
410
|
throw new ApiError("not_found", "no such pairing code: make a new one on the console's Computers page");
|
|
343
411
|
log("computer_paired", { computer: id, kind: hello.kind, os: hello.os, driver: hello.version });
|
|
344
|
-
|
|
412
|
+
note(id, name);
|
|
413
|
+
return { computer: id, token, runs_refused: await runsRefused() };
|
|
345
414
|
}
|
|
346
|
-
/** DELETE /v2/computers/{id}: its token revoked, a run's hold on it ended, its socket closed with 4401
|
|
415
|
+
/** DELETE /v2/computers/{id}: its token revoked, a run's hold on it ended, its socket closed with 4401 (a relayed one
|
|
416
|
+
* too, which the relay carries to the runner's hub), and its route gone. */
|
|
347
417
|
async function remove(id) {
|
|
348
418
|
const row = await paired.get(id);
|
|
349
419
|
if (row === undefined || row.revokedAt !== null || !(await paired.revoke(id)))
|
|
350
420
|
throw new ApiError("not_found", `no paired computer ${id}: GET /v2/computers lists them under paired`);
|
|
351
|
-
|
|
421
|
+
const gone = `${row.name} was removed from this server's console: pair it again to use it`;
|
|
422
|
+
hub.revoke(id, gone);
|
|
423
|
+
relayedPaired.get(id)?.ws.close(ATTACH_CLOSE.unauthorized, closeReason(gone));
|
|
424
|
+
note(id, null);
|
|
425
|
+
// Not in the revoke's transaction: a route left by a crash between the two names a revoked computer, whose token
|
|
426
|
+
// the upgrade refuses anyway, and the tick removes it.
|
|
427
|
+
await settings?.remove(routeKeyOf(id)).catch((error) => log("computer_route_unremoved", { computer: id, error: error instanceof Error ? error.message : String(error) }));
|
|
352
428
|
log("computer_removed", { computer: id });
|
|
353
429
|
}
|
|
354
430
|
/** A dial provider's tokens (computer package MachineTokens): a new one per create, kept as its SHA-256 by the machine
|
|
@@ -380,15 +456,27 @@ export function createAttachRoutes(options) {
|
|
|
380
456
|
field: "computer",
|
|
381
457
|
event: "computer",
|
|
382
458
|
gone: `${row.name} was removed from this server's console: pair it again to use it`,
|
|
383
|
-
|
|
459
|
+
moved: `${row.name} moved while it said hello: its route left this runner, and its redial goes where the route says`,
|
|
460
|
+
// Revoked first: a removal drops the route too. On a runner's listener the route must still name this runner and
|
|
461
|
+
// incarnation; one that left it (a cancelled bring, a run moved) is a move, never a revoke.
|
|
462
|
+
standing: async () => ((await paired.get(row.id))?.revokedAt !== null ? "revoked" : options.owner !== undefined && !(await routedHere(row.id)) ? "moved" : "good"),
|
|
384
463
|
seen: (hello) => paired.seen(row.id, hello),
|
|
385
464
|
});
|
|
465
|
+
/** Whether paired computer `id`'s route names this runner's listener (`owner`) and its incarnation. */
|
|
466
|
+
async function routedHere(id) {
|
|
467
|
+
if (options.owner === undefined || settings === undefined)
|
|
468
|
+
return false;
|
|
469
|
+
const { route } = await seeRoute(settings, id);
|
|
470
|
+
return route.runner === options.owner.runner && route.incarnation === options.owner.incarnation;
|
|
471
|
+
}
|
|
386
472
|
const machineDialer = (row) => ({
|
|
387
473
|
computer: mintedComputer(row.machine, { kind: row.kind, os: row.os, arch: row.arch }),
|
|
388
474
|
field: "machine",
|
|
389
475
|
event: "machine",
|
|
390
476
|
gone: machineGone(row.machine),
|
|
391
|
-
|
|
477
|
+
// A minted machine has no route, so it never moves: good or revoked.
|
|
478
|
+
moved: machineGone(row.machine),
|
|
479
|
+
standing: async () => ((await minted.get(row.machine))?.revokedAt === null ? "good" : "revoked"),
|
|
392
480
|
seen: async () => undefined,
|
|
393
481
|
});
|
|
394
482
|
/** The minted machine whose good token `authorization` bears (Bearer), or undefined. */
|
|
@@ -399,31 +487,80 @@ export function createAttachRoutes(options) {
|
|
|
399
487
|
/** Whether the socket here takes `row`'s machine: on a runner's listener (owner), only a token that runner minted in
|
|
400
488
|
* this incarnation; on the server, one its own process minted (another runner's is relayed, `relayDial`). */
|
|
401
489
|
const takes = (row) => (options.owner ? row.runner === options.owner.runner && row.incarnation === options.owner.incarnation : row.runner === null);
|
|
402
|
-
/**
|
|
403
|
-
|
|
404
|
-
* since, answers at a host this server does not dial, or does not take it; else the machine's socket is joined to one
|
|
405
|
-
* opened to the runner's listener with the proxy's proof for this token, runner and incarnation, each with this
|
|
406
|
-
* module's limits. */
|
|
407
|
-
async function relayDial(row, bearer, req, socket, head, deny) {
|
|
490
|
+
/** A minted machine's dial to the runner that minted it, as relayDial takes it. */
|
|
491
|
+
const machineTarget = (row, bearer) => {
|
|
408
492
|
const runner = row.runner;
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
493
|
+
return {
|
|
494
|
+
runner, incarnation: row.incarnation, subject: dialOf(row), whose: "made it",
|
|
495
|
+
gone: "its run makes a new machine at its next claim",
|
|
496
|
+
restarted: `runner ${runner}, which made it, restarted since, and the machine was its earlier process's: its run makes a new machine at its next claim`,
|
|
497
|
+
event: "machine", named: { ...(row.run !== null ? { run: row.run } : {}), machine: row.machine, runner },
|
|
413
498
|
// Kept on the token's row, where the runner's create waiting for this machine reads it (MachineTokens refused).
|
|
414
|
-
|
|
499
|
+
keep: (words) => minted.refuse(sha256Hex(bearer), words),
|
|
500
|
+
};
|
|
501
|
+
};
|
|
502
|
+
/** A paired computer's dial to the runner its route names (`seen`: the route's row as read), as relayDial takes it. A
|
|
503
|
+
* stale route's refusal drops the route and keeps its words on the row, in one compare-and-set on the row as read: the
|
|
504
|
+
* box's next dial lands in this hub. Any other refusal is noted on the route, so the claim waiting for the box ends at
|
|
505
|
+
* once in its words. */
|
|
506
|
+
const pairedTarget = (row, route, seen) => {
|
|
507
|
+
const runner = route.runner;
|
|
508
|
+
return {
|
|
509
|
+
runner, incarnation: route.incarnation, subject: `${row.name} (${row.id}) for run ${route.run}`, whose: "holds it",
|
|
510
|
+
gone: ROUTE_COMES_BACK,
|
|
511
|
+
restarted: routeRestartedWords(runner),
|
|
512
|
+
event: "computer", named: { run: route.run, computer: row.id, runner },
|
|
513
|
+
// Read again and tried again while the row still holds this route (the tick may have noted it meanwhile).
|
|
514
|
+
keep: async (words, stale) => {
|
|
515
|
+
let was = seen;
|
|
516
|
+
let current = route;
|
|
517
|
+
for (let tried = 0; tried < 5; tried++) {
|
|
518
|
+
if (await replaceRoute(settings, row.id, was, stale ? { ...EMPTY_ROUTE, refused: words } : { ...current, refused: words }))
|
|
519
|
+
return true;
|
|
520
|
+
const again = await seeRoute(settings, row.id);
|
|
521
|
+
if (again.route.runner !== route.runner || again.route.incarnation !== route.incarnation || again.route.run !== route.run)
|
|
522
|
+
return false;
|
|
523
|
+
({ seen: was, route: current } = again);
|
|
524
|
+
}
|
|
525
|
+
return false;
|
|
526
|
+
},
|
|
527
|
+
joined: (ws) => {
|
|
528
|
+
dialing.delete(row.id);
|
|
529
|
+
relayedPaired.set(row.id, { ws, runner, incarnation: route.incarnation });
|
|
530
|
+
ws.once("close", () => {
|
|
531
|
+
if (relayedPaired.get(row.id)?.ws === ws)
|
|
532
|
+
relayedPaired.delete(row.id);
|
|
533
|
+
});
|
|
534
|
+
// A note an earlier dial left on the route (not attached now) goes, now that the box is there.
|
|
535
|
+
if (route.refused !== null)
|
|
536
|
+
void replaceRoute(settings, row.id, seen, { ...route, refused: null }).catch(() => undefined);
|
|
537
|
+
},
|
|
538
|
+
};
|
|
539
|
+
};
|
|
540
|
+
/** The server's relay of a dial (its token `bearer`) to the runner its row names (`target`): refused 503 in words that
|
|
541
|
+
* name the dial (its run, then the machine or the computer), never its token, where that runner keeps no fresh
|
|
542
|
+
* listener, restarted since, answers at a host this server does not dial, or does not take it; else the box's socket
|
|
543
|
+
* is joined to one opened to the runner's listener with the proxy's proof for this token, runner and incarnation, each
|
|
544
|
+
* with this module's limits. */
|
|
545
|
+
async function relayDial(target, bearer, req, socket, head, deny) {
|
|
546
|
+
const { runner, named, event } = target;
|
|
547
|
+
// Kept before the answer, so whoever reads the row after the 503 (the waiting create, the box's next dial) reads it.
|
|
548
|
+
const refuse = async (why, stale = false) => {
|
|
549
|
+
const words = `${target.subject}: ${why}`;
|
|
550
|
+
log(`${event}_relay_refused`, { ...named, why: words });
|
|
551
|
+
await target.keep(words, stale).catch((error) => log(`${event}_relay_refusal_unkept`, { ...named, error: error instanceof Error ? error.message : String(error) }));
|
|
415
552
|
deny("503 Service Unavailable", words);
|
|
416
553
|
};
|
|
417
554
|
if (options.relay === undefined)
|
|
418
|
-
return refuse(`runner ${runner}
|
|
555
|
+
return refuse(`runner ${runner} ${target.whose}, and this server relays no dial to a runner`);
|
|
419
556
|
const at = (await liveRelays(options.relay.runners)).find((row) => row.runner === runner);
|
|
420
557
|
if (at === undefined)
|
|
421
|
-
return refuse(`runner ${runner}, which
|
|
422
|
-
if (at.incarnation !==
|
|
423
|
-
return refuse(
|
|
558
|
+
return refuse(`runner ${runner}, which ${target.whose}, is not running now (no attach listener of its has said it is there for ${PRESENCE_STALE_MS / 1000} s): ${target.gone}`, true);
|
|
559
|
+
if (at.incarnation !== target.incarnation)
|
|
560
|
+
return refuse(target.restarted, true);
|
|
424
561
|
const host = dialHost(at.host, options.relay.reach);
|
|
425
562
|
if (host === undefined)
|
|
426
|
-
return refuse(`runner ${runner}, which
|
|
563
|
+
return refuse(`runner ${runner}, which ${target.whose}, answers at ${at.host}, which is not one of this server's runners' hosts (AGENTRUN_RUNNER_HOSTS)`);
|
|
427
564
|
const proof = relayProof(options.relay.reach, { tokenSha256: sha256Hex(bearer), runner, incarnation: at.incarnation });
|
|
428
565
|
const upstream = new WebSocket(`ws://${host}:${at.port}${ATTACH_PATH}`, ATTACH_SUBPROTOCOL, {
|
|
429
566
|
headers: { authorization: `Bearer ${bearer}`, [LIVE_PROOF_HEADER]: proof },
|
|
@@ -434,14 +571,14 @@ export function createAttachRoutes(options) {
|
|
|
434
571
|
settled = true;
|
|
435
572
|
res.resume();
|
|
436
573
|
upstream.terminate();
|
|
437
|
-
refuse(`runner ${runner}, which
|
|
574
|
+
void refuse(`runner ${runner}, which ${target.whose}, did not take it (HTTP ${res.statusCode})`);
|
|
438
575
|
});
|
|
439
576
|
upstream.on("error", (error) => {
|
|
440
577
|
if (settled)
|
|
441
578
|
return;
|
|
442
579
|
settled = true;
|
|
443
580
|
// The library's words name the address and the step, never a header.
|
|
444
|
-
refuse(`runner ${runner}, which
|
|
581
|
+
void refuse(`runner ${runner}, which ${target.whose}, did not answer at ${host}:${at.port} (${error.message})`);
|
|
445
582
|
});
|
|
446
583
|
upstream.once("open", () => {
|
|
447
584
|
settled = true;
|
|
@@ -455,10 +592,25 @@ export function createAttachRoutes(options) {
|
|
|
455
592
|
one.once("close", () => sockets.delete(one));
|
|
456
593
|
}
|
|
457
594
|
joinSockets(ws, upstream, options.timing);
|
|
458
|
-
|
|
595
|
+
target.joined?.(ws);
|
|
596
|
+
log(`${event}_relayed`, named);
|
|
459
597
|
});
|
|
460
598
|
});
|
|
461
599
|
}
|
|
600
|
+
/** A paired computer's dial on the server, its route read (`seen`, `route`): relayed to the runner the route names,
|
|
601
|
+
* marked as dialing (the tick refuses no box mid-dial) until the relay is up or refused. */
|
|
602
|
+
async function relayPaired(row, route, seen, bearer, req, socket, head, deny) {
|
|
603
|
+
dialing.add(row.id);
|
|
604
|
+
const done = () => void dialing.delete(row.id);
|
|
605
|
+
socket.once("close", done);
|
|
606
|
+
try {
|
|
607
|
+
await relayDial(pairedTarget(row, route, seen), bearer, req, socket, head, (status, words) => (done(), deny(status, words)));
|
|
608
|
+
}
|
|
609
|
+
catch (error) {
|
|
610
|
+
done();
|
|
611
|
+
throw error;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
462
614
|
/** GET /v2/computers/daemon?arch=: the path of this server's computerd build for `arch`, for the minted machine whose
|
|
463
615
|
* token `authorization` bears. 401 for a token no machine has good (unknown, revoked or past its end); 429 past
|
|
464
616
|
* DAEMON_FETCHES_PER_MINUTE a minute by one machine; 422 for an arch with no build. */
|
|
@@ -481,8 +633,11 @@ export function createAttachRoutes(options) {
|
|
|
481
633
|
if (closing)
|
|
482
634
|
return deny("503 Service Unavailable", "this server is stopping");
|
|
483
635
|
const bearer = bearerOf(req.headers.authorization);
|
|
484
|
-
const
|
|
485
|
-
|
|
636
|
+
const found = !options.machinesOnly && bearer?.startsWith(ATTACH_TOKEN_PREFIX) ? await paired.byToken(sha256Hex(bearer)) : undefined;
|
|
637
|
+
// On a runner's listener, a paired computer's token is taken only while its route names this runner and incarnation:
|
|
638
|
+
// any other is one it does not know.
|
|
639
|
+
const row = found !== undefined && options.owner !== undefined && !(await routedHere(found.id)) ? undefined : found;
|
|
640
|
+
const machine = found === undefined ? await machineBy(req.headers.authorization) : undefined;
|
|
486
641
|
// Another runner's machine, on the server: relayed to that runner's listener below. On a runner's listener, a token
|
|
487
642
|
// of any other's (another runner's, another incarnation's, the server's) is one it does not know.
|
|
488
643
|
const relayed = machine !== undefined && !options.owner && machine.runner !== null;
|
|
@@ -493,27 +648,50 @@ export function createAttachRoutes(options) {
|
|
|
493
648
|
if (!asked.includes(ATTACH_SUBPROTOCOL)) {
|
|
494
649
|
return deny("426 Upgrade Required", `this server speaks ${ATTACH_SUBPROTOCOL}, and the driver asked for ${asked.join(", ") || "no subprotocol"}: update whichever is older`, { "sec-websocket-protocol": ATTACH_SUBPROTOCOL });
|
|
495
650
|
}
|
|
651
|
+
// A paired computer on the server: its route says where its socket goes (no route, or a run's of the runner beside
|
|
652
|
+
// this server: this hub).
|
|
653
|
+
const routed = row !== undefined && !options.owner && settings !== undefined ? await seeRoute(settings, row.id) : undefined;
|
|
496
654
|
if (socket.destroyed || closing)
|
|
497
655
|
return void socket.destroy();
|
|
498
656
|
if (relayed)
|
|
499
|
-
return relayDial(machine, bearer, req, socket, head, deny);
|
|
657
|
+
return relayDial(machineTarget(machine, bearer), bearer, req, socket, head, deny);
|
|
658
|
+
if (row !== undefined && routed !== undefined && routed.route.runner !== null && !besideRoute(routed.route))
|
|
659
|
+
return relayPaired(row, routed.route, routed.seen, bearer, req, socket, head, deny);
|
|
500
660
|
wss.handleUpgrade(req, socket, head, (ws) => {
|
|
501
661
|
sockets.add(ws);
|
|
502
662
|
ws.once("close", () => sockets.delete(ws));
|
|
503
663
|
const attach = attachSocketOf(ws, options.timing);
|
|
504
664
|
const { computer, field, event } = dialer;
|
|
505
665
|
options.onSocket?.(computer.id, ws);
|
|
666
|
+
if (routed !== undefined) {
|
|
667
|
+
home.set(computer.id, ws);
|
|
668
|
+
ws.once("close", () => {
|
|
669
|
+
if (home.get(computer.id) === ws)
|
|
670
|
+
home.delete(computer.id);
|
|
671
|
+
});
|
|
672
|
+
}
|
|
506
673
|
void hub.accept(computer, attach).then(async (said) => {
|
|
507
674
|
if (!said.ok)
|
|
508
675
|
return log(`${event}_refused`, { [field]: computer.id, code: said.error.code, why: said.error.message });
|
|
509
676
|
// The row read for the token may be older than a removal that committed, and its hub.revoke, while this socket
|
|
510
677
|
// was still being accepted (a Postgres read and write are separate connections): read the row again, now that the
|
|
511
678
|
// socket is the hub's. A removal committing after this read revokes the hub's socket after this accept, closing it.
|
|
512
|
-
|
|
679
|
+
// A route that left this runner meanwhile (only a runner's listener reads one here) is a move, as the server's
|
|
680
|
+
// tick closes one: this socket alone closes with 1001 and its driver redials; the pairing is untouched.
|
|
681
|
+
const standing = await dialer.standing();
|
|
682
|
+
if (standing === "revoked") {
|
|
513
683
|
hub.revoke(computer.id, dialer.gone);
|
|
514
684
|
return log(`${event}_refused`, { [field]: computer.id, why: "removed while it attached" });
|
|
515
685
|
}
|
|
686
|
+
if (standing === "moved") {
|
|
687
|
+
ws.close(ATTACH_CLOSE.going, closeReason(dialer.moved));
|
|
688
|
+
return log(`${event}_refused`, { [field]: computer.id, why: "its route left this runner while it attached" });
|
|
689
|
+
}
|
|
516
690
|
await dialer.seen(said.value);
|
|
691
|
+
// The note a refused or dropped route left (routed.route.refused) goes, now that the box is here; a route of the
|
|
692
|
+
// runner beside this server stays, its note alone gone.
|
|
693
|
+
if (routed !== undefined && routed.route.refused !== null)
|
|
694
|
+
await replaceRoute(settings, computer.id, routed.seen, { ...routed.route, refused: null }).catch(() => undefined);
|
|
517
695
|
log(`${event}_attached`, { [field]: computer.id, driver: said.value.version });
|
|
518
696
|
}).catch((error) => {
|
|
519
697
|
log(`${event}_refused`, { [field]: computer.id, why: error instanceof Error ? error.message : String(error) });
|
|
@@ -522,6 +700,80 @@ export function createAttachRoutes(options) {
|
|
|
522
700
|
});
|
|
523
701
|
})().catch(() => deny("500 Internal Server Error"));
|
|
524
702
|
}
|
|
703
|
+
/** The runner paired computer `id`'s socket is relayed to now, or undefined (in this hub, or attached nowhere). */
|
|
704
|
+
const relayedTo = (id) => relayedPaired.get(id)?.runner;
|
|
705
|
+
/** Closes a paired computer's socket with 1001 for a move, saying why, and notes it as moving: its driver redials, and
|
|
706
|
+
* the upgrade sends it where its route says. */
|
|
707
|
+
const move = (id, ws, why) => {
|
|
708
|
+
moving.set(id, now());
|
|
709
|
+
ws.close(ATTACH_CLOSE.going, closeReason(why));
|
|
710
|
+
};
|
|
711
|
+
/** The server's route tick (every PAIRED_ROUTE_MS, server.ts, its runner apart or beside it): one read of every route
|
|
712
|
+
* row, compared with where each box's socket is now (this hub, or the runner it is relayed to, in this process's
|
|
713
|
+
* memory). A route of the runner beside this server (besideRoute) says this hub. A socket in the wrong place (here
|
|
714
|
+
* with a runner apart's route, relayed to a runner its route no longer names, relayed with no route or with the
|
|
715
|
+
* runner beside's) is closed with 1001, its redial routed; a box routed and attached nowhere, neither moving nor
|
|
716
|
+
* mid-dial, gets its route noted "not attached now", which ends its claim's wait at once. An empty row, and a row of a
|
|
717
|
+
* computer that is no longer paired, goes. Nothing while no computer is paired; with this server's runner beside it
|
|
718
|
+
* (not runnerApart), nothing past that one read while no route row is there and no box is relayed. It judges no
|
|
719
|
+
* runner's liveness: a stale route is dropped at the box's next upgrade. One tick runs at a time: one called while
|
|
720
|
+
* another runs does nothing. */
|
|
721
|
+
async function tick() {
|
|
722
|
+
if (ticking)
|
|
723
|
+
return;
|
|
724
|
+
ticking = true;
|
|
725
|
+
try {
|
|
726
|
+
await judgeRoutes();
|
|
727
|
+
}
|
|
728
|
+
finally {
|
|
729
|
+
ticking = false;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
/** One tick's work (tick). */
|
|
733
|
+
async function judgeRoutes() {
|
|
734
|
+
if (options.owner !== undefined || settings === undefined || closing)
|
|
735
|
+
return;
|
|
736
|
+
const known = await knownNow();
|
|
737
|
+
if (known.size === 0 && relayedPaired.size === 0)
|
|
738
|
+
return;
|
|
739
|
+
const rows = await allRoutes(settings);
|
|
740
|
+
// With its runner beside it, only a runner apart on another host takes a route: the read says whether one did. A
|
|
741
|
+
// box still relayed counts too, since the drop that ended its route (with no note) removed the row.
|
|
742
|
+
if (options.runnerApart !== true && rows.length === 0 && relayedPaired.size === 0)
|
|
743
|
+
return;
|
|
744
|
+
const at = now();
|
|
745
|
+
for (const [id, since] of moving)
|
|
746
|
+
if (at - since > PAIRED_BRING_MS)
|
|
747
|
+
moving.delete(id);
|
|
748
|
+
const routes = new Map(rows.map((row) => [row.id, row.route]));
|
|
749
|
+
for (const [id, relayed] of relayedPaired) {
|
|
750
|
+
const route = routes.get(id);
|
|
751
|
+
if (route === undefined || route.runner === null)
|
|
752
|
+
move(id, relayed.ws, `${known.get(id) ?? id} came back to this server: no run holds it now`);
|
|
753
|
+
else if (besideRoute(route))
|
|
754
|
+
move(id, relayed.ws, `${known.get(id) ?? id} came back to this server: run ${route.run} holds it here`);
|
|
755
|
+
else if (route.runner !== relayed.runner || route.incarnation !== relayed.incarnation)
|
|
756
|
+
move(id, relayed.ws, `${known.get(id) ?? id} moved to the runner that holds run ${route.run}`);
|
|
757
|
+
}
|
|
758
|
+
for (const { id, seen, route } of rows) {
|
|
759
|
+
const name = known.get(id);
|
|
760
|
+
if (name === undefined || (route.runner === null && route.refused === null)) {
|
|
761
|
+
await replaceRoute(settings, id, seen, EMPTY_ROUTE).catch(() => undefined);
|
|
762
|
+
continue;
|
|
763
|
+
}
|
|
764
|
+
if (route.runner === null)
|
|
765
|
+
continue;
|
|
766
|
+
const here = home.get(id);
|
|
767
|
+
// A box in this hub under a run of the runner beside this server is where its route says: it stays.
|
|
768
|
+
if (here !== undefined && besideRoute(route))
|
|
769
|
+
continue;
|
|
770
|
+
if (here !== undefined)
|
|
771
|
+
move(id, here, `${name} moved to the runner that holds run ${route.run}`);
|
|
772
|
+
else if (!relayedPaired.has(id) && !moving.has(id) && !dialing.has(id) && route.refused === null) {
|
|
773
|
+
await replaceRoute(settings, id, seen, { ...route, refused: notAttachedWords(name) }).catch(() => undefined);
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
}
|
|
525
777
|
/** Every attach socket closed with 1001 (the server is going), and no new one taken; resolves once each has ended. A
|
|
526
778
|
* peer that does not answer the close within a second is cut, so the server's own close never waits on it. */
|
|
527
779
|
async function close() {
|
|
@@ -534,5 +786,5 @@ export function createAttachRoutes(options) {
|
|
|
534
786
|
await Promise.all(ended);
|
|
535
787
|
clearTimeout(cut);
|
|
536
788
|
}
|
|
537
|
-
return { createPairing, pair, remove, upgrade, close, machineTokens, daemon };
|
|
789
|
+
return { createPairing, pair, remove, upgrade, close, machineTokens, daemon, tick, relayedTo, runsRefused };
|
|
538
790
|
}
|
|
@@ -18,10 +18,10 @@ export declare function heldField(store: HeldStore, runId: string, options?: Hel
|
|
|
18
18
|
why?: string;
|
|
19
19
|
} | undefined>;
|
|
20
20
|
/** The run read's `computer` field (`{ computer }`, or nothing for a run with none): its revision's computer setting, else
|
|
21
|
-
* the one its place says; taken_over while the owner holds its desktop or phone (computerHoldOf, read from the run's
|
|
22
|
-
* journal only while that computer is up); with `held` for a run that holds its computer for its owner. The journal is
|
|
21
|
+
* the one its place says; released once a stop let it go, or while the run is parked with it let go (releasedAtPark); taken_over while the owner holds its desktop or phone (computerHoldOf, read from the run's
|
|
22
|
+
* journal only while that computer is up) or its browser (browserHeld, one settings read while it is up); with `held` for a run that holds its computer for its owner. The journal is
|
|
23
23
|
* read at most once, and not at all for a run that neither can be taken over nor holds. `view` is the run read so far. */
|
|
24
|
-
export declare function runComputerRead(store: HeldStore & Pick<Store, "agents">, runId: string, view: Record<string, unknown>, options?: HeldOptions): Promise<{
|
|
24
|
+
export declare function runComputerRead(store: HeldStore & Pick<Store, "agents" | "settings">, runId: string, view: Record<string, unknown>, options?: HeldOptions): Promise<{
|
|
25
25
|
computer?: Record<string, unknown>;
|
|
26
26
|
}>;
|
|
27
27
|
/**
|
|
@@ -29,7 +29,8 @@ export declare function runComputerRead(store: HeldStore & Pick<Store, "agents">
|
|
|
29
29
|
* - callRunComputer: the channel's answer (callComputer), the computer's own: `{ ok: true, value }` or `{ ok: false,
|
|
30
30
|
* error }`. 422 for a method the protocol does not have.
|
|
31
31
|
* - startRun, releaseRunComputer: 404 for a run that holds no computer, and for a release of one already let go; 409
|
|
32
|
-
* computer_not_live when the hold is not where the route needs it (not up yet, already started, released).
|
|
32
|
+
* computer_not_live when the hold is not where the route needs it (not up yet, already started, released). A start of a
|
|
33
|
+
* run whose model kept it from running at its create: 409 agent_not_runnable, in its create's words (hold.refused).
|
|
33
34
|
* Either: 503 when the runner took no row in time.
|
|
34
35
|
*/
|
|
35
36
|
export declare function heldRoute(name: string, runId: string, body: unknown, store: HeldStore, options?: HeldOptions): Promise<unknown>;
|