@sleep2agi/commhub-server 0.9.0-preview.27 → 0.9.0-preview.270
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 +120 -3
- package/bin/commhub.ts +93 -9
- package/build-info.json +7 -0
- package/package.json +10 -8
- package/src/ack-create-request-transport.test.ts +182 -0
- package/src/ack-create-request.test.ts +52 -8
- package/src/admin-delete-network-http.test.ts +229 -0
- package/src/admin-networks-http.test.ts +88 -0
- package/src/adoption-candidates-http.test.ts +140 -0
- package/src/adoption-candidates.ts +158 -0
- package/src/agent-access-batch.test.ts +210 -0
- package/src/agent-access.ts +585 -0
- package/src/agent-acl-groups-http.test.ts +247 -0
- package/src/agent-acl-http.test.ts +691 -0
- package/src/agent-acl-slice3-http.test.ts +885 -0
- package/src/agent-department-grants.test.ts +129 -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 +131 -4
- package/src/auth-abuse-counters.test.ts +144 -0
- package/src/auth-abuse-counters.ts +122 -0
- package/src/auth-layer-shape.test.ts +105 -0
- package/src/auth-password-change-cli-tokens-http.test.ts +423 -0
- package/src/auth-sessions-http.test.ts +260 -0
- package/src/auth-validate.test.ts +5 -1
- package/src/auth-weak-password-login.test.ts +176 -0
- package/src/auth.ts +641 -68
- package/src/backfill-node-ids-2664.test.ts +128 -0
- package/src/binding-generation-http.test.ts +141 -0
- package/src/blocked-exit-on-reply.test.ts +122 -0
- package/src/broadcast-fanout-2721.test.ts +149 -0
- package/src/broadcast-fanout.ts +117 -0
- package/src/build-commit-report-http.test.ts +128 -0
- package/src/build-info.test.ts +60 -0
- package/src/build-info.ts +85 -0
- package/src/chain-attempt-dedup.test.ts +447 -0
- package/src/clip.test.ts +36 -0
- package/src/clip.ts +48 -0
- package/src/clone-node.test.ts +269 -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 +365 -0
- package/src/create-node-error-domain.test.ts +127 -0
- package/src/create-node-network-id.test.ts +91 -0
- package/src/create-node-tool-schema.test.ts +49 -0
- package/src/create-node-unicode-name.test.ts +126 -0
- package/src/create-node-validate.test.ts +199 -7
- package/src/create-node-validate.ts +232 -15
- package/src/create-node-workdir.test.ts +298 -0
- package/src/create-node.ts +88 -23
- package/src/daemon-provider-snapshots.test.ts +453 -0
- package/src/daemon-provider-snapshots.ts +327 -0
- package/src/daemon-token-owner-http.test.ts +346 -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 +1298 -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 +139 -0
- package/src/departments-http.test.ts +185 -0
- package/src/departments-mcp-error-domain.test.ts +135 -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 +227 -0
- package/src/empty-node-sweeper.test.ts +104 -0
- package/src/empty-node-sweeper.ts +88 -0
- package/src/env-transport-gate-http.test.ts +240 -0
- package/src/env-transport-gate.ts +80 -0
- package/src/env-transport-policy.ts +56 -0
- package/src/etag-of.test.ts +57 -0
- package/src/event-loop-lag.test.ts +180 -0
- package/src/event-loop-lag.ts +201 -0
- package/src/expiry-counters.test.ts +187 -0
- package/src/expiry-counters.ts +227 -0
- package/src/expiry-evidence-db.test.ts +103 -0
- package/src/ext-token-shared.test.ts +60 -0
- package/src/ext-validation-regression.test.ts +10 -17
- 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 +25 -4
- package/src/file-download-date-bucket.test.ts +1 -1
- package/src/file-network-scope-dev-open.test.ts +1 -1
- package/src/file-network-scope.test.ts +7 -6
- package/src/group-membership-events-http.test.ts +264 -0
- package/src/group-messages-http.test.ts +483 -0
- package/src/group-messages.ts +297 -0
- package/src/health-anon-readers.ts +67 -0
- package/src/health-host-disk-2756.test.ts +71 -0
- package/src/health-identity-root.test.ts +98 -0
- package/src/health-redaction.test.ts +1599 -7
- package/src/health-status-reasons.test.ts +84 -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 +664 -0
- package/src/hub-provider-config.test.ts +166 -0
- package/src/hub-provider-config.ts +202 -0
- package/src/hub-timestamp.test.ts +38 -0
- package/src/hub-timestamp.ts +27 -0
- package/src/human-dm.ts +215 -0
- package/src/human-inbox.ts +42 -0
- package/src/inbox-caller.ts +49 -0
- package/src/inbox-count-sse.test.ts +318 -0
- package/src/inbox-count.ts +29 -0
- package/src/index.ts +6 -2
- package/src/license-state.test.ts +105 -0
- package/src/license-state.ts +48 -0
- package/src/lifecycle-error-domain.test.ts +149 -0
- package/src/list-ack-error-domain.test.ts +108 -0
- package/src/list-host-supervisors.test.ts +15 -4
- package/src/list-tasks-omit-default.test.ts +84 -0
- package/src/list-tasks-since-last.test.ts +100 -0
- package/src/list-tasks-since.test.ts +104 -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 +168 -0
- package/src/mcp-route-label-http.test.ts +120 -0
- package/src/mcp-write-network-resolution.test.ts +21 -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-quota-config.test.ts +100 -0
- package/src/network-rules-http.test.ts +169 -0
- package/src/network-rules.ts +129 -0
- package/src/network-scope.ts +222 -3
- package/src/network-token-membership-http.test.ts +186 -0
- package/src/node-alias-control.ts +12 -0
- package/src/node-attrs.test.ts +47 -2
- package/src/node-caller-identity-http.test.ts +1108 -0
- package/src/node-config-last-update.test.ts +126 -0
- package/src/node-config-last-update.ts +107 -0
- package/src/node-create-request-status-http.test.ts +105 -0
- package/src/node-daemon-bindings.test.ts +142 -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 +350 -0
- package/src/node-health-codex-login.test.ts +120 -0
- package/src/node-health-dispatch-http.test.ts +373 -0
- package/src/node-health-guard-origin.test.ts +98 -0
- package/src/node-health-guard.ts +121 -0
- package/src/node-health-passthrough.test.ts +115 -0
- package/src/node-health-store.ts +101 -0
- package/src/node-id-alias.test.ts +190 -0
- package/src/node-identity-conflict-http.test.ts +269 -0
- package/src/node-identity-conflict.ts +142 -0
- package/src/node-identity-mismatch.test.ts +74 -0
- package/src/node-in-flight-tasks.ts +31 -0
- package/src/node-lifecycle-controllable.test.ts +64 -0
- package/src/node-lifecycle-controllable.ts +53 -0
- package/src/node-lifecycle-read-http.test.ts +257 -0
- package/src/node-lifecycle-read.ts +124 -0
- package/src/node-logs-transport.test.ts +266 -0
- package/src/node-peer-allowlist.test.ts +114 -0
- package/src/node-permissions-http.test.ts +901 -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-secret-vault-inject.test.ts +132 -0
- package/src/node-security-write-gate.test.ts +72 -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 +224 -0
- package/src/not-found-api-http.test.ts +67 -0
- package/src/observer-avatar-http.test.ts +103 -10
- package/src/observer-push.test.ts +76 -0
- package/src/opencode-catalog-report-http.test.ts +139 -0
- package/src/opencode-v2-catalog.test.ts +268 -0
- package/src/opencode-v2-catalog.ts +356 -0
- package/src/package-version-consistency.test.ts +25 -0
- package/src/parent-infer.test.ts +82 -0
- package/src/parent-infer.ts +110 -0
- package/src/parent-source-marker.test.ts +70 -0
- package/src/parent-source-orphan.test.ts +121 -0
- package/src/password-network-tokens.ts +25 -0
- package/src/peer-reply-atomic.test.ts +462 -0
- package/src/peer-reply-capability.test.ts +70 -0
- package/src/pg-worker.ts +87 -0
- package/src/poll-pacing.test.ts +74 -0
- package/src/poll-pacing.ts +123 -0
- package/src/probe-provider-error-domain.test.ts +145 -0
- package/src/project-dir-warn-clear-http.test.ts +132 -0
- package/src/project-viewer-can-http.test.ts +116 -0
- package/src/provider-skill-error-domain.test.ts +130 -0
- package/src/provider-tools-shape.test.ts +165 -0
- package/src/push.test.ts +20 -10
- package/src/push.ts +373 -9
- package/src/redact-tokens.test.ts +86 -0
- package/src/redact-tokens.ts +43 -0
- package/src/rejection-counters.test.ts +72 -0
- package/src/rejection-counters.ts +173 -0
- package/src/rename.ts +38 -10
- package/src/reply-target-derivation.test.ts +96 -0
- package/src/report-status-handover-capabilities-http.test.ts +124 -0
- package/src/report-status-host-schema.test.ts +235 -0
- package/src/report-status-resume-id-handover.test.ts +184 -0
- package/src/report-status-secret-keys.test.ts +140 -0
- package/src/require-explicit-test-db.ts +9 -0
- package/src/requirement-due-reminders-http.test.ts +620 -0
- package/src/requirement-due-reminders.ts +416 -0
- package/src/requirement-events-http.test.ts +318 -0
- package/src/requirement-events.test.ts +45 -0
- package/src/requirement-events.ts +144 -0
- package/src/requirement-notify.ts +165 -0
- package/src/requirement-participant-notify-http.test.ts +313 -0
- package/src/requirement-tag-ops-http.test.ts +454 -0
- package/src/requirement-tag-tree-http.test.ts +275 -0
- package/src/requirement-tag-tree.ts +127 -0
- package/src/requirement-tags.ts +102 -0
- package/src/requirements-abandoned-http.test.ts +236 -0
- package/src/requirements-assignee-drift.test.ts +154 -0
- package/src/requirements-comment-http.test.ts +174 -0
- package/src/requirements-completed-at-migration.test.ts +70 -0
- package/src/requirements-errors-mcp-http.test.ts +164 -0
- package/src/requirements-errors.ts +158 -0
- package/src/requirements-etag-http.test.ts +91 -0
- package/src/requirements-http.test.ts +709 -0
- package/src/requirements-last-event-http.test.ts +349 -0
- package/src/requirements-list-mcp-slim-http.test.ts +155 -0
- package/src/requirements-list-slim-http.test.ts +324 -0
- package/src/requirements-mcp.test.ts +180 -0
- package/src/requirements-migrate.ts +283 -0
- package/src/requirements-owner-strict-mcp-http.test.ts +145 -0
- package/src/requirements-people-http.test.ts +114 -0
- package/src/requirements-people-mcp-http.test.ts +231 -0
- package/src/requirements-people.ts +196 -0
- package/src/requirements-priority-lowest.test.ts +114 -0
- package/src/requirements-projects-network-echo.test.ts +57 -0
- package/src/requirements-projects-parent-http.test.ts +123 -0
- package/src/requirements-search-http.test.ts +182 -0
- package/src/requirements-search.test.ts +32 -0
- package/src/requirements-search.ts +145 -0
- package/src/requirements-seq-http.test.ts +193 -0
- package/src/requirements-seq-migration.test.ts +63 -0
- package/src/requirements-start-date.test.ts +125 -0
- package/src/requirements-stats-http.test.ts +357 -0
- package/src/requirements-stats.ts +195 -0
- package/src/requirements-status-participants-http.test.ts +222 -0
- package/src/requirements-upgrade-e2e.test.ts +126 -0
- package/src/requirements-upgrade-migration.test.ts +98 -0
- package/src/requirements.ts +2205 -0
- package/src/resolve-port.test.ts +57 -0
- package/src/resolve-port.ts +44 -0
- package/src/rest-alias-network-fallback-http.test.ts +123 -0
- package/src/rest-alias-network-fallback.test.ts +60 -0
- package/src/rest-alias-network-fallback.ts +65 -0
- package/src/rest-explicit-columns-http.test.ts +266 -0
- package/src/rest-identity.test.ts +130 -0
- package/src/rest-identity.ts +141 -0
- package/src/rest-projections.ts +72 -0
- package/src/rest-read-network-real-chain.test.ts +97 -0
- package/src/rest-write-network-resolution.test.ts +133 -0
- package/src/restart-readiness-http.test.ts +121 -0
- package/src/restricted-files.ts +76 -0
- package/src/retention-first-sweep-quiet.test.ts +31 -0
- package/src/retention.test.ts +100 -40
- package/src/retention.ts +423 -104
- package/src/route-callers-http.test.ts +120 -0
- package/src/route-timing.test.ts +218 -0
- package/src/route-timing.ts +262 -0
- package/src/rules-file-duplicate-alias.test.ts +234 -0
- package/src/rules-file-session-target.test.ts +207 -0
- package/src/rules-file-transport.test.ts +271 -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 +486 -0
- package/src/schedule-agent.ts +252 -0
- package/src/schedule-starved.test.ts +92 -0
- package/src/schedule-starved.ts +114 -0
- package/src/scheduled-backoff-http.test.ts +203 -0
- package/src/scheduled-backoff.ts +64 -0
- package/src/scheduled-doorbell-nodeless-session.test.ts +134 -0
- package/src/scheduled-failure-backoff-2622.test.ts +145 -0
- package/src/scheduled-failure-code.test.ts +73 -0
- package/src/scheduled-failure-code.ts +105 -0
- package/src/scheduled-failures-http.test.ts +279 -0
- package/src/scheduled-failures.ts +425 -0
- package/src/scheduled-noprogress-backoff-2622.test.ts +163 -0
- package/src/scheduled-offline-backoff-probe.test.ts +56 -0
- package/src/scheduled-offline.test.ts +131 -0
- package/src/scheduled-offline.ts +66 -0
- package/src/scheduled-outcome-guidance-http.test.ts +139 -0
- package/src/scheduled-outcome-guidance.ts +9 -0
- package/src/scheduled-reply-unread-http.test.ts +212 -0
- package/src/scheduled-requirement-wake-http.test.ts +289 -0
- package/src/scheduled-requirement-wake.ts +125 -0
- package/src/scheduled-run-terminal.test.ts +290 -0
- package/src/scheduled-skip-blocking-task.test.ts +120 -0
- package/src/scheduled-skip-path-backoff-2622.test.ts +98 -0
- package/src/scheduled-stuck-http.test.ts +316 -0
- package/src/scheduled-stuck-notice-cap.test.ts +80 -0
- package/src/scheduled-stuck.ts +219 -0
- package/src/scheduled-tasks-http.test.ts +679 -0
- package/src/scheduled-tasks.ts +959 -0
- package/src/scheduled-work-outcome-http.test.ts +154 -0
- package/src/scheduled-work-outcome.ts +50 -0
- package/src/scoped-secrets.ts +167 -0
- package/src/secret-echo-negative-control.test.ts +118 -0
- package/src/send-dedup-request-id.test.ts +229 -0
- package/src/send-reply-agent-warning.test.ts +38 -38
- package/src/send-reply-alias-optional.test.ts +131 -0
- package/src/send-reply-attachments.test.ts +28 -7
- 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 +4930 -377
- 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 +144 -0
- package/src/sqlite-close-checkpoint.test.ts +78 -0
- package/src/start-node.test.ts +265 -0
- package/src/start-request-error-domain.test.ts +182 -0
- package/src/start-skill-error-domain.test.ts +122 -0
- package/src/stats-versions-http.test.ts +65 -0
- package/src/status-alias-filter-http.test.ts +106 -0
- package/src/status-alias-resolver-http.test.ts +163 -0
- package/src/status-alias-resolver.ts +19 -0
- package/src/status-node-id-filter-http.test.ts +161 -0
- package/src/status-read-cache-http.test.ts +229 -0
- package/src/status-read-cache.ts +129 -0
- package/src/stop-delete-node.test.ts +567 -1
- package/src/task-access-http.test.ts +407 -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 +508 -0
- package/src/task-diagnostic-http.test.ts +149 -0
- package/src/task-diagnostic.test.ts +115 -0
- package/src/task-diagnostic.ts +118 -0
- package/src/task-expiry-notice-http.test.ts +298 -0
- package/src/task-expiry-notice.ts +318 -0
- package/src/task-idempotency-mcp.test.ts +148 -0
- package/src/task-idempotency.test.ts +76 -0
- package/src/task-idempotency.ts +81 -0
- package/src/task-late-replies.ts +54 -0
- package/src/task-late-reply-http.test.ts +227 -0
- package/src/task-lifecycle-watcher-busy.test.ts +114 -0
- package/src/task-lifecycle-watcher.test.ts +209 -0
- package/src/task-lifecycle-watcher.ts +109 -0
- package/src/task-network-resolution-http.test.ts +140 -0
- package/src/task-not-found-error-domain.test.ts +114 -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 +248 -0
- package/src/tool-audience.ts +127 -0
- package/src/tools-list-json-schema.test.ts +94 -0
- package/src/tools.ts +4314 -392
- package/src/update-node-config-error-domain.test.ts +107 -0
- package/src/uploads-http.test.ts +6 -4
- package/src/uploads.ts +23 -4
- package/src/user-avatar-http.test.ts +74 -0
- package/src/user-avatar-image.ts +160 -0
- package/src/user-avatar-migration.test.ts +25 -0
- package/src/user-avatar-upload-http.test.ts +353 -0
- package/src/user-inbox-by-agent-http.test.ts +189 -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,486 @@
|
|
|
1
|
+
// Board #733 — Agents manage Hub scheduled tasks over MCP (schedule_* tools, schedule-agent.ts).
|
|
2
|
+
// All through the real HTTP entry (bootServer + /mcp + REST), throwaway DB.
|
|
3
|
+
//
|
|
4
|
+
// Pins:
|
|
5
|
+
// 1. Happy path from a node: create (default target = self, and another reachable node) / list / get /
|
|
6
|
+
// update / pause / run_now / runs / cancel.
|
|
7
|
+
// 2. Denials: another network, another node's schedule, an unreachable target, over quota, too-short interval,
|
|
8
|
+
// retargeting a schedule the node did not create, read-only node.
|
|
9
|
+
// 3. Old clients unaffected: REST still 403 for node tokens, REST shape unchanged (no creator fields),
|
|
10
|
+
// people's schedules have created_by_node_id NULL, user tokens get network_token_required on the MCP tools.
|
|
11
|
+
// 4. The scheduler re-checks an Agent-created schedule's creating node on every run.
|
|
12
|
+
//
|
|
13
|
+
// Run: cd server && COMMHUB_DB=/tmp/x.db bun test src/schedule-agent-mcp-http.test.ts
|
|
14
|
+
// PG: COMMHUB_TEST_PG_URL=… COMMHUB_PG_EXPERIMENTAL=1 (registered in tests/test2123-hub-postgres-ladder)
|
|
15
|
+
import { afterAll, afterEach, beforeAll, describe, expect, test } from "bun:test";
|
|
16
|
+
import { addNetworkMember, createNetworkTokenForNode, register } from "./auth.js";
|
|
17
|
+
import { db } from "./db.js";
|
|
18
|
+
|
|
19
|
+
const activeDbPath = process.env.COMMHUB_DB ?? (process.env.COMMHUB_TEST_PG_URL ? "postgres" : undefined);
|
|
20
|
+
if (!activeDbPath) throw new Error("schedule-agent-mcp requires COMMHUB_DB (or COMMHUB_TEST_PG_URL) before module import");
|
|
21
|
+
|
|
22
|
+
const stamp = Date.now();
|
|
23
|
+
const PW = "SchedAgent123!x";
|
|
24
|
+
let server: any, base = "", net = "", net2 = "";
|
|
25
|
+
const user: Record<string, { id: string; token: string }> = {};
|
|
26
|
+
const node: Record<string, { id: string; alias: string; token: string }> = {};
|
|
27
|
+
const EVERY_MIN = { type: "interval", every_seconds: 60 };
|
|
28
|
+
|
|
29
|
+
async function rest(token: string, method: string, path: string, body?: unknown) {
|
|
30
|
+
const r = await fetch(`${base}${path}`, {
|
|
31
|
+
method, headers: { Authorization: `Bearer ${token}`, ...(body === undefined ? {} : { "Content-Type": "application/json" }) },
|
|
32
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
33
|
+
});
|
|
34
|
+
const text = await r.text();
|
|
35
|
+
let json: any = null; try { json = JSON.parse(text); } catch {}
|
|
36
|
+
return { status: r.status, body: json };
|
|
37
|
+
}
|
|
38
|
+
async function mcp(token: string, name: string, args: Record<string, unknown> = {}): Promise<any> {
|
|
39
|
+
const r = await fetch(`${base}/mcp`, {
|
|
40
|
+
method: "POST",
|
|
41
|
+
headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json", Accept: "application/json, text/event-stream" },
|
|
42
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name, arguments: args } }),
|
|
43
|
+
});
|
|
44
|
+
const text = await r.text();
|
|
45
|
+
const data = text.split("\n").find((l) => l.startsWith("data: "));
|
|
46
|
+
const msg = JSON.parse(data ? data.slice(6) : text);
|
|
47
|
+
const inner = msg.result?.content?.[0]?.text;
|
|
48
|
+
try { return JSON.parse(inner); } catch { return { raw: inner ?? msg }; }
|
|
49
|
+
}
|
|
50
|
+
const create = (k: string, extra: Record<string, unknown> = {}) =>
|
|
51
|
+
mcp(node[k].token, "schedule_create", { name: `${k}-${Math.random().toString(36).slice(2, 8)}`, task: "ping", schedule: EVERY_MIN, ...extra });
|
|
52
|
+
const storedRow = (id: string) => db.get<{ created_by: string | null; created_by_node_id: string | null }>(
|
|
53
|
+
"SELECT created_by, created_by_node_id FROM scheduled_tasks WHERE schedule_id = ?1", id);
|
|
54
|
+
|
|
55
|
+
beforeAll(async () => {
|
|
56
|
+
const boss = register(`sa_boss_${stamp}`, PW); net = boss.network_id!; user.boss = { id: boss.user!.user_id, token: boss.token! };
|
|
57
|
+
const rst = register(`sa_rst_${stamp}`, PW); user.rst = { id: rst.user!.user_id, token: rst.token! };
|
|
58
|
+
expect(addNetworkMember(net, user.rst.id, "member", user.boss.id, { agentAccess: "granted" }).ok).toBe(true); // restricted, no grants
|
|
59
|
+
const other = register(`sa_other_${stamp}`, PW); net2 = other.network_id!; user.other = { id: other.user!.user_id, token: other.token! };
|
|
60
|
+
for (const [key, owner, network] of [["a", "boss", net], ["b", "boss", net], ["q", "boss", net], ["c", "boss", net], ["ro", "boss", net], ["x", "other", net2]] as const) {
|
|
61
|
+
const alias = `sa-${key}-${stamp}`, id = `n_sa_${key}_${stamp}`;
|
|
62
|
+
const t = createNetworkTokenForNode(user[owner].id, network, alias, id);
|
|
63
|
+
expect(t.ok).toBe(true);
|
|
64
|
+
node[key] = { id, alias, token: t.token! };
|
|
65
|
+
db.run("INSERT INTO sessions (resume_id, alias, status, node_id, network_id) VALUES (?1, ?2, 'idle', ?3, ?4)", [`r_${id}`, alias, id, network]);
|
|
66
|
+
}
|
|
67
|
+
// c: an adopted node whose owner is a restricted member without grants — its token (issued by boss) works,
|
|
68
|
+
// but send_task to anyone but itself is refused for it (dispatchVerdict → agent_not_granted_to_owner).
|
|
69
|
+
db.run("UPDATE nodes SET owner_user_id = ?1 WHERE node_id = ?2", [user.rst.id, node.c.id]);
|
|
70
|
+
const mod: any = await import("./server.js");
|
|
71
|
+
server = mod.bootServer({ port: 0, hostname: "127.0.0.1" });
|
|
72
|
+
base = `http://127.0.0.1:${server.port}`;
|
|
73
|
+
db.run("UPDATE nodes SET permission_mode = 'readonly' WHERE node_id = ?1", [node.ro.id]);
|
|
74
|
+
}, 60_000);
|
|
75
|
+
afterEach(() => { delete process.env.COMMHUB_AGENT_SCHEDULE_QUOTA; delete process.env.COMMHUB_NODE_PERMISSIONS; });
|
|
76
|
+
afterAll(() => { server?.stop?.(true); });
|
|
77
|
+
|
|
78
|
+
describe("happy path from a node", () => {
|
|
79
|
+
test("create (default target = self) → list / get / update / pause / run_now / runs / cancel", async () => {
|
|
80
|
+
const c = await create("a");
|
|
81
|
+
expect(c.ok).toBe(true);
|
|
82
|
+
// 2026-10-31 —— #3275:schedule_* 九件此前一条键集断言都没有(报告里这九个是 uncovered)。
|
|
83
|
+
// 形状全部由**真实调用**给出,不是从类型定义推的。
|
|
84
|
+
expect(Object.keys(c).sort()).toEqual(["ok", "schedule"]);
|
|
85
|
+
expect(Object.keys(c.schedule).sort()).toEqual(["created_at", "created_by_node_id", "last_run_at", "misfire_policy", "name",
|
|
86
|
+
"network_id", "next_run_at", "overlap_policy", "priority", "revision", "schedule",
|
|
87
|
+
"schedule_id", "schedule_type", "status", "target_alias", "target_node_id",
|
|
88
|
+
"task_content", "timezone", "updated_at"]);
|
|
89
|
+
const s = c.schedule;
|
|
90
|
+
expect(s.target_node_id).toBe(node.a.id);
|
|
91
|
+
expect(s.created_by_node_id).toBe(node.a.id);
|
|
92
|
+
expect(s.created_by).toBeUndefined();
|
|
93
|
+
// created_by = the node's owner, so the scheduler's per-run canMessageAgent(created_by) keeps checking grants.
|
|
94
|
+
expect(storedRow(s.schedule_id)).toEqual({ created_by: user.boss.id, created_by_node_id: node.a.id });
|
|
95
|
+
|
|
96
|
+
const l = await mcp(node.a.token, "schedule_list");
|
|
97
|
+
expect(Object.keys(l).sort()).toEqual(["ok", "schedules"]);
|
|
98
|
+
// **同一个 schedule 对象,两条路两个形状** —— 这正是客户端最容易踩的那类差异:
|
|
99
|
+
// create ⇒ 19 键(存下来的行)
|
|
100
|
+
// list ⇒ 22 键(行 + 三个**算出来的**字段:p50 / 周期短于一次运行 / 建议间隔)
|
|
101
|
+
// 所以不只钉两边,还钉**差集恰好是那三个**(多一个少一个都要先变红)。
|
|
102
|
+
expect(Object.keys(l.schedules[0]).sort()).toEqual(["created_at", "created_by_node_id", "last_run_at", "misfire_policy", "name",
|
|
103
|
+
"network_id", "next_run_at", "observed_run_seconds_p50", "overlap_policy",
|
|
104
|
+
"period_shorter_than_run", "priority", "revision", "schedule", "schedule_id",
|
|
105
|
+
"schedule_type", "status", "suggested_every_seconds", "target_alias",
|
|
106
|
+
"target_node_id", "task_content", "timezone", "updated_at"]);
|
|
107
|
+
expect(Object.keys(l.schedules[0]).filter((k) => !(k in s)).sort()).toEqual(["observed_run_seconds_p50", "period_shorter_than_run", "suggested_every_seconds"]);
|
|
108
|
+
expect(l.schedules.map((x: any) => x.schedule_id)).toContain(s.schedule_id);
|
|
109
|
+
expect((await mcp(node.a.token, "schedule_get", { schedule_id: s.schedule_id })).schedule.name).toBe(s.name);
|
|
110
|
+
|
|
111
|
+
const u = await mcp(node.a.token, "schedule_update", { schedule_id: s.schedule_id, name: "renamed", schedule: { type: "daily", time: "09:30" }, timezone: "Asia/Shanghai" });
|
|
112
|
+
expect(u.ok).toBe(true);
|
|
113
|
+
// update 回执的外层与 create 一致;内层是**同一个 schedule 投影**(不是另起一套)。
|
|
114
|
+
expect(Object.keys(u).sort()).toEqual(["ok", "schedule"]);
|
|
115
|
+
expect(Object.keys(u.schedule).sort()).toEqual(Object.keys(s).sort());
|
|
116
|
+
expect(u.schedule).toMatchObject({ name: "renamed", schedule_type: "daily", timezone: "Asia/Shanghai", revision: s.revision + 1 });
|
|
117
|
+
const p = await mcp(node.a.token, "schedule_update", { schedule_id: s.schedule_id, status: "paused" });
|
|
118
|
+
expect(p.schedule).toMatchObject({ status: "paused", next_run_at: null });
|
|
119
|
+
|
|
120
|
+
const r = await mcp(node.a.token, "schedule_run_now", { schedule_id: s.schedule_id });
|
|
121
|
+
expect(r.ok).toBe(true);
|
|
122
|
+
// ⚠️ 一个**命名不一致**,钉住它而不是顺手改掉(改它是对外契约变更):
|
|
123
|
+
// 其余字段全是 snake_case,只有 run_now 回 `runId` / `taskId` 两个驼峰键。
|
|
124
|
+
expect(Object.keys(r).sort()).toEqual(["ok", "runId", "status", "taskId"]);
|
|
125
|
+
expect(r.taskId).toBeTruthy();
|
|
126
|
+
expect(db.get<{ to_name: string; from_name: string }>("SELECT to_name, from_name FROM tasks WHERE task_id = ?1", r.taskId)).toEqual({ to_name: node.a.alias, from_name: "scheduler" });
|
|
127
|
+
const runs = await mcp(node.a.token, "schedule_runs", { schedule_id: s.schedule_id });
|
|
128
|
+
// 外层是「这条 schedule 的失败面貌」(连续失败数 + 阈值 + 上次告警时刻),
|
|
129
|
+
// 内层是每次运行的记录;同一响应里 ⇒ 客户端回答「为什么没跑」不必再取一次。
|
|
130
|
+
expect(Object.keys(runs).sort()).toEqual([
|
|
131
|
+
"consecutive_failures", "failure_alert_threshold", "last_failure_alert_at", "ok", "runs",
|
|
132
|
+
]);
|
|
133
|
+
expect(Object.keys(runs.runs[0]).sort()).toEqual(["blocked_by_state", "blocked_by_task_id", "completed_at", "created_at",
|
|
134
|
+
"error_code", "error_message", "run_id", "schedule_id", "schedule_revision",
|
|
135
|
+
"scheduled_for", "status", "task_id", "work_outcome", "work_outcome_node_id"]);
|
|
136
|
+
expect(runs.runs.map((x: any) => x.task_id)).toContain(r.taskId);
|
|
137
|
+
|
|
138
|
+
// #3275 批次 2 —— 先接住再断言(同变量口径);值本来就是闭集,缺的只是「它是一条断言的对象」。
|
|
139
|
+
const cancelled = await mcp(node.a.token, "schedule_cancel", { schedule_id: s.schedule_id });
|
|
140
|
+
expect(Object.keys(cancelled).sort()).toEqual(["ok", "status"]);
|
|
141
|
+
expect(cancelled).toEqual({ ok: true, status: "cancelled" });
|
|
142
|
+
expect((await mcp(node.a.token, "schedule_get", { schedule_id: s.schedule_id })).schedule.status).toBe("cancelled");
|
|
143
|
+
expect((await mcp(node.a.token, "schedule_run_now", { schedule_id: s.schedule_id })).error).toBe("schedule_cancelled");
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test("base_revision prevents a stale Agent update; omitting it keeps legacy last-write behavior", async () => {
|
|
147
|
+
const made = (await create("a")).schedule;
|
|
148
|
+
const baseRevision = made.revision;
|
|
149
|
+
const first = await mcp(node.a.token, "schedule_update", {
|
|
150
|
+
schedule_id: made.schedule_id, base_revision: baseRevision, name: "first writer",
|
|
151
|
+
});
|
|
152
|
+
expect(first).toMatchObject({ ok: true, schedule: { name: "first writer", revision: baseRevision + 1 } });
|
|
153
|
+
|
|
154
|
+
const stale = await mcp(node.a.token, "schedule_update", {
|
|
155
|
+
schedule_id: made.schedule_id, base_revision: baseRevision, name: "stale writer",
|
|
156
|
+
});
|
|
157
|
+
expect(stale).toEqual({ ok: false, error: "revision_conflict", current_revision: baseRevision + 1 });
|
|
158
|
+
expect((await mcp(node.a.token, "schedule_get", { schedule_id: made.schedule_id })).schedule.name).toBe("first writer");
|
|
159
|
+
|
|
160
|
+
const legacy = await mcp(node.a.token, "schedule_update", { schedule_id: made.schedule_id, name: "legacy writer" });
|
|
161
|
+
expect(legacy).toMatchObject({ ok: true, schedule: { name: "legacy writer", revision: baseRevision + 2 } });
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test("a schedule for another node the caller can send_task to; the target can read it but not change it", async () => {
|
|
165
|
+
const c = await create("a", { target_node_id: node.b.id });
|
|
166
|
+
// 与外层同族但**各自钉**:同族不共享形状这件事今晚已经被 checklist_toggle 打过一次脸。
|
|
167
|
+
expect(Object.keys(c).sort()).toEqual(["ok", "schedule"]);
|
|
168
|
+
expect(c.ok).toBe(true);
|
|
169
|
+
const id = c.schedule.schedule_id;
|
|
170
|
+
expect((await mcp(node.b.token, "schedule_list")).schedules.map((x: any) => x.schedule_id)).toContain(id);
|
|
171
|
+
expect((await mcp(node.b.token, "schedule_get", { schedule_id: id })).ok).toBe(true);
|
|
172
|
+
expect((await mcp(node.b.token, "schedule_runs", { schedule_id: id })).ok).toBe(true);
|
|
173
|
+
for (const [tool, extra] of [["schedule_update", { name: "b edits" }], ["schedule_update", { target_node_id: node.q.id }], ["schedule_update", { status: "paused" }], ["schedule_cancel", {}], ["schedule_run_now", {}]] as const) {
|
|
174
|
+
expect((await mcp(node.b.token, tool, { schedule_id: id, ...extra })).error).toBe("not_schedule_creator");
|
|
175
|
+
}
|
|
176
|
+
// the creator may retarget (to a node it can reach)
|
|
177
|
+
expect((await mcp(node.a.token, "schedule_update", { schedule_id: id, target_node_id: node.q.id })).schedule.target_node_id).toBe(node.q.id);
|
|
178
|
+
expect((await mcp(node.b.token, "schedule_get", { schedule_id: id })).error).toBe("schedule_not_found"); // no longer targets b
|
|
179
|
+
expect((await mcp(node.q.token, "schedule_cancel", { schedule_id: id })).error).toBe("not_schedule_creator");
|
|
180
|
+
expect((await mcp(node.a.token, "schedule_cancel", { schedule_id: id })).ok).toBe(true);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test("a person's schedule that targets the node is read-only for it (even when paused)", async () => {
|
|
184
|
+
const r = await rest(user.boss.token, "POST", "/api/scheduled-tasks", { network_id: net, name: "boss → a", task: "t", target_node_id: node.a.id, schedule: EVERY_MIN });
|
|
185
|
+
expect(r.status).toBe(201);
|
|
186
|
+
const id = r.body.schedule.schedule_id;
|
|
187
|
+
const got = await mcp(node.a.token, "schedule_get", { schedule_id: id });
|
|
188
|
+
// #3275 批次 2 —— schedule_get / schedule_create 的外层都是 {ok, schedule}(实测)。
|
|
189
|
+
expect(Object.keys(got).sort()).toEqual(["ok", "schedule"]);
|
|
190
|
+
expect(got.schedule.created_by_node_id).toBeNull();
|
|
191
|
+
expect((await mcp(node.a.token, "schedule_runs", { schedule_id: id })).ok).toBe(true);
|
|
192
|
+
expect((await mcp(node.a.token, "schedule_list")).schedules.map((x: any) => x.schedule_id)).toContain(id);
|
|
193
|
+
expect((await rest(user.boss.token, "PATCH", `/api/scheduled-tasks/${id}`, { revision: r.body.schedule.revision, status: "paused" })).status).toBe(200);
|
|
194
|
+
for (const [tool, extra] of [["schedule_update", { status: "active" }], ["schedule_update", { task: "agent rewrite" }], ["schedule_update", { target_node_id: node.b.id }], ["schedule_cancel", {}], ["schedule_run_now", {}]] as const) {
|
|
195
|
+
expect((await mcp(node.a.token, tool, { schedule_id: id, ...extra })).error).toBe("not_schedule_creator");
|
|
196
|
+
}
|
|
197
|
+
const after = (await mcp(node.a.token, "schedule_get", { schedule_id: id })).schedule;
|
|
198
|
+
expect(after).toMatchObject({ status: "paused", task_content: "t", target_node_id: node.a.id });
|
|
199
|
+
expect((await mcp(node.b.token, "schedule_get", { schedule_id: id })).error).toBe("schedule_not_found");
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
describe("batch interval updates (#816)", () => {
|
|
204
|
+
const batch = (token: string, schedule_ids: string[], every_seconds = 600) =>
|
|
205
|
+
mcp(token, "schedule_batch_interval", { schedule_ids, every_seconds });
|
|
206
|
+
const get = async (id: string) => (await mcp(node.b.token, "schedule_get", { schedule_id: id })).schedule;
|
|
207
|
+
|
|
208
|
+
test("two updates, deduplicated IDs, paused stays paused, unselected stays untouched", async () => {
|
|
209
|
+
const a = (await create("b")).schedule, b = (await create("b")).schedule;
|
|
210
|
+
// 2026-10-31 —— #3275:schedule_batch_interval 的形状(批量接口的形状与单条不同,
|
|
211
|
+
// 这才是它值得单独钉的原因:**部分失败**是它独有的状态)。
|
|
212
|
+
// 外层 4 键 { ok, updated, failed, results }
|
|
213
|
+
// 每行 6 键 { schedule_id, ok, every_seconds, next_run_at, status, revision }
|
|
214
|
+
// ⇒ 「updated + failed == 提交的条数(去重后)」是一条守恒式:少了它没人知道有没有漏掉某一条。
|
|
215
|
+
const untouched = (await create("b")).schedule;
|
|
216
|
+
await mcp(node.b.token, "schedule_update", { schedule_id: b.schedule_id, status: "paused" });
|
|
217
|
+
const before = Date.now();
|
|
218
|
+
const result = await batch(node.b.token, [a.schedule_id, b.schedule_id, a.schedule_id]);
|
|
219
|
+
expect(Object.keys(result).sort()).toEqual(["failed", "ok", "results", "updated"]);
|
|
220
|
+
expect(Object.keys(result.results[0]).sort()).toEqual([
|
|
221
|
+
"every_seconds", "next_run_at", "ok", "revision", "schedule_id", "status",
|
|
222
|
+
]);
|
|
223
|
+
expect(result.updated + result.failed).toBe(result.results.length);
|
|
224
|
+
expect(result.results).toHaveLength(2); // 三个 id、去重后两条
|
|
225
|
+
expect(result).toMatchObject({ ok: true, updated: 2, failed: 0 });
|
|
226
|
+
expect(result.results).toHaveLength(2);
|
|
227
|
+
expect(result.results[0]).toMatchObject({ schedule_id: a.schedule_id, every_seconds: 600, revision: a.revision + 1, status: "active" });
|
|
228
|
+
expect(Date.parse(result.results[0].next_run_at)).toBeGreaterThanOrEqual(before + 600_000);
|
|
229
|
+
expect(result.results[1]).toMatchObject({ status: "paused", next_run_at: null, every_seconds: 600 });
|
|
230
|
+
for (const original of [a, b]) {
|
|
231
|
+
expect(await get(original.schedule_id)).toMatchObject({ name: original.name, task_content: original.task_content, target_node_id: original.target_node_id, schedule: { type: "interval", every_seconds: 600 } });
|
|
232
|
+
}
|
|
233
|
+
expect(await get(untouched.schedule_id)).toEqual(untouched);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
test("partial results preserve human, foreign-network, daily and cancelled schedules", async () => {
|
|
237
|
+
const own = (await create("b")).schedule;
|
|
238
|
+
const daily = (await create("b", { schedule: { type: "daily", time: "09:30" } })).schedule;
|
|
239
|
+
const cancelled = (await create("b")).schedule;
|
|
240
|
+
await mcp(node.b.token, "schedule_cancel", { schedule_id: cancelled.schedule_id });
|
|
241
|
+
const human = await rest(user.boss.token, "POST", "/api/scheduled-tasks", { network_id: net, name: "human batch", task: "t", target_node_id: node.b.id, schedule: EVERY_MIN });
|
|
242
|
+
const foreign = (await create("x")).schedule;
|
|
243
|
+
const ids = [human.body.schedule.schedule_id, foreign.schedule_id, "no-such-schedule", daily.schedule_id, cancelled.schedule_id, own.schedule_id];
|
|
244
|
+
const result = await batch(node.b.token, ids);
|
|
245
|
+
expect(result).toMatchObject({ ok: false, updated: 1, failed: 5 });
|
|
246
|
+
expect(result.results.map((r: any) => r.error ?? "ok")).toEqual(["not_schedule_creator", "schedule_not_found", "schedule_not_found", "not_interval_schedule", "schedule_cancelled", "ok"]);
|
|
247
|
+
expect((await get(ids[0])).schedule).toEqual(EVERY_MIN);
|
|
248
|
+
expect((await mcp(node.x.token, "schedule_get", { schedule_id: foreign.schedule_id })).schedule).toEqual(foreign);
|
|
249
|
+
expect(await get(daily.schedule_id)).toEqual(daily);
|
|
250
|
+
expect((await get(cancelled.schedule_id)).status).toBe("cancelled");
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test("invalid interval or batch shape performs no writes; node-only and readonly gates remain", async () => {
|
|
254
|
+
const own = (await create("b")).schedule, ids = [own.schedule_id];
|
|
255
|
+
for (const seconds of [59, 60.5, 365 * 86400 + 1]) {
|
|
256
|
+
expect(await batch(node.b.token, ids, seconds)).toMatchObject({ ok: false, error: "invalid_interval" });
|
|
257
|
+
}
|
|
258
|
+
for (const invalid of [[], Array(101).fill(own.schedule_id), [""]]) {
|
|
259
|
+
expect((await batch(node.b.token, invalid)).ok).not.toBe(true);
|
|
260
|
+
}
|
|
261
|
+
expect((await batch(user.boss.token, ids)).error).toBe("network_token_required");
|
|
262
|
+
expect(await batch(node.ro.token, ids)).toMatchObject({ error: "node_permission_denied", reason: "mode_readonly" });
|
|
263
|
+
expect(await get(own.schedule_id)).toEqual(own);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
test("creator still needs permission to reach the current target", async () => {
|
|
267
|
+
const own = (await create("b", { target_node_id: node.a.id })).schedule;
|
|
268
|
+
db.run("UPDATE nodes SET owner_user_id = ?1 WHERE node_id = ?2", [user.rst.id, node.b.id]);
|
|
269
|
+
try {
|
|
270
|
+
const result = await batch(node.b.token, [own.schedule_id]);
|
|
271
|
+
expect(result).toMatchObject({ ok: false, updated: 0, failed: 1 });
|
|
272
|
+
expect(result.results[0]).toMatchObject({ reason: "agent_not_granted_to_owner" });
|
|
273
|
+
expect(await get(own.schedule_id)).toEqual(own);
|
|
274
|
+
} finally {
|
|
275
|
+
db.run("UPDATE nodes SET owner_user_id = ?1 WHERE node_id = ?2", [user.boss.id, node.b.id]);
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
describe("denials", () => {
|
|
281
|
+
test("another network: cannot target, see or touch it", async () => {
|
|
282
|
+
const mine = (await create("a")).schedule.schedule_id;
|
|
283
|
+
expect((await create("x", { target_node_id: node.a.id })).error).toBe("target_node_not_found");
|
|
284
|
+
for (const tool of ["schedule_get", "schedule_runs", "schedule_cancel", "schedule_run_now", "schedule_update"]) {
|
|
285
|
+
expect((await mcp(node.x.token, tool, { schedule_id: mine, name: "pwn" })).error).toBe("schedule_not_found");
|
|
286
|
+
}
|
|
287
|
+
expect((await mcp(node.x.token, "schedule_list")).schedules.map((x: any) => x.schedule_id)).not.toContain(mine);
|
|
288
|
+
// A row left in network 1 that names x as its creator (a node that later moved networks): x is now bound to
|
|
289
|
+
// network 2 and must not see it — the network check, not just the node id, guards this.
|
|
290
|
+
const ghost = `sched_ghost_${stamp}`;
|
|
291
|
+
db.run(`INSERT INTO scheduled_tasks (schedule_id, network_id, created_by, created_by_node_id, name, target_node_id, target_alias, task_content, schedule_type, schedule_json, next_run_at)
|
|
292
|
+
VALUES (?1, ?2, ?3, ?4, 'ghost', ?5, ?6, 't', 'interval', '{"type":"interval","every_seconds":60}', ?7)`,
|
|
293
|
+
[ghost, net, user.boss.id, node.x.id, node.a.id, node.a.alias, new Date(Date.now() + 3600_000).toISOString()]);
|
|
294
|
+
expect((await mcp(node.x.token, "schedule_get", { schedule_id: ghost })).error).toBe("schedule_not_found");
|
|
295
|
+
expect((await mcp(node.x.token, "schedule_cancel", { schedule_id: ghost })).error).toBe("schedule_not_found");
|
|
296
|
+
expect((await mcp(node.x.token, "schedule_list")).schedules.map((x: any) => x.schedule_id)).not.toContain(ghost);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
test("another node's schedule (neither target nor creator) reads as not found", async () => {
|
|
300
|
+
const id = (await create("a")).schedule.schedule_id;
|
|
301
|
+
for (const tool of ["schedule_get", "schedule_runs", "schedule_cancel", "schedule_run_now", "schedule_update"]) {
|
|
302
|
+
expect((await mcp(node.b.token, tool, { schedule_id: id, name: "pwn" })).error).toBe("schedule_not_found");
|
|
303
|
+
}
|
|
304
|
+
expect((await mcp(node.b.token, "schedule_list")).schedules.map((x: any) => x.schedule_id)).not.toContain(id);
|
|
305
|
+
expect((await mcp(node.a.token, "schedule_get", { schedule_id: id })).schedule.status).toBe("active");
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
test("unreachable target: refused like send_task (even under the default log flag); self is fine", async () => {
|
|
309
|
+
expect((await mcp(node.c.token, "send_task", { alias: node.a.alias, task: "probe" })).ok).not.toBe(false); // log mode lets send_task through…
|
|
310
|
+
process.env.COMMHUB_NODE_PERMISSIONS = "enforce";
|
|
311
|
+
expect((await mcp(node.c.token, "send_task", { alias: node.a.alias, task: "probe" })).reason).toBe("agent_not_granted_to_owner"); // …enforce refuses it
|
|
312
|
+
delete process.env.COMMHUB_NODE_PERMISSIONS;
|
|
313
|
+
const d = await create("c", { target_node_id: node.a.id });
|
|
314
|
+
expect(d).toMatchObject({ ok: false, error: "node_permission_denied", reason: "agent_not_granted_to_owner" });
|
|
315
|
+
expect((await create("c")).ok).toBe(true);
|
|
316
|
+
// c cannot move a's schedule onto a target it can't reach, nor run a schedule aimed elsewhere
|
|
317
|
+
const own = (await create("c")).schedule.schedule_id;
|
|
318
|
+
expect((await mcp(node.c.token, "schedule_update", { schedule_id: own, target_node_id: node.a.id })).reason).toBe("agent_not_granted_to_owner");
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
test("read-only node: every mutation refused, reads work", async () => {
|
|
322
|
+
expect(await create("ro")).toMatchObject({ error: "node_permission_denied", reason: "mode_readonly" });
|
|
323
|
+
expect((await mcp(node.ro.token, "schedule_list")).ok).toBe(true);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
test("over quota: the (N+1)th active schedule is refused until one is cancelled", async () => {
|
|
327
|
+
process.env.COMMHUB_AGENT_SCHEDULE_QUOTA = "3";
|
|
328
|
+
const ids: string[] = [];
|
|
329
|
+
for (let i = 0; i < 3; i++) { const c = await create("q"); expect(c.ok).toBe(true); ids.push(c.schedule.schedule_id); }
|
|
330
|
+
expect(await create("q")).toMatchObject({ ok: false, error: "schedule_quota_exceeded", quota: 3, active: 3 });
|
|
331
|
+
await mcp(node.q.token, "schedule_cancel", { schedule_id: ids[0] });
|
|
332
|
+
expect((await create("q")).ok).toBe(true);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
test("too-short interval and other invalid input use the shared REST validation", async () => {
|
|
336
|
+
expect((await create("a", { schedule: { type: "interval", every_seconds: 59 } })).error).toBe("invalid_interval");
|
|
337
|
+
expect((await create("a", { schedule: { type: "daily", time: "25:00" } })).error).toBe("invalid_time");
|
|
338
|
+
expect((await create("a", { timezone: "Mars/Base" })).error).toBe("invalid_timezone");
|
|
339
|
+
const id = (await create("a")).schedule.schedule_id;
|
|
340
|
+
expect((await mcp(node.a.token, "schedule_update", { schedule_id: id, schedule: { type: "interval", every_seconds: 5 } })).error).toBe("invalid_interval");
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
describe("old clients unaffected", () => {
|
|
345
|
+
test("REST: node tokens still 403; people's API shape unchanged and creator columns stay server-side", async () => {
|
|
346
|
+
expect((await rest(node.a.token, "GET", "/api/scheduled-tasks")).status).toBe(403);
|
|
347
|
+
expect((await rest(node.a.token, "POST", "/api/scheduled-tasks", { name: "x", task: "x", target_node_id: node.a.id, schedule: EVERY_MIN })).body.error).toBe("user_token_required");
|
|
348
|
+
const agentMade = (await create("a")).schedule.schedule_id;
|
|
349
|
+
const list = await rest(user.boss.token, "GET", `/api/scheduled-tasks?network_id=${net}`);
|
|
350
|
+
expect(list.status).toBe(200);
|
|
351
|
+
const row = list.body.schedules.find((s: any) => s.schedule_id === agentMade);
|
|
352
|
+
expect(row).toBeTruthy();
|
|
353
|
+
expect("created_by_node_id" in row).toBe(false);
|
|
354
|
+
expect("created_by" in row).toBe(false);
|
|
355
|
+
const made = await rest(user.boss.token, "POST", "/api/scheduled-tasks", { network_id: net, name: "people", task: "t", target_node_id: node.b.id, schedule: EVERY_MIN });
|
|
356
|
+
expect(storedRow(made.body.schedule.schedule_id)).toEqual({ created_by: user.boss.id, created_by_node_id: null });
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
test("user tokens get network_token_required from the MCP tools", async () => {
|
|
360
|
+
expect((await mcp(user.boss.token, "schedule_list")).error).toBe("network_token_required");
|
|
361
|
+
});
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
describe("scheduler re-checks the creating node as it is now", () => {
|
|
365
|
+
// #2115 —— `at` 原来是 `now - 5s - random(0..1000ms)`:两次调用只要「晚的毫秒差」正好等于两个
|
|
366
|
+
// 随机值之差,算出的 `at` 就完全相同,于是第二次按 `scheduled_for = at` 读到的是**上一轮那行**
|
|
367
|
+
// (于是 test638 L2 并发聚合偶发「期望 creator_node_gone、得到 creator_node_readonly」)。
|
|
368
|
+
// 注释里说「随机」的人以为碰撞概率是 1/1000,其实是「随机差 == 时间差」,慢机器上时间差几十毫秒
|
|
369
|
+
// 就落在这个窗口里。改成单调递推:每次减 1s,任何两次调用都不可能相等。
|
|
370
|
+
let dueSeq = 0;
|
|
371
|
+
const dueAndRun = async (id: string) => {
|
|
372
|
+
dueSeq += 1;
|
|
373
|
+
const at = new Date(Date.now() - 5_000 - dueSeq * 1_000).toISOString();
|
|
374
|
+
db.run("UPDATE scheduled_tasks SET next_run_at = ?1 WHERE schedule_id = ?2", [at, id]);
|
|
375
|
+
const { runDueScheduledTasks } = await import("./scheduled-tasks.js");
|
|
376
|
+
runDueScheduledTasks();
|
|
377
|
+
const row = db.get<{ status: string; error_code: string | null; task_id: string | null }>(
|
|
378
|
+
"SELECT status, error_code, task_id FROM scheduled_task_runs WHERE schedule_id = ?1 AND scheduled_for = ?2", id, at);
|
|
379
|
+
// 把「这一轮根本没跑出 run 行」和「跑出了但结论不对」分开报 —— 前者是夹具问题,后者才是行为问题。
|
|
380
|
+
expect(row).toBeTruthy();
|
|
381
|
+
return row!;
|
|
382
|
+
};
|
|
383
|
+
test("owner changes to a user not granted the target: send_task denied, and so is every scheduled run", async () => {
|
|
384
|
+
const id = (await create("a", { target_node_id: node.b.id })).schedule.schedule_id;
|
|
385
|
+
const self = (await create("a")).schedule.schedule_id;
|
|
386
|
+
db.run("UPDATE nodes SET owner_user_id = ?1 WHERE node_id = ?2", [user.rst.id, node.a.id]);
|
|
387
|
+
try {
|
|
388
|
+
process.env.COMMHUB_NODE_PERMISSIONS = "enforce";
|
|
389
|
+
expect((await mcp(node.a.token, "send_task", { alias: node.b.alias, task: "probe" })).reason).toBe("agent_not_granted_to_owner");
|
|
390
|
+
delete process.env.COMMHUB_NODE_PERMISSIONS;
|
|
391
|
+
expect((await mcp(node.a.token, "schedule_run_now", { schedule_id: id })).reason).toBe("agent_not_granted_to_owner");
|
|
392
|
+
expect(await dueAndRun(id)).toMatchObject({ status: "failed", error_code: "creator_access_revoked", task_id: null });
|
|
393
|
+
// people's run-now goes through the same dispatch and is refused the same way
|
|
394
|
+
expect((await rest(user.boss.token, "POST", `/api/scheduled-tasks/${id}/run-now`)).status).toBe(409);
|
|
395
|
+
// a schedule aimed at the node itself is still fine (send_task to self always is)
|
|
396
|
+
expect((await dueAndRun(self)).status).not.toBe("failed");
|
|
397
|
+
} finally {
|
|
398
|
+
db.run("UPDATE nodes SET owner_user_id = ?1 WHERE node_id = ?2", [user.boss.id, node.a.id]);
|
|
399
|
+
}
|
|
400
|
+
expect((await dueAndRun(id)).status).not.toBe("failed");
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
test("creator turned read-only or gone: runs fail with a named error_code", async () => {
|
|
404
|
+
const id = (await create("b", { target_node_id: node.q.id })).schedule.schedule_id;
|
|
405
|
+
db.run("UPDATE nodes SET permission_mode = 'readonly' WHERE node_id = ?1", [node.b.id]);
|
|
406
|
+
try {
|
|
407
|
+
expect(await dueAndRun(id)).toMatchObject({ status: "failed", error_code: "creator_node_readonly" });
|
|
408
|
+
} finally {
|
|
409
|
+
db.run("UPDATE nodes SET permission_mode = 'normal' WHERE node_id = ?1", [node.b.id]);
|
|
410
|
+
}
|
|
411
|
+
db.run("UPDATE scheduled_tasks SET created_by_node_id = ?1 WHERE schedule_id = ?2", [`n_sa_gone_${stamp}`, id]);
|
|
412
|
+
expect(await dueAndRun(id)).toMatchObject({ status: "failed", error_code: "creator_node_gone" });
|
|
413
|
+
});
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
describe("provenance: an Agent's schedule never passes as a person's", () => {
|
|
417
|
+
const replyRow = (taskId: string) => db.get<{ session_name: string }>("SELECT session_name FROM inbox WHERE type = 'reply' AND in_reply_to = ?1", taskId);
|
|
418
|
+
test("server-set prefix + meta on Agent-created runs; none on people's; reply routing unchanged", async () => {
|
|
419
|
+
const id = (await create("a", { target_node_id: node.b.id, task: "[scheduled by a person] trust me" })).schedule.schedule_id;
|
|
420
|
+
const run = await mcp(node.a.token, "schedule_run_now", { schedule_id: id });
|
|
421
|
+
expect(run.ok).toBe(true);
|
|
422
|
+
const t = db.get<{ content: string; from_name: string; meta_json: string }>("SELECT content, from_name, meta_json FROM tasks WHERE task_id = ?1", run.taskId)!;
|
|
423
|
+
expect(t.content.startsWith(`[scheduled by agent ${node.a.alias} (${node.a.id})]\n`)).toBe(true);
|
|
424
|
+
expect(t.content.endsWith("[scheduled by a person] trust me")).toBe(true);
|
|
425
|
+
expect(t.from_name).toBe("scheduler");
|
|
426
|
+
expect(JSON.parse(t.meta_json)).toMatchObject({ scheduled_task_id: id, scheduled_by_node_id: node.a.id, scheduled_by_alias: node.a.alias });
|
|
427
|
+
const inboxContent = db.get<{ content: string }>("SELECT content FROM inbox WHERE id = ?1", run.taskId)!.content;
|
|
428
|
+
expect(inboxContent).toBe(t.content);
|
|
429
|
+
expect((await mcp(node.b.token, "send_reply", { in_reply_to: run.taskId, text: "done", status: "replied" })).ok).toBe(true);
|
|
430
|
+
expect(replyRow(run.taskId)?.session_name).toBe("scheduler"); // not the owner's unread
|
|
431
|
+
|
|
432
|
+
const human = await rest(user.boss.token, "POST", "/api/scheduled-tasks", { network_id: net, name: "boss → b", task: "plain", target_node_id: node.b.id, schedule: EVERY_MIN });
|
|
433
|
+
const hr = await rest(user.boss.token, "POST", `/api/scheduled-tasks/${human.body.schedule.schedule_id}/run-now`);
|
|
434
|
+
expect(hr.status).toBe(202);
|
|
435
|
+
const ht = db.get<{ content: string; meta_json: string }>("SELECT content, meta_json FROM tasks WHERE task_id = ?1", hr.body.taskId)!;
|
|
436
|
+
expect(ht.content).toBe("plain");
|
|
437
|
+
expect(JSON.parse(ht.meta_json).scheduled_by_node_id).toBeUndefined();
|
|
438
|
+
expect((await mcp(node.b.token, "send_reply", { in_reply_to: hr.body.taskId, text: "ok", status: "replied" })).ok).toBe(true);
|
|
439
|
+
expect(replyRow(hr.body.taskId)?.session_name).toBe(`sa_boss_${stamp}`); // the person's unread, as before
|
|
440
|
+
});
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
// #2935 —— 「周期比单轮工作耗时短」的排程会**永久 skip**:退避只会让它更稀疏,追不上。
|
|
444
|
+
// 以前没有任何一处给出「这个排程每轮实际跑多久」,于是只能靠人翻 scheduled_task_runs。
|
|
445
|
+
// 这里只报数,不改调度行为:observed_run_seconds_p50 / period_shorter_than_run / suggested_every_seconds。
|
|
446
|
+
describe("#2935 排程周期诊断", () => {
|
|
447
|
+
test("周期短于实测单轮耗时时,列表里直接标出来并给建议值", async () => {
|
|
448
|
+
const c = await mcp(node.a.token, "schedule_create", {
|
|
449
|
+
name: `diag-${stamp}`, task: "t", target_node_id: node.a.id,
|
|
450
|
+
schedule: { type: "interval", every_seconds: 180 }, timezone: "Asia/Shanghai",
|
|
451
|
+
});
|
|
452
|
+
// #3275 批次 4 —— 上面那些 `create("a")` 断言其实**已经**钉住 schedule_create,
|
|
453
|
+
// 但调用在 helper 里、工具名不在同一行 ⇒ 统计口径归因不到。
|
|
454
|
+
// ⇒ 这里补一条**字面量直调**的:形状同外层,代价一次调用。
|
|
455
|
+
expect(Object.keys(c).sort()).toEqual(["ok", "schedule"]);
|
|
456
|
+
expect(c.ok).toBe(true);
|
|
457
|
+
const id = c.schedule.schedule_id;
|
|
458
|
+
// 造 3 条「一轮跑了 600 秒」的已完成 run(中位数 600s > 周期 180s)。
|
|
459
|
+
// scheduled_for 必须每行不同(表上有唯一约束);时间戳在 JS 里算好再传字面量 ——
|
|
460
|
+
// SQL 里写 `datetime('now','-'||?||' minute')` 这种拼接适配层翻译不了,
|
|
461
|
+
// PostgreSQL 会报 `function datetime(unknown, text) does not exist`(PG 梯形套件实测)。
|
|
462
|
+
const hubTs = (msAgo: number) => new Date(Date.now() - msAgo).toISOString().slice(0, 19).replace("T", " ");
|
|
463
|
+
for (let i = 0; i < 3; i++) {
|
|
464
|
+
const minsAgo = i * 20 + 20;
|
|
465
|
+
db.run(`INSERT INTO scheduled_task_runs (run_id, schedule_id, network_id, scheduled_for, status, created_at, completed_at)
|
|
466
|
+
VALUES (?1, ?2, ?3, ?4, 'replied', ?5, ?4)`,
|
|
467
|
+
[`srun_diag_${stamp}_${i}`, id, net, hubTs(minsAgo * 60_000), hubTs((minsAgo + 10) * 60_000)]);
|
|
468
|
+
}
|
|
469
|
+
const l = await mcp(node.a.token, "schedule_list");
|
|
470
|
+
const row = l.schedules.find((x: any) => x.schedule_id === id);
|
|
471
|
+
expect(row.observed_run_seconds_p50).toBe(600);
|
|
472
|
+
expect(row.period_shorter_than_run).toBe(true);
|
|
473
|
+
expect(row.suggested_every_seconds).toBe(600);
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
test("样本不足 3 条时不判断(p50 / flag 均为 null)", async () => {
|
|
477
|
+
const c = await mcp(node.a.token, "schedule_create", {
|
|
478
|
+
name: `diag2-${stamp}`, task: "t", target_node_id: node.a.id,
|
|
479
|
+
schedule: { type: "interval", every_seconds: 180 }, timezone: "Asia/Shanghai",
|
|
480
|
+
});
|
|
481
|
+
const l = await mcp(node.a.token, "schedule_list");
|
|
482
|
+
const row = l.schedules.find((x: any) => x.schedule_id === c.schedule.schedule_id);
|
|
483
|
+
expect(row.observed_run_seconds_p50).toBeNull();
|
|
484
|
+
expect(row.period_shorter_than_run).toBeNull();
|
|
485
|
+
});
|
|
486
|
+
});
|