@sleep2agi/commhub-server 0.9.0-preview.22 → 0.9.0-preview.221
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +129 -9
- package/bin/commhub.ts +93 -9
- package/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 +654 -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 +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 +52 -2
- package/src/avatar-validate.ts +35 -1
- package/src/backfill-node-ids-2664.test.ts +128 -0
- package/src/binding-generation-http.test.ts +121 -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/chain-attempt-dedup.test.ts +447 -0
- package/src/clone-node.test.ts +248 -0
- package/src/clone-node.ts +289 -0
- package/src/codex-config-flags.ts +77 -0
- package/src/codex-fork-contract.ts +23 -0
- package/src/codex-fork-migration.test.ts +38 -0
- package/src/commhub-bin-args.test.ts +85 -0
- package/src/config-apply-validate.test.ts +188 -1
- package/src/config-apply-validate.ts +222 -3
- package/src/cors-origins.test.ts +56 -0
- package/src/create-node-blocked-gate.test.ts +356 -0
- package/src/create-node-network-id.test.ts +91 -0
- package/src/create-node-tool-schema.test.ts +49 -0
- package/src/create-node-unicode-name.test.ts +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 +1296 -153
- package/src/delivered-without-push.test.ts +140 -0
- package/src/department-groups-http.test.ts +209 -0
- package/src/department-groups-sync-http.test.ts +328 -0
- package/src/department-groups.ts +434 -0
- package/src/department-heads-http.test.ts +350 -0
- package/src/department-heads.ts +226 -0
- package/src/departments-agent-count.test.ts +132 -0
- package/src/departments-http.test.ts +185 -0
- package/src/departments-mcp.test.ts +128 -0
- package/src/departments.ts +318 -0
- package/src/desktop-message-seam.test.ts +150 -0
- package/src/desktop-message.test.ts +368 -0
- package/src/dm-file-scope-http.test.ts +178 -0
- package/src/empty-node-sweeper.test.ts +104 -0
- package/src/empty-node-sweeper.ts +88 -0
- package/src/env-transport-gate-http.test.ts +240 -0
- package/src/env-transport-gate.ts +80 -0
- package/src/env-transport-policy.ts +56 -0
- package/src/etag-of.test.ts +57 -0
- package/src/event-loop-lag.test.ts +180 -0
- package/src/event-loop-lag.ts +201 -0
- package/src/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-anon-readers.ts +59 -0
- package/src/health-host-disk-2756.test.ts +71 -0
- package/src/health-redaction.test.ts +1583 -2
- 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 +266 -0
- package/src/hub-provider-config.test.ts +156 -0
- package/src/hub-provider-config.ts +124 -0
- package/src/hub-timestamp.test.ts +38 -0
- package/src/hub-timestamp.ts +27 -0
- package/src/human-dm.ts +215 -0
- package/src/human-inbox.ts +42 -0
- package/src/inbox-caller.ts +49 -0
- package/src/inbox-count-sse.test.ts +310 -0
- package/src/inbox-count.ts +29 -0
- package/src/index.ts +6 -2
- package/src/license-state.test.ts +56 -0
- package/src/license-state.ts +48 -0
- package/src/list-host-supervisors.test.ts +15 -4
- package/src/mcp-context-cost-http.test.ts +127 -0
- package/src/mcp-inflight.test.ts +92 -0
- package/src/mcp-inflight.ts +38 -0
- package/src/mcp-projects-events-http.test.ts +165 -0
- package/src/mcp-route-label-http.test.ts +120 -0
- package/src/mcp-write-network-resolution.test.ts +435 -0
- package/src/member-audit-network-id-http.test.ts +93 -0
- package/src/member-presence-http.test.ts +210 -0
- package/src/messages-ack-by-agent-http.test.ts +142 -0
- package/src/migrate-sqlite-to-pg.test.ts +47 -0
- package/src/migrate-sqlite-to-pg.ts +263 -0
- package/src/model-auth-notify-http.test.ts +181 -0
- package/src/model-auth-notify.ts +116 -0
- package/src/network-name-http.test.ts +75 -0
- package/src/network-rules-http.test.ts +169 -0
- package/src/network-rules.ts +129 -0
- package/src/network-scope.ts +276 -0
- package/src/network-token-membership-http.test.ts +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 +1091 -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 +335 -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 +250 -0
- package/src/node-lifecycle-read.ts +124 -0
- package/src/node-logs-transport.test.ts +263 -0
- package/src/node-peer-allowlist.test.ts +114 -0
- package/src/node-permissions-http.test.ts +728 -0
- package/src/node-permissions.ts +494 -0
- package/src/node-request-retention.test.ts +281 -0
- package/src/node-request-retention.ts +120 -0
- package/src/node-secret-rest-http.test.ts +190 -0
- package/src/node-serviceability.test.ts +86 -0
- package/src/node-serviceability.ts +124 -0
- package/src/node-session-control-http.test.ts +310 -0
- package/src/node-session-control.ts +258 -0
- package/src/node-skills-transport.test.ts +212 -0
- package/src/node-token-identity.ts +35 -0
- package/src/node-token-ownership.ts +51 -0
- package/src/node-token-privilege-http.test.ts +158 -0
- package/src/not-found-api-http.test.ts +67 -0
- package/src/observer-avatar-http.test.ts +102 -9
- package/src/observer-push.test.ts +76 -0
- package/src/opencode-catalog-report-http.test.ts +139 -0
- package/src/opencode-v2-catalog.test.ts +268 -0
- package/src/opencode-v2-catalog.ts +356 -0
- package/src/package-version-consistency.test.ts +25 -0
- package/src/parent-source-marker.test.ts +50 -0
- package/src/password-network-tokens.ts +25 -0
- package/src/peer-reply-atomic.test.ts +457 -0
- package/src/peer-reply-capability.test.ts +70 -0
- package/src/pg-worker.ts +87 -0
- package/src/poll-pacing.test.ts +74 -0
- package/src/poll-pacing.ts +123 -0
- package/src/project-dir-warn-clear-http.test.ts +132 -0
- package/src/project-viewer-can-http.test.ts +116 -0
- package/src/push.test.ts +20 -10
- package/src/push.ts +382 -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 +295 -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-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 +2057 -0
- package/src/resolve-port.test.ts +57 -0
- package/src/resolve-port.ts +44 -0
- package/src/rest-alias-network-fallback-http.test.ts +123 -0
- package/src/rest-alias-network-fallback.test.ts +60 -0
- package/src/rest-alias-network-fallback.ts +65 -0
- package/src/rest-explicit-columns-http.test.ts +266 -0
- package/src/rest-identity.test.ts +130 -0
- package/src/rest-identity.ts +141 -0
- package/src/rest-projections.ts +72 -0
- package/src/rest-read-network-real-chain.test.ts +97 -0
- package/src/rest-write-network-resolution.test.ts +133 -0
- package/src/restart-readiness-http.test.ts +121 -0
- package/src/restricted-files.ts +76 -0
- package/src/retention-first-sweep-quiet.test.ts +31 -0
- package/src/retention.test.ts +100 -40
- package/src/retention.ts +423 -104
- package/src/route-callers-http.test.ts +120 -0
- package/src/route-timing.test.ts +218 -0
- package/src/route-timing.ts +262 -0
- package/src/rules-file-duplicate-alias.test.ts +234 -0
- package/src/rules-file-session-target.test.ts +207 -0
- package/src/rules-file-transport.test.ts +237 -0
- package/src/runtime-label.test.ts +22 -0
- package/src/runtime-label.ts +21 -0
- package/src/runtime-readiness.ts +90 -0
- package/src/schedule-agent-mcp-http.test.ts +432 -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 +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 +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 +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 +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/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 +73 -0
- package/src/serve-error.ts +56 -0
- package/src/server.ts +4945 -338
- package/src/session-task-on-dispatch.ts +14 -0
- package/src/session-token-ownership-http.test.ts +122 -0
- package/src/shared/codex-inventory.ts +41 -0
- package/src/shared/daemon-provider-login.ts +31 -0
- package/src/shared/daemon-provider-snapshot.ts +52 -0
- package/src/shared/external-schedule-contract.ts +60 -0
- package/src/shared/node-name.test.ts +62 -0
- package/src/shared/node-name.ts +161 -0
- package/src/shared/probe-host-allowlist-drift.test.ts +45 -0
- package/src/shared/provider-application.ts +29 -0
- package/src/shared/provider-collection.ts +14 -0
- package/src/shared/provider-initialization.ts +8 -0
- package/src/shared/reserved-env-drift.test.ts +10 -0
- package/src/shared/scoped-secret-contract.ts +5 -0
- package/src/shared/task-content-limit-drift.test.ts +28 -0
- package/src/shared/task-content-limit.ts +25 -0
- package/src/side-thread-command-transport.test.ts +312 -0
- package/src/side-thread-command-transport.ts +318 -0
- package/src/side-thread-http-integration.test.ts +233 -0
- package/src/side-thread-http.ts +504 -0
- package/src/side-thread-production.test.ts +72 -0
- package/src/side-thread-production.ts +118 -0
- package/src/side-thread.test.ts +1067 -0
- package/src/side-thread.ts +2499 -0
- package/src/skillhub-http.test.ts +142 -0
- package/src/sqlite-close-checkpoint.test.ts +78 -0
- package/src/start-node.test.ts +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 +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 +490 -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 +227 -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 +58 -0
- package/src/task-sender-http.test.ts +143 -0
- package/src/task-stale-open-http.test.ts +292 -0
- package/src/task-stale-open.ts +103 -0
- package/src/tasks-pagination-http.test.ts +141 -0
- package/src/team-rules-status-http.test.ts +90 -0
- package/src/team-rules-status.test.ts +59 -0
- package/src/team-rules-status.ts +72 -0
- package/src/tmux-allowlist.test.ts +48 -0
- package/src/tool-audience-http.test.ts +230 -0
- package/src/tool-audience.ts +127 -0
- package/src/tools-list-json-schema.test.ts +94 -0
- package/src/tools.ts +4320 -413
- package/src/upload-early-reject-guard.test.ts +155 -0
- package/src/uploads-http.test.ts +86 -7
- 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
package/src/pg-worker.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PostgreSQL worker for PgAdapter (RFC-039 S2b).
|
|
3
|
+
*
|
|
4
|
+
* Holds ONE reserved Bun.SQL connection for the life of the Hub, so BEGIN /
|
|
5
|
+
* statements / COMMIT all run on the same backend and transactions are real.
|
|
6
|
+
* The main thread posts one request at a time and blocks on `flag` with
|
|
7
|
+
* Atomics.wait; this worker answers on `port` and then notifies, which keeps
|
|
8
|
+
* DbAdapter synchronous exactly like bun:sqlite.
|
|
9
|
+
*
|
|
10
|
+
* Request: { sql, params?, guard? } guard=true wraps the statement in a
|
|
11
|
+
* savepoint so a failed statement inside a transaction behaves like
|
|
12
|
+
* SQLite (the statement fails, the transaction survives).
|
|
13
|
+
* Response: { ok: true, rows, count } | { ok: false, error, code }
|
|
14
|
+
*/
|
|
15
|
+
import { workerData } from "node:worker_threads";
|
|
16
|
+
import { SQL } from "bun";
|
|
17
|
+
|
|
18
|
+
type Request = { sql: string; params?: unknown[]; guard?: boolean };
|
|
19
|
+
|
|
20
|
+
const { port, flag, url, statementTimeoutMs } = workerData as {
|
|
21
|
+
port: MessagePort;
|
|
22
|
+
flag: Int32Array;
|
|
23
|
+
url: string;
|
|
24
|
+
statementTimeoutMs: number;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// bigint:true makes int8 arrive as BigInt, so it can be told apart from a
|
|
28
|
+
// TEXT column that happens to hold digits; normalise() turns it into a number.
|
|
29
|
+
// libpq's `connect_timeout` (seconds) in the URL is honoured, default 10 s,
|
|
30
|
+
// so an endpoint that accepts TCP but never speaks PostgreSQL fails the
|
|
31
|
+
// startup check instead of hanging the Hub.
|
|
32
|
+
const connectTimeout = Number(new URL(url).searchParams.get("connect_timeout") || 10);
|
|
33
|
+
const sql = new SQL(url, { max: 1, bigint: true, connectionTimeout: connectTimeout > 0 ? connectTimeout : 10 });
|
|
34
|
+
let conn: any = null;
|
|
35
|
+
|
|
36
|
+
async function connection() {
|
|
37
|
+
if (!conn) {
|
|
38
|
+
conn = await sql.reserve();
|
|
39
|
+
await conn.unsafe("SET TIME ZONE 'UTC'");
|
|
40
|
+
await conn.unsafe(`SET statement_timeout = ${Math.max(0, Math.floor(statementTimeoutMs))}`);
|
|
41
|
+
}
|
|
42
|
+
return conn;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function normalise(value: unknown): unknown {
|
|
46
|
+
if (typeof value === "bigint") {
|
|
47
|
+
const n = Number(value);
|
|
48
|
+
return Number.isSafeInteger(n) ? n : value.toString();
|
|
49
|
+
}
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function handle(req: Request) {
|
|
54
|
+
const c = await connection();
|
|
55
|
+
if (req.guard) await c.unsafe("SAVEPOINT anet_stmt");
|
|
56
|
+
let result: any;
|
|
57
|
+
try {
|
|
58
|
+
result = await c.unsafe(req.sql, req.params ?? []);
|
|
59
|
+
} catch (e) {
|
|
60
|
+
if (req.guard) {
|
|
61
|
+
await c.unsafe("ROLLBACK TO SAVEPOINT anet_stmt");
|
|
62
|
+
await c.unsafe("RELEASE SAVEPOINT anet_stmt");
|
|
63
|
+
}
|
|
64
|
+
throw e;
|
|
65
|
+
}
|
|
66
|
+
if (req.guard) await c.unsafe("RELEASE SAVEPOINT anet_stmt");
|
|
67
|
+
const rows = [...result].map((row: Record<string, unknown>) => {
|
|
68
|
+
for (const k of Object.keys(row)) row[k] = normalise(row[k]);
|
|
69
|
+
return row;
|
|
70
|
+
});
|
|
71
|
+
return { ok: true, rows, count: typeof result.count === "number" ? result.count : rows.length };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
port.on("message", async (req: Request) => {
|
|
75
|
+
let out: unknown;
|
|
76
|
+
try {
|
|
77
|
+
out = await handle(req);
|
|
78
|
+
} catch (e: any) {
|
|
79
|
+
// A dropped connection is not retried: a retry inside a transaction would
|
|
80
|
+
// replay half of it. Forget it so the next request reconnects.
|
|
81
|
+
if (String(e?.code ?? "").startsWith("ERR_POSTGRES_CONNECTION")) conn = null;
|
|
82
|
+
out = { ok: false, error: String(e?.message ?? e), code: e?.errno ?? e?.code ?? null };
|
|
83
|
+
}
|
|
84
|
+
port.postMessage(out);
|
|
85
|
+
Atomics.store(flag, 0, 1);
|
|
86
|
+
Atomics.notify(flag, 0);
|
|
87
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { POLL_PACING_NOTE, POLL_NUDGE_THRESHOLD, POLL_RECOMMENDED_SECONDS, createPollPacer } from "./poll-pacing";
|
|
3
|
+
|
|
4
|
+
// #3001 —— Hub 侧的轮询节奏提示。
|
|
5
|
+
// 现场:一个节点在空闲等待里以约 13 次/分钟连续拉了 8 小时 get_task(约 6000 次),
|
|
6
|
+
// 每次都是一整轮 LLM。这里钉住三件事:什么时候提示、什么时候**不**提示、提示里说了什么。
|
|
7
|
+
describe("#3001 get_task 轮询节奏提示", () => {
|
|
8
|
+
function pacerWithClock() {
|
|
9
|
+
let t = 1_000_000;
|
|
10
|
+
const pacer = createPollPacer({ now: () => t });
|
|
11
|
+
return { pacer, advance: (ms: number) => { t += ms; } };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
test("偶发查询不提示(正常等子任务不会被这句打扰)", () => {
|
|
15
|
+
const { pacer, advance } = pacerWithClock();
|
|
16
|
+
for (let i = 0; i < POLL_NUDGE_THRESHOLD; i++) {
|
|
17
|
+
expect(pacer.record("tok:t1:task-a")).toBeNull();
|
|
18
|
+
advance(3_000);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("60 秒内超过阈值才提示,且带次数与建议间隔", () => {
|
|
23
|
+
const { pacer, advance } = pacerWithClock();
|
|
24
|
+
let pacing = null as ReturnType<typeof pacer.record>;
|
|
25
|
+
for (let i = 0; i <= POLL_NUDGE_THRESHOLD; i++) {
|
|
26
|
+
pacing = pacer.record("tok:t1:task-a");
|
|
27
|
+
advance(4_600); // 约 13 次/分钟:实测的失控频率
|
|
28
|
+
}
|
|
29
|
+
expect(pacing).not.toBeNull();
|
|
30
|
+
expect(pacing!.calls_last_60s).toBeGreaterThan(POLL_NUDGE_THRESHOLD);
|
|
31
|
+
expect(pacing!.recommended_interval_seconds).toBe(POLL_RECOMMENDED_SECONDS);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("隔一分钟以上再看,计数清零、不再提示(纪律生效后自动安静)", () => {
|
|
35
|
+
const { pacer, advance } = pacerWithClock();
|
|
36
|
+
for (let i = 0; i <= POLL_NUDGE_THRESHOLD; i++) {
|
|
37
|
+
pacer.record("tok:t1:task-a");
|
|
38
|
+
advance(500);
|
|
39
|
+
}
|
|
40
|
+
advance(POLL_RECOMMENDED_SECONDS * 1000 + 1);
|
|
41
|
+
expect(pacer.record("tok:t1:task-a")).toBeNull();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("不同 task_id 互不累加(同时等多个子任务不算连环打)", () => {
|
|
45
|
+
const { pacer, advance } = pacerWithClock();
|
|
46
|
+
for (let i = 0; i <= POLL_NUDGE_THRESHOLD; i++) {
|
|
47
|
+
expect(pacer.record(`tok:t1:task-${i}`)).toBeNull();
|
|
48
|
+
advance(500);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("身份查询用更宽的阈值(p50 约 2.3 次/分钟是设计频率,不该被提示打扰)", () => {
|
|
53
|
+
const { pacer } = pacerWithClock();
|
|
54
|
+
// 同一个 60 秒窗口内:task_poll 第 5 次就提示,identity_poll 要到第 9 次
|
|
55
|
+
for (let i = 1; i <= 8; i++) expect(pacer.record("whoami:x", "identity_poll")).toBeNull();
|
|
56
|
+
const pacing = pacer.record("whoami:x", "identity_poll");
|
|
57
|
+
expect(pacing).not.toBeNull();
|
|
58
|
+
expect(pacing!.calls_last_60s).toBe(9);
|
|
59
|
+
expect(pacing!.note).toContain("org_whoami");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("提示文案必须说明「结果会自动 chain」——否则 agent 仍会紧盯", () => {
|
|
63
|
+
expect(POLL_PACING_NOTE).toContain("60 秒");
|
|
64
|
+
expect(POLL_PACING_NOTE).toContain("chain 回上游");
|
|
65
|
+
expect(POLL_PACING_NOTE).toContain("一整轮 LLM");
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("key 数量有上限(调用方是外部输入,不能当它只查自己的任务)", () => {
|
|
69
|
+
const pacer = createPollPacer({ maxKeys: 10, windowMs: 1, now: () => 1_000_000 });
|
|
70
|
+
for (let i = 0; i < 500; i++) pacer.record(`tok:t1:task-${i}`);
|
|
71
|
+
// 窗口 1ms,每次 record 时旧条目都已过期,应被回收
|
|
72
|
+
expect(pacer.size()).toBeLessThanOrEqual(11);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #3001 —— get_task 的轮询节奏提示(Hub 侧)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么放在 Hub 而不是只改 agent-node 的提示词:
|
|
5
|
+
* 提示词在 `agent-node/src/cli.ts` 里,改完要发 agent-node + CLI 配对版本,
|
|
6
|
+
* 而且**只有升级过的节点才会变**。而 `get_task` 是 Hub 提供的 MCP 工具 ——
|
|
7
|
+
* 在响应里加一句话,所有节点下一次调用就能看到,不需要任何客户端升级。
|
|
8
|
+
*
|
|
9
|
+
* 现场(TMW微软欧,2026-10-29 16:00Z → 10-30 01:00Z):
|
|
10
|
+
*
|
|
11
|
+
* 每小时 get_task:256 / 565 / 647 / 703 / 770 / 855 / 893 / 972 / 736
|
|
12
|
+
* ⇒ 约 13 次/分钟、连续 8 小时、合计约 6000 次
|
|
13
|
+
*
|
|
14
|
+
* 每次 `get_task` 都是一整轮 LLM 调用。这 6000 次没有推进任何工作,
|
|
15
|
+
* 只是在问「好没好」。而子任务的最终结果本来就会通过 parent_task_id 自动 chain 回上游。
|
|
16
|
+
*
|
|
17
|
+
* 设计取舍:
|
|
18
|
+
* · **不返回错误、不改状态**,只在同一个响应里多一个 `pacing` 字段 ——
|
|
19
|
+
* 调用方即使完全忽略它也照常工作(信息不缺,行为不破)。
|
|
20
|
+
* · 阈值 4 次/60 秒:正常的「隔几分钟看一眼」永远碰不到;
|
|
21
|
+
* 只有真正在连环打的才会看到这句提示。
|
|
22
|
+
* · 计数按 (调用方, task_id) 分开,避免把「同时等多个子任务」误判成连环打。
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export type PollPacing = {
|
|
26
|
+
calls_last_60s: number;
|
|
27
|
+
recommended_interval_seconds: number;
|
|
28
|
+
note: string;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const WHOAMI_PACING_NOTE =
|
|
32
|
+
"org_whoami 只需要在「不确定自己是谁/团队变了」时查。实测有节点以约 4–6 次/分钟连续查询、" +
|
|
33
|
+
"同时窗口内没接到任何任务 —— 这些调用没有推进工作。建议隔 60 秒以上再看。";
|
|
34
|
+
|
|
35
|
+
export const POLL_PACING_NOTE =
|
|
36
|
+
"每次 get_task 都是一整轮 LLM 调用。子任务的最终结果会通过 parent_task_id 自动 chain 回上游," +
|
|
37
|
+
"所以不必连续轮询 —— 建议隔 60 秒以上再看。";
|
|
38
|
+
|
|
39
|
+
export const POLL_WINDOW_MS = 60_000;
|
|
40
|
+
export const POLL_NUDGE_THRESHOLD = 4;
|
|
41
|
+
export const POLL_RECOMMENDED_SECONDS = 60;
|
|
42
|
+
|
|
43
|
+
/** 两类轮询的阈值与文案。分开是因为「正常频率」差一个数量级。 */
|
|
44
|
+
export const PACE_KINDS = {
|
|
45
|
+
/** 等子任务:正常是「隔几分钟看一眼」,1 分钟内 4 次以上基本就是连环打。 */
|
|
46
|
+
task_poll: { threshold: POLL_NUDGE_THRESHOLD, note: POLL_PACING_NOTE },
|
|
47
|
+
/** 身份查询:p50 约 2.3 次/分钟是设计频率,1 分钟内 8 次以上才提示。 */
|
|
48
|
+
identity_poll: { threshold: 8, note: WHOAMI_PACING_NOTE },
|
|
49
|
+
} as const;
|
|
50
|
+
|
|
51
|
+
export type PaceKind = keyof typeof PACE_KINDS;
|
|
52
|
+
|
|
53
|
+
export type PollPacer = {
|
|
54
|
+
/** 记一次调用;超过阈值时返回提示,否则返回 null。 */
|
|
55
|
+
record(key: string, kind?: PaceKind): PollPacing | null;
|
|
56
|
+
/** 仅测试用:当前记录的 key 数。 */
|
|
57
|
+
size(): number;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export function createPollPacer(opts: {
|
|
61
|
+
windowMs?: number;
|
|
62
|
+
threshold?: number;
|
|
63
|
+
maxKeys?: number;
|
|
64
|
+
now?: () => number;
|
|
65
|
+
} = {}): PollPacer {
|
|
66
|
+
const windowMs = opts.windowMs ?? POLL_WINDOW_MS;
|
|
67
|
+
const defaultThreshold = opts.threshold ?? POLL_NUDGE_THRESHOLD;
|
|
68
|
+
const maxKeys = opts.maxKeys ?? 2000;
|
|
69
|
+
const now = opts.now ?? Date.now;
|
|
70
|
+
const seen = new Map<string, number[]>();
|
|
71
|
+
|
|
72
|
+
function prune(ts: number[], cutoff: number): number[] {
|
|
73
|
+
// 时间戳按升序追加,从头部删到窗口内为止。
|
|
74
|
+
let i = 0;
|
|
75
|
+
while (i < ts.length && ts[i] <= cutoff) i++;
|
|
76
|
+
return i === 0 ? ts : ts.slice(i);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
record(key: string, kind: PaceKind = "task_poll"): PollPacing | null {
|
|
81
|
+
// 显式传了 threshold(测试用)就压过 kind 表
|
|
82
|
+
const spec = opts.threshold !== undefined
|
|
83
|
+
? { threshold: defaultThreshold, note: PACE_KINDS[kind].note }
|
|
84
|
+
: PACE_KINDS[kind];
|
|
85
|
+
const t = now();
|
|
86
|
+
const cutoff = t - windowMs;
|
|
87
|
+
const prev = seen.get(key);
|
|
88
|
+
const ts = prune(prev ?? [], cutoff);
|
|
89
|
+
ts.push(t);
|
|
90
|
+
seen.set(key, ts);
|
|
91
|
+
|
|
92
|
+
// 无上限的 Map 就是一条内存泄漏 —— 调用方是外部输入,不能假设它只查自己的任务。
|
|
93
|
+
if (seen.size > maxKeys) {
|
|
94
|
+
for (const k of seen.keys()) {
|
|
95
|
+
if (seen.size <= maxKeys) break;
|
|
96
|
+
const keep = prune(seen.get(k) ?? [], cutoff);
|
|
97
|
+
if (keep.length === 0) seen.delete(k);
|
|
98
|
+
else seen.set(k, keep);
|
|
99
|
+
}
|
|
100
|
+
// 光靠过期回收不够:如果所有 key 都在窗口内(例如有人用几百个 task_id 乱打),
|
|
101
|
+
// 过期一条也删不掉,长度仍然无界 —— 这里再按插入顺序硬砍到上限。
|
|
102
|
+
while (seen.size > maxKeys) {
|
|
103
|
+
const oldest = seen.keys().next();
|
|
104
|
+
if (oldest.done) break;
|
|
105
|
+
seen.delete(oldest.value);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (ts.length <= spec.threshold) return null;
|
|
110
|
+
return {
|
|
111
|
+
calls_last_60s: ts.length,
|
|
112
|
+
recommended_interval_seconds: POLL_RECOMMENDED_SECONDS,
|
|
113
|
+
note: spec.note,
|
|
114
|
+
};
|
|
115
|
+
},
|
|
116
|
+
size(): number {
|
|
117
|
+
return seen.size;
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** 进程级单例:Hub 只需要看「最近一分钟这位调用方问了多少次」。 */
|
|
123
|
+
export const pollPacer = createPollPacer();
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// #667 — a restart from the right directory must clear the cwd warning the
|
|
2
|
+
// previous process left on the same resume_id. Hub stores
|
|
3
|
+
// `task = COALESCE(?11, sessions.task)`, so omitting task keeps the warning.
|
|
4
|
+
// Register sends task: "" once. Later idle reports omit task, so the
|
|
5
|
+
// description of a finished task stays. This boots a throwaway hub on
|
|
6
|
+
// port 0 (never 9200).
|
|
7
|
+
|
|
8
|
+
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
9
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
10
|
+
import { tmpdir } from "node:os";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import { reportedTask } from "../../agent-node/src/project-dir-mismatch.ts";
|
|
13
|
+
|
|
14
|
+
const DIR = mkdtempSync(join(tmpdir(), "anet-667-clear-"));
|
|
15
|
+
process.env.COMMHUB_DB = join(DIR, "hub.db");
|
|
16
|
+
process.env.COMMHUB_UPLOADS_DIR = join(DIR, "uploads");
|
|
17
|
+
process.env.HOST = "127.0.0.1";
|
|
18
|
+
|
|
19
|
+
const PW = "ProjectDirWarn123!x";
|
|
20
|
+
const stamp = Date.now();
|
|
21
|
+
const ROOT = "/work";
|
|
22
|
+
const NODE_DIR = "/work/.anet/nodes/demo-node";
|
|
23
|
+
const CONFIG = `${NODE_DIR}/config.json`;
|
|
24
|
+
|
|
25
|
+
let db: { get: (sql: string, ...params: unknown[]) => { task: string | null } | null };
|
|
26
|
+
let hub: { port: number; stop: (close?: boolean) => void } | null = null;
|
|
27
|
+
let BASE = "";
|
|
28
|
+
let NET = "";
|
|
29
|
+
let node: { alias: string; nodeId: string; token: string };
|
|
30
|
+
|
|
31
|
+
async function tool(token: string, name: string, args: Record<string, unknown>) {
|
|
32
|
+
const res = await fetch(`${BASE}/mcp`, {
|
|
33
|
+
method: "POST",
|
|
34
|
+
headers: {
|
|
35
|
+
Authorization: `Bearer ${token}`,
|
|
36
|
+
"Content-Type": "application/json",
|
|
37
|
+
Accept: "application/json, text/event-stream",
|
|
38
|
+
"MCP-Protocol-Version": "2025-03-26",
|
|
39
|
+
},
|
|
40
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name, arguments: args } }),
|
|
41
|
+
});
|
|
42
|
+
const raw = await res.text();
|
|
43
|
+
const data = raw.split("\n").filter((line) => line.startsWith("data:"));
|
|
44
|
+
const payload = data.length ? JSON.parse(data.at(-1)!.slice(5).trim()) : JSON.parse(raw);
|
|
45
|
+
return JSON.parse(payload.result.content[0].text);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function sessionTask(): string | null {
|
|
49
|
+
return db.get("SELECT task FROM sessions WHERE resume_id = ?1", `sdk-${node.nodeId}`)?.task ?? null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
beforeAll(async () => {
|
|
53
|
+
({ db } = await import("./db.js"));
|
|
54
|
+
const { createNetworkTokenForNode, register } = await import("./auth.js");
|
|
55
|
+
const boss = register(`cwd_boss_${stamp}`, PW);
|
|
56
|
+
expect(boss.ok).toBe(true);
|
|
57
|
+
NET = boss.network_id!;
|
|
58
|
+
const alias = `demo-node-${stamp}`;
|
|
59
|
+
const nodeId = `n_cwd_${stamp}`;
|
|
60
|
+
const minted = createNetworkTokenForNode(boss.user!.user_id, NET, alias, nodeId);
|
|
61
|
+
expect(minted.ok).toBe(true);
|
|
62
|
+
node = { alias, nodeId, token: minted.token! };
|
|
63
|
+
const mod = await import("./server.js");
|
|
64
|
+
hub = mod.bootServer({ port: 0, hostname: "127.0.0.1" });
|
|
65
|
+
expect(hub.port).not.toBe(9200);
|
|
66
|
+
expect(hub.port).toBeGreaterThan(0);
|
|
67
|
+
BASE = `http://127.0.0.1:${hub.port}`;
|
|
68
|
+
}, 30_000);
|
|
69
|
+
|
|
70
|
+
afterAll(() => {
|
|
71
|
+
try { hub?.stop(true); } catch { /* already stopped */ }
|
|
72
|
+
try { rmSync(DIR, { recursive: true, force: true }); } catch { /* temp dir */ }
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("#667 restart from the workspace root clears the cwd warning", () => {
|
|
76
|
+
test("wrong directory writes the warning; the right directory sends \"\" and the hub row is empty", async () => {
|
|
77
|
+
const resume = `sdk-${node.nodeId}`;
|
|
78
|
+
const wrong = reportedTask({ configPath: CONFIG, cwd: NODE_DIR, inFlight: 0, status: "idle" });
|
|
79
|
+
expect(wrong).toContain("目录不一致");
|
|
80
|
+
const started = await tool(node.token, "report_status", {
|
|
81
|
+
resume_id: resume, alias: node.alias, status: "idle", node_id: node.nodeId, network_id: NET, task: wrong,
|
|
82
|
+
});
|
|
83
|
+
expect(started.ok).toBe(true);
|
|
84
|
+
expect(sessionTask()).toBe(wrong);
|
|
85
|
+
|
|
86
|
+
const kept = await tool(node.token, "report_status", {
|
|
87
|
+
resume_id: resume, alias: node.alias, status: "idle", node_id: node.nodeId, network_id: NET,
|
|
88
|
+
});
|
|
89
|
+
expect(kept.ok).toBe(true);
|
|
90
|
+
expect(sessionTask()).toBe(wrong);
|
|
91
|
+
|
|
92
|
+
const idle = reportedTask({ configPath: CONFIG, cwd: ROOT, inFlight: 0, status: "idle" });
|
|
93
|
+
expect(idle).toBeUndefined();
|
|
94
|
+
const cleared = idle ?? "";
|
|
95
|
+
expect(cleared).toBe("");
|
|
96
|
+
const fixed = await tool(node.token, "report_status", {
|
|
97
|
+
resume_id: resume, alias: node.alias, status: "idle", node_id: node.nodeId, network_id: NET, task: cleared,
|
|
98
|
+
});
|
|
99
|
+
expect(fixed.ok).toBe(true);
|
|
100
|
+
expect(sessionTask()).toBe("");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("after startup, a finished task's description stays on the hub", async () => {
|
|
104
|
+
const resume = `sdk-${node.nodeId}`;
|
|
105
|
+
const description = "BOARD667-FINISHED-TASK";
|
|
106
|
+
const startup = reportedTask({ configPath: CONFIG, cwd: ROOT, inFlight: 0, status: "idle" }) ?? "";
|
|
107
|
+
expect(startup).toBe("");
|
|
108
|
+
const registered = await tool(node.token, "report_status", {
|
|
109
|
+
resume_id: resume, alias: node.alias, status: "idle", node_id: node.nodeId, network_id: NET, task: startup,
|
|
110
|
+
});
|
|
111
|
+
expect(registered.ok).toBe(true);
|
|
112
|
+
expect(sessionTask()).toBe("");
|
|
113
|
+
|
|
114
|
+
const working = reportedTask({
|
|
115
|
+
configPath: CONFIG, cwd: ROOT, inFlight: 1, status: "working", task: description,
|
|
116
|
+
});
|
|
117
|
+
expect(working).toBe(description);
|
|
118
|
+
const ran = await tool(node.token, "report_status", {
|
|
119
|
+
resume_id: resume, alias: node.alias, status: "working", node_id: node.nodeId, network_id: NET, task: working,
|
|
120
|
+
});
|
|
121
|
+
expect(ran.ok).toBe(true);
|
|
122
|
+
expect(sessionTask()).toBe(description);
|
|
123
|
+
|
|
124
|
+
const finished = reportedTask({ configPath: CONFIG, cwd: ROOT, inFlight: 0, status: "idle" });
|
|
125
|
+
expect(finished).toBeUndefined();
|
|
126
|
+
const done = await tool(node.token, "report_status", {
|
|
127
|
+
resume_id: resume, alias: node.alias, status: "idle", node_id: node.nodeId, network_id: NET,
|
|
128
|
+
});
|
|
129
|
+
expect(done.ok).toBe(true);
|
|
130
|
+
expect(sessionTask()).toBe(description);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// GET /api/requirements/projects 的每行 viewer_can.edit(app 任务页审计 L13)—— HTTP 集成测试(真实 Bun.serve,私有端口,临时库)。
|
|
2
|
+
//
|
|
3
|
+
// 客户端据此只在新建 / 挪卡的项目下拉里列「能编辑」的项目。判据必须与真写卡时一致,所以这里对每个项目
|
|
4
|
+
// 都真建一张卡,断言「viewer_can.edit === 建卡成功」,而不是另抄一份规则来比。
|
|
5
|
+
// admin —— Hub 管理员 + 网络 owner:未归档项目全 true,归档的 false(projectRef 拒归档)
|
|
6
|
+
// alice —— scoped 成员:P1 只看 → false,P2 可改 → true(只列得到这两个)
|
|
7
|
+
// carol —— task_access='all' 的成员:未归档全 true
|
|
8
|
+
// vic —— scoped viewer,P3 只看:false(viewer 本来就不能写卡)
|
|
9
|
+
|
|
10
|
+
import { describe, expect, test, beforeAll, afterAll } from "bun:test";
|
|
11
|
+
import { mkdtempSync, rmSync } from "fs";
|
|
12
|
+
import { join } from "path";
|
|
13
|
+
import { tmpdir } from "os";
|
|
14
|
+
import { register } from "./auth.js";
|
|
15
|
+
import { db } from "./db.js";
|
|
16
|
+
|
|
17
|
+
const DIR = mkdtempSync(join(tmpdir(), "anet-project-viewer-can-"));
|
|
18
|
+
let BASE = "";
|
|
19
|
+
let hub: any = null;
|
|
20
|
+
const PW = "ProjViewerCanPassw0rd!x";
|
|
21
|
+
let NET = "";
|
|
22
|
+
const U: Record<string, { token: string; id: string }> = {};
|
|
23
|
+
const P: Record<string, string> = {};
|
|
24
|
+
|
|
25
|
+
type R = { status: number; body: any };
|
|
26
|
+
async function send(token: string, method: string, path: string, payload?: unknown): Promise<R> {
|
|
27
|
+
const res = await fetch(`${BASE}${path}`, {
|
|
28
|
+
method,
|
|
29
|
+
headers: { ...(token ? { Authorization: `Bearer ${token}` } : {}), "Content-Type": "application/json" },
|
|
30
|
+
body: payload === undefined ? undefined : JSON.stringify(payload),
|
|
31
|
+
});
|
|
32
|
+
const text = await res.text();
|
|
33
|
+
let body: any = null;
|
|
34
|
+
try { body = JSON.parse(text); } catch {}
|
|
35
|
+
return { status: res.status, body };
|
|
36
|
+
}
|
|
37
|
+
const projects = async (who: string) => {
|
|
38
|
+
const r = await send(U[who].token, "GET", `/api/requirements/projects?network_id=${NET}`);
|
|
39
|
+
expect(r.status).toBe(200);
|
|
40
|
+
return Object.fromEntries((r.body.projects as any[]).map((p) => [p.name, p]));
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
beforeAll(async () => {
|
|
44
|
+
process.env.COMMHUB_DB ||= join(DIR, "hub.db");
|
|
45
|
+
process.env.COMMHUB_UPLOADS_DIR = join(DIR, "uploads");
|
|
46
|
+
process.env.HOST = "127.0.0.1";
|
|
47
|
+
const a = register(`pvc_admin_${Date.now()}`, PW, undefined, "Admin");
|
|
48
|
+
expect(a.ok).toBe(true);
|
|
49
|
+
U.admin = { token: a.token!, id: a.user!.user_id };
|
|
50
|
+
NET = a.network_id!;
|
|
51
|
+
db.run("UPDATE users SET role = 'admin' WHERE user_id = ?1", [U.admin.id]);
|
|
52
|
+
const mod: any = await import("./server.js");
|
|
53
|
+
hub = mod.bootServer({ port: 0, hostname: "127.0.0.1" });
|
|
54
|
+
BASE = `http://127.0.0.1:${hub.port}`;
|
|
55
|
+
|
|
56
|
+
const mk = async (username: string, role: string) => {
|
|
57
|
+
expect((await send(U.admin.token, "POST", "/api/admin/users", { username, password: PW, network_id: NET, role })).status).toBe(200);
|
|
58
|
+
const login = await send("", "POST", "/api/auth/login", { username, password: PW });
|
|
59
|
+
expect(login.status).toBe(200);
|
|
60
|
+
return { token: login.body.token as string, id: login.body.user.user_id as string };
|
|
61
|
+
};
|
|
62
|
+
const stamp = Date.now();
|
|
63
|
+
U.alice = await mk(`pvc_alice_${stamp}`, "member");
|
|
64
|
+
U.carol = await mk(`pvc_carol_${stamp}`, "member");
|
|
65
|
+
U.vic = await mk(`pvc_vic_${stamp}`, "viewer");
|
|
66
|
+
db.run("UPDATE network_members SET task_access = 'all' WHERE network_id = ?1 AND user_id = ?2", [NET, U.carol.id]);
|
|
67
|
+
|
|
68
|
+
for (const name of ["pvc-P1", "pvc-P2", "pvc-P3", "pvc-P4"]) {
|
|
69
|
+
const r = await send(U.admin.token, "POST", "/api/requirements/projects", { network_id: NET, name });
|
|
70
|
+
expect(r.status).toBe(201);
|
|
71
|
+
P[name] = r.body.project.id;
|
|
72
|
+
}
|
|
73
|
+
expect((await send(U.admin.token, "PATCH", `/api/requirements/projects/${P["pvc-P4"]}?network_id=${NET}`, { archived: true })).status).toBe(200);
|
|
74
|
+
expect((await send(U.admin.token, "PUT", `/api/networks/${NET}/members/${U.alice.id}/task-grants`, { task_access: "scoped", project_grants: [{ project_id: P["pvc-P1"] }, { project_id: P["pvc-P2"], can_edit: true }] })).status).toBe(200);
|
|
75
|
+
expect((await send(U.admin.token, "PUT", `/api/networks/${NET}/members/${U.vic.id}/task-grants`, { task_access: "scoped", project_grants: [P["pvc-P3"]] })).status).toBe(200);
|
|
76
|
+
}, 30_000);
|
|
77
|
+
|
|
78
|
+
afterAll(() => {
|
|
79
|
+
try { hub?.stop?.(true); } catch {}
|
|
80
|
+
try { rmSync(DIR, { recursive: true, force: true }); } catch {}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe("GET /api/requirements/projects 的 viewer_can.edit", () => {
|
|
84
|
+
test("每个调用者的取值", async () => {
|
|
85
|
+
const edit = (rows: Record<string, any>) => Object.fromEntries(Object.entries(rows).map(([n, p]) => [n, p.viewer_can?.edit]));
|
|
86
|
+
expect(edit(await projects("admin"))).toEqual({ "pvc-P1": true, "pvc-P2": true, "pvc-P3": true, "pvc-P4": false });
|
|
87
|
+
expect(edit(await projects("carol"))).toEqual({ "pvc-P1": true, "pvc-P2": true, "pvc-P3": true, "pvc-P4": false });
|
|
88
|
+
expect(edit(await projects("alice"))).toEqual({ "pvc-P1": false, "pvc-P2": true });
|
|
89
|
+
expect(edit(await projects("vic"))).toEqual({ "pvc-P3": false });
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("只加字段:其余键与之前逐字相同", async () => {
|
|
93
|
+
const row = (await projects("alice"))["pvc-P2"];
|
|
94
|
+
// parentId 是 2026-10-24 加的「子项目」字段(admin:管理项目里看不到层级);
|
|
95
|
+
// 它同样是加法式的:这里钉的是「已知键一个不多一个不少」,新键必须显式加进这个清单。
|
|
96
|
+
expect(Object.keys(row).sort()).toEqual(["archived", "color", "createdAt", "id", "name", "parentId", "sort", "viewer_can"]);
|
|
97
|
+
expect(Object.keys(row.viewer_can)).toEqual(["edit"]);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("与真写卡一致:edit === 往这个项目建卡成功", async () => {
|
|
101
|
+
let checked = 0;
|
|
102
|
+
for (const who of ["admin", "carol", "alice", "vic"]) {
|
|
103
|
+
for (const [name, p] of Object.entries(await projects(who))) {
|
|
104
|
+
const r = await send(U[who].token, "POST", "/api/requirements", { network_id: NET, name: `pvc-card-${who}-${name}`, project_id: p.id });
|
|
105
|
+
expect({ who, name, created: r.status === 201 }).toEqual({ who, name, created: p.viewer_can.edit });
|
|
106
|
+
checked++;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
expect(checked).toBe(11);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("授权改成可编辑后立即反映", async () => {
|
|
113
|
+
expect((await send(U.admin.token, "PUT", `/api/networks/${NET}/members/${U.alice.id}/task-grants`, { project_grants: [{ project_id: P["pvc-P1"], can_edit: true }, { project_id: P["pvc-P2"], can_edit: true }] })).status).toBe(200);
|
|
114
|
+
expect((await projects("alice"))["pvc-P1"].viewer_can.edit).toBe(true);
|
|
115
|
+
});
|
|
116
|
+
});
|
package/src/push.test.ts
CHANGED
|
@@ -351,26 +351,36 @@ describe("real ReadableStream — byte-level backpressure (round-2 fix)", () =>
|
|
|
351
351
|
await res.body!.cancel();
|
|
352
352
|
});
|
|
353
353
|
|
|
354
|
-
test("non-reading consumer
|
|
354
|
+
test("non-reading consumer stops growing at COALESCE_AT and is reaped by the stuck timeout", async () => {
|
|
355
|
+
// #2363 —— 语义变更:落后超过 COALESCE_AT_BYTES 后**跳过非关键事件**,不再一路排队到
|
|
356
|
+
// 1 MB 硬顶。这样 150 条慢订阅者不会把内存放大到 连接数 × 1 MB(实测 2 GB 尖峰)。
|
|
357
|
+
// 兜底仍然存在:硬顶对这一帧的**关键帧**生效;长期不读的由 STUCK_CLOSE_MS 回收。
|
|
358
|
+
const coalesce = __SSE_INTERNALS_FOR_TEST.COALESCE_AT_BYTES;
|
|
355
359
|
const ceiling = __SSE_INTERNALS_FOR_TEST.MAX_QUEUE_BACKPRESSURE_BYTES;
|
|
356
360
|
const res = createSSEStream("byte-cap-probe", "net-bcp");
|
|
357
361
|
const key = "net-bcp:byte-cap-probe";
|
|
358
362
|
const c = __SSE_INTERNALS_FOR_TEST.clientsMap.get(key)![0];
|
|
359
363
|
|
|
360
|
-
|
|
361
|
-
// With chunk-count strategy this would NOT close (each event is 1
|
|
362
|
-
// chunk; desiredSize would be at -N for N events, never -1M).
|
|
363
|
-
// With byte strategy this SHOULD close once accumulated bytes
|
|
364
|
-
// exceed ceiling.
|
|
365
|
-
const chunkSize = 100_000; // 100KB per push
|
|
366
|
-
const needed = Math.ceil(ceiling / chunkSize) + 2; // overshoot a bit
|
|
364
|
+
const chunkSize = 100_000; // 100KB per push, larger than COALESCE_AT
|
|
367
365
|
const big = "x".repeat(chunkSize);
|
|
368
|
-
|
|
366
|
+
const before = __SSE_INTERNALS_FOR_TEST.degradedDropCount();
|
|
367
|
+
for (let i = 0; i < Math.ceil(ceiling / chunkSize) + 2; i++) {
|
|
369
368
|
if (c.closed) break;
|
|
370
369
|
pushEvent("byte-cap-probe", { type: "big", payload: big }, "net-bcp");
|
|
371
370
|
}
|
|
371
|
+
// 关键断言 1:**没有被立刻关闭**(跳过的语义是“不排队”,不是“断开”)
|
|
372
|
+
expect(c.closed).toBe(false);
|
|
373
|
+
// 关键断言 2:队列没有一路涨到硬顶 —— 落后量被 COALESCE_AT 挡住
|
|
374
|
+
const desired = c.controller.desiredSize ?? 0;
|
|
375
|
+
expect(-desired).toBeLessThan(ceiling);
|
|
376
|
+
expect(-desired).toBeLessThanOrEqual(coalesce + chunkSize * 2);
|
|
377
|
+
// 关键断言 3:跳过被**计数**(不静默)
|
|
378
|
+
expect(__SSE_INTERNALS_FOR_TEST.degradedDropCount()).toBeGreaterThan(before);
|
|
379
|
+
|
|
380
|
+
// 关键断言 4:长期不读仍会被收回(沿用原有 stuck 回收)
|
|
381
|
+
c.stuckSince = Date.now() - (__SSE_INTERNALS_FOR_TEST.STUCK_CLOSE_MS + 1);
|
|
382
|
+
__SSE_INTERNALS_FOR_TEST.tryEnqueueBytes(c, new Uint8Array(1), { critical: true });
|
|
372
383
|
expect(c.closed).toBe(true);
|
|
373
|
-
expect(__SSE_INTERNALS_FOR_TEST.clientsMap.has(key)).toBe(false);
|
|
374
384
|
|
|
375
385
|
try { await res.body!.cancel(); } catch { /* already closed */ }
|
|
376
386
|
});
|