@sleep2agi/commhub-server 0.9.0-preview.21 → 0.9.0-preview.210
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +130 -10
- package/bin/commhub.ts +99 -11
- package/package.json +7 -7
- package/src/ack-create-request-transport.test.ts +182 -0
- package/src/ack-create-request.test.ts +50 -7
- package/src/admin-delete-network-http.test.ts +228 -0
- package/src/admin-networks-http.test.ts +88 -0
- package/src/adoption-candidates-http.test.ts +137 -0
- package/src/adoption-candidates.ts +158 -0
- package/src/agent-access-batch.test.ts +210 -0
- package/src/agent-access.ts +487 -0
- package/src/agent-acl-groups-http.test.ts +247 -0
- package/src/agent-acl-http.test.ts +659 -0
- package/src/agent-acl-slice3-http.test.ts +267 -0
- package/src/agent-department-http.test.ts +215 -0
- package/src/agent-groups-perf.test.ts +223 -0
- package/src/agent-notice.ts +48 -0
- package/src/agent-team-whoami-http.test.ts +98 -0
- package/src/agent-teams-http.test.ts +330 -0
- package/src/agent-teams.ts +244 -0
- package/src/alias-baseline-2571.test.ts +162 -0
- package/src/alias-baseline.ts +113 -0
- package/src/alias-filter.test.ts +72 -0
- package/src/alias-filter.ts +42 -0
- package/src/api-host-supervisors-can-create-nodes.test.ts +350 -0
- package/src/api-host-supervisors-fallback.test.ts +21 -12
- package/src/api-host-supervisors-runtime-readiness.test.ts +195 -0
- package/src/api-nodes-shape.test.ts +120 -13
- package/src/auth-password-change-cli-tokens-http.test.ts +423 -0
- package/src/auth-sessions-http.test.ts +250 -0
- package/src/auth-validate.test.ts +5 -1
- package/src/auth-weak-password-login.test.ts +176 -0
- package/src/auth.ts +609 -51
- package/src/avatar-validate.test.ts +163 -0
- package/src/avatar-validate.ts +103 -0
- package/src/backfill-node-ids-2664.test.ts +128 -0
- package/src/binding-generation-http.test.ts +121 -0
- package/src/blocked-exit-on-reply.test.ts +122 -0
- package/src/boot-side-effect.test.ts +124 -0
- package/src/broadcast-fanout-2721.test.ts +118 -0
- package/src/broadcast-fanout.ts +98 -0
- package/src/chain-attempt-dedup.test.ts +447 -0
- package/src/clone-node.test.ts +248 -0
- package/src/clone-node.ts +289 -0
- package/src/codex-config-flags.ts +77 -0
- package/src/codex-fork-contract.ts +23 -0
- package/src/codex-fork-migration.test.ts +38 -0
- package/src/commhub-bin-args.test.ts +85 -0
- package/src/config-apply-validate.test.ts +42 -1
- package/src/config-apply-validate.ts +30 -2
- package/src/create-node-blocked-gate.test.ts +356 -0
- package/src/create-node-network-id.test.ts +79 -0
- package/src/create-node-tool-schema.test.ts +49 -0
- package/src/create-node-unicode-name.test.ts +120 -0
- package/src/create-node-validate.test.ts +199 -7
- package/src/create-node-validate.ts +232 -15
- package/src/create-node-workdir.test.ts +289 -0
- package/src/create-node.ts +88 -23
- package/src/daemon-provider-snapshots.test.ts +421 -0
- package/src/daemon-provider-snapshots.ts +327 -0
- package/src/daemon-token-owner-http.test.ts +236 -0
- package/src/dashboard-slash-routing-http.test.ts +109 -0
- package/src/db-adapter-guard.test.ts +148 -0
- package/src/db-adapter-translate.test.ts +128 -0
- package/src/db-adapter.ts +377 -91
- package/src/db-timestamp.test.ts +38 -0
- package/src/db-timestamp.ts +15 -0
- package/src/db.ts +1306 -153
- package/src/delivered-without-push.test.ts +140 -0
- package/src/department-groups-http.test.ts +209 -0
- package/src/department-groups-sync-http.test.ts +328 -0
- package/src/department-groups.ts +434 -0
- package/src/department-heads-http.test.ts +330 -0
- package/src/department-heads.ts +226 -0
- package/src/departments-agent-count.test.ts +129 -0
- package/src/departments-http.test.ts +185 -0
- package/src/departments-mcp.test.ts +128 -0
- package/src/departments.ts +318 -0
- package/src/desktop-message-seam.test.ts +150 -0
- package/src/desktop-message.test.ts +368 -0
- package/src/dm-file-scope-http.test.ts +178 -0
- package/src/empty-node-sweeper.test.ts +104 -0
- package/src/empty-node-sweeper.ts +88 -0
- package/src/env-transport-gate-http.test.ts +240 -0
- package/src/env-transport-gate.ts +80 -0
- package/src/env-transport-policy.ts +56 -0
- package/src/event-loop-lag.test.ts +180 -0
- package/src/event-loop-lag.ts +181 -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 -2385
- package/src/list-host-supervisors.test.ts +5 -4
- package/src/mcp-context-cost-http.test.ts +127 -0
- package/src/mcp-inflight.test.ts +92 -0
- package/src/mcp-inflight.ts +38 -0
- package/src/mcp-projects-events-http.test.ts +162 -0
- package/src/mcp-route-label-http.test.ts +120 -0
- package/src/mcp-write-network-resolution.test.ts +432 -0
- package/src/member-audit-network-id-http.test.ts +93 -0
- package/src/member-presence-http.test.ts +210 -0
- package/src/messages-ack-by-agent-http.test.ts +142 -0
- package/src/migrate-sqlite-to-pg.test.ts +47 -0
- package/src/migrate-sqlite-to-pg.ts +263 -0
- package/src/model-auth-notify-http.test.ts +181 -0
- package/src/model-auth-notify.ts +116 -0
- package/src/network-name-http.test.ts +75 -0
- package/src/network-rules-http.test.ts +169 -0
- package/src/network-rules.ts +129 -0
- package/src/network-scope.ts +276 -0
- package/src/network-token-membership-http.test.ts +139 -0
- package/src/node-alias-control.ts +12 -0
- package/src/node-attrs-validate.ts +88 -0
- package/src/node-attrs.test.ts +258 -0
- package/src/node-caller-identity-http.test.ts +1083 -0
- package/src/node-create-request-status-http.test.ts +105 -0
- package/src/node-daemon-bindings.test.ts +132 -0
- package/src/node-daemon-bindings.ts +135 -0
- package/src/node-failure-alert.test.ts +150 -0
- package/src/node-failure-alert.ts +177 -0
- package/src/node-files-transport.test.ts +344 -0
- package/src/node-health-codex-login.test.ts +120 -0
- package/src/node-health-dispatch-http.test.ts +279 -0
- package/src/node-health-guard.ts +105 -0
- package/src/node-health-passthrough.test.ts +115 -0
- package/src/node-health-store.ts +101 -0
- package/src/node-id-alias.test.ts +190 -0
- package/src/node-identity-conflict-http.test.ts +269 -0
- package/src/node-identity-conflict.ts +142 -0
- package/src/node-identity-mismatch.test.ts +74 -0
- package/src/node-in-flight-tasks.ts +31 -0
- package/src/node-lifecycle-controllable.test.ts +64 -0
- package/src/node-lifecycle-controllable.ts +53 -0
- package/src/node-lifecycle-read-http.test.ts +250 -0
- package/src/node-lifecycle-read.ts +124 -0
- package/src/node-logs-transport.test.ts +263 -0
- package/src/node-peer-allowlist.test.ts +114 -0
- package/src/node-permissions-http.test.ts +648 -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 +209 -0
- package/src/node-token-identity.ts +35 -0
- package/src/node-token-ownership.ts +51 -0
- package/src/node-token-privilege-http.test.ts +158 -0
- package/src/not-found-api-http.test.ts +67 -0
- package/src/observer-avatar-http.test.ts +442 -0
- package/src/observer-push.test.ts +252 -0
- package/src/opencode-catalog-report-http.test.ts +139 -0
- package/src/opencode-v2-catalog.test.ts +268 -0
- package/src/opencode-v2-catalog.ts +352 -0
- package/src/package-version-consistency.test.ts +25 -0
- package/src/parent-source-marker.test.ts +50 -0
- package/src/password-network-tokens.ts +25 -0
- package/src/peer-reply-atomic.test.ts +455 -0
- package/src/peer-reply-capability.test.ts +70 -0
- package/src/pg-worker.ts +87 -0
- package/src/project-dir-warn-clear-http.test.ts +132 -0
- package/src/project-viewer-can-http.test.ts +116 -0
- package/src/push.test.ts +20 -10
- package/src/push.ts +466 -13
- package/src/redact-tokens.test.ts +65 -0
- package/src/redact-tokens.ts +40 -0
- package/src/rename.ts +38 -10
- package/src/reply-target-derivation.test.ts +96 -0
- package/src/report-status-handover-capabilities-http.test.ts +124 -0
- package/src/report-status-host-schema.test.ts +235 -0
- package/src/report-status-resume-id-handover.test.ts +184 -0
- package/src/report-status-secret-keys.test.ts +140 -0
- package/src/require-explicit-test-db.ts +9 -0
- package/src/requirement-due-reminders-http.test.ts +620 -0
- package/src/requirement-due-reminders.ts +416 -0
- package/src/requirement-events-http.test.ts +268 -0
- package/src/requirement-events.test.ts +45 -0
- package/src/requirement-events.ts +144 -0
- package/src/requirement-notify.ts +165 -0
- package/src/requirement-participant-notify-http.test.ts +309 -0
- package/src/requirement-tag-ops-http.test.ts +425 -0
- package/src/requirement-tag-tree-http.test.ts +275 -0
- package/src/requirement-tag-tree.ts +127 -0
- package/src/requirement-tags.ts +102 -0
- package/src/requirements-abandoned-http.test.ts +236 -0
- package/src/requirements-comment-http.test.ts +167 -0
- package/src/requirements-completed-at-migration.test.ts +70 -0
- package/src/requirements-errors-mcp-http.test.ts +164 -0
- package/src/requirements-errors.ts +158 -0
- package/src/requirements-etag-http.test.ts +91 -0
- package/src/requirements-http.test.ts +709 -0
- package/src/requirements-last-event-http.test.ts +349 -0
- package/src/requirements-list-mcp-slim-http.test.ts +155 -0
- package/src/requirements-list-slim-http.test.ts +324 -0
- package/src/requirements-mcp.test.ts +163 -0
- package/src/requirements-migrate.ts +283 -0
- package/src/requirements-owner-strict-mcp-http.test.ts +145 -0
- package/src/requirements-people-http.test.ts +114 -0
- package/src/requirements-people-mcp-http.test.ts +231 -0
- package/src/requirements-people.ts +196 -0
- package/src/requirements-priority-lowest.test.ts +114 -0
- package/src/requirements-projects-parent-http.test.ts +123 -0
- package/src/requirements-search-http.test.ts +182 -0
- package/src/requirements-search.test.ts +32 -0
- package/src/requirements-search.ts +145 -0
- package/src/requirements-seq-http.test.ts +193 -0
- package/src/requirements-seq-migration.test.ts +63 -0
- package/src/requirements-start-date.test.ts +125 -0
- package/src/requirements-stats-http.test.ts +357 -0
- package/src/requirements-stats.ts +195 -0
- package/src/requirements-status-participants-http.test.ts +222 -0
- package/src/requirements-upgrade-e2e.test.ts +126 -0
- package/src/requirements-upgrade-migration.test.ts +98 -0
- package/src/requirements.ts +2027 -0
- package/src/resolve-port.test.ts +57 -0
- package/src/resolve-port.ts +44 -0
- package/src/response-charset.test.ts +130 -0
- package/src/rest-alias-network-fallback-http.test.ts +123 -0
- package/src/rest-alias-network-fallback.test.ts +60 -0
- package/src/rest-alias-network-fallback.ts +65 -0
- package/src/rest-explicit-columns-http.test.ts +266 -0
- package/src/rest-identity.test.ts +130 -0
- package/src/rest-identity.ts +141 -0
- package/src/rest-projections.ts +72 -0
- package/src/rest-read-network-real-chain.test.ts +97 -0
- package/src/rest-write-network-resolution.test.ts +133 -0
- package/src/restart-readiness-http.test.ts +121 -0
- package/src/restricted-files.ts +76 -0
- package/src/retention-first-sweep-quiet.test.ts +31 -0
- package/src/retention.test.ts +100 -40
- package/src/retention.ts +423 -104
- package/src/route-callers-http.test.ts +120 -0
- package/src/route-timing.test.ts +211 -0
- package/src/route-timing.ts +262 -0
- package/src/rules-file-duplicate-alias.test.ts +234 -0
- package/src/rules-file-session-target.test.ts +207 -0
- package/src/rules-file-transport.test.ts +237 -0
- package/src/runtime-label.test.ts +22 -0
- package/src/runtime-label.ts +21 -0
- package/src/runtime-readiness.ts +90 -0
- package/src/schedule-agent-mcp-http.test.ts +391 -0
- package/src/schedule-agent.ts +192 -0
- package/src/schedule-starved.test.ts +92 -0
- package/src/schedule-starved.ts +114 -0
- package/src/scheduled-backoff-http.test.ts +193 -0
- package/src/scheduled-backoff.ts +64 -0
- package/src/scheduled-doorbell-nodeless-session.test.ts +134 -0
- package/src/scheduled-failure-backoff-2622.test.ts +145 -0
- package/src/scheduled-failure-code.test.ts +73 -0
- package/src/scheduled-failure-code.ts +105 -0
- package/src/scheduled-failures-http.test.ts +279 -0
- package/src/scheduled-failures.ts +377 -0
- package/src/scheduled-noprogress-backoff-2622.test.ts +133 -0
- package/src/scheduled-offline-backoff-probe.test.ts +56 -0
- package/src/scheduled-offline.test.ts +131 -0
- package/src/scheduled-offline.ts +66 -0
- package/src/scheduled-outcome-guidance-http.test.ts +139 -0
- package/src/scheduled-outcome-guidance.ts +9 -0
- package/src/scheduled-reply-unread-http.test.ts +212 -0
- package/src/scheduled-requirement-wake-http.test.ts +279 -0
- package/src/scheduled-requirement-wake.ts +125 -0
- package/src/scheduled-run-terminal.test.ts +268 -0
- package/src/scheduled-skip-blocking-task.test.ts +120 -0
- package/src/scheduled-skip-path-backoff-2622.test.ts +98 -0
- package/src/scheduled-stuck-http.test.ts +312 -0
- package/src/scheduled-stuck-notice-cap.test.ts +80 -0
- package/src/scheduled-stuck.ts +219 -0
- package/src/scheduled-tasks-http.test.ts +679 -0
- package/src/scheduled-tasks.ts +959 -0
- package/src/scheduled-work-outcome-http.test.ts +154 -0
- package/src/scheduled-work-outcome.ts +50 -0
- package/src/scoped-secrets.ts +141 -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 +6220 -0
- package/src/session-task-on-dispatch.ts +14 -0
- package/src/session-token-ownership-http.test.ts +122 -0
- package/src/shared/codex-inventory.ts +41 -0
- package/src/shared/daemon-provider-login.ts +31 -0
- package/src/shared/daemon-provider-snapshot.ts +52 -0
- package/src/shared/external-schedule-contract.ts +60 -0
- package/src/shared/node-name.test.ts +62 -0
- package/src/shared/node-name.ts +161 -0
- package/src/shared/provider-application.ts +29 -0
- package/src/shared/provider-collection.ts +14 -0
- package/src/shared/provider-initialization.ts +8 -0
- package/src/shared/reserved-env-drift.test.ts +10 -0
- package/src/shared/scoped-secret-contract.ts +5 -0
- package/src/shared/task-content-limit-drift.test.ts +28 -0
- package/src/shared/task-content-limit.ts +25 -0
- package/src/side-thread-command-transport.test.ts +312 -0
- package/src/side-thread-command-transport.ts +318 -0
- package/src/side-thread-http-integration.test.ts +233 -0
- package/src/side-thread-http.ts +504 -0
- package/src/side-thread-production.test.ts +72 -0
- package/src/side-thread-production.ts +118 -0
- package/src/side-thread.test.ts +1067 -0
- package/src/side-thread.ts +2499 -0
- package/src/skillhub-http.test.ts +108 -0
- package/src/sqlite-close-checkpoint.test.ts +78 -0
- package/src/start-node.test.ts +264 -0
- package/src/status-alias-filter-http.test.ts +106 -0
- package/src/status-alias-resolver-http.test.ts +163 -0
- package/src/status-alias-resolver.ts +19 -0
- package/src/status-node-id-filter-http.test.ts +161 -0
- package/src/status-read-cache-http.test.ts +229 -0
- package/src/status-read-cache.ts +122 -0
- package/src/stop-delete-node.test.ts +567 -1
- package/src/task-access-http.test.ts +402 -0
- package/src/task-access.ts +420 -0
- package/src/task-activity.ts +60 -0
- package/src/task-auth-origin-http.test.ts +93 -0
- package/src/task-auth-origin.test.ts +24 -0
- package/src/task-auth-origin.ts +7 -0
- package/src/task-consumption.test.ts +482 -0
- package/src/task-diagnostic-http.test.ts +149 -0
- package/src/task-diagnostic.test.ts +115 -0
- package/src/task-diagnostic.ts +118 -0
- package/src/task-expiry-notice-http.test.ts +287 -0
- package/src/task-expiry-notice.ts +315 -0
- package/src/task-idempotency-mcp.test.ts +142 -0
- package/src/task-idempotency.test.ts +76 -0
- package/src/task-idempotency.ts +81 -0
- package/src/task-late-replies.ts +54 -0
- package/src/task-late-reply-http.test.ts +152 -0
- package/src/task-lifecycle-watcher-busy.test.ts +114 -0
- package/src/task-lifecycle-watcher.test.ts +204 -0
- package/src/task-lifecycle-watcher.ts +109 -0
- package/src/task-network-resolution-http.test.ts +140 -0
- package/src/task-offline-delivery-2668.test.ts +119 -0
- package/src/task-offline-delivery.ts +81 -0
- package/src/task-orphan-http.test.ts +287 -0
- package/src/task-orphan.ts +119 -0
- package/src/task-queue-ahead-http.test.ts +309 -0
- package/src/task-queue-ahead-scale.test.ts +236 -0
- package/src/task-queue-ahead.ts +186 -0
- package/src/task-rejection-log-http.test.ts +160 -0
- package/src/task-rejection-log.ts +49 -0
- package/src/task-sender-http.test.ts +143 -0
- package/src/task-stale-open-http.test.ts +290 -0
- package/src/task-stale-open.ts +103 -0
- package/src/tasks-pagination-http.test.ts +141 -0
- package/src/team-rules-status-http.test.ts +90 -0
- package/src/team-rules-status.test.ts +59 -0
- package/src/team-rules-status.ts +72 -0
- package/src/tool-audience-http.test.ts +230 -0
- package/src/tool-audience.ts +120 -0
- package/src/tools-list-json-schema.test.ts +94 -0
- package/src/tools.ts +4119 -391
- package/src/upload-early-reject-guard.test.ts +155 -0
- package/src/uploads-http.test.ts +97 -15
- package/src/uploads.ts +103 -6
- package/src/user-avatar-http.test.ts +74 -0
- package/src/user-avatar-image.ts +160 -0
- package/src/user-avatar-migration.test.ts +25 -0
- package/src/user-avatar-upload-http.test.ts +348 -0
- package/src/user-inbox-by-agent-http.test.ts +161 -0
- package/src/user-inbox-netid-notnull.test.ts +122 -0
- package/src/user-inbox-read-http.test.ts +223 -0
- package/src/user-inbox-schema.test.ts +54 -0
- package/src/version-stats.test.ts +66 -0
- package/src/version-stats.ts +109 -0
|
@@ -0,0 +1,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,240 @@
|
|
|
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
|
+
const UNKNOWN = "n_gate_unknown";
|
|
28
|
+
// #1510 补档 —— 生产里占绝大多数的是「从未上报」:2026-10-29 只读统计 373 个会话,
|
|
29
|
+
// env_transport = NULL 有 332 个(89%)。原实现是拒绝名单(只拒 plain),
|
|
30
|
+
// null 一路放行;卡里的口径是允许名单(只许 loopback/https)。这两者只在 null 上分歧,
|
|
31
|
+
// 所以这里把 null 当成一等公民单独种一个节点。
|
|
32
|
+
|
|
33
|
+
const post = async (path: string, token: string, body: unknown) => {
|
|
34
|
+
const r = await fetch(`${base}${path}`, {
|
|
35
|
+
method: "POST",
|
|
36
|
+
headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" },
|
|
37
|
+
body: JSON.stringify(body),
|
|
38
|
+
});
|
|
39
|
+
return { status: r.status, body: await r.json() as any };
|
|
40
|
+
};
|
|
41
|
+
const get = async (path: string, token: string) => {
|
|
42
|
+
const r = await fetch(`${base}${path}`, { headers: { Authorization: `Bearer ${token}` } });
|
|
43
|
+
return { status: r.status, body: await r.json() as any };
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
beforeAll(async () => {
|
|
47
|
+
const { db } = await import("./db.js");
|
|
48
|
+
const { register } = await import("./auth.js");
|
|
49
|
+
const stamp = Date.now();
|
|
50
|
+
const owner = register(`gate_owner_${stamp}`, "GateOwner-Strong-1!");
|
|
51
|
+
expect(owner.ok).toBe(true);
|
|
52
|
+
ownerToken = owner.token!;
|
|
53
|
+
net = owner.network_id!;
|
|
54
|
+
ownerId = db.get<{ user_id: string }>("SELECT user_id FROM users WHERE username = ?1", `gate_owner_${stamp}`)!.user_id;
|
|
55
|
+
|
|
56
|
+
const seed = (nodeId: string, transport: string | null) => db.run(
|
|
57
|
+
`INSERT INTO nodes (node_id, node_name, alias, network_id, lifecycle_state, config_snapshot)
|
|
58
|
+
VALUES (?1, ?1, ?1, ?2, 'active', ?3)`,
|
|
59
|
+
[nodeId, net, JSON.stringify({ node_secrets_capable: true })],
|
|
60
|
+
);
|
|
61
|
+
seed(PLAIN, "plain");
|
|
62
|
+
seed(TLS, "https");
|
|
63
|
+
seed(LOOPBACK, "loopback");
|
|
64
|
+
seed(UNKNOWN, null);
|
|
65
|
+
// The transport is reported on the session row (that is where report_status
|
|
66
|
+
// writes it).
|
|
67
|
+
const session = (nodeId: string, transport: string | null) => db.run(
|
|
68
|
+
`INSERT INTO sessions (resume_id, alias, status, node_id, network_id, env_transport, updated_at, last_seen_at)
|
|
69
|
+
VALUES (?1, ?1, 'idle', ?1, ?2, ?3, datetime('now'), datetime('now'))`,
|
|
70
|
+
[nodeId, net, transport],
|
|
71
|
+
);
|
|
72
|
+
session(PLAIN, "plain");
|
|
73
|
+
session(TLS, "https");
|
|
74
|
+
session(LOOPBACK, "loopback");
|
|
75
|
+
session(UNKNOWN, null); // 从未上报 transport
|
|
76
|
+
|
|
77
|
+
const mod = await import("./server.js");
|
|
78
|
+
server = mod.bootServer({ port: 0, hostname: "127.0.0.1" });
|
|
79
|
+
base = `http://127.0.0.1:${server.port}`;
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
afterAll(() => {
|
|
83
|
+
try { server?.stop(true); } catch {}
|
|
84
|
+
try { rmSync(PRIVATE_DB_DIR, { recursive: true, force: true }); } catch {}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
beforeEach(async () => {
|
|
88
|
+
// The lane allows one pending request per node; drain whatever the previous
|
|
89
|
+
// case left so the next one starts clean.
|
|
90
|
+
const { pullNodeSecrets, ackNodeSecrets } = await import("./scoped-secrets.js");
|
|
91
|
+
for (const nodeId of [PLAIN, TLS, LOOPBACK, UNKNOWN]) {
|
|
92
|
+
const daemon = { ok: true as const, networkId: net, daemonNodeId: nodeId, daemonAlias: nodeId };
|
|
93
|
+
const pulled = pullNodeSecrets(daemon) as any;
|
|
94
|
+
for (const r of (pulled?.requests ?? [])) ackNodeSecrets(daemon, r.request_id, null, true);
|
|
95
|
+
}
|
|
96
|
+
process.env.ANET_SECRET_TRANSPORT_GATE = "log";
|
|
97
|
+
delete process.env.ANET_SECRET_TRANSPORT_GATE_STRICT;
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe("log mode (default)", () => {
|
|
101
|
+
test("a put to a plain node still succeeds AND leaves a hit", async () => {
|
|
102
|
+
const r = await post(`/api/nodes/${PLAIN}/secrets?network_id=${net}`, ownerToken, { action: "put", key: "A_KEY", value: "v", revision: 1 });
|
|
103
|
+
expect(r.status).toBe(200);
|
|
104
|
+
expect(r.body.ok).toBe(true); // not refused: that is the whole point of log mode
|
|
105
|
+
|
|
106
|
+
const log = await get(`/api/networks/${net}/env-transport-log`, ownerToken);
|
|
107
|
+
expect(log.status).toBe(200);
|
|
108
|
+
expect(log.body.total).toBe(1);
|
|
109
|
+
expect(log.body.rows[0]).toMatchObject({ node_id: PLAIN, route: "node_secret_put", hits: 1 });
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("a put to a TLS node leaves no hit", async () => {
|
|
113
|
+
const before = (await get(`/api/networks/${net}/env-transport-log`, ownerToken)).body.total;
|
|
114
|
+
const r = await post(`/api/nodes/${TLS}/secrets?network_id=${net}`, ownerToken, { action: "put", key: "B_KEY", value: "v", revision: 1 });
|
|
115
|
+
expect(r.status).toBe(200);
|
|
116
|
+
const after = (await get(`/api/networks/${net}/env-transport-log`, ownerToken)).body.total;
|
|
117
|
+
expect(after).toBe(before);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// #1510 acceptance leg 1: loopback is an allowed transport, so it must neither
|
|
121
|
+
// be refused nor pollute the would-refuse log (the log is the decision input
|
|
122
|
+
// for turning enforce on — a loopback hit in it would read as "something to
|
|
123
|
+
// block" when nothing is).
|
|
124
|
+
test("a put to a loopback node succeeds AND leaves no hit", async () => {
|
|
125
|
+
const before = (await get(`/api/networks/${net}/env-transport-log`, ownerToken)).body.total;
|
|
126
|
+
const r = await post(`/api/nodes/${LOOPBACK}/secrets?network_id=${net}`, ownerToken, { action: "put", key: "LB_KEY", value: "v", revision: 1 });
|
|
127
|
+
expect(r.status).toBe(200);
|
|
128
|
+
expect(r.body.ok).toBe(true);
|
|
129
|
+
const after = (await get(`/api/networks/${net}/env-transport-log`, ownerToken)).body.total;
|
|
130
|
+
expect(after).toBe(before);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("list is not observed (it carries no secret)", async () => {
|
|
134
|
+
const before = (await get(`/api/networks/${net}/env-transport-log`, ownerToken)).body.total;
|
|
135
|
+
await post(`/api/nodes/${PLAIN}/secrets?network_id=${net}`, ownerToken, { action: "list" });
|
|
136
|
+
const after = (await get(`/api/networks/${net}/env-transport-log`, ownerToken)).body.total;
|
|
137
|
+
expect(after).toBe(before);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe("enforce mode", () => {
|
|
142
|
+
test("the same put is refused, and nothing is queued", async () => {
|
|
143
|
+
process.env.ANET_SECRET_TRANSPORT_GATE = "enforce";
|
|
144
|
+
const r = await post(`/api/nodes/${PLAIN}/secrets?network_id=${net}`, ownerToken, { action: "put", key: "C_KEY", value: "v", revision: 1 });
|
|
145
|
+
expect(r.status).toBe(400);
|
|
146
|
+
expect(r.body.error).toBe("secret_transport_plain");
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test("a TLS node is unaffected", async () => {
|
|
150
|
+
process.env.ANET_SECRET_TRANSPORT_GATE = "enforce";
|
|
151
|
+
const r = await post(`/api/nodes/${TLS}/secrets?network_id=${net}`, ownerToken, { action: "put", key: "D_KEY", value: "v", revision: 1 });
|
|
152
|
+
expect(r.status).toBe(200);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// #1510 acceptance: "loopback 通过 / https 通过 / 明文公网被拒". enforce is
|
|
156
|
+
// defined as *refuse plain* — loopback is a local link and must keep working
|
|
157
|
+
// under enforce, otherwise turning the gate on would break the one transport
|
|
158
|
+
// it exists to protect.
|
|
159
|
+
test("a loopback node passes enforce too", async () => {
|
|
160
|
+
process.env.ANET_SECRET_TRANSPORT_GATE = "enforce";
|
|
161
|
+
const r = await post(`/api/nodes/${LOOPBACK}/secrets?network_id=${net}`, ownerToken, { action: "put", key: "LB2_KEY", value: "v", revision: 1 });
|
|
162
|
+
expect(r.status).toBe(200);
|
|
163
|
+
expect(r.body.ok).toBe(true);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
describe("off mode", () => {
|
|
168
|
+
test("records nothing at all", async () => {
|
|
169
|
+
process.env.ANET_SECRET_TRANSPORT_GATE = "off";
|
|
170
|
+
const before = (await get(`/api/networks/${net}/env-transport-log`, ownerToken)).body.total;
|
|
171
|
+
const r = await post(`/api/nodes/${PLAIN}/secrets?network_id=${net}`, ownerToken, { action: "put", key: "E_KEY", value: "v", revision: 1 });
|
|
172
|
+
expect(r.status).toBe(200);
|
|
173
|
+
const after = (await get(`/api/networks/${net}/env-transport-log`, ownerToken)).body.total;
|
|
174
|
+
expect(after).toBe(before);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
describe("the log endpoint is owner/admin only", () => {
|
|
179
|
+
test("an unrelated user gets 403", async () => {
|
|
180
|
+
const { register } = await import("./auth.js");
|
|
181
|
+
const other = register(`gate_other_${Date.now()}`, "GateOther-Strong-1!");
|
|
182
|
+
const r = await get(`/api/networks/${net}/env-transport-log`, other.token!);
|
|
183
|
+
expect(r.status).toBe(403);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
// #1510 补档 —— 「未知 transport」必须有名字,而且默认不能被当成「不安全」。
|
|
188
|
+
//
|
|
189
|
+
// 生产数据(2026-10-29 只读):373 个会话里 332 个 env_transport = NULL。
|
|
190
|
+
// 如果 enforce 默认就把 null 拒掉,打开开关当天就会拦掉近九成节点 —— 那不是收紧,
|
|
191
|
+
// 那是一次不在计划内的下线。所以:
|
|
192
|
+
// · 默认(strict 关):null 放行,且不进 would-refuse 表(不制造噪声)
|
|
193
|
+
// · 显式 ANET_SECRET_TRANSPORT_GATE_STRICT=1:null 拒,原因码单独叫
|
|
194
|
+
// secret_transport_unknown —— 运维看到这个码,知道要去做的是「让节点上报」,
|
|
195
|
+
// 而不是去查某台机器的网络链路
|
|
196
|
+
describe("#1510 未知 transport(未上报)", () => {
|
|
197
|
+
const putTo = (nodeId: string, key: string) =>
|
|
198
|
+
post(`/api/nodes/${nodeId}/secrets?network_id=${net}`, ownerToken, { action: "put", key, value: "v", revision: 1 });
|
|
199
|
+
|
|
200
|
+
test("log 模式:未上报的节点不受影响,也不进 would-refuse 表", async () => {
|
|
201
|
+
const before = (await get(`/api/networks/${net}/env-transport-log`, ownerToken)).body.total;
|
|
202
|
+
const r = await putTo(UNKNOWN, "U1_KEY");
|
|
203
|
+
expect(r.status).toBe(200);
|
|
204
|
+
expect(r.body.ok).toBe(true);
|
|
205
|
+
const after = (await get(`/api/networks/${net}/env-transport-log`, ownerToken)).body.total;
|
|
206
|
+
expect(after).toBe(before);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test("enforce 但 strict 关:未上报仍然放行(默认不收紧到「未知即拒」)", async () => {
|
|
210
|
+
process.env.ANET_SECRET_TRANSPORT_GATE = "enforce";
|
|
211
|
+
const r = await putTo(UNKNOWN, "U2_KEY");
|
|
212
|
+
expect(r.status).toBe(200);
|
|
213
|
+
expect(r.body.ok).toBe(true);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test("enforce + strict:未上报被拒,原因码是 secret_transport_unknown(不是 plain)", async () => {
|
|
217
|
+
process.env.ANET_SECRET_TRANSPORT_GATE = "enforce";
|
|
218
|
+
process.env.ANET_SECRET_TRANSPORT_GATE_STRICT = "1";
|
|
219
|
+
const r = await putTo(UNKNOWN, "U3_KEY");
|
|
220
|
+
expect(r.status).toBe(400);
|
|
221
|
+
expect(r.body.error).toBe("secret_transport_unknown");
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
test("enforce + strict:明确 plain 的原因码不受影响,仍是 secret_transport_plain", async () => {
|
|
225
|
+
process.env.ANET_SECRET_TRANSPORT_GATE = "enforce";
|
|
226
|
+
process.env.ANET_SECRET_TRANSPORT_GATE_STRICT = "1";
|
|
227
|
+
const r = await putTo(PLAIN, "U4_KEY");
|
|
228
|
+
expect(r.status).toBe(400);
|
|
229
|
+
expect(r.body.error).toBe("secret_transport_plain");
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
test("enforce + strict:loopback / https 仍然放行(收紧不能倒向坏另一边)", async () => {
|
|
233
|
+
process.env.ANET_SECRET_TRANSPORT_GATE = "enforce";
|
|
234
|
+
process.env.ANET_SECRET_TRANSPORT_GATE_STRICT = "1";
|
|
235
|
+
for (const [node, key] of [[LOOPBACK, "U5_KEY"], [TLS, "U6_KEY"]] as const) {
|
|
236
|
+
const r = await putTo(node, key);
|
|
237
|
+
expect(r.body.ok).toBe(true);
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
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, secretTransportVerdict } 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
|
+
*
|
|
62
|
+
* #1510 补丁:以前这里写死了「只有 plain 才记」,于是日得表**永远只反映一种原因**。
|
|
63
|
+
* 现在改成问 `secretTransportVerdict`(把模式仮定为 enforce):
|
|
64
|
+
* 「如果现在打开 enforce,这一笔会不会被拒、原因是什么」—— 表格才是决策输入。
|
|
65
|
+
* 默认 strict 关闭时结果与以前完全一致(只记 plain)。 */
|
|
66
|
+
export function observeSecretWrite(networkId: string, nodeId: string, route: string): void {
|
|
67
|
+
if (getEnvTransportMode() !== "log") return;
|
|
68
|
+
const transport = transportOf(networkId, nodeId);
|
|
69
|
+
const would = secretTransportVerdict(transport, { ...process.env, ANET_SECRET_TRANSPORT_GATE: "enforce" });
|
|
70
|
+
if (!would) return;
|
|
71
|
+
noteEnvTransportWouldRefuse(networkId, nodeId, route, `${would} transport=${transport ?? "unknown"}`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function readEnvTransportLog(networkId: string, sinceHour: string) {
|
|
75
|
+
return db.all<{ node_id: string; route: string; hour: string; hits: number | string; sample: string | null }>(
|
|
76
|
+
`SELECT node_id, route, hour, hits, sample FROM env_transport_would_refuse
|
|
77
|
+
WHERE network_id = ?1 AND hour >= ?2 ORDER BY hour DESC, node_id, route`,
|
|
78
|
+
[networkId, sinceHour],
|
|
79
|
+
).map(r => ({ ...r, hits: Number(r.hits) }));
|
|
80
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
|
|
19
|
+
/**
|
|
20
|
+
* #1510(补「未知」这一档)—— enforce 到底拒什么。
|
|
21
|
+
*
|
|
22
|
+
* 原实现是**拒绝名单**:只拒 `plain`,其余(含 `null` = 从未上报)一律放行。
|
|
23
|
+
* 卡里的口径是**允许名单**:「只有 loopback 或 https 才允许把密钥转发给节点」。
|
|
24
|
+
* 两者在 `null` 上分歧最大,而生产现状是(2026-10-29 只读统计,373 个会话):
|
|
25
|
+
*
|
|
26
|
+
* env_transport = NULL 332 ← 89% 从未上报
|
|
27
|
+
* plain 33
|
|
28
|
+
* loopback 7
|
|
29
|
+
* https 1
|
|
30
|
+
*
|
|
31
|
+
* 所以「未知即拒」不能直接当成默认:一打开 enforce 就会拦掉近九成节点。
|
|
32
|
+
* 它必须是一个**额外显式打开**的开关(`ANET_SECRET_TRANSPORT_GATE_STRICT=1`),
|
|
33
|
+
* 让运维在看清「未知有多少」之后自己决定什么时候收紧。
|
|
34
|
+
*
|
|
35
|
+
* 返回值直接就是拒绝原因码,避免调用方各自拼自符串:
|
|
36
|
+
* "secret_transport_plain" — 明确是明文公网
|
|
37
|
+
* "secret_transport_unknown" — 从未上报(仅 strict 下拒)
|
|
38
|
+
* null — 允许
|
|
39
|
+
*/
|
|
40
|
+
export type SecretTransportVerdict = "secret_transport_plain" | "secret_transport_unknown" | null;
|
|
41
|
+
|
|
42
|
+
/** 「未知即拒」的显式开关。默认关。 */
|
|
43
|
+
export function getEnvTransportStrict(env: Record<string, string | undefined> = process.env): boolean {
|
|
44
|
+
const raw = (env.ANET_SECRET_TRANSPORT_GATE_STRICT ?? "").trim().toLowerCase();
|
|
45
|
+
return raw === "1" || raw === "true" || raw === "on" || raw === "yes";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function secretTransportVerdict(
|
|
49
|
+
transport: string | null,
|
|
50
|
+
env: Record<string, string | undefined> = process.env,
|
|
51
|
+
): SecretTransportVerdict {
|
|
52
|
+
if (getEnvTransportMode(env) !== "enforce") return null;
|
|
53
|
+
if (transport === "plain") return "secret_transport_plain";
|
|
54
|
+
if (transport === "loopback" || transport === "https") return null;
|
|
55
|
+
return getEnvTransportStrict(env) ? "secret_transport_unknown" : null;
|
|
56
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
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
|
+
// 不关掉启动宽限的话第一个 tick 会被当成启动尖峰跳过(见 #2721 那组用例)。
|
|
103
|
+
bootGraceMs: 0,
|
|
104
|
+
});
|
|
105
|
+
const until = performance.now() + 120;
|
|
106
|
+
while (performance.now() < until) { /* spin 阻塞事件循环 */ }
|
|
107
|
+
await Bun.sleep(60);
|
|
108
|
+
clearInterval(timer);
|
|
109
|
+
expect(logs.length).toBeGreaterThan(0);
|
|
110
|
+
expect(logs[0]).toStartWith("[commhub stall] loop idle ~");
|
|
111
|
+
expect(logs[0]).toContain("rss 4502 MB");
|
|
112
|
+
expect(logs[0]).toContain("served 0 req");
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe("db-adapter: 慢 SQL 日志", () => {
|
|
117
|
+
test("默认阈值 1000ms;环境变量可调,0 表示关闭", () => {
|
|
118
|
+
const prev = process.env.COMMHUB_SLOW_SQL_MS;
|
|
119
|
+
try {
|
|
120
|
+
delete process.env.COMMHUB_SLOW_SQL_MS;
|
|
121
|
+
expect(slowSqlThresholdMs()).toBe(1000);
|
|
122
|
+
process.env.COMMHUB_SLOW_SQL_MS = "250";
|
|
123
|
+
expect(slowSqlThresholdMs()).toBe(250);
|
|
124
|
+
process.env.COMMHUB_SLOW_SQL_MS = "0";
|
|
125
|
+
expect(slowSqlThresholdMs()).toBe(0);
|
|
126
|
+
process.env.COMMHUB_SLOW_SQL_MS = "not-a-number";
|
|
127
|
+
expect(slowSqlThresholdMs()).toBe(1000);
|
|
128
|
+
} finally {
|
|
129
|
+
if (prev === undefined) delete process.env.COMMHUB_SLOW_SQL_MS;
|
|
130
|
+
else process.env.COMMHUB_SLOW_SQL_MS = prev;
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("日志压平换行并截断,且不含绑定参数", () => {
|
|
135
|
+
const sql = "DELETE FROM agent_telemetry\n WHERE created_at < datetime('now', ?1)";
|
|
136
|
+
const line = formatSlowSql(10_588.4, sql);
|
|
137
|
+
expect(line).toStartWith("[commhub slow-sql] 10588ms DELETE FROM agent_telemetry WHERE created_at < datetime('now', ?1)");
|
|
138
|
+
expect(line).not.toContain("\n");
|
|
139
|
+
const long = formatSlowSql(1_200, "SELECT " + "x".repeat(500));
|
|
140
|
+
expect(long.length).toBeLessThanOrEqual("[commhub slow-sql] 1200ms ".length + 160);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// #2721 —— **只跳过第一个 tick**。
|
|
145
|
+
//
|
|
146
|
+
// 事故形状:进程启动后 3 秒记到 gap 2972ms —— 那是「监控开始 → 第一次 tick」被
|
|
147
|
+
// 模块加载/开库/迁移占满的时间,度量的是启动耗时,不是线上停顿。
|
|
148
|
+
//
|
|
149
|
+
// 第一版实现用的是 10 秒**时间窗**,等于监控在前 10 秒什么都不记,
|
|
150
|
+
// 把下面那条「阈值 40ms、spin 120ms 应打停顿日志」的既有测试整体吞掉了
|
|
151
|
+
// —— 修完 qa.yml 事故、QA 恢复后立刻红。窗口越宽越像「把监控关一会儿」。
|
|
152
|
+
describe("#2721 启动尖峰不算停顿(判据:进程 uptime)", () => {
|
|
153
|
+
test("进程刚启动时的第一个 gap 被跳过", async () => {
|
|
154
|
+
resetEventLoopLagForTests();
|
|
155
|
+
const logs: string[] = [];
|
|
156
|
+
const timer = startEventLoopLagMonitor({
|
|
157
|
+
intervalMs: 20, thresholdMs: 40, log: (m) => logs.push(m), rssMb: () => 123,
|
|
158
|
+
bootGraceMs: 15_000, uptimeMs: () => 3_000, // 模拟「启动后 3 秒」
|
|
159
|
+
});
|
|
160
|
+
const until = performance.now() + 120;
|
|
161
|
+
while (performance.now() < until) { /* spin 落在第一个 tick */ }
|
|
162
|
+
await Bun.sleep(80);
|
|
163
|
+
clearInterval(timer);
|
|
164
|
+
expect(logs).toEqual([]);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test("进程已经跑了一会儿 ⇒ 第一个 gap 照常记录(不能把监控关掉)", async () => {
|
|
168
|
+
resetEventLoopLagForTests();
|
|
169
|
+
const logs: string[] = [];
|
|
170
|
+
const timer = startEventLoopLagMonitor({
|
|
171
|
+
intervalMs: 20, thresholdMs: 40, log: (m) => logs.push(m), rssMb: () => 123,
|
|
172
|
+
bootGraceMs: 15_000, uptimeMs: () => 600_000, // 进程跑了 10 分钟
|
|
173
|
+
});
|
|
174
|
+
const until = performance.now() + 120;
|
|
175
|
+
while (performance.now() < until) { /* spin */ }
|
|
176
|
+
await Bun.sleep(80);
|
|
177
|
+
clearInterval(timer);
|
|
178
|
+
expect(logs.length).toBeGreaterThan(0);
|
|
179
|
+
});
|
|
180
|
+
});
|