@sleep2agi/commhub-server 0.9.0-preview.24 → 0.9.0-preview.241
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/package.json +7 -7
- package/src/ack-create-request-transport.test.ts +182 -0
- package/src/ack-create-request.test.ts +50 -7
- 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 +137 -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 +659 -0
- package/src/agent-acl-slice3-http.test.ts +654 -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 +250 -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 +121 -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/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 +248 -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 +120 -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 +289 -0
- package/src/create-node.ts +88 -23
- package/src/daemon-provider-snapshots.test.ts +434 -0
- package/src/daemon-provider-snapshots.ts +327 -0
- package/src/daemon-token-owner-http.test.ts +236 -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/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 +73 -18
- package/src/file-download-date-bucket.test.ts +436 -0
- 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 +482 -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 +1443 -3
- 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 +582 -0
- package/src/hub-provider-config.test.ts +156 -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 +139 -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 +1091 -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 +344 -0
- package/src/node-health-codex-login.test.ts +120 -0
- package/src/node-health-dispatch-http.test.ts +335 -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 +250 -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 +102 -9
- 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 +457 -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/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 +295 -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 +443 -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 +122 -0
- package/src/requirements-comment-http.test.ts +167 -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 +163 -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 +2116 -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 +432 -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 +193 -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 +279 -0
- package/src/scheduled-requirement-wake.ts +125 -0
- package/src/scheduled-run-terminal.test.ts +268 -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/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 +5025 -435
- 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 +264 -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 +490 -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 +287 -0
- package/src/task-expiry-notice.ts +315 -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 +204 -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 +4310 -419
- package/src/upload-early-reject-guard.test.ts +155 -0
- package/src/uploads-http.test.ts +86 -7
- package/src/uploads.ts +103 -6
- 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 +161 -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,582 @@
|
|
|
1
|
+
// #1473 (#909) — the Hub's Skills / secrets / Provider catalogs had MCP tools
|
|
2
|
+
// but no HTTP read surface, so the desktop client shipped a fixture page
|
|
3
|
+
// (agent-network-app/src/hub-scope-demo.ts: "Hub 这些写接口还没接通 ... 不发请求").
|
|
4
|
+
//
|
|
5
|
+
// These three endpoints are read-only and user-token only. The load-bearing
|
|
6
|
+
// properties are:
|
|
7
|
+
// * a NODE token cannot enumerate the network's catalogs;
|
|
8
|
+
// * a plain member of another network cannot read this network's catalogs;
|
|
9
|
+
// * pending SkillHub submissions stay invisible unless the caller reviews;
|
|
10
|
+
// * NO secret value ever appears in a response body (asserted with a
|
|
11
|
+
// sentinel that would be trivially visible if a value were serialized).
|
|
12
|
+
//
|
|
13
|
+
// The sentinel test is deliberately byte-level on the raw response text: a
|
|
14
|
+
// future field like `value_preview` would pass a JSON-shape assertion while
|
|
15
|
+
// still leaking the secret.
|
|
16
|
+
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
17
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
18
|
+
import { tmpdir } from "node:os";
|
|
19
|
+
import { join } from "node:path";
|
|
20
|
+
|
|
21
|
+
const PRIVATE_DB_DIR = mkdtempSync(join(tmpdir(), "anet-hub-catalog-"));
|
|
22
|
+
process.env.COMMHUB_DB ||= join(PRIVATE_DB_DIR, "hub.db");
|
|
23
|
+
// The vault encrypts on write; set the master key before db.ts/vault.ts load.
|
|
24
|
+
process.env.ANET_HUB_SECRET_VAULT_KEY ||= "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff";
|
|
25
|
+
|
|
26
|
+
let server: ReturnType<typeof Bun.serve>;
|
|
27
|
+
let base = "";
|
|
28
|
+
let ownerToken = "", strangerToken = "", nodeToken = "";
|
|
29
|
+
let ownerNet = "", strangerNet = "", ownerUserId = "";
|
|
30
|
+
const SECRET_NAME = "PROVIDER_SENTINEL_KEY";
|
|
31
|
+
const SECRET_VALUE = "sentinel-value-DO-NOT-LEAK-4f9a2c";
|
|
32
|
+
|
|
33
|
+
const get = async (path: string, token: string) => {
|
|
34
|
+
const r = await fetch(`${base}${path}`, { headers: { Authorization: `Bearer ${token}` } });
|
|
35
|
+
const text = await r.text();
|
|
36
|
+
let body: any = null;
|
|
37
|
+
try { body = JSON.parse(text); } catch { body = null; }
|
|
38
|
+
return { status: r.status, body, text };
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
beforeAll(async () => {
|
|
42
|
+
const { db } = await import("./db.js");
|
|
43
|
+
const { register, createNetworkTokenForNode } = await import("./auth.js");
|
|
44
|
+
const { vaultUpsert } = await import("./vault.js");
|
|
45
|
+
const stamp = Date.now();
|
|
46
|
+
|
|
47
|
+
const owner = register(`hc_owner_${stamp}`, "HcOwner-Strong-1!");
|
|
48
|
+
const stranger = register(`hc_stranger_${stamp}`, "HcStranger-Strong-1!");
|
|
49
|
+
expect(owner.ok && stranger.ok).toBe(true);
|
|
50
|
+
ownerToken = owner.token!;
|
|
51
|
+
strangerToken = stranger.token!;
|
|
52
|
+
ownerNet = owner.network_id!;
|
|
53
|
+
strangerNet = stranger.network_id!;
|
|
54
|
+
ownerUserId = db.get<{ user_id: string }>("SELECT user_id FROM users WHERE username = ?1", `hc_owner_${stamp}`)!.user_id;
|
|
55
|
+
|
|
56
|
+
// A node token in the SAME network: it must still be refused, because a node
|
|
57
|
+
// is not a person and may not enumerate catalogs.
|
|
58
|
+
const nodeTok = createNetworkTokenForNode(ownerUserId, ownerNet, "hc-node", "n_hc_test");
|
|
59
|
+
expect(nodeTok.ok).toBe(true);
|
|
60
|
+
nodeToken = nodeTok.token!;
|
|
61
|
+
|
|
62
|
+
// Published + pending skills in the owner's network.
|
|
63
|
+
const skill = (id: string, status: string, slug: string) => db.run(
|
|
64
|
+
`INSERT INTO skillhub_skills (skill_id, network_id, slug, name, description, content, content_hash, version, status, source_type, source_alias, created_at, updated_at)
|
|
65
|
+
VALUES (?1, ?2, ?3, ?3, 'd', '# SKILL body', 'hash-' || ?1, '1.0.0', ?4, 'node', 'hc-node', datetime('now'), datetime('now'))`,
|
|
66
|
+
[id, ownerNet, slug, status],
|
|
67
|
+
);
|
|
68
|
+
skill("skill_hc_pub", "published", "hc-published");
|
|
69
|
+
skill("skill_hc_pending", "pending", "hc-pending");
|
|
70
|
+
|
|
71
|
+
// The sentinel secret. Names may be shown; values never.
|
|
72
|
+
vaultUpsert(ownerNet, SECRET_NAME, SECRET_VALUE);
|
|
73
|
+
|
|
74
|
+
db.run(
|
|
75
|
+
`INSERT INTO hub_provider_configs (network_id, provider_id, revision, config_json, secret_key_ref, updated_at)
|
|
76
|
+
VALUES (?1, 'hc-provider', 3, ?2, ?3, ?4)`,
|
|
77
|
+
[ownerNet, JSON.stringify({ id: "hc-provider", label: "HC Provider", enabled: true, protocol: "openai-responses", baseUrl: "https://example.test/v1", models: ["m1"] }), "HC_PROVIDER_KEY", Date.now()],
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
const mod = await import("./server.js");
|
|
81
|
+
server = mod.bootServer({ port: 0, hostname: "127.0.0.1" });
|
|
82
|
+
base = `http://127.0.0.1:${server.port}`;
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
afterAll(() => {
|
|
86
|
+
try { server?.stop(true); } catch {}
|
|
87
|
+
try { rmSync(PRIVATE_DB_DIR, { recursive: true, force: true }); } catch {}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe("GET /api/skills", () => {
|
|
91
|
+
test("owner sees published skills and no content body", async () => {
|
|
92
|
+
const r = await get(`/api/skills?network_id=${ownerNet}`, ownerToken);
|
|
93
|
+
expect(r.status).toBe(200);
|
|
94
|
+
expect(r.body.ok).toBe(true);
|
|
95
|
+
expect(r.body.reviewer).toBe(true);
|
|
96
|
+
expect(r.body.network_id).toBe(ownerNet);
|
|
97
|
+
const slugs = r.body.skills.map((s: any) => s.slug);
|
|
98
|
+
expect(slugs).toContain("hc-published");
|
|
99
|
+
// Pending is opt-in even for a reviewer.
|
|
100
|
+
expect(slugs).not.toContain("hc-pending");
|
|
101
|
+
// The list is metadata only — a body field would mean SKILL.md text leaked.
|
|
102
|
+
for (const s of r.body.skills) expect(s.content).toBeUndefined();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("reviewer may ask for pending submissions", async () => {
|
|
106
|
+
const r = await get(`/api/skills?network_id=${ownerNet}&include_pending=1`, ownerToken);
|
|
107
|
+
expect(r.status).toBe(200);
|
|
108
|
+
const slugs = r.body.skills.map((s: any) => s.slug);
|
|
109
|
+
expect(slugs).toContain("hc-pending");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// 2026-10-31 —— **两条「providers」读的是不同的表**,而空数组在载荷里长得一样:
|
|
113
|
+
// GET /api/providers → hub_provider_configs | MCP list_providers → providers / provider_models
|
|
114
|
+
// 客户端「没有可选模型」既可能是前者空,也可能是后者空 ⇒ 必须能在载荷里区分。
|
|
115
|
+
test("an empty registry says which table it read, and whether the CLI registry has rows", async () => {
|
|
116
|
+
const { db } = await import("./db.js");
|
|
117
|
+
// strangerNet 里没有 hub_provider_configs 行;先看「两张表都空」这一档
|
|
118
|
+
const before = await get(`/api/providers?network_id=${strangerNet}`, strangerToken);
|
|
119
|
+
expect(before.status).toBe(200);
|
|
120
|
+
expect(before.body.state).toBe("unconfigured");
|
|
121
|
+
expect(before.body.source_table).toBe("hub_provider_configs");
|
|
122
|
+
expect(before.body.counts.providers).toBe(0);
|
|
123
|
+
expect(before.body.hint).toContain("hub_provider_configs");
|
|
124
|
+
|
|
125
|
+
// 再给 CLI 注册表塞一行(另一张表),本端点**仍然为空**,但 hint 必须改口
|
|
126
|
+
db.run(
|
|
127
|
+
`INSERT OR REPLACE INTO providers (provider_id, network_id, name, vendor, base_url, secret_key_ref, created_at, created_by, enabled)
|
|
128
|
+
VALUES ('hc-cli-only', ?1, 'hc-cli-only', 'openai', 'https://api.example.com', 'K', ?2, 'test', 1)`,
|
|
129
|
+
[strangerNet, Date.now()],
|
|
130
|
+
);
|
|
131
|
+
const after = await get(`/api/providers?network_id=${strangerNet}`, strangerToken);
|
|
132
|
+
expect(after.body.providers).toHaveLength(0); // 本端点还是空
|
|
133
|
+
expect(after.body.counts.providers_in_cli_registry).toBeGreaterThan(0);
|
|
134
|
+
expect(after.body.hint).toContain("不会自动出现在本端点");
|
|
135
|
+
// 反向:有行时不该带 hint(载荷不变胖,且「有理由」与「没理由」可区分)
|
|
136
|
+
const owner = await get(`/api/providers?network_id=${ownerNet}`, ownerToken);
|
|
137
|
+
expect(owner.body.state).toBe("configured");
|
|
138
|
+
expect("hint" in owner.body).toBe(false);
|
|
139
|
+
|
|
140
|
+
// 2026-10-31 —— **闭集不许有死成员**:客户端 union 里写着 all_disabled,
|
|
141
|
+
// 而这个端点原来只产出前两个 ⇒ 那一格永远不会出现、界面分支永远跑不到。
|
|
142
|
+
// 本地 hub 实测:建了 enabled:true 的行 ⇒ configured;把 enabled 改成 false ⇒ **仍是 configured**
|
|
143
|
+
// ⇒ 读的人会把「全都关了」看成「配置好了」。现在三态都必须可产出。
|
|
144
|
+
db.run(`UPDATE hub_provider_configs SET config_json = json_set(config_json, '$.enabled', json('false'))
|
|
145
|
+
WHERE network_id = ?1`, [ownerNet]);
|
|
146
|
+
const disabled = await get(`/api/providers?network_id=${ownerNet}`, ownerToken);
|
|
147
|
+
expect(disabled.body.state).toBe("all_disabled");
|
|
148
|
+
expect(disabled.body.counts.providers_enabled).toBe(0);
|
|
149
|
+
expect(disabled.body.counts.providers_disabled).toBeGreaterThan(0);
|
|
150
|
+
expect(disabled.body.counts.models_enabled).toBe(0);
|
|
151
|
+
expect(disabled.body.hint).toContain("全部被禁用");
|
|
152
|
+
expect(disabled.body.hint).toContain("不是「还没配置」");
|
|
153
|
+
|
|
154
|
+
// 还原:否则后面的用例会看到一个全禁用的网络
|
|
155
|
+
db.run(`UPDATE hub_provider_configs SET config_json = json_set(config_json, '$.enabled', json('true'))
|
|
156
|
+
WHERE network_id = ?1`, [ownerNet]);
|
|
157
|
+
const back = await get(`/api/providers?network_id=${ownerNet}`, ownerToken);
|
|
158
|
+
expect(back.body.state).toBe("configured");
|
|
159
|
+
expect(back.body.counts.models_enabled).toBeGreaterThan(0);
|
|
160
|
+
|
|
161
|
+
// 2026-10-31 —— **形状测试必须先证明输入彼此不同,再断言各自属性**
|
|
162
|
+
// (外部复核 ③:「顺序不能反:先断言几种形状的字节两两不等,再断言各自含有应有的属性;
|
|
163
|
+
// 反过来时『四种其实是一种』也能全绿。」)
|
|
164
|
+
// 这里是三态:删掉 state 之后,三份载荷必须两两不同 —— 否则「三态」只是同一个载荷换了标签。
|
|
165
|
+
const strip = (b: any) => { const { state: _s, ...rest } = b; return JSON.stringify(rest); };
|
|
166
|
+
const three = [strip(before.body), strip(disabled.body), strip(back.body)];
|
|
167
|
+
expect(new Set(three).size).toBe(3);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test("a member of another network is refused", async () => {
|
|
171
|
+
const r = await get(`/api/skills?network_id=${ownerNet}`, strangerToken);
|
|
172
|
+
expect(r.status).toBe(403);
|
|
173
|
+
// Cross-network access is refused by REST scope resolution BEFORE the role
|
|
174
|
+
// check runs, so the error names the scope gate. Either way: 403, no data.
|
|
175
|
+
expect(String(r.body.error)).toMatch(/access denied|permission_denied/);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("a node token is refused even inside its own network", async () => {
|
|
179
|
+
const r = await get(`/api/skills?network_id=${ownerNet}`, nodeToken);
|
|
180
|
+
expect(r.status).toBe(403);
|
|
181
|
+
expect(r.body.error).toBe("user_token_required");
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test("include_pending is ignored for a non-reviewer of that network", async () => {
|
|
185
|
+
// The stranger's own network has no skills; the point is that asking for
|
|
186
|
+
// pending must not widen what they can see when they ask about their own.
|
|
187
|
+
const r = await get(`/api/skills?network_id=${strangerNet}&include_pending=1`, strangerToken);
|
|
188
|
+
expect(r.status).toBe(200);
|
|
189
|
+
expect(r.body.reviewer).toBe(true); // they own their own network
|
|
190
|
+
expect(r.body.skills).toEqual([]);
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
describe("GET /api/secrets", () => {
|
|
195
|
+
test("returns key names and timestamps, never the value", async () => {
|
|
196
|
+
const r = await get(`/api/secrets?network_id=${ownerNet}`, ownerToken);
|
|
197
|
+
expect(r.status).toBe(200);
|
|
198
|
+
const names = r.body.keys.map((k: any) => k.key);
|
|
199
|
+
expect(names).toContain(SECRET_NAME);
|
|
200
|
+
// <-- The load-bearing assertion: raw body text must not contain the value.
|
|
201
|
+
expect(r.text).not.toContain(SECRET_VALUE);
|
|
202
|
+
expect(r.text).not.toContain("ciphertext");
|
|
203
|
+
expect(r.text).not.toContain("iv");
|
|
204
|
+
expect(r.text).not.toContain("tag");
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test("a member of another network is refused and learns no key names", async () => {
|
|
208
|
+
const r = await get(`/api/secrets?network_id=${ownerNet}`, strangerToken);
|
|
209
|
+
expect(r.status).toBe(403);
|
|
210
|
+
expect(r.text).not.toContain(SECRET_NAME);
|
|
211
|
+
expect(r.text).not.toContain(SECRET_VALUE);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test("a node token is refused", async () => {
|
|
215
|
+
const r = await get(`/api/secrets?network_id=${ownerNet}`, nodeToken);
|
|
216
|
+
expect(r.status).toBe(403);
|
|
217
|
+
expect(r.body.error).toBe("user_token_required");
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
describe("GET /api/providers", () => {
|
|
222
|
+
test("returns the registry without secret references", async () => {
|
|
223
|
+
const r = await get(`/api/providers?network_id=${ownerNet}`, ownerToken);
|
|
224
|
+
expect(r.status).toBe(200);
|
|
225
|
+
expect(r.body.providers).toHaveLength(1);
|
|
226
|
+
expect(r.body.providers[0]).toMatchObject({ id: "hc-provider", label: "HC Provider", source: "hub" });
|
|
227
|
+
// The vault key NAME is a hint an attacker does not need; `exposeRefs` is
|
|
228
|
+
// false here on purpose.
|
|
229
|
+
expect(r.body.providers[0].secret_key_ref).toBeUndefined();
|
|
230
|
+
expect(r.text).not.toContain("HC_PROVIDER_KEY");
|
|
231
|
+
expect(r.text).not.toContain(SECRET_VALUE);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
test("a member of another network is refused", async () => {
|
|
235
|
+
const r = await get(`/api/providers?network_id=${ownerNet}`, strangerToken);
|
|
236
|
+
expect(r.status).toBe(403);
|
|
237
|
+
expect(r.text).not.toContain("hc-provider");
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
test("a node token is refused", async () => {
|
|
241
|
+
const r = await get(`/api/providers?network_id=${ownerNet}`, nodeToken);
|
|
242
|
+
expect(r.status).toBe(403);
|
|
243
|
+
expect(r.body.error).toBe("user_token_required");
|
|
244
|
+
});
|
|
245
|
+
// 2026-10-31 —— **写侧原来只有 MCP**:客户端能显示「还没有 provider」,却没办法把它变成「有」。
|
|
246
|
+
// 这条把「第一行」确认成一次 HTTP 调用,并核对三件事:落库、读回来一致、revision 冲突是 409。
|
|
247
|
+
test("PUT /api/providers creates the first row, and the read side sees it", async () => {
|
|
248
|
+
const body = {
|
|
249
|
+
revision: 0,
|
|
250
|
+
provider: {
|
|
251
|
+
id: "hc-first", label: "First Provider", enabled: true,
|
|
252
|
+
protocol: "openai-chat-completions", baseUrl: "https://api.example.com/v1",
|
|
253
|
+
models: ["gpt-x"], key: "sk-test-not-a-real-secret",
|
|
254
|
+
},
|
|
255
|
+
};
|
|
256
|
+
const r = await fetch(`${base}/api/providers?network_id=${ownerNet}`, {
|
|
257
|
+
method: "PUT",
|
|
258
|
+
headers: { Authorization: `Bearer ${ownerToken}`, "content-type": "application/json" },
|
|
259
|
+
body: JSON.stringify(body),
|
|
260
|
+
});
|
|
261
|
+
const saved = await r.json() as any;
|
|
262
|
+
expect(r.status).toBe(200);
|
|
263
|
+
expect(saved.ok).toBe(true);
|
|
264
|
+
expect(saved.provider.id).toBe("hc-first");
|
|
265
|
+
expect(saved.provider.revision).toBe(1);
|
|
266
|
+
// 密钥永不回显
|
|
267
|
+
expect(JSON.stringify(saved)).not.toContain("sk-test-not-a-real-secret");
|
|
268
|
+
|
|
269
|
+
const read = await get(`/api/providers?network_id=${ownerNet}`, ownerToken);
|
|
270
|
+
const ids = read.body.providers.map((p: any) => p.id);
|
|
271
|
+
expect(ids).toContain("hc-first");
|
|
272
|
+
|
|
273
|
+
// 用旧 revision 再写一次 ⇒ 409(乐观锁;这正是「两个人同时改」的那一格)
|
|
274
|
+
const stale = await fetch(`${base}/api/providers?network_id=${ownerNet}`, {
|
|
275
|
+
method: "PUT",
|
|
276
|
+
headers: { Authorization: `Bearer ${ownerToken}`, "content-type": "application/json" },
|
|
277
|
+
body: JSON.stringify({ ...body, revision: 0, provider: { ...body.provider, label: "Second Writer" } }),
|
|
278
|
+
});
|
|
279
|
+
expect(stale.status).toBe(409);
|
|
280
|
+
expect((await stale.json() as any).error).toBe("hub_provider_revision_conflict");
|
|
281
|
+
|
|
282
|
+
// 别的网络成员不许写
|
|
283
|
+
const foreign = await fetch(`${base}/api/providers?network_id=${ownerNet}`, {
|
|
284
|
+
method: "PUT",
|
|
285
|
+
headers: { Authorization: `Bearer ${strangerToken}`, "content-type": "application/json" },
|
|
286
|
+
body: JSON.stringify({ ...body, revision: 1 }),
|
|
287
|
+
});
|
|
288
|
+
expect(foreign.status).toBe(403);
|
|
289
|
+
|
|
290
|
+
// 清理:这条测试造了一行,后面的断言按「1 行」计数,不能留痕
|
|
291
|
+
const { db } = await import("./db.js");
|
|
292
|
+
db.run(`DELETE FROM hub_provider_configs WHERE network_id = ?1 AND provider_id = 'hc-first'`, [ownerNet]);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
// 2026-10-31 —— **写侧状态码是穷尽表**(外部复核:三元式默认分支 = 未映射与真实含义同值)。
|
|
297
|
+
// 两条断言分开:① 映射键 == 码清单;② 码清单里的每个码都真的在源码里被产出(跨源)。
|
|
298
|
+
test("provider write: every error code has a status, and every status code is really produced", async () => {
|
|
299
|
+
const { PROVIDER_WRITE_ERROR_CODES, PROVIDER_WRITE_STATUS } = await import("./hub-provider-config.js");
|
|
300
|
+
const { readFileSync } = await import("node:fs");
|
|
301
|
+
expect(Object.keys(PROVIDER_WRITE_STATUS).sort()).toEqual([...PROVIDER_WRITE_ERROR_CODES].sort());
|
|
302
|
+
for (const code of PROVIDER_WRITE_ERROR_CODES) {
|
|
303
|
+
expect(Number.isInteger(PROVIDER_WRITE_STATUS[code])).toBe(true);
|
|
304
|
+
expect(PROVIDER_WRITE_STATUS[code]).toBeGreaterThanOrEqual(400);
|
|
305
|
+
}
|
|
306
|
+
// 跨源:这些码必须能在源码里找到(`denied('x')` 或 vault 的 code 字面量),
|
|
307
|
+
// 否则清单会退化成「我们以为会发生的错误」。
|
|
308
|
+
const src = readFileSync(new URL("./hub-provider-config.ts", import.meta.url), "utf8");
|
|
309
|
+
const missing = [...PROVIDER_WRITE_ERROR_CODES].filter((c) => !src.includes(`'${c}'`) && !src.includes(`"${c}"`));
|
|
310
|
+
if (missing.length) throw new Error(`码清单里这些码在源码中找不到产出点:${missing.join(", ")}`);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
// 2026-10-31 —— 节点当前 provider/模型的三态读口(#3145 的数据面)。
|
|
314
|
+
// 关键:**从未切换过 / 正在切 / 切失败** 必须能分开 —— 界面给的下一步完全不同。
|
|
315
|
+
test("GET /api/nodes/:id/provider-selection distinguishes never_switched / pending / applied / failed", async () => {
|
|
316
|
+
const { db } = await import("./db.js");
|
|
317
|
+
const nodeId = "ps-probe-node";
|
|
318
|
+
db.run(`INSERT OR REPLACE INTO nodes (node_id, node_name, network_id, alias, created_at, updated_at)
|
|
319
|
+
VALUES (?1, 'ps-probe', ?2, 'ps-probe', ?3, ?3)`, [nodeId, ownerNet, Date.now()]);
|
|
320
|
+
const read = async () => {
|
|
321
|
+
const r = await fetch(`${base}/api/nodes/${nodeId}/provider-selection`, { headers: { Authorization: `Bearer ${ownerToken}` } });
|
|
322
|
+
return { status: r.status, body: await r.json() as any };
|
|
323
|
+
};
|
|
324
|
+
try {
|
|
325
|
+
// ① 从未切换过:带 hint,selection 为 null(不假装是 Hub 的读数)
|
|
326
|
+
const a = await read();
|
|
327
|
+
expect(a.status).toBe(200);
|
|
328
|
+
expect(a.body.state).toBe("never_switched");
|
|
329
|
+
expect(a.body.selection).toBeNull();
|
|
330
|
+
expect(String(a.body.hint)).toContain("本地默认配置");
|
|
331
|
+
|
|
332
|
+
// ② 正在切:pending 时不带 hint(hint 只属于「没有读数」那一档)
|
|
333
|
+
const q = (requestId: string, status: string, payload: string | null, error: string | null) => db.run(
|
|
334
|
+
`INSERT OR REPLACE INTO daemon_provider_snapshots (request_id, network_id, daemon_node_id, requested_by, status, created_at, expires_at, observed_at, snapshot_json, error)
|
|
335
|
+
VALUES (?1, ?2, ?3, 'test', ?4, ?5, ?6, ?5, ?7, ?8)`,
|
|
336
|
+
[requestId, ownerNet, nodeId, status, Date.now(), Date.now() + 60_000, payload, error],
|
|
337
|
+
);
|
|
338
|
+
q("dpa_probe_1", "pending", null, null);
|
|
339
|
+
const b = await read();
|
|
340
|
+
expect(b.body.state).toBe("pending");
|
|
341
|
+
expect("hint" in b.body).toBe(false);
|
|
342
|
+
|
|
343
|
+
// ③ 已生效:selection 原样带回,并给出 revision
|
|
344
|
+
// 2026-10-31 —— 这条 fixture 原来写 `"completed"`,而**生产端从不写这个值**
|
|
345
|
+
// (daemon 成功时写的是 `succeeded`,见 daemon-provider-snapshots.ts:322)。
|
|
346
|
+
// 于是这条断言只在「兜底分支」上成立:它证明的不是「applied 状态能读到」,
|
|
347
|
+
// 而是「任何没枚举到的值都会被读成 applied」——一条把缺陷当契约保护起来的断言。
|
|
348
|
+
// ⇒ fixture 改用生产者真的会写的值;兜底方向另立一条断言(见下面的 unknown 用例)。
|
|
349
|
+
q("dpa_probe_2", "succeeded", JSON.stringify({ expected_revision: 7, provider_id: "main", model: "m1" }), null);
|
|
350
|
+
const c = await read();
|
|
351
|
+
expect(c.body.state).toBe("applied");
|
|
352
|
+
expect(c.body.selection).toMatchObject({ provider_id: "main", model: "m1" });
|
|
353
|
+
expect(c.body.revision).toBe(7);
|
|
354
|
+
expect(typeof c.body.observed_at).toBe("string");
|
|
355
|
+
|
|
356
|
+
// ④ 失败:必须带 error(否则界面只能说「失败了」)
|
|
357
|
+
q("dpa_probe_3", "failed", null, "daemon_rejected_selection");
|
|
358
|
+
const d = await read();
|
|
359
|
+
expect(d.body.state).toBe("failed");
|
|
360
|
+
expect(d.body.error).toBe("daemon_rejected_selection");
|
|
361
|
+
|
|
362
|
+
// ⑤ 未知状态:**兜底不许是最乐观的那一档**
|
|
363
|
+
// 原实现是 `… ? "failed" : "applied"` ⇒ 任何没被枚举到的 status 都折叠成「已生效」。
|
|
364
|
+
// daemon 侧只要新增一个状态(取消 / 回滚 / 拼错),界面就会读到「切好了」—— 唯一会主动撒谎的折叠方向。
|
|
365
|
+
// 现在闭集显式映射,不认识的值报 unknown 且把原值带出去(不猜、不美化)。
|
|
366
|
+
q("dpa_probe_4", "cancelled_by_operator", null, null);
|
|
367
|
+
const e = await read();
|
|
368
|
+
expect(e.body.state).toBe("unknown");
|
|
369
|
+
expect(e.body.state).not.toBe("applied");
|
|
370
|
+
expect(e.body.observed_status).toBe("cancelled_by_operator");
|
|
371
|
+
expect(String(e.body.hint)).toContain("不等于已生效");
|
|
372
|
+
|
|
373
|
+
// 2026-10-31 —— **要数的是「出口量里能独立变化的维度数」,不是「有几条路径」**
|
|
374
|
+
// (外部复核 (d):「出口三元组实测秩 = 1:头无条件设置、键数恒为 3、状态是 code 的纯函数
|
|
375
|
+
// ⇒ A、B 两条向量改变的是同一个出口量」)。
|
|
376
|
+
// 对应到本路由:五个 state 如果**去掉 state 字段**就分不出来,那 state 就是唯一信息源,
|
|
377
|
+
// 载荷只是标签的复读 —— 秩 = 1。判据:把 state 删掉之后,五个载荷仍两两可区分。
|
|
378
|
+
const strip = (b: any) => { const { state: _s, ...rest } = b; return JSON.stringify(rest); };
|
|
379
|
+
const five = [a, b, c, d, e].map((r) => strip(r.body));
|
|
380
|
+
expect(new Set(five).size).toBe(5); // 两两不同 ⇒ 秩 ≥ 2,state 不是唯一维度
|
|
381
|
+
} finally {
|
|
382
|
+
db.run(`DELETE FROM daemon_provider_snapshots WHERE daemon_node_id = ?1`, [nodeId]);
|
|
383
|
+
db.run(`DELETE FROM node_daemon_bindings WHERE request_id = 'bind_ps'`);
|
|
384
|
+
db.run(`DELETE FROM nodes WHERE node_id = 'ps-target-node'`);
|
|
385
|
+
db.run(`DELETE FROM api_tokens WHERE token_id = 'tok_ps_write'`);
|
|
386
|
+
db.run(`DELETE FROM nodes WHERE node_id = ?1`, [nodeId]);
|
|
387
|
+
}
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
// 2026-10-31 —— 写侧:切 provider 的**第一步**(排队 + 带切换前读数)。
|
|
391
|
+
// 关键断言不是「返回 202」,而是「它把 before 带回来了」——没有 before,
|
|
392
|
+
// 「切成功了吗」只能靠客户端自己记住上一次。
|
|
393
|
+
test("POST /api/nodes/:id/provider-selection queues an application and returns the pre-state", async () => {
|
|
394
|
+
const { db } = await import("./db.js");
|
|
395
|
+
const nodeId = "ps-write-node";
|
|
396
|
+
// provider 切换只对 host_supervisor 守护进程有意义 —— 那条前置条件本身也要先被证明:
|
|
397
|
+
// 普通节点 ⇒ 404 daemon_not_found(下面第一条断言)。
|
|
398
|
+
db.run(`INSERT OR REPLACE INTO nodes (node_id, node_name, network_id, alias, created_at, updated_at, config_snapshot)
|
|
399
|
+
VALUES (?1, 'ps-write', ?2, 'ps-write', ?3, ?3, '{}')`, [nodeId, ownerNet, Date.now()]);
|
|
400
|
+
const selection0 = { node_id: nodeId, node_revision: "a".repeat(64), provider_id: "main", auth_id: "auth1", model: "m1", confirm_restart: true };
|
|
401
|
+
const postRaw = async (body: any) => {
|
|
402
|
+
const r = await fetch(`${base}/api/nodes/${nodeId}/provider-selection`, {
|
|
403
|
+
method: "POST",
|
|
404
|
+
headers: { Authorization: `Bearer ${ownerToken}`, "content-type": "application/json" },
|
|
405
|
+
body: JSON.stringify(body),
|
|
406
|
+
});
|
|
407
|
+
return { status: r.status, body: await r.json() as any };
|
|
408
|
+
};
|
|
409
|
+
db.run(`INSERT OR REPLACE INTO daemon_provider_snapshots (request_id, network_id, daemon_node_id, requested_by, status, created_at, expires_at, observed_at, snapshot_json, error)
|
|
410
|
+
VALUES ('dpa_prev', ?1, ?2, 'test', 'completed', ?3, ?4, ?3, ?5, NULL)`,
|
|
411
|
+
[ownerNet, nodeId, Date.now(), Date.now() + 60_000, JSON.stringify({ expected_revision: 3, provider_id: "prev", model: "m0" })]);
|
|
412
|
+
// 真实语义:切的是**被这个守护进程监督的节点**,不是守护进程自己 ⇒ 需要一个 target 节点 + 一条 active 绑定
|
|
413
|
+
const targetId = "ps-target-node";
|
|
414
|
+
db.run(`INSERT OR REPLACE INTO nodes (node_id, node_name, network_id, alias, created_at, updated_at)
|
|
415
|
+
VALUES (?1, 'ps-target', ?2, 'ps-target', ?3, ?3)`, [targetId, ownerNet, Date.now()]);
|
|
416
|
+
db.run(`INSERT OR REPLACE INTO node_daemon_bindings (request_id, network_id, node_id, daemon_node_id, workdir, requested_by, status, created_at, updated_at)
|
|
417
|
+
VALUES ('bind_ps', ?1, ?2, ?3, '/tmp', 'test', 'active', ?4, ?4)`, [ownerNet, targetId, nodeId, Date.now()]);
|
|
418
|
+
const selection = { node_id: targetId, node_revision: "a".repeat(64), provider_id: "main", auth_id: "auth1", model: "m1", confirm_restart: true };
|
|
419
|
+
try {
|
|
420
|
+
const post = async (body: any) => {
|
|
421
|
+
const r = await fetch(`${base}/api/nodes/${nodeId}/provider-selection`, {
|
|
422
|
+
method: "POST",
|
|
423
|
+
headers: { Authorization: `Bearer ${ownerToken}`, "content-type": "application/json" },
|
|
424
|
+
body: JSON.stringify(body),
|
|
425
|
+
});
|
|
426
|
+
return { status: r.status, body: await r.json() as any };
|
|
427
|
+
};
|
|
428
|
+
// ① 普通节点(不是 host_supervisor)⇒ 404:切换只对守护进程有意义
|
|
429
|
+
expect((await postRaw({ revision: 3, selection: selection0 })).status).toBe(404);
|
|
430
|
+
|
|
431
|
+
// 补上前置条件:节点既要有 node-scoped token,也要 config_snapshot.role=host_supervisor
|
|
432
|
+
db.run(`INSERT OR REPLACE INTO api_tokens (token_id, token_hash, user_id, network_id, name, created_at)
|
|
433
|
+
VALUES ('tok_ps_write', 'hash', ?1, ?2, 'node:ps-write', datetime('now'))`, [ownerUserId, ownerNet]);
|
|
434
|
+
db.run(`UPDATE nodes SET config_snapshot = '{"role":"host_supervisor"}' WHERE node_id = ?1`, [nodeId]);
|
|
435
|
+
|
|
436
|
+
// 缺 revision ⇒ 400(且提示「先 GET 同一路由」)
|
|
437
|
+
const noRev = await post({ selection });
|
|
438
|
+
expect(noRev.status).toBe(400);
|
|
439
|
+
expect(noRev.body.error).toBe("invalid_revision");
|
|
440
|
+
|
|
441
|
+
// 正常:202 + state=pending + before 是上一次生效的 selection
|
|
442
|
+
const ok = await post({ revision: 3, selection });
|
|
443
|
+
if (ok.status !== 202) throw new Error(`期望 202,实际 ${ok.status}:${JSON.stringify(ok.body)}`);
|
|
444
|
+
expect(ok.status).toBe(202);
|
|
445
|
+
expect(ok.body.state).toBe("pending");
|
|
446
|
+
expect(ok.body.request_id).toMatch(/^dpa_/);
|
|
447
|
+
expect(ok.body.before).toMatchObject({ provider_id: "prev", model: "m0" });
|
|
448
|
+
expect(String(ok.body.hint)).toContain("pending 不等于生效");
|
|
449
|
+
|
|
450
|
+
// 回读:pending 状态在 GET 上立刻可见(写与读用同一个状态源)
|
|
451
|
+
const get = await fetch(`${base}/api/nodes/${nodeId}/provider-selection`, { headers: { Authorization: `Bearer ${ownerToken}` } });
|
|
452
|
+
const got = await get.json() as any;
|
|
453
|
+
expect(got.state).toBe("pending");
|
|
454
|
+
} finally {
|
|
455
|
+
db.run(`DELETE FROM daemon_provider_applications WHERE request_id IN (SELECT request_id FROM daemon_provider_snapshots WHERE daemon_node_id = ?1)`, [nodeId]);
|
|
456
|
+
db.run(`DELETE FROM daemon_provider_snapshots WHERE daemon_node_id = ?1`, [nodeId]);
|
|
457
|
+
db.run(`DELETE FROM node_daemon_bindings WHERE request_id = 'bind_ps'`);
|
|
458
|
+
db.run(`DELETE FROM nodes WHERE node_id = 'ps-target-node'`);
|
|
459
|
+
db.run(`DELETE FROM api_tokens WHERE token_id = 'tok_ps_write'`);
|
|
460
|
+
db.run(`DELETE FROM nodes WHERE node_id = ?1`, [nodeId]);
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
// 2026-10-31 —— **格式约束与数量约束分开**(外部复核 (a):
|
|
465
|
+
// 「白名单管『怎么写』,这条管『写几处』」)。
|
|
466
|
+
// 这两张状态映射各自只能有**一处定义**,且必须被生产代码**至少读一次**:
|
|
467
|
+
// 只有定义、没有读取 ⇒ 它是一张装饰表(写 1 处、读 0 处)。
|
|
468
|
+
test("status maps are defined exactly once and read by production code (a comment mentioning them does not count)", async () => {
|
|
469
|
+
const { readFileSync, readdirSync, statSync } = await import("node:fs");
|
|
470
|
+
const { join } = await import("node:path");
|
|
471
|
+
const root = join(import.meta.dir);
|
|
472
|
+
const walk = (dir: string, out: string[] = []): string[] => {
|
|
473
|
+
for (const name of readdirSync(dir)) {
|
|
474
|
+
const full = join(dir, name);
|
|
475
|
+
if (statSync(full).isDirectory()) walk(full, out);
|
|
476
|
+
else if (name.endsWith(".ts") && !name.endsWith(".test.ts")) out.push(full);
|
|
477
|
+
}
|
|
478
|
+
return out;
|
|
479
|
+
};
|
|
480
|
+
const files = walk(root).map((f) => ({ f, src: readFileSync(f, "utf8") }));
|
|
481
|
+
for (const name of ["PROVIDER_WRITE_STATUS", "TAG_CREATE_STATUS"]) {
|
|
482
|
+
const defs = files.filter(({ src }) => src.includes(`export const ${name}`));
|
|
483
|
+
expect(defs.length).toBe(1);
|
|
484
|
+
const readers = files.flatMap(({ f, src }) =>
|
|
485
|
+
src.split("\n").map((line, i) => ({ line, f, i }))
|
|
486
|
+
.filter(({ line }) => line.includes(name) && !line.includes(`export const ${name}`))
|
|
487
|
+
// 2026-10-31 —— **注释不是读点**:`// 状态码来自穷尽表(PROVIDER_WRITE_STATUS…)`
|
|
488
|
+
// 会满足「读到过」而实际上没有任何代码用它。这正是本仓实测到的形态。
|
|
489
|
+
.filter(({ line }) => {
|
|
490
|
+
const t = line.trim();
|
|
491
|
+
return !t.startsWith("//") && !t.startsWith("*") && !t.startsWith("/*");
|
|
492
|
+
}));
|
|
493
|
+
// 「读」= 出现但不是定义行(索引 / 展开 / 断言都算)
|
|
494
|
+
expect(readers.length).toBeGreaterThan(0);
|
|
495
|
+
// 2026-10-31 —— **计数 = 1 还不够,位置也要在集合内**(外部复核:
|
|
496
|
+
// 「只断言计数 1 ⇒ 留下的是『1 个读点,它自己另搞一套』—— 完全满足计数 1,
|
|
497
|
+
// 而语义已经不是你想的那个。」)
|
|
498
|
+
// ⇒ 读点必须落在**路由文件**里(server.ts),否则等于表有了、用法另有其处。
|
|
499
|
+
// 位置:读点必须落在**实现或路由**里(不是只落在注释里)
|
|
500
|
+
// 2026-10-31 —— **同值不能证明同源**(外部复核:「facade 走映射链、pty 直接写字面,
|
|
501
|
+
// 今天同值,而链改了 pty 不跟 ⇒ 任何静态断言都看不见这一格;
|
|
502
|
+
// 同值能证明它们此刻一致,不能证明它们受同一个约束。」)
|
|
503
|
+
//
|
|
504
|
+
// 本仓的同形:`state` 的取值域由**服务端**产出(闭集映射 + never_switched + unknown),
|
|
505
|
+
// 而**客户端**的 TS union 是另一份手写清单。两份今天相等,改一边不会红。
|
|
506
|
+
// 判据:把「服务端实际可产出的集合」与「客户端 union 的集合」直接比;
|
|
507
|
+
// 并且这条比较本身必须能红(witness:改客户端 union 少一个值 ⇒ 立刻红)。
|
|
508
|
+
const { SELECTION_STATE_BY_STATUS } = await import("./server.js");
|
|
509
|
+
const serverStates = new Set<string>([
|
|
510
|
+
"never_switched",
|
|
511
|
+
...Object.values(SELECTION_STATE_BY_STATUS as Record<string, string>),
|
|
512
|
+
"unknown",
|
|
513
|
+
]);
|
|
514
|
+
const clientSrc = readFileSync(join(import.meta.dir, "..", "..", "prototype", "anet-client-app", "src", "api.ts"), "utf8");
|
|
515
|
+
const unionLine = clientSrc.split("\n").find((l) => l.includes("export type ProviderSelectionState")) ?? "";
|
|
516
|
+
const clientStates = new Set([...unionLine.matchAll(/"([a-z_]+)"/g)].map((m) => m[1]));
|
|
517
|
+
expect(clientStates.size).toBeGreaterThan(0); // 解析不出 union ⇒ 没有读数
|
|
518
|
+
expect([...clientStates].sort()).toEqual([...serverStates].sort());
|
|
519
|
+
// 空集守卫:两边都不能是空(否则「相等」是真空真)
|
|
520
|
+
expect(serverStates.size).toBe(5);
|
|
521
|
+
|
|
522
|
+
// 2026-10-31 —— **兜底不许把「我们的缺陷」说成「客户端的错」**:
|
|
523
|
+
// 表里查不到的码 ⇒ 归因指向了错误的行动方(返回 400 会让客户端去改自己的请求)。
|
|
524
|
+
// 判据同「遍历每个抛点断言它不等于 catch-all」:逐个码断言它映射到**自己那一格**,
|
|
525
|
+
// 再断言未登记的码落 500 而不是 400。
|
|
526
|
+
const { PROVIDER_WRITE_ERROR_CODES: CODES, PROVIDER_WRITE_STATUS: STATUS, providerWriteStatus: statusOf } =
|
|
527
|
+
await import("./hub-provider-config.js");
|
|
528
|
+
const CLOSED = new Set([400, 401, 403, 404, 409, 500, 503]);
|
|
529
|
+
// 2026-10-31 —— **把「恒真的断言」换成能假的那条**(外部复核 (a):
|
|
530
|
+
// 「同一个字面量 code 有两个 id 生产者,而选择依据是另一个观测量 ⇒
|
|
531
|
+
// 两条测试都没覆盖第二个分支 ⇒ `observed.id === body.error.diagnosticId` 永远成立」)。
|
|
532
|
+
//
|
|
533
|
+
// 本仓原来这里写的是 `expect(statusOf(code)).toBe(STATUS[code])` ——
|
|
534
|
+
// 而 statusOf 的实现就是 `STATUS[code] ?? 500` ⇒ 对表里的码**恒真**,
|
|
535
|
+
// 它证明不了任何东西(把状态表整个改成 200 它也绿,只要两边一起改)。
|
|
536
|
+
// ⇒ 换成两条能假、且各自守一件事的断言:
|
|
537
|
+
// ① 码清单与状态表的**键集相等**(新增一个码却没给状态 ⇒ 红)
|
|
538
|
+
// ② 每个值落在 HTTP 闭集里(有人写 299 / 0 ⇒ 红)
|
|
539
|
+
expect(Object.keys(STATUS).sort()).toEqual([...CODES].sort());
|
|
540
|
+
for (const code of CODES) {
|
|
541
|
+
expect(CLOSED.has(STATUS[code])).toBe(true);
|
|
542
|
+
}
|
|
543
|
+
// 镜像(must-fire):一个**不在表里**的码必须落到 500,不能落 400
|
|
544
|
+
const bogus = statusOf("definitely_not_a_listed_code" as any);
|
|
545
|
+
expect(bogus).toBe(500);
|
|
546
|
+
expect(bogus).not.toBe(400);
|
|
547
|
+
|
|
548
|
+
const readerFiles = new Set(readers.map((r) => String(r.f).split("/").pop()));
|
|
549
|
+
expect(readerFiles.size).toBeGreaterThan(0);
|
|
550
|
+
expect([...readerFiles].some((f) => f === "server.ts" || f === "hub-provider-config.ts" || f === "requirements.ts")).toBe(true);
|
|
551
|
+
}
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
// 2026-10-31 —— **枚举里不能有打不中的值**(外部复核:
|
|
555
|
+
// 「5 值枚举里 2 个不可达(stage_missing / id_not_uuid)——那是装饰,
|
|
556
|
+
// 而读者会以为那两条分支存在且被测过」)。
|
|
557
|
+
// 判据:每个成员的字面量必须在**生产源码**里出现(类型声明行不算)。
|
|
558
|
+
test("every enum member is actually produced somewhere in production code", async () => {
|
|
559
|
+
const { readFileSync, readdirSync, statSync } = await import("node:fs");
|
|
560
|
+
const { join } = await import("node:path");
|
|
561
|
+
const walk = (dir: string, out: string[] = []): string[] => {
|
|
562
|
+
for (const name of readdirSync(dir)) {
|
|
563
|
+
const full = join(dir, name);
|
|
564
|
+
if (statSync(full).isDirectory()) walk(full, out);
|
|
565
|
+
else if (name.endsWith(".ts") && !name.endsWith(".test.ts")) out.push(full);
|
|
566
|
+
}
|
|
567
|
+
return out;
|
|
568
|
+
};
|
|
569
|
+
const prod = walk(join(import.meta.dir)).map((f) => readFileSync(f, "utf8")).join("\n");
|
|
570
|
+
const enums: Record<string, string[]> = {
|
|
571
|
+
version_source: ["embedded_env", "package_json", "read_failed", "empty_value"],
|
|
572
|
+
provider_selection_state: ["never_switched", "pending", "applied", "failed"],
|
|
573
|
+
};
|
|
574
|
+
for (const [label, members] of Object.entries(enums)) {
|
|
575
|
+
const dead = members.filter((m) => !new RegExp(`["']${m}["']`).test(prod));
|
|
576
|
+
if (dead.length) {
|
|
577
|
+
throw new Error(`${label} 里有打不中成员(生产源码没出现过这个字面量):${dead.join(", ")}\n` +
|
|
578
|
+
" ⇒ 要么删掉该成员,要么补上产出它的分支;留着等于给读者一条不存在的分支。");
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
});
|