@parcha/agentrun-serve 0.1.0-beta.57 → 0.1.0-beta.59
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 +376 -0
- package/README.md +393 -118
- package/dist/acp-package.d.ts +5 -1
- package/dist/acp-package.js +19 -2
- package/dist/adapters.d.ts +23 -2
- package/dist/adapters.js +106 -33
- package/dist/agents-store.js +1 -1
- package/dist/answer-text.d.ts +3 -0
- package/dist/answer-text.js +22 -0
- package/dist/api-only.d.ts +2 -4
- package/dist/api-only.js +2 -4
- package/dist/attach-conformance.js +5 -4
- package/dist/box-paths.d.ts +7 -5
- package/dist/box-paths.js +12 -8
- package/dist/browser-computer.d.ts +3 -8
- package/dist/browser-computer.js +12 -35
- package/dist/browser-cost.js +94 -20
- package/dist/capacity.d.ts +1 -1
- package/dist/capacity.js +3 -3
- package/dist/chatgpt-plan.d.ts +5 -0
- package/dist/chatgpt-plan.js +25 -11
- package/dist/claude-login-token.d.ts +121 -0
- package/dist/claude-login-token.js +797 -0
- package/dist/claude-login.d.ts +119 -34
- package/dist/claude-login.js +551 -79
- package/dist/claude-sign-in.d.ts +3 -0
- package/dist/claude-sign-in.js +10 -1
- package/dist/cli.js +325 -328
- package/dist/composio-made.d.ts +1 -6
- package/dist/composio-made.js +1 -6
- package/dist/composio-owners.d.ts +26 -0
- package/dist/composio-owners.js +36 -0
- package/dist/composio-preset.d.ts +56 -23
- package/dist/composio-preset.js +173 -77
- package/dist/composio-triggers.d.ts +4 -4
- package/dist/composio-triggers.js +6 -7
- package/dist/compute-cost.js +11 -2
- package/dist/computer-attach.d.ts +40 -19
- package/dist/computer-attach.js +326 -62
- package/dist/computer-held-routes.d.ts +1 -1
- package/dist/computer-held-routes.js +9 -4
- package/dist/computer-keys.d.ts +11 -1
- package/dist/computer-keys.js +26 -1
- package/dist/computer-record.js +3 -1
- package/dist/computer-view.d.ts +5 -3
- package/dist/computer-view.js +10 -6
- package/dist/computer-viewer.js +36 -7
- package/dist/computers.d.ts +12 -11
- package/dist/computers.js +11 -8
- package/dist/console/THIRD-PARTY-NOTICES.txt +50 -0
- package/dist/console/assets/backend-BztxEAvb.js +166 -0
- package/dist/console/assets/index-LwNL4L9q.js +619 -0
- package/dist/console/assets/share-L84tlUDW.js +6 -0
- package/dist/console/assets/styles-CaRKlHke.js +291 -0
- package/dist/console/assets/styles-DQo8rzY-.css +1 -0
- package/dist/console/index.html +4 -3
- package/dist/console/share.html +17 -0
- package/dist/decisions.d.ts +17 -7
- package/dist/decisions.js +41 -15
- package/dist/dial-relay.d.ts +25 -4
- package/dist/dial-relay.js +139 -5
- package/dist/docker-daemon.js +4 -5
- package/dist/docker-host-addresses.js +29 -11
- package/dist/email/account.d.ts +6 -0
- package/dist/email/account.js +6 -0
- package/dist/email/authentication.d.ts +21 -0
- package/dist/email/authentication.js +106 -0
- package/dist/email/email-faux.d.ts +11 -2
- package/dist/email/email-faux.js +21 -5
- package/dist/email/mail.d.ts +25 -0
- package/dist/email/mail.js +58 -2
- package/dist/email/names.js +2 -2
- package/dist/email/tools.d.ts +9 -4
- package/dist/email/tools.js +95 -13
- package/dist/env-switch.d.ts +13 -5
- package/dist/env-switch.js +24 -6
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +2 -0
- package/dist/fal-price.d.ts +12 -4
- package/dist/fal-price.js +110 -26
- package/dist/faux-computers.js +1 -1
- package/dist/filed-answer.d.ts +2 -2
- package/dist/filed-answer.js +2 -2
- package/dist/graph-served.d.ts +10 -5
- package/dist/graph-served.js +40 -10
- package/dist/handed-values.js +1 -2
- package/dist/harness-check.d.ts +2 -1
- package/dist/harness-check.js +45 -12
- package/dist/harness-keys.js +9 -6
- package/dist/harness-opencode/facts.js +2 -0
- package/dist/harness-recipe-entry.d.ts +9 -3
- package/dist/harness-recipe-entry.js +18 -9
- package/dist/harness-recipe.d.ts +21 -0
- package/dist/harness-recipe.js +25 -3
- package/dist/harness-table.d.ts +8 -0
- package/dist/harness-table.js +17 -6
- package/dist/helper-env.d.ts +4 -1
- package/dist/helper-env.js +4 -1
- package/dist/host-check.d.ts +2 -3
- package/dist/host-check.js +13 -7
- package/dist/hosted-search.js +1 -1
- package/dist/import.d.ts +11 -8
- package/dist/import.js +437 -186
- package/dist/key-refusal.d.ts +9 -2
- package/dist/key-refusal.js +86 -32
- package/dist/learn/audit.d.ts +8 -5
- package/dist/learn/audit.js +15 -7
- package/dist/learn/compile.d.ts +5 -2
- package/dist/learn/compile.js +45 -21
- package/dist/learn/field-check.d.ts +111 -0
- package/dist/learn/field-check.js +174 -0
- package/dist/learn/ledger.d.ts +173 -0
- package/dist/learn/ledger.js +225 -0
- package/dist/learn/promote.d.ts +82 -39
- package/dist/learn/promote.js +61 -66
- package/dist/learn/secrets.d.ts +4 -0
- package/dist/learn/secrets.js +12 -0
- package/dist/learned-nodes.d.ts +8 -5
- package/dist/learned-nodes.js +8 -5
- package/dist/learned-workflow.d.ts +2 -1
- package/dist/learned-workflow.js +13 -10
- package/dist/learning-cost.d.ts +40 -21
- package/dist/learning-cost.js +30 -21
- package/dist/learning-setting.d.ts +6 -0
- package/dist/learning-setting.js +11 -1
- package/dist/listen-first.d.ts +5 -4
- package/dist/listen-first.js +4 -5
- package/dist/live-reach.d.ts +9 -0
- package/dist/live-reach.js +9 -0
- package/dist/mcp-api-only-origins.d.ts +23 -3
- package/dist/mcp-api-only-origins.js +57 -17
- package/dist/mcp-gateway.d.ts +16 -2
- package/dist/mcp-gateway.js +41 -3
- package/dist/mcp-oauth.d.ts +1 -1
- package/dist/mcp-oauth.js +3 -2
- package/dist/mcp-probe.d.ts +3 -0
- package/dist/mcp-probe.js +3 -0
- package/dist/mcp-secret-writes.d.ts +24 -2
- package/dist/mcp-secret-writes.js +30 -2
- package/dist/mcp-servers-store.d.ts +52 -14
- package/dist/mcp-servers-store.js +165 -61
- package/dist/mcp-sources.d.ts +1 -2
- package/dist/mcp-sources.js +1 -2
- package/dist/mcp-store.d.ts +5 -5
- package/dist/mcp-store.js +3 -3
- package/dist/mcp.js +1 -1
- package/dist/micro.d.ts +2 -0
- package/dist/micro.js +2 -0
- package/dist/model-gateway.d.ts +1 -1
- package/dist/model-gateway.js +20 -36
- package/dist/model.d.ts +5 -4
- package/dist/model.js +7 -6
- package/dist/monid.d.ts +14 -1
- package/dist/monid.js +103 -26
- package/dist/ollama-provider.d.ts +23 -9
- package/dist/ollama-provider.js +49 -20
- package/dist/openapi.js +234 -61
- package/dist/otel/genai.d.ts +83 -7
- package/dist/otel/genai.js +69 -9
- package/dist/otel/tracer.d.ts +42 -13
- package/dist/otel/tracer.js +241 -56
- package/dist/page.d.ts +7 -1
- package/dist/page.js +76 -22
- package/dist/paid-calls.d.ts +6 -4
- package/dist/paid-calls.js +15 -7
- package/dist/place-offers.d.ts +59 -34
- package/dist/place-offers.js +100 -58
- package/dist/place.d.ts +13 -14
- package/dist/place.js +56 -41
- package/dist/print.js +1 -1
- package/dist/provider.d.ts +5 -4
- package/dist/provider.js +26 -13
- package/dist/redact/match.d.ts +27 -9
- package/dist/redact/match.js +266 -57
- package/dist/run-census.d.ts +1 -1
- package/dist/run-census.js +4 -5
- package/dist/run-cut.js +4 -5
- package/dist/run-export-store.d.ts +15 -0
- package/dist/run-export-store.js +30 -0
- package/dist/run-export.d.ts +139 -0
- package/dist/run-export.js +379 -0
- package/dist/run-file-cut.d.ts +5 -0
- package/dist/run-file-cut.js +13 -0
- package/dist/run-launch.js +16 -3
- package/dist/run-learning-served.d.ts +2 -2
- package/dist/run-model-key.d.ts +15 -6
- package/dist/run-model-key.js +30 -31
- package/dist/run-reads-over-store.js +4 -4
- package/dist/run-secrets.js +3 -5
- package/dist/run-timeline-served.js +3 -1
- package/dist/run-view.d.ts +1 -2
- package/dist/run-view.js +2 -1
- package/dist/runnable.js +2 -2
- package/dist/runner/adapter-loop.d.ts +35 -18
- package/dist/runner/adapter-loop.js +97 -63
- package/dist/runner/adapter-run-spec.d.ts +34 -14
- package/dist/runner/adapter-run-spec.js +120 -75
- package/dist/runner/adapters/pi.js +4 -2
- package/dist/runner/audit.d.ts +8 -5
- package/dist/runner/audit.js +37 -17
- package/dist/runner/beside.d.ts +9 -2
- package/dist/runner/beside.js +29 -13
- package/dist/runner/claim-secrets.d.ts +27 -18
- package/dist/runner/claim-secrets.js +61 -47
- package/dist/runner/cli-run-spec.d.ts +18 -0
- package/dist/runner/cli-run-spec.js +93 -46
- package/dist/runner/compare-end.d.ts +2 -1
- package/dist/runner/compare-end.js +25 -10
- package/dist/runner/conformance/harness.js +2 -3
- package/dist/runner/conformance/subject.d.ts +4 -1
- package/dist/runner/harness-loops.js +3 -3
- package/dist/runner/held.d.ts +19 -4
- package/dist/runner/journal-kinds.d.ts +4 -1
- package/dist/runner/journal-kinds.js +22 -4
- package/dist/runner/learned-first.js +20 -4
- package/dist/runner/learning-end.d.ts +2 -8
- package/dist/runner/learning-end.js +11 -19
- package/dist/runner/live-ticker.js +7 -5
- package/dist/runner/loop-contract.d.ts +0 -2
- package/dist/runner/loop.d.ts +7 -0
- package/dist/runner/loop.js +1 -0
- package/dist/runner/main.d.ts +11 -6
- package/dist/runner/main.js +24 -18
- package/dist/runner/mcp-claim.js +24 -7
- package/dist/runner/mcp-run.d.ts +3 -6
- package/dist/runner/mcp-run.js +3 -4
- package/dist/runner/meta-tools.js +3 -2
- package/dist/runner/metrics.d.ts +7 -0
- package/dist/runner/metrics.js +19 -0
- package/dist/runner/model-route.d.ts +10 -7
- package/dist/runner/model-route.js +79 -15
- package/dist/runner/native-loop.js +10 -8
- package/dist/runner/presence.d.ts +11 -4
- package/dist/runner/presence.js +11 -6
- package/dist/runner/promotion.d.ts +25 -13
- package/dist/runner/promotion.js +125 -50
- package/dist/runner/redact.d.ts +3 -1
- package/dist/runner/redact.js +5 -3
- package/dist/runner/reflect-hook.js +1 -1
- package/dist/runner/responses-fetch.d.ts +36 -0
- package/dist/runner/responses-fetch.js +511 -0
- package/dist/runner/run-child-host.d.ts +38 -12
- package/dist/runner/run-child-host.js +231 -83
- package/dist/runner/run-child.js +11 -4
- package/dist/runner/runner-check.js +5 -1
- package/dist/runner/runner.d.ts +5 -3
- package/dist/runner/runner.js +14 -10
- package/dist/runner/save-slots.js +2 -1
- package/dist/runner/sdk-loop.js +8 -8
- package/dist/runner/spec-holds.d.ts +8 -1
- package/dist/runner/spec-holds.js +9 -4
- package/dist/runner/subagent.d.ts +11 -0
- package/dist/runner/subagent.js +17 -3
- package/dist/runner/workflow-calls.d.ts +9 -0
- package/dist/runner/workflow-calls.js +150 -0
- package/dist/runner/workflow-first.d.ts +138 -37
- package/dist/runner/workflow-first.js +230 -99
- package/dist/runner/workflow-spec.js +2 -2
- package/dist/runs-over-store.js +8 -7
- package/dist/save-once.d.ts +14 -0
- package/dist/save-once.js +31 -0
- package/dist/scrub-command.d.ts +34 -31
- package/dist/scrub-command.js +116 -147
- package/dist/sdk/runs.d.ts +6 -0
- package/dist/sdk/runs.js +89 -10
- package/dist/seal-key.js +4 -4
- package/dist/secret-cut.d.ts +3 -0
- package/dist/secret-cut.js +7 -1
- package/dist/secret-rules.d.ts +8 -1
- package/dist/secret-rules.js +482 -53
- package/dist/secrets.d.ts +51 -9
- package/dist/secrets.js +311 -21
- package/dist/server-keys.d.ts +4 -3
- package/dist/server-keys.js +4 -3
- package/dist/server-secrets-store.js +2 -4
- package/dist/server.d.ts +33 -10
- package/dist/server.js +454 -136
- package/dist/share-context.d.ts +22 -0
- package/dist/share-context.js +76 -0
- package/dist/shell-lex.d.ts +105 -0
- package/dist/shell-lex.js +646 -0
- package/dist/sign-ins-store.d.ts +5 -7
- package/dist/sign-ins-store.js +12 -16
- package/dist/sign-ins.d.ts +7 -11
- package/dist/sign-ins.js +40 -41
- package/dist/skills/agentrun-serve/SKILL.md +5 -2
- package/dist/slots.d.ts +9 -0
- package/dist/slots.js +45 -0
- package/dist/start-switches.d.ts +6 -5
- package/dist/start-switches.js +8 -4
- package/dist/stats.d.ts +5 -1
- package/dist/stats.js +1 -1
- package/dist/store/agent-body.js +2 -2
- package/dist/store/dialect.d.ts +4 -0
- package/dist/store/dialect.js +1 -0
- package/dist/store/index.d.ts +2 -2
- package/dist/store/index.js +1 -1
- package/dist/store/mcp-record-keys.d.ts +10 -0
- package/dist/store/mcp-record-keys.js +13 -0
- package/dist/store/postgres/dialect.js +2 -0
- package/dist/store/postgres/migrations/0028_learning_ledger.sql +44 -0
- package/dist/store/postgres/migrations/0029_run_launch_census_settle.sql +28 -0
- package/dist/store/postgres/migrations/0030_learning_ledger_epoch.sql +40 -0
- package/dist/store/postgres/migrations/0031_learning_ledger_calls.sql +12 -0
- package/dist/store/postgres/store.js +4 -0
- package/dist/store/schema.d.ts +44 -4
- package/dist/store/schema.js +111 -11
- package/dist/store/seal.d.ts +4 -1
- package/dist/store/sqlite/run-launch.d.ts +3 -10
- package/dist/store/sqlite/run-launch.js +12 -23
- package/dist/store/sqlite/run-patch.d.ts +19 -2
- package/dist/store/sqlite/run-patch.js +67 -2
- package/dist/store/sqlite/store.d.ts +3 -0
- package/dist/store/sqlite/store.js +177 -32
- package/dist/store/store-schema.js +3 -4
- package/dist/store/store.d.ts +74 -25
- package/dist/store/store.js +14 -11
- package/dist/store-reads/analytics.d.ts +2 -1
- package/dist/store-reads/analytics.js +7 -5
- package/dist/store-reads/learning.d.ts +102 -136
- package/dist/store-reads/learning.js +251 -302
- package/dist/store-reads/stats.d.ts +2 -2
- package/dist/store-reads/stats.js +13 -7
- package/dist/store-view.js +10 -7
- package/dist/suggestions.d.ts +1 -1
- package/dist/suggestions.js +29 -13
- package/dist/template-eval.d.ts +91 -9
- package/dist/template-eval.js +448 -105
- package/dist/template-import.d.ts +0 -10
- package/dist/template-import.js +5 -10
- package/dist/timeline/labels.d.ts +1 -1
- package/dist/timeline/labels.js +4 -3
- package/dist/timeline/timeline.d.ts +23 -11
- package/dist/timeline/timeline.js +115 -31
- package/dist/timeline/web-calls.d.ts +14 -14
- package/dist/timeline/web-calls.js +18 -30
- package/dist/tool-gate.d.ts +13 -1
- package/dist/tool-gate.js +43 -3
- package/dist/tool-keys-store.js +2 -4
- package/dist/tool-keys.d.ts +11 -0
- package/dist/tool-keys.js +16 -0
- package/dist/tool-plane.d.ts +50 -11
- package/dist/tool-plane.js +94 -30
- package/dist/tools.d.ts +5 -1
- package/dist/tools.js +7 -7
- package/dist/trigger-email.js +12 -6
- package/dist/triggers.d.ts +15 -34
- package/dist/triggers.js +119 -126
- package/dist/validate.d.ts +8 -1
- package/dist/validate.js +163 -23
- package/dist/web-fake.d.ts +2 -1
- package/dist/web-fake.js +8 -6
- package/dist/web-fetch.d.ts +8 -6
- package/dist/web-fetch.js +13 -11
- package/dist/web-holds.d.ts +32 -0
- package/dist/web-holds.js +86 -0
- package/dist/web-run.d.ts +32 -5
- package/dist/web-run.js +75 -28
- package/dist/web-save.d.ts +4 -9
- package/dist/web-save.js +27 -55
- package/dist/web-search.js +14 -25
- package/dist/wires.d.ts +4 -3
- package/dist/wires.js +5 -4
- package/dist/workspace/archil-claim.d.ts +12 -5
- package/dist/workspace/archil-claim.js +45 -5
- package/dist/workspace/attach.js +52 -19
- package/dist/workspace/attached-computers.d.ts +38 -11
- package/dist/workspace/attached-computers.js +81 -16
- package/dist/workspace/computer-deps.d.ts +29 -7
- package/dist/workspace/computer-deps.js +36 -18
- package/dist/workspace/computer-for-claim.d.ts +21 -4
- package/dist/workspace/computer-for-claim.js +166 -57
- package/dist/workspace/computer-intervals.d.ts +2 -0
- package/dist/workspace/computer-intervals.js +1 -1
- package/dist/workspace/computer-janitor.d.ts +48 -14
- package/dist/workspace/computer-janitor.js +153 -44
- package/dist/workspace/computer-kept.d.ts +158 -32
- package/dist/workspace/computer-kept.js +288 -63
- package/dist/workspace/computer-providers.d.ts +8 -3
- package/dist/workspace/computer-providers.js +12 -5
- package/dist/workspace/index.d.ts +1 -1
- package/dist/workspace/index.js +1 -1
- package/dist/workspace/local.js +41 -1
- package/dist/workspace/mount-revoke-left.d.ts +31 -0
- package/dist/workspace/mount-revoke-left.js +72 -0
- package/dist/workspace/paired-route.d.ts +91 -0
- package/dist/workspace/paired-route.js +152 -0
- package/dist/workspace/resolve-computer.d.ts +5 -2
- package/dist/workspace/resolve-computer.js +9 -3
- package/dist/workspace/run-end-computers.d.ts +11 -5
- package/dist/workspace/run-end-computers.js +153 -28
- package/dist/workspace/run-tools.d.ts +22 -3
- package/dist/workspace/run-tools.js +17 -5
- package/dist/workspace/run-workspace.d.ts +14 -1
- package/dist/workspace/run-workspace.js +34 -1
- package/dist/workspace/workspace.d.ts +6 -0
- package/dist/wrapped.d.ts +8 -2
- package/dist/wrapped.js +21 -6
- package/package.json +12 -13
- package/skills/agentrun-serve/SKILL.md +5 -2
- package/templates/README.md +25 -9
- package/dist/console/assets/backend-DVhHzJX2.js +0 -166
- package/dist/console/assets/index-Bwn1oTem.css +0 -1
- package/dist/console/assets/index-okP-y8PK.js +0 -747
package/dist/computer-attach.js
CHANGED
|
@@ -14,24 +14,37 @@
|
|
|
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
|
-
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,
|
|
35
|
+
import { ATTACH_CLOSE, ATTACH_HEARTBEAT_MS, ATTACH_IDLE_MS, ATTACH_PATH, ATTACH_SUBPROTOCOL, attachSubprotocolOf, helloProblem, hostedDaemonPath, laterProtocol, MAX_LINE_BYTES, mintedComputer, newMachineToken, PAIRING_CODE_PATTERN, PAIRING_CODE_TTL_MS, RUN_LABEL, upgradeRefusal } 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, RELAY_TOKEN_HEADER, relayProof } from "./live-reach.js";
|
|
31
39
|
import { peerKey } from "./owner.js";
|
|
32
40
|
import { registerNotSecretWrite } from "./secret-writes.js";
|
|
33
|
-
import {
|
|
41
|
+
import { liveRelays } from "./runner/presence.js";
|
|
42
|
+
import { RUNNER_STALE_MS } from "./store/index.js";
|
|
34
43
|
import { pairedOf, PAIRED_ID_PREFIX, RUNNER_APART_WORDS, servesNoKind } from "./workspace/attached-computers.js";
|
|
44
|
+
import { allRoutes, EMPTY_ROUTE, notAttachedWords, PAIRED_BRING_MS, replaceRoute, ROUTE_COMES_BACK, routeKeyOf, routeRestartedWords, seeRoute } from "./workspace/paired-route.js";
|
|
45
|
+
// Where they were first defined, for the readers that import them from here.
|
|
46
|
+
export { liveRelays } from "./runner/presence.js";
|
|
47
|
+
export { RELAY_CONNECT_MS } from "./live-reach.js";
|
|
35
48
|
// Routes under /computers, where a provider's keys are a secret write (computers.ts): these three write none of the
|
|
36
49
|
// owner's secrets, so the API token may call them as the console's session does.
|
|
37
50
|
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.");
|
|
@@ -167,11 +180,14 @@ export function pairBodyOf(body) {
|
|
|
167
180
|
return { code: code, name, hello: hello };
|
|
168
181
|
}
|
|
169
182
|
/** Why a driver's hello may not pair here, in the version rule's words (attach.ts helloMismatch), or undefined: its
|
|
170
|
-
* protocol version must be this server
|
|
183
|
+
* protocol version must be one this server takes (helloProblem: 0.2 or a later 0.x). The words name the side to
|
|
184
|
+
* update, and the code stays good, for the driver once it is updated. */
|
|
171
185
|
export function pairRefusal(name, hello) {
|
|
172
|
-
|
|
186
|
+
const problem = helloProblem(hello.protocol, "server");
|
|
187
|
+
if (!problem)
|
|
173
188
|
return undefined;
|
|
174
|
-
|
|
189
|
+
const update = laterProtocol(hello.protocol) ? "the server is older, so once whoever runs it updates it, pair" : `update the driver on ${name}, then pair`;
|
|
190
|
+
return `${name}'s driver ${hello.version} ${problem}: ${update} with the same code`;
|
|
175
191
|
}
|
|
176
192
|
/** A hello's features as the list shows them: each that is on (true) or off with why ({off: words}); anything else a
|
|
177
193
|
* driver sent is left out, so a malformed hello never reaches the console as a shape it does not know. */
|
|
@@ -180,12 +196,17 @@ function featuresOf(features) {
|
|
|
180
196
|
return {};
|
|
181
197
|
return Object.fromEntries(Object.entries(features).flatMap(([name, value]) => (value === true ? [[name, true]] : isObject(value) && typeof value.off === "string" ? [[name, { off: value.off }]] : [])));
|
|
182
198
|
}
|
|
183
|
-
|
|
199
|
+
/** `row` as GET /v2/computers lists it. `runsRefused`: why no run here can use it (RUNNER_APART_WORDS), or null. `route`:
|
|
200
|
+
* its route (workspace/paired-route.ts), which names the run that holds it and the runner whose hub does while a run's
|
|
201
|
+
* claim routed it there; `relayed`: its socket is relayed to that runner now (createAttachRoutes relayedPaired). */
|
|
202
|
+
export function pairedView(row, hub, serves, runsRefused = null, route = EMPTY_ROUTE, relayed = false) {
|
|
184
203
|
const paired = row.hello;
|
|
185
204
|
// Kind and os stay what it paired as (another is refused at hello); the rest follows its driver's live hello.
|
|
186
205
|
const hello = hub.hello(row.id) ?? paired;
|
|
187
|
-
|
|
188
|
-
const
|
|
206
|
+
// A run's hold on a runner's hub is the route's; on this server's hub, the hub's own.
|
|
207
|
+
const routed = route.runner !== null;
|
|
208
|
+
const holder = routed ? route.run : (hub.claimed(row.id)?.[RUN_LABEL] ?? null);
|
|
209
|
+
const until = routed ? (route.until === null ? undefined : new Date(route.until)) : holder === null ? undefined : hub.deadline(row.id);
|
|
189
210
|
// The hub has the socket live before the store keeps its hello (attach's accept, then seen), so a list between the
|
|
190
211
|
// two reads the hub's time: an online computer is never listed as never seen.
|
|
191
212
|
const seen = hub.since(row.id)?.getTime() ?? row.seenAt;
|
|
@@ -195,7 +216,7 @@ export function pairedView(row, hub, serves, runnerApart = false) {
|
|
|
195
216
|
kind: String(paired.kind ?? ""),
|
|
196
217
|
serves: [...serves],
|
|
197
218
|
unserved: serves.length === 0 ? (servesNoKind(row.name, paired) ?? null) : null,
|
|
198
|
-
runs_refused:
|
|
219
|
+
runs_refused: runsRefused,
|
|
199
220
|
os: String(paired.os ?? ""),
|
|
200
221
|
os_version: typeof hello.osVersion === "string" ? hello.osVersion : null,
|
|
201
222
|
device: hello.device && typeof hello.device.maker === "string" && typeof hello.device.model === "string" ? { maker: hello.device.maker, model: hello.device.model } : null,
|
|
@@ -204,9 +225,11 @@ export function pairedView(row, hub, serves, runnerApart = false) {
|
|
|
204
225
|
groups: Array.isArray(hello.groups) ? hello.groups.map(String) : [],
|
|
205
226
|
features: featuresOf(hello.features),
|
|
206
227
|
driver: hub.hello(row.id)?.version ?? row.driver,
|
|
207
|
-
online: hub.online(row.id),
|
|
228
|
+
online: hub.online(row.id) || relayed,
|
|
208
229
|
held_by_run: holder,
|
|
209
230
|
held_until: until === undefined ? null : until.toISOString(),
|
|
231
|
+
held_on: route.runner,
|
|
232
|
+
hold_note: route.refused,
|
|
210
233
|
pairing: row.pairingId,
|
|
211
234
|
created_at: new Date(row.createdAt).toISOString(),
|
|
212
235
|
seen_at: seen === null ? null : new Date(seen).toISOString(),
|
|
@@ -271,14 +294,6 @@ export function attachSocketOf(ws, timing = { heartbeatMs: ATTACH_HEARTBEAT_MS,
|
|
|
271
294
|
}
|
|
272
295
|
/** A request's Bearer token, or undefined. */
|
|
273
296
|
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
297
|
/** 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
298
|
const sendable = (code) => ((code >= 1000 && code <= 1014 && code !== 1004 && code !== 1005 && code !== 1006) || (code >= 3000 && code <= 4999) ? code : ATTACH_CLOSE.going);
|
|
284
299
|
/** Two WebSockets joined frame by frame, each an attach socket with `timing` (attachSocketOf: its idle close, its binary
|
|
@@ -306,21 +321,78 @@ const machineGone = (machine) => `${machine} was deleted: its token ends`;
|
|
|
306
321
|
* provider mints. */
|
|
307
322
|
export function createAttachRoutes(options) {
|
|
308
323
|
const { store, hub, log = () => { }, now = Date.now } = options;
|
|
309
|
-
|
|
324
|
+
/** Why no run here can use a paired computer, read at each answer: on a server whose runner runs apart, while no
|
|
325
|
+
* runner relays. */
|
|
326
|
+
const runsRefused = async () => (options.runnerApart !== true || (await options.relayed?.()) === true ? null : RUNNER_APART_WORDS);
|
|
310
327
|
const paired = store.pairedComputers;
|
|
311
328
|
const minted = store.mintedTokens;
|
|
329
|
+
const settings = store.settings;
|
|
312
330
|
const tries = pairTries(now);
|
|
313
331
|
const fetches = new MinuteLimit(DAEMON_FETCHES_PER_MINUTE, undefined, now);
|
|
314
332
|
const sockets = new Set();
|
|
315
333
|
let closing = false;
|
|
316
|
-
const wss = new WebSocketServer({ noServer: true, maxPayload: MAX_LINE_BYTES, clientTracking: false, handleProtocols: () =>
|
|
334
|
+
const wss = new WebSocketServer({ noServer: true, maxPayload: MAX_LINE_BYTES, clientTracking: false, handleProtocols: (asked) => attachSubprotocolOf([...asked]) ?? false });
|
|
335
|
+
// The server's side of the paired computers' routes (no owner: this is the server, not a runner's listener).
|
|
336
|
+
/** Each paired computer whose socket is in this hub, by its id: the socket the tick closes for a move. */
|
|
337
|
+
const home = new Map();
|
|
338
|
+
/** Each paired computer whose socket this server relays, by its id: the box's socket and the runner it goes to. */
|
|
339
|
+
const relayedPaired = new Map();
|
|
340
|
+
/** Each paired computer the tick closed for a move, and when: mid-redial until PAIRED_BRING_MS after, never refused
|
|
341
|
+
* as not attached meanwhile. */
|
|
342
|
+
const moving = new Map();
|
|
343
|
+
/** Each paired computer whose relayed dial is opening now. */
|
|
344
|
+
const dialing = new Set();
|
|
345
|
+
/** The paired computers (not removed) by id, with their names: read once at the first tick, then kept by this
|
|
346
|
+
* server's own pair and remove answers (only the server writes paired rows). */
|
|
347
|
+
let known;
|
|
348
|
+
/** The pair (its name) and remove (null) answers given while that first read is pending, by computer id: its snapshot
|
|
349
|
+
* may predate them (on Postgres a read and a write are separate connections), so they go over it before it is
|
|
350
|
+
* `known`. A read that starts after an answer's commit sees it, so an answer given while no read is pending needs no
|
|
351
|
+
* keeping, and a read that fails leaves the answers to the next one. */
|
|
352
|
+
let early;
|
|
353
|
+
/** Whether a tick is running: the interval does not wait on one, and a tick then does nothing. */
|
|
354
|
+
let ticking = false;
|
|
355
|
+
/** A pair (`name`) or a remove (null) answered: told to `known`, or kept for the first read while it is pending. */
|
|
356
|
+
function note(id, name) {
|
|
357
|
+
if (known === undefined)
|
|
358
|
+
return void early?.set(id, name);
|
|
359
|
+
if (name === null)
|
|
360
|
+
known.delete(id);
|
|
361
|
+
else
|
|
362
|
+
known.set(id, name);
|
|
363
|
+
}
|
|
364
|
+
/** The paired computers by id: `known`, or the first read, with the answers given meanwhile over its snapshot. */
|
|
365
|
+
async function knownNow() {
|
|
366
|
+
if (known !== undefined)
|
|
367
|
+
return known;
|
|
368
|
+
early = new Map();
|
|
369
|
+
try {
|
|
370
|
+
const read = new Map((await paired.list()).map((row) => [row.id, row.name]));
|
|
371
|
+
for (const [id, name] of early) {
|
|
372
|
+
if (name === null)
|
|
373
|
+
read.delete(id);
|
|
374
|
+
else
|
|
375
|
+
read.set(id, name);
|
|
376
|
+
}
|
|
377
|
+
// In the same step as the answers' merge, so none given after it misses `known`.
|
|
378
|
+
known = read;
|
|
379
|
+
return read;
|
|
380
|
+
}
|
|
381
|
+
finally {
|
|
382
|
+
early = undefined;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
/** Whether `route` is a run's of the runner beside this server, in this process (`beside`): the box's place is this
|
|
386
|
+
* hub. A route of an earlier server's runner beside it names another runner (a new name and incarnation at each
|
|
387
|
+
* start), so it is stale as any runner's that is gone. */
|
|
388
|
+
const besideRoute = (route) => options.beside !== undefined && route.runner === options.beside.runner && route.incarnation === options.beside.incarnation;
|
|
317
389
|
/** POST /v2/computers/pairings: a new code, good once until it expires. The store keeps its hash. */
|
|
318
390
|
async function createPairing() {
|
|
319
391
|
const code = newPairingCode();
|
|
320
392
|
const id = `pairing-${randomBytes(9).toString("base64url")}`;
|
|
321
393
|
const expiresAt = now() + PAIRING_CODE_TTL_MS;
|
|
322
394
|
await paired.addCode(id, sha256Hex(code), expiresAt);
|
|
323
|
-
return { id, code, expires_at: new Date(expiresAt).toISOString(), runs_refused: runsRefused };
|
|
395
|
+
return { id, code, expires_at: new Date(expiresAt).toISOString(), runs_refused: await runsRefused() };
|
|
324
396
|
}
|
|
325
397
|
/** POST /v2/computers/pair: the code traded for the computer's id and token, once; `address` the try's source. */
|
|
326
398
|
async function pair(body, address) {
|
|
@@ -341,14 +413,22 @@ export function createAttachRoutes(options) {
|
|
|
341
413
|
if (!made)
|
|
342
414
|
throw new ApiError("not_found", "no such pairing code: make a new one on the console's Computers page");
|
|
343
415
|
log("computer_paired", { computer: id, kind: hello.kind, os: hello.os, driver: hello.version });
|
|
344
|
-
|
|
416
|
+
note(id, name);
|
|
417
|
+
return { computer: id, token, runs_refused: await runsRefused() };
|
|
345
418
|
}
|
|
346
|
-
/** DELETE /v2/computers/{id}: its token revoked, a run's hold on it ended, its socket closed with 4401
|
|
419
|
+
/** DELETE /v2/computers/{id}: its token revoked, a run's hold on it ended, its socket closed with 4401 (a relayed one
|
|
420
|
+
* too, which the relay carries to the runner's hub), and its route gone. */
|
|
347
421
|
async function remove(id) {
|
|
348
422
|
const row = await paired.get(id);
|
|
349
423
|
if (row === undefined || row.revokedAt !== null || !(await paired.revoke(id)))
|
|
350
424
|
throw new ApiError("not_found", `no paired computer ${id}: GET /v2/computers lists them under paired`);
|
|
351
|
-
|
|
425
|
+
const gone = `${row.name} was removed from this server's console: pair it again to use it`;
|
|
426
|
+
hub.revoke(id, gone);
|
|
427
|
+
relayedPaired.get(id)?.ws.close(ATTACH_CLOSE.unauthorized, closeReason(gone));
|
|
428
|
+
note(id, null);
|
|
429
|
+
// Not in the revoke's transaction: a route left by a crash between the two names a revoked computer, whose token
|
|
430
|
+
// the upgrade refuses anyway, and the tick removes it.
|
|
431
|
+
await settings?.remove(routeKeyOf(id)).catch((error) => log("computer_route_unremoved", { computer: id, error: error instanceof Error ? error.message : String(error) }));
|
|
352
432
|
log("computer_removed", { computer: id });
|
|
353
433
|
}
|
|
354
434
|
/** A dial provider's tokens (computer package MachineTokens): a new one per create, kept as its SHA-256 by the machine
|
|
@@ -380,15 +460,27 @@ export function createAttachRoutes(options) {
|
|
|
380
460
|
field: "computer",
|
|
381
461
|
event: "computer",
|
|
382
462
|
gone: `${row.name} was removed from this server's console: pair it again to use it`,
|
|
383
|
-
|
|
463
|
+
moved: `${row.name} moved while it said hello: its route left this runner, and its redial goes where the route says`,
|
|
464
|
+
// Revoked first: a removal drops the route too. On a runner's listener the route must still name this runner and
|
|
465
|
+
// incarnation; one that left it (a cancelled bring, a run moved) is a move, never a revoke.
|
|
466
|
+
standing: async () => ((await paired.get(row.id))?.revokedAt !== null ? "revoked" : options.owner !== undefined && !(await routedHere(row.id)) ? "moved" : "good"),
|
|
384
467
|
seen: (hello) => paired.seen(row.id, hello),
|
|
385
468
|
});
|
|
469
|
+
/** Whether paired computer `id`'s route names this runner's listener (`owner`) and its incarnation. */
|
|
470
|
+
async function routedHere(id) {
|
|
471
|
+
if (options.owner === undefined || settings === undefined)
|
|
472
|
+
return false;
|
|
473
|
+
const { route } = await seeRoute(settings, id);
|
|
474
|
+
return route.runner === options.owner.runner && route.incarnation === options.owner.incarnation;
|
|
475
|
+
}
|
|
386
476
|
const machineDialer = (row) => ({
|
|
387
477
|
computer: mintedComputer(row.machine, { kind: row.kind, os: row.os, arch: row.arch }),
|
|
388
478
|
field: "machine",
|
|
389
479
|
event: "machine",
|
|
390
480
|
gone: machineGone(row.machine),
|
|
391
|
-
|
|
481
|
+
// A minted machine has no route, so it never moves: good or revoked.
|
|
482
|
+
moved: machineGone(row.machine),
|
|
483
|
+
standing: async () => ((await minted.get(row.machine))?.revokedAt === null ? "good" : "revoked"),
|
|
392
484
|
seen: async () => undefined,
|
|
393
485
|
});
|
|
394
486
|
/** The minted machine whose good token `authorization` bears (Bearer), or undefined. */
|
|
@@ -399,34 +491,86 @@ export function createAttachRoutes(options) {
|
|
|
399
491
|
/** Whether the socket here takes `row`'s machine: on a runner's listener (owner), only a token that runner minted in
|
|
400
492
|
* this incarnation; on the server, one its own process minted (another runner's is relayed, `relayDial`). */
|
|
401
493
|
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) {
|
|
494
|
+
/** A minted machine's dial to the runner that minted it, as relayDial takes it. */
|
|
495
|
+
const machineTarget = (row, bearer) => {
|
|
408
496
|
const runner = row.runner;
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
497
|
+
return {
|
|
498
|
+
runner, incarnation: row.incarnation, token: { authorization: `Bearer ${bearer}` }, subject: dialOf(row), whose: "made it",
|
|
499
|
+
gone: "its run makes a new machine at its next claim",
|
|
500
|
+
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`,
|
|
501
|
+
event: "machine", named: { ...(row.run !== null ? { run: row.run } : {}), machine: row.machine, runner },
|
|
413
502
|
// Kept on the token's row, where the runner's create waiting for this machine reads it (MachineTokens refused).
|
|
414
|
-
|
|
415
|
-
|
|
503
|
+
keep: (words) => minted.refuse(sha256Hex(bearer), words),
|
|
504
|
+
};
|
|
505
|
+
};
|
|
506
|
+
/** A paired computer's dial to the runner its route names (`seen`: the route's row as read), as relayDial takes it. A
|
|
507
|
+
* 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
|
|
508
|
+
* 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
|
|
509
|
+
* once in its words. */
|
|
510
|
+
const pairedTarget = (row, route, seen, bearer) => {
|
|
511
|
+
const runner = route.runner;
|
|
512
|
+
return {
|
|
513
|
+
runner, incarnation: route.incarnation, token: { [RELAY_TOKEN_HEADER]: sha256Hex(bearer) }, subject: `${row.name} (${row.id}) for run ${route.run}`, whose: "holds it",
|
|
514
|
+
gone: ROUTE_COMES_BACK,
|
|
515
|
+
restarted: routeRestartedWords(runner),
|
|
516
|
+
event: "computer", named: { run: route.run, computer: row.id, runner },
|
|
517
|
+
// Read again and tried again while the row still holds this route (the tick may have noted it meanwhile).
|
|
518
|
+
keep: async (words, stale) => {
|
|
519
|
+
let was = seen;
|
|
520
|
+
let current = route;
|
|
521
|
+
for (let tried = 0; tried < 5; tried++) {
|
|
522
|
+
if (await replaceRoute(settings, row.id, was, stale ? { ...EMPTY_ROUTE, refused: words } : { ...current, refused: words }))
|
|
523
|
+
return true;
|
|
524
|
+
const again = await seeRoute(settings, row.id);
|
|
525
|
+
if (again.route.runner !== route.runner || again.route.incarnation !== route.incarnation || again.route.run !== route.run)
|
|
526
|
+
return false;
|
|
527
|
+
({ seen: was, route: current } = again);
|
|
528
|
+
}
|
|
529
|
+
return false;
|
|
530
|
+
},
|
|
531
|
+
joined: (ws) => {
|
|
532
|
+
dialing.delete(row.id);
|
|
533
|
+
relayedPaired.set(row.id, { ws, runner, incarnation: route.incarnation });
|
|
534
|
+
ws.once("close", () => {
|
|
535
|
+
if (relayedPaired.get(row.id)?.ws === ws)
|
|
536
|
+
relayedPaired.delete(row.id);
|
|
537
|
+
});
|
|
538
|
+
// A note an earlier dial left on the route (not attached now) goes, now that the box is there.
|
|
539
|
+
if (route.refused !== null)
|
|
540
|
+
void replaceRoute(settings, row.id, seen, { ...route, refused: null }).catch(() => undefined);
|
|
541
|
+
},
|
|
542
|
+
};
|
|
543
|
+
};
|
|
544
|
+
/** The server's relay of a dial (its token `bearer`) to the runner its row names (`target`): refused 503 in words that
|
|
545
|
+
* name the dial (its run, then the machine or the computer), never its token, where that runner keeps no fresh
|
|
546
|
+
* listener, restarted since, answers at a host this server does not dial, or does not take it; else the box's socket
|
|
547
|
+
* is joined to one opened to the runner's listener with the proxy's proof for this token, runner and incarnation, each
|
|
548
|
+
* with this module's limits. The token goes on that hop as `target.token` names it: a paired box's never as itself. */
|
|
549
|
+
async function relayDial(target, bearer, req, socket, head, deny) {
|
|
550
|
+
const { runner, named, event } = target;
|
|
551
|
+
// Kept before the answer, so whoever reads the row after the 503 (the waiting create, the box's next dial) reads it.
|
|
552
|
+
// The box and the row get `told` where the words name where the runner answers (an internal host and port, a
|
|
553
|
+
// socket's error): the row's words reach a person too (a paired box's hold note, the waiting claim's failure, a
|
|
554
|
+
// minted machine's create), so those stay in the log line alone.
|
|
555
|
+
const refuse = async (why, stale = false, told = why) => {
|
|
556
|
+
const said = `${target.subject}: ${told}`;
|
|
557
|
+
log(`${event}_relay_refused`, { ...named, why: `${target.subject}: ${why}` });
|
|
558
|
+
await target.keep(said, stale).catch((error) => log(`${event}_relay_refusal_unkept`, { ...named, error: error instanceof Error ? error.message : String(error) }));
|
|
559
|
+
deny("503 Service Unavailable", said);
|
|
416
560
|
};
|
|
417
561
|
if (options.relay === undefined)
|
|
418
|
-
return refuse(`runner ${runner}
|
|
562
|
+
return refuse(`runner ${runner} ${target.whose}, and this server relays no dial to a runner`);
|
|
419
563
|
const at = (await liveRelays(options.relay.runners)).find((row) => row.runner === runner);
|
|
420
564
|
if (at === undefined)
|
|
421
|
-
return refuse(`runner ${runner}, which
|
|
422
|
-
if (at.incarnation !==
|
|
423
|
-
return refuse(
|
|
565
|
+
return refuse(`runner ${runner}, which ${target.whose}, is not running now (no attach listener of its has said it is there for ${RUNNER_STALE_MS / 1000} s): ${target.gone}`, true);
|
|
566
|
+
if (at.incarnation !== target.incarnation)
|
|
567
|
+
return refuse(target.restarted, true);
|
|
424
568
|
const host = dialHost(at.host, options.relay.reach);
|
|
425
569
|
if (host === undefined)
|
|
426
|
-
return refuse(`runner ${runner}, which
|
|
570
|
+
return refuse(`runner ${runner}, which ${target.whose}, answers at ${at.host}, which is not one of this server's runners' hosts (AGENTRUN_RUNNER_HOSTS)`, false, `runner ${runner}, which ${target.whose}, answers at a host this server does not dial (AGENTRUN_RUNNER_HOSTS)`);
|
|
427
571
|
const proof = relayProof(options.relay.reach, { tokenSha256: sha256Hex(bearer), runner, incarnation: at.incarnation });
|
|
428
572
|
const upstream = new WebSocket(`ws://${host}:${at.port}${ATTACH_PATH}`, ATTACH_SUBPROTOCOL, {
|
|
429
|
-
headers: {
|
|
573
|
+
headers: { ...target.token, [LIVE_PROOF_HEADER]: proof },
|
|
430
574
|
maxPayload: MAX_LINE_BYTES, perMessageDeflate: false, followRedirects: false, handshakeTimeout: RELAY_CONNECT_MS,
|
|
431
575
|
});
|
|
432
576
|
let settled = false;
|
|
@@ -434,14 +578,14 @@ export function createAttachRoutes(options) {
|
|
|
434
578
|
settled = true;
|
|
435
579
|
res.resume();
|
|
436
580
|
upstream.terminate();
|
|
437
|
-
refuse(`runner ${runner}, which
|
|
581
|
+
void refuse(`runner ${runner}, which ${target.whose}, did not take it (HTTP ${res.statusCode})`);
|
|
438
582
|
});
|
|
439
583
|
upstream.on("error", (error) => {
|
|
440
584
|
if (settled)
|
|
441
585
|
return;
|
|
442
586
|
settled = true;
|
|
443
587
|
// The library's words name the address and the step, never a header.
|
|
444
|
-
refuse(`runner ${runner}, which
|
|
588
|
+
void refuse(`runner ${runner}, which ${target.whose}, did not answer at ${host}:${at.port} (${error.message})`, false, `runner ${runner}, which ${target.whose}, did not answer`);
|
|
445
589
|
});
|
|
446
590
|
upstream.once("open", () => {
|
|
447
591
|
settled = true;
|
|
@@ -455,10 +599,25 @@ export function createAttachRoutes(options) {
|
|
|
455
599
|
one.once("close", () => sockets.delete(one));
|
|
456
600
|
}
|
|
457
601
|
joinSockets(ws, upstream, options.timing);
|
|
458
|
-
|
|
602
|
+
target.joined?.(ws);
|
|
603
|
+
log(`${event}_relayed`, named);
|
|
459
604
|
});
|
|
460
605
|
});
|
|
461
606
|
}
|
|
607
|
+
/** A paired computer's dial on the server, its route read (`seen`, `route`): relayed to the runner the route names,
|
|
608
|
+
* marked as dialing (the tick refuses no box mid-dial) until the relay is up or refused. */
|
|
609
|
+
async function relayPaired(row, route, seen, bearer, req, socket, head, deny) {
|
|
610
|
+
dialing.add(row.id);
|
|
611
|
+
const done = () => void dialing.delete(row.id);
|
|
612
|
+
socket.once("close", done);
|
|
613
|
+
try {
|
|
614
|
+
await relayDial(pairedTarget(row, route, seen, bearer), bearer, req, socket, head, (status, words) => (done(), deny(status, words)));
|
|
615
|
+
}
|
|
616
|
+
catch (error) {
|
|
617
|
+
done();
|
|
618
|
+
throw error;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
462
621
|
/** GET /v2/computers/daemon?arch=: the path of this server's computerd build for `arch`, for the minted machine whose
|
|
463
622
|
* token `authorization` bears. 401 for a token no machine has good (unknown, revoked or past its end); 429 past
|
|
464
623
|
* DAEMON_FETCHES_PER_MINUTE a minute by one machine; 422 for an arch with no build. */
|
|
@@ -474,15 +633,24 @@ export function createAttachRoutes(options) {
|
|
|
474
633
|
return hostedDaemonPath(arch);
|
|
475
634
|
}
|
|
476
635
|
/** GET /v2/computers/attach's upgrade: the token's paired computer or minted machine, then the protocol, then the hub's
|
|
477
|
-
* hello. A refusal is an HTTP answer before the upgrade (401 for a token neither has good, 426
|
|
478
|
-
*
|
|
479
|
-
|
|
636
|
+
* hello. A refusal is an HTTP answer before the upgrade (401 for a token neither has good, 426 when no
|
|
637
|
+
* subprotocol it asks for names a protocol this server takes, in words naming the side to update: upgradeRefusal). A
|
|
638
|
+
* minted machine's socket goes to the hub under its name.
|
|
639
|
+
*
|
|
640
|
+
* On a runner's listener (`owner`), a paired computer's token comes as `hop`, its SHA-256 alone as the server's
|
|
641
|
+
* relay hop names it (dial-relay.ts reads it off RELAY_TOKEN_HEADER, its proof checked): a paired token as a bearer
|
|
642
|
+
* is one it does not know, and a hash finds no minted machine (whose token comes as its bearer). */
|
|
643
|
+
function upgrade(req, socket, head, deny, hop) {
|
|
480
644
|
void (async () => {
|
|
481
645
|
if (closing)
|
|
482
646
|
return deny("503 Service Unavailable", "this server is stopping");
|
|
483
647
|
const bearer = bearerOf(req.headers.authorization);
|
|
484
|
-
const
|
|
485
|
-
const
|
|
648
|
+
const pairedSha256 = options.owner ? hop?.tokenSha256 : bearer?.startsWith(ATTACH_TOKEN_PREFIX) ? sha256Hex(bearer) : undefined;
|
|
649
|
+
const found = !options.machinesOnly && pairedSha256 !== undefined ? await paired.byToken(pairedSha256) : undefined;
|
|
650
|
+
// On a runner's listener, a paired computer's token is taken only while its route names this runner and incarnation:
|
|
651
|
+
// any other is one it does not know.
|
|
652
|
+
const row = found !== undefined && options.owner !== undefined && !(await routedHere(found.id)) ? undefined : found;
|
|
653
|
+
const machine = found === undefined && hop === undefined ? await machineBy(req.headers.authorization) : undefined;
|
|
486
654
|
// Another runner's machine, on the server: relayed to that runner's listener below. On a runner's listener, a token
|
|
487
655
|
// of any other's (another runner's, another incarnation's, the server's) is one it does not know.
|
|
488
656
|
const relayed = machine !== undefined && !options.owner && machine.runner !== null;
|
|
@@ -490,30 +658,52 @@ export function createAttachRoutes(options) {
|
|
|
490
658
|
if (dialer === undefined)
|
|
491
659
|
return deny("401 Unauthorized", options.machinesOnly ? MACHINE_TOKEN_REFUSED : TOKEN_REFUSED, { "www-authenticate": "Bearer" });
|
|
492
660
|
const asked = String(req.headers["sec-websocket-protocol"] ?? "").split(",").map((one) => one.trim()).filter(Boolean);
|
|
493
|
-
if (
|
|
494
|
-
return deny("426 Upgrade Required",
|
|
495
|
-
|
|
661
|
+
if (attachSubprotocolOf(asked) === undefined)
|
|
662
|
+
return deny("426 Upgrade Required", upgradeRefusal(dialer.computer, asked), { "sec-websocket-protocol": ATTACH_SUBPROTOCOL });
|
|
663
|
+
// A paired computer on the server: its route says where its socket goes (no route, or a run's of the runner beside
|
|
664
|
+
// this server: this hub).
|
|
665
|
+
const routed = row !== undefined && !options.owner && settings !== undefined ? await seeRoute(settings, row.id) : undefined;
|
|
496
666
|
if (socket.destroyed || closing)
|
|
497
667
|
return void socket.destroy();
|
|
498
668
|
if (relayed)
|
|
499
|
-
return relayDial(machine, bearer, req, socket, head, deny);
|
|
669
|
+
return relayDial(machineTarget(machine, bearer), bearer, req, socket, head, deny);
|
|
670
|
+
if (row !== undefined && routed !== undefined && routed.route.runner !== null && !besideRoute(routed.route))
|
|
671
|
+
return relayPaired(row, routed.route, routed.seen, bearer, req, socket, head, deny);
|
|
500
672
|
wss.handleUpgrade(req, socket, head, (ws) => {
|
|
501
673
|
sockets.add(ws);
|
|
502
674
|
ws.once("close", () => sockets.delete(ws));
|
|
503
675
|
const attach = attachSocketOf(ws, options.timing);
|
|
504
676
|
const { computer, field, event } = dialer;
|
|
505
677
|
options.onSocket?.(computer.id, ws);
|
|
678
|
+
if (routed !== undefined) {
|
|
679
|
+
home.set(computer.id, ws);
|
|
680
|
+
ws.once("close", () => {
|
|
681
|
+
if (home.get(computer.id) === ws)
|
|
682
|
+
home.delete(computer.id);
|
|
683
|
+
});
|
|
684
|
+
}
|
|
506
685
|
void hub.accept(computer, attach).then(async (said) => {
|
|
507
686
|
if (!said.ok)
|
|
508
687
|
return log(`${event}_refused`, { [field]: computer.id, code: said.error.code, why: said.error.message });
|
|
509
688
|
// The row read for the token may be older than a removal that committed, and its hub.revoke, while this socket
|
|
510
689
|
// was still being accepted (a Postgres read and write are separate connections): read the row again, now that the
|
|
511
690
|
// socket is the hub's. A removal committing after this read revokes the hub's socket after this accept, closing it.
|
|
512
|
-
|
|
691
|
+
// A route that left this runner meanwhile (only a runner's listener reads one here) is a move, as the server's
|
|
692
|
+
// tick closes one: this socket alone closes with 1001 and its driver redials; the pairing is untouched.
|
|
693
|
+
const standing = await dialer.standing();
|
|
694
|
+
if (standing === "revoked") {
|
|
513
695
|
hub.revoke(computer.id, dialer.gone);
|
|
514
696
|
return log(`${event}_refused`, { [field]: computer.id, why: "removed while it attached" });
|
|
515
697
|
}
|
|
698
|
+
if (standing === "moved") {
|
|
699
|
+
ws.close(ATTACH_CLOSE.going, closeReason(dialer.moved));
|
|
700
|
+
return log(`${event}_refused`, { [field]: computer.id, why: "its route left this runner while it attached" });
|
|
701
|
+
}
|
|
516
702
|
await dialer.seen(said.value);
|
|
703
|
+
// The note a refused or dropped route left (routed.route.refused) goes, now that the box is here; a route of the
|
|
704
|
+
// runner beside this server stays, its note alone gone.
|
|
705
|
+
if (routed !== undefined && routed.route.refused !== null)
|
|
706
|
+
await replaceRoute(settings, computer.id, routed.seen, { ...routed.route, refused: null }).catch(() => undefined);
|
|
517
707
|
log(`${event}_attached`, { [field]: computer.id, driver: said.value.version });
|
|
518
708
|
}).catch((error) => {
|
|
519
709
|
log(`${event}_refused`, { [field]: computer.id, why: error instanceof Error ? error.message : String(error) });
|
|
@@ -522,6 +712,80 @@ export function createAttachRoutes(options) {
|
|
|
522
712
|
});
|
|
523
713
|
})().catch(() => deny("500 Internal Server Error"));
|
|
524
714
|
}
|
|
715
|
+
/** The runner paired computer `id`'s socket is relayed to now, or undefined (in this hub, or attached nowhere). */
|
|
716
|
+
const relayedTo = (id) => relayedPaired.get(id)?.runner;
|
|
717
|
+
/** Closes a paired computer's socket with 1001 for a move, saying why, and notes it as moving: its driver redials, and
|
|
718
|
+
* the upgrade sends it where its route says. */
|
|
719
|
+
const move = (id, ws, why) => {
|
|
720
|
+
moving.set(id, now());
|
|
721
|
+
ws.close(ATTACH_CLOSE.going, closeReason(why));
|
|
722
|
+
};
|
|
723
|
+
/** The server's route tick (every PAIRED_ROUTE_MS, server.ts, its runner apart or beside it): one read of every route
|
|
724
|
+
* row, compared with where each box's socket is now (this hub, or the runner it is relayed to, in this process's
|
|
725
|
+
* memory). A route of the runner beside this server (besideRoute) says this hub. A socket in the wrong place (here
|
|
726
|
+
* with a runner apart's route, relayed to a runner its route no longer names, relayed with no route or with the
|
|
727
|
+
* runner beside's) is closed with 1001, its redial routed; a box routed and attached nowhere, neither moving nor
|
|
728
|
+
* 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
|
|
729
|
+
* computer that is no longer paired, goes. Nothing while no computer is paired; with this server's runner beside it
|
|
730
|
+
* (not runnerApart), nothing past that one read while no route row is there and no box is relayed. It judges no
|
|
731
|
+
* runner's liveness: a stale route is dropped at the box's next upgrade. One tick runs at a time: one called while
|
|
732
|
+
* another runs does nothing. */
|
|
733
|
+
async function tick() {
|
|
734
|
+
if (ticking)
|
|
735
|
+
return;
|
|
736
|
+
ticking = true;
|
|
737
|
+
try {
|
|
738
|
+
await judgeRoutes();
|
|
739
|
+
}
|
|
740
|
+
finally {
|
|
741
|
+
ticking = false;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
/** One tick's work (tick). */
|
|
745
|
+
async function judgeRoutes() {
|
|
746
|
+
if (options.owner !== undefined || settings === undefined || closing)
|
|
747
|
+
return;
|
|
748
|
+
const known = await knownNow();
|
|
749
|
+
if (known.size === 0 && relayedPaired.size === 0)
|
|
750
|
+
return;
|
|
751
|
+
const rows = await allRoutes(settings);
|
|
752
|
+
// With its runner beside it, only a runner apart on another host takes a route: the read says whether one did. A
|
|
753
|
+
// box still relayed counts too, since the drop that ended its route (with no note) removed the row.
|
|
754
|
+
if (options.runnerApart !== true && rows.length === 0 && relayedPaired.size === 0)
|
|
755
|
+
return;
|
|
756
|
+
const at = now();
|
|
757
|
+
for (const [id, since] of moving)
|
|
758
|
+
if (at - since > PAIRED_BRING_MS)
|
|
759
|
+
moving.delete(id);
|
|
760
|
+
const routes = new Map(rows.map((row) => [row.id, row.route]));
|
|
761
|
+
for (const [id, relayed] of relayedPaired) {
|
|
762
|
+
const route = routes.get(id);
|
|
763
|
+
if (route === undefined || route.runner === null)
|
|
764
|
+
move(id, relayed.ws, `${known.get(id) ?? id} came back to this server: no run holds it now`);
|
|
765
|
+
else if (besideRoute(route))
|
|
766
|
+
move(id, relayed.ws, `${known.get(id) ?? id} came back to this server: run ${route.run} holds it here`);
|
|
767
|
+
else if (route.runner !== relayed.runner || route.incarnation !== relayed.incarnation)
|
|
768
|
+
move(id, relayed.ws, `${known.get(id) ?? id} moved to the runner that holds run ${route.run}`);
|
|
769
|
+
}
|
|
770
|
+
for (const { id, seen, route } of rows) {
|
|
771
|
+
const name = known.get(id);
|
|
772
|
+
if (name === undefined || (route.runner === null && route.refused === null)) {
|
|
773
|
+
await replaceRoute(settings, id, seen, EMPTY_ROUTE).catch(() => undefined);
|
|
774
|
+
continue;
|
|
775
|
+
}
|
|
776
|
+
if (route.runner === null)
|
|
777
|
+
continue;
|
|
778
|
+
const here = home.get(id);
|
|
779
|
+
// A box in this hub under a run of the runner beside this server is where its route says: it stays.
|
|
780
|
+
if (here !== undefined && besideRoute(route))
|
|
781
|
+
continue;
|
|
782
|
+
if (here !== undefined)
|
|
783
|
+
move(id, here, `${name} moved to the runner that holds run ${route.run}`);
|
|
784
|
+
else if (!relayedPaired.has(id) && !moving.has(id) && !dialing.has(id) && route.refused === null) {
|
|
785
|
+
await replaceRoute(settings, id, seen, { ...route, refused: notAttachedWords(name) }).catch(() => undefined);
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
}
|
|
525
789
|
/** Every attach socket closed with 1001 (the server is going), and no new one taken; resolves once each has ended. A
|
|
526
790
|
* peer that does not answer the close within a second is cut, so the server's own close never waits on it. */
|
|
527
791
|
async function close() {
|
|
@@ -534,5 +798,5 @@ export function createAttachRoutes(options) {
|
|
|
534
798
|
await Promise.all(ended);
|
|
535
799
|
clearTimeout(cut);
|
|
536
800
|
}
|
|
537
|
-
return { createPairing, pair, remove, upgrade, close, machineTokens, daemon };
|
|
801
|
+
return { createPairing, pair, remove, upgrade, close, machineTokens, daemon, tick, relayedTo, runsRefused };
|
|
538
802
|
}
|
|
@@ -21,7 +21,7 @@ export declare function heldField(store: HeldStore, runId: string, options?: Hel
|
|
|
21
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
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" | "settings">, runId: string, view: Record<string, unknown>, options?: HeldOptions): Promise<{
|
|
24
|
+
export declare function runComputerRead(store: HeldStore & Pick<Store, "agents" | "settings" | "sealedKeys">, runId: string, view: Record<string, unknown>, options?: HeldOptions): Promise<{
|
|
25
25
|
computer?: Record<string, unknown>;
|
|
26
26
|
}>;
|
|
27
27
|
/**
|
|
@@ -40,12 +40,17 @@ export async function heldField(store, runId, options = {}) {
|
|
|
40
40
|
return { state, minutes: read.minutes, ...(state === "released" ? {} : { until: new Date(read.until).toISOString() }), ...(read.why ? { why: read.why } : {}) };
|
|
41
41
|
}
|
|
42
42
|
/** Whether parked run `runId`'s computer of `kind` was let go: a park's claim releases it but for a desktop, a phone or a
|
|
43
|
-
* sandbox kept for the resume, until its keep's time (workspace/computer-kept.ts). A browser's is said on its own read.
|
|
44
|
-
|
|
43
|
+
* sandbox kept for the resume, until its keep's time (workspace/computer-kept.ts). A browser's is said on its own read.
|
|
44
|
+
* The keep's `until` was stamped on the store's clock (Kept.until), so its time is judged on it, as the run's computer
|
|
45
|
+
* view judges it (server.ts getRunComputer); a clock that can't be read judges no time, as that view does. */
|
|
46
|
+
async function releasedAtPark(store, runId, kind) {
|
|
45
47
|
if (kind === "browser")
|
|
46
48
|
return false;
|
|
47
49
|
const kept = await readKept(store.settings, runId).catch(() => undefined);
|
|
48
|
-
|
|
50
|
+
if (!kept || kept.kind !== kind || kept.ended === true || kept.ending === true)
|
|
51
|
+
return true;
|
|
52
|
+
const at = await store.sealedKeys.clock().catch(() => undefined);
|
|
53
|
+
return at !== undefined && at >= kept.until;
|
|
49
54
|
}
|
|
50
55
|
/** The run read's `computer` field (`{ computer }`, or nothing for a run with none): its revision's computer setting, else
|
|
51
56
|
* 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
|
|
@@ -59,7 +64,7 @@ export async function runComputerRead(store, runId, view, options = {}) {
|
|
|
59
64
|
const error = view.error;
|
|
60
65
|
// A stop lets the run's computer go (the run's row is final until a follow-up queues it again); a park keeps a desktop,
|
|
61
66
|
// a phone or a sandbox only for its keep's time.
|
|
62
|
-
const released = run?.state === "stopped" || (run?.state === "parked" && (await releasedAtPark(store, runId, setting.kind
|
|
67
|
+
const released = run?.state === "stopped" || (run?.state === "parked" && (await releasedAtPark(store, runId, setting.kind)));
|
|
63
68
|
const read = { id: runId, status: String(view.status), ...(error && typeof error.code === "string" ? { error: { code: error.code, message: String(error.message ?? "") } } : {}), ...(released ? { released } : {}) };
|
|
64
69
|
// The run's journal, read at most once for the whole field (the hold and `held` both read it), and only when one needs
|
|
65
70
|
// it: a long run's every poll would pay for a read that grows with it.
|