@oyasmi/pipiclaw 0.9.4-beta.2 → 0.9.5-beta.2

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 (405) hide show
  1. package/README.en.md +28 -22
  2. package/README.md +27 -21
  3. package/dist/agent/builtin-commands.d.ts +7 -2
  4. package/dist/agent/builtin-commands.js +19 -23
  5. package/dist/agent/channel-runner.d.ts +13 -13
  6. package/dist/agent/channel-runner.js +93 -76
  7. package/dist/agent/maintenance-context.d.ts +6 -5
  8. package/dist/agent/maintenance-context.js +15 -15
  9. package/dist/agent/prompt/manifest.js +1 -1
  10. package/dist/agent/prompt/resources.d.ts +1 -1
  11. package/dist/agent/prompt/resources.js +1 -1
  12. package/dist/agent/prompt/sections.js +4 -4
  13. package/dist/agent/runner-factory.d.ts +6 -6
  14. package/dist/agent/runner-factory.js +6 -7
  15. package/dist/agent/session/compaction-input.d.ts +6 -0
  16. package/dist/agent/session/compaction-input.js +75 -0
  17. package/dist/agent/session/compaction-snapshot.d.ts +30 -0
  18. package/dist/agent/session/compaction-snapshot.js +154 -0
  19. package/dist/agent/session/compaction-summary.d.ts +39 -0
  20. package/dist/agent/session/compaction-summary.js +158 -0
  21. package/dist/agent/session/compaction.d.ts +35 -0
  22. package/dist/agent/session/compaction.js +294 -0
  23. package/dist/agent/session/context-input.d.ts +15 -0
  24. package/dist/agent/session/context-input.js +82 -0
  25. package/dist/agent/session/context-snapshot.d.ts +11 -0
  26. package/dist/agent/session/context-snapshot.js +72 -0
  27. package/dist/agent/session/event-guards.d.ts +2 -0
  28. package/dist/agent/session/event-guards.js +4 -0
  29. package/dist/agent/session/request-tools.d.ts +3 -0
  30. package/dist/agent/session/request-tools.js +20 -0
  31. package/dist/agent/session/session-adapter.d.ts +2 -1
  32. package/dist/agent/session/session-adapter.js +6 -3
  33. package/dist/agent/session/session-events.d.ts +5 -4
  34. package/dist/agent/session/session-events.js +46 -59
  35. package/dist/agent/session/session-host.d.ts +29 -41
  36. package/dist/agent/session/session-host.js +86 -69
  37. package/dist/agent/session/settings-diagnostics.d.ts +5 -0
  38. package/dist/agent/session/settings-diagnostics.js +21 -0
  39. package/dist/agent/status-render.js +5 -2
  40. package/dist/{runtime → agent}/stop-watchdog.d.ts +3 -1
  41. package/dist/{runtime → agent}/stop-watchdog.js +7 -3
  42. package/dist/agent/turn/progress-formatter.d.ts +1 -3
  43. package/dist/agent/turn/progress-formatter.js +3 -22
  44. package/dist/agent/turn/task-digest.d.ts +4 -2
  45. package/dist/agent/turn/task-digest.js +7 -4
  46. package/dist/agent/turn/turn-context.d.ts +11 -7
  47. package/dist/agent/turn/turn-context.js +26 -18
  48. package/dist/agent/turn/turn-finish.d.ts +17 -5
  49. package/dist/agent/turn/turn-finish.js +51 -77
  50. package/dist/agent/turn/turn-input.d.ts +2 -2
  51. package/dist/agent/turn/turn-input.js +4 -3
  52. package/dist/agent/turn/turn-output.d.ts +19 -0
  53. package/dist/agent/turn/turn-output.js +41 -0
  54. package/dist/agent/turn/turn-pipeline.d.ts +11 -8
  55. package/dist/agent/turn/turn-pipeline.js +54 -55
  56. package/dist/agent/turn/turn-prompt.d.ts +1 -1
  57. package/dist/agent/turn/turn-recovery.d.ts +11 -10
  58. package/dist/agent/turn/turn-recovery.js +14 -29
  59. package/dist/agent/types.d.ts +31 -12
  60. package/dist/agent/types.js +2 -1
  61. package/dist/app/auth-command.js +1 -1
  62. package/dist/app/home.js +31 -15
  63. package/dist/channel/active-session-store.d.ts +14 -12
  64. package/dist/channel/active-session-store.js +29 -25
  65. package/dist/channel/attachments.d.ts +25 -0
  66. package/dist/channel/attachments.js +56 -0
  67. package/dist/channel/channel-context.d.ts +87 -96
  68. package/dist/channel/channel-context.js +1 -66
  69. package/dist/channel/channel-event.d.ts +51 -41
  70. package/dist/channel/channel-event.js +6 -11
  71. package/dist/channel/channel-id.d.ts +22 -0
  72. package/dist/channel/channel-id.js +13 -0
  73. package/dist/channel/channel-index.d.ts +17 -34
  74. package/dist/channel/channel-index.js +41 -80
  75. package/dist/channel/config.d.ts +10 -0
  76. package/dist/channel/config.js +55 -0
  77. package/dist/channel/ingress.d.ts +101 -0
  78. package/dist/channel/ingress.js +5 -0
  79. package/dist/channel/store.d.ts +13 -15
  80. package/dist/channel/store.js +21 -25
  81. package/dist/channel/transport.d.ts +99 -0
  82. package/dist/channel/turn-context.d.ts +23 -0
  83. package/dist/channel/turn-context.js +1 -0
  84. package/dist/commands/catalog.d.ts +4 -7
  85. package/dist/commands/catalog.js +24 -31
  86. package/dist/commands/router.d.ts +23 -12
  87. package/dist/commands/router.js +22 -24
  88. package/dist/config/settings.d.ts +5 -4
  89. package/dist/desk/bindings.d.ts +6 -0
  90. package/dist/desk/bindings.js +105 -0
  91. package/dist/desk/commands.d.ts +81 -0
  92. package/dist/desk/commands.js +145 -0
  93. package/dist/desk/default.d.ts +4 -0
  94. package/dist/desk/default.js +11 -0
  95. package/dist/desk/paths.d.ts +41 -0
  96. package/dist/desk/paths.js +81 -0
  97. package/dist/desk/registry.d.ts +17 -0
  98. package/dist/desk/registry.js +191 -0
  99. package/dist/desk/startup-error.d.ts +4 -0
  100. package/dist/desk/startup-error.js +7 -0
  101. package/dist/desk/types.d.ts +72 -0
  102. package/dist/desk/types.js +7 -0
  103. package/dist/dingtalk/bot.d.ts +18 -35
  104. package/dist/dingtalk/bot.js +21 -204
  105. package/dist/dingtalk/channel-ids.d.ts +15 -0
  106. package/dist/dingtalk/channel-ids.js +39 -0
  107. package/dist/dingtalk/config.d.ts +6 -3
  108. package/dist/dingtalk/config.js +13 -25
  109. package/dist/dingtalk/connection.d.ts +1 -0
  110. package/dist/dingtalk/connection.js +5 -4
  111. package/dist/dingtalk/conversation-meta.d.ts +10 -5
  112. package/dist/dingtalk/conversation-meta.js +72 -50
  113. package/dist/dingtalk/delivery.d.ts +30 -14
  114. package/dist/dingtalk/delivery.js +189 -62
  115. package/dist/dingtalk/inbound-media.d.ts +2 -18
  116. package/dist/dingtalk/inbound-media.js +0 -53
  117. package/dist/dingtalk/inbound.d.ts +10 -11
  118. package/dist/dingtalk/inbound.js +12 -19
  119. package/dist/dingtalk/media.d.ts +9 -11
  120. package/dist/dingtalk/media.js +18 -31
  121. package/dist/dingtalk/progress.d.ts +8 -0
  122. package/dist/dingtalk/progress.js +20 -0
  123. package/dist/dingtalk/response-mode.d.ts +13 -0
  124. package/dist/dingtalk/response-mode.js +31 -0
  125. package/dist/dingtalk/transport.d.ts +47 -0
  126. package/dist/dingtalk/transport.js +71 -0
  127. package/dist/events/commands.d.ts +7 -2
  128. package/dist/events/commands.js +40 -22
  129. package/dist/events/pre-action-error.d.ts +10 -0
  130. package/dist/events/pre-action-error.js +10 -0
  131. package/dist/events/types.d.ts +20 -2
  132. package/dist/events/watcher.d.ts +14 -7
  133. package/dist/events/watcher.js +49 -26
  134. package/dist/index.d.ts +4 -2
  135. package/dist/jobs/completion-wake.d.ts +14 -0
  136. package/dist/jobs/completion-wake.js +36 -0
  137. package/dist/jobs/manager.d.ts +27 -22
  138. package/dist/jobs/manager.js +58 -91
  139. package/dist/jobs/record.d.ts +2 -0
  140. package/dist/jobs/record.js +2 -0
  141. package/dist/log.d.ts +0 -1
  142. package/dist/log.js +1 -1
  143. package/dist/main.js +10 -3
  144. package/dist/memory/boundary-work.d.ts +17 -0
  145. package/dist/memory/boundary-work.js +162 -0
  146. package/dist/memory/commands.d.ts +4 -3
  147. package/dist/memory/commands.js +16 -16
  148. package/dist/memory/desk-memory-queue.d.ts +18 -0
  149. package/dist/memory/desk-memory-queue.js +33 -0
  150. package/dist/memory/index-budget.d.ts +3 -3
  151. package/dist/memory/index-budget.js +3 -3
  152. package/dist/memory/journal.d.ts +6 -6
  153. package/dist/memory/journal.js +14 -14
  154. package/dist/memory/lifecycle.d.ts +14 -21
  155. package/dist/memory/lifecycle.js +73 -103
  156. package/dist/memory/maintenance-state.d.ts +14 -15
  157. package/dist/memory/maintenance-state.js +43 -42
  158. package/dist/memory/reflect-job.d.ts +5 -4
  159. package/dist/memory/reflect-job.js +21 -15
  160. package/dist/memory/reflect.d.ts +2 -1
  161. package/dist/memory/reflect.js +13 -12
  162. package/dist/memory/render.d.ts +1 -1
  163. package/dist/memory/render.js +2 -2
  164. package/dist/memory/review-log.d.ts +4 -4
  165. package/dist/memory/review-log.js +5 -5
  166. package/dist/memory/scheduler.d.ts +12 -19
  167. package/dist/memory/scheduler.js +28 -64
  168. package/dist/memory/session-corpus.d.ts +4 -1
  169. package/dist/memory/session-corpus.js +70 -29
  170. package/dist/memory/session-search.d.ts +11 -3
  171. package/dist/memory/session-search.js +14 -11
  172. package/dist/memory/sidecar-worker.d.ts +1 -0
  173. package/dist/memory/sidecar-worker.js +57 -76
  174. package/dist/memory/source-window.d.ts +0 -6
  175. package/dist/memory/source-window.js +0 -20
  176. package/dist/memory/store.d.ts +12 -12
  177. package/dist/memory/store.js +44 -44
  178. package/dist/paths.d.ts +2 -0
  179. package/dist/paths.js +2 -0
  180. package/dist/playbooks/agent-delegation.md +16 -16
  181. package/dist/playbooks/background-jobs.md +5 -3
  182. package/dist/playbooks/event-scheduling.md +7 -5
  183. package/dist/playbooks/memory-and-learning.md +16 -12
  184. package/dist/playbooks/outbound-media.md +1 -1
  185. package/dist/playbooks/runtime-orientation.md +35 -23
  186. package/dist/playbooks/task-lead.md +13 -11
  187. package/dist/runtime/bootstrap.d.ts +36 -13
  188. package/dist/runtime/bootstrap.js +310 -138
  189. package/dist/runtime/channel-queue.d.ts +23 -0
  190. package/dist/{dingtalk → runtime}/channel-queue.js +20 -9
  191. package/dist/runtime/daemon-lock.d.ts +12 -0
  192. package/dist/runtime/daemon-lock.js +28 -0
  193. package/dist/runtime/desk-runtime.d.ts +131 -0
  194. package/dist/runtime/desk-runtime.js +402 -0
  195. package/dist/runtime/durable-dispatch.d.ts +11 -5
  196. package/dist/runtime/durable-dispatch.js +20 -8
  197. package/dist/runtime/inbound-images.d.ts +32 -10
  198. package/dist/runtime/inbound-images.js +95 -21
  199. package/dist/runtime/ingress-busy.d.ts +26 -0
  200. package/dist/runtime/ingress-busy.js +211 -0
  201. package/dist/runtime/ingress-commands.d.ts +7 -0
  202. package/dist/runtime/ingress-commands.js +162 -0
  203. package/dist/runtime/ingress-deps.d.ts +62 -0
  204. package/dist/runtime/ingress-deps.js +1 -0
  205. package/dist/runtime/ingress.d.ts +22 -0
  206. package/dist/runtime/ingress.js +376 -0
  207. package/dist/runtime/request-execution.d.ts +19 -0
  208. package/dist/runtime/request-execution.js +88 -0
  209. package/dist/runtime/shutdown.d.ts +13 -5
  210. package/dist/runtime/shutdown.js +10 -4
  211. package/dist/runtime/task-driver.d.ts +38 -22
  212. package/dist/runtime/task-driver.js +142 -116
  213. package/dist/runtime/task-spawn.d.ts +4 -4
  214. package/dist/runtime/task-spawn.js +6 -5
  215. package/dist/runtime/task-wake.d.ts +6 -4
  216. package/dist/runtime/task-wake.js +7 -7
  217. package/dist/runtime/transports.d.ts +32 -0
  218. package/dist/runtime/transports.js +101 -0
  219. package/dist/runtime/turn-run.d.ts +18 -9
  220. package/dist/runtime/turn-run.js +302 -71
  221. package/dist/runtime/wake-coalescing.d.ts +11 -3
  222. package/dist/runtime/wake-coalescing.js +10 -0
  223. package/dist/security/config.js +5 -10
  224. package/dist/security/logger.js +1 -1
  225. package/dist/security/path-guard.js +7 -63
  226. package/dist/security/types.d.ts +27 -30
  227. package/dist/security/types.js +0 -1
  228. package/dist/shared/atomic-file.d.ts +2 -0
  229. package/dist/shared/atomic-file.js +14 -1
  230. package/dist/shared/file-lock.d.ts +35 -0
  231. package/dist/shared/file-lock.js +195 -0
  232. package/dist/shared/with-timeout.d.ts +2 -0
  233. package/dist/shared/with-timeout.js +21 -0
  234. package/dist/skills/loader.d.ts +1 -1
  235. package/dist/skills/loader.js +2 -2
  236. package/dist/skills/workspace.js +2 -2
  237. package/dist/subagents/completion-wake.d.ts +11 -2
  238. package/dist/subagents/completion-wake.js +22 -9
  239. package/dist/subagents/discovery.js +1 -1
  240. package/dist/subagents/dispatch.d.ts +0 -10
  241. package/dist/subagents/dispatch.js +10 -28
  242. package/dist/subagents/external/dispatch.js +1 -1
  243. package/dist/subagents/external/run.d.ts +1 -1
  244. package/dist/subagents/external/run.js +3 -3
  245. package/dist/subagents/internal/run.js +1 -1
  246. package/dist/subagents/internal/worker.js +6 -14
  247. package/dist/subagents/run-archive.d.ts +11 -0
  248. package/dist/subagents/run-archive.js +49 -0
  249. package/dist/subagents/run-record.d.ts +9 -8
  250. package/dist/subagents/run-record.js +3 -3
  251. package/dist/subagents/run-registry.d.ts +5 -3
  252. package/dist/subagents/run-registry.js +22 -45
  253. package/dist/subagents/runs.d.ts +23 -13
  254. package/dist/subagents/runs.js +75 -82
  255. package/dist/subagents/task-envelope.js +9 -11
  256. package/dist/subagents/types.d.ts +6 -5
  257. package/dist/tasks/board.d.ts +1 -1
  258. package/dist/tasks/board.js +2 -2
  259. package/dist/tasks/bound-work.d.ts +2 -2
  260. package/dist/tasks/bound-work.js +7 -7
  261. package/dist/tasks/brief.d.ts +1 -1
  262. package/dist/tasks/brief.js +9 -9
  263. package/dist/tasks/commands.d.ts +11 -2
  264. package/dist/tasks/commands.js +66 -41
  265. package/dist/tasks/contract.d.ts +0 -2
  266. package/dist/tasks/contract.js +1 -3
  267. package/dist/tasks/create.d.ts +3 -1
  268. package/dist/tasks/create.js +10 -4
  269. package/dist/tasks/frontmatter.d.ts +6 -2
  270. package/dist/tasks/frontmatter.js +10 -10
  271. package/dist/tasks/loop-log.d.ts +7 -13
  272. package/dist/tasks/loop-log.js +14 -17
  273. package/dist/tasks/mutation-lock.d.ts +7 -5
  274. package/dist/tasks/mutation-lock.js +32 -9
  275. package/dist/tasks/session-path.d.ts +3 -3
  276. package/dist/tasks/session-path.js +5 -5
  277. package/dist/tasks/steer.d.ts +5 -5
  278. package/dist/tasks/steer.js +42 -40
  279. package/dist/tasks/store.d.ts +11 -11
  280. package/dist/tasks/store.js +32 -29
  281. package/dist/tools/bash.d.ts +5 -5
  282. package/dist/tools/bash.js +19 -18
  283. package/dist/tools/contract/names.d.ts +7 -0
  284. package/dist/tools/contract/names.js +22 -0
  285. package/dist/tools/edit.js +1 -1
  286. package/dist/tools/event-manage.d.ts +3 -1
  287. package/dist/tools/event-manage.js +34 -33
  288. package/dist/tools/glob.js +5 -5
  289. package/dist/tools/grep.js +2 -2
  290. package/dist/tools/index.d.ts +9 -8
  291. package/dist/tools/index.js +12 -16
  292. package/dist/tools/job.d.ts +4 -2
  293. package/dist/tools/job.js +11 -8
  294. package/dist/tools/memory-manage.d.ts +5 -3
  295. package/dist/tools/memory-manage.js +24 -24
  296. package/dist/tools/read.js +1 -1
  297. package/dist/tools/registry.d.ts +12 -8
  298. package/dist/tools/registry.js +13 -9
  299. package/dist/tools/send-media.d.ts +2 -2
  300. package/dist/tools/send-media.js +3 -3
  301. package/dist/tools/session-search.d.ts +3 -1
  302. package/dist/tools/session-search.js +8 -6
  303. package/dist/tools/subagent-manage.d.ts +2 -8
  304. package/dist/tools/subagent-manage.js +19 -15
  305. package/dist/tools/subagent.js +1 -1
  306. package/dist/tools/task-manage/create.js +1 -1
  307. package/dist/tools/task-manage/factory.js +2 -2
  308. package/dist/tools/task-manage/lifecycle.js +3 -1
  309. package/dist/tools/task-manage/log-view.js +2 -2
  310. package/dist/tools/task-manage/shared.js +3 -3
  311. package/dist/tools/task-manage/step-end.js +3 -3
  312. package/dist/tools/task-manage/types.d.ts +8 -3
  313. package/dist/tools/web-cache.d.ts +3 -3
  314. package/dist/tools/web-cache.js +12 -12
  315. package/dist/tools/web-fetch.d.ts +2 -2
  316. package/dist/tools/web-fetch.js +7 -7
  317. package/dist/tools/write-content.js +1 -1
  318. package/dist/tui/cli.d.ts +17 -5
  319. package/dist/tui/cli.js +90 -27
  320. package/dist/tui/client.d.ts +26 -0
  321. package/dist/tui/client.js +157 -0
  322. package/dist/tui/colors.d.ts +0 -1
  323. package/dist/tui/colors.js +0 -1
  324. package/dist/tui/commands.d.ts +10 -51
  325. package/dist/tui/commands.js +16 -39
  326. package/dist/tui/frontend.d.ts +2 -2
  327. package/dist/tui/frontend.js +2 -2
  328. package/dist/tui/pitui-frontend.d.ts +0 -2
  329. package/dist/tui/pitui-frontend.js +1 -6
  330. package/dist/tui/render.d.ts +62 -0
  331. package/dist/tui/render.js +123 -0
  332. package/dist/tui/session.d.ts +92 -0
  333. package/dist/tui/session.js +317 -0
  334. package/dist/usage/ledger.d.ts +5 -0
  335. package/dist/usage/ledger.js +2 -0
  336. package/dist/usage/render.d.ts +6 -1
  337. package/dist/usage/render.js +18 -13
  338. package/dist/webapi/auth.d.ts +13 -0
  339. package/dist/webapi/auth.js +29 -0
  340. package/dist/webapi/config.d.ts +29 -0
  341. package/dist/webapi/config.js +100 -0
  342. package/dist/webapi/errors.d.ts +39 -0
  343. package/dist/webapi/errors.js +32 -0
  344. package/dist/webapi/http.d.ts +25 -0
  345. package/dist/webapi/http.js +4 -0
  346. package/dist/webapi/ids.d.ts +9 -0
  347. package/dist/webapi/ids.js +20 -0
  348. package/dist/webapi/input.d.ts +17 -0
  349. package/dist/webapi/input.js +229 -0
  350. package/dist/webapi/items.d.ts +37 -0
  351. package/dist/webapi/items.js +48 -0
  352. package/dist/webapi/key-command.d.ts +2 -0
  353. package/dist/webapi/key-command.js +110 -0
  354. package/dist/webapi/limits.d.ts +47 -0
  355. package/dist/webapi/limits.js +48 -0
  356. package/dist/webapi/request-policy.d.ts +17 -0
  357. package/dist/webapi/request-policy.js +160 -0
  358. package/dist/webapi/response-record.d.ts +68 -0
  359. package/dist/webapi/response-record.js +58 -0
  360. package/dist/webapi/response-state.d.ts +90 -0
  361. package/dist/webapi/response-state.js +347 -0
  362. package/dist/webapi/routes/conversations.d.ts +8 -0
  363. package/dist/webapi/routes/conversations.js +80 -0
  364. package/dist/webapi/routes/models.d.ts +3 -0
  365. package/dist/webapi/routes/models.js +12 -0
  366. package/dist/webapi/routes/responses.d.ts +8 -0
  367. package/dist/webapi/routes/responses.js +118 -0
  368. package/dist/webapi/routes/websocket.d.ts +9 -0
  369. package/dist/webapi/routes/websocket.js +328 -0
  370. package/dist/webapi/server.d.ts +24 -0
  371. package/dist/webapi/server.js +229 -0
  372. package/dist/webapi/service.d.ts +130 -0
  373. package/dist/webapi/service.js +588 -0
  374. package/dist/webapi/store.d.ts +46 -0
  375. package/dist/webapi/store.js +245 -0
  376. package/dist/webapi/stream.d.ts +13 -0
  377. package/dist/webapi/stream.js +76 -0
  378. package/dist/webapi/transport.d.ts +43 -0
  379. package/dist/webapi/transport.js +100 -0
  380. package/package.json +9 -2
  381. package/dist/channel/channel-paths.d.ts +0 -16
  382. package/dist/channel/channel-paths.js +0 -53
  383. package/dist/channel/commands.d.ts +0 -32
  384. package/dist/channel/commands.js +0 -109
  385. package/dist/channel/project-scope-store.d.ts +0 -51
  386. package/dist/channel/project-scope-store.js +0 -112
  387. package/dist/dingtalk/channel-queue.d.ts +0 -19
  388. package/dist/dingtalk/handler.d.ts +0 -90
  389. package/dist/memory/channel-maintenance-queue.d.ts +0 -5
  390. package/dist/memory/channel-maintenance-queue.js +0 -8
  391. package/dist/runtime/task-migration.d.ts +0 -30
  392. package/dist/runtime/task-migration.js +0 -465
  393. package/dist/runtime/turn-handler.d.ts +0 -47
  394. package/dist/runtime/turn-handler.js +0 -318
  395. package/dist/security/project-scope.d.ts +0 -45
  396. package/dist/security/project-scope.js +0 -105
  397. package/dist/tui/app.d.ts +0 -16
  398. package/dist/tui/app.js +0 -166
  399. package/dist/tui/terminal-context.d.ts +0 -27
  400. package/dist/tui/terminal-context.js +0 -84
  401. package/dist/tui/terminal-media-sender.d.ts +0 -9
  402. package/dist/tui/terminal-media-sender.js +0 -15
  403. package/dist/tui/turn-controller.d.ts +0 -64
  404. package/dist/tui/turn-controller.js +0 -311
  405. /package/dist/{dingtalk/handler.js → channel/transport.js} +0 -0
