@sleep2agi/commhub-server 0.9.0-preview.20 → 0.9.0-preview.201

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.
Files changed (398) hide show
  1. package/README.md +130 -10
  2. package/bin/commhub.ts +99 -11
  3. package/package.json +7 -7
  4. package/src/ack-create-request-transport.test.ts +182 -0
  5. package/src/ack-create-request.test.ts +50 -7
  6. package/src/admin-delete-network-http.test.ts +228 -0
  7. package/src/admin-networks-http.test.ts +88 -0
  8. package/src/adoption-candidates-http.test.ts +137 -0
  9. package/src/adoption-candidates.ts +158 -0
  10. package/src/agent-access-batch.test.ts +210 -0
  11. package/src/agent-access.ts +487 -0
  12. package/src/agent-acl-groups-http.test.ts +247 -0
  13. package/src/agent-acl-http.test.ts +659 -0
  14. package/src/agent-acl-slice3-http.test.ts +267 -0
  15. package/src/agent-department-http.test.ts +215 -0
  16. package/src/agent-groups-perf.test.ts +223 -0
  17. package/src/agent-notice.ts +48 -0
  18. package/src/agent-team-whoami-http.test.ts +98 -0
  19. package/src/agent-teams-http.test.ts +330 -0
  20. package/src/agent-teams.ts +244 -0
  21. package/src/alias-baseline-2571.test.ts +162 -0
  22. package/src/alias-baseline.ts +113 -0
  23. package/src/alias-filter.test.ts +72 -0
  24. package/src/alias-filter.ts +42 -0
  25. package/src/api-host-supervisors-can-create-nodes.test.ts +350 -0
  26. package/src/api-host-supervisors-fallback.test.ts +21 -12
  27. package/src/api-host-supervisors-runtime-readiness.test.ts +195 -0
  28. package/src/api-nodes-shape.test.ts +120 -13
  29. package/src/auth-password-change-cli-tokens-http.test.ts +423 -0
  30. package/src/auth-sessions-http.test.ts +250 -0
  31. package/src/auth-validate.test.ts +5 -1
  32. package/src/auth-weak-password-login.test.ts +176 -0
  33. package/src/auth.ts +609 -51
  34. package/src/avatar-validate.test.ts +163 -0
  35. package/src/avatar-validate.ts +103 -0
  36. package/src/backfill-node-ids-2664.test.ts +128 -0
  37. package/src/binding-generation-http.test.ts +121 -0
  38. package/src/blocked-exit-on-reply.test.ts +122 -0
  39. package/src/boot-side-effect.test.ts +124 -0
  40. package/src/broadcast-fanout-2721.test.ts +118 -0
  41. package/src/broadcast-fanout.ts +98 -0
  42. package/src/chain-attempt-dedup.test.ts +447 -0
  43. package/src/clone-node.test.ts +248 -0
  44. package/src/clone-node.ts +289 -0
  45. package/src/codex-config-flags.ts +77 -0
  46. package/src/codex-fork-contract.ts +23 -0
  47. package/src/codex-fork-migration.test.ts +38 -0
  48. package/src/commhub-bin-args.test.ts +85 -0
  49. package/src/config-apply-sec1.test.ts +107 -0
  50. package/src/config-apply-validate.test.ts +134 -1
  51. package/src/config-apply-validate.ts +111 -5
  52. package/src/create-node-blocked-gate.test.ts +356 -0
  53. package/src/create-node-network-id.test.ts +79 -0
  54. package/src/create-node-tool-schema.test.ts +49 -0
  55. package/src/create-node-unicode-name.test.ts +120 -0
  56. package/src/create-node-validate.test.ts +199 -7
  57. package/src/create-node-validate.ts +232 -15
  58. package/src/create-node-workdir.test.ts +289 -0
  59. package/src/create-node.ts +88 -23
  60. package/src/daemon-provider-snapshots.test.ts +421 -0
  61. package/src/daemon-provider-snapshots.ts +327 -0
  62. package/src/daemon-token-owner-http.test.ts +236 -0
  63. package/src/dashboard-slash-routing-http.test.ts +109 -0
  64. package/src/db-adapter-guard.test.ts +148 -0
  65. package/src/db-adapter-translate.test.ts +128 -0
  66. package/src/db-adapter.ts +377 -91
  67. package/src/db-timestamp.test.ts +38 -0
  68. package/src/db-timestamp.ts +15 -0
  69. package/src/db.ts +1306 -153
  70. package/src/delivered-without-push.test.ts +140 -0
  71. package/src/department-groups-http.test.ts +209 -0
  72. package/src/department-groups-sync-http.test.ts +328 -0
  73. package/src/department-groups.ts +434 -0
  74. package/src/department-heads-http.test.ts +330 -0
  75. package/src/department-heads.ts +226 -0
  76. package/src/departments-agent-count.test.ts +129 -0
  77. package/src/departments-http.test.ts +185 -0
  78. package/src/departments-mcp.test.ts +128 -0
  79. package/src/departments.ts +318 -0
  80. package/src/desktop-message-seam.test.ts +150 -0
  81. package/src/desktop-message.test.ts +368 -0
  82. package/src/dm-file-scope-http.test.ts +178 -0
  83. package/src/empty-node-sweeper.test.ts +104 -0
  84. package/src/empty-node-sweeper.ts +88 -0
  85. package/src/env-transport-gate-http.test.ts +177 -0
  86. package/src/env-transport-gate.ts +74 -0
  87. package/src/env-transport-policy.ts +21 -0
  88. package/src/event-loop-lag.test.ts +139 -0
  89. package/src/event-loop-lag.ts +150 -0
  90. package/src/ext-token-shared.test.ts +60 -0
  91. package/src/ext-validation-regression.test.ts +131 -0
  92. package/src/external-schedule-edits-http.test.ts +321 -0
  93. package/src/external-schedule-edits-timestamps.test.ts +64 -0
  94. package/src/external-schedule-edits.ts +327 -0
  95. package/src/file-download-authz-dev-open.test.ts +135 -0
  96. package/src/file-download-authz.test.ts +404 -0
  97. package/src/file-download-date-bucket.test.ts +436 -0
  98. package/src/file-network-scope-dev-open.test.ts +97 -0
  99. package/src/file-network-scope.test.ts +732 -0
  100. package/src/group-membership-events-http.test.ts +264 -0
  101. package/src/group-messages-http.test.ts +482 -0
  102. package/src/group-messages.ts +297 -0
  103. package/src/health-host-disk-2756.test.ts +49 -0
  104. package/src/health-redaction.test.ts +685 -0
  105. package/src/host-supervisor-identity-http.test.ts +152 -0
  106. package/src/host-supervisor-identity.ts +92 -0
  107. package/src/host-supervisors-daemon-version.test.ts +137 -0
  108. package/src/http-gzip.test.ts +94 -0
  109. package/src/http-gzip.ts +112 -0
  110. package/src/hub-catalog-read-http.test.ts +187 -0
  111. package/src/hub-provider-config.test.ts +139 -0
  112. package/src/hub-provider-config.ts +105 -0
  113. package/src/hub-timestamp.test.ts +38 -0
  114. package/src/hub-timestamp.ts +27 -0
  115. package/src/human-dm.ts +215 -0
  116. package/src/human-inbox.ts +42 -0
  117. package/src/inbox-caller.ts +49 -0
  118. package/src/inbox-count-sse.test.ts +310 -0
  119. package/src/inbox-count.ts +29 -0
  120. package/src/index.ts +15 -2380
  121. package/src/list-host-supervisors.test.ts +5 -4
  122. package/src/mcp-context-cost-http.test.ts +127 -0
  123. package/src/mcp-inflight.test.ts +92 -0
  124. package/src/mcp-inflight.ts +38 -0
  125. package/src/mcp-projects-events-http.test.ts +162 -0
  126. package/src/mcp-route-label-http.test.ts +120 -0
  127. package/src/mcp-write-network-resolution.test.ts +432 -0
  128. package/src/member-audit-network-id-http.test.ts +93 -0
  129. package/src/member-presence-http.test.ts +210 -0
  130. package/src/messages-ack-by-agent-http.test.ts +142 -0
  131. package/src/migrate-sqlite-to-pg.test.ts +47 -0
  132. package/src/migrate-sqlite-to-pg.ts +263 -0
  133. package/src/model-auth-notify-http.test.ts +181 -0
  134. package/src/model-auth-notify.ts +116 -0
  135. package/src/network-name-http.test.ts +75 -0
  136. package/src/network-rules-http.test.ts +169 -0
  137. package/src/network-rules.ts +129 -0
  138. package/src/network-scope.ts +276 -0
  139. package/src/network-token-membership-http.test.ts +139 -0
  140. package/src/node-alias-control.ts +12 -0
  141. package/src/node-attrs-validate.ts +88 -0
  142. package/src/node-attrs.test.ts +258 -0
  143. package/src/node-caller-identity-http.test.ts +1083 -0
  144. package/src/node-create-request-status-http.test.ts +105 -0
  145. package/src/node-daemon-bindings.test.ts +132 -0
  146. package/src/node-daemon-bindings.ts +135 -0
  147. package/src/node-failure-alert.test.ts +150 -0
  148. package/src/node-failure-alert.ts +177 -0
  149. package/src/node-files-transport.test.ts +344 -0
  150. package/src/node-health-codex-login.test.ts +120 -0
  151. package/src/node-health-dispatch-http.test.ts +279 -0
  152. package/src/node-health-guard.ts +105 -0
  153. package/src/node-health-passthrough.test.ts +115 -0
  154. package/src/node-health-store.ts +101 -0
  155. package/src/node-id-alias.test.ts +190 -0
  156. package/src/node-identity-conflict-http.test.ts +269 -0
  157. package/src/node-identity-conflict.ts +142 -0
  158. package/src/node-identity-mismatch.test.ts +74 -0
  159. package/src/node-in-flight-tasks.ts +31 -0
  160. package/src/node-lifecycle-controllable.test.ts +64 -0
  161. package/src/node-lifecycle-controllable.ts +53 -0
  162. package/src/node-lifecycle-read-http.test.ts +250 -0
  163. package/src/node-lifecycle-read.ts +124 -0
  164. package/src/node-logs-transport.test.ts +263 -0
  165. package/src/node-peer-allowlist.test.ts +114 -0
  166. package/src/node-permissions-http.test.ts +345 -0
  167. package/src/node-permissions.ts +420 -0
  168. package/src/node-request-retention.test.ts +281 -0
  169. package/src/node-request-retention.ts +120 -0
  170. package/src/node-secret-rest-http.test.ts +190 -0
  171. package/src/node-serviceability.test.ts +86 -0
  172. package/src/node-serviceability.ts +124 -0
  173. package/src/node-session-control-http.test.ts +310 -0
  174. package/src/node-session-control.ts +258 -0
  175. package/src/node-skills-transport.test.ts +209 -0
  176. package/src/node-token-identity.ts +35 -0
  177. package/src/node-token-ownership.ts +51 -0
  178. package/src/node-token-privilege-http.test.ts +158 -0
  179. package/src/not-found-api-http.test.ts +67 -0
  180. package/src/observer-avatar-http.test.ts +442 -0
  181. package/src/observer-push.test.ts +252 -0
  182. package/src/opencode-catalog-report-http.test.ts +139 -0
  183. package/src/opencode-v2-catalog.test.ts +268 -0
  184. package/src/opencode-v2-catalog.ts +352 -0
  185. package/src/package-version-consistency.test.ts +25 -0
  186. package/src/parent-source-marker.test.ts +50 -0
  187. package/src/password-network-tokens.ts +25 -0
  188. package/src/peer-reply-atomic.test.ts +455 -0
  189. package/src/peer-reply-capability.test.ts +70 -0
  190. package/src/pg-worker.ts +87 -0
  191. package/src/project-dir-warn-clear-http.test.ts +132 -0
  192. package/src/project-viewer-can-http.test.ts +116 -0
  193. package/src/push.test.ts +20 -10
  194. package/src/push.ts +466 -13
  195. package/src/redact-tokens.test.ts +65 -0
  196. package/src/redact-tokens.ts +40 -0
  197. package/src/rename.ts +38 -10
  198. package/src/reply-target-derivation.test.ts +96 -0
  199. package/src/report-status-handover-capabilities-http.test.ts +124 -0
  200. package/src/report-status-host-schema.test.ts +235 -0
  201. package/src/report-status-resume-id-handover.test.ts +184 -0
  202. package/src/report-status-secret-keys.test.ts +140 -0
  203. package/src/require-explicit-test-db.ts +9 -0
  204. package/src/requirement-due-reminders-http.test.ts +620 -0
  205. package/src/requirement-due-reminders.ts +416 -0
  206. package/src/requirement-events-http.test.ts +268 -0
  207. package/src/requirement-events.test.ts +45 -0
  208. package/src/requirement-events.ts +144 -0
  209. package/src/requirement-notify.ts +165 -0
  210. package/src/requirement-participant-notify-http.test.ts +309 -0
  211. package/src/requirement-tag-ops-http.test.ts +425 -0
  212. package/src/requirement-tag-tree-http.test.ts +275 -0
  213. package/src/requirement-tag-tree.ts +127 -0
  214. package/src/requirement-tags.ts +102 -0
  215. package/src/requirements-abandoned-http.test.ts +236 -0
  216. package/src/requirements-comment-http.test.ts +167 -0
  217. package/src/requirements-completed-at-migration.test.ts +70 -0
  218. package/src/requirements-errors-mcp-http.test.ts +164 -0
  219. package/src/requirements-errors.ts +158 -0
  220. package/src/requirements-etag-http.test.ts +91 -0
  221. package/src/requirements-http.test.ts +709 -0
  222. package/src/requirements-last-event-http.test.ts +349 -0
  223. package/src/requirements-list-mcp-slim-http.test.ts +155 -0
  224. package/src/requirements-list-slim-http.test.ts +324 -0
  225. package/src/requirements-mcp.test.ts +163 -0
  226. package/src/requirements-migrate.ts +283 -0
  227. package/src/requirements-owner-strict-mcp-http.test.ts +145 -0
  228. package/src/requirements-people-http.test.ts +114 -0
  229. package/src/requirements-people-mcp-http.test.ts +231 -0
  230. package/src/requirements-people.ts +196 -0
  231. package/src/requirements-priority-lowest.test.ts +114 -0
  232. package/src/requirements-projects-parent-http.test.ts +123 -0
  233. package/src/requirements-search-http.test.ts +182 -0
  234. package/src/requirements-search.test.ts +32 -0
  235. package/src/requirements-search.ts +145 -0
  236. package/src/requirements-seq-http.test.ts +193 -0
  237. package/src/requirements-seq-migration.test.ts +63 -0
  238. package/src/requirements-start-date.test.ts +125 -0
  239. package/src/requirements-stats-http.test.ts +357 -0
  240. package/src/requirements-stats.ts +195 -0
  241. package/src/requirements-status-participants-http.test.ts +222 -0
  242. package/src/requirements-upgrade-e2e.test.ts +126 -0
  243. package/src/requirements-upgrade-migration.test.ts +98 -0
  244. package/src/requirements.ts +2027 -0
  245. package/src/resolve-port.test.ts +57 -0
  246. package/src/resolve-port.ts +44 -0
  247. package/src/response-charset.test.ts +130 -0
  248. package/src/rest-alias-network-fallback-http.test.ts +123 -0
  249. package/src/rest-alias-network-fallback.test.ts +60 -0
  250. package/src/rest-alias-network-fallback.ts +65 -0
  251. package/src/rest-explicit-columns-http.test.ts +266 -0
  252. package/src/rest-identity.test.ts +130 -0
  253. package/src/rest-identity.ts +141 -0
  254. package/src/rest-projections.ts +72 -0
  255. package/src/rest-read-network-real-chain.test.ts +97 -0
  256. package/src/rest-write-network-resolution.test.ts +133 -0
  257. package/src/restart-readiness-http.test.ts +121 -0
  258. package/src/restricted-files.ts +76 -0
  259. package/src/retention-first-sweep-quiet.test.ts +31 -0
  260. package/src/retention.test.ts +100 -40
  261. package/src/retention.ts +423 -104
  262. package/src/route-callers-http.test.ts +120 -0
  263. package/src/route-timing.test.ts +211 -0
  264. package/src/route-timing.ts +262 -0
  265. package/src/rules-file-duplicate-alias.test.ts +234 -0
  266. package/src/rules-file-session-target.test.ts +207 -0
  267. package/src/rules-file-transport.test.ts +237 -0
  268. package/src/runtime-label.test.ts +22 -0
  269. package/src/runtime-label.ts +21 -0
  270. package/src/runtime-readiness.ts +90 -0
  271. package/src/schedule-agent-mcp-http.test.ts +391 -0
  272. package/src/schedule-agent.ts +192 -0
  273. package/src/schedule-starved.test.ts +92 -0
  274. package/src/schedule-starved.ts +114 -0
  275. package/src/scheduled-backoff-http.test.ts +193 -0
  276. package/src/scheduled-backoff.ts +64 -0
  277. package/src/scheduled-doorbell-nodeless-session.test.ts +134 -0
  278. package/src/scheduled-failure-backoff-2622.test.ts +145 -0
  279. package/src/scheduled-failure-code.test.ts +73 -0
  280. package/src/scheduled-failure-code.ts +105 -0
  281. package/src/scheduled-failures-http.test.ts +279 -0
  282. package/src/scheduled-failures.ts +377 -0
  283. package/src/scheduled-noprogress-backoff-2622.test.ts +133 -0
  284. package/src/scheduled-offline-backoff-probe.test.ts +56 -0
  285. package/src/scheduled-offline.test.ts +131 -0
  286. package/src/scheduled-offline.ts +66 -0
  287. package/src/scheduled-outcome-guidance-http.test.ts +139 -0
  288. package/src/scheduled-outcome-guidance.ts +9 -0
  289. package/src/scheduled-reply-unread-http.test.ts +212 -0
  290. package/src/scheduled-requirement-wake-http.test.ts +279 -0
  291. package/src/scheduled-requirement-wake.ts +125 -0
  292. package/src/scheduled-run-terminal.test.ts +268 -0
  293. package/src/scheduled-skip-blocking-task.test.ts +120 -0
  294. package/src/scheduled-skip-path-backoff-2622.test.ts +98 -0
  295. package/src/scheduled-stuck-http.test.ts +312 -0
  296. package/src/scheduled-stuck-notice-cap.test.ts +80 -0
  297. package/src/scheduled-stuck.ts +219 -0
  298. package/src/scheduled-tasks-http.test.ts +679 -0
  299. package/src/scheduled-tasks.ts +959 -0
  300. package/src/scheduled-work-outcome-http.test.ts +154 -0
  301. package/src/scheduled-work-outcome.ts +50 -0
  302. package/src/scoped-secrets.ts +139 -0
  303. package/src/send-dedup-request-id.test.ts +229 -0
  304. package/src/send-reply-agent-warning.test.ts +202 -0
  305. package/src/send-reply-alias-optional.test.ts +131 -0
  306. package/src/send-reply-attachments.test.ts +458 -0
  307. package/src/send_dedup.test.ts +54 -0
  308. package/src/send_dedup.ts +52 -6
  309. package/src/serve-error.test.ts +38 -0
  310. package/src/serve-error.ts +27 -0
  311. package/src/server.ts +6028 -0
  312. package/src/session-task-on-dispatch.ts +14 -0
  313. package/src/session-token-ownership-http.test.ts +122 -0
  314. package/src/shared/codex-inventory.ts +41 -0
  315. package/src/shared/daemon-provider-login.ts +31 -0
  316. package/src/shared/daemon-provider-snapshot.ts +52 -0
  317. package/src/shared/external-schedule-contract.ts +60 -0
  318. package/src/shared/node-name.test.ts +62 -0
  319. package/src/shared/node-name.ts +161 -0
  320. package/src/shared/provider-application.ts +29 -0
  321. package/src/shared/provider-collection.ts +14 -0
  322. package/src/shared/provider-initialization.ts +8 -0
  323. package/src/shared/reserved-env-drift.test.ts +10 -0
  324. package/src/shared/scoped-secret-contract.ts +5 -0
  325. package/src/shared/task-content-limit-drift.test.ts +28 -0
  326. package/src/shared/task-content-limit.ts +25 -0
  327. package/src/side-thread-command-transport.test.ts +312 -0
  328. package/src/side-thread-command-transport.ts +318 -0
  329. package/src/side-thread-http-integration.test.ts +233 -0
  330. package/src/side-thread-http.ts +504 -0
  331. package/src/side-thread-production.test.ts +72 -0
  332. package/src/side-thread-production.ts +118 -0
  333. package/src/side-thread.test.ts +1067 -0
  334. package/src/side-thread.ts +2499 -0
  335. package/src/skillhub-http.test.ts +108 -0
  336. package/src/sqlite-close-checkpoint.test.ts +78 -0
  337. package/src/start-node.test.ts +264 -0
  338. package/src/status-alias-filter-http.test.ts +106 -0
  339. package/src/status-alias-resolver-http.test.ts +163 -0
  340. package/src/status-alias-resolver.ts +19 -0
  341. package/src/status-node-id-filter-http.test.ts +161 -0
  342. package/src/status-read-cache-http.test.ts +229 -0
  343. package/src/status-read-cache.ts +122 -0
  344. package/src/stop-delete-node.test.ts +567 -1
  345. package/src/task-access-http.test.ts +402 -0
  346. package/src/task-access.ts +420 -0
  347. package/src/task-activity.ts +60 -0
  348. package/src/task-auth-origin-http.test.ts +93 -0
  349. package/src/task-auth-origin.test.ts +24 -0
  350. package/src/task-auth-origin.ts +7 -0
  351. package/src/task-consumption.test.ts +482 -0
  352. package/src/task-diagnostic-http.test.ts +149 -0
  353. package/src/task-diagnostic.test.ts +115 -0
  354. package/src/task-diagnostic.ts +118 -0
  355. package/src/task-expiry-notice-http.test.ts +287 -0
  356. package/src/task-expiry-notice.ts +315 -0
  357. package/src/task-idempotency-mcp.test.ts +142 -0
  358. package/src/task-idempotency.test.ts +76 -0
  359. package/src/task-idempotency.ts +81 -0
  360. package/src/task-late-replies.ts +54 -0
  361. package/src/task-late-reply-http.test.ts +152 -0
  362. package/src/task-lifecycle-watcher-busy.test.ts +114 -0
  363. package/src/task-lifecycle-watcher.test.ts +204 -0
  364. package/src/task-lifecycle-watcher.ts +109 -0
  365. package/src/task-network-resolution-http.test.ts +140 -0
  366. package/src/task-offline-delivery-2668.test.ts +119 -0
  367. package/src/task-offline-delivery.ts +81 -0
  368. package/src/task-orphan-http.test.ts +287 -0
  369. package/src/task-orphan.ts +119 -0
  370. package/src/task-queue-ahead-http.test.ts +309 -0
  371. package/src/task-queue-ahead-scale.test.ts +236 -0
  372. package/src/task-queue-ahead.ts +186 -0
  373. package/src/task-rejection-log-http.test.ts +160 -0
  374. package/src/task-rejection-log.ts +49 -0
  375. package/src/task-sender-http.test.ts +143 -0
  376. package/src/task-stale-open-http.test.ts +290 -0
  377. package/src/task-stale-open.ts +103 -0
  378. package/src/tasks-pagination-http.test.ts +141 -0
  379. package/src/team-rules-status-http.test.ts +90 -0
  380. package/src/team-rules-status.test.ts +59 -0
  381. package/src/team-rules-status.ts +72 -0
  382. package/src/tool-audience-http.test.ts +230 -0
  383. package/src/tool-audience.ts +120 -0
  384. package/src/tools-list-json-schema.test.ts +94 -0
  385. package/src/tools.ts +4226 -405
  386. package/src/upload-early-reject-guard.test.ts +155 -0
  387. package/src/uploads-http.test.ts +97 -15
  388. package/src/uploads.ts +103 -6
  389. package/src/user-avatar-http.test.ts +74 -0
  390. package/src/user-avatar-image.ts +160 -0
  391. package/src/user-avatar-migration.test.ts +25 -0
  392. package/src/user-avatar-upload-http.test.ts +348 -0
  393. package/src/user-inbox-by-agent-http.test.ts +161 -0
  394. package/src/user-inbox-netid-notnull.test.ts +122 -0
  395. package/src/user-inbox-read-http.test.ts +223 -0
  396. package/src/user-inbox-schema.test.ts +54 -0
  397. package/src/version-stats.test.ts +66 -0
  398. package/src/version-stats.ts +109 -0
