@llman-sdd/cli 0.9.0 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,13 +3,13 @@ import { join } from 'node:path';
3
3
  import { createInterface } from 'node:readline';
4
4
 
5
5
  import {
6
+ DEFAULT_LOCALE,
6
7
  addReq,
7
8
  addScenario,
8
9
  buildUnboundFeed,
9
10
  hasNativeRules,
10
11
  migrateNativeSource,
11
12
  nextReqId,
12
- renderMachine,
13
13
  resolveReq,
14
14
  scaffoldSpec,
15
15
  TRANSITIONS_INTROSPECTION,
@@ -19,12 +19,14 @@ import type { Command } from 'commander';
19
19
 
20
20
  import {
21
21
  addReportOutputOptions,
22
+ addSpecDirOption,
22
23
  CliError,
23
24
  loadCliConfig,
24
25
  loadRootSpecEntries,
25
- newIo,
26
- resolveOutMode,
26
+ emitReport,
27
+ resolveCommandRoot,
27
28
  resolveRootSpecsDir,
29
+ type CliContext,
28
30
  } from '../cli-shared.ts';
29
31
 
30
32
  /** Walk paths collecting .feature files (directories recurse). */
@@ -55,80 +57,77 @@ function confirmInteractive(question: string): Promise<boolean> {
55
57
  });
56
58
  }
57
59
 
