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

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 (272) hide show
  1. package/README.en.md +9 -11
  2. package/README.md +9 -11
  3. package/dist/agent/channel-runner.d.ts +12 -24
  4. package/dist/agent/channel-runner.js +74 -30
  5. package/dist/agent/maintenance-context.d.ts +2 -0
  6. package/dist/agent/maintenance-context.js +15 -0
  7. package/dist/agent/prompt/sections.js +8 -6
  8. package/dist/agent/prompt/types.d.ts +1 -1
  9. package/dist/agent/runner-factory.js +1 -1
  10. package/dist/agent/session/command-extension.js +11 -0
  11. package/dist/agent/session/compaction-summary.js +6 -2
  12. package/dist/agent/session/execution-trace.d.ts +14 -0
  13. package/dist/agent/session/execution-trace.js +57 -0
  14. package/dist/agent/session/session-events.d.ts +2 -7
  15. package/dist/agent/session/session-events.js +35 -12
  16. package/dist/agent/session/session-host.d.ts +12 -22
  17. package/dist/agent/session/session-host.js +46 -65
  18. package/dist/agent/turn/task-digest.d.ts +1 -2
  19. package/dist/agent/turn/task-digest.js +33 -33
  20. package/dist/agent/turn/turn-context.d.ts +1 -1
  21. package/dist/agent/turn/turn-context.js +1 -1
  22. package/dist/agent/turn/turn-finish.d.ts +2 -2
  23. package/dist/agent/turn/turn-finish.js +3 -2
  24. package/dist/agent/turn/turn-pipeline.d.ts +4 -2
  25. package/dist/agent/turn/turn-pipeline.js +33 -5
  26. package/dist/agent/types.d.ts +64 -14
  27. package/dist/agent/types.js +3 -0
  28. package/dist/app/home.js +3 -4
  29. package/dist/channel/channel-event.d.ts +4 -12
  30. package/dist/channel/ingress.d.ts +1 -9
  31. package/dist/channel/store.d.ts +1 -1
  32. package/dist/commands/catalog.js +25 -19
  33. package/dist/commands/router.d.ts +5 -2
  34. package/dist/commands/router.js +9 -4
  35. package/dist/config/settings.d.ts +5 -12
  36. package/dist/config/settings.js +0 -7
  37. package/dist/config/tools.d.ts +2 -2
  38. package/dist/config/tools.js +2 -2
  39. package/dist/dingtalk/bot.d.ts +2 -0
  40. package/dist/dingtalk/bot.js +4 -0
  41. package/dist/dingtalk/cards.d.ts +7 -0
  42. package/dist/dingtalk/cards.js +32 -2
  43. package/dist/dingtalk/delivery.d.ts +5 -1
  44. package/dist/dingtalk/delivery.js +157 -142
  45. package/dist/dingtalk/progress.d.ts +55 -6
  46. package/dist/dingtalk/progress.js +195 -16
  47. package/dist/file-store.d.ts +4 -0
  48. package/dist/file-store.js +6 -1
  49. package/dist/jobs/completion-observer.d.ts +20 -0
  50. package/dist/jobs/completion-observer.js +107 -0
  51. package/dist/jobs/completion-signal.d.ts +9 -0
  52. package/dist/jobs/completion-signal.js +32 -0
  53. package/dist/jobs/completion-wake.d.ts +1 -1
  54. package/dist/jobs/completion-wake.js +2 -13
  55. package/dist/jobs/manager.d.ts +11 -29
  56. package/dist/jobs/manager.js +103 -176
  57. package/dist/jobs/probe.d.ts +3 -0
  58. package/dist/jobs/probe.js +42 -0
  59. package/dist/jobs/record.d.ts +3 -6
  60. package/dist/jobs/runtime.d.ts +28 -0
  61. package/dist/jobs/runtime.js +69 -0
  62. package/dist/memory/boundary-work.d.ts +2 -0
  63. package/dist/memory/boundary-work.js +79 -37
  64. package/dist/memory/commands.js +4 -1
  65. package/dist/memory/index-budget.js +18 -15
  66. package/dist/memory/journal.js +2 -1
  67. package/dist/memory/lifecycle.d.ts +4 -8
  68. package/dist/memory/lifecycle.js +21 -51
  69. package/dist/memory/reflect-job.d.ts +0 -6
  70. package/dist/memory/reflect-job.js +13 -34
  71. package/dist/memory/reflect.d.ts +4 -0
  72. package/dist/memory/reflect.js +32 -5
  73. package/dist/memory/review-log.d.ts +13 -0
  74. package/dist/memory/review-log.js +49 -0
  75. package/dist/memory/search.js +7 -4
  76. package/dist/memory/source-window.d.ts +18 -1
  77. package/dist/memory/source-window.js +59 -9
  78. package/dist/memory/store.d.ts +2 -0
  79. package/dist/memory/store.js +45 -13
  80. package/dist/playbooks/agent-delegation.md +10 -4
  81. package/dist/playbooks/background-jobs.md +4 -4
  82. package/dist/playbooks/event-scheduling.md +2 -2
  83. package/dist/playbooks/memory-and-learning.md +3 -3
  84. package/dist/playbooks/outbound-media.md +2 -2
  85. package/dist/playbooks/runtime-orientation.md +10 -6
  86. package/dist/playbooks/task-lead.md +54 -33
  87. package/dist/runtime/bootstrap.d.ts +11 -24
  88. package/dist/runtime/bootstrap.js +92 -59
  89. package/dist/runtime/channel-queue.d.ts +2 -0
  90. package/dist/runtime/channel-queue.js +5 -1
  91. package/dist/runtime/durable-dispatch.d.ts +7 -0
  92. package/dist/runtime/durable-dispatch.js +66 -7
  93. package/dist/runtime/ingress-busy.d.ts +1 -2
  94. package/dist/runtime/ingress-busy.js +2 -35
  95. package/dist/runtime/ingress-commands.d.ts +1 -1
  96. package/dist/runtime/ingress-commands.js +28 -20
  97. package/dist/runtime/ingress-deps.d.ts +4 -7
  98. package/dist/runtime/ingress.js +23 -13
  99. package/dist/runtime/shutdown.d.ts +1 -1
  100. package/dist/runtime/shutdown.js +2 -2
  101. package/dist/runtime/task-close.d.ts +4 -0
  102. package/dist/runtime/task-close.js +102 -0
  103. package/dist/runtime/task-control.d.ts +34 -0
  104. package/dist/runtime/task-control.js +225 -0
  105. package/dist/runtime/task-input.d.ts +13 -0
  106. package/dist/runtime/task-input.js +129 -0
  107. package/dist/runtime/task-lane-types.d.ts +123 -0
  108. package/dist/runtime/task-lane-types.js +14 -0
  109. package/dist/runtime/task-lane.d.ts +99 -0
  110. package/dist/runtime/task-lane.js +703 -0
  111. package/dist/runtime/task-pending.d.ts +8 -0
  112. package/dist/runtime/task-pending.js +40 -0
  113. package/dist/runtime/task-spawn.d.ts +4 -4
  114. package/dist/runtime/task-spawn.js +4 -4
  115. package/dist/runtime/task-step.d.ts +32 -0
  116. package/dist/runtime/task-step.js +276 -0
  117. package/dist/runtime/turn-run.d.ts +4 -16
  118. package/dist/runtime/turn-run.js +17 -155
  119. package/dist/runtime/wake-coalescing.d.ts +1 -3
  120. package/dist/runtime/wake-coalescing.js +2 -4
  121. package/dist/shared/format.d.ts +0 -6
  122. package/dist/shared/format.js +0 -17
  123. package/dist/shared/host-process.d.ts +2 -0
  124. package/dist/shared/host-process.js +16 -1
  125. package/dist/shared/jsonl-appender.d.ts +5 -0
  126. package/dist/shared/jsonl-appender.js +25 -2
  127. package/dist/skills/catalog.d.ts +9 -0
  128. package/dist/skills/catalog.js +34 -0
  129. package/dist/skills/commands.d.ts +3 -12
  130. package/dist/skills/commands.js +36 -22
  131. package/dist/skills/loader.d.ts +2 -31
  132. package/dist/skills/loader.js +101 -68
  133. package/dist/skills/roots.d.ts +12 -0
  134. package/dist/skills/roots.js +18 -0
  135. package/dist/skills/types.d.ts +24 -0
  136. package/dist/skills/types.js +1 -0
  137. package/dist/subagents/completion-wake.js +0 -10
  138. package/dist/subagents/discovery.d.ts +1 -1
  139. package/dist/subagents/discovery.js +10 -16
  140. package/dist/subagents/dispatch.js +8 -16
  141. package/dist/subagents/external/cursor-cli.d.ts +2 -0
  142. package/dist/subagents/external/cursor-cli.js +256 -0
  143. package/dist/subagents/external/harness.d.ts +5 -5
  144. package/dist/subagents/external/reconciliation.d.ts +7 -0
  145. package/dist/subagents/external/reconciliation.js +105 -0
  146. package/dist/subagents/external/recovery.d.ts +20 -0
  147. package/dist/subagents/external/recovery.js +71 -0
  148. package/dist/subagents/external/registry.js +2 -0
  149. package/dist/subagents/external/role-warnings.d.ts +17 -0
  150. package/dist/subagents/external/role-warnings.js +42 -0
  151. package/dist/subagents/external/run.js +18 -22
  152. package/dist/subagents/format.d.ts +1 -1
  153. package/dist/subagents/format.js +1 -1
  154. package/dist/subagents/grace.d.ts +2 -2
  155. package/dist/subagents/grace.js +3 -3
  156. package/dist/subagents/internal/run.js +7 -5
  157. package/dist/subagents/internal/worker.js +31 -2
  158. package/dist/subagents/result.js +2 -3
  159. package/dist/subagents/run-record.d.ts +4 -7
  160. package/dist/subagents/run-record.js +2 -2
  161. package/dist/subagents/runs.d.ts +33 -46
  162. package/dist/subagents/runs.js +126 -245
  163. package/dist/subagents/task-settlement.d.ts +3 -0
  164. package/dist/subagents/task-settlement.js +20 -0
  165. package/dist/subagents/types.d.ts +1 -2
  166. package/dist/subagents/workspace-summary.js +5 -1
  167. package/dist/tasks/archive.d.ts +11 -0
  168. package/dist/tasks/archive.js +141 -0
  169. package/dist/tasks/board.d.ts +5 -12
  170. package/dist/tasks/board.js +4 -30
  171. package/dist/tasks/bound-work.d.ts +12 -6
  172. package/dist/tasks/bound-work.js +37 -24
  173. package/dist/tasks/brief.d.ts +38 -14
  174. package/dist/tasks/brief.js +128 -70
  175. package/dist/tasks/budget.d.ts +3 -3
  176. package/dist/tasks/budget.js +9 -4
  177. package/dist/tasks/commands.d.ts +14 -9
  178. package/dist/tasks/commands.js +190 -121
  179. package/dist/tasks/completion.d.ts +12 -0
  180. package/dist/tasks/completion.js +35 -0
  181. package/dist/tasks/contract.d.ts +8 -17
  182. package/dist/tasks/contract.js +41 -43
  183. package/dist/tasks/control.d.ts +138 -0
  184. package/dist/tasks/control.js +1 -0
  185. package/dist/tasks/create.d.ts +1 -1
  186. package/dist/tasks/create.js +3 -3
  187. package/dist/tasks/delivery.d.ts +24 -0
  188. package/dist/tasks/delivery.js +221 -0
  189. package/dist/tasks/file-mutation.d.ts +5 -0
  190. package/dist/tasks/file-mutation.js +38 -0
  191. package/dist/tasks/frontmatter.d.ts +43 -28
  192. package/dist/tasks/frontmatter.js +83 -40
  193. package/dist/tasks/input-receipts.d.ts +7 -0
  194. package/dist/tasks/input-receipts.js +60 -0
  195. package/dist/tasks/loop-log.d.ts +147 -28
  196. package/dist/tasks/loop-log.js +239 -28
  197. package/dist/tasks/receipts.d.ts +19 -0
  198. package/dist/tasks/receipts.js +48 -0
  199. package/dist/tasks/state.d.ts +93 -0
  200. package/dist/tasks/state.js +156 -0
  201. package/dist/tasks/store.d.ts +81 -49
  202. package/dist/tasks/store.js +244 -147
  203. package/dist/tools/bash.d.ts +3 -5
  204. package/dist/tools/bash.js +8 -22
  205. package/dist/tools/contract/details.d.ts +6 -6
  206. package/dist/tools/contract/details.js +6 -6
  207. package/dist/tools/contract/execution-trace.d.ts +4 -0
  208. package/dist/tools/contract/execution-trace.js +23 -0
  209. package/dist/tools/contract/presentation.js +6 -2
  210. package/dist/tools/edit.d.ts +1 -0
  211. package/dist/tools/edit.js +45 -42
  212. package/dist/tools/index.d.ts +8 -12
  213. package/dist/tools/index.js +16 -6
  214. package/dist/tools/memory-manage.js +8 -1
  215. package/dist/tools/registry.d.ts +7 -11
  216. package/dist/tools/registry.js +52 -25
  217. package/dist/tools/skill.d.ts +2 -2
  218. package/dist/tools/skill.js +24 -10
  219. package/dist/tools/subagent-manage.js +7 -4
  220. package/dist/tools/subagent.d.ts +0 -2
  221. package/dist/tools/subagent.js +2 -7
  222. package/dist/tools/task-manage/control-tools.d.ts +4 -0
  223. package/dist/tools/task-manage/control-tools.js +65 -0
  224. package/dist/tools/task-manage/create.js +2 -1
  225. package/dist/tools/task-manage/factory.d.ts +12 -6
  226. package/dist/tools/task-manage/factory.js +72 -57
  227. package/dist/tools/task-manage/lifecycle.d.ts +7 -12
  228. package/dist/tools/task-manage/lifecycle.js +118 -66
  229. package/dist/tools/task-manage/log-view.js +8 -5
  230. package/dist/tools/task-manage/schema.d.ts +21 -26
  231. package/dist/tools/task-manage/schema.js +29 -35
  232. package/dist/tools/task-manage/shared.d.ts +8 -39
  233. package/dist/tools/task-manage/shared.js +19 -68
  234. package/dist/tools/task-manage/step-tools.d.ts +5 -0
  235. package/dist/tools/task-manage/step-tools.js +103 -0
  236. package/dist/tools/task-manage/types.d.ts +25 -18
  237. package/dist/tools/write-content.d.ts +1 -0
  238. package/dist/tools/write-content.js +6 -1
  239. package/dist/tools/write.d.ts +1 -0
  240. package/dist/tools/write.js +1 -0
  241. package/dist/usage/ledger.d.ts +4 -4
  242. package/dist/usage/render.js +2 -2
  243. package/dist/web/search-providers.js +0 -33
  244. package/dist/webapi/request-policy.js +1 -1
  245. package/examples/sub-agents/README.md +23 -16
  246. package/examples/sub-agents/agents/claude-high.md +1 -1
  247. package/examples/sub-agents/agents/claude-main.md +1 -1
  248. package/examples/sub-agents/agents/codex-flash.md +1 -1
  249. package/examples/sub-agents/agents/codex-high.md +1 -1
  250. package/examples/sub-agents/agents/codex-main.md +4 -4
  251. package/examples/sub-agents/agents/cursor-high.md +22 -0
  252. package/examples/sub-agents/agents/cursor-main.md +22 -0
  253. package/examples/sub-agents/agents/glm-flash.md +1 -1
  254. package/examples/sub-agents/agents/glm-high.md +2 -2
  255. package/examples/sub-agents/agents/scout.md +30 -0
  256. package/package.json +6 -6
  257. package/dist/runtime/task-driver.d.ts +0 -116
  258. package/dist/runtime/task-driver.js +0 -443
  259. package/dist/runtime/task-wake.d.ts +0 -48
  260. package/dist/runtime/task-wake.js +0 -32
  261. package/dist/shared/wake-claim.d.ts +0 -23
  262. package/dist/shared/wake-claim.js +0 -26
  263. package/dist/skills/validation.d.ts +0 -8
  264. package/dist/skills/validation.js +0 -91
  265. package/dist/skills/workspace.d.ts +0 -33
  266. package/dist/skills/workspace.js +0 -116
  267. package/dist/tasks/steer.d.ts +0 -8
  268. package/dist/tasks/steer.js +0 -81
  269. package/dist/tasks/ticket.d.ts +0 -71
  270. package/dist/tasks/ticket.js +0 -138
  271. package/dist/tools/task-manage/step-end.d.ts +0 -11
  272. package/dist/tools/task-manage/step-end.js +0 -117
