slicekit 0.3.2 → 0.3.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/audit.js +233 -0
- package/src/build/pr.js +10 -19
- package/src/check/ci-event.js +3 -2
- package/src/check/rules/runbook.js +60 -0
- package/src/cli/audit.js +89 -0
- package/src/cli/branch.js +66 -7
- package/src/cli/index.js +6 -0
- package/src/cli/ops.js +78 -0
- package/src/cli/release.js +57 -0
- package/src/cli/update.js +4 -0
- package/src/cli/workflow.js +2 -0
- package/src/config.js +1 -1
- package/src/hooks/subagent-start.js +7 -3
- package/src/init.js +23 -1
- package/src/markdown.js +2 -1
- package/src/observability.js +203 -0
- package/src/ops.js +223 -0
- package/src/paths.js +1 -0
- package/src/release.js +296 -0
- package/src/reports.js +131 -0
- package/src/runbook.js +96 -0
- package/src/workflow.js +23 -7
- package/templates/claude-code/agents/ops.md +3 -3
- package/templates/instructions/agents/auditor.md +13 -2
- package/templates/instructions/agents/ops.md +30 -9
- package/templates/instructions/commands/audit.md +38 -0
- package/templates/instructions/commands/intake.md +10 -0
- package/templates/instructions/commands/ops.md +38 -0
- package/templates/instructions/commands/release.md +51 -0
package/package.json
CHANGED
package/src/audit.js
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
// `slicekit audit` (REQ-2.14, D-58): the claims `/slicekit audit` hands the auditor, each with the
|
|
2
|
+
// files it names and the commits that could have broken it, and the check of the auditor's report.
|
|
3
|
+
// It reads the spec, the runbook, graph.json and git's history; it changes no tracked file.
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { readText } from './fs.js';
|
|
7
|
+
import { GitError, git } from './git.js';
|
|
8
|
+
import { slug } from './graph/docs.js';
|
|
9
|
+
import { indexGraph, loadGraph, normalisePath } from './graph/load.js';
|
|
10
|
+
import { GraphError, cmp, keyOf } from './graph/schema.js';
|
|
11
|
+
import { headings, lines, prose } from './markdown.js';
|
|
12
|
+
import { kitPaths } from './paths.js';
|
|
13
|
+
import { footerProblems, lineExists, nextReport, reportSections, sectionTable } from './reports.js';
|
|
14
|
+
import { reqBlocks } from './stage.js';
|
|
15
|
+
|
|
16
|
+
export const STATUSES = ['holds', 'drifted', 'unverifiable'];
|
|
17
|
+
export const COLUMNS = ['Claim', 'Where', 'Evidence', 'Found at', 'Status', 'Commit'];
|
|
18
|
+
|
|
19
|
+
const posix = (p) => p.split(path.sep).join('/');
|
|
20
|
+
const CODE_SPAN = /`([^`\n]+)`/g;
|
|
21
|
+
const LINK = /\[[^\]]*\]\(\s*<?((?:[^()\s<>]|\([^()\s]*\))+)>?(?:\s+(?:"[^"]*"|'[^']*'))?\s*\)/g;
|
|
22
|
+
|
|
23
|
+
// The file paths of the graph and every directory that holds one.
|
|
24
|
+
function graphPaths(index) {
|
|
25
|
+
const files = new Set(index.byKind('file').map((n) => keyOf(n.id)));
|
|
26
|
+
const dirs = new Set();
|
|
27
|
+
for (const f of files) {
|
|
28
|
+
const parts = f.split('/');
|
|
29
|
+
for (let i = 1; i < parts.length; i++) dirs.add(parts.slice(0, i).join('/'));
|
|
30
|
+
}
|
|
31
|
+
return { files, dirs };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// The paths a claim's text names that are a file of the graph or a directory holding one: each code
|
|
35
|
+
// span and each link target, read from the root and, for a link, from the claim's own directory.
|
|
36
|
+
function namedFiles(text, docRel, known) {
|
|
37
|
+
const out = new Set();
|
|
38
|
+
const consider = (raw, relative) => {
|
|
39
|
+
const token = raw.trim().replace(/[#?].*$/, '').replace(/:\d+(?:-\d+)?$/, '');
|
|
40
|
+
const tries = [normalisePath(token)];
|
|
41
|
+
if (relative) tries.push(posix(path.posix.normalize(path.posix.join(path.posix.dirname(docRel), token.replace(/\\/g, '/')))).replace(/\/+$/, ''));
|
|
42
|
+
for (const p of tries) {
|
|
43
|
+
if (p !== '' && p !== '.' && !p.startsWith('..') && (known.files.has(p) || known.dirs.has(p))) {
|
|
44
|
+
out.add(p);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
for (const [line] of prose(text)) {
|
|
50
|
+
for (const m of line.matchAll(CODE_SPAN)) consider(m[1], false);
|
|
51
|
+
for (const m of line.replace(/`[^`]*`/g, '').matchAll(LINK)) consider(m[1], true);
|
|
52
|
+
}
|
|
53
|
+
return [...out].sort(cmp);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Every claim, before its history is read: the REQ blocks of the spec in spec order, then the `##`
|
|
57
|
+
// sections of the runbook in runbook order. -> [{id, where, title, files, file, line, kind}]
|
|
58
|
+
function allClaims(root, cfg, index) {
|
|
59
|
+
const known = graphPaths(index);
|
|
60
|
+
const p = kitPaths(root, cfg);
|
|
61
|
+
const out = [];
|
|
62
|
+
const texts = new Map();
|
|
63
|
+
for (const b of reqBlocks(root, cfg)) {
|
|
64
|
+
if (!texts.has(b.file)) texts.set(b.file, lines(readText(path.join(root, b.file))));
|
|
65
|
+
const heading = texts.get(b.file)[b.line - 1] ?? '';
|
|
66
|
+
const title = heading.replace(/^#+\s*/, '').replace(/\s*#*\s*$/, '').slice(b.id.length).trim();
|
|
67
|
+
out.push({ kind: 'spec', id: b.id, where: `${b.file}:${b.line}`, title, files: namedFiles(`${heading}\n${b.block}`, b.file, known), file: b.file, line: b.line });
|
|
68
|
+
}
|
|
69
|
+
if (fs.existsSync(p.runbook)) {
|
|
70
|
+
const rel = posix(path.relative(root, p.runbook));
|
|
71
|
+
const text = readText(p.runbook);
|
|
72
|
+
const all = lines(text);
|
|
73
|
+
const hs = headings(text).filter((h) => h.level <= 2);
|
|
74
|
+
hs.forEach((h, i) => {
|
|
75
|
+
if (h.level !== 2) return;
|
|
76
|
+
const end = hs[i + 1] ? hs[i + 1].line - 1 : all.length;
|
|
77
|
+
const body = all.slice(h.line - 1, end).join('\n');
|
|
78
|
+
out.push({ kind: 'runbook', id: `runbook#${slug(h.text)}`, where: `${rel}:${h.line}`, title: h.text, files: namedFiles(body, rel, known), file: rel, line: h.line });
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// The claims an area names (D-58): none for all; `runbook`; a REQ prefix such as `REQ-2`, or one REQ
|
|
85
|
+
// ID; a spec file's stem or path; else a path, naming the claims one of whose files is it, is under
|
|
86
|
+
// it or holds it.
|
|
87
|
+
function inArea(claims, area) {
|
|
88
|
+
if (area === undefined || area === null || String(area).trim() === '') return claims;
|
|
89
|
+
const a = String(area).trim();
|
|
90
|
+
if (a.toLowerCase() === 'runbook') return claims.filter((c) => c.kind === 'runbook');
|
|
91
|
+
if (/^REQ-\d+$/i.test(a)) return claims.filter((c) => c.kind === 'spec' && c.id.toUpperCase().startsWith(`${a.toUpperCase()}.`));
|
|
92
|
+
if (/^REQ-\d+\.\d+$/i.test(a)) return claims.filter((c) => c.kind === 'spec' && c.id.toUpperCase() === a.toUpperCase());
|
|
93
|
+
const p = normalisePath(a);
|
|
94
|
+
const ofDoc = claims.filter((c) => c.file === p || (c.kind === 'spec' && path.posix.basename(c.file, '.md') === p));
|
|
95
|
+
if (ofDoc.length > 0) return ofDoc;
|
|
96
|
+
return claims.filter((c) => c.files.some((f) => f === p || f.startsWith(`${p}/`) || p.startsWith(`${f}/`)));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// The commit that last changed each line of a file, read with one `git blame --porcelain`:
|
|
100
|
+
// line number -> {sha, date}; a line not yet committed has no entry.
|
|
101
|
+
function blame(root, file) {
|
|
102
|
+
const out = new Map();
|
|
103
|
+
let text;
|
|
104
|
+
try {
|
|
105
|
+
text = git(root, ['blame', '--porcelain', '--', file]);
|
|
106
|
+
} catch (e) {
|
|
107
|
+
if (e instanceof GitError) return out;
|
|
108
|
+
throw e;
|
|
109
|
+
}
|
|
110
|
+
const dates = new Map();
|
|
111
|
+
let sha = null;
|
|
112
|
+
for (const l of text.split('\n')) {
|
|
113
|
+
const head = /^([0-9a-f]{40,64}) \d+ (\d+)(?: \d+)?$/.exec(l);
|
|
114
|
+
if (head) {
|
|
115
|
+
sha = head[1];
|
|
116
|
+
if (!/^0+$/.test(sha)) out.set(Number(head[2]), { sha, date: null });
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
const time = /^committer-time (\d+)$/.exec(l);
|
|
120
|
+
if (time && sha) dates.set(sha, new Date(Number(time[1]) * 1000).toISOString());
|
|
121
|
+
}
|
|
122
|
+
for (const entry of out.values()) entry.date = dates.get(entry.sha) ?? null;
|
|
123
|
+
return out;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// The commits that touched one of `files` and are not reachable from `since`, newest first by
|
|
127
|
+
// ancestry (`git log --topo-order`): a commit's dates are printed and never compared.
|
|
128
|
+
function candidates(root, since, files) {
|
|
129
|
+
if (!since || files.length === 0) return [];
|
|
130
|
+
const text = git(root, ['log', '--topo-order', '--format=%H%x09%cI%x09%s', `${since}..HEAD`, '--', ...files]);
|
|
131
|
+
return text === ''
|
|
132
|
+
? []
|
|
133
|
+
: text.split('\n').map((l) => {
|
|
134
|
+
const [sha, date, ...subject] = l.split('\t');
|
|
135
|
+
return { sha, date, subject: subject.join('\t') };
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// The claims of `area` (D-58), in spec order then runbook order:
|
|
140
|
+
// [{id, where, title, files, since: {sha, date} | null, candidates: [{sha, date, subject}]}].
|
|
141
|
+
// `since` is the commit that last changed the claim's heading line; a candidate touched one of the
|
|
142
|
+
// claim's files and descends from it. An area that names no claim is a usage error.
|
|
143
|
+
export function auditClaims(root, cfg, index, area) {
|
|
144
|
+
const picked = inArea(allClaims(root, cfg, index), area);
|
|
145
|
+
if (picked.length === 0) {
|
|
146
|
+
throw new GraphError(2, area === undefined || area === null || String(area).trim() === '' ? 'the spec and the runbook hold no claim' : `${area} names no claim`);
|
|
147
|
+
}
|
|
148
|
+
const blames = new Map();
|
|
149
|
+
const logs = new Map();
|
|
150
|
+
return picked.map((c) => {
|
|
151
|
+
if (!blames.has(c.file)) blames.set(c.file, blame(root, c.file));
|
|
152
|
+
const since = blames.get(c.file).get(c.line) ?? null;
|
|
153
|
+
const key = `${since?.sha ?? ''}\n${c.files.join('\n')}`;
|
|
154
|
+
if (!logs.has(key)) logs.set(key, candidates(root, since?.sha, c.files));
|
|
155
|
+
return { id: c.id, where: c.where, title: c.title, files: c.files, since, candidates: logs.get(key) };
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// What `/slicekit audit` hands the auditor. The report path is reserved after the claims are read,
|
|
160
|
+
// so an area that names no claim leaves no empty report behind.
|
|
161
|
+
export function auditContext(root, cfg, area) {
|
|
162
|
+
const index = indexGraph(loadGraph(root, cfg));
|
|
163
|
+
const claims = auditClaims(root, cfg, index, area);
|
|
164
|
+
return { report: nextReport(root, cfg, 'audit'), claims };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const bare = (cell) => cell.trim().replace(/^`+|`+$/g, '').trim();
|
|
168
|
+
|
|
169
|
+
// What is wrong with an audit report, one `<rel>:<line> <message>` per problem (REQ-2.14): a table
|
|
170
|
+
// under `## Claims` with the six columns; each row a claim of the area, written where the claim is,
|
|
171
|
+
// with a status of the three; a drifted row naming one of its claim's candidate commits; an
|
|
172
|
+
// unverifiable row saying why; a holds or drifted row naming where the evidence was found; no claim
|
|
173
|
+
// with two rows; the two closing lines. With `everyClaim`, each of `claims` has a row: `claims` are
|
|
174
|
+
// the claims of the area, as `auditClaims` reads them from the spec and the runbook.
|
|
175
|
+
export function auditReportProblems(root, rel, text, claims, { everyClaim = false } = {}) {
|
|
176
|
+
const out = [];
|
|
177
|
+
const section = reportSections(text).get('claims');
|
|
178
|
+
const table = sectionTable(text, 'Claims');
|
|
179
|
+
if (!section) out.push(`${rel}:1 no "## Claims" section`);
|
|
180
|
+
else if (!table) out.push(`${rel}:${section.line} the "## Claims" section holds no table`);
|
|
181
|
+
else if (table.header.cells.length !== COLUMNS.length || table.header.cells.some((c, i) => c.toLowerCase() !== COLUMNS[i].toLowerCase())) {
|
|
182
|
+
out.push(`${rel}:${table.header.line} the Claims table's columns are not ${COLUMNS.join(', ')}`);
|
|
183
|
+
} else if (table.rows.length === 0) out.push(`${rel}:${table.header.line} the Claims table holds no row`);
|
|
184
|
+
else {
|
|
185
|
+
const byId = new Map(claims.map((c) => [c.id, c]));
|
|
186
|
+
const rowOf = new Map();
|
|
187
|
+
for (const { cells, line } of table.rows) {
|
|
188
|
+
const at = `${rel}:${line}`;
|
|
189
|
+
if (cells.length !== COLUMNS.length) {
|
|
190
|
+
out.push(`${at} the row holds ${cells.length} cells, not ${COLUMNS.length}`);
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
const [id, where, status, commit] = [cells[0], cells[1], cells[4], cells[5]].map(bare);
|
|
194
|
+
const evidence = cells[2].trim();
|
|
195
|
+
const foundAt = cells[3].trim();
|
|
196
|
+
const claim = byId.get(id);
|
|
197
|
+
if (!claim) {
|
|
198
|
+
out.push(`${at} ${id === '' ? 'the row names no claim' : `${id} is not a claim of this audit`}`);
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
if (rowOf.has(id)) {
|
|
202
|
+
out.push(`${at} ${id} has a second row; its first is on line ${rowOf.get(id)}`);
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
rowOf.set(id, line);
|
|
206
|
+
if (where !== claim.where) out.push(`${at} ${id} is written at ${claim.where}, not ${where === '' ? 'nowhere' : where}`);
|
|
207
|
+
if (!STATUSES.includes(status)) {
|
|
208
|
+
out.push(`${at} the status of ${id} is "${status}", not ${STATUSES.slice(0, 2).join(', ')} or ${STATUSES[2]}`);
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
if (status === 'drifted') {
|
|
212
|
+
const lower = commit.toLowerCase();
|
|
213
|
+
const named = /^[0-9a-f]{7,64}$/.test(lower) && claim.candidates.some((c) => c.sha.toLowerCase().startsWith(lower));
|
|
214
|
+
if (commit === '') out.push(`${at} drifted ${id} names no commit; name one that touched its files after it was written`);
|
|
215
|
+
else if (!named) out.push(`${at} drifted ${id} names ${commit}, which is not a commit that touched its files after it was written`);
|
|
216
|
+
}
|
|
217
|
+
if (status === 'unverifiable') {
|
|
218
|
+
if (evidence === '') out.push(`${at} unverifiable ${id} does not say why in Evidence`);
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
const cited = /^([^\s`]+):(\d+)$/.exec(bare(foundAt));
|
|
222
|
+
if (cited) {
|
|
223
|
+
if (!lineExists(root, normalisePath(cited[1]), Number(cited[2]))) out.push(`${at} ${cited[1]}:${cited[2]} is not a line of a file in the repository`);
|
|
224
|
+
} else if (!/`[^`]+`/.test(foundAt)) {
|
|
225
|
+
out.push(`${at} Found at of ${id} is neither a <file>:<line> nor a command in backticks`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (everyClaim) {
|
|
229
|
+
for (const c of claims) if (!rowOf.has(c.id)) out.push(`${rel}:${table.header.line} no row for ${c.id}`);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return [...out, ...footerProblems(rel, text)];
|
|
233
|
+
}
|
package/src/build/pr.js
CHANGED
|
@@ -12,6 +12,7 @@ import { stateFileName } from '../ids.js';
|
|
|
12
12
|
import { kitPaths } from '../paths.js';
|
|
13
13
|
import { openOrUpdate } from '../pr.js';
|
|
14
14
|
import { PrBodyError, getSection, renderPrBody } from '../prbody.js';
|
|
15
|
+
import { mergeBase, runbookRel, runbookSections, runbookVerdict } from '../runbook.js';
|
|
15
16
|
import { readSlice, recordHead } from '../state/index.js';
|
|
16
17
|
import { headCovers } from '../state/covers.js';
|
|
17
18
|
import { posixRel, runCommand, sliceIn, unusableRun } from './red.js';
|
|
@@ -23,7 +24,7 @@ import { BuildError, findWorktree } from './worktree.js';
|
|
|
23
24
|
|
|
24
25
|
const FROM = ['building', 'draft-pr', 'in-review', 'changes-requested', 'ci-green', 'ready'];
|
|
25
26
|
const NOTES_SHAPE = 'notes need tryIt (three steps) and skipped';
|
|
26
|
-
const
|
|
27
|
+
const NO_CHANGE_LINE = 'no operational change: the slice touches no configuration, infrastructure, migration, alert, queue or integration';
|
|
27
28
|
const RUNBOOK_REFUSAL = 'the slice touches operations; change the runbook or give notes.runbook "no operational change: <reason>"';
|
|
28
29
|
const COMMANDS = ['test', 'lint', 'build'];
|
|
29
30
|
// The sections the notes give: with no notes, the live body must hold each of them.
|
|
@@ -97,20 +98,6 @@ function pendingDraftPr(wt, stateRel, current) {
|
|
|
97
98
|
);
|
|
98
99
|
}
|
|
99
100
|
|
|
100
|
-
// Whether the branch changed the runbook since it left the default branch.
|
|
101
|
-
function runbookChanged(wt, cfg, rel) {
|
|
102
|
-
const origin = `refs/remotes/origin/${cfg.defaultBranch}`;
|
|
103
|
-
let from;
|
|
104
|
-
try {
|
|
105
|
-
git(wt, ['rev-parse', '--verify', '--quiet', origin]);
|
|
106
|
-
from = origin;
|
|
107
|
-
} catch {
|
|
108
|
-
from = `refs/heads/${cfg.defaultBranch}`;
|
|
109
|
-
}
|
|
110
|
-
const base = git(wt, ['merge-base', 'HEAD', from]);
|
|
111
|
-
return git(wt, ['--literal-pathspecs', 'diff', '--name-only', base, 'HEAD', '--', rel]) !== '';
|
|
112
|
-
}
|
|
113
|
-
|
|
114
101
|
// The test run's pass and fail totals, from the last line naming each; the summary line when the
|
|
115
102
|
// output does not name both on separate lines.
|
|
116
103
|
function proofSummary(run) {
|
|
@@ -204,10 +191,14 @@ export function openSlicePr(root, cfg, host, id, { notes, worktree, env = proces
|
|
|
204
191
|
if (missing) throw new BuildError(`the pull request body has no "${missing}"; pass --notes <file> to supply it`);
|
|
205
192
|
}
|
|
206
193
|
|
|
207
|
-
|
|
208
|
-
|
|
194
|
+
// One judgement with the `runbook` check rule and `slicekit release` (REQ-3.7, D-55): a `##` section
|
|
195
|
+
// of the runbook changed since the branch left the default branch, or a Runbook line that begins
|
|
196
|
+
// "no operational change". A change above the first `##` heading, or of trailing whitespace, is none.
|
|
197
|
+
const rel = runbookRel(wt, cfg);
|
|
198
|
+
const sections = runbookSections(wt, rel, mergeBase(wt, cfg), 'HEAD');
|
|
199
|
+
const changed = sections.length > 0;
|
|
209
200
|
const runbookNote = changed ? null : (given?.runbook ?? (current.pr ? kept('Runbook', null) : null));
|
|
210
|
-
if (slice.runbook
|
|
201
|
+
if (runbookVerdict({ flag: slice.runbook, sections, body: runbookNote === null ? null : `**Runbook:** ${runbookNote}`, rel }) !== null) {
|
|
211
202
|
throw new BuildError(RUNBOOK_REFUSAL);
|
|
212
203
|
}
|
|
213
204
|
const { summary, command: testCommand, local } = runCommands(root, wt, cfg, env);
|
|
@@ -229,7 +220,7 @@ export function openSlicePr(root, cfg, host, id, { notes, worktree, env = proces
|
|
|
229
220
|
'Done when': slice.doneWhen.map((c, i) => `${i + 1}. ${c}`).join(' '),
|
|
230
221
|
Proof: `\`${testCommand}\` → ${summary} · CI: pending`,
|
|
231
222
|
Decisions: given?.decisions === undefined ? kept('Decisions', 'none') : given.decisions.join('; ') || 'none',
|
|
232
|
-
Runbook: changed ? `changed: ${
|
|
223
|
+
Runbook: changed ? `changed: ${rel} (${sections.join(', ')})` : (runbookNote ?? NO_CHANGE_LINE),
|
|
233
224
|
'Infra / migration': noted('infra', 'Infra / migration', 'none'),
|
|
234
225
|
'Try it': given ? given.tryIt.map((s, i) => `${i + 1}. ${s.trim()}`).join(' ') : kept('Try it', ''),
|
|
235
226
|
'Reviewer blocked': kept('Reviewer blocked', 'nothing'),
|
package/src/check/ci-event.js
CHANGED
|
@@ -77,8 +77,9 @@ export function isBotAuthor(cfg, pr) {
|
|
|
77
77
|
return typeof pr?.author === 'string' && bots.includes(pr.author);
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
// A `[spec] `, `[plan] ` or `[
|
|
81
|
-
|
|
80
|
+
// A `[spec] `, `[plan] `, `[sync] ` or `[release] ` title: a spec, plan, state sync or release record
|
|
81
|
+
// pull request (D-54), not a slice's.
|
|
82
|
+
export const EXEMPT_TITLE = /^\[(spec|plan|sync|release)\] /;
|
|
82
83
|
|
|
83
84
|
// A `[sync] ` title: the state sync pull request, which belongs on a `sync/<short sha>` branch only.
|
|
84
85
|
export const SYNC_TITLE = /^\[sync\] /;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// runbook (03 REQ-3.7, D-21, D-55): on a slice branch whose slice file says `runbook: yes` and whose
|
|
2
|
+
// state is draft-pr or later and not merged or released, the branch changes a `##` section of the
|
|
3
|
+
// runbook or the pull request body's Runbook line begins "no operational change". The body comes
|
|
4
|
+
// from the CI event or `SLICEKIT_PR_BODY`; with neither the rule does not judge, because `build pr`
|
|
5
|
+
// already refuses to open a pull request without one of the two (D-55). One finding, at the slice
|
|
6
|
+
// file's `runbook:` line.
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { readText } from '../../fs.js';
|
|
9
|
+
import { sliceFromBranch } from '../../ids.js';
|
|
10
|
+
import { lines } from '../../markdown.js';
|
|
11
|
+
import { mergeBase, runbookRel, runbookSections, runbookVerdict } from '../../runbook.js';
|
|
12
|
+
import { findSliceFiles, parseSliceFile } from '../../slicefile.js';
|
|
13
|
+
import { readSlice } from '../../state/index.js';
|
|
14
|
+
import { branchTip, readCiEvent } from '../ci-event.js';
|
|
15
|
+
|
|
16
|
+
export const id = 'runbook';
|
|
17
|
+
export const blocking = true;
|
|
18
|
+
export const ratchet = false;
|
|
19
|
+
|
|
20
|
+
const JUDGED = ['draft-pr', 'in-review', 'changes-requested', 'ci-green', 'ready'];
|
|
21
|
+
|
|
22
|
+
// The slice file of `slice`: {rel, text, flag}, or null when no readable slice file names it.
|
|
23
|
+
function sliceFileOf(root, cfg, slice) {
|
|
24
|
+
for (const f of findSliceFiles(root, cfg)) {
|
|
25
|
+
let text;
|
|
26
|
+
let parsed;
|
|
27
|
+
try {
|
|
28
|
+
text = readText(f.path);
|
|
29
|
+
parsed = parseSliceFile(text, f.path);
|
|
30
|
+
} catch {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (parsed.id === slice) return { rel: path.relative(root, f.path).split(path.sep).join('/'), text, flag: parsed.runbook };
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function run(ctx) {
|
|
39
|
+
const { root, cfg } = ctx;
|
|
40
|
+
const at = branchTip(ctx);
|
|
41
|
+
if (!at) return [];
|
|
42
|
+
const slice = sliceFromBranch(at.branch);
|
|
43
|
+
if (!slice) return [];
|
|
44
|
+
const file = sliceFileOf(root, cfg, slice);
|
|
45
|
+
if (!file || !file.flag) return [];
|
|
46
|
+
if (!JUDGED.includes(readSlice(root, cfg, slice).state)) return [];
|
|
47
|
+
const env = ctx.env ?? {};
|
|
48
|
+
const hasEvent = typeof env.GITHUB_EVENT_PATH === 'string' && env.GITHUB_EVENT_PATH !== '';
|
|
49
|
+
if (!hasEvent && env.SLICEKIT_PR_BODY === undefined) return [];
|
|
50
|
+
const pr = readCiEvent(env, { cfg, root });
|
|
51
|
+
if (!pr) return [];
|
|
52
|
+
const rel = runbookRel(root, cfg);
|
|
53
|
+
const sections = runbookSections(root, rel, mergeBase(root, cfg, at.tip), at.tip);
|
|
54
|
+
const message = runbookVerdict({ flag: true, sections, body: pr.body, rel });
|
|
55
|
+
if (message === null) return [];
|
|
56
|
+
const line = lines(file.text).findIndex((l) => /^runbook\s*:/.test(l)) + 1;
|
|
57
|
+
return [{ file: file.rel, line, message }];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export default { id, blocking, ratchet, run };
|
package/src/cli/audit.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// slicekit audit context [area] [--all] [--json] and slicekit audit check <report> [area] [--every-claim]
|
|
2
|
+
// (REQ-2.14, D-58):
|
|
3
|
+
// the claims `/slicekit audit` hands the auditor, each with the files it names and its candidate
|
|
4
|
+
// commits, and the check of the auditor's report against the claims of the same area, which the
|
|
5
|
+
// check reads from the spec and the runbook as the context does. With --every-claim a claim of the
|
|
6
|
+
// area that has no row is a problem. Neither changes a tracked file.
|
|
7
|
+
import { parseArgs } from 'node:util';
|
|
8
|
+
import { auditClaims, auditContext, auditReportProblems } from '../audit.js';
|
|
9
|
+
import { ConfigError, loadConfig } from '../config.js';
|
|
10
|
+
import { GitError } from '../git.js';
|
|
11
|
+
import { indexGraph, loadGraph } from '../graph/load.js';
|
|
12
|
+
import { GraphError } from '../graph/schema.js';
|
|
13
|
+
import { readReport } from '../reports.js';
|
|
14
|
+
|
|
15
|
+
const USAGE = 'audit context [area] [--all] [--json]\n audit check <report> [area] [--every-claim]';
|
|
16
|
+
const SUBCOMMANDS = ['context', 'check'];
|
|
17
|
+
|
|
18
|
+
const usage = (io, why) => {
|
|
19
|
+
io.stderr(`${why ? `slicekit: ${why}\n` : ''}usage: slicekit ${USAGE}\n`);
|
|
20
|
+
return 2;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// `(none)` is what a procedure prints for no argument.
|
|
24
|
+
const areaOf = (arg) => (arg === undefined || arg === '(none)' ? undefined : arg);
|
|
25
|
+
|
|
26
|
+
// The candidate commits printed for one claim without --all (P5/S-23 review H-7).
|
|
27
|
+
export const SHOWN = 10;
|
|
28
|
+
|
|
29
|
+
// The narrowest area that names `claim`: its REQ ID, or `runbook` for a runbook section.
|
|
30
|
+
const areaFor = (claim) => (claim.id.startsWith('runbook#') ? 'runbook' : claim.id);
|
|
31
|
+
|
|
32
|
+
// At most SHOWN candidates per claim, the newest, then one line counting the rest and naming the
|
|
33
|
+
// command that prints them; with `all`, each one. --json and the check hold every candidate.
|
|
34
|
+
function contextLines(c, { all = false } = {}) {
|
|
35
|
+
const out = [c.report];
|
|
36
|
+
for (const claim of c.claims) {
|
|
37
|
+
out.push(`${claim.id} ${claim.where} ${claim.title}`.trimEnd());
|
|
38
|
+
out.push(` files: ${claim.files.join(', ')}`.trimEnd());
|
|
39
|
+
const shown = all ? claim.candidates : claim.candidates.slice(0, SHOWN);
|
|
40
|
+
for (const k of shown) out.push(` candidate ${k.sha.slice(0, 7)} ${k.date} ${k.subject}`.trimEnd());
|
|
41
|
+
const more = claim.candidates.length - shown.length;
|
|
42
|
+
if (more > 0) out.push(` ${more} more candidate${more === 1 ? '' : 's'}, each older: audit context ${areaFor(claim)} --all`);
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function run(args, io) {
|
|
48
|
+
let values;
|
|
49
|
+
let positionals;
|
|
50
|
+
try {
|
|
51
|
+
({ values, positionals } = parseArgs({ args, options: { json: { type: 'boolean' }, all: { type: 'boolean' }, 'every-claim': { type: 'boolean' } }, allowPositionals: true }));
|
|
52
|
+
} catch (e) {
|
|
53
|
+
return usage(io, e.message);
|
|
54
|
+
}
|
|
55
|
+
const [sub, ...rest] = positionals;
|
|
56
|
+
if (!SUBCOMMANDS.includes(sub)) return usage(io, sub === undefined ? 'missing subcommand' : `unknown subcommand '${sub}'`);
|
|
57
|
+
const json = Boolean(values.json);
|
|
58
|
+
const refuse = (message, code) => {
|
|
59
|
+
io.stderr(`slicekit: ${message}\n`);
|
|
60
|
+
if (json) io.stdout(`${JSON.stringify({ error: message }, null, 2)}\n`);
|
|
61
|
+
return code;
|
|
62
|
+
};
|
|
63
|
+
const root = io.cwd;
|
|
64
|
+
try {
|
|
65
|
+
const cfg = loadConfig(root);
|
|
66
|
+
if (sub === 'context') {
|
|
67
|
+
if (rest.length > 1) return usage(io, `unexpected argument ${rest[1]}`);
|
|
68
|
+
const context = auditContext(root, cfg, areaOf(rest[0]));
|
|
69
|
+
io.stdout(json ? `${JSON.stringify(context, null, 2)}\n` : contextLines(context, { all: Boolean(values.all) }).map((l) => `${l}\n`).join(''));
|
|
70
|
+
return 0;
|
|
71
|
+
}
|
|
72
|
+
if (rest.length === 0) return usage(io, 'missing report');
|
|
73
|
+
if (rest.length > 2) return usage(io, `unexpected argument ${rest[2]}`);
|
|
74
|
+
const { rel, text } = readReport(root, rest[0]);
|
|
75
|
+
if (text === null) return refuse(`${rel} is not a file`, 2);
|
|
76
|
+
const claims = auditClaims(root, cfg, indexGraph(loadGraph(root, cfg)), areaOf(rest[1]));
|
|
77
|
+
const problems = auditReportProblems(root, rel, text, claims, { everyClaim: Boolean(values['every-claim']) });
|
|
78
|
+
if (json) io.stdout(`${JSON.stringify({ report: rel, problems }, null, 2)}\n`);
|
|
79
|
+
else io.stdout(problems.length > 0 ? problems.map((p) => `${p}\n`).join('') : `ok ${rel}\n`);
|
|
80
|
+
return problems.length > 0 ? 1 : 0;
|
|
81
|
+
} catch (e) {
|
|
82
|
+
if (e instanceof GraphError) return refuse(e.message, e.code);
|
|
83
|
+
if (e instanceof ConfigError) return refuse(e.message, 2);
|
|
84
|
+
if (e instanceof GitError) return refuse(e.message, 1);
|
|
85
|
+
throw e;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export const auditCommand = { run, usage: USAGE, subcommands: SUBCOMMANDS };
|
package/src/cli/branch.js
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
// `slicekit branch <spec|gaps|plan> <slug>` (REQ-2.3..REQ-2.5): puts the session on the
|
|
2
|
-
// of a document pull request, cut from the default branch as origin has it. The command owns git
|
|
1
|
+
// `slicekit branch <spec|gaps|plan|release> <slug>` (REQ-2.3..REQ-2.5, REQ-2.9, D-54): puts the session on the
|
|
2
|
+
// work branch of a document pull request, cut from the default branch as origin has it. The command owns git
|
|
3
3
|
// (D-19). A change to a tracked file (staged or not) is refused, so no edit is carried across;
|
|
4
4
|
// untracked files go along as git carries them, so a source dropped in for intake survives.
|
|
5
|
+
// A `release/<slug>` branch that exists away from the default branch's tip is moved there or refused;
|
|
6
|
+
// one that holds its own commits is refused with its open pull request, read through the git host
|
|
7
|
+
// (C-1 pullRequestForBranch), or with the next free slug when the host says none is open.
|
|
8
|
+
import { getHost } from '../adapters/githost/index.js';
|
|
9
|
+
import { GitHostError } from '../adapters/githost/errors.js';
|
|
5
10
|
import { ConfigError, loadConfig } from '../config.js';
|
|
6
11
|
import { GitError, assertRef, currentBranch, git, hasRemote, switchBranch } from '../git.js';
|
|
7
12
|
|
|
8
|
-
const KINDS = ['spec', 'gaps', 'plan'];
|
|
13
|
+
const KINDS = ['spec', 'gaps', 'plan', 'release'];
|
|
9
14
|
const SLUG = /^[a-z0-9][a-z0-9-]{0,59}$/;
|
|
10
|
-
const USAGE = 'branch <spec|gaps|plan> <slug>';
|
|
15
|
+
const USAGE = 'branch <spec|gaps|plan|release> <slug>';
|
|
11
16
|
|
|
12
17
|
const resolves = (root, ref) => {
|
|
13
18
|
try {
|
|
@@ -34,19 +39,24 @@ async function run(args, io) {
|
|
|
34
39
|
try {
|
|
35
40
|
const cfg = loadConfig(root);
|
|
36
41
|
const base = assertRef(cfg.defaultBranch);
|
|
37
|
-
|
|
42
|
+
const onTarget = currentBranch(root) === target;
|
|
43
|
+
if (onTarget && kind !== 'release') {
|
|
38
44
|
io.stdout(`${target}\n`);
|
|
39
45
|
return 0;
|
|
40
46
|
}
|
|
41
|
-
|
|
47
|
+
const dirty = git(root, ['status', '--porcelain', '--untracked-files=no']) !== '';
|
|
48
|
+
if (dirty && !onTarget) {
|
|
42
49
|
io.stderr('slicekit: commit or stash your changes first\n');
|
|
43
50
|
return 1;
|
|
44
51
|
}
|
|
45
52
|
// The full ref: a tag named like the default branch must not be what the branch is cut from.
|
|
46
53
|
let from = `refs/heads/${base}`;
|
|
47
|
-
|
|
54
|
+
const origin = hasRemote(root, 'origin');
|
|
55
|
+
let fetched = false;
|
|
56
|
+
if (origin) {
|
|
48
57
|
try {
|
|
49
58
|
git(root, ['fetch', '-q', 'origin', base]);
|
|
59
|
+
fetched = true;
|
|
50
60
|
if (resolves(root, `refs/remotes/origin/${base}`)) from = `refs/remotes/origin/${base}`;
|
|
51
61
|
// The work branch too, so one pushed from another clone is found; origin not having it is
|
|
52
62
|
// the usual case for a new branch, not an error.
|
|
@@ -65,6 +75,55 @@ async function run(args, io) {
|
|
|
65
75
|
// A work branch someone already pushed is taken up where it stands, tracking origin's copy;
|
|
66
76
|
// only a new one is cut from the default branch.
|
|
67
77
|
const remote = `refs/remotes/origin/${target}`;
|
|
78
|
+
if (kind === 'release') {
|
|
79
|
+
// `slicekit release` runs only at the default branch's tip. A release branch that stands
|
|
80
|
+
// elsewhere, here or on origin, is moved to the tip when it holds no commit the default
|
|
81
|
+
// branch lacks; one that does is refused: with its open pull request when the host has one,
|
|
82
|
+
// else with the next free slug.
|
|
83
|
+
const tip = git(root, ['rev-parse', '--verify', `${from}^{commit}`]);
|
|
84
|
+
const copies = [`refs/heads/${target}`, remote].filter((ref) => resolves(root, ref));
|
|
85
|
+
const stale = copies.filter((ref) => git(root, ['rev-parse', '--verify', `${ref}^{commit}`]) !== tip);
|
|
86
|
+
if (stale.some((ref) => git(root, ['rev-list', '--count', `${from}..${ref}`]) !== '0')) {
|
|
87
|
+
// The branch may be that of the phase's open release pull request; a second slug would then
|
|
88
|
+
// open a second one beside it. A pull request needs its branch on origin, so the host is
|
|
89
|
+
// asked unless a fetch that worked found no such branch there.
|
|
90
|
+
if (origin && (!fetched || resolves(root, remote))) {
|
|
91
|
+
let open;
|
|
92
|
+
try {
|
|
93
|
+
open = getHost(cfg, io.env ?? process.env, root).pullRequestForBranch(target, { base });
|
|
94
|
+
} catch (e) {
|
|
95
|
+
if (!(e instanceof GitHostError)) throw e;
|
|
96
|
+
io.stderr(`slicekit: ${target} holds commits ${base} lacks, and the git host could not be asked for its open pull request (${e.message}); no branch is cut
|
|
97
|
+
`);
|
|
98
|
+
return 1;
|
|
99
|
+
}
|
|
100
|
+
if (open) {
|
|
101
|
+
io.stderr(`slicekit: ${target} is the branch of open pull request #${open.number} (${open.url}); the phase's release is already open
|
|
102
|
+
`);
|
|
103
|
+
return 1;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
let n = 2;
|
|
107
|
+
while (resolves(root, `refs/heads/${target}-${n}`) || resolves(root, `refs/remotes/origin/${target}-${n}`)) n += 1;
|
|
108
|
+
io.stderr(`slicekit: ${target} holds commits ${base} lacks, so no release runs on it; run slicekit branch release ${slug}-${n}\n`);
|
|
109
|
+
return 1;
|
|
110
|
+
}
|
|
111
|
+
if (onTarget && stale.length === 0) {
|
|
112
|
+
io.stdout(`${target}\n`);
|
|
113
|
+
return 0;
|
|
114
|
+
}
|
|
115
|
+
if (stale.length > 0) {
|
|
116
|
+
if (dirty) {
|
|
117
|
+
io.stderr('slicekit: commit or stash your changes first\n');
|
|
118
|
+
return 1;
|
|
119
|
+
}
|
|
120
|
+
// -C makes the branch, or moves it, to stand at the tip; it held no commit of its own.
|
|
121
|
+
git(root, ['switch', '-q', '--no-track', '-C', target, from]);
|
|
122
|
+
io.stderr(`slicekit: ${target} was behind ${base}; moved to the tip of ${base}\n`);
|
|
123
|
+
io.stdout(`${target}\n`);
|
|
124
|
+
return 0;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
68
127
|
if (resolves(root, `refs/heads/${target}`)) switchBranch(root, target);
|
|
69
128
|
else if (resolves(root, remote)) switchBranch(root, target, { create: true, from: remote, track: true });
|
|
70
129
|
else switchBranch(root, target, { create: true, from });
|
package/src/cli/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
|
+
import { auditCommand } from './audit.js';
|
|
2
3
|
import { baselineCommand } from './baseline.js';
|
|
3
4
|
import { branchCommand } from './branch.js';
|
|
4
5
|
import { buildCommand } from './build.js';
|
|
@@ -14,9 +15,11 @@ import { intakeCommand } from './intake.js';
|
|
|
14
15
|
import { prCommand } from './pr.js';
|
|
15
16
|
import { instructionsCommand } from './instructions.js';
|
|
16
17
|
import { journalCommand } from './journal.js';
|
|
18
|
+
import { opsCommand } from './ops.js';
|
|
17
19
|
import { planCommand } from './plan.js';
|
|
18
20
|
import { hookCommand } from './hook.js';
|
|
19
21
|
import { readyLabelCommand } from './ready-label.js';
|
|
22
|
+
import { releaseCommand } from './release.js';
|
|
20
23
|
import { reviewCommand } from './review.js';
|
|
21
24
|
import { specCommand } from './spec.js';
|
|
22
25
|
import { stageCommand } from './stage.js';
|
|
@@ -32,6 +35,7 @@ import { workflowCommand } from './workflow.js';
|
|
|
32
35
|
|
|
33
36
|
// Later tasks add entries: { [name]: { run(args, io) -> Promise<number>, usage: string, subcommands?: string[] } }
|
|
34
37
|
export const COMMANDS = {
|
|
38
|
+
audit: auditCommand,
|
|
35
39
|
baseline: baselineCommand,
|
|
36
40
|
branch: branchCommand,
|
|
37
41
|
build: buildCommand,
|
|
@@ -47,9 +51,11 @@ export const COMMANDS = {
|
|
|
47
51
|
instructions: instructionsCommand,
|
|
48
52
|
hook: hookCommand,
|
|
49
53
|
journal: journalCommand,
|
|
54
|
+
ops: opsCommand,
|
|
50
55
|
plan: planCommand,
|
|
51
56
|
pr: prCommand,
|
|
52
57
|
'ready-label': readyLabelCommand,
|
|
58
|
+
release: releaseCommand,
|
|
53
59
|
review: reviewCommand,
|
|
54
60
|
spec: specCommand,
|
|
55
61
|
shipped: shippedCommand,
|