@sleep2agi/commhub-server 0.9.0-preview.20 → 0.9.0-preview.200
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +130 -10
- package/bin/commhub.ts +99 -11
- package/package.json +7 -7
- package/src/ack-create-request-transport.test.ts +182 -0
- package/src/ack-create-request.test.ts +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-sec1.test.ts +107 -0
- package/src/config-apply-validate.test.ts +134 -1
- package/src/config-apply-validate.ts +111 -5
- package/src/create-node-blocked-gate.test.ts +356 -0
- package/src/create-node-network-id.test.ts +79 -0
- package/src/create-node-tool-schema.test.ts +49 -0
- package/src/create-node-unicode-name.test.ts +120 -0
- package/src/create-node-validate.test.ts +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 +177 -0
- package/src/env-transport-gate.ts +74 -0
- package/src/env-transport-policy.ts +21 -0
- package/src/event-loop-lag.test.ts +139 -0
- package/src/event-loop-lag.ts +150 -0
- package/src/ext-token-shared.test.ts +60 -0
- package/src/ext-validation-regression.test.ts +131 -0
- package/src/external-schedule-edits-http.test.ts +321 -0
- package/src/external-schedule-edits-timestamps.test.ts +64 -0
- package/src/external-schedule-edits.ts +327 -0
- package/src/file-download-authz-dev-open.test.ts +135 -0
- package/src/file-download-authz.test.ts +404 -0
- package/src/file-download-date-bucket.test.ts +436 -0
- package/src/file-network-scope-dev-open.test.ts +97 -0
- package/src/file-network-scope.test.ts +732 -0
- package/src/group-membership-events-http.test.ts +264 -0
- package/src/group-messages-http.test.ts +482 -0
- package/src/group-messages.ts +297 -0
- package/src/health-host-disk-2756.test.ts +49 -0
- package/src/health-redaction.test.ts +685 -0
- package/src/host-supervisor-identity-http.test.ts +152 -0
- package/src/host-supervisor-identity.ts +92 -0
- package/src/host-supervisors-daemon-version.test.ts +137 -0
- package/src/http-gzip.test.ts +94 -0
- package/src/http-gzip.ts +112 -0
- package/src/hub-catalog-read-http.test.ts +187 -0
- package/src/hub-provider-config.test.ts +139 -0
- package/src/hub-provider-config.ts +105 -0
- package/src/hub-timestamp.test.ts +38 -0
- package/src/hub-timestamp.ts +27 -0
- package/src/human-dm.ts +215 -0
- package/src/human-inbox.ts +42 -0
- package/src/inbox-caller.ts +49 -0
- package/src/inbox-count-sse.test.ts +310 -0
- package/src/inbox-count.ts +29 -0
- package/src/index.ts +15 -2380
- 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 +79 -0
- package/src/node-permissions-http.test.ts +345 -0
- package/src/node-permissions.ts +393 -0
- package/src/node-request-retention.test.ts +281 -0
- package/src/node-request-retention.ts +120 -0
- package/src/node-secret-rest-http.test.ts +190 -0
- package/src/node-serviceability.test.ts +86 -0
- package/src/node-serviceability.ts +124 -0
- package/src/node-session-control-http.test.ts +310 -0
- package/src/node-session-control.ts +258 -0
- package/src/node-skills-transport.test.ts +209 -0
- package/src/node-token-identity.ts +35 -0
- package/src/node-token-ownership.ts +51 -0
- package/src/node-token-privilege-http.test.ts +158 -0
- package/src/not-found-api-http.test.ts +67 -0
- package/src/observer-avatar-http.test.ts +442 -0
- package/src/observer-push.test.ts +252 -0
- package/src/opencode-catalog-report-http.test.ts +139 -0
- package/src/opencode-v2-catalog.test.ts +268 -0
- package/src/opencode-v2-catalog.ts +352 -0
- package/src/package-version-consistency.test.ts +25 -0
- package/src/parent-source-marker.test.ts +50 -0
- package/src/password-network-tokens.ts +25 -0
- package/src/peer-reply-atomic.test.ts +455 -0
- package/src/peer-reply-capability.test.ts +70 -0
- package/src/pg-worker.ts +87 -0
- package/src/project-dir-warn-clear-http.test.ts +132 -0
- package/src/project-viewer-can-http.test.ts +116 -0
- package/src/push.test.ts +20 -10
- package/src/push.ts +466 -13
- package/src/redact-tokens.test.ts +65 -0
- package/src/redact-tokens.ts +40 -0
- package/src/rename.ts +38 -10
- package/src/reply-target-derivation.test.ts +96 -0
- package/src/report-status-handover-capabilities-http.test.ts +124 -0
- package/src/report-status-host-schema.test.ts +235 -0
- package/src/report-status-resume-id-handover.test.ts +184 -0
- package/src/report-status-secret-keys.test.ts +140 -0
- package/src/require-explicit-test-db.ts +9 -0
- package/src/requirement-due-reminders-http.test.ts +620 -0
- package/src/requirement-due-reminders.ts +416 -0
- package/src/requirement-events-http.test.ts +268 -0
- package/src/requirement-events.test.ts +45 -0
- package/src/requirement-events.ts +144 -0
- package/src/requirement-notify.ts +165 -0
- package/src/requirement-participant-notify-http.test.ts +309 -0
- package/src/requirement-tag-ops-http.test.ts +425 -0
- package/src/requirement-tag-tree-http.test.ts +275 -0
- package/src/requirement-tag-tree.ts +127 -0
- package/src/requirement-tags.ts +102 -0
- package/src/requirements-abandoned-http.test.ts +236 -0
- package/src/requirements-comment-http.test.ts +167 -0
- package/src/requirements-completed-at-migration.test.ts +70 -0
- package/src/requirements-errors-mcp-http.test.ts +164 -0
- package/src/requirements-errors.ts +158 -0
- package/src/requirements-etag-http.test.ts +91 -0
- package/src/requirements-http.test.ts +709 -0
- package/src/requirements-last-event-http.test.ts +349 -0
- package/src/requirements-list-mcp-slim-http.test.ts +155 -0
- package/src/requirements-list-slim-http.test.ts +324 -0
- package/src/requirements-mcp.test.ts +163 -0
- package/src/requirements-migrate.ts +283 -0
- package/src/requirements-owner-strict-mcp-http.test.ts +145 -0
- package/src/requirements-people-http.test.ts +114 -0
- package/src/requirements-people-mcp-http.test.ts +231 -0
- package/src/requirements-people.ts +196 -0
- package/src/requirements-priority-lowest.test.ts +114 -0
- package/src/requirements-projects-parent-http.test.ts +123 -0
- package/src/requirements-search-http.test.ts +182 -0
- package/src/requirements-search.test.ts +32 -0
- package/src/requirements-search.ts +145 -0
- package/src/requirements-seq-http.test.ts +193 -0
- package/src/requirements-seq-migration.test.ts +63 -0
- package/src/requirements-start-date.test.ts +125 -0
- package/src/requirements-stats-http.test.ts +357 -0
- package/src/requirements-stats.ts +195 -0
- package/src/requirements-status-participants-http.test.ts +222 -0
- package/src/requirements-upgrade-e2e.test.ts +126 -0
- package/src/requirements-upgrade-migration.test.ts +98 -0
- package/src/requirements.ts +2027 -0
- package/src/resolve-port.test.ts +57 -0
- package/src/resolve-port.ts +44 -0
- package/src/response-charset.test.ts +130 -0
- package/src/rest-alias-network-fallback-http.test.ts +123 -0
- package/src/rest-alias-network-fallback.test.ts +60 -0
- package/src/rest-alias-network-fallback.ts +65 -0
- package/src/rest-explicit-columns-http.test.ts +266 -0
- package/src/rest-identity.test.ts +130 -0
- package/src/rest-identity.ts +141 -0
- package/src/rest-projections.ts +72 -0
- package/src/rest-read-network-real-chain.test.ts +97 -0
- package/src/rest-write-network-resolution.test.ts +133 -0
- package/src/restart-readiness-http.test.ts +121 -0
- package/src/restricted-files.ts +76 -0
- package/src/retention-first-sweep-quiet.test.ts +31 -0
- package/src/retention.test.ts +100 -40
- package/src/retention.ts +423 -104
- package/src/route-callers-http.test.ts +120 -0
- package/src/route-timing.test.ts +211 -0
- package/src/route-timing.ts +262 -0
- package/src/rules-file-duplicate-alias.test.ts +234 -0
- package/src/rules-file-session-target.test.ts +207 -0
- package/src/rules-file-transport.test.ts +237 -0
- package/src/runtime-label.test.ts +22 -0
- package/src/runtime-label.ts +21 -0
- package/src/runtime-readiness.ts +90 -0
- package/src/schedule-agent-mcp-http.test.ts +391 -0
- package/src/schedule-agent.ts +192 -0
- package/src/schedule-starved.test.ts +92 -0
- package/src/schedule-starved.ts +114 -0
- package/src/scheduled-backoff-http.test.ts +193 -0
- package/src/scheduled-backoff.ts +64 -0
- package/src/scheduled-doorbell-nodeless-session.test.ts +134 -0
- package/src/scheduled-failure-backoff-2622.test.ts +145 -0
- package/src/scheduled-failure-code.test.ts +73 -0
- package/src/scheduled-failure-code.ts +105 -0
- package/src/scheduled-failures-http.test.ts +279 -0
- package/src/scheduled-failures.ts +377 -0
- package/src/scheduled-noprogress-backoff-2622.test.ts +133 -0
- package/src/scheduled-offline-backoff-probe.test.ts +56 -0
- package/src/scheduled-offline.test.ts +131 -0
- package/src/scheduled-offline.ts +66 -0
- package/src/scheduled-outcome-guidance-http.test.ts +139 -0
- package/src/scheduled-outcome-guidance.ts +9 -0
- package/src/scheduled-reply-unread-http.test.ts +212 -0
- package/src/scheduled-requirement-wake-http.test.ts +279 -0
- package/src/scheduled-requirement-wake.ts +125 -0
- package/src/scheduled-run-terminal.test.ts +268 -0
- package/src/scheduled-skip-blocking-task.test.ts +120 -0
- package/src/scheduled-skip-path-backoff-2622.test.ts +98 -0
- package/src/scheduled-stuck-http.test.ts +312 -0
- package/src/scheduled-stuck-notice-cap.test.ts +80 -0
- package/src/scheduled-stuck.ts +219 -0
- package/src/scheduled-tasks-http.test.ts +679 -0
- package/src/scheduled-tasks.ts +959 -0
- package/src/scheduled-work-outcome-http.test.ts +154 -0
- package/src/scheduled-work-outcome.ts +50 -0
- package/src/scoped-secrets.ts +139 -0
- package/src/send-dedup-request-id.test.ts +229 -0
- package/src/send-reply-agent-warning.test.ts +202 -0
- package/src/send-reply-alias-optional.test.ts +131 -0
- package/src/send-reply-attachments.test.ts +458 -0
- package/src/send_dedup.test.ts +54 -0
- package/src/send_dedup.ts +52 -6
- package/src/serve-error.test.ts +38 -0
- package/src/serve-error.ts +27 -0
- package/src/server.ts +5997 -0
- package/src/session-task-on-dispatch.ts +14 -0
- package/src/session-token-ownership-http.test.ts +122 -0
- package/src/shared/codex-inventory.ts +41 -0
- package/src/shared/daemon-provider-login.ts +31 -0
- package/src/shared/daemon-provider-snapshot.ts +52 -0
- package/src/shared/external-schedule-contract.ts +60 -0
- package/src/shared/node-name.test.ts +62 -0
- package/src/shared/node-name.ts +161 -0
- package/src/shared/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 +4212 -404
- 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,119 @@
|
|
|
1
|
+
// #758 —— 「孤儿任务」:任务还停在 acked / running,接收节点却已经回到 idle(或 offline)、
|
|
2
|
+
// 并且在任务开工之后还报过状态 —— 节点转去干别的了,没回这一条。
|
|
3
|
+
//
|
|
4
|
+
// 生产只读实测:17 条任务 acked / running 超过 1 小时,接收节点早已 idle;最长一条(admin 的 P0)挂了 15.7 小时。
|
|
5
|
+
// 以前巡检只对 delivered 记警告(task-lifecycle-watcher.ts),acked / running 这一段谁都看不见。
|
|
6
|
+
//
|
|
7
|
+
// 规则(每次 5 分钟任务巡检跑一遍,server.ts runTaskPatrol):
|
|
8
|
+
// status ∈ {acked, running}
|
|
9
|
+
// ∧ 开工时刻 = COALESCE(started_at, delivered_at, created_at) 早于 COMMHUB_ORPHAN_TASK_MINUTES(默认 60;0 = 关)
|
|
10
|
+
// ∧ 开工时刻在最近 COMMHUB_ORPHAN_TASK_LOOKBACK_HOURS 内(默认 72;0 = 不设上限)—— 首次部署不爆发历史通知
|
|
11
|
+
// ∧ 接收节点(sessions 同网络同别名)当前 status ∈ {idle, offline}
|
|
12
|
+
// ∧ 节点最后一次报状态 COALESCE(last_seen_at, updated_at) 晚于开工时刻
|
|
13
|
+
// 命中后对每条任务**只做一次**:
|
|
14
|
+
// - task_events 写一行 event_type = event_key = task.orphan_suspected(唯一索引 (task_id, event_key) 兜底,
|
|
15
|
+
// ON CONFLICT DO NOTHING;只有真插进去的那一次才往下走)。
|
|
16
|
+
// - 通知派活的一方,与过期通知同一条路(task-expiry-notice.ts 的 classifyExpirySender / deliver):
|
|
17
|
+
// agent → inbox reply + new_reply;人 → user_inbox;scheduler / hub / api → 不发。
|
|
18
|
+
// - **不改任务状态**:不 fail、不关、不碰过期语义。
|
|
19
|
+
|
|
20
|
+
import { db } from "./db.js";
|
|
21
|
+
import { parseDbTimestampMs } from "./db-timestamp.js";
|
|
22
|
+
import { classifyExpirySender, deliver, type ExpiredTaskRow } from "./task-expiry-notice.js";
|
|
23
|
+
|
|
24
|
+
export const ORPHAN_TASK_ENV = "COMMHUB_ORPHAN_TASK_MINUTES";
|
|
25
|
+
export const ORPHAN_DEFAULT_MINUTES = 60;
|
|
26
|
+
export const ORPHAN_LOOKBACK_ENV = "COMMHUB_ORPHAN_TASK_LOOKBACK_HOURS";
|
|
27
|
+
export const ORPHAN_DEFAULT_LOOKBACK_HOURS = 72;
|
|
28
|
+
export const ORPHAN_EVENT_TYPE = "task.orphan_suspected";
|
|
29
|
+
export const ORPHAN_NOTICE_KIND = "task_orphan_suspected";
|
|
30
|
+
const BATCH_LIMIT = 200;
|
|
31
|
+
|
|
32
|
+
/** 未设 / 非数字 → 默认 60;0 或负数 → 关闭(null)。 */
|
|
33
|
+
export function orphanTaskMinutes(env: Record<string, string | undefined> = process.env): number | null {
|
|
34
|
+
const raw = (env[ORPHAN_TASK_ENV] ?? "").trim();
|
|
35
|
+
if (!raw) return ORPHAN_DEFAULT_MINUTES;
|
|
36
|
+
const n = Number(raw);
|
|
37
|
+
if (!Number.isFinite(n)) return ORPHAN_DEFAULT_MINUTES;
|
|
38
|
+
return n > 0 ? n : null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 只看开工时刻在最近 N 小时内的任务:未设 / 非数字 / 负数 → 默认 72;0 → 不设上限。
|
|
42
|
+
* 首次部署时不把几周前的历史 acked / running 一次性全标出来、给派活方(包括 owner)刷一屏通知。 */
|
|
43
|
+
export function orphanLookbackHours(env: Record<string, string | undefined> = process.env): number | null {
|
|
44
|
+
const raw = (env[ORPHAN_LOOKBACK_ENV] ?? "").trim();
|
|
45
|
+
const n = raw ? Number(raw) : NaN;
|
|
46
|
+
if (!Number.isFinite(n) || n < 0) return ORPHAN_DEFAULT_LOOKBACK_HOURS;
|
|
47
|
+
return n === 0 ? null : n;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
type OrphanRow = ExpiredTaskRow & { status: string; started: string; node_status: string; node_seen: string };
|
|
51
|
+
|
|
52
|
+
export type OrphanFlagged = { task_id: string; notified: "agent" | "user" | null };
|
|
53
|
+
|
|
54
|
+
export function orphanNoticeText(r: Pick<OrphanRow, "task_id" | "to_name" | "status" | "content" | "node_status">, openMin: number | null): { title: string; text: string } {
|
|
55
|
+
const preview = (r.content ?? "").replace(/\s+/g, " ").trim().slice(0, 60);
|
|
56
|
+
const m = openMin === null ? "?" : String(openMin);
|
|
57
|
+
return {
|
|
58
|
+
title: "任务可能没人收尾",
|
|
59
|
+
text: `[Hub] 任务可能没人收尾:你派给 ${r.to_name} 的任务 ${r.task_id} 停在 ${r.status} 已 ${m} 分钟,但 ${r.to_name} 之后已回到 ${r.node_status},一直没有回复。`
|
|
60
|
+
+ `Hub 没有改这条任务的状态。\n内容:${preview}\n`
|
|
61
|
+
+ `建议:问 ${r.to_name} 一句,或 cancel_task 后换个节点重派。\n`
|
|
62
|
+
+ `Task ${r.task_id} to ${r.to_name} has been ${r.status} for ${m} min while ${r.to_name} went ${r.node_status} without replying. Its status was not changed.`,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** 一次巡检的孤儿阶段。返回本次新标记的任务(测试 / 日志用)。调用方负责吞异常。 */
|
|
67
|
+
export function flagOrphanTasks(opts: { minutes?: number | null; lookbackHours?: number | null; nowMs?: number } = {}): OrphanFlagged[] {
|
|
68
|
+
const minutes = opts.minutes === undefined ? orphanTaskMinutes() : opts.minutes;
|
|
69
|
+
if (!minutes || minutes <= 0) return [];
|
|
70
|
+
const cutoff = `datetime('now', '-${Math.max(1, Math.round(minutes * 60))} seconds')`;
|
|
71
|
+
const started = "COALESCE(t.started_at, t.delivered_at, t.created_at)";
|
|
72
|
+
const lookback = opts.lookbackHours === undefined ? orphanLookbackHours() : opts.lookbackHours;
|
|
73
|
+
const lookbackSql = lookback && lookback > 0
|
|
74
|
+
? `AND ${started} >= datetime('now', '-${Math.max(1, Math.round(lookback * 3600))} seconds')`
|
|
75
|
+
: "";
|
|
76
|
+
const rows = db.all<OrphanRow>(
|
|
77
|
+
`SELECT t.task_id, t.network_id, t.from_name, t.from_node_id, t.to_name, t.content, t.created_at, t.expires_at,
|
|
78
|
+
t.parent_task_id, t.meta_json, t.status, ${started} AS started,
|
|
79
|
+
s.status AS node_status, COALESCE(s.last_seen_at, s.updated_at) AS node_seen
|
|
80
|
+
FROM tasks t
|
|
81
|
+
JOIN sessions s ON s.alias = t.to_name AND s.network_id = COALESCE(t.network_id, 'default')
|
|
82
|
+
WHERE t.status IN ('acked', 'running')
|
|
83
|
+
AND ${started} < ${cutoff}
|
|
84
|
+
${lookbackSql}
|
|
85
|
+
AND s.status IN ('idle', 'offline')
|
|
86
|
+
AND COALESCE(s.last_seen_at, s.updated_at) > ${started}
|
|
87
|
+
AND NOT EXISTS (SELECT 1 FROM task_events e WHERE e.task_id = t.task_id AND e.event_key = '${ORPHAN_EVENT_TYPE}')
|
|
88
|
+
ORDER BY t.created_at ASC LIMIT ${BATCH_LIMIT}`,
|
|
89
|
+
);
|
|
90
|
+
const nowMs = opts.nowMs ?? Date.now();
|
|
91
|
+
const flagged: OrphanFlagged[] = [];
|
|
92
|
+
for (const r of rows) {
|
|
93
|
+
const route = classifyExpirySender(r);
|
|
94
|
+
const who = route.kind === "none" ? `none (${route.reason})` : `${route.kind} ${route.kind === "agent" ? route.alias : route.username}`;
|
|
95
|
+
const detail = `${r.to_name} is ${r.node_status} (last status ${r.node_seen}) since ${r.status} at ${r.started}; notify: ${who}`;
|
|
96
|
+
const res = db.run(
|
|
97
|
+
`INSERT INTO task_events (task_id, from_status, to_status, event_type, event_key, actor, detail, network_id)
|
|
98
|
+
VALUES (?1, ?2, ?2, '${ORPHAN_EVENT_TYPE}', '${ORPHAN_EVENT_TYPE}', 'patrol', ?3, ?4)
|
|
99
|
+
ON CONFLICT(task_id, event_key) DO NOTHING`,
|
|
100
|
+
[r.task_id, r.status, detail, r.network_id ?? null],
|
|
101
|
+
);
|
|
102
|
+
if (res.changes < 1) continue; // 只做一次:别的巡检 / 别的 Hub worker 已经标过了
|
|
103
|
+
let notified: OrphanFlagged["notified"] = null;
|
|
104
|
+
if (route.kind !== "none") {
|
|
105
|
+
try {
|
|
106
|
+
const startedMs = parseDbTimestampMs(r.started);
|
|
107
|
+
const openMin = Number.isFinite(startedMs) ? Math.max(0, Math.round((nowMs - startedMs) / 60_000)) : null;
|
|
108
|
+
const meta = { task_orphan_suspected: { task_id: r.task_id, target: r.to_name, status: r.status, node_status: r.node_status } };
|
|
109
|
+
const id = deliver(route, r.network_id, r.to_name, r.task_id, orphanNoticeText(r, openMin), meta,
|
|
110
|
+
{ kind: ORPHAN_NOTICE_KIND, status: "orphan_suspected", agentPrefix: "orph_", userPrefix: "dm_orphan_" });
|
|
111
|
+
if (id) notified = route.kind;
|
|
112
|
+
} catch (e: any) {
|
|
113
|
+
console.error(`[patrol] orphan notice failed task=${r.task_id}: ${e?.message || e}`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
flagged.push({ task_id: r.task_id, notified });
|
|
117
|
+
}
|
|
118
|
+
return flagged;
|
|
119
|
+
}
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
// #500 step 2 —— 派活时让发送方看见目标的队列。HTTP 集成测试(真实 Bun.serve,私有端口,临时库)。
|
|
2
|
+
// test2123 在真实 PostgreSQL 上原样再跑一遍(COMMHUB_TEST_PG_URL)。
|
|
3
|
+
//
|
|
4
|
+
// 任务一律经真入口派出:POST /api/task(用户 / 节点令牌)和 /mcp send_task(节点令牌);消息经 /mcp send_message。
|
|
5
|
+
// 只有「历史耗时」和「已开工」这类节点侧时间戳用 SQL 拨。
|
|
6
|
+
//
|
|
7
|
+
// 钉住:
|
|
8
|
+
// 1. queue_ahead = 派这一条之前目标上开着的任务(created/delivered/acked/running,24 小时内);
|
|
9
|
+
// 消息不算、终态不算、超过 24 小时的遗弃行不算、别的节点 / 别的网络不算。REST 与 MCP 同一个数。
|
|
10
|
+
// 2. target_busy:有已开工(acked / running / consumed_at)的开着任务 → true;会话 working(report_status 真入口)→ true。
|
|
11
|
+
// 3. est_wait_minutes:前面没人 → 0;有人但没历史(< 3 条 replied)→ null;有历史 → 中位数 × queue_ahead。
|
|
12
|
+
// 4. warning:queue_ahead 2 → 没有;3 → 有,且仍然 200 ok(从不拒绝);est_wait > 30 → 有。
|
|
13
|
+
// 5. /api/status 全量每行 queue_depth;light / 旧别名解析器投影没有这个字段(字节不变);
|
|
14
|
+
// 只写 tasks 的语句也让全量缓存失效(ETag 变、queue_depth 跟着变);MCP get_all_status 每行 queue_depth。
|
|
15
|
+
//
|
|
16
|
+
// 跑法:cd server && COMMHUB_DB=/tmp/x.db bun test src/task-queue-ahead-http.test.ts
|
|
17
|
+
|
|
18
|
+
import { afterAll, beforeAll, beforeEach, describe, expect, test } from "bun:test";
|
|
19
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
20
|
+
import { tmpdir } from "node:os";
|
|
21
|
+
import { join } from "node:path";
|
|
22
|
+
|
|
23
|
+
const DIR = mkdtempSync(join(tmpdir(), "anet-queue-ahead-"));
|
|
24
|
+
process.env.COMMHUB_DB ||= join(DIR, "hub.db");
|
|
25
|
+
process.env.COMMHUB_UPLOADS_DIR = join(DIR, "uploads");
|
|
26
|
+
process.env.HOST = "127.0.0.1";
|
|
27
|
+
const PW = "QueueAhead123!x";
|
|
28
|
+
const stamp = Date.now();
|
|
29
|
+
|
|
30
|
+
let db: any;
|
|
31
|
+
let hub: any = null;
|
|
32
|
+
let BASE = "";
|
|
33
|
+
let NET = "", NET2 = "";
|
|
34
|
+
let bossToken = "", boss2Token = "";
|
|
35
|
+
type Node = { alias: string; nodeId: string; token: string };
|
|
36
|
+
const nodes: Record<"sender" | "target" | "idle", Node> = {} as any;
|
|
37
|
+
let other: Node;
|
|
38
|
+
|
|
39
|
+
async function send(token: string, method: string, path: string, payload?: unknown, headers: Record<string, string> = {}) {
|
|
40
|
+
const res = await fetch(`${BASE}${path}`, {
|
|
41
|
+
method,
|
|
42
|
+
headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json", ...headers },
|
|
43
|
+
body: payload === undefined ? undefined : JSON.stringify(payload),
|
|
44
|
+
});
|
|
45
|
+
return { status: res.status, headers: res.headers, body: await res.json().catch(() => null) as any };
|
|
46
|
+
}
|
|
47
|
+
async function tool(token: string, name: string, args: Record<string, unknown>) {
|
|
48
|
+
const res = await fetch(`${BASE}/mcp`, {
|
|
49
|
+
method: "POST",
|
|
50
|
+
headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json", Accept: "application/json, text/event-stream", "MCP-Protocol-Version": "2025-03-26" },
|
|
51
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name, arguments: args } }),
|
|
52
|
+
});
|
|
53
|
+
const raw = await res.text();
|
|
54
|
+
const data = raw.split("\n").filter((x) => x.startsWith("data:"));
|
|
55
|
+
const payload = data.length ? JSON.parse(data.at(-1)!.slice(5).trim()) : JSON.parse(raw);
|
|
56
|
+
return JSON.parse(payload.result.content[0].text);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let seq = 0;
|
|
60
|
+
const text = () => `queue case ${stamp} #${++seq}`;
|
|
61
|
+
/** 经 POST /api/task 派一条,返回整个响应体。 */
|
|
62
|
+
async function dispatch(token: string, to: string, extra: Record<string, unknown> = {}) {
|
|
63
|
+
const r = await send(token, "POST", "/api/task", { alias: to, task: text(), network_id: NET, ...extra });
|
|
64
|
+
expect(r.status).toBe(200);
|
|
65
|
+
expect(r.body.ok).toBe(true);
|
|
66
|
+
return r.body;
|
|
67
|
+
}
|
|
68
|
+
const setStatus = (taskId: string, status: string) => db.run("UPDATE tasks SET status = ?1 WHERE task_id = ?2", [status, taskId]);
|
|
69
|
+
const reportStatus = (n: Node, status: string, task?: string) =>
|
|
70
|
+
tool(n.token, "report_status", { resume_id: `sdk-${n.nodeId}`, alias: n.alias, status, node_id: n.nodeId, network_id: NET, ...(task ? { task } : {}) });
|
|
71
|
+
/** 目标上 3 条 replied 历史:耗时 10 / 20 / 30 分钟(started_at 到 completed_at),中位数 20。 */
|
|
72
|
+
async function seedHistory(to: Node, minutes = [10, 20, 30]) {
|
|
73
|
+
for (const m of minutes) {
|
|
74
|
+
const b = await dispatch(bossToken, to.alias);
|
|
75
|
+
db.run(
|
|
76
|
+
`UPDATE tasks SET status = 'replied', created_at = datetime('now', '-${m + 5} minutes'), started_at = datetime('now', '-${m + 1} minutes'),
|
|
77
|
+
completed_at = datetime('now', '-1 minutes') WHERE task_id = ?1`,
|
|
78
|
+
[b.task_id],
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
beforeAll(async () => {
|
|
84
|
+
({ db } = await import("./db.js"));
|
|
85
|
+
const { createNetworkTokenForNode, register } = await import("./auth.js");
|
|
86
|
+
const boss = register(`qa_boss_${stamp}`, PW);
|
|
87
|
+
expect(boss.ok).toBe(true);
|
|
88
|
+
bossToken = boss.token!; NET = boss.network_id!;
|
|
89
|
+
const bossId = boss.user!.user_id;
|
|
90
|
+
for (const key of ["sender", "target", "idle"] as const) {
|
|
91
|
+
const alias = `qa-${key}-${stamp}`;
|
|
92
|
+
const nodeId = `n_qa_${key}_${stamp}`;
|
|
93
|
+
const minted = createNetworkTokenForNode(bossId, NET, alias, nodeId);
|
|
94
|
+
expect(minted.ok).toBe(true);
|
|
95
|
+
nodes[key] = { alias, nodeId, token: minted.token! };
|
|
96
|
+
}
|
|
97
|
+
// 另一个网络里一个**同名**节点:它的任务不许算进 NET 的队列。
|
|
98
|
+
const boss2 = register(`qa_boss2_${stamp}`, PW);
|
|
99
|
+
boss2Token = boss2.token!; NET2 = boss2.network_id!;
|
|
100
|
+
const m2 = createNetworkTokenForNode(boss2.user!.user_id, NET2, nodes.target.alias, `n_qa_other_${stamp}`);
|
|
101
|
+
other = { alias: nodes.target.alias, nodeId: `n_qa_other_${stamp}`, token: m2.token! };
|
|
102
|
+
const mod: any = await import("./server.js");
|
|
103
|
+
hub = mod.bootServer({ port: 0, hostname: "127.0.0.1" });
|
|
104
|
+
BASE = `http://127.0.0.1:${hub.port}`;
|
|
105
|
+
for (const n of Object.values(nodes)) expect((await reportStatus(n, "idle")).ok).toBe(true);
|
|
106
|
+
expect((await tool(other.token, "report_status", { resume_id: `sdk-${other.nodeId}`, alias: other.alias, status: "idle", node_id: other.nodeId, network_id: NET2 })).ok).toBe(true);
|
|
107
|
+
}, 30_000);
|
|
108
|
+
|
|
109
|
+
beforeEach(async () => {
|
|
110
|
+
// 每条用例从「目标上没有开着的任务、没有历史、会话 idle」开始。
|
|
111
|
+
db.run("UPDATE tasks SET status = 'cancelled', completed_at = datetime('now') WHERE network_id IN (?1, ?2) AND status IN ('created', 'delivered', 'acked', 'running')", [NET, NET2]);
|
|
112
|
+
db.run("DELETE FROM tasks WHERE network_id = ?1 AND status = 'replied'", [NET]);
|
|
113
|
+
for (const n of Object.values(nodes)) await reportStatus(n, "idle");
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
afterAll(() => {
|
|
117
|
+
try { hub?.stop?.(true); } catch {}
|
|
118
|
+
try { rmSync(DIR, { recursive: true, force: true }); } catch {}
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe("#500 queue_ahead on dispatch", () => {
|
|
122
|
+
test("empty target → queue_ahead 0, not busy, est_wait 0, no warning (REST and MCP)", async () => {
|
|
123
|
+
const r = await dispatch(nodes.sender.token, nodes.target.alias);
|
|
124
|
+
expect(r).toMatchObject({ queue_ahead: 0, target_busy: false, est_wait_minutes: 0 });
|
|
125
|
+
expect(r.warning).toBeUndefined();
|
|
126
|
+
setStatus(r.task_id, "cancelled");
|
|
127
|
+
const m = await tool(nodes.sender.token, "send_task", { alias: nodes.target.alias, task: text(), network_id: NET });
|
|
128
|
+
expect(m.ok).toBe(true);
|
|
129
|
+
expect(m).toMatchObject({ queue_ahead: 0, target_busy: false, est_wait_minutes: 0 });
|
|
130
|
+
expect(m.warning).toBeUndefined();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("counts open tasks only: messages, terminal, >24 h stale, other nodes and other networks are excluded", async () => {
|
|
134
|
+
const a = await dispatch(bossToken, nodes.target.alias); // delivered → counts
|
|
135
|
+
const b = await dispatch(nodes.idle.token, nodes.target.alias); // delivered → counts (another sender)
|
|
136
|
+
// 消息:只写 inbox,不算。
|
|
137
|
+
expect((await tool(nodes.sender.token, "send_message", { alias: nodes.target.alias, message: `msg ${stamp}`, network_id: NET })).ok).toBe(true);
|
|
138
|
+
// 终态:不算。
|
|
139
|
+
for (const st of ["replied", "failed", "expired", "cancelled"]) setStatus((await dispatch(bossToken, nodes.target.alias)).task_id, st);
|
|
140
|
+
// 24 小时之前派出、从没终态的遗弃行:不算。
|
|
141
|
+
const stale = await dispatch(bossToken, nodes.target.alias);
|
|
142
|
+
db.run("UPDATE tasks SET status = 'running', created_at = datetime('now', '-25 hours') WHERE task_id = ?1", [stale.task_id]);
|
|
143
|
+
// 别的节点 / 别的网络里同名节点:不算。
|
|
144
|
+
await dispatch(bossToken, nodes.idle.alias);
|
|
145
|
+
const o = await send(boss2Token, "POST", "/api/task", { alias: other.alias, task: text(), network_id: NET2 });
|
|
146
|
+
expect(o.status).toBe(200);
|
|
147
|
+
expect(o.body.queue_ahead).toBe(0);
|
|
148
|
+
|
|
149
|
+
const r = await dispatch(nodes.sender.token, nodes.target.alias);
|
|
150
|
+
expect(r.queue_ahead).toBe(2);
|
|
151
|
+
expect(r.target_busy).toBe(false);
|
|
152
|
+
// MCP 同一个数(上一条也开着了 → 3)。
|
|
153
|
+
const m = await tool(nodes.sender.token, "send_task", { alias: nodes.target.alias, task: text(), network_id: NET });
|
|
154
|
+
expect(m.queue_ahead).toBe(3);
|
|
155
|
+
// acked / running 也是开着的。
|
|
156
|
+
setStatus(a.task_id, "acked"); setStatus(b.task_id, "running");
|
|
157
|
+
const r2 = await dispatch(nodes.sender.token, nodes.target.alias);
|
|
158
|
+
expect(r2.queue_ahead).toBe(4);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test("target_busy: a started open task, or the session reporting working", async () => {
|
|
162
|
+
const a = await dispatch(bossToken, nodes.target.alias);
|
|
163
|
+
expect((await dispatch(nodes.sender.token, nodes.target.alias)).target_busy).toBe(false);
|
|
164
|
+
// 运行时取走(consumed_at)但状态还是 delivered → 已开工。
|
|
165
|
+
db.run("UPDATE tasks SET consumed_at = datetime('now') WHERE task_id = ?1", [a.task_id]);
|
|
166
|
+
expect((await dispatch(nodes.sender.token, nodes.target.alias)).target_busy).toBe(true);
|
|
167
|
+
db.run("UPDATE tasks SET status = 'cancelled' WHERE network_id = ?1 AND status = 'delivered'", [NET]);
|
|
168
|
+
expect((await dispatch(nodes.sender.token, nodes.target.alias)).target_busy).toBe(false);
|
|
169
|
+
// 真入口:节点 report_status(working, task=<原文>)→ 任务 running + 会话 working。
|
|
170
|
+
const c = await dispatch(bossToken, nodes.target.alias);
|
|
171
|
+
const content = db.get("SELECT content FROM tasks WHERE task_id = ?1", c.task_id).content;
|
|
172
|
+
expect((await reportStatus(nodes.target, "working", content)).ok).toBe(true);
|
|
173
|
+
expect(db.get("SELECT status FROM tasks WHERE task_id = ?1", c.task_id).status).toBe("running");
|
|
174
|
+
const r = await dispatch(nodes.sender.token, nodes.target.alias);
|
|
175
|
+
expect(r.target_busy).toBe(true);
|
|
176
|
+
expect(r.queue_ahead).toBe(2);
|
|
177
|
+
// 会话 working 但没有开着的任务 → 仍然 busy(节点自己说在忙)。
|
|
178
|
+
db.run("UPDATE tasks SET status = 'cancelled' WHERE network_id = ?1 AND status IN ('delivered','running')", [NET]);
|
|
179
|
+
const w = await dispatch(nodes.sender.token, nodes.target.alias);
|
|
180
|
+
expect(w).toMatchObject({ queue_ahead: 0, target_busy: true, est_wait_minutes: 0 });
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test("est_wait_minutes: null without history, median × queue_ahead with it", async () => {
|
|
184
|
+
await dispatch(bossToken, nodes.target.alias);
|
|
185
|
+
const noHist = await dispatch(nodes.sender.token, nodes.target.alias);
|
|
186
|
+
expect(noHist.queue_ahead).toBe(1);
|
|
187
|
+
expect(noHist.est_wait_minutes).toBeNull();
|
|
188
|
+
// 两条历史还不够(< 3)。
|
|
189
|
+
await seedHistory(nodes.target, [10, 20]);
|
|
190
|
+
expect((await dispatch(nodes.sender.token, nodes.target.alias)).est_wait_minutes).toBeNull();
|
|
191
|
+
db.run("DELETE FROM tasks WHERE network_id = ?1 AND status = 'replied'", [NET]);
|
|
192
|
+
await seedHistory(nodes.target, [10, 20, 60]);
|
|
193
|
+
const r = await dispatch(nodes.sender.token, nodes.target.alias);
|
|
194
|
+
expect(r.queue_ahead).toBe(3);
|
|
195
|
+
expect(r.est_wait_minutes).toBe(60); // 中位数 20 × 3(平均数会是 30 × 3 = 90)
|
|
196
|
+
// 别的节点的历史不借用。
|
|
197
|
+
const idle = await dispatch(nodes.sender.token, nodes.idle.alias);
|
|
198
|
+
expect(idle.est_wait_minutes).toBe(0);
|
|
199
|
+
const idle2 = await dispatch(nodes.sender.token, nodes.idle.alias);
|
|
200
|
+
expect(idle2.est_wait_minutes).toBeNull();
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
test("warning at queue_ahead ≥ 3 or est_wait > 30 — the send still succeeds", async () => {
|
|
204
|
+
await dispatch(bossToken, nodes.target.alias);
|
|
205
|
+
await dispatch(bossToken, nodes.target.alias);
|
|
206
|
+
const two = await dispatch(nodes.sender.token, nodes.target.alias); // 前面 2 条
|
|
207
|
+
expect(two.queue_ahead).toBe(2);
|
|
208
|
+
expect(two.warning).toBeUndefined();
|
|
209
|
+
const three = await dispatch(nodes.sender.token, nodes.target.alias, { ttl_seconds: 1800 }); // 前面 3 条
|
|
210
|
+
expect(three.ok).toBe(true);
|
|
211
|
+
expect(three.queue_ahead).toBe(3);
|
|
212
|
+
expect(typeof three.warning).toBe("string");
|
|
213
|
+
for (const frag of [nodes.target.alias, "3 open task(s) ahead", "queued, not refused", "idle node", "ttl_seconds", "30 min", "Do not resend"]) {
|
|
214
|
+
expect(three.warning).toContain(frag);
|
|
215
|
+
}
|
|
216
|
+
// 任务确实写进去了(没被拒)。
|
|
217
|
+
expect(db.get("SELECT status FROM tasks WHERE task_id = ?1", three.task_id).status).toBe("delivered");
|
|
218
|
+
// MCP 同样带 warning。
|
|
219
|
+
const m = await tool(nodes.sender.token, "send_task", { alias: nodes.target.alias, task: text(), network_id: NET });
|
|
220
|
+
expect(m.ok).toBe(true);
|
|
221
|
+
expect(m.queue_ahead).toBe(4);
|
|
222
|
+
expect(m.warning).toContain("4 open task(s) ahead");
|
|
223
|
+
|
|
224
|
+
// 只排 1 个,但估计要等 > 30 分钟 → 也给。
|
|
225
|
+
db.run("UPDATE tasks SET status = 'cancelled' WHERE network_id = ?1 AND status = 'delivered'", [NET]);
|
|
226
|
+
await seedHistory(nodes.target, [40, 45, 50]);
|
|
227
|
+
await dispatch(bossToken, nodes.target.alias);
|
|
228
|
+
const slow = await dispatch(nodes.sender.token, nodes.target.alias, { ttl_seconds: 1800 });
|
|
229
|
+
expect(slow).toMatchObject({ queue_ahead: 1, est_wait_minutes: 45 });
|
|
230
|
+
expect(slow.warning).toContain("rough wait ~45 min");
|
|
231
|
+
expect(slow.warning).toContain("may expire before it starts");
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
describe("#500 queue_depth on status reads", () => {
|
|
236
|
+
const rowOf = (body: any, alias: string) => body.sessions.find((s: any) => s.alias === alias);
|
|
237
|
+
|
|
238
|
+
test("/api/status full rows carry queue_depth; light and the old alias-resolver projection do not", async () => {
|
|
239
|
+
await dispatch(bossToken, nodes.target.alias);
|
|
240
|
+
await dispatch(bossToken, nodes.target.alias);
|
|
241
|
+
const full = await send(bossToken, "GET", `/api/status?network_id=${NET}`);
|
|
242
|
+
expect(full.status).toBe(200);
|
|
243
|
+
expect(rowOf(full.body, nodes.target.alias).queue_depth).toBe(2);
|
|
244
|
+
expect(rowOf(full.body, nodes.idle.alias).queue_depth).toBe(0);
|
|
245
|
+
const light = await send(bossToken, "GET", `/api/status?network_id=${NET}&light=1`);
|
|
246
|
+
expect("queue_depth" in rowOf(light.body, nodes.target.alias)).toBe(false);
|
|
247
|
+
const resolver = await send(nodes.idle.token, "GET", `/api/status?network_id=${NET}`, undefined, { Accept: "application/json" });
|
|
248
|
+
expect(resolver.headers.get("x-status-projection")).toBe("alias-resolver");
|
|
249
|
+
expect("queue_depth" in rowOf(resolver.body, nodes.target.alias)).toBe(false);
|
|
250
|
+
// 另一个网络里的同名节点:看不到 NET 的队列。
|
|
251
|
+
const net2 = await send(boss2Token, "GET", `/api/status?network_id=${NET2}`);
|
|
252
|
+
expect(rowOf(net2.body, other.alias).queue_depth).toBe(0);
|
|
253
|
+
// App 的单行读(?alias=)只查这一个别名,数一样。
|
|
254
|
+
const one = await send(bossToken, "GET", `/api/status?network_id=${NET}&alias=${encodeURIComponent(nodes.target.alias)}`);
|
|
255
|
+
expect(one.body.sessions.length).toBe(1);
|
|
256
|
+
expect(one.body.sessions[0].queue_depth).toBe(2);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
test("queueDepthByNode: the alias-list query and the whole-table scan (> 50 aliases) give the same counts", async () => {
|
|
260
|
+
const { queueDepthByNode, queueDepthKey, QUEUE_DEPTH_ALIAS_LIST_MAX } = await import("./task-queue-ahead.js");
|
|
261
|
+
await dispatch(bossToken, nodes.target.alias);
|
|
262
|
+
await dispatch(bossToken, nodes.target.alias);
|
|
263
|
+
await dispatch(bossToken, nodes.idle.alias);
|
|
264
|
+
await send(boss2Token, "POST", "/api/task", { alias: other.alias, task: text(), network_id: NET2 });
|
|
265
|
+
const few = queueDepthByNode([nodes.target.alias, nodes.idle.alias]);
|
|
266
|
+
const fillers = Array.from({ length: QUEUE_DEPTH_ALIAS_LIST_MAX }, (_, i) => `qa-filler-${i}`);
|
|
267
|
+
const many = queueDepthByNode([nodes.target.alias, nodes.idle.alias, ...fillers]);
|
|
268
|
+
for (const m of [few, many]) {
|
|
269
|
+
expect(m.get(queueDepthKey(NET, nodes.target.alias))).toBe(2);
|
|
270
|
+
expect(m.get(queueDepthKey(NET, nodes.idle.alias))).toBe(1);
|
|
271
|
+
expect(m.get(queueDepthKey(NET2, other.alias))).toBe(1); // 同名、别的网络:分开计
|
|
272
|
+
}
|
|
273
|
+
expect(queueDepthByNode([]).size).toBe(0);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test("a write that touches only tasks invalidates the cached full body (new ETag, new queue_depth); light stays cached", async () => {
|
|
277
|
+
const a = await dispatch(bossToken, nodes.target.alias);
|
|
278
|
+
await dispatch(bossToken, nodes.target.alias);
|
|
279
|
+
const first = await send(bossToken, "GET", `/api/status?network_id=${NET}`);
|
|
280
|
+
const etag = first.headers.get("etag")!;
|
|
281
|
+
expect(rowOf(first.body, nodes.target.alias).queue_depth).toBe(2);
|
|
282
|
+
const lightBefore = await send(bossToken, "GET", `/api/status?network_id=${NET}&light=1`);
|
|
283
|
+
// 只改 tasks(不碰 sessions):全量必须重算。
|
|
284
|
+
setStatus(a.task_id, "replied");
|
|
285
|
+
const again = await send(bossToken, "GET", `/api/status?network_id=${NET}`, undefined, { "If-None-Match": etag });
|
|
286
|
+
expect(again.status).toBe(200);
|
|
287
|
+
expect(again.headers.get("etag")).not.toBe(etag);
|
|
288
|
+
expect(rowOf(again.body, nodes.target.alias).queue_depth).toBe(1);
|
|
289
|
+
// light 不读 tasks:同一个 ETag → 304。
|
|
290
|
+
const lightAfter = await fetch(`${BASE}/api/status?network_id=${NET}&light=1`, { headers: { Authorization: `Bearer ${bossToken}`, "If-None-Match": lightBefore.headers.get("etag")! } });
|
|
291
|
+
expect(lightAfter.status).toBe(304);
|
|
292
|
+
// 没有写 → 全量命中缓存:同一个 ETag → 304。
|
|
293
|
+
const cached = await fetch(`${BASE}/api/status?network_id=${NET}`, { headers: { Authorization: `Bearer ${bossToken}`, "If-None-Match": again.headers.get("etag")! } });
|
|
294
|
+
expect(cached.status).toBe(304);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
test("MCP get_all_status rows carry queue_depth", async () => {
|
|
298
|
+
await dispatch(bossToken, nodes.target.alias);
|
|
299
|
+
const r = await tool(bossToken, "get_all_status", { network_id: NET, filter_alias: `${nodes.target.alias},${nodes.idle.alias}` });
|
|
300
|
+
expect(r.ok).toBe(true);
|
|
301
|
+
expect(rowOf(r, nodes.target.alias).queue_depth).toBe(1);
|
|
302
|
+
expect(rowOf(r, nodes.idle.alias).queue_depth).toBe(0);
|
|
303
|
+
// filter_status + filter_alias on top of the network scope: every placeholder numbered (PG rejected the mix).
|
|
304
|
+
const f = await tool(bossToken, "get_all_status", { network_id: NET, filter_status: "idle", filter_alias: nodes.target.alias });
|
|
305
|
+
expect(f.ok).toBe(true);
|
|
306
|
+
expect(f.sessions.length).toBe(1);
|
|
307
|
+
expect(f.sessions[0].queue_depth).toBe(1);
|
|
308
|
+
});
|
|
309
|
+
});
|