@sleep2agi/commhub-server 0.9.0-preview.24 → 0.9.0-preview.240

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 (409) 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 +48 -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 +434 -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 +1443 -3
  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 +564 -0
  114. package/src/hub-provider-config.test.ts +156 -0
  115. package/src/hub-provider-config.ts +202 -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 +373 -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-assignee-drift.test.ts +112 -0
  225. package/src/requirements-comment-http.test.ts +167 -0
  226. package/src/requirements-completed-at-migration.test.ts +70 -0
  227. package/src/requirements-errors-mcp-http.test.ts +164 -0
  228. package/src/requirements-errors.ts +158 -0
  229. package/src/requirements-etag-http.test.ts +91 -0
  230. package/src/requirements-http.test.ts +709 -0
  231. package/src/requirements-last-event-http.test.ts +349 -0
  232. package/src/requirements-list-mcp-slim-http.test.ts +155 -0
  233. package/src/requirements-list-slim-http.test.ts +324 -0
  234. package/src/requirements-mcp.test.ts +163 -0
  235. package/src/requirements-migrate.ts +283 -0
  236. package/src/requirements-owner-strict-mcp-http.test.ts +145 -0
  237. package/src/requirements-people-http.test.ts +114 -0
  238. package/src/requirements-people-mcp-http.test.ts +231 -0
  239. package/src/requirements-people.ts +196 -0
  240. package/src/requirements-priority-lowest.test.ts +114 -0
  241. package/src/requirements-projects-network-echo.test.ts +57 -0
  242. package/src/requirements-projects-parent-http.test.ts +123 -0
  243. package/src/requirements-search-http.test.ts +182 -0
  244. package/src/requirements-search.test.ts +32 -0
  245. package/src/requirements-search.ts +145 -0
  246. package/src/requirements-seq-http.test.ts +193 -0
  247. package/src/requirements-seq-migration.test.ts +63 -0
  248. package/src/requirements-start-date.test.ts +125 -0
  249. package/src/requirements-stats-http.test.ts +357 -0
  250. package/src/requirements-stats.ts +195 -0
  251. package/src/requirements-status-participants-http.test.ts +222 -0
  252. package/src/requirements-upgrade-e2e.test.ts +126 -0
  253. package/src/requirements-upgrade-migration.test.ts +98 -0
  254. package/src/requirements.ts +2116 -0
  255. package/src/resolve-port.test.ts +57 -0
  256. package/src/resolve-port.ts +44 -0
  257. package/src/rest-alias-network-fallback-http.test.ts +123 -0
  258. package/src/rest-alias-network-fallback.test.ts +60 -0
  259. package/src/rest-alias-network-fallback.ts +65 -0
  260. package/src/rest-explicit-columns-http.test.ts +266 -0
  261. package/src/rest-identity.test.ts +130 -0
  262. package/src/rest-identity.ts +141 -0
  263. package/src/rest-projections.ts +72 -0
  264. package/src/rest-read-network-real-chain.test.ts +97 -0
  265. package/src/rest-write-network-resolution.test.ts +133 -0
  266. package/src/restart-readiness-http.test.ts +121 -0
  267. package/src/restricted-files.ts +76 -0
  268. package/src/retention-first-sweep-quiet.test.ts +31 -0
  269. package/src/retention.test.ts +100 -40
  270. package/src/retention.ts +423 -104
  271. package/src/route-callers-http.test.ts +120 -0
  272. package/src/route-timing.test.ts +218 -0
  273. package/src/route-timing.ts +262 -0
  274. package/src/rules-file-duplicate-alias.test.ts +234 -0
  275. package/src/rules-file-session-target.test.ts +207 -0
  276. package/src/rules-file-transport.test.ts +237 -0
  277. package/src/runtime-label.test.ts +22 -0
  278. package/src/runtime-label.ts +21 -0
  279. package/src/runtime-readiness.ts +90 -0
  280. package/src/schedule-agent-mcp-http.test.ts +432 -0
  281. package/src/schedule-agent.ts +252 -0
  282. package/src/schedule-starved.test.ts +92 -0
  283. package/src/schedule-starved.ts +114 -0
  284. package/src/scheduled-backoff-http.test.ts +193 -0
  285. package/src/scheduled-backoff.ts +64 -0
  286. package/src/scheduled-doorbell-nodeless-session.test.ts +134 -0
  287. package/src/scheduled-failure-backoff-2622.test.ts +145 -0
  288. package/src/scheduled-failure-code.test.ts +73 -0
  289. package/src/scheduled-failure-code.ts +105 -0
  290. package/src/scheduled-failures-http.test.ts +279 -0
  291. package/src/scheduled-failures.ts +425 -0
  292. package/src/scheduled-noprogress-backoff-2622.test.ts +163 -0
  293. package/src/scheduled-offline-backoff-probe.test.ts +56 -0
  294. package/src/scheduled-offline.test.ts +131 -0
  295. package/src/scheduled-offline.ts +66 -0
  296. package/src/scheduled-outcome-guidance-http.test.ts +139 -0
  297. package/src/scheduled-outcome-guidance.ts +9 -0
  298. package/src/scheduled-reply-unread-http.test.ts +212 -0
  299. package/src/scheduled-requirement-wake-http.test.ts +279 -0
  300. package/src/scheduled-requirement-wake.ts +125 -0
  301. package/src/scheduled-run-terminal.test.ts +268 -0
  302. package/src/scheduled-skip-blocking-task.test.ts +120 -0
  303. package/src/scheduled-skip-path-backoff-2622.test.ts +98 -0
  304. package/src/scheduled-stuck-http.test.ts +316 -0
  305. package/src/scheduled-stuck-notice-cap.test.ts +80 -0
  306. package/src/scheduled-stuck.ts +219 -0
  307. package/src/scheduled-tasks-http.test.ts +679 -0
  308. package/src/scheduled-tasks.ts +959 -0
  309. package/src/scheduled-work-outcome-http.test.ts +154 -0
  310. package/src/scheduled-work-outcome.ts +50 -0
  311. package/src/scoped-secrets.ts +141 -0
  312. package/src/send-dedup-request-id.test.ts +229 -0
  313. package/src/send-reply-agent-warning.test.ts +38 -38
  314. package/src/send-reply-alias-optional.test.ts +131 -0
  315. package/src/send-reply-attachments.test.ts +458 -0
  316. package/src/send_dedup.test.ts +54 -0
  317. package/src/send_dedup.ts +52 -6
  318. package/src/serve-error.test.ts +73 -0
  319. package/src/serve-error.ts +56 -0
  320. package/src/server.ts +5010 -429
  321. package/src/session-task-on-dispatch.ts +14 -0
  322. package/src/session-token-ownership-http.test.ts +122 -0
  323. package/src/shared/codex-inventory.ts +41 -0
  324. package/src/shared/daemon-provider-login.ts +31 -0
  325. package/src/shared/daemon-provider-snapshot.ts +52 -0
  326. package/src/shared/external-schedule-contract.ts +60 -0
  327. package/src/shared/node-name.test.ts +62 -0
  328. package/src/shared/node-name.ts +161 -0
  329. package/src/shared/probe-host-allowlist-drift.test.ts +45 -0
  330. package/src/shared/provider-application.ts +29 -0
  331. package/src/shared/provider-collection.ts +14 -0
  332. package/src/shared/provider-initialization.ts +8 -0
  333. package/src/shared/reserved-env-drift.test.ts +10 -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 +142 -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 +129 -0
  354. package/src/stop-delete-node.test.ts +567 -1
  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 +490 -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 +227 -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 +61 -0
  385. package/src/task-sender-http.test.ts +143 -0
  386. package/src/task-stale-open-http.test.ts +292 -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/tmux-allowlist.test.ts +48 -0
  393. package/src/tool-audience-http.test.ts +230 -0
  394. package/src/tool-audience.ts +127 -0
  395. package/src/tools-list-json-schema.test.ts +94 -0
  396. package/src/tools.ts +4310 -419
  397. package/src/upload-early-reject-guard.test.ts +155 -0
  398. package/src/uploads-http.test.ts +86 -7
  399. package/src/uploads.ts +103 -6
  400. package/src/user-avatar-http.test.ts +74 -0
  401. package/src/user-avatar-image.ts +160 -0
  402. package/src/user-avatar-migration.test.ts +25 -0
  403. package/src/user-avatar-upload-http.test.ts +348 -0
  404. package/src/user-inbox-by-agent-http.test.ts +161 -0
  405. package/src/user-inbox-netid-notnull.test.ts +122 -0
  406. package/src/user-inbox-read-http.test.ts +223 -0
  407. package/src/user-inbox-schema.test.ts +54 -0
  408. package/src/version-stats.test.ts +66 -0
  409. package/src/version-stats.ts +109 -0
