@agentic-sdlc-kit/cli 1.0.1 → 1.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/dist/application/agent/gateway.d.ts +15 -1
- package/dist/application/agent/gateway.d.ts.map +1 -1
- package/dist/application/agent/gateway.js +60 -4
- package/dist/application/agent/gateway.js.map +1 -1
- package/dist/application/dispatcher.d.ts.map +1 -1
- package/dist/application/dispatcher.js +3 -0
- package/dist/application/dispatcher.js.map +1 -1
- package/dist/application/improvement/capture.d.ts +25 -0
- package/dist/application/improvement/capture.d.ts.map +1 -0
- package/dist/application/improvement/capture.js +45 -0
- package/dist/application/improvement/capture.js.map +1 -0
- package/dist/application/improvement/github-api.d.ts +38 -0
- package/dist/application/improvement/github-api.d.ts.map +1 -0
- package/dist/application/improvement/github-api.js +132 -0
- package/dist/application/improvement/github-api.js.map +1 -0
- package/dist/application/improvement/github-context.d.ts +19 -0
- package/dist/application/improvement/github-context.d.ts.map +1 -0
- package/dist/application/improvement/github-context.js +39 -0
- package/dist/application/improvement/github-context.js.map +1 -0
- package/dist/application/improvement/scrub-terms.d.ts +12 -0
- package/dist/application/improvement/scrub-terms.d.ts.map +1 -0
- package/dist/application/improvement/scrub-terms.js +66 -0
- package/dist/application/improvement/scrub-terms.js.map +1 -0
- package/dist/application/improvement/upstream.d.ts +18 -0
- package/dist/application/improvement/upstream.d.ts.map +1 -0
- package/dist/application/improvement/upstream.js +32 -0
- package/dist/application/improvement/upstream.js.map +1 -0
- package/dist/commands/improve-kit.d.ts +10 -0
- package/dist/commands/improve-kit.d.ts.map +1 -0
- package/dist/commands/improve-kit.js +79 -0
- package/dist/commands/improve-kit.js.map +1 -0
- package/dist/commands/improve-project.d.ts +18 -0
- package/dist/commands/improve-project.d.ts.map +1 -0
- package/dist/commands/improve-project.js +133 -0
- package/dist/commands/improve-project.js.map +1 -0
- package/dist/commands/improve-review.d.ts +12 -0
- package/dist/commands/improve-review.d.ts.map +1 -0
- package/dist/commands/improve-review.js +56 -0
- package/dist/commands/improve-review.js.map +1 -0
- package/dist/commands/improve.d.ts +12 -0
- package/dist/commands/improve.d.ts.map +1 -0
- package/dist/commands/improve.js +185 -0
- package/dist/commands/improve.js.map +1 -0
- package/dist/index.js +19 -2
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/application/agent/gateway.test.ts +80 -3
- package/src/application/agent/gateway.ts +61 -4
- package/src/application/dispatcher.ts +2 -0
- package/src/application/improvement/capture.test.ts +80 -0
- package/src/application/improvement/capture.ts +72 -0
- package/src/application/improvement/github-api.test.ts +191 -0
- package/src/application/improvement/github-api.ts +214 -0
- package/src/application/improvement/github-context.test.ts +55 -0
- package/src/application/improvement/github-context.ts +67 -0
- package/src/application/improvement/scrub-terms.ts +76 -0
- package/src/application/improvement/upstream.test.ts +70 -0
- package/src/application/improvement/upstream.ts +43 -0
- package/src/commands/evolve.test.ts +153 -0
- package/src/commands/improve-kit.test.ts +126 -0
- package/src/commands/improve-kit.ts +93 -0
- package/src/commands/improve-project.test.ts +194 -0
- package/src/commands/improve-project.ts +192 -0
- package/src/commands/improve-review.test.ts +126 -0
- package/src/commands/improve-review.ts +75 -0
- package/src/commands/improve.test.ts +121 -0
- package/src/commands/improve.ts +215 -0
- package/src/commands/journal.test.ts +150 -0
- package/src/commands/memory.test.ts +146 -0
- package/src/commands/optimize.test.ts +53 -0
- package/src/commands/refactor.test.ts +57 -0
- package/src/index.ts +18 -2
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// packages/cli/src/commands/improve.ts
|
|
2
|
+
//
|
|
3
|
+
// `kit improve` (ADR-0028). Two targets: `project` findings stay in the user's repository;
|
|
4
|
+
// `kit` feedback is sanitized, opt-in, and sent upstream only by the user.
|
|
5
|
+
//
|
|
6
|
+
// Output never contains a matched value. Exit codes: 0 ok, 1 findings with --fail-on-findings,
|
|
7
|
+
// 2 usage or runtime error.
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import { improvement } from '@agentic-sdlc-kit/core';
|
|
10
|
+
import type { Command, CommandContext, CommandResult } from './types.js';
|
|
11
|
+
import { defaultGh, type GhRunner } from '../application/improvement/upstream.js';
|
|
12
|
+
import type { GitHubApi, GitHubApiOptions } from '../application/improvement/github-api.js';
|
|
13
|
+
import { kitIssues, kitScan } from './improve-kit.js';
|
|
14
|
+
import { defaultGitHub, projectIssues, type ProjectIssueDeps } from './improve-project.js';
|
|
15
|
+
import { requestReview } from './improve-review.js';
|
|
16
|
+
|
|
17
|
+
const HELP = `Usage:
|
|
18
|
+
kit improve scan [--target project|kit] [options]
|
|
19
|
+
kit improve issues --target project [--sync] [--repo owner/name] [options]
|
|
20
|
+
kit improve issues --target kit [--key <key>] [--submit [--yes]]
|
|
21
|
+
kit improve review --pr <n> [--kind <kind>] [--instruction <text>] [--repo owner/name] [--yes]
|
|
22
|
+
|
|
23
|
+
Targets (ADR-0028):
|
|
24
|
+
project Findings about this repository. They stay here and are never sent upstream.
|
|
25
|
+
kit Sanitized feedback about the kit itself. Capture is opt-in (.ai-kit/improvement.yml);
|
|
26
|
+
only you can send it, one report at a time, after previewing it.
|
|
27
|
+
|
|
28
|
+
scan --target project:
|
|
29
|
+
--changed-only <ref> Scan only files changed since <ref> (a base ref or SHA).
|
|
30
|
+
--ignore-file <path> Ignore globs, one per line (default: .ai-kit/scan-ignore).
|
|
31
|
+
--ignore <glob> Extra ignore glob. Repeatable.
|
|
32
|
+
--format json|md Output format (default json). Output never contains matched values.
|
|
33
|
+
--fail-on-findings Exit 1 when any finding is reported.
|
|
34
|
+
|
|
35
|
+
issues --target project:
|
|
36
|
+
(no flags) Dry run: draft one issue per rule and file. No token, no GitHub call.
|
|
37
|
+
--sync Create or update those issues in this repository (GITHUB_TOKEN, GH_TOKEN
|
|
38
|
+
or your gh login). Closed issues are never reopened; fixed findings are
|
|
39
|
+
reported as stale, never closed. Bodies never contain matched values.
|
|
40
|
+
--repo owner/name Target repository (default: GITHUB_REPOSITORY, then the origin remote).
|
|
41
|
+
--format json|md Output format (default json). --ignore-file and --ignore work as for scan.
|
|
42
|
+
Exit 1 when the privacy gate withholds an issue.
|
|
43
|
+
|
|
44
|
+
review:
|
|
45
|
+
Asks this repository's ai-review.yml workflow (ci-agent) for one focused AI review. The kit has
|
|
46
|
+
no review engine of its own. The workflow's review gate still applies: an open, non-draft PR
|
|
47
|
+
from this repository, every other check green, and this kind not yet reviewed at the head.
|
|
48
|
+
--kind <kind> correctness (default), security, testing, architecture, performance,
|
|
49
|
+
maintainability, documentation, or custom.
|
|
50
|
+
--instruction <text> The focus for a custom review only (up to 500 characters).
|
|
51
|
+
--yes Send it. Without --yes, nothing is sent: AI reviews cost money.
|
|
52
|
+
|
|
53
|
+
issues --target kit:
|
|
54
|
+
(no flags) Preview every local report as the issue it would become.
|
|
55
|
+
--key <key> Limit to one report.
|
|
56
|
+
--submit --key <key> Show exactly what would be sent, and where.
|
|
57
|
+
--submit --yes Send it with your own gh login, or get a prefilled link if gh is unavailable.
|
|
58
|
+
`;
|
|
59
|
+
|
|
60
|
+
function single(argv: readonly string[], name: string): string | undefined {
|
|
61
|
+
const i = argv.indexOf(name);
|
|
62
|
+
const v = i >= 0 ? argv[i + 1] : undefined;
|
|
63
|
+
return v !== undefined && !v.startsWith('--') ? v : undefined;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function repeated(argv: readonly string[], name: string): string[] {
|
|
67
|
+
const out: string[] = [];
|
|
68
|
+
for (let i = 0; i < argv.length; i++) {
|
|
69
|
+
const v = argv[i + 1];
|
|
70
|
+
if (argv[i] === name && v !== undefined && !v.startsWith('--')) out.push(v);
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function fail(ctx: CommandContext, message: string, exitCode = 1): CommandResult {
|
|
76
|
+
ctx.stderr(`kit improve: ${message}\n`);
|
|
77
|
+
return { exitCode, message };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function scan(ctx: CommandContext, root: string): Promise<CommandResult> {
|
|
81
|
+
const format = single(ctx.argv, '--format') ?? 'json';
|
|
82
|
+
if (format !== 'json' && format !== 'md') return fail(ctx, `unknown --format ${format}`, 2);
|
|
83
|
+
|
|
84
|
+
const changedOnly = single(ctx.argv, '--changed-only');
|
|
85
|
+
const ignoreFile = single(ctx.argv, '--ignore-file');
|
|
86
|
+
try {
|
|
87
|
+
const report = await improvement.scanRepo({
|
|
88
|
+
root,
|
|
89
|
+
...(changedOnly !== undefined ? { changedOnly } : {}),
|
|
90
|
+
...(ignoreFile !== undefined ? { ignoreFile: path.resolve(ctx.cwd, ignoreFile) } : {}),
|
|
91
|
+
extraIgnores: repeated(ctx.argv, '--ignore'),
|
|
92
|
+
});
|
|
93
|
+
ctx.stdout(
|
|
94
|
+
format === 'md' ? improvement.toMarkdown(report) : `${JSON.stringify(report, null, 2)}\n`,
|
|
95
|
+
);
|
|
96
|
+
const failOn = ctx.argv.includes('--fail-on-findings');
|
|
97
|
+
return { exitCode: failOn && report.findings.length > 0 ? 1 : 0 };
|
|
98
|
+
} catch (err) {
|
|
99
|
+
// Report the error class and first line only. Never print file contents from here.
|
|
100
|
+
const e = err as NodeJS.ErrnoException;
|
|
101
|
+
const first = (e.message ?? '').split('\n')[0] ?? '';
|
|
102
|
+
return fail(ctx, `scan failed: ${e.code ?? e.name}: ${first}`, 2);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
type Target = 'project' | 'kit';
|
|
107
|
+
|
|
108
|
+
function targetOf(ctx: CommandContext, fallback?: Target): Target | 'invalid' | undefined {
|
|
109
|
+
const t = single(ctx.argv, '--target') ?? fallback;
|
|
110
|
+
if (t === undefined) return undefined;
|
|
111
|
+
return t === 'project' || t === 'kit' ? t : 'invalid';
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function runScan(ctx: CommandContext, root: string): Promise<CommandResult> {
|
|
115
|
+
const target = targetOf(ctx, 'project');
|
|
116
|
+
if (target === 'invalid') return fail(ctx, 'unknown --target (use project or kit)', 2);
|
|
117
|
+
if (target === 'project') return scan(ctx, root);
|
|
118
|
+
const format = single(ctx.argv, '--format') ?? 'json';
|
|
119
|
+
if (format !== 'json' && format !== 'md') return fail(ctx, `unknown --format ${format}`, 2);
|
|
120
|
+
return kitScan(ctx, root, format);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function runProjectIssues(
|
|
124
|
+
ctx: CommandContext,
|
|
125
|
+
root: string,
|
|
126
|
+
deps: ProjectIssueDeps,
|
|
127
|
+
): Promise<CommandResult> | CommandResult {
|
|
128
|
+
const format = single(ctx.argv, '--format') ?? 'json';
|
|
129
|
+
if (format !== 'json' && format !== 'md') return fail(ctx, `unknown --format ${format}`, 2);
|
|
130
|
+
const repo = single(ctx.argv, '--repo');
|
|
131
|
+
const ignoreFile = single(ctx.argv, '--ignore-file');
|
|
132
|
+
return projectIssues(
|
|
133
|
+
ctx,
|
|
134
|
+
root,
|
|
135
|
+
{
|
|
136
|
+
sync: ctx.argv.includes('--sync'),
|
|
137
|
+
format,
|
|
138
|
+
...(repo !== undefined ? { repo } : {}),
|
|
139
|
+
...(ignoreFile !== undefined ? { ignoreFile: path.resolve(ctx.cwd, ignoreFile) } : {}),
|
|
140
|
+
extraIgnores: repeated(ctx.argv, '--ignore'),
|
|
141
|
+
},
|
|
142
|
+
deps,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async function runIssues(
|
|
147
|
+
ctx: CommandContext,
|
|
148
|
+
root: string,
|
|
149
|
+
deps: ProjectIssueDeps,
|
|
150
|
+
): Promise<CommandResult> {
|
|
151
|
+
const target = targetOf(ctx);
|
|
152
|
+
if (target === undefined || target === 'invalid')
|
|
153
|
+
return fail(ctx, 'issues needs --target kit or --target project', 2);
|
|
154
|
+
if (target === 'project') return runProjectIssues(ctx, root, deps);
|
|
155
|
+
const key = single(ctx.argv, '--key');
|
|
156
|
+
const opts = {
|
|
157
|
+
...(key !== undefined ? { key } : {}),
|
|
158
|
+
submit: ctx.argv.includes('--submit'),
|
|
159
|
+
yes: ctx.argv.includes('--yes'),
|
|
160
|
+
};
|
|
161
|
+
return kitIssues(ctx, root, opts, deps.gh);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function runReview(ctx: CommandContext, deps: ProjectIssueDeps): Promise<CommandResult> {
|
|
165
|
+
const pick = (name: string): Record<string, string> => {
|
|
166
|
+
const v = single(ctx.argv, `--${name}`);
|
|
167
|
+
return v === undefined ? {} : { [name]: v };
|
|
168
|
+
};
|
|
169
|
+
return requestReview(
|
|
170
|
+
ctx,
|
|
171
|
+
{
|
|
172
|
+
...pick('pr'),
|
|
173
|
+
...pick('kind'),
|
|
174
|
+
...pick('instruction'),
|
|
175
|
+
...pick('repo'),
|
|
176
|
+
yes: ctx.argv.includes('--yes'),
|
|
177
|
+
},
|
|
178
|
+
deps,
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface ImproveDeps {
|
|
183
|
+
readonly gh?: GhRunner;
|
|
184
|
+
readonly env?: NodeJS.ProcessEnv;
|
|
185
|
+
readonly github?: (opts: GitHubApiOptions) => GitHubApi;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** `gh`, the environment and the GitHub client are injectable so tests never reach GitHub. */
|
|
189
|
+
export function createImproveCommand(deps: ImproveDeps = {}): Command {
|
|
190
|
+
const resolved: ProjectIssueDeps = {
|
|
191
|
+
gh: deps.gh ?? defaultGh,
|
|
192
|
+
env: deps.env ?? process.env,
|
|
193
|
+
github: deps.github ?? defaultGitHub,
|
|
194
|
+
};
|
|
195
|
+
return {
|
|
196
|
+
name: 'improve',
|
|
197
|
+
summary:
|
|
198
|
+
'Continuous improvement: project findings, and opt-in sanitized feedback about the kit (ADR-0028)',
|
|
199
|
+
async run(ctx: CommandContext): Promise<CommandResult> {
|
|
200
|
+
const sub = ctx.argv.find((a) => !a.startsWith('-'));
|
|
201
|
+
const help = ctx.argv.includes('--help') || ctx.argv.includes('-h');
|
|
202
|
+
if (help || sub === undefined) {
|
|
203
|
+
ctx.stdout(HELP);
|
|
204
|
+
return { exitCode: sub === undefined && !help ? 2 : 0 };
|
|
205
|
+
}
|
|
206
|
+
const root = path.resolve(ctx.cwd);
|
|
207
|
+
if (sub === 'scan') return runScan(ctx, root);
|
|
208
|
+
if (sub === 'issues') return runIssues(ctx, root, resolved);
|
|
209
|
+
if (sub === 'review') return runReview(ctx, resolved);
|
|
210
|
+
return fail(ctx, `unknown subcommand: ${sub}`, 2);
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export const improveCommand: Command = createImproveCommand();
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// packages/cli/src/commands/journal.test.ts
|
|
2
|
+
//
|
|
3
|
+
// CLI-layer tests for `kit journal` argument handling and output
|
|
4
|
+
// formatting (the chain-integration paths — read/verify/tamper on a
|
|
5
|
+
// driven feature run — are covered in next.test.ts). This file covers
|
|
6
|
+
// --help, the missing-identifier validation, empty journals, --limit
|
|
7
|
+
// truncation, and the --verify --json machine surface.
|
|
8
|
+
import { promises as fs } from 'node:fs';
|
|
9
|
+
import os from 'node:os';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { describe, expect, it } from 'vitest';
|
|
12
|
+
import { appendJournalEvent } from '@agentic-sdlc-kit/core';
|
|
13
|
+
import { journalCommand } from './journal.js';
|
|
14
|
+
import type { CommandContext } from './types.js';
|
|
15
|
+
|
|
16
|
+
const makeCtx = (
|
|
17
|
+
argv: string[],
|
|
18
|
+
cwd: string,
|
|
19
|
+
): CommandContext & { out: string[]; err: string[] } => {
|
|
20
|
+
const out: string[] = [];
|
|
21
|
+
const err: string[] = [];
|
|
22
|
+
return {
|
|
23
|
+
argv,
|
|
24
|
+
cwd,
|
|
25
|
+
stdout: (s) => void out.push(s),
|
|
26
|
+
stderr: (s) => void err.push(s),
|
|
27
|
+
out,
|
|
28
|
+
err,
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
async function tmpRoot(): Promise<string> {
|
|
33
|
+
return fs.mkdtemp(path.join(os.tmpdir(), 'kit-cli-journal-'));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function seedEvents(root: string, featureId: string, count: number): Promise<void> {
|
|
37
|
+
for (let i = 0; i < count; i++) {
|
|
38
|
+
const res = await appendJournalEvent(root, {
|
|
39
|
+
featureId,
|
|
40
|
+
kind: 'workflow.started',
|
|
41
|
+
actor: { kind: 'kit' },
|
|
42
|
+
payload: { attempt: i },
|
|
43
|
+
});
|
|
44
|
+
expect(res.ok).toBe(true);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
describe('kit journal (argument handling)', () => {
|
|
49
|
+
it('shows usage with --help and exits 0', async () => {
|
|
50
|
+
const ctx = makeCtx(['--help'], await tmpRoot());
|
|
51
|
+
const result = await journalCommand.run(ctx);
|
|
52
|
+
expect(result.exitCode).toBe(0);
|
|
53
|
+
expect(ctx.out.join('')).toContain('Usage: kit journal <feature-id>');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('fails (exit 1) when the feature identifier is missing', async () => {
|
|
57
|
+
const ctx = makeCtx([], await tmpRoot());
|
|
58
|
+
const result = await journalCommand.run(ctx);
|
|
59
|
+
expect(result.exitCode).toBe(1);
|
|
60
|
+
expect(ctx.err.join('')).toContain('missing required feature identifier');
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('kit journal (reading)', () => {
|
|
65
|
+
it('lists seeded events in human form', async () => {
|
|
66
|
+
const root = await tmpRoot();
|
|
67
|
+
await seedEvents(root, 'FEATURE-001', 2);
|
|
68
|
+
const ctx = makeCtx(['FEATURE-001'], root);
|
|
69
|
+
const result = await journalCommand.run(ctx);
|
|
70
|
+
expect(result.exitCode).toBe(0);
|
|
71
|
+
const text = ctx.out.join('');
|
|
72
|
+
expect(text).toContain('[0]');
|
|
73
|
+
expect(text).toContain('[1]');
|
|
74
|
+
expect(text).toContain('workflow.started');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('emits machine-readable events with --json', async () => {
|
|
78
|
+
const root = await tmpRoot();
|
|
79
|
+
await seedEvents(root, 'FEATURE-001', 2);
|
|
80
|
+
const ctx = makeCtx(['FEATURE-001', '--json'], root);
|
|
81
|
+
const result = await journalCommand.run(ctx);
|
|
82
|
+
expect(result.exitCode).toBe(0);
|
|
83
|
+
const events = JSON.parse(ctx.out.join('')) as Array<{ seq: number; kind: string }>;
|
|
84
|
+
expect(events).toHaveLength(2);
|
|
85
|
+
expect(events[0]?.seq).toBe(0);
|
|
86
|
+
expect(events[1]?.kind).toBe('workflow.started');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('hides earlier events with --limit N', async () => {
|
|
90
|
+
const root = await tmpRoot();
|
|
91
|
+
await seedEvents(root, 'FEATURE-001', 5);
|
|
92
|
+
const ctx = makeCtx(['FEATURE-001', '--limit', '2'], root);
|
|
93
|
+
const result = await journalCommand.run(ctx);
|
|
94
|
+
expect(result.exitCode).toBe(0);
|
|
95
|
+
const text = ctx.out.join('');
|
|
96
|
+
expect(text).toContain('3 earlier event(s) hidden');
|
|
97
|
+
expect(text).toContain('[4]');
|
|
98
|
+
expect(text).not.toContain('[0]');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('reports an empty journal honestly', async () => {
|
|
102
|
+
const root = await tmpRoot();
|
|
103
|
+
const file = path.join(root, '.ai-kit', 'journal', 'feature-runs', 'FEATURE-001.jsonl');
|
|
104
|
+
await fs.mkdir(path.dirname(file), { recursive: true });
|
|
105
|
+
await fs.writeFile(file, '', 'utf8');
|
|
106
|
+
const ctx = makeCtx(['FEATURE-001'], root);
|
|
107
|
+
const result = await journalCommand.run(ctx);
|
|
108
|
+
expect(result.exitCode).toBe(0);
|
|
109
|
+
expect(ctx.out.join('')).toContain('is empty');
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('fails (exit 1) when the journal does not exist', async () => {
|
|
113
|
+
const ctx = makeCtx(['FEATURE-404'], await tmpRoot());
|
|
114
|
+
const result = await journalCommand.run(ctx);
|
|
115
|
+
expect(result.exitCode).toBe(1);
|
|
116
|
+
expect(ctx.err.join('')).toContain('No journal found');
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe('kit journal (verification)', () => {
|
|
121
|
+
it('verifies a seeded chain (exit 0, human form)', async () => {
|
|
122
|
+
const root = await tmpRoot();
|
|
123
|
+
await seedEvents(root, 'FEATURE-001', 3);
|
|
124
|
+
const ctx = makeCtx(['FEATURE-001', '--verify'], root);
|
|
125
|
+
const result = await journalCommand.run(ctx);
|
|
126
|
+
expect(result.exitCode).toBe(0);
|
|
127
|
+
const text = ctx.out.join('');
|
|
128
|
+
expect(text).toContain('Journal verified for FEATURE-001: 3 event(s)');
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('emits the verification result as JSON with --verify --json', async () => {
|
|
132
|
+
const root = await tmpRoot();
|
|
133
|
+
await seedEvents(root, 'FEATURE-001', 2);
|
|
134
|
+
const ctx = makeCtx(['FEATURE-001', '--verify', '--json'], root);
|
|
135
|
+
const result = await journalCommand.run(ctx);
|
|
136
|
+
expect(result.exitCode).toBe(0);
|
|
137
|
+
const payload = JSON.parse(ctx.out.join('')) as {
|
|
138
|
+
featureId: string;
|
|
139
|
+
valid: boolean;
|
|
140
|
+
eventCount: number;
|
|
141
|
+
lastHash?: string;
|
|
142
|
+
};
|
|
143
|
+
expect(payload.featureId).toBe('FEATURE-001');
|
|
144
|
+
expect(payload.valid).toBe(true);
|
|
145
|
+
expect(payload.eventCount).toBe(2);
|
|
146
|
+
if (payload.eventCount > 0) {
|
|
147
|
+
expect(payload.lastHash).toMatch(/^[0-9a-f]{64}$/);
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
});
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// packages/cli/src/commands/memory.test.ts
|
|
2
|
+
//
|
|
3
|
+
// CLI-layer tests for `kit memory` (M29): honest no-memory reporting,
|
|
4
|
+
// query search, --list, --domain filtering, and the --json surface.
|
|
5
|
+
// The persisted memory is seeded through the real MemoryStore
|
|
6
|
+
// (saveToDisk), so the tests exercise the exact on-disk contract the
|
|
7
|
+
// command reads (.ai-kit/state/memory.json).
|
|
8
|
+
import { promises as fs } from 'node:fs';
|
|
9
|
+
import os from 'node:os';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { MemoryStore } from '@agentic-sdlc-kit/core';
|
|
12
|
+
import type { MemoryEntry } from '@agentic-sdlc-kit/contracts';
|
|
13
|
+
import { describe, expect, it } from 'vitest';
|
|
14
|
+
import { memoryCommand } from './memory.js';
|
|
15
|
+
import type { CommandContext } from './types.js';
|
|
16
|
+
|
|
17
|
+
const makeCtx = (
|
|
18
|
+
argv: string[],
|
|
19
|
+
cwd: string,
|
|
20
|
+
): CommandContext & { out: string[]; err: string[] } => {
|
|
21
|
+
const out: string[] = [];
|
|
22
|
+
const err: string[] = [];
|
|
23
|
+
return {
|
|
24
|
+
argv,
|
|
25
|
+
cwd,
|
|
26
|
+
stdout: (s) => void out.push(s),
|
|
27
|
+
stderr: (s) => void err.push(s),
|
|
28
|
+
out,
|
|
29
|
+
err,
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
function entry(overrides: Partial<MemoryEntry> = {}): MemoryEntry {
|
|
34
|
+
return {
|
|
35
|
+
id: 'MEM-001',
|
|
36
|
+
type: 'semantic',
|
|
37
|
+
domain: 'architecture',
|
|
38
|
+
summary: 'Keep contracts package dependency-free',
|
|
39
|
+
content: 'The contracts package must import nothing from sibling packages.',
|
|
40
|
+
confidence: 0.8,
|
|
41
|
+
provenance: ['FEATURE-001'],
|
|
42
|
+
appliedCount: 2,
|
|
43
|
+
successCount: 2,
|
|
44
|
+
createdAt: '2026-01-01T00:00:00.000Z',
|
|
45
|
+
updatedAt: '2026-01-01T00:00:00.000Z',
|
|
46
|
+
...overrides,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function seedMemory(root: string, entries: readonly MemoryEntry[]): Promise<void> {
|
|
51
|
+
const store = new MemoryStore();
|
|
52
|
+
for (const e of entries) store.put(e);
|
|
53
|
+
await store.saveToDisk(root);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function tmpRoot(): Promise<string> {
|
|
57
|
+
return fs.mkdtemp(path.join(os.tmpdir(), 'kit-cli-memory-'));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
describe('kit memory', () => {
|
|
61
|
+
it('shows usage with --help and exits 0', async () => {
|
|
62
|
+
const ctx = makeCtx(['--help'], await tmpRoot());
|
|
63
|
+
const result = await memoryCommand.run(ctx);
|
|
64
|
+
expect(result.exitCode).toBe(0);
|
|
65
|
+
expect(ctx.out.join('')).toContain('Usage: kit memory [query]');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('reports honestly when no memory has been accumulated yet', async () => {
|
|
69
|
+
const ctx = makeCtx(['architecture'], await tmpRoot());
|
|
70
|
+
const result = await memoryCommand.run(ctx);
|
|
71
|
+
expect(result.exitCode).toBe(0);
|
|
72
|
+
const text = ctx.out.join('');
|
|
73
|
+
expect(text).toContain('no memory entries found');
|
|
74
|
+
expect(text).toContain('kit evolve');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('reports the empty state as JSON with --json', async () => {
|
|
78
|
+
const ctx = makeCtx(['--json'], await tmpRoot());
|
|
79
|
+
const result = await memoryCommand.run(ctx);
|
|
80
|
+
expect(result.exitCode).toBe(0);
|
|
81
|
+
const payload = JSON.parse(ctx.out.join('')) as { entries: unknown[]; message: string };
|
|
82
|
+
expect(payload.entries).toEqual([]);
|
|
83
|
+
expect(payload.message).toContain('no memory entries found');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('finds memory entries by query text', async () => {
|
|
87
|
+
const root = await tmpRoot();
|
|
88
|
+
await seedMemory(root, [entry()]);
|
|
89
|
+
const ctx = makeCtx(['contracts'], root);
|
|
90
|
+
const result = await memoryCommand.run(ctx);
|
|
91
|
+
expect(result.exitCode).toBe(0);
|
|
92
|
+
const text = ctx.out.join('');
|
|
93
|
+
expect(text).toContain('Found 1 memory entries');
|
|
94
|
+
expect(text).toContain('MEM-001');
|
|
95
|
+
expect(text).toContain('[SEMANTIC]');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('says so when a query matches nothing', async () => {
|
|
99
|
+
const root = await tmpRoot();
|
|
100
|
+
await seedMemory(root, [entry()]);
|
|
101
|
+
const ctx = makeCtx(['kubernetes'], root);
|
|
102
|
+
const result = await memoryCommand.run(ctx);
|
|
103
|
+
expect(result.exitCode).toBe(0);
|
|
104
|
+
expect(ctx.out.join('')).toContain('No matching engineering memories found');
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('lists all entries with --list', async () => {
|
|
108
|
+
const root = await tmpRoot();
|
|
109
|
+
await seedMemory(root, [
|
|
110
|
+
entry(),
|
|
111
|
+
entry({ id: 'MEM-002', type: 'procedural', domain: 'tdd', summary: 'Red before Green' }),
|
|
112
|
+
]);
|
|
113
|
+
const ctx = makeCtx(['--list'], root);
|
|
114
|
+
const result = await memoryCommand.run(ctx);
|
|
115
|
+
expect(result.exitCode).toBe(0);
|
|
116
|
+
const text = ctx.out.join('');
|
|
117
|
+
expect(text).toContain('MEM-001');
|
|
118
|
+
expect(text).toContain('MEM-002');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('filters --list output by --domain', async () => {
|
|
122
|
+
const root = await tmpRoot();
|
|
123
|
+
await seedMemory(root, [
|
|
124
|
+
entry(),
|
|
125
|
+
entry({ id: 'MEM-002', domain: 'tdd', summary: 'Red before Green' }),
|
|
126
|
+
]);
|
|
127
|
+
const ctx = makeCtx(['--list', '--domain', 'tdd'], root);
|
|
128
|
+
const result = await memoryCommand.run(ctx);
|
|
129
|
+
expect(result.exitCode).toBe(0);
|
|
130
|
+
const text = ctx.out.join('');
|
|
131
|
+
expect(text).not.toContain('MEM-001');
|
|
132
|
+
expect(text).toContain('MEM-002');
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('emits machine-readable entries with --json', async () => {
|
|
136
|
+
const root = await tmpRoot();
|
|
137
|
+
await seedMemory(root, [entry()]);
|
|
138
|
+
const ctx = makeCtx(['--json', '--list'], root);
|
|
139
|
+
const result = await memoryCommand.run(ctx);
|
|
140
|
+
expect(result.exitCode).toBe(0);
|
|
141
|
+
const entries = JSON.parse(ctx.out.join('')) as Array<{ id: string; domain: string }>;
|
|
142
|
+
expect(entries).toHaveLength(1);
|
|
143
|
+
expect(entries[0]?.id).toBe('MEM-001');
|
|
144
|
+
expect(entries[0]?.domain).toBe('architecture');
|
|
145
|
+
});
|
|
146
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// packages/cli/src/commands/optimize.test.ts
|
|
2
|
+
//
|
|
3
|
+
// CLI-layer tests for `kit optimize`: the informational surface of the
|
|
4
|
+
// M9 optimization workflow — exit code, baseline invariant, and the
|
|
5
|
+
// guidance content the host workflow expects.
|
|
6
|
+
import { describe, expect, it } from 'vitest';
|
|
7
|
+
import { optimizeCommand } from './optimize.js';
|
|
8
|
+
import type { CommandContext } from './types.js';
|
|
9
|
+
|
|
10
|
+
const makeCtx = (argv: string[], cwd = '/'): CommandContext & { out: string[]; err: string[] } => {
|
|
11
|
+
const out: string[] = [];
|
|
12
|
+
const err: string[] = [];
|
|
13
|
+
return {
|
|
14
|
+
argv,
|
|
15
|
+
cwd,
|
|
16
|
+
stdout: (s) => void out.push(s),
|
|
17
|
+
stderr: (s) => void err.push(s),
|
|
18
|
+
out,
|
|
19
|
+
err,
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
describe('kit optimize', () => {
|
|
24
|
+
it('exits 0 and prints the workflow guidance', () => {
|
|
25
|
+
const ctx = makeCtx([]);
|
|
26
|
+
const result = optimizeCommand.run(ctx);
|
|
27
|
+
expect(result.exitCode).toBe(0);
|
|
28
|
+
const text = ctx.out.join('');
|
|
29
|
+
expect(text).toContain('kit optimize — Performance optimization workflow');
|
|
30
|
+
expect(text).toContain('Expected lifecycle:');
|
|
31
|
+
expect(text).toContain('baseline');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('states the baseline measurement invariant', () => {
|
|
35
|
+
const ctx = makeCtx([]);
|
|
36
|
+
optimizeCommand.run(ctx);
|
|
37
|
+
const text = ctx.out.join('');
|
|
38
|
+
expect(text).toContain('performance optimization requires baseline');
|
|
39
|
+
expect(text).toContain('without an established baseline');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('declares its release status honestly', () => {
|
|
43
|
+
const ctx = makeCtx([]);
|
|
44
|
+
optimizeCommand.run(ctx);
|
|
45
|
+
expect(ctx.out.join('')).toContain('Status: declared in 0.6.0');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('never writes to stderr', () => {
|
|
49
|
+
const ctx = makeCtx([]);
|
|
50
|
+
optimizeCommand.run(ctx);
|
|
51
|
+
expect(ctx.err.join('')).toBe('');
|
|
52
|
+
});
|
|
53
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// packages/cli/src/commands/refactor.test.ts
|
|
2
|
+
//
|
|
3
|
+
// CLI-layer tests for `kit refactor`: the informational surface of the
|
|
4
|
+
// M9 refactoring workflow — exit code, lifecycle description, and the
|
|
5
|
+
// behavioral-preservation constraints.
|
|
6
|
+
import { describe, expect, it } from 'vitest';
|
|
7
|
+
import { refactorCommand } from './refactor.js';
|
|
8
|
+
import type { CommandContext } from './types.js';
|
|
9
|
+
|
|
10
|
+
const makeCtx = (argv: string[], cwd = '/'): CommandContext & { out: string[]; err: string[] } => {
|
|
11
|
+
const out: string[] = [];
|
|
12
|
+
const err: string[] = [];
|
|
13
|
+
return {
|
|
14
|
+
argv,
|
|
15
|
+
cwd,
|
|
16
|
+
stdout: (s) => void out.push(s),
|
|
17
|
+
stderr: (s) => void err.push(s),
|
|
18
|
+
out,
|
|
19
|
+
err,
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
describe('kit refactor', () => {
|
|
24
|
+
it('exits 0 and prints the workflow guidance', () => {
|
|
25
|
+
const ctx = makeCtx([]);
|
|
26
|
+
const result = refactorCommand.run(ctx);
|
|
27
|
+
expect(result.exitCode).toBe(0);
|
|
28
|
+
const text = ctx.out.join('');
|
|
29
|
+
expect(text).toContain('kit refactor — Refactoring workflow');
|
|
30
|
+
expect(text).toContain('driven by your AI assistant');
|
|
31
|
+
expect(text).toContain('/kit:refactor <target module and goal>');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('lists the M9 lifecycle stages', () => {
|
|
35
|
+
const ctx = makeCtx([]);
|
|
36
|
+
refactorCommand.run(ctx);
|
|
37
|
+
const text = ctx.out.join('');
|
|
38
|
+
expect(text).toContain('characterization-tests');
|
|
39
|
+
expect(text).toContain('incremental-refactor');
|
|
40
|
+
expect(text).toContain('regression-testing');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('states the behavior-preservation constraints', () => {
|
|
44
|
+
const ctx = makeCtx([]);
|
|
45
|
+
refactorCommand.run(ctx);
|
|
46
|
+
const text = ctx.out.join('');
|
|
47
|
+
expect(text).toContain('Preserve observable behavior');
|
|
48
|
+
expect(text).toContain('Establish baseline tests before structural changes');
|
|
49
|
+
expect(text).toContain('Do not use a design pattern simply because it exists');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('never writes to stderr', () => {
|
|
53
|
+
const ctx = makeCtx([]);
|
|
54
|
+
refactorCommand.run(ctx);
|
|
55
|
+
expect(ctx.err.join('')).toBe('');
|
|
56
|
+
});
|
|
57
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -3,8 +3,13 @@
|
|
|
3
3
|
//
|
|
4
4
|
// `kit` executable entry point. Parses argv, dispatches to a command,
|
|
5
5
|
// prints output, and exits.
|
|
6
|
+
//
|
|
7
|
+
// After a failing run, opt-in kit feedback capture (ADR-0028) may record a sanitized record about
|
|
8
|
+
// the kit itself. Capture is off unless .ai-kit/improvement.yml enables it, sends nothing, and
|
|
9
|
+
// never changes the exit code.
|
|
6
10
|
|
|
7
11
|
import { createDispatcher } from './application/dispatcher.js';
|
|
12
|
+
import { afterDispatch, captureKitFailure } from './application/improvement/capture.js';
|
|
8
13
|
import type { CommandContext } from './commands/types.js';
|
|
9
14
|
|
|
10
15
|
async function main(): Promise<void> {
|
|
@@ -15,12 +20,23 @@ async function main(): Promise<void> {
|
|
|
15
20
|
stdout: (s: string) => process.stdout.write(s + '\n'),
|
|
16
21
|
stderr: (s: string) => process.stderr.write(s + '\n'),
|
|
17
22
|
};
|
|
18
|
-
const { dispatch } = createDispatcher();
|
|
19
|
-
const
|
|
23
|
+
const { dispatch, registry } = createDispatcher();
|
|
24
|
+
const isKnown = (name: string): boolean => registry.get(name) !== undefined;
|
|
25
|
+
let result;
|
|
26
|
+
try {
|
|
27
|
+
result = await dispatch(ctx);
|
|
28
|
+
} catch (e) {
|
|
29
|
+
const command = argv[0] ?? '';
|
|
30
|
+
if (isKnown(command)) {
|
|
31
|
+
await captureKitFailure({ cwd: ctx.cwd, argv, command, exitCode: 2, failure: 'internal-error', message: String(e) });
|
|
32
|
+
}
|
|
33
|
+
throw e;
|
|
34
|
+
}
|
|
20
35
|
if (result.message && result.command !== 'help' && result.command !== 'doctor') {
|
|
21
36
|
// help and doctor write their own output; everything else prints the message.
|
|
22
37
|
process.stdout.write(result.message + '\n');
|
|
23
38
|
}
|
|
39
|
+
await afterDispatch(ctx.cwd, argv, result, isKnown);
|
|
24
40
|
process.exit(result.exitCode);
|
|
25
41
|
}
|
|
26
42
|
|