@oyasmi/pipiclaw 0.9.2 → 0.9.3-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.en.md +250 -0
- package/README.md +3 -1
- package/dist/agent/channel-runner.d.ts +59 -2
- package/dist/agent/channel-runner.js +177 -76
- package/dist/agent/job-manager.d.ts +76 -21
- package/dist/agent/job-manager.js +447 -63
- package/dist/agent/maintenance-context.js +4 -5
- package/dist/agent/prompt/manifest.d.ts +0 -2
- package/dist/agent/prompt/manifest.js +2 -3
- package/dist/agent/prompt/sections.js +3 -3
- package/dist/agent/session-events.js +4 -8
- package/dist/agent/turn-prompt.d.ts +7 -7
- package/dist/agent/turn-prompt.js +6 -9
- package/dist/agent/types.d.ts +12 -0
- package/dist/agent/types.js +1 -0
- package/dist/channel/channel-context.d.ts +13 -0
- package/dist/channel/channel-context.js +21 -1
- package/dist/commands/catalog.js +47 -20
- package/dist/memory/commands.d.ts +8 -0
- package/dist/memory/commands.js +113 -148
- package/dist/memory/index-budget.d.ts +27 -0
- package/dist/memory/index-budget.js +99 -0
- package/dist/memory/journal.d.ts +20 -0
- package/dist/memory/journal.js +78 -0
- package/dist/memory/lifecycle.d.ts +9 -15
- package/dist/memory/lifecycle.js +55 -145
- package/dist/memory/maintenance-gates.d.ts +13 -47
- package/dist/memory/maintenance-gates.js +13 -92
- package/dist/memory/maintenance-state.d.ts +10 -16
- package/dist/memory/maintenance-state.js +12 -59
- package/dist/memory/maintenance-tuning.d.ts +7 -27
- package/dist/memory/maintenance-tuning.js +11 -28
- package/dist/memory/migrate.d.ts +15 -0
- package/dist/memory/migrate.js +364 -0
- package/dist/memory/reflect-job.d.ts +43 -0
- package/dist/memory/reflect-job.js +117 -0
- package/dist/memory/reflect.d.ts +52 -0
- package/dist/memory/reflect.js +313 -0
- package/dist/memory/render.d.ts +19 -0
- package/dist/memory/render.js +35 -0
- package/dist/memory/review-log.d.ts +2 -1
- package/dist/memory/scheduler.d.ts +2 -3
- package/dist/memory/scheduler.js +14 -49
- package/dist/memory/search.d.ts +35 -0
- package/dist/memory/search.js +297 -0
- package/dist/memory/session-search.js +1 -1
- package/dist/memory/store.d.ts +108 -0
- package/dist/memory/store.js +418 -0
- package/dist/memory/task-digest.d.ts +2 -2
- package/dist/memory/task-digest.js +29 -36
- package/dist/memory/tombstones.d.ts +0 -11
- package/dist/memory/tombstones.js +6 -37
- package/dist/memory/transcript.js +12 -5
- package/dist/playbooks/agent-delegation.md +37 -9
- package/dist/playbooks/background-jobs.md +1 -1
- package/dist/playbooks/event-scheduling.md +6 -2
- package/dist/playbooks/memory-and-learning.md +36 -12
- package/dist/playbooks/outbound-media.md +1 -1
- package/dist/playbooks/runtime-orientation.md +9 -8
- package/dist/playbooks/task-loop.md +110 -0
- package/dist/runtime/bootstrap.d.ts +3 -3
- package/dist/runtime/bootstrap.js +66 -41
- package/dist/runtime/events.d.ts +9 -0
- package/dist/runtime/events.js +43 -4
- package/dist/runtime/task-commands.d.ts +14 -15
- package/dist/runtime/task-commands.js +249 -485
- package/dist/runtime/task-driver.d.ts +41 -29
- package/dist/runtime/task-driver.js +170 -317
- package/dist/runtime/task-migration.d.ts +34 -30
- package/dist/runtime/task-migration.js +229 -183
- package/dist/runtime/task-wake.d.ts +6 -3
- package/dist/runtime/task-wake.js +26 -19
- package/dist/settings.d.ts +0 -29
- package/dist/settings.js +12 -35
- package/dist/shared/markdown-sections.d.ts +0 -1
- package/dist/shared/markdown-sections.js +0 -3
- package/dist/subagents/discovery.d.ts +2 -2
- package/dist/subagents/discovery.js +21 -10
- package/dist/subagents/external/progress-tail.js +6 -1
- package/dist/subagents/external/run.js +1 -0
- package/dist/subagents/runs.d.ts +19 -2
- package/dist/subagents/runs.js +73 -4
- package/dist/subagents/tool.d.ts +10 -8
- package/dist/subagents/tool.js +55 -94
- package/dist/tasks/brief.d.ts +17 -0
- package/dist/tasks/brief.js +57 -0
- package/dist/tasks/budget.d.ts +26 -0
- package/dist/tasks/budget.js +59 -0
- package/dist/tasks/cycle.d.ts +48 -0
- package/dist/tasks/cycle.js +94 -0
- package/dist/tasks/frontmatter.d.ts +73 -0
- package/dist/tasks/frontmatter.js +175 -0
- package/dist/tasks/ledger.d.ts +66 -106
- package/dist/tasks/ledger.js +90 -546
- package/dist/tasks/log.d.ts +88 -0
- package/dist/tasks/log.js +97 -0
- package/dist/tasks/rounds.d.ts +41 -0
- package/dist/tasks/rounds.js +53 -0
- package/dist/tasks/session-path.d.ts +9 -0
- package/dist/tasks/session-path.js +16 -0
- package/dist/tasks/steer.d.ts +8 -0
- package/dist/tasks/steer.js +79 -0
- package/dist/tasks/store.d.ts +60 -35
- package/dist/tasks/store.js +166 -107
- package/dist/tasks/ticket.d.ts +102 -0
- package/dist/tasks/ticket.js +209 -0
- package/dist/tasks/verification.d.ts +47 -0
- package/dist/tasks/verification.js +60 -1
- package/dist/tools/index.d.ts +13 -4
- package/dist/tools/index.js +5 -10
- package/dist/tools/memory-manage.d.ts +6 -16
- package/dist/tools/memory-manage.js +108 -129
- package/dist/tools/presentation.js +2 -1
- package/dist/tools/registry.d.ts +11 -2
- package/dist/tools/registry.js +17 -6
- package/dist/tools/subagent-manage.d.ts +0 -9
- package/dist/tools/subagent-manage.js +8 -17
- package/dist/tools/task-manage/create.js +2 -2
- package/dist/tools/task-manage/lifecycle.d.ts +9 -4
- package/dist/tools/task-manage/lifecycle.js +136 -188
- package/dist/tools/task-manage/log-view.d.ts +8 -0
- package/dist/tools/task-manage/log-view.js +24 -0
- package/dist/tools/task-manage/schema.d.ts +38 -14
- package/dist/tools/task-manage/schema.js +46 -27
- package/dist/tools/task-manage/shared.d.ts +35 -31
- package/dist/tools/task-manage/shared.js +130 -141
- package/dist/tools/task-manage/step-end.d.ts +11 -0
- package/dist/tools/task-manage/step-end.js +134 -0
- package/dist/tools/task-manage/types.d.ts +23 -32
- package/dist/tools/task-manage.d.ts +4 -3
- package/dist/tools/task-manage.js +30 -15
- package/dist/tools/tool-details.d.ts +1 -1
- package/dist/tui/app.js +0 -2
- package/examples/sub-agents/README.md +54 -38
- package/examples/sub-agents/builder-hard.md +21 -12
- package/examples/sub-agents/builder.md +24 -16
- package/examples/sub-agents/explorer.md +17 -15
- package/examples/sub-agents/git-committer.md +8 -6
- package/examples/sub-agents/planner.md +17 -16
- package/examples/sub-agents/reviewer.md +11 -9
- package/examples/sub-agents/verifier.md +22 -16
- package/examples/sub-agents/worker.md +22 -6
- package/package.json +1 -1
- package/dist/agent/effect-ledger.d.ts +0 -32
- package/dist/agent/effect-ledger.js +0 -89
- package/dist/memory/bootstrap.d.ts +0 -17
- package/dist/memory/bootstrap.js +0 -120
- package/dist/memory/candidates.d.ts +0 -26
- package/dist/memory/candidates.js +0 -202
- package/dist/memory/consolidation.d.ts +0 -54
- package/dist/memory/consolidation.js +0 -292
- package/dist/memory/extraction.d.ts +0 -54
- package/dist/memory/extraction.js +0 -240
- package/dist/memory/files.d.ts +0 -71
- package/dist/memory/files.js +0 -419
- package/dist/memory/maintenance-jobs.d.ts +0 -41
- package/dist/memory/maintenance-jobs.js +0 -265
- package/dist/memory/metadata.d.ts +0 -57
- package/dist/memory/metadata.js +0 -142
- package/dist/memory/probation.d.ts +0 -17
- package/dist/memory/probation.js +0 -44
- package/dist/memory/promotion.d.ts +0 -38
- package/dist/memory/promotion.js +0 -36
- package/dist/memory/recall.d.ts +0 -59
- package/dist/memory/recall.js +0 -746
- package/dist/memory/session.d.ts +0 -24
- package/dist/memory/session.js +0 -273
- package/dist/playbooks/task-driving.md +0 -92
- package/dist/playbooks/task-planning.md +0 -58
- package/dist/tasks/control.d.ts +0 -56
- package/dist/tasks/control.js +0 -169
- package/dist/tasks/transitions.d.ts +0 -16
- package/dist/tasks/transitions.js +0 -48
- package/dist/tools/task-manage/verification.d.ts +0 -19
- package/dist/tools/task-manage/verification.js +0 -91
- package/examples/sub-agents/documenter.md +0 -50
- package/examples/sub-agents/log-sifter.md +0 -47
- package/examples/sub-agents/scout.md +0 -34
package/dist/tasks/ledger.js
CHANGED
|
@@ -1,21 +1,17 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
1
2
|
import { readdir, readFile, stat } from "node:fs/promises";
|
|
2
3
|
import { join } from "node:path";
|
|
3
|
-
import { formatLocalTime, parseLocalTime } from "../shared/local-time.js";
|
|
4
4
|
import { RecoverableToolError } from "../shared/recoverable-error.js";
|
|
5
5
|
import { normalizeSafeId } from "../shared/safe-id.js";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import { normalizeStoredStatus } from "./transitions.js";
|
|
9
|
-
const FRONTMATTER_FIELDS = ["status", "enabled", "wake", "schedule", "outcome", "closedAt", "control"];
|
|
6
|
+
import { isTaskRunnable, parseTaskFrontmatterV4, renderTaskFrontmatter, } from "./frontmatter.js";
|
|
7
|
+
import { ticketDueMs, ticketExpired } from "./ticket.js";
|
|
10
8
|
const TASK_ID_PATTERN = /^[A-Za-z0-9._-]+$/;
|
|
11
|
-
export const DEFAULT_TASK_MANUAL = "- Follow the DoD,
|
|
9
|
+
export const DEFAULT_TASK_MANUAL = "- Follow the DoD, record evidence in every step note, and park on a ticket when waiting.";
|
|
12
10
|
export const STANDARD_TASK_SECTIONS = [
|
|
13
11
|
{ label: "Goal", names: ["Goal", "目标"] },
|
|
14
12
|
{ label: "DoD", names: ["DoD"] },
|
|
15
13
|
{ label: "Manual", names: ["Manual", "手册"] },
|
|
16
14
|
{ label: "Verification", names: ["Verification", "验收"] },
|
|
17
|
-
{ label: "Current Cycle", names: ["Current Cycle", "当前周期"] },
|
|
18
|
-
{ label: "History", names: ["History", "历史"] },
|
|
19
15
|
];
|
|
20
16
|
/**
|
|
21
17
|
* `## Plan` is a deliberately optional section (spec 037, D2) — it is not part of
|
|
@@ -23,13 +19,11 @@ export const STANDARD_TASK_SECTIONS = [
|
|
|
23
19
|
* task or any pre-existing task simply has no Plan.
|
|
24
20
|
*/
|
|
25
21
|
const PLAN_SECTION_NAMES = ["Plan", "计划"];
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
const HISTORY_OMISSION_NOTE = "- Older cycle details omitted from working context; use session_search with the task id or cycle id to inspect cold history.";
|
|
32
|
-
const HISTORY_TRUNCATION_NOTE = "- Entry truncated in working context; use session_search with the task id or cycle id to inspect the complete cold history.";
|
|
22
|
+
/**
|
|
23
|
+
* The single historical paragraph a v4 contract keeps (spec 051, D5). Declared here rather than
|
|
24
|
+
* in `cycle.ts` so `taskContractSegment` can end the contract at it without a circular import.
|
|
25
|
+
*/
|
|
26
|
+
export const LAST_RESULT_SECTION_NAMES = ["上次结果", "Last Result"];
|
|
33
27
|
/** Validate/normalize a task id (filename without `.md`), rejecting path traversal. */
|
|
34
28
|
export function normalizeTaskId(id) {
|
|
35
29
|
return normalizeSafeId(id, { suffix: ".md", pattern: TASK_ID_PATTERN, label: "task id" });
|
|
@@ -45,8 +39,8 @@ export function taskBody(content) {
|
|
|
45
39
|
return after === -1 ? "" : content.slice(after + 1);
|
|
46
40
|
}
|
|
47
41
|
/**
|
|
48
|
-
* The task's *contract* segment: the body up to (excluding) whichever of "Plan" or "
|
|
49
|
-
*
|
|
42
|
+
* The task's *contract* segment: the body up to (excluding) whichever of "Plan" or "上次结果"
|
|
43
|
+
* appears first — i.e. the H1 title, Goal, DoD (with its checkbox state), Manual and
|
|
50
44
|
* Verification (spec 029, D4).
|
|
51
45
|
*
|
|
52
46
|
* Verification PASS binds to this segment, not the whole body, so routine `progress` notes
|
|
@@ -65,12 +59,21 @@ export function taskContractSegment(body) {
|
|
|
65
59
|
const match = /^#{1,6}\s+(.+?)\s*$/.exec(lines[index] ?? "");
|
|
66
60
|
if (match &&
|
|
67
61
|
(matchesTaskSectionTitle(match[1] ?? "", PLAN_SECTION_NAMES) ||
|
|
68
|
-
matchesTaskSectionTitle(match[1] ?? "",
|
|
62
|
+
matchesTaskSectionTitle(match[1] ?? "", LAST_RESULT_SECTION_NAMES))) {
|
|
69
63
|
return lines.slice(0, index).join("\n").replace(/\s+$/, "");
|
|
70
64
|
}
|
|
71
65
|
}
|
|
72
66
|
return body;
|
|
73
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Hash the task's *contract* segment (Goal/DoD/Manual/Verification), not the whole body, so a
|
|
70
|
+
* verification PASS survives routine step logging and only breaks when the contract itself
|
|
71
|
+
* changes (spec 029, D4). Lives next to `taskContractSegment` because it is that function's
|
|
72
|
+
* only real consumer.
|
|
73
|
+
*/
|
|
74
|
+
export function taskBodyHash(body) {
|
|
75
|
+
return createHash("sha256").update(taskContractSegment(body)).digest("hex");
|
|
76
|
+
}
|
|
74
77
|
const PLAN_STEP_STATUS_BY_MARKER = {
|
|
75
78
|
" ": "todo",
|
|
76
79
|
x: "done",
|
|
@@ -143,17 +146,21 @@ function renderPlanStepLine(id, status, text, dodRefs = []) {
|
|
|
143
146
|
const suffix = dodRefs.length > 0 ? ` → dod:${dodRefs.join(",")}` : "";
|
|
144
147
|
return `- [${PLAN_STATUS_MARKER[status]}] ${id} ${text}${suffix}`;
|
|
145
148
|
}
|
|
146
|
-
/**
|
|
149
|
+
/**
|
|
150
|
+
* Insert an empty "## Plan" heading after the contract — immediately before "## 上次结果" when the
|
|
151
|
+
* task has one, otherwise at the end. Spec 051 removed "## Current Cycle", which used to be the
|
|
152
|
+
* anchor; a task with neither section simply gets its Plan appended rather than being rejected.
|
|
153
|
+
*/
|
|
147
154
|
function insertEmptyPlanSection(body) {
|
|
148
155
|
const lines = body.split("\n");
|
|
149
156
|
for (let index = 0; index < lines.length; index++) {
|
|
150
157
|
const match = /^(#{1,6})\s+(.+?)\s*$/.exec(lines[index] ?? "");
|
|
151
|
-
if (match && matchesTaskSectionTitle(match[2] ?? "",
|
|
158
|
+
if (match && matchesTaskSectionTitle(match[2] ?? "", LAST_RESULT_SECTION_NAMES)) {
|
|
152
159
|
lines.splice(index, 0, "## Plan", "");
|
|
153
160
|
return lines.join("\n");
|
|
154
161
|
}
|
|
155
162
|
}
|
|
156
|
-
|
|
163
|
+
return `${body.replace(/\n+$/, "")}\n\n## Plan\n`;
|
|
157
164
|
}
|
|
158
165
|
/**
|
|
159
166
|
* Update or append Plan steps by id (spec 037, D3). An id that already exists gets its
|
|
@@ -203,99 +210,6 @@ export function applyTaskPlanPatch(body, patches) {
|
|
|
203
210
|
}
|
|
204
211
|
return { body: working, summary: deltas.length > 0 ? `plan: ${deltas.join("; ")}` : "" };
|
|
205
212
|
}
|
|
206
|
-
/** Parse the leading frontmatter block and normalize legacy v1 fields in memory. */
|
|
207
|
-
export function parseTaskFrontmatter(content) {
|
|
208
|
-
if (!content.startsWith("---"))
|
|
209
|
-
return { readable: false, enabled: true };
|
|
210
|
-
const end = content.indexOf("\n---", 3);
|
|
211
|
-
if (end === -1)
|
|
212
|
-
return { readable: false, enabled: true };
|
|
213
|
-
const block = content.slice(3, end);
|
|
214
|
-
const frontmatter = { readable: true, enabled: true };
|
|
215
|
-
for (const line of block.split("\n")) {
|
|
216
|
-
const idx = line.indexOf(":");
|
|
217
|
-
if (idx === -1)
|
|
218
|
-
continue;
|
|
219
|
-
const key = line.slice(0, idx).trim();
|
|
220
|
-
if (FRONTMATTER_FIELDS.includes(key)) {
|
|
221
|
-
const value = line.slice(idx + 1).trim();
|
|
222
|
-
if (key === "control") {
|
|
223
|
-
if (!value) {
|
|
224
|
-
frontmatter.controlReadable = false;
|
|
225
|
-
}
|
|
226
|
-
else {
|
|
227
|
-
frontmatter.controlRaw = value;
|
|
228
|
-
try {
|
|
229
|
-
frontmatter.control = parseTaskControl(value);
|
|
230
|
-
frontmatter.controlReadable = true;
|
|
231
|
-
}
|
|
232
|
-
catch {
|
|
233
|
-
frontmatter.controlReadable = false;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
else if (key === "enabled") {
|
|
238
|
-
frontmatter.enabled = value !== "false";
|
|
239
|
-
}
|
|
240
|
-
else if (key === "outcome") {
|
|
241
|
-
if (value === "completed" || value === "cancelled")
|
|
242
|
-
frontmatter.archiveOutcome = value;
|
|
243
|
-
}
|
|
244
|
-
else if (key === "closedAt") {
|
|
245
|
-
frontmatter.closedAt = value || undefined;
|
|
246
|
-
}
|
|
247
|
-
else if (key === "status" || key === "wake" || key === "schedule") {
|
|
248
|
-
frontmatter[key] = value || undefined;
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
// Legacy status vocabulary is a startup-migration concern (`migrateLegacyTaskState`), not a
|
|
253
|
-
// read-time one: any value outside the current three fails open to "active" here, same as an
|
|
254
|
-
// unreadable control block fails open to "actionable" rather than being silently reinterpreted.
|
|
255
|
-
const rawStatus = frontmatter.status;
|
|
256
|
-
frontmatter.rawStatus = rawStatus;
|
|
257
|
-
if (frontmatter.readable) {
|
|
258
|
-
frontmatter.status = normalizeStoredStatus(rawStatus);
|
|
259
|
-
}
|
|
260
|
-
return frontmatter;
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* The single shared judgement: is there work to do on this task right now?
|
|
264
|
-
* Unreadable frontmatter is fail-open (actionable) so a corrupt ledger surfaces
|
|
265
|
-
* rather than being silently skipped.
|
|
266
|
-
*
|
|
267
|
-
* `waiting` with no `wake` is the *parked* form (`src/tasks/transitions.ts`): the task is
|
|
268
|
-
* blocked on an external signal — a background job finishing, the user answering, `/tasks run` —
|
|
269
|
-
* and only that signal may resume it. Without this the runtime's own recommended shape for
|
|
270
|
-
* delegating work (start a blocking wait as a `bash async` job, park the task, end the turn)
|
|
271
|
-
* was re-dispatched the instant the turn ended, found the job still running, answered
|
|
272
|
-
* `[SILENT]`, and after three such wakes was disabled by the governor. A `waiting` task that
|
|
273
|
-
* *does* carry a wake is an ordinary timed re-check and stays gated on that wake; an
|
|
274
|
-
* unparseable wake still fails open so a hand-edited file surfaces rather than parks forever.
|
|
275
|
-
*/
|
|
276
|
-
/** `waiting` with no `wake`: the driver will not come back on its own. See `isTaskActionable`. */
|
|
277
|
-
export function isTaskParked(frontmatter) {
|
|
278
|
-
return frontmatter.readable && frontmatter.status === "waiting" && !frontmatter.wake;
|
|
279
|
-
}
|
|
280
|
-
export function isTaskActionable(frontmatter, now) {
|
|
281
|
-
if (!frontmatter.readable)
|
|
282
|
-
return true;
|
|
283
|
-
if (frontmatter.controlReadable === false)
|
|
284
|
-
return true;
|
|
285
|
-
if (frontmatter.archiveOutcome || frontmatter.enabled === false)
|
|
286
|
-
return false;
|
|
287
|
-
// status is already canonicalised by parseTaskFrontmatter.
|
|
288
|
-
if (frontmatter.status === "sleeping") {
|
|
289
|
-
const wakeAt = parseWakeMs(frontmatter);
|
|
290
|
-
return wakeAt !== undefined && wakeAt <= now;
|
|
291
|
-
}
|
|
292
|
-
if (isTaskParked(frontmatter))
|
|
293
|
-
return false;
|
|
294
|
-
const wakeAt = parseWakeMs(frontmatter);
|
|
295
|
-
if (wakeAt !== undefined && wakeAt > now)
|
|
296
|
-
return false;
|
|
297
|
-
return true;
|
|
298
|
-
}
|
|
299
213
|
/** First `# ` heading after the frontmatter block, or the id when there is none. */
|
|
300
214
|
export function extractTaskTitle(content, fallbackId) {
|
|
301
215
|
for (const line of taskBody(content).split("\n")) {
|
|
@@ -305,7 +219,7 @@ export function extractTaskTitle(content, fallbackId) {
|
|
|
305
219
|
}
|
|
306
220
|
return fallbackId;
|
|
307
221
|
}
|
|
308
|
-
function matchesTaskSectionTitle(title, names) {
|
|
222
|
+
export function matchesTaskSectionTitle(title, names) {
|
|
309
223
|
const normalized = title.trim().toLowerCase();
|
|
310
224
|
return names.some((name) => {
|
|
311
225
|
const expected = name.toLowerCase();
|
|
@@ -321,7 +235,7 @@ function matchesTaskSectionTitle(title, names) {
|
|
|
321
235
|
* (Plan, Current Cycle, DoD, Verification) shares this exact rule; only what a caller does with
|
|
322
236
|
* the bounds differs.
|
|
323
237
|
*/
|
|
324
|
-
function findTaskSectionBounds(lines, names) {
|
|
238
|
+
export function findTaskSectionBounds(lines, names) {
|
|
325
239
|
for (let index = 0; index < lines.length; index++) {
|
|
326
240
|
const match = /^(#{1,6})\s+(.+?)\s*$/.exec(lines[index] ?? "");
|
|
327
241
|
if (!match || !matchesTaskSectionTitle(match[2] ?? "", names))
|
|
@@ -433,6 +347,11 @@ export function countTaskDodItems(content) {
|
|
|
433
347
|
function ensurePlanStepId(line, fallbackIndex) {
|
|
434
348
|
return PLAN_STEP_ID_PREFIX.test(line) ? line : `P${fallbackIndex} ${line}`;
|
|
435
349
|
}
|
|
350
|
+
/**
|
|
351
|
+
* The v4 task skeleton: contract only (spec 051, D5). `## Current Cycle` and `## History` are
|
|
352
|
+
* gone — the per-step record lives in `<id>.jsonl`, and the one paragraph the contract keeps
|
|
353
|
+
* (`## 上次结果`) is written by the cycle-close path, not by the skeleton.
|
|
354
|
+
*/
|
|
436
355
|
export function renderStandardTaskBody(input) {
|
|
437
356
|
const manual = input.manual?.trim() || DEFAULT_TASK_MANUAL;
|
|
438
357
|
const verificationPlan = input.verificationPlan?.trim() ||
|
|
@@ -464,364 +383,20 @@ export function renderStandardTaskBody(input) {
|
|
|
464
383
|
...(planLines.length > 0
|
|
465
384
|
? ["## Plan", ...planLines.map((line, index) => `- [ ] ${ensurePlanStepId(line, index + 1)}`), ""]
|
|
466
385
|
: []),
|
|
467
|
-
"## Current Cycle",
|
|
468
|
-
"- Created; next step: start work and append progress here before ending each turn.",
|
|
469
|
-
"",
|
|
470
|
-
"## History",
|
|
471
|
-
"",
|
|
472
386
|
].join("\n");
|
|
473
387
|
}
|
|
474
|
-
/**
|
|
475
|
-
* Enforce the v3 write-path invariants. Runtime due transitions are intentionally separate: a
|
|
476
|
-
* waiting task is first written active, and a sleeping task is first opened into a cycle, before
|
|
477
|
-
* the driver dispatches anything.
|
|
478
|
-
*/
|
|
479
|
-
export function normalizeTaskFields(fields, now = new Date()) {
|
|
480
|
-
const next = {
|
|
481
|
-
...fields,
|
|
482
|
-
status: normalizeStoredStatus(fields.status),
|
|
483
|
-
enabled: fields.enabled !== false,
|
|
484
|
-
};
|
|
485
|
-
if (next.outcome) {
|
|
486
|
-
// Archive documents are never driver inputs. Do not carry a live stop marker into them.
|
|
487
|
-
next.enabled = undefined;
|
|
488
|
-
return next;
|
|
489
|
-
}
|
|
490
|
-
if (next.enabled === true && next.control?.stop) {
|
|
491
|
-
next.control = { ...next.control, stop: undefined };
|
|
492
|
-
}
|
|
493
|
-
if (next.enabled === false) {
|
|
494
|
-
// Pausing is intentionally lossless: do not normalize status, wake, or schedule while disabled.
|
|
495
|
-
// A hand-written disabled task without a stop receipt is diagnosed rather than given a fabricated actor.
|
|
496
|
-
return next;
|
|
497
|
-
}
|
|
498
|
-
const nowMs = now.getTime();
|
|
499
|
-
const wakeMs = next.wake ? parseLocalTime(next.wake) : undefined;
|
|
500
|
-
if (next.status === "active") {
|
|
501
|
-
if (wakeMs !== undefined && wakeMs > nowMs) {
|
|
502
|
-
next.status = "waiting";
|
|
503
|
-
next.control = next.control ? { ...next.control, waitingFor: "time" } : undefined;
|
|
504
|
-
}
|
|
505
|
-
else {
|
|
506
|
-
next.wake = undefined;
|
|
507
|
-
next.control = next.control ? { ...next.control, waitingFor: undefined } : undefined;
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
if (next.status === "waiting") {
|
|
511
|
-
if (next.wake && wakeMs !== undefined) {
|
|
512
|
-
const waitingFor = next.control?.waitingFor === "external-signal" ? "external-signal" : "time";
|
|
513
|
-
next.control = next.control ? { ...next.control, waitingFor } : undefined;
|
|
514
|
-
}
|
|
515
|
-
else if (!next.wake && next.control) {
|
|
516
|
-
const waitingFor = next.control.waitingFor === "time" ? "external-signal" : next.control.waitingFor;
|
|
517
|
-
next.control = { ...next.control, waitingFor: waitingFor ?? "external-signal" };
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
if (next.status === "sleeping" && next.schedule) {
|
|
521
|
-
const occurrence = next.wake ? parseLocalTime(next.wake) : undefined;
|
|
522
|
-
const validOccurrence = occurrence !== undefined && nextTaskWake(next.schedule, new Date(occurrence - 1))?.getTime() === occurrence;
|
|
523
|
-
if (!validOccurrence) {
|
|
524
|
-
const upcoming = nextTaskWake(next.schedule, now);
|
|
525
|
-
if (upcoming)
|
|
526
|
-
next.wake = formatLocalTime(upcoming);
|
|
527
|
-
}
|
|
528
|
-
if (next.control?.waitingFor) {
|
|
529
|
-
next.control = { ...next.control, waitingFor: undefined };
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
return next;
|
|
533
|
-
}
|
|
534
|
-
/**
|
|
535
|
-
* Whether an open recurring cycle has already crossed its next schedule occurrence.
|
|
536
|
-
* The runtime must keep working on the old cycle rather than opening a concurrent one.
|
|
537
|
-
* Cycle ids intentionally carry the local calendar date; the optional suffix handles a
|
|
538
|
-
* same-day manual reopen deterministically.
|
|
539
|
-
*/
|
|
540
|
-
export function recurringTaskMissedOccurrence(fields, now = new Date()) {
|
|
541
|
-
if (!fields.schedule || (fields.status !== "active" && fields.status !== "waiting") || !fields.control?.cycleId) {
|
|
542
|
-
return false;
|
|
543
|
-
}
|
|
544
|
-
const match = /^cycle-(\d{4})-(\d{2})-(\d{2})(?:-(\d+))?$/.exec(fields.control.cycleId);
|
|
545
|
-
if (!match)
|
|
546
|
-
return false;
|
|
547
|
-
const start = new Date(Number(match[1]), Number(match[2]) - 1, Number(match[3]));
|
|
548
|
-
if (!Number.isFinite(start.getTime()))
|
|
549
|
-
return false;
|
|
550
|
-
let occurrence = nextTaskWake(fields.schedule, new Date(start.getTime() - 1));
|
|
551
|
-
const count = match[4] ? Number.parseInt(match[4], 10) : 1;
|
|
552
|
-
for (let index = 1; occurrence && index < count; index++) {
|
|
553
|
-
occurrence = nextTaskWake(fields.schedule, occurrence);
|
|
554
|
-
}
|
|
555
|
-
const next = occurrence ? nextTaskWake(fields.schedule, occurrence) : undefined;
|
|
556
|
-
return next !== undefined && next.getTime() <= now.getTime();
|
|
557
|
-
}
|
|
388
|
+
/** Serialize a task document: v4 frontmatter block plus the body, unchanged. */
|
|
558
389
|
export function renderTaskDocument(fields, rawBody) {
|
|
559
|
-
|
|
560
|
-
const lines = ["---"];
|
|
561
|
-
if (document.outcome) {
|
|
562
|
-
lines.push(`outcome: ${document.outcome}`);
|
|
563
|
-
if (document.closedAt)
|
|
564
|
-
lines.push(`closedAt: ${document.closedAt}`);
|
|
565
|
-
}
|
|
566
|
-
else {
|
|
567
|
-
lines.push(`status: ${document.status}`, `enabled: ${document.enabled !== false}`);
|
|
568
|
-
}
|
|
569
|
-
if (document.wake)
|
|
570
|
-
lines.push(`wake: ${document.wake}`);
|
|
571
|
-
if (document.schedule)
|
|
572
|
-
lines.push(`schedule: ${document.schedule}`);
|
|
573
|
-
if (document.control)
|
|
574
|
-
lines.push(`control: ${JSON.stringify(document.control)}`);
|
|
575
|
-
lines.push("---");
|
|
576
|
-
return `${lines.join("\n")}\n${rawBody}`;
|
|
577
|
-
}
|
|
578
|
-
/** Append one progress bullet to the standard Current Cycle section. */
|
|
579
|
-
export function appendCurrentCycleNote(content, note) {
|
|
580
|
-
const trimmedNote = note.trim().replace(/\s+/g, " ");
|
|
581
|
-
if (!trimmedNote) {
|
|
582
|
-
throw new Error("Current Cycle note must not be empty.");
|
|
583
|
-
}
|
|
584
|
-
const lines = content.split("\n");
|
|
585
|
-
const bounds = findTaskSectionBounds(lines, CURRENT_CYCLE_SECTION_NAMES);
|
|
586
|
-
if (!bounds) {
|
|
587
|
-
throw new Error('Task body has no "Current Cycle" section; normalize the task skeleton first.');
|
|
588
|
-
}
|
|
589
|
-
let insertAt = bounds.end;
|
|
590
|
-
while (insertAt > bounds.headingIndex + 1 && (lines[insertAt - 1] ?? "").trim() === "") {
|
|
591
|
-
insertAt--;
|
|
592
|
-
}
|
|
593
|
-
lines.splice(insertAt, 0, `- ${trimmedNote}`);
|
|
594
|
-
return lines.join("\n");
|
|
390
|
+
return `${renderTaskFrontmatter(fields)}\n${rawBody}`;
|
|
595
391
|
}
|
|
596
392
|
/**
|
|
597
|
-
*
|
|
598
|
-
*
|
|
599
|
-
*
|
|
600
|
-
*
|
|
601
|
-
* attempts from appending duplicate evidence blocks.
|
|
393
|
+
* Reset a recurring task's per-cycle checkboxes: DoD/Verification acceptance items and Plan
|
|
394
|
+
* steps both go back to unchecked, so cycle N+1 can never pass its acceptance gate on cycle N's
|
|
395
|
+
* evidence. `[~]` dropped plan steps are left alone — that step was deliberately abandoned, not
|
|
396
|
+
* merely finished, and a new cycle should not resurrect it.
|
|
602
397
|
*/
|
|
603
|
-
export function
|
|
604
|
-
const lines =
|
|
605
|
-
const currentBounds = findTaskSectionBounds(lines, CURRENT_CYCLE_SECTION_NAMES);
|
|
606
|
-
if (!currentBounds || currentBounds.level >= 6) {
|
|
607
|
-
// Legacy/non-standard one-shot tasks were historically completable without the
|
|
608
|
-
// canonical skeleton. Keep that compatibility, but upsert one bounded top-level
|
|
609
|
-
// block instead of returning to the old append-only behavior.
|
|
610
|
-
const stripped = stripLegacyCompletionEvidence(content).content.replace(/\n+$/, "");
|
|
611
|
-
return `${stripped}\n\n## Completion Evidence\n\n${evidenceLines.join("\n")}\n`;
|
|
612
|
-
}
|
|
613
|
-
const currentStart = currentBounds.headingIndex;
|
|
614
|
-
const currentLevel = currentBounds.level;
|
|
615
|
-
let currentEnd = currentBounds.end;
|
|
616
|
-
const evidenceLevel = currentLevel + 1;
|
|
617
|
-
let evidenceStart = -1;
|
|
618
|
-
let evidenceEnd = -1;
|
|
619
|
-
for (let index = currentStart + 1; index < currentEnd; index++) {
|
|
620
|
-
const match = /^(#{1,6})\s+(.+?)\s*$/.exec(lines[index] ?? "");
|
|
621
|
-
if (!match)
|
|
622
|
-
continue;
|
|
623
|
-
const level = match[1]?.length ?? 7;
|
|
624
|
-
if (level === evidenceLevel && matchesTaskSectionTitle(match[2] ?? "", ["Completion Evidence"])) {
|
|
625
|
-
evidenceStart = index;
|
|
626
|
-
evidenceEnd = currentEnd;
|
|
627
|
-
for (let nested = index + 1; nested < currentEnd; nested++) {
|
|
628
|
-
const nestedHeading = /^(#{1,6})\s+/.exec(lines[nested] ?? "");
|
|
629
|
-
if (nestedHeading && (nestedHeading[1]?.length ?? 7) <= evidenceLevel) {
|
|
630
|
-
evidenceEnd = nested;
|
|
631
|
-
break;
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
break;
|
|
635
|
-
}
|
|
636
|
-
}
|
|
637
|
-
if (evidenceStart !== -1) {
|
|
638
|
-
lines.splice(evidenceStart, evidenceEnd - evidenceStart);
|
|
639
|
-
currentEnd -= evidenceEnd - evidenceStart;
|
|
640
|
-
}
|
|
641
|
-
while (currentEnd > currentStart + 1 && (lines[currentEnd - 1] ?? "").trim() === "")
|
|
642
|
-
currentEnd--;
|
|
643
|
-
const block = [`${"#".repeat(evidenceLevel)} Completion Evidence`, "", ...evidenceLines, ""];
|
|
644
|
-
lines.splice(currentEnd, 0, ...block);
|
|
645
|
-
return lines.join("\n");
|
|
646
|
-
}
|
|
647
|
-
function stripLegacyCompletionEvidence(content) {
|
|
648
|
-
const lines = content.split("\n");
|
|
649
|
-
const blocks = [];
|
|
650
|
-
for (let index = 0; index < lines.length;) {
|
|
651
|
-
const heading = /^(#{1,6})\s+(.+?)\s*$/.exec(lines[index] ?? "");
|
|
652
|
-
if (!heading ||
|
|
653
|
-
(heading[1]?.length ?? 0) !== 2 ||
|
|
654
|
-
!matchesTaskSectionTitle(heading[2] ?? "", ["Completion Evidence"])) {
|
|
655
|
-
index++;
|
|
656
|
-
continue;
|
|
657
|
-
}
|
|
658
|
-
let end = lines.length;
|
|
659
|
-
for (let nested = index + 1; nested < lines.length; nested++) {
|
|
660
|
-
const nextHeading = /^(#{1,6})\s+/.exec(lines[nested] ?? "");
|
|
661
|
-
if (nextHeading && (nextHeading[1]?.length ?? 7) <= 2) {
|
|
662
|
-
end = nested;
|
|
663
|
-
break;
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
blocks.push(lines.slice(index, end).join("\n").trim());
|
|
667
|
-
lines.splice(index, end - index);
|
|
668
|
-
while (index > 0 &&
|
|
669
|
-
index < lines.length &&
|
|
670
|
-
(lines[index - 1] ?? "").trim() === "" &&
|
|
671
|
-
(lines[index] ?? "").trim() === "") {
|
|
672
|
-
lines.splice(index, 1);
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
return { content: lines.join("\n"), blocks };
|
|
676
|
-
}
|
|
677
|
-
function clipHistoryEntry(entry) {
|
|
678
|
-
if (entry.length <= MAX_INLINE_TASK_HISTORY_ENTRY_CHARS)
|
|
679
|
-
return entry.trim();
|
|
680
|
-
const suffix = `\n${HISTORY_TRUNCATION_NOTE}`;
|
|
681
|
-
const limit = Math.max(0, MAX_INLINE_TASK_HISTORY_ENTRY_CHARS - suffix.length);
|
|
682
|
-
let clipped = entry.slice(0, limit);
|
|
683
|
-
const lastNewline = clipped.lastIndexOf("\n");
|
|
684
|
-
if (lastNewline > 0)
|
|
685
|
-
clipped = clipped.slice(0, lastNewline);
|
|
686
|
-
return `${clipped.trimEnd()}${suffix}`;
|
|
687
|
-
}
|
|
688
|
-
function compactTaskHistory(content, alreadyOmitted = false) {
|
|
689
|
-
const lines = content.split("\n");
|
|
690
|
-
let historyStart = -1;
|
|
691
|
-
let historyLevel = 0;
|
|
692
|
-
for (let index = 0; index < lines.length; index++) {
|
|
693
|
-
const heading = /^(#{1,6})\s+(.+?)\s*$/.exec(lines[index] ?? "");
|
|
694
|
-
if (heading && matchesTaskSectionTitle(heading[2] ?? "", ["History", "历史"])) {
|
|
695
|
-
historyStart = index;
|
|
696
|
-
historyLevel = heading[1]?.length ?? 0;
|
|
697
|
-
break;
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
if (historyStart === -1 || historyLevel >= 6)
|
|
701
|
-
return content;
|
|
702
|
-
let historyEnd = lines.length;
|
|
703
|
-
for (let index = historyStart + 1; index < lines.length; index++) {
|
|
704
|
-
const heading = /^(#{1,6})\s+/.exec(lines[index] ?? "");
|
|
705
|
-
if (heading && (heading[1]?.length ?? 7) <= historyLevel) {
|
|
706
|
-
historyEnd = index;
|
|
707
|
-
break;
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
const entryStarts = [];
|
|
711
|
-
for (let index = historyStart + 1; index < historyEnd; index++) {
|
|
712
|
-
const heading = /^(#{1,6})\s+/.exec(lines[index] ?? "");
|
|
713
|
-
if ((heading?.[1]?.length ?? 0) === historyLevel + 1)
|
|
714
|
-
entryStarts.push(index);
|
|
715
|
-
}
|
|
716
|
-
if (entryStarts.length === 0)
|
|
717
|
-
return content;
|
|
718
|
-
const entries = entryStarts.map((start, index) => clipHistoryEntry(lines
|
|
719
|
-
.slice(start, entryStarts[index + 1] ?? historyEnd)
|
|
720
|
-
.join("\n")
|
|
721
|
-
.trim()));
|
|
722
|
-
const kept = [];
|
|
723
|
-
let chars = 0;
|
|
724
|
-
for (const entry of entries) {
|
|
725
|
-
const nextChars = chars + entry.length + (kept.length > 0 ? 2 : 0);
|
|
726
|
-
if (kept.length >= MAX_INLINE_TASK_HISTORY_ENTRIES || nextChars > MAX_INLINE_TASK_HISTORY_CHARS)
|
|
727
|
-
break;
|
|
728
|
-
kept.push(entry);
|
|
729
|
-
chars = nextChars;
|
|
730
|
-
}
|
|
731
|
-
const omitted = alreadyOmitted || kept.length < entries.length;
|
|
732
|
-
const replacement = [
|
|
733
|
-
"",
|
|
734
|
-
...kept.flatMap((entry, index) => (index === kept.length - 1 ? [entry] : [entry, ""])),
|
|
735
|
-
...(omitted ? ["", HISTORY_OMISSION_NOTE] : []),
|
|
736
|
-
"",
|
|
737
|
-
];
|
|
738
|
-
lines.splice(historyStart + 1, historyEnd - historyStart - 1, ...replacement);
|
|
739
|
-
return lines.join("\n");
|
|
740
|
-
}
|
|
741
|
-
/**
|
|
742
|
-
* Close the visible current-cycle notes into History and open a fresh cycle.
|
|
743
|
-
* Periodic task cycles deliberately use this small, deterministic transformation
|
|
744
|
-
* instead of asking the model to hand-edit headings and risk appending future
|
|
745
|
-
* checkpoints to the previous cycle.
|
|
746
|
-
*/
|
|
747
|
-
export function startTaskCycle(content, cycleId, archivePrevious = true) {
|
|
748
|
-
const normalizedCycleId = cycleId.trim();
|
|
749
|
-
if (!normalizedCycleId)
|
|
750
|
-
throw new Error("Cycle id must not be empty.");
|
|
751
|
-
const legacy = stripLegacyCompletionEvidence(content);
|
|
752
|
-
const lines = legacy.content.split("\n");
|
|
753
|
-
let currentStart = -1;
|
|
754
|
-
let currentLevel = 0;
|
|
755
|
-
let historyStart = -1;
|
|
756
|
-
for (let index = 0; index < lines.length; index++) {
|
|
757
|
-
const match = /^(#{1,6})\s+(.+?)\s*$/.exec(lines[index] ?? "");
|
|
758
|
-
if (!match)
|
|
759
|
-
continue;
|
|
760
|
-
const level = match[1]?.length ?? 0;
|
|
761
|
-
if (matchesTaskSectionTitle(match[2] ?? "", CURRENT_CYCLE_SECTION_NAMES)) {
|
|
762
|
-
currentStart = index;
|
|
763
|
-
currentLevel = level;
|
|
764
|
-
}
|
|
765
|
-
if (matchesTaskSectionTitle(match[2] ?? "", ["History", "历史"])) {
|
|
766
|
-
historyStart = index;
|
|
767
|
-
break;
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
if (currentStart === -1 || historyStart === -1 || historyStart <= currentStart) {
|
|
771
|
-
throw new Error('Task body needs ordered "Current Cycle" and "History" sections before starting a new cycle.');
|
|
772
|
-
}
|
|
773
|
-
let currentEnd = historyStart;
|
|
774
|
-
for (let index = currentStart + 1; index < historyStart; index++) {
|
|
775
|
-
const match = /^(#{1,6})\s+/.exec(lines[index] ?? "");
|
|
776
|
-
if (match && (match[1]?.length ?? 7) <= currentLevel) {
|
|
777
|
-
currentEnd = index;
|
|
778
|
-
break;
|
|
779
|
-
}
|
|
780
|
-
}
|
|
781
|
-
let previous = lines
|
|
782
|
-
.slice(currentStart + 1, currentEnd)
|
|
783
|
-
.join("\n")
|
|
784
|
-
.trim();
|
|
785
|
-
const latestLegacyEvidence = legacy.blocks.at(-1);
|
|
786
|
-
if (latestLegacyEvidence && !/^(#{1,6})\s+Completion Evidence\b/im.test(previous)) {
|
|
787
|
-
previous = `${previous}\n\n${latestLegacyEvidence.replace(/^##\s+/, "### ")}`.trim();
|
|
788
|
-
}
|
|
789
|
-
// The archived cycle gets its own H3 entry under History. Demote subsections that
|
|
790
|
-
// were nested under the H2 Current Cycle so they remain children of that entry.
|
|
791
|
-
previous = previous
|
|
792
|
-
.split("\n")
|
|
793
|
-
.map((line) => {
|
|
794
|
-
const heading = /^(#{1,6})(\s+.*)$/.exec(line);
|
|
795
|
-
const level = heading?.[1]?.length ?? 0;
|
|
796
|
-
return heading && level > currentLevel && level < 6 ? `#${line}` : line;
|
|
797
|
-
})
|
|
798
|
-
.join("\n");
|
|
799
|
-
const previousHeading = (lines[currentStart] ?? "## Current Cycle").replace(/^#+\s*/, "").trim();
|
|
800
|
-
const replacement = [
|
|
801
|
-
`${"#".repeat(currentLevel)} Current Cycle (${normalizedCycleId})`,
|
|
802
|
-
"- Cycle started; next step: follow the Manual and checkpoint concrete progress.",
|
|
803
|
-
];
|
|
804
|
-
lines.splice(currentStart, currentEnd - currentStart, ...replacement);
|
|
805
|
-
const shiftedHistoryStart = historyStart + replacement.length - (currentEnd - currentStart);
|
|
806
|
-
if (archivePrevious && previous && !/^[-*]\s+Created; next step:/i.test(previous)) {
|
|
807
|
-
const historyEntry = [`### ${previousHeading} — closed`, previous, ""];
|
|
808
|
-
// A nested history heading would be unusual; inserting directly after the
|
|
809
|
-
// canonical History heading remains predictable and preserves all older notes.
|
|
810
|
-
lines.splice(shiftedHistoryStart + 1, 0, ...historyEntry);
|
|
811
|
-
}
|
|
812
|
-
const compacted = compactTaskHistory(lines.join("\n"), legacy.blocks.length > 1);
|
|
813
|
-
return resetPlanStepCheckboxes(resetTaskAcceptanceCheckboxes(compacted));
|
|
814
|
-
}
|
|
815
|
-
/**
|
|
816
|
-
* Uncheck every "- [x]" under DoD/Verification.
|
|
817
|
-
*
|
|
818
|
-
* `startTaskCycle` archives the previous cycle's log but never touched these boxes, so a
|
|
819
|
-
* periodic task that finished cycle 1 with a fully checked DoD would open cycle 2 with
|
|
820
|
-
* `uncheckedTaskAcceptanceItems` reporting zero unchecked items — the acceptance gate would
|
|
821
|
-
* silently pass on stale evidence from a cycle that no longer exists.
|
|
822
|
-
*/
|
|
823
|
-
function resetTaskAcceptanceCheckboxes(content) {
|
|
824
|
-
const lines = content.split("\n");
|
|
398
|
+
export function resetTaskPlanForCycle(body) {
|
|
399
|
+
const lines = body.split("\n");
|
|
825
400
|
const sectionOf = classifyTaskSectionLines(lines, DOD_VERIFICATION_SECTIONS);
|
|
826
401
|
for (let index = 0; index < lines.length; index++) {
|
|
827
402
|
if (!sectionOf[index])
|
|
@@ -830,90 +405,56 @@ function resetTaskAcceptanceCheckboxes(content) {
|
|
|
830
405
|
if (checkbox)
|
|
831
406
|
lines[index] = `${checkbox[1]}[ ]${checkbox[2]}`;
|
|
832
407
|
}
|
|
833
|
-
return lines.join("\n");
|
|
834
|
-
}
|
|
835
|
-
/**
|
|
836
|
-
* Reset `## Plan` steps for a new cycle: `[x]` done and `[!]` blocked both go back to `[ ]`
|
|
837
|
-
* todo, exactly like `resetTaskAcceptanceCheckboxes` — a stale "done" step from the previous
|
|
838
|
-
* cycle must not silently pass as still true. `[~]` dropped is left alone: that step was
|
|
839
|
-
* deliberately abandoned, not merely finished, and a new cycle should not resurrect it.
|
|
840
|
-
*/
|
|
841
|
-
function resetPlanStepCheckboxes(content) {
|
|
842
|
-
const lines = content.split("\n");
|
|
843
408
|
const bounds = findTaskSectionBounds(lines, PLAN_SECTION_NAMES);
|
|
844
|
-
if (
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
lines[index] = `${checkbox[1]}[ ]${checkbox[2]}`;
|
|
850
|
-
}
|
|
851
|
-
return lines.join("\n");
|
|
852
|
-
}
|
|
853
|
-
/** Last non-empty bullet/line under a "当前周期"/"current cycle" heading. */
|
|
854
|
-
function extractLatestNote(content) {
|
|
855
|
-
const lines = content.split("\n");
|
|
856
|
-
let inSection = false;
|
|
857
|
-
let sectionLevel = 0;
|
|
858
|
-
let latest;
|
|
859
|
-
for (const line of lines) {
|
|
860
|
-
const heading = /^(#{1,6})\s+(.+?)\s*$/.exec(line);
|
|
861
|
-
if (heading) {
|
|
862
|
-
if (inSection && (heading[1]?.length ?? 7) <= sectionLevel)
|
|
863
|
-
break;
|
|
864
|
-
if (!inSection && matchesTaskSectionTitle(heading[2] ?? "", CURRENT_CYCLE_SECTION_NAMES)) {
|
|
865
|
-
inSection = true;
|
|
866
|
-
sectionLevel = heading[1]?.length ?? 0;
|
|
867
|
-
}
|
|
868
|
-
continue;
|
|
409
|
+
if (bounds) {
|
|
410
|
+
for (let index = bounds.headingIndex + 1; index < bounds.end; index++) {
|
|
411
|
+
const checkbox = /^(\s*[-*]\s+)\[[xX!]\](\s*.*)$/.exec(lines[index] ?? "");
|
|
412
|
+
if (checkbox)
|
|
413
|
+
lines[index] = `${checkbox[1]}[ ]${checkbox[2]}`;
|
|
869
414
|
}
|
|
870
|
-
if (!inSection)
|
|
871
|
-
continue;
|
|
872
|
-
const trimmed = line.replace(/^\s*[-*]\s+/, "").trim();
|
|
873
|
-
if (trimmed)
|
|
874
|
-
latest = trimmed;
|
|
875
415
|
}
|
|
876
|
-
return
|
|
877
|
-
}
|
|
878
|
-
function parseWakeMs(frontmatter) {
|
|
879
|
-
if (!frontmatter.wake)
|
|
880
|
-
return undefined;
|
|
881
|
-
return parseLocalTime(frontmatter.wake);
|
|
416
|
+
return lines.join("\n");
|
|
882
417
|
}
|
|
883
|
-
/**
|
|
418
|
+
/**
|
|
419
|
+
* Runnable first; then a parked task whose backstop already expired (the runtime owes it a
|
|
420
|
+
* reopen); then earliest due ticket; then id. Sorting expired parks ahead of ordinary parks is
|
|
421
|
+
* what keeps D2-INV's deadline honest when a channel has more ready work than one tick can carry.
|
|
422
|
+
*/
|
|
884
423
|
export function compareTaskEntries(a, b) {
|
|
885
|
-
if (a.
|
|
886
|
-
return a.
|
|
887
|
-
if (a.
|
|
888
|
-
return a.
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
: Number.POSITIVE_INFINITY;
|
|
896
|
-
if (ad !== bd)
|
|
897
|
-
return ad - bd;
|
|
898
|
-
}
|
|
899
|
-
const aw = a.wakeMs ?? Number.NEGATIVE_INFINITY;
|
|
900
|
-
const bw = b.wakeMs ?? Number.NEGATIVE_INFINITY;
|
|
901
|
-
if (aw !== bw)
|
|
902
|
-
return aw - bw;
|
|
424
|
+
if (a.runnable !== b.runnable)
|
|
425
|
+
return a.runnable ? -1 : 1;
|
|
426
|
+
if (a.expired !== b.expired)
|
|
427
|
+
return a.expired ? -1 : 1;
|
|
428
|
+
const paused = (entry) => (entry.fields.paused ? 1 : 0);
|
|
429
|
+
if (paused(a) !== paused(b))
|
|
430
|
+
return paused(a) - paused(b);
|
|
431
|
+
const due = (entry) => entry.dueMs ?? Number.POSITIVE_INFINITY;
|
|
432
|
+
if (due(a) !== due(b))
|
|
433
|
+
return due(a) - due(b);
|
|
903
434
|
return a.id.localeCompare(b.id);
|
|
904
435
|
}
|
|
905
|
-
function toEntry(id, content
|
|
906
|
-
const
|
|
436
|
+
function toEntry(id, content) {
|
|
437
|
+
const parsed = parseTaskFrontmatterV4(content);
|
|
907
438
|
return {
|
|
908
439
|
id,
|
|
909
440
|
title: extractTaskTitle(content, id),
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
latestNote: extractLatestNote(content),
|
|
441
|
+
fields: parsed.fields,
|
|
442
|
+
readable: parsed.readable,
|
|
443
|
+
legacy: parsed.legacy,
|
|
914
444
|
plan: parseTaskPlan(content),
|
|
915
445
|
};
|
|
916
446
|
}
|
|
447
|
+
/** The clock-dependent half of an entry, recomputed on every read (never cached). */
|
|
448
|
+
function withClock(entry, now) {
|
|
449
|
+
const nowDate = new Date(now);
|
|
450
|
+
const ticket = entry.fields.ticket;
|
|
451
|
+
return {
|
|
452
|
+
...entry,
|
|
453
|
+
runnable: isTaskRunnable(entry.fields),
|
|
454
|
+
dueMs: ticket ? ticketDueMs(ticket) : undefined,
|
|
455
|
+
expired: ticket !== undefined && !entry.fields.paused && ticketExpired(ticket, nowDate),
|
|
456
|
+
};
|
|
457
|
+
}
|
|
917
458
|
const parseCache = new Map();
|
|
918
459
|
/** Bound the cache for a long-lived daemon: far above any real ledger, and cheap to refill. */
|
|
919
460
|
const PARSE_CACHE_LIMIT = 512;
|
|
@@ -921,18 +462,18 @@ async function readEntry(path, id, now) {
|
|
|
921
462
|
const stats = await stat(path);
|
|
922
463
|
const cached = parseCache.get(path);
|
|
923
464
|
if (cached && cached.mtimeMs === stats.mtimeMs && cached.ctimeMs === stats.ctimeMs && cached.size === stats.size) {
|
|
924
|
-
return
|
|
465
|
+
return withClock(cached.entry, now);
|
|
925
466
|
}
|
|
926
|
-
const entry = toEntry(id, await readFile(path, "utf-8")
|
|
467
|
+
const entry = toEntry(id, await readFile(path, "utf-8"));
|
|
927
468
|
if (parseCache.size >= PARSE_CACHE_LIMIT)
|
|
928
469
|
parseCache.clear();
|
|
929
470
|
parseCache.set(path, { mtimeMs: stats.mtimeMs, ctimeMs: stats.ctimeMs, size: stats.size, entry });
|
|
930
|
-
return entry;
|
|
471
|
+
return withClock(entry, now);
|
|
931
472
|
}
|
|
932
473
|
/**
|
|
933
474
|
* Read every `.md` file in `tasks/` (root only — the `archive/` subdirectory is not
|
|
934
|
-
* scanned), returning entries sorted
|
|
935
|
-
*
|
|
475
|
+
* scanned), returning entries sorted runnable-first. A file that cannot be read is still
|
|
476
|
+
* returned (fail-open: `readable: false`, `runnable: true`) so problems surface.
|
|
936
477
|
* Missing directory → empty list.
|
|
937
478
|
*/
|
|
938
479
|
export async function readActiveTasks(tasksDir, now = Date.now()) {
|
|
@@ -957,8 +498,11 @@ export async function readActiveTasks(tasksDir, now = Date.now()) {
|
|
|
957
498
|
entries.push({
|
|
958
499
|
id,
|
|
959
500
|
title: id,
|
|
960
|
-
|
|
961
|
-
|
|
501
|
+
fields: { state: "open" },
|
|
502
|
+
readable: false,
|
|
503
|
+
legacy: false,
|
|
504
|
+
runnable: true,
|
|
505
|
+
expired: false,
|
|
962
506
|
});
|
|
963
507
|
}
|
|
964
508
|
}
|