58
- export function registerSpec(program: Command): void {
60
+ export function registerSpec(program: Command, ctx: CliContext): void {
59
61
  const spec = program.command('spec').description('Spec authoring helpers');
60
62
 
61
- spec
63
+ const skeleton = spec
62
64
  .command('skeleton')
63
65
  .description('Generate a single-track spec skeleton for a capability')
64
66
  .argument('<capability>')
65
- .option('--force', 'overwrite an existing spec file')
66
- .option(
67
- '--directory <path>',
68
- 'instance root resolution start (default: nearest llmanspec/ at or above cwd)',
69
- )
70
- .action((capability: string, options: { force?: boolean; directory?: string }) => {
71
- const locale = loadCliConfig()?.locale ?? 'en';
72
- const specsDir = resolveRootSpecsDir(options.directory);
73
- const path = join(specsDir, `${capability}.feature`);
74
- if (!options.force && existsSync(path)) {
75
- throw new CliError(`spec already exists: ${path} (use --force to overwrite)`);
76
- }
77
- const written = scaffoldSpec(newIo(), specsDir, capability, locale, {
78
- force: options.force,
79
- });
80
- console.log(`wrote ${written}`);
67
+ .option('--force', 'overwrite an existing spec file');
68
+ addSpecDirOption(skeleton);
69
+ skeleton.action((capability: string, options: { force?: boolean; directory?: string }) => {
70
+ // r109/r115: locale 属命中实例根的配置,`--directory` 指向子根时不得读启动根
71
+ const locale =
72
+ loadCliConfig(resolveCommandRoot(ctx, options.directory))?.locale ?? DEFAULT_LOCALE;
73
+ const specsDir = resolveRootSpecsDir(options.directory, ctx.io);
74
+ const path = join(specsDir, `${capability}.feature`);
75
+ if (!options.force && existsSync(path)) {
76
+ throw new CliError(`spec already exists: ${path} (use --force to overwrite)`);
77
+ }
78
+ const written = scaffoldSpec(ctx.io, specsDir, capability, locale, {
79
+ force: options.force,
81
80
  });
81
+ console.log(`wrote ${written}`);
82
+ });
82
83
 
83
- spec
84
+ const nextReq = spec
84
85
  .command('next-req-id')
85
- .description('Allocate the next global req id (max in use + 1, rN)')
86
- .option('--json', 'emit {reqId}')
87
- .option(
88
- '--directory <path>',
89
- 'instance root resolution start (default: nearest llmanspec/ at or above cwd)',
90
- )
91
- .action((options: { json?: boolean; directory?: string }) => {
92
- const reqId = nextReqId(newIo(), resolveRootSpecsDir(options.directory));
93
- if (options.json) console.log(JSON.stringify({ reqId }, null, 2));
94
- else console.log(reqId);
95
- });
86
+ .description('Allocate the next global req id (max in use + 1, rN)');
87
+ addReportOutputOptions(nextReq, { outputHint: 'json {reqId}; toon default' });
88
+ addSpecDirOption(nextReq);
89
+ nextReq.action(
90
+ (options: { json?: boolean; output?: string; compactJson?: boolean; directory?: string }) => {
91
+ const reqId = nextReqId(ctx.io, resolveRootSpecsDir(options.directory, ctx.io));
92
+ emitReport({ reqId }, options, () => reqId);
93
+ },
94
+ );
96
95
 
97
- spec
96
+ const addReqCmd = spec
98
97
  .command('add-req')
99
98
  .alias('add-requirement')
100
99
  .description('Append a 规则: block (with @req handle) to a capability spec')
101
100
  .argument('<capability>')
102
101
  .argument('<req_id>')
103
102
  .requiredOption('--title <title>', 'rule title')
104
- .requiredOption('--statement <statement>', 'rule statement (free text; multiple lines via \\n)')
105
- .option(
106
- '--directory <path>',
107
- 'instance root resolution start (default: nearest llmanspec/ at or above cwd)',
108
- )
109
- .action(
110
- (
111
- capability: string,
112
- reqId: string,
113
- options: { title: string; statement: string; directory?: string },
114
- ) => {
115
- const io = newIo();
116
- const path = addReq(
117
- io,
118
- resolveRootSpecsDir(options.directory),
119
- loadRootSpecEntries(options.directory),
120
- {
121
- capability,
122
- reqId,
123
- title: options.title,
124
- statement: options.statement,
125
- },
126
- );
127
- console.log(path);
128
- },
103
+ .requiredOption(
104
+ '--statement <statement>',
105
+ 'rule statement (free text; multiple lines via \\n)',
129
106
  );
107
+ addSpecDirOption(addReqCmd);
108
+ addReqCmd.action(
109
+ (
110
+ capability: string,
111
+ reqId: string,
112
+ options: { title: string; statement: string; directory?: string },
113
+ ) => {
114
+ const io = ctx.io;
115
+ const path = addReq(
116
+ io,
117
+ resolveRootSpecsDir(options.directory, ctx.io),
118
+ loadRootSpecEntries(options.directory, ctx.io),
119
+ {
120
+ capability,
121
+ reqId,
122
+ title: options.title,
123
+ statement: options.statement,
124
+ },
125
+ );
126
+ console.log(path);
127
+ },
128
+ );
130
129
 
131
- spec
130
+ const addScenarioCmd = spec
132
131
  .command('add-scenario')
133
132
  .description('Insert a nested 场景: under the named rule in a spec')
134
133
  .argument('<capability>')
@@ -136,45 +135,47 @@ export function registerSpec(program: Command): void {
136
135
  .argument('<scenario_id>')
137
136
  .option('--given <given>', 'Given step (optional)')
138
137
  .requiredOption('--when <when>', 'When step')
139
- .requiredOption('--then <then>', 'Then step')
140
- .option(
141
- '--directory <path>',
142
- 'instance root resolution start (default: nearest llmanspec/ at or above cwd)',
143
- )
144
- .action(
145
- (
146
- capability: string,
147
- reqId: string,
148
- scenarioId: string,
149
- options: { given?: string; when: string; then: string; directory?: string },
150
- ) => {
151
- const io = newIo();
152
- const path = addScenario(
153
- io,
154
- resolveRootSpecsDir(options.directory),
155
- loadRootSpecEntries(options.directory),
156
- {
157
- capability,
158
- reqId,
159
- scenarioId,
160
- given: options.given,
161
- when: options.when,
162
- thenText: options.then,
163
- },
164
- );
165
- console.log(path);
166
- },
167
- );
138
+ .requiredOption('--then <then>', 'Then step');
139
+ addSpecDirOption(addScenarioCmd);
140
+ addScenarioCmd.action(
141
+ (
142
+ capability: string,
143
+ reqId: string,
144
+ scenarioId: string,
145
+ options: { given?: string; when: string; then: string; directory?: string },
146
+ ) => {
147
+ const io = ctx.io;
148
+ const path = addScenario(
149
+ io,
150
+ resolveRootSpecsDir(options.directory, ctx.io),
151
+ loadRootSpecEntries(options.directory, ctx.io),
152
+ {
153
+ capability,
154
+ reqId,
155
+ scenarioId,
156
+ given: options.given,
157
+ when: options.when,
158
+ thenText: options.then,
159
+ },
160
+ );
161
+ console.log(path);
162
+ },
163
+ );
168
164
 
169
- spec
165
+ const migrateNative = spec
170
166
  .command('migrate-native')
171
167
  .description('Migrate legacy tag-based .feature files to the native 规则:/场景: layout')
172
168
  .argument('[paths...]', 'feature files or directories (default: llmanspec/specs)')
173
169
  .option('--dry-run', 'print the migration plan without writing')
174
- .option('-y, --yes', 'proceed without per-file confirmation')
175
- .action(async (paths: string[], options: { dryRun?: boolean; yes?: boolean }) => {
176
- const targets = collectFeatureFiles(paths.length > 0 ? paths : ['llmanspec/specs']);
177
- const io = newIo();
170
+ .option('-y, --yes', 'proceed without per-file confirmation');
171
+ addSpecDirOption(migrateNative);
172
+ migrateNative.action(
173
+ async (paths: string[], options: { dryRun?: boolean; yes?: boolean; directory?: string }) => {
174
+ // r116: 显式传入的路径按参数原义处理(豁免);缺省目标才经根发现算子
175
+ const targets = collectFeatureFiles(
176
+ paths.length > 0 ? paths : [resolveRootSpecsDir(options.directory, ctx.io)],
177
+ );
178
+ const io = ctx.io;
178
179
  let migrated = 0;
179
180
  let skipped = 0;
180
181
  for (const file of targets) {
@@ -205,70 +206,80 @@ export function registerSpec(program: Command): void {
205
206
  }
206
207
  }
207
208
  console.log(`migrate-native: ${migrated} file(s) migrated, ${skipped} already native`);
208
- });
209
+ },
210
+ );
209
211
 
210
- spec
212
+ const resolveReqCmd = spec
211
213
  .command('resolve-req')
212
214
  .description('Resolve an rN to its capability and statement')
213
- .argument('<req_id>')
214
- .option(
215
- '--directory <path>',
216
- 'instance root resolution start (default: nearest llmanspec/ at or above cwd)',
217
- )
218
- .action((reqId: string, options: { directory?: string }) => {
219
- const resolved = resolveReq(loadRootSpecEntries(options.directory), reqId);
220
- if (resolved === null) {
221
- throw new CliError(`req id not found: ${reqId}`);
222
- }
223
- console.log(`reqId: ${resolved.reqId}`);
224
- console.log(`capability: ${resolved.capability}`);
225
- console.log(`title: ${resolved.title}`);
226
- console.log(`statement: ${resolved.statement.replaceAll('\n', ' ')}`);
227
- console.log('harness:');
228
- for (const h of resolved.harness) console.log(` - ${h}`);
229
- });
215
+ .argument('<req_id>');
216
+ addSpecDirOption(resolveReqCmd);
217
+ resolveReqCmd.action((reqId: string, options: { directory?: string }) => {
218
+ const resolved = resolveReq(loadRootSpecEntries(options.directory, ctx.io), reqId);
219
+ if (resolved === null) {
220
+ throw new CliError(`req id not found: ${reqId}`);
221
+ }
222
+ console.log(`reqId: ${resolved.reqId}`);
223
+ console.log(`capability: ${resolved.capability}`);
224
+ console.log(`title: ${resolved.title}`);
225
+ console.log(`statement: ${resolved.statement.replaceAll('\n', ' ')}`);
226
+ console.log('harness:');
227
+ for (const h of resolved.harness) console.log(` - ${h}`);
228
+ });
230
229
 
231
- spec
230
+ const transitions = spec
232
231
  .command('transitions')
233
- .description('Show transition-table validation rules: alias families, tags, gates')
234
- .action(() => {
235
- const info = TRANSITIONS_INTROSPECTION;
236
- console.log('columns:');
237
- for (const fam of info.columnFamilies) {
238
- const optional = 'optional' in fam && fam.optional ? ' (optional)' : '';
239
- console.log(` ${fam.role}${optional}: ${fam.aliases.join(' | ')}`);
240
- }
241
- console.log(`discovery: ${info.discovery}`);
242
- console.log('tags:');
243
- for (const t of info.tags) console.log(` ${t.tag} — ${t.semantics}`);
244
- console.log('gates:');
245
- for (const g of info.gates) console.log(` - ${g}`);
246
- });
232
+ .description('Show transition-table validation rules: alias families, tags, gates');
233
+ addSpecDirOption(transitions);
234
+ transitions.action((options: { directory?: string }) => {
235
+ // r109: 本命令输出为静态内省,无实例根语义;仍按「豁免外全部命令声明并接受
236
+ // --directory」声明该选项,并仅在显式给出时经同一根发现算子校验(无实例报域错误)。
237
+ if (options.directory !== undefined) resolveCommandRoot(ctx, options.directory);
238
+ const info = TRANSITIONS_INTROSPECTION;
239
+ console.log('columns:');
240
+ for (const fam of info.columnFamilies) {
241
+ const optional = 'optional' in fam && fam.optional ? ' (optional)' : '';
242
+ console.log(` ${fam.role}${optional}: ${fam.aliases.join(' | ')}`);
243
+ }
244
+ console.log(`discovery: ${info.discovery}`);
245
+ console.log('tags:');
246
+ for (const t of info.tags) console.log(` ${t.tag} — ${t.semantics}`);
247
+ console.log('gates:');
248
+ for (const g of info.gates) console.log(` - ${g}`);
249
+ });
247
250
 
248
251
  const unbound = spec
249
252
  .command('unbound')
250
253
  .description('List unbound requirements (no runnable nested scenario) for implementation')
251
254
  .option('--limit <N>', 'max entries to return (default 1; 0 = all)');
252
255
  addReportOutputOptions(unbound);
256
+ addSpecDirOption(unbound);
253
257
  unbound.action(
254
- (options: { limit?: string; output?: string; json?: boolean; compactJson?: boolean }) => {
258
+ (options: {
259
+ limit?: string;
260
+ output?: string;
261
+ json?: boolean;
262
+ compactJson?: boolean;
263
+ directory?: string;
264
+ }) => {
255
265
  const raw = options.limit ?? '1';
256
266
  if (!/^\d+$/u.test(raw)) {
257
267
  throw new CliError(`invalid --limit: ${raw} (non-negative integer)`, 2);
258
268
  }
259
269
  const limit = Number(raw);
260
- const feed: UnboundFeed = buildUnboundFeed(loadRootSpecEntries(), limit);
261
- const mode = resolveOutMode(options.output, options.json, options.compactJson);
262
- if (mode !== 'human') {
263
- console.log(renderMachine(feed, mode));
264
- return;
265
- }
266
- console.log(`spec unbound: ${feed.returned} shown of ${feed.total} total`);
267
- for (const r of feed.requirements) {
268
- console.log(` - [${r.reqId}] ${r.featurePath} :: ${r.title}`);
269
- for (const line of r.statement.split('\n')) console.log(` ${line}`);
270
- }
271
- if (feed.hint !== '') console.log(feed.hint);
270
+ const feed: UnboundFeed = buildUnboundFeed(
271
+ loadRootSpecEntries(options.directory, ctx.io),
272
+ limit,
273
+ );
274
+ emitReport(feed, options, () => {
275
+ const lines = [`spec unbound: ${feed.returned} shown of ${feed.total} total`];
276
+ for (const r of feed.requirements) {
277
+ lines.push(` - [${r.reqId}] ${r.featurePath} :: ${r.title}`);
278
+ for (const line of r.statement.split('\n')) lines.push(` ${line}`);
279
+ }
280
+ if (feed.hint !== '') lines.push(feed.hint);
281
+ return lines.join('\n');
282
+ });
272
283
  },
273
284
  );
274
285
  }