ai-agentmanager 1.31.0 → 1.33.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/CHANGELOG.md +15 -0
- package/package.json +1 -1
- package/public/app.js +58 -6
- package/public/codemap.js +119 -5
- package/public/eval.js +42 -0
- package/public/issues.js +2 -1
- package/public/style.css +45 -0
- package/public/today.js +22 -2
- package/src/benchmarks.js +309 -0
- package/src/eval.js +438 -0
- package/src/issues.js +2 -0
- package/src/jira.js +7 -4
- package/src/manager.js +60 -5
- package/src/review.js +21 -0
- package/src/server.js +54 -3
- package/src/today.js +13 -1
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
// Micro benchmarks per module, for the Jira issues that change them. You mark the modules of a
|
|
2
|
+
// project where speed matters (on the Map's Speed view), each with the Jira components or labels
|
|
3
|
+
// that mean it. An agent working a Jira issue benchmarks a module when its issue names it (a
|
|
4
|
+
// component or label, or the folder in the request), or when its change touches the module: if it
|
|
5
|
+
// says it's done without having measured one, it's asked to before it counts as done.
|
|
6
|
+
//
|
|
7
|
+
// The benchmarks of a module are kept on this computer, outside the repository (bench/ in the home
|
|
8
|
+
// folder), and reused: the next issue on the module runs the same ones again, before and after its
|
|
9
|
+
// change, and adds its own cases, so the numbers line up from one issue to the next. Each run
|
|
10
|
+
// leaves its numbers in BENCH.json next to the agent's brief; they are kept here per module and
|
|
11
|
+
// issue, and a case more than 5% slower is shown in Today.
|
|
12
|
+
|
|
13
|
+
import crypto from 'node:crypto';
|
|
14
|
+
import fs from 'node:fs';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
import { briefDir } from './brief.js';
|
|
17
|
+
import { cleanPath } from './bughunt.js';
|
|
18
|
+
import { paths } from './config.js';
|
|
19
|
+
|
|
20
|
+
export const RESULTS_FILE = 'BENCH.json';
|
|
21
|
+
export const resultsPath = (agentId) => path.join(briefDir(agentId), RESULTS_FILE);
|
|
22
|
+
// Slower than this (in percent) and the run shows in Today.
|
|
23
|
+
export const SLOWER_PCT = 5;
|
|
24
|
+
const MAX_RUNS = 500;
|
|
25
|
+
const MAX_CASES = 40;
|
|
26
|
+
const err = (status, message) => Object.assign(new Error(message), { status });
|
|
27
|
+
const clip = (s, n) => {
|
|
28
|
+
const t = String(s ?? '').trim();
|
|
29
|
+
return t.length > n ? `${t.slice(0, n - 1)}…` : t;
|
|
30
|
+
};
|
|
31
|
+
const now = () => new Date().toISOString();
|
|
32
|
+
|
|
33
|
+
/** Where a module's benchmarks are kept: bench/<project>-<hash>/<module> in the home folder. */
|
|
34
|
+
export function moduleDir(projectKey, modulePath, home = paths.home) {
|
|
35
|
+
const name = String(projectKey).split(/[/:]/).filter(Boolean).pop()?.replace(/[^\w.-]+/g, '-').slice(0, 40) || 'project';
|
|
36
|
+
const hash = crypto.createHash('sha1').update(String(projectKey)).digest('hex').slice(0, 8);
|
|
37
|
+
return path.join(home, 'bench', `${name}-${hash}`, (modulePath || '_root').replace(/\//g, '__'));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Whether a repository file is in the module (a folder; '' is the whole repository). */
|
|
41
|
+
export const inModule = (file, modulePath) => !modulePath || file === modulePath || file.startsWith(`${modulePath}/`);
|
|
42
|
+
|
|
43
|
+
/** Component or label names: trimmed, no duplicates (case aside), at most 20. */
|
|
44
|
+
function names(list) {
|
|
45
|
+
const out = [];
|
|
46
|
+
for (const n of (Array.isArray(list) ? list : String(list ?? '').split(',')).map((x) => clip(x, 80)).filter(Boolean)) {
|
|
47
|
+
if (!out.some((o) => o.toLowerCase() === n.toLowerCase())) out.push(n);
|
|
48
|
+
}
|
|
49
|
+
return out.slice(0, 20);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The watched modules a Jira issue is about: a component or label of the issue is one of the
|
|
54
|
+
* module's (or its folder's name), or the request names the folder ("src/storage").
|
|
55
|
+
*/
|
|
56
|
+
export function matchModules(modules, { components = [], labels = [], text = '' } = {}) {
|
|
57
|
+
const tags = new Set([...components, ...labels].map((x) => String(x).trim().toLowerCase()).filter(Boolean));
|
|
58
|
+
const body = String(text ?? '');
|
|
59
|
+
return modules.filter((m) => {
|
|
60
|
+
const own = [...(m.components ?? []), m.path.split('/').pop()].map((x) => String(x).toLowerCase()).filter(Boolean);
|
|
61
|
+
if (own.some((x) => tags.has(x))) return true;
|
|
62
|
+
// The folder by its path, not its last name alone: "api" in a sentence says little.
|
|
63
|
+
return m.path.includes('/') && new RegExp(`(^|[\\s\`'"(])${m.path.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(/|[\\s\`'".,)]|$)`).test(body);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* A case's numbers, checked: { name, unit, before, after, better, change, note }. `change` is how
|
|
69
|
+
* much worse it got in percent (negative is faster), whichever way is better for the unit.
|
|
70
|
+
*/
|
|
71
|
+
export function normalizeCase(c) {
|
|
72
|
+
if (!c || typeof c !== 'object') return null;
|
|
73
|
+
const before = Number(c.before);
|
|
74
|
+
const after = Number(c.after);
|
|
75
|
+
const name = clip(c.name, 120);
|
|
76
|
+
if (!name || !Number.isFinite(after) || after < 0) return null;
|
|
77
|
+
const unit = clip(c.unit, 20) || null;
|
|
78
|
+
// Throughput (ops/s, MB/s) is better higher; time is better lower.
|
|
79
|
+
const better = c.better === 'higher' || c.better === 'lower' ? c.better : /\/s(ec)?$|ops|per sec|throughput/i.test(unit ?? '') ? 'higher' : 'lower';
|
|
80
|
+
const has = Number.isFinite(before) && before > 0;
|
|
81
|
+
const change = has ? Math.round(((better === 'lower' ? after / before : before / after) - 1) * 1000) / 10 : null;
|
|
82
|
+
return { name, unit, before: has ? before : null, after, better, change, ...(c.note ? { note: clip(c.note, 300) } : {}) };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** An agent's BENCH.json, checked: { runs: [{ module, command, cases }], env } (null when missing or unreadable). */
|
|
86
|
+
export function readResults(agentId) {
|
|
87
|
+
let raw;
|
|
88
|
+
try {
|
|
89
|
+
raw = JSON.parse(fs.readFileSync(resultsPath(agentId), 'utf8'));
|
|
90
|
+
} catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
return normalizeResults(raw);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function normalizeResults(raw) {
|
|
97
|
+
// One module's results on their own, or a list of them.
|
|
98
|
+
const list = Array.isArray(raw?.modules) ? raw.modules : Array.isArray(raw) ? raw : raw && typeof raw === 'object' ? [raw] : [];
|
|
99
|
+
const runs = [];
|
|
100
|
+
for (const r of list) {
|
|
101
|
+
const cases = (Array.isArray(r?.cases) ? r.cases : []).map(normalizeCase).filter(Boolean).slice(0, MAX_CASES);
|
|
102
|
+
if (!cases.length) continue;
|
|
103
|
+
let module = '';
|
|
104
|
+
try {
|
|
105
|
+
module = cleanPath(r.module ?? '');
|
|
106
|
+
} catch {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
runs.push({ module, command: clip(r.command, 300) || null, cases });
|
|
110
|
+
}
|
|
111
|
+
return runs.length ? { runs, env: clip(raw?.env ?? list[0]?.env, 300) || null } : null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The agent's instructions for the modules it benchmarks: [{ path, dir, existing }]. */
|
|
115
|
+
export function moduleBenchPrompt(file, json, modules) {
|
|
116
|
+
const list = modules
|
|
117
|
+
.map((m) => `- ${m.path || '(the whole repository)'}: the benchmarks are kept in ${m.dir}${m.existing ? ' (earlier issues left benchmarks there: read its README.md and run them as they are, then add cases for your change)' : ' (none yet: start them there, with a README.md that says what each measures and the one command that runs them)'}`)
|
|
118
|
+
.join('\n');
|
|
119
|
+
return (
|
|
120
|
+
'Measure the speed of these modules with micro benchmarks, once your change works (tests pass):\n' +
|
|
121
|
+
`${list}\n` +
|
|
122
|
+
"Each module's benchmarks are kept outside the repository in the folder named, and reused from one issue to the next, " +
|
|
123
|
+
'so the numbers can be compared: keep the existing cases and their names as they are (fix one only if your change breaks it, and say so), ' +
|
|
124
|
+
'and add cases for the code you changed, with realistic inputs at two or three sizes. Never copy them into the repository or commit them. ' +
|
|
125
|
+
'Warm up, run enough iterations for stable numbers, and take the median. Run every case on the base commit ' +
|
|
126
|
+
'(check it out in a temporary git worktree) and on your change, back to back on this machine. ' +
|
|
127
|
+
`Write the results to ${file} (Markdown: what was measured and the command, a table case | before | after | change, the environment, notes), and the numbers to ${json} as JSON:\n` +
|
|
128
|
+
'{"modules": [{"module": "<folder as listed>", "command": "<how to run them>", "cases": [{"name": "<case>", "unit": "ns/op", "before": 1234, "after": 1100, "note": "<only when it got slower>"}]}], "env": "<CPU, OS, runtime>"}\n' +
|
|
129
|
+
`A case more than ${SLOWER_PCT}% slower needs a note on why, or a fix. If you open a pull request, put the table in its description. ` +
|
|
130
|
+
'Only real measurements: never estimate a number. Run them again if you change the code afterwards.'
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** What the agent is told when it says it's done but changed a module without measuring it. */
|
|
135
|
+
export function askMessage(file, json, modules) {
|
|
136
|
+
return `Before this is done: your change touches ${modules.map((m) => m.path || 'the repository').join(', ')}, where speed matters, and there are no benchmark results for it yet. ${moduleBenchPrompt(file, json, modules)} Then mark the brief done again.`;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export class ModuleBenchmarks {
|
|
140
|
+
/**
|
|
141
|
+
* `changedFiles(agent)` gives the files an agent's branch changed (repository paths), and
|
|
142
|
+
* `read(agentId)` its results (both for tests); `home` is where the benchmarks are kept.
|
|
143
|
+
*/
|
|
144
|
+
constructor({ manager, store, changedFiles = async () => [], read = readResults, home = paths.home, log = () => {} }) {
|
|
145
|
+
this.manager = manager;
|
|
146
|
+
this.store = store;
|
|
147
|
+
this.changedFiles = changedFiles;
|
|
148
|
+
this.read = read;
|
|
149
|
+
this.home = home;
|
|
150
|
+
this.log = log;
|
|
151
|
+
this.onFinished = (agent) => {
|
|
152
|
+
if (agent) this.finished(agent.id).catch((e) => this.log(`benchmarks: ${e.message}`));
|
|
153
|
+
};
|
|
154
|
+
manager.on('finished', this.onFinished);
|
|
155
|
+
// The manager asks which modules an agent benchmarks, and how to tell it (manager.js).
|
|
156
|
+
manager.benchModules = this;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
close() {
|
|
160
|
+
this.manager.off('finished', this.onFinished);
|
|
161
|
+
if (this.manager.benchModules === this) this.manager.benchModules = null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
get modules() {
|
|
165
|
+
return (this.store.data.benchModules ??= []);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
get runs() {
|
|
169
|
+
return (this.store.data.benchRuns ??= []);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Mark a module of a project for benchmarks, or change its components: { project, path, components }. */
|
|
173
|
+
watch({ project, path: p, components } = {}) {
|
|
174
|
+
const key = String(project ?? '').trim();
|
|
175
|
+
if (!key) throw err(400, 'Which project?');
|
|
176
|
+
const folder = cleanPath(p ?? '');
|
|
177
|
+
let m = this.modules.find((x) => x.project === key && x.path === folder);
|
|
178
|
+
if (!m) this.modules.push((m = { id: crypto.randomBytes(3).toString('hex'), project: key, path: folder, components: [], addedAt: now() }));
|
|
179
|
+
if (components !== undefined) m.components = names(components);
|
|
180
|
+
this.store.save();
|
|
181
|
+
return this.view(m);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
unwatch(id) {
|
|
185
|
+
const i = this.modules.findIndex((x) => x.id === id);
|
|
186
|
+
if (i < 0) throw err(404, 'No such module');
|
|
187
|
+
this.modules.splice(i, 1);
|
|
188
|
+
this.store.save();
|
|
189
|
+
return { ok: true };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
view(m) {
|
|
193
|
+
const dir = moduleDir(m.project, m.path, this.home);
|
|
194
|
+
return { ...m, dir, existing: fs.existsSync(dir) && fs.readdirSync(dir).length > 0 };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** The watched modules of a project (by key). */
|
|
198
|
+
of(projectKey) {
|
|
199
|
+
return this.modules.filter((m) => m.project === projectKey);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** An agent this applies to: a task with a Jira issue, not a hunter, fixer, reviewer or team member. */
|
|
203
|
+
eligible(agent) {
|
|
204
|
+
return Boolean(agent && agent.kind === 'task' && !agent.hunting && !agent.fixing && !agent.reviewing && !agent.team && !agent.builds && this.manager.issueKey(agent));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** The modules an agent should benchmark from its issue: { components, labels } and its request. */
|
|
208
|
+
forIssue(agent, fields = {}) {
|
|
209
|
+
if (!this.eligible(agent)) return [];
|
|
210
|
+
const key = this.manager.projectInfo(agent)?.key;
|
|
211
|
+
return matchModules(this.of(key), { ...fields, text: agent.intent }).map((m) => this.view(m));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** The modules an agent was given to benchmark, as it should be told them. */
|
|
215
|
+
forAgent(agent) {
|
|
216
|
+
const key = this.manager.projectInfo(agent)?.key;
|
|
217
|
+
return (agent.benchModules ?? []).map((p) => this.view(this.of(key).find((m) => m.path === p) ?? { project: key, path: p }));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** It says it's done: keep its numbers, or ask for them when its change touched a watched module. */
|
|
221
|
+
async finished(agentId) {
|
|
222
|
+
const agent = this.store.get(agentId);
|
|
223
|
+
if (!agent) return null;
|
|
224
|
+
const recorded = this.record(agent);
|
|
225
|
+
if (!this.eligible(agent)) return { recorded, asked: [] };
|
|
226
|
+
const key = this.manager.projectInfo(agent)?.key;
|
|
227
|
+
const watched = this.of(key);
|
|
228
|
+
if (!watched.length) return { recorded, asked: [] };
|
|
229
|
+
const changed = await this.changedFiles(agent).catch(() => []);
|
|
230
|
+
const measured = new Set((this.read(agent.id)?.runs ?? []).map((r) => r.module));
|
|
231
|
+
const asked = new Set(agent.benchAsked ?? []);
|
|
232
|
+
const missing = watched.filter((m) => changed.some((f) => inModule(f, m.path)) && !measured.has(m.path) && !asked.has(m.path)).map((m) => this.view(m));
|
|
233
|
+
if (!missing.length) return { recorded, asked: [] };
|
|
234
|
+
// Asked once per module: a second "done" without numbers stands.
|
|
235
|
+
this.manager.addBenchModules(agent.id, missing, { asked: true });
|
|
236
|
+
return { recorded, asked: missing.map((m) => m.path) };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Keep an agent's numbers, one run per module (a new run of the same agent replaces its last). */
|
|
240
|
+
record(agent) {
|
|
241
|
+
const res = this.read(agent.id);
|
|
242
|
+
if (!res) return 0;
|
|
243
|
+
const projectKey = this.manager.projectInfo(agent)?.key ?? null;
|
|
244
|
+
const issue = this.manager.issueKey(agent);
|
|
245
|
+
let n = 0;
|
|
246
|
+
for (const r of res.runs) {
|
|
247
|
+
const at = this.runs.findIndex((x) => x.agentId === agent.id && x.module === r.module);
|
|
248
|
+
const prev = at >= 0 ? this.runs[at] : null;
|
|
249
|
+
const worst = r.cases.reduce((w, c) => (c.change !== null && (w === null || c.change > w) ? c.change : w), null);
|
|
250
|
+
const run = {
|
|
251
|
+
id: prev?.id ?? crypto.randomBytes(3).toString('hex'),
|
|
252
|
+
agentId: agent.id,
|
|
253
|
+
agent: agent.name,
|
|
254
|
+
issue,
|
|
255
|
+
project: projectKey,
|
|
256
|
+
module: r.module,
|
|
257
|
+
command: r.command,
|
|
258
|
+
env: res.env,
|
|
259
|
+
cases: r.cases,
|
|
260
|
+
worst,
|
|
261
|
+
at: now(),
|
|
262
|
+
...(prev?.accepted ? { accepted: prev.accepted } : {}),
|
|
263
|
+
};
|
|
264
|
+
if (prev && JSON.stringify(prev.cases) === JSON.stringify(run.cases)) continue;
|
|
265
|
+
if (at >= 0) this.runs.splice(at, 1);
|
|
266
|
+
this.runs.unshift(run);
|
|
267
|
+
n++;
|
|
268
|
+
}
|
|
269
|
+
if (this.runs.length > MAX_RUNS) this.runs.length = MAX_RUNS;
|
|
270
|
+
if (n) this.store.save();
|
|
271
|
+
return n;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** Slower than it should be, and nobody has said it's fine. */
|
|
275
|
+
slower() {
|
|
276
|
+
return this.runs.filter((r) => r.worst !== null && r.worst > SLOWER_PCT && !r.accepted);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** "It's fine" for a slower run (Undo: `accepted` false). */
|
|
280
|
+
accept(runId, accepted = true) {
|
|
281
|
+
const r = this.runs.find((x) => x.id === runId);
|
|
282
|
+
if (!r) throw err(404, 'No such benchmark run');
|
|
283
|
+
if (accepted) r.accepted = now();
|
|
284
|
+
else delete r.accepted;
|
|
285
|
+
this.store.save();
|
|
286
|
+
return r;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** Ask the agent that made it slower to fix it, or explain. */
|
|
290
|
+
askToFix(runId) {
|
|
291
|
+
const r = this.runs.find((x) => x.id === runId);
|
|
292
|
+
if (!r) throw err(404, 'No such benchmark run');
|
|
293
|
+
const cases = r.cases.filter((c) => c.change !== null && c.change > SLOWER_PCT);
|
|
294
|
+
const lines = cases.map((c) => `- ${c.name}: ${c.before} → ${c.after}${c.unit ? ` ${c.unit}` : ''} (${c.change}% slower)`).join('\n');
|
|
295
|
+
this.manager.tell(r.agentId, `Your change made ${r.module || 'the repository'} slower in the micro benchmarks:\n${lines}\nFind out why and make it at least as fast as before, or, if it can't be, say why in the benchmark notes. Then run the benchmarks again and update the results.`, { source: 'benchmarks' });
|
|
296
|
+
return { ok: true };
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** For the Map's Speed view: the project's watched modules, each with its issues' runs, newest first. */
|
|
300
|
+
speed(projectKey) {
|
|
301
|
+
const runs = this.runs.filter((r) => r.project === projectKey);
|
|
302
|
+
return {
|
|
303
|
+
modules: this.of(projectKey).map((m) => ({ ...this.view(m), runs: runs.filter((r) => r.module === m.path).slice(0, 20) })),
|
|
304
|
+
// Measured in modules nobody marked (an agent with benchmarks on).
|
|
305
|
+
other: runs.filter((r) => !this.of(projectKey).some((m) => m.path === r.module)).slice(0, 20),
|
|
306
|
+
slowerPct: SLOWER_PCT,
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
}
|