@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,6 +1,5 @@
1
1
  import { readdir, readFile, rename, rmdir, stat, unlink } from "node:fs/promises";
2
2
  import { join } from "node:path";
3
- import { getChannelDir } from "../channel/channel-paths.js";
4
3
  import * as log from "../log.js";
5
4
  import { writeFileAtomically } from "../shared/atomic-file.js";
6
5
  import { isProcessAlive, killProcessGroup, readProcessStartTime } from "../shared/host-process.js";
@@ -9,11 +8,13 @@ import { createSerialQueue } from "../shared/serial-queue.js";
9
8
  import { errorMessage } from "../shared/text-utils.js";
10
9
  import { beginWakeClaim, finishWakeClaim } from "../shared/wake-claim.js";
11
10
  import { logTaskDispatch, logTaskSettlement } from "../tasks/bound-work.js";
11
+ import { redeemTicket } from "../tasks/store.js";
12
12
  import { createEmptyUsageTotals } from "../usage/totals.js";
13
- import { buildRunCompletionWake, WAKE_OUTPUT_TAIL_CHARS } from "./completion-wake.js";
13
+ import { buildRunCompletionWake, countWakeSiblings, WAKE_OUTPUT_TAIL_CHARS } from "./completion-wake.js";
14
14
  import { createProgressPacer, startExternalProgressTail } from "./external/progress-tail.js";
15
15
  import { finalizeExternalRun } from "./external/settlement.js";
16
- import { applyPendingSettlement, createPendingSettlement, isTerminal, MAX_RUNNING_SUBAGENT_RUNS_PER_CHANNEL, MAX_RUNNING_SUBAGENT_RUNS_PER_HOST, normalizeRunIdQuery, parseRunRecord, RUN_ARTIFACT_FILENAMES, RUN_ID_PREFIX, RUN_RETENTION_MS, randomRunIdSuffix, } from "./run-record.js";
16
+ import { runArchiveEntry, runLedgerEntry } from "./run-archive.js";
17
+ import { applyPendingSettlement, createPendingSettlement, isTerminal, MAX_RUNNING_SUBAGENT_RUNS_PER_DESK, MAX_RUNNING_SUBAGENT_RUNS_PER_HOST, normalizeRunIdQuery, parseRunRecord, RUN_ARTIFACT_FILENAMES, RUN_ID_PREFIX, RUN_RETENTION_MS, randomRunIdSuffix, } from "./run-record.js";
17
18
  import { acquireWorkspaceLease, formatWorkspaceLeaseConflict, releaseWorkspaceLease } from "./workspace-lease.js";
18
19
  /** Registration is serialized across channel managers so the host-wide count and insertion are
19
20
  * one admission transaction. The per-run manager queue alone cannot protect the 19 -> 20 edge
@@ -29,14 +30,15 @@ async function unlinkIfPresent(path) {
29
30
  }
30
31
  }
31
32
  /**
32
- * Per-channel manager for delegation runs — the single settlement/usage/wake authority for both
33
+ * Per-desk manager for delegation runs — the single settlement/usage/wake authority for both
33
34
  * internal sub-agents and (once wired) external harnesses (D7). Modeled directly on
34
- * `ChannelJobManager`; the two are not unified because a run's terminal status depends on a
35
+ * `DeskJobManager`; the two are not unified because a run's terminal status depends on a
35
36
  * protocol verdict, not just process exit (D4), so the state machines genuinely differ.
36
37
  */
