@sleep2agi/commhub-server 0.9.0-preview.2 → 0.9.0-preview.200
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 +130 -10
- package/bin/commhub.ts +99 -11
- package/package.json +7 -7
- package/src/ack-create-request-transport.test.ts +182 -0
- package/src/ack-create-request.test.ts +374 -0
- 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 +267 -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 +271 -0
- package/src/api-host-supervisors-runtime-readiness.test.ts +195 -0
- package/src/api-nodes-shape.test.ts +279 -0
- 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 +611 -51
- package/src/avatar-validate.test.ts +163 -0
- package/src/avatar-validate.ts +103 -0
- 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/boot-side-effect.test.ts +124 -0
- package/src/broadcast-fanout-2721.test.ts +118 -0
- package/src/broadcast-fanout.ts +98 -0
- package/src/chain-attempt-dedup.test.ts +447 -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-sec1.test.ts +107 -0
- package/src/config-apply-validate.test.ts +134 -1
- package/src/config-apply-validate.ts +111 -5
- package/src/create-node-blocked-gate.test.ts +356 -0
- package/src/create-node-network-id.test.ts +79 -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 +379 -0
- package/src/create-node-validate.ts +423 -0
- package/src/create-node-workdir.test.ts +289 -0
- package/src/create-node.test.ts +354 -0
- package/src/create-node.ts +411 -0
- package/src/daemon-provider-snapshots.test.ts +421 -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 +1501 -152
- 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 +330 -0
- package/src/department-heads.ts +226 -0
- package/src/departments-agent-count.test.ts +129 -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 +177 -0
- package/src/env-transport-gate.ts +74 -0
- package/src/env-transport-policy.ts +21 -0
- package/src/event-loop-lag.test.ts +139 -0
- package/src/event-loop-lag.ts +150 -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 +135 -0
- package/src/file-download-authz.test.ts +404 -0
- 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-host-disk-2756.test.ts +49 -0
- package/src/health-redaction.test.ts +685 -0
- 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 +187 -0
- package/src/hub-provider-config.test.ts +139 -0
- package/src/hub-provider-config.ts +105 -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 +15 -2172
- package/src/lifecycle-guard.ts +50 -0
- package/src/list-host-supervisors.test.ts +315 -0
- 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 +162 -0
- package/src/mcp-route-label-http.test.ts +120 -0
- package/src/mcp-write-network-resolution.test.ts +432 -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-validate.ts +88 -0
- package/src/node-attrs.test.ts +258 -0
- package/src/node-caller-identity-http.test.ts +1083 -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 +279 -0
- package/src/node-health-guard.ts +105 -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 +79 -0
- package/src/node-permissions-http.test.ts +345 -0
- package/src/node-permissions.ts +393 -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 +209 -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 +442 -0
- package/src/observer-push.test.ts +252 -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 +352 -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 +455 -0
- package/src/peer-reply-capability.test.ts +70 -0
- package/src/pg-worker.ts +87 -0
- package/src/probe-validate.test.ts +221 -0
- package/src/probe-validate.ts +104 -0
- package/src/probe.test.ts +80 -0
- package/src/probe.ts +200 -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 +466 -13
- package/src/redact-tokens.test.ts +65 -0
- package/src/redact-tokens.ts +40 -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 +268 -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 +425 -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-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-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 +2027 -0
- package/src/resolve-port.test.ts +57 -0
- package/src/resolve-port.ts +44 -0
- package/src/response-charset.test.ts +130 -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 +211 -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 +391 -0
- package/src/schedule-agent.ts +192 -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 +377 -0
- package/src/scheduled-noprogress-backoff-2622.test.ts +133 -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 +312 -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 +139 -0
- package/src/send-dedup-request-id.test.ts +229 -0
- package/src/send-reply-agent-warning.test.ts +202 -0
- 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 +38 -0
- package/src/serve-error.ts +27 -0
- package/src/server.ts +5997 -0
- 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 +54 -0
- package/src/shared/probe-host-allowlist.ts +128 -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 +53 -0
- package/src/shared/reserved-env.test.ts +35 -0
- package/src/shared/reserved-env.ts +31 -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 +108 -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 +122 -0
- package/src/stop-delete-node.test.ts +1286 -0
- 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 +482 -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 +152 -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 +49 -0
- package/src/task-sender-http.test.ts +143 -0
- package/src/task-stale-open-http.test.ts +290 -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/tool-audience-http.test.ts +230 -0
- package/src/tool-audience.ts +120 -0
- package/src/tools-list-json-schema.test.ts +94 -0
- package/src/tools.ts +5815 -348
- package/src/update-provider.test.ts +234 -0
- package/src/upload-early-reject-guard.test.ts +155 -0
- package/src/uploads-http.test.ts +97 -15
- package/src/uploads.test.ts +66 -0
- package/src/uploads.ts +138 -5
- 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/vault.test.ts +200 -0
- package/src/vault.ts +156 -0
- package/src/version-stats.test.ts +66 -0
- package/src/version-stats.ts +109 -0
|
@@ -0,0 +1,289 @@
|
|
|
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
|
+
import { daemonDefaultWorkdirRoot, validateWorkdir, ValidationError } from "./create-node-validate.js";
|
|
7
|
+
import { addNetworkMember, login, register } from "./auth.js";
|
|
8
|
+
|
|
9
|
+
// app「新建节点」工作目录 —— hub 这一层管三件事:
|
|
10
|
+
// ① node_spec.workdir 的**形状**(绝对 / ~/… / C:\…),派发前就拒
|
|
11
|
+
// ② daemon 没自报 default_workdir_root(= 老 daemon,会静默忽略 workdir)⇒ 拒,不派
|
|
12
|
+
// ③ 请求行存下 workdir,get_create_request 原样交给 daemon;不带就**不出现**这个键
|
|
13
|
+
// 以及 /api/host-supervisors 只给 admin/owner 透出默认根(它是那台机器的家目录路径)。
|
|
14
|
+
|
|
15
|
+
const NET = "net_workdir_gate";
|
|
16
|
+
const USER_ID = "u_workdir_admin";
|
|
17
|
+
const DAEMON_ID = "node_workdir_daemon";
|
|
18
|
+
const DAEMON_ALIAS = "workdir-daemon";
|
|
19
|
+
const DAEMON_TOK = "tok_workdir_daemon";
|
|
20
|
+
const ROOT = "/home/user";
|
|
21
|
+
|
|
22
|
+
interface ToolHandler { (args: any, extra?: any): Promise<{ content: Array<{ type: "text"; text: string }> }>; }
|
|
23
|
+
interface Reply { ok?: boolean; error?: string; request_id?: string; node_spec?: any; [k: string]: unknown; }
|
|
24
|
+
|
|
25
|
+
function cleanup() {
|
|
26
|
+
for (const t of ["node_create_requests", "audit_log", "nodes", "sessions", "api_tokens", "network_members"]) {
|
|
27
|
+
try { db.run(`DELETE FROM ${t} WHERE network_id = ?1`, [NET]); } catch {}
|
|
28
|
+
}
|
|
29
|
+
try { db.run("DELETE FROM networks WHERE network_id = ?1", [NET]); } catch {}
|
|
30
|
+
try { db.run("DELETE FROM users WHERE user_id = ?1", [USER_ID]); } catch {}
|
|
31
|
+
}
|
|
32
|
+
beforeEach(cleanup);
|
|
33
|
+
afterAll(cleanup);
|
|
34
|
+
|
|
35
|
+
function seed(caps: Record<string, unknown>) {
|
|
36
|
+
db.run(`INSERT INTO users (user_id, username, password_hash, role, created_at) VALUES (?1, ?1, 'x', 'user', datetime('now'))`, [USER_ID]);
|
|
37
|
+
db.run(`INSERT OR REPLACE INTO networks (network_id, network_name, owner_id, created_at) VALUES (?1, ?1, ?2, datetime('now'))`, [NET, USER_ID]);
|
|
38
|
+
db.run(`INSERT INTO network_members (user_id, network_id, role, joined_at) VALUES (?1, ?2, 'admin', datetime('now'))`, [USER_ID, NET]);
|
|
39
|
+
db.run(
|
|
40
|
+
`INSERT INTO nodes (node_id, node_name, alias, network_id, config_snapshot, hostname, created_at, updated_at, lifecycle_state)
|
|
41
|
+
VALUES (?1, ?2, ?2, ?3, ?4, 'host-x', datetime('now'), datetime('now'), 'active')`,
|
|
42
|
+
[DAEMON_ID, DAEMON_ALIAS, NET, JSON.stringify({ role: "host_supervisor", daemon_capabilities: { runtimes_supported: ["claude-agent-sdk"], ...caps } })],
|
|
43
|
+
);
|
|
44
|
+
db.run(
|
|
45
|
+
`INSERT INTO api_tokens (token_id, user_id, network_id, scope, name, token_hash, expires_at, revoked_at)
|
|
46
|
+
VALUES (?1, ?2, ?3, 'network', ?4, ?5, NULL, NULL)`,
|
|
47
|
+
[DAEMON_TOK, USER_ID, NET, `node:${DAEMON_ALIAS}`, `hash_${DAEMON_TOK}`],
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function tools(callerTokenId: string | null = null): Record<string, ToolHandler> {
|
|
52
|
+
const server = new McpServer({ name: "test", version: "0" }) as any;
|
|
53
|
+
const out: Record<string, ToolHandler> = {};
|
|
54
|
+
const origTool = server.tool.bind(server);
|
|
55
|
+
server.tool = (name: string, d: string, schema: any, handler: ToolHandler) => { out[name] = handler; return origTool(name, d, schema, handler); };
|
|
56
|
+
const origRegisterTool = server.registerTool?.bind(server);
|
|
57
|
+
if (origRegisterTool) {
|
|
58
|
+
server.registerTool = (name: string, cfg: any, handler: ToolHandler) => { out[name] = handler; return origRegisterTool(name, cfg, handler); };
|
|
59
|
+
}
|
|
60
|
+
registerTools(server, undefined, NET, USER_ID, null, true, callerTokenId);
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function call(h: ToolHandler, args: any): Promise<Reply> {
|
|
65
|
+
return JSON.parse((await h(args)).content[0].text) as Reply;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const baseSpec = { name: "wd-child", runtime: "claude-agent-sdk", model: "claude-sonnet-4-5", flags: {} };
|
|
69
|
+
|
|
70
|
+
function rowCount(): number {
|
|
71
|
+
return db.get<{ n: number }>("SELECT COUNT(*) AS n FROM node_create_requests WHERE daemon_node_id = ?1", DAEMON_ID)?.n ?? -1;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
describe("validateWorkdir — shape only", () => {
|
|
75
|
+
const ok = ["/home/user/proj", "/srv/x", "~", "~/proj", "C:\\Users\\alice\\proj", "D:/work", " /padded "];
|
|
76
|
+
for (const v of ok) test(`accepts ${JSON.stringify(v)}`, () => { expect(() => validateWorkdir(v)).not.toThrow(); });
|
|
77
|
+
test("accepts undefined / null (field omitted)", () => {
|
|
78
|
+
expect(() => validateWorkdir(undefined)).not.toThrow();
|
|
79
|
+
expect(() => validateWorkdir(null)).not.toThrow();
|
|
80
|
+
});
|
|
81
|
+
const bad: unknown[] = ["", " ", "proj", "./proj", "../x", "~alice/x", "C:proj", "/a\nb", "/a\u0000b", "x".repeat(10) + "/", "/" + "a".repeat(1024), 42, {}];
|
|
82
|
+
for (const v of bad) {
|
|
83
|
+
test(`rejects ${JSON.stringify(v)?.slice(0, 40)}`, () => {
|
|
84
|
+
let code = "";
|
|
85
|
+
try { validateWorkdir(v); } catch (e) { code = (e as ValidationError).code; }
|
|
86
|
+
expect(code).toBe("workdir_invalid");
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe("daemonDefaultWorkdirRoot — sanitize the self-report", () => {
|
|
92
|
+
const snap = (v: unknown) => JSON.stringify({ daemon_capabilities: { default_workdir_root: v } });
|
|
93
|
+
test("absolute posix root passes through", () => { expect(daemonDefaultWorkdirRoot(snap(ROOT))).toBe(ROOT); });
|
|
94
|
+
test("windows root passes through", () => { expect(daemonDefaultWorkdirRoot(snap("C:\\Users\\alice"))).toBe("C:\\Users\\alice"); });
|
|
95
|
+
test("object snapshot (not string) also accepted", () => {
|
|
96
|
+
expect(daemonDefaultWorkdirRoot({ daemon_capabilities: { default_workdir_root: ROOT } })).toBe(ROOT);
|
|
97
|
+
});
|
|
98
|
+
for (const v of [undefined, null, "", "relative", "~/x", 7, "/a\nb", "/" + "a".repeat(1100)]) {
|
|
99
|
+
test(`not reported / malformed ${JSON.stringify(v)?.slice(0, 30)} → null`, () => {
|
|
100
|
+
expect(daemonDefaultWorkdirRoot(snap(v))).toBeNull();
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
test("null / garbage snapshot → null", () => {
|
|
104
|
+
expect(daemonDefaultWorkdirRoot(null)).toBeNull();
|
|
105
|
+
expect(daemonDefaultWorkdirRoot("{not json")).toBeNull();
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe("create_node + workdir", () => {
|
|
110
|
+
test("#829 V2 flags survive Hub storage and daemon pull", async () => {
|
|
111
|
+
seed({ runtimes_supported: ["opencode-cli"] });
|
|
112
|
+
const flags = { opencodeGeneration: "v2", opencodeUnsafeTools: true, timeout: 600000 };
|
|
113
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: { name: "v2-child", runtime: "opencode-cli", model: "stub/model", flags } });
|
|
114
|
+
expect(r.ok).toBe(true);
|
|
115
|
+
const row = db.get<{ flags_json: string }>("SELECT flags_json FROM node_create_requests WHERE request_id = ?1", r.request_id);
|
|
116
|
+
expect(JSON.parse(row!.flags_json)).toEqual(flags);
|
|
117
|
+
const pulled = await call(tools(DAEMON_TOK).get_create_request, { request_id: r.request_id });
|
|
118
|
+
expect(pulled.ok).toBe(true);
|
|
119
|
+
expect(pulled.node_spec.flags).toEqual(flags);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("#829 V2 without native provider/model rejected before request or child token mint", async () => {
|
|
123
|
+
seed({ runtimes_supported: ["opencode-cli"] });
|
|
124
|
+
const tokenCount = () => db.get<{ n: number }>("SELECT COUNT(*) AS n FROM api_tokens WHERE network_id = ?1", NET)!.n;
|
|
125
|
+
const before = tokenCount();
|
|
126
|
+
const flags = { opencodeGeneration: "v2", opencodeUnsafeTools: true };
|
|
127
|
+
const missing = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: { name: "v2-child", runtime: "opencode-cli", flags } });
|
|
128
|
+
expect(missing.error).toBe("opencode_v2_requires_provider_model");
|
|
129
|
+
const bare = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: { name: "v2-child", runtime: "opencode-cli", model: "opencode", flags } });
|
|
130
|
+
expect(bare.error).toBe("opencode_v2_requires_provider_model");
|
|
131
|
+
expect(rowCount()).toBe(0);
|
|
132
|
+
expect(tokenCount()).toBe(before);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test("#829 missing opt-in rejected before request or child token mint", async () => {
|
|
136
|
+
seed({ runtimes_supported: ["opencode-cli"] });
|
|
137
|
+
const tokenCount = () => db.get<{ n: number }>("SELECT COUNT(*) AS n FROM api_tokens WHERE network_id = ?1", NET)!.n;
|
|
138
|
+
const before = tokenCount();
|
|
139
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: { name: "v2-child", runtime: "opencode-cli", flags: { opencodeGeneration: "v2" } } });
|
|
140
|
+
expect(r.error).toBe("opencode_v2_requires_unsafe_opt_in");
|
|
141
|
+
expect(rowCount()).toBe(0);
|
|
142
|
+
expect(tokenCount()).toBe(before);
|
|
143
|
+
});
|
|
144
|
+
test("🔴 old daemon (no default_workdir_root) + workdir → workdir_not_supported_by_daemon, NO row", async () => {
|
|
145
|
+
seed({});
|
|
146
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: { ...baseSpec, workdir: `${ROOT}/wd-child` } });
|
|
147
|
+
expect(r.ok).toBe(false);
|
|
148
|
+
expect(r.error).toBe("workdir_not_supported_by_daemon");
|
|
149
|
+
expect(rowCount()).toBe(0);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test("old daemon WITHOUT workdir still dispatches (no regression for existing callers)", async () => {
|
|
153
|
+
seed({});
|
|
154
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: baseSpec });
|
|
155
|
+
expect(r.ok).toBe(true);
|
|
156
|
+
const row = db.get<{ workdir: string | null }>("SELECT workdir FROM node_create_requests WHERE request_id = ?1", r.request_id);
|
|
157
|
+
expect(row?.workdir).toBeNull();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("bad shape rejected before dispatch → workdir_invalid, NO row", async () => {
|
|
161
|
+
seed({ default_workdir_root: ROOT });
|
|
162
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: { ...baseSpec, workdir: "relative/dir" } });
|
|
163
|
+
expect(r.error).toBe("workdir_invalid");
|
|
164
|
+
expect(rowCount()).toBe(0);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test("new daemon + workdir → row stores it and get_create_request hands it to the daemon", async () => {
|
|
168
|
+
seed({ default_workdir_root: ROOT });
|
|
169
|
+
const wd = `${ROOT}/wd-child`;
|
|
170
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: { ...baseSpec, workdir: wd } });
|
|
171
|
+
expect(r.ok).toBe(true);
|
|
172
|
+
const row = db.get<{ workdir: string | null }>("SELECT workdir FROM node_create_requests WHERE request_id = ?1", r.request_id);
|
|
173
|
+
expect(row?.workdir).toBe(wd);
|
|
174
|
+
const g = await call(tools(DAEMON_TOK).get_create_request, { request_id: r.request_id });
|
|
175
|
+
expect(g.ok).toBe(true);
|
|
176
|
+
expect(g.node_spec.workdir).toBe(wd);
|
|
177
|
+
expect(g.node_spec.name).toBe(baseSpec.name);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test("workdir is trimmed before storage", async () => {
|
|
181
|
+
seed({ default_workdir_root: ROOT });
|
|
182
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: { ...baseSpec, workdir: ` ~/wd-child ` } });
|
|
183
|
+
expect(r.ok).toBe(true);
|
|
184
|
+
const row = db.get<{ workdir: string | null }>("SELECT workdir FROM node_create_requests WHERE request_id = ?1", r.request_id);
|
|
185
|
+
expect(row?.workdir).toBe("~/wd-child");
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test("new daemon WITHOUT workdir → get_create_request node_spec has NO workdir key (daemon keeps legacy layout)", async () => {
|
|
189
|
+
seed({ default_workdir_root: ROOT });
|
|
190
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: baseSpec });
|
|
191
|
+
expect(r.ok).toBe(true);
|
|
192
|
+
const g = await call(tools(DAEMON_TOK).get_create_request, { request_id: r.request_id });
|
|
193
|
+
expect(g.ok).toBe(true);
|
|
194
|
+
expect("workdir" in g.node_spec).toBe(false);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test("audit detail records the requested workdir", async () => {
|
|
198
|
+
seed({ default_workdir_root: ROOT });
|
|
199
|
+
const wd = `${ROOT}/wd-child`;
|
|
200
|
+
const r = await call(tools().create_node, { daemon_node_id: DAEMON_ID, node_spec: { ...baseSpec, workdir: wd } });
|
|
201
|
+
const a = db.get<{ detail: string }>(
|
|
202
|
+
"SELECT detail FROM audit_log WHERE target_id = ?1 AND action = 'create_node_dispatch_attempted'", r.request_id,
|
|
203
|
+
);
|
|
204
|
+
expect(JSON.parse(a?.detail || "{}").workdir).toBe(wd);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test("list_host_supervisors (MCP) surfaces default_workdir_root for admin", async () => {
|
|
208
|
+
seed({ default_workdir_root: ROOT });
|
|
209
|
+
const r = await call(tools().list_host_supervisors, {});
|
|
210
|
+
const d = (r as any).daemons.find((x: any) => x.daemon_node_id === DAEMON_ID);
|
|
211
|
+
expect(d.default_workdir_root).toBe(ROOT);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test("list_host_supervisors (MCP) omits the key for an old daemon", async () => {
|
|
215
|
+
seed({});
|
|
216
|
+
const r = await call(tools().list_host_supervisors, {});
|
|
217
|
+
const d = (r as any).daemons.find((x: any) => x.daemon_node_id === DAEMON_ID);
|
|
218
|
+
expect(d).toBeDefined();
|
|
219
|
+
expect("default_workdir_root" in d).toBe(false);
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
// ── REST /api/host-supervisors: admin sees the root, member does not ──────────
|
|
224
|
+
let BASE = "";
|
|
225
|
+
let server: any = null;
|
|
226
|
+
let ownerTok = "", memberTok = "", restNet = "", ownerId = "";
|
|
227
|
+
describe("/api/host-supervisors — default_workdir_root visibility", () => {
|
|
228
|
+
beforeAll(async () => {
|
|
229
|
+
process.env.HOST = "127.0.0.1";
|
|
230
|
+
const sfx = `${Date.now()}_${Math.floor(Math.random() * 1000)}`;
|
|
231
|
+
const pw = "BootstrapPw123Aa!";
|
|
232
|
+
const s = register(`wd_seed_${sfx}`, pw, undefined, "seed");
|
|
233
|
+
if (!s.ok) throw new Error("seed failed");
|
|
234
|
+
db.run("UPDATE users SET role = 'admin' WHERE username = ?1", [`wd_seed_${sfx}`]);
|
|
235
|
+
const o = register(`wd_owner_${sfx}`, pw, undefined, "seed");
|
|
236
|
+
if (!o.ok || !o.token) throw new Error("owner register failed");
|
|
237
|
+
ownerTok = o.token; restNet = o.network_id ?? "";
|
|
238
|
+
ownerId = login(`wd_owner_${sfx}`, pw).user!.user_id;
|
|
239
|
+
const m = register(`wd_member_${sfx}`, pw, undefined, "seed");
|
|
240
|
+
if (!m.ok || !m.token) throw new Error("member register failed");
|
|
241
|
+
memberTok = m.token;
|
|
242
|
+
const memberId = login(`wd_member_${sfx}`, pw).user!.user_id;
|
|
243
|
+
// 多用户 Agent 权限:新成员默认只看授权 Agent;本测试测的是「完全信任的成员」旧语义,显式 agent_access=all。
|
|
244
|
+
const add = addNetworkMember(restNet, memberId, "member", undefined, { agentAccess: "all" });
|
|
245
|
+
if (!add.ok) throw new Error("add member failed: " + add.error);
|
|
246
|
+
const { bootServer } = await import("./server.js");
|
|
247
|
+
server = bootServer({ port: 0, hostname: "127.0.0.1" });
|
|
248
|
+
BASE = `http://127.0.0.1:${server.port}`;
|
|
249
|
+
for (const [alias, caps] of [["wd-new", { default_workdir_root: ROOT }], ["wd-old", {}]] as const) {
|
|
250
|
+
db.run(
|
|
251
|
+
`INSERT OR REPLACE INTO nodes (node_id, node_name, alias, runtime, model, config_path, channels, server, hostname, network_id, config_revision, config_snapshot)
|
|
252
|
+
VALUES (?1, ?2, ?2, 'claude-agent-sdk', 'm', '/tmp/cfg.json', '[]', 'h', 'h', ?3, 0, ?4)`,
|
|
253
|
+
[`wd_n_${alias}`, alias, restNet, JSON.stringify({ role: "host_supervisor", daemon_capabilities: caps })],
|
|
254
|
+
);
|
|
255
|
+
db.run(
|
|
256
|
+
`INSERT OR REPLACE INTO api_tokens (token_id, token_hash, user_id, network_id, name, scope) VALUES (?1, ?2, ?3, ?4, ?5, 'network')`,
|
|
257
|
+
[`wd_t_${alias}`, `hash_wd_${alias}`, ownerId, restNet, `node:${alias}`],
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
afterAll(() => {
|
|
262
|
+
try { server?.stop?.(true); } catch {}
|
|
263
|
+
try { db.run("DELETE FROM nodes WHERE network_id = ?1", [restNet]); } catch {}
|
|
264
|
+
try { db.run("DELETE FROM api_tokens WHERE network_id = ?1 AND name LIKE 'node:%'", [restNet]); } catch {}
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
async function list(tok: string): Promise<any[]> {
|
|
268
|
+
const res = await fetch(`${BASE}/api/host-supervisors?network_id=${encodeURIComponent(restNet)}`, { headers: { Authorization: `Bearer ${tok}` } });
|
|
269
|
+
const b = await res.json();
|
|
270
|
+
if (!b.ok) throw new Error("list failed: " + JSON.stringify(b));
|
|
271
|
+
return b.daemons;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
test("owner sees default_workdir_root on the new daemon", async () => {
|
|
275
|
+
const d = (await list(ownerTok)).find(x => x.alias === "wd-new");
|
|
276
|
+
expect(d?.default_workdir_root).toBe(ROOT);
|
|
277
|
+
});
|
|
278
|
+
test("old daemon row has NO default_workdir_root key (app hides the row)", async () => {
|
|
279
|
+
const d = (await list(ownerTok)).find(x => x.alias === "wd-old");
|
|
280
|
+
expect(d).toBeDefined();
|
|
281
|
+
expect("default_workdir_root" in d).toBe(false);
|
|
282
|
+
});
|
|
283
|
+
test("🔴 member does NOT see the host's home path", async () => {
|
|
284
|
+
const rows = await list(memberTok);
|
|
285
|
+
const d = rows.find(x => x.alias === "wd-new");
|
|
286
|
+
expect(d).toBeDefined();
|
|
287
|
+
expect("default_workdir_root" in d).toBe(false);
|
|
288
|
+
});
|
|
289
|
+
});
|
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
// RFC-026 v4 P1 — unit tests for state + sweeper (covers H + J live-
|
|
2
|
+
// test deferment per 通信牛 PR #299 review). These exercise the same
|
|
3
|
+
// pure functions the daemon-facing MCP tools call into; the e2e
|
|
4
|
+
// scenarios H/J would only re-verify these same code paths through
|
|
5
|
+
// multi-daemon or crash-sim plumbing, which is Phase 3 work.
|
|
6
|
+
|
|
7
|
+
import { describe, expect, test, beforeEach, afterAll } from "bun:test";
|
|
8
|
+
import { db } from "./db.js";
|
|
9
|
+
import {
|
|
10
|
+
putPendingEnvBlob,
|
|
11
|
+
takePendingEnvBlob,
|
|
12
|
+
peekPendingEnvBlob,
|
|
13
|
+
runOrphanSweepOnce,
|
|
14
|
+
evictExpired,
|
|
15
|
+
finalizeCreateOnFirstRegister,
|
|
16
|
+
newRequestId,
|
|
17
|
+
stopBackgroundTimersForTest,
|
|
18
|
+
resolveCallerDaemonTokenBound,
|
|
19
|
+
} from "./create-node.js";
|
|
20
|
+
import { hashToken } from "./db.js";
|
|
21
|
+
|
|
22
|
+
// ── helpers ────────────────────────────────────────────────────────
|
|
23
|
+
function freshFixtures() {
|
|
24
|
+
// Wipe RFC-026 state between tests. Other suites use the same DB;
|
|
25
|
+
// we only delete from tables RFC-026 owns.
|
|
26
|
+
db.run("DELETE FROM node_create_requests");
|
|
27
|
+
db.run("DELETE FROM api_tokens WHERE role = 'child' OR role = 'test_child' OR (request_id IS NOT NULL AND request_id LIKE 'cr_test_%')");
|
|
28
|
+
stopBackgroundTimersForTest();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function insertRequestRow(
|
|
32
|
+
request_id: string,
|
|
33
|
+
daemon_node_id: string,
|
|
34
|
+
child_name: string,
|
|
35
|
+
status: "pending" | "delivered" | "succeeded" | "failed" | "expired",
|
|
36
|
+
created_at_ms: number,
|
|
37
|
+
child_token_id: string | null = null,
|
|
38
|
+
network_id: string = "test_net",
|
|
39
|
+
) {
|
|
40
|
+
db.run(
|
|
41
|
+
`INSERT INTO node_create_requests
|
|
42
|
+
(request_id, daemon_node_id, child_name, network_id, runtime, model, flags_json, env_keys, status, child_token_id, created_at, created_by_token)
|
|
43
|
+
VALUES (?1, ?2, ?3, ?4, 'claude-agent-sdk', 'x', '{}', '[]', ?5, ?6, ?7, 'tok_test')`,
|
|
44
|
+
[request_id, daemon_node_id, child_name, network_id, status, child_token_id, created_at_ms],
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function insertChildTokenRow(token_id: string, request_id: string) {
|
|
49
|
+
db.run(
|
|
50
|
+
`INSERT INTO api_tokens (token_id, token_hash, user_id, network_id, name, scope, role, request_id)
|
|
51
|
+
VALUES (?1, ?2, ?3, ?4, ?5, ?6, 'child', ?7)`,
|
|
52
|
+
[token_id, `hash_${token_id}`, "u_test_creator", "test_net", `node:test_child_${token_id}`, "network", request_id],
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ── tests ──────────────────────────────────────────────────────────
|
|
57
|
+
|
|
58
|
+
beforeEach(() => freshFixtures());
|
|
59
|
+
afterAll(() => stopBackgroundTimersForTest());
|
|
60
|
+
|
|
61
|
+
describe("§4.4 F1 mint-stream-evict — pendingEnvBlobs Map (covers H C2)", () => {
|
|
62
|
+
test("takePendingEnvBlob with WRONG daemon_node_id returns null (C2 cross-daemon guard)", () => {
|
|
63
|
+
const id = newRequestId();
|
|
64
|
+
putPendingEnvBlob({
|
|
65
|
+
request_id: id,
|
|
66
|
+
daemon_node_id: "node_daemon_A",
|
|
67
|
+
env_blob: { ANTHROPIC_API_KEY: "secret-A" },
|
|
68
|
+
child_token: "ntok_test_child_A",
|
|
69
|
+
child_token_id: "tok_test_child_A",
|
|
70
|
+
});
|
|
71
|
+
// daemonB attempts to take daemonA's request → null
|
|
72
|
+
expect(takePendingEnvBlob(id, "node_daemon_B")).toBeNull();
|
|
73
|
+
// But the entry IS NOT consumed by the wrong-caller attempt — daemonA
|
|
74
|
+
// can still take it. (Otherwise an attacker could DoS legit dispatch
|
|
75
|
+
// by spamming wrong-daemon takes.)
|
|
76
|
+
const blob = takePendingEnvBlob(id, "node_daemon_A");
|
|
77
|
+
expect(blob).not.toBeNull();
|
|
78
|
+
expect(blob!.env_blob).toEqual({ ANTHROPIC_API_KEY: "secret-A" });
|
|
79
|
+
expect(blob!.child_token).toBe("ntok_test_child_A");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test("takePendingEnvBlob with RIGHT daemon_node_id evicts immediately (F1 one-shot consume)", () => {
|
|
83
|
+
const id = newRequestId();
|
|
84
|
+
putPendingEnvBlob({
|
|
85
|
+
request_id: id,
|
|
86
|
+
daemon_node_id: "node_daemon_X",
|
|
87
|
+
env_blob: { K1: "v1" },
|
|
88
|
+
child_token: "ntok_x",
|
|
89
|
+
child_token_id: "tok_x",
|
|
90
|
+
});
|
|
91
|
+
expect(peekPendingEnvBlob(id)).not.toBeNull();
|
|
92
|
+
expect(takePendingEnvBlob(id, "node_daemon_X")).not.toBeNull();
|
|
93
|
+
// Second take is null — entry was evicted on first successful take
|
|
94
|
+
expect(takePendingEnvBlob(id, "node_daemon_X")).toBeNull();
|
|
95
|
+
expect(peekPendingEnvBlob(id)).toBeNull();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("takePendingEnvBlob with unknown id returns null", () => {
|
|
99
|
+
expect(takePendingEnvBlob("cr_never_existed", "node_anything")).toBeNull();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("evictExpired drops entries past TTL", () => {
|
|
103
|
+
const id = newRequestId();
|
|
104
|
+
putPendingEnvBlob({
|
|
105
|
+
request_id: id,
|
|
106
|
+
daemon_node_id: "node_z",
|
|
107
|
+
env_blob: {},
|
|
108
|
+
child_token: "ntok_z",
|
|
109
|
+
child_token_id: "tok_z",
|
|
110
|
+
});
|
|
111
|
+
// Fast-forward past 60s TTL
|
|
112
|
+
const past = Date.now() + 60_001;
|
|
113
|
+
expect(evictExpired(past)).toBeGreaterThan(0);
|
|
114
|
+
expect(takePendingEnvBlob(id, "node_z")).toBeNull();
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe("§4.4.8 C4 orphan sweeper — runOrphanSweepOnce (covers J F-1 + F-2)", () => {
|
|
119
|
+
test("F-1: status='pending' age > TTL → revoke child-ntok + mark status='failed'", () => {
|
|
120
|
+
const reqId = `cr_test_${Date.now()}_F1`;
|
|
121
|
+
const tokId = `tok_test_${Date.now()}_F1`;
|
|
122
|
+
const longAgo = Date.now() - 90_000; // 90s past — well > 60s TTL
|
|
123
|
+
insertChildTokenRow(tokId, reqId);
|
|
124
|
+
insertRequestRow(reqId, "node_daemon_F1", "child_F1", "pending", longAgo, tokId);
|
|
125
|
+
const result = runOrphanSweepOnce();
|
|
126
|
+
expect(result.swept).toBeGreaterThanOrEqual(1);
|
|
127
|
+
expect(result.revoked).toBeGreaterThanOrEqual(1);
|
|
128
|
+
// Verify api_tokens.revoked_at is now set
|
|
129
|
+
const tokRow = db.get<{ revoked_at: string | null }>(
|
|
130
|
+
"SELECT revoked_at FROM api_tokens WHERE token_id = ?1", tokId,
|
|
131
|
+
);
|
|
132
|
+
expect(tokRow?.revoked_at).not.toBeNull();
|
|
133
|
+
// Verify request status flipped to 'failed'
|
|
134
|
+
const reqRow = db.get<{ status: string; error: string | null }>(
|
|
135
|
+
"SELECT status, error FROM node_create_requests WHERE request_id = ?1", reqId,
|
|
136
|
+
);
|
|
137
|
+
expect(reqRow?.status).toBe("failed");
|
|
138
|
+
expect(reqRow?.error).toContain("sweeper_revoked_before_delivery");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("F-2: status='delivered' age > TTL → revoke child-ntok + mark status='expired'", () => {
|
|
142
|
+
const reqId = `cr_test_${Date.now()}_F2`;
|
|
143
|
+
const tokId = `tok_test_${Date.now()}_F2`;
|
|
144
|
+
const longAgo = Date.now() - 120_000; // 120s past
|
|
145
|
+
insertChildTokenRow(tokId, reqId);
|
|
146
|
+
insertRequestRow(reqId, "node_daemon_F2", "child_F2", "delivered", longAgo, tokId);
|
|
147
|
+
const result = runOrphanSweepOnce();
|
|
148
|
+
expect(result.swept).toBeGreaterThanOrEqual(1);
|
|
149
|
+
expect(result.revoked).toBeGreaterThanOrEqual(1);
|
|
150
|
+
const tokRow = db.get<{ revoked_at: string | null }>(
|
|
151
|
+
"SELECT revoked_at FROM api_tokens WHERE token_id = ?1", tokId,
|
|
152
|
+
);
|
|
153
|
+
expect(tokRow?.revoked_at).not.toBeNull();
|
|
154
|
+
const reqRow = db.get<{ status: string; error: string | null }>(
|
|
155
|
+
"SELECT status, error FROM node_create_requests WHERE request_id = ?1", reqId,
|
|
156
|
+
);
|
|
157
|
+
expect(reqRow?.status).toBe("expired");
|
|
158
|
+
expect(reqRow?.error).toContain("sweeper_revoked_after_delivery_no_ack");
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test("does NOT sweep recent pending/delivered rows (within TTL)", () => {
|
|
162
|
+
const reqId = `cr_test_${Date.now()}_fresh`;
|
|
163
|
+
const tokId = `tok_test_${Date.now()}_fresh`;
|
|
164
|
+
insertChildTokenRow(tokId, reqId);
|
|
165
|
+
// created 5s ago — well within 60s TTL
|
|
166
|
+
insertRequestRow(reqId, "node_daemon_fresh", "child_fresh", "pending", Date.now() - 5_000, tokId);
|
|
167
|
+
const result = runOrphanSweepOnce();
|
|
168
|
+
expect(result.swept).toBe(0);
|
|
169
|
+
expect(result.revoked).toBe(0);
|
|
170
|
+
const tokRow = db.get<{ revoked_at: string | null }>(
|
|
171
|
+
"SELECT revoked_at FROM api_tokens WHERE token_id = ?1", tokId,
|
|
172
|
+
);
|
|
173
|
+
expect(tokRow?.revoked_at).toBeNull();
|
|
174
|
+
const reqRow = db.get<{ status: string }>(
|
|
175
|
+
"SELECT status FROM node_create_requests WHERE request_id = ?1", reqId,
|
|
176
|
+
);
|
|
177
|
+
expect(reqRow?.status).toBe("pending");
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test("does NOT sweep terminal rows (succeeded/failed/expired)", () => {
|
|
181
|
+
const reqId = `cr_test_${Date.now()}_terminal`;
|
|
182
|
+
const tokId = `tok_test_${Date.now()}_terminal`;
|
|
183
|
+
insertChildTokenRow(tokId, reqId);
|
|
184
|
+
insertRequestRow(reqId, "node_daemon_t", "child_t", "succeeded", Date.now() - 120_000, tokId);
|
|
185
|
+
const result = runOrphanSweepOnce();
|
|
186
|
+
expect(result.swept).toBe(0);
|
|
187
|
+
expect(result.revoked).toBe(0);
|
|
188
|
+
const tokRow = db.get<{ revoked_at: string | null }>(
|
|
189
|
+
"SELECT revoked_at FROM api_tokens WHERE token_id = ?1", tokId,
|
|
190
|
+
);
|
|
191
|
+
expect(tokRow?.revoked_at).toBeNull();
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test("sweeper is idempotent — second run on same stale row is a no-op", () => {
|
|
195
|
+
const reqId = `cr_test_${Date.now()}_idem`;
|
|
196
|
+
const tokId = `tok_test_${Date.now()}_idem`;
|
|
197
|
+
insertChildTokenRow(tokId, reqId);
|
|
198
|
+
insertRequestRow(reqId, "node_daemon_i", "child_i", "pending", Date.now() - 120_000, tokId);
|
|
199
|
+
const r1 = runOrphanSweepOnce();
|
|
200
|
+
expect(r1.swept).toBe(1);
|
|
201
|
+
const r2 = runOrphanSweepOnce();
|
|
202
|
+
expect(r2.swept).toBe(0); // already terminal after first sweep
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
describe("§4.1.4 C2 token-bound daemon resolution regression (PR #299 BLOCKER #1)", () => {
|
|
207
|
+
// Calls the REAL resolveCallerDaemonTokenBound helper exported from
|
|
208
|
+
// create-node.ts — same code path the daemon-facing MCP tools take
|
|
209
|
+
// (per 通信龙 PR #299 nit 1, no inline-mirror SQL: anti-pattern that
|
|
210
|
+
// would let helper-drift slip past). Setup: two daemons same alias
|
|
211
|
+
// in different networks; two ntoks bound to each; confirm each
|
|
212
|
+
// ntok's resolution returns ITS OWN node row (not the other).
|
|
213
|
+
test("two daemons same alias different networks: helper resolves token to correct node", () => {
|
|
214
|
+
db.run("DELETE FROM nodes WHERE alias = 'daemon-shared'");
|
|
215
|
+
db.run("DELETE FROM api_tokens WHERE token_id IN ('tok_test_A', 'tok_test_B')");
|
|
216
|
+
// daemonA in netA
|
|
217
|
+
db.run(
|
|
218
|
+
`INSERT INTO nodes (node_id, node_name, alias, runtime, model, network_id, updated_at)
|
|
219
|
+
VALUES ('node_test_daemonA', 'daemon-shared', 'daemon-shared', 'claude-agent-sdk', 'm', 'net_test_A', datetime('now'))`,
|
|
220
|
+
);
|
|
221
|
+
// daemonB in netB — SAME alias
|
|
222
|
+
db.run(
|
|
223
|
+
`INSERT INTO nodes (node_id, node_name, alias, runtime, model, network_id, updated_at)
|
|
224
|
+
VALUES ('node_test_daemonB', 'daemon-shared', 'daemon-shared', 'claude-agent-sdk', 'm', 'net_test_B', datetime('now'))`,
|
|
225
|
+
);
|
|
226
|
+
// ntok rows bound to each daemon (name='node:daemon-shared'; network_id distinguishes)
|
|
227
|
+
db.run(
|
|
228
|
+
`INSERT INTO api_tokens (token_id, token_hash, user_id, network_id, name, scope)
|
|
229
|
+
VALUES ('tok_test_A', ?1, 'u_test_creator', 'net_test_A', 'node:daemon-shared', 'network')`,
|
|
230
|
+
[hashToken("ntok_test_A")],
|
|
231
|
+
);
|
|
232
|
+
db.run(
|
|
233
|
+
`INSERT INTO api_tokens (token_id, token_hash, user_id, network_id, name, scope)
|
|
234
|
+
VALUES ('tok_test_B', ?1, 'u_test_creator', 'net_test_B', 'node:daemon-shared', 'network')`,
|
|
235
|
+
[hashToken("ntok_test_B")],
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
// ntok A → must resolve to node_test_daemonA (NOT daemonB)
|
|
239
|
+
const resA = resolveCallerDaemonTokenBound({
|
|
240
|
+
callerTokenIsNetwork: true,
|
|
241
|
+
callerTokenId: "tok_test_A",
|
|
242
|
+
enforceNetworkId: "net_test_A",
|
|
243
|
+
});
|
|
244
|
+
expect(resA.ok).toBe(true);
|
|
245
|
+
if (resA.ok) {
|
|
246
|
+
expect(resA.daemonNodeId).toBe("node_test_daemonA");
|
|
247
|
+
expect(resA.networkId).toBe("net_test_A");
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// ntok B → must resolve to node_test_daemonB
|
|
251
|
+
const resB = resolveCallerDaemonTokenBound({
|
|
252
|
+
callerTokenIsNetwork: true,
|
|
253
|
+
callerTokenId: "tok_test_B",
|
|
254
|
+
enforceNetworkId: "net_test_B",
|
|
255
|
+
});
|
|
256
|
+
expect(resB.ok).toBe(true);
|
|
257
|
+
if (resB.ok) expect(resB.daemonNodeId).toBe("node_test_daemonB");
|
|
258
|
+
|
|
259
|
+
// Pre-blocker buggy form (alias only) would be ambiguous —
|
|
260
|
+
// confirm there really ARE 2 rows so the SQL was un-scoped before.
|
|
261
|
+
const all = db.all<{ node_id: string }>(
|
|
262
|
+
`SELECT node_id FROM nodes WHERE alias = ?1`, "daemon-shared",
|
|
263
|
+
);
|
|
264
|
+
expect(all.length).toBe(2);
|
|
265
|
+
|
|
266
|
+
// Mismatched scope: tok_test_A presented with enforceNetworkId=B
|
|
267
|
+
// (compromised caller trying to pivot to wrong net) → reject.
|
|
268
|
+
const resCross = resolveCallerDaemonTokenBound({
|
|
269
|
+
callerTokenIsNetwork: true,
|
|
270
|
+
callerTokenId: "tok_test_A",
|
|
271
|
+
enforceNetworkId: "net_test_B",
|
|
272
|
+
});
|
|
273
|
+
expect(resCross.ok).toBe(false);
|
|
274
|
+
|
|
275
|
+
// utok (callerTokenIsNetwork=false) → reject as not-a-daemon
|
|
276
|
+
const resUtok = resolveCallerDaemonTokenBound({
|
|
277
|
+
callerTokenIsNetwork: false,
|
|
278
|
+
callerTokenId: "tok_test_A",
|
|
279
|
+
enforceNetworkId: "net_test_A",
|
|
280
|
+
});
|
|
281
|
+
expect(resUtok.ok).toBe(false);
|
|
282
|
+
|
|
283
|
+
// Revoked token → reject
|
|
284
|
+
db.run("UPDATE api_tokens SET revoked_at = datetime('now') WHERE token_id = 'tok_test_A'");
|
|
285
|
+
const resRevoked = resolveCallerDaemonTokenBound({
|
|
286
|
+
callerTokenIsNetwork: true,
|
|
287
|
+
callerTokenId: "tok_test_A",
|
|
288
|
+
enforceNetworkId: "net_test_A",
|
|
289
|
+
});
|
|
290
|
+
expect(resRevoked.ok).toBe(false);
|
|
291
|
+
|
|
292
|
+
db.run("DELETE FROM nodes WHERE node_id IN ('node_test_daemonA', 'node_test_daemonB')");
|
|
293
|
+
db.run("DELETE FROM api_tokens WHERE token_id IN ('tok_test_A', 'tok_test_B')");
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
test("daemon B cannot take/ack daemon A's request even with same alias (Map-level guard)", () => {
|
|
297
|
+
const id = newRequestId();
|
|
298
|
+
putPendingEnvBlob({
|
|
299
|
+
request_id: id,
|
|
300
|
+
daemon_node_id: "node_legit_daemonA",
|
|
301
|
+
env_blob: { ANTHROPIC_API_KEY: "s" },
|
|
302
|
+
child_token: "ntok_a",
|
|
303
|
+
child_token_id: "tok_a",
|
|
304
|
+
});
|
|
305
|
+
// daemonB-token resolves to node_id = node_attacker (different node_id)
|
|
306
|
+
expect(takePendingEnvBlob(id, "node_attacker")).toBeNull();
|
|
307
|
+
// Original entry still there for daemonA
|
|
308
|
+
expect(takePendingEnvBlob(id, "node_legit_daemonA")).not.toBeNull();
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
describe("finalizeCreateOnFirstRegister — content-match on child register", () => {
|
|
313
|
+
test("matching child_name + network_id flips status='succeeded' + stamps child_node_id", () => {
|
|
314
|
+
const reqId = `cr_test_${Date.now()}_finalize`;
|
|
315
|
+
insertRequestRow(reqId, "node_daemon_F", "child_finalize_test", "delivered", Date.now() - 1_000, null, "test_net_finalize");
|
|
316
|
+
const r = finalizeCreateOnFirstRegister({
|
|
317
|
+
node_id: "node_child_real_F",
|
|
318
|
+
alias: "child_finalize_test",
|
|
319
|
+
network_id: "test_net_finalize",
|
|
320
|
+
});
|
|
321
|
+
expect(r.finalized).toBe(1);
|
|
322
|
+
const row = db.get<{ status: string; child_node_id: string | null }>(
|
|
323
|
+
"SELECT status, child_node_id FROM node_create_requests WHERE request_id = ?1", reqId,
|
|
324
|
+
);
|
|
325
|
+
expect(row?.status).toBe("succeeded");
|
|
326
|
+
expect(row?.child_node_id).toBe("node_child_real_F");
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
test("non-matching alias is a no-op", () => {
|
|
330
|
+
const reqId = `cr_test_${Date.now()}_nomatch`;
|
|
331
|
+
insertRequestRow(reqId, "node_daemon_nm", "expected_alias", "pending", Date.now(), null, "test_net_nm");
|
|
332
|
+
const r = finalizeCreateOnFirstRegister({
|
|
333
|
+
node_id: "node_intruder",
|
|
334
|
+
alias: "different_alias",
|
|
335
|
+
network_id: "test_net_nm",
|
|
336
|
+
});
|
|
337
|
+
expect(r.finalized).toBe(0);
|
|
338
|
+
const row = db.get<{ status: string }>(
|
|
339
|
+
"SELECT status FROM node_create_requests WHERE request_id = ?1", reqId,
|
|
340
|
+
);
|
|
341
|
+
expect(row?.status).toBe("pending");
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
test("cross-network: same child_name in different network is a no-op (network_id mismatch)", () => {
|
|
345
|
+
const reqId = `cr_test_${Date.now()}_crossnet`;
|
|
346
|
+
insertRequestRow(reqId, "node_daemon_xn", "shared_name", "delivered", Date.now(), null, "test_net_A");
|
|
347
|
+
const r = finalizeCreateOnFirstRegister({
|
|
348
|
+
node_id: "node_evil",
|
|
349
|
+
alias: "shared_name",
|
|
350
|
+
network_id: "test_net_B", // different network
|
|
351
|
+
});
|
|
352
|
+
expect(r.finalized).toBe(0);
|
|
353
|
+
});
|
|
354
|
+
});
|