@sleep2agi/commhub-server 0.9.0-preview.25 → 0.9.0-preview.250
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +129 -9
- package/bin/commhub.ts +93 -9
- package/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 +228 -0
- package/src/admin-networks-http.test.ts +88 -0
- package/src/adoption-candidates-http.test.ts +140 -0
- package/src/adoption-candidates.ts +158 -0
- package/src/agent-access-batch.test.ts +210 -0
- package/src/agent-access.ts +487 -0
- package/src/agent-acl-groups-http.test.ts +247 -0
- package/src/agent-acl-http.test.ts +675 -0
- package/src/agent-acl-slice3-http.test.ts +717 -0
- package/src/agent-department-http.test.ts +215 -0
- package/src/agent-groups-perf.test.ts +223 -0
- package/src/agent-notice.ts +48 -0
- package/src/agent-team-whoami-http.test.ts +98 -0
- package/src/agent-teams-http.test.ts +330 -0
- package/src/agent-teams.ts +244 -0
- package/src/alias-baseline-2571.test.ts +162 -0
- package/src/alias-baseline.ts +113 -0
- package/src/alias-filter.test.ts +72 -0
- package/src/alias-filter.ts +42 -0
- package/src/api-host-supervisors-can-create-nodes.test.ts +350 -0
- package/src/api-host-supervisors-fallback.test.ts +6 -7
- package/src/api-host-supervisors-runtime-readiness.test.ts +195 -0
- package/src/api-nodes-shape.test.ts +95 -4
- package/src/auth-password-change-cli-tokens-http.test.ts +423 -0
- package/src/auth-sessions-http.test.ts +260 -0
- package/src/auth-validate.test.ts +5 -1
- package/src/auth-weak-password-login.test.ts +176 -0
- package/src/auth.ts +609 -51
- package/src/avatar-validate.test.ts +52 -2
- package/src/avatar-validate.ts +35 -1
- package/src/backfill-node-ids-2664.test.ts +128 -0
- package/src/binding-generation-http.test.ts +132 -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-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 +262 -0
- package/src/clone-node.ts +289 -0
- package/src/codex-config-flags.ts +77 -0
- package/src/codex-fork-contract.ts +23 -0
- package/src/codex-fork-migration.test.ts +38 -0
- package/src/commhub-bin-args.test.ts +85 -0
- package/src/config-apply-validate.test.ts +188 -1
- package/src/config-apply-validate.ts +222 -3
- package/src/cors-origins.test.ts +73 -0
- package/src/create-node-blocked-gate.test.ts +356 -0
- package/src/create-node-network-id.test.ts +91 -0
- package/src/create-node-tool-schema.test.ts +49 -0
- package/src/create-node-unicode-name.test.ts +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 +445 -0
- package/src/daemon-provider-snapshots.ts +327 -0
- package/src/daemon-token-owner-http.test.ts +292 -0
- package/src/dashboard-slash-routing-http.test.ts +109 -0
- package/src/db-adapter-guard.test.ts +148 -0
- package/src/db-adapter-translate.test.ts +128 -0
- package/src/db-adapter.ts +377 -91
- package/src/db-timestamp.test.ts +38 -0
- package/src/db-timestamp.ts +15 -0
- package/src/db.ts +1264 -153
- package/src/delivered-without-push.test.ts +140 -0
- package/src/department-groups-http.test.ts +209 -0
- package/src/department-groups-sync-http.test.ts +328 -0
- package/src/department-groups.ts +434 -0
- package/src/department-heads-http.test.ts +350 -0
- package/src/department-heads.ts +226 -0
- package/src/departments-agent-count.test.ts +132 -0
- package/src/departments-http.test.ts +185 -0
- package/src/departments-mcp.test.ts +128 -0
- package/src/departments.ts +318 -0
- package/src/desktop-message-seam.test.ts +150 -0
- package/src/desktop-message.test.ts +368 -0
- package/src/dm-file-scope-http.test.ts +178 -0
- package/src/empty-node-sweeper.test.ts +104 -0
- package/src/empty-node-sweeper.ts +88 -0
- package/src/env-transport-gate-http.test.ts +240 -0
- package/src/env-transport-gate.ts +80 -0
- package/src/env-transport-policy.ts +56 -0
- package/src/etag-of.test.ts +57 -0
- package/src/event-loop-lag.test.ts +180 -0
- package/src/event-loop-lag.ts +201 -0
- package/src/expiry-counters.test.ts +111 -0
- package/src/expiry-counters.ts +93 -0
- package/src/ext-token-shared.test.ts +60 -0
- package/src/ext-validation-regression.test.ts +131 -0
- package/src/external-schedule-edits-http.test.ts +321 -0
- package/src/external-schedule-edits-timestamps.test.ts +64 -0
- package/src/external-schedule-edits.ts +327 -0
- package/src/file-download-authz-dev-open.test.ts +1 -1
- package/src/file-download-authz.test.ts +73 -18
- package/src/file-download-date-bucket.test.ts +8 -26
- 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 +483 -0
- package/src/group-messages.ts +297 -0
- package/src/health-anon-readers.ts +60 -0
- package/src/health-host-disk-2756.test.ts +71 -0
- package/src/health-redaction.test.ts +1541 -7
- 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 +641 -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 +310 -0
- package/src/inbox-count.ts +29 -0
- package/src/index.ts +6 -2
- package/src/license-state.test.ts +56 -0
- package/src/license-state.ts +48 -0
- package/src/list-host-supervisors.test.ts +15 -4
- package/src/mcp-context-cost-http.test.ts +127 -0
- package/src/mcp-inflight.test.ts +92 -0
- package/src/mcp-inflight.ts +38 -0
- package/src/mcp-projects-events-http.test.ts +165 -0
- package/src/mcp-route-label-http.test.ts +120 -0
- package/src/mcp-write-network-resolution.test.ts +435 -0
- package/src/member-audit-network-id-http.test.ts +93 -0
- package/src/member-presence-http.test.ts +210 -0
- package/src/messages-ack-by-agent-http.test.ts +142 -0
- package/src/migrate-sqlite-to-pg.test.ts +47 -0
- package/src/migrate-sqlite-to-pg.ts +263 -0
- package/src/model-auth-notify-http.test.ts +181 -0
- package/src/model-auth-notify.ts +116 -0
- package/src/network-name-http.test.ts +75 -0
- package/src/network-rules-http.test.ts +169 -0
- package/src/network-rules.ts +129 -0
- package/src/network-scope.ts +276 -0
- package/src/network-token-membership-http.test.ts +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-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 +347 -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.ts +121 -0
- package/src/node-health-passthrough.test.ts +115 -0
- package/src/node-health-store.ts +101 -0
- package/src/node-id-alias.test.ts +190 -0
- package/src/node-identity-conflict-http.test.ts +269 -0
- package/src/node-identity-conflict.ts +142 -0
- package/src/node-identity-mismatch.test.ts +74 -0
- package/src/node-in-flight-tasks.ts +31 -0
- package/src/node-lifecycle-controllable.test.ts +64 -0
- package/src/node-lifecycle-controllable.ts +53 -0
- package/src/node-lifecycle-read-http.test.ts +257 -0
- package/src/node-lifecycle-read.ts +124 -0
- package/src/node-logs-transport.test.ts +263 -0
- package/src/node-peer-allowlist.test.ts +114 -0
- package/src/node-permissions-http.test.ts +728 -0
- package/src/node-permissions.ts +494 -0
- package/src/node-request-retention.test.ts +281 -0
- package/src/node-request-retention.ts +120 -0
- package/src/node-secret-rest-http.test.ts +190 -0
- package/src/node-serviceability.test.ts +86 -0
- package/src/node-serviceability.ts +124 -0
- package/src/node-session-control-http.test.ts +310 -0
- package/src/node-session-control.ts +258 -0
- package/src/node-skills-transport.test.ts +212 -0
- package/src/node-token-identity.ts +35 -0
- package/src/node-token-ownership.ts +51 -0
- package/src/node-token-privilege-http.test.ts +158 -0
- package/src/not-found-api-http.test.ts +67 -0
- package/src/observer-avatar-http.test.ts +102 -9
- package/src/observer-push.test.ts +76 -0
- package/src/opencode-catalog-report-http.test.ts +139 -0
- package/src/opencode-v2-catalog.test.ts +268 -0
- package/src/opencode-v2-catalog.ts +356 -0
- package/src/package-version-consistency.test.ts +25 -0
- package/src/parent-source-marker.test.ts +50 -0
- package/src/password-network-tokens.ts +25 -0
- package/src/peer-reply-atomic.test.ts +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/project-dir-warn-clear-http.test.ts +132 -0
- package/src/project-viewer-can-http.test.ts +116 -0
- package/src/provider-tools-shape.test.ts +150 -0
- package/src/push.test.ts +20 -10
- package/src/push.ts +373 -9
- package/src/redact-tokens.test.ts +86 -0
- package/src/redact-tokens.ts +43 -0
- package/src/rejection-counters.test.ts +72 -0
- package/src/rejection-counters.ts +173 -0
- package/src/rename.ts +38 -10
- package/src/reply-target-derivation.test.ts +96 -0
- package/src/report-status-handover-capabilities-http.test.ts +124 -0
- package/src/report-status-host-schema.test.ts +235 -0
- package/src/report-status-resume-id-handover.test.ts +184 -0
- package/src/report-status-secret-keys.test.ts +140 -0
- package/src/require-explicit-test-db.ts +9 -0
- package/src/requirement-due-reminders-http.test.ts +620 -0
- package/src/requirement-due-reminders.ts +416 -0
- package/src/requirement-events-http.test.ts +318 -0
- package/src/requirement-events.test.ts +45 -0
- package/src/requirement-events.ts +144 -0
- package/src/requirement-notify.ts +165 -0
- package/src/requirement-participant-notify-http.test.ts +309 -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 +164 -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 +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 +475 -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 +285 -0
- package/src/scheduled-requirement-wake.ts +125 -0
- package/src/scheduled-run-terminal.test.ts +290 -0
- package/src/scheduled-skip-blocking-task.test.ts +120 -0
- package/src/scheduled-skip-path-backoff-2622.test.ts +98 -0
- package/src/scheduled-stuck-http.test.ts +316 -0
- package/src/scheduled-stuck-notice-cap.test.ts +80 -0
- package/src/scheduled-stuck.ts +219 -0
- package/src/scheduled-tasks-http.test.ts +679 -0
- package/src/scheduled-tasks.ts +959 -0
- package/src/scheduled-work-outcome-http.test.ts +154 -0
- package/src/scheduled-work-outcome.ts +50 -0
- package/src/scoped-secrets.ts +141 -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 +458 -0
- package/src/send_dedup.test.ts +54 -0
- package/src/send_dedup.ts +52 -6
- package/src/serve-error.test.ts +73 -0
- package/src/serve-error.ts +56 -0
- package/src/server.ts +5134 -503
- package/src/session-task-on-dispatch.ts +14 -0
- package/src/session-token-ownership-http.test.ts +122 -0
- package/src/shared/codex-inventory.ts +41 -0
- package/src/shared/daemon-provider-login.ts +31 -0
- package/src/shared/daemon-provider-snapshot.ts +52 -0
- package/src/shared/external-schedule-contract.ts +60 -0
- package/src/shared/node-name.test.ts +62 -0
- package/src/shared/node-name.ts +161 -0
- package/src/shared/probe-host-allowlist-drift.test.ts +45 -0
- package/src/shared/provider-application.ts +29 -0
- package/src/shared/provider-collection.ts +14 -0
- package/src/shared/provider-initialization.ts +8 -0
- package/src/shared/reserved-env-drift.test.ts +10 -0
- package/src/shared/scoped-secret-contract.ts +5 -0
- package/src/shared/task-content-limit-drift.test.ts +28 -0
- package/src/shared/task-content-limit.ts +25 -0
- package/src/side-thread-command-transport.test.ts +312 -0
- package/src/side-thread-command-transport.ts +318 -0
- package/src/side-thread-http-integration.test.ts +233 -0
- package/src/side-thread-http.ts +504 -0
- package/src/side-thread-production.test.ts +72 -0
- package/src/side-thread-production.ts +118 -0
- package/src/side-thread.test.ts +1067 -0
- package/src/side-thread.ts +2499 -0
- package/src/skillhub-http.test.ts +142 -0
- package/src/sqlite-close-checkpoint.test.ts +78 -0
- package/src/start-node.test.ts +265 -0
- package/src/stats-versions-http.test.ts +65 -0
- package/src/status-alias-filter-http.test.ts +106 -0
- package/src/status-alias-resolver-http.test.ts +163 -0
- package/src/status-alias-resolver.ts +19 -0
- package/src/status-node-id-filter-http.test.ts +161 -0
- package/src/status-read-cache-http.test.ts +229 -0
- package/src/status-read-cache.ts +129 -0
- package/src/stop-delete-node.test.ts +567 -1
- package/src/task-access-http.test.ts +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 +498 -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 +318 -0
- package/src/task-idempotency-mcp.test.ts +142 -0
- package/src/task-idempotency.test.ts +76 -0
- package/src/task-idempotency.ts +81 -0
- package/src/task-late-replies.ts +54 -0
- package/src/task-late-reply-http.test.ts +227 -0
- package/src/task-lifecycle-watcher-busy.test.ts +114 -0
- package/src/task-lifecycle-watcher.test.ts +209 -0
- package/src/task-lifecycle-watcher.ts +109 -0
- package/src/task-network-resolution-http.test.ts +140 -0
- package/src/task-offline-delivery-2668.test.ts +119 -0
- package/src/task-offline-delivery.ts +81 -0
- package/src/task-orphan-http.test.ts +287 -0
- package/src/task-orphan.ts +119 -0
- package/src/task-queue-ahead-http.test.ts +309 -0
- package/src/task-queue-ahead-scale.test.ts +236 -0
- package/src/task-queue-ahead.ts +186 -0
- package/src/task-rejection-log-http.test.ts +160 -0
- package/src/task-rejection-log.ts +61 -0
- package/src/task-sender-http.test.ts +143 -0
- package/src/task-stale-open-http.test.ts +292 -0
- package/src/task-stale-open.ts +103 -0
- package/src/tasks-pagination-http.test.ts +141 -0
- package/src/team-rules-status-http.test.ts +90 -0
- package/src/team-rules-status.test.ts +59 -0
- package/src/team-rules-status.ts +72 -0
- package/src/tmux-allowlist.test.ts +48 -0
- package/src/tool-audience-http.test.ts +230 -0
- package/src/tool-audience.ts +127 -0
- package/src/tools-list-json-schema.test.ts +94 -0
- package/src/tools.ts +4326 -420
- package/src/upload-early-reject-guard.test.ts +155 -0
- package/src/uploads-http.test.ts +86 -7
- package/src/uploads.ts +38 -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 +348 -0
- package/src/user-inbox-by-agent-http.test.ts +165 -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,494 @@
|
|
|
1
|
+
// RFC-041 第一阶段(board #487):节点(Agent)自己的权限 —— 先记录、后执行。
|
|
2
|
+
//
|
|
3
|
+
// 节点的权限 = 主人的权限 ∩ 节点的模式(nodes.permission_mode:normal 正常 / readonly 只读 / restricted 受限)。
|
|
4
|
+
//
|
|
5
|
+
// • 模式是主人主动设的,**设了就立刻生效**(不看开关):只读的节点写什么都拒(mode_readonly);
|
|
6
|
+
// 受限的节点只碰派给它的任务、派活只给主人授权的 Agent(mode_restricted_not_assigned / agent_not_granted_to_owner)。
|
|
7
|
+
// • 正常模式下的收紧(超出主人可见范围 beyond_owner_visibility、主人没被授权的 Agent agent_not_granted_to_owner、
|
|
8
|
+
// 只有人能做的事 human_only)由 Hub 开关 COMMHUB_NODE_PERMISSIONS 决定:
|
|
9
|
+
// log(默认)—— 照常放行,只把「本来会被拒」按 (节点, 路由, 原因, 小时) 合并记进 node_permission_log;
|
|
10
|
+
// enforce —— 记录并拒绝(REST 403 / MCP 错误 node_permission_denied + reason + hint);
|
|
11
|
+
// off —— 不判也不记(显式模式仍然生效)。
|
|
12
|
+
//
|
|
13
|
+
// 判定的入口:REST(requirements.ts、/api/task、/api/broadcast、/api/nodes/:id 写)、MCP(tools.ts 按工具名包一层)、
|
|
14
|
+
// SSE(/events/:session、/events/network/:id)。只对节点令牌(ntok_)生效;用户令牌一条不变。
|
|
15
|
+
// 只增不改:nodes 多一列 permission_mode(默认 normal),新表 node_permission_log。回滚到上一版安全:旧代码不读它们。
|
|
16
|
+
|
|
17
|
+
import { db } from "./db.js";
|
|
18
|
+
import { canMessageAgent, type AgentRef } from "./agent-access.js";
|
|
19
|
+
import { resolveNodeCaller } from "./create-node.js";
|
|
20
|
+
|
|
21
|
+
export type NodePermissionMode = "normal" | "readonly" | "restricted";
|
|
22
|
+
export const NODE_PERMISSION_MODES: readonly NodePermissionMode[] = ["normal", "readonly", "restricted"];
|
|
23
|
+
export type NodePermissionsFlag = "log" | "off" | "enforce";
|
|
24
|
+
export type NodePermissionReason =
|
|
25
|
+
| "beyond_owner_visibility"
|
|
26
|
+
| "agent_not_granted_to_owner"
|
|
27
|
+
| "mode_readonly"
|
|
28
|
+
| "mode_restricted_not_assigned"
|
|
29
|
+
| "human_only"
|
|
30
|
+
| "owner_unknown"
|
|
31
|
+
/** #2969 —— 主人给这个节点设了对端白名单,目标不在名单里。 */
|
|
32
|
+
| "peer_not_allowed"
|
|
33
|
+
/** #2969 后半 —— 主人在这个节点的接口白名单里没有列这个工具/路由。 */
|
|
34
|
+
| "tool_not_allowed"
|
|
35
|
+
/** #2969 续 —— 令牌解析不到节点行(常见成因:节点改过名、令牌名没跟着改)。
|
|
36
|
+
* 这类令牌**没有 nodes 行**,两张白名单都管不到它;先按「记录」模式跑,看有多少。 */
|
|
37
|
+
| "unbound_token"
|
|
38
|
+
/** #2969 续 —— 主人在这个节点的 REST 白名单里没有列这条写路由。 */
|
|
39
|
+
| "rest_not_allowed";
|
|
40
|
+
|
|
41
|
+
/** 显式模式的原因:不看开关,永远拒。 */
|
|
42
|
+
// 白名单和 readonly/restricted 同类:**主人主动设的,设了就立刻生效**,不看开关。
|
|
43
|
+
const MODE_REASONS: ReadonlySet<NodePermissionReason> = new Set(["mode_readonly", "mode_restricted_not_assigned", "peer_not_allowed", "tool_not_allowed", "rest_not_allowed"]);
|
|
44
|
+
|
|
45
|
+
// #2969 —— 节点级对端白名单:JSON 数组(alias 或 node_id);NULL = 不限制。
|
|
46
|
+
// 只增不改:旧代码不读这一列;设为 NULL 时行为与现在完全一致。
|
|
47
|
+
// #2969 后半 —— 接口白名单:JSON 数组(MCP 工具名);NULL = 不限制。
|
|
48
|
+
// #2969 续 —— REST 写接口白名单:JSON 数组(形如 "POST /api/task");NULL = 不限制。
|
|
49
|
+
// 只对**节点令牌的写方法**生效(GET / SSE 永不经过它,否则节点会变聋)。
|
|
50
|
+
try { db.exec("ALTER TABLE nodes ADD COLUMN rest_allowlist TEXT"); } catch (e: any) {
|
|
51
|
+
if (!/duplicate column|already exists/i.test(e?.message || "")) throw e;
|
|
52
|
+
}
|
|
53
|
+
try { db.exec("ALTER TABLE nodes ADD COLUMN tool_allowlist TEXT"); } catch (e: any) {
|
|
54
|
+
if (!/duplicate column|already exists/i.test(e?.message || "")) throw e;
|
|
55
|
+
}
|
|
56
|
+
try { db.exec("ALTER TABLE nodes ADD COLUMN peer_allowlist TEXT"); } catch (e: any) {
|
|
57
|
+
if (!/duplicate column|already exists/i.test(e?.message || "")) throw e;
|
|
58
|
+
}
|
|
59
|
+
try { db.exec("ALTER TABLE nodes ADD COLUMN permission_mode TEXT NOT NULL DEFAULT 'normal'"); } catch (e: any) {
|
|
60
|
+
if (!/duplicate column|already exists/i.test(e?.message || "")) throw e;
|
|
61
|
+
}
|
|
62
|
+
db.exec(`
|
|
63
|
+
CREATE TABLE IF NOT EXISTS node_permission_log (
|
|
64
|
+
network_id TEXT NOT NULL,
|
|
65
|
+
node_id TEXT NOT NULL,
|
|
66
|
+
route TEXT NOT NULL,
|
|
67
|
+
reason TEXT NOT NULL,
|
|
68
|
+
hour TEXT NOT NULL,
|
|
69
|
+
hits INTEGER NOT NULL DEFAULT 1,
|
|
70
|
+
sample TEXT,
|
|
71
|
+
PRIMARY KEY (network_id, node_id, route, reason, hour)
|
|
72
|
+
);
|
|
73
|
+
CREATE INDEX IF NOT EXISTS idx_node_permission_log_hour ON node_permission_log(network_id, hour);
|
|
74
|
+
`);
|
|
75
|
+
|
|
76
|
+
// #2823 —— 调用面计数。
|
|
77
|
+
//
|
|
78
|
+
// 为什么需要第二张表:`node_permission_log` 只记 **would-be denial**(`if (!verdict) return false`),
|
|
79
|
+
// 所以它回答不了「节点到底有没有在调这些受限工具」。生产实测 30 天 0 行,
|
|
80
|
+
// 分不清「没人越界」和「几乎没人碰过」——而这两者的结论完全相反(#525 的切 enforce 决策就卡在这)。
|
|
81
|
+
//
|
|
82
|
+
// 设计取舍:
|
|
83
|
+
// · 只记**到达闸的调用**(nodeDecide 的入参就是 classified 工具),不记全部 MCP 调用;
|
|
84
|
+
// · 与拒绝日志同一条纪律:按 (网络, 节点, 工具, 小时) 合并成一行 + hits,不逐次插行;
|
|
85
|
+
// · 同样的 30 天保留期与 20000 行上限,超限后只加已有的行;
|
|
86
|
+
// · 与 `node_permission_log` **分开**,绝不把放行样本混进拒绝统计(那会把既有报表口径改坏)。
|
|
87
|
+
db.exec(`
|
|
88
|
+
CREATE TABLE IF NOT EXISTS node_tool_calls (
|
|
89
|
+
network_id TEXT NOT NULL,
|
|
90
|
+
node_id TEXT NOT NULL,
|
|
91
|
+
tool TEXT NOT NULL,
|
|
92
|
+
hour TEXT NOT NULL,
|
|
93
|
+
hits INTEGER NOT NULL DEFAULT 1,
|
|
94
|
+
PRIMARY KEY (network_id, node_id, tool, hour)
|
|
95
|
+
);
|
|
96
|
+
CREATE INDEX IF NOT EXISTS idx_node_tool_calls_hour ON node_tool_calls(network_id, hour);
|
|
97
|
+
`);
|
|
98
|
+
|
|
99
|
+
let callLogFull = false;
|
|
100
|
+
/**
|
|
101
|
+
* 行数上限。**刻意比 `LOG_MAX_ROWS`(20 000) 高一个数量级**。
|
|
102
|
+
*
|
|
103
|
+
* 为什么不能沿用 20 000:这张表的键是 `(network, node, tool, hour)` —— 每过一个小时
|
|
104
|
+
* 就是一整批新键。生产实测(.184 上线 1 小时):75 行/小时 ⇒ 1 800 行/天,
|
|
105
|
+
* 20 000 ÷ 1 800 ≈ **11 天**,而保留期是 30 天。
|
|
106
|
+
*
|
|
107
|
+
* 撞上限的后果不是「变慢」,而是 `callLogFull` 只给已有行加次数、**不再插新行**,
|
|
108
|
+
* 于是新的一小时一行都写不进去 —— 计数**静默归零**,不报错、不告警。
|
|
109
|
+
* 而这张表正是 #525「要不要切 enforce」的判据,读到归零会把「没人调用」当成结论。
|
|
110
|
+
*
|
|
111
|
+
* 200 000 行 ≈ 26 MB(实测 91 行 = 12 KB),对这张表完全可接受;
|
|
112
|
+
* 按当前速率可覆盖 30 天保留期且留 2 倍余量。
|
|
113
|
+
*/
|
|
114
|
+
export const TOOL_CALL_MAX_ROWS = 200_000;
|
|
115
|
+
|
|
116
|
+
/** 计一次「到达权限闸」的工具调用。绝不影响请求本身 —— 出错只写日志。 */
|
|
117
|
+
export function recordNodeToolCall(id: NodeIdentity, tool: string): void {
|
|
118
|
+
try {
|
|
119
|
+
const now = new Date();
|
|
120
|
+
if (now.getTime() - lastPrune > PRUNE_EVERY_MS) {
|
|
121
|
+
pruneNodePermissionLog(now);
|
|
122
|
+
const calls = Number(db.get<{ n: number | string }>("SELECT COUNT(*) AS n FROM node_tool_calls")?.n ?? 0);
|
|
123
|
+
callLogFull = calls >= TOOL_CALL_MAX_ROWS;
|
|
124
|
+
db.run("DELETE FROM node_tool_calls WHERE hour < ?1", [hourOf(new Date(now.getTime() - LOG_RETENTION_DAYS * 86_400_000))]);
|
|
125
|
+
}
|
|
126
|
+
const hour = hourOf(now);
|
|
127
|
+
const name = tool.slice(0, 120);
|
|
128
|
+
const updated = db.run(
|
|
129
|
+
"UPDATE node_tool_calls SET hits = hits + 1 WHERE network_id = ?1 AND node_id = ?2 AND tool = ?3 AND hour = ?4",
|
|
130
|
+
[id.networkId, id.logKey, name, hour],
|
|
131
|
+
);
|
|
132
|
+
if (Number(updated?.changes ?? 0) > 0 || callLogFull) return;
|
|
133
|
+
db.run(
|
|
134
|
+
`INSERT INTO node_tool_calls (network_id, node_id, tool, hour, hits) VALUES (?1, ?2, ?3, ?4, 1)
|
|
135
|
+
ON CONFLICT(network_id, node_id, tool, hour) DO UPDATE SET hits = node_tool_calls.hits + 1`,
|
|
136
|
+
[id.networkId, id.logKey, name, hour],
|
|
137
|
+
);
|
|
138
|
+
} catch (e) {
|
|
139
|
+
console.error(`[node-permissions] tool-call counter failed: ${(e as Error).message}`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** 开关。未设 / 写错 → log(安全的默认:只记不拦)。每次现读:测试和运维改环境变量后无需重启模块。 */
|
|
144
|
+
export function nodePermissionsFlag(): NodePermissionsFlag {
|
|
145
|
+
const raw = (process.env.COMMHUB_NODE_PERMISSIONS ?? "").trim().toLowerCase();
|
|
146
|
+
return raw === "off" || raw === "enforce" ? raw : "log";
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export type NodeIdentity = {
|
|
150
|
+
networkId: string;
|
|
151
|
+
/** 绑定的节点;老的未绑定令牌 = null(按「正常」判,记录时用 token:<token_id>)。 */
|
|
152
|
+
nodeId: string | null;
|
|
153
|
+
logKey: string;
|
|
154
|
+
aliases: string[];
|
|
155
|
+
ownerUserId: string | null;
|
|
156
|
+
mode: NodePermissionMode;
|
|
157
|
+
/** #2969 —— 对端白名单(alias / node_id 都算命中)。null = 不限制。 */
|
|
158
|
+
peerAllowlist: string[] | null;
|
|
159
|
+
/** #2969 后半 —— 接口白名单(MCP 工具名)。null = 不限制。 */
|
|
160
|
+
toolAllowlist: string[] | null;
|
|
161
|
+
/** #2969 续 —— REST 写接口白名单(形如 "POST /api/task")。null = 不限制。 */
|
|
162
|
+
restAllowlist: string[] | null;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* #2969 续 —— 路径归一化成路由标签。
|
|
167
|
+
*
|
|
168
|
+
* 为什么要归一化:白名单里存的是 `POST /api/task` 这种**形状稳定的标签**,
|
|
169
|
+
* 而实际请求带的是具体 id(`PUT /api/nodes/n_abc123/attrs`)。不归一化,
|
|
170
|
+
* 白名单就只能写死某台节点的 id —— 换一台就失效。
|
|
171
|
+
*
|
|
172
|
+
* 判据只看段数,不看语义:**看起来像 id 的段换成 `:id`**。
|
|
173
|
+
* 「像 id」= 含下划线/连字符/数字,或纯数字,或 UUID 形状。
|
|
174
|
+
* 宁可多换(把 `checklist` 这种词留下来是因为它不含数字也不含分隔符)。
|
|
175
|
+
*/
|
|
176
|
+
const ID_SEGMENT_RE = /^(?:[a-z]+_[A-Za-z0-9_-]+|[A-Za-z0-9]+-[A-Za-z0-9-]+|\d+|[0-9a-f]{8,})$/i;
|
|
177
|
+
export function restRouteLabel(method: string, pathname: string): string {
|
|
178
|
+
const segs = pathname.split("/").filter(Boolean).map(seg => (ID_SEGMENT_RE.test(seg) ? ":id" : seg));
|
|
179
|
+
return `${method.toUpperCase()} /${segs.join("/")}`;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* #2969 续 —— REST 写白名单判定。
|
|
184
|
+
*
|
|
185
|
+
* 只在**设置了清单**时才判(NULL = 不限制,与 MCP 白名单同构);
|
|
186
|
+
* 两条 SSE 流永远不经过这里 —— 调用方只对写方法调用本函数。
|
|
187
|
+
*/
|
|
188
|
+
export function restAllowlistVerdict(id: NodeIdentity, label: string): Verdict {
|
|
189
|
+
if (!id.restAllowlist) return null;
|
|
190
|
+
return id.restAllowlist.includes(label) ? null : { reason: "rest_not_allowed", sample: label };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function normalizePeerAllowlist(raw: unknown): string[] | null {
|
|
194
|
+
if (typeof raw !== "string" || !raw.trim()) return null;
|
|
195
|
+
try {
|
|
196
|
+
const parsed = JSON.parse(raw);
|
|
197
|
+
if (!Array.isArray(parsed)) return null;
|
|
198
|
+
const out = parsed.filter((x): x is string => typeof x === "string" && x.trim().length > 0).map(x => x.trim());
|
|
199
|
+
return out.length ? out : null;
|
|
200
|
+
} catch { return null; }
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export const normalizeToolAllowlist = normalizePeerAllowlist;
|
|
204
|
+
|
|
205
|
+
function normalizeMode(raw: unknown): NodePermissionMode {
|
|
206
|
+
return raw === "readonly" || raw === "restricted" ? raw : "normal";
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** 节点令牌 → 节点身份(一次带索引的查询)。不是节点令牌 / 查不到 → null。 */
|
|
210
|
+
export function nodeIdentity(tokenId: string | null | undefined, networkId: string | null | undefined): NodeIdentity | null {
|
|
211
|
+
if (!tokenId || !networkId) return null;
|
|
212
|
+
const row = db.get<{ user_id: string | null; name: string | null }>(
|
|
213
|
+
`SELECT user_id, name FROM api_tokens WHERE token_id = ?1 AND network_id = ?2`,
|
|
214
|
+
tokenId, networkId,
|
|
215
|
+
);
|
|
216
|
+
if (!row) return null;
|
|
217
|
+
const resolved = resolveNodeCaller(tokenId);
|
|
218
|
+
// Revoked or not node-shaped: this lookup is not a second revocation gate.
|
|
219
|
+
if (!resolved.ok && resolved.reason === "not_a_node_token") {
|
|
220
|
+
const aliases = row.name?.startsWith("node:") ? [row.name.slice(5)] : [];
|
|
221
|
+
return {
|
|
222
|
+
networkId,
|
|
223
|
+
nodeId: null,
|
|
224
|
+
logKey: `token:${tokenId}`,
|
|
225
|
+
aliases,
|
|
226
|
+
ownerUserId: row.user_id,
|
|
227
|
+
mode: "normal",
|
|
228
|
+
peerAllowlist: null,
|
|
229
|
+
toolAllowlist: null,
|
|
230
|
+
restAllowlist: null,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
if (!resolved.ok || resolved.networkId !== networkId) {
|
|
234
|
+
return {
|
|
235
|
+
networkId,
|
|
236
|
+
nodeId: null,
|
|
237
|
+
logKey: `token:${tokenId}`,
|
|
238
|
+
aliases: [],
|
|
239
|
+
ownerUserId: row.user_id,
|
|
240
|
+
mode: "normal",
|
|
241
|
+
peerAllowlist: null,
|
|
242
|
+
toolAllowlist: null,
|
|
243
|
+
restAllowlist: null,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
if (resolved.kind === "unregistered") {
|
|
247
|
+
return {
|
|
248
|
+
networkId,
|
|
249
|
+
nodeId: null,
|
|
250
|
+
logKey: `token:${tokenId}`,
|
|
251
|
+
aliases: resolved.alias ? [resolved.alias] : [],
|
|
252
|
+
ownerUserId: row.user_id,
|
|
253
|
+
mode: "normal",
|
|
254
|
+
peerAllowlist: null,
|
|
255
|
+
toolAllowlist: null,
|
|
256
|
+
restAllowlist: null,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
const node = db.get<{ owner_user_id: string | null; permission_mode: string | null; peer_allowlist: string | null; tool_allowlist: string | null; rest_allowlist: string | null }>(
|
|
260
|
+
`SELECT owner_user_id, permission_mode, peer_allowlist, tool_allowlist, rest_allowlist FROM nodes WHERE node_id = ?1 AND network_id = ?2`,
|
|
261
|
+
resolved.nodeId, networkId,
|
|
262
|
+
);
|
|
263
|
+
return {
|
|
264
|
+
networkId,
|
|
265
|
+
nodeId: resolved.nodeId,
|
|
266
|
+
logKey: resolved.nodeId,
|
|
267
|
+
aliases: [resolved.alias],
|
|
268
|
+
ownerUserId: node?.owner_user_id || row.user_id || null,
|
|
269
|
+
mode: normalizeMode(node?.permission_mode),
|
|
270
|
+
peerAllowlist: normalizePeerAllowlist(node?.peer_allowlist),
|
|
271
|
+
toolAllowlist: normalizeToolAllowlist(node?.tool_allowlist),
|
|
272
|
+
restAllowlist: normalizePeerAllowlist(node?.rest_allowlist),
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export type Verdict = { reason: NodePermissionReason; sample?: string } | null;
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* 判一次:verdict=null → 放行;否则按原因和开关决定拒不拒,并记一行(off 时正常模式的原因不记)。
|
|
280
|
+
* 返回 true = 拒绝。
|
|
281
|
+
*/
|
|
282
|
+
export function nodeDecide(id: NodeIdentity, route: string, verdict: Verdict): boolean {
|
|
283
|
+
// #2823 —— 先计数,再判定。放行与拒绝都算一次「到达闸的调用」,
|
|
284
|
+
// 这样「受限工具被调了 N 次、其中本来会被拒 M 次」才可测;否则分母永远是 0。
|
|
285
|
+
recordNodeToolCall(id, route);
|
|
286
|
+
if (!verdict) return false;
|
|
287
|
+
const explicit = MODE_REASONS.has(verdict.reason);
|
|
288
|
+
const flag = nodePermissionsFlag();
|
|
289
|
+
if (!explicit && flag === "off") return false;
|
|
290
|
+
recordNodePermission(id, route, verdict.reason, verdict.sample);
|
|
291
|
+
return explicit || flag === "enforce";
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const HINTS: Record<NodePermissionReason, string> = {
|
|
295
|
+
beyond_owner_visibility: "this node acts with its owner's permissions, and its owner cannot see or edit this; ask the owner for access, or assign the task to this node",
|
|
296
|
+
agent_not_granted_to_owner: "this Agent has not been granted to this node's owner; ask a network admin to grant it",
|
|
297
|
+
mode_readonly: "this node is in read-only mode; the node's owner can switch it to normal in the app",
|
|
298
|
+
mode_restricted_not_assigned: "this node is in restricted mode and only touches tasks assigned to it; assign the task to this node, or have the owner switch it to normal",
|
|
299
|
+
human_only: "only people can do this (members, projects, permissions, providers, secrets, other nodes); use a user token",
|
|
300
|
+
owner_unknown: "this node's owner is unknown; recreate the node token from the owner's account",
|
|
301
|
+
peer_not_allowed: "this node is limited to an explicit peer allowlist; the target is not on it — ask the node's owner to add it, or clear the allowlist",
|
|
302
|
+
tool_not_allowed: "this node is limited to an explicit tool allowlist; this tool is not on it — ask the node's owner to add it, or clear the allowlist",
|
|
303
|
+
unbound_token: "this token does not resolve to a node (usually the node was renamed and the token was not); rename or bind the token, or issue a node-bound token",
|
|
304
|
+
rest_not_allowed: "this node is limited to an explicit REST write allowlist; this route is not on it — ask the node's owner to add it, or clear the allowlist",
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
export function nodePermissionDeniedBody(reason: NodePermissionReason, route: string): { ok: false; error: "node_permission_denied"; reason: NodePermissionReason; route: string; hint: string } {
|
|
308
|
+
return { ok: false, error: "node_permission_denied", reason, route, hint: HINTS[reason] };
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// ── 记录:按 (网络, 节点, 路由, 原因, 小时) 合并;有上限、会清理 ──
|
|
312
|
+
export const LOG_RETENTION_DAYS = 30;
|
|
313
|
+
export const LOG_MAX_ROWS = 20_000;
|
|
314
|
+
const PRUNE_EVERY_MS = 60 * 60 * 1000;
|
|
315
|
+
let lastPrune = 0;
|
|
316
|
+
let logFull = false;
|
|
317
|
+
|
|
318
|
+
function hourOf(now = new Date()): string {
|
|
319
|
+
return now.toISOString().slice(0, 13); // 2026-10-03T08
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** 清掉保留期外的行,并重新判断是否到了上限(到上限后只给已有的行加次数,不再新开行)。 */
|
|
323
|
+
export function pruneNodePermissionLog(now = new Date()): void {
|
|
324
|
+
const cutoff = hourOf(new Date(now.getTime() - LOG_RETENTION_DAYS * 86_400_000));
|
|
325
|
+
db.run("DELETE FROM node_permission_log WHERE hour < ?1", [cutoff]);
|
|
326
|
+
const n = Number(db.get<{ n: number | string }>("SELECT COUNT(*) AS n FROM node_permission_log")?.n ?? 0);
|
|
327
|
+
logFull = n >= LOG_MAX_ROWS;
|
|
328
|
+
lastPrune = now.getTime();
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export function recordNodePermission(id: NodeIdentity, route: string, reason: NodePermissionReason, sample?: string): void {
|
|
332
|
+
try {
|
|
333
|
+
const now = new Date();
|
|
334
|
+
if (now.getTime() - lastPrune > PRUNE_EVERY_MS) pruneNodePermissionLog(now);
|
|
335
|
+
const hour = hourOf(now);
|
|
336
|
+
const s = sample ? sample.slice(0, 200) : null;
|
|
337
|
+
const updated = db.run(
|
|
338
|
+
"UPDATE node_permission_log SET hits = hits + 1, sample = COALESCE(?6, sample) WHERE network_id = ?1 AND node_id = ?2 AND route = ?3 AND reason = ?4 AND hour = ?5",
|
|
339
|
+
[id.networkId, id.logKey, route.slice(0, 120), reason, hour, s],
|
|
340
|
+
);
|
|
341
|
+
if (Number(updated?.changes ?? 0) > 0 || logFull) return;
|
|
342
|
+
db.run(
|
|
343
|
+
`INSERT INTO node_permission_log (network_id, node_id, route, reason, hour, hits, sample) VALUES (?1, ?2, ?3, ?4, ?5, 1, ?6)
|
|
344
|
+
ON CONFLICT(network_id, node_id, route, reason, hour) DO UPDATE SET hits = node_permission_log.hits + 1`,
|
|
345
|
+
[id.networkId, id.logKey, route.slice(0, 120), reason, hour, s],
|
|
346
|
+
);
|
|
347
|
+
} catch (e) {
|
|
348
|
+
// 记录失败绝不影响请求本身。
|
|
349
|
+
console.error(`[node-permissions] log failed: ${(e as Error).message}`);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** Test-only:让下一次记录先清理一遍,并可改上限判断。 */
|
|
354
|
+
export function __resetNodePermissionLogStateForTest(): void { lastPrune = 0; logFull = false; }
|
|
355
|
+
|
|
356
|
+
// ── 规则 ──
|
|
357
|
+
|
|
358
|
+
/** 派活 / 发消息 / 重派给 target。发给自己永远可以。 */
|
|
359
|
+
export function dispatchVerdict(id: NodeIdentity, target: AgentRef): Verdict {
|
|
360
|
+
if ((target.alias && id.aliases.includes(target.alias)) || (target.nodeId && id.nodeId && target.nodeId === id.nodeId)) return null;
|
|
361
|
+
const sample = target.alias ?? target.nodeId ?? undefined;
|
|
362
|
+
// #2969 —— 主人设了对端白名单:不在名单上的一律拒(显式原因,不看开关)。
|
|
363
|
+
// 命中判定用 alias 或 node_id 任一即可 —— 主人按 alias 写、节点按 node_id 找,两边都要认。
|
|
364
|
+
if (id.peerAllowlist) {
|
|
365
|
+
const hit = (target.alias && id.peerAllowlist.includes(target.alias))
|
|
366
|
+
|| (target.nodeId && id.peerAllowlist.includes(target.nodeId));
|
|
367
|
+
if (!hit) return { reason: "peer_not_allowed", sample };
|
|
368
|
+
}
|
|
369
|
+
if (id.mode === "readonly") return { reason: "mode_readonly", sample };
|
|
370
|
+
if (!id.ownerUserId) return id.mode === "restricted" ? { reason: "mode_restricted_not_assigned", sample } : { reason: "owner_unknown" };
|
|
371
|
+
if (!canMessageAgent(id.ownerUserId, id.networkId, target)) {
|
|
372
|
+
// 受限是主人设的 → 必拒(模式原因);正常下是开关管的收紧。
|
|
373
|
+
return { reason: id.mode === "restricted" ? "mode_restricted_not_assigned" : "agent_not_granted_to_owner", sample };
|
|
374
|
+
}
|
|
375
|
+
return null;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* #2969 后半 —— 接口白名单判定。
|
|
380
|
+
*
|
|
381
|
+
* 🔴 `always` 类**永远放行**:get_inbox / ack_inbox / send_reply / report_status 这些是节点
|
|
382
|
+
* 自己的生命周期协议。把它们也挡掉,节点既不能干活、也不能报告自己干不了 —— 那会变成
|
|
383
|
+
* 一个比"没受限"更隐蔽的假活节点。
|
|
384
|
+
*/
|
|
385
|
+
export function toolAllowlistVerdict(id: NodeIdentity, tool: string, alwaysClass: boolean): Verdict {
|
|
386
|
+
if (!id.toolAllowlist || alwaysClass) return null;
|
|
387
|
+
return id.toolAllowlist.includes(tool) ? null : { reason: "tool_not_allowed", sample: tool };
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/** 广播:只读 / 受限都不行(会发给没派给它、也未必授权的 Agent)。 */
|
|
391
|
+
export function broadcastVerdict(id: NodeIdentity): Verdict {
|
|
392
|
+
// #2969 —— 设了对端白名单的节点不能广播:广播的收件人是「任意对端」,
|
|
393
|
+
// 与白名单的语义直接冲突(名单说「只能找这几个」,广播说「发给所有人」)。
|
|
394
|
+
if (id.peerAllowlist) return { reason: "peer_not_allowed" };
|
|
395
|
+
if (id.mode === "readonly") return { reason: "mode_readonly" };
|
|
396
|
+
if (id.mode === "restricted") return { reason: "mode_restricted_not_assigned" };
|
|
397
|
+
if (!id.ownerUserId) return { reason: "owner_unknown" };
|
|
398
|
+
return null;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/** 一般的写(管节点、提交技能、探测模型…):只读 / 受限拒;正常放行(由各自的既有规则管)。 */
|
|
402
|
+
export function writeVerdict(id: NodeIdentity, sample?: string): Verdict {
|
|
403
|
+
if (id.mode === "readonly") return { reason: "mode_readonly", sample };
|
|
404
|
+
if (id.mode === "restricted") return { reason: "mode_restricted_not_assigned", sample };
|
|
405
|
+
return null;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** 只有人能做的事。只读 / 受限下报模式原因(那是主人设的、必拒);正常下报 human_only。 */
|
|
409
|
+
export function humanOnlyVerdict(id: NodeIdentity, sample?: string): Verdict {
|
|
410
|
+
return writeVerdict(id, sample) ?? { reason: "human_only", sample };
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/** 订阅别的会话 / 整个网络的推送流:受限不行;正常按主人能不能看到那个 Agent。 */
|
|
414
|
+
export function streamVerdict(id: NodeIdentity, target: AgentRef | "network"): Verdict {
|
|
415
|
+
if (target !== "network" && ((target.alias && id.aliases.includes(target.alias)) || (target.nodeId && target.nodeId === id.nodeId))) return null;
|
|
416
|
+
if (id.mode === "restricted") return { reason: "mode_restricted_not_assigned", sample: target === "network" ? "network" : target.alias ?? undefined };
|
|
417
|
+
if (!id.ownerUserId) return { reason: "owner_unknown" };
|
|
418
|
+
if (target !== "network" && !canMessageAgent(id.ownerUserId, id.networkId, target)) {
|
|
419
|
+
return { reason: "beyond_owner_visibility", sample: target.alias ?? undefined };
|
|
420
|
+
}
|
|
421
|
+
return null;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// ── MCP:每个工具归一类(test 钉住「每个注册的工具都归了类」,新工具忘了归类会红) ──
|
|
425
|
+
export type ToolClass = "always" | "read" | "dispatch" | "broadcast" | "requirements" | "schedule" | "node_write" | "human_only";
|
|
426
|
+
export const NODE_TOOL_CLASS: Readonly<Record<string, ToolClass>> = {
|
|
427
|
+
daemon_provider_snapshot: "human_only",
|
|
428
|
+
pull_daemon_provider_snapshots: "always", ack_daemon_provider_snapshot: "always",
|
|
429
|
+
// 回复 / 上报 / 自己的收件箱 / 生命周期协议(节点自己的请求)—— 任何模式都能做
|
|
430
|
+
report_status: "always", report_completion: "always", get_inbox: "always", ack_inbox: "always",
|
|
431
|
+
mark_tasks_runtime_submitted: "always", mark_tasks_consumed: "always", send_reply: "always", send_peer_reply: "always", send_ack: "always",
|
|
432
|
+
get_config_update: "always", ack_config_update: "always", get_rules_file_request: "always", ack_rules_file_request: "always", get_rules_file_result: "always",
|
|
433
|
+
get_create_request: "always", ack_create_request: "always", get_stop_request: "always", ack_stop_request: "always",
|
|
434
|
+
get_start_request: "always", ack_start_request: "always", get_probe_request: "always", ack_probe_request: "always",
|
|
435
|
+
get_opencode_catalog_request: "always", ack_opencode_catalog: "always",
|
|
436
|
+
list_my_pending_create_requests: "always", list_my_pending_lifecycle_requests: "always", list_my_children: "always",
|
|
437
|
+
request_adopt_node: "human_only", unadopt_node: "human_only",
|
|
438
|
+
get_adopt_request: "always", ack_adopt_request: "always",
|
|
439
|
+
send_desktop_message: "always", // 发给人的私信,不是派活
|
|
440
|
+
requirement_tags: "requirements",
|
|
441
|
+
// 读
|
|
442
|
+
get_all_status: "read", get_session_status: "read", get_task: "read", list_tasks: "read", get_completions: "read", org_whoami: "read",
|
|
443
|
+
list_skills: "read", get_skill: "read", list_host_supervisors: "read", list_network_secrets: "read", list_providers: "read",
|
|
444
|
+
get_probe_results: "read", read_node_rules_file: "read", list_node_skills: "read", read_node_skill: "read", list_node_files: "read",
|
|
445
|
+
read_node_file: "read", tail_node_logs: "read",
|
|
446
|
+
// 派活
|
|
447
|
+
send_task: "dispatch", send_message: "dispatch", retry_task: "dispatch", reassign_task: "dispatch", cancel_task: "dispatch",
|
|
448
|
+
broadcast: "broadcast",
|
|
449
|
+
// 任务看板:在 requirements.ts 里按卡判(REST 与 MCP 同一处)
|
|
450
|
+
requirements_list: "requirements", requirements_people: "requirements", requirements_get: "requirements", requirements_create: "requirements",
|
|
451
|
+
// #1894 —— 组织架构:读是常规读,写由工具内部再判网络 owner/admin(普通成员/节点写会被拒并 audit)。
|
|
452
|
+
departments: "requirements",
|
|
453
|
+
requirements_update: "requirements", requirements_checklist_toggle: "requirements", requirements_upsert_by_external_ref: "requirements",
|
|
454
|
+
requirements_events: "requirements", requirements_comment: "requirements", projects_list: "requirements",
|
|
455
|
+
// 定时任务(#733):在 schedule-agent.ts 里按排程判(范围 / 目标 = send_task 的 dispatchVerdict / 配额)
|
|
456
|
+
schedule_list: "read", schedule_get: "read", schedule_runs: "read",
|
|
457
|
+
schedule_create: "schedule", schedule_update: "schedule", schedule_cancel: "schedule", schedule_run_now: "schedule",
|
|
458
|
+
schedule_batch_interval: "schedule",
|
|
459
|
+
// 写节点 / 技能 / 探测:只读、受限不行;正常由 RFC-036 等既有规则管
|
|
460
|
+
update_node_config: "node_write", write_node_rules_file: "node_write", restart_node: "node_write", create_node: "node_write", clone_node: "node_write",
|
|
461
|
+
stop_node: "node_write", delete_node: "node_write", start_node: "node_write", submit_skill: "node_write", probe_provider_model: "node_write",
|
|
462
|
+
// 只有人能做
|
|
463
|
+
upsert_network_secret: "human_only", upsert_provider: "human_only", update_provider: "human_only",
|
|
464
|
+
projects_create: "human_only", projects_update: "human_only", review_skill: "human_only",
|
|
465
|
+
read_opencode_catalog: "human_only", get_opencode_catalog: "human_only",
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* #2970 判据④ 演练发现的口径问题 —— `node_permission_log` 的行数**不等于**
|
|
470
|
+
* 「打开 enforce 后行为会改变多少次」。
|
|
471
|
+
*
|
|
472
|
+
* 演练现场(`.208`,独立实例):
|
|
473
|
+
*
|
|
474
|
+
* log 模式 projects_create → user_token_required (调用方**也是被拒的**)
|
|
475
|
+
* node_permission_log += 1(reason=human_only)
|
|
476
|
+
* enforce projects_create → node_permission_denied / human_only
|
|
477
|
+
*
|
|
478
|
+
* 也就是说:`projects_create` 在 log 模式下就已经被处理函数自己的「只有人能做的操作」
|
|
479
|
+
* 检查挡住了。**这一条计入 would-be denial,但它并不是 enforce 的增量。**
|
|
480
|
+
*
|
|
481
|
+
* 所以把原因码分三类,报表按类求和 —— 否则切 enforce 时拿到的是一份高估的影响面:
|
|
482
|
+
*
|
|
483
|
+
* explicit 显式原因(MODE_REASONS):**永远拒**,与开关无关 ⇒ 不计入影响面
|
|
484
|
+
* likely_already_blocked human_only:多数写路由另有一道「只有人能操作」的检查,
|
|
485
|
+
* log 模式下调用方本来就被拒 ⇒ 通常不是增量(逐路由不同,标而不减)
|
|
486
|
+
* changes_behavior 只有 enforce 才会拒的理由 ⇒ **这才是真切会变的**
|
|
487
|
+
*/
|
|
488
|
+
export type EnforceImpact = "explicit" | "likely_already_blocked" | "changes_behavior";
|
|
489
|
+
|
|
490
|
+
export function enforceImpactOf(reason: NodePermissionReason): EnforceImpact {
|
|
491
|
+
if (MODE_REASONS.has(reason)) return "explicit";
|
|
492
|
+
if (reason === "human_only") return "likely_already_blocked";
|
|
493
|
+
return "changes_behavior";
|
|
494
|
+
}
|