@oyasmi/pipiclaw 0.9.3 → 0.9.4-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 (336) hide show
  1. package/dist/agent/builtin-commands.d.ts +19 -0
  2. package/dist/agent/builtin-commands.js +80 -0
  3. package/dist/agent/channel-runner.d.ts +20 -208
  4. package/dist/agent/channel-runner.js +167 -1188
  5. package/dist/agent/maintenance-context.d.ts +1 -1
  6. package/dist/agent/maintenance-context.js +1 -1
  7. package/dist/agent/prompt/manifest.d.ts +6 -1
  8. package/dist/agent/prompt/manifest.js +45 -64
  9. package/dist/agent/prompt/resources.d.ts +3 -0
  10. package/dist/agent/prompt/resources.js +11 -2
  11. package/dist/agent/runner-factory.d.ts +1 -43
  12. package/dist/agent/{command-extension.d.ts → session/command-extension.d.ts} +4 -0
  13. package/dist/agent/{command-extension.js → session/command-extension.js} +48 -35
  14. package/dist/agent/{type-guards.d.ts → session/event-guards.d.ts} +2 -2
  15. package/dist/agent/{type-guards.js → session/event-guards.js} +5 -1
  16. package/dist/agent/session/model-selection.d.ts +47 -0
  17. package/dist/agent/session/model-selection.js +99 -0
  18. package/dist/agent/{session-adapter.d.ts → session/session-adapter.d.ts} +21 -1
  19. package/dist/agent/session/session-adapter.js +159 -0
  20. package/dist/agent/{session-events.d.ts → session/session-events.d.ts} +9 -5
  21. package/dist/agent/{session-events.js → session/session-events.js} +24 -13
  22. package/dist/agent/session/session-host.d.ts +200 -0
  23. package/dist/agent/session/session-host.js +621 -0
  24. package/dist/agent/{session-resource-gate.js → session/session-resource-gate.js} +5 -3
  25. package/dist/agent/status-render.d.ts +0 -1
  26. package/dist/agent/status-render.js +4 -16
  27. package/dist/agent/{context-budget.js → turn/context-budget.js} +5 -1
  28. package/dist/agent/{progress-formatter.d.ts → turn/progress-formatter.d.ts} +4 -1
  29. package/dist/agent/{run-queue.d.ts → turn/run-queue.d.ts} +1 -1
  30. package/dist/agent/{run-queue.js → turn/run-queue.js} +10 -5
  31. package/dist/{memory → agent/turn}/task-digest.js +4 -4
  32. package/dist/agent/turn/turn-context.d.ts +14 -0
  33. package/dist/agent/turn/turn-context.js +64 -0
  34. package/dist/agent/turn/turn-error.d.ts +31 -0
  35. package/dist/agent/turn/turn-error.js +68 -0
  36. package/dist/agent/turn/turn-finish.d.ts +50 -0
  37. package/dist/agent/turn/turn-finish.js +188 -0
  38. package/dist/agent/turn/turn-input.d.ts +43 -0
  39. package/dist/agent/turn/turn-input.js +79 -0
  40. package/dist/agent/turn/turn-pipeline.d.ts +68 -0
  41. package/dist/agent/turn/turn-pipeline.js +257 -0
  42. package/dist/agent/{turn-prompt.d.ts → turn/turn-prompt.d.ts} +2 -2
  43. package/dist/agent/{turn-prompt.js → turn/turn-prompt.js} +6 -2
  44. package/dist/agent/{turn-recovery.js → turn/turn-recovery.js} +1 -1
  45. package/dist/agent/{turn-state.d.ts → turn/turn-state.d.ts} +1 -1
  46. package/dist/agent/types.d.ts +43 -2
  47. package/dist/agent/types.js +1 -1
  48. package/dist/{models/auth-cli.d.ts → app/auth-command.d.ts} +1 -1
  49. package/dist/{models/auth-cli.js → app/auth-command.js} +14 -10
  50. package/dist/{runtime/app-home.d.ts → app/home.d.ts} +6 -2
  51. package/dist/{runtime/app-home.js → app/home.js} +22 -75
  52. package/dist/{runtime → app}/proxy.js +4 -2
  53. package/dist/{app-services.d.ts → app/services.d.ts} +5 -5
  54. package/dist/{app-services.js → app/services.js} +17 -14
  55. package/dist/channel/channel-context.d.ts +12 -2
  56. package/dist/channel/channel-context.js +29 -2
  57. package/dist/channel/channel-event.d.ts +10 -2
  58. package/dist/channel/channel-event.js +1 -1
  59. package/dist/channel/channel-index.js +4 -1
  60. package/dist/{runtime/project-commands.d.ts → channel/commands.d.ts} +1 -1
  61. package/dist/{runtime/project-commands.js → channel/commands.js} +17 -16
  62. package/dist/channel/project-scope-store.js +1 -1
  63. package/dist/channel/store.js +4 -0
  64. package/dist/commands/catalog.d.ts +24 -7
  65. package/dist/commands/catalog.js +91 -44
  66. package/dist/commands/reply-limits.d.ts +6 -0
  67. package/dist/commands/reply-limits.js +24 -0
  68. package/dist/{runtime/report-commands.d.ts → commands/router.d.ts} +10 -3
  69. package/dist/{runtime/report-commands.js → commands/router.js} +6 -6
  70. package/dist/{settings.d.ts → config/settings.d.ts} +10 -10
  71. package/dist/{settings.js → config/settings.js} +11 -11
  72. package/dist/{tools/config.d.ts → config/tools.d.ts} +1 -2
  73. package/dist/{tools/config.js → config/tools.js} +5 -2
  74. package/dist/dingtalk/access-token.d.ts +12 -0
  75. package/dist/dingtalk/access-token.js +51 -0
  76. package/dist/dingtalk/api.d.ts +19 -0
  77. package/dist/dingtalk/api.js +67 -0
  78. package/dist/dingtalk/bot.d.ts +97 -0
  79. package/dist/dingtalk/bot.js +389 -0
  80. package/dist/dingtalk/cards.d.ts +56 -0
  81. package/dist/dingtalk/cards.js +318 -0
  82. package/dist/{runtime → dingtalk}/channel-queue.js +4 -1
  83. package/dist/dingtalk/config.d.ts +19 -0
  84. package/dist/dingtalk/config.js +95 -0
  85. package/dist/dingtalk/connection.d.ts +57 -0
  86. package/dist/dingtalk/connection.js +356 -0
  87. package/dist/dingtalk/conversation-meta.d.ts +16 -0
  88. package/dist/dingtalk/conversation-meta.js +77 -0
  89. package/dist/{runtime → dingtalk}/delivery.d.ts +6 -2
  90. package/dist/{runtime → dingtalk}/delivery.js +29 -15
  91. package/dist/dingtalk/handler.d.ts +90 -0
  92. package/dist/dingtalk/handler.js +1 -0
  93. package/dist/dingtalk/inbound.d.ts +66 -0
  94. package/dist/dingtalk/inbound.js +137 -0
  95. package/dist/dingtalk/media.d.ts +66 -0
  96. package/dist/dingtalk/media.js +214 -0
  97. package/dist/events/{event-commands.js → commands.js} +99 -45
  98. package/dist/events/parse.d.ts +14 -0
  99. package/dist/events/parse.js +114 -0
  100. package/dist/events/types.d.ts +93 -0
  101. package/dist/events/types.js +1 -0
  102. package/dist/events/{event-validation.d.ts → validation.d.ts} +14 -3
  103. package/dist/events/{event-validation.js → validation.js} +17 -9
  104. package/dist/events/watcher.d.ts +67 -0
  105. package/dist/events/{events.js → watcher.js} +63 -121
  106. package/dist/executor.js +2 -2
  107. package/dist/index.d.ts +6 -4
  108. package/dist/index.js +2 -2
  109. package/dist/jobs/launch.d.ts +42 -0
  110. package/dist/jobs/launch.js +118 -0
  111. package/dist/{agent/job-manager.d.ts → jobs/manager.d.ts} +1 -67
  112. package/dist/{agent/job-manager.js → jobs/manager.js} +34 -157
  113. package/dist/jobs/record.d.ts +55 -0
  114. package/dist/jobs/record.js +36 -0
  115. package/dist/log.d.ts +12 -2
  116. package/dist/log.js +26 -34
  117. package/dist/main.js +3 -3
  118. package/dist/memory/commands.js +29 -23
  119. package/dist/memory/lifecycle.js +26 -5
  120. package/dist/memory/maintenance-gates.d.ts +2 -2
  121. package/dist/memory/maintenance-state.js +4 -1
  122. package/dist/memory/maintenance-tuning.d.ts +15 -0
  123. package/dist/memory/maintenance-tuning.js +3 -0
  124. package/dist/memory/reflect-job.d.ts +2 -2
  125. package/dist/memory/reflect-job.js +4 -2
  126. package/dist/memory/scheduler.d.ts +3 -3
  127. package/dist/memory/scheduler.js +5 -2
  128. package/dist/memory/sidecar-worker.js +1 -1
  129. package/dist/memory/transcript.js +1 -1
  130. package/dist/{agent/model-fallback.d.ts → models/fallback.d.ts} +6 -6
  131. package/dist/{agent/model-fallback.js → models/fallback.js} +15 -28
  132. package/dist/models/login-identity.d.ts +2 -0
  133. package/dist/models/login-identity.js +83 -0
  134. package/dist/models/provider-login.d.ts +3 -1
  135. package/dist/models/provider-login.js +8 -2
  136. package/dist/models/reference.d.ts +20 -0
  137. package/dist/models/{utils.js → reference.js} +0 -48
  138. package/dist/models/{utils.d.ts → runtime.d.ts} +1 -16
  139. package/dist/models/runtime.js +52 -0
  140. package/dist/paths.d.ts +2 -0
  141. package/dist/paths.js +9 -0
  142. package/dist/playbooks/agent-delegation.md +2 -2
  143. package/dist/playbooks/catalog.d.ts +6 -2
  144. package/dist/playbooks/catalog.js +9 -6
  145. package/dist/runtime/bootstrap.d.ts +7 -17
  146. package/dist/runtime/bootstrap.js +70 -659
  147. package/dist/runtime/durable-dispatch.d.ts +43 -4
  148. package/dist/runtime/durable-dispatch.js +153 -3
  149. package/dist/runtime/inbound-images.d.ts +18 -0
  150. package/dist/runtime/inbound-images.js +51 -0
  151. package/dist/runtime/shutdown.d.ts +39 -0
  152. package/dist/runtime/shutdown.js +120 -0
  153. package/dist/runtime/stop-watchdog.d.ts +16 -0
  154. package/dist/runtime/stop-watchdog.js +32 -0
  155. package/dist/runtime/task-driver.d.ts +6 -4
  156. package/dist/runtime/task-driver.js +58 -23
  157. package/dist/runtime/task-migration.js +25 -8
  158. package/dist/runtime/task-spawn.d.ts +1 -1
  159. package/dist/runtime/task-spawn.js +13 -6
  160. package/dist/runtime/turn-handler.d.ts +47 -0
  161. package/dist/runtime/turn-handler.js +318 -0
  162. package/dist/runtime/turn-run.d.ts +52 -0
  163. package/dist/runtime/turn-run.js +318 -0
  164. package/dist/runtime/wake-coalescing.d.ts +18 -0
  165. package/dist/runtime/wake-coalescing.js +29 -0
  166. package/dist/security/block-message.d.ts +22 -1
  167. package/dist/security/block-message.js +66 -1
  168. package/dist/security/command-guard.js +4 -0
  169. package/dist/security/network.d.ts +2 -1
  170. package/dist/security/network.js +7 -2
  171. package/dist/security/path-guard-check.js +2 -2
  172. package/dist/security/path-guard.js +4 -0
  173. package/dist/security/types.d.ts +1 -2
  174. package/dist/security/types.js +1 -2
  175. package/dist/shared/exact-keys.d.ts +1 -1
  176. package/dist/shared/format.d.ts +12 -0
  177. package/dist/shared/format.js +37 -0
  178. package/dist/shared/host-process.js +16 -4
  179. package/dist/shared/wake-claim.d.ts +1 -1
  180. package/dist/shared/with-timeout.d.ts +3 -2
  181. package/dist/shared/with-timeout.js +14 -3
  182. package/dist/{runtime/skill-commands.d.ts → skills/commands.d.ts} +2 -2
  183. package/dist/{runtime/skill-commands.js → skills/commands.js} +18 -11
  184. package/dist/{agent/workspace-resources.js → skills/loader.js} +1 -1
  185. package/dist/{tools/skill-security.js → skills/validation.js} +4 -0
  186. package/dist/skills/workspace.d.ts +33 -0
  187. package/dist/skills/workspace.js +116 -0
  188. package/dist/{runtime/subagent-commands.d.ts → subagents/commands.d.ts} +9 -7
  189. package/dist/{runtime/subagent-commands.js → subagents/commands.js} +116 -77
  190. package/dist/subagents/completion-wake.d.ts +17 -0
  191. package/dist/subagents/completion-wake.js +76 -0
  192. package/dist/subagents/detached-discovery.js +1 -1
  193. package/dist/subagents/discovery.d.ts +1 -1
  194. package/dist/subagents/discovery.js +2 -2
  195. package/dist/subagents/dispatch.d.ts +33 -0
  196. package/dist/subagents/dispatch.js +190 -0
  197. package/dist/subagents/external/claude-code.js +23 -1
  198. package/dist/subagents/external/dispatch.d.ts +25 -0
  199. package/dist/subagents/external/dispatch.js +90 -0
  200. package/dist/subagents/external/harness.d.ts +1 -1
  201. package/dist/subagents/external/progress-tail.d.ts +18 -1
  202. package/dist/subagents/external/progress-tail.js +64 -17
  203. package/dist/subagents/external/run.d.ts +5 -1
  204. package/dist/subagents/external/run.js +23 -9
  205. package/dist/subagents/external/settlement.d.ts +4 -1
  206. package/dist/subagents/external/settlement.js +1 -1
  207. package/dist/subagents/format.d.ts +1 -1
  208. package/dist/subagents/grace.d.ts +24 -0
  209. package/dist/subagents/grace.js +57 -0
  210. package/dist/subagents/internal/outcome.d.ts +24 -0
  211. package/dist/subagents/internal/outcome.js +149 -0
  212. package/dist/subagents/internal/provider-retry.d.ts +19 -0
  213. package/dist/subagents/internal/provider-retry.js +83 -0
  214. package/dist/subagents/internal/run.d.ts +31 -0
  215. package/dist/subagents/internal/run.js +240 -0
  216. package/dist/subagents/internal/state.d.ts +43 -0
  217. package/dist/subagents/internal/state.js +48 -0
  218. package/dist/subagents/internal/worker.d.ts +51 -0
  219. package/dist/subagents/internal/worker.js +167 -0
  220. package/dist/subagents/result.d.ts +62 -0
  221. package/dist/subagents/result.js +103 -0
  222. package/dist/subagents/run-record.d.ts +222 -0
  223. package/dist/subagents/run-record.js +101 -0
  224. package/dist/subagents/run-registry.d.ts +9 -0
  225. package/dist/subagents/run-registry.js +125 -0
  226. package/dist/subagents/runs.d.ts +16 -232
  227. package/dist/subagents/runs.js +132 -310
  228. package/dist/subagents/task-envelope.d.ts +36 -0
  229. package/dist/subagents/task-envelope.js +168 -0
  230. package/dist/subagents/types.d.ts +154 -0
  231. package/dist/subagents/types.js +1 -0
  232. package/dist/subagents/verification-outcome.d.ts +1 -1
  233. package/dist/subagents/workspace-summary.d.ts +16 -4
  234. package/dist/subagents/workspace-summary.js +74 -18
  235. package/dist/tasks/board.d.ts +10 -3
  236. package/dist/tasks/board.js +38 -3
  237. package/dist/tasks/{work-log.d.ts → bound-work.d.ts} +1 -1
  238. package/dist/tasks/{work-log.js → bound-work.js} +1 -1
  239. package/dist/tasks/brief.js +1 -1
  240. package/dist/{runtime/task-commands.d.ts → tasks/commands.d.ts} +1 -1
  241. package/dist/{runtime/task-commands.js → tasks/commands.js} +117 -67
  242. package/dist/tasks/contract-input.d.ts +1 -1
  243. package/dist/tasks/contract-input.js +1 -1
  244. package/dist/tasks/create.js +1 -1
  245. package/dist/tasks/frontmatter.d.ts +5 -0
  246. package/dist/tasks/frontmatter.js +8 -0
  247. package/dist/tasks/{log.d.ts → loop-log.d.ts} +11 -1
  248. package/dist/tasks/{log.js → loop-log.js} +59 -3
  249. package/dist/tasks/mutation-lock.js +1 -1
  250. package/dist/tasks/session-path.js +1 -1
  251. package/dist/tasks/steer.js +1 -1
  252. package/dist/tasks/store.js +6 -4
  253. package/dist/tasks/ticket.d.ts +3 -1
  254. package/dist/tasks/ticket.js +19 -1
  255. package/dist/tools/bash.d.ts +1 -1
  256. package/dist/tools/bash.js +2 -2
  257. package/dist/tools/{tool-details.d.ts → contract/details.d.ts} +11 -3
  258. package/dist/tools/{tool-details.js → contract/details.js} +42 -5
  259. package/dist/tools/{presentation.js → contract/presentation.js} +3 -3
  260. package/dist/tools/event-manage.d.ts +1 -1
  261. package/dist/tools/event-manage.js +4 -4
  262. package/dist/tools/index.d.ts +5 -3
  263. package/dist/tools/index.js +8 -6
  264. package/dist/tools/job.d.ts +1 -1
  265. package/dist/tools/memory-manage.js +4 -1
  266. package/dist/tools/registry.d.ts +7 -7
  267. package/dist/tools/registry.js +6 -6
  268. package/dist/tools/session-search.d.ts +1 -1
  269. package/dist/tools/skill.d.ts +2 -34
  270. package/dist/tools/skill.js +8 -116
  271. package/dist/tools/subagent-manage.d.ts +1 -1
  272. package/dist/tools/subagent-manage.js +19 -13
  273. package/dist/tools/subagent.d.ts +46 -0
  274. package/dist/tools/subagent.js +127 -0
  275. package/dist/tools/{task-manage.d.ts → task-manage/factory.d.ts} +7 -3
  276. package/dist/tools/{task-manage.js → task-manage/factory.js} +7 -7
  277. package/dist/tools/task-manage/lifecycle.js +2 -2
  278. package/dist/tools/task-manage/log-view.js +2 -2
  279. package/dist/tools/task-manage/shared.js +3 -3
  280. package/dist/tools/task-manage/step-end.js +7 -4
  281. package/dist/tools/task-manage/types.d.ts +1 -1
  282. package/dist/tools/web-fetch.d.ts +1 -1
  283. package/dist/tools/web-search.d.ts +1 -1
  284. package/dist/tui/app.d.ts +1 -1
  285. package/dist/tui/app.js +6 -6
  286. package/dist/tui/cli.d.ts +1 -1
  287. package/dist/tui/cli.js +3 -3
  288. package/dist/tui/commands.js +4 -4
  289. package/dist/tui/frontend.d.ts +44 -0
  290. package/dist/tui/frontend.js +13 -0
  291. package/dist/tui/pitui-frontend.d.ts +1 -1
  292. package/dist/tui/pitui-frontend.js +1 -1
  293. package/dist/tui/plain-frontend.d.ts +1 -1
  294. package/dist/tui/renderer.d.ts +3 -42
  295. package/dist/tui/renderer.js +3 -2
  296. package/dist/tui/terminal-context.d.ts +2 -2
  297. package/dist/tui/terminal-media-sender.d.ts +1 -1
  298. package/dist/tui/turn-controller.d.ts +3 -1
  299. package/dist/tui/turn-controller.js +36 -21
  300. package/dist/usage/ledger.js +7 -1
  301. package/dist/usage/render.js +3 -1
  302. package/dist/{shared/types.d.ts → usage/totals.d.ts} +3 -0
  303. package/dist/web/client.d.ts +5 -1
  304. package/dist/web/client.js +4 -0
  305. package/dist/web/config.d.ts +1 -1
  306. package/dist/web/fetch.d.ts +1 -1
  307. package/dist/web/search-providers.d.ts +1 -1
  308. package/dist/web/search.d.ts +1 -1
  309. package/package.json +9 -7
  310. package/dist/agent/session-adapter.js +0 -77
  311. package/dist/events/events.d.ts +0 -159
  312. package/dist/runtime/dingtalk.d.ts +0 -272
  313. package/dist/runtime/dingtalk.js +0 -1416
  314. package/dist/subagents/tool.d.ts +0 -206
  315. package/dist/subagents/tool.js +0 -1088
  316. /package/dist/{runtime → agent/prompt}/workspace-templates.d.ts +0 -0
  317. /package/dist/{runtime → agent/prompt}/workspace-templates.js +0 -0
  318. /package/dist/agent/{session-resource-gate.d.ts → session/session-resource-gate.d.ts} +0 -0
  319. /package/dist/agent/{context-budget.d.ts → turn/context-budget.d.ts} +0 -0
  320. /package/dist/agent/{progress-formatter.js → turn/progress-formatter.js} +0 -0
  321. /package/dist/{memory → agent/turn}/task-digest.d.ts +0 -0
  322. /package/dist/agent/{turn-recovery.d.ts → turn/turn-recovery.d.ts} +0 -0
  323. /package/dist/agent/{turn-state.js → turn/turn-state.js} +0 -0
  324. /package/dist/{runtime → app}/proxy.d.ts +0 -0
  325. /package/dist/{runtime → dingtalk}/channel-queue.d.ts +0 -0
  326. /package/dist/{runtime → dingtalk}/inbound-media.d.ts +0 -0
  327. /package/dist/{runtime → dingtalk}/inbound-media.js +0 -0
  328. /package/dist/events/{event-commands.d.ts → commands.d.ts} +0 -0
  329. /package/dist/{agent/workspace-resources.d.ts → skills/loader.d.ts} +0 -0
  330. /package/dist/{tools/skill-security.d.ts → skills/validation.d.ts} +0 -0
  331. /package/dist/tasks/{ledger.d.ts → contract.d.ts} +0 -0
  332. /package/dist/tasks/{ledger.js → contract.js} +0 -0
  333. /package/dist/tools/{subagent-tool-names.d.ts → contract/names.d.ts} +0 -0
  334. /package/dist/tools/{subagent-tool-names.js → contract/names.js} +0 -0
  335. /package/dist/tools/{presentation.d.ts → contract/presentation.d.ts} +0 -0
  336. /package/dist/{shared/types.js → usage/totals.js} +0 -0