@@ -0,0 +1,335 @@
1
+ // #460(#448 子项)—— 降级节点拒收新任务。
2
+ //
3
+ // 钉住:
4
+ // 1. 新鲜健康报告说某层坏了(app_server.ok=false / tui.ok=false / model_auth revoked|expired)
5
+ // → REST /api/task 409、MCP send_task / retry_task / reassign_task 返回 node_degraded,带层、原因、修法;
6
+ // 定时任务的这一次 run 记 failed + error_code=node_degraded,不建任务行。
7
+ // 2. 不知道 = 放行:没有报告(老 agent-node)、报告过期(> TTL)、各层都 ok、某层没报 —— 与改动前逐字相同的成功路径。
8
+ // 3. 逃生口 force=true 只对用户令牌生效;节点令牌带 force 照样被拒(不让 agent 绕过对另一个 agent 的健康判断)。
9
+ // 4. 老客户端请求形状(不带 force)在健康 / 未知节点上完全照旧 —— 兼容回放。
10
+ //
11
+ // 跑法:cd server && COMMHUB_DB=/tmp/x.db bun test src/node-health-dispatch-http.test.ts
12
+ // PG:COMMHUB_TEST_PG_URL=… COMMHUB_PG_EXPERIMENTAL=1(tests/test2123-hub-postgres-ladder 里注册)
13
+ import { afterAll, beforeAll, beforeEach, describe, expect, test } from "bun:test";
14
+ import { mkdtempSync } from "node:fs";
15
+ import { join } from "node:path";
16
+ import { tmpdir } from "node:os";
17
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
18
+ import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
19
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
20
+ import { createNetworkTokenForNode, register } from "./auth.js";
21
+ import { db } from "./db.js";
22
+ import { registerTools } from "./tools.js";
23
+ import { clearNodeHealthStore, NODE_HEALTH_TTL_MS, recordNodeHealth } from "./node-health-store.js";
24
+ import { degradedLayers } from "./node-health-guard.js";
25
+ import { runDueScheduledTasks } from "./scheduled-tasks.js";
26
+
27
+ const dir = mkdtempSync(join(tmpdir(), "anet-health-dispatch-"));
28
+ const activeDbPath = process.env.COMMHUB_DB ?? (process.env.COMMHUB_TEST_PG_URL ? "postgres" : undefined);
29
+ if (!activeDbPath) throw new Error("node-health-dispatch requires COMMHUB_DB (or COMMHUB_TEST_PG_URL) before module import");
30
+
31
+ let server: any;
32
+ let base = "";
33
+ let ownerToken = "", ownerId = "", networkId = "";
34
+ let peerNodeToken = "", peerNodeTokenId = "";
35
+ const stamp = Date.now();
36
+ const TARGET = `hd-target-${stamp}`;
37
+ const TARGET_NODE = `n_hd_target_${stamp}`;
38
+ const PEER = `hd-peer-${stamp}`;
39
+ const PEER_NODE = `n_hd_peer_${stamp}`;
40
+ let seq = 0;
41
+ const text = (what: string) => `${what} #${++seq}`; // distinct content: the 5-minute dedup must not interfere
42
+
43
+ async function api(token: string, path: string, init?: RequestInit) {
44
+ const res = await fetch(`${base}${path}`, {
45
+ ...init,
46
+ headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json", ...(init?.headers || {}) },
47
+ });
48
+ return { status: res.status, body: await res.json() as any };
49
+ }
50
+ const postTask = (token: string, extra: Record<string, unknown> = {}) =>
51
+ api(token, "/api/task", { method: "POST", body: JSON.stringify({ alias: TARGET, task: text("rest"), network_id: networkId, ...extra }) });
52
+
53
+ async function mcpAs(identity: { net: string | null; user: string; alias: string; isNetworkToken: boolean; tokenId: string }) {
54
+ const s = new McpServer({ name: "health-dispatch", version: "1" });
55
+ registerTools(s, undefined, identity.net, identity.user, identity.alias, identity.isNetworkToken, identity.tokenId);
56
+ const client = new Client({ name: "health-dispatch-client", version: "1" });
57
+ const [ct, st] = InMemoryTransport.createLinkedPair();
58
+ await s.connect(st);
59
+ await client.connect(ct);
60
+ const call = async (name: string, args: Record<string, unknown>) => {
61
+ const r: any = await client.callTool({ name, arguments: args });
62
+ const raw = r.content?.[0]?.text ?? "null";
63
+ try { return JSON.parse(raw); } catch { return { raw }; }
64
+ };
65
+ return { call, close: async () => { await client.close(); await s.close(); } };
66
+ }
67
+
68
+ const DOWN_APP = { bridge: "ok" as const, app_server: { ok: false, rtt_ms: null, last_error: "connect ECONNREFUSED 127.0.0.1:4500" }, model_auth: "ok" as const };
69
+ const HEALTHY = { bridge: "ok" as const, app_server: { ok: true, rtt_ms: 3, last_error: null }, tui: { ok: true, reason: "running" }, model_auth: "ok" as const };
70
+ const setHealth = (h: any, at?: number) => recordNodeHealth(networkId, TARGET, h, at);
71
+ const taskCount = () => db.get<{ n: number }>("SELECT COUNT(*) AS n FROM tasks WHERE to_name = ?1 AND network_id = ?2", TARGET, networkId)!.n;
72
+
73
+ beforeAll(async () => {
74
+ process.env.COMMHUB_UPLOADS_DIR = join(dir, "uploads");
75
+ process.env.HOST = "127.0.0.1";
76
+ const owner = register(`hd_owner_${stamp}`, "HealthDispatchOwner123!", undefined, "seed");
77
+ ownerToken = owner.token!; ownerId = owner.user!.user_id; networkId = owner.network_id!;
78
+ for (const [nodeId, alias] of [[TARGET_NODE, TARGET], [PEER_NODE, PEER]]) {
79
+ db.run(`INSERT INTO nodes (node_id, node_name, alias, runtime, network_id, owner_user_id) VALUES (?1, ?2, ?2, 'codex-app-server', ?3, ?4)`, [nodeId, alias, networkId, ownerId]);
80
+ db.run(`INSERT INTO sessions (resume_id, alias, status, node_id, network_id) VALUES (?1, ?2, 'idle', ?3, ?4)`, [`r_${nodeId}`, alias, nodeId, networkId]);
81
+ }
82
+ const minted = createNetworkTokenForNode(ownerId, networkId, PEER, PEER_NODE);
83
+ peerNodeToken = minted.token!; peerNodeTokenId = minted.token_id!;
84
+ const mod: any = await import("./server.js");
85
+ server = mod.bootServer({ port: 0, hostname: "127.0.0.1" });
86
+ base = `http://127.0.0.1:${server.port}`;
87
+ }, 30_000);
88
+ afterAll(() => { server?.stop?.(true); clearNodeHealthStore(); });
89
+ beforeEach(() => clearNodeHealthStore());
90
+
91
+ describe("#460 which layers count as degraded", () => {
92
+ test("each failing layer is named; ok / missing layers are not", () => {
93
+ expect(degradedLayers(null)).toEqual([]);
94
+ expect(degradedLayers(HEALTHY)).toEqual([]);
95
+ expect(degradedLayers({ bridge: "ok", model_auth: "unknown" })).toEqual([]);
96
+ expect(degradedLayers(DOWN_APP).map(l => [l.layer, l.label])).toEqual([["app_server", "App Server 断开"]]);
97
+ expect(degradedLayers({ tui: { ok: false, reason: "sleep-placeholder" } }).map(l => [l.layer, l.reason, l.label])).toEqual([["tui", "sleep-placeholder", "TUI 被占位(sleep)"]]);
98
+ expect(degradedLayers({ model_auth: "revoked" }).map(l => l.label)).toEqual(["需要重新登录"]);
99
+ expect(degradedLayers({ model_auth: "expired" }).map(l => l.label)).toEqual(["登录已过期"]);
100
+ // 2026-10-31 —— `reason` 有两个生产者(节点上报 vs Hub 合成 "unreachable"),
101
+ // 所以 origin 是唯一能把两者分开的一格。上游可以**原样声明** "unreachable",
102
+ // 因此改名 / 拆分 catch / 加前缀都不构成判别力(`:148` 同形)。
103
+ expect(degradedLayers(DOWN_APP).map(l => [l.layer, l.origin])).toEqual([["app_server", "node"]]);
104
+ expect(degradedLayers({ app_server: { ok: false, rtt_ms: null, last_error: null } }).map(l => [l.reason, l.origin])).toEqual([["unreachable", "hub"]]);
105
+ // must-fire:两边都写出 "unreachable" 时,只有 origin 不同 —— 这条就是那一格存在的理由
106
+ expect(degradedLayers({ app_server: { ok: false, rtt_ms: null, last_error: "unreachable" } }).map(l => [l.reason, l.origin])).toEqual([["unreachable", "node"]]);
107
+ expect(degradedLayers({ tui: { ok: false, reason: "sleep-placeholder" } })[0].origin).toBe("node");
108
+
109
+ // 修法只指向本节点重新登录,不教人拷别人的凭据
110
+ expect(degradedLayers({ model_auth: "revoked" })[0].hint).toContain("不要拷别的节点的 auth.json");
111
+ });
112
+ });
113
+
114
+ // 2026-10-31 —— **属性断言**(外部复核 ④:「键集契约只能对工厂断言,不能对样本断言」)。
115
+ // degradedLayers(health) 是工厂:它对**任意** health 形状都必须产出同一种元素形状。
116
+ // 这里枚举 2×2×5×2 = 40 种组合,断言每个元素的键集恒为那五个。
117
+ describe("degradedLayers 是工厂:任意 health 组合下元素键集不变", () => {
118
+ test("40 种组合,元素键集恒为 {layer,reason,origin,label,hint}", () => {
119
+ const seen = new Set<string>();
120
+ for (const appOk of [true, false]) {
121
+ for (const tuiOk of [true, false]) {
122
+ for (const auth of ["ok", "revoked", "expired", "unknown", undefined]) {
123
+ for (const lastErr of [null, "refused"]) {
124
+ const health: any = {
125
+ bridge: "ok",
126
+ app_server: appOk ? { ok: true, rtt_ms: 1, last_error: null }
127
+ : { ok: false, rtt_ms: null, last_error: lastErr },
128
+ ...(tuiOk === undefined ? {} : { tui: { ok: tuiOk, reason: "sleep-placeholder" } }),
129
+ ...(auth === undefined ? {} : { model_auth: auth }),
130
+ };
131
+ for (const layer of degradedLayers(health)) {
132
+ seen.add(Object.keys(layer).sort().join(","));
133
+ }
134
+ }
135
+ }
136
+ }
137
+ }
138
+ // 排序后的顺序是字典序(label 在 layer 前)——我第一版按定义顺序写,被断言当场纠正。
139
+ expect([...seen]).toEqual(["hint,label,layer,origin,reason"]);
140
+ });
141
+ });
142
+
143
+ describe("#460 REST /api/task", () => {
144
+ test("unknown health (old agent-node) → dispatched exactly as before", async () => {
145
+ const before = taskCount();
146
+ const r = await postTask(ownerToken);
147
+ expect(r.status).toBe(200);
148
+ expect(r.body.ok).toBe(true);
149
+ expect(taskCount()).toBe(before + 1);
150
+ });
151
+
152
+ test("healthy report → dispatched", async () => {
153
+ setHealth(HEALTHY);
154
+ expect((await postTask(ownerToken)).status).toBe(200);
155
+ });
156
+
157
+ test("App Server down → 409 node_degraded with layer, reason and hint; no task row", async () => {
158
+ setHealth(DOWN_APP);
159
+ const before = taskCount();
160
+ const r = await postTask(ownerToken);
161
+ expect(r.status).toBe(409);
162
+ expect(r.body.ok).toBe(false);
163
+ expect(r.body.error).toBe("node_degraded");
164
+ expect(r.body.alias).toBe(TARGET);
165
+ expect(r.body.layers.map((l: any) => l.layer)).toEqual(["app_server"]);
166
+ expect(r.body.layers[0].reason).toContain("ECONNREFUSED");
167
+ expect(r.body.layers[0].origin).toBe("node");
168
+ expect(r.body.message).toContain("App Server 断开");
169
+ expect(r.body.hint).toContain("force=true");
170
+ expect(r.body.force_allowed).toBe(true);
171
+ expect(typeof r.body.health_observed_ms_ago).toBe("number");
172
+ expect(taskCount()).toBe(before);
173
+ });
174
+
175
+ test("TUI gone and login revoked are refused too, all layers listed", async () => {
176
+ setHealth({ bridge: "ok", app_server: { ok: true, rtt_ms: 2, last_error: null }, tui: { ok: false, reason: "session-missing" }, model_auth: "revoked" });
177
+ const r = await postTask(ownerToken);
178
+ expect(r.status).toBe(409);
179
+ expect(r.body.layers.map((l: any) => l.layer)).toEqual(["tui", "model_auth"]);
180
+ });
181
+
182
+ test("expired login alone is refused", async () => {
183
+ setHealth({ bridge: "ok", model_auth: "expired" });
184
+ expect((await postTask(ownerToken)).body.error).toBe("node_degraded");
185
+ });
186
+
187
+ test("stale report (older than the TTL) = unknown → dispatched", async () => {
188
+ setHealth(DOWN_APP, Date.now() - NODE_HEALTH_TTL_MS - 1_000);
189
+ expect((await postTask(ownerToken)).status).toBe(200);
190
+ });
191
+
192
+ test("owner escape hatch: force=true with a user token dispatches", async () => {
193
+ setHealth(DOWN_APP);
194
+ const before = taskCount();
195
+ const r = await postTask(ownerToken, { force: true });
196
+ expect(r.status).toBe(200);
197
+ expect(taskCount()).toBe(before + 1);
198
+ });
199
+
200
+ test("a node token cannot force past another node's health", async () => {
201
+ setHealth(DOWN_APP);
202
+ const r = await api(peerNodeToken, "/api/task", { method: "POST", body: JSON.stringify({ alias: TARGET, task: text("node-force"), force: true }) });
203
+ expect(r.status).toBe(409);
204
+ expect(r.body.error).toBe("node_degraded");
205
+ expect(r.body.force_allowed).toBe(false);
206
+ expect(r.body.hint).not.toContain("force=true");
207
+ });
208
+
209
+ test("old-client request shapes are untouched on a healthy node (compat replay)", async () => {
210
+ setHealth(HEALTHY);
211
+ // the app's send (meta + client_request_id), a plain anet send, and a parent-linked dispatch
212
+ const shapes = [
213
+ { alias: TARGET, task: text("app"), network_id: networkId, priority: "normal", meta: { client_request_id: `cr_${stamp}_${seq}` } },
214
+ { alias: TARGET, task: text("anet"), network_id: networkId },
215
+ { alias: TARGET, task: text("ttl"), network_id: networkId, priority: "high", ttl_seconds: 600 },
216
+ ];
217
+ for (const shape of shapes) {
218
+ const r = await api(ownerToken, "/api/task", { method: "POST", body: JSON.stringify(shape) });
219
+ expect(r.status).toBe(200);
220
+ expect(r.body.ok).toBe(true);
221
+ }
222
+ });
223
+ });
224
+
225
+ describe("#460 MCP send_task / retry_task / reassign_task", () => {
226
+ const asOwner = () => ({ net: null, user: ownerId, alias: `hd_owner_${stamp}`, isNetworkToken: false, tokenId: "tok_hd_owner_login" });
227
+ const asPeerNode = () => ({ net: networkId, user: ownerId, alias: PEER, isNetworkToken: true, tokenId: peerNodeTokenId });
228
+
229
+ test("send_task from an agent to a degraded node → node_degraded (no queueing); force ignored for node tokens", async () => {
230
+ setHealth(DOWN_APP);
231
+ const peer = await mcpAs(asPeerNode());
232
+ try {
233
+ const before = taskCount();
234
+ const r = await peer.call("send_task", { alias: TARGET, task: text("mcp"), force: true });
235
+ expect(r.ok).toBe(false);
236
+ expect(r.error).toBe("node_degraded");
237
+ expect(r.force_allowed).toBe(false);
238
+ expect(taskCount()).toBe(before);
239
+ } finally { await peer.close(); }
240
+ });
241
+
242
+ test("send_task: unknown health → ok; owner force → ok", async () => {
243
+ const owner = await mcpAs(asOwner());
244
+ try {
245
+ expect((await owner.call("send_task", { alias: TARGET, task: text("mcp-unknown"), network_id: networkId })).ok).toBe(true);
246
+ setHealth({ bridge: "ok", model_auth: "revoked" });
247
+ expect((await owner.call("send_task", { alias: TARGET, task: text("mcp-refused"), network_id: networkId })).error).toBe("node_degraded");
248
+ expect((await owner.call("send_task", { alias: TARGET, task: text("mcp-forced"), network_id: networkId, force: true })).ok).toBe(true);
249
+ } finally { await owner.close(); }
250
+ });
251
+
252
+ test("retry_task and reassign_task onto a degraded node are refused", async () => {
253
+ const owner = await mcpAs(asOwner());
254
+ try {
255
+ const sent = await owner.call("send_task", { alias: PEER, task: text("to-peer"), network_id: networkId });
256
+ expect(sent.ok).toBe(true);
257
+ setHealth(DOWN_APP);
258
+ const re = await owner.call("reassign_task", { task_id: sent.task_id ?? sent.message_id, new_alias: TARGET, network_id: networkId });
259
+ expect(re.error).toBe("node_degraded");
260
+ const failed = await owner.call("send_task", { alias: TARGET, task: text("to-fail"), network_id: networkId, force: true });
261
+ const failedId = failed.task_id ?? failed.message_id;
262
+ db.run("UPDATE tasks SET status = 'failed' WHERE task_id = ?1", [failedId]);
263
+ const retry = await owner.call("retry_task", { task_id: failedId, network_id: networkId });
264
+ expect(retry.error).toBe("node_degraded");
265
+ clearNodeHealthStore();
266
+ expect((await owner.call("retry_task", { task_id: failedId, network_id: networkId })).ok).toBe(true);
267
+ } finally { await owner.close(); }
268
+ });
269
+ });
270
+
271
+ describe("#460 scheduled tasks", () => {
272
+ test("a due run on a degraded node fails with node_degraded and creates no task", async () => {
273
+ const created = await api(ownerToken, "/api/scheduled-tasks", {
274
+ method: "POST",
275
+ body: JSON.stringify({ network_id: networkId, name: `hd-schedule-${stamp}`, target_node_id: TARGET_NODE, task: text("scheduled"), timezone: "UTC", schedule: { type: "interval", every_seconds: 60 } }),
276
+ });
277
+ expect(created.status).toBe(201);
278
+ const scheduleId = created.body.schedule.schedule_id;
279
+ setHealth({ bridge: "ok", tui: { ok: false, reason: "pane-dead" }, model_auth: "ok" });
280
+ const before = taskCount();
281
+ db.run("UPDATE scheduled_tasks SET next_run_at = ?1 WHERE schedule_id = ?2", [new Date(Date.now() - 60_000).toISOString(), scheduleId]);
282
+ runDueScheduledTasks();
283
+ const run = db.get<{ status: string; error_code: string | null; error_message: string | null; task_id: string | null }>(
284
+ "SELECT status, error_code, error_message, task_id FROM scheduled_task_runs WHERE schedule_id = ?1 ORDER BY created_at DESC LIMIT 1", scheduleId);
285
+ expect(run?.status).toBe("failed");
286
+ expect(run?.error_code).toBe("node_degraded");
287
+ expect(run?.error_message).toContain("TUI 已退出");
288
+ expect(run?.task_id ?? null).toBeNull();
289
+ expect(taskCount()).toBe(before);
290
+ });
291
+ });
292
+
293
+ describe("#460 /api/status carries `degraded` only for degraded nodes", () => {
294
+ const row = async (light: boolean) => {
295
+ const r = await api(ownerToken, `/api/status?network_id=${encodeURIComponent(networkId)}${light ? "&light=1" : ""}`);
296
+ expect(r.status).toBe(200);
297
+ return r.body.sessions.find((x: any) => x.alias === TARGET);
298
+ };
299
+ test("unknown / healthy → no `degraded` key at all (light response bytes unchanged)", async () => {
300
+ expect("degraded" in (await row(true))).toBe(false);
301
+ setHealth(HEALTHY);
302
+ expect("degraded" in (await row(true))).toBe(false);
303
+ expect("degraded" in (await row(false))).toBe(false);
304
+ });
305
+ test("degraded → light and full rows list the failing layers with the app's labels", async () => {
306
+ setHealth({ bridge: "ok", app_server: { ok: false, rtt_ms: null, last_error: "refused" }, model_auth: "revoked" });
307
+ // 2026-10-31 —— 加了 `origin`(reason 的生产者)。这是**加字段**,不是改语义:
308
+ // 少这一格,「节点说自己不可达」与「Hub 问不到节点」在响应里同形。
309
+ const want = [
310
+ { layer: "app_server", label: "App Server 断开", reason: "refused", origin: "node" },
311
+ { layer: "model_auth", label: "需要重新登录", reason: "revoked", origin: "node" },
312
+ ];
313
+ expect((await row(true)).degraded).toEqual(want);
314
+ expect((await row(false)).degraded).toEqual(want);
315
+ });
316
+ // 2026-10-31 —— **两个面必须说同一件事**(外部复核那条「共用的是解析还是判据」)。
317
+ // 同一个节点健康,经两条路到达用户:
318
+ // ① /api/task 的 409 `layers`(派发被拒时当场告诉他哪层坏了)
319
+ // ② /api/status 行里的 `degraded`(列表里一直可见)
320
+ // 二者都由 degradedLayers() 产出,但**没有任何测试保证两个面一致** ——
321
+ // 只要有人在其中一处 map 里漏掉 `origin`,两处就会开始说不同的版本。
322
+ test("parity: 409 layers and status degraded describe the same failure", async () => {
323
+ setHealth({ bridge: "ok", app_server: { ok: false, rtt_ms: null, last_error: "refused" }, model_auth: "revoked" });
324
+ const r = await postTask(ownerToken);
325
+ expect(r.status).toBe(409);
326
+ const fromTask = (r.body.layers as any[]).map((l) => [l.layer, l.reason, l.origin]);
327
+ const fromStatus = ((await row(false)).degraded as any[]).map((l) => [l.layer, l.reason, l.origin]);
328
+ expect(fromTask).toEqual(fromStatus);
329
+ });
330
+
331
+ test("stale → no `degraded`", async () => {
332
+ setHealth(DOWN_APP, Date.now() - NODE_HEALTH_TTL_MS - 1_000);
333
+ expect("degraded" in (await row(true))).toBe(false);
334
+ });
335
+ });
@@ -0,0 +1,121 @@
1
+ // #460(#448 的子项)—— 降级节点拒收新任务。
2
+ //
3
+ // 现场:节点在 Hub 上 idle / in_flight=0,实际 app-server 端口没人听、TUI 变成 `sleep infinity`、
4
+ // 或登录态 revoked —— 派过去的任务**静默排队**,谁都不知道它永远不会被执行。agent-node .94 起
5
+ // 把这几层报上来(report_status.health,node-health-store.ts 只存内存、10 分钟 TTL);这里在**派发**
6
+ // 那一刻读它:新鲜的健康报告说某一层坏了,就直接拒,带上是哪一层、怎么修。
7
+ //
8
+ // 🔴 只拒「确知坏了」的:没有报告(老 agent-node / claude-code 节点)、报告已过期(>TTL)、
9
+ // 或某层没报(比如非共存节点没有 tui)= 不知道 = 放行。宁可像以前一样排队,
10
+ // 也不能因为缺数据把一台正常的老节点挡在外面。
11
+ // 🔴 只管**新任务派发**(send_task / REST /api/task / 定时任务 / retry / reassign / broadcast)。
12
+ // 回复、send_message、ack 不经过这里 —— 降级节点发出的回复照常送达,发给它的普通消息也照常入库。
13
+ // 逃生口:用户(不是节点令牌)可以带 force=true 强制派发 —— 比如明知 TUI 不在、但 bridge 能干活。
14
+ // 节点令牌不能 force:不让 agent 绕过对另一个 agent 的健康判断。
15
+
16
+ import { readNodeHealth, type NodeHealth } from "./node-health-store.js";
17
+
18
+ export type DegradedLayer = {
19
+ layer: "app_server" | "tui" | "model_auth";
20
+ /** 机器可读的原因:app_server 的错误摘要 / tui 的 reason / model_auth 的状态。 */
21
+ reason: string;
22
+ /**
23
+ * 2026-10-31 —— `reason` 是**谁写的**。
24
+ *
25
+ * `app_server.reason` 有两个生产者:节点上报的 `last_error`(≤200 任意串),
26
+ * 和 Hub 在自己这边合成的字面量 `"unreachable"`(节点没报错误摘要时)。
27
+ * 两者在同一个字段里 ⇒ 读者无法区分「节点说它 unreachable」与「Hub 根本没能问到节点」。
28
+ *
29
+ * ⇒ 改名 / 拆分 catch 都拦不住:**上游可以原样声明 `"unreachable"`**(`:148` 同形)。
30
+ * 所以判别力只能来自一个独立的来源字段。
31
+ */
32
+ origin: "node" | "hub";
33
+ /** 给人看的一句(中文,与 app 的「降级」徽标同一套措辞)。 */
34
+ label: string;
35
+ hint: string;
36
+ };
37
+
38
+ const TUI_LABEL: Record<string, string> = {
39
+ "session-missing": "TUI 会话不在",
40
+ "pane-dead": "TUI 已退出",
41
+ "sleep-placeholder": "TUI 被占位(sleep)",
42
+ "tmux-unavailable": "tmux 不可用",
43
+ };
44
+
45
+ /** 一份健康报告里确知坏掉的层;没报的层不算。 */
46
+ export function degradedLayers(health: NodeHealth | null | undefined): DegradedLayer[] {
47
+ if (!health) return [];
48
+ const out: DegradedLayer[] = [];
49
+ if (health.app_server && health.app_server.ok === false) {
50
+ out.push({
51
+ layer: "app_server",
52
+ reason: (health.app_server.last_error || "unreachable").slice(0, 200),
53
+ // 没有 upstream 摘要时这个串是**我们**写的;有则逐字是节点的。
54
+ // ⚠️ 节点自己也可以报 "unreachable" ⇒ 只有这一格能分辨。
55
+ origin: health.app_server.last_error ? "node" : "hub",
56
+ label: "App Server 断开",
57
+ hint: "节点的 codex app-server 端口没有响应:在节点所在机器上重启该节点(anet node restart <别名>)",
58
+ });
59
+ }
60
+ if (health.tui && health.tui.ok === false) {
61
+ out.push({
62
+ layer: "tui",
63
+ reason: health.tui.reason,
64
+ origin: "node",
65
+ label: TUI_LABEL[health.tui.reason] ?? "TUI 不可用",
66
+ hint: "共存 TUI 不在运行:在节点所在机器上重新拉起该节点的 TUI(anet node restart <别名>)",
67
+ });
68
+ }
69
+ if (health.model_auth === "revoked" || health.model_auth === "expired") {
70
+ out.push({
71
+ layer: "model_auth",
72
+ reason: health.model_auth,
73
+ origin: "node",
74
+ label: health.model_auth === "revoked" ? "需要重新登录" : "登录已过期",
75
+ hint: "该节点自己的 CODEX_HOME 需要重新登录(在那台机器上:CODEX_HOME=<节点目录>/codex-home codex login);不要拷别的节点的 auth.json",
76
+ });
77
+ }
78
+ return out;
79
+ }
80
+
81
+ export type HealthGuardResult =
82
+ | { ok: true; forced?: true }
83
+ | {
84
+ ok: false;
85
+ error: "node_degraded";
86
+ alias: string;
87
+ layers: DegradedLayer[];
88
+ message: string;
89
+ hint: string;
90
+ health_observed_ms_ago: number;
91
+ /** 能不能用 force=true 绕过(只有用户令牌能)。 */
92
+ force_allowed: boolean;
93
+ };
94
+
95
+ /**
96
+ * 派发前的健康门。networkId 用与 report_status 写入时同一个键(sessionNetId = 网络 id ?? "default")。
97
+ * force 只在 forceAllowed(用户令牌)时生效。
98
+ */
99
+ export function assertNodeHealthy(
100
+ alias: string,
101
+ networkId: string | null | undefined,
102
+ opts: { force?: boolean; forceAllowed: boolean; now?: number },
103
+ ): HealthGuardResult {
104
+ if (!alias) return { ok: true };
105
+ const hit = readNodeHealth(networkId ?? "default", alias, opts.now);
106
+ if (!hit) return { ok: true };
107
+ const layers = degradedLayers(hit.health);
108
+ if (!layers.length) return { ok: true };
109
+ if (opts.force && opts.forceAllowed) return { ok: true, forced: true };
110
+ const labels = layers.map(l => l.label).join("、");
111
+ return {
112
+ ok: false,
113
+ error: "node_degraded",
114
+ alias,
115
+ layers,
116
+ message: `节点 ${alias} 处于降级状态(${labels}),任务不会被执行,已拒绝派发。`,
117
+ hint: layers[0].hint + (opts.forceAllowed ? "。确需强制派发可带 force=true。" : ""),
118
+ health_observed_ms_ago: hit.observed_ms_ago,
119
+ force_allowed: opts.forceAllowed,
120
+ };
121
+ }
@@ -0,0 +1,115 @@
1
+ // #448 — report_status.health passthrough (layered node health, report-only).
2
+ //
3
+ // Hub-side contract verified here:
4
+ // 1. A well-formed `health` from the node's own token is kept (memory-only) and readable.
5
+ // 2. Malformed `health` never rejects the report — the node stays online, health reads as null.
6
+ // 3. Another node / a user login cannot speak for this node's health.
7
+ // 4. Stale (> TTL) health reads as null, never as "healthy".
8
+ //
9
+ // 跑法:cd server && COMMHUB_DB=/tmp/node-health.db bun test src/node-health-passthrough.test.ts
10
+ import { afterAll, beforeEach, describe, expect, test } from "bun:test";
11
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
12
+ import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
13
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
14
+ import { db } from "./db.js";
15
+ import { registerTools } from "./tools.js";
16
+ import { clearNodeHealthStore, NODE_HEALTH_TTL_MS, readNodeHealth, recordNodeHealth } from "./node-health-store.js";
17
+
18
+ const NET = "net_health_t";
19
+ const OWNER = "u_health_owner";
20
+ const NODE_A = "health-node-a";
21
+ const NODE_B = "health-node-b";
22
+ const NODE_A_ID = "node_health_a";
23
+ const NODE_B_ID = "node_health_b";
24
+ const T_A = "tok_health_node_a";
25
+ const T_B = "tok_health_node_b";
26
+
27
+ function cleanup() {
28
+ for (const t of ["sessions", "nodes", "api_tokens", "network_members", "networks"]) {
29
+ try { db.run(`DELETE FROM ${t} WHERE network_id = ?1`, [NET]); } catch {}
30
+ }
31
+ try { db.run("DELETE FROM users WHERE user_id = ?1", [OWNER]); } catch {}
32
+ clearNodeHealthStore();
33
+ }
34
+ function seed() {
35
+ db.run(`INSERT INTO users (user_id, username, password_hash, role, created_at) VALUES (?1, ?2, 'x', 'user', datetime('now'))`, [OWNER, OWNER]);
36
+ db.run(`INSERT INTO networks (network_id, network_name, owner_id, created_at) VALUES (?1, ?2, ?3, datetime('now'))`, [NET, NET, OWNER]);
37
+ db.run(`INSERT INTO network_members (user_id, network_id, role, joined_at) VALUES (?1, ?2, 'owner', datetime('now'))`, [OWNER, NET]);
38
+ for (const [id, alias, tok] of [[NODE_A_ID, NODE_A, T_A], [NODE_B_ID, NODE_B, T_B]]) {
39
+ db.run(`INSERT INTO nodes (node_id, node_name, alias, network_id, owner_user_id, created_at, updated_at) VALUES (?1, ?2, ?3, ?4, ?5, datetime('now'), datetime('now'))`, [id, alias, alias, NET, OWNER]);
40
+ db.run(`INSERT INTO api_tokens (token_id, user_id, network_id, scope, name, token_hash, expires_at, revoked_at, bound_node_id) VALUES (?1, ?2, ?3, 'network', ?4, ?5, NULL, NULL, ?6)`, [tok, OWNER, NET, `node:${alias}`, `hash_${tok}`, id]);
41
+ }
42
+ }
43
+
44
+ async function connectAs(alias: string, tokenId: string) {
45
+ const server = new McpServer({ name: "health-test", version: "1" });
46
+ registerTools(server, undefined, NET, OWNER, alias, true, tokenId);
47
+ const client = new Client({ name: "health-client", version: "1" });
48
+ const [ct, st] = InMemoryTransport.createLinkedPair();
49
+ await server.connect(st);
50
+ await client.connect(ct);
51
+ const call = async (args: Record<string, unknown>) => {
52
+ const r: any = await client.callTool({ name: "report_status", arguments: args });
53
+ return { isError: r.isError === true, body: JSON.parse(r.content?.[0]?.text ?? "null") };
54
+ };
55
+ return { call, close: async () => { await client.close(); await server.close(); } };
56
+ }
57
+
58
+ const HEALTH = {
59
+ bridge: "ok",
60
+ app_server: { ok: false, rtt_ms: null, last_error: "connection refused" },
61
+ tui: { ok: false, reason: "sleep-placeholder" },
62
+ model_auth: "revoked",
63
+ };
64
+
65
+ beforeEach(() => { cleanup(); seed(); });
66
+ afterAll(cleanup);
67
+
68
+ describe("#448 report_status.health passthrough", () => {
69
+ test("node's own token: stored and readable as reported", async () => {
70
+ const a = await connectAs(NODE_A, T_A);
71
+ try {
72
+ const r = await a.call({ resume_id: `sdk-${NODE_A_ID}`, alias: NODE_A, status: "idle", node_id: NODE_A_ID, health: HEALTH });
73
+ expect(r.isError).toBe(false);
74
+ expect(r.body.ok).not.toBe(false);
75
+ } finally { await a.close(); }
76
+ expect(readNodeHealth(NET, NODE_A)?.health).toEqual(HEALTH as any);
77
+ });
78
+
79
+ test("malformed health never rejects the report (node stays online); it reads as not reported", async () => {
80
+ const a = await connectAs(NODE_A, T_A);
81
+ try {
82
+ for (const bad of [42, "ok", { app_server: { ok: "yes" }, model_auth: "weird", tui: [] }, { app_server: { ok: true, rtt_ms: NaN, last_error: 5 } }]) {
83
+ const r = await a.call({ resume_id: `sdk-${NODE_A_ID}`, alias: NODE_A, status: "idle", node_id: NODE_A_ID, health: bad });
84
+ expect(r.isError).toBe(false);
85
+ }
86
+ } finally { await a.close(); }
87
+ expect(db.get<{ s: string }>("SELECT status AS s FROM sessions WHERE alias = ?1 AND network_id = ?2", NODE_A, NET)?.s).toBe("idle");
88
+ // 最后一份里 app_server.ok 合法、其余两格降级成 null —— 留下的是能证实的部分,不是编出来的。
89
+ expect(readNodeHealth(NET, NODE_A)?.health.app_server).toEqual({ ok: true, rtt_ms: null, last_error: null });
90
+ });
91
+
92
+ test("another node's token cannot speak for this node", async () => {
93
+ const b = await connectAs(NODE_B, T_B);
94
+ try {
95
+ await b.call({ resume_id: `sdk-${NODE_A_ID}`, alias: NODE_A, status: "idle", node_id: NODE_A_ID, health: HEALTH });
96
+ } finally { await b.close(); }
97
+ expect(readNodeHealth(NET, NODE_A)).toBeNull();
98
+ });
99
+
100
+ test("stale health reads as null, never as healthy", () => {
101
+ const t0 = 1_000_000;
102
+ recordNodeHealth(NET, NODE_A, { bridge: "ok", model_auth: "ok" }, t0);
103
+ expect(readNodeHealth(NET, NODE_A, t0 + 1_000)?.observed_ms_ago).toBe(1_000);
104
+ expect(readNodeHealth(NET, NODE_A, t0 + NODE_HEALTH_TTL_MS + 1)).toBeNull();
105
+ });
106
+
107
+ test("a report without health leaves the last one (heartbeats from older code paths do not erase it)", async () => {
108
+ const a = await connectAs(NODE_A, T_A);
109
+ try {
110
+ await a.call({ resume_id: `sdk-${NODE_A_ID}`, alias: NODE_A, status: "idle", node_id: NODE_A_ID, health: HEALTH });
111
+ await a.call({ resume_id: `sdk-${NODE_A_ID}`, alias: NODE_A, status: "working", node_id: NODE_A_ID });
112
+ } finally { await a.close(); }
113
+ expect(readNodeHealth(NET, NODE_A)?.health.model_auth).toBe("revoked");
114
+ });
115
+ });