@sleep2agi/commhub-server 0.9.0-preview.2 → 0.9.0-preview.200

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (412) 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 +374 -0
  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 +271 -0
  27. package/src/api-host-supervisors-runtime-readiness.test.ts +195 -0
  28. package/src/api-nodes-shape.test.ts +279 -0
  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 +611 -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 +379 -0
  57. package/src/create-node-validate.ts +423 -0
  58. package/src/create-node-workdir.test.ts +289 -0
  59. package/src/create-node.test.ts +354 -0
  60. package/src/create-node.ts +411 -0
  61. package/src/daemon-provider-snapshots.test.ts +421 -0
  62. package/src/daemon-provider-snapshots.ts +327 -0
  63. package/src/daemon-token-owner-http.test.ts +236 -0
  64. package/src/dashboard-slash-routing-http.test.ts +109 -0
  65. package/src/db-adapter-guard.test.ts +148 -0
  66. package/src/db-adapter-translate.test.ts +128 -0
  67. package/src/db-adapter.ts +377 -91
  68. package/src/db-timestamp.test.ts +38 -0
  69. package/src/db-timestamp.ts +15 -0
  70. package/src/db.ts +1501 -152
  71. package/src/delivered-without-push.test.ts +140 -0
  72. package/src/department-groups-http.test.ts +209 -0
  73. package/src/department-groups-sync-http.test.ts +328 -0
  74. package/src/department-groups.ts +434 -0
  75. package/src/department-heads-http.test.ts +330 -0
  76. package/src/department-heads.ts +226 -0
  77. package/src/departments-agent-count.test.ts +129 -0
  78. package/src/departments-http.test.ts +185 -0
  79. package/src/departments-mcp.test.ts +128 -0
  80. package/src/departments.ts +318 -0
  81. package/src/desktop-message-seam.test.ts +150 -0
  82. package/src/desktop-message.test.ts +368 -0
  83. package/src/dm-file-scope-http.test.ts +178 -0
  84. package/src/empty-node-sweeper.test.ts +104 -0
  85. package/src/empty-node-sweeper.ts +88 -0
  86. package/src/env-transport-gate-http.test.ts +177 -0
  87. package/src/env-transport-gate.ts +74 -0
  88. package/src/env-transport-policy.ts +21 -0
  89. package/src/event-loop-lag.test.ts +139 -0
  90. package/src/event-loop-lag.ts +150 -0
  91. package/src/ext-token-shared.test.ts +60 -0
  92. package/src/ext-validation-regression.test.ts +131 -0
  93. package/src/external-schedule-edits-http.test.ts +321 -0
  94. package/src/external-schedule-edits-timestamps.test.ts +64 -0
  95. package/src/external-schedule-edits.ts +327 -0
  96. package/src/file-download-authz-dev-open.test.ts +135 -0
  97. package/src/file-download-authz.test.ts +404 -0
  98. package/src/file-download-date-bucket.test.ts +436 -0
  99. package/src/file-network-scope-dev-open.test.ts +97 -0
  100. package/src/file-network-scope.test.ts +732 -0
  101. package/src/group-membership-events-http.test.ts +264 -0
  102. package/src/group-messages-http.test.ts +482 -0
  103. package/src/group-messages.ts +297 -0
  104. package/src/health-host-disk-2756.test.ts +49 -0
  105. package/src/health-redaction.test.ts +685 -0
  106. package/src/host-supervisor-identity-http.test.ts +152 -0
  107. package/src/host-supervisor-identity.ts +92 -0
  108. package/src/host-supervisors-daemon-version.test.ts +137 -0
  109. package/src/http-gzip.test.ts +94 -0
  110. package/src/http-gzip.ts +112 -0
  111. package/src/hub-catalog-read-http.test.ts +187 -0
  112. package/src/hub-provider-config.test.ts +139 -0
  113. package/src/hub-provider-config.ts +105 -0
  114. package/src/hub-timestamp.test.ts +38 -0
  115. package/src/hub-timestamp.ts +27 -0
  116. package/src/human-dm.ts +215 -0
  117. package/src/human-inbox.ts +42 -0
  118. package/src/inbox-caller.ts +49 -0
  119. package/src/inbox-count-sse.test.ts +310 -0
  120. package/src/inbox-count.ts +29 -0
  121. package/src/index.ts +15 -2172
  122. package/src/lifecycle-guard.ts +50 -0
  123. package/src/list-host-supervisors.test.ts +315 -0
  124. package/src/mcp-context-cost-http.test.ts +127 -0
  125. package/src/mcp-inflight.test.ts +92 -0
  126. package/src/mcp-inflight.ts +38 -0
  127. package/src/mcp-projects-events-http.test.ts +162 -0
  128. package/src/mcp-route-label-http.test.ts +120 -0
  129. package/src/mcp-write-network-resolution.test.ts +432 -0
  130. package/src/member-audit-network-id-http.test.ts +93 -0
  131. package/src/member-presence-http.test.ts +210 -0
  132. package/src/messages-ack-by-agent-http.test.ts +142 -0
  133. package/src/migrate-sqlite-to-pg.test.ts +47 -0
  134. package/src/migrate-sqlite-to-pg.ts +263 -0
  135. package/src/model-auth-notify-http.test.ts +181 -0
  136. package/src/model-auth-notify.ts +116 -0
  137. package/src/network-name-http.test.ts +75 -0
  138. package/src/network-rules-http.test.ts +169 -0
  139. package/src/network-rules.ts +129 -0
  140. package/src/network-scope.ts +276 -0
  141. package/src/network-token-membership-http.test.ts +139 -0
  142. package/src/node-alias-control.ts +12 -0
  143. package/src/node-attrs-validate.ts +88 -0
  144. package/src/node-attrs.test.ts +258 -0
  145. package/src/node-caller-identity-http.test.ts +1083 -0
  146. package/src/node-create-request-status-http.test.ts +105 -0
  147. package/src/node-daemon-bindings.test.ts +132 -0
  148. package/src/node-daemon-bindings.ts +135 -0
  149. package/src/node-failure-alert.test.ts +150 -0
  150. package/src/node-failure-alert.ts +177 -0
  151. package/src/node-files-transport.test.ts +344 -0
  152. package/src/node-health-codex-login.test.ts +120 -0
  153. package/src/node-health-dispatch-http.test.ts +279 -0
  154. package/src/node-health-guard.ts +105 -0
  155. package/src/node-health-passthrough.test.ts +115 -0
  156. package/src/node-health-store.ts +101 -0
  157. package/src/node-id-alias.test.ts +190 -0
  158. package/src/node-identity-conflict-http.test.ts +269 -0
  159. package/src/node-identity-conflict.ts +142 -0
  160. package/src/node-identity-mismatch.test.ts +74 -0
  161. package/src/node-in-flight-tasks.ts +31 -0
  162. package/src/node-lifecycle-controllable.test.ts +64 -0
  163. package/src/node-lifecycle-controllable.ts +53 -0
  164. package/src/node-lifecycle-read-http.test.ts +250 -0
  165. package/src/node-lifecycle-read.ts +124 -0
  166. package/src/node-logs-transport.test.ts +263 -0
  167. package/src/node-peer-allowlist.test.ts +79 -0
  168. package/src/node-permissions-http.test.ts +345 -0
  169. package/src/node-permissions.ts +393 -0
  170. package/src/node-request-retention.test.ts +281 -0
  171. package/src/node-request-retention.ts +120 -0
  172. package/src/node-secret-rest-http.test.ts +190 -0
  173. package/src/node-serviceability.test.ts +86 -0
  174. package/src/node-serviceability.ts +124 -0
  175. package/src/node-session-control-http.test.ts +310 -0
  176. package/src/node-session-control.ts +258 -0
  177. package/src/node-skills-transport.test.ts +209 -0
  178. package/src/node-token-identity.ts +35 -0
  179. package/src/node-token-ownership.ts +51 -0
  180. package/src/node-token-privilege-http.test.ts +158 -0
  181. package/src/not-found-api-http.test.ts +67 -0
  182. package/src/observer-avatar-http.test.ts +442 -0
  183. package/src/observer-push.test.ts +252 -0
  184. package/src/opencode-catalog-report-http.test.ts +139 -0
  185. package/src/opencode-v2-catalog.test.ts +268 -0
  186. package/src/opencode-v2-catalog.ts +352 -0
  187. package/src/package-version-consistency.test.ts +25 -0
  188. package/src/parent-source-marker.test.ts +50 -0
  189. package/src/password-network-tokens.ts +25 -0
  190. package/src/peer-reply-atomic.test.ts +455 -0
  191. package/src/peer-reply-capability.test.ts +70 -0
  192. package/src/pg-worker.ts +87 -0
  193. package/src/probe-validate.test.ts +221 -0
  194. package/src/probe-validate.ts +104 -0
  195. package/src/probe.test.ts +80 -0
  196. package/src/probe.ts +200 -0
  197. package/src/project-dir-warn-clear-http.test.ts +132 -0
  198. package/src/project-viewer-can-http.test.ts +116 -0
  199. package/src/push.test.ts +20 -10
  200. package/src/push.ts +466 -13
  201. package/src/redact-tokens.test.ts +65 -0
  202. package/src/redact-tokens.ts +40 -0
  203. package/src/rename.ts +38 -10
  204. package/src/reply-target-derivation.test.ts +96 -0
  205. package/src/report-status-handover-capabilities-http.test.ts +124 -0
  206. package/src/report-status-host-schema.test.ts +235 -0
  207. package/src/report-status-resume-id-handover.test.ts +184 -0
  208. package/src/report-status-secret-keys.test.ts +140 -0
  209. package/src/require-explicit-test-db.ts +9 -0
  210. package/src/requirement-due-reminders-http.test.ts +620 -0
  211. package/src/requirement-due-reminders.ts +416 -0
  212. package/src/requirement-events-http.test.ts +268 -0
  213. package/src/requirement-events.test.ts +45 -0
  214. package/src/requirement-events.ts +144 -0
  215. package/src/requirement-notify.ts +165 -0
  216. package/src/requirement-participant-notify-http.test.ts +309 -0
  217. package/src/requirement-tag-ops-http.test.ts +425 -0
  218. package/src/requirement-tag-tree-http.test.ts +275 -0
  219. package/src/requirement-tag-tree.ts +127 -0
  220. package/src/requirement-tags.ts +102 -0
  221. package/src/requirements-abandoned-http.test.ts +236 -0
  222. package/src/requirements-comment-http.test.ts +167 -0
  223. package/src/requirements-completed-at-migration.test.ts +70 -0
  224. package/src/requirements-errors-mcp-http.test.ts +164 -0
  225. package/src/requirements-errors.ts +158 -0
  226. package/src/requirements-etag-http.test.ts +91 -0
  227. package/src/requirements-http.test.ts +709 -0
  228. package/src/requirements-last-event-http.test.ts +349 -0
  229. package/src/requirements-list-mcp-slim-http.test.ts +155 -0
  230. package/src/requirements-list-slim-http.test.ts +324 -0
  231. package/src/requirements-mcp.test.ts +163 -0
  232. package/src/requirements-migrate.ts +283 -0
  233. package/src/requirements-owner-strict-mcp-http.test.ts +145 -0
  234. package/src/requirements-people-http.test.ts +114 -0
  235. package/src/requirements-people-mcp-http.test.ts +231 -0
  236. package/src/requirements-people.ts +196 -0
  237. package/src/requirements-priority-lowest.test.ts +114 -0
  238. package/src/requirements-projects-parent-http.test.ts +123 -0
  239. package/src/requirements-search-http.test.ts +182 -0
  240. package/src/requirements-search.test.ts +32 -0
  241. package/src/requirements-search.ts +145 -0
  242. package/src/requirements-seq-http.test.ts +193 -0
  243. package/src/requirements-seq-migration.test.ts +63 -0
  244. package/src/requirements-start-date.test.ts +125 -0
  245. package/src/requirements-stats-http.test.ts +357 -0
  246. package/src/requirements-stats.ts +195 -0
  247. package/src/requirements-status-participants-http.test.ts +222 -0
  248. package/src/requirements-upgrade-e2e.test.ts +126 -0
  249. package/src/requirements-upgrade-migration.test.ts +98 -0
  250. package/src/requirements.ts +2027 -0
  251. package/src/resolve-port.test.ts +57 -0
  252. package/src/resolve-port.ts +44 -0
  253. package/src/response-charset.test.ts +130 -0
  254. package/src/rest-alias-network-fallback-http.test.ts +123 -0
  255. package/src/rest-alias-network-fallback.test.ts +60 -0
  256. package/src/rest-alias-network-fallback.ts +65 -0
  257. package/src/rest-explicit-columns-http.test.ts +266 -0
  258. package/src/rest-identity.test.ts +130 -0
  259. package/src/rest-identity.ts +141 -0
  260. package/src/rest-projections.ts +72 -0
  261. package/src/rest-read-network-real-chain.test.ts +97 -0
  262. package/src/rest-write-network-resolution.test.ts +133 -0
  263. package/src/restart-readiness-http.test.ts +121 -0
  264. package/src/restricted-files.ts +76 -0
  265. package/src/retention-first-sweep-quiet.test.ts +31 -0
  266. package/src/retention.test.ts +100 -40
  267. package/src/retention.ts +423 -104
  268. package/src/route-callers-http.test.ts +120 -0
  269. package/src/route-timing.test.ts +211 -0
  270. package/src/route-timing.ts +262 -0
  271. package/src/rules-file-duplicate-alias.test.ts +234 -0
  272. package/src/rules-file-session-target.test.ts +207 -0
  273. package/src/rules-file-transport.test.ts +237 -0
  274. package/src/runtime-label.test.ts +22 -0
  275. package/src/runtime-label.ts +21 -0
  276. package/src/runtime-readiness.ts +90 -0
  277. package/src/schedule-agent-mcp-http.test.ts +391 -0
  278. package/src/schedule-agent.ts +192 -0
  279. package/src/schedule-starved.test.ts +92 -0
  280. package/src/schedule-starved.ts +114 -0
  281. package/src/scheduled-backoff-http.test.ts +193 -0
  282. package/src/scheduled-backoff.ts +64 -0
  283. package/src/scheduled-doorbell-nodeless-session.test.ts +134 -0
  284. package/src/scheduled-failure-backoff-2622.test.ts +145 -0
  285. package/src/scheduled-failure-code.test.ts +73 -0
  286. package/src/scheduled-failure-code.ts +105 -0
  287. package/src/scheduled-failures-http.test.ts +279 -0
  288. package/src/scheduled-failures.ts +377 -0
  289. package/src/scheduled-noprogress-backoff-2622.test.ts +133 -0
  290. package/src/scheduled-offline-backoff-probe.test.ts +56 -0
  291. package/src/scheduled-offline.test.ts +131 -0
  292. package/src/scheduled-offline.ts +66 -0
  293. package/src/scheduled-outcome-guidance-http.test.ts +139 -0
  294. package/src/scheduled-outcome-guidance.ts +9 -0
  295. package/src/scheduled-reply-unread-http.test.ts +212 -0
  296. package/src/scheduled-requirement-wake-http.test.ts +279 -0
  297. package/src/scheduled-requirement-wake.ts +125 -0
  298. package/src/scheduled-run-terminal.test.ts +268 -0
  299. package/src/scheduled-skip-blocking-task.test.ts +120 -0
  300. package/src/scheduled-skip-path-backoff-2622.test.ts +98 -0
  301. package/src/scheduled-stuck-http.test.ts +312 -0
  302. package/src/scheduled-stuck-notice-cap.test.ts +80 -0
  303. package/src/scheduled-stuck.ts +219 -0
  304. package/src/scheduled-tasks-http.test.ts +679 -0
  305. package/src/scheduled-tasks.ts +959 -0
  306. package/src/scheduled-work-outcome-http.test.ts +154 -0
  307. package/src/scheduled-work-outcome.ts +50 -0
  308. package/src/scoped-secrets.ts +139 -0
  309. package/src/send-dedup-request-id.test.ts +229 -0
  310. package/src/send-reply-agent-warning.test.ts +202 -0
  311. package/src/send-reply-alias-optional.test.ts +131 -0
  312. package/src/send-reply-attachments.test.ts +458 -0
  313. package/src/send_dedup.test.ts +54 -0
  314. package/src/send_dedup.ts +52 -6
  315. package/src/serve-error.test.ts +38 -0
  316. package/src/serve-error.ts +27 -0
  317. package/src/server.ts +5997 -0
  318. package/src/session-task-on-dispatch.ts +14 -0
  319. package/src/session-token-ownership-http.test.ts +122 -0
  320. package/src/shared/codex-inventory.ts +41 -0
  321. package/src/shared/daemon-provider-login.ts +31 -0
  322. package/src/shared/daemon-provider-snapshot.ts +52 -0
  323. package/src/shared/external-schedule-contract.ts +60 -0
  324. package/src/shared/node-name.test.ts +62 -0
  325. package/src/shared/node-name.ts +161 -0
  326. package/src/shared/probe-host-allowlist-drift.test.ts +54 -0
  327. package/src/shared/probe-host-allowlist.ts +128 -0
  328. package/src/shared/provider-application.ts +29 -0
  329. package/src/shared/provider-collection.ts +14 -0
  330. package/src/shared/provider-initialization.ts +8 -0
  331. package/src/shared/reserved-env-drift.test.ts +53 -0
  332. package/src/shared/reserved-env.test.ts +35 -0
  333. package/src/shared/reserved-env.ts +31 -0
  334. package/src/shared/scoped-secret-contract.ts +5 -0
  335. package/src/shared/task-content-limit-drift.test.ts +28 -0
  336. package/src/shared/task-content-limit.ts +25 -0
  337. package/src/side-thread-command-transport.test.ts +312 -0
  338. package/src/side-thread-command-transport.ts +318 -0
  339. package/src/side-thread-http-integration.test.ts +233 -0
  340. package/src/side-thread-http.ts +504 -0
  341. package/src/side-thread-production.test.ts +72 -0
  342. package/src/side-thread-production.ts +118 -0
  343. package/src/side-thread.test.ts +1067 -0
  344. package/src/side-thread.ts +2499 -0
  345. package/src/skillhub-http.test.ts +108 -0
  346. package/src/sqlite-close-checkpoint.test.ts +78 -0
  347. package/src/start-node.test.ts +264 -0
  348. package/src/status-alias-filter-http.test.ts +106 -0
  349. package/src/status-alias-resolver-http.test.ts +163 -0
  350. package/src/status-alias-resolver.ts +19 -0
  351. package/src/status-node-id-filter-http.test.ts +161 -0
  352. package/src/status-read-cache-http.test.ts +229 -0
  353. package/src/status-read-cache.ts +122 -0
  354. package/src/stop-delete-node.test.ts +1286 -0
  355. package/src/task-access-http.test.ts +402 -0
  356. package/src/task-access.ts +420 -0
  357. package/src/task-activity.ts +60 -0
  358. package/src/task-auth-origin-http.test.ts +93 -0
  359. package/src/task-auth-origin.test.ts +24 -0
  360. package/src/task-auth-origin.ts +7 -0
  361. package/src/task-consumption.test.ts +482 -0
  362. package/src/task-diagnostic-http.test.ts +149 -0
  363. package/src/task-diagnostic.test.ts +115 -0
  364. package/src/task-diagnostic.ts +118 -0
  365. package/src/task-expiry-notice-http.test.ts +287 -0
  366. package/src/task-expiry-notice.ts +315 -0
  367. package/src/task-idempotency-mcp.test.ts +142 -0
  368. package/src/task-idempotency.test.ts +76 -0
  369. package/src/task-idempotency.ts +81 -0
  370. package/src/task-late-replies.ts +54 -0
  371. package/src/task-late-reply-http.test.ts +152 -0
  372. package/src/task-lifecycle-watcher-busy.test.ts +114 -0
  373. package/src/task-lifecycle-watcher.test.ts +204 -0
  374. package/src/task-lifecycle-watcher.ts +109 -0
  375. package/src/task-network-resolution-http.test.ts +140 -0
  376. package/src/task-offline-delivery-2668.test.ts +119 -0
  377. package/src/task-offline-delivery.ts +81 -0
  378. package/src/task-orphan-http.test.ts +287 -0
  379. package/src/task-orphan.ts +119 -0
  380. package/src/task-queue-ahead-http.test.ts +309 -0
  381. package/src/task-queue-ahead-scale.test.ts +236 -0
  382. package/src/task-queue-ahead.ts +186 -0
  383. package/src/task-rejection-log-http.test.ts +160 -0
  384. package/src/task-rejection-log.ts +49 -0
  385. package/src/task-sender-http.test.ts +143 -0
  386. package/src/task-stale-open-http.test.ts +290 -0
  387. package/src/task-stale-open.ts +103 -0
  388. package/src/tasks-pagination-http.test.ts +141 -0
  389. package/src/team-rules-status-http.test.ts +90 -0
  390. package/src/team-rules-status.test.ts +59 -0
  391. package/src/team-rules-status.ts +72 -0
  392. package/src/tool-audience-http.test.ts +230 -0
  393. package/src/tool-audience.ts +120 -0
  394. package/src/tools-list-json-schema.test.ts +94 -0
  395. package/src/tools.ts +5815 -348
  396. package/src/update-provider.test.ts +234 -0
  397. package/src/upload-early-reject-guard.test.ts +155 -0
  398. package/src/uploads-http.test.ts +97 -15
  399. package/src/uploads.test.ts +66 -0
  400. package/src/uploads.ts +138 -5
  401. package/src/user-avatar-http.test.ts +74 -0
  402. package/src/user-avatar-image.ts +160 -0
  403. package/src/user-avatar-migration.test.ts +25 -0
  404. package/src/user-avatar-upload-http.test.ts +348 -0
  405. package/src/user-inbox-by-agent-http.test.ts +161 -0
  406. package/src/user-inbox-netid-notnull.test.ts +122 -0
  407. package/src/user-inbox-read-http.test.ts +223 -0
  408. package/src/user-inbox-schema.test.ts +54 -0
  409. package/src/vault.test.ts +200 -0
  410. package/src/vault.ts +156 -0
  411. package/src/version-stats.test.ts +66 -0
  412. package/src/version-stats.ts +109 -0
