@andares/pi-toolkits 0.13.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 +56 -0
- package/README.md +123 -25
- package/README.zh-CN.md +75 -21
- package/package.json +14 -4
- 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/permission/policy.test.ts +10 -0
- package/src/features/permission/units.test.ts +157 -0
- package/src/features/permission/units.ts +317 -15
- 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/features/whats-new/changelog.ts +104 -0
- package/src/features/whats-new/constants.ts +35 -0
- package/src/features/whats-new/index.ts +146 -0
- package/src/features/whats-new/notice.ts +68 -0
- package/src/features/whats-new/paths.ts +29 -0
- package/src/features/whats-new/store.ts +114 -0
- package/src/features/whats-new/version.ts +43 -0
- package/src/features/whats-new/whats-new.test.ts +460 -0
- package/src/index.ts +3 -0
- package/src/lib/phases.ts +37 -0
|
@@ -65,6 +65,7 @@ export function installedProject(extra: Record<string, string> = {}): string {
|
|
|
65
65
|
"| --- | --- | --- |",
|
|
66
66
|
"| `specs/PLAN.md` | route | before work |",
|
|
67
67
|
"| `.agents/memory/FINISHED.md` | closed work | closing |",
|
|
68
|
+
"| `.agents/implementations/` | change records | after work |",
|
|
68
69
|
"",
|
|
69
70
|
].join("\n"),
|
|
70
71
|
"specs/PLAN.md": "# Plan (open work)\n\n## Route\n\n_None._\n",
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* init feature — the one framework-owned region inside `AGENTS.md`.
|
|
3
|
+
*
|
|
4
|
+
* The workflow block is delimited by HTML comments. Matching must be strict:
|
|
5
|
+
* documentation often *shows* these markers as inline code, and a plain
|
|
6
|
+
* `indexOf` would treat such an example as the real block and replace the
|
|
7
|
+
* surrounding prose. Markers therefore have to occupy a whole line, sit outside
|
|
8
|
+
* a fenced code block, and come in exactly one matching pair.
|
|
9
|
+
*/
|
|
10
|
+
import { WORKFLOW_BLOCK_BEGIN, WORKFLOW_BLOCK_END } from "./spec.js";
|
|
11
|
+
|
|
12
|
+
export type BlockStatus =
|
|
13
|
+
| "current"
|
|
14
|
+
| "updated"
|
|
15
|
+
| "inserted"
|
|
16
|
+
| "malformed"
|
|
17
|
+
| "hand-written"
|
|
18
|
+
| "missing-agents";
|
|
19
|
+
|
|
20
|
+
/** What an existing file looks like; `missing-agents` is a file-level fact. */
|
|
21
|
+
export type WorkflowStatus =
|
|
22
|
+
| "current"
|
|
23
|
+
| "stale"
|
|
24
|
+
| "missing"
|
|
25
|
+
| "malformed"
|
|
26
|
+
| "hand-written"
|
|
27
|
+
| "missing-agents";
|
|
28
|
+
|
|
29
|
+
interface LineSpan {
|
|
30
|
+
readonly start: number;
|
|
31
|
+
/** Offset just after the line's newline (or the end of the text). */
|
|
32
|
+
readonly end: number;
|
|
33
|
+
readonly text: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const BEGIN_RE = /^<!--\s*ask-init:begin workflow\b[^\n]*-->\s*$/;
|
|
37
|
+
const END_RE = /^<!--\s*ask-init:end workflow\s*-->\s*$/;
|
|
38
|
+
const FENCE_RE = /^\s*(```+|~~~+)/;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Lines outside fenced code blocks, with their byte offsets. The fence lines
|
|
42
|
+
* themselves are skipped; a marker cannot share a line with them.
|
|
43
|
+
*/
|
|
44
|
+
function linesOutsideFences(text: string): LineSpan[] {
|
|
45
|
+
const out: LineSpan[] = [];
|
|
46
|
+
const raw = text.split("\n");
|
|
47
|
+
let offset = 0;
|
|
48
|
+
let fence: string | undefined;
|
|
49
|
+
for (let index = 0; index < raw.length; index += 1) {
|
|
50
|
+
const line = raw[index];
|
|
51
|
+
const start = offset;
|
|
52
|
+
const hasNewline = index < raw.length - 1;
|
|
53
|
+
const end = start + line.length + (hasNewline ? 1 : 0);
|
|
54
|
+
offset = end;
|
|
55
|
+
const visible = line.endsWith("\r") ? line.slice(0, -1) : line;
|
|
56
|
+
const fenceMatch = FENCE_RE.exec(visible);
|
|
57
|
+
if (fenceMatch) {
|
|
58
|
+
const marker = fenceMatch[1][0];
|
|
59
|
+
if (!fence) fence = marker;
|
|
60
|
+
else if (fence === marker) fence = undefined;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (fence) continue;
|
|
64
|
+
out.push({ start, end, text: visible });
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
type Located =
|
|
70
|
+
| { kind: "none" }
|
|
71
|
+
| { kind: "malformed"; reason: string }
|
|
72
|
+
| { kind: "found"; begin: LineSpan; end: LineSpan };
|
|
73
|
+
|
|
74
|
+
/** Locate exactly one marker pair; anything ambiguous is malformed. */
|
|
75
|
+
export function locateWorkflowBlock(text: string): Located {
|
|
76
|
+
const lines = linesOutsideFences(text);
|
|
77
|
+
const begins = lines.filter((line) => BEGIN_RE.test(line.text));
|
|
78
|
+
const ends = lines.filter((line) => END_RE.test(line.text));
|
|
79
|
+
if (begins.length === 0 && ends.length === 0) return { kind: "none" };
|
|
80
|
+
if (begins.length === 0 || ends.length === 0) {
|
|
81
|
+
return {
|
|
82
|
+
kind: "malformed",
|
|
83
|
+
reason: begins.length === 0 ? "end marker without begin marker" : "begin marker without end marker",
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
if (begins.length > 1 || ends.length > 1) {
|
|
87
|
+
return { kind: "malformed", reason: "more than one workflow marker pair" };
|
|
88
|
+
}
|
|
89
|
+
const begin = begins[0];
|
|
90
|
+
const end = ends[0];
|
|
91
|
+
if (end.start < begin.start) {
|
|
92
|
+
return { kind: "malformed", reason: "end marker appears before the begin marker" };
|
|
93
|
+
}
|
|
94
|
+
return { kind: "found", begin, end };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Replace the block between a valid marker pair, or insert it. Any malformed
|
|
99
|
+
* marker layout returns the input untouched so prose can never be damaged.
|
|
100
|
+
*/
|
|
101
|
+
export function applyWorkflowBlock(
|
|
102
|
+
text: string,
|
|
103
|
+
block: string,
|
|
104
|
+
): { text: string; status: Exclude<BlockStatus, "missing-agents"> } {
|
|
105
|
+
const located = locateWorkflowBlock(text);
|
|
106
|
+
if (located.kind === "malformed") return { text, status: "malformed" };
|
|
107
|
+
if (located.kind === "found") {
|
|
108
|
+
const suffix = text.slice(located.end.end);
|
|
109
|
+
// Keep the final newline when the block is the last content: dropping it
|
|
110
|
+
// makes every second refresh look like an update.
|
|
111
|
+
const next = `${text.slice(0, located.begin.start)}${block.trimEnd()}${
|
|
112
|
+
suffix.length === 0 && text.endsWith("\n") ? "\n" : suffix
|
|
113
|
+
}`;
|
|
114
|
+
return { text: next, status: next === text ? "current" : "updated" };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const appendix = linesOutsideFences(text).find((line) =>
|
|
118
|
+
/^##[ \t]+(?:附录|appendix)(?:[ \t]|$)/i.test(line.text),
|
|
119
|
+
);
|
|
120
|
+
// A project may have written its own workflow section. Do not append a
|
|
121
|
+
// second one: report it and let the operator add markers or remove it.
|
|
122
|
+
const handWritten = linesOutsideFences(text).some((line) =>
|
|
123
|
+
/^##[ \t]+Ask workflow\b/i.test(line.text),
|
|
124
|
+
);
|
|
125
|
+
if (handWritten) return { text, status: "hand-written" };
|
|
126
|
+
if (appendix) {
|
|
127
|
+
return {
|
|
128
|
+
text: `${text.slice(0, appendix.start)}${block.trimEnd()}\n\n${text.slice(appendix.start)}`,
|
|
129
|
+
status: "inserted",
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
if (text.trim() === "") return { text: `${block.trimEnd()}\n`, status: "inserted" };
|
|
133
|
+
return { text: `${text.trimEnd()}\n\n${block.trimEnd()}\n`, status: "inserted" };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Inspect a present AGENTS.md without changing it. */
|
|
137
|
+
export function inspectWorkflowBlock(
|
|
138
|
+
text: string,
|
|
139
|
+
expected: string,
|
|
140
|
+
): { status: Exclude<WorkflowStatus, "missing-agents">; reason?: string } {
|
|
141
|
+
const located = locateWorkflowBlock(text);
|
|
142
|
+
if (located.kind === "malformed") return { status: "malformed", reason: located.reason };
|
|
143
|
+
if (located.kind === "found") {
|
|
144
|
+
const actual = text.slice(located.begin.start, located.end.end).trimEnd();
|
|
145
|
+
return { status: actual === expected.trimEnd() ? "current" : "stale" };
|
|
146
|
+
}
|
|
147
|
+
const handWritten = linesOutsideFences(text).some((line) =>
|
|
148
|
+
/^##[ \t]+Ask workflow\b/i.test(line.text),
|
|
149
|
+
);
|
|
150
|
+
if (handWritten) {
|
|
151
|
+
return {
|
|
152
|
+
status: "hand-written",
|
|
153
|
+
reason: "a hand-written 'Ask workflow' section exists without the managed markers",
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
return { status: "missing" };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export { WORKFLOW_BLOCK_BEGIN, WORKFLOW_BLOCK_END };
|
|
@@ -215,6 +215,16 @@ describe("fail closed", () => {
|
|
|
215
215
|
const decision = decide('cd "$DIR" && rm x', ctx);
|
|
216
216
|
expect(decision.action).toBe("ask");
|
|
217
217
|
});
|
|
218
|
+
|
|
219
|
+
it("does not ignore an unresolved input redirect under strict read modes", () => {
|
|
220
|
+
const cwd = project();
|
|
221
|
+
for (const outRead of ["ask", "deny"] as const) {
|
|
222
|
+
const ctx = makeContext(cwd, { ...DEFAULT_MODES, outRead });
|
|
223
|
+
const decision = decide('cat < "$UNKNOWN"', ctx);
|
|
224
|
+
expect(decision.action).toBe("ask");
|
|
225
|
+
expect(decision.layer).toBe("opaque");
|
|
226
|
+
}
|
|
227
|
+
});
|
|
218
228
|
});
|
|
219
229
|
|
|
220
230
|
describe("decision helpers", () => {
|
|
@@ -219,3 +219,160 @@ describe("fail closed", () => {
|
|
|
219
219
|
expect(first('bash -c "$CMD"').opaque).toBe(true);
|
|
220
220
|
});
|
|
221
221
|
});
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Regressions from the 2026-10-26 → 2026-11-12 approval log (491 records,
|
|
225
|
+
* 61 asks; replaying the commands against the parser showed 52 would still
|
|
226
|
+
* ask). Each case below is one of the false-positive classes that log exposed.
|
|
227
|
+
*/
|
|
228
|
+
describe("approval-log regressions", () => {
|
|
229
|
+
it("keeps a trailing `$` inside a quoted pattern literal", () => {
|
|
230
|
+
const unit = first('grep -n "</div>\\s*$" src/a.vue');
|
|
231
|
+
expect(unit).toMatchObject({ command: "grep", effect: "read", opaque: false });
|
|
232
|
+
expect(unit.targets.map((target) => target.path)).toEqual(["/proj/src/a.vue"]);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("keeps `$` before `|` and `)` inside a quoted pattern literal", () => {
|
|
236
|
+
for (const command of [
|
|
237
|
+
'grep -E "\\.php$"',
|
|
238
|
+
'grep -cE "<STable($|[^A-Za-z])" f',
|
|
239
|
+
'grep -n "label:\\|action\\|$" f',
|
|
240
|
+
]) {
|
|
241
|
+
expect(first(command).opaque).toBe(false);
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it("treats a backtick inside single quotes as pattern text", () => {
|
|
246
|
+
// Shell: the backtick is literal inside '…'; the old scanner saw an
|
|
247
|
+
// unterminated command substitution and failed the whole command closed.
|
|
248
|
+
const unit = first("grep -rn ':title=\"x`' src/views");
|
|
249
|
+
expect(unit.opaque).toBe(false);
|
|
250
|
+
expect(unit.note).toBeUndefined();
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it("splits commands separated by a newline (missing writes were possible)", () => {
|
|
254
|
+
const units = summary("ls\nrm -rf /tmp/x");
|
|
255
|
+
expect(units.map((unit) => unit.command)).toEqual(["ls", "rm"]);
|
|
256
|
+
expect(units[1]?.targets).toEqual(["write:/tmp/x"]);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("joins a backslash-newline as a line continuation", () => {
|
|
260
|
+
const units = summary("echo a \\\nb");
|
|
261
|
+
expect(units).toHaveLength(1);
|
|
262
|
+
expect(units[0]?.command).toBe("echo");
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it("gives a bare assignment no unit", () => {
|
|
266
|
+
expect(parseCommandUnits("f=2026-10-29.jsonl", CWD)).toEqual([]);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it("leaves ordinary assignments unresolved without control-flow analysis", () => {
|
|
270
|
+
const units = summary('f=notes.md; grep -n x "src/$f"');
|
|
271
|
+
expect(units).toHaveLength(1);
|
|
272
|
+
expect(units[0]).toMatchObject({ command: "grep", effect: "write", opaque: true });
|
|
273
|
+
expect(units[0]?.targets).toEqual([]);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it("does not substitute a non-literal assignment", () => {
|
|
277
|
+
const units = summary('f=$(date); sed -n 1p "d/$f"');
|
|
278
|
+
expect(units.find((unit) => unit.command === "sed")?.opaque).toBe(true);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("expands a literal for-loop over real paths", () => {
|
|
282
|
+
const units = summary('for f in a b; do grep -n x "src/$f.vue"; done');
|
|
283
|
+
expect(units.map((unit) => unit.command)).toEqual(["grep", "grep"]);
|
|
284
|
+
expect(units.map((unit) => unit.targets)).toEqual([
|
|
285
|
+
["read:/proj/src/a.vue"],
|
|
286
|
+
["read:/proj/src/b.vue"],
|
|
287
|
+
]);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("expands a literal loop for cd-like flags (`git -C`)", () => {
|
|
291
|
+
const units = summary('for d in /a /b; do git -C "$d" status; done');
|
|
292
|
+
expect(units).toHaveLength(2);
|
|
293
|
+
expect(units[0]).toMatchObject({ command: "git", base: "/a", opaque: false });
|
|
294
|
+
expect(units[1]).toMatchObject({ command: "git", base: "/b", opaque: false });
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("leaves a loop over a substitution unexpanded (fail closed)", () => {
|
|
298
|
+
const units = summary('for f in $(ls); do sed -n 1p "d/$f"; done');
|
|
299
|
+
const sed = units.find((unit) => unit.command === "sed");
|
|
300
|
+
expect(sed?.opaque).toBe(true);
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it("treats a plain `env` as a read, not an opaque wrapper", () => {
|
|
304
|
+
const units = summary('env | grep -i "^PI_"');
|
|
305
|
+
expect(units.map((unit) => [unit.command, unit.effect, unit.opaque])).toEqual([
|
|
306
|
+
["env", "read", false],
|
|
307
|
+
["grep", "read", false],
|
|
308
|
+
]);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("does not treat curl data flags as path operands", () => {
|
|
312
|
+
const unit = first("curl -s -o /dev/null -w '%{http_code}\\n' http://localhost:5173/");
|
|
313
|
+
expect(unit.opaque).toBe(false);
|
|
314
|
+
expect(unit.targets).toEqual([]);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it("keeps an unresolved input redirect fail-closed", () => {
|
|
318
|
+
const unit = first('wc -l < "$f"');
|
|
319
|
+
expect(unit).toMatchObject({ command: "wc", effect: "write", opaque: true });
|
|
320
|
+
expect(unit.note).toBe("unresolved redirect target");
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it("still fails closed on an unresolved output redirect", () => {
|
|
324
|
+
expect(first('echo x > "$f"').opaque).toBe(true);
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
it("classifies container inspection subcommands as reads", () => {
|
|
328
|
+
expect(first("docker compose ps --format '{{.Name}}'").effect).toBe("read");
|
|
329
|
+
expect(first("docker ps -a").effect).toBe("read");
|
|
330
|
+
expect(first("docker compose exec worker php -l x").effect).toBe("write");
|
|
331
|
+
expect(first("docker run --rm alpine sh").effect).toBe("write");
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it("does not let a previous assignment shadow a literal loop variable", () => {
|
|
335
|
+
const units = summary('f=/proj; for f in /etc; do rm "$f/x"; done');
|
|
336
|
+
expect(units).toHaveLength(1);
|
|
337
|
+
expect(units[0]?.targets).toEqual(["write:/etc/x"]);
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
it("uses the inner binding for nested loops with the same variable", () => {
|
|
341
|
+
const units = summary('for f in /proj; do for f in /etc; do rm "$f/x"; done; done');
|
|
342
|
+
expect(units).toHaveLength(1);
|
|
343
|
+
expect(units[0]?.targets).toEqual(["write:/etc/x"]);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
it("stops loop expansion after the variable is reassigned", () => {
|
|
347
|
+
const unit = first('for f in /proj; do f=/etc; rm "$f/x"; done');
|
|
348
|
+
expect(unit).toMatchObject({ command: "rm", opaque: true });
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
it("stops loop expansion after a builtin can mutate the variable", () => {
|
|
352
|
+
const unit = summary('for f in /proj; do read f; rm "$f/x"; done')
|
|
353
|
+
.find((entry) => entry.command === "rm");
|
|
354
|
+
expect(unit).toMatchObject({ command: "rm", opaque: true });
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it("does not treat a conditional assignment as having executed", () => {
|
|
358
|
+
const unit = summary('false && d=/proj; rm "$d/x"')
|
|
359
|
+
.find((entry) => entry.command === "rm");
|
|
360
|
+
expect(unit).toMatchObject({ command: "rm", opaque: true });
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
it("does not reuse a literal assignment after `read` changes it", () => {
|
|
364
|
+
const unit = summary('d=/proj; read d; rm "$d/x"')
|
|
365
|
+
.find((entry) => entry.command === "rm");
|
|
366
|
+
expect(unit).toMatchObject({ command: "rm", opaque: true });
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it("keeps container output options as writes", () => {
|
|
370
|
+
const unit = first("docker compose config --output /tmp/compose.yml");
|
|
371
|
+
expect(unit.effect).toBe("write");
|
|
372
|
+
expect(unit.targets).toContainEqual({
|
|
373
|
+
path: "/tmp/compose.yml",
|
|
374
|
+
effect: "write",
|
|
375
|
+
source: "operand",
|
|
376
|
+
});
|
|
377
|
+
});
|
|
378
|
+
});
|