@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,70 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { describe, expect, it } from 'vitest';
|
|
5
|
+
import { collectScrubTerms, remoteParts } from './scrub-terms.js';
|
|
6
|
+
import { createIssue, findExistingIssue, KIT_UPSTREAM_REPO, newIssueUrl, type GhRunner } from './upstream.js';
|
|
7
|
+
|
|
8
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
9
|
+
|
|
10
|
+
function recordingGh(responses: string[]): { gh: GhRunner; calls: string[][] } {
|
|
11
|
+
const calls: string[][] = [];
|
|
12
|
+
const gh: GhRunner = async (args) => {
|
|
13
|
+
calls.push(args);
|
|
14
|
+
return { code: 0, stdout: responses.shift() ?? '', stderr: '' };
|
|
15
|
+
};
|
|
16
|
+
return { gh, calls };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe('upstream transport', () => {
|
|
20
|
+
it('targets the repository named in the CLI package manifest', () => {
|
|
21
|
+
const pkg = JSON.parse(readFileSync(path.join(here, '..', '..', '..', 'package.json'), 'utf8')) as {
|
|
22
|
+
repository: { url: string };
|
|
23
|
+
};
|
|
24
|
+
expect(pkg.repository.url).toContain(`github.com/${KIT_UPSTREAM_REPO}`);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('searches upstream by report token and returns the first match', async () => {
|
|
28
|
+
const { gh, calls } = recordingGh(['https://github.com/o/r/issues/7\n']);
|
|
29
|
+
const r = await findExistingIssue(gh, 'kitfb0123456789abcdef');
|
|
30
|
+
expect(r.url).toBe('https://github.com/o/r/issues/7');
|
|
31
|
+
expect(calls[0]).toContain(`q=repo:${KIT_UPSTREAM_REPO} is:issue in:body kitfb0123456789abcdef`);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('reports no match when the search returns nothing', async () => {
|
|
35
|
+
const { gh } = recordingGh(['']);
|
|
36
|
+
expect((await findExistingIssue(gh, 'kitfb0')).url).toBeUndefined();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('creates the issue through the REST API with title and body as separate arguments', async () => {
|
|
40
|
+
const { gh, calls } = recordingGh(['https://github.com/o/r/issues/8']);
|
|
41
|
+
await createIssue(gh, 'T; rm -rf /', 'B $(x)');
|
|
42
|
+
expect(calls[0]).toEqual(expect.arrayContaining([`repos/${KIT_UPSTREAM_REPO}/issues`, 'title=T; rm -rf /', 'body=B $(x)']));
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('builds a prefilled new-issue link with encoded title and body', () => {
|
|
46
|
+
const url = newIssueUrl('a b', 'c&d');
|
|
47
|
+
expect(url).toBe(`https://github.com/${KIT_UPSTREAM_REPO}/issues/new?title=a%20b&body=c%26d`);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('scrub terms', () => {
|
|
52
|
+
it('extracts owner and repository from https and ssh remotes', () => {
|
|
53
|
+
expect(remoteParts('https://github.com/acme-corp/acme-payments.git')).toEqual([
|
|
54
|
+
'acme-corp',
|
|
55
|
+
'acme-payments',
|
|
56
|
+
'acme-corp/acme-payments',
|
|
57
|
+
]);
|
|
58
|
+
expect(remoteParts('git@github.com:acme-corp/acme-payments.git')).toContain('acme-payments');
|
|
59
|
+
expect(remoteParts('not a remote')).toEqual([]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('collects project, branch segments and author identity, and tolerates a missing git', async () => {
|
|
63
|
+
const terms = await collectScrubTerms('/work/acme-payments', async (_cwd, args) =>
|
|
64
|
+
args.join(' ') === 'rev-parse --abbrev-ref HEAD' ? 'feature/billing-v2' : undefined,
|
|
65
|
+
);
|
|
66
|
+
for (const t of ['/work/acme-payments', 'acme-payments', 'feature/billing-v2', 'feature', 'billing-v2']) {
|
|
67
|
+
expect(terms).toContain(t);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// packages/cli/src/application/improvement/upstream.ts
|
|
2
|
+
//
|
|
3
|
+
// Transport for upstream kit feedback (ADR-0028). Issues are filed with the user's own `gh`
|
|
4
|
+
// login, only when they run `kit improve issues --target kit --submit --yes`. Arguments go to gh
|
|
5
|
+
// as an array (no shell). When gh is unavailable the user gets a prefilled new-issue link instead.
|
|
6
|
+
import { execFile } from 'node:child_process';
|
|
7
|
+
|
|
8
|
+
/** The kit's own repository. A test keeps it in step with packages/cli/package.json. */
|
|
9
|
+
export const KIT_UPSTREAM_REPO = 'shubhamtaywade82/agentic-sdlc-kit';
|
|
10
|
+
|
|
11
|
+
export interface GhResult {
|
|
12
|
+
readonly code: number;
|
|
13
|
+
readonly stdout: string;
|
|
14
|
+
readonly stderr: string;
|
|
15
|
+
}
|
|
16
|
+
export type GhRunner = (args: string[]) => Promise<GhResult>;
|
|
17
|
+
|
|
18
|
+
/** Runs `gh` without a shell. A missing binary is code 127. */
|
|
19
|
+
export const defaultGh: GhRunner = (args) =>
|
|
20
|
+
new Promise((resolve) => {
|
|
21
|
+
execFile('gh', args, { timeout: 30_000 }, (err, stdout, stderr) => {
|
|
22
|
+
if (!err) return resolve({ code: 0, stdout, stderr });
|
|
23
|
+
const e = err as NodeJS.ErrnoException & { code?: number | string };
|
|
24
|
+
resolve({ code: e.code === 'ENOENT' ? 127 : typeof e.code === 'number' ? e.code : 1, stdout, stderr });
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
/** URL of an existing upstream issue carrying `token`, or undefined. */
|
|
29
|
+
export async function findExistingIssue(gh: GhRunner, token: string): Promise<GhResult & { url?: string }> {
|
|
30
|
+
const q = `repo:${KIT_UPSTREAM_REPO} is:issue in:body ${token}`;
|
|
31
|
+
const r = await gh(['api', '-X', 'GET', 'search/issues', '-f', `q=${q}`, '--jq', '.items[0].html_url // ""']);
|
|
32
|
+
const url = r.stdout.trim();
|
|
33
|
+
return url.length > 0 ? { ...r, url } : r;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function createIssue(gh: GhRunner, title: string, body: string): Promise<GhResult> {
|
|
37
|
+
return gh(['api', `repos/${KIT_UPSTREAM_REPO}/issues`, '-X', 'POST', '-f', `title=${title}`, '-f', `body=${body}`, '--jq', '.html_url']);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A prefilled new-issue link, for submitting in the browser when gh is not available. */
|
|
41
|
+
export function newIssueUrl(title: string, body: string): string {
|
|
42
|
+
return `https://github.com/${KIT_UPSTREAM_REPO}/issues/new?title=${encodeURIComponent(title)}&body=${encodeURIComponent(body)}`;
|
|
43
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// packages/cli/src/commands/evolve.test.ts
|
|
2
|
+
//
|
|
3
|
+
// CLI-layer tests for `kit evolve` (M31-M33): honest no-experience
|
|
4
|
+
// reporting (exit 1), the full no-benchmark pipeline over seeded
|
|
5
|
+
// experiences (distill → hypothesize → not-run verdicts → withheld
|
|
6
|
+
// metrics), --dry-run, and the --json surface.
|
|
7
|
+
//
|
|
8
|
+
// Seeded experiences use the real on-disk contract the command reads
|
|
9
|
+
// (.ai-kit/state/experiences.json); recurring failures on one stack
|
|
10
|
+
// produce one distilled lesson and one hypothesis whose Evolution Lab
|
|
11
|
+
// benchmark reports notImplemented — surfaced as verdict 'not-run',
|
|
12
|
+
// never as completed work (ADR-0018).
|
|
13
|
+
import { promises as fs } from 'node:fs';
|
|
14
|
+
import os from 'node:os';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
import type { ExperienceRecord } from '@agentic-sdlc-kit/contracts';
|
|
17
|
+
import { describe, expect, it } from 'vitest';
|
|
18
|
+
import { evolveCommand } from './evolve.js';
|
|
19
|
+
import type { CommandContext } from './types.js';
|
|
20
|
+
|
|
21
|
+
const makeCtx = (
|
|
22
|
+
argv: string[],
|
|
23
|
+
cwd: string,
|
|
24
|
+
): CommandContext & { out: string[]; err: string[] } => {
|
|
25
|
+
const out: string[] = [];
|
|
26
|
+
const err: string[] = [];
|
|
27
|
+
return {
|
|
28
|
+
argv,
|
|
29
|
+
cwd,
|
|
30
|
+
stdout: (s) => void out.push(s),
|
|
31
|
+
stderr: (s) => void err.push(s),
|
|
32
|
+
out,
|
|
33
|
+
err,
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
function failureRecord(id: string): ExperienceRecord {
|
|
38
|
+
return {
|
|
39
|
+
id,
|
|
40
|
+
workflowId: 'feature',
|
|
41
|
+
featureId: 'FEATURE-001',
|
|
42
|
+
stack: 'node-typescript',
|
|
43
|
+
outcome: 'failure',
|
|
44
|
+
phases: [
|
|
45
|
+
{ name: 'specification', status: 'passed', durationMs: 100 },
|
|
46
|
+
{ name: 'implementation', status: 'failed', durationMs: 500 },
|
|
47
|
+
{ name: 'verification', status: 'passed', durationMs: 200 },
|
|
48
|
+
],
|
|
49
|
+
failureSummary: 'TASK-001: AssertionError: expected marker to exist',
|
|
50
|
+
timestamp: '2026-01-01T00:00:00.000Z',
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function seedExperiences(root: string, records: readonly ExperienceRecord[]): Promise<void> {
|
|
55
|
+
const target = path.join(root, '.ai-kit', 'state', 'experiences.json');
|
|
56
|
+
await fs.mkdir(path.dirname(target), { recursive: true });
|
|
57
|
+
await fs.writeFile(target, JSON.stringify({ schemaVersion: 1, records }, null, 2) + '\n', 'utf8');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function tmpRoot(): Promise<string> {
|
|
61
|
+
return fs.mkdtemp(path.join(os.tmpdir(), 'kit-cli-evolve-'));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
describe('kit evolve', () => {
|
|
65
|
+
it('shows usage with --help and exits 0', async () => {
|
|
66
|
+
const ctx = makeCtx(['--help'], await tmpRoot());
|
|
67
|
+
const result = await evolveCommand.run(ctx);
|
|
68
|
+
expect(result.exitCode).toBe(0);
|
|
69
|
+
expect(ctx.out.join('')).toContain('Usage: kit evolve [--dry-run] [--json]');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('fails honestly (exit 1) when no experiences have been accumulated', async () => {
|
|
73
|
+
const ctx = makeCtx([], await tmpRoot());
|
|
74
|
+
const result = await evolveCommand.run(ctx);
|
|
75
|
+
expect(result.exitCode).toBe(1);
|
|
76
|
+
expect(result.message).toBe('no experiences to distill');
|
|
77
|
+
const text = ctx.out.join('');
|
|
78
|
+
expect(text).toContain('no experience records found');
|
|
79
|
+
expect(text).toContain('kit feature');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('reports the no-experience state as JSON with --json', async () => {
|
|
83
|
+
const ctx = makeCtx(['--json'], await tmpRoot());
|
|
84
|
+
const result = await evolveCommand.run(ctx);
|
|
85
|
+
expect(result.exitCode).toBe(1);
|
|
86
|
+
const payload = JSON.parse(ctx.out.join('')) as { error: string };
|
|
87
|
+
expect(payload.error).toBe('no-experiences');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('distills lessons from recurring failures and reports the lab honestly as NOT RUN', async () => {
|
|
91
|
+
const root = await tmpRoot();
|
|
92
|
+
// Three independent failures on the same stack and phase → one
|
|
93
|
+
// distilled lesson with confidence 0.8 (>= 0.7) → one hypothesis.
|
|
94
|
+
await seedExperiences(root, [
|
|
95
|
+
failureRecord('EXP-001'),
|
|
96
|
+
failureRecord('EXP-002'),
|
|
97
|
+
failureRecord('EXP-003'),
|
|
98
|
+
]);
|
|
99
|
+
const ctx = makeCtx([], root);
|
|
100
|
+
const result = await evolveCommand.run(ctx);
|
|
101
|
+
expect(result.exitCode).toBe(0);
|
|
102
|
+
const text = ctx.out.join('');
|
|
103
|
+
expect(text).toContain('Experiences Loaded: 3');
|
|
104
|
+
expect(text).toContain('Distilled Lessons: 1');
|
|
105
|
+
expect(text).toContain('Hypotheses: 1');
|
|
106
|
+
expect(text).toContain('Evolution Lab: NOT RUN (benchmark environment unavailable)');
|
|
107
|
+
expect(text).toContain('Metrics: withheld (no executed benchmarks)');
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('emits the machine-readable report with --json', async () => {
|
|
111
|
+
const root = await tmpRoot();
|
|
112
|
+
await seedExperiences(root, [
|
|
113
|
+
failureRecord('EXP-001'),
|
|
114
|
+
failureRecord('EXP-002'),
|
|
115
|
+
failureRecord('EXP-003'),
|
|
116
|
+
]);
|
|
117
|
+
const ctx = makeCtx(['--json', '--dry-run'], root);
|
|
118
|
+
const result = await evolveCommand.run(ctx);
|
|
119
|
+
expect(result.exitCode).toBe(0);
|
|
120
|
+
const payload = JSON.parse(ctx.out.join('')) as {
|
|
121
|
+
experiencesLoaded: number;
|
|
122
|
+
distilledLessons: number;
|
|
123
|
+
hypothesesGenerated: number;
|
|
124
|
+
benchmarksExecuted: number;
|
|
125
|
+
benchmarksNotRun: number;
|
|
126
|
+
metrics: null | Record<string, unknown>;
|
|
127
|
+
};
|
|
128
|
+
expect(payload.experiencesLoaded).toBe(3);
|
|
129
|
+
expect(payload.distilledLessons).toBe(1);
|
|
130
|
+
expect(payload.hypothesesGenerated).toBe(1);
|
|
131
|
+
expect(payload.benchmarksExecuted).toBe(0);
|
|
132
|
+
expect(payload.benchmarksNotRun).toBe(1);
|
|
133
|
+
expect(payload.metrics).toBeNull();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('persists distilled lessons to .ai-kit/state/memory.json', async () => {
|
|
137
|
+
const root = await tmpRoot();
|
|
138
|
+
await seedExperiences(root, [
|
|
139
|
+
failureRecord('EXP-001'),
|
|
140
|
+
failureRecord('EXP-002'),
|
|
141
|
+
failureRecord('EXP-003'),
|
|
142
|
+
]);
|
|
143
|
+
const result = await evolveCommand.run(makeCtx(['--dry-run'], root));
|
|
144
|
+
expect(result.exitCode).toBe(0);
|
|
145
|
+
const memoryFile = path.join(root, '.ai-kit', 'state', 'memory.json');
|
|
146
|
+
const stored = JSON.parse(await fs.readFile(memoryFile, 'utf8')) as {
|
|
147
|
+
entries: Array<{ id: string; type: string; domain: string }>;
|
|
148
|
+
};
|
|
149
|
+
expect(stored.entries).toHaveLength(1);
|
|
150
|
+
expect(stored.entries[0]?.type).toBe('semantic');
|
|
151
|
+
expect(stored.entries[0]?.domain).toBe('node-typescript');
|
|
152
|
+
});
|
|
153
|
+
});
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
5
|
+
import { improvement } from '@agentic-sdlc-kit/core';
|
|
6
|
+
import type { KitFeedback } from '@agentic-sdlc-kit/contracts';
|
|
7
|
+
import type { GhRunner } from '../application/improvement/upstream.js';
|
|
8
|
+
import { createImproveCommand } from './improve.js';
|
|
9
|
+
|
|
10
|
+
let dir: string;
|
|
11
|
+
let calls: string[][];
|
|
12
|
+
|
|
13
|
+
const record: KitFeedback = {
|
|
14
|
+
schemaVersion: 1,
|
|
15
|
+
key: '0a1b2c3d4e5f6789',
|
|
16
|
+
kitVersion: '0.10.2',
|
|
17
|
+
command: 'verify',
|
|
18
|
+
failure: 'runtime-error',
|
|
19
|
+
exitCode: 2,
|
|
20
|
+
errorCode: 'ENOENT',
|
|
21
|
+
errorText: 'ENOENT: no such file, open <string>',
|
|
22
|
+
os: 'linux',
|
|
23
|
+
nodeMajor: 22,
|
|
24
|
+
occurrences: 1,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function gh(responses: Array<{ code: number; stdout?: string }>): GhRunner {
|
|
28
|
+
return async (args) => {
|
|
29
|
+
calls.push(args);
|
|
30
|
+
const r = responses.shift() ?? { code: 0 };
|
|
31
|
+
return { code: r.code, stdout: r.stdout ?? '', stderr: '' };
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function invoke(argv: string[], runner: GhRunner = gh([])) {
|
|
36
|
+
let out = '';
|
|
37
|
+
let err = '';
|
|
38
|
+
const ctx = { argv, cwd: dir, stdout: (s: string) => (out += s), stderr: (s: string) => (err += s) };
|
|
39
|
+
const r = await createImproveCommand({ gh: runner }).run(ctx);
|
|
40
|
+
return { exitCode: r.exitCode, out, err };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
beforeEach(async () => {
|
|
44
|
+
dir = await mkdtemp(path.join(os.tmpdir(), 'kit-improve-kit-'));
|
|
45
|
+
calls = [];
|
|
46
|
+
await improvement.recordKitFeedback(dir, record, {});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
afterEach(async () => {
|
|
50
|
+
await rm(dir, { recursive: true, force: true });
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const SUBMIT = ['issues', '--target', 'kit', '--key', record.key, '--submit'];
|
|
54
|
+
|
|
55
|
+
describe('kit improve scan --target kit', () => {
|
|
56
|
+
it('lists local records as JSON', async () => {
|
|
57
|
+
const r = await invoke(['scan', '--target', 'kit']);
|
|
58
|
+
expect(JSON.parse(r.out)).toEqual([record]);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('rejects an unknown target', async () => {
|
|
62
|
+
expect((await invoke(['scan', '--target', 'cloud'])).exitCode).toBe(2);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('kit improve issues --target kit', () => {
|
|
67
|
+
it('previews every record and never calls gh', async () => {
|
|
68
|
+
const r = await invoke(['issues', '--target', 'kit']);
|
|
69
|
+
expect(r.exitCode).toBe(0);
|
|
70
|
+
expect(r.out).toContain('nothing has been sent');
|
|
71
|
+
expect(r.out).toContain('[kit-feedback] kit verify fails (ENOENT)');
|
|
72
|
+
expect(calls).toEqual([]);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('--submit without --key is refused', async () => {
|
|
76
|
+
expect((await invoke(['issues', '--target', 'kit', '--submit', '--yes'])).exitCode).toBe(2);
|
|
77
|
+
expect(calls).toEqual([]);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('--submit shows what would be sent and where, and waits for --yes', async () => {
|
|
81
|
+
const r = await invoke(SUBMIT);
|
|
82
|
+
expect(r.out).toContain('under your GitHub account');
|
|
83
|
+
expect(r.out).toContain('--yes');
|
|
84
|
+
expect(calls).toEqual([]);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('--submit --yes searches for a duplicate, then files the issue', async () => {
|
|
88
|
+
const r = await invoke([...SUBMIT, '--yes'], gh([{ code: 0, stdout: '' }, { code: 0, stdout: 'https://github.com/o/r/issues/9' }]));
|
|
89
|
+
expect(r.out).toContain('Filed: https://github.com/o/r/issues/9');
|
|
90
|
+
expect(calls).toHaveLength(2);
|
|
91
|
+
expect(calls[1]?.join(' ')).toContain(improvement.reportKeyToken(record.key));
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('links an existing report instead of filing a duplicate', async () => {
|
|
95
|
+
const r = await invoke([...SUBMIT, '--yes'], gh([{ code: 0, stdout: 'https://github.com/o/r/issues/3' }]));
|
|
96
|
+
expect(r.out).toContain('Already reported: https://github.com/o/r/issues/3');
|
|
97
|
+
expect(calls).toHaveLength(1);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('offers a prefilled link when gh is not installed, and sends nothing', async () => {
|
|
101
|
+
const r = await invoke([...SUBMIT, '--yes'], gh([{ code: 127 }]));
|
|
102
|
+
expect(r.exitCode).toBe(0);
|
|
103
|
+
expect(r.out).toContain('gh is not installed');
|
|
104
|
+
expect(r.out).toContain('/issues/new?title=');
|
|
105
|
+
expect(calls).toHaveLength(1);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('a record tampered on disk with PII is blocked by the egress check before gh is called', async () => {
|
|
109
|
+
const feedbackDir = improvement.kitFeedbackDir(dir, {});
|
|
110
|
+
const aadhaar = '23456789012' + improvement.verhoeffCheckDigit('23456789012');
|
|
111
|
+
const tampered = { ...record, key: 'fedcba9876543210', errorText: `id ${aadhaar}` };
|
|
112
|
+
await mkdir(feedbackDir, { recursive: true });
|
|
113
|
+
await writeFile(path.join(feedbackDir, `${tampered.key}.json`), JSON.stringify(tampered));
|
|
114
|
+
const r = await invoke(['issues', '--target', 'kit', '--key', tampered.key, '--submit', '--yes']);
|
|
115
|
+
expect(r.exitCode).toBe(2);
|
|
116
|
+
expect(r.err).toContain('blocked: outbound text matched aadhaar');
|
|
117
|
+
expect(calls).toEqual([]);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('issues needs a target, and a project scan outside a git repository fails cleanly', async () => {
|
|
121
|
+
expect((await invoke(['issues'])).exitCode).toBe(2);
|
|
122
|
+
const project = await invoke(['issues', '--target', 'project']);
|
|
123
|
+
expect(project.exitCode).toBe(2);
|
|
124
|
+
expect(project.err).toContain('scan failed');
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// packages/cli/src/commands/improve-kit.ts
|
|
2
|
+
//
|
|
3
|
+
// The "kit" target of `kit improve` (ADR-0028): feedback about the kit itself, never about the
|
|
4
|
+
// user's project. `scan` lists the local sanitized records. `issues` previews them as generic
|
|
5
|
+
// issues and, only with --key <key> --submit --yes, files one upstream under the user's own GitHub
|
|
6
|
+
// login, after the egress check and a duplicate search.
|
|
7
|
+
import { improvement } from '@agentic-sdlc-kit/core';
|
|
8
|
+
import type { KitFeedback } from '@agentic-sdlc-kit/contracts';
|
|
9
|
+
import {
|
|
10
|
+
createIssue,
|
|
11
|
+
findExistingIssue,
|
|
12
|
+
KIT_UPSTREAM_REPO,
|
|
13
|
+
newIssueUrl,
|
|
14
|
+
type GhRunner,
|
|
15
|
+
} from '../application/improvement/upstream.js';
|
|
16
|
+
import type { CommandContext, CommandResult } from './types.js';
|
|
17
|
+
|
|
18
|
+
export interface KitIssueOptions {
|
|
19
|
+
readonly key?: string;
|
|
20
|
+
readonly submit: boolean;
|
|
21
|
+
readonly yes: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type Draft = ReturnType<typeof improvement.renderKitIssue>;
|
|
25
|
+
|
|
26
|
+
function fail(ctx: CommandContext, message: string, exitCode: number): CommandResult {
|
|
27
|
+
ctx.stderr(`kit improve: ${message}\n`);
|
|
28
|
+
return { exitCode, message };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const NOTHING = 'Nothing recorded. Capture is opt-in: set kitFeedback.capture: true in .ai-kit/improvement.yml.\n';
|
|
32
|
+
|
|
33
|
+
function renderList(records: ReadonlyArray<KitFeedback>): string {
|
|
34
|
+
if (records.length === 0) return `## Kit feedback (local, not sent)\n\n${NOTHING}`;
|
|
35
|
+
const rows = records.map((r) => `| \`${r.key}\` | ${r.command} | ${r.errorCode ?? r.failure} | ${r.occurrences} |`);
|
|
36
|
+
return ['## Kit feedback (local, not sent)', '', '| Key | Command | Error | Seen |', '| --- | --- | --- | --- |', ...rows, ''].join('\n');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function kitScan(ctx: CommandContext, root: string, format: 'json' | 'md'): Promise<CommandResult> {
|
|
40
|
+
const records = await improvement.listKitFeedback(root);
|
|
41
|
+
ctx.stdout(format === 'md' ? renderList(records) : `${JSON.stringify(records, null, 2)}\n`);
|
|
42
|
+
return { exitCode: 0 };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function preview(record: KitFeedback, draft: Draft): string {
|
|
46
|
+
return `--- ${record.key} (preview; nothing has been sent) ---\nTitle: ${draft.title}\n\n${draft.body}\n`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function offerLink(ctx: CommandContext, draft: Draft, reason: string, exitCode: number): CommandResult {
|
|
50
|
+
ctx.stdout(`${reason}. Nothing was sent. To submit it in your browser instead, open:\n${newIssueUrl(draft.title, draft.body)}\n`);
|
|
51
|
+
return { exitCode };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function send(ctx: CommandContext, gh: GhRunner, record: KitFeedback, draft: Draft): Promise<CommandResult> {
|
|
55
|
+
const existing = await findExistingIssue(gh, improvement.reportKeyToken(record.key));
|
|
56
|
+
if (existing.code === 127) return offerLink(ctx, draft, 'gh is not installed', 0);
|
|
57
|
+
if (existing.code !== 0) return offerLink(ctx, draft, 'gh could not search upstream (run gh auth login)', 1);
|
|
58
|
+
if (existing.url) {
|
|
59
|
+
ctx.stdout(`Already reported: ${existing.url}\nNothing new was filed. Add a thumbs-up there if it affects you.\n`);
|
|
60
|
+
return { exitCode: 0 };
|
|
61
|
+
}
|
|
62
|
+
const created = await createIssue(gh, draft.title, draft.body);
|
|
63
|
+
if (created.code !== 0) return offerLink(ctx, draft, 'gh could not create the issue', 1);
|
|
64
|
+
ctx.stdout(`Filed: ${created.stdout.trim()}\n`);
|
|
65
|
+
return { exitCode: 0 };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function selectRecords(root: string, key: string | undefined): Promise<KitFeedback[]> {
|
|
69
|
+
if (key === undefined) return improvement.listKitFeedback(root);
|
|
70
|
+
const one = await improvement.readKitFeedback(root, key);
|
|
71
|
+
return one ? [one] : [];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export async function kitIssues(ctx: CommandContext, root: string, opts: KitIssueOptions, gh: GhRunner): Promise<CommandResult> {
|
|
75
|
+
const records = await selectRecords(root, opts.key);
|
|
76
|
+
if (opts.key !== undefined && records.length === 0) return fail(ctx, `no valid local record for key ${opts.key}`, 2);
|
|
77
|
+
if (!opts.submit) {
|
|
78
|
+
if (records.length === 0) ctx.stdout(NOTHING);
|
|
79
|
+
for (const r of records) ctx.stdout(preview(r, improvement.renderKitIssue(r)));
|
|
80
|
+
return { exitCode: 0 };
|
|
81
|
+
}
|
|
82
|
+
const record = records[0];
|
|
83
|
+
if (opts.key === undefined || record === undefined) return fail(ctx, '--submit needs --key <key>: reports are sent one at a time', 2);
|
|
84
|
+
const draft = improvement.renderKitIssue(record);
|
|
85
|
+
ctx.stdout(preview(record, draft));
|
|
86
|
+
const verdict = improvement.guardEgress([draft.title, draft.body]);
|
|
87
|
+
if (!verdict.ok) return fail(ctx, `blocked: outbound text matched ${verdict.rules.join(', ')}; nothing was sent`, 2);
|
|
88
|
+
if (!opts.yes) {
|
|
89
|
+
ctx.stdout(`This will be filed on github.com/${KIT_UPSTREAM_REPO} under your GitHub account.\nRun again with --yes to send it.\n`);
|
|
90
|
+
return { exitCode: 0 };
|
|
91
|
+
}
|
|
92
|
+
return send(ctx, gh, record, draft);
|
|
93
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { mkdir, mkdtemp, rm, unlink, writeFile } from 'node:fs/promises';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { promisify } from 'node:util';
|
|
6
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
7
|
+
import { createImproveCommand } from './improve.js';
|
|
8
|
+
import type { CommandContext } from './types.js';
|
|
9
|
+
import {
|
|
10
|
+
GitHubApiError,
|
|
11
|
+
type GitHubApi,
|
|
12
|
+
type IssueSummary,
|
|
13
|
+
} from '../application/improvement/github-api.js';
|
|
14
|
+
import type { GhRunner } from '../application/improvement/upstream.js';
|
|
15
|
+
|
|
16
|
+
const run = promisify(execFile);
|
|
17
|
+
const PAN = 'ABCPE1234F';
|
|
18
|
+
const MAIL = 'jane.doe@corp-example-test.dev';
|
|
19
|
+
const ENV = { GITHUB_TOKEN: 'test-token', GITHUB_REPOSITORY: 'acme/app' };
|
|
20
|
+
const noGh: GhRunner = async () => ({ code: 127, stdout: '', stderr: '' });
|
|
21
|
+
|
|
22
|
+
let dir: string;
|
|
23
|
+
|
|
24
|
+
async function commit(files: Record<string, string>): Promise<void> {
|
|
25
|
+
for (const [rel, text] of Object.entries(files)) {
|
|
26
|
+
await mkdir(path.dirname(path.join(dir, rel)), { recursive: true });
|
|
27
|
+
await writeFile(path.join(dir, rel), text);
|
|
28
|
+
}
|
|
29
|
+
await run('git', ['-C', dir, 'add', '-A']);
|
|
30
|
+
await run('git', [
|
|
31
|
+
'-C',
|
|
32
|
+
dir,
|
|
33
|
+
'-c',
|
|
34
|
+
'user.email=t@example.test',
|
|
35
|
+
'-c',
|
|
36
|
+
'user.name=t',
|
|
37
|
+
'commit',
|
|
38
|
+
'-q',
|
|
39
|
+
'-m',
|
|
40
|
+
'c',
|
|
41
|
+
]);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface Fake {
|
|
45
|
+
readonly api: GitHubApi;
|
|
46
|
+
readonly issues: IssueSummary[];
|
|
47
|
+
readonly writes: string[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function fakeGitHub(failOn?: 'create'): Fake {
|
|
51
|
+
const issues: IssueSummary[] = [];
|
|
52
|
+
const writes: string[] = [];
|
|
53
|
+
const api: GitHubApi = {
|
|
54
|
+
listIssuesByLabel: async (label) => issues.filter((i) => i.labels.includes(label)),
|
|
55
|
+
createIssue: async (n) => {
|
|
56
|
+
writes.push(`create ${n.title}`);
|
|
57
|
+
if (failOn === 'create')
|
|
58
|
+
throw new GitHubApiError(
|
|
59
|
+
403,
|
|
60
|
+
'GitHub refused POST (403): the token lacks the permission it needs.',
|
|
61
|
+
);
|
|
62
|
+
const created = {
|
|
63
|
+
number: issues.length + 1,
|
|
64
|
+
state: 'open' as const,
|
|
65
|
+
url: `u/${issues.length + 1}`,
|
|
66
|
+
...n,
|
|
67
|
+
labels: [...n.labels],
|
|
68
|
+
};
|
|
69
|
+
issues.push(created);
|
|
70
|
+
return created;
|
|
71
|
+
},
|
|
72
|
+
updateIssueBody: async (num, body) => {
|
|
73
|
+
writes.push(`update #${num}`);
|
|
74
|
+
const i = issues.findIndex((x) => x.number === num);
|
|
75
|
+
issues[i] = { ...issues[i]!, body };
|
|
76
|
+
return issues[i]!;
|
|
77
|
+
},
|
|
78
|
+
permissionOf: async () => 'none',
|
|
79
|
+
dispatchWorkflow: async () => undefined,
|
|
80
|
+
defaultBranch: async () => 'main',
|
|
81
|
+
};
|
|
82
|
+
return { api, issues, writes };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function issues(argv: string[], fake: Fake, env: NodeJS.ProcessEnv = ENV) {
|
|
86
|
+
let out = '';
|
|
87
|
+
let err = '';
|
|
88
|
+
const ctx: CommandContext = {
|
|
89
|
+
argv: ['issues', '--target', 'project', ...argv],
|
|
90
|
+
cwd: dir,
|
|
91
|
+
stdout: (s) => (out += s),
|
|
92
|
+
stderr: (s) => (err += s),
|
|
93
|
+
};
|
|
94
|
+
const cmd = createImproveCommand({ gh: noGh, env, github: () => fake.api });
|
|
95
|
+
const { exitCode } = await cmd.run(ctx);
|
|
96
|
+
return { exitCode, out, err, json: argv.includes('md') ? undefined : JSON.parse(out || '{}') };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const kinds = (json: { entries: Array<{ kind: string }> }): string[] =>
|
|
100
|
+
json.entries.map((e) => e.kind);
|
|
101
|
+
|
|
102
|
+
beforeEach(async () => {
|
|
103
|
+
dir = await mkdtemp(path.join(os.tmpdir(), 'kit-improve-project-'));
|
|
104
|
+
await run('git', ['-C', dir, 'init', '-q']);
|
|
105
|
+
await commit({
|
|
106
|
+
'src/a.ts': `const owner = '${MAIL}';\n`,
|
|
107
|
+
'docs/b.md': `pan ${PAN}\n`,
|
|
108
|
+
'clean.ts': 'export {};\n',
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
afterEach(async () => {
|
|
113
|
+
await rm(dir, { recursive: true, force: true });
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe('dry run (default)', () => {
|
|
117
|
+
it('drafts one issue per rule and file without a token or any GitHub call', async () => {
|
|
118
|
+
const fake = fakeGitHub();
|
|
119
|
+
const r = await issues([], fake, {});
|
|
120
|
+
expect(r.exitCode).toBe(0);
|
|
121
|
+
expect(r.json.mode).toBe('dry-run');
|
|
122
|
+
expect(kinds(r.json)).toEqual(['draft', 'draft']);
|
|
123
|
+
expect(fake.writes).toEqual([]);
|
|
124
|
+
expect(r.out).not.toContain(MAIL);
|
|
125
|
+
expect(r.out).not.toContain(PAN);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe('--sync', () => {
|
|
130
|
+
it('creates issues once; a second run creates no duplicates', async () => {
|
|
131
|
+
const fake = fakeGitHub();
|
|
132
|
+
const first = await issues(['--sync'], fake);
|
|
133
|
+
expect(first.exitCode).toBe(0);
|
|
134
|
+
expect(kinds(first.json)).toEqual(['created', 'created']);
|
|
135
|
+
const second = await issues(['--sync'], fake);
|
|
136
|
+
expect(kinds(second.json)).toEqual(['unchanged', 'unchanged']);
|
|
137
|
+
expect(fake.issues).toHaveLength(2);
|
|
138
|
+
expect(fake.issues.flatMap((i) => i.labels)).not.toContain('auto-fix');
|
|
139
|
+
expect(JSON.stringify(fake.issues)).not.toContain(MAIL);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('updates an issue in place when its evidence changes', async () => {
|
|
143
|
+
const fake = fakeGitHub();
|
|
144
|
+
await issues(['--sync'], fake);
|
|
145
|
+
await commit({ 'docs/b.md': `pan ${PAN}\nagain ${PAN}\n` });
|
|
146
|
+
const r = await issues(['--sync'], fake);
|
|
147
|
+
expect(kinds(r.json).sort()).toEqual(['unchanged', 'updated']);
|
|
148
|
+
expect(fake.issues).toHaveLength(2);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('never reopens a closed issue, and reports a fixed finding as stale without closing it', async () => {
|
|
152
|
+
const fake = fakeGitHub();
|
|
153
|
+
await issues(['--sync'], fake);
|
|
154
|
+
fake.issues[0] = { ...fake.issues[0]!, state: 'closed' };
|
|
155
|
+
await unlink(path.join(dir, 'src/a.ts'));
|
|
156
|
+
await commit({});
|
|
157
|
+
const r = await issues(['--sync'], fake);
|
|
158
|
+
// Issue #1 is docs/b.md (closed by a maintainer); issue #2 is src/a.ts (finding now gone).
|
|
159
|
+
expect(r.json.entries.map((e: { kind: string; issue?: number }) => [e.kind, e.issue])).toEqual([
|
|
160
|
+
['closed', 1],
|
|
161
|
+
['stale', 2],
|
|
162
|
+
]);
|
|
163
|
+
expect(fake.writes.filter((w) => w.startsWith('create'))).toHaveLength(2);
|
|
164
|
+
expect(fake.issues.filter((i) => i.state === 'open')).toHaveLength(1);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('blocks an issue whose own text fails the privacy gate, and exits 1', async () => {
|
|
168
|
+
await commit({ [`people/${MAIL}.txt`]: `pan ${PAN}\n` });
|
|
169
|
+
const fake = fakeGitHub();
|
|
170
|
+
const r = await issues(['--sync'], fake);
|
|
171
|
+
expect(r.exitCode).toBe(1);
|
|
172
|
+
expect(r.json.entries.find((e: { kind: string }) => e.kind === 'blocked')).toMatchObject({
|
|
173
|
+
title: '(withheld)',
|
|
174
|
+
});
|
|
175
|
+
expect(fake.writes.some((w) => w.includes('people/'))).toBe(false);
|
|
176
|
+
expect(r.out).not.toContain(MAIL);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('refuses without a token, before any GitHub call', async () => {
|
|
180
|
+
const fake = fakeGitHub();
|
|
181
|
+
const r = await issues(['--sync'], fake, { GITHUB_REPOSITORY: 'acme/app' });
|
|
182
|
+
expect(r.exitCode).toBe(2);
|
|
183
|
+
expect(r.err).toMatch(/no GitHub token/);
|
|
184
|
+
expect(fake.writes).toEqual([]);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('stops at the first failed write, reports what was done, and does not retry', async () => {
|
|
188
|
+
const fake = fakeGitHub('create');
|
|
189
|
+
const r = await issues(['--sync'], fake);
|
|
190
|
+
expect(r.exitCode).toBe(2);
|
|
191
|
+
expect(r.err).toMatch(/403/);
|
|
192
|
+
expect(fake.writes).toHaveLength(1);
|
|
193
|
+
});
|
|
194
|
+
});
|