@sleep2agi/commhub-server 0.9.0-preview.28 → 0.9.0-preview.280

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 (479) hide show
  1. package/README.md +120 -3
  2. package/bin/commhub.ts +93 -9
  3. package/build-info.json +7 -0
  4. package/package.json +10 -8
  5. package/src/ack-create-request-transport.test.ts +182 -0
  6. package/src/ack-create-request.test.ts +52 -8
  7. package/src/admin-delete-network-http.test.ts +229 -0
  8. package/src/admin-networks-http.test.ts +88 -0
  9. package/src/adoption-candidates-http.test.ts +140 -0
  10. package/src/adoption-candidates.ts +158 -0
  11. package/src/agent-access-batch.test.ts +215 -0
  12. package/src/agent-access.ts +585 -0
  13. package/src/agent-acl-groups-http.test.ts +247 -0
  14. package/src/agent-acl-http.test.ts +691 -0
  15. package/src/agent-acl-slice3-http.test.ts +888 -0
  16. package/src/agent-department-grants.test.ts +129 -0
  17. package/src/agent-department-http.test.ts +215 -0
  18. package/src/agent-groups-perf.test.ts +228 -0
  19. package/src/agent-notice.ts +48 -0
  20. package/src/agent-team-whoami-http.test.ts +98 -0
  21. package/src/agent-teams-http.test.ts +330 -0
  22. package/src/agent-teams.ts +244 -0
  23. package/src/alias-baseline-2571.test.ts +162 -0
  24. package/src/alias-baseline.ts +113 -0
  25. package/src/alias-filter.test.ts +72 -0
  26. package/src/alias-filter.ts +42 -0
  27. package/src/all-status-cache.test.ts +31 -0
  28. package/src/all-status-cache.ts +37 -0
  29. package/src/anet-variant-real-daemon-http.test.ts +184 -0
  30. package/src/api-host-supervisors-can-create-nodes.test.ts +350 -0
  31. package/src/api-host-supervisors-fallback.test.ts +6 -7
  32. package/src/api-host-supervisors-runtime-readiness.test.ts +195 -0
  33. package/src/api-nodes-shape.test.ts +131 -4
  34. package/src/auth-abuse-counters.test.ts +144 -0
  35. package/src/auth-abuse-counters.ts +122 -0
  36. package/src/auth-layer-shape.test.ts +105 -0
  37. package/src/auth-password-change-cli-tokens-http.test.ts +423 -0
  38. package/src/auth-sessions-http.test.ts +260 -0
  39. package/src/auth-validate.test.ts +5 -1
  40. package/src/auth-weak-password-login.test.ts +176 -0
  41. package/src/auth.ts +641 -68
  42. package/src/backfill-node-ids-2664.test.ts +128 -0
  43. package/src/binding-generation-http.test.ts +141 -0
  44. package/src/blocked-exit-on-reply.test.ts +122 -0
  45. package/src/broadcast-fanout-2721.test.ts +149 -0
  46. package/src/broadcast-fanout.ts +117 -0
  47. package/src/build-commit-report-http.test.ts +218 -0
  48. package/src/build-info.test.ts +60 -0
  49. package/src/build-info.ts +85 -0
  50. package/src/chain-attempt-dedup.test.ts +447 -0
  51. package/src/clip.test.ts +36 -0
  52. package/src/clip.ts +48 -0
  53. package/src/clone-node.test.ts +269 -0
  54. package/src/clone-node.ts +289 -0
  55. package/src/codex-config-flags.ts +77 -0
  56. package/src/codex-fork-contract.ts +23 -0
  57. package/src/codex-fork-migration.test.ts +38 -0
  58. package/src/commhub-bin-args.test.ts +85 -0
  59. package/src/config-apply-sec1.test.ts +37 -0
  60. package/src/config-apply-validate.test.ts +188 -1
  61. package/src/config-apply-validate.ts +226 -3
  62. package/src/contracts/session-project-create.ts +14 -0
  63. package/src/contracts/session-source-delivery.ts +16 -0
  64. package/src/contracts/session-source-directory.ts +54 -0
  65. package/src/contracts/session-source-evidence.ts +72 -0
  66. package/src/contracts/session-source-execution.ts +24 -0
  67. package/src/contracts/session-source-transport.ts +18 -0
  68. package/src/cors-origins.test.ts +73 -0
  69. package/src/create-node-blocked-gate.test.ts +365 -0
  70. package/src/create-node-error-domain.test.ts +127 -0
  71. package/src/create-node-network-id.test.ts +91 -0
  72. package/src/create-node-tool-schema.test.ts +59 -0
  73. package/src/create-node-unicode-name.test.ts +126 -0
  74. package/src/create-node-validate.test.ts +199 -7
  75. package/src/create-node-validate.ts +232 -15
  76. package/src/create-node-workdir.test.ts +298 -0
  77. package/src/create-node.ts +88 -23
  78. package/src/daemon-provider-snapshots.test.ts +453 -0
  79. package/src/daemon-provider-snapshots.ts +327 -0
  80. package/src/daemon-token-owner-http.test.ts +346 -0
  81. package/src/dashboard-slash-routing-http.test.ts +109 -0
  82. package/src/db-adapter-guard.test.ts +148 -0
  83. package/src/db-adapter-translate.test.ts +128 -0
  84. package/src/db-adapter.ts +377 -91
  85. package/src/db-timestamp.test.ts +38 -0
  86. package/src/db-timestamp.ts +15 -0
  87. package/src/db.ts +1334 -154
  88. package/src/delivered-without-push.test.ts +140 -0
  89. package/src/department-groups-http.test.ts +209 -0
  90. package/src/department-groups-sync-http.test.ts +328 -0
  91. package/src/department-groups.ts +434 -0
  92. package/src/department-heads-http.test.ts +350 -0
  93. package/src/department-heads.ts +226 -0
  94. package/src/departments-agent-count.test.ts +139 -0
  95. package/src/departments-http.test.ts +185 -0
  96. package/src/departments-mcp-error-domain.test.ts +135 -0
  97. package/src/departments-mcp.test.ts +128 -0
  98. package/src/departments.ts +318 -0
  99. package/src/desktop-message-seam.test.ts +150 -0
  100. package/src/desktop-message.test.ts +368 -0
  101. package/src/dm-file-scope-http.test.ts +227 -0
  102. package/src/empty-node-sweeper.test.ts +104 -0
  103. package/src/empty-node-sweeper.ts +88 -0
  104. package/src/env-transport-gate-http.test.ts +240 -0
  105. package/src/env-transport-gate.ts +80 -0
  106. package/src/env-transport-policy.ts +56 -0
  107. package/src/etag-of.test.ts +57 -0
  108. package/src/event-loop-lag.test.ts +180 -0
  109. package/src/event-loop-lag.ts +201 -0
  110. package/src/expiry-counters.test.ts +187 -0
  111. package/src/expiry-counters.ts +227 -0
  112. package/src/expiry-evidence-db.test.ts +103 -0
  113. package/src/ext-token-shared.test.ts +60 -0
  114. package/src/ext-validation-regression.test.ts +10 -17
  115. package/src/external-schedule-edits-http.test.ts +321 -0
  116. package/src/external-schedule-edits-timestamps.test.ts +64 -0
  117. package/src/external-schedule-edits.ts +327 -0
  118. package/src/file-download-authz-dev-open.test.ts +1 -1
  119. package/src/file-download-authz.test.ts +25 -4
  120. package/src/file-download-date-bucket.test.ts +1 -1
  121. package/src/file-network-scope-dev-open.test.ts +1 -1
  122. package/src/file-network-scope.test.ts +7 -6
  123. package/src/group-membership-events-http.test.ts +264 -0
  124. package/src/group-messages-http.test.ts +483 -0
  125. package/src/group-messages.ts +297 -0
  126. package/src/health-anon-readers.ts +67 -0
  127. package/src/health-host-disk-2756.test.ts +71 -0
  128. package/src/health-identity-root.test.ts +98 -0
  129. package/src/health-redaction.test.ts +1599 -7
  130. package/src/health-status-reasons.test.ts +84 -0
  131. package/src/host-supervisor-identity-http.test.ts +152 -0
  132. package/src/host-supervisor-identity.ts +92 -0
  133. package/src/host-supervisors-daemon-version.test.ts +137 -0
  134. package/src/http-gzip.test.ts +94 -0
  135. package/src/http-gzip.ts +112 -0
  136. package/src/hub-catalog-read-http.test.ts +664 -0
  137. package/src/hub-provider-config.test.ts +166 -0
  138. package/src/hub-provider-config.ts +202 -0
  139. package/src/hub-timestamp.test.ts +38 -0
  140. package/src/hub-timestamp.ts +27 -0
  141. package/src/human-dm.ts +215 -0
  142. package/src/human-inbox.ts +42 -0
  143. package/src/inbox-caller.ts +49 -0
  144. package/src/inbox-count-sse.test.ts +318 -0
  145. package/src/inbox-count.ts +29 -0
  146. package/src/index.ts +6 -2
  147. package/src/license-state.test.ts +105 -0
  148. package/src/license-state.ts +48 -0
  149. package/src/lifecycle-error-domain.test.ts +149 -0
  150. package/src/list-ack-error-domain.test.ts +108 -0
  151. package/src/list-host-supervisors.test.ts +15 -4
  152. package/src/list-tasks-omit-default.test.ts +84 -0
  153. package/src/list-tasks-since-last.test.ts +100 -0
  154. package/src/list-tasks-since.test.ts +104 -0
  155. package/src/mcp-context-cost-http.test.ts +127 -0
  156. package/src/mcp-inflight.test.ts +92 -0
  157. package/src/mcp-inflight.ts +38 -0
  158. package/src/mcp-projects-events-http.test.ts +168 -0
  159. package/src/mcp-route-label-http.test.ts +120 -0
  160. package/src/mcp-write-network-resolution.test.ts +21 -0
  161. package/src/member-audit-network-id-http.test.ts +93 -0
  162. package/src/member-presence-http.test.ts +210 -0
  163. package/src/messages-ack-by-agent-http.test.ts +142 -0
  164. package/src/migrate-sqlite-to-pg.test.ts +47 -0
  165. package/src/migrate-sqlite-to-pg.ts +263 -0
  166. package/src/model-auth-notify-http.test.ts +181 -0
  167. package/src/model-auth-notify.ts +116 -0
  168. package/src/network-name-http.test.ts +75 -0
  169. package/src/network-quota-config.test.ts +100 -0
  170. package/src/network-rules-http.test.ts +169 -0
  171. package/src/network-rules.ts +129 -0
  172. package/src/network-scope.ts +222 -3
  173. package/src/network-token-membership-http.test.ts +186 -0
  174. package/src/node-alias-control.ts +12 -0
  175. package/src/node-attrs.test.ts +47 -2
  176. package/src/node-caller-identity-http.test.ts +1108 -0
  177. package/src/node-config-last-update.test.ts +126 -0
  178. package/src/node-config-last-update.ts +107 -0
  179. package/src/node-create-request-status-http.test.ts +105 -0
  180. package/src/node-daemon-bindings.test.ts +142 -0
  181. package/src/node-daemon-bindings.ts +135 -0
  182. package/src/node-failure-alert.test.ts +150 -0
  183. package/src/node-failure-alert.ts +177 -0
  184. package/src/node-files-transport.test.ts +350 -0
  185. package/src/node-health-codex-login.test.ts +120 -0
  186. package/src/node-health-dispatch-http.test.ts +373 -0
  187. package/src/node-health-guard-origin.test.ts +98 -0
  188. package/src/node-health-guard.ts +121 -0
  189. package/src/node-health-passthrough.test.ts +115 -0
  190. package/src/node-health-store.ts +101 -0
  191. package/src/node-id-alias.test.ts +190 -0
  192. package/src/node-identity-conflict-http.test.ts +269 -0
  193. package/src/node-identity-conflict.ts +142 -0
  194. package/src/node-identity-mismatch.test.ts +74 -0
  195. package/src/node-in-flight-tasks.ts +31 -0
  196. package/src/node-lifecycle-controllable.test.ts +64 -0
  197. package/src/node-lifecycle-controllable.ts +53 -0
  198. package/src/node-lifecycle-read-http.test.ts +257 -0
  199. package/src/node-lifecycle-read.ts +126 -0
  200. package/src/node-logs-transport.test.ts +266 -0
  201. package/src/node-peer-allowlist.test.ts +114 -0
  202. package/src/node-permissions-http.test.ts +901 -0
  203. package/src/node-permissions.ts +496 -0
  204. package/src/node-request-retention.test.ts +281 -0
  205. package/src/node-request-retention.ts +120 -0
  206. package/src/node-secret-rest-http.test.ts +190 -0
  207. package/src/node-secret-vault-inject.test.ts +132 -0
  208. package/src/node-security-write-gate.test.ts +72 -0
  209. package/src/node-serviceability.test.ts +86 -0
  210. package/src/node-serviceability.ts +124 -0
  211. package/src/node-session-control-http.test.ts +398 -0
  212. package/src/node-session-control.ts +316 -0
  213. package/src/node-skills-transport.test.ts +212 -0
  214. package/src/node-token-identity.ts +35 -0
  215. package/src/node-token-ownership.ts +51 -0
  216. package/src/node-token-privilege-http.test.ts +224 -0
  217. package/src/node-token-usage.test.ts +128 -0
  218. package/src/node-token-usage.ts +228 -0
  219. package/src/not-found-api-http.test.ts +67 -0
  220. package/src/observer-avatar-http.test.ts +103 -10
  221. package/src/observer-push.test.ts +76 -0
  222. package/src/opencode-catalog-report-http.test.ts +139 -0
  223. package/src/opencode-v2-catalog.test.ts +282 -0
  224. package/src/opencode-v2-catalog.ts +357 -0
  225. package/src/package-self-contained.test.ts +32 -0
  226. package/src/package-version-consistency.test.ts +25 -0
  227. package/src/parent-infer.test.ts +82 -0
  228. package/src/parent-infer.ts +110 -0
  229. package/src/parent-source-marker.test.ts +70 -0
  230. package/src/parent-source-orphan.test.ts +121 -0
  231. package/src/password-network-tokens.ts +25 -0
  232. package/src/peer-reply-atomic.test.ts +462 -0
  233. package/src/peer-reply-capability.test.ts +70 -0
  234. package/src/pg-worker.ts +87 -0
  235. package/src/poll-pacing.test.ts +74 -0
  236. package/src/poll-pacing.ts +123 -0
  237. package/src/probe-provider-error-domain.test.ts +145 -0
  238. package/src/project-dir-warn-clear-http.test.ts +132 -0
  239. package/src/project-viewer-can-http.test.ts +116 -0
  240. package/src/provider-skill-error-domain.test.ts +130 -0
  241. package/src/provider-tools-shape.test.ts +165 -0
  242. package/src/push.test.ts +20 -10
  243. package/src/push.ts +373 -9
  244. package/src/redact-tokens.test.ts +86 -0
  245. package/src/redact-tokens.ts +43 -0
  246. package/src/rejection-counters.test.ts +72 -0
  247. package/src/rejection-counters.ts +173 -0
  248. package/src/rename.ts +38 -10
  249. package/src/reply-target-derivation.test.ts +96 -0
  250. package/src/report-status-handover-capabilities-http.test.ts +124 -0
  251. package/src/report-status-host-schema.test.ts +272 -0
  252. package/src/report-status-resume-id-handover.test.ts +184 -0
  253. package/src/report-status-secret-keys.test.ts +140 -0
  254. package/src/require-explicit-test-db.ts +9 -0
  255. package/src/requirement-due-reminders-http.test.ts +620 -0
  256. package/src/requirement-due-reminders.ts +416 -0
  257. package/src/requirement-events-http.test.ts +318 -0
  258. package/src/requirement-events.test.ts +45 -0
  259. package/src/requirement-events.ts +144 -0
  260. package/src/requirement-notify.ts +165 -0
  261. package/src/requirement-participant-notify-http.test.ts +313 -0
  262. package/src/requirement-tag-ops-http.test.ts +454 -0
  263. package/src/requirement-tag-tree-http.test.ts +275 -0
  264. package/src/requirement-tag-tree.ts +127 -0
  265. package/src/requirement-tags.ts +102 -0
  266. package/src/requirements-abandoned-http.test.ts +236 -0
  267. package/src/requirements-assignee-drift.test.ts +154 -0
  268. package/src/requirements-comment-http.test.ts +174 -0
  269. package/src/requirements-completed-at-migration.test.ts +70 -0
  270. package/src/requirements-errors-mcp-http.test.ts +164 -0
  271. package/src/requirements-errors.ts +158 -0
  272. package/src/requirements-etag-http.test.ts +91 -0
  273. package/src/requirements-http.test.ts +709 -0
  274. package/src/requirements-last-event-http.test.ts +349 -0
  275. package/src/requirements-list-mcp-slim-http.test.ts +155 -0
  276. package/src/requirements-list-slim-http.test.ts +324 -0
  277. package/src/requirements-mcp.test.ts +180 -0
  278. package/src/requirements-migrate.ts +283 -0
  279. package/src/requirements-owner-strict-mcp-http.test.ts +145 -0
  280. package/src/requirements-people-http.test.ts +114 -0
  281. package/src/requirements-people-mcp-http.test.ts +231 -0
  282. package/src/requirements-people.ts +196 -0
  283. package/src/requirements-priority-lowest.test.ts +114 -0
  284. package/src/requirements-projects-network-echo.test.ts +57 -0
  285. package/src/requirements-projects-parent-http.test.ts +123 -0
  286. package/src/requirements-search-http.test.ts +182 -0
  287. package/src/requirements-search.test.ts +32 -0
  288. package/src/requirements-search.ts +145 -0
  289. package/src/requirements-seq-http.test.ts +193 -0
  290. package/src/requirements-seq-migration.test.ts +63 -0
  291. package/src/requirements-start-date.test.ts +125 -0
  292. package/src/requirements-stats-http.test.ts +357 -0
  293. package/src/requirements-stats.ts +195 -0
  294. package/src/requirements-status-participants-http.test.ts +222 -0
  295. package/src/requirements-upgrade-e2e.test.ts +126 -0
  296. package/src/requirements-upgrade-migration.test.ts +98 -0
  297. package/src/requirements.ts +2205 -0
  298. package/src/resolve-port.test.ts +57 -0
  299. package/src/resolve-port.ts +44 -0
  300. package/src/rest-alias-network-fallback-http.test.ts +123 -0
  301. package/src/rest-alias-network-fallback.test.ts +60 -0
  302. package/src/rest-alias-network-fallback.ts +65 -0
  303. package/src/rest-explicit-columns-http.test.ts +266 -0
  304. package/src/rest-identity.test.ts +130 -0
  305. package/src/rest-identity.ts +141 -0
  306. package/src/rest-projections.ts +72 -0
  307. package/src/rest-read-network-real-chain.test.ts +97 -0
  308. package/src/rest-write-network-resolution.test.ts +133 -0
  309. package/src/restart-readiness-http.test.ts +121 -0
  310. package/src/restricted-files.ts +76 -0
  311. package/src/retention-first-sweep-quiet.test.ts +31 -0
  312. package/src/retention.test.ts +100 -40
  313. package/src/retention.ts +423 -104
  314. package/src/route-callers-http.test.ts +120 -0
  315. package/src/route-timing.test.ts +218 -0
  316. package/src/route-timing.ts +262 -0
  317. package/src/rules-file-duplicate-alias.test.ts +234 -0
  318. package/src/rules-file-session-target.test.ts +207 -0
  319. package/src/rules-file-transport.test.ts +280 -0
  320. package/src/runtime-label.test.ts +22 -0
  321. package/src/runtime-label.ts +21 -0
  322. package/src/runtime-readiness.ts +90 -0
  323. package/src/schedule-agent-mcp-http.test.ts +486 -0
  324. package/src/schedule-agent.ts +252 -0
  325. package/src/schedule-starved.test.ts +92 -0
  326. package/src/schedule-starved.ts +114 -0
  327. package/src/scheduled-backoff-http.test.ts +203 -0
  328. package/src/scheduled-backoff.ts +64 -0
  329. package/src/scheduled-doorbell-nodeless-session.test.ts +134 -0
  330. package/src/scheduled-failure-backoff-2622.test.ts +145 -0
  331. package/src/scheduled-failure-code.test.ts +73 -0
  332. package/src/scheduled-failure-code.ts +105 -0
  333. package/src/scheduled-failures-http.test.ts +279 -0
  334. package/src/scheduled-failures.ts +425 -0
  335. package/src/scheduled-noprogress-backoff-2622.test.ts +163 -0
  336. package/src/scheduled-offline-backoff-probe.test.ts +56 -0
  337. package/src/scheduled-offline.test.ts +131 -0
  338. package/src/scheduled-offline.ts +66 -0
  339. package/src/scheduled-outcome-guidance-http.test.ts +139 -0
  340. package/src/scheduled-outcome-guidance.ts +9 -0
  341. package/src/scheduled-reply-unread-http.test.ts +212 -0
  342. package/src/scheduled-requirement-wake-http.test.ts +289 -0
  343. package/src/scheduled-requirement-wake.ts +125 -0
  344. package/src/scheduled-run-terminal.test.ts +290 -0
  345. package/src/scheduled-skip-blocking-task.test.ts +120 -0
  346. package/src/scheduled-skip-path-backoff-2622.test.ts +98 -0
  347. package/src/scheduled-stuck-http.test.ts +316 -0
  348. package/src/scheduled-stuck-notice-cap.test.ts +80 -0
  349. package/src/scheduled-stuck.ts +219 -0
  350. package/src/scheduled-tasks-http.test.ts +679 -0
  351. package/src/scheduled-tasks.ts +964 -0
  352. package/src/scheduled-work-outcome-http.test.ts +154 -0
  353. package/src/scheduled-work-outcome.ts +50 -0
  354. package/src/scoped-secrets.ts +167 -0
  355. package/src/secret-echo-negative-control.test.ts +118 -0
  356. package/src/send-dedup-request-id.test.ts +229 -0
  357. package/src/send-reply-agent-warning.test.ts +38 -38
  358. package/src/send-reply-alias-optional.test.ts +131 -0
  359. package/src/send-reply-attachments.test.ts +28 -7
  360. package/src/send_dedup.test.ts +54 -0
  361. package/src/send_dedup.ts +52 -6
  362. package/src/serve-error.test.ts +73 -0
  363. package/src/serve-error.ts +56 -0
  364. package/src/server.ts +5033 -377
  365. package/src/session-fork-store.test.ts +43 -0
  366. package/src/session-fork-store.ts +51 -0
  367. package/src/session-project-attach-migration.test.ts +22 -0
  368. package/src/session-project-attach-native.test.ts +121 -0
  369. package/src/session-project-create-http.test.ts +407 -0
  370. package/src/session-project-create-native.test.ts +290 -0
  371. package/src/session-project-create.ts +159 -0
  372. package/src/session-source-admission-store.ts +58 -0
  373. package/src/session-source-create.test.ts +155 -0
  374. package/src/session-source-create.ts +97 -0
  375. package/src/session-source-delivery.ts +109 -0
  376. package/src/session-source-directory-http.test.ts +192 -0
  377. package/src/session-source-evidence.test.ts +898 -0
  378. package/src/session-source-evidence.ts +191 -0
  379. package/src/session-source-execution.ts +87 -0
  380. package/src/session-source-grant.test.ts +117 -0
  381. package/src/session-source-grant.ts +78 -0
  382. package/src/session-source-issuer.test.ts +250 -0
  383. package/src/session-source-issuer.ts +164 -0
  384. package/src/session-source-transport.ts +375 -0
  385. package/src/session-task-on-dispatch.ts +14 -0
  386. package/src/session-token-ownership-http.test.ts +122 -0
  387. package/src/shared/codex-inventory.ts +41 -0
  388. package/src/shared/daemon-provider-login.ts +31 -0
  389. package/src/shared/daemon-provider-snapshot.ts +52 -0
  390. package/src/shared/external-schedule-contract.ts +60 -0
  391. package/src/shared/node-name.test.ts +62 -0
  392. package/src/shared/node-name.ts +161 -0
  393. package/src/shared/probe-host-allowlist-drift.test.ts +45 -0
  394. package/src/shared/provider-application.ts +29 -0
  395. package/src/shared/provider-collection.ts +14 -0
  396. package/src/shared/provider-initialization.ts +8 -0
  397. package/src/shared/reserved-env-drift.test.ts +10 -0
  398. package/src/shared/scoped-secret-contract.ts +5 -0
  399. package/src/shared/task-content-limit-drift.test.ts +28 -0
  400. package/src/shared/task-content-limit.ts +25 -0
  401. package/src/side-thread-command-transport.test.ts +312 -0
  402. package/src/side-thread-command-transport.ts +318 -0
  403. package/src/side-thread-http-integration.test.ts +233 -0
  404. package/src/side-thread-http.ts +504 -0
  405. package/src/side-thread-production.test.ts +72 -0
  406. package/src/side-thread-production.ts +118 -0
  407. package/src/side-thread.test.ts +1067 -0
  408. package/src/side-thread.ts +2499 -0
  409. package/src/skillhub-http.test.ts +144 -0
  410. package/src/sqlite-close-checkpoint.test.ts +78 -0
  411. package/src/start-node.test.ts +265 -0
  412. package/src/start-request-error-domain.test.ts +182 -0
  413. package/src/start-skill-error-domain.test.ts +122 -0
  414. package/src/stats-versions-http.test.ts +65 -0
  415. package/src/status-alias-filter-http.test.ts +106 -0
  416. package/src/status-alias-resolver-http.test.ts +163 -0
  417. package/src/status-alias-resolver.ts +19 -0
  418. package/src/status-node-id-filter-http.test.ts +161 -0
  419. package/src/status-node-secrets-capable-http.test.ts +44 -0
  420. package/src/status-read-cache-http.test.ts +229 -0
  421. package/src/status-read-cache.ts +129 -0
  422. package/src/stop-delete-node.test.ts +567 -1
  423. package/src/task-access-http.test.ts +407 -0
  424. package/src/task-access.ts +420 -0
  425. package/src/task-activity.ts +60 -0
  426. package/src/task-auth-origin-http.test.ts +93 -0
  427. package/src/task-auth-origin.test.ts +24 -0
  428. package/src/task-auth-origin.ts +7 -0
  429. package/src/task-consumption.test.ts +512 -0
  430. package/src/task-diagnostic-http.test.ts +149 -0
  431. package/src/task-diagnostic.test.ts +115 -0
  432. package/src/task-diagnostic.ts +118 -0
  433. package/src/task-expiry-notice-http.test.ts +298 -0
  434. package/src/task-expiry-notice.ts +318 -0
  435. package/src/task-idempotency-mcp.test.ts +148 -0
  436. package/src/task-idempotency.test.ts +76 -0
  437. package/src/task-idempotency.ts +81 -0
  438. package/src/task-late-replies.ts +54 -0
  439. package/src/task-late-reply-http.test.ts +227 -0
  440. package/src/task-lifecycle-watcher-busy.test.ts +114 -0
  441. package/src/task-lifecycle-watcher.test.ts +209 -0
  442. package/src/task-lifecycle-watcher.ts +109 -0
  443. package/src/task-network-resolution-http.test.ts +140 -0
  444. package/src/task-not-found-error-domain.test.ts +117 -0
  445. package/src/task-offline-delivery-2668.test.ts +119 -0
  446. package/src/task-offline-delivery.ts +81 -0
  447. package/src/task-orphan-http.test.ts +287 -0
  448. package/src/task-orphan.ts +119 -0
  449. package/src/task-queue-ahead-http.test.ts +309 -0
  450. package/src/task-queue-ahead-scale.test.ts +236 -0
  451. package/src/task-queue-ahead.ts +186 -0
  452. package/src/task-rejection-log-http.test.ts +160 -0
  453. package/src/task-rejection-log.ts +61 -0
  454. package/src/task-sender-http.test.ts +143 -0
  455. package/src/task-stale-open-http.test.ts +292 -0
  456. package/src/task-stale-open.ts +103 -0
  457. package/src/tasks-pagination-http.test.ts +141 -0
  458. package/src/team-rules-status-http.test.ts +90 -0
  459. package/src/team-rules-status.test.ts +59 -0
  460. package/src/team-rules-status.ts +72 -0
  461. package/src/tmux-allowlist.test.ts +48 -0
  462. package/src/token-usage-http.test.ts +156 -0
  463. package/src/tool-audience-http.test.ts +266 -0
  464. package/src/tool-audience.ts +130 -0
  465. package/src/tools-list-json-schema.test.ts +94 -0
  466. package/src/tools.ts +4530 -471
  467. package/src/update-node-config-error-domain.test.ts +107 -0
  468. package/src/uploads-http.test.ts +6 -4
  469. package/src/uploads.ts +23 -4
  470. package/src/user-avatar-http.test.ts +74 -0
  471. package/src/user-avatar-image.ts +160 -0
  472. package/src/user-avatar-migration.test.ts +25 -0
  473. package/src/user-avatar-upload-http.test.ts +353 -0
  474. package/src/user-inbox-by-agent-http.test.ts +189 -0
  475. package/src/user-inbox-netid-notnull.test.ts +122 -0
  476. package/src/user-inbox-read-http.test.ts +223 -0
  477. package/src/user-inbox-schema.test.ts +54 -0
  478. package/src/version-stats.test.ts +66 -0
  479. package/src/version-stats.ts +109 -0