37
38
  export class SubAgentRunManager {
38
- constructor(channelId, options) {
39
- this.channelId = channelId;
39
+ /** `target` owns these runs; each run's own `channelId` is where it reports back. */
40
+ constructor(target, options) {
41
+ this.target = target;
40
42
  this.options = options;
41
43
  this.runs = new Map();
42
44
  /** In-memory cancel handles for runs alive in *this* process (internal worker abort or live
@@ -59,6 +61,10 @@ export class SubAgentRunManager {
59
61
  this.internalProgressPacers = new Map();
60
62
  this.queue = createSerialQueue();
61
63
  }
64
+ /** The completion wake's dispatch id — the only id `beginWakeConsumption` accepts for it. */
65
+ wakeDispatchId(runId) {
66
+ return `subagent:${this.target.desk}:${runId}:done`;
67
+ }
62
68
  /** Release observation timers without cancelling dispatched work or its settlement. */
63
69
  stop() {
64
70
  this.stopped = true;
@@ -68,10 +74,8 @@ export class SubAgentRunManager {
68
74
  this.stopExternalProgressTail(runId);
69
75
  }
70
76
  recordDir() {
71
- // `getChannelDir`, not a raw join: a DingTalk group id routinely contains `/`, which used
72
- // to nest the records one directory deeper than the startup scan ever looked — so a
73
- // restart silently abandoned every delegation run of every such channel.
74
- return this.options.stateDir ? getChannelDir(this.options.stateDir, this.channelId) : undefined;
77
+ // The desk name is a validated path segment; a channel id never is (spec 058, INV-3).
78
+ return this.options.stateDir ? join(this.options.stateDir, this.target.desk) : undefined;
75
79
  }
76
80
  recordPath(runId) {
77
81
  const dir = this.recordDir();
@@ -116,12 +120,12 @@ export class SubAgentRunManager {
116
120
  }
117
121
  }
118
122
  /** Fire-and-forget: a notifier failure must never affect settlement or the progress tail. */
119
- emitNotice(notice) {
123
+ emitNotice(record, notice) {
120
124
  const notify = this.options.notify;
121
125
  if (!notify)
122
126
  return;
123
127
  void Promise.resolve()
124
- .then(() => notify(this.channelId, notice))
128
+ .then(() => notify(this.target.desk, record.channelId, notice))
125
129
  .catch((error) => log.logWarn("subagent.notice.failed", "Run notice failed", {
126
130
  reason: errorMessage(error),
127
131
  fields: { runId: notice.runId },
@@ -140,7 +144,7 @@ export class SubAgentRunManager {
140
144
  harnessId: record.harness,
141
145
  startedAt: record.processStartedAt ?? record.startedAt,
142
146
  onProgress: (step) => {
143
- this.emitNotice({
147
+ this.emitNotice(record, {
144
148
  kind: "progress",
145
149
  runId: record.runId,
146
150
  agent: record.agent,
@@ -149,7 +153,7 @@ export class SubAgentRunManager {
149
153
  });
150
154
  },
151
155
  onStall: (silentMs) => {
152
- this.emitNotice({
156
+ this.emitNotice(record, {
153
157
  kind: "stalled",
154
158
  runId: record.runId,
155
159
  agent: record.agent,
@@ -173,7 +177,7 @@ export class SubAgentRunManager {
173
177
  return;
174
178
  let pacer = this.internalProgressPacers.get(runId);
175
179
  if (!pacer) {
176
- pacer = createProgressPacer(record.startedAt, (label) => this.emitNotice({
180
+ pacer = createProgressPacer(record.startedAt, (label) => this.emitNotice(record, {
177
181
  kind: "progress",
178
182
  runId,
179
183
  agent: record.agent,
@@ -215,16 +219,6 @@ export class SubAgentRunManager {
215
219
  list() {
216
220
  return Array.from(this.runs.values());
217
221
  }
218
- /** Task ids a still-running delegation on this channel will eventually wake. Feeds `/tasks doctor`. */
219
- runningTaskIds() {
220
- const ids = new Set();
221
- for (const record of this.runs.values()) {
222
- if (record.status === "running" && record.taskId) {
223
- ids.add(record.taskId);
224
- }
225
- }
226
- return ids;
227
- }
228
222
  runningCount() {
229
223
  return Array.from(this.runs.values()).filter((record) => record.status === "running").length;
230
224
  }
@@ -259,11 +253,11 @@ export class SubAgentRunManager {
259
253
  /** Register the run's lifecycle before any work starts, and persist it immediately (D1). */
260
254
  async register(input) {
261
255
  return hostAdmissionQueue.run("host", () => this.queue.run(input.runId, async () => {
262
- // D10.2: a per-channel and a host-wide cap, both code constants (a local response to a
256
+ // D10.2: a per-desk and a host-wide cap, both code constants (a local response to a
263
257
  // runaway model, not a global cross-subsystem admission scheme — see the design doc's
264
258
  // non-goals). Checked before anything is persisted, so a rejected run costs nothing.
265
- if (this.runningCount() >= MAX_RUNNING_SUBAGENT_RUNS_PER_CHANNEL) {
266
- throw new RecoverableToolError(`Too many delegation runs already running on this channel (>= ${MAX_RUNNING_SUBAGENT_RUNS_PER_CHANNEL}). ` +
259
+ if (this.runningCount() >= MAX_RUNNING_SUBAGENT_RUNS_PER_DESK) {
260
+ throw new RecoverableToolError(`Too many delegation runs already running in this desk (>= ${MAX_RUNNING_SUBAGENT_RUNS_PER_DESK}). ` +
267
261
  "Wait for one to finish, or cancel one with subagent_run op=cancel first.");
268
262
  }
269
263
  if ((this.options.runningCount?.() ?? this.runningCount()) >= MAX_RUNNING_SUBAGENT_RUNS_PER_HOST) {
@@ -271,7 +265,7 @@ export class SubAgentRunManager {
271
265
  "Wait for one to finish before dispatching another.");
272
266
  }
273
267
  if (this.runs.has(input.runId)) {
274
- throw new RecoverableToolError(`Run id "${input.runId}" is already registered on this channel.`);
268
+ throw new RecoverableToolError(`Run id "${input.runId}" is already registered in this desk.`);
275
269
  }
276
270
  const record = {
277
271
  ...input,
@@ -292,8 +286,8 @@ export class SubAgentRunManager {
292
286
  this.externalLaunches.delete(record.runId);
293
287
  throw error;
294
288
  }
295
- if (record.taskId && record.channelDir) {
296
- await logTaskDispatch(record.channelDir, record.taskId, {
289
+ if (record.taskId) {
290
+ await logTaskDispatch(this.target.deskDir, record.taskId, {
297
291
  ref: record.runId,
298
292
  item: record.item,
299
293
  agent: record.agent,
@@ -346,7 +340,7 @@ export class SubAgentRunManager {
346
340
  record.sessionId = info.sessionId;
347
341
  record.maxWallTimeSec = info.maxWallTimeSec;
348
342
  record.processStartedAt = info.processStartedAt;
349
- record.channelDir = info.channelDir;
343
+ record.deskDir = info.deskDir;
350
344
  record.invocationWarnings = info.invocationWarnings;
351
345
  record.roleFingerprint = info.roleFingerprint;
352
346
  record.parserVersion = info.parserVersion;
@@ -378,7 +372,7 @@ export class SubAgentRunManager {
378
372
  const record = this.runs.get(runId);
379
373
  if (!record)
380
374
  return false;
381
- const expected = `subagent:${record.channelId}:${runId}:done`;
375
+ const expected = this.wakeDispatchId(runId);
382
376
  const eligible = record.settledAt !== undefined && record.taskId === taskId && dispatchId === expected;
383
377
  const previousClaim = record.wakeClaimDispatchId;
384
378
  if (!beginWakeClaim(record, eligible, dispatchId))
@@ -475,50 +469,11 @@ export class SubAgentRunManager {
475
469
  this.internalProgressPacers.delete(runId);
476
470
  if (!record.usageRecorded) {
477
471
  record.usageRecorded = true;
478
- this.options.ledger?.record({
479
- channelId: record.channelId,
480
- kind: "subagent",
481
- model: record.model ?? "unknown",
482
- label: record.label,
483
- runId: record.runId,
484
- taskId: record.taskId,
485
- usage: {
486
- input: input.usage.input,
487
- output: input.usage.output,
488
- cacheRead: input.usage.cacheRead,
489
- cacheWrite: input.usage.cacheWrite,
490
- total: input.usage.total,
491
- },
492
- cost: { ...input.usage.cost },
493
- usageKnown: input.usageKnown,
494
- costKnown: input.costKnown,
495
- });
472
+ this.options.ledger?.record(runLedgerEntry(this.target.desk, record, input));
496
473
  // Archive is an observability write, not a settlement fact — a full queue must not
497
474
  // swallow the completion wake below (P0-2).
498
475
  await this.options.store
499
- ?.logSubAgentRun(record.channelId, {
500
- date: new Date().toISOString(),
501
- toolCallId: record.runId,
502
- label: record.label,
503
- agent: record.agent,
504
- source: record.source,
505
- model: record.model ?? "unknown",
506
- tools: record.tools,
507
- turns: input.turns,
508
- toolCalls: input.toolCalls,
509
- durationMs: input.durationMs,
510
- failed: input.status !== "completed",
511
- failureReason: input.failureReason,
512
- output: input.outputText.length > 16_000 ? input.outputText.slice(0, 16_000) : input.outputText,
513
- outputTruncated: input.outputText.length > 16_000,
514
- usage: { ...input.usage, cost: { ...input.usage.cost } },
515
- runId: record.runId,
516
- runtime: record.runtime,
517
- harness: record.harness,
518
- status: pending.status,
519
- taskId: record.taskId,
520
- artifactDir: record.artifactDir,
521
- })
476
+ ?.logSubAgentRun(this.target.deskDir, runArchiveEntry(record, input, pending.status))
522
477
  .catch((error) => {
523
478
  log.logWarn("subagent.archive.failed", "Run archive failed", {
524
479
  reason: errorMessage(error),
@@ -531,7 +486,7 @@ export class SubAgentRunManager {
531
486
  // budget and the outcome onto its log (the board's source), whether or not the run record
532
487
  // is still around by the time the leader looks. Guarded by its own idempotency marker
533
488
  // inside the same settlement lock, so a replay writes nothing twice.
534
- if (!record.taskAccounted && record.taskId && record.channelDir) {
489
+ if (!record.taskAccounted && record.taskId) {
535
490
  record.taskAccounted = true;
536
491
  await this.creditOwningTask(record, input, outputSaved).catch((error) => {
537
492
  log.logWarn("subagent.task_settle.log_failed", "Task credit for run failed", {
@@ -557,11 +512,10 @@ export class SubAgentRunManager {
557
512
  * block the completion wake.
558
513
  */
559
514
  async creditOwningTask(record, input, outputSaved) {
560
- const channelDir = record.channelDir;
561
515
  const taskId = record.taskId;
562
- if (!channelDir || !taskId)
516
+ if (!taskId)
563
517
  return;
564
- await logTaskSettlement(channelDir, taskId, {
518
+ await logTaskSettlement(this.target.deskDir, taskId, {
565
519
  ref: record.runId,
566
520
  item: record.item,
567
521
  status: record.status,
@@ -583,7 +537,7 @@ export class SubAgentRunManager {
583
537
  // caller was not already holding the result in the same tool call (`announce: true`, the same
584
538
  // condition that gates the wake below). It reaches the channel in roughly the time the run
585
539
  // itself took to settle, independent of the wake turn's own LLM latency on top of that.
586
- this.emitNotice({
540
+ this.emitNotice(record, {
587
541
  kind: "settled",
588
542
  runId: record.runId,
589
543
  agent: record.agent,
@@ -592,8 +546,19 @@ export class SubAgentRunManager {
592
546
  });
593
547
  if (!this.options.dispatch)
594
548
  return;
595
- const siblingCount = Array.from(this.runs.values()).filter((other) => other.taskId === record.taskId && other.status === "running" && other.runId !== record.runId).length;
596
- const event = buildRunCompletionWake({ record, outputText, outputSaved, siblingCount });
549
+ if (this.options.isBoundTo && !this.options.isBoundTo(record.channelId, this.target.desk)) {
550
+ await this.suppressWake(record);
551
+ return;
552
+ }
553
+ const siblingCount = countWakeSiblings(record, this.runs.values());
554
+ const event = buildRunCompletionWake({
555
+ desk: this.target.desk,
556
+ dispatchId: this.wakeDispatchId(record.runId),
557
+ record,
558
+ outputText,
559
+ outputSaved,
560
+ siblingCount,
561
+ });
597
562
  // The `boolean` return value (queue-full/backpressure -> `false`) is deliberately not
598
563
  // checked here, unlike the thrown-error case above: `DurableDispatchService.enqueueEvent`
599
564
  // puts a rejected record back into `pending` rather than dropping it, and the 30s periodic
@@ -613,6 +578,28 @@ export class SubAgentRunManager {
613
578
  record.wakeEnqueued = true;
614
579
  await this.persist(record);
615
580
  }
581
+ /**
582
+ * Spec 058, D8: the run's channel left this desk, so the wake has no recipient. Settlement and the
583
+ * task's settle record already happened; the wake is dropped (marker set, never replayed), and the
584
+ * `work` ticket it would have redeemed is redeemed here via `redeemTicket`, so the suspended task
585
+ * resumes as soon as its delivery channel is bound again.
586
+ */
587
+ async suppressWake(record) {
588
+ log.logInfoEvent("subagent.wake.suppressed", "Completion wake dropped: channel not bound to the run's desk", {
589
+ channelId: record.channelId,
590
+ fields: { runId: record.runId, desk: this.target.desk, taskId: record.taskId },
591
+ });
592
+ if (record.taskId) {
593
+ await redeemTicket(this.target.deskDir, record.taskId, (ticket) => ticket.kind === "work").catch((error) => {
594
+ log.logWarn("subagent.wake.redeem_failed", "Work ticket redeem for a dropped wake failed", {
595
+ reason: errorMessage(error),
596
+ fields: { runId: record.runId, taskId: record.taskId },
597
+ });
598
+ });
599
+ }
600
+ record.wakeEnqueued = true;
601
+ await this.persist(record);
602
+ }
616
603
  /** Explicit cancel (`subagent_run op=cancel`, spec 040 D6). Does not wake — it is the model's own decision. */
617
604
  async cancel(runId) {
618
605
  let adoptedRecord;
@@ -1017,6 +1004,12 @@ export class SubAgentRunManager {
1017
1004
  const current = this.runs.get(record.runId);
1018
1005
  if (!current || current.status !== "running")
1019
1006
  return;
1007
+ // Timers can fire just before their wall-clock deadline. Re-arm rather than losing
1008
+ // the only recovery check and leaving an adopted process running indefinitely.
1009
+ if (current.deadlineAt && Date.now() < current.deadlineAt) {
1010
+ this.scheduleExternalRecovery(current);
1011
+ return;
1012
+ }
1020
1013
  void this.reconcileExternalRunAtDeadline(current).catch((error) => {
1021
1014
  log.logWarn("subagent.reconcile.failed", "Adopted external run reconcile failed", {
1022
1015
  reason: errorMessage(error),
@@ -5,9 +5,8 @@
5
5
  */
6
6
  import { readFile } from "node:fs/promises";
7
7
  import { join } from "node:path";
8
- import { getChannelDir } from "../channel/channel-paths.js";
9
8
  import * as log from "../log.js";
10
- import { buildChannelIndexForBootstrap, CHANNEL_INDEX_MAX_UNITS, clipJournalTailForBootstrap, clipWorkspaceMemoryForBootstrap, JOURNAL_TAIL_MAX_UNITS, WORKSPACE_MEMORY_MAX_UNITS, } from "../memory/index-budget.js";
9
+ import { buildMemoryIndexForBootstrap, clipJournalTailForBootstrap, clipWorkspaceMemoryForBootstrap, JOURNAL_TAIL_MAX_UNITS, MEMORY_INDEX_MAX_UNITS, WORKSPACE_MEMORY_MAX_UNITS, } from "../memory/index-budget.js";
11
10
  import { readJournalDay } from "../memory/journal.js";
12
11
  import { renderMemoryBootstrap } from "../memory/render.js";
13
12
  import { listMemoryEntries } from "../memory/store.js";
@@ -77,8 +76,9 @@ export function buildSubAgentTask(task, config, runtimeContext, contextBlocks, r
77
76
  const lines = [
78
77
  `Runtime context:`,
79
78
  `- Workspace root: ${runtimeContext.workspaceDir}`,
79
+ `- Desk: ${runtimeContext.desk}`,
80
+ `- Desk directory: ${runtimeContext.deskDir}`,
80
81
  `- Channel id: ${runtimeContext.channelId}`,
81
- `- Channel directory: ${getChannelDir(runtimeContext.workspaceDir, runtimeContext.channelId)}`,
82
82
  `- Working directory: ${runContext.workingDirectory}`,
83
83
  `- Artifact directory: ${runContext.artifactDir}`,
84
84
  `- Your configured role: ${config.name}`,
@@ -96,9 +96,7 @@ export function buildSubAgentTask(task, config, runtimeContext, contextBlocks, r
96
96
  return lines.join("\n");
97
97
  }
98
98
  function verificationTaskPath(runtimeContext, runContext) {
99
- return runContext.taskId
100
- ? join(getChannelDir(runtimeContext.workspaceDir, runtimeContext.channelId), "tasks", `${runContext.taskId}.md`)
101
- : undefined;
99
+ return runContext.taskId ? join(runtimeContext.deskDir, "tasks", `${runContext.taskId}.md`) : undefined;
102
100
  }
103
101
  /**
104
102
  * The envelope for `subagent_run op=follow_up`. A resumed session already holds the first
@@ -123,7 +121,7 @@ export function buildFollowUpTask(task, runtimeContext, runContext) {
123
121
  }
124
122
  /**
125
123
  * Spec 050, D12: `memory: index` injects the same three sections the main agent's first-turn
126
- * bootstrap does — workspace MEMORY.md, the channel index, today's journal tail — at half the
124
+ * bootstrap does — workspace MEMORY.md, the desk memory index, today's journal tail — at half the
127
125
  * main agent's budget. No per-call LLM recall: a sub-agent task is short-lived, so there is no
128
126
  * "first turn" to bootstrap once and reuse; it gets the same snapshot every dispatch.
129
127
  */
@@ -148,14 +146,14 @@ async function buildContextualBlocks(config, options) {
148
146
  }
149
147
  };
150
148
  const [entries, workspaceMemory, journalToday] = await Promise.all([
151
- listMemoryEntries(options.channelDir).catch(() => []),
149
+ listMemoryEntries(options.deskDir).catch(() => []),
152
150
  readOptionalFile(join(options.workspaceDir, "MEMORY.md")),
153
- readJournalDay(options.channelDir, today).catch(() => ""),
151
+ readJournalDay(options.deskDir, today).catch(() => ""),
154
152
  ]);
155
- const index = buildChannelIndexForBootstrap(entries, CHANNEL_INDEX_MAX_UNITS / 2);
153
+ const index = buildMemoryIndexForBootstrap(entries, MEMORY_INDEX_MAX_UNITS / 2);
156
154
  const bootstrap = renderMemoryBootstrap({
157
155
  workspaceMemory: clipWorkspaceMemoryForBootstrap(workspaceMemory, WORKSPACE_MEMORY_MAX_UNITS / 2),
158
- channelIndex: entries.length > 0 ? index.text : "",
156
+ memoryIndex: entries.length > 0 ? index.text : "",
159
157
  journal: (() => {
160
158
  const text = clipJournalTailForBootstrap(journalToday, JOURNAL_TAIL_MAX_UNITS / 2);
161
159
  return text ? { date: today, text } : undefined;
@@ -86,7 +86,7 @@ export interface WorkerToolRequest {
86
86
  securityConfig: SecurityConfig;
87
87
  securityContext: SecurityRuntimeContext;
88
88
  channelId: string;
89
- channelDir: string;
89
+ deskDir: string;
90
90
  workspaceDir: string;
91
91
  webConfig?: PipiclawWebToolsConfig;
92
92
  rtkEnabled?: boolean;
@@ -99,15 +99,13 @@ export interface SubAgentToolOptions {
99
99
  buildWorkerTools: BuildWorkerTools;
100
100
  executor: Executor;
101
101
  fileStore: FileStore;
102
- /** Host checkout used as the sub-agent's cwd. Defaults to process.cwd(). */
102
+ /** Default working directory for a run: the parent's cwd. Defaults to process.cwd(). */
103
103
  workingDirectory?: string;
104
- /** Mirrors the parent channel's ProjectScope.boundary onto the sub-agent's own tool set (spec 043, D6.2). */
105
- projectBoundary?: "project" | "unbounded";
106
104
  getCurrentModel: () => Model<Api>;
107
105
  getAvailableModels: () => Model<Api>[];
108
106
  resolveApiKey: (model: Model<Api>) => Promise<string>;
109
107
  workspaceDir: string;
110
- channelDir: string;
108
+ deskDir: string;
111
109
  getSubAgentDiscovery?: () => SubAgentDiscoveryResult;
112
110
  /** `settings.subagentModel` (spec 032 D5); null/undefined means unset. */
113
111
  getSubAgentModelReference?: () => string | null;
@@ -116,6 +114,9 @@ export interface SubAgentToolOptions {
116
114
  rtkEnabled?: boolean;
117
115
  runtimeContext: {
118
116
  workspaceDir: string;
117
+ /** The desk the delegating turn belongs to, and its directory (tasks, memory). */
118
+ desk: string;
119
+ deskDir: string;
119
120
  channelId: string;
120
121
  };
121
122
  createWorker?: (config: {
@@ -31,7 +31,7 @@ export interface TaskBoard {
31
31
  }
32
32
  /** Ref lines the board renders before folding old, already-seen results away. */
33
33
  export declare const BOARD_REF_LINE_BUDGET = 12;
34
- export declare function buildTaskBoard(channelDir: string, taskId: string, body: string): Promise<TaskBoard | undefined>;
34
+ export declare function buildTaskBoard(deskDir: string, taskId: string, body: string): Promise<TaskBoard | undefined>;
35
35
  /** Render the `<task_board>` block body (model-facing; `/tasks show` uses `renderTaskBoardForUser`). */
36
36
  export declare function renderTaskBoard(board: TaskBoard, nowMs?: number): string;
37
37
  /**
@@ -5,8 +5,8 @@ import { parseTaskItems } from "./contract.js";
5
5
  import { describeSettlement, describeWorkRef, readTaskLog, } from "./loop-log.js";
6
6
  /** Ref lines the board renders before folding old, already-seen results away. */
7
7
  export const BOARD_REF_LINE_BUDGET = 12;
8
- export async function buildTaskBoard(channelDir, taskId, body) {
9
- const records = await readTaskLog(channelDir, taskId, { kinds: ["dispatch", "settle", "step"] });
8
+ export async function buildTaskBoard(deskDir, taskId, body) {
9
+ const records = await readTaskLog(deskDir, taskId, { kinds: ["dispatch", "settle", "step"] });
10
10
  const dispatches = new Map();
11
11
  const settles = new Map();
12
12
  let lastStepIndex = -1;
@@ -8,7 +8,7 @@ import { type TaskDispatchRecord, type TaskSettleRecord } from "./loop-log.js";
8
8
  * replayed settlement writes nothing twice.
9
9
  */
10
10
  /** Record that a delegation or job was bound to the task. A task that no longer exists is skipped. */
11
- export declare function logTaskDispatch(channelDir: string, taskId: string, record: Omit<TaskDispatchRecord, "ts" | "kind">): Promise<void>;
11
+ export declare function logTaskDispatch(deskDir: string, taskId: string, record: Omit<TaskDispatchRecord, "ts" | "kind">): Promise<void>;
12
12
  export interface TaskSettlementCost {
13
13
  usd: number;
14
14
  estimated: boolean;
@@ -17,4 +17,4 @@ export interface TaskSettlementCost {
17
17
  * Record that bound work settled and add its cost to the task's usage. The cost lands in the
18
18
  * same budget the leader's own steps spend from, so a runaway fan-out stops the task.
19
19
  */
20
- export declare function logTaskSettlement(channelDir: string, taskId: string, record: Omit<TaskSettleRecord, "ts" | "kind" | "usd" | "usdEstimated">, cost?: TaskSettlementCost): Promise<void>;
20
+ export declare function logTaskSettlement(deskDir: string, taskId: string, record: Omit<TaskSettleRecord, "ts" | "kind" | "usd" | "usdEstimated">, cost?: TaskSettlementCost): Promise<void>;
@@ -9,18 +9,18 @@ import { readStoredTask, updateStoredTask } from "./store.js";
9
9
  * replayed settlement writes nothing twice.
10
10
  */
11
11
  /** Record that a delegation or job was bound to the task. A task that no longer exists is skipped. */
12
- export async function logTaskDispatch(channelDir, taskId, record) {
13
- if (!(await readStoredTask(channelDir, taskId)))
12
+ export async function logTaskDispatch(deskDir, taskId, record) {
13
+ if (!(await readStoredTask(deskDir, taskId)))
14
14
  return;
15
- await appendTaskLog(channelDir, taskId, { kind: "dispatch", ...record });
15
+ await appendTaskLog(deskDir, taskId, { kind: "dispatch", ...record });
16
16
  }
17
17
  /**
18
18
  * Record that bound work settled and add its cost to the task's usage. The cost lands in the
19
19
  * same budget the leader's own steps spend from, so a runaway fan-out stops the task.
20
20
  */
21
- export async function logTaskSettlement(channelDir, taskId, record, cost = { usd: 0, estimated: false }) {
21
+ export async function logTaskSettlement(deskDir, taskId, record, cost = { usd: 0, estimated: false }) {
22
22
  if (cost.usd > 0 || cost.estimated) {
23
- await updateStoredTask(channelDir, taskId, (task) => {
23
+ await updateStoredTask(deskDir, taskId, (task) => {
24
24
  const usage = task.fields.usage;
25
25
  if (!usage)
26
26
  return;
@@ -31,9 +31,9 @@ export async function logTaskSettlement(channelDir, taskId, record, cost = { usd
31
31
  };
32
32
  });
33
33
  }
34
- if (!(await readStoredTask(channelDir, taskId)))
34
+ if (!(await readStoredTask(deskDir, taskId)))
35
35
  return;
36
- await appendTaskLog(channelDir, taskId, {
36
+ await appendTaskLog(deskDir, taskId, {
37
37
  kind: "settle",
38
38
  ...record,
39
39
  ...(cost.usd > 0 ? { usd: cost.usd } : {}),
@@ -3,7 +3,7 @@ export declare const BRIEF_LOG_LINES = 8;
3
3
  /** Cap on each part (close note, last report) of the previous occurrence carried into a spawned instance's first step. */
4
4
  export declare const PREVIOUS_OCCURRENCE_MAX_CHARS = 1200;
5
5
  export interface TaskBriefInput {
6
- channelDir: string;
6
+ deskDir: string;
7
7
  taskId: string;
8
8
  /** Why this step is running, when the caller knows. */
9
9
  reason?: string;
@@ -19,8 +19,8 @@ export const PREVIOUS_OCCURRENCE_MAX_CHARS = 1_200;
19
19
  * no scan of every archived contract. A candidate is accepted only if its own `origin` says so,
20
20
  * since one event name can be a prefix of another's.
21
21
  */
22
- async function findPreviousOccurrence(channelDir, origin, currentId) {
23
- const archiveDir = join(tasksDir(channelDir), "archive");
22
+ async function findPreviousOccurrence(deskDir, origin, currentId) {
23
+ const archiveDir = join(tasksDir(deskDir), "archive");
24
24
  if (!existsSync(archiveDir))
25
25
  return undefined;
26
26
  const candidates = (await readdir(archiveDir))
@@ -30,10 +30,10 @@ async function findPreviousOccurrence(channelDir, origin, currentId) {
30
30
  .sort()
31
31
  .reverse();
32
32
  for (const id of candidates) {
33
- const document = await readStoredTask(channelDir, id, true).catch(() => undefined);
33
+ const document = await readStoredTask(deskDir, id, true).catch(() => undefined);
34
34
  if (!document || document.fields.origin !== origin)
35
35
  continue;
36
- const latestFirst = (await readTaskLog(channelDir, id, { kinds: ["close", "step"] })).reverse();
36
+ const latestFirst = (await readTaskLog(deskDir, id, { kinds: ["close", "step"] })).reverse();
37
37
  const close = latestFirst.find((record) => record.kind === "close");
38
38
  const reported = latestFirst.find((record) => record.kind === "step" && record.report);
39
39
  return {
@@ -58,13 +58,13 @@ async function findPreviousOccurrence(channelDir, origin, currentId) {
58
58
  * the brief that is genuinely new since the previous step.
59
59
  */
60
60
  export async function buildTaskStepBrief(input) {
61
- const document = await readStoredTask(input.channelDir, input.taskId);
61
+ const document = await readStoredTask(input.deskDir, input.taskId);
62
62
  if (!document)
63
63
  return undefined;
64
- const steer = await consumeTaskSteer(input.channelDir, input.taskId);
65
- const log = await readTaskLog(input.channelDir, input.taskId, { kinds: ["step", "expired", "close", "note"] });
64
+ const steer = await consumeTaskSteer(input.deskDir, input.taskId);
65
+ const log = await readTaskLog(input.deskDir, input.taskId, { kinds: ["step", "expired", "close"] });
66
66
  const recent = log.slice(-BRIEF_LOG_LINES);
67
- const board = await buildTaskBoard(input.channelDir, input.taskId, document.body);
67
+ const board = await buildTaskBoard(input.deskDir, input.taskId, document.body);
68
68
  const blocks = [`[TASK_STEP:${input.taskId}]`];
69
69
  if (input.reason)
70
70
  blocks.push(input.reason);
@@ -78,7 +78,7 @@ export async function buildTaskStepBrief(input) {
78
78
  blocks.push(`<user_guidance>\n${steer}\n</user_guidance>`);
79
79
  const isFirstStep = !log.some((record) => record.kind === "step");
80
80
  if (isFirstStep && document.fields.origin) {
81
- const previous = await findPreviousOccurrence(input.channelDir, document.fields.origin, input.taskId);
81
+ const previous = await findPreviousOccurrence(input.deskDir, document.fields.origin, input.taskId);
82
82
  if (previous) {
83
83
  const parts = [`收尾记录:${clipText(previous.note, PREVIOUS_OCCURRENCE_MAX_CHARS)}`];
84
84
  if (previous.report)
@@ -1,8 +1,17 @@
1
1
  import { type TaskBudget } from "./frontmatter.js";
2
+ /** What `/tasks` needs to say about a delivery channel (structurally `channel/channel-id.ts`'s `ChannelDescription`). */
3
+ interface TaskChannelDescription {
4
+ label: string;
5
+ /** Still bound to this desk; false means the task is suspended (spec 058, D8). */
6
+ boundHere: boolean;
7
+ }
2
8
  export interface HandleTasksCommandOptions {
3
9
  args: string;
4
- /** The channel directory; tasks live in `<channelDir>/tasks/`. */
5
- channelDir: string;
10
+ /** The desk directory; tasks live in `<deskDir>/tasks/`. */
11
+ deskDir: string;
12
+ /** The channel the command was typed in: a task delivering elsewhere names its channel. */
13
+ currentChannelId: string;
14
+ describeChannel: (channelId: string) => TaskChannelDescription;
6
15
  /** Optional immediate task wake, available in the long-lived DingTalk runtime. */
7
16
  dispatchTask?: (id: string) => Promise<boolean>;
8
17
  }