@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
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* init feature — framework-owned text versus the installed tool.
|
|
3
|
+
*
|
|
4
|
+
* `/ask-init` must answer one question on a repeated run: *is the project's
|
|
5
|
+
* framework text on par with the version of this tool, and if not, what can be
|
|
6
|
+
* repaired without judgement?* This module is the read-only half of that
|
|
7
|
+
* answer. It compares each managed file with the bytes this version renders,
|
|
8
|
+
* distinguishes a hand edit from an older template, and reports required files
|
|
9
|
+
* that are simply missing.
|
|
10
|
+
*
|
|
11
|
+
* It never writes. The operator's files are theirs: a managed file is rewritten
|
|
12
|
+
* only when `refresh` has an anchor proving the disk still holds our bytes.
|
|
13
|
+
*/
|
|
14
|
+
import { createHash } from "node:crypto";
|
|
15
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
16
|
+
import { join } from "node:path";
|
|
17
|
+
import { inspectWorkflowBlock, type WorkflowStatus } from "./workflow.js";
|
|
18
|
+
import {
|
|
19
|
+
AGENTS_FILE,
|
|
20
|
+
BUDGETS,
|
|
21
|
+
CONTRACT_FILE,
|
|
22
|
+
FRAMEWORK_VERSION,
|
|
23
|
+
IMPLEMENTATIONS_DIR,
|
|
24
|
+
MEMORY_DIR,
|
|
25
|
+
MEMORY_FILES,
|
|
26
|
+
PLAN_FILE,
|
|
27
|
+
SIGNPOST_FILE,
|
|
28
|
+
STATE_FILE,
|
|
29
|
+
STATE_GITIGNORE,
|
|
30
|
+
UAT_CONTRACT_FILE,
|
|
31
|
+
} from "./spec.js";
|
|
32
|
+
import { renderTemplate } from "./templates.js";
|
|
33
|
+
import type { FrameworkState } from "./types.js";
|
|
34
|
+
|
|
35
|
+
export interface ManagedSpec {
|
|
36
|
+
readonly path: string;
|
|
37
|
+
readonly template: string;
|
|
38
|
+
readonly tokens: Record<string, string>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Files `refresh` owns. The set is also what state.toml anchors. */
|
|
42
|
+
export const MANAGED_SPECS: readonly ManagedSpec[] = [
|
|
43
|
+
{ path: CONTRACT_FILE, template: "memory-readme", tokens: {} },
|
|
44
|
+
{ path: SIGNPOST_FILE, template: "signpost", tokens: {} },
|
|
45
|
+
{
|
|
46
|
+
path: UAT_CONTRACT_FILE,
|
|
47
|
+
template: "uat-readme",
|
|
48
|
+
tokens: { implementationsDir: IMPLEMENTATIONS_DIR },
|
|
49
|
+
},
|
|
50
|
+
{ path: STATE_GITIGNORE, template: "state-gitignore", tokens: {} },
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
export function baseTokens(): Record<string, string> {
|
|
54
|
+
return {
|
|
55
|
+
frameworkVersion: String(FRAMEWORK_VERSION),
|
|
56
|
+
softBudget: String(BUDGETS.agentsMdSoftChars),
|
|
57
|
+
hardBudget: String(BUDGETS.agentsMdHardChars),
|
|
58
|
+
memoryBudget: String(BUDGETS.memoryFileChars),
|
|
59
|
+
implementationsBudget: String(BUDGETS.implementationsFiles),
|
|
60
|
+
implementationsDir: IMPLEMENTATIONS_DIR,
|
|
61
|
+
statePath: STATE_FILE,
|
|
62
|
+
contractPath: CONTRACT_FILE,
|
|
63
|
+
uatLedger: ".agents/uat/ledger.jsonl",
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The exact Markdown for the ask workflow block. */
|
|
68
|
+
export function workflowBlockText(): string {
|
|
69
|
+
return renderTemplate("workflow-block", {}, "en");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function renderManaged(spec: ManagedSpec): string {
|
|
73
|
+
return renderTemplate(spec.template, { ...baseTokens(), ...spec.tokens }, "en");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function sha256(text: string): string {
|
|
77
|
+
return createHash("sha256").update(text).digest("hex");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export type ManagedStatus = "current" | "stale" | "missing" | "hand-edited" | "unverifiable";
|
|
81
|
+
|
|
82
|
+
export interface ManagedParity {
|
|
83
|
+
readonly path: string;
|
|
84
|
+
readonly status: ManagedStatus;
|
|
85
|
+
readonly reason?: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Compare every managed file with the current template. `state` may be absent:
|
|
90
|
+
* without anchors every differing file is `unverifiable`, never a hand edit.
|
|
91
|
+
*/
|
|
92
|
+
export function inspectManaged(
|
|
93
|
+
root: string,
|
|
94
|
+
state?: FrameworkState,
|
|
95
|
+
): ManagedParity[] {
|
|
96
|
+
const byPath = new Map((state?.managed ?? []).map((entry) => [entry.path, entry]));
|
|
97
|
+
return MANAGED_SPECS.map((spec) => {
|
|
98
|
+
const absolute = join(root, spec.path);
|
|
99
|
+
if (!existsSync(absolute)) return { path: spec.path, status: "missing" };
|
|
100
|
+
let disk: string;
|
|
101
|
+
try {
|
|
102
|
+
disk = readFileSync(absolute, "utf8");
|
|
103
|
+
} catch (error) {
|
|
104
|
+
return {
|
|
105
|
+
path: spec.path,
|
|
106
|
+
status: "unverifiable",
|
|
107
|
+
reason: `could not read the file (${error instanceof Error ? error.message : String(error)})`,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
const rendered = renderManaged(spec);
|
|
111
|
+
const anchor = byPath.get(spec.path)?.templateSha256;
|
|
112
|
+
if (anchor === undefined) {
|
|
113
|
+
return disk === rendered
|
|
114
|
+
? { path: spec.path, status: "current" }
|
|
115
|
+
: {
|
|
116
|
+
path: spec.path,
|
|
117
|
+
status: "unverifiable",
|
|
118
|
+
reason: "no template anchor: cannot tell a hand edit from an older template",
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
if (sha256(disk) !== anchor) {
|
|
122
|
+
return {
|
|
123
|
+
path: spec.path,
|
|
124
|
+
status: "hand-edited",
|
|
125
|
+
reason: "the file no longer matches the bytes this tool last rendered",
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
return disk === rendered
|
|
129
|
+
? { path: spec.path, status: "current" }
|
|
130
|
+
: { path: spec.path, status: "stale" };
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Files the framework contract requires for a usable loop. Managed files are
|
|
136
|
+
* reported separately (they have their own refresh path); this list is what
|
|
137
|
+
* `/ask-init` can recreate mechanically from seeds.
|
|
138
|
+
*/
|
|
139
|
+
export function requiredFrameworkFiles(): string[] {
|
|
140
|
+
return [
|
|
141
|
+
AGENTS_FILE,
|
|
142
|
+
...MEMORY_FILES.filter((file) => file.core).map((file) => `${MEMORY_DIR}/${file.file}`),
|
|
143
|
+
PLAN_FILE,
|
|
144
|
+
];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function missingRequired(root: string): string[] {
|
|
148
|
+
return requiredFrameworkFiles().filter((rel) => !isFile(join(root, rel)));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** A directory named `AGENTS.md` is not an entry file; readiness checks types. */
|
|
152
|
+
function isFile(absolute: string): boolean {
|
|
153
|
+
try {
|
|
154
|
+
return statSync(absolute).isFile();
|
|
155
|
+
} catch {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function isDirectory(absolute: string): boolean {
|
|
161
|
+
try {
|
|
162
|
+
return statSync(absolute).isDirectory();
|
|
163
|
+
} catch {
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** The workflow block in AGENTS.md, or a reason it cannot be judged. */
|
|
169
|
+
export function inspectWorkflow(root: string): WorkflowStatus {
|
|
170
|
+
const absolute = join(root, AGENTS_FILE);
|
|
171
|
+
if (!existsSync(absolute)) return "missing-agents";
|
|
172
|
+
try {
|
|
173
|
+
const text = readFileSync(absolute, "utf8");
|
|
174
|
+
return inspectWorkflowBlock(text, workflowBlockText()).status;
|
|
175
|
+
} catch {
|
|
176
|
+
return "malformed";
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export interface FrameworkParity {
|
|
181
|
+
readonly managed: ManagedParity[];
|
|
182
|
+
readonly requiredMissing: string[];
|
|
183
|
+
readonly workflow: WorkflowStatus;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function inspectFramework(root: string, state?: FrameworkState): FrameworkParity {
|
|
187
|
+
return {
|
|
188
|
+
managed: inspectManaged(root, state),
|
|
189
|
+
requiredMissing: missingRequired(root),
|
|
190
|
+
workflow: inspectWorkflow(root),
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Compact lines for the plan preview, the audit report and a no-op notice. */
|
|
195
|
+
export function describeParity(parity: FrameworkParity): string[] {
|
|
196
|
+
const lines: string[] = [];
|
|
197
|
+
const drift = parity.managed.filter((entry) => entry.status !== "current");
|
|
198
|
+
if (drift.length === 0) {
|
|
199
|
+
lines.push("- framework files: current");
|
|
200
|
+
} else {
|
|
201
|
+
for (const entry of drift) {
|
|
202
|
+
lines.push(`- ${entry.status}: ${entry.path}${entry.reason ? ` (${entry.reason})` : ""}`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
lines.push(`- workflow block: ${parity.workflow}`);
|
|
206
|
+
if (parity.requiredMissing.length > 0) {
|
|
207
|
+
lines.push(`- required files missing: ${parity.requiredMissing.join(", ")}`);
|
|
208
|
+
}
|
|
209
|
+
return lines;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* The tolerant "can the ask loop run here?" predicate.
|
|
214
|
+
*
|
|
215
|
+
* Deliberately feature-based, not byte-based: small edits to a memory file do
|
|
216
|
+
* not break the loop, so they must not block `/ask-uat` or `record_change`.
|
|
217
|
+
*/
|
|
218
|
+
export interface FrameworkReadiness {
|
|
219
|
+
readonly ready: boolean;
|
|
220
|
+
readonly missing: string[];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function frameworkReadiness(root: string): FrameworkReadiness {
|
|
224
|
+
const missing = [STATE_FILE, AGENTS_FILE, CONTRACT_FILE].filter(
|
|
225
|
+
(rel) => !isFile(join(root, rel)),
|
|
226
|
+
);
|
|
227
|
+
if (!isDirectory(join(root, IMPLEMENTATIONS_DIR))) missing.push(IMPLEMENTATIONS_DIR);
|
|
228
|
+
return { ready: missing.length === 0, missing };
|
|
229
|
+
}
|
|
@@ -41,6 +41,7 @@ import {
|
|
|
41
41
|
} from "./spec.js";
|
|
42
42
|
import { duplicateHeadings, largestSections, sectionsAfterAppendix, tombstones } from "./analyze.js";
|
|
43
43
|
import { countBareRefs } from "./detect.js";
|
|
44
|
+
import { missingRequired } from "./parity.js";
|
|
44
45
|
import { indexBlock, rootsBlock } from "./render.js";
|
|
45
46
|
import { renderTemplate } from "./templates.js";
|
|
46
47
|
import type {
|
|
@@ -181,7 +182,10 @@ function selectMode(facts: Facts): PlanMode {
|
|
|
181
182
|
!facts.retiredTodos &&
|
|
182
183
|
// A PLAN.md left in memory is pre-v1 layout, not an installed plan home.
|
|
183
184
|
!existsSync(join(facts.root, MEMORY_DIR, "PLAN.md")) &&
|
|
184
|
-
existsSync(join(facts.root, CONTRACT_FILE))
|
|
185
|
+
existsSync(join(facts.root, CONTRACT_FILE)) &&
|
|
186
|
+
// AGENTS.md and the core memory seeds are what make the loop readable;
|
|
187
|
+
// missing ones are mechanical repairs, not a reason to audit only.
|
|
188
|
+
missingRequired(facts.root).length === 0;
|
|
185
189
|
return layoutOk ? "audit-only" : "reconcile";
|
|
186
190
|
}
|
|
187
191
|
const anyContent =
|
|
@@ -329,6 +333,7 @@ function editAgentsTask(facts: Facts, deep: boolean): Task | undefined {
|
|
|
329
333
|
acceptance: [
|
|
330
334
|
"no fact was dropped: every removed sentence is either still present verbatim in the destination or listed in the coverage table",
|
|
331
335
|
`AGENTS.md stays under ${BUDGETS.agentsMdHardChars} characters (soft: ${BUDGETS.agentsMdSoftChars})`,
|
|
336
|
+
"the ask workflow markers (`<!-- ask-init:begin workflow … -->` … `<!-- ask-init:end workflow -->`) stay intact; the block inside them is framework-owned",
|
|
332
337
|
],
|
|
333
338
|
};
|
|
334
339
|
}
|
|
@@ -566,6 +571,10 @@ export function buildPlan(facts: Facts, options: PlanOptions): Plan {
|
|
|
566
571
|
const snapshotPaths = [
|
|
567
572
|
AGENTS_FILE,
|
|
568
573
|
CLAUDE_FILE,
|
|
574
|
+
CONTRACT_FILE,
|
|
575
|
+
SIGNPOST_FILE,
|
|
576
|
+
UAT_CONTRACT_FILE,
|
|
577
|
+
STATE_GITIGNORE,
|
|
569
578
|
facts.deprecated.memory ? "memory" : "",
|
|
570
579
|
facts.deprecated.implementations ? "implementations" : "",
|
|
571
580
|
facts.deprecated.prompts ? "prompts" : "",
|
|
@@ -738,7 +747,6 @@ export function buildPlan(facts: Facts, options: PlanOptions): Plan {
|
|
|
738
747
|
}
|
|
739
748
|
|
|
740
749
|
/* Tasks. */
|
|
741
|
-
if (mode !== "upgrade") tasks.push(surveyTask(facts));
|
|
742
750
|
const edit = editAgentsTask(facts, deep);
|
|
743
751
|
if (edit) tasks.push(edit);
|
|
744
752
|
else if (!facts.context.agents) {
|
|
@@ -808,9 +816,20 @@ export function buildPlan(facts: Facts, options: PlanOptions): Plan {
|
|
|
808
816
|
if (roots) tasks.push(roots);
|
|
809
817
|
const budget = budgetTask(facts);
|
|
810
818
|
if (budget) tasks.push(budget);
|
|
811
|
-
const
|
|
819
|
+
const needsCoverage =
|
|
820
|
+
mode === "adopt" ||
|
|
821
|
+
facts.legacyMemory.length > 0 ||
|
|
822
|
+
facts.legacyDocs.length > 0 ||
|
|
823
|
+
facts.todos.length > 0;
|
|
824
|
+
const coverage = needsCoverage ? coverageTask(facts, now) : undefined;
|
|
812
825
|
if (coverage) tasks.push(coverage);
|
|
813
826
|
|
|
827
|
+
// A reconcile caused only by missing files/directories is mechanical: no
|
|
828
|
+
// survey and no model turn. A survey is only worth paying for when there is
|
|
829
|
+
// prose work to decide; fresh/adopt always need it.
|
|
830
|
+
const mechanicalOnly = mode === "reconcile" && tasks.length === 0;
|
|
831
|
+
if (!mechanicalOnly) tasks.unshift(surveyTask(facts));
|
|
832
|
+
|
|
814
833
|
/* State last: it records what this run created and moved. */
|
|
815
834
|
actions.push({ kind: "write-state", state: buildState(facts, now, options.toolVersion, mode) });
|
|
816
835
|
|
|
@@ -837,6 +856,9 @@ export function buildPlan(facts: Facts, options: PlanOptions): Plan {
|
|
|
837
856
|
if (creates.length > 0) {
|
|
838
857
|
summary.push(`create ${creates.length} files (contract, signpost, seeds, state)`);
|
|
839
858
|
}
|
|
859
|
+
if (mechanicalOnly) {
|
|
860
|
+
summary.push("mechanical repair only — no model tasks");
|
|
861
|
+
}
|
|
840
862
|
if (tasks.length > 0) {
|
|
841
863
|
summary.push(`hand ${tasks.length} tasks to the model: ${tasks.map((task) => task.id).join(", ")}`);
|
|
842
864
|
}
|
|
@@ -4,7 +4,7 @@ import { join } from "node:path";
|
|
|
4
4
|
import { afterEach, describe, expect, it } from "vitest";
|
|
5
5
|
import { stateToToml } from "./apply.js";
|
|
6
6
|
import { loadState } from "./detect.js";
|
|
7
|
-
import { applyWorkflowBlock, refreshFramework } from "./refresh.js";
|
|
7
|
+
import { applyWorkflowBlock, describeRefresh, refreshFramework } from "./refresh.js";
|
|
8
8
|
import { CONTRACT_FILE, UAT_CONTRACT_FILE, UAT_DIR, WORKFLOW_BLOCK_BEGIN, WORKFLOW_BLOCK_END } from "./spec.js";
|
|
9
9
|
import { installedProject, remove } from "./test-helpers.js";
|
|
10
10
|
|
|
@@ -80,6 +80,62 @@ describe("workflow block", () => {
|
|
|
80
80
|
const appended = applyWorkflowBlock("# demo\n\n## Body\n\ntext\n", block);
|
|
81
81
|
expect(appended.text.trimEnd().endsWith(WORKFLOW_BLOCK_END)).toBe(true);
|
|
82
82
|
});
|
|
83
|
+
|
|
84
|
+
it("never treats inline marker examples as the managed block", () => {
|
|
85
|
+
const text = [
|
|
86
|
+
"# demo",
|
|
87
|
+
"",
|
|
88
|
+
"- docs: `<!-- ask-init:begin workflow v1 -->`",
|
|
89
|
+
" `<!-- ask-init:end workflow -->`,块外文字保持原样",
|
|
90
|
+
"",
|
|
91
|
+
"## Body",
|
|
92
|
+
"",
|
|
93
|
+
"tail",
|
|
94
|
+
].join("\n");
|
|
95
|
+
const block = `${WORKFLOW_BLOCK_BEGIN} v1 -->\n## Ask workflow\n${WORKFLOW_BLOCK_END}`;
|
|
96
|
+
const result = applyWorkflowBlock(text, block);
|
|
97
|
+
expect(result.status).toBe("inserted");
|
|
98
|
+
expect(result.text).toContain("`<!-- ask-init:begin workflow v1 -->`");
|
|
99
|
+
expect(result.text).toContain("块外文字保持原样");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("ignores markers inside fenced code blocks", () => {
|
|
103
|
+
const text = [
|
|
104
|
+
"# demo",
|
|
105
|
+
"",
|
|
106
|
+
"```md",
|
|
107
|
+
"<!-- ask-init:begin workflow v1 -->",
|
|
108
|
+
"## example",
|
|
109
|
+
"<!-- ask-init:end workflow -->",
|
|
110
|
+
"```",
|
|
111
|
+
"",
|
|
112
|
+
].join("\n");
|
|
113
|
+
const block = `${WORKFLOW_BLOCK_BEGIN} v1 -->\n## Ask workflow\n${WORKFLOW_BLOCK_END}`;
|
|
114
|
+
const result = applyWorkflowBlock(text, block);
|
|
115
|
+
expect(result.status).toBe("inserted");
|
|
116
|
+
expect(result.text).toContain("## example");
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("does not add a second block when a hand-written Ask workflow section exists", () => {
|
|
120
|
+
const text = "# demo\n\n## Ask workflow\n\nDo it by hand.\n";
|
|
121
|
+
const block = `${WORKFLOW_BLOCK_BEGIN} v1 -->\n## Ask workflow\n${WORKFLOW_BLOCK_END}`;
|
|
122
|
+
const result = applyWorkflowBlock(text, block);
|
|
123
|
+
expect(result.status).toBe("hand-written");
|
|
124
|
+
expect(result.text).toBe(text);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("reports malformed marker layouts without touching the file", () => {
|
|
128
|
+
const block = `${WORKFLOW_BLOCK_BEGIN} v1 -->\n## Ask workflow\n${WORKFLOW_BLOCK_END}`;
|
|
129
|
+
const beginOnly = `# demo\n\n${WORKFLOW_BLOCK_BEGIN} v1 -->\n## old\n`;
|
|
130
|
+
const first = applyWorkflowBlock(beginOnly, block);
|
|
131
|
+
expect(first.status).toBe("malformed");
|
|
132
|
+
expect(first.text).toBe(beginOnly);
|
|
133
|
+
|
|
134
|
+
const endFirst = `# demo\n\n${WORKFLOW_BLOCK_END}\n## old\n${WORKFLOW_BLOCK_BEGIN} v1 -->\n`;
|
|
135
|
+
const second = applyWorkflowBlock(endFirst, block);
|
|
136
|
+
expect(second.status).toBe("malformed");
|
|
137
|
+
expect(second.text).toBe(endFirst);
|
|
138
|
+
});
|
|
83
139
|
});
|
|
84
140
|
|
|
85
141
|
describe("refreshFramework", () => {
|
|
@@ -142,9 +198,18 @@ describe("refreshFramework", () => {
|
|
|
142
198
|
const result = refreshFramework(root);
|
|
143
199
|
expect(result.updated).not.toContain(CONTRACT_FILE);
|
|
144
200
|
expect(result.skipped.map((entry) => entry.path)).toContain(CONTRACT_FILE);
|
|
201
|
+
expect(result.skipped.find((entry) => entry.path === CONTRACT_FILE)?.reason).toMatch(/edited by hand/);
|
|
145
202
|
expect(readFileSync(join(root, CONTRACT_FILE), "utf8")).toBe("my own contract\n");
|
|
146
203
|
});
|
|
147
204
|
|
|
205
|
+
it("reports a missing AGENTS.md instead of calling the framework current", () => {
|
|
206
|
+
const root = installed();
|
|
207
|
+
remove(join(root, "AGENTS.md"));
|
|
208
|
+
const result = refreshFramework(root, { dryRun: true });
|
|
209
|
+
expect(result.block).toBe("missing-agents");
|
|
210
|
+
expect(describeRefresh(result)).toContain("AGENTS.md is missing");
|
|
211
|
+
});
|
|
212
|
+
|
|
148
213
|
it("writes nothing in a dry run", () => {
|
|
149
214
|
const root = installed();
|
|
150
215
|
const before = readFileSync(join(root, "AGENTS.md"), "utf8");
|
|
@@ -175,6 +240,28 @@ describe("refreshFramework", () => {
|
|
|
175
240
|
expect(readFileSync(join(root, CONTRACT_FILE), "utf8")).toBe(edited);
|
|
176
241
|
});
|
|
177
242
|
|
|
243
|
+
it("adopts a legacy file only when --adopt is explicit", () => {
|
|
244
|
+
const root = installed();
|
|
245
|
+
const legacy = "legacy contract from an older version\n";
|
|
246
|
+
writeFileSync(join(root, CONTRACT_FILE), legacy, "utf8");
|
|
247
|
+
writeState(root, [
|
|
248
|
+
{
|
|
249
|
+
path: CONTRACT_FILE,
|
|
250
|
+
sha256: sha256(legacy),
|
|
251
|
+
template: "memory-readme",
|
|
252
|
+
templateSha256: undefined,
|
|
253
|
+
},
|
|
254
|
+
]);
|
|
255
|
+
const skipped = refreshFramework(root);
|
|
256
|
+
expect(skipped.updated).not.toContain(CONTRACT_FILE);
|
|
257
|
+
expect(skipped.skipped.map((entry) => entry.path)).toContain(CONTRACT_FILE);
|
|
258
|
+
|
|
259
|
+
const adopted = refreshFramework(root, { adoptLegacy: true });
|
|
260
|
+
expect(adopted.adopted).toContain(CONTRACT_FILE);
|
|
261
|
+
expect(adopted.updated).toContain(CONTRACT_FILE);
|
|
262
|
+
expect(readFileSync(join(root, CONTRACT_FILE), "utf8")).not.toBe(legacy);
|
|
263
|
+
});
|
|
264
|
+
|
|
178
265
|
it("persists missing template anchors even when no file content changes", () => {
|
|
179
266
|
const root = installed();
|
|
180
267
|
refreshFramework(root, { toolVersion: "test" });
|