@oyasmi/pipiclaw 0.9.4 → 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 +8 -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
@@ -0,0 +1,13 @@
1
+ /** `dingtalk:dm:abc` → `{ transport: "dingtalk", native: "dm:abc" }`; `undefined` when either side is empty or there is no colon. */
2
+ export function parseChannelId(channelId) {
3
+ const colon = channelId.indexOf(":");
4
+ if (colon <= 0)
5
+ return undefined;
6
+ const native = channelId.slice(colon + 1);
7
+ if (!native || native.includes("\0"))
8
+ return undefined;
9
+ return { transport: channelId.slice(0, colon), native };
10
+ }
11
+ export function transportOf(channelId) {
12
+ return parseChannelId(channelId)?.transport;
13
+ }
@@ -1,10 +1,10 @@
1
1
  /**
2
2
  * `workspace/CHANNELS.md` — the readable map of every channel this runtime knows.
3
3
  *
4
- * A channel's on-disk identity is its raw id (`group_cidYDhGqxhJOzS7VDv/eDInUw==`), which is
5
- * unreadable in a directory listing, in `/events` output and in the logs. This index gives
6
- * every id a name and a last-seen time in one place, so the operator browsing the workspace
7
- * and the agent asking "what other channels are there?" both have something to read.
4
+ * A channel's identity is its raw id (`group_cidYDhGqxhJOzS7VDv/eDInUw==`), which is unreadable
5
+ * in `/events` output and in the logs. This index gives every id a name, the desk it is bound to,
6
+ * and a last-seen time in one place, so the operator browsing the workspace and `/desk` both have
7
+ * something to read. It names channels; it is not how the runtime finds them (spec 058, D17).
8
8
  *
9
9
  * ## Maintenance contract
10
10
  *
@@ -20,13 +20,11 @@
20
20
  * `close()` at shutdown.
21
21
  * - **What survives a rewrite**: every flush re-reads the file first, so the hand-written
22
22
  * `主题` column — and any row for a channel this process never saw — is carried over
23
- * verbatim. The runtime owns three columns; the fourth belongs to whoever edits it.
23
+ * verbatim. The runtime owns four columns; the fifth belongs to whoever edits it. The `desk`
24
+ * column is rendered from the live bindings, and a binding change is structural.
24
25
  *
25
26
  * The index is the durable store for channel names: the transport learns a name only while a
26
- * message is being handled, so nothing else outlives a restart. It fills in from the first
27
- * message in each channel rather than being seeded from disk — a channel *directory* name is
28
- * the escaped form of the id (`/` → `__`, see `channel-paths.ts`) and cannot be turned back
29
- * into one, so scanning the workspace would populate rows keyed by ids that do not exist.
27
+ * message is being handled, so nothing else outlives a restart.
30
28
  */
31
29
  export declare const CHANNELS_INDEX_FILENAME = "CHANNELS.md";
32
30
  /**
@@ -44,14 +42,22 @@ export interface ChannelObservation {
44
42
  export interface ChannelIndexEntry {
45
43
  channelId: string;
46
44
  name: string;
45
+ /** The desk the channel is bound to, as of the last rewrite; `""` when unbound. */
46
+ desk: string;
47
47
  /** Local wall-clock time of the last human message, or `""` when never observed. */
48
48
  lastMessageAt: string;
49
49
  /** Hand-written; the runtime only ever copies it forward. */
50
50
  topic: string;
51
51
  }
