@sddio/cli 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +139 -2
- package/dist/agents.js +35 -0
- package/dist/brief.js +104 -0
- package/dist/check.js +109 -0
- package/dist/claims.js +60 -0
- package/dist/cli.js +341 -0
- package/dist/cursor.js +65 -0
- package/dist/events.js +37 -0
- package/dist/hooks.js +68 -0
- package/dist/init.js +172 -0
- package/dist/paths.js +31 -0
- package/dist/prompt.js +63 -0
- package/dist/repo.js +93 -0
- package/dist/workspace.js +25 -0
- package/package.json +54 -4
- package/templates/.sddio/context/README.md +81 -0
- package/templates/.sddio/context/config.json +16 -0
- package/templates/.sddio/context/instructions/.schema/adr.md +28 -0
- package/templates/.sddio/context/instructions/.schema/discussion.md +16 -0
- package/templates/.sddio/context/instructions/.schema/spec.md +15 -0
- package/templates/.sddio/context/instructions/adr.md +43 -0
- package/templates/.sddio/context/instructions/bootstrap.md +54 -0
- package/templates/.sddio/context/instructions/discussion.md +31 -0
- package/templates/.sddio/context/instructions/entries.md +61 -0
- package/templates/.sddio/context/instructions/reconciliation.md +92 -0
- package/templates/.sddio/context/instructions/spec.md +50 -0
- package/templates/.sddio/context/instructions.md +64 -0
- package/templates/.sddio/context/preferences.md +16 -0
- package/templates/.sddio/context/roadmap.md +28 -0
- package/templates/.sddio/context/routes/routes.md +26 -0
- package/templates/.sddio/context/writing.md +80 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Entry point. Compiled to `dist/cli.js`, which is what `bin` points at, so
|
|
4
|
+
* `npx @sddio/cli` runs prebuilt output with no build step on the consumer
|
|
5
|
+
* side (ADR-002).
|
|
6
|
+
*/
|
|
7
|
+
import { readFileSync } from 'node:fs';
|
|
8
|
+
import { join, resolve } from 'node:path';
|
|
9
|
+
import { createInterface } from 'node:readline/promises';
|
|
10
|
+
import { runInit } from './init.js';
|
|
11
|
+
import { DEFAULT_HOOK, hasDeclaredHooks } from './hooks.js';
|
|
12
|
+
import { renderBrief, renderEntry } from './brief.js';
|
|
13
|
+
import { inspect, renderCheck, renderDiff } from './check.js';
|
|
14
|
+
import { readClaims, renderClaims, runClaims } from './claims.js';
|
|
15
|
+
import { measure, recordEvent } from './events.js';
|
|
16
|
+
import { findRoot, readEntries } from './repo.js';
|
|
17
|
+
import { packageRoot } from './paths.js';
|
|
18
|
+
import { renderNextStep, selectNextStep } from './prompt.js';
|
|
19
|
+
const USAGE = `sddio — spec-driven development artifacts for a project
|
|
20
|
+
|
|
21
|
+
Usage:
|
|
22
|
+
sddio init [--dir <path>] [--hook <name>] [--no-install] [--yes]
|
|
23
|
+
[--force] [--dry-run]
|
|
24
|
+
sddio brief [<path>...]
|
|
25
|
+
sddio show <id>
|
|
26
|
+
sddio check [--json]
|
|
27
|
+
sddio diff [--json]
|
|
28
|
+
sddio start [<path>...]
|
|
29
|
+
sddio --version
|
|
30
|
+
sddio --help
|
|
31
|
+
|
|
32
|
+
Commands:
|
|
33
|
+
init Copy .sddio and sddio_docs templates into the target project,
|
|
34
|
+
register the sddio snippet in AGENTS.md, and record the hook that
|
|
35
|
+
triggers reconciliation.
|
|
36
|
+
brief What the workspace knows about the paths in front of you. Titles
|
|
37
|
+
only; use show for a body. Reads nothing else.
|
|
38
|
+
show Print one entry by ID.
|
|
39
|
+
check Is the workspace behind HEAD, and did any moved file go unrecorded.
|
|
40
|
+
Exit 1 when behind, for CI.
|
|
41
|
+
diff The worklist: which entries name a moved file, and which files
|
|
42
|
+
nobody has written about. Prints; never writes.
|
|
43
|
+
start One call for a hook: the cursor, a hint, and the brief.
|
|
44
|
+
|
|
45
|
+
Options:
|
|
46
|
+
--dir <path> Target project root (default: current directory)
|
|
47
|
+
--hook <name> Hook to declare: flow-trail (default) or none. Naming one
|
|
48
|
+
installs it into the project unless --no-install is given.
|
|
49
|
+
--no-install Declare the hook without running its installer
|
|
50
|
+
--yes Take the default hook without asking, and install it
|
|
51
|
+
--force Overwrite existing files, including sddio_docs
|
|
52
|
+
--dry-run Report what would happen without writing anything
|
|
53
|
+
-h, --help Show this help
|
|
54
|
+
-v, --version Show the version
|
|
55
|
+
`;
|
|
56
|
+
function readVersion() {
|
|
57
|
+
const manifest = readFileSync(join(packageRoot, 'package.json'), 'utf8');
|
|
58
|
+
return JSON.parse(manifest).version;
|
|
59
|
+
}
|
|
60
|
+
function parseInitArgs(args) {
|
|
61
|
+
const parsed = { cwd: process.cwd(), force: false, dryRun: false, yes: false, noInstall: false };
|
|
62
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
63
|
+
const argument = args[index];
|
|
64
|
+
if (argument === '--force') {
|
|
65
|
+
parsed.force = true;
|
|
66
|
+
}
|
|
67
|
+
else if (argument === '--dry-run') {
|
|
68
|
+
parsed.dryRun = true;
|
|
69
|
+
}
|
|
70
|
+
else if (argument === '--yes') {
|
|
71
|
+
parsed.yes = true;
|
|
72
|
+
}
|
|
73
|
+
else if (argument === '--no-install') {
|
|
74
|
+
parsed.noInstall = true;
|
|
75
|
+
}
|
|
76
|
+
else if (argument === '--hook') {
|
|
77
|
+
const value = args[index + 1];
|
|
78
|
+
if (value === undefined) {
|
|
79
|
+
throw new Error('--hook requires a name: flow-trail or none');
|
|
80
|
+
}
|
|
81
|
+
parsed.hook = value;
|
|
82
|
+
index += 1;
|
|
83
|
+
}
|
|
84
|
+
else if (argument === '--dir') {
|
|
85
|
+
const value = args[index + 1];
|
|
86
|
+
if (value === undefined) {
|
|
87
|
+
throw new Error('--dir requires a path');
|
|
88
|
+
}
|
|
89
|
+
parsed.cwd = resolve(value);
|
|
90
|
+
index += 1;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
throw new Error(`Unknown option: ${String(argument)}`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return parsed;
|
|
97
|
+
}
|
|
98
|
+
function report(result) {
|
|
99
|
+
const label = (value) => value.padEnd(12);
|
|
100
|
+
console.log(`sddio init ${readVersion()}${result.dryRun ? ' (dry run)' : ''}`);
|
|
101
|
+
console.log(`target: ${result.target}\n`);
|
|
102
|
+
for (const file of result.overwritten) {
|
|
103
|
+
console.log(` ${label('overwritten')}${file}`);
|
|
104
|
+
}
|
|
105
|
+
for (const file of result.created) {
|
|
106
|
+
console.log(` ${label('created')}${file}`);
|
|
107
|
+
}
|
|
108
|
+
for (const file of result.skipped) {
|
|
109
|
+
console.log(` ${label('skipped')}${file}`);
|
|
110
|
+
}
|
|
111
|
+
console.log(`\ncreated ${result.created.length}, overwritten ${result.overwritten.length}, skipped ${result.skipped.length}`);
|
|
112
|
+
if (result.hook !== null) {
|
|
113
|
+
console.log(`hook: ${result.hook}${result.install === null ? ' (declared, not installed)' : ''}`);
|
|
114
|
+
}
|
|
115
|
+
if (result.dryRun) {
|
|
116
|
+
console.log('dry run: nothing was written');
|
|
117
|
+
}
|
|
118
|
+
for (const error of result.errors) {
|
|
119
|
+
console.error(`error: ${error}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* One question, one default. The alternative is a flag nobody reads, and the
|
|
124
|
+
* choice has to be written into `config.json` either way. A project that has
|
|
125
|
+
* already answered is not asked again: an upgrade is not a re-interview.
|
|
126
|
+
*/
|
|
127
|
+
async function askHook() {
|
|
128
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
129
|
+
try {
|
|
130
|
+
console.log('\nWhich hook should trigger reconciliation?');
|
|
131
|
+
console.log(' 1) flow-trail (default) — a card board in `flow-trail/`; installed into this project');
|
|
132
|
+
console.log(' 2) none — nothing calls sddio; you run the check yourself');
|
|
133
|
+
const answer = (await rl.question('Choose [1]: ')).trim().toLowerCase();
|
|
134
|
+
return answer === '2' || answer === 'none' ? 'none' : 'flow-trail';
|
|
135
|
+
}
|
|
136
|
+
finally {
|
|
137
|
+
rl.close();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
async function commandInit(args) {
|
|
141
|
+
const options = parseInitArgs(args);
|
|
142
|
+
const alreadyDeclared = hasDeclaredHooks(options.cwd);
|
|
143
|
+
const interactive = options.hook === undefined &&
|
|
144
|
+
!options.yes &&
|
|
145
|
+
!alreadyDeclared &&
|
|
146
|
+
process.stdin.isTTY === true &&
|
|
147
|
+
process.stdout.isTTY === true;
|
|
148
|
+
if (interactive) {
|
|
149
|
+
options.hook = await askHook();
|
|
150
|
+
}
|
|
151
|
+
// `--yes` takes the same default the question offers. On an upgrade the
|
|
152
|
+
// question is not asked and the existing declaration stands, so a second
|
|
153
|
+
// `--yes` does not re-download anything.
|
|
154
|
+
if (options.hook === undefined && options.yes && !alreadyDeclared) {
|
|
155
|
+
options.hook = DEFAULT_HOOK;
|
|
156
|
+
}
|
|
157
|
+
const result = await runInit({
|
|
158
|
+
cwd: options.cwd,
|
|
159
|
+
force: options.force,
|
|
160
|
+
dryRun: options.dryRun,
|
|
161
|
+
hook: options.hook,
|
|
162
|
+
install: options.hook !== undefined && !options.noInstall,
|
|
163
|
+
});
|
|
164
|
+
report(result);
|
|
165
|
+
if (result.errors.length > 0) {
|
|
166
|
+
return 1;
|
|
167
|
+
}
|
|
168
|
+
if (result.dryRun) {
|
|
169
|
+
console.log('dry run: run again without --dry-run to initialise, then paste the prompt it prints');
|
|
170
|
+
return 0;
|
|
171
|
+
}
|
|
172
|
+
console.log(`\n${renderNextStep(selectNextStep(result.created.length, result.overwritten.length))}`);
|
|
173
|
+
return 0;
|
|
174
|
+
}
|
|
175
|
+
/** Every read-side command starts here: no workspace, no output, one message. */
|
|
176
|
+
function workspaceRoot() {
|
|
177
|
+
return findRoot(process.cwd());
|
|
178
|
+
}
|
|
179
|
+
async function commandBrief(args) {
|
|
180
|
+
const root = workspaceRoot();
|
|
181
|
+
if (root === null) {
|
|
182
|
+
console.error('no .sddio/context/config.json above this directory');
|
|
183
|
+
return 1;
|
|
184
|
+
}
|
|
185
|
+
const started = Date.now();
|
|
186
|
+
const { text, ids } = renderBrief(root, args);
|
|
187
|
+
process.stdout.write(text);
|
|
188
|
+
recordEvent(root, readVersion(), {
|
|
189
|
+
cmd: 'brief',
|
|
190
|
+
args: { paths: args },
|
|
191
|
+
out: measure(text, ids),
|
|
192
|
+
exit: 0,
|
|
193
|
+
ms: Date.now() - started,
|
|
194
|
+
});
|
|
195
|
+
return 0;
|
|
196
|
+
}
|
|
197
|
+
async function commandShow(args) {
|
|
198
|
+
const root = workspaceRoot();
|
|
199
|
+
if (root === null) {
|
|
200
|
+
console.error('no .sddio/context/config.json above this directory');
|
|
201
|
+
return 1;
|
|
202
|
+
}
|
|
203
|
+
const id = args[0];
|
|
204
|
+
if (id === undefined) {
|
|
205
|
+
throw new Error('show requires an entry ID, for example: sddio show ADR-012');
|
|
206
|
+
}
|
|
207
|
+
const entry = readEntries(root).find((candidate) => candidate.id.toLowerCase() === id.toLowerCase());
|
|
208
|
+
if (entry === undefined) {
|
|
209
|
+
console.error(`no entry with ID ${id}`);
|
|
210
|
+
return 1;
|
|
211
|
+
}
|
|
212
|
+
const started = Date.now();
|
|
213
|
+
const text = renderEntry(entry);
|
|
214
|
+
process.stdout.write(text);
|
|
215
|
+
recordEvent(root, readVersion(), {
|
|
216
|
+
cmd: 'show',
|
|
217
|
+
args: { id: entry.id },
|
|
218
|
+
out: measure(text, [entry.id]),
|
|
219
|
+
exit: 0,
|
|
220
|
+
ms: Date.now() - started,
|
|
221
|
+
});
|
|
222
|
+
return 0;
|
|
223
|
+
}
|
|
224
|
+
async function commandCheck(args) {
|
|
225
|
+
const root = workspaceRoot();
|
|
226
|
+
if (root === null) {
|
|
227
|
+
console.error('no .sddio/context/config.json above this directory');
|
|
228
|
+
return 1;
|
|
229
|
+
}
|
|
230
|
+
const started = Date.now();
|
|
231
|
+
const report = inspect(root);
|
|
232
|
+
if (args.includes('--claims')) {
|
|
233
|
+
const results = runClaims(root, readClaims(root));
|
|
234
|
+
const rendered = renderClaims(results);
|
|
235
|
+
process.stdout.write(rendered.text);
|
|
236
|
+
recordEvent(root, readVersion(), {
|
|
237
|
+
cmd: 'check --claims',
|
|
238
|
+
cursor: report.cursor.state,
|
|
239
|
+
out: measure(rendered.text, results.map((result) => result.id)),
|
|
240
|
+
exit: rendered.exit,
|
|
241
|
+
ms: Date.now() - started,
|
|
242
|
+
});
|
|
243
|
+
return rendered.exit;
|
|
244
|
+
}
|
|
245
|
+
if (args.includes('--json')) {
|
|
246
|
+
process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
|
|
247
|
+
return renderCheck(report).exit;
|
|
248
|
+
}
|
|
249
|
+
const { text, ids, exit } = renderCheck(report);
|
|
250
|
+
process.stdout.write(text);
|
|
251
|
+
recordEvent(root, readVersion(), {
|
|
252
|
+
cmd: 'check',
|
|
253
|
+
cursor: report.cursor.state,
|
|
254
|
+
out: measure(text, ids),
|
|
255
|
+
exit,
|
|
256
|
+
ms: Date.now() - started,
|
|
257
|
+
});
|
|
258
|
+
return exit;
|
|
259
|
+
}
|
|
260
|
+
async function commandDiff(args) {
|
|
261
|
+
const root = workspaceRoot();
|
|
262
|
+
if (root === null) {
|
|
263
|
+
console.error('no .sddio/context/config.json above this directory');
|
|
264
|
+
return 1;
|
|
265
|
+
}
|
|
266
|
+
const started = Date.now();
|
|
267
|
+
const report = inspect(root);
|
|
268
|
+
if (args.includes('--json')) {
|
|
269
|
+
process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
|
|
270
|
+
return 0;
|
|
271
|
+
}
|
|
272
|
+
const { text, ids } = renderDiff(report);
|
|
273
|
+
process.stdout.write(text);
|
|
274
|
+
recordEvent(root, readVersion(), {
|
|
275
|
+
cmd: 'diff',
|
|
276
|
+
cursor: report.cursor.state,
|
|
277
|
+
out: measure(text, ids),
|
|
278
|
+
exit: 0,
|
|
279
|
+
ms: Date.now() - started,
|
|
280
|
+
});
|
|
281
|
+
return 0;
|
|
282
|
+
}
|
|
283
|
+
async function commandStart(args) {
|
|
284
|
+
const root = workspaceRoot();
|
|
285
|
+
if (root === null) {
|
|
286
|
+
console.error('no .sddio/context/config.json above this directory');
|
|
287
|
+
return 1;
|
|
288
|
+
}
|
|
289
|
+
const started = Date.now();
|
|
290
|
+
const { text, ids } = renderBrief(root, args, { label: 'start', hint: true, budget: 10 });
|
|
291
|
+
process.stdout.write(text);
|
|
292
|
+
recordEvent(root, readVersion(), {
|
|
293
|
+
cmd: 'start',
|
|
294
|
+
args: { paths: args },
|
|
295
|
+
out: measure(text, ids),
|
|
296
|
+
exit: 0,
|
|
297
|
+
ms: Date.now() - started,
|
|
298
|
+
});
|
|
299
|
+
return 0;
|
|
300
|
+
}
|
|
301
|
+
async function main(args) {
|
|
302
|
+
if (args.includes('-h') || args.includes('--help')) {
|
|
303
|
+
console.log(USAGE);
|
|
304
|
+
return 0;
|
|
305
|
+
}
|
|
306
|
+
if (args.includes('-v') || args.includes('--version')) {
|
|
307
|
+
console.log(readVersion());
|
|
308
|
+
return 0;
|
|
309
|
+
}
|
|
310
|
+
const [command, ...rest] = args;
|
|
311
|
+
if (command === undefined) {
|
|
312
|
+
console.log(USAGE);
|
|
313
|
+
return 1;
|
|
314
|
+
}
|
|
315
|
+
try {
|
|
316
|
+
switch (command) {
|
|
317
|
+
case 'init':
|
|
318
|
+
return await commandInit(rest);
|
|
319
|
+
case 'brief':
|
|
320
|
+
return await commandBrief(rest);
|
|
321
|
+
case 'show':
|
|
322
|
+
return await commandShow(rest);
|
|
323
|
+
case 'check':
|
|
324
|
+
return await commandCheck(rest);
|
|
325
|
+
case 'diff':
|
|
326
|
+
return await commandDiff(rest);
|
|
327
|
+
case 'start':
|
|
328
|
+
return await commandStart(rest);
|
|
329
|
+
default:
|
|
330
|
+
console.error(`Unknown command: ${command}\n`);
|
|
331
|
+
console.log(USAGE);
|
|
332
|
+
return 1;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
catch (error) {
|
|
336
|
+
console.error(`error: ${error.message}`);
|
|
337
|
+
return 1;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
const exitCode = await main(process.argv.slice(2));
|
|
341
|
+
process.exitCode = exitCode;
|
package/dist/cursor.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The cursor, resolved against git (SPEC-010).
|
|
3
|
+
*
|
|
4
|
+
* One implementation for `brief`, `check`, `diff` and `start`, so four commands
|
|
5
|
+
* cannot disagree about whether the workspace is behind.
|
|
6
|
+
*/
|
|
7
|
+
import { execFileSync } from 'node:child_process';
|
|
8
|
+
function run(root, args) {
|
|
9
|
+
try {
|
|
10
|
+
return execFileSync('git', args, { cwd: root, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim();
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function ok(root, args) {
|
|
17
|
+
try {
|
|
18
|
+
execFileSync('git', args, { cwd: root, stdio: ['ignore', 'ignore', 'ignore'] });
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function exists(root, sha) {
|
|
26
|
+
return ok(root, ['cat-file', '-e', `${sha}^{commit}`]);
|
|
27
|
+
}
|
|
28
|
+
export function cursor(root, config) {
|
|
29
|
+
const last = config.reconcileCursor?.lastProcessedCommit ?? null;
|
|
30
|
+
const previous = config.reconcileCursor?.previousCommit ?? null;
|
|
31
|
+
if (run(root, ['rev-parse', '--git-dir']) === null || run(root, ['rev-parse', 'HEAD']) === null) {
|
|
32
|
+
return { state: 'nogit', behind: 0, last };
|
|
33
|
+
}
|
|
34
|
+
if (last === null) {
|
|
35
|
+
return { state: 'unknown', behind: 0, last };
|
|
36
|
+
}
|
|
37
|
+
if (!exists(root, last)) {
|
|
38
|
+
if (previous !== null && exists(root, previous) && ok(root, ['merge-base', '--is-ancestor', previous, 'HEAD'])) {
|
|
39
|
+
return { state: 'lost', behind: 0, last };
|
|
40
|
+
}
|
|
41
|
+
return { state: 'unknown', behind: 0, last };
|
|
42
|
+
}
|
|
43
|
+
if (!ok(root, ['merge-base', '--is-ancestor', last, 'HEAD'])) {
|
|
44
|
+
return { state: 'rewritten', behind: 0, last };
|
|
45
|
+
}
|
|
46
|
+
const behind = Number(run(root, ['rev-list', '--count', `${last}..HEAD`]) ?? '0');
|
|
47
|
+
return { state: behind === 0 ? 'current' : 'behind', behind, last };
|
|
48
|
+
}
|
|
49
|
+
/** The one clause a command prints about the cursor. */
|
|
50
|
+
export function describe(state) {
|
|
51
|
+
switch (state.state) {
|
|
52
|
+
case 'current':
|
|
53
|
+
return 'current';
|
|
54
|
+
case 'behind':
|
|
55
|
+
return `${state.behind} commit${state.behind === 1 ? '' : 's'} behind`;
|
|
56
|
+
case 'rewritten':
|
|
57
|
+
return 'history rewritten — reconcile both sides';
|
|
58
|
+
case 'lost':
|
|
59
|
+
return 'anchor lost — reconcile from previousCommit';
|
|
60
|
+
case 'unknown':
|
|
61
|
+
return 'no baseline';
|
|
62
|
+
case 'nogit':
|
|
63
|
+
return 'not a git repository';
|
|
64
|
+
}
|
|
65
|
+
}
|
package/dist/events.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local observation log (contract 12).
|
|
3
|
+
*
|
|
4
|
+
* One JSON object per command call, appended to `.sddio/evals/events.jsonl`.
|
|
5
|
+
* The file is gitignored, never leaves the machine, and nothing reads it to do
|
|
6
|
+
* work — deleting it costs history and nothing else.
|
|
7
|
+
*
|
|
8
|
+
* Recording must never change what a command does. Every failure in here is
|
|
9
|
+
* swallowed: an observation that can break a command is worse than a missing
|
|
10
|
+
* observation.
|
|
11
|
+
*/
|
|
12
|
+
import { appendFileSync, mkdirSync } from 'node:fs';
|
|
13
|
+
import { dirname, join } from 'node:path';
|
|
14
|
+
export function eventsPath(root) {
|
|
15
|
+
return join(root, '.sddio', 'evals', 'events.jsonl');
|
|
16
|
+
}
|
|
17
|
+
export function recordEvent(root, version, event) {
|
|
18
|
+
if (process.env.SDDIO_NO_EVENTS === '1') {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const path = eventsPath(root);
|
|
23
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
24
|
+
appendFileSync(path, `${JSON.stringify({ ts: new Date().toISOString(), v: version, ...event })}\n`, 'utf8');
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
// Silent by design; see the header.
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/** Measure an output before it is printed, so the log and the screen agree. */
|
|
31
|
+
export function measure(text, ids) {
|
|
32
|
+
return {
|
|
33
|
+
lines: text.split('\n').length - (text.endsWith('\n') ? 1 : 0),
|
|
34
|
+
bytes: Buffer.byteLength(text, 'utf8'),
|
|
35
|
+
ids,
|
|
36
|
+
};
|
|
37
|
+
}
|
package/dist/hooks.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook selection (SPEC-018).
|
|
3
|
+
*
|
|
4
|
+
* A hook decides when sddio is told to reconcile; sddio never calls one. `init`
|
|
5
|
+
* records the choice in `config.json` and, for `flow-trail`, runs the same
|
|
6
|
+
* bootstrap this repository uses — the integration is dogfooded or it is not
|
|
7
|
+
* tested at all.
|
|
8
|
+
*
|
|
9
|
+
* Only `flow-trail` is offered today. The list exists so a second hook is a row
|
|
10
|
+
* rather than a branch, and so the declared shape (`id`, `kind`, `events`)
|
|
11
|
+
* matches what is actually consumed.
|
|
12
|
+
*/
|
|
13
|
+
import { spawnSync } from 'node:child_process';
|
|
14
|
+
import { readFileSync } from 'node:fs';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
export const HOOKS = [
|
|
17
|
+
{
|
|
18
|
+
id: 'flow-trail',
|
|
19
|
+
kind: 'external',
|
|
20
|
+
events: ['knowledge.reconcile'],
|
|
21
|
+
summary: 'a board of folders in git; calls sddio when a card closes',
|
|
22
|
+
},
|
|
23
|
+
];
|
|
24
|
+
export const DEFAULT_HOOK = 'flow-trail';
|
|
25
|
+
/** The paste block flow-trail publishes; kept verbatim so both routes install the same thing. */
|
|
26
|
+
export const FLOW_TRAIL_BOOTSTRAP = 'bash -c "$(curl -fsSL https://raw.githubusercontent.com/benxgao/flow-trail/HEAD/scripts/bootstrap.sh)"';
|
|
27
|
+
export function hookEntry(id) {
|
|
28
|
+
const hook = HOOKS.find((candidate) => candidate.id === id);
|
|
29
|
+
if (hook === undefined) {
|
|
30
|
+
throw new Error(`Unknown hook: ${id}. Known hooks: ${HOOKS.map((h) => h.id).join(', ')}, none`);
|
|
31
|
+
}
|
|
32
|
+
return hook;
|
|
33
|
+
}
|
|
34
|
+
export function declarationsFor(id) {
|
|
35
|
+
if (id === 'none') {
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
const { id: hookId, kind, events } = hookEntry(id);
|
|
39
|
+
return [{ id: hookId, kind, events }];
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Has this project already answered? An empty array is an answer — "none" —
|
|
43
|
+
* so the presence of the key decides, not its length.
|
|
44
|
+
*/
|
|
45
|
+
export function hasDeclaredHooks(cwd) {
|
|
46
|
+
try {
|
|
47
|
+
const config = JSON.parse(readFileSync(join(cwd, '.sddio', 'context', 'config.json'), 'utf8'));
|
|
48
|
+
return Array.isArray(config.hooks);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Runs the hook's own installer in the target project. `stdio: inherit` is
|
|
56
|
+
* deliberate: the bootstrap prints every path it touches, and hiding that
|
|
57
|
+
* behind a spinner would leave the user guessing what landed in their repo.
|
|
58
|
+
*/
|
|
59
|
+
export function installFlowTrail(cwd) {
|
|
60
|
+
const result = spawnSync('bash', ['-c', FLOW_TRAIL_BOOTSTRAP], { cwd, stdio: 'inherit' });
|
|
61
|
+
if (result.error !== undefined) {
|
|
62
|
+
return { ok: false, message: result.error.message };
|
|
63
|
+
}
|
|
64
|
+
if (result.status !== 0) {
|
|
65
|
+
return { ok: false, message: `installer exited with ${String(result.status)}` };
|
|
66
|
+
}
|
|
67
|
+
return { ok: true, message: 'flow-trail installed' };
|
|
68
|
+
}
|
package/dist/init.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sddio init` — lay down `.sddio/` and `sddio_docs/` in a project.
|
|
3
|
+
*
|
|
4
|
+
* Ownership rules (ADR-001, SPEC-003):
|
|
5
|
+
* - `.sddio/` is package-owned: refreshed on every run, so upgrades carry the
|
|
6
|
+
* current context layer.
|
|
7
|
+
* - `sddio_docs/` is user-owned: created once, never overwritten unless
|
|
8
|
+
* `--force`.
|
|
9
|
+
*
|
|
10
|
+
* The two roots are produced differently on purpose (ADR-005). `.sddio/` is
|
|
11
|
+
* copied from the tarball. `sddio_docs/` is only a layout — directories
|
|
12
|
+
* plus empty seed files — so it is built from `lib/workspace.ts` rather than
|
|
13
|
+
* from template files, and empty directories survive because they are created
|
|
14
|
+
* explicitly rather than carried by the package.
|
|
15
|
+
*/
|
|
16
|
+
import { copyFile, mkdir, readdir, readFile, writeFile } from 'node:fs/promises';
|
|
17
|
+
import { existsSync } from 'node:fs';
|
|
18
|
+
import { dirname, join, resolve } from 'node:path';
|
|
19
|
+
import { ensureAgentsSnippet } from './agents.js';
|
|
20
|
+
import { declarationsFor, installFlowTrail } from './hooks.js';
|
|
21
|
+
import { contextTemplateRoot } from './paths.js';
|
|
22
|
+
import { WORKSPACE_DIRS, WORKSPACE_SEEDS } from './workspace.js';
|
|
23
|
+
function toSegments(relativePath) {
|
|
24
|
+
return relativePath.split('/');
|
|
25
|
+
}
|
|
26
|
+
async function listFiles(root, prefix = '') {
|
|
27
|
+
const entries = await readdir(join(root, ...toSegments(prefix)), { withFileTypes: true });
|
|
28
|
+
const files = [];
|
|
29
|
+
for (const entry of entries) {
|
|
30
|
+
const relativePath = prefix === '' ? entry.name : `${prefix}/${entry.name}`;
|
|
31
|
+
if (entry.isDirectory()) {
|
|
32
|
+
files.push(...(await listFiles(root, relativePath)));
|
|
33
|
+
}
|
|
34
|
+
else if (entry.isFile()) {
|
|
35
|
+
files.push(relativePath);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return files.sort();
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* `config.json` is the one package-owned file that carries a user choice: the
|
|
42
|
+
* hooks that call sddio. The template ships the cursor empty and the default
|
|
43
|
+
* hook list, but a refresh must keep the hooks the project already declared —
|
|
44
|
+
* otherwise an upgrade silently re-wires the project to a caller it abandoned.
|
|
45
|
+
*/
|
|
46
|
+
const CONFIG_FILE = 'context/config.json';
|
|
47
|
+
function mergeConfig(existing, incoming, chosen) {
|
|
48
|
+
const template = JSON.parse(incoming);
|
|
49
|
+
if (existing === null) {
|
|
50
|
+
return chosen === null
|
|
51
|
+
? incoming
|
|
52
|
+
: `${JSON.stringify({ ...template, hooks: chosen }, null, 2)}\n`;
|
|
53
|
+
}
|
|
54
|
+
let previous;
|
|
55
|
+
try {
|
|
56
|
+
previous = JSON.parse(existing);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
previous = {};
|
|
60
|
+
}
|
|
61
|
+
const declared = chosen ?? (Array.isArray(previous.hooks) ? previous.hooks : template.hooks);
|
|
62
|
+
return `${JSON.stringify({ ...template, hooks: declared }, null, 2)}\n`;
|
|
63
|
+
}
|
|
64
|
+
/** `.sddio/` — package-owned, copied as `templates/.sddio/<path>` → `.sddio/<path>`. */
|
|
65
|
+
async function copyContext(target, dryRun, chosen, result) {
|
|
66
|
+
for (const relativePath of await listFiles(contextTemplateRoot)) {
|
|
67
|
+
const source = join(contextTemplateRoot, ...toSegments(relativePath));
|
|
68
|
+
const destination = join(target, '.sddio', ...toSegments(relativePath));
|
|
69
|
+
const exists = existsSync(destination);
|
|
70
|
+
try {
|
|
71
|
+
if (relativePath === CONFIG_FILE) {
|
|
72
|
+
const current = exists ? await readFile(destination, 'utf8') : null;
|
|
73
|
+
const merged = mergeConfig(current, await readFile(source, 'utf8'), chosen);
|
|
74
|
+
if (current === merged) {
|
|
75
|
+
result.skipped.push(`.sddio/${relativePath}`);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (!dryRun) {
|
|
79
|
+
await mkdir(dirname(destination), { recursive: true });
|
|
80
|
+
await writeFile(destination, merged, 'utf8');
|
|
81
|
+
}
|
|
82
|
+
(exists ? result.overwritten : result.created).push(`.sddio/${relativePath}`);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (exists && (await readFile(destination)).equals(await readFile(source))) {
|
|
86
|
+
result.skipped.push(`.sddio/${relativePath}`);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (!dryRun) {
|
|
90
|
+
await mkdir(dirname(destination), { recursive: true });
|
|
91
|
+
await copyFile(source, destination);
|
|
92
|
+
}
|
|
93
|
+
(exists ? result.overwritten : result.created).push(`.sddio/${relativePath}`);
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
result.errors.push(`.sddio/${relativePath}: ${error.message}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** `sddio_docs/` — user-owned: directories plus empty seeds. */
|
|
101
|
+
async function seedWorkspace(target, options, result) {
|
|
102
|
+
const dryRun = options.dryRun === true;
|
|
103
|
+
for (const directory of WORKSPACE_DIRS) {
|
|
104
|
+
if (!dryRun) {
|
|
105
|
+
await mkdir(join(target, ...toSegments(directory)), { recursive: true });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
for (const relativePath of WORKSPACE_SEEDS) {
|
|
109
|
+
const destination = join(target, ...toSegments(relativePath));
|
|
110
|
+
const exists = existsSync(destination);
|
|
111
|
+
if (exists && options.force !== true) {
|
|
112
|
+
result.skipped.push(relativePath);
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
try {
|
|
116
|
+
if (!dryRun) {
|
|
117
|
+
await mkdir(dirname(destination), { recursive: true });
|
|
118
|
+
await writeFile(destination, '', 'utf8');
|
|
119
|
+
}
|
|
120
|
+
(exists ? result.overwritten : result.created).push(relativePath);
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
result.errors.push(`${relativePath}: ${error.message}`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
async function registerAgents(target, dryRun, result) {
|
|
128
|
+
const agentsPath = join(target, 'AGENTS.md');
|
|
129
|
+
const existing = existsSync(agentsPath) ? await readFile(agentsPath, 'utf8') : null;
|
|
130
|
+
const snippet = ensureAgentsSnippet(existing);
|
|
131
|
+
if (!snippet.changed) {
|
|
132
|
+
result.skipped.push('AGENTS.md');
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
if (!dryRun) {
|
|
137
|
+
await writeFile(agentsPath, snippet.content, 'utf8');
|
|
138
|
+
}
|
|
139
|
+
(existing === null ? result.created : result.overwritten).push('AGENTS.md');
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
result.errors.push(`AGENTS.md: ${error.message}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
export async function runInit(options) {
|
|
146
|
+
const target = resolve(options.cwd);
|
|
147
|
+
const dryRun = options.dryRun === true;
|
|
148
|
+
const chosen = options.hook === undefined ? null : declarationsFor(options.hook);
|
|
149
|
+
const result = {
|
|
150
|
+
target,
|
|
151
|
+
dryRun,
|
|
152
|
+
created: [],
|
|
153
|
+
skipped: [],
|
|
154
|
+
overwritten: [],
|
|
155
|
+
errors: [],
|
|
156
|
+
hook: options.hook ?? null,
|
|
157
|
+
install: null,
|
|
158
|
+
};
|
|
159
|
+
if (!existsSync(contextTemplateRoot)) {
|
|
160
|
+
throw new Error(`Templates are missing from the package: ${contextTemplateRoot}`);
|
|
161
|
+
}
|
|
162
|
+
await copyContext(target, dryRun, chosen, result);
|
|
163
|
+
await seedWorkspace(target, options, result);
|
|
164
|
+
await registerAgents(target, dryRun, result);
|
|
165
|
+
if (!dryRun && options.install === true && options.hook === 'flow-trail') {
|
|
166
|
+
result.install = installFlowTrail(target);
|
|
167
|
+
if (!result.install.ok) {
|
|
168
|
+
result.errors.push(`flow-trail: ${result.install.message}`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return result;
|
|
172
|
+
}
|