package/src/push.ts CHANGED
@@ -59,6 +59,24 @@ function envNum(k: string, fallback: number): number {
59
59
  * half-open consumer that has stopped reading entirely. */
60
60
  const MAX_QUEUE_BACKPRESSURE_BYTES = envNum("ANET_SSE_MAX_QUEUE_BYTES", 1_000_000);
61
61
 
62
+ /** #2363 —— 订阅者“已经落后多少字节”就不再给它非关键事件。
63
+ *
64
+ * 背景(2026-10-26 实测):150 条慢订阅者 + 30 msg/s 广播,RSS 峰值可到 2 GB;
65
+ * 每条连接的上限只是 1 MB,但 300 条 × 1 MB 就是 GB 级。与其把上限一路压到底,
66
+ * 更合理的是:**明显落后的连接直接跳过非关键事件**(keepalive 与 connected 必须保留,
67
+ * 否则 liveness 探测和重连握手会失效),并计数 `sse_dropped_degraded`。
68
+ *
69
+ * 这不是“丢消息”的新语义:SSE 本来就是 best-effort,客户端重连后会拉一次快照;
70
+ * 真正的问题是把“慢消费者”变成“Hub 的内存账”。 */
71
+ const COALESCE_AT_BYTES = envNum("ANET_SSE_COALESCE_AT_BYTES", 65_536);
72
+
73
+ /** #2363 —— 所有广播共用同一个 TextEncoder:编码结果只读、不跨事件复用同一 Uint8Array。 */
74
+ const SHARED_ENCODER = new TextEncoder();
75
+
76
+ /** 被跳过的事件计数(进程内累计,供 /health 与测试读取)。 */
77
+ let droppedDegraded = 0;
78
+ export function sseDegradedDropCount(): number { return droppedDegraded; }
79
+
62
80
  /** How long `desiredSize` can stay negative (i.e. consumer behind HWM)
63
81
  * before we declare the client dead. 60s by default — past 2× the
64
82
  * keepalive interval, so a live-but-slow consumer wouldn't trip it. */
@@ -84,6 +102,40 @@ type SSEClient = {
84
102
  keepaliveTimer?: ReturnType<typeof setInterval>;
85
103
  /** Stable key for log/diagnostic context. */
86
104
  key: string;
105
+ /** 多用户 Agent 权限:订阅者的 user_id(有的话),权限变化时据此断开重连重新鉴权。 */
106
+ subscriberUserId?: string;
107
+ /** 只推满足条件的事件(受限成员的网络观察流用)。缺省 = 全推。 */
108
+ eventFilter?: (event: Record<string, unknown>) => boolean;
109
+ /** #507 — set only on a node's OWN task stream (/events/<alias> opened with the
110
+ * node token bound to that alias). Dashboard monitors / master-token streams on
111
+ * the same key leave it unset and are never superseded or counted here. */
112
+ node?: NodeSubscriberInfo;
113
+ };
114
+
115
+ /** #507 — what the Hub knows about one node-scoped SSE connection. No tokens. */
116
+ export type NodeSubscriberInfo = {
117
+ nodeId: string | null;
118
+ /** Optional, client-supplied (`X-Anet-Instance-Id` header or `?instance_id=`).
119
+ * null = the client did not say (every agent-node released so far). */
120
+ instanceId: string | null;
121
+ remote: string | null;
122
+ userAgent: string | null;
123
+ connectedAt: number;
124
+ };
125
+
126
+ /** #507 — one supersede of a node-scoped connection by a newer one. */
127
+ export type NodeIdentityConflict = {
128
+ key: string;
129
+ alias: string;
130
+ networkId: string | null;
131
+ nodeId: string | null;
132
+ /** "same" = both connections carried the same instance id (a reconnect of the
133
+ * same process; not reported as a conflict). "different" = both carried ids and
134
+ * they differ (two live copies). "unknown" = at least one carried none. */
135
+ instanceMatch: "same" | "different" | "unknown";
136
+ superseded: NodeSubscriberInfo;
137
+ current: NodeSubscriberInfo;
138
+ at: number;
87
139
  };
88
140
 
89
141
  // 一个 session 可能有多个 SSE 连接(重连时短暂并存)
@@ -97,6 +149,45 @@ function clientKey(sessionName: string, networkId?: string | null): string {
97
149
  return `${networkId || "global"}:${sessionName}`;
98
150
  }
99
151
 
152
+ // ── #461 network observer channels ───────────────────────────────────
153
+ // Dashboard (or any network member) subscribes GET /events/network/:id
154
+ // and receives SUMMARY events for ALL new_task / new_reply traffic in
155
+ // that network — not just tasks it sent/received. Summary events carry
156
+ // ids + routing metadata only, never task/reply content.
157
+ //
158
+ // Key scheme: leading NUL byte makes an observer key impossible to
159
+ // forge from the session scheme above (`${networkId}:${sessionName}`)
160
+ // for any networkId the hub itself issues (net_* / "global"). Only a
161
+ // master-token caller could even inject a NUL into network_id via the
162
+ // legacy ?network_id= path, and master token is root already.
163
+ const OBSERVER_KEY_PREFIX = "\u0000netobs:";
164
+ const USER_KEY_PREFIX = "\u0000user:";
165
+
166
+ /** Printable form of `OBSERVER_KEY_PREFIX` — the shape emitted by
167
+ * `printableKey()` (below) and therefore the shape callers see via
168
+ * `getSSEStats().sessions`. Exported so downstream consumers (e.g.
169
+ * `/health` filter in server.ts) key off the same literal instead of
170
+ * each side re-writing the escape sequence — CR3 audit 4703b0e7
171
+ * found server.ts had `"netobs:"` (no `\\0`) which never matched and
172
+ * silently over-filtered observer keys. */
173
+ export const PRINTABLE_OBSERVER_KEY_PREFIX = "\\0netobs:";
174
+
175
+ function observerKey(networkId: string): string {
176
+ return `${OBSERVER_KEY_PREFIX}${networkId}`;
177
+ }
178
+
179
+ function userKey(networkId: string, userId: string): string {
180
+ return `${USER_KEY_PREFIX}${networkId}:${userId}`;
181
+ }
182
+
183
+ /** Render a client key for logs / stats. Observer keys embed a raw NUL
184
+ * byte (collision guard above); printing that verbatim makes grep treat
185
+ * hub logs as binary and breaks log shippers, and getSSEStats feeds
186
+ * /health. Display as a visible "\\0" escape instead. */
187
+ function printableKey(key: string): string {
188
+ return key.split("\u0000").join("\\0");
189
+ }
190
+
100
191
  // ─────────────────────────────────────────────────────────────────────
101
192
  // Close + dispatch helpers
102
193
  // ─────────────────────────────────────────────────────────────────────
@@ -114,13 +205,20 @@ function closeClient(client: SSEClient, reason: string): void {
114
205
  // Already closed by the runtime, or controller errored. Either way
115
206
  // we're done with it.
116
207
  }
117
- console.log(`[${ts()}] SSE ✕ ${client.key} closed (reason=${reason})`);
208
+ console.log(`[${ts()}] SSE ✕ ${printableKey(client.key)} closed (reason=${reason})`);
209
+ if (client.node && reason !== SUPERSEDED_REASON && !hasLiveNodeClient(client.key)) {
210
+ notifyNodeDrained(client.key);
211
+ }
212
+ if (isUserStreamClient(client)) {
213
+ touchUserSeen(client.subscriberUserId!);
214
+ if (!isUserOnline(client.subscriberUserId!)) notifyPresence(client.subscriberUserId!, false);
215
+ }
118
216
  }
119
217
 
120
218
  /** Best-effort enqueue with backpressure guard. Returns:
121
219
  * - "ok" → bytes accepted, queue healthy
122
220
  * - "dead" → client closed; caller must remove from map */
123
- function tryEnqueueBytes(client: SSEClient, bytes: Uint8Array): "ok" | "dead" {
221
+ function tryEnqueueBytes(client: SSEClient, bytes: Uint8Array, opts: { critical?: boolean } = {}): "ok" | "dead" {
124
222
  if (client.closed) return "dead";
125
223
 
126
224
  const ctrl = client.controller;
@@ -139,6 +237,14 @@ function tryEnqueueBytes(client: SSEClient, bytes: Uint8Array): "ok" | "dead" {
139
237
  return "dead";
140
238
  }
141
239
 
240
+ // #2363 —— 落后超过 COALESCE_AT_BYTES 且不是关键帧:跳过,不排队、也不关连接
241
+ // (上限之上的仍然按硬顶关闭,见上一段)。这样“慢消费者”不再把内存账单放大到
242
+ // 连接数 × 单连接上限,同时保留原有“彻底读不动就断开回收”的语义。
243
+ if (!opts.critical && desired < -COALESCE_AT_BYTES) {
244
+ droppedDegraded += 1;
245
+ return "ok";
246
+ }
247
+
142
248
  // Track stuck-since-when for the soft timeout below.
143
249
  if (desired < 0) {
144
250
  if (client.stuckSince === null) {
@@ -213,10 +319,144 @@ function ensureLivenessSweep(): void {
213
319
  // Public API
214
320
  // ─────────────────────────────────────────────────────────────────────
215
321
 
216
- /** 创建 SSE Response 并注册到 clients map */
217
- export function createSSEStream(sessionName: string, networkId?: string | null): Response {
322
+ /** 创建 SSE Response 并注册到 clients map。
323
+ * #507 — pass `opts.node` only for a node's own stream (node token bound to this
324
+ * alias); such a stream supersedes any older node-scoped stream on the same key. */
325
+ export function createSSEStream(
326
+ sessionName: string,
327
+ networkId?: string | null,
328
+ opts: { node?: Omit<NodeSubscriberInfo, "connectedAt"> } = {},
329
+ ): Response {
330
+ return createStreamForKey(
331
+ clientKey(sessionName, networkId),
332
+ { type: "connected", session: sessionName, network_id: networkId ?? null },
333
+ opts.node ? { node: { ...opts.node, connectedAt: Date.now() } } : {},
334
+ );
335
+ }
336
+
337
+ // ── #507 node identity conflict: at most ONE node-scoped subscriber per key ──
338
+ //
339
+ // Before #507 every connection on `${networkId}:${alias}` received every push. A
340
+ // node directory copied verbatim (same node token, same alias) and started
341
+ // elsewhere therefore got every new_task twice — both copies ran it — and either
342
+ // copy's shutdown report_status(offline) marked the node offline while the other
343
+ // was still connected.
344
+ //
345
+ // Policy: NEWEST CONNECTION WINS. Measured reconnect path (agent-node connectSSE →
346
+ // superviseChild): a restarted process's old socket is closed by the OS, so a
347
+ // legit restart never overlaps its own previous stream; an overlap only happens
348
+ // with (a) a half-open ghost the Hub has not reaped yet (up to STUCK_CLOSE_MS) or
349
+ // (b) a second live copy. In (a) the new connection is the real one — keeping the
350
+ // old would park tasks on a dead socket — so the newest must win. In (b) the
351
+ // loser reconnects ~1 s later and wins back: the copies flap, which is loud
352
+ // (log + node_identity_conflict event + /api/stats/sse) instead of silently
353
+ // double-executing. An "only if both alive > N s" rule cannot tell (a) from (b):
354
+ // a ghost looks alive to the Hub until it is reaped.
355
+ export const SUPERSEDED_REASON = "superseded_by_new_connection";
356
+ const REPLACED_REASON = "replaced_by_reconnect";
357
+
358
+ let nodeConflictListener: ((c: NodeIdentityConflict) => void) | null = null;
359
+ let nodeDrainedListener: ((alias: string, networkId: string | null) => void) | null = null;
360
+
361
+ /** Called for every supersede whose instances are not provably the same process. */
362
+ export function onNodeIdentityConflict(cb: ((c: NodeIdentityConflict) => void) | null): void {
363
+ nodeConflictListener = cb;
364
+ }
365
+
366
+ /** Called when the LAST node-scoped subscriber of a key goes away (not when it is
367
+ * superseded — the newer connection is still there). */
368
+ export function onNodeSubscribersDrained(cb: ((alias: string, networkId: string | null) => void) | null): void {
369
+ nodeDrainedListener = cb;
370
+ }
371
+
372
+ function splitClientKey(key: string): { alias: string; networkId: string | null } {
373
+ const i = key.indexOf(":");
374
+ const net = key.slice(0, i);
375
+ return { alias: key.slice(i + 1), networkId: net === "global" ? null : net };
376
+ }
377
+
378
+ function hasLiveNodeClient(key: string): boolean {
379
+ return (clients.get(key) ?? []).some((c) => !c.closed && !!c.node);
380
+ }
381
+
382
+ function notifyNodeDrained(key: string): void {
383
+ if (!nodeDrainedListener) return;
384
+ const { alias, networkId } = splitClientKey(key);
385
+ try { nodeDrainedListener(alias, networkId); } catch (e: any) {
386
+ console.log(`[${ts()}] node drained listener failed: ${e?.message || e}`);
387
+ }
388
+ }
389
+
390
+ /** Is a node-scoped (node-token) subscriber connected on this alias right now? */
391
+ export function hasNodeSubscriber(sessionName: string, networkId?: string | null): boolean {
392
+ return hasLiveNodeClient(clientKey(sessionName, networkId));
393
+ }
394
+
395
+ /** Keep only `keep` among the node-scoped clients of `key`; close the others with
396
+ * a final `node_connection_superseded` frame. Dashboard monitors are untouched. */
397
+ function enforceSingleNodeSubscriber(key: string, keep: SSEClient): void {
398
+ const arr = clients.get(key);
399
+ if (!arr || !keep.node) return;
400
+ for (const old of arr) {
401
+ if (old === keep || old.closed || !old.node) continue;
402
+ const a = old.node.instanceId, b = keep.node.instanceId;
403
+ const instanceMatch: NodeIdentityConflict["instanceMatch"] = a && b ? (a === b ? "same" : "different") : "unknown";
404
+ const reason = instanceMatch === "same" ? REPLACED_REASON : SUPERSEDED_REASON;
405
+ tryEnqueueBytes(old, old.encoder.encode(`data: ${JSON.stringify({
406
+ type: "node_connection_superseded",
407
+ reason,
408
+ instance_match: instanceMatch,
409
+ // The newer connection's network origin, so the loser's log says WHERE the
410
+ // other copy runs. Never a token.
411
+ by: { instance_id: keep.node.instanceId, remote: keep.node.remote },
412
+ })}\n\n`));
413
+ closeClient(old, reason);
414
+ if (instanceMatch !== "same" && nodeConflictListener) {
415
+ const { alias, networkId } = splitClientKey(key);
416
+ try {
417
+ nodeConflictListener({
418
+ key: printableKey(key), alias, networkId,
419
+ nodeId: keep.node.nodeId ?? old.node.nodeId,
420
+ instanceMatch, superseded: old.node, current: keep.node, at: Date.now(),
421
+ });
422
+ } catch (e: any) {
423
+ console.log(`[${ts()}] node conflict listener failed: ${e?.message || e}`);
424
+ }
425
+ }
426
+ }
427
+ pruneClosed(key);
428
+ }
429
+
430
+ /** #461 — 创建网络级观察者 SSE Response(dashboard 观察第三方流量)。
431
+ * Shares the exact same registration / backpressure / liveness
432
+ * machinery as session streams; only the key scheme differs. */
433
+ export function createNetworkObserverStream(
434
+ networkId: string,
435
+ opts: { subscriberUserId?: string; eventFilter?: (event: Record<string, unknown>) => boolean } = {},
436
+ ): Response {
437
+ return createStreamForKey(
438
+ observerKey(networkId),
439
+ { type: "connected", observer: true, network_id: networkId },
440
+ opts,
441
+ );
442
+ }
443
+
444
+ /** Desktop/user client SSE stream. Keyed by authenticated user_id, not
445
+ * alias/session, so Desktop does not need to mint a pseudo node token. */
446
+ export function createUserEventStream(networkId: string, userId: string): Response {
447
+ return createStreamForKey(
448
+ userKey(networkId, userId),
449
+ { type: "connected", user: true, network_id: networkId, user_id: userId },
450
+ { subscriberUserId: userId },
451
+ );
452
+ }
453
+
454
+ function createStreamForKey(
455
+ key: string,
456
+ initialEvent: Record<string, unknown>,
457
+ opts: { subscriberUserId?: string; eventFilter?: (event: Record<string, unknown>) => boolean; node?: NodeSubscriberInfo } = {},
458
+ ): Response {
218
459
  const encoder = new TextEncoder();
219
- const key = clientKey(sessionName, networkId);
220
460
  let client: SSEClient;
221
461
 
222
462
  const stream = new ReadableStream<Uint8Array>({
@@ -227,17 +467,25 @@ export function createSSEStream(sessionName: string, networkId?: string | null):
227
467
  stuckSince: null,
228
468
  closed: false,
229
469
  key,
470
+ ...(opts.subscriberUserId ? { subscriberUserId: opts.subscriberUserId } : {}),
471
+ ...(opts.eventFilter ? { eventFilter: opts.eventFilter } : {}),
472
+ ...(opts.node ? { node: opts.node } : {}),
230
473
  };
231
474
 
475
+ const presenceUser = isUserStreamClient(client) ? client.subscriberUserId! : null;
476
+ const wasOnline = presenceUser ? isUserOnline(presenceUser) : true;
232
477
  if (!clients.has(key)) clients.set(key, []);
233
478
  clients.get(key)!.push(client);
234
- console.log(`[${ts()}] SSE ← ${key} connected (${clients.get(key)!.length} clients)`);
479
+ if (presenceUser) touchUserSeen(presenceUser);
480
+ console.log(`[${ts()}] SSE ← ${printableKey(key)} connected (${clients.get(key)!.length} clients)`);
481
+ if (client.node) enforceSingleNodeSubscriber(key, client);
235
482
 
236
483
  // Send initial connected frame through the backpressure guard so
237
484
  // even the first byte respects the contract.
238
485
  tryEnqueueBytes(
239
486
  client,
240
- encoder.encode(`data: ${JSON.stringify({ type: "connected", session: sessionName, network_id: networkId ?? null })}\n\n`),
487
+ encoder.encode(`data: ${JSON.stringify(initialEvent)}\n\n`),
488
+ { critical: true },
241
489
  );
242
490
 
243
491
  // Periodic keepalive doubles as a half-open probe — if the
@@ -248,7 +496,8 @@ export function createSSEStream(sessionName: string, networkId?: string | null):
248
496
  if (client.keepaliveTimer) clearInterval(client.keepaliveTimer);
249
497
  return;
250
498
  }
251
- const result = tryEnqueueBytes(client, encoder.encode(`: keepalive\n\n`));
499
+ const result = tryEnqueueBytes(client, encoder.encode(`: keepalive\n\n`), { critical: true });
500
+ if (result === "ok" && isUserStreamClient(client)) touchUserSeen(client.subscriberUserId!);
252
501
  if (result === "dead") {
253
502
  // closeClient already cleared the timer; just make sure we
254
503
  // also prune from the map.
@@ -258,6 +507,8 @@ export function createSSEStream(sessionName: string, networkId?: string | null):
258
507
  (client.keepaliveTimer as any)?.unref?.();
259
508
 
260
509
  ensureLivenessSweep();
510
+ // 在线状态的广播放在 connected 帧之后:订阅方先拿到自己的 connected,再看到别人的变化。
511
+ if (presenceUser && !wasOnline) notifyPresence(presenceUser, true);
261
512
  },
262
513
  cancel() {
263
514
  // Consumer side hangup — straightforward path, mark closed and
@@ -280,7 +531,12 @@ export function createSSEStream(sessionName: string, networkId?: string | null):
280
531
 
281
532
  return new Response(stream, {
282
533
  headers: {
283
- "Content-Type": "text/event-stream",
534
+ // #426: explicit charset so legacy Windows clients (PowerShell 5.1's
535
+ // Invoke-RestMethod / Invoke-WebRequest default to ISO-8859-1 when
536
+ // Content-Type omits the charset) decode UTF-8 payloads correctly.
537
+ // Hub itself was already writing clean UTF-8 bytes; the client-side
538
+ // 双重编码样式 mojibake was the header telling the client to guess.
539
+ "Content-Type": "text/event-stream; charset=utf-8",
284
540
  "Cache-Control": "no-cache, no-transform",
285
541
  "Connection": "keep-alive",
286
542
  },
@@ -294,7 +550,7 @@ function pruneClosed(key: string): void {
294
550
  const alive = arr.filter((c) => !c.closed);
295
551
  if (alive.length === 0) {
296
552
  clients.delete(key);
297
- console.log(`[${ts()}] SSE ✕ ${key} disconnected (0 remaining)`);
553
+ console.log(`[${ts()}] SSE ✕ ${printableKey(key)} disconnected (0 remaining)`);
298
554
  } else if (alive.length !== arr.length) {
299
555
  clients.set(key, alive);
300
556
  }
@@ -312,9 +568,16 @@ function rekeyClient(oldSessionName: string, newSessionName: string, networkId?:
312
568
  for (const c of oldClients) c.key = newKey;
313
569
 
314
570
  const existing = clients.get(newKey) || [];
315
- clients.set(newKey, existing.concat(oldClients));
571
+ const merged = existing.concat(oldClients);
572
+ clients.set(newKey, merged);
316
573
  clients.delete(oldKey);
317
574
  console.log(`[${ts()}] SSE ↔ rekey ${oldKey} → ${newKey} (${oldClients.length} clients)`);
575
+ // #507 — a rename can merge two node-scoped streams onto one key; newest wins.
576
+ const nodeClients = merged.filter((c) => !c.closed && c.node);
577
+ if (nodeClients.length > 1) {
578
+ const newest = nodeClients.reduce((a, b) => (b.node!.connectedAt >= a.node!.connectedAt ? b : a));
579
+ enforceSingleNodeSubscriber(newKey, newest);
580
+ }
318
581
  return oldClients.length;
319
582
  }
320
583
 
@@ -327,12 +590,26 @@ eventBus.on("rename-committed", (event: RenameCommittedEvent) => {
327
590
  });
328
591
 
329
592
  /** 推送事件给指定 session 的所有 SSE 连接 */
593
+ /** 这个 alias 上此刻是否真的有活着的 SSE 订阅者。
594
+ *
595
+ * 这是投递可达性的**实测**,区别于 resolveDeliveryTarget 里那个基于
596
+ * last_seen_at 的**猜测**。一个安静但连着的会话(例如遵守「停发心跳」
597
+ * 规则的 MCP 会话)在时间戳判据下是 offline,在这里是 true。 */
598
+ export function hasSubscribers(sessionName: string, networkId?: string | null): boolean {
599
+ const arr = clients.get(clientKey(sessionName, networkId));
600
+ if (!arr) return false;
601
+ return arr.some((c) => !c.closed);
602
+ }
603
+
330
604
  export function pushEvent(sessionName: string, event: Record<string, unknown>, networkId?: string | null): void {
331
605
  const key = clientKey(sessionName, networkId ?? (event.network_id as string | null | undefined));
332
606
  const arr = clients.get(key);
333
607
  if (!arr || arr.length === 0) return;
334
608
 
335
609
  const data = `data: ${JSON.stringify(event)}\n\n`;
610
+ // #2363 —— 一次编码,所有订阅者共用同一份字节:150 条连接 × 每秒几十条事件时,
611
+ // 每连接重新 encode 一次会产生大量短命 Uint8Array(实测 150×30/s 时 RSS 峰值 2 GB)。
612
+ const bytes = SHARED_ENCODER.encode(data);
336
613
  let needPrune = false;
337
614
 
338
615
  for (const c of arr) {
@@ -340,18 +617,192 @@ export function pushEvent(sessionName: string, event: Record<string, unknown>, n
340
617
  needPrune = true;
341
618
  continue;
342
619
  }
343
- const result = tryEnqueueBytes(c, c.encoder.encode(data));
620
+ const result = tryEnqueueBytes(c, bytes);
344
621
  if (result === "dead") needPrune = true;
345
622
  }
346
623
 
347
624
  if (needPrune) pruneClosed(key);
348
625
  }
349
626
 
627
+ /** #461 — 推送网络级摘要事件给该网络的所有观察者连接。
628
+ *
629
+ * Callers pass ROUTING METADATA ONLY (task_id / from / to / status /
630
+ * priority / message_id) — never task or reply content. The event is
631
+ * stamped with `network_id` + `scope: "network"` here so observers can
632
+ * always tell which network the event belongs to and that it came from
633
+ * the observer stream rather than their personal channel.
634
+ *
635
+ * No-op when networkId is falsy: legacy null-network traffic has no
636
+ * observer stream (the route requires an explicit network id). */
637
+ export function pushNetworkObserverEvent(
638
+ networkId: string | null | undefined,
639
+ event: Record<string, unknown>,
640
+ ): void {
641
+ if (!networkId) return;
642
+ const key = observerKey(networkId);
643
+ const arr = clients.get(key);
644
+ if (!arr || arr.length === 0) return;
645
+
646
+ const data = `data: ${JSON.stringify({ ...event, network_id: networkId, scope: "network" })}\n\n`;
647
+ const bytes = SHARED_ENCODER.encode(data); // #2363:一次编码,所有 observer 共用
648
+ let needPrune = false;
649
+
650
+ for (const c of arr) {
651
+ if (c.closed) {
652
+ needPrune = true;
653
+ continue;
654
+ }
655
+ if (c.eventFilter) {
656
+ let pass = false;
657
+ try { pass = c.eventFilter(event); } catch { pass = false; }
658
+ if (!pass) continue;
659
+ }
660
+ const result = tryEnqueueBytes(c, bytes);
661
+ if (result === "dead") needPrune = true;
662
+ }
663
+
664
+ if (needPrune) pruneClosed(key);
665
+ }
666
+
667
+ /** Push an event to every active Desktop/web client for one user in one
668
+ * network. No-op for falsy network/user ids; auth must be enforced by
669
+ * the caller before invoking this helper. */
670
+ /** 这个用户此刻是否真的有活着的 SSE 订阅者。
671
+ *
672
+ * 与 alias 版 `hasSubscribers` 同形状:投递可达性的**实测**,
673
+ * 调用方据此把「送到了」和「没人听」区分开,而不是一律报成功。 */
674
+ export function hasUserSubscribers(
675
+ networkId: string | null | undefined,
676
+ userId: string | null | undefined,
677
+ ): boolean {
678
+ if (!networkId || !userId) return false;
679
+ const arr = clients.get(userKey(networkId, userId));
680
+ if (!arr) return false;
681
+ return arr.some((c) => !c.closed);
682
+ }
683
+
684
+ export function pushUserEvent(
685
+ networkId: string | null | undefined,
686
+ userId: string | null | undefined,
687
+ event: Record<string, unknown>,
688
+ ): void {
689
+ if (!networkId || !userId) return;
690
+ const key = userKey(networkId, userId);
691
+ const arr = clients.get(key);
692
+ if (!arr || arr.length === 0) return;
693
+
694
+ const data = `data: ${JSON.stringify({ ...event, network_id: networkId, user_id: userId, scope: "user" })}\n\n`;
695
+ const bytes = SHARED_ENCODER.encode(data); // #2363 同 pushEvent:一次编码共用
696
+ let needPrune = false;
697
+
698
+ for (const c of arr) {
699
+ if (c.closed) {
700
+ needPrune = true;
701
+ continue;
702
+ }
703
+ const result = tryEnqueueBytes(c, bytes);
704
+ if (result === "dead") needPrune = true;
705
+ }
706
+
707
+ if (needPrune) pruneClosed(key);
708
+ }
709
+
710
+ // ── 人类成员在线状态(人员列表的在线点)──────────────────────────────────
711
+ // 判据就是上面这张用户流(/events/users/me)注册表:此刻有活着的用户流 = 在线。按**用户**算,
712
+ // 不按网络 —— 桌面端同一时刻只连当前网络的用户流,按网络算会把正在别的网络里的人画成离线。
713
+ // last_seen 只在内存:连上、每次 keepalive 成功、断开时刷新。Hub 重启后清空(未知 = null,
714
+ // 调用方不要把 null 画成「很久以前」)。
715
+
716
+ const userLastSeen = new Map<string, number>();
717
+ let presenceListener: ((userId: string, online: boolean, lastSeenAt: string | null) => void) | null = null;
718
+
719
+ function isUserStreamClient(client: SSEClient): boolean {
720
+ return !!client.subscriberUserId && client.key.startsWith(USER_KEY_PREFIX);
721
+ }
722
+
723
+ function touchUserSeen(userId: string): void {
724
+ userLastSeen.set(userId, Date.now());
725
+ }
726
+
727
+ function isUserOnline(userId: string): boolean {
728
+ for (const [key, arr] of clients) {
729
+ if (!key.startsWith(USER_KEY_PREFIX)) continue;
730
+ if (arr.some((c) => !c.closed && c.subscriberUserId === userId)) return true;
731
+ }
732
+ return false;
733
+ }
734
+
735
+ function lastSeenIso(userId: string): string | null {
736
+ const ms = userLastSeen.get(userId);
737
+ return ms === undefined ? null : new Date(ms).toISOString();
738
+ }
739
+
740
+ function notifyPresence(userId: string, online: boolean): void {
741
+ if (!presenceListener) return;
742
+ try {
743
+ presenceListener(userId, online, lastSeenIso(userId));
744
+ } catch (e: any) {
745
+ console.log(`[${ts()}] presence listener failed: ${e?.message || e}`);
746
+ }
747
+ }
748
+
749
+ /** 用户的第一条用户流连上 / 最后一条断开时回调(server.ts 用它向同网络成员推 member_presence)。 */
750
+ export function onUserPresenceChange(cb: ((userId: string, online: boolean, lastSeenAt: string | null) => void) | null): void {
751
+ presenceListener = cb;
752
+ }
753
+
754
+ /** 一批用户此刻的在线状态。鉴权由调用方负责。 */
755
+ export function getUserPresence(userIds: readonly string[]): Map<string, { online: boolean; last_seen_at: string | null }> {
756
+ const online = new Set<string>();
757
+ for (const [key, arr] of clients) {
758
+ if (!key.startsWith(USER_KEY_PREFIX)) continue;
759
+ for (const c of arr) if (!c.closed && c.subscriberUserId) online.add(c.subscriberUserId);
760
+ }
761
+ const out = new Map<string, { online: boolean; last_seen_at: string | null }>();
762
+ for (const uid of userIds) out.set(uid, { online: online.has(uid), last_seen_at: lastSeenIso(uid) });
763
+ return out;
764
+ }
765
+
766
+ /** 推给某网络里所有连着用户流的人(可排除一个)。鉴权由调用方负责。 */
767
+ export function pushUserEventToNetwork(networkId: string, event: Record<string, unknown>, exceptUserId?: string): number {
768
+ const prefix = `${USER_KEY_PREFIX}${networkId}:`;
769
+ const recipients: string[] = [];
770
+ for (const key of clients.keys()) {
771
+ if (!key.startsWith(prefix)) continue;
772
+ const uid = key.slice(prefix.length);
773
+ if (uid && uid !== exceptUserId) recipients.push(uid);
774
+ }
775
+ for (const uid of recipients) pushUserEvent(networkId, uid, event);
776
+ return recipients.length;
777
+ }
778
+
779
+ /**
780
+ * 多用户 Agent 权限:某用户在某网络的授权 / 成员资格变了,断开他在这个网络里**订阅时带了身份**
781
+ * 的流(网络观察流、用户流),客户端重连时按新权限重新鉴权。返回断开的条数。
782
+ * Agent 频道(/events/<alias>)受限成员本来就订阅不了,不需要这里处理。
783
+ */
784
+ export function closeUserStreamsInNetwork(networkId: string, userId: string): number {
785
+ let closed = 0;
786
+ for (const key of [observerKey(networkId), userKey(networkId, userId)]) {
787
+ const arr = clients.get(key);
788
+ if (!arr) continue;
789
+ for (const c of arr) {
790
+ if (!c.closed && c.subscriberUserId === userId) {
791
+ closeClient(c, "access-changed");
792
+ closed++;
793
+ }
794
+ }
795
+ pruneClosed(key);
796
+ }
797
+ return closed;
798
+ }
799
+
350
800
  export function __resetSSEClientsForTest(): void {
351
801
  for (const arr of clients.values()) {
352
802
  for (const c of arr) closeClient(c, "reset-for-test");
353
803
  }
354
804
  clients.clear();
805
+ userLastSeen.clear();
355
806
  if (livenessSweepTimer) {
356
807
  clearInterval(livenessSweepTimer);
357
808
  livenessSweepTimer = null;
@@ -365,7 +816,7 @@ export function getSSEStats(): { total: number; sessions: Record<string, number>
365
816
  for (const [name, arr] of clients) {
366
817
  const alive = arr.filter((c) => !c.closed);
367
818
  if (alive.length === 0) continue;
368
- sessions[name] = alive.length;
819
+ sessions[printableKey(name)] = alive.length;
369
820
  total += alive.length;
370
821
  }
371
822
  return { total, sessions };
@@ -378,6 +829,8 @@ export const __SSE_INTERNALS_FOR_TEST = {
378
829
  sweepLiveness,
379
830
  closeClient,
380
831
  MAX_QUEUE_BACKPRESSURE_BYTES,
832
+ COALESCE_AT_BYTES,
833
+ degradedDropCount: sseDegradedDropCount,
381
834
  STUCK_CLOSE_MS,
382
835
  KEEPALIVE_MS,
383
836
  LIVENESS_SWEEP_MS,
@@ -0,0 +1,65 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { redactTokens, redactMessageRow } from "./redact-tokens.js";
3
+
4
+ describe("#1459 ③ redact-at-read", () => {
5
+ test("🔴 各种前缀形状都被遮住,且前缀保留(读者仍知道泄漏的是哪一类)", () => {
6
+ const cases: Array<[string, string]> = [
7
+ ["ntok_abcdef123456", "ntok_"],
8
+ ["utok_abcdef123456", "utok_"],
9
+ ["ghp_" + "A".repeat(24), "ghp_"],
10
+ ["github_pat_" + "B".repeat(24), "github_pat_"],
11
+ ["xoxb-1234567890-abcdef", "xoxb-"],
12
+ ["sk-" + "C".repeat(20), "sk-"],
13
+ ];
14
+ for (const [raw, prefix] of cases) {
15
+ const out = redactTokens(`before ${raw} after`);
16
+ expect(out).toContain("***redacted***");
17
+ expect(out).toContain(prefix);
18
+ expect(out).not.toContain(raw); // 原值一定不能整体幸存
19
+ expect(out).toContain("before"); // 周围文本不受影响
20
+ expect(out).toContain("after");
21
+ }
22
+ });
23
+
24
+ test("🔴 保守:不像凭据的内容一个字都不能改", () => {
25
+ for (const s of [
26
+ "任务完成,磁盘 96%",
27
+ "see https://example.com/a-b_c",
28
+ "sk-", // 太短,不是凭据
29
+ "ntok_", // 光前缀
30
+ "the stock ticker is SKU-12",
31
+ ]) {
32
+ expect(redactTokens(s)).toBe(s);
33
+ }
34
+ });
35
+
36
+ test("同一段里的多个 token 全部被遮", () => {
37
+ const out = redactTokens(`a ntok_aaaaaaaaaa b sk-${"z".repeat(20)} c`);
38
+ expect(out.match(/\*\*\*redacted\*\*\*/g)?.length).toBe(2);
39
+ });
40
+
41
+ test("redactMessageRow 只碰自由文本字段,不动结构字段", () => {
42
+ const row = {
43
+ message_id: "dm_ntok_looking_but_is_an_id",
44
+ user_id: "u_1",
45
+ content: "here is ntok_abcdef123456",
46
+ title: "sk-" + "d".repeat(20),
47
+ meta_json: JSON.stringify({ note: "ghp_" + "E".repeat(24) }),
48
+ created_at: "2026-08-30 00:00:00",
49
+ };
50
+ const out = redactMessageRow(row);
51
+ expect(out.content).toContain("***redacted***");
52
+ expect(out.title).toContain("***redacted***");
53
+ expect(out.meta_json).toContain("***redacted***");
54
+ // 结构字段原样 —— 即便 message_id 里恰好有个像前缀的子串
55
+ expect(out.message_id).toBe(row.message_id);
56
+ expect(out.user_id).toBe(row.user_id);
57
+ expect(out.created_at).toBe(row.created_at);
58
+ });
59
+
60
+ test("非字符串原样返回(null/数字不该被改成字符串)", () => {
61
+ expect(redactTokens(null as any)).toBeNull();
62
+ expect(redactTokens(42 as any)).toBe(42);
63
+ expect(redactMessageRow({ content: null, title: 7 } as any)).toEqual({ content: null, title: 7 } as any);
64
+ });
65
+ });