@sleep2agi/commhub-server 0.9.0-preview.25 → 0.9.0-preview.251
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +129 -9
- package/bin/commhub.ts +93 -9
- package/build-info.json +7 -0
- package/package.json +10 -8
- package/src/ack-create-request-transport.test.ts +182 -0
- package/src/ack-create-request.test.ts +52 -8
- package/src/admin-delete-network-http.test.ts +228 -0
- package/src/admin-networks-http.test.ts +88 -0
- package/src/adoption-candidates-http.test.ts +140 -0
- package/src/adoption-candidates.ts +158 -0
- package/src/agent-access-batch.test.ts +210 -0
- package/src/agent-access.ts +487 -0
- package/src/agent-acl-groups-http.test.ts +247 -0
- package/src/agent-acl-http.test.ts +675 -0
- package/src/agent-acl-slice3-http.test.ts +717 -0
- package/src/agent-department-http.test.ts +215 -0
- package/src/agent-groups-perf.test.ts +223 -0
- package/src/agent-notice.ts +48 -0
- package/src/agent-team-whoami-http.test.ts +98 -0
- package/src/agent-teams-http.test.ts +330 -0
- package/src/agent-teams.ts +244 -0
- package/src/alias-baseline-2571.test.ts +162 -0
- package/src/alias-baseline.ts +113 -0
- package/src/alias-filter.test.ts +72 -0
- package/src/alias-filter.ts +42 -0
- package/src/api-host-supervisors-can-create-nodes.test.ts +350 -0
- package/src/api-host-supervisors-fallback.test.ts +6 -7
- package/src/api-host-supervisors-runtime-readiness.test.ts +195 -0
- package/src/api-nodes-shape.test.ts +95 -4
- package/src/auth-password-change-cli-tokens-http.test.ts +423 -0
- package/src/auth-sessions-http.test.ts +260 -0
- package/src/auth-validate.test.ts +5 -1
- package/src/auth-weak-password-login.test.ts +176 -0
- package/src/auth.ts +609 -51
- package/src/avatar-validate.test.ts +52 -2
- package/src/avatar-validate.ts +35 -1
- package/src/backfill-node-ids-2664.test.ts +128 -0
- package/src/binding-generation-http.test.ts +132 -0
- package/src/blocked-exit-on-reply.test.ts +122 -0
- package/src/broadcast-fanout-2721.test.ts +149 -0
- package/src/broadcast-fanout.ts +117 -0
- package/src/build-info.test.ts +60 -0
- package/src/build-info.ts +85 -0
- package/src/chain-attempt-dedup.test.ts +447 -0
- package/src/clip.test.ts +36 -0
- package/src/clip.ts +48 -0
- package/src/clone-node.test.ts +262 -0
- package/src/clone-node.ts +289 -0
- package/src/codex-config-flags.ts +77 -0
- package/src/codex-fork-contract.ts +23 -0
- package/src/codex-fork-migration.test.ts +38 -0
- package/src/commhub-bin-args.test.ts +85 -0
- package/src/config-apply-validate.test.ts +188 -1
- package/src/config-apply-validate.ts +222 -3
- package/src/cors-origins.test.ts +73 -0
- package/src/create-node-blocked-gate.test.ts +356 -0
- package/src/create-node-network-id.test.ts +91 -0
- package/src/create-node-tool-schema.test.ts +49 -0
- package/src/create-node-unicode-name.test.ts +126 -0
- package/src/create-node-validate.test.ts +199 -7
- package/src/create-node-validate.ts +232 -15
- package/src/create-node-workdir.test.ts +298 -0
- package/src/create-node.ts +88 -23
- package/src/daemon-provider-snapshots.test.ts +445 -0
- package/src/daemon-provider-snapshots.ts +327 -0
- package/src/daemon-token-owner-http.test.ts +292 -0
- package/src/dashboard-slash-routing-http.test.ts +109 -0
- package/src/db-adapter-guard.test.ts +148 -0
- package/src/db-adapter-translate.test.ts +128 -0
- package/src/db-adapter.ts +377 -91
- package/src/db-timestamp.test.ts +38 -0
- package/src/db-timestamp.ts +15 -0
- package/src/db.ts +1264 -153
- package/src/delivered-without-push.test.ts +140 -0
- package/src/department-groups-http.test.ts +209 -0
- package/src/department-groups-sync-http.test.ts +328 -0
- package/src/department-groups.ts +434 -0
- package/src/department-heads-http.test.ts +350 -0
- package/src/department-heads.ts +226 -0
- package/src/departments-agent-count.test.ts +132 -0
- package/src/departments-http.test.ts +185 -0
- package/src/departments-mcp.test.ts +128 -0
- package/src/departments.ts +318 -0
- package/src/desktop-message-seam.test.ts +150 -0
- package/src/desktop-message.test.ts +368 -0
- package/src/dm-file-scope-http.test.ts +178 -0
- package/src/empty-node-sweeper.test.ts +104 -0
- package/src/empty-node-sweeper.ts +88 -0
- package/src/env-transport-gate-http.test.ts +240 -0
- package/src/env-transport-gate.ts +80 -0
- package/src/env-transport-policy.ts +56 -0
- package/src/etag-of.test.ts +57 -0
- package/src/event-loop-lag.test.ts +180 -0
- package/src/event-loop-lag.ts +201 -0
- package/src/expiry-counters.test.ts +141 -0
- package/src/expiry-counters.ts +112 -0
- package/src/ext-token-shared.test.ts +60 -0
- package/src/ext-validation-regression.test.ts +131 -0
- package/src/external-schedule-edits-http.test.ts +321 -0
- package/src/external-schedule-edits-timestamps.test.ts +64 -0
- package/src/external-schedule-edits.ts +327 -0
- package/src/file-download-authz-dev-open.test.ts +1 -1
- package/src/file-download-authz.test.ts +75 -20
- package/src/file-download-date-bucket.test.ts +8 -26
- package/src/file-network-scope-dev-open.test.ts +97 -0
- package/src/file-network-scope.test.ts +732 -0
- package/src/group-membership-events-http.test.ts +264 -0
- package/src/group-messages-http.test.ts +483 -0
- package/src/group-messages.ts +297 -0
- package/src/health-anon-readers.ts +60 -0
- package/src/health-host-disk-2756.test.ts +71 -0
- package/src/health-redaction.test.ts +1541 -7
- package/src/host-supervisor-identity-http.test.ts +152 -0
- package/src/host-supervisor-identity.ts +92 -0
- package/src/host-supervisors-daemon-version.test.ts +137 -0
- package/src/http-gzip.test.ts +94 -0
- package/src/http-gzip.ts +112 -0
- package/src/hub-catalog-read-http.test.ts +641 -0
- package/src/hub-provider-config.test.ts +166 -0
- package/src/hub-provider-config.ts +202 -0
- package/src/hub-timestamp.test.ts +38 -0
- package/src/hub-timestamp.ts +27 -0
- package/src/human-dm.ts +215 -0
- package/src/human-inbox.ts +42 -0
- package/src/inbox-caller.ts +49 -0
- package/src/inbox-count-sse.test.ts +310 -0
- package/src/inbox-count.ts +29 -0
- package/src/index.ts +6 -2
- package/src/license-state.test.ts +56 -0
- package/src/license-state.ts +48 -0
- package/src/list-host-supervisors.test.ts +15 -4
- package/src/mcp-context-cost-http.test.ts +127 -0
- package/src/mcp-inflight.test.ts +92 -0
- package/src/mcp-inflight.ts +38 -0
- package/src/mcp-projects-events-http.test.ts +165 -0
- package/src/mcp-route-label-http.test.ts +120 -0
- package/src/mcp-write-network-resolution.test.ts +435 -0
- package/src/member-audit-network-id-http.test.ts +93 -0
- package/src/member-presence-http.test.ts +210 -0
- package/src/messages-ack-by-agent-http.test.ts +142 -0
- package/src/migrate-sqlite-to-pg.test.ts +47 -0
- package/src/migrate-sqlite-to-pg.ts +263 -0
- package/src/model-auth-notify-http.test.ts +181 -0
- package/src/model-auth-notify.ts +116 -0
- package/src/network-name-http.test.ts +75 -0
- package/src/network-rules-http.test.ts +169 -0
- package/src/network-rules.ts +129 -0
- package/src/network-scope.ts +276 -0
- package/src/network-token-membership-http.test.ts +186 -0
- package/src/node-alias-control.ts +12 -0
- package/src/node-attrs.test.ts +47 -2
- package/src/node-caller-identity-http.test.ts +1108 -0
- package/src/node-create-request-status-http.test.ts +105 -0
- package/src/node-daemon-bindings.test.ts +132 -0
- package/src/node-daemon-bindings.ts +135 -0
- package/src/node-failure-alert.test.ts +150 -0
- package/src/node-failure-alert.ts +177 -0
- package/src/node-files-transport.test.ts +347 -0
- package/src/node-health-codex-login.test.ts +120 -0
- package/src/node-health-dispatch-http.test.ts +373 -0
- package/src/node-health-guard.ts +121 -0
- package/src/node-health-passthrough.test.ts +115 -0
- package/src/node-health-store.ts +101 -0
- package/src/node-id-alias.test.ts +190 -0
- package/src/node-identity-conflict-http.test.ts +269 -0
- package/src/node-identity-conflict.ts +142 -0
- package/src/node-identity-mismatch.test.ts +74 -0
- package/src/node-in-flight-tasks.ts +31 -0
- package/src/node-lifecycle-controllable.test.ts +64 -0
- package/src/node-lifecycle-controllable.ts +53 -0
- package/src/node-lifecycle-read-http.test.ts +257 -0
- package/src/node-lifecycle-read.ts +124 -0
- package/src/node-logs-transport.test.ts +263 -0
- package/src/node-peer-allowlist.test.ts +114 -0
- package/src/node-permissions-http.test.ts +728 -0
- package/src/node-permissions.ts +494 -0
- package/src/node-request-retention.test.ts +281 -0
- package/src/node-request-retention.ts +120 -0
- package/src/node-secret-rest-http.test.ts +190 -0
- package/src/node-serviceability.test.ts +86 -0
- package/src/node-serviceability.ts +124 -0
- package/src/node-session-control-http.test.ts +310 -0
- package/src/node-session-control.ts +258 -0
- package/src/node-skills-transport.test.ts +212 -0
- package/src/node-token-identity.ts +35 -0
- package/src/node-token-ownership.ts +51 -0
- package/src/node-token-privilege-http.test.ts +158 -0
- package/src/not-found-api-http.test.ts +67 -0
- package/src/observer-avatar-http.test.ts +103 -10
- package/src/observer-push.test.ts +76 -0
- package/src/opencode-catalog-report-http.test.ts +139 -0
- package/src/opencode-v2-catalog.test.ts +268 -0
- package/src/opencode-v2-catalog.ts +356 -0
- package/src/package-version-consistency.test.ts +25 -0
- package/src/parent-source-marker.test.ts +50 -0
- package/src/password-network-tokens.ts +25 -0
- package/src/peer-reply-atomic.test.ts +462 -0
- package/src/peer-reply-capability.test.ts +70 -0
- package/src/pg-worker.ts +87 -0
- package/src/poll-pacing.test.ts +74 -0
- package/src/poll-pacing.ts +123 -0
- package/src/project-dir-warn-clear-http.test.ts +132 -0
- package/src/project-viewer-can-http.test.ts +116 -0
- package/src/provider-tools-shape.test.ts +150 -0
- package/src/push.test.ts +20 -10
- package/src/push.ts +373 -9
- package/src/redact-tokens.test.ts +86 -0
- package/src/redact-tokens.ts +43 -0
- package/src/rejection-counters.test.ts +72 -0
- package/src/rejection-counters.ts +173 -0
- package/src/rename.ts +38 -10
- package/src/reply-target-derivation.test.ts +96 -0
- package/src/report-status-handover-capabilities-http.test.ts +124 -0
- package/src/report-status-host-schema.test.ts +235 -0
- package/src/report-status-resume-id-handover.test.ts +184 -0
- package/src/report-status-secret-keys.test.ts +140 -0
- package/src/require-explicit-test-db.ts +9 -0
- package/src/requirement-due-reminders-http.test.ts +620 -0
- package/src/requirement-due-reminders.ts +416 -0
- package/src/requirement-events-http.test.ts +318 -0
- package/src/requirement-events.test.ts +45 -0
- package/src/requirement-events.ts +144 -0
- package/src/requirement-notify.ts +165 -0
- package/src/requirement-participant-notify-http.test.ts +309 -0
- package/src/requirement-tag-ops-http.test.ts +454 -0
- package/src/requirement-tag-tree-http.test.ts +275 -0
- package/src/requirement-tag-tree.ts +127 -0
- package/src/requirement-tags.ts +102 -0
- package/src/requirements-abandoned-http.test.ts +236 -0
- package/src/requirements-assignee-drift.test.ts +154 -0
- package/src/requirements-comment-http.test.ts +174 -0
- package/src/requirements-completed-at-migration.test.ts +70 -0
- package/src/requirements-errors-mcp-http.test.ts +164 -0
- package/src/requirements-errors.ts +158 -0
- package/src/requirements-etag-http.test.ts +91 -0
- package/src/requirements-http.test.ts +709 -0
- package/src/requirements-last-event-http.test.ts +349 -0
- package/src/requirements-list-mcp-slim-http.test.ts +155 -0
- package/src/requirements-list-slim-http.test.ts +324 -0
- package/src/requirements-mcp.test.ts +164 -0
- package/src/requirements-migrate.ts +283 -0
- package/src/requirements-owner-strict-mcp-http.test.ts +145 -0
- package/src/requirements-people-http.test.ts +114 -0
- package/src/requirements-people-mcp-http.test.ts +231 -0
- package/src/requirements-people.ts +196 -0
- package/src/requirements-priority-lowest.test.ts +114 -0
- package/src/requirements-projects-network-echo.test.ts +57 -0
- package/src/requirements-projects-parent-http.test.ts +123 -0
- package/src/requirements-search-http.test.ts +182 -0
- package/src/requirements-search.test.ts +32 -0
- package/src/requirements-search.ts +145 -0
- package/src/requirements-seq-http.test.ts +193 -0
- package/src/requirements-seq-migration.test.ts +63 -0
- package/src/requirements-start-date.test.ts +125 -0
- package/src/requirements-stats-http.test.ts +357 -0
- package/src/requirements-stats.ts +195 -0
- package/src/requirements-status-participants-http.test.ts +222 -0
- package/src/requirements-upgrade-e2e.test.ts +126 -0
- package/src/requirements-upgrade-migration.test.ts +98 -0
- package/src/requirements.ts +2205 -0
- package/src/resolve-port.test.ts +57 -0
- package/src/resolve-port.ts +44 -0
- package/src/rest-alias-network-fallback-http.test.ts +123 -0
- package/src/rest-alias-network-fallback.test.ts +60 -0
- package/src/rest-alias-network-fallback.ts +65 -0
- package/src/rest-explicit-columns-http.test.ts +266 -0
- package/src/rest-identity.test.ts +130 -0
- package/src/rest-identity.ts +141 -0
- package/src/rest-projections.ts +72 -0
- package/src/rest-read-network-real-chain.test.ts +97 -0
- package/src/rest-write-network-resolution.test.ts +133 -0
- package/src/restart-readiness-http.test.ts +121 -0
- package/src/restricted-files.ts +76 -0
- package/src/retention-first-sweep-quiet.test.ts +31 -0
- package/src/retention.test.ts +100 -40
- package/src/retention.ts +423 -104
- package/src/route-callers-http.test.ts +120 -0
- package/src/route-timing.test.ts +218 -0
- package/src/route-timing.ts +262 -0
- package/src/rules-file-duplicate-alias.test.ts +234 -0
- package/src/rules-file-session-target.test.ts +207 -0
- package/src/rules-file-transport.test.ts +237 -0
- package/src/runtime-label.test.ts +22 -0
- package/src/runtime-label.ts +21 -0
- package/src/runtime-readiness.ts +90 -0
- package/src/schedule-agent-mcp-http.test.ts +475 -0
- package/src/schedule-agent.ts +252 -0
- package/src/schedule-starved.test.ts +92 -0
- package/src/schedule-starved.ts +114 -0
- package/src/scheduled-backoff-http.test.ts +203 -0
- package/src/scheduled-backoff.ts +64 -0
- package/src/scheduled-doorbell-nodeless-session.test.ts +134 -0
- package/src/scheduled-failure-backoff-2622.test.ts +145 -0
- package/src/scheduled-failure-code.test.ts +73 -0
- package/src/scheduled-failure-code.ts +105 -0
- package/src/scheduled-failures-http.test.ts +279 -0
- package/src/scheduled-failures.ts +425 -0
- package/src/scheduled-noprogress-backoff-2622.test.ts +163 -0
- package/src/scheduled-offline-backoff-probe.test.ts +56 -0
- package/src/scheduled-offline.test.ts +131 -0
- package/src/scheduled-offline.ts +66 -0
- package/src/scheduled-outcome-guidance-http.test.ts +139 -0
- package/src/scheduled-outcome-guidance.ts +9 -0
- package/src/scheduled-reply-unread-http.test.ts +212 -0
- package/src/scheduled-requirement-wake-http.test.ts +285 -0
- package/src/scheduled-requirement-wake.ts +125 -0
- package/src/scheduled-run-terminal.test.ts +290 -0
- package/src/scheduled-skip-blocking-task.test.ts +120 -0
- package/src/scheduled-skip-path-backoff-2622.test.ts +98 -0
- package/src/scheduled-stuck-http.test.ts +316 -0
- package/src/scheduled-stuck-notice-cap.test.ts +80 -0
- package/src/scheduled-stuck.ts +219 -0
- package/src/scheduled-tasks-http.test.ts +679 -0
- package/src/scheduled-tasks.ts +959 -0
- package/src/scheduled-work-outcome-http.test.ts +154 -0
- package/src/scheduled-work-outcome.ts +50 -0
- package/src/scoped-secrets.ts +141 -0
- package/src/secret-echo-negative-control.test.ts +118 -0
- package/src/send-dedup-request-id.test.ts +229 -0
- package/src/send-reply-agent-warning.test.ts +38 -38
- package/src/send-reply-alias-optional.test.ts +131 -0
- package/src/send-reply-attachments.test.ts +458 -0
- package/src/send_dedup.test.ts +54 -0
- package/src/send_dedup.ts +52 -6
- package/src/serve-error.test.ts +73 -0
- package/src/serve-error.ts +56 -0
- package/src/server.ts +5134 -503
- package/src/session-task-on-dispatch.ts +14 -0
- package/src/session-token-ownership-http.test.ts +122 -0
- package/src/shared/codex-inventory.ts +41 -0
- package/src/shared/daemon-provider-login.ts +31 -0
- package/src/shared/daemon-provider-snapshot.ts +52 -0
- package/src/shared/external-schedule-contract.ts +60 -0
- package/src/shared/node-name.test.ts +62 -0
- package/src/shared/node-name.ts +161 -0
- package/src/shared/probe-host-allowlist-drift.test.ts +45 -0
- package/src/shared/provider-application.ts +29 -0
- package/src/shared/provider-collection.ts +14 -0
- package/src/shared/provider-initialization.ts +8 -0
- package/src/shared/reserved-env-drift.test.ts +10 -0
- package/src/shared/scoped-secret-contract.ts +5 -0
- package/src/shared/task-content-limit-drift.test.ts +28 -0
- package/src/shared/task-content-limit.ts +25 -0
- package/src/side-thread-command-transport.test.ts +312 -0
- package/src/side-thread-command-transport.ts +318 -0
- package/src/side-thread-http-integration.test.ts +233 -0
- package/src/side-thread-http.ts +504 -0
- package/src/side-thread-production.test.ts +72 -0
- package/src/side-thread-production.ts +118 -0
- package/src/side-thread.test.ts +1067 -0
- package/src/side-thread.ts +2499 -0
- package/src/skillhub-http.test.ts +142 -0
- package/src/sqlite-close-checkpoint.test.ts +78 -0
- package/src/start-node.test.ts +265 -0
- package/src/stats-versions-http.test.ts +65 -0
- package/src/status-alias-filter-http.test.ts +106 -0
- package/src/status-alias-resolver-http.test.ts +163 -0
- package/src/status-alias-resolver.ts +19 -0
- package/src/status-node-id-filter-http.test.ts +161 -0
- package/src/status-read-cache-http.test.ts +229 -0
- package/src/status-read-cache.ts +129 -0
- package/src/stop-delete-node.test.ts +567 -1
- package/src/task-access-http.test.ts +402 -0
- package/src/task-access.ts +420 -0
- package/src/task-activity.ts +60 -0
- package/src/task-auth-origin-http.test.ts +93 -0
- package/src/task-auth-origin.test.ts +24 -0
- package/src/task-auth-origin.ts +7 -0
- package/src/task-consumption.test.ts +498 -0
- package/src/task-diagnostic-http.test.ts +149 -0
- package/src/task-diagnostic.test.ts +115 -0
- package/src/task-diagnostic.ts +118 -0
- package/src/task-expiry-notice-http.test.ts +298 -0
- package/src/task-expiry-notice.ts +318 -0
- package/src/task-idempotency-mcp.test.ts +142 -0
- package/src/task-idempotency.test.ts +76 -0
- package/src/task-idempotency.ts +81 -0
- package/src/task-late-replies.ts +54 -0
- package/src/task-late-reply-http.test.ts +227 -0
- package/src/task-lifecycle-watcher-busy.test.ts +114 -0
- package/src/task-lifecycle-watcher.test.ts +209 -0
- package/src/task-lifecycle-watcher.ts +109 -0
- package/src/task-network-resolution-http.test.ts +140 -0
- package/src/task-offline-delivery-2668.test.ts +119 -0
- package/src/task-offline-delivery.ts +81 -0
- package/src/task-orphan-http.test.ts +287 -0
- package/src/task-orphan.ts +119 -0
- package/src/task-queue-ahead-http.test.ts +309 -0
- package/src/task-queue-ahead-scale.test.ts +236 -0
- package/src/task-queue-ahead.ts +186 -0
- package/src/task-rejection-log-http.test.ts +160 -0
- package/src/task-rejection-log.ts +61 -0
- package/src/task-sender-http.test.ts +143 -0
- package/src/task-stale-open-http.test.ts +292 -0
- package/src/task-stale-open.ts +103 -0
- package/src/tasks-pagination-http.test.ts +141 -0
- package/src/team-rules-status-http.test.ts +90 -0
- package/src/team-rules-status.test.ts +59 -0
- package/src/team-rules-status.ts +72 -0
- package/src/tmux-allowlist.test.ts +48 -0
- package/src/tool-audience-http.test.ts +230 -0
- package/src/tool-audience.ts +127 -0
- package/src/tools-list-json-schema.test.ts +94 -0
- package/src/tools.ts +4326 -420
- package/src/upload-early-reject-guard.test.ts +155 -0
- package/src/uploads-http.test.ts +86 -7
- package/src/uploads.ts +38 -4
- package/src/user-avatar-http.test.ts +74 -0
- package/src/user-avatar-image.ts +160 -0
- package/src/user-avatar-migration.test.ts +25 -0
- package/src/user-avatar-upload-http.test.ts +348 -0
- package/src/user-inbox-by-agent-http.test.ts +165 -0
- package/src/user-inbox-netid-notnull.test.ts +122 -0
- package/src/user-inbox-read-http.test.ts +223 -0
- package/src/user-inbox-schema.test.ts +54 -0
- package/src/version-stats.test.ts +66 -0
- package/src/version-stats.ts +109 -0
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
// #906: human-requested readback of ONE daemon's private Provider metadata.
|
|
2
|
+
// Hub registry is not a fallback. No credential values enter this protocol.
|
|
3
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
4
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
5
|
+
import { db } from "./db";
|
|
6
|
+
import { getUserNetworkRole } from "./auth";
|
|
7
|
+
import { validateDaemonProviderSnapshot } from "./shared/daemon-provider-snapshot";
|
|
8
|
+
import { encryptSecret, decryptSecret, VaultError } from './vault';
|
|
9
|
+
import { buildControllableMap } from './node-lifecycle-controllable';
|
|
10
|
+
import { validProviderApplicationSelection, validProviderApplicationReceipt, type ProviderApplicationSelection } from './shared/provider-application';
|
|
11
|
+
import { validLoginIntent, validLoginReceipt } from './shared/daemon-provider-login';
|
|
12
|
+
import { validProviderCollection, type ProviderCollection } from './shared/provider-collection';
|
|
13
|
+
|
|
14
|
+
export function providerScanTargets(networkId: string, daemonId: string) {
|
|
15
|
+
const ownership = buildControllableMap(
|
|
16
|
+
db.all<{ request_id: string; daemon_node_id: string }>('SELECT request_id,daemon_node_id FROM node_create_requests WHERE network_id=?1', networkId),
|
|
17
|
+
db.all<{ node_id: string; daemon_node_id: string }>('SELECT node_id,daemon_node_id FROM node_daemon_bindings WHERE network_id=?1 AND status=\'active\'', networkId));
|
|
18
|
+
const targets = db.all<{ node_id: string; alias: string }>('SELECT node_id,alias FROM nodes WHERE network_id=?1 ORDER BY node_id', networkId)
|
|
19
|
+
.filter(n => ownership.get(n.node_id) === daemonId && typeof n.alias === 'string');
|
|
20
|
+
if (targets.length > 200) throw Error('inventory_limit'); // no silent incomplete denominator
|
|
21
|
+
return targets;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type Human = { userId?: string | null; isNode: boolean; networkId: string | null };
|
|
25
|
+
type Daemon = { ok: false; error: string } | { ok: true; networkId: string; daemonNodeId: string; daemonAlias: string };
|
|
26
|
+
type Row = { request_id: string; network_id: string; daemon_node_id: string; status: string;
|
|
27
|
+
created_at: number; expires_at: number; observed_at: number | null; snapshot_json: string | null; error: string | null };
|
|
28
|
+
const denied = (error: string) => ({ ok: false as const, error });
|
|
29
|
+
function authorize(h: Human) {
|
|
30
|
+
if (h.isNode || !h.userId) return denied("user_token_required");
|
|
31
|
+
if (!h.networkId || !["owner", "admin"].includes(getUserNetworkRole(h.userId, h.networkId) || "")) return denied("provider_admin_required");
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
function daemonExists(networkId: string, nodeId: string): boolean {
|
|
35
|
+
const node = db.get<{ config_snapshot: string }>(
|
|
36
|
+
`SELECT n.config_snapshot FROM nodes n WHERE n.node_id=?1 AND n.network_id=?2
|
|
37
|
+
AND EXISTS (SELECT 1 FROM api_tokens t WHERE t.network_id=n.network_id
|
|
38
|
+
AND t.name='node:' || n.alias AND t.revoked_at IS NULL
|
|
39
|
+
AND (t.expires_at IS NULL OR t.expires_at > datetime('now')))`, nodeId, networkId);
|
|
40
|
+
try { return JSON.parse(node?.config_snapshot || "{}").role === "host_supervisor"; } catch { return false; }
|
|
41
|
+
}
|
|
42
|
+
function expire(networkId: string, daemonId?: string) {
|
|
43
|
+
// Device-code instructions are short-lived; do not leave them in audit history.
|
|
44
|
+
db.run(`UPDATE daemon_provider_snapshots SET status='expired', snapshot_json=NULL, error='request_expired'
|
|
45
|
+
WHERE network_id=?1 AND request_id LIKE 'dpl_%' AND expires_at <= ?2`, [networkId, Date.now()]);
|
|
46
|
+
db.run(`UPDATE daemon_provider_snapshots SET status='expired', error='request_expired'
|
|
47
|
+
WHERE network_id=?1 AND status='pending' AND expires_at <= ?2
|
|
48
|
+
AND (?3 IS NULL OR daemon_node_id=?3)`, [networkId, Date.now(), daemonId ?? null]);
|
|
49
|
+
db.run(`DELETE FROM daemon_provider_writes WHERE request_id IN
|
|
50
|
+
(SELECT request_id FROM daemon_provider_snapshots WHERE network_id=?1 AND status!='pending')`, [networkId]);
|
|
51
|
+
db.run(`DELETE FROM daemon_provider_applications WHERE request_id IN
|
|
52
|
+
(SELECT request_id FROM daemon_provider_snapshots WHERE network_id=?1 AND status!='pending')`, [networkId]);
|
|
53
|
+
db.run(`DELETE FROM daemon_provider_logins WHERE request_id IN
|
|
54
|
+
(SELECT request_id FROM daemon_provider_snapshots WHERE network_id=?1 AND status!='pending')`, [networkId]);
|
|
55
|
+
db.run(`DELETE FROM daemon_provider_snapshots WHERE network_id=?1 AND expires_at < ?2`, [networkId, Date.now() - 7 * 86400_000]);
|
|
56
|
+
}
|
|
57
|
+
/** Validate the complete public projection before encrypting the private input. */
|
|
58
|
+
function validateWrite(provider: any, networkId: string, daemonId: string) {
|
|
59
|
+
if (Buffer.byteLength(JSON.stringify(provider) || '') > 64 * 1024) throw new Error('invalid_provider');
|
|
60
|
+
const publicProvider = { ...provider, runtimes: provider?.runtimes?.map((r: any) => ({ ...r,
|
|
61
|
+
auth: r.auth?.map((a: any) => {
|
|
62
|
+
const { key, ...rest } = a;
|
|
63
|
+
if (a.kind === 'chatgpt' && key !== undefined) throw new Error('invalid_provider');
|
|
64
|
+
if (key !== undefined && (typeof key !== 'string' || !key || key.length > 8192 || /[\s\x00-\x1f\x7f]/.test(key))) throw new Error('invalid_provider');
|
|
65
|
+
// Only CLI write fields may be supplied; public-state flags are not writable.
|
|
66
|
+
if (Object.keys(a).some(k => !['id','kind','models','baseUrl','key','account_id'].includes(k))) throw new Error('invalid_provider');
|
|
67
|
+
// A bound login write must be confirmed present by its owning daemon;
|
|
68
|
+
// this projection is the expected receipt, not an independent Hub claim.
|
|
69
|
+
return { ...rest, credential_present: a.kind === 'api_key' || (a.kind === 'chatgpt' && a.account_id !== undefined), verification: 'not_checked', application: 'not_applied' };
|
|
70
|
+
}),
|
|
71
|
+
})) };
|
|
72
|
+
validateDaemonProviderSnapshot({ network_id: networkId, daemon_node_id: daemonId, source: 'daemon', revision: 0, providers: [publicProvider] }, networkId, daemonId);
|
|
73
|
+
return publicProvider;
|
|
74
|
+
}
|
|
75
|
+
export function requestProviderWrite(h: Human, daemonId: string, expectedRevision: number, provider: unknown) {
|
|
76
|
+
const refusal = authorize(h); if (refusal) return refusal;
|
|
77
|
+
if (!daemonExists(h.networkId!, daemonId)) return denied('daemon_not_found');
|
|
78
|
+
if (!Number.isSafeInteger(expectedRevision) || expectedRevision < 0 || expectedRevision >= Number.MAX_SAFE_INTEGER) return denied('invalid_revision');
|
|
79
|
+
try { validateWrite(provider, h.networkId!, daemonId); } catch { return denied('invalid_provider'); }
|
|
80
|
+
expire(h.networkId!, daemonId);
|
|
81
|
+
if (db.get('SELECT request_id FROM daemon_provider_snapshots WHERE network_id=?1 AND daemon_node_id=?2 AND status=\'pending\'', h.networkId!, daemonId)) return denied('provider_snapshot_busy');
|
|
82
|
+
try {
|
|
83
|
+
const enc = encryptSecret(JSON.stringify({ network_id: h.networkId, daemon_node_id: daemonId, provider }));
|
|
84
|
+
const requestId = `dps_${randomUUID()}`, now = Date.now();
|
|
85
|
+
db.transaction(() => {
|
|
86
|
+
db.run(`INSERT INTO daemon_provider_snapshots (request_id,network_id,daemon_node_id,requested_by,status,created_at,expires_at)
|
|
87
|
+
VALUES (?1,?2,?3,?4,'pending',?5,?6)`, [requestId, h.networkId!, daemonId, h.userId!, now, now + 120_000]);
|
|
88
|
+
db.run(`INSERT INTO daemon_provider_writes (request_id,expected_revision,ciphertext,iv,tag) VALUES (?1,?2,?3,?4,?5)`,
|
|
89
|
+
[requestId, expectedRevision, enc.ciphertext, enc.iv, enc.tag]);
|
|
90
|
+
});
|
|
91
|
+
return { ok: true as const, request_id: requestId, status: 'pending', expires_at: now + 120_000 };
|
|
92
|
+
} catch (error) { return denied(error instanceof VaultError ? error.code : 'provider_write_unavailable'); }
|
|
93
|
+
}
|
|
94
|
+
function privateWrite(requestId: string, networkId: string, daemonId: string) {
|
|
95
|
+
const row = db.get<{ expected_revision: number; ciphertext: any; iv: any; tag: any }>('SELECT * FROM daemon_provider_writes WHERE request_id=?1', requestId);
|
|
96
|
+
if (!row) return null;
|
|
97
|
+
const payload = JSON.parse(decryptSecret({ ciphertext: Buffer.from(row.ciphertext), iv: Buffer.from(row.iv), tag: Buffer.from(row.tag) }));
|
|
98
|
+
if (payload.network_id !== networkId || payload.daemon_node_id !== daemonId) throw new Error('scope_mismatch');
|
|
99
|
+
if (payload.operation === 'collect') {
|
|
100
|
+
if (!validProviderCollection(payload.selection)) throw Error('invalid_provider_collection');
|
|
101
|
+
return { expected_revision: row.expected_revision, operation: 'collect' as const, selection: payload.selection };
|
|
102
|
+
}
|
|
103
|
+
validateWrite(payload.provider, networkId, daemonId);
|
|
104
|
+
return { expected_revision: row.expected_revision, operation: 'put' as const, provider: payload.provider };
|
|
105
|
+
}
|
|
106
|
+
function boundCollectionTarget(networkId: string, daemonId: string, selection: ProviderCollection) {
|
|
107
|
+
return selection.node_id !== daemonId && providerScanTargets(networkId, daemonId)
|
|
108
|
+
.some(n => n.node_id === selection.node_id && n.alias === selection.alias);
|
|
109
|
+
}
|
|
110
|
+
export function requestProviderCollection(h: Human, daemonId: string, revision: number, selection: unknown) {
|
|
111
|
+
const refusal = authorize(h); if (refusal) return refusal;
|
|
112
|
+
if (!daemonExists(h.networkId!, daemonId)) return denied('daemon_not_found');
|
|
113
|
+
if (!Number.isSafeInteger(revision) || revision < 0 || revision >= Number.MAX_SAFE_INTEGER) return denied('invalid_revision');
|
|
114
|
+
if (!validProviderCollection(selection)) return denied('invalid_provider_collection');
|
|
115
|
+
try {
|
|
116
|
+
if (!boundCollectionTarget(h.networkId!, daemonId, selection)) return denied('collection_target_not_bound');
|
|
117
|
+
expire(h.networkId!, daemonId);
|
|
118
|
+
if (db.get("SELECT request_id FROM daemon_provider_snapshots WHERE network_id=?1 AND daemon_node_id=?2 AND status='pending'", h.networkId!, daemonId)) return denied('provider_snapshot_busy');
|
|
119
|
+
// Reuse the short-lived write queue, but no credential is supplied to Hub.
|
|
120
|
+
const enc = encryptSecret(JSON.stringify({ network_id: h.networkId, daemon_node_id: daemonId, operation: 'collect', selection }));
|
|
121
|
+
const requestId = `dps_${randomUUID()}`, now = Date.now();
|
|
122
|
+
db.transaction(() => {
|
|
123
|
+
db.run("INSERT INTO daemon_provider_snapshots (request_id,network_id,daemon_node_id,requested_by,status,created_at,expires_at) VALUES (?1,?2,?3,?4,'pending',?5,?6)",
|
|
124
|
+
[requestId, h.networkId!, daemonId, h.userId!, now, now + 120_000]);
|
|
125
|
+
db.run('INSERT INTO daemon_provider_writes (request_id,expected_revision,ciphertext,iv,tag) VALUES (?1,?2,?3,?4,?5)',
|
|
126
|
+
[requestId, revision, enc.ciphertext, enc.iv, enc.tag]);
|
|
127
|
+
});
|
|
128
|
+
return { ok: true as const, request_id: requestId, status: 'pending', expires_at: now + 120_000 };
|
|
129
|
+
} catch (error) { return denied(error instanceof VaultError ? error.code : 'provider_collection_unavailable'); }
|
|
130
|
+
}
|
|
131
|
+
function applicationIntent(requestId: string) {
|
|
132
|
+
const row = db.get<{ expected_revision: number; selection_json: string }>('SELECT * FROM daemon_provider_applications WHERE request_id=?1', requestId);
|
|
133
|
+
if (!row) return null;
|
|
134
|
+
const selection = JSON.parse(row.selection_json);
|
|
135
|
+
if (!validProviderApplicationSelection(selection)) throw Error('invalid_application');
|
|
136
|
+
return { expected_revision: row.expected_revision, selection };
|
|
137
|
+
}
|
|
138
|
+
function boundApplicationTarget(networkId: string, daemonId: string, selection: ProviderApplicationSelection) {
|
|
139
|
+
return selection.node_id !== daemonId && providerScanTargets(networkId, daemonId).find(n => n.node_id === selection.node_id);
|
|
140
|
+
}
|
|
141
|
+
export function requestProviderApplication(h: Human, daemonId: string, revision: number, selection: unknown) {
|
|
142
|
+
const refusal = authorize(h); if (refusal) return refusal;
|
|
143
|
+
if (!daemonExists(h.networkId!, daemonId)) return denied('daemon_not_found');
|
|
144
|
+
if (!Number.isSafeInteger(revision) || revision < 0) return denied('invalid_revision');
|
|
145
|
+
if (!validProviderApplicationSelection(selection)) return denied('invalid_application');
|
|
146
|
+
try {
|
|
147
|
+
if (!boundApplicationTarget(h.networkId!, daemonId, selection)) return denied('application_target_not_bound');
|
|
148
|
+
expire(h.networkId!, daemonId);
|
|
149
|
+
const requestId = `dpa_${randomUUID()}`, now = Date.now();
|
|
150
|
+
db.transaction(() => {
|
|
151
|
+
db.run(`INSERT INTO daemon_provider_snapshots (request_id,network_id,daemon_node_id,requested_by,status,created_at,expires_at)
|
|
152
|
+
VALUES (?1,?2,?3,?4,'pending',?5,?6)`, [requestId, h.networkId!, daemonId, h.userId!, now, now + 600_000]);
|
|
153
|
+
db.run('INSERT INTO daemon_provider_applications (request_id,expected_revision,selection_json) VALUES (?1,?2,?3)',
|
|
154
|
+
[requestId, revision, JSON.stringify(selection)]);
|
|
155
|
+
});
|
|
156
|
+
return { ok: true as const, request_id: requestId, status: 'pending', expires_at: now + 600_000 };
|
|
157
|
+
} catch { return denied('provider_application_unavailable'); }
|
|
158
|
+
}
|
|
159
|
+
export function requestProviderSnapshot(h: Human, daemonId: string) {
|
|
160
|
+
const refusal = authorize(h); if (refusal) return refusal;
|
|
161
|
+
if (!daemonExists(h.networkId!, daemonId)) return denied("daemon_not_found");
|
|
162
|
+
expire(h.networkId!, daemonId);
|
|
163
|
+
// Repeated refresh clicks share the pending request; pending never means ready.
|
|
164
|
+
const pending = db.get<Row>(`SELECT * FROM daemon_provider_snapshots WHERE network_id=?1 AND daemon_node_id=?2 AND status='pending' ORDER BY created_at DESC LIMIT 1`, h.networkId!, daemonId);
|
|
165
|
+
if (pending) return { ok: true as const, request_id: pending.request_id, status: "pending", expires_at: pending.expires_at };
|
|
166
|
+
const requestId = `dps_${randomUUID()}`, now = Date.now();
|
|
167
|
+
db.run(`INSERT INTO daemon_provider_snapshots
|
|
168
|
+
(request_id,network_id,daemon_node_id,requested_by,status,created_at,expires_at)
|
|
169
|
+
VALUES (?1,?2,?3,?4,'pending',?5,?6) ON CONFLICT DO NOTHING`, [requestId, h.networkId!, daemonId, h.userId!, now, now + 120_000]);
|
|
170
|
+
const accepted = db.get<Row>(`SELECT * FROM daemon_provider_snapshots WHERE network_id=?1 AND daemon_node_id=?2 AND status='pending'`, h.networkId!, daemonId);
|
|
171
|
+
if (!accepted) return denied("provider_snapshot_busy");
|
|
172
|
+
return { ok: true as const, request_id: accepted.request_id, status: "pending", expires_at: accepted.expires_at };
|
|
173
|
+
}
|
|
174
|
+
export function requestProviderLogin(h: Human, daemonId: string, intent: unknown) {
|
|
175
|
+
const refusal = authorize(h); if (refusal) return refusal;
|
|
176
|
+
if (!daemonExists(h.networkId!, daemonId)) return denied('daemon_not_found');
|
|
177
|
+
if (!validLoginIntent(intent)) return denied('invalid_login_intent');
|
|
178
|
+
expire(h.networkId!, daemonId);
|
|
179
|
+
const requestId = `dpl_${randomUUID()}`, now = Date.now();
|
|
180
|
+
try {
|
|
181
|
+
db.transaction(() => {
|
|
182
|
+
db.run(`INSERT INTO daemon_provider_snapshots (request_id,network_id,daemon_node_id,requested_by,status,created_at,expires_at)
|
|
183
|
+
VALUES (?1,?2,?3,?4,'pending',?5,?6)`, [requestId, h.networkId!, daemonId, h.userId!, now, now + 120_000]);
|
|
184
|
+
db.run('INSERT INTO daemon_provider_logins (request_id,intent_json) VALUES (?1,?2)', [requestId, JSON.stringify(intent)]);
|
|
185
|
+
});
|
|
186
|
+
return { ok: true as const, request_id: requestId, status: 'pending', expires_at: now + 120_000 };
|
|
187
|
+
} catch { return denied('provider_snapshot_busy'); }
|
|
188
|
+
}
|
|
189
|
+
export function getProviderSnapshot(h: Human, requestId: string, includeDiscovery = false) {
|
|
190
|
+
const refusal = authorize(h); if (refusal) return refusal;
|
|
191
|
+
expire(h.networkId!);
|
|
192
|
+
const row = db.get<Row>(`SELECT * FROM daemon_provider_snapshots WHERE request_id=?1 AND network_id=?2`, requestId, h.networkId!);
|
|
193
|
+
if (!row) return denied("request_not_found");
|
|
194
|
+
const snapshot = !/^dp[al]_/.test(row.request_id) && row.snapshot_json ? JSON.parse(row.snapshot_json) : null;
|
|
195
|
+
// Old clients validate an exact contract. Their default projection stays unchanged.
|
|
196
|
+
if (!includeDiscovery && snapshot?.codex_inventory) delete snapshot.codex_inventory.local_discovery;
|
|
197
|
+
return { ok: true, request_id: row.request_id, daemon_node_id: row.daemon_node_id, status: row.status,
|
|
198
|
+
expires_at: row.expires_at, observed_at: row.observed_at, error: row.error,
|
|
199
|
+
snapshot,
|
|
200
|
+
...(row.request_id.startsWith('dpl_') ? { login: row.snapshot_json ? JSON.parse(row.snapshot_json) : null } : {}),
|
|
201
|
+
...(row.request_id.startsWith('dpa_') ? { application: row.snapshot_json ? JSON.parse(row.snapshot_json) : null } : {}) };
|
|
202
|
+
}
|
|
203
|
+
export function pullProviderSnapshots(d: Daemon, acceptWrites = false, acceptApplications = false, acceptLogins = false, acceptDiscovery = false, acceptCollections = false) {
|
|
204
|
+
if (!d.ok) return denied("caller_not_a_daemon");
|
|
205
|
+
if (!daemonExists(d.networkId, d.daemonNodeId)) return denied("caller_not_a_daemon");
|
|
206
|
+
expire(d.networkId, d.daemonNodeId);
|
|
207
|
+
const requests = db.all<{ request_id: string; expires_at: number }>(`SELECT request_id,expires_at FROM daemon_provider_snapshots
|
|
208
|
+
WHERE network_id=?1 AND daemon_node_id=?2 AND status='pending' ORDER BY created_at LIMIT 20`, d.networkId, d.daemonNodeId);
|
|
209
|
+
try { return { ok: true, requests: requests.flatMap<Record<string, unknown>>(r => {
|
|
210
|
+
if (r.request_id.startsWith('dpl_')) {
|
|
211
|
+
if (!acceptLogins) return [];
|
|
212
|
+
const row = db.get<{ intent_json: string }>('SELECT intent_json FROM daemon_provider_logins WHERE request_id=?1', r.request_id);
|
|
213
|
+
const intent = row && JSON.parse(row.intent_json);
|
|
214
|
+
if (!validLoginIntent(intent)) throw Error('invalid_login_intent');
|
|
215
|
+
return [{ ...r, operation: 'login', intent }];
|
|
216
|
+
}
|
|
217
|
+
if (r.request_id.startsWith('dpa_')) {
|
|
218
|
+
if (!acceptApplications) return []; // old daemons must not consume applications as scans
|
|
219
|
+
const application = applicationIntent(r.request_id);
|
|
220
|
+
const target = application && boundApplicationTarget(d.networkId, d.daemonNodeId, application.selection);
|
|
221
|
+
if (!target) {
|
|
222
|
+
db.run("UPDATE daemon_provider_snapshots SET status='failed',error='application_target_not_bound',observed_at=?1 WHERE request_id=?2 AND status='pending'", [Date.now(), r.request_id]);
|
|
223
|
+
db.run('DELETE FROM daemon_provider_applications WHERE request_id=?1', [r.request_id]);
|
|
224
|
+
return [];
|
|
225
|
+
}
|
|
226
|
+
return [{ ...r, operation: 'apply', ...application!, alias: target.alias }];
|
|
227
|
+
}
|
|
228
|
+
const write = privateWrite(r.request_id, d.networkId, d.daemonNodeId);
|
|
229
|
+
if (write?.operation === 'collect') {
|
|
230
|
+
if (!acceptCollections) return []; // old write-capable daemons cannot consume collection as put/scan
|
|
231
|
+
if (!boundCollectionTarget(d.networkId, d.daemonNodeId, write.selection)) {
|
|
232
|
+
db.run("UPDATE daemon_provider_snapshots SET status='failed',error='collection_target_not_bound',observed_at=?1 WHERE request_id=?2 AND status='pending'", [Date.now(), r.request_id]);
|
|
233
|
+
db.run('DELETE FROM daemon_provider_writes WHERE request_id=?1', [r.request_id]);
|
|
234
|
+
return [];
|
|
235
|
+
}
|
|
236
|
+
} else if (write && !acceptWrites) return [];
|
|
237
|
+
return [write ? { ...r, ...write } : { ...r, scan_targets: providerScanTargets(d.networkId, d.daemonNodeId), ...(acceptDiscovery ? { discover_local: true } : {}) }];
|
|
238
|
+
}) }; } catch { return denied('provider_write_unavailable'); }
|
|
239
|
+
}
|
|
240
|
+
export function ackProviderSnapshot(d: Daemon, requestId: string, snapshot?: unknown, failed = false, appliedRevision?: number, application?: unknown, login?: unknown) {
|
|
241
|
+
if (!d.ok || !daemonExists(d.networkId, d.daemonNodeId)) return denied("caller_not_a_daemon");
|
|
242
|
+
expire(d.networkId, d.daemonNodeId);
|
|
243
|
+
const row = db.get<Row>(`SELECT * FROM daemon_provider_snapshots WHERE request_id=?1 AND network_id=?2 AND daemon_node_id=?3`, requestId, d.networkId, d.daemonNodeId);
|
|
244
|
+
if (!row) return denied("request_not_found");
|
|
245
|
+
// First accepted receipt wins; reconnect replay cannot rewrite prior evidence.
|
|
246
|
+
if (row.status !== "pending") return { ok: row.status === "succeeded" || row.status === "failed", status: row.status };
|
|
247
|
+
if (requestId.startsWith('dpl_')) {
|
|
248
|
+
if (snapshot !== undefined || application !== undefined || appliedRevision !== undefined || (failed && login !== undefined)) return denied('invalid_login_receipt');
|
|
249
|
+
const intentRow = db.get<{ intent_json: string }>('SELECT intent_json FROM daemon_provider_logins WHERE request_id=?1', requestId);
|
|
250
|
+
const intent = intentRow && JSON.parse(intentRow.intent_json);
|
|
251
|
+
if (!failed && (!validLoginIntent(intent) || !validLoginReceipt(login, intent, d.networkId, d.daemonNodeId))) return denied('invalid_login_receipt');
|
|
252
|
+
db.run(`UPDATE daemon_provider_snapshots SET status=?1,snapshot_json=?2,error=?3,observed_at=?4
|
|
253
|
+
WHERE request_id=?5 AND status='pending'`, [failed ? 'failed' : 'succeeded', failed ? null : JSON.stringify(login),
|
|
254
|
+
failed ? 'provider_login_unavailable' : null, Date.now(), requestId]);
|
|
255
|
+
db.run('DELETE FROM daemon_provider_logins WHERE request_id=?1', [requestId]);
|
|
256
|
+
return { ok: true, status: failed ? 'failed' : 'succeeded' };
|
|
257
|
+
}
|
|
258
|
+
if (login !== undefined) return denied('invalid_login_receipt');
|
|
259
|
+
if (requestId.startsWith('dpa_')) {
|
|
260
|
+
if (snapshot !== undefined || appliedRevision !== undefined || (failed && application !== undefined)) return denied('invalid_application_receipt');
|
|
261
|
+
if (!failed) {
|
|
262
|
+
try {
|
|
263
|
+
const intent = applicationIntent(requestId);
|
|
264
|
+
if (!intent || !boundApplicationTarget(d.networkId, d.daemonNodeId, intent.selection)
|
|
265
|
+
|| !validProviderApplicationReceipt(application, requestId, intent.expected_revision, intent.selection)
|
|
266
|
+
|| (application as any).runtime_provider !== (intent.selection.account_id ? 'openai' : `anet_${createHash('sha256').update(JSON.stringify([intent.selection.provider_id, intent.selection.auth_id])).digest('hex').slice(0, 20)}`)
|
|
267
|
+
|| (application as any).applied_at < row.created_at - 30_000) return denied('invalid_application_receipt');
|
|
268
|
+
} catch { return denied('invalid_application_receipt'); }
|
|
269
|
+
}
|
|
270
|
+
db.run(`UPDATE daemon_provider_snapshots SET status=?1,snapshot_json=?2,error=?3,observed_at=?4
|
|
271
|
+
WHERE request_id=?5 AND status='pending'`, [failed ? 'failed' : 'succeeded', failed ? null : JSON.stringify(application),
|
|
272
|
+
failed ? 'provider_application_unavailable' : null, Date.now(), requestId]);
|
|
273
|
+
db.run('DELETE FROM daemon_provider_applications WHERE request_id=?1', [requestId]);
|
|
274
|
+
return { ok: true, status: failed ? 'failed' : 'succeeded' };
|
|
275
|
+
}
|
|
276
|
+
if (application !== undefined) return denied('invalid_application_receipt');
|
|
277
|
+
let safe: unknown = null;
|
|
278
|
+
if (failed && snapshot !== undefined) return denied("invalid_provider_snapshot");
|
|
279
|
+
if (!failed) {
|
|
280
|
+
try { safe = validateDaemonProviderSnapshot(snapshot, d.networkId, d.daemonNodeId); }
|
|
281
|
+
catch { return denied("invalid_provider_snapshot"); }
|
|
282
|
+
if ((safe as any).codex_inventory) {
|
|
283
|
+
try {
|
|
284
|
+
const targets = new Map(providerScanTargets(d.networkId, d.daemonNodeId).map(n => [n.node_id, n.alias]));
|
|
285
|
+
const rows = (safe as any).codex_inventory.rows;
|
|
286
|
+
if (rows.length !== targets.size || rows.some((r: any) => targets.get(r.node_id) !== r.alias)) return denied('invalid_provider_snapshot');
|
|
287
|
+
const local = (safe as any).codex_inventory.local_discovery;
|
|
288
|
+
if (local) {
|
|
289
|
+
const registered = new Map(db.all<{ node_id: string; alias: string }>('SELECT node_id,alias FROM nodes WHERE network_id=?1', d.networkId).map(n => [n.node_id, n.alias]));
|
|
290
|
+
const ownership = buildControllableMap(
|
|
291
|
+
db.all<{ request_id: string; daemon_node_id: string }>('SELECT request_id,daemon_node_id FROM node_create_requests WHERE network_id=?1', d.networkId),
|
|
292
|
+
db.all<{ node_id: string; daemon_node_id: string }>("SELECT node_id,daemon_node_id FROM node_daemon_bindings WHERE network_id=?1 AND status='active'", d.networkId));
|
|
293
|
+
// Filesystem discovery is not control authority. Other-daemon and
|
|
294
|
+
// unregistered identities cannot enter this network's public receipt.
|
|
295
|
+
local.rows = local.rows.filter((r: any) => registered.get(r.node_id) === r.alias && !ownership.has(r.node_id));
|
|
296
|
+
}
|
|
297
|
+
} catch { return denied('invalid_provider_snapshot'); }
|
|
298
|
+
}
|
|
299
|
+
try {
|
|
300
|
+
const write = privateWrite(requestId, d.networkId, d.daemonNodeId);
|
|
301
|
+
if (write) {
|
|
302
|
+
const actual = safe as any;
|
|
303
|
+
if (appliedRevision !== write.expected_revision + 1 || actual.revision !== appliedRevision) return denied('provider_write_unconfirmed');
|
|
304
|
+
if (write.operation === 'collect') {
|
|
305
|
+
if (!boundCollectionTarget(d.networkId, d.daemonNodeId, write.selection)) return denied('collection_target_not_bound');
|
|
306
|
+
const saved = actual.providers.find((p: any) => p.id === write.selection.provider_id);
|
|
307
|
+
const auth = saved?.runtimes.find((r: any) => r.runtime === 'codex-tui')?.auth.find((a: any) => a.id === write.selection.auth_id);
|
|
308
|
+
if (saved?.label !== write.selection.label.trim() || auth?.kind !== 'api_key' || auth.credential_present !== true
|
|
309
|
+
|| auth.models.length !== 1 || auth.verification !== 'not_checked' || auth.application !== 'not_applied') return denied('provider_write_unconfirmed');
|
|
310
|
+
} else {
|
|
311
|
+
const expected = validateWrite(write.provider, d.networkId, d.daemonNodeId);
|
|
312
|
+
const saved = actual.providers.find((p: any) => p.id === expected.id);
|
|
313
|
+
// CLI canonicalizes label and endpoint. No credential is compared or returned here.
|
|
314
|
+
expected.label = expected.label.trim();
|
|
315
|
+
for (const r of expected.runtimes) for (const a of r.auth) if (a.baseUrl) a.baseUrl = new URL(a.baseUrl).toString().replace(/\/$/, '');
|
|
316
|
+
if (!isDeepStrictEqual(saved, expected)) return denied('provider_write_unconfirmed');
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
} catch { return denied('provider_write_unconfirmed'); }
|
|
320
|
+
}
|
|
321
|
+
db.run(`UPDATE daemon_provider_snapshots SET status=?1, snapshot_json=?2, error=?3, observed_at=?4
|
|
322
|
+
WHERE request_id=?5 AND status='pending'`, [failed ? "failed" : "succeeded", safe ? JSON.stringify(safe) : null,
|
|
323
|
+
failed ? "provider_snapshot_unavailable" : null, Date.now(), requestId]);
|
|
324
|
+
const accepted = db.get<Row>("SELECT * FROM daemon_provider_snapshots WHERE request_id=?1", requestId);
|
|
325
|
+
db.run('DELETE FROM daemon_provider_writes WHERE request_id=?1', [requestId]);
|
|
326
|
+
return { ok: true, status: accepted!.status };
|
|
327
|
+
}
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { beforeAll, afterAll, expect, test } from "bun:test";
|
|
2
|
+
import { mkdtempSync, readFileSync, readdirSync } from "node:fs";
|
|
3
|
+
import { nodeTokenReissueError } from "../../agent-network/src/cli-errors";
|
|
4
|
+
|
|
5
|
+
process.env.COMMHUB_DB ||= `${mkdtempSync("/tmp/owner678-")}/hub.db`;
|
|
6
|
+
let db: any, hub: ReturnType<typeof Bun.serve>, base: string;
|
|
7
|
+
let owner: any, member: any, outsider: any, legacy: string, mismatched: string, bound: string;
|
|
8
|
+
let wrongBound: string, ownerless: string, revoked: string, crossNetwork: string, requestId: string;
|
|
9
|
+
const daemon = "n_owner_fixture_daemon", child = "n_owner_fixture_child";
|
|
10
|
+
|
|
11
|
+
async function call(token: string, name: string, args: Record<string, unknown> = {}) {
|
|
12
|
+
const response = await fetch(`${base}/mcp`, {
|
|
13
|
+
method: "POST", headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json",
|
|
14
|
+
Accept: "application/json, text/event-stream", "MCP-Protocol-Version": "2025-03-26" },
|
|
15
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name, arguments: args } }),
|
|
16
|
+
});
|
|
17
|
+
expect(response.status).toBe(200);
|
|
18
|
+
const raw = await response.text(), lines = raw.split("\n").filter(line => line.startsWith("data:"));
|
|
19
|
+
const message = JSON.parse(lines.length ? lines.at(-1)!.slice(5) : raw);
|
|
20
|
+
expect(message.error).toBeUndefined();
|
|
21
|
+
return JSON.parse(message.result.content[0].text);
|
|
22
|
+
}
|
|
23
|
+
async function mint(token: string, network: string, name: string, nodeId?: string) {
|
|
24
|
+
const response = await fetch(`${base}/api/auth/node-token`, { method: "POST",
|
|
25
|
+
headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" },
|
|
26
|
+
body: JSON.stringify({ network_id: network, node_name: name, ...(nodeId ? { node_id: nodeId } : {}) }),
|
|
27
|
+
});
|
|
28
|
+
return { status: response.status, body: await response.json() as any };
|
|
29
|
+
}
|
|
30
|
+
beforeAll(async () => {
|
|
31
|
+
({ db } = await import("./db.js"));
|
|
32
|
+
const { register } = await import("./auth.js");
|
|
33
|
+
const { generateNetworkToken, hashToken } = await import("./db.js");
|
|
34
|
+
owner = register("owner678", "Fixture-Strong1!");
|
|
35
|
+
member = register("member678", "Fixture-Strong1!");
|
|
36
|
+
outsider = register("outside678", "Fixture-Strong1!");
|
|
37
|
+
db.run("UPDATE users SET role='user' WHERE user_id IN (?1,?2)", [member.user.user_id, outsider.user.user_id]);
|
|
38
|
+
db.run("INSERT INTO network_members(network_id,user_id,role) VALUES(?1,?2,'member')", [owner.network_id, member.user.user_id]);
|
|
39
|
+
for (const id of [daemon, child, "n_ownerless_fixture"]) {
|
|
40
|
+
db.run(`INSERT INTO nodes(node_id,node_name,alias,network_id,owner_user_id,hostname,config_snapshot,lifecycle_state)
|
|
41
|
+
VALUES(?1,?1,?1,?2,?3,'fixture-host',?4,'active')`, [id, owner.network_id,
|
|
42
|
+
id === "n_ownerless_fixture" ? null : owner.user.user_id,
|
|
43
|
+
JSON.stringify(id === daemon ? { role: "host_supervisor", daemon_capabilities: { adopt_capable: true } } : {})]);
|
|
44
|
+
}
|
|
45
|
+
db.run("INSERT INTO sessions(resume_id,alias,node_id,network_id,status,last_seen_at) VALUES(?1,?1,?1,?2,'idle',datetime('now'))", [daemon, owner.network_id]);
|
|
46
|
+
function credential(label: string, user: string, node: string | null = null, alias = daemon, network = owner.network_id) {
|
|
47
|
+
const token = generateNetworkToken();
|
|
48
|
+
db.run(`INSERT INTO api_tokens(token_id,user_id,network_id,scope,name,token_hash,bound_node_id)
|
|
49
|
+
VALUES(?1,?2,?3,'network',?4,?5,?6)`, [label, user, network, `node:${alias}`, hashToken(token), node]);
|
|
50
|
+
return token;
|
|
51
|
+
}
|
|
52
|
+
legacy = credential("owner678_legacy", owner.user.user_id);
|
|
53
|
+
mismatched = credential("owner678_mismatch", member.user.user_id);
|
|
54
|
+
bound = credential("owner678_bound", member.user.user_id, daemon);
|
|
55
|
+
wrongBound = credential("owner678_wrong_bound", member.user.user_id, "n_absent_bound");
|
|
56
|
+
ownerless = credential("owner678_ownerless", member.user.user_id, null, "n_ownerless_fixture");
|
|
57
|
+
db.run("UPDATE api_tokens SET node_identity_epoch=1 WHERE token_id='owner678_ownerless'");
|
|
58
|
+
revoked = credential("owner678_revoked", owner.user.user_id);
|
|
59
|
+
db.run("UPDATE api_tokens SET revoked_at=datetime('now') WHERE token_id='owner678_revoked'");
|
|
60
|
+
crossNetwork = credential("owner678_cross", outsider.user.user_id, daemon, daemon, outsider.network_id);
|
|
61
|
+
const { bootServer } = await import("./server.js");
|
|
62
|
+
hub = bootServer({ port: 0, hostname: "127.0.0.1" }); base = `http://127.0.0.1:${hub.port}`;
|
|
63
|
+
const request = await call(owner.token, "request_adopt_node", { node_id: child, daemon_node_id: daemon, workdir: "/fixture/project" });
|
|
64
|
+
expect(request.ok).toBe(true); requestId = request.request_id;
|
|
65
|
+
}, 60000);
|
|
66
|
+
afterAll(() => hub?.stop(true));
|
|
67
|
+
|
|
68
|
+
test("name-only issuance checks existing ownership atomically", async () => {
|
|
69
|
+
const count = () => db.get("SELECT COUNT(*) AS n FROM api_tokens").n;
|
|
70
|
+
const before = count();
|
|
71
|
+
expect(await mint(member.token, owner.network_id, daemon)).toEqual({ status: 400, body: { ok: false, error: "node_owner_mismatch" } });
|
|
72
|
+
expect(await mint(owner.token, owner.network_id, "n_ownerless_fixture")).toEqual({ status: 400, body: { ok: false, error: "node_owner_mismatch" } });
|
|
73
|
+
expect(count()).toBe(before);
|
|
74
|
+
// Legacy owner refresh and a new node name remain supported.
|
|
75
|
+
const refresh = await mint(owner.token, owner.network_id, daemon);
|
|
76
|
+
expect(refresh.status).toBe(200);
|
|
77
|
+
expect((await call(refresh.body.token, "list_my_children")).ok).toBe(true);
|
|
78
|
+
expect((await mint(member.token, owner.network_id, "fresh678")).status).toBe(200);
|
|
79
|
+
expect((await mint(member.token, owner.network_id, daemon, daemon)).body.error).toBe("node_owner_mismatch");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Every daemon-facing handler must fail at the shared identity boundary,
|
|
83
|
+
// not incidentally at request lookup or argument validation. The resolver
|
|
84
|
+
// mutation expects all 15 of these cases to go red together.
|
|
85
|
+
const endpoints: [string, Record<string, unknown>][] = [
|
|
86
|
+
["list_my_pending_create_requests", {}], ["list_my_pending_lifecycle_requests", {}],
|
|
87
|
+
["get_create_request", { request_id: "cr_fixture_absent" }],
|
|
88
|
+
["ack_create_request", { request_id: "cr_fixture_absent", status: "started" }],
|
|
89
|
+
["get_stop_request", { request_id: "sr_fixture_absent" }],
|
|
90
|
+
["ack_stop_request", { request_id: "sr_fixture_absent", status: "stopped" }],
|
|
91
|
+
["get_start_request", { request_id: "start_fixture_absent" }],
|
|
92
|
+
["ack_start_request", { request_id: "start_fixture_absent", status: "started" }],
|
|
93
|
+
["list_my_children", {}], ["get_adopt_request", {}], ["ack_adopt_request", { status: "adopted" }],
|
|
94
|
+
["get_probe_request", { probe_id: "probe_fixture_absent" }],
|
|
95
|
+
["ack_probe_request", { probe_id: "probe_fixture_absent", status: "ok", latency_ms: 1 }],
|
|
96
|
+
["get_opencode_catalog_request", { request_id: "cat_fixture_absent" }],
|
|
97
|
+
["ack_opencode_catalog", { request_id: "cat_fixture_absent", error: "unavailable" }],
|
|
98
|
+
];
|
|
99
|
+
for (const [name, arguments_] of endpoints) {
|
|
100
|
+
test(`daemon identity boundary: ${name}`, async () => {
|
|
101
|
+
const args = name.includes("adopt_request") ? { ...arguments_, request_id: requestId } : arguments_;
|
|
102
|
+
for (const token of [mismatched, wrongBound, ownerless, owner.token, member.token]) {
|
|
103
|
+
const denied = await call(token, name, args);
|
|
104
|
+
expect(denied).toMatchObject({ ok: false, error: "caller_not_a_daemon" });
|
|
105
|
+
// 2026-10-31 —— #3275:**错误信封也是契约**。
|
|
106
|
+
// 这八个 ack/get 工具此前只被断言过 error 的**值**,没有一条钉住键集:
|
|
107
|
+
// 谁顺手在某个出口多塞一个字段(例如 hint / request_id),客户端的分支就多一个没人知道的键。
|
|
108
|
+
// ⇒ 在同一个循环里把形状钉住,成本几乎为零。
|
|
109
|
+
expect(Object.keys(denied).sort()).toEqual(["error", "ok"]);
|
|
110
|
+
}
|
|
111
|
+
expect(db.get("SELECT status FROM node_daemon_bindings WHERE request_id=?1", requestId).status).toBe("pending");
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
test("legacy owner and exact bound identity remain supported", async () => {
|
|
116
|
+
expect(db.get("SELECT bound_node_id FROM api_tokens WHERE token_id='owner678_legacy'").bound_node_id).toBeNull();
|
|
117
|
+
for (const token of [legacy, bound]) {
|
|
118
|
+
expect((await call(token, "list_my_children")).ok).toBe(true);
|
|
119
|
+
// 2026-10-31 —— #3275 批次 2:这两条是 **SSE 重连补偿**的入口(漏了门铃节点会卡死),
|
|
120
|
+
// 客户端按字段名读;键集从未被钉住。改为 `const` 赋值,顺便让严格口径能数到它们。
|
|
121
|
+
const pendCreate = await call(token, "list_my_pending_create_requests");
|
|
122
|
+
expect(pendCreate.ok).toBe(true);
|
|
123
|
+
expect(Object.keys(pendCreate).sort()).toEqual(["clone_count", "clone_requests", "count", "ok", "requests"]);
|
|
124
|
+
|
|
125
|
+
const pendLife = await call(token, "list_my_pending_lifecycle_requests");
|
|
126
|
+
expect(pendLife.ok).toBe(true);
|
|
127
|
+
expect(Object.keys(pendLife).sort()).toEqual(["ok", "start_count", "start_requests", "stop_count", "stop_requests"]);
|
|
128
|
+
// 计数与数组同源:两个 count 必须等于各自数组长度(否则「有 N 条」与「列出来」会分叉)
|
|
129
|
+
expect(pendLife.stop_count).toBe(pendLife.stop_requests.length);
|
|
130
|
+
expect(pendLife.start_count).toBe(pendLife.start_requests.length);
|
|
131
|
+
// 2026-10-31 —— #3275:形状由**一次真实调用**给出(不靠读源码猜)
|
|
132
|
+
const gotAdopt = await call(token, "get_adopt_request", { request_id: requestId });
|
|
133
|
+
expect(gotAdopt.ok).toBe(true);
|
|
134
|
+
// ⚠️ 我第一版按源码猜了 7 个键(binding_request_id / child_alias / child_node_id …)——**全错**。
|
|
135
|
+
// 真实形状由这次调用给出,12 个键;其中 error 为空、request_id 是这一行自己的 id。
|
|
136
|
+
// 这是本卡第三次被「读源码猜形状」打回,写进注释免得下一个人再猜:
|
|
137
|
+
// 行对象整行透传时,源码里看到的字段名是**SQL 的列名**,与对外载荷不是一回事。
|
|
138
|
+
expect(Object.keys(gotAdopt).sort()).toEqual([
|
|
139
|
+
"alias", "created_at", "daemon_node_id", "error", "network_id", "node_id",
|
|
140
|
+
"ok", "request_id", "requested_by", "status", "updated_at", "workdir",
|
|
141
|
+
]);
|
|
142
|
+
}
|
|
143
|
+
// An explicit binding does not bypass the network or revocation boundaries.
|
|
144
|
+
expect(await call(crossNetwork, "list_my_children")).toMatchObject({ ok: false, error: "caller_not_a_daemon" });
|
|
145
|
+
const denied = await fetch(`${base}/mcp`, { method: "POST", headers: { Authorization: `Bearer ${revoked}` } });
|
|
146
|
+
expect(denied.status).toBe(401);
|
|
147
|
+
// 2026-10-31 —— #3275:**成功体也要钉键集**(上面那个循环钉的是错误信封)。
|
|
148
|
+
// 这四条是 daemon 侧最常被按字段名读的回执;多一个键不会让谁报错,但会改变解析分支。
|
|
149
|
+
const adopted = await call(legacy, "ack_adopt_request", { request_id: requestId, status: "adopted" });
|
|
150
|
+
expect(adopted).toMatchObject({ ok: true, status: "active" });
|
|
151
|
+
expect(Object.keys(adopted).sort()).toEqual(["ok", "status"]);
|
|
152
|
+
expect((await call(legacy, "list_my_children")).children).toContainEqual({ child_node_id: child, alias: child, lifecycle_state: "active", managed: "adopted", binding_request_id: requestId });
|
|
153
|
+
// 2026-10-31 —— #3275:get_probe_request 的**不存在**分支键集(成功分支需要真实 probe 夹具,
|
|
154
|
+
// 本文件不造;错误分支同样是契约:客户端按 error 分支,多一个键就会多一个没人知道的分支)
|
|
155
|
+
const gotProbe = await call(legacy, "get_probe_request", { probe_id: "probe_fixture_absent" });
|
|
156
|
+
expect(gotProbe.ok).toBe(false);
|
|
157
|
+
// 实测:错误分支多一个 reason(不是只 {error, ok})——**又一次由真实调用纠正**
|
|
158
|
+
expect(Object.keys(gotProbe).sort()).toEqual(["error", "ok", "reason"]);
|
|
159
|
+
// 2026-10-31 —— #3275:get_opencode_catalog_request 的**不存在**分支恰好两键
|
|
160
|
+
// (与 get_probe_request 的三键不同族 —— 又一次说明「错误信封不是全仓一个形状」)
|
|
161
|
+
const gotCatalog = await call(legacy, "get_opencode_catalog_request", { request_id: "cat_fixture_absent" });
|
|
162
|
+
expect(gotCatalog.ok).toBe(false);
|
|
163
|
+
expect(Object.keys(gotCatalog).sort()).toEqual(["error", "ok"]);
|
|
164
|
+
const stop = await call(owner.token, "stop_node", { node_id: child, network_id: owner.network_id });
|
|
165
|
+
expect(stop.ok).toBe(true);
|
|
166
|
+
// 2026-10-31 —— #3275 批次 2:get_stop_request 的**成功体**键集(daemon 按字段名读,
|
|
167
|
+
// 尤其 delete_config / grace_seconds / force 三个决定它要不要删配置)
|
|
168
|
+
const gotStop = await call(legacy, "get_stop_request", { request_id: stop.request_id });
|
|
169
|
+
expect(gotStop.ok).toBe(true);
|
|
170
|
+
expect(Object.keys(gotStop).sort()).toEqual([
|
|
171
|
+
"action", "child_alias", "child_node_id", "delete_config", "force", "grace_seconds", "ok", "request_id",
|
|
172
|
+
]);
|
|
173
|
+
const stoppedAck = await call(legacy, "ack_stop_request", { request_id: stop.request_id, status: "stopped" });
|
|
174
|
+
expect(stoppedAck.ok).toBe(true);
|
|
175
|
+
expect(Object.keys(stoppedAck).sort()).toEqual(["ok", "status"]); // #3275:成功体键集
|
|
176
|
+
const start = await call(owner.token, "start_node", { node_id: child, network_id: owner.network_id });
|
|
177
|
+
expect(start.ok).toBe(true);
|
|
178
|
+
const gotStart = await call(legacy, "get_start_request", { request_id: start.request_id });
|
|
179
|
+
expect(gotStart.ok).toBe(true);
|
|
180
|
+
// 成功体:四个固定键 + start_completion_capable;fork_recovery 只在「创建即恢复」路径出现(条件键)
|
|
181
|
+
expect(Object.keys(gotStart).sort()).toEqual([
|
|
182
|
+
"child_alias", "child_node_id", "ok", "request_id", "start_completion_capable",
|
|
183
|
+
]);
|
|
184
|
+
const startedAck = await call(legacy, "ack_start_request", { request_id: start.request_id, status: "started", child_pid: 4321 });
|
|
185
|
+
expect(startedAck).toMatchObject({ ok: true, status: "started" });
|
|
186
|
+
expect(Object.keys(startedAck).sort()).toEqual(["ok", "status"]); // #3275:成功体键集
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test("ownerless legacy daemon remains supported without rewriting its owner", async () => {
|
|
190
|
+
db.run("UPDATE api_tokens SET node_identity_epoch=0 WHERE token_id='owner678_ownerless'");
|
|
191
|
+
try {
|
|
192
|
+
expect((await call(ownerless, "list_my_children")).ok).toBe(true);
|
|
193
|
+
const refresh = await mint(member.token, owner.network_id, "n_ownerless_fixture");
|
|
194
|
+
expect(refresh.status).toBe(200);
|
|
195
|
+
expect(refresh.body.node_id).toBe("n_ownerless_fixture");
|
|
196
|
+
expect((await call(refresh.body.token, "list_my_children")).ok).toBe(true);
|
|
197
|
+
const withId = await mint(member.token, owner.network_id, "n_ownerless_fixture", "n_ownerless_fixture");
|
|
198
|
+
expect(withId.status).toBe(200);
|
|
199
|
+
expect(db.get("SELECT owner_user_id FROM nodes WHERE node_id='n_ownerless_fixture'").owner_user_id).toBeNull();
|
|
200
|
+
expect((await mint(owner.token, owner.network_id, "n_ownerless_fixture")).status).toBe(400);
|
|
201
|
+
} finally { db.run("UPDATE api_tokens SET node_identity_epoch=1 WHERE token_id='owner678_ownerless'"); }
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test("new name-only registration binds once and its holder can refresh with either shape", async () => {
|
|
205
|
+
const first = await mint(member.token, owner.network_id, "refresh_fixture");
|
|
206
|
+
expect(first.status).toBe(200);
|
|
207
|
+
expect(db.get("SELECT node_identity_epoch FROM api_tokens WHERE token_id=?1", first.body.token_id).node_identity_epoch).toBe(2);
|
|
208
|
+
const result = await call(first.body.token, "report_status", {
|
|
209
|
+
resume_id: "refresh-fixture-session", alias: "refresh_fixture", status: "idle", node_id: "n_refresh_fixture",
|
|
210
|
+
});
|
|
211
|
+
expect(result.ok).toBe(true);
|
|
212
|
+
expect(db.get("SELECT bound_node_id FROM api_tokens WHERE token_id=?1", first.body.token_id).bound_node_id).toBe("n_refresh_fixture");
|
|
213
|
+
expect(db.get("SELECT owner_user_id FROM nodes WHERE node_id='n_refresh_fixture'").owner_user_id).toBeNull();
|
|
214
|
+
for (const id of [undefined, "n_refresh_fixture"]) {
|
|
215
|
+
const refresh = await mint(member.token, owner.network_id, "refresh_fixture", id);
|
|
216
|
+
expect(refresh.status).toBe(200);
|
|
217
|
+
expect(refresh.body.node_id).toBe("n_refresh_fixture");
|
|
218
|
+
expect((await call(refresh.body.token, "list_my_children")).ok).toBe(true);
|
|
219
|
+
}
|
|
220
|
+
expect((await mint(owner.token, owner.network_id, "refresh_fixture")).status).toBe(400);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test("fresh ID cannot mint another node alias", async () => {
|
|
224
|
+
const before = db.get("SELECT COUNT(*) AS n FROM api_tokens").n;
|
|
225
|
+
expect((await mint(member.token, owner.network_id, daemon, "n_fresh_impostor")).status).toBe(400);
|
|
226
|
+
expect(db.get("SELECT node_id FROM nodes WHERE node_id='n_fresh_impostor'")).toBeNull();
|
|
227
|
+
expect(db.get("SELECT COUNT(*) AS n FROM api_tokens").n).toBe(before);
|
|
228
|
+
expect((await call(legacy, "list_my_children")).ok).toBe(true);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test("duplicate legacy aliases use bound IDs and an unbound token follows its owner", async () => {
|
|
232
|
+
// Historical duplicate rows: the member's row was inserted first. Neither
|
|
233
|
+
// lookup order nor alias can redirect an exact-bound token. An unbound
|
|
234
|
+
// token resolves to the row its issuer owns.
|
|
235
|
+
const { hashToken, generateNetworkToken } = await import("./db.js");
|
|
236
|
+
const tokens: string[] = [];
|
|
237
|
+
for (const [id, user] of [["n_duplicate_first", member.user.user_id], ["n_duplicate_owner", owner.user.user_id]]) {
|
|
238
|
+
db.run("INSERT INTO nodes(node_id,node_name,alias,network_id,owner_user_id) VALUES(?1,'duplicate_fixture','duplicate_fixture',?2,?3)", [id, owner.network_id, user]);
|
|
239
|
+
const token = generateNetworkToken(); tokens.push(token);
|
|
240
|
+
db.run("INSERT INTO api_tokens(token_id,token_hash,user_id,network_id,name,scope,bound_node_id) VALUES(?1,?2,?3,?4,'node:duplicate_fixture','network',?1)", [id, hashToken(token), user, owner.network_id]);
|
|
241
|
+
db.run("INSERT INTO node_daemon_bindings(request_id,network_id,node_id,daemon_node_id,workdir,requested_by,status,created_at,updated_at) VALUES(?1,?2,?1,?1,'/fixture',?3,'pending',1,1)", [id, owner.network_id, user]);
|
|
242
|
+
}
|
|
243
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
244
|
+
const result = await call(tokens[i], "list_my_children");
|
|
245
|
+
expect(result.ok).toBe(true);
|
|
246
|
+
const ownId = i === 0 ? "n_duplicate_first" : "n_duplicate_owner";
|
|
247
|
+
expect((await call(tokens[i], "get_adopt_request", { request_id: ownId })).ok).toBe(true);
|
|
248
|
+
expect((await call(tokens[i], "get_adopt_request", { request_id: i === 0 ? "n_duplicate_owner" : "n_duplicate_first" })).ok).toBe(false);
|
|
249
|
+
}
|
|
250
|
+
db.run("UPDATE api_tokens SET bound_node_id=NULL WHERE token_id='n_duplicate_owner'");
|
|
251
|
+
expect((await call(tokens[1], "list_my_children")).ok).toBe(true);
|
|
252
|
+
expect((await call(tokens[1], "get_adopt_request", { request_id: "n_duplicate_owner" })).ok).toBe(true);
|
|
253
|
+
expect((await call(tokens[1], "get_adopt_request", { request_id: "n_duplicate_first" })).ok).toBe(false);
|
|
254
|
+
db.run("UPDATE api_tokens SET bound_node_id=NULL WHERE token_id='n_duplicate_first'");
|
|
255
|
+
expect((await call(tokens[0], "list_my_children")).ok).toBe(true);
|
|
256
|
+
expect((await call(tokens[0], "get_adopt_request", { request_id: "n_duplicate_first" })).ok).toBe(true);
|
|
257
|
+
expect((await call(tokens[0], "get_adopt_request", { request_id: "n_duplicate_owner" })).ok).toBe(false);
|
|
258
|
+
expect((await mint(owner.token, owner.network_id, "duplicate_fixture", "n_third_duplicate")).status).toBe(400);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
test("all production token issuers explicitly mark the rollout epoch", () => {
|
|
262
|
+
const root = `${import.meta.dir}/../..`;
|
|
263
|
+
function sources(dir: string): string[] {
|
|
264
|
+
return readdirSync(dir, { withFileTypes: true }).flatMap(entry => {
|
|
265
|
+
const path = `${dir}/${entry.name}`;
|
|
266
|
+
return entry.isDirectory() ? sources(path) : entry.name.endsWith(".ts") && !entry.name.endsWith(".test.ts") ? [path] : [];
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
const sourceFiles = ["server/src", "agent-network/src", "agent-network/bin", "agent-node/src"].flatMap(dir => sources(`${root}/${dir}`));
|
|
270
|
+
let count = 0;
|
|
271
|
+
for (const file of sourceFiles) {
|
|
272
|
+
const source = readFileSync(file, "utf8");
|
|
273
|
+
for (const match of source.matchAll(/INSERT\s+INTO\s+api_tokens\s*\(([^)]+)\)/gi)) {
|
|
274
|
+
expect(match[1]).toContain("node_identity_epoch"); count++;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
// Hub #938: clone_node mints a child ntok_ with node_identity_epoch=2,
|
|
278
|
+
// the same column create_node already writes. 9 issuers → 10.
|
|
279
|
+
expect(count).toBe(10);
|
|
280
|
+
expect(readFileSync(`${root}/agent-network/bin/cli.ts`, "utf8")).toContain("'admin-reset', 'user', 1)");
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
test("revoked ownerless credential cannot be reclaimed by doctor and gives actionable guidance", async () => {
|
|
284
|
+
db.run("UPDATE api_tokens SET revoked_at=datetime('now') WHERE name='node:n_ownerless_fixture'");
|
|
285
|
+
const result = await mint(member.token, owner.network_id, "n_ownerless_fixture");
|
|
286
|
+
expect(result.status).toBe(400);
|
|
287
|
+
expect(result.body.error).toBe("node_owner_mismatch");
|
|
288
|
+
expect(nodeTokenReissueError(result.body)).toContain("这个节点没有归属,请联系管理员认领(#682)");
|
|
289
|
+
expect(nodeTokenReissueError({ error: "node_owner_unclaimed" })).toContain("请联系管理员认领(#682)");
|
|
290
|
+
expect(nodeTokenReissueError({ error: "network_not_found" })).toBe("network_not_found");
|
|
291
|
+
expect(readFileSync(`${import.meta.dir}/../../agent-network/bin/cli.ts`, "utf8")).toContain("body ? nodeTokenReissueError(body) : r.status");
|
|
292
|
+
});
|