@oyasmi/pipiclaw 0.9.0 → 0.9.1-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/channel-runner.d.ts +5 -1
- package/dist/agent/channel-runner.js +36 -2
- package/dist/agent/commands.js +3 -1
- package/dist/agent/runner-factory.d.ts +0 -2
- package/dist/agent/types.d.ts +6 -0
- package/dist/memory/lifecycle.d.ts +15 -0
- package/dist/memory/lifecycle.js +60 -0
- package/dist/memory/task-digest.js +0 -2
- package/dist/playbooks/agent-delegation.md +3 -2
- package/dist/playbooks/task-driving.md +11 -13
- package/dist/playbooks/task-planning.md +3 -5
- package/dist/runtime/active-session-store.d.ts +12 -0
- package/dist/runtime/active-session-store.js +26 -0
- package/dist/runtime/bootstrap.d.ts +1 -1
- package/dist/runtime/bootstrap.js +87 -49
- package/dist/runtime/channel-event.d.ts +0 -6
- package/dist/runtime/delivery.js +1 -1
- package/dist/runtime/dingtalk.d.ts +7 -0
- package/dist/runtime/dingtalk.js +22 -3
- package/dist/runtime/task-commands.d.ts +1 -1
- package/dist/runtime/task-commands.js +33 -174
- package/dist/runtime/task-driver.d.ts +1 -3
- package/dist/runtime/task-driver.js +34 -40
- package/dist/runtime/task-migration.d.ts +7 -6
- package/dist/runtime/task-migration.js +85 -19
- package/dist/runtime/task-wake.d.ts +3 -5
- package/dist/runtime/task-wake.js +7 -15
- package/dist/subagents/external/run.js +18 -1
- package/dist/subagents/runs.js +22 -3
- package/dist/subagents/tool.d.ts +10 -0
- package/dist/subagents/tool.js +32 -7
- package/dist/subagents/verification-outcome.js +17 -10
- package/dist/tasks/control.d.ts +18 -67
- package/dist/tasks/control.js +44 -176
- package/dist/tasks/ledger.d.ts +7 -13
- package/dist/tasks/ledger.js +13 -49
- package/dist/tasks/store.d.ts +15 -48
- package/dist/tasks/store.js +22 -154
- package/dist/tasks/transitions.d.ts +3 -4
- package/dist/tasks/transitions.js +8 -27
- package/dist/tools/index.d.ts +0 -2
- package/dist/tools/index.js +2 -1
- package/dist/tools/registry.d.ts +0 -1
- package/dist/tools/registry.js +0 -1
- package/dist/tools/subagent-manage.d.ts +8 -1
- package/dist/tools/subagent-manage.js +77 -7
- package/dist/tools/task-manage/lifecycle.js +19 -17
- package/dist/tools/task-manage/schema.d.ts +2 -6
- package/dist/tools/task-manage/schema.js +4 -17
- package/dist/tools/task-manage/shared.js +1 -6
- package/dist/tools/task-manage/types.d.ts +0 -3
- package/dist/tools/task-manage/verification.d.ts +9 -6
- package/dist/tools/task-manage/verification.js +19 -71
- package/dist/tools/task-manage.d.ts +1 -1
- package/dist/tools/task-manage.js +2 -4
- package/dist/usage/ledger.d.ts +4 -0
- package/dist/usage/ledger.js +3 -0
- package/package.json +1 -1
|
@@ -4,12 +4,11 @@ import { join, resolve, sep } from "node:path";
|
|
|
4
4
|
import { channelJobTaskIds } from "../agent/job-manager.js";
|
|
5
5
|
import { formatLocalTime, parseLocalTime, parseWakeInput } from "../shared/local-time.js";
|
|
6
6
|
import { errorMessage } from "../shared/text-utils.js";
|
|
7
|
-
import { isRecord } from "../shared/type-guards.js";
|
|
8
7
|
import { channelDelegationTaskIds } from "../subagents/runs.js";
|
|
9
|
-
import { applyTaskControlPatch, createDefaultTaskControl,
|
|
10
|
-
import { countTaskDodItems, extractTaskTitle,
|
|
8
|
+
import { applyTaskControlPatch, createDefaultTaskControl, taskBudgetViolation } from "../tasks/control.js";
|
|
9
|
+
import { countTaskDodItems, extractTaskTitle, missingStandardTaskSections, normalizeTaskId, readActiveTasks, recurringTaskMissedOccurrence, } from "../tasks/ledger.js";
|
|
11
10
|
import { withTaskMutation } from "../tasks/mutation-lock.js";
|
|
12
|
-
import {
|
|
11
|
+
import { openRecurringTaskCycle, readStoredTask, taskBodyHash, writeStoredTask } from "../tasks/store.js";
|
|
13
12
|
import { parseTaskEventName, taskEventPrefix } from "../tasks/task-events.js";
|
|
14
13
|
import { nextTaskWake, validateTaskSchedule } from "../tasks/task-schedule.js";
|
|
15
14
|
import { normalizeStoredStatus, resolveTaskTransition } from "../tasks/transitions.js";
|
|
@@ -23,7 +22,7 @@ import { parseScheduledEventContent } from "./events.js";
|
|
|
23
22
|
* transitions and verification — stays with `task_manage`, whose state
|
|
24
23
|
* machine those fields belong to. The value is the rest of the line, so it may contain spaces.
|
|
25
24
|
*/
|
|
26
|
-
const SETTABLE_TASK_FIELDS = ["wake", "next", "
|
|
25
|
+
const SETTABLE_TASK_FIELDS = ["wake", "next", "deadline"];
|
|
27
26
|
function usage() {
|
|
28
27
|
return `# 任务
|
|
29
28
|
|
|
@@ -36,7 +35,6 @@ function usage() {
|
|
|
36
35
|
- \`/tasks resume <id>\` — 重新启用该任务,按当前阶段继续
|
|
37
36
|
- \`/tasks run <id>\` — 恢复并立即排入一次执行(需要运行时可用)
|
|
38
37
|
- \`/tasks set <id> <${SETTABLE_TASK_FIELDS.join("|")}> <值>\` — 直接改一个字段,不花一个 LLM 回合
|
|
39
|
-
- \`/tasks stats [id]\` — 查看任务级的尝试次数、token、花费与验收结果
|
|
40
38
|
- \`/tasks doctor\` — 只读检查任务/事件一致性`;
|
|
41
39
|
}
|
|
42
40
|
function parseTasksCommand(args) {
|
|
@@ -90,12 +88,6 @@ function parseTasksCommand(args) {
|
|
|
90
88
|
value: match[3].trim(),
|
|
91
89
|
};
|
|
92
90
|
}
|
|
93
|
-
if (action === "stats") {
|
|
94
|
-
const id = parts[1];
|
|
95
|
-
if (parts.length > 2)
|
|
96
|
-
throw new Error("用法:/tasks stats [id]");
|
|
97
|
-
return { action: "stats", id };
|
|
98
|
-
}
|
|
99
91
|
throw new Error(`未知的 /tasks 动作:${action}`);
|
|
100
92
|
}
|
|
101
93
|
function tasksDir(channelDir) {
|
|
@@ -167,26 +159,6 @@ function validWakeMs(entry) {
|
|
|
167
159
|
function issue(problem, nextStep) {
|
|
168
160
|
return `- ${problem}\n Next step: ${nextStep}`;
|
|
169
161
|
}
|
|
170
|
-
/**
|
|
171
|
-
* Render the ordering edges a stored task still declares, e.g. `child → parent, a → b`.
|
|
172
|
-
* Empty when the task carries no retired relation keys (spec 036, D8).
|
|
173
|
-
*/
|
|
174
|
-
function describeDroppedTaskRelations(id, rawControl) {
|
|
175
|
-
if (!isRecord(rawControl))
|
|
176
|
-
return "";
|
|
177
|
-
const edges = [];
|
|
178
|
-
const parent = rawControl.parent;
|
|
179
|
-
if (typeof parent === "string" && parent.trim())
|
|
180
|
-
edges.push(`${id} → ${parent.trim()}`);
|
|
181
|
-
const dependsOn = rawControl.dependsOn;
|
|
182
|
-
if (Array.isArray(dependsOn)) {
|
|
183
|
-
for (const dependency of dependsOn) {
|
|
184
|
-
if (typeof dependency === "string" && dependency.trim())
|
|
185
|
-
edges.push(`${id} → ${dependency.trim()}`);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
return edges.join(", ");
|
|
189
|
-
}
|
|
190
162
|
async function readActiveTaskContent(channelDir, id) {
|
|
191
163
|
try {
|
|
192
164
|
return await readFile(join(tasksDir(channelDir), `${id}.md`), "utf-8");
|
|
@@ -207,8 +179,6 @@ async function listTasks(channelDir) {
|
|
|
207
179
|
const detail = [` status: ${status}`, `next wake: ${relativeWake(entry.wakeMs, now)}`];
|
|
208
180
|
const control = entry.frontmatter.control;
|
|
209
181
|
if (control) {
|
|
210
|
-
detail.push(`priority: ${control.priority}`);
|
|
211
|
-
detail.push(`attempts: ${control.usage.attempts}/${control.budget.maxAttempts}`);
|
|
212
182
|
if (control.verification.required)
|
|
213
183
|
detail.push(`verify: required/${control.verification.status}`);
|
|
214
184
|
if (control.waitingFor)
|
|
@@ -225,8 +195,6 @@ async function listTasks(channelDir) {
|
|
|
225
195
|
}
|
|
226
196
|
if (entry.frontmatter.enabled === false)
|
|
227
197
|
detail.push("enabled: false");
|
|
228
|
-
if (entry.frontmatter.recurrence)
|
|
229
|
-
detail.push(`recurrence: ${entry.frontmatter.recurrence}`);
|
|
230
198
|
if (entry.frontmatter.schedule) {
|
|
231
199
|
detail.push(`schedule timezone: ${Intl.DateTimeFormat().resolvedOptions().timeZone} (host)`);
|
|
232
200
|
}
|
|
@@ -242,7 +210,7 @@ async function pauseTaskLocked(options, id) {
|
|
|
242
210
|
const task = await readStoredTask(options.channelDir, id);
|
|
243
211
|
if (!task)
|
|
244
212
|
return `找不到任务:${id}`;
|
|
245
|
-
const from = normalizeStoredStatus(task.fields.status
|
|
213
|
+
const from = normalizeStoredStatus(task.fields.status);
|
|
246
214
|
try {
|
|
247
215
|
resolveTaskTransition("pause", id, from);
|
|
248
216
|
}
|
|
@@ -263,7 +231,7 @@ export async function resumeTask(options, idInput) {
|
|
|
263
231
|
const task = await readStoredTask(options.channelDir, id);
|
|
264
232
|
if (!task)
|
|
265
233
|
return `找不到任务:${id}`;
|
|
266
|
-
const from = normalizeStoredStatus(task.fields.status
|
|
234
|
+
const from = normalizeStoredStatus(task.fields.status);
|
|
267
235
|
if (task.fields.enabled !== false)
|
|
268
236
|
return `任务 ${id} 已启用。`;
|
|
269
237
|
resolveTaskTransition("resume", id, from);
|
|
@@ -303,13 +271,7 @@ async function setTaskField(options, idInput, field, value) {
|
|
|
303
271
|
else {
|
|
304
272
|
const control = task.fields.control ?? createDefaultTaskControl();
|
|
305
273
|
try {
|
|
306
|
-
task.fields.control = applyTaskControlPatch(control, field === "next"
|
|
307
|
-
? { nextAction: value }
|
|
308
|
-
: field === "deadline"
|
|
309
|
-
? { deadline: value }
|
|
310
|
-
: field === "priority"
|
|
311
|
-
? { priority: parseTaskPriority(value) }
|
|
312
|
-
: { maxAttempts: parseAttempts(value) });
|
|
274
|
+
task.fields.control = applyTaskControlPatch(control, field === "next" ? { nextAction: value } : { deadline: value });
|
|
313
275
|
}
|
|
314
276
|
catch (error) {
|
|
315
277
|
return errorMessage(error);
|
|
@@ -318,19 +280,6 @@ async function setTaskField(options, idInput, field, value) {
|
|
|
318
280
|
await writeStoredTask(task);
|
|
319
281
|
return `已更新任务 ${id}:${field} = ${value || "(已清除)"}`;
|
|
320
282
|
}
|
|
321
|
-
function parseTaskPriority(value) {
|
|
322
|
-
if (!TASK_PRIORITIES.includes(value)) {
|
|
323
|
-
throw new Error(`priority 必须是 ${TASK_PRIORITIES.join(" / ")} 之一。`);
|
|
324
|
-
}
|
|
325
|
-
return value;
|
|
326
|
-
}
|
|
327
|
-
function parseAttempts(value) {
|
|
328
|
-
const parsed = Number(value);
|
|
329
|
-
if (!Number.isInteger(parsed) || parsed < 1) {
|
|
330
|
-
throw new Error("attempts 必须是不小于 1 的整数。");
|
|
331
|
-
}
|
|
332
|
-
return parsed;
|
|
333
|
-
}
|
|
334
283
|
async function runTask(options, idInput) {
|
|
335
284
|
const id = normalizeTaskId(idInput);
|
|
336
285
|
const task = await readStoredTask(options.channelDir, id);
|
|
@@ -359,77 +308,14 @@ async function runTask(options, idInput) {
|
|
|
359
308
|
if (task.fields.control) {
|
|
360
309
|
task.fields.control.stop = undefined;
|
|
361
310
|
task.fields.control.waitingFor = undefined;
|
|
362
|
-
task.fields.control.blockedReason = undefined;
|
|
363
311
|
}
|
|
364
312
|
await writeStoredTask(task);
|
|
365
313
|
}
|
|
366
|
-
const
|
|
367
|
-
const enqueued = await options.dispatchTask?.(id, claim?.generation);
|
|
368
|
-
if (!enqueued && claim)
|
|
369
|
-
await releaseTaskAttemptClaim(options.channelDir, id, claim);
|
|
314
|
+
const enqueued = await options.dispatchTask?.(id);
|
|
370
315
|
return enqueued
|
|
371
316
|
? `已把任务 ${id} 排入一次立即执行。`
|
|
372
317
|
: `任务 ${id} 已就绪。启动钉钉守护进程可自动派发,或在本会话里直接发一条普通消息推进它。`;
|
|
373
318
|
}
|
|
374
|
-
function renderUsageLine(entry) {
|
|
375
|
-
const control = entry.frontmatter.control;
|
|
376
|
-
if (!control)
|
|
377
|
-
return `- ${entry.id}:旧格式任务(没有受治理的用量记录)`;
|
|
378
|
-
const verification = control.verification;
|
|
379
|
-
const cycleCost = control.usage.costKnown ? `$${control.usage.costUsd.toFixed(4)}` : "unavailable";
|
|
380
|
-
return [
|
|
381
|
-
`- ${entry.id} — ${entry.title}`,
|
|
382
|
-
` this cycle: ${control.usage.attempts}/${control.budget.maxAttempts} attempts, ${control.usage.tokens} tokens, ${cycleCost}, ${control.usage.wallTimeMinutes.toFixed(1)}m`,
|
|
383
|
-
` last run: ${control.lastOutcome}`,
|
|
384
|
-
` verification: ${verification.required ? "required" : "not required"}/${verification.status}`,
|
|
385
|
-
].join("\n");
|
|
386
|
-
}
|
|
387
|
-
async function taskStats(options, idInput) {
|
|
388
|
-
if (idInput) {
|
|
389
|
-
const id = normalizeTaskId(idInput);
|
|
390
|
-
const task = await readStoredTask(options.channelDir, id, true, true);
|
|
391
|
-
if (!task)
|
|
392
|
-
return `找不到任务:${id}`;
|
|
393
|
-
const entry = {
|
|
394
|
-
id,
|
|
395
|
-
title: extractTaskTitle(task.body, id),
|
|
396
|
-
frontmatter: {
|
|
397
|
-
readable: true,
|
|
398
|
-
status: task.fields.status,
|
|
399
|
-
enabled: task.fields.enabled !== false,
|
|
400
|
-
wake: task.fields.wake,
|
|
401
|
-
schedule: task.fields.schedule,
|
|
402
|
-
recurrence: task.fields.recurrence,
|
|
403
|
-
control: task.fields.control,
|
|
404
|
-
},
|
|
405
|
-
actionable: false,
|
|
406
|
-
};
|
|
407
|
-
return `# 任务用量\n\n${renderUsageLine(entry)}`;
|
|
408
|
-
}
|
|
409
|
-
const entries = (await readActiveTasks(tasksDir(options.channelDir))).filter((entry) => !entry.frontmatter.archiveOutcome);
|
|
410
|
-
const governed = entries.filter((entry) => entry.frontmatter.control);
|
|
411
|
-
const totals = governed.reduce((total, entry) => {
|
|
412
|
-
const usage = entry.frontmatter.control.usage;
|
|
413
|
-
total.attempts += usage.attempts;
|
|
414
|
-
total.tokens += usage.tokens;
|
|
415
|
-
total.costUsd += usage.costUsd;
|
|
416
|
-
total.costKnown &&= usage.costKnown;
|
|
417
|
-
total.wallTimeMinutes += usage.wallTimeMinutes;
|
|
418
|
-
return total;
|
|
419
|
-
}, { attempts: 0, tokens: 0, costUsd: 0, costKnown: true, wallTimeMinutes: 0 });
|
|
420
|
-
const verified = governed.filter((entry) => entry.frontmatter.control?.verification.status === "passed").length;
|
|
421
|
-
const stalled = governed.filter((entry) => entry.frontmatter.control?.lastOutcome === "failed").length;
|
|
422
|
-
return [
|
|
423
|
-
"# 任务用量",
|
|
424
|
-
"",
|
|
425
|
-
`governed tasks: ${governed.length}/${entries.length}`,
|
|
426
|
-
`this cycle: ${totals.attempts} attempts, ${totals.tokens} tokens, ${totals.costKnown ? `$${totals.costUsd.toFixed(4)}` : "cost unavailable"}, ${totals.wallTimeMinutes.toFixed(1)}m`,
|
|
427
|
-
`verification PASS: ${verified}`,
|
|
428
|
-
`last-run failures: ${stalled}`,
|
|
429
|
-
"",
|
|
430
|
-
...governed.map(renderUsageLine),
|
|
431
|
-
].join("\n");
|
|
432
|
-
}
|
|
433
319
|
async function showTask(channelDir, id) {
|
|
434
320
|
const dir = tasksDir(channelDir);
|
|
435
321
|
const taskId = normalizeTaskId(id);
|
|
@@ -480,7 +366,7 @@ async function doctor(options) {
|
|
|
480
366
|
for (const entry of entries) {
|
|
481
367
|
const status = entry.frontmatter.status ?? "active";
|
|
482
368
|
if (!entry.frontmatter.readable) {
|
|
483
|
-
issues.push(issue(`tasks/${entry.id}.md has unreadable frontmatter; wake/status cannot be trusted.`, `Fix tasks/${entry.id}.md so it starts with readable status/wake/
|
|
369
|
+
issues.push(issue(`tasks/${entry.id}.md has unreadable frontmatter; wake/status cannot be trusted.`, `Fix tasks/${entry.id}.md so it starts with readable status/wake/schedule frontmatter.`));
|
|
484
370
|
continue;
|
|
485
371
|
}
|
|
486
372
|
if (entry.frontmatter.controlReadable === false) {
|
|
@@ -489,7 +375,7 @@ async function doctor(options) {
|
|
|
489
375
|
}
|
|
490
376
|
const control = entry.frontmatter.control;
|
|
491
377
|
if (!control && entry.frontmatter.enabled === false) {
|
|
492
|
-
issues.push(issue(`tasks/${entry.id}.md is disabled but has no
|
|
378
|
+
issues.push(issue(`tasks/${entry.id}.md is disabled but has no control.stop receipt.`, `Use task_manage set to repair control metadata, then /tasks resume ${entry.id} when it is safe to continue.`));
|
|
493
379
|
}
|
|
494
380
|
if (control) {
|
|
495
381
|
const storedTask = await readStoredTask(options.channelDir, entry.id);
|
|
@@ -497,19 +383,16 @@ async function doctor(options) {
|
|
|
497
383
|
if (violation) {
|
|
498
384
|
issues.push(issue(`tasks/${entry.id}.md exceeds its control limit: ${violation}.`, `Review the task, then explicitly raise its budget/deadline or cancel it; the driver will escalate it.`));
|
|
499
385
|
}
|
|
500
|
-
if (status !== "sleeping" && control.verification.status === "passed"
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
if (!attestationOk) {
|
|
511
|
-
issues.push(issue(`tasks/${entry.id}.md records an independent PASS with no matching verifier attestation on disk.`, `Run a fresh purpose=verify sub-agent and import its attestation with task_manage verify before completion.`));
|
|
512
|
-
}
|
|
386
|
+
if (status !== "sleeping" && control.verification.status === "passed") {
|
|
387
|
+
const attestationOk = control.verification.runId
|
|
388
|
+
? await readVerificationAttestation(options.channelDir, control.verification.runId)
|
|
389
|
+
.then((attestation) => attestation.taskId === entry.id &&
|
|
390
|
+
attestation.verdict === "pass" &&
|
|
391
|
+
(!storedTask || attestation.bodyHash === taskBodyHash(storedTask.body)))
|
|
392
|
+
.catch(() => false)
|
|
393
|
+
: false;
|
|
394
|
+
if (!attestationOk) {
|
|
395
|
+
issues.push(issue(`tasks/${entry.id}.md records an independent PASS with no matching, fresh verifier attestation on disk.`, `Run a fresh purpose=verify sub-agent and import its attestation with task_manage verify before completion.`));
|
|
513
396
|
}
|
|
514
397
|
}
|
|
515
398
|
if (entry.frontmatter.enabled === false && !control.stop) {
|
|
@@ -540,18 +423,6 @@ async function doctor(options) {
|
|
|
540
423
|
validWakeMs(entry) > now) {
|
|
541
424
|
issues.push(issue(`tasks/${entry.id}.md is active but its wake is in the future.`, `Use task_manage set/progress to make it waiting with waitingFor=time, or clear wake to continue now.`));
|
|
542
425
|
}
|
|
543
|
-
if (status === "waiting" && entry.frontmatter.wake && !control?.waitingFor) {
|
|
544
|
-
issues.push(issue(`tasks/${entry.id}.md is waiting on a timed wake but has no waitingFor marker.`, `Run task_manage set with control.waitingFor=time or progress the task to normalize the wait.`));
|
|
545
|
-
}
|
|
546
|
-
if (status === "waiting" &&
|
|
547
|
-
entry.frontmatter.wake &&
|
|
548
|
-
control?.waitingFor &&
|
|
549
|
-
!["time", "external-signal"].includes(control.waitingFor)) {
|
|
550
|
-
issues.push(issue(`tasks/${entry.id}.md is waiting on a wake but waitingFor=${control.waitingFor} is not a timed source.`, `Set control.waitingFor to time, or clear wake and record the real signal source before resuming ${entry.id}.`));
|
|
551
|
-
}
|
|
552
|
-
if (status === "waiting" && !entry.frontmatter.wake && control?.waitingFor === "time") {
|
|
553
|
-
issues.push(issue(`tasks/${entry.id}.md is parked but waitingFor=time has no wake.`, `Set a valid wake or change waitingFor to the actual recovery source.`));
|
|
554
|
-
}
|
|
555
426
|
if (recurringTaskMissedOccurrence({ status, schedule: entry.frontmatter.schedule, control }, new Date(now))) {
|
|
556
427
|
issues.push(issue(`tasks/${entry.id}.md has an open ${status} cycle past its next recurring occurrence; no concurrent cycle was opened.`, `Finish, skip, or cancel the current cycle for ${entry.id}; then let the driver compute the next occurrence.`));
|
|
557
428
|
}
|
|
@@ -579,16 +450,18 @@ async function doctor(options) {
|
|
|
579
450
|
}
|
|
580
451
|
}
|
|
581
452
|
}
|
|
582
|
-
// A
|
|
583
|
-
//
|
|
584
|
-
//
|
|
585
|
-
//
|
|
586
|
-
//
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
453
|
+
// A waiting task resumes only via a real durable source: a valid wake (the driver's timed
|
|
454
|
+
// reactivation) or an actually-running background job/delegation recorded against this
|
|
455
|
+
// id — never by what `waitingFor` merely claims, which is model-written display text and
|
|
456
|
+
// no longer gates anything. With neither, resumption is entirely up to the user —
|
|
457
|
+
// legitimate when waiting on an answer, and indistinguishable, on disk, from a task
|
|
458
|
+
// everyone has forgotten. Report the fact and name every way out.
|
|
459
|
+
if (status === "waiting") {
|
|
460
|
+
const hasDurableWake = Boolean(entry.frontmatter.wake) && validWakeMs(entry) !== undefined;
|
|
461
|
+
const hasRunningSource = runningJobTaskIds.has(entry.id) || runningDelegationTaskIds.has(entry.id);
|
|
462
|
+
if (!hasDurableWake && !hasRunningSource) {
|
|
463
|
+
issues.push(issue(`tasks/${entry.id}.md is waiting with no durable way to resume (no valid wake, no running job/delegation recorded against it).`, `It resumes only when you reply to it, run /tasks run ${entry.id}, or give it a wake with /tasks set ${entry.id} wake <local time or +2h>; cancel it if what it waits for is gone.`));
|
|
464
|
+
}
|
|
592
465
|
}
|
|
593
466
|
if (entry.frontmatter.wake && validWakeMs(entry) === undefined) {
|
|
594
467
|
issues.push(issue(`tasks/${entry.id}.md has an invalid wake value (${entry.frontmatter.wake}); the native driver will treat it as due.`, `Use task_manage set or progress to replace wake with a valid local time, or clear it if the task should continue now.`));
|
|
@@ -608,18 +481,6 @@ async function doctor(options) {
|
|
|
608
481
|
}
|
|
609
482
|
}
|
|
610
483
|
}
|
|
611
|
-
// Spec 036 D8: retired control keys are ignored on read, which keeps stored tasks readable
|
|
612
|
-
// but silently discards whatever `parent`/`dependsOn` once expressed. Report the loss —
|
|
613
|
-
// naming the dropped edges, not just the key — so the user can restate any real ordering.
|
|
614
|
-
for (const entry of entries) {
|
|
615
|
-
const retired = retiredTaskControlKeys(entry.frontmatter.rawControl);
|
|
616
|
-
if (retired.length === 0)
|
|
617
|
-
continue;
|
|
618
|
-
const edges = describeDroppedTaskRelations(entry.id, entry.frontmatter.rawControl);
|
|
619
|
-
issues.push(issue(`tasks/${entry.id}.md still carries retired control keys: ${retired.join(", ")}.${edges ? ` Dropped ordering: ${edges}.` : ""}`, edges
|
|
620
|
-
? `These no longer constrain execution. Restate any real ordering in the task body or with wake, then remove the keys (any task_manage write drops them).`
|
|
621
|
-
: `They are ignored; any task_manage write drops them.`));
|
|
622
|
-
}
|
|
623
484
|
for (const event of events) {
|
|
624
485
|
if (!event.id || !event.use) {
|
|
625
486
|
issues.push(issue(`events/${event.filename} does not follow task.<channelId>.<taskId>.<use>.json.`, "Rename the event to the task-owned naming convention or manage it as a normal event."));
|
|
@@ -652,7 +513,7 @@ export async function handleTasksCommand(options) {
|
|
|
652
513
|
return `${message}\n\n${usage()}`;
|
|
653
514
|
}
|
|
654
515
|
try {
|
|
655
|
-
if ("id" in command && command.id &&
|
|
516
|
+
if ("id" in command && command.id && command.action !== "show") {
|
|
656
517
|
return await withTaskMutation(options.channelDir, command.id, () => dispatchTasksCommand(options, command));
|
|
657
518
|
}
|
|
658
519
|
return await dispatchTasksCommand(options, command);
|
|
@@ -678,8 +539,6 @@ async function dispatchTasksCommand(options, command) {
|
|
|
678
539
|
return await runTask(options, command.id);
|
|
679
540
|
case "set":
|
|
680
541
|
return await setTaskField(options, command.id, command.field, command.value);
|
|
681
|
-
case "stats":
|
|
682
|
-
return await taskStats(options, command.id);
|
|
683
542
|
case "doctor":
|
|
684
543
|
return await doctor(options);
|
|
685
544
|
}
|
|
@@ -22,9 +22,7 @@ export interface TaskDriverOptions {
|
|
|
22
22
|
notify?: (event: ChannelEvent) => boolean | Promise<boolean>;
|
|
23
23
|
}
|
|
24
24
|
export declare function discoverTaskChannels(workspaceDir: string, knownChannelIds?: Iterable<string>): Promise<string[]>;
|
|
25
|
-
export declare function createTaskDriverEvent(channelId: string, entry: TaskLedgerEntry, nowMs: number
|
|
26
|
-
/** Durable checker wake created by request-verification; it is a normal main-agent turn. */
|
|
27
|
-
export declare function createTaskVerificationEvent(channelId: string, entry: TaskLedgerEntry, nowMs: number): ChannelEvent;
|
|
25
|
+
export declare function createTaskDriverEvent(channelId: string, entry: TaskLedgerEntry, nowMs: number): ChannelEvent;
|
|
28
26
|
export declare function taskGovernorReceipt(channelId: string, entry: TaskLedgerEntry, reason: string, nowMs: number): ChannelEvent;
|
|
29
27
|
/**
|
|
30
28
|
* Native, token-gated driver for the persistent task ledger.
|
|
@@ -6,7 +6,7 @@ import { parseLocalTime } from "../shared/local-time.js";
|
|
|
6
6
|
import { errorMessage } from "../shared/text-utils.js";
|
|
7
7
|
import { taskBudgetViolation } from "../tasks/control.js";
|
|
8
8
|
import { normalizeTaskFields, readActiveTasks, recurringTaskMissedOccurrence, } from "../tasks/ledger.js";
|
|
9
|
-
import { activateWaitingTask,
|
|
9
|
+
import { activateWaitingTask, escalateTask, openRecurringTaskCycle, updateStoredTask } from "../tasks/store.js";
|
|
10
10
|
import { nextTaskWake } from "../tasks/task-schedule.js";
|
|
11
11
|
import { discoverWorkspaceChannelIds } from "./channel-index.js";
|
|
12
12
|
import { isChannelId } from "./channel-paths.js";
|
|
@@ -16,6 +16,10 @@ const NUDGE_DEBOUNCE_MS = 50;
|
|
|
16
16
|
const MIN_SLEEP_MS = 250;
|
|
17
17
|
/** Consecutive no-progress wakes before the governor pauses a task (spec 029, D5). */
|
|
18
18
|
const FUTILE_WAKE_LIMIT = 3;
|
|
19
|
+
/** Wakes-per-cycle ceiling: a deliberately generous circuit breaker, not a normal governance
|
|
20
|
+
* path (deadline and futileCount are). Guards only against a task that keeps "progressing"
|
|
21
|
+
* without ever converging or hitting its deadline. */
|
|
22
|
+
const MAX_WAKES_PER_CYCLE = 500;
|
|
19
23
|
export async function discoverTaskChannels(workspaceDir, knownChannelIds = []) {
|
|
20
24
|
const channels = new Set();
|
|
21
25
|
for (const channelId of knownChannelIds) {
|
|
@@ -33,9 +37,8 @@ function attemptKey(channelId, taskId) {
|
|
|
33
37
|
/**
|
|
34
38
|
* What "this task moved" means to the driver.
|
|
35
39
|
*
|
|
36
|
-
* Do not use mtime/size here
|
|
37
|
-
*
|
|
38
|
-
* continuation interval forever.
|
|
40
|
+
* Do not use mtime/size here: routine frontmatter rewrites (status/note churn) must not read as
|
|
41
|
+
* progress, or governed tasks would retry at the short continuation interval forever.
|
|
39
42
|
*
|
|
40
43
|
* `latestNote` is deliberately absent (spec 031, D7): it is the model's own account of its work,
|
|
41
44
|
* so including it let a wake that did nothing but append a note reset the futile counter forever,
|
|
@@ -48,9 +51,9 @@ function attemptKey(channelId, taskId) {
|
|
|
48
51
|
* checkbox would reset the futile counter and buy the short retry tier — a governor bypass this
|
|
49
52
|
* driver must not offer. The Plan is a capsule/agenda display concern only.
|
|
50
53
|
*
|
|
51
|
-
* nextAction
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
+
* nextAction is also a model-written explanation, not evidence of an effect. Including it would
|
|
55
|
+
* let a progress/set call reset the futile counter without changing the task's externally
|
|
56
|
+
* observable work.
|
|
54
57
|
*/
|
|
55
58
|
function taskFingerprint(entry, effects) {
|
|
56
59
|
const control = entry.frontmatter.control;
|
|
@@ -59,7 +62,6 @@ function taskFingerprint(entry, effects) {
|
|
|
59
62
|
entry.frontmatter.status ?? "",
|
|
60
63
|
entry.frontmatter.wake ?? "",
|
|
61
64
|
entry.frontmatter.schedule ?? "",
|
|
62
|
-
entry.frontmatter.recurrence ?? "",
|
|
63
65
|
control?.verification.status ?? "",
|
|
64
66
|
control?.waitingFor ?? "",
|
|
65
67
|
entry.frontmatter.enabled === false ? "disabled" : "enabled",
|
|
@@ -81,9 +83,9 @@ function taskFingerprint(entry, effects) {
|
|
|
81
83
|
* - the ledger changed with no effect (status/note churn) ⇒ the ordinary continuation delay;
|
|
82
84
|
* - nothing changed ⇒ the long stalled-retry backoff, and the futile counter keeps running.
|
|
83
85
|
*
|
|
84
|
-
* Nothing here relaxes the stop-losses: the fast tier still
|
|
85
|
-
*
|
|
86
|
-
* sooner in wall time instead of an hour per step.
|
|
86
|
+
* Nothing here relaxes the stop-losses: the fast tier still counts toward `MAX_WAKES_PER_CYCLE`
|
|
87
|
+
* and the futile-wake limit, so a task that spins productively-looking still hits the governor —
|
|
88
|
+
* just sooner in wall time instead of an hour per step.
|
|
87
89
|
*/
|
|
88
90
|
function attemptDelayMs(attempt, fingerprint, effects, settings) {
|
|
89
91
|
// A dispatch the channel refused never ran; retry it on the short delay regardless of state.
|
|
@@ -112,7 +114,7 @@ function taskDispatchId(channelId, entry, nowMs) {
|
|
|
112
114
|
const occurrence = entry.frontmatter.control?.cycleId ?? entry.frontmatter.wake ?? `t${nowMs}`;
|
|
113
115
|
return `task:${channelId}:${entry.id}:${occurrence}`;
|
|
114
116
|
}
|
|
115
|
-
export function createTaskDriverEvent(channelId, entry, nowMs
|
|
117
|
+
export function createTaskDriverEvent(channelId, entry, nowMs) {
|
|
116
118
|
const repairOnly = !entry.frontmatter.readable || entry.frontmatter.controlReadable === false;
|
|
117
119
|
const repair = repairOnly
|
|
118
120
|
? ` Task metadata is not readable; repair only the frontmatter/control in tasks/${entry.id}.md, then stop. ` +
|
|
@@ -128,7 +130,6 @@ export function createTaskDriverEvent(channelId, entry, nowMs, attemptGeneration
|
|
|
128
130
|
: "",
|
|
129
131
|
entry.latestNote ? `latest=${entry.latestNote};` : "",
|
|
130
132
|
control?.nextAction ? `next=${control.nextAction};` : "",
|
|
131
|
-
control ? `budget=${control.usage.attempts}/${control.budget.maxAttempts} attempts;` : "",
|
|
132
133
|
]
|
|
133
134
|
.filter(Boolean)
|
|
134
135
|
.join(" ");
|
|
@@ -148,24 +149,6 @@ export function createTaskDriverEvent(channelId, entry, nowMs, attemptGeneration
|
|
|
148
149
|
ts: String(nowMs),
|
|
149
150
|
conversationType: channelId.startsWith("group_") ? "2" : "1",
|
|
150
151
|
dispatchId: taskDispatchId(channelId, entry, nowMs),
|
|
151
|
-
taskAttemptGeneration: attemptGeneration,
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
/** Durable checker wake created by request-verification; it is a normal main-agent turn. */
|
|
155
|
-
export function createTaskVerificationEvent(channelId, entry, nowMs) {
|
|
156
|
-
const control = entry.frontmatter.control;
|
|
157
|
-
return {
|
|
158
|
-
type: channelId.startsWith("group_") ? "group" : "dm",
|
|
159
|
-
channelId,
|
|
160
|
-
user: "TASK_VERIFIER",
|
|
161
|
-
userName: "TASK_VERIFIER",
|
|
162
|
-
text: `[TASK_VERIFY:${entry.id}] Verify task ${entry.id} independently. ` +
|
|
163
|
-
`Read tasks/${entry.id}.md and the workspace verification instructions, then run a read-only ` +
|
|
164
|
-
`subagent with purpose=verify. Import its attestation with task_manage verify using the returned run id. ` +
|
|
165
|
-
`Do not modify the workspace or task contract; if verification fails, record the failure and leave the task recoverable.`,
|
|
166
|
-
ts: String(nowMs),
|
|
167
|
-
conversationType: channelId.startsWith("group_") ? "2" : "1",
|
|
168
|
-
dispatchId: `task:${channelId}:${entry.id}:verification:${control?.cycleId ?? `t${nowMs}`}`,
|
|
169
152
|
};
|
|
170
153
|
}
|
|
171
154
|
/** sleeping + a schedule cadence + a valid wake that is due → time to open the next cycle. */
|
|
@@ -511,17 +494,28 @@ export class TaskDriver {
|
|
|
511
494
|
}
|
|
512
495
|
continue;
|
|
513
496
|
}
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
entry.
|
|
519
|
-
|
|
497
|
+
const cycleId = entry.frontmatter.control?.cycleId;
|
|
498
|
+
const wakeCount = !repairOnly && previous && previous.cycleId === cycleId ? previous.wakeCount + 1 : 1;
|
|
499
|
+
if (wakeCount > MAX_WAKES_PER_CYCLE) {
|
|
500
|
+
const reason = `task exceeded ${MAX_WAKES_PER_CYCLE} wakes in this cycle (runaway failsafe)`;
|
|
501
|
+
if (await escalateTask(channelDir, entry.id, reason)) {
|
|
502
|
+
const receipt = taskGovernorReceipt(channelId, entry, reason, nowMs);
|
|
503
|
+
try {
|
|
504
|
+
await this.options.notify?.(receipt);
|
|
505
|
+
}
|
|
506
|
+
catch (error) {
|
|
507
|
+
log.logWarning(`[${channelId}] Task governor receipt failed`, errorMessage(error));
|
|
508
|
+
}
|
|
509
|
+
this.attempts.delete(key);
|
|
510
|
+
this.lastDispatchedTaskId.set(channelId, entry.id);
|
|
511
|
+
log.logWarning(`[${channelId}] Task driver disabled ${entry.id} (governor)`, reason);
|
|
512
|
+
}
|
|
513
|
+
continue;
|
|
514
|
+
}
|
|
515
|
+
const event = createTaskDriverEvent(channelId, entry, nowMs);
|
|
520
516
|
const accepted = await this.options.dispatch(event);
|
|
521
517
|
this.observeDispatch(event, accepted);
|
|
522
|
-
|
|
523
|
-
await releaseTaskAttemptClaim(channelDir, entry.id, claim);
|
|
524
|
-
this.attempts.set(key, { fingerprint, atMs: nowMs, accepted, effects, futileCount });
|
|
518
|
+
this.attempts.set(key, { fingerprint, atMs: nowMs, accepted, effects, futileCount, wakeCount, cycleId });
|
|
525
519
|
this.lastDispatchedTaskId.set(channelId, entry.id);
|
|
526
520
|
if (accepted) {
|
|
527
521
|
dispatched++;
|
|
@@ -10,11 +10,12 @@
|
|
|
10
10
|
*/
|
|
11
11
|
export declare function migrateLegacyTaskScheduleEvents(workspaceDir: string): Promise<void>;
|
|
12
12
|
/**
|
|
13
|
-
* Deterministically upgrade active
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
13
|
+
* Deterministically upgrade every active task file to the current (v3) control contract and
|
|
14
|
+
* status vocabulary (spec 043, phase 5). Unlike the earlier marker-gated pass, this scans and
|
|
15
|
+
* self-heals on every startup: each file is judged independently by what it actually contains
|
|
16
|
+
* (`control.version` and the raw `status` value), not by whether a one-time marker was written.
|
|
17
|
+
* A hand-edited or freshly-restored legacy file is repaired the next time the daemon starts, no
|
|
18
|
+
* matter how it got there. It never dispatches a task or performs an external action, and every
|
|
19
|
+
* file is protected by the same per-task mutation lock as normal lifecycle writes.
|
|
19
20
|
*/
|
|
20
21
|
export declare function migrateLegacyTaskState(workspaceDir: string): Promise<void>;
|