@andares/pi-toolkits 0.12.4 → 0.14.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 (77) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/README.md +134 -17
  3. package/README.zh-CN.md +115 -16
  4. package/package.json +24 -11
  5. package/src/features/ask/index.ts +6 -0
  6. package/src/features/init/apply.ts +32 -12
  7. package/src/features/init/audit-uat.test.ts +103 -0
  8. package/src/features/init/audit.ts +148 -0
  9. package/src/features/init/detect.ts +4 -0
  10. package/src/features/init/index.ts +47 -2
  11. package/src/features/init/plan.ts +11 -0
  12. package/src/features/init/refresh.test.ts +208 -0
  13. package/src/features/init/refresh.ts +255 -0
  14. package/src/features/init/spec.ts +20 -0
  15. package/src/features/init/templates/agents-skeleton.md +2 -0
  16. package/src/features/init/templates/uat-readme.md +53 -0
  17. package/src/features/init/templates/workflow-block.md +21 -0
  18. package/src/features/init/templates.test.ts +3 -1
  19. package/src/features/init/templates.ts +2 -0
  20. package/src/features/init/types.ts +12 -1
  21. package/src/features/permission/index.ts +32 -0
  22. package/src/features/permission/policy.test.ts +10 -0
  23. package/src/features/permission/units.test.ts +157 -0
  24. package/src/features/permission/units.ts +317 -15
  25. package/src/features/plan/assets/index-C1YLIi4I.js +1 -0
  26. package/src/features/plan/assets/index-DMEpYC3V.css +1 -0
  27. package/src/features/plan/assets/index.html +14 -0
  28. package/src/features/plan/assets.test.ts +64 -0
  29. package/src/features/plan/blocks.test.ts +77 -0
  30. package/src/features/plan/blocks.ts +153 -0
  31. package/src/features/plan/diff.test.ts +144 -0
  32. package/src/features/plan/diff.ts +193 -0
  33. package/src/features/plan/feedback.test.ts +233 -0
  34. package/src/features/plan/feedback.ts +182 -0
  35. package/src/features/plan/index.test.ts +385 -0
  36. package/src/features/plan/index.ts +433 -0
  37. package/src/features/plan/prompt.test.ts +91 -0
  38. package/src/features/plan/prompt.ts +110 -0
  39. package/src/features/plan/render.test.ts +94 -0
  40. package/src/features/plan/render.ts +131 -0
  41. package/src/features/plan/server.test.ts +374 -0
  42. package/src/features/plan/server.ts +272 -0
  43. package/src/features/plan/store.test.ts +145 -0
  44. package/src/features/plan/store.ts +244 -0
  45. package/src/features/review/index.ts +54 -28
  46. package/src/features/review/review.test.ts +46 -1
  47. package/src/features/uat/assets/index-D-W9zj0w.css +1 -0
  48. package/src/features/uat/assets/index-DCBQCsli.js +1 -0
  49. package/src/features/uat/assets/index.html +14 -0
  50. package/src/features/uat/constants.ts +23 -0
  51. package/src/features/uat/index.test.ts +404 -0
  52. package/src/features/uat/index.ts +329 -0
  53. package/src/features/uat/item-tool.ts +280 -0
  54. package/src/features/uat/ledger.test.ts +249 -0
  55. package/src/features/uat/ledger.ts +504 -0
  56. package/src/features/uat/prompt.ts +78 -0
  57. package/src/features/uat/record-tool.ts +167 -0
  58. package/src/features/uat/records.test.ts +145 -0
  59. package/src/features/uat/records.ts +268 -0
  60. package/src/features/uat/server.ts +181 -0
  61. package/src/features/uat/state.test.ts +41 -0
  62. package/src/features/uat/state.ts +67 -0
  63. package/src/features/whats-new/changelog.ts +104 -0
  64. package/src/features/whats-new/constants.ts +35 -0
  65. package/src/features/whats-new/index.ts +146 -0
  66. package/src/features/whats-new/notice.ts +68 -0
  67. package/src/features/whats-new/paths.ts +29 -0
  68. package/src/features/whats-new/store.ts +114 -0
  69. package/src/features/whats-new/version.ts +43 -0
  70. package/src/features/whats-new/whats-new.test.ts +460 -0
  71. package/src/index.ts +13 -1
  72. package/src/lib/ask-control.ts +25 -0
  73. package/src/lib/opener.test.ts +72 -0
  74. package/src/lib/opener.ts +121 -0
  75. package/src/lib/permission-control.ts +39 -0
  76. package/src/lib/startup.test.ts +1 -1
  77. package/src/lib/web-review.ts +341 -0
