machine-bridge-mcp 2.0.0 → 3.0.0-beta.102
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 +1142 -0
- package/CONTRIBUTING.md +23 -25
- package/GOVERNANCE.md +2 -2
- package/README.md +58 -20
- package/SECURITY.md +161 -120
- package/SUPPORT.md +1 -1
- package/browser-extension/broker-auth.js +83 -0
- package/browser-extension/broker-liveness.js +35 -0
- package/browser-extension/browser-error-boundary.js +96 -0
- package/browser-extension/browser-operations.js +1456 -101
- package/browser-extension/devtools-input.js +195 -34
- package/browser-extension/devtools-observation.js +429 -0
- package/browser-extension/devtools-session.js +48 -0
- package/browser-extension/manifest.json +4 -3
- package/browser-extension/page-automation.js +578 -118
- package/browser-extension/pairing-bootstrap.js +42 -0
- package/browser-extension/pairing.js +28 -12
- package/browser-extension/service-worker.js +126 -106
- package/docs/AGENT_CONTEXT.md +13 -10
- package/docs/ARCHITECTURE.md +159 -67
- package/docs/AUDIT.md +1302 -4
- package/docs/CLIENTS.md +19 -6
- package/docs/COMPUTER_USE.md +297 -0
- package/docs/ENGINEERING.md +72 -13
- package/docs/GETTING_STARTED.md +18 -20
- package/docs/LOCAL_AUTHORIZATION.md +115 -72
- package/docs/LOCAL_AUTOMATION.md +27 -21
- package/docs/LOGGING.md +40 -19
- package/docs/MANAGED_JOBS.md +37 -34
- package/docs/MULTI_ACCOUNT.md +98 -37
- package/docs/OPERATIONS.md +145 -51
- package/docs/OVERVIEW.md +14 -11
- package/docs/POLICY_REFERENCE.md +71 -3
- package/docs/PRIVACY.md +41 -2
- package/docs/PROJECT_STANDARDS.md +29 -6
- package/docs/RELEASING.md +238 -64
- package/docs/TESTING.md +150 -39
- package/docs/THREAT_MODEL.md +164 -67
- package/docs/TOOL_REFERENCE.md +653 -60
- package/docs/UPGRADING.md +78 -18
- package/native/macos/MachineBridgeBackgroundInput.swift +1051 -0
- package/native/macos/MachineBridgeBackgroundInputSmokeFixture.swift +295 -0
- package/native/macos/MachineBridgeTrustBroker.swift +233 -0
- package/package.json +94 -16
- package/scripts/accepted-candidate-tarball.mjs +77 -0
- package/scripts/candidate-runtime-store.mjs +93 -0
- package/scripts/check-plan.mjs +73 -3
- package/scripts/check-runner.mjs +129 -17
- package/scripts/commit-message-check.mjs +5 -2
- package/scripts/consumer-package-security.mjs +293 -0
- package/scripts/coverage-check.mjs +251 -15
- package/scripts/coverage-generation.mjs +41 -0
- package/scripts/coverage-range-merge.mjs +41 -0
- package/scripts/foreground-daemon-recovery.mjs +88 -0
- package/scripts/generate-policy-reference.mjs +28 -5
- package/scripts/generate-tool-reference.mjs +1 -1
- package/scripts/generate-worker-types.mjs +21 -11
- package/scripts/github-backlog.mjs +11 -10
- package/scripts/github-push.mjs +62 -16
- package/scripts/github-release-asset.mjs +58 -0
- package/scripts/github-release.mjs +241 -132
- package/scripts/global-package-installation.mjs +68 -0
- package/scripts/hardened-npm-session.mjs +42 -0
- package/scripts/install-published-prerelease.mjs +198 -0
- package/scripts/local-release-acceptance.mjs +103 -45
- package/scripts/macos-background-input-smoke.mjs +515 -0
- package/scripts/network-retry.mjs +8 -1
- package/scripts/npm-global-prefix.mjs +27 -0
- package/scripts/npm-publication-policy.mjs +17 -0
- package/scripts/official-mcp-conformance.mjs +267 -0
- package/scripts/persistent-activation-process.mjs +101 -0
- package/scripts/prepare-pinned-npm.mjs +12 -69
- package/scripts/prerelease-activation.mjs +160 -0
- package/scripts/privacy-check.mjs +14 -6
- package/scripts/promotion-digest.mjs +128 -0
- package/scripts/publish-npm.mjs +260 -0
- package/scripts/published-release.mjs +132 -0
- package/scripts/release-acceptance.mjs +62 -54
- package/scripts/release-browser-extension-store.mjs +172 -0
- package/scripts/release-candidate-manifest.mjs +52 -0
- package/scripts/release-channel.mjs +106 -0
- package/scripts/release-diagnostic.mjs +41 -0
- package/scripts/release-impact-check.mjs +12 -22
- package/scripts/release-oauth-canary-core.mjs +325 -0
- package/scripts/release-oauth-canary-evidence.mjs +100 -0
- package/scripts/release-oauth-canary.mjs +127 -0
- package/scripts/release-publication-guard.mjs +67 -0
- package/scripts/release-soak.mjs +284 -0
- package/scripts/release-state.mjs +1 -1
- package/scripts/run-checks.mjs +44 -7
- package/scripts/run-worker-dry-run.mjs +21 -0
- package/scripts/sarif-security-gate.mjs +2 -1
- package/scripts/sbom-check.mjs +106 -0
- package/scripts/start-release-candidate.mjs +267 -44
- package/scripts/syntax-check.mjs +36 -14
- package/scripts/verification-generation-guard.mjs +15 -0
- package/scripts/verification-idle-sleep-guard.mjs +37 -0
- package/scripts/verification-state.mjs +108 -0
- package/scripts/wrangler-command-lifecycle.mjs +115 -0
- package/src/local/account-access.mjs +13 -1
- package/src/local/account-admin-response.mjs +92 -0
- package/src/local/account-admin.mjs +52 -19
- package/src/local/agent-context-projection.mjs +26 -7
- package/src/local/agent-context.mjs +88 -49
- package/src/local/agent-skill-discovery.mjs +16 -5
- package/src/local/app-automation-macos-jxa.mjs +350 -0
- package/src/local/app-automation.mjs +873 -163
- package/src/local/application-capability-projection.mjs +24 -0
- package/src/local/authority-context.mjs +106 -0
- package/src/local/autostart-log-maintenance.mjs +36 -0
- package/src/local/browser-bridge-http.mjs +5 -3
- package/src/local/browser-bridge.mjs +109 -31
- package/src/local/browser-broker-auth-http.mjs +67 -0
- package/src/local/browser-broker-auth.mjs +98 -0
- package/src/local/browser-broker-routes.mjs +63 -20
- package/src/local/browser-broker-server.mjs +17 -7
- package/src/local/browser-command.mjs +79 -30
- package/src/local/browser-computer-observation-service.mjs +266 -0
- package/src/local/browser-extension-identity.mjs +50 -0
- package/src/local/browser-extension-path.mjs +22 -0
- package/src/local/browser-extension-protocol.mjs +50 -11
- package/src/local/browser-operation-service.mjs +147 -117
- package/src/local/browser-pairing-grant.mjs +109 -0
- package/src/local/browser-pairing-http.mjs +36 -0
- package/src/local/browser-pairing-launch.mjs +58 -0
- package/src/local/browser-pairing-store.mjs +85 -48
- package/src/local/browser-request-registry.mjs +40 -14
- package/src/local/browser-request-settlement.mjs +78 -0
- package/src/local/browser-resource-input.mjs +74 -0
- package/src/local/browser-trusted-input-health.mjs +63 -0
- package/src/local/call-authority.mjs +21 -0
- package/src/local/call-capacity.mjs +45 -0
- package/src/local/call-registry-drain.mjs +54 -0
- package/src/local/call-registry.mjs +30 -15
- package/src/local/capability-observer.mjs +5 -0
- package/src/local/capability-ranking.mjs +3 -2
- package/src/local/child-process-settlement.mjs +132 -0
- package/src/local/cli-account-admin.mjs +40 -6
- package/src/local/cli-activate.mjs +162 -0
- package/src/local/cli-local-admin.mjs +20 -45
- package/src/local/cli-options.mjs +12 -13
- package/src/local/cli-ready-output.mjs +52 -0
- package/src/local/cli-service.mjs +91 -11
- package/src/local/cli.mjs +232 -150
- package/src/local/computer-use-application-observation.mjs +224 -0
- package/src/local/computer-use-arguments.mjs +270 -0
- package/src/local/computer-use-deadline.mjs +50 -0
- package/src/local/computer-use-dispatch-settlement.mjs +40 -0
- package/src/local/computer-use-expectation.mjs +171 -0
- package/src/local/computer-use-observation.mjs +769 -0
- package/src/local/computer-use-recovery.mjs +82 -0
- package/src/local/computer-use-result-budget.mjs +145 -0
- package/src/local/computer-use-snapshot-store.mjs +67 -0
- package/src/local/computer-use.mjs +2255 -0
- package/src/local/daemon-http-relay-auth.mjs +26 -0
- package/src/local/daemon-http-relay-connection.mjs +298 -0
- package/src/local/daemon-http-relay-request.mjs +64 -0
- package/src/local/daemon-http-relay-sequence.mjs +49 -0
- package/src/local/daemon-process.mjs +40 -7
- package/src/local/delegated-process-sandbox.mjs +148 -0
- package/src/local/device-identity.mjs +156 -49
- package/src/local/device-root-provider.mjs +81 -0
- package/src/local/directory-metadata.mjs +47 -0
- package/src/local/doctor-reporting.mjs +23 -0
- package/src/local/durable-process-spec.mjs +80 -0
- package/src/local/errors.mjs +25 -15
- package/src/local/exclusive-file.mjs +74 -26
- package/src/local/execution-limits.mjs +9 -0
- package/src/local/execution-routing.mjs +276 -0
- package/src/local/execution-surface.mjs +23 -0
- package/src/local/file-mutation-coordinator.mjs +47 -0
- package/src/local/file-snapshot-preservation.mjs +24 -0
- package/src/local/filesystem-identity.mjs +54 -0
- package/src/local/fixed-process-environment.mjs +21 -0
- package/src/local/full-access-test.mjs +31 -13
- package/src/local/git-commit.mjs +69 -0
- package/src/local/git-config-safety.mjs +55 -0
- package/src/local/git-log-parser.mjs +41 -0
- package/src/local/git-metadata-boundary.mjs +77 -0
- package/src/local/git-metadata-tree-safety.mjs +59 -0
- package/src/local/git-operation-state.mjs +19 -0
- package/src/local/git-service.mjs +165 -30
- package/src/local/hardened-npm-download-timeout.mjs +46 -0
- package/src/local/hardened-npm-download.mjs +109 -0
- package/src/local/hardened-npm-extract.mjs +20 -0
- package/src/local/hardened-npm-verification.mjs +104 -0
- package/src/local/hardened-npm.mjs +199 -0
- package/src/local/job-runner.mjs +244 -89
- package/src/local/lifecycle.mjs +6 -0
- package/src/local/log.mjs +19 -45
- package/src/local/macos-background-input.mjs +398 -0
- package/src/local/macos-idle-sleep-assertion.mjs +65 -0
- package/src/local/macos-trust-broker.mjs +404 -0
- package/src/local/managed-job-cancellation.mjs +42 -0
- package/src/local/managed-job-capacity.mjs +16 -0
- package/src/local/managed-job-directory-generation.mjs +60 -0
- package/src/local/managed-job-directory.mjs +75 -0
- package/src/local/managed-job-durable-process.mjs +49 -0
- package/src/local/managed-job-lock.mjs +46 -14
- package/src/local/managed-job-plan.mjs +38 -14
- package/src/local/managed-job-projection.mjs +23 -15
- package/src/local/managed-job-retention.mjs +128 -0
- package/src/local/managed-job-runner-claim.mjs +70 -0
- package/src/local/managed-job-runner.mjs +35 -10
- package/src/local/managed-job-storage.mjs +22 -6
- package/src/local/managed-job-terminal-maintenance.mjs +40 -0
- package/src/local/managed-job-terminal.mjs +150 -0
- package/src/local/managed-jobs.mjs +424 -287
- package/src/local/npm-cli.mjs +42 -0
- package/src/local/npm-environment.mjs +37 -0
- package/src/local/operation-authorization.mjs +68 -324
- package/src/local/operation-risk.mjs +51 -4
- package/src/local/owner-state-lock.mjs +118 -0
- package/src/local/package-identity.mjs +9 -0
- package/src/local/patch.mjs +20 -18
- package/src/local/path-inspection.mjs +23 -0
- package/src/local/policy.mjs +19 -0
- package/src/local/private-toolchain-integrity.mjs +54 -0
- package/src/local/process-execution.mjs +87 -75
- package/src/local/process-foreground-timeout.mjs +50 -0
- package/src/local/process-identity.mjs +41 -7
- package/src/local/process-nonreplayable-settlement.mjs +50 -0
- package/src/local/process-result-projection.mjs +22 -0
- package/src/local/process-session-events.mjs +47 -0
- package/src/local/process-session-remote-activity.mjs +10 -0
- package/src/local/process-session-termination.mjs +73 -0
- package/src/local/process-sessions.mjs +144 -112
- package/src/local/process-tracker.mjs +140 -13
- package/src/local/process-tree-ownership-types.d.ts +37 -0
- package/src/local/process-tree-ownership.mjs +57 -0
- package/src/local/process-tree-signal.mjs +34 -0
- package/src/local/process-tree-snapshot.mjs +77 -0
- package/src/local/process-tree-supervisor.mjs +69 -0
- package/src/local/process-tree.mjs +11 -53
- package/src/local/project-metadata.mjs +19 -6
- package/src/local/relay-call-recovery.mjs +58 -38
- package/src/local/relay-connection-classification.mjs +131 -0
- package/src/local/relay-connection-support.mjs +91 -0
- package/src/local/relay-connection.mjs +179 -272
- package/src/local/relay-diagnostics.mjs +91 -0
- package/src/local/relay-heartbeat.mjs +119 -0
- package/src/local/relay-liveness.mjs +109 -0
- package/src/local/relay-peer-diagnostics.mjs +29 -0
- package/src/local/release-runtime-lock.mjs +18 -0
- package/src/local/remote-activity-idle-sleep-guard.mjs +77 -0
- package/src/local/remote-configuration.mjs +48 -0
- package/src/local/resilient-relay-connection.mjs +178 -0
- package/src/local/resource-admission-diagnostics.mjs +52 -0
- package/src/local/resource-admission-policy.mjs +115 -0
- package/src/local/resource-admission.mjs +339 -0
- package/src/local/resource-build-root.mjs +83 -0
- package/src/local/resource-cargo-concurrency.mjs +38 -0
- package/src/local/resource-cmake-concurrency.mjs +42 -0
- package/src/local/resource-command-concurrency.mjs +37 -0
- package/src/local/resource-command-profile.mjs +176 -0
- package/src/local/resource-coordinator-accounting.mjs +21 -0
- package/src/local/resource-cpu-window.mjs +20 -0
- package/src/local/resource-disk-headroom.mjs +13 -0
- package/src/local/resource-elastic-memory.mjs +19 -0
- package/src/local/resource-elastic-request.mjs +36 -0
- package/src/local/resource-foreground-wait.mjs +29 -0
- package/src/local/resource-go-concurrency.mjs +43 -0
- package/src/local/resource-gradle-concurrency.mjs +57 -0
- package/src/local/resource-host-cache.mjs +39 -0
- package/src/local/resource-host-darwin.mjs +54 -0
- package/src/local/resource-host-linux.mjs +35 -0
- package/src/local/resource-host-sample-file.mjs +23 -0
- package/src/local/resource-host-snapshot.mjs +99 -0
- package/src/local/resource-lease-accounting.mjs +119 -0
- package/src/local/resource-light-command.mjs +6 -0
- package/src/local/resource-make-concurrency.mjs +42 -0
- package/src/local/resource-maven-concurrency.mjs +38 -0
- package/src/local/resource-ninja-command-concurrency.mjs +20 -0
- package/src/local/resource-ninja-concurrency.mjs +28 -0
- package/src/local/resource-operations.mjs +26 -10
- package/src/local/resource-pressure.mjs +67 -0
- package/src/local/resource-probe-command.mjs +35 -0
- package/src/local/resource-process-admission.mjs +59 -0
- package/src/local/resource-process-ancestry-cache.mjs +43 -0
- package/src/local/resource-process-ancestry.mjs +32 -0
- package/src/local/resource-process-priority.mjs +15 -0
- package/src/local/resource-project-key.mjs +53 -0
- package/src/local/resource-pytest-concurrency.mjs +33 -0
- package/src/local/resource-release-control-classification.mjs +14 -0
- package/src/local/resource-release-control-executable.mjs +70 -0
- package/src/local/resource-release-control-workspace.mjs +41 -0
- package/src/local/resource-request-contract.mjs +30 -0
- package/src/local/resource-script-classification.mjs +44 -0
- package/src/local/resource-shell-analysis.mjs +58 -0
- package/src/local/resource-staging-recovery.mjs +97 -0
- package/src/local/resource-swift-concurrency.mjs +31 -0
- package/src/local/resource-transaction-lock.mjs +189 -0
- package/src/local/resource-wait.mjs +48 -0
- package/src/local/resource-waiters.mjs +139 -0
- package/src/local/resource-xcode-command.mjs +7 -0
- package/src/local/resource-xcode-concurrency.mjs +34 -0
- package/src/local/resource-xcode-non-build.mjs +19 -0
- package/src/local/runtime-activation.mjs +700 -0
- package/src/local/runtime-activity-projection.mjs +48 -0
- package/src/local/runtime-capabilities.mjs +48 -15
- package/src/local/runtime-diagnostic-state.mjs +14 -0
- package/src/local/runtime-diagnostics.mjs +40 -13
- package/src/local/runtime-info-projection.mjs +60 -0
- package/src/local/runtime-info-relay-projection.mjs +21 -0
- package/src/local/runtime-paths.mjs +2 -1
- package/src/local/runtime-process-routing.mjs +33 -0
- package/src/local/runtime-relay-connection-options.mjs +44 -0
- package/src/local/runtime-relay-control.mjs +98 -0
- package/src/local/runtime-relay.mjs +26 -50
- package/src/local/runtime-reporting.mjs +37 -21
- package/src/local/runtime-resource-service.mjs +77 -0
- package/src/local/runtime-tool-handlers.mjs +13 -10
- package/src/local/runtime.mjs +244 -208
- package/src/local/secure-file.mjs +73 -20
- package/src/local/security-audit-dispatch.mjs +35 -0
- package/src/local/security-audit-log.mjs +215 -0
- package/src/local/security-audit-state.mjs +139 -0
- package/src/local/security-audit-storage.mjs +135 -0
- package/src/local/security-audit-warning.mjs +31 -0
- package/src/local/security-audit-worker.mjs +87 -0
- package/src/local/service-convergence.mjs +42 -1
- package/src/local/service-definition.mjs +24 -0
- package/src/local/service-environment.mjs +18 -10
- package/src/local/service-owner.mjs +146 -0
- package/src/local/service-ownership.mjs +17 -0
- package/src/local/service-restart-handoff.mjs +64 -0
- package/src/local/service-restart-scheduler.mjs +49 -0
- package/src/local/service-runtime-convergence.mjs +52 -0
- package/src/local/service-runtime.mjs +105 -0
- package/src/local/service-status.mjs +60 -0
- package/src/local/service.mjs +385 -92
- package/src/local/shell.mjs +39 -19
- package/src/local/ssh-key.mjs +158 -42
- package/src/local/state-inventory.mjs +50 -24
- package/src/local/state-owner-lock-inventory.mjs +23 -0
- package/src/local/state-root-owned-namespaces.mjs +83 -0
- package/src/local/state-root-retirement.mjs +89 -0
- package/src/local/state.mjs +386 -96
- package/src/local/stdio.mjs +96 -66
- package/src/local/support-state-projection.mjs +30 -0
- package/src/local/system-network-route.mjs +76 -0
- package/src/local/systemd-removal.mjs +20 -0
- package/src/local/tool-executor.mjs +85 -20
- package/src/local/tool-result-boundary.mjs +51 -0
- package/src/local/toolchain-operation-lock.mjs +41 -0
- package/src/local/tools.mjs +7 -5
- package/src/local/trusted-executable.mjs +53 -0
- package/src/local/trusted-git-executable.mjs +34 -0
- package/src/local/trusted-github-cli.mjs +24 -0
- package/src/local/windows-service-convergence.mjs +49 -0
- package/src/local/windows-service.mjs +52 -45
- package/src/local/worker-deployment-fingerprint.mjs +102 -0
- package/src/local/worker-deployment.mjs +48 -60
- package/src/local/worker-health.mjs +12 -4
- package/src/local/worker-secret-file.mjs +37 -17
- package/src/local/workspace-file-service.mjs +156 -239
- package/src/local/workspace-file-transaction.mjs +189 -0
- package/src/local/workspace-search.mjs +55 -0
- package/src/local/wrangler-toolchain/package-lock.json +1505 -0
- package/src/local/wrangler-toolchain/package.json +23 -0
- package/src/local/wrangler-toolchain-verification.mjs +135 -0
- package/src/local/wrangler-toolchain.mjs +188 -0
- package/src/shared/access-contract.json +7 -1
- package/src/shared/activation-recovery.mjs +49 -0
- package/src/shared/admin-auth.d.mts +2 -1
- package/src/shared/admin-auth.mjs +3 -3
- package/src/shared/authority-revocation.d.mts +9 -0
- package/src/shared/authority-revocation.mjs +28 -0
- package/src/shared/daemon-auth.d.mts +11 -0
- package/src/shared/daemon-auth.mjs +17 -0
- package/src/shared/device-session-auth.d.mts +20 -0
- package/src/shared/device-session-auth.mjs +51 -0
- package/src/shared/foreground-timeout.d.mts +15 -0
- package/src/shared/foreground-timeout.mjs +70 -0
- package/src/shared/log-redaction.d.mts +9 -0
- package/src/shared/log-redaction.mjs +51 -0
- package/src/shared/mcp-protocol.d.mts +27 -0
- package/src/shared/mcp-protocol.mjs +275 -0
- package/src/shared/project-overview-projection.d.mts +4 -0
- package/src/shared/project-overview-projection.mjs +96 -0
- package/src/shared/relay-contract.json +28 -0
- package/src/shared/result-projection.d.mts +2 -1
- package/src/shared/result-projection.mjs +13 -2
- package/src/shared/server-metadata.json +15 -9
- package/src/shared/tool-argument-validation.d.mts +17 -0
- package/src/shared/tool-argument-validation.mjs +325 -0
- package/src/shared/tool-call-capacity.d.mts +41 -0
- package/src/shared/tool-call-capacity.mjs +59 -0
- package/src/shared/tool-catalog.json +626 -57
- package/src/worker/access.ts +12 -0
- package/src/worker/account-admin.ts +55 -33
- package/src/worker/authority-revocations.ts +174 -0
- package/src/worker/authority.ts +52 -6
- package/src/worker/daemon-auth.ts +49 -68
- package/src/worker/daemon-channel.ts +36 -0
- package/src/worker/daemon-http-auth.ts +63 -0
- package/src/worker/daemon-http-channel.ts +120 -0
- package/src/worker/daemon-http-controller.ts +163 -0
- package/src/worker/daemon-http-protocol.ts +62 -0
- package/src/worker/daemon-http-registry.ts +82 -0
- package/src/worker/daemon-last-observation.ts +30 -0
- package/src/worker/daemon-liveness.ts +8 -6
- package/src/worker/daemon-ready-dispatch.ts +17 -0
- package/src/worker/daemon-ready-messages.ts +73 -0
- package/src/worker/daemon-ready-waiters.ts +79 -0
- package/src/worker/daemon-recovery-budget.ts +13 -0
- package/src/worker/daemon-registry.ts +58 -0
- package/src/worker/daemon-relay-diagnostics.ts +127 -0
- package/src/worker/daemon-socket-attachment.ts +55 -0
- package/src/worker/daemon-sockets.ts +72 -62
- package/src/worker/daemon-status.ts +38 -0
- package/src/worker/device-session-verifier.ts +129 -0
- package/src/worker/dpop.ts +157 -0
- package/src/worker/durable-process-timeout.ts +29 -0
- package/src/worker/errors.ts +57 -0
- package/src/worker/http.ts +112 -26
- package/src/worker/index.ts +446 -440
- package/src/worker/mcp-access.ts +57 -0
- package/src/worker/mcp-controller.ts +188 -0
- package/src/worker/mcp-http-accept.ts +11 -0
- package/src/worker/mcp-http-contract.ts +322 -0
- package/src/worker/mcp-initialization-compat.ts +141 -0
- package/src/worker/mcp-jsonrpc.ts +11 -29
- package/src/worker/mcp-removed-protocol.ts +17 -0
- package/src/worker/mcp-response-proxy.ts +135 -0
- package/src/worker/mcp-response-stream.ts +65 -0
- package/src/worker/mcp-stale-schema-compat.ts +57 -0
- package/src/worker/mcp-stream-proxy-contract.ts +48 -0
- package/src/worker/mcp-tool-call-input.ts +23 -0
- package/src/worker/nonce-store.ts +33 -35
- package/src/worker/oauth-authorization-page.ts +21 -1
- package/src/worker/oauth-client-admin.ts +52 -0
- package/src/worker/oauth-client-contract.ts +38 -0
- package/src/worker/oauth-controller.ts +107 -47
- package/src/worker/oauth-field-contract.ts +31 -0
- package/src/worker/oauth-record-contract.ts +7 -0
- package/src/worker/oauth-refresh-authority.ts +27 -0
- package/src/worker/oauth-refresh-exchange.ts +162 -0
- package/src/worker/oauth-refresh-families.ts +130 -27
- package/src/worker/oauth-refresh-persistence.ts +116 -0
- package/src/worker/oauth-state.ts +33 -18
- package/src/worker/oauth-store-validation.ts +131 -0
- package/src/worker/oauth-token-derivation.ts +33 -0
- package/src/worker/oauth-token-issuance.ts +119 -0
- package/src/worker/oauth-tokens.ts +56 -190
- package/src/worker/observability.ts +38 -5
- package/src/worker/pending-admission.ts +15 -0
- package/src/worker/pending-call-capacity.ts +108 -0
- package/src/worker/pending-call-contract.ts +42 -5
- package/src/worker/pending-call-deadlines.ts +75 -0
- package/src/worker/pending-calls.ts +131 -125
- package/src/worker/runtime-alarm-storage.ts +37 -0
- package/src/worker/runtime-alarm.ts +136 -0
- package/src/worker/server-info-activity.ts +36 -0
- package/src/worker/server-info-tool-delivery.ts +15 -0
- package/src/worker/server-info.ts +168 -0
- package/src/worker/tool-catalog.ts +47 -1
- package/src/worker/tool-timeout.ts +75 -14
- package/src/worker/websocket-protocol.ts +26 -2
- package/src/worker/worker-edge-guard.ts +90 -0
- package/src/worker/worker-edge-log.ts +65 -0
- package/src/worker/worker-entry.ts +52 -0
- package/src/worker/worker-mcp-config.ts +23 -0
- package/src/worker/worker-metadata.ts +48 -0
- package/src/worker/worker-rate-limit-key.ts +30 -0
- package/src/worker/worker-runtime-config.ts +20 -0
- package/src/worker/worker-static-routes.ts +54 -0
- package/src/worker/worker-task-lifetime.ts +18 -0
- package/tsconfig.local.json +17 -1
- package/wrangler.jsonc +13 -0
- package/src/local/cli-approval.mjs +0 -117
- package/src/local/operation-state-lock.mjs +0 -92
- package/src/worker/mcp-session.ts +0 -72
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import relayContract from "../shared/relay-contract.json" with { type: "json" };
|
|
2
|
+
import { randomToken } from "./oauth-state.ts";
|
|
3
|
+
import { DaemonHttpChannel } from "./daemon-http-channel.ts";
|
|
4
|
+
import type { DaemonAttachment } from "./daemon-socket-attachment.ts";
|
|
5
|
+
|
|
6
|
+
export class DaemonHttpRegistry {
|
|
7
|
+
private readonly channels = new Map<string, DaemonHttpChannel>();
|
|
8
|
+
|
|
9
|
+
beginCandidate(sessionId: string, attachment: DaemonAttachment, now = Date.now()): DaemonHttpChannel | undefined {
|
|
10
|
+
const existing = this.channels.get(sessionId);
|
|
11
|
+
if (existing) { existing.touch(now); return existing; }
|
|
12
|
+
this.pruneCandidates(now);
|
|
13
|
+
if (this.channels.size >= 2) {
|
|
14
|
+
const oldest = [...this.channels.values()]
|
|
15
|
+
.filter((channel) => !channel.isActivated)
|
|
16
|
+
.sort((a, b) => a.lastSeenMs - b.lastSeenMs)[0];
|
|
17
|
+
if (!oldest) return undefined;
|
|
18
|
+
this.close(oldest);
|
|
19
|
+
}
|
|
20
|
+
const channel = new DaemonHttpChannel({
|
|
21
|
+
sessionId, activationToken: randomToken("activate"), attachment, now,
|
|
22
|
+
});
|
|
23
|
+
this.channels.set(sessionId, channel);
|
|
24
|
+
return channel;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
get(sessionId: string): DaemonHttpChannel | undefined { return this.channels.get(sessionId); }
|
|
28
|
+
|
|
29
|
+
readyChannels(now = Date.now()): DaemonHttpChannel[] {
|
|
30
|
+
return [...this.channels.values()]
|
|
31
|
+
.filter((channel) => channel.readyState === 1
|
|
32
|
+
&& now - channel.lastSeenMs < relayContract.httpFallbackLivenessTimeoutMs)
|
|
33
|
+
.sort((left, right) => right.activatedMs - left.activatedMs);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
candidates(now = Date.now()): DaemonHttpChannel[] {
|
|
37
|
+
this.pruneCandidates(now);
|
|
38
|
+
return [...this.channels.values()].filter((channel) => channel.readyState !== 1
|
|
39
|
+
&& now - channel.lastSeenMs < relayContract.httpFallbackLivenessTimeoutMs);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
activate(sessionId: string, activationToken: string, now = Date.now()): DaemonHttpChannel | undefined {
|
|
43
|
+
const channel = this.channels.get(sessionId);
|
|
44
|
+
if (!channel || channel.activationToken !== activationToken) return undefined;
|
|
45
|
+
channel.activate(now);
|
|
46
|
+
return channel;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
attachment(channel: DaemonHttpChannel): DaemonAttachment | undefined {
|
|
50
|
+
return this.channels.get(channel.sessionId) === channel ? channel.attachment : undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
close(channel: DaemonHttpChannel): boolean {
|
|
54
|
+
if (this.channels.get(channel.sessionId) !== channel) return false;
|
|
55
|
+
this.channels.delete(channel.sessionId);
|
|
56
|
+
channel.close();
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
staleReady(now = Date.now()): DaemonHttpChannel[] {
|
|
61
|
+
return [...this.channels.values()].filter((channel) => channel.readyState === 1
|
|
62
|
+
&& now - channel.lastSeenMs >= relayContract.httpFallbackLivenessTimeoutMs);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
staleOwned(now = Date.now()): DaemonHttpChannel[] {
|
|
66
|
+
return [...this.channels.values()].filter((channel) => channel.isActivated
|
|
67
|
+
&& now - channel.lastSeenMs >= relayContract.httpFallbackLivenessTimeoutMs);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
nextDeadline(now = Date.now()): number {
|
|
71
|
+
const deadlines = [...this.channels.values()].map((channel) => channel.lastSeenMs + relayContract.httpFallbackLivenessTimeoutMs);
|
|
72
|
+
return deadlines.length > 0 ? Math.max(now + 1, Math.min(...deadlines)) : Number.POSITIVE_INFINITY;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private pruneCandidates(now = Date.now()): void {
|
|
76
|
+
for (const channel of [...this.channels.values()]) {
|
|
77
|
+
if (channel.isActivated) continue;
|
|
78
|
+
if (now - channel.lastSeenMs < relayContract.httpFallbackLivenessTimeoutMs) continue;
|
|
79
|
+
this.close(channel);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { DaemonChannel } from "./daemon-channel.ts";
|
|
2
|
+
import type { DaemonAttachment } from "./daemon-socket-attachment.ts";
|
|
3
|
+
import type { DaemonRelayDiagnostics } from "./daemon-relay-diagnostics.ts";
|
|
4
|
+
|
|
5
|
+
export interface LastDaemonObservation {
|
|
6
|
+
transport: "websocket" | "https";
|
|
7
|
+
connectedAt: string;
|
|
8
|
+
lastSeenAt: string;
|
|
9
|
+
disconnectedAt: string | null;
|
|
10
|
+
relayDiagnostics?: DaemonRelayDiagnostics;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class DaemonLastObservation {
|
|
14
|
+
private value: LastDaemonObservation | undefined;
|
|
15
|
+
|
|
16
|
+
remember(channel: DaemonChannel, attachment: DaemonAttachment | undefined, disconnected: boolean): void {
|
|
17
|
+
if (attachment?.role !== "daemon") return;
|
|
18
|
+
this.value = {
|
|
19
|
+
transport: channel.daemonTransport === "https" ? "https" : "websocket",
|
|
20
|
+
connectedAt: attachment.connectedAt,
|
|
21
|
+
lastSeenAt: attachment.lastSeenAt ?? attachment.connectedAt,
|
|
22
|
+
disconnectedAt: disconnected ? new Date().toISOString() : null,
|
|
23
|
+
relayDiagnostics: attachment.relayDiagnostics && { ...attachment.relayDiagnostics },
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
snapshot(): LastDaemonObservation | undefined {
|
|
28
|
+
return this.value ? { ...this.value, relayDiagnostics: this.value.relayDiagnostics && { ...this.value.relayDiagnostics } } : undefined;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Worker-side authenticated daemon liveness.
|
|
3
3
|
*
|
|
4
|
-
* Local daemons heartbeat about every 25s
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* Local daemons send an application heartbeat about every 25s so this attachment
|
|
5
|
+
* stays fresh, while an independent protocol-level ping/pong watchdog detects a
|
|
6
|
+
* black-holed transport on a much shorter interval without waking the Durable
|
|
7
|
+
* Object. The Worker still applies a wider application-liveness bound:
|
|
8
|
+
* role=daemon + readyState=OPEN is not enough after Durable Object hibernation or
|
|
9
|
+
* a half-closed transport, because those sockets can still look connected while
|
|
10
|
+
* tool_call never returns.
|
|
8
11
|
*/
|
|
9
12
|
export const DAEMON_LIVENESS_TIMEOUT_MS = 90_000;
|
|
10
13
|
export const DAEMON_READY_TIMEOUT_MS = 15_000;
|
|
@@ -18,8 +21,7 @@ export interface DaemonLivenessFields {
|
|
|
18
21
|
}
|
|
19
22
|
|
|
20
23
|
export function daemonLastSeenMs(attachment: DaemonLivenessFields | undefined | null): number {
|
|
21
|
-
|
|
22
|
-
return Date.parse(raw);
|
|
24
|
+
return Date.parse(attachment?.lastSeenAt || "");
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
export function isLiveDaemonAttachment(
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { waitForReadyDaemon } from "./daemon-ready-waiters.ts";
|
|
2
|
+
import { readyDaemonChannels, type DaemonChannel, type ReadyDaemonRegistry } from "./daemon-channel.ts";
|
|
3
|
+
|
|
4
|
+
type ReadyWaitOptions = Parameters<typeof waitForReadyDaemon>[1];
|
|
5
|
+
type ReadyDispatch = Readonly<{ socket: DaemonChannel; recoveryDelayMs: number }>;
|
|
6
|
+
|
|
7
|
+
export async function readyDaemonForDispatch(
|
|
8
|
+
registry: ReadyDaemonRegistry,
|
|
9
|
+
options: ReadyWaitOptions = {},
|
|
10
|
+
now: () => number = () => performance.now(),
|
|
11
|
+
): Promise<ReadyDispatch> {
|
|
12
|
+
const immediate = readyDaemonChannels(registry)[0];
|
|
13
|
+
if (immediate) return Object.freeze({ socket: immediate, recoveryDelayMs: 0 });
|
|
14
|
+
const startedAt = now();
|
|
15
|
+
const socket = await waitForReadyDaemon(registry, options);
|
|
16
|
+
return Object.freeze({ socket, recoveryDelayMs: Math.max(0, now() - startedAt) });
|
|
17
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { acknowledgeAuthorityRevocation, authorityRevocationAckId } from "./authority-revocations.ts";
|
|
2
|
+
import type { DaemonChannel } from "./daemon-channel.ts";
|
|
3
|
+
import { daemonResumeMissingCallIds } from "./websocket-protocol.ts";
|
|
4
|
+
import { daemonCallNotReceivedAfterReconnectError, daemonToolError } from "./errors.ts";
|
|
5
|
+
import type { PendingCallOutcome } from "./pending-call-contract.ts";
|
|
6
|
+
import type { PendingCallRegistry } from "./pending-calls.ts";
|
|
7
|
+
import type { WorkerObservability } from "./observability.ts";
|
|
8
|
+
import { trySendDaemonChannel } from "./daemon-channel.ts";
|
|
9
|
+
|
|
10
|
+
export type ReadyMessageDisposition = Readonly<{
|
|
11
|
+
ok: boolean;
|
|
12
|
+
errorCode?: string;
|
|
13
|
+
errorMessage?: string;
|
|
14
|
+
}>;
|
|
15
|
+
|
|
16
|
+
export async function handleReadyDaemonMessage(input: {
|
|
17
|
+
channel: DaemonChannel;
|
|
18
|
+
body: Record<string, unknown>;
|
|
19
|
+
pending: PendingCallRegistry;
|
|
20
|
+
storage: DurableObjectStorage;
|
|
21
|
+
observability: WorkerObservability;
|
|
22
|
+
}): Promise<ReadyMessageDisposition> {
|
|
23
|
+
const { channel, body, pending, storage, observability } = input;
|
|
24
|
+
if (body.type === "resume_calls_ack") {
|
|
25
|
+
const missingIds = daemonResumeMissingCallIds(body.missing_ids);
|
|
26
|
+
if (!missingIds) return invalid("invalid_resume_calls_ack", "invalid resume calls acknowledgement");
|
|
27
|
+
await settleDaemonProvenMissingCalls({ ids: missingIds, channel, pending, observability });
|
|
28
|
+
return { ok: true };
|
|
29
|
+
}
|
|
30
|
+
if (body.type === "authority_revoke_ack") {
|
|
31
|
+
const revocationId = authorityRevocationAckId(body.revocation_id);
|
|
32
|
+
if (!revocationId) return invalid("invalid_authority_revoke_ack", "invalid authority revocation acknowledgement");
|
|
33
|
+
await acknowledgeAuthorityRevocation(storage, revocationId);
|
|
34
|
+
return { ok: true };
|
|
35
|
+
}
|
|
36
|
+
if (body.type !== "tool_result" || typeof body.id !== "string") {
|
|
37
|
+
return invalid("unknown_message_type", "unknown daemon message type");
|
|
38
|
+
}
|
|
39
|
+
const outcome: PendingCallOutcome = body.ok === false
|
|
40
|
+
? { ok: false, error: daemonToolError(body.error) }
|
|
41
|
+
: { ok: true, value: body.result };
|
|
42
|
+
const ownership = pending.resultOwnership(body.id, channel);
|
|
43
|
+
const transientMatched = ownership === "owned" && (outcome.ok
|
|
44
|
+
? await pending.resolve(body.id, channel, outcome.value)
|
|
45
|
+
: await pending.reject(body.id, outcome.error, channel));
|
|
46
|
+
if (transientMatched || ownership === "missing") trySendDaemonChannel(channel, { type: "tool_result_ack", id: body.id });
|
|
47
|
+
observability.daemonTerminalResult(transientMatched ? "committed"
|
|
48
|
+
: ownership === "missing" ? "owner_missing_acknowledged" : "stale_connection_rejected");
|
|
49
|
+
return { ok: true };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function settleDaemonProvenMissingCalls(input: {
|
|
53
|
+
ids: Iterable<string>;
|
|
54
|
+
channel: DaemonChannel;
|
|
55
|
+
pending: PendingCallRegistry;
|
|
56
|
+
observability: WorkerObservability;
|
|
57
|
+
}): Promise<{ redelivered: number; rejected: number }> {
|
|
58
|
+
let redelivered = 0;
|
|
59
|
+
const rejected = await input.pending.rejectSocketIds(
|
|
60
|
+
input.ids, input.channel, (record) => daemonCallNotReceivedAfterReconnectError(record.recovery), undefined,
|
|
61
|
+
(record) => {
|
|
62
|
+
if (record.redeliverAfterProvenMissing?.(record, input.channel) !== true) return false;
|
|
63
|
+
redelivered += 1; return true;
|
|
64
|
+
},
|
|
65
|
+
);
|
|
66
|
+
if (redelivered > 0) input.observability.event("info", "daemon.calls.redelivered_after_proven_non_delivery", { calls: redelivered });
|
|
67
|
+
if (rejected > 0) input.observability.event("info", "daemon.calls.not_received_after_reconnect", { calls: rejected });
|
|
68
|
+
return { redelivered, rejected };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function invalid(errorCode: string, errorMessage: string): ReadyMessageDisposition {
|
|
72
|
+
return { ok: false, errorCode, errorMessage };
|
|
73
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import relayContract from "../shared/relay-contract.json" with { type: "json" };
|
|
2
|
+
import { WorkerToolError } from "./errors.ts";
|
|
3
|
+
import { readyDaemonChannels, type DaemonChannel, type ReadyDaemonRegistry } from "./daemon-channel.ts";
|
|
4
|
+
import { assertWorkerPendingCallAdmission, type PendingCapacitySnapshot } from "./pending-call-capacity.ts";
|
|
5
|
+
|
|
6
|
+
type ReadyWaiter = Readonly<{ tool: string; resolve: (socket: DaemonChannel) => void }>;
|
|
7
|
+
type WaitOptions = Readonly<{ graceMs?: number; signal?: AbortSignal; tool?: string; pending?: PendingCapacitySnapshot }>;
|
|
8
|
+
|
|
9
|
+
const waitersByRegistry = new WeakMap<ReadyDaemonRegistry, Set<ReadyWaiter>>();
|
|
10
|
+
|
|
11
|
+
export async function waitForReadyDaemon(
|
|
12
|
+
registry: ReadyDaemonRegistry,
|
|
13
|
+
options: WaitOptions = {},
|
|
14
|
+
): Promise<DaemonChannel> {
|
|
15
|
+
const immediate = readyDaemonChannels(registry)[0];
|
|
16
|
+
if (immediate) return immediate;
|
|
17
|
+
const graceMs = positiveInteger(options.graceMs, relayContract.newCallReconnectGraceMs);
|
|
18
|
+
if (options.signal?.aborted) throw cancelledError();
|
|
19
|
+
const waiters = waitersByRegistry.get(registry) ?? new Set<ReadyWaiter>();
|
|
20
|
+
const tool = String(options.tool || "unknown");
|
|
21
|
+
const byTool: Record<string, number> = { ...(options.pending?.by_tool ?? {}) };
|
|
22
|
+
for (const waiter of waiters) byTool[waiter.tool] = (byTool[waiter.tool] ?? 0) + 1;
|
|
23
|
+
assertWorkerPendingCallAdmission({ active: (options.pending?.active ?? 0) + waiters.size, by_tool: byTool }, tool);
|
|
24
|
+
waitersByRegistry.set(registry, waiters);
|
|
25
|
+
return new Promise<DaemonChannel>((resolvePromise, rejectPromise) => {
|
|
26
|
+
let settled = false;
|
|
27
|
+
let timer: ReturnType<typeof setTimeout> | null = null;
|
|
28
|
+
const finish = (socket?: DaemonChannel, error?: Error) => {
|
|
29
|
+
if (settled) return;
|
|
30
|
+
settled = true;
|
|
31
|
+
if (timer) clearTimeout(timer);
|
|
32
|
+
waiters.delete(waiter);
|
|
33
|
+
if (waiters.size === 0) waitersByRegistry.delete(registry);
|
|
34
|
+
options.signal?.removeEventListener("abort", abort);
|
|
35
|
+
if (socket) resolvePromise(socket);
|
|
36
|
+
else rejectPromise(error ?? unavailableError(graceMs));
|
|
37
|
+
};
|
|
38
|
+
const waiter: ReadyWaiter = { tool, resolve: (socket) => finish(socket) };
|
|
39
|
+
const abort = () => finish(undefined, cancelledError());
|
|
40
|
+
waiters.add(waiter);
|
|
41
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
42
|
+
timer = setTimeout(() => finish(readyDaemonChannels(registry)[0]), graceMs);
|
|
43
|
+
const raced = readyDaemonChannels(registry)[0];
|
|
44
|
+
if (raced) finish(raced);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function notifyReadyDaemon(registry: ReadyDaemonRegistry): number {
|
|
49
|
+
const socket = readyDaemonChannels(registry)[0];
|
|
50
|
+
const waiters = waitersByRegistry.get(registry);
|
|
51
|
+
if (!socket || !waiters?.size) return 0;
|
|
52
|
+
const count = waiters.size;
|
|
53
|
+
waitersByRegistry.delete(registry);
|
|
54
|
+
for (const waiter of [...waiters]) waiter.resolve(socket);
|
|
55
|
+
return count;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function readyDaemonWaiterSnapshot(registry: ReadyDaemonRegistry): PendingCapacitySnapshot {
|
|
59
|
+
const waiters = waitersByRegistry.get(registry) ?? new Set<ReadyWaiter>();
|
|
60
|
+
const by_tool: Record<string, number> = {};
|
|
61
|
+
for (const waiter of waiters) by_tool[waiter.tool] = (by_tool[waiter.tool] ?? 0) + 1;
|
|
62
|
+
return { active: waiters.size, by_tool };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function unavailableError(graceMs: number): WorkerToolError {
|
|
66
|
+
return new WorkerToolError(
|
|
67
|
+
"unavailable",
|
|
68
|
+
`local daemon did not reconnect within ${Math.ceil(graceMs / 1000)} seconds; keep the CLI start command running`,
|
|
69
|
+
true,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function cancelledError(): WorkerToolError {
|
|
74
|
+
return new WorkerToolError("cancelled", "tool call cancelled while waiting for local daemon recovery");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function positiveInteger(value: number | undefined, fallback: number): number {
|
|
78
|
+
return Number.isFinite(value) && Number(value) > 0 ? Math.floor(Number(value)) : fallback;
|
|
79
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import relayContract from "../shared/relay-contract.json" with { type: "json" };
|
|
2
|
+
import { WorkerToolError } from "./errors.ts";
|
|
3
|
+
import type { DaemonToolTimeoutBudget } from "./tool-timeout.ts";
|
|
4
|
+
|
|
5
|
+
export function daemonToolTimeoutBudgetAfterDelay(budget: DaemonToolTimeoutBudget, elapsedMs: number): DaemonToolTimeoutBudget {
|
|
6
|
+
const elapsed = Math.max(0, Math.floor(Number(elapsedMs) || 0));
|
|
7
|
+
const settlementTimeoutMs = Math.max(0, budget.settlementTimeoutMs - elapsed);
|
|
8
|
+
const executionTimeoutMs = Math.min(budget.executionTimeoutMs, settlementTimeoutMs - relayContract.workerSettlementOverheadMs);
|
|
9
|
+
if (executionTimeoutMs < 1) {
|
|
10
|
+
throw new WorkerToolError("unavailable", "local daemon recovery consumed the tool execution window; retry the call", true, { side_effects_started: false });
|
|
11
|
+
}
|
|
12
|
+
return Object.freeze({ executionTimeoutMs: Math.floor(executionTimeoutMs), settlementTimeoutMs: Math.floor(settlementTimeoutMs) });
|
|
13
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { DaemonChallenge, DaemonPreflightAuthorization } from "./daemon-auth.ts";
|
|
2
|
+
import type { DaemonChannel, ReadyDaemonRegistry } from "./daemon-channel.ts";
|
|
3
|
+
import { DaemonHttpChannel } from "./daemon-http-channel.ts";
|
|
4
|
+
import { DaemonHttpRegistry } from "./daemon-http-registry.ts";
|
|
5
|
+
import { DaemonLastObservation, type LastDaemonObservation } from "./daemon-last-observation.ts";
|
|
6
|
+
import type { DaemonAttachment } from "./daemon-socket-attachment.ts";
|
|
7
|
+
import { DaemonSocketRegistry, type DaemonSocketCleanup } from "./daemon-sockets.ts";
|
|
8
|
+
import type { DaemonPolicy } from "./policy.ts";
|
|
9
|
+
import type { DaemonRelayDiagnostics } from "./daemon-relay-diagnostics.ts";
|
|
10
|
+
|
|
11
|
+
interface WebSocketContext { getWebSockets(): WebSocket[] }
|
|
12
|
+
|
|
13
|
+
export class DaemonRegistry implements ReadyDaemonRegistry {
|
|
14
|
+
readonly http = new DaemonHttpRegistry();
|
|
15
|
+
private readonly sockets: DaemonSocketRegistry;
|
|
16
|
+
private readonly lastObservation = new DaemonLastObservation();
|
|
17
|
+
constructor(context: WebSocketContext) { this.sockets = new DaemonSocketRegistry(context); }
|
|
18
|
+
|
|
19
|
+
readyChannels(now = Date.now()): DaemonChannel[] {
|
|
20
|
+
const webSockets = this.sockets.readySockets(now);
|
|
21
|
+
return webSockets.length > 0 ? webSockets : this.http.readyChannels(now);
|
|
22
|
+
}
|
|
23
|
+
readyAttachment(channel: DaemonChannel): DaemonAttachment | undefined {
|
|
24
|
+
return channel.daemonTransport === "https"
|
|
25
|
+
? this.http.attachment(channel as DaemonHttpChannel)
|
|
26
|
+
: this.sockets.readyAttachment(channel as WebSocket);
|
|
27
|
+
}
|
|
28
|
+
httpReadyChannels(now = Date.now()): DaemonHttpChannel[] { return this.http.readyChannels(now); }
|
|
29
|
+
httpCandidates(now = Date.now()): DaemonHttpChannel[] { return this.http.candidates(now); }
|
|
30
|
+
|
|
31
|
+
attachment(socket: WebSocket): DaemonAttachment | undefined { return this.sockets.attachment(socket); }
|
|
32
|
+
candidateSockets(): WebSocket[] { return this.sockets.candidateSockets(); }
|
|
33
|
+
probingSockets(): WebSocket[] { return this.sockets.probingSockets(); }
|
|
34
|
+
readyRoleSockets(): WebSocket[] { return this.sockets.readyRoleSockets(); }
|
|
35
|
+
readySockets(now = Date.now()): WebSocket[] { return this.sockets.readySockets(now); }
|
|
36
|
+
nonReadySockets(): WebSocket[] { return this.sockets.nonReadySockets(); }
|
|
37
|
+
beginCandidate(socket: WebSocket, challenge: DaemonChallenge, preflight: DaemonPreflightAuthorization, connectionId: string, connectedAt?: string): void {
|
|
38
|
+
this.sockets.beginCandidate(socket, challenge, preflight, connectionId, connectedAt);
|
|
39
|
+
}
|
|
40
|
+
beginProbe(socket: WebSocket, values: {
|
|
41
|
+
connectedAt: string; probeId: string; instanceId: string; connectionId: string;
|
|
42
|
+
policy: DaemonPolicy; tools: string[]; relayDiagnostics?: DaemonRelayDiagnostics;
|
|
43
|
+
}): void { this.sockets.beginProbe(socket, values); }
|
|
44
|
+
promote(socket: WebSocket, lastSeenAt?: string): DaemonAttachment | undefined {
|
|
45
|
+
const attachment = this.sockets.promote(socket, lastSeenAt); this.lastObservation.remember(socket, attachment, false); return attachment;
|
|
46
|
+
}
|
|
47
|
+
touch(socket: WebSocket, lastSeenAt?: string): DaemonAttachment | undefined {
|
|
48
|
+
const attachment = this.sockets.touch(socket, lastSeenAt); this.lastObservation.remember(socket, attachment, false); return attachment;
|
|
49
|
+
}
|
|
50
|
+
beginCleanup(socket: WebSocket, operation: (attachment: DaemonAttachment) => Promise<unknown>): DaemonSocketCleanup | undefined {
|
|
51
|
+
this.lastObservation.remember(socket, this.sockets.attachment(socket), true);
|
|
52
|
+
return this.sockets.beginCleanup(socket, operation);
|
|
53
|
+
}
|
|
54
|
+
socketForConnectionId(connectionId: string): WebSocket | undefined { return this.sockets.socketForConnectionId(connectionId); }
|
|
55
|
+
rememberReady(channel: DaemonChannel): void { this.lastObservation.remember(channel, this.readyAttachment(channel), false); }
|
|
56
|
+
rememberDisconnected(channel: DaemonChannel): void { this.lastObservation.remember(channel, this.readyAttachment(channel), true); }
|
|
57
|
+
lastDaemonObservation(): LastDaemonObservation | undefined { return this.lastObservation.snapshot(); }
|
|
58
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { sanitizeMetadataText } from "./http.ts";
|
|
2
|
+
|
|
3
|
+
const NETWORK_ROUTES = new Set([
|
|
4
|
+
"unresolved",
|
|
5
|
+
"application-http-proxy",
|
|
6
|
+
"system-network-stack",
|
|
7
|
+
"invalid-application-proxy-configuration",
|
|
8
|
+
]);
|
|
9
|
+
const TRANSPORTS = new Set(["websocket", "https"]);
|
|
10
|
+
const CONNECT_STAGES = new Set([
|
|
11
|
+
"idle", "socket_constructing", "proxy_connecting", "tcp_connecting", "dns_resolved",
|
|
12
|
+
"tcp_connected", "tls_established", "http_rejected", "websocket_open",
|
|
13
|
+
]);
|
|
14
|
+
const TRANSPORT_ERROR_CLASSES = new Set([
|
|
15
|
+
"cancelled", "timeout", "authentication_failed", "authorization_denied", "policy_denied",
|
|
16
|
+
"invalid_request", "not_found", "conflict", "limit_exceeded", "permission_denied",
|
|
17
|
+
"path_boundary", "network_error", "protocol_error", "unavailable", "integrity_error",
|
|
18
|
+
"execution_failed", "internal_error",
|
|
19
|
+
]);
|
|
20
|
+
const CLOSE_CATEGORIES = new Set([
|
|
21
|
+
"connection_interrupted",
|
|
22
|
+
"local_authority_revocation_retry",
|
|
23
|
+
"relay_restarting_or_unavailable",
|
|
24
|
+
"relay_policy_rejected",
|
|
25
|
+
"relay_internal_error",
|
|
26
|
+
"relay_protocol_mismatch",
|
|
27
|
+
"relay_authentication_failed",
|
|
28
|
+
"relay_connect_timeout",
|
|
29
|
+
"relay_handshake_timeout",
|
|
30
|
+
"relay_readiness_timeout",
|
|
31
|
+
"relay_heartbeat_timeout",
|
|
32
|
+
"relay_transport_timeout",
|
|
33
|
+
"relay_transport_error",
|
|
34
|
+
"relay_protocol_error",
|
|
35
|
+
"relay_proxy_configuration",
|
|
36
|
+
"invalid_transport_payload",
|
|
37
|
+
"message_too_large",
|
|
38
|
+
"normal_close",
|
|
39
|
+
"unexpected_close",
|
|
40
|
+
"superseded",
|
|
41
|
+
]);
|
|
42
|
+
|
|
43
|
+
export interface DaemonRelayDiagnostics {
|
|
44
|
+
schema_version: 1;
|
|
45
|
+
transport: string;
|
|
46
|
+
network_route: string;
|
|
47
|
+
last_connect_stage: string;
|
|
48
|
+
last_connect_duration_ms: number;
|
|
49
|
+
last_connect_http_status: number | null;
|
|
50
|
+
outage_count: number;
|
|
51
|
+
outage_active: boolean;
|
|
52
|
+
outage_started_at: string | null;
|
|
53
|
+
outage_duration_ms: number;
|
|
54
|
+
outage_attempts: number;
|
|
55
|
+
last_close_category: string | null;
|
|
56
|
+
last_close_code: number | null;
|
|
57
|
+
last_transport_error_class: string | null;
|
|
58
|
+
last_disconnected_at: string | null;
|
|
59
|
+
previous_ready_duration_ms: number;
|
|
60
|
+
previous_ready_inbound_silence_ms: number;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function sanitizeDaemonRelayDiagnostics(value: unknown): DaemonRelayDiagnostics | undefined {
|
|
64
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
|
|
65
|
+
const candidate = value as Record<string, unknown>;
|
|
66
|
+
if (candidate.schema_version !== 1) return undefined;
|
|
67
|
+
return {
|
|
68
|
+
schema_version: 1,
|
|
69
|
+
transport: enumText(candidate.transport, TRANSPORTS, "websocket"),
|
|
70
|
+
network_route: enumText(candidate.network_route, NETWORK_ROUTES, "unresolved"),
|
|
71
|
+
last_connect_stage: enumText(candidate.last_connect_stage, CONNECT_STAGES, "idle"),
|
|
72
|
+
last_connect_duration_ms: boundedInteger(candidate.last_connect_duration_ms, 0, 10 * 60_000, 0),
|
|
73
|
+
last_connect_http_status: nullableInteger(candidate.last_connect_http_status, 100, 599),
|
|
74
|
+
outage_count: boundedInteger(candidate.outage_count, 0, 1_000_000_000, 0),
|
|
75
|
+
outage_active: candidate.outage_active === true,
|
|
76
|
+
outage_started_at: timestamp(candidate.outage_started_at),
|
|
77
|
+
outage_duration_ms: boundedInteger(candidate.outage_duration_ms, 0, 31 * 24 * 60 * 60_000, 0),
|
|
78
|
+
outage_attempts: boundedInteger(candidate.outage_attempts, 0, 1_000_000, 0),
|
|
79
|
+
last_close_category: nullableEnum(candidate.last_close_category, CLOSE_CATEGORIES),
|
|
80
|
+
last_close_code: nullableInteger(candidate.last_close_code, 0, 4999),
|
|
81
|
+
last_transport_error_class: nullableEnum(candidate.last_transport_error_class, TRANSPORT_ERROR_CLASSES),
|
|
82
|
+
last_disconnected_at: timestamp(candidate.last_disconnected_at),
|
|
83
|
+
previous_ready_duration_ms: boundedInteger(candidate.previous_ready_duration_ms, 0, 365 * 24 * 60 * 60_000, 0),
|
|
84
|
+
previous_ready_inbound_silence_ms: boundedInteger(candidate.previous_ready_inbound_silence_ms, 0, 31 * 24 * 60 * 60_000, 0),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function enumText(value: unknown, allowed: Set<string>, fallback: string): string {
|
|
89
|
+
const text = typeof value === "string" ? value : "";
|
|
90
|
+
return allowed.has(text) ? text : fallback;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function nullableEnum(value: unknown, allowed: Set<string>): string | null {
|
|
94
|
+
const text = typeof value === "string" ? value : "";
|
|
95
|
+
return allowed.has(text) ? text : null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function relayDiagnosticsAfterReady(
|
|
99
|
+
value: DaemonRelayDiagnostics | undefined,
|
|
100
|
+
readyAt?: string,
|
|
101
|
+
): DaemonRelayDiagnostics | undefined {
|
|
102
|
+
if (!value) return undefined;
|
|
103
|
+
const started = Date.parse(value.outage_started_at ?? "");
|
|
104
|
+
const ready = Date.parse(readyAt ?? "");
|
|
105
|
+
const elapsed = Number.isFinite(started) && Number.isFinite(ready) ? Math.max(0, ready - started) : 0;
|
|
106
|
+
return {
|
|
107
|
+
...value,
|
|
108
|
+
outage_active: false,
|
|
109
|
+
outage_duration_ms: Math.min(31 * 24 * 60 * 60_000, Math.max(value.outage_duration_ms, elapsed)),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function timestamp(value: unknown): string | null {
|
|
114
|
+
const text = sanitizeMetadataText(value, 64);
|
|
115
|
+
const parsed = text ? Date.parse(text) : Number.NaN;
|
|
116
|
+
return Number.isFinite(parsed) ? new Date(parsed).toISOString() : null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function nullableInteger(value: unknown, minimum: number, maximum: number): number | null {
|
|
120
|
+
const number = Number(value);
|
|
121
|
+
return Number.isSafeInteger(number) && number >= minimum && number <= maximum ? number : null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function boundedInteger(value: unknown, minimum: number, maximum: number, fallback: number): number {
|
|
125
|
+
const number = Number(value);
|
|
126
|
+
return Number.isSafeInteger(number) && number >= minimum && number <= maximum ? number : fallback;
|
|
127
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { sanitizeDaemonChallengeAttachment } from "./daemon-auth.ts";
|
|
2
|
+
import type { DaemonRole } from "./daemon-liveness.ts";
|
|
3
|
+
import { sanitizeDaemonRelayDiagnostics, type DaemonRelayDiagnostics } from "./daemon-relay-diagnostics.ts";
|
|
4
|
+
import { sanitizeMetadataText } from "./http.ts";
|
|
5
|
+
import { sanitizeDaemonPolicy, sanitizeDaemonTools, type DaemonPolicy } from "./policy.ts";
|
|
6
|
+
|
|
7
|
+
export interface DaemonAttachment {
|
|
8
|
+
role: DaemonRole;
|
|
9
|
+
connectedAt: string;
|
|
10
|
+
lastSeenAt?: string;
|
|
11
|
+
probeId?: string;
|
|
12
|
+
instanceId?: string;
|
|
13
|
+
connectionId?: string;
|
|
14
|
+
policy?: DaemonPolicy;
|
|
15
|
+
tools?: string[];
|
|
16
|
+
relayDiagnostics?: DaemonRelayDiagnostics;
|
|
17
|
+
authChallenge?: string;
|
|
18
|
+
authIssuedAt?: number;
|
|
19
|
+
authExpiresAt?: number;
|
|
20
|
+
workerOrigin?: string;
|
|
21
|
+
authSessionPublicKeyJson?: string;
|
|
22
|
+
authSessionKeyId?: string;
|
|
23
|
+
authCertificateExpiresAt?: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function sanitizeDaemonAttachment(value: unknown): DaemonAttachment | undefined {
|
|
27
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
|
|
28
|
+
const candidate = value as Partial<DaemonAttachment>;
|
|
29
|
+
if (!["candidate", "probing", "expired", "daemon"].includes(String(candidate.role))) return undefined;
|
|
30
|
+
const policy = sanitizeDaemonPolicy(candidate.policy);
|
|
31
|
+
return {
|
|
32
|
+
role: candidate.role as DaemonRole,
|
|
33
|
+
connectedAt: sanitizeMetadataText(candidate.connectedAt, 64) ?? "",
|
|
34
|
+
lastSeenAt: sanitizeMetadataText(candidate.lastSeenAt, 64),
|
|
35
|
+
probeId: sanitizeProbeId(candidate.probeId),
|
|
36
|
+
instanceId: sanitizeDaemonInstanceId(candidate.instanceId),
|
|
37
|
+
connectionId: sanitizeConnectionId(candidate.connectionId),
|
|
38
|
+
policy,
|
|
39
|
+
tools: sanitizeDaemonTools(candidate.tools, policy),
|
|
40
|
+
relayDiagnostics: sanitizeDaemonRelayDiagnostics(candidate.relayDiagnostics),
|
|
41
|
+
...sanitizeDaemonChallengeAttachment(candidate as Record<string, unknown>),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function sanitizeProbeId(value: unknown): string | undefined {
|
|
46
|
+
return typeof value === "string" && /^probe_[A-Za-z0-9_-]{8,240}$/.test(value) ? value : undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function sanitizeDaemonInstanceId(value: unknown): string | undefined {
|
|
50
|
+
return typeof value === "string" && /^daemon_[A-Za-z0-9_-]{16,96}$/.test(value) ? value : undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function sanitizeConnectionId(value: unknown): string | undefined {
|
|
54
|
+
return typeof value === "string" && /^connection_[A-Za-z0-9_-]{43}$/.test(value) ? value : undefined;
|
|
55
|
+
}
|