@andares/pi-toolkits 0.14.0 → 1.0.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.
- package/CHANGELOG.md +13 -0
- package/README.md +87 -21
- package/README.zh-CN.md +52 -17
- package/package.json +8 -2
- package/src/features/ask/index.test.ts +15 -0
- package/src/features/ask/index.ts +17 -0
- package/src/features/init/audit.test.ts +19 -2
- package/src/features/init/audit.ts +117 -15
- package/src/features/init/index.test.ts +123 -1
- package/src/features/init/index.ts +156 -46
- package/src/features/init/parity.test.ts +176 -0
- package/src/features/init/parity.ts +229 -0
- package/src/features/init/plan.ts +25 -3
- package/src/features/init/refresh.test.ts +88 -1
- package/src/features/init/refresh.ts +43 -64
- package/src/features/init/spec.ts +11 -0
- package/src/features/init/templates/memory-readme.md +9 -2
- package/src/features/init/test-helpers.ts +1 -0
- package/src/features/init/workflow.ts +159 -0
- package/src/features/plan/index.test.ts +51 -0
- package/src/features/plan/index.ts +24 -4
- package/src/features/quit-guard/constants.ts +18 -5
- package/src/features/quit-guard/index.ts +75 -29
- package/src/features/quit-guard/killer.test.ts +77 -0
- package/src/features/quit-guard/killer.ts +215 -0
- package/src/features/review/index.ts +14 -0
- package/src/features/review/review.test.ts +34 -2
- package/src/features/uat/index.test.ts +15 -0
- package/src/features/uat/index.ts +10 -7
- package/src/features/uat/record-tool.ts +3 -2
- package/src/features/uat/state.ts +13 -5
- package/src/lib/phases.ts +37 -0
|
@@ -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
|
-
|
|
219
|
-
|
|
220
|
-
`${facts.refs.hits} bare path references in ${facts.refs.paths.length} files
|
|
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
|
-
|
|
542
|
-
|
|
552
|
+
if (!facts.state) return [];
|
|
553
|
+
const parity = inspectFramework(facts.root, facts.state);
|
|
543
554
|
const out: Finding[] = [];
|
|
544
|
-
for (const entry of
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
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.
|
|
608
|
+
AUDIT_CODES.frameworkFileMissing,
|
|
553
609
|
"warning",
|
|
554
|
-
"framework file
|
|
555
|
-
|
|
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 {
|
|
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("plan_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-plan/.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-"));
|
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
* - never while the agent is streaming (a plan needs a stable tree);
|
|
20
20
|
* - never without `write`/`edit` in the active tool set (`/ask` mode would
|
|
21
21
|
* reject the model's own work);
|
|
22
|
-
* - never
|
|
22
|
+
* - never performs a migration without an explicit decision (TUI panel; no UI ⇒ report only);
|
|
23
|
+
* the safe, already-installed refresh path still runs when `/ask-init` is invoked.
|
|
23
24
|
*/
|
|
24
25
|
import { copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
25
26
|
import { homedir } from "node:os";
|
|
@@ -47,7 +48,10 @@ import {
|
|
|
47
48
|
import { FRAMEWORK_AUDIT_TOOL } from "./tool.js";
|
|
48
49
|
import { confirmPlan, showReport } from "./ui.js";
|
|
49
50
|
import { describeRefresh, refreshFramework } from "./refresh.js";
|
|
50
|
-
import type {
|
|
51
|
+
import type { RefreshResult } from "./refresh.js";
|
|
52
|
+
import { inspectFramework, describeParity } from "./parity.js";
|
|
53
|
+
import { activePhases, describePhases } from "../../lib/phases.js";
|
|
54
|
+
import type { ApplyResult, Facts, Plan } from "./types.js";
|
|
51
55
|
import { registerAuditTool } from "./tool.js";
|
|
52
56
|
|
|
53
57
|
export interface InitOptions {
|
|
@@ -65,6 +69,8 @@ export interface ParsedInitArgs {
|
|
|
65
69
|
readonly dryRun: boolean;
|
|
66
70
|
readonly deep: boolean;
|
|
67
71
|
readonly yes: boolean;
|
|
72
|
+
/** Take ownership of legacy managed files that have no template anchor. */
|
|
73
|
+
readonly adopt: boolean;
|
|
68
74
|
readonly lang?: ProjectLanguage;
|
|
69
75
|
/** Unknown arguments are an error, never a silent full run. */
|
|
70
76
|
readonly error?: string;
|
|
@@ -76,6 +82,7 @@ const USAGE = [
|
|
|
76
82
|
"/ask-init check audit the installed framework (read-only)",
|
|
77
83
|
"/ask-init roots sync pi-add-dir roots into the framework state",
|
|
78
84
|
"/ask-init refresh refresh framework-owned files and the AGENTS.md workflow block",
|
|
85
|
+
"/ask-init refresh --adopt take ownership of legacy files without an anchor, then refresh",
|
|
79
86
|
"/ask-init --dry-run report only, write nothing (same as plan)",
|
|
80
87
|
"/ask-init --yes skip the confirmation panel",
|
|
81
88
|
"/ask-init --lang zh|en rendering language for framework-generated files",
|
|
@@ -87,6 +94,7 @@ export function parseInitArgs(input: string): ParsedInitArgs {
|
|
|
87
94
|
let dryRun = false;
|
|
88
95
|
let deep = false;
|
|
89
96
|
let yes = false;
|
|
97
|
+
let adopt = false;
|
|
90
98
|
let lang: ProjectLanguage | undefined;
|
|
91
99
|
const unknown: string[] = [];
|
|
92
100
|
for (let index = 0; index < tokens.length; index += 1) {
|
|
@@ -115,6 +123,10 @@ export function parseInitArgs(input: string): ParsedInitArgs {
|
|
|
115
123
|
yes = true;
|
|
116
124
|
continue;
|
|
117
125
|
}
|
|
126
|
+
if (token === "--adopt") {
|
|
127
|
+
adopt = true;
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
118
130
|
if (token === "--lang") {
|
|
119
131
|
const value = tokens[index + 1];
|
|
120
132
|
if (value === "zh" || value === "en") {
|
|
@@ -140,6 +152,7 @@ export function parseInitArgs(input: string): ParsedInitArgs {
|
|
|
140
152
|
dryRun,
|
|
141
153
|
deep,
|
|
142
154
|
yes,
|
|
155
|
+
adopt,
|
|
143
156
|
lang,
|
|
144
157
|
error:
|
|
145
158
|
unknown.length > 0
|
|
@@ -236,7 +249,7 @@ function writeRuntimeReport(root: string, name: string, text: string): string |
|
|
|
236
249
|
return rel;
|
|
237
250
|
}
|
|
238
251
|
|
|
239
|
-
function planText(plan: Plan): string {
|
|
252
|
+
function planText(plan: Plan, facts: Facts): string {
|
|
240
253
|
const lines = [
|
|
241
254
|
`/ask-init — ${plan.mode}`,
|
|
242
255
|
`root: ${plan.root}`,
|
|
@@ -248,6 +261,7 @@ function planText(plan: Plan): string {
|
|
|
248
261
|
if (plan.warnings.length > 0) {
|
|
249
262
|
lines.push("", "## warnings", ...plan.warnings.map((line) => `- ${line}`));
|
|
250
263
|
}
|
|
264
|
+
lines.push(...parityLines(plan, facts));
|
|
251
265
|
if (plan.actions.length > 0) {
|
|
252
266
|
lines.push("", "## mechanical actions");
|
|
253
267
|
for (const action of plan.actions) {
|
|
@@ -285,6 +299,12 @@ function planText(plan: Plan): string {
|
|
|
285
299
|
return lines.join("\n");
|
|
286
300
|
}
|
|
287
301
|
|
|
302
|
+
/** The parity section of the plan and of a no-op run. */
|
|
303
|
+
function parityLines(plan: Plan, facts: Facts): string[] {
|
|
304
|
+
if (!facts.state) return ["", "## framework parity", "- framework not installed yet"];
|
|
305
|
+
return ["", "## framework parity", ...describeParity(inspectFramework(plan.root, facts.state))];
|
|
306
|
+
}
|
|
307
|
+
|
|
288
308
|
function appliedText(applied: ApplyResult): string {
|
|
289
309
|
const lines = ["## applied"];
|
|
290
310
|
if (applied.snapshot) {
|
|
@@ -370,7 +390,10 @@ async function reportAudit(
|
|
|
370
390
|
now: Date,
|
|
371
391
|
): Promise<void> {
|
|
372
392
|
const findings = audit(facts);
|
|
373
|
-
const
|
|
393
|
+
const parity = facts.state
|
|
394
|
+
? ["", "## framework parity", ...describeParity(inspectFramework(root, facts.state))]
|
|
395
|
+
: [];
|
|
396
|
+
const text = [renderFindings(findings, { root, frameworkVersion: FRAMEWORK_VERSION }), ...parity].join("\n");
|
|
374
397
|
const rel = writeRuntimeReport(root, `audit-${runStamp(iso(now))}.md`, text);
|
|
375
398
|
const counts = countFindings(findings);
|
|
376
399
|
ctx.ui.notify(
|
|
@@ -381,9 +404,11 @@ async function reportAudit(
|
|
|
381
404
|
}
|
|
382
405
|
|
|
383
406
|
/**
|
|
384
|
-
* Apply the deterministic half,
|
|
385
|
-
* model. Activation of the audit tool happens
|
|
386
|
-
* `before_agent_start`, because pi rebuilds the
|
|
407
|
+
* Apply the deterministic half, refresh framework-owned text, record the run,
|
|
408
|
+
* then hand the prose work to the model. Activation of the audit tool happens
|
|
409
|
+
* here and is asserted again in `before_agent_start`, because pi rebuilds the
|
|
410
|
+
* tool loadout for the new run. A run with no model tasks stops after the
|
|
411
|
+
* mechanical repair; there is nothing for a model to decide.
|
|
387
412
|
*/
|
|
388
413
|
async function applyAndDispatch(
|
|
389
414
|
pi: ExtensionAPI,
|
|
@@ -391,14 +416,17 @@ async function applyAndDispatch(
|
|
|
391
416
|
plan: Plan,
|
|
392
417
|
runtime: { auditActive: boolean },
|
|
393
418
|
now: Date,
|
|
419
|
+
adoptLegacy = false,
|
|
394
420
|
): Promise<ApplyResult> {
|
|
395
421
|
const applied = applyPlan(plan, { now });
|
|
396
|
-
|
|
397
|
-
|
|
422
|
+
const refreshed = runRefresh(plan.root, { now, toolVersion: plan.toolVersion, adoptLegacy });
|
|
423
|
+
const refreshLine = refreshed.result
|
|
424
|
+
? describeRefresh(refreshed.result)
|
|
425
|
+
: `refresh failed: ${refreshed.error ?? "unknown error"}`;
|
|
398
426
|
|
|
399
427
|
pi.appendEntry("ask-init:run", {
|
|
400
428
|
mode: plan.mode,
|
|
401
|
-
summary: [...plan.summary, ...applied.summary],
|
|
429
|
+
summary: [...plan.summary, ...applied.summary, refreshLine],
|
|
402
430
|
at: plan.createdAt,
|
|
403
431
|
});
|
|
404
432
|
|
|
@@ -406,10 +434,23 @@ async function applyAndDispatch(
|
|
|
406
434
|
`ask-init: ${plan.mode} applied — ${applied.summary.slice(0, 2).join(" · ")}`,
|
|
407
435
|
"info",
|
|
408
436
|
);
|
|
409
|
-
await showReport(
|
|
410
|
-
|
|
411
|
-
|
|
437
|
+
await showReport(
|
|
438
|
+
ctx,
|
|
439
|
+
`ask-init ${plan.mode}`,
|
|
440
|
+
`${appliedText(applied)}\n\n## refresh\n- ${refreshLine}`,
|
|
441
|
+
{ path: applied.planPath },
|
|
442
|
+
);
|
|
443
|
+
if (refreshed.result?.block === "inserted" || refreshed.result?.block === "updated") {
|
|
444
|
+
ctx.ui.notify("ask-init: AGENTS.md workflow block updated — /reload after this run.", "info");
|
|
445
|
+
}
|
|
412
446
|
|
|
447
|
+
if (plan.tasks.length === 0) {
|
|
448
|
+
ctx.ui.notify("ask-init: mechanical repair applied — no model turn needed.", "info");
|
|
449
|
+
return applied;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
runtime.auditActive = true;
|
|
453
|
+
activateAuditTool(pi);
|
|
413
454
|
pi.sendUserMessage(buildWorkOrder({ plan, applied, planPath: applied.planPath }), {
|
|
414
455
|
expandPromptTemplates: false,
|
|
415
456
|
});
|
|
@@ -420,6 +461,22 @@ async function applyAndDispatch(
|
|
|
420
461
|
return applied;
|
|
421
462
|
}
|
|
422
463
|
|
|
464
|
+
interface RefreshAttempt {
|
|
465
|
+
readonly result?: RefreshResult;
|
|
466
|
+
readonly error?: string;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function runRefresh(
|
|
470
|
+
root: string,
|
|
471
|
+
options: { now: Date; toolVersion: string; dryRun?: boolean; adoptLegacy?: boolean },
|
|
472
|
+
): RefreshAttempt {
|
|
473
|
+
try {
|
|
474
|
+
return { result: refreshFramework(root, options) };
|
|
475
|
+
} catch (error) {
|
|
476
|
+
return { error: error instanceof Error ? error.message : String(error) };
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
423
480
|
/**
|
|
424
481
|
* The command body, kept out of `registerInit` so registration stays a thin
|
|
425
482
|
* table of hooks and the dispatcher can be read on its own.
|
|
@@ -452,6 +509,20 @@ function createRunHandler(
|
|
|
452
509
|
return;
|
|
453
510
|
}
|
|
454
511
|
|
|
512
|
+
// Mutating subcommands need a stable tree: a plan under review or an open
|
|
513
|
+
// UAT page must not be rewritten under the operator's feet. Read-only
|
|
514
|
+
// subcommands (`check`, `plan`, `help`) stay available.
|
|
515
|
+
const phases = activePhases(pi);
|
|
516
|
+
const mutating =
|
|
517
|
+
parsed.sub === "run" ||
|
|
518
|
+
parsed.sub === "refresh" ||
|
|
519
|
+
parsed.sub === "roots" ||
|
|
520
|
+
parsed.sub === "scrub";
|
|
521
|
+
if (mutating && phases.length > 0) {
|
|
522
|
+
ctx.ui.notify(`ask-init: ${describePhases(phases)} is active — leave it first.`, "warning");
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
|
|
455
526
|
// Hidden maintenance subcommand (not in USAGE): removes retired fields
|
|
456
527
|
// from an installed framework. Documented in AGENTS.md only; obeys
|
|
457
528
|
// `--dry-run` because that flag promises "nothing is written".
|
|
@@ -478,40 +549,59 @@ function createRunHandler(
|
|
|
478
549
|
|
|
479
550
|
// Framework-owned text is refreshable without an upgrade recipe: managed
|
|
480
551
|
// files are rewritten only while the disk still equals the bytes we wrote,
|
|
481
|
-
// and AGENTS.md is touched only between
|
|
552
|
+
// and AGENTS.md is touched only between a valid workflow marker pair.
|
|
482
553
|
if (parsed.sub === "refresh") {
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
554
|
+
const attempt = runRefresh(root, {
|
|
555
|
+
dryRun: parsed.dryRun,
|
|
556
|
+
now,
|
|
557
|
+
toolVersion,
|
|
558
|
+
adoptLegacy: parsed.adopt,
|
|
559
|
+
});
|
|
560
|
+
if (!attempt.result) {
|
|
561
|
+
ctx.ui.notify(`ask-init refresh: ${attempt.error ?? "unknown error"}`, "error");
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
const result = attempt.result;
|
|
565
|
+
const warning =
|
|
566
|
+
result.skipped.length > 0 ||
|
|
567
|
+
result.block === "malformed" ||
|
|
568
|
+
result.block === "hand-written" ||
|
|
569
|
+
result.block === "missing-agents";
|
|
570
|
+
ctx.ui.notify(
|
|
571
|
+
`ask-init refresh: ${describeRefresh(result)}${parsed.dryRun ? " — nothing was written." : ""}`,
|
|
572
|
+
warning ? "warning" : "info",
|
|
573
|
+
);
|
|
574
|
+
if (!parsed.dryRun && (result.block === "inserted" || result.block === "updated")) {
|
|
575
|
+
ctx.ui.notify("ask-init: run /reload so the new AGENTS.md enters the prompt.", "info");
|
|
576
|
+
}
|
|
577
|
+
if (warning) {
|
|
578
|
+
await showReport(
|
|
579
|
+
ctx,
|
|
580
|
+
"ask-init refresh — left untouched",
|
|
581
|
+
[
|
|
582
|
+
"These framework files were left untouched.",
|
|
583
|
+
"Hand edits stay yours; a file written by an older version without an anchor cannot be told apart from a hand edit.",
|
|
584
|
+
"",
|
|
585
|
+
...(result.block === "malformed"
|
|
586
|
+
? [
|
|
587
|
+
"AGENTS.md: the workflow markers are inconsistent — fix the marker pair, then rerun refresh.",
|
|
588
|
+
"",
|
|
589
|
+
]
|
|
590
|
+
: []),
|
|
591
|
+
...(result.block === "hand-written"
|
|
592
|
+
? [
|
|
593
|
+
"AGENTS.md: a hand-written Ask workflow section exists without markers — add markers or remove it; refresh will not add a second one.",
|
|
594
|
+
"",
|
|
595
|
+
]
|
|
596
|
+
: []),
|
|
597
|
+
...result.skipped.map((entry) => `- ${entry.path}: ${entry.reason}`),
|
|
598
|
+
].join("\n"),
|
|
509
599
|
);
|
|
510
600
|
}
|
|
511
601
|
return;
|
|
512
602
|
}
|
|
513
603
|
const addedRoots = options.addedRoots ?? addedRootsFromBranch(ctx);
|
|
514
|
-
|
|
604
|
+
let facts = detect(root, { addedRoots, lang: parsed.lang });
|
|
515
605
|
|
|
516
606
|
const doCheck = (): Promise<void> => reportAudit(ctx, root, facts, now);
|
|
517
607
|
|
|
@@ -532,7 +622,7 @@ function createRunHandler(
|
|
|
532
622
|
});
|
|
533
623
|
|
|
534
624
|
if (parsed.sub === "plan") {
|
|
535
|
-
await showReport(ctx, "ask-init plan", planText(plan));
|
|
625
|
+
await showReport(ctx, "ask-init plan", planText(plan, facts));
|
|
536
626
|
if (plan.warnings.length > 0) {
|
|
537
627
|
ctx.ui.notify(`ask-init plan: ${plan.warnings.length} warning(s) — see the panel.`, "warning");
|
|
538
628
|
}
|
|
@@ -546,13 +636,13 @@ function createRunHandler(
|
|
|
546
636
|
"ask-init: this project's framework is newer than this tool.",
|
|
547
637
|
"error",
|
|
548
638
|
);
|
|
549
|
-
await showReport(ctx, "ask-init", planText(plan));
|
|
639
|
+
await showReport(ctx, "ask-init", planText(plan, facts));
|
|
550
640
|
return;
|
|
551
641
|
}
|
|
552
642
|
if (plan.mode === "upgrade") {
|
|
553
643
|
// There is no v1 upgrade recipe. Reporting is safe; applying a scaffold
|
|
554
644
|
// over an older state file would be a guess, not a migration.
|
|
555
|
-
await showReport(ctx, "ask-init upgrade", planText(plan));
|
|
645
|
+
await showReport(ctx, "ask-init upgrade", planText(plan, facts));
|
|
556
646
|
ctx.ui.notify(
|
|
557
647
|
"ask-init: no automatic upgrade recipe for this state version — nothing was written. Migrate by hand after reviewing .agents/memory/README.md.",
|
|
558
648
|
"warning",
|
|
@@ -560,6 +650,25 @@ function createRunHandler(
|
|
|
560
650
|
return;
|
|
561
651
|
}
|
|
562
652
|
if (plan.mode === "audit-only") {
|
|
653
|
+
// A repeated run answers "are we on par?" before it checks content:
|
|
654
|
+
// refresh repairs missing/stale framework text when safe, leaves hand
|
|
655
|
+
// edits alone, and reports what it did. The audit then runs against
|
|
656
|
+
// the refreshed tree.
|
|
657
|
+
const attempt = runRefresh(root, { now, toolVersion, adoptLegacy: parsed.adopt });
|
|
658
|
+
if (attempt.result) {
|
|
659
|
+
const result = attempt.result;
|
|
660
|
+
const warning =
|
|
661
|
+
result.skipped.length > 0 ||
|
|
662
|
+
result.block === "malformed" ||
|
|
663
|
+
result.block === "hand-written";
|
|
664
|
+
ctx.ui.notify(`ask-init: ${describeRefresh(result)}`, warning ? "warning" : "info");
|
|
665
|
+
if (result.block === "inserted" || result.block === "updated") {
|
|
666
|
+
ctx.ui.notify("ask-init: run /reload so the new AGENTS.md enters the prompt.", "info");
|
|
667
|
+
}
|
|
668
|
+
facts = detect(root, { addedRoots, lang: parsed.lang });
|
|
669
|
+
} else {
|
|
670
|
+
ctx.ui.notify(`ask-init refresh: ${attempt.error ?? "unknown error"}`, "error");
|
|
671
|
+
}
|
|
563
672
|
await doCheck();
|
|
564
673
|
return;
|
|
565
674
|
}
|
|
@@ -576,12 +685,12 @@ function createRunHandler(
|
|
|
576
685
|
return;
|
|
577
686
|
}
|
|
578
687
|
if (decision === "dry-run") {
|
|
579
|
-
await showReport(ctx, "ask-init plan", planText(plan));
|
|
688
|
+
await showReport(ctx, "ask-init plan", planText(plan, facts));
|
|
580
689
|
ctx.ui.notify("ask-init: report only — nothing was written.", "info");
|
|
581
690
|
return;
|
|
582
691
|
}
|
|
583
692
|
|
|
584
|
-
await applyAndDispatch(pi, ctx, plan, runtime, now);
|
|
693
|
+
await applyAndDispatch(pi, ctx, plan, runtime, now, parsed.adopt);
|
|
585
694
|
};
|
|
586
695
|
}
|
|
587
696
|
|
|
@@ -624,6 +733,7 @@ export function registerInit(pi0: ExtensionAPI, options: InitOptions = {}): void
|
|
|
624
733
|
{ value: "check", label: "check", description: "audit the installed framework" },
|
|
625
734
|
{ value: "roots", label: "roots", description: "sync pi-add-dir roots into state" },
|
|
626
735
|
{ value: "refresh", label: "refresh", description: "refresh framework-owned files (safe, idempotent)" },
|
|
736
|
+
{ value: "--adopt", label: "--adopt", description: "take ownership of legacy framework files without an anchor" },
|
|
627
737
|
{ value: "--dry-run", label: "--dry-run", description: "report only, write nothing" },
|
|
628
738
|
{ value: "--deep", label: "--deep", description: "allow structural rewrites" },
|
|
629
739
|
{ value: "--yes", label: "--yes", description: "skip the confirmation panel" },
|