@andares/pi-toolkits 0.12.4 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/README.md +100 -15
  2. package/README.zh-CN.md +94 -14
  3. package/package.json +19 -10
  4. package/src/features/ask/index.ts +6 -0
  5. package/src/features/init/apply.ts +32 -12
  6. package/src/features/init/audit-uat.test.ts +103 -0
  7. package/src/features/init/audit.ts +148 -0
  8. package/src/features/init/detect.ts +4 -0
  9. package/src/features/init/index.ts +47 -2
  10. package/src/features/init/plan.ts +11 -0
  11. package/src/features/init/refresh.test.ts +208 -0
  12. package/src/features/init/refresh.ts +255 -0
  13. package/src/features/init/spec.ts +20 -0
  14. package/src/features/init/templates/agents-skeleton.md +2 -0
  15. package/src/features/init/templates/uat-readme.md +53 -0
  16. package/src/features/init/templates/workflow-block.md +21 -0
  17. package/src/features/init/templates.test.ts +3 -1
  18. package/src/features/init/templates.ts +2 -0
  19. package/src/features/init/types.ts +12 -1
  20. package/src/features/permission/index.ts +32 -0
  21. package/src/features/plan/assets/index-C1YLIi4I.js +1 -0
  22. package/src/features/plan/assets/index-DMEpYC3V.css +1 -0
  23. package/src/features/plan/assets/index.html +14 -0
  24. package/src/features/plan/assets.test.ts +64 -0
  25. package/src/features/plan/blocks.test.ts +77 -0
  26. package/src/features/plan/blocks.ts +153 -0
  27. package/src/features/plan/diff.test.ts +144 -0
  28. package/src/features/plan/diff.ts +193 -0
  29. package/src/features/plan/feedback.test.ts +233 -0
  30. package/src/features/plan/feedback.ts +182 -0
  31. package/src/features/plan/index.test.ts +385 -0
  32. package/src/features/plan/index.ts +433 -0
  33. package/src/features/plan/prompt.test.ts +91 -0
  34. package/src/features/plan/prompt.ts +110 -0
  35. package/src/features/plan/render.test.ts +94 -0
  36. package/src/features/plan/render.ts +131 -0
  37. package/src/features/plan/server.test.ts +374 -0
  38. package/src/features/plan/server.ts +272 -0
  39. package/src/features/plan/store.test.ts +145 -0
  40. package/src/features/plan/store.ts +244 -0
  41. package/src/features/review/index.ts +54 -28
  42. package/src/features/review/review.test.ts +46 -1
  43. package/src/features/uat/assets/index-D-W9zj0w.css +1 -0
  44. package/src/features/uat/assets/index-DCBQCsli.js +1 -0
  45. package/src/features/uat/assets/index.html +14 -0
  46. package/src/features/uat/constants.ts +23 -0
  47. package/src/features/uat/index.test.ts +404 -0
  48. package/src/features/uat/index.ts +329 -0
  49. package/src/features/uat/item-tool.ts +280 -0
  50. package/src/features/uat/ledger.test.ts +249 -0
  51. package/src/features/uat/ledger.ts +504 -0
  52. package/src/features/uat/prompt.ts +78 -0
  53. package/src/features/uat/record-tool.ts +167 -0
  54. package/src/features/uat/records.test.ts +145 -0
  55. package/src/features/uat/records.ts +268 -0
  56. package/src/features/uat/server.ts +181 -0
  57. package/src/features/uat/state.test.ts +41 -0
  58. package/src/features/uat/state.ts +67 -0
  59. package/src/index.ts +10 -1
  60. package/src/lib/ask-control.ts +25 -0
  61. package/src/lib/opener.test.ts +72 -0
  62. package/src/lib/opener.ts +121 -0
  63. package/src/lib/permission-control.ts +39 -0
  64. package/src/lib/startup.test.ts +1 -1
  65. package/src/lib/web-review.ts +341 -0
