@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
package/dist/tasks/control.js
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
import { formatLocalTime, parseLocalTime } from "../shared/local-time.js";
|
|
2
2
|
import { errorMessage } from "../shared/text-utils.js";
|
|
3
|
-
|
|
4
|
-
const WAITING_FOR = ["time", "user", "job", "verification", "external-signal"];
|
|
3
|
+
const WAITING_FOR = ["time", "user", "job", "external-signal"];
|
|
5
4
|
const VERIFICATION_STATUSES = ["pending", "passed", "failed"];
|
|
6
|
-
const OUTCOMES = ["pending", "running", "progress", "blocked", "failed"];
|
|
7
|
-
function normalizeLegacyOutcome(value) {
|
|
8
|
-
return value === "verified" || value === "skipped" ? "progress" : value;
|
|
9
|
-
}
|
|
10
5
|
function isRecord(value) {
|
|
11
6
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
12
7
|
}
|
|
@@ -16,38 +11,6 @@ function optionalString(value) {
|
|
|
16
11
|
const trimmed = value.trim();
|
|
17
12
|
return trimmed || undefined;
|
|
18
13
|
}
|
|
19
|
-
function finiteNonNegative(value, fallback = 0) {
|
|
20
|
-
if (value === undefined)
|
|
21
|
-
return fallback;
|
|
22
|
-
if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
|
|
23
|
-
throw new Error("control usage values must be finite non-negative numbers");
|
|
24
|
-
}
|
|
25
|
-
return value;
|
|
26
|
-
}
|
|
27
|
-
function nonNegativeInteger(value, fallback = 0) {
|
|
28
|
-
if (value === undefined)
|
|
29
|
-
return fallback;
|
|
30
|
-
if (typeof value !== "number" || !Number.isInteger(value) || value < 0) {
|
|
31
|
-
throw new Error("control.attemptGeneration must be a non-negative integer; remove it to rebuild from 0.");
|
|
32
|
-
}
|
|
33
|
-
return value;
|
|
34
|
-
}
|
|
35
|
-
function parseTaskUsage(value) {
|
|
36
|
-
const attempts = Math.floor(finiteNonNegative(value.attempts));
|
|
37
|
-
const tokens = Math.floor(finiteNonNegative(value.tokens));
|
|
38
|
-
const costUsd = finiteNonNegative(value.costUsd);
|
|
39
|
-
const wallTimeMinutes = finiteNonNegative(value.wallTimeMinutes);
|
|
40
|
-
const costKnown = typeof value.costKnown === "boolean" ? value.costKnown : costUsd > 0 || (tokens === 0 && wallTimeMinutes === 0);
|
|
41
|
-
return { attempts, tokens, costUsd, costKnown, wallTimeMinutes };
|
|
42
|
-
}
|
|
43
|
-
function optionalPositive(value) {
|
|
44
|
-
if (value === undefined)
|
|
45
|
-
return undefined;
|
|
46
|
-
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
|
|
47
|
-
throw new Error("control budget values must be finite positive numbers");
|
|
48
|
-
}
|
|
49
|
-
return value;
|
|
50
|
-
}
|
|
51
14
|
function enumValue(value, values, fallback) {
|
|
52
15
|
if (value === undefined)
|
|
53
16
|
return fallback;
|
|
@@ -56,12 +19,6 @@ function enumValue(value, values, fallback) {
|
|
|
56
19
|
}
|
|
57
20
|
return value;
|
|
58
21
|
}
|
|
59
|
-
function parseVerificationRequired(verification) {
|
|
60
|
-
if (typeof verification.required === "boolean")
|
|
61
|
-
return verification.required;
|
|
62
|
-
// v1's independent mode was the only mode that represented a real checker gate.
|
|
63
|
-
return verification.mode === "independent";
|
|
64
|
-
}
|
|
65
22
|
function parseStop(value) {
|
|
66
23
|
if (value === undefined)
|
|
67
24
|
return undefined;
|
|
@@ -75,92 +32,50 @@ function parseStop(value) {
|
|
|
75
32
|
}
|
|
76
33
|
return { by, reason, at: formatLocalTime(new Date(parseLocalTime(at))) };
|
|
77
34
|
}
|
|
78
|
-
function
|
|
79
|
-
if (value === undefined)
|
|
80
|
-
return undefined;
|
|
81
|
-
if (!isRecord(value))
|
|
82
|
-
throw new Error("control.provenance must be an object");
|
|
83
|
-
const createdBy = optionalString(value.createdBy);
|
|
84
|
-
const createdAt = optionalString(value.createdAt);
|
|
85
|
-
const sourceMessageId = optionalString(value.sourceMessageId);
|
|
86
|
-
if (createdAt && parseLocalTime(createdAt) === undefined) {
|
|
87
|
-
throw new Error("control.provenance.createdAt must be a valid local time");
|
|
88
|
-
}
|
|
89
|
-
return { createdBy, createdAt, sourceMessageId };
|
|
90
|
-
}
|
|
91
|
-
function parseWakeHandoff(value) {
|
|
92
|
-
if (value === undefined)
|
|
93
|
-
return undefined;
|
|
94
|
-
if (!isRecord(value))
|
|
95
|
-
throw new Error("control.wakeHandoff must be an object");
|
|
96
|
-
if (value.kind !== "subagent" || typeof value.resourceId !== "string" || typeof value.dispatchId !== "string") {
|
|
97
|
-
throw new Error("control.wakeHandoff requires kind=subagent, resourceId, and dispatchId");
|
|
98
|
-
}
|
|
35
|
+
export function createDefaultTaskControl(requiresVerification = false) {
|
|
99
36
|
return {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
dispatchId: value.dispatchId,
|
|
103
|
-
generation: nonNegativeInteger(value.generation),
|
|
104
|
-
previousLastOutcome: enumValue(normalizeLegacyOutcome(value.previousLastOutcome), OUTCOMES, "pending"),
|
|
105
|
-
previousBlockedReason: optionalString(value.previousBlockedReason),
|
|
106
|
-
previousLastStartedAt: optionalString(value.previousLastStartedAt),
|
|
37
|
+
version: 3,
|
|
38
|
+
verification: { required: requiresVerification, status: "pending" },
|
|
107
39
|
};
|
|
108
40
|
}
|
|
109
|
-
/**
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
"budget.maxWallTimeMinutes",
|
|
128
|
-
];
|
|
129
|
-
/** Retired keys actually present in a raw stored control block. */
|
|
130
|
-
export function retiredTaskControlKeys(raw) {
|
|
131
|
-
if (!isRecord(raw))
|
|
132
|
-
return [];
|
|
133
|
-
return RETIRED_TASK_CONTROL_KEYS.filter((key) => {
|
|
134
|
-
const [head, nested] = key.split(".");
|
|
135
|
-
if (!nested)
|
|
136
|
-
return Object.hasOwn(raw, head);
|
|
137
|
-
const parent = raw[head];
|
|
138
|
-
return isRecord(parent) && Object.hasOwn(parent, nested);
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
export function createDefaultTaskControl(requiresVerification = false, provenance) {
|
|
41
|
+
/** Read the v3 control contract only. Anything else is a read-time failure — see `parseLegacyTaskControl`. */
|
|
42
|
+
export function parseTaskControl(raw) {
|
|
43
|
+
let value;
|
|
44
|
+
try {
|
|
45
|
+
value = JSON.parse(raw);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
throw new Error(`control is not valid JSON: ${errorMessage(error)}`);
|
|
49
|
+
}
|
|
50
|
+
if (!isRecord(value) || value.version !== 3) {
|
|
51
|
+
throw new Error("control must be a version 3 JSON object; repair it with task_manage set.");
|
|
52
|
+
}
|
|
53
|
+
const verification = isRecord(value.verification) ? value.verification : {};
|
|
54
|
+
const deadline = optionalString(value.deadline);
|
|
55
|
+
if (deadline && parseLocalTime(deadline) === undefined) {
|
|
56
|
+
throw new Error("control.deadline must be a valid local time");
|
|
57
|
+
}
|
|
58
|
+
const waitingFor = value.waitingFor === undefined ? undefined : enumValue(value.waitingFor, WAITING_FOR, "external-signal");
|
|
142
59
|
return {
|
|
143
|
-
version:
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
wallTimeMinutes: 0,
|
|
60
|
+
version: 3,
|
|
61
|
+
deadline,
|
|
62
|
+
nextAction: optionalString(value.nextAction),
|
|
63
|
+
waitingFor,
|
|
64
|
+
verification: {
|
|
65
|
+
required: typeof verification.required === "boolean" ? verification.required : false,
|
|
66
|
+
status: enumValue(verification.status, VERIFICATION_STATUSES, "pending"),
|
|
67
|
+
runId: optionalString(verification.runId),
|
|
152
68
|
},
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
lastOutcome: "pending",
|
|
156
|
-
...(provenance ? { provenance } : {}),
|
|
69
|
+
cycleId: optionalString(value.cycleId),
|
|
70
|
+
stop: parseStop(value.stop),
|
|
157
71
|
};
|
|
158
72
|
}
|
|
159
73
|
/**
|
|
160
|
-
*
|
|
161
|
-
*
|
|
74
|
+
* Migration-only reader for v1/v2 control blocks. `parseTaskControl` no longer accepts them —
|
|
75
|
+
* this exists so `migrateLegacyTaskState` (spec 029, D6's window, closed here) can durably
|
|
76
|
+
* upgrade an old file the one time it is encountered. Nothing else may call this.
|
|
162
77
|
*/
|
|
163
|
-
export function
|
|
78
|
+
export function parseLegacyTaskControl(raw) {
|
|
164
79
|
let value;
|
|
165
80
|
try {
|
|
166
81
|
value = JSON.parse(raw);
|
|
@@ -169,24 +84,14 @@ export function parseTaskControl(raw) {
|
|
|
169
84
|
throw new Error(`control is not valid JSON: ${errorMessage(error)}`);
|
|
170
85
|
}
|
|
171
86
|
if (!isRecord(value) || (value.version !== 1 && value.version !== 2)) {
|
|
172
|
-
throw new Error("control must be a version 1 or version 2 JSON object
|
|
87
|
+
throw new Error("legacy control must be a version 1 or version 2 JSON object");
|
|
173
88
|
}
|
|
174
|
-
const budget = isRecord(value.budget) ? value.budget : {};
|
|
175
|
-
const usage = isRecord(value.usage) ? value.usage : {};
|
|
176
|
-
const parsedUsage = parseTaskUsage(usage);
|
|
177
89
|
const verification = isRecord(value.verification) ? value.verification : {};
|
|
178
|
-
const maxAttempts = optionalPositive(budget.maxAttempts);
|
|
179
|
-
if (!maxAttempts || !Number.isInteger(maxAttempts)) {
|
|
180
|
-
throw new Error("control.budget.maxAttempts must be a positive integer");
|
|
181
|
-
}
|
|
182
90
|
const deadline = optionalString(value.deadline);
|
|
183
|
-
if (deadline && parseLocalTime(deadline) === undefined) {
|
|
184
|
-
throw new Error("control.deadline must be a valid local time");
|
|
185
|
-
}
|
|
186
91
|
const waitingFor = value.waitingFor === undefined ? undefined : enumValue(value.waitingFor, WAITING_FOR, "external-signal");
|
|
187
92
|
let stop = parseStop(value.stop);
|
|
188
93
|
// v1 stored the stop dimension as a status-side `pausedBy` value. Preserve its
|
|
189
|
-
// operational meaning while converting it to the
|
|
94
|
+
// operational meaning while converting it to the structured record.
|
|
190
95
|
if (!stop && value.pausedBy !== undefined) {
|
|
191
96
|
const by = enumValue(value.pausedBy, ["user", "governor"], "user");
|
|
192
97
|
stop = {
|
|
@@ -196,36 +101,20 @@ export function parseTaskControl(raw) {
|
|
|
196
101
|
};
|
|
197
102
|
}
|
|
198
103
|
return {
|
|
199
|
-
version:
|
|
200
|
-
|
|
201
|
-
deadline,
|
|
104
|
+
version: 3,
|
|
105
|
+
deadline: deadline && parseLocalTime(deadline) !== undefined ? deadline : undefined,
|
|
202
106
|
nextAction: optionalString(value.nextAction),
|
|
203
|
-
blockedReason: optionalString(value.blockedReason),
|
|
204
107
|
waitingFor,
|
|
205
|
-
budget: { maxAttempts: Math.max(1, Math.floor(maxAttempts)) },
|
|
206
|
-
usage: parsedUsage,
|
|
207
108
|
verification: {
|
|
208
|
-
|
|
109
|
+
// v1's independent mode was the only mode that represented a real checker gate.
|
|
110
|
+
required: typeof verification.required === "boolean" ? verification.required : verification.mode === "independent",
|
|
209
111
|
status: enumValue(verification.status, VERIFICATION_STATUSES, "pending"),
|
|
210
112
|
runId: optionalString(verification.runId),
|
|
211
|
-
evidence: optionalString(verification.evidence),
|
|
212
|
-
bodyHash: optionalString(verification.bodyHash),
|
|
213
|
-
checkedAt: optionalString(verification.checkedAt),
|
|
214
|
-
subjectHash: optionalString(verification.subjectHash),
|
|
215
113
|
},
|
|
216
|
-
attemptGeneration: nonNegativeInteger(value.attemptGeneration),
|
|
217
|
-
lastOutcome: enumValue(normalizeLegacyOutcome(value.lastOutcome), OUTCOMES, "pending"),
|
|
218
|
-
lastStartedAt: optionalString(value.lastStartedAt),
|
|
219
|
-
lastFinishedAt: optionalString(value.lastFinishedAt),
|
|
220
114
|
cycleId: optionalString(value.cycleId),
|
|
221
115
|
stop,
|
|
222
|
-
provenance: parseProvenance(value.provenance),
|
|
223
|
-
wakeHandoff: parseWakeHandoff(value.wakeHandoff),
|
|
224
116
|
};
|
|
225
117
|
}
|
|
226
|
-
function zeroUsage() {
|
|
227
|
-
return { attempts: 0, tokens: 0, costUsd: 0, costKnown: true, wallTimeMinutes: 0 };
|
|
228
|
-
}
|
|
229
118
|
/** Reset state that is meaningful only within one recurring task cycle. */
|
|
230
119
|
export function resetTaskControlForCycle(control, cycleId) {
|
|
231
120
|
const normalizedCycleId = cycleId.trim();
|
|
@@ -234,16 +123,10 @@ export function resetTaskControlForCycle(control, cycleId) {
|
|
|
234
123
|
return {
|
|
235
124
|
...structuredClone(control),
|
|
236
125
|
nextAction: undefined,
|
|
237
|
-
lastOutcome: "pending",
|
|
238
|
-
blockedReason: undefined,
|
|
239
126
|
waitingFor: undefined,
|
|
240
|
-
usage: zeroUsage(),
|
|
241
127
|
verification: { required: control.verification.required, status: "pending" },
|
|
242
|
-
lastStartedAt: undefined,
|
|
243
|
-
lastFinishedAt: undefined,
|
|
244
128
|
cycleId: normalizedCycleId,
|
|
245
129
|
stop: undefined,
|
|
246
|
-
wakeHandoff: undefined,
|
|
247
130
|
};
|
|
248
131
|
}
|
|
249
132
|
function patchOptionalString(current, value) {
|
|
@@ -253,8 +136,6 @@ function patchOptionalString(current, value) {
|
|
|
253
136
|
}
|
|
254
137
|
export function applyTaskControlPatch(control, patch) {
|
|
255
138
|
const next = structuredClone(control);
|
|
256
|
-
if (patch.priority !== undefined)
|
|
257
|
-
next.priority = patch.priority;
|
|
258
139
|
let normalizedDeadlinePatch = patch.deadline;
|
|
259
140
|
if (patch.deadline?.trim()) {
|
|
260
141
|
const deadlineMs = parseLocalTime(patch.deadline);
|
|
@@ -264,27 +145,17 @@ export function applyTaskControlPatch(control, patch) {
|
|
|
264
145
|
}
|
|
265
146
|
next.deadline = patchOptionalString(next.deadline, normalizedDeadlinePatch);
|
|
266
147
|
next.nextAction = patchOptionalString(next.nextAction, patch.nextAction);
|
|
267
|
-
next.blockedReason = patchOptionalString(next.blockedReason, patch.blockedReason);
|
|
268
148
|
if (patch.waitingFor !== undefined)
|
|
269
149
|
next.waitingFor = patch.waitingFor;
|
|
270
|
-
if (patch.maxAttempts !== undefined) {
|
|
271
|
-
if (!Number.isInteger(patch.maxAttempts) || patch.maxAttempts < 1) {
|
|
272
|
-
throw new Error("maxAttempts must be a positive integer.");
|
|
273
|
-
}
|
|
274
|
-
next.budget.maxAttempts = patch.maxAttempts;
|
|
275
|
-
}
|
|
276
150
|
if (patch.verificationRequired !== undefined && patch.verificationRequired !== next.verification.required) {
|
|
277
151
|
next.verification = { required: patch.verificationRequired, status: "pending" };
|
|
278
152
|
}
|
|
279
153
|
return next;
|
|
280
154
|
}
|
|
281
|
-
export function taskPriorityRank(priority) {
|
|
282
|
-
return { critical: 0, high: 1, normal: 2, low: 3 }[priority];
|
|
283
|
-
}
|
|
284
155
|
/**
|
|
285
156
|
* Return a deterministic governance violation for a live task. Sleeping tasks have no current
|
|
286
|
-
* work, so
|
|
287
|
-
*
|
|
157
|
+
* work, so a deadline cannot stop them; waiting and active tasks are both subject to it — a
|
|
158
|
+
* deadline is the user's own intent, not something a task can dodge by parking.
|
|
288
159
|
*/
|
|
289
160
|
export function taskBudgetViolation(control, nowMs, status = "active") {
|
|
290
161
|
if (status === "sleeping")
|
|
@@ -294,8 +165,5 @@ export function taskBudgetViolation(control, nowMs, status = "active") {
|
|
|
294
165
|
if (deadlineMs !== undefined && deadlineMs < nowMs)
|
|
295
166
|
return `deadline exceeded (${control.deadline})`;
|
|
296
167
|
}
|
|
297
|
-
if (status === "active" && control.usage.attempts >= control.budget.maxAttempts) {
|
|
298
|
-
return `attempt budget exhausted (${control.usage.attempts}/${control.budget.maxAttempts})`;
|
|
299
|
-
}
|
|
300
168
|
return undefined;
|
|
301
169
|
}
|
package/dist/tasks/ledger.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export interface TaskFrontmatter {
|
|
|
16
16
|
/** false => frontmatter could not be read (fail-open: the task is treated as actionable). */
|
|
17
17
|
readable: boolean;
|
|
18
18
|
status?: string;
|
|
19
|
-
/**
|
|
19
|
+
/** The unnormalized on-disk status value; startup migration is its only consumer. */
|
|
20
20
|
rawStatus?: string;
|
|
21
21
|
/** True when a legacy terminal file in the active directory needs archiving. */
|
|
22
22
|
archiveOutcome?: TaskArchiveOutcome;
|
|
@@ -25,16 +25,11 @@ export interface TaskFrontmatter {
|
|
|
25
25
|
wake?: string;
|
|
26
26
|
/** Five-field cron cadence (host timezone). Present ⇒ this is a recurring task. */
|
|
27
27
|
schedule?: string;
|
|
28
|
-
recurrence?: string;
|
|
29
28
|
control?: TaskControl;
|
|
30
|
-
/** false only when a control field exists but cannot be parsed/validated. */
|
|
29
|
+
/** false only when a control field exists but cannot be parsed/validated as v3. */
|
|
31
30
|
controlReadable?: boolean;
|
|
32
|
-
/**
|
|
33
|
-
|
|
34
|
-
* `/tasks doctor` needs this to report what an older build wrote (spec 036, D8) — the
|
|
35
|
-
* parsed `control` above cannot show a key whose whole point is that it is now ignored.
|
|
36
|
-
*/
|
|
37
|
-
rawControl?: unknown;
|
|
31
|
+
/** The unparsed `control:` value; startup migration re-parses this with the legacy reader. */
|
|
32
|
+
controlRaw?: string;
|
|
38
33
|
}
|
|
39
34
|
export interface TaskLedgerEntry {
|
|
40
35
|
/** Filename without `.md`; the task id. */
|
|
@@ -188,7 +183,7 @@ export declare function missingStandardTaskSections(content: string): string[];
|
|
|
188
183
|
* Without the "no checklist items" case, a DoD written as prose or a numbered
|
|
189
184
|
* list (no `- [ ]` anywhere) makes this function return an empty array —
|
|
190
185
|
* indistinguishable from "everything is checked" — which would silently let
|
|
191
|
-
* `task_manage
|
|
186
|
+
* `task_manage complete` through with nothing ever actually verified.
|
|
192
187
|
*/
|
|
193
188
|
export declare function uncheckedTaskAcceptanceItems(content: string): string[];
|
|
194
189
|
/**
|
|
@@ -204,13 +199,12 @@ export interface TaskDocumentFields {
|
|
|
204
199
|
enabled?: boolean;
|
|
205
200
|
wake?: string;
|
|
206
201
|
schedule?: string;
|
|
207
|
-
recurrence?: string;
|
|
208
202
|
control?: TaskControl;
|
|
209
203
|
outcome?: TaskArchiveOutcome;
|
|
210
204
|
closedAt?: string;
|
|
211
205
|
}
|
|
212
206
|
/**
|
|
213
|
-
* Enforce the
|
|
207
|
+
* Enforce the v3 write-path invariants. Runtime due transitions are intentionally separate: a
|
|
214
208
|
* waiting task is first written active, and a sleeping task is first opened into a cycle, before
|
|
215
209
|
* the driver dispatches anything.
|
|
216
210
|
*/
|
|
@@ -240,7 +234,7 @@ export declare function upsertCurrentCycleCompletionEvidence(content: string, ev
|
|
|
240
234
|
* checkpoints to the previous cycle.
|
|
241
235
|
*/
|
|
242
236
|
export declare function startTaskCycle(content: string, cycleId: string, archivePrevious?: boolean): string;
|
|
243
|
-
/** Actionable first; then earliest wake first (unset
|
|
237
|
+
/** Actionable first; then earliest deadline/wake first (unset sorts as "ready now"); then id. */
|
|
244
238
|
export declare function compareTaskEntries(a: TaskLedgerEntry, b: TaskLedgerEntry): number;
|
|
245
239
|
/**
|
|
246
240
|
* Read every `.md` file in `tasks/` (root only — the `archive/` subdirectory is not
|
package/dist/tasks/ledger.js
CHANGED
|
@@ -2,19 +2,10 @@ import { readdir, readFile, stat } from "node:fs/promises";
|
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { formatLocalTime, parseLocalTime } from "../shared/local-time.js";
|
|
4
4
|
import { RecoverableToolError } from "../shared/recoverable-error.js";
|
|
5
|
-
import {
|
|
5
|
+
import { parseTaskControl } from "./control.js";
|
|
6
6
|
import { nextTaskWake } from "./task-schedule.js";
|
|
7
|
-
import { normalizeStoredStatus
|
|
8
|
-
const FRONTMATTER_FIELDS = [
|
|
9
|
-
"status",
|
|
10
|
-
"enabled",
|
|
11
|
-
"wake",
|
|
12
|
-
"schedule",
|
|
13
|
-
"recurrence",
|
|
14
|
-
"outcome",
|
|
15
|
-
"closedAt",
|
|
16
|
-
"control",
|
|
17
|
-
];
|
|
7
|
+
import { normalizeStoredStatus } from "./transitions.js";
|
|
8
|
+
const FRONTMATTER_FIELDS = ["status", "enabled", "wake", "schedule", "outcome", "closedAt", "control"];
|
|
18
9
|
const TASK_ID_PATTERN = /^[A-Za-z0-9._-]+$/;
|
|
19
10
|
export const DEFAULT_TASK_MANUAL = "- Follow the DoD, keep the current cycle log updated, and schedule check-ins when waiting.";
|
|
20
11
|
export const STANDARD_TASK_SECTIONS = [
|
|
@@ -268,12 +259,7 @@ export function parseTaskFrontmatter(content) {
|
|
|
268
259
|
frontmatter.controlReadable = false;
|
|
269
260
|
}
|
|
270
261
|
else {
|
|
271
|
-
|
|
272
|
-
frontmatter.rawControl = JSON.parse(value);
|
|
273
|
-
}
|
|
274
|
-
catch {
|
|
275
|
-
// Leave rawControl unset; the parse below reports the real problem.
|
|
276
|
-
}
|
|
262
|
+
frontmatter.controlRaw = value;
|
|
277
263
|
try {
|
|
278
264
|
frontmatter.control = parseTaskControl(value);
|
|
279
265
|
frontmatter.controlReadable = true;
|
|
@@ -293,34 +279,18 @@ export function parseTaskFrontmatter(content) {
|
|
|
293
279
|
else if (key === "closedAt") {
|
|
294
280
|
frontmatter.closedAt = value || undefined;
|
|
295
281
|
}
|
|
296
|
-
else if (key === "status" || key === "wake" || key === "schedule"
|
|
282
|
+
else if (key === "status" || key === "wake" || key === "schedule") {
|
|
297
283
|
frontmatter[key] = value || undefined;
|
|
298
284
|
}
|
|
299
285
|
}
|
|
300
286
|
}
|
|
301
|
-
//
|
|
302
|
-
//
|
|
303
|
-
//
|
|
287
|
+
// Legacy status vocabulary is a startup-migration concern (`migrateLegacyTaskState`), not a
|
|
288
|
+
// read-time one: any value outside the current three fails open to "active" here, same as an
|
|
289
|
+
// unreadable control block fails open to "actionable" rather than being silently reinterpreted.
|
|
304
290
|
const rawStatus = frontmatter.status;
|
|
305
291
|
frontmatter.rawStatus = rawStatus;
|
|
306
292
|
if (frontmatter.readable) {
|
|
307
|
-
|
|
308
|
-
frontmatter.archiveOutcome = "completed";
|
|
309
|
-
if (rawStatus === "cancelled")
|
|
310
|
-
frontmatter.archiveOutcome = "cancelled";
|
|
311
|
-
frontmatter.status = normalizeStoredStatus(rawStatus, Boolean(frontmatter.schedule));
|
|
312
|
-
if (rawStatus === "paused" || wasLegacyEscalated(rawStatus)) {
|
|
313
|
-
frontmatter.enabled = false;
|
|
314
|
-
frontmatter.control ??= createDefaultTaskControl();
|
|
315
|
-
if (frontmatter.control && !frontmatter.control.stop) {
|
|
316
|
-
frontmatter.control.stop = {
|
|
317
|
-
by: wasLegacyEscalated(rawStatus) ? "governor" : "user",
|
|
318
|
-
reason: frontmatter.control.blockedReason ??
|
|
319
|
-
`Task stopped by ${wasLegacyEscalated(rawStatus) ? "governor" : "user"}.`,
|
|
320
|
-
at: formatLocalTime(),
|
|
321
|
-
};
|
|
322
|
-
}
|
|
323
|
-
}
|
|
293
|
+
frontmatter.status = normalizeStoredStatus(rawStatus);
|
|
324
294
|
}
|
|
325
295
|
return frontmatter;
|
|
326
296
|
}
|
|
@@ -396,7 +366,7 @@ export function missingStandardTaskSections(content) {
|
|
|
396
366
|
* Without the "no checklist items" case, a DoD written as prose or a numbered
|
|
397
367
|
* list (no `- [ ]` anywhere) makes this function return an empty array —
|
|
398
368
|
* indistinguishable from "everything is checked" — which would silently let
|
|
399
|
-
* `task_manage
|
|
369
|
+
* `task_manage complete` through with nothing ever actually verified.
|
|
400
370
|
*/
|
|
401
371
|
export function uncheckedTaskAcceptanceItems(content) {
|
|
402
372
|
const unchecked = [];
|
|
@@ -507,14 +477,14 @@ export function renderStandardTaskBody(input) {
|
|
|
507
477
|
].join("\n");
|
|
508
478
|
}
|
|
509
479
|
/**
|
|
510
|
-
* Enforce the
|
|
480
|
+
* Enforce the v3 write-path invariants. Runtime due transitions are intentionally separate: a
|
|
511
481
|
* waiting task is first written active, and a sleeping task is first opened into a cycle, before
|
|
512
482
|
* the driver dispatches anything.
|
|
513
483
|
*/
|
|
514
484
|
export function normalizeTaskFields(fields, now = new Date()) {
|
|
515
485
|
const next = {
|
|
516
486
|
...fields,
|
|
517
|
-
status: normalizeStoredStatus(fields.status
|
|
487
|
+
status: normalizeStoredStatus(fields.status),
|
|
518
488
|
enabled: fields.enabled !== false,
|
|
519
489
|
};
|
|
520
490
|
if (next.outcome) {
|
|
@@ -605,8 +575,6 @@ export function renderTaskDocument(fields, rawBody) {
|
|
|
605
575
|
lines.push(`wake: ${document.wake}`);
|
|
606
576
|
if (document.schedule)
|
|
607
577
|
lines.push(`schedule: ${document.schedule}`);
|
|
608
|
-
if (document.recurrence)
|
|
609
|
-
lines.push(`recurrence: ${document.recurrence}`);
|
|
610
578
|
if (document.control)
|
|
611
579
|
lines.push(`control: ${JSON.stringify(document.control)}`);
|
|
612
580
|
lines.push("---");
|
|
@@ -982,17 +950,13 @@ function parseWakeMs(frontmatter) {
|
|
|
982
950
|
return undefined;
|
|
983
951
|
return parseLocalTime(frontmatter.wake);
|
|
984
952
|
}
|
|
985
|
-
/** Actionable first; then earliest wake first (unset
|
|
953
|
+
/** Actionable first; then earliest deadline/wake first (unset sorts as "ready now"); then id. */
|
|
986
954
|
export function compareTaskEntries(a, b) {
|
|
987
955
|
if (a.actionable !== b.actionable)
|
|
988
956
|
return a.actionable ? -1 : 1;
|
|
989
957
|
if (a.frontmatter.enabled !== b.frontmatter.enabled)
|
|
990
958
|
return a.frontmatter.enabled ? -1 : 1;
|
|
991
959
|
if (a.actionable && b.actionable) {
|
|
992
|
-
const ap = taskPriorityRank(a.frontmatter.control?.priority ?? "normal");
|
|
993
|
-
const bp = taskPriorityRank(b.frontmatter.control?.priority ?? "normal");
|
|
994
|
-
if (ap !== bp)
|
|
995
|
-
return ap - bp;
|
|
996
960
|
const ad = a.frontmatter.control?.deadline
|
|
997
961
|
? (parseLocalTime(a.frontmatter.control.deadline) ?? Number.POSITIVE_INFINITY)
|
|
998
962
|
: Number.POSITIVE_INFINITY;
|
package/dist/tasks/store.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type TaskWaitingFor } from "./control.js";
|
|
2
2
|
import { type TaskDocumentFields } from "./ledger.js";
|
|
3
3
|
export interface StoredTaskDocument {
|
|
4
4
|
id: string;
|
|
@@ -17,61 +17,28 @@ export declare function taskBodyHash(body: string): string;
|
|
|
17
17
|
export declare function readStoredTask(channelDir: string, idInput: string, includeArchive?: boolean, allowInvalidControl?: boolean): Promise<StoredTaskDocument | undefined>;
|
|
18
18
|
export declare function writeStoredTask(document: StoredTaskDocument): Promise<void>;
|
|
19
19
|
export declare function updateStoredTask(channelDir: string, id: string, update: (document: StoredTaskDocument) => void, includeArchive?: boolean): Promise<StoredTaskDocument | undefined>;
|
|
20
|
-
export interface TaskAttemptClaim {
|
|
21
|
-
control: TaskControl;
|
|
22
|
-
previousLastOutcome: TaskOutcome;
|
|
23
|
-
previousBlockedReason?: string;
|
|
24
|
-
previousLastStartedAt?: string;
|
|
25
|
-
generation: number;
|
|
26
|
-
}
|
|
27
|
-
export declare function claimTaskAttempt(channelDir: string, id: string, now: Date): Promise<TaskAttemptClaim | undefined>;
|
|
28
|
-
export declare function releaseTaskAttemptClaim(channelDir: string, id: string, claim: TaskAttemptClaim): Promise<void>;
|
|
29
|
-
export declare function finishTaskAttempt(channelDir: string, id: string, result: {
|
|
30
|
-
tokens: number;
|
|
31
|
-
costUsd: number;
|
|
32
|
-
costKnown: boolean;
|
|
33
|
-
wallTimeMinutes: number;
|
|
34
|
-
failed: boolean;
|
|
35
|
-
silent?: boolean;
|
|
36
|
-
finishedAt: Date;
|
|
37
|
-
/**
|
|
38
|
-
* The `attemptGeneration` this turn's claim recorded (spec 029's monotonic claim
|
|
39
|
-
* identity, see `TaskControl.attemptGeneration`). When present and stale — a newer
|
|
40
|
-
* claim has since superseded it — usage is still tallied (the spend genuinely
|
|
41
|
-
* happened), but the outcome/finish fields are left alone so a slow, superseded turn
|
|
42
|
-
* cannot overwrite the newer attempt's in-flight or already-finished state. Absent
|
|
43
|
-
* (e.g. legacy callers) means "apply unconditionally", matching prior behavior.
|
|
44
|
-
*/
|
|
45
|
-
generation?: number;
|
|
46
|
-
}): Promise<void>;
|
|
47
20
|
/**
|
|
48
21
|
* A governor stop: disable the task without changing its lifecycle stage or wake. The structured
|
|
49
22
|
* stop record is the durable recovery receipt; notification is handled by the runtime driver.
|
|
50
23
|
*/
|
|
51
24
|
export declare function escalateTask(channelDir: string, id: string, reason: string): Promise<boolean>;
|
|
52
|
-
/** Atomically convert a due timed wait to active before any wake is dispatched. */
|
|
53
|
-
export declare function activateWaitingTask(channelDir: string, id: string, expectedWaitingFor?: TaskWaitingFor): Promise<StoredTaskDocument | undefined>;
|
|
54
25
|
export interface WakeTaskTransitionHooks {
|
|
55
|
-
/** Test-only fault seam
|
|
26
|
+
/** Test-only fault seam, called immediately before the atomic activation write. */
|
|
56
27
|
beforeActivation?: () => void;
|
|
57
|
-
beforeAttemptClaim?: () => void;
|
|
58
|
-
}
|
|
59
|
-
export interface WakeTaskHandoffInput {
|
|
60
|
-
kind: "subagent";
|
|
61
|
-
resourceId: string;
|
|
62
|
-
dispatchId: string;
|
|
63
28
|
}
|
|
64
|
-
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
export declare function
|
|
29
|
+
/**
|
|
30
|
+
* Atomically convert a due/completion-woken task to active. Idempotent by construction: once a
|
|
31
|
+
* task is no longer `waiting`, a redelivered wake or retry finds `status !== "waiting"` and is a
|
|
32
|
+
* safe no-op, so no separate claim/handoff bookkeeping is needed to detect a duplicate activation.
|
|
33
|
+
*
|
|
34
|
+
* Wake legitimacy is entirely the caller's responsibility (spec 043's `waitingFor`
|
|
35
|
+
* de-authorization): a completion wake reaches here only after the caller has verified an
|
|
36
|
+
* actually-settled run/job whose own recorded `taskId` names this task (`task-wake.ts`'s
|
|
37
|
+
* `isVerified*Wake`). `waitingFor` itself is display-only and never gates activation.
|
|
38
|
+
*/
|
|
39
|
+
export declare function activateWaitingTask(channelDir: string, id: string, hooks?: WakeTaskTransitionHooks): Promise<StoredTaskDocument | undefined>;
|
|
40
|
+
/** Return a task to waiting after a transport failed to accept its just-activated turn. */
|
|
41
|
+
export declare function rollbackWaitingTask(channelDir: string, id: string, waitingFor: TaskWaitingFor): Promise<void>;
|
|
75
42
|
/**
|
|
76
43
|
* A stable cycle id for a runtime-opened recurring cycle: `cycle-YYYY-MM-DD`, disambiguated
|
|
77
44
|
* with a `-N` suffix when the same task is reopened more than once on the same local day.
|