@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
@@ -0,0 +1,141 @@
1
+ import { createHash } from "node:crypto";
2
+ import { existsSync } from "node:fs";
3
+ import { mkdir, open, readdir, readFile, rm } from "node:fs/promises";
4
+ import { join } from "node:path";
5
+ import * as log from "../log.js";
6
+ import { writeFileAtomically } from "../shared/atomic-file.js";
7
+ import { errorMessage } from "../shared/text-utils.js";
8
+ import { isPlainObject } from "../shared/type-guards.js";
9
+ import { normalizeTaskId, renderTaskDocument } from "./contract.js";
10
+ import { withTaskMutation } from "./mutation-lock.js";
11
+ const SUFFIXES = ["", ".1", ".2"];
12
+ /** A current-format crash-recovery record, removed only after publication and source cleanup. */
13
+ export function taskArchiveCommitPath(deskDir, id) {
14
+ return join(deskDir, "tasks", ".closing", `${normalizeTaskId(id)}.json`);
15
+ }
16
+ export function isTaskArchiving(deskDir, id) {
17
+ return existsSync(taskArchiveCommitPath(deskDir, id));
18
+ }
19
+ const digest = (content) => createHash("sha256").update(content).digest("hex");
20
+ /** Publication is already durable; unsupported directory sync must not turn cleanup into a failure. */
21
+ async function syncDirectory(path) {
22
+ await open(path, "r")
23
+ .then(async (handle) => {
24
+ try {
25
+ await handle.sync();
26
+ }
27
+ finally {
28
+ await handle.close();
29
+ }
30
+ })
31
+ .catch(() => undefined);
32
+ }
33
+ async function readCommit(path, id) {
34
+ const value = JSON.parse(await readFile(path, "utf-8"));
35
+ if (!isPlainObject(value) || value.id !== id || typeof value.contract !== "string" || !Array.isArray(value.logs)) {
36
+ throw new Error(`Invalid task archive commit: ${path}; have the deployer inspect it before retrying.`);
37
+ }
38
+ const logs = [];
39
+ for (const entry of value.logs) {
40
+ if (!isPlainObject(entry) ||
41
+ !SUFFIXES.includes(entry.suffix) ||
42
+ typeof entry.sha256 !== "string" ||
43
+ !/^[a-f0-9]{64}$/.test(entry.sha256) ||
44
+ logs.some((log) => log.suffix === entry.suffix)) {
45
+ throw new Error(`Invalid task archive shard: ${path}; have the deployer inspect it before retrying.`);
46
+ }
47
+ logs.push({ suffix: entry.suffix, sha256: entry.sha256 });
48
+ }
49
+ return { id, contract: value.contract, logs };
50
+ }
51
+ /** Caller holds the task lock. Published files are immutable; retries verify, never overwrite conflicts. */
52
+ async function finishCommit(deskDir, commit) {
53
+ const tasks = join(deskDir, "tasks");
54
+ const archive = join(tasks, "archive");
55
+ await mkdir(archive, { recursive: true });
56
+ for (const shard of commit.logs) {
57
+ const source = join(tasks, `${commit.id}.jsonl${shard.suffix}`);
58
+ const target = join(archive, `${commit.id}.jsonl${shard.suffix}`);
59
+ const original = existsSync(source) ? await readFile(source) : undefined;
60
+ const published = existsSync(target) ? await readFile(target) : undefined;
61
+ if ((original && digest(original) !== shard.sha256) || (published && digest(published) !== shard.sha256)) {
62
+ throw new Error(`Task archive log conflicts with its commit: ${source}; have the deployer inspect both copies.`);
63
+ }
64
+ if (!published) {
65
+ if (!original)
66
+ throw new Error(`Task archive log is missing: ${source}; restore it before retrying.`);
67
+ await writeFileAtomically(target, original);
68
+ }
69
+ }
70
+ const target = join(archive, `${commit.id}.md`);
71
+ if (existsSync(target)) {
72
+ if ((await readFile(target, "utf-8")) !== commit.contract) {
73
+ throw new Error(`Task archive contract conflicts with its commit: ${target}; have the deployer inspect both copies.`);
74
+ }
75
+ }
76
+ else
77
+ await writeFileAtomically(target, commit.contract);
78
+ // Keep the active contract until every archive file is durable. It is the lane's visible handle
79
+ // throughout cleanup; the commit independently covers a crash after that handle is removed.
80
+ for (const { suffix } of commit.logs)
81
+ await rm(join(tasks, `${commit.id}.jsonl${suffix}`), { force: true });
82
+ await rm(join(tasks, `${commit.id}.md`), { force: true });
83
+ await syncDirectory(tasks);
84
+ await rm(taskArchiveCommitPath(deskDir, commit.id), { force: true });
85
+ await syncDirectory(join(tasks, ".closing"));
86
+ }
87
+ /** Snapshot once, publish all shards, then clean up. Ordinary I/O failures and SIGKILL use the same recovery path. */
88
+ export async function commitTaskArchive(deskDir, id, document, outcome, closedAt) {
89
+ const commitPath = taskArchiveCommitPath(deskDir, id);
90
+ if (existsSync(commitPath))
91
+ return finishCommit(deskDir, await readCommit(commitPath, id));
92
+ if (!document || !outcome || !closedAt)
93
+ return;
94
+ const tasks = join(deskDir, "tasks");
95
+ for (const name of [`${id}.md`, ...SUFFIXES.map((suffix) => `${id}.jsonl${suffix}`)]) {
96
+ const target = join(tasks, "archive", name);
97
+ if (existsSync(target))
98
+ throw new Error(`archive entry already exists, refusing to overwrite: ${target}`);
99
+ }
100
+ const logs = [];
101
+ for (const suffix of SUFFIXES) {
102
+ const source = join(tasks, `${id}.jsonl${suffix}`);
103
+ if (existsSync(source))
104
+ logs.push({ suffix, sha256: digest(await readFile(source)) });
105
+ }
106
+ const commit = {
107
+ id,
108
+ contract: renderTaskDocument({ ...document.fields, outcome, closedAt }, document.body),
109
+ logs,
110
+ };
111
+ await writeFileAtomically(commitPath, JSON.stringify(commit));
112
+ await finishCommit(deskDir, commit);
113
+ }
114
+ /** Only unfinished closes are enumerated, never the growing historical archive. */
115
+ export async function recoverTaskArchives(deskDir) {
116
+ const dir = join(deskDir, "tasks", ".closing");
117
+ let names;
118
+ try {
119
+ names = await readdir(dir);
120
+ }
121
+ catch (error) {
122
+ if (error.code === "ENOENT")
123
+ return;
124
+ throw error;
125
+ }
126
+ for (const name of names.filter((name) => name.endsWith(".json"))) {
127
+ try {
128
+ const id = normalizeTaskId(name.slice(0, -5));
129
+ await withTaskMutation(deskDir, id, async () => {
130
+ if (isTaskArchiving(deskDir, id))
131
+ await commitTaskArchive(deskDir, id);
132
+ });
133
+ }
134
+ catch (error) {
135
+ log.logWarn("tasks.archive.recover_failed", "Task archive recovery failed", {
136
+ reason: errorMessage(error),
137
+ fields: { deskDir, file: name },
138
+ });
139
+ }
140
+ }
141
+ }
@@ -1,13 +1,13 @@
1
1
  import { type TaskItem } from "./contract.js";