52
52
  export interface ChannelIndex {
53
+ /** Read the names already on disk, so `nameOf` knows channels not seen since this process started. */
54
+ load(): Promise<void>;
55
+ /** The channel's name, from this process's observations or the file; `undefined` when never named. */
56
+ nameOf(channelId: string): string | undefined;
53
57
  /** Record one inbound human message. Cheap and synchronous; writing is decided internally. */
54
58
  note(observation: ChannelObservation): void;
59
+ /** A binding changed: rewrite now so the `desk` column is current (structural, not debounced). */
60
+ noteBindingChanged(): void;
55
61
  /** Write pending changes now. No-op when nothing changed. */
56
62
  flush(): Promise<void>;
57
63
  /** Cancel the debounce timer and write one last time. */
@@ -68,33 +74,10 @@ export declare function renderChannelIndex(entries: ChannelIndexEntry[]): string
68
74
  * stop the runtime from recording the next message.
69
75
  */
70
76
  export declare function parseChannelIndex(content: string): ChannelIndexEntry[];
71
- /** Channel ids recorded in the index, i.e. every channel that has ever received a message. */
72
- export declare function readChannelIndexIds(workspaceDir: string, options?: DiscoverOptions): Promise<string[]>;
73
- export interface DiscoverOptions {
74
- /**
75
- * Throw on anything other than "does not exist" instead of reporting no channels. For the one-shot
76
- * migration, where an unreadable workspace must not look like an empty one.
77
- */
78
- strict?: boolean;
79
- }
80
- /**
81
- * Every channel this workspace knows about, for the background workers that must visit
82
- * channels nobody has spoken to since the process started (memory maintenance, the task
83
- * driver). Live channels come from the caller's runner map; this covers the rest.
84
- *
85
- * The index is the accurate source — it stores real ids. Scanning the workspace root is the
86
- * fallback for a workspace that predates the index, and it is inherently lossy: a channel
87
- * *directory* is the escaped form of the id (`/` → `__`), which cannot be turned back into the
88
- * original. It is still kept alongside the index rather than only when the file is missing,
89
- * because the index fills in one channel at a time: on the first run after an upgrade the file
90
- * exists with a single row, and treating that as "the index has answers now" would drop every
91
- * other pre-existing channel until each of them happened to be spoken in. Directories whose
92
- * name is the escaped form of an already-indexed id are skipped, so a channel is never visited
93
- * twice under two spellings.
94
- */
95
- export declare function discoverWorkspaceChannelIds(workspaceDir: string, options?: DiscoverOptions): Promise<string[]>;
96
77
  export interface CreateChannelIndexOptions {
97
78
  workspaceDir: string;
79
+ /** The desk a channel is bound to right now, for the `desk` column. */
80
+ getBoundDesk: (channelId: string) => string | undefined;
98
81
  /** Override the timestamp-only debounce; tests use a short one. */
99
82
  debounceMs?: number;
100
83
  }
@@ -1,10 +1,10 @@
1
1
  /**
2
2
  * `workspace/CHANNELS.md` — the readable map of every channel this runtime knows.
3
3
  *
4
- * A channel's on-disk identity is its raw id (`group_cidYDhGqxhJOzS7VDv/eDInUw==`), which is
5
- * unreadable in a directory listing, in `/events` output and in the logs. This index gives
6
- * every id a name and a last-seen time in one place, so the operator browsing the workspace
7
- * and the agent asking "what other channels are there?" both have something to read.
4
+ * A channel's identity is its raw id (`group_cidYDhGqxhJOzS7VDv/eDInUw==`), which is unreadable
5
+ * in `/events` output and in the logs. This index gives every id a name, the desk it is bound to,
6
+ * and a last-seen time in one place, so the operator browsing the workspace and `/desk` both have
7
+ * something to read. It names channels; it is not how the runtime finds them (spec 058, D17).
8
8
  *
9
9
  * ## Maintenance contract
10
10
  *
@@ -20,22 +20,19 @@
20
20
  * `close()` at shutdown.
21
21
  * - **What survives a rewrite**: every flush re-reads the file first, so the hand-written
22
22
  * `主题` column — and any row for a channel this process never saw — is carried over
23
- * verbatim. The runtime owns three columns; the fourth belongs to whoever edits it.
23
+ * verbatim. The runtime owns four columns; the fifth belongs to whoever edits it. The `desk`
24
+ * column is rendered from the live bindings, and a binding change is structural.
24
25
  *
25
26
  * The index is the durable store for channel names: the transport learns a name only while a
26
- * message is being handled, so nothing else outlives a restart. It fills in from the first
27
- * message in each channel rather than being seeded from disk — a channel *directory* name is
28
- * the escaped form of the id (`/` → `__`, see `channel-paths.ts`) and cannot be turned back
29
- * into one, so scanning the workspace would populate rows keyed by ids that do not exist.
27
+ * message is being handled, so nothing else outlives a restart.
30
28
  */
31
- import { readdir, readFile } from "node:fs/promises";
29
+ import { readFile } from "node:fs/promises";
32
30
  import { join } from "node:path";
33
31
  import * as log from "../log.js";
34
32
  import { writeFileAtomically } from "../shared/atomic-file.js";
35
33
  import { formatLocalTime, parseLocalTime } from "../shared/local-time.js";
36
34
  import { createSerialQueue } from "../shared/serial-queue.js";
37
35
  import { errorMessage } from "../shared/text-utils.js";
38
- import { getChannelDirName, isChannelId } from "./channel-paths.js";
39
36
  export const CHANNELS_INDEX_FILENAME = "CHANNELS.md";
40
37
  /**
41
38
  * Timestamp-only debounce. Five minutes is far below the resolution anyone reads this file at,
@@ -45,13 +42,13 @@ export const DEFAULT_DEBOUNCE_MS = 5 * 60_000;
45
42
  /** Names come from a chat platform; clip so one absurd group title cannot deform the table. */
46
43
  const MAX_NAME_CHARS = 80;
47
44
  // === Rendering and parsing ===
48
- const HEADER_CELLS = ["频道 ID", "名称", "最近消息", "主题"];
45
+ const HEADER_CELLS = ["频道 ID", "名称", "工位", "最近消息", "主题"];
49
46
  const FILE_HEADER = `# Channels
50
47
 
51
48
  <!--
52
49
  运行时自动维护的频道索引。
53
50
 
54
- - 「频道 ID」「名称」「最近消息」三列由运行时在收到消息时更新,手工改动会在下次重写时被覆盖。
51
+ - 「频道 ID」「名称」「工位」「最近消息」四列由运行时维护,手工改动会在下次重写时被覆盖;「工位」是本频道当前绑定的工位,用 \`/desk bind\` 修改。
55
52
  - 「主题」一列留给你(或 agent)填写,重写索引时原样保留 — 用一句话写清这个频道是干什么的。
56
53
  - 「最近消息」只统计真人消息,不含定时事件和任务驱动的后台唤醒,所以它能反映频道是否还活跃。
57
54
  - 行按最近消息时间倒序排列。
@@ -97,7 +94,7 @@ function isSeparatorRow(cells) {
97
94
  return cells.length > 0 && cells.every((cell) => /^:?-{2,}:?$/.test(cell));
98
95
  }
99
96
  export function renderChannelIndex(entries) {
100
- const rows = entries.map((entry) => `| \`${escapeCell(entry.channelId)}\` | ${escapeCell(entry.name)} | ${escapeCell(entry.lastMessageAt)} | ${escapeCell(entry.topic)} |`);
97
+ const rows = entries.map((entry) => `| \`${escapeCell(entry.channelId)}\` | ${escapeCell(entry.name)} | ${escapeCell(entry.desk)} | ${escapeCell(entry.lastMessageAt)} | ${escapeCell(entry.topic)} |`);
101
98
  return [
102
99
  FILE_HEADER,
103
100
  `| ${HEADER_CELLS.join(" | ")} |`,
@@ -129,78 +126,18 @@ export function parseChannelIndex(content) {
129
126
  if (!channelId || channelId === HEADER_CELLS[0] || seen.has(channelId))
130
127
  continue;
131
128
  seen.add(channelId);
129
+ if (cells.length !== HEADER_CELLS.length)
130
+ continue;
132
131
  entries.push({
133
132
  channelId,
134
133
  name: cells[1] ?? "",
135
- lastMessageAt: cells[2] ?? "",
136
- topic: cells[3] ?? "",
134
+ desk: cells[2] ?? "",
135
+ lastMessageAt: cells[3] ?? "",
136
+ topic: cells[4] ?? "",
137
137
  });
138
138
  }
139
139
  return entries;
140
140
  }
