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.
Files changed (170) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +252 -3
  3. package/bin/slicekit.js +4 -0
  4. package/package.json +21 -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 +281 -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 +79 -0
  154. package/templates/githooks/pre-merge-commit +79 -0
  155. package/templates/githooks/prepare-commit-msg +110 -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,239 @@
1
+ // `slicekit build <sub>` (REQ-2.6): the deterministic steps of `/slicekit build`. Each subcommand
2
+ // lands with its task; S-15 and S-16 add theirs.
3
+ import path from 'node:path';
4
+ import { parseArgs } from 'node:util';
5
+ import { ConfigError, loadConfig } from '../config.js';
6
+ import { GitError } from '../git.js';
7
+ import { getHost } from '../adapters/githost/index.js';
8
+ import { GitHostError } from '../adapters/githost/errors.js';
9
+ import { getTracker, TrackerError } from '../adapters/tracker/index.js';
10
+ import { PrError } from '../pr.js';
11
+ import { PrBodyError } from '../prbody.js';
12
+ import { SliceFileError } from '../slicefile.js';
13
+ import { LockError, TransitionError } from '../state/index.js';
14
+ import { runnable, waiting } from '../build/order.js';
15
+ import { amend, commitWork } from '../build/commit.js';
16
+ import { declineFinding } from '../build/decline.js';
17
+ import { finish } from '../build/finish.js';
18
+ import { startParallel } from '../build/parallel.js';
19
+ import { openSlicePr } from '../build/pr.js';
20
+ import { red } from '../build/red.js';
21
+ import { syncWorktree } from '../build/sync.js';
22
+ import { LOOP_ROUNDS, latestVerdictPath, recordVerdict } from '../build/verdict.js';
23
+ import { BuildError, startSlice } from '../build/worktree.js';
24
+ import { readSlice } from '../state/index.js';
25
+ import { INDEPENDENT_MISSING } from '../state/transitions.js';
26
+
27
+ const SUBCOMMANDS = ['next', 'start', 'red', 'commit', 'amend', 'pr', 'verdict', 'decline', 'finish', 'sync', 'parallel'];
28
+ const USAGE =
29
+ 'build next | build start <id> [--descr <d>] | build parallel P<n> | build red <id> --tests <a,b> [--worktree <p>] | build commit <id> --message <m> [--require-green] [--worktree <p>] | build amend <id> --test <path> --reason <r> [--worktree <p>] | build pr <id> [--notes <file>] [--worktree <p>] | build verdict <id> <file> [--independent] [--worktree <p>] | build decline <id> <H-n> [--worktree <p>] | build finish <id> [--wait <s>, default 90] [--worktree <p>] | build sync <id> [--worktree <p>]';
30
+
31
+ const OPTIONS = {
32
+ descr: { type: 'string' },
33
+ tests: { type: 'string' },
34
+ worktree: { type: 'string' },
35
+ message: { type: 'string' },
36
+ 'require-green': { type: 'boolean' },
37
+ test: { type: 'string' },
38
+ reason: { type: 'string' },
39
+ notes: { type: 'string' },
40
+ wait: { type: 'string' },
41
+ independent: { type: 'boolean' },
42
+ };
43
+
44
+ function next(root, io, positionals) {
45
+ if (positionals.length > 0) return usage(io);
46
+ const cfg = loadConfig(root);
47
+ const ready = runnable(root, cfg);
48
+ if (ready.length > 0) {
49
+ io.stdout(`${ready[0].id}\n`);
50
+ return 0;
51
+ }
52
+ io.stdout(['no slice is ready to build', ...waiting(root, cfg)].map((l) => `${l}\n`).join(''));
53
+ return 1;
54
+ }
55
+
56
+ function start(root, io, positionals, values) {
57
+ if (positionals.length !== 1) return usage(io);
58
+ const cfg = loadConfig(root);
59
+ const { worktree, branch, resumed, state } = startSlice(root, cfg, positionals[0], {
60
+ descr: values.descr,
61
+ onWarning: (msg) => io.stderr(`${msg}\n`),
62
+ });
63
+ // Forward slashes on every platform, as the brief's Where section prints it: a backslash path
64
+ // pasted unquoted into a POSIX shell loses its separators.
65
+ const where = path.resolve(worktree).split(path.sep).join('/');
66
+ if (!resumed) {
67
+ io.stdout(`${where}\n${branch}\n`);
68
+ return 0;
69
+ }
70
+ // At in-review the latest round is recorded and its next step not yet acted on: `build verdict`
71
+ // with that round's file re-prints the step and records nothing again. After an independent round
72
+ // that file is recorded only from the review worktree: the next review is a new session's.
73
+ const current = state === 'in-review' ? readSlice(worktree, cfg, positionals[0]) : null;
74
+ const last = current?.verdicts?.at(-1);
75
+ let line = '';
76
+ if (last?.independent) line = `next review: /slicekit review ${current.pr?.number ?? '<PR>'} in a new session\n`;
77
+ else if (last) line = `verdict ${latestVerdictPath(root, current.verdicts)}\n`;
78
+ io.stdout(`resumed ${where} at ${state}\n${line}`);
79
+ return 0;
80
+ }
81
+
82
+ // Prints `<id> <worktree> <branch>` for each slice started, and a refusal on stderr for each that
83
+ // was not, `held in <where>` for one another clone holds. Each start is claimed on its own, so the ones that started stay started when another
84
+ // fails: the exit is 0 only when every runnable slice of the step started, else 1, with the started
85
+ // lines still printed so the caller knows which worktrees exist.
86
+ function parallel(root, io, positionals) {
87
+ if (positionals.length !== 1 || !/^P\d+$/.test(positionals[0])) return usage(io);
88
+ const cfg = loadConfig(root);
89
+ const outcomes = startParallel(root, cfg, positionals[0], { onWarning: (msg) => io.stderr(`${msg}\n`) });
90
+ for (const o of outcomes) {
91
+ if (o.error === undefined) io.stdout(`${o.id} ${path.resolve(o.worktree).split(path.sep).join('/')} ${o.branch}\n`);
92
+ else io.stderr(`slicekit: ${o.id} not started: ${o.error}\n`);
93
+ }
94
+ return outcomes.every((o) => o.error === undefined || o.held) ? 0 : 1;
95
+ }
96
+
97
+ function redStep(root, io, positionals, values) {
98
+ if (positionals.length !== 1 || values.tests === undefined) return usage(io);
99
+ const cfg = loadConfig(root);
100
+ const { commit } = red(root, cfg, positionals[0], { tests: values.tests, worktree: values.worktree });
101
+ io.stdout(`${commit}\ntests-red → building\n`);
102
+ return 0;
103
+ }
104
+
105
+ function commitStep(root, io, positionals, values) {
106
+ if (positionals.length !== 1 || values.message === undefined) return usage(io);
107
+ const cfg = loadConfig(root);
108
+ const { sha, exit } = commitWork(root, cfg, positionals[0], {
109
+ message: values.message,
110
+ requireGreen: Boolean(values['require-green']),
111
+ worktree: values.worktree,
112
+ });
113
+ io.stdout(sha === null ? 'nothing to commit\n' : `${sha.slice(0, 7)} exit ${exit}\n`);
114
+ return 0;
115
+ }
116
+
117
+ function amendStep(root, io, positionals, values) {
118
+ if (positionals.length !== 1 || values.test === undefined || values.reason === undefined) return usage(io);
119
+ const cfg = loadConfig(root);
120
+ const { test } = amend(root, cfg, positionals[0], { test: values.test, reason: values.reason, worktree: values.worktree });
121
+ io.stdout(`unfroze ${test}\n`);
122
+ return 0;
123
+ }
124
+
125
+ function prStep(root, io, positionals, values) {
126
+ if (positionals.length !== 1) return usage(io);
127
+ const cfg = loadConfig(root);
128
+ const host = getHost(cfg, io.env ?? process.env, root);
129
+ const { number, url, verdictPath } = openSlicePr(root, cfg, host, positionals[0], { notes: values.notes, worktree: values.worktree });
130
+ // Last, the path the next reviewer writes; none once the loop's rounds are used.
131
+ io.stdout(`#${number} ${url}\n${verdictPath ? `verdict ${verdictPath}\n` : ''}`);
132
+ return 0;
133
+ }
134
+
135
+ // Prints the next step, the round's finding IDs and the in-loop rounds used; a stopped loop (D-18)
136
+ // also says so and exits 1. `--independent` records an independent review's verdict (D-39).
137
+ function verdictStep(root, io, positionals, values) {
138
+ if (positionals.length !== 2) return usage(io);
139
+ const cfg = loadConfig(root);
140
+ const [id, file] = positionals;
141
+ const r = recordVerdict(root, cfg, getHost(cfg, io.env ?? process.env, root), id, file, {
142
+ worktree: values.worktree,
143
+ independent: Boolean(values.independent),
144
+ });
145
+ const list = (ids) => (ids.length > 0 ? ids.join(', ') : 'none');
146
+ const out = [`next: ${r.next}`, `blockers: ${list(r.blockers)}`, `should-haves: ${list(r.shouldHaves)}`, `rounds: ${r.roundsUsed} of ${LOOP_ROUNDS}`];
147
+ if (r.next === 'ask' && r.roundsUsed >= LOOP_ROUNDS) out.push('fix: not available after round 2; decline or leave for the independent review');
148
+ if (r.verdictPath) out.push(`verdict ${r.verdictPath}`);
149
+ io.stdout(out.map((l) => `${l}\n`).join(''));
150
+ if (r.next === 'stuck') {
151
+ io.stderr(`slicekit: stopped: ${r.blockers.join(', ')} survive round ${r.round}; the pull request stays draft\n`);
152
+ return 1;
153
+ }
154
+ return 0;
155
+ }
156
+
157
+ // Prints the issue's URL, and `(already filed)` (`, closed` for a closed one) when the finding's issue
158
+ // was there before.
159
+ function declineStep(root, io, positionals, values) {
160
+ if (positionals.length !== 2) return usage(io);
161
+ const cfg = loadConfig(root);
162
+ const env = io.env ?? process.env;
163
+ const [id, finding] = positionals;
164
+ const { url, created, state } = declineFinding(root, cfg, getHost(cfg, env, root), getTracker(cfg, env, root), id, finding, { worktree: values.worktree });
165
+ const filed = state === 'closed' ? 'already filed, closed' : 'already filed';
166
+ io.stdout(created ? `${url}\n` : `${url} (${filed})\n`);
167
+ return 0;
168
+ }
169
+
170
+ // Prints `ci-green` and the CI run's URL once CI succeeds on the pull request's live head, then
171
+ // `ready`, or that the slice is waiting for an independent review.
172
+ function finishStep(root, io, positionals, values) {
173
+ if (positionals.length !== 1) return usage(io);
174
+ let wait;
175
+ if (values.wait !== undefined) {
176
+ if (!/^\d+$/.test(values.wait)) return usage(io);
177
+ wait = Number(values.wait);
178
+ }
179
+ const cfg = loadConfig(root);
180
+ const { ci, ready, reason, number } = finish(root, cfg, getHost(cfg, io.env ?? process.env, root), positionals[0], { worktree: values.worktree, ...(wait === undefined ? {} : { wait }) });
181
+ io.stdout(`ci-green\n${ci.url ?? `passed on ${ci.sha.slice(0, 7)}`}\n`);
182
+ if (ready) io.stdout('ready\n');
183
+ else if (reason === INDEPENDENT_MISSING) io.stdout(`ready: waiting for an independent review; run /slicekit review ${number} in a new session\n`);
184
+ else io.stdout(`ready: not yet: ${reason}\n`);
185
+ return 0;
186
+ }
187
+
188
+ // Prints the worktree's head once it is fast-forwarded to its branch on origin.
189
+ function syncStep(root, io, positionals, values) {
190
+ if (positionals.length !== 1) return usage(io);
191
+ const cfg = loadConfig(root);
192
+ const { head } = syncWorktree(root, cfg, positionals[0], { worktree: values.worktree });
193
+ io.stdout(`${head}\n`);
194
+ return 0;
195
+ }
196
+
197
+ const HANDLERS = { next, start, parallel, red: redStep, commit: commitStep, amend: amendStep, pr: prStep, verdict: verdictStep, decline: declineStep, finish: finishStep, sync: syncStep };
198
+
199
+ function usage(io) {
200
+ io.stderr(`usage: slicekit ${USAGE}\n`);
201
+ return 2;
202
+ }
203
+
204
+ async function run(args, io) {
205
+ let values;
206
+ let positionals;
207
+ try {
208
+ ({ values, positionals } = parseArgs({ args, options: OPTIONS, allowPositionals: true }));
209
+ } catch (e) {
210
+ io.stderr(`slicekit: ${e.message}\n`);
211
+ return usage(io);
212
+ }
213
+ const [sub, ...rest] = positionals;
214
+ const handler = Object.hasOwn(HANDLERS, sub ?? '') ? HANDLERS[sub] : undefined;
215
+ if (!handler) return usage(io);
216
+ try {
217
+ return handler(io.cwd, io, rest, values);
218
+ } catch (e) {
219
+ if (e instanceof BuildError || e instanceof PrError) {
220
+ io.stderr(`slicekit: ${e.message}\n`);
221
+ return e.code;
222
+ }
223
+ if (e instanceof ConfigError) {
224
+ io.stderr(`slicekit: ${e.message}\n`);
225
+ return 2;
226
+ }
227
+ if (e instanceof GitError || e instanceof GitHostError || e instanceof TrackerError || e instanceof PrBodyError || e instanceof SliceFileError || e instanceof TransitionError || e instanceof LockError) {
228
+ io.stderr(`slicekit: ${e.message}\n`);
229
+ return 1;
230
+ }
231
+ if (/^unknown slice /.test(e.message)) {
232
+ io.stderr(`slicekit: ${e.message}\n`);
233
+ return 2;
234
+ }
235
+ throw e;
236
+ }
237
+ }
238
+
239
+ export const buildCommand = { run, usage: USAGE, subcommands: SUBCOMMANDS };
@@ -0,0 +1,40 @@
1
+ import { parseArgs } from 'node:util';
2
+ import { ConfigError } from '../config.js';
3
+ import { check } from '../check/index.js';
4
+
5
+ const USAGE = 'check [--ci] [--json]';
6
+
7
+ const OPTIONS = {
8
+ ci: { type: 'boolean' },
9
+ json: { type: 'boolean' },
10
+ };
11
+
12
+ // The kit's one gate: the Stop hook and CI both run it. Exit 0 clean, 1 on a failing finding,
13
+ // 2 when it cannot run (bad arguments or config). Under --json stdout carries one JSON document.
14
+ async function run(args, io) {
15
+ let values;
16
+ try {
17
+ ({ values } = parseArgs({ args, options: OPTIONS, allowPositionals: false }));
18
+ } catch (e) {
19
+ io.stderr(`slicekit: ${e.message}\nusage: slicekit ${USAGE}\n`);
20
+ return 2;
21
+ }
22
+ let result;
23
+ try {
24
+ result = check(io.cwd, { ci: Boolean(values.ci), env: io.env ?? process.env, ...(io.rules ? { rules: io.rules } : {}) });
25
+ } catch (e) {
26
+ if (!(e instanceof ConfigError)) throw e;
27
+ if (values.json) io.stdout(`${JSON.stringify({ error: e.message }, null, 2)}\n`);
28
+ io.stderr(`slicekit: ${e.message}\n`);
29
+ return 2;
30
+ }
31
+ if (values.json) {
32
+ const { findings, counts, exitCode, rules, over, blocking } = result;
33
+ io.stdout(`${JSON.stringify({ findings, counts, over, blocking, exitCode, rules }, null, 2)}\n`);
34
+ } else {
35
+ io.stdout(`${result.lines.join('\n')}\n`);
36
+ }
37
+ return result.exitCode;
38
+ }
39
+
40
+ export const checkCommand = { run, usage: USAGE };
@@ -0,0 +1,59 @@
1
+ import { parseArgs } from 'node:util';
2
+ import { ConfigError, loadConfig } from '../config.js';
3
+ import { DecisionError, addDecision, settleDecision } from '../decisions.js';
4
+
5
+ const USAGE =
6
+ 'decide add (--type <T> --question <q> --answer <a> | --open [--type <T>] --question <q> --why <w> --recommendation <r>) [--who <w>] [--replaces D-<n>] [--interface <name> [--source <file>:<line>]]\n' +
7
+ ' slicekit decide settle D-<n> --answer <a> [--type <T>] [--who <w>]\n' +
8
+ ' (a value that starts with "-" goes as --answer=<text>, and so for every option)';
9
+
10
+ const ADD = ['type', 'question', 'answer', 'why', 'recommendation', 'who', 'replaces', 'interface', 'source', 'open'];
11
+ const SETTLE = ['answer', 'who', 'type'];
12
+ const OPTIONS = { ...Object.fromEntries(ADD.map((k) => [k, { type: 'string' }])), open: { type: 'boolean' } };
13
+
14
+ const usage = (io, why) => {
15
+ io.stderr(`${why ? `slicekit: ${why}\n` : ''}usage: slicekit ${USAGE}\n`);
16
+ return 2;
17
+ };
18
+
19
+ // Writes a decision row (REQ-1.4) and prints its ID. Exit 1 on a file problem or a settled row,
20
+ // 2 on bad usage or input.
21
+ async function run(args, io) {
22
+ let values;
23
+ let positionals;
24
+ try {
25
+ ({ values, positionals } = parseArgs({ args, options: OPTIONS, allowPositionals: true }));
26
+ } catch (e) {
27
+ return usage(io, e.message);
28
+ }
29
+ const [sub, ...rest] = positionals;
30
+ const allowed = { add: ADD, settle: SETTLE }[sub];
31
+ if (!allowed) return usage(io);
32
+ const extra = Object.keys(values).filter((k) => !allowed.includes(k));
33
+ if (extra.length > 0) return usage(io, `${sub} does not take --${extra[0]}`);
34
+ if (sub === 'add' && (rest.length > 0 || (values.type === undefined && !values.open) || values.question === undefined)) return usage(io);
35
+ if (sub === 'add' && values.source !== undefined && values.interface === undefined) return usage(io, '--source needs --interface');
36
+ if (sub === 'settle' && (rest.length !== 1 || values.answer === undefined)) return usage(io);
37
+ try {
38
+ const cfg = loadConfig(io.cwd);
39
+ const { interface: name, source, ...fields } = values;
40
+ const { id } =
41
+ sub === 'add'
42
+ ? addDecision(io.cwd, cfg, { ...fields, ...(name !== undefined ? { iface: { name, source } } : {}) })
43
+ : settleDecision(io.cwd, cfg, rest[0], values);
44
+ io.stdout(`${id}\n`);
45
+ return 0;
46
+ } catch (e) {
47
+ if (e instanceof DecisionError) {
48
+ io.stderr(`slicekit: ${e.message}\n`);
49
+ return e.code;
50
+ }
51
+ if (e instanceof ConfigError) {
52
+ io.stderr(`slicekit: ${e.message}\n`);
53
+ return 2;
54
+ }
55
+ throw e;
56
+ }
57
+ }
58
+
59
+ export const decideCommand = { run, usage: USAGE, subcommands: ['add', 'settle'] };
@@ -0,0 +1,73 @@
1
+ import { parseArgs } from 'node:util';
2
+ import { ConfigError, loadConfig } from '../config.js';
3
+ import { GapError, addGap, closeGap, scanGaps, verifyGaps } from '../gaps.js';
4
+
5
+ const USAGE =
6
+ 'gaps scan\n' +
7
+ ' slicekit gaps verify\n' +
8
+ ' slicekit gaps add --class <c> --where <file>:<line> --what <text>\n' +
9
+ ' slicekit gaps close G-<n> (--decision D-<n> | --req <REQ>)\n' +
10
+ ' (a value that starts with "-" goes as --what=<text>, and so for every option)';
11
+
12
+ const ALLOWED = { scan: [], verify: [], add: ['class', 'where', 'what'], close: ['decision', 'req'] };
13
+ const OPTIONS = Object.fromEntries(Object.values(ALLOWED).flat().map((k) => [k, { type: 'string' }]));
14
+
15
+ const usage = (io, why) => {
16
+ io.stderr(`${why ? `slicekit: ${why}\n` : ''}usage: slicekit ${USAGE}\n`);
17
+ return 2;
18
+ };
19
+
20
+ // Scans for gaps, verifies the gaps file, or writes or closes a gap row (REQ-2.4). Exit 1 on a
21
+ // file problem, a closed row or a verify finding, 2 on bad usage, bad input or an unknown ID.
22
+ async function run(args, io) {
23
+ let values;
24
+ let positionals;
25
+ try {
26
+ ({ values, positionals } = parseArgs({ args, options: OPTIONS, allowPositionals: true }));
27
+ } catch (e) {
28
+ return usage(io, e.message);
29
+ }
30
+ const [sub, ...rest] = positionals;
31
+ const allowed = ALLOWED[sub];
32
+ if (!allowed) return usage(io);
33
+ const extra = Object.keys(values).filter((k) => !allowed.includes(k));
34
+ if (extra.length > 0) return usage(io, `${sub} does not take --${extra[0]}`);
35
+ if ((sub === 'scan' || sub === 'verify') && rest.length > 0) return usage(io);
36
+ if (sub === 'add' && (rest.length > 0 || allowed.some((k) => values[k] === undefined))) return usage(io);
37
+ if (sub === 'close' && (rest.length !== 1 || (values.decision === undefined) === (values.req === undefined))) {
38
+ return usage(io, rest.length === 1 ? 'close takes exactly one of --decision and --req' : undefined);
39
+ }
40
+ try {
41
+ const cfg = loadConfig(io.cwd);
42
+ if (sub === 'scan') {
43
+ const added = scanGaps(io.cwd, cfg).filter((g) => g.added);
44
+ io.stdout(added.length > 0 ? added.map((g) => `${g.id} ${g.cls} ${g.what}\n`).join('') : 'no new gaps\n');
45
+ } else if (sub === 'verify') {
46
+ const found = verifyGaps(io.cwd, cfg);
47
+ if (found.length > 0) {
48
+ io.stdout(found.map((f) => `${f.where} ${f.message}\n`).join(''));
49
+ return 1;
50
+ }
51
+ io.stdout('ready: gap-scan\n');
52
+ } else if (sub === 'add') {
53
+ const { id } = addGap(io.cwd, cfg, { cls: values.class, where: values.where, what: values.what });
54
+ io.stdout(`${id}\n`);
55
+ } else {
56
+ const { id, closedBy } = closeGap(io.cwd, cfg, rest[0], values);
57
+ io.stdout(`${id} closed by ${closedBy}\n`);
58
+ }
59
+ return 0;
60
+ } catch (e) {
61
+ if (e instanceof GapError) {
62
+ io.stderr(`slicekit: ${e.message}\n`);
63
+ return e.code;
64
+ }
65
+ if (e instanceof ConfigError) {
66
+ io.stderr(`slicekit: ${e.message}\n`);
67
+ return 2;
68
+ }
69
+ throw e;
70
+ }
71
+ }
72
+
73
+ export const gapsCommand = { run, usage: USAGE, subcommands: ['scan', 'verify', 'add', 'close'] };
@@ -0,0 +1,28 @@
1
+ import { failureOutput, runHook } from '../hooks/io.js';
2
+
3
+ // `slicekit hook <name>`: read all of stdin, print the hook's JSON decision, exit with its code.
4
+ // Any throw here answers like a failed hook: the refusal for a fail-closed name, {} for a fail-open one.
5
+ export const hookCommand = {
6
+ usage: 'hook <name> run a hook handler (called by the agent tool)',
7
+ async run(args, io) {
8
+ const [name] = args;
9
+ if (!name) {
10
+ await io.stderr('usage: slicekit hook <name>\n');
11
+ return 2;
12
+ }
13
+ let r;
14
+ try {
15
+ const stdin = await io.stdin();
16
+ const load = io.loadHook ? { load: io.loadHook } : {};
17
+ const dispatch = io.runHook ?? runHook; // io.runHook: a seam for tests only
18
+ r = await dispatch(name, { stdin, env: io.env ?? process.env, ...load });
19
+ } catch (err) {
20
+ r = { stdout: failureOutput(name, err?.message ?? String(err)), code: 0, stderr: '' };
21
+ }
22
+ if (r.stderr) await io.stderr(r.stderr);
23
+ // Never print nothing for a known name: zero bytes at exit 0 reads as an allow.
24
+ const answer = r.stdout || (r.code === 2 ? '' : failureOutput(name, 'hook produced no answer'));
25
+ if (answer) await io.stdout(`${answer}\n`);
26
+ return r.code;
27
+ },
28
+ };
@@ -0,0 +1,83 @@
1
+ import fs from 'node:fs';
2
+ import { baselineCommand } from './baseline.js';
3
+ import { branchCommand } from './branch.js';
4
+ import { buildCommand } from './build.js';
5
+ import { checkCommand } from './check.js';
6
+ import { decideCommand } from './decide.js';
7
+ import { gapsCommand } from './gaps.js';
8
+ import { initCommand } from './init.js';
9
+ import { intakeCommand } from './intake.js';
10
+ import { prCommand } from './pr.js';
11
+ import { instructionsCommand } from './instructions.js';
12
+ import { journalCommand } from './journal.js';
13
+ import { planCommand } from './plan.js';
14
+ import { hookCommand } from './hook.js';
15
+ import { readyLabelCommand } from './ready-label.js';
16
+ import { reviewCommand } from './review.js';
17
+ import { specCommand } from './spec.js';
18
+ import { stageCommand } from './stage.js';
19
+ import { stateCommand } from './state.js';
20
+ import { statusCommand } from './status.js';
21
+ import { updateCommand } from './update.js';
22
+ import { workflowCommand } from './workflow.js';
23
+
24
+ // Later tasks add entries: { [name]: { run(args, io) -> Promise<number>, usage: string, subcommands?: string[] } }
25
+ export const COMMANDS = {
26
+ baseline: baselineCommand,
27
+ branch: branchCommand,
28
+ build: buildCommand,
29
+ check: checkCommand,
30
+ decide: decideCommand,
31
+ gaps: gapsCommand,
32
+ init: initCommand,
33
+ intake: intakeCommand,
34
+ instructions: instructionsCommand,
35
+ hook: hookCommand,
36
+ journal: journalCommand,
37
+ plan: planCommand,
38
+ pr: prCommand,
39
+ 'ready-label': readyLabelCommand,
40
+ review: reviewCommand,
41
+ spec: specCommand,
42
+ stage: stageCommand,
43
+ state: stateCommand,
44
+ status: statusCommand,
45
+ update: updateCommand,
46
+ workflow: workflowCommand,
47
+ };
48
+
49
+ function readVersion() {
50
+ const url = new URL('../../package.json', import.meta.url);
51
+ return JSON.parse(fs.readFileSync(url, 'utf8')).version;
52
+ }
53
+
54
+ function usageText() {
55
+ const lines = ['usage: slicekit <command> [options]', ''];
56
+ const names = Object.keys(COMMANDS).sort();
57
+ if (names.length > 0) {
58
+ lines.push('commands:');
59
+ for (const name of names) lines.push(` ${COMMANDS[name].usage}`);
60
+ lines.push('');
61
+ }
62
+ lines.push('options:', ' --help show this text', ' --version print the version', '');
63
+ return lines.join('\n');
64
+ }
65
+
66
+ export async function main(argv, io) {
67
+ const [first, ...rest] = argv;
68
+ if (first === '--version') {
69
+ io.stdout(`${readVersion()}\n`);
70
+ return 0;
71
+ }
72
+ if (first === '--help') {
73
+ io.stderr(usageText());
74
+ return 0;
75
+ }
76
+ const command = Object.hasOwn(COMMANDS, first ?? '') ? COMMANDS[first] : undefined;
77
+ if (!command) {
78
+ io.stderr(usageText());
79
+ if (first !== undefined) io.stderr(`slicekit: unknown command '${first}'\n`);
80
+ return 2;
81
+ }
82
+ return command.run(rest, io);
83
+ }
@@ -0,0 +1,96 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { parseArgs } from 'node:util';
4
+ import { CONFIG_PATH, ConfigError } from '../config.js';
5
+ import { exists } from '../fs.js';
6
+ import { git, GitError, isRepo } from '../git.js';
7
+ import { checkCli } from '../hook-cli.js';
8
+ import { init } from '../init.js';
9
+ import { kitVersion } from '../render/index.js';
10
+
11
+ const USAGE =
12
+ 'init --shape <backend|fullstack|data> [--name <n>] [--docs <dir>] [--cli <command>] [--test <cmd>] [--lint <cmd>] [--build <cmd>] [--e2e <cmd>]';
13
+
14
+ const OPTIONS = {
15
+ shape: { type: 'string' },
16
+ cli: { type: 'string' },
17
+ name: { type: 'string' },
18
+ docs: { type: 'string' },
19
+ test: { type: 'string' },
20
+ lint: { type: 'string' },
21
+ build: { type: 'string' },
22
+ e2e: { type: 'string' },
23
+ };
24
+
25
+ function samePath(a, b) {
26
+ const norm = (p) => {
27
+ const real = fs.realpathSync.native(path.resolve(p));
28
+ return process.platform === 'win32' ? real.toLowerCase() : real;
29
+ };
30
+ return norm(a) === norm(b);
31
+ }
32
+
33
+ // A typed `docs\` or `docs/` becomes `docs`; a hand-edited config gets no such help.
34
+ const normaliseDocs = (d) => (d === undefined ? undefined : d.replace(/\\/g, '/').replace(/\/+$/, ''));
35
+
36
+ async function run(args, io) {
37
+ let values;
38
+ try {
39
+ ({ values } = parseArgs({ args, options: OPTIONS, allowPositionals: false }));
40
+ } catch (e) {
41
+ io.stderr(`slicekit: ${e.message}\nusage: slicekit ${USAGE}\n`);
42
+ return 2;
43
+ }
44
+ const root = io.cwd;
45
+ if (isRepo(root)) {
46
+ let top;
47
+ try {
48
+ top = git(root, ['rev-parse', '--show-toplevel']);
49
+ } catch (e) {
50
+ if (!(e instanceof GitError)) throw e;
51
+ io.stderr(`slicekit: ${e.message}\n`);
52
+ return 2;
53
+ }
54
+ if (!samePath(top, root)) {
55
+ io.stderr(`slicekit: run slicekit init from the repository root: ${path.resolve(top)}\n`);
56
+ return 2;
57
+ }
58
+ }
59
+ if (!values.shape && !exists(path.join(root, CONFIG_PATH))) {
60
+ io.stderr(`usage: slicekit ${USAGE}\n`);
61
+ return 2;
62
+ }
63
+ const commands = {};
64
+ for (const key of ['test', 'lint', 'build', 'e2e']) {
65
+ if (values[key] !== undefined) commands[key] = values[key];
66
+ }
67
+ let result;
68
+ try {
69
+ result = init(root, {
70
+ name: values.name ?? path.basename(root),
71
+ shape: values.shape,
72
+ docsRoot: normaliseDocs(values.docs),
73
+ commands,
74
+ cli: values.cli,
75
+ env: io.env ?? process.env,
76
+ });
77
+ } catch (e) {
78
+ if (!(e instanceof ConfigError)) throw e;
79
+ io.stderr(`slicekit: ${e.message}\n`);
80
+ return 2;
81
+ }
82
+ for (const rel of result.created) io.stdout(`created ${rel}\n`);
83
+ for (const rel of result.updated) io.stdout(`updated ${rel}\n`);
84
+ for (const rel of result.skipped) io.stdout(`skipped ${rel}\n`);
85
+ io.stdout(`hooks run: ${result.cli}\n`);
86
+ if (result.next) io.stdout(`${result.next}\n`);
87
+ for (const warning of result.warnings) io.stderr(`slicekit: ${warning}\n`);
88
+ // The not-installed warning already says the hooks will fail; the --version check would repeat it.
89
+ if (!result.cliWarned) {
90
+ const unanswered = checkCli(result.cli, { version: kitVersion(), cwd: root });
91
+ if (unanswered) io.stderr(`slicekit: ${unanswered}\n`);
92
+ }
93
+ return 0;
94
+ }
95
+
96
+ export const initCommand = { run, usage: USAGE };