2
- import { type TaskSettleRecord } from "./loop-log.js";
2
+ import { type TaskLogRecord, type TaskSettleRecord } from "./loop-log.js";
3
3
  /**
4
4
  * The team board (spec 052, D5): what the leader's delegations and jobs are doing right now.
5
5
  *
6
6
  * It is derived from the loop log, never from the run registry — settled runs are garbage
7
7
  * collected after a week, while a project can outlive that (INV-5). Every row is one `ref`
8
8
  * (a run or job id) with its latest state; rows are grouped under the Work Item they were
9
- * dispatched for. The board itself carries status and output *paths*; only results that came back
10
- * since the previous step also carry their tail, in `renderNewResults`.
9
+ * dispatched for. The board itself carries status and output *paths*; what came back since the
10
+ * previous step (with its output tail) reaches the leader as `<task_inputs>`.
11
11
  */
12
12
  export interface BoardRef {
13
13
  ref: string;
@@ -16,7 +16,7 @@ export interface BoardRef {
16
16
  purpose?: "work" | "verify";
17
17
  dispatchedAtMs: number;
18
18
  settled?: TaskSettleRecord;
19
- /** Settled since the previous step ended — i.e. what this step was (probably) woken for. */
19
+ /** Settled since the previous step began — kept on the board even when old results are folded away. */
20
20
  isNew: boolean;
21
21
  }
22
22
  export interface TaskBoard {
@@ -31,7 +31,7 @@ export interface TaskBoard {
31
31
  }
32
32
  /** Ref lines the board renders before folding old, already-seen results away. */
33
33
  export declare const BOARD_REF_LINE_BUDGET = 12;
34
- export declare function buildTaskBoard(deskDir: string, taskId: string, body: string): Promise<TaskBoard | undefined>;
34
+ export declare function buildTaskBoard(records: readonly TaskLogRecord[], body: string): TaskBoard | undefined;
35
35
  /** Render the `<task_board>` block body (model-facing; `/tasks show` uses `renderTaskBoardForUser`). */
36
36
  export declare function renderTaskBoard(board: TaskBoard, nowMs?: number): string;
37
37
  /**
@@ -41,10 +41,3 @@ export declare function renderTaskBoard(board: TaskBoard, nowMs?: number): strin
41
41
  * de-duplication aid) and no pointer at a model tool.
42
42
  */
43
43
  export declare function renderTaskBoardForUser(board: TaskBoard, taskId: string, nowMs?: number): string;
44
- /**
45
- * The `<task_results>` block body: what came back since the previous step, as the completion wake
46
- * showed it — the output's tail and, for a run, what it left changed. A task step's brief replaces
47
- * the wake text, so without this block a task step would see less of a result than a chat turn.
48
- * Returns `undefined` when nothing new settled.
49
- */
50
- export declare function renderNewResults(board: TaskBoard): string | undefined;
@@ -2,11 +2,10 @@ import { formatElapsedZh } from "../shared/format.js";
2
2
  import { parseLocalTime } from "../shared/local-time.js";
3
3
  import { truncate } from "../shared/text-utils.js";
4
4
  import { parseTaskItems } from "./contract.js";
5
- import { describeSettlement, describeWorkRef, readTaskLog, } from "./loop-log.js";
5
+ import { describeSettlement, describeWorkRef, } from "./loop-log.js";
6
6
  /** Ref lines the board renders before folding old, already-seen results away. */
7
7
  export const BOARD_REF_LINE_BUDGET = 12;
8
- export async function buildTaskBoard(deskDir, taskId, body) {
9
- const records = await readTaskLog(deskDir, taskId, { kinds: ["dispatch", "settle", "step"] });
8
+ export function buildTaskBoard(records, body) {
10
9
  const dispatches = new Map();
11
10
  const settles = new Map();
12
11
  let lastStepIndex = -1;
@@ -31,7 +30,7 @@ export async function buildTaskBoard(deskDir, taskId, body) {
31
30
  isNew: settle !== undefined && settle.index > lastStepIndex,
32
31
  });
33
32
  }
34
- // A settle with no dispatch record (a run bound before this version) still belongs on the board.
33
+ // A settle with no dispatch record (its dispatch line was lost) still belongs on the board.
35
34
  for (const [ref, settle] of settles) {
36
35
  if (dispatches.has(ref))
37
36
  continue;
@@ -87,10 +86,7 @@ function renderRef(entry, nowMs) {
87
86
  parts.push(`exit ${settle.exitCode}`);
88
87
  if (settle.durationMs !== undefined)
89
88
  parts.push(formatDuration(settle.durationMs));
90
- const tail = [entry.isNew ? "★新" : undefined, settle.output ? `output: ${settle.output}` : undefined]
91
- .filter(Boolean)
92
- .join(" · ");
93
- return `${label} ${parts.join(" ")}${tail ? ` ${tail}` : ""}`;
89
+ return `${label} ${parts.join(" ")}${settle.output ? ` output: ${settle.output}` : ""}`;
94
90
  }
95
91
  const ITEM_MARKER = { todo: " ", done: "x", blocked: "!", dropped: "~" };
96
92
  /** Render the `<task_board>` block body (model-facing; `/tasks show` uses `renderTaskBoardForUser`). */
@@ -145,25 +141,3 @@ export function renderTaskBoardForUser(board, taskId, nowMs = Date.now()) {
145
141
  }
146
142
  return lines.join("\n");
147
143
  }
148
- /**
149
- * The `<task_results>` block body: what came back since the previous step, as the completion wake
150
- * showed it — the output's tail and, for a run, what it left changed. A task step's brief replaces
151
- * the wake text, so without this block a task step would see less of a result than a chat turn.
152
- * Returns `undefined` when nothing new settled.
153
- */
154
- export function renderNewResults(board) {
155
- const fresh = [...board.items.flatMap((entry) => entry.refs), ...board.unlinked].filter((entry) => entry.isNew && entry.settled);
156
- if (fresh.length === 0)
157
- return undefined;
158
- return fresh
159
- .map((entry) => {
160
- const settle = entry.settled;
161
- const head = [entry.ref, entry.item, entry.agent, settle.status].filter(Boolean).join(" · ");
162
- const lines = [`[${head}]`];
163
- if (settle.changed)
164
- lines.push(`改动(git status):${settle.changed}`);
165
- lines.push(`<untrusted_agent_output>\n${settle.tail ?? "(无输出)"}\n</untrusted_agent_output>`);
166
- return lines.join("\n");
167
- })
168
- .join("\n\n");
169
- }
@@ -2,19 +2,25 @@ import { type TaskDispatchRecord, type TaskSettleRecord } from "./loop-log.js";
2
2
  /**
3
3
  * The run and job managers' only way to tell a task about work bound to it (spec 052, D4).
4
4
  *
5
- * Both are best-effort by design: a task whose file has since been archived or hand-deleted must
6
- * not block a completion wake, and a failed log write must not fail a dispatch. Each caller guards
7
- * these with its own idempotency marker (`taskAccounted` for runs, `taskLogged` for jobs) so a
8
- * replayed settlement writes nothing twice.
5
+ * Dispatch logs may be best-effort. Settlement logs and cost are committed together; ref-based
6
+ * deduplication lets the owning managers retry a failed receipt without charging twice.
9
7
  */
10
8
  /** Record that a delegation or job was bound to the task. A task that no longer exists is skipped. */
11
9
  export declare function logTaskDispatch(deskDir: string, taskId: string, record: Omit<TaskDispatchRecord, "ts" | "kind">): Promise<void>;
12
10
  export interface TaskSettlementCost {
13
11
  usd: number;
14
12
  estimated: boolean;
13
+ known?: boolean;
15
14
  }
16
15
  /**
17
16
  * Record that bound work settled and add its cost to the task's usage. The cost lands in the
18
- * same budget the leader's own steps spend from, so a runaway fan-out stops the task.
17
+ * same budget the leader's own steps spend from, so a runaway fan-out stops the task. Returns
18
+ * whether the task still existed to receive the record — callers fall back to a chat wake when it
19
+ * did not (spec 064, D19).
19
20
  */
20
- export declare function logTaskSettlement(deskDir: string, taskId: string, record: Omit<TaskSettleRecord, "ts" | "kind" | "usd" | "usdEstimated">, cost?: TaskSettlementCost): Promise<void>;
21
+ export declare function logTaskSettlement(deskDir: string, taskId: string, record: Omit<TaskSettleRecord, "ts" | "kind" | "usd" | "usdEstimated">, cost?: TaskSettlementCost): Promise<boolean>;
22
+ /** An external run bound to the task has been silent for `silentMinutes`: an input the leader judges (D16). */
23
+ export declare function logTaskStall(deskDir: string, taskId: string, record: {
24
+ ref: string;
25
+ silentMinutes: number;
26
+ }): Promise<boolean>;
@@ -1,12 +1,11 @@
1
- import { appendTaskLog } from "./loop-log.js";
2
- import { readStoredTask, updateStoredTask } from "./store.js";
1
+ import { appendTaskLog, readTaskLog } from "./loop-log.js";
2
+ import { withTaskMutation } from "./mutation-lock.js";
3
+ import { commitTask, readStoredTask } from "./store.js";
3
4
  /**
4
5
  * The run and job managers' only way to tell a task about work bound to it (spec 052, D4).
5
6
  *
6
- * Both are best-effort by design: a task whose file has since been archived or hand-deleted must
7
- * not block a completion wake, and a failed log write must not fail a dispatch. Each caller guards
8
- * these with its own idempotency marker (`taskAccounted` for runs, `taskLogged` for jobs) so a
9
- * replayed settlement writes nothing twice.
7
+ * Dispatch logs may be best-effort. Settlement logs and cost are committed together; ref-based
8
+ * deduplication lets the owning managers retry a failed receipt without charging twice.
10
9
  */
11
10
  /** Record that a delegation or job was bound to the task. A task that no longer exists is skipped. */
12
11
  export async function logTaskDispatch(deskDir, taskId, record) {
@@ -16,27 +15,41 @@ export async function logTaskDispatch(deskDir, taskId, record) {
16
15
  }
17
16
  /**
18
17
  * Record that bound work settled and add its cost to the task's usage. The cost lands in the
19
- * same budget the leader's own steps spend from, so a runaway fan-out stops the task.
18
+ * same budget the leader's own steps spend from, so a runaway fan-out stops the task. Returns
19
+ * whether the task still existed to receive the record — callers fall back to a chat wake when it
20
+ * did not (spec 064, D19).
20
21
  */
21
22
  export async function logTaskSettlement(deskDir, taskId, record, cost = { usd: 0, estimated: false }) {
22
- if (cost.usd > 0 || cost.estimated) {
23
- await updateStoredTask(deskDir, taskId, (task) => {
23
+ return withTaskMutation(deskDir, taskId, async () => {
24
+ const task = await readStoredTask(deskDir, taskId);
25
+ if (!task)
26
+ return false;
27
+ if ((await readTaskLog(deskDir, taskId, { strict: true })).some((entry) => entry.kind === "settle" && entry.ref === record.ref))
28
+ return true;
29
+ if (cost.usd > 0 || cost.estimated || cost.known === false) {
24
30
  const usage = task.fields.usage;
25
- if (!usage)
26
- return;
27
- task.fields.usage = {
28
- ...usage,
29
- usd: usage.usd + Math.max(0, cost.usd),
30
- usdEstimated: usage.usdEstimated || cost.estimated,
31
- };
31
+ if (usage)
32
+ task.fields.usage = {
33
+ ...usage,
34
+ usd: usage.usd + Math.max(0, cost.usd),
35
+ usdEstimated: usage.usdEstimated || cost.estimated,
36
+ ...(cost.known === false ? { unknownCostCount: (usage.unknownCostCount ?? 0) + 1 } : {}),
37
+ };
38
+ }
39
+ await commitTask(task, deskDir, {
40
+ kind: "settle",
41
+ ...record,
42
+ ...(cost.usd > 0 ? { usd: cost.usd } : {}),
43
+ ...(cost.estimated ? { usdEstimated: true } : {}),
44
+ ...(cost.known === false ? { usdKnown: false } : {}),
32
45
  });
33
- }
34
- if (!(await readStoredTask(deskDir, taskId)))
35
- return;
36
- await appendTaskLog(deskDir, taskId, {
37
- kind: "settle",
38
- ...record,
39
- ...(cost.usd > 0 ? { usd: cost.usd } : {}),
40
- ...(cost.estimated ? { usdEstimated: true } : {}),
46
+ return true;
41
47
  });
42
48
  }
49
+ /** An external run bound to the task has been silent for `silentMinutes`: an input the leader judges (D16). */
50
+ export async function logTaskStall(deskDir, taskId, record) {
51
+ if (!(await readStoredTask(deskDir, taskId)))
52
+ return false;
53
+ await appendTaskLog(deskDir, taskId, { kind: "stall", ...record });
54
+ return true;
55
+ }
@@ -1,22 +1,46 @@
1
- /** How many loop-log lines a step brief carries. Older records stay addressable via `task_log`. */
2
- export declare const BRIEF_LOG_LINES = 8;
1
+ import { type TaskInputRecord, type TaskLogRecord } from "./loop-log.js";
2
+ import { type StoredTaskDocument } from "./store.js";
3
+ /** How many `step_end` / `close` lines a full brief carries. Older records stay addressable via `task_log`. */
4
+ export declare const BRIEF_LOG_LINES = 5;
5
+ /** Inputs a brief lists before folding the older ones into a pointer at `task_log`. */
6
+ export declare const BRIEF_INPUT_LIMIT = 12;
3
7
  /** Cap on each part (close note, last report) of the previous occurrence carried into a spawned instance's first step. */
4
8
  export declare const PREVIOUS_OCCURRENCE_MAX_CHARS = 1200;
9
+ /** The first 12 hex digits of the contract body's hash: what a session checks to know it holds the current contract. */
10
+ export declare function contractSha(body: string): string;
11
+ /** The opening tag of a full brief's contract block — one function, so the brief and the lane's "is it in context?" check cannot drift. */
12
+ export declare function contractOpenTag(taskId: string, sha: string): string;
13
+ /** A delegation or job bound to the task and still unsettled, as the brief's state line shows it. */
14
+ export interface PendingWorkView {
15
+ ref: string;
16
+ label?: string;
17
+ runningMs: number;
18
+ }
5
19
  export interface TaskBriefInput {
6
20
  deskDir: string;
7
- taskId: string;
8
- /** Why this step is running, when the caller knows. */
9
- reason?: string;
21
+ document: StoredTaskDocument;
22
+ seq: number;
23
+ /** The records as they stood when the step began (before its own `step` record). */
24
+ records: readonly TaskLogRecord[];
25
+ /** `newInputs(records)` — inputs not yet confirmed in the persisted task transcript. */
26
+ inputs: readonly TaskInputRecord[];
27
+ /** True when the session does not hold the current contract: first step, compaction, or an edited contract. */
28
+ full: boolean;
29
+ pending: readonly PendingWorkView[];
30
+ now?: number;
31
+ }
32
+ export interface TaskBrief {
33
+ text: string;
34
+ contractSha: string;
10
35
  }
36
+ /** The `<task_inputs>` block body, or `undefined` when nothing new arrived. Executor output is data, not instruction. */
37
+ export declare function renderTaskInputs(records: readonly TaskLogRecord[], inputs: readonly TaskInputRecord[]): string | undefined;
11
38
  /**
12
- * The turn input for one task loop step (spec 051, D3; spec 052, D5).
39
+ * The turn input for one task step (spec 064, D15).
13
40
  *
14
- * The authored contract is injected whole; authors must keep it short. What changes from step to
15
- * step is carried by the `<task_board>` (what each delegation is doing and what just came back)
16
- * and the last few log lines. The wake text of a settled delegation is *not* shown to a task step
17
- * — the brief replaces it — so the board tells the leader which results are new and
18
- * `<task_results>` carries what the wake would have shown for them. Any
19
- * pending `/tasks steer` or `/tasks reply` is consumed here and put first: it is the one thing in
20
- * the brief that is genuinely new since the previous step.
41
+ * A step that already has the current contract in its session gets only the delta: the new inputs,
42
+ * the board and the state line. A step that does not — the first, one after compaction dropped the
43
+ * contract, one after the contract changed — gets the contract whole plus the recent log. The caller
44
+ * decides which (`full`) by looking at the session, not at a flag it keeps.
21
45
  */
22
- export declare function buildTaskStepBrief(input: TaskBriefInput): Promise<string | undefined>;
46
+ export declare function buildTaskStepBrief(input: TaskBriefInput): Promise<TaskBrief>;