141
- // === Discovery ===
142
- /** Channel ids recorded in the index, i.e. every channel that has ever received a message. */
143
- export async function readChannelIndexIds(workspaceDir, options = {}) {
144
- try {
145
- const content = await readFile(join(workspaceDir, CHANNELS_INDEX_FILENAME), "utf-8");
146
- return parseChannelIndex(content)
147
- .map((entry) => entry.channelId)
148
- .filter(isChannelId);
149
- }
150
- catch (error) {
151
- if (options.strict && !isMissing(error))
152
- throw error;
153
- return [];
154
- }
155
- }
156
- function isMissing(error) {
157
- const code = error.code;
158
- return code === "ENOENT" || code === "ENOTDIR";
159
- }
160
- /**
161
- * Every channel this workspace knows about, for the background workers that must visit
162
- * channels nobody has spoken to since the process started (memory maintenance, the task
163
- * driver). Live channels come from the caller's runner map; this covers the rest.
164
- *
165
- * The index is the accurate source — it stores real ids. Scanning the workspace root is the
166
- * fallback for a workspace that predates the index, and it is inherently lossy: a channel
167
- * *directory* is the escaped form of the id (`/` → `__`), which cannot be turned back into the
168
- * original. It is still kept alongside the index rather than only when the file is missing,
169
- * because the index fills in one channel at a time: on the first run after an upgrade the file
170
- * exists with a single row, and treating that as "the index has answers now" would drop every
171
- * other pre-existing channel until each of them happened to be spoken in. Directories whose
172
- * name is the escaped form of an already-indexed id are skipped, so a channel is never visited
173
- * twice under two spellings.
174
- */
175
- export async function discoverWorkspaceChannelIds(workspaceDir, options = {}) {
176
- const channelIds = new Set(await readChannelIndexIds(workspaceDir, options));
177
- const indexedDirNames = new Set([...channelIds].map(getChannelDirName));
178
- try {
179
- const dirents = await readdir(workspaceDir, { withFileTypes: true });
180
- for (const entry of dirents) {
181
- // A directory id containing `__` may be an escaped group id (`/` → `__`). The
182
- // workspace scan cannot recover the original id, which is required in task event
183
- // templates. Background discovery keeps its historical best-effort behavior; a
184
- // one-shot migration must stop instead of writing events to a fake channel id.
185
- if (options.strict &&
186
- entry.isDirectory() &&
187
- /^(dm|group)_/.test(entry.name) &&
188
- entry.name.includes("__") &&
189
- !indexedDirNames.has(entry.name)) {
190
- throw new Error(`Cannot recover the real channel id for escaped workspace directory ${entry.name}; restore ${CHANNELS_INDEX_FILENAME} from backup before migration.`);
191
- }
192
- if (!entry.isDirectory() || indexedDirNames.has(entry.name) || !isChannelId(entry.name))
193
- continue;
194
- channelIds.add(entry.name);
195
- }
196
- }
197
- catch (error) {
198
- // Outside strict mode a missing or unreadable workspace simply has no channels to report.
199
- if (options.strict && !isMissing(error))
200
- throw error;
201
- }
202
- return [...channelIds].sort();
203
- }
204
141
  function entryTime(entry) {
205
142
  return entry.lastMessageAt ? (parseLocalTime(entry.lastMessageAt) ?? 0) : 0;
206
143
  }