@@ -1,13 +1,26 @@
1
- import { type Ticket } from "./ticket.js";
2
1
  /**
3
- * The task frontmatter (spec 052, §3.3).
2
+ * The task frontmatter (spec 064, D2).
4
3
  *
5
- * A task is a one-off project: it opens, is worked until `done` or cancelled, and is archived.
6
- * Recurring work is an event that spawns a fresh instance each time (spec 052, D2), so there is
7
- * no cadence, no cycle and no verification flag here. `state: "parked"` ⟺ `ticket` present
8
- * (INV-1) is enforced on every read and every write rather than checked after the fact.
4
+ * A task is a one-off project: it is queued, runs, waits and is finally archived. Every field
5
+ * except the contract body belongs to the runtime — the lane and `tasks/store.ts` write them from
6
+ * observed facts, and no model tool can (INV-2). `state: waiting` ⟺ `waiting` present and
7
+ * `state: paused` ⟺ `paused` present (INV-3) are enforced on every read and every write rather
8
+ * than checked after the fact. An archived file carries `outcome` instead of a state.
9
9
  */
10
- export type TaskState = "open" | "parked" | "done";
10
+ export type TaskState = "queued" | "running" | "waiting" | "paused";
11
+ /** What a waiting task is waiting for — derived from facts, never declared by the model. */
12
+ export type TaskWaiting = {
13
+ kind: "work";
14
+ since: string;
15
+ } | {
16
+ kind: "user";
17
+ question: string;
18
+ since: string;
19
+ } | {
20
+ kind: "time";
21
+ until: string;
22
+ since: string;
23
+ };
11
24
  export interface TaskPaused {
12
25
  by: "user" | "runtime";
13
26
  reason: string;
@@ -20,8 +33,8 @@ export interface TaskUsage {
20
33
  usd: number;
21
34
  /** True once any cost came from an estimate rather than reported usage. */
22
35
  usdEstimated: boolean;
23
- /** Consecutive ticket backstops that expired; the second one stops the task (D3). */
24
- expired: number;
36
+ /** Contributions whose price is unavailable; usd is only the known subtotal in that case. */
37
+ unknownCostCount?: number;
25
38
  }
26
39
  export interface TaskBudget {
27
40
  steps: number;
@@ -29,20 +42,24 @@ export interface TaskBudget {
29
42
  }
30
43
  export type TaskArchiveOutcome = "completed" | "cancelled";
31
44
  export interface TaskFrontmatter {
45
+ /** Last state checkpoint applied from the loop log. Updated atomically with state and usage. */
46
+ revision?: number;
47
+ /** Meaningless once `outcome` is set: an archived task has no state. */
32
48
  state: TaskState;
49
+ /** Present iff `state === "waiting"` (INV-3). */
50
+ waiting?: TaskWaiting;
51
+ /** Present iff `state === "paused"` (INV-3). */
33
52
  paused?: TaskPaused;
34
53
  /**
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.
54
+ * The delivery channel: where every report, question and receipt of this task goes (spec 058,
55
+ * D13). Plain text so a human can retarget it by hand. Missing, or not bound to this task's
56
+ * desk, means the task is frozen until the channel is bound again (INV-7).
38
57
  */
39
58
  channel?: string;
40
- /** Present iff `state === "parked"` (INV-1). */
41
- ticket?: Ticket;
42
- usage?: TaskUsage;
43
- budget?: Partial<TaskBudget>;
44
59
  /** Name of the event whose template spawned this instance (spec 052, D2). */
45
60
  origin?: string;
61
+ usage?: TaskUsage;
62
+ budget?: Partial<TaskBudget>;
46
63
  /** Archive-only. */
47
64
  outcome?: TaskArchiveOutcome;
48
65
  closedAt?: string;
@@ -53,23 +70,21 @@ export interface ParsedTaskFrontmatter {
53
70
  readable: boolean;
54
71
  }
55
72
  /**
56
- * The one user-facing name for a task state (spec 053, D8). `/tasks`, the stop receipt and the
57
- * spawn-skip receipt all read it here, so a third hand-written copy cannot drift again.
73
+ * The one user-facing name for a task's state (spec 053, D8; spec 064, D6). `/tasks`, the task
74
+ * agenda and the spawn-skip receipt all read it here, so a third hand-written copy cannot drift.
58
75
  */
59
- export declare function taskStateLabel(state: TaskState): string;
76
+ export declare function taskStateLabel(fields: Pick<TaskFrontmatter, "state" | "waiting">): string;
60
77
  /**
61
- * Read the leading frontmatter block. Unreadable input fails **open** to a live `open` task: a
62
- * corrupt file has to surface as work the runtime tries to touch, not silently disappear from the
63
- * ledger.
78
+ * Read the leading frontmatter block. Unreadable input fails **open**: the task still appears in
79
+ * the ledger (as `queued`, flagged unreadable) so a corrupt file surfaces instead of vanishing; the
80
+ * lane will not execute it (`/tasks doctor` points at the repair).
64
81
  */
65
82
  export declare function parseTaskFrontmatter(content: string): ParsedTaskFrontmatter;
66
83
  /**
67
- * Enforce INV-1 on every write and read: `parked` requires a ticket, and a ticket only means
68
- * anything while parked. Doing it here rather than at each call site is what makes "a parked task
69
- * always has a redeemable, backstopped source" a property of the file format instead of a rule
70
- * every writer has to remember.
84
+ * Enforce INV-3 on every write and read: `waiting` and `paused` states need their field, and the
85
+ * fields mean nothing outside their state. Doing it here rather than at each call site is what
86
+ * makes "waiting with nothing described" a property of the file format instead of a rule every
87
+ * writer has to remember.
71
88
  */
72
89
  export declare function normalizeTaskFrontmatter(fields: TaskFrontmatter): TaskFrontmatter;
73
90
  export declare function renderTaskFrontmatter(fields: TaskFrontmatter): string;
74
- /** Whether the driver may schedule a step for this task right now. */
75
- export declare function isTaskRunnable(fields: TaskFrontmatter): boolean;
@@ -1,17 +1,35 @@
1
- import { formatLocalTime } from "../shared/local-time.js";
1
+ import { formatLocalTime, parseLocalTime } from "../shared/local-time.js";
2
2
  import { isPlainObject } from "../shared/type-guards.js";
3
- import { parseTicket } from "./ticket.js";
4
- const STATES = ["open", "parked", "done"];
5
- const STATE_LABELS = { open: "进行中", parked: "等待中", done: "已结束" };
3
+ const STATES = ["queued", "running", "waiting", "paused"];
4
+ /** Fixed render order, so a diff of a task file reads the same way every time. */
5
+ const FIELD_ORDER = ["state", "waiting", "paused", "channel", "origin", "usage", "budget", "revision"];
6
+ /** `2026-10-12T09:00:00.000+08:00` → `10-12 09:00`, read off the stored wall-clock text. */
7
+ function minuteOf(at) {
8
+ const match = /^\d{4}-(\d{2}-\d{2})T(\d{2}:\d{2})/.exec(at);
9
+ return match ? `${match[1]} ${match[2]}` : at;
10
+ }
6
11
  /**
7
- * The one user-facing name for a task state (spec 053, D8). `/tasks`, the stop receipt and the
8
- * spawn-skip receipt all read it here, so a third hand-written copy cannot drift again.
12
+ * The one user-facing name for a task's state (spec 053, D8; spec 064, D6). `/tasks`, the task
13
+ * agenda and the spawn-skip receipt all read it here, so a third hand-written copy cannot drift.
9
14
  */
10
- export function taskStateLabel(state) {
11
- return STATE_LABELS[state] ?? state;
15
+ export function taskStateLabel(fields) {
16
+ switch (fields.state) {
17
+ case "queued":
18
+ return "排队中";
19
+ case "running":
20
+ return "执行中";
21
+ case "paused":
22
+ return "已暂停";
23
+ case "waiting": {
24
+ const waiting = fields.waiting;
25
+ if (waiting?.kind === "user")
26
+ return "等你回复";
27
+ if (waiting?.kind === "time")
28
+ return `等到 ${minuteOf(waiting.until)}`;
29
+ return "等结果";
30
+ }
31
+ }
12
32
  }
13
- /** Fixed render order, so a diff of a task file reads the same way every time. */
14
- const FIELD_ORDER = ["state", "paused", "channel", "origin", "ticket", "usage", "budget"];
15
33
  function parseJsonObject(raw) {
16
34
  try {
17
35
  const value = JSON.parse(raw);
@@ -21,16 +39,38 @@ function parseJsonObject(raw) {
21
39
  return undefined;
22
40
  }
23
41
  }
42
+ function nonEmptyString(value) {
43
+ return typeof value === "string" && value.trim() ? value.trim() : undefined;
44
+ }
45
+ function parseWaiting(raw) {
46
+ const value = parseJsonObject(raw);
47
+ if (!value)
48
+ return undefined;
49
+ const since = nonEmptyString(value.since) ?? formatLocalTime();
50
+ switch (value.kind) {
51
+ case "work":
52
+ return { kind: "work", since };
53
+ case "user": {
54
+ const question = nonEmptyString(value.question);
55
+ return question ? { kind: "user", question, since } : undefined;
56
+ }
57
+ case "time": {
58
+ const until = nonEmptyString(value.until);
59
+ return until && parseLocalTime(until) !== undefined ? { kind: "time", until, since } : undefined;
60
+ }
61
+ default:
62
+ return undefined;
63
+ }
64
+ }
24
65
  function parsePaused(raw) {
25
66
  const value = parseJsonObject(raw);
26
67
  if (!value)
27
68
  return undefined;
28
69
  const by = value.by === "runtime" ? "runtime" : "user";
29
- const reason = typeof value.reason === "string" ? value.reason.trim() : "";
30
- const at = typeof value.at === "string" ? value.at.trim() : "";
70
+ const reason = nonEmptyString(value.reason);
31
71
  if (!reason)
32
72
  return undefined;
33
- return { by, reason, at: at || formatLocalTime() };
73
+ return { by, reason, at: nonEmptyString(value.at) ?? formatLocalTime() };
34
74
  }
35
75
  function nonNegativeNumber(value, fallback) {
36
76
  return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : fallback;
@@ -40,11 +80,13 @@ function parseUsage(raw) {
40
80
  if (!value)
41
81
  return undefined;
42
82
  return {
43
- startedAt: typeof value.startedAt === "string" && value.startedAt ? value.startedAt : formatLocalTime(),
83
+ startedAt: nonEmptyString(value.startedAt) ?? formatLocalTime(),
44
84
  steps: nonNegativeNumber(value.steps, 0),
45
85
  usd: nonNegativeNumber(value.usd, 0),
46
86
  usdEstimated: value.usdEstimated === true,
47
- expired: nonNegativeNumber(value.expired, 0),
87
+ ...(nonNegativeNumber(value.unknownCostCount, 0) > 0
88
+ ? { unknownCostCount: Math.floor(nonNegativeNumber(value.unknownCostCount, 0)) }
89
+ : {}),
48
90
  };
49
91
  }
50
92
  function parseBudget(raw) {
@@ -59,17 +101,17 @@ function parseBudget(raw) {
59
101
  return Object.keys(budget).length > 0 ? budget : undefined;
60
102
  }
61
103
  /**
62
- * Read the leading frontmatter block. Unreadable input fails **open** to a live `open` task: a
63
- * corrupt file has to surface as work the runtime tries to touch, not silently disappear from the
64
- * ledger.
104
+ * Read the leading frontmatter block. Unreadable input fails **open**: the task still appears in
105
+ * the ledger (as `queued`, flagged unreadable) so a corrupt file surfaces instead of vanishing; the
106
+ * lane will not execute it (`/tasks doctor` points at the repair).
65
107
  */
66
108
  export function parseTaskFrontmatter(content) {
67
109
  if (!content.startsWith("---"))
68
- return { fields: { state: "open" }, readable: false };
110
+ return { fields: { state: "queued" }, readable: false };
69
111
  const end = content.indexOf("\n---", 3);
70
112
  if (end === -1)
71
- return { fields: { state: "open" }, readable: false };
72
- const fields = { state: "open" };
113
+ return { fields: { state: "queued" }, readable: false };
114
+ const fields = { state: "queued" };
73
115
  let sawState = false;
74
116
  for (const line of content.slice(3, end).split("\n")) {
75
117
  const idx = line.indexOf(":");
@@ -78,18 +120,22 @@ export function parseTaskFrontmatter(content) {
78
120
  const key = line.slice(0, idx).trim();
79
121
  const value = line.slice(idx + 1).trim();
80
122
  switch (key) {
123
+ case "revision":
124
+ if (/^\d+$/.test(value))
125
+ fields.revision = Number(value);
126
+ break;
81
127
  case "state":
82
128
  if (STATES.includes(value)) {
83
129
  fields.state = value;
84
130
  sawState = true;
85
131
  }
86
132
  break;
133
+ case "waiting":
134
+ fields.waiting = parseWaiting(value);
135
+ break;
87
136
  case "paused":
88
137
  fields.paused = parsePaused(value);
89
138
  break;
90
- case "ticket":
91
- fields.ticket = parseTicket(value);
92
- break;
93
139
  case "usage":
94
140
  fields.usage = parseUsage(value);
95
141
  break;
@@ -113,30 +159,31 @@ export function parseTaskFrontmatter(content) {
113
159
  break;
114
160
  }
115
161
  }
116
- if (fields.outcome)
117
- fields.state = "done";
118
162
  // A file with no recognizable marker is not frontmatter we understand.
119
163
  const readable = sawState || fields.outcome !== undefined;
120
164
  return { fields: normalizeTaskFrontmatter(fields), readable };
121
165
  }
122
166
  /**
123
- * Enforce INV-1 on every write and read: `parked` requires a ticket, and a ticket only means
124
- * anything while parked. Doing it here rather than at each call site is what makes "a parked task
125
- * always has a redeemable, backstopped source" a property of the file format instead of a rule
126
- * every writer has to remember.
167
+ * Enforce INV-3 on every write and read: `waiting` and `paused` states need their field, and the
168
+ * fields mean nothing outside their state. Doing it here rather than at each call site is what
169
+ * makes "waiting with nothing described" a property of the file format instead of a rule every
170
+ * writer has to remember.
127
171
  */
128
172
  export function normalizeTaskFrontmatter(fields) {
129
173
  const next = { ...fields };
130
174
  if (next.outcome) {
131
- next.state = "done";
132
- next.ticket = undefined;
175
+ next.waiting = undefined;
133
176
  next.paused = undefined;
134
177
  return next;
135
178
  }
136
- if (next.state === "parked" && !next.ticket)
137
- next.state = "open";
138
- if (next.state !== "parked")
139
- next.ticket = undefined;
179
+ if (next.state === "waiting" && !next.waiting)
180
+ next.state = "queued";
181
+ if (next.state === "paused" && !next.paused)
182
+ next.state = "queued";
183
+ if (next.state !== "waiting")
184
+ next.waiting = undefined;
185
+ if (next.state !== "paused")
186
+ next.paused = undefined;
140
187
  return next;
141
188
  }
142
189
  export function renderTaskFrontmatter(fields) {
@@ -164,7 +211,3 @@ export function renderTaskFrontmatter(fields) {
164
211
  lines.push("---");
165
212
  return lines.join("\n");
166
213
  }
167
- /** Whether the driver may schedule a step for this task right now. */
168
- export function isTaskRunnable(fields) {
169
- return fields.state === "open" && !fields.paused && !fields.outcome;
170
- }
@@ -0,0 +1,7 @@
1
+ export declare function taskInputTag(id: string): string;
2
+ /** Read the actual transcript, not SDK queue/message events (those precede persistence). */
3
+ export declare function acknowledgeTaskInputs(deskDir: string, taskId: string, trace?: {
4
+ desk: string;
5
+ seq: number;
6
+ generation?: string;
7
+ }): Promise<void>;
@@ -0,0 +1,60 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import * as log from "../log.js";
3
+ import { isPlainObject } from "../shared/type-guards.js";
4
+ import { appendTaskLog, newInputs, readTaskLog } from "./loop-log.js";
5
+ import { withTaskMutation } from "./mutation-lock.js";
6
+ import { taskSessionPath } from "./session-path.js";
7
+ export function taskInputTag(id) {
8
+ return `<task_input id="${id}" />`;
9
+ }
10
+ /** Read the actual transcript, not SDK queue/message events (those precede persistence). */
11
+ export async function acknowledgeTaskInputs(deskDir, taskId, trace) {
12
+ await withTaskMutation(deskDir, taskId, async () => {
13
+ const pending = newInputs(await readTaskLog(deskDir, taskId, { strict: true }));
14
+ if (pending.length === 0)
15
+ return;
16
+ let transcript;
17
+ try {
18
+ transcript = await readFile(taskSessionPath(deskDir, taskId), "utf-8");
19
+ }
20
+ catch (error) {
21
+ if (error instanceof Error && "code" in error && error.code === "ENOENT")
22
+ return;
23
+ throw error;
24
+ }
25
+ const received = new Set();
26
+ for (const line of transcript.split("\n")) {
27
+ let entry;
28
+ try {
29
+ entry = JSON.parse(line);
30
+ }
31
+ catch {
32
+ continue;
33
+ }
34
+ if (!isPlainObject(entry) ||
35
+ entry.type !== "message" ||
36
+ !isPlainObject(entry.message) ||
37
+ entry.message.role !== "user")
38
+ continue;
39
+ const content = entry.message.content;
40
+ const text = typeof content === "string"
41
+ ? content
42
+ : Array.isArray(content)
43
+ ? content
44
+ .filter(isPlainObject)
45
+ .filter((part) => part.type === "text")
46
+ .map((part) => part.text)
47
+ .join("\n")
48
+ : "";
49
+ for (const match of text.matchAll(/<task_input id="([a-f0-9-]+)" \/>/g))
50
+ received.add(match[1]);
51
+ }
52
+ const ids = pending.flatMap((record) => (record.id && received.has(record.id) ? [record.id] : []));
53
+ if (ids.length > 0) {
54
+ await appendTaskLog(deskDir, taskId, { kind: "input_ack", ids });
55
+ log.logInfoEvent("tasks.input.received", "Task inputs received", {
56
+ fields: { deskDir, taskId, ...trace, ids },
57
+ });
58
+ }
59
+ });
60
+ }
@@ -1,33 +1,58 @@
1
+ import type { TaskBudget, TaskFrontmatter } from "./frontmatter.js";
1
2
  /**
2
- * The task's append-only loop log (spec 051, D5) — `tasks/<id>.jsonl`.
3
+ * The task's append-only loop log (spec 051, D5; spec 064, D3) — `tasks/<id>.jsonl`.
3
4
  *
4
- * v3 kept this history inline in the task file under `## History`, capped at 24 KB / 8 entries.
5
- * Both long-lived tasks on the author's machine sat permanently *at* that cap: 79% of a 30 KB
6
- * task file was closed cycles the model re-read on every single wake and never acted on. Moving
7
- * it here keeps the contract small enough to inject whole (INV-6) while making the history much
8
- * longer before anything is folded away: the log rotates at `MAX_LOG_BYTES` and keeps two rotated
9
- * backups (`.1`, `.2`) alongside the current file — bounded retention, not an accidental cap.
10
- * `readTaskLog`/`archiveTask` must enumerate all three; reading or archiving only the current
11
- * file silently drops whatever had already rotated out of it (R9).
12
- *
13
- * Spec 052 adds `dispatch` / `settle` records: the run and job managers write them when work is
14
- * bound to the task and when it settles, so the task keeps the outcome of every delegation long
15
- * after the run record itself has been garbage-collected (INV-5). The step brief renders them as
16
- * the `<task_board>`. The log is append-only and is never rewritten.
5
+ * Every input to the task (a settlement, a stall report, a user message, a resume) is appended here
6
+ * *before* the lane is told. Inputs remain pending until their identity appears in a persisted
7
+ * user message in the task transcript and an `input_ack` records that receipt (INV-6). The
8
+ * log rotates at `MAX_LOG_BYTES` and keeps two rotated backups (`.1`, `.2`) alongside the current
9
+ * file: `readTaskLog`/`archiveTask` must enumerate all three, or whatever had already rotated out is
10
+ * silently dropped (R9). The log is append-only and is never rewritten (INV-9).
17
11
  */
18
- export type TaskStepOutcome = "continue" | "park" | "done";
12
+ export type TaskStepReason = "start" | "input" | "continue" | "resume" | "retry";
13
+ export type TaskStepResult = "ok" | "timeout" | "error" | "aborted" | "interrupted";
14
+ export type TaskStepNext = "continue" | "work" | "user" | "time" | "closed" | "paused";
15
+ /** A step began: written by the lane before the model is called. */
19
16
  export interface TaskStepRecord {
20
17
  ts: string;
21
18
  kind: "step";
22
19
  seq: number;
23
- outcome: TaskStepOutcome;
20
+ reason: TaskStepReason;
21
+ brief: "full" | "delta";
22
+ }
23
+ /** A step ended. `step` without `step_end` means the process died mid-step (D10 repairs it). */
24
+ export interface TaskStepEndRecord {
25
+ ts: string;
26
+ kind: "step_end";
27
+ seq: number;
28
+ result: TaskStepResult;
29
+ next: TaskStepNext;
30
+ /** The model's last reply of the step: what it did, the evidence, what comes next. */
24
31
  note: string;
25
32
  tools: string[];
26
- /** What this step sent the user, when it sent anything. Kept so the next occurrence knows what was delivered. */
27
- report?: string;
28
33
  usd?: number;
29
34
  usdEstimated?: boolean;
30
- units?: number;
35
+ usdKnown?: boolean;
36
+ error?: string;
37
+ close?: {
38
+ outcome: "complete" | "cancel";
39
+ note: string;
40
+ reportId?: string;
41
+ };
42
+ }
43
+ export interface TaskInputAckRecord {
44
+ ts: string;
45
+ kind: "input_ack";
46
+ ids: string[];
47
+ }
48
+ export interface TaskAskDeliveredRecord {
49
+ ts: string;
50
+ kind: "ask_delivered";
51
+ seq: number;
52
+ }
53
+ export interface TaskCheckpointRecord {
54
+ ts: string;
55
+ kind: "checkpoint";
31
56
  }
32
57
  /** A delegation or background job was bound to this task (spec 052, D4). `ref` is a run or job id. */
33
58
  export interface TaskDispatchRecord {
@@ -55,6 +80,7 @@ export interface TaskSettleRecord {
55
80
  verdict?: "pass" | "fail";
56
81
  usd?: number;
57
82
  usdEstimated?: boolean;
83
+ usdKnown?: boolean;
58
84
  /** Where the full output lives (a run's `output.md`, a job's output file). */
59
85
  output?: string;
60
86
  /** The output's tail, as the completion wake shows it. Untrusted executor text, not instructions. */
@@ -63,22 +89,84 @@ export interface TaskSettleRecord {
63
89
  changed?: string;
64
90
  exitCode?: number;
65
91
  durationMs?: number;
92
+ /**
93
+ * The result was returned to the step inside the very tool call that started the work, so it is
94
+ * not news to anyone: it stays on the board but is not an input that wakes or briefs (D3).
95
+ */
96
+ inline?: boolean;
97
+ }
98
+ /** An external run has produced no output for a long while; the leader decides what to do (D16). */
99
+ export interface TaskStallRecord {
100
+ ts: string;
101
+ kind: "stall";
102
+ ref: string;
103
+ silentMinutes: number;
104
+ }
105
+ /** The user said something to the task; only a durable input receipt consumes it. */
106
+ export interface TaskMessageRecord {
107
+ ts: string;
108
+ kind: "message";
109
+ from: string;
110
+ channel: string;
111
+ text: string;
112
+ }
113
+ export interface TaskAskRecord {
114
+ ts: string;
115
+ kind: "ask";
116
+ seq: number;
117
+ question: string;
118
+ }
119
+ export interface TaskWaitRecord {
120
+ ts: string;
121
+ kind: "wait";
122
+ seq: number;
123
+ until: string;
124
+ }
125
+ export interface TaskReportRecord {
126
+ ts: string;
127
+ kind: "report";
128
+ seq: number;
129
+ text: string;
130
+ delivered: boolean;
131
+ channel?: string;
132
+ }
133
+ export interface TaskReportDeliveredRecord {
134
+ ts: string;
135
+ kind: "report_delivered";
136
+ reportId: string;
66
137
  }
67
- export interface TaskExpiredRecord {
138
+ export interface TaskPauseRecord {
68
139
  ts: string;
69
- kind: "expired";
70
- ticket: string;
71
- action: "reopened" | "paused";
140
+ kind: "pause";
141
+ by: "user" | "runtime";
142
+ reason: string;
143
+ }
144
+ export interface TaskResumeRecord {
145
+ ts: string;
146
+ kind: "resume";
147
+ grants?: Partial<TaskBudget>;
72
148
  }
73
149
  export interface TaskCloseRecord {
74
150
  ts: string;
75
151
  kind: "close";
76
152
  outcome: "done" | "cancelled";
153
+ by: "leader" | "user";
77
154
  note: string;
78
155
  steps: number;
79
156
  usd: number;
157
+ usdEstimated?: boolean;
158
+ unknownCostCount?: number;
159
+ reportId?: string;
160
+ }
161
+ export interface TaskCloseRejectedRecord {
162
+ ts: string;
163
+ kind: "close_rejected";
164
+ reason: string;
80
165
  }
81
- export type TaskLogRecord = TaskStepRecord | TaskDispatchRecord | TaskSettleRecord | TaskExpiredRecord | TaskCloseRecord;
166
+ export type TaskLogRecord = (TaskInputAckRecord | TaskAskDeliveredRecord | TaskCheckpointRecord | TaskStepRecord | TaskStepEndRecord | TaskDispatchRecord | TaskSettleRecord | TaskStallRecord | TaskMessageRecord | TaskAskRecord | TaskWaitRecord | TaskReportRecord | TaskReportDeliveredRecord | TaskPauseRecord | TaskResumeRecord | TaskCloseRecord | TaskCloseRejectedRecord) & {
167
+ id?: string;
168
+ checkpoint?: TaskFrontmatter;
169
+ };
82
170
  export declare function taskLogPath(deskDir: string, id: string): string;
83
171
  /** Where `archiveTask` moves a closed task's `.jsonl`. */
84
172
  export declare function taskArchiveLogPath(deskDir: string, id: string): string;
@@ -86,19 +174,27 @@ export declare function taskArchiveLogPath(deskDir: string, id: string): string;
86
174
  export declare function taskLogIsArchived(deskDir: string, id: string): boolean;
87
175
  /** Test seam: drop cached appenders so a temp-dir fixture does not leak into the next case. */
88
176
  export declare function resetTaskLogAppenders(): Promise<void>;
89
- export type TaskLogInput = Omit<TaskStepRecord, "ts"> | Omit<TaskDispatchRecord, "ts"> | Omit<TaskSettleRecord, "ts"> | Omit<TaskExpiredRecord, "ts"> | Omit<TaskCloseRecord, "ts">;
177
+ type OmitTs<T> = T extends unknown ? Omit<T, "ts"> : never;
178
+ export type TaskLogInput = OmitTs<TaskLogRecord>;
90
179
  /**
91
180
  * Append one record. `appendStrict` rather than `append`: the loop log is the only durable trace
92
181
  * of what a step did, so a dropped record would make the next brief lie about the task's history.
93
182
  */
94
183
  export declare function appendTaskLog(deskDir: string, id: string, record: TaskLogInput & {
95
184
  ts?: string;
96
- }): Promise<void>;
185
+ }): Promise<string>;
97
186
  export interface ReadTaskLogOptions {
98
187
  /** Keep at most this many, counted from the end (the most recent). */
99
188
  limit?: number;
100
189
  kinds?: readonly TaskLogRecord["kind"][];
190
+ /** Recovery must distinguish unreadable storage from an empty history. */
191
+ strict?: boolean;
101
192
  }
193
+ /** Explicit diagnostics are cold-path work: normal reads stay tolerant, doctor exposes damaged history. */
194
+ export declare function taskLogIssues(deskDir: string, id: string): Promise<Array<{
195
+ path: string;
196
+ line: number;
197
+ }>>;
102
198
  /**
103
199
  * Read the log back, newest-last. Unparseable lines are skipped rather than failing the read: a
104
200
  * hand-edited or half-written line must not make a task's whole history unreadable.
@@ -117,7 +213,30 @@ export declare function describeWorkRef(ref: string): string;
117
213
  export declare function describeSettlement(settle: Pick<TaskSettleRecord, "status" | "verdict" | "exitCode">): string;
118
214
  /**
119
215
  * One line per record for `/tasks show` and `/tasks log` (spec 053, D2). The same records as
120
- * `renderTaskLogLine`, said in the user's terms: no `task_step_end` outcome values, no `VERDICT`,
121
- * no tool names, minute precision.
216
+ * `renderTaskLogLine`, said in the user's terms: no `result` / `next` values, no `VERDICT`, no
217
+ * tool names, minute precision.
122
218
  */
123
219
  export declare function renderTaskLogLineForUser(record: TaskLogRecord): string;
220
+ /** The last `step` record, and whether a `step_end` for it exists. `undefined` before the first step. */
221
+ export declare function lastStep(records: readonly TaskLogRecord[]): {
222
+ step: TaskStepRecord;
223
+ ended: boolean;
224
+ } | undefined;
225
+ /** A record that wakes a waiting task and is shown to the next step. */
226
+ export type TaskInputRecord = (TaskSettleRecord | TaskStallRecord | TaskMessageRecord | TaskResumeRecord | TaskCloseRejectedRecord) & {
227
+ id?: string;
228
+ };
229
+ /**
230
+ * Pending inputs, in journal order. Starting a step or accepting a steer never consumes them.
231
+ * Inline results are already returned by their tool; other inputs need a durable receipt.
232
+ */
233
+ export declare function newInputs(records: readonly TaskLogRecord[]): TaskInputRecord[];
234
+ /**
235
+ * The `step_end` records since the user last resumed the task, oldest first. A guard (idle, timeout,
236
+ * error) counts only these: a resume is the user looking at the problem, so the same evidence must
237
+ * not pause the task a second time.
238
+ */
239
+ export declare function stepEndsSinceResume(records: readonly TaskLogRecord[]): Array<TaskStepEndRecord & {
240
+ id?: string;
241
+ }>;
242
+ export {};