@@ -1,7 +1,5 @@
1
1
  import { createHash } from "node:crypto";
2
2
  import { join } from "node:path";
3
- import { discoverWorkspaceChannelIds } from "../channel/channel-index.js";
4
- import { dedupeChannelIdsByDirectory, getChannelDir, isChannelId } from "../channel/channel-paths.js";
5
3
  import { TASK_DRIVER_SETTINGS } from "../config/settings.js";
6
4
  import * as log from "../log.js";
7
5
  import { PLAYBOOKS_DIR } from "../paths.js";
@@ -17,37 +15,22 @@ const NUDGE_DEBOUNCE_MS = 50;
17
15
  const MIN_SLEEP_MS = 250;
18
16
  /** A task dispatched this recently is assumed still in flight; do not queue it twice. */
19
17
  const REDISPATCH_GUARD_MS = 30_000;
20
- /**
21
- * Channels this driver should scan. Live ids come first so a channel discovered on disk under
22
- * its escaped directory name never displaces the real id the runner map already knows.
23
- */
24
- export async function discoverTaskChannels(workspaceDir, knownChannelIds = [], options = {}) {
25
- return dedupeChannelIdsByDirectory([
26
- ...[...knownChannelIds].filter(isChannelId),
27
- ...(await discoverWorkspaceChannelIds(workspaceDir, options)),
28
- ]);
29
- }
30
- function attemptKey(channelId, taskId) {
31
- return `${channelId}\0${taskId}`;
32
- }
33
- function channelType(channelId) {
34
- return channelId.startsWith("group_")
35
- ? { type: "group", conversationType: "2" }
36
- : { type: "dm", conversationType: "1" };
18
+ function attemptKey(desk, taskId) {
19
+ return `${desk}\0${taskId}`;
37
20
  }
38
21
  /**
39
22
  * Stable, provenance-derived dispatch id for a task step (spec 031, D1; spec 051 keeps the shape).
40
23
  * Each step is genuinely new work, so the dispatch moment disambiguates it; collapsing two steps
41
24
  * onto one durable record would silently drop the later one.
42
25
  */
43
- function stepDispatchId(channelId, entry, nowMs) {
44
- return `task:${channelId}:${entry.id}:${nowMs}`;
26
+ function stepDispatchId(desk, entry, nowMs) {
27
+ return `task:${desk}:${entry.id}:${nowMs}`;
45
28
  }
46
29
  /** The task a `[TASK_DRIVER:<id>]` dispatch text names, if it is one. */
47
30
  export function parseDriverTaskId(text) {
48
31
  return /^\[TASK_DRIVER:([A-Za-z0-9._-]+)\]/.exec(text ?? "")?.[1];
49
32
  }
50
- export function createTaskDriverEvent(channelId, entry, nowMs) {
33
+ export function createTaskDriverEvent(desk, channelId, entry, nowMs) {
51
34
  const repairOnly = !entry.readable;
52
35
  const repair = repairOnly
53
36
  ? ` Task metadata is not readable; repair only the frontmatter in tasks/${entry.id}.md, then stop. ` +
@@ -63,10 +46,9 @@ export function createTaskDriverEvent(channelId, entry, nowMs) {
63
46
  .filter(Boolean)
64
47
  .join(" ");
65
48
  return {
66
- ...channelType(channelId),
49
+ source: "wake",
67
50
  channelId,
68
- user: "TASK_DRIVER",
69
- userName: "TASK_DRIVER",
51
+ producer: "TASK_DRIVER",
70
52
  text: `[TASK_DRIVER:${entry.id}] Resume task ${entry.id}. ${capsule}${repair} ` +
71
53
  `Open tasks/${entry.id}.md and read ${join(PLAYBOOKS_DIR, "task-lead.md")} before acting. ` +
72
54
  (repairOnly
@@ -74,7 +56,8 @@ export function createTaskDriverEvent(channelId, entry, nowMs) {
74
56
  : "Advance the next concrete step under the task's current goal, work items and acceptance state. ") +
75
57
  "End the step with task_step_end (continue / park / done).",
76
58
  ts: String(nowMs),
77
- dispatchId: stepDispatchId(channelId, entry, nowMs),
59
+ dispatchId: stepDispatchId(desk, entry, nowMs),
60
+ desk,
78
61
  };
79
62
  }
80
63
  /**
@@ -83,15 +66,14 @@ export function createTaskDriverEvent(channelId, entry, nowMs) {
83
66
  * call to phrase that is exactly the kind of cost this design removes. A budget stop also says
84
67
  * how to resume with more of the dimension that ran out (spec 053, D6).
85
68
  */
86
- export function taskStopReceipt(channelId, entry, reason, nowMs, breach) {
69
+ export function taskStopReceipt(desk, channelId, entry, reason, nowMs, breach) {
87
70
  const usage = entry.fields.usage ? ` · 已用 ${entry.fields.usage.steps} 步` : "";
88
71
  const grant = breach === "steps" ? " +steps 10" : breach === "usd" ? " +usd 5" : "";
89
72
  const resumeLabel = grant ? "继续并追加预算" : "继续";
90
73
  return {
91
- ...channelType(channelId),
74
+ source: "wake",
92
75
  channelId,
93
- user: "TASK_DRIVER",
94
- userName: "TASK_DRIVER",
76
+ producer: "TASK_DRIVER",
95
77
  text: `🛑 任务 \`${entry.id}\`(${entry.title})已停止自动执行:${reason}。\n` +
96
78
  `状态:${taskStateLabel(entry.fields.state)}${usage}\n` +
97
79
  `查看:\`/tasks show ${entry.id}\`;${resumeLabel}:\`/tasks resume ${entry.id}${grant}\`\n` +
@@ -99,13 +81,14 @@ export function taskStopReceipt(channelId, entry, reason, nowMs, breach) {
99
81
  ts: String(nowMs),
100
82
  // Keyed on the cause, not the moment: re-detecting the same stop before the user has acted
101
83
  // must not queue a second identical receipt, while a different cause still does.
102
- dispatchId: `task:${channelId}:${entry.id}:stop:${createHash("sha256").update(reason).digest("hex").slice(0, 12)}`,
84
+ dispatchId: `task:${desk}:${entry.id}:stop:${createHash("sha256").update(reason).digest("hex").slice(0, 12)}`,
85
+ desk,
103
86
  };
104
87
  }
105
88
  /**
106
89
  * Native, ticket-driven scheduler for the persistent task ledger (spec 051, D9; spec 052).
107
90
  *
108
- * Three jobs, in this order per channel: redeem due `time` tickets, apply the backstop to expired
91
+ * Three jobs, in this order per desk: redeem due `time` tickets, apply the backstop to expired
109
92
  * ones, and queue one step for a runnable task. `work` and `ask` tickets are never polled — their
110
93
  * owners push them (a settling delegation or job, `/tasks reply`) — so the scan stays a cheap read
111
94
  * of a handful of frontmatters. Recurring work is not the driver's business: an event spawns a
@@ -125,15 +108,16 @@ export class TaskDriver {
125
108
  this.nudgeTimer = null;
126
109
  this.loopActive = false;
127
110
  this.running = false;
128
- this.nextChannelIndex = 0;
111
+ this.nextDeskIndex = 0;
129
112
  /** Last dispatch time per task, so one in-flight step is not queued again on the next tick. */
130
113
  this.lastDispatchMs = new Map();
131
114
  /**
132
- * Last task id dispatched per channel. Starting the search just after it gives every ready
133
- * task in a channel the same round-robin fairness an actively-progressing task would
134
- * otherwise monopolise.
115
+ * Last task id dispatched per desk. Starting the search just after it gives every ready task in
116
+ * a desk the same round-robin fairness an actively-progressing task would otherwise monopolise.
135
117
  */
136
118
  this.lastDispatchedTaskId = new Map();
119
+ /** Tasks currently suspended (D8), so the transition is logged once rather than every scan. */
120
+ this.suspended = new Set();
137
121
  }
138
122
  observeDispatch(event, accepted) {
139
123
  try {
@@ -213,9 +197,9 @@ export class TaskDriver {
213
197
  this.nextWakeMs = candidate;
214
198
  }
215
199
  /** Stop one task and notify. Delivery failure is logged, never fatal: the durable stop already happened. */
216
- async notifyStop(channelId, entry, reason, nowMs, breach) {
200
+ async notifyStop(desk, channelId, entry, reason, nowMs, breach) {
217
201
  try {
218
- await this.options.notify?.(taskStopReceipt(channelId, entry, reason, nowMs, breach));
202
+ await this.options.notify?.(taskStopReceipt(desk, channelId, entry, reason, nowMs, breach));
219
203
  }
220
204
  catch (error) {
221
205
  log.logWarn("taskdriver.receipt.failed", "Task stop receipt failed", {
@@ -225,14 +209,14 @@ export class TaskDriver {
225
209
  }
226
210
  }
227
211
  /**
228
- * Deterministic, zero-token maintenance for one channel: redeem due tickets and apply expiry
229
- * backstops. Runs whether or not the channel is busy — only model
230
- * dispatch is gated on an idle channel.
212
+ * Deterministic, zero-token maintenance for one desk's live tasks: redeem due tickets and apply
213
+ * expiry backstops. Runs whether or not the delivery channel is busy — only model dispatch is
214
+ * gated on an idle channel.
231
215
  */
232
- async maintain(channelDir, channelId, entries, now) {
216
+ async maintain(desk, deskDir, entries, now) {
233
217
  const nowMs = now.getTime();
234
218
  let changed = false;
235
- for (const entry of entries) {
219
+ for (const { entry, channelId } of entries) {
236
220
  if (entry.fields.paused || entry.fields.state !== "parked")
237
221
  continue;
238
222
  const ticket = entry.fields.ticket;
@@ -240,12 +224,12 @@ export class TaskDriver {
240
224
  continue;
241
225
  // A due timer redeems into an open task.
242
226
  if (entry.dueMs !== undefined && entry.dueMs <= nowMs) {
243
- if (await redeemTicket(channelDir, entry.id, (current) => current.kind === ticket.kind))
227
+ if (await redeemTicket(deskDir, entry.id, (current) => current.kind === ticket.kind))
244
228
  changed = true;
245
229
  continue;
246
230
  }
247
231
  if (entry.expired) {
248
- const expiry = await expireTicket(channelDir, entry.id);
232
+ const expiry = await expireTicket(deskDir, entry.id);
249
233
  if (!expiry)
250
234
  continue;
251
235
  changed = true;
@@ -253,15 +237,15 @@ export class TaskDriver {
253
237
  log.logWarn("taskdriver.task.paused", "Task paused: ticket expired twice", {
254
238
  channelId: channelId,
255
239
  reason: expiry.ticket,
256
- fields: { taskId: entry.id, cause: "ticket_expired" },
240
+ fields: { taskId: entry.id, desk, cause: "ticket_expired" },
257
241
  });
258
- await this.notifyStop(channelId, entry, `等待条件一直未满足(${expiry.ticket}),已连续两次超过最晚检查时间`, nowMs);
242
+ await this.notifyStop(desk, channelId, entry, `等待条件一直未满足(${expiry.ticket}),已连续两次超过最晚检查时间`, nowMs);
259
243
  }
260
244
  else {
261
245
  log.logInfoEvent("taskdriver.task.reopened", "Task reopened after its ticket expired", {
262
246
  channelId: channelId,
263
247
  reason: expiry.ticket,
264
- fields: { taskId: entry.id },
248
+ fields: { taskId: entry.id, desk },
265
249
  });
266
250
  }
267
251
  continue;
@@ -277,31 +261,70 @@ export class TaskDriver {
277
261
  * each step actually did, so "no progress" is an observed fact rather than a fingerprint
278
262
  * comparison a `echo x` could defeat.
279
263
  */
280
- async detectIdleLoop(channelDir, channelId, entries, nowMs) {
264
+ async detectIdleLoop(desk, deskDir, entries, nowMs) {
281
265
  let paused = false;
282
- for (const entry of entries) {
266
+ for (const { entry, channelId } of entries) {
283
267
  const usage = entry.fields.usage;
284
268
  if (!usage || usage.steps < IDLE_STEP_LIMIT)
285
269
  continue;
286
- const steps = (await readTaskLog(channelDir, entry.id, { kinds: ["step"] })).slice(-IDLE_STEP_LIMIT);
270
+ const steps = (await readTaskLog(deskDir, entry.id, { kinds: ["step"] })).slice(-IDLE_STEP_LIMIT);
287
271
  if (steps.length < IDLE_STEP_LIMIT)
288
272
  continue;
289
273
  const idle = steps.every((record) => record.kind === "step" && record.tools.length === 0);
290
274
  if (!idle)
291
275
  continue;
292
276
  const reason = `连续 ${IDLE_STEP_LIMIT} 步没有任何工具调用,已停下等你确认`;
293
- if (await pauseTask(channelDir, entry.id, { by: "runtime", reason })) {
277
+ if (await pauseTask(deskDir, entry.id, { by: "runtime", reason })) {
294
278
  paused = true;
295
279
  log.logWarn("taskdriver.task.paused", "Task paused: idle loop", {
296
280
  channelId: channelId,
297
281
  reason: reason,
298
- fields: { taskId: entry.id, cause: "idle" },
282
+ fields: { taskId: entry.id, desk, cause: "idle" },
299
283
  });
300
- await this.notifyStop(channelId, entry, reason, nowMs);
284
+ await this.notifyStop(desk, channelId, entry, reason, nowMs);
301
285
  }
302
286
  }
303
287
  return paused;
304
288
  }
289
+ /**
290
+ * The tasks of one desk the driver may touch at all (spec 058, D8): those whose delivery channel
291
+ * is bound to this desk and reachable. Every other task — no `channel`, or a channel bound elsewhere or nowhere, or on a transport that is off
292
+ * — is frozen: no ticket redemption, no backstop, no budget or idle check, no step, no receipt.
293
+ * Freezing reads nothing back into the file, so a rebound task resumes from exactly where it was.
294
+ */
295
+ liveTasks(desk, entries) {
296
+ const live = [];
297
+ for (const entry of entries) {
298
+ const key = attemptKey(desk, entry.id);
299
+ const channelId = entry.fields.channel;
300
+ if (channelId && this.options.isBoundTo(channelId, desk) && this.options.isReachable(channelId)) {
301
+ if (this.suspended.delete(key)) {
302
+ log.logInfoEvent("taskdriver.task.resumed", "Task resumed: delivery channel bound again", {
303
+ channelId,
304
+ fields: { taskId: entry.id, desk },
305
+ });
306
+ }
307
+ live.push({ entry, channelId });
308
+ continue;
309
+ }
310
+ if (!this.suspended.has(key)) {
311
+ this.suspended.add(key);
312
+ log.logInfoEvent("taskdriver.task.suspended", "Task suspended: delivery channel not usable", {
313
+ channelId,
314
+ fields: {
315
+ taskId: entry.id,
316
+ desk,
317
+ reason: !channelId
318
+ ? "channel_missing"
319
+ : this.options.isBoundTo(channelId, desk)
320
+ ? "channel_unreachable"
321
+ : "channel_not_bound",
322
+ },
323
+ });
324
+ }
325
+ }
326
+ return live;
327
+ }
305
328
  async runOnce(now = new Date()) {
306
329
  if (this.options.isEnabled?.() === false || this.running)
307
330
  return;
@@ -310,105 +333,108 @@ export class TaskDriver {
310
333
  this.running = true;
311
334
  this.nextWakeMs = undefined;
312
335
  try {
313
- const channels = await discoverTaskChannels(this.options.workspaceDir, this.options.getKnownChannelIds?.());
314
- if (channels.length === 0) {
336
+ const desks = this.options.getActiveDesks();
337
+ if (desks.length === 0) {
315
338
  this.lastDispatchMs.clear();
316
339
  return;
317
340
  }
318
341
  const seen = new Set();
319
- const start = this.nextChannelIndex % channels.length;
342
+ const start = this.nextDeskIndex % desks.length;
320
343
  let dispatched = 0;
321
344
  let lastDispatchOffset = -1;
322
- for (let offset = 0; offset < channels.length; offset++) {
323
- const channelId = channels[(start + offset) % channels.length];
324
- if (!channelId)
345
+ for (let offset = 0; offset < desks.length; offset++) {
346
+ const target = desks[(start + offset) % desks.length];
347
+ if (!target)
325
348
  continue;
326
- // `channelId` is the raw id, which for a DingTalk group routinely contains `/`;
327
- // only `getChannelDir` folds it into the escaped directory name that exists on disk.
328
- const channelDir = getChannelDir(this.options.workspaceDir, channelId);
329
- let entries = await readActiveTasks(join(channelDir, "tasks"), nowMs);
330
- for (const entry of entries)
331
- seen.add(attemptKey(channelId, entry.id));
332
- if (await this.maintain(channelDir, channelId, entries, now)) {
333
- entries = await readActiveTasks(join(channelDir, "tasks"), nowMs);
334
- }
335
- if (dispatched >= settings.maxDispatchesPerTick || this.options.isChannelActive(channelId))
349
+ const { desk, deskDir } = target;
350
+ const tasksDir = join(deskDir, "tasks");
351
+ const readLive = async () => this.liveTasks(desk, await readActiveTasks(tasksDir, nowMs));
352
+ let live = await readLive();
353
+ for (const { entry } of live)
354
+ seen.add(attemptKey(desk, entry.id));
355
+ if (await this.maintain(desk, deskDir, live, now))
356
+ live = await readLive();
357
+ if (dispatched >= settings.maxDispatchesPerTick)
336
358
  continue;
337
359
  // A task that has already run out of budget must not spend one more model call to
338
360
  // discover that (D6). Checked before dispatch, and paired with a receipt so the user
339
361
  // learns why it stopped and what to type to give it more rope.
340
362
  const budgeted = [];
341
- for (const entry of entries) {
363
+ for (const task of live) {
364
+ const { entry, channelId } = task;
342
365
  if (!entry.runnable)
343
366
  continue;
344
367
  const status = checkBudget(entry.fields);
345
368
  if (!status.breach || !status.reason) {
346
- budgeted.push(entry);
369
+ budgeted.push(task);
347
370
  continue;
348
371
  }
349
- if (await pauseTask(channelDir, entry.id, { by: "runtime", reason: status.reason })) {
372
+ if (await pauseTask(deskDir, entry.id, { by: "runtime", reason: status.reason })) {
350
373
  log.logWarn("taskdriver.task.paused", "Task paused: budget exhausted", {
351
374
  channelId: channelId,
352
375
  reason: status.reason,
353
- fields: { taskId: entry.id, cause: `budget_${status.breach}` },
376
+ fields: { taskId: entry.id, desk, cause: `budget_${status.breach}` },
354
377
  });
355
- await this.notifyStop(channelId, entry, status.reason, nowMs, status.breach);
378
+ await this.notifyStop(desk, channelId, entry, status.reason, nowMs, status.breach);
356
379
  }
357
380
  }
358
- const idled = await this.detectIdleLoop(channelDir, channelId, budgeted, nowMs);
359
- const ready = idled
360
- ? (await readActiveTasks(join(channelDir, "tasks"), nowMs)).filter((entry) => entry.runnable)
361
- : budgeted;
362
- const candidates = ready.filter((entry) => !entry.fields.paused);
381
+ const idled = await this.detectIdleLoop(desk, deskDir, budgeted, nowMs);
382
+ const ready = idled ? (await readLive()).filter(({ entry }) => entry.runnable) : budgeted;
383
+ // The busy check is per task: a desk's tasks may deliver to different channels.
384
+ const candidates = ready.filter(({ entry, channelId }) => !entry.fields.paused && !this.options.isChannelActive(channelId));
363
385
  if (candidates.length === 0)
364
386
  continue;
365
- const lastId = this.lastDispatchedTaskId.get(channelId);
366
- const lastIndex = lastId ? candidates.findIndex((candidate) => candidate.id === lastId) : -1;
387
+ const lastId = this.lastDispatchedTaskId.get(desk);
388
+ const lastIndex = lastId ? candidates.findIndex(({ entry }) => entry.id === lastId) : -1;
367
389
  const rotated = lastIndex >= 0
368
390
  ? [...candidates.slice(lastIndex + 1), ...candidates.slice(0, lastIndex + 1)]
369
391
  : candidates;
370
- const entry = rotated.find((candidate) => {
371
- const previous = this.lastDispatchMs.get(attemptKey(channelId, candidate.id));
372
- return previous === undefined || nowMs - previous >= REDISPATCH_GUARD_MS;
373
- });
374
- if (!entry) {
375
- for (const candidate of rotated) {
376
- const previous = this.lastDispatchMs.get(attemptKey(channelId, candidate.id));
377
- if (previous !== undefined)
378
- this.noteHorizon(previous + REDISPATCH_GUARD_MS, nowMs);
392
+ // One step per delivery channel per pass: a desk's tasks may deliver to different channels,
393
+ // whose queues run independently, so one channel's step must not hold back another's.
394
+ // Tasks sharing a channel still take turns (D6 rotation).
395
+ const usedChannels = new Set();
396
+ for (const { entry, channelId } of rotated) {
397
+ if (dispatched >= settings.maxDispatchesPerTick)
398
+ break;
399
+ if (usedChannels.has(channelId))
400
+ continue;
401
+ const previous = this.lastDispatchMs.get(attemptKey(desk, entry.id));
402
+ if (previous !== undefined && nowMs - previous < REDISPATCH_GUARD_MS) {
403
+ this.noteHorizon(previous + REDISPATCH_GUARD_MS, nowMs);
404
+ continue;
405
+ }
406
+ usedChannels.add(channelId);
407
+ const event = createTaskDriverEvent(desk, channelId, entry, nowMs);
408
+ const accepted = await this.options.dispatch(event);
409
+ this.observeDispatch(event, accepted);
410
+ this.lastDispatchMs.set(attemptKey(desk, entry.id), nowMs);
411
+ this.lastDispatchedTaskId.set(desk, entry.id);
412
+ if (accepted) {
413
+ dispatched++;
414
+ lastDispatchOffset = offset;
415
+ log.logDebugEvent("taskdriver.step.enqueued", "Task step enqueued", {
416
+ channelId: channelId,
417
+ fields: { taskId: entry.id, desk },
418
+ });
419
+ }
420
+ else {
421
+ log.logWarn("taskdriver.step.enqueue_failed", "Task step not enqueued: channel queue unavailable", {
422
+ channelId: channelId,
423
+ fields: { taskId: entry.id, desk },
424
+ });
379
425
  }
380
- continue;
381
- }
382
- const event = createTaskDriverEvent(channelId, entry, nowMs);
383
- const accepted = await this.options.dispatch(event);
384
- this.observeDispatch(event, accepted);
385
- this.lastDispatchMs.set(attemptKey(channelId, entry.id), nowMs);
386
- this.lastDispatchedTaskId.set(channelId, entry.id);
387
- if (accepted) {
388
- dispatched++;
389
- lastDispatchOffset = offset;
390
- log.logDebugEvent("taskdriver.step.enqueued", "Task step enqueued", {
391
- channelId: channelId,
392
- fields: { taskId: entry.id },
393
- });
394
- }
395
- else {
396
- log.logWarn("taskdriver.step.enqueue_failed", "Task step not enqueued: channel queue unavailable", {
397
- channelId: channelId,
398
- fields: { taskId: entry.id },
399
- });
400
426
  }
401
427
  }
402
428
  for (const key of this.lastDispatchMs.keys()) {
403
429
  if (!seen.has(key))
404
430
  this.lastDispatchMs.delete(key);
405
431
  }
406
- const channelSet = new Set(channels);
407
- for (const channelId of this.lastDispatchedTaskId.keys()) {
408
- if (!channelSet.has(channelId))
409
- this.lastDispatchedTaskId.delete(channelId);
432
+ const deskSet = new Set(desks.map((target) => target.desk));
433
+ for (const desk of this.lastDispatchedTaskId.keys()) {
434
+ if (!deskSet.has(desk))
435
+ this.lastDispatchedTaskId.delete(desk);
410
436
  }
411
- this.nextChannelIndex = (start + (lastDispatchOffset >= 0 ? lastDispatchOffset + 1 : 1)) % channels.length;
437
+ this.nextDeskIndex = (start + (lastDispatchOffset >= 0 ? lastDispatchOffset + 1 : 1)) % desks.length;
412
438
  }
413
439
  finally {
414
440
  this.running = false;
@@ -1,11 +1,11 @@
1
1
  import type { SpawnTaskRequest, SpawnTaskResult } from "../events/types.js";
2
2
  export interface TaskSpawnerOptions {
3
- /** The channel's directory under the workspace. */
4
- getChannelDir: (channelId: string) => string;
3
+ /** The event's desk directory: the instance is created in its `tasks/`. */
4
+ getDeskDir: (desk: string) => string;
5
5
  /** Wake the driver so the new instance's first step is queued promptly. */
6
6
  nudge: () => void;
7
- /** Direct, non-LLM message to the channel — used for the "previous instance still running" receipt. */
8
- notify: (channelId: string, text: string) => Promise<unknown>;
7
+ /** Direct, non-LLM receipt to the event's channel, through the desk's binding guard (D7). */
8
+ notify: (desk: string, channelId: string, text: string) => Promise<unknown>;
9
9
  }
10
10
  /**
11
11
  * Create the task instance a template event asks for (spec 052, D2).
@@ -20,16 +20,16 @@ import { archivedTaskPath, taskPath, tasksDir } from "../tasks/store.js";
20
20
  */
21
21
  export function createTaskSpawner(options) {
22
22
  return async (request) => {
23
- const channelDir = options.getChannelDir(request.channelId);
23
+ const deskDir = options.getDeskDir(request.desk);
24
24
  const id = `${request.eventName}-${instanceStamp(request.occurrence)}`;
25
- if (existsSync(taskPath(channelDir, id)) || existsSync(archivedTaskPath(channelDir, id))) {
25
+ if (existsSync(taskPath(deskDir, id)) || existsSync(archivedTaskPath(deskDir, id))) {
26
26
  return { outcome: "exists", id };
27
27
  }
28
- const active = (await readActiveTasks(tasksDir(channelDir))).find((entry) => entry.fields.origin === request.eventName && !entry.fields.outcome);
28
+ const active = (await readActiveTasks(tasksDir(deskDir))).find((entry) => entry.fields.origin === request.eventName && !entry.fields.outcome);
29
29
  if (active) {
30
30
  const state = `${taskStateLabel(active.fields.state)}${active.fields.paused ? ",已暂停" : ""}`;
31
31
  await options
32
- .notify(request.channelId, `⚠️ 周期任务 \`${request.eventName}\` 本次(${instanceStamp(request.occurrence)})未启动:上一个实例 \`${active.id}\` 仍未结束(${state})。\n查看:\`/tasks show ${active.id}\``)
32
+ .notify(request.desk, request.channelId, `⚠️ 周期任务 \`${request.eventName}\` 本次(${instanceStamp(request.occurrence)})未启动:上一个实例 \`${active.id}\` 仍未结束(${state})。\n查看:\`/tasks show ${active.id}\``)
33
33
  .catch((error) => {
34
34
  log.logWarn("tasks.spawn.receipt_failed", "Skip receipt delivery failed", {
35
35
  channelId: request.channelId,
@@ -40,9 +40,10 @@ export function createTaskSpawner(options) {
40
40
  return { outcome: "skipped", id: active.id };
41
41
  }
42
42
  await createTaskDocument({
43
- channelDir,
43
+ deskDir,
44
44
  id,
45
45
  input: request.template,
46
+ channel: request.channelId,
46
47
  origin: request.eventName,
47
48
  now: request.occurrence,
48
49
  });
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Claiming a background job's or delegation run's completion wake before it may reopen a parked
3
- * task (spec 040, D7/T9). Pure functions over an explicit `event`/`workspaceDir`/owner, with no
3
+ * task (spec 040, D7/T9). Pure functions over an explicit `event`/`deskDir`/owner, with no
4
4
  * dependency on `createRuntimeContext`'s closures.
5
5
  *
6
6
  * Trust comes from two things only: the producer-created `internalWake` envelope (DingTalk
@@ -9,8 +9,8 @@
9
9
  * settled, names this task, and that the dispatch id is exactly the one its own completion wake
10
10
  * carries.
11
11
  */
12
- import type { ChannelEvent } from "../channel/channel-event.js";
13
- /** The slice of `ChannelJobManager` / `SubAgentRunManager` a wake claim needs. */
12
+ import type { WakeEvent } from "../channel/channel-event.js";
13
+ /** The slice of `DeskJobManager` / `SubAgentRunManager` a wake claim needs. */
14
14
  export interface WakeOwner {
15
15
  beginWakeConsumption(id: string, taskId: string, dispatchId: string): Promise<boolean>;
16
16
  finishWakeConsumption(id: string, dispatchId: string): Promise<void>;
@@ -43,4 +43,6 @@ export interface ClaimedWake {
43
43
  * waiting for it, and reopening it here would be exactly the unverified resumption path spec 051
44
44
  * exists to remove.
45
45
  */
46
- export declare function claimVerifiedWake(event: ChannelEvent, workspaceDir: string, owner: WakeOwner, hooks?: WakeTaskTransitionHooks): Promise<ClaimedWake | undefined>;
46
+ export declare function claimVerifiedWake(event: WakeEvent,
47
+ /** The admitted wake's desk directory — where the task it names lives. */
48
+ deskDir: string, owner: WakeOwner, hooks?: WakeTaskTransitionHooks): Promise<ClaimedWake | undefined>;
@@ -1,7 +1,6 @@
1
- import { getChannelDir } from "../channel/channel-paths.js";
2
1
  import { readStoredTask, redeemTicket } from "../tasks/store.js";
3
- async function isTaskActivelyDriven(channelDir, taskId) {
4
- const document = await readStoredTask(channelDir, taskId).catch(() => undefined);
2
+ async function isTaskActivelyDriven(deskDir, taskId) {
3
+ const document = await readStoredTask(deskDir, taskId).catch(() => undefined);
5
4
  return document !== undefined && document.fields.state === "open" && !document.fields.paused;
6
5
  }
7
6
  /**
@@ -13,20 +12,21 @@ async function isTaskActivelyDriven(channelDir, taskId) {
13
12
  * waiting for it, and reopening it here would be exactly the unverified resumption path spec 051
14
13
  * exists to remove.
15
14
  */
16
- export async function claimVerifiedWake(event, workspaceDir, owner, hooks) {
15
+ export async function claimVerifiedWake(event,
16
+ /** The admitted wake's desk directory — where the task it names lives. */
17
+ deskDir, owner, hooks) {
17
18
  const wake = event.internalWake;
18
19
  if (!wake || typeof event.dispatchId !== "string")
19
20
  return undefined;
20
21
  if (!(await owner.beginWakeConsumption(wake.resourceId, wake.taskId, event.dispatchId)))
21
22
  return undefined;
22
23
  const dispatchId = event.dispatchId;
23
- const channelDir = getChannelDir(workspaceDir, event.channelId);
24
24
  hooks?.beforeActivation?.();
25
- const redeemed = await redeemTicket(channelDir, wake.taskId, (ticket) => ticket.kind === "work");
25
+ const redeemed = await redeemTicket(deskDir, wake.taskId, (ticket) => ticket.kind === "work");
26
26
  return {
27
27
  taskId: wake.taskId,
28
28
  activated: redeemed !== undefined,
29
- taskStillDriven: redeemed !== undefined || (await isTaskActivelyDriven(channelDir, wake.taskId)),
29
+ taskStillDriven: redeemed !== undefined || (await isTaskActivelyDriven(deskDir, wake.taskId)),
30
30
  finish: () => owner.finishWakeConsumption(wake.resourceId, dispatchId),
31
31
  };
32
32
  }
@@ -0,0 +1,32 @@
1
+ import type { BootstrapPaths } from "../app/home.js";
2
+ import type { Ingress } from "../channel/ingress.js";
3
+ import type { Transport } from "../channel/transport.js";
4
+ import type { ConversationKind } from "../desk/types.js";
5
+ import type { SecurityLogEvent } from "../security/types.js";
6
+ export interface TransportRegistry {
7
+ /** The transport that owns `channelId`; `undefined` when its transport is not hosted by this process. */
8
+ forChannel(channelId: string): Transport | undefined;
9
+ /** Whether a message can be delivered to `channelId` right now. */
10
+ isReachable(channelId: string): boolean;
11
+ all(): readonly Transport[];
12
+ conversationKindOf(channelId: string): ConversationKind | undefined;
13
+ /** How a channel is named to a user; an unreachable channel falls back to its id. */
14
+ describeChannel(channelId: string, knownName?: string): string;
15
+ startAll(ingress: Ingress): Promise<void>;
16
+ /** Every transport stops taking messages and keeps delivering (see `Transport.stopAccepting`). */
17
+ stopAcceptingAll(): Promise<void>;
18
+ stopAll(): Promise<void>;
19
+ }
20
+ export declare function createTransportRegistry(transports: readonly Transport[]): TransportRegistry;
21
+ /**
22
+ * Build the transports `channel.json` turns on. A transport's code is imported only when its turn
23
+ * comes, so a disabled transport costs nothing — not its module, not its dependencies, not a
24
+ * listener (INV-7). A configuration that cannot be used, or that turns nothing on, refuses to start.
25
+ */
26
+ export declare function loadTransports(deps: {
27
+ paths: BootstrapPaths;
28
+ stateDir: string;
29
+ archiveBotResponse: (channelId: string, text: string) => Promise<void>;
30
+ /** The security audit log, for the transports that take requests from the network. */
31
+ audit: (event: SecurityLogEvent) => void;
32
+ }): Promise<Transport[]>;