@@ -2,13 +2,14 @@ import { createHash } from "node:crypto";
2
2
  import { join } from "node:path";
3
3
  import { discoverWorkspaceChannelIds } from "../channel/channel-index.js";
4
4
  import { dedupeChannelIdsByDirectory, getChannelDir, isChannelId } from "../channel/channel-paths.js";
5
+ import { TASK_DRIVER_SETTINGS } from "../config/settings.js";
5
6
  import * as log from "../log.js";
6
7
  import { PLAYBOOKS_DIR } from "../paths.js";
7
- import { TASK_DRIVER_SETTINGS } from "../settings.js";
8
8
  import { errorMessage } from "../shared/text-utils.js";
9
9
  import { checkBudget, IDLE_STEP_LIMIT } from "../tasks/budget.js";
10
- import { readActiveTasks } from "../tasks/ledger.js";
11
- import { readTaskLog } from "../tasks/log.js";
10
+ import { readActiveTasks } from "../tasks/contract.js";
11
+ import { taskStateLabel } from "../tasks/frontmatter.js";
12
+ import { readTaskLog } from "../tasks/loop-log.js";
12
13
  import { expireTicket, pauseTask, redeemTicket } from "../tasks/store.js";
13
14
  /** Short debounce so a burst of nudges collapses into a single rescan. */
