@oyasmi/pipiclaw 0.8.8 → 0.8.9-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/README.md +8 -3
- package/dist/agent/channel-runner.d.ts +1 -0
- package/dist/agent/channel-runner.js +3 -2
- package/dist/agent/effect-ledger.d.ts +11 -0
- package/dist/agent/effect-ledger.js +42 -0
- package/dist/agent/job-manager.d.ts +90 -13
- package/dist/agent/job-manager.js +235 -20
- package/dist/agent/prompt/sections.js +11 -2
- package/dist/agent/prompt/types.d.ts +7 -1
- package/dist/agent/runner-factory.d.ts +7 -0
- package/dist/agent/session-events.js +20 -1
- package/dist/index.d.ts +1 -1
- package/dist/log.d.ts +6 -0
- package/dist/log.js +12 -0
- package/dist/memory/consolidation.d.ts +5 -0
- package/dist/memory/consolidation.js +35 -147
- package/dist/memory/extraction.d.ts +45 -0
- package/dist/memory/extraction.js +227 -0
- package/dist/memory/lifecycle.js +5 -0
- package/dist/memory/maintenance-jobs.js +7 -0
- package/dist/memory/post-turn-review.d.ts +3 -1
- package/dist/memory/post-turn-review.js +20 -189
- package/dist/memory/recall.d.ts +0 -1
- package/dist/memory/recall.js +135 -77
- package/dist/memory/sidecar-worker.d.ts +7 -0
- package/dist/memory/sidecar-worker.js +23 -5
- package/dist/memory/task-digest.js +1 -1
- package/dist/paths.d.ts +1 -0
- package/dist/paths.js +11 -0
- package/dist/playbooks/background-jobs.md +40 -0
- package/dist/playbooks/catalog.d.ts +1 -7
- package/dist/playbooks/catalog.js +4 -14
- package/dist/playbooks/event-scheduling.md +8 -4
- package/dist/playbooks/memory-and-learning.md +1 -1
- package/dist/playbooks/outbound-media.md +45 -0
- package/dist/playbooks/runtime-orientation.md +1 -1
- package/dist/playbooks/task-closeout.md +12 -11
- package/dist/playbooks/task-delegation.md +11 -5
- package/dist/playbooks/task-driving.md +54 -21
- package/dist/playbooks/task-planning.md +46 -19
- package/dist/runtime/bootstrap.js +29 -4
- package/dist/runtime/channel-context.d.ts +33 -0
- package/dist/runtime/dingtalk.d.ts +22 -2
- package/dist/runtime/dingtalk.js +141 -0
- package/dist/runtime/durable-dispatch.d.ts +10 -0
- package/dist/runtime/durable-dispatch.js +42 -1
- package/dist/runtime/event-validation.d.ts +48 -0
- package/dist/runtime/event-validation.js +81 -0
- package/dist/runtime/events.d.ts +17 -8
- package/dist/runtime/events.js +96 -22
- package/dist/runtime/store.d.ts +1 -1
- package/dist/runtime/task-commands.js +29 -51
- package/dist/runtime/task-driver.d.ts +5 -8
- package/dist/runtime/task-driver.js +120 -93
- package/dist/runtime/task-migration.d.ts +11 -0
- package/dist/runtime/task-migration.js +70 -0
- package/dist/settings.d.ts +11 -0
- package/dist/settings.js +12 -0
- package/dist/shared/recoverable-error.d.ts +26 -0
- package/dist/shared/recoverable-error.js +29 -0
- package/dist/shared/task-events.d.ts +0 -26
- package/dist/shared/task-events.js +0 -15
- package/dist/shared/task-ledger.d.ts +32 -8
- package/dist/shared/task-ledger.js +66 -17
- package/dist/subagents/builtin/explorer.md +16 -0
- package/dist/subagents/builtin/researcher.md +17 -0
- package/dist/subagents/builtin/verifier.md +17 -0
- package/dist/subagents/discovery.d.ts +17 -3
- package/dist/subagents/discovery.js +88 -15
- package/dist/subagents/tool.d.ts +21 -1
- package/dist/subagents/tool.js +140 -12
- package/dist/tasks/control.d.ts +4 -0
- package/dist/tasks/control.js +3 -0
- package/dist/tasks/store.d.ts +28 -1
- package/dist/tasks/store.js +63 -6
- package/dist/tasks/transitions.d.ts +40 -0
- package/dist/tasks/transitions.js +82 -0
- package/dist/tools/bash.d.ts +2 -0
- package/dist/tools/bash.js +18 -5
- package/dist/tools/event-manage.js +28 -77
- package/dist/tools/index.d.ts +4 -0
- package/dist/tools/index.js +7 -2
- package/dist/tools/job.js +2 -2
- package/dist/tools/memory-manage.js +0 -1
- package/dist/tools/registry.d.ts +29 -8
- package/dist/tools/registry.js +39 -22
- package/dist/tools/send-media.d.ts +28 -0
- package/dist/tools/send-media.js +88 -0
- package/dist/tools/task-manage/create.d.ts +2 -0
- package/dist/tools/task-manage/create.js +35 -0
- package/dist/tools/task-manage/lifecycle.d.ts +6 -0
- package/dist/tools/task-manage/lifecycle.js +197 -0
- package/dist/tools/task-manage/schema.d.ts +43 -0
- package/dist/tools/task-manage/schema.js +90 -0
- package/dist/tools/task-manage/shared.d.ts +36 -0
- package/dist/tools/task-manage/shared.js +247 -0
- package/dist/tools/task-manage/types.d.ts +53 -0
- package/dist/tools/task-manage/types.js +1 -0
- package/dist/tools/task-manage/verification.d.ts +16 -0
- package/dist/tools/task-manage/verification.js +112 -0
- package/dist/tools/task-manage.d.ts +9 -89
- package/dist/tools/task-manage.js +8 -696
- package/dist/tools/tool-details.d.ts +44 -0
- package/dist/tools/tool-details.js +62 -0
- package/dist/tui/app.js +2 -0
- package/dist/tui/terminal-media-sender.d.ts +9 -0
- package/dist/tui/terminal-media-sender.js +15 -0
- package/package.json +2 -2
- package/dist/playbooks/task-recurring.md +0 -41
- package/dist/playbooks/task-repair.md +0 -45
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { readdir, readFile, unlink } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { parseScheduledEventContent } from "../../runtime/events.js";
|
|
5
|
+
import { taskEventPrefix } from "../../shared/task-events.js";
|
|
6
|
+
import { parseTaskFrontmatter, readActiveTasks, renderStandardTaskBody, renderTaskDocument, taskBody, } from "../../shared/task-ledger.js";
|
|
7
|
+
import { nextTaskWake, validateTaskSchedule } from "../../shared/task-schedule.js";
|
|
8
|
+
import { applyTaskControlPatch, createDefaultTaskControl } from "../../tasks/control.js";
|
|
9
|
+
import { readStoredTask } from "../../tasks/store.js";
|
|
10
|
+
import { isSettableTaskStatus } from "../../tasks/transitions.js";
|
|
11
|
+
import { RecoverableToolError } from "../tool-details.js";
|
|
12
|
+
import { SETTABLE_STATUSES } from "./schema.js";
|
|
13
|
+
export function tasksDir(options) {
|
|
14
|
+
return join(options.channelDir, "tasks");
|
|
15
|
+
}
|
|
16
|
+
export function eventsDir(options) {
|
|
17
|
+
return join(options.workspaceDir, "events");
|
|
18
|
+
}
|
|
19
|
+
export function renderTaskFile(fields, body) {
|
|
20
|
+
return renderTaskDocument(fields, body);
|
|
21
|
+
}
|
|
22
|
+
export function requiredField(value, field, action) {
|
|
23
|
+
const trimmed = value?.trim();
|
|
24
|
+
if (!trimmed) {
|
|
25
|
+
throw new RecoverableToolError(`action "${action}" requires ${field}.`);
|
|
26
|
+
}
|
|
27
|
+
return trimmed;
|
|
28
|
+
}
|
|
29
|
+
export function requireNonEmpty(value, field) {
|
|
30
|
+
return requiredField(value, field, "done");
|
|
31
|
+
}
|
|
32
|
+
export function markdownValue(value) {
|
|
33
|
+
const lines = value.trim().split(/\r?\n/);
|
|
34
|
+
if (lines.length === 1)
|
|
35
|
+
return lines[0];
|
|
36
|
+
return lines.map((line, index) => (index === 0 ? line : ` ${line}`)).join("\n");
|
|
37
|
+
}
|
|
38
|
+
export function appendCompletionEvidence(body, request) {
|
|
39
|
+
const summary = requireNonEmpty(request.summary, "summary");
|
|
40
|
+
const evidence = requireNonEmpty(request.evidence, "evidence");
|
|
41
|
+
const lines = [
|
|
42
|
+
"## Completion Evidence",
|
|
43
|
+
"",
|
|
44
|
+
`- Summary: ${markdownValue(summary)}`,
|
|
45
|
+
`- Evidence: ${markdownValue(evidence)}`,
|
|
46
|
+
];
|
|
47
|
+
const residualRisk = request.residualRisk?.trim();
|
|
48
|
+
if (residualRisk) {
|
|
49
|
+
lines.push(`- Residual risk: ${markdownValue(residualRisk)}`);
|
|
50
|
+
}
|
|
51
|
+
const normalizedBody = body.endsWith("\n") ? body.replace(/\n+$/, "\n") : `${body}\n`;
|
|
52
|
+
return `${normalizedBody}\n${lines.join("\n")}\n`;
|
|
53
|
+
}
|
|
54
|
+
function normalizeCreateStatus(status) {
|
|
55
|
+
if (status === undefined)
|
|
56
|
+
return "active";
|
|
57
|
+
if (isSettableTaskStatus(status))
|
|
58
|
+
return status;
|
|
59
|
+
throw new RecoverableToolError(`Invalid status "${status}". Use one of ${SETTABLE_STATUSES.join(", ")}.`);
|
|
60
|
+
}
|
|
61
|
+
export function renderTaskSkeleton(request) {
|
|
62
|
+
const title = requiredField(request.title, "title", "create");
|
|
63
|
+
const goal = requiredField(request.goal, "goal", "create");
|
|
64
|
+
const dod = requiredField(request.dod, "dod", "create");
|
|
65
|
+
// Independent verification is a real tax (an extra dispatch round plus a verifier
|
|
66
|
+
// sub-agent run) that only pays off when there is something a read-only verifier can
|
|
67
|
+
// actually check. Default to evidence (maker self-checks the DoD); the tool schema asks
|
|
68
|
+
// the model to opt into independent explicitly for tasks with a checkable artifact.
|
|
69
|
+
const mode = request.control?.verificationMode ?? "evidence";
|
|
70
|
+
const control = applyTaskControlPatch(createDefaultTaskControl(mode), request.control ?? {});
|
|
71
|
+
const fields = applySet({ status: normalizeCreateStatus(request.status), control }, request);
|
|
72
|
+
// First-cycle scheduling. A recurring task created without an explicit wake follows cron
|
|
73
|
+
// semantics: its first run is deferred to the next scheduled occurrence, not fired now.
|
|
74
|
+
// Without this the task is `active` + no `wake` → immediately actionable, so the driver
|
|
75
|
+
// resumes it the instant it is created (ignoring the schedule) and re-dispatches it every
|
|
76
|
+
// backoff interval; an agent expecting a scheduled wake then idles through those dispatches.
|
|
77
|
+
// Seeding the first wake here mirrors what `done` does for every subsequent cycle. Only a
|
|
78
|
+
// freshly opened task with no caller-supplied wake is seeded — an explicit wake (including a
|
|
79
|
+
// past one for "start now") or a non-active initial status is always honoured verbatim.
|
|
80
|
+
if (fields.schedule && fields.status === "active" && request.wake === undefined) {
|
|
81
|
+
fields.wake = nextTaskWake(fields.schedule)?.toISOString();
|
|
82
|
+
}
|
|
83
|
+
const body = renderStandardTaskBody({
|
|
84
|
+
title,
|
|
85
|
+
goal,
|
|
86
|
+
dod,
|
|
87
|
+
manual: request.manual,
|
|
88
|
+
verificationPlan: request.verificationPlan,
|
|
89
|
+
verificationMode: control.verification.mode,
|
|
90
|
+
});
|
|
91
|
+
return { fields, body };
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Read a task file and split it into validated frontmatter and a verbatim body.
|
|
95
|
+
* Fail-closed: an unreadable frontmatter block is rejected (fix it with edit first)
|
|
96
|
+
* rather than guessed at, so task_manage never silently mangles a body.
|
|
97
|
+
*/
|
|
98
|
+
export async function readTaskDocument(taskPath, id, allowControlRepair = false) {
|
|
99
|
+
if (!existsSync(taskPath)) {
|
|
100
|
+
throw new RecoverableToolError(`Task "${id}" does not exist; create it with task_manage action "create" first.`);
|
|
101
|
+
}
|
|
102
|
+
const content = await readFile(taskPath, "utf-8");
|
|
103
|
+
const frontmatter = parseTaskFrontmatter(content);
|
|
104
|
+
if (!frontmatter.readable) {
|
|
105
|
+
throw new RecoverableToolError(`Task "${id}" has no readable frontmatter; fix it with edit before using task_manage.`);
|
|
106
|
+
}
|
|
107
|
+
if (frontmatter.controlReadable === false && !allowControlRepair) {
|
|
108
|
+
throw new RecoverableToolError(`Task "${id}" has invalid control metadata; repair it with task_manage action "set" and an explicit control patch first.`);
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
fields: {
|
|
112
|
+
status: frontmatter.status ?? "active",
|
|
113
|
+
wake: frontmatter.wake,
|
|
114
|
+
schedule: frontmatter.schedule,
|
|
115
|
+
recurrence: frontmatter.recurrence,
|
|
116
|
+
control: frontmatter.control,
|
|
117
|
+
},
|
|
118
|
+
body: taskBody(content),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/** Apply a `set` request's optional fields onto the existing frontmatter. */
|
|
122
|
+
export function applySet(fields, request) {
|
|
123
|
+
const next = { ...fields };
|
|
124
|
+
if (request.control?.externalApproval === "granted") {
|
|
125
|
+
throw new Error('Only a user can grant external-action approval with "/tasks approve <id>".');
|
|
126
|
+
}
|
|
127
|
+
if (request.status !== undefined) {
|
|
128
|
+
if (!isSettableTaskStatus(request.status)) {
|
|
129
|
+
throw new RecoverableToolError(`Invalid status "${request.status}". Use one of ${SETTABLE_STATUSES.join(", ")}, or action "done".`);
|
|
130
|
+
}
|
|
131
|
+
next.status = request.status;
|
|
132
|
+
}
|
|
133
|
+
if (request.wake !== undefined) {
|
|
134
|
+
const trimmed = request.wake.trim();
|
|
135
|
+
if (trimmed === "") {
|
|
136
|
+
next.wake = undefined;
|
|
137
|
+
}
|
|
138
|
+
else if (!Number.isFinite(new Date(trimmed).getTime())) {
|
|
139
|
+
throw new RecoverableToolError(`wake "${request.wake}" is not a valid ISO8601 date.`);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
next.wake = trimmed;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (request.schedule !== undefined) {
|
|
146
|
+
const trimmed = request.schedule.trim();
|
|
147
|
+
if (trimmed === "") {
|
|
148
|
+
next.schedule = undefined;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
validateTaskSchedule(trimmed);
|
|
152
|
+
next.schedule = trimmed;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (request.recurrence !== undefined) {
|
|
156
|
+
const trimmed = request.recurrence.trim();
|
|
157
|
+
next.recurrence = trimmed === "" ? undefined : trimmed;
|
|
158
|
+
}
|
|
159
|
+
if (request.control !== undefined) {
|
|
160
|
+
next.control = applyTaskControlPatch(next.control ?? createDefaultTaskControl("evidence"), request.control);
|
|
161
|
+
}
|
|
162
|
+
// A done recurring task's wake is the single time rule's job: `normalizeTaskFields` fills the
|
|
163
|
+
// next occurrence on the write path, so no per-action recompute lives here anymore (D1).
|
|
164
|
+
return next;
|
|
165
|
+
}
|
|
166
|
+
export async function validateTaskRelations(options, id, fields) {
|
|
167
|
+
const control = fields.control;
|
|
168
|
+
if (!control)
|
|
169
|
+
return;
|
|
170
|
+
if (control.parent === id || control.dependsOn.includes(id)) {
|
|
171
|
+
throw new RecoverableToolError(`Task "${id}" cannot be its own parent or dependency.`);
|
|
172
|
+
}
|
|
173
|
+
for (const relatedId of [control.parent, ...control.dependsOn].filter((value) => Boolean(value))) {
|
|
174
|
+
const active = join(tasksDir(options), `${relatedId}.md`);
|
|
175
|
+
const archived = join(tasksDir(options), "archive", `${relatedId}.md`);
|
|
176
|
+
if (!existsSync(active) && !existsSync(archived)) {
|
|
177
|
+
throw new RecoverableToolError(`Related task "${relatedId}" does not exist; create it before linking task "${id}".`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (control.parent) {
|
|
181
|
+
const visited = new Set();
|
|
182
|
+
let current = control.parent;
|
|
183
|
+
while (current) {
|
|
184
|
+
if (visited.has(current)) {
|
|
185
|
+
throw new RecoverableToolError(`Existing parent chain for "${control.parent}" already contains a cycle.`);
|
|
186
|
+
}
|
|
187
|
+
if (current === id) {
|
|
188
|
+
throw new RecoverableToolError(`Task parent cycle detected while linking "${id}" to "${control.parent}".`);
|
|
189
|
+
}
|
|
190
|
+
visited.add(current);
|
|
191
|
+
const task = await readStoredTask(options.channelDir, current, true);
|
|
192
|
+
current = task?.fields.control?.parent;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
for (const dependencyId of control.dependsOn) {
|
|
196
|
+
const visited = new Set();
|
|
197
|
+
const stack = [dependencyId];
|
|
198
|
+
while (stack.length > 0) {
|
|
199
|
+
const current = stack.pop();
|
|
200
|
+
if (!current || visited.has(current))
|
|
201
|
+
continue;
|
|
202
|
+
if (current === id) {
|
|
203
|
+
throw new RecoverableToolError(`Task dependency cycle detected while linking "${id}" to "${dependencyId}".`);
|
|
204
|
+
}
|
|
205
|
+
visited.add(current);
|
|
206
|
+
const task = await readStoredTask(options.channelDir, current, true);
|
|
207
|
+
stack.push(...(task?.fields.control?.dependsOn ?? []));
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* On close-out (done or cancel), delete every task-owned event.
|
|
213
|
+
*
|
|
214
|
+
* Recurrence cadence now lives solely in the task's `schedule` frontmatter (027/029), so a
|
|
215
|
+
* recurring task needs no surviving `.schedule` event — the driver reopens each cycle from
|
|
216
|
+
* frontmatter. Any remaining task-owned events are temporary sensors/check-ins that a closed
|
|
217
|
+
* task should not keep alive. A file that cannot be parsed is left for `/events` to handle.
|
|
218
|
+
*/
|
|
219
|
+
export async function cleanupTaskEvents(options, id) {
|
|
220
|
+
const dir = eventsDir(options);
|
|
221
|
+
if (!existsSync(dir))
|
|
222
|
+
return { deleted: [] };
|
|
223
|
+
const prefix = taskEventPrefix(options.channelId, id);
|
|
224
|
+
const deleted = [];
|
|
225
|
+
for (const filename of (await readdir(dir)).sort()) {
|
|
226
|
+
if (!filename.endsWith(".json") || !filename.startsWith(prefix))
|
|
227
|
+
continue;
|
|
228
|
+
const eventPath = join(dir, filename);
|
|
229
|
+
try {
|
|
230
|
+
parseScheduledEventContent(await readFile(eventPath, "utf-8"), filename);
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
continue; // can't classify → leave it for /events to handle
|
|
234
|
+
}
|
|
235
|
+
await unlink(eventPath);
|
|
236
|
+
deleted.push(filename.slice(0, -".json".length));
|
|
237
|
+
}
|
|
238
|
+
return { deleted };
|
|
239
|
+
}
|
|
240
|
+
export async function unfinishedChildren(options, parentId) {
|
|
241
|
+
const entries = await readActiveTasks(tasksDir(options));
|
|
242
|
+
return entries
|
|
243
|
+
.filter((entry) => entry.frontmatter.control?.parent === parentId &&
|
|
244
|
+
entry.frontmatter.status !== "done" &&
|
|
245
|
+
entry.frontmatter.status !== "cancelled")
|
|
246
|
+
.map((entry) => entry.id);
|
|
247
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { TaskControl, TaskControlPatch } from "../../tasks/control.js";
|
|
2
|
+
export type TaskManageAction = "create" | "progress" | "candidate" | "set" | "verify" | "done" | "cancel" | "list";
|
|
3
|
+
export interface TaskManageResult {
|
|
4
|
+
action: TaskManageAction;
|
|
5
|
+
id?: string;
|
|
6
|
+
path?: string;
|
|
7
|
+
status?: string;
|
|
8
|
+
archived?: boolean;
|
|
9
|
+
deletedEvents?: string[];
|
|
10
|
+
tasks?: Array<{
|
|
11
|
+
id: string;
|
|
12
|
+
title: string;
|
|
13
|
+
status: string;
|
|
14
|
+
wake?: string;
|
|
15
|
+
actionable: boolean;
|
|
16
|
+
control?: TaskControl;
|
|
17
|
+
}>;
|
|
18
|
+
notice: string;
|
|
19
|
+
}
|
|
20
|
+
export interface TaskManageRequest {
|
|
21
|
+
action: TaskManageAction;
|
|
22
|
+
id?: string;
|
|
23
|
+
title?: string;
|
|
24
|
+
goal?: string;
|
|
25
|
+
dod?: string;
|
|
26
|
+
manual?: string;
|
|
27
|
+
status?: string;
|
|
28
|
+
wake?: string;
|
|
29
|
+
schedule?: string;
|
|
30
|
+
recurrence?: string;
|
|
31
|
+
note?: string;
|
|
32
|
+
verificationPlan?: string;
|
|
33
|
+
control?: TaskControlPatch;
|
|
34
|
+
verifierRunId?: string;
|
|
35
|
+
summary?: string;
|
|
36
|
+
evidence?: string;
|
|
37
|
+
residualRisk?: string;
|
|
38
|
+
reason?: string;
|
|
39
|
+
}
|
|
40
|
+
export interface TaskManageToolOptions {
|
|
41
|
+
workspaceDir: string;
|
|
42
|
+
channelDir: string;
|
|
43
|
+
channelId: string;
|
|
44
|
+
/** Project checkout whose artifact state an independent verifier binds to. */
|
|
45
|
+
workingDirectory?: string;
|
|
46
|
+
}
|
|
47
|
+
export interface TaskFields {
|
|
48
|
+
status: string;
|
|
49
|
+
wake?: string;
|
|
50
|
+
schedule?: string;
|
|
51
|
+
recurrence?: string;
|
|
52
|
+
control?: TaskControl;
|
|
53
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type TaskVerification } from "../../tasks/control.js";
|
|
2
|
+
import type { TaskManageRequest, TaskManageResult, TaskManageToolOptions } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Move a task into the verification lane. The runtime driver will explicitly
|
|
5
|
+
* wake a fresh verifier on the next attempt, keeping the maker from grading
|
|
6
|
+
* its own work in the same reasoning context.
|
|
7
|
+
*/
|
|
8
|
+
export declare function candidateTask(options: TaskManageToolOptions, request: TaskManageRequest): Promise<TaskManageResult>;
|
|
9
|
+
export declare function verifyTask(options: TaskManageToolOptions, request: TaskManageRequest): Promise<TaskManageResult>;
|
|
10
|
+
/**
|
|
11
|
+
* `verify` writes control.verification from a real attestation file, but the task Markdown
|
|
12
|
+
* itself is writable by the agent's own write/edit tools — nothing stops it from hand-crafting
|
|
13
|
+
* a "passed" verification block that was never backed by a real verifier run. Re-check the
|
|
14
|
+
* attestation file on the consuming side (done/doctor) too, not just at import time.
|
|
15
|
+
*/
|
|
16
|
+
export declare function assertVerificationAttestationMatches(channelDir: string, id: string, verification: TaskVerification): Promise<void>;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { writeFileAtomically } from "../../shared/atomic-file.js";
|
|
3
|
+
import { appendCurrentCycleNote, normalizeTaskId, uncheckedTaskAcceptanceItems } from "../../shared/task-ledger.js";
|
|
4
|
+
import { workspaceSubjectHash } from "../../tasks/artifact-subject.js";
|
|
5
|
+
import { createDefaultTaskControl } from "../../tasks/control.js";
|
|
6
|
+
import { readStoredTask, taskBodyHash } from "../../tasks/store.js";
|
|
7
|
+
import { normalizeStoredStatus, resolveTaskTransition } from "../../tasks/transitions.js";
|
|
8
|
+
import { readVerificationAttestation } from "../../tasks/verification.js";
|
|
9
|
+
import { RecoverableToolError } from "../tool-details.js";
|
|
10
|
+
import { readTaskDocument, renderTaskFile, requiredField, tasksDir } from "./shared.js";
|
|
11
|
+
/**
|
|
12
|
+
* Move a task into the verification lane. The runtime driver will explicitly
|
|
13
|
+
* wake a fresh verifier on the next attempt, keeping the maker from grading
|
|
14
|
+
* its own work in the same reasoning context.
|
|
15
|
+
*/
|
|
16
|
+
export async function candidateTask(options, request) {
|
|
17
|
+
if (!request.id)
|
|
18
|
+
throw new RecoverableToolError('action "candidate" requires an id.');
|
|
19
|
+
const id = normalizeTaskId(request.id);
|
|
20
|
+
const note = requiredField(request.note, "note", "candidate");
|
|
21
|
+
const taskPath = join(tasksDir(options), `${id}.md`);
|
|
22
|
+
const { fields, body } = await readTaskDocument(taskPath, id);
|
|
23
|
+
resolveTaskTransition("candidate", id, normalizeStoredStatus(fields.status));
|
|
24
|
+
const uncheckedAcceptance = uncheckedTaskAcceptanceItems(body);
|
|
25
|
+
if (uncheckedAcceptance.length > 0) {
|
|
26
|
+
throw new RecoverableToolError(`Task "${id}" still has unchecked acceptance items: ${uncheckedAcceptance.join("; ")}. Check them with evidence before requesting verification.`);
|
|
27
|
+
}
|
|
28
|
+
const control = fields.control ?? createDefaultTaskControl("independent");
|
|
29
|
+
control.verification = { mode: "independent", status: "pending" };
|
|
30
|
+
control.lastOutcome = "progress";
|
|
31
|
+
control.blockedReason = undefined;
|
|
32
|
+
control.nextAction = "Run a purpose=verify sub-agent and import its attestation.";
|
|
33
|
+
const nextBody = appendCurrentCycleNote(body, note);
|
|
34
|
+
await writeFileAtomically(taskPath, renderTaskFile({ ...fields, status: "verifying", wake: undefined, control }, nextBody));
|
|
35
|
+
return {
|
|
36
|
+
action: "candidate",
|
|
37
|
+
id,
|
|
38
|
+
path: taskPath,
|
|
39
|
+
status: "verifying",
|
|
40
|
+
notice: `任务 \`${id}\` 已进入独立验收队列;driver 将安排 verifier。`,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export async function verifyTask(options, request) {
|
|
44
|
+
if (!request.id)
|
|
45
|
+
throw new RecoverableToolError('action "verify" requires an id.');
|
|
46
|
+
const id = normalizeTaskId(request.id);
|
|
47
|
+
const runId = requiredField(request.verifierRunId, "verifierRunId", "verify");
|
|
48
|
+
const task = await readStoredTask(options.channelDir, id);
|
|
49
|
+
if (!task)
|
|
50
|
+
throw new RecoverableToolError(`Task "${id}" does not exist; create it before verification.`);
|
|
51
|
+
const attestation = await readVerificationAttestation(options.channelDir, runId);
|
|
52
|
+
if (attestation.taskId !== id) {
|
|
53
|
+
throw new RecoverableToolError(`Verification run "${runId}" belongs to task "${attestation.taskId}", not "${id}".`);
|
|
54
|
+
}
|
|
55
|
+
if (attestation.workspaceChanged) {
|
|
56
|
+
throw new RecoverableToolError(`Verification run "${runId}" changed the workspace; rerun a read-only verifier.`);
|
|
57
|
+
}
|
|
58
|
+
if (attestation.bodyHash !== taskBodyHash(task.body)) {
|
|
59
|
+
throw new RecoverableToolError(`Task "${id}" changed after verification run "${runId}"; rerun the verifier on current content.`);
|
|
60
|
+
}
|
|
61
|
+
if (attestation.subjectHash) {
|
|
62
|
+
const currentSubject = await workspaceSubjectHash(options.workingDirectory ?? process.cwd());
|
|
63
|
+
if (!currentSubject) {
|
|
64
|
+
throw new RecoverableToolError(`Verification run "${runId}" is bound to a Git artifact subject, but the current checkout cannot be read. Rerun verification from the project checkout.`);
|
|
65
|
+
}
|
|
66
|
+
if (currentSubject !== attestation.subjectHash) {
|
|
67
|
+
throw new RecoverableToolError(`Task "${id}" artifacts changed after verification run "${runId}"; rerun the verifier.`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const control = task.fields.control ?? createDefaultTaskControl("independent");
|
|
71
|
+
control.verification = {
|
|
72
|
+
mode: "independent",
|
|
73
|
+
status: attestation.verdict === "pass" ? "passed" : "failed",
|
|
74
|
+
runId,
|
|
75
|
+
evidence: attestation.evidence,
|
|
76
|
+
bodyHash: attestation.bodyHash,
|
|
77
|
+
subjectHash: attestation.subjectHash,
|
|
78
|
+
checkedAt: attestation.checkedAt,
|
|
79
|
+
};
|
|
80
|
+
control.lastOutcome = attestation.verdict === "pass" ? "verified" : "failed";
|
|
81
|
+
control.blockedReason = attestation.verdict === "fail" ? attestation.evidence : undefined;
|
|
82
|
+
task.fields.control = control;
|
|
83
|
+
await writeFileAtomically(task.path, renderTaskFile(task.fields, task.body));
|
|
84
|
+
return {
|
|
85
|
+
action: "verify",
|
|
86
|
+
id,
|
|
87
|
+
path: task.path,
|
|
88
|
+
status: task.fields.status,
|
|
89
|
+
notice: `任务 \`${id}\` 独立验收结果:${attestation.verdict.toUpperCase()}(run: ${runId})。`,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* `verify` writes control.verification from a real attestation file, but the task Markdown
|
|
94
|
+
* itself is writable by the agent's own write/edit tools — nothing stops it from hand-crafting
|
|
95
|
+
* a "passed" verification block that was never backed by a real verifier run. Re-check the
|
|
96
|
+
* attestation file on the consuming side (done/doctor) too, not just at import time.
|
|
97
|
+
*/
|
|
98
|
+
export async function assertVerificationAttestationMatches(channelDir, id, verification) {
|
|
99
|
+
if (!verification.runId) {
|
|
100
|
+
throw new RecoverableToolError(`Task "${id}" has no verification run id; rerun task_manage verify before done.`);
|
|
101
|
+
}
|
|
102
|
+
const attestation = await readVerificationAttestation(channelDir, verification.runId);
|
|
103
|
+
if (attestation.taskId !== id) {
|
|
104
|
+
throw new RecoverableToolError(`Verification run "${verification.runId}" belongs to task "${attestation.taskId}", not "${id}"; rerun verification.`);
|
|
105
|
+
}
|
|
106
|
+
if (attestation.verdict !== "pass") {
|
|
107
|
+
throw new RecoverableToolError(`Verification run "${verification.runId}" recorded a FAIL, not a PASS; rerun verification.`);
|
|
108
|
+
}
|
|
109
|
+
if (attestation.bodyHash !== verification.bodyHash) {
|
|
110
|
+
throw new RecoverableToolError(`Task "${id}" control.verification.bodyHash does not match the attestation for run "${verification.runId}"; rerun task_manage verify.`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -1,92 +1,12 @@
|
|
|
1
1
|
import type { AgentTool } from "@earendil-works/pi-agent-core";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
manual: Type.TOptional<Type.TString>;
|
|
12
|
-
verificationPlan: Type.TOptional<Type.TString>;
|
|
13
|
-
control: Type.TOptional<Type.TObject<{
|
|
14
|
-
priority: Type.TOptional<Type.TUnion<[Type.TLiteral<"low">, Type.TLiteral<"normal">, Type.TLiteral<"high">, Type.TLiteral<"critical">]>>;
|
|
15
|
-
deadline: Type.TOptional<Type.TString>;
|
|
16
|
-
nextAction: Type.TOptional<Type.TString>;
|
|
17
|
-
lastOutcome: Type.TOptional<Type.TUnion<[Type.TLiteral<"pending">, Type.TLiteral<"running">, Type.TLiteral<"progress">, Type.TLiteral<"blocked">, Type.TLiteral<"failed">, Type.TLiteral<"verified">]>>;
|
|
18
|
-
blockedReason: Type.TOptional<Type.TString>;
|
|
19
|
-
parent: Type.TOptional<Type.TString>;
|
|
20
|
-
dependsOn: Type.TOptional<Type.TArray<Type.TString>>;
|
|
21
|
-
isolation: Type.TOptional<Type.TUnion<[Type.TLiteral<"shared">, Type.TLiteral<"worktree">]>>;
|
|
22
|
-
sideEffects: Type.TOptional<Type.TUnion<[Type.TLiteral<"read-only">, Type.TLiteral<"workspace">, Type.TLiteral<"external">]>>;
|
|
23
|
-
externalApproval: Type.TOptional<Type.TUnion<[Type.TLiteral<"not-required">, Type.TLiteral<"required">]>>;
|
|
24
|
-
maxAttempts: Type.TOptional<Type.TInteger>;
|
|
25
|
-
maxTokens: Type.TOptional<Type.TNumber>;
|
|
26
|
-
maxCostUsd: Type.TOptional<Type.TNumber>;
|
|
27
|
-
maxWallTimeMinutes: Type.TOptional<Type.TNumber>;
|
|
28
|
-
verificationMode: Type.TOptional<Type.TUnion<[Type.TLiteral<"evidence">, Type.TLiteral<"independent">]>>;
|
|
29
|
-
worktreePath: Type.TOptional<Type.TString>;
|
|
30
|
-
worktreeBranch: Type.TOptional<Type.TString>;
|
|
31
|
-
}>>;
|
|
32
|
-
status: Type.TOptional<Type.TUnion<Type.TLiteral<"blocked" | "paused" | "open" | "in-progress" | "awaiting-user">[]>>;
|
|
33
|
-
wake: Type.TOptional<Type.TString>;
|
|
34
|
-
schedule: Type.TOptional<Type.TString>;
|
|
35
|
-
recurrence: Type.TOptional<Type.TString>;
|
|
36
|
-
note: Type.TOptional<Type.TString>;
|
|
37
|
-
verifierRunId: Type.TOptional<Type.TString>;
|
|
38
|
-
summary: Type.TOptional<Type.TString>;
|
|
39
|
-
evidence: Type.TOptional<Type.TString>;
|
|
40
|
-
residualRisk: Type.TOptional<Type.TString>;
|
|
41
|
-
reason: Type.TOptional<Type.TString>;
|
|
42
|
-
cycleId: Type.TOptional<Type.TString>;
|
|
43
|
-
}>;
|
|
44
|
-
export type TaskManageAction = "create" | "progress" | "candidate" | "set" | "verify" | "done" | "cancel" | "start-cycle" | "list";
|
|
45
|
-
export interface TaskManageResult {
|
|
46
|
-
action: TaskManageAction;
|
|
47
|
-
id?: string;
|
|
48
|
-
path?: string;
|
|
49
|
-
status?: string;
|
|
50
|
-
archived?: boolean;
|
|
51
|
-
deletedEvents?: string[];
|
|
52
|
-
tasks?: Array<{
|
|
53
|
-
id: string;
|
|
54
|
-
title: string;
|
|
55
|
-
status: string;
|
|
56
|
-
wake?: string;
|
|
57
|
-
actionable: boolean;
|
|
58
|
-
control?: TaskControl;
|
|
59
|
-
}>;
|
|
60
|
-
notice: string;
|
|
61
|
-
}
|
|
62
|
-
export interface TaskManageRequest {
|
|
63
|
-
action: TaskManageAction;
|
|
64
|
-
id?: string;
|
|
65
|
-
title?: string;
|
|
66
|
-
goal?: string;
|
|
67
|
-
dod?: string;
|
|
68
|
-
manual?: string;
|
|
69
|
-
status?: string;
|
|
70
|
-
wake?: string;
|
|
71
|
-
schedule?: string;
|
|
72
|
-
recurrence?: string;
|
|
73
|
-
note?: string;
|
|
74
|
-
verificationPlan?: string;
|
|
75
|
-
control?: TaskControlPatch;
|
|
76
|
-
verifierRunId?: string;
|
|
77
|
-
summary?: string;
|
|
78
|
-
evidence?: string;
|
|
79
|
-
residualRisk?: string;
|
|
80
|
-
reason?: string;
|
|
81
|
-
cycleId?: string;
|
|
82
|
-
}
|
|
83
|
-
export interface TaskManageToolOptions {
|
|
84
|
-
workspaceDir: string;
|
|
85
|
-
channelDir: string;
|
|
86
|
-
channelId: string;
|
|
87
|
-
/** Project checkout whose artifact state an independent verifier binds to. */
|
|
88
|
-
workingDirectory?: string;
|
|
89
|
-
}
|
|
2
|
+
import { taskManageSchema } from "./task-manage/schema.js";
|
|
3
|
+
import type { TaskManageRequest, TaskManageResult, TaskManageToolOptions } from "./task-manage/types.js";
|
|
4
|
+
export type { TaskManageAction, TaskManageRequest, TaskManageResult, TaskManageToolOptions, } from "./task-manage/types.js";
|
|
5
|
+
/**
|
|
6
|
+
* The `task_manage` dispatcher. Each action lives in a focused module — `create`,
|
|
7
|
+
* `lifecycle` (progress/set/done/cancel/list) and `verification` (candidate/verify) — over a
|
|
8
|
+
* shared helper layer, with the status transition table in `src/tasks/transitions.ts` (spec
|
|
9
|
+
* 029, D7). This entry only routes and wraps the tool.
|
|
10
|
+
*/
|
|
90
11
|
export declare function manageTask(options: TaskManageToolOptions, request: TaskManageRequest): Promise<TaskManageResult>;
|
|
91
12
|
export declare function createTaskManageTool(options: TaskManageToolOptions): AgentTool<typeof taskManageSchema>;
|
|
92
|
-
export {};
|