slicekit 0.0.1 → 0.1.1
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 +276 -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 +59 -0
- package/templates/githooks/pre-merge-commit +59 -0
- package/templates/githooks/prepare-commit-msg +74 -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,128 @@
|
|
|
1
|
+
const MARKERS = {
|
|
2
|
+
html: {
|
|
3
|
+
start: (v) => `<!-- slicekit:managed:start v${v} -->`,
|
|
4
|
+
end: '<!-- slicekit:managed:end -->',
|
|
5
|
+
startRe: /^<!-- slicekit:managed:start v(.*?) -->\r?$/,
|
|
6
|
+
endRe: /^<!-- slicekit:managed:end -->\r?$/,
|
|
7
|
+
},
|
|
8
|
+
hash: {
|
|
9
|
+
start: (v) => `# slicekit:managed:start v${v}`,
|
|
10
|
+
end: '# slicekit:managed:end',
|
|
11
|
+
startRe: /^# slicekit:managed:start v(.*?)\r?$/,
|
|
12
|
+
endRe: /^# slicekit:managed:end\r?$/,
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const KEY = 'slicekit-managed';
|
|
17
|
+
const KEY_RE = /^slicekit-managed:\s*v(.*?)\s*$/;
|
|
18
|
+
|
|
19
|
+
const bare = (line) => line.replace(/\r$/, '');
|
|
20
|
+
|
|
21
|
+
// A file whose first line is --- keeps it there: the version rides inside the front matter.
|
|
22
|
+
function frontmatterEnd(lines) {
|
|
23
|
+
return bare(lines[0]) === '---' ? lines.findIndex((l, i) => i > 0 && bare(l) === '---') : -1;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function markerStyle(path, content) {
|
|
27
|
+
const base = String(path).split(/[\\/]/).pop();
|
|
28
|
+
const dot = base.lastIndexOf('.');
|
|
29
|
+
const ext = dot > 0 ? base.slice(dot + 1).toLowerCase() : '';
|
|
30
|
+
if (ext === 'md' && typeof content === 'string' && /^---\r?\n/.test(content)) return 'frontmatter';
|
|
31
|
+
if (ext === 'md' || ext === 'html') return 'html';
|
|
32
|
+
if (ext === 'json') return 'json';
|
|
33
|
+
return 'hash';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function markersFor(style) {
|
|
37
|
+
const m = MARKERS[style];
|
|
38
|
+
if (!m) throw new Error(`unknown managed style: ${style}`);
|
|
39
|
+
return m;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function oneNewline(s) {
|
|
43
|
+
return s.replace(/\n+$/, '') + '\n';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function jsonWith(content, version) {
|
|
47
|
+
const obj = JSON.parse(content);
|
|
48
|
+
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) throw new Error('managed json must be an object');
|
|
49
|
+
obj._slicekit = { managed: true, version };
|
|
50
|
+
return JSON.stringify(obj, null, 2) + '\n';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function frontmatterWith(content, version) {
|
|
54
|
+
const lines = content.split('\n');
|
|
55
|
+
const end = frontmatterEnd(lines);
|
|
56
|
+
if (end < 0) throw new Error('managed frontmatter: no front matter block');
|
|
57
|
+
const cr = lines[0].endsWith('\r') ? '\r' : '';
|
|
58
|
+
const kept = lines.slice(1, end).filter((l) => !KEY_RE.test(bare(l)));
|
|
59
|
+
return [lines[0], ...kept, `${KEY}: v${version}${cr}`, ...lines.slice(end)].join('\n');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function wrapManaged(content, { version, style }) {
|
|
63
|
+
if (style === 'frontmatter') return frontmatterWith(content, version);
|
|
64
|
+
if (style === 'json') return jsonWith(content, version);
|
|
65
|
+
const m = markersFor(style);
|
|
66
|
+
return `${m.start(version)}\n${oneNewline(content)}${m.end}\n`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function extractManaged(text, style) {
|
|
70
|
+
if (style === 'frontmatter') {
|
|
71
|
+
const lines = text.split('\n');
|
|
72
|
+
const end = frontmatterEnd(lines);
|
|
73
|
+
if (end < 0) return null;
|
|
74
|
+
for (const l of lines.slice(1, end)) {
|
|
75
|
+
const hit = KEY_RE.exec(bare(l));
|
|
76
|
+
if (hit) return { before: '', block: text, after: '', version: hit[1] };
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
if (style === 'json') {
|
|
81
|
+
let obj;
|
|
82
|
+
try {
|
|
83
|
+
obj = JSON.parse(text);
|
|
84
|
+
} catch {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
const meta = obj && typeof obj === 'object' ? obj._slicekit : null;
|
|
88
|
+
if (!meta || meta.managed !== true) return null;
|
|
89
|
+
return { before: '', block: text, after: '', version: meta.version };
|
|
90
|
+
}
|
|
91
|
+
const m = markersFor(style);
|
|
92
|
+
let offset = 0;
|
|
93
|
+
let startEnd = -1;
|
|
94
|
+
let endStart = -1;
|
|
95
|
+
let version = null;
|
|
96
|
+
for (const line of text.split('\n')) {
|
|
97
|
+
const next = offset + line.length + 1;
|
|
98
|
+
if (startEnd < 0) {
|
|
99
|
+
const hit = m.startRe.exec(line);
|
|
100
|
+
if (hit) {
|
|
101
|
+
startEnd = Math.min(next, text.length);
|
|
102
|
+
version = hit[1];
|
|
103
|
+
}
|
|
104
|
+
} else if (m.endRe.test(line)) {
|
|
105
|
+
endStart = offset;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
offset = next;
|
|
109
|
+
}
|
|
110
|
+
if (startEnd < 0 || endStart < 0) return null;
|
|
111
|
+
return {
|
|
112
|
+
before: text.slice(0, startEnd),
|
|
113
|
+
block: text.slice(startEnd, endStart),
|
|
114
|
+
after: text.slice(endStart),
|
|
115
|
+
version,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function replaceManaged(text, newBlock, { version, style }) {
|
|
120
|
+
const got = extractManaged(text, style);
|
|
121
|
+
if (!got) throw new Error('no managed block found');
|
|
122
|
+
if (style === 'json' || style === 'frontmatter') return wrapManaged(newBlock, { version, style });
|
|
123
|
+
const m = markersFor(style);
|
|
124
|
+
const nl = got.before.endsWith('\r\n') ? '\r\n' : '\n';
|
|
125
|
+
const lineStart = got.before.lastIndexOf('\n', got.before.length - 2) + 1;
|
|
126
|
+
const before = got.before.slice(0, lineStart) + m.start(version) + nl;
|
|
127
|
+
return before + oneNewline(newBlock) + got.after;
|
|
128
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
import { SHAPES } from '../config.js';
|
|
4
|
+
|
|
5
|
+
const ROOT = fileURLToPath(new URL('../../templates/claude-code/', import.meta.url));
|
|
6
|
+
|
|
7
|
+
export function loadManifest() {
|
|
8
|
+
const entries = JSON.parse(fs.readFileSync(ROOT + 'manifest.json', 'utf8'));
|
|
9
|
+
if (!Array.isArray(entries)) throw new Error('manifest: must be an array');
|
|
10
|
+
entries.forEach((e, i) => {
|
|
11
|
+
const at = `manifest[${i}]`;
|
|
12
|
+
if (typeof e?.src !== 'string' || e.src === '') throw new Error(`${at}: src must be a non-empty string`);
|
|
13
|
+
if (typeof e.dest !== 'string' || e.dest === '') throw new Error(`${at}: dest must be a non-empty string`);
|
|
14
|
+
if (!Array.isArray(e.shapes) || e.shapes.length === 0 || !e.shapes.every((s) => SHAPES.includes(s))) {
|
|
15
|
+
throw new Error(`${at}: shapes must be a non-empty array of ${SHAPES.join(', ')}`);
|
|
16
|
+
}
|
|
17
|
+
if (!fs.existsSync(ROOT + e.src)) throw new Error(`${at}: src not found: ${e.src}`);
|
|
18
|
+
});
|
|
19
|
+
return entries;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function entriesForShape(shape) {
|
|
23
|
+
return loadManifest().filter((e) => e.shapes.includes(shape));
|
|
24
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { ConfigError } from '../config.js';
|
|
3
|
+
import { exists, readText } from '../fs.js';
|
|
4
|
+
|
|
5
|
+
const LEGACY = 'slicekit hook ';
|
|
6
|
+
export const SETTINGS_PATH = '.claude/settings.json';
|
|
7
|
+
|
|
8
|
+
// The parsed settings object, undefined when the file is absent; ConfigError when it is unusable.
|
|
9
|
+
export function readSettings(root) {
|
|
10
|
+
const file = path.join(root, SETTINGS_PATH);
|
|
11
|
+
if (!exists(file)) return undefined;
|
|
12
|
+
try {
|
|
13
|
+
const obj = JSON.parse(readText(file));
|
|
14
|
+
if (obj === null || typeof obj !== 'object' || Array.isArray(obj)) throw new Error('not an object');
|
|
15
|
+
return obj;
|
|
16
|
+
} catch {
|
|
17
|
+
throw new ConfigError(['.claude/settings.json is not valid JSON; fix it or remove it']);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function isOurs(hook, prefixes) {
|
|
22
|
+
return typeof hook?.command === 'string' && prefixes.some((p) => hook.command.startsWith(p));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Drops only our commands; an entry survives with the team's commands, or goes when nothing is left.
|
|
26
|
+
function withoutOurs(entry, prefixes) {
|
|
27
|
+
if (!entry || !Array.isArray(entry.hooks)) return [entry];
|
|
28
|
+
if (!entry.hooks.some((h) => isOurs(h, prefixes))) return [entry];
|
|
29
|
+
const rest = entry.hooks.filter((h) => !isOurs(h, prefixes));
|
|
30
|
+
return rest.length === 0 ? [] : [{ ...entry, hooks: rest }];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Every command prefix that marks a hook entry as slicekit's: the legacy one, the current cli, the recorded cli.
|
|
34
|
+
export function ourPrefixes(existing, cli) {
|
|
35
|
+
const prefixes = [LEGACY];
|
|
36
|
+
if (cli) prefixes.push(`${cli} hook `);
|
|
37
|
+
const recorded = existing?._slicekit?.cli;
|
|
38
|
+
if (typeof recorded === 'string' && recorded !== '') prefixes.push(`${recorded} hook `);
|
|
39
|
+
return prefixes;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const canon = (v) =>
|
|
43
|
+
Array.isArray(v)
|
|
44
|
+
? `[${v.map(canon).join(',')}]`
|
|
45
|
+
: v && typeof v === 'object'
|
|
46
|
+
? `{${Object.keys(v).sort().map((k) => `${JSON.stringify(k)}:${canon(v[k])}`).join(',')}}`
|
|
47
|
+
: JSON.stringify(v);
|
|
48
|
+
|
|
49
|
+
// { event: [canonical entry] } of the entries that are ours, whole (matcher, timeout, every field),
|
|
50
|
+
// each cut down to its slicekit hooks so a team hook sharing the entry does not count. Order-independent.
|
|
51
|
+
export function ourEntries(settings, prefixes) {
|
|
52
|
+
const out = {};
|
|
53
|
+
const hooks = settings && typeof settings.hooks === 'object' && settings.hooks !== null ? settings.hooks : {};
|
|
54
|
+
for (const event of Object.keys(hooks).sort()) {
|
|
55
|
+
const entries = Array.isArray(hooks[event]) ? hooks[event] : [];
|
|
56
|
+
const ours = entries
|
|
57
|
+
.filter((e) => Array.isArray(e?.hooks) && e.hooks.some((h) => isOurs(h, prefixes)))
|
|
58
|
+
.map((e) => canon({ ...e, hooks: e.hooks.filter((h) => isOurs(h, prefixes)) }))
|
|
59
|
+
.sort();
|
|
60
|
+
if (ours.length > 0) out[event] = ours;
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function mergeSettings(existing, hooks, { version, cli } = {}) {
|
|
66
|
+
const base = existing && typeof existing === 'object' && !Array.isArray(existing) ? existing : {};
|
|
67
|
+
const prefixes = ourPrefixes(base, cli);
|
|
68
|
+
const merged = { ...(base.hooks ?? {}) };
|
|
69
|
+
for (const [event, entries] of Object.entries(hooks)) {
|
|
70
|
+
const kept = Array.isArray(merged[event]) ? merged[event].flatMap((e) => withoutOurs(e, prefixes)) : [];
|
|
71
|
+
merged[event] = [...kept, ...entries];
|
|
72
|
+
}
|
|
73
|
+
const meta = { managed: true, version };
|
|
74
|
+
if (cli) meta.cli = cli;
|
|
75
|
+
return { ...base, hooks: merged, _slicekit: meta };
|
|
76
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { exists, readText, writeText } from '../fs.js';
|
|
4
|
+
import { entriesForShape } from './manifest.js';
|
|
5
|
+
import { managedPart } from './compare.js';
|
|
6
|
+
import { RENDERED, SETTINGS, TEMPLATES, renderEntry, loadHooks, loadReference, varsFor } from './entry.js';
|
|
7
|
+
import { mergeSettings, ourEntries, ourPrefixes, readSettings } from './settings.js';
|
|
8
|
+
|
|
9
|
+
const CONTEXT = 3;
|
|
10
|
+
const NO_EOL = '\';
|
|
11
|
+
|
|
12
|
+
// Minimal unified-style diff by line LCS: '-' removed, '+' added, ' ' context, '@@' between far hunks.
|
|
13
|
+
export function diffLines(a, b) {
|
|
14
|
+
if (a === b) return '';
|
|
15
|
+
const split = (s) => (s === '' ? [] : s.replace(/\n$/, '').split('\n'));
|
|
16
|
+
const x = split(a);
|
|
17
|
+
const y = split(b);
|
|
18
|
+
if (x.join('\n') === y.join('\n')) {
|
|
19
|
+
// Only the trailing newline differs.
|
|
20
|
+
const last = x[x.length - 1];
|
|
21
|
+
return `-${last}\n${a.endsWith('\n') ? '' : `${NO_EOL}\n`}+${last}\n${b.endsWith('\n') ? '' : `${NO_EOL}\n`}`;
|
|
22
|
+
}
|
|
23
|
+
const lcs = Array.from({ length: x.length + 1 }, () => new Array(y.length + 1).fill(0));
|
|
24
|
+
for (let i = x.length - 1; i >= 0; i--) {
|
|
25
|
+
for (let j = y.length - 1; j >= 0; j--) {
|
|
26
|
+
lcs[i][j] = x[i] === y[j] ? lcs[i + 1][j + 1] + 1 : Math.max(lcs[i + 1][j], lcs[i][j + 1]);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const ops = [];
|
|
30
|
+
let i = 0;
|
|
31
|
+
let j = 0;
|
|
32
|
+
while (i < x.length || j < y.length) {
|
|
33
|
+
if (i < x.length && j < y.length && x[i] === y[j]) {
|
|
34
|
+
ops.push([' ', x[i]]);
|
|
35
|
+
i++;
|
|
36
|
+
j++;
|
|
37
|
+
} else if (j < y.length && (i === x.length || lcs[i][j + 1] > lcs[i + 1][j])) ops.push(['+', y[j++]]);
|
|
38
|
+
else ops.push(['-', x[i++]]);
|
|
39
|
+
}
|
|
40
|
+
const keep = ops.map(() => false);
|
|
41
|
+
ops.forEach(([sign], k) => {
|
|
42
|
+
if (sign === ' ') return;
|
|
43
|
+
for (let n = Math.max(0, k - CONTEXT); n <= Math.min(ops.length - 1, k + CONTEXT); n++) keep[n] = true;
|
|
44
|
+
});
|
|
45
|
+
const out = [];
|
|
46
|
+
let skipped = false;
|
|
47
|
+
ops.forEach(([sign, line], k) => {
|
|
48
|
+
if (!keep[k]) {
|
|
49
|
+
skipped = out.length > 0;
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (skipped) out.push('@@');
|
|
53
|
+
skipped = false;
|
|
54
|
+
out.push(`${sign}${line}`);
|
|
55
|
+
});
|
|
56
|
+
return out.length === 0 ? '' : `${out.join('\n')}\n`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function fileItem(root, cfg, entry, opts) {
|
|
60
|
+
const { dest, current, reference, verdict, next, fresh, style } = renderEntry(root, cfg, entry, opts);
|
|
61
|
+
// With no reference copy the verdict was taken against the fresh render.
|
|
62
|
+
const base = { dest, next, current, reference, refText: next, noReference: reference === null };
|
|
63
|
+
if (verdict === 'missing' || verdict === 'unmanaged') return { ...base, status: verdict, diff: '' };
|
|
64
|
+
const diff = diffLines(managedPart(current, style), managedPart(fresh, style));
|
|
65
|
+
if (verdict === 'drifted') return { ...base, status: 'drifted', diff };
|
|
66
|
+
return { ...base, status: next === current ? 'same' : 'changed', diff };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// settings.json is judged by its slicekit hook entries: those are the gate, so a removed or altered
|
|
70
|
+
// one is drift. A version bump or a newly added event is an ordinary change. The reference copy is
|
|
71
|
+
// the hooks slicekit last wrote.
|
|
72
|
+
function settingsItem(root, cfg, { version, templates }) {
|
|
73
|
+
const hooks = loadHooks(templates, varsFor(cfg, version));
|
|
74
|
+
const existing = readSettings(root);
|
|
75
|
+
const next = `${JSON.stringify(mergeSettings(existing, hooks, { version, cli: cfg.cli }), null, 2)}\n`;
|
|
76
|
+
const file = path.join(root, SETTINGS);
|
|
77
|
+
const current = exists(file) ? readText(file) : null;
|
|
78
|
+
const refText = `${JSON.stringify({ hooks }, null, 2)}\n`;
|
|
79
|
+
const reference = loadReference(root, SETTINGS);
|
|
80
|
+
const base = { dest: SETTINGS, next, current, reference, refText, noReference: reference === null };
|
|
81
|
+
if (current === null) return { ...base, status: 'missing', diff: '' };
|
|
82
|
+
if (current === next) return { ...base, status: 'same', diff: '' };
|
|
83
|
+
const diff = diffLines(current, next);
|
|
84
|
+
if (!existing?._slicekit) return { ...base, status: 'changed', diff };
|
|
85
|
+
const prefixes = ourPrefixes(existing, cfg.cli);
|
|
86
|
+
let baseline;
|
|
87
|
+
try {
|
|
88
|
+
baseline = reference === null ? { hooks } : JSON.parse(reference);
|
|
89
|
+
} catch {
|
|
90
|
+
baseline = { hooks };
|
|
91
|
+
}
|
|
92
|
+
const drifted = JSON.stringify(ourEntries(existing, prefixes)) !== JSON.stringify(ourEntries(baseline, prefixes));
|
|
93
|
+
return { ...base, status: drifted ? 'drifted' : 'changed', diff };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// opts.entries and opts.templates render from another manifest (tests, future renderers).
|
|
97
|
+
// The settings item is planned only when the template root holds hooks.json (or opts.settings is true):
|
|
98
|
+
// a template root without hooks.json renders files only and never touches .claude/settings.json.
|
|
99
|
+
// Each item carries { dest, status, diff } plus what applyUpdate needs to write it.
|
|
100
|
+
export function planUpdate(root, cfg, { version, entries = entriesForShape(cfg.shape), templates = TEMPLATES, settings }) {
|
|
101
|
+
const opts = { version, templates };
|
|
102
|
+
const items = entries.map((entry) => fileItem(root, cfg, entry, opts));
|
|
103
|
+
if (settings ?? exists(path.join(templates, 'hooks.json'))) items.push(settingsItem(root, cfg, opts));
|
|
104
|
+
return items;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// How a plan item is reported: its status, except that a drifted item with no reference copy is
|
|
108
|
+
// `noReference`, because the verdict was then taken against the kit's current templates and cannot
|
|
109
|
+
// tell a kit change from an edit. The managed check rule reports with the same choice.
|
|
110
|
+
export const reportKind = (item) => (item.status === 'drifted' && item.noReference ? 'noReference' : item.status);
|
|
111
|
+
|
|
112
|
+
const WARNING = {
|
|
113
|
+
drifted: (dest) => `${dest} was edited since slicekit wrote it; left untouched (slicekit update --force overwrites)`,
|
|
114
|
+
noReference: (dest) => `no reference copy for ${dest}; cannot tell a kit change from an edit; slicekit update --force overwrites`,
|
|
115
|
+
unmanaged: (dest) => `${dest} exists and is not managed by slicekit; left untouched`,
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
// Every reference directory except the current version's.
|
|
119
|
+
function pruneReferences(renderedRoot, version) {
|
|
120
|
+
for (const entry of fs.readdirSync(renderedRoot)) {
|
|
121
|
+
if (entry !== version) fs.rmSync(path.join(renderedRoot, entry), { recursive: true, force: true });
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Writes exactly the entries the plan marked changed or missing, and drifted ones only with force.
|
|
126
|
+
// Then writes the new reference directory whole, and only after that prunes the old ones, so an
|
|
127
|
+
// interrupted run leaves a complete reference behind. opts.prune replaces the prune step (tests).
|
|
128
|
+
// Returns { written: [dest], skipped: [{dest, status, warning}] }.
|
|
129
|
+
export function applyUpdate(root, cfg, plan, { version, force = false, prune = pruneReferences }) {
|
|
130
|
+
const written = [];
|
|
131
|
+
const skipped = [];
|
|
132
|
+
const copies = [];
|
|
133
|
+
for (const item of plan) {
|
|
134
|
+
const writable = item.status === 'changed' || item.status === 'missing' || (item.status === 'drifted' && force);
|
|
135
|
+
if (writable) {
|
|
136
|
+
writeText(path.join(root, item.dest), item.next);
|
|
137
|
+
written.push(item.dest);
|
|
138
|
+
copies.push([item.dest, item.refText]);
|
|
139
|
+
} else if (item.status === 'same') {
|
|
140
|
+
copies.push([item.dest, item.dest === SETTINGS ? item.refText : item.current]);
|
|
141
|
+
} else {
|
|
142
|
+
skipped.push({ dest: item.dest, status: item.status, warning: WARNING[reportKind(item)](item.dest) });
|
|
143
|
+
// Carry the old reference forward so the edit is still seen next time.
|
|
144
|
+
if (item.status === 'drifted' && item.reference !== null) copies.push([item.dest, item.reference]);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const renderedRoot = path.join(root, RENDERED);
|
|
148
|
+
for (const [dest, text] of copies) writeText(path.join(renderedRoot, version, dest), text);
|
|
149
|
+
if (exists(renderedRoot)) prune(renderedRoot, version);
|
|
150
|
+
return { written, skipped };
|
|
151
|
+
}
|
package/src/slicefile.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { frontMatter, headings, lines, prose, tables } from './markdown.js';
|
|
4
|
+
import { parseSliceAddr } from './ids.js';
|
|
5
|
+
import { kitPaths } from './paths.js';
|
|
6
|
+
|
|
7
|
+
export class SliceFileError extends Error {
|
|
8
|
+
constructor(message, { path: file, field } = {}) {
|
|
9
|
+
super(`${file ?? '<slice>'}: ${message}`);
|
|
10
|
+
this.name = 'SliceFileError';
|
|
11
|
+
this.path = file;
|
|
12
|
+
this.field = field;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const KINDS = ['happy', 'negative', 'e2e'];
|
|
17
|
+
const SCALARS = ['slice', 'name', 'repo', 'group', 'runbook'];
|
|
18
|
+
const LISTS = ['requirements', 'decisions'];
|
|
19
|
+
|
|
20
|
+
// The text under the first `## <title>` heading, up to the next level-2 heading.
|
|
21
|
+
function section(body, title) {
|
|
22
|
+
const at = sectionAt(body, title);
|
|
23
|
+
return at === null ? null : at.text;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// The same, with the heading's 1-based line in body.
|
|
27
|
+
function sectionAt(body, title) {
|
|
28
|
+
const hs = headings(body).filter((h) => h.level === 2);
|
|
29
|
+
const i = hs.findIndex((h) => h.text.toLowerCase() === title.toLowerCase());
|
|
30
|
+
if (i === -1) return null;
|
|
31
|
+
const all = lines(body);
|
|
32
|
+
const end = i + 1 < hs.length ? hs[i + 1].line - 1 : all.length;
|
|
33
|
+
return { line: hs[i].line, text: all.slice(hs[i].line, end).join('\n') };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const asList = (v) => (Array.isArray(v) ? v : v === '' || v == null ? [] : [String(v)]);
|
|
37
|
+
|
|
38
|
+
const PHASE_DIR = /^P(\d+)(?:\b|[-_])/;
|
|
39
|
+
|
|
40
|
+
// The one place a phase directory's name becomes a phase: `P01-spine` and `P1-spine` are both P1.
|
|
41
|
+
// Returns { n, id, name } or null; `name` is the text after the first dash or underscore, or the
|
|
42
|
+
// whole directory name when there is none.
|
|
43
|
+
export function phaseOfDir(dirName) {
|
|
44
|
+
const m = PHASE_DIR.exec(dirName);
|
|
45
|
+
if (!m) return null;
|
|
46
|
+
const n = Number(m[1]);
|
|
47
|
+
const rest = dirName.slice(m[0].length).replace(/^[-_]/, '');
|
|
48
|
+
return { n, id: `P${n}`, name: rest === '' ? dirName : rest };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function parseSliceFile(text, file) {
|
|
52
|
+
const fail = (message, field) => {
|
|
53
|
+
throw new SliceFileError(message, { path: file, field });
|
|
54
|
+
};
|
|
55
|
+
const { data, body } = frontMatter(text);
|
|
56
|
+
for (const key of [...SCALARS, ...LISTS]) {
|
|
57
|
+
if (!(key in data)) fail(`missing front-matter field "${key}"`, key);
|
|
58
|
+
}
|
|
59
|
+
const id = String(data.slice);
|
|
60
|
+
const addr = parseSliceAddr(id);
|
|
61
|
+
if (!addr) fail(`front-matter field "slice" is not a slice address: ${id}`, 'slice');
|
|
62
|
+
const dirPhase = file ? phaseOfDir(path.basename(path.dirname(file))) : null;
|
|
63
|
+
if (dirPhase && dirPhase.n !== addr.phase) {
|
|
64
|
+
fail(`front-matter field "slice" is ${id}, but ${id} is in the directory of phase ${dirPhase.id}`, 'slice');
|
|
65
|
+
}
|
|
66
|
+
const rb = String(data.runbook).toLowerCase();
|
|
67
|
+
if (!['yes', 'true', 'no', 'false'].includes(rb)) {
|
|
68
|
+
fail(`front-matter field "runbook" must be yes or no, got "${data.runbook}"`, 'runbook');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const done = section(body, 'Done when');
|
|
72
|
+
if (done === null) fail('missing section "## Done when"', 'Done when');
|
|
73
|
+
const doneWhen = lines(done)
|
|
74
|
+
.map((l) => /^\s*(?:\d+[.)]|[-*+])\s+(.*)$/.exec(l))
|
|
75
|
+
.filter(Boolean)
|
|
76
|
+
.map((m) => m[1].trim());
|
|
77
|
+
|
|
78
|
+
const evalText = section(body, 'Evaluation set');
|
|
79
|
+
if (evalText === null) fail('missing section "## Evaluation set"', 'Evaluation set');
|
|
80
|
+
const table = tables(evalText)[0];
|
|
81
|
+
const header = table ? table.header.map((h) => h.toLowerCase()) : [];
|
|
82
|
+
if (!table || header[0] !== 'test' || header[1] !== 'kind' || header[2] !== 'covers') {
|
|
83
|
+
fail('"## Evaluation set" has no table with the columns Test, Kind, Covers', 'Evaluation set');
|
|
84
|
+
}
|
|
85
|
+
if (table.rows.length === 0) fail('"## Evaluation set" table has no rows', 'Evaluation set');
|
|
86
|
+
const evaluation = table.rows.map((row) => {
|
|
87
|
+
const [test = '', kind = '', covers = ''] = row;
|
|
88
|
+
if (!KINDS.includes(kind)) {
|
|
89
|
+
fail(`evaluation row "${test}" has kind "${kind}", expected happy, negative or e2e`, 'Evaluation set');
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
test,
|
|
93
|
+
kind,
|
|
94
|
+
covers: covers.split(',').map((c) => c.trim()).filter((c) => c !== ''),
|
|
95
|
+
};
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// Each backticked path under `## Expected changes`, with its line in the file (front matter
|
|
99
|
+
// included), so a finding about it can point at it.
|
|
100
|
+
const offset = lines(text).length - lines(body).length;
|
|
101
|
+
const changesAt = sectionAt(body, 'Expected changes');
|
|
102
|
+
const expectedChanges = [];
|
|
103
|
+
const expectedChangeLines = [];
|
|
104
|
+
if (changesAt) {
|
|
105
|
+
lines(changesAt.text).forEach((l, k) => {
|
|
106
|
+
for (const m of l.matchAll(/`([^`]+)`/g)) {
|
|
107
|
+
expectedChanges.push(m[1].trim());
|
|
108
|
+
expectedChangeLines.push(offset + changesAt.line + k + 1);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
const expectedChangesLine = changesAt ? offset + changesAt.line : null;
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
id,
|
|
116
|
+
name: String(data.name),
|
|
117
|
+
repo: String(data.repo),
|
|
118
|
+
group: String(data.group),
|
|
119
|
+
runbook: rb === 'yes' || rb === 'true',
|
|
120
|
+
requirements: asList(data.requirements),
|
|
121
|
+
decisions: asList(data.decisions),
|
|
122
|
+
doneWhen,
|
|
123
|
+
evaluation,
|
|
124
|
+
expectedChanges,
|
|
125
|
+
expectedChangeLines,
|
|
126
|
+
expectedChangesLine,
|
|
127
|
+
path: file,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function findSliceFiles(root, cfg) {
|
|
132
|
+
const phases = kitPaths(root, cfg).phases;
|
|
133
|
+
if (!fs.existsSync(phases)) return [];
|
|
134
|
+
const out = [];
|
|
135
|
+
for (const dir of fs.readdirSync(phases, { withFileTypes: true })) {
|
|
136
|
+
const phase = phaseOfDir(dir.name);
|
|
137
|
+
if (!dir.isDirectory() || !phase) continue;
|
|
138
|
+
const phaseDir = path.join(phases, dir.name);
|
|
139
|
+
for (const f of fs.readdirSync(phaseDir)) {
|
|
140
|
+
const sm = /^S-(\d+).*\.md$/.exec(f);
|
|
141
|
+
if (sm) {
|
|
142
|
+
out.push({
|
|
143
|
+
path: path.join(phaseDir, f),
|
|
144
|
+
phaseDir,
|
|
145
|
+
phaseId: phase.id,
|
|
146
|
+
key: [phase.n, Number(sm[1])],
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
out.sort((a, b) => a.key[0] - b.key[0] || a.key[1] - b.key[1]);
|
|
152
|
+
return out.map(({ path: p, phaseDir, phaseId }) => ({ path: p, phaseDir, phaseId }));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function parsePhaseFile(text) {
|
|
156
|
+
for (const h of headings(text)) {
|
|
157
|
+
if (h.level !== 1) continue;
|
|
158
|
+
const m = /^Phase\s+(\d+)\s*(?:—|-|:)\s*(.+)$/.exec(h.text);
|
|
159
|
+
if (m) return { id: `P${m[1]}`, name: m[2].replace(/\s*:\s*implementation plan\s*$/i, '').trim() };
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// The phase file's `**Order:** <step>; <step>; …` line, the first one outside fenced code: each
|
|
165
|
+
// step a list of groups that run in parallel, split on `;` and then `,`, trimmed. Null without
|
|
166
|
+
// one. S-13 validates it; S-14 and S-16 follow it.
|
|
167
|
+
export function parseOrder(phaseText) {
|
|
168
|
+
for (const [line] of prose(phaseText)) {
|
|
169
|
+
const m = /^\*\*Order:\*\*\s*(.+)$/.exec(line);
|
|
170
|
+
if (m) return m[1].split(';').map((step) => step.split(',').map((g) => g.trim()));
|
|
171
|
+
}
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// `spec verify` (REQ-2.3): what the spec stands on before it goes to review. The earlier stages are
|
|
2
|
+
// done, every REQ cites a decision row or an intake file that exists, and every interface named at
|
|
3
|
+
// intake has a contract. A `[NEEDS CLARIFICATION: …]` marker is listed, never a finding: specify
|
|
4
|
+
// may leave one, plan refuses it.
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { citedRefs, intakeExists } from './check/rules/citations.js';
|
|
7
|
+
import { exists, listFiles, readText } from './fs.js';
|
|
8
|
+
import { interfacesWithoutContract, readDecisions } from './decisions.js';
|
|
9
|
+
import { prose } from './markdown.js';
|
|
10
|
+
import { kitPaths } from './paths.js';
|
|
11
|
+
import { clarificationMarkers, programStage, reqBlocks } from './stage.js';
|
|
12
|
+
|
|
13
|
+
const posix = (p) => p.split(path.sep).join('/');
|
|
14
|
+
const mdFiles = (dir) => (exists(dir) ? listFiles(dir, { ext: '.md' }).map((f) => path.join(dir, f)) : []);
|
|
15
|
+
|
|
16
|
+
// Each REQ whose `Cites:` entries name no D row present in decisions.md and no file that exists
|
|
17
|
+
// under `<docsRoot>/intake/`, resolved exactly as check's citation rule resolves them.
|
|
18
|
+
export function unsupportedReqs(root, cfg) {
|
|
19
|
+
const p = kitPaths(root, cfg);
|
|
20
|
+
const ids = new Set(exists(p.decisions) ? readDecisions(readText(p.decisions)).rows.map((r) => r.id) : []);
|
|
21
|
+
const resolves = (r) => (r.kind === 'decision' ? ids.has(r.name) : intakeExists(p.intake, r.name));
|
|
22
|
+
return reqBlocks(root, cfg)
|
|
23
|
+
.filter(({ line, block }) => !citedRefs([...prose(block)].map(([l, n]) => [l, n + line])).some(resolves))
|
|
24
|
+
.map(({ id, file, line }) => ({ req: id, file, line }));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Every clarification marker in the spec's Markdown files, contracts included.
|
|
28
|
+
function markers(root, cfg) {
|
|
29
|
+
return mdFiles(kitPaths(root, cfg).spec).flatMap((f) => {
|
|
30
|
+
const file = posix(path.relative(root, f));
|
|
31
|
+
return clarificationMarkers(readText(f)).map((m) => ({ file, ...m }));
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function verifySpec(root, cfg) {
|
|
36
|
+
const findings = [];
|
|
37
|
+
const now = programStage(root, cfg, { upTo: 'specify' });
|
|
38
|
+
if (!now.complete && now.stage !== 'specify') {
|
|
39
|
+
for (const b of now.blockers) findings.push({ file: '-', line: 0, message: `${now.stage} not done: ${b}` });
|
|
40
|
+
}
|
|
41
|
+
for (const r of unsupportedReqs(root, cfg)) {
|
|
42
|
+
findings.push({ file: r.file, line: r.line, message: `${r.req} cites nothing that exists; write [NEEDS CLARIFICATION: <question>] instead of a guess` });
|
|
43
|
+
}
|
|
44
|
+
const p = kitPaths(root, cfg);
|
|
45
|
+
if (exists(p.decisions)) {
|
|
46
|
+
const where = posix(path.relative(root, p.decisions));
|
|
47
|
+
const contracts = `${posix(path.relative(root, p.contracts))}/`;
|
|
48
|
+
for (const i of interfacesWithoutContract(root, cfg)) {
|
|
49
|
+
findings.push({ file: where, line: i.line, message: `interface ${i.name} (${i.id}) has no contract under ${contracts}` });
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const found = markers(root, cfg);
|
|
53
|
+
return {
|
|
54
|
+
findings,
|
|
55
|
+
counts: { reqs: reqBlocks(root, cfg).length, contracts: mdFiles(kitPaths(root, cfg).contracts).length, markers: found.length },
|
|
56
|
+
markers: found,
|
|
57
|
+
};
|
|
58
|
+
}
|