@oyasmi/pipiclaw 0.7.6 → 0.8.0

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.
Files changed (48) hide show
  1. package/README.md +13 -0
  2. package/dist/agent/channel-runner.d.ts +3 -0
  3. package/dist/agent/channel-runner.js +14 -4
  4. package/dist/agent/commands.js +8 -2
  5. package/dist/agent/prompt-builder.js +46 -0
  6. package/dist/agent/session-events.js +5 -0
  7. package/dist/agent/types.d.ts +2 -0
  8. package/dist/memory/recall.js +6 -0
  9. package/dist/memory/task-digest.js +17 -0
  10. package/dist/models/api-keys.d.ts +5 -1
  11. package/dist/models/api-keys.js +10 -4
  12. package/dist/runtime/bootstrap.d.ts +5 -0
  13. package/dist/runtime/bootstrap.js +59 -2
  14. package/dist/runtime/dingtalk.d.ts +2 -0
  15. package/dist/runtime/dingtalk.js +13 -1
  16. package/dist/runtime/durable-dispatch.d.ts +28 -0
  17. package/dist/runtime/durable-dispatch.js +144 -0
  18. package/dist/runtime/events.d.ts +4 -2
  19. package/dist/runtime/events.js +12 -8
  20. package/dist/runtime/task-commands.d.ts +9 -0
  21. package/dist/runtime/task-commands.js +429 -2
  22. package/dist/runtime/task-driver.d.ts +32 -0
  23. package/dist/runtime/task-driver.js +270 -0
  24. package/dist/settings.d.ts +12 -0
  25. package/dist/settings.js +30 -0
  26. package/dist/shared/atomic-file.js +23 -3
  27. package/dist/shared/task-events.d.ts +32 -0
  28. package/dist/shared/task-events.js +29 -0
  29. package/dist/shared/task-ledger.d.ts +53 -0
  30. package/dist/shared/task-ledger.js +246 -8
  31. package/dist/subagents/tool.d.ts +13 -0
  32. package/dist/subagents/tool.js +229 -15
  33. package/dist/tasks/artifact-subject.d.ts +6 -0
  34. package/dist/tasks/artifact-subject.js +29 -0
  35. package/dist/tasks/control.d.ts +83 -0
  36. package/dist/tasks/control.js +292 -0
  37. package/dist/tasks/store.d.ts +32 -0
  38. package/dist/tasks/store.js +142 -0
  39. package/dist/tasks/verification.d.ts +23 -0
  40. package/dist/tasks/verification.js +68 -0
  41. package/dist/tools/registry.d.ts +1 -1
  42. package/dist/tools/registry.js +6 -4
  43. package/dist/tools/task-manage.d.ts +51 -3
  44. package/dist/tools/task-manage.js +491 -37
  45. package/dist/tools/web-fetch.js +3 -0
  46. package/dist/tui/app.js +1 -1
  47. package/dist/tui/commands.js +1 -1
  48. package/package.json +1 -1
@@ -1,7 +1,17 @@
1
1
  import { readdir, readFile } from "node:fs/promises";
2
2
  import { join } from "node:path";
3
- const FRONTMATTER_FIELDS = ["status", "wake", "recurrence"];
3
+ import { parseTaskControl, taskPriorityRank } from "../tasks/control.js";
4
+ const FRONTMATTER_FIELDS = ["status", "wake", "recurrence", "control"];
4
5
  const TASK_ID_PATTERN = /^[A-Za-z0-9._-]+$/;
6
+ export const DEFAULT_TASK_MANUAL = "- Follow the DoD, keep the current cycle log updated, and schedule check-ins when waiting.";
7
+ export const STANDARD_TASK_SECTIONS = [
8
+ { label: "Goal", names: ["Goal", "目标"] },
9
+ { label: "DoD", names: ["DoD"] },
10
+ { label: "Manual", names: ["Manual", "手册"] },
11
+ { label: "Verification", names: ["Verification", "验收"] },
12
+ { label: "Current Cycle", names: ["Current Cycle", "当前周期"] },
13
+ { label: "History", names: ["History", "历史"] },
14
+ ];
5
15
  /** Validate/normalize a task id (filename without `.md`), rejecting path traversal. */
