@sleep2agi/commhub-server 0.9.0-preview.28 → 0.9.0-preview.280
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 +215 -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 +888 -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 +228 -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/all-status-cache.test.ts +31 -0
- package/src/all-status-cache.ts +37 -0
- package/src/anet-variant-real-daemon-http.test.ts +184 -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 +218 -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-sec1.test.ts +37 -0
- package/src/config-apply-validate.test.ts +188 -1
- package/src/config-apply-validate.ts +226 -3
- package/src/contracts/session-project-create.ts +14 -0
- package/src/contracts/session-source-delivery.ts +16 -0
- package/src/contracts/session-source-directory.ts +54 -0
- package/src/contracts/session-source-evidence.ts +72 -0
- package/src/contracts/session-source-execution.ts +24 -0
- package/src/contracts/session-source-transport.ts +18 -0
- 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 +59 -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 +1334 -154
- 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 +126 -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 +496 -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 +398 -0
- package/src/node-session-control.ts +316 -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/node-token-usage.test.ts +128 -0
- package/src/node-token-usage.ts +228 -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 +282 -0
- package/src/opencode-v2-catalog.ts +357 -0
- package/src/package-self-contained.test.ts +32 -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 +272 -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 +280 -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 +964 -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 +5033 -377
- package/src/session-fork-store.test.ts +43 -0
- package/src/session-fork-store.ts +51 -0
- package/src/session-project-attach-migration.test.ts +22 -0
- package/src/session-project-attach-native.test.ts +121 -0
- package/src/session-project-create-http.test.ts +407 -0
- package/src/session-project-create-native.test.ts +290 -0
- package/src/session-project-create.ts +159 -0
- package/src/session-source-admission-store.ts +58 -0
- package/src/session-source-create.test.ts +155 -0
- package/src/session-source-create.ts +97 -0
- package/src/session-source-delivery.ts +109 -0
- package/src/session-source-directory-http.test.ts +192 -0
- package/src/session-source-evidence.test.ts +898 -0
- package/src/session-source-evidence.ts +191 -0
- package/src/session-source-execution.ts +87 -0
- package/src/session-source-grant.test.ts +117 -0
- package/src/session-source-grant.ts +78 -0
- package/src/session-source-issuer.test.ts +250 -0
- package/src/session-source-issuer.ts +164 -0
- package/src/session-source-transport.ts +375 -0
- package/src/session-task-on-dispatch.ts +14 -0
- package/src/session-token-ownership-http.test.ts +122 -0
- package/src/shared/codex-inventory.ts +41 -0
- package/src/shared/daemon-provider-login.ts +31 -0
- package/src/shared/daemon-provider-snapshot.ts +52 -0
- package/src/shared/external-schedule-contract.ts +60 -0
- package/src/shared/node-name.test.ts +62 -0
- package/src/shared/node-name.ts +161 -0
- package/src/shared/probe-host-allowlist-drift.test.ts +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-node-secrets-capable-http.test.ts +44 -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 +512 -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 +117 -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/token-usage-http.test.ts +156 -0
- package/src/tool-audience-http.test.ts +266 -0
- package/src/tool-audience.ts +130 -0
- package/src/tools-list-json-schema.test.ts +94 -0
- package/src/tools.ts +4530 -471
- 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,100 @@
|
|
|
1
|
+
// #2980 —— 自建 Hub 的网络数上限:一个可配置的数,不是「free/pro 套餐」。
|
|
2
|
+
//
|
|
3
|
+
// ## 为什么改
|
|
4
|
+
//
|
|
5
|
+
// 原来 `QUOTAS` 是一张按 plan 分档的表:free 2/3、pro 10/20、admin 无限。
|
|
6
|
+
// `pro` 那一行**没有任何路径能到达**(没有代码把 plan 设成 pro),
|
|
7
|
+
// 而它对自部署的人可见 —— 报错写的是「max 2 networks for free plan」。
|
|
8
|
+
// 在纯开源语境里这句话无法解释:哪来的套餐、谁在收费。
|
|
9
|
+
//
|
|
10
|
+
// ## 改成什么
|
|
11
|
+
//
|
|
12
|
+
// 默认上限仍是 2(**零行为变化**)
|
|
13
|
+
// 上限由 COMMHUB_MAX_NETWORKS_PER_USER 决定(非法值回落默认,不抛错)
|
|
14
|
+
// admin 仍然无限
|
|
15
|
+
// 报错文案自解释,且说出怎么改
|
|
16
|
+
// users.plan 列保留但不参与判定(不删列 = 不引入迁移风险)
|
|
17
|
+
import { afterAll, beforeAll, beforeEach, describe, expect, test } from "bun:test";
|
|
18
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
19
|
+
import { join } from "node:path";
|
|
20
|
+
import { tmpdir } from "node:os";
|
|
21
|
+
import { db } from "./db.js";
|
|
22
|
+
import { DEFAULT_MAX_NETWORKS_PER_USER, createNetwork, maxNetworksPerUser, register } from "./auth.js";
|
|
23
|
+
|
|
24
|
+
const DIR = mkdtempSync(join(tmpdir(), "anet-netquota-"));
|
|
25
|
+
const PW = "NetQuotaPassw0rd!";
|
|
26
|
+
let userId = "";
|
|
27
|
+
|
|
28
|
+
beforeAll(() => {
|
|
29
|
+
process.env.COMMHUB_DB ||= join(DIR, "hub.db");
|
|
30
|
+
// 🔴 不要动「第一个用户」:同一进程里多个测试文件共用 `process.env.COMMHUB_DB ||= …`
|
|
31
|
+
// 的那个库,把首用户降级会让别的文件的 `expect(admin.role).toBe("admin")` 红。
|
|
32
|
+
// ⇒ 本文件另注册一个**只属于自己**的用户来做配额实验。
|
|
33
|
+
register(`nq_seed_${Date.now()}`, PW); // 首用户(别碰它的角色)
|
|
34
|
+
const r = register(`nq_${Date.now() + 1}`, PW); // 本文件用的用户
|
|
35
|
+
userId = r.user!.user_id;
|
|
36
|
+
db.run("UPDATE users SET role = 'member' WHERE user_id = ?1", [userId]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
afterAll(() => {
|
|
40
|
+
delete process.env.COMMHUB_MAX_NETWORKS_PER_USER;
|
|
41
|
+
try { rmSync(DIR, { recursive: true, force: true }); } catch {}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// 每例前把环境变量清干净:它是**进程级**的,前一例留下的值会让后一例的「默认」不是默认。
|
|
45
|
+
beforeEach(() => { delete process.env.COMMHUB_MAX_NETWORKS_PER_USER; });
|
|
46
|
+
|
|
47
|
+
describe("#2980 每用户网络数上限", () => {
|
|
48
|
+
test("默认值是 2,且环境变量缺省/空串时都用默认", () => {
|
|
49
|
+
expect(DEFAULT_MAX_NETWORKS_PER_USER).toBe(2);
|
|
50
|
+
delete process.env.COMMHUB_MAX_NETWORKS_PER_USER;
|
|
51
|
+
expect(maxNetworksPerUser()).toBe(2);
|
|
52
|
+
process.env.COMMHUB_MAX_NETWORKS_PER_USER = "";
|
|
53
|
+
expect(maxNetworksPerUser()).toBe(2);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("非法值回落到默认,而不是让 Hub 起不来", () => {
|
|
57
|
+
for (const bad of ["abc", "-1", "NaN", "1.5x"]) {
|
|
58
|
+
process.env.COMMHUB_MAX_NETWORKS_PER_USER = bad;
|
|
59
|
+
expect(maxNetworksPerUser()).toBe(DEFAULT_MAX_NETWORKS_PER_USER);
|
|
60
|
+
}
|
|
61
|
+
process.env.COMMHUB_MAX_NETWORKS_PER_USER = "0";
|
|
62
|
+
expect(maxNetworksPerUser()).toBe(0); // 0 是合法值(禁止自建),不回落到 2
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("达到上限时的报错**不含套餐词**,且说出怎么改", () => {
|
|
66
|
+
delete process.env.COMMHUB_MAX_NETWORKS_PER_USER;
|
|
67
|
+
// 基线相对:register() 可能已经给这个用户建过网络,所以先数现有条数。
|
|
68
|
+
const owned = () => Number(db.get<{ n: number }>(
|
|
69
|
+
"SELECT COUNT(*) AS n FROM networks WHERE owner_id = ?1", userId)!.n);
|
|
70
|
+
const base = owned();
|
|
71
|
+
let last: any = { ok: true };
|
|
72
|
+
for (let i = base; i < DEFAULT_MAX_NETWORKS_PER_USER; i += 1) {
|
|
73
|
+
last = createNetwork(userId, `nq-fill-${i}`);
|
|
74
|
+
expect(last.ok).toBe(true);
|
|
75
|
+
}
|
|
76
|
+
expect(owned()).toBe(DEFAULT_MAX_NETWORKS_PER_USER);
|
|
77
|
+
last = createNetwork(userId, "nq-over");
|
|
78
|
+
expect(last.ok).toBe(false);
|
|
79
|
+
expect(last.error).toContain("COMMHUB_MAX_NETWORKS_PER_USER"); // 可操作
|
|
80
|
+
// 「套餐」这套说辞在开源版里没有对应物 ⇒ 文案里不许出现
|
|
81
|
+
expect(last.error).not.toContain("free");
|
|
82
|
+
expect(last.error).not.toContain("pro plan");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("上限可调:调大之后能继续建,调到正好时再建就拒", () => {
|
|
86
|
+
const owned = () => Number(db.get<{ n: number }>(
|
|
87
|
+
"SELECT COUNT(*) AS n FROM networks WHERE owner_id = ?1", userId)!.n);
|
|
88
|
+
const now = owned();
|
|
89
|
+
process.env.COMMHUB_MAX_NETWORKS_PER_USER = String(now + 1);
|
|
90
|
+
expect(createNetwork(userId, "nq-third-ok").ok).toBe(true);
|
|
91
|
+
expect(createNetwork(userId, "nq-fourth").ok).toBe(false);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("admin 不受上限约束(与改动前一致)", () => {
|
|
95
|
+
db.run("UPDATE users SET role = 'admin' WHERE user_id = ?1", [userId]);
|
|
96
|
+
process.env.COMMHUB_MAX_NETWORKS_PER_USER = "0";
|
|
97
|
+
expect(createNetwork(userId, "nq-admin-ok").ok).toBe(true);
|
|
98
|
+
db.run("UPDATE users SET role = 'member' WHERE user_id = ?1", [userId]);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// #993 first slice — network-scoped rules: GET / PUT /api/networks/:id/rules
|
|
2
|
+
//
|
|
3
|
+
// The contract this fixes:
|
|
4
|
+
// * any member may READ (the rules are what nodes obey);
|
|
5
|
+
// * only owner/admin may WRITE, and only with a matching `expected_revision`
|
|
6
|
+
// (blind writes would let two people overwrite the document every node inherits);
|
|
7
|
+
// * node tokens cannot write (user_token_required), and non-members cannot read;
|
|
8
|
+
// * reads before any write report revision 0 with empty content, so a client can
|
|
9
|
+
// create with `expected_revision: 0` and no special "create" path;
|
|
10
|
+
// * size is capped in UTF-8 bytes.
|
|
11
|
+
import { describe, expect, test, beforeAll, afterAll } from "bun:test";
|
|
12
|
+
import "./require-explicit-test-db.js";
|
|
13
|
+
import { register } from "./auth.js";
|
|
14
|
+
import { db } from "./db.js";
|
|
15
|
+
|
|
16
|
+
let BASE = "";
|
|
17
|
+
let server: any = null;
|
|
18
|
+
let adminToken = "";
|
|
19
|
+
let memberToken = "";
|
|
20
|
+
let netId = "";
|
|
21
|
+
let nodeToken = "";
|
|
22
|
+
|
|
23
|
+
async function send(token: string, method: string, path: string, payload?: unknown) {
|
|
24
|
+
const res = await fetch(`${BASE}${path}`, {
|
|
25
|
+
method,
|
|
26
|
+
headers: { ...(token ? { Authorization: `Bearer ${token}` } : {}), "Content-Type": "application/json" },
|
|
27
|
+
body: payload === undefined ? undefined : JSON.stringify(payload),
|
|
28
|
+
});
|
|
29
|
+
const text = await res.text();
|
|
30
|
+
let body: any = null;
|
|
31
|
+
try { body = JSON.parse(text); } catch {}
|
|
32
|
+
return { status: res.status, body };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
beforeAll(async () => {
|
|
36
|
+
process.env.HOST = "127.0.0.1";
|
|
37
|
+
const suffix = `${Date.now()}_${Math.floor(Math.random() * 1000)}`;
|
|
38
|
+
const pw = "NetworkRulesPw123Aa!";
|
|
39
|
+
const a = register(`nr_admin_${suffix}`, pw, undefined, "admin");
|
|
40
|
+
if (!a.ok) throw new Error("admin register failed");
|
|
41
|
+
adminToken = a.token!;
|
|
42
|
+
netId = a.network_id!;
|
|
43
|
+
db.run("UPDATE users SET role = 'admin' WHERE user_id = ?1", [a.user!.user_id]);
|
|
44
|
+
|
|
45
|
+
// The server must be listening before any HTTP call below.
|
|
46
|
+
const { bootServer } = await import("./server.js");
|
|
47
|
+
server = bootServer({ port: 0, hostname: "127.0.0.1" });
|
|
48
|
+
BASE = `http://127.0.0.1:${server.port}`;
|
|
49
|
+
|
|
50
|
+
const m = await send(adminToken, "POST", "/api/admin/users", { username: `nr_member_${suffix}`, password: pw, network_id: netId, role: "member" });
|
|
51
|
+
expect(m.status).toBe(200);
|
|
52
|
+
const login = await send("", "POST", "/api/auth/login", { username: `nr_member_${suffix}`, password: pw });
|
|
53
|
+
expect(login.status).toBe(200);
|
|
54
|
+
memberToken = login.body.token;
|
|
55
|
+
|
|
56
|
+
const { createNetworkTokenForNode } = await import("./auth.js");
|
|
57
|
+
const nt = createNetworkTokenForNode(a.user!.user_id, netId, "rules-node");
|
|
58
|
+
expect(nt.ok).toBe(true);
|
|
59
|
+
nodeToken = nt.token!;
|
|
60
|
+
}, 30_000);
|
|
61
|
+
|
|
62
|
+
afterAll(() => { try { server?.stop?.(true); } catch {} });
|
|
63
|
+
|
|
64
|
+
describe("#993 — network rules storage + CAS write", () => {
|
|
65
|
+
test("a member reads an empty document as revision 0", async () => {
|
|
66
|
+
const r = await send(memberToken, "GET", `/api/networks/${netId}/rules`);
|
|
67
|
+
expect(r.status).toBe(200);
|
|
68
|
+
expect(r.body.rules).toMatchObject({ network_id: netId, content: "", revision: 0, updated_at: null, updated_by: null });
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("a plain member cannot write", async () => {
|
|
72
|
+
const r = await send(memberToken, "PUT", `/api/networks/${netId}/rules`, { content: "nope", expected_revision: 0 });
|
|
73
|
+
expect(r.status).toBe(403);
|
|
74
|
+
expect(r.body.error).toBe("owner/admin required");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("admin writes with expected_revision 0 and reads it back", async () => {
|
|
78
|
+
const w = await send(adminToken, "PUT", `/api/networks/${netId}/rules`, {
|
|
79
|
+
content: "# Team rules\n\n- be precise\n", expected_revision: 0,
|
|
80
|
+
});
|
|
81
|
+
expect(w.status).toBe(200);
|
|
82
|
+
expect(w.body.rules.revision).toBe(1);
|
|
83
|
+
expect(w.body.rules.updated_by.username).toBeTruthy();
|
|
84
|
+
const r = await send(memberToken, "GET", `/api/networks/${netId}/rules`);
|
|
85
|
+
expect(r.body.rules.content).toContain("be precise");
|
|
86
|
+
expect(r.body.rules.revision).toBe(1);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("a stale expected_revision is a 409, and nothing is overwritten", async () => {
|
|
90
|
+
const w = await send(adminToken, "PUT", `/api/networks/${netId}/rules`, { content: "clobber", expected_revision: 0 });
|
|
91
|
+
expect(w.status).toBe(409);
|
|
92
|
+
expect(w.body.error).toBe("rules_revision_conflict");
|
|
93
|
+
const r = await send(adminToken, "GET", `/api/networks/${netId}/rules`);
|
|
94
|
+
expect(r.body.rules.content).toContain("be precise");
|
|
95
|
+
expect(r.body.rules.revision).toBe(1);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("a node token cannot write (user_token_required)", async () => {
|
|
99
|
+
const r = await send(nodeToken, "PUT", `/api/networks/${netId}/rules`, { content: "x", expected_revision: 1 });
|
|
100
|
+
expect(r.status).toBe(403);
|
|
101
|
+
expect(r.body.error).toBe("user_token_required");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("oversized content is refused (bytes, not UTF-16 units)", async () => {
|
|
105
|
+
const r = await send(adminToken, "PUT", `/api/networks/${netId}/rules`, { content: "a".repeat(256 * 1024 + 1), expected_revision: 1 });
|
|
106
|
+
expect(r.status).toBe(413);
|
|
107
|
+
expect(r.body.error).toBe("rules_too_large");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test("re-writing identical bytes is idempotent (revision does not move, changed=false)", async () => {
|
|
111
|
+
// The next slice injects a managed block keyed by revision; a no-op save that
|
|
112
|
+
// bumped the number would make every node rewrite its file for nothing.
|
|
113
|
+
const before = (await send(adminToken, "GET", `/api/networks/${netId}/rules`)).body.rules;
|
|
114
|
+
const w = await send(adminToken, "PUT", `/api/networks/${netId}/rules`, {
|
|
115
|
+
content: before.content, expected_revision: before.revision,
|
|
116
|
+
});
|
|
117
|
+
expect(w.status).toBe(200);
|
|
118
|
+
expect(w.body.changed).toBe(false);
|
|
119
|
+
expect(w.body.rules.revision).toBe(before.revision);
|
|
120
|
+
// …and a real edit still moves it.
|
|
121
|
+
const w2 = await send(adminToken, "PUT", `/api/networks/${netId}/rules`, {
|
|
122
|
+
content: before.content + "\n- one more line\n", expected_revision: before.revision,
|
|
123
|
+
});
|
|
124
|
+
expect(w2.status).toBe(200);
|
|
125
|
+
expect(w2.body.changed).toBe(true);
|
|
126
|
+
expect(w2.body.rules.revision).toBe(before.revision + 1);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("the 256 KiB cap is the same ruler as write_node_rules_file", async () => {
|
|
130
|
+
// 200 KiB used to be the cap here while node rules allowed 256 KiB — the same text
|
|
131
|
+
// could fit in the Hub and then be refused at the node.
|
|
132
|
+
const atCap = "a".repeat(256 * 1024);
|
|
133
|
+
const rev = (await send(adminToken, "GET", `/api/networks/${netId}/rules`)).body.rules.revision;
|
|
134
|
+
const ok = await send(adminToken, "PUT", `/api/networks/${netId}/rules`, { content: atCap, expected_revision: rev });
|
|
135
|
+
expect(ok.status).toBe(200);
|
|
136
|
+
const rev2 = ok.body.rules.revision;
|
|
137
|
+
const over = await send(adminToken, "PUT", `/api/networks/${netId}/rules`, { content: atCap + "a", expected_revision: rev2 });
|
|
138
|
+
expect(over.status).toBe(413);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("deleting the network deletes its rules row (no orphans)", async () => {
|
|
142
|
+
const own = register(`nr_del_${Date.now()}`, "DelPw123Aa!", undefined, "del");
|
|
143
|
+
expect(own.ok).toBe(true);
|
|
144
|
+
const put = await send(own.token!, "PUT", `/api/networks/${own.network_id}/rules`, { content: "team rules", expected_revision: 0 });
|
|
145
|
+
expect(put.status).toBe(200);
|
|
146
|
+
const del = await send(own.token!, "DELETE", `/api/networks/${own.network_id}`);
|
|
147
|
+
expect(del.status).toBe(200);
|
|
148
|
+
const left = db.get<{ cnt: number }>("SELECT COUNT(*) as cnt FROM network_rules WHERE network_id = ?1", own.network_id!);
|
|
149
|
+
expect(left!.cnt).toBe(0);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test("a node token cannot read another network's rules", async () => {
|
|
153
|
+
// The node token belongs to a hub admin (that is how it was minted), so the
|
|
154
|
+
// hubAdmin branch alone would have allowed it. A node stays inside its network.
|
|
155
|
+
const other = register(`nr_net2_${Date.now()}`, "OtherPw123Aa!", undefined, "net2");
|
|
156
|
+
expect(other.ok).toBe(true);
|
|
157
|
+
const r = await send(nodeToken, "GET", `/api/networks/${other.network_id}/rules`);
|
|
158
|
+
expect(r.status).toBe(403);
|
|
159
|
+
expect(r.body.error).toBe("not_a_member");
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test("a non-member cannot read", async () => {
|
|
163
|
+
const other = register(`nr_other_${Date.now()}`, "OtherPw123Aa!", undefined, "other");
|
|
164
|
+
expect(other.ok).toBe(true);
|
|
165
|
+
const r = await send(other.token!, "GET", `/api/networks/${netId}/rules`);
|
|
166
|
+
expect(r.status).toBe(403);
|
|
167
|
+
expect(r.body.error).toBe("not_a_member");
|
|
168
|
+
});
|
|
169
|
+
});
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// #993 — network-scoped rules text.
|
|
2
|
+
//
|
|
3
|
+
// First slice: storage + a read/write API with a compare-and-swap revision and an
|
|
4
|
+
// audit entry. Inheritance (Hub → Daemon → node) is NOT implemented here: it needs
|
|
5
|
+
// a precedence and conflict story of its own, and shipping the storage first is
|
|
6
|
+
// what lets that be reviewed against real content.
|
|
7
|
+
//
|
|
8
|
+
// Deliberate choices:
|
|
9
|
+
// * one row per network, text is the whole document (no chunking) — a team rule
|
|
10
|
+
// file is a few kilobytes, and partial reads would make "which revision did I
|
|
11
|
+
// read" ambiguous;
|
|
12
|
+
// * `revision` starts at 1 for the first write; reads before any write report
|
|
13
|
+
// revision 0 and empty content, so a client can PUT with expected_revision 0
|
|
14
|
+
// without a special "create" path;
|
|
15
|
+
// * the size cap is measured in UTF-8 bytes, not UTF-16 units, because the
|
|
16
|
+
// content is a file the node will write to disk.
|
|
17
|
+
import type { DbAdapter } from "./db-adapter.js";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 256 KiB — deliberately the same ruler as `write_node_rules_file` (tools.ts), because
|
|
21
|
+
* these two texts are meant to end up next to each other on a node. Two different caps
|
|
22
|
+
* for the same kind of document is how "it fit in the Hub but not on the node" happens.
|
|
23
|
+
*/
|
|
24
|
+
export const NETWORK_RULES_MAX_BYTES = 256 * 1024;
|
|
25
|
+
|
|
26
|
+
export type NetworkRules = {
|
|
27
|
+
network_id: string;
|
|
28
|
+
content: string;
|
|
29
|
+
revision: number;
|
|
30
|
+
updated_at: string | null;
|
|
31
|
+
updated_by: { user_id: string; username: string | null } | null;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
type Row = { content: string; revision: number; updated_at: string; updated_by_json: string | null };
|
|
35
|
+
|
|
36
|
+
export function ensureNetworkRules(database: DbAdapter): void {
|
|
37
|
+
database.exec(`
|
|
38
|
+
CREATE TABLE IF NOT EXISTS network_rules (
|
|
39
|
+
network_id TEXT NOT NULL PRIMARY KEY,
|
|
40
|
+
content TEXT NOT NULL,
|
|
41
|
+
revision INTEGER NOT NULL,
|
|
42
|
+
updated_at TEXT NOT NULL,
|
|
43
|
+
updated_by_json TEXT
|
|
44
|
+
);
|
|
45
|
+
`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let ensured = false;
|
|
49
|
+
/** Idempotent: the table is created on first use, not on every request. */
|
|
50
|
+
export function ensureNetworkRulesOnce(database: DbAdapter): void {
|
|
51
|
+
if (ensured) return;
|
|
52
|
+
ensureNetworkRules(database);
|
|
53
|
+
ensured = true;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function current(database: DbAdapter, networkId: string): Row | null {
|
|
57
|
+
return database.get<Row>(
|
|
58
|
+
"SELECT content, revision, updated_at, updated_by_json FROM network_rules WHERE network_id = ?1",
|
|
59
|
+
networkId,
|
|
60
|
+
) ?? null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function readNetworkRules(database: DbAdapter, networkId: string): NetworkRules {
|
|
64
|
+
const row = current(database, networkId);
|
|
65
|
+
if (!row) return { network_id: networkId, content: "", revision: 0, updated_at: null, updated_by: null };
|
|
66
|
+
let updatedBy: NetworkRules["updated_by"] = null;
|
|
67
|
+
try {
|
|
68
|
+
const parsed = JSON.parse(row.updated_by_json ?? "null");
|
|
69
|
+
if (parsed && typeof parsed === "object" && typeof parsed.user_id === "string") {
|
|
70
|
+
updatedBy = { user_id: parsed.user_id, username: typeof parsed.username === "string" ? parsed.username : null };
|
|
71
|
+
}
|
|
72
|
+
} catch { /* a corrupt actor blob must not make the rules unreadable */ }
|
|
73
|
+
return {
|
|
74
|
+
network_id: networkId,
|
|
75
|
+
content: row.content,
|
|
76
|
+
revision: Number(row.revision) || 0,
|
|
77
|
+
updated_at: row.updated_at,
|
|
78
|
+
updated_by: updatedBy,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export type WriteResult =
|
|
83
|
+
| { ok: true; rules: NetworkRules; changed: boolean }
|
|
84
|
+
| { ok: false; status: number; error: "invalid_rules_content" | "rules_too_large" | "rules_revision_conflict" };
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Compare-and-swap write. `expectedRevision` is required: a blind PUT would let two
|
|
88
|
+
* clients overwrite each other, and this is the same document every node in the
|
|
89
|
+
* network would inherit.
|
|
90
|
+
*/
|
|
91
|
+
export function writeNetworkRules(
|
|
92
|
+
database: DbAdapter,
|
|
93
|
+
networkId: string,
|
|
94
|
+
content: unknown,
|
|
95
|
+
expectedRevision: unknown,
|
|
96
|
+
actor: { user_id: string; username: string | null },
|
|
97
|
+
now = new Date().toISOString(),
|
|
98
|
+
): WriteResult {
|
|
99
|
+
if (typeof content !== "string") return { ok: false, status: 400, error: "invalid_rules_content" };
|
|
100
|
+
if (typeof expectedRevision !== "number" || !Number.isSafeInteger(expectedRevision) || expectedRevision < 0) {
|
|
101
|
+
return { ok: false, status: 400, error: "invalid_rules_content" };
|
|
102
|
+
}
|
|
103
|
+
if (Buffer.byteLength(content, "utf8") > NETWORK_RULES_MAX_BYTES) {
|
|
104
|
+
return { ok: false, status: 413, error: "rules_too_large" };
|
|
105
|
+
}
|
|
106
|
+
const row = current(database, networkId);
|
|
107
|
+
const revision = row ? Number(row.revision) || 0 : 0;
|
|
108
|
+
if (revision !== expectedRevision) return { ok: false, status: 409, error: "rules_revision_conflict" };
|
|
109
|
+
// Idempotent revision: writing the same bytes must not mint a new revision.
|
|
110
|
+
// The next slice injects a managed block keyed by revision, so a no-op save that
|
|
111
|
+
// bumped the number would make every node rewrite its file for nothing.
|
|
112
|
+
if (row && row.content === content) {
|
|
113
|
+
return { ok: true, rules: readNetworkRules(database, networkId), changed: false };
|
|
114
|
+
}
|
|
115
|
+
const next = revision + 1;
|
|
116
|
+
const actorJson = JSON.stringify({ user_id: actor.user_id, username: actor.username });
|
|
117
|
+
if (row) {
|
|
118
|
+
database.run(
|
|
119
|
+
"UPDATE network_rules SET content = ?1, revision = ?2, updated_at = ?3, updated_by_json = ?4 WHERE network_id = ?5",
|
|
120
|
+
[content, next, now, actorJson, networkId],
|
|
121
|
+
);
|
|
122
|
+
} else {
|
|
123
|
+
database.run(
|
|
124
|
+
"INSERT INTO network_rules (network_id, content, revision, updated_at, updated_by_json) VALUES (?1, ?2, ?3, ?4, ?5)",
|
|
125
|
+
[networkId, content, next, now, actorJson],
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
return { ok: true, rules: readNetworkRules(database, networkId), changed: true };
|
|
129
|
+
}
|
package/src/network-scope.ts
CHANGED
|
@@ -11,13 +11,33 @@
|
|
|
11
11
|
|
|
12
12
|
import { db } from "./db.js";
|
|
13
13
|
import { getUserNetworkRole } from "./auth.js";
|
|
14
|
+
import { isAgentRestricted, restrictedNetworkIds, usernameIsAgentAlias, visibleAgents } from "./agent-access.js";
|
|
14
15
|
|
|
15
16
|
export type RestNetworkScope = {
|
|
16
17
|
networkId: string | null;
|
|
17
18
|
networkIds: string[] | null;
|
|
18
19
|
denied?: string;
|
|
20
|
+
// 多用户 Agent 权限:scope 里哪些网络对这个调用者是「只看授权 Agent」的。
|
|
21
|
+
// addNetworkScope 对这些网络 fail-closed(整网不返回);要按授权放行的路径
|
|
22
|
+
// 显式改用 addAgentNetworkScope / addOwnTrafficScope,人类数据用 addHumanNetworkScope。
|
|
23
|
+
agentRestriction?: { userId: string; username: string; networkIds: string[] };
|
|
19
24
|
};
|
|
20
25
|
|
|
26
|
+
type ScopeAuthCtx = { userId: string; networkId: string | null; username?: string | null };
|
|
27
|
+
|
|
28
|
+
function usernameOf(userId: string, known?: string | null): string {
|
|
29
|
+
if (known) return known;
|
|
30
|
+
return db.get<{ username: string }>("SELECT username FROM users WHERE user_id = ?1", userId)?.username ?? "";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function withAgentRestriction(scope: RestNetworkScope, authCtx: ScopeAuthCtx): RestNetworkScope {
|
|
34
|
+
const inScope = scope.networkId ? [scope.networkId] : (scope.networkIds ?? []);
|
|
35
|
+
if (inScope.length === 0) return scope;
|
|
36
|
+
const restricted = restrictedNetworkIds(authCtx.userId).filter((id) => inScope.includes(id));
|
|
37
|
+
if (restricted.length === 0) return scope;
|
|
38
|
+
return { ...scope, agentRestriction: { userId: authCtx.userId, username: usernameOf(authCtx.userId, authCtx.username), networkIds: restricted } };
|
|
39
|
+
}
|
|
40
|
+
|
|
21
41
|
export function getUserNetworkIds(userId: string): string[] {
|
|
22
42
|
// JOIN networks: deleteNetwork historically left network_members rows
|
|
23
43
|
// behind (fixed alongside PR #519, but legacy DBs still carry strays).
|
|
@@ -31,7 +51,7 @@ export function getUserNetworkIds(userId: string): string[] {
|
|
|
31
51
|
).map((row) => row.network_id);
|
|
32
52
|
}
|
|
33
53
|
|
|
34
|
-
export function resolveRestNetworkScope(requested: string | null, authCtx:
|
|
54
|
+
export function resolveRestNetworkScope(requested: string | null, authCtx: ScopeAuthCtx | null, isAdmin: boolean): RestNetworkScope {
|
|
35
55
|
// Legacy global token or open dev mode keeps the old global behavior.
|
|
36
56
|
if (!authCtx) return { networkId: requested || null, networkIds: null };
|
|
37
57
|
|
|
@@ -44,13 +64,41 @@ export function resolveRestNetworkScope(requested: string | null, authCtx: { use
|
|
|
44
64
|
if (requested) {
|
|
45
65
|
const role = getUserNetworkRole(authCtx.userId, requested);
|
|
46
66
|
if (!role) return { networkId: null, networkIds: [], denied: "access denied to requested network" };
|
|
47
|
-
return { networkId: requested, networkIds: null };
|
|
67
|
+
return withAgentRestriction({ networkId: requested, networkIds: null }, authCtx);
|
|
48
68
|
}
|
|
49
69
|
|
|
50
|
-
return { networkId: null, networkIds: getUserNetworkIds(authCtx.userId) };
|
|
70
|
+
return withAgentRestriction({ networkId: null, networkIds: getUserNetworkIds(authCtx.userId) }, authCtx);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function unrestrictedNetworks(scope: RestNetworkScope): string[] | null {
|
|
74
|
+
const excluded = scope.agentRestriction?.networkIds ?? [];
|
|
75
|
+
if (scope.networkId) return excluded.includes(scope.networkId) ? [] : [scope.networkId];
|
|
76
|
+
if (scope.networkIds) return scope.networkIds.filter((id) => !excluded.includes(id));
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function placeholders(params: any[], values: string[]): string {
|
|
81
|
+
const start = params.length;
|
|
82
|
+
params.push(...values);
|
|
83
|
+
return values.map((_, i) => `?${start + i + 1}`).join(", ");
|
|
51
84
|
}
|
|
52
85
|
|
|
86
|
+
/**
|
|
87
|
+
* 默认的网络作用域 —— 对受限成员的受限网络 **fail-closed**:整网不返回。
|
|
88
|
+
* 没被逐条审过、不知道按授权怎么放行的查询,默认走这里,受限成员什么都看不到。
|
|
89
|
+
*/
|
|
53
90
|
export function addNetworkScope(sql: string, params: any[], scope: RestNetworkScope, column = "network_id"): string {
|
|
91
|
+
if (!scope.agentRestriction) return addHumanNetworkScope(sql, params, scope, column);
|
|
92
|
+
const allowed = unrestrictedNetworks(scope) ?? [];
|
|
93
|
+
if (allowed.length === 0) return `${sql} AND 1=0`;
|
|
94
|
+
return `${sql} AND ${column} IN (${placeholders(params, allowed)})`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 人类数据(自己的私信收件箱、需求看板、项目、网络成员)的作用域:受限成员照常看得到
|
|
99
|
+
* 自己所在网络。只给已确认不含 Agent 内容的查询用。
|
|
100
|
+
*/
|
|
101
|
+
export function addHumanNetworkScope(sql: string, params: any[], scope: RestNetworkScope, column = "network_id"): string {
|
|
54
102
|
if (scope.networkId) {
|
|
55
103
|
sql += ` AND ${column} = ?${params.length + 1}`;
|
|
56
104
|
params.push(scope.networkId);
|
|
@@ -66,13 +114,184 @@ export function addNetworkScope(sql: string, params: any[], scope: RestNetworkSc
|
|
|
66
114
|
return sql;
|
|
67
115
|
}
|
|
68
116
|
|
|
117
|
+
type AgentColumns = { network?: string; alias: string; nodeId?: string };
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Agent 行(sessions / nodes)的作用域:不受限的网络整网放行;受限网络只放行授权的 Agent。
|
|
121
|
+
* mode="message" 时只放行 can_message 的授权。
|
|
122
|
+
*/
|
|
123
|
+
export function addAgentNetworkScope(sql: string, params: any[], scope: RestNetworkScope, cols: AgentColumns, mode: "see" | "message" = "see"): string {
|
|
124
|
+
const restriction = scope.agentRestriction;
|
|
125
|
+
if (!restriction) return addHumanNetworkScope(sql, params, scope, cols.network ?? "network_id");
|
|
126
|
+
const net = cols.network ?? "network_id";
|
|
127
|
+
const parts: string[] = [];
|
|
128
|
+
const allowed = unrestrictedNetworks(scope) ?? [];
|
|
129
|
+
if (allowed.length) parts.push(`${net} IN (${placeholders(params, allowed)})`);
|
|
130
|
+
for (const networkId of restriction.networkIds) {
|
|
131
|
+
const visible = visibleAgents(restriction.userId, networkId);
|
|
132
|
+
const aliases = mode === "message" ? visible.messageAliases : visible.aliases;
|
|
133
|
+
const nodeIds = mode === "message" ? visible.messageNodeIds : visible.nodeIds;
|
|
134
|
+
const conds: string[] = [];
|
|
135
|
+
if (aliases.length) conds.push(`${cols.alias} IN (${placeholders(params, aliases)})`);
|
|
136
|
+
if (cols.nodeId && nodeIds.length) conds.push(`${cols.nodeId} IN (${placeholders(params, nodeIds)})`);
|
|
137
|
+
if (conds.length === 0) continue;
|
|
138
|
+
parts.push(`(${net} = ${placeholders(params, [networkId])} AND (${conds.join(" OR ")}))`);
|
|
139
|
+
}
|
|
140
|
+
return parts.length ? `${sql} AND (${parts.join(" OR ")})` : `${sql} AND 1=0`;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
type TrafficColumns = { network?: string; from: string; to: string; fromNodeId?: string; toNodeId?: string };
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 任务 / inbox 这类「谁发给谁」的行:受限网络里只放行**自己**与**授权 Agent**之间的往来
|
|
147
|
+
* (from=自己 且 to=授权 Agent,或反过来)。别人(包括 owner)和同一个 Agent 的对话看不到。
|
|
148
|
+
*/
|
|
149
|
+
export function addOwnTrafficScope(sql: string, params: any[], scope: RestNetworkScope, cols: TrafficColumns): string {
|
|
150
|
+
const restriction = scope.agentRestriction;
|
|
151
|
+
if (!restriction) return addHumanNetworkScope(sql, params, scope, cols.network ?? "network_id");
|
|
152
|
+
const net = cols.network ?? "network_id";
|
|
153
|
+
const parts: string[] = [];
|
|
154
|
+
const allowed = unrestrictedNetworks(scope) ?? [];
|
|
155
|
+
if (allowed.length) parts.push(`${net} IN (${placeholders(params, allowed)})`);
|
|
156
|
+
if (restriction.username) {
|
|
157
|
+
for (const networkId of restriction.networkIds) {
|
|
158
|
+
// 用户名与本网络某个 Agent 的 alias 撞名时(入网时拦过,但 Agent 之后注册的 alias 拦不住),
|
|
159
|
+
// 「发给我的」无法和「发给那个 Agent 的」区分开 —— 这个网络的往来一律不放行。
|
|
160
|
+
if (usernameIsAgentAlias(networkId, restriction.username)) continue;
|
|
161
|
+
const visible = visibleAgents(restriction.userId, networkId);
|
|
162
|
+
// 先判空再生成占位符:side() 会往 params 里推值,推了又丢弃会让参数个数对不上。
|
|
163
|
+
const useNodeIds = !!(cols.fromNodeId && cols.toNodeId) && visible.nodeIds.length > 0;
|
|
164
|
+
if (visible.aliases.length === 0 && !useNodeIds) continue;
|
|
165
|
+
const side = (aliasCol: string, nodeCol?: string): string | null => {
|
|
166
|
+
const conds: string[] = [];
|
|
167
|
+
if (visible.aliases.length) conds.push(`${aliasCol} IN (${placeholders(params, visible.aliases)})`);
|
|
168
|
+
if (nodeCol && useNodeIds) conds.push(`${nodeCol} IN (${placeholders(params, visible.nodeIds)})`);
|
|
169
|
+
return conds.length ? `(${conds.join(" OR ")})` : null;
|
|
170
|
+
};
|
|
171
|
+
const toAgent = side(cols.to, cols.toNodeId)!;
|
|
172
|
+
const fromAgent = side(cols.from, cols.fromNodeId)!;
|
|
173
|
+
const me = placeholders(params, [restriction.username]);
|
|
174
|
+
parts.push(`(${net} = ${placeholders(params, [networkId])} AND ((${cols.from} = ${me} AND ${toAgent}) OR (${cols.to} = ${me} AND ${fromAgent})))`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return parts.length ? `${sql} AND (${parts.join(" OR ")})` : `${sql} AND 1=0`;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* #563 —— 「不同用户看到同一个节点,消息应该是一样的」。
|
|
182
|
+
*
|
|
183
|
+
* tasks 行(聊天窗口的历史 + 回复都在这张表)的作用域:受限网络里,只要成员**看得见** Agent N
|
|
184
|
+
* (直接授权或经组授权,见 visibleAgents),N 的整条时间线他都看得见 —— 不论是谁发的(包括 owner、
|
|
185
|
+
* 其他成员),也包括授权之前的历史。时间线 = 一端是看得见的 Agent,另一端是:
|
|
186
|
+
* - 另一个看得见的 Agent;或
|
|
187
|
+
* - 不是本网络任何 Agent 的名字(人类用户 / scheduler / hub 这类系统发送方)。
|
|
188
|
+
* 另一端是**看不见**的 Agent 的行一律不放行(不借时间线暴露看不见的节点)。
|
|
189
|
+
* 没有任何授权的成员什么都看不到(与之前一样);能不能**发**仍由 canMessageAgent 管。
|
|
190
|
+
* Agent 主动发给某个人的 user_inbox 私信不在这张表里,不受影响。
|
|
191
|
+
*
|
|
192
|
+
* 与 addOwnTrafficScope 的区别只在受限网络:那里只放行「自己 ↔ 授权 Agent」。inbox(投递队列)
|
|
193
|
+
* 仍走 addOwnTrafficScope。用户名与 Agent alias 撞名的网络照旧整网不放行(fail-closed)。
|
|
194
|
+
*/
|
|
195
|
+
export function addAgentTimelineScope(sql: string, params: any[], scope: RestNetworkScope, cols: Required<Pick<TrafficColumns, "from" | "to" | "fromNodeId" | "toNodeId">> & { network?: string }): string {
|
|
196
|
+
const restriction = scope.agentRestriction;
|
|
197
|
+
if (!restriction) return addHumanNetworkScope(sql, params, scope, cols.network ?? "network_id");
|
|
198
|
+
const net = cols.network ?? "network_id";
|
|
199
|
+
const parts: string[] = [];
|
|
200
|
+
const allowed = unrestrictedNetworks(scope) ?? [];
|
|
201
|
+
if (allowed.length) parts.push(`${net} IN (${placeholders(params, allowed)})`);
|
|
202
|
+
if (restriction.username) {
|
|
203
|
+
for (const networkId of restriction.networkIds) {
|
|
204
|
+
if (usernameIsAgentAlias(networkId, restriction.username)) continue;
|
|
205
|
+
const visible = visibleAgents(restriction.userId, networkId);
|
|
206
|
+
// 先判空再推参数:推了又不用的绑定参数会让 PG 报错(SQLite 会忽略)。
|
|
207
|
+
if (visible.aliases.length === 0 && visible.nodeIds.length === 0) continue;
|
|
208
|
+
const aliasIn = visible.aliases.length ? placeholders(params, visible.aliases) : null;
|
|
209
|
+
const nodeIn = visible.nodeIds.length ? placeholders(params, visible.nodeIds) : null;
|
|
210
|
+
const netP = placeholders(params, [networkId]);
|
|
211
|
+
const isVisible = (aliasCol: string, nodeCol: string): string => {
|
|
212
|
+
const conds: string[] = [];
|
|
213
|
+
if (aliasIn) conds.push(`${aliasCol} IN (${aliasIn})`);
|
|
214
|
+
if (nodeIn) conds.push(`${nodeCol} IN (${nodeIn})`);
|
|
215
|
+
return `(${conds.join(" OR ")})`;
|
|
216
|
+
};
|
|
217
|
+
// 「不是本网络任何 Agent」:没有 node_id,且 alias 不在本网络的 sessions / nodes 里。
|
|
218
|
+
const notAnAgent = (aliasCol: string, nodeCol: string): string =>
|
|
219
|
+
`(${nodeCol} IS NULL`
|
|
220
|
+
+ ` AND ${aliasCol} NOT IN (SELECT alias FROM sessions WHERE network_id = ${netP} AND alias IS NOT NULL)`
|
|
221
|
+
+ ` AND ${aliasCol} NOT IN (SELECT alias FROM nodes WHERE network_id = ${netP} AND alias IS NOT NULL))`;
|
|
222
|
+
const toV = isVisible(cols.to, cols.toNodeId);
|
|
223
|
+
const fromV = isVisible(cols.from, cols.fromNodeId);
|
|
224
|
+
const otherFrom = `(${fromV} OR ${notAnAgent(cols.from, cols.fromNodeId)})`;
|
|
225
|
+
const otherTo = `(${toV} OR ${notAnAgent(cols.to, cols.toNodeId)})`;
|
|
226
|
+
parts.push(`(${net} = ${netP} AND ((${toV} AND ${otherFrom}) OR (${fromV} AND ${otherTo})))`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return parts.length ? `${sql} AND (${parts.join(" OR ")})` : `${sql} AND 1=0`;
|
|
230
|
+
}
|
|
231
|
+
|
|
69
232
|
export function singleNetworkId(scope: RestNetworkScope): string | null {
|
|
70
233
|
if (scope.networkId) return scope.networkId;
|
|
71
234
|
if (scope.networkIds?.length === 1) return scope.networkIds[0];
|
|
72
235
|
return null;
|
|
73
236
|
}
|
|
74
237
|
|
|
238
|
+
/**
|
|
239
|
+
* Resolve one unambiguous network for a REST write.
|
|
240
|
+
*
|
|
241
|
+
* Admin read scope is intentionally global (`networkIds=null`), so it cannot
|
|
242
|
+
* by itself express that an admin happens to have exactly one membership.
|
|
243
|
+
* Writes must not inherit that ambiguity: use the sole real membership when
|
|
244
|
+
* one exists, and keep requiring an explicit network for zero or 2+.
|
|
245
|
+
*/
|
|
246
|
+
export function resolveRestWriteNetworkId(
|
|
247
|
+
scope: RestNetworkScope,
|
|
248
|
+
authCtx: { userId: string; networkId: string | null } | null,
|
|
249
|
+
isAdmin: boolean,
|
|
250
|
+
): string | null {
|
|
251
|
+
const scoped = singleNetworkId(scope);
|
|
252
|
+
if (scoped) return scoped;
|
|
253
|
+
if (!authCtx || !isAdmin) return null;
|
|
254
|
+
const memberships = getUserNetworkIds(authCtx.userId);
|
|
255
|
+
return memberships.length === 1 ? memberships[0] : null;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* 能不能往这个网络写**面向 Agent 的东西**(派任务、改节点、排程……)。
|
|
260
|
+
* 受限成员在受限网络里恒为 false(fail-closed);给授权 Agent 发任务的路径另用
|
|
261
|
+
* canMessageAgent 单独放行,人类侧的写入(私信、需求、上传)用 canRestWriteNetworkAsHuman。
|
|
262
|
+
*/
|
|
75
263
|
export function canRestWriteNetwork(authCtx: { userId: string; networkId: string | null } | null, networkId: string | null, isAdmin: boolean): boolean {
|
|
264
|
+
if (!canRestWriteNetworkAsHuman(authCtx, networkId, isAdmin)) return false;
|
|
265
|
+
if (!authCtx || isAdmin || !networkId) return true;
|
|
266
|
+
return !isAgentRestricted(authCtx.userId, networkId);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* 能不能改**节点自身的安全配置**(permission-mode / rest-allowlist / peer-allowlist)。
|
|
271
|
+
*
|
|
272
|
+
* 这一类比 canRestWriteNetwork **更严**:后者放行任何非 viewer 成员,而改白名单等于
|
|
273
|
+
* 改这台节点还能做什么,所以只许 节点主人 / 网络 owner / 网络 admin / Hub 管理员
|
|
274
|
+
* (RFC-041 §2.3 / §2.4)。
|
|
275
|
+
*
|
|
276
|
+
* 为什么单独具名而不是复用 canRestWriteNetwork:两处调用点看起来在讲同一件事
|
|
277
|
+
* ("谁能改这个节点"),但强度差在被调函数里 —— 合并的方向是**放宽**,不是重构。
|
|
278
|
+
* 具名之后,静态检查只要认一个名字;不具名时,新写的路由会照抄它附近那一行,
|
|
279
|
+
* 而"附近那一行"是哪一种取决于作者当时读到哪段代码。
|
|
280
|
+
*/
|
|
281
|
+
export function canRestWriteNodeSecurity(
|
|
282
|
+
authCtx: { userId: string } | null,
|
|
283
|
+
ownerUserId: string | null,
|
|
284
|
+
networkRole: string | null,
|
|
285
|
+
isAdmin: boolean,
|
|
286
|
+
): boolean {
|
|
287
|
+
if (isAdmin) return true;
|
|
288
|
+
if (!authCtx) return false;
|
|
289
|
+
if (ownerUserId && ownerUserId === authCtx.userId) return true;
|
|
290
|
+
return networkRole === "owner" || networkRole === "admin";
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** 旧的「成员且不是 viewer」判据,不看 Agent 授权。只给人类侧写入用。 */
|
|
294
|
+
export function canRestWriteNetworkAsHuman(authCtx: { userId: string; networkId: string | null } | null, networkId: string | null, isAdmin: boolean): boolean {
|
|
76
295
|
if (!authCtx) return true; // legacy global token or open dev mode
|
|
77
296
|
if (isAdmin) return true;
|
|
78
297
|
if (!networkId) return false;
|