slicekit 0.0.0-stage → 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.
Files changed (170) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +253 -2
  3. package/bin/slicekit.js +4 -0
  4. package/package.json +23 -4
  5. package/src/adapters/githost/errors.js +17 -0
  6. package/src/adapters/githost/fixture.js +136 -0
  7. package/src/adapters/githost/gh-json.js +99 -0
  8. package/src/adapters/githost/github.js +127 -0
  9. package/src/adapters/githost/index.js +12 -0
  10. package/src/adapters/tracker/errors.js +53 -0
  11. package/src/adapters/tracker/fixture.js +71 -0
  12. package/src/adapters/tracker/github-issues.js +65 -0
  13. package/src/adapters/tracker/index.js +14 -0
  14. package/src/baseline.js +109 -0
  15. package/src/build/commit.js +201 -0
  16. package/src/build/decline.js +81 -0
  17. package/src/build/finish.js +155 -0
  18. package/src/build/order.js +158 -0
  19. package/src/build/overlap.js +173 -0
  20. package/src/build/parallel.js +50 -0
  21. package/src/build/pr.js +263 -0
  22. package/src/build/record.js +28 -0
  23. package/src/build/red.js +207 -0
  24. package/src/build/review.js +123 -0
  25. package/src/build/sync.js +47 -0
  26. package/src/build/verdict.js +410 -0
  27. package/src/build/worktree.js +169 -0
  28. package/src/check/ci-event.js +52 -0
  29. package/src/check/context.js +129 -0
  30. package/src/check/index.js +140 -0
  31. package/src/check/rules/.gitkeep +0 -0
  32. package/src/check/rules/ci-body.js +22 -0
  33. package/src/check/rules/ci-title.js +43 -0
  34. package/src/check/rules/citations.js +224 -0
  35. package/src/check/rules/commands.js +146 -0
  36. package/src/check/rules/coverage.js +248 -0
  37. package/src/check/rules/hooks.js +58 -0
  38. package/src/check/rules/journal.js +17 -0
  39. package/src/check/rules/layout.js +56 -0
  40. package/src/check/rules/managed.js +31 -0
  41. package/src/check/rules/queue.js +106 -0
  42. package/src/check/rules/references.js +249 -0
  43. package/src/check/rules/style.js +177 -0
  44. package/src/check/rules/trailer.js +67 -0
  45. package/src/cli/baseline.js +66 -0
  46. package/src/cli/branch.js +86 -0
  47. package/src/cli/build.js +239 -0
  48. package/src/cli/check.js +40 -0
  49. package/src/cli/decide.js +59 -0
  50. package/src/cli/gaps.js +73 -0
  51. package/src/cli/hook.js +28 -0
  52. package/src/cli/index.js +83 -0
  53. package/src/cli/init.js +96 -0
  54. package/src/cli/instructions.js +63 -0
  55. package/src/cli/intake.js +107 -0
  56. package/src/cli/journal.js +82 -0
  57. package/src/cli/plan.js +29 -0
  58. package/src/cli/pr.js +83 -0
  59. package/src/cli/process.js +59 -0
  60. package/src/cli/ready-label.js +119 -0
  61. package/src/cli/review.js +64 -0
  62. package/src/cli/spec.js +31 -0
  63. package/src/cli/stage.js +37 -0
  64. package/src/cli/state.js +106 -0
  65. package/src/cli/status.js +108 -0
  66. package/src/cli/update.js +62 -0
  67. package/src/cli/workflow.js +48 -0
  68. package/src/config.js +203 -0
  69. package/src/decisions.js +289 -0
  70. package/src/evidence.js +27 -0
  71. package/src/fs.js +38 -0
  72. package/src/gaps.js +271 -0
  73. package/src/git.js +209 -0
  74. package/src/githooks.js +37 -0
  75. package/src/hook-cli.js +63 -0
  76. package/src/hooks/io.js +205 -0
  77. package/src/hooks/paths.js +82 -0
  78. package/src/hooks/pre-bash.js +715 -0
  79. package/src/hooks/pre-compact.js +139 -0
  80. package/src/hooks/pre-edit.js +170 -0
  81. package/src/hooks/session-end.js +118 -0
  82. package/src/hooks/session-start.js +209 -0
  83. package/src/hooks/shapes.js +103 -0
  84. package/src/hooks/stop.js +37 -0
  85. package/src/hooks/subagent-marker.js +88 -0
  86. package/src/hooks/subagent-start.js +100 -0
  87. package/src/hooks/subagent-stop.js +106 -0
  88. package/src/ids.js +43 -0
  89. package/src/init.js +276 -0
  90. package/src/instructions.js +235 -0
  91. package/src/intake.js +409 -0
  92. package/src/journal.js +326 -0
  93. package/src/lock.js +35 -0
  94. package/src/markdown.js +155 -0
  95. package/src/paths.js +33 -0
  96. package/src/plan.js +25 -0
  97. package/src/pr.js +73 -0
  98. package/src/prbody.js +266 -0
  99. package/src/ready-label.js +107 -0
  100. package/src/redact.js +33 -0
  101. package/src/render/compare.js +23 -0
  102. package/src/render/entry.js +101 -0
  103. package/src/render/index.js +27 -0
  104. package/src/render/managed.js +128 -0
  105. package/src/render/manifest.js +24 -0
  106. package/src/render/settings.js +76 -0
  107. package/src/render/update.js +151 -0
  108. package/src/slicefile.js +173 -0
  109. package/src/spec-verify.js +58 -0
  110. package/src/stage.js +325 -0
  111. package/src/state/build.js +156 -0
  112. package/src/state/covers.js +58 -0
  113. package/src/state/index.js +191 -0
  114. package/src/state/sync.js +64 -0
  115. package/src/state/transitions.js +282 -0
  116. package/src/verdict.js +203 -0
  117. package/src/which.js +37 -0
  118. package/src/workflow.js +177 -0
  119. package/src/worktrees.js +68 -0
  120. package/templates/claude-code/agents/auditor.md +7 -0
  121. package/templates/claude-code/agents/designer.md +7 -0
  122. package/templates/claude-code/agents/developer.md +7 -0
  123. package/templates/claude-code/agents/fixer.md +7 -0
  124. package/templates/claude-code/agents/ops.md +7 -0
  125. package/templates/claude-code/agents/reviewer.md +7 -0
  126. package/templates/claude-code/commands/slicekit-audit.md +7 -0
  127. package/templates/claude-code/commands/slicekit-build.md +7 -0
  128. package/templates/claude-code/commands/slicekit-gaps.md +6 -0
  129. package/templates/claude-code/commands/slicekit-intake.md +7 -0
  130. package/templates/claude-code/commands/slicekit-ops.md +7 -0
  131. package/templates/claude-code/commands/slicekit-plan.md +6 -0
  132. package/templates/claude-code/commands/slicekit-release.md +7 -0
  133. package/templates/claude-code/commands/slicekit-review.md +7 -0
  134. package/templates/claude-code/commands/slicekit-specify.md +6 -0
  135. package/templates/claude-code/commands/slicekit.md +7 -0
  136. package/templates/claude-code/hooks.json +90 -0
  137. package/templates/claude-code/manifest.json +171 -0
  138. package/templates/claude-code/skills/slicekit-ids/SKILL.md +16 -0
  139. package/templates/claude-code/skills/slicekit-loop/SKILL.md +23 -0
  140. package/templates/claude-code/skills/slicekit-writing/SKILL.md +24 -0
  141. package/templates/docs/BUILD.md +5 -0
  142. package/templates/docs/decisions.md +15 -0
  143. package/templates/docs/intake/.keep +0 -0
  144. package/templates/docs/observability.md +25 -0
  145. package/templates/docs/phases/.keep +0 -0
  146. package/templates/docs/platform.md +25 -0
  147. package/templates/docs/releases/.keep +0 -0
  148. package/templates/docs/runbook.md +21 -0
  149. package/templates/docs/spec/00-overview.md +8 -0
  150. package/templates/docs/spec/contracts/.keep +0 -0
  151. package/templates/docs/spec/gaps.md +6 -0
  152. package/templates/docs/spec/stack.md +3 -0
  153. package/templates/githooks/pre-commit +59 -0
  154. package/templates/githooks/pre-merge-commit +59 -0
  155. package/templates/githooks/prepare-commit-msg +74 -0
  156. package/templates/instructions/agents/auditor.md +33 -0
  157. package/templates/instructions/agents/designer.md +21 -0
  158. package/templates/instructions/agents/developer.md +49 -0
  159. package/templates/instructions/agents/fixer.md +41 -0
  160. package/templates/instructions/agents/ops.md +21 -0
  161. package/templates/instructions/agents/reviewer.md +63 -0
  162. package/templates/instructions/commands/build.md +234 -0
  163. package/templates/instructions/commands/gaps.md +65 -0
  164. package/templates/instructions/commands/intake.md +90 -0
  165. package/templates/instructions/commands/plan.md +60 -0
  166. package/templates/instructions/commands/review.md +117 -0
  167. package/templates/instructions/commands/specify.md +49 -0
  168. package/templates/style.json +11 -0
  169. package/templates/workflow/pins.json +6 -0
  170. package/templates/workflow/slicekit.yml +110 -0
