@sleep2agi/commhub-server 0.9.0-preview.26 → 0.9.0-preview.260
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +120 -3
- package/bin/commhub.ts +93 -9
- package/build-info.json +7 -0
- package/package.json +10 -8
- package/src/ack-create-request-transport.test.ts +182 -0
- package/src/ack-create-request.test.ts +52 -8
- package/src/admin-delete-network-http.test.ts +228 -0
- package/src/admin-networks-http.test.ts +88 -0
- package/src/adoption-candidates-http.test.ts +140 -0
- package/src/adoption-candidates.ts +158 -0
- package/src/agent-access-batch.test.ts +210 -0
- package/src/agent-access.ts +487 -0
- package/src/agent-acl-groups-http.test.ts +247 -0
- package/src/agent-acl-http.test.ts +680 -0
- package/src/agent-acl-slice3-http.test.ts +885 -0
- package/src/agent-department-http.test.ts +215 -0
- package/src/agent-groups-perf.test.ts +223 -0
- package/src/agent-notice.ts +48 -0
- package/src/agent-team-whoami-http.test.ts +98 -0
- package/src/agent-teams-http.test.ts +330 -0
- package/src/agent-teams.ts +244 -0
- package/src/alias-baseline-2571.test.ts +162 -0
- package/src/alias-baseline.ts +113 -0
- package/src/alias-filter.test.ts +72 -0
- package/src/alias-filter.ts +42 -0
- package/src/api-host-supervisors-can-create-nodes.test.ts +350 -0
- package/src/api-host-supervisors-fallback.test.ts +6 -7
- package/src/api-host-supervisors-runtime-readiness.test.ts +195 -0
- package/src/api-nodes-shape.test.ts +131 -4
- package/src/auth-abuse-counters.test.ts +125 -0
- package/src/auth-abuse-counters.ts +122 -0
- package/src/auth-password-change-cli-tokens-http.test.ts +423 -0
- package/src/auth-sessions-http.test.ts +260 -0
- package/src/auth-validate.test.ts +5 -1
- package/src/auth-weak-password-login.test.ts +176 -0
- package/src/auth.ts +609 -56
- package/src/backfill-node-ids-2664.test.ts +128 -0
- package/src/binding-generation-http.test.ts +141 -0
- package/src/blocked-exit-on-reply.test.ts +122 -0
- package/src/broadcast-fanout-2721.test.ts +149 -0
- package/src/broadcast-fanout.ts +117 -0
- package/src/build-commit-report-http.test.ts +128 -0
- package/src/build-info.test.ts +60 -0
- package/src/build-info.ts +85 -0
- package/src/chain-attempt-dedup.test.ts +447 -0
- package/src/clip.test.ts +36 -0
- package/src/clip.ts +48 -0
- package/src/clone-node.test.ts +269 -0
- package/src/clone-node.ts +289 -0
- package/src/codex-config-flags.ts +77 -0
- package/src/codex-fork-contract.ts +23 -0
- package/src/codex-fork-migration.test.ts +38 -0
- package/src/commhub-bin-args.test.ts +85 -0
- package/src/config-apply-validate.test.ts +188 -1
- package/src/config-apply-validate.ts +222 -3
- package/src/cors-origins.test.ts +73 -0
- package/src/create-node-blocked-gate.test.ts +365 -0
- package/src/create-node-error-domain.test.ts +124 -0
- package/src/create-node-network-id.test.ts +91 -0
- package/src/create-node-tool-schema.test.ts +49 -0
- package/src/create-node-unicode-name.test.ts +126 -0
- package/src/create-node-validate.test.ts +199 -7
- package/src/create-node-validate.ts +232 -15
- package/src/create-node-workdir.test.ts +298 -0
- package/src/create-node.ts +88 -23
- package/src/daemon-provider-snapshots.test.ts +453 -0
- package/src/daemon-provider-snapshots.ts +327 -0
- package/src/daemon-token-owner-http.test.ts +346 -0
- package/src/dashboard-slash-routing-http.test.ts +109 -0
- package/src/db-adapter-guard.test.ts +148 -0
- package/src/db-adapter-translate.test.ts +128 -0
- package/src/db-adapter.ts +377 -91
- package/src/db-timestamp.test.ts +38 -0
- package/src/db-timestamp.ts +15 -0
- package/src/db.ts +1269 -153
- package/src/delivered-without-push.test.ts +140 -0
- package/src/department-groups-http.test.ts +209 -0
- package/src/department-groups-sync-http.test.ts +328 -0
- package/src/department-groups.ts +434 -0
- package/src/department-heads-http.test.ts +350 -0
- package/src/department-heads.ts +226 -0
- package/src/departments-agent-count.test.ts +139 -0
- package/src/departments-http.test.ts +185 -0
- package/src/departments-mcp-error-domain.test.ts +135 -0
- package/src/departments-mcp.test.ts +128 -0
- package/src/departments.ts +318 -0
- package/src/desktop-message-seam.test.ts +150 -0
- package/src/desktop-message.test.ts +368 -0
- package/src/dm-file-scope-http.test.ts +178 -0
- package/src/empty-node-sweeper.test.ts +104 -0
- package/src/empty-node-sweeper.ts +88 -0
- package/src/env-transport-gate-http.test.ts +240 -0
- package/src/env-transport-gate.ts +80 -0
- package/src/env-transport-policy.ts +56 -0
- package/src/etag-of.test.ts +57 -0
- package/src/event-loop-lag.test.ts +180 -0
- package/src/event-loop-lag.ts +201 -0
- package/src/expiry-counters.test.ts +187 -0
- package/src/expiry-counters.ts +227 -0
- package/src/expiry-evidence-db.test.ts +103 -0
- package/src/ext-token-shared.test.ts +60 -0
- package/src/ext-validation-regression.test.ts +10 -17
- package/src/external-schedule-edits-http.test.ts +321 -0
- package/src/external-schedule-edits-timestamps.test.ts +64 -0
- package/src/external-schedule-edits.ts +327 -0
- package/src/file-download-authz-dev-open.test.ts +1 -1
- package/src/file-download-authz.test.ts +25 -4
- package/src/file-download-date-bucket.test.ts +1 -1
- package/src/file-network-scope-dev-open.test.ts +1 -1
- package/src/file-network-scope.test.ts +7 -6
- package/src/group-membership-events-http.test.ts +264 -0
- package/src/group-messages-http.test.ts +483 -0
- package/src/group-messages.ts +297 -0
- package/src/health-anon-readers.ts +67 -0
- package/src/health-host-disk-2756.test.ts +71 -0
- package/src/health-redaction.test.ts +1576 -7
- package/src/host-supervisor-identity-http.test.ts +152 -0
- package/src/host-supervisor-identity.ts +92 -0
- package/src/host-supervisors-daemon-version.test.ts +137 -0
- package/src/http-gzip.test.ts +94 -0
- package/src/http-gzip.ts +112 -0
- package/src/hub-catalog-read-http.test.ts +641 -0
- package/src/hub-provider-config.test.ts +166 -0
- package/src/hub-provider-config.ts +202 -0
- package/src/hub-timestamp.test.ts +38 -0
- package/src/hub-timestamp.ts +27 -0
- package/src/human-dm.ts +215 -0
- package/src/human-inbox.ts +42 -0
- package/src/inbox-caller.ts +49 -0
- package/src/inbox-count-sse.test.ts +318 -0
- package/src/inbox-count.ts +29 -0
- package/src/index.ts +6 -2
- package/src/license-state.test.ts +56 -0
- package/src/license-state.ts +48 -0
- package/src/lifecycle-error-domain.test.ts +109 -0
- package/src/list-ack-error-domain.test.ts +108 -0
- package/src/list-host-supervisors.test.ts +15 -4
- package/src/mcp-context-cost-http.test.ts +127 -0
- package/src/mcp-inflight.test.ts +92 -0
- package/src/mcp-inflight.ts +38 -0
- package/src/mcp-projects-events-http.test.ts +168 -0
- package/src/mcp-route-label-http.test.ts +120 -0
- package/src/mcp-write-network-resolution.test.ts +21 -0
- package/src/member-audit-network-id-http.test.ts +93 -0
- package/src/member-presence-http.test.ts +210 -0
- package/src/messages-ack-by-agent-http.test.ts +142 -0
- package/src/migrate-sqlite-to-pg.test.ts +47 -0
- package/src/migrate-sqlite-to-pg.ts +263 -0
- package/src/model-auth-notify-http.test.ts +181 -0
- package/src/model-auth-notify.ts +116 -0
- package/src/network-name-http.test.ts +75 -0
- package/src/network-rules-http.test.ts +169 -0
- package/src/network-rules.ts +129 -0
- package/src/network-scope.ts +222 -3
- package/src/network-token-membership-http.test.ts +186 -0
- package/src/node-alias-control.ts +12 -0
- package/src/node-attrs.test.ts +47 -2
- package/src/node-caller-identity-http.test.ts +1108 -0
- package/src/node-create-request-status-http.test.ts +105 -0
- package/src/node-daemon-bindings.test.ts +142 -0
- package/src/node-daemon-bindings.ts +135 -0
- package/src/node-failure-alert.test.ts +150 -0
- package/src/node-failure-alert.ts +177 -0
- package/src/node-files-transport.test.ts +350 -0
- package/src/node-health-codex-login.test.ts +120 -0
- package/src/node-health-dispatch-http.test.ts +373 -0
- package/src/node-health-guard.ts +121 -0
- package/src/node-health-passthrough.test.ts +115 -0
- package/src/node-health-store.ts +101 -0
- package/src/node-id-alias.test.ts +190 -0
- package/src/node-identity-conflict-http.test.ts +269 -0
- package/src/node-identity-conflict.ts +142 -0
- package/src/node-identity-mismatch.test.ts +74 -0
- package/src/node-in-flight-tasks.ts +31 -0
- package/src/node-lifecycle-controllable.test.ts +64 -0
- package/src/node-lifecycle-controllable.ts +53 -0
- package/src/node-lifecycle-read-http.test.ts +257 -0
- package/src/node-lifecycle-read.ts +124 -0
- package/src/node-logs-transport.test.ts +266 -0
- package/src/node-peer-allowlist.test.ts +114 -0
- package/src/node-permissions-http.test.ts +728 -0
- package/src/node-permissions.ts +494 -0
- package/src/node-request-retention.test.ts +281 -0
- package/src/node-request-retention.ts +120 -0
- package/src/node-secret-rest-http.test.ts +190 -0
- package/src/node-security-write-gate.test.ts +72 -0
- package/src/node-serviceability.test.ts +86 -0
- package/src/node-serviceability.ts +124 -0
- package/src/node-session-control-http.test.ts +310 -0
- package/src/node-session-control.ts +258 -0
- package/src/node-skills-transport.test.ts +212 -0
- package/src/node-token-identity.ts +35 -0
- package/src/node-token-ownership.ts +51 -0
- package/src/node-token-privilege-http.test.ts +158 -0
- package/src/not-found-api-http.test.ts +67 -0
- package/src/observer-avatar-http.test.ts +103 -10
- package/src/observer-push.test.ts +76 -0
- package/src/opencode-catalog-report-http.test.ts +139 -0
- package/src/opencode-v2-catalog.test.ts +268 -0
- package/src/opencode-v2-catalog.ts +356 -0
- package/src/package-version-consistency.test.ts +25 -0
- package/src/parent-source-marker.test.ts +50 -0
- package/src/password-network-tokens.ts +25 -0
- package/src/peer-reply-atomic.test.ts +462 -0
- package/src/peer-reply-capability.test.ts +70 -0
- package/src/pg-worker.ts +87 -0
- package/src/poll-pacing.test.ts +74 -0
- package/src/poll-pacing.ts +123 -0
- package/src/probe-provider-error-domain.test.ts +141 -0
- package/src/project-dir-warn-clear-http.test.ts +132 -0
- package/src/project-viewer-can-http.test.ts +116 -0
- package/src/provider-skill-error-domain.test.ts +130 -0
- package/src/provider-tools-shape.test.ts +165 -0
- package/src/push.test.ts +20 -10
- package/src/push.ts +373 -9
- package/src/redact-tokens.test.ts +86 -0
- package/src/redact-tokens.ts +43 -0
- package/src/rejection-counters.test.ts +72 -0
- package/src/rejection-counters.ts +173 -0
- package/src/rename.ts +38 -10
- package/src/reply-target-derivation.test.ts +96 -0
- package/src/report-status-handover-capabilities-http.test.ts +124 -0
- package/src/report-status-host-schema.test.ts +235 -0
- package/src/report-status-resume-id-handover.test.ts +184 -0
- package/src/report-status-secret-keys.test.ts +140 -0
- package/src/require-explicit-test-db.ts +9 -0
- package/src/requirement-due-reminders-http.test.ts +620 -0
- package/src/requirement-due-reminders.ts +416 -0
- package/src/requirement-events-http.test.ts +318 -0
- package/src/requirement-events.test.ts +45 -0
- package/src/requirement-events.ts +144 -0
- package/src/requirement-notify.ts +165 -0
- package/src/requirement-participant-notify-http.test.ts +313 -0
- package/src/requirement-tag-ops-http.test.ts +454 -0
- package/src/requirement-tag-tree-http.test.ts +275 -0
- package/src/requirement-tag-tree.ts +127 -0
- package/src/requirement-tags.ts +102 -0
- package/src/requirements-abandoned-http.test.ts +236 -0
- package/src/requirements-assignee-drift.test.ts +154 -0
- package/src/requirements-comment-http.test.ts +174 -0
- package/src/requirements-completed-at-migration.test.ts +70 -0
- package/src/requirements-errors-mcp-http.test.ts +164 -0
- package/src/requirements-errors.ts +158 -0
- package/src/requirements-etag-http.test.ts +91 -0
- package/src/requirements-http.test.ts +709 -0
- package/src/requirements-last-event-http.test.ts +349 -0
- package/src/requirements-list-mcp-slim-http.test.ts +155 -0
- package/src/requirements-list-slim-http.test.ts +324 -0
- package/src/requirements-mcp.test.ts +180 -0
- package/src/requirements-migrate.ts +283 -0
- package/src/requirements-owner-strict-mcp-http.test.ts +145 -0
- package/src/requirements-people-http.test.ts +114 -0
- package/src/requirements-people-mcp-http.test.ts +231 -0
- package/src/requirements-people.ts +196 -0
- package/src/requirements-priority-lowest.test.ts +114 -0
- package/src/requirements-projects-network-echo.test.ts +57 -0
- package/src/requirements-projects-parent-http.test.ts +123 -0
- package/src/requirements-search-http.test.ts +182 -0
- package/src/requirements-search.test.ts +32 -0
- package/src/requirements-search.ts +145 -0
- package/src/requirements-seq-http.test.ts +193 -0
- package/src/requirements-seq-migration.test.ts +63 -0
- package/src/requirements-start-date.test.ts +125 -0
- package/src/requirements-stats-http.test.ts +357 -0
- package/src/requirements-stats.ts +195 -0
- package/src/requirements-status-participants-http.test.ts +222 -0
- package/src/requirements-upgrade-e2e.test.ts +126 -0
- package/src/requirements-upgrade-migration.test.ts +98 -0
- package/src/requirements.ts +2205 -0
- package/src/resolve-port.test.ts +57 -0
- package/src/resolve-port.ts +44 -0
- package/src/rest-alias-network-fallback-http.test.ts +123 -0
- package/src/rest-alias-network-fallback.test.ts +60 -0
- package/src/rest-alias-network-fallback.ts +65 -0
- package/src/rest-explicit-columns-http.test.ts +266 -0
- package/src/rest-identity.test.ts +130 -0
- package/src/rest-identity.ts +141 -0
- package/src/rest-projections.ts +72 -0
- package/src/rest-read-network-real-chain.test.ts +97 -0
- package/src/rest-write-network-resolution.test.ts +133 -0
- package/src/restart-readiness-http.test.ts +121 -0
- package/src/restricted-files.ts +76 -0
- package/src/retention-first-sweep-quiet.test.ts +31 -0
- package/src/retention.test.ts +100 -40
- package/src/retention.ts +423 -104
- package/src/route-callers-http.test.ts +120 -0
- package/src/route-timing.test.ts +218 -0
- package/src/route-timing.ts +262 -0
- package/src/rules-file-duplicate-alias.test.ts +234 -0
- package/src/rules-file-session-target.test.ts +207 -0
- package/src/rules-file-transport.test.ts +271 -0
- package/src/runtime-label.test.ts +22 -0
- package/src/runtime-label.ts +21 -0
- package/src/runtime-readiness.ts +90 -0
- package/src/schedule-agent-mcp-http.test.ts +486 -0
- package/src/schedule-agent.ts +252 -0
- package/src/schedule-starved.test.ts +92 -0
- package/src/schedule-starved.ts +114 -0
- package/src/scheduled-backoff-http.test.ts +203 -0
- package/src/scheduled-backoff.ts +64 -0
- package/src/scheduled-doorbell-nodeless-session.test.ts +134 -0
- package/src/scheduled-failure-backoff-2622.test.ts +145 -0
- package/src/scheduled-failure-code.test.ts +73 -0
- package/src/scheduled-failure-code.ts +105 -0
- package/src/scheduled-failures-http.test.ts +279 -0
- package/src/scheduled-failures.ts +425 -0
- package/src/scheduled-noprogress-backoff-2622.test.ts +163 -0
- package/src/scheduled-offline-backoff-probe.test.ts +56 -0
- package/src/scheduled-offline.test.ts +131 -0
- package/src/scheduled-offline.ts +66 -0
- package/src/scheduled-outcome-guidance-http.test.ts +139 -0
- package/src/scheduled-outcome-guidance.ts +9 -0
- package/src/scheduled-reply-unread-http.test.ts +212 -0
- package/src/scheduled-requirement-wake-http.test.ts +289 -0
- package/src/scheduled-requirement-wake.ts +125 -0
- package/src/scheduled-run-terminal.test.ts +290 -0
- package/src/scheduled-skip-blocking-task.test.ts +120 -0
- package/src/scheduled-skip-path-backoff-2622.test.ts +98 -0
- package/src/scheduled-stuck-http.test.ts +316 -0
- package/src/scheduled-stuck-notice-cap.test.ts +80 -0
- package/src/scheduled-stuck.ts +219 -0
- package/src/scheduled-tasks-http.test.ts +679 -0
- package/src/scheduled-tasks.ts +959 -0
- package/src/scheduled-work-outcome-http.test.ts +154 -0
- package/src/scheduled-work-outcome.ts +50 -0
- package/src/scoped-secrets.ts +141 -0
- package/src/secret-echo-negative-control.test.ts +118 -0
- package/src/send-dedup-request-id.test.ts +229 -0
- package/src/send-reply-agent-warning.test.ts +38 -38
- package/src/send-reply-alias-optional.test.ts +131 -0
- package/src/send-reply-attachments.test.ts +28 -7
- package/src/send_dedup.test.ts +54 -0
- package/src/send_dedup.ts +52 -6
- package/src/serve-error.test.ts +73 -0
- package/src/serve-error.ts +56 -0
- package/src/server.ts +4842 -325
- package/src/session-task-on-dispatch.ts +14 -0
- package/src/session-token-ownership-http.test.ts +122 -0
- package/src/shared/codex-inventory.ts +41 -0
- package/src/shared/daemon-provider-login.ts +31 -0
- package/src/shared/daemon-provider-snapshot.ts +52 -0
- package/src/shared/external-schedule-contract.ts +60 -0
- package/src/shared/node-name.test.ts +62 -0
- package/src/shared/node-name.ts +161 -0
- package/src/shared/probe-host-allowlist-drift.test.ts +45 -0
- package/src/shared/provider-application.ts +29 -0
- package/src/shared/provider-collection.ts +14 -0
- package/src/shared/provider-initialization.ts +8 -0
- package/src/shared/reserved-env-drift.test.ts +10 -0
- package/src/shared/scoped-secret-contract.ts +5 -0
- package/src/shared/task-content-limit-drift.test.ts +28 -0
- package/src/shared/task-content-limit.ts +25 -0
- package/src/side-thread-command-transport.test.ts +312 -0
- package/src/side-thread-command-transport.ts +318 -0
- package/src/side-thread-http-integration.test.ts +233 -0
- package/src/side-thread-http.ts +504 -0
- package/src/side-thread-production.test.ts +72 -0
- package/src/side-thread-production.ts +118 -0
- package/src/side-thread.test.ts +1067 -0
- package/src/side-thread.ts +2499 -0
- package/src/skillhub-http.test.ts +144 -0
- package/src/sqlite-close-checkpoint.test.ts +78 -0
- package/src/start-node.test.ts +265 -0
- package/src/start-request-error-domain.test.ts +182 -0
- package/src/start-skill-error-domain.test.ts +122 -0
- package/src/stats-versions-http.test.ts +65 -0
- package/src/status-alias-filter-http.test.ts +106 -0
- package/src/status-alias-resolver-http.test.ts +163 -0
- package/src/status-alias-resolver.ts +19 -0
- package/src/status-node-id-filter-http.test.ts +161 -0
- package/src/status-read-cache-http.test.ts +229 -0
- package/src/status-read-cache.ts +129 -0
- package/src/stop-delete-node.test.ts +567 -1
- package/src/task-access-http.test.ts +402 -0
- package/src/task-access.ts +420 -0
- package/src/task-activity.ts +60 -0
- package/src/task-auth-origin-http.test.ts +93 -0
- package/src/task-auth-origin.test.ts +24 -0
- package/src/task-auth-origin.ts +7 -0
- package/src/task-consumption.test.ts +508 -0
- package/src/task-diagnostic-http.test.ts +149 -0
- package/src/task-diagnostic.test.ts +115 -0
- package/src/task-diagnostic.ts +118 -0
- package/src/task-expiry-notice-http.test.ts +298 -0
- package/src/task-expiry-notice.ts +318 -0
- package/src/task-idempotency-mcp.test.ts +148 -0
- package/src/task-idempotency.test.ts +76 -0
- package/src/task-idempotency.ts +81 -0
- package/src/task-late-replies.ts +54 -0
- package/src/task-late-reply-http.test.ts +227 -0
- package/src/task-lifecycle-watcher-busy.test.ts +114 -0
- package/src/task-lifecycle-watcher.test.ts +209 -0
- package/src/task-lifecycle-watcher.ts +109 -0
- package/src/task-network-resolution-http.test.ts +140 -0
- package/src/task-not-found-error-domain.test.ts +76 -0
- package/src/task-offline-delivery-2668.test.ts +119 -0
- package/src/task-offline-delivery.ts +81 -0
- package/src/task-orphan-http.test.ts +287 -0
- package/src/task-orphan.ts +119 -0
- package/src/task-queue-ahead-http.test.ts +309 -0
- package/src/task-queue-ahead-scale.test.ts +236 -0
- package/src/task-queue-ahead.ts +186 -0
- package/src/task-rejection-log-http.test.ts +160 -0
- package/src/task-rejection-log.ts +61 -0
- package/src/task-sender-http.test.ts +143 -0
- package/src/task-stale-open-http.test.ts +292 -0
- package/src/task-stale-open.ts +103 -0
- package/src/tasks-pagination-http.test.ts +141 -0
- package/src/team-rules-status-http.test.ts +90 -0
- package/src/team-rules-status.test.ts +59 -0
- package/src/team-rules-status.ts +72 -0
- package/src/tmux-allowlist.test.ts +48 -0
- package/src/tool-audience-http.test.ts +248 -0
- package/src/tool-audience.ts +127 -0
- package/src/tools-list-json-schema.test.ts +94 -0
- package/src/tools.ts +4215 -397
- package/src/update-node-config-error-domain.test.ts +107 -0
- package/src/uploads-http.test.ts +6 -4
- package/src/uploads.ts +23 -4
- package/src/user-avatar-http.test.ts +74 -0
- package/src/user-avatar-image.ts +160 -0
- package/src/user-avatar-migration.test.ts +25 -0
- package/src/user-avatar-upload-http.test.ts +348 -0
- package/src/user-inbox-by-agent-http.test.ts +165 -0
- package/src/user-inbox-netid-notnull.test.ts +122 -0
- package/src/user-inbox-read-http.test.ts +223 -0
- package/src/user-inbox-schema.test.ts +54 -0
- package/src/version-stats.test.ts +66 -0
- package/src/version-stats.ts +109 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
// 部门群(RFC-042,看板 #457)—— 第三个 PR:群消息、未读、实时推送。
|
|
2
|
+
//
|
|
3
|
+
// 只增迁移:两张新表(chat_group_messages、chat_group_reads),零改列。旧代码不读它们,回滚安全。
|
|
4
|
+
//
|
|
5
|
+
// 尽量照私信(human-dm.ts)做,App 能复用私信界面:
|
|
6
|
+
// - 正文上限 MAX_DM_CHARS(1 万字)、附件走同一个 validateAttachments、同样的文件可见性校验;
|
|
7
|
+
// - 去重按 client_request_id(同一个人在同一个群里重投同一个 id → 同一条消息,不发第二条、不推第二次),
|
|
8
|
+
// 不按内容哈希(hub 旧私信去重按内容吞掉用户的重复消息,那是 bug,见 reference_hub_send_dedup);
|
|
9
|
+
// - 消息行形状对齐 user_inbox 私信行:message_id / network_id / sender_user_id / from_session / kind / content /
|
|
10
|
+
// meta_json / created_at(毫秒)+ direction,另加 group_id 和 seq(翻页游标)。
|
|
11
|
+
// - 推送复用 /events/users/me(pushUserEvent),新事件类型 group_message,发给发送时**当前**的每个群成员(含发信人自己:
|
|
12
|
+
// 多端同步;App 按 message_id 去重)。被同步移出的人自然收不到 —— 收件人在发送那一刻从 chat_group_members 现取。
|
|
13
|
+
//
|
|
14
|
+
// 一条消息一行(不按人复制);每人已读位置一行(chat_group_reads.last_read_seq,只前进不后退)。
|
|
15
|
+
// 未读 = 群里 seq > 已读位置、且在我入群之后发的、不是我自己发的消息条数。
|
|
16
|
+
// 读写只认「当前是群成员」:被移出的人(同步移出、手动移出、移出网络)立刻读不到这个群的任何历史,也打不开群里的附件;
|
|
17
|
+
// 重新入群后整段历史又可见(群历史属于群,不属于某个人)。
|
|
18
|
+
|
|
19
|
+
import { createHash } from "node:crypto";
|
|
20
|
+
import { db, uuidv4 } from "./db.js";
|
|
21
|
+
import { isAgentRestricted } from "./agent-access.js";
|
|
22
|
+
import { pushUserEvent, hasUserSubscribers } from "./push.js";
|
|
23
|
+
import { DM_FILE_SCOPE, FILE_ID_REGEX, validateAttachments } from "./uploads.js";
|
|
24
|
+
import { restrictedMemberCanUseFile } from "./restricted-files.js";
|
|
25
|
+
import { redactMessageRow } from "./redact-tokens.js";
|
|
26
|
+
import { MAX_DM_CHARS, dmParticipantSeesFile, networkFileEntry } from "./human-dm.js";
|
|
27
|
+
import { isGroupMember, publicDisplayName } from "./department-groups.js";
|
|
28
|
+
|
|
29
|
+
// 两张表的 DDL 在 department-groups.ts 里(和 chat_groups 一起建,删网络时一起删,不形成模块环)。
|
|
30
|
+
|
|
31
|
+
export const GROUP_MESSAGE_KIND = "group_message";
|
|
32
|
+
export const GROUP_MESSAGE_EVENT = "group_message";
|
|
33
|
+
export const GROUP_READ_EVENT = "group_read";
|
|
34
|
+
const CLIENT_REQUEST_ID = /^[A-Za-z0-9._-]{1,80}$/;
|
|
35
|
+
const MSG_COLS = "seq, message_id, group_id, network_id, sender_user_id, from_session, content, meta_json, created_at";
|
|
36
|
+
|
|
37
|
+
type Fail = { ok: false; status: number; error: string };
|
|
38
|
+
const fail = (status: number, error: string): Fail => ({ ok: false, status, error });
|
|
39
|
+
type MsgRow = { seq: number | string; message_id: string; group_id: string; network_id: string; sender_user_id: string; from_session: string; content: string; meta_json: string | null; created_at: string };
|
|
40
|
+
|
|
41
|
+
function shape(row: MsgRow, meUserId: string) {
|
|
42
|
+
return {
|
|
43
|
+
...redactMessageRow(row),
|
|
44
|
+
seq: Number(row.seq),
|
|
45
|
+
kind: GROUP_MESSAGE_KIND,
|
|
46
|
+
direction: row.sender_user_id === meUserId ? "out" : "in",
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 消息接口的门:群在这个网络里、我是当前成员。否则 null(调用方一律 404 group_not_found,不分「没有」和「不让看」)。 */
|
|
51
|
+
export function memberGroup(networkId: string, groupId: string, userId: string): { group_id: string; name: string } | null {
|
|
52
|
+
const g = db.get<{ group_id: string; name: string }>("SELECT group_id, name FROM chat_groups WHERE network_id = ?1 AND group_id = ?2", networkId, groupId);
|
|
53
|
+
if (!g || !isGroupMember(groupId, userId)) return null;
|
|
54
|
+
return g;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 这个用户是不是**当前**某个群的成员、且那个群里有一条消息带着这个文件。群里的人因此能下载群消息的附件
|
|
59
|
+
* (私信文件和受限成员两条分支都认它,见 server.ts authorizeFileDownload)。被移出群 = 立刻失去。
|
|
60
|
+
* LIKE 只做预筛,真正的判定按 attachments 键解析(和 dmParticipantSeesFile 同一个写法)。
|
|
61
|
+
*/
|
|
62
|
+
export function groupMemberSeesFile(userId: string, networkId: string, fileId: string): boolean {
|
|
63
|
+
if (!FILE_ID_REGEX.test(fileId)) return false;
|
|
64
|
+
const needle = `%"${fileId.replace(/_/g, "\\_")}"%`;
|
|
65
|
+
const rows = db.all<{ meta_json: string | null }>(
|
|
66
|
+
`SELECT g.meta_json FROM chat_group_messages g
|
|
67
|
+
JOIN chat_group_members m ON m.group_id = g.group_id AND m.user_id = ?2
|
|
68
|
+
WHERE g.network_id = ?1 AND g.meta_json LIKE ?3 ESCAPE '\\' LIMIT 50`,
|
|
69
|
+
networkId, userId, needle,
|
|
70
|
+
);
|
|
71
|
+
return rows.some((row) => {
|
|
72
|
+
try {
|
|
73
|
+
const list = JSON.parse(row.meta_json ?? "null")?.attachments;
|
|
74
|
+
return Array.isArray(list) && list.some((a: any) => a && typeof a === "object" && a.file_id === fileId);
|
|
75
|
+
} catch { return false; }
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export type GroupSendInput = {
|
|
80
|
+
networkId: string;
|
|
81
|
+
groupId: string;
|
|
82
|
+
sender: { userId: string; username: string };
|
|
83
|
+
message?: unknown;
|
|
84
|
+
attachments?: unknown;
|
|
85
|
+
clientRequestId?: unknown;
|
|
86
|
+
};
|
|
87
|
+
export type GroupSendResult =
|
|
88
|
+
| { ok: true; message: ReturnType<typeof shape>; duplicate: boolean; delivered_to: number }
|
|
89
|
+
| Fail;
|
|
90
|
+
|
|
91
|
+
export function sendGroupMessage(input: GroupSendInput): GroupSendResult {
|
|
92
|
+
const { networkId, groupId, sender } = input;
|
|
93
|
+
const group = memberGroup(networkId, groupId, sender.userId);
|
|
94
|
+
if (!group) return fail(404, "group_not_found");
|
|
95
|
+
const text = typeof input.message === "string" ? input.message : "";
|
|
96
|
+
if (text.length > MAX_DM_CHARS) return fail(400, "message_too_long");
|
|
97
|
+
const attachments = validateAttachments(input.attachments);
|
|
98
|
+
if (!attachments.ok) return fail(400, "bad_attachments");
|
|
99
|
+
if (!text.trim() && attachments.attachments.length === 0) return fail(400, "message_required");
|
|
100
|
+
|
|
101
|
+
// 文件校验与私信同一套:必须是本网络的文件;受限成员只能发自己能用的;私信文件只能由看得见它的人转发
|
|
102
|
+
// (上传者、带着它的私信里的人、带着它的群里的当前成员)—— 否则把别人私信里的 file_id 塞进群,就给全群解锁了它。
|
|
103
|
+
const restricted = isAgentRestricted(sender.userId, networkId);
|
|
104
|
+
for (const a of attachments.attachments) {
|
|
105
|
+
const fileId = (a as { file_id?: string }).file_id;
|
|
106
|
+
if (!fileId) continue;
|
|
107
|
+
const entry = networkFileEntry(networkId, fileId);
|
|
108
|
+
const usable = restricted
|
|
109
|
+
? restrictedMemberCanUseFile(sender.userId, sender.username, networkId, fileId) || (!!entry && groupMemberSeesFile(sender.userId, networkId, fileId))
|
|
110
|
+
: !!entry;
|
|
111
|
+
if (!usable) return fail(403, "attachment_not_accessible");
|
|
112
|
+
if (entry?.scope === DM_FILE_SCOPE && entry.owner_id !== sender.userId
|
|
113
|
+
&& !dmParticipantSeesFile(sender.userId, networkId, fileId) && !groupMemberSeesFile(sender.userId, networkId, fileId)) {
|
|
114
|
+
return fail(403, "attachment_not_accessible");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const clientRequestId = typeof input.clientRequestId === "string" && CLIENT_REQUEST_ID.test(input.clientRequestId) ? input.clientRequestId : null;
|
|
119
|
+
// 同一个气泡重试:按 (群, 发信人, client_request_id) 定出同一个 message_id(哈希,不截断拼接,避免长 id 截出碰撞)。
|
|
120
|
+
const messageId = clientRequestId
|
|
121
|
+
? `gm_${createHash("sha256").update(`${groupId}\n${sender.userId}\n${clientRequestId}`).digest("hex").slice(0, 32)}`
|
|
122
|
+
: `gm_${uuidv4().replace(/-/g, "")}`;
|
|
123
|
+
const metaJson = attachments.attachments.length ? JSON.stringify({ attachments: attachments.attachments }) : null;
|
|
124
|
+
|
|
125
|
+
let inserted = false;
|
|
126
|
+
db.transaction(() => {
|
|
127
|
+
const r = db.run(
|
|
128
|
+
`INSERT INTO chat_group_messages (message_id, group_id, network_id, sender_user_id, from_session, content, meta_json, created_at)
|
|
129
|
+
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, strftime('%Y-%m-%d %H:%M:%f', 'now'))
|
|
130
|
+
ON CONFLICT(message_id) DO NOTHING`,
|
|
131
|
+
[messageId, groupId, networkId, sender.userId, sender.username, text, metaJson],
|
|
132
|
+
);
|
|
133
|
+
inserted = (r?.changes ?? 0) > 0;
|
|
134
|
+
if (!inserted) return;
|
|
135
|
+
db.run(
|
|
136
|
+
`INSERT INTO audit_log (user_id, username, action, target_type, target_id, detail, network_id)
|
|
137
|
+
VALUES (?1, ?2, 'chat_group_message_sent', 'chat_group', ?3, ?4, ?5)`,
|
|
138
|
+
[sender.userId, sender.username, groupId, JSON.stringify({ message_id: messageId, attachments: attachments.attachments.length }), networkId],
|
|
139
|
+
);
|
|
140
|
+
});
|
|
141
|
+
const row = db.get<MsgRow>(`SELECT ${MSG_COLS} FROM chat_group_messages WHERE message_id = ?1`, messageId)!;
|
|
142
|
+
const message = shape(row, sender.userId);
|
|
143
|
+
// 重投:同一条消息原样返回,不再推送(第一次已经推过)。推送在提交之后:回滚的消息不能先被看见。
|
|
144
|
+
const deliveredTo = inserted ? pushGroupMessage(networkId, group, row) : 0;
|
|
145
|
+
return { ok: true, message, duplicate: !inserted, delivered_to: deliveredTo };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** 推 group_message 给发送这一刻的每个群成员(含发信人自己,多端同步),每人带上自己的未读数。返回推送时在线的人数。 */
|
|
149
|
+
function pushGroupMessage(networkId: string, group: { group_id: string; name: string }, row: MsgRow): number {
|
|
150
|
+
const unread = unreadByMember(group.group_id);
|
|
151
|
+
let online = 0;
|
|
152
|
+
for (const [userId, count] of unread) {
|
|
153
|
+
if (hasUserSubscribers(networkId, userId)) online++;
|
|
154
|
+
pushUserEvent(networkId, userId, {
|
|
155
|
+
type: GROUP_MESSAGE_EVENT,
|
|
156
|
+
message_id: row.message_id,
|
|
157
|
+
seq: Number(row.seq),
|
|
158
|
+
group_id: group.group_id,
|
|
159
|
+
group_name: group.name,
|
|
160
|
+
kind: GROUP_MESSAGE_KIND,
|
|
161
|
+
from: row.from_session,
|
|
162
|
+
from_user_id: row.sender_user_id,
|
|
163
|
+
message: redactMessageRow({ content: row.content }).content,
|
|
164
|
+
severity: "info",
|
|
165
|
+
created_at: row.created_at,
|
|
166
|
+
unread: count,
|
|
167
|
+
...(row.meta_json ? { meta: JSON.parse(redactMessageRow({ meta_json: row.meta_json }).meta_json) } : {}),
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
return online;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// 未读口径(一处定义,三个地方用):别人发的、在我入群之后、seq 超过我的已读位置。
|
|
174
|
+
// joined_at 是秒级、created_at 是毫秒级的同格式 UTC 文本(SQLite 与 PG 一致),逐字比较即时间先后;
|
|
175
|
+
// 入群那一秒里发的消息算未读(宁可多一条,不漏)。重新入群 joined_at 刷新,离开期间的消息不算未读。
|
|
176
|
+
const UNREAD_SUBQUERY = `(SELECT COUNT(*) FROM chat_group_messages g
|
|
177
|
+
WHERE g.group_id = m.group_id AND g.sender_user_id <> m.user_id
|
|
178
|
+
AND g.created_at > m.joined_at AND g.seq > COALESCE(r.last_read_seq, 0))`;
|
|
179
|
+
|
|
180
|
+
function unreadByMember(groupId: string): Map<string, number> {
|
|
181
|
+
const rows = db.all<{ user_id: string; unread: number | string }>(
|
|
182
|
+
`SELECT m.user_id, ${UNREAD_SUBQUERY} AS unread
|
|
183
|
+
FROM chat_group_members m LEFT JOIN chat_group_reads r ON r.group_id = m.group_id AND r.user_id = m.user_id
|
|
184
|
+
WHERE m.group_id = ?1`,
|
|
185
|
+
groupId,
|
|
186
|
+
);
|
|
187
|
+
return new Map(rows.map((r) => [r.user_id, Number(r.unread ?? 0)]));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function groupUnreadFor(groupId: string, userId: string): number {
|
|
191
|
+
const r = db.get<{ unread: number | string }>(
|
|
192
|
+
`SELECT ${UNREAD_SUBQUERY} AS unread
|
|
193
|
+
FROM chat_group_members m LEFT JOIN chat_group_reads r ON r.group_id = m.group_id AND r.user_id = m.user_id
|
|
194
|
+
WHERE m.group_id = ?1 AND m.user_id = ?2`,
|
|
195
|
+
groupId, userId,
|
|
196
|
+
);
|
|
197
|
+
return Number(r?.unread ?? 0);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* 会话列表里的最后一条预览(App 补口,只增)。text = 正文(去敏后)空白折成一个空格、取前 80 个字符(按码点,不切坏 emoji);
|
|
202
|
+
* 只有附件 → text 为 "",attachment_count 是附件数。sender_name = 发信人当前的 display_name(非空)否则 username
|
|
203
|
+
* (账号没了回落到发送时的 from_session)。at = 这条消息的 created_at(与 last_at 同值)。
|
|
204
|
+
*/
|
|
205
|
+
export type GroupLastMessage = { text: string; attachment_count: number; sender_user_id: string; sender_name: string; at: string };
|
|
206
|
+
export const LAST_MESSAGE_PREVIEW_CHARS = 80;
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 我在这个网络里每个群的最后一条消息,一次查询(按群取 MAX(seq) 走 (group_id, seq) 索引,不逐群发查询)。
|
|
210
|
+
* 只覆盖我**当前**在里面的群 —— 不是成员的群(管理身份看得到群资料)不给预览,和 last_message_at 同一口径。
|
|
211
|
+
*/
|
|
212
|
+
export function lastGroupMessagesFor(networkId: string, userId: string): Map<string, GroupLastMessage> {
|
|
213
|
+
const rows = db.all<{ group_id: string; sender_user_id: string; from_session: string; content: string; meta_json: string | null; created_at: string; username: string | null; display_name: string | null }>(
|
|
214
|
+
`SELECT g.group_id, g.sender_user_id, g.from_session, g.content, g.meta_json, g.created_at, u.username, u.display_name
|
|
215
|
+
FROM chat_group_members m
|
|
216
|
+
JOIN chat_group_messages g ON g.group_id = m.group_id
|
|
217
|
+
AND g.seq = (SELECT MAX(g2.seq) FROM chat_group_messages g2 WHERE g2.group_id = m.group_id)
|
|
218
|
+
LEFT JOIN users u ON u.user_id = g.sender_user_id
|
|
219
|
+
WHERE m.network_id = ?1 AND m.user_id = ?2`,
|
|
220
|
+
networkId, userId,
|
|
221
|
+
);
|
|
222
|
+
const out = new Map<string, GroupLastMessage>();
|
|
223
|
+
for (const r of rows) {
|
|
224
|
+
const content = String(redactMessageRow({ content: r.content }).content ?? "");
|
|
225
|
+
let attachmentCount = 0;
|
|
226
|
+
try {
|
|
227
|
+
const list = JSON.parse(r.meta_json ?? "null")?.attachments;
|
|
228
|
+
if (Array.isArray(list)) attachmentCount = list.length;
|
|
229
|
+
} catch {}
|
|
230
|
+
const name = publicDisplayName(r.username, r.display_name).trim() || r.username || r.from_session;
|
|
231
|
+
out.set(r.group_id, {
|
|
232
|
+
text: [...content.replace(/\s+/g, " ").trim()].slice(0, LAST_MESSAGE_PREVIEW_CHARS).join(""),
|
|
233
|
+
attachment_count: attachmentCount,
|
|
234
|
+
sender_user_id: r.sender_user_id,
|
|
235
|
+
sender_name: name,
|
|
236
|
+
at: r.created_at,
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
return out;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export type GroupThread = { group_id: string; name: string; department_id: string | null; last_at: string | null; unread: number; last_read_seq: number; last_message: GroupLastMessage | null };
|
|
243
|
+
|
|
244
|
+
/** 我在这个网络里的群会话:每个群一行,最后一条时间 + 我的未读数。按最后一条时间倒序(没消息的排最后),和私信会话列表同序。 */
|
|
245
|
+
export function listGroupThreads(networkId: string, userId: string): GroupThread[] {
|
|
246
|
+
const rows = db.all<{ group_id: string; name: string; department_id: string | null; last_at: string | null; unread: number | string; last_read_seq: number | string | null }>(
|
|
247
|
+
`SELECT m.group_id, c.name, c.department_id, ${UNREAD_SUBQUERY} AS unread,
|
|
248
|
+
(SELECT MAX(g2.created_at) FROM chat_group_messages g2 WHERE g2.group_id = m.group_id) AS last_at,
|
|
249
|
+
r.last_read_seq
|
|
250
|
+
FROM chat_group_members m
|
|
251
|
+
JOIN chat_groups c ON c.group_id = m.group_id
|
|
252
|
+
LEFT JOIN chat_group_reads r ON r.group_id = m.group_id AND r.user_id = m.user_id
|
|
253
|
+
WHERE m.network_id = ?1 AND m.user_id = ?2`,
|
|
254
|
+
networkId, userId,
|
|
255
|
+
);
|
|
256
|
+
const last = lastGroupMessagesFor(networkId, userId);
|
|
257
|
+
// 排序放在 JS 里:SQLite 和 PG 对 DESC 里 NULL 的位置相反。
|
|
258
|
+
return rows
|
|
259
|
+
.map((r) => ({ group_id: r.group_id, name: r.name, department_id: r.department_id, last_at: r.last_at, unread: Number(r.unread ?? 0), last_read_seq: Number(r.last_read_seq ?? 0), last_message: last.get(r.group_id) ?? null }))
|
|
260
|
+
.sort((a, b) => (b.last_at ?? "").localeCompare(a.last_at ?? "") || a.group_id.localeCompare(b.group_id));
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** 群消息记录,新的在前。before = 上一页最后(最老)一条的 seq;返回 next_before(没有更多 → null)。 */
|
|
264
|
+
export function listGroupMessages(groupId: string, meUserId: string, limit = 50, before?: string | null): { messages: ReturnType<typeof shape>[]; next_before: number | null } | Fail {
|
|
265
|
+
const n = Math.max(1, Math.min(Number.isFinite(limit) ? Math.trunc(limit) : 50, 200));
|
|
266
|
+
let rows: MsgRow[];
|
|
267
|
+
if (before !== undefined && before !== null && before !== "") {
|
|
268
|
+
if (!/^\d{1,15}$/.test(before)) return fail(400, "invalid_before");
|
|
269
|
+
rows = db.all<MsgRow>(`SELECT ${MSG_COLS} FROM chat_group_messages WHERE group_id = ?1 AND seq < ?2 ORDER BY seq DESC LIMIT ?3`, groupId, Number(before), n);
|
|
270
|
+
} else {
|
|
271
|
+
rows = db.all<MsgRow>(`SELECT ${MSG_COLS} FROM chat_group_messages WHERE group_id = ?1 ORDER BY seq DESC LIMIT ?2`, groupId, n);
|
|
272
|
+
}
|
|
273
|
+
const messages = rows.map((r) => shape(r, meUserId));
|
|
274
|
+
return { messages, next_before: rows.length === n ? Number(rows[rows.length - 1].seq) : null };
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* 标已读。seq 缺省 = 群里最新一条;给了就读到那一条(超过最新按最新算)。已读位置只前进不后退
|
|
279
|
+
* (两台设备先后上报,旧的那次不会把未读又顶回来)。读完推 group_read 给我自己的其他设备,让角标一起清。
|
|
280
|
+
*/
|
|
281
|
+
export function markGroupRead(networkId: string, groupId: string, userId: string, seq: unknown): { ok: true; last_read_seq: number; unread: number } | Fail {
|
|
282
|
+
if (seq !== undefined && seq !== null && !(typeof seq === "number" && Number.isSafeInteger(seq) && seq >= 0)) return fail(400, "invalid_seq");
|
|
283
|
+
const latest = Number(db.get<{ s: number | string | null }>("SELECT MAX(seq) AS s FROM chat_group_messages WHERE group_id = ?1", groupId)?.s ?? 0);
|
|
284
|
+
const target = typeof seq === "number" ? Math.min(seq, latest) : latest;
|
|
285
|
+
db.run(
|
|
286
|
+
`INSERT INTO chat_group_reads (group_id, user_id, network_id, last_read_seq, updated_at)
|
|
287
|
+
VALUES (?1, ?2, ?3, ?4, datetime('now'))
|
|
288
|
+
ON CONFLICT(group_id, user_id) DO UPDATE SET
|
|
289
|
+
last_read_seq = CASE WHEN excluded.last_read_seq > chat_group_reads.last_read_seq THEN excluded.last_read_seq ELSE chat_group_reads.last_read_seq END,
|
|
290
|
+
updated_at = excluded.updated_at`,
|
|
291
|
+
[groupId, userId, networkId, target],
|
|
292
|
+
);
|
|
293
|
+
const lastRead = Number(db.get<{ s: number | string }>("SELECT last_read_seq AS s FROM chat_group_reads WHERE group_id = ?1 AND user_id = ?2", groupId, userId)?.s ?? 0);
|
|
294
|
+
const unread = groupUnreadFor(groupId, userId);
|
|
295
|
+
pushUserEvent(networkId, userId, { type: GROUP_READ_EVENT, group_id: groupId, last_read_seq: lastRead, unread });
|
|
296
|
+
return { ok: true, last_read_seq: lastRead, unread };
|
|
297
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 匿名 `/health` 字段的**读者登记表**。
|
|
3
|
+
*
|
|
4
|
+
* 2026-10-31 —— 外部复核 ⑤ 的第 ②′ 问:
|
|
5
|
+
* 「这条信息对匿名读者是**路标**还是**仪表**?路标(往哪打)⇒ 一律不给;仪表(忙不忙)⇒ 可以给聚合。」
|
|
6
|
+
*
|
|
7
|
+
* 本仓已有三分类(landmark / gauge / credential)回答「该不该给」,
|
|
8
|
+
* 但没有回答「**谁在读**」。于是白名单可以无限增长,每一行都写着「watchdog 需要」而没人验证过。
|
|
9
|
+
*
|
|
10
|
+
* ⇒ 每个匿名键必须登记一个读者,且读者是可机械核对的一类:
|
|
11
|
+
*
|
|
12
|
+
* cli 本仓客户端真的读它(**测试会在 agent-network/src 里逐字找这个名字**)
|
|
13
|
+
* probe 探针/自检脚本读它(同上,在 scripts/ 里找)
|
|
14
|
+
* external 仓外读者(监控、负载均衡健康检查、运维手册)—— 仓内无法验证,
|
|
15
|
+
* 所以必须写明「是哪一类外部系统」,并接受「未验」这个事实
|
|
16
|
+
*
|
|
17
|
+
* 判据:**一个白名单条目,如果连读者都指不出来,它就不是「仪表」,而是没人看的暴露面。**
|
|
18
|
+
*/
|
|
19
|
+
// 2026-10-31 —— 第四档是今晚查出来的:
|
|
20
|
+
// `mcp_inflight` / `mcp_inflight_peak` / `event_loop` 在仓内**没有任何代码读**,
|
|
21
|
+
// 只出现在注释(引用生产实测数值)与历史报告里 —— 读它们的是**人**(运维手工 curl)。
|
|
22
|
+
// 「程序读」与「人读」是两件事:前者能跨源核对,后者只能登记谁在什么场合看。
|
|
23
|
+
// 把它们混进 external(外部程序)会让「有读者」这句话虚高。
|
|
24
|
+
export type AnonReader = "cli" | "probe" | "external" | "operator";
|
|
25
|
+
|
|
26
|
+
export interface AnonReaderEntry {
|
|
27
|
+
reader: AnonReader;
|
|
28
|
+
/** 为什么要给匿名读者;external 的必须写清是哪一类系统 */
|
|
29
|
+
why: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const PUBLIC_HEALTH_KEY_READERS: Readonly<Record<string, AnonReaderEntry>> = {
|
|
33
|
+
ok: { reader: "cli", why: "CLI/客户端判断「Hub 是否在」的第一位" },
|
|
34
|
+
status: { reader: "cli", why: "客户端展示 Hub 的细分状态(ok / degraded)" },
|
|
35
|
+
status_reasons: { reader: "cli", why: "anet status 把 status_reasons 拼成一行原因给用户看(bin/cli.ts)" },
|
|
36
|
+
version: { reader: "cli", why: "版本不一致提示(anet 与 hub 的版本对齐)" },
|
|
37
|
+
version_source: { reader: "operator", why: "运维判断「版本没变」是没发出去、被 env 覆盖,还是读不到/读到空;仓内无代码读" },
|
|
38
|
+
version_role: { reader: "external", why: "docs/release-hub-runbook.md 描述的仓外 dashboard 读它区分构建;仓内无消费者,未验" },
|
|
39
|
+
api_version: { reader: "cli", why: "客户端与 Hub 协商协议版本,不匹配时给出升级提示" },
|
|
40
|
+
transport: { reader: "cli", why: "协议协商(streamable-http)" },
|
|
41
|
+
uptime: { reader: "cli", why: "`anet status` 展示" },
|
|
42
|
+
startedAt: { reader: "cli", why: "展示启动时刻(粗粒度,不是 landmark 的 git_commit)" },
|
|
43
|
+
sessions_count: { reader: "cli", why: "状态面板的连接数" },
|
|
44
|
+
sse_connections: { reader: "cli", why: "状态面板展示当前 SSE 长连接数量" },
|
|
45
|
+
mcp_inflight: { reader: "operator", why: "运维手工 curl 看实时在途数;仓内无代码读,仅注释引用过生产数值" },
|
|
46
|
+
mcp_inflight_peak: { reader: "operator", why: "运维手工看**峰值**,用于判断重连风暴是否发生过;无程序读者" },
|
|
47
|
+
event_loop: { reader: "operator", why: "停顿指标只出现在历史发布报告里,仓内无代码读;靠人看" },
|
|
48
|
+
// #3271 —— 我原来把「观察这条闸有没有 fire」写成了 /health.counters.…,
|
|
49
|
+
// 而 counters 不在匿名投影里 ⇒ 没有 token 的读者(包括当时的我)看不到它。
|
|
50
|
+
// ⇒ 聚合走匿名投影;by_node 仍是拓扑,不给。
|
|
51
|
+
task_expiries_never_consumed_total: { reader: "operator", why: "运维/agent 判断「派单硬闸有没有真的 fire 过」;只有总数,不含节点名" },
|
|
52
|
+
counters_since: { reader: "operator", why: "上一条的窗口起点:计数器随重启归零,只有总数时「刚重启」与「一次都没发生」同值" },
|
|
53
|
+
// #3193 子卡 —— 与上一条同形:拒绝次数的总数给匿名,by_reason 明细留在鉴权投影。
|
|
54
|
+
task_rejections_total: { reader: "operator", why: "运维/agent 判断「今天拒了多少」而不必翻日志(日志轮转后 grep 会静默 0)" },
|
|
55
|
+
auth: { reader: "cli", why: "客户端据此决定登录方式" },
|
|
56
|
+
security: { reader: "cli", why: "客户端据此提示是否需要 TLS/密钥配置" },
|
|
57
|
+
tmux: { reader: "cli", why: "共存 TUI 的前置检查" },
|
|
58
|
+
v3_auth: { reader: "external", why: "老客户端兼容位;仓内无消费者,未验" },
|
|
59
|
+
multi_network: { reader: "cli", why: "多网络客户端据此决定是否显示网络切换" },
|
|
60
|
+
license: { reader: "cli", why: "兼容字段,展示用(判据请用 license_state)" },
|
|
61
|
+
license_state: { reader: "external", why: "监控/合规只读;仓内无消费者,未验" },
|
|
62
|
+
limits: { reader: "cli", why: "上传代理在开流前读它决定单次上传上限" },
|
|
63
|
+
capabilities: { reader: "cli", why: "客户端据此隐藏未实现的功能入口" },
|
|
64
|
+
probe_nonce: { reader: "probe", why: "探针回显:只在带 X-Probe-Nonce 时出现,用于验证同一次请求" },
|
|
65
|
+
served_at: { reader: "probe", why: "响应时刻:探针用它判断两次读数是不是同一次处理的产物" },
|
|
66
|
+
build_identified: { reader: "operator", why: "按 release-hub-runbook.md 由人核对构建是否可识别;无程序读者" },
|
|
67
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// #2756 —— `/health` 必须能看见 Hub 自己所在磁盘的水位。
|
|
2
|
+
//
|
|
3
|
+
// 起因:2026-10-28 04:05 我「偶尔巡检」才发现根分区 95%(剩 28G)。
|
|
4
|
+
// 上一轮清理(85%)之后它只用了一天多就涨回 10 个点 —— 靠人记得看,必然会漏;
|
|
5
|
+
// 而 Hub 的 DB / 日志 / 运行时目录都写在这块盘上,写满即生产停摆。
|
|
6
|
+
//
|
|
7
|
+
// 这条测试钉三件事:
|
|
8
|
+
// 1. 形状稳定(path / total_gb / used_gb / avail_gb / used_pct 都有);
|
|
9
|
+
// 2. 数值自洽(used + avail ≈ total,used_pct 与两者一致);
|
|
10
|
+
// 3. 探针打在 DB 目录上,不是写死 `/`。
|
|
11
|
+
//
|
|
12
|
+
// Run: COMMHUB_DB=/tmp/2756-health-disk.db bun test src/health-host-disk-2756.test.ts
|
|
13
|
+
|
|
14
|
+
import { describe, expect, test } from "bun:test";
|
|
15
|
+
import { hostDiskState, redactHomeInPath } from "./server.js";
|
|
16
|
+
|
|
17
|
+
describe("#2756 /health.host_disk", () => {
|
|
18
|
+
test("形状完整且数值自洽", () => {
|
|
19
|
+
const d = hostDiskState();
|
|
20
|
+
expect(d).not.toBeNull();
|
|
21
|
+
const v = d!;
|
|
22
|
+
expect(typeof v.path).toBe("string");
|
|
23
|
+
expect(v.path.startsWith("/")).toBe(true);
|
|
24
|
+
expect(v.total_gb).toBeGreaterThan(0);
|
|
25
|
+
expect(v.avail_gb).toBeGreaterThanOrEqual(0);
|
|
26
|
+
expect(v.used_gb).toBeGreaterThanOrEqual(0);
|
|
27
|
+
expect(v.used_gb + v.avail_gb).toBeCloseTo(v.total_gb, 0);
|
|
28
|
+
expect(v.used_pct).toBeGreaterThanOrEqual(0);
|
|
29
|
+
expect(v.used_pct).toBeLessThanOrEqual(100);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("探针目录来自 COMMHUB_DB 所在目录(不是写死 /)", () => {
|
|
33
|
+
const d = hostDiskState();
|
|
34
|
+
const db = process.env.COMMHUB_DB || "";
|
|
35
|
+
if (db.includes("/")) {
|
|
36
|
+
const dir = db.replace(/[^/]*$/, "");
|
|
37
|
+
expect(d!.path).toBe(dir);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("水位与 df 同量级(10% 以内偏差,防止把字节单位算错)", () => {
|
|
42
|
+
const d = hostDiskState()!;
|
|
43
|
+
// 用 statfs 自身再算一遍独立值作对照
|
|
44
|
+
const fs = require("node:fs");
|
|
45
|
+
const st = fs.statfsSync(d.path);
|
|
46
|
+
const pct = Math.round((1 - Number(st.bavail) / Number(st.blocks)) * 1000) / 10;
|
|
47
|
+
expect(Math.abs(d.used_pct - pct)).toBeLessThan(10);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe("#2756 /health 暴露面:家目录不进免鉴权响应", () => {
|
|
52
|
+
test("redactHomeInPath 把家目录换成 ~,其余路径不动", () => {
|
|
53
|
+
const home = process.env.HOME || "";
|
|
54
|
+
if (!home || home === "/") return;
|
|
55
|
+
const red = redactHomeInPath({ path: home + "/.commhub/" });
|
|
56
|
+
expect(red!.path).toBe("~/.commhub/");
|
|
57
|
+
expect(red!.path.includes(home)).toBe(false);
|
|
58
|
+
// 非家目录路径保持原样
|
|
59
|
+
expect(redactHomeInPath({ path: "/var/lib/commhub/" })!.path).toBe("/var/lib/commhub/");
|
|
60
|
+
// null 透传
|
|
61
|
+
expect(redactHomeInPath(null)).toBeNull();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("真实 /health 响应里的 host_disk.path 不含家目录", () => {
|
|
65
|
+
const d = redactHomeInPath(hostDiskState());
|
|
66
|
+
const home = process.env.HOME || "";
|
|
67
|
+
if (d && home && home !== "/") {
|
|
68
|
+
expect(d.path.startsWith(home)).toBe(false);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
});
|