6
16
  export function normalizeTaskId(id) {
7
17
  const trimmed = id.trim();
@@ -37,7 +47,23 @@ export function parseTaskFrontmatter(content) {
37
47
  const key = line.slice(0, idx).trim();
38
48
  if (FRONTMATTER_FIELDS.includes(key)) {
39
49
  const value = line.slice(idx + 1).trim();
40
- frontmatter[key] = value || undefined;
50
+ if (key === "control") {
51
+ if (!value) {
52
+ frontmatter.controlReadable = false;
53
+ }
54
+ else {
55
+ try {
56
+ frontmatter.control = parseTaskControl(value);
57
+ frontmatter.controlReadable = true;
58
+ }
59
+ catch {
60
+ frontmatter.controlReadable = false;
61
+ }
62
+ }
63
+ }
64
+ else {
65
+ frontmatter[key] = value || undefined;
66
+ }
41
67
  }
42
68
  }
43
69
  return frontmatter;
@@ -50,8 +76,14 @@ export function parseTaskFrontmatter(content) {
50
76
  export function isTaskActionable(frontmatter, now) {
51
77
  if (!frontmatter.readable)
52
78
  return true;
53
- if (frontmatter.status === "done")
79
+ if (frontmatter.controlReadable === false)
80
+ return true;
81
+ if (frontmatter.status === "done" ||
82
+ frontmatter.status === "cancelled" ||
83
+ frontmatter.status === "escalated" ||
84
+ frontmatter.status === "paused") {
54
85
  return false;
86
+ }
55
87
  if (frontmatter.wake) {
56
88
  const wakeAt = new Date(frontmatter.wake).getTime();
57
89
  if (Number.isFinite(wakeAt) && wakeAt > now)
@@ -68,22 +100,214 @@ export function extractTaskTitle(content, fallbackId) {
68
100
  }
69
101
  return fallbackId;
70
102
  }
71
- /** First non-empty bullet/line under a "当前周期"/"current cycle" heading. */
103
+ function matchesTaskSectionTitle(title, names) {
104
+ const normalized = title.trim().toLowerCase();
105
+ return names.some((name) => {
106
+ const expected = name.toLowerCase();
107
+ return (normalized === expected ||
108
+ normalized.startsWith(`${expected} `) ||
109
+ normalized.startsWith(`${expected}(`) ||
110
+ normalized.startsWith(`${expected}(`));
111
+ });
112
+ }
113
+ export function hasTaskHeading(content, names) {
114
+ return content.split("\n").some((line) => {
115
+ const match = /^#{1,6}\s+(.+?)\s*$/.exec(line);
116
+ return match ? matchesTaskSectionTitle(match[1] ?? "", names) : false;
117
+ });
118
+ }
119
+ export function missingStandardTaskSections(content) {
120
+ return STANDARD_TASK_SECTIONS.filter((section) => !hasTaskHeading(content, section.names)).map((section) => section.label);
121
+ }
122
+ /** Unchecked Markdown acceptance boxes under DoD or Verification sections. */
123
+ export function uncheckedTaskAcceptanceItems(content) {
124
+ const unchecked = [];
125
+ let section;
126
+ let sectionLevel = 0;
127
+ for (const line of content.split("\n")) {
128
+ const heading = /^(#{1,6})\s+(.+?)\s*$/.exec(line);
129
+ if (heading) {
130
+ const level = heading[1]?.length ?? 7;
131
+ if (matchesTaskSectionTitle(heading[2] ?? "", ["DoD"])) {
132
+ section = "DoD";
133
+ sectionLevel = level;
134
+ }
135
+ else if (matchesTaskSectionTitle(heading[2] ?? "", ["Verification", "验收"])) {
136
+ section = "Verification";
137
+ sectionLevel = level;
138
+ }
139
+ else if (section && level <= sectionLevel) {
140
+ section = undefined;
141
+ }
142
+ continue;
143
+ }
144
+ if (!section)
145
+ continue;
146
+ const item = /^\s*[-*]\s+\[\s\]\s*(.+?)\s*$/.exec(line)?.[1];
147
+ if (item)
148
+ unchecked.push(`${section}: ${item}`);
149
+ }
150
+ return unchecked;
151
+ }
152
+ export function renderStandardTaskBody(input) {
153
+ const manual = input.manual?.trim() || DEFAULT_TASK_MANUAL;
154
+ const verificationPlan = input.verificationPlan?.trim() ||
155
+ "- Check every DoD item against concrete evidence.\n- Run the relevant deterministic checks before declaring PASS.";
156
+ const verificationMode = input.verificationMode ?? "independent";
157
+ return [
158
+ `# ${input.title}`,
159
+ "",
160
+ "## Goal",
161
+ input.goal,
162
+ "",
163
+ "## DoD",
164
+ input.dod,
165
+ "",
166
+ "## Manual",
167
+ manual,
168
+ "",
169
+ "## Verification",
170
+ `Mode: ${verificationMode}`,
171
+ verificationPlan,
172
+ "",
173
+ "## Current Cycle",
174
+ "- Created; next step: start work and append progress here before ending each turn.",
175
+ "",
176
+ "## History",
177
+ "",
178
+ ].join("\n");
179
+ }
180
+ export function renderTaskDocument(fields, body) {
181
+ const lines = ["---", `status: ${fields.status}`];
182
+ if (fields.wake)
183
+ lines.push(`wake: ${fields.wake}`);
184
+ if (fields.recurrence)
185
+ lines.push(`recurrence: ${fields.recurrence}`);
186
+ if (fields.control)
187
+ lines.push(`control: ${JSON.stringify(fields.control)}`);
188
+ lines.push("---");
189
+ return `${lines.join("\n")}\n${body}`;
190
+ }
191
+ /** Append one progress bullet to the standard Current Cycle section. */
192
+ export function appendCurrentCycleNote(content, note) {
193
+ const trimmedNote = note.trim().replace(/\s+/g, " ");
194
+ if (!trimmedNote) {
195
+ throw new Error("Current Cycle note must not be empty.");
196
+ }
197
+ const lines = content.split("\n");
198
+ let sectionStart = -1;
199
+ let sectionLevel = 0;
200
+ for (let index = 0; index < lines.length; index++) {
201
+ const match = /^(#{1,6})\s+(.+?)\s*$/.exec(lines[index] ?? "");
202
+ if (!match || !matchesTaskSectionTitle(match[2] ?? "", ["Current Cycle", "当前周期"]))
203
+ continue;
204
+ sectionStart = index;
205
+ sectionLevel = match[1]?.length ?? 0;
206
+ }
207
+ if (sectionStart === -1) {
208
+ throw new Error('Task body has no "Current Cycle" section; normalize the task skeleton first.');
209
+ }
210
+ let insertAt = lines.length;
211
+ for (let index = sectionStart + 1; index < lines.length; index++) {
212
+ const match = /^(#{1,6})\s+/.exec(lines[index] ?? "");
213
+ if (match && (match[1]?.length ?? 7) <= sectionLevel) {
214
+ insertAt = index;
215
+ break;
216
+ }
217
+ }
218
+ while (insertAt > sectionStart + 1 && (lines[insertAt - 1] ?? "").trim() === "") {
219
+ insertAt--;
220
+ }
221
+ lines.splice(insertAt, 0, `- ${trimmedNote}`);
222
+ return lines.join("\n");
223
+ }
224
+ /**
225
+ * Close the visible current-cycle notes into History and open a fresh cycle.
226
+ * Periodic task cycles deliberately use this small, deterministic transformation
227
+ * instead of asking the model to hand-edit headings and risk appending future
228
+ * checkpoints to the previous cycle.
229
+ */
230
+ export function startTaskCycle(content, cycleId) {
231
+ const normalizedCycleId = cycleId.trim();
232
+ if (!normalizedCycleId)
233
+ throw new Error("Cycle id must not be empty.");
234
+ const lines = content.split("\n");
235
+ let currentStart = -1;
236
+ let currentLevel = 0;
237
+ let historyStart = -1;
238
+ let historyLevel = 0;
239
+ for (let index = 0; index < lines.length; index++) {
240
+ const match = /^(#{1,6})\s+(.+?)\s*$/.exec(lines[index] ?? "");
241
+ if (!match)
242
+ continue;
243
+ const level = match[1]?.length ?? 0;
244
+ if (matchesTaskSectionTitle(match[2] ?? "", ["Current Cycle", "当前周期"])) {
245
+ currentStart = index;
246
+ currentLevel = level;
247
+ }
248
+ if (matchesTaskSectionTitle(match[2] ?? "", ["History", "历史"])) {
249
+ historyStart = index;
250
+ historyLevel = level;
251
+ break;
252
+ }
253
+ }
254
+ if (currentStart === -1 || historyStart === -1 || historyStart <= currentStart) {
255
+ throw new Error('Task body needs ordered "Current Cycle" and "History" sections before starting a new cycle.');
256
+ }
257
+ let currentEnd = historyStart;
258
+ for (let index = currentStart + 1; index < historyStart; index++) {
259
+ const match = /^(#{1,6})\s+/.exec(lines[index] ?? "");
260
+ if (match && (match[1]?.length ?? 7) <= currentLevel) {
261
+ currentEnd = index;
262
+ break;
263
+ }
264
+ }
265
+ const previous = lines
266
+ .slice(currentStart + 1, currentEnd)
267
+ .join("\n")
268
+ .trim();
269
+ const previousHeading = (lines[currentStart] ?? "## Current Cycle").replace(/^#+\s*/, "").trim();
270
+ const replacement = [
271
+ `${"#".repeat(currentLevel)} Current Cycle (${normalizedCycleId})`,
272
+ "- Cycle started; next step: follow the Manual and checkpoint concrete progress.",
273
+ ];
274
+ lines.splice(currentStart, currentEnd - currentStart, ...replacement);
275
+ const shiftedHistoryStart = historyStart + replacement.length - (currentEnd - currentStart);
276
+ const historyEntry = [
277
+ `### ${previousHeading} — closed`,
278
+ ...(previous ? [previous] : ["- No checkpoint was recorded in the previous cycle."]),
279
+ "",
280
+ ];
281
+ // A nested history heading would be unusual; inserting directly after the
282
+ // canonical History heading remains predictable and preserves all older notes.
283
+ void historyLevel;
284
+ lines.splice(shiftedHistoryStart + 1, 0, ...historyEntry);
285
+ return lines.join("\n");
286
+ }
287
+ /** Last non-empty bullet/line under a "当前周期"/"current cycle" heading. */
72
288
  function extractLatestNote(content) {
73
289
  const lines = content.split("\n");
74
290
  let inSection = false;
291
+ let sectionLevel = 0;
292
+ let latest;
75
293
  for (const line of lines) {
76
- if (/^#{1,6}\s/.test(line)) {
77
- inSection = /当前周期|current cycle/i.test(line);
294
+ const heading = /^(#{1,6})\s+(.+?)\s*$/.exec(line);
295
+ if (heading) {
296
+ if (inSection && (heading[1]?.length ?? 7) <= sectionLevel)
297
+ break;
298
+ if (!inSection && matchesTaskSectionTitle(heading[2] ?? "", ["Current Cycle", "当前周期"])) {
299
+ inSection = true;
300
+ sectionLevel = heading[1]?.length ?? 0;
301
+ }
78
302
  continue;
79
303
  }
80
304
  if (!inSection)
81
305
  continue;
82
306
  const trimmed = line.replace(/^\s*[-*]\s+/, "").trim();
83
307
  if (trimmed)
84
- return trimmed;
308
+ latest = trimmed;
85
309
  }
86
- return undefined;
310
+ return latest;
87
311
  }
88
312
  function wakeMsOf(frontmatter) {
89
313
  if (!frontmatter.wake)
@@ -95,6 +319,20 @@ function wakeMsOf(frontmatter) {
95
319
  export function compareTaskEntries(a, b) {
96
320
  if (a.actionable !== b.actionable)
97
321
  return a.actionable ? -1 : 1;
322
+ if (a.actionable && b.actionable) {
323
+ const ap = taskPriorityRank(a.frontmatter.control?.priority ?? "normal");
324
+ const bp = taskPriorityRank(b.frontmatter.control?.priority ?? "normal");
325
+ if (ap !== bp)
326
+ return ap - bp;
327
+ const ad = a.frontmatter.control?.deadline
328
+ ? new Date(a.frontmatter.control.deadline).getTime()
329
+ : Number.POSITIVE_INFINITY;
330
+ const bd = b.frontmatter.control?.deadline
331
+ ? new Date(b.frontmatter.control.deadline).getTime()
332
+ : Number.POSITIVE_INFINITY;
333
+ if (ad !== bd)
334
+ return ad - bd;
335
+ }
98
336
  const aw = a.wakeMs ?? Number.NEGATIVE_INFINITY;
99
337
  const bw = b.wakeMs ?? Number.NEGATIVE_INFINITY;
100
338
  if (aw !== bw)
@@ -23,6 +23,10 @@ declare const subagentSchema: Type.TObject<{
23
23
  contextMode: Type.TOptional<Type.TUnion<[Type.TLiteral<"isolated">, Type.TLiteral<"contextual">]>>;
24
24
  memory: Type.TOptional<Type.TUnion<[Type.TLiteral<"none">, Type.TLiteral<"session">, Type.TLiteral<"relevant">]>>;
25
25
  paths: Type.TOptional<Type.TArray<Type.TString>>;
26
+ purpose: Type.TOptional<Type.TUnion<[Type.TLiteral<"work">, Type.TLiteral<"verify">]>>;
27
+ taskId: Type.TOptional<Type.TString>;
28
+ isolation: Type.TOptional<Type.TUnion<[Type.TLiteral<"shared">, Type.TLiteral<"worktree">]>>;
29
+ worktreePath: Type.TOptional<Type.TString>;
26
30
  }>;
27
31
  export interface SubAgentToolDetails {
28
32
  kind: "subagent";
@@ -36,9 +40,18 @@ export interface SubAgentToolDetails {
36
40
  failed: boolean;
37
41
  failureReason?: string;
38
42
  usage: UsageTotals;
43
+ runId: string;
44
+ purpose: "work" | "verify";
45
+ taskId?: string;
46
+ isolation: "shared" | "worktree";
47
+ worktreePath?: string;
48
+ worktreeBranch?: string;
49
+ verificationVerdict?: "pass" | "fail";
39
50
  }
40
51
  export interface SubAgentToolOptions {
41
52
  executor: Executor;
53
+ /** Host checkout used as the shared cwd and worktree source. Defaults to process.cwd(). */
54
+ workingDirectory?: string;
42
55
  getCurrentModel: () => Model<Api>;
43
56
  getAvailableModels: () => Model<Api>[];
44
57
  resolveApiKey: (model: Model<Api>) => Promise<string>;