@andares/pi-toolkits 0.14.0 → 1.0.1

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 (59) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +101 -34
  3. package/README.zh-CN.md +68 -32
  4. package/package.json +10 -3
  5. package/src/features/ask/index.test.ts +15 -0
  6. package/src/features/ask/index.ts +18 -1
  7. package/src/features/init/audit.test.ts +19 -2
  8. package/src/features/init/audit.ts +117 -15
  9. package/src/features/init/index.test.ts +123 -1
  10. package/src/features/init/index.ts +156 -46
  11. package/src/features/init/parity.test.ts +176 -0
  12. package/src/features/init/parity.ts +229 -0
  13. package/src/features/init/plan.ts +25 -3
  14. package/src/features/init/refresh.test.ts +88 -1
  15. package/src/features/init/refresh.ts +43 -64
  16. package/src/features/init/spec.ts +11 -0
  17. package/src/features/init/templates/memory-readme.md +9 -2
  18. package/src/features/init/templates/workflow-block.md +2 -2
  19. package/src/features/init/test-helpers.ts +1 -0
  20. package/src/features/init/workflow.ts +159 -0
  21. package/src/features/permission/index.ts +1 -1
  22. package/src/features/quit-guard/constants.ts +18 -5
  23. package/src/features/quit-guard/index.ts +75 -29
  24. package/src/features/quit-guard/killer.test.ts +77 -0
  25. package/src/features/quit-guard/killer.ts +215 -0
  26. package/src/features/review/index.ts +14 -0
  27. package/src/features/review/review.test.ts +34 -2
  28. package/src/features/spec/assets/index-CdTerMvN.css +1 -0
  29. package/src/features/spec/assets/index-Dv52AIjX.js +1 -0
  30. package/src/features/{plan → spec}/assets/index.html +3 -3
  31. package/src/features/{plan → spec}/assets.test.ts +5 -5
  32. package/src/features/{plan → spec}/blocks.ts +1 -1
  33. package/src/features/{plan → spec}/feedback.test.ts +26 -26
  34. package/src/features/{plan → spec}/feedback.ts +14 -14
  35. package/src/features/{plan → spec}/index.test.ts +148 -63
  36. package/src/features/{plan → spec}/index.ts +136 -105
  37. package/src/features/{plan → spec}/prompt.test.ts +20 -20
  38. package/src/features/{plan → spec}/prompt.ts +25 -25
  39. package/src/features/{plan → spec}/render.test.ts +2 -2
  40. package/src/features/{plan → spec}/server.test.ts +21 -21
  41. package/src/features/{plan → spec}/server.ts +22 -22
  42. package/src/features/{plan → spec}/store.test.ts +77 -35
  43. package/src/features/{plan → spec}/store.ts +80 -52
  44. package/src/features/uat/index.test.ts +20 -5
  45. package/src/features/uat/index.ts +16 -13
  46. package/src/features/uat/record-tool.ts +6 -5
  47. package/src/features/uat/records.test.ts +5 -5
  48. package/src/features/uat/state.ts +13 -5
  49. package/src/index.ts +6 -6
  50. package/src/lib/ask-control.ts +2 -2
  51. package/src/lib/permission-control.ts +4 -4
  52. package/src/lib/phases.ts +50 -0
  53. package/src/lib/web-review.ts +1 -1
  54. package/src/features/plan/assets/index-C1YLIi4I.js +0 -1
  55. package/src/features/plan/assets/index-DMEpYC3V.css +0 -1
  56. /package/src/features/{plan → spec}/blocks.test.ts +0 -0
  57. /package/src/features/{plan → spec}/diff.test.ts +0 -0
  58. /package/src/features/{plan → spec}/diff.ts +0 -0
  59. /package/src/features/{plan → spec}/render.ts +0 -0
@@ -33,6 +33,7 @@ import type {
33
33
  } from "@earendil-works/pi-coding-agent";
34
34
  import type { GateHub } from "../../lib/gates.js";
35
35
  import { setAskControl } from "../../lib/ask-control.js";
36
+ import { activePhases, describePhases } from "../../lib/phases.js";
36
37
  // The jailed-tool registry lives here too: ask marks `scratchpad` while the mode
37
38
  // is active so the boundary gate (`permission`) knows it has no path to judge.
