@oyasmi/pipiclaw 0.9.1-beta.1 → 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 +0 -1
- package/dist/agent/channel-runner.js +0 -2
- package/dist/agent/runner-factory.d.ts +0 -2
- 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/bootstrap.d.ts +1 -1
- package/dist/runtime/bootstrap.js +13 -49
- package/dist/runtime/channel-event.d.ts +0 -6
- 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
package/dist/tasks/store.js
CHANGED
|
@@ -4,7 +4,7 @@ import { readFile } from "node:fs/promises";
|
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { writeFileAtomically } from "../shared/atomic-file.js";
|
|
6
6
|
import { formatLocalTime, parseLocalTime } from "../shared/local-time.js";
|
|
7
|
-
import { createDefaultTaskControl, resetTaskControlForCycle
|
|
7
|
+
import { createDefaultTaskControl, resetTaskControlForCycle } from "./control.js";
|
|
8
8
|
import { normalizeTaskId, parseTaskFrontmatter, renderTaskDocument, startTaskCycle, taskBody, taskContractSegment, } from "./ledger.js";
|
|
9
9
|
import { withTaskMutation } from "./mutation-lock.js";
|
|
10
10
|
import { normalizeStoredStatus, resolveTaskTransition } from "./transitions.js";
|
|
@@ -45,7 +45,6 @@ export async function readStoredTask(channelDir, idInput, includeArchive = false
|
|
|
45
45
|
enabled: frontmatter.enabled,
|
|
46
46
|
wake: frontmatter.wake,
|
|
47
47
|
schedule: frontmatter.schedule,
|
|
48
|
-
recurrence: frontmatter.recurrence,
|
|
49
48
|
control: frontmatter.control,
|
|
50
49
|
outcome: frontmatter.archiveOutcome,
|
|
51
50
|
closedAt: frontmatter.closedAt,
|
|
@@ -66,186 +65,55 @@ export async function updateStoredTask(channelDir, id, update, includeArchive =
|
|
|
66
65
|
return document;
|
|
67
66
|
});
|
|
68
67
|
}
|
|
69
|
-
export async function claimTaskAttempt(channelDir, id, now) {
|
|
70
|
-
let claim;
|
|
71
|
-
const document = await updateStoredTask(channelDir, id, (task) => {
|
|
72
|
-
const status = normalizeStoredStatus(task.fields.status, Boolean(task.fields.schedule));
|
|
73
|
-
if (status !== "active" || task.fields.enabled === false)
|
|
74
|
-
return;
|
|
75
|
-
task.fields.control ??= createDefaultTaskControl();
|
|
76
|
-
claim = {
|
|
77
|
-
control: task.fields.control,
|
|
78
|
-
previousLastOutcome: task.fields.control.lastOutcome,
|
|
79
|
-
previousBlockedReason: task.fields.control.blockedReason,
|
|
80
|
-
previousLastStartedAt: task.fields.control.lastStartedAt,
|
|
81
|
-
generation: task.fields.control.attemptGeneration + 1,
|
|
82
|
-
};
|
|
83
|
-
task.fields.control.usage.attempts++;
|
|
84
|
-
task.fields.control.attemptGeneration = claim.generation;
|
|
85
|
-
task.fields.control.lastStartedAt = formatLocalTime(now);
|
|
86
|
-
task.fields.control.lastOutcome = "running";
|
|
87
|
-
});
|
|
88
|
-
if (claim && document?.fields.control)
|
|
89
|
-
claim.control = document.fields.control;
|
|
90
|
-
return claim;
|
|
91
|
-
}
|
|
92
|
-
export async function releaseTaskAttemptClaim(channelDir, id, claim) {
|
|
93
|
-
await updateStoredTask(channelDir, id, (task) => {
|
|
94
|
-
const control = task.fields.control;
|
|
95
|
-
if (!control || control.attemptGeneration !== claim.generation)
|
|
96
|
-
return;
|
|
97
|
-
control.usage.attempts = Math.max(0, control.usage.attempts - 1);
|
|
98
|
-
control.lastStartedAt = claim.previousLastStartedAt;
|
|
99
|
-
control.lastOutcome = claim.previousLastOutcome;
|
|
100
|
-
control.blockedReason = claim.previousBlockedReason;
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
export async function finishTaskAttempt(channelDir, id, result) {
|
|
104
|
-
await updateStoredTask(channelDir, id, (task) => {
|
|
105
|
-
const control = task.fields.control;
|
|
106
|
-
if (!control)
|
|
107
|
-
return;
|
|
108
|
-
control.usage.tokens += Math.max(0, Math.floor(result.tokens));
|
|
109
|
-
control.usage.costUsd += Math.max(0, result.costUsd);
|
|
110
|
-
control.usage.costKnown &&= result.costKnown;
|
|
111
|
-
control.usage.wallTimeMinutes += Math.max(0, result.wallTimeMinutes);
|
|
112
|
-
if (result.generation !== undefined && control.attemptGeneration !== result.generation)
|
|
113
|
-
return;
|
|
114
|
-
control.lastFinishedAt = formatLocalTime(result.finishedAt);
|
|
115
|
-
if (result.silent) {
|
|
116
|
-
// The driver claimed before dispatch to prevent concurrent work. A silent
|
|
117
|
-
// turn performed no task advancement, so retain its cost audit but not its
|
|
118
|
-
// attempt charge.
|
|
119
|
-
control.usage.attempts = Math.max(0, control.usage.attempts - 1);
|
|
120
|
-
if (control.lastOutcome === "running") {
|
|
121
|
-
control.lastOutcome = "pending";
|
|
122
|
-
control.blockedReason = undefined;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
else if (result.failed) {
|
|
126
|
-
control.lastOutcome = "failed";
|
|
127
|
-
control.blockedReason = "The previous agent run failed; inspect the runtime error before retrying.";
|
|
128
|
-
}
|
|
129
|
-
else if (control.lastOutcome === "running") {
|
|
130
|
-
control.lastOutcome = "progress";
|
|
131
|
-
control.blockedReason = undefined;
|
|
132
|
-
}
|
|
133
|
-
}, true);
|
|
134
|
-
}
|
|
135
68
|
/**
|
|
136
69
|
* A governor stop: disable the task without changing its lifecycle stage or wake. The structured
|
|
137
70
|
* stop record is the durable recovery receipt; notification is handled by the runtime driver.
|
|
138
71
|
*/
|
|
139
72
|
export async function escalateTask(channelDir, id, reason) {
|
|
140
73
|
const document = await updateStoredTask(channelDir, id, (task) => {
|
|
141
|
-
const status = normalizeStoredStatus(task.fields.status
|
|
74
|
+
const status = normalizeStoredStatus(task.fields.status);
|
|
142
75
|
resolveTaskTransition("governor-stop", id, status);
|
|
143
76
|
task.fields.enabled = false;
|
|
144
77
|
task.fields.control ??= createDefaultTaskControl();
|
|
145
78
|
task.fields.control.stop = { by: "governor", reason, at: formatLocalTime() };
|
|
146
|
-
task.fields.control.lastOutcome = "blocked";
|
|
147
|
-
task.fields.control.blockedReason = reason;
|
|
148
79
|
});
|
|
149
80
|
return document !== undefined;
|
|
150
81
|
}
|
|
151
|
-
/**
|
|
152
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Atomically convert a due/completion-woken task to active. Idempotent by construction: once a
|
|
84
|
+
* task is no longer `waiting`, a redelivered wake or retry finds `status !== "waiting"` and is a
|
|
85
|
+
* safe no-op, so no separate claim/handoff bookkeeping is needed to detect a duplicate activation.
|
|
86
|
+
*
|
|
87
|
+
* Wake legitimacy is entirely the caller's responsibility (spec 043's `waitingFor`
|
|
88
|
+
* de-authorization): a completion wake reaches here only after the caller has verified an
|
|
89
|
+
* actually-settled run/job whose own recorded `taskId` names this task (`task-wake.ts`'s
|
|
90
|
+
* `isVerified*Wake`). `waitingFor` itself is display-only and never gates activation.
|
|
91
|
+
*/
|
|
92
|
+
export async function activateWaitingTask(channelDir, id, hooks) {
|
|
153
93
|
let activated = false;
|
|
154
94
|
const document = await updateStoredTask(channelDir, id, (task) => {
|
|
155
|
-
const status = normalizeStoredStatus(task.fields.status
|
|
95
|
+
const status = normalizeStoredStatus(task.fields.status);
|
|
156
96
|
if (status !== "waiting" || task.fields.enabled === false)
|
|
157
97
|
return;
|
|
158
|
-
|
|
159
|
-
return;
|
|
98
|
+
hooks?.beforeActivation?.();
|
|
160
99
|
activated = true;
|
|
161
100
|
task.fields.control ??= createDefaultTaskControl();
|
|
162
101
|
task.fields.status = "active";
|
|
163
102
|
task.fields.wake = undefined;
|
|
164
|
-
if (task.fields.control)
|
|
103
|
+
if (task.fields.control)
|
|
165
104
|
task.fields.control.waitingFor = undefined;
|
|
166
|
-
task.fields.control.blockedReason = undefined;
|
|
167
|
-
}
|
|
168
105
|
});
|
|
169
106
|
return activated ? document : undefined;
|
|
170
107
|
}
|
|
171
|
-
/**
|
|
172
|
-
|
|
173
|
-
* but unclaimed, which would make a retry of the same durable wake look ineligible. */
|
|
174
|
-
export async function activateWaitingTaskAndClaimAttempt(channelDir, id, expectedWaitingFor, now, hooks, handoff) {
|
|
175
|
-
let generation;
|
|
176
|
-
const document = await updateStoredTask(channelDir, id, (task) => {
|
|
177
|
-
const status = normalizeStoredStatus(task.fields.status, Boolean(task.fields.schedule));
|
|
178
|
-
const existingHandoff = task.fields.control?.wakeHandoff;
|
|
179
|
-
if (handoff &&
|
|
180
|
-
status === "active" &&
|
|
181
|
-
existingHandoff?.kind === handoff.kind &&
|
|
182
|
-
existingHandoff.resourceId === handoff.resourceId &&
|
|
183
|
-
existingHandoff.dispatchId === handoff.dispatchId &&
|
|
184
|
-
task.fields.control?.attemptGeneration === existingHandoff.generation) {
|
|
185
|
-
generation = existingHandoff.generation;
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
if (status !== "waiting" || task.fields.enabled === false)
|
|
189
|
-
return;
|
|
190
|
-
if (task.fields.control?.waitingFor !== expectedWaitingFor)
|
|
191
|
-
return;
|
|
192
|
-
hooks?.beforeActivation?.();
|
|
193
|
-
task.fields.control ??= createDefaultTaskControl();
|
|
194
|
-
task.fields.status = "active";
|
|
195
|
-
task.fields.wake = undefined;
|
|
196
|
-
task.fields.control.waitingFor = undefined;
|
|
197
|
-
task.fields.control.blockedReason = undefined;
|
|
198
|
-
hooks?.beforeAttemptClaim?.();
|
|
199
|
-
generation = task.fields.control.attemptGeneration + 1;
|
|
200
|
-
const previousLastOutcome = task.fields.control.lastOutcome;
|
|
201
|
-
const previousBlockedReason = task.fields.control.blockedReason;
|
|
202
|
-
const previousLastStartedAt = task.fields.control.lastStartedAt;
|
|
203
|
-
task.fields.control.usage.attempts++;
|
|
204
|
-
task.fields.control.attemptGeneration = generation;
|
|
205
|
-
task.fields.control.lastStartedAt = formatLocalTime(now);
|
|
206
|
-
task.fields.control.lastOutcome = "running";
|
|
207
|
-
if (handoff) {
|
|
208
|
-
task.fields.control.wakeHandoff = {
|
|
209
|
-
...handoff,
|
|
210
|
-
generation,
|
|
211
|
-
previousLastOutcome,
|
|
212
|
-
previousBlockedReason,
|
|
213
|
-
previousLastStartedAt,
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
});
|
|
217
|
-
return document && generation !== undefined ? { document, generation } : undefined;
|
|
218
|
-
}
|
|
219
|
-
function isMatchingWakeHandoff(handoff, expected) {
|
|
220
|
-
return (handoff?.kind === expected.kind &&
|
|
221
|
-
handoff.resourceId === expected.resourceId &&
|
|
222
|
-
handoff.dispatchId === expected.dispatchId);
|
|
223
|
-
}
|
|
224
|
-
/** Return a transport-rejected structured wake to its exact pre-activation task state. */
|
|
225
|
-
export async function rollbackWakeTaskActivation(channelDir, id, expected) {
|
|
108
|
+
/** Return a task to waiting after a transport failed to accept its just-activated turn. */
|
|
109
|
+
export async function rollbackWaitingTask(channelDir, id, waitingFor) {
|
|
226
110
|
await updateStoredTask(channelDir, id, (task) => {
|
|
227
|
-
|
|
228
|
-
const handoff = control?.wakeHandoff;
|
|
229
|
-
if (!control || !isMatchingWakeHandoff(handoff, expected) || control.attemptGeneration !== handoff.generation) {
|
|
111
|
+
if (task.fields.status !== "active")
|
|
230
112
|
return;
|
|
231
|
-
}
|
|
232
113
|
task.fields.status = "waiting";
|
|
233
114
|
task.fields.wake = undefined;
|
|
234
|
-
control
|
|
235
|
-
|
|
236
|
-
control.attemptGeneration = Math.max(0, handoff.generation - 1);
|
|
237
|
-
control.lastOutcome = handoff.previousLastOutcome;
|
|
238
|
-
control.blockedReason = handoff.previousBlockedReason;
|
|
239
|
-
control.lastStartedAt = handoff.previousLastStartedAt;
|
|
240
|
-
control.wakeHandoff = undefined;
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
/** Clear the durable handoff after the transport has accepted the activated turn. */
|
|
244
|
-
export async function finishWakeTaskActivation(channelDir, id, expected) {
|
|
245
|
-
await updateStoredTask(channelDir, id, (task) => {
|
|
246
|
-
if (isMatchingWakeHandoff(task.fields.control?.wakeHandoff, expected) && task.fields.control) {
|
|
247
|
-
task.fields.control.wakeHandoff = undefined;
|
|
248
|
-
}
|
|
115
|
+
if (task.fields.control)
|
|
116
|
+
task.fields.control.waitingFor = waitingFor;
|
|
249
117
|
});
|
|
250
118
|
}
|
|
251
119
|
/**
|
|
@@ -270,7 +138,7 @@ export function nextCycleId(previousCycleId, occurrence) {
|
|
|
270
138
|
export async function openRecurringTaskCycle(channelDir, id, now, force = false) {
|
|
271
139
|
let cycleId;
|
|
272
140
|
const document = await updateStoredTask(channelDir, id, (task) => {
|
|
273
|
-
const status = normalizeStoredStatus(task.fields.status
|
|
141
|
+
const status = normalizeStoredStatus(task.fields.status);
|
|
274
142
|
if (status !== "sleeping" || !task.fields.schedule)
|
|
275
143
|
return;
|
|
276
144
|
const wakeMs = task.fields.wake ? parseLocalTime(task.fields.wake) : undefined;
|
|
@@ -4,10 +4,9 @@ export type TaskStatus = (typeof TASK_STATUSES)[number];
|
|
|
4
4
|
/** Statuses a model/user may set directly; recurring sleeping is runtime-owned but repairable. */
|
|
5
5
|
export declare const SETTABLE_TASK_STATUSES: readonly ["active", "waiting", "sleeping"];
|
|
6
6
|
export type SettableTaskStatus = (typeof SETTABLE_TASK_STATUSES)[number];
|
|
7
|
-
export type TaskLifecycleAction = "create" | "progress" | "
|
|
8
|
-
/**
|
|
9
|
-
export declare function normalizeStoredStatus(raw: string | undefined
|
|
10
|
-
export declare function wasLegacyEscalated(raw: string | undefined): boolean;
|
|
7
|
+
export type TaskLifecycleAction = "create" | "progress" | "verify" | "complete" | "skip" | "cancel" | "set" | "pause" | "resume" | "run" | "governor-stop" | "wait-due" | "cycle-due";
|
|
8
|
+
/** Canonicalize a stored status value, fail-open to `active` for anything not in the current vocabulary. */
|
|
9
|
+
export declare function normalizeStoredStatus(raw: string | undefined): TaskStatus;
|
|
11
10
|
export declare function isSettableTaskStatus(value: string): value is SettableTaskStatus;
|
|
12
11
|
/** Assert an action is legal and resolve caller-supplied target status. */
|
|
13
12
|
export declare function resolveTaskTransition(action: TaskLifecycleAction, id: string, from: TaskStatus, requestedStatus?: string): TaskStatus;
|
|
@@ -8,8 +8,11 @@ const WORKABLE = ["active", "waiting"];
|
|
|
8
8
|
const TRANSITIONS = {
|
|
9
9
|
create: { from: [], to: "active" },
|
|
10
10
|
progress: { from: WORKABLE, to: "caller" },
|
|
11
|
-
|
|
12
|
-
verify
|
|
11
|
+
// Not gated on a specific waitingFor value: the wake that reactivates a task parked for a
|
|
12
|
+
// purpose=verify sub-agent already flips it to active before this runs (the same as any other
|
|
13
|
+
// delegation wake), but `verify` also accepts waiting so it is not sensitive to exactly when
|
|
14
|
+
// that reactivation lands relative to the model's own turn.
|
|
15
|
+
verify: { from: WORKABLE, to: "active" },
|
|
13
16
|
complete: { from: ["active"], to: "caller" },
|
|
14
17
|
skip: { from: WORKABLE, to: "sleeping" },
|
|
15
18
|
cancel: { from: LIVE, to: "caller" },
|
|
@@ -21,31 +24,9 @@ const TRANSITIONS = {
|
|
|
21
24
|
"wait-due": { from: ["waiting"], to: "active" },
|
|
22
25
|
"cycle-due": { from: ["sleeping"], to: "active" },
|
|
23
26
|
};
|
|
24
|
-
/**
|
|
25
|
-
export function normalizeStoredStatus(raw
|
|
26
|
-
|
|
27
|
-
case undefined:
|
|
28
|
-
case "":
|
|
29
|
-
case "open":
|
|
30
|
-
case "in-progress":
|
|
31
|
-
case "verifying":
|
|
32
|
-
return "active";
|
|
33
|
-
case "awaiting-user":
|
|
34
|
-
case "blocked":
|
|
35
|
-
return "waiting";
|
|
36
|
-
case "paused":
|
|
37
|
-
case "escalated":
|
|
38
|
-
return "active";
|
|
39
|
-
case "done":
|
|
40
|
-
return recurring ? "sleeping" : "active";
|
|
41
|
-
case "cancelled":
|
|
42
|
-
return "active";
|
|
43
|
-
default:
|
|
44
|
-
return TASK_STATUSES.includes(raw) ? raw : "active";
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
export function wasLegacyEscalated(raw) {
|
|
48
|
-
return raw === "escalated";
|
|
27
|
+
/** Canonicalize a stored status value, fail-open to `active` for anything not in the current vocabulary. */
|
|
28
|
+
export function normalizeStoredStatus(raw) {
|
|
29
|
+
return raw !== undefined && TASK_STATUSES.includes(raw) ? raw : "active";
|
|
49
30
|
}
|
|
50
31
|
export function isSettableTaskStatus(value) {
|
|
51
32
|
return SETTABLE_TASK_STATUSES.includes(value);
|
package/dist/tools/index.d.ts
CHANGED
|
@@ -27,7 +27,5 @@ export interface CreatePipiclawToolsOptions {
|
|
|
27
27
|
toolsConfig?: PipiclawToolsConfig;
|
|
28
28
|
/** Transport-provided attachment port; when present, enables the `send_media` tool. */
|
|
29
29
|
mediaSender?: MediaSender;
|
|
30
|
-
/** Runtime-provided durable verifier dispatch. */
|
|
31
|
-
dispatchVerification?: (taskId: string) => Promise<boolean>;
|
|
32
30
|
}
|
|
33
31
|
export declare function createPipiclawTools(options: CreatePipiclawToolsOptions): AgentTool<any>[];
|
package/dist/tools/index.js
CHANGED
|
@@ -38,7 +38,6 @@ export function createPipiclawTools(options) {
|
|
|
38
38
|
getSessionSearchSettings: options.getSessionSearchSettings,
|
|
39
39
|
memoryCandidateStore: options.memoryCandidateStore,
|
|
40
40
|
mediaSender: options.mediaSender,
|
|
41
|
-
dispatchVerification: options.dispatchVerification,
|
|
42
41
|
});
|
|
43
42
|
return [
|
|
44
43
|
...leafTools,
|
|
@@ -72,6 +71,8 @@ export function createPipiclawTools(options) {
|
|
|
72
71
|
channelDir: options.channelDir,
|
|
73
72
|
getSubAgentDiscovery: options.getSubAgentDiscovery,
|
|
74
73
|
workspaceDir: options.workspaceDir,
|
|
74
|
+
workingDirectory: options.projectScope.projectRoot,
|
|
75
|
+
projectBoundary: options.projectScope.boundary,
|
|
75
76
|
securityConfig,
|
|
76
77
|
// Spec 042 D7: follow_up builds its envelope through the same buildContextualBlocks
|
|
77
78
|
// the initial dispatch uses, which needs these to resolve a memory: relevant recall.
|
package/dist/tools/registry.d.ts
CHANGED
|
@@ -47,7 +47,6 @@ export interface ToolBuildContext {
|
|
|
47
47
|
* files. Gates the `send_media` tool; sub-agents never receive it.
|
|
48
48
|
*/
|
|
49
49
|
mediaSender?: MediaSender;
|
|
50
|
-
dispatchVerification?: (taskId: string) => Promise<boolean>;
|
|
51
50
|
}
|
|
52
51
|
export interface ToolRegistration {
|
|
53
52
|
/** Also the tool's `details.kind`: the name is the authoritative discriminator. */
|
package/dist/tools/registry.js
CHANGED
|
@@ -7,7 +7,7 @@ import type { PipiclawMemoryRecallSettings } from "../settings.js";
|
|
|
7
7
|
import { type SubAgentDiscoveryResult } from "../subagents/discovery.js";
|
|
8
8
|
declare const subagentManageSchema: Type.TObject<{
|
|
9
9
|
label: Type.TString;
|
|
10
|
-
op: Type.TUnion<[Type.TLiteral<"list">, Type.TLiteral<"cancel">, Type.TLiteral<"follow_up">]>;
|
|
10
|
+
op: Type.TUnion<[Type.TLiteral<"list">, Type.TLiteral<"cancel">, Type.TLiteral<"follow_up">, Type.TLiteral<"show">]>;
|
|
11
11
|
runId: Type.TOptional<Type.TString>;
|
|
12
12
|
task: Type.TOptional<Type.TString>;
|
|
13
13
|
}>;
|
|
@@ -23,6 +23,13 @@ export interface SubAgentManageToolOptions {
|
|
|
23
23
|
*/
|
|
24
24
|
workspaceDir: string;
|
|
25
25
|
channelDir: string;
|
|
26
|
+
/**
|
|
27
|
+
* The current project scope, so `follow_up` can re-check a resumed run's `workingDirectory`
|
|
28
|
+
* against the boundary in effect *now* rather than trusting the one recorded at dispatch time
|
|
29
|
+
* (review 2026-08-23 §3.1) — `/project` can move `workingDirectory` between the two.
|
|
30
|
+
*/
|
|
31
|
+
workingDirectory?: string;
|
|
32
|
+
projectBoundary?: "project" | "unbounded";
|
|
26
33
|
/** Needed only for `follow_up`: re-resolves the role's current config by name. */
|
|
27
34
|
getSubAgentDiscovery?: () => SubAgentDiscoveryResult;
|
|
28
35
|
securityConfig?: SecurityConfig;
|
|
@@ -1,24 +1,29 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
1
2
|
import { dirname, join } from "node:path";
|
|
2
3
|
import { Type } from "typebox";
|
|
3
4
|
import { DEFAULT_SECURITY_CONFIG } from "../security/config.js";
|
|
4
5
|
import { RecoverableToolError } from "../shared/recoverable-error.js";
|
|
5
|
-
import { externalRoleFingerprint } from "../subagents/discovery.js";
|
|
6
|
+
import { externalRoleFingerprint, validateSubAgentTask } from "../subagents/discovery.js";
|
|
6
7
|
import { launchExternalRun } from "../subagents/external/run.js";
|
|
7
8
|
import { formatCost, formatRunDuration, harnessLabel } from "../subagents/format.js";
|
|
8
9
|
import { getSubAgentRunManager } from "../subagents/runs.js";
|
|
9
|
-
import { assertVerifyAdmissible, buildContextualBlocks, buildSubAgentTask, } from "../subagents/tool.js";
|
|
10
|
+
import { assertVerifyAdmissible, assertWithinProjectBoundary, buildContextualBlocks, buildSubAgentTask, } from "../subagents/tool.js";
|
|
10
11
|
import { acquireWorkspaceLease, formatWorkspaceLeaseConflict, releaseWorkspaceLease, } from "../subagents/workspace-lease.js";
|
|
11
12
|
const subagentManageSchema = Type.Object({
|
|
12
13
|
label: Type.String({ description: "Brief description of what you're checking (shown to user)" }),
|
|
13
|
-
op: Type.Union([Type.Literal("list"), Type.Literal("cancel"), Type.Literal("follow_up")], {
|
|
14
|
-
description: '"list" a snapshot of this channel\'s delegation runs, "cancel" a running one by runId,
|
|
15
|
-
'"follow_up" to continue a resumable run with a new instruction (produces a new runId)
|
|
14
|
+
op: Type.Union([Type.Literal("list"), Type.Literal("cancel"), Type.Literal("follow_up"), Type.Literal("show")], {
|
|
15
|
+
description: '"list" a snapshot of this channel\'s delegation runs, "cancel" a running one by runId, ' +
|
|
16
|
+
'"follow_up" to continue a resumable run with a new instruction (produces a new runId), or ' +
|
|
17
|
+
'"show" the full detail on one run by runId (argv, dispatch warnings, stderr tail) for self-diagnosis of a failed run.',
|
|
16
18
|
}),
|
|
17
|
-
runId: Type.Optional(Type.String({ description: "Required for cancel and
|
|
19
|
+
runId: Type.Optional(Type.String({ description: "Required for cancel, follow_up, and show." })),
|
|
18
20
|
task: Type.Optional(Type.String({ description: "Follow-up instruction. Required for op=follow_up." })),
|
|
19
21
|
});
|
|
20
22
|
/** Spec 042 D9: cap on `op=list`'s output — see the call site for why running runs are exempt. */
|
|
21
23
|
const LIST_CAP = 50;
|
|
24
|
+
/** Matches `/subagents show`'s own cap (`runtime/subagent-commands.ts`) — enough to diagnose a
|
|
25
|
+
* failure without dumping an unbounded log into the model's context. */
|
|
26
|
+
const STDERR_TAIL_CHARS = 2_000;
|
|
22
27
|
function formatRunLine(record) {
|
|
23
28
|
const task = record.taskId ? `, task ${record.taskId}` : "";
|
|
24
29
|
const lease = record.leaseKey ? ", lease held" : "";
|
|
@@ -30,6 +35,53 @@ function formatRunLine(record) {
|
|
|
30
35
|
}
|
|
31
36
|
return header;
|
|
32
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* The machine-readable subset of `/subagents show` (`runtime/subagent-commands.ts`'s `showRun`):
|
|
40
|
+
* argv, dispatch-time warnings, and the adapter/CLI version pair the model previously had no way
|
|
41
|
+
* to see at all — it could only guess at a failed external run's cause or re-dispatch blindly
|
|
42
|
+
* (review 2026-08-23 §3.4).
|
|
43
|
+
*/
|
|
44
|
+
async function formatRunShow(record) {
|
|
45
|
+
const lines = [
|
|
46
|
+
`Run ${record.runId}: ${record.agent} (${harnessLabel(record)}, ${record.source}) — ${record.status} (${formatRunDuration(record)})`,
|
|
47
|
+
`purpose=${record.purpose}, workingDirectory=${record.workingDirectory}, artifactDir=${record.artifactDir}`,
|
|
48
|
+
];
|
|
49
|
+
if (record.failureReason)
|
|
50
|
+
lines.push(`failureReason: ${record.failureReason}`);
|
|
51
|
+
if (record.verificationVerdict) {
|
|
52
|
+
lines.push(`verification: ${record.verificationVerdict.toUpperCase()}${record.verificationStrength === "advisory" ? " (advisory)" : ""}`);
|
|
53
|
+
}
|
|
54
|
+
if (record.taskId)
|
|
55
|
+
lines.push(`taskId: ${record.taskId}`);
|
|
56
|
+
if (record.leaseKey)
|
|
57
|
+
lines.push("holds write lease: yes");
|
|
58
|
+
if (record.model)
|
|
59
|
+
lines.push(`model: ${record.model}`);
|
|
60
|
+
const cost = formatCost(record);
|
|
61
|
+
lines.push(`usage: input=${record.usage.input} output=${record.usage.output}${cost ? ` cost=${cost}` : ""}`);
|
|
62
|
+
if (record.argv)
|
|
63
|
+
lines.push(`argv: ${JSON.stringify(record.argv)}`);
|
|
64
|
+
if (record.parserVersion !== undefined || record.cliVersion) {
|
|
65
|
+
const parts = [];
|
|
66
|
+
if (record.parserVersion !== undefined)
|
|
67
|
+
parts.push(`parserVersion=${record.parserVersion}`);
|
|
68
|
+
parts.push(`cliVersion=${record.cliVersion ?? "(unknown)"}`);
|
|
69
|
+
lines.push(`adapter/CLI: ${parts.join(", ")}`);
|
|
70
|
+
}
|
|
71
|
+
if (record.invocationWarnings?.length) {
|
|
72
|
+
lines.push("dispatch warnings:", ...record.invocationWarnings.map((warning) => `- ${warning}`));
|
|
73
|
+
}
|
|
74
|
+
if (record.sessionId)
|
|
75
|
+
lines.push(`sessionId: ${record.sessionId} (usable with subagent_manage op=follow_up)`);
|
|
76
|
+
if (record.runtime === "external") {
|
|
77
|
+
const stderrTail = await readFile(join(record.artifactDir, "stderr.log"), "utf-8")
|
|
78
|
+
.then((text) => text.slice(-STDERR_TAIL_CHARS))
|
|
79
|
+
.catch(() => undefined);
|
|
80
|
+
if (stderrTail?.trim())
|
|
81
|
+
lines.push("", "stderr (tail):", stderrTail);
|
|
82
|
+
}
|
|
83
|
+
return lines.join("\n");
|
|
84
|
+
}
|
|
33
85
|
/** Resumable harnesses only: `claude-code`/`codex-cli`. `exec` has no session concept, and internal
|
|
34
86
|
* runs have no persisted transcript by design (spec 040, non-goals). */
|
|
35
87
|
function resumableHarnessOf(record) {
|
|
@@ -45,7 +97,8 @@ export function createSubAgentManageTool(options) {
|
|
|
45
97
|
description: "Inspect and control delegation runs (internal sub-agents and, once configured, external agents): " +
|
|
46
98
|
"op=list shows a snapshot of this channel's runs; op=cancel stops a running one by runId without " +
|
|
47
99
|
"waking the channel (that is your own decision, not a failure); op=follow_up continues a resumable " +
|
|
48
|
-
"run with a new instruction, producing a new runId
|
|
100
|
+
"run with a new instruction, producing a new runId; op=show gives full detail on one run (argv, " +
|
|
101
|
+
"dispatch warnings, stderr tail) for self-diagnosis. A run finishing wakes this channel by itself, " +
|
|
49
102
|
"so never poll here waiting for one — end the turn instead.",
|
|
50
103
|
parameters: subagentManageSchema,
|
|
51
104
|
execute: async (_toolCallId, { op, runId, task }) => {
|
|
@@ -81,6 +134,13 @@ export function createSubAgentManageTool(options) {
|
|
|
81
134
|
throw new RecoverableToolError(`Run "${runId}" was not found on this channel.`);
|
|
82
135
|
}
|
|
83
136
|
const resolvedRunId = resolution.record.runId;
|
|
137
|
+
if (op === "show") {
|
|
138
|
+
const text = await formatRunShow(resolution.record);
|
|
139
|
+
return {
|
|
140
|
+
content: [{ type: "text", text }],
|
|
141
|
+
details: { op, runId: resolvedRunId },
|
|
142
|
+
};
|
|
143
|
+
}
|
|
84
144
|
if (op === "cancel") {
|
|
85
145
|
const status = await manager().cancel(resolvedRunId);
|
|
86
146
|
return {
|
|
@@ -92,7 +152,17 @@ export function createSubAgentManageTool(options) {
|
|
|
92
152
|
if (!task || !task.trim()) {
|
|
93
153
|
throw new RecoverableToolError("follow_up requires task.");
|
|
94
154
|
}
|
|
155
|
+
const taskLengthError = validateSubAgentTask(task);
|
|
156
|
+
if (taskLengthError) {
|
|
157
|
+
throw new RecoverableToolError(taskLengthError);
|
|
158
|
+
}
|
|
95
159
|
const record = resolution.record;
|
|
160
|
+
// The initial dispatch validated `record.workingDirectory` against the project boundary
|
|
161
|
+
// in effect at the time (spec 043, D6.2); `/project` can move that boundary afterward, and
|
|
162
|
+
// this re-checks the *current* one before resuming a process there (review 2026-08-23 §3.1).
|
|
163
|
+
if (options.projectBoundary === "project" && options.workingDirectory) {
|
|
164
|
+
assertWithinProjectBoundary(record.workingDirectory, options.workingDirectory, record.workingDirectory);
|
|
165
|
+
}
|
|
96
166
|
const harness = resumableHarnessOf(record);
|
|
97
167
|
if (!harness) {
|
|
98
168
|
// Internal runs have no persisted transcript to resume — the isolated-context design
|
|
@@ -15,7 +15,7 @@ export async function setTask(options, request) {
|
|
|
15
15
|
const id = normalizeTaskId(request.id);
|
|
16
16
|
const taskPath = join(tasksDir(options), `${id}.md`);
|
|
17
17
|
const { fields, body } = await readTaskDocument(taskPath, id, request.control !== undefined);
|
|
18
|
-
const fromStatus = normalizeStoredStatus(fields.status
|
|
18
|
+
const fromStatus = normalizeStoredStatus(fields.status);
|
|
19
19
|
resolveTaskTransition("set", id, fromStatus, request.status);
|
|
20
20
|
const nextFields = applySet(fields, request);
|
|
21
21
|
if (nextFields.status === "sleeping" && !nextFields.schedule) {
|
|
@@ -37,7 +37,7 @@ export async function progressTask(options, request) {
|
|
|
37
37
|
const note = requiredField(request.note, "note", "progress");
|
|
38
38
|
const taskPath = join(tasksDir(options), `${id}.md`);
|
|
39
39
|
const { fields, body } = await readTaskDocument(taskPath, id);
|
|
40
|
-
const from = normalizeStoredStatus(fields.status
|
|
40
|
+
const from = normalizeStoredStatus(fields.status);
|
|
41
41
|
resolveTaskTransition("progress", id, from, request.status);
|
|
42
42
|
if (from === "sleeping") {
|
|
43
43
|
throw new RecoverableToolError(`Task "${id}" is sleeping; wait for its occurrence or use /tasks run ${id}.`);
|
|
@@ -49,7 +49,6 @@ export async function progressTask(options, request) {
|
|
|
49
49
|
if (nextFields.control) {
|
|
50
50
|
if (nextFields.status === "active") {
|
|
51
51
|
nextFields.control.waitingFor = undefined;
|
|
52
|
-
nextFields.control.blockedReason = undefined;
|
|
53
52
|
}
|
|
54
53
|
else if (!nextFields.control.waitingFor) {
|
|
55
54
|
nextFields.control.waitingFor = nextFields.wake ? "time" : "external-signal";
|
|
@@ -75,22 +74,20 @@ export async function completeTask(options, request) {
|
|
|
75
74
|
const dir = tasksDir(options);
|
|
76
75
|
const taskPath = join(dir, `${id}.md`);
|
|
77
76
|
const { fields, body } = await readTaskDocument(taskPath, id);
|
|
78
|
-
resolveTaskTransition("complete", id, normalizeStoredStatus(fields.status
|
|
77
|
+
resolveTaskTransition("complete", id, normalizeStoredStatus(fields.status));
|
|
79
78
|
const uncheckedAcceptance = uncheckedTaskAcceptanceItems(body);
|
|
80
79
|
if (uncheckedAcceptance.length > 0) {
|
|
81
80
|
throw new RecoverableToolError(`Task "${id}" still has unchecked acceptance items: ${uncheckedAcceptance.join("; ")}. Check them with evidence before complete.`);
|
|
82
81
|
}
|
|
82
|
+
let verificationNote = "";
|
|
83
83
|
if (fields.control?.verification.required) {
|
|
84
84
|
const verification = fields.control.verification;
|
|
85
|
-
if (verification.status !== "passed" || !verification.
|
|
86
|
-
throw new RecoverableToolError(`Task "${id}" requires an independent PASS.
|
|
85
|
+
if (verification.status !== "passed" || !verification.runId) {
|
|
86
|
+
throw new RecoverableToolError(`Task "${id}" requires an independent PASS. Dispatch a purpose=verify sub-agent, then task_manage verify with its run id.`);
|
|
87
87
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const attestation = await assertVerificationAttestationMatches(options.channelDir, id, verification);
|
|
92
|
-
// The attestation is the durable source of the artifact subject. The mirrored control
|
|
93
|
-
// field is writable task metadata, so it must not decide whether freshness is checked.
|
|
88
|
+
// The attestation file is the sole authority — for the verdict, the body-hash freshness
|
|
89
|
+
// check, and the artifact subject. The task Markdown is agent-writable and proves nothing.
|
|
90
|
+
const attestation = await assertVerificationAttestationMatches(options.channelDir, id, verification.runId, taskBodyHash(body));
|
|
94
91
|
if (attestation.subjectHash) {
|
|
95
92
|
const subjectDir = attestation.subjectDir ?? options.workingDirectory ?? process.cwd();
|
|
96
93
|
const currentSubject = await workspaceSubjectHash(subjectDir);
|
|
@@ -101,10 +98,16 @@ export async function completeTask(options, request) {
|
|
|
101
98
|
throw new RecoverableToolError(`Task "${id}" artifacts changed after its independent PASS; request verification again.`);
|
|
102
99
|
}
|
|
103
100
|
}
|
|
101
|
+
// advisory means runtime could not structurally prove the verifier left artifacts alone
|
|
102
|
+
// (external harness, or bash left in an internal verifier's tool set) — surface it at the
|
|
103
|
+
// one moment completion is actually decided, not just in a file nobody re-reads (review
|
|
104
|
+
// 2026-08-23 §2.1).
|
|
105
|
+
if (attestation.verificationStrength === "advisory") {
|
|
106
|
+
verificationNote = " ⚠️ 验收结论为 advisory:runtime 无法证明验收者未改动产物,请自行抽查 diff 与测试结果。";
|
|
107
|
+
}
|
|
104
108
|
}
|
|
105
109
|
const bodyWithEvidence = appendCompletionEvidence(body, request);
|
|
106
110
|
if (fields.control) {
|
|
107
|
-
fields.control.blockedReason = undefined;
|
|
108
111
|
fields.control.waitingFor = undefined;
|
|
109
112
|
}
|
|
110
113
|
const recurring = Boolean(fields.schedule);
|
|
@@ -143,7 +146,7 @@ export async function completeTask(options, request) {
|
|
|
143
146
|
status: recurring ? "sleeping" : undefined,
|
|
144
147
|
archived,
|
|
145
148
|
deletedEvents: deleted,
|
|
146
|
-
notice: `任务 \`${id}\` 已完成(${archived ? "已归档" : "已进入 sleeping,等待下一 occurrence"}${cleanup}
|
|
149
|
+
notice: `任务 \`${id}\` 已完成(${archived ? "已归档" : "已进入 sleeping,等待下一 occurrence"}${cleanup})。${verificationNote}`,
|
|
147
150
|
};
|
|
148
151
|
}
|
|
149
152
|
export async function skipTask(options, request) {
|
|
@@ -153,13 +156,12 @@ export async function skipTask(options, request) {
|
|
|
153
156
|
const reason = requiredField(request.reason, "reason", "skip");
|
|
154
157
|
const taskPath = join(tasksDir(options), `${id}.md`);
|
|
155
158
|
const { fields, body } = await readTaskDocument(taskPath, id);
|
|
156
|
-
resolveTaskTransition("skip", id, normalizeStoredStatus(fields.status
|
|
159
|
+
resolveTaskTransition("skip", id, normalizeStoredStatus(fields.status));
|
|
157
160
|
if (!fields.schedule) {
|
|
158
161
|
throw new RecoverableToolError(`Task "${id}" is not recurring; use complete after satisfying its DoD or cancel it.`);
|
|
159
162
|
}
|
|
160
163
|
const skippedBody = appendCurrentCycleNote(body, `Skipped: ${reason}`);
|
|
161
164
|
if (fields.control) {
|
|
162
|
-
fields.control.blockedReason = undefined;
|
|
163
165
|
fields.control.waitingFor = undefined;
|
|
164
166
|
fields.control.verification = { required: fields.control.verification.required, status: "pending" };
|
|
165
167
|
}
|
|
@@ -183,7 +185,7 @@ export async function cancelTask(options, request) {
|
|
|
183
185
|
const dir = tasksDir(options);
|
|
184
186
|
const taskPath = join(dir, `${id}.md`);
|
|
185
187
|
const { fields, body } = await readTaskDocument(taskPath, id);
|
|
186
|
-
resolveTaskTransition("cancel", id, normalizeStoredStatus(fields.status
|
|
188
|
+
resolveTaskTransition("cancel", id, normalizeStoredStatus(fields.status));
|
|
187
189
|
const cancelledBody = `${body.replace(/\n+$/, "\n")}\n## Cancellation\n\n- Reason: ${markdownValue(reason)}\n`;
|
|
188
190
|
const archiveDir = join(dir, "archive");
|
|
189
191
|
await mkdir(archiveDir, { recursive: true });
|
|
@@ -3,7 +3,7 @@ import type { TaskManageAction } from "./types.js";
|
|
|
3
3
|
export declare const SETTABLE_STATUSES: readonly ["active", "waiting", "sleeping"];
|
|
4
4
|
export declare const taskManageSchema: Type.TObject<{
|
|
5
5
|
label: Type.TString;
|
|
6
|
-
action: Type.TUnion<[Type.TLiteral<"create">, Type.TLiteral<"progress">, Type.TLiteral<"set">, Type.TLiteral<"
|
|
6
|
+
action: Type.TUnion<[Type.TLiteral<"create">, Type.TLiteral<"progress">, Type.TLiteral<"set">, Type.TLiteral<"verify">, Type.TLiteral<"complete">, Type.TLiteral<"skip">, Type.TLiteral<"cancel">, Type.TLiteral<"list">]>;
|
|
7
7
|
id: Type.TOptional<Type.TString>;
|
|
8
8
|
title: Type.TOptional<Type.TString>;
|
|
9
9
|
goal: Type.TOptional<Type.TString>;
|
|
@@ -17,18 +17,14 @@ export declare const taskManageSchema: Type.TObject<{
|
|
|
17
17
|
text: Type.TOptional<Type.TString>;
|
|
18
18
|
}>>>;
|
|
19
19
|
control: Type.TOptional<Type.TObject<{
|
|
20
|
-
priority: Type.TOptional<Type.TUnion<[Type.TLiteral<"low">, Type.TLiteral<"normal">, Type.TLiteral<"high">, Type.TLiteral<"critical">]>>;
|
|
21
20
|
deadline: Type.TOptional<Type.TString>;
|
|
22
21
|
nextAction: Type.TOptional<Type.TString>;
|
|
23
|
-
|
|
24
|
-
waitingFor: Type.TOptional<Type.TUnion<[Type.TLiteral<"time">, Type.TLiteral<"user">, Type.TLiteral<"job">, Type.TLiteral<"verification">, Type.TLiteral<"external-signal">]>>;
|
|
25
|
-
maxAttempts: Type.TOptional<Type.TInteger>;
|
|
22
|
+
waitingFor: Type.TOptional<Type.TUnion<[Type.TLiteral<"time">, Type.TLiteral<"user">, Type.TLiteral<"job">, Type.TLiteral<"external-signal">]>>;
|
|
26
23
|
verificationRequired: Type.TOptional<Type.TBoolean>;
|
|
27
24
|
}>>;
|
|
28
25
|
status: Type.TOptional<Type.TUnion<Type.TLiteral<"active" | "waiting" | "sleeping">[]>>;
|
|
29
26
|
wake: Type.TOptional<Type.TString>;
|
|
30
27
|
schedule: Type.TOptional<Type.TString>;
|
|
31
|
-
recurrence: Type.TOptional<Type.TString>;
|
|
32
28
|
note: Type.TOptional<Type.TString>;
|
|
33
29
|
verifierRunId: Type.TOptional<Type.TString>;
|
|
34
30
|
summary: Type.TOptional<Type.TString>;
|