@@ -0,0 +1,63 @@
1
+ import { parseArgs } from 'node:util';
2
+ import { ConfigError, loadConfig } from '../config.js';
3
+ import { InstructionsError, briefSlice, checkRole, renderBrief, renderProcedure, renderSummary } from '../instructions.js';
4
+
5
+ const USAGE =
6
+ 'instructions <role> [P<n>/S-<nn>] [--worktree <path> | --summary]\n' +
7
+ ' slicekit instructions --command <name> [args...]';
8
+
9
+ const usage = (io, why) => {
10
+ io.stderr(`${why ? `slicekit: ${why}\n` : ''}usage: slicekit ${USAGE}\n`);
11
+ return 2;
12
+ };
13
+
14
+ // Prints an agent's brief, its short form (`--summary`, which subagent-start injects) or a command's
15
+ // procedure (REQ-2.12). Everything after `--command <name>`
16
+ // goes to the procedure unparsed. Exit 1 when the template is not built, 2 on usage or an unknown name.
17
+ async function run(args, io) {
18
+ let produce;
19
+ if (args[0] === '--command') {
20
+ const [, name, ...rest] = args;
21
+ if (name === undefined) return usage(io, 'missing command name');
22
+ produce = (cfg) => renderProcedure(io.cwd, cfg, name, rest);
23
+ } else {
24
+ let values;
25
+ let positionals;
26
+ try {
27
+ ({ values, positionals } = parseArgs({ args, options: { worktree: { type: 'string' }, summary: { type: 'boolean' } }, allowPositionals: true }));
28
+ } catch (e) {
29
+ return usage(io, e.message);
30
+ }
31
+ if (positionals.length === 0) return usage(io, 'missing role');
32
+ if (positionals.length > 2) return usage(io, `unexpected argument ${positionals[2]}`);
33
+ if (values.worktree !== undefined && values.worktree.trim() === '') return usage(io, '--worktree needs a path');
34
+ if (values.summary && values.worktree !== undefined) return usage(io, '--summary takes no --worktree');
35
+ const [role, slice] = positionals;
36
+ // The role is checked before the slice is resolved, so a bad role is the error a caller sees.
37
+ produce = (cfg) => {
38
+ checkRole(cfg, role);
39
+ const id = briefSlice(io.cwd, slice);
40
+ const fromBranch = slice === undefined;
41
+ return values.summary
42
+ ? renderSummary(io.cwd, cfg, role, id, { fromBranch })
43
+ : renderBrief(io.cwd, cfg, role, id, { worktree: values.worktree, fromBranch });
44
+ };
45
+ }
46
+ try {
47
+ const text = produce(loadConfig(io.cwd));
48
+ io.stdout(text.endsWith('\n') ? text : `${text}\n`);
49
+ return 0;
50
+ } catch (e) {
51
+ if (e instanceof InstructionsError) {
52
+ io.stderr(`slicekit: ${e.message}\n`);
53
+ return e.code;
54
+ }
55
+ if (e instanceof ConfigError) {
56
+ io.stderr(`slicekit: ${e.message}\n`);
57
+ return 2;
58
+ }
59
+ throw e;
60
+ }
61
+ }
62
+
63
+ export const instructionsCommand = { run, usage: USAGE };
@@ -0,0 +1,107 @@
1
+ // `slicekit intake snapshot <file|url>` (REQ-1.2): prints the snapshot's path relative to root.
2
+ // `ask`, `answer` and `done` (REQ-2.2) hold the interview: `ask` prints `question <n>`, `answer`
3
+ // the D-ID it wrote, `done` one `open: <D-ID> <question>` line per open row, or `open: none`.
4
+ import path from 'node:path';
5
+ import { parseArgs } from 'node:util';
6
+ import { ConfigError, loadConfig } from '../config.js';
7
+ import { DecisionError } from '../decisions.js';
8
+ import { GitError } from '../git.js';
9
+ import { IntakeError, answer, ask, done, snapshot } from '../intake.js';
10
+
11
+ const SUBCOMMANDS = ['snapshot', 'ask', 'answer', 'done'];
12
+ const ISO = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/;
13
+ const USAGE =
14
+ 'intake snapshot <file|url> [--slug <s>] [--who <w>]\n' +
15
+ ' slicekit intake ask --pass <product|platform> --topic <t> --question <q>\n' +
16
+ ' slicekit intake answer (--answer <a> [--type <T>] | --open --why <w> --recommendation <r>) [--who <w>] [--interface <name> [--source <file>:<line>]]\n' +
17
+ ' slicekit intake done';
18
+
19
+ // The options each subcommand takes, and the positionals it wants after its name.
20
+ const SPEC = {
21
+ snapshot: { options: ['slug', 'who'], positionals: 1 },
22
+ ask: { options: ['pass', 'topic', 'question'], positionals: 0 },
23
+ answer: { options: ['answer', 'type', 'open', 'why', 'recommendation', 'who', 'interface', 'source'], positionals: 0 },
24
+ done: { options: [], positionals: 0 },
25
+ };
26
+ const STRINGS = ['slug', 'who', 'pass', 'topic', 'question', 'answer', 'type', 'why', 'recommendation', 'interface', 'source'];
27
+ const OPTIONS = { ...Object.fromEntries(STRINGS.map((k) => [k, { type: 'string' }])), open: { type: 'boolean' } };
28
+
29
+ const usage = (io, why) => {
30
+ io.stderr(`${why ? `slicekit: ${why}\n` : ''}usage: slicekit ${USAGE}\n`);
31
+ return 2;
32
+ };
33
+
34
+ // SLICEKIT_NOW is for tests only: it fixes the instant a snapshot is named and stamped with, and
35
+ // the date on a decision row.
36
+ function clock(io) {
37
+ const fixed = (io.env ?? process.env).SLICEKIT_NOW;
38
+ if (fixed === undefined || fixed === '') return new Date();
39
+ const now = new Date(fixed);
40
+ return ISO.test(fixed) && !Number.isNaN(now.getTime()) ? now : null;
41
+ }
42
+
43
+ async function perform(sub, root, cfg, values, rest, now, io) {
44
+ if (sub === 'snapshot') {
45
+ const { file } = await snapshot(root, cfg, rest[0], { slug: values.slug, who: values.who, now });
46
+ io.stdout(`${path.relative(root, file).split(path.sep).join('/')}\n`);
47
+ } else if (sub === 'ask') {
48
+ const { n } = ask(root, cfg, { pass: values.pass, topic: values.topic, question: values.question, now });
49
+ io.stdout(`question ${n}\n`);
50
+ } else if (sub === 'answer') {
51
+ const { interface: name, source, ...fields } = values;
52
+ const { id } = answer(root, cfg, { ...fields, open: Boolean(values.open), ...(name !== undefined ? { iface: { name, source } } : {}), now });
53
+ io.stdout(`${id}\n`);
54
+ } else {
55
+ const { open } = done(root, cfg);
56
+ io.stdout(open.length === 0 ? 'open: none\n' : open.map((r) => `open: ${r.id} ${r.question}\n`).join(''));
57
+ }
58
+ }
59
+
60
+ async function run(args, io) {
61
+ let values;
62
+ let positionals;
63
+ try {
64
+ ({ values, positionals } = parseArgs({ args, options: OPTIONS, allowPositionals: true }));
65
+ } catch (e) {
66
+ return usage(io, e.message);
67
+ }
68
+ const [sub, ...rest] = positionals;
69
+ const spec = Object.hasOwn(SPEC, sub ?? '') ? SPEC[sub] : undefined;
70
+ if (!spec || rest.length !== spec.positionals) return usage(io);
71
+ const extra = Object.keys(values).filter((k) => !spec.options.includes(k));
72
+ if (extra.length > 0) return usage(io, `${sub} does not take --${extra[0]}`);
73
+ if (sub === 'ask' && ['pass', 'topic', 'question'].some((k) => values[k] === undefined)) return usage(io);
74
+ if (sub === 'answer') {
75
+ if (values.source !== undefined && values.interface === undefined) return usage(io, '--source needs --interface');
76
+ if (!values.open && (values.why !== undefined || values.recommendation !== undefined)) {
77
+ return usage(io, '--why and --recommendation go with --open');
78
+ }
79
+ if (values.open ? values.answer !== undefined || values.why === undefined || values.recommendation === undefined : values.answer === undefined) {
80
+ return usage(io);
81
+ }
82
+ }
83
+ const now = clock(io);
84
+ if (now === null) return usage(io, `SLICEKIT_NOW '${(io.env ?? process.env).SLICEKIT_NOW}' is not an ISO time`);
85
+ const root = io.cwd;
86
+ try {
87
+ const cfg = loadConfig(root);
88
+ await perform(sub, root, cfg, values, rest, now, io);
89
+ return 0;
90
+ } catch (e) {
91
+ if (e instanceof IntakeError || e instanceof DecisionError) {
92
+ io.stderr(`slicekit: ${e.message}\n`);
93
+ return e.code;
94
+ }
95
+ if (e instanceof ConfigError) {
96
+ io.stderr(`slicekit: ${e.message}\n`);
97
+ return 2;
98
+ }
99
+ if (e instanceof GitError) {
100
+ io.stderr(`slicekit: ${e.message}\n`);
101
+ return 1;
102
+ }
103
+ throw e;
104
+ }
105
+ }
106
+
107
+ export const intakeCommand = { run, usage: USAGE, subcommands: SUBCOMMANDS };
@@ -0,0 +1,82 @@
1
+ import fs from 'node:fs';
2
+ import { parseArgs } from 'node:util';
3
+ import { loadConfig } from '../config.js';
4
+ import { appendOnlyProblems, journalBranch, journalFile, verifyJournal } from '../journal.js';
5
+
6
+ const USAGE = 'journal verify [--against <ref>] | journal show [-n 10] [--branch <b>]';
7
+ const NO_ENTRIES = 'journal: no entries\n';
8
+
9
+ // The config when there is one; otherwise the defaults the journal needs (docsRoot, defaultBranch).
10
+ function configOf(root) {
11
+ try {
12
+ return loadConfig(root);
13
+ } catch {
14
+ return { docsRoot: 'docs', defaultBranch: 'main' };
15
+ }
16
+ }
17
+
18
+ // `slicekit journal verify [--against <ref>]` (REQ-3.8): prints every problem and exits 1 on a broken
19
+ // chain, an unexpected entry, or a committed journal file shortened or removed since the merge base with
20
+ // <ref> (default: the default branch); 0 otherwise. Never writes.
21
+ function verify(root, args, io) {
22
+ let values;
23
+ try {
24
+ ({ values } = parseArgs({ args, options: { against: { type: 'string' } }, allowPositionals: false }));
25
+ } catch (e) {
26
+ io.stderr(`slicekit: ${e.message}\nusage: slicekit ${USAGE}\n`);
27
+ return 2;
28
+ }
29
+ const cfg = configOf(root);
30
+ const r = verifyJournal(root, cfg);
31
+ const problems = [...r.problems, ...appendOnlyProblems(root, cfg, { against: values.against })];
32
+ if (problems.length > 0) {
33
+ io.stdout(`${problems.map((p) => p.message).join('\n')}\n`);
34
+ return 1;
35
+ }
36
+ if (r.lines === 0) {
37
+ io.stdout(NO_ENTRIES);
38
+ return 0;
39
+ }
40
+ io.stdout(`journal: ${r.lines} lines in ${r.files.length} files verified\n`);
41
+ return 0;
42
+ }
43
+
44
+ // `slicekit journal show`: the last n lines of this clone's file for one branch (default: the current one).
45
+ function show(root, args, io) {
46
+ let values;
47
+ try {
48
+ ({ values } = parseArgs({ args, options: { n: { type: 'string', short: 'n' }, branch: { type: 'string' } }, allowPositionals: false }));
49
+ } catch (e) {
50
+ io.stderr(`slicekit: ${e.message}\nusage: slicekit ${USAGE}\n`);
51
+ return 2;
52
+ }
53
+ const n = values.n === undefined ? 10 : Number(values.n);
54
+ if (!Number.isInteger(n) || n < 1) {
55
+ io.stderr(`slicekit: -n must be a positive whole number\nusage: slicekit ${USAGE}\n`);
56
+ return 2;
57
+ }
58
+ let text = '';
59
+ try {
60
+ const file = journalFile(root, values.branch ?? journalBranch(root));
61
+ text = file === null ? '' : fs.readFileSync(file, 'utf8');
62
+ } catch (e) {
63
+ if (e.code !== 'ENOENT') throw e;
64
+ }
65
+ const lines = text.split('\n').map((l) => l.replace(/\r$/, '')).filter((l) => l !== '');
66
+ if (lines.length === 0) {
67
+ io.stdout(NO_ENTRIES);
68
+ return 0;
69
+ }
70
+ io.stdout(`${lines.slice(-n).join('\n')}\n`);
71
+ return 0;
72
+ }
73
+
74
+ async function run(args, io) {
75
+ const [sub, ...rest] = args;
76
+ if (sub === 'verify') return verify(io.cwd, rest, io);
77
+ if (sub === 'show') return show(io.cwd, rest, io);
78
+ io.stderr(`usage: slicekit ${USAGE}\n`);
79
+ return 2;
80
+ }
81
+
82
+ export const journalCommand = { run, usage: USAGE };
@@ -0,0 +1,29 @@
1
+ // `slicekit plan check` (REQ-2.5): exit 0 with `ready to plan`, or exit 1 with one blocker per line.
2
+ import { ConfigError, loadConfig } from '../config.js';
3
+ import { planBlockers } from '../plan.js';
4
+
5
+ const USAGE = 'plan check';
6
+
7
+ async function run(args, io) {
8
+ if (args.length !== 1 || args[0] !== 'check') {
9
+ io.stderr(`usage: slicekit ${USAGE}\n`);
10
+ return 2;
11
+ }
12
+ let cfg;
13
+ try {
14
+ cfg = loadConfig(io.cwd);
15
+ } catch (e) {
16
+ if (!(e instanceof ConfigError)) throw e;
17
+ io.stderr(`slicekit: ${e.message}\n`);
18
+ return 2;
19
+ }
20
+ const blockers = planBlockers(io.cwd, cfg);
21
+ if (blockers.length > 0) {
22
+ io.stdout(blockers.map((b) => `${b}\n`).join(''));
23
+ return 1;
24
+ }
25
+ io.stdout('ready to plan\n');
26
+ return 0;
27
+ }
28
+
29
+ export const planCommand = { run, usage: USAGE, subcommands: ['check'] };
package/src/cli/pr.js ADDED
@@ -0,0 +1,83 @@
1
+ // `slicekit pr open` (02 REQ-2.10, REQ-2.1): commits the named paths, pushes the branch, and opens
2
+ // its pull request through the git host (C-1), or updates the body of the one already open. The
3
+ // command owns git (D-19). Exit 0 with `#<n> <url>` or `updated #<n> <url>`; 1 when git, the host
4
+ // or the body refuses; 2 on bad usage.
5
+ import fs from 'node:fs';
6
+ import path from 'node:path';
7
+ import { parseArgs } from 'node:util';
8
+ import { ConfigError, loadConfig } from '../config.js';
9
+ import { GitError } from '../git.js';
10
+ import { getHost } from '../adapters/githost/index.js';
11
+ import { GitHostError } from '../adapters/githost/errors.js';
12
+ import { PrError, openOrUpdate } from '../pr.js';
13
+
14
+ const USAGE = 'pr open --title <t> --body-file <f> [--commit <message> --paths <a,b>] [--draft] [--base <b>]';
15
+ const OPTIONS = {
16
+ title: { type: 'string' },
17
+ 'body-file': { type: 'string' },
18
+ commit: { type: 'string' },
19
+ paths: { type: 'string' },
20
+ base: { type: 'string' },
21
+ draft: { type: 'boolean' },
22
+ };
23
+
24
+ const usage = (io, why) => {
25
+ io.stderr(`${why ? `slicekit: ${why}\n` : ''}usage: slicekit ${USAGE}\n`);
26
+ return 2;
27
+ };
28
+
29
+ const fail = (io, message, code) => {
30
+ for (const line of message.split('\n')) io.stderr(`slicekit: ${line}\n`);
31
+ return code;
32
+ };
33
+
34
+ async function run(args, io) {
35
+ let values;
36
+ let positionals;
37
+ try {
38
+ ({ values, positionals } = parseArgs({ args, options: OPTIONS, allowPositionals: true }));
39
+ } catch (e) {
40
+ return usage(io, e.message);
41
+ }
42
+ if (positionals.length !== 1 || positionals[0] !== 'open') return usage(io);
43
+ if (values.title === undefined || values['body-file'] === undefined) return usage(io);
44
+ if ((values.commit === undefined) !== (values.paths === undefined)) return usage(io, '--commit and --paths go together');
45
+ const paths = values.paths === undefined ? undefined : values.paths.split(',').map((p) => p.trim()).filter((p) => p !== '');
46
+ if (paths !== undefined && paths.length === 0) return usage(io, '--paths names no path');
47
+ if (values.commit !== undefined && values.commit.trim() === '') return usage(io, '--commit needs a message');
48
+
49
+ const root = io.cwd;
50
+ let bytes;
51
+ try {
52
+ bytes = fs.readFileSync(path.resolve(root, values['body-file']));
53
+ } catch (e) {
54
+ return usage(io, `cannot read the body file ${values['body-file']}: ${e.code ?? e.message}`);
55
+ }
56
+ let body;
57
+ try {
58
+ body = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
59
+ } catch {
60
+ return fail(io, `body file is not UTF-8: ${values['body-file']}`, 2);
61
+ }
62
+ try {
63
+ const cfg = loadConfig(root);
64
+ const host = getHost(cfg, io.env ?? process.env, root);
65
+ const r = openOrUpdate(root, cfg, host, {
66
+ title: values.title,
67
+ body,
68
+ base: values.base,
69
+ draft: values.draft,
70
+ commit: values.commit === undefined ? undefined : { message: values.commit, paths },
71
+ });
72
+ if (r.committed === null) io.stderr(`slicekit: nothing to commit for ${paths.join(', ')}\n`);
73
+ io.stdout(`${r.created ? '' : 'updated '}#${r.number} ${r.url}\n`);
74
+ return 0;
75
+ } catch (e) {
76
+ if (e instanceof PrError) return fail(io, e.message, e.code);
77
+ if (e instanceof ConfigError) return fail(io, e.message, 2);
78
+ if (e instanceof GitError || e instanceof GitHostError) return fail(io, e.message, 1);
79
+ throw e;
80
+ }
81
+ }
82
+
83
+ export const prCommand = { run, usage: USAGE, subcommands: ['open'] };
@@ -0,0 +1,59 @@
1
+ import { main } from './index.js';
2
+ import { FAIL_OPEN, failureOutput } from '../hooks/io.js';
3
+
4
+ async function readStdin() {
5
+ const chunks = [];
6
+ for await (const chunk of process.stdin) chunks.push(chunk);
7
+ return Buffer.concat(chunks).toString('utf8');
8
+ }
9
+
10
+ const write = (stream, s) => new Promise((resolve) => stream.write(s, () => resolve()));
11
+
12
+ // The process entry behind bin/slicekit.js. `stdin` and `loadHook` are seams for tests only.
13
+ export async function runProcess(argv, { stdin = readStdin, loadHook } = {}) {
14
+ if (argv[0] !== 'hook') {
15
+ const io = {
16
+ cwd: process.cwd(),
17
+ stdout: (s) => process.stdout.write(s),
18
+ stderr: (s) => process.stderr.write(s),
19
+ stdin,
20
+ env: process.env,
21
+ };
22
+ process.exitCode = await main(argv, io);
23
+ return;
24
+ }
25
+ // For a hook, a write resolves once flushed, so process.exit below cannot cut the answer short.
26
+ const io = {
27
+ cwd: process.cwd(),
28
+ stdout: (s) => write(process.stdout, s),
29
+ stderr: (s) => write(process.stderr, s),
30
+ stdin,
31
+ env: process.env,
32
+ loadHook,
33
+ };
34
+
35
+ // A hook answers exactly once and then ends the process itself, so nothing the handler left behind
36
+ // (a late rejection, a stray throw, a promise that never settles) can change the exit code or the answer.
37
+ const name = argv[1];
38
+ let answered = false;
39
+ const finish = async (code) => {
40
+ answered = true;
41
+ await write(process.stdout, '');
42
+ process.exit(code);
43
+ };
44
+ const late = (err) => {
45
+ process.stderr.write(`slicekit hook ${name}: ignored after the answer: ${err?.message ?? err}\n`);
46
+ };
47
+ process.on('unhandledRejection', late);
48
+ process.on('uncaughtException', late);
49
+ // The loop emptied with the handler still pending (nothing holds it open): answer as a failure.
50
+ process.on('beforeExit', () => {
51
+ if (answered) return;
52
+ answered = true;
53
+ if (FAIL_OPEN.has(name)) process.stderr.write(`slicekit hook ${name} failed: handler never settled
54
+ `);
55
+ process.stdout.write(`${failureOutput(name, 'handler never settled')}\n`, () => process.exit(0));
56
+ });
57
+ const code = await main(argv, io);
58
+ if (!answered) await finish(code);
59
+ }
@@ -0,0 +1,119 @@
1
+ // `slicekit ready-label` (03 REQ-3.11): the ready-label job of the generated workflow. It reads the
2
+ // pull request from the GitHub event, decides, prints the decision, and with GH_TOKEN set adds or
3
+ // removes the `ready` label through the git host. It always exits 0: the label is information.
4
+ import fs from 'node:fs';
5
+ import path from 'node:path';
6
+ import { parseArgs } from 'node:util';
7
+ import { loadConfig } from '../config.js';
8
+ import { stateFileName } from '../ids.js';
9
+ import { kitPaths } from '../paths.js';
10
+ import { EXEMPT_TITLE, readCiEvent } from '../check/ci-event.js';
11
+ import { titleSlice } from '../check/rules/ci-title.js';
12
+ import { readSlice } from '../state/index.js';
13
+ import { READY_LABEL, READY_LABEL_COLOR, READY_LABEL_DESCRIPTION, decideReady, withBaseReviewMode } from '../ready-label.js';
14
+ import { getHost } from '../adapters/githost/index.js';
15
+
16
+ const USAGE = 'ready-label';
17
+
18
+ // A workflow command's message: GitHub reads %, CR and LF as escapes, so they are written escaped.
19
+ const annotation = (text) => text.replace(/%/g, '%25').replace(/\r/g, '%0D').replace(/\n/g, '%0A');
20
+
21
+ const tryConfig = (root) => {
22
+ try {
23
+ return loadConfig(root);
24
+ } catch {
25
+ return undefined;
26
+ }
27
+ };
28
+
29
+ // The decision for one pull request, as { slice, ready, reason }. Anything that stops a decision
30
+ // being made is a "not ready" with that reason.
31
+ function decide(root, env, pr) {
32
+ if (EXEMPT_TITLE.test(pr.title)) {
33
+ return { slice: null, ready: false, reason: 'the title names no slice (a [spec] or [plan] pull request)' };
34
+ }
35
+ const slice = titleSlice(pr.title);
36
+ if (!slice) return { slice: null, ready: false, reason: 'the title names no slice' };
37
+ // Fail closed: only the workflow's explicit report that every needed job passed counts.
38
+ if (env.SLICEKIT_CHECKS_OK !== 'true') return { slice, ready: false, reason: 'a required check did not pass' };
39
+ try {
40
+ const cfg = loadConfig(root);
41
+ if (!fs.existsSync(path.join(kitPaths(root, cfg).state, stateFileName(slice)))) {
42
+ return { slice, ready: false, reason: `slice ${slice} has no state file` };
43
+ }
44
+ const state = readSlice(root, cfg, slice);
45
+ // review.independent comes from the base branch on origin, never from the pull request's own
46
+ // checkout, so a pull request cannot switch off its independent-review gate. GitHub names the
47
+ // base in GITHUB_BASE_REF; the config's defaultBranch is the fallback.
48
+ const gate = withBaseReviewMode(root, cfg, env.GITHUB_BASE_REF || cfg.defaultBranch);
49
+ return { slice, ...decideReady(state, gate, { headSha: pr.headSha ?? undefined, number: pr.number, root }) };
50
+ } catch (e) {
51
+ return { slice, ready: false, reason: e.message };
52
+ }
53
+ }
54
+
55
+ async function run(args, io) {
56
+ try {
57
+ parseArgs({ args, options: {}, allowPositionals: false });
58
+ } catch (e) {
59
+ io.stderr(`slicekit: ${e.message}\nusage: slicekit ${USAGE}\n`);
60
+ return 0;
61
+ }
62
+ const root = io.cwd;
63
+ const env = io.env ?? process.env;
64
+ let pr;
65
+ try {
66
+ pr = readCiEvent(env);
67
+ } catch (e) {
68
+ io.stderr(`ready-label: ${e.message}\n`);
69
+ return 0;
70
+ }
71
+ if (!pr) {
72
+ io.stdout('ready-label: not a pull request; nothing to do\n');
73
+ return 0;
74
+ }
75
+ const d = decide(root, env, pr);
76
+ io.stdout(`ready-label: ${d.slice ?? 'pull request'} ${d.ready ? 'ready' : 'not ready'}: ${d.reason}\n`);
77
+ // Adding is idempotent, so a ready decision always adds; a removal needs the label to be there.
78
+ if (!d.ready && !pr.labels.includes(READY_LABEL)) return 0;
79
+ if (!env.GH_TOKEN) {
80
+ io.stdout('ready-label: GH_TOKEN not set; label left unchanged\n');
81
+ return 0;
82
+ }
83
+ if (!Number.isInteger(pr.number)) {
84
+ io.stdout('ready-label: the event carries no pull request number; label left unchanged\n');
85
+ return 0;
86
+ }
87
+ // A label call that fails is reported on stderr and as a GitHub warning annotation on stdout.
88
+ const warn = (what, e) => {
89
+ const message = `could not ${what} the label: ${e.message}`;
90
+ io.stderr(`ready-label: ${message}\n`);
91
+ io.stdout(`::warning title=slicekit ready-label::${annotation(message)}\n`);
92
+ };
93
+ const action = d.ready ? 'add' : 'remove';
94
+ let host;
95
+ try {
96
+ host = getHost(tryConfig(root), env, root);
97
+ } catch (e) {
98
+ warn(action, e);
99
+ return 0;
100
+ }
101
+ // The repository may not have the label yet: create it first (idempotent). Adding is tried even
102
+ // when that fails, since the label may already be there.
103
+ if (d.ready) {
104
+ try {
105
+ host.ensureLabel(READY_LABEL, { color: READY_LABEL_COLOR, description: READY_LABEL_DESCRIPTION });
106
+ } catch (e) {
107
+ warn('create', e);
108
+ }
109
+ }
110
+ try {
111
+ host.setLabel(pr.number, READY_LABEL, d.ready);
112
+ io.stdout(d.ready ? `ready-label: added label ${READY_LABEL} to #${pr.number}\n` : `ready-label: removed label ${READY_LABEL} from #${pr.number}\n`);
113
+ } catch (e) {
114
+ warn(action, e);
115
+ }
116
+ return 0;
117
+ }
118
+
119
+ export const readyLabelCommand = { run, usage: USAGE };
@@ -0,0 +1,64 @@
1
+ // `slicekit review <sub>` (REQ-2.8): the deterministic steps of `/slicekit review`. `review start <PR>`
2
+ // prints the slice, the review worktree, the round and the verdict path; later `build` calls in that
3
+ // worktree pass `--worktree <path>`, since two worktrees then hold the slice.
4
+ import path from 'node:path';
5
+ import { parseArgs } from 'node:util';
6
+ import { ConfigError, loadConfig } from '../config.js';
7
+ import { GitError } from '../git.js';
8
+ import { getHost } from '../adapters/githost/index.js';
9
+ import { GitHostError } from '../adapters/githost/errors.js';
10
+ import { LockError, TransitionError } from '../state/index.js';
11
+ import { startReview } from '../build/review.js';
12
+ import { BuildError } from '../build/worktree.js';
13
+
14
+ const SUBCOMMANDS = ['start'];
15
+ const USAGE = 'review start <PR>';
16
+
17
+ function usage(io) {
18
+ io.stderr(`usage: slicekit ${USAGE}\n`);
19
+ return 2;
20
+ }
21
+
22
+ function start(root, io, positionals) {
23
+ if (positionals.length !== 1 || !/^[1-9]\d*$/.test(positionals[0])) return usage(io);
24
+ const cfg = loadConfig(root);
25
+ const host = getHost(cfg, io.env ?? process.env, root);
26
+ const r = startReview(root, cfg, host, Number(positionals[0]));
27
+ const where = path.resolve(r.worktree).split(path.sep).join('/');
28
+ io.stdout(`slice ${r.id}\nworktree ${where}\nround ${r.round}\nverdict ${r.verdictPath}\n`);
29
+ return 0;
30
+ }
31
+
32
+ const HANDLERS = { start };
33
+
34
+ async function run(args, io) {
35
+ let positionals;
36
+ try {
37
+ ({ positionals } = parseArgs({ args, options: {}, allowPositionals: true }));
38
+ } catch (e) {
39
+ io.stderr(`slicekit: ${e.message}\n`);
40
+ return usage(io);
41
+ }
42
+ const [sub, ...rest] = positionals;
43
+ const handler = Object.hasOwn(HANDLERS, sub ?? '') ? HANDLERS[sub] : undefined;
44
+ if (!handler) return usage(io);
45
+ try {
46
+ return handler(io.cwd, io, rest);
47
+ } catch (e) {
48
+ if (e instanceof BuildError) {
49
+ io.stderr(`slicekit: ${e.message}\n`);
50
+ return e.code;
51
+ }
52
+ if (e instanceof ConfigError) {
53
+ io.stderr(`slicekit: ${e.message}\n`);
54
+ return 2;
55
+ }
56
+ if (e instanceof GitError || e instanceof GitHostError || e instanceof TransitionError || e instanceof LockError) {
57
+ io.stderr(`slicekit: ${e.message}\n`);
58
+ return 1;
59
+ }
60
+ throw e;
61
+ }
62
+ }
63
+
64
+ export const reviewCommand = { run, usage: USAGE, subcommands: SUBCOMMANDS };
@@ -0,0 +1,31 @@
1
+ // `slicekit spec verify` (REQ-2.3): exit 0 with the REQ, contract and marker counts, then each
2
+ // clarification marker; exit 1 with one `<file>:<line> <message>` per finding.
3
+ import { ConfigError, loadConfig } from '../config.js';
4
+ import { verifySpec } from '../spec-verify.js';
5
+
6
+ const USAGE = 'spec verify';
7
+
8
+ async function run(args, io) {
9
+ if (args.length !== 1 || args[0] !== 'verify') {
10
+ io.stderr(`usage: slicekit ${USAGE}\n`);
11
+ return 2;
12
+ }
13
+ let cfg;
14
+ try {
15
+ cfg = loadConfig(io.cwd);
16
+ } catch (e) {
17
+ if (!(e instanceof ConfigError)) throw e;
18
+ io.stderr(`slicekit: ${e.message}\n`);
19
+ return 2;
20
+ }
21
+ const { findings, counts, markers } = verifySpec(io.cwd, cfg);
22
+ if (findings.length > 0) {
23
+ io.stdout(findings.map((f) => `${f.file}:${f.line} ${f.message}\n`).join(''));
24
+ return 1;
25
+ }
26
+ const head = `spec verified: ${counts.reqs} REQ, ${counts.contracts} contracts, ${counts.markers} clarification markers\n`;
27
+ io.stdout(head + markers.map((m) => `${m.file}:${m.line} ${m.text}\n`).join(''));
28
+ return 0;
29
+ }
30
+
31
+ export const specCommand = { run, usage: USAGE, subcommands: ['verify'] };
@@ -0,0 +1,37 @@
1
+ // `slicekit stage require <stage>` (REQ-1.7): a command's gate. Exit 0 with `ready: <stage>` when
2
+ // the stage and every one before it are complete; exit 1 with the first blocker and the command
3
+ // that clears it. Pending rows that do not block follow, one per line.
4
+ import { ConfigError, loadConfig } from '../config.js';
5
+ import { STAGES, StageError, programStage } from '../stage.js';
6
+
7
+ const USAGE = 'stage require <stage>';
8
+
9
+ async function run(args, io) {
10
+ const [sub, stage, ...rest] = args;
11
+ if (sub !== 'require' || stage === undefined || rest.length > 0) {
12
+ io.stderr(`usage: slicekit ${USAGE}\n`);
13
+ return 2;
14
+ }
15
+ if (!STAGES.includes(stage)) {
16
+ io.stderr(`slicekit: unknown stage '${stage}'; one of ${STAGES.join(', ')}\n`);
17
+ return 2;
18
+ }
19
+ let cfg;
20
+ try {
21
+ cfg = loadConfig(io.cwd);
22
+ } catch (e) {
23
+ if (!(e instanceof ConfigError)) throw e;
24
+ io.stderr(`slicekit: ${e.message}\n`);
25
+ return 2;
26
+ }
27
+ const now = programStage(io.cwd, cfg, { upTo: stage });
28
+ const pending = (now.pending ?? []).map((line) => `${line}\n`).join('');
29
+ if (!now.complete) {
30
+ io.stderr(`${new StageError(now.stage, now.blockers[0]).message}\n${pending}`);
31
+ return 1;
32
+ }
33
+ io.stdout(`ready: ${stage}\n${pending}`);
34
+ return 0;
35
+ }
36
+
37
+ export const stageCommand = { run, usage: USAGE, subcommands: ['require'] };