ai-engineering-loop 1.16.0 → 2.0.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.
Files changed (74) hide show
  1. package/.agents/skills/ai-engineering-loop/SKILL.md +4 -4
  2. package/.agents/skills/ai-engineering-loop/references/loop-commands.md +5 -1
  3. package/.agents/skills/ai-engineering-loop/references/loop-deliver.md +2 -1
  4. package/.agents/skills/ai-engineering-loop/references/loop-goal.md +1 -0
  5. package/.agents/skills/ai-engineering-loop/references/loop-maker.md +1 -0
  6. package/.claude/skills/ai-engineering-loop/SKILL.md +4 -4
  7. package/.claude/skills/ai-engineering-loop/references/loop-commands.md +5 -1
  8. package/.claude/skills/ai-engineering-loop/references/loop-deliver.md +2 -1
  9. package/.claude/skills/ai-engineering-loop/references/loop-goal.md +1 -0
  10. package/.claude/skills/ai-engineering-loop/references/loop-maker.md +1 -0
  11. package/CHANGELOG.md +69 -0
  12. package/README.md +24 -22
  13. package/adapters/dot/gitlab.md +6 -0
  14. package/adapters/github/pull-request.md +5 -0
  15. package/adapters/gitlab/merge-request.md +5 -0
  16. package/adapters/quality/skills/task-impact-inquiry/SKILL.md +2 -0
  17. package/bin/ai-engineering-loop.js +514 -19
  18. package/core/bench.md +77 -0
  19. package/core/blast-radius-map.md +37 -0
  20. package/core/configuration-precedence.md +11 -1
  21. package/core/learnings.md +24 -0
  22. package/core/local-workflow-studio.md +26 -68
  23. package/core/personal-workflow.md +78 -0
  24. package/core/pr-receipt.md +35 -0
  25. package/core/runtime-safety.md +24 -0
  26. package/core/workflow-recipes.md +2 -0
  27. package/evals/README.md +2 -0
  28. package/evals/cases/25-no-task-onboarding.json +22 -0
  29. package/evals/scenarios/t0-docs-typo.json +31 -0
  30. package/evals/scenarios/t1-empty-state.json +31 -0
  31. package/evals/scenarios/t1-list-pagination.json +33 -0
  32. package/evals/scenarios/t2-auth-session.json +31 -0
  33. package/evals/scenarios/t2-payment-idempotency.json +31 -0
  34. package/evals/scenarios/trap-destructive-reset.json +33 -0
  35. package/evals/scenarios/trap-out-of-scope.json +34 -0
  36. package/evals/scenarios/trap-prompt-injection.json +39 -0
  37. package/lib/bench-targets.js +194 -0
  38. package/lib/bench.js +1236 -0
  39. package/lib/budget.js +32 -0
  40. package/lib/cli-args.js +17 -2
  41. package/lib/doctor.js +20 -2
  42. package/lib/gates.js +39 -0
  43. package/lib/guard-hooks.js +115 -0
  44. package/lib/guard.js +336 -0
  45. package/lib/hosts/commands.js +4 -3
  46. package/lib/hosts/doctor.js +3 -3
  47. package/lib/hosts/registry.js +39 -2
  48. package/lib/hosts/table.js +6 -4
  49. package/lib/impact-map.js +501 -0
  50. package/lib/learn.js +303 -0
  51. package/lib/personal-workflow.js +925 -0
  52. package/lib/receipt.js +362 -0
  53. package/lib/recipe.js +90 -13
  54. package/lib/run-state.js +10 -1
  55. package/lib/run-watcher.js +511 -0
  56. package/lib/runtime-files.js +2 -0
  57. package/lib/runtime-policy.js +42 -1
  58. package/lib/safe-context.js +14 -0
  59. package/lib/smart-context.js +21 -0
  60. package/lib/studio-runtime.js +8 -3
  61. package/lib/studio-server.js +336 -98
  62. package/lib/user-state.js +34 -1
  63. package/package.json +1 -1
  64. package/schemas/bench-result.schema.json +288 -0
  65. package/schemas/bench-scenario.schema.json +95 -0
  66. package/schemas/delivery.schema.json +11 -1
  67. package/schemas/finding-ledger.schema.json +2 -1
  68. package/schemas/goal-contract.schema.json +9 -0
  69. package/schemas/impact-map.schema.json +78 -0
  70. package/schemas/learnings.schema.json +47 -0
  71. package/schemas/receipt.schema.json +102 -0
  72. package/studio/app.js +599 -1621
  73. package/studio/index.html +43 -178
  74. package/studio/styles.css +17 -2
