slicekit 0.3.3 → 0.5.0
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/README.md +20 -9
- package/package.json +1 -1
- package/src/audit.js +234 -0
- package/src/check/rules/sync-release.js +17 -5
- package/src/cli/audit.js +91 -0
- package/src/cli/branch.js +189 -28
- package/src/cli/index.js +76 -0
- package/src/cli/ops.js +78 -0
- package/src/cli/update.js +4 -0
- package/src/config.js +80 -5
- package/src/hooks/subagent-start.js +7 -3
- package/src/init.js +19 -0
- package/src/ops.js +223 -0
- package/src/paths.js +1 -0
- package/src/release-version.js +90 -10
- package/src/reports.js +131 -0
- package/src/state/sync.js +4 -3
- package/templates/claude-code/agents/ops.md +3 -3
- package/templates/instructions/agents/auditor.md +13 -2
- package/templates/instructions/agents/ops.md +30 -9
- package/templates/instructions/commands/audit.md +40 -0
- package/templates/instructions/commands/ops.md +38 -0
- package/templates/instructions/commands/release.md +9 -4
package/src/cli/branch.js
CHANGED
|
@@ -1,13 +1,25 @@
|
|
|
1
1
|
// `slicekit branch <spec|gaps|plan|release> <slug>` (REQ-2.3..REQ-2.5, REQ-2.9, D-54): puts the session on the
|
|
2
2
|
// work branch of a document pull request, cut from the default branch as origin has it. The command owns git
|
|
3
3
|
// (D-19). A change to a tracked file (staged or not) is refused, so no edit is carried across;
|
|
4
|
-
// untracked files go along as git carries them, so a source dropped in for intake survives.
|
|
4
|
+
// untracked files go along as git carries them, so a source dropped in for intake survives. The one
|
|
5
|
+
// change carried is the config as the migration writes it from the committed one (D-94): an earlier
|
|
6
|
+
// command on this branch, such as `release --dry-run`, wrote it, and `pr open` commits it. Where the
|
|
7
|
+
// commit the session is put on holds another config than the one here (an earlier `pr open`
|
|
8
|
+
// committed the migration there), the change is dropped when that config equals it, and refused
|
|
9
|
+
// with one line naming the difference when it does not (issue #162).
|
|
5
10
|
// A `release/<slug>` branch that exists away from the default branch's tip is moved there or refused;
|
|
6
11
|
// one that holds its own commits is refused with its open pull request, read through the git host
|
|
7
|
-
// (C-1 pullRequestForBranch), or with the next free slug when the host says none is open.
|
|
12
|
+
// (C-1 pullRequestForBranch), or with the next free slug when the host says none is open. The host
|
|
13
|
+
// is asked about each `release/<slug>-<n>` branch as well: the phase's open release pull request
|
|
14
|
+
// may be on one of them, taken after `release/<slug>` was closed unmerged (issue #152). A slug
|
|
15
|
+
// given numbered, `<base>-<n>` with <n> 2 or more (the numbers the command itself names), belongs
|
|
16
|
+
// to the family of `release/<base>`: the host is asked about that branch and each
|
|
17
|
+
// `release/<base>-<n>` too, so an open release pull request on any of them refuses.
|
|
8
18
|
import { getHost } from '../adapters/githost/index.js';
|
|
9
19
|
import { GitHostError } from '../adapters/githost/errors.js';
|
|
10
|
-
import
|
|
20
|
+
import fs from 'node:fs';
|
|
21
|
+
import path from 'node:path';
|
|
22
|
+
import { CONFIG_PATH, ConfigError, loadConfig, migrateConfig } from '../config.js';
|
|
11
23
|
import { GitError, assertRef, currentBranch, git, hasRemote, switchBranch } from '../git.js';
|
|
12
24
|
|
|
13
25
|
const KINDS = ['spec', 'gaps', 'plan', 'release'];
|
|
@@ -24,6 +36,90 @@ const resolves = (root, ref) => {
|
|
|
24
36
|
}
|
|
25
37
|
};
|
|
26
38
|
|
|
39
|
+
const CONFIG_FILE = CONFIG_PATH.split(path.sep).join('/');
|
|
40
|
+
|
|
41
|
+
// D-94: whether the only uncommitted change to a tracked file is the config, and the file holds
|
|
42
|
+
// exactly what the migration writes from the config the last commit holds. A config staged with
|
|
43
|
+
// one content and holding another in the working directory is not that change.
|
|
44
|
+
function onlyMigratedConfig(root) {
|
|
45
|
+
const names = (args) => git(root, ['diff', '--name-only', '--no-renames', '-z', ...args]).split('\0').filter((f) => f !== '');
|
|
46
|
+
const staged = names(['--cached', 'HEAD']);
|
|
47
|
+
const unstaged = names([]);
|
|
48
|
+
const changed = new Set([...staged, ...unstaged]);
|
|
49
|
+
if (changed.size !== 1 || !changed.has(CONFIG_FILE)) return false;
|
|
50
|
+
if (staged.length > 0 && unstaged.length > 0) return false;
|
|
51
|
+
try {
|
|
52
|
+
const committed = JSON.parse(git(root, ['show', `HEAD:${CONFIG_FILE}`]));
|
|
53
|
+
const { cfg, from } = migrateConfig(committed);
|
|
54
|
+
if (cfg === committed || from === cfg.configVersion) return false;
|
|
55
|
+
return fs.readFileSync(path.join(root, CONFIG_PATH), 'utf8') === `${JSON.stringify(cfg, null, 2)}\n`;
|
|
56
|
+
} catch (e) {
|
|
57
|
+
if (e instanceof GitError || e instanceof ConfigError || e instanceof SyntaxError || e.code !== undefined) return false;
|
|
58
|
+
throw e;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// The text of the config at `ref`, or null when that commit holds none.
|
|
63
|
+
function configAt(root, ref) {
|
|
64
|
+
try {
|
|
65
|
+
return git(root, ['show', `${ref}:${CONFIG_FILE}`]);
|
|
66
|
+
} catch (e) {
|
|
67
|
+
if (e instanceof GitError) return null;
|
|
68
|
+
throw e;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const plain = (text) => text.replace(/\r\n/g, '\n').trimEnd();
|
|
73
|
+
const sameText = (a, b) => a !== null && b !== null && plain(a) === plain(b);
|
|
74
|
+
|
|
75
|
+
// Where two configs differ: the top-level fields whose values differ, or, when one of the two is
|
|
76
|
+
// not a JSON object, the first line that differs.
|
|
77
|
+
function configDifference(here, there) {
|
|
78
|
+
const parse = (text) => {
|
|
79
|
+
try {
|
|
80
|
+
const v = JSON.parse(text);
|
|
81
|
+
return v !== null && typeof v === 'object' && !Array.isArray(v) ? v : null;
|
|
82
|
+
} catch {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
const a = parse(here);
|
|
87
|
+
const b = parse(there);
|
|
88
|
+
if (a && b) {
|
|
89
|
+
const keys = [...new Set([...Object.keys(a), ...Object.keys(b)])].filter((k) => JSON.stringify(a[k]) !== JSON.stringify(b[k]));
|
|
90
|
+
if (keys.length > 0) return `${keys.length === 1 ? 'the field' : 'the fields'} ${keys.join(', ')}`;
|
|
91
|
+
}
|
|
92
|
+
const x = plain(here).split('\n');
|
|
93
|
+
const y = plain(there).split('\n');
|
|
94
|
+
let n = 0;
|
|
95
|
+
while (n < x.length && n < y.length && x[n] === y[n]) n += 1;
|
|
96
|
+
return `line ${n + 1}`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// What becomes of the migrated config, the only uncommitted change, when the session moves to the
|
|
100
|
+
// commit `ref` (issue #162, D-94). -> 'carry' when that commit holds the config HEAD holds, so git
|
|
101
|
+
// carries the change; 'drop' when it holds the migrated config already; else the line to refuse with.
|
|
102
|
+
function migratedConfigTo(root, ref, label) {
|
|
103
|
+
const there = configAt(root, ref);
|
|
104
|
+
if (sameText(there, configAt(root, 'HEAD'))) return 'carry';
|
|
105
|
+
const here = fs.readFileSync(path.join(root, CONFIG_PATH), 'utf8');
|
|
106
|
+
if (sameText(there, here)) return 'drop';
|
|
107
|
+
if (there === null) return `${CONFIG_FILE} is migrated here and not committed, and ${label} holds no ${CONFIG_FILE}; the change is left here and no branch is switched to`;
|
|
108
|
+
return `${CONFIG_FILE} is migrated here and not committed, and ${CONFIG_FILE} on ${label} differs from it in ${configDifference(here, there)}; the change is left here and no branch is switched to`;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// The `release/<slug>-<n>` branches that exist here or on origin, as branch names in the order of <n>.
|
|
112
|
+
function numberedBranches(root, target) {
|
|
113
|
+
const found = new Map();
|
|
114
|
+
const refs = git(root, ['for-each-ref', '--format=%(refname)', `refs/heads/${target}-*`, `refs/remotes/origin/${target}-*`]);
|
|
115
|
+
for (const ref of refs.split('\n').filter((l) => l !== '')) {
|
|
116
|
+
const name = ref.replace(/^refs\/(heads|remotes\/origin)\//, '');
|
|
117
|
+
const m = /^-(\d+)$/.exec(name.slice(target.length));
|
|
118
|
+
if (m) found.set(name, Number(m[1]));
|
|
119
|
+
}
|
|
120
|
+
return [...found.keys()].sort((x, y) => found.get(x) - found.get(y));
|
|
121
|
+
}
|
|
122
|
+
|
|
27
123
|
async function run(args, io) {
|
|
28
124
|
const [kind, slug, ...rest] = args;
|
|
29
125
|
if (!KINDS.includes(kind) || slug === undefined || rest.length > 0) {
|
|
@@ -36,6 +132,9 @@ async function run(args, io) {
|
|
|
36
132
|
}
|
|
37
133
|
const root = io.cwd;
|
|
38
134
|
const target = `${kind}/${slug}`;
|
|
135
|
+
// The first branch of the release family a numbered slug belongs to; else the target itself.
|
|
136
|
+
const numberedSlug = kind === 'release' ? /^(.+)-(\d+)$/.exec(slug) : null;
|
|
137
|
+
const family = numberedSlug && Number(numberedSlug[2]) >= 2 ? `${kind}/${numberedSlug[1]}` : target;
|
|
39
138
|
try {
|
|
40
139
|
const cfg = loadConfig(root);
|
|
41
140
|
const base = assertRef(cfg.defaultBranch);
|
|
@@ -44,7 +143,46 @@ async function run(args, io) {
|
|
|
44
143
|
io.stdout(`${target}\n`);
|
|
45
144
|
return 0;
|
|
46
145
|
}
|
|
47
|
-
const
|
|
146
|
+
const changed = git(root, ['status', '--porcelain', '--untracked-files=no']) !== '';
|
|
147
|
+
const migrated = changed && onlyMigratedConfig(root);
|
|
148
|
+
const dirty = changed && !migrated;
|
|
149
|
+
// Moves the session to the commit `ref` with `go`. The migrated config is carried, dropped
|
|
150
|
+
// first where `ref` holds it already, or the move is refused. -> false when refused, with the
|
|
151
|
+
// line printed. A dropped copy is put back when the move fails. After the move the file holds
|
|
152
|
+
// the bytes the migration wrote: a checkout that converts line endings writes the same config
|
|
153
|
+
// with other ones, which git reads as no change.
|
|
154
|
+
const move = (ref, label, go) => {
|
|
155
|
+
const file = path.join(root, CONFIG_PATH);
|
|
156
|
+
let kept = null;
|
|
157
|
+
if (migrated) {
|
|
158
|
+
const what = migratedConfigTo(root, ref, label);
|
|
159
|
+
if (what !== 'carry' && what !== 'drop') {
|
|
160
|
+
io.stderr(`slicekit: ${what}\n`);
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
if (what === 'drop') {
|
|
164
|
+
kept = fs.readFileSync(file);
|
|
165
|
+
git(root, ['restore', '-q', '--staged', '--worktree', '--source=HEAD', '--', CONFIG_FILE]);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
try {
|
|
169
|
+
go();
|
|
170
|
+
} catch (e) {
|
|
171
|
+
if (kept !== null) fs.writeFileSync(file, kept);
|
|
172
|
+
throw e;
|
|
173
|
+
}
|
|
174
|
+
if (kept !== null) {
|
|
175
|
+
const now = fs.readFileSync(file);
|
|
176
|
+
if (!now.equals(kept) && sameText(now.toString('utf8'), kept.toString('utf8'))) {
|
|
177
|
+
fs.writeFileSync(file, kept);
|
|
178
|
+
// The file's size changed, which git takes for a change until the index entry is read
|
|
179
|
+
// again; the add stages nothing, since the content is the commit's.
|
|
180
|
+
git(root, ['--literal-pathspecs', 'add', '--', CONFIG_FILE]);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return true;
|
|
184
|
+
};
|
|
185
|
+
const short = (ref) => ref.replace(/^refs\/(heads|remotes)\//, '');
|
|
48
186
|
if (dirty && !onTarget) {
|
|
49
187
|
io.stderr('slicekit: commit or stash your changes first\n');
|
|
50
188
|
return 1;
|
|
@@ -65,6 +203,19 @@ async function run(args, io) {
|
|
|
65
203
|
} catch (e) {
|
|
66
204
|
if (!(e instanceof GitError)) throw e;
|
|
67
205
|
}
|
|
206
|
+
// And, for a release, each numbered branch of the slug another clone pushed.
|
|
207
|
+
if (kind === 'release') {
|
|
208
|
+
const specs = [`+refs/heads/${target}-*:refs/remotes/origin/${target}-*`];
|
|
209
|
+
// And, for a numbered slug, the first branch of its family and that one's numbered branches.
|
|
210
|
+
if (family !== target) specs.push(`+refs/heads/${family}:refs/remotes/origin/${family}`, `+refs/heads/${family}-*:refs/remotes/origin/${family}-*`);
|
|
211
|
+
for (const spec of specs) {
|
|
212
|
+
try {
|
|
213
|
+
git(root, ['fetch', '-q', 'origin', spec]);
|
|
214
|
+
} catch (e) {
|
|
215
|
+
if (!(e instanceof GitError)) throw e;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
68
219
|
} catch (e) {
|
|
69
220
|
if (!(e instanceof GitError)) throw e;
|
|
70
221
|
const lines = e.stderr.split('\n');
|
|
@@ -79,30 +230,37 @@ async function run(args, io) {
|
|
|
79
230
|
// `slicekit release` runs only at the default branch's tip. A release branch that stands
|
|
80
231
|
// elsewhere, here or on origin, is moved to the tip when it holds no commit the default
|
|
81
232
|
// branch lacks; one that does is refused: with its open pull request when the host has one,
|
|
82
|
-
// else with the next free slug.
|
|
233
|
+
// else with the next free slug. A numbered branch of the slug, `release/<slug>-<n>`, that
|
|
234
|
+
// holds commits the default branch lacks is asked about too, whatever `release/<slug>` holds.
|
|
235
|
+
// So are the other branches of a numbered slug's family: `release/<base>` and each
|
|
236
|
+
// `release/<base>-<n>`.
|
|
83
237
|
const tip = git(root, ['rev-parse', '--verify', `${from}^{commit}`]);
|
|
84
238
|
const copies = [`refs/heads/${target}`, remote].filter((ref) => resolves(root, ref));
|
|
85
239
|
const stale = copies.filter((ref) => git(root, ['rev-parse', '--verify', `${ref}^{commit}`]) !== tip);
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
return 1;
|
|
104
|
-
}
|
|
240
|
+
const ahead = (ref) => git(root, ['rev-list', '--count', `${from}..${ref}`]) !== '0';
|
|
241
|
+
const own = stale.some(ahead);
|
|
242
|
+
// A branch may be that of the phase's open release pull request; a second slug would then
|
|
243
|
+
// open a second one beside it. A pull request needs its branch on origin, so the host is
|
|
244
|
+
// asked unless a fetch that worked found no such branch there.
|
|
245
|
+
const onOrigin = (name) => origin && (!fetched || resolves(root, `refs/remotes/origin/${name}`));
|
|
246
|
+
const holdsOwn = (name) => [`refs/heads/${name}`, `refs/remotes/origin/${name}`].some((ref) => resolves(root, ref) && ahead(ref));
|
|
247
|
+
const numbered = numberedBranches(root, target).filter(holdsOwn);
|
|
248
|
+
const kin = family === target ? [] : [family, ...numberedBranches(root, family)].filter((name) => name !== target && holdsOwn(name));
|
|
249
|
+
for (const name of [...kin, ...(own ? [target] : []), ...numbered].filter(onOrigin)) {
|
|
250
|
+
let open;
|
|
251
|
+
try {
|
|
252
|
+
open = getHost(cfg, io.env ?? process.env, root).pullRequestForBranch(name, { base });
|
|
253
|
+
} catch (e) {
|
|
254
|
+
if (!(e instanceof GitHostError)) throw e;
|
|
255
|
+
io.stderr(`slicekit: ${name} holds commits ${base} lacks, and the git host could not be asked for its open pull request (${e.message}); no branch is cut\n`);
|
|
256
|
+
return 1;
|
|
105
257
|
}
|
|
258
|
+
if (open) {
|
|
259
|
+
io.stderr(`slicekit: ${name} is the branch of open pull request #${open.number} (${open.url}); the phase's release is already open\n`);
|
|
260
|
+
return 1;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (own) {
|
|
106
264
|
let n = 2;
|
|
107
265
|
while (resolves(root, `refs/heads/${target}-${n}`) || resolves(root, `refs/remotes/origin/${target}-${n}`)) n += 1;
|
|
108
266
|
io.stderr(`slicekit: ${target} holds commits ${base} lacks, so no release runs on it; run slicekit branch release ${slug}-${n}\n`);
|
|
@@ -118,15 +276,18 @@ async function run(args, io) {
|
|
|
118
276
|
return 1;
|
|
119
277
|
}
|
|
120
278
|
// -C makes the branch, or moves it, to stand at the tip; it held no commit of its own.
|
|
121
|
-
git(root, ['switch', '-q', '--no-track', '-C', target, from]);
|
|
279
|
+
if (!move(from, short(from), () => git(root, ['switch', '-q', '--no-track', '-C', target, from]))) return 1;
|
|
122
280
|
io.stderr(`slicekit: ${target} was behind ${base}; moved to the tip of ${base}\n`);
|
|
123
281
|
io.stdout(`${target}\n`);
|
|
124
282
|
return 0;
|
|
125
283
|
}
|
|
126
284
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
285
|
+
const local = `refs/heads/${target}`;
|
|
286
|
+
let moved;
|
|
287
|
+
if (resolves(root, local)) moved = move(local, target, () => switchBranch(root, target));
|
|
288
|
+
else if (resolves(root, remote)) moved = move(remote, short(remote), () => switchBranch(root, target, { create: true, from: remote, track: true }));
|
|
289
|
+
else moved = move(from, short(from), () => switchBranch(root, target, { create: true, from }));
|
|
290
|
+
if (!moved) return 1;
|
|
130
291
|
io.stdout(`${target}\n`);
|
|
131
292
|
return 0;
|
|
132
293
|
} catch (e) {
|
package/src/cli/index.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { CONFIG_PATH, ConfigNewerError, loadConfig, migrateConfigFile } from '../config.js';
|
|
4
|
+
import { currentBranch } from '../git.js';
|
|
5
|
+
import { sliceFromBranch } from '../ids.js';
|
|
6
|
+
import { findSliceFiles, parseSliceFile } from '../slicefile.js';
|
|
7
|
+
import { auditCommand } from './audit.js';
|
|
2
8
|
import { baselineCommand } from './baseline.js';
|
|
3
9
|
import { branchCommand } from './branch.js';
|
|
4
10
|
import { buildCommand } from './build.js';
|
|
@@ -14,6 +20,7 @@ import { intakeCommand } from './intake.js';
|
|
|
14
20
|
import { prCommand } from './pr.js';
|
|
15
21
|
import { instructionsCommand } from './instructions.js';
|
|
16
22
|
import { journalCommand } from './journal.js';
|
|
23
|
+
import { opsCommand } from './ops.js';
|
|
17
24
|
import { planCommand } from './plan.js';
|
|
18
25
|
import { hookCommand } from './hook.js';
|
|
19
26
|
import { readyLabelCommand } from './ready-label.js';
|
|
@@ -33,6 +40,7 @@ import { workflowCommand } from './workflow.js';
|
|
|
33
40
|
|
|
34
41
|
// Later tasks add entries: { [name]: { run(args, io) -> Promise<number>, usage: string, subcommands?: string[] } }
|
|
35
42
|
export const COMMANDS = {
|
|
43
|
+
audit: auditCommand,
|
|
36
44
|
baseline: baselineCommand,
|
|
37
45
|
branch: branchCommand,
|
|
38
46
|
build: buildCommand,
|
|
@@ -48,6 +56,7 @@ export const COMMANDS = {
|
|
|
48
56
|
instructions: instructionsCommand,
|
|
49
57
|
hook: hookCommand,
|
|
50
58
|
journal: journalCommand,
|
|
59
|
+
ops: opsCommand,
|
|
51
60
|
plan: planCommand,
|
|
52
61
|
pr: prCommand,
|
|
53
62
|
'ready-label': readyLabelCommand,
|
|
@@ -114,9 +123,76 @@ export async function main(argv, io) {
|
|
|
114
123
|
io.stdout(commandUsage(command));
|
|
115
124
|
return 0;
|
|
116
125
|
}
|
|
126
|
+
// REQ-5.6, D-60, D-93: an older config is migrated before the command reads it. A hook never
|
|
127
|
+
// writes the team's tracked file mid-turn; it reads the config migrated in memory (loadConfig).
|
|
128
|
+
// D-93 names two more cases that read it migrated, write nothing and print one line: a slice
|
|
129
|
+
// branch or a slice's review branch (writesMigration), and `slicekit branch` of every kind, which
|
|
130
|
+
// refuses a changed tracked file; the branch is cut first and the next command writes on it. A
|
|
131
|
+
// migration an earlier command already wrote is carried by `slicekit branch` itself (D-94).
|
|
132
|
+
if (command !== COMMANDS.hook && typeof io.cwd === 'string') {
|
|
133
|
+
try {
|
|
134
|
+
// The branch is asked about only when the config is older, so a current one costs no git call.
|
|
135
|
+
const older = migrateConfigFile(io.cwd, { write: false });
|
|
136
|
+
const here = older !== null && writesMigration(io.cwd);
|
|
137
|
+
const write = here && command !== COMMANDS.branch;
|
|
138
|
+
const migrated = write ? migrateConfigFile(io.cwd) : older;
|
|
139
|
+
if (migrated && !write) {
|
|
140
|
+
const where = here
|
|
141
|
+
? 'slicekit branch writes nothing, and the next command writes it on the branch it runs on'
|
|
142
|
+
: 'it is migrated on the default branch or on a plan branch';
|
|
143
|
+
io.stderr(`config: ${CONFIG_FILE} is at configVersion ${migrated.from} and is read as ${migrated.to} here, unwritten; ${where}
|
|
144
|
+
`);
|
|
145
|
+
} else if (migrated) {
|
|
146
|
+
io.stderr(`config: migrated ${CONFIG_FILE} from configVersion ${migrated.from} to ${migrated.to}
|
|
147
|
+
`);
|
|
148
|
+
for (const change of migrated.changes) io.stderr(` - ${change}
|
|
149
|
+
`);
|
|
150
|
+
}
|
|
151
|
+
} catch (e) {
|
|
152
|
+
if (!(e instanceof ConfigNewerError)) throw e;
|
|
153
|
+
io.stderr(`slicekit: ${e.message}
|
|
154
|
+
`);
|
|
155
|
+
return 2;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
117
158
|
return command.run(rest, io);
|
|
118
159
|
}
|
|
119
160
|
|
|
161
|
+
const CONFIG_FILE = CONFIG_PATH.split(path.sep).join('/');
|
|
162
|
+
|
|
163
|
+
// Whether a command run in `cwd` may write the migrated config (review r1, H-1; D-93). On a slice branch
|
|
164
|
+
// or a slice's review branch no build step commits a changed config (`build commit` refuses a kit
|
|
165
|
+
// file, `build pr` an uncommitted change), so there the config is read migrated and left as it is.
|
|
166
|
+
// The exception is the slice whose Expected changes name the config: its slice branch and its review
|
|
167
|
+
// branch both write, since a review branch's commits go to the slice branch and `build config`
|
|
168
|
+
// commits the file (D-53, D-94). Anything unreadable on the way (no slice file, a config that does
|
|
169
|
+
// not load) is no reason to write.
|
|
170
|
+
function writesMigration(cwd) {
|
|
171
|
+
let branch;
|
|
172
|
+
try {
|
|
173
|
+
branch = currentBranch(cwd);
|
|
174
|
+
} catch {
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
const id = branch === null ? null : sliceFromBranch(branch);
|
|
178
|
+
if (id === null) return true;
|
|
179
|
+
try {
|
|
180
|
+
const cfg = loadConfig(cwd);
|
|
181
|
+
for (const f of findSliceFiles(cwd, cfg)) {
|
|
182
|
+
let slice;
|
|
183
|
+
try {
|
|
184
|
+
slice = parseSliceFile(fs.readFileSync(f.path, 'utf8'), f.path);
|
|
185
|
+
} catch {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (slice.id === id) return slice.expectedChanges.includes(CONFIG_FILE);
|
|
189
|
+
}
|
|
190
|
+
} catch {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
|
|
120
196
|
function commandUsage(command) {
|
|
121
197
|
const lines = command.usage.split('\n').map((line) => ` slicekit ${line.trim()}`);
|
|
122
198
|
return `usage:\n${lines.join('\n')}\n`;
|
package/src/cli/ops.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// slicekit ops context <symptom> [--path <p>]… [--json] and slicekit ops check <report> (REQ-2.13,
|
|
2
|
+
// D-57): what `/slicekit ops` hands the ops agent, and the check of the report it replies with.
|
|
3
|
+
// Neither runs git, calls the host or takes an action.
|
|
4
|
+
import { parseArgs } from 'node:util';
|
|
5
|
+
import { ConfigError, loadConfig } from '../config.js';
|
|
6
|
+
import { historyLine } from '../graph/history.js';
|
|
7
|
+
import { indexGraph, loadGraph } from '../graph/load.js';
|
|
8
|
+
import { GraphError } from '../graph/schema.js';
|
|
9
|
+
import { opsContext, opsReportProblems } from '../ops.js';
|
|
10
|
+
import { readReport } from '../reports.js';
|
|
11
|
+
|
|
12
|
+
const USAGE = 'ops context <symptom> [--path <p>]… [--json]\n ops check <report>';
|
|
13
|
+
const SUBCOMMANDS = ['context', 'check'];
|
|
14
|
+
|
|
15
|
+
const usage = (io, why) => {
|
|
16
|
+
io.stderr(`${why ? `slicekit: ${why}\n` : ''}usage: slicekit ${USAGE}\n`);
|
|
17
|
+
return 2;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function contextLines(c) {
|
|
21
|
+
return [
|
|
22
|
+
c.report,
|
|
23
|
+
`symptom: ${c.symptom}`,
|
|
24
|
+
...c.files.map((f) => `file: ${f}`),
|
|
25
|
+
...c.broad.map((b) => `too broad: ${b.word} (${b.count} files)`),
|
|
26
|
+
...(c.unmatched.length > 0 ? [`no file: ${c.unmatched.join(', ')}`] : []),
|
|
27
|
+
'history:',
|
|
28
|
+
...c.history.map(historyLine),
|
|
29
|
+
`runbook: ${c.runbook}${c.missing.includes(c.runbook) ? ' (missing)' : ''}`,
|
|
30
|
+
`observability: ${c.observability}${c.missing.includes(c.observability) ? ' (missing)' : ''}`,
|
|
31
|
+
`last release: ${c.lastRelease ?? 'none'}`,
|
|
32
|
+
];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function run(args, io) {
|
|
36
|
+
let values;
|
|
37
|
+
let positionals;
|
|
38
|
+
try {
|
|
39
|
+
({ values, positionals } = parseArgs({ args, options: { path: { type: 'string', multiple: true }, json: { type: 'boolean' } }, allowPositionals: true }));
|
|
40
|
+
} catch (e) {
|
|
41
|
+
return usage(io, e.message);
|
|
42
|
+
}
|
|
43
|
+
const [sub, ...rest] = positionals;
|
|
44
|
+
if (!SUBCOMMANDS.includes(sub)) return usage(io, sub === undefined ? 'missing subcommand' : `unknown subcommand '${sub}'`);
|
|
45
|
+
const json = Boolean(values.json);
|
|
46
|
+
const refuse = (message, code) => {
|
|
47
|
+
io.stderr(`slicekit: ${message}\n`);
|
|
48
|
+
if (json) io.stdout(`${JSON.stringify({ error: message }, null, 2)}\n`);
|
|
49
|
+
return code;
|
|
50
|
+
};
|
|
51
|
+
const root = io.cwd;
|
|
52
|
+
try {
|
|
53
|
+
const cfg = loadConfig(root);
|
|
54
|
+
if (sub === 'context') {
|
|
55
|
+
// The words of a symptom typed without quotes are one symptom.
|
|
56
|
+
const symptom = rest.join(' ').trim();
|
|
57
|
+
const paths = values.path ?? [];
|
|
58
|
+
if (symptom === '' && paths.length === 0) return usage(io, 'missing symptom');
|
|
59
|
+
const context = opsContext(root, cfg, symptom, { paths });
|
|
60
|
+
io.stdout(json ? `${JSON.stringify(context, null, 2)}\n` : contextLines(context).map((l) => `${l}\n`).join(''));
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
if (rest.length !== 1) return usage(io, rest.length === 0 ? 'missing report' : `unexpected argument ${rest[1]}`);
|
|
64
|
+
if (values.path) return usage(io, 'ops check takes no --path');
|
|
65
|
+
const { rel, text } = readReport(root, rest[0]);
|
|
66
|
+
if (text === null) return refuse(`${rel} is not a file`, 2);
|
|
67
|
+
const problems = opsReportProblems(root, indexGraph(loadGraph(root, cfg)), rel, text);
|
|
68
|
+
if (json) io.stdout(`${JSON.stringify({ report: rel, problems }, null, 2)}\n`);
|
|
69
|
+
else io.stdout(problems.length > 0 ? problems.map((p) => `${p}\n`).join('') : `ok ${rel}\n`);
|
|
70
|
+
return problems.length > 0 ? 1 : 0;
|
|
71
|
+
} catch (e) {
|
|
72
|
+
if (e instanceof GraphError) return refuse(e.message, e.code);
|
|
73
|
+
if (e instanceof ConfigError) return refuse(e.message, 2);
|
|
74
|
+
throw e;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export const opsCommand = { run, usage: USAGE, subcommands: SUBCOMMANDS };
|
package/src/cli/update.js
CHANGED
|
@@ -2,6 +2,7 @@ import path from 'node:path';
|
|
|
2
2
|
import { parseArgs } from 'node:util';
|
|
3
3
|
import { ConfigError, loadConfig } from '../config.js';
|
|
4
4
|
import { exists } from '../fs.js';
|
|
5
|
+
import { missingReportsRule } from '../init.js';
|
|
5
6
|
import { checkCli } from '../hook-cli.js';
|
|
6
7
|
import { kitVersion, RENDERED } from '../render/index.js';
|
|
7
8
|
import { applyUpdate, planUpdate } from '../render/update.js';
|
|
@@ -40,6 +41,9 @@ async function run(args, io) {
|
|
|
40
41
|
io.stdout(`${status} ${dest}\n`);
|
|
41
42
|
if (diff !== '') io.stdout(diff);
|
|
42
43
|
}
|
|
44
|
+
// update never writes the ignore file (REQ-5.6): a project that lacks the rule is told the line.
|
|
45
|
+
const rule = missingReportsRule(root, cfg);
|
|
46
|
+
if (rule) io.stdout(`.gitignore has no rule for ${rule}, where /slicekit ops and /slicekit audit write their reports; add the line ${rule}\n`);
|
|
43
47
|
if (values.check) {
|
|
44
48
|
if (!exists(path.join(root, RENDERED))) io.stdout(`${RENDERED}/ is missing; commit it so drift can be told from kit changes\n`);
|
|
45
49
|
return pending.some((p) => p.status === 'drifted' || p.status === 'missing') ? 1 : 0;
|
package/src/config.js
CHANGED
|
@@ -3,7 +3,7 @@ import fs from 'node:fs';
|
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import { fileURLToPath } from 'node:url';
|
|
5
5
|
|
|
6
|
-
export const CONFIG_VERSION =
|
|
6
|
+
export const CONFIG_VERSION = 2;
|
|
7
7
|
export const SHAPES = ['backend', 'fullstack', 'data'];
|
|
8
8
|
export const CONFIG_PATH = path.join('.slicekit', 'config.json');
|
|
9
9
|
|
|
@@ -30,6 +30,49 @@ export class ConfigError extends Error {
|
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
// D-33: a config written by a newer kit is refused, never read as this kit's version.
|
|
34
|
+
export class ConfigNewerError extends ConfigError {
|
|
35
|
+
constructor(version) {
|
|
36
|
+
super([`config: configVersion ${version} is newer than this kit's ${CONFIG_VERSION}; upgrade slicekit`]);
|
|
37
|
+
this.name = 'ConfigNewerError';
|
|
38
|
+
this.version = version;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const WORKSPACE_PATH = '.slicekit/workspace.json';
|
|
43
|
+
|
|
44
|
+
// REQ-5.6, D-60: one entry per configVersion, taking a config at that version to the next one. Each
|
|
45
|
+
// changes the config in place and returns one line per change.
|
|
46
|
+
export const MIGRATIONS = {
|
|
47
|
+
// Version 2 removes repos, accepted and unread since D-47: the workspace file is the only list.
|
|
48
|
+
1: (cfg) => {
|
|
49
|
+
const held = Array.isArray(cfg.repos) ? cfg.repos : [];
|
|
50
|
+
delete cfg.repos;
|
|
51
|
+
const changes = ['removed repos, unread since D-47'];
|
|
52
|
+
if (held.length > 0) {
|
|
53
|
+
const names = held.map((r) => (typeof r === 'string' ? r : typeof r?.name === 'string' ? r.name : JSON.stringify(r)));
|
|
54
|
+
changes.push(`repos held ${names.join(', ')}; list them in ${WORKSPACE_PATH}`);
|
|
55
|
+
}
|
|
56
|
+
return changes;
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// -> { cfg, from, changes }: a copy of `cfg` at CONFIG_VERSION, the version read, and one line per
|
|
61
|
+
// change (none when it was current). A configVersion that is not an integer of 1 or more is left as
|
|
62
|
+
// it is for validateConfig to name; one above CONFIG_VERSION throws ConfigNewerError.
|
|
63
|
+
export function migrateConfig(cfg) {
|
|
64
|
+
const from = cfg !== null && typeof cfg === 'object' && !Array.isArray(cfg) ? cfg.configVersion : undefined;
|
|
65
|
+
if (!(Number.isInteger(from) && from >= 1)) return { cfg, from, changes: [] };
|
|
66
|
+
if (from > CONFIG_VERSION) throw new ConfigNewerError(from);
|
|
67
|
+
const copy = structuredClone(cfg);
|
|
68
|
+
const changes = [];
|
|
69
|
+
for (let v = from; v < CONFIG_VERSION; v += 1) {
|
|
70
|
+
changes.push(...MIGRATIONS[v](copy));
|
|
71
|
+
copy.configVersion = v + 1;
|
|
72
|
+
}
|
|
73
|
+
return { cfg: copy, from, changes };
|
|
74
|
+
}
|
|
75
|
+
|
|
33
76
|
// The kit's own filler list, before any team extends it.
|
|
34
77
|
export function seedBanned() {
|
|
35
78
|
const file = fileURLToPath(new URL('../templates/style.json', import.meta.url));
|
|
@@ -47,7 +90,6 @@ export function defaultConfig({ name, shape, docsRoot = 'docs', commands = {}, c
|
|
|
47
90
|
cli,
|
|
48
91
|
protectedBranches: [defaultBranch],
|
|
49
92
|
commands: { test: '', lint: '', build: '', e2e: '', ...commands },
|
|
50
|
-
repos: [],
|
|
51
93
|
gitHost: { kind: 'github' },
|
|
52
94
|
tracker: { kind: 'github-issues' },
|
|
53
95
|
review: { independent: 'required' },
|
|
@@ -212,7 +254,9 @@ export function validateConfig(cfg) {
|
|
|
212
254
|
if (!COMMAND_KEYS.includes(key)) errors.push(`commands.${key}: unknown command; use ${COMMAND_KEYS.join(', ')}`);
|
|
213
255
|
}
|
|
214
256
|
}
|
|
215
|
-
if (
|
|
257
|
+
if (cfg.configVersion === CONFIG_VERSION && Object.hasOwn(cfg, 'repos')) {
|
|
258
|
+
errors.push(`repos: removed in configVersion 2; list repositories in ${WORKSPACE_PATH}`);
|
|
259
|
+
}
|
|
216
260
|
for (const key of ['gitHost', 'tracker']) {
|
|
217
261
|
if (!isObject(cfg[key]) || typeof cfg[key].kind !== 'string') errors.push(`${key}.kind: must be a string`);
|
|
218
262
|
}
|
|
@@ -286,6 +330,8 @@ export function compileDestructive(cfg) {
|
|
|
286
330
|
return cfg.destructive.map((src) => new RegExp(src, 'i'));
|
|
287
331
|
}
|
|
288
332
|
|
|
333
|
+
// An older config is read migrated (D-60); the file is never written here, so a hook that loads the
|
|
334
|
+
// config leaves the tracked file as it is.
|
|
289
335
|
export function loadConfig(root) {
|
|
290
336
|
const file = path.join(root, CONFIG_PATH);
|
|
291
337
|
let text;
|
|
@@ -294,17 +340,46 @@ export function loadConfig(root) {
|
|
|
294
340
|
} catch {
|
|
295
341
|
throw new ConfigError([`config: cannot read ${CONFIG_PATH}`]);
|
|
296
342
|
}
|
|
297
|
-
let
|
|
343
|
+
let raw;
|
|
298
344
|
try {
|
|
299
|
-
|
|
345
|
+
raw = JSON.parse(text);
|
|
300
346
|
} catch {
|
|
301
347
|
throw new ConfigError([`config: ${CONFIG_PATH} is not valid JSON`]);
|
|
302
348
|
}
|
|
349
|
+
const { cfg } = migrateConfig(raw);
|
|
303
350
|
const errors = validateConfig(cfg);
|
|
304
351
|
if (errors.length > 0) throw new ConfigError(errors);
|
|
305
352
|
return cfg;
|
|
306
353
|
}
|
|
307
354
|
|
|
355
|
+
// REQ-5.6, D-60: the config file of `root` moved to CONFIG_VERSION when it is older, written through
|
|
356
|
+
// a temporary name and a rename. -> { from, to, changes }, or null when there is no config file, it
|
|
357
|
+
// is not JSON, or it is current. A newer configVersion throws ConfigNewerError and writes nothing.
|
|
358
|
+
// With `write` false the same answer is given and the file is left as it is (D-93: main() asks so
|
|
359
|
+
// on a slice branch or a slice's review branch, and for `slicekit branch`).
|
|
360
|
+
export function migrateConfigFile(root, { write = true } = {}) {
|
|
361
|
+
const file = path.join(root, CONFIG_PATH);
|
|
362
|
+
let raw;
|
|
363
|
+
try {
|
|
364
|
+
raw = JSON.parse(fs.readFileSync(file, 'utf8').replace(/^/, ''));
|
|
365
|
+
} catch {
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
const { cfg, from, changes } = migrateConfig(raw);
|
|
369
|
+
if (cfg === raw || from === CONFIG_VERSION) return null;
|
|
370
|
+
if (!write) return { from, to: CONFIG_VERSION, changes };
|
|
371
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
372
|
+
try {
|
|
373
|
+
fs.writeFileSync(temporary, `${JSON.stringify(cfg, null, 2)}
|
|
374
|
+
`);
|
|
375
|
+
fs.renameSync(temporary, file);
|
|
376
|
+
} catch (e) {
|
|
377
|
+
fs.rmSync(temporary, { force: true });
|
|
378
|
+
throw e;
|
|
379
|
+
}
|
|
380
|
+
return { from, to: CONFIG_VERSION, changes };
|
|
381
|
+
}
|
|
382
|
+
|
|
308
383
|
// The machine's own settings (D-43): `.slicekit/local.json`, untracked, in the session root (the
|
|
309
384
|
// main checkout). Only `testCommand` is read, by the build steps that run the test command on this
|
|
310
385
|
// machine (`build red`, `build commit --require-green`, `build pr`), and by the `commands` rule
|
|
@@ -15,6 +15,10 @@ import { addAgent, usableId } from './subagent-marker.js';
|
|
|
15
15
|
export { roleOf };
|
|
16
16
|
export const BRIEF_MAX = 8000;
|
|
17
17
|
export const NO_GIT = 'You never run a git command that commits, pushes or moves a branch; report and the command commits.';
|
|
18
|
+
// The ops agent holds no tool that runs a command or writes a file, and nothing of its work is
|
|
19
|
+
// committed (D-57): it is told what becomes of its reply instead.
|
|
20
|
+
export const OPS_REPLY = 'You run no command and write no file. Your reply is the report: the main session writes it under .slicekit/reports/ and checks it, and it is never committed.';
|
|
21
|
+
const closing = (role) => (role === 'ops' ? OPS_REPLY : NO_GIT);
|
|
18
22
|
const SKILL = path.join('.claude', 'skills', 'slicekit-writing', 'SKILL.md');
|
|
19
23
|
const KIT_SKILL = fileURLToPath(new URL('../../templates/claude-code/skills/slicekit-writing/SKILL.md', import.meta.url));
|
|
20
24
|
|
|
@@ -74,11 +78,11 @@ function withoutRules(text) {
|
|
|
74
78
|
return out.join('\n').trim();
|
|
75
79
|
}
|
|
76
80
|
|
|
77
|
-
// The brief, the rules and the no-git sentence
|
|
78
|
-
// sentence always last and whole. A brief that does not fit is cut from its end and followed by a
|
|
81
|
+
// The brief, the rules and the role's closing sentence (the no-git sentence; for ops, OPS_REPLY) in
|
|
82
|
+
// at most BRIEF_MAX characters, the rules and the sentence always last and whole. A brief that does not fit is cut from its end and followed by a
|
|
79
83
|
// line naming where the full text is. Only when the rules alone do not fit are they cut too.
|
|
80
84
|
function assemble(brief, rules, role, cli) {
|
|
81
|
-
const tail = `\n\n${
|
|
85
|
+
const tail = `\n\n${closing(role)}`;
|
|
82
86
|
const end = rules.text === '' ? tail : `\n\n${rules.text}${tail}`;
|
|
83
87
|
if (brief.length + end.length <= BRIEF_MAX) return brief + end;
|
|
84
88
|
const note = `\n(brief truncated; full text in ${cli} instructions ${role})`;
|