@@ -0,0 +1,124 @@
1
+ // #438 corrective — regression gate for the two boot-path failure modes:
2
+ //
3
+ // 1. import side effect: `import("./server.js")` must NOT bind any port.
4
+ // Pre-corrective (7fa3ad35, when this module was still index.ts with
5
+ // module-level `export const server = bootServer()`) this fails:
6
+ // witnessed-red showed the probe port EADDRINUSE right after import.
7
+ // The run-entry shim src/index.ts is the ONLY module whose import
8
+ // boots — by design, documented there — and is deliberately not
9
+ // imported anywhere in the test suite.
10
+ // 2. silent no-op double start: `startHub()` is single-shot and must
11
+ // REJECT a second call observably — "I thought I started it but
12
+ // nothing happened" is the same failure family as the
13
+ // import.meta.main incident (#438 review).
14
+ //
15
+ // Test order inside this file matters: the no-side-effect probe must run
16
+ // BEFORE anything calls startHub in this process. bun executes tests in
17
+ // declaration order, and no other suite calls startHub.
18
+
19
+ import { describe, expect, test } from "bun:test";
20
+
21
+ const PROBE_PORT = 18500 + Math.floor(Math.random() * 500);
22
+
23
+ describe("#438 corrective — importing server.ts is side-effect-free", () => {
24
+ test("importing server.js binds nothing (probe port stays free)", async () => {
25
+ // Pin the module's default PORT to the probe so that IF an import
26
+ // side effect ever regresses, it binds exactly the port we probe.
27
+ process.env.PORT = process.env.PORT || String(PROBE_PORT);
28
+ const probe = Number(process.env.PORT) || PROBE_PORT;
29
+ await import("./server.js");
30
+ // If import had booted a server, this bind would throw EADDRINUSE.
31
+ let listener: any = null;
32
+ expect(() => {
33
+ listener = Bun.listen({ hostname: "127.0.0.1", port: probe, socket: { data() {} } });
34
+ }).not.toThrow();
35
+ listener?.stop?.(true);
36
+ });
37
+ });
38
+
39
+ describe("#476 — import alone must let the process exit", () => {
40
+ test("a subprocess that only imports server.js exits by itself (no module-level timers hold the loop)", () => {
41
+ // Witnessed-red: pre-#476 (main 541a80aa) this child is killed by the
42
+ // timeout with a non-zero exit — two module-level setIntervals
43
+ // (rate-limit sweep + DB-writing task patrol) held the event loop
44
+ // after a bare import. Post-#476 both live inside startHub, so the
45
+ // child exits 0 on its own. Runs in a subprocess because the parent
46
+ // test process has its own handles and can't observe loop-emptiness.
47
+ const child = Bun.spawnSync({
48
+ cmd: ["bun", "-e", 'await import("./src/server.js");'],
49
+ cwd: `${import.meta.dir}/..`,
50
+ env: { ...process.env, COMMHUB_DB: process.env.COMMHUB_DB || "/tmp/anet-476-probe.db" },
51
+ timeout: 10_000,
52
+ });
53
+ expect(child.exitCode).toBe(0);
54
+ }, 15_000);
55
+
56
+ test("the moved task patrol actually FIRES under startHub (not just registered-looking)", () => {
57
+ // Fake-gate guard for the #476 move: stub patrolExpiredTasks to a
58
+ // no-op (or forget to register it in startHub) and this goes red.
59
+ // Runs in a subprocess because startHub is single-shot per process
60
+ // and the patrol period is shrunk via env for observability.
61
+ const script = `
62
+ process.env.COMMHUB_TASK_PATROL_MS = "100";
63
+ const { startHub } = await import("./src/server.js");
64
+ const { db } = await import("./src/db.js");
65
+ const hub = startHub({ port: 0, hostname: "127.0.0.1" });
66
+ db.run("DELETE FROM tasks WHERE task_id = 't476_patrol'");
67
+ db.run("INSERT INTO tasks (task_id, from_name, to_name, priority, status, content, created_at, expires_at) " +
68
+ "VALUES ('t476_patrol', 'a', 'b', 'normal', 'delivered', 'x', datetime('now'), datetime('now', '-1 minute'))");
69
+ await new Promise((r) => setTimeout(r, 600));
70
+ const row = db.get("SELECT status FROM tasks WHERE task_id = 't476_patrol'");
71
+ console.log("PATROL_RESULT:" + (row ? row.status : "missing"));
72
+ db.run("DELETE FROM tasks WHERE task_id = 't476_patrol'");
73
+ hub.stop(true);
74
+ process.exit(0);
75
+ `;
76
+ const child = Bun.spawnSync({
77
+ cmd: ["bun", "-e", script],
78
+ cwd: `${import.meta.dir}/..`,
79
+ env: { ...process.env, COMMHUB_DB: process.env.COMMHUB_DB || "/tmp/anet-476-patrol.db" },
80
+ timeout: 15_000,
81
+ });
82
+ const out = new TextDecoder().decode(child.stdout);
83
+ expect(out).toContain("PATROL_RESULT:expired");
84
+ }, 20_000);
85
+ });
86
+
87
+ describe("#438 corrective — startHub is explicit, single-shot, observable", () => {
88
+ test("startHub boots a live hub; second call throws with first boot's coordinates; bootServer stays available", async () => {
89
+ const { startHub, bootServer, getStartedHub } = await import("./server.js") as any;
90
+ expect(getStartedHub()).toBeNull();
91
+
92
+ const hub = startHub({ port: 0, hostname: "127.0.0.1" });
93
+ try {
94
+ expect(hub.port).toBeGreaterThan(0);
95
+ expect(getStartedHub()?.server).toBe(hub);
96
+
97
+ // Really listening — the production liveness probe, not just a
98
+ // non-null handle.
99
+ const health = await fetch(`http://127.0.0.1:${hub.port}/health`);
100
+ expect(health.status).toBe(200);
101
+ expect(((await health.json()) as any).ok).toBe(true);
102
+
103
+ // Second call must be an OBSERVABLE rejection, not a silent no-op,
104
+ // and must tell the caller where the first boot lives.
105
+ let thrown: Error | null = null;
106
+ try {
107
+ startHub({ port: 0 });
108
+ } catch (e: any) {
109
+ thrown = e;
110
+ }
111
+ expect(thrown).not.toBeNull();
112
+ expect(String(thrown!.message)).toContain("single-shot");
113
+ expect(String(thrown!.message)).toContain(String(hub.port));
114
+
115
+ // Throwaway instances stay available after startHub.
116
+ const extra = bootServer({ port: 0, hostname: "127.0.0.1" });
117
+ expect(extra.port).toBeGreaterThan(0);
118
+ expect(extra.port).not.toBe(hub.port);
119
+ extra.stop(true);
120
+ } finally {
121
+ hub.stop(true);
122
+ }
123
+ });
124
+ });
@@ -0,0 +1,118 @@
1
+ // #2721 —— 广播扇出分批:把「一条广播 = 几百次同步写」摊成「每批一次事务 + 批间让出」。
2
+ //
3
+ // 现场(2026-10-27 07:10,生产 `.165`):384 行 inbox 让 Hub 出现 143.9 秒的 timer 饥饿,
4
+ // RSS 冲到 2.9GB,`requests_since_last_tick` 高达 1175(饱和而非阻塞)。
5
+ //
6
+ // 这个文件钉住四件事:
7
+ // 1. 收件人逐个入库,条数与收件人一致(语义不变);
8
+ // 2. 每 chunk 一批、批间**真的**让出(`yieldBetweenChunks` 被调用 ⌈n/chunk⌉ 次);
9
+ // 3. 非 active 的收件人静默跳过(RFC-027 best-effort),且不推事件;
10
+ // 4. 事件只推给**真的入库了**的收件人(#1440 的语义)。
11
+
12
+ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
13
+ import { db } from "./db.js";
14
+ import { BROADCAST_CHUNK, enqueueBroadcast, broadcastRecentState } from './broadcast-fanout.js';
15
+
16
+ const stamp = `${Date.now()}`;
17
+ let NET = "";
18
+
19
+ function targets(n: number, netId = NET) {
20
+ return Array.from({ length: n }, (_, i) => ({
21
+ alias: `fan-${i}-${stamp}`,
22
+ node_id: `n_fan_${i}_${stamp}`,
23
+ network_id: netId,
24
+ }));
25
+ }
26
+
27
+ const inboxRows = (netId = NET) =>
28
+ db.get<{ n: number }>("SELECT COUNT(*) AS n FROM inbox WHERE network_id = ?1 AND type = 'broadcast'", [netId])!.n;
29
+
30
+ beforeAll(async () => {
31
+ const { register } = await import("./auth.js");
32
+ const auth = register(`fan_owner_${stamp}`, "Fan-Owner-Strong-1!", undefined, "tester");
33
+ expect(auth.ok).toBe(true);
34
+ NET = auth.network_id!;
35
+ });
36
+
37
+ afterAll(() => {
38
+ try { db.run("DELETE FROM inbox WHERE network_id = ?1 AND type = 'broadcast'", [NET]); } catch {}
39
+ });
40
+
41
+ describe("#2721 broadcast fan-out is chunked", () => {
42
+ test("逐条入库,条数等于收件人数", async () => {
43
+ const before = inboxRows();
44
+ const res = await enqueueBroadcast(targets(120), "hello", "hub", NET, {
45
+ isActive: () => true,
46
+ notify: () => {},
47
+ yieldBetweenChunks: async () => {},
48
+ });
49
+ expect(res.ids).toHaveLength(120);
50
+ expect(res.delivered).toHaveLength(120);
51
+ expect(inboxRows() - before).toBe(120);
52
+ });
53
+
54
+ test("批间真的让出:⌈120/50⌉ = 3 次", async () => {
55
+ let yields = 0;
56
+ await enqueueBroadcast(targets(120), "hello", "hub", NET, {
57
+ isActive: () => true,
58
+ notify: () => {},
59
+ yieldBetweenChunks: async () => { yields += 1; },
60
+ });
61
+ expect(yields).toBe(Math.ceil(120 / BROADCAST_CHUNK));
62
+ });
63
+
64
+ test("自定义 chunk 生效(10 → 12 批)", async () => {
65
+ let yields = 0;
66
+ await enqueueBroadcast(targets(120), "hello", "hub", NET, {
67
+ isActive: () => true,
68
+ notify: () => {},
69
+ chunkSize: 10,
70
+ yieldBetweenChunks: async () => { yields += 1; },
71
+ });
72
+ expect(yields).toBe(12);
73
+ });
74
+
75
+ test("非 active 收件人:不入库、不推事件", async () => {
76
+ const notified: string[] = [];
77
+ const ts = targets(10);
78
+ const res = await enqueueBroadcast(ts, "hello", "hub", NET, {
79
+ // 只让偶数号 active
80
+ isActive: (alias) => Number(alias.split("-")[1]) % 2 === 0,
81
+ notify: (alias) => notified.push(alias),
82
+ yieldBetweenChunks: async () => {},
83
+ });
84
+ expect(res.ids).toHaveLength(5);
85
+ expect(notified).toHaveLength(5);
86
+ expect(notified.every(a => Number(a.split("-")[1]) % 2 === 0)).toBe(true);
87
+ });
88
+
89
+ test("空收件人列表:不写、不推、不让出", async () => {
90
+ let yields = 0;
91
+ const res = await enqueueBroadcast([], "hello", "hub", NET, {
92
+ isActive: () => true,
93
+ notify: () => {},
94
+ yieldBetweenChunks: async () => { yields += 1; },
95
+ });
96
+ expect(res.ids).toEqual([]);
97
+ expect(yields).toBe(0);
98
+ });
99
+ });
100
+
101
+ // #2721 —— /health 的 `broadcast` 一格:最近一次广播的时间/人数/批数/耗时。
102
+ describe('broadcastRecentState', () => {
103
+ test('records the last broadcast so a stall can be reconciled afterwards', async () => {
104
+ const targets = Array.from({ length: 7 }, (_, i) => ({ alias: `r${i}`, node_id: null, network_id: 'n1' }));
105
+ await enqueueBroadcast(targets, 'hello', 'hub', 'n1', {
106
+ isActive: () => true,
107
+ notify: () => {},
108
+ yieldBetweenChunks: async () => {},
109
+ chunkSize: 3,
110
+ });
111
+ const rec = broadcastRecentState();
112
+ expect(rec).not.toBeNull();
113
+ expect(rec!.recipients).toBe(7);
114
+ expect(rec!.chunks).toBe(3); // ceil(7/3)
115
+ expect(typeof rec!.took_ms).toBe('number');
116
+ expect(Number.isNaN(Date.parse(rec!.at))).toBe(false);
117
+ });
118
+ });
@@ -0,0 +1,98 @@
1
+ // #2721 —— 广播扇出:把「一条广播 = 几百次同步写 + 同一秒全员唤醒」摊开。
2
+ //
3
+ // 现场(2026-10-27 07:10–07:15,生产 `.165`,触发者是我自己的一条广播):
4
+ // 一条广播 → 384 行 inbox(每行一次 autocommit INSERT)
5
+ // → 07:11:16 起连续四次 timer 饥饿:3.5s / 20.0s / **143.9s** / 10.0s
6
+ // → RSS 639MB → 979MB → 2507MB → 2910MB,3 分钟后自行排空
7
+ // → `requests_since_last_tick` 分别是 145 / 123 / **1175** / 294
8
+ // ⇒ 当时的循环是**被打满**(8 req/s 持续),不是被阻塞。
9
+ //
10
+ // 这里只改**节奏**,不改语义:仍然逐行入库、仍然给每个收件人推事件,
11
+ // 但每 50 个收件人一批、批间让出事件循环,把「同一毫秒 384 次 commit」
12
+ // 变成「8 批、每批一次事务」。
13
+ //
14
+ // 为什么有用:真正的负载大头是收件 agent 随后各自的 report_status/get_inbox/send_reply
15
+ // (实测一条广播 ≈ 500+ 次调用)。把入库摊开几百毫秒,能让唤醒时刻自然分散,
16
+ // 而不是让 384 个 agent 在同一秒同时醒来。
17
+
18
+ import { db } from "./db.js";
19
+
20
+ /** 一批多少个收件人。50 是“一次事务不至于太长、又明显少于一次全量”的折中。 */
21
+ export const BROADCAST_CHUNK = 50;
22
+
23
+ /**
24
+ * #2721 —— 最近一次广播的「事后可对上时间的」记录。
25
+ *
26
+ * 为什么要有:07:11 那次事故里,我花了不少时间才把“143.9 秒停顿”与“07:10 那条广播”
27
+ * 对上 —— 因为停顿记录(`event_loop.recent_stalls`)里只有时间与 RSS,没有“当时发生了什么”。
28
+ * 广播是唯一一种能一次性唤醒几百个 agent 的动作,值得在 `/health` 里留一格。
29
+ */
30
+ export type BroadcastRecord = {
31
+ at: string;
32
+ recipients: number;
33
+ chunks: number;
34
+ took_ms: number;
35
+ };
36
+
37
+ let lastBroadcast: BroadcastRecord | null = null;
38
+
39
+ export function broadcastRecentState(): BroadcastRecord | null {
40
+ return lastBroadcast;
41
+ }
42
+
43
+ export type BroadcastTarget = { alias: string; node_id: string | null; network_id: string | null };
44
+ export type BroadcastDelivered = { alias: string; netId: string | null };
45
+
46
+ export type BroadcastDeps = {
47
+ /** RFC-027 §2.3:非 active 的收件人静默跳过(广播是 best-effort)。 */
48
+ isActive: (alias: string, netId: string | null) => boolean;
49
+ /** 入库之后给该收件人推一次事件(原实现用 pushEvent + pendingInboxCount)。 */
50
+ notify: (alias: string, netId: string | null) => void;
51
+ /** 测试缝:批间的让出动作。 */
52
+ yieldBetweenChunks?: () => Promise<void>;
53
+ chunkSize?: number;
54
+ };
55
+
56
+ export async function enqueueBroadcast(
57
+ targets: readonly BroadcastTarget[],
58
+ message: string,
59
+ fromSession: string,
60
+ effectiveNetId: string | null,
61
+ deps: BroadcastDeps,
62
+ ): Promise<{ ids: string[]; delivered: BroadcastDelivered[] }> {
63
+ const startedMs = Date.now();
64
+ const chunk = deps.chunkSize && deps.chunkSize > 0 ? deps.chunkSize : BROADCAST_CHUNK;
65
+ const ids: string[] = [];
66
+ const delivered: BroadcastDelivered[] = [];
67
+ const yieldNow = deps.yieldBetweenChunks ?? (() => Bun.sleep(0));
68
+
69
+ for (let i = 0; i < targets.length; i += chunk) {
70
+ const slice = targets.slice(i, i + chunk);
71
+ // 一批一个事务:把 N 次 autocommit 变成 1 次,既快又不会长时间占写锁。
72
+ db.transaction(() => {
73
+ for (const t of slice) {
74
+ const netId = effectiveNetId ?? t.network_id ?? null;
75
+ if (!deps.isActive(t.alias, netId)) continue;
76
+ const id = crypto.randomUUID();
77
+ db.run(
78
+ `INSERT INTO inbox (id, session_name, node_id, type, priority, content, from_session, network_id)
79
+ VALUES (?1, ?2, ?3, 'broadcast', 'normal', ?4, ?5, ?6)`,
80
+ [id, t.alias, t.node_id ?? null, message, fromSession, netId],
81
+ );
82
+ ids.push(id);
83
+ delivered.push({ alias: t.alias, netId });
84
+ }
85
+ });
86
+ // 批间让出:没有这一句,384 行仍然是在同一个 tick 里写完的。
87
+ await yieldNow();
88
+ }
89
+
90
+ for (const d of delivered) deps.notify(d.alias, d.netId);
91
+ lastBroadcast = {
92
+ at: new Date().toISOString(),
93
+ recipients: delivered.length,
94
+ chunks: Math.ceil(targets.length / chunk),
95
+ took_ms: Date.now() - startedMs,
96
+ };
97
+ return { ids, delivered };
98
+ }