@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
@@ -5,6 +5,15 @@
5
5
  // tools.ts wrap these with auth checks (SEC-1 network scope, ntok_/utok_
6
6
  // caller identity, role gates).
7
7
 
8
+ import { isValidTimeoutMs, TIMEOUT_MS_REASON } from "./create-node-validate.js";
9
+ import {
10
+ REASONING_EFFORT_VALUES,
11
+ validateCodexConfigFlag,
12
+ type ModelReasoningEffort,
13
+ } from "./codex-config-flags.js";
14
+
15
+ export { REASONING_EFFORT_VALUES, type ModelReasoningEffort } from "./codex-config-flags.js";
16
+
8
17
  /**
9
18
  * Fields the dashboard may change. Anything not in this list is rejected
10
19
  * by hub-side validation regardless of role — the UI cannot smuggle
@@ -16,6 +25,12 @@ export const ALLOWED_FLAGS = new Set<string>([
16
25
  "maxTurns",
17
26
  "budget",
18
27
  "timeout",
28
+ // Codex (#815 / client): posture + co-presence grant + thinking level
29
+ "approvalPolicy",
30
+ "sandboxMode",
31
+ "skipGitRepoCheck",
32
+ "copresenceFullAccess",
33
+ "modelReasoningEffort",
19
34
  ]);
20
35
  // NB on teammateMode (dropped from P1 scope per #290 cross-agent
21
36
  // review): teammateMode is consumed ONLY by the claude-code-cli
@@ -43,6 +58,9 @@ export const ALLOWED_FLAGS = new Set<string>([
43
58
  export const SECURITY_SENSITIVE_FLAGS = new Set<string>([
44
59
  "permissionMode",
45
60
  "dangerouslySkipPermissions",
61
+ "approvalPolicy",
62
+ "sandboxMode",
63
+ "copresenceFullAccess",
46
64
  ]);
47
65
 
48
66
  /**
@@ -55,6 +73,11 @@ export const RESTART_REQUIRED_FLAGS = new Set<string>([
55
73
  "permissionMode",
56
74
  "dangerouslySkipPermissions",
57
75
  "timeout",
76
+ "approvalPolicy",
77
+ "sandboxMode",
78
+ "skipGitRepoCheck",
79
+ "copresenceFullAccess",
80
+ "modelReasoningEffort",
58
81
  ]);
59
82
 
60
83
  /**
@@ -126,6 +149,182 @@ export function isAllowedToChangeFlag(
126
149
  return null;
127
150
  }
128
151
 
152
+ /**
153
+ * Runtime families the hub treats as "OpenCode". `nodes.runtime` is
154
+ * written from report_status and carries the legacy `-sdk` bucket
155
+ * (`opencode-sdk`) for older CLI builds, while newer callers pass the
156
+ * config-level spelling (`opencode-cli` / `opencode`). All three mean
157
+ * the same agent-node runtime, so the hot-model-switch gate below must
158
+ * accept every spelling — otherwise the same node flips between hot
159
+ * and restart depending on which build reported last.
160
+ */
161
+ const OPENCODE_RUNTIME_LABELS = new Set<string>(["opencode-sdk", "opencode-cli", "opencode"]);
162
+
163
+ /** Reported runtime family + explicit mode only; never local ownership proof. */
164
+ export function isOpenCodeV2CopresenceRuntime(runtime: string | null | undefined,
165
+ generation: "v1" | "v2" | null, mode: string | null): boolean {
166
+ return !!runtime && OPENCODE_RUNTIME_LABELS.has(runtime) && generation === "v2" && mode === "copresence";
167
+ }
168
+
169
+ /**
170
+ * Read the OpenCode knobs the hub persists in `nodes.config_snapshot`.
171
+ * Both are reported by agent-node (see buildConfigSnapshot in
172
+ * agent-node/src/runtime/config-apply.ts) and are the only two fields
173
+ * the apply-mode decision needs.
174
+ *
175
+ * Snapshot is stored as text but older rows hold objects; a parse
176
+ * failure means "this cell is empty" (never an exception) — same
177
+ * fail-shape as the api-nodes projection.
178
+ */
179
+ export function readOpencodeSnapshotFlags(configSnapshot: unknown): {
180
+ opencodeGeneration: "v1" | "v2" | null;
181
+ opencodeMode: string | null;
182
+ } {
183
+ let parsed: any = null;
184
+ try {
185
+ parsed = typeof configSnapshot === "string"
186
+ ? (configSnapshot ? JSON.parse(configSnapshot) : null)
187
+ : configSnapshot;
188
+ } catch {
189
+ parsed = null;
190
+ }
191
+ const flags = parsed?.flags;
192
+ const generation = flags?.opencodeGeneration;
193
+ const mode = flags?.opencodeMode;
194
+ return {
195
+ opencodeGeneration: generation === "v1" || generation === "v2" ? generation : null,
196
+ opencodeMode: typeof mode === "string" && mode.length <= 32 ? mode : null,
197
+ };
198
+ }
199
+
200
+ /**
201
+ * True when the node can swap the live model without a process restart.
202
+ *
203
+ * Mirrors `computeApplyMode` in agent-node/src/runtime/config-apply.ts
204
+ * (#3146). The node has been able to do this since OpenCode V2
205
+ * co-presence shipped, but the hub classified every `model` patch as
206
+ * `restart`, so the client showed "restarting" and waited for a
207
+ * restart that the node deliberately never performed. Hub and node now
208
+ * agree on the same three conditions:
209
+ *
210
+ * runtime ∈ OpenCode · opencodeGeneration === "v2" · mode === "copresence"
211
+ *
212
+ * `copresence` is required because the hot switch drives the running
213
+ * TUI session (`opencodeCopresenceSession.setSelectedModel`); a
214
+ * headless V2 node has no such session and must restart. Only a
215
+ * model-ONLY patch qualifies — adding flags or channels drags in
216
+ * restart-tier fields, and the node would restart anyway.
217
+ */
218
+ export function isHotModelSwitchSupported(
219
+ model: string | undefined,
220
+ flags: Record<string, unknown>,
221
+ channels: string[] | undefined,
222
+ runtime: string | null | undefined,
223
+ opencodeGeneration: "v1" | "v2" | null,
224
+ opencodeMode: string | null,
225
+ ): boolean {
226
+ if (model === undefined) return false;
227
+ if (Object.keys(flags).length !== 0) return false;
228
+ if (channels !== undefined) return false;
229
+ return isOpenCodeV2CopresenceRuntime(runtime, opencodeGeneration, opencodeMode);
230
+ }
231
+
232
+ /**
233
+ * Read `config_update_capable` from `nodes.config_snapshot`.
234
+ *
235
+ * agent-node reports this as "this process runs under a supervisor wrapper that
236
+ * honours the sentinel-75 restart path" (agent-node/src/cli.ts report_status).
237
+ * A bare-spawn node (`nohup node cli.js …`, no pm2/anet wrapper) reports
238
+ * `false` — and a restart-tier patch on such a node writes the config, acks
239
+ * `restarting`, exits 75, and **nothing respawns it**. The update then sits in
240
+ * `pending`/`restarting` until the 60s reaper marks it `timeout`, so the caller
241
+ * only ever sees "timed out" and never learns that the node is now down.
242
+ *
243
+ * Absent/unparseable snapshot → `null` ("unknown"), never `false`: we must not
244
+ * warn on nodes that simply haven't reported it yet.
245
+ */
246
+ export function readConfigUpdateCapable(configSnapshot: unknown): boolean | null {
247
+ let parsed: any = null;
248
+ try {
249
+ parsed = typeof configSnapshot === "string"
250
+ ? (configSnapshot ? JSON.parse(configSnapshot) : null)
251
+ : configSnapshot;
252
+ } catch {
253
+ return null;
254
+ }
255
+ // 优先读更具名的 `restart_supervised`(agent-node 同时上报,永远同值);
256
+ // 旧客户端只报 config_update_capable,回退读它。
257
+ const named = parsed?.restart_supervised;
258
+ if (typeof named === "boolean") return named;
259
+ const value = parsed?.config_update_capable;
260
+ return typeof value === "boolean" ? value : null;
261
+ }
262
+
263
+ /**
264
+ * 2026-10-31 —— 双写字段的**分歧**本身是一条读数,而今天它是静默的。
265
+ *
266
+ * `readConfigUpdateCapable` 在两个字段都在时优先读 `restart_supervised`,
267
+ * 文档说它们「永远同值」。所以「不同值」不是噪声,是一个真实信号:
268
+ * 半升级(wrapper 转发了一路、漏了另一路)、旧节点、或有人手改了快照。
269
+ *
270
+ * 🔴 为什么值得单独一个函数:
271
+ * 两个生产者的字段,只要**允许一个遮住另一个**,那个被遮住的值就再也读不到了 ——
272
+ * 而“读不到”与“两个一致”在调用方看来完全一样。这正是隐性回退的通用形状:
273
+ * 值从一个隐式来源解析出来,解析过程不留痕。
274
+ *
275
+ * 返回值:只在**两个都是布尔且不同**时给出两份值;其余一律 null(没有分歧可报)。
276
+ * 调用方负责把 `restart_supervision_flag_divergence` 放进 warnings —— 本函数不改判据。
277
+ */
278
+ export function configUpdateCapabilityDivergence(
279
+ configSnapshot: unknown,
280
+ ): { named: boolean; legacy: boolean } | null {
281
+ let parsed: any = null;
282
+ try {
283
+ parsed = typeof configSnapshot === "string"
284
+ ? (configSnapshot ? JSON.parse(configSnapshot) : null)
285
+ : configSnapshot;
286
+ } catch {
287
+ return null;
288
+ }
289
+ const named = parsed?.restart_supervised;
290
+ const legacy = parsed?.config_update_capable;
291
+ if (typeof named !== "boolean" || typeof legacy !== "boolean") return null;
292
+ return named === legacy ? null : { named, legacy };
293
+ }
294
+
295
+ /**
296
+ * 2026-10-31 —— 用节点**自己**报的托管判定取代「没有声明」(#3168 的自然下一步)。
297
+ *
298
+ * `config_update_capable=false` 只说明「没有声明 sentinel-75 重启包装」,
299
+ * 而 systemd/pm2 托管的节点也可能报 false(它们不走那套包装)⇒ 于是只能写成
300
+ * 「does not advertise」,把判断交给调用方 —— 那是对的,但不够具体。
301
+ *
302
+ * 节点现在会上报三态 `systemd_supervision`(supervised / not_supervised / unknown),
303
+ * 于是「它自己说没人拉它」与「它自己说有人拉它」可以被分开讲:
304
+ *
305
+ * not_supervised ⇒ 明确后果:exit 75 之后**没有东西会把它拉起来**
306
+ * supervised ⇒ 不做警告(具名判定优先,胜过 legacy 布尔)
307
+ * unknown/缺失 ⇒ 退回旧口径(「没有声明」),因为「不知道」不能当成「没人拉」
308
+ *
309
+ * 🔴 三态而不是布尔:`unknown` 与 `not_supervised` 在处置上完全相反 ——
310
+ * 前者要人去看,后者要人别发这类变更。
311
+ */
312
+ export function readSystemdSupervision(
313
+ configSnapshot: unknown,
314
+ ): "supervised" | "not_supervised" | "unknown" | null {
315
+ let parsed: any = null;
316
+ try {
317
+ parsed = typeof configSnapshot === "string"
318
+ ? (configSnapshot ? JSON.parse(configSnapshot) : null)
319
+ : configSnapshot;
320
+ } catch {
321
+ return null;
322
+ }
323
+ const v = parsed?.systemd_supervision;
324
+ if (v === "supervised" || v === "not_supervised" || v === "unknown") return v;
325
+ return null; // 老节点没有这个字段:**缺失 ≠ unknown**,缺失是「这一版还不会报」
326
+ }
327
+
129
328
  /**
130
329
  * Classify the patch's required apply mode. Empty patch (model + flags
131
330
  * both empty, no channels) → "restart_only" (used by restart_node tool).
@@ -134,15 +333,34 @@ export function isAllowedToChangeFlag(
134
333
  * once at boot in agent-node/src/cli.ts and forks per-channel workers
135
334
  * from it, so a channel enable/disable takes effect on process restart
136
335
  * only — see #260 P5 wire discipline). Otherwise → "hot".
336
+ *
337
+ * `opts` carries the target node's runtime identity. Callers that omit
338
+ * it get the pre-#3146 behaviour (model ⇒ restart), which is the safe
339
+ * answer for any node we know nothing about.
137
340
  */
