slicekit 0.0.1 → 0.1.2
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/LICENSE +21 -0
- package/README.md +252 -3
- package/bin/slicekit.js +4 -0
- package/package.json +21 -4
- package/src/adapters/githost/errors.js +17 -0
- package/src/adapters/githost/fixture.js +136 -0
- package/src/adapters/githost/gh-json.js +99 -0
- package/src/adapters/githost/github.js +127 -0
- package/src/adapters/githost/index.js +12 -0
- package/src/adapters/tracker/errors.js +53 -0
- package/src/adapters/tracker/fixture.js +71 -0
- package/src/adapters/tracker/github-issues.js +65 -0
- package/src/adapters/tracker/index.js +14 -0
- package/src/baseline.js +109 -0
- package/src/build/commit.js +201 -0
- package/src/build/decline.js +81 -0
- package/src/build/finish.js +155 -0
- package/src/build/order.js +158 -0
- package/src/build/overlap.js +173 -0
- package/src/build/parallel.js +50 -0
- package/src/build/pr.js +263 -0
- package/src/build/record.js +28 -0
- package/src/build/red.js +207 -0
- package/src/build/review.js +123 -0
- package/src/build/sync.js +47 -0
- package/src/build/verdict.js +410 -0
- package/src/build/worktree.js +169 -0
- package/src/check/ci-event.js +52 -0
- package/src/check/context.js +129 -0
- package/src/check/index.js +140 -0
- package/src/check/rules/.gitkeep +0 -0
- package/src/check/rules/ci-body.js +22 -0
- package/src/check/rules/ci-title.js +43 -0
- package/src/check/rules/citations.js +224 -0
- package/src/check/rules/commands.js +146 -0
- package/src/check/rules/coverage.js +248 -0
- package/src/check/rules/hooks.js +58 -0
- package/src/check/rules/journal.js +17 -0
- package/src/check/rules/layout.js +56 -0
- package/src/check/rules/managed.js +31 -0
- package/src/check/rules/queue.js +106 -0
- package/src/check/rules/references.js +249 -0
- package/src/check/rules/style.js +177 -0
- package/src/check/rules/trailer.js +67 -0
- package/src/cli/baseline.js +66 -0
- package/src/cli/branch.js +86 -0
- package/src/cli/build.js +239 -0
- package/src/cli/check.js +40 -0
- package/src/cli/decide.js +59 -0
- package/src/cli/gaps.js +73 -0
- package/src/cli/hook.js +28 -0
- package/src/cli/index.js +83 -0
- package/src/cli/init.js +96 -0
- package/src/cli/instructions.js +63 -0
- package/src/cli/intake.js +107 -0
- package/src/cli/journal.js +82 -0
- package/src/cli/plan.js +29 -0
- package/src/cli/pr.js +83 -0
- package/src/cli/process.js +59 -0
- package/src/cli/ready-label.js +119 -0
- package/src/cli/review.js +64 -0
- package/src/cli/spec.js +31 -0
- package/src/cli/stage.js +37 -0
- package/src/cli/state.js +106 -0
- package/src/cli/status.js +108 -0
- package/src/cli/update.js +62 -0
- package/src/cli/workflow.js +48 -0
- package/src/config.js +203 -0
- package/src/decisions.js +289 -0
- package/src/evidence.js +27 -0
- package/src/fs.js +38 -0
- package/src/gaps.js +271 -0
- package/src/git.js +209 -0
- package/src/githooks.js +37 -0
- package/src/hook-cli.js +63 -0
- package/src/hooks/io.js +205 -0
- package/src/hooks/paths.js +82 -0
- package/src/hooks/pre-bash.js +715 -0
- package/src/hooks/pre-compact.js +139 -0
- package/src/hooks/pre-edit.js +170 -0
- package/src/hooks/session-end.js +118 -0
- package/src/hooks/session-start.js +209 -0
- package/src/hooks/shapes.js +103 -0
- package/src/hooks/stop.js +37 -0
- package/src/hooks/subagent-marker.js +88 -0
- package/src/hooks/subagent-start.js +100 -0
- package/src/hooks/subagent-stop.js +106 -0
- package/src/ids.js +43 -0
- package/src/init.js +281 -0
- package/src/instructions.js +235 -0
- package/src/intake.js +409 -0
- package/src/journal.js +326 -0
- package/src/lock.js +35 -0
- package/src/markdown.js +155 -0
- package/src/paths.js +33 -0
- package/src/plan.js +25 -0
- package/src/pr.js +73 -0
- package/src/prbody.js +266 -0
- package/src/ready-label.js +107 -0
- package/src/redact.js +33 -0
- package/src/render/compare.js +23 -0
- package/src/render/entry.js +101 -0
- package/src/render/index.js +27 -0
- package/src/render/managed.js +128 -0
- package/src/render/manifest.js +24 -0
- package/src/render/settings.js +76 -0
- package/src/render/update.js +151 -0
- package/src/slicefile.js +173 -0
- package/src/spec-verify.js +58 -0
- package/src/stage.js +325 -0
- package/src/state/build.js +156 -0
- package/src/state/covers.js +58 -0
- package/src/state/index.js +191 -0
- package/src/state/sync.js +64 -0
- package/src/state/transitions.js +282 -0
- package/src/verdict.js +203 -0
- package/src/which.js +37 -0
- package/src/workflow.js +177 -0
- package/src/worktrees.js +68 -0
- package/templates/claude-code/agents/auditor.md +7 -0
- package/templates/claude-code/agents/designer.md +7 -0
- package/templates/claude-code/agents/developer.md +7 -0
- package/templates/claude-code/agents/fixer.md +7 -0
- package/templates/claude-code/agents/ops.md +7 -0
- package/templates/claude-code/agents/reviewer.md +7 -0
- package/templates/claude-code/commands/slicekit-audit.md +7 -0
- package/templates/claude-code/commands/slicekit-build.md +7 -0
- package/templates/claude-code/commands/slicekit-gaps.md +6 -0
- package/templates/claude-code/commands/slicekit-intake.md +7 -0
- package/templates/claude-code/commands/slicekit-ops.md +7 -0
- package/templates/claude-code/commands/slicekit-plan.md +6 -0
- package/templates/claude-code/commands/slicekit-release.md +7 -0
- package/templates/claude-code/commands/slicekit-review.md +7 -0
- package/templates/claude-code/commands/slicekit-specify.md +6 -0
- package/templates/claude-code/commands/slicekit.md +7 -0
- package/templates/claude-code/hooks.json +90 -0
- package/templates/claude-code/manifest.json +171 -0
- package/templates/claude-code/skills/slicekit-ids/SKILL.md +16 -0
- package/templates/claude-code/skills/slicekit-loop/SKILL.md +23 -0
- package/templates/claude-code/skills/slicekit-writing/SKILL.md +24 -0
- package/templates/docs/BUILD.md +5 -0
- package/templates/docs/decisions.md +15 -0
- package/templates/docs/intake/.keep +0 -0
- package/templates/docs/observability.md +25 -0
- package/templates/docs/phases/.keep +0 -0
- package/templates/docs/platform.md +25 -0
- package/templates/docs/releases/.keep +0 -0
- package/templates/docs/runbook.md +21 -0
- package/templates/docs/spec/00-overview.md +8 -0
- package/templates/docs/spec/contracts/.keep +0 -0
- package/templates/docs/spec/gaps.md +6 -0
- package/templates/docs/spec/stack.md +3 -0
- package/templates/githooks/pre-commit +79 -0
- package/templates/githooks/pre-merge-commit +79 -0
- package/templates/githooks/prepare-commit-msg +110 -0
- package/templates/instructions/agents/auditor.md +33 -0
- package/templates/instructions/agents/designer.md +21 -0
- package/templates/instructions/agents/developer.md +49 -0
- package/templates/instructions/agents/fixer.md +41 -0
- package/templates/instructions/agents/ops.md +21 -0
- package/templates/instructions/agents/reviewer.md +63 -0
- package/templates/instructions/commands/build.md +234 -0
- package/templates/instructions/commands/gaps.md +65 -0
- package/templates/instructions/commands/intake.md +90 -0
- package/templates/instructions/commands/plan.md +60 -0
- package/templates/instructions/commands/review.md +117 -0
- package/templates/instructions/commands/specify.md +49 -0
- package/templates/style.json +11 -0
- package/templates/workflow/pins.json +6 -0
- package/templates/workflow/slicekit.yml +110 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { exists, readText, writeText } from '../fs.js';
|
|
4
|
+
import { commitExists, hasCommits, isRepo, resolveCommit } from '../git.js';
|
|
5
|
+
import { parseSliceAddr, stateFileName } from '../ids.js';
|
|
6
|
+
import { withFileLock } from '../lock.js';
|
|
7
|
+
import { kitPaths } from '../paths.js';
|
|
8
|
+
import { findSliceFiles, parseSliceFile } from '../slicefile.js';
|
|
9
|
+
import { TRANSITIONS, TransitionError, allowedMove, nextStates, validateEvidence } from './transitions.js';
|
|
10
|
+
|
|
11
|
+
export { TransitionError };
|
|
12
|
+
|
|
13
|
+
// Another slicekit holds the slice's lock for longer than the wait budget.
|
|
14
|
+
export class LockError extends Error {
|
|
15
|
+
constructor(id) {
|
|
16
|
+
super(`could not lock ${id}: another slicekit is writing it`);
|
|
17
|
+
this.name = 'LockError';
|
|
18
|
+
this.slice = id;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const LOCK_BUDGET_MS = 5_000;
|
|
23
|
+
|
|
24
|
+
function stateFile(root, cfg, id) {
|
|
25
|
+
if (!parseSliceAddr(id)) throw new Error(`unknown slice ${id}`);
|
|
26
|
+
return path.join(kitPaths(root, cfg).state, stateFileName(id));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// One writer per slice at a time: the read-modify-write in setState and unfreeze runs under it.
|
|
30
|
+
function withLock(root, cfg, id, fn, budgetMs = LOCK_BUDGET_MS) {
|
|
31
|
+
const lock = `${stateFile(root, cfg, id)}.lock`;
|
|
32
|
+
fs.mkdirSync(path.dirname(lock), { recursive: true });
|
|
33
|
+
return withFileLock(lock, fn, { budgetMs, busy: () => new LockError(id) });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function defaultState(id) {
|
|
37
|
+
return {
|
|
38
|
+
slice: id,
|
|
39
|
+
state: 'todo',
|
|
40
|
+
branch: null,
|
|
41
|
+
pr: null,
|
|
42
|
+
redCommit: null,
|
|
43
|
+
frozenTests: [],
|
|
44
|
+
unfrozen: [],
|
|
45
|
+
verdicts: [],
|
|
46
|
+
ci: null,
|
|
47
|
+
mergedAt: null,
|
|
48
|
+
handoff: null,
|
|
49
|
+
history: [],
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function sliceExists(root, cfg, id) {
|
|
54
|
+
return findSliceFiles(root, cfg).some((f) => {
|
|
55
|
+
try {
|
|
56
|
+
return parseSliceFile(readText(f.path), f.path).id === id;
|
|
57
|
+
} catch {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function readSlice(root, cfg, id) {
|
|
64
|
+
const file = stateFile(root, cfg, id);
|
|
65
|
+
if (exists(file)) {
|
|
66
|
+
let stored;
|
|
67
|
+
try {
|
|
68
|
+
stored = JSON.parse(readText(file));
|
|
69
|
+
} catch {
|
|
70
|
+
throw new Error(`${file}: not valid JSON`);
|
|
71
|
+
}
|
|
72
|
+
return { ...defaultState(id), ...stored };
|
|
73
|
+
}
|
|
74
|
+
if (sliceExists(root, cfg, id)) return defaultState(id);
|
|
75
|
+
throw new Error(`unknown slice ${id}`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function writeSlice(root, cfg, state) {
|
|
79
|
+
writeText(stateFile(root, cfg, state.slice), `${JSON.stringify(state, null, 2)}\n`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function listSlices(root, cfg) {
|
|
83
|
+
const out = [];
|
|
84
|
+
for (const f of findSliceFiles(root, cfg)) {
|
|
85
|
+
const slice = parseSliceFile(readText(f.path), f.path);
|
|
86
|
+
out.push({ file: f.path, state: readSlice(root, cfg, slice.id) });
|
|
87
|
+
}
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Who made a move: a backfill, the host sync (evidence the host reported), or a person at the CLI.
|
|
92
|
+
const by = (backfill, ev) => (backfill ? 'backfill' : ev.source === 'host' ? 'sync' : 'cli');
|
|
93
|
+
|
|
94
|
+
function entry(from, to, by, evidence) {
|
|
95
|
+
return { from, to, at: new Date().toISOString(), by, evidence };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// In a repository with commits, the red commit must be one of them, and every SHA the repository
|
|
99
|
+
// can resolve is stored as its full 40-hex form. A head or CI SHA the repository has not fetched
|
|
100
|
+
// is kept as given; comparisons then match by prefix.
|
|
101
|
+
function resolveShas(root, ev) {
|
|
102
|
+
const given = [ev.commit, ev.pr?.headSha, ev.ci?.sha].some((s) => s !== undefined);
|
|
103
|
+
if (!given || !isRepo(root) || !hasCommits(root)) return ev;
|
|
104
|
+
const out = { ...ev };
|
|
105
|
+
if (ev.commit !== undefined) {
|
|
106
|
+
if (!commitExists(root, ev.commit)) throw new TransitionError(`commit ${ev.commit} is not a commit in this repository`);
|
|
107
|
+
out.commit = resolveCommit(root, ev.commit) ?? ev.commit;
|
|
108
|
+
}
|
|
109
|
+
if (ev.pr?.headSha !== undefined) {
|
|
110
|
+
const full = resolveCommit(root, ev.pr.headSha);
|
|
111
|
+
if (full) out.pr = { ...ev.pr, headSha: full };
|
|
112
|
+
}
|
|
113
|
+
if (ev.ci?.sha !== undefined) {
|
|
114
|
+
const full = resolveCommit(root, ev.ci.sha);
|
|
115
|
+
if (full) out.ci = { ...ev.ci, sha: full };
|
|
116
|
+
}
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// The only states backfill records.
|
|
121
|
+
const BACKFILL_TO = ['draft-pr', 'merged'];
|
|
122
|
+
|
|
123
|
+
export function setState(root, cfg, id, to, evidence = {}) {
|
|
124
|
+
return withLock(root, cfg, id, () => {
|
|
125
|
+
const current = readSlice(root, cfg, id);
|
|
126
|
+
const { backfill, ...given } = evidence;
|
|
127
|
+
const rule = TRANSITIONS[to];
|
|
128
|
+
if (!rule) throw new TransitionError(`${current.state} cannot move to ${to}`);
|
|
129
|
+
const typeError = validateEvidence(given);
|
|
130
|
+
if (typeError) throw new TransitionError(typeError);
|
|
131
|
+
const ev = resolveShas(root, given);
|
|
132
|
+
if (backfill) {
|
|
133
|
+
// Backfill adopts a slice built outside the loop: an open pull request or a merge. A review,
|
|
134
|
+
// CI or ready state is recorded only by the build commands, whose checks backfill would skip.
|
|
135
|
+
if (!BACKFILL_TO.includes(to)) throw new TransitionError('backfill records only draft-pr or merged; later states come from the build commands');
|
|
136
|
+
if (current.state !== 'todo') throw new TransitionError('backfill is only allowed from todo');
|
|
137
|
+
const missing = rule.required.filter((k) => ev[k] === undefined);
|
|
138
|
+
if (missing.length > 0) throw new TransitionError(`backfill to ${to} needs ${missing.join(', ')}`);
|
|
139
|
+
const pr = ev.pr;
|
|
140
|
+
if (!pr || !Number.isInteger(pr.number) || typeof pr.headSha !== 'string' || pr.headSha === '') {
|
|
141
|
+
throw new TransitionError(`backfill to ${to} needs pr.number and pr.headSha`);
|
|
142
|
+
}
|
|
143
|
+
} else {
|
|
144
|
+
if (!allowedMove(current.state, to, ev)) {
|
|
145
|
+
throw new TransitionError(`${current.state} cannot move to ${to}`);
|
|
146
|
+
}
|
|
147
|
+
const reason = rule.check(current, ev, { root, cfg, id });
|
|
148
|
+
if (reason) throw new TransitionError(reason);
|
|
149
|
+
}
|
|
150
|
+
const next = {
|
|
151
|
+
...current,
|
|
152
|
+
...rule.apply(current, ev),
|
|
153
|
+
state: to,
|
|
154
|
+
// A handoff belongs to the state it was written in.
|
|
155
|
+
handoff: null,
|
|
156
|
+
history: [...current.history, entry(current.state, to, by(backfill, ev), ev)],
|
|
157
|
+
};
|
|
158
|
+
writeSlice(root, cfg, next);
|
|
159
|
+
return next;
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Only a test frozen at tests-red can be unfrozen; any other path has nothing to release. `extra`
|
|
164
|
+
// joins the history entry's evidence (build amend gives the amendment commit and the reason).
|
|
165
|
+
export function unfreeze(root, cfg, id, file, extra = {}) {
|
|
166
|
+
return withLock(root, cfg, id, () => {
|
|
167
|
+
const current = readSlice(root, cfg, id);
|
|
168
|
+
if (!current.frozenTests.includes(file)) {
|
|
169
|
+
throw new TransitionError(`${file} is not a frozen test of ${id}`);
|
|
170
|
+
}
|
|
171
|
+
const next = {
|
|
172
|
+
...current,
|
|
173
|
+
unfrozen: [...current.unfrozen, file],
|
|
174
|
+
history: [...current.history, entry(current.state, current.state, 'cli', { unfreeze: file, ...extra })],
|
|
175
|
+
};
|
|
176
|
+
writeSlice(root, cfg, next);
|
|
177
|
+
return next;
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Replaces only the slice's handoff, under the slice lock, re-reading the file inside it so a
|
|
182
|
+
// concurrent `slicekit state set` is never undone. State and history are left as they are. A slice with
|
|
183
|
+
// no state file yet is skipped (returns false): the handoff never creates one. Throws LockError when
|
|
184
|
+
// the lock is not free within budgetMs.
|
|
185
|
+
export function setHandoff(root, cfg, id, handoff, { budgetMs = LOCK_BUDGET_MS } = {}) {
|
|
186
|
+
return withLock(root, cfg, id, () => {
|
|
187
|
+
if (!exists(stateFile(root, cfg, id))) return false;
|
|
188
|
+
writeSlice(root, cfg, { ...readSlice(root, cfg, id), handoff });
|
|
189
|
+
return true;
|
|
190
|
+
}, budgetMs);
|
|
191
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { GitHostNotFound } from '../adapters/githost/errors.js';
|
|
2
|
+
import { listSlices, setState } from './index.js';
|
|
3
|
+
import { isDate } from '../evidence.js';
|
|
4
|
+
import { HOST_MERGE_FROM } from './transitions.js';
|
|
5
|
+
|
|
6
|
+
// Before a pull request exists in the state machine: a merged pull request recorded here means the
|
|
7
|
+
// record is wrong, and a human should look rather than the sync guess.
|
|
8
|
+
const BEFORE_DRAFT = ['todo', 'tests-red', 'building'];
|
|
9
|
+
|
|
10
|
+
// Moves every slice in a pre-merge state (draft-pr through ready) whose pull request the host
|
|
11
|
+
// reports merged to `merged`, recording the real previous state and `by: 'sync'`. A slice before
|
|
12
|
+
// draft-pr with a merged pull request is warned, not moved. Returns
|
|
13
|
+
// { changes: [{ id, from, to, pr }], warnings: [string] }.
|
|
14
|
+
//
|
|
15
|
+
// A pull request the host cannot find, an answer about another pull request, and a merge time that
|
|
16
|
+
// is not a date are each warned and skipped. Any other error stops the sync; it
|
|
17
|
+
// carries what was already written as `error.partial = { changes, warnings }`, so a caller can
|
|
18
|
+
// report every change that reached disk before the failure.
|
|
19
|
+
export function syncFromHost(root, cfg, host) {
|
|
20
|
+
const changes = [];
|
|
21
|
+
const warnings = [];
|
|
22
|
+
try {
|
|
23
|
+
for (const { state } of listSlices(root, cfg)) {
|
|
24
|
+
const from = state.state;
|
|
25
|
+
const syncable = HOST_MERGE_FROM.includes(from);
|
|
26
|
+
if (!syncable && !BEFORE_DRAFT.includes(from)) continue;
|
|
27
|
+
const number = state.pr?.number;
|
|
28
|
+
if (!Number.isInteger(number) || number < 1) {
|
|
29
|
+
if (syncable) warnings.push(`${state.slice} is ${from} but has no valid pull request number recorded; skipped`);
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
let pr;
|
|
33
|
+
try {
|
|
34
|
+
pr = host.getPullRequest(number);
|
|
35
|
+
} catch (e) {
|
|
36
|
+
if (!(e instanceof GitHostNotFound)) throw e;
|
|
37
|
+
warnings.push(`${state.slice}: pull request ${number} could not be fetched: ${e.message}; skipped`);
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (pr.number !== number) {
|
|
41
|
+
warnings.push(`${state.slice}: the host answered pull request ${number} with pull request ${pr.number}; skipped`);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (!pr.mergedAt) continue;
|
|
45
|
+
if (!isDate(pr.mergedAt)) {
|
|
46
|
+
warnings.push(`${state.slice}: pull request ${number} has a merge time that is not a date ('${pr.mergedAt}'); skipped`);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (!syncable) {
|
|
50
|
+
warnings.push(`${state.slice} is ${from} but pull request ${number} is merged; its record needs a human; skipped`);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const evidence = { number };
|
|
54
|
+
if (pr.url) evidence.url = pr.url;
|
|
55
|
+
if (pr.headSha) evidence.headSha = pr.headSha;
|
|
56
|
+
setState(root, cfg, state.slice, 'merged', { mergedAt: pr.mergedAt, pr: evidence, source: 'host' });
|
|
57
|
+
changes.push({ id: state.slice, from, to: 'merged', pr: number });
|
|
58
|
+
}
|
|
59
|
+
} catch (e) {
|
|
60
|
+
if (e && typeof e === 'object') e.partial = { changes, warnings };
|
|
61
|
+
throw e;
|
|
62
|
+
}
|
|
63
|
+
return { changes, warnings };
|
|
64
|
+
}
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { isDate, isSha, sameSha } from '../evidence.js';
|
|
4
|
+
import { isReviewWorktreeAt } from '../worktrees.js';
|
|
5
|
+
import { headCovers } from './covers.js';
|
|
6
|
+
|
|
7
|
+
export const STATES = [
|
|
8
|
+
'todo',
|
|
9
|
+
'tests-red',
|
|
10
|
+
'building',
|
|
11
|
+
'draft-pr',
|
|
12
|
+
'in-review',
|
|
13
|
+
'changes-requested',
|
|
14
|
+
'ci-green',
|
|
15
|
+
'ready',
|
|
16
|
+
'merged',
|
|
17
|
+
'released',
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
export class TransitionError extends Error {
|
|
21
|
+
constructor(reason) {
|
|
22
|
+
super(reason);
|
|
23
|
+
this.name = 'TransitionError';
|
|
24
|
+
this.reason = reason;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const last = (list) => list[list.length - 1];
|
|
29
|
+
const isInt = (n) => Number.isInteger(n) && n >= 0;
|
|
30
|
+
const isStr = (s) => typeof s === 'string' && s !== '';
|
|
31
|
+
const present = (root, rel) => fs.existsSync(path.resolve(root, rel));
|
|
32
|
+
const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
33
|
+
|
|
34
|
+
// The verdicts that hold on the pull request head as it stands now: those given on it, and, with a
|
|
35
|
+
// repository to look in, those given on an earlier head whose later commits are only the kit's own
|
|
36
|
+
// bookkeeping (D-35). Any other earlier head reviewed code a fix push has since replaced. `head`
|
|
37
|
+
// defaults to the head the state recorded; a caller holding the live head (a CI event) passes it.
|
|
38
|
+
// No head, no verdicts.
|
|
39
|
+
export function verdictsOnHead(current, head = current.pr?.headSha, root) {
|
|
40
|
+
return (current.verdicts ?? []).filter(
|
|
41
|
+
(v) => sameSha(v.headSha, head) || (root !== undefined && headCovers(root, v.headSha, head)),
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
const onHead = (current, ctx) => verdictsOnHead(current, current.pr?.headSha, ctx.root);
|
|
45
|
+
|
|
46
|
+
// The independent-review rule `ready` and the ready label share: the latest independent verdict on
|
|
47
|
+
// the head decides, so a later independent verdict with blockers is not hidden by an earlier clean one.
|
|
48
|
+
export const INDEPENDENT_MISSING = 'independent review missing';
|
|
49
|
+
export function independentProblem(verdicts) {
|
|
50
|
+
const ind = verdicts.filter((v) => v.independent).at(-1);
|
|
51
|
+
if (!ind) return INDEPENDENT_MISSING;
|
|
52
|
+
if (ind.blockers !== 0) return `independent review found ${ind.blockers} blocker${ind.blockers === 1 ? '' : 's'}`;
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const SHA_SHAPE = '7 to 40 lowercase hex characters';
|
|
57
|
+
|
|
58
|
+
// The states in-review is reached from only on an independent verdict (D-39).
|
|
59
|
+
export const INDEPENDENT_ONLY = ['in-review', 'ci-green', 'ready'];
|
|
60
|
+
|
|
61
|
+
// Every evidence value that is given must have the right type, whatever the target state.
|
|
62
|
+
export function validateEvidence(ev) {
|
|
63
|
+
const bad = (name, want) => `${name} must be ${want}`;
|
|
64
|
+
for (const k of ['commit', 'release']) {
|
|
65
|
+
if (ev[k] !== undefined && !isStr(ev[k])) return bad(k, 'a non-empty string');
|
|
66
|
+
}
|
|
67
|
+
if (ev.commit !== undefined && !isSha(ev.commit)) return bad('commit', SHA_SHAPE);
|
|
68
|
+
if (ev.mergedAt !== undefined && !isDate(ev.mergedAt)) return bad('mergedAt', 'an ISO date');
|
|
69
|
+
if (ev.source !== undefined && ev.source !== 'host') return bad('source', "'host'");
|
|
70
|
+
for (const k of ['testExit', 'checkExit']) {
|
|
71
|
+
if (ev[k] !== undefined && !isInt(ev[k])) return bad(k, 'a non-negative integer');
|
|
72
|
+
}
|
|
73
|
+
if (ev.tests !== undefined && !(Array.isArray(ev.tests) && ev.tests.every(isStr))) {
|
|
74
|
+
return bad('tests', 'an array of non-empty strings');
|
|
75
|
+
}
|
|
76
|
+
const pr = ev.pr;
|
|
77
|
+
if (pr !== undefined) {
|
|
78
|
+
if (pr === null || typeof pr !== 'object') return bad('pr', 'an object');
|
|
79
|
+
if (pr.number !== undefined && !(Number.isInteger(pr.number) && pr.number >= 1)) {
|
|
80
|
+
return bad('pr.number', 'a positive integer');
|
|
81
|
+
}
|
|
82
|
+
for (const k of ['url', 'headSha', 'title']) {
|
|
83
|
+
if (pr[k] !== undefined && !isStr(pr[k])) return bad(`pr.${k}`, 'a non-empty string');
|
|
84
|
+
}
|
|
85
|
+
if (pr.headSha !== undefined && !isSha(pr.headSha)) return bad('pr.headSha', SHA_SHAPE);
|
|
86
|
+
}
|
|
87
|
+
const v = ev.verdict;
|
|
88
|
+
if (v !== undefined) {
|
|
89
|
+
if (v === null || typeof v !== 'object') return bad('verdict', 'an object');
|
|
90
|
+
for (const k of ['round', 'blockers']) {
|
|
91
|
+
if (v[k] !== undefined && !isInt(v[k])) return bad(`verdict.${k}`, 'a non-negative integer');
|
|
92
|
+
}
|
|
93
|
+
if (v.path !== undefined && !isStr(v.path)) return bad('verdict.path', 'a non-empty string');
|
|
94
|
+
}
|
|
95
|
+
const ci = ev.ci;
|
|
96
|
+
if (ci !== undefined) {
|
|
97
|
+
if (ci === null || typeof ci !== 'object') return bad('ci', 'an object');
|
|
98
|
+
for (const k of ['sha', 'status']) {
|
|
99
|
+
if (ci[k] !== undefined && !isStr(ci[k])) return bad(`ci.${k}`, 'a non-empty string');
|
|
100
|
+
}
|
|
101
|
+
if (ci.sha !== undefined && !isSha(ci.sha)) return bad('ci.sha', SHA_SHAPE);
|
|
102
|
+
if (ci.url !== undefined && !isStr(ci.url)) return bad('ci.url', 'a non-empty string');
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// `from` is the one place the graph is written; `required` names the evidence keys the target
|
|
108
|
+
// stores, which --backfill still demands. "Frozen tests unchanged since the red commit" is not
|
|
109
|
+
// checked here: the pre-edit hook (S-06) and `slicekit check` enforce it.
|
|
110
|
+
export const TRANSITIONS = {
|
|
111
|
+
'tests-red': {
|
|
112
|
+
from: ['todo'],
|
|
113
|
+
required: ['commit', 'tests'],
|
|
114
|
+
check(_current, ev) {
|
|
115
|
+
if (!ev.commit) return 'tests-red needs the commit sha of the failing run (--commit)';
|
|
116
|
+
if (ev.testExit === undefined || ev.testExit === 0) {
|
|
117
|
+
return 'tests-red needs a non-zero test exit code (--test-exit); the tests must fail first';
|
|
118
|
+
}
|
|
119
|
+
if (!Array.isArray(ev.tests) || ev.tests.length === 0) {
|
|
120
|
+
return 'tests-red needs the test file paths (--tests a,b)';
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
},
|
|
124
|
+
apply: (_current, ev) => ({ redCommit: ev.commit, frozenTests: [...ev.tests] }),
|
|
125
|
+
},
|
|
126
|
+
building: {
|
|
127
|
+
from: ['tests-red'],
|
|
128
|
+
required: [],
|
|
129
|
+
check(current, _ev, ctx) {
|
|
130
|
+
if (!current.redCommit) return 'building needs a recorded red commit';
|
|
131
|
+
const missing = current.frozenTests.filter((t) => !present(ctx.root, t));
|
|
132
|
+
return missing.length > 0 ? `frozen tests are missing: ${missing.join(', ')}` : null;
|
|
133
|
+
},
|
|
134
|
+
apply: () => ({}),
|
|
135
|
+
},
|
|
136
|
+
'draft-pr': {
|
|
137
|
+
from: ['building', 'in-review', 'changes-requested', 'ci-green', 'ready'],
|
|
138
|
+
required: ['pr'],
|
|
139
|
+
check(current, ev, ctx) {
|
|
140
|
+
if (ev.testExit !== 0) return 'draft-pr needs the test command to exit 0 (--test-exit 0)';
|
|
141
|
+
if (ev.checkExit !== 0) return 'draft-pr needs slicekit check to exit 0 (--check-exit 0)';
|
|
142
|
+
const prefix = `[${ctx.id}]`;
|
|
143
|
+
const pr = ev.pr;
|
|
144
|
+
const titled = pr && typeof pr.title === 'string' && (pr.title === prefix || pr.title.startsWith(`${prefix} `));
|
|
145
|
+
if (!titled) return `draft-pr needs a pull request whose title begins ${prefix} followed by a space`;
|
|
146
|
+
if (!isInt(pr.number)) return 'draft-pr needs the pull request number (pr.number)';
|
|
147
|
+
if (!isStr(pr.headSha)) return 'draft-pr needs the pull request head sha (pr.headSha)';
|
|
148
|
+
if (current.state !== 'building' && current.pr && sameSha(pr.headSha, current.pr.headSha)) {
|
|
149
|
+
return 'a return to draft-pr needs a new head sha (a fix push)';
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
},
|
|
153
|
+
apply: (_current, ev) => ({
|
|
154
|
+
pr: { number: ev.pr.number, url: ev.pr.url, headSha: ev.pr.headSha },
|
|
155
|
+
ci: null,
|
|
156
|
+
}),
|
|
157
|
+
},
|
|
158
|
+
// D-39: an independent verdict can also be given on a slice already in review, CI-green or ready,
|
|
159
|
+
// and sends it back to review; an in-loop verdict cannot.
|
|
160
|
+
'in-review': {
|
|
161
|
+
from: ['draft-pr', 'changes-requested', 'in-review', 'ci-green', 'ready'],
|
|
162
|
+
required: ['verdict'],
|
|
163
|
+
check(current, ev, ctx) {
|
|
164
|
+
const v = ev.verdict;
|
|
165
|
+
if (INDEPENDENT_ONLY.includes(current.state) && v?.independent !== true) return `${current.state} takes only an independent verdict`;
|
|
166
|
+
// The building session never approves itself: an independent verdict is recorded only in the
|
|
167
|
+
// review worktree `review start` makes, whichever command records it (D-39). A root that is no
|
|
168
|
+
// worktree cannot show it is that one, so it is refused too.
|
|
169
|
+
if (v?.independent === true && !isReviewWorktreeAt(ctx.root, ctx.id)) {
|
|
170
|
+
return `an independent verdict is recorded from the review worktree; run slicekit review start ${current.pr?.number ?? '<PR>'} in a new session`;
|
|
171
|
+
}
|
|
172
|
+
if (!v || !isInt(v.round) || v.round < 1) return 'in-review needs a verdict with round 1 or more';
|
|
173
|
+
if (!isInt(v.blockers)) return 'in-review needs the verdict blocker count';
|
|
174
|
+
if (!isStr(v.path)) return 'in-review needs the verdict file path (verdict.path)';
|
|
175
|
+
if (!present(ctx.root, v.path)) return `verdict file not found: ${v.path}`;
|
|
176
|
+
if (!current.pr?.headSha) return 'in-review needs a recorded pull request head sha';
|
|
177
|
+
const prev = last(current.verdicts);
|
|
178
|
+
if (!prev && v.round !== 1) return `the first verdict must be round 1, got round ${v.round}`;
|
|
179
|
+
if (prev && v.round <= prev.round) return `verdict round ${v.round} is not after round ${prev.round}`;
|
|
180
|
+
return null;
|
|
181
|
+
},
|
|
182
|
+
// Each verdict is stamped with the head it was given on; `ready` trusts only those on the current head.
|
|
183
|
+
apply: (current, ev) => ({
|
|
184
|
+
verdicts: [
|
|
185
|
+
...current.verdicts,
|
|
186
|
+
{
|
|
187
|
+
round: ev.verdict.round,
|
|
188
|
+
path: ev.verdict.path,
|
|
189
|
+
blockers: ev.verdict.blockers,
|
|
190
|
+
independent: Boolean(ev.verdict.independent),
|
|
191
|
+
headSha: current.pr.headSha,
|
|
192
|
+
},
|
|
193
|
+
],
|
|
194
|
+
}),
|
|
195
|
+
},
|
|
196
|
+
'changes-requested': {
|
|
197
|
+
from: ['in-review'],
|
|
198
|
+
required: [],
|
|
199
|
+
check(current, _ev, ctx) {
|
|
200
|
+
const v = last(onHead(current, ctx));
|
|
201
|
+
return v && v.blockers > 0 ? null : 'changes-requested needs a latest verdict on the current head sha with open blockers';
|
|
202
|
+
},
|
|
203
|
+
apply: () => ({}),
|
|
204
|
+
},
|
|
205
|
+
'ci-green': {
|
|
206
|
+
from: ['in-review'],
|
|
207
|
+
required: ['ci'],
|
|
208
|
+
check(current, ev, ctx) {
|
|
209
|
+
const v = last(onHead(current, ctx));
|
|
210
|
+
if (!v || v.blockers !== 0) return 'ci-green needs a latest verdict on the current head sha with no blockers';
|
|
211
|
+
if (!ev.ci || ev.ci.status !== 'success') return 'ci-green needs the CI status to be success (--ci-status)';
|
|
212
|
+
if (!isStr(ev.ci.sha)) return 'ci-green needs the CI sha (--ci-sha)';
|
|
213
|
+
// D-35: CI on a later head that adds only the kit's bookkeeping ran on the reviewed work.
|
|
214
|
+
const head = current.pr?.headSha;
|
|
215
|
+
if (!head || !(sameSha(ev.ci.sha, head) || headCovers(ctx.root, head, ev.ci.sha))) {
|
|
216
|
+
return 'ci-green needs the CI sha to be the pull request head sha or a later head that only adds bookkeeping';
|
|
217
|
+
}
|
|
218
|
+
return null;
|
|
219
|
+
},
|
|
220
|
+
apply: (_current, ev) => ({ ci: { sha: ev.ci.sha, status: ev.ci.status, ...(ev.ci.url ? { url: ev.ci.url } : {}) } }),
|
|
221
|
+
},
|
|
222
|
+
ready: {
|
|
223
|
+
from: ['ci-green'],
|
|
224
|
+
required: [],
|
|
225
|
+
check(current, _ev, ctx) {
|
|
226
|
+
if (ctx.cfg.review?.independent === 'optional') return null;
|
|
227
|
+
const problem = independentProblem(onHead(current, ctx));
|
|
228
|
+
return problem ? `ready needs an independent review verdict with no blockers on the current head sha: ${problem}` : null;
|
|
229
|
+
},
|
|
230
|
+
apply: () => ({}),
|
|
231
|
+
},
|
|
232
|
+
// A merge the git host reports is a fact, whatever pre-merge state the slice was in: `hostFrom`
|
|
233
|
+
// reaches merged only with `source: 'host'` evidence naming the recorded pull request (the sync
|
|
234
|
+
// supplies it; the state CLI cannot). The `ready` path is the ordinary one.
|
|
235
|
+
merged: {
|
|
236
|
+
from: ['ready'],
|
|
237
|
+
hostFrom: ['draft-pr', 'in-review', 'changes-requested', 'ci-green'],
|
|
238
|
+
required: ['mergedAt'],
|
|
239
|
+
check(current, ev) {
|
|
240
|
+
if (!ev.mergedAt) return 'merged needs mergedAt (--merged-at)';
|
|
241
|
+
if (current.state === 'ready') return null;
|
|
242
|
+
if (ev.source !== 'host') return `${current.state} reaches merged only on a merge the git host reports`;
|
|
243
|
+
const n = current.pr?.number;
|
|
244
|
+
if (!Number.isInteger(n)) return 'a host merge needs a recorded pull request number';
|
|
245
|
+
if (ev.pr?.number !== n) return `a host merge must name the recorded pull request #${n}`;
|
|
246
|
+
return null;
|
|
247
|
+
},
|
|
248
|
+
apply: (current, ev) => ({
|
|
249
|
+
mergedAt: ev.mergedAt,
|
|
250
|
+
...(ev.pr ? { pr: { ...(current.pr ?? {}), ...ev.pr } } : {}),
|
|
251
|
+
}),
|
|
252
|
+
},
|
|
253
|
+
released: {
|
|
254
|
+
from: ['merged'],
|
|
255
|
+
required: ['release'],
|
|
256
|
+
check(current, ev, ctx) {
|
|
257
|
+
if (!ev.release) return 'released needs the release file path (--release)';
|
|
258
|
+
if (!present(ctx.root, ev.release)) return `release file not found: ${ev.release}`;
|
|
259
|
+
const pr = current.pr;
|
|
260
|
+
if (!pr) return 'released needs a recorded pull request to look for in the release record';
|
|
261
|
+
const text = fs.readFileSync(path.resolve(ctx.root, ev.release), 'utf8');
|
|
262
|
+
// `#7` is not `#70`, and the url of pull request 7 is not the url of pull request 70.
|
|
263
|
+
const byNumber = pr.number !== undefined && new RegExp(`#${pr.number}(?![0-9A-Za-z_])`).test(text);
|
|
264
|
+
const byUrl = isStr(pr.url) && new RegExp(`${escape(pr.url)}(?![0-9A-Za-z_])`).test(text);
|
|
265
|
+
return byNumber || byUrl ? null : `the release record does not name the pull request (#${pr.number})`;
|
|
266
|
+
},
|
|
267
|
+
apply: () => ({}),
|
|
268
|
+
},
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
export function nextStates(from) {
|
|
272
|
+
return STATES.filter((to) => TRANSITIONS[to]?.from.includes(from));
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// The states a merge the host reports moves to merged.
|
|
276
|
+
export const HOST_MERGE_FROM = [...TRANSITIONS.merged.hostFrom, ...TRANSITIONS.merged.from];
|
|
277
|
+
|
|
278
|
+
// Whether from → to is an edge for this evidence: the graph's own, or a host-only edge with host evidence.
|
|
279
|
+
export function allowedMove(from, to, ev) {
|
|
280
|
+
if (nextStates(from).includes(to)) return true;
|
|
281
|
+
return ev.source === 'host' && Boolean(TRANSITIONS[to]?.hostFrom?.includes(from));
|
|
282
|
+
}
|