@@ -32,6 +32,7 @@ import type {
32
32
  ExtensionContext,
33
33
  } from "@earendil-works/pi-coding-agent";
34
34
  import type { GateHub } from "../../lib/gates.js";
35
+ import { setAskControl } from "../../lib/ask-control.js";
35
36
  // The jailed-tool registry lives here too: ask marks `scratchpad` while the mode
36
37
  // is active so the boundary gate (`permission`) knows it has no path to judge.
37
38
  import {
@@ -183,6 +184,11 @@ export function registerAsk(
183
184
  }
184
185
  }
185
186
 
187
+ // Planning is deliberately less restrictive than consult mode (markdown may
188
+ // be written and bash stays available), so `/ask-plan` exits through the
189
+ // owning feature rather than trying to restore its tool set itself.
190
+ setAskControl({ isActive: () => enabled, exit });
191
+
186
192
  pi.registerCommand("ask", {
187
193
  description: "Toggle ask mode (consult mode: no project writes, scratch notes allowed)",
188
194
  handler: async (_args, ctx) => {
@@ -42,6 +42,7 @@ import {
42
42
  STATE_FILE,
43
43
  SCAN,
44
44
  STATE_GITIGNORE,
45
+ UAT_CONTRACT_FILE,
45
46
  } from "./spec.js";
46
47
  import { isBareRef, readDoc, walk } from "./detect.js";
47
48
  import { renderTemplate } from "./templates.js";
@@ -81,6 +82,10 @@ function sha256File(absolute: string): string {
81
82
  return createHash("sha256").update(readFileSync(absolute)).digest("hex");
82
83
  }
83
84
 
85
+ function sha256Text(text: string): string {
86
+ return createHash("sha256").update(text).digest("hex");
87
+ }
88
+
84
89
  function mkdirFor(absolute: string): void {
85
90
  mkdirSync(dirname(absolute), { recursive: true });
86
91
  }
@@ -397,6 +402,7 @@ export function stateToToml(state: FrameworkState): string {
397
402
  path: entry.path,
398
403
  sha256: entry.sha256,
399
404
  template: entry.template,
405
+ template_sha256: entry.templateSha256,
400
406
  })),
401
407
  migrations: state.migrations.map((entry) =>
402
408
  compact({ at: entry.at, to: entry.to, notes: entry.notes }),
@@ -481,6 +487,8 @@ interface ApplyContext {
481
487
  readonly moved: ApplyResult["moved"];
482
488
  readonly created: string[];
483
489
  readonly skipped: ApplyResult["skipped"];
490
+ /** Hash of the bytes we rendered for each file created by this run. */
491
+ readonly managedHashes: Map<string, string>;
484
492
  rewrites?: ApplyResult["rewrites"];
485
493
  snapshot?: ApplyResult["snapshot"];
486
494
  statePath?: string;
@@ -559,8 +567,12 @@ function applyCreateFile(ctx: ApplyContext, action: Action & { kind: "create-fil
559
567
  return;
560
568
  }
561
569
  const body = createFile(ctx.root, action, ctx.language);
562
- if (body === undefined) ctx.skipped.push({ path: action.path, reason: "already exists" });
563
- else ctx.created.push(action.path);
570
+ if (body === undefined) {
571
+ ctx.skipped.push({ path: action.path, reason: "already exists" });
572
+ return;
573
+ }
574
+ ctx.created.push(action.path);
575
+ ctx.managedHashes.set(action.path, sha256Text(body));
564
576
  }
565
577
 
566
578
  function applyWriteState(ctx: ApplyContext, action: Action & { kind: "write-state" }): void {
@@ -568,7 +580,7 @@ function applyWriteState(ctx: ApplyContext, action: Action & { kind: "write-stat
568
580
  ctx.statePath = STATE_FILE;
569
581
  return;
570
582
  }
571
- ctx.statePath = writeState(ctx.root, action.state);
583
+ ctx.statePath = writeState(ctx.root, action.state, ctx.managedHashes);
572
584
  }
573
585
 
574
586
  function runAction(ctx: ApplyContext, action: Action): void {
@@ -609,6 +621,7 @@ export function applyPlan(plan: Plan, options: ApplyOptions = {}): ApplyResult {
609
621
  moved: [],
610
622
  created: [],
611
623
  skipped: [],
624
+ managedHashes: new Map(),
612
625
  };
613
626
 
614
627
  for (const action of plan.actions) runAction(ctx, action);
@@ -765,23 +778,30 @@ export function scrubRetiredFields(root: string, options: ScrubOptions = {}): Sc
765
778
  }
766
779
 
767
780
  /** Write state, refreshing the hashes of files this framework manages. */
768
- function writeState(root: string, state: FrameworkState): string {
769
- const managedPaths = [CONTRACT_FILE, SIGNPOST_FILE, STATE_GITIGNORE];
781
+ function writeState(
782
+ root: string,
783
+ state: FrameworkState,
784
+ managedHashes: Map<string, string> = new Map(),
785
+ ): string {
786
+ const managedPaths = [CONTRACT_FILE, SIGNPOST_FILE, UAT_CONTRACT_FILE, STATE_GITIGNORE];
787
+ const templates: Record<string, string> = {
788
+ [CONTRACT_FILE]: "memory-readme",
789
+ [SIGNPOST_FILE]: "signpost",
790
+ [UAT_CONTRACT_FILE]: "uat-readme",
791
+ [STATE_GITIGNORE]: "state-gitignore",
792
+ };
770
793
  const previous = new Map(state.managed.map((entry) => [entry.path, entry]));
771
794
  const managed: FrameworkState["managed"] = [];
772
795
  for (const rel of managedPaths) {
773
796
  const absolute = inside(root, rel);
774
797
  if (!existsSync(absolute)) continue;
775
- const template =
776
- rel === CONTRACT_FILE
777
- ? "memory-readme"
778
- : rel === SIGNPOST_FILE
779
- ? "signpost"
780
- : "state-gitignore";
781
798
  managed.push({
782
799
  path: rel,
783
800
  sha256: sha256File(absolute),
784
- template: previous.get(rel)?.template ?? template,
801
+ template: previous.get(rel)?.template ?? templates[rel] ?? "",
802
+ // Anchor = the bytes this tool rendered. A file we did not write this
803
+ // run keeps the previous anchor; without one, refresh stays hands-off.
804
+ templateSha256: managedHashes.get(rel) ?? previous.get(rel)?.templateSha256,
785
805
  });
786
806
  }
787
807
  const absolute = inside(root, STATE_FILE);
@@ -0,0 +1,103 @@
1
+ import { createHash } from "node:crypto";
2
+ import { mkdirSync, writeFileSync } from "node:fs";
3
+ import { join } from "node:path";
4
+ import { afterEach, describe, expect, it } from "vitest";
5
+ import { audit } from "./audit.js";
6
+ import { stateToToml } from "./apply.js";
7
+ import { detect } from "./detect.js";
8
+ import { AUDIT_CODES, CONTRACT_FILE, UAT_CONTRACT_FILE } from "./spec.js";
9
+ import { installedProject, remove } from "./test-helpers.js";
10
+
11
+ const roots: string[] = [];
12
+
13
+ function installed(extra: Record<string, string> = {}): string {
14
+ const root = installedProject(extra);
15
+ roots.push(root);
16
+ return root;
17
+ }
18
+
19
+ function codes(root: string): string[] {
20
+ return audit(detect(root)).map((finding) => finding.code);
21
+ }
22
+
23
+ function writeLedger(root: string, lines: string[]): void {
24
+ mkdirSync(join(root, ".agents/uat"), { recursive: true });
25
+ writeFileSync(join(root, ".agents/uat/ledger.jsonl"), `${lines.join("\n")}\n`, "utf8");
26
+ }
27
+
28
+ function ledgerEvent(body: Record<string, unknown>): string {
29
+ return JSON.stringify({ v: 1, id: `e-${Math.random().toString(36).slice(2)}`, ts: "2026-11-13T21:00+08:00", ...body });
30
+ }
31
+
32
+ afterEach(() => {
33
+ for (const root of roots.splice(0)) remove(root);
34
+ });
35
+
36
+ describe("UAT audit findings", () => {
37
+ it("reports a missing acceptance ledger as info", () => {
38
+ const root = installed();
39
+ expect(codes(root)).toContain(AUDIT_CODES.uatNotInstalled);
40
+ });
41
+
42
+ it("stays quiet once the contract exists", () => {
43
+ const root = installed({ [UAT_CONTRACT_FILE]: "# ledger\n" });
44
+ writeLedger(root, []);
45
+ expect(codes(root)).not.toContain(AUDIT_CODES.uatNotInstalled);
46
+ });
47
+
48
+ it("fails on an unparsable ledger line", () => {
49
+ const root = installed({ [UAT_CONTRACT_FILE]: "# ledger\n" });
50
+ writeLedger(root, ["{not json"]);
51
+ const findings = audit(detect(root));
52
+ const finding = findings.find((entry) => entry.code === AUDIT_CODES.uatLedgerBroken);
53
+ expect(finding?.severity).toBe("error");
54
+ });
55
+
56
+ it("warns when an item points at a missing record", () => {
57
+ const root = installed({ [UAT_CONTRACT_FILE]: "# ledger\n" });
58
+ writeLedger(root, [
59
+ ledgerEvent({ type: "item", item: "U-1", title: "t", summary: "s", reports: ["gone"] }),
60
+ ]);
61
+ const findings = audit(detect(root));
62
+ expect(findings.some((entry) => entry.code === AUDIT_CODES.uatMissingReport)).toBe(true);
63
+ });
64
+
65
+ it("warns about a recent non-canonical change record", () => {
66
+ const root = installed();
67
+ writeFileSync(
68
+ join(root, ".agents/implementations/2026-11-13_handwritten.md"),
69
+ "# Handwritten\n\nno time\n",
70
+ "utf8",
71
+ );
72
+ expect(codes(root)).toContain(AUDIT_CODES.recordHeader);
73
+ });
74
+
75
+ it("warns when a framework file was edited by hand", () => {
76
+ const root = installed();
77
+ const text = "# my own contract\n";
78
+ writeFileSync(join(root, CONTRACT_FILE), text, "utf8");
79
+ writeFileSync(
80
+ join(root, ".agents/framework/state.toml"),
81
+ stateToToml({
82
+ version: 1,
83
+ tool: "test",
84
+ created: "2026-01-01T00:00:00+08:00",
85
+ updated: "2026-01-01T00:00:00+08:00",
86
+ profile: "core",
87
+ modules: [],
88
+ roots: [],
89
+ managed: [
90
+ {
91
+ path: CONTRACT_FILE,
92
+ sha256: createHash("sha256").update(text).digest("hex"),
93
+ template: "memory-readme",
94
+ templateSha256: createHash("sha256").update("rendered once\n").digest("hex"),
95
+ },
96
+ ],
97
+ migrations: [],
98
+ }),
99
+ "utf8",
100
+ );
101
+ expect(codes(root)).toContain(AUDIT_CODES.managedEdited);
102
+ });
103
+ });
@@ -9,6 +9,7 @@
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";
12
13
  import { existsSync, readFileSync, readdirSync } from "node:fs";
13
14
  import { join, posix } from "node:path";
14
15
  import {
@@ -22,6 +23,9 @@ import {
22
23
  PLAN_FILE,
23
24
  REPORTS_DIR,
24
25
  SPECS_DIR,
26
+ UAT_CONTRACT_FILE,
27
+ UAT_DIR,
28
+ UAT_LEDGER_FILE,
25
29
  } from "./spec.js";
26
30
  import {
27
31
  duplicateHeadings,
@@ -30,6 +34,8 @@ import {
30
34
  sectionsAfterAppendix,
31
35
  } from "./analyze.js";
32
36
  import { parseCoverage } from "./coverage.js";
37
+ import { deriveState, ledgerPath, readEvents } from "../uat/ledger.js";
38
+ import { listRecords } from "../uat/records.js";
33
39
  import { staleRoots } from "./plan.js";
34
40
  import type { DocInfo, Facts, Finding, Severity } from "./types.js";
35
41
 
@@ -523,6 +529,146 @@ function checkScaffold(facts: Facts): Finding[] {
523
529
  /* Entry points */
524
530
  /* -------------------------------------------------------------------------- */
525
531
 
532
+ const DAY_MS = 86_400_000;
533
+
534
+ /**
535
+ * Framework-owned files: a refresh may rewrite one only while the disk still
536
+ * equals the bytes this tool rendered. Anything else is a hand edit, and
537
+ * silently overwriting it would destroy operator work — so the audit reports it
538
+ * instead.
539
+ */
540
+ function checkFrameworkFiles(facts: Facts): Finding[] {
541
+ const state = facts.state;
542
+ if (!state) return [];
543
+ 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;
550
+ out.push(
551
+ finding(
552
+ AUDIT_CODES.managedEdited,
553
+ "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,
556
+ ),
557
+ );
558
+ }
559
+ return out;
560
+ }
561
+
562
+ /**
563
+ * Acceptance ledger hygiene. All of these are warnings except a corrupt stream:
564
+ * a ledger line that cannot be parsed means a lost event, which is an error.
565
+ */
566
+ function checkUat(facts: Facts): Finding[] {
567
+ if (!facts.state) return [];
568
+ const out: Finding[] = [];
569
+ const now = Date.now();
570
+ const records = listRecords(facts.root);
571
+
572
+ // Record hygiene is independent of the ledger: the canonical frame is what
573
+ // the tool writes, and the ledger is the only consumer either way.
574
+ const recentNonCanonical = records.filter(
575
+ (record) => !record.canonical && record.epochMs >= now - 14 * DAY_MS,
576
+ );
577
+ if (recentNonCanonical.length > 0) {
578
+ const first = recentNonCanonical[0];
579
+ out.push(
580
+ finding(
581
+ AUDIT_CODES.recordHeader,
582
+ "warning",
583
+ `${recentNonCanonical.length} recent change record(s) do not use the canonical minute header/name (first: ${first?.id}); write new ones with record_change`,
584
+ first?.path,
585
+ ),
586
+ );
587
+ }
588
+
589
+ if (!existsSync(join(facts.root, UAT_DIR))) {
590
+ out.push(
591
+ finding(
592
+ AUDIT_CODES.uatNotInstalled,
593
+ "info",
594
+ "acceptance ledger not installed (.agents/uat/): run /ask-init refresh",
595
+ UAT_DIR,
596
+ ),
597
+ );
598
+ return out;
599
+ }
600
+ if (!existsSync(join(facts.root, UAT_CONTRACT_FILE))) {
601
+ out.push(
602
+ finding(
603
+ AUDIT_CODES.uatNotInstalled,
604
+ "info",
605
+ "acceptance ledger contract is missing: run /ask-init refresh",
606
+ UAT_CONTRACT_FILE,
607
+ ),
608
+ );
609
+ }
610
+
611
+ const { events, skipped } = readEvents(ledgerPath(facts.root));
612
+ if (skipped > 0) {
613
+ out.push(
614
+ finding(
615
+ AUDIT_CODES.uatLedgerBroken,
616
+ "error",
617
+ `${skipped} line(s) in the acceptance ledger could not be parsed: fix or remove them (${UAT_LEDGER_FILE})`,
618
+ UAT_LEDGER_FILE,
619
+ ),
620
+ );
621
+ }
622
+
623
+ const state = deriveState(records, events);
624
+ const missing = state.items.flatMap((item) =>
625
+ item.missingReports.map((report) => `${item.id} → ${report}`),
626
+ );
627
+ if (missing.length > 0) {
628
+ out.push(
629
+ finding(
630
+ AUDIT_CODES.uatMissingReport,
631
+ "warning",
632
+ `acceptance item(s) point at change records that no longer exist: ${missing.slice(0, 5).join(", ")}${
633
+ missing.length > 5 ? " …" : ""
634
+ }`,
635
+ UAT_LEDGER_FILE,
636
+ ),
637
+ );
638
+ }
639
+
640
+ const stalePending = state.pending.filter(
641
+ (record) => record.epochMs > 0 && now - record.epochMs > 14 * DAY_MS,
642
+ );
643
+ if (stalePending.length > 0) {
644
+ out.push(
645
+ finding(
646
+ AUDIT_CODES.uatStaleCandidate,
647
+ "warning",
648
+ `${stalePending.length} change record(s) have been waiting for acceptance for over 14 days: run /ask-uat`,
649
+ UAT_LEDGER_FILE,
650
+ ),
651
+ );
652
+ }
653
+
654
+ const staleOpen = state.open.filter((item) => {
655
+ const created = Date.parse(item.createdAt);
656
+ return Number.isFinite(created) && now - created > 30 * DAY_MS;
657
+ });
658
+ if (staleOpen.length > 0) {
659
+ out.push(
660
+ finding(
661
+ AUDIT_CODES.uatStaleItem,
662
+ "warning",
663
+ `${staleOpen.length} acceptance item(s) have been open for over 30 days: verify or drop them`,
664
+ UAT_LEDGER_FILE,
665
+ ),
666
+ );
667
+ }
668
+
669
+ return out;
670
+ }
671
+
526
672
  export function audit(facts: Facts, options: AuditOptions = {}): Finding[] {
527
673
  const findings = [
528
674
  ...checkStructure(facts),
@@ -538,6 +684,8 @@ export function audit(facts: Facts, options: AuditOptions = {}): Finding[] {
538
684
  ...checkPlanDrift(facts),
539
685
  ...checkCoverage(facts, options.coveragePath),
540
686
  ...checkScaffold(facts),
687
+ ...checkFrameworkFiles(facts),
688
+ ...checkUat(facts),
541
689
  ];
542
690
  const rank: Record<Severity, number> = { error: 0, warning: 1, info: 2 };
543
691
  return findings.sort((a, b) => rank[a.severity] - rank[b.severity] || a.code.localeCompare(b.code));
@@ -412,6 +412,10 @@ export function parseState(raw: string): FrameworkState {
412
412
  path: String(entry.path ?? ""),
413
413
  sha256: String(entry.sha256 ?? ""),
414
414
  template: String(entry.template ?? ""),
415
+ templateSha256:
416
+ entry.template_sha256 === undefined
417
+ ? undefined
418
+ : String(entry.template_sha256),
415
419
  }))
416
420
  : [];
417
421
  const migrations = Array.isArray(data.migrations)
@@ -46,6 +46,7 @@ import {
46
46
  } from "./spec.js";
47
47
  import { FRAMEWORK_AUDIT_TOOL } from "./tool.js";
48
48
  import { confirmPlan, showReport } from "./ui.js";
49
+ import { describeRefresh, refreshFramework } from "./refresh.js";
49
50
  import type { ApplyResult, Plan } from "./types.js";
50
51
  import { registerAuditTool } from "./tool.js";
51
52
 
@@ -60,7 +61,7 @@ export interface InitOptions {
60
61
  }
61
62
 
62
63
  export interface ParsedInitArgs {
63
- readonly sub: "run" | "plan" | "check" | "roots" | "scrub" | "help";
64
+ readonly sub: "run" | "plan" | "check" | "roots" | "refresh" | "scrub" | "help";
64
65
  readonly dryRun: boolean;
65
66
  readonly deep: boolean;
66
67
  readonly yes: boolean;
@@ -74,6 +75,7 @@ const USAGE = [
74
75
  "/ask-init plan [--deep] show the plan only (writes nothing)",
75
76
  "/ask-init check audit the installed framework (read-only)",
76
77
  "/ask-init roots sync pi-add-dir roots into the framework state",
78
+ "/ask-init refresh refresh framework-owned files and the AGENTS.md workflow block",
77
79
  "/ask-init --dry-run report only, write nothing (same as plan)",
78
80
  "/ask-init --yes skip the confirmation panel",
79
81
  "/ask-init --lang zh|en rendering language for framework-generated files",
@@ -89,7 +91,14 @@ export function parseInitArgs(input: string): ParsedInitArgs {
89
91
  const unknown: string[] = [];
90
92
  for (let index = 0; index < tokens.length; index += 1) {
91
93
  const token = tokens[index];
92
- if (token === "plan" || token === "check" || token === "roots" || token === "scrub" || token === "help") {
94
+ if (
95
+ token === "plan" ||
96
+ token === "check" ||
97
+ token === "roots" ||
98
+ token === "refresh" ||
99
+ token === "scrub" ||
100
+ token === "help"
101
+ ) {
93
102
  sub = token;
94
103
  continue;
95
104
  }
@@ -466,6 +475,41 @@ function createRunHandler(
466
475
 
467
476
  const toolVersion = options.toolVersion ?? packageVersion();
468
477
  const now = options.now ?? new Date();
478
+
479
+ // Framework-owned text is refreshable without an upgrade recipe: managed
480
+ // files are rewritten only while the disk still equals the bytes we wrote,
481
+ // and AGENTS.md is touched only between the workflow markers.
482
+ if (parsed.sub === "refresh") {
483
+ try {
484
+ const result = refreshFramework(root, {
485
+ dryRun: parsed.dryRun,
486
+ now,
487
+ toolVersion,
488
+ });
489
+ ctx.ui.notify(
490
+ `ask-init refresh: ${describeRefresh(result)}${parsed.dryRun ? " — nothing was written." : ""}`,
491
+ result.skipped.length > 0 ? "warning" : "info",
492
+ );
493
+ if (result.skipped.length > 0) {
494
+ await showReport(
495
+ ctx,
496
+ "ask-init refresh — left untouched",
497
+ [
498
+ "These framework files were edited by hand, so refresh did not overwrite them.",
499
+ "Move your additions out of the managed file, or re-apply them after refreshing.",
500
+ "",
501
+ ...result.skipped.map((entry) => `- ${entry.path}: ${entry.reason}`),
502
+ ].join("\n"),
503
+ );
504
+ }
505
+ } catch (error) {
506
+ ctx.ui.notify(
507
+ `ask-init refresh: ${error instanceof Error ? error.message : String(error)}`,
508
+ "error",
509
+ );
510
+ }
511
+ return;
512
+ }
469
513
  const addedRoots = options.addedRoots ?? addedRootsFromBranch(ctx);
470
514
  const facts = detect(root, { addedRoots, lang: parsed.lang });
471
515
 
@@ -579,6 +623,7 @@ export function registerInit(pi0: ExtensionAPI, options: InitOptions = {}): void
579
623
  { value: "plan", label: "plan", description: "show the plan only (writes nothing)" },
580
624
  { value: "check", label: "check", description: "audit the installed framework" },
581
625
  { value: "roots", label: "roots", description: "sync pi-add-dir roots into state" },
626
+ { value: "refresh", label: "refresh", description: "refresh framework-owned files (safe, idempotent)" },
582
627
  { value: "--dry-run", label: "--dry-run", description: "report only, write nothing" },
583
628
  { value: "--deep", label: "--deep", description: "allow structural rewrites" },
584
629
  { value: "--yes", label: "--yes", description: "skip the confirmation panel" },
@@ -36,11 +36,13 @@ import {
36
36
  SPECS_DIR,
37
37
  STATE_FILE,
38
38
  STATE_GITIGNORE,
39
+ UAT_CONTRACT_FILE,
39
40
  type ProjectLanguage,
40
41
  } from "./spec.js";
41
42
  import { duplicateHeadings, largestSections, sectionsAfterAppendix, tombstones } from "./analyze.js";
42
43
  import { countBareRefs } from "./detect.js";
43
44
  import { indexBlock, rootsBlock } from "./render.js";
45
+ import { renderTemplate } from "./templates.js";
44
46
  import type {
45
47
  Action,
46
48
  CreateFileAction,
@@ -152,6 +154,7 @@ function buildTokens(facts: Facts, now: Date, created: ReadonlySet<string>): Tok
152
154
  ),
153
155
  index: indexBlock(facts, language, created),
154
156
  roots: rootsBlock(facts.roots, language),
157
+ workflowBlock: renderTemplate("workflow-block", {}, language),
155
158
  };
156
159
  }
157
160
 
@@ -692,6 +695,14 @@ export function buildPlan(facts: Facts, options: PlanOptions): Plan {
692
695
  "signpost for the hidden agent directory",
693
696
  );
694
697
  add(CONTRACT_FILE, "memory-readme", { frameworkVersion: String(FRAMEWORK_VERSION) }, "framework contract");
698
+ // The acceptance ledger is additive: a project without it is still a valid
699
+ // v1 framework (the audit reports I-UAT-01, not an error).
700
+ add(
701
+ UAT_CONTRACT_FILE,
702
+ "uat-readme",
703
+ { implementationsDir: IMPLEMENTATIONS_DIR },
704
+ "acceptance ledger contract",
705
+ );
695
706
  add(STATE_GITIGNORE, "state-gitignore", {}, "ignore runtime state");
696
707
  for (const spec of MEMORY_FILES) {
697
708
  if (!spec.core) continue;