@rasensio/aidlc 1.46.0 → 1.48.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/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +2 -0
- package/dist/cli.js.map +1 -1
- package/dist/commands/spec.d.ts +22 -0
- package/dist/commands/spec.d.ts.map +1 -0
- package/dist/commands/spec.js +200 -0
- package/dist/commands/spec.js.map +1 -0
- package/dist/commands/transition.d.ts.map +1 -1
- package/dist/commands/transition.js +66 -1
- package/dist/commands/transition.js.map +1 -1
- package/dist/core/next-steps.d.ts.map +1 -1
- package/dist/core/next-steps.js +7 -0
- package/dist/core/next-steps.js.map +1 -1
- package/dist/core/types.d.ts +3 -1
- package/dist/core/types.d.ts.map +1 -1
- package/dist/cost/completion.d.ts +40 -0
- package/dist/cost/completion.d.ts.map +1 -0
- package/dist/cost/completion.js +66 -0
- package/dist/cost/completion.js.map +1 -0
- package/dist/cost/recorder.d.ts.map +1 -1
- package/dist/cost/recorder.js +6 -1
- package/dist/cost/recorder.js.map +1 -1
- package/dist/cost/sync.d.ts.map +1 -1
- package/dist/cost/sync.js +6 -1
- package/dist/cost/sync.js.map +1 -1
- package/dist/menu/roster.d.ts.map +1 -1
- package/dist/menu/roster.js +2 -0
- package/dist/menu/roster.js.map +1 -1
- package/dist/spec/corpus.d.ts +43 -0
- package/dist/spec/corpus.d.ts.map +1 -0
- package/dist/spec/corpus.js +155 -0
- package/dist/spec/corpus.js.map +1 -0
- package/dist/spec/delta.d.ts +17 -0
- package/dist/spec/delta.d.ts.map +1 -0
- package/dist/spec/delta.js +181 -0
- package/dist/spec/delta.js.map +1 -0
- package/dist/spec/fold.d.ts +30 -0
- package/dist/spec/fold.d.ts.map +1 -0
- package/dist/spec/fold.js +228 -0
- package/dist/spec/fold.js.map +1 -0
- package/dist/spec/types.d.ts +93 -0
- package/dist/spec/types.d.ts.map +1 -0
- package/dist/spec/types.js +9 -0
- package/dist/spec/types.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The spec corpus: `.aidlc/specs/<capability>.md`, one file per capability,
|
|
3
|
+
* saying what the system does today (living-spec-corpus/AC-1).
|
|
4
|
+
*
|
|
5
|
+
* Read-only. Only the `### Requirement: <name>` blocks are parsed; a file's
|
|
6
|
+
* title, introduction and any other heading are never interpreted, which is
|
|
7
|
+
* what lets a fold leave them byte for byte (living-spec-corpus/AC-17).
|
|
8
|
+
*
|
|
9
|
+
* Requirements: living-spec-corpus/AC-1, living-spec-corpus/AC-2,
|
|
10
|
+
* living-spec-corpus/AC-9, living-spec-corpus/AC-17
|
|
11
|
+
*/
|
|
12
|
+
import { createHash } from 'node:crypto';
|
|
13
|
+
import { existsSync, readFileSync, readdirSync } from 'node:fs';
|
|
14
|
+
import { join } from 'node:path';
|
|
15
|
+
import { scanOutsideFences } from '../change/parser.js';
|
|
16
|
+
export const SPECS_RELATIVE = '.aidlc/specs';
|
|
17
|
+
export const ARCHIVE_RELATIVE = `${SPECS_RELATIVE}/archive`;
|
|
18
|
+
// The slug reaches a file path, so it is an allowlist, not a sanitiser
|
|
19
|
+
// (secure-defaults: path traversal).
|
|
20
|
+
const CAPABILITY_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
21
|
+
const MAX_CAPABILITY = 64;
|
|
22
|
+
export const REQUIREMENT_HEADING_RE = /^ {0,3}###[ \t]+Requirement:[ \t]*(.*?)[ \t]*$/;
|
|
23
|
+
/** Any heading that bounds a requirement block: levels 1–3. */
|
|
24
|
+
const BOUNDING_HEADING_RE = /^ {0,3}#{1,3}(?:[ \t]|$)/;
|
|
25
|
+
export const PROVENANCE_RE = /^_Last changed by (\S+), (\d{4}-\d{2}-\d{2})\._\s*$/;
|
|
26
|
+
export function isValidCapability(slug) {
|
|
27
|
+
return typeof slug === 'string' && slug.length <= MAX_CAPABILITY && CAPABILITY_RE.test(slug);
|
|
28
|
+
}
|
|
29
|
+
/** Identity of a requirement name: case and spacing are not part of it. */
|
|
30
|
+
export function requirementKey(name) {
|
|
31
|
+
return name.trim().replace(/\s+/g, ' ').toLowerCase();
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Normalise a requirement body: trailing whitespace off each line, leading and
|
|
35
|
+
* trailing blank lines dropped, blank-line runs collapsed to one. Re-wrapping
|
|
36
|
+
* whitespace by hand does not change a revision; changing a word does.
|
|
37
|
+
*/
|
|
38
|
+
export function normaliseBody(body) {
|
|
39
|
+
const lines = body.split(/\r?\n/).map((l) => l.replace(/\s+$/, ''));
|
|
40
|
+
const out = [];
|
|
41
|
+
for (const line of lines) {
|
|
42
|
+
if (line === '' && (out.length === 0 || out[out.length - 1] === ''))
|
|
43
|
+
continue;
|
|
44
|
+
out.push(line);
|
|
45
|
+
}
|
|
46
|
+
while (out.length > 0 && out[out.length - 1] === '')
|
|
47
|
+
out.pop();
|
|
48
|
+
return out.join('\n');
|
|
49
|
+
}
|
|
50
|
+
export function revisionOf(body) {
|
|
51
|
+
return createHash('sha256').update(normaliseBody(body)).digest('hex').slice(0, 8);
|
|
52
|
+
}
|
|
53
|
+
export function provenanceLine(instance, date) {
|
|
54
|
+
return `_Last changed by ${instance}, ${date}._`;
|
|
55
|
+
}
|
|
56
|
+
export function capabilityRelative(slug) {
|
|
57
|
+
return `${SPECS_RELATIVE}/${slug}.md`;
|
|
58
|
+
}
|
|
59
|
+
/** Parse one capability file's requirement blocks. */
|
|
60
|
+
export function parseCapability(text, capability, path) {
|
|
61
|
+
const lines = text.split('\n');
|
|
62
|
+
const bounding = [];
|
|
63
|
+
const outside = new Set();
|
|
64
|
+
scanOutsideFences(lines, (line, i) => {
|
|
65
|
+
outside.add(i);
|
|
66
|
+
if (BOUNDING_HEADING_RE.test(line))
|
|
67
|
+
bounding.push(i);
|
|
68
|
+
});
|
|
69
|
+
const requirements = [];
|
|
70
|
+
const errors = [];
|
|
71
|
+
const seen = new Map();
|
|
72
|
+
bounding.forEach((start, n) => {
|
|
73
|
+
const heading = REQUIREMENT_HEADING_RE.exec(lines[start]);
|
|
74
|
+
if (!heading)
|
|
75
|
+
return;
|
|
76
|
+
const name = heading[1];
|
|
77
|
+
if (name === '') {
|
|
78
|
+
errors.push({ path, line: start + 1, message: 'requirement heading has no name' });
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
let end = n + 1 < bounding.length ? bounding[n + 1] : lines.length;
|
|
82
|
+
while (end > start + 1 && lines[end - 1].trim() === '')
|
|
83
|
+
end--;
|
|
84
|
+
let provenanceAt = -1;
|
|
85
|
+
let changedBy = null;
|
|
86
|
+
let changedOn = null;
|
|
87
|
+
for (let i = end - 1; i > start; i--) {
|
|
88
|
+
if (!outside.has(i))
|
|
89
|
+
continue;
|
|
90
|
+
const match = PROVENANCE_RE.exec(lines[i].trim());
|
|
91
|
+
if (match) {
|
|
92
|
+
provenanceAt = i;
|
|
93
|
+
changedBy = match[1];
|
|
94
|
+
changedOn = match[2];
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const body = lines
|
|
99
|
+
.slice(start + 1, end)
|
|
100
|
+
.filter((_, offset) => start + 1 + offset !== provenanceAt)
|
|
101
|
+
.join('\n');
|
|
102
|
+
const key = requirementKey(name);
|
|
103
|
+
const first = seen.get(key);
|
|
104
|
+
if (first !== undefined) {
|
|
105
|
+
errors.push({
|
|
106
|
+
path,
|
|
107
|
+
line: start + 1,
|
|
108
|
+
message: `requirement "${name}" is defined twice (first at line ${first})`,
|
|
109
|
+
});
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
seen.set(key, start + 1);
|
|
113
|
+
requirements.push({
|
|
114
|
+
name,
|
|
115
|
+
key,
|
|
116
|
+
revision: revisionOf(body),
|
|
117
|
+
changedBy,
|
|
118
|
+
changedOn,
|
|
119
|
+
startLine: start,
|
|
120
|
+
endLine: end,
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
return { capability, path, requirements, errors };
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Read one capability file. `null` when the file does not exist. Throws on an
|
|
127
|
+
* invalid slug — callers validate first, and no path is built from one that
|
|
128
|
+
* fails (living-spec-corpus/AC-4).
|
|
129
|
+
*/
|
|
130
|
+
export function readCapability(projectRoot, slug) {
|
|
131
|
+
if (!isValidCapability(slug))
|
|
132
|
+
throw new Error(`invalid capability name '${slug}'`);
|
|
133
|
+
const file = join(projectRoot, SPECS_RELATIVE, `${slug}.md`);
|
|
134
|
+
if (!existsSync(file))
|
|
135
|
+
return null;
|
|
136
|
+
return parseCapability(readFileSync(file, 'utf8'), slug, capabilityRelative(slug));
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Every capability file, sorted by slug. `null` when the corpus directory does
|
|
140
|
+
* not exist. A `.md` file whose name is not a valid slug is not a capability
|
|
141
|
+
* and is skipped.
|
|
142
|
+
*/
|
|
143
|
+
export function listCapabilities(projectRoot) {
|
|
144
|
+
const dir = join(projectRoot, SPECS_RELATIVE);
|
|
145
|
+
if (!existsSync(dir))
|
|
146
|
+
return null;
|
|
147
|
+
return readdirSync(dir, { withFileTypes: true })
|
|
148
|
+
.filter((e) => e.isFile() && e.name.endsWith('.md'))
|
|
149
|
+
.map((e) => e.name.slice(0, -3))
|
|
150
|
+
.filter(isValidCapability)
|
|
151
|
+
.sort()
|
|
152
|
+
.map((slug) => readCapability(projectRoot, slug))
|
|
153
|
+
.filter((c) => c !== null);
|
|
154
|
+
}
|
|
155
|
+
//# sourceMappingURL=corpus.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"corpus.js","sourceRoot":"","sources":["../../src/spec/corpus.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAChE,OAAO,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section the grammar is closed and every departure is an error with
|
|
6
|
+
* its line, never a skip: a block the parser cannot read would otherwise fold
|
|
7
|
+
* as if it had not been written, and a parser narrower than the prose people
|
|
8
|
+
* write makes text invisible rather than malformed (lessons digest,
|
|
9
|
+
* change-management). Outside the section, a heading that looks like a delta
|
|
10
|
+
* but is not the exact one is a warning, for the same reason.
|
|
11
|
+
*
|
|
12
|
+
* Requirements: living-spec-corpus/AC-5, living-spec-corpus/AC-6,
|
|
13
|
+
* living-spec-corpus/AC-7, living-spec-corpus/AC-8, living-spec-corpus/AC-9
|
|
14
|
+
*/
|
|
15
|
+
import type { ParsedDelta } from './types.js';
|
|
16
|
+
export declare function parseDelta(text: string): ParsedDelta;
|
|
17
|
+
//# sourceMappingURL=delta.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"delta.d.ts","sourceRoot":"","sources":["../../src/spec/delta.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAIH,OAAO,KAAK,EAAuB,WAAW,EAAa,MAAM,YAAY,CAAC;AAoB9E,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,CAuJpD"}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Delta parser: the `## Spec delta` section of a requirements.md
|
|
3
|
+
* (living-spec-corpus/AC-5).
|
|
4
|
+
*
|
|
5
|
+
* Inside the section the grammar is closed and every departure is an error with
|
|
6
|
+
* its line, never a skip: a block the parser cannot read would otherwise fold
|
|
7
|
+
* as if it had not been written, and a parser narrower than the prose people
|
|
8
|
+
* write makes text invisible rather than malformed (lessons digest,
|
|
9
|
+
* change-management). Outside the section, a heading that looks like a delta
|
|
10
|
+
* but is not the exact one is a warning, for the same reason.
|
|
11
|
+
*
|
|
12
|
+
* Requirements: living-spec-corpus/AC-5, living-spec-corpus/AC-6,
|
|
13
|
+
* living-spec-corpus/AC-7, living-spec-corpus/AC-8, living-spec-corpus/AC-9
|
|
14
|
+
*/
|
|
15
|
+
import { scanOutsideFences } from '../change/parser.js';
|
|
16
|
+
import { isValidCapability, requirementKey } from './corpus.js';
|
|
17
|
+
const SECTION_RE = /^ {0,3}##[ \t]+spec delta[ \t]*$/i;
|
|
18
|
+
/** Ends the section: any level 1–2 heading. */
|
|
19
|
+
const SECTION_END_RE = /^ {0,3}#{1,2}(?:[ \t]|$)/;
|
|
20
|
+
const LEVEL3_RE = /^ {0,3}###(?:[ \t]|$)/;
|
|
21
|
+
const BLOCK_RE = /^ {0,3}###[ \t]+(ADDED|MODIFIED|REMOVED)[ \t]+([^:\s]+):[ \t]*(.*?)[ \t]*$/;
|
|
22
|
+
const NEAR_MISS_RE = /^ {0,3}#{1,6}[ \t]+spec[ \t]+(?:deltas?|changes?)\b/i;
|
|
23
|
+
// The same three emphasis variants the amendment parser accepts for its fields
|
|
24
|
+
// (living-spec-corpus/AC-6): authors write all three.
|
|
25
|
+
const BASE_RE = /^\s*(?:Base:|\*\*Base\*\*:|\*\*Base:\*\*)\s*(.*?)\s*$/;
|
|
26
|
+
const REVISION_RE = /^[0-9a-f]{8}$/;
|
|
27
|
+
const MAX_NAME = 120;
|
|
28
|
+
export function parseDelta(text) {
|
|
29
|
+
const lines = text.split('\n');
|
|
30
|
+
const outside = new Set();
|
|
31
|
+
scanOutsideFences(lines, (_line, i) => outside.add(i));
|
|
32
|
+
const errors = [];
|
|
33
|
+
const warnings = [];
|
|
34
|
+
let start = -1;
|
|
35
|
+
let end = lines.length;
|
|
36
|
+
for (let i = 0; i < lines.length; i++) {
|
|
37
|
+
if (!outside.has(i))
|
|
38
|
+
continue;
|
|
39
|
+
const line = lines[i];
|
|
40
|
+
if (SECTION_RE.test(line)) {
|
|
41
|
+
if (start === -1) {
|
|
42
|
+
start = i;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
// Reading only the first would fold half the author's changes.
|
|
46
|
+
errors.push({ line: i + 1, message: 'a second `## Spec delta` section; merge them into one' });
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (start !== -1 && end === lines.length && i > start && SECTION_END_RE.test(line)) {
|
|
50
|
+
end = i;
|
|
51
|
+
}
|
|
52
|
+
if (NEAR_MISS_RE.test(line)) {
|
|
53
|
+
warnings.push({
|
|
54
|
+
line: i + 1,
|
|
55
|
+
message: `"${line.trim()}" looks like a spec delta but only \`## Spec delta\` is read`,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (start === -1) {
|
|
60
|
+
return { present: false, section: '', blocks: [], errors, warnings };
|
|
61
|
+
}
|
|
62
|
+
const raw = [];
|
|
63
|
+
for (let i = start + 1; i < end; i++) {
|
|
64
|
+
const isOutside = outside.has(i);
|
|
65
|
+
if (isOutside && LEVEL3_RE.test(lines[i])) {
|
|
66
|
+
raw.push({ heading: i, bodyLines: [] });
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
raw[raw.length - 1]?.bodyLines.push({ text: lines[i], index: i, outside: isOutside });
|
|
70
|
+
}
|
|
71
|
+
const blocks = [];
|
|
72
|
+
const seen = new Map();
|
|
73
|
+
for (const block of raw) {
|
|
74
|
+
const lineNo = block.heading + 1;
|
|
75
|
+
const match = BLOCK_RE.exec(lines[block.heading]);
|
|
76
|
+
if (!match) {
|
|
77
|
+
errors.push({
|
|
78
|
+
line: lineNo,
|
|
79
|
+
message: `"${lines[block.heading].trim()}" is not a delta block; write ` +
|
|
80
|
+
'`### ADDED|MODIFIED|REMOVED <capability>: <requirement name>`',
|
|
81
|
+
});
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const op = match[1];
|
|
85
|
+
const capability = match[2];
|
|
86
|
+
const name = match[3];
|
|
87
|
+
let ok = true;
|
|
88
|
+
if (!isValidCapability(capability)) {
|
|
89
|
+
errors.push({
|
|
90
|
+
line: lineNo,
|
|
91
|
+
message: `capability "${capability}" is not a lowercase kebab-case name of at most 64 characters`,
|
|
92
|
+
});
|
|
93
|
+
ok = false;
|
|
94
|
+
}
|
|
95
|
+
if (name === '' || name.length > MAX_NAME) {
|
|
96
|
+
errors.push({
|
|
97
|
+
line: lineNo,
|
|
98
|
+
message: name === '' ? 'the block names no requirement' : `requirement name is over ${MAX_NAME} characters`,
|
|
99
|
+
});
|
|
100
|
+
ok = false;
|
|
101
|
+
}
|
|
102
|
+
let base = null;
|
|
103
|
+
let baseCount = 0;
|
|
104
|
+
const body = [];
|
|
105
|
+
for (const b of block.bodyLines) {
|
|
106
|
+
const baseMatch = b.outside ? BASE_RE.exec(b.text) : null;
|
|
107
|
+
if (!baseMatch) {
|
|
108
|
+
body.push(b.text);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
baseCount++;
|
|
112
|
+
if (!REVISION_RE.test(baseMatch[1])) {
|
|
113
|
+
errors.push({
|
|
114
|
+
line: b.index + 1,
|
|
115
|
+
message: `Base "${baseMatch[1]}" is not a revision; copy the 8 characters \`aidlc spec show ${capability}\` prints`,
|
|
116
|
+
});
|
|
117
|
+
ok = false;
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
base = baseMatch[1];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const bodyText = trimBlank(body).join('\n');
|
|
124
|
+
if (baseCount > 1) {
|
|
125
|
+
errors.push({ line: lineNo, message: 'the block has more than one Base line' });
|
|
126
|
+
ok = false;
|
|
127
|
+
}
|
|
128
|
+
if (op === 'ADDED' && baseCount > 0) {
|
|
129
|
+
errors.push({ line: lineNo, message: 'an ADDED block takes no Base: there is nothing to be based on' });
|
|
130
|
+
ok = false;
|
|
131
|
+
}
|
|
132
|
+
if (op !== 'ADDED' && baseCount === 0) {
|
|
133
|
+
errors.push({
|
|
134
|
+
line: lineNo,
|
|
135
|
+
message: `a ${op} block needs a \`Base: <revision>\` line from \`aidlc spec show ${capability}\``,
|
|
136
|
+
});
|
|
137
|
+
ok = false;
|
|
138
|
+
}
|
|
139
|
+
if (op !== 'REMOVED' && bodyText.trim() === '') {
|
|
140
|
+
errors.push({ line: lineNo, message: `a ${op} block needs the requirement's text` });
|
|
141
|
+
ok = false;
|
|
142
|
+
}
|
|
143
|
+
const key = requirementKey(name);
|
|
144
|
+
const identity = `${capability}\u0000${key}`;
|
|
145
|
+
const first = seen.get(identity);
|
|
146
|
+
if (first !== undefined) {
|
|
147
|
+
errors.push({
|
|
148
|
+
line: lineNo,
|
|
149
|
+
message: `requirement "${name}" in ${capability} is already changed by the block at line ${first}`,
|
|
150
|
+
});
|
|
151
|
+
ok = false;
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
seen.set(identity, lineNo);
|
|
155
|
+
}
|
|
156
|
+
if (ok)
|
|
157
|
+
blocks.push({ op, capability, name, key, base, body: bodyText, line: lineNo });
|
|
158
|
+
}
|
|
159
|
+
// Prose before the first block is allowed; an empty section is not a delta.
|
|
160
|
+
if (raw.length === 0) {
|
|
161
|
+
errors.push({ line: start + 1, message: '`## Spec delta` has no blocks' });
|
|
162
|
+
}
|
|
163
|
+
errors.sort((a, b) => a.line - b.line);
|
|
164
|
+
return {
|
|
165
|
+
present: true,
|
|
166
|
+
section: trimBlank(lines.slice(start, end)).join('\n'),
|
|
167
|
+
blocks,
|
|
168
|
+
errors,
|
|
169
|
+
warnings,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function trimBlank(lines) {
|
|
173
|
+
let a = 0;
|
|
174
|
+
let b = lines.length;
|
|
175
|
+
while (a < b && lines[a].trim() === '')
|
|
176
|
+
a++;
|
|
177
|
+
while (b > a && lines[b - 1].trim() === '')
|
|
178
|
+
b--;
|
|
179
|
+
return lines.slice(a, b);
|
|
180
|
+
}
|
|
181
|
+
//# sourceMappingURL=delta.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"delta.js","sourceRoot":"","sources":["../../src/spec/delta.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM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; `applyFold` is the only writer. The split is
|
|
5
|
+
* what makes a fold all-or-nothing: every file's new content exists in memory
|
|
6
|
+
* before the first write, so a conflict found in the last block leaves the
|
|
7
|
+
* corpus exactly as it was (living-spec-corpus/AC-11), and `--dry-run` and the
|
|
8
|
+
* completion check in `aidlc transition` are the same computation as the real
|
|
9
|
+
* fold rather than a second copy of it.
|
|
10
|
+
*
|
|
11
|
+
* Preconditions are checked against the corpus as it is *before* the fold, for
|
|
12
|
+
* every block, so the conflict list is complete — not just the first one found.
|
|
13
|
+
*
|
|
14
|
+
* Requirements: living-spec-corpus/AC-10, living-spec-corpus/AC-11,
|
|
15
|
+
* living-spec-corpus/AC-12, living-spec-corpus/AC-13,
|
|
16
|
+
* living-spec-corpus/AC-14, living-spec-corpus/AC-17
|
|
17
|
+
*/
|
|
18
|
+
import type { FoldPlan } from './types.js';
|
|
19
|
+
/** The archive record already written for this instance, if any (AC-13). */
|
|
20
|
+
export declare function findArchiveRecord(projectRoot: string, instance: string): string | null;
|
|
21
|
+
/**
|
|
22
|
+
* Compute what folding `instance` would do. Throws only when the instance name
|
|
23
|
+
* is not a single path segment or the instance does not exist.
|
|
24
|
+
*/
|
|
25
|
+
export declare function planFold(projectRoot: string, instance: string, now?: Date): FoldPlan;
|
|
26
|
+
/** Write a ready plan; anything else is a no-op. Archive record last (design D5). */
|
|
27
|
+
export declare function applyFold(plan: FoldPlan): void;
|
|
28
|
+
/** One line per error or conflict, for the CLI and for transition violations. */
|
|
29
|
+
export declare function describeIssues(plan: FoldPlan): string[];
|
|
30
|
+
//# sourceMappingURL=fold.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fold.d.ts","sourceRoot":"","sources":["../../src/spec/fold.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAeH,OAAO,KAAK,EAKV,QAAQ,EAET,MAAM,YAAY,CAAC;AAIpB,4EAA4E;AAC5E,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAWtF;AAeD;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,WAAW,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,GAAE,IAAiB,GAAG,QAAQ,CA6DhG;AAED,qFAAqF;AACrF,wBAAgB,SAAS,CAAC,IAAI,EAAE,QAAQ,GAAG,IAAI,CAG9C;AAiID,iFAAiF;AACjF,wBAAgB,cAAc,CAAC,IAAI,EAAE,QAAQ,GAAG,MAAM,EAAE,CAQvD"}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fold an instance's spec delta into the corpus (living-spec-corpus/AC-10).
|
|
3
|
+
*
|
|
4
|
+
* `planFold` reads and computes; `applyFold` is the only writer. The split is
|
|
5
|
+
* what makes a fold all-or-nothing: every file's new content exists in memory
|
|
6
|
+
* before the first write, so a conflict found in the last block leaves the
|
|
7
|
+
* corpus exactly as it was (living-spec-corpus/AC-11), and `--dry-run` and the
|
|
8
|
+
* completion check in `aidlc transition` are the same computation as the real
|
|
9
|
+
* fold rather than a second copy of it.
|
|
10
|
+
*
|
|
11
|
+
* Preconditions are checked against the corpus as it is *before* the fold, for
|
|
12
|
+
* every block, so the conflict list is complete — not just the first one found.
|
|
13
|
+
*
|
|
14
|
+
* Requirements: living-spec-corpus/AC-10, living-spec-corpus/AC-11,
|
|
15
|
+
* living-spec-corpus/AC-12, living-spec-corpus/AC-13,
|
|
16
|
+
* living-spec-corpus/AC-14, living-spec-corpus/AC-17
|
|
17
|
+
*/
|
|
18
|
+
import { existsSync, readFileSync, readdirSync } from 'node:fs';
|
|
19
|
+
import { join } from 'node:path';
|
|
20
|
+
import { isSafeSegment } from '../core/paths.js';
|
|
21
|
+
import { atomicWrite } from '../state/snapshot-store.js';
|
|
22
|
+
import { ARCHIVE_RELATIVE, SPECS_RELATIVE, capabilityRelative, parseCapability, provenanceLine, revisionOf, } from './corpus.js';
|
|
23
|
+
import { parseDelta } from './delta.js';
|
|
24
|
+
const DATE_PREFIX_RE = /^\d{4}-\d{2}-\d{2}-$/;
|
|
25
|
+
/** The archive record already written for this instance, if any (AC-13). */
|
|
26
|
+
export function findArchiveRecord(projectRoot, instance) {
|
|
27
|
+
const dir = join(projectRoot, ARCHIVE_RELATIVE);
|
|
28
|
+
if (!existsSync(dir))
|
|
29
|
+
return null;
|
|
30
|
+
const suffix = `${instance}.md`;
|
|
31
|
+
const hit = readdirSync(dir).find((name) => name.length === 11 + suffix.length &&
|
|
32
|
+
name.endsWith(suffix) &&
|
|
33
|
+
DATE_PREFIX_RE.test(name.slice(0, 11)));
|
|
34
|
+
return hit ? `${ARCHIVE_RELATIVE}/${hit}` : null;
|
|
35
|
+
}
|
|
36
|
+
function emptyPlan(instance, status) {
|
|
37
|
+
return {
|
|
38
|
+
instance,
|
|
39
|
+
status,
|
|
40
|
+
operations: [],
|
|
41
|
+
errors: [],
|
|
42
|
+
conflicts: [],
|
|
43
|
+
warnings: [],
|
|
44
|
+
writes: [],
|
|
45
|
+
archive: null,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Compute what folding `instance` would do. Throws only when the instance name
|
|
50
|
+
* is not a single path segment or the instance does not exist.
|
|
51
|
+
*/
|
|
52
|
+
export function planFold(projectRoot, instance, now = new Date()) {
|
|
53
|
+
if (!isSafeSegment(instance)) {
|
|
54
|
+
throw new Error(`invalid instance name '${instance}'`);
|
|
55
|
+
}
|
|
56
|
+
const instanceDir = join(projectRoot, '.aidlc', 'state', instance);
|
|
57
|
+
if (!existsSync(instanceDir)) {
|
|
58
|
+
throw new Error(`instance '${instance}' not found`);
|
|
59
|
+
}
|
|
60
|
+
const existing = findArchiveRecord(projectRoot, instance);
|
|
61
|
+
if (existing)
|
|
62
|
+
return { ...emptyPlan(instance, 'already-folded'), archive: existing };
|
|
63
|
+
const requirementsPath = join(instanceDir, 'requirements.md');
|
|
64
|
+
if (!existsSync(requirementsPath))
|
|
65
|
+
return emptyPlan(instance, 'no-delta');
|
|
66
|
+
const delta = parseDelta(readFileSync(requirementsPath, 'utf8'));
|
|
67
|
+
if (!delta.present)
|
|
68
|
+
return { ...emptyPlan(instance, 'no-delta'), warnings: delta.warnings };
|
|
69
|
+
const plan = { ...emptyPlan(instance, 'blocked'), warnings: delta.warnings };
|
|
70
|
+
plan.errors.push(...delta.errors);
|
|
71
|
+
// Read every touched capability once, as it is before the fold.
|
|
72
|
+
const byCapability = new Map();
|
|
73
|
+
for (const block of delta.blocks) {
|
|
74
|
+
const list = byCapability.get(block.capability) ?? [];
|
|
75
|
+
list.push(block);
|
|
76
|
+
byCapability.set(block.capability, list);
|
|
77
|
+
}
|
|
78
|
+
const corpus = new Map();
|
|
79
|
+
for (const capability of byCapability.keys()) {
|
|
80
|
+
const file = join(projectRoot, SPECS_RELATIVE, `${capability}.md`);
|
|
81
|
+
const text = existsSync(file) ? readFileSync(file, 'utf8') : null;
|
|
82
|
+
const spec = text === null ? null : parseCapability(text, capability, capabilityRelative(capability));
|
|
83
|
+
if (spec)
|
|
84
|
+
plan.errors.push(...spec.errors);
|
|
85
|
+
corpus.set(capability, { text, spec });
|
|
86
|
+
}
|
|
87
|
+
for (const block of delta.blocks) {
|
|
88
|
+
const conflict = checkBlock(block, corpus.get(block.capability)?.spec ?? null);
|
|
89
|
+
if (conflict)
|
|
90
|
+
plan.conflicts.push(conflict);
|
|
91
|
+
}
|
|
92
|
+
if (plan.errors.length > 0 || plan.conflicts.length > 0)
|
|
93
|
+
return plan;
|
|
94
|
+
const date = now.toISOString().slice(0, 10);
|
|
95
|
+
const applied = new Map();
|
|
96
|
+
for (const [capability, blocks] of byCapability) {
|
|
97
|
+
const { text, spec } = corpus.get(capability);
|
|
98
|
+
const content = foldCapability(capability, text, spec, blocks, instance, date, applied);
|
|
99
|
+
plan.writes.push({ path: join(projectRoot, SPECS_RELATIVE, `${capability}.md`), content });
|
|
100
|
+
}
|
|
101
|
+
// Reported in the order the author wrote them, not the order they were applied.
|
|
102
|
+
plan.operations = delta.blocks.map((b) => applied.get(b));
|
|
103
|
+
const archiveName = `${date}-${instance}.md`;
|
|
104
|
+
plan.archive = `${ARCHIVE_RELATIVE}/${archiveName}`;
|
|
105
|
+
plan.writes.push({
|
|
106
|
+
path: join(projectRoot, ARCHIVE_RELATIVE, archiveName),
|
|
107
|
+
content: renderArchive(instance, date, plan.operations, delta.section),
|
|
108
|
+
});
|
|
109
|
+
plan.status = 'ready';
|
|
110
|
+
return plan;
|
|
111
|
+
}
|
|
112
|
+
/** Write a ready plan; anything else is a no-op. Archive record last (design D5). */
|
|
113
|
+
export function applyFold(plan) {
|
|
114
|
+
if (plan.status !== 'ready')
|
|
115
|
+
return;
|
|
116
|
+
for (const write of plan.writes)
|
|
117
|
+
atomicWrite(write.path, write.content);
|
|
118
|
+
}
|
|
119
|
+
function checkBlock(block, spec) {
|
|
120
|
+
const current = spec?.requirements.find((r) => r.key === block.key) ?? null;
|
|
121
|
+
const base = {
|
|
122
|
+
op: block.op,
|
|
123
|
+
capability: block.capability,
|
|
124
|
+
requirement: block.name,
|
|
125
|
+
currentRevision: current?.revision ?? null,
|
|
126
|
+
changedBy: current?.changedBy ?? null,
|
|
127
|
+
line: block.line,
|
|
128
|
+
};
|
|
129
|
+
if (block.op === 'ADDED') {
|
|
130
|
+
return current
|
|
131
|
+
? { ...base, reason: `${block.capability} already has a requirement "${current.name}"` }
|
|
132
|
+
: null;
|
|
133
|
+
}
|
|
134
|
+
if (!current) {
|
|
135
|
+
return { ...base, reason: `${block.capability} has no requirement "${block.name}"` };
|
|
136
|
+
}
|
|
137
|
+
if (current.revision !== block.base) {
|
|
138
|
+
const by = current.changedBy ? ` by ${current.changedBy}` : '';
|
|
139
|
+
return {
|
|
140
|
+
...base,
|
|
141
|
+
reason: `"${current.name}" was written against ${block.base}, but it is now ${current.revision}` +
|
|
142
|
+
`${by} — rewrite the block against the current text`,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
const NEW_FILE_HEADER = (capability) => [
|
|
148
|
+
`# ${capability}`,
|
|
149
|
+
'',
|
|
150
|
+
`What the system does today for \`${capability}\`. Changed only by folding a finished`,
|
|
151
|
+
"instance's spec delta (`aidlc spec fold`).",
|
|
152
|
+
'',
|
|
153
|
+
'## Requirements',
|
|
154
|
+
];
|
|
155
|
+
function renderBlock(heading, body, instance, date) {
|
|
156
|
+
return [heading, ...body.split('\n'), '', provenanceLine(instance, date)];
|
|
157
|
+
}
|
|
158
|
+
function foldCapability(capability, text, spec, blocks, instance, date, operations) {
|
|
159
|
+
const lines = text === null ? NEW_FILE_HEADER(capability) : text.split('\n');
|
|
160
|
+
// In-place edits bottom-up, so the line indices of earlier blocks stay valid.
|
|
161
|
+
const inPlace = blocks
|
|
162
|
+
.filter((b) => b.op !== 'ADDED')
|
|
163
|
+
.map((b) => ({ block: b, req: spec.requirements.find((r) => r.key === b.key) }))
|
|
164
|
+
.sort((a, b) => b.req.startLine - a.req.startLine);
|
|
165
|
+
for (const { block, req } of inPlace) {
|
|
166
|
+
if (block.op === 'MODIFIED') {
|
|
167
|
+
// The existing heading line is kept verbatim: the name's casing is the corpus's.
|
|
168
|
+
lines.splice(req.startLine, req.endLine - req.startLine, ...renderBlock(lines[req.startLine], block.body, instance, date));
|
|
169
|
+
operations.set(block, {
|
|
170
|
+
op: 'MODIFIED',
|
|
171
|
+
capability,
|
|
172
|
+
requirement: req.name,
|
|
173
|
+
before: req.revision,
|
|
174
|
+
after: revisionOf(block.body),
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
let end = req.endLine;
|
|
179
|
+
while (end < lines.length && lines[end].trim() === '')
|
|
180
|
+
end++;
|
|
181
|
+
lines.splice(req.startLine, end - req.startLine);
|
|
182
|
+
operations.set(block, { op: 'REMOVED', capability, requirement: req.name, before: req.revision, after: null });
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const added = blocks.filter((b) => b.op === 'ADDED');
|
|
186
|
+
if (added.length > 0) {
|
|
187
|
+
// Trailing blank lines (including the final newline's empty element) are
|
|
188
|
+
// re-laid once below; nothing else at the end of the file moves.
|
|
189
|
+
while (lines.length > 0 && lines[lines.length - 1].trim() === '')
|
|
190
|
+
lines.pop();
|
|
191
|
+
for (const block of added) {
|
|
192
|
+
lines.push('', ...renderBlock(`### Requirement: ${block.name}`, block.body, instance, date));
|
|
193
|
+
operations.set(block, {
|
|
194
|
+
op: 'ADDED',
|
|
195
|
+
capability,
|
|
196
|
+
requirement: block.name,
|
|
197
|
+
before: null,
|
|
198
|
+
after: revisionOf(block.body),
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
lines.push('');
|
|
202
|
+
}
|
|
203
|
+
return lines.join('\n');
|
|
204
|
+
}
|
|
205
|
+
function renderArchive(instance, date, operations, section) {
|
|
206
|
+
const out = [
|
|
207
|
+
`# Spec fold — ${instance}`,
|
|
208
|
+
'',
|
|
209
|
+
`Folded on ${date} from \`.aidlc/state/${instance}/requirements.md\`.`,
|
|
210
|
+
'',
|
|
211
|
+
'## Operations',
|
|
212
|
+
'',
|
|
213
|
+
...operations.map((o) => `- ${o.op} ${o.capability}: ${o.requirement} — ${o.before ?? 'none'} → ${o.after ?? 'none'}`),
|
|
214
|
+
'',
|
|
215
|
+
'## Delta as written',
|
|
216
|
+
'',
|
|
217
|
+
section,
|
|
218
|
+
'',
|
|
219
|
+
];
|
|
220
|
+
return out.join('\n');
|
|
221
|
+
}
|
|
222
|
+
/** One line per error or conflict, for the CLI and for transition violations. */
|
|
223
|
+
export function describeIssues(plan) {
|
|
224
|
+
const issues = plan.errors.map((e) => e.path ? `${e.path}:${e.line}: ${e.message}` : `requirements.md:${e.line}: ${e.message}`);
|
|
225
|
+
const conflicts = plan.conflicts.map((c) => `requirements.md:${c.line}: ${c.op} ${c.capability}: ${c.requirement} — ${c.reason}`);
|
|
226
|
+
return [...issues, ...conflicts];
|
|
227
|
+
}
|
|
228
|
+
//# sourceMappingURL=fold.js.map
|