ai-engineering-loop 1.10.0 → 1.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agents/devil-advocate.md +13 -1
- package/.agents/judge.md +10 -0
- package/.agents/skills/backend-development/SKILL.md +272 -0
- package/.agents/skills/backend-development/references/api-design-guidelines.md +95 -0
- package/.agents/skills/backend-development/references/query-and-database-best-practices.md +53 -0
- package/.agents/skills/backend-development/references/security-checklist.md +60 -0
- package/.agents/workflows/ai-engineering-loop.md +4 -4
- package/.claude/agents/devil-advocate.md +13 -1
- package/.claude/agents/judge.md +10 -0
- package/.claude/skills/ai-engineering-loop/SKILL.md +4 -4
- package/.claude/skills/backend-development/SKILL.md +272 -0
- package/.claude/skills/backend-development/references/api-design-guidelines.md +95 -0
- package/.claude/skills/backend-development/references/query-and-database-best-practices.md +53 -0
- package/.claude/skills/backend-development/references/security-checklist.md +60 -0
- package/.gemini/skills/ai-engineering-loop/SKILL.md +4 -4
- package/.gemini/skills/backend-development/SKILL.md +272 -0
- package/.gemini/skills/backend-development/references/api-design-guidelines.md +95 -0
- package/.gemini/skills/backend-development/references/query-and-database-best-practices.md +53 -0
- package/.gemini/skills/backend-development/references/security-checklist.md +60 -0
- package/.grok/agents/devil-advocate.md +13 -1
- package/.grok/agents/judge.md +10 -0
- package/.grok/skills/ai-engineering-loop/SKILL.md +4 -4
- package/.grok/skills/backend-development/SKILL.md +272 -0
- package/.grok/skills/backend-development/references/api-design-guidelines.md +95 -0
- package/.grok/skills/backend-development/references/query-and-database-best-practices.md +53 -0
- package/.grok/skills/backend-development/references/security-checklist.md +60 -0
- package/README.md +33 -1
- package/README.npm.md +8 -1
- package/agents/shared/devil-advocate.body.md +13 -1
- package/agents/shared/judge.body.md +10 -0
- package/bin/ai-engineering-loop.js +207 -2
- package/core/runtime-safety.md +43 -0
- package/lib/budget.js +35 -0
- package/lib/doctor.js +4 -0
- package/lib/run-compaction.js +170 -0
- package/lib/runtime-policy.js +24 -1
- package/lib/safe-context.js +32 -7
- package/lib/smart-context.js +643 -0
- package/lib/studio-server.js +19 -2
- package/lib/sync-hosts.js +20 -0
- package/lib/update-manager.js +147 -0
- package/lib/verification-summary.js +130 -0
- package/package.json +1 -1
- package/policies/review-budget.md +12 -0
- package/studio/app.js +5 -2
|
@@ -0,0 +1,643 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const { execFileSync } = require('child_process');
|
|
7
|
+
const { atomicWriteJson, getCurrentRun, loadRun, runsDir } = require('./run-state.js');
|
|
8
|
+
const { assertBudgetAvailable } = require('./budget.js');
|
|
9
|
+
const { ensurePrivateRuntimeIgnores } = require('./runtime-files.js');
|
|
10
|
+
const {
|
|
11
|
+
CONTEXT_PROFILE_LIMITS,
|
|
12
|
+
estimateTokens,
|
|
13
|
+
isSensitivePath,
|
|
14
|
+
redactSecrets,
|
|
15
|
+
resolveSafeRepoFile
|
|
16
|
+
} = require('./safe-context.js');
|
|
17
|
+
const { loadPolicy, normalizeReviewProfile } = require('./runtime-policy.js');
|
|
18
|
+
|
|
19
|
+
const DIFF_PACK_LIMITS = Object.freeze({
|
|
20
|
+
lean: Object.freeze({ maxFiles: 20, maxHunksPerFile: 8, maxHunkBytes: 4_000, maxTotalBytes: 32_000 }),
|
|
21
|
+
standard: Object.freeze({ maxFiles: 35, maxHunksPerFile: 12, maxHunkBytes: 8_000, maxTotalBytes: 64_000 }),
|
|
22
|
+
thorough: Object.freeze({ maxFiles: 60, maxHunksPerFile: 20, maxHunkBytes: 12_000, maxTotalBytes: 128_000 })
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const REVIEW_BUDGETS = Object.freeze({
|
|
26
|
+
lean: Object.freeze({ devilAdvocateToolCalls: 8, devilAdvocateMaxFindings: 5, judgeToolCalls: 4, judgeMayReadFullDiff: false }),
|
|
27
|
+
standard: Object.freeze({ devilAdvocateToolCalls: 10, devilAdvocateMaxFindings: 8, judgeToolCalls: 4, judgeMayReadFullDiff: false }),
|
|
28
|
+
thorough: Object.freeze({ devilAdvocateToolCalls: 14, devilAdvocateMaxFindings: 12, judgeToolCalls: 6, judgeMayReadFullDiff: true })
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const SMALL_TASK_LIMITS = Object.freeze({
|
|
32
|
+
maxFiles: 3,
|
|
33
|
+
maxChangedLines: 180
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const SMALL_TASK_HIGH_RISK_PATTERNS = Object.freeze([
|
|
37
|
+
/(^|\/)(auth|authorization|permissions?|access-control)(\/|[._-])/i,
|
|
38
|
+
/(^|\/)(payments?|billing|invoices?|ledger)(\/|[._-])/i,
|
|
39
|
+
/(^|\/)(migrations?|schema)(\/|[._-])/i,
|
|
40
|
+
/(^|\/)(infra|terraform|k8s|kubernetes|deploy)(\/|[._-])/i,
|
|
41
|
+
/(^|\/)(crypto|encryption|secrets?)(\/|[._-])/i,
|
|
42
|
+
/(^|\/)(package-lock|pnpm-lock|yarn\.lock)$/i
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
const DEFAULT_CONTEXT_IGNORE_PATTERNS = Object.freeze([
|
|
46
|
+
'*.css',
|
|
47
|
+
'*.map',
|
|
48
|
+
'*.min.js',
|
|
49
|
+
'package-lock.json',
|
|
50
|
+
'pnpm-lock.yaml',
|
|
51
|
+
'yarn.lock',
|
|
52
|
+
'bun.lockb',
|
|
53
|
+
'dist/',
|
|
54
|
+
'build/',
|
|
55
|
+
'coverage/',
|
|
56
|
+
'node_modules/',
|
|
57
|
+
'studio/styles.css',
|
|
58
|
+
'studio/assets/',
|
|
59
|
+
'docs/images/',
|
|
60
|
+
'.ai-engineering-loop/'
|
|
61
|
+
]);
|
|
62
|
+
|
|
63
|
+
function smartContextError(message, code = 'SMART_CONTEXT_ERROR') {
|
|
64
|
+
const err = new Error(message);
|
|
65
|
+
err.code = code;
|
|
66
|
+
return err;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function sha256(content) {
|
|
70
|
+
return crypto.createHash('sha256').update(content).digest('hex');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function contextRoot(rootDir) {
|
|
74
|
+
return path.join(rootDir, '.ai-engineering-loop', 'context');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function summaryCacheDir(rootDir) {
|
|
78
|
+
return path.join(contextRoot(rootDir), 'files');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function git(rootDir, args) {
|
|
82
|
+
return execFileSync('git', args, {
|
|
83
|
+
cwd: rootDir,
|
|
84
|
+
encoding: 'utf8',
|
|
85
|
+
stdio: ['ignore', 'pipe', 'pipe']
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function normalizeRepoPath(relativePath) {
|
|
90
|
+
return String(relativePath || '').replace(/\\/g, '/').replace(/^\.\/+/, '');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function escapeRegExp(value) {
|
|
94
|
+
return String(value).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function globToRegExp(pattern) {
|
|
98
|
+
const parts = String(pattern).split('*').map(escapeRegExp);
|
|
99
|
+
return new RegExp(`^${parts.join('.*')}$`);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function readContextIgnorePatterns(rootDir) {
|
|
103
|
+
const ignorePath = path.join(rootDir, '.aelcontextignore');
|
|
104
|
+
if (!fs.existsSync(ignorePath)) return [];
|
|
105
|
+
return fs.readFileSync(ignorePath, 'utf8')
|
|
106
|
+
.split('\n')
|
|
107
|
+
.map((line) => line.trim())
|
|
108
|
+
.filter((line) => line && !line.startsWith('#'));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function matchesContextIgnorePattern(relativePath, pattern) {
|
|
112
|
+
const candidate = normalizeRepoPath(relativePath);
|
|
113
|
+
const normalizedPattern = normalizeRepoPath(pattern);
|
|
114
|
+
if (!normalizedPattern) return false;
|
|
115
|
+
if (normalizedPattern.endsWith('/')) {
|
|
116
|
+
return candidate === normalizedPattern.slice(0, -1) || candidate.startsWith(normalizedPattern);
|
|
117
|
+
}
|
|
118
|
+
if (!normalizedPattern.includes('/')) {
|
|
119
|
+
const basename = path.posix.basename(candidate);
|
|
120
|
+
return normalizedPattern.includes('*')
|
|
121
|
+
? globToRegExp(normalizedPattern).test(basename)
|
|
122
|
+
: basename === normalizedPattern;
|
|
123
|
+
}
|
|
124
|
+
return normalizedPattern.includes('*')
|
|
125
|
+
? globToRegExp(normalizedPattern).test(candidate)
|
|
126
|
+
: candidate === normalizedPattern;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function contextIgnorePatterns(rootDir) {
|
|
130
|
+
return [...DEFAULT_CONTEXT_IGNORE_PATTERNS, ...readContextIgnorePatterns(rootDir)];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function isReviewContextIgnored(rootDir, relativePath, { includeIgnored = false } = {}) {
|
|
134
|
+
if (includeIgnored) return false;
|
|
135
|
+
const candidate = normalizeRepoPath(relativePath);
|
|
136
|
+
return contextIgnorePatterns(rootDir).some((pattern) => matchesContextIgnorePattern(candidate, pattern));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function listTrackedFiles(rootDir, { includeIgnored = false } = {}) {
|
|
140
|
+
return git(rootDir, ['ls-files'])
|
|
141
|
+
.split('\n')
|
|
142
|
+
.map((line) => line.trim())
|
|
143
|
+
.filter(Boolean)
|
|
144
|
+
.filter((relativePath) => !isSensitivePath(relativePath))
|
|
145
|
+
.filter((relativePath) => !isReviewContextIgnored(rootDir, relativePath, { includeIgnored }));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function languageFor(relativePath) {
|
|
149
|
+
const extension = path.extname(relativePath).replace(/^\./, '').toLowerCase();
|
|
150
|
+
return extension || 'text';
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function extractSymbols(relativePath, text) {
|
|
154
|
+
const symbols = [];
|
|
155
|
+
const patterns = [
|
|
156
|
+
/\bfunction\s+([A-Za-z_$][\w$]*)\s*\(/g,
|
|
157
|
+
/\bclass\s+([A-Za-z_$][\w$]*)\b/g,
|
|
158
|
+
/\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=/g,
|
|
159
|
+
/\bexports\.([A-Za-z_$][\w$]*)\s*=/g,
|
|
160
|
+
/\bmodule\.exports\.([A-Za-z_$][\w$]*)\s*=/g,
|
|
161
|
+
/^#{1,6}\s+(.+)$/gm
|
|
162
|
+
];
|
|
163
|
+
for (const pattern of patterns) {
|
|
164
|
+
for (const match of text.matchAll(pattern)) {
|
|
165
|
+
const symbol = String(match[1] || '').trim();
|
|
166
|
+
if (symbol && !symbols.includes(symbol)) symbols.push(symbol);
|
|
167
|
+
if (symbols.length >= 20) return symbols;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (symbols.length === 0 && ['json', 'md', 'txt'].includes(languageFor(relativePath))) {
|
|
171
|
+
const firstLine = text.split('\n').find((line) => line.trim());
|
|
172
|
+
if (firstLine) symbols.push(firstLine.trim().slice(0, 80));
|
|
173
|
+
}
|
|
174
|
+
return symbols;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function summarizeFile(rootDir, relativePath) {
|
|
178
|
+
const safe = resolveSafeRepoFile(rootDir, relativePath);
|
|
179
|
+
const raw = fs.readFileSync(safe.absolute);
|
|
180
|
+
const sourceSha = sha256(raw);
|
|
181
|
+
const cached = readCachedSummary(rootDir, sourceSha);
|
|
182
|
+
if (cached) return { ...cached, path: safe.relative, cacheHit: true };
|
|
183
|
+
if (raw.includes(0)) {
|
|
184
|
+
const summary = {
|
|
185
|
+
path: safe.relative,
|
|
186
|
+
binary: true,
|
|
187
|
+
sourceSha256: sourceSha,
|
|
188
|
+
sourceBytes: raw.length,
|
|
189
|
+
language: languageFor(safe.relative),
|
|
190
|
+
lines: null,
|
|
191
|
+
symbols: []
|
|
192
|
+
};
|
|
193
|
+
writeCachedSummary(rootDir, summary);
|
|
194
|
+
return { ...summary, cacheHit: false };
|
|
195
|
+
}
|
|
196
|
+
const text = raw.toString('utf8');
|
|
197
|
+
const summary = {
|
|
198
|
+
path: safe.relative,
|
|
199
|
+
binary: false,
|
|
200
|
+
sourceSha256: sourceSha,
|
|
201
|
+
sourceBytes: raw.length,
|
|
202
|
+
language: languageFor(safe.relative),
|
|
203
|
+
lines: text.length === 0 ? 0 : text.split('\n').length,
|
|
204
|
+
symbols: extractSymbols(safe.relative, text)
|
|
205
|
+
};
|
|
206
|
+
writeCachedSummary(rootDir, summary);
|
|
207
|
+
return { ...summary, cacheHit: false };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function readCachedSummary(rootDir, sourceSha) {
|
|
211
|
+
const filePath = path.join(summaryCacheDir(rootDir), `${sourceSha}.json`);
|
|
212
|
+
if (!fs.existsSync(filePath)) return null;
|
|
213
|
+
try {
|
|
214
|
+
const cached = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
215
|
+
if (cached.sourceSha256 !== sourceSha) return null;
|
|
216
|
+
return cached;
|
|
217
|
+
} catch {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function writeCachedSummary(rootDir, summary) {
|
|
223
|
+
const filePath = path.join(summaryCacheDir(rootDir), `${summary.sourceSha256}.json`);
|
|
224
|
+
atomicWriteJson(filePath, {
|
|
225
|
+
path: summary.path,
|
|
226
|
+
binary: summary.binary,
|
|
227
|
+
sourceSha256: summary.sourceSha256,
|
|
228
|
+
sourceBytes: summary.sourceBytes,
|
|
229
|
+
language: summary.language,
|
|
230
|
+
lines: summary.lines,
|
|
231
|
+
symbols: summary.symbols
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function buildContextIndex(rootDir, { files, includeIgnored = false } = {}) {
|
|
236
|
+
ensurePrivateRuntimeIgnores(rootDir);
|
|
237
|
+
const hasExplicitFiles = files && files.length;
|
|
238
|
+
const candidates = (hasExplicitFiles ? files : listTrackedFiles(rootDir, { includeIgnored }))
|
|
239
|
+
.map((file) => String(file).trim())
|
|
240
|
+
.filter(Boolean)
|
|
241
|
+
.filter((file) => hasExplicitFiles || !isReviewContextIgnored(rootDir, file, { includeIgnored }));
|
|
242
|
+
const summaries = [];
|
|
243
|
+
const cache = { hits: 0, misses: 0 };
|
|
244
|
+
for (const candidate of [...new Set(candidates)]) {
|
|
245
|
+
try {
|
|
246
|
+
const summary = summarizeFile(rootDir, candidate);
|
|
247
|
+
if (summary.cacheHit) cache.hits += 1;
|
|
248
|
+
else cache.misses += 1;
|
|
249
|
+
const { cacheHit: _cacheHit, ...publicSummary } = summary;
|
|
250
|
+
summaries.push(publicSummary);
|
|
251
|
+
} catch (cause) {
|
|
252
|
+
if (cause.code === 'CONTEXT_FILE_MISSING') continue;
|
|
253
|
+
throw cause;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
const index = {
|
|
257
|
+
schemaVersion: 1,
|
|
258
|
+
createdAt: new Date().toISOString(),
|
|
259
|
+
cache,
|
|
260
|
+
files: summaries.sort((left, right) => left.path.localeCompare(right.path))
|
|
261
|
+
};
|
|
262
|
+
const indexPath = path.join(contextRoot(rootDir), 'index.json');
|
|
263
|
+
atomicWriteJson(indexPath, index);
|
|
264
|
+
return {
|
|
265
|
+
index,
|
|
266
|
+
path: path.relative(rootDir, indexPath).split(path.sep).join('/')
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function readOrBuildContextIndex(rootDir) {
|
|
271
|
+
const indexPath = path.join(contextRoot(rootDir), 'index.json');
|
|
272
|
+
if (fs.existsSync(indexPath)) {
|
|
273
|
+
try {
|
|
274
|
+
const index = JSON.parse(fs.readFileSync(indexPath, 'utf8'));
|
|
275
|
+
if (index?.schemaVersion === 1 && Array.isArray(index.files)) {
|
|
276
|
+
return {
|
|
277
|
+
index,
|
|
278
|
+
path: path.relative(rootDir, indexPath).split(path.sep).join('/'),
|
|
279
|
+
cacheStatus: 'existing'
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
} catch {
|
|
283
|
+
// Optional cache corruption should not make metadata-only context queries fail.
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return { ...buildContextIndex(rootDir), cacheStatus: 'rebuilt' };
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function queryContextIndex(rootDir, { query, limit = 10 } = {}) {
|
|
290
|
+
const needle = String(query || '').trim().toLowerCase();
|
|
291
|
+
if (!needle) throw smartContextError('Context query requires a non-empty search term', 'EMPTY_CONTEXT_QUERY');
|
|
292
|
+
const result = readOrBuildContextIndex(rootDir);
|
|
293
|
+
const matches = result.index.files
|
|
294
|
+
.map((file) => {
|
|
295
|
+
const pathScore = file.path.toLowerCase().includes(needle) ? 2 : 0;
|
|
296
|
+
const matchedSymbols = (file.symbols || []).filter((symbol) => String(symbol).toLowerCase().includes(needle));
|
|
297
|
+
const symbolScore = matchedSymbols.length ? 3 : 0;
|
|
298
|
+
return {
|
|
299
|
+
path: file.path,
|
|
300
|
+
language: file.language,
|
|
301
|
+
lines: file.lines,
|
|
302
|
+
sourceBytes: file.sourceBytes,
|
|
303
|
+
symbols: matchedSymbols.slice(0, 8),
|
|
304
|
+
score: pathScore + symbolScore
|
|
305
|
+
};
|
|
306
|
+
})
|
|
307
|
+
.filter((match) => match.score > 0)
|
|
308
|
+
.sort((left, right) => right.score - left.score || left.path.localeCompare(right.path))
|
|
309
|
+
.slice(0, Number(limit) || 10)
|
|
310
|
+
.map(({ score: _score, ...match }) => match);
|
|
311
|
+
return {
|
|
312
|
+
schemaVersion: 1,
|
|
313
|
+
query: needle,
|
|
314
|
+
indexPath: result.path,
|
|
315
|
+
cacheStatus: result.cacheStatus,
|
|
316
|
+
matches
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function pathTokens(relativePath) {
|
|
321
|
+
return new Set(String(relativePath || '')
|
|
322
|
+
.toLowerCase()
|
|
323
|
+
.split(/[^a-z0-9]+/)
|
|
324
|
+
.filter((token) => token.length >= 3));
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function relatedContextIndex(rootDir, { file, limit = 10 } = {}) {
|
|
328
|
+
const target = String(file || '').trim();
|
|
329
|
+
if (!target) throw smartContextError('Related context requires a repository-relative file path', 'EMPTY_RELATED_FILE');
|
|
330
|
+
const result = readOrBuildContextIndex(rootDir);
|
|
331
|
+
const source = result.index.files.find((entry) => entry.path === target);
|
|
332
|
+
if (!source) throw smartContextError(`File is not present in the context index: ${target}`, 'FILE_NOT_INDEXED');
|
|
333
|
+
const sourceTokens = pathTokens(source.path);
|
|
334
|
+
const sourceSymbols = new Set((source.symbols || []).map((symbol) => String(symbol).toLowerCase()));
|
|
335
|
+
const matches = result.index.files
|
|
336
|
+
.filter((entry) => entry.path !== source.path)
|
|
337
|
+
.map((entry) => {
|
|
338
|
+
const tokens = pathTokens(entry.path);
|
|
339
|
+
const tokenOverlap = [...tokens].filter((token) => sourceTokens.has(token)).length;
|
|
340
|
+
const symbolOverlap = (entry.symbols || []).filter((symbol) => sourceSymbols.has(String(symbol).toLowerCase())).length;
|
|
341
|
+
const sameLanguage = entry.language === source.language ? 1 : 0;
|
|
342
|
+
return {
|
|
343
|
+
path: entry.path,
|
|
344
|
+
language: entry.language,
|
|
345
|
+
lines: entry.lines,
|
|
346
|
+
sourceBytes: entry.sourceBytes,
|
|
347
|
+
symbols: (entry.symbols || []).slice(0, 8),
|
|
348
|
+
score: tokenOverlap * 3 + symbolOverlap * 2 + sameLanguage
|
|
349
|
+
};
|
|
350
|
+
})
|
|
351
|
+
.filter((match) => match.score > 0)
|
|
352
|
+
.sort((left, right) => right.score - left.score || left.path.localeCompare(right.path))
|
|
353
|
+
.slice(0, Number(limit) || 10)
|
|
354
|
+
.map(({ score: _score, ...match }) => match);
|
|
355
|
+
return {
|
|
356
|
+
schemaVersion: 1,
|
|
357
|
+
file: source.path,
|
|
358
|
+
indexPath: result.path,
|
|
359
|
+
cacheStatus: result.cacheStatus,
|
|
360
|
+
matches
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function parseDiff(diff) {
|
|
365
|
+
const files = [];
|
|
366
|
+
let currentFile = null;
|
|
367
|
+
let currentHunk = null;
|
|
368
|
+
for (const line of String(diff || '').split('\n')) {
|
|
369
|
+
const fileMatch = line.match(/^diff --git a\/(.+) b\/(.+)$/);
|
|
370
|
+
if (fileMatch) {
|
|
371
|
+
currentFile = {
|
|
372
|
+
path: fileMatch[2],
|
|
373
|
+
oldPath: fileMatch[1],
|
|
374
|
+
hunks: []
|
|
375
|
+
};
|
|
376
|
+
files.push(currentFile);
|
|
377
|
+
currentHunk = null;
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
if (!currentFile) continue;
|
|
381
|
+
if (line.startsWith('@@ ')) {
|
|
382
|
+
currentHunk = { header: line, lines: [line] };
|
|
383
|
+
currentFile.hunks.push(currentHunk);
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
if (currentHunk) currentHunk.lines.push(line);
|
|
387
|
+
}
|
|
388
|
+
return files.filter((file) => file.hunks.length > 0);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function compactFinding(finding) {
|
|
392
|
+
return {
|
|
393
|
+
id: finding.id,
|
|
394
|
+
axis: finding.axis,
|
|
395
|
+
severity: finding.severity,
|
|
396
|
+
validity: finding.validity,
|
|
397
|
+
disposition: finding.disposition,
|
|
398
|
+
location: finding.location,
|
|
399
|
+
acceptanceCriteria: finding.acceptanceCriteria
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function pathFromFindingLocation(location) {
|
|
404
|
+
const value = String(location || '').split('#')[0].trim();
|
|
405
|
+
return value || null;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function unresolvedFindings(rootDir, runId) {
|
|
409
|
+
const filePath = path.join(runsDir(rootDir), runId, 'findings.json');
|
|
410
|
+
if (!fs.existsSync(filePath)) return [];
|
|
411
|
+
let parsed;
|
|
412
|
+
try {
|
|
413
|
+
parsed = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
414
|
+
} catch (cause) {
|
|
415
|
+
throw smartContextError(`Unable to read findings.json: ${cause.message}`, 'INVALID_FINDINGS');
|
|
416
|
+
}
|
|
417
|
+
return (parsed.findings || [])
|
|
418
|
+
.filter((finding) => finding.validity === 'VALID')
|
|
419
|
+
.filter((finding) => ['BLOCKER', 'HIGH'].includes(finding.severity) || finding.disposition === 'STRONG')
|
|
420
|
+
.map(compactFinding)
|
|
421
|
+
.slice(0, 20);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function resolveRun(rootDir, runId) {
|
|
425
|
+
if (runId) return loadRun(rootDir, runId);
|
|
426
|
+
const current = getCurrentRun(rootDir);
|
|
427
|
+
if (!current) throw smartContextError('No current run. Pass --run <id>.', 'NO_CURRENT_RUN');
|
|
428
|
+
return current;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function summarizeChangedFile(rootDir, relativePath) {
|
|
432
|
+
try {
|
|
433
|
+
return summarizeFile(rootDir, relativePath);
|
|
434
|
+
} catch (cause) {
|
|
435
|
+
if (cause.code === 'CONTEXT_FILE_MISSING') {
|
|
436
|
+
return {
|
|
437
|
+
path: relativePath,
|
|
438
|
+
deleted: true,
|
|
439
|
+
binary: false,
|
|
440
|
+
sourceSha256: null,
|
|
441
|
+
sourceBytes: 0,
|
|
442
|
+
language: languageFor(relativePath),
|
|
443
|
+
lines: null,
|
|
444
|
+
symbols: []
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
throw cause;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function boundedHunks(file, limits, remainingBytes) {
|
|
452
|
+
const hunks = [];
|
|
453
|
+
let includedBytes = 0;
|
|
454
|
+
let truncated = file.hunks.length > limits.maxHunksPerFile;
|
|
455
|
+
for (const hunk of file.hunks.slice(0, limits.maxHunksPerFile)) {
|
|
456
|
+
if (remainingBytes - includedBytes <= 0) {
|
|
457
|
+
truncated = true;
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
const rawContent = hunk.lines.join('\n');
|
|
461
|
+
const redacted = redactSecrets(rawContent);
|
|
462
|
+
const allowed = Math.min(Buffer.byteLength(redacted.text), limits.maxHunkBytes, remainingBytes - includedBytes);
|
|
463
|
+
const content = Buffer.from(redacted.text).subarray(0, allowed).toString('utf8');
|
|
464
|
+
if (allowed < Buffer.byteLength(redacted.text)) truncated = true;
|
|
465
|
+
includedBytes += allowed;
|
|
466
|
+
hunks.push({
|
|
467
|
+
header: hunk.header,
|
|
468
|
+
content,
|
|
469
|
+
redactions: redacted.categories,
|
|
470
|
+
truncated: allowed < Buffer.byteLength(redacted.text)
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
return { hunks, includedBytes, truncated };
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function createDiffHunkPack(rootDir, { runId, base = 'HEAD', profile, includeIgnored = false } = {}) {
|
|
477
|
+
const run = resolveRun(rootDir, runId);
|
|
478
|
+
const policy = loadPolicy(rootDir);
|
|
479
|
+
const reviewProfile = normalizeReviewProfile(profile || policy.reviewProfile);
|
|
480
|
+
const limits = DIFF_PACK_LIMITS[reviewProfile];
|
|
481
|
+
const contextLimits = CONTEXT_PROFILE_LIMITS[reviewProfile]['devil-advocate'];
|
|
482
|
+
const diff = git(rootDir, ['diff', '--no-ext-diff', '--unified=3', base, '--']);
|
|
483
|
+
const parsedDiffFiles = parseDiff(diff).filter((file) => !isSensitivePath(file.path));
|
|
484
|
+
const ignoredPaths = parsedDiffFiles
|
|
485
|
+
.filter((file) => isReviewContextIgnored(rootDir, file.path, { includeIgnored }))
|
|
486
|
+
.map((file) => file.path);
|
|
487
|
+
const allParsedFiles = parsedDiffFiles
|
|
488
|
+
.filter((file) => !isReviewContextIgnored(rootDir, file.path, { includeIgnored }));
|
|
489
|
+
const parsedFiles = allParsedFiles.slice(0, limits.maxFiles);
|
|
490
|
+
const totalFilesTruncated = allParsedFiles.length > parsedFiles.length;
|
|
491
|
+
const unresolved = unresolvedFindings(rootDir, run.runId);
|
|
492
|
+
const changedPaths = allParsedFiles.map((file) => file.path);
|
|
493
|
+
const focusPaths = [...new Set(unresolved
|
|
494
|
+
.map((finding) => pathFromFindingLocation(finding.location))
|
|
495
|
+
.filter((location) => location && changedPaths.includes(location)))];
|
|
496
|
+
const files = [];
|
|
497
|
+
let totalBytes = 0;
|
|
498
|
+
let estimatedTokens = 0;
|
|
499
|
+
for (const file of parsedFiles) {
|
|
500
|
+
const remaining = limits.maxTotalBytes - totalBytes;
|
|
501
|
+
if (remaining <= 0) break;
|
|
502
|
+
const bounded = boundedHunks(file, limits, remaining);
|
|
503
|
+
totalBytes += bounded.includedBytes;
|
|
504
|
+
estimatedTokens += estimateTokens(bounded.hunks.map((hunk) => hunk.content).join('\n'));
|
|
505
|
+
files.push({
|
|
506
|
+
path: file.path,
|
|
507
|
+
oldPath: file.oldPath === file.path ? null : file.oldPath,
|
|
508
|
+
summary: summarizeChangedFile(rootDir, file.path),
|
|
509
|
+
includedBytes: bounded.includedBytes,
|
|
510
|
+
truncated: bounded.truncated,
|
|
511
|
+
hunks: bounded.hunks
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
if (estimatedTokens > contextLimits.maxEstimatedTokens) {
|
|
515
|
+
throw smartContextError(`${reviewProfile} diff hunk pack exceeds devil-advocate token budget`, 'DIFF_PACK_TOKEN_LIMIT');
|
|
516
|
+
}
|
|
517
|
+
assertBudgetAvailable(rootDir, { runId: run.runId, estimatedTokens });
|
|
518
|
+
const pack = {
|
|
519
|
+
schemaVersion: 1,
|
|
520
|
+
runId: run.runId,
|
|
521
|
+
reviewProfile,
|
|
522
|
+
base,
|
|
523
|
+
createdAt: new Date().toISOString(),
|
|
524
|
+
mode: 'diff-hunks',
|
|
525
|
+
limits,
|
|
526
|
+
reviewBudget: REVIEW_BUDGETS[reviewProfile],
|
|
527
|
+
totalBytes,
|
|
528
|
+
estimatedTokens,
|
|
529
|
+
truncated: totalFilesTruncated || files.some((file) => file.truncated),
|
|
530
|
+
reviewDelta: {
|
|
531
|
+
previousDiffHash: run.artifacts?.diff?.sha256 || null,
|
|
532
|
+
currentDiffHash: sha256(diff),
|
|
533
|
+
changedPaths,
|
|
534
|
+
ignoredPaths,
|
|
535
|
+
unresolvedFindingIds: unresolved.map((finding) => finding.id),
|
|
536
|
+
focusPaths
|
|
537
|
+
},
|
|
538
|
+
unresolvedFindings: unresolved,
|
|
539
|
+
files
|
|
540
|
+
};
|
|
541
|
+
const packPath = path.join(runsDir(rootDir), run.runId, 'context', 'diff-hunks.json');
|
|
542
|
+
atomicWriteJson(packPath, pack);
|
|
543
|
+
return {
|
|
544
|
+
pack,
|
|
545
|
+
path: path.relative(rootDir, packPath).split(path.sep).join('/')
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
function changedLineCount(diff) {
|
|
550
|
+
let count = 0;
|
|
551
|
+
for (const line of String(diff || '').split('\n')) {
|
|
552
|
+
if ((line.startsWith('+') && !line.startsWith('+++')) || (line.startsWith('-') && !line.startsWith('---'))) count += 1;
|
|
553
|
+
}
|
|
554
|
+
return count;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
function assessSmallTaskFastPath(rootDir, { base = 'HEAD', includeIgnored = false } = {}) {
|
|
558
|
+
const diff = git(rootDir, ['diff', '--no-ext-diff', '--unified=0', base, '--']);
|
|
559
|
+
const parsedFiles = parseDiff(diff).filter((file) => !isSensitivePath(file.path));
|
|
560
|
+
const ignoredDiffPaths = parsedFiles
|
|
561
|
+
.filter((file) => isReviewContextIgnored(rootDir, file.path, { includeIgnored }))
|
|
562
|
+
.map((file) => file.path);
|
|
563
|
+
const files = parsedFiles
|
|
564
|
+
.filter((file) => !isReviewContextIgnored(rootDir, file.path, { includeIgnored }));
|
|
565
|
+
const allUntrackedPaths = git(rootDir, ['ls-files', '--others', '--exclude-standard'])
|
|
566
|
+
.split('\n')
|
|
567
|
+
.map((line) => line.trim())
|
|
568
|
+
.filter(Boolean)
|
|
569
|
+
.filter((filePath) => !filePath.startsWith('.ai-engineering-loop/'))
|
|
570
|
+
.filter((filePath) => !isSensitivePath(filePath));
|
|
571
|
+
const ignoredUntrackedPaths = allUntrackedPaths
|
|
572
|
+
.filter((filePath) => isReviewContextIgnored(rootDir, filePath, { includeIgnored }));
|
|
573
|
+
const untrackedPaths = allUntrackedPaths
|
|
574
|
+
.filter((filePath) => !isReviewContextIgnored(rootDir, filePath, { includeIgnored }));
|
|
575
|
+
const changedPaths = [...new Set([...files.map((file) => file.path), ...untrackedPaths])];
|
|
576
|
+
const ignoredPaths = [...new Set([...ignoredDiffPaths, ...ignoredUntrackedPaths])];
|
|
577
|
+
const untrackedLines = untrackedPaths.reduce((sum, filePath) => {
|
|
578
|
+
try {
|
|
579
|
+
const file = resolveSafeRepoFile(rootDir, filePath);
|
|
580
|
+
const raw = fs.readFileSync(file.absolute);
|
|
581
|
+
if (raw.includes(0)) return sum + SMALL_TASK_LIMITS.maxChangedLines + 1;
|
|
582
|
+
return sum + raw.toString('utf8').split('\n').length;
|
|
583
|
+
} catch {
|
|
584
|
+
return sum;
|
|
585
|
+
}
|
|
586
|
+
}, 0);
|
|
587
|
+
const changedLines = changedLineCount(diff) + untrackedLines;
|
|
588
|
+
const riskyPaths = changedPaths.filter((filePath) => SMALL_TASK_HIGH_RISK_PATTERNS.some((pattern) => pattern.test(filePath)));
|
|
589
|
+
const reasons = [];
|
|
590
|
+
if (changedPaths.length === 0) reasons.push('NO_DIFF');
|
|
591
|
+
if (changedPaths.length > SMALL_TASK_LIMITS.maxFiles) reasons.push('TOO_MANY_FILES');
|
|
592
|
+
if (changedLines > SMALL_TASK_LIMITS.maxChangedLines) reasons.push('TOO_MANY_CHANGED_LINES');
|
|
593
|
+
if (riskyPaths.length) reasons.push('HIGH_RISK_PATH');
|
|
594
|
+
return {
|
|
595
|
+
schemaVersion: 1,
|
|
596
|
+
eligible: reasons.length === 0,
|
|
597
|
+
recommendedProfile: reasons.length === 0 ? 'lean' : 'standard',
|
|
598
|
+
limits: SMALL_TASK_LIMITS,
|
|
599
|
+
changedPaths,
|
|
600
|
+
ignoredPaths,
|
|
601
|
+
changedLines,
|
|
602
|
+
riskyPaths,
|
|
603
|
+
reasons
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
function smartContextSummary(result) {
|
|
608
|
+
if (result.pack) {
|
|
609
|
+
return {
|
|
610
|
+
schemaVersion: result.pack.schemaVersion,
|
|
611
|
+
runId: result.pack.runId,
|
|
612
|
+
reviewProfile: result.pack.reviewProfile,
|
|
613
|
+
path: result.path,
|
|
614
|
+
files: result.pack.files.length,
|
|
615
|
+
totalBytes: result.pack.totalBytes,
|
|
616
|
+
estimatedTokens: result.pack.estimatedTokens,
|
|
617
|
+
truncated: result.pack.truncated,
|
|
618
|
+
unresolvedFindings: result.pack.unresolvedFindings.length,
|
|
619
|
+
ignoredPaths: result.pack.reviewDelta?.ignoredPaths?.length || 0,
|
|
620
|
+
reviewBudget: result.pack.reviewBudget
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
return {
|
|
624
|
+
schemaVersion: result.index.schemaVersion,
|
|
625
|
+
path: result.path,
|
|
626
|
+
files: result.index.files.length
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
module.exports = {
|
|
631
|
+
DIFF_PACK_LIMITS,
|
|
632
|
+
DEFAULT_CONTEXT_IGNORE_PATTERNS,
|
|
633
|
+
REVIEW_BUDGETS,
|
|
634
|
+
SMALL_TASK_LIMITS,
|
|
635
|
+
assessSmallTaskFastPath,
|
|
636
|
+
buildContextIndex,
|
|
637
|
+
createDiffHunkPack,
|
|
638
|
+
isReviewContextIgnored,
|
|
639
|
+
queryContextIndex,
|
|
640
|
+
relatedContextIndex,
|
|
641
|
+
parseDiff,
|
|
642
|
+
smartContextSummary
|
|
643
|
+
};
|
package/lib/studio-server.js
CHANGED
|
@@ -54,6 +54,7 @@ const {
|
|
|
54
54
|
|
|
55
55
|
const MAX_BODY = 256 * 1024;
|
|
56
56
|
const ASSET_DIR = path.join(__dirname, '..', 'studio');
|
|
57
|
+
const LIVE_WORKFLOW_EVENT_LIMIT = 12;
|
|
57
58
|
|
|
58
59
|
function json(response, status, value) {
|
|
59
60
|
response.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
@@ -108,11 +109,27 @@ function liveSnapshot(rootDir) {
|
|
|
108
109
|
const run = getCurrentRun(rootDir);
|
|
109
110
|
if (!run?.workflow) return null;
|
|
110
111
|
const workflow = workflowStatus(rootDir, { runId: run.runId });
|
|
112
|
+
const eventTail = workflow.events.slice(-LIVE_WORKFLOW_EVENT_LIMIT);
|
|
113
|
+
const edges = edgesFromPlan(workflow.plan, workflow.state);
|
|
111
114
|
return redactedValue({
|
|
112
115
|
run,
|
|
116
|
+
summary: {
|
|
117
|
+
runId: run.runId,
|
|
118
|
+
state: run.state,
|
|
119
|
+
nodeCount: workflow.plan.nodes.length,
|
|
120
|
+
edgeCount: edges.length,
|
|
121
|
+
workflowEventCount: workflow.events.length,
|
|
122
|
+
workflowEventLimit: LIVE_WORKFLOW_EVENT_LIMIT,
|
|
123
|
+
eventsTruncated: workflow.events.length > eventTail.length,
|
|
124
|
+
details: {
|
|
125
|
+
nodeIo: `/api/runs/${encodeURIComponent(run.runId)}/nodes/{nodeId}`,
|
|
126
|
+
artifacts: `/api/runs/${encodeURIComponent(run.runId)}/artifacts?path={path}`,
|
|
127
|
+
lifecycle: `/api/runs/${encodeURIComponent(run.runId)}/lifecycle?after={sequence}`
|
|
128
|
+
}
|
|
129
|
+
},
|
|
113
130
|
plan: workflow.plan,
|
|
114
131
|
nodes: workflow.state.nodes,
|
|
115
|
-
edges
|
|
132
|
+
edges,
|
|
116
133
|
loopGroups: workflow.state.loopGroups,
|
|
117
134
|
journey: (() => {
|
|
118
135
|
try {
|
|
@@ -128,7 +145,7 @@ function liveSnapshot(rootDir) {
|
|
|
128
145
|
}
|
|
129
146
|
})(),
|
|
130
147
|
budget: budgetStatus(rootDir, { runId: run.runId }),
|
|
131
|
-
events:
|
|
148
|
+
events: eventTail.map(({ initialNodes, changes, ...event }) => ({
|
|
132
149
|
...event,
|
|
133
150
|
changedNodes: (changes || []).map((change) => change.nodeId)
|
|
134
151
|
}))
|