@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
|
@@ -17,6 +17,11 @@
|
|
|
17
17
|
* 3. **Missing optional pieces are created** (the UAT contract), so a project
|
|
18
18
|
* that adopted the framework before the ledger existed can catch up.
|
|
19
19
|
*
|
|
20
|
+
* `/ask-init` runs this on every invocation: on an installed project the first
|
|
21
|
+
* step is now the same comparison, so a repeat run either reports "current" or
|
|
22
|
+
* repairs what is safe before the audit. Marker matching lives in `workflow.ts`
|
|
23
|
+
* and refuses to touch ambiguous layouts.
|
|
24
|
+
*
|
|
20
25
|
* The command is idempotent: a second run reports "current" everywhere.
|
|
21
26
|
*/
|
|
22
27
|
import { createHash } from "node:crypto";
|
|
@@ -24,47 +29,39 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
|
24
29
|
import { dirname, resolve, sep } from "node:path";
|
|
25
30
|
import {
|
|
26
31
|
AGENTS_FILE,
|
|
27
|
-
BUDGETS,
|
|
28
|
-
CONTRACT_FILE,
|
|
29
|
-
FRAMEWORK_VERSION,
|
|
30
|
-
IMPLEMENTATIONS_DIR,
|
|
31
|
-
SIGNPOST_FILE,
|
|
32
32
|
STATE_FILE,
|
|
33
|
-
STATE_GITIGNORE,
|
|
34
|
-
UAT_CONTRACT_FILE,
|
|
35
|
-
WORKFLOW_BLOCK_BEGIN,
|
|
36
|
-
WORKFLOW_BLOCK_END,
|
|
37
33
|
} from "./spec.js";
|
|
38
34
|
import { loadState } from "./detect.js";
|
|
39
35
|
import { iso } from "./plan.js";
|
|
40
36
|
import { stateToToml } from "./apply.js";
|
|
41
|
-
import {
|
|
37
|
+
import { MANAGED_SPECS, renderManaged, workflowBlockText } from "./parity.js";
|
|
42
38
|
import type { FrameworkState } from "./types.js";
|
|
39
|
+
import {
|
|
40
|
+
applyWorkflowBlock,
|
|
41
|
+
type BlockStatus,
|
|
42
|
+
} from "./workflow.js";
|
|
43
|
+
|
|
44
|
+
export { applyWorkflowBlock } from "./workflow.js";
|
|
43
45
|
|
|
44
46
|
export interface RefreshOptions {
|
|
45
47
|
readonly dryRun?: boolean;
|
|
46
48
|
readonly now?: Date;
|
|
47
49
|
readonly toolVersion?: string;
|
|
50
|
+
/** Treat a legacy managed file with no anchor as ours and update it. */
|
|
51
|
+
readonly adoptLegacy?: boolean;
|
|
48
52
|
}
|
|
49
53
|
|
|
50
|
-
export type BlockStatus = "current" | "updated" | "inserted" | "missing-agents";
|
|
51
|
-
|
|
52
54
|
export interface RefreshResult {
|
|
53
55
|
readonly created: string[];
|
|
54
56
|
readonly updated: string[];
|
|
57
|
+
/** Legacy files taken over because `--adopt` was explicit. */
|
|
58
|
+
readonly adopted: string[];
|
|
55
59
|
readonly skipped: Array<{ path: string; reason: string }>;
|
|
56
60
|
readonly block: BlockStatus;
|
|
57
61
|
readonly stateUpdated: boolean;
|
|
58
62
|
readonly dryRun: boolean;
|
|
59
63
|
}
|
|
60
64
|
|
|
61
|
-
const MANAGED_TEMPLATES: ReadonlyArray<{ path: string; template: string; tokens: Record<string, string> }> = [
|
|
62
|
-
{ path: CONTRACT_FILE, template: "memory-readme", tokens: {} },
|
|
63
|
-
{ path: SIGNPOST_FILE, template: "signpost", tokens: {} },
|
|
64
|
-
{ path: UAT_CONTRACT_FILE, template: "uat-readme", tokens: { implementationsDir: IMPLEMENTATIONS_DIR } },
|
|
65
|
-
{ path: STATE_GITIGNORE, template: "state-gitignore", tokens: {} },
|
|
66
|
-
];
|
|
67
|
-
|
|
68
65
|
function sha256(text: string): string {
|
|
69
66
|
return createHash("sha256").update(text).digest("hex");
|
|
70
67
|
}
|
|
@@ -78,43 +75,6 @@ function inside(root: string, rel: string): string {
|
|
|
78
75
|
return absolute;
|
|
79
76
|
}
|
|
80
77
|
|
|
81
|
-
function baseTokens(): Record<string, string> {
|
|
82
|
-
return {
|
|
83
|
-
frameworkVersion: String(FRAMEWORK_VERSION),
|
|
84
|
-
softBudget: String(BUDGETS.agentsMdSoftChars),
|
|
85
|
-
hardBudget: String(BUDGETS.agentsMdHardChars),
|
|
86
|
-
memoryBudget: String(BUDGETS.memoryFileChars),
|
|
87
|
-
implementationsBudget: String(BUDGETS.implementationsFiles),
|
|
88
|
-
implementationsDir: IMPLEMENTATIONS_DIR,
|
|
89
|
-
statePath: STATE_FILE,
|
|
90
|
-
contractPath: CONTRACT_FILE,
|
|
91
|
-
uatLedger: ".agents/uat/ledger.jsonl",
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/** Replace or insert the managed workflow block; never touches anything else. */
|
|
96
|
-
export function applyWorkflowBlock(
|
|
97
|
-
text: string,
|
|
98
|
-
block: string,
|
|
99
|
-
): { text: string; status: Exclude<BlockStatus, "missing-agents"> } {
|
|
100
|
-
const begin = text.indexOf(WORKFLOW_BLOCK_BEGIN);
|
|
101
|
-
const end = text.indexOf(WORKFLOW_BLOCK_END);
|
|
102
|
-
if (begin >= 0 && end > begin) {
|
|
103
|
-
const endLine = text.indexOf("\n", end);
|
|
104
|
-
const stop = endLine === -1 ? text.length : endLine;
|
|
105
|
-
const next = `${text.slice(0, begin)}${block.trimEnd()}${text.slice(stop)}`;
|
|
106
|
-
return { text: next, status: next === text ? "current" : "updated" };
|
|
107
|
-
}
|
|
108
|
-
const appendix = /^##[ \t]+(?:附录|appendix)/im.exec(text);
|
|
109
|
-
if (appendix?.index !== undefined) {
|
|
110
|
-
return {
|
|
111
|
-
text: `${text.slice(0, appendix.index)}${block.trimEnd()}\n\n${text.slice(appendix.index)}`,
|
|
112
|
-
status: "inserted",
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
return { text: `${text.trimEnd()}\n\n${block.trimEnd()}\n`, status: "inserted" };
|
|
116
|
-
}
|
|
117
|
-
|
|
118
78
|
export function refreshFramework(root: string, options: RefreshOptions = {}): RefreshResult {
|
|
119
79
|
const loaded = loadState(root);
|
|
120
80
|
if (!loaded.state) {
|
|
@@ -127,13 +87,14 @@ export function refreshFramework(root: string, options: RefreshOptions = {}): Re
|
|
|
127
87
|
const dryRun = options.dryRun === true;
|
|
128
88
|
const created: string[] = [];
|
|
129
89
|
const updated: string[] = [];
|
|
90
|
+
const adopted: string[] = [];
|
|
130
91
|
const skipped: RefreshResult["skipped"] = [];
|
|
131
92
|
const previous = new Map(state.managed.map((entry) => [entry.path, entry]));
|
|
132
93
|
const nextManaged = new Map(state.managed.map((entry) => [entry.path, { ...entry }]));
|
|
133
94
|
|
|
134
|
-
for (const spec of
|
|
95
|
+
for (const spec of MANAGED_SPECS) {
|
|
135
96
|
const absolute = inside(root, spec.path);
|
|
136
|
-
const rendered =
|
|
97
|
+
const rendered = renderManaged(spec);
|
|
137
98
|
const exists = existsSync(absolute);
|
|
138
99
|
const disk = exists ? readFileSync(absolute, "utf8") : undefined;
|
|
139
100
|
const entry = previous.get(spec.path);
|
|
@@ -145,7 +106,7 @@ export function refreshFramework(root: string, options: RefreshOptions = {}): Re
|
|
|
145
106
|
const safe = exists
|
|
146
107
|
? entry?.templateSha256 !== undefined
|
|
147
108
|
? diskHash === entry.templateSha256
|
|
148
|
-
: diskHash === sha256(rendered)
|
|
109
|
+
: diskHash === sha256(rendered) || options.adoptLegacy === true
|
|
149
110
|
: true;
|
|
150
111
|
|
|
151
112
|
if (!exists) {
|
|
@@ -166,7 +127,10 @@ export function refreshFramework(root: string, options: RefreshOptions = {}): Re
|
|
|
166
127
|
if (!safe) {
|
|
167
128
|
skipped.push({
|
|
168
129
|
path: spec.path,
|
|
169
|
-
reason:
|
|
130
|
+
reason:
|
|
131
|
+
entry?.templateSha256 !== undefined
|
|
132
|
+
? "edited by hand since the framework last wrote it — left untouched"
|
|
133
|
+
: "no template anchor and the bytes differ from the current template — cannot tell a hand edit from an older template; left untouched",
|
|
170
134
|
});
|
|
171
135
|
continue;
|
|
172
136
|
}
|
|
@@ -181,6 +145,9 @@ export function refreshFramework(root: string, options: RefreshOptions = {}): Re
|
|
|
181
145
|
continue;
|
|
182
146
|
}
|
|
183
147
|
|
|
148
|
+
if (entry?.templateSha256 === undefined && options.adoptLegacy === true) {
|
|
149
|
+
adopted.push(spec.path);
|
|
150
|
+
}
|
|
184
151
|
if (!dryRun) writeFileSync(absolute, rendered, "utf8");
|
|
185
152
|
updated.push(spec.path);
|
|
186
153
|
nextManaged.set(spec.path, {
|
|
@@ -191,15 +158,16 @@ export function refreshFramework(root: string, options: RefreshOptions = {}): Re
|
|
|
191
158
|
});
|
|
192
159
|
}
|
|
193
160
|
|
|
194
|
-
// The one block this tool owns inside a file it does not own.
|
|
161
|
+
// The one block this tool owns inside a file it does not own. A malformed
|
|
162
|
+
// marker layout is reported, never rewritten.
|
|
195
163
|
let block: BlockStatus = "missing-agents";
|
|
196
164
|
const agentsPath = inside(root, AGENTS_FILE);
|
|
197
165
|
if (existsSync(agentsPath)) {
|
|
198
166
|
const text = readFileSync(agentsPath, "utf8");
|
|
199
|
-
const rendered =
|
|
167
|
+
const rendered = workflowBlockText();
|
|
200
168
|
const result = applyWorkflowBlock(text, rendered);
|
|
201
169
|
block = result.status;
|
|
202
|
-
if (result.status
|
|
170
|
+
if ((result.status === "inserted" || result.status === "updated") && !dryRun) {
|
|
203
171
|
writeFileSync(agentsPath, result.text, "utf8");
|
|
204
172
|
}
|
|
205
173
|
}
|
|
@@ -234,6 +202,7 @@ export function refreshFramework(root: string, options: RefreshOptions = {}): Re
|
|
|
234
202
|
return {
|
|
235
203
|
created,
|
|
236
204
|
updated,
|
|
205
|
+
adopted,
|
|
237
206
|
skipped,
|
|
238
207
|
block,
|
|
239
208
|
stateUpdated: !dryRun && stateChanged,
|
|
@@ -247,9 +216,19 @@ export function describeRefresh(result: RefreshResult): string {
|
|
|
247
216
|
const verb = result.dryRun ? "would refresh" : "refreshed";
|
|
248
217
|
if (result.created.length > 0) parts.push(`${verb} ${result.created.length} created file(s)`);
|
|
249
218
|
if (result.updated.length > 0) parts.push(`${result.updated.length} framework file(s)`);
|
|
219
|
+
if (result.adopted.length > 0) parts.push(`${result.adopted.length} legacy file(s) adopted`);
|
|
250
220
|
if (result.block === "inserted") parts.push("workflow block inserted into AGENTS.md");
|
|
251
221
|
if (result.block === "updated") parts.push("workflow block updated");
|
|
222
|
+
if (result.block === "malformed") {
|
|
223
|
+
parts.push("workflow markers in AGENTS.md are inconsistent — left untouched");
|
|
224
|
+
}
|
|
225
|
+
if (result.block === "hand-written") {
|
|
226
|
+
parts.push("a hand-written Ask workflow section exists without markers — left untouched");
|
|
227
|
+
}
|
|
228
|
+
if (result.block === "missing-agents") {
|
|
229
|
+
parts.push("AGENTS.md is missing — run /ask-init to rebuild it");
|
|
230
|
+
}
|
|
252
231
|
if (parts.length === 0) parts.push("framework files are already current");
|
|
253
|
-
if (result.skipped.length > 0) parts.push(`${result.skipped.length} left untouched (hand edits)`);
|
|
232
|
+
if (result.skipped.length > 0) parts.push(`${result.skipped.length} left untouched (hand edits or no anchor)`);
|
|
254
233
|
return parts.join(" · ");
|
|
255
234
|
}
|
|
@@ -287,6 +287,9 @@ export const SCAN = {
|
|
|
287
287
|
|
|
288
288
|
export const AUDIT_CODES = {
|
|
289
289
|
pathBareRef: "E-PATH-01",
|
|
290
|
+
// Bare legacy references while no legacy directory exists: nothing a move
|
|
291
|
+
// would break, so this is stale documentation, not a migration blocker.
|
|
292
|
+
pathBareRefStale: "W-PATH-01",
|
|
290
293
|
stateUntracked: "E-STATE-01",
|
|
291
294
|
stateUncommitted: "W-STATE-01",
|
|
292
295
|
stateVersion: "E-STATE-02",
|
|
@@ -318,6 +321,14 @@ export const AUDIT_CODES = {
|
|
|
318
321
|
recordHeader: "W-REC-01",
|
|
319
322
|
// Framework-owned file edited by hand: a refresh will not overwrite it.
|
|
320
323
|
managedEdited: "W-FW-01",
|
|
324
|
+
// Managed file is missing or behind the installed template: refresh fixes it.
|
|
325
|
+
managedStale: "W-FW-02",
|
|
326
|
+
// The AGENTS.md workflow block is absent, behind, hand-written or malformed.
|
|
327
|
+
workflowBlock: "W-FW-03",
|
|
328
|
+
// A required framework file is missing: /ask-init can rebuild it mechanically.
|
|
329
|
+
frameworkFileMissing: "W-FW-04",
|
|
330
|
+
// Legacy state has no template anchor: cannot tell a hand edit from old bytes.
|
|
331
|
+
legacyAnchor: "I-FW-01",
|
|
321
332
|
uatNotInstalled: "I-UAT-01",
|
|
322
333
|
} as const;
|
|
323
334
|
|
|
@@ -80,8 +80,9 @@ compaction plan is.
|
|
|
80
80
|
|
|
81
81
|
`/ask-init check` (or the `framework_audit` tool during an init run) reports:
|
|
82
82
|
|
|
83
|
-
- structure: missing files, index ↔ file mismatches, `AGENTS.override.md` shadowing
|
|
84
|
-
-
|
|
83
|
+
- structure: missing required files, index ↔ file mismatches, `AGENTS.override.md` shadowing
|
|
84
|
+
- framework files: managed text that is missing or behind the installed template, a missing or malformed `AGENTS.md` workflow block, and hand edits (`/ask-init` refreshes only what is safe)
|
|
85
|
+
- paths: references to `memory` or `implementations` without the `.agents/` prefix; an error while a legacy directory still exists (`E-PATH-01`), a warning once it is gone (`W-PATH-01`)
|
|
85
86
|
- duplication: the same section heading in two files (one fact, two homes)
|
|
86
87
|
- order: sections appended after the historical appendix
|
|
87
88
|
- budgets: `AGENTS.md` and memory-file sizes
|
|
@@ -97,3 +98,9 @@ version is newer than the state file, `/ask-init` plans an upgrade: deterministi
|
|
|
97
98
|
steps run in code, prose changes are handed to the model as tasks. A project whose
|
|
98
99
|
state version is newer than the installed tool is never migrated — upgrade the
|
|
99
100
|
tool instead.
|
|
101
|
+
|
|
102
|
+
Text-only changes (the workflow block, the signpost, this contract) do not bump
|
|
103
|
+
the framework version. Run `/ask-init` (or `/ask-init refresh`) after upgrading
|
|
104
|
+
the package to bring them up to date, then `/reload` if `AGENTS.md` changed.
|
|
105
|
+
Re-running `/ask-init` is safe: it reports `current` when nothing drifted and
|
|
106
|
+
leaves files you edited by hand alone.
|
|
@@ -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 };
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* control are fakes.
|
|
7
7
|
*/
|
|
8
8
|
import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { execFileSync } from "node:child_process";
|
|
9
10
|
import { request } from "node:http";
|
|
10
11
|
import { tmpdir } from "node:os";
|
|
11
12
|
import { dirname, join } from "node:path";
|
|
@@ -155,6 +156,56 @@ describe("ask-plan command", () => {
|
|
|
155
156
|
expect(h.control.restore).toHaveBeenCalled();
|
|
156
157
|
expect(h.activeTools()).not.toContain(PLAN_SUBMIT_TOOL);
|
|
157
158
|
});
|
|
159
|
+
|
|
160
|
+
it("refuses to start while ask-uat is open", async () => {
|
|
161
|
+
const h = harness();
|
|
162
|
+
h.pi.setActiveTools([...h.activeTools(), "uat_item"]);
|
|
163
|
+
await h.commands.get(PLAN_COMMAND)!.handler("topic", h.ctx);
|
|
164
|
+
expect(h.sendUserMessage).not.toHaveBeenCalled();
|
|
165
|
+
expect(h.notify).toHaveBeenCalledWith(expect.stringContaining("ask-uat"), "warning");
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("refuses to start while the agent is still running", async () => {
|
|
169
|
+
const h = harness();
|
|
170
|
+
const busy = { ...h.ctx, isIdle: () => false } as ExtensionContext;
|
|
171
|
+
await h.commands.get(PLAN_COMMAND)!.handler("topic", busy);
|
|
172
|
+
expect(h.sendUserMessage).not.toHaveBeenCalled();
|
|
173
|
+
expect(h.notify).toHaveBeenCalledWith(expect.stringContaining("still running"), "warning");
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("reads framework readiness from the main checkout in a linked worktree", async () => {
|
|
177
|
+
const main = mkdtempSync(join(tmpdir(), "plan-main-"));
|
|
178
|
+
const parent = mkdtempSync(join(tmpdir(), "plan-worktrees-"));
|
|
179
|
+
const linked = join(parent, "linked");
|
|
180
|
+
const git = (cwd: string, args: string[]) =>
|
|
181
|
+
execFileSync("git", ["-C", cwd, ...args], { stdio: "ignore" });
|
|
182
|
+
try {
|
|
183
|
+
git(main, ["init", "-q"]);
|
|
184
|
+
writeFileSync(join(main, "tracked.txt"), "one\n", "utf8");
|
|
185
|
+
git(main, ["add", "tracked.txt"]);
|
|
186
|
+
git(main, ["-c", "user.email=test@example.com", "-c", "user.name=test", "commit", "-qm", "init"]);
|
|
187
|
+
git(main, ["worktree", "add", "-qb", "plan-review", linked]);
|
|
188
|
+
mkdirSync(join(main, ".agents/framework"), { recursive: true });
|
|
189
|
+
writeFileSync(join(main, ".agents/framework/state.toml"), 'version = 1\ntool = "test"\n', "utf8");
|
|
190
|
+
writeFileSync(join(main, "AGENTS.md"), "# demo\n", "utf8");
|
|
191
|
+
mkdirSync(join(main, ".agents/memory"), { recursive: true });
|
|
192
|
+
writeFileSync(join(main, ".agents/memory/README.md"), "# contract\n", "utf8");
|
|
193
|
+
mkdirSync(join(main, ".agents/implementations"), { recursive: true });
|
|
194
|
+
|
|
195
|
+
const h = harness();
|
|
196
|
+
const linkedCtx = { ...h.ctx, cwd: linked } as ExtensionContext;
|
|
197
|
+
const handler = h.commands.get(PLAN_COMMAND)!.handler;
|
|
198
|
+
await handler("topic", linkedCtx);
|
|
199
|
+
expect(h.notify).not.toHaveBeenCalledWith(
|
|
200
|
+
expect.stringContaining("no complete agent framework"),
|
|
201
|
+
"info",
|
|
202
|
+
);
|
|
203
|
+
await handler("off", linkedCtx);
|
|
204
|
+
} finally {
|
|
205
|
+
rmSync(main, { recursive: true, force: true });
|
|
206
|
+
rmSync(parent, { recursive: true, force: true });
|
|
207
|
+
}
|
|
208
|
+
});
|
|
158
209
|
});
|
|
159
210
|
|
|
160
211
|
describe("phase integration", () => {
|
|
@@ -18,14 +18,17 @@
|
|
|
18
18
|
* plannotator's habit of switching models silently is the reason this one
|
|
19
19
|
* is written from scratch.
|
|
20
20
|
*/
|
|
21
|
-
import { readFileSync
|
|
22
|
-
import { isAbsolute,
|
|
21
|
+
import { readFileSync } from "node:fs";
|
|
22
|
+
import { isAbsolute, relative, resolve } from "node:path";
|
|
23
23
|
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
24
24
|
import { Type } from "typebox";
|
|
25
25
|
import type { ExtensionAPI, ExtensionContext, ToolCallEvent } from "@earendil-works/pi-coding-agent";
|
|
26
26
|
import type { GateHub } from "../../lib/gates.js";
|
|
27
27
|
import { getPermissionControl, type PermissionSnapshot } from "../../lib/permission-control.js";
|
|
28
28
|
import { getAskControl } from "../../lib/ask-control.js";
|
|
29
|
+
import { PLAN_PHASE_TOOL, UAT_PHASE_TOOL } from "../../lib/phases.js";
|
|
30
|
+
import { frameworkReadiness } from "../init/parity.js";
|
|
31
|
+
import { resolveProjectRoot } from "../uat/state.js";
|
|
29
32
|
import { canonicalize, isWithin } from "../permission/paths.js";
|
|
30
33
|
import { parseCommandUnits } from "../permission/units.js";
|
|
31
34
|
import { buildFeedbackMarkdown } from "./feedback.js";
|
|
@@ -43,7 +46,7 @@ import {
|
|
|
43
46
|
} from "./store.js";
|
|
44
47
|
|
|
45
48
|
export const PLAN_COMMAND = "ask-plan";
|
|
46
|
-
export const PLAN_SUBMIT_TOOL =
|
|
49
|
+
export const PLAN_SUBMIT_TOOL = PLAN_PHASE_TOOL;
|
|
47
50
|
|
|
48
51
|
/** Files the planning phase may write: project markdown, nothing else. */
|
|
49
52
|
function isMarkdownPath(path: string): boolean {
|
|
@@ -173,7 +176,7 @@ export function registerPlan(pi: ExtensionAPI, gates: GateHub, options: PlanOpti
|
|
|
173
176
|
// approval only when the project has the framework and the tool exists.
|
|
174
177
|
recordTool:
|
|
175
178
|
pi.getAllTools().some((tool) => tool.name === "record_change") &&
|
|
176
|
-
|
|
179
|
+
frameworkReadiness(resolveProjectRoot(cwd)).ready,
|
|
177
180
|
});
|
|
178
181
|
}
|
|
179
182
|
|
|
@@ -240,6 +243,23 @@ export function registerPlan(pi: ExtensionAPI, gates: GateHub, options: PlanOpti
|
|
|
240
243
|
return;
|
|
241
244
|
}
|
|
242
245
|
|
|
246
|
+
if (!ctx.isIdle()) {
|
|
247
|
+
ctx.ui.notify("ask-plan: the agent is still running — finish or abort first.", "warning");
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (pi.getActiveTools().includes(UAT_PHASE_TOOL)) {
|
|
251
|
+
ctx.ui.notify("ask-plan: ask-uat is open — /ask-uat off first.", "warning");
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const readiness = frameworkReadiness(resolveProjectRoot(cwd));
|
|
256
|
+
if (!readiness.ready) {
|
|
257
|
+
ctx.ui.notify(
|
|
258
|
+
`ask-plan: no complete agent framework (missing: ${readiness.missing.join(", ")}). The spec still works, but record_change and /ask-uat stay unavailable until /ask-init.`,
|
|
259
|
+
"info",
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
|
|
243
263
|
const slug = planSlug(arg || "plan");
|
|
244
264
|
specPath = `specs/${slug}.md`;
|
|
245
265
|
session = undefined;
|
|
@@ -73,13 +73,26 @@ export const TEARDOWN_MARKERS = ["\x1b[?2004l", "\x1b[?1049l"] as const;
|
|
|
73
73
|
export const SHUTDOWN_EVENT_MARKER = "session_shutdown";
|
|
74
74
|
|
|
75
75
|
/**
|
|
76
|
-
* Terminal restore written before
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
76
|
+
* Terminal restore written before every forced exit (writeSync(1, …), which
|
|
77
|
+
* bypasses our own stdout wrapper and cannot re-arm the guard).
|
|
78
|
+
*
|
|
79
|
+
* The shutdown-event path needs it because nothing restored the terminal:
|
|
80
|
+
* without it the prompt comes back on the alternate screen, with bracketed
|
|
81
|
+
* paste on and the cursor hidden. The marker path gets it too (since
|
|
82
|
+
* 2026-11-13): `ui.stop()` can also hang between its two sequences —
|
|
83
|
+
* `?2004l` written, `?1049l` never — and an already-restored terminal
|
|
84
|
+
* simply ignores the extra sequences. Idempotent, unlike a frozen screen.
|
|
81
85
|
*/
|
|
82
86
|
export const TERMINAL_RESTORE = "\x1b[?2004l\x1b[?1049l\x1b[?25h\x1b[0m\r\n";
|
|
83
87
|
|
|
88
|
+
/**
|
|
89
|
+
* Grace period for the kill fallbacks that run AFTER the report is on disk
|
|
90
|
+
* and "forcing exit" has been printed: `process.exit(0)` first, then, in
|
|
91
|
+
* layers, a same-loop `setTimeout` SIGKILL and the independent-thread
|
|
92
|
+
* SIGKILL from killer.ts (see there for why the timer alone is not enough:
|
|
93
|
+
* the main thread can deadlock inside `process.exit`, where no timer runs).
|
|
94
|
+
*/
|
|
95
|
+
export const HARD_KILL_DELAY_MS = 500;
|
|
96
|
+
|
|
84
97
|
/** Sequences that mean a TUI is starting (mode switch or suspend/resume). */
|
|
85
98
|
export const RESTART_MARKERS = ["\x1b[?2004h", "\x1b[?1049h"] as const;
|