slicekit 0.7.2 → 0.7.4
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/package.json +1 -1
- package/src/build/commit.js +128 -42
- package/src/build/red.js +41 -36
- package/src/build/verdict.js +50 -18
- package/src/check/rules/frozen.js +70 -19
- package/src/check/rules/lane.js +100 -0
- package/src/cli/build.js +9 -5
- package/src/hooks/paths.js +25 -10
- package/src/hooks/pre-edit.js +148 -17
- package/src/slicefile.js +8 -0
- package/src/state/index.js +28 -8
- package/src/state/transitions.js +24 -3
- package/src/testnames.js +293 -0
- package/templates/instructions/commands/build.md +26 -16
- package/templates/instructions/commands/plan.md +9 -1
- package/templates/instructions/commands/review.md +2 -1
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
// frozen (03 REQ-3.15, D-10): on a slice branch, a test of the slice's evaluation set
|
|
2
|
-
// the red commit recorded in the slice's state file, unless
|
|
3
|
-
//
|
|
4
|
-
// the
|
|
5
|
-
//
|
|
1
|
+
// frozen (03 REQ-3.15, D-10, D-102): on a slice branch, a test of the slice's evaluation set, found by
|
|
2
|
+
// its name in its file, stays as it was at the red commit recorded in the slice's state file, unless
|
|
3
|
+
// `build amend` unfroze that test after the red commit: the tip's state file lists it in `unfrozen`
|
|
4
|
+
// and the slice file was changed in a commit after red. Other tests of the same file are not
|
|
5
|
+
// compared. A path entry of a state file written before P8/S-60 is every evaluation test of that file
|
|
6
|
+
// by name in the slice file at the red commit, or the whole file when that evaluation set names none
|
|
7
|
+
// in it. The state file, the red commit, the slice file and the tests are all read from git objects
|
|
8
|
+
// (of the tip judged, or of the red commit), never the working tree: a file hidden with
|
|
9
|
+
// `assume-unchanged` or `skip-worktree` is judged by its committed content (issue #71).
|
|
6
10
|
//
|
|
7
11
|
// The tip is HEAD, or under --ci on a pull request the pull request's head (branchTip). No branch, a
|
|
8
12
|
// branch that is not a slice branch, no red commit recorded in any version of the state file (a slice
|
|
@@ -22,7 +26,8 @@ import { GitError, git, resolveCommit } from '../../git.js';
|
|
|
22
26
|
import { branchTip } from '../ci-event.js';
|
|
23
27
|
import { parseSliceAddr, sliceFromBranch, stateFileName } from '../../ids.js';
|
|
24
28
|
import { kitPaths } from '../../paths.js';
|
|
25
|
-
import { phaseOfDir } from '../../slicefile.js';
|
|
29
|
+
import { SliceFileError, parseSliceFile, phaseOfDir } from '../../slicefile.js';
|
|
30
|
+
import { TestNameError, entryFile, entryLabel, evaluationRows, findTest, frozenEntries, isEntry, isUnfrozen } from '../../testnames.js';
|
|
26
31
|
|
|
27
32
|
export const id = 'frozen';
|
|
28
33
|
export const blocking = true;
|
|
@@ -115,22 +120,68 @@ export function run(ctx) {
|
|
|
115
120
|
return [...removed, { file: stateRel, line: 0, message: `red commit ${red} of ${slice} is not in this repository` }];
|
|
116
121
|
}
|
|
117
122
|
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
if (
|
|
121
|
-
if (tests.length === 0) return removed;
|
|
123
|
+
const bad = listed.filter((t) => !isEntry(t) || !validTest(entryFile(t)));
|
|
124
|
+
if (bad.length > 0) return [...removed, { file: stateRel, line: 0, message: `frozen tests of ${slice} name a path that is not a repository path: ${bad.map((t) => (isEntry(t) ? entryLabel(t) : JSON.stringify(t))).join(', ')}` }];
|
|
125
|
+
if (listed.length === 0) return removed;
|
|
122
126
|
|
|
123
|
-
//
|
|
124
|
-
//
|
|
125
|
-
// the
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
127
|
+
// The slice file at the red commit: its evaluation set reads a path entry, the form of a state file
|
|
128
|
+
// written before P8/S-60, as every evaluation test of that file by name (D-102). A later change to
|
|
129
|
+
// the slice file does not change what is frozen; with no slice file at red that parses, a path
|
|
130
|
+
// entry is the whole file.
|
|
131
|
+
const phasesRel = posixRel(root, paths.phases);
|
|
132
|
+
const sliceFiles = sliceFilesAt(root, tip, phasesRel, addr);
|
|
133
|
+
const redSliceFiles = sliceFilesAt(root, red, phasesRel, addr);
|
|
134
|
+
let rows = [];
|
|
135
|
+
if (redSliceFiles.length === 1) {
|
|
136
|
+
try {
|
|
137
|
+
rows = evaluationRows(parseSliceFile(showAt(root, red, redSliceFiles[0]) ?? '', redSliceFiles[0]));
|
|
138
|
+
} catch (e) {
|
|
139
|
+
if (!(e instanceof SliceFileError)) throw e;
|
|
140
|
+
}
|
|
130
141
|
}
|
|
142
|
+
const entries = frozenEntries(listed, rows);
|
|
143
|
+
|
|
144
|
+
// A test `build amend` unfroze after the red commit is not judged (REQ-3.15): the tip's state file
|
|
145
|
+
// lists it in `unfrozen`, and the slice file was amended after red. An amendment of the slice file
|
|
146
|
+
// alone unfreezes nothing (D-102). Only the slice branch's own commits count: --first-parent
|
|
147
|
+
// --no-merges leaves out a change to the slice file made on the default branch and merged in.
|
|
148
|
+
const amended = sliceFiles.length > 0
|
|
149
|
+
&& git(root, ['--literal-pathspecs', 'log', '--first-parent', '--no-merges', '--format=%H', `${red}..${tip}`, '--', ...sliceFiles]) !== '';
|
|
150
|
+
const tipState = text === null ? null : parse(text);
|
|
151
|
+
const unfrozen = amended && Array.isArray(tipState?.unfrozen) ? tipState.unfrozen.filter(isEntry) : [];
|
|
152
|
+
const judged = entries.filter((e) => !isUnfrozen(e, unfrozen));
|
|
153
|
+
if (judged.length === 0) return removed;
|
|
131
154
|
|
|
132
|
-
const
|
|
133
|
-
|
|
155
|
+
const files = [...new Set(judged.map((e) => e.file))];
|
|
156
|
+
const changed = new Set(lines(git(root, ['--literal-pathspecs', 'diff', '--name-only', '--no-renames', red, tip, '--', ...files])));
|
|
157
|
+
const findings = [];
|
|
158
|
+
for (const e of judged) {
|
|
159
|
+
if (!changed.has(e.file)) continue;
|
|
160
|
+
if (e.name === null) {
|
|
161
|
+
findings.push({ file: e.file, line: 0, message: `frozen test changed after red: ${e.file}` });
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
// A test frozen by name: its text at the tip against its text at red; another test of the file,
|
|
165
|
+
// or any other line of it, is not compared. A file that cannot be read by name is a change.
|
|
166
|
+
const before = bodyOf(showAt(root, red, e.file), e.name);
|
|
167
|
+
const after = bodyOf(showAt(root, tip, e.file), e.name);
|
|
168
|
+
if (after === null || after === undefined || before === undefined || after !== before) {
|
|
169
|
+
findings.push({ file: e.file, line: 0, message: `frozen test changed after red: ${entryLabel(e)}` });
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return [...removed, ...findings];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// The text of the test named `name` in `text`: null when the file or the test is missing; undefined
|
|
176
|
+
// when the file cannot be read by name (unparseable, or the name given twice).
|
|
177
|
+
function bodyOf(text, name) {
|
|
178
|
+
if (text === null) return null;
|
|
179
|
+
try {
|
|
180
|
+
return findTest(text, name)?.text ?? null;
|
|
181
|
+
} catch (e) {
|
|
182
|
+
if (e instanceof TestNameError) return undefined;
|
|
183
|
+
throw e;
|
|
184
|
+
}
|
|
134
185
|
}
|
|
135
186
|
|
|
136
187
|
export default { id, blocking, ratchet, run };
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// lane (03 REQ-3.7, REQ-1.5, D-103): a slice marked `lane: quick` changes no file under `src/`, `bin/`
|
|
2
|
+
// or `templates/`. Two findings, each naming the slice and the path:
|
|
3
|
+
//
|
|
4
|
+
// - a quick slice file whose `## Expected changes` names a path in one of those folders, at that
|
|
5
|
+
// path's line of the slice file;
|
|
6
|
+
// - on the quick slice's own branch, a file in one of those folders that the branch changes against
|
|
7
|
+
// the default branch. The changes are read from git objects (the merge base with the default branch
|
|
8
|
+
// against the tip judged, as the frozen rule reads them), never the working tree, so a change merged
|
|
9
|
+
// in from the default branch is not the slice's.
|
|
10
|
+
//
|
|
11
|
+
// A slice with no lane is never a finding. A slice file that does not parse is the citations rule's
|
|
12
|
+
// to report.
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import { GitError, git } from '../../git.js';
|
|
15
|
+
import { readText } from '../../fs.js';
|
|
16
|
+
import { sliceFromBranch } from '../../ids.js';
|
|
17
|
+
import { lines } from '../../markdown.js';
|
|
18
|
+
import { mergeBase } from '../../runbook.js';
|
|
19
|
+
import { findSliceFiles, parseSliceFile } from '../../slicefile.js';
|
|
20
|
+
import { branchTip } from '../ci-event.js';
|
|
21
|
+
|
|
22
|
+
export const id = 'lane';
|
|
23
|
+
export const blocking = true;
|
|
24
|
+
export const ratchet = false;
|
|
25
|
+
|
|
26
|
+
export const CODE_FOLDERS = ['src', 'bin', 'templates'];
|
|
27
|
+
|
|
28
|
+
const posixRel = (root, abs) => path.relative(root, abs).split(path.sep).join('/');
|
|
29
|
+
|
|
30
|
+
// True when `p`, a repository path, is one of the code folders or lies under one.
|
|
31
|
+
export function reachesCode(p) {
|
|
32
|
+
const clean = String(p).trim().replace(/\\/g, '/').replace(/^\.\//, '').replace(/^\/+/, '');
|
|
33
|
+
const first = clean.split('/')[0];
|
|
34
|
+
return CODE_FOLDERS.includes(first);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Every quick slice file in the working tree: [{ rel, parsed, text }].
|
|
38
|
+
function quickSlices(root, cfg) {
|
|
39
|
+
const out = [];
|
|
40
|
+
for (const f of findSliceFiles(root, cfg)) {
|
|
41
|
+
let text;
|
|
42
|
+
let parsed;
|
|
43
|
+
try {
|
|
44
|
+
text = readText(f.path);
|
|
45
|
+
parsed = parseSliceFile(text, f.path);
|
|
46
|
+
} catch {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (parsed.lane === 'quick') out.push({ rel: posixRel(root, f.path), parsed, text });
|
|
50
|
+
}
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// The paths the branch at `tip` changes against the default branch, or null when the merge base
|
|
55
|
+
// cannot be read.
|
|
56
|
+
function branchChanges(root, cfg, tip) {
|
|
57
|
+
let base;
|
|
58
|
+
try {
|
|
59
|
+
base = mergeBase(root, cfg, tip);
|
|
60
|
+
} catch (e) {
|
|
61
|
+
if (e instanceof GitError) return null;
|
|
62
|
+
throw e;
|
|
63
|
+
}
|
|
64
|
+
const out = git(root, ['--literal-pathspecs', 'diff', '--name-only', '--no-renames', base, tip]);
|
|
65
|
+
return out.split('\n').filter((l) => l !== '');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function run(ctx) {
|
|
69
|
+
const { root, cfg } = ctx;
|
|
70
|
+
const quick = quickSlices(root, cfg);
|
|
71
|
+
if (quick.length === 0) return [];
|
|
72
|
+
const findings = [];
|
|
73
|
+
for (const s of quick) {
|
|
74
|
+
s.parsed.expectedChanges.forEach((p, i) => {
|
|
75
|
+
if (!reachesCode(p)) return;
|
|
76
|
+
findings.push({
|
|
77
|
+
file: s.rel,
|
|
78
|
+
line: s.parsed.expectedChangeLines[i] ?? 0,
|
|
79
|
+
message: `${s.parsed.id} is marked lane: quick but its expected changes name ${p}, under ${CODE_FOLDERS.map((d) => `${d}/`).join(', ')} (D-103)`,
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const at = branchTip(ctx);
|
|
85
|
+
const slice = at ? sliceFromBranch(at.branch) : null;
|
|
86
|
+
const mine = slice ? quick.find((s) => s.parsed.id === slice) : undefined;
|
|
87
|
+
if (!mine) return findings;
|
|
88
|
+
const laneLine = lines(mine.text).findIndex((l) => /^lane\s*:/.test(l)) + 1;
|
|
89
|
+
const changed = branchChanges(root, cfg, at.tip);
|
|
90
|
+
if (changed === null) {
|
|
91
|
+
findings.push({ file: mine.rel, line: laneLine, message: `${slice} is marked lane: quick and the branch's changes against ${cfg.defaultBranch} cannot be read; fetch ${cfg.defaultBranch}` });
|
|
92
|
+
return findings;
|
|
93
|
+
}
|
|
94
|
+
for (const p of changed.filter(reachesCode)) {
|
|
95
|
+
findings.push({ file: mine.rel, line: laneLine, message: `${slice} is marked lane: quick but its branch changes ${p}, under ${CODE_FOLDERS.map((d) => `${d}/`).join(', ')} (D-103)` });
|
|
96
|
+
}
|
|
97
|
+
return findings;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export default { id, blocking, ratchet, run };
|
package/src/cli/build.js
CHANGED
|
@@ -28,7 +28,7 @@ import { INDEPENDENT_MISSING } from '../state/transitions.js';
|
|
|
28
28
|
|
|
29
29
|
const SUBCOMMANDS = ['next', 'start', 'red', 'commit', 'render', 'config', 'amend', 'pr', 'verdict', 'decline', 'finish', 'sync', 'parallel'];
|
|
30
30
|
const USAGE =
|
|
31
|
-
'build next | build start <id> [--descr <d>] | build parallel P<n> | build red <id> --tests <a,b> [--worktree <p>] | build commit <id> --message <m> [--require-green] [--worktree <p>] (commits the staged paths; with nothing staged, every changed path) | build render <id> [--worktree <p>] | build config <id> [--worktree <p>] | build amend <id> [--test <
|
|
31
|
+
'build next | build start <id> [--descr <d>] | build parallel P<n> | build red <id> --tests <a,b> [--worktree <p>] | build commit <id> --message <m> [--require-green] [--worktree <p>] (commits the staged paths; with nothing staged, every changed path) | build render <id> [--worktree <p>] | build config <id> [--worktree <p>] | build amend <id> [--test "<file> » <name>" | --test <file>] --reason <r> [--worktree <p>] (no --test: the slice file alone, every frozen test stays frozen) | build pr <id> [--notes <file>] [--worktree <p>] | build verdict <id> <file> [--independent] [--worktree <p>] | build decline <id> <H-n> [--worktree <p>] | build finish <id> [--wait <s>, default 90] [--worktree <p>] | build sync <id> [--worktree <p>]';
|
|
32
32
|
|
|
33
33
|
const OPTIONS = {
|
|
34
34
|
descr: { type: 'string' },
|
|
@@ -140,8 +140,10 @@ function configStep(root, io, positionals, values) {
|
|
|
140
140
|
function amendStep(root, io, positionals, values) {
|
|
141
141
|
if (positionals.length !== 1 || values.reason === undefined) return usage(io);
|
|
142
142
|
const cfg = loadConfig(root);
|
|
143
|
-
const { sha, test } = amend(root, cfg, positionals[0], { test: values.test, reason: values.reason, worktree: values.worktree });
|
|
144
|
-
|
|
143
|
+
const { sha, test, again } = amend(root, cfg, positionals[0], { test: values.test, reason: values.reason, worktree: values.worktree });
|
|
144
|
+
// A second amendment of a test already unfrozen unfreezes nothing (D-102).
|
|
145
|
+
if (again) io.stdout(`${sha.slice(0, 7)} amended the slice file; ${test} was already unfrozen\n`);
|
|
146
|
+
else io.stdout(test === null ? `${sha.slice(0, 7)} amended the slice file\n` : `unfroze ${test}\n`);
|
|
145
147
|
return 0;
|
|
146
148
|
}
|
|
147
149
|
|
|
@@ -169,8 +171,10 @@ function verdictStep(root, io, positionals, values) {
|
|
|
169
171
|
independent: Boolean(values.independent),
|
|
170
172
|
});
|
|
171
173
|
const list = (ids) => (ids.length > 0 ? ids.join(', ') : 'none');
|
|
172
|
-
|
|
173
|
-
|
|
174
|
+
// The slice's in-loop budget: two, one for a quick slice (D-103).
|
|
175
|
+
const rounds = r.loopRounds ?? LOOP_ROUNDS;
|
|
176
|
+
const out = [`next: ${r.next}`, `blockers: ${list(r.blockers)}`, `should-haves: ${list(r.shouldHaves)}`, `rounds: ${r.roundsUsed} of ${rounds}`];
|
|
177
|
+
if (r.next === 'ask' && r.roundsUsed >= rounds) out.push(`fix: not available after round ${rounds}; decline or leave for the independent review`);
|
|
174
178
|
if (r.verdictPath) out.push(`verdict ${r.verdictPath}`);
|
|
175
179
|
// D-101: the command of the next independent round, when one is left after a blocker.
|
|
176
180
|
if (r.reviewStart) out.push(`review: ${r.reviewStart}`);
|
package/src/hooks/paths.js
CHANGED
|
@@ -5,6 +5,7 @@ import path from 'node:path';
|
|
|
5
5
|
import { GitError, currentBranch } from '../git.js';
|
|
6
6
|
import { sliceFromBranch } from '../ids.js';
|
|
7
7
|
import { readSlice } from '../state/index.js';
|
|
8
|
+
import { entryFile, isEntry } from '../testnames.js';
|
|
8
9
|
|
|
9
10
|
export const toPosix = (p) => p.split(path.sep).join('/');
|
|
10
11
|
|
|
@@ -115,26 +116,40 @@ export function activeSlice(root, cfg) {
|
|
|
115
116
|
// The slice states in which its frozen tests are guarded (REQ-3.3).
|
|
116
117
|
export const GUARDED_STATES = new Set(['tests-red', 'building']);
|
|
117
118
|
|
|
118
|
-
|
|
119
|
+
// A state file's `frozenTests` and `unfrozen`: each entry a path (a whole file, the form written before
|
|
120
|
+
// P8/S-60) or { file, name } (a test frozen by its name, D-102). Throws on any other shape, so a guard
|
|
121
|
+
// reading them fails closed.
|
|
122
|
+
export function checkFrozenLists(active) {
|
|
123
|
+
for (const field of ['frozenTests', 'unfrozen']) {
|
|
124
|
+
const v = active.state[field];
|
|
125
|
+
if (!Array.isArray(v) || !v.every(isEntry)) throw new Error(`${active.id} state: ${field} must be an array of strings (paths) or { file, name } entries`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
119
128
|
|
|
120
|
-
// The
|
|
121
|
-
//
|
|
122
|
-
//
|
|
123
|
-
//
|
|
129
|
+
// The files that hold a test frozen for root's active slice: {id, files: [POSIX path relative to root's
|
|
130
|
+
// real path]}. A file is left out once `unfrozen` lists it whole, or lists every test frozen in it by
|
|
131
|
+
// name; a path entry is the whole file. Null when tests are not frozen, no slice is active, or the slice
|
|
132
|
+
// is not in a guarded state. Throws when the state file cannot be read or its lists are malformed, so
|
|
133
|
+
// a guard calling it fails closed.
|
|
124
134
|
export function frozenTests(root, cfg) {
|
|
125
135
|
if (!cfg.freezeTests) return null;
|
|
126
136
|
const active = activeSlice(root, cfg);
|
|
127
137
|
if (!active) return null;
|
|
128
138
|
if (active.error) throw new Error(active.error);
|
|
129
|
-
|
|
130
|
-
if (!stringList(active.state[field])) throw new Error(`${active.id} state: ${field} must be an array of strings`);
|
|
131
|
-
}
|
|
139
|
+
checkFrozenLists(active);
|
|
132
140
|
if (!GUARDED_STATES.has(active.state.state)) return null;
|
|
133
141
|
const canon = real(path.resolve(root));
|
|
134
142
|
const rel = (f) => toPosix(path.relative(canon, real(path.resolve(canon, fromGitBash(f.replace(/\\/g, '/'))))));
|
|
135
143
|
const key = (p) => (process.platform === 'win32' ? p.toLowerCase() : p);
|
|
136
|
-
const
|
|
137
|
-
const
|
|
144
|
+
const norm = (e) => (typeof e === 'string' ? { file: key(rel(e)), name: null } : { file: key(rel(e.file)), name: e.name });
|
|
145
|
+
const unfrozen = active.state.unfrozen.map(norm);
|
|
146
|
+
const lifted = (e) => unfrozen.some((u) => u.file === e.file && (u.name === null || (e.name !== null && u.name === e.name)));
|
|
147
|
+
const files = [];
|
|
148
|
+
for (const e of active.state.frozenTests) {
|
|
149
|
+
if (lifted(norm(e))) continue;
|
|
150
|
+
const f = rel(entryFile(e));
|
|
151
|
+
if (!files.some((g) => key(g) === key(f))) files.push(f);
|
|
152
|
+
}
|
|
138
153
|
return { id: active.id, files };
|
|
139
154
|
}
|
|
140
155
|
|
package/src/hooks/pre-edit.js
CHANGED
|
@@ -10,9 +10,13 @@
|
|
|
10
10
|
// — always; (4) a path that matches a pattern of the root's `agents.denyPaths` (REQ-3.13) — always;
|
|
11
11
|
// then, only while
|
|
12
12
|
// the file's own root has an active slice (a `slice/*` branch with a state file) in `tests-red` or
|
|
13
|
-
// `building`: (1) a frozen test of that slice, unless `unfrozen` lists it
|
|
14
|
-
//
|
|
15
|
-
// the
|
|
13
|
+
// `building`: (1) a change to a frozen test of that slice, unless `unfrozen` lists it: a test frozen by
|
|
14
|
+
// name (D-102) is denied when the call changes its text against the red commit, removes it or renames
|
|
15
|
+
// it, and another test or line of the same file may change; a file frozen whole (a path entry of a
|
|
16
|
+
// state file written before P8/S-60 whose evaluation set names no test in it) is denied for any
|
|
17
|
+
// change; (2) a file under the docs root that does not exist yet (only a create can name a missing
|
|
18
|
+
// file), except the runbook, anything in the slice's own phase directory, and a path the slice file's
|
|
19
|
+
// Expected changes names (D-102).
|
|
16
20
|
//
|
|
17
21
|
// The file's own root is the nearest directory above it holding `.slicekit/config.json`, else its git
|
|
18
22
|
// top level, else the session's root: a session in the main checkout editing a nested worktree is
|
|
@@ -29,9 +33,10 @@ import { CONFIG_PATH, loadConfig } from '../config.js';
|
|
|
29
33
|
import { GitError, git } from '../git.js';
|
|
30
34
|
import { parseSliceAddr } from '../ids.js';
|
|
31
35
|
import { kitPaths } from '../paths.js';
|
|
32
|
-
import { phaseOfDir } from '../slicefile.js';
|
|
36
|
+
import { findSliceFiles, parseSliceFile, phaseOfDir } from '../slicefile.js';
|
|
37
|
+
import { TestNameError, entryFile, evaluationRows, findTest, frozenEntries, isUnfrozen } from '../testnames.js';
|
|
33
38
|
import { allow, deny } from './io.js';
|
|
34
|
-
import { activeSlice, denyPaths, existingAncestor, fromGitBash, leaves, matchesDeny, namesUnder, real, toPosix } from './paths.js';
|
|
39
|
+
import { activeSlice, checkFrozenLists, denyPaths, existingAncestor, fromGitBash, leaves, matchesDeny, namesUnder, real, toPosix } from './paths.js';
|
|
35
40
|
|
|
36
41
|
const WIN = process.platform === 'win32';
|
|
37
42
|
const GUARDED_STATES = new Set(['tests-red', 'building']);
|
|
@@ -115,7 +120,6 @@ function ownRoot(abs) {
|
|
|
115
120
|
return top ? { root: top, config: false } : null;
|
|
116
121
|
}
|
|
117
122
|
|
|
118
|
-
const stringList = (v) => Array.isArray(v) && v.every((s) => typeof s === 'string');
|
|
119
123
|
|
|
120
124
|
// Whether rel lies inside the slice's own phase directory, `<docs>/phases/P<n>[-name]/`.
|
|
121
125
|
function inOwnPhase(rel, phasesRel, sliceId) {
|
|
@@ -161,20 +165,14 @@ function judge(filePath, input, hookRoot, hookCfg) {
|
|
|
161
165
|
|
|
162
166
|
const active = activeSlice(root, cfg);
|
|
163
167
|
if (active?.error) throw new Error(active.error);
|
|
164
|
-
if (active)
|
|
165
|
-
for (const field of ['frozenTests', 'unfrozen']) {
|
|
166
|
-
if (!stringList(active.state[field])) throw new Error(`${active.id} state: ${field} must be an array of strings`);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
168
|
+
if (active) checkFrozenLists(active);
|
|
169
169
|
if (!active || !GUARDED_STATES.has(active.state.state)) return allow();
|
|
170
170
|
const { id, state } = active;
|
|
171
|
+
const slice = sliceOf(root, cfg, id);
|
|
171
172
|
|
|
172
173
|
// (1)
|
|
173
|
-
if (cfg.freezeTests) {
|
|
174
|
-
|
|
175
|
-
if (listed(state.frozenTests) && !listed(state.unfrozen)) {
|
|
176
|
-
return deny(`frozen for ${id}; amend the slice file to change the evaluation set`);
|
|
177
|
-
}
|
|
174
|
+
if (cfg.freezeTests && changesFrozen(root, rel, abs, state, slice, input)) {
|
|
175
|
+
return deny(`frozen for ${id}; amend the slice file to change the evaluation set`);
|
|
178
176
|
}
|
|
179
177
|
|
|
180
178
|
// (2)
|
|
@@ -182,12 +180,145 @@ function judge(filePath, input, hookRoot, hookCfg) {
|
|
|
182
180
|
const docs = toPosix(path.relative(root, k.docs));
|
|
183
181
|
if (under(rel, docs) && !same(rel, docs) && !fs.existsSync(abs)
|
|
184
182
|
&& !same(rel, toPosix(path.relative(root, k.runbook)))
|
|
185
|
-
&& !inOwnPhase(rel, toPosix(path.relative(root, k.phases)), id)
|
|
183
|
+
&& !inOwnPhase(rel, toPosix(path.relative(root, k.phases)), id)
|
|
184
|
+
&& !namedInExpected(rel, sliceAtHead(root, slice))) {
|
|
186
185
|
return deny("no slice starts a document; a document changes only when this slice's code made it wrong");
|
|
187
186
|
}
|
|
188
187
|
return allow();
|
|
189
188
|
}
|
|
190
189
|
|
|
190
|
+
// The active slice's parsed slice file under root, or null when none parses with that id.
|
|
191
|
+
function sliceOf(root, cfg, id) {
|
|
192
|
+
for (const f of findSliceFiles(root, cfg)) {
|
|
193
|
+
try {
|
|
194
|
+
const s = parseSliceFile(fs.readFileSync(f.path, 'utf8'), f.path);
|
|
195
|
+
if (s.id === id) return s;
|
|
196
|
+
} catch {
|
|
197
|
+
// A slice file that does not parse names nothing.
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// The slice file as committed at HEAD (D-102). The guard allows an Edit of the slice file, so the
|
|
204
|
+
// new-document exception reads Expected changes from git: only `build amend` commits the slice file.
|
|
205
|
+
// Null when the file is not at HEAD or does not parse (names nothing). A repository with no commit
|
|
206
|
+
// yet has no HEAD to read; its slice file is the working tree's.
|
|
207
|
+
function sliceAtHead(root, slice) {
|
|
208
|
+
if (!slice?.path) return null;
|
|
209
|
+
const rel = relInside(root, real(slice.path));
|
|
210
|
+
if (rel === null) return null;
|
|
211
|
+
let text;
|
|
212
|
+
try {
|
|
213
|
+
text = git(root, ['show', `HEAD:./${rel}`]);
|
|
214
|
+
} catch (e) {
|
|
215
|
+
if (!(e instanceof GitError)) throw e;
|
|
216
|
+
try {
|
|
217
|
+
git(root, ['rev-parse', '--verify', '--quiet', 'HEAD']);
|
|
218
|
+
return null;
|
|
219
|
+
} catch (e2) {
|
|
220
|
+
if (e2 instanceof GitError) return slice;
|
|
221
|
+
throw e2;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
try {
|
|
225
|
+
return parseSliceFile(text, slice.path);
|
|
226
|
+
} catch {
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// The slice file's Expected changes name `rel` (D-102): a new document the slice was planned to write.
|
|
232
|
+
const namedInExpected = (rel, slice) =>
|
|
233
|
+
(slice?.expectedChanges ?? []).some((p) => same(rel, toPosix(path.posix.normalize(p.replace(/\\/g, '/'))).replace(/^\.\//, '')));
|
|
234
|
+
|
|
235
|
+
// The file's text after the tool call: Write's content, or Edit's and MultiEdit's replacements applied
|
|
236
|
+
// to the text on disk. Null when the call cannot be applied, so it cannot be judged (fails closed).
|
|
237
|
+
function textAfter(abs, input) {
|
|
238
|
+
const tool = input?.tool_name;
|
|
239
|
+
const ti = input?.tool_input ?? {};
|
|
240
|
+
if (tool === 'Write') return typeof ti.content === 'string' ? ti.content : null;
|
|
241
|
+
const edits = tool === 'Edit' ? [ti] : tool === 'MultiEdit' && Array.isArray(ti.edits) ? ti.edits : null;
|
|
242
|
+
if (!edits || !fs.existsSync(abs)) return null;
|
|
243
|
+
let text = fs.readFileSync(abs, 'utf8');
|
|
244
|
+
for (const e of edits) {
|
|
245
|
+
if (typeof e?.old_string !== 'string' || typeof e?.new_string !== 'string' || e.old_string === '') return null;
|
|
246
|
+
let from = e.old_string;
|
|
247
|
+
if (!text.includes(from)) {
|
|
248
|
+
// The tool matches across line endings; so does the guard.
|
|
249
|
+
text = text.replace(/\r\n/g, '\n');
|
|
250
|
+
from = from.replace(/\r\n/g, '\n');
|
|
251
|
+
if (!text.includes(from)) return null;
|
|
252
|
+
}
|
|
253
|
+
const to = e.new_string.replace(/\r\n/g, text.includes('\r\n') ? '\r\n' : '\n');
|
|
254
|
+
text = e.replace_all === true ? text.split(from).join(to) : text.replace(from, () => to);
|
|
255
|
+
}
|
|
256
|
+
return text;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// The text of the test named `name`: null when missing; undefined when the file cannot be read by name.
|
|
260
|
+
function bodyOf(text, name) {
|
|
261
|
+
if (text === null) return null;
|
|
262
|
+
try {
|
|
263
|
+
return findTest(text, name)?.text ?? null;
|
|
264
|
+
} catch (e) {
|
|
265
|
+
if (e instanceof TestNameError) return undefined;
|
|
266
|
+
throw e;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// The slice file as it was at the red commit, read from git (D-102): a path entry of a state file
|
|
271
|
+
// written before P8/S-60 is read with that evaluation set, never the working tree's, which this guard
|
|
272
|
+
// does not protect. Null when there is no red commit, no such file at it, or it does not parse: each
|
|
273
|
+
// path entry is then the whole file, frozen for any change (fails closed).
|
|
274
|
+
function sliceAtRed(root, state, slice) {
|
|
275
|
+
if (!slice?.path || typeof state.redCommit !== 'string' || state.redCommit === '') return null;
|
|
276
|
+
const rel = relInside(root, real(slice.path));
|
|
277
|
+
if (rel === null) return null;
|
|
278
|
+
let text;
|
|
279
|
+
try {
|
|
280
|
+
text = git(root, ['show', `${state.redCommit}:./${rel}`]);
|
|
281
|
+
} catch (e) {
|
|
282
|
+
if (e instanceof GitError) return null;
|
|
283
|
+
throw e;
|
|
284
|
+
}
|
|
285
|
+
try {
|
|
286
|
+
return parseSliceFile(text, slice.path);
|
|
287
|
+
} catch {
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Whether the call changes, removes or renames a test frozen in `rel` (REQ-3.3, D-102). A file frozen
|
|
293
|
+
// whole (a path entry the evaluation set names no test in) is denied for any change; a test frozen by
|
|
294
|
+
// name, only when its text after the call differs from its text at the red commit. Another test of
|
|
295
|
+
// the file, or any other line, may change.
|
|
296
|
+
function changesFrozen(root, rel, abs, state, slice, input) {
|
|
297
|
+
const at = (f) => normalise(f, root) ?? '';
|
|
298
|
+
const mine = state.frozenTests.filter((e) => same(rel, at(entryFile(e))));
|
|
299
|
+
if (mine.length === 0) return false;
|
|
300
|
+
const local = (e) => (typeof e === 'string' ? rel : { file: rel, name: e.name });
|
|
301
|
+
const unfrozen = state.unfrozen.filter((e) => same(rel, at(entryFile(e)))).map(local);
|
|
302
|
+
const rows = mine.some((e) => typeof e === 'string') ? evaluationRows(sliceAtRed(root, state, slice)) : [];
|
|
303
|
+
const entries = frozenEntries(mine.map(local), rows, same).filter((e) => !isUnfrozen(e, unfrozen, same));
|
|
304
|
+
if (entries.length === 0) return false;
|
|
305
|
+
if (entries.some((e) => e.name === null)) return true;
|
|
306
|
+
const after = textAfter(abs, input);
|
|
307
|
+
if (after === null) return true;
|
|
308
|
+
let atRed = null;
|
|
309
|
+
try {
|
|
310
|
+
atRed = git(root, ['show', `${state.redCommit}:./${rel}`]);
|
|
311
|
+
} catch (e) {
|
|
312
|
+
if (!(e instanceof GitError)) throw e;
|
|
313
|
+
}
|
|
314
|
+
if (typeof state.redCommit !== 'string' || atRed === null) return true;
|
|
315
|
+
return entries.some((e) => {
|
|
316
|
+
const before = bodyOf(atRed, e.name);
|
|
317
|
+
const now = bodyOf(after, e.name);
|
|
318
|
+
return before === undefined || before === null || now === undefined || now === null || before !== now;
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
|
|
191
322
|
export function handle(input, { root, cfg }) {
|
|
192
323
|
const given = PATH_KEYS.filter((k) => input?.tool_input?.[k] !== undefined);
|
|
193
324
|
if (!given.length) throw new Error('tool_input.file_path is missing');
|
package/src/slicefile.js
CHANGED
|
@@ -16,6 +16,7 @@ export class SliceFileError extends Error {
|
|
|
16
16
|
const KINDS = ['happy', 'negative', 'e2e'];
|
|
17
17
|
const SCALARS = ['slice', 'name', 'repo', 'group', 'runbook'];
|
|
18
18
|
const LISTS = ['requirements', 'decisions'];
|
|
19
|
+
const LANES = ['quick'];
|
|
19
20
|
|
|
20
21
|
// The text under the first `## <title>` heading, up to the next level-2 heading.
|
|
21
22
|
function section(body, title) {
|
|
@@ -67,6 +68,12 @@ export function parseSliceFile(text, file) {
|
|
|
67
68
|
if (!['yes', 'true', 'no', 'false'].includes(rb)) {
|
|
68
69
|
fail(`front-matter field "runbook" must be yes or no, got "${data.runbook}"`, 'runbook');
|
|
69
70
|
}
|
|
71
|
+
// The optional lane (REQ-1.5, D-103): absent, or `quick`.
|
|
72
|
+
let lane = null;
|
|
73
|
+
if ('lane' in data && data.lane !== '' && data.lane != null) {
|
|
74
|
+
lane = String(data.lane).trim().toLowerCase();
|
|
75
|
+
if (!LANES.includes(lane)) fail(`front-matter field "lane" must be quick, got "${data.lane}"`, 'lane');
|
|
76
|
+
}
|
|
70
77
|
|
|
71
78
|
const done = section(body, 'Done when');
|
|
72
79
|
if (done === null) fail('missing section "## Done when"', 'Done when');
|
|
@@ -117,6 +124,7 @@ export function parseSliceFile(text, file) {
|
|
|
117
124
|
repo: String(data.repo),
|
|
118
125
|
group: String(data.group),
|
|
119
126
|
runbook: rb === 'yes' || rb === 'true',
|
|
127
|
+
lane,
|
|
120
128
|
requirements: asList(data.requirements),
|
|
121
129
|
decisions: asList(data.decisions),
|
|
122
130
|
doneWhen,
|
package/src/state/index.js
CHANGED
|
@@ -6,6 +6,7 @@ import { parseSliceAddr, stateFileName } from '../ids.js';
|
|
|
6
6
|
import { withFileLock } from '../lock.js';
|
|
7
7
|
import { kitPaths } from '../paths.js';
|
|
8
8
|
import { findSliceFiles, parseSliceFile } from '../slicefile.js';
|
|
9
|
+
import { entryLabel, evaluationRows, frozenEntries } from '../testnames.js';
|
|
9
10
|
import { TRANSITIONS, TransitionError, allowedMove, nextStates, validateEvidence } from './transitions.js';
|
|
10
11
|
|
|
11
12
|
export { TransitionError };
|
|
@@ -187,22 +188,41 @@ export function recordHead(root, cfg, id, pr) {
|
|
|
187
188
|
});
|
|
188
189
|
}
|
|
189
190
|
|
|
190
|
-
//
|
|
191
|
-
//
|
|
192
|
-
//
|
|
193
|
-
export function
|
|
191
|
+
// The tests a state file freezes, as [{ file, name }] (`name` null for a whole file), read with the
|
|
192
|
+
// slice file's evaluation set: a path string, the form written before P8/S-60, is every evaluation
|
|
193
|
+
// test of that file by name, or the whole file when the evaluation set names none in it (D-102).
|
|
194
|
+
export function frozenOf(state, slice) {
|
|
195
|
+
return frozenEntries(state.frozenTests, evaluationRows(slice));
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Whether `test` (a path, or { file, name }) is frozen in `current`: a path only when it is listed as a
|
|
199
|
+
// path, the form of a state file written before P8/S-60; the file of a test frozen by name is not, so a
|
|
200
|
+
// bare path never unfreezes tests frozen by name (REQ-1.5, D-102); { file, name } is one of the tests
|
|
201
|
+
// frozenOf reads. `slice` is the slice file at the red commit.
|
|
202
|
+
export function isFrozen(current, test, slice) {
|
|
203
|
+
if (typeof test === 'string') return current.frozenTests.some((t) => typeof t === 'string' && t === test);
|
|
204
|
+
return frozenOf(current, slice).some((e) => e.file === test.file && e.name === test.name);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Only a test frozen at tests-red can be unfrozen; any other path has nothing to release. `test` is a
|
|
208
|
+
// path (the whole file, as before P8/S-60) or { file, name } (D-102); `slice` is the parsed slice file,
|
|
209
|
+
// whose evaluation set reads an old path entry by name. `extra` joins the history entry's evidence
|
|
210
|
+
// (build amend gives the amendment commit and the reason). `to` moves the slice in the same write:
|
|
211
|
+
// build amend during review takes it back to building (D-43).
|
|
212
|
+
export function unfreeze(root, cfg, id, test, extra = {}, { to, slice } = {}) {
|
|
194
213
|
return withLock(root, cfg, id, () => {
|
|
195
214
|
const current = readSlice(root, cfg, id);
|
|
196
|
-
|
|
197
|
-
|
|
215
|
+
const label = entryLabel(test);
|
|
216
|
+
if (!isFrozen(current, test, slice)) {
|
|
217
|
+
throw new TransitionError(`${label} is not a frozen test of ${id}`);
|
|
198
218
|
}
|
|
199
219
|
const state = to ?? current.state;
|
|
200
220
|
const next = {
|
|
201
221
|
...current,
|
|
202
222
|
state,
|
|
203
223
|
...(state === current.state ? {} : { handoff: null }),
|
|
204
|
-
unfrozen: [...current.unfrozen, file],
|
|
205
|
-
history: [...current.history, entry(current.state, state, 'cli', { unfreeze:
|
|
224
|
+
unfrozen: [...current.unfrozen, typeof test === 'string' ? test : { file: test.file, name: test.name }],
|
|
225
|
+
history: [...current.history, entry(current.state, state, 'cli', { unfreeze: label, ...extra })],
|
|
206
226
|
};
|
|
207
227
|
writeSlice(root, cfg, next);
|
|
208
228
|
return next;
|