@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.
- package/CHANGELOG.md +23 -0
- package/README.md +101 -34
- package/README.zh-CN.md +68 -32
- package/package.json +10 -3
- package/src/features/ask/index.test.ts +15 -0
- package/src/features/ask/index.ts +18 -1
- 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/templates/workflow-block.md +2 -2
- package/src/features/init/test-helpers.ts +1 -0
- package/src/features/init/workflow.ts +159 -0
- package/src/features/permission/index.ts +1 -1
- 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/spec/assets/index-CdTerMvN.css +1 -0
- package/src/features/spec/assets/index-Dv52AIjX.js +1 -0
- package/src/features/{plan → spec}/assets/index.html +3 -3
- package/src/features/{plan → spec}/assets.test.ts +5 -5
- package/src/features/{plan → spec}/blocks.ts +1 -1
- package/src/features/{plan → spec}/feedback.test.ts +26 -26
- package/src/features/{plan → spec}/feedback.ts +14 -14
- package/src/features/{plan → spec}/index.test.ts +148 -63
- package/src/features/{plan → spec}/index.ts +136 -105
- package/src/features/{plan → spec}/prompt.test.ts +20 -20
- package/src/features/{plan → spec}/prompt.ts +25 -25
- package/src/features/{plan → spec}/render.test.ts +2 -2
- package/src/features/{plan → spec}/server.test.ts +21 -21
- package/src/features/{plan → spec}/server.ts +22 -22
- package/src/features/{plan → spec}/store.test.ts +77 -35
- package/src/features/{plan → spec}/store.ts +80 -52
- package/src/features/uat/index.test.ts +20 -5
- package/src/features/uat/index.ts +16 -13
- package/src/features/uat/record-tool.ts +6 -5
- package/src/features/uat/records.test.ts +5 -5
- package/src/features/uat/state.ts +13 -5
- package/src/index.ts +6 -6
- package/src/lib/ask-control.ts +2 -2
- package/src/lib/permission-control.ts +4 -4
- package/src/lib/phases.ts +50 -0
- package/src/lib/web-review.ts +1 -1
- package/src/features/plan/assets/index-C1YLIi4I.js +0 -1
- package/src/features/plan/assets/index-DMEpYC3V.css +0 -1
- /package/src/features/{plan → spec}/blocks.test.ts +0 -0
- /package/src/features/{plan → spec}/diff.test.ts +0 -0
- /package/src/features/{plan → spec}/diff.ts +0 -0
- /package/src/features/{plan → spec}/render.ts +0 -0
|
@@ -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" },
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { describe, expect, it } from "vitest";
|
|
5
|
+
import { stateToToml } from "./apply.js";
|
|
6
|
+
import { audit } from "./audit.js";
|
|
7
|
+
import { detect } from "./detect.js";
|
|
8
|
+
import {
|
|
9
|
+
frameworkReadiness,
|
|
10
|
+
inspectFramework,
|
|
11
|
+
inspectManaged,
|
|
12
|
+
inspectWorkflow,
|
|
13
|
+
renderManaged,
|
|
14
|
+
MANAGED_SPECS,
|
|
15
|
+
workflowBlockText,
|
|
16
|
+
} from "./parity.js";
|
|
17
|
+
import { AGENTS_FILE, CONTRACT_FILE } from "./spec.js";
|
|
18
|
+
import { project, remove } from "./test-helpers.js";
|
|
19
|
+
import type { FrameworkState } from "./types.js";
|
|
20
|
+
|
|
21
|
+
const hash = (text: string) => createHash("sha256").update(text).digest("hex");
|
|
22
|
+
|
|
23
|
+
function state(managed: FrameworkState["managed"]): FrameworkState {
|
|
24
|
+
return {
|
|
25
|
+
version: 1,
|
|
26
|
+
tool: "test",
|
|
27
|
+
created: "2026-01-01T00:00:00+08:00",
|
|
28
|
+
updated: "2026-01-01T00:00:00+08:00",
|
|
29
|
+
profile: "core",
|
|
30
|
+
modules: [],
|
|
31
|
+
roots: [],
|
|
32
|
+
managed,
|
|
33
|
+
migrations: [],
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function writeState(root: string, managed: FrameworkState["managed"]): void {
|
|
38
|
+
mkdirSync(join(root, ".agents/framework"), { recursive: true });
|
|
39
|
+
writeFileSync(
|
|
40
|
+
join(root, ".agents/framework/state.toml"),
|
|
41
|
+
stateToToml(state(managed)),
|
|
42
|
+
"utf8",
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
describe("parity", () => {
|
|
47
|
+
it("distinguishes current, stale, hand-edited, missing and unverifiable files", () => {
|
|
48
|
+
const current = renderManaged(MANAGED_SPECS[0]);
|
|
49
|
+
|
|
50
|
+
const staleRoot = project({ [CONTRACT_FILE]: "old contract\n" });
|
|
51
|
+
writeState(staleRoot, [
|
|
52
|
+
{
|
|
53
|
+
path: CONTRACT_FILE,
|
|
54
|
+
sha256: hash("old contract\n"),
|
|
55
|
+
template: "memory-readme",
|
|
56
|
+
templateSha256: hash("old contract\n"),
|
|
57
|
+
},
|
|
58
|
+
]);
|
|
59
|
+
expect(inspectManaged(staleRoot, detect(staleRoot).state).find((entry) => entry.path === CONTRACT_FILE)?.status).toBe(
|
|
60
|
+
"stale",
|
|
61
|
+
);
|
|
62
|
+
remove(staleRoot);
|
|
63
|
+
|
|
64
|
+
const editedRoot = project({ [CONTRACT_FILE]: "my edits\n" });
|
|
65
|
+
writeState(editedRoot, [
|
|
66
|
+
{
|
|
67
|
+
path: CONTRACT_FILE,
|
|
68
|
+
sha256: hash("my edits\n"),
|
|
69
|
+
template: "memory-readme",
|
|
70
|
+
templateSha256: hash("what we wrote\n"),
|
|
71
|
+
},
|
|
72
|
+
]);
|
|
73
|
+
expect(inspectManaged(editedRoot, detect(editedRoot).state).find((entry) => entry.path === CONTRACT_FILE)?.status).toBe(
|
|
74
|
+
"hand-edited",
|
|
75
|
+
);
|
|
76
|
+
remove(editedRoot);
|
|
77
|
+
|
|
78
|
+
const legacyRoot = project({ [CONTRACT_FILE]: "old contract\n" });
|
|
79
|
+
expect(inspectManaged(legacyRoot, detect(legacyRoot).state).find((entry) => entry.path === CONTRACT_FILE)?.status).toBe(
|
|
80
|
+
"unverifiable",
|
|
81
|
+
);
|
|
82
|
+
remove(legacyRoot);
|
|
83
|
+
|
|
84
|
+
const missingRoot = project({});
|
|
85
|
+
expect(inspectManaged(missingRoot).find((entry) => entry.path === CONTRACT_FILE)?.status).toBe(
|
|
86
|
+
"missing",
|
|
87
|
+
);
|
|
88
|
+
remove(missingRoot);
|
|
89
|
+
|
|
90
|
+
const currentRoot = project({ [CONTRACT_FILE]: current });
|
|
91
|
+
writeState(currentRoot, [
|
|
92
|
+
{
|
|
93
|
+
path: CONTRACT_FILE,
|
|
94
|
+
sha256: hash(current),
|
|
95
|
+
template: "memory-readme",
|
|
96
|
+
templateSha256: hash(current),
|
|
97
|
+
},
|
|
98
|
+
]);
|
|
99
|
+
expect(inspectManaged(currentRoot, detect(currentRoot).state).find((entry) => entry.path === CONTRACT_FILE)?.status).toBe(
|
|
100
|
+
"current",
|
|
101
|
+
);
|
|
102
|
+
remove(currentRoot);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("keeps the readiness predicate tolerant of file content edits", () => {
|
|
106
|
+
const empty = project({});
|
|
107
|
+
expect(frameworkReadiness(empty).ready).toBe(false);
|
|
108
|
+
expect(frameworkReadiness(empty).missing).toContain(AGENTS_FILE);
|
|
109
|
+
remove(empty);
|
|
110
|
+
|
|
111
|
+
const ready = project({
|
|
112
|
+
[AGENTS_FILE]: "# demo\n",
|
|
113
|
+
[CONTRACT_FILE]: "# my own contract\n",
|
|
114
|
+
".agents/implementations/2026-01-01_x.md": "# x\n",
|
|
115
|
+
".agents/framework/state.toml": "version = 1\ntool = \"test\"\n",
|
|
116
|
+
});
|
|
117
|
+
expect(frameworkReadiness(ready)).toEqual({ ready: true, missing: [] });
|
|
118
|
+
remove(ready);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("rejects paths with the wrong file type", () => {
|
|
122
|
+
const root = project({
|
|
123
|
+
".agents/framework/state.toml": "version = 1\ntool = \"test\"\n",
|
|
124
|
+
".agents/memory/README.md": "# contract\n",
|
|
125
|
+
"AGENTS.md/placeholder.txt": "not a file\n",
|
|
126
|
+
".agents/implementations": "not a directory\n",
|
|
127
|
+
});
|
|
128
|
+
const readiness = frameworkReadiness(root);
|
|
129
|
+
expect(readiness.ready).toBe(false);
|
|
130
|
+
expect(readiness.missing).toEqual(expect.arrayContaining([AGENTS_FILE, ".agents/implementations"]));
|
|
131
|
+
remove(root);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("classifies the workflow block without touching it", () => {
|
|
135
|
+
const missing = project({ [AGENTS_FILE]: "# demo\n" });
|
|
136
|
+
expect(inspectWorkflow(missing)).toBe("missing");
|
|
137
|
+
remove(missing);
|
|
138
|
+
|
|
139
|
+
const handWritten = project({ [AGENTS_FILE]: "# demo\n\n## Ask workflow\n\nDo it.\n" });
|
|
140
|
+
expect(inspectWorkflow(handWritten)).toBe("hand-written");
|
|
141
|
+
remove(handWritten);
|
|
142
|
+
|
|
143
|
+
const malformed = project({
|
|
144
|
+
[AGENTS_FILE]: "# demo\n\n<!-- ask-init:begin workflow v1 -->\n## old\n",
|
|
145
|
+
});
|
|
146
|
+
expect(inspectWorkflow(malformed)).toBe("malformed");
|
|
147
|
+
remove(malformed);
|
|
148
|
+
|
|
149
|
+
const current = project({ [AGENTS_FILE]: `# demo\n\n${workflowBlockText()}` });
|
|
150
|
+
expect(inspectWorkflow(current)).toBe("current");
|
|
151
|
+
remove(current);
|
|
152
|
+
|
|
153
|
+
const stale = project({
|
|
154
|
+
[AGENTS_FILE]: `# demo\n\n<!-- ask-init:begin workflow v1 -->\n## old\n<!-- ask-init:end workflow -->\n`,
|
|
155
|
+
});
|
|
156
|
+
expect(inspectWorkflow(stale)).toBe("stale");
|
|
157
|
+
remove(stale);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("reports required files and surfaces parity findings through audit", () => {
|
|
161
|
+
const root = project({
|
|
162
|
+
[AGENTS_FILE]: "# demo\n",
|
|
163
|
+
[CONTRACT_FILE]: "old contract\n",
|
|
164
|
+
".agents/framework/state.toml": "version = 1\ntool = \"test\"\n",
|
|
165
|
+
});
|
|
166
|
+
try {
|
|
167
|
+
expect(inspectFramework(root, detect(root).state).requiredMissing.length).toBeGreaterThan(0);
|
|
168
|
+
const codes = audit(detect(root)).map((finding) => finding.code);
|
|
169
|
+
expect(codes).toContain("W-FW-02");
|
|
170
|
+
expect(codes).toContain("W-FW-03");
|
|
171
|
+
expect(codes).toContain("W-FW-04");
|
|
172
|
+
} finally {
|
|
173
|
+
remove(root);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
});
|