@@ -222,6 +159,8 @@ export function createChannelIndex(options) {
222
159
  // Serialized so two flushes can never interleave their read-merge-write.
223
160
  const queue = createSerialQueue();
224
161
  const observed = new Map();
162
+ /** Names read from the file at `load`, for channels this process has not observed. */
163
+ const known = new Map();
225
164
  let dirty = false;
226
165
  let closed = false;
227
166
  let timer = null;
@@ -261,11 +200,12 @@ export function createChannelIndex(options) {
261
200
  // An observation without a name (a DM from a nameless sender) must not erase a
262
201
  // name the file already carries.
263
202
  name: channel.name || existing?.name || "",
203
+ desk: "",
264
204
  lastMessageAt: renderTimestamp(Math.max(channel.atMs, existing ? entryTime(existing) : 0)),
265
205
  topic: existing?.topic ?? "",
266
206
  });
267
207
  }
268
- return sortEntries([...byId.values()]);
208
+ return sortEntries([...byId.values()].map((entry) => ({ ...entry, desk: options.getBoundDesk(entry.channelId) ?? "" })));
269
209
  };
270
210
  const flush = () => {
271
211
  // A structural change starts its write without awaiting it, so "nothing to do" still has
@@ -290,6 +230,27 @@ export function createChannelIndex(options) {
290
230
  return pending;
291
231
  };
292
232
  return {
233
+ async load() {
234
+ try {
235
+ for (const entry of parseChannelIndex(await readFile(indexPath, "utf-8"))) {
236
+ if (entry.name)
237
+ known.set(entry.channelId, entry.name);
238
+ }
239
+ }
240
+ catch {
241
+ // No index yet: names fill in as channels speak.
242
+ }
243
+ },
244
+ nameOf(channelId) {
245
+ return observed.get(channelId)?.name || known.get(channelId) || undefined;
246
+ },
247
+ noteBindingChanged() {
248
+ if (closed)
249
+ return;
250
+ dirty = true;
251
+ cancelTimer();
252
+ void flush();
253
+ },
293
254
  note(observation) {
294
255
  if (closed || !observation.channelId)
295
256
  return;
@@ -0,0 +1,10 @@
1
+ export declare const CHANNEL_SECTION_NAMES: readonly ["dingtalk", "webapi"];
2
+ export type ChannelSectionName = (typeof CHANNEL_SECTION_NAMES)[number];
3
+ /** The raw content of every section that is switched on. */
4
+ export type EnabledChannelSections = Partial<Record<ChannelSectionName, unknown>>;
5
+ /** `channel.json` (or one of its sections) cannot be used; `issues` say what to fix. */
6
+ export declare class ChannelConfigError extends Error {
7
+ readonly issues: string[];
8
+ constructor(issues: string[]);
9
+ }
10
+ export declare function parseChannelConfig(text: string): EnabledChannelSections;
@@ -0,0 +1,55 @@
1
+ /**
2
+ * `channel.json`'s outer shape (spec 061, D1/D2): one section per transport, each with its own
3
+ * `enabled` switch. This file only decides *which* sections are on; what is inside a section is
4
+ * that transport's own business, and a section that is off is never looked at beyond its switch,
5
+ * so credentials can stay in the file while a transport is disabled.
6
+ */
7
+ import { docsUrl } from "../paths.js";
8
+ import { errorMessage } from "../shared/text-utils.js";
9
+ import { isPlainObject } from "../shared/type-guards.js";
10
+ export const CHANNEL_SECTION_NAMES = ["dingtalk", "webapi"];
11
+ /** `channel.json` (or one of its sections) cannot be used; `issues` say what to fix. */
12
+ export class ChannelConfigError extends Error {
13
+ constructor(issues) {
14
+ super(issues.join("\n"));
15
+ this.issues = issues;
16
+ this.name = "ChannelConfigError";
17
+ }
18
+ }
19
+ export function parseChannelConfig(text) {
20
+ let parsed;
21
+ try {
22
+ parsed = JSON.parse(text);
23
+ }
24
+ catch (error) {
25
+ throw new ChannelConfigError([`Not valid JSON: ${errorMessage(error)}`]);
26
+ }
27
+ if (!isPlainObject(parsed)) {
28
+ throw new ChannelConfigError(["Expected a JSON object with a `dingtalk` and/or a `webapi` section."]);
29
+ }
30
+ const issues = [];
31
+ for (const key of Object.keys(parsed)) {
32
+ if (!CHANNEL_SECTION_NAMES.includes(key)) {
33
+ issues.push(`Unknown top-level key \`${key}\`. channel.json has one section per transport (${CHANNEL_SECTION_NAMES.map((name) => `\`${name}\``).join(", ")}); move transport settings into their section (${docsUrl("configuration.md")}).`);
34
+ }
35
+ }
36
+ const enabled = {};
37
+ for (const name of CHANNEL_SECTION_NAMES) {
38
+ const section = parsed[name];
39
+ if (section === undefined)
40
+ continue;
41
+ if (!isPlainObject(section)) {
42
+ issues.push(`\`${name}\` must be an object.`);
43
+ continue;
44
+ }
45
+ if (typeof section.enabled !== "boolean") {
46
+ issues.push(`\`${name}.enabled\` must be true or false.`);
47
+ continue;
48
+ }
49
+ if (section.enabled)
50
+ enabled[name] = section;
51
+ }
52
+ if (issues.length > 0)
53
+ throw new ChannelConfigError(issues);
54
+ return enabled;
55
+ }
@@ -0,0 +1,101 @@
1
+ /**
2
+ * The channel plane's inbound contract (spec 061, D5): the one place a transport hands a message
3
+ * to the runtime. Authorization has already happened by the time a transport calls it.
4
+ */
5
+ export interface InboundMessage {
6
+ channelId: string;
7
+ user: string;
8
+ userName?: string;
9
+ /** The group title or the peer's nickname, for the channel index; absent when there is none. */
10
+ channelName?: string;
11
+ /** The user's text; each attachment sits where its `[图片N]` / `[文件N]` marker is. */
12
+ text: string;
13
+ /** Raw attachment bytes. A transport downloads or decodes and checks size; persisting is the runtime's job. */
14
+ attachments?: InboundAttachment[];
15
+ /** The transport's correlation reference; replies to this message carry it back (`PushMessage.ref`). */
16
+ ref?: string;
17
+ ts: string;
18
+ /**
19
+ * The desks this sender may use; absent means any (an allow-listed DingTalk user, the local
20
+ * socket). The first entry is the sender's default desk: an unbound channel is bound to it.
21
+ */
22
+ allowedDesks?: readonly string[];
23
+ context?: import("./turn-context.js").TurnContext;
24
+ /** Runtime-derived execution desk for a saved response continuation. */
25
+ desk?: string;
26
+ }
27
+ /** `index` is the N of the `[图片N]` / `[文件N]` marker the attachment belongs to. */
28
+ export type InboundAttachment = {
29
+ kind: "image";
30
+ index: number;
31
+ data: Buffer;
32
+ } | {
33
+ kind: "file";
34
+ index: number;
35
+ data: Buffer;
36
+ fileName: string;
37
+ };
38
+ export type IngressResult =
39
+ /** A turn for this message starts now. */
40
+ {
41
+ kind: "started";
42
+ }
43
+ /** It runs after the current turn, as a turn of its own. */
44
+ | {
45
+ kind: "queued";
46
+ }
47
+ /** A command or a steer was handled; the reply (if any) went out through `push` with the message's `ref`. */
48
+ | {
49
+ kind: "replied";
50
+ } | {
51
+ kind: "rejected";
52
+ reason: IngressRejection;
53
+ };
54
+ export type IngressRejection = {
55
+ code: "control_command";
56
+ command: "stop" | "steer" | "followup";
57
+ } | {
58
+ code: "desk_not_allowed";
59
+ desk: string;
60
+ } | {
61
+ code: "queue_full";
62
+ } | {
63
+ code: "request_capacity";
64
+ } | {
65
+ code: "shutting_down";
66
+ };
67
+ export type SteerResult = {
68
+ kind: "accepted";
69
+ } | {
70
+ kind: "rejected";
71
+ code: "response_not_active" | "too_many_pending_steers" | "invalid_input";
72
+ };
73
+ /**
74
+ * What `/stop` actually did. Stopping a task step also pauses that task, which the user has to be
75
+ * told: a paused task is not picked up again until `/tasks resume`, so silence would read as "the
76
+ * turn was interrupted" while the whole task has in fact gone quiet.
77
+ */
78
+ export interface StopOutcome {
79
+ pausedTaskId?: string;
80
+ }
81
+ export interface Ingress {
82
+ receive(message: InboundMessage): Promise<IngressResult>;
83
+ /** Isolated generation: no channel binding, transcript archive, or automatic reflection. */
84
+ execute?(message: InboundMessage): Promise<IngressResult>;
85
+ /** Inject `message` into the turn running on `channelId`. */
86
+ steer(channelId: string, message: InboundMessage): Promise<SteerResult>;
87
+ /** The same as `/stop`: the running turn ends and everything queued behind it is dropped. */
88
+ cancel(channelId: string): Promise<StopOutcome>;
89
+ /**
90
+ * Stop the turn running on `channelId` only if it answers the message that carried `ref`;
91
+ * whether it did. What is queued behind it, and anything another sender is waiting for, stays.
92
+ */
93
+ cancelTurn(channelId: string, ref: string): Promise<boolean>;
94
+ /** Drop a message that has not started; whether one with this `ref` was found. */
95
+ dropQueued(channelId: string, ref: string): boolean;
96
+ /**
97
+ * The transport ended this channel for good (a deleted Web API conversation): nothing more is
98
+ * kept for it — no binding, no runner, no background scans. `busy` while a turn runs or waits.
99
+ */
100
+ closeChannel(channelId: string): Promise<"closed" | "busy">;
101
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * The channel plane's inbound contract (spec 061, D5): the one place a transport hands a message
3
+ * to the runtime. Authorization has already happened by the time a transport calls it.
4
+ */
5
+ export {};
@@ -9,7 +9,7 @@ export interface LoggedMessage {
9
9
  deliveryMode?: "steer" | "followUp";
10
10
  skipContextSync?: boolean;
11
11
  /**
12
- * Paths of any images the user sent alongside `text` (spec 049), relative to the channel
12
+ * Paths of any images the user sent alongside `text` (spec 049), relative to the desk
13
13
  * directory. Kept even for an image that never made it into the model's context (unsupported
14
14
  * format, oversized, model without vision support) — this is the only place that record
15
15
  * survives once the turn's `[图片N: ...]` marker has scrolled out of `HISTORY.md`.
@@ -17,7 +17,11 @@ export interface LoggedMessage {
17
17
  images?: string[];
18
18
  }
19
19
  export interface ChannelStoreConfig {
20
- workingDir: string;
20
+ /**
21
+ * Where a channel's archive goes right now (spec 058, D17): its conversation directory in the
22
+ * desk it is bound to. `undefined` for an unbound channel, which is not archived at all.
23
+ */
24
+ resolveArchiveDir: (channelId: string) => string | undefined;
21
25
  }
22
26
  export interface LoggedSubAgentRun {
23
27
  date: string;
@@ -57,29 +61,23 @@ export interface LoggedSubAgentRun {
57
61
  };
58
62
  }
59
63
  export declare class ChannelStore {
60
- private workingDir;
64
+ private readonly resolveArchiveDir;
61
65
  private recentlyLogged;
62
66
  private cleanupTimer;
63
67
  private archiveAppender;
64
68
  private closed;
65
69
  constructor(config: ChannelStoreConfig);
66
- /**
67
- * The archive path for a channel — resolved, not created.
68
- *
69
- * This used to call `ensureChannelDir`, i.e. a synchronous `mkdirSync` on the event loop for
70
- * every archived message. `logBotResponse` runs on every progress update of a streaming turn,
71
- * so that was a blocking syscall per progress tick. The appender's own `ensureDir` already
72
- * creates the directory before it writes, and memoizes it, so the mkdir here was pure
73
- * duplicated I/O.
74
- */
75
- private channelArchiveDir;
76
70
  /**
77
71
  * Log a message to the channel's log.jsonl raw archive.
78
72
  * This file is cold storage and is not proactively loaded into memory context.
79
- * Returns false if message was already logged (duplicate)
73
+ * Returns false if the message was already logged (duplicate), or the channel is unbound.
80
74
  */
81
75
  logMessage(channelId: string, message: LoggedMessage): Promise<boolean>;
82
- logSubAgentRun(channelId: string, run: LoggedSubAgentRun): Promise<void>;
76
+ /**
77
+ * Archive a settled run in the desk it ran in. Takes the desk directory rather than resolving the
78
+ * channel: a run that settles after its channel was rebound still belongs to its own desk.
79
+ */
80
+ logSubAgentRun(deskDir: string, run: LoggedSubAgentRun): Promise<void>;
83
81
  /**
84
82
  * Log a bot response
85
83
  */
@@ -1,13 +1,14 @@
1
1
  /**
2
- * Per-channel message archive (`log.jsonl`): inbound messages, bot responses and delegation records,
3
- * with de-duplication, size rotation and a debounced flush.
2
+ * The message archive: each channel's `log.jsonl` (inbound messages, bot responses) in its
3
+ * conversation directory, and each desk's `subagent-runs.jsonl`, with de-duplication, size
4
+ * rotation and a debounced flush.
4
5
  */
5
- import { existsSync, mkdirSync } from "fs";
6
6
  import { writeFile } from "fs/promises";
7
7
  import { basename, dirname, join } from "path";
8
+ import { getSubAgentRunsLogPath } from "../desk/paths.js";
9
+ import * as log from "../log.js";
8
10
  import { createJsonlAppender } from "../shared/jsonl-appender.js";
9
11
  import { formatLocalTime } from "../shared/local-time.js";
10
- import { getChannelDir } from "./channel-paths.js";
11
12
  const MAX_LOG_SIZE_BYTES = 1_000_000;
12
13
  const DEDUPE_TTL_MS = 60_000;
13
14
  const DEDUPE_CLEANUP_INTERVAL_MS = 30_000;
@@ -16,7 +17,7 @@ export class ChannelStore {
16
17
  this.recentlyLogged = new Map();
17
18
  this.cleanupTimer = null;
18
19
  this.closed = false;
19
- this.workingDir = config.workingDir;
20
+ this.resolveArchiveDir = config.resolveArchiveDir;
20
21
  this.archiveAppender = createJsonlAppender({
21
22
  pathFor: (_now, record) => record.filePath,
22
23
  recordForWrite: (record) => record.value,
@@ -33,32 +34,23 @@ export class ChannelStore {
33
34
  }
34
35
  },
35
36
  });
36
- // Ensure working directory exists
37
- if (!existsSync(this.workingDir)) {
38
- mkdirSync(this.workingDir, { recursive: true });
39
- }
40
- }
41
- /**
42
- * The archive path for a channel — resolved, not created.
43
- *
44
- * This used to call `ensureChannelDir`, i.e. a synchronous `mkdirSync` on the event loop for
45
- * every archived message. `logBotResponse` runs on every progress update of a streaming turn,
46
- * so that was a blocking syscall per progress tick. The appender's own `ensureDir` already
47
- * creates the directory before it writes, and memoizes it, so the mkdir here was pure
48
- * duplicated I/O.
49
- */
50
- channelArchiveDir(channelId) {
51
- return getChannelDir(this.workingDir, channelId);
52
37
  }
53
38
  /**
54
39
  * Log a message to the channel's log.jsonl raw archive.
55
40
  * This file is cold storage and is not proactively loaded into memory context.
56
- * Returns false if message was already logged (duplicate)
41
+ * Returns false if the message was already logged (duplicate), or the channel is unbound.
57
42
  */
58
43
  async logMessage(channelId, message) {
59
44
  if (this.closed) {
60
45
  throw new Error("Channel archive is closed. Retry after the runtime has restarted.");
61
46
  }
47
+ // Resolved, not created: the appender's own `ensureDir` creates the directory before it
48
+ // writes, and memoizes it — a sync mkdir here would block on every progress update.
49
+ const archiveDir = this.resolveArchiveDir(channelId);
50
+ if (!archiveDir) {
51
+ log.logDebugEvent("channel.archive.skipped", "Archive skipped: channel not bound to a desk", { channelId });
52
+ return false;
53
+ }
62
54
  const dedupeKey = `${channelId}:${message.ts}`;
63
55
  const now = Date.now();
64
56
  const previousLogTime = this.recentlyLogged.get(dedupeKey);
@@ -68,7 +60,7 @@ export class ChannelStore {
68
60
  }
69
61
  this.recentlyLogged.delete(dedupeKey);
70
62
  }
71
- const logPath = join(this.channelArchiveDir(channelId), "log.jsonl");
63
+ const logPath = join(archiveDir, "log.jsonl");
72
64
  if (!message.date) {
73
65
  message.date = formatLocalTime();
74
66
  }
@@ -79,10 +71,14 @@ export class ChannelStore {
79
71
  this.startCleanupTimer();
80
72
  return true;
81
73
  }
82
- async logSubAgentRun(channelId, run) {
74
+ /**
75
+ * Archive a settled run in the desk it ran in. Takes the desk directory rather than resolving the
76
+ * channel: a run that settles after its channel was rebound still belongs to its own desk.
77
+ */
78
+ async logSubAgentRun(deskDir, run) {
83
79
  if (this.closed)
84
80
  return;
85
- const logPath = join(this.channelArchiveDir(channelId), "subagent-runs.jsonl");
81
+ const logPath = getSubAgentRunsLogPath(deskDir);
86
82
  if (!this.archiveAppender.tryAppend({ filePath: logPath, value: run })) {
87
83
  throw new Error("Sub-agent archive queue is full. Retry after pending log writes drain.");
88
84
  }