@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
@@ -7,7 +7,6 @@ import { readdir } from "node:fs/promises";
7
7
  import { join } from "node:path";
8
8
  import { renderSubcommandUsage } from "../commands/catalog.js";
9
9
  import { capReply, flattenMarkdownForChat } from "../commands/reply-limits.js";
10
- import { docsUrl } from "../paths.js";
11
10
  import { formatSpanZh } from "../shared/format.js";
12
11
  import { parseLocalTime } from "../shared/local-time.js";
13
12
  import { errorMessage } from "../shared/text-utils.js";
@@ -80,8 +79,8 @@ export function parseTasksCommand(args) {
80
79
  }
81
80
  throw new Error(`未知子命令 \`${action}\`。`);
82
81
  }
83
- function tasksDir(channelDir) {
84
- return join(channelDir, "tasks");
82
+ function tasksDir(deskDir) {
83
+ return join(deskDir, "tasks");
85
84
  }
86
85
  /** `(最晚 3 小时后重新检查)` — when the runtime looks at a parked task again no matter what. */
87
86
  function backstopHint(by, now) {
@@ -102,8 +101,21 @@ function ticketSummary(entry, now) {
102
101
  const ticket = entry.fields.ticket;
103
102
  return ticket ? `${describeTicketForUser(ticket)}${backstopHint(ticket.by, now)}` : undefined;
104
103
  }
105
- function renderListLine(entry, channelDir, now) {
104
+ /** The delivery-channel note for a task, or `undefined` when it delivers here and is live. */
105
+ function deliveryNote(channel, options) {
106
+ if (!channel)
107
+ return "已暂停:未指定投递频道";
108
+ const described = options.describeChannel(channel);
109
+ if (!described.boundHere)
110
+ return `已暂停:投递频道未绑定到本工位(${described.label})`;
111
+ return channel === options.currentChannelId ? undefined : `投递到${described.label}`;
112
+ }
113
+ function renderListLine(entry, options, now) {
114
+ const { deskDir } = options;
106
115
  const bits = [`\`${entry.id}\` ${entry.title}`, taskStateLabel(entry.fields.state)];
116
+ const delivery = deliveryNote(entry.fields.channel, options);
117
+ if (delivery)
118
+ bits.push(delivery);
107
119
  if (entry.fields.paused)
108
120
  bits.push(`已暂停(${entry.fields.paused.reason})`);
109
121
  const ticket = ticketSummary(entry, now);
@@ -116,21 +128,21 @@ function renderListLine(entry, channelDir, now) {
116
128
  const usage = usageSummary(entry);
117
129
  if (usage)
118
130
  bits.push(usage);
119
- if (hasTaskSteer(channelDir, entry.id))
131
+ if (hasTaskSteer(deskDir, entry.id))
120
132
  bits.push("有待处理的指示");
121
133
  if (!entry.readable)
122
134
  bits.push("⚠️ 文件头(frontmatter)不可读");
123
135
  return `- ${bits.join(" · ")}`;
124
136
  }
125
- async function listTasks(channelDir) {
137
+ async function listTasks(options) {
126
138
  const now = Date.now();
127
- const entries = (await readActiveTasks(tasksDir(channelDir), now)).filter((entry) => !entry.fields.outcome);
139
+ const entries = (await readActiveTasks(tasksDir(options.deskDir), now)).filter((entry) => !entry.fields.outcome);
128
140
  if (entries.length === 0) {
129
141
  return "暂无进行中的任务。\n在对话里告诉我目标,并说明作为长程任务推进。";
130
142
  }
131
143
  const report = [
132
144
  `**任务(${entries.length})**`,
133
- ...entries.map((entry) => renderListLine(entry, channelDir, now)),
145
+ ...entries.map((entry) => renderListLine(entry, options, now)),
134
146
  ].join("\n");
135
147
  return capReply(report, { nextStepHint: "用 `/tasks show <id>` 查看单个任务" }).text;
136
148
  }
@@ -148,15 +160,23 @@ function contractExcerpt(body, maxChars) {
148
160
  const openFence = (head.match(/^```/gm)?.length ?? 0) % 2 === 1;
149
161
  return { text: openFence ? `${head}\n\`\`\`` : head, clipped: true };
150
162
  }
151
- async function showTask(channelDir, idInput) {
163
+ async function showTask(options, idInput) {
164
+ const { deskDir } = options;
152
165
  const id = normalizeTaskId(idInput);
153
- const document = (await readStoredTask(channelDir, id, true)) ?? undefined;
166
+ const document = (await readStoredTask(deskDir, id, true)) ?? undefined;
154
167
  if (!document)
155
168
  return notFound(id);
156
169
  const now = Date.now();
157
- const entry = (await readActiveTasks(tasksDir(channelDir), now)).find((candidate) => candidate.id === id);
170
+ const entry = (await readActiveTasks(tasksDir(deskDir), now)).find((candidate) => candidate.id === id);
158
171
  const head = [`**任务 ${id}**`];
159
172
  head.push(`- 状态:${taskStateLabel(document.fields.state)}`);
173
+ const channel = document.fields.channel;
174
+ if (!document.fields.outcome) {
175
+ const note = deliveryNote(channel, options);
176
+ head.push(channel
177
+ ? `- 投递频道(channel):${options.describeChannel(channel).label}${note?.startsWith("已暂停") ? `(${note})` : ""}`
178
+ : `- 投递频道:未指定(${note})`);
179
+ }
160
180
  if (document.fields.paused) {
161
181
  const by = document.fields.paused.by === "user" ? "由你暂停" : "由系统暂停";
162
182
  head.push(`- 已暂停(${by}):${document.fields.paused.reason}`);
@@ -170,14 +190,14 @@ async function showTask(channelDir, idInput) {
170
190
  const usage = entry ? usageSummary(entry) : undefined;
171
191
  if (usage)
172
192
  head.push(`- 用量:${usage}`);
173
- const board = await buildTaskBoard(channelDir, id, document.body);
193
+ const board = await buildTaskBoard(deskDir, id, document.body);
174
194
  const boardLines = board ? ["", "**工作项**", renderTaskBoardForUser(board, id, now)] : [];
175
- const log = await readTaskLog(channelDir, id, {
176
- kinds: ["step", "expired", "close", "note"],
195
+ const log = await readTaskLog(deskDir, id, {
196
+ kinds: ["step", "expired", "close"],
177
197
  limit: SHOW_LOG_LINES,
178
198
  });
179
199
  const logLines = log.length > 0 ? ["", "**最近记录**", ...log.map(renderTaskLogLineForUser)] : [];
180
- const path = existsSync(taskPath(channelDir, id)) ? taskPath(channelDir, id) : archivedTaskPath(channelDir, id);
200
+ const path = existsSync(taskPath(deskDir, id)) ? taskPath(deskDir, id) : archivedTaskPath(deskDir, id);
181
201
  const contract = contractExcerpt(document.body, TASK_SHOW_CONTRACT_CHARS);
182
202
  const report = [
183
203
  ...head,
@@ -193,8 +213,8 @@ async function showTask(channelDir, idInput) {
193
213
  return capReply(report, { nextStepHint: `用 \`/tasks log ${id}\` 查看记录,完整任务说明在 \`${path}\`` }).text;
194
214
  }
195
215
  const ARCHIVE_OUTCOME = { completed: "已完成", cancelled: "已取消" };
196
- async function listArchive(channelDir) {
197
- const archiveDir = join(tasksDir(channelDir), "archive");
216
+ async function listArchive(deskDir) {
217
+ const archiveDir = join(tasksDir(deskDir), "archive");
198
218
  if (!existsSync(archiveDir))
199
219
  return "暂无已归档任务。\n任务完成或取消后会出现在这里。";
200
220
  const files = (await readdir(archiveDir)).filter((name) => name.endsWith(".md")).sort();
@@ -203,7 +223,7 @@ async function listArchive(channelDir) {
203
223
  const lines = [];
204
224
  for (const filename of files.slice(0, ARCHIVE_LIST_LIMIT)) {
205
225
  const id = filename.slice(0, -".md".length);
206
- const document = await readStoredTask(channelDir, id, true).catch(() => undefined);
226
+ const document = await readStoredTask(deskDir, id, true).catch(() => undefined);
207
227
  const outcome = document?.fields.outcome;
208
228
  const bits = [`\`${id}\``];
209
229
  if (outcome)
@@ -219,13 +239,13 @@ async function listArchive(channelDir) {
219
239
  nextStepHint: `完整列表在 \`${archiveDir}\``,
220
240
  }).text;
221
241
  }
222
- async function showTaskLog(channelDir, idInput) {
242
+ async function showTaskLog(deskDir, idInput) {
223
243
  const id = normalizeTaskId(idInput);
224
- const all = await readTaskLog(channelDir, id);
244
+ const all = await readTaskLog(deskDir, id);
225
245
  if (all.length === 0)
226
246
  return `任务 \`${id}\` 暂无记录。\n任务执行第一步后会出现在这里。`;
227
247
  const records = all.slice(-LOG_LINES);
228
- const path = taskLogIsArchived(channelDir, id) ? taskArchiveLogPath(channelDir, id) : taskLogPath(channelDir, id);
248
+ const path = taskLogIsArchived(deskDir, id) ? taskArchiveLogPath(deskDir, id) : taskLogPath(deskDir, id);
229
249
  const lines = [
230
250
  `**任务 ${id} 的记录(最近 ${records.length} 条,共 ${all.length} 条)**`,
231
251
  ...records.map(renderTaskLogLineForUser),
@@ -236,7 +256,7 @@ async function showTaskLog(channelDir, idInput) {
236
256
  }
237
257
  async function pauseTask(options, idInput) {
238
258
  const id = normalizeTaskId(idInput);
239
- const document = await pauseTaskDocument(options.channelDir, id, {
259
+ const document = await pauseTaskDocument(options.deskDir, id, {
240
260
  by: "user",
241
261
  reason: "用户通过 /tasks pause 暂停。",
242
262
  });
@@ -246,12 +266,12 @@ async function pauseTask(options, idInput) {
246
266
  }
247
267
  export async function resumeTask(options, idInput, grants = {}) {
248
268
  const id = normalizeTaskId(idInput);
249
- const document = await resumeTaskDocument(options.channelDir, id);
269
+ const document = await resumeTaskDocument(options.deskDir, id);
250
270
  if (!document)
251
271
  return notFound(id);
252
272
  const granted = [];
253
273
  if (Object.keys(grants).length > 0) {
254
- await updateStoredTask(options.channelDir, id, (task) => {
274
+ await updateStoredTask(options.deskDir, id, (task) => {
255
275
  const budget = { ...task.fields.budget };
256
276
  for (const key of ["steps", "usd"]) {
257
277
  const extra = grants[key];
@@ -271,10 +291,10 @@ export async function resumeTask(options, idInput, grants = {}) {
271
291
  }
272
292
  async function steerTask(options, idInput, text) {
273
293
  const id = normalizeTaskId(idInput);
274
- const document = await readStoredTask(options.channelDir, id);
294
+ const document = await readStoredTask(options.deskDir, id);
275
295
  if (!document)
276
296
  return notFound(id);
277
- await queueTaskSteer(options.channelDir, id, text);
297
+ await queueTaskSteer(options.deskDir, id, text);
278
298
  return `已把这条指示排给任务 \`${id}\` 的下一步,不会打断正在执行的这一步。`;
279
299
  }
280
300
  /**
@@ -284,11 +304,11 @@ async function steerTask(options, idInput, text) {
284
304
  */
285
305
  async function replyToTask(options, idInput, text) {
286
306
  const id = normalizeTaskId(idInput);
287
- const document = await readStoredTask(options.channelDir, id);
307
+ const document = await readStoredTask(options.deskDir, id);
288
308
  if (!document)
289
309
  return notFound(id);
290
- await queueTaskSteer(options.channelDir, id, text, "用户回答");
291
- const redeemed = await redeemTicket(options.channelDir, id, (ticket) => ticket.kind === "ask");
310
+ await queueTaskSteer(options.deskDir, id, text, "用户回答");
311
+ const redeemed = await redeemTicket(options.deskDir, id, (ticket) => ticket.kind === "ask");
292
312
  if (!redeemed) {
293
313
  return `任务 \`${id}\` 当前并没有在等你的回答;内容已记下,下一步会看到。`;
294
314
  }
@@ -303,20 +323,23 @@ function issue(problem, nextStep) {
303
323
  * `/tasks doctor`, deliberately small (spec 051, D11; spec 052, D11).
304
324
  *
305
325
  * Most of what older doctors looked for cannot be produced any more: the frontmatter normalizer
306
- * and `resolveTicket` reject those states at write time. What is left is what a *hand edit*, or a
307
- * conversion that did not run, can still create.
326
+ * and `resolveTicket` reject those states at write time. What is left is what a *hand edit* can
327
+ * still create.
308
328
  */
309
329
  async function doctor(options) {
310
330
  const now = Date.now();
311
- const entries = await readActiveTasks(tasksDir(options.channelDir), now);
331
+ const entries = await readActiveTasks(tasksDir(options.deskDir), now);
312
332
  const problems = [];
313
333
  for (const entry of entries) {
314
334
  if (!entry.readable) {
315
- problems.push(issue(`\`${entry.id}\` 的文件头(frontmatter)不可读`, `让我修复 \`${taskPath(options.channelDir, entry.id)}\` 的文件头,或手动修改`));
335
+ problems.push(issue(`\`${entry.id}\` 的文件头(frontmatter)不可读`, `让我修复 \`${taskPath(options.deskDir, entry.id)}\` 的文件头,或手动修改`));
316
336
  continue;
317
337
  }
318
- if (entry.legacy) {
319
- problems.push(issue(`\`${entry.id}\` 仍含旧版本的字段(status / enabled / control / wake / schedule)`, `保留原件,按 ${docsUrl("events-and-tasks.md")} 的升级步骤处理;不要只删旧字段`));
338
+ if (!entry.fields.channel) {
339
+ problems.push(issue(`\`${entry.id}\` 没有投递频道(channel),已暂停`, `在 \`${taskPath(options.deskDir, entry.id)}\` 的文件头加一行 \`channel: <频道 ID>\``));
340
+ }
341
+ else if (!options.describeChannel(entry.fields.channel).boundHere) {
342
+ problems.push(issue(`\`${entry.id}\` 的投递频道没有绑定到本工位,已暂停`, "在那个频道里把它绑定回本工位,或把文件头的 `channel:` 改成本工位已绑定的频道"));
320
343
  }
321
344
  const by = entry.fields.ticket ? parseLocalTime(entry.fields.ticket.by) : undefined;
322
345
  if (entry.expired && !entry.fields.paused && by !== undefined && by < now - EXPIRED_GRACE_MS) {
@@ -325,7 +348,9 @@ async function doctor(options) {
325
348
  }
326
349
  if (problems.length === 0)
327
350
  return `检查了 ${entries.length} 个任务,未发现问题。`;
328
- return [`**任务体检(${problems.length} 项)**`, ...problems].join("\n");
351
+ return capReply([`**任务体检(${problems.length} 项)**`, ...problems].join("\n"), {
352
+ nextStepHint: "逐个修复后再运行 `/tasks doctor` 查看剩余的问题",
353
+ }).text;
329
354
  }
330
355
  export async function handleTasksCommand(options) {
331
356
  let command;
@@ -337,7 +362,7 @@ export async function handleTasksCommand(options) {
337
362
  }
338
363
  try {
339
364
  if ("id" in command && command.id && command.action !== "show" && command.action !== "log") {
340
- return await withTaskMutation(options.channelDir, command.id, () => dispatchTasksCommand(options, command));
365
+ return await withTaskMutation(options.deskDir, command.id, () => dispatchTasksCommand(options, command));
341
366
  }
342
367
  return await dispatchTasksCommand(options, command);
343
368
  }
@@ -348,13 +373,13 @@ export async function handleTasksCommand(options) {
348
373
  async function dispatchTasksCommand(options, command) {
349
374
  switch (command.action) {
350
375
  case "list":
351
- return await listTasks(options.channelDir);
376
+ return await listTasks(options);
352
377
  case "show":
353
- return await showTask(options.channelDir, command.id);
378
+ return await showTask(options, command.id);
354
379
  case "log":
355
- return await showTaskLog(options.channelDir, command.id);
380
+ return await showTaskLog(options.deskDir, command.id);
356
381
  case "archive":
357
- return await listArchive(options.channelDir);
382
+ return await listArchive(options.deskDir);
358
383
  case "pause":
359
384
  return await pauseTask(options, command.id);
360
385
  case "resume":
@@ -17,8 +17,6 @@ export interface TaskLedgerEntry {
17
17
  fields: TaskFrontmatter;
18
18
  /** false => frontmatter could not be read; the task is surfaced rather than skipped. */
19
19
  readable: boolean;
20
- /** True when this file still carries v3 frontmatter the conversion should have removed; it is never run. */
21
- legacy: boolean;
22
20
  /** The driver may schedule a step for this task right now. */
23
21
  runnable: boolean;
24
22
  /** When a driver-polled `time` ticket comes due, if it has not already. */
@@ -295,7 +295,6 @@ function toEntry(id, content) {
295
295
  title: extractTaskTitle(content, id),
296
296
  fields: parsed.fields,
297
297
  readable: parsed.readable,
298
- legacy: parsed.legacy,
299
298
  items: parseTaskItems(content),
300
299
  };
301
300
  }
@@ -305,7 +304,7 @@ function withClock(entry, now) {
305
304
  const ticket = entry.fields.ticket;
306
305
  return {
307
306
  ...entry,
308
- runnable: isTaskRunnable(entry.fields) && !entry.legacy,
307
+ runnable: isTaskRunnable(entry.fields),
309
308
  dueMs: ticket ? ticketDueMs(ticket) : undefined,
310
309
  expired: ticket !== undefined && !entry.fields.paused && ticketExpired(ticket, nowDate),
311
310
  };
@@ -355,7 +354,6 @@ export async function readActiveTasks(tasksDir, now = Date.now()) {
355
354
  title: id,
356
355
  fields: { state: "open" },
357
356
  readable: false,
358
- legacy: false,
359
357
  runnable: true,
360
358
  expired: false,
361
359
  });
@@ -1,9 +1,11 @@
1
1
  import type { TaskCreateInput } from "./contract-input.js";
2
2
  import type { StoredTaskDocument } from "./store.js";
3
3
  export interface CreateTaskDocumentOptions {
4
- channelDir: string;
4
+ deskDir: string;
5
5
  id: string;
6
6
  input: TaskCreateInput;
7
+ /** The delivery channel (spec 058, D13): the creating turn's channel, or the spawning event's. */
8
+ channel: string;
7
9
  /** The event whose template spawned this instance. */
8
10
  origin?: string;
9
11
  now?: Date;
@@ -5,29 +5,35 @@ import { formatLocalTime } from "../shared/local-time.js";
5
5
  import { RecoverableToolError } from "../shared/recoverable-error.js";
6
6
  import { createUsage } from "./budget.js";
7
7
  import { normalizeTaskId, renderStandardTaskBody, renderTaskDocument } from "./contract.js";
8
+ import { withTaskMutation } from "./mutation-lock.js";
8
9
  import { archivedTaskPath, taskPath, tasksDir } from "./store.js";
9
10
  /**
10
11
  * Write a new task file, `open`, with its usage clock started. Shared by `task_create` and the
11
12
  * event-template spawner so both produce byte-identical documents.
12
13
  */
13
- export async function createTaskDocument(options) {
14
+ export function createTaskDocument(options) {
15
+ // Locked like every other task write, so two processes cannot both pass the existence check.
16
+ return withTaskMutation(options.deskDir, options.id, () => writeNewTask(options));
17
+ }
18
+ async function writeNewTask(options) {
14
19
  const id = normalizeTaskId(options.id);
15
- const path = taskPath(options.channelDir, id);
20
+ const path = taskPath(options.deskDir, id);
16
21
  if (existsSync(path)) {
17
22
  throw new RecoverableToolError(`Task "${id}" already exists; use task_update or edit the body instead.`);
18
23
  }
19
- if (existsSync(archivedTaskPath(options.channelDir, id))) {
24
+ if (existsSync(archivedTaskPath(options.deskDir, id))) {
20
25
  throw new RecoverableToolError(`Archived task "${id}" already exists; choose a new id or restore it manually first.`);
21
26
  }
22
27
  const now = options.now ?? new Date();
23
28
  const fields = {
24
29
  state: "open",
30
+ channel: options.channel,
25
31
  origin: options.origin,
26
32
  usage: createUsage(now),
27
33
  budget: options.input.budget,
28
34
  };
29
35
  const body = renderStandardTaskBody(options.input);
30
- await writeFileAtomically(join(tasksDir(options.channelDir), `${id}.md`), renderTaskDocument(fields, body));
36
+ await writeFileAtomically(join(tasksDir(options.deskDir), `${id}.md`), renderTaskDocument(fields, body));
31
37
  return { id, path, fields, body };
32
38
  }
33
39
  /** The local-time stamp spawned instances carry in their id: `YYYYMMDD-HHmm`. */
@@ -31,6 +31,12 @@ export type TaskArchiveOutcome = "completed" | "cancelled";
31
31
  export interface TaskFrontmatter {
32
32
  state: TaskState;
33
33
  paused?: TaskPaused;
34
+ /**
35
+ * The delivery channel: where every step, stop receipt and report of this task goes (spec 058,
36
+ * D13). Plain text so a human can retarget it by hand. Missing, or not bound to this task's desk,
37
+ * means the task is suspended — the driver leaves it frozen until the channel is bound again.
38
+ */
39
+ channel?: string;
34
40
  /** Present iff `state === "parked"` (INV-1). */
35
41
  ticket?: Ticket;
36
42
  usage?: TaskUsage;
@@ -45,8 +51,6 @@ export interface ParsedTaskFrontmatter {
45
51
  fields: TaskFrontmatter;
46
52
  /** false => the block could not be read at all; the task is surfaced rather than skipped. */
47
53
  readable: boolean;
48
- /** True when a v3 line (`status`, `control`, `schedule`…) is present — the file is not converted, so it never runs. */
49
- legacy: boolean;
50
54
  }
51
55
  /**
52
56
  * The one user-facing name for a task state (spec 053, D8). `/tasks`, the stop receipt and the
@@ -11,9 +11,7 @@ export function taskStateLabel(state) {
11
11
  return STATE_LABELS[state] ?? state;
12
12
  }
13
13
  /** Fixed render order, so a diff of a task file reads the same way every time. */
14
- const FIELD_ORDER = ["state", "paused", "origin", "ticket", "usage", "budget"];
15
- /** v3 frontmatter keys; their presence means the one-time conversion has not run on this file. */
16
- const LEGACY_KEYS = new Set(["status", "enabled", "control", "wake", "schedule"]);
14
+ const FIELD_ORDER = ["state", "paused", "channel", "origin", "ticket", "usage", "budget"];
17
15
  function parseJsonObject(raw) {
18
16
  try {
19
17
  const value = JSON.parse(raw);
@@ -67,12 +65,11 @@ function parseBudget(raw) {
67
65
  */
68
66
  export function parseTaskFrontmatter(content) {
69
67
  if (!content.startsWith("---"))
70
- return { fields: { state: "open" }, readable: false, legacy: false };
68
+ return { fields: { state: "open" }, readable: false };
71
69
  const end = content.indexOf("\n---", 3);
72
70
  if (end === -1)
73
- return { fields: { state: "open" }, readable: false, legacy: false };
71
+ return { fields: { state: "open" }, readable: false };
74
72
  const fields = { state: "open" };
75
- let legacy = false;
76
73
  let sawState = false;
77
74
  for (const line of content.slice(3, end).split("\n")) {
78
75
  const idx = line.indexOf(":");
@@ -102,6 +99,9 @@ export function parseTaskFrontmatter(content) {
102
99
  case "origin":
103
100
  fields.origin = value || undefined;
104
101
  break;
102
+ case "channel":
103
+ fields.channel = value || undefined;
104
+ break;
105
105
  case "outcome":
106
106
  if (value === "completed" || value === "cancelled")
107
107
  fields.outcome = value;
@@ -110,16 +110,14 @@ export function parseTaskFrontmatter(content) {
110
110
  fields.closedAt = value || undefined;
111
111
  break;
112
112
  default:
113
- if (LEGACY_KEYS.has(key))
114
- legacy = true;
115
113
  break;
116
114
  }
117
115
  }
118
116
  if (fields.outcome)
119
117
  fields.state = "done";
120
118
  // A file with no recognizable marker is not frontmatter we understand.
121
- const readable = sawState || fields.outcome !== undefined || legacy;
122
- return { fields: normalizeTaskFrontmatter(fields), readable, legacy };
119
+ const readable = sawState || fields.outcome !== undefined;
120
+ return { fields: normalizeTaskFrontmatter(fields), readable };
123
121
  }
124
122
  /**
125
123
  * Enforce INV-1 on every write and read: `parked` requires a ticket, and a ticket only means
@@ -148,6 +146,8 @@ export function renderTaskFrontmatter(fields) {
148
146
  lines.push(`outcome: ${document.outcome}`);
149
147
  if (document.closedAt)
150
148
  lines.push(`closedAt: ${document.closedAt}`);
149
+ if (document.channel)
150
+ lines.push(`channel: ${document.channel}`);
151
151
  if (document.origin)
152
152
  lines.push(`origin: ${document.origin}`);
153
153
  if (document.usage)
@@ -78,26 +78,20 @@ export interface TaskCloseRecord {
78
78
  steps: number;
79
79
  usd: number;
80
80
  }
81
- /** Written only by the v3 → v5 conversion, to explain a decision it made about this task. */
82
- export interface TaskNoteRecord {
83
- ts: string;
84
- kind: "note";
85
- note: string;
86
- }
87
- export type TaskLogRecord = TaskStepRecord | TaskDispatchRecord | TaskSettleRecord | TaskExpiredRecord | TaskCloseRecord | TaskNoteRecord;
88
- export declare function taskLogPath(channelDir: string, id: string): string;
81
+ export type TaskLogRecord = TaskStepRecord | TaskDispatchRecord | TaskSettleRecord | TaskExpiredRecord | TaskCloseRecord;
82
+ export declare function taskLogPath(deskDir: string, id: string): string;
89
83
  /** Where `archiveTask` moves a closed task's `.jsonl`. */
90
- export declare function taskArchiveLogPath(channelDir: string, id: string): string;
84
+ export declare function taskArchiveLogPath(deskDir: string, id: string): string;
91
85
  /** True when the loop log for `id` lives only in the archive (the task is closed). */
92
- export declare function taskLogIsArchived(channelDir: string, id: string): boolean;
86
+ export declare function taskLogIsArchived(deskDir: string, id: string): boolean;
93
87
  /** Test seam: drop cached appenders so a temp-dir fixture does not leak into the next case. */
94
88
  export declare function resetTaskLogAppenders(): Promise<void>;
95
- export type TaskLogInput = Omit<TaskStepRecord, "ts"> | Omit<TaskDispatchRecord, "ts"> | Omit<TaskSettleRecord, "ts"> | Omit<TaskExpiredRecord, "ts"> | Omit<TaskCloseRecord, "ts"> | Omit<TaskNoteRecord, "ts">;
89
+ export type TaskLogInput = Omit<TaskStepRecord, "ts"> | Omit<TaskDispatchRecord, "ts"> | Omit<TaskSettleRecord, "ts"> | Omit<TaskExpiredRecord, "ts"> | Omit<TaskCloseRecord, "ts">;
96
90
  /**
97
91
  * Append one record. `appendStrict` rather than `append`: the loop log is the only durable trace
98
92
  * of what a step did, so a dropped record would make the next brief lie about the task's history.
99
93
  */
100
- export declare function appendTaskLog(channelDir: string, id: string, record: TaskLogInput & {
94
+ export declare function appendTaskLog(deskDir: string, id: string, record: TaskLogInput & {
101
95
  ts?: string;
102
96
  }): Promise<void>;
103
97
  export interface ReadTaskLogOptions {
@@ -114,7 +108,7 @@ export interface ReadTaskLogOptions {
114
108
  * current file made rotated-out history invisible to `task_log`, briefs, and and the board
115
109
  * alike even though it was still on disk.
116
110
  */
117
- export declare function readTaskLog(channelDir: string, id: string, options?: ReadTaskLogOptions): Promise<TaskLogRecord[]>;
111
+ export declare function readTaskLog(deskDir: string, id: string, options?: ReadTaskLogOptions): Promise<TaskLogRecord[]>;
118
112
  /** One line per record, for the step brief and `task_log` (model-facing; `/tasks` uses `renderTaskLogLineForUser`). */
119
113
  export declare function renderTaskLogLine(record: TaskLogRecord): string;
120
114
  /** A run id is `run_…`; anything else bound to a task is a background job. */
@@ -6,19 +6,20 @@ import { formatLocalTime } from "../shared/local-time.js";
6
6
  import { clipText, truncate } from "../shared/text-utils.js";
7
7
  import { isPlainObject } from "../shared/type-guards.js";
8
8
  import { normalizeTaskId } from "./contract.js";
9
+ import { withTaskMutation } from "./mutation-lock.js";
9
10
  /** Same rotation shape as the channel archive: the log is working history, not an audit trail. */
10
11
  const MAX_LOG_BYTES = 4 * 1024 * 1024;
11
12
  const NOTE_MAX_CHARS = 4_000;
12
- export function taskLogPath(channelDir, id) {
13
- return join(channelDir, "tasks", `${normalizeTaskId(id)}.jsonl`);
13
+ export function taskLogPath(deskDir, id) {
14
+ return join(deskDir, "tasks", `${normalizeTaskId(id)}.jsonl`);
14
15
  }
15
16
  /** Where `archiveTask` moves a closed task's `.jsonl`. */
16
- export function taskArchiveLogPath(channelDir, id) {
17
- return join(channelDir, "tasks", "archive", `${normalizeTaskId(id)}.jsonl`);
17
+ export function taskArchiveLogPath(deskDir, id) {
18
+ return join(deskDir, "tasks", "archive", `${normalizeTaskId(id)}.jsonl`);
18
19
  }
19
20
  /** True when the loop log for `id` lives only in the archive (the task is closed). */
20
- export function taskLogIsArchived(channelDir, id) {
21
- return !existsSync(taskLogPath(channelDir, id)) && existsSync(taskArchiveLogPath(channelDir, id));
21
+ export function taskLogIsArchived(deskDir, id) {
22
+ return !existsSync(taskLogPath(deskDir, id)) && existsSync(taskArchiveLogPath(deskDir, id));
22
23
  }
23
24
  const appenders = new Map();
24
25
  function appenderFor(path) {
@@ -39,16 +40,18 @@ export async function resetTaskLogAppenders() {
39
40
  * Append one record. `appendStrict` rather than `append`: the loop log is the only durable trace
40
41
  * of what a step did, so a dropped record would make the next brief lie about the task's history.
41
42
  */
42
- export async function appendTaskLog(channelDir, id, record) {
43
+ export async function appendTaskLog(deskDir, id, record) {
43
44
  const withTs = { ts: record.ts ?? formatLocalTime(), ...record };
44
45
  if (withTs.kind === "step") {
45
46
  withTs.note = clipText(withTs.note, NOTE_MAX_CHARS);
46
47
  if (withTs.report)
47
48
  withTs.report = clipText(withTs.report, NOTE_MAX_CHARS);
48
49
  }
49
- await appenderFor(taskLogPath(channelDir, id)).appendStrict(withTs);
50
+ // Under the task's mutation lock: the appender rotates by size, and two processes rotating
51
+ // one log at once would each move the other's fresh records aside.
52
+ await withTaskMutation(deskDir, id, () => appenderFor(taskLogPath(deskDir, id)).appendStrict(withTs));
50
53
  }
51
- const RECORD_KINDS = ["step", "dispatch", "settle", "expired", "close", "note"];
54
+ const RECORD_KINDS = ["step", "dispatch", "settle", "expired", "close"];
52
55
  function toRecord(value) {
53
56
  if (!isPlainObject(value) || typeof value.ts !== "string")
54
57
  return undefined;
@@ -90,12 +93,10 @@ function parseLogLines(content, options, into) {
90
93
  * current file made rotated-out history invisible to `task_log`, briefs, and and the board
91
94
  * alike even though it was still on disk.
92
95
  */
93
- export async function readTaskLog(channelDir, id, options = {}) {
96
+ export async function readTaskLog(deskDir, id, options = {}) {
94
97
  // Fall back to the archived copy so `task_log` on a completed task returns its history rather
95
98
  // than "暂无日志" — a task that finished still has a loop log worth reading.
96
- const basePath = existsSync(taskLogPath(channelDir, id))
97
- ? taskLogPath(channelDir, id)
98
- : taskArchiveLogPath(channelDir, id);
99
+ const basePath = existsSync(taskLogPath(deskDir, id)) ? taskLogPath(deskDir, id) : taskArchiveLogPath(deskDir, id);
99
100
  const paths = shardPaths(basePath);
100
101
  if (paths.length === 0)
101
102
  return [];
@@ -125,8 +126,6 @@ export function renderTaskLogLine(record) {
125
126
  return `- [${record.ts}] 等待票过期(${record.ticket})→ ${record.action}`;
126
127
  case "close":
127
128
  return `- [${record.ts}] ${record.outcome === "done" ? "完成" : "取消"}:${record.note}`;
128
- case "note":
129
- return `- [${record.ts}] ${record.note}`;
130
129
  }
131
130
  }
132
131
  /** `2026-10-11T14:02:33.000+08:00` → `10-11 14:02`: read straight off the stored wall-clock text, never re-zoned. */
@@ -181,7 +180,5 @@ export function renderTaskLogLineForUser(record) {
181
180
  return `- ${at} 等待超时(${oneLine(record.ticket, 80)}),${record.action === "reopened" ? "已重新开始" : "已暂停"}`;
182
181
  case "close":
183
182
  return `- ${at} ${record.outcome === "done" ? "任务完成" : "任务取消"}:${oneLine(record.note, 160)}`;
184
- case "note":
185
- return `- ${at} ${oneLine(record.note, 160)}`;
186
183
  }
187
184
  }
@@ -1,8 +1,10 @@
1
1
  /**
2
- * Serialize mutations of one task inside this process.
2
+ * Serialize mutations of one task: inside this process with a serial queue, and across processes
3
+ * with the task's file lock (spec 058, D6) — the daemon and a TUI can open the same desk, and both
4
+ * may write its tasks. Every writer of a task's contract, loop log or steer/notice files goes
5
+ * through here.
3
6
  *
4
- * Pipiclaw deliberately supports one process per workspace; cross-process sharing remains
5
- * unsupported. Re-entrancy is required because a command/tool transaction may call a lower-level
6
- * store helper for the same task.
7
+ * Re-entrant: a command/tool transaction may call a lower-level store helper for the same task,
8
+ * and the inner call then runs inside the outer one's hold on both layers.
7
9
  */
8
- export declare function withTaskMutation<T>(channelDir: string, id: string, mutate: () => Promise<T>): Promise<T>;
10
+ export declare function withTaskMutation<T>(deskDir: string, id: string, mutate: () => Promise<T>): Promise<T>;
@@ -1,23 +1,46 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
2
  import { join } from "node:path";
3
+ import { FileLockTimeoutError, withFileLock } from "../shared/file-lock.js";
4
+ import { RecoverableToolError } from "../shared/recoverable-error.js";
3
5
  import { createSerialQueue } from "../shared/serial-queue.js";
4
6
  import { normalizeTaskId } from "./contract.js";
5
7
  const mutationQueue = createSerialQueue();
6
8
  const heldKeys = new AsyncLocalStorage();
7
- function taskMutationKey(channelDir, id) {
8
- return join(channelDir, "tasks", `${normalizeTaskId(id)}.md`);
9
+ /**
10
+ * A task mutation is a few small file writes; waiting longer than this means the other process
11
+ * is stuck, and the caller is better off retrying than hanging a tool call or a driver tick.
12
+ */
13
+ const TASK_LOCK_TIMEOUT_MS = 30_000;
14
+ function taskMutationKey(deskDir, id) {
15
+ return join(deskDir, "tasks", `${normalizeTaskId(id)}.md`);
16
+ }
17
+ /** `<deskDir>/.locks/task-<id>.lock` — the layout `desk/paths.ts` defines (`LOCKS_DIRNAME`, which must stay
18
+ * in step with the literal below): tasks stays a leaf and does not import it. */
19
+ function taskLockPath(deskDir, id) {
20
+ return join(deskDir, ".locks", `task-${normalizeTaskId(id)}.lock`);
9
21
  }
10
22
  /**
11
- * Serialize mutations of one task inside this process.
23
+ * Serialize mutations of one task: inside this process with a serial queue, and across processes
24
+ * with the task's file lock (spec 058, D6) — the daemon and a TUI can open the same desk, and both
25
+ * may write its tasks. Every writer of a task's contract, loop log or steer/notice files goes
26
+ * through here.
12
27
  *
13
- * Pipiclaw deliberately supports one process per workspace; cross-process sharing remains
14
- * unsupported. Re-entrancy is required because a command/tool transaction may call a lower-level
15
- * store helper for the same task.
28
+ * Re-entrant: a command/tool transaction may call a lower-level store helper for the same task,
29
+ * and the inner call then runs inside the outer one's hold on both layers.
16
30
  */
17
- export function withTaskMutation(channelDir, id, mutate) {
18
- const key = taskMutationKey(channelDir, id);
31
+ export function withTaskMutation(deskDir, id, mutate) {
32
+ const key = taskMutationKey(deskDir, id);
19
33
  const current = heldKeys.getStore();
20
34
  if (current?.has(key))
21
35
  return mutate();
22
- return mutationQueue.run(key, () => heldKeys.run(new Set([...(current ?? []), key]), mutate));
36
+ return mutationQueue.run(key, () => heldKeys.run(new Set([...(current ?? []), key]), async () => {
37
+ try {
38
+ return await withFileLock(taskLockPath(deskDir, id), mutate, { timeoutMs: TASK_LOCK_TIMEOUT_MS });
39
+ }
40
+ catch (error) {
41
+ if (!(error instanceof FileLockTimeoutError))
42
+ throw error;
43
+ throw new RecoverableToolError(`Task "${normalizeTaskId(id)}" is being modified by another Pipiclaw process${error.heldBy ? ` (pid ${error.heldBy.pid})` : ""}; retry this call in a moment.`);
44
+ }
45
+ }));
23
46
  }