38
39
  import {
@@ -185,13 +186,29 @@ export function registerAsk(
185
186
  }
186
187
 
187
188
  // Planning is deliberately less restrictive than consult mode (markdown may
188
- // be written and bash stays available), so `/ask-plan` exits through the
189
+ // be written and bash stays available), so `/ask-spec` exits through the
189
190
  // owning feature rather than trying to restore its tool set itself.
190
191
  setAskControl({ isActive: () => enabled, exit });
191
192
 
192
193
  pi.registerCommand("ask", {
193
194
  description: "Toggle ask mode (consult mode: no project writes, scratch notes allowed)",
194
195
  handler: async (_args, ctx) => {
196
+ if (!enabled) {
197
+ if (ctx.isIdle?.() === false) {
198
+ ctx.ui.notify("Ask mode: the agent is still running — wait for it to finish.", "warning");
199
+ return;
200
+ }
201
+ // A phase already owns a write surface; entering a read-only mode on top
202
+ // would be a promise the other phase's before_agent_start can break.
203
+ const phases = activePhases(pi).filter((phase) => phase !== "ask");
204
+ if (phases.length > 0) {
205
+ ctx.ui.notify(
206
+ `Ask mode cannot start while ${describePhases(phases)} is active — leave it first.`,
207
+ "warning",
208
+ );
209
+ return;
210
+ }
211
+ }
195
212
  toggle(ctx);
196
213
  },
197
214
  });
@@ -4,6 +4,7 @@ import { join } from "node:path";
4
4
  import { audit, countFindings, latestCoverageReport, renderFindings } from "./audit.js";
5
5
  import { detect } from "./detect.js";
6
6
  import { AUDIT_CODES, REPORTS_DIR } from "./spec.js";
7
+ import { workflowBlockText } from "./parity.js";
7
8
  import { gitProject, installedProject, project, remove } from "./test-helpers.js";
8
9
 
9
10
  function codes(root: string): string[] {
@@ -26,8 +27,11 @@ const CLEAN: Record<string, string> = {
26
27
  "| `.agents/memory/FUTURE.md` | deferred |",
27
28
  "| `.agents/memory/ARCHITECTURE.md` | decisions |",
28
29
  "",
30
+ workflowBlockText(),
29
31
  ].join("\n"),
30
32
  "specs/PLAN.md": "# Plan\n\n## Route\n\n- next\n",
33
+ ".agents/README.md": "# `.agents/` — agent-facing material\n",
34
+ ".agents/framework/.gitignore": "reports/\n",
31
35
  ".agents/memory/README.md": "# Agent memory framework v1\n",
32
36
  ".agents/memory/FINISHED.md":
33
37
  "# Finished\n\n## Summary\n\n- init — `.agents/implementations/2026-01-01_init.md`\n",
@@ -58,8 +62,21 @@ describe("audit", () => {
58
62
  }
59
63
  });
60
64
 
61
- it("reports bare legacy references", () => {
65
+ it("reports bare legacy references as a warning when there is nothing to move", () => {
62
66
  const root = project({ ...CLEAN, "src/a.ts": "// see memory/DESIGN.md\n" });
67
+ try {
68
+ expect(codes(root)).toContain(AUDIT_CODES.pathBareRefStale);
69
+ } finally {
70
+ remove(root);
71
+ }
72
+ });
73
+
74
+ it("keeps bare references an error while a legacy directory still exists", () => {
75
+ const root = project({
76
+ ...CLEAN,
77
+ "memory/PLAN.md": "# old plan\n",
78
+ "src/a.ts": "// see memory/DESIGN.md\n",
79
+ });
63
80
  try {
64
81
  expect(codes(root)).toContain(AUDIT_CODES.pathBareRef);
65
82
  } finally {
@@ -225,7 +242,7 @@ describe("audit", () => {
225
242
  expect(findings.map((item) => item.code)).toContain(AUDIT_CODES.scaffoldLeftover);
226
243
  const text = renderFindings(findings, { root, frameworkVersion: 1 });
227
244
  expect(text).toMatch(/errors/);
228
- expect(text).toContain(AUDIT_CODES.pathBareRef);
245
+ expect(text).toContain(AUDIT_CODES.pathBareRefStale);
229
246
  } finally {
230
247
  remove(root);
231
248
  }
@@ -9,10 +9,10 @@
9
9
  * It is read-only and never "fixes" anything itself.
10
10
  */
11
11
  import { execFileSync } from "node:child_process";
12
- import { createHash } from "node:crypto";
13
12
  import { existsSync, readFileSync, readdirSync } from "node:fs";
14
13
  import { join, posix } from "node:path";
15
14
  import {
15
+ AGENTS_FILE,
16
16
  AUDIT_CODES,
17
17
  BUDGETS,
18
18
  CONTRACT_FILE,
@@ -36,6 +36,7 @@ import {
36
36
  import { parseCoverage } from "./coverage.js";
37
37
  import { deriveState, ledgerPath, readEvents } from "../uat/ledger.js";
38
38
  import { listRecords } from "../uat/records.js";
39
+ import { inspectFramework } from "./parity.js";
39
40
  import { staleRoots } from "./plan.js";
40
41
  import type { DocInfo, Facts, Finding, Severity } from "./types.js";
41
42
 
@@ -213,11 +214,21 @@ function checkRefs(facts: Facts): Finding[] {
213
214
  .slice(0, 6)
214
215
  .map(([area, bucket]) => `${area} (${bucket.files} files / ${bucket.hits})`);
215
216
  const example = facts.refs.examples[0];
217
+ // The message says "would break on a move". That is only an error while a
218
+ // legacy directory is still there to be moved; otherwise the references are
219
+ // stale documentation and a warning is the honest severity (the init feature
220
+ // itself, and test fixtures, often quote the old names deliberately).
221
+ const legacyPresent = facts.deprecated.memory || facts.deprecated.implementations;
222
+ const code = legacyPresent ? AUDIT_CODES.pathBareRef : AUDIT_CODES.pathBareRefStale;
223
+ const severity: Severity = legacyPresent ? "error" : "warning";
224
+ const note = legacyPresent
225
+ ? "would break on a move"
226
+ : "the legacy directories are already gone, so nothing breaks; update them when convenient";
216
227
  return [
217
228
  finding(
218
- AUDIT_CODES.pathBareRef,
219
- "error",
220
- `${facts.refs.hits} bare path references in ${facts.refs.paths.length} files would break on a move: ${areas.join(", ")}${example ? `; e.g. ${example.path}:${example.line}` : ""}`,
229
+ code,
230
+ severity,
231
+ `${facts.refs.hits} bare path references in ${facts.refs.paths.length} files ${note}: ${areas.join(", ")}${example ? `; e.g. ${example.path}:${example.line}` : ""}`,
221
232
  example?.path,
222
233
  example?.line,
223
234
  ),
@@ -538,24 +549,115 @@ const DAY_MS = 86_400_000;
538
549
  * instead.
539
550
  */
540
551
  function checkFrameworkFiles(facts: Facts): Finding[] {
541
- const state = facts.state;
542
- if (!state) return [];
552
+ if (!facts.state) return [];
553
+ const parity = inspectFramework(facts.root, facts.state);
543
554
  const out: Finding[] = [];
544
- for (const entry of state.managed) {
545
- if (entry.path === "" || entry.templateSha256 === undefined) continue;
546
- const absolute = join(facts.root, entry.path);
547
- if (!existsSync(absolute)) continue;
548
- const hash = createHash("sha256").update(readFileSync(absolute)).digest("hex");
549
- if (hash === entry.templateSha256) continue;
555
+ for (const entry of parity.managed) {
556
+ switch (entry.status) {
557
+ case "hand-edited":
558
+ out.push(
559
+ finding(
560
+ AUDIT_CODES.managedEdited,
561
+ "warning",
562
+ "framework file was edited by hand: /ask-init refresh will not overwrite it — keep your additions outside the framework-owned text, or re-apply them after a refresh",
563
+ entry.path,
564
+ ),
565
+ );
566
+ break;
567
+ case "stale":
568
+ out.push(
569
+ finding(
570
+ AUDIT_CODES.managedStale,
571
+ "warning",
572
+ "framework file is behind the installed template: /ask-init (or refresh) will update it",
573
+ entry.path,
574
+ ),
575
+ );
576
+ break;
577
+ case "missing":
578
+ // The UAT contract is optional and additive; `checkUat` reports it as
579
+ // I-UAT-01. Do not double-report it as a broken managed file.
580
+ if (entry.path === UAT_CONTRACT_FILE) break;
581
+ out.push(
582
+ finding(
583
+ AUDIT_CODES.managedStale,
584
+ "warning",
585
+ "framework file is missing: /ask-init (or refresh) will create it",
586
+ entry.path,
587
+ ),
588
+ );
589
+ break;
590
+ case "unverifiable":
591
+ out.push(
592
+ finding(
593
+ AUDIT_CODES.legacyAnchor,
594
+ "info",
595
+ `framework file cannot be compared with the installed template (${entry.reason ?? "no anchor"}): if the bytes are the framework's own text, run /ask-init refresh --adopt to take ownership`,
596
+ entry.path,
597
+ ),
598
+ );
599
+ break;
600
+ case "current":
601
+ break;
602
+ }
603
+ }
604
+
605
+ for (const rel of parity.requiredMissing) {
550
606
  out.push(
551
607
  finding(
552
- AUDIT_CODES.managedEdited,
608
+ AUDIT_CODES.frameworkFileMissing,
553
609
  "warning",
554
- "framework file was edited by hand: /ask-init refresh will not overwrite it — keep your additions outside the framework-owned text, or re-apply them after a refresh",
555
- entry.path,
610
+ "required framework file is missing: /ask-init can rebuild it from the seed/template",
611
+ rel,
556
612
  ),
557
613
  );
558
614
  }
615
+
616
+ switch (parity.workflow) {
617
+ case "missing":
618
+ out.push(
619
+ finding(
620
+ AUDIT_CODES.workflowBlock,
621
+ "warning",
622
+ "AGENTS.md has no ask workflow block: /ask-init will insert it",
623
+ AGENTS_FILE,
624
+ ),
625
+ );
626
+ break;
627
+ case "stale":
628
+ out.push(
629
+ finding(
630
+ AUDIT_CODES.workflowBlock,
631
+ "warning",
632
+ "the ask workflow block in AGENTS.md is behind the installed template: /ask-init will update it",
633
+ AGENTS_FILE,
634
+ ),
635
+ );
636
+ break;
637
+ case "hand-written":
638
+ out.push(
639
+ finding(
640
+ AUDIT_CODES.workflowBlock,
641
+ "warning",
642
+ "AGENTS.md has a hand-written 'Ask workflow' section without the managed markers: /ask-init will not add a second one; add the markers or remove the section",
643
+ AGENTS_FILE,
644
+ ),
645
+ );
646
+ break;
647
+ case "malformed":
648
+ out.push(
649
+ finding(
650
+ AUDIT_CODES.workflowBlock,
651
+ "warning",
652
+ "the ask workflow markers in AGENTS.md are inconsistent: fix the marker pair before /ask-init can refresh the block",
653
+ AGENTS_FILE,
654
+ ),
655
+ );
656
+ break;
657
+ case "missing-agents":
658
+ case "current":
659
+ break;
660
+ }
559
661
  return out;
560
662
  }
561
663
 
@@ -1,9 +1,15 @@
1
1
  import { describe, expect, it } from "vitest";
2
- import { existsSync, mkdtempSync, readFileSync } from "node:fs";
2
+ import { createHash } from "node:crypto";
3
+ import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
3
4
  import { homedir, tmpdir } from "node:os";
4
5
  import { join } from "node:path";
5
6
  import { addedRootsFromBranch, parseInitArgs, registerInit } from "./index.js";
7
+ import { stateToToml } from "./apply.js";
8
+ import { MANAGED_SPECS, renderManaged } from "./parity.js";
9
+ import { CONTRACT_FILE } from "./spec.js";
10
+ import { setAskControl } from "../../lib/ask-control.js";
6
11
  import { installedProject, project, remove } from "./test-helpers.js";
12
+ import type { FrameworkState } from "./types.js";
7
13
  import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
8
14
 
9
15
  interface HarnessOptions {
@@ -85,6 +91,8 @@ describe("parseInitArgs", () => {
85
91
  expect(parseInitArgs("roots").sub).toBe("roots");
86
92
  expect(parseInitArgs("scrub").sub).toBe("scrub");
87
93
  expect(parseInitArgs("plan --deep").sub).toBe("plan");
94
+ expect(parseInitArgs("refresh --adopt").adopt).toBe(true);
95
+ expect(parseInitArgs("refresh").adopt).toBe(false);
88
96
  });
89
97
 
90
98
  it("treats --dry-run as the plan subcommand", () => {
@@ -220,6 +228,120 @@ describe("/ask-init", () => {
220
228
  }
221
229
  });
222
230
 
231
+ it("does not recommend reload after a dry-run refresh", async () => {
232
+ const root = installedProject();
233
+ try {
234
+ const h = harness(root);
235
+ registerInit(h.pi, { now: NOW, toolVersion: "test" });
236
+ await h.run("refresh --dry-run");
237
+ expect(h.messages.some((item) => item.message.includes("/reload"))).toBe(false);
238
+ expect(existsSync(join(root, ".agents/uat/README.md"))).toBe(false);
239
+ } finally {
240
+ remove(root);
241
+ }
242
+ });
243
+
244
+ it("refreshes framework-owned text on a repeated run and then reports current", async () => {
245
+ const root = installedProject();
246
+ const rendered = renderManaged(MANAGED_SPECS.find((spec) => spec.path === CONTRACT_FILE)!);
247
+ const hash = (text: string) => createHash("sha256").update(text).digest("hex");
248
+ writeFileSync(join(root, CONTRACT_FILE), rendered, "utf8");
249
+ const state: FrameworkState = {
250
+ version: 1,
251
+ tool: "0.0.0-test",
252
+ created: "2026-01-01T00:00:00+08:00",
253
+ updated: "2026-01-01T00:00:00+08:00",
254
+ profile: "core",
255
+ modules: [],
256
+ roots: [],
257
+ managed: [
258
+ {
259
+ path: CONTRACT_FILE,
260
+ sha256: hash(rendered),
261
+ template: "memory-readme",
262
+ templateSha256: hash(rendered),
263
+ },
264
+ ],
265
+ migrations: [],
266
+ };
267
+ writeFileSync(join(root, ".agents/framework/state.toml"), stateToToml(state), "utf8");
268
+ try {
269
+ const first = harness(root);
270
+ registerInit(first.pi, { now: NOW, toolVersion: "test" });
271
+ await first.run("--yes");
272
+ expect(first.sent).toHaveLength(0);
273
+ expect(existsSync(join(root, ".agents/uat/README.md"))).toBe(true);
274
+ expect(first.messages.some((item) => /created|refreshed/.test(item.message))).toBe(true);
275
+
276
+ const second = harness(root);
277
+ registerInit(second.pi, { now: NOW, toolVersion: "test" });
278
+ await second.run("--yes");
279
+ expect(second.sent).toHaveLength(0);
280
+ expect(second.messages.some((item) => /already current|no framework files changed/.test(item.message))).toBe(true);
281
+ } finally {
282
+ remove(root);
283
+ }
284
+ });
285
+
286
+ it("rebuilds a missing AGENTS.md through the normal model work order", async () => {
287
+ const root = installedProject();
288
+ rmSync(join(root, "AGENTS.md"));
289
+ try {
290
+ const h = harness(root);
291
+ registerInit(h.pi, { now: NOW, toolVersion: "test" });
292
+ await h.run("--yes");
293
+ expect(existsSync(join(root, "AGENTS.md"))).toBe(true);
294
+ expect(h.sent.length).toBeGreaterThan(0);
295
+ expect(h.sent[0]).toContain("fill-agents");
296
+ } finally {
297
+ remove(root);
298
+ }
299
+ });
300
+
301
+ it("rebuilds a missing core memory seed without a model turn", async () => {
302
+ const root = installedProject();
303
+ rmSync(join(root, ".agents/memory/FINISHED.md"));
304
+ try {
305
+ const h = harness(root);
306
+ registerInit(h.pi, { now: NOW, toolVersion: "test" });
307
+ await h.run("--yes");
308
+ expect(existsSync(join(root, ".agents/memory/FINISHED.md"))).toBe(true);
309
+ expect(h.sent).toHaveLength(0);
310
+ expect(h.messages.some((item) => /mechanical repair/.test(item.message))).toBe(true);
311
+ } finally {
312
+ remove(root);
313
+ }
314
+ });
315
+
316
+ it("refuses mutating runs while a phase owns the tree", async () => {
317
+ const root = installedProject();
318
+ try {
319
+ const plan = harness(root);
320
+ plan.tools.push("spec_submit");
321
+ registerInit(plan.pi, { now: NOW, toolVersion: "test" });
322
+ await plan.run("--yes");
323
+ expect(plan.sent).toHaveLength(0);
324
+ expect(plan.messages.some((item) => /ask-spec/.test(item.message))).toBe(true);
325
+
326
+ const uat = harness(root);
327
+ uat.tools.push("uat_item");
328
+ registerInit(uat.pi, { now: NOW, toolVersion: "test" });
329
+ await uat.run("--yes");
330
+ expect(uat.sent).toHaveLength(0);
331
+ expect(uat.messages.some((item) => /ask-uat/.test(item.message))).toBe(true);
332
+
333
+ setAskControl({ isActive: () => true, exit: () => {} });
334
+ const ask = harness(root);
335
+ registerInit(ask.pi, { now: NOW, toolVersion: "test" });
336
+ await ask.run("--yes");
337
+ expect(ask.sent).toHaveLength(0);
338
+ expect(ask.messages.some((item) => /\/ask is active/.test(item.message))).toBe(true);
339
+ } finally {
340
+ setAskControl(undefined);
341
+ remove(root);
342
+ }
343
+ });
344
+
223
345
  it("syncs add-dir roots into the state", async () => {
224
346
  const root = project({ "README.md": "# demo\n" });
225
347
  const sibling = mkdtempSync(join(tmpdir(), "pi-init-root-"));