@sleep2agi/commhub-server 0.9.0-preview.2 → 0.9.0-preview.200
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +130 -10
- package/bin/commhub.ts +99 -11
- package/package.json +7 -7
- package/src/ack-create-request-transport.test.ts +182 -0
- package/src/ack-create-request.test.ts +374 -0
- package/src/admin-delete-network-http.test.ts +228 -0
- package/src/admin-networks-http.test.ts +88 -0
- package/src/adoption-candidates-http.test.ts +137 -0
- package/src/adoption-candidates.ts +158 -0
- package/src/agent-access-batch.test.ts +210 -0
- package/src/agent-access.ts +487 -0
- package/src/agent-acl-groups-http.test.ts +247 -0
- package/src/agent-acl-http.test.ts +659 -0
- package/src/agent-acl-slice3-http.test.ts +267 -0
- package/src/agent-department-http.test.ts +215 -0
- package/src/agent-groups-perf.test.ts +223 -0
- package/src/agent-notice.ts +48 -0
- package/src/agent-team-whoami-http.test.ts +98 -0
- package/src/agent-teams-http.test.ts +330 -0
- package/src/agent-teams.ts +244 -0
- package/src/alias-baseline-2571.test.ts +162 -0
- package/src/alias-baseline.ts +113 -0
- package/src/alias-filter.test.ts +72 -0
- package/src/alias-filter.ts +42 -0
- package/src/api-host-supervisors-can-create-nodes.test.ts +350 -0
- package/src/api-host-supervisors-fallback.test.ts +271 -0
- package/src/api-host-supervisors-runtime-readiness.test.ts +195 -0
- package/src/api-nodes-shape.test.ts +279 -0
- package/src/auth-password-change-cli-tokens-http.test.ts +423 -0
- package/src/auth-sessions-http.test.ts +250 -0
- package/src/auth-validate.test.ts +5 -1
- package/src/auth-weak-password-login.test.ts +176 -0
- package/src/auth.ts +611 -51
- package/src/avatar-validate.test.ts +163 -0
- package/src/avatar-validate.ts +103 -0
- package/src/backfill-node-ids-2664.test.ts +128 -0
- package/src/binding-generation-http.test.ts +121 -0
- package/src/blocked-exit-on-reply.test.ts +122 -0
- package/src/boot-side-effect.test.ts +124 -0
- package/src/broadcast-fanout-2721.test.ts +118 -0
- package/src/broadcast-fanout.ts +98 -0
- package/src/chain-attempt-dedup.test.ts +447 -0
- package/src/clone-node.test.ts +248 -0
- package/src/clone-node.ts +289 -0
- package/src/codex-config-flags.ts +77 -0
- package/src/codex-fork-contract.ts +23 -0
- package/src/codex-fork-migration.test.ts +38 -0
- package/src/commhub-bin-args.test.ts +85 -0
- package/src/config-apply-sec1.test.ts +107 -0
- package/src/config-apply-validate.test.ts +134 -1
- package/src/config-apply-validate.ts +111 -5
- package/src/create-node-blocked-gate.test.ts +356 -0
- package/src/create-node-network-id.test.ts +79 -0
- package/src/create-node-tool-schema.test.ts +49 -0
- package/src/create-node-unicode-name.test.ts +120 -0
- package/src/create-node-validate.test.ts +379 -0
- package/src/create-node-validate.ts +423 -0
- package/src/create-node-workdir.test.ts +289 -0
- package/src/create-node.test.ts +354 -0
- package/src/create-node.ts +411 -0
- package/src/daemon-provider-snapshots.test.ts +421 -0
- package/src/daemon-provider-snapshots.ts +327 -0
- package/src/daemon-token-owner-http.test.ts +236 -0
- package/src/dashboard-slash-routing-http.test.ts +109 -0
- package/src/db-adapter-guard.test.ts +148 -0
- package/src/db-adapter-translate.test.ts +128 -0
- package/src/db-adapter.ts +377 -91
- package/src/db-timestamp.test.ts +38 -0
- package/src/db-timestamp.ts +15 -0
- package/src/db.ts +1501 -152
- package/src/delivered-without-push.test.ts +140 -0
- package/src/department-groups-http.test.ts +209 -0
- package/src/department-groups-sync-http.test.ts +328 -0
- package/src/department-groups.ts +434 -0
- package/src/department-heads-http.test.ts +330 -0
- package/src/department-heads.ts +226 -0
- package/src/departments-agent-count.test.ts +129 -0
- package/src/departments-http.test.ts +185 -0
- package/src/departments-mcp.test.ts +128 -0
- package/src/departments.ts +318 -0
- package/src/desktop-message-seam.test.ts +150 -0
- package/src/desktop-message.test.ts +368 -0
- package/src/dm-file-scope-http.test.ts +178 -0
- package/src/empty-node-sweeper.test.ts +104 -0
- package/src/empty-node-sweeper.ts +88 -0
- package/src/env-transport-gate-http.test.ts +177 -0
- package/src/env-transport-gate.ts +74 -0
- package/src/env-transport-policy.ts +21 -0
- package/src/event-loop-lag.test.ts +139 -0
- package/src/event-loop-lag.ts +150 -0
- package/src/ext-token-shared.test.ts +60 -0
- package/src/ext-validation-regression.test.ts +131 -0
- package/src/external-schedule-edits-http.test.ts +321 -0
- package/src/external-schedule-edits-timestamps.test.ts +64 -0
- package/src/external-schedule-edits.ts +327 -0
- package/src/file-download-authz-dev-open.test.ts +135 -0
- package/src/file-download-authz.test.ts +404 -0
- package/src/file-download-date-bucket.test.ts +436 -0
- package/src/file-network-scope-dev-open.test.ts +97 -0
- package/src/file-network-scope.test.ts +732 -0
- package/src/group-membership-events-http.test.ts +264 -0
- package/src/group-messages-http.test.ts +482 -0
- package/src/group-messages.ts +297 -0
- package/src/health-host-disk-2756.test.ts +49 -0
- package/src/health-redaction.test.ts +685 -0
- package/src/host-supervisor-identity-http.test.ts +152 -0
- package/src/host-supervisor-identity.ts +92 -0
- package/src/host-supervisors-daemon-version.test.ts +137 -0
- package/src/http-gzip.test.ts +94 -0
- package/src/http-gzip.ts +112 -0
- package/src/hub-catalog-read-http.test.ts +187 -0
- package/src/hub-provider-config.test.ts +139 -0
- package/src/hub-provider-config.ts +105 -0
- package/src/hub-timestamp.test.ts +38 -0
- package/src/hub-timestamp.ts +27 -0
- package/src/human-dm.ts +215 -0
- package/src/human-inbox.ts +42 -0
- package/src/inbox-caller.ts +49 -0
- package/src/inbox-count-sse.test.ts +310 -0
- package/src/inbox-count.ts +29 -0
- package/src/index.ts +15 -2172
- package/src/lifecycle-guard.ts +50 -0
- package/src/list-host-supervisors.test.ts +315 -0
- package/src/mcp-context-cost-http.test.ts +127 -0
- package/src/mcp-inflight.test.ts +92 -0
- package/src/mcp-inflight.ts +38 -0
- package/src/mcp-projects-events-http.test.ts +162 -0
- package/src/mcp-route-label-http.test.ts +120 -0
- package/src/mcp-write-network-resolution.test.ts +432 -0
- package/src/member-audit-network-id-http.test.ts +93 -0
- package/src/member-presence-http.test.ts +210 -0
- package/src/messages-ack-by-agent-http.test.ts +142 -0
- package/src/migrate-sqlite-to-pg.test.ts +47 -0
- package/src/migrate-sqlite-to-pg.ts +263 -0
- package/src/model-auth-notify-http.test.ts +181 -0
- package/src/model-auth-notify.ts +116 -0
- package/src/network-name-http.test.ts +75 -0
- package/src/network-rules-http.test.ts +169 -0
- package/src/network-rules.ts +129 -0
- package/src/network-scope.ts +276 -0
- package/src/network-token-membership-http.test.ts +139 -0
- package/src/node-alias-control.ts +12 -0
- package/src/node-attrs-validate.ts +88 -0
- package/src/node-attrs.test.ts +258 -0
- package/src/node-caller-identity-http.test.ts +1083 -0
- package/src/node-create-request-status-http.test.ts +105 -0
- package/src/node-daemon-bindings.test.ts +132 -0
- package/src/node-daemon-bindings.ts +135 -0
- package/src/node-failure-alert.test.ts +150 -0
- package/src/node-failure-alert.ts +177 -0
- package/src/node-files-transport.test.ts +344 -0
- package/src/node-health-codex-login.test.ts +120 -0
- package/src/node-health-dispatch-http.test.ts +279 -0
- package/src/node-health-guard.ts +105 -0
- package/src/node-health-passthrough.test.ts +115 -0
- package/src/node-health-store.ts +101 -0
- package/src/node-id-alias.test.ts +190 -0
- package/src/node-identity-conflict-http.test.ts +269 -0
- package/src/node-identity-conflict.ts +142 -0
- package/src/node-identity-mismatch.test.ts +74 -0
- package/src/node-in-flight-tasks.ts +31 -0
- package/src/node-lifecycle-controllable.test.ts +64 -0
- package/src/node-lifecycle-controllable.ts +53 -0
- package/src/node-lifecycle-read-http.test.ts +250 -0
- package/src/node-lifecycle-read.ts +124 -0
- package/src/node-logs-transport.test.ts +263 -0
- package/src/node-peer-allowlist.test.ts +79 -0
- package/src/node-permissions-http.test.ts +345 -0
- package/src/node-permissions.ts +393 -0
- package/src/node-request-retention.test.ts +281 -0
- package/src/node-request-retention.ts +120 -0
- package/src/node-secret-rest-http.test.ts +190 -0
- package/src/node-serviceability.test.ts +86 -0
- package/src/node-serviceability.ts +124 -0
- package/src/node-session-control-http.test.ts +310 -0
- package/src/node-session-control.ts +258 -0
- package/src/node-skills-transport.test.ts +209 -0
- package/src/node-token-identity.ts +35 -0
- package/src/node-token-ownership.ts +51 -0
- package/src/node-token-privilege-http.test.ts +158 -0
- package/src/not-found-api-http.test.ts +67 -0
- package/src/observer-avatar-http.test.ts +442 -0
- package/src/observer-push.test.ts +252 -0
- package/src/opencode-catalog-report-http.test.ts +139 -0
- package/src/opencode-v2-catalog.test.ts +268 -0
- package/src/opencode-v2-catalog.ts +352 -0
- package/src/package-version-consistency.test.ts +25 -0
- package/src/parent-source-marker.test.ts +50 -0
- package/src/password-network-tokens.ts +25 -0
- package/src/peer-reply-atomic.test.ts +455 -0
- package/src/peer-reply-capability.test.ts +70 -0
- package/src/pg-worker.ts +87 -0
- package/src/probe-validate.test.ts +221 -0
- package/src/probe-validate.ts +104 -0
- package/src/probe.test.ts +80 -0
- package/src/probe.ts +200 -0
- package/src/project-dir-warn-clear-http.test.ts +132 -0
- package/src/project-viewer-can-http.test.ts +116 -0
- package/src/push.test.ts +20 -10
- package/src/push.ts +466 -13
- package/src/redact-tokens.test.ts +65 -0
- package/src/redact-tokens.ts +40 -0
- package/src/rename.ts +38 -10
- package/src/reply-target-derivation.test.ts +96 -0
- package/src/report-status-handover-capabilities-http.test.ts +124 -0
- package/src/report-status-host-schema.test.ts +235 -0
- package/src/report-status-resume-id-handover.test.ts +184 -0
- package/src/report-status-secret-keys.test.ts +140 -0
- package/src/require-explicit-test-db.ts +9 -0
- package/src/requirement-due-reminders-http.test.ts +620 -0
- package/src/requirement-due-reminders.ts +416 -0
- package/src/requirement-events-http.test.ts +268 -0
- package/src/requirement-events.test.ts +45 -0
- package/src/requirement-events.ts +144 -0
- package/src/requirement-notify.ts +165 -0
- package/src/requirement-participant-notify-http.test.ts +309 -0
- package/src/requirement-tag-ops-http.test.ts +425 -0
- package/src/requirement-tag-tree-http.test.ts +275 -0
- package/src/requirement-tag-tree.ts +127 -0
- package/src/requirement-tags.ts +102 -0
- package/src/requirements-abandoned-http.test.ts +236 -0
- package/src/requirements-comment-http.test.ts +167 -0
- package/src/requirements-completed-at-migration.test.ts +70 -0
- package/src/requirements-errors-mcp-http.test.ts +164 -0
- package/src/requirements-errors.ts +158 -0
- package/src/requirements-etag-http.test.ts +91 -0
- package/src/requirements-http.test.ts +709 -0
- package/src/requirements-last-event-http.test.ts +349 -0
- package/src/requirements-list-mcp-slim-http.test.ts +155 -0
- package/src/requirements-list-slim-http.test.ts +324 -0
- package/src/requirements-mcp.test.ts +163 -0
- package/src/requirements-migrate.ts +283 -0
- package/src/requirements-owner-strict-mcp-http.test.ts +145 -0
- package/src/requirements-people-http.test.ts +114 -0
- package/src/requirements-people-mcp-http.test.ts +231 -0
- package/src/requirements-people.ts +196 -0
- package/src/requirements-priority-lowest.test.ts +114 -0
- package/src/requirements-projects-parent-http.test.ts +123 -0
- package/src/requirements-search-http.test.ts +182 -0
- package/src/requirements-search.test.ts +32 -0
- package/src/requirements-search.ts +145 -0
- package/src/requirements-seq-http.test.ts +193 -0
- package/src/requirements-seq-migration.test.ts +63 -0
- package/src/requirements-start-date.test.ts +125 -0
- package/src/requirements-stats-http.test.ts +357 -0
- package/src/requirements-stats.ts +195 -0
- package/src/requirements-status-participants-http.test.ts +222 -0
- package/src/requirements-upgrade-e2e.test.ts +126 -0
- package/src/requirements-upgrade-migration.test.ts +98 -0
- package/src/requirements.ts +2027 -0
- package/src/resolve-port.test.ts +57 -0
- package/src/resolve-port.ts +44 -0
- package/src/response-charset.test.ts +130 -0
- package/src/rest-alias-network-fallback-http.test.ts +123 -0
- package/src/rest-alias-network-fallback.test.ts +60 -0
- package/src/rest-alias-network-fallback.ts +65 -0
- package/src/rest-explicit-columns-http.test.ts +266 -0
- package/src/rest-identity.test.ts +130 -0
- package/src/rest-identity.ts +141 -0
- package/src/rest-projections.ts +72 -0
- package/src/rest-read-network-real-chain.test.ts +97 -0
- package/src/rest-write-network-resolution.test.ts +133 -0
- package/src/restart-readiness-http.test.ts +121 -0
- package/src/restricted-files.ts +76 -0
- package/src/retention-first-sweep-quiet.test.ts +31 -0
- package/src/retention.test.ts +100 -40
- package/src/retention.ts +423 -104
- package/src/route-callers-http.test.ts +120 -0
- package/src/route-timing.test.ts +211 -0
- package/src/route-timing.ts +262 -0
- package/src/rules-file-duplicate-alias.test.ts +234 -0
- package/src/rules-file-session-target.test.ts +207 -0
- package/src/rules-file-transport.test.ts +237 -0
- package/src/runtime-label.test.ts +22 -0
- package/src/runtime-label.ts +21 -0
- package/src/runtime-readiness.ts +90 -0
- package/src/schedule-agent-mcp-http.test.ts +391 -0
- package/src/schedule-agent.ts +192 -0
- package/src/schedule-starved.test.ts +92 -0
- package/src/schedule-starved.ts +114 -0
- package/src/scheduled-backoff-http.test.ts +193 -0
- package/src/scheduled-backoff.ts +64 -0
- package/src/scheduled-doorbell-nodeless-session.test.ts +134 -0
- package/src/scheduled-failure-backoff-2622.test.ts +145 -0
- package/src/scheduled-failure-code.test.ts +73 -0
- package/src/scheduled-failure-code.ts +105 -0
- package/src/scheduled-failures-http.test.ts +279 -0
- package/src/scheduled-failures.ts +377 -0
- package/src/scheduled-noprogress-backoff-2622.test.ts +133 -0
- package/src/scheduled-offline-backoff-probe.test.ts +56 -0
- package/src/scheduled-offline.test.ts +131 -0
- package/src/scheduled-offline.ts +66 -0
- package/src/scheduled-outcome-guidance-http.test.ts +139 -0
- package/src/scheduled-outcome-guidance.ts +9 -0
- package/src/scheduled-reply-unread-http.test.ts +212 -0
- package/src/scheduled-requirement-wake-http.test.ts +279 -0
- package/src/scheduled-requirement-wake.ts +125 -0
- package/src/scheduled-run-terminal.test.ts +268 -0
- package/src/scheduled-skip-blocking-task.test.ts +120 -0
- package/src/scheduled-skip-path-backoff-2622.test.ts +98 -0
- package/src/scheduled-stuck-http.test.ts +312 -0
- package/src/scheduled-stuck-notice-cap.test.ts +80 -0
- package/src/scheduled-stuck.ts +219 -0
- package/src/scheduled-tasks-http.test.ts +679 -0
- package/src/scheduled-tasks.ts +959 -0
- package/src/scheduled-work-outcome-http.test.ts +154 -0
- package/src/scheduled-work-outcome.ts +50 -0
- package/src/scoped-secrets.ts +139 -0
- package/src/send-dedup-request-id.test.ts +229 -0
- package/src/send-reply-agent-warning.test.ts +202 -0
- package/src/send-reply-alias-optional.test.ts +131 -0
- package/src/send-reply-attachments.test.ts +458 -0
- package/src/send_dedup.test.ts +54 -0
- package/src/send_dedup.ts +52 -6
- package/src/serve-error.test.ts +38 -0
- package/src/serve-error.ts +27 -0
- package/src/server.ts +5997 -0
- package/src/session-task-on-dispatch.ts +14 -0
- package/src/session-token-ownership-http.test.ts +122 -0
- package/src/shared/codex-inventory.ts +41 -0
- package/src/shared/daemon-provider-login.ts +31 -0
- package/src/shared/daemon-provider-snapshot.ts +52 -0
- package/src/shared/external-schedule-contract.ts +60 -0
- package/src/shared/node-name.test.ts +62 -0
- package/src/shared/node-name.ts +161 -0
- package/src/shared/probe-host-allowlist-drift.test.ts +54 -0
- package/src/shared/probe-host-allowlist.ts +128 -0
- package/src/shared/provider-application.ts +29 -0
- package/src/shared/provider-collection.ts +14 -0
- package/src/shared/provider-initialization.ts +8 -0
- package/src/shared/reserved-env-drift.test.ts +53 -0
- package/src/shared/reserved-env.test.ts +35 -0
- package/src/shared/reserved-env.ts +31 -0
- package/src/shared/scoped-secret-contract.ts +5 -0
- package/src/shared/task-content-limit-drift.test.ts +28 -0
- package/src/shared/task-content-limit.ts +25 -0
- package/src/side-thread-command-transport.test.ts +312 -0
- package/src/side-thread-command-transport.ts +318 -0
- package/src/side-thread-http-integration.test.ts +233 -0
- package/src/side-thread-http.ts +504 -0
- package/src/side-thread-production.test.ts +72 -0
- package/src/side-thread-production.ts +118 -0
- package/src/side-thread.test.ts +1067 -0
- package/src/side-thread.ts +2499 -0
- package/src/skillhub-http.test.ts +108 -0
- package/src/sqlite-close-checkpoint.test.ts +78 -0
- package/src/start-node.test.ts +264 -0
- package/src/status-alias-filter-http.test.ts +106 -0
- package/src/status-alias-resolver-http.test.ts +163 -0
- package/src/status-alias-resolver.ts +19 -0
- package/src/status-node-id-filter-http.test.ts +161 -0
- package/src/status-read-cache-http.test.ts +229 -0
- package/src/status-read-cache.ts +122 -0
- package/src/stop-delete-node.test.ts +1286 -0
- package/src/task-access-http.test.ts +402 -0
- package/src/task-access.ts +420 -0
- package/src/task-activity.ts +60 -0
- package/src/task-auth-origin-http.test.ts +93 -0
- package/src/task-auth-origin.test.ts +24 -0
- package/src/task-auth-origin.ts +7 -0
- package/src/task-consumption.test.ts +482 -0
- package/src/task-diagnostic-http.test.ts +149 -0
- package/src/task-diagnostic.test.ts +115 -0
- package/src/task-diagnostic.ts +118 -0
- package/src/task-expiry-notice-http.test.ts +287 -0
- package/src/task-expiry-notice.ts +315 -0
- package/src/task-idempotency-mcp.test.ts +142 -0
- package/src/task-idempotency.test.ts +76 -0
- package/src/task-idempotency.ts +81 -0
- package/src/task-late-replies.ts +54 -0
- package/src/task-late-reply-http.test.ts +152 -0
- package/src/task-lifecycle-watcher-busy.test.ts +114 -0
- package/src/task-lifecycle-watcher.test.ts +204 -0
- package/src/task-lifecycle-watcher.ts +109 -0
- package/src/task-network-resolution-http.test.ts +140 -0
- package/src/task-offline-delivery-2668.test.ts +119 -0
- package/src/task-offline-delivery.ts +81 -0
- package/src/task-orphan-http.test.ts +287 -0
- package/src/task-orphan.ts +119 -0
- package/src/task-queue-ahead-http.test.ts +309 -0
- package/src/task-queue-ahead-scale.test.ts +236 -0
- package/src/task-queue-ahead.ts +186 -0
- package/src/task-rejection-log-http.test.ts +160 -0
- package/src/task-rejection-log.ts +49 -0
- package/src/task-sender-http.test.ts +143 -0
- package/src/task-stale-open-http.test.ts +290 -0
- package/src/task-stale-open.ts +103 -0
- package/src/tasks-pagination-http.test.ts +141 -0
- package/src/team-rules-status-http.test.ts +90 -0
- package/src/team-rules-status.test.ts +59 -0
- package/src/team-rules-status.ts +72 -0
- package/src/tool-audience-http.test.ts +230 -0
- package/src/tool-audience.ts +120 -0
- package/src/tools-list-json-schema.test.ts +94 -0
- package/src/tools.ts +5815 -348
- package/src/update-provider.test.ts +234 -0
- package/src/upload-early-reject-guard.test.ts +155 -0
- package/src/uploads-http.test.ts +97 -15
- package/src/uploads.test.ts +66 -0
- package/src/uploads.ts +138 -5
- package/src/user-avatar-http.test.ts +74 -0
- package/src/user-avatar-image.ts +160 -0
- package/src/user-avatar-migration.test.ts +25 -0
- package/src/user-avatar-upload-http.test.ts +348 -0
- package/src/user-inbox-by-agent-http.test.ts +161 -0
- package/src/user-inbox-netid-notnull.test.ts +122 -0
- package/src/user-inbox-read-http.test.ts +223 -0
- package/src/user-inbox-schema.test.ts +54 -0
- package/src/vault.test.ts +200 -0
- package/src/vault.ts +156 -0
- package/src/version-stats.test.ts +66 -0
- package/src/version-stats.ts +109 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// #2596 —— 组织架构页那些“重复名字”的真身:**注册失败留下的空白 node 行**。
|
|
2
|
+
//
|
|
3
|
+
// 生产实测(2026-10-27,只读):
|
|
4
|
+
// ```
|
|
5
|
+
// nodes 总数 312
|
|
6
|
+
// runtime / hostname / config_path 全为空、且没有任何 session 的行 = 12
|
|
7
|
+
// 形态高度一致:同一 alias 在几分钟内连着出现,最后一个才被填上真实身份
|
|
8
|
+
// TMA门户鲸2号 ×5 09-17 15:47:52 → 16:03:05 (6 分钟注册 5 次,第 6 次成功)
|
|
9
|
+
// ```
|
|
10
|
+
// ⇒ 注册流程先建行、节点再补身份;补不上的尝试留下永久空壳,被组织架构页当成 node 渲染。
|
|
11
|
+
//
|
|
12
|
+
// 这里只做**清理**,不动注册流程:
|
|
13
|
+
// · 只删「三个身份字段全空 + 没有任何 session」的行 —— 这种行不可能提供任何功能;
|
|
14
|
+
// · 只删 `created_at` 早于 N 天(默认 2 天)的行 —— 正在注册中的行自然不会被误删;
|
|
15
|
+
// (用 created_at 不用 updated_at:后者会被无关的批量更新刷新,见下方注释里的生产实例)
|
|
16
|
+
// · 删除前把整行导成 JSON 到日志(可人工恢复),并返回被删的 id 列表。
|
|
17
|
+
//
|
|
18
|
+
// 为什么不用「同 alias 只留一条」这种规则:不同机器共用 alias 是**数据问题不是脏数据**
|
|
19
|
+
// (Mac打包狗 / TM微软牛 / TM汇报牛 这类),删了会丢真实节点。空壳判据才安全。
|
|
20
|
+
|
|
21
|
+
import { db } from "./db.js";
|
|
22
|
+
|
|
23
|
+
export const EMPTY_NODE_AGE_DAYS_ENV = "COMMHUB_EMPTY_NODE_GC_DAYS";
|
|
24
|
+
export const EMPTY_NODE_AGE_DAYS_DEFAULT = 2;
|
|
25
|
+
|
|
26
|
+
export type EmptyNodeSweepResult = {
|
|
27
|
+
deleted: number;
|
|
28
|
+
ids: string[];
|
|
29
|
+
ageDays: number;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export function readEmptyNodeAgeDays(raw = process.env[EMPTY_NODE_AGE_DAYS_ENV]): number {
|
|
33
|
+
if (raw === undefined || raw === "") return EMPTY_NODE_AGE_DAYS_DEFAULT;
|
|
34
|
+
const n = Number(raw);
|
|
35
|
+
if (!Number.isFinite(n)) return EMPTY_NODE_AGE_DAYS_DEFAULT;
|
|
36
|
+
return Math.floor(n);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const IDENTITY_EMPTY = `COALESCE(runtime, '') = '' AND COALESCE(hostname, '') = '' AND COALESCE(config_path, '') = ''`;
|
|
40
|
+
|
|
41
|
+
export function sweepEmptyNodeRows(
|
|
42
|
+
opts: { olderThanDays?: number; dryRun?: boolean } = {},
|
|
43
|
+
): EmptyNodeSweepResult {
|
|
44
|
+
const ageDays = opts.olderThanDays ?? readEmptyNodeAgeDays();
|
|
45
|
+
// <=0 关闭(运维可以彻底停掉这个清理)
|
|
46
|
+
if (ageDays <= 0) return { deleted: 0, ids: [], ageDays: 0 };
|
|
47
|
+
|
|
48
|
+
const rows = db.all<{ node_id: string }>(
|
|
49
|
+
`SELECT n.node_id
|
|
50
|
+
FROM nodes n
|
|
51
|
+
WHERE ${IDENTITY_EMPTY.replace(/\b(runtime|hostname|config_path)\b/g, "n.$1")}
|
|
52
|
+
-- #2596 —— 用 created_at 而不是 updated_at:空壳行会被无关的批量更新
|
|
53
|
+
-- 把 updated_at 顶到「今天」(生产实测:2026-10-26 的一次批量更新把 08/09 月
|
|
54
|
+
-- 建的空壳全部刷成 10-26),那样 2 天的阈值就永远删不掉它们。
|
|
55
|
+
-- created_at 才是“这行空了多久”的稳定量;在注册中的行 created_at 必然是新的。
|
|
56
|
+
AND n.created_at < datetime('now', ?1)
|
|
57
|
+
AND NOT EXISTS (SELECT 1 FROM sessions s WHERE s.node_id = n.node_id)
|
|
58
|
+
LIMIT 500`,
|
|
59
|
+
`-${ageDays} days`,
|
|
60
|
+
);
|
|
61
|
+
if (rows.length === 0) return { deleted: 0, ids: [], ageDays };
|
|
62
|
+
|
|
63
|
+
const ids = rows.map((r) => r.node_id);
|
|
64
|
+
if (opts.dryRun) return { deleted: 0, ids, ageDays };
|
|
65
|
+
|
|
66
|
+
// 删前留证:整行 JSON 打到日志里(运维可据此恢复),再逐条删。
|
|
67
|
+
try {
|
|
68
|
+
const full = db.all<Record<string, unknown>>(
|
|
69
|
+
`SELECT * FROM nodes WHERE node_id IN (${ids.map((_, i) => `?${i + 1}`).join(",")})`,
|
|
70
|
+
ids,
|
|
71
|
+
);
|
|
72
|
+
console.log(`[commhub empty-node-gc] deleting ${ids.length} placeholder node row(s): ${JSON.stringify(full)}`);
|
|
73
|
+
} catch (e: any) {
|
|
74
|
+
console.log(`[commhub empty-node-gc] backup dump failed, aborting: ${e?.message ?? e}`);
|
|
75
|
+
return { deleted: 0, ids: [], ageDays };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let deleted = 0;
|
|
79
|
+
for (const id of ids) {
|
|
80
|
+
try {
|
|
81
|
+
const res = db.run(`DELETE FROM nodes WHERE node_id = ?1 AND ${IDENTITY_EMPTY}`, [id]);
|
|
82
|
+
deleted += res.changes ?? 0;
|
|
83
|
+
} catch (e: any) {
|
|
84
|
+
console.log(`[commhub empty-node-gc] delete ${id} failed: ${e?.message ?? e}`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return { deleted, ids, ageDays };
|
|
88
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// #1510 — the secret-transport gate, shipped as log-only first.
|
|
2
|
+
//
|
|
3
|
+
// The load-bearing property is symmetric: with the gate in log mode a write to a
|
|
4
|
+
// `plain` node MUST still succeed (nobody's workflow breaks on upgrade) but MUST
|
|
5
|
+
// leave a hit behind, and with `enforce` the same write MUST be refused. A
|
|
6
|
+
// half-implemented gate that logs but also refuses would break every plain-HTTP
|
|
7
|
+
// node on the next deploy; one that refuses but never logs leaves nothing to
|
|
8
|
+
// decide with.
|
|
9
|
+
import { afterAll, beforeAll, beforeEach, describe, expect, test } from "bun:test";
|
|
10
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
11
|
+
import { tmpdir } from "node:os";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
|
|
14
|
+
const PRIVATE_DB_DIR = mkdtempSync(join(tmpdir(), "anet-transport-gate-"));
|
|
15
|
+
process.env.COMMHUB_DB ||= join(PRIVATE_DB_DIR, "hub.db");
|
|
16
|
+
|
|
17
|
+
let net = "", ownerId = "", ownerToken = "";
|
|
18
|
+
let server: ReturnType<typeof Bun.serve>;
|
|
19
|
+
let base = "";
|
|
20
|
+
const PLAIN = "n_gate_plain";
|
|
21
|
+
const TLS = "n_gate_tls";
|
|
22
|
+
// #1510 acceptance names three transports: loopback passes, https passes, plain
|
|
23
|
+
// public is refused. log/enforce only ever *special-case* `plain`, so loopback
|
|
24
|
+
// could rot without anyone noticing it is still accepted — seed it as its own
|
|
25
|
+
// node rather than folding it in with TLS, so a regression reads by name.
|
|
26
|
+
const LOOPBACK = "n_gate_loopback";
|
|
27
|
+
|
|
28
|
+
const post = async (path: string, token: string, body: unknown) => {
|
|
29
|
+
const r = await fetch(`${base}${path}`, {
|
|
30
|
+
method: "POST",
|
|
31
|
+
headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" },
|
|
32
|
+
body: JSON.stringify(body),
|
|
33
|
+
});
|
|
34
|
+
return { status: r.status, body: await r.json() as any };
|
|
35
|
+
};
|
|
36
|
+
const get = async (path: string, token: string) => {
|
|
37
|
+
const r = await fetch(`${base}${path}`, { headers: { Authorization: `Bearer ${token}` } });
|
|
38
|
+
return { status: r.status, body: await r.json() as any };
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
beforeAll(async () => {
|
|
42
|
+
const { db } = await import("./db.js");
|
|
43
|
+
const { register } = await import("./auth.js");
|
|
44
|
+
const stamp = Date.now();
|
|
45
|
+
const owner = register(`gate_owner_${stamp}`, "GateOwner-Strong-1!");
|
|
46
|
+
expect(owner.ok).toBe(true);
|
|
47
|
+
ownerToken = owner.token!;
|
|
48
|
+
net = owner.network_id!;
|
|
49
|
+
ownerId = db.get<{ user_id: string }>("SELECT user_id FROM users WHERE username = ?1", `gate_owner_${stamp}`)!.user_id;
|
|
50
|
+
|
|
51
|
+
const seed = (nodeId: string, transport: string) => db.run(
|
|
52
|
+
`INSERT INTO nodes (node_id, node_name, alias, network_id, lifecycle_state, config_snapshot)
|
|
53
|
+
VALUES (?1, ?1, ?1, ?2, 'active', ?3)`,
|
|
54
|
+
[nodeId, net, JSON.stringify({ node_secrets_capable: true })],
|
|
55
|
+
);
|
|
56
|
+
seed(PLAIN, "plain");
|
|
57
|
+
seed(TLS, "https");
|
|
58
|
+
seed(LOOPBACK, "loopback");
|
|
59
|
+
// The transport is reported on the session row (that is where report_status
|
|
60
|
+
// writes it).
|
|
61
|
+
const session = (nodeId: string, transport: string) => db.run(
|
|
62
|
+
`INSERT INTO sessions (resume_id, alias, status, node_id, network_id, env_transport, updated_at, last_seen_at)
|
|
63
|
+
VALUES (?1, ?1, 'idle', ?1, ?2, ?3, datetime('now'), datetime('now'))`,
|
|
64
|
+
[nodeId, net, transport],
|
|
65
|
+
);
|
|
66
|
+
session(PLAIN, "plain");
|
|
67
|
+
session(TLS, "https");
|
|
68
|
+
session(LOOPBACK, "loopback");
|
|
69
|
+
|
|
70
|
+
const mod = await import("./server.js");
|
|
71
|
+
server = mod.bootServer({ port: 0, hostname: "127.0.0.1" });
|
|
72
|
+
base = `http://127.0.0.1:${server.port}`;
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
afterAll(() => {
|
|
76
|
+
try { server?.stop(true); } catch {}
|
|
77
|
+
try { rmSync(PRIVATE_DB_DIR, { recursive: true, force: true }); } catch {}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
beforeEach(async () => {
|
|
81
|
+
// The lane allows one pending request per node; drain whatever the previous
|
|
82
|
+
// case left so the next one starts clean.
|
|
83
|
+
const { pullNodeSecrets, ackNodeSecrets } = await import("./scoped-secrets.js");
|
|
84
|
+
for (const nodeId of [PLAIN, TLS, LOOPBACK]) {
|
|
85
|
+
const daemon = { ok: true as const, networkId: net, daemonNodeId: nodeId, daemonAlias: nodeId };
|
|
86
|
+
const pulled = pullNodeSecrets(daemon) as any;
|
|
87
|
+
for (const r of (pulled?.requests ?? [])) ackNodeSecrets(daemon, r.request_id, null, true);
|
|
88
|
+
}
|
|
89
|
+
process.env.ANET_SECRET_TRANSPORT_GATE = "log";
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe("log mode (default)", () => {
|
|
93
|
+
test("a put to a plain node still succeeds AND leaves a hit", async () => {
|
|
94
|
+
const r = await post(`/api/nodes/${PLAIN}/secrets?network_id=${net}`, ownerToken, { action: "put", key: "A_KEY", value: "v", revision: 1 });
|
|
95
|
+
expect(r.status).toBe(200);
|
|
96
|
+
expect(r.body.ok).toBe(true); // not refused: that is the whole point of log mode
|
|
97
|
+
|
|
98
|
+
const log = await get(`/api/networks/${net}/env-transport-log`, ownerToken);
|
|
99
|
+
expect(log.status).toBe(200);
|
|
100
|
+
expect(log.body.total).toBe(1);
|
|
101
|
+
expect(log.body.rows[0]).toMatchObject({ node_id: PLAIN, route: "node_secret_put", hits: 1 });
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("a put to a TLS node leaves no hit", async () => {
|
|
105
|
+
const before = (await get(`/api/networks/${net}/env-transport-log`, ownerToken)).body.total;
|
|
106
|
+
const r = await post(`/api/nodes/${TLS}/secrets?network_id=${net}`, ownerToken, { action: "put", key: "B_KEY", value: "v", revision: 1 });
|
|
107
|
+
expect(r.status).toBe(200);
|
|
108
|
+
const after = (await get(`/api/networks/${net}/env-transport-log`, ownerToken)).body.total;
|
|
109
|
+
expect(after).toBe(before);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// #1510 acceptance leg 1: loopback is an allowed transport, so it must neither
|
|
113
|
+
// be refused nor pollute the would-refuse log (the log is the decision input
|
|
114
|
+
// for turning enforce on — a loopback hit in it would read as "something to
|
|
115
|
+
// block" when nothing is).
|
|
116
|
+
test("a put to a loopback node succeeds AND leaves no hit", async () => {
|
|
117
|
+
const before = (await get(`/api/networks/${net}/env-transport-log`, ownerToken)).body.total;
|
|
118
|
+
const r = await post(`/api/nodes/${LOOPBACK}/secrets?network_id=${net}`, ownerToken, { action: "put", key: "LB_KEY", value: "v", revision: 1 });
|
|
119
|
+
expect(r.status).toBe(200);
|
|
120
|
+
expect(r.body.ok).toBe(true);
|
|
121
|
+
const after = (await get(`/api/networks/${net}/env-transport-log`, ownerToken)).body.total;
|
|
122
|
+
expect(after).toBe(before);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("list is not observed (it carries no secret)", async () => {
|
|
126
|
+
const before = (await get(`/api/networks/${net}/env-transport-log`, ownerToken)).body.total;
|
|
127
|
+
await post(`/api/nodes/${PLAIN}/secrets?network_id=${net}`, ownerToken, { action: "list" });
|
|
128
|
+
const after = (await get(`/api/networks/${net}/env-transport-log`, ownerToken)).body.total;
|
|
129
|
+
expect(after).toBe(before);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe("enforce mode", () => {
|
|
134
|
+
test("the same put is refused, and nothing is queued", async () => {
|
|
135
|
+
process.env.ANET_SECRET_TRANSPORT_GATE = "enforce";
|
|
136
|
+
const r = await post(`/api/nodes/${PLAIN}/secrets?network_id=${net}`, ownerToken, { action: "put", key: "C_KEY", value: "v", revision: 1 });
|
|
137
|
+
expect(r.status).toBe(400);
|
|
138
|
+
expect(r.body.error).toBe("secret_transport_plain");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("a TLS node is unaffected", async () => {
|
|
142
|
+
process.env.ANET_SECRET_TRANSPORT_GATE = "enforce";
|
|
143
|
+
const r = await post(`/api/nodes/${TLS}/secrets?network_id=${net}`, ownerToken, { action: "put", key: "D_KEY", value: "v", revision: 1 });
|
|
144
|
+
expect(r.status).toBe(200);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// #1510 acceptance: "loopback 通过 / https 通过 / 明文公网被拒". enforce is
|
|
148
|
+
// defined as *refuse plain* — loopback is a local link and must keep working
|
|
149
|
+
// under enforce, otherwise turning the gate on would break the one transport
|
|
150
|
+
// it exists to protect.
|
|
151
|
+
test("a loopback node passes enforce too", async () => {
|
|
152
|
+
process.env.ANET_SECRET_TRANSPORT_GATE = "enforce";
|
|
153
|
+
const r = await post(`/api/nodes/${LOOPBACK}/secrets?network_id=${net}`, ownerToken, { action: "put", key: "LB2_KEY", value: "v", revision: 1 });
|
|
154
|
+
expect(r.status).toBe(200);
|
|
155
|
+
expect(r.body.ok).toBe(true);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
describe("off mode", () => {
|
|
160
|
+
test("records nothing at all", async () => {
|
|
161
|
+
process.env.ANET_SECRET_TRANSPORT_GATE = "off";
|
|
162
|
+
const before = (await get(`/api/networks/${net}/env-transport-log`, ownerToken)).body.total;
|
|
163
|
+
const r = await post(`/api/nodes/${PLAIN}/secrets?network_id=${net}`, ownerToken, { action: "put", key: "E_KEY", value: "v", revision: 1 });
|
|
164
|
+
expect(r.status).toBe(200);
|
|
165
|
+
const after = (await get(`/api/networks/${net}/env-transport-log`, ownerToken)).body.total;
|
|
166
|
+
expect(after).toBe(before);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
describe("the log endpoint is owner/admin only", () => {
|
|
171
|
+
test("an unrelated user gets 403", async () => {
|
|
172
|
+
const { register } = await import("./auth.js");
|
|
173
|
+
const other = register(`gate_other_${Date.now()}`, "GateOther-Strong-1!");
|
|
174
|
+
const r = await get(`/api/networks/${net}/env-transport-log`, other.token!);
|
|
175
|
+
expect(r.status).toBe(403);
|
|
176
|
+
});
|
|
177
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// #1510 (log-only) — record the secret writes that a future transport gate would
|
|
2
|
+
// have refused, without refusing any of them.
|
|
3
|
+
//
|
|
4
|
+
// Why log-only first: refusing makes an existing workflow fail on upgrade, and
|
|
5
|
+
// the nodes that talk plain HTTP today are exactly the ones we cannot see from
|
|
6
|
+
// the Hub. This table answers "how many writes would have been blocked, on which
|
|
7
|
+
// node" *before* anyone decides to turn the gate on — the same rollout shape as
|
|
8
|
+
// #519's stale-open sweep and #525's human_only log mode.
|
|
9
|
+
//
|
|
10
|
+
// Hourly aggregation, like node_permission_log: a chatty node must not turn this
|
|
11
|
+
// into one row per write.
|
|
12
|
+
import { db } from "./db.js";
|
|
13
|
+
import { getEnvTransportMode } from "./env-transport-policy.js";
|
|
14
|
+
|
|
15
|
+
const HOURS_RETAINED = 30 * 24;
|
|
16
|
+
const MAX_ROWS = 20_000;
|
|
17
|
+
|
|
18
|
+
function hourOf(now = new Date()): string {
|
|
19
|
+
return now.toISOString().slice(0, 13);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Record one would-refuse hit. Never throws: logging must not break a write. */
|
|
23
|
+
export function noteEnvTransportWouldRefuse(networkId: string, nodeId: string, route: string, sample?: string): void {
|
|
24
|
+
try {
|
|
25
|
+
const now = new Date();
|
|
26
|
+
if (Math.random() < 0.02) {
|
|
27
|
+
db.run("DELETE FROM env_transport_would_refuse WHERE hour < ?1",
|
|
28
|
+
[hourOf(new Date(now.getTime() - HOURS_RETAINED * 86_400_000))]);
|
|
29
|
+
}
|
|
30
|
+
const count = Number(db.get<{ n: number | string }>("SELECT COUNT(*) AS n FROM env_transport_would_refuse")?.n ?? 0);
|
|
31
|
+
const hour = hourOf(now);
|
|
32
|
+
const s = sample ? sample.slice(0, 120) : null;
|
|
33
|
+
if (count >= MAX_ROWS) {
|
|
34
|
+
db.run(
|
|
35
|
+
"UPDATE env_transport_would_refuse SET hits = hits + 1, sample = COALESCE(sample, ?5) WHERE network_id = ?1 AND node_id = ?2 AND route = ?3 AND hour = ?4",
|
|
36
|
+
[networkId, nodeId, route, hour, s],
|
|
37
|
+
);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
db.run(
|
|
41
|
+
`INSERT INTO env_transport_would_refuse (network_id, node_id, route, hour, hits, sample) VALUES (?1, ?2, ?3, ?4, 1, ?5)
|
|
42
|
+
ON CONFLICT(network_id, node_id, route, hour) DO UPDATE SET hits = env_transport_would_refuse.hits + 1`,
|
|
43
|
+
[networkId, nodeId, route, hour, s],
|
|
44
|
+
);
|
|
45
|
+
} catch (e) {
|
|
46
|
+
console.error(`[env-transport] would-refuse log failed: ${(e as Error).message}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Where a secret write is aimed: the node's own row, or a daemon's. Returns the
|
|
51
|
+
* transport the node last reported, or null when it is unknown. */
|
|
52
|
+
export function transportOf(networkId: string, nodeId: string): string | null {
|
|
53
|
+
const row = db.get<{ env_transport: string | null }>(
|
|
54
|
+
"SELECT env_transport FROM sessions WHERE network_id = ?1 AND (node_id = ?2 OR alias = ?2) ORDER BY updated_at DESC LIMIT 1",
|
|
55
|
+
[networkId, nodeId],
|
|
56
|
+
);
|
|
57
|
+
return row?.env_transport ?? null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Called from the write lanes. Logs when the current policy would refuse. */
|
|
61
|
+
export function observeSecretWrite(networkId: string, nodeId: string, route: string): void {
|
|
62
|
+
if (getEnvTransportMode() !== "log") return;
|
|
63
|
+
const transport = transportOf(networkId, nodeId);
|
|
64
|
+
if (transport !== "plain") return;
|
|
65
|
+
noteEnvTransportWouldRefuse(networkId, nodeId, route, `transport=${transport}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function readEnvTransportLog(networkId: string, sinceHour: string) {
|
|
69
|
+
return db.all<{ node_id: string; route: string; hour: string; hits: number | string; sample: string | null }>(
|
|
70
|
+
`SELECT node_id, route, hour, hits, sample FROM env_transport_would_refuse
|
|
71
|
+
WHERE network_id = ?1 AND hour >= ?2 ORDER BY hour DESC, node_id, route`,
|
|
72
|
+
[networkId, sinceHour],
|
|
73
|
+
).map(r => ({ ...r, hits: Number(r.hits) }));
|
|
74
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// #1510 — the switch for the secret-transport gate.
|
|
2
|
+
//
|
|
3
|
+
// (unset) / "log" — record what a gate WOULD refuse; refuse nothing.
|
|
4
|
+
// "enforce" — refuse secret writes to nodes that report `plain`.
|
|
5
|
+
// "off" — do nothing at all (no recording either).
|
|
6
|
+
//
|
|
7
|
+
// Reading the env on every call (like the stale-open sweep) means an operator can
|
|
8
|
+
// change the mode without a code change and tests can flip it per case.
|
|
9
|
+
export type EnvTransportMode = "off" | "log" | "enforce";
|
|
10
|
+
|
|
11
|
+
export function getEnvTransportMode(env: Record<string, string | undefined> = process.env): EnvTransportMode {
|
|
12
|
+
const raw = (env.ANET_SECRET_TRANSPORT_GATE ?? "").trim().toLowerCase();
|
|
13
|
+
if (raw === "off") return "off";
|
|
14
|
+
if (raw === "enforce") return "enforce";
|
|
15
|
+
return "log";
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** True when a write to a node reporting `plain` must be refused. */
|
|
19
|
+
export function refusesPlainTransport(transport: string | null, env: Record<string, string | undefined> = process.env): boolean {
|
|
20
|
+
return getEnvTransportMode(env) === "enforce" && transport === "plain";
|
|
21
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// #2614 —— 事件循环停顿监测 + 慢 SQL 日志的单元测试。
|
|
2
|
+
//
|
|
3
|
+
// 这两个东西的价值不在“能测出停顿”,而在**停顿发生时留下可对齐的证据**:
|
|
4
|
+
// 2026-10-26 生产两次 RSS 3.6/4.5 GB 的尖峰,事后只能靠“日志静默几十秒”
|
|
5
|
+
// 手工推断,`/api/stats/routes` 只给出受害者请求(含排队时间)。
|
|
6
|
+
// 所以测试要钉住的是语义:怎么算一次停顿、留什么档、日志阈值怎么生效、关闭后还计数。
|
|
7
|
+
|
|
8
|
+
import { describe, expect, test } from "bun:test";
|
|
9
|
+
import {
|
|
10
|
+
eventLoopStats,
|
|
11
|
+
noteRequestServed,
|
|
12
|
+
recordGap,
|
|
13
|
+
resetEventLoopLagForTests,
|
|
14
|
+
startEventLoopLagMonitor,
|
|
15
|
+
} from "./event-loop-lag.js";
|
|
16
|
+
import { formatSlowSql, slowSqlThresholdMs } from "./db-adapter.js";
|
|
17
|
+
|
|
18
|
+
describe("event-loop-lag: recordGap", () => {
|
|
19
|
+
test("正常 tick(≈interval)不算停顿", () => {
|
|
20
|
+
resetEventLoopLagForTests();
|
|
21
|
+
expect(recordGap(260, { intervalMs: 250, thresholdMs: 2000 })).toBe(10);
|
|
22
|
+
const s = eventLoopStats();
|
|
23
|
+
expect(s.stall_count).toBe(0);
|
|
24
|
+
expect(s.max_gap_ms).toBe(260);
|
|
25
|
+
expect(s.recent_stalls).toEqual([]);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("超过阈值的 tick 记一条停顿,带 stall_ms / rss_mb / at", () => {
|
|
29
|
+
resetEventLoopLagForTests();
|
|
30
|
+
const now = Date.parse("2026-10-26T22:10:41Z");
|
|
31
|
+
const stall = recordGap(28_000, { intervalMs: 250, thresholdMs: 2000, rssMb: 4502, now });
|
|
32
|
+
expect(stall).toBe(27_750);
|
|
33
|
+
const s = eventLoopStats();
|
|
34
|
+
expect(s.stall_count).toBe(1);
|
|
35
|
+
expect(s.last_stall_ms).toBe(27_750);
|
|
36
|
+
expect(s.last_stall_at).toBe("2026-10-26T22:10:41.000Z");
|
|
37
|
+
expect(s.max_gap_ms).toBe(28_000);
|
|
38
|
+
expect(s.recent_stalls[0]).toMatchObject({ gap_ms: 28_000, stall_ms: 27_750, rss_mb: 4502 });
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("recent_stalls 最新在前,且只保留 20 条", () => {
|
|
42
|
+
resetEventLoopLagForTests();
|
|
43
|
+
for (let i = 0; i < 25; i++) {
|
|
44
|
+
recordGap(5_000 + i, { intervalMs: 250, thresholdMs: 2_000, rssMb: 300, now: 1_700_000_000_000 + i * 1_000 });
|
|
45
|
+
}
|
|
46
|
+
const s = eventLoopStats();
|
|
47
|
+
expect(s.stall_count).toBe(25);
|
|
48
|
+
expect(s.recent_stalls.length).toBe(20);
|
|
49
|
+
// 最新一条是 i=24
|
|
50
|
+
expect(s.recent_stalls[0].gap_ms).toBe(5_024);
|
|
51
|
+
expect(s.recent_stalls[19].gap_ms).toBe(5_005);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("记录这段间隔里服务了多少请求(区分“阻塞”与“timer 饥饿”)", () => {
|
|
55
|
+
resetEventLoopLagForTests();
|
|
56
|
+
recordGap(85_000, { intervalMs: 250, thresholdMs: 2_000, rssMb: 2076, served: 137 });
|
|
57
|
+
const s = eventLoopStats();
|
|
58
|
+
expect(s.recent_stalls[0].requests_since_last_tick).toBe(137);
|
|
59
|
+
// 0 服务数 = 这段时间真的什么都没推进
|
|
60
|
+
recordGap(30_000, { intervalMs: 250, thresholdMs: 2_000, rssMb: 500, served: 0 });
|
|
61
|
+
expect(eventLoopStats().recent_stalls[0].requests_since_last_tick).toBe(0);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("max_gap_ms 保留历史最大值(回落后不被覆盖)", () => {
|
|
65
|
+
resetEventLoopLagForTests();
|
|
66
|
+
recordGap(47_000, { intervalMs: 250, thresholdMs: 2_000 });
|
|
67
|
+
recordGap(260, { intervalMs: 250, thresholdMs: 2_000 });
|
|
68
|
+
const s = eventLoopStats();
|
|
69
|
+
expect(s.max_gap_ms).toBe(47_000);
|
|
70
|
+
expect(s.last_gap_ms).toBe(260);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe("event-loop-lag: startEventLoopLagMonitor", () => {
|
|
75
|
+
test("阈值 0 时不打日志,但仍然计数", async () => {
|
|
76
|
+
resetEventLoopLagForTests();
|
|
77
|
+
const logs: string[] = [];
|
|
78
|
+
const timer = startEventLoopLagMonitor({
|
|
79
|
+
intervalMs: 20,
|
|
80
|
+
thresholdMs: 0,
|
|
81
|
+
log: (m) => logs.push(m),
|
|
82
|
+
rssMb: () => 123,
|
|
83
|
+
});
|
|
84
|
+
// 同步阻塞 80ms,让下一次 tick 一定超阈值
|
|
85
|
+
const until = performance.now() + 80;
|
|
86
|
+
while (performance.now() < until) { /* spin */ }
|
|
87
|
+
await Bun.sleep(60);
|
|
88
|
+
clearInterval(timer);
|
|
89
|
+
expect(logs).toEqual([]);
|
|
90
|
+
expect(eventLoopStats().max_gap_ms).toBeGreaterThan(0);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("真实停顿会打一行 [commhub stall] 且带 rss", async () => {
|
|
94
|
+
resetEventLoopLagForTests();
|
|
95
|
+
const logs: string[] = [];
|
|
96
|
+
const timer = startEventLoopLagMonitor({
|
|
97
|
+
intervalMs: 20,
|
|
98
|
+
thresholdMs: 40,
|
|
99
|
+
log: (m) => logs.push(m),
|
|
100
|
+
rssMb: () => 4502,
|
|
101
|
+
});
|
|
102
|
+
const until = performance.now() + 120;
|
|
103
|
+
while (performance.now() < until) { /* spin 阻塞事件循环 */ }
|
|
104
|
+
await Bun.sleep(60);
|
|
105
|
+
clearInterval(timer);
|
|
106
|
+
expect(logs.length).toBeGreaterThan(0);
|
|
107
|
+
expect(logs[0]).toStartWith("[commhub stall] loop idle ~");
|
|
108
|
+
expect(logs[0]).toContain("rss 4502 MB");
|
|
109
|
+
expect(logs[0]).toContain("served 0 req");
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe("db-adapter: 慢 SQL 日志", () => {
|
|
114
|
+
test("默认阈值 1000ms;环境变量可调,0 表示关闭", () => {
|
|
115
|
+
const prev = process.env.COMMHUB_SLOW_SQL_MS;
|
|
116
|
+
try {
|
|
117
|
+
delete process.env.COMMHUB_SLOW_SQL_MS;
|
|
118
|
+
expect(slowSqlThresholdMs()).toBe(1000);
|
|
119
|
+
process.env.COMMHUB_SLOW_SQL_MS = "250";
|
|
120
|
+
expect(slowSqlThresholdMs()).toBe(250);
|
|
121
|
+
process.env.COMMHUB_SLOW_SQL_MS = "0";
|
|
122
|
+
expect(slowSqlThresholdMs()).toBe(0);
|
|
123
|
+
process.env.COMMHUB_SLOW_SQL_MS = "not-a-number";
|
|
124
|
+
expect(slowSqlThresholdMs()).toBe(1000);
|
|
125
|
+
} finally {
|
|
126
|
+
if (prev === undefined) delete process.env.COMMHUB_SLOW_SQL_MS;
|
|
127
|
+
else process.env.COMMHUB_SLOW_SQL_MS = prev;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("日志压平换行并截断,且不含绑定参数", () => {
|
|
132
|
+
const sql = "DELETE FROM agent_telemetry\n WHERE created_at < datetime('now', ?1)";
|
|
133
|
+
const line = formatSlowSql(10_588.4, sql);
|
|
134
|
+
expect(line).toStartWith("[commhub slow-sql] 10588ms DELETE FROM agent_telemetry WHERE created_at < datetime('now', ?1)");
|
|
135
|
+
expect(line).not.toContain("\n");
|
|
136
|
+
const long = formatSlowSql(1_200, "SELECT " + "x".repeat(500));
|
|
137
|
+
expect(long.length).toBeLessThanOrEqual("[commhub slow-sql] 1200ms ".length + 160);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// #2614 —— 事件循环停顿监测(只观测,不改任何行为)。
|
|
2
|
+
//
|
|
3
|
+
// 背景:2026-10-26 21:55–22:12 生产 Hub 出现两次内存尖峰(RSS 3.6 → 4.5 GB),
|
|
4
|
+
// 伴随机 `mcp_inflight` 387 → 807。事后看日志才发现**同一时刻日志静默 14–47 秒**
|
|
5
|
+
// ——Hub 是单线程(Bun + 同步 SQLite),日志静默就等于事件循环被阻塞。
|
|
6
|
+
//
|
|
7
|
+
// 现有 `/api/stats/routes` 只能告诉我们“哪个请求**受害**了”(它的耗时包含了排队时间),
|
|
8
|
+
// 不能告诉我们“当时**谁**在阻塞”。缺的就是停顿本身的时间序列。
|
|
9
|
+
//
|
|
10
|
+
// 这个模块补上这一格:每 `intervalMs` 醒一次,量 `now - lastTick`;
|
|
11
|
+
// 正常情况 ≈ interval,超出 `thresholdMs` 的部分就是一次真实停顿。
|
|
12
|
+
// 停顿会:① 计数/留档(`/health` 的 `event_loop` 字段可读);
|
|
13
|
+
// ② 打一行日志(带当时的 RSS,方便和内存曲线对齐)。
|
|
14
|
+
//
|
|
15
|
+
// 注意:`setInterval` 自己也会被阻塞 —— 这正是我们要测量的东西。
|
|
16
|
+
// 停顿结束后第一次 tick 的 gap 就是停顿长度,不需要外部进程配合。
|
|
17
|
+
|
|
18
|
+
export type EventLoopStall = {
|
|
19
|
+
/** 这个间隔里处理完的请求数:0 = 真的没推进;>0 = 循环在干活,只是 timer 被饿住。 */
|
|
20
|
+
/** ISO 时间(停顿**结束**的时刻,即这次 tick 的时间)。 */
|
|
21
|
+
at: string;
|
|
22
|
+
/** 距上次 tick 的实际间隔(毫秒)。等于 interval + 停顿。 */
|
|
23
|
+
gap_ms: number;
|
|
24
|
+
/** 停顿量 = gap - interval。 */
|
|
25
|
+
stall_ms: number;
|
|
26
|
+
/** 停顿结束时的进程 RSS(MB),用于和内存尖峰对齐。 */
|
|
27
|
+
rss_mb: number;
|
|
28
|
+
requests_since_last_tick?: number;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const KEEP_STALLS = 20;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* #2614(第二轮)—— 2026-10-27 00:39 线上出现 gap 84887ms,但**同一时段日志在正常写**
|
|
35
|
+
* (00:38:52 / 00:38:58 / 00:39:08 …),而且 `/api/stats/routes` 里最慢的请求只有 2.4s。
|
|
36
|
+
* 也就是说:事件循环**没有被阻塞 84 秒**,是 **timer 回调被 I/O 队列饿住**了。
|
|
37
|
+
* 只看 tick 间隔会把这两种情况混成一条“卡了 84 秒”的假结论。
|
|
38
|
+
*
|
|
39
|
+
* 区分办法:数一下这个间隔里**服务了多少请求**。
|
|
40
|
+
* requests_since_last_tick > 0 ⇒ 循环在干活 ⇒ 这是 timer 饥饿(高负载/大批回调)
|
|
41
|
+
* requests_since_last_tick = 0 ⇒ 真的没有推进任何请求 ⇒ 才可能是同步阻塞
|
|
42
|
+
*/
|
|
43
|
+
let servedSinceTick = 0;
|
|
44
|
+
|
|
45
|
+
/** 请求处理完成时调用(route-timing 里)。只计数,不参与任何决策。 */
|
|
46
|
+
export function noteRequestServed(): void {
|
|
47
|
+
servedSinceTick++;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let maxGapMs = 0;
|
|
51
|
+
let lastGapMs = 0;
|
|
52
|
+
let stallCount = 0;
|
|
53
|
+
let lastStallAt: string | null = null;
|
|
54
|
+
let lastStallMs = 0;
|
|
55
|
+
const stalls: EventLoopStall[] = [];
|
|
56
|
+
|
|
57
|
+
/** 记录一次 tick 间隔。返回这次 tick 的停顿量(毫秒,可为负→归零)。 */
|
|
58
|
+
export function recordGap(
|
|
59
|
+
gapMs: number,
|
|
60
|
+
opts: { intervalMs?: number; thresholdMs?: number; rssMb?: number; now?: number; served?: number } = {},
|
|
61
|
+
): number {
|
|
62
|
+
const interval = opts.intervalMs ?? 250;
|
|
63
|
+
const threshold = opts.thresholdMs ?? 2000;
|
|
64
|
+
lastGapMs = gapMs;
|
|
65
|
+
if (gapMs > maxGapMs) maxGapMs = gapMs;
|
|
66
|
+
const stallMs = Math.max(0, gapMs - interval);
|
|
67
|
+
if (stallMs < threshold) return stallMs;
|
|
68
|
+
const at = new Date(opts.now ?? Date.now()).toISOString();
|
|
69
|
+
stallCount++;
|
|
70
|
+
lastStallAt = at;
|
|
71
|
+
lastStallMs = stallMs;
|
|
72
|
+
stalls.push({
|
|
73
|
+
at,
|
|
74
|
+
gap_ms: Math.round(gapMs),
|
|
75
|
+
stall_ms: Math.round(stallMs),
|
|
76
|
+
rss_mb: opts.rssMb ?? 0,
|
|
77
|
+
requests_since_last_tick: opts.served ?? 0,
|
|
78
|
+
});
|
|
79
|
+
while (stalls.length > KEEP_STALLS) stalls.shift();
|
|
80
|
+
return stallMs;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function eventLoopStats(): {
|
|
84
|
+
max_gap_ms: number;
|
|
85
|
+
last_gap_ms: number;
|
|
86
|
+
stall_count: number;
|
|
87
|
+
last_stall_ms: number;
|
|
88
|
+
last_stall_at: string | null;
|
|
89
|
+
recent_stalls: EventLoopStall[];
|
|
90
|
+
} {
|
|
91
|
+
return {
|
|
92
|
+
max_gap_ms: Math.round(maxGapMs),
|
|
93
|
+
last_gap_ms: Math.round(lastGapMs),
|
|
94
|
+
stall_count: stallCount,
|
|
95
|
+
last_stall_ms: Math.round(lastStallMs),
|
|
96
|
+
last_stall_at: lastStallAt,
|
|
97
|
+
recent_stalls: [...stalls].reverse(),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** 只给测试用:清空累计状态。 */
|
|
102
|
+
export function resetEventLoopLagForTests(): void {
|
|
103
|
+
maxGapMs = 0;
|
|
104
|
+
lastGapMs = 0;
|
|
105
|
+
stallCount = 0;
|
|
106
|
+
lastStallAt = null;
|
|
107
|
+
lastStallMs = 0;
|
|
108
|
+
stalls.length = 0;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function envMs(name: string, fallback: number): number {
|
|
112
|
+
const raw = Number(process.env[name]);
|
|
113
|
+
return Number.isFinite(raw) && raw >= 0 ? raw : fallback;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 启动监测。返回 timer(调用方负责 clearInterval)。
|
|
118
|
+
* - `COMMHUB_STALL_LOG_MS`:停顿日志阈值,默认 2000 ms,设 0 关闭日志(仍计数);
|
|
119
|
+
* - `COMMHUB_STALL_TICK_MS`:采样间隔,默认 250 ms。
|
|
120
|
+
*/
|
|
121
|
+
export function startEventLoopLagMonitor(opts: {
|
|
122
|
+
intervalMs?: number;
|
|
123
|
+
thresholdMs?: number;
|
|
124
|
+
log?: (msg: string) => void;
|
|
125
|
+
rssMb?: () => number;
|
|
126
|
+
} = {}): ReturnType<typeof setInterval> {
|
|
127
|
+
const intervalMs = opts.intervalMs ?? Math.max(50, envMs("COMMHUB_STALL_TICK_MS", 250));
|
|
128
|
+
const thresholdMs = opts.thresholdMs ?? envMs("COMMHUB_STALL_LOG_MS", 2000);
|
|
129
|
+
const log = opts.log ?? ((m: string) => console.log(m));
|
|
130
|
+
const rssMb = opts.rssMb ?? (() => Math.round(process.memoryUsage().rss / 1048576));
|
|
131
|
+
let last = performance.now();
|
|
132
|
+
const timer = setInterval(() => {
|
|
133
|
+
const now = performance.now();
|
|
134
|
+
const gap = now - last;
|
|
135
|
+
last = now;
|
|
136
|
+
const rss = rssMb();
|
|
137
|
+
const served = servedSinceTick;
|
|
138
|
+
servedSinceTick = 0;
|
|
139
|
+
const stall = recordGap(gap, { intervalMs, thresholdMs, rssMb: rss, served });
|
|
140
|
+
if (thresholdMs > 0 && stall >= thresholdMs) {
|
|
141
|
+
// 和 retention/slow-sql 的日志同一前缀,方便 grep。
|
|
142
|
+
// `served` 是判据的关键:它非零就说明循环在干活,这段“gap”是 timer 被饿住,不是阻塞。
|
|
143
|
+
log(`[commhub stall] loop idle ~${Math.round(stall)}ms (tick gap ${Math.round(gap)}ms, rss ${rss} MB, served ${served} req)`);
|
|
144
|
+
}
|
|
145
|
+
}, intervalMs);
|
|
146
|
+
// 监测不该拖住进程退出。
|
|
147
|
+
(timer as unknown as { unref?: () => void }).unref?.();
|
|
148
|
+
last = performance.now();
|
|
149
|
+
return timer;
|
|
150
|
+
}
|