@@ -0,0 +1,233 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { parseBlocks } from "./blocks.js";
3
+ import { diffBlocks } from "./diff.js";
4
+ import { buildFeedbackMarkdown } from "./feedback.js";
5
+
6
+ const parse = (markdown: string) => parseBlocks(markdown);
7
+
8
+ describe("buildFeedbackMarkdown", () => {
9
+ it("writes a denial with global and block annotations", () => {
10
+ const blocks = parse("## Milestones\n\n1. blocks\n\n## Risks");
11
+ const text = buildFeedbackMarkdown({
12
+ decision: "deny",
13
+ version: 2,
14
+ totalVersions: 5,
15
+ blocks,
16
+ diff: diffBlocks(parse("## Milestones\n\n1. blocks\n\n## Risks"), blocks),
17
+ global: [{ verdict: "change", comment: "Keep the permission toggle out of the plan flow." }],
18
+ perBlock: [
19
+ {
20
+ blockId: blocks[1]!.id,
21
+ annotations: [{ verdict: "question", comment: "What stops a third round here?" }],
22
+ },
23
+ ],
24
+ });
25
+ expect(text).toContain("## Plan feedback — v2 of 5 (no block changes)");
26
+ expect(text).toContain("### Global notes (apply to the whole plan)");
27
+ expect(text).toContain("- [change] Keep the permission toggle out of the plan flow.");
28
+ expect(text).toContain("### Block notes (apply to the quoted block only)");
29
+ expect(text).toContain("#### line 3 · 1. blocks (unchanged)");
30
+ expect(text).toContain("> 1. blocks");
31
+ expect(text).toContain("- [question] What stops a third round here?");
32
+ expect(text).toContain(
33
+ "Update the plan file to address every annotation above, then submit it again with plan_submit.",
34
+ );
35
+ });
36
+
37
+ it("marks the changed count and quotes the previous first line for a modified block", () => {
38
+ const before = parse("## Milestones\n\n1. blocks and server");
39
+ const after = parse("## Milestones\n\n1. blocks, then the server");
40
+ const text = buildFeedbackMarkdown({
41
+ decision: "deny",
42
+ version: 3,
43
+ totalVersions: 5,
44
+ blocks: after,
45
+ diff: diffBlocks(before, after),
46
+ previousBlocks: before,
47
+ global: [],
48
+ perBlock: [
49
+ {
50
+ blockId: after[1]!.id,
51
+ annotations: [{ verdict: "change", comment: "Split it in two." }],
52
+ },
53
+ ],
54
+ });
55
+ expect(text).toContain("## Plan feedback — v3 of 5 (1 block changed)");
56
+ expect(text).toContain("#### line 3 · 1. blocks, then the server (modified)");
57
+ expect(text).toContain("> 1. blocks, then the server");
58
+ expect(text).toContain("- [change] Split it in two.");
59
+ });
60
+
61
+ it("keeps an approval short when there is nothing to say", () => {
62
+ const blocks = parse("## Plan");
63
+ const text = buildFeedbackMarkdown({
64
+ decision: "approve",
65
+ version: 3,
66
+ totalVersions: 3,
67
+ blocks,
68
+ diff: diffBlocks(blocks, blocks),
69
+ global: [],
70
+ perBlock: [],
71
+ });
72
+ expect(text).toBe(
73
+ [
74
+ "## Plan approved — v3 of 3 (no block changes)",
75
+ "",
76
+ "No notes were attached.",
77
+ "",
78
+ "The plan is approved: start implementing it now.",
79
+ 'Approval only means "go" — every annotation above still applies while you build.',
80
+ "Record the implementation work in the todo list (`todo` tool) as you go, and update each entry",
81
+ "as it starts, finishes or gets blocked, so progress stays traceable.",
82
+ ].join("\n"),
83
+ );
84
+ });
85
+
86
+ it("quotes the approved revision and asks for todo tracking on approval", () => {
87
+ const blocks = parse("## Plan\n\nstep one");
88
+ const text = buildFeedbackMarkdown({
89
+ decision: "approve",
90
+ version: 2,
91
+ totalVersions: 2,
92
+ blocks,
93
+ diff: diffBlocks(blocks, blocks),
94
+ global: [{ verdict: "suggest", comment: "Keep commits small." }],
95
+ perBlock: [],
96
+ planPath: "specs/demo.md",
97
+ planText: "## Plan\n\nstep one\n",
98
+ });
99
+ expect(text).toContain("### Approved plan (specs/demo.md)");
100
+ expect(text).toContain("## Plan\n\nstep one");
101
+ expect(text).toContain("Record the implementation work in the todo list");
102
+ expect(text).toContain("- [suggest] Keep commits small.");
103
+ });
104
+
105
+ it("does not quote a plan back on denial", () => {
106
+ const blocks = parse("## Plan");
107
+ const text = buildFeedbackMarkdown({
108
+ decision: "deny",
109
+ version: 1,
110
+ totalVersions: 1,
111
+ blocks,
112
+ diff: diffBlocks([], blocks),
113
+ global: [],
114
+ perBlock: [],
115
+ planPath: "specs/demo.md",
116
+ planText: "## Plan",
117
+ });
118
+ expect(text).not.toContain("### Approved plan");
119
+ expect(text).not.toContain("todo list");
120
+ });
121
+
122
+ it("keeps notes attached to an approval", () => {
123
+ const blocks = parse("## Plan");
124
+ const text = buildFeedbackMarkdown({
125
+ decision: "approve",
126
+ version: 1,
127
+ totalVersions: 1,
128
+ blocks,
129
+ diff: diffBlocks([], blocks),
130
+ global: [{ verdict: "suggest", comment: "Commit per milestone." }],
131
+ perBlock: [],
132
+ });
133
+ expect(text).toContain("## Plan approved — v1 of 1 (1 block changed)");
134
+ expect(text).toContain("- [suggest] Commit per milestone.");
135
+ expect(text).toContain("Approval only means");
136
+ });
137
+
138
+ it("asks what should change when a denial carries no annotations", () => {
139
+ const blocks = parse("## Plan");
140
+ const text = buildFeedbackMarkdown({
141
+ decision: "deny",
142
+ version: 1,
143
+ totalVersions: 2,
144
+ blocks,
145
+ diff: diffBlocks([], blocks),
146
+ global: [],
147
+ perBlock: [],
148
+ });
149
+ expect(text).toContain(
150
+ "No specific annotations were left — ask what should change before resubmitting.",
151
+ );
152
+ });
153
+
154
+ it("drops empty comments and annotations pointing at unknown blocks", () => {
155
+ const blocks = parse("## Plan");
156
+ const text = buildFeedbackMarkdown({
157
+ decision: "deny",
158
+ version: 1,
159
+ totalVersions: 1,
160
+ blocks,
161
+ diff: diffBlocks([], blocks),
162
+ global: [{ verdict: "change", comment: " " }],
163
+ perBlock: [
164
+ { blockId: "deadbeef0000#0", annotations: [{ verdict: "change", comment: "ghost" }] },
165
+ ],
166
+ });
167
+ expect(text).not.toContain("### Global notes");
168
+ expect(text).not.toContain("ghost");
169
+ expect(text).toContain(
170
+ "No specific annotations were left — ask what should change before resubmitting.",
171
+ );
172
+ });
173
+
174
+ it("keeps annotations on removed blocks", () => {
175
+ const before = parse("## Plan\n\nremove this step");
176
+ const after = parse("## Plan");
177
+ const text = buildFeedbackMarkdown({
178
+ decision: "deny",
179
+ version: 2,
180
+ totalVersions: 2,
181
+ blocks: after,
182
+ diff: diffBlocks(before, after),
183
+ previousBlocks: before,
184
+ global: [],
185
+ perBlock: [
186
+ {
187
+ blockId: before[1].id,
188
+ annotations: [{ verdict: "question", comment: "Why was this dropped?" }],
189
+ },
190
+ ],
191
+ });
192
+ expect(text).toContain("#### was in the previous revision · remove this step (removed)");
193
+ expect(text).toContain("> remove this step");
194
+ expect(text).toContain("- [question] Why was this dropped?");
195
+ });
196
+
197
+ it("resolves an annotation keyed by the previous block id instead of dropping it", () => {
198
+ const before = parse("## Plan\n\nwrite the parser first");
199
+ const after = parse("## Plan\n\nwrite the block parser first");
200
+ const text = buildFeedbackMarkdown({
201
+ decision: "deny",
202
+ version: 2,
203
+ totalVersions: 2,
204
+ blocks: after,
205
+ diff: diffBlocks(before, after),
206
+ previousBlocks: before,
207
+ global: [],
208
+ perBlock: [
209
+ {
210
+ // a tab that has not refreshed still refers to the old block
211
+ blockId: before[1].id,
212
+ annotations: [{ verdict: "change", comment: "still relevant" }],
213
+ },
214
+ ],
215
+ });
216
+ expect(text).toContain("still relevant");
217
+ expect(text).toContain("(modified)");
218
+ });
219
+
220
+ it("collapses multi-line comments onto one line", () => {
221
+ const blocks = parse("## Plan");
222
+ const text = buildFeedbackMarkdown({
223
+ decision: "deny",
224
+ version: 1,
225
+ totalVersions: 1,
226
+ blocks,
227
+ diff: diffBlocks([], blocks),
228
+ global: [{ verdict: "change", comment: "first line\nsecond line" }],
229
+ perBlock: [],
230
+ });
231
+ expect(text).toContain("- [change] first line second line");
232
+ });
233
+ });
@@ -0,0 +1,182 @@
1
+ /**
2
+ * plan feedback — annotations → the message the agent receives.
3
+ *
4
+ * The template is deliberately fixed (and tested byte for byte): the agent
5
+ * reads it, not a human, so a stable shape is what lets the planning prompt say
6
+ * "address every block marked (modified)" and have that mean something. It is
7
+ * English like every other runtime string.
8
+ *
9
+ * Two levels only, by design: global notes (about the plan as a whole) and
10
+ * block notes (about one paragraph). There is no inline/range annotation, which
11
+ * is what keeps the block model from needing re-anchoring after an edit.
12
+ */
13
+ import { blockLabel, type Block } from "./blocks.js";
14
+ import type { DiffEntry } from "./diff.js";
15
+
16
+ export type AnnotationVerdict = "change" | "suggest" | "question";
17
+
18
+ export interface Annotation {
19
+ verdict: AnnotationVerdict;
20
+ comment: string;
21
+ }
22
+
23
+ export interface BlockAnnotations {
24
+ blockId: string;
25
+ annotations: Annotation[];
26
+ }
27
+
28
+ export interface FeedbackInput {
29
+ decision: "approve" | "deny";
30
+ version: number;
31
+ totalVersions: number;
32
+ /** Blocks of the annotated version. */
33
+ blocks: readonly Block[];
34
+ /** Diff of that version against its predecessor (empty for v1). */
35
+ diff: readonly DiffEntry[];
36
+ /** Blocks of the predecessor, for the "previous first line" quote. */
37
+ previousBlocks?: readonly Block[];
38
+ global: readonly Annotation[];
39
+ perBlock: readonly BlockAnnotations[];
40
+ /** The spec file the operator approved (path shown to the model). */
41
+ planPath?: string;
42
+ /** Approved revision text, quoted back so the model implements exactly this. */
43
+ planText?: string;
44
+ /** Whether `record_change` is available in this project (framework installed). */
45
+ recordTool?: boolean;
46
+ }
47
+
48
+ const VERDICT_LABEL: Record<AnnotationVerdict, string> = {
49
+ change: "change",
50
+ suggest: "suggest",
51
+ question: "question",
52
+ };
53
+
54
+ const STATUS_LABEL: Record<string, string> = {
55
+ unchanged: "unchanged",
56
+ moved: "moved",
57
+ modified: "modified",
58
+ added: "added",
59
+ removed: "removed",
60
+ };
61
+
62
+ function countChanged(diff: readonly DiffEntry[]): number {
63
+ return diff.filter((entry) => entry.status !== "unchanged").length;
64
+ }
65
+
66
+ function summarizeChanged(count: number): string {
67
+ if (count === 0) return "no block changes";
68
+ return `${count} block${count === 1 ? "" : "s"} changed`;
69
+ }
70
+
71
+ function annotationLines(annotations: readonly Annotation[]): string[] {
72
+ return annotations
73
+ .filter((annotation) => annotation.comment.trim() !== "")
74
+ .map(
75
+ (annotation) =>
76
+ `- [${VERDICT_LABEL[annotation.verdict]}] ${annotation.comment.trim().replace(/\n+/g, " ")}`,
77
+ );
78
+ }
79
+
80
+ /** Longest block excerpt quoted back — enough to locate it, not a second copy of the plan. */
81
+ const QUOTE_LIMIT = 600;
82
+
83
+ function quoteBlock(text: string): string {
84
+ const trimmed = text.trim();
85
+ const clipped =
86
+ trimmed.length > QUOTE_LIMIT ? `${trimmed.slice(0, QUOTE_LIMIT)}…` : trimmed;
87
+ // Every line of a blockquote needs the marker, otherwise the tail of a
88
+ // multi-line paragraph escapes the quote and reads like instructions.
89
+ return clipped
90
+ .split("\n")
91
+ .map((line) => `> ${line}`)
92
+ .join("\n");
93
+ }
94
+
95
+ /**
96
+ * Build the message for one decision. Approval without notes gets a short
97
+ * instruction to start; approval with notes keeps the notes; denial always
98
+ * carries at least one line telling the agent what to do next.
99
+ */
100
+ export function buildFeedbackMarkdown(input: FeedbackInput): string {
101
+ const changed = countChanged(input.diff);
102
+ const header = `## Plan ${input.decision === "approve" ? "approved" : "feedback"} — v${input.version} of ${input.totalVersions} (${summarizeChanged(changed)})`;
103
+ const byId = new Map(input.blocks.map((block) => [block.id, block]));
104
+ const prevById = new Map((input.previousBlocks ?? []).map((block) => [block.id, block]));
105
+ const diffById = new Map(input.diff.map((entry) => [entry.id, entry]));
106
+
107
+ const sections: string[] = [];
108
+ const globalLines = annotationLines(input.global);
109
+ if (globalLines.length > 0) {
110
+ sections.push(["### Global notes (apply to the whole plan)", ...globalLines].join("\n"));
111
+ }
112
+
113
+ // An annotation may arrive keyed by the block's PREVIOUS id (a tab that has
114
+ // not refreshed, a block that was edited between review and submit). Resolve
115
+ // through `prevId` instead of dropping the operator's note on the floor.
116
+ const entryByPrevId = new Map(
117
+ input.diff.flatMap((entry) => (entry.prevId ? [[entry.prevId, entry] as const] : [])),
118
+ );
119
+
120
+ const blockSections: string[] = [];
121
+ for (const group of input.perBlock) {
122
+ const viaPrevious = entryByPrevId.get(group.blockId);
123
+ const blockId = byId.has(group.blockId) ? group.blockId : (viaPrevious?.id ?? group.blockId);
124
+ const block = byId.get(blockId);
125
+ const status = diffById.get(blockId)?.status ?? "unchanged";
126
+ // Removed blocks stay visible in the review and remain annotatable; their
127
+ // text comes from the previous revision. A genuinely unknown id is still
128
+ // ignored (it cannot be rendered or meaningfully addressed).
129
+ const source = block ?? (status === "removed" ? prevById.get(blockId) : undefined);
130
+ if (!source) continue;
131
+ const lines = annotationLines(group.annotations);
132
+ if (lines.length === 0) continue;
133
+ const anchor = status === "removed" ? "was in the previous revision" : `line ${source.line}`;
134
+ const blockHeading = `#### ${anchor} · ${blockLabel(source, 100)} (${STATUS_LABEL[status] ?? status})`;
135
+ // Quote the block itself, not its first line: two similar list items are
136
+ // indistinguishable from a label alone, and the model edits files, so it
137
+ // needs text it can search for. Removed blocks quote the old text.
138
+ blockSections.push([blockHeading, quoteBlock(source.text), ...lines].join("\n"));
139
+ }
140
+ if (blockSections.length > 0) sections.push(["### Block notes (apply to the quoted block only)", ...blockSections].join("\n\n"));
141
+
142
+ const body =
143
+ sections.length > 0
144
+ ? sections.join("\n\n")
145
+ : input.decision === "deny"
146
+ ? "No specific annotations were left — ask what should change before resubmitting."
147
+ : "No notes were attached.";
148
+
149
+ const instruction =
150
+ input.decision === "approve"
151
+ ? [
152
+ "The plan is approved: start implementing it now.",
153
+ "Approval only means \"go\" — every annotation above still applies while you build.",
154
+ "Record the implementation work in the todo list (`todo` tool) as you go, and update each entry",
155
+ "as it starts, finishes or gets blocked, so progress stays traceable.",
156
+ ...(input.recordTool
157
+ ? [
158
+ "When the work is finished and tested, call `record_change` once for this round so the",
159
+ "change enters acceptance (`/ask-uat`) instead of waiting to be remembered later.",
160
+ ]
161
+ : []),
162
+ ]
163
+ .join("\n")
164
+ : "Update the plan file to address every annotation above, then submit it again with plan_submit.";
165
+
166
+ // On approval the model must implement the plan it was shown, not a stale
167
+ // recollection of it: the file may have been edited after the last submit, so
168
+ // quote the approved bytes back and name the path explicitly.
169
+ const approvedPlan =
170
+ input.decision === "approve" && input.planText !== undefined
171
+ ? [
172
+ `### Approved plan (${input.planPath ?? "the plan file"})`,
173
+ "",
174
+ input.planText.trimEnd(),
175
+ ]
176
+ .join("\n")
177
+ : undefined;
178
+
179
+ const parts = [header, "", body, "", instruction];
180
+ if (approvedPlan) parts.push("", approvedPlan);
181
+ return parts.join("\n");
182
+ }