138
341
  export function computeApplyMode(
139
342
  model: string | undefined,
140
343
  flags: Record<string, unknown>,
141
344
  channels?: string[] | undefined,
345
+ opts?: {
346
+ runtime?: string | null;
347
+ opencodeGeneration?: "v1" | "v2" | null;
348
+ opencodeMode?: string | null;
349
+ },
142
350
  ): "hot" | "restart" | "restart_only" {
143
351
  const fieldCount = (model !== undefined ? 1 : 0) + Object.keys(flags).length + (channels !== undefined ? 1 : 0);
144
352
  if (fieldCount === 0) return "restart_only";
145
- if (model !== undefined) return "restart";
353
+ if (model !== undefined) {
354
+ if (isHotModelSwitchSupported(
355
+ model,
356
+ flags,
357
+ channels,
358
+ opts?.runtime,
359
+ opts?.opencodeGeneration ?? null,
360
+ opts?.opencodeMode ?? null,
361
+ )) return "hot";
362
+ return "restart";
363
+ }
146
364
  if (channels !== undefined) return "restart";
147
365
  for (const key of Object.keys(flags)) {
148
366
  if (RESTART_REQUIRED_FLAGS.has(key)) return "restart";
@@ -184,6 +402,7 @@ export function validatePatch(
184
402
  model: string | undefined,
185
403
  flags: Record<string, unknown>,
186
404
  channels?: string[] | undefined,
405
+ nodeRuntime?: string | null,
187
406
  ): { field: string; reason: string } | null {
188
407
  if (model !== undefined) {
189
408
  if (typeof model !== "string" || model.length === 0 || model.length > 200) {
@@ -211,6 +430,8 @@ export function validatePatch(
211
430
  if (!ALLOWED_FLAGS.has(key)) {
212
431
  return { field: `flags.${key}`, reason: "not in allowlist" };
213
432
  }
433
+ const codexFail = validateCodexConfigFlag(key, val, nodeRuntime ?? null);
434
+ if (codexFail) return codexFail;
214
435
  switch (key) {
215
436
  case "permissionMode":
216
437
  if (
@@ -239,8 +460,10 @@ export function validatePatch(
239
460
  }
240
461
  break;
241
462
  case "timeout":
242
- if (typeof val !== "number" || !Number.isInteger(val) || val < 0 || val > 3_600_000) {
243
- return { field: "flags.timeout", reason: "must be an integer ms in [0, 3_600_000]" };
463
+ // #605 —— 与 create_node 同一把尺子(create-node-validate.ts isValidTimeoutMs):
464
+ // 毫秒,0 或 1000..3_600_000;1..999 是「按秒填了」的形状,拒绝而不是给一个亚秒超时。
465
+ if (!isValidTimeoutMs(val)) {
466
+ return { field: "flags.timeout", reason: TIMEOUT_MS_REASON };
244
467
  }
245
468
  break;
246
469
  }
@@ -0,0 +1,14 @@
1
+ export const PROJECT_CREATE_CAPABILITY = "project-new-session-v1-opencode-2.0.22";
2
+ export const PROJECT_CREATE_ROOT = "/api/node-session-control/source/project-create";
3
+ export const SESSION_ATTACH_CAPABILITY = "existing-session-attach-v1-opencode-2.0.22";
4
+ export type SessionAttachSelection = { selection_scope: string; snapshot_id: string; session_id: string; child_name: string; intent_id: string };
5
+ export type ProjectOperation = "project_new_session" | "attach_existing_session";
6
+ export type ProjectCreateSelection = { selection_scope: string; snapshot_id: string; project_id: string; child_name: string; intent_id: string };
7
+ export type ProjectCreateTask = ProjectCreateSelection & { request_id: string; network_id: string; source_node_id: string;
8
+ daemon_node_id: string; accepted_at: number; expires_at: number; operation_kind?: ProjectOperation; selected_session_id?: string | null };
9
+ export type ProjectCreateDelivery = ProjectCreateTask & { session_id: string; child_node_id: string; child_token: string; credential_expires_at: number };
10
+ export type ProjectCreateStatus = "create_pending" | "create_claimed" | "delivery_pending" | "delivery_claimed" | "registration_confirmed" | "unknown_rotation_required";
11
+ export type ProjectCreateReceipt = { ok: true; request_id: string; network_id: string; daemon_node_id: string; intent_id: string; child_name: string;
12
+ status: ProjectCreateStatus; accepted_at: number; expires_at: number; operation_kind: ProjectOperation; session_creation: "confirmed" | "not_started" | "unknown" | "not_performed";
13
+ child_node_id?: string; registration_is_health: false; native_acceptance: "not_verified" };
14
+ export const isProjectCreateId = (v: unknown): v is string => typeof v === "string" && /^prjc_[a-f0-9-]{36}$/.test(v);
@@ -0,0 +1,16 @@
1
+ import { isSourceExecutionTask, type SourceExecutionTask } from "./session-source-execution";
2
+ export const SOURCE_DELIVERY_CAPABILITY = "source-node-delivery-v1-opencode-2.0.22";
3
+ export type SourceNodeDelivery = {
4
+ delivery_id: string; execution: SourceExecutionTask; session_id: string;
5
+ child_node_id: string; child_name: string; child_token: string; expires_at: number;
6
+ };
7
+ export function isSourceNodeDelivery(v: any): v is SourceNodeDelivery {
8
+ return !!v && typeof v === "object" && Object.keys(v).length === 7
9
+ && typeof v.delivery_id === "string" && /^srcd_[a-f0-9-]{36}$/.test(v.delivery_id)
10
+ && isSourceExecutionTask(v.execution) && typeof v.session_id === "string" && /^ses_[A-Za-z0-9]+$/.test(v.session_id)
11
+ && typeof v.child_node_id === "string" && v.child_node_id === `node_${v.delivery_id}`
12
+ && typeof v.child_name === "string" && v.child_name.length > 0 && v.child_name.length <= 256
13
+ && typeof v.child_token === "string" && /^ntok_[A-Za-z0-9_-]+$/.test(v.child_token)
14
+ && Number.isSafeInteger(v.expires_at) && v.expires_at > 0
15
+ && (v.execution.evidence.request.mode === "attach_existing" ? v.session_id === v.execution.evidence.request.session_id : v.session_id !== v.execution.evidence.request.session_id);
16
+ }
@@ -0,0 +1,54 @@
1
+ export const SOURCE_DIRECTORY_CAPABILITY = "source-directory-read-v1-opencode-2.0.22";
2
+ export type SourceDirectorySelection = { network_id: string; source_node_id: string; daemon_node_id: string };
3
+ export type SourceDirectoryRequest = SourceDirectorySelection & ({ server_id?: never; selection_scope?: never }
4
+ | { server_id: string; selection_scope: string });
5
+ export type SourceDirectoryReceipt = SourceDirectoryTask & { ok: true; status: "pending" | "claimed" | "ready" | "failed";
6
+ snapshot?: SourceDirectorySnapshot; error?: "directory_read_failed" };
7
+ export type SourceDirectoryTask = { request_id: string; network_id: string; source_node_id: string; daemon_node_id: string;
8
+ server_id?: string; expires_at: number };
9
+ export type SourceDirectorySnapshot = { snapshot_id: string; expires_at: number; selection_expires_at?: number; lease_kind: "process_local";
10
+ servers: { server_id: string; title: string }[];
11
+ projects: { project_id: string; server_id: string; title: string }[];
12
+ sessions: { session_id: string; project_id: string; title: string; activity: "running" | "unknown"; binding: "unknown"; attach_authorized: false }[];
13
+ completeness: "authorized_scope_only" };
14
+ const id = (v: unknown, prefix: string) => typeof v === "string" && new RegExp(`^${prefix}_[a-f0-9-]{36}$`).test(v);
15
+ export const isDirectoryRequestId = (v: unknown) => id(v, "srcdir");
16
+ export const isDirectoryServerId = (v: unknown) => id(v, "osrv");
17
+ export function directoryTitle(v: unknown, fallback: "Project" | "Session" | "Server"): string {
18
+ if (typeof v !== "string" || !v.trim() || v.length > 120
19
+ || /[\x00-\x1f\x7f/\\:]|(?:ntok_|atok_|sk-|Bearer|password|token|gh[pousr]_|github_pat_|xox[baprs]-|AKIA|AIza|ya29\.|glpat-)|\b[A-Za-z0-9_=-]{28,}\b/i.test(v)) return fallback;
20
+ return v;
21
+ }
22
+ // Exact key-set comparison as arrays, never a joined string: a key name that
23
+ // contained the separator could otherwise hide an extra key (keyset-join gate).
24
+ const keysAreExactly = (v: unknown, expected: readonly string[]): boolean => {
25
+ const keys = Object.keys(v as Record<string, unknown>).sort();
26
+ return keys.length === expected.length && keys.every((k, i) => k === expected[i]);
27
+ };
28
+ const SNAPSHOT_KEYS = ["completeness", "expires_at", "lease_kind", "projects", "servers", "sessions", "snapshot_id"] as const;
29
+ const SNAPSHOT_KEYS_WITH_SELECTION = ["completeness", "expires_at", "lease_kind", "projects", "selection_expires_at", "servers", "sessions", "snapshot_id"] as const;
30
+ export function isSourceDirectorySnapshot(v: any, expires: number): v is SourceDirectorySnapshot {
31
+ if (!v || !(keysAreExactly(v, SNAPSHOT_KEYS) || keysAreExactly(v, SNAPSHOT_KEYS_WITH_SELECTION))
32
+ || !id(v.snapshot_id, "snap") || v.expires_at !== expires || v.lease_kind !== "process_local" || v.completeness !== "authorized_scope_only"
33
+ || (v.selection_expires_at !== undefined && v.selection_expires_at !== expires + 600000)
34
+ || !Array.isArray(v.servers) || v.servers.length > 16 || !Array.isArray(v.projects) || v.projects.length > 16
35
+ || !Array.isArray(v.sessions) || v.sessions.length > 100) return false;
36
+ const servers = new Set<string>(), projects = new Set<string>(), sessions = new Set<string>();
37
+ for (const s of v.servers) {
38
+ if (!s || !keysAreExactly(s, ["server_id", "title"]) || !isDirectoryServerId(s.server_id) || servers.has(s.server_id)
39
+ || directoryTitle(s.title, "Server") !== s.title) return false;
40
+ servers.add(s.server_id);
41
+ }
42
+ for (const p of v.projects) {
43
+ if (!p || !keysAreExactly(p, ["project_id", "server_id", "title"]) || !id(p.project_id, "oprj") || projects.has(p.project_id)
44
+ || !servers.has(p.server_id) || directoryTitle(p.title, "Project") !== p.title) return false;
45
+ projects.add(p.project_id);
46
+ }
47
+ for (const s of v.sessions) {
48
+ if (!s || !keysAreExactly(s, ["activity", "attach_authorized", "binding", "project_id", "session_id", "title"]) || !id(s.session_id, "oses") || sessions.has(s.session_id)
49
+ || !projects.has(s.project_id) || directoryTitle(s.title, "Session") !== s.title || !["running", "unknown"].includes(s.activity)
50
+ || s.binding !== "unknown" || s.attach_authorized !== false) return false;
51
+ sessions.add(s.session_id);
52
+ }
53
+ return true;
54
+ }
@@ -0,0 +1,72 @@
1
+ // Read-only wire contract. No URL/auth/path input and no create-node directive.
2
+ export type SourceReadChallenge = {
3
+ request_id: string; challenge: string; network_id: string; daemon_node_id: string;
4
+ source_node_id: string; alias: string; session_id: string; expires_at: number;
5
+ user_id: string; requester_token_id: string; mode: "attach_existing" | "fork_independent";
6
+ };
7
+ export type SourceReadEvidence = {
8
+ request: SourceReadChallenge;
9
+ kind: "process_local_lease"; lease_id: string; api_version: "2.0.22" | "2.0.22-anet-test.29040e2";
10
+ canonical_directory: string; directory: { dev: string; ino: string; uid: number };
11
+ // OS observation, not caller strings or a binary-version attestation.
12
+ server: { pid: number; start_ticks: string; listen_inode: string };
13
+ };
14
+ const object = (v: unknown): v is Record<string, any> => !!v && typeof v === "object" && !Array.isArray(v);
15
+ const keys = (v: Record<string, any>, names: string[]) => Object.keys(v).length === names.length && names.every(k => k in v);
16
+ const id = (v: unknown) => typeof v === "string" && /^[A-Za-z0-9_-]{1,200}$/.test(v);
17
+ const decimal = (v: unknown) => typeof v === "string" && /^\d{1,30}$/.test(v);
18
+ export function isSourceReadChallenge(v: unknown): v is SourceReadChallenge {
19
+ return object(v) && keys(v, ["request_id", "challenge", "network_id", "daemon_node_id", "source_node_id", "alias", "session_id", "expires_at", "user_id", "requester_token_id", "mode"])
20
+ && typeof v.request_id === "string" && /^srcq_[A-Za-z0-9_-]{1,100}$/.test(v.request_id)
21
+ && typeof v.challenge === "string" && /^[A-Za-z0-9_-]{43}$/.test(v.challenge)
22
+ && [v.network_id, v.daemon_node_id, v.source_node_id, v.user_id, v.requester_token_id].every(id)
23
+ && (v.mode === "attach_existing" || v.mode === "fork_independent")
24
+ && typeof v.alias === "string" && v.alias.length > 0 && v.alias.length <= 256 && !/[\x00-\x1f\x7f/\\]/.test(v.alias)
25
+ && typeof v.session_id === "string" && /^ses_[A-Za-z0-9]{1,190}$/.test(v.session_id)
26
+ && Number.isSafeInteger(v.expires_at) && v.expires_at > 0;
27
+ }
28
+ export function isSourceReadEvidence(v: unknown): v is SourceReadEvidence {
29
+ return object(v) && keys(v, ["request", "kind", "lease_id", "api_version", "canonical_directory", "directory", "server"])
30
+ && isSourceReadChallenge(v.request) && v.kind === "process_local_lease" && ["2.0.22", "2.0.22-anet-test.29040e2"].includes(v.api_version)
31
+ && typeof v.lease_id === "string" && /^srcl_[A-Za-z0-9_-]{36}$/.test(v.lease_id)
32
+ && typeof v.canonical_directory === "string" && v.canonical_directory.startsWith("/")
33
+ && v.canonical_directory.length <= 4096 && !/[\x00-\x1f\x7f]/.test(v.canonical_directory)
34
+ && object(v.directory) && keys(v.directory, ["dev", "ino", "uid"]) && decimal(v.directory.dev) && decimal(v.directory.ino)
35
+ && Number.isSafeInteger(v.directory.uid) && v.directory.uid >= 0
36
+ && object(v.server) && keys(v.server, ["pid", "start_ticks", "listen_inode"])
37
+ && Number.isSafeInteger(v.server.pid) && v.server.pid > 1 && decimal(v.server.start_ticks) && decimal(v.server.listen_inode);
38
+ }
39
+ export function sameSourceChallenge(a: SourceReadChallenge, b: SourceReadChallenge): boolean {
40
+ return (Object.keys(a) as (keyof SourceReadChallenge)[]).every(k => a[k] === b[k]);
41
+ }
42
+
43
+ export type SourceLeaseRecheck = {
44
+ request_id: string; challenge: string; evidence: SourceReadEvidence;
45
+ intent_id: string; child_name: string; expires_at: number;
46
+ };
47
+ export type SourceLeaseConfirmation = {
48
+ request: SourceLeaseRecheck; outcome: "lease_current";
49
+ execution: "daemon_recheck_in_execution_context_required";
50
+ };
51
+ export function isSourceLeaseRecheck(v: unknown): v is SourceLeaseRecheck {
52
+ return object(v) && keys(v, ["request_id", "challenge", "evidence", "intent_id", "child_name", "expires_at"])
53
+ && typeof v.request_id === "string" && /^srcr_[A-Za-z0-9_-]{36}$/.test(v.request_id)
54
+ && typeof v.challenge === "string" && /^[A-Za-z0-9_-]{43}$/.test(v.challenge)
55
+ && isSourceReadEvidence(v.evidence) && id(v.intent_id)
56
+ && typeof v.child_name === "string" && v.child_name.length > 0 && v.child_name.length <= 256
57
+ && Number.isSafeInteger(v.expires_at) && v.expires_at > 0 && v.expires_at <= v.evidence.request.expires_at;
58
+ }
59
+ export function sameSourceEvidence(a: SourceReadEvidence, b: SourceReadEvidence): boolean {
60
+ return sameSourceChallenge(a.request, b.request) && a.kind === b.kind && a.lease_id === b.lease_id && a.api_version === b.api_version
61
+ && a.canonical_directory === b.canonical_directory && a.directory.dev === b.directory.dev && a.directory.ino === b.directory.ino
62
+ && a.directory.uid === b.directory.uid && a.server.pid === b.server.pid && a.server.start_ticks === b.server.start_ticks
63
+ && a.server.listen_inode === b.server.listen_inode;
64
+ }
65
+ export function sameSourceRecheck(a: SourceLeaseRecheck, b: SourceLeaseRecheck): boolean {
66
+ return a.request_id === b.request_id && a.challenge === b.challenge && a.intent_id === b.intent_id
67
+ && a.child_name === b.child_name && a.expires_at === b.expires_at && sameSourceEvidence(a.evidence, b.evidence);
68
+ }
69
+ export function isSourceLeaseConfirmation(v: unknown): v is SourceLeaseConfirmation {
70
+ return object(v) && keys(v, ["request", "outcome", "execution"]) && isSourceLeaseRecheck(v.request)
71
+ && v.outcome === "lease_current" && v.execution === "daemon_recheck_in_execution_context_required";
72
+ }
@@ -0,0 +1,24 @@
1
+ import { isSourceReadEvidence, type SourceReadEvidence } from "./session-source-evidence";
2
+ export type SourceExecutionTask = {
3
+ execution_id: string; admission_request_id: string; evidence: SourceReadEvidence;
4
+ authorized_until: number; owner_policy: "retain_existing_bridge_only";
5
+ };
6
+ export type SourceExecutionResult = {
7
+ execution_id: string; admission_request_id: string;
8
+ status: "session_ready" | "refused" | "unknown";
9
+ session_id?: string; bridge: "not_created";
10
+ };
11
+ const object = (v: unknown): v is Record<string, any> => !!v && typeof v === "object" && !Array.isArray(v);
12
+ export function isSourceExecutionTask(v: unknown): v is SourceExecutionTask {
13
+ return object(v) && Object.keys(v).length === 5 && typeof v.execution_id === "string" && /^srcx_[a-f0-9-]{36}$/.test(v.execution_id)
14
+ && typeof v.admission_request_id === "string" && /^cr_[A-Za-z0-9_-]{1,190}$/.test(v.admission_request_id)
15
+ && isSourceReadEvidence(v.evidence) && v.owner_policy === "retain_existing_bridge_only"
16
+ && Number.isSafeInteger(v.authorized_until) && v.authorized_until > 0 && v.authorized_until <= v.evidence.request.expires_at;
17
+ }
18
+ export function isSourceExecutionResult(v: unknown, task: SourceExecutionTask): v is SourceExecutionResult {
19
+ if (!object(v) || Object.keys(v).some(k => !["execution_id", "admission_request_id", "status", "session_id", "bridge"].includes(k))
20
+ || v.execution_id !== task.execution_id || v.admission_request_id !== task.admission_request_id || v.bridge !== "not_created") return false;
21
+ if (v.status === "unknown" || v.status === "refused") return v.session_id === undefined;
22
+ return v.status === "session_ready" && typeof v.session_id === "string" && /^ses_[A-Za-z0-9]+$/.test(v.session_id)
23
+ && (task.evidence.request.mode === "attach_existing" ? v.session_id === task.evidence.request.session_id : v.session_id !== task.evidence.request.session_id);
24
+ }
@@ -0,0 +1,18 @@
1
+ import type { SourceReadChallenge, SourceLeaseRecheck } from "./session-source-evidence";
2
+ import type { SourceExecutionTask } from "./session-source-execution";
3
+ import type { SourceNodeDelivery } from "./session-source-delivery";
4
+ export const SOURCE_FORK_ROOT = "/api/node-session-control/source";
5
+ export const SOURCE_FORK_CAPABILITY = "source-fork-transport-v1-opencode-2.0.22";
6
+ export type SourceForkSelection = { network_id: string; source_node_id: string; session_id: string;
7
+ mode: "fork_independent"; child_name: string; intent_id: string };
8
+ export type SourceForkTask = { request_id: string; intent_id: string; network_id: string; source_node_id: string;
9
+ daemon_node_id: string } & (
10
+ { phase: "evidence"; task: SourceReadChallenge } | { phase: "recheck"; task: SourceLeaseRecheck }
11
+ | { phase: "execute"; task: SourceExecutionTask } | { phase: "deliver"; task: SourceNodeDelivery });
12
+ export type SourceForkStatus = "evidence_pending" | "evidence_claimed" | "recheck_pending" | "recheck_claimed"
13
+ | "execute_pending" | "execute_claimed" | "deliver_pending" | "deliver_claimed"
14
+ | "registration_confirmed" | "refused" | "unknown_rotation_required";
15
+ export type SourceForkReceipt = SourceForkSelection & { ok: true; request_id: string; daemon_node_id: string;
16
+ status: SourceForkStatus; expires_at: number; child_node_id?: string; child_session_id?: string;
17
+ native_acceptance: "not_verified"; dispatch_scope: "gated_preview" };
18
+ export const isSourceForkRequestId = (v: unknown): v is string => typeof v === "string" && /^srcf_[a-f0-9-]{36}$/.test(v);
@@ -0,0 +1,73 @@
1
+ // 2026-10-31 —— 「设了空」与「没设」是两件事。
2
+ //
3
+ // 起因(外部复核):`'TMAICLOUD_PUBLIC_ORIGIN' in process.env` 才能区分「设了空」与「没设」,
4
+ // 而 `if (process.env.X)` 把两者都判假。
5
+ //
6
+ // 在本仓的落点:COMMHUB_CORS_ORIGINS。
7
+ // · 未设 ⇒ 开发默认(localhost 两个端口)
8
+ // · 设了但为空 ⇒ **空清单**(谁都不许)
9
+ // 如果两者都判假并回退默认,那么运维写 `COMMHUB_CORS_ORIGINS=`(想「一个都不许」)
10
+ // 得到的却是「放行 localhost」—— 空落到了**更松**的一侧。
11
+ //
12
+ // 判据:写一个允许清单的默认值之前,先写出它的**最松值**。
13
+ // 这里最松值不是空(空 = 谁都不许 = 最严),而是 `*`。所以空绝不能被读成「用默认」。
14
+ import { describe, expect, test } from "bun:test";
15
+ import { assertCorsOrigins, normalizeOrigin, parseCorsOrigins } from "./server.js";
16
+
17
+ describe("parseCorsOrigins: 未设 / 设空 / 设值 三态", () => {
18
+ test("未设 ⇒ 开发默认(localhost 两个端口)", () => {
19
+ expect(parseCorsOrigins(undefined)).toEqual(["http://localhost:3000", "http://localhost:3001"]);
20
+ });
21
+
22
+ test("设了但为空 ⇒ 空清单(谁都不许),**不回退默认**", () => {
23
+ expect(parseCorsOrigins("")).toEqual([]);
24
+ // 只写了分隔符 / 空白也是空清单,而不是"默认"
25
+ expect(parseCorsOrigins(",")).toEqual([]);
26
+ expect(parseCorsOrigins(" , ")).toEqual([]);
27
+ });
28
+
29
+ test("设了值 ⇒ 逐条 trim,空条目被丢掉(不许出现空字符串条目)", () => {
30
+ expect(parseCorsOrigins(" https://a.example , https://b.example ")).toEqual(["https://a.example", "https://b.example"]);
31
+ expect(parseCorsOrigins("https://a.example,")).toEqual(["https://a.example"]);
32
+ });
33
+
34
+ // 2026-10-31 —— **语义变更(更严)**:非法条目现在被丢弃,而不是原样保留。
35
+ // 旧实现保留 "*",于是运行期 `includes(origin)` 永远不命中 ——
36
+ // 「保留一条永不匹配的项」与「丢弃它」在行为上相同,但前者会让运维以为已经放行。
37
+ test("非法条目被丢弃('*' / 裸主机名 / 非 http(s))", () => {
38
+ expect(parseCorsOrigins("*")).toEqual([]);
39
+ expect(parseCorsOrigins("a.example")).toEqual([]);
40
+ expect(parseCorsOrigins("ftp://a.example")).toEqual([]);
41
+ expect(parseCorsOrigins(undefined)).not.toContain("*");
42
+ });
43
+
44
+ // 外部复核 ①:`https://host` 与 `https://host:443` 在字符串上不等、在 origin 上相等。
45
+ test("默认端口与大小写被规范化 —— 字符串比较会在这里判不同", () => {
46
+ expect(normalizeOrigin("https://A.Example:443")).toBe("https://a.example");
47
+ expect(normalizeOrigin("https://a.example")).toBe("https://a.example");
48
+ expect(normalizeOrigin("http://a.example:80")).toBe("http://a.example");
49
+ expect(normalizeOrigin("https://a.example:8443")).toBe("https://a.example:8443");
50
+ expect(normalizeOrigin("https://a.example/")).toBe("https://a.example");
51
+ // 清单写成带默认端口 ⇒ 规范化后与不带端口的请求相等(这正是那批「地址栏正常却全 403」的用户)
52
+ expect(parseCorsOrigins("https://a.example:443")).toEqual(["https://a.example"]);
53
+ // 去重:同一 origin 的两种写法只留一条
54
+ expect(parseCorsOrigins("https://a.example,https://a.example:443/")).toEqual(["https://a.example"]);
55
+ });
56
+
57
+ // 2026-10-31 —— **配错了就别起服务**(「解除武装 ≠ 修好」):
58
+ // 丢弃非法条目只是让服务更安全,但配置仍然是错的,而且没有任何信号。
59
+ // 这里钉的是启动期断言:非法条目必须**抛**,且把非法条目逐个点出来。
60
+ test("startup assertion refuses invalid CORS entries and names them", () => {
61
+ expect(() => assertCorsOrigins(undefined)).not.toThrow(); // 未设 ⇒ 默认,合法
62
+ expect(() => assertCorsOrigins("")).not.toThrow(); // 设空 ⇒ 空清单,合法(更严)
63
+ expect(() => assertCorsOrigins("https://a.example,http://b.example:8080")).not.toThrow();
64
+ // 每个非法形态都要抛,并且错误信息里能看到那个值
65
+ for (const bad of ["*", "a.example", "ftp://a.example", "https://a.example, *"]) {
66
+ let msg = "";
67
+ try { assertCorsOrigins(bad); } catch (e) { msg = String((e as Error).message); }
68
+ expect(msg).toContain("不是 http(s) origin");
69
+ expect(msg.length).toBeGreaterThan(20);
70
+ }
71
+ expect(() => assertCorsOrigins("*")).toThrow(/\*/);
72
+ });
73
+ });