@sleep2agi/commhub-server 0.9.0-preview.23 → 0.9.0-preview.230

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 (408) hide show
  1. package/README.md +129 -9
  2. package/bin/commhub.ts +93 -9
  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 +654 -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 +6 -7
  27. package/src/api-host-supervisors-runtime-readiness.test.ts +195 -0
  28. package/src/api-nodes-shape.test.ts +95 -4
  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 +52 -2
  35. package/src/avatar-validate.ts +35 -1
  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/broadcast-fanout-2721.test.ts +149 -0
  40. package/src/broadcast-fanout.ts +117 -0
  41. package/src/chain-attempt-dedup.test.ts +447 -0
  42. package/src/clip.test.ts +36 -0
  43. package/src/clip.ts +46 -0
  44. package/src/clone-node.test.ts +248 -0
  45. package/src/clone-node.ts +289 -0
  46. package/src/codex-config-flags.ts +77 -0
  47. package/src/codex-fork-contract.ts +23 -0
  48. package/src/codex-fork-migration.test.ts +38 -0
  49. package/src/commhub-bin-args.test.ts +85 -0
  50. package/src/config-apply-validate.test.ts +188 -1
  51. package/src/config-apply-validate.ts +222 -3
  52. package/src/cors-origins.test.ts +73 -0
  53. package/src/create-node-blocked-gate.test.ts +356 -0
  54. package/src/create-node-network-id.test.ts +91 -0
  55. package/src/create-node-tool-schema.test.ts +49 -0
  56. package/src/create-node-unicode-name.test.ts +120 -0
  57. package/src/create-node-validate.test.ts +199 -7
  58. package/src/create-node-validate.ts +232 -15
  59. package/src/create-node-workdir.test.ts +289 -0
  60. package/src/create-node.ts +88 -23
  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 +1264 -153
  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 +350 -0
  76. package/src/department-heads.ts +226 -0
  77. package/src/departments-agent-count.test.ts +132 -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 +240 -0
  87. package/src/env-transport-gate.ts +80 -0
  88. package/src/env-transport-policy.ts +56 -0
  89. package/src/etag-of.test.ts +57 -0
  90. package/src/event-loop-lag.test.ts +180 -0
  91. package/src/event-loop-lag.ts +201 -0
  92. package/src/ext-token-shared.test.ts +60 -0
  93. package/src/ext-validation-regression.test.ts +131 -0
  94. package/src/external-schedule-edits-http.test.ts +321 -0
  95. package/src/external-schedule-edits-timestamps.test.ts +64 -0
  96. package/src/external-schedule-edits.ts +327 -0
  97. package/src/file-download-authz-dev-open.test.ts +1 -1
  98. package/src/file-download-authz.test.ts +73 -18
  99. package/src/file-download-date-bucket.test.ts +436 -0
  100. package/src/file-network-scope-dev-open.test.ts +97 -0
  101. package/src/file-network-scope.test.ts +732 -0
  102. package/src/group-membership-events-http.test.ts +264 -0
  103. package/src/group-messages-http.test.ts +482 -0
  104. package/src/group-messages.ts +297 -0
  105. package/src/health-anon-readers.ts +60 -0
  106. package/src/health-host-disk-2756.test.ts +71 -0
  107. package/src/health-redaction.test.ts +1721 -2
  108. package/src/host-supervisor-identity-http.test.ts +152 -0
  109. package/src/host-supervisor-identity.ts +92 -0
  110. package/src/host-supervisors-daemon-version.test.ts +137 -0
  111. package/src/http-gzip.test.ts +94 -0
  112. package/src/http-gzip.ts +112 -0
  113. package/src/hub-catalog-read-http.test.ts +409 -0
  114. package/src/hub-provider-config.test.ts +156 -0
  115. package/src/hub-provider-config.ts +170 -0
  116. package/src/hub-timestamp.test.ts +38 -0
  117. package/src/hub-timestamp.ts +27 -0
  118. package/src/human-dm.ts +215 -0
  119. package/src/human-inbox.ts +42 -0
  120. package/src/inbox-caller.ts +49 -0
  121. package/src/inbox-count-sse.test.ts +310 -0
  122. package/src/inbox-count.ts +29 -0
  123. package/src/index.ts +6 -2
  124. package/src/license-state.test.ts +56 -0
  125. package/src/license-state.ts +48 -0
  126. package/src/list-host-supervisors.test.ts +15 -4
  127. package/src/mcp-context-cost-http.test.ts +127 -0
  128. package/src/mcp-inflight.test.ts +92 -0
  129. package/src/mcp-inflight.ts +38 -0
  130. package/src/mcp-projects-events-http.test.ts +165 -0
  131. package/src/mcp-route-label-http.test.ts +120 -0
  132. package/src/mcp-write-network-resolution.test.ts +435 -0
  133. package/src/member-audit-network-id-http.test.ts +93 -0
  134. package/src/member-presence-http.test.ts +210 -0
  135. package/src/messages-ack-by-agent-http.test.ts +142 -0
  136. package/src/migrate-sqlite-to-pg.test.ts +47 -0
  137. package/src/migrate-sqlite-to-pg.ts +263 -0
  138. package/src/model-auth-notify-http.test.ts +181 -0
  139. package/src/model-auth-notify.ts +116 -0
  140. package/src/network-name-http.test.ts +75 -0
  141. package/src/network-rules-http.test.ts +169 -0
  142. package/src/network-rules.ts +129 -0
  143. package/src/network-scope.ts +276 -0
  144. package/src/network-token-membership-http.test.ts +139 -0
  145. package/src/node-alias-control.ts +12 -0
  146. package/src/node-attrs.test.ts +47 -2
  147. package/src/node-caller-identity-http.test.ts +1091 -0
  148. package/src/node-create-request-status-http.test.ts +105 -0
  149. package/src/node-daemon-bindings.test.ts +132 -0
  150. package/src/node-daemon-bindings.ts +135 -0
  151. package/src/node-failure-alert.test.ts +150 -0
  152. package/src/node-failure-alert.ts +177 -0
  153. package/src/node-files-transport.test.ts +344 -0
  154. package/src/node-health-codex-login.test.ts +120 -0
  155. package/src/node-health-dispatch-http.test.ts +335 -0
  156. package/src/node-health-guard.ts +121 -0
  157. package/src/node-health-passthrough.test.ts +115 -0
  158. package/src/node-health-store.ts +101 -0
  159. package/src/node-id-alias.test.ts +190 -0
  160. package/src/node-identity-conflict-http.test.ts +269 -0
  161. package/src/node-identity-conflict.ts +142 -0
  162. package/src/node-identity-mismatch.test.ts +74 -0
  163. package/src/node-in-flight-tasks.ts +31 -0
  164. package/src/node-lifecycle-controllable.test.ts +64 -0
  165. package/src/node-lifecycle-controllable.ts +53 -0
  166. package/src/node-lifecycle-read-http.test.ts +250 -0
  167. package/src/node-lifecycle-read.ts +124 -0
  168. package/src/node-logs-transport.test.ts +263 -0
  169. package/src/node-peer-allowlist.test.ts +114 -0
  170. package/src/node-permissions-http.test.ts +728 -0
  171. package/src/node-permissions.ts +494 -0
  172. package/src/node-request-retention.test.ts +281 -0
  173. package/src/node-request-retention.ts +120 -0
  174. package/src/node-secret-rest-http.test.ts +190 -0
  175. package/src/node-serviceability.test.ts +86 -0
  176. package/src/node-serviceability.ts +124 -0
  177. package/src/node-session-control-http.test.ts +310 -0
  178. package/src/node-session-control.ts +258 -0
  179. package/src/node-skills-transport.test.ts +212 -0
  180. package/src/node-token-identity.ts +35 -0
  181. package/src/node-token-ownership.ts +51 -0
  182. package/src/node-token-privilege-http.test.ts +158 -0
  183. package/src/not-found-api-http.test.ts +67 -0
  184. package/src/observer-avatar-http.test.ts +102 -9
  185. package/src/observer-push.test.ts +76 -0
  186. package/src/opencode-catalog-report-http.test.ts +139 -0
  187. package/src/opencode-v2-catalog.test.ts +268 -0
  188. package/src/opencode-v2-catalog.ts +356 -0
  189. package/src/package-version-consistency.test.ts +25 -0
  190. package/src/parent-source-marker.test.ts +50 -0
  191. package/src/password-network-tokens.ts +25 -0
  192. package/src/peer-reply-atomic.test.ts +457 -0
  193. package/src/peer-reply-capability.test.ts +70 -0
  194. package/src/pg-worker.ts +87 -0
  195. package/src/poll-pacing.test.ts +74 -0
  196. package/src/poll-pacing.ts +123 -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 +382 -9
  201. package/src/redact-tokens.test.ts +86 -0
  202. package/src/redact-tokens.ts +43 -0
  203. package/src/rejection-counters.test.ts +72 -0
  204. package/src/rejection-counters.ts +173 -0
  205. package/src/rename.ts +38 -10
  206. package/src/reply-target-derivation.test.ts +96 -0
  207. package/src/report-status-handover-capabilities-http.test.ts +124 -0
  208. package/src/report-status-host-schema.test.ts +235 -0
  209. package/src/report-status-resume-id-handover.test.ts +184 -0
  210. package/src/report-status-secret-keys.test.ts +140 -0
  211. package/src/require-explicit-test-db.ts +9 -0
  212. package/src/requirement-due-reminders-http.test.ts +620 -0
  213. package/src/requirement-due-reminders.ts +416 -0
  214. package/src/requirement-events-http.test.ts +295 -0
  215. package/src/requirement-events.test.ts +45 -0
  216. package/src/requirement-events.ts +144 -0
  217. package/src/requirement-notify.ts +165 -0
  218. package/src/requirement-participant-notify-http.test.ts +309 -0
  219. package/src/requirement-tag-ops-http.test.ts +443 -0
  220. package/src/requirement-tag-tree-http.test.ts +275 -0
  221. package/src/requirement-tag-tree.ts +127 -0
  222. package/src/requirement-tags.ts +102 -0
  223. package/src/requirements-abandoned-http.test.ts +236 -0
  224. package/src/requirements-comment-http.test.ts +167 -0
  225. package/src/requirements-completed-at-migration.test.ts +70 -0
  226. package/src/requirements-errors-mcp-http.test.ts +164 -0
  227. package/src/requirements-errors.ts +158 -0
  228. package/src/requirements-etag-http.test.ts +91 -0
  229. package/src/requirements-http.test.ts +709 -0
  230. package/src/requirements-last-event-http.test.ts +349 -0
  231. package/src/requirements-list-mcp-slim-http.test.ts +155 -0
  232. package/src/requirements-list-slim-http.test.ts +324 -0
  233. package/src/requirements-mcp.test.ts +163 -0
  234. package/src/requirements-migrate.ts +283 -0
  235. package/src/requirements-owner-strict-mcp-http.test.ts +145 -0
  236. package/src/requirements-people-http.test.ts +114 -0
  237. package/src/requirements-people-mcp-http.test.ts +231 -0
  238. package/src/requirements-people.ts +196 -0
  239. package/src/requirements-priority-lowest.test.ts +114 -0
  240. package/src/requirements-projects-network-echo.test.ts +57 -0
  241. package/src/requirements-projects-parent-http.test.ts +123 -0
  242. package/src/requirements-search-http.test.ts +182 -0
  243. package/src/requirements-search.test.ts +32 -0
  244. package/src/requirements-search.ts +145 -0
  245. package/src/requirements-seq-http.test.ts +193 -0
  246. package/src/requirements-seq-migration.test.ts +63 -0
  247. package/src/requirements-start-date.test.ts +125 -0
  248. package/src/requirements-stats-http.test.ts +357 -0
  249. package/src/requirements-stats.ts +195 -0
  250. package/src/requirements-status-participants-http.test.ts +222 -0
  251. package/src/requirements-upgrade-e2e.test.ts +126 -0
  252. package/src/requirements-upgrade-migration.test.ts +98 -0
  253. package/src/requirements.ts +2064 -0
  254. package/src/resolve-port.test.ts +57 -0
  255. package/src/resolve-port.ts +44 -0
  256. package/src/rest-alias-network-fallback-http.test.ts +123 -0
  257. package/src/rest-alias-network-fallback.test.ts +60 -0
  258. package/src/rest-alias-network-fallback.ts +65 -0
  259. package/src/rest-explicit-columns-http.test.ts +266 -0
  260. package/src/rest-identity.test.ts +130 -0
  261. package/src/rest-identity.ts +141 -0
  262. package/src/rest-projections.ts +72 -0
  263. package/src/rest-read-network-real-chain.test.ts +97 -0
  264. package/src/rest-write-network-resolution.test.ts +133 -0
  265. package/src/restart-readiness-http.test.ts +121 -0
  266. package/src/restricted-files.ts +76 -0
  267. package/src/retention-first-sweep-quiet.test.ts +31 -0
  268. package/src/retention.test.ts +100 -40
  269. package/src/retention.ts +423 -104
  270. package/src/route-callers-http.test.ts +120 -0
  271. package/src/route-timing.test.ts +218 -0
  272. package/src/route-timing.ts +262 -0
  273. package/src/rules-file-duplicate-alias.test.ts +234 -0
  274. package/src/rules-file-session-target.test.ts +207 -0
  275. package/src/rules-file-transport.test.ts +237 -0
  276. package/src/runtime-label.test.ts +22 -0
  277. package/src/runtime-label.ts +21 -0
  278. package/src/runtime-readiness.ts +90 -0
  279. package/src/schedule-agent-mcp-http.test.ts +432 -0
  280. package/src/schedule-agent.ts +252 -0
  281. package/src/schedule-starved.test.ts +92 -0
  282. package/src/schedule-starved.ts +114 -0
  283. package/src/scheduled-backoff-http.test.ts +193 -0
  284. package/src/scheduled-backoff.ts +64 -0
  285. package/src/scheduled-doorbell-nodeless-session.test.ts +134 -0
  286. package/src/scheduled-failure-backoff-2622.test.ts +145 -0
  287. package/src/scheduled-failure-code.test.ts +73 -0
  288. package/src/scheduled-failure-code.ts +105 -0
  289. package/src/scheduled-failures-http.test.ts +279 -0
  290. package/src/scheduled-failures.ts +425 -0
  291. package/src/scheduled-noprogress-backoff-2622.test.ts +163 -0
  292. package/src/scheduled-offline-backoff-probe.test.ts +56 -0
  293. package/src/scheduled-offline.test.ts +131 -0
  294. package/src/scheduled-offline.ts +66 -0
  295. package/src/scheduled-outcome-guidance-http.test.ts +139 -0
  296. package/src/scheduled-outcome-guidance.ts +9 -0
  297. package/src/scheduled-reply-unread-http.test.ts +212 -0
  298. package/src/scheduled-requirement-wake-http.test.ts +279 -0
  299. package/src/scheduled-requirement-wake.ts +125 -0
  300. package/src/scheduled-run-terminal.test.ts +268 -0
  301. package/src/scheduled-skip-blocking-task.test.ts +120 -0
  302. package/src/scheduled-skip-path-backoff-2622.test.ts +98 -0
  303. package/src/scheduled-stuck-http.test.ts +316 -0
  304. package/src/scheduled-stuck-notice-cap.test.ts +80 -0
  305. package/src/scheduled-stuck.ts +219 -0
  306. package/src/scheduled-tasks-http.test.ts +679 -0
  307. package/src/scheduled-tasks.ts +959 -0
  308. package/src/scheduled-work-outcome-http.test.ts +154 -0
  309. package/src/scheduled-work-outcome.ts +50 -0
  310. package/src/scoped-secrets.ts +141 -0
  311. package/src/send-dedup-request-id.test.ts +229 -0
  312. package/src/send-reply-agent-warning.test.ts +38 -38
  313. package/src/send-reply-alias-optional.test.ts +131 -0
  314. package/src/send-reply-attachments.test.ts +458 -0
  315. package/src/send_dedup.test.ts +54 -0
  316. package/src/send_dedup.ts +52 -6
  317. package/src/serve-error.test.ts +73 -0
  318. package/src/serve-error.ts +56 -0
  319. package/src/server.ts +5035 -457
  320. package/src/session-task-on-dispatch.ts +14 -0
  321. package/src/session-token-ownership-http.test.ts +122 -0
  322. package/src/shared/codex-inventory.ts +41 -0
  323. package/src/shared/daemon-provider-login.ts +31 -0
  324. package/src/shared/daemon-provider-snapshot.ts +52 -0
  325. package/src/shared/external-schedule-contract.ts +60 -0
  326. package/src/shared/node-name.test.ts +62 -0
  327. package/src/shared/node-name.ts +161 -0
  328. package/src/shared/probe-host-allowlist-drift.test.ts +45 -0
  329. package/src/shared/provider-application.ts +29 -0
  330. package/src/shared/provider-collection.ts +14 -0
  331. package/src/shared/provider-initialization.ts +8 -0
  332. package/src/shared/reserved-env-drift.test.ts +10 -0
  333. package/src/shared/scoped-secret-contract.ts +5 -0
  334. package/src/shared/task-content-limit-drift.test.ts +28 -0
  335. package/src/shared/task-content-limit.ts +25 -0
  336. package/src/side-thread-command-transport.test.ts +312 -0
  337. package/src/side-thread-command-transport.ts +318 -0
  338. package/src/side-thread-http-integration.test.ts +233 -0
  339. package/src/side-thread-http.ts +504 -0
  340. package/src/side-thread-production.test.ts +72 -0
  341. package/src/side-thread-production.ts +118 -0
  342. package/src/side-thread.test.ts +1067 -0
  343. package/src/side-thread.ts +2499 -0
  344. package/src/skillhub-http.test.ts +142 -0
  345. package/src/sqlite-close-checkpoint.test.ts +78 -0
  346. package/src/start-node.test.ts +264 -0
  347. package/src/status-alias-filter-http.test.ts +106 -0
  348. package/src/status-alias-resolver-http.test.ts +163 -0
  349. package/src/status-alias-resolver.ts +19 -0
  350. package/src/status-node-id-filter-http.test.ts +161 -0
  351. package/src/status-read-cache-http.test.ts +229 -0
  352. package/src/status-read-cache.ts +129 -0
  353. package/src/stop-delete-node.test.ts +567 -1
  354. package/src/task-access-http.test.ts +402 -0
  355. package/src/task-access.ts +420 -0
  356. package/src/task-activity.ts +60 -0
  357. package/src/task-auth-origin-http.test.ts +93 -0
  358. package/src/task-auth-origin.test.ts +24 -0
  359. package/src/task-auth-origin.ts +7 -0
  360. package/src/task-consumption.test.ts +490 -0
  361. package/src/task-diagnostic-http.test.ts +149 -0
  362. package/src/task-diagnostic.test.ts +115 -0
  363. package/src/task-diagnostic.ts +118 -0
  364. package/src/task-expiry-notice-http.test.ts +287 -0
  365. package/src/task-expiry-notice.ts +315 -0
  366. package/src/task-idempotency-mcp.test.ts +142 -0
  367. package/src/task-idempotency.test.ts +76 -0
  368. package/src/task-idempotency.ts +81 -0
  369. package/src/task-late-replies.ts +54 -0
  370. package/src/task-late-reply-http.test.ts +227 -0
  371. package/src/task-lifecycle-watcher-busy.test.ts +114 -0
  372. package/src/task-lifecycle-watcher.test.ts +204 -0
  373. package/src/task-lifecycle-watcher.ts +109 -0
  374. package/src/task-network-resolution-http.test.ts +140 -0
  375. package/src/task-offline-delivery-2668.test.ts +119 -0
  376. package/src/task-offline-delivery.ts +81 -0
  377. package/src/task-orphan-http.test.ts +287 -0
  378. package/src/task-orphan.ts +119 -0
  379. package/src/task-queue-ahead-http.test.ts +309 -0
  380. package/src/task-queue-ahead-scale.test.ts +236 -0
  381. package/src/task-queue-ahead.ts +186 -0
  382. package/src/task-rejection-log-http.test.ts +160 -0
  383. package/src/task-rejection-log.ts +61 -0
  384. package/src/task-sender-http.test.ts +143 -0
  385. package/src/task-stale-open-http.test.ts +292 -0
  386. package/src/task-stale-open.ts +103 -0
  387. package/src/tasks-pagination-http.test.ts +141 -0
  388. package/src/team-rules-status-http.test.ts +90 -0
  389. package/src/team-rules-status.test.ts +59 -0
  390. package/src/team-rules-status.ts +72 -0
  391. package/src/tmux-allowlist.test.ts +48 -0
  392. package/src/tool-audience-http.test.ts +230 -0
  393. package/src/tool-audience.ts +127 -0
  394. package/src/tools-list-json-schema.test.ts +94 -0
  395. package/src/tools.ts +4310 -419
  396. package/src/upload-early-reject-guard.test.ts +155 -0
  397. package/src/uploads-http.test.ts +86 -7
  398. package/src/uploads.ts +103 -6
  399. package/src/user-avatar-http.test.ts +74 -0
  400. package/src/user-avatar-image.ts +160 -0
  401. package/src/user-avatar-migration.test.ts +25 -0
  402. package/src/user-avatar-upload-http.test.ts +348 -0
  403. package/src/user-inbox-by-agent-http.test.ts +161 -0
  404. package/src/user-inbox-netid-notnull.test.ts +122 -0
  405. package/src/user-inbox-read-http.test.ts +223 -0
  406. package/src/user-inbox-schema.test.ts +54 -0
  407. package/src/version-stats.test.ts +66 -0
  408. 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 连接(重连时短暂并存)
