@oyasmi/pipiclaw 0.9.3-beta.5 → 0.9.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.
- package/README.en.md +1 -1
- package/README.md +1 -1
- package/dist/agent/channel-runner.d.ts +4 -8
- package/dist/agent/channel-runner.js +14 -34
- package/dist/agent/job-manager.d.ts +12 -0
- package/dist/agent/job-manager.js +42 -1
- package/dist/agent/prompt/sections.js +2 -2
- package/dist/agent/runner-factory.d.ts +2 -2
- package/dist/agent/types.d.ts +11 -7
- package/dist/channel/channel-index.d.ts +9 -2
- package/dist/channel/channel-index.js +25 -6
- package/dist/channel/project-scope-store.d.ts +20 -0
- package/dist/channel/project-scope-store.js +22 -7
- package/dist/commands/catalog.js +6 -12
- package/dist/events/event-commands.js +2 -2
- package/dist/events/events.d.ts +51 -30
- package/dist/events/events.js +70 -98
- package/dist/memory/journal.d.ts +2 -3
- package/dist/memory/journal.js +2 -3
- package/dist/memory/maintenance-tuning.d.ts +0 -1
- package/dist/memory/maintenance-tuning.js +0 -1
- package/dist/memory/scheduler.js +8 -16
- package/dist/memory/search.js +2 -3
- package/dist/memory/session-search.js +3 -9
- package/dist/memory/store.d.ts +1 -1
- package/dist/memory/store.js +9 -1
- package/dist/memory/task-digest.js +10 -11
- package/dist/memory/tombstones.js +2 -4
- package/dist/models/provider-login.d.ts +1 -1
- package/dist/models/provider-login.js +1 -1
- package/dist/playbooks/agent-delegation.md +6 -6
- package/dist/playbooks/background-jobs.md +1 -1
- package/dist/playbooks/event-scheduling.md +22 -13
- package/dist/playbooks/memory-and-learning.md +2 -2
- package/dist/playbooks/outbound-media.md +1 -1
- package/dist/playbooks/runtime-orientation.md +3 -3
- package/dist/playbooks/task-lead.md +82 -0
- package/dist/runtime/bootstrap.js +94 -187
- package/dist/runtime/durable-dispatch.js +30 -3
- package/dist/runtime/report-commands.d.ts +30 -0
- package/dist/runtime/report-commands.js +71 -0
- package/dist/runtime/subagent-commands.js +1 -1
- package/dist/runtime/task-commands.d.ts +0 -8
- package/dist/runtime/task-commands.js +48 -85
- package/dist/runtime/task-driver.d.ts +15 -10
- package/dist/runtime/task-driver.js +36 -41
- package/dist/runtime/task-migration.d.ts +23 -29
- package/dist/runtime/task-migration.js +364 -183
- package/dist/runtime/task-spawn.d.ts +24 -0
- package/dist/runtime/task-spawn.js +49 -0
- package/dist/runtime/task-wake.d.ts +33 -46
- package/dist/runtime/task-wake.js +15 -90
- package/dist/security/network.js +44 -9
- package/dist/settings.d.ts +1 -8
- package/dist/settings.js +1 -8
- package/dist/subagents/discovery.js +3 -15
- package/dist/subagents/external/run.d.ts +3 -1
- package/dist/subagents/external/run.js +2 -20
- package/dist/subagents/external/settlement.d.ts +4 -16
- package/dist/subagents/external/settlement.js +16 -67
- package/dist/subagents/runs.d.ts +15 -29
- package/dist/subagents/runs.js +70 -158
- package/dist/subagents/tool.d.ts +20 -4
- package/dist/subagents/tool.js +63 -87
- package/dist/subagents/verification-outcome.d.ts +13 -19
- package/dist/subagents/verification-outcome.js +8 -22
- package/dist/subagents/workspace-summary.d.ts +8 -0
- package/dist/subagents/workspace-summary.js +29 -0
- package/dist/tasks/board.d.ts +43 -0
- package/dist/tasks/board.js +134 -0
- package/dist/tasks/brief.d.ts +10 -5
- package/dist/tasks/brief.js +78 -26
- package/dist/tasks/budget.d.ts +13 -14
- package/dist/tasks/budget.js +21 -40
- package/dist/tasks/contract-input.d.ts +19 -0
- package/dist/tasks/contract-input.js +55 -0
- package/dist/tasks/create.d.ts +17 -0
- package/dist/tasks/create.js +36 -0
- package/dist/tasks/frontmatter.d.ts +20 -27
- package/dist/tasks/frontmatter.js +18 -31
- package/dist/tasks/ledger.d.ts +61 -117
- package/dist/tasks/ledger.js +78 -223
- package/dist/tasks/log.d.ts +62 -35
- package/dist/tasks/log.js +51 -29
- package/dist/tasks/session-path.d.ts +3 -3
- package/dist/tasks/session-path.js +4 -5
- package/dist/tasks/store.d.ts +27 -28
- package/dist/tasks/store.js +89 -83
- package/dist/tasks/ticket.d.ts +33 -66
- package/dist/tasks/ticket.js +31 -120
- package/dist/tasks/work-log.d.ts +20 -0
- package/dist/tasks/work-log.js +42 -0
- package/dist/tools/bash.d.ts +1 -1
- package/dist/tools/bash.js +1 -1
- package/dist/tools/event-manage.d.ts +16 -2
- package/dist/tools/event-manage.js +30 -12
- package/dist/tools/index.d.ts +5 -3
- package/dist/tools/index.js +5 -5
- package/dist/tools/registry.d.ts +6 -4
- package/dist/tools/registry.js +1 -4
- package/dist/tools/subagent-manage.js +4 -2
- package/dist/tools/task-manage/create.js +11 -27
- package/dist/tools/task-manage/lifecycle.d.ts +6 -7
- package/dist/tools/task-manage/lifecycle.js +23 -106
- package/dist/tools/task-manage/log-view.d.ts +3 -2
- package/dist/tools/task-manage/log-view.js +6 -6
- package/dist/tools/task-manage/schema.d.ts +10 -30
- package/dist/tools/task-manage/schema.js +33 -49
- package/dist/tools/task-manage/shared.d.ts +35 -42
- package/dist/tools/task-manage/shared.js +53 -192
- package/dist/tools/task-manage/step-end.d.ts +6 -6
- package/dist/tools/task-manage/step-end.js +61 -98
- package/dist/tools/task-manage/types.d.ts +8 -9
- package/dist/tools/task-manage.js +5 -5
- package/dist/tui/app.js +11 -38
- package/dist/tui/commands.d.ts +18 -8
- package/dist/tui/commands.js +2 -9
- package/dist/tui/turn-controller.d.ts +2 -14
- package/dist/tui/turn-controller.js +1 -17
- package/dist/web/client.js +9 -1
- package/examples/sub-agents/README.md +7 -4
- package/examples/sub-agents/agents/codex-flash.md +2 -2
- package/examples/sub-agents/agents/codex-high.md +1 -1
- package/examples/sub-agents/agents/codex-main.md +1 -1
- package/examples/sub-agents/agents/codex-review.md +23 -0
- package/examples/sub-agents/roles/verifier.md +1 -1
- package/package.json +1 -1
- package/dist/memory/maintenance-migration.d.ts +0 -5
- package/dist/memory/maintenance-migration.js +0 -91
- package/dist/memory/migrate.d.ts +0 -15
- package/dist/memory/migrate.js +0 -364
- package/dist/playbooks/task-loop.md +0 -90
- package/dist/tasks/artifact-subject.d.ts +0 -40
- package/dist/tasks/artifact-subject.js +0 -334
- package/dist/tasks/cycle.d.ts +0 -48
- package/dist/tasks/cycle.js +0 -94
- package/dist/tasks/rounds.d.ts +0 -41
- package/dist/tasks/rounds.js +0 -53
- package/dist/tasks/task-events.d.ts +0 -6
- package/dist/tasks/task-events.js +0 -14
- package/dist/tasks/task-schedule.d.ts +0 -18
- package/dist/tasks/task-schedule.js +0 -50
- package/dist/tasks/verification.d.ts +0 -114
- package/dist/tasks/verification.js +0 -226
|
@@ -4,24 +4,27 @@ import { join } from "node:path";
|
|
|
4
4
|
import { renderSubcommandUsage } from "../commands/catalog.js";
|
|
5
5
|
import { parseLocalTime } from "../shared/local-time.js";
|
|
6
6
|
import { clipText, errorMessage } from "../shared/text-utils.js";
|
|
7
|
+
import { buildTaskBoard, renderTaskBoard } from "../tasks/board.js";
|
|
7
8
|
import { normalizeTaskId, readActiveTasks } from "../tasks/ledger.js";
|
|
8
9
|
import { readTaskLog, renderTaskLogLine } from "../tasks/log.js";
|
|
9
10
|
import { withTaskMutation } from "../tasks/mutation-lock.js";
|
|
10
11
|
import { hasTaskSteer, queueTaskSteer } from "../tasks/steer.js";
|
|
11
|
-
import {
|
|
12
|
+
import { pauseTask as pauseTaskDocument, readStoredTask, redeemTicket, resumeTask as resumeTaskDocument, updateStoredTask, } from "../tasks/store.js";
|
|
12
13
|
import { describeTicket } from "../tasks/ticket.js";
|
|
13
14
|
/** `/tasks show <id>` head-snippet cap; the full file is always readable at its own path. */
|
|
14
15
|
const TASK_SHOW_MAX_CHARS = 2_400;
|
|
15
16
|
const SHOW_LOG_LINES = 8;
|
|
17
|
+
/** A backstop this far past due means the driver is not running, not that it is merely about to act. */
|
|
18
|
+
const EXPIRED_GRACE_MS = 60 * 60 * 1000;
|
|
16
19
|
// Broadcast (subcommand names, args, descriptions, examples) lives once in commands/catalog.ts's
|
|
17
20
|
// `BUILT_IN_COMMANDS` entry for "tasks"; this just renders it (review 2026-08-24 §3.1).
|
|
18
21
|
function usage() {
|
|
19
22
|
return renderSubcommandUsage("tasks");
|
|
20
23
|
}
|
|
21
|
-
/** `+steps 20`, `+
|
|
24
|
+
/** `+steps 20`, `+usd 5` — the two grants `/tasks resume` accepts. */
|
|
22
25
|
function parseGrants(rest) {
|
|
23
26
|
const grants = {};
|
|
24
|
-
const pattern = /\+(steps|
|
|
27
|
+
const pattern = /\+(steps|usd)\s+([0-9]+(?:\.[0-9]+)?)/g;
|
|
25
28
|
for (const match of rest.matchAll(pattern)) {
|
|
26
29
|
const value = Number(match[2]);
|
|
27
30
|
if (!Number.isFinite(value) || value <= 0)
|
|
@@ -45,22 +48,16 @@ export function parseTasksCommand(args) {
|
|
|
45
48
|
throw new Error(`用法:/tasks ${action}`);
|
|
46
49
|
return { action };
|
|
47
50
|
}
|
|
48
|
-
if (action === "show" || action === "pause" || action === "
|
|
51
|
+
if (action === "show" || action === "pause" || action === "log") {
|
|
49
52
|
const id = parts[1];
|
|
50
53
|
if (!id || parts.length > 2)
|
|
51
54
|
throw new Error(`用法:/tasks ${action} <id>`);
|
|
52
55
|
return { action, id };
|
|
53
56
|
}
|
|
54
|
-
if (action === "log") {
|
|
55
|
-
const id = parts[1];
|
|
56
|
-
if (!id || parts.length > 3)
|
|
57
|
-
throw new Error("用法:/tasks log <id> [cycle]");
|
|
58
|
-
return { action: "log", id, cycle: parts[2] };
|
|
59
|
-
}
|
|
60
57
|
if (action === "resume") {
|
|
61
58
|
const id = parts[1];
|
|
62
59
|
if (!id)
|
|
63
|
-
throw new Error("用法:/tasks resume <id> [+steps N|+
|
|
60
|
+
throw new Error("用法:/tasks resume <id> [+steps N|+usd X]");
|
|
64
61
|
return { action: "resume", id, grants: parseGrants(parts.slice(2).join(" ")) };
|
|
65
62
|
}
|
|
66
63
|
if (action === "steer" || action === "reply") {
|
|
@@ -91,12 +88,12 @@ function relative(atMs, now) {
|
|
|
91
88
|
return `${Math.round(hours / 24)}d`;
|
|
92
89
|
}
|
|
93
90
|
const STATE_LABEL = { open: "进行中", parked: "等待中", done: "已关闭" };
|
|
94
|
-
function
|
|
95
|
-
const
|
|
96
|
-
if (!
|
|
91
|
+
function usageSummary(entry) {
|
|
92
|
+
const usage = entry.fields.usage;
|
|
93
|
+
if (!usage)
|
|
97
94
|
return undefined;
|
|
98
|
-
const cost =
|
|
99
|
-
return `${
|
|
95
|
+
const cost = usage.usd > 0 ? `,$${usage.usd.toFixed(2)}${usage.usdEstimated ? "(含估算)" : ""}` : "";
|
|
96
|
+
return `${usage.steps} 步${cost}`;
|
|
100
97
|
}
|
|
101
98
|
function ticketSummary(entry, now) {
|
|
102
99
|
const ticket = entry.fields.ticket;
|
|
@@ -112,11 +109,13 @@ function renderListLine(entry, channelDir, now) {
|
|
|
112
109
|
const ticket = ticketSummary(entry, now);
|
|
113
110
|
if (ticket)
|
|
114
111
|
bits.push(ticket);
|
|
115
|
-
if (entry.
|
|
116
|
-
bits.push(`
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
112
|
+
if (entry.items)
|
|
113
|
+
bits.push(`items ${entry.items.done}/${entry.items.total}`);
|
|
114
|
+
if (entry.fields.origin)
|
|
115
|
+
bits.push(`来自 ${entry.fields.origin}`);
|
|
116
|
+
const usage = usageSummary(entry);
|
|
117
|
+
if (usage)
|
|
118
|
+
bits.push(usage);
|
|
120
119
|
if (hasTaskSteer(channelDir, entry.id))
|
|
121
120
|
bits.push("有待处理的指示");
|
|
122
121
|
if (!entry.readable)
|
|
@@ -146,22 +145,20 @@ async function showTask(channelDir, idInput) {
|
|
|
146
145
|
const by = relative(parseLocalTime(document.fields.ticket.by), now);
|
|
147
146
|
head.push(`- 等待:${describeTicket(document.fields.ticket)}${by ? `(兜底 ${by})` : ""}`);
|
|
148
147
|
}
|
|
149
|
-
if (document.fields.
|
|
150
|
-
head.push(`-
|
|
151
|
-
const
|
|
152
|
-
if (
|
|
153
|
-
head.push(`-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
const
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
const log = await readTaskLog(channelDir, id, { limit: SHOW_LOG_LINES });
|
|
148
|
+
if (document.fields.origin)
|
|
149
|
+
head.push(`- 来自事件:${document.fields.origin}`);
|
|
150
|
+
const usage = entry ? usageSummary(entry) : undefined;
|
|
151
|
+
if (usage)
|
|
152
|
+
head.push(`- 用量:${usage}`);
|
|
153
|
+
const board = await buildTaskBoard(channelDir, id, document.body);
|
|
154
|
+
const boardLines = board ? ["", "**团队看板**", renderTaskBoard(board, now)] : [];
|
|
155
|
+
const log = await readTaskLog(channelDir, id, {
|
|
156
|
+
kinds: ["step", "expired", "close", "note"],
|
|
157
|
+
limit: SHOW_LOG_LINES,
|
|
158
|
+
});
|
|
162
159
|
const logLines = log.length > 0 ? ["", `**最近 ${log.length} 条日志**`, ...log.map(renderTaskLogLine)] : [];
|
|
163
160
|
const body = clipText(document.body.trim(), TASK_SHOW_MAX_CHARS);
|
|
164
|
-
return [...head, ...logLines, "", "**契约**", body, "", `完整日志:/tasks log ${id}`].join("\n");
|
|
161
|
+
return [...head, ...boardLines, ...logLines, "", "**契约**", body, "", `完整日志:/tasks log ${id}`].join("\n");
|
|
165
162
|
}
|
|
166
163
|
async function listArchive(channelDir) {
|
|
167
164
|
const archiveDir = join(tasksDir(channelDir), "archive");
|
|
@@ -180,14 +177,14 @@ async function listArchive(channelDir) {
|
|
|
180
177
|
lines.push(`- (另有 ${files.length - 20} 个,见 tasks/archive/)`);
|
|
181
178
|
return [`**已归档任务(${files.length})**`, ...lines].join("\n");
|
|
182
179
|
}
|
|
183
|
-
async function showTaskLog(channelDir, idInput
|
|
180
|
+
async function showTaskLog(channelDir, idInput) {
|
|
184
181
|
const id = normalizeTaskId(idInput);
|
|
185
|
-
const records = await readTaskLog(channelDir, id, {
|
|
182
|
+
const records = await readTaskLog(channelDir, id, { limit: 30 });
|
|
186
183
|
if (records.length === 0)
|
|
187
|
-
return `任务 \`${id}\`
|
|
184
|
+
return `任务 \`${id}\` 暂无循环日志。`;
|
|
188
185
|
return [`**任务 ${id} 日志(${records.length} 条)**`, ...records.map(renderTaskLogLine)].join("\n");
|
|
189
186
|
}
|
|
190
|
-
|
|
187
|
+
async function pauseTask(options, idInput) {
|
|
191
188
|
const id = normalizeTaskId(idInput);
|
|
192
189
|
const document = await pauseTaskDocument(options.channelDir, id, {
|
|
193
190
|
by: "user",
|
|
@@ -206,18 +203,14 @@ export async function resumeTask(options, idInput, grants = {}) {
|
|
|
206
203
|
if (Object.keys(grants).length > 0) {
|
|
207
204
|
await updateStoredTask(options.channelDir, id, (task) => {
|
|
208
205
|
const budget = { ...task.fields.budget };
|
|
209
|
-
for (const key of ["steps", "
|
|
206
|
+
for (const key of ["steps", "usd"]) {
|
|
210
207
|
const extra = grants[key];
|
|
211
208
|
if (extra === undefined)
|
|
212
209
|
continue;
|
|
213
|
-
// A grant is *additional* headroom on top of whatever
|
|
210
|
+
// A grant is *additional* headroom on top of whatever the task already spent, so
|
|
214
211
|
// resuming a task that hit its ceiling actually lets it run rather than re-stopping
|
|
215
212
|
// on the next step.
|
|
216
|
-
const spent = key === "steps"
|
|
217
|
-
? task.fields.cycle?.steps
|
|
218
|
-
: key === "rounds"
|
|
219
|
-
? task.fields.cycle?.rounds
|
|
220
|
-
: task.fields.cycle?.usd;
|
|
213
|
+
const spent = key === "steps" ? task.fields.usage?.steps : task.fields.usage?.usd;
|
|
221
214
|
budget[key] = (spent ?? 0) + extra;
|
|
222
215
|
granted.push(`${key}+${extra}`);
|
|
223
216
|
}
|
|
@@ -226,24 +219,6 @@ export async function resumeTask(options, idInput, grants = {}) {
|
|
|
226
219
|
}
|
|
227
220
|
return `已恢复任务 \`${id}\`${granted.length > 0 ? `(${granted.join(",")})` : ""}。`;
|
|
228
221
|
}
|
|
229
|
-
async function runTask(options, idInput) {
|
|
230
|
-
const id = normalizeTaskId(idInput);
|
|
231
|
-
const document = await readStoredTask(options.channelDir, id);
|
|
232
|
-
if (!document)
|
|
233
|
-
return `未找到任务 \`${id}\`。`;
|
|
234
|
-
if (document.fields.paused) {
|
|
235
|
-
return `任务 \`${id}\` 处于暂停状态;先用 /tasks resume ${id} 恢复,再 /tasks run。`;
|
|
236
|
-
}
|
|
237
|
-
if (!document.fields.cycle || document.fields.state === "parked") {
|
|
238
|
-
const opened = await openCycle(options.channelDir, id);
|
|
239
|
-
if (!opened)
|
|
240
|
-
return `任务 \`${id}\` 无法开启新周期(可能已归档)。`;
|
|
241
|
-
}
|
|
242
|
-
const dispatched = await options.dispatchTask?.(id);
|
|
243
|
-
return dispatched === false
|
|
244
|
-
? `任务 \`${id}\` 已就绪,但频道队列暂时无法接受;driver 会在下一轮重试。`
|
|
245
|
-
: `已立即唤醒任务 \`${id}\`。`;
|
|
246
|
-
}
|
|
247
222
|
async function steerTask(options, idInput, text) {
|
|
248
223
|
const id = normalizeTaskId(idInput);
|
|
249
224
|
const document = await readStoredTask(options.channelDir, id);
|
|
@@ -275,12 +250,11 @@ function issue(problem, nextStep) {
|
|
|
275
250
|
return `- ${problem}(下一步:${nextStep})`;
|
|
276
251
|
}
|
|
277
252
|
/**
|
|
278
|
-
* `/tasks doctor`, deliberately
|
|
253
|
+
* `/tasks doctor`, deliberately small (spec 051, D11; spec 052, D11).
|
|
279
254
|
*
|
|
280
|
-
* Most of what
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
* time. What is left is what a *hand edit* can still create.
|
|
255
|
+
* Most of what older doctors looked for cannot be produced any more: the frontmatter normalizer
|
|
256
|
+
* and `resolveTicket` reject those states at write time. What is left is what a *hand edit*, or a
|
|
257
|
+
* conversion that did not run, can still create.
|
|
284
258
|
*/
|
|
285
259
|
async function doctor(options) {
|
|
286
260
|
const now = Date.now();
|
|
@@ -292,20 +266,11 @@ async function doctor(options) {
|
|
|
292
266
|
continue;
|
|
293
267
|
}
|
|
294
268
|
if (entry.legacy) {
|
|
295
|
-
problems.push(issue(`${entry.id}
|
|
296
|
-
}
|
|
297
|
-
if (entry.fields.state === "parked" && !entry.fields.ticket) {
|
|
298
|
-
problems.push(issue(`${entry.id} 停泊但没有等待票`, `用 /tasks run ${entry.id} 重新开始,或让 Agent 重新停泊`));
|
|
299
|
-
}
|
|
300
|
-
if (entry.expired && !entry.fields.paused) {
|
|
301
|
-
problems.push(issue(`${entry.id} 的等待票已过兜底时限`, "driver 会在下一轮重开或通知;若一直没有,检查 daemon 是否在跑"));
|
|
302
|
-
}
|
|
303
|
-
if (entry.fields.schedule && !entry.fields.cycle) {
|
|
304
|
-
problems.push(issue(`${entry.id} 有 schedule 但没有周期记录`, `用 /tasks run ${entry.id} 开启第一个周期`));
|
|
269
|
+
problems.push(issue(`${entry.id} 仍含旧版本的字段(status / enabled / control / wake / schedule)`, "保留原件,按 docs/events-and-tasks.md 的升级步骤处理;不要仅删旧字段"));
|
|
305
270
|
}
|
|
306
|
-
const
|
|
307
|
-
if (
|
|
308
|
-
problems.push(issue(`${entry.id}
|
|
271
|
+
const by = entry.fields.ticket ? parseLocalTime(entry.fields.ticket.by) : undefined;
|
|
272
|
+
if (entry.expired && !entry.fields.paused && by !== undefined && by < now - EXPIRED_GRACE_MS) {
|
|
273
|
+
problems.push(issue(`${entry.id} 的等待票已过兜底时限超过 1 小时`, "driver 本该重开或通知;检查 daemon 是否在跑"));
|
|
309
274
|
}
|
|
310
275
|
}
|
|
311
276
|
if (problems.length === 0)
|
|
@@ -337,15 +302,13 @@ async function dispatchTasksCommand(options, command) {
|
|
|
337
302
|
case "show":
|
|
338
303
|
return await showTask(options.channelDir, command.id);
|
|
339
304
|
case "log":
|
|
340
|
-
return await showTaskLog(options.channelDir, command.id
|
|
305
|
+
return await showTaskLog(options.channelDir, command.id);
|
|
341
306
|
case "archive":
|
|
342
307
|
return await listArchive(options.channelDir);
|
|
343
308
|
case "pause":
|
|
344
309
|
return await pauseTask(options, command.id);
|
|
345
310
|
case "resume":
|
|
346
311
|
return await resumeTask(options, command.id, command.grants);
|
|
347
|
-
case "run":
|
|
348
|
-
return await runTask(options, command.id);
|
|
349
312
|
case "steer":
|
|
350
313
|
return await steerTask(options, command.id, command.text);
|
|
351
314
|
case "reply":
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ChannelEvent } from "../channel/channel-event.js";
|
|
2
|
-
import type
|
|
2
|
+
import { type DiscoverOptions } from "../channel/channel-index.js";
|
|
3
|
+
import { type PipiclawTaskDriverSettings } from "../settings.js";
|
|
3
4
|
import { type TaskLedgerEntry } from "../tasks/ledger.js";
|
|
4
5
|
export interface TaskDriverOptions {
|
|
5
6
|
workspaceDir: string;
|
|
@@ -8,7 +9,8 @@ export interface TaskDriverOptions {
|
|
|
8
9
|
dispatch: (event: ChannelEvent) => boolean | Promise<boolean>;
|
|
9
10
|
/** Optional observability hook. It runs after every production dispatch attempt. */
|
|
10
11
|
onDispatch?: (event: ChannelEvent, accepted: boolean) => void;
|
|
11
|
-
|
|
12
|
+
/** Test-only override; production uses `TASK_DRIVER_SETTINGS`. */
|
|
13
|
+
settings?: PipiclawTaskDriverSettings;
|
|
12
14
|
/** Master autonomy switch (`tools.tasks.enabled`); re-read every tick. Defaults to on. */
|
|
13
15
|
isEnabled?: () => boolean;
|
|
14
16
|
/** Test-only override for the idle-sleep cap; production uses `settings.maxSleepMinutes`. */
|
|
@@ -20,7 +22,9 @@ export interface TaskDriverOptions {
|
|
|
20
22
|
* Channels this driver should scan. Live ids come first so a channel discovered on disk under
|
|
21
23
|
* its escaped directory name never displaces the real id the runner map already knows.
|
|
22
24
|
*/
|
|
23
|
-
export declare function discoverTaskChannels(workspaceDir: string, knownChannelIds?: Iterable<string
|
|
25
|
+
export declare function discoverTaskChannels(workspaceDir: string, knownChannelIds?: Iterable<string>, options?: DiscoverOptions): Promise<string[]>;
|
|
26
|
+
/** The task a `[TASK_DRIVER:<id>]` dispatch text names, if it is one. */
|
|
27
|
+
export declare function parseDriverTaskId(text: string | undefined): string | undefined;
|
|
24
28
|
export declare function createTaskDriverEvent(channelId: string, entry: TaskLedgerEntry, nowMs: number): ChannelEvent;
|
|
25
29
|
/**
|
|
26
30
|
* The deterministic receipt a runtime stop produces (spec 051, D11). It is delivered directly,
|
|
@@ -29,12 +33,13 @@ export declare function createTaskDriverEvent(channelId: string, entry: TaskLedg
|
|
|
29
33
|
*/
|
|
30
34
|
export declare function taskStopReceipt(channelId: string, entry: TaskLedgerEntry, reason: string, nowMs: number): ChannelEvent;
|
|
31
35
|
/**
|
|
32
|
-
* Native, ticket-driven scheduler for the persistent task ledger (spec 051, D9).
|
|
36
|
+
* Native, ticket-driven scheduler for the persistent task ledger (spec 051, D9; spec 052).
|
|
33
37
|
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
+
* Three jobs, in this order per channel: redeem due `time` tickets, apply the backstop to expired
|
|
39
|
+
* ones, and queue one step for a runnable task. `work` and `ask` tickets are never polled — their
|
|
40
|
+
* owners push them (a settling delegation or job, `/tasks reply`) — so the scan stays a cheap read
|
|
41
|
+
* of a handful of frontmatters. Recurring work is not the driver's business: an event spawns a
|
|
42
|
+
* fresh task instance each time (spec 052, D2).
|
|
38
43
|
*
|
|
39
44
|
* What is deliberately *not* here any more: the ten-field ledger fingerprint, the process-local
|
|
40
45
|
* effect ledger, the futile-wake and wake-per-cycle counters, and the three-tier backoff table.
|
|
@@ -77,8 +82,8 @@ export declare class TaskDriver {
|
|
|
77
82
|
/** Stop one task and notify. Delivery failure is logged, never fatal: the durable stop already happened. */
|
|
78
83
|
private notifyStop;
|
|
79
84
|
/**
|
|
80
|
-
* Deterministic, zero-token maintenance for one channel: redeem due tickets
|
|
81
|
-
* backstops
|
|
85
|
+
* Deterministic, zero-token maintenance for one channel: redeem due tickets and apply expiry
|
|
86
|
+
* backstops. Runs whether or not the channel is busy — only model
|
|
82
87
|
* dispatch is gated on an idle channel.
|
|
83
88
|
*/
|
|
84
89
|
private maintain;
|
|
@@ -4,11 +4,12 @@ import { discoverWorkspaceChannelIds } from "../channel/channel-index.js";
|
|
|
4
4
|
import { dedupeChannelIdsByDirectory, getChannelDir, isChannelId } from "../channel/channel-paths.js";
|
|
5
5
|
import * as log from "../log.js";
|
|
6
6
|
import { PLAYBOOKS_DIR } from "../paths.js";
|
|
7
|
+
import { TASK_DRIVER_SETTINGS } from "../settings.js";
|
|
7
8
|
import { errorMessage } from "../shared/text-utils.js";
|
|
8
9
|
import { checkBudget, IDLE_STEP_LIMIT } from "../tasks/budget.js";
|
|
9
10
|
import { readActiveTasks } from "../tasks/ledger.js";
|
|
10
11
|
import { readTaskLog } from "../tasks/log.js";
|
|
11
|
-
import { expireTicket,
|
|
12
|
+
import { expireTicket, pauseTask, redeemTicket } from "../tasks/store.js";
|
|
12
13
|
/** Short debounce so a burst of nudges collapses into a single rescan. */
|
|
13
14
|
const NUDGE_DEBOUNCE_MS = 50;
|
|
14
15
|
/** Never schedule a scan closer than this, so near-now horizons cannot spin the loop. */
|
|
@@ -19,10 +20,10 @@ const REDISPATCH_GUARD_MS = 30_000;
|
|
|
19
20
|
* Channels this driver should scan. Live ids come first so a channel discovered on disk under
|
|
20
21
|
* its escaped directory name never displaces the real id the runner map already knows.
|
|
21
22
|
*/
|
|
22
|
-
export async function discoverTaskChannels(workspaceDir, knownChannelIds = []) {
|
|
23
|
+
export async function discoverTaskChannels(workspaceDir, knownChannelIds = [], options = {}) {
|
|
23
24
|
return dedupeChannelIdsByDirectory([
|
|
24
25
|
...[...knownChannelIds].filter(isChannelId),
|
|
25
|
-
...(await discoverWorkspaceChannelIds(workspaceDir)),
|
|
26
|
+
...(await discoverWorkspaceChannelIds(workspaceDir, options)),
|
|
26
27
|
]);
|
|
27
28
|
}
|
|
28
29
|
function attemptKey(channelId, taskId) {
|
|
@@ -35,23 +36,27 @@ function channelType(channelId) {
|
|
|
35
36
|
}
|
|
36
37
|
/**
|
|
37
38
|
* Stable, provenance-derived dispatch id for a task step (spec 031, D1; spec 051 keeps the shape).
|
|
38
|
-
*
|
|
39
|
-
*
|
|
39
|
+
* Each step is genuinely new work, so the dispatch moment disambiguates it; collapsing two steps
|
|
40
|
+
* onto one durable record would silently drop the later one.
|
|
40
41
|
*/
|
|
41
42
|
function stepDispatchId(channelId, entry, nowMs) {
|
|
42
|
-
return `task:${channelId}:${entry.id}:${
|
|
43
|
+
return `task:${channelId}:${entry.id}:${nowMs}`;
|
|
44
|
+
}
|
|
45
|
+
/** The task a `[TASK_DRIVER:<id>]` dispatch text names, if it is one. */
|
|
46
|
+
export function parseDriverTaskId(text) {
|
|
47
|
+
return /^\[TASK_DRIVER:([A-Za-z0-9._-]+)\]/.exec(text ?? "")?.[1];
|
|
43
48
|
}
|
|
44
49
|
export function createTaskDriverEvent(channelId, entry, nowMs) {
|
|
45
|
-
const repairOnly = !entry.readable
|
|
50
|
+
const repairOnly = !entry.readable;
|
|
46
51
|
const repair = repairOnly
|
|
47
52
|
? ` Task metadata is not readable; repair only the frontmatter in tasks/${entry.id}.md, then stop. ` +
|
|
48
|
-
`Do not execute the task goal or any external action. Read ${join(PLAYBOOKS_DIR, "task-
|
|
53
|
+
`Do not execute the task goal or any external action. Read ${join(PLAYBOOKS_DIR, "task-lead.md")} for the repair path.`
|
|
49
54
|
: "";
|
|
50
55
|
const capsule = [
|
|
51
56
|
`Task capsule: title=${entry.title}; state=${entry.fields.state};`,
|
|
52
|
-
entry.fields.
|
|
53
|
-
entry.
|
|
54
|
-
? `
|
|
57
|
+
entry.fields.usage ? `${entry.fields.usage.steps} steps so far;` : "",
|
|
58
|
+
entry.items
|
|
59
|
+
? `items=${entry.items.done}/${entry.items.total} done, current=${entry.items.current?.id ?? "none"};`
|
|
55
60
|
: "",
|
|
56
61
|
]
|
|
57
62
|
.filter(Boolean)
|
|
@@ -62,11 +67,11 @@ export function createTaskDriverEvent(channelId, entry, nowMs) {
|
|
|
62
67
|
user: "TASK_DRIVER",
|
|
63
68
|
userName: "TASK_DRIVER",
|
|
64
69
|
text: `[TASK_DRIVER:${entry.id}] Resume task ${entry.id}. ${capsule}${repair} ` +
|
|
65
|
-
`Open tasks/${entry.id}.md and read ${join(PLAYBOOKS_DIR, "task-
|
|
70
|
+
`Open tasks/${entry.id}.md and read ${join(PLAYBOOKS_DIR, "task-lead.md")} before acting. ` +
|
|
66
71
|
(repairOnly
|
|
67
72
|
? "After the metadata is repaired, leave task work for a later step. "
|
|
68
|
-
: "Advance the next concrete step under the task's current goal,
|
|
69
|
-
"End the step with task_step_end (continue / park / done
|
|
73
|
+
: "Advance the next concrete step under the task's current goal, work items and acceptance state. ") +
|
|
74
|
+
"End the step with task_step_end (continue / park / done).",
|
|
70
75
|
ts: String(nowMs),
|
|
71
76
|
dispatchId: stepDispatchId(channelId, entry, nowMs),
|
|
72
77
|
};
|
|
@@ -83,7 +88,7 @@ export function taskStopReceipt(channelId, entry, reason, nowMs) {
|
|
|
83
88
|
user: "TASK_DRIVER",
|
|
84
89
|
userName: "TASK_DRIVER",
|
|
85
90
|
text: `任务 ${entry.id}(${entry.title})已停止自动执行:${reason}\n` +
|
|
86
|
-
`当前阶段:${entry.fields.state}${entry.fields.
|
|
91
|
+
`当前阶段:${entry.fields.state}${entry.fields.usage ? `;已用 ${entry.fields.usage.steps} 步` : ""}\n` +
|
|
87
92
|
`查看:/tasks show ${entry.id}\n继续:/tasks resume ${entry.id}\n不再需要:让 Agent cancel 该任务。`,
|
|
88
93
|
ts: String(nowMs),
|
|
89
94
|
// Keyed on the cause, not the moment: re-detecting the same stop before the user has acted
|
|
@@ -92,12 +97,13 @@ export function taskStopReceipt(channelId, entry, reason, nowMs) {
|
|
|
92
97
|
};
|
|
93
98
|
}
|
|
94
99
|
/**
|
|
95
|
-
* Native, ticket-driven scheduler for the persistent task ledger (spec 051, D9).
|
|
100
|
+
* Native, ticket-driven scheduler for the persistent task ledger (spec 051, D9; spec 052).
|
|
96
101
|
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
102
|
+
* Three jobs, in this order per channel: redeem due `time` tickets, apply the backstop to expired
|
|
103
|
+
* ones, and queue one step for a runnable task. `work` and `ask` tickets are never polled — their
|
|
104
|
+
* owners push them (a settling delegation or job, `/tasks reply`) — so the scan stays a cheap read
|
|
105
|
+
* of a handful of frontmatters. Recurring work is not the driver's business: an event spawns a
|
|
106
|
+
* fresh task instance each time (spec 052, D2).
|
|
101
107
|
*
|
|
102
108
|
* What is deliberately *not* here any more: the ten-field ledger fingerprint, the process-local
|
|
103
109
|
* effect ledger, the futile-wake and wake-per-cycle counters, and the three-tier backoff table.
|
|
@@ -179,7 +185,7 @@ export class TaskDriver {
|
|
|
179
185
|
scheduleNext() {
|
|
180
186
|
if (!this.loopActive || this.timer || this.nudgeTimer)
|
|
181
187
|
return;
|
|
182
|
-
const settings = this.options.
|
|
188
|
+
const settings = this.options.settings ?? TASK_DRIVER_SETTINGS;
|
|
183
189
|
const capMs = this.options.intervalMs ?? settings.maxSleepMinutes * 60_000;
|
|
184
190
|
const untilNext = this.nextWakeMs !== undefined ? this.nextWakeMs - Date.now() : Number.POSITIVE_INFINITY;
|
|
185
191
|
const sleepMs = Math.max(MIN_SLEEP_MS, Math.min(capMs, untilNext));
|
|
@@ -206,8 +212,8 @@ export class TaskDriver {
|
|
|
206
212
|
}
|
|
207
213
|
}
|
|
208
214
|
/**
|
|
209
|
-
* Deterministic, zero-token maintenance for one channel: redeem due tickets
|
|
210
|
-
* backstops
|
|
215
|
+
* Deterministic, zero-token maintenance for one channel: redeem due tickets and apply expiry
|
|
216
|
+
* backstops. Runs whether or not the channel is busy — only model
|
|
211
217
|
* dispatch is gated on an idle channel.
|
|
212
218
|
*/
|
|
213
219
|
async maintain(channelDir, channelId, entries, now) {
|
|
@@ -219,21 +225,10 @@ export class TaskDriver {
|
|
|
219
225
|
const ticket = entry.fields.ticket;
|
|
220
226
|
if (!ticket)
|
|
221
227
|
continue;
|
|
222
|
-
// A due timer redeems into an open task
|
|
228
|
+
// A due timer redeems into an open task.
|
|
223
229
|
if (entry.dueMs !== undefined && entry.dueMs <= nowMs) {
|
|
224
|
-
if (
|
|
225
|
-
const opened = await openCycle(channelDir, entry.id, now).catch((error) => {
|
|
226
|
-
log.logWarning(`[${channelId}] Could not open next cycle for ${entry.id}`, errorMessage(error));
|
|
227
|
-
return undefined;
|
|
228
|
-
});
|
|
229
|
-
if (opened) {
|
|
230
|
-
changed = true;
|
|
231
|
-
log.logInfo(`[${channelId}] Task driver opened cycle ${opened.cycleId} for ${entry.id}`);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
else if (await redeemTicket(channelDir, entry.id, (current) => current.kind === ticket.kind)) {
|
|
230
|
+
if (await redeemTicket(channelDir, entry.id, (current) => current.kind === ticket.kind))
|
|
235
231
|
changed = true;
|
|
236
|
-
}
|
|
237
232
|
continue;
|
|
238
233
|
}
|
|
239
234
|
if (entry.expired) {
|
|
@@ -264,10 +259,10 @@ export class TaskDriver {
|
|
|
264
259
|
async detectIdleLoop(channelDir, channelId, entries, nowMs) {
|
|
265
260
|
let paused = false;
|
|
266
261
|
for (const entry of entries) {
|
|
267
|
-
const
|
|
268
|
-
if (!
|
|
262
|
+
const usage = entry.fields.usage;
|
|
263
|
+
if (!usage || usage.steps < IDLE_STEP_LIMIT)
|
|
269
264
|
continue;
|
|
270
|
-
const steps = (await readTaskLog(channelDir, entry.id, {
|
|
265
|
+
const steps = (await readTaskLog(channelDir, entry.id, { kinds: ["step"] })).slice(-IDLE_STEP_LIMIT);
|
|
271
266
|
if (steps.length < IDLE_STEP_LIMIT)
|
|
272
267
|
continue;
|
|
273
268
|
const idle = steps.every((record) => record.kind === "step" && record.tools.length === 0);
|
|
@@ -285,7 +280,7 @@ export class TaskDriver {
|
|
|
285
280
|
async runOnce(now = new Date()) {
|
|
286
281
|
if (this.options.isEnabled?.() === false || this.running)
|
|
287
282
|
return;
|
|
288
|
-
const settings = this.options.
|
|
283
|
+
const settings = this.options.settings ?? TASK_DRIVER_SETTINGS;
|
|
289
284
|
const nowMs = now.getTime();
|
|
290
285
|
this.running = true;
|
|
291
286
|
this.nextWakeMs = undefined;
|
|
@@ -321,7 +316,7 @@ export class TaskDriver {
|
|
|
321
316
|
for (const entry of entries) {
|
|
322
317
|
if (!entry.runnable)
|
|
323
318
|
continue;
|
|
324
|
-
const status = checkBudget(entry.fields
|
|
319
|
+
const status = checkBudget(entry.fields);
|
|
325
320
|
if (!status.breach || !status.reason) {
|
|
326
321
|
budgeted.push(entry);
|
|
327
322
|
continue;
|
|
@@ -1,36 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
interface LegacyControl {
|
|
3
|
-
deadline?: string;
|
|
4
|
-
nextAction?: string;
|
|
5
|
-
cycleId?: string;
|
|
6
|
-
verification?: {
|
|
7
|
-
required?: boolean;
|
|
8
|
-
};
|
|
9
|
-
stop?: {
|
|
10
|
-
by?: string;
|
|
11
|
-
reason?: string;
|
|
12
|
-
at?: string;
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
interface LegacyFields {
|
|
1
|
+
interface V3Fields {
|
|
16
2
|
status?: string;
|
|
17
|
-
enabled
|
|
18
|
-
wake?: string;
|
|
3
|
+
enabled?: boolean;
|
|
19
4
|
schedule?: string;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
5
|
+
stopReason?: string;
|
|
6
|
+
/** `control.stop` is present (with or without a usable reason). */
|
|
7
|
+
stopped: boolean;
|
|
8
|
+
/** A state key is present: skip this file, whether v4 or v5. */
|
|
9
|
+
v5: boolean;
|
|
23
10
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
11
|
+
/** Minimal reader for the v3 frontmatter block; the only place that still understands it. */
|
|
12
|
+
export declare function parseV3Frontmatter(content: string): V3Fields | undefined;
|
|
13
|
+
/** Rename `## Plan` to `## Work Items` and drop the per-cycle sections, the two body changes v5 makes. */
|
|
14
|
+
export declare function convertBody(body: string): string;
|
|
15
|
+
interface TemplateOutcome {
|
|
16
|
+
template?: Record<string, unknown>;
|
|
17
|
+
reason?: string;
|
|
28
18
|
}
|
|
29
19
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
20
|
+
* Build the event template a recurring v3 task becomes. The original Manual and Verification text
|
|
21
|
+
* is carried into the Goal (v5 has no such sections, and a recurring job's accumulated rules are
|
|
22
|
+
* exactly what the template exists to keep), and every checkbox is reset.
|
|
23
|
+
*/
|
|
24
|
+
export declare function buildTemplate(id: string, body: string): TemplateOutcome;
|
|
25
|
+
/**
|
|
26
|
+
* Convert every v3 task file in every known channel. Any failure leaves no marker, writes
|
|
27
|
+
* `state/task-migration-v5.failed.json`, and throws so the caller does not start services.
|
|
32
28
|
*/
|
|
33
|
-
export declare function
|
|
34
|
-
/** Migrate every task file in every known channel. Failures are logged and skipped. */
|
|
35
|
-
export declare function migrateTasksToV4(workspaceDir: string, stateDir: string): Promise<void>;
|
|
29
|
+
export declare function migrateTasksToV5(workspaceDir: string, stateDir: string): Promise<void>;
|
|
36
30
|
export {};
|