@sleep2agi/commhub-server 0.9.0-preview.20 → 0.9.0-preview.201
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 +50 -7
- package/src/admin-delete-network-http.test.ts +228 -0
- package/src/admin-networks-http.test.ts +88 -0
- package/src/adoption-candidates-http.test.ts +137 -0
- package/src/adoption-candidates.ts +158 -0
- package/src/agent-access-batch.test.ts +210 -0
- package/src/agent-access.ts +487 -0
- package/src/agent-acl-groups-http.test.ts +247 -0
- package/src/agent-acl-http.test.ts +659 -0
- package/src/agent-acl-slice3-http.test.ts +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 +21 -12
- package/src/api-host-supervisors-runtime-readiness.test.ts +195 -0
- package/src/api-nodes-shape.test.ts +120 -13
- package/src/auth-password-change-cli-tokens-http.test.ts +423 -0
- package/src/auth-sessions-http.test.ts +250 -0
- package/src/auth-validate.test.ts +5 -1
- package/src/auth-weak-password-login.test.ts +176 -0
- package/src/auth.ts +609 -51
- package/src/avatar-validate.test.ts +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 +199 -7
- package/src/create-node-validate.ts +232 -15
- package/src/create-node-workdir.test.ts +289 -0
- package/src/create-node.ts +88 -23
- package/src/daemon-provider-snapshots.test.ts +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 +1306 -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 +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 -2380
- package/src/list-host-supervisors.test.ts +5 -4
- package/src/mcp-context-cost-http.test.ts +127 -0
- package/src/mcp-inflight.test.ts +92 -0
- package/src/mcp-inflight.ts +38 -0
- package/src/mcp-projects-events-http.test.ts +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 +114 -0
- package/src/node-permissions-http.test.ts +345 -0
- package/src/node-permissions.ts +420 -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/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 +6028 -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/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 +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 +567 -1
- package/src/task-access-http.test.ts +402 -0
- package/src/task-access.ts +420 -0
- package/src/task-activity.ts +60 -0
- package/src/task-auth-origin-http.test.ts +93 -0
- package/src/task-auth-origin.test.ts +24 -0
- package/src/task-auth-origin.ts +7 -0
- package/src/task-consumption.test.ts +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 +4226 -405
- package/src/upload-early-reject-guard.test.ts +155 -0
- package/src/uploads-http.test.ts +97 -15
- package/src/uploads.ts +103 -6
- package/src/user-avatar-http.test.ts +74 -0
- package/src/user-avatar-image.ts +160 -0
- package/src/user-avatar-migration.test.ts +25 -0
- package/src/user-avatar-upload-http.test.ts +348 -0
- package/src/user-inbox-by-agent-http.test.ts +161 -0
- package/src/user-inbox-netid-notnull.test.ts +122 -0
- package/src/user-inbox-read-http.test.ts +223 -0
- package/src/user-inbox-schema.test.ts +54 -0
- package/src/version-stats.test.ts +66 -0
- package/src/version-stats.ts +109 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// #2720 复盘 —— 一次**我判断错了**的根因,钉成合同测试。
|
|
2
|
+
//
|
|
3
|
+
// 背景:我看 `/api/requirements/{tags,stats,people,people/directory}` 这四个只读端点
|
|
4
|
+
// 用的是 `resolveRestWriteNetworkId`(写解析器),于是一口咬定「非 admin 会员只要
|
|
5
|
+
// 请求里不带 network_id 就会 400 network_id_required」,还为此写了一个新的
|
|
6
|
+
// `resolveRestReadNetworkId` 想放宽它。
|
|
7
|
+
//
|
|
8
|
+
// 通信设计师 要求用**真实链路**复证,不要手搓 scope。复证结果:
|
|
9
|
+
//
|
|
10
|
+
// 请求不带 network_id,走 resolveRestNetworkScope(null, auth, false):
|
|
11
|
+
// 非 admin 单网络 → scope.networkIds = [NET_A] ⇒ 写解析器第一行的
|
|
12
|
+
// singleNetworkId(scope) 就已经返回 NET_A —— **根本不会 400**
|
|
13
|
+
// 非 admin 双网络 → scope.networkIds = [NET_A,NET_B] ⇒ null(歧义,确实 400)
|
|
14
|
+
// admin 单网络 → scope.networkIds = null ⇒ 回退成员关系,返回 NET_A
|
|
15
|
+
//
|
|
16
|
+
// ⇒ 那个「读解析器」在所有真实输入下与写解析器**结果完全相同**,是纯 no-op。
|
|
17
|
+
// 我当初的测试把非 admin 的 auth 配上 `{networkIds: null}`(真实链路不会产出的组合),
|
|
18
|
+
// 等于自己造故障再修故障 —— 所以那个 PR 已关闭,这里只留**真实链路**的断言。
|
|
19
|
+
//
|
|
20
|
+
// 这条测试的价值不是「证明代码对」,是**钉住真实的解析顺序**,让下一次想「放宽读权限」
|
|
21
|
+
// 的人先看到:单网络用户本来就没被拦,真正 400 的只有多网络且没显式指定的人。
|
|
22
|
+
//
|
|
23
|
+
// Run: COMMHUB_DB=/tmp/2720-real-chain.db bun test src/rest-read-network-real-chain.test.ts
|
|
24
|
+
|
|
25
|
+
import { afterAll, beforeEach, describe, expect, test } from "bun:test";
|
|
26
|
+
import { db } from "./db.js";
|
|
27
|
+
import { resolveRestNetworkScope, resolveRestWriteNetworkId } from "./network-scope.js";
|
|
28
|
+
|
|
29
|
+
const NET_A = "net_chain_a";
|
|
30
|
+
const NET_B = "net_chain_b";
|
|
31
|
+
const U_SINGLE = "u_chain_single"; // 只属于 NET_A
|
|
32
|
+
const U_MULTI = "u_chain_multi"; // 属于 NET_A + NET_B
|
|
33
|
+
|
|
34
|
+
function cleanup() {
|
|
35
|
+
for (const t of ["network_members", "networks"]) {
|
|
36
|
+
try { db.run(`DELETE FROM ${t} WHERE network_id IN (?1, ?2)`, [NET_A, NET_B]); } catch {}
|
|
37
|
+
}
|
|
38
|
+
for (const u of [U_SINGLE, U_MULTI]) {
|
|
39
|
+
try { db.run("DELETE FROM users WHERE user_id = ?1", [u]); } catch {}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function seed() {
|
|
44
|
+
for (const u of [U_SINGLE, U_MULTI]) {
|
|
45
|
+
db.run(
|
|
46
|
+
"INSERT INTO users (user_id, username, password_hash, role, created_at) VALUES (?1,?2,'x','user',datetime('now'))",
|
|
47
|
+
[u, u],
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
db.run("INSERT INTO networks (network_id, network_name, owner_id, created_at) VALUES (?1,?1,?2,datetime('now'))", [NET_A, U_SINGLE]);
|
|
51
|
+
db.run("INSERT INTO networks (network_id, network_name, owner_id, created_at) VALUES (?1,?1,?2,datetime('now'))", [NET_B, U_MULTI]);
|
|
52
|
+
db.run("INSERT INTO network_members (user_id, network_id, role, joined_at) VALUES (?1,?2,'owner',datetime('now'))", [U_SINGLE, NET_A]);
|
|
53
|
+
db.run("INSERT INTO network_members (user_id, network_id, role, joined_at) VALUES (?1,?2,'member',datetime('now'))", [U_MULTI, NET_A]);
|
|
54
|
+
db.run("INSERT INTO network_members (user_id, network_id, role, joined_at) VALUES (?1,?2,'owner',datetime('now'))", [U_MULTI, NET_B]);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
beforeEach(() => { cleanup(); seed(); });
|
|
58
|
+
afterAll(() => { cleanup(); });
|
|
59
|
+
|
|
60
|
+
/** 走真实链路:先按「请求里没有 network_id」解析作用域,再交给写解析器。 */
|
|
61
|
+
function resolveWithoutRequestedNetwork(userId: string, isAdmin: boolean) {
|
|
62
|
+
const authCtx = { userId, networkId: null as string | null };
|
|
63
|
+
const scope = resolveRestNetworkScope(null, authCtx, false ? null : isAdmin);
|
|
64
|
+
return { scope, networkId: resolveRestWriteNetworkId(scope, authCtx, isAdmin) };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
describe("#2720 真实链路:只读端点不会因为「没带 network_id」就把单网络会员打回 400", () => {
|
|
68
|
+
test("🔴 非 admin + 恰好 1 个成员关系 → 解析出 NET_A(不是 null)", () => {
|
|
69
|
+
const { scope, networkId } = resolveWithoutRequestedNetwork(U_SINGLE, false);
|
|
70
|
+
expect(scope.networkIds).toEqual([NET_A]);
|
|
71
|
+
expect(networkId).toBe(NET_A);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("非 admin + 2 个成员关系 → null(歧义时确实要显式指定,这条不能放宽)", () => {
|
|
75
|
+
const { scope, networkId } = resolveWithoutRequestedNetwork(U_MULTI, false);
|
|
76
|
+
expect(scope.networkIds?.length).toBe(2);
|
|
77
|
+
expect(networkId).toBeNull();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("admin + 1 个成员关系 → NET_A(回退分支)", () => {
|
|
81
|
+
const { networkId } = resolveWithoutRequestedNetwork(U_SINGLE, true);
|
|
82
|
+
expect(networkId).toBe(NET_A);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("请求里显式指定了自己所属的网络 → 用它(与角色无关)", () => {
|
|
86
|
+
const authCtx = { userId: U_MULTI, networkId: null as string | null };
|
|
87
|
+
const scope = resolveRestNetworkScope(NET_B, authCtx, false);
|
|
88
|
+
expect(resolveRestWriteNetworkId(scope, authCtx, false)).toBe(NET_B);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("请求里指定了一个自己不属于的网络 → scope 明确 denied,解析结果 null", () => {
|
|
92
|
+
const authCtx = { userId: U_SINGLE, networkId: null as string | null };
|
|
93
|
+
const scope = resolveRestNetworkScope(NET_B, authCtx, false);
|
|
94
|
+
expect(scope.denied).toBeTruthy();
|
|
95
|
+
expect(resolveRestWriteNetworkId(scope, authCtx, false)).toBeNull();
|
|
96
|
+
});
|
|
97
|
+
});
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// #819 — `resolveRestWriteNetworkId` 是 network-scope.ts 里唯一没有任何测试点名的
|
|
2
|
+
// 导出函数。同文件的五个兄弟各有 1-2 个测试文件点名它们:
|
|
3
|
+
//
|
|
4
|
+
// resolveRestNetworkScope 1 canRestWriteNetwork 1
|
|
5
|
+
// singleNetworkId 2 addNetworkScope 1
|
|
6
|
+
// getUserNetworkIds 1 resolveRestWriteNetworkId 🔴 0
|
|
7
|
+
//
|
|
8
|
+
// 而它决定的是**一次 REST 写入落到哪个网络**。它的 docstring 说明了一条不显然的
|
|
9
|
+
// 规则:管理员的**读**作用域按设计是全局的(networkIds=null),所以它本身表达不了
|
|
10
|
+
// 「这个管理员恰好只属于一个网络」;而**写**不能继承这个歧义 —— 只有在确实只有
|
|
11
|
+
// 一个成员关系时才用它,0 个或 ≥2 个都必须显式指定网络。
|
|
12
|
+
//
|
|
13
|
+
// 🔴 这条规则的两半都要钉:
|
|
14
|
+
// - **放行那半**(admin + 恰好 1 个成员关系 → 用它)如果坏了,会退化成「管理员
|
|
15
|
+
// 写任何东西都要显式带 network_id」——很吵,但**安全**,所以没人会急着修;
|
|
16
|
+
// - **收紧那半**(admin + 0 或 ≥2 → null)如果坏了,一次写入会**落到一个没被
|
|
17
|
+
// 指定的网络里**,而调用方看到的是成功。
|
|
18
|
+
//
|
|
19
|
+
// 只写反向断言是不够的:一个「永远返回 null」的实现能通过所有「收紧」用例。
|
|
20
|
+
// 所以每一条收紧断言都配了正控。
|
|
21
|
+
//
|
|
22
|
+
// Run: COMMHUB_DB=/tmp/819-rest-write-scope.db bun test src/rest-write-network-resolution.test.ts
|
|
23
|
+
|
|
24
|
+
import { afterAll, beforeEach, describe, expect, test } from "bun:test";
|
|
25
|
+
import { db } from "./db.js";
|
|
26
|
+
import { resolveRestNetworkScope, resolveRestWriteNetworkId, type RestNetworkScope } from "./network-scope.js";
|
|
27
|
+
|
|
28
|
+
const NET_A = "net_819_a";
|
|
29
|
+
const NET_B = "net_819_b";
|
|
30
|
+
const U_SINGLE = "u819_single"; // 只属于 NET_A
|
|
31
|
+
const U_MULTI = "u819_multi"; // 属于 NET_A + NET_B
|
|
32
|
+
const U_NONE = "u819_none"; // 一个都不属于
|
|
33
|
+
const ALL_USERS = [U_SINGLE, U_MULTI, U_NONE];
|
|
34
|
+
|
|
35
|
+
function cleanup() {
|
|
36
|
+
try { db.run("DELETE FROM network_members WHERE network_id IN (?1, ?2)", [NET_A, NET_B]); } catch {}
|
|
37
|
+
try { db.run("DELETE FROM networks WHERE network_id IN (?1, ?2)", [NET_A, NET_B]); } catch {}
|
|
38
|
+
for (const u of ALL_USERS) {
|
|
39
|
+
try { db.run("DELETE FROM users WHERE user_id = ?1", [u]); } catch {}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function seed() {
|
|
44
|
+
for (const u of ALL_USERS) {
|
|
45
|
+
db.run(
|
|
46
|
+
`INSERT INTO users (user_id, username, password_hash, role, created_at)
|
|
47
|
+
VALUES (?1, ?2, 'x', 'user', datetime('now'))`,
|
|
48
|
+
[u, u],
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
db.run(`INSERT INTO networks (network_id, network_name, owner_id, created_at) VALUES (?1, ?1, ?2, datetime('now'))`, [NET_A, U_SINGLE]);
|
|
52
|
+
db.run(`INSERT INTO networks (network_id, network_name, owner_id, created_at) VALUES (?1, ?1, ?2, datetime('now'))`, [NET_B, U_MULTI]);
|
|
53
|
+
const member = (u: string, net: string, role: string) =>
|
|
54
|
+
db.run(`INSERT INTO network_members (user_id, network_id, role, joined_at) VALUES (?1, ?2, ?3, datetime('now'))`, [u, net, role]);
|
|
55
|
+
member(U_SINGLE, NET_A, "owner");
|
|
56
|
+
member(U_MULTI, NET_A, "member");
|
|
57
|
+
member(U_MULTI, NET_B, "owner");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
beforeEach(() => { cleanup(); seed(); });
|
|
61
|
+
afterAll(() => { cleanup(); });
|
|
62
|
+
|
|
63
|
+
/** 管理员的读作用域:全局(networkIds=null),这正是歧义的来源。 */
|
|
64
|
+
const ADMIN_SCOPE: RestNetworkScope = { networkIds: null };
|
|
65
|
+
const scopeOf = (...ids: string[]): RestNetworkScope => ({ networkIds: ids });
|
|
66
|
+
const ctx = (userId: string) => ({ userId, networkId: null });
|
|
67
|
+
|
|
68
|
+
describe("#819 resolveRestWriteNetworkId — 作用域里就能确定时,直接用它", () => {
|
|
69
|
+
test("作用域恰好一个网络 → 用那一个(与是不是 admin 无关)", () => {
|
|
70
|
+
expect(resolveRestWriteNetworkId(scopeOf(NET_A), ctx(U_MULTI), false)).toBe(NET_A);
|
|
71
|
+
expect(resolveRestWriteNetworkId(scopeOf(NET_A), ctx(U_MULTI), true)).toBe(NET_A);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("作用域两个网络 → 歧义,null(即使调用者是 admin)", () => {
|
|
75
|
+
expect(resolveRestWriteNetworkId(scopeOf(NET_A, NET_B), ctx(U_MULTI), true)).toBeNull();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// 🔴 这一条我一开始断错了,留下来当记录:我以为「作用域为空数组 → null」,
|
|
79
|
+
// 实测是 NET_A。原因是 `[]` 过不了 singleNetworkId,于是落到 admin 的成员关系
|
|
80
|
+
// 回退分支,而 U_SINGLE 恰好只有一个成员关系。
|
|
81
|
+
//
|
|
82
|
+
// 而 `networkIds: []` 在 resolveRestNetworkScope 里有确切含义:`:46` 那一行,
|
|
83
|
+
// **非 admin 请求了一个自己没有角色的网络** —— 它带着 `denied: "access denied
|
|
84
|
+
// to requested network"`。也就是说 `[]` 不是「没指定」,是「明确被拒」。
|
|
85
|
+
//
|
|
86
|
+
// 现在**没有**问题,因为那一行只对非 admin 产生(admin 在 `:42` 就提前返回
|
|
87
|
+
// networkIds:null 了),而非 admin 走到这里必然 null —— 见下一条。
|
|
88
|
+
//
|
|
89
|
+
// 但它是一个潜伏的形状:如果将来有任何路径让 admin 拿到 networkIds: [],
|
|
90
|
+
// 这次写入会**忽略那条明确的拒绝**,回退到他的单一成员关系。所以两条都钉住:
|
|
91
|
+
// 当前行为,以及那条让它安全的前提。
|
|
92
|
+
test("作用域为空数组 + admin + 恰好一个成员关系 → 回退到成员关系(当前行为,记录)", () => {
|
|
93
|
+
expect(resolveRestWriteNetworkId(scopeOf(), ctx(U_SINGLE), true)).toBe(NET_A);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("🔴 让上一条安全的前提:空数组作用域只由非 admin 产生,而非 admin → null", () => {
|
|
97
|
+
expect(resolveRestWriteNetworkId(scopeOf(), ctx(U_SINGLE), false)).toBeNull();
|
|
98
|
+
// 前提本身也断一次:resolveRestNetworkScope 对 admin 从不产出空数组。
|
|
99
|
+
const adminScope = resolveRestNetworkScope(NET_B, ctx(U_NONE), true);
|
|
100
|
+
expect(adminScope.networkIds).toBeNull();
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe("#819 admin 的全局读作用域不能替写入决定网络", () => {
|
|
105
|
+
test("🔴 admin + 恰好 1 个成员关系 → 用那一个(放行那半)", () => {
|
|
106
|
+
expect(resolveRestWriteNetworkId(ADMIN_SCOPE, ctx(U_SINGLE), true)).toBe(NET_A);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("🔴 admin + 2 个成员关系 → null,必须显式指定(收紧那半)", () => {
|
|
110
|
+
expect(resolveRestWriteNetworkId(ADMIN_SCOPE, ctx(U_MULTI), true)).toBeNull();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("🔴 admin + 0 个成员关系 → null(不能因为读是全局的就随便挑一个)", () => {
|
|
114
|
+
expect(resolveRestWriteNetworkId(ADMIN_SCOPE, ctx(U_NONE), true)).toBeNull();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("非 admin + 全局作用域 → null(这种组合本身就不该发生,fail closed)", () => {
|
|
118
|
+
// 正控在上面那条「admin + 单成员 → NET_A」:如果实现改成永远 null,那条会红。
|
|
119
|
+
expect(resolveRestWriteNetworkId(ADMIN_SCOPE, ctx(U_SINGLE), false)).toBeNull();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("没有 authCtx + 全局作用域 → null", () => {
|
|
123
|
+
expect(resolveRestWriteNetworkId(ADMIN_SCOPE, null, true)).toBeNull();
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe("#819 作用域优先于成员关系回退", () => {
|
|
128
|
+
test("作用域说 NET_B,而该用户唯一的成员关系是 NET_A → 结果是 NET_B", () => {
|
|
129
|
+
// 归属由作用域决定,不由「他碰巧属于哪个网络」决定。
|
|
130
|
+
// 如果实现把两者的优先级搞反,这条会返回 NET_A。
|
|
131
|
+
expect(resolveRestWriteNetworkId(scopeOf(NET_B), ctx(U_SINGLE), true)).toBe(NET_B);
|
|
132
|
+
});
|
|
133
|
+
});
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// #1511 —— `/api/nodes` 要给客户端一份「能不能替你重启」的判断,而且要和在途任务一致。
|
|
2
|
+
//
|
|
3
|
+
// 客户端场景:保存密钥/env 之后,必须能立刻二选一告诉用户
|
|
4
|
+
// 「已安排重启」 vs 「需要你自己重启(命令:…)」
|
|
5
|
+
// 判据必须和 restart_node / stop_node 同一份,否则会出现「客户端说空闲、一点就被拒」。
|
|
6
|
+
|
|
7
|
+
import { describe, expect, test, beforeAll, afterAll } from "bun:test";
|
|
8
|
+
import { mkdtempSync, rmSync } from "fs";
|
|
9
|
+
import { join } from "path";
|
|
10
|
+
import { tmpdir } from "os";
|
|
11
|
+
import { register, createNetworkTokenForNode } from "./auth.js";
|
|
12
|
+
import { db } from "./db.js";
|
|
13
|
+
|
|
14
|
+
const DIR = mkdtempSync(join(tmpdir(), "anet-restart-readiness-"));
|
|
15
|
+
let BASE = "";
|
|
16
|
+
let hub: any = null;
|
|
17
|
+
const PW = "RestartReadinessPassw0rd!";
|
|
18
|
+
let NET = "";
|
|
19
|
+
let TOKEN = "";
|
|
20
|
+
let USER = "";
|
|
21
|
+
|
|
22
|
+
async function send(method: string, path: string, body?: unknown) {
|
|
23
|
+
const res = await fetch(`${BASE}${path}`, { method, headers: { Authorization: `Bearer ${TOKEN}`, "Content-Type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) });
|
|
24
|
+
const text = await res.text();
|
|
25
|
+
let json: any = null; try { json = JSON.parse(text); } catch {}
|
|
26
|
+
return { status: res.status, body: json };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let seq = 0;
|
|
30
|
+
function mkNode(prefix: "node" | "n", alias: string, opts: { role?: string; lifecycle?: string } = {}) {
|
|
31
|
+
const id = `${prefix}_rr_${seq++}_${Date.now()}`;
|
|
32
|
+
const snap = opts.role ? JSON.stringify({ role: opts.role }) : null;
|
|
33
|
+
db.run("INSERT INTO nodes (node_id, node_name, alias, runtime, network_id, lifecycle_state, config_snapshot, owner_user_id) VALUES (?1,?2,?2,'codex',?3,?4,?5,?6)",
|
|
34
|
+
[id, alias, NET, opts.lifecycle ?? "active", snap, USER]);
|
|
35
|
+
return id;
|
|
36
|
+
}
|
|
37
|
+
function mkTaskFor(alias: string, status: string) {
|
|
38
|
+
const taskId = `t_rr_${seq++}`;
|
|
39
|
+
db.run("INSERT INTO tasks (task_id, from_name, to_name, priority, status, content, created_at, network_id) VALUES (?1,'x',?2,'normal',?3,'c',datetime('now'),?4)", [taskId, alias, status, NET]);
|
|
40
|
+
db.run("INSERT INTO inbox (id, session_name, type, task_id, acked, content, created_at, network_id) VALUES (?1,?2,'task',?3,0,'c',datetime('now'),?4)", [`i_rr_${seq++}`, alias, taskId, NET]);
|
|
41
|
+
return taskId;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function mkCreateRequest(nodeId: string) {
|
|
45
|
+
db.run(`INSERT INTO node_create_requests
|
|
46
|
+
(request_id, network_id, daemon_node_id, child_name, runtime, flags_json, env_keys, status, created_at, created_by_token)
|
|
47
|
+
VALUES (?1,?2,'node_daemon_rr',?3,'codex','{}','[]','succeeded',?4,'t')`,
|
|
48
|
+
[`cr_${nodeId.slice(5)}`, NET, `child-${seq++}`, Date.now()]);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const rowOf = async (nodeId: string) => {
|
|
52
|
+
const r = await send("GET", `/api/nodes?network_id=${NET}&node_id=${nodeId}`);
|
|
53
|
+
expect(r.status).toBe(200);
|
|
54
|
+
return (r.body.nodes as any[])[0];
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
beforeAll(async () => {
|
|
58
|
+
process.env.COMMHUB_DB ||= join(DIR, "hub.db");
|
|
59
|
+
process.env.COMMHUB_UPLOADS_DIR = join(DIR, "uploads");
|
|
60
|
+
process.env.HOST = "127.0.0.1";
|
|
61
|
+
const a = register(`rr_owner_${Date.now()}`, PW, undefined, "Owner");
|
|
62
|
+
expect(a.ok).toBe(true);
|
|
63
|
+
TOKEN = a.token!; USER = a.user!.user_id; NET = a.network_id!;
|
|
64
|
+
const mod: any = await import("./server.js");
|
|
65
|
+
hub = mod.bootServer({ port: 0, hostname: "127.0.0.1" });
|
|
66
|
+
BASE = `http://127.0.0.1:${hub.port}`;
|
|
67
|
+
}, 30_000);
|
|
68
|
+
|
|
69
|
+
afterAll(() => {
|
|
70
|
+
try { hub?.stop?.(true); } catch {}
|
|
71
|
+
try { rmSync(DIR, { recursive: true, force: true }); } catch {}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe("#1511 restart_readiness", () => {
|
|
75
|
+
test("没人管的节点 = manual,且 can_restart_now=false", async () => {
|
|
76
|
+
const id = mkNode("n", `solo-${seq}`);
|
|
77
|
+
const row = await rowOf(id);
|
|
78
|
+
expect(row.restart_readiness).toEqual({ path: "manual", in_flight: 0, can_restart_now: false });
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("created-daemon 的节点 = restart_node;在途任务让它变 false", async () => {
|
|
82
|
+
const id = mkNode("node", `supervised-${seq}`);
|
|
83
|
+
mkCreateRequest(id);
|
|
84
|
+
let row = await rowOf(id);
|
|
85
|
+
expect(row.restart_readiness.path).toBe("restart_node");
|
|
86
|
+
expect(row.restart_readiness.can_restart_now).toBe(true);
|
|
87
|
+
|
|
88
|
+
mkTaskFor(row.alias, "running");
|
|
89
|
+
row = await rowOf(id);
|
|
90
|
+
expect(row.restart_readiness.in_flight).toBe(1);
|
|
91
|
+
expect(row.restart_readiness.can_restart_now).toBe(false);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("终态任务不算在途(与 stop_node 同一判据)", async () => {
|
|
95
|
+
const id = mkNode("node", `replied-${seq}`);
|
|
96
|
+
mkCreateRequest(id);
|
|
97
|
+
const row = await rowOf(id);
|
|
98
|
+
mkTaskFor(row.alias, "replied");
|
|
99
|
+
const after = await rowOf(id);
|
|
100
|
+
expect(after.restart_readiness.in_flight).toBe(0);
|
|
101
|
+
expect(after.restart_readiness.can_restart_now).toBe(true);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("adopt 来的节点 = stop_then_start(restart_node 会被拒,但 daemon 仍能 Stop→Start)", async () => {
|
|
105
|
+
const id = mkNode("n", `adopted-${seq}`);
|
|
106
|
+
db.run("INSERT INTO node_daemon_bindings (request_id, network_id, node_id, daemon_node_id, workdir, requested_by, status, created_at, updated_at) VALUES (?1,?2,?3,'node_daemon_rr','/tmp','u','active',0,0)",
|
|
107
|
+
[`adopt_rr_${seq}`, NET, id]);
|
|
108
|
+
const row = await rowOf(id);
|
|
109
|
+
expect(row.restart_readiness).toEqual({ path: "stop_then_start", in_flight: 0, can_restart_now: true });
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("stopped 节点:在途计数不挡路(restart 是把 lifecycle 翻回 active 的出口)", async () => {
|
|
113
|
+
const id = mkNode("node", `stopped-${seq}`, { lifecycle: "stopped" });
|
|
114
|
+
mkCreateRequest(id);
|
|
115
|
+
const row = await rowOf(id);
|
|
116
|
+
mkTaskFor(row.alias, "running");
|
|
117
|
+
const after = await rowOf(id);
|
|
118
|
+
expect(after.restart_readiness.in_flight).toBe(1);
|
|
119
|
+
expect(after.restart_readiness.can_restart_now).toBe(true);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// 多用户 Agent 权限 —— 受限成员与上传文件。
|
|
2
|
+
// 文件按网络归属;受限成员(只看授权 Agent 的成员)在受限网络里不能凭 file_id 读网络里任意文件,
|
|
3
|
+
// 也不能把自己看不见的 file_id 塞进发给授权 Agent 的任务(那等于借 Agent 的网络令牌去读)。
|
|
4
|
+
import { existsSync, readFileSync } from "fs";
|
|
5
|
+
import { db } from "./db.js";
|
|
6
|
+
import { addAgentTimelineScope, addOwnTrafficScope, type RestNetworkScope } from "./network-scope.js";
|
|
7
|
+
import { FILE_ID_REGEX, indexEntryPath, validateIndexEntry } from "./uploads.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 受限成员能不能看见这个文件:只认**对方(授权 Agent / 给他发私信的人)放进来**的附件 ——
|
|
11
|
+
* user_inbox 里发给自己的私信附件;
|
|
12
|
+
* 授权 Agent 时间线上(#563,与 /api/tasks 同一判据 addAgentTimelineScope)**别人**发出的任务的
|
|
13
|
+
* attachments —— 时间线上看得见那条消息,就得看得见它带的图;发送方是受限成员时发送路径已校验过
|
|
14
|
+
* 他看得见这些文件,不受限的发送方本来就看得见全网文件;
|
|
15
|
+
* 时间线上任务的 reply_attachments(Agent 回复写进的);
|
|
16
|
+
* 授权 Agent 发给自己的 inbox 行的 attachments。
|
|
17
|
+
* 自己随任务带出去的 attachments **不算**:否则把任意 file_id 塞进自己的任务就能「解锁」别人的文件。
|
|
18
|
+
*/
|
|
19
|
+
export function restrictedMemberSeesFile(userId: string, username: string, networkId: string, fileId: string): boolean {
|
|
20
|
+
if (!FILE_ID_REGEX.test(fileId)) return false;
|
|
21
|
+
const listed = (metaJson: string | null | undefined, key: "attachments" | "reply_attachments"): boolean => {
|
|
22
|
+
if (!metaJson) return false;
|
|
23
|
+
try {
|
|
24
|
+
const list = JSON.parse(metaJson)?.[key];
|
|
25
|
+
return Array.isArray(list) && list.some((a: any) => a && typeof a === "object" && a.file_id === fileId);
|
|
26
|
+
} catch { return false; }
|
|
27
|
+
};
|
|
28
|
+
// LIKE 只做预筛(file_id 只含 [A-Za-z0-9_-],`_` 转义后逐字匹配),真正的判定在 listed() 里按键解析。
|
|
29
|
+
const needle = `%"${fileId.replace(/_/g, "\\_")}"%`;
|
|
30
|
+
for (const row of db.all<{ meta_json: string | null }>(
|
|
31
|
+
"SELECT meta_json FROM user_inbox WHERE user_id = ?1 AND network_id = ?2 AND meta_json LIKE ?3 ESCAPE '\\' LIMIT 50",
|
|
32
|
+
userId, networkId, needle,
|
|
33
|
+
)) {
|
|
34
|
+
if (listed(row.meta_json, "attachments")) return true;
|
|
35
|
+
}
|
|
36
|
+
const scope: RestNetworkScope = { networkId, networkIds: null, agentRestriction: { userId, username, networkIds: [networkId] } };
|
|
37
|
+
const taskParams: unknown[] = [needle];
|
|
38
|
+
const taskSql = addAgentTimelineScope("SELECT from_name, to_name, meta_json FROM tasks WHERE meta_json LIKE ?1 ESCAPE '\\'", taskParams, scope, { from: "from_name", to: "to_name", fromNodeId: "from_node_id", toNodeId: "to_node_id" });
|
|
39
|
+
for (const row of db.all<{ from_name: string; to_name: string; meta_json: string | null }>(taskSql + " LIMIT 50", ...taskParams)) {
|
|
40
|
+
if (row.from_name !== username && listed(row.meta_json, "attachments")) return true;
|
|
41
|
+
if (listed(row.meta_json, "reply_attachments")) return true;
|
|
42
|
+
}
|
|
43
|
+
const inboxParams: unknown[] = [needle];
|
|
44
|
+
const inboxSql = addOwnTrafficScope("SELECT session_name, meta_json FROM inbox WHERE meta_json LIKE ?1 ESCAPE '\\'", inboxParams, scope, { from: "from_session", to: "session_name" });
|
|
45
|
+
for (const row of db.all<{ session_name: string; meta_json: string | null }>(inboxSql + " LIMIT 50", ...inboxParams)) {
|
|
46
|
+
if (row.session_name === username && listed(row.meta_json, "attachments")) return true;
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** 受限成员能不能用这个文件(下载 / 当附件转给 Agent):自己传的,或 restrictedMemberSeesFile。 */
|
|
52
|
+
export function restrictedMemberCanUseFile(userId: string, username: string, networkId: string, fileId: string): boolean {
|
|
53
|
+
if (!FILE_ID_REGEX.test(fileId)) return false;
|
|
54
|
+
const path = indexEntryPath(fileId);
|
|
55
|
+
if (!path || !existsSync(path)) return false;
|
|
56
|
+
let entry: any;
|
|
57
|
+
try { entry = JSON.parse(readFileSync(path, "utf8")); } catch { return false; }
|
|
58
|
+
if (!validateIndexEntry(entry) || entry.file_id !== fileId) return false;
|
|
59
|
+
if (entry.network_id !== networkId) return false;
|
|
60
|
+
if (entry.owner_id === userId) return true;
|
|
61
|
+
return restrictedMemberSeesFile(userId, username, networkId, fileId);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 受限成员发给 Agent 的任务 / 消息里的附件:每个 file_id 都必须是他自己能用的文件。
|
|
66
|
+
* 返回第一个不合格的 file_id(调用方回 403),全部合格返回 null。
|
|
67
|
+
*/
|
|
68
|
+
export function restrictedMemberAttachmentsDenied(userId: string, username: string, networkId: string, raw: unknown): string | null {
|
|
69
|
+
if (!Array.isArray(raw)) return null;
|
|
70
|
+
for (const item of raw) {
|
|
71
|
+
const fileId = item && typeof item === "object" ? (item as { file_id?: unknown }).file_id : undefined;
|
|
72
|
+
if (fileId === undefined) continue;
|
|
73
|
+
if (typeof fileId !== "string" || !restrictedMemberCanUseFile(userId, username, networkId, fileId)) return String(fileId);
|
|
74
|
+
}
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// #2614(第三轮)—— 首次 sweep 不是“等 N 秒”,而是“等循环安静”。
|
|
2
|
+
//
|
|
3
|
+
// 两次部署实测:60 秒后仍撞上风暴(max 1238ms);180 秒后撞得更狠(max 3919ms,风暴持续 5 分钟)。
|
|
4
|
+
// 这里钉住判据本身(纯函数),不看时钟、不看真实 hub。
|
|
5
|
+
import { describe, expect, test } from "bun:test";
|
|
6
|
+
import { firstSweepShouldWait, FIRST_SWEEP_QUIET_INFLIGHT } from "./retention.js";
|
|
7
|
+
|
|
8
|
+
const NOW = Date.parse("2026-10-27T12:00:00.000Z");
|
|
9
|
+
const iso = (s: number) => new Date(NOW - s * 1000).toISOString();
|
|
10
|
+
|
|
11
|
+
describe("firstSweepShouldWait (#2614)", () => {
|
|
12
|
+
test("waits while requests are queued", () => {
|
|
13
|
+
expect(firstSweepShouldWait({ now: NOW, inflight: FIRST_SWEEP_QUIET_INFLIGHT + 1, lastStallAtIso: null })).toBe(true);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("waits while the loop stalled within the last minute", () => {
|
|
17
|
+
expect(firstSweepShouldWait({ now: NOW, inflight: 0, lastStallAtIso: iso(30) })).toBe(true);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("runs once inflight is low and the last stall is old enough", () => {
|
|
21
|
+
expect(firstSweepShouldWait({ now: NOW, inflight: 3, lastStallAtIso: iso(120) })).toBe(false);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("runs when there has never been a stall", () => {
|
|
25
|
+
expect(firstSweepShouldWait({ now: NOW, inflight: 0, lastStallAtIso: null })).toBe(false);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("an unparseable stall timestamp does not block forever", () => {
|
|
29
|
+
expect(firstSweepShouldWait({ now: NOW, inflight: 0, lastStallAtIso: "not-a-date" })).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
});
|