@sleep2agi/commhub-server 0.9.0-preview.27 → 0.9.0-preview.270
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +120 -3
- package/bin/commhub.ts +93 -9
- package/build-info.json +7 -0
- package/package.json +10 -8
- package/src/ack-create-request-transport.test.ts +182 -0
- package/src/ack-create-request.test.ts +52 -8
- package/src/admin-delete-network-http.test.ts +229 -0
- package/src/admin-networks-http.test.ts +88 -0
- package/src/adoption-candidates-http.test.ts +140 -0
- package/src/adoption-candidates.ts +158 -0
- package/src/agent-access-batch.test.ts +210 -0
- package/src/agent-access.ts +585 -0
- package/src/agent-acl-groups-http.test.ts +247 -0
- package/src/agent-acl-http.test.ts +691 -0
- package/src/agent-acl-slice3-http.test.ts +885 -0
- package/src/agent-department-grants.test.ts +129 -0
- package/src/agent-department-http.test.ts +215 -0
- package/src/agent-groups-perf.test.ts +223 -0
- package/src/agent-notice.ts +48 -0
- package/src/agent-team-whoami-http.test.ts +98 -0
- package/src/agent-teams-http.test.ts +330 -0
- package/src/agent-teams.ts +244 -0
- package/src/alias-baseline-2571.test.ts +162 -0
- package/src/alias-baseline.ts +113 -0
- package/src/alias-filter.test.ts +72 -0
- package/src/alias-filter.ts +42 -0
- package/src/api-host-supervisors-can-create-nodes.test.ts +350 -0
- package/src/api-host-supervisors-fallback.test.ts +6 -7
- package/src/api-host-supervisors-runtime-readiness.test.ts +195 -0
- package/src/api-nodes-shape.test.ts +131 -4
- package/src/auth-abuse-counters.test.ts +144 -0
- package/src/auth-abuse-counters.ts +122 -0
- package/src/auth-layer-shape.test.ts +105 -0
- package/src/auth-password-change-cli-tokens-http.test.ts +423 -0
- package/src/auth-sessions-http.test.ts +260 -0
- package/src/auth-validate.test.ts +5 -1
- package/src/auth-weak-password-login.test.ts +176 -0
- package/src/auth.ts +641 -68
- package/src/backfill-node-ids-2664.test.ts +128 -0
- package/src/binding-generation-http.test.ts +141 -0
- package/src/blocked-exit-on-reply.test.ts +122 -0
- package/src/broadcast-fanout-2721.test.ts +149 -0
- package/src/broadcast-fanout.ts +117 -0
- package/src/build-commit-report-http.test.ts +128 -0
- package/src/build-info.test.ts +60 -0
- package/src/build-info.ts +85 -0
- package/src/chain-attempt-dedup.test.ts +447 -0
- package/src/clip.test.ts +36 -0
- package/src/clip.ts +48 -0
- package/src/clone-node.test.ts +269 -0
- package/src/clone-node.ts +289 -0
- package/src/codex-config-flags.ts +77 -0
- package/src/codex-fork-contract.ts +23 -0
- package/src/codex-fork-migration.test.ts +38 -0
- package/src/commhub-bin-args.test.ts +85 -0
- package/src/config-apply-validate.test.ts +188 -1
- package/src/config-apply-validate.ts +222 -3
- package/src/cors-origins.test.ts +73 -0
- package/src/create-node-blocked-gate.test.ts +365 -0
- package/src/create-node-error-domain.test.ts +127 -0
- package/src/create-node-network-id.test.ts +91 -0
- package/src/create-node-tool-schema.test.ts +49 -0
- package/src/create-node-unicode-name.test.ts +126 -0
- package/src/create-node-validate.test.ts +199 -7
- package/src/create-node-validate.ts +232 -15
- package/src/create-node-workdir.test.ts +298 -0
- package/src/create-node.ts +88 -23
- package/src/daemon-provider-snapshots.test.ts +453 -0
- package/src/daemon-provider-snapshots.ts +327 -0
- package/src/daemon-token-owner-http.test.ts +346 -0
- package/src/dashboard-slash-routing-http.test.ts +109 -0
- package/src/db-adapter-guard.test.ts +148 -0
- package/src/db-adapter-translate.test.ts +128 -0
- package/src/db-adapter.ts +377 -91
- package/src/db-timestamp.test.ts +38 -0
- package/src/db-timestamp.ts +15 -0
- package/src/db.ts +1298 -153
- package/src/delivered-without-push.test.ts +140 -0
- package/src/department-groups-http.test.ts +209 -0
- package/src/department-groups-sync-http.test.ts +328 -0
- package/src/department-groups.ts +434 -0
- package/src/department-heads-http.test.ts +350 -0
- package/src/department-heads.ts +226 -0
- package/src/departments-agent-count.test.ts +139 -0
- package/src/departments-http.test.ts +185 -0
- package/src/departments-mcp-error-domain.test.ts +135 -0
- package/src/departments-mcp.test.ts +128 -0
- package/src/departments.ts +318 -0
- package/src/desktop-message-seam.test.ts +150 -0
- package/src/desktop-message.test.ts +368 -0
- package/src/dm-file-scope-http.test.ts +227 -0
- package/src/empty-node-sweeper.test.ts +104 -0
- package/src/empty-node-sweeper.ts +88 -0
- package/src/env-transport-gate-http.test.ts +240 -0
- package/src/env-transport-gate.ts +80 -0
- package/src/env-transport-policy.ts +56 -0
- package/src/etag-of.test.ts +57 -0
- package/src/event-loop-lag.test.ts +180 -0
- package/src/event-loop-lag.ts +201 -0
- package/src/expiry-counters.test.ts +187 -0
- package/src/expiry-counters.ts +227 -0
- package/src/expiry-evidence-db.test.ts +103 -0
- package/src/ext-token-shared.test.ts +60 -0
- package/src/ext-validation-regression.test.ts +10 -17
- package/src/external-schedule-edits-http.test.ts +321 -0
- package/src/external-schedule-edits-timestamps.test.ts +64 -0
- package/src/external-schedule-edits.ts +327 -0
- package/src/file-download-authz-dev-open.test.ts +1 -1
- package/src/file-download-authz.test.ts +25 -4
- package/src/file-download-date-bucket.test.ts +1 -1
- package/src/file-network-scope-dev-open.test.ts +1 -1
- package/src/file-network-scope.test.ts +7 -6
- package/src/group-membership-events-http.test.ts +264 -0
- package/src/group-messages-http.test.ts +483 -0
- package/src/group-messages.ts +297 -0
- package/src/health-anon-readers.ts +67 -0
- package/src/health-host-disk-2756.test.ts +71 -0
- package/src/health-identity-root.test.ts +98 -0
- package/src/health-redaction.test.ts +1599 -7
- package/src/health-status-reasons.test.ts +84 -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 +664 -0
- package/src/hub-provider-config.test.ts +166 -0
- package/src/hub-provider-config.ts +202 -0
- package/src/hub-timestamp.test.ts +38 -0
- package/src/hub-timestamp.ts +27 -0
- package/src/human-dm.ts +215 -0
- package/src/human-inbox.ts +42 -0
- package/src/inbox-caller.ts +49 -0
- package/src/inbox-count-sse.test.ts +318 -0
- package/src/inbox-count.ts +29 -0
- package/src/index.ts +6 -2
- package/src/license-state.test.ts +105 -0
- package/src/license-state.ts +48 -0
- package/src/lifecycle-error-domain.test.ts +149 -0
- package/src/list-ack-error-domain.test.ts +108 -0
- package/src/list-host-supervisors.test.ts +15 -4
- package/src/list-tasks-omit-default.test.ts +84 -0
- package/src/list-tasks-since-last.test.ts +100 -0
- package/src/list-tasks-since.test.ts +104 -0
- package/src/mcp-context-cost-http.test.ts +127 -0
- package/src/mcp-inflight.test.ts +92 -0
- package/src/mcp-inflight.ts +38 -0
- package/src/mcp-projects-events-http.test.ts +168 -0
- package/src/mcp-route-label-http.test.ts +120 -0
- package/src/mcp-write-network-resolution.test.ts +21 -0
- package/src/member-audit-network-id-http.test.ts +93 -0
- package/src/member-presence-http.test.ts +210 -0
- package/src/messages-ack-by-agent-http.test.ts +142 -0
- package/src/migrate-sqlite-to-pg.test.ts +47 -0
- package/src/migrate-sqlite-to-pg.ts +263 -0
- package/src/model-auth-notify-http.test.ts +181 -0
- package/src/model-auth-notify.ts +116 -0
- package/src/network-name-http.test.ts +75 -0
- package/src/network-quota-config.test.ts +100 -0
- package/src/network-rules-http.test.ts +169 -0
- package/src/network-rules.ts +129 -0
- package/src/network-scope.ts +222 -3
- package/src/network-token-membership-http.test.ts +186 -0
- package/src/node-alias-control.ts +12 -0
- package/src/node-attrs.test.ts +47 -2
- package/src/node-caller-identity-http.test.ts +1108 -0
- package/src/node-config-last-update.test.ts +126 -0
- package/src/node-config-last-update.ts +107 -0
- package/src/node-create-request-status-http.test.ts +105 -0
- package/src/node-daemon-bindings.test.ts +142 -0
- package/src/node-daemon-bindings.ts +135 -0
- package/src/node-failure-alert.test.ts +150 -0
- package/src/node-failure-alert.ts +177 -0
- package/src/node-files-transport.test.ts +350 -0
- package/src/node-health-codex-login.test.ts +120 -0
- package/src/node-health-dispatch-http.test.ts +373 -0
- package/src/node-health-guard-origin.test.ts +98 -0
- package/src/node-health-guard.ts +121 -0
- package/src/node-health-passthrough.test.ts +115 -0
- package/src/node-health-store.ts +101 -0
- package/src/node-id-alias.test.ts +190 -0
- package/src/node-identity-conflict-http.test.ts +269 -0
- package/src/node-identity-conflict.ts +142 -0
- package/src/node-identity-mismatch.test.ts +74 -0
- package/src/node-in-flight-tasks.ts +31 -0
- package/src/node-lifecycle-controllable.test.ts +64 -0
- package/src/node-lifecycle-controllable.ts +53 -0
- package/src/node-lifecycle-read-http.test.ts +257 -0
- package/src/node-lifecycle-read.ts +124 -0
- package/src/node-logs-transport.test.ts +266 -0
- package/src/node-peer-allowlist.test.ts +114 -0
- package/src/node-permissions-http.test.ts +901 -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-secret-vault-inject.test.ts +132 -0
- package/src/node-security-write-gate.test.ts +72 -0
- package/src/node-serviceability.test.ts +86 -0
- package/src/node-serviceability.ts +124 -0
- package/src/node-session-control-http.test.ts +310 -0
- package/src/node-session-control.ts +258 -0
- package/src/node-skills-transport.test.ts +212 -0
- package/src/node-token-identity.ts +35 -0
- package/src/node-token-ownership.ts +51 -0
- package/src/node-token-privilege-http.test.ts +224 -0
- package/src/not-found-api-http.test.ts +67 -0
- package/src/observer-avatar-http.test.ts +103 -10
- package/src/observer-push.test.ts +76 -0
- package/src/opencode-catalog-report-http.test.ts +139 -0
- package/src/opencode-v2-catalog.test.ts +268 -0
- package/src/opencode-v2-catalog.ts +356 -0
- package/src/package-version-consistency.test.ts +25 -0
- package/src/parent-infer.test.ts +82 -0
- package/src/parent-infer.ts +110 -0
- package/src/parent-source-marker.test.ts +70 -0
- package/src/parent-source-orphan.test.ts +121 -0
- package/src/password-network-tokens.ts +25 -0
- package/src/peer-reply-atomic.test.ts +462 -0
- package/src/peer-reply-capability.test.ts +70 -0
- package/src/pg-worker.ts +87 -0
- package/src/poll-pacing.test.ts +74 -0
- package/src/poll-pacing.ts +123 -0
- package/src/probe-provider-error-domain.test.ts +145 -0
- package/src/project-dir-warn-clear-http.test.ts +132 -0
- package/src/project-viewer-can-http.test.ts +116 -0
- package/src/provider-skill-error-domain.test.ts +130 -0
- package/src/provider-tools-shape.test.ts +165 -0
- package/src/push.test.ts +20 -10
- package/src/push.ts +373 -9
- package/src/redact-tokens.test.ts +86 -0
- package/src/redact-tokens.ts +43 -0
- package/src/rejection-counters.test.ts +72 -0
- package/src/rejection-counters.ts +173 -0
- package/src/rename.ts +38 -10
- package/src/reply-target-derivation.test.ts +96 -0
- package/src/report-status-handover-capabilities-http.test.ts +124 -0
- package/src/report-status-host-schema.test.ts +235 -0
- package/src/report-status-resume-id-handover.test.ts +184 -0
- package/src/report-status-secret-keys.test.ts +140 -0
- package/src/require-explicit-test-db.ts +9 -0
- package/src/requirement-due-reminders-http.test.ts +620 -0
- package/src/requirement-due-reminders.ts +416 -0
- package/src/requirement-events-http.test.ts +318 -0
- package/src/requirement-events.test.ts +45 -0
- package/src/requirement-events.ts +144 -0
- package/src/requirement-notify.ts +165 -0
- package/src/requirement-participant-notify-http.test.ts +313 -0
- package/src/requirement-tag-ops-http.test.ts +454 -0
- package/src/requirement-tag-tree-http.test.ts +275 -0
- package/src/requirement-tag-tree.ts +127 -0
- package/src/requirement-tags.ts +102 -0
- package/src/requirements-abandoned-http.test.ts +236 -0
- package/src/requirements-assignee-drift.test.ts +154 -0
- package/src/requirements-comment-http.test.ts +174 -0
- package/src/requirements-completed-at-migration.test.ts +70 -0
- package/src/requirements-errors-mcp-http.test.ts +164 -0
- package/src/requirements-errors.ts +158 -0
- package/src/requirements-etag-http.test.ts +91 -0
- package/src/requirements-http.test.ts +709 -0
- package/src/requirements-last-event-http.test.ts +349 -0
- package/src/requirements-list-mcp-slim-http.test.ts +155 -0
- package/src/requirements-list-slim-http.test.ts +324 -0
- package/src/requirements-mcp.test.ts +180 -0
- package/src/requirements-migrate.ts +283 -0
- package/src/requirements-owner-strict-mcp-http.test.ts +145 -0
- package/src/requirements-people-http.test.ts +114 -0
- package/src/requirements-people-mcp-http.test.ts +231 -0
- package/src/requirements-people.ts +196 -0
- package/src/requirements-priority-lowest.test.ts +114 -0
- package/src/requirements-projects-network-echo.test.ts +57 -0
- package/src/requirements-projects-parent-http.test.ts +123 -0
- package/src/requirements-search-http.test.ts +182 -0
- package/src/requirements-search.test.ts +32 -0
- package/src/requirements-search.ts +145 -0
- package/src/requirements-seq-http.test.ts +193 -0
- package/src/requirements-seq-migration.test.ts +63 -0
- package/src/requirements-start-date.test.ts +125 -0
- package/src/requirements-stats-http.test.ts +357 -0
- package/src/requirements-stats.ts +195 -0
- package/src/requirements-status-participants-http.test.ts +222 -0
- package/src/requirements-upgrade-e2e.test.ts +126 -0
- package/src/requirements-upgrade-migration.test.ts +98 -0
- package/src/requirements.ts +2205 -0
- package/src/resolve-port.test.ts +57 -0
- package/src/resolve-port.ts +44 -0
- package/src/rest-alias-network-fallback-http.test.ts +123 -0
- package/src/rest-alias-network-fallback.test.ts +60 -0
- package/src/rest-alias-network-fallback.ts +65 -0
- package/src/rest-explicit-columns-http.test.ts +266 -0
- package/src/rest-identity.test.ts +130 -0
- package/src/rest-identity.ts +141 -0
- package/src/rest-projections.ts +72 -0
- package/src/rest-read-network-real-chain.test.ts +97 -0
- package/src/rest-write-network-resolution.test.ts +133 -0
- package/src/restart-readiness-http.test.ts +121 -0
- package/src/restricted-files.ts +76 -0
- package/src/retention-first-sweep-quiet.test.ts +31 -0
- package/src/retention.test.ts +100 -40
- package/src/retention.ts +423 -104
- package/src/route-callers-http.test.ts +120 -0
- package/src/route-timing.test.ts +218 -0
- package/src/route-timing.ts +262 -0
- package/src/rules-file-duplicate-alias.test.ts +234 -0
- package/src/rules-file-session-target.test.ts +207 -0
- package/src/rules-file-transport.test.ts +271 -0
- package/src/runtime-label.test.ts +22 -0
- package/src/runtime-label.ts +21 -0
- package/src/runtime-readiness.ts +90 -0
- package/src/schedule-agent-mcp-http.test.ts +486 -0
- package/src/schedule-agent.ts +252 -0
- package/src/schedule-starved.test.ts +92 -0
- package/src/schedule-starved.ts +114 -0
- package/src/scheduled-backoff-http.test.ts +203 -0
- package/src/scheduled-backoff.ts +64 -0
- package/src/scheduled-doorbell-nodeless-session.test.ts +134 -0
- package/src/scheduled-failure-backoff-2622.test.ts +145 -0
- package/src/scheduled-failure-code.test.ts +73 -0
- package/src/scheduled-failure-code.ts +105 -0
- package/src/scheduled-failures-http.test.ts +279 -0
- package/src/scheduled-failures.ts +425 -0
- package/src/scheduled-noprogress-backoff-2622.test.ts +163 -0
- package/src/scheduled-offline-backoff-probe.test.ts +56 -0
- package/src/scheduled-offline.test.ts +131 -0
- package/src/scheduled-offline.ts +66 -0
- package/src/scheduled-outcome-guidance-http.test.ts +139 -0
- package/src/scheduled-outcome-guidance.ts +9 -0
- package/src/scheduled-reply-unread-http.test.ts +212 -0
- package/src/scheduled-requirement-wake-http.test.ts +289 -0
- package/src/scheduled-requirement-wake.ts +125 -0
- package/src/scheduled-run-terminal.test.ts +290 -0
- package/src/scheduled-skip-blocking-task.test.ts +120 -0
- package/src/scheduled-skip-path-backoff-2622.test.ts +98 -0
- package/src/scheduled-stuck-http.test.ts +316 -0
- package/src/scheduled-stuck-notice-cap.test.ts +80 -0
- package/src/scheduled-stuck.ts +219 -0
- package/src/scheduled-tasks-http.test.ts +679 -0
- package/src/scheduled-tasks.ts +959 -0
- package/src/scheduled-work-outcome-http.test.ts +154 -0
- package/src/scheduled-work-outcome.ts +50 -0
- package/src/scoped-secrets.ts +167 -0
- package/src/secret-echo-negative-control.test.ts +118 -0
- package/src/send-dedup-request-id.test.ts +229 -0
- package/src/send-reply-agent-warning.test.ts +38 -38
- package/src/send-reply-alias-optional.test.ts +131 -0
- package/src/send-reply-attachments.test.ts +28 -7
- package/src/send_dedup.test.ts +54 -0
- package/src/send_dedup.ts +52 -6
- package/src/serve-error.test.ts +73 -0
- package/src/serve-error.ts +56 -0
- package/src/server.ts +4930 -377
- package/src/session-task-on-dispatch.ts +14 -0
- package/src/session-token-ownership-http.test.ts +122 -0
- package/src/shared/codex-inventory.ts +41 -0
- package/src/shared/daemon-provider-login.ts +31 -0
- package/src/shared/daemon-provider-snapshot.ts +52 -0
- package/src/shared/external-schedule-contract.ts +60 -0
- package/src/shared/node-name.test.ts +62 -0
- package/src/shared/node-name.ts +161 -0
- package/src/shared/probe-host-allowlist-drift.test.ts +45 -0
- package/src/shared/provider-application.ts +29 -0
- package/src/shared/provider-collection.ts +14 -0
- package/src/shared/provider-initialization.ts +8 -0
- package/src/shared/reserved-env-drift.test.ts +10 -0
- package/src/shared/scoped-secret-contract.ts +5 -0
- package/src/shared/task-content-limit-drift.test.ts +28 -0
- package/src/shared/task-content-limit.ts +25 -0
- package/src/side-thread-command-transport.test.ts +312 -0
- package/src/side-thread-command-transport.ts +318 -0
- package/src/side-thread-http-integration.test.ts +233 -0
- package/src/side-thread-http.ts +504 -0
- package/src/side-thread-production.test.ts +72 -0
- package/src/side-thread-production.ts +118 -0
- package/src/side-thread.test.ts +1067 -0
- package/src/side-thread.ts +2499 -0
- package/src/skillhub-http.test.ts +144 -0
- package/src/sqlite-close-checkpoint.test.ts +78 -0
- package/src/start-node.test.ts +265 -0
- package/src/start-request-error-domain.test.ts +182 -0
- package/src/start-skill-error-domain.test.ts +122 -0
- package/src/stats-versions-http.test.ts +65 -0
- package/src/status-alias-filter-http.test.ts +106 -0
- package/src/status-alias-resolver-http.test.ts +163 -0
- package/src/status-alias-resolver.ts +19 -0
- package/src/status-node-id-filter-http.test.ts +161 -0
- package/src/status-read-cache-http.test.ts +229 -0
- package/src/status-read-cache.ts +129 -0
- package/src/stop-delete-node.test.ts +567 -1
- package/src/task-access-http.test.ts +407 -0
- package/src/task-access.ts +420 -0
- package/src/task-activity.ts +60 -0
- package/src/task-auth-origin-http.test.ts +93 -0
- package/src/task-auth-origin.test.ts +24 -0
- package/src/task-auth-origin.ts +7 -0
- package/src/task-consumption.test.ts +508 -0
- package/src/task-diagnostic-http.test.ts +149 -0
- package/src/task-diagnostic.test.ts +115 -0
- package/src/task-diagnostic.ts +118 -0
- package/src/task-expiry-notice-http.test.ts +298 -0
- package/src/task-expiry-notice.ts +318 -0
- package/src/task-idempotency-mcp.test.ts +148 -0
- package/src/task-idempotency.test.ts +76 -0
- package/src/task-idempotency.ts +81 -0
- package/src/task-late-replies.ts +54 -0
- package/src/task-late-reply-http.test.ts +227 -0
- package/src/task-lifecycle-watcher-busy.test.ts +114 -0
- package/src/task-lifecycle-watcher.test.ts +209 -0
- package/src/task-lifecycle-watcher.ts +109 -0
- package/src/task-network-resolution-http.test.ts +140 -0
- package/src/task-not-found-error-domain.test.ts +114 -0
- package/src/task-offline-delivery-2668.test.ts +119 -0
- package/src/task-offline-delivery.ts +81 -0
- package/src/task-orphan-http.test.ts +287 -0
- package/src/task-orphan.ts +119 -0
- package/src/task-queue-ahead-http.test.ts +309 -0
- package/src/task-queue-ahead-scale.test.ts +236 -0
- package/src/task-queue-ahead.ts +186 -0
- package/src/task-rejection-log-http.test.ts +160 -0
- package/src/task-rejection-log.ts +61 -0
- package/src/task-sender-http.test.ts +143 -0
- package/src/task-stale-open-http.test.ts +292 -0
- package/src/task-stale-open.ts +103 -0
- package/src/tasks-pagination-http.test.ts +141 -0
- package/src/team-rules-status-http.test.ts +90 -0
- package/src/team-rules-status.test.ts +59 -0
- package/src/team-rules-status.ts +72 -0
- package/src/tmux-allowlist.test.ts +48 -0
- package/src/tool-audience-http.test.ts +248 -0
- package/src/tool-audience.ts +127 -0
- package/src/tools-list-json-schema.test.ts +94 -0
- package/src/tools.ts +4314 -392
- package/src/update-node-config-error-domain.test.ts +107 -0
- package/src/uploads-http.test.ts +6 -4
- package/src/uploads.ts +23 -4
- package/src/user-avatar-http.test.ts +74 -0
- package/src/user-avatar-image.ts +160 -0
- package/src/user-avatar-migration.test.ts +25 -0
- package/src/user-avatar-upload-http.test.ts +353 -0
- package/src/user-inbox-by-agent-http.test.ts +189 -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,123 @@
|
|
|
1
|
+
// 子项目(admin 2026-10-24:管理项目里只能平铺,看不到层级)。
|
|
2
|
+
//
|
|
3
|
+
// 本文件钉住四件事,任何一条错都会让「项目」这个词变得不可信:
|
|
4
|
+
// 1. 建/搬家都能指定 parent_id,GET 列表能读回;
|
|
5
|
+
// 2. 深度上限 3 层(顶层=1),超了拒绝而不是悄悄截断;
|
|
6
|
+
// 3. 环(把自己挂到自己的后代下面 / 挂到自己身上)必须拒绝;
|
|
7
|
+
// 4. 有子项目的项目不能直接删(不偷偷把子项目变孤儿或改写它们的层级)。
|
|
8
|
+
// 另外:父项目必须同网络、未归档。
|
|
9
|
+
import { describe, expect, test, beforeAll, afterAll } from "bun:test";
|
|
10
|
+
import "./require-explicit-test-db.js";
|
|
11
|
+
import { register } from "./auth.js";
|
|
12
|
+
import { bootServer } from "./server.js";
|
|
13
|
+
|
|
14
|
+
let BASE = "";
|
|
15
|
+
let server: any = null;
|
|
16
|
+
let token = "";
|
|
17
|
+
let NET = "";
|
|
18
|
+
|
|
19
|
+
async function send(method: string, path: string, payload?: unknown) {
|
|
20
|
+
const res = await fetch(`${BASE}${path}`, {
|
|
21
|
+
method,
|
|
22
|
+
headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" },
|
|
23
|
+
body: payload === undefined ? undefined : JSON.stringify(payload),
|
|
24
|
+
});
|
|
25
|
+
const text = await res.text();
|
|
26
|
+
let body: any = null;
|
|
27
|
+
try { body = JSON.parse(text); } catch {}
|
|
28
|
+
return { status: res.status, body };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const P = (path: string) => `${path}${path.includes("?") ? "&" : "?"}network_id=${NET}`;
|
|
32
|
+
const mk = (name: string, parent_id?: string | null) =>
|
|
33
|
+
send("POST", P("/api/requirements/projects"), parent_id === undefined ? { name } : { name, parent_id });
|
|
34
|
+
|
|
35
|
+
beforeAll(async () => {
|
|
36
|
+
const suffix = `${Date.now()}_${Math.floor(Math.random() * 1000)}`;
|
|
37
|
+
const a = register(`projparent_${suffix}`, "ProjParentPw123!", undefined, "pp");
|
|
38
|
+
expect(a.ok).toBe(true);
|
|
39
|
+
token = a.token!;
|
|
40
|
+
NET = a.network_id!;
|
|
41
|
+
server = bootServer({ port: 0, hostname: "127.0.0.1" });
|
|
42
|
+
BASE = `http://127.0.0.1:${server.port}`;
|
|
43
|
+
}, 30_000);
|
|
44
|
+
|
|
45
|
+
afterAll(() => { try { server?.stop?.(true); } catch {} });
|
|
46
|
+
|
|
47
|
+
describe("sub-projects (parent_id)", () => {
|
|
48
|
+
test("create a child and read the hierarchy back", async () => {
|
|
49
|
+
const parent = await mk("父项目");
|
|
50
|
+
expect(parent.status).toBe(201);
|
|
51
|
+
const parentId = parent.body.project.id;
|
|
52
|
+
expect(parent.body.project.parentId).toBeNull();
|
|
53
|
+
|
|
54
|
+
const child = await mk("子项目", parentId);
|
|
55
|
+
expect(child.status).toBe(201);
|
|
56
|
+
expect(child.body.project.parentId).toBe(parentId);
|
|
57
|
+
|
|
58
|
+
const list = await send("GET", P("/api/requirements/projects"));
|
|
59
|
+
expect(list.status).toBe(200);
|
|
60
|
+
const rows = list.body.projects as any[];
|
|
61
|
+
expect(rows.find((r) => r.name === "子项目")!.parentId).toBe(parentId);
|
|
62
|
+
expect(rows.find((r) => r.name === "父项目")!.parentId).toBeNull();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("a project can be moved under another one, and back to the top", async () => {
|
|
66
|
+
const a = await mk("可搬 A");
|
|
67
|
+
const b = await mk("可搬 B");
|
|
68
|
+
const moved = await send("PATCH", P(`/api/requirements/projects/${b.body.project.id}`), { parent_id: a.body.project.id });
|
|
69
|
+
expect(moved.status).toBe(200);
|
|
70
|
+
expect(moved.body.project.parentId).toBe(a.body.project.id);
|
|
71
|
+
|
|
72
|
+
const back = await send("PATCH", P(`/api/requirements/projects/${b.body.project.id}`), { parent_id: null });
|
|
73
|
+
expect(back.status).toBe(200);
|
|
74
|
+
expect(back.body.project.parentId).toBeNull();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("depth is capped at 3 (top level counts as 1)", async () => {
|
|
78
|
+
const l1 = await mk("深 1");
|
|
79
|
+
const l2 = await mk("深 2", l1.body.project.id);
|
|
80
|
+
const l3 = await mk("深 3", l2.body.project.id);
|
|
81
|
+
expect(l3.status).toBe(201);
|
|
82
|
+
const l4 = await mk("深 4", l3.body.project.id);
|
|
83
|
+
expect(l4.status).toBe(400);
|
|
84
|
+
expect(l4.body.error).toBe("project_parent_too_deep");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("a cycle is refused (self, and a descendant)", async () => {
|
|
88
|
+
const a = await mk("环 A");
|
|
89
|
+
const b = await mk("环 B", a.body.project.id);
|
|
90
|
+
const self = await send("PATCH", P(`/api/requirements/projects/${a.body.project.id}`), { parent_id: a.body.project.id });
|
|
91
|
+
expect(self.status).toBe(400);
|
|
92
|
+
expect(self.body.error).toBe("project_parent_cycle");
|
|
93
|
+
|
|
94
|
+
// 把 A 挂到它自己的子项目 B 下面 = 环
|
|
95
|
+
const loop = await send("PATCH", P(`/api/requirements/projects/${a.body.project.id}`), { parent_id: b.body.project.id });
|
|
96
|
+
expect(loop.status).toBe(400);
|
|
97
|
+
expect(loop.body.error).toBe("project_parent_cycle");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("a parent that does not exist, is archived, or lives in another network is refused", async () => {
|
|
101
|
+
const missing = await mk("缺父", "proj_does_not_exist");
|
|
102
|
+
expect(missing.status).toBe(404);
|
|
103
|
+
expect(missing.body.error).toBe("project_parent_not_found");
|
|
104
|
+
|
|
105
|
+
const parent = await mk("会归档的父");
|
|
106
|
+
await send("PATCH", P(`/api/requirements/projects/${parent.body.project.id}`), { archived: true });
|
|
107
|
+
const archived = await mk("挂到归档父", parent.body.project.id);
|
|
108
|
+
expect(archived.status).toBe(400);
|
|
109
|
+
expect(archived.body.error).toBe("project_parent_archived");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("a project with children cannot be deleted (children are not silently orphaned)", async () => {
|
|
113
|
+
const parent = await mk("有孩子的父");
|
|
114
|
+
const child = await mk("孩子", parent.body.project.id);
|
|
115
|
+
const del = await send("DELETE", P(`/api/requirements/projects/${parent.body.project.id}`));
|
|
116
|
+
expect(del.status).toBe(409);
|
|
117
|
+
expect(del.body.error).toBe("project_has_children");
|
|
118
|
+
|
|
119
|
+
// 先删孩子,父就能删
|
|
120
|
+
expect((await send("DELETE", P(`/api/requirements/projects/${child.body.project.id}`))).status).toBe(200);
|
|
121
|
+
expect((await send("DELETE", P(`/api/requirements/projects/${parent.body.project.id}`))).status).toBe(200);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// GET /api/requirements 的 q= 服务端搜索与 limit / cursor 分页(requirements-search.ts)。一次性 hub:临时 DB、端口 0。
|
|
2
|
+
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
3
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
|
|
7
|
+
const dir = mkdtempSync(join(tmpdir(), "anet-req-search-"));
|
|
8
|
+
process.env.COMMHUB_DB = join(dir, "hub.db");
|
|
9
|
+
|
|
10
|
+
let server: { port: number; stop?: (force?: boolean) => void };
|
|
11
|
+
let base = "";
|
|
12
|
+
let token = "";
|
|
13
|
+
let otherToken = "";
|
|
14
|
+
let net = "";
|
|
15
|
+
let db: any;
|
|
16
|
+
|
|
17
|
+
async function api(tok: string, path: string, init?: RequestInit) {
|
|
18
|
+
const res = await fetch(`${base}${path}`, { ...init, headers: { Authorization: `Bearer ${tok}`, "Content-Type": "application/json", ...(init?.headers || {}) } });
|
|
19
|
+
return { status: res.status, body: await res.json() as any };
|
|
20
|
+
}
|
|
21
|
+
const list = (query: string, tok = token) => api(tok, `/api/requirements?network_id=${net}${query ? `&${query}` : ""}`);
|
|
22
|
+
const ids = async (query: string, tok = token) => {
|
|
23
|
+
const r = await list(query, tok);
|
|
24
|
+
expect(r.status).toBe(200);
|
|
25
|
+
return (r.body.requirements as any[]).map(x => x.name);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
beforeAll(async () => {
|
|
29
|
+
({ db } = await import("./db.js"));
|
|
30
|
+
const { register } = await import("./auth.js");
|
|
31
|
+
const owner = register(`search_owner_${Date.now()}`, "SearchOwner123!", undefined, "seed");
|
|
32
|
+
token = owner.token!;
|
|
33
|
+
net = owner.network_id!;
|
|
34
|
+
const other = register(`search_other_${Date.now()}`, "SearchOther123!", undefined, "seed");
|
|
35
|
+
otherToken = other.token!;
|
|
36
|
+
const mod: any = await import("./server.js");
|
|
37
|
+
server = mod.bootServer({ port: 0, hostname: "127.0.0.1" });
|
|
38
|
+
base = `http://127.0.0.1:${server.port}`;
|
|
39
|
+
db.run("INSERT INTO nodes(node_id,node_name,alias,display_name,network_id) VALUES (?1,?2,?2,?3,?4)", ["n_search_a", "portal-cow", "示例-门户牛", net]);
|
|
40
|
+
const project = await api(token, "/api/requirements/projects", { method: "POST", body: JSON.stringify({ name: "示例门户项目", network_id: net }) });
|
|
41
|
+
const pid = project.body.project.id;
|
|
42
|
+
const mk = (name: string, extra: Record<string, unknown> = {}) => api(token, "/api/requirements", { method: "POST", body: JSON.stringify({ name, network_id: net, ...extra }) });
|
|
43
|
+
await mk("企业组织树权限设置");
|
|
44
|
+
await mk("Portal Token 管理 | 估算成本卡布局", { agent_owner: { kind: "node", id: "n_search_a" }, tags: ["前端"] });
|
|
45
|
+
await mk("Space 网络策略 | 历史 receipt 误报", { description: "看  和 [链接文字](https://example.com/x),重启导致 binding 丢失" });
|
|
46
|
+
await mk("子项目的卡", { project_id: pid });
|
|
47
|
+
const arch = await mk("已归档的组织树旧方案");
|
|
48
|
+
await api(token, `/api/requirements/${arch.body.requirement.id}`, { method: "PATCH", body: JSON.stringify({ archived: true }) });
|
|
49
|
+
await api(otherToken, "/api/requirements", { method: "POST", body: JSON.stringify({ name: "别的网络的组织树" }) });
|
|
50
|
+
}, 30_000);
|
|
51
|
+
|
|
52
|
+
afterAll(() => {
|
|
53
|
+
try { server?.stop?.(true); } catch {}
|
|
54
|
+
try { rmSync(dir, { recursive: true, force: true }); } catch {}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe("q= server-side search (same semantics as the app's task search)", () => {
|
|
58
|
+
test("title substring, CJK, case-insensitive, full-width input", async () => {
|
|
59
|
+
expect(await ids("q=" + encodeURIComponent("组织树"))).toEqual(["企业组织树权限设置"]);
|
|
60
|
+
expect(await ids("q=portal")).toEqual(["Portal Token 管理 | 估算成本卡布局"]);
|
|
61
|
+
expect(await ids("q=" + encodeURIComponent("PORTAL"))).toEqual(["Portal Token 管理 | 估算成本卡布局"]);
|
|
62
|
+
});
|
|
63
|
+
test("agent owner display name, tags, project name, description text (not URLs)", async () => {
|
|
64
|
+
expect(await ids("q=" + encodeURIComponent("门户牛"))).toEqual(["Portal Token 管理 | 估算成本卡布局"]);
|
|
65
|
+
expect(await ids("q=" + encodeURIComponent("前端"))).toEqual(["Portal Token 管理 | 估算成本卡布局"]);
|
|
66
|
+
expect(await ids("q=" + encodeURIComponent("示例门户项目"))).toEqual(["子项目的卡"]);
|
|
67
|
+
expect(await ids("q=binding")).toEqual(["Space 网络策略 | 历史 receipt 误报"]);
|
|
68
|
+
expect(await ids("q=" + encodeURIComponent("链接文字"))).toEqual(["Space 网络策略 | 历史 receipt 误报"]);
|
|
69
|
+
expect(await ids("q=abc123")).toEqual([]);
|
|
70
|
+
expect(await ids("q=example.com")).toEqual([]);
|
|
71
|
+
});
|
|
72
|
+
test("space-separated terms are ANDed across fields", async () => {
|
|
73
|
+
expect(await ids("q=" + encodeURIComponent("成本 门户牛"))).toEqual(["Portal Token 管理 | 估算成本卡布局"]);
|
|
74
|
+
expect(await ids("q=" + encodeURIComponent("成本 receipt"))).toEqual([]);
|
|
75
|
+
});
|
|
76
|
+
test("archived only with archived=true / include_archived=1; other networks never", async () => {
|
|
77
|
+
expect(await ids("q=" + encodeURIComponent("组织树"))).toEqual(["企业组织树权限设置"]);
|
|
78
|
+
expect(await ids("archived=true&q=" + encodeURIComponent("组织树"))).toEqual(["已归档的组织树旧方案"]);
|
|
79
|
+
expect((await ids("include_archived=1&q=" + encodeURIComponent("组织树"))).sort()).toEqual(["企业组织树权限设置", "已归档的组织树旧方案"].sort());
|
|
80
|
+
});
|
|
81
|
+
test("combines with the other filters", async () => {
|
|
82
|
+
expect(await ids("status=done&q=portal")).toEqual([]);
|
|
83
|
+
expect(await ids("agent_owner=node:n_search_a&q=" + encodeURIComponent("成本"))).toEqual(["Portal Token 管理 | 估算成本卡布局"]);
|
|
84
|
+
});
|
|
85
|
+
test("empty / whitespace q = no search; too long q = 400", async () => {
|
|
86
|
+
expect((await ids("q=")).length).toBe(4);
|
|
87
|
+
expect((await ids("q=%20%20")).length).toBe(4);
|
|
88
|
+
const r = await list("q=" + "x".repeat(201));
|
|
89
|
+
expect(r.status).toBe(400);
|
|
90
|
+
expect(r.body.error).toBe("invalid_q");
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe("q= also matches task ids: #N / N on seq (#2139), full id and 8+ char prefix", () => {
|
|
95
|
+
const idOf = async (name: string) => (await list("limit=1000&include_archived=1")).body.requirements.find((r: any) => r.name === name).id as string;
|
|
96
|
+
test("full id and id prefix (with or without req_), not shorter prefixes", async () => {
|
|
97
|
+
const id = await idOf("企业组织树权限设置");
|
|
98
|
+
const bare = id.replace(/^req_/, "");
|
|
99
|
+
expect(await ids("q=" + encodeURIComponent(id))).toEqual(["企业组织树权限设置"]);
|
|
100
|
+
expect(await ids("q=" + encodeURIComponent(id.toUpperCase()))).toEqual(["企业组织树权限设置"]);
|
|
101
|
+
expect(await ids("q=" + encodeURIComponent(bare.slice(0, 10)))).toEqual(["企业组织树权限设置"]);
|
|
102
|
+
expect(await ids("q=" + encodeURIComponent(`req_${bare.slice(0, 9)}`))).toEqual(["企业组织树权限设置"]);
|
|
103
|
+
// 7 位以下的前缀不算 ID(否则输入几个字母就搜出一堆)
|
|
104
|
+
expect(await ids("q=" + encodeURIComponent(`req_${bare.slice(0, 5)}`))).toEqual([]);
|
|
105
|
+
});
|
|
106
|
+
test("#N / full-width #N / padded #N match the task's seq (#2139); #N plus text is plain text; filters still apply", async () => {
|
|
107
|
+
const target = await idOf("Space 网络策略 | 历史 receipt 误报");
|
|
108
|
+
const n = db.get("SELECT seq FROM requirements WHERE requirement_id = ?1", target).seq as number;
|
|
109
|
+
expect(typeof n).toBe("number");
|
|
110
|
+
expect(await ids(`q=%23${n}`)).toEqual(["Space 网络策略 | 历史 receipt 误报"]);
|
|
111
|
+
const fullWidth = `#${String(n).replace(/\d/g, d => String.fromCharCode(0xff10 + Number(d)))}`;
|
|
112
|
+
expect(await ids("q=" + encodeURIComponent(fullWidth))).toEqual(["Space 网络策略 | 历史 receipt 误报"]);
|
|
113
|
+
expect(await ids("q=" + encodeURIComponent(` #${n} `))).toEqual(["Space 网络策略 | 历史 receipt 误报"]);
|
|
114
|
+
// 「#N receipt」整句不是 ID,只按文字「且」:没有卡同时含「#N」和「receipt」
|
|
115
|
+
expect(await ids("q=" + encodeURIComponent(`#${n} receipt`))).toEqual([]);
|
|
116
|
+
// ID 命中也受筛选管
|
|
117
|
+
expect(await ids(`status=done&q=%23${n}`)).toEqual([]);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe("paging: limit / cursor / has_more / next_cursor", () => {
|
|
122
|
+
test("no params: legacy shape plus has_more=false, next_cursor=null, capabilities advertise search + paging", async () => {
|
|
123
|
+
const r = await list("");
|
|
124
|
+
expect(r.body.requirements.length).toBe(4);
|
|
125
|
+
expect(r.body.has_more).toBe(false);
|
|
126
|
+
expect(r.body.next_cursor).toBeNull();
|
|
127
|
+
expect(r.body.capabilities).toContain("search");
|
|
128
|
+
expect(r.body.capabilities).toContain("paging");
|
|
129
|
+
});
|
|
130
|
+
test("walking pages returns every row exactly once, newest first, including same-timestamp ties", async () => {
|
|
131
|
+
// 25 张,其中 10 张同一个 created_at(翻页边界落在并列上也不能漏 / 重)
|
|
132
|
+
const same = new Date(Date.now() + 60_000).toISOString();
|
|
133
|
+
for (let i = 0; i < 25; i++) {
|
|
134
|
+
const c = await api(token, "/api/requirements", { method: "POST", body: JSON.stringify({ name: `页-${String(i).padStart(2, "0")}`, network_id: net }) });
|
|
135
|
+
if (i >= 10 && i < 20) db.run("UPDATE requirements SET created_at = ?1 WHERE requirement_id = ?2", [same, c.body.requirement.id]);
|
|
136
|
+
}
|
|
137
|
+
const full = await ids("limit=1000");
|
|
138
|
+
expect(full.length).toBe(29);
|
|
139
|
+
const seen: string[] = [];
|
|
140
|
+
let cursor = "";
|
|
141
|
+
let pages = 0;
|
|
142
|
+
for (;;) {
|
|
143
|
+
const r = await list(`limit=7${cursor ? `&cursor=${cursor}` : ""}`);
|
|
144
|
+
expect(r.status).toBe(200);
|
|
145
|
+
seen.push(...r.body.requirements.map((x: any) => x.name));
|
|
146
|
+
pages++;
|
|
147
|
+
if (!r.body.has_more) { expect(r.body.next_cursor).toBeNull(); break; }
|
|
148
|
+
expect(typeof r.body.next_cursor).toBe("string");
|
|
149
|
+
cursor = r.body.next_cursor;
|
|
150
|
+
expect(pages).toBeLessThan(10);
|
|
151
|
+
}
|
|
152
|
+
expect(pages).toBe(5);
|
|
153
|
+
expect(seen).toEqual(full);
|
|
154
|
+
expect(new Set(seen).size).toBe(29);
|
|
155
|
+
});
|
|
156
|
+
test("q= pages too", async () => {
|
|
157
|
+
const first = await list("limit=4&q=" + encodeURIComponent("页-"));
|
|
158
|
+
expect(first.body.requirements.length).toBe(4);
|
|
159
|
+
expect(first.body.has_more).toBe(true);
|
|
160
|
+
const rest = await list(`limit=100&q=${encodeURIComponent("页-")}&cursor=${first.body.next_cursor}`);
|
|
161
|
+
expect(rest.body.requirements.length).toBe(21);
|
|
162
|
+
expect(rest.body.has_more).toBe(false);
|
|
163
|
+
});
|
|
164
|
+
test("default limit stays 500 (legacy clients see the same cap)", async () => {
|
|
165
|
+
const { DEFAULT_LIST_LIMIT } = await import("./requirements-search.js");
|
|
166
|
+
expect(DEFAULT_LIST_LIMIT).toBe(500);
|
|
167
|
+
});
|
|
168
|
+
test("bad limit / cursor → 400", async () => {
|
|
169
|
+
for (const q of ["limit=0", "limit=1001", "limit=abc", "limit=-1", "cursor=%%%", "cursor=bm9wZQ"]) {
|
|
170
|
+
const r = await list(q);
|
|
171
|
+
expect(r.status).toBe(400);
|
|
172
|
+
expect(["invalid_limit", "invalid_cursor"]).toContain(r.body.error);
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
test("ETag still works with the new fields", async () => {
|
|
176
|
+
const first = await fetch(`${base}/api/requirements?network_id=${net}&limit=5`, { headers: { Authorization: `Bearer ${token}` } });
|
|
177
|
+
const etag = first.headers.get("etag")!;
|
|
178
|
+
await first.text();
|
|
179
|
+
const again = await fetch(`${base}/api/requirements?network_id=${net}&limit=5`, { headers: { Authorization: `Bearer ${token}`, "If-None-Match": etag } });
|
|
180
|
+
expect(again.status).toBe(304);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// requirements-search.ts 的纯函数:任务 ID 匹配(与 App 的 task-short-id.ts matchesTaskId 同一规则)。
|
|
2
|
+
import { describe, expect, test } from "bun:test";
|
|
3
|
+
import { matchesTaskId, parseListQuery, taskIdQuery } from "./requirements-search.js";
|
|
4
|
+
|
|
5
|
+
const row = (id: string, seq: number | null) => ({ requirement_id: id, seq });
|
|
6
|
+
|
|
7
|
+
describe("matchesTaskId", () => {
|
|
8
|
+
test("#N / N match seq exactly (#7 does not hit #70)", () => {
|
|
9
|
+
expect(matchesTaskId(row("req_a", 7), "#7")).toBe(true);
|
|
10
|
+
expect(matchesTaskId(row("req_a", 7), "7")).toBe(true);
|
|
11
|
+
expect(matchesTaskId(row("req_a", 70), "#7")).toBe(false);
|
|
12
|
+
expect(matchesTaskId(row("req_a", null), "#7")).toBe(false);
|
|
13
|
+
});
|
|
14
|
+
test("full id and 8+ char prefix, with or without req_; shorter prefixes do not count", () => {
|
|
15
|
+
const id = "req_0a1b2c3d-4e5f-6789-abcd-ef0123456789";
|
|
16
|
+
expect(matchesTaskId(row(id, 1), id)).toBe(true);
|
|
17
|
+
expect(matchesTaskId(row(id, 1), "0a1b2c3d")).toBe(true);
|
|
18
|
+
expect(matchesTaskId(row(id, 1), "req_0a1b2c3d-4")).toBe(true);
|
|
19
|
+
expect(matchesTaskId(row(id, 1), "0a1b2c3")).toBe(false);
|
|
20
|
+
expect(matchesTaskId(row(id, 1), "")).toBe(false);
|
|
21
|
+
});
|
|
22
|
+
test("taskIdQuery: trim, NFKC (full-width #7 → #7), lowercase", () => {
|
|
23
|
+
expect(taskIdQuery(" #7 ")).toBe("#7");
|
|
24
|
+
expect(taskIdQuery("REQ_0A1B")).toBe("req_0a1b");
|
|
25
|
+
});
|
|
26
|
+
test("parseListQuery: idQuery only when there are terms", () => {
|
|
27
|
+
const q = (s: string) => parseListQuery(new URLSearchParams(s)) as any;
|
|
28
|
+
expect(q("q=%EF%BC%83%EF%BC%97").idQuery).toBe("#7");
|
|
29
|
+
expect(q("q=%20%20").idQuery).toBe("");
|
|
30
|
+
expect(q("").idQuery).toBe("");
|
|
31
|
+
});
|
|
32
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
// GET /api/requirements 的服务端搜索(q=)与分页(limit / cursor)。纯函数,requirements.ts 接线。
|
|
2
|
+
//
|
|
3
|
+
// 为什么要:列表一直是 `ORDER BY created_at DESC LIMIT 500`,App 的任务搜索只在读到的行里搜 ——
|
|
4
|
+
// 生产一个网络已经 ~400 张卡,过了 500 最老的那些就搜不到、也翻不到。
|
|
5
|
+
//
|
|
6
|
+
// 语义与 App 的任务搜索(agent-network-app src/task-search.ts)一致,两边搜同一个词得到同一批卡:
|
|
7
|
+
// · 字段:标题、描述(去掉 markdown 图片 / 链接的地址,链接文字保留)、负责人 / 负责 Agent / 参与人的显示名、
|
|
8
|
+
// 旧 assignee 文本、项目名、标签
|
|
9
|
+
// · NFKC(全角 → 半角)+ 小写;中文按子串;空白隔开的词是「且」,每个词可以落在不同字段,但不跨字段拼
|
|
10
|
+
// · 调用者看不见的节点(受限成员)在卡片上本来就被隐去,它的名字也不参与匹配 —— 否则 q= 就成了
|
|
11
|
+
// 「这张卡是不是某个没授权给我的 Agent 负责」的探测器
|
|
12
|
+
//
|
|
13
|
+
// 分页:按 (created_at DESC, requirement_id DESC) 排,cursor = 上一页最后一行的这两个值(不透明的 base64url)。
|
|
14
|
+
// 不带 limit / cursor / q 的旧请求:同样的 500 行、同样的顺序,响应只多两个字段 has_more / next_cursor。
|
|
15
|
+
|
|
16
|
+
export const DEFAULT_LIST_LIMIT = 500;
|
|
17
|
+
export const MAX_LIST_LIMIT = 1000;
|
|
18
|
+
export const MAX_QUERY_LENGTH = 200;
|
|
19
|
+
|
|
20
|
+
const fold = (s: string): string => s.normalize("NFKC").toLocaleLowerCase();
|
|
21
|
+
|
|
22
|
+
/** 搜索词:归一后按空白切开,去重、去空。 */
|
|
23
|
+
export function searchTerms(q: string): string[] {
|
|
24
|
+
return [...new Set(fold(q).split(/\s+/).filter(Boolean))];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** 描述是 markdown:图片 / 链接的地址不参与搜索,链接文字保留;html 标签去掉。 */
|
|
28
|
+
export function descriptionText(md: string | null | undefined): string {
|
|
29
|
+
if (!md) return "";
|
|
30
|
+
return md
|
|
31
|
+
.replace(/!\[([^\]]*)\]\([^)]*\)/g, "$1")
|
|
32
|
+
.replace(/\[([^\]]*)\]\([^)]*\)/g, "$1")
|
|
33
|
+
.replace(/<[^>]+>/g, " ");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type Ref = { kind?: unknown; id?: unknown } | null | undefined;
|
|
37
|
+
export type SearchableRow = {
|
|
38
|
+
name: string;
|
|
39
|
+
description: string;
|
|
40
|
+
assignee: string;
|
|
41
|
+
tags: readonly string[];
|
|
42
|
+
project_id: string | null;
|
|
43
|
+
owner: Ref;
|
|
44
|
+
agent_owner: Ref;
|
|
45
|
+
participants: readonly Ref[] | unknown;
|
|
46
|
+
};
|
|
47
|
+
export type NameMaps = {
|
|
48
|
+
/** `user:<id>` / `node:<id>` → 显示名(看不见的节点不在里面)。 */
|
|
49
|
+
people: ReadonlyMap<string, string>;
|
|
50
|
+
/** project_id → 项目名。 */
|
|
51
|
+
projects: ReadonlyMap<string, string>;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const refName = (ref: Ref, maps: NameMaps): string => {
|
|
55
|
+
if (!ref || typeof ref !== "object" || typeof ref.id !== "string" || (ref.kind !== "user" && ref.kind !== "node")) return "";
|
|
56
|
+
return maps.people.get(`${ref.kind}:${ref.id}`) ?? "";
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/** 一张卡能被搜到的文字,字段之间用换行隔开(一个词不会跨两个字段拼出来)。 */
|
|
60
|
+
export function searchText(row: SearchableRow, maps: NameMaps): string {
|
|
61
|
+
const participants = Array.isArray(row.participants) ? row.participants as Ref[] : [];
|
|
62
|
+
return fold([
|
|
63
|
+
row.name,
|
|
64
|
+
descriptionText(row.description),
|
|
65
|
+
refName(row.owner, maps),
|
|
66
|
+
refName(row.agent_owner, maps),
|
|
67
|
+
...participants.map(r => refName(r, maps)),
|
|
68
|
+
row.assignee,
|
|
69
|
+
row.project_id ? maps.projects.get(row.project_id) ?? "" : "",
|
|
70
|
+
...row.tags,
|
|
71
|
+
].filter(Boolean).join("\n"));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function matchesTerms(row: SearchableRow, terms: readonly string[], maps: NameMaps): boolean {
|
|
75
|
+
if (!terms.length) return true;
|
|
76
|
+
const hay = searchText(row, maps);
|
|
77
|
+
return terms.every(t => hay.includes(t));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export type Cursor = { createdAt: string; id: string };
|
|
81
|
+
|
|
82
|
+
export function encodeCursor(c: Cursor): string {
|
|
83
|
+
return Buffer.from(JSON.stringify([c.createdAt, c.id]), "utf8").toString("base64url");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function decodeCursor(raw: string): Cursor | null {
|
|
87
|
+
if (!raw || raw.length > 600 || !/^[A-Za-z0-9_-]+$/.test(raw)) return null;
|
|
88
|
+
try {
|
|
89
|
+
const v = JSON.parse(Buffer.from(raw, "base64url").toString("utf8"));
|
|
90
|
+
if (!Array.isArray(v) || v.length !== 2 || typeof v[0] !== "string" || typeof v[1] !== "string" || !v[0] || !v[1]) return null;
|
|
91
|
+
return { createdAt: v[0], id: v[1] };
|
|
92
|
+
} catch {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export type ListQuery = { limit: number; cursor: Cursor | null; terms: string[]; /** 整句当任务 ID 去对(taskIdQuery);空 = 没有搜索。 */ idQuery: string; /** #471 —— 只要带这个标签的卡(精确、区分大小写);null = 不按标签筛。 */ tag: string | null };
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 任务 ID(同 App 的 task-short-id.ts matchesTaskId):「#42」「42」精确对短号 seq;完整 id,或 8 位以上的
|
|
101
|
+
* id 前缀(带不带 req_ 都行)对主键。整句当一个 ID —— 和文字匹配是「或」,不拆成「ID 且文字」。
|
|
102
|
+
* 全角「#42」先 NFKC 归一(ID 本身都是 ASCII)。
|
|
103
|
+
*/
|
|
104
|
+
export const taskIdQuery = (q: string): string => q.trim().normalize("NFKC").toLowerCase();
|
|
105
|
+
export const seqOfQuery = (idQuery: string): number | null => {
|
|
106
|
+
const m = /^#?(\d{1,15})$/.exec(idQuery);
|
|
107
|
+
return m ? Number(m[1]) : null;
|
|
108
|
+
};
|
|
109
|
+
export function matchesTaskId(row: { requirement_id: string; seq?: number | null }, idQuery: string): boolean {
|
|
110
|
+
if (!idQuery) return false;
|
|
111
|
+
const n = seqOfQuery(idQuery);
|
|
112
|
+
if (n !== null) return typeof row.seq === "number" && row.seq === n;
|
|
113
|
+
const id = row.requirement_id.toLowerCase();
|
|
114
|
+
if (idQuery === id) return true;
|
|
115
|
+
const bare = idQuery.replace(/^req_/, "");
|
|
116
|
+
return bare.length >= 8 && id.replace(/^req_/, "").startsWith(bare);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** 解析 limit / cursor / q。不合法 → 错误码(400)。都没带 = 旧行为(500 行、第一页、不搜)。 */
|
|
120
|
+
export function parseListQuery(params: URLSearchParams): ListQuery | { error: string } {
|
|
121
|
+
let limit = DEFAULT_LIST_LIMIT;
|
|
122
|
+
const rawLimit = params.get("limit");
|
|
123
|
+
if (rawLimit !== null) {
|
|
124
|
+
if (!/^\d{1,5}$/.test(rawLimit)) return { error: "invalid_limit" };
|
|
125
|
+
limit = Number(rawLimit);
|
|
126
|
+
if (limit < 1 || limit > MAX_LIST_LIMIT) return { error: "invalid_limit" };
|
|
127
|
+
}
|
|
128
|
+
let cursor: Cursor | null = null;
|
|
129
|
+
const rawCursor = params.get("cursor");
|
|
130
|
+
if (rawCursor !== null && rawCursor !== "") {
|
|
131
|
+
cursor = decodeCursor(rawCursor);
|
|
132
|
+
if (!cursor) return { error: "invalid_cursor" };
|
|
133
|
+
}
|
|
134
|
+
const q = params.get("q") ?? "";
|
|
135
|
+
if (q.length > MAX_QUERY_LENGTH) return { error: "invalid_q" };
|
|
136
|
+
const terms = searchTerms(q);
|
|
137
|
+
// #471 —— tag=:和卡片标签同一套规范(去首尾空白、1–20 个字符、不含控制字符);不带 = 旧行为。
|
|
138
|
+
const rawTag = params.get("tag");
|
|
139
|
+
let tag: string | null = null;
|
|
140
|
+
if (rawTag !== null) {
|
|
141
|
+
tag = rawTag.trim();
|
|
142
|
+
if (!tag || [...tag].length > 20 || /[\u0000-\u001f\u007f]/u.test(tag)) return { error: "invalid_tag" };
|
|
143
|
+
}
|
|
144
|
+
return { limit, cursor, terms, idQuery: terms.length ? taskIdQuery(q) : "", tag };
|
|
145
|
+
}
|