14
15
  const NUDGE_DEBOUNCE_MS = 50;
@@ -79,17 +80,22 @@ export function createTaskDriverEvent(channelId, entry, nowMs) {
79
80
  /**
80
81
  * The deterministic receipt a runtime stop produces (spec 051, D11). It is delivered directly,
81
82
  * without opening a model turn: the user needs to know their task stopped, and spending an LLM
82
- * call to phrase that is exactly the kind of cost this design removes.
83
+ * call to phrase that is exactly the kind of cost this design removes. A budget stop also says
84
+ * how to resume with more of the dimension that ran out (spec 053, D6).
83
85
  */
84
- export function taskStopReceipt(channelId, entry, reason, nowMs) {
86
+ export function taskStopReceipt(channelId, entry, reason, nowMs, breach) {
87
+ const usage = entry.fields.usage ? ` · 已用 ${entry.fields.usage.steps} 步` : "";
88
+ const grant = breach === "steps" ? " +steps 10" : breach === "usd" ? " +usd 5" : "";
89
+ const resumeLabel = grant ? "继续并追加预算" : "继续";
85
90
  return {
86
91
  ...channelType(channelId),
87
92
  channelId,
88
93
  user: "TASK_DRIVER",
89
94
  userName: "TASK_DRIVER",
90
- text: `任务 ${entry.id}(${entry.title})已停止自动执行:${reason}\n` +
91
- `当前阶段:${entry.fields.state}${entry.fields.usage ? `;已用 ${entry.fields.usage.steps} 步` : ""}\n` +
92
- `查看:/tasks show ${entry.id}\n继续:/tasks resume ${entry.id}\n不再需要:让 Agent cancel 该任务。`,
95
+ text: `🛑 任务 \`${entry.id}\`(${entry.title})已停止自动执行:${reason}。\n` +
96
+ `状态:${taskStateLabel(entry.fields.state)}${usage}\n` +
97
+ `查看:\`/tasks show ${entry.id}\`;${resumeLabel}:\`/tasks resume ${entry.id}${grant}\`\n` +
98
+ "不再需要:直接告诉我取消该任务。",
93
99
  ts: String(nowMs),
94
100
  // Keyed on the cause, not the moment: re-detecting the same stop before the user has acted
95
101
  // must not queue a second identical receipt, while a different cause still does.
@@ -134,14 +140,18 @@ export class TaskDriver {
134
140
  this.options.onDispatch?.(event, accepted);
135
141
  }
136
142
  catch (error) {
137
- log.logWarning("Task driver dispatch observer failed", errorMessage(error));
143
+ log.logWarn("taskdriver.observer.failed", "Task driver dispatch observer failed", {
144
+ reason: errorMessage(error),
145
+ });
138
146
  }
139
147
  }
140
148
  start() {
141
149
  if (this.loopActive)
142
150
  return;
143
151
  this.loopActive = true;
144
- log.logInfo("Task driver started", `schedule timezone=${Intl.DateTimeFormat().resolvedOptions().timeZone} (host)`);
152
+ log.logInfoEvent("taskdriver.started", "Task driver started", {
153
+ fields: { timezone: Intl.DateTimeFormat().resolvedOptions().timeZone },
154
+ });
145
155
  void this.tick();
146
156
  }
147
157
  stop() {
@@ -178,7 +188,7 @@ export class TaskDriver {
178
188
  await this.runOnce();
179
189
  }
180
190
  catch (error) {
181
- log.logWarning("Task driver tick failed", errorMessage(error));
191
+ log.logWarn("taskdriver.tick.failed", "Task driver tick failed", { reason: errorMessage(error) });
182
192
  }
183
193
  this.scheduleNext();
184
194
  }
@@ -203,12 +213,15 @@ export class TaskDriver {
203
213
  this.nextWakeMs = candidate;
204
214
  }
205
215
  /** Stop one task and notify. Delivery failure is logged, never fatal: the durable stop already happened. */
206
- async notifyStop(channelId, entry, reason, nowMs) {
216
+ async notifyStop(channelId, entry, reason, nowMs, breach) {
207
217
  try {
208
- await this.options.notify?.(taskStopReceipt(channelId, entry, reason, nowMs));
218
+ await this.options.notify?.(taskStopReceipt(channelId, entry, reason, nowMs, breach));
209
219
  }
210
220
  catch (error) {
211
- log.logWarning(`[${channelId}] Task stop receipt failed`, errorMessage(error));
221
+ log.logWarn("taskdriver.receipt.failed", "Task stop receipt failed", {
222
+ channelId: channelId,
223
+ reason: errorMessage(error),
224
+ });
212
225
  }
213
226
  }
214
227
  /**
@@ -237,11 +250,19 @@ export class TaskDriver {
237
250
  continue;
238
251
  changed = true;
239
252
  if (expiry.outcome === "paused") {
240
- log.logWarning(`[${channelId}] Task ${entry.id} paused: ticket expired twice`, expiry.ticket);
241
- await this.notifyStop(channelId, entry, `等待未兑现(${expiry.ticket}),已连续两次超过兜底时限`, nowMs);
253
+ log.logWarn("taskdriver.task.paused", "Task paused: ticket expired twice", {
254
+ channelId: channelId,
255
+ reason: expiry.ticket,
256
+ fields: { taskId: entry.id, cause: "ticket_expired" },
257
+ });
258
+ await this.notifyStop(channelId, entry, `等待条件一直未满足(${expiry.ticket}),已连续两次超过最晚检查时间`, nowMs);
242
259
  }
243
260
  else {
244
- log.logInfo(`[${channelId}] Task ${entry.id} reopened after its ticket expired`, expiry.ticket);
261
+ log.logInfoEvent("taskdriver.task.reopened", "Task reopened after its ticket expired", {
262
+ channelId: channelId,
263
+ reason: expiry.ticket,
264
+ fields: { taskId: entry.id },
265
+ });
245
266
  }
246
267
  continue;
247
268
  }
@@ -268,10 +289,14 @@ export class TaskDriver {
268
289
  const idle = steps.every((record) => record.kind === "step" && record.tools.length === 0);
269
290
  if (!idle)
270
291
  continue;
271
- const reason = `连续 ${IDLE_STEP_LIMIT} 步没有任何工具调用,循环已停下等你确认`;
292
+ const reason = `连续 ${IDLE_STEP_LIMIT} 步没有任何工具调用,已停下等你确认`;
272
293
  if (await pauseTask(channelDir, entry.id, { by: "runtime", reason })) {
273
294
  paused = true;
274
- log.logWarning(`[${channelId}] Task ${entry.id} paused: idle loop`, reason);
295
+ log.logWarn("taskdriver.task.paused", "Task paused: idle loop", {
296
+ channelId: channelId,
297
+ reason: reason,
298
+ fields: { taskId: entry.id, cause: "idle" },
299
+ });
275
300
  await this.notifyStop(channelId, entry, reason, nowMs);
276
301
  }
277
302
  }
@@ -322,8 +347,12 @@ export class TaskDriver {
322
347
  continue;
323
348
  }
324
349
  if (await pauseTask(channelDir, entry.id, { by: "runtime", reason: status.reason })) {
325
- log.logWarning(`[${channelId}] Task ${entry.id} paused: budget ${status.breach}`, status.reason);
326
- await this.notifyStop(channelId, entry, status.reason, nowMs);
350
+ log.logWarn("taskdriver.task.paused", "Task paused: budget exhausted", {
351
+ channelId: channelId,
352
+ reason: status.reason,
353
+ fields: { taskId: entry.id, cause: `budget_${status.breach}` },
354
+ });
355
+ await this.notifyStop(channelId, entry, status.reason, nowMs, status.breach);
327
356
  }
328
357
  }
329
358
  const idled = await this.detectIdleLoop(channelDir, channelId, budgeted, nowMs);
@@ -358,10 +387,16 @@ export class TaskDriver {
358
387
  if (accepted) {
359
388
  dispatched++;
360
389
  lastDispatchOffset = offset;
361
- log.logInfo(`[${channelId}] Task driver enqueued ${entry.id}`);
390
+ log.logDebugEvent("taskdriver.step.enqueued", "Task step enqueued", {
391
+ channelId: channelId,
392
+ fields: { taskId: entry.id },
393
+ });
362
394
  }
363
395
  else {
364
- log.logWarning(`[${channelId}] Task driver could not enqueue ${entry.id}`, "channel queue unavailable");
396
+ log.logWarn("taskdriver.step.enqueue_failed", "Task step not enqueued: channel queue unavailable", {
397
+ channelId: channelId,
398
+ fields: { taskId: entry.id },
399
+ });
365
400
  }
366
401
  }
367
402
  for (const key of this.lastDispatchMs.keys()) {
@@ -8,9 +8,9 @@ import { formatLocalTime } from "../shared/local-time.js";
8
8
  import { errorMessage } from "../shared/text-utils.js";
9
9
  import { isPlainObject } from "../shared/type-guards.js";
10
10
  import { createUsage } from "../tasks/budget.js";
11
+ import { extractTaskTitle, findTaskSectionBounds, normalizeTaskId, parseTaskItems, taskBody, } from "../tasks/contract.js";
11
12
  import { validateTaskContractInput } from "../tasks/contract-input.js";
12
- import { extractTaskTitle, findTaskSectionBounds, normalizeTaskId, parseTaskItems, taskBody } from "../tasks/ledger.js";
13
- import { appendTaskLog } from "../tasks/log.js";
13
+ import { appendTaskLog } from "../tasks/loop-log.js";
14
14
  import { withTaskMutation } from "../tasks/mutation-lock.js";
15
15
  import { ArchiveUndoError, archiveTask, readStoredTask, writeStoredTask } from "../tasks/store.js";
16
16
  import { discoverTaskChannels } from "./task-driver.js";
@@ -291,7 +291,10 @@ async function convertOneTask(workspaceDir, channelId, channelDir, id, now) {
291
291
  stage = "archive";
292
292
  await archiveTask(channelDir, id, "cancelled");
293
293
  }
294
- log.logInfo(`Task ${id} converted to v5`, `state=${fields.state}${fields.paused ? " paused" : ""}${origin ? ` origin=${origin}` : ""}${retire ? " (retired)" : ""}`);
294
+ log.logInfoEvent("tasks.migration.converted", "Task converted to v5", {
295
+ reason: `state=${fields.state}${fields.paused ? " paused" : ""}${origin ? ` origin=${origin}` : ""}${retire ? " (retired)" : ""}`,
296
+ fields: { taskId: id },
297
+ });
295
298
  return true;
296
299
  }
297
300
  catch (error) {
@@ -400,7 +403,9 @@ export async function migrateTasksToV5(workspaceDir, stateDir) {
400
403
  }
401
404
  catch (error) {
402
405
  // An undiscoverable workspace is not an empty one: no task was looked at, so no marker.
403
- log.logWarning("Channels of the workspace could not be discovered", errorMessage(error));
406
+ log.logWarn("tasks.migration.discover_failed", "Workspace channels could not be discovered", {
407
+ reason: errorMessage(error),
408
+ });
404
409
  failures.push({ channelId: "*", stage: "discover", error: errorMessage(error), rollback: "ok" });
405
410
  }
406
411
  for (const channelId of channelIds) {
@@ -411,7 +416,10 @@ export async function migrateTasksToV5(workspaceDir, stateDir) {
411
416
  }
412
417
  catch (error) {
413
418
  // An unreadable task directory is not an empty one: the originals were not looked at.
414
- log.logWarning(`Tasks of ${channelId} could not be listed`, errorMessage(error));
419
+ log.logWarn("tasks.migration.list_failed", "Channel tasks could not be listed", {
420
+ channelId: channelId,
421
+ reason: errorMessage(error),
422
+ });
415
423
  failures.push({ channelId, stage: "scan", error: errorMessage(error), rollback: "ok" });
416
424
  }
417
425
  for (const filename of filenames) {
@@ -421,7 +429,10 @@ export async function migrateTasksToV5(workspaceDir, stateDir) {
421
429
  converted++;
422
430
  }
423
431
  catch (error) {
424
- log.logWarning(`Task ${id} could not be converted to v5`, errorMessage(error));
432
+ log.logWarn("tasks.migration.convert_failed", "Task could not be converted to v5", {
433
+ reason: errorMessage(error),
434
+ fields: { taskId: id },
435
+ });
425
436
  failures.push({
426
437
  channelId,
427
438
  task: id,
@@ -432,7 +443,10 @@ export async function migrateTasksToV5(workspaceDir, stateDir) {
432
443
  }
433
444
  }
434
445
  await moveTaskOwnedEvents(workspaceDir, channelId, channelDir).catch((error) => {
435
- log.logWarning(`Task-owned events of ${channelId} could not be moved`, errorMessage(error));
446
+ log.logWarn("tasks.migration.events_failed", "Task-owned events could not be moved", {
447
+ channelId: channelId,
448
+ reason: errorMessage(error),
449
+ });
436
450
  failures.push({ channelId, stage: "events", error: errorMessage(error), rollback: "ok" });
437
451
  });
438
452
  }
@@ -444,5 +458,8 @@ export async function migrateTasksToV5(workspaceDir, stateDir) {
444
458
  await rm(reportPath, { force: true });
445
459
  await writeFile(markerPath, `${formatLocalTime(now)}\n`, "utf-8");
446
460
  if (converted > 0)
447
- log.logInfo("Task conversion to v5 complete", `${converted} task(s)`);
461
+ log.logInfoEvent("tasks.migration.done", "Task conversion to v5 complete", {
462
+ reason: `${converted} task(s)`,
463
+ fields: { converted },
464
+ });
448
465
  }
@@ -1,4 +1,4 @@
1
- import type { SpawnTaskRequest, SpawnTaskResult } from "../events/events.js";
1
+ import type { SpawnTaskRequest, SpawnTaskResult } from "../events/types.js";
2
2
  export interface TaskSpawnerOptions {
3
3
  /** The channel's directory under the workspace. */
4
4
  getChannelDir: (channelId: string) => string;
@@ -1,9 +1,9 @@
1
1
  import { existsSync } from "node:fs";
2
2
  import * as log from "../log.js";
3
+ import { readActiveTasks } from "../tasks/contract.js";
3
4
  import { createTaskDocument, instanceStamp } from "../tasks/create.js";
4
- import { readActiveTasks } from "../tasks/ledger.js";
5
+ import { taskStateLabel } from "../tasks/frontmatter.js";
5
6
  import { archivedTaskPath, taskPath, tasksDir } from "../tasks/store.js";
6
- const STATE_LABEL = { open: "进行中", parked: "等待中" };
7
7
  /**
8
8
  * Create the task instance a template event asks for (spec 052, D2).
9
9
  *
@@ -27,11 +27,15 @@ export function createTaskSpawner(options) {
27
27
  }
28
28
  const active = (await readActiveTasks(tasksDir(channelDir))).find((entry) => entry.fields.origin === request.eventName && !entry.fields.outcome);
29
29
  if (active) {
30
- const state = STATE_LABEL[active.fields.state] ?? active.fields.state;
30
+ const state = `${taskStateLabel(active.fields.state)}${active.fields.paused ? ",已暂停" : ""}`;
31
31
  await options
32
- .notify(request.channelId, `周期任务 ${request.eventName} 本次(${instanceStamp(request.occurrence)})未启动:上一实例 ${active.id} 仍在进行(${state}${active.fields.paused ? ",已暂停" : ""})。\n查看:/tasks show ${active.id}`)
32
+ .notify(request.channelId, `⚠️ 周期任务 \`${request.eventName}\` 本次(${instanceStamp(request.occurrence)})未启动:上一个实例 \`${active.id}\` 仍未结束(${state})。\n查看:\`/tasks show ${active.id}\``)
33
33
  .catch((error) => {
34
- log.logWarning(`Could not deliver skip receipt for ${request.eventName}`, String(error));
34
+ log.logWarn("tasks.spawn.receipt_failed", "Skip receipt delivery failed", {
35
+ channelId: request.channelId,
36
+ reason: String(error),
37
+ fields: { event: request.eventName },
38
+ });
35
39
  });
36
40
  return { outcome: "skipped", id: active.id };
37
41
  }
@@ -42,7 +46,10 @@ export function createTaskSpawner(options) {
42
46
  origin: request.eventName,
43
47
  now: request.occurrence,
44
48
  });
45
- log.logInfo(`[${request.channelId}] Event ${request.eventName} spawned task ${id}`);
49
+ log.logInfoEvent("tasks.spawn.created", "Event spawned a task instance", {
50
+ channelId: request.channelId,
51
+ fields: { event: request.eventName, taskId: id },
52
+ });
46
53
  options.nudge();
47
54
  return { outcome: "spawned", id };
48
55
  };
@@ -0,0 +1,47 @@
1
+ import type { AgentRunner } from "../agent/types.js";
2
+ import type { BootstrapPaths } from "../app/home.js";
3
+ import type { ChannelIndex } from "../channel/channel-index.js";
4
+ import type { ChannelStore } from "../channel/store.js";
5
+ import type { DingTalkHandler } from "../dingtalk/handler.js";
6
+ import type { JobRuntime } from "../jobs/manager.js";
7
+ import type { SubAgentRuntime } from "../subagents/run-registry.js";
8
+ import type { DurableDispatchService } from "./durable-dispatch.js";
9
+ import type { WakeTaskTransitionHooks } from "./task-wake.js";
10
+ import { type ArchivedMessage } from "./turn-run.js";
11
+ export interface TurnHandlerOptions {
12
+ paths: BootstrapPaths;
13
+ /** Override the `/stop` watchdog's grace window (tests use a short one). */
14
+ stopForceEndGraceMs?: number;
15
+ /** Test-only fault seams for the atomic structured-wake task transition. */
16
+ wakeTransitionHooks?: Partial<Record<"job" | "subagent", WakeTaskTransitionHooks>>;
17
+ }
18
+ /**
19
+ * Everything the turn handler needs from `createRuntimeContext`'s scope (review 2026-08-30):
20
+ * pulled out so the handler's dependencies are a reviewable, typed list instead of "however many
21
+ * closures happen to be in scope" in an 850-line inline object literal. The four `get*`/`is*`
22
+ * accessors exist only for state this function's own scope assigns *after* the handler is built
23
+ * (`durableDispatch`, `executor`, `taskDriver`) or mutates later (`shuttingDown`) — a handler
24
+ * method only ever runs long after all of them are set, but a plain value captured at
25
+ * construction time would freeze a stale snapshot instead of observing the real one.
26
+ */
27
+ export interface TurnHandlerDeps {
28
+ options: TurnHandlerOptions;
29
+ store: ChannelStore;
30
+ channelRunners: Map<string, AgentRunner>;
31
+ sessionResetChains: Map<string, Promise<void>>;
32
+ activeTasks: Set<Promise<void>>;
33
+ stopWatchdogs: Set<string>;
34
+ channelIndex: ChannelIndex;
35
+ archiveIncomingMessage: (channelId: string, message: ArchivedMessage, contextLabel: string) => Promise<void>;
36
+ getRunner: (channelId: string) => AgentRunner;
37
+ cliVersion: string;
38
+ startedAt: number;
39
+ isShuttingDown: () => boolean;
40
+ getDurableDispatch: () => DurableDispatchService | undefined;
41
+ getJobs: () => JobRuntime;
42
+ getRuns: () => SubAgentRuntime;
43
+ getTaskDriver: () => {
44
+ nudge?(): void;
45
+ };
46
+ }
47
+ export declare function createTurnHandler(deps: TurnHandlerDeps): DingTalkHandler;