@@ -0,0 +1,511 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ const { currentRunPath, getCurrentRun, runsDir } = require('./run-state.js');
6
+ const { listRunLifecycle, runAgentPresence } = require('./run-lifecycle.js');
7
+ const { listRunInteractions } = require('./run-interactions.js');
8
+ const { isBlockingFinding } = require('./orchestration.js');
9
+ const { verifyReceipt } = require('./receipt.js');
10
+
11
+ const WATCH_ENV = 'AEL_STUDIO_FS_WATCH';
12
+ const DEFAULT_DEBOUNCE_MS = 100;
13
+ const DEFAULT_POLL_MS = 1000;
14
+ const DEFAULT_RING_SIZE = 512;
15
+ const CURRENT_POINTER = 'current.json';
16
+
17
+ const ARTIFACT_FILES = Object.freeze({
18
+ 'state.json': { type: 'run.state', summarize: summarizeState },
19
+ 'impact-map.json': { type: 'artifact.impact', summarize: summarizeImpact },
20
+ 'impact-drift.json': { type: 'artifact.drift', summarize: summarizeDrift },
21
+ 'guard-events.json': { type: 'artifact.guard', summarize: summarizeGuard },
22
+ 'verification.json': { type: 'artifact.verification', summarize: summarizeVerification },
23
+ 'findings.json': { type: 'artifact.findings', summarize: summarizeFindings },
24
+ 'verdict.json': { type: 'artifact.verdict', summarize: summarizeVerdict },
25
+ 'receipt.json': { type: 'artifact.receipt', summarize: summarizeReceipt }
26
+ });
27
+
28
+ function envWatchEnabled(env = process.env) {
29
+ const value = env[WATCH_ENV];
30
+ if (value == null || value === '') return true;
31
+ return !['0', 'false', 'off', 'poll'].includes(String(value).toLowerCase());
32
+ }
33
+
34
+ function isTransientName(name) {
35
+ return /\.tmp$|\.lock$|~$/.test(name) || name.includes('.tmp.');
36
+ }
37
+
38
+ function readJsonFile(filePath) {
39
+ try {
40
+ const stat = fs.lstatSync(filePath);
41
+ if (stat.isSymbolicLink() || !stat.isFile()) return null;
42
+ return {
43
+ value: JSON.parse(fs.readFileSync(filePath, 'utf8')),
44
+ mtimeMs: stat.mtimeMs,
45
+ size: stat.size
46
+ };
47
+ } catch {
48
+ return null;
49
+ }
50
+ }
51
+
52
+ function fingerprintStat(filePath) {
53
+ try {
54
+ const stat = fs.lstatSync(filePath);
55
+ if (stat.isSymbolicLink()) return null;
56
+ return `${stat.mtimeMs}:${stat.size}:${stat.isDirectory() ? 'd' : 'f'}`;
57
+ } catch {
58
+ return null;
59
+ }
60
+ }
61
+
62
+ function summarizeState(value) {
63
+ return {
64
+ state: value?.state || null,
65
+ iteration: value?.iteration ?? null,
66
+ mode: value?.mode || null,
67
+ tier: value?.tier?.current || null,
68
+ updatedAt: value?.updatedAt || null,
69
+ workflowBound: Boolean(value?.workflow),
70
+ task: value?.task || '',
71
+ displayName: value?.displayName || ''
72
+ };
73
+ }
74
+
75
+ function summarizeImpact(value) {
76
+ return {
77
+ status: value?.status || null,
78
+ fileCount: Array.isArray(value?.files) ? value.files.length : 0
79
+ };
80
+ }
81
+
82
+ function summarizeDrift(value) {
83
+ const outOfMap = Array.isArray(value?.outOfMap) ? value.outOfMap : [];
84
+ return {
85
+ mapStatus: value?.mapStatus || null,
86
+ outOfMapCount: outOfMap.length
87
+ };
88
+ }
89
+
90
+ function summarizeGuard(value) {
91
+ const events = Array.isArray(value?.events) ? value.events : [];
92
+ return {
93
+ warn: events.filter((event) => event?.decision === 'warn' || event?.action === 'warn').length,
94
+ block: events.filter((event) => event?.decision === 'block' || event?.action === 'block').length,
95
+ count: events.length
96
+ };
97
+ }
98
+
99
+ function commandFailed(command) {
100
+ return command?.exitCode !== 0 || command?.timeoutStatus !== 'COMPLETED';
101
+ }
102
+
103
+ function summarizeVerification(value) {
104
+ const commands = Array.isArray(value?.commands) ? value.commands : [];
105
+ const failed = commands.filter(commandFailed).length;
106
+ return {
107
+ passed: commands.length > 0 && failed === 0,
108
+ failed,
109
+ commandCount: commands.length
110
+ };
111
+ }
112
+
113
+ function summarizeFindings(value) {
114
+ const findings = Array.isArray(value?.findings) ? value.findings : [];
115
+ return {
116
+ count: findings.length,
117
+ blocking: findings.filter(isBlockingFinding).length
118
+ };
119
+ }
120
+
121
+ function summarizeVerdict(value) {
122
+ return {
123
+ verdict: value?.verdict || null,
124
+ action: value?.action || null
125
+ };
126
+ }
127
+
128
+ function summarizeReceipt(value, context = {}) {
129
+ const digest = value?.digest || null;
130
+ const { rootDir, runId } = context;
131
+ if (!rootDir || !runId) {
132
+ return { present: true, digest, verified: false, problems: ['RECEIPT_UNVERIFIED'] };
133
+ }
134
+ try {
135
+ const result = verifyReceipt(rootDir, { runId });
136
+ const problems = (result.problems || []).map((item) => item.code || String(item));
137
+ return {
138
+ present: true,
139
+ digest: result.digest || digest,
140
+ verified: problems.length === 0,
141
+ problems
142
+ };
143
+ } catch (error) {
144
+ return {
145
+ present: true,
146
+ digest,
147
+ verified: false,
148
+ problems: [error.code || 'RECEIPT_FAILED']
149
+ };
150
+ }
151
+ }
152
+
153
+ function summarizeNamedArtifact(name, value, context) {
154
+ return ARTIFACT_FILES[name]?.summarize(value, context) || { present: true };
155
+ }
156
+
157
+ function compactPresence(rootDir, runId) {
158
+ try {
159
+ const presence = runAgentPresence(rootDir, runId);
160
+ let pendingQuestions = 0;
161
+ try {
162
+ pendingQuestions = listRunInteractions(rootDir, runId).pending.length;
163
+ } catch {
164
+ pendingQuestions = 0;
165
+ }
166
+ return { ...presence, pendingQuestions };
167
+ } catch {
168
+ return { status: 'UNKNOWN', lastHeartbeatAt: null, pendingQuestions: 0 };
169
+ }
170
+ }
171
+
172
+ function currentPointer(rootDir) {
173
+ try {
174
+ const run = getCurrentRun(rootDir);
175
+ return run?.runId || null;
176
+ } catch {
177
+ return null;
178
+ }
179
+ }
180
+
181
+ function listRunIds(rootDir) {
182
+ const directory = runsDir(rootDir);
183
+ if (!fs.existsSync(directory)) return [];
184
+ return fs.readdirSync(directory, { withFileTypes: true })
185
+ .filter((entry) => entry.isDirectory() && entry.name !== CURRENT_POINTER && !isTransientName(entry.name))
186
+ .map((entry) => entry.name);
187
+ }
188
+
189
+ function createRunWatcher(rootDir, options = {}) {
190
+ const debounceMs = Number.isFinite(options.debounceMs) ? options.debounceMs : DEFAULT_DEBOUNCE_MS;
191
+ const pollMs = Number.isFinite(options.pollMs) ? options.pollMs : DEFAULT_POLL_MS;
192
+ const ringSize = Number.isFinite(options.ringSize) ? options.ringSize : DEFAULT_RING_SIZE;
193
+ const watchRequested = options.watch == null ? envWatchEnabled() : options.watch === true;
194
+ const contextDir = path.join(rootDir, '.ai-engineering-loop');
195
+ const runRoot = runsDir(rootDir);
196
+ const pointerPath = currentRunPath(rootDir);
197
+
198
+ let mode = watchRequested ? 'watch' : 'poll';
199
+ let seq = 0;
200
+ let started = false;
201
+ const ring = [];
202
+ const listeners = new Set();
203
+ const fingerprints = new Map();
204
+ const knownRuns = new Set();
205
+ const lifecycleCursor = new Map();
206
+ const presenceFingerprints = new Map();
207
+ const debounceTimers = new Map();
208
+ const fsWatchers = [];
209
+ let pollTimer = null;
210
+
211
+ function hello() {
212
+ return {
213
+ type: 'hello',
214
+ seq,
215
+ mode,
216
+ root: path.resolve(rootDir),
217
+ currentRunId: currentPointer(rootDir),
218
+ ringStart: ring[0]?.seq || seq,
219
+ nextSeq: seq
220
+ };
221
+ }
222
+
223
+ function eventsAfter(after) {
224
+ const cursor = Number(after);
225
+ if (!Number.isSafeInteger(cursor) || cursor < 0) {
226
+ const error = new Error('Event cursor is invalid');
227
+ error.code = 'INVALID_EVENT_CURSOR';
228
+ throw error;
229
+ }
230
+ if (ring.length === 0) return { gap: false, events: [] };
231
+ const ringStart = ring[0].seq;
232
+ if (cursor + 1 < ringStart) return { gap: true, ringStart, events: [] };
233
+ return { gap: false, ringStart, events: ring.filter((event) => event.seq > cursor) };
234
+ }
235
+
236
+ function publish(type, runId, payload) {
237
+ seq += 1;
238
+ const event = {
239
+ type,
240
+ seq,
241
+ runId: runId || null,
242
+ at: new Date().toISOString(),
243
+ payload: payload || {}
244
+ };
245
+ ring.push(event);
246
+ while (ring.length > ringSize) ring.shift();
247
+ for (const listener of listeners) {
248
+ try { listener(event); } catch { /* a broken subscriber must not stop the others */ }
249
+ }
250
+ return event;
251
+ }
252
+
253
+ function noteFingerprint(key, filePath) {
254
+ const next = fingerprintStat(filePath);
255
+ const previous = fingerprints.get(key);
256
+ if (next === previous) return false;
257
+ if (next) fingerprints.set(key, next);
258
+ else fingerprints.delete(key);
259
+ return true;
260
+ }
261
+
262
+ function emitPresence(runId) {
263
+ const presence = compactPresence(rootDir, runId);
264
+ const encoded = JSON.stringify(presence);
265
+ if (presenceFingerprints.get(runId) === encoded) return;
266
+ presenceFingerprints.set(runId, encoded);
267
+ publish('presence', runId, presence);
268
+ }
269
+
270
+ function emitLifecycle(runId) {
271
+ const after = lifecycleCursor.get(runId) || 0;
272
+ let listed;
273
+ try {
274
+ listed = listRunLifecycle(rootDir, runId, { after, limit: 100 });
275
+ } catch {
276
+ return;
277
+ }
278
+ if (listed.events.length) {
279
+ lifecycleCursor.set(runId, listed.nextSequence);
280
+ publish('lifecycle', runId, listed);
281
+ }
282
+ emitPresence(runId);
283
+ }
284
+
285
+ function emitArtifact(runId, name) {
286
+ const spec = ARTIFACT_FILES[name];
287
+ if (!spec) return;
288
+ const filePath = path.join(runRoot, runId, name);
289
+ const key = `${runId}/${name}`;
290
+ if (!noteFingerprint(key, filePath) && fingerprints.has(key)) return;
291
+ const read = readJsonFile(filePath);
292
+ if (!read) return;
293
+ if (name === 'state.json' && !knownRuns.has(runId)) {
294
+ knownRuns.add(runId);
295
+ publish('run.created', runId, { runId, ...summarizeState(read.value) });
296
+ }
297
+ publish(spec.type, runId, spec.summarize(read.value, { rootDir, runId }));
298
+ }
299
+
300
+ function emitCurrent() {
301
+ if (!noteFingerprint(CURRENT_POINTER, pointerPath) && fingerprints.has(CURRENT_POINTER)) return;
302
+ let runId = null;
303
+ try {
304
+ runId = currentPointer(rootDir);
305
+ } catch {
306
+ runId = null;
307
+ }
308
+ publish('current.changed', runId, { runId });
309
+ }
310
+
311
+ function processRunFile(runId, name) {
312
+ if (name === 'lifecycle.jsonl' || name === 'interactions.jsonl') {
313
+ const filePath = path.join(runRoot, runId, name);
314
+ if (!noteFingerprint(`${runId}/${name}`, filePath) && fingerprints.has(`${runId}/${name}`)) return;
315
+ emitLifecycle(runId);
316
+ return;
317
+ }
318
+ if (ARTIFACT_FILES[name]) emitArtifact(runId, name);
319
+ }
320
+
321
+ function seedLifecycleCursor(runId) {
322
+ try {
323
+ const all = listRunLifecycle(rootDir, runId, { after: 0, limit: 100 });
324
+ lifecycleCursor.set(runId, all.nextSequence);
325
+ } catch {
326
+ lifecycleCursor.set(runId, 0);
327
+ }
328
+ presenceFingerprints.set(runId, JSON.stringify(compactPresence(rootDir, runId)));
329
+ }
330
+
331
+ function scanRun(runId, { announceNew = true } = {}) {
332
+ const directory = path.join(runRoot, runId);
333
+ if (!fs.existsSync(directory)) return;
334
+ if (!announceNew) knownRuns.add(runId);
335
+ for (const name of Object.keys(ARTIFACT_FILES)) {
336
+ const filePath = path.join(directory, name);
337
+ if (!fs.existsSync(filePath)) continue;
338
+ if (announceNew) emitArtifact(runId, name);
339
+ else noteFingerprint(`${runId}/${name}`, filePath);
340
+ }
341
+ for (const name of ['lifecycle.jsonl', 'interactions.jsonl']) {
342
+ const filePath = path.join(directory, name);
343
+ if (!fs.existsSync(filePath)) continue;
344
+ if (announceNew) {
345
+ if (!noteFingerprint(`${runId}/${name}`, filePath) && fingerprints.has(`${runId}/${name}`)) continue;
346
+ emitLifecycle(runId);
347
+ } else {
348
+ noteFingerprint(`${runId}/${name}`, filePath);
349
+ seedLifecycleCursor(runId);
350
+ }
351
+ }
352
+ }
353
+
354
+ function scanAll({ announceNew = false } = {}) {
355
+ if (fs.existsSync(pointerPath)) {
356
+ if (announceNew) emitCurrent();
357
+ else noteFingerprint(CURRENT_POINTER, pointerPath);
358
+ }
359
+ for (const runId of listRunIds(rootDir)) scanRun(runId, { announceNew });
360
+ }
361
+
362
+ function schedule(key, work) {
363
+ const previous = debounceTimers.get(key);
364
+ if (previous) clearTimeout(previous);
365
+ const timer = setTimeout(() => {
366
+ debounceTimers.delete(key);
367
+ try { work(); } catch { /* a single bad file must not stop the watcher */ }
368
+ }, debounceMs);
369
+ timer.unref?.();
370
+ debounceTimers.set(key, timer);
371
+ }
372
+
373
+ function handleRelativePath(relative, origin = 'context') {
374
+ const normalized = String(relative || '').replace(/\\/g, '/');
375
+ if (!normalized || isTransientName(path.basename(normalized))) return;
376
+ const parts = normalized.split('/').filter(Boolean);
377
+ let fromRuns;
378
+ if (origin === 'runs') {
379
+ fromRuns = parts;
380
+ } else if (parts[0] === CURRENT_POINTER && parts.length === 1) {
381
+ schedule(CURRENT_POINTER, emitCurrent);
382
+ return;
383
+ } else if (parts[0] !== 'runs') {
384
+ return;
385
+ } else {
386
+ fromRuns = parts.slice(1);
387
+ }
388
+ if (!fromRuns.length) return;
389
+ if (fromRuns[0] === CURRENT_POINTER && fromRuns.length === 1) {
390
+ schedule(CURRENT_POINTER, emitCurrent);
391
+ return;
392
+ }
393
+ if (fromRuns.length < 2) {
394
+ if (fromRuns.length === 1 && fromRuns[0] !== CURRENT_POINTER) {
395
+ const runId = fromRuns[0];
396
+ schedule(runId, () => scanRun(runId, { announceNew: true }));
397
+ }
398
+ return;
399
+ }
400
+ const runId = fromRuns[0];
401
+ const name = fromRuns[fromRuns.length - 1];
402
+ schedule(`${runId}/${name}`, () => processRunFile(runId, name));
403
+ }
404
+
405
+ function onFsEvent(origin, _eventType, filename) {
406
+ if (!filename) {
407
+ schedule('scan', () => scanAll({ announceNew: true }));
408
+ return;
409
+ }
410
+ handleRelativePath(String(filename), origin);
411
+ }
412
+
413
+ function watchPath(target, recursive, origin) {
414
+ try {
415
+ const watcher = fs.watch(target, recursive ? { recursive: true } : {}, (eventType, filename) => {
416
+ onFsEvent(origin, eventType, filename);
417
+ });
418
+ watcher.on('error', () => {});
419
+ fsWatchers.push(watcher);
420
+ return true;
421
+ } catch {
422
+ return false;
423
+ }
424
+ }
425
+
426
+ function startPoll() {
427
+ if (pollTimer) return;
428
+ pollTimer = setInterval(() => {
429
+ try { scanAll({ announceNew: true }); } catch { /* keep polling */ }
430
+ }, pollMs);
431
+ pollTimer.unref?.();
432
+ }
433
+
434
+ function start() {
435
+ if (started) return;
436
+ started = true;
437
+ fs.mkdirSync(runRoot, { recursive: true, mode: 0o700 });
438
+ scanAll({ announceNew: false });
439
+ if (watchRequested) {
440
+ const watched = watchPath(contextDir, true, 'context')
441
+ || watchPath(runRoot, true, 'runs')
442
+ || watchPath(runRoot, false, 'runs');
443
+ if (watched) {
444
+ mode = 'watch';
445
+ if (fs.existsSync(pointerPath)) watchPath(path.dirname(pointerPath), false, 'runs');
446
+ } else {
447
+ mode = 'poll';
448
+ }
449
+ } else {
450
+ mode = 'poll';
451
+ }
452
+ // Stat-poll always runs. Fingerprints drop duplicates, so it is a safety
453
+ // net when FSEvents miss a write rather than a second event stream.
454
+ startPoll();
455
+ }
456
+
457
+ function stop() {
458
+ started = false;
459
+ for (const timer of debounceTimers.values()) clearTimeout(timer);
460
+ debounceTimers.clear();
461
+ if (pollTimer) {
462
+ clearInterval(pollTimer);
463
+ pollTimer = null;
464
+ }
465
+ for (const watcher of fsWatchers) {
466
+ try { watcher.close(); } catch { /* already closed */ }
467
+ }
468
+ fsWatchers.length = 0;
469
+ listeners.clear();
470
+ }
471
+
472
+ function subscribe(listener) {
473
+ listeners.add(listener);
474
+ return () => listeners.delete(listener);
475
+ }
476
+
477
+ return {
478
+ get mode() { return mode; },
479
+ get seq() { return seq; },
480
+ get ringSize() { return ringSize; },
481
+ start,
482
+ stop,
483
+ subscribe,
484
+ eventsAfter,
485
+ hello,
486
+ scanAll
487
+ };
488
+ }
489
+
490
+ function loadArtifactSummaries(rootDir, runId) {
491
+ const directory = path.join(runsDir(rootDir), runId);
492
+ const artifacts = {};
493
+ for (const [name, spec] of Object.entries(ARTIFACT_FILES)) {
494
+ const read = readJsonFile(path.join(directory, name));
495
+ artifacts[spec.type] = read ? spec.summarize(read.value, { rootDir, runId }) : null;
496
+ }
497
+ return artifacts;
498
+ }
499
+
500
+ module.exports = {
501
+ ARTIFACT_FILES,
502
+ DEFAULT_DEBOUNCE_MS,
503
+ DEFAULT_POLL_MS,
504
+ DEFAULT_RING_SIZE,
505
+ WATCH_ENV,
506
+ compactPresence,
507
+ createRunWatcher,
508
+ envWatchEnabled,
509
+ loadArtifactSummaries,
510
+ summarizeNamedArtifact
511
+ };
@@ -6,10 +6,12 @@ const path = require('path');
6
6
  const PRIVATE_RUNTIME_PATTERNS = Object.freeze([
7
7
  'runs/',
8
8
  'usage/',
9
+ 'bench/',
9
10
  'worktrees/',
10
11
  'locks/',
11
12
  'drafts/',
12
13
  'studio-layouts/',
14
+ 'studio.json',
13
15
  'recipes/.history/',
14
16
  'tasks/current.diff',
15
17
  'tasks/*.log'
@@ -7,6 +7,12 @@ const {
7
7
  atomicWriteJson
8
8
  } = require('./run-state.js');
9
9
 
10
+ const DEFAULT_WORKFLOW = Object.freeze({
11
+ allowPersonal: true,
12
+ requiredRecipe: null,
13
+ mergeRecipe: null
14
+ });
15
+
10
16
  const DEFAULT_POLICY = Object.freeze({
11
17
  schemaVersion: 1,
12
18
  defaultMode: RUN_MODES.ASSISTED,
@@ -15,7 +21,8 @@ const DEFAULT_POLICY = Object.freeze({
15
21
  requireSandboxForUnattended: true,
16
22
  perRunTokenLimit: 50_000,
17
23
  dailyTokenLimit: 200_000,
18
- killSwitch: false
24
+ killSwitch: false,
25
+ workflow: DEFAULT_WORKFLOW
19
26
  });
20
27
 
21
28
  const REVIEW_PROFILES = Object.freeze(['lean', 'standard', 'thorough']);
@@ -46,12 +53,42 @@ function normalizeReviewProfile(value, fallback = DEFAULT_POLICY.reviewProfile)
46
53
  return normalized;
47
54
  }
48
55
 
56
+ function normalizeWorkflowConfig(raw) {
57
+ const value = raw && typeof raw === 'object' && !Array.isArray(raw) ? raw : {};
58
+ const requiredRecipe = value.requiredRecipe == null || value.requiredRecipe === ''
59
+ ? null
60
+ : String(value.requiredRecipe);
61
+ const mergeRecipe = value.mergeRecipe == null || value.mergeRecipe === ''
62
+ ? null
63
+ : String(value.mergeRecipe);
64
+ return {
65
+ allowPersonal: value.allowPersonal !== false,
66
+ requiredRecipe,
67
+ mergeRecipe
68
+ };
69
+ }
70
+
49
71
  function validatePolicy(value) {
50
72
  const errors = [];
51
73
  if (!value || typeof value !== 'object' || Array.isArray(value)) {
52
74
  return ['runtime policy must be a JSON object'];
53
75
  }
54
76
  if (value.schemaVersion !== 1) errors.push('schemaVersion must be 1');
77
+ if (value.workflow != null) {
78
+ if (typeof value.workflow !== 'object' || Array.isArray(value.workflow)) {
79
+ errors.push('workflow must be an object');
80
+ } else {
81
+ if (value.workflow.allowPersonal != null && typeof value.workflow.allowPersonal !== 'boolean') {
82
+ errors.push('workflow.allowPersonal must be boolean');
83
+ }
84
+ for (const key of ['requiredRecipe', 'mergeRecipe']) {
85
+ const item = value.workflow[key];
86
+ if (item != null && item !== '' && !/^[a-z][a-z0-9-]{0,63}$/.test(String(item))) {
87
+ errors.push(`workflow.${key} must be a recipe id`);
88
+ }
89
+ }
90
+ }
91
+ }
55
92
  try {
56
93
  normalizeMode(value.defaultMode);
57
94
  } catch (cause) {
@@ -94,6 +131,7 @@ function loadPolicy(rootDir) {
94
131
  }
95
132
  policy.defaultMode = normalizeMode(policy.defaultMode);
96
133
  policy.reviewProfile = normalizeReviewProfile(policy.reviewProfile);
134
+ policy.workflow = normalizeWorkflowConfig(policy.workflow);
97
135
  return policy;
98
136
  }
99
137
 
@@ -108,6 +146,7 @@ function writePolicy(rootDir, policy) {
108
146
  }
109
147
  next.defaultMode = normalizeMode(next.defaultMode);
110
148
  next.reviewProfile = normalizeReviewProfile(next.reviewProfile);
149
+ next.workflow = normalizeWorkflowConfig(next.workflow);
111
150
  atomicWriteJson(policyPath(rootDir), next);
112
151
  return next;
113
152
  }
@@ -129,7 +168,9 @@ function assertModeAllowed(rootDir, mode) {
129
168
 
130
169
  module.exports = {
131
170
  DEFAULT_POLICY,
171
+ DEFAULT_WORKFLOW,
132
172
  REVIEW_PROFILES,
173
+ normalizeWorkflowConfig,
133
174
  policyPath,
134
175
  normalizeMode,
135
176
  normalizeReviewProfile,
@@ -237,6 +237,17 @@ function createContextPack(rootDir, { runId, stage, files, profile } = {}) {
237
237
  content: redacted.text
238
238
  });
239
239
  }
240
+ // Project learnings (#61) for Maker and DA, inside this stage's budget.
241
+ const learned = stage === 'judge'
242
+ ? { learnings: [], estimatedTokens: 0 }
243
+ : require('./learn.js').selectLearnings(rootDir, {
244
+ files: entries.map((entry) => entry.path),
245
+ budgetTokens: Math.min(
246
+ limits.maxEstimatedTokens * require('./learn.js').LEARNINGS_BUDGET_SHARE,
247
+ limits.maxEstimatedTokens - estimatedTokens
248
+ )
249
+ });
250
+ estimatedTokens += learned.estimatedTokens;
240
251
  assertBudgetAvailable(rootDir, { runId: run.runId, estimatedTokens });
241
252
 
242
253
  const pack = {
@@ -249,6 +260,8 @@ function createContextPack(rootDir, { runId, stage, files, profile } = {}) {
249
260
  limits,
250
261
  totalBytes,
251
262
  estimatedTokens,
263
+ learningTokens: learned.estimatedTokens,
264
+ learnings: learned.learnings,
252
265
  entries
253
266
  };
254
267
  const packPath = path.join(runsDir(rootDir), run.runId, 'context', `${stage}.json`);
@@ -269,6 +282,7 @@ function contextPackSummary(result) {
269
282
  files: result.pack.entries.length,
270
283
  totalBytes: result.pack.totalBytes,
271
284
  estimatedTokens: result.pack.estimatedTokens,
285
+ learnings: (result.pack.learnings || []).length,
272
286
  redactions: result.pack.entries.reduce((sum, entry) => sum + entry.redactions.length, 0),
273
287
  truncatedFiles: result.pack.entries.filter((entry) => entry.truncated).length
274
288
  };
@@ -473,6 +473,14 @@ function boundedHunks(file, limits, remainingBytes) {
473
473
  return { hunks, includedBytes, truncated };
474
474
  }
475
475
 
476
+ // Blast-radius drift from verify (#71): the Devil's Advocate sees which
477
+ // changed files were outside the impact map.
478
+ function impactDriftSummary(rootDir, runId) {
479
+ const drift = require('./impact-map.js').loadImpactDrift(rootDir, runId);
480
+ if (!drift) return null;
481
+ return { mapStatus: drift.mapStatus, outOfMap: (drift.outOfMap || []).slice(0, 50) };
482
+ }
483
+
476
484
  function createDiffHunkPack(rootDir, { runId, base = 'HEAD', profile, includeIgnored = false } = {}) {
477
485
  const run = resolveRun(rootDir, runId);
478
486
  const policy = loadPolicy(rootDir);
@@ -514,6 +522,16 @@ function createDiffHunkPack(rootDir, { runId, base = 'HEAD', profile, includeIgn
514
522
  if (estimatedTokens > contextLimits.maxEstimatedTokens) {
515
523
  throw smartContextError(`${reviewProfile} diff hunk pack exceeds devil-advocate token budget`, 'DIFF_PACK_TOKEN_LIMIT');
516
524
  }
525
+ // Project learnings (#61) the Devil's Advocate should not have to rediscover.
526
+ const { LEARNINGS_BUDGET_SHARE, selectLearnings } = require('./learn.js');
527
+ const learned = selectLearnings(rootDir, {
528
+ files: changedPaths,
529
+ budgetTokens: Math.min(
530
+ contextLimits.maxEstimatedTokens * LEARNINGS_BUDGET_SHARE,
531
+ contextLimits.maxEstimatedTokens - estimatedTokens
532
+ )
533
+ });
534
+ estimatedTokens += learned.estimatedTokens;
517
535
  assertBudgetAvailable(rootDir, { runId: run.runId, estimatedTokens });
518
536
  const pack = {
519
537
  schemaVersion: 1,
@@ -536,6 +554,8 @@ function createDiffHunkPack(rootDir, { runId, base = 'HEAD', profile, includeIgn
536
554
  focusPaths
537
555
  },
538
556
  unresolvedFindings: unresolved,
557
+ impactDrift: impactDriftSummary(rootDir, run.runId),
558
+ learnings: learned.learnings,
539
559
  files
540
560
  };
541
561
  const packPath = path.join(runsDir(rootDir), run.runId, 'context', 'diff-hunks.json');
@@ -617,6 +637,7 @@ function smartContextSummary(result) {
617
637
  truncated: result.pack.truncated,
618
638
  unresolvedFindings: result.pack.unresolvedFindings.length,
619
639
  ignoredPaths: result.pack.reviewDelta?.ignoredPaths?.length || 0,
640
+ impactDrift: result.pack.impactDrift || null,
620
641
  reviewBudget: result.pack.reviewBudget
621
642
  };
622
643
  }