@sleep2agi/commhub-server 0.9.0-preview.24 → 0.9.0-preview.240
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 +564 -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 +112 -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 +5010 -429
- 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,73 @@
|
|
|
1
|
+
// 2026-10-31 —— 「设了空」与「没设」是两件事。
|
|
2
|
+
//
|
|
3
|
+
// 起因(外部复核):`'TMAICLOUD_PUBLIC_ORIGIN' in process.env` 才能区分「设了空」与「没设」,
|
|
4
|
+
// 而 `if (process.env.X)` 把两者都判假。
|
|
5
|
+
//
|
|
6
|
+
// 在本仓的落点:COMMHUB_CORS_ORIGINS。
|
|
7
|
+
// · 未设 ⇒ 开发默认(localhost 两个端口)
|
|
8
|
+
// · 设了但为空 ⇒ **空清单**(谁都不许)
|
|
9
|
+
// 如果两者都判假并回退默认,那么运维写 `COMMHUB_CORS_ORIGINS=`(想「一个都不许」)
|
|
10
|
+
// 得到的却是「放行 localhost」—— 空落到了**更松**的一侧。
|
|
11
|
+
//
|
|
12
|
+
// 判据:写一个允许清单的默认值之前,先写出它的**最松值**。
|
|
13
|
+
// 这里最松值不是空(空 = 谁都不许 = 最严),而是 `*`。所以空绝不能被读成「用默认」。
|
|
14
|
+
import { describe, expect, test } from "bun:test";
|
|
15
|
+
import { assertCorsOrigins, normalizeOrigin, parseCorsOrigins } from "./server.js";
|
|
16
|
+
|
|
17
|
+
describe("parseCorsOrigins: 未设 / 设空 / 设值 三态", () => {
|
|
18
|
+
test("未设 ⇒ 开发默认(localhost 两个端口)", () => {
|
|
19
|
+
expect(parseCorsOrigins(undefined)).toEqual(["http://localhost:3000", "http://localhost:3001"]);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("设了但为空 ⇒ 空清单(谁都不许),**不回退默认**", () => {
|
|
23
|
+
expect(parseCorsOrigins("")).toEqual([]);
|
|
24
|
+
// 只写了分隔符 / 空白也是空清单,而不是"默认"
|
|
25
|
+
expect(parseCorsOrigins(",")).toEqual([]);
|
|
26
|
+
expect(parseCorsOrigins(" , ")).toEqual([]);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("设了值 ⇒ 逐条 trim,空条目被丢掉(不许出现空字符串条目)", () => {
|
|
30
|
+
expect(parseCorsOrigins(" https://a.example , https://b.example ")).toEqual(["https://a.example", "https://b.example"]);
|
|
31
|
+
expect(parseCorsOrigins("https://a.example,")).toEqual(["https://a.example"]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// 2026-10-31 —— **语义变更(更严)**:非法条目现在被丢弃,而不是原样保留。
|
|
35
|
+
// 旧实现保留 "*",于是运行期 `includes(origin)` 永远不命中 ——
|
|
36
|
+
// 「保留一条永不匹配的项」与「丢弃它」在行为上相同,但前者会让运维以为已经放行。
|
|
37
|
+
test("非法条目被丢弃('*' / 裸主机名 / 非 http(s))", () => {
|
|
38
|
+
expect(parseCorsOrigins("*")).toEqual([]);
|
|
39
|
+
expect(parseCorsOrigins("a.example")).toEqual([]);
|
|
40
|
+
expect(parseCorsOrigins("ftp://a.example")).toEqual([]);
|
|
41
|
+
expect(parseCorsOrigins(undefined)).not.toContain("*");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// 外部复核 ①:`https://host` 与 `https://host:443` 在字符串上不等、在 origin 上相等。
|
|
45
|
+
test("默认端口与大小写被规范化 —— 字符串比较会在这里判不同", () => {
|
|
46
|
+
expect(normalizeOrigin("https://A.Example:443")).toBe("https://a.example");
|
|
47
|
+
expect(normalizeOrigin("https://a.example")).toBe("https://a.example");
|
|
48
|
+
expect(normalizeOrigin("http://a.example:80")).toBe("http://a.example");
|
|
49
|
+
expect(normalizeOrigin("https://a.example:8443")).toBe("https://a.example:8443");
|
|
50
|
+
expect(normalizeOrigin("https://a.example/")).toBe("https://a.example");
|
|
51
|
+
// 清单写成带默认端口 ⇒ 规范化后与不带端口的请求相等(这正是那批「地址栏正常却全 403」的用户)
|
|
52
|
+
expect(parseCorsOrigins("https://a.example:443")).toEqual(["https://a.example"]);
|
|
53
|
+
// 去重:同一 origin 的两种写法只留一条
|
|
54
|
+
expect(parseCorsOrigins("https://a.example,https://a.example:443/")).toEqual(["https://a.example"]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// 2026-10-31 —— **配错了就别起服务**(「解除武装 ≠ 修好」):
|
|
58
|
+
// 丢弃非法条目只是让服务更安全,但配置仍然是错的,而且没有任何信号。
|
|
59
|
+
// 这里钉的是启动期断言:非法条目必须**抛**,且把非法条目逐个点出来。
|
|
60
|
+
test("startup assertion refuses invalid CORS entries and names them", () => {
|
|
61
|
+
expect(() => assertCorsOrigins(undefined)).not.toThrow(); // 未设 ⇒ 默认,合法
|
|
62
|
+
expect(() => assertCorsOrigins("")).not.toThrow(); // 设空 ⇒ 空清单,合法(更严)
|
|
63
|
+
expect(() => assertCorsOrigins("https://a.example,http://b.example:8080")).not.toThrow();
|
|
64
|
+
// 每个非法形态都要抛,并且错误信息里能看到那个值
|
|
65
|
+
for (const bad of ["*", "a.example", "ftp://a.example", "https://a.example, *"]) {
|
|
66
|
+
let msg = "";
|
|
67
|
+
try { assertCorsOrigins(bad); } catch (e) { msg = String((e as Error).message); }
|
|
68
|
+
expect(msg).toContain("不是 http(s) origin");
|
|
69
|
+
expect(msg.length).toBeGreaterThan(20);
|
|
70
|
+
}
|
|
71
|
+
expect(() => assertCorsOrigins("*")).toThrow(/\*/);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import { afterAll, beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import { db } from "./db.js";
|
|
3
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
4
|
+
import { registerTools } from "./tools.js";
|
|
5
|
+
|
|
6
|
+
// #1353 Fix ① — hub gates create_node dispatch on the daemon's
|
|
7
|
+
// self-reported can_create_nodes bit. Before this, daemon reported
|
|
8
|
+
// can_create_nodes=false (via #1371 + #1377) → hub stored it → nothing
|
|
9
|
+
// read it → dispatch returned {ok:true, request_id} and pushed a
|
|
10
|
+
// doorbell to a daemon that had already told hub it couldn't create
|
|
11
|
+
// nodes. That's the exact "在线但静默失败" case the issue title reports.
|
|
12
|
+
//
|
|
13
|
+
// Vincent 2026-08-28 comment on #1353:
|
|
14
|
+
// "daemon 侧确知不可用时,hub 应当**拒绝派发**并返回可操作的错误,
|
|
15
|
+
// 而不是发一个注定失败的 doorbell"
|
|
16
|
+
//
|
|
17
|
+
// 🔴 Pre-#1371 daemons (preview.55 and earlier) don't report the field
|
|
18
|
+
// at all. That must NOT fail-closed — the guard is "known-blocked",
|
|
19
|
+
// not "unknown-treated-as-blocked". Tested explicitly below.
|
|
20
|
+
|
|
21
|
+
const NET = "net_1353_gate";
|
|
22
|
+
const USER_ID = "u_1353_admin";
|
|
23
|
+
const DAEMON_ID = "node_1353_daemon";
|
|
24
|
+
const DAEMON_ALIAS = "gate-daemon";
|
|
25
|
+
const DAEMON_TOK = "tok_1353_daemon";
|
|
26
|
+
|
|
27
|
+
interface ToolHandler { (args: any, extra?: any): Promise<{ content: Array<{ type: "text"; text: string }> }>; }
|
|
28
|
+
interface Reply { ok?: boolean; error?: string; blocked_reason?: string; request_id?: string; [k: string]: unknown; }
|
|
29
|
+
|
|
30
|
+
function cleanup() {
|
|
31
|
+
try { db.run("DELETE FROM node_create_requests WHERE network_id = ?1", [NET]); } catch {}
|
|
32
|
+
try { db.run("DELETE FROM audit_log WHERE network_id = ?1", [NET]); } catch {}
|
|
33
|
+
try { db.run("DELETE FROM nodes WHERE network_id = ?1", [NET]); } catch {}
|
|
34
|
+
try { db.run("DELETE FROM sessions WHERE network_id = ?1", [NET]); } catch {}
|
|
35
|
+
try { db.run("DELETE FROM api_tokens WHERE network_id = ?1", [NET]); } catch {}
|
|
36
|
+
try { db.run("DELETE FROM network_members WHERE network_id = ?1", [NET]); } catch {}
|
|
37
|
+
try { db.run("DELETE FROM networks WHERE network_id = ?1", [NET]); } catch {}
|
|
38
|
+
try { db.run("DELETE FROM users WHERE user_id = ?1", [USER_ID]); } catch {}
|
|
39
|
+
}
|
|
40
|
+
beforeEach(cleanup);
|
|
41
|
+
afterAll(cleanup);
|
|
42
|
+
|
|
43
|
+
/** #1545 —— 可选:写一条 sessions 心跳行。
|
|
44
|
+
* 默认**不写**,这样既有那 9 条测试的行为逐字不变;
|
|
45
|
+
* 只有要断言绝对年龄的用例才显式给 `heartbeatAgoMs`。 */
|
|
46
|
+
function seedHeartbeat(agoMs: number) {
|
|
47
|
+
const iso = new Date(Date.now() - agoMs).toISOString();
|
|
48
|
+
db.run(
|
|
49
|
+
`INSERT INTO sessions (alias, node_id, network_id, status, last_seen_at, updated_at)
|
|
50
|
+
VALUES (?1, ?2, ?3, 'idle', ?4, ?4)`,
|
|
51
|
+
[DAEMON_ALIAS, DAEMON_ID, NET, iso],
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function seed(snapshot: object | string | null) {
|
|
56
|
+
db.run(
|
|
57
|
+
`INSERT INTO users (user_id, username, password_hash, role, created_at)
|
|
58
|
+
VALUES (?1, ?2, 'x', 'user', datetime('now'))`,
|
|
59
|
+
[USER_ID, USER_ID],
|
|
60
|
+
);
|
|
61
|
+
db.run(
|
|
62
|
+
`INSERT OR REPLACE INTO networks (network_id, network_name, owner_id, created_at)
|
|
63
|
+
VALUES (?1, ?1, ?2, datetime('now'))`,
|
|
64
|
+
[NET, USER_ID],
|
|
65
|
+
);
|
|
66
|
+
db.run(
|
|
67
|
+
`INSERT INTO network_members (user_id, network_id, role, joined_at)
|
|
68
|
+
VALUES (?1, ?2, 'admin', datetime('now'))`,
|
|
69
|
+
[USER_ID, NET],
|
|
70
|
+
);
|
|
71
|
+
const snap = snapshot === null
|
|
72
|
+
? null
|
|
73
|
+
: (typeof snapshot === "string" ? snapshot : JSON.stringify(snapshot));
|
|
74
|
+
db.run(
|
|
75
|
+
`INSERT INTO nodes (node_id, node_name, alias, network_id, config_snapshot, hostname, created_at, updated_at, lifecycle_state)
|
|
76
|
+
VALUES (?1, ?2, ?3, ?4, ?5, ?6, datetime('now'), datetime('now'), 'active')`,
|
|
77
|
+
[DAEMON_ID, DAEMON_ALIAS, DAEMON_ALIAS, NET, snap, `host-${DAEMON_ALIAS}`],
|
|
78
|
+
);
|
|
79
|
+
db.run(
|
|
80
|
+
`INSERT INTO api_tokens (token_id, user_id, network_id, scope, name, token_hash, expires_at, revoked_at)
|
|
81
|
+
VALUES (?1, ?2, ?3, 'network', ?4, ?5, NULL, NULL)`,
|
|
82
|
+
[DAEMON_TOK, USER_ID, NET, `node:${DAEMON_ALIAS}`, `hash_${DAEMON_TOK}`],
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function tools(): Record<string, ToolHandler> {
|
|
87
|
+
const server = new McpServer({ name: "test", version: "0" }) as any;
|
|
88
|
+
const out: Record<string, ToolHandler> = {};
|
|
89
|
+
const origTool = server.tool.bind(server);
|
|
90
|
+
server.tool = (name: string, _desc: string, _schema: any, handler: ToolHandler) => {
|
|
91
|
+
out[name] = handler;
|
|
92
|
+
return origTool(name, _desc, _schema, handler);
|
|
93
|
+
};
|
|
94
|
+
const origRegisterTool = server.registerTool?.bind(server);
|
|
95
|
+
if (origRegisterTool) {
|
|
96
|
+
server.registerTool = (name: string, _cfg: any, handler: ToolHandler) => {
|
|
97
|
+
out[name] = handler;
|
|
98
|
+
return origRegisterTool(name, _cfg, handler);
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
registerTools(
|
|
102
|
+
server, undefined,
|
|
103
|
+
/* enforceNetworkId */ NET,
|
|
104
|
+
/* enforceUserId */ USER_ID,
|
|
105
|
+
/* callerAlias */ null,
|
|
106
|
+
/* callerTokenIsNetwork */ true,
|
|
107
|
+
/* callerTokenId */ null,
|
|
108
|
+
);
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async function call(handler: ToolHandler, args: any): Promise<Reply> {
|
|
113
|
+
const r = await handler(args);
|
|
114
|
+
return JSON.parse(r.content[0].text) as Reply;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const spec = {
|
|
118
|
+
name: "child-1353",
|
|
119
|
+
runtime: "claude-agent-sdk",
|
|
120
|
+
model: "claude-sonnet-4-5",
|
|
121
|
+
flags: {},
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
describe("#1353 Fix ① — hub gates create_node on daemon-reported can_create_nodes", () => {
|
|
125
|
+
test("🔴 witnessed-red: blocked daemon (can_create_nodes=false + reason) → refuse with reason, NO dispatch", async () => {
|
|
126
|
+
seed({
|
|
127
|
+
role: "host_supervisor",
|
|
128
|
+
daemon_capabilities: {
|
|
129
|
+
runtimes_supported: ["claude-agent-sdk"],
|
|
130
|
+
can_create_nodes: false,
|
|
131
|
+
create_nodes_blocked_reason: "anet_bin_permission",
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
const t = tools();
|
|
135
|
+
const r = await call(t.create_node, { daemon_node_id: DAEMON_ID, node_spec: spec });
|
|
136
|
+
expect(r.ok).toBe(false);
|
|
137
|
+
expect(r.error).toBe("daemon_cannot_create_nodes");
|
|
138
|
+
expect(r.blocked_reason).toBe("anet_bin_permission");
|
|
139
|
+
// No request row inserted — hub must refuse BEFORE dispatch.
|
|
140
|
+
const rows = db.get<{ n: number }>(
|
|
141
|
+
"SELECT COUNT(*) AS n FROM node_create_requests WHERE daemon_node_id = ?1",
|
|
142
|
+
DAEMON_ID,
|
|
143
|
+
);
|
|
144
|
+
expect(rows?.n).toBe(0);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test("blocked daemon with each of the 4 reason codes surfaces that specific code", async () => {
|
|
148
|
+
const codes = ["anet_bin_identity", "anet_bin_source", "anet_bin_shape", "anet_bin_permission"];
|
|
149
|
+
for (const code of codes) {
|
|
150
|
+
cleanup();
|
|
151
|
+
seed({
|
|
152
|
+
role: "host_supervisor",
|
|
153
|
+
daemon_capabilities: {
|
|
154
|
+
runtimes_supported: ["claude-agent-sdk"],
|
|
155
|
+
can_create_nodes: false,
|
|
156
|
+
create_nodes_blocked_reason: code,
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: spec });
|
|
160
|
+
expect(r.error).toBe("daemon_cannot_create_nodes");
|
|
161
|
+
expect(r.blocked_reason).toBe(code);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("blocked with .40-shape 'anet_bin_pin_unresolved' reason surfaces that literal (backcompat)", async () => {
|
|
166
|
+
// The Zod enum in tools.ts still accepts this coarse pre-#1377 code
|
|
167
|
+
// because agent-node@2.5.0-preview.40 daemons in the wild still emit
|
|
168
|
+
// it. If we ever swallow it into "unknown", those daemons' user
|
|
169
|
+
// messages get worse than they already are.
|
|
170
|
+
seed({
|
|
171
|
+
role: "host_supervisor",
|
|
172
|
+
daemon_capabilities: {
|
|
173
|
+
runtimes_supported: ["claude-agent-sdk"],
|
|
174
|
+
can_create_nodes: false,
|
|
175
|
+
create_nodes_blocked_reason: "anet_bin_pin_unresolved",
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: spec });
|
|
179
|
+
expect(r.blocked_reason).toBe("anet_bin_pin_unresolved");
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("blocked with NO reason field → fallback 'anet_bin_unknown' (defensive)", async () => {
|
|
183
|
+
seed({
|
|
184
|
+
role: "host_supervisor",
|
|
185
|
+
daemon_capabilities: {
|
|
186
|
+
runtimes_supported: ["claude-agent-sdk"],
|
|
187
|
+
can_create_nodes: false,
|
|
188
|
+
// no create_nodes_blocked_reason
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: spec });
|
|
192
|
+
expect(r.error).toBe("daemon_cannot_create_nodes");
|
|
193
|
+
expect(r.blocked_reason).toBe("anet_bin_unknown");
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test("can_create_nodes=true daemon dispatches normally (gate must not false-positive on healthy daemon)", async () => {
|
|
197
|
+
seed({
|
|
198
|
+
role: "host_supervisor",
|
|
199
|
+
daemon_capabilities: {
|
|
200
|
+
runtimes_supported: ["claude-agent-sdk"],
|
|
201
|
+
can_create_nodes: true,
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: spec });
|
|
205
|
+
// Doesn't matter whether downstream dispatch succeeds — the point
|
|
206
|
+
// is that THIS gate did not reject. If reject, r.error would be
|
|
207
|
+
// "daemon_cannot_create_nodes".
|
|
208
|
+
expect(r.error).not.toBe("daemon_cannot_create_nodes");
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
test("🔴 pre-#1371 compat: snapshot with NO can_create_nodes field passes through (must NOT fail-closed)", async () => {
|
|
212
|
+
// preview.10 through preview.55 daemons never report this field.
|
|
213
|
+
// If we fail-closed on undefined we silently break every in-flight
|
|
214
|
+
// daemon that hasn't upgraded. Guard is "known-blocked", not
|
|
215
|
+
// "unknown-treated-as-blocked".
|
|
216
|
+
seed({
|
|
217
|
+
role: "host_supervisor",
|
|
218
|
+
daemon_capabilities: {
|
|
219
|
+
runtimes_supported: ["claude-agent-sdk"],
|
|
220
|
+
// no can_create_nodes at all
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: spec });
|
|
224
|
+
expect(r.error).not.toBe("daemon_cannot_create_nodes");
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
test("pre-#1371 compat: snapshot with no daemon_capabilities at all → passes through", async () => {
|
|
228
|
+
seed({ role: "host_supervisor" });
|
|
229
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: spec });
|
|
230
|
+
expect(r.error).not.toBe("daemon_cannot_create_nodes");
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test("pre-#1371 compat: NULL config_snapshot passes through (very old daemon shape)", async () => {
|
|
234
|
+
seed(null);
|
|
235
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: spec });
|
|
236
|
+
expect(r.error).not.toBe("daemon_cannot_create_nodes");
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
test("malformed config_snapshot: permissive fallthrough (mirrors runtimes_supported catch), no crash", async () => {
|
|
240
|
+
seed("{not json");
|
|
241
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: spec });
|
|
242
|
+
expect(r.error).not.toBe("daemon_cannot_create_nodes");
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
/* #1545 —— 拒绝的时候还要说**那个判断是什么时候做出来的**。
|
|
247
|
+
*
|
|
248
|
+
* 为什么这一格落在拒绝路径上最要紧:用户被挡住的这一刻,正好在决定"去修哪台机器"。
|
|
249
|
+
* 刚测的 blocked ⇒ 去那台机器按 reason 修
|
|
250
|
+
* 三周前测的 blocked ⇒ 那台 daemon 是**开机算一次就永久缓存**的旧版本
|
|
251
|
+
* (agent-node ≤ 2.5.0-preview.54),它可能早就好了 —— 先重启/升级它
|
|
252
|
+
* 只给 `blocked_reason` 的话,这两种情况给用户的字**一模一样**。 */
|
|
253
|
+
describe("#1545 拒绝文案带上「这个判断是多久以前做的」", () => {
|
|
254
|
+
const blockedSnap = (extra: object) => ({
|
|
255
|
+
role: "host_supervisor",
|
|
256
|
+
daemon_capabilities: {
|
|
257
|
+
can_create_nodes: false,
|
|
258
|
+
create_nodes_blocked_reason: "anet_bin_permission",
|
|
259
|
+
...extra,
|
|
260
|
+
},
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
/* 🔴 报出去的是**到现在为止**的绝对年龄,不是 daemon 报的那个原始时长。
|
|
264
|
+
* daemon 报的是「测完到**这份 report 发出**」;直接透传会漏掉心跳那一段,
|
|
265
|
+
* **朝更新鲜的方向低报** —— 正是这一格要防的方向。
|
|
266
|
+
* 绝对年龄 = (now − last_seen_at) + daemon 报的时长 */
|
|
267
|
+
test("daemon 报了年龄 + 有心跳时间 → 补上心跳那一段,标记 known", async () => {
|
|
268
|
+
seed(blockedSnap({ create_capability_observed_ms_ago: 12_345 }));
|
|
269
|
+
seedHeartbeat(60_000); // 上次心跳在 60s 前
|
|
270
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: spec });
|
|
271
|
+
expect(r.ok).toBe(false);
|
|
272
|
+
expect(r.blocked_reason).toBe("anet_bin_permission");
|
|
273
|
+
expect(r.capability_age).toBe("known");
|
|
274
|
+
// 允许 ±5s 的执行抖动;关键是它**明显大于** 12,345,即心跳那一段确实被加上了
|
|
275
|
+
const got = r.capability_observed_ms_ago as number;
|
|
276
|
+
expect(got).toBeGreaterThanOrEqual(12_345 + 60_000 - 5_000);
|
|
277
|
+
expect(got).toBeLessThanOrEqual(12_345 + 60_000 + 5_000);
|
|
278
|
+
// 🔴 反向锚:如果实现是"原样透传",这个数会恰好等于 12,345
|
|
279
|
+
expect(got).not.toBe(12_345);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
/* 🔴 第三种 unknown,不能和「旧 daemon 没报」塌成同一个:
|
|
283
|
+
* 它报了,但 hub 这边没有可用的心跳时间 ⇒ **补不出绝对年龄**,而不是"年龄是 0"。 */
|
|
284
|
+
test("报了年龄但 hub 没有心跳时间 → unknown_no_heartbeat_time(不是 legacy,也不是 0)", async () => {
|
|
285
|
+
seed(blockedSnap({ create_capability_observed_ms_ago: 12_345 }));
|
|
286
|
+
// 故意不 seedHeartbeat
|
|
287
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: spec });
|
|
288
|
+
expect(r.capability_age).toBe("unknown_no_heartbeat_time");
|
|
289
|
+
expect(r.capability_observed_ms_ago).toBeNull();
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
/* 🔴 本组的重点:年龄未知时给的是**显式 null + 说明为什么的枚举**,
|
|
293
|
+
* 而不是省略这两个键。省略读起来像"没有这个问题",而调用方(常常是个 agent)
|
|
294
|
+
* 会默认它是刚测的 —— 那是朝「更可信」方向撒谎,比不给更糟。 */
|
|
295
|
+
test("🔴 daemon 没报年龄 → 两个键都在:null + unknown_legacy_daemon(不是省略)", async () => {
|
|
296
|
+
seed(blockedSnap({}));
|
|
297
|
+
seedHeartbeat(1_000); // 心跳齐全 ⇒ 唯一缺的就是 daemon 没报那一格
|
|
298
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: spec });
|
|
299
|
+
expect(r.ok).toBe(false);
|
|
300
|
+
expect("capability_observed_ms_ago" in r).toBe(true);
|
|
301
|
+
expect(r.capability_observed_ms_ago).toBeNull();
|
|
302
|
+
expect(r.capability_age).toBe("unknown_legacy_daemon");
|
|
303
|
+
// 🔴 绝不能渲染成"刚测的"
|
|
304
|
+
expect(r.capability_observed_ms_ago).not.toBe(0);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
test.each([
|
|
308
|
+
["负数", -1],
|
|
309
|
+
["超过一年", 400 * 24 * 60 * 60 * 1000],
|
|
310
|
+
["非数字", "12"],
|
|
311
|
+
["null", null],
|
|
312
|
+
])("坏值当作没报(不 clamp 成一个看起来正常的年龄):%s", async (_l, v) => {
|
|
313
|
+
seed(blockedSnap({ create_capability_observed_ms_ago: v }));
|
|
314
|
+
// 🔴 必须给心跳:否则这几条会因为"没有心跳时间"而变成
|
|
315
|
+
// unknown_no_heartbeat_time,**坏值那一维根本没被测到**
|
|
316
|
+
// (夹具只该在被测的那一维上不合法)。
|
|
317
|
+
seedHeartbeat(1_000);
|
|
318
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: spec });
|
|
319
|
+
expect(r.capability_age).toBe("unknown_legacy_daemon");
|
|
320
|
+
expect(r.capability_observed_ms_ago).toBeNull();
|
|
321
|
+
// 分母自证:拒绝本身仍然发生了,不是因为整条路径没走到才"没有年龄"
|
|
322
|
+
expect(r.error).toBe("daemon_cannot_create_nodes");
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
test("边界:恰好一年被接受 ⇒ 上面那些不是因为恒判 unknown 才绿", async () => {
|
|
326
|
+
const oneYear = 365 * 24 * 60 * 60 * 1000;
|
|
327
|
+
seed(blockedSnap({ create_capability_observed_ms_ago: oneYear }));
|
|
328
|
+
seedHeartbeat(1_000);
|
|
329
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: spec });
|
|
330
|
+
expect(r.capability_age).toBe("known");
|
|
331
|
+
expect(r.capability_observed_ms_ago as number).toBeGreaterThanOrEqual(oneYear);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
/* 🔴 hub 出**代码**,不出渲染好的修法命令 —— 那张 code → 命令 的表只有一个作者
|
|
335
|
+
* (@sleep2agi/agent-network 的 daemon-capability-display)。抄一份到 hub,
|
|
336
|
+
* 两份就会各自漂移,而「hub 教的修法」和「CLI 教的修法」不一致时没有任何东西会红。 */
|
|
337
|
+
test("🔴 hub 不返回具体修法命令,只把人指到那唯一一份", async () => {
|
|
338
|
+
seed(blockedSnap({ create_capability_observed_ms_ago: 0 }));
|
|
339
|
+
seedHeartbeat(1_000);
|
|
340
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: spec });
|
|
341
|
+
const blob = JSON.stringify(r);
|
|
342
|
+
for (const leaked of ["chmod go-w", "npm i -g", "readlink -f", "/etc/anet-daemon"]) {
|
|
343
|
+
expect(blob).not.toContain(leaked);
|
|
344
|
+
}
|
|
345
|
+
expect(String(r.remediation_hint)).toContain("anet daemon list");
|
|
346
|
+
// 正控:这四条不是恒真 —— 它们确实是那份表里的真实内容
|
|
347
|
+
expect("该二进制 group/other 可写。一行修:chmod go-w \"$(command -v anet)\"").toContain("chmod go-w");
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
test("健康 daemon 不受影响:正常派发,不带这两个键", async () => {
|
|
351
|
+
seed({ role: "host_supervisor", daemon_capabilities: { can_create_nodes: true } });
|
|
352
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: spec });
|
|
353
|
+
expect(r.ok).not.toBe(false);
|
|
354
|
+
expect("capability_age" in r).toBe(false);
|
|
355
|
+
});
|
|
356
|
+
});
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { afterAll, beforeAll, beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import "./require-explicit-test-db.js";
|
|
3
|
+
import { db } from "./db.js";
|
|
4
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
5
|
+
import { registerTools } from "./tools.js";
|
|
6
|
+
|
|
7
|
+
// #1027: get_create_request must return network_id so the daemon can write
|
|
8
|
+
// it into the child config. Without it, child nodes fail the OpenCode
|
|
9
|
+
// runtime binding check and die within 5s.
|
|
10
|
+
|
|
11
|
+
const NET = "net_1027_hub";
|
|
12
|
+
const USER_ID = "u_1027_hub";
|
|
13
|
+
const DAEMON_ID = "node_1027_hub_daemon";
|
|
14
|
+
const DAEMON_ALIAS = "hub1027-daemon";
|
|
15
|
+
const DAEMON_TOK = "tok_1027_hub_daemon";
|
|
16
|
+
|
|
17
|
+
interface ToolHandler { (args: any, extra?: any): Promise<{ content: Array<{ type: "text"; text: string }> }>; }
|
|
18
|
+
interface Reply { ok?: boolean; error?: string; request_id?: string; network_id?: string; node_spec?: any; [k: string]: unknown; }
|
|
19
|
+
|
|
20
|
+
function cleanup() {
|
|
21
|
+
for (const t of ["node_create_requests", "audit_log", "nodes", "sessions", "api_tokens", "network_members"]) {
|
|
22
|
+
try { db.run(`DELETE FROM ${t} WHERE network_id = ?1`, [NET]); } catch {}
|
|
23
|
+
}
|
|
24
|
+
try { db.run("DELETE FROM networks WHERE network_id = ?1", [NET]); } catch {}
|
|
25
|
+
try { db.run("DELETE FROM users WHERE user_id = ?1", [USER_ID]); } catch {}
|
|
26
|
+
}
|
|
27
|
+
beforeEach(cleanup);
|
|
28
|
+
afterAll(cleanup);
|
|
29
|
+
|
|
30
|
+
function seed() {
|
|
31
|
+
db.run(`INSERT INTO users (user_id, username, password_hash, role, created_at) VALUES (?1, ?1, 'x', 'user', datetime('now'))`, [USER_ID]);
|
|
32
|
+
db.run(`INSERT OR REPLACE INTO networks (network_id, network_name, owner_id, created_at) VALUES (?1, ?1, ?2, datetime('now'))`, [NET, USER_ID]);
|
|
33
|
+
db.run(`INSERT INTO network_members (user_id, network_id, role, joined_at) VALUES (?1, ?2, 'admin', datetime('now'))`, [USER_ID, NET]);
|
|
34
|
+
db.run(
|
|
35
|
+
`INSERT INTO nodes (node_id, node_name, alias, network_id, config_snapshot, hostname, created_at, updated_at, lifecycle_state)
|
|
36
|
+
VALUES (?1, ?2, ?2, ?3, ?4, 'host-x', datetime('now'), datetime('now'), 'active')`,
|
|
37
|
+
[DAEMON_ID, DAEMON_ALIAS, NET, JSON.stringify({ role: "host_supervisor", daemon_capabilities: { runtimes_supported: ["opencode-cli"] } })],
|
|
38
|
+
);
|
|
39
|
+
db.run(
|
|
40
|
+
`INSERT INTO api_tokens (token_id, user_id, network_id, scope, name, token_hash, expires_at, revoked_at)
|
|
41
|
+
VALUES (?1, ?2, ?3, 'network', ?4, ?5, NULL, NULL)`,
|
|
42
|
+
[DAEMON_TOK, USER_ID, NET, `node:${DAEMON_ALIAS}`, `hash_${DAEMON_TOK}`],
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function tools(callerTokenId: string | null = null): Record<string, ToolHandler> {
|
|
47
|
+
const server = new McpServer({ name: "test", version: "0" }) as any;
|
|
48
|
+
const out: Record<string, ToolHandler> = {};
|
|
49
|
+
const origTool = server.tool.bind(server);
|
|
50
|
+
server.tool = (name: string, d: string, schema: any, handler: ToolHandler) => { out[name] = handler; return origTool(name, d, schema, handler); };
|
|
51
|
+
const origRegisterTool = server.registerTool?.bind(server);
|
|
52
|
+
if (origRegisterTool) {
|
|
53
|
+
server.registerTool = (name: string, cfg: any, handler: ToolHandler) => { out[name] = handler; return origRegisterTool(name, cfg, handler); };
|
|
54
|
+
}
|
|
55
|
+
registerTools(server, undefined, NET, USER_ID, null, true, callerTokenId);
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function call(h: ToolHandler, args: any): Promise<Reply> {
|
|
60
|
+
return JSON.parse((await h(args)).content[0].text) as Reply;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
describe("#1027 get_create_request returns network_id", () => {
|
|
64
|
+
test("network_id is included in the response", async () => {
|
|
65
|
+
seed();
|
|
66
|
+
const t = tools(DAEMON_TOK);
|
|
67
|
+
const r = await call(t.create_node, {
|
|
68
|
+
daemon_node_id: DAEMON_ID,
|
|
69
|
+
node_spec: { name: "child-1027", runtime: "opencode-cli", model: null, flags: {} },
|
|
70
|
+
});
|
|
71
|
+
expect(r.ok).toBe(true);
|
|
72
|
+
expect(r.request_id).toMatch(/^cr_/);
|
|
73
|
+
|
|
74
|
+
const g = await call(t.get_create_request, { request_id: r.request_id });
|
|
75
|
+
expect(g.ok).toBe(true);
|
|
76
|
+
expect(g.network_id).toBe(NET);
|
|
77
|
+
expect(g.node_spec?.name).toBe("child-1027");
|
|
78
|
+
// 2026-10-31 —— **精确键集**(#3171):上面全是字段级断言,
|
|
79
|
+
// 「多给一个字段」不会被发现,而这条响应会流到 daemon 侧按字段消费。
|
|
80
|
+
// ⚠️ 我先后猜了两次键集(第一次少了 child_token/env_blob,第二次多了 op/status),
|
|
81
|
+
// 两次都是断言把真实形状打出来的。这一条不写「我认为的形状」,写**它实际给的**。
|
|
82
|
+
expect(Object.keys(g).sort()).toEqual([
|
|
83
|
+
"child_token", "env_blob", "network_id", "node_spec", "ok", "request_id",
|
|
84
|
+
].sort());
|
|
85
|
+
// 未知 id ⇒ 确定性两键**错误**形状(第三次被断言纠正:这里不是 {ok,request:null},
|
|
86
|
+
// 而是 {ok,error};两种都是「两键」,值不同 —— 正是精确键集要区分的那一格)。
|
|
87
|
+
const missing = await call(t.get_create_request, { request_id: "cr_does_not_exist" });
|
|
88
|
+
expect(Object.keys(missing).sort()).toEqual(["error", "ok"]);
|
|
89
|
+
expect(missing.ok).toBe(false);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
|
+
import { registerTools } from "./tools.js";
|
|
4
|
+
|
|
5
|
+
type ToolHandler = (args: any) => Promise<any> | any;
|
|
6
|
+
|
|
7
|
+
function captureSchemas(): Record<string, Record<string, any>> {
|
|
8
|
+
const server = new McpServer({ name: "test", version: "0" }) as any;
|
|
9
|
+
const schemas: Record<string, Record<string, any>> = {};
|
|
10
|
+
const origTool = server.tool.bind(server);
|
|
11
|
+
server.tool = (name: string, desc: string, schema: any, handler: ToolHandler) => {
|
|
12
|
+
schemas[name] = schema;
|
|
13
|
+
return origTool(name, desc, schema, handler);
|
|
14
|
+
};
|
|
15
|
+
registerTools(server, undefined, null, "u_schema_test", "schema-test", false, null);
|
|
16
|
+
return schemas;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe("create_node MCP schema", () => {
|
|
20
|
+
test("#829 rejects misplaced V2 controls instead of stripping them", () => {
|
|
21
|
+
const nodeSpec = captureSchemas().create_node.node_spec;
|
|
22
|
+
const base = { name: "v2-node", runtime: "opencode-cli" };
|
|
23
|
+
expect(nodeSpec.safeParse({ ...base, opencodeGeneration: "v2" }).success).toBe(false);
|
|
24
|
+
expect(nodeSpec.safeParse({ ...base, opencodeUnsafeTools: true }).success).toBe(false);
|
|
25
|
+
expect(nodeSpec.safeParse({ ...base, opencodeNewServer: true }).success).toBe(false);
|
|
26
|
+
const flags = { opencodeGeneration: "v2", opencodeUnsafeTools: true };
|
|
27
|
+
expect(nodeSpec.parse({ ...base, flags }).flags).toEqual(flags);
|
|
28
|
+
expect(nodeSpec.parse(base)).toEqual(base);
|
|
29
|
+
});
|
|
30
|
+
test("model is optional on daemon create specs but empty strings stay invalid", () => {
|
|
31
|
+
const schemas = captureSchemas();
|
|
32
|
+
const nodeSpec = schemas.create_node.node_spec;
|
|
33
|
+
|
|
34
|
+
expect(nodeSpec.safeParse({
|
|
35
|
+
name: "child",
|
|
36
|
+
runtime: "claude-agent-sdk",
|
|
37
|
+
}).success).toBe(true);
|
|
38
|
+
expect(nodeSpec.safeParse({
|
|
39
|
+
name: "child",
|
|
40
|
+
runtime: "claude-agent-sdk",
|
|
41
|
+
model: null,
|
|
42
|
+
}).success).toBe(true);
|
|
43
|
+
expect(nodeSpec.safeParse({
|
|
44
|
+
name: "child",
|
|
45
|
+
runtime: "claude-agent-sdk",
|
|
46
|
+
model: "",
|
|
47
|
+
}).success).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
});
|