@@ -0,0 +1,964 @@
1
+ import { bindRequirementWake, parseRequirementWake, publicRequirementWake, requirementWakeHead, pollRequirementWakes, type RequirementWake } from "./scheduled-requirement-wake.js";
2
+ import { scheduledOutcomeGuidance } from "./scheduled-outcome-guidance.js";
3
+ import { parseBackoffPolicy, refreshScheduledBackoff } from "./scheduled-backoff.js";
4
+ import { db, logTaskEvent } from "./db.js";
5
+ import { sessionTaskOnDispatch } from "./session-task-on-dispatch.js";
6
+ import { TASK_CONTENT_MAX, sessionTaskPreview } from "./shared/task-content-limit.js";
7
+ import { assertNodeActive } from "./lifecycle-guard.js";
8
+ import { assertNodeHealthy } from "./node-health-guard.js";
9
+ import { nodeCanAcceptDispatch } from "./node-serviceability.js";
10
+ import { addNetworkScope, canRestWriteNetwork, singleNetworkId, type RestNetworkScope } from "./network-scope.js";
11
+ import { canMessageAgent, type AgentRef } from "./agent-access.js";
12
+ import { dispatchVerdict } from "./node-permissions.js";
13
+ import { hasSubscribers, pushEvent, pushNetworkObserverEvent } from "./push.js";
14
+ import { SCHEDULED_TASK_STORAGE_SELECT } from "./rest-projections.js";
15
+ import { blockerTimedOut, blockerTimeoutClock, expireStuckBlocker, sendStuckNotice, skipsBlockedBy, stuckNoticeSkips, stuckTimeoutMs, type StuckNotice } from "./scheduled-stuck.js";
16
+ import { applyNoProgressBackoff, evaluateScheduleFailures, failureSummary } from "./scheduled-failures.js";
17
+ import { parseHubTimestamp } from "./hub-timestamp.js";
18
+ import { busyElapsedNote } from "./scheduled-failure-code.js";
19
+ import { scheduledTargetOffline, sendOfflineNotice, type OfflineNotice } from "./scheduled-offline.js";
20
+
21
+ export type ScheduleSpec =
22
+ | { type: "once"; run_at: string }
23
+ | { type: "interval"; every_seconds: number }
24
+ | { type: "daily"; time: string }
25
+ | { type: "weekly"; time: string; weekdays: number[] };
26
+
27
+ export type MisfirePolicy = "catch_up_once" | "skip";
28
+
29
+ export type ScheduledRow = {
30
+ schedule_id: string;
31
+ network_id: string;
32
+ created_by: string | null;
33
+ created_by_node_id: string | null;
34
+ name: string;
35
+ target_node_id: string;
36
+ target_alias: string;
37
+ task_content: string;
38
+ priority: "high" | "normal" | "low";
39
+ schedule_type: ScheduleSpec["type"];
40
+ schedule_json: string;
41
+ timezone: string;
42
+ overlap_policy: "skip" | "allow";
43
+ misfire_policy: MisfirePolicy;
44
+ status: "active" | "paused" | "completed" | "cancelled";
45
+ next_run_at: string | null;
46
+ last_run_at: string | null;
47
+ no_change_backoff: number;
48
+ backoff_streak: number;
49
+ backoff_effective_seconds: number | null;
50
+ backoff_last_run_id: string | null;
51
+ requirement_wake_json: string | null;
52
+ requirement_wake_cursor: number;
53
+ requirement_wake_last_event_id: number | null;
54
+ revision: number;
55
+ created_at: string;
56
+ updated_at: string;
57
+ };
58
+
59
+ type RequestAuth = {
60
+ userId: string;
61
+ networkId: string | null;
62
+ username: string;
63
+ } | null;
64
+
65
+ export type ScheduledRequestContext = {
66
+ req: Request;
67
+ url: URL;
68
+ auth: RequestAuth;
69
+ isAdmin: boolean;
70
+ isNodeToken: boolean;
71
+ scope: RestNetworkScope;
72
+ };
73
+
74
+ const PRIORITIES = new Set(["high", "normal", "low"]);
75
+ const TIME_RE = /^(?:[01]\d|2[0-3]):[0-5]\d$/;
76
+ const OPEN_TASK_STATUSES = ["created", "delivered", "acked", "running"];
77
+ const MISFIRE_POLICIES = new Set<MisfirePolicy>(["catch_up_once", "skip"]);
78
+ export const MISFIRE_GRACE_MS = 60_000;
79
+
80
+ export function assertScheduledTaskBackendSupported(): void {
81
+ const refusal = db.transactionalFeaturesRefusal;
82
+ if (refusal !== null) {
83
+ throw new Error(`scheduled_tasks_require_transactional_sqlite_backend${refusal ? `: ${refusal}` : ""}`);
84
+ }
85
+ }
86
+
87
+ function jsonError(error: string, status: number, extra: Record<string, unknown> = {}): Response {
88
+ return Response.json({ ok: false, error, ...extra }, { status });
89
+ }
90
+
91
+ function iso(date: Date): string {
92
+ return date.toISOString();
93
+ }
94
+
95
+ function validTimezone(timezone: string): boolean {
96
+ try {
97
+ new Intl.DateTimeFormat("en-US", { timeZone: timezone }).format(new Date());
98
+ return true;
99
+ } catch {
100
+ return false;
101
+ }
102
+ }
103
+
104
+ function parseMisfirePolicy(raw: unknown, fallback: MisfirePolicy = "catch_up_once"): MisfirePolicy {
105
+ if (raw === undefined) return fallback;
106
+ if (typeof raw !== "string" || !MISFIRE_POLICIES.has(raw as MisfirePolicy)) throw new Error("invalid_misfire_policy");
107
+ return raw as MisfirePolicy;
108
+ }
109
+
110
+ function zonedParts(date: Date, timezone: string): { date: string; time: string; weekday: number } {
111
+ const parts = new Intl.DateTimeFormat("en-US", {
112
+ timeZone: timezone,
113
+ year: "numeric",
114
+ month: "2-digit",
115
+ day: "2-digit",
116
+ hourCycle: "h23",
117
+ hour: "2-digit",
118
+ minute: "2-digit",
119
+ weekday: "short",
120
+ }).formatToParts(date);
121
+ const value = (type: Intl.DateTimeFormatPartTypes) => parts.find((p) => p.type === type)?.value || "";
122
+ const weekday = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"].indexOf(value("weekday"));
123
+ return { date: `${value("year")}-${value("month")}-${value("day")}`, time: `${value("hour")}:${value("minute")}`, weekday };
124
+ }
125
+
126
+ /** Parse and validate the public schedule shape. No cron expressions: the
127
+ * supported forms are intentionally portable between Dashboard and mobile. */
128
+ export function parseScheduleSpec(raw: unknown, timezoneRaw: unknown): { spec: ScheduleSpec; timezone: string } {
129
+ if (!raw || typeof raw !== "object") throw new Error("schedule_required");
130
+ const obj = raw as Record<string, unknown>;
131
+ const type = obj.type;
132
+ const timezone = typeof timezoneRaw === "string" && timezoneRaw.trim() ? timezoneRaw.trim() : "UTC";
133
+ if (!validTimezone(timezone)) throw new Error("invalid_timezone");
134
+
135
+ if (type === "once") {
136
+ if (typeof obj.run_at !== "string") throw new Error("run_at_required");
137
+ // #3951 —— 用户给的 run_at 走同一个入口:无时区标记的 'YYYY-MM-DDTHH:MM:SS'
138
+ // 若直接 new Date() 会按**本机时区**解释(hub 的列是 UTC 无标记)⇒ 跨时区差 8 小时。
139
+ const whenMs = parseHubTimestamp(obj.run_at);
140
+ if (whenMs === null) throw new Error("invalid_run_at");
141
+ return { spec: { type, run_at: iso(new Date(whenMs)) }, timezone };
142
+ }
143
+ if (type === "interval") {
144
+ const every = Number(obj.every_seconds);
145
+ if (!Number.isSafeInteger(every) || every < 60 || every > 365 * 86400) throw new Error("invalid_interval");
146
+ return { spec: { type, every_seconds: every }, timezone };
147
+ }
148
+ if (type === "daily") {
149
+ if (typeof obj.time !== "string" || !TIME_RE.test(obj.time)) throw new Error("invalid_time");
150
+ return { spec: { type, time: obj.time }, timezone };
151
+ }
152
+ if (type === "weekly") {
153
+ if (typeof obj.time !== "string" || !TIME_RE.test(obj.time)) throw new Error("invalid_time");
154
+ if (!Array.isArray(obj.weekdays) || obj.weekdays.length === 0) throw new Error("weekdays_required");
155
+ const weekdays = [...new Set(obj.weekdays.map(Number))].sort();
156
+ if (weekdays.some((d) => !Number.isSafeInteger(d) || d < 0 || d > 6)) throw new Error("invalid_weekdays");
157
+ return { spec: { type, time: obj.time, weekdays }, timezone };
158
+ }
159
+ throw new Error("invalid_schedule_type");
160
+ }
161
+
162
+ /** Return the first occurrence strictly after `after`. Daily/weekly scans UTC
163
+ * minutes and compares IANA-zone wall time, so DST gaps/folds follow the
164
+ * platform timezone database instead of a hand-written offset guess. */
165
+ export function nextOccurrence(spec: ScheduleSpec, timezone: string, after: Date): Date | null {
166
+ if (spec.type === "once") {
167
+ // spec.run_at 由上面的 normalize 写出(总是带 Z),但仍然走同一个解析入口:
168
+ // 「这一处安全」是上一行写出来的事实,不是这一行能自己保证的。
169
+ const ms = parseHubTimestamp(spec.run_at);
170
+ if (ms === null) return null;
171
+ return ms > after.getTime() ? new Date(ms) : null;
172
+ }
173
+ if (spec.type === "interval") return new Date(after.getTime() + spec.every_seconds * 1000);
174
+
175
+ // A wall-clock time can occur twice when DST falls back. If today's local
176
+ // occurrence already happened at or before `after`, skip every duplicate
177
+ // carrying that same local date. Otherwise a "daily 01:30" schedule runs
178
+ // twice on the fall-back day.
179
+ const afterLocal = zonedParts(after, timezone);
180
+ let alreadyOccurredDate: string | null = null;
181
+ const backward = new Date(Math.floor(after.getTime() / 60000) * 60000);
182
+ for (let i = 0; i <= 26 * 60; i++, backward.setTime(backward.getTime() - 60000)) {
183
+ const local = zonedParts(backward, timezone);
184
+ if (local.date === afterLocal.date && local.time === spec.time && (spec.type === "daily" || spec.weekdays.includes(local.weekday))) {
185
+ alreadyOccurredDate = local.date;
186
+ break;
187
+ }
188
+ }
189
+
190
+ const cursor = new Date(Math.floor(after.getTime() / 60000) * 60000 + 60000);
191
+ const max = 8 * 24 * 60;
192
+ for (let i = 0; i < max; i++, cursor.setTime(cursor.getTime() + 60000)) {
193
+ const local = zonedParts(cursor, timezone);
194
+ if (local.time !== spec.time) continue;
195
+ if (local.date === alreadyOccurredDate) continue;
196
+ if (spec.type === "daily" || spec.weekdays.includes(local.weekday)) return new Date(cursor);
197
+ }
198
+ throw new Error("next_occurrence_unresolvable");
199
+ }
200
+
201
+ /** REST shape (pinned by rest-explicit-columns-http.test.ts): creator ids stay server-side. */
202
+ function decodeRow(row: ScheduledRow): Record<string, unknown> {
203
+ let schedule: unknown = null;
204
+ try { schedule = JSON.parse(row.schedule_json); } catch {}
205
+ const { created_by: _createdBy, created_by_node_id: _createdByNode, schedule_json: _scheduleJson,
206
+ no_change_backoff, backoff_streak, backoff_effective_seconds, backoff_last_run_id,
207
+ requirement_wake_json, requirement_wake_cursor, requirement_wake_last_event_id, ...publicRow } = row;
208
+ if (requirement_wake_json && schedule && typeof schedule === "object")
209
+ schedule = { ...schedule, requirement_wake: publicRequirementWake(requirement_wake_json) };
210
+ return { ...publicRow, schedule, ...(requirement_wake_json ? { requirement_wake_state:
211
+ { cursor: requirement_wake_cursor, last_event_id: requirement_wake_last_event_id } } : {}), ...(no_change_backoff === 1 ? { no_change_backoff: true,
212
+ backoff: { streak: backoff_streak, effective_seconds: backoff_effective_seconds
213
+ ?? (schedule as { every_seconds?: number } | null)?.every_seconds, last_run_id: backoff_last_run_id } } : {}) };
214
+ }
215
+
216
+ /** MCP (agent) shape: REST shape + which node created it (null = a person / old row). Never the user id. */
217
+ export function decodeRowForAgent(row: ScheduledRow): Record<string, unknown> {
218
+ return { ...decodeRow(row), created_by_node_id: row.created_by_node_id ?? null };
219
+ }
220
+
221
+ export function getScheduleRow(scheduleId: string): ScheduledRow | null {
222
+ return db.get<ScheduledRow>(`SELECT ${SCHEDULED_TASK_STORAGE_SELECT} FROM scheduled_tasks WHERE schedule_id = ?1`, scheduleId);
223
+ }
224
+
225
+ export class ScheduleInputError extends Error {
226
+ constructor(public code: string, public status: number, public extra: Record<string, unknown> = {}) { super(code); }
227
+ }
228
+
229
+ export type ValidatedScheduleCreate = {
230
+ name: string; content: string; priority: string; misfirePolicy: MisfirePolicy; backoff: boolean; wake: RequirementWake | null;
231
+ target: { node_id: string; alias: string }; spec: ScheduleSpec; timezone: string; next: Date;
232
+ };
233
+
234
+ /** Shared by REST POST and MCP schedule_create. Throws Error(code) like before. */
235
+ export function validateScheduleCreate(networkId: string, body: Record<string, unknown>): ValidatedScheduleCreate {
236
+ const name = typeof body.name === "string" ? body.name.trim() : "";
237
+ const content = typeof body.task === "string" ? body.task.trim() : "";
238
+ if (!name || name.length > 120) throw new Error("invalid_name");
239
+ if (!content || content.length > TASK_CONTENT_MAX) throw new Error("invalid_task");
240
+ const priority = typeof body.priority === "string" ? body.priority : "normal";
241
+ if (!PRIORITIES.has(priority)) throw new Error("invalid_priority");
242
+ const misfirePolicy = parseMisfirePolicy(body.misfire_policy);
243
+ const target = validateTarget(networkId, body.target_node_id);
244
+ const { spec, timezone } = parseScheduleSpec(body.schedule, body.timezone);
245
+ const next = nextOccurrence(spec, timezone, new Date());
246
+ if (!next) throw new Error("schedule_has_no_future_occurrence");
247
+ return { name, content, priority, misfirePolicy, target, spec, timezone, next,
248
+ backoff: parseBackoffPolicy(body.no_change_backoff, spec.type),
249
+ wake: parseRequirementWake((body.schedule as Record<string, unknown>).requirement_wake) };
250
+ }
251
+
252
+ export function insertSchedule(networkId: string, createdBy: string | null, createdByNodeId: string | null, v: ValidatedScheduleCreate): ScheduledRow {
253
+ const scheduleId = `sched_${crypto.randomUUID()}`;
254
+ const wakeJson = bindRequirementWake({ network_id: networkId, created_by: createdBy,
255
+ created_by_node_id: createdByNodeId, target_node_id: v.target.node_id }, v.wake, v.backoff, v.spec.type, createdBy);
256
+ db.run(
257
+ `INSERT INTO scheduled_tasks
258
+ (schedule_id, network_id, created_by, created_by_node_id, name, target_node_id, target_alias, task_content, priority, schedule_type, schedule_json, timezone, overlap_policy, misfire_policy, next_run_at, no_change_backoff, requirement_wake_json, requirement_wake_cursor)
259
+ VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, 'skip', ?13, ?14, ?15, ?16, ?17)`,
260
+ [scheduleId, networkId, createdBy, createdByNodeId, v.name, v.target.node_id, v.target.alias, v.content, v.priority, v.spec.type, JSON.stringify(v.spec), v.timezone, v.misfirePolicy, iso(v.next), v.backoff ? 1 : 0, wakeJson, wakeJson ? requirementWakeHead(networkId) : 0],
261
+ );
262
+ return getScheduleRow(scheduleId)!;
263
+ }
264
+
265
+ /** Idempotent cancel (shared by REST DELETE / POST cancel and MCP schedule_cancel). */
266
+ export function cancelSchedule(row: ScheduledRow): void {
267
+ if (row.status === "cancelled") return;
268
+ db.run("UPDATE scheduled_tasks SET status = 'cancelled', next_run_at = NULL, backoff_streak = 0, backoff_effective_seconds = NULL, backoff_last_run_id = NULL, revision = revision + 1, updated_at = datetime('now') WHERE schedule_id = ?1", [row.schedule_id]);
269
+ }
270
+
271
+ export function scheduleRuns(row: ScheduledRow, limit: number): Record<string, unknown> {
272
+ const runs = db.all(
273
+ // #523 — a task that ended `failed` leaves run.error_message empty; its reason is the task's reply text.
274
+ // #2884 — 共享会话忙的失败额外带上实测拆解(排队 / 会话等待两个量分开报)。
275
+ `SELECT r.run_id, r.schedule_id, r.scheduled_for, r.task_id, r.status, r.error_code,
276
+ CASE WHEN r.error_message IS NULL AND r.status = 'failed' THEN SUBSTR(t.result, 1, 500) ELSE r.error_message END AS error_message,
277
+ r.blocked_by_task_id, r.blocked_by_state, r.created_at, r.completed_at, r.work_outcome, r.schedule_revision, r.work_outcome_node_id,
278
+ t.created_at AS task_created_at, t.started_at AS task_started_at, t.completed_at AS task_completed_at
279
+ FROM scheduled_task_runs r
280
+ LEFT JOIN tasks t ON t.task_id = r.task_id AND t.network_id = r.network_id
281
+ WHERE r.schedule_id = ?1 AND r.network_id = ?2 ORDER BY r.created_at DESC, r.scheduled_for DESC LIMIT ?3`,
282
+ row.schedule_id, row.network_id, limit,
283
+ ).map((r: any) => {
284
+ const note = r.error_code === "task_failed_busy"
285
+ ? busyElapsedNote(r.task_created_at, r.task_started_at, r.task_completed_at)
286
+ : null;
287
+ const { task_created_at, task_started_at, task_completed_at, ...rest } = r;
288
+ return note ? { ...rest, error_message: [r.error_message, note].filter(Boolean).join(" ") } : rest;
289
+ });
290
+ return { runs, ...failureSummary(row.schedule_id) };
291
+ }
292
+
293
+ /** Shared PATCH semantics (REST PATCH and MCP schedule_update). Throws ScheduleInputError. */
294
+ export function patchSchedule(row: ScheduledRow, body: Record<string, unknown>, actorUserId: string | null = null): ScheduledRow {
295
+ // Cancellation/completion are terminal states. Editing must never become
296
+ // an implicit resurrection path (including by supplying status=active).
297
+ if (row.status === "cancelled") throw new ScheduleInputError("schedule_cancelled", 409);
298
+ if (row.status === "completed") throw new ScheduleInputError("schedule_completed", 409);
299
+ if (!Number.isSafeInteger(body.revision) || Number(body.revision) !== row.revision) throw new ScheduleInputError("revision_conflict", 409, { current_revision: row.revision });
300
+ try {
301
+ const name = body.name === undefined ? row.name : String(body.name).trim();
302
+ const content = body.task === undefined ? row.task_content : String(body.task).trim();
303
+ const priority = body.priority === undefined ? row.priority : String(body.priority);
304
+ if (!name || name.length > 120) throw new Error("invalid_name");
305
+ if (!content || content.length > TASK_CONTENT_MAX) throw new Error("invalid_task");
306
+ if (!PRIORITIES.has(priority)) throw new Error("invalid_priority");
307
+ const target = body.target_node_id === undefined ? { node_id: row.target_node_id, alias: row.target_alias } : validateTarget(row.network_id, body.target_node_id);
308
+ const parsed = body.schedule === undefined
309
+ ? { spec: JSON.parse(row.schedule_json) as ScheduleSpec, timezone: body.timezone === undefined ? row.timezone : String(body.timezone) }
310
+ : parseScheduleSpec(body.schedule, body.timezone === undefined ? row.timezone : body.timezone);
311
+ if (!validTimezone(parsed.timezone)) throw new Error("invalid_timezone");
312
+ const requestedStatus = body.status === undefined ? row.status : String(body.status);
313
+ if (!new Set(["active", "paused"]).has(requestedStatus)) throw new Error("invalid_status");
314
+ const misfirePolicy = parseMisfirePolicy(body.misfire_policy, row.misfire_policy);
315
+ // Editing descriptive fields must not silently reset the schedule's
316
+ // cadence. Recompute only when the scheduling inputs change, when a
317
+ // paused schedule resumes, or when repairing an impossible active row
318
+ // with no next occurrence. The recompute uses the same DST-safe helper
319
+ // as creation and dispatch advancement.
320
+ const scheduleJson = JSON.stringify(parsed.spec);
321
+ const backoff = parseBackoffPolicy(body.no_change_backoff, parsed.spec.type, row.no_change_backoff === 1);
322
+ if (backoff && row.overlap_policy !== "skip") throw new Error("no_change_backoff_requires_skip_overlap");
323
+ const rawWake = (body.schedule as Record<string, unknown> | undefined)?.requirement_wake;
324
+ const wake = parseRequirementWake(rawWake, backoff ? row.requirement_wake_json : null);
325
+ const wakeJson = bindRequirementWake({ ...row, target_node_id: target.node_id }, wake, backoff, parsed.spec.type, actorUserId);
326
+ // Every edit creates a new reporting generation. Reset opted-in cadence
327
+ // too, so the visible state never describes an older target/revision.
328
+ const schedulingChanged = scheduleJson !== row.schedule_json || parsed.timezone !== row.timezone;
329
+ const backoffGenerationReset = row.no_change_backoff === 1 || backoff;
330
+ const resumed = row.status !== "active" && requestedStatus === "active";
331
+ const next = requestedStatus !== "active"
332
+ ? null
333
+ : schedulingChanged || backoffGenerationReset || resumed || !row.next_run_at
334
+ ? nextOccurrence(parsed.spec, parsed.timezone, new Date())
335
+ : new Date(parseHubTimestamp(row.next_run_at) ?? Date.now());
336
+ if (requestedStatus === "active" && !next) throw new Error("schedule_has_no_future_occurrence");
337
+ // Runs no longer bump revision, so the scheduler may have advanced
338
+ // next_run_at after `row` was read. When cadence is preserved, keep the
339
+ // stored value instead of writing back the already-fired occurrence.
340
+ const keepStoredNext = requestedStatus === "active" && !schedulingChanged && !backoffGenerationReset && !resumed && !!row.next_run_at ? 1 : 0;
341
+ const updated = db.run(
342
+ `UPDATE scheduled_tasks SET name = ?1, target_node_id = ?2, target_alias = ?3, task_content = ?4,
343
+ priority = ?5, schedule_type = ?6, schedule_json = ?7, timezone = ?8, status = ?9,
344
+ next_run_at = CASE WHEN ?14 = 1 AND next_run_at IS NOT NULL THEN next_run_at ELSE ?10 END,
345
+ misfire_policy = ?11, no_change_backoff = ?15, requirement_wake_json = ?16, requirement_wake_cursor = ?17, requirement_wake_last_event_id = NULL, backoff_streak = 0, backoff_effective_seconds = NULL, backoff_last_run_id = NULL, revision = revision + 1, updated_at = datetime('now') WHERE schedule_id = ?12 AND revision = ?13`,
346
+ [name, target.node_id, target.alias, content, priority, parsed.spec.type, scheduleJson, parsed.timezone, requestedStatus, next ? iso(next) : null, misfirePolicy, row.schedule_id, row.revision, keepStoredNext, backoff ? 1 : 0, wakeJson, wakeJson ? requirementWakeHead(row.network_id) : 0],
347
+ );
348
+ if (updated.changes !== 1) throw new ScheduleInputError("revision_conflict", 409);
349
+ return getScheduleRow(row.schedule_id)!;
350
+ } catch (e: any) {
351
+ if (e instanceof ScheduleInputError) throw e;
352
+ const code = String(e?.message || "invalid_schedule");
353
+ throw new ScheduleInputError(code, code === "target_node_not_found" ? 404 : 400);
354
+ }
355
+ }
356
+
357
+ function scopedSchedule(scheduleId: string, scope: RestNetworkScope): ScheduledRow | null {
358
+ const params: unknown[] = [scheduleId];
359
+ let sql = `SELECT ${SCHEDULED_TASK_STORAGE_SELECT} FROM scheduled_tasks WHERE schedule_id = ?1`;
360
+ sql = addNetworkScope(sql, params, scope);
361
+ return db.get<ScheduledRow>(sql, ...params);
362
+ }
363
+
364
+ function resolveWriteNetwork(body: Record<string, unknown>, ctx: ScheduledRequestContext): string | null {
365
+ if (ctx.auth?.networkId) return ctx.auth.networkId;
366
+ if (typeof body.network_id === "string" && body.network_id) return body.network_id;
367
+ return singleNetworkId(ctx.scope);
368
+ }
369
+
370
+ function validateTarget(networkId: string, nodeId: unknown): { node_id: string; alias: string } {
371
+ if (typeof nodeId !== "string" || nodeId.length < 2 || nodeId.length > 200) throw new Error("invalid_target_node_id");
372
+ const node = db.get<{ node_id: string; alias: string | null }>(
373
+ "SELECT node_id, alias FROM nodes WHERE node_id = ?1 AND network_id = ?2",
374
+ nodeId, networkId,
375
+ );
376
+ if (!node?.alias) throw new Error("target_node_not_found");
377
+ return { node_id: node.node_id, alias: node.alias };
378
+ }
379
+
380
+ function stuckNoticeFor(row: ScheduledRow, reason: StuckNotice["reason"], blocker: { task_id: string; created_at: string }, skips: number, timeoutMs: number): StuckNotice {
381
+ return {
382
+ reason, scheduleId: row.schedule_id, scheduleName: row.name, networkId: row.network_id, createdBy: row.created_by,
383
+ alias: row.target_alias, timezone: row.timezone, taskId: blocker.task_id, taskCreatedAt: blocker.created_at, skips, timeoutMs,
384
+ };
385
+ }
386
+
387
+ /** The line an Agent-created schedule's task always starts with (board #733). */
388
+ export function agentSchedulePrefix(alias: string | null, nodeId: string): string {
389
+ return `[scheduled by agent ${alias ?? "?"} (${nodeId})]\n`;
390
+ }
391
+
392
+ function agentCreatorDenial(row: ScheduledRow, target: AgentRef): { code: string; message: string } | null {
393
+ const creator = db.get<{ alias: string | null; owner_user_id: string | null; permission_mode: string | null }>(
394
+ "SELECT alias, owner_user_id, permission_mode FROM nodes WHERE node_id = ?1 AND network_id = ?2", row.created_by_node_id, row.network_id);
395
+ if (!creator) return { code: "creator_node_gone", message: "The node that created this schedule no longer exists in this network" };
396
+ const mode = creator.permission_mode === "readonly" || creator.permission_mode === "restricted" ? creator.permission_mode : "normal";
397
+ if (mode === "readonly") return { code: "creator_node_readonly", message: "The node that created this schedule is read-only" };
398
+ const verdict = dispatchVerdict({
399
+ networkId: row.network_id, nodeId: row.created_by_node_id, logKey: row.created_by_node_id!,
400
+ aliases: creator.alias ? [creator.alias] : [], ownerUserId: creator.owner_user_id, mode,
401
+ }, target);
402
+ if (verdict) return { code: "creator_access_revoked", message: `The node that created this schedule may not send_task to this agent now (${verdict.reason})` };
403
+ return null;
404
+ }
405
+
406
+ type DispatchEvent = { alias: string; networkId: string; taskId: string; priority: string; state: "delivered" | "queued" };
407
+
408
+ export function dispatchScheduledOccurrence(row: ScheduledRow, scheduledFor: string, advanceSchedule: boolean, advanceAfter = new Date()): { runId: string; taskId?: string; status: string; event?: DispatchEvent } {
409
+ const runId = `srun_${crypto.randomUUID()}`;
410
+ let event: DispatchEvent | undefined;
411
+ let finalStatus = "failed";
412
+ let createdTaskId: string | undefined;
413
+ const notices: StuckNotice[] = [];
414
+ let offlineNotice: OfflineNotice | null = null;
415
+ const timedOut: Array<{ taskId: string; fromStatus: string }> = [];
416
+
417
+ db.transaction(() => {
418
+ // UNIQUE(schedule_id, scheduled_for) is the cross-process claim. If two
419
+ // Hub processes race, one INSERT wins and the other transaction aborts.
420
+ db.run(
421
+ `INSERT INTO scheduled_task_runs (run_id, schedule_id, network_id, scheduled_for, status, schedule_revision)
422
+ VALUES (?1, ?2, ?3, ?4, 'claiming', ?5)`,
423
+ [runId, row.schedule_id, row.network_id, scheduledFor, row.revision],
424
+ );
425
+
426
+ const current = getScheduleRow(row.schedule_id);
427
+ if (!current || current.revision !== row.revision || current.status === "cancelled"
428
+ || (advanceSchedule && current.status !== "active")) throw new Error("schedule_changed");
429
+ row = current;
430
+ // Manual dispatch is an explicit wake. Its new generation fences all
431
+ // already-running reports, while a paused schedule remains paused.
432
+ if (!advanceSchedule && row.no_change_backoff === 1) {
433
+ const base = JSON.parse(row.schedule_json).every_seconds;
434
+ db.run(`UPDATE scheduled_tasks SET revision = revision + 1, backoff_streak = 0,
435
+ backoff_effective_seconds = NULL, backoff_last_run_id = NULL,
436
+ requirement_wake_cursor = ?4, requirement_wake_last_event_id = NULL,
437
+ next_run_at = CASE WHEN status = 'active' THEN ?1 ELSE next_run_at END,
438
+ updated_at = datetime('now') WHERE schedule_id = ?2 AND revision = ?3`,
439
+ [new Date(advanceAfter.getTime() + base * 1000).toISOString(), row.schedule_id, row.revision, requirementWakeHead(row.network_id)]);
440
+ row = getScheduleRow(row.schedule_id)!;
441
+ }
442
+ db.run("UPDATE scheduled_task_runs SET schedule_revision = ?1 WHERE run_id = ?2", [row.revision, runId]);
443
+
444
+ // Board #757 — do not create a task that will merely expire while its
445
+ // exact target session is offline. This branch deliberately precedes the
446
+ // overlap check: target_offline is the actionable reason for this round.
447
+ const targetNode = db.get<{ node_id: string; alias: string | null }>(
448
+ "SELECT node_id, alias FROM nodes WHERE node_id = ?1 AND network_id = ?2",
449
+ row.target_node_id, row.network_id,
450
+ );
451
+ if (targetNode?.alias && scheduledTargetOffline(targetNode.node_id, targetNode.alias, row.network_id, advanceAfter.getTime())) {
452
+ finalStatus = "skipped";
453
+ db.run(
454
+ `UPDATE scheduled_task_runs SET status = 'skipped', error_code = 'target_offline',
455
+ error_message = 'The target node is offline; no task was created', completed_at = datetime('now') WHERE run_id = ?1`,
456
+ [runId],
457
+ );
458
+ const claimed = db.run(
459
+ "UPDATE scheduled_tasks SET offline_alert_at = ?1 WHERE schedule_id = ?2 AND offline_alert_at IS NULL",
460
+ [advanceAfter.toISOString(), row.schedule_id],
461
+ ).changes === 1;
462
+ if (claimed) offlineNotice = {
463
+ scheduleId: row.schedule_id, scheduleName: row.name, networkId: row.network_id,
464
+ createdBy: row.created_by, createdByNodeId: row.created_by_node_id,
465
+ targetAlias: targetNode.alias,
466
+ };
467
+ if (advanceSchedule) advance({ ...row, target_alias: targetNode.alias }, scheduledFor, advanceAfter);
468
+ // #2622 —— 目标长期离线时按同因链放慢(默认 3 次后 2 倍递增,上限 1 小时)。
469
+ try {
470
+ const bo = applyNoProgressBackoff(row.schedule_id);
471
+ if (bo && bo.effective_seconds) {
472
+ const sched = db.get<{ next_run_at: string | null }>("SELECT next_run_at FROM scheduled_tasks WHERE schedule_id = ?1", row.schedule_id);
473
+ const at = parseHubTimestamp(sched?.next_run_at);
474
+ // 可观测性(不是装饰):生产出现「backoff_effective_seconds=5760 但实际每 6 分钟跳一次」,
475
+ // 只看列分不清是哪一步把 next_run_at 写回去了。这行把「算出的 eff」与「此刻落库的 next」
476
+ // 打在一起;再配上本函数返回前的最终值,就能对上账。
477
+ console.error(`[commhub scheduled-tasks] offline-backoff schedule=${row.schedule_id} cause=${bo.cause} streak=${bo.streak} effective=${bo.effective_seconds}s next_before=${sched?.next_run_at ?? 'null'}`);
478
+ if (at !== null) advance({ ...row, target_alias: targetNode.alias }, scheduledFor, new Date(at));
479
+ const after = db.get<{ next_run_at: string | null }>("SELECT next_run_at FROM scheduled_tasks WHERE schedule_id = ?1", row.schedule_id);
480
+ console.error(`[commhub scheduled-tasks] offline-backoff-final schedule=${row.schedule_id} next_after=${after?.next_run_at ?? 'null'}`);
481
+ }
482
+ } catch (e: any) {
483
+ console.error(`[scheduled-tasks] no-progress backoff failed schedule=${row.schedule_id}: ${e?.message || e}`);
484
+ }
485
+ return;
486
+ }
487
+ // Seeing the target online rearms exactly one warning for a future offline episode.
488
+ if (targetNode?.alias) db.run("UPDATE scheduled_tasks SET offline_alert_at = NULL WHERE schedule_id = ?1 AND offline_alert_at IS NOT NULL", [row.schedule_id]);
489
+
490
+ if (row.overlap_policy === "skip") {
491
+ const placeholders = OPEN_TASK_STATUSES.map((_, i) => `?${i + 2}`).join(", ");
492
+ // received:节点那边有没有把这条任务取走过(inbox 行被 ack,或任务已进 acked/running)。
493
+ // 跳过记录带上它,客户端才分得清「节点还没收到」和「节点在处理」—— 前者是投递故障,
494
+ // 后者才是真的上一次还没做完。
495
+ const findOpen = () => db.get<{ task_id: string; received: number; created_at: string; consumed_at: string | null; started_at: string | null; last_activity_at: string | null; status: string }>(
496
+ `SELECT r.task_id, t.created_at, t.consumed_at, t.started_at, t.last_activity_at, t.status,
497
+ CASE WHEN t.status IN ('acked', 'running')
498
+ OR EXISTS (SELECT 1 FROM inbox i WHERE (i.task_id = r.task_id OR i.id = r.task_id) AND i.acked = 1)
499
+ THEN 1 ELSE 0 END AS received
500
+ FROM scheduled_task_runs r
501
+ JOIN tasks t ON t.task_id = r.task_id
502
+ WHERE r.schedule_id = ?1 AND t.status IN (${placeholders})
503
+ ORDER BY r.created_at DESC LIMIT 1`,
504
+ row.schedule_id, ...OPEN_TASK_STATUSES,
505
+ );
506
+ let open = findOpen();
507
+ // #464 — a blocker open past the stuck timeout is ended as `expired` (the existing terminal state) and
508
+ // this occurrence dispatches normally. Bounded: each pass ends one exact open task or stops.
509
+ for (let guard = 0; open && guard < 20 && blockerTimedOut(open, row.schedule_json, advanceAfter); guard++) {
510
+ const { ageMs, limitMs, source } = blockerTimeoutClock(open, row.schedule_json, advanceAfter);
511
+ if (!expireStuckBlocker(open.task_id, row.network_id, ageMs, limitMs, source)) break;
512
+ timedOut.push({ taskId: open.task_id, fromStatus: open.status });
513
+ // Each stuck episode gets exactly one notice: if the stuck notice already went out (N skips), stay quiet.
514
+ if (skipsBlockedBy(row.schedule_id, open.task_id) < stuckNoticeSkips()) {
515
+ notices.push(stuckNoticeFor(row, "timed_out", open, skipsBlockedBy(row.schedule_id, open.task_id), limitMs));
516
+ }
517
+ open = findOpen();
518
+ }
519
+ if (open) {
520
+ finalStatus = "skipped";
521
+ const blockedState = open.received ? "in_progress" : "not_received";
522
+ db.run(
523
+ `UPDATE scheduled_task_runs SET status = 'skipped', error_code = 'previous_run_active', error_message = ?2,
524
+ blocked_by_task_id = ?3, blocked_by_state = ?4, completed_at = datetime('now') WHERE run_id = ?1`,
525
+ [
526
+ runId,
527
+ blockedState === "not_received"
528
+ ? `previous run task ${open.task_id} has not been received by the node yet`
529
+ : `previous run task ${open.task_id} is still in progress on the node`,
530
+ open.task_id,
531
+ blockedState,
532
+ ],
533
+ );
534
+ // #464 — the Nth consecutive skip behind the same task tells the schedule's creator, once.
535
+ const skips = skipsBlockedBy(row.schedule_id, open.task_id);
536
+ if (skips === stuckNoticeSkips()) notices.push(stuckNoticeFor(row, "stuck", open, skips, stuckTimeoutMs(row.schedule_json)));
537
+ if (advanceSchedule) advance(row, scheduledFor, advanceAfter);
538
+ // #2622(第二轮漏接的就是这里)—— 「上一次还在跑」这条跳过路径**从来没有调用过**退避:
539
+ // 上面的 UPDATE 把 run 记成 `skipped/previous_run_active`,然后直接 `advance` + `return`,
540
+ // 于是 `every_seconds=180` 的排程在上游一直忙的时候每 3 分钟写一行 skip,永不减速。
541
+ // 生产实测(.178,2026-10-28 00:05 起 1.9 小时):10 个排程写了 175 行,
542
+ // 其中 4 个各 31–35 行、`backoff_effective_seconds` **全部为 NULL** ——
543
+ // 而同一次部署里走 target_offline 路径的排程退避是正常的(840 秒)。
544
+ // 顺序要紧:必须放在 UPDATE 之后(退避函数读「最近 12 次」,
545
+ // 最新那条 skip 必须已经入表),也要放在 advance 之后(它会把 next_run_at 再往后推)。
546
+ try {
547
+ const bo = applyNoProgressBackoff(row.schedule_id);
548
+ if (bo && bo.effective_seconds) {
549
+ // 这条日志是**可观测性**,不是装饰:上一轮「代码加了但没人调用」整轮排查,
550
+ // 就是因为生产日志里看不出退避到底有没有在这个分支生效。
551
+ console.error(`[commhub scheduled-tasks] skip-path backoff schedule=${row.schedule_id} cause=${bo.cause} streak=${bo.streak} effective=${bo.effective_seconds}s`);
552
+ const sched = db.get<{ next_run_at: string | null }>("SELECT next_run_at FROM scheduled_tasks WHERE schedule_id = ?1", row.schedule_id);
553
+ const at = parseHubTimestamp(sched?.next_run_at);
554
+ if (at !== null) advance(row, scheduledFor, new Date(at));
555
+ }
556
+ } catch (e: any) {
557
+ console.error(`[scheduled-tasks] no-progress backoff failed schedule=${row.schedule_id}: ${e?.message || e}`);
558
+ }
559
+ return;
560
+ }
561
+ }
562
+
563
+ const node = targetNode;
564
+ if (!node?.alias) {
565
+ finalStatus = "failed";
566
+ db.run(
567
+ "UPDATE scheduled_task_runs SET status = 'failed', error_code = 'target_node_not_found', error_message = 'The bound node no longer exists in this network', completed_at = datetime('now') WHERE run_id = ?1",
568
+ [runId],
569
+ );
570
+ if (advanceSchedule) advance(row, scheduledFor, advanceAfter);
571
+ return;
572
+ }
573
+ // 多用户 Agent 权限:排程是「建它的人」的委托。建的人后来被设成受限成员、而且没被授权
574
+ // 给目标 Agent 发任务时,不再代他派发(否则受限前建的排程会永远绕过授权)。
575
+ // Board #733 — an Agent-created schedule is that node's standing send_task: every run re-evaluates the
576
+ // creating node as it is NOW (exists, not read-only, current owner) with send_task's own dispatchVerdict.
577
+ const creatorDenial = row.created_by_node_id
578
+ ? agentCreatorDenial(row, { alias: node.alias, nodeId: node.node_id })
579
+ : row.created_by && !canMessageAgent(row.created_by, row.network_id, { alias: node.alias, nodeId: node.node_id })
580
+ ? { code: "creator_access_revoked", message: "The schedule creator is no longer allowed to message this agent" }
581
+ : null;
582
+ if (creatorDenial) {
583
+ finalStatus = "failed";
584
+ db.run(
585
+ "UPDATE scheduled_task_runs SET status = 'failed', error_code = ?1, error_message = ?2, completed_at = datetime('now') WHERE run_id = ?3",
586
+ [creatorDenial.code, creatorDenial.message, runId],
587
+ );
588
+ if (advanceSchedule) advance(row, scheduledFor, advanceAfter);
589
+ return;
590
+ }
591
+ const creatorNode = row.created_by_node_id
592
+ ? db.get<{ alias: string | null }>("SELECT alias FROM nodes WHERE node_id = ?1 AND network_id = ?2", row.created_by_node_id, row.network_id)
593
+ : null;
594
+ const lifecycle = assertNodeActive(node.alias, row.network_id);
595
+ if (!lifecycle.ok) {
596
+ finalStatus = "failed";
597
+ db.run(
598
+ "UPDATE scheduled_task_runs SET status = 'failed', error_code = 'target_not_active', error_message = ?1, completed_at = datetime('now') WHERE run_id = ?2",
599
+ [String((lifecycle as any).error || "target_not_active").slice(0, 500), runId],
600
+ );
601
+ if (advanceSchedule) advance(row, scheduledFor, advanceAfter);
602
+ return;
603
+ }
604
+
605
+ // #2880 —— 节点级忙熔断(默认关闭;COMMHUB_NODE_BUSY_CIRCUIT=1 才生效)。
606
+ // 与 lifecycle/health 的区别:节点没坏、也没停,只是最近一直没有产出(忙类失败多且 0 成功),
607
+ // 投进去必然白等 ~30 分钟。所以记 **skipped** 而不是 failed —— 这不是故障,是「现在别投」。
608
+ // 熔断期间每 COMMHUB_NODE_BUSY_CIRCUIT_PROBE_HOURS 小时会放一条探针(见 node-serviceability.ts),
609
+ // 避免「不投递 ⇒ 拿不到成功 ⇒ 永远解不开」的死锁。
610
+ {
611
+ const circuit = nodeCanAcceptDispatch(node.alias, row.network_id);
612
+ if (!circuit.ok) {
613
+ finalStatus = "skipped";
614
+ db.run(
615
+ "UPDATE scheduled_task_runs SET status = 'skipped', error_code = 'target_busy_no_output', error_message = ?1, completed_at = datetime('now') WHERE run_id = ?2",
616
+ [String(circuit.reason ?? "target_busy_no_output").slice(0, 500), runId],
617
+ );
618
+ if (advanceSchedule) advance(row, scheduledFor, advanceAfter);
619
+ return;
620
+ }
621
+ }
622
+
623
+ // #460 — the target reports a degraded layer (App Server down / TUI gone / login revoked): the run fails
624
+ // with that reason instead of queueing a task nobody will execute. Unknown / stale health runs as before.
625
+ const health = assertNodeHealthy(node.alias, row.network_id, { forceAllowed: false });
626
+ if (!health.ok) {
627
+ finalStatus = "failed";
628
+ db.run(
629
+ "UPDATE scheduled_task_runs SET status = 'failed', error_code = 'node_degraded', error_message = ?1, completed_at = datetime('now') WHERE run_id = ?2",
630
+ [health.message.slice(0, 500), runId],
631
+ );
632
+ if (advanceSchedule) advance(row, scheduledFor, advanceAfter);
633
+ return;
634
+ }
635
+
636
+ const taskId = crypto.randomUUID();
637
+ // claude-code 裸节点的 node-server 上报 report_status 不带 node_id,它的 sessions 行
638
+ // node_id 永远是 NULL —— 只按 node_id 找会话,这类节点每一次都判成 queued、门铃不响,
639
+ // 任务在 inbox 里等到别的消息顺带捎进去(生产上 20–36 分钟)。按 node_id 找不到时退回
640
+ // 同网同别名、node_id 为空的会话;SSE 订阅者注册表是「有没有人在听」的实测,也算数。
641
+ const session = db.get<{ status: string | null }>(
642
+ "SELECT status FROM sessions WHERE node_id = ?1 AND network_id = ?2 ORDER BY updated_at DESC LIMIT 1",
643
+ row.target_node_id, row.network_id,
644
+ ) ?? db.get<{ status: string | null }>(
645
+ "SELECT status FROM sessions WHERE alias = ?1 AND network_id = ?2 AND node_id IS NULL ORDER BY updated_at DESC LIMIT 1",
646
+ node.alias, row.network_id,
647
+ );
648
+ const deliveryState: "delivered" | "queued" =
649
+ (session && session.status !== "offline") || hasSubscribers(node.alias, row.network_id) ? "delivered" : "queued";
650
+ // Board #733 — provenance is server-set: an Agent-created schedule names its node in meta and in a
651
+ // prefix the receiving agent always sees first, so it can never pass as a person's schedule.
652
+ const metaJson = JSON.stringify({
653
+ scheduled_task_id: row.schedule_id,
654
+ scheduled_run_id: runId,
655
+ scheduled_for: scheduledFor,
656
+ auth_origin: "hub_scheduler",
657
+ ...(row.created_by_node_id ? { scheduled_by_node_id: row.created_by_node_id, scheduled_by_alias: creatorNode?.alias ?? null } : {}),
658
+ });
659
+ const originalContent = row.created_by_node_id
660
+ ? `${agentSchedulePrefix(creatorNode?.alias ?? null, row.created_by_node_id)}${row.task_content}`
661
+ : row.task_content;
662
+ const taskContent = originalContent + (row.no_change_backoff === 1
663
+ && row.schedule_type === "interval" && row.overlap_policy === "skip"
664
+ && db.transactionalFeaturesRefusal === null ? scheduledOutcomeGuidance(taskId) : "");
665
+ db.run(
666
+ `INSERT INTO inbox (id, task_id, session_name, node_id, type, priority, content, from_session, requires_response, network_id, meta_json)
667
+ VALUES (?1, ?1, ?2, ?3, 'task', ?4, ?5, 'scheduler', 'reply', ?6, ?7)`,
668
+ [taskId, node.alias, node.node_id, row.priority, taskContent, row.network_id, metaJson],
669
+ );
670
+ db.run(
671
+ `INSERT INTO tasks (task_id, from_name, to_node_id, to_name, priority, status, content, requires_response, created_at, delivered_at, expires_at, network_id, meta_json)
672
+ VALUES (?1, 'scheduler', ?2, ?3, ?4, 'delivered', ?5, 'reply', datetime('now'), datetime('now'), datetime('now', '+86400 seconds'), ?6, ?7)`,
673
+ [taskId, node.node_id, node.alias, row.priority, taskContent, row.network_id, metaJson],
674
+ );
675
+ // Dispatch is not completion. Keep the run open until the exact bound
676
+ // task reaches replied/failed/cancelled/expired; db.ts owns that mirror.
677
+ db.run("UPDATE scheduled_task_runs SET task_id = ?1, status = ?2, completed_at = NULL WHERE run_id = ?3", [taskId, deliveryState, runId]);
678
+
679
+ // #2622 —— 上一层任务还没跑完时按同因链放慢(间隔 180s 的排程曾一天写 343 行 skip)。
680
+ try {
681
+ applyNoProgressBackoff(row.schedule_id);
682
+ } catch (e: any) {
683
+ console.error(`[scheduled-tasks] no-progress backoff failed schedule=${row.schedule_id}: ${e?.message || e}`);
684
+ }
685
+ db.run(`UPDATE sessions SET task = ${sessionTaskOnDispatch("?1")}, updated_at = datetime('now') WHERE node_id = ?2 AND network_id = ?3`, [sessionTaskPreview(taskContent), node.node_id, row.network_id]);
686
+ db.run("UPDATE scheduled_tasks SET target_alias = ?1, last_run_at = ?2, updated_at = datetime('now') WHERE schedule_id = ?3", [node.alias, scheduledFor, row.schedule_id]);
687
+ if (advanceSchedule) advance({ ...row, target_alias: node.alias }, scheduledFor, advanceAfter);
688
+ createdTaskId = taskId;
689
+ finalStatus = deliveryState;
690
+ event = { alias: node.alias, networkId: row.network_id, taskId, priority: row.priority, state: deliveryState };
691
+ });
692
+
693
+ // Everything below is a post-commit doorbell. A logging/SSE failure must
694
+ // never turn a durably-created task into an API-level failure: callers may
695
+ // retry an apparent failure and create a second manual occurrence.
696
+ try {
697
+ for (const t of timedOut) logTaskEvent(t.taskId, t.fromStatus, "expired", "hub-scheduler", `stuck timeout; schedule=${row.schedule_id} run=${runId}`);
698
+ for (const n of notices) sendStuckNotice(n);
699
+ if (offlineNotice) sendOfflineNotice(offlineNotice);
700
+ // #523 — a run that failed at dispatch counts toward the consecutive-failure alert (task failures arrive via db.ts).
701
+ if (finalStatus === "failed") evaluateScheduleFailures(row.schedule_id, advanceAfter);
702
+ if (createdTaskId) logTaskEvent(createdTaskId, null, "delivered", "hub-scheduler", `schedule=${row.schedule_id} run=${runId}`);
703
+ if (event) {
704
+ const pending = db.get<{ cnt: number }>("SELECT COUNT(*) AS cnt FROM inbox WHERE session_name = ?1 AND network_id = ?2 AND acked = 0", event.alias, event.networkId);
705
+ if (event.state === "delivered") pushEvent(event.alias, { type: "new_task", inbox_count: pending?.cnt ?? 1, priority: event.priority, from: "scheduler" }, event.networkId);
706
+ pushNetworkObserverEvent(event.networkId, { type: "new_task", task_id: event.taskId, from: "scheduler", to: event.alias, status: event.state, priority: event.priority });
707
+ }
708
+ } catch (e: any) {
709
+ console.error(`[scheduled-tasks] post-commit notification failed schedule=${row.schedule_id} run=${runId}: ${e?.message || e}`);
710
+ }
711
+ return { runId, taskId: createdTaskId, status: finalStatus, event };
712
+ }
713
+
714
+ // `revision` is the optimistic-concurrency token for *user edits*. Run
715
+ // bookkeeping (next_run_at/last_run_at) must not bump it: a 2-minute schedule
716
+ // would otherwise turn every edit that spans a run into a 409 and the client
717
+ // would drop the user's draft. Terminal completion still bumps, because it is
718
+ // a status change the user sees and it is what stops an in-flight PATCH (whose
719
+ // UPDATE is guarded by `AND revision = ?`) from reviving a finished one-shot.
720
+ function advance(row: ScheduledRow, scheduledFor: string, after = new Date(), recordLastRun = true): void {
721
+ refreshScheduledBackoff(row.schedule_id, row.network_id, after);
722
+ const stored = getScheduleRow(row.schedule_id)!;
723
+ const spec = JSON.parse(row.schedule_json) as ScheduleSpec;
724
+ const effective = spec.type === "interval" && stored.no_change_backoff === 1
725
+ ? { ...spec, every_seconds: Math.max(spec.every_seconds, stored.backoff_effective_seconds ?? spec.every_seconds) } : spec;
726
+ const next = nextOccurrence(effective, row.timezone, after);
727
+ const lastRunAt = recordLastRun ? scheduledFor : row.last_run_at;
728
+ if (spec.type === "once" || !next) {
729
+ db.run(
730
+ "UPDATE scheduled_tasks SET status = 'completed', next_run_at = NULL, last_run_at = ?1, revision = revision + 1, updated_at = datetime('now') WHERE schedule_id = ?2",
731
+ [lastRunAt, row.schedule_id],
732
+ );
733
+ } else {
734
+ db.run(
735
+ "UPDATE scheduled_tasks SET next_run_at = ?1, last_run_at = ?2, updated_at = datetime('now') WHERE schedule_id = ?3",
736
+ [iso(next), lastRunAt, row.schedule_id],
737
+ );
738
+ }
739
+ }
740
+
741
+ function isMissedOccurrence(scheduledFor: string, now: Date): boolean {
742
+ const scheduledAt = new Date(scheduledFor).getTime();
743
+ return Number.isFinite(scheduledAt) && now.getTime() - scheduledAt > MISFIRE_GRACE_MS;
744
+ }
745
+
746
+ function skipScheduledMisfire(row: ScheduledRow, scheduledFor: string, now: Date): void {
747
+ const runId = `srun_${crypto.randomUUID()}`;
748
+ db.transaction(() => {
749
+ db.run(
750
+ `INSERT INTO scheduled_task_runs
751
+ (run_id, schedule_id, network_id, scheduled_for, status, error_code, error_message, completed_at, schedule_revision)
752
+ VALUES (?1, ?2, ?3, ?4, 'skipped', 'misfire_skipped', 'The occurrence was overdue and the schedule is configured to skip missed runs', datetime('now'), ?5)`,
753
+ [runId, row.schedule_id, row.network_id, scheduledFor, row.revision],
754
+ );
755
+ // A skipped occurrence is auditable but is not an execution: preserve
756
+ // last_run_at while moving the schedule directly to its next future slot.
757
+ advance(row, scheduledFor, now, false);
758
+ });
759
+ }
760
+
761
+ export function runDueScheduledTasks(now = new Date(), limit = 100): { processed: number; failed: number } {
762
+ pollRequirementWakes(now);
763
+ const due = db.all<ScheduledRow>(
764
+ `SELECT ${SCHEDULED_TASK_STORAGE_SELECT} FROM scheduled_tasks WHERE status = 'active' AND next_run_at IS NOT NULL AND next_run_at <= ?1 ORDER BY next_run_at LIMIT ?2`,
765
+ iso(now), limit,
766
+ );
767
+ let processed = 0;
768
+ let failed = 0;
769
+ for (const row of due) {
770
+ try {
771
+ // Re-read inside the claim path so a pause/cancel immediately before the
772
+ // tick wins. The unique occurrence key handles a second scheduler.
773
+ const current = db.get<ScheduledRow>(`SELECT ${SCHEDULED_TASK_STORAGE_SELECT} FROM scheduled_tasks WHERE schedule_id = ?1 AND status = 'active' AND next_run_at = ?2`, row.schedule_id, row.next_run_at);
774
+ if (!current?.next_run_at) continue;
775
+ if (current.misfire_policy === "skip" && isMissedOccurrence(current.next_run_at, now)) {
776
+ skipScheduledMisfire(current, current.next_run_at, now);
777
+ } else {
778
+ // catch_up_once is also the compatibility default for pre-migration
779
+ // rows. Advancing from `now` collapses any number of missed slots into
780
+ // one task instead of replaying an outage as a dispatch storm.
781
+ dispatchScheduledOccurrence(current, current.next_run_at, true, now);
782
+ }
783
+ processed++;
784
+ } catch (e: any) {
785
+ if (/UNIQUE|duplicate key/i.test(String(e?.message))) continue;
786
+ failed++;
787
+ console.error(`[scheduled-tasks] occurrence failed schedule=${row.schedule_id}: ${e?.message || e}`);
788
+ }
789
+ }
790
+ return { processed, failed };
791
+ }
792
+
793
+ export function startScheduledTaskScheduler(intervalMs?: number): ReturnType<typeof setInterval> {
794
+ const configured = Number(process.env.COMMHUB_SCHEDULER_TICK_MS);
795
+ const ms = intervalMs && intervalMs > 0 ? intervalMs : configured > 0 ? configured : 10_000;
796
+ // First tick is immediate so a restarted Hub converges without waiting one
797
+ // full cadence. Recurrences advance from "now", intentionally collapsing a
798
+ // long outage into one catch-up occurrence instead of a dispatch storm.
799
+ runDueScheduledTasks();
800
+ return setInterval(() => runDueScheduledTasks(), ms);
801
+ }
802
+
803
+ async function bodyObject(req: Request): Promise<Record<string, unknown>> {
804
+ try {
805
+ const value = await req.json();
806
+ if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error();
807
+ return value as Record<string, unknown>;
808
+ } catch {
809
+ throw new Error("invalid_json");
810
+ }
811
+ }
812
+
813
+ function writeAllowed(ctx: ScheduledRequestContext, networkId: string | null): boolean {
814
+ return !ctx.isNodeToken && canRestWriteNetwork(ctx.auth, networkId, ctx.isAdmin);
815
+ }
816
+
817
+
818
+ type WatcherPublic = { user_id: string; username: string; display_name: string };
819
+
820
+ function scheduleWatchers(scheduleId: string): WatcherPublic[] {
821
+ return db.all<{ user_id: string; username: string; display_name: string | null }>(
822
+ `SELECT w.user_id, u.username, u.display_name
823
+ FROM scheduled_task_watchers w JOIN users u ON u.user_id = w.user_id
824
+ WHERE w.schedule_id = ?1 ORDER BY u.username`,
825
+ [scheduleId],
826
+ ).map(row => ({ user_id: row.user_id, username: row.username, display_name: row.display_name ?? "" }));
827
+ }
828
+
829
+ function userInNetwork(userId: string, networkId: string): boolean {
830
+ if (db.get("SELECT 1 AS ok FROM network_members WHERE network_id = ?1 AND user_id = ?2", [networkId, userId])) return true;
831
+ return !!db.get("SELECT 1 AS ok FROM networks WHERE network_id = ?1 AND owner_id = ?2", [networkId, userId]);
832
+ }
833
+
834
+ function replaceScheduleWatchers(scheduleId: string, networkId: string, raw: unknown): void {
835
+ if (!Array.isArray(raw) || raw.length > 20) throw new ScheduleInputError("invalid_watchers", 400);
836
+ const ids: string[] = [];
837
+ for (const item of raw) {
838
+ const id = typeof item === "string" ? item : item && typeof item === "object" && typeof (item as { user_id?: unknown }).user_id === "string" ? (item as { user_id: string }).user_id : "";
839
+ if (!id || id.length > 200) throw new ScheduleInputError("invalid_watchers", 400);
840
+ if (!ids.includes(id)) ids.push(id);
841
+ }
842
+ for (const id of ids) {
843
+ if (!userInNetwork(id, networkId)) throw new ScheduleInputError("watcher_not_in_network", 400, { user_id: id });
844
+ }
845
+ db.run("DELETE FROM scheduled_task_watchers WHERE schedule_id = ?1", [scheduleId]);
846
+ for (const id of ids) db.run("INSERT INTO scheduled_task_watchers (schedule_id, user_id) VALUES (?1, ?2)", [scheduleId, id]);
847
+ }
848
+
849
+ function watchingSchedule(scheduleId: string, userId: string | null): boolean {
850
+ if (!userId) return false;
851
+ return !!db.get("SELECT 1 AS ok FROM scheduled_task_watchers WHERE schedule_id = ?1 AND user_id = ?2", [scheduleId, userId]);
852
+ }
853
+
854
+ function canManageWatchers(row: ScheduledRow, ctx: ScheduledRequestContext): boolean {
855
+ const uid = ctx.auth?.userId ?? null;
856
+ if (ctx.isAdmin) return true;
857
+ if (uid && row.created_by === uid) return true;
858
+ return !row.created_by && writeAllowed(ctx, row.network_id);
859
+ }
860
+
861
+ function decorateSchedule(row: ScheduledRow, ctx: ScheduledRequestContext): Record<string, unknown> {
862
+ const watchers = scheduleWatchers(row.schedule_id);
863
+ const uid = ctx.auth?.userId ?? null;
864
+ const isWatcher = !!uid && watchers.some(w => w.user_id === uid);
865
+ return {
866
+ ...decodeRow(row),
867
+ watchers,
868
+ is_watcher: isWatcher,
869
+ can_edit: writeAllowed(ctx, row.network_id) || isWatcher,
870
+ can_manage_watchers: canManageWatchers(row, ctx),
871
+ };
872
+ }
873
+
874
+ export async function handleScheduledTaskRequest(ctx: ScheduledRequestContext): Promise<Response | null> {
875
+ const { req, url } = ctx;
876
+ if (url.pathname !== "/api/scheduled-tasks" && !url.pathname.startsWith("/api/scheduled-tasks/")) return null;
877
+ if (ctx.isNodeToken) return jsonError("user_token_required", 403);
878
+
879
+ if (url.pathname === "/api/scheduled-tasks" && req.method === "GET") {
880
+ const params: unknown[] = [];
881
+ let sql = `SELECT ${SCHEDULED_TASK_STORAGE_SELECT} FROM scheduled_tasks WHERE 1=1`;
882
+ sql = addNetworkScope(sql, params, ctx.scope);
883
+ const status = url.searchParams.get("status");
884
+ if (status) { sql += ` AND status = ?${params.length + 1}`; params.push(status); }
885
+ sql += " ORDER BY updated_at DESC LIMIT 500";
886
+ return Response.json({ ok: true, schedules: db.all<ScheduledRow>(sql, ...params).map(row => decorateSchedule(row, ctx)) });
887
+ }
888
+
889
+ if (url.pathname === "/api/scheduled-tasks" && req.method === "POST") {
890
+ let body: Record<string, unknown>;
891
+ try { body = await bodyObject(req); } catch { return jsonError("invalid_json", 400); }
892
+ const networkId = resolveWriteNetwork(body, ctx);
893
+ if (!networkId) return jsonError("network_id_required", 400);
894
+ if (!writeAllowed(ctx, networkId)) return jsonError("permission_denied", 403);
895
+ try {
896
+ const created = insertSchedule(networkId, ctx.auth?.userId ?? null, null, validateScheduleCreate(networkId, body));
897
+ if (body.watchers !== undefined) replaceScheduleWatchers(created.schedule_id, networkId, body.watchers);
898
+ return Response.json({ ok: true, schedule: decorateSchedule(getScheduleRow(created.schedule_id)!, ctx) }, { status: 201 });
899
+ } catch (e: any) {
900
+ if (e instanceof ScheduleInputError) return jsonError(e.code, e.status, e.extra);
901
+ const code = String(e?.message || "invalid_schedule");
902
+ const status = code === "target_node_not_found" ? 404 : 400;
903
+ return jsonError(code, status);
904
+ }
905
+ }
906
+
907
+ const match = url.pathname.match(/^\/api\/scheduled-tasks\/([^/]+)(?:\/(runs|run-now|cancel))?$/);
908
+ if (!match) return jsonError("not_found", 404);
909
+ const scheduleId = decodeURIComponent(match[1]);
910
+ const sub = match[2] || null;
911
+ const row = scopedSchedule(scheduleId, ctx.scope);
912
+ if (!row) return jsonError("schedule_not_found", 404);
913
+
914
+ if (sub === "runs" && req.method === "GET") {
915
+ const limit = Math.max(1, Math.min(200, Number(url.searchParams.get("limit")) || 50));
916
+ return Response.json({ ok: true, ...scheduleRuns(row, limit) });
917
+ }
918
+ if (!sub && req.method === "GET") return Response.json({ ok: true, schedule: decorateSchedule(row, ctx) });
919
+ const watcher = watchingSchedule(row.schedule_id, ctx.auth?.userId ?? null);
920
+ const writer = writeAllowed(ctx, row.network_id);
921
+ if (!writer && !watcher) return jsonError("permission_denied", 403);
922
+ if (!writer && (sub === "cancel" || (!sub && req.method === "DELETE"))) return jsonError("watcher_cannot_cancel", 403);
923
+ if (!writer && sub === "run-now") return jsonError("watcher_cannot_run", 403);
924
+
925
+ if (sub === "run-now" && req.method === "POST") {
926
+ if (row.status === "cancelled") return jsonError("schedule_cancelled", 409);
927
+ try {
928
+ const result = dispatchScheduledOccurrence(row, iso(new Date()), false);
929
+ return Response.json({ ok: true, ...result }, { status: result.status === "failed" ? 409 : 202 });
930
+ } catch (e: any) {
931
+ return jsonError("dispatch_failed", 409, { message: String(e?.message || e).slice(0, 500) });
932
+ }
933
+ }
934
+
935
+ // Cancel — accept two spellings for the same operation:
936
+ // DELETE /api/scheduled-tasks/:id (original)
937
+ // POST /api/scheduled-tasks/:id/cancel (added because some reverse
938
+ // proxies swallow DELETE and
939
+ // return 405 HTML; POST is
940
+ // universally allowed)
941
+ // Idempotent: re-cancelling an already-cancelled row is a 200 no-op, not a
942
+ // 409, because a client that just saw the row (still on screen as
943
+ // "cancelled" between polls) should get the same outcome whether it's the
944
+ // first click or a retry.
945
+ if ((!sub && req.method === "DELETE") || (sub === "cancel" && req.method === "POST")) {
946
+ cancelSchedule(row);
947
+ return Response.json({ ok: true, status: "cancelled" });
948
+ }
949
+
950
+ if (!sub && req.method === "PATCH") {
951
+ let body: Record<string, unknown>;
952
+ try { body = await bodyObject(req); } catch { return jsonError("invalid_json", 400); }
953
+ if (body.watchers !== undefined && !canManageWatchers(row, ctx)) return jsonError("watcher_cannot_manage_watchers", 403);
954
+ try {
955
+ const updated = patchSchedule(row, body, ctx.auth?.userId ?? null);
956
+ if (body.watchers !== undefined) replaceScheduleWatchers(updated.schedule_id, updated.network_id, body.watchers);
957
+ return Response.json({ ok: true, schedule: decorateSchedule(getScheduleRow(updated.schedule_id)!, ctx) });
958
+ } catch (e: any) {
959
+ if (e instanceof ScheduleInputError) return jsonError(e.code, e.status, e.extra);
960
+ throw e;
961
+ }
962
+ }
963
+ return jsonError("method_not_allowed", 405);
964
+ }