@@ -109,11 +161,25 @@ function clientKey(sessionName: string, networkId?: string | null): string {
109
161
  // master-token caller could even inject a NUL into network_id via the
110
162
  // legacy ?network_id= path, and master token is root already.
111
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:";
112
174
 
113
175
  function observerKey(networkId: string): string {
114
176
  return `${OBSERVER_KEY_PREFIX}${networkId}`;
115
177
  }
116
178
 
179
+ function userKey(networkId: string, userId: string): string {
180
+ return `${USER_KEY_PREFIX}${networkId}:${userId}`;
181
+ }
182
+
117
183
  /** Render a client key for logs / stats. Observer keys embed a raw NUL
118
184
  * byte (collision guard above); printing that verbatim makes grep treat
119
185
  * hub logs as binary and breaks log shippers, and getSSEStats feeds
@@ -140,12 +206,19 @@ function closeClient(client: SSEClient, reason: string): void {
140
206
  // we're done with it.
141
207
  }
142
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
+ }
143
216
  }
144
217
 
145
218
  /** Best-effort enqueue with backpressure guard. Returns:
146
219
  * - "ok" → bytes accepted, queue healthy
147
220
  * - "dead" → client closed; caller must remove from map */
148
- function tryEnqueueBytes(client: SSEClient, bytes: Uint8Array): "ok" | "dead" {
221
+ function tryEnqueueBytes(client: SSEClient, bytes: Uint8Array, opts: { critical?: boolean } = {}): "ok" | "dead" {
149
222
  if (client.closed) return "dead";
150
223
 
151
224
  const ctrl = client.controller;
@@ -164,6 +237,14 @@ function tryEnqueueBytes(client: SSEClient, bytes: Uint8Array): "ok" | "dead" {
164
237
  return "dead";
165
238
  }
166
239
 
240
+ // #2363 —— 落后超过 COALESCE_AT_BYTES 且不是关键帧:跳过,不排队、也不关连接
241
+ // (上限之上的仍然按硬顶关闭,见上一段)。这样“慢消费者”不再把内存账单放大到
242
+ // 连接数 × 单连接上限,同时保留原有“彻底读不动就断开回收”的语义。
243
+ if (!opts.critical && desired < -COALESCE_AT_BYTES) {
244
+ droppedDegraded += 1;
245
+ return "ok";
246
+ }
247
+
167
248
  // Track stuck-since-when for the soft timeout below.
168
249
  if (desired < 0) {
169
250
  if (client.stuckSince === null) {
@@ -238,25 +319,143 @@ function ensureLivenessSweep(): void {
238
319
  // Public API
239
320
  // ─────────────────────────────────────────────────────────────────────
240
321
 
241
- /** 创建 SSE Response 并注册到 clients map */
242
- 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 {
243
330
  return createStreamForKey(
244
331
  clientKey(sessionName, networkId),
245
332
  { type: "connected", session: sessionName, network_id: networkId ?? null },
333
+ opts.node ? { node: { ...opts.node, connectedAt: Date.now() } } : {},
246
334
  );
247
335
  }
248
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
+
249
430
  /** #461 — 创建网络级观察者 SSE Response(dashboard 观察第三方流量)。
250
431
  * Shares the exact same registration / backpressure / liveness
251
432
  * machinery as session streams; only the key scheme differs. */
252
- export function createNetworkObserverStream(networkId: string): Response {
433
+ export function createNetworkObserverStream(
434
+ networkId: string,
435
+ opts: { subscriberUserId?: string; eventFilter?: (event: Record<string, unknown>) => boolean } = {},
436
+ ): Response {
253
437
  return createStreamForKey(
254
438
  observerKey(networkId),
255
439
  { type: "connected", observer: true, network_id: networkId },
440
+ opts,
256
441
  );
257
442
  }
258
443
 
259
- function createStreamForKey(key: string, initialEvent: Record<string, unknown>): Response {
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 {
260
459
  const encoder = new TextEncoder();
261
460
  let client: SSEClient;
262
461
 
@@ -268,17 +467,25 @@ function createStreamForKey(key: string, initialEvent: Record<string, unknown>):
268
467
  stuckSince: null,
269
468
  closed: false,
270
469
  key,
470
+ ...(opts.subscriberUserId ? { subscriberUserId: opts.subscriberUserId } : {}),
471
+ ...(opts.eventFilter ? { eventFilter: opts.eventFilter } : {}),
472
+ ...(opts.node ? { node: opts.node } : {}),
271
473
  };
272
474
 
475
+ const presenceUser = isUserStreamClient(client) ? client.subscriberUserId! : null;
476
+ const wasOnline = presenceUser ? isUserOnline(presenceUser) : true;
273
477
  if (!clients.has(key)) clients.set(key, []);
274
478
  clients.get(key)!.push(client);
479
+ if (presenceUser) touchUserSeen(presenceUser);
275
480
  console.log(`[${ts()}] SSE ← ${printableKey(key)} connected (${clients.get(key)!.length} clients)`);
481
+ if (client.node) enforceSingleNodeSubscriber(key, client);
276
482
 
277
483
  // Send initial connected frame through the backpressure guard so
278
484
  // even the first byte respects the contract.
279
485
  tryEnqueueBytes(
280
486
  client,
281
487
  encoder.encode(`data: ${JSON.stringify(initialEvent)}\n\n`),
488
+ { critical: true },
282
489
  );
283
490
 
284
491
  // Periodic keepalive doubles as a half-open probe — if the
@@ -289,7 +496,8 @@ function createStreamForKey(key: string, initialEvent: Record<string, unknown>):
289
496
  if (client.keepaliveTimer) clearInterval(client.keepaliveTimer);
290
497
  return;
291
498
  }
292
- 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!);
293
501
  if (result === "dead") {
294
502
  // closeClient already cleared the timer; just make sure we
295
503
  // also prune from the map.
@@ -299,6 +507,8 @@ function createStreamForKey(key: string, initialEvent: Record<string, unknown>):
299
507
  (client.keepaliveTimer as any)?.unref?.();
300
508
 
301
509
  ensureLivenessSweep();
510
+ // 在线状态的广播放在 connected 帧之后:订阅方先拿到自己的 connected,再看到别人的变化。
511
+ if (presenceUser && !wasOnline) notifyPresence(presenceUser, true);
302
512
  },
303
513
  cancel() {
304
514
  // Consumer side hangup — straightforward path, mark closed and
@@ -358,9 +568,16 @@ function rekeyClient(oldSessionName: string, newSessionName: string, networkId?:
358
568
  for (const c of oldClients) c.key = newKey;
359
569
 
360
570
  const existing = clients.get(newKey) || [];
361
- clients.set(newKey, existing.concat(oldClients));
571
+ const merged = existing.concat(oldClients);
572
+ clients.set(newKey, merged);
362
573
  clients.delete(oldKey);
363
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
+ }
364
581
  return oldClients.length;
365
582
  }
366
583
 
@@ -373,12 +590,26 @@ eventBus.on("rename-committed", (event: RenameCommittedEvent) => {
373
590
  });
374
591
 
375
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
+
376
604
  export function pushEvent(sessionName: string, event: Record<string, unknown>, networkId?: string | null): void {
377
605
  const key = clientKey(sessionName, networkId ?? (event.network_id as string | null | undefined));
378
606
  const arr = clients.get(key);
379
607
  if (!arr || arr.length === 0) return;
380
608
 
381
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);
382
613
  let needPrune = false;
383
614
 
384
615
  for (const c of arr) {
@@ -386,7 +617,7 @@ export function pushEvent(sessionName: string, event: Record<string, unknown>, n
386
617
  needPrune = true;
387
618
  continue;
388
619
  }
389
- const result = tryEnqueueBytes(c, c.encoder.encode(data));
620
+ const result = tryEnqueueBytes(c, bytes);
390
621
  if (result === "dead") needPrune = true;
391
622
  }
392
623
 
@@ -413,6 +644,7 @@ export function pushNetworkObserverEvent(
413
644
  if (!arr || arr.length === 0) return;
414
645
 
415
646
  const data = `data: ${JSON.stringify({ ...event, network_id: networkId, scope: "network" })}\n\n`;
647
+ const bytes = SHARED_ENCODER.encode(data); // #2363:一次编码,所有 observer 共用
416
648
  let needPrune = false;
417
649
 
418
650
  for (const c of arr) {
@@ -420,18 +652,157 @@ export function pushNetworkObserverEvent(
420
652
  needPrune = true;
421
653
  continue;
422
654
  }
423
- const result = tryEnqueueBytes(c, c.encoder.encode(data));
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);
424
661
  if (result === "dead") needPrune = true;
425
662
  }
426
663
 
427
664
  if (needPrune) pruneClosed(key);
428
665
  }
429
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
+
430
800
  export function __resetSSEClientsForTest(): void {
431
801
  for (const arr of clients.values()) {
432
802
  for (const c of arr) closeClient(c, "reset-for-test");
433
803
  }
434
804
  clients.clear();
805
+ userLastSeen.clear();
435
806
  if (livenessSweepTimer) {
436
807
  clearInterval(livenessSweepTimer);
437
808
  livenessSweepTimer = null;
@@ -458,6 +829,8 @@ export const __SSE_INTERNALS_FOR_TEST = {
458
829
  sweepLiveness,
459
830
  closeClient,
460
831
  MAX_QUEUE_BACKPRESSURE_BYTES,
832
+ COALESCE_AT_BYTES,
833
+ degradedDropCount: sseDegradedDropCount,
461
834
  STUCK_CLOSE_MS,
462
835
  KEEPALIVE_MS,
463
836
  LIVENESS_SWEEP_MS,
@@ -0,0 +1,86 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { TOKEN_PATTERNS, redactTokens, redactMessageRow } from "./redact-tokens.js";
3
+
4
+ // 2026-10-31 —— **计数管「有没有少」,集合管「有没有被换」**(外部复核:
5
+ // 「删一条加一条,数不变,而遮掉的东西变了」)。
6
+ // 这份清单是凭据形状的**唯一防线**,所以它需要的是身份断言,不是规模断言。
7
+ test("credential shape list is pinned by identity, not by count", () => {
8
+ // ① 规模:少了要红
9
+ expect(TOKEN_PATTERNS).toHaveLength(5);
10
+ // ② 身份:换了也要红(把 sk- 换成 bearer、把 ghp_ 换成 gho_ 都会落在这里)
11
+ expect(TOKEN_PATTERNS.map((r) => r.source).sort()).toEqual([
12
+ "\\b(ghp_)[A-Za-z0-9]{20,}",
13
+ "\\b(github_pat_)[A-Za-z0-9_]{20,}",
14
+ "\\b(ntok_|utok_|atok_)[A-Za-z0-9_-]{6,}",
15
+ "\\b(sk-)[A-Za-z0-9_-]{16,}",
16
+ "\\b(xox[bpoars]-)[A-Za-z0-9-]{10,}",
17
+ ].sort());
18
+ // ③ 前提:每条都必须带一个捕获组(前缀要保留给人看),且必须是全局的(一次替换多处)
19
+ for (const re of TOKEN_PATTERNS) {
20
+ expect(re.global).toBe(true);
21
+ expect(re.source.includes("(")).toBe(true);
22
+ }
23
+ });
24
+
25
+ describe("#1459 ③ redact-at-read", () => {
26
+ test("🔴 各种前缀形状都被遮住,且前缀保留(读者仍知道泄漏的是哪一类)", () => {
27
+ const cases: Array<[string, string]> = [
28
+ ["ntok_abcdef123456", "ntok_"],
29
+ ["utok_abcdef123456", "utok_"],
30
+ ["ghp_" + "A".repeat(24), "ghp_"],
31
+ ["github_pat_" + "B".repeat(24), "github_pat_"],
32
+ ["xoxb-1234567890-abcdef", "xoxb-"],
33
+ ["sk-" + "C".repeat(20), "sk-"],
34
+ ];
35
+ for (const [raw, prefix] of cases) {
36
+ const out = redactTokens(`before ${raw} after`);
37
+ expect(out).toContain("***redacted***");
38
+ expect(out).toContain(prefix);
39
+ expect(out).not.toContain(raw); // 原值一定不能整体幸存
40
+ expect(out).toContain("before"); // 周围文本不受影响
41
+ expect(out).toContain("after");
42
+ }
43
+ });
44
+
45
+ test("🔴 保守:不像凭据的内容一个字都不能改", () => {
46
+ for (const s of [
47
+ "任务完成,磁盘 96%",
48
+ "see https://example.com/a-b_c",
49
+ "sk-", // 太短,不是凭据
50
+ "ntok_", // 光前缀
51
+ "the stock ticker is SKU-12",
52
+ ]) {
53
+ expect(redactTokens(s)).toBe(s);
54
+ }
55
+ });
56
+
57
+ test("同一段里的多个 token 全部被遮", () => {
58
+ const out = redactTokens(`a ntok_aaaaaaaaaa b sk-${"z".repeat(20)} c`);
59
+ expect(out.match(/\*\*\*redacted\*\*\*/g)?.length).toBe(2);
60
+ });
61
+
62
+ test("redactMessageRow 只碰自由文本字段,不动结构字段", () => {
63
+ const row = {
64
+ message_id: "dm_ntok_looking_but_is_an_id",
65
+ user_id: "u_1",
66
+ content: "here is ntok_abcdef123456",
67
+ title: "sk-" + "d".repeat(20),
68
+ meta_json: JSON.stringify({ note: "ghp_" + "E".repeat(24) }),
69
+ created_at: "2026-08-30 00:00:00",
70
+ };
71
+ const out = redactMessageRow(row);
72
+ expect(out.content).toContain("***redacted***");
73
+ expect(out.title).toContain("***redacted***");
74
+ expect(out.meta_json).toContain("***redacted***");
75
+ // 结构字段原样 —— 即便 message_id 里恰好有个像前缀的子串
76
+ expect(out.message_id).toBe(row.message_id);
77
+ expect(out.user_id).toBe(row.user_id);
78
+ expect(out.created_at).toBe(row.created_at);
79
+ });
80
+
81
+ test("非字符串原样返回(null/数字不该被改成字符串)", () => {
82
+ expect(redactTokens(null as any)).toBeNull();
83
+ expect(redactTokens(42 as any)).toBe(42);
84
+ expect(redactMessageRow({ content: null, title: 7 } as any)).toEqual({ content: null, title: 7 } as any);
85
+ });
86
+ });
@@ -0,0 +1,43 @@
1
+ // #1459 ③ —— 读取时的 token 形状脱敏(redact-at-read)。
2
+ //
3
+ // 为什么在**读取**侧而不是写入侧:写入方是 agent,不受信任。存储侧已经走
4
+ // normalizeMetaJson(跨主机路径脱敏,与 inbox 一致),那一层防的是"路径泄漏";
5
+ // 这一层防的是"有人把凭据塞进消息正文或 meta"。两层管的不是同一件事。
6
+ //
7
+ // 🔴 保守优先:宁可漏脱敏一个不像凭据的串,也不要把正常内容改花。所以只匹配
8
+ // 几个**有明确前缀**的形状,不做熵检测、不猜。
9
+
10
+ /** 有明确前缀的凭据形状。
11
+ *
12
+ * 🔴 前缀用**捕获组显式声明**,不靠"切到第一个下划线"之类的推断 ——
13
+ * 第一版就是那么写的,`github_pat_xxx` 被切成了 `github_`,把"这是哪一类
14
+ * 凭据"这个唯一有用的信息也遮掉了。声明比推断短,也不会错。 */
15
+ // 2026-10-31 —— 导出这份清单,是因为**「条数对」不等于「内容是同一套」**
16
+ // (外部复核:「删一条加一条,数不变,而遮掉的东西变了」)。
17
+ // 只断言 length=5 的门,对「把 sk- 换成 bearer 」这种换手完全无感。
18
+ export const TOKEN_PATTERNS: ReadonlyArray<RegExp> = [
19
+ /\b(ntok_|utok_|atok_)[A-Za-z0-9_-]{6,}/g, // 本仓自己的 token
20
+ /\b(github_pat_)[A-Za-z0-9_]{20,}/g, // GitHub fine-grained PAT(必须排在 ghp_ 之前无关,但更长的前缀先声明更清楚)
21
+ /\b(ghp_)[A-Za-z0-9]{20,}/g, // GitHub classic PAT
22
+ /\b(xox[bpoars]-)[A-Za-z0-9-]{10,}/g, // Slack
23
+ /\b(sk-)[A-Za-z0-9_-]{16,}/g, // OpenAI 风格
24
+ ];
25
+
26
+ /** 对任意文本做 token 形状脱敏。非字符串原样返回。 */
27
+ export function redactTokens<T>(value: T): T {
28
+ if (typeof value !== "string") return value;
29
+ let out: string = value;
30
+ // 保留捕获到的前缀(让读者知道泄漏的是哪一类),其余一律遮掉;不保留尾部 ——
31
+ // 尾部对识别没帮助,却能帮攻击者缩小搜索空间。
32
+ for (const re of TOKEN_PATTERNS) out = out.replace(re, (_m, prefix: string) => `${prefix}***redacted***`);
33
+ return out as unknown as T;
34
+ }
35
+
36
+ /** 对一行消息做脱敏:只碰会承载自由文本的字段,不动 id/时间戳等结构字段。 */
37
+ export function redactMessageRow<T extends Record<string, unknown>>(row: T): T {
38
+ const out: Record<string, unknown> = { ...row };
39
+ for (const field of ["content", "title", "meta_json"]) {
40
+ if (typeof out[field] === "string") out[field] = redactTokens(out[field] as string);
41
+ }
42
+ return out as T;
43
+ }