@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,185 @@
|
|
|
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 { defaultGh } from '../application/improvement/upstream.js';
|
|
11
|
+
import { kitIssues, kitScan } from './improve-kit.js';
|
|
12
|
+
import { defaultGitHub, projectIssues } from './improve-project.js';
|
|
13
|
+
import { requestReview } from './improve-review.js';
|
|
14
|
+
const HELP = `Usage:
|
|
15
|
+
kit improve scan [--target project|kit] [options]
|
|
16
|
+
kit improve issues --target project [--sync] [--repo owner/name] [options]
|
|
17
|
+
kit improve issues --target kit [--key <key>] [--submit [--yes]]
|
|
18
|
+
kit improve review --pr <n> [--kind <kind>] [--instruction <text>] [--repo owner/name] [--yes]
|
|
19
|
+
|
|
20
|
+
Targets (ADR-0028):
|
|
21
|
+
project Findings about this repository. They stay here and are never sent upstream.
|
|
22
|
+
kit Sanitized feedback about the kit itself. Capture is opt-in (.ai-kit/improvement.yml);
|
|
23
|
+
only you can send it, one report at a time, after previewing it.
|
|
24
|
+
|
|
25
|
+
scan --target project:
|
|
26
|
+
--changed-only <ref> Scan only files changed since <ref> (a base ref or SHA).
|
|
27
|
+
--ignore-file <path> Ignore globs, one per line (default: .ai-kit/scan-ignore).
|
|
28
|
+
--ignore <glob> Extra ignore glob. Repeatable.
|
|
29
|
+
--format json|md Output format (default json). Output never contains matched values.
|
|
30
|
+
--fail-on-findings Exit 1 when any finding is reported.
|
|
31
|
+
|
|
32
|
+
issues --target project:
|
|
33
|
+
(no flags) Dry run: draft one issue per rule and file. No token, no GitHub call.
|
|
34
|
+
--sync Create or update those issues in this repository (GITHUB_TOKEN, GH_TOKEN
|
|
35
|
+
or your gh login). Closed issues are never reopened; fixed findings are
|
|
36
|
+
reported as stale, never closed. Bodies never contain matched values.
|
|
37
|
+
--repo owner/name Target repository (default: GITHUB_REPOSITORY, then the origin remote).
|
|
38
|
+
--format json|md Output format (default json). --ignore-file and --ignore work as for scan.
|
|
39
|
+
Exit 1 when the privacy gate withholds an issue.
|
|
40
|
+
|
|
41
|
+
review:
|
|
42
|
+
Asks this repository's ai-review.yml workflow (ci-agent) for one focused AI review. The kit has
|
|
43
|
+
no review engine of its own. The workflow's review gate still applies: an open, non-draft PR
|
|
44
|
+
from this repository, every other check green, and this kind not yet reviewed at the head.
|
|
45
|
+
--kind <kind> correctness (default), security, testing, architecture, performance,
|
|
46
|
+
maintainability, documentation, or custom.
|
|
47
|
+
--instruction <text> The focus for a custom review only (up to 500 characters).
|
|
48
|
+
--yes Send it. Without --yes, nothing is sent: AI reviews cost money.
|
|
49
|
+
|
|
50
|
+
issues --target kit:
|
|
51
|
+
(no flags) Preview every local report as the issue it would become.
|
|
52
|
+
--key <key> Limit to one report.
|
|
53
|
+
--submit --key <key> Show exactly what would be sent, and where.
|
|
54
|
+
--submit --yes Send it with your own gh login, or get a prefilled link if gh is unavailable.
|
|
55
|
+
`;
|
|
56
|
+
function single(argv, name) {
|
|
57
|
+
const i = argv.indexOf(name);
|
|
58
|
+
const v = i >= 0 ? argv[i + 1] : undefined;
|
|
59
|
+
return v !== undefined && !v.startsWith('--') ? v : undefined;
|
|
60
|
+
}
|
|
61
|
+
function repeated(argv, name) {
|
|
62
|
+
const out = [];
|
|
63
|
+
for (let i = 0; i < argv.length; i++) {
|
|
64
|
+
const v = argv[i + 1];
|
|
65
|
+
if (argv[i] === name && v !== undefined && !v.startsWith('--'))
|
|
66
|
+
out.push(v);
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
function fail(ctx, message, exitCode = 1) {
|
|
71
|
+
ctx.stderr(`kit improve: ${message}\n`);
|
|
72
|
+
return { exitCode, message };
|
|
73
|
+
}
|
|
74
|
+
async function scan(ctx, root) {
|
|
75
|
+
const format = single(ctx.argv, '--format') ?? 'json';
|
|
76
|
+
if (format !== 'json' && format !== 'md')
|
|
77
|
+
return fail(ctx, `unknown --format ${format}`, 2);
|
|
78
|
+
const changedOnly = single(ctx.argv, '--changed-only');
|
|
79
|
+
const ignoreFile = single(ctx.argv, '--ignore-file');
|
|
80
|
+
try {
|
|
81
|
+
const report = await improvement.scanRepo({
|
|
82
|
+
root,
|
|
83
|
+
...(changedOnly !== undefined ? { changedOnly } : {}),
|
|
84
|
+
...(ignoreFile !== undefined ? { ignoreFile: path.resolve(ctx.cwd, ignoreFile) } : {}),
|
|
85
|
+
extraIgnores: repeated(ctx.argv, '--ignore'),
|
|
86
|
+
});
|
|
87
|
+
ctx.stdout(format === 'md' ? improvement.toMarkdown(report) : `${JSON.stringify(report, null, 2)}\n`);
|
|
88
|
+
const failOn = ctx.argv.includes('--fail-on-findings');
|
|
89
|
+
return { exitCode: failOn && report.findings.length > 0 ? 1 : 0 };
|
|
90
|
+
}
|
|
91
|
+
catch (err) {
|
|
92
|
+
// Report the error class and first line only. Never print file contents from here.
|
|
93
|
+
const e = err;
|
|
94
|
+
const first = (e.message ?? '').split('\n')[0] ?? '';
|
|
95
|
+
return fail(ctx, `scan failed: ${e.code ?? e.name}: ${first}`, 2);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function targetOf(ctx, fallback) {
|
|
99
|
+
const t = single(ctx.argv, '--target') ?? fallback;
|
|
100
|
+
if (t === undefined)
|
|
101
|
+
return undefined;
|
|
102
|
+
return t === 'project' || t === 'kit' ? t : 'invalid';
|
|
103
|
+
}
|
|
104
|
+
async function runScan(ctx, root) {
|
|
105
|
+
const target = targetOf(ctx, 'project');
|
|
106
|
+
if (target === 'invalid')
|
|
107
|
+
return fail(ctx, 'unknown --target (use project or kit)', 2);
|
|
108
|
+
if (target === 'project')
|
|
109
|
+
return scan(ctx, root);
|
|
110
|
+
const format = single(ctx.argv, '--format') ?? 'json';
|
|
111
|
+
if (format !== 'json' && format !== 'md')
|
|
112
|
+
return fail(ctx, `unknown --format ${format}`, 2);
|
|
113
|
+
return kitScan(ctx, root, format);
|
|
114
|
+
}
|
|
115
|
+
function runProjectIssues(ctx, root, deps) {
|
|
116
|
+
const format = single(ctx.argv, '--format') ?? 'json';
|
|
117
|
+
if (format !== 'json' && format !== 'md')
|
|
118
|
+
return fail(ctx, `unknown --format ${format}`, 2);
|
|
119
|
+
const repo = single(ctx.argv, '--repo');
|
|
120
|
+
const ignoreFile = single(ctx.argv, '--ignore-file');
|
|
121
|
+
return projectIssues(ctx, root, {
|
|
122
|
+
sync: ctx.argv.includes('--sync'),
|
|
123
|
+
format,
|
|
124
|
+
...(repo !== undefined ? { repo } : {}),
|
|
125
|
+
...(ignoreFile !== undefined ? { ignoreFile: path.resolve(ctx.cwd, ignoreFile) } : {}),
|
|
126
|
+
extraIgnores: repeated(ctx.argv, '--ignore'),
|
|
127
|
+
}, deps);
|
|
128
|
+
}
|
|
129
|
+
async function runIssues(ctx, root, deps) {
|
|
130
|
+
const target = targetOf(ctx);
|
|
131
|
+
if (target === undefined || target === 'invalid')
|
|
132
|
+
return fail(ctx, 'issues needs --target kit or --target project', 2);
|
|
133
|
+
if (target === 'project')
|
|
134
|
+
return runProjectIssues(ctx, root, deps);
|
|
135
|
+
const key = single(ctx.argv, '--key');
|
|
136
|
+
const opts = {
|
|
137
|
+
...(key !== undefined ? { key } : {}),
|
|
138
|
+
submit: ctx.argv.includes('--submit'),
|
|
139
|
+
yes: ctx.argv.includes('--yes'),
|
|
140
|
+
};
|
|
141
|
+
return kitIssues(ctx, root, opts, deps.gh);
|
|
142
|
+
}
|
|
143
|
+
function runReview(ctx, deps) {
|
|
144
|
+
const pick = (name) => {
|
|
145
|
+
const v = single(ctx.argv, `--${name}`);
|
|
146
|
+
return v === undefined ? {} : { [name]: v };
|
|
147
|
+
};
|
|
148
|
+
return requestReview(ctx, {
|
|
149
|
+
...pick('pr'),
|
|
150
|
+
...pick('kind'),
|
|
151
|
+
...pick('instruction'),
|
|
152
|
+
...pick('repo'),
|
|
153
|
+
yes: ctx.argv.includes('--yes'),
|
|
154
|
+
}, deps);
|
|
155
|
+
}
|
|
156
|
+
/** `gh`, the environment and the GitHub client are injectable so tests never reach GitHub. */
|
|
157
|
+
export function createImproveCommand(deps = {}) {
|
|
158
|
+
const resolved = {
|
|
159
|
+
gh: deps.gh ?? defaultGh,
|
|
160
|
+
env: deps.env ?? process.env,
|
|
161
|
+
github: deps.github ?? defaultGitHub,
|
|
162
|
+
};
|
|
163
|
+
return {
|
|
164
|
+
name: 'improve',
|
|
165
|
+
summary: 'Continuous improvement: project findings, and opt-in sanitized feedback about the kit (ADR-0028)',
|
|
166
|
+
async run(ctx) {
|
|
167
|
+
const sub = ctx.argv.find((a) => !a.startsWith('-'));
|
|
168
|
+
const help = ctx.argv.includes('--help') || ctx.argv.includes('-h');
|
|
169
|
+
if (help || sub === undefined) {
|
|
170
|
+
ctx.stdout(HELP);
|
|
171
|
+
return { exitCode: sub === undefined && !help ? 2 : 0 };
|
|
172
|
+
}
|
|
173
|
+
const root = path.resolve(ctx.cwd);
|
|
174
|
+
if (sub === 'scan')
|
|
175
|
+
return runScan(ctx, root);
|
|
176
|
+
if (sub === 'issues')
|
|
177
|
+
return runIssues(ctx, root, resolved);
|
|
178
|
+
if (sub === 'review')
|
|
179
|
+
return runReview(ctx, resolved);
|
|
180
|
+
return fail(ctx, `unknown subcommand: ${sub}`, 2);
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
export const improveCommand = createImproveCommand();
|
|
185
|
+
//# sourceMappingURL=improve.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"improve.js","sourceRoot":"","sources":["../../src/commands/improve.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,EAAE;AACF,2FAA2F;AAC3F,2EAA2E;AAC3E,EAAE;AACF,+FAA+F;AAC/F,4BAA4B;AAC5B,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAErD,OAAO,EAAE,SAAS,EAAiB,MAAM,wCAAwC,CAAC;AAElF,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,aAAa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,IAAI,CAAC,GAAG,EAAE,uBAAuB,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC;QACpD,CAAC;KACF,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,MAAM,cAAc,GAAY,oBAAoB,EAAE,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,12 @@
|
|
|
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
|
import { createDispatcher } from './application/dispatcher.js';
|
|
11
|
+
import { afterDispatch, captureKitFailure } from './application/improvement/capture.js';
|
|
7
12
|
async function main() {
|
|
8
13
|
const argv = process.argv.slice(2);
|
|
9
14
|
const ctx = {
|
|
@@ -12,12 +17,24 @@ async function main() {
|
|
|
12
17
|
stdout: (s) => process.stdout.write(s + '\n'),
|
|
13
18
|
stderr: (s) => process.stderr.write(s + '\n'),
|
|
14
19
|
};
|
|
15
|
-
const { dispatch } = createDispatcher();
|
|
16
|
-
const
|
|
20
|
+
const { dispatch, registry } = createDispatcher();
|
|
21
|
+
const isKnown = (name) => registry.get(name) !== undefined;
|
|
22
|
+
let result;
|
|
23
|
+
try {
|
|
24
|
+
result = await dispatch(ctx);
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
const command = argv[0] ?? '';
|
|
28
|
+
if (isKnown(command)) {
|
|
29
|
+
await captureKitFailure({ cwd: ctx.cwd, argv, command, exitCode: 2, failure: 'internal-error', message: String(e) });
|
|
30
|
+
}
|
|
31
|
+
throw e;
|
|
32
|
+
}
|
|
17
33
|
if (result.message && result.command !== 'help' && result.command !== 'doctor') {
|
|
18
34
|
// help and doctor write their own output; everything else prints the message.
|
|
19
35
|
process.stdout.write(result.message + '\n');
|
|
20
36
|
}
|
|
37
|
+
await afterDispatch(ctx.cwd, argv, result, isKnown);
|
|
21
38
|
process.exit(result.exitCode);
|
|
22
39
|
}
|
|
23
40
|
main().catch((e) => {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA,4BAA4B;AAC5B,EAAE;AACF,sEAAsE;AACtE,4BAA4B;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA,4BAA4B;AAC5B,EAAE;AACF,sEAAsE;AACtE,4BAA4B;AAC5B,EAAE;AACF,kGAAkG;AAClG,+FAA+F;AAC/F,+BAA+B;AAE/B,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,sCAAsC,CAAC;AAGxF,KAAK,UAAU,IAAI;IACjB,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACnC,MAAM,GAAG,GAAmB;QAC1B,IAAI;QACJ,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE;QAClB,MAAM,EAAE,CAAC,CAAS,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,GAAG,IAAI,CAAC;QACrD,MAAM,EAAE,CAAC,CAAS,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,GAAG,IAAI,CAAC;KACtD,CAAC;IACF,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,gBAAgB,EAAE,CAAC;IAClD,MAAM,OAAO,GAAG,CAAC,IAAY,EAAW,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,SAAS,CAAC;IAC5E,IAAI,MAAM,CAAC;IACX,IAAI,CAAC;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`kit` executable: user-facing entry point for the Agentic SDLC Kit.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -25,12 +25,12 @@
|
|
|
25
25
|
"src"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@agentic-sdlc-kit/contracts": "1.0
|
|
29
|
-
"@agentic-sdlc-kit/core": "1.0
|
|
28
|
+
"@agentic-sdlc-kit/contracts": "1.2.0",
|
|
29
|
+
"@agentic-sdlc-kit/core": "1.2.0",
|
|
30
30
|
"@modelcontextprotocol/sdk": "^1.32.2"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@agentic-sdlc-kit/testing": "1.0
|
|
33
|
+
"@agentic-sdlc-kit/testing": "1.2.0",
|
|
34
34
|
"@types/node": "^26.6.11",
|
|
35
35
|
"typescript": "^5.5.0",
|
|
36
36
|
"vitest": "^2.0.0"
|
|
@@ -66,12 +66,17 @@ class ScriptedProvider implements AgentProvider {
|
|
|
66
66
|
readonly id: AgentProviderId;
|
|
67
67
|
private readonly events: readonly AgentExecutionEvent[];
|
|
68
68
|
private readonly result: (request: AgentExecutionRequest) => Promise<AgentExecutionResult>;
|
|
69
|
+
// Optional gates hold capabilities() or start() open, so a test can act inside that window.
|
|
70
|
+
private readonly capabilitiesGate: Promise<void> | undefined;
|
|
71
|
+
private readonly startGate: Promise<void> | undefined;
|
|
69
72
|
readonly started: AgentExecutionRequest[] = [];
|
|
70
73
|
|
|
71
74
|
constructor(opts: {
|
|
72
75
|
id?: string;
|
|
73
76
|
events?: readonly AgentExecutionEvent[];
|
|
74
77
|
result?: (request: AgentExecutionRequest) => Promise<AgentExecutionResult>;
|
|
78
|
+
capabilitiesGate?: Promise<void>;
|
|
79
|
+
startGate?: Promise<void>;
|
|
75
80
|
}) {
|
|
76
81
|
// Default 'mock': the shared sample request's providerId is 'mock', so
|
|
77
82
|
// the gateway's registry lookup succeeds without request surgery.
|
|
@@ -79,9 +84,12 @@ class ScriptedProvider implements AgentProvider {
|
|
|
79
84
|
this.events = opts.events ?? [];
|
|
80
85
|
this.result =
|
|
81
86
|
opts.result ?? ((request) => Promise.resolve(buildScriptedResult(request.executionId)));
|
|
87
|
+
this.capabilitiesGate = opts.capabilitiesGate;
|
|
88
|
+
this.startGate = opts.startGate;
|
|
82
89
|
}
|
|
83
90
|
|
|
84
91
|
async capabilities(): Promise<AgentProviderCapabilities> {
|
|
92
|
+
await this.capabilitiesGate;
|
|
85
93
|
return {
|
|
86
94
|
capabilities: ['repository.read', 'repository.write', 'process.test'],
|
|
87
95
|
budgetEnforcement: {
|
|
@@ -104,6 +112,7 @@ class ScriptedProvider implements AgentProvider {
|
|
|
104
112
|
_signal: AbortSignal,
|
|
105
113
|
): Promise<AgentExecutionHandle> {
|
|
106
114
|
this.started.push(request);
|
|
115
|
+
await this.startGate;
|
|
107
116
|
const listeners = new Set<AgentEventSink>();
|
|
108
117
|
// Events replay synchronously to every subscriber (the gateway
|
|
109
118
|
// subscribes before awaiting result()).
|
|
@@ -513,9 +522,14 @@ describe('AgentGateway resume and cancel', () => {
|
|
|
513
522
|
});
|
|
514
523
|
const gateway = new AgentGateway({ providers: [provider], projectRoot: root, drainMs: 200 });
|
|
515
524
|
const startPromise = gateway.start(request, { policySnapshot: snapshot });
|
|
516
|
-
//
|
|
517
|
-
|
|
518
|
-
|
|
525
|
+
// Cancel only once start has persisted the record: cancel() reports an unsaved id as unknown
|
|
526
|
+
// and never reaches the in-flight abort, so a fixed sleep raced on slow CI runners and the
|
|
527
|
+
// never-settling provider hung the test until its timeout.
|
|
528
|
+
for (let i = 0; i < 200 && !(await gateway.getExecution(request.executionId)); i++) {
|
|
529
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
530
|
+
}
|
|
531
|
+
const cancelled = await gateway.cancel(request.executionId);
|
|
532
|
+
expect(cancelled.ok).toBe(true);
|
|
519
533
|
const res = await startPromise;
|
|
520
534
|
// The scripted provider never lands a result; after the drain window
|
|
521
535
|
// the gateway concludes the execution ended by its own cancellation.
|
|
@@ -527,3 +541,66 @@ describe('AgentGateway resume and cancel', () => {
|
|
|
527
541
|
expect(again.ok).toBe(true);
|
|
528
542
|
});
|
|
529
543
|
});
|
|
544
|
+
|
|
545
|
+
/** A promise and the function that resolves it. */
|
|
546
|
+
function gate(): { wait: Promise<void>; open: () => void } {
|
|
547
|
+
let open = (): void => undefined;
|
|
548
|
+
const wait = new Promise<void>((resolve) => {
|
|
549
|
+
open = resolve;
|
|
550
|
+
});
|
|
551
|
+
return { wait, open };
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
const neverSettles = (): Promise<AgentExecutionResult> =>
|
|
555
|
+
new Promise<AgentExecutionResult>(() => undefined);
|
|
556
|
+
|
|
557
|
+
describe('AgentGateway cancel of an execution that is not yet persisted or started', () => {
|
|
558
|
+
it('cancels before the record is persisted, records the request, and never starts the provider', async () => {
|
|
559
|
+
const root = await tempProjectRoot();
|
|
560
|
+
const { request, snapshot } = buildRequest();
|
|
561
|
+
const caps = gate();
|
|
562
|
+
const provider = new ScriptedProvider({ result: neverSettles, capabilitiesGate: caps.wait });
|
|
563
|
+
const gateway = new AgentGateway({ providers: [provider], projectRoot: root, drainMs: 200 });
|
|
564
|
+
const startPromise = gateway.start(request, { policySnapshot: snapshot });
|
|
565
|
+
// start() is held inside capabilities(): nothing is persisted yet.
|
|
566
|
+
const cancelPromise = gateway.cancel(request.executionId);
|
|
567
|
+
caps.open();
|
|
568
|
+
const cancelled = await cancelPromise;
|
|
569
|
+
expect(cancelled.ok).toBe(true);
|
|
570
|
+
if (cancelled.ok) expect(cancelled.value.status).toBe('cancelled');
|
|
571
|
+
const res = await startPromise;
|
|
572
|
+
expect(res.ok).toBe(true);
|
|
573
|
+
if (res.ok) expect(res.value.status).toBe('cancelled');
|
|
574
|
+
expect(provider.started).toHaveLength(0);
|
|
575
|
+
const stored = await gateway.getExecution(request.executionId);
|
|
576
|
+
expect(stored?.status).toBe('cancelled');
|
|
577
|
+
expect(stored?.request.executionId).toBe(request.executionId);
|
|
578
|
+
});
|
|
579
|
+
|
|
580
|
+
it('cancels while the provider is still starting, instead of running to the duration budget', async () => {
|
|
581
|
+
const root = await tempProjectRoot();
|
|
582
|
+
const { request, snapshot } = buildRequest();
|
|
583
|
+
const starting = gate();
|
|
584
|
+
const provider = new ScriptedProvider({ result: neverSettles, startGate: starting.wait });
|
|
585
|
+
const gateway = new AgentGateway({ providers: [provider], projectRoot: root, drainMs: 200 });
|
|
586
|
+
const startPromise = gateway.start(request, { policySnapshot: snapshot });
|
|
587
|
+
// Wait until the record is persisted and the provider has been asked to start.
|
|
588
|
+
for (let i = 0; i < 200 && provider.started.length === 0; i++) {
|
|
589
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
590
|
+
}
|
|
591
|
+
const cancelled = await gateway.cancel(request.executionId);
|
|
592
|
+
expect(cancelled.ok).toBe(true);
|
|
593
|
+
starting.open();
|
|
594
|
+
const res = await startPromise;
|
|
595
|
+
expect(res.ok).toBe(true);
|
|
596
|
+
if (res.ok) expect(res.value.status).toBe('cancelled');
|
|
597
|
+
expect((await gateway.getExecution(request.executionId))?.status).toBe('cancelled');
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
it('still refuses an id that is neither stored nor in flight', async () => {
|
|
601
|
+
const root = await tempProjectRoot();
|
|
602
|
+
const gateway = new AgentGateway({ providers: [new ScriptedProvider({})], projectRoot: root });
|
|
603
|
+
const res = await gateway.cancel('exec-never-started');
|
|
604
|
+
expect(res.ok).toBe(false);
|
|
605
|
+
});
|
|
606
|
+
});
|
|
@@ -87,8 +87,14 @@ export class AgentGateway {
|
|
|
87
87
|
private readonly drainMs: number;
|
|
88
88
|
/** Resume tokens, held in memory ONLY (never persisted). */
|
|
89
89
|
private readonly resumeTokens = new Map<string, string>();
|
|
90
|
-
/**
|
|
90
|
+
/**
|
|
91
|
+
* In-flight execution controllers, so cancel() can reach them. start()
|
|
92
|
+
* registers its controller before its first await, so an execution is
|
|
93
|
+
* reachable before its record is persisted or its provider has started.
|
|
94
|
+
*/
|
|
91
95
|
private readonly inFlight = new Map<string, AbortController>();
|
|
96
|
+
/** Settlement of each start() still running, so cancel() can await an unsaved one. */
|
|
97
|
+
private readonly starting = new Map<string, Promise<Result<StoredAgentExecution>>>();
|
|
92
98
|
/**
|
|
93
99
|
* Per-execution persistence queue. Event arrivals, the terminal result,
|
|
94
100
|
* and late drain reports all write the SAME record file; concurrent
|
|
@@ -132,7 +138,24 @@ export class AgentGateway {
|
|
|
132
138
|
};
|
|
133
139
|
}
|
|
134
140
|
const valid = parsed.data;
|
|
141
|
+
const id = valid.executionId;
|
|
142
|
+
const controller = new AbortController();
|
|
143
|
+
this.inFlight.set(id, controller);
|
|
144
|
+
const run = this.startValidated(valid, opts, controller);
|
|
145
|
+
this.starting.set(id, run);
|
|
146
|
+
try {
|
|
147
|
+
return await run;
|
|
148
|
+
} finally {
|
|
149
|
+
if (this.starting.get(id) === run) this.starting.delete(id);
|
|
150
|
+
if (this.inFlight.get(id) === controller) this.inFlight.delete(id);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
135
153
|
|
|
154
|
+
private async startValidated(
|
|
155
|
+
valid: AgentExecutionRequest,
|
|
156
|
+
opts: StartExecutionOptions,
|
|
157
|
+
controller: AbortController,
|
|
158
|
+
): Promise<Result<StoredAgentExecution>> {
|
|
136
159
|
// 2. Provider must be configured.
|
|
137
160
|
const provider = this.providers.get(valid.providerId);
|
|
138
161
|
if (!provider) {
|
|
@@ -182,14 +205,21 @@ export class AgentGateway {
|
|
|
182
205
|
if (persistErr) return { ok: false, error: persistErr };
|
|
183
206
|
|
|
184
207
|
// 6. Run under the gateway's controller, which links the external
|
|
185
|
-
// signal and the duration budget.
|
|
186
|
-
|
|
208
|
+
// signal and the duration budget. An execution cancelled or aborted
|
|
209
|
+
// before this point never reaches the provider: its request is on
|
|
210
|
+
// disk (step 5) and it ends cancelled here.
|
|
187
211
|
const onExternalAbort = () => controller.abort();
|
|
188
212
|
if (opts.signal) {
|
|
189
213
|
if (opts.signal.aborted) controller.abort();
|
|
190
214
|
else opts.signal.addEventListener('abort', onExternalAbort, { once: true });
|
|
191
215
|
}
|
|
192
216
|
try {
|
|
217
|
+
if (controller.signal.aborted) {
|
|
218
|
+
return await this.finalize(execution, {
|
|
219
|
+
status: 'cancelled',
|
|
220
|
+
gatewayNotes: [...execution.gatewayNotes, 'gateway: cancelled before the provider started'],
|
|
221
|
+
});
|
|
222
|
+
}
|
|
193
223
|
const handle = await provider.start(valid, controller.signal);
|
|
194
224
|
return await this.awaitSettlement(execution, handle, controller, valid.budget);
|
|
195
225
|
} finally {
|
|
@@ -238,12 +268,32 @@ export class AgentGateway {
|
|
|
238
268
|
/** Cancel an execution (idempotent; unknown ids are an error, not a guess). */
|
|
239
269
|
async cancel(executionId: string): Promise<Result<StoredAgentExecution>> {
|
|
240
270
|
const existing = await loadExecution(this.projectRoot, executionId);
|
|
241
|
-
if (!existing)
|
|
271
|
+
if (!existing) return this.cancelUnsaved(executionId);
|
|
272
|
+
return this.cancelStored(existing);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* An execution whose start() is still running but has not persisted its
|
|
277
|
+
* record yet: abort it and return the record that start() settles on.
|
|
278
|
+
* An id that is neither stored nor in flight stays an error.
|
|
279
|
+
*/
|
|
280
|
+
private async cancelUnsaved(executionId: string): Promise<Result<StoredAgentExecution>> {
|
|
281
|
+
const pending = this.starting.get(executionId);
|
|
282
|
+
if (pending === undefined) {
|
|
283
|
+
// start() may have persisted the record since the first lookup.
|
|
284
|
+
const late = await loadExecution(this.projectRoot, executionId);
|
|
285
|
+
if (late) return this.cancelStored(late);
|
|
242
286
|
return {
|
|
243
287
|
ok: false,
|
|
244
288
|
error: Errors.contractInvalid(`Unknown agent execution '${executionId}'`),
|
|
245
289
|
};
|
|
246
290
|
}
|
|
291
|
+
this.inFlight.get(executionId)?.abort();
|
|
292
|
+
return pending;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
private async cancelStored(existing: StoredAgentExecution): Promise<Result<StoredAgentExecution>> {
|
|
296
|
+
const executionId = existing.executionId;
|
|
247
297
|
const provider = this.providers.get(existing.providerId);
|
|
248
298
|
if (provider) await provider.cancel(executionId); // unknown id: no-op by contract
|
|
249
299
|
// Reach the in-flight execution: aborting its controller starts the
|
|
@@ -324,6 +374,13 @@ export class AgentGateway {
|
|
|
324
374
|
// if it never does, the gateway concludes cancelled.
|
|
325
375
|
const onAbortDrain = (): void => breachNotifier?.();
|
|
326
376
|
controller.signal.addEventListener('abort', onAbortDrain, { once: true });
|
|
377
|
+
// Aborted while the provider was still starting (cancel or external
|
|
378
|
+
// abort): the listener above never fires for a past abort, so tell the
|
|
379
|
+
// provider now and start the bounded drain.
|
|
380
|
+
if (controller.signal.aborted) {
|
|
381
|
+
void this.providers.get(current.providerId)?.cancel(current.executionId);
|
|
382
|
+
onAbortDrain();
|
|
383
|
+
}
|
|
327
384
|
|
|
328
385
|
let drainTimer: ReturnType<typeof setTimeout> | undefined;
|
|
329
386
|
const settlement: Settlement = await new Promise<Settlement>((resolve) => {
|
|
@@ -16,6 +16,7 @@ import { rulesCommand } from '../commands/rules.js';
|
|
|
16
16
|
import { onboardCommand } from '../commands/onboard.js';
|
|
17
17
|
import { contextCommand } from '../commands/context.js';
|
|
18
18
|
import { learningCommand } from '../commands/learning.js';
|
|
19
|
+
import { improveCommand } from '../commands/improve.js';
|
|
19
20
|
import { benchCommand } from '../commands/bench.js';
|
|
20
21
|
import { routingCommand } from '../commands/routing.js';
|
|
21
22
|
import { knowledgeCommand } from '../commands/knowledge.js';
|
|
@@ -64,6 +65,7 @@ export function createDispatcher(opts: DispatcherOptions = {}): {
|
|
|
64
65
|
if (!registry.get('onboard')) registry.register(onboardCommand);
|
|
65
66
|
if (!registry.get('context')) registry.register(contextCommand);
|
|
66
67
|
if (!registry.get('learning')) registry.register(learningCommand);
|
|
68
|
+
if (!registry.get('improve')) registry.register(improveCommand);
|
|
67
69
|
if (!registry.get('bench')) registry.register(benchCommand);
|
|
68
70
|
if (!registry.get('routing')) registry.register(routingCommand);
|
|
69
71
|
if (!registry.get('knowledge')) registry.register(knowledgeCommand);
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { mkdir, mkdtemp, readdir, 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 { afterDispatch, captureKitFailure } from './capture.js';
|
|
6
|
+
import type { GitRunner } from './scrub-terms.js';
|
|
7
|
+
|
|
8
|
+
// A fictional developer's repository, as git would report it.
|
|
9
|
+
const git: GitRunner = async (_cwd, args) => {
|
|
10
|
+
const key = args.join(' ');
|
|
11
|
+
if (key === 'remote get-url origin') return 'git@github.com:acme-corp/acme-payments.git';
|
|
12
|
+
if (key === 'rev-parse --abbrev-ref HEAD') return 'feature/billing-v2';
|
|
13
|
+
if (key === 'config user.name') return 'Priya Sharma';
|
|
14
|
+
if (key === 'config user.email') return 'priya@acme.dev';
|
|
15
|
+
return undefined;
|
|
16
|
+
};
|
|
17
|
+
const DEPS = { git, env: {}, kitVersion: '0.10.2' };
|
|
18
|
+
|
|
19
|
+
let dir: string;
|
|
20
|
+
|
|
21
|
+
async function optIn(): Promise<void> {
|
|
22
|
+
await mkdir(path.join(dir, '.ai-kit'), { recursive: true });
|
|
23
|
+
await writeFile(path.join(dir, '.ai-kit', 'improvement.yml'), 'version: 1\nkitFeedback:\n capture: true\n');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const failure = (message: string) => ({ cwd: dir, command: 'verify', exitCode: 2, failure: 'runtime-error' as const, message });
|
|
27
|
+
|
|
28
|
+
beforeEach(async () => {
|
|
29
|
+
dir = await mkdtemp(path.join(os.tmpdir(), 'kit-capture-'));
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
afterEach(async () => {
|
|
33
|
+
await rm(dir, { recursive: true, force: true });
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe('captureKitFailure', () => {
|
|
37
|
+
it('records nothing and creates no state when the user has not opted in', async () => {
|
|
38
|
+
expect(await captureKitFailure(failure('ENOENT: missing'), DEPS)).toBeUndefined();
|
|
39
|
+
expect(await readdir(dir)).toEqual([]);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('records a sanitized record with no trace of the project, repository, branch or author', async () => {
|
|
43
|
+
await optIn();
|
|
44
|
+
const message = `ENOENT: no such file, open '${dir}/src/ledger.ts' on feature/billing-v2 by Priya Sharma <priya@acme.dev> for acme-corp/acme-payments`;
|
|
45
|
+
const record = await captureKitFailure(failure(message), DEPS);
|
|
46
|
+
expect(record?.errorCode).toBe('ENOENT');
|
|
47
|
+
const json = JSON.stringify(record).toLowerCase();
|
|
48
|
+
for (const term of [dir.toLowerCase(), path.basename(dir).toLowerCase(), 'acme', 'priya', 'sharma', 'billing', 'ledger']) {
|
|
49
|
+
expect(json).not.toContain(term);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('scrubs what the user typed: refs, globs and flag values never reach the record', async () => {
|
|
54
|
+
await optIn();
|
|
55
|
+
const argv = ['improve', 'scan', '--changed-only', 'hotfix-ledger', '--ignore=vendor/acmekit/**'];
|
|
56
|
+
const message = 'git diff hotfix-ledger...HEAD failed; ignore acmekit; exit 128';
|
|
57
|
+
const record = await captureKitFailure({ ...failure(message), argv }, DEPS);
|
|
58
|
+
const json = JSON.stringify(record).toLowerCase();
|
|
59
|
+
for (const term of ['hotfix-ledger', 'ledger', 'acmekit']) expect(json).not.toContain(term);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('never throws on an invalid config, and records nothing', async () => {
|
|
63
|
+
await mkdir(path.join(dir, '.ai-kit'), { recursive: true });
|
|
64
|
+
await writeFile(path.join(dir, '.ai-kit', 'improvement.yml'), 'version: 2\n');
|
|
65
|
+
await expect(captureKitFailure(failure('boom'), DEPS)).resolves.toBeUndefined();
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe('afterDispatch', () => {
|
|
70
|
+
it('captures runtime failures of known commands only', async () => {
|
|
71
|
+
await optIn();
|
|
72
|
+
const known = (n: string): boolean => n === 'verify';
|
|
73
|
+
await afterDispatch(dir, ['verify'], { exitCode: 1, command: 'verify', message: 'usage' }, known, DEPS);
|
|
74
|
+
await afterDispatch(dir, ['x'], { exitCode: 2, command: 'not-a-command', message: 'x' }, known, DEPS);
|
|
75
|
+
expect(await readdir(dir)).toEqual(['.ai-kit']);
|
|
76
|
+
await afterDispatch(dir, ['verify'], { exitCode: 2, command: 'verify', message: 'ENOENT: missing' }, known, DEPS);
|
|
77
|
+
const stored = await readdir(path.join(dir, '.ai-kit', 'state', 'kit-feedback'));
|
|
78
|
+
expect(stored.filter((n) => n.endsWith('.json'))).toHaveLength(1);
|
|
79
|
+
});
|
|
80
|
+
});
|