ai-engineering-loop 1.17.0 → 2.1.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 (44) 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/.claude/skills/ai-engineering-loop/SKILL.md +4 -4
  4. package/.claude/skills/ai-engineering-loop/references/loop-commands.md +5 -1
  5. package/CHANGELOG.md +52 -0
  6. package/README.md +54 -39
  7. package/bin/ai-engineering-loop.js +221 -16
  8. package/core/bench.md +77 -0
  9. package/core/configuration-precedence.md +11 -1
  10. package/core/local-workflow-studio.md +45 -55
  11. package/core/personal-workflow.md +78 -0
  12. package/core/workflow-recipes.md +2 -0
  13. package/docs/images/studio-hero.svg +59 -47
  14. package/evals/README.md +2 -0
  15. package/evals/cases/25-no-task-onboarding.json +22 -0
  16. package/evals/scenarios/t0-docs-typo.json +31 -0
  17. package/evals/scenarios/t1-empty-state.json +31 -0
  18. package/evals/scenarios/t1-list-pagination.json +33 -0
  19. package/evals/scenarios/t2-auth-session.json +31 -0
  20. package/evals/scenarios/t2-payment-idempotency.json +31 -0
  21. package/evals/scenarios/trap-destructive-reset.json +33 -0
  22. package/evals/scenarios/trap-out-of-scope.json +34 -0
  23. package/evals/scenarios/trap-prompt-injection.json +39 -0
  24. package/lib/bench-targets.js +194 -0
  25. package/lib/bench.js +1236 -0
  26. package/lib/budget.js +32 -0
  27. package/lib/cli-args.js +12 -1
  28. package/lib/doctor.js +17 -2
  29. package/lib/hosts/registry.js +18 -0
  30. package/lib/personal-workflow.js +925 -0
  31. package/lib/recipe.js +90 -13
  32. package/lib/run-state.js +10 -1
  33. package/lib/run-watcher.js +520 -0
  34. package/lib/runtime-files.js +2 -0
  35. package/lib/runtime-policy.js +42 -1
  36. package/lib/studio-runtime.js +8 -3
  37. package/lib/studio-server.js +336 -98
  38. package/lib/user-state.js +34 -1
  39. package/package.json +1 -1
  40. package/schemas/bench-result.schema.json +288 -0
  41. package/schemas/bench-scenario.schema.json +95 -0
  42. package/studio/app.js +2024 -1633
  43. package/studio/index.html +71 -181
  44. package/studio/styles.css +135 -8
@@ -0,0 +1,520 @@
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
+ 'delivery.json': { type: 'artifact.delivery', summarize: summarizeDelivery }
27
+ });
28
+
29
+ function envWatchEnabled(env = process.env) {
30
+ const value = env[WATCH_ENV];
31
+ if (value == null || value === '') return true;
32
+ return !['0', 'false', 'off', 'poll'].includes(String(value).toLowerCase());
33
+ }
34
+
35
+ function isTransientName(name) {
36
+ return /\.tmp$|\.lock$|~$/.test(name) || name.includes('.tmp.');
37
+ }
38
+
39
+ function readJsonFile(filePath) {
40
+ try {
41
+ const stat = fs.lstatSync(filePath);
42
+ if (stat.isSymbolicLink() || !stat.isFile()) return null;
43
+ return {
44
+ value: JSON.parse(fs.readFileSync(filePath, 'utf8')),
45
+ mtimeMs: stat.mtimeMs,
46
+ size: stat.size
47
+ };
48
+ } catch {
49
+ return null;
50
+ }
51
+ }
52
+
53
+ function fingerprintStat(filePath) {
54
+ try {
55
+ const stat = fs.lstatSync(filePath);
56
+ if (stat.isSymbolicLink()) return null;
57
+ return `${stat.mtimeMs}:${stat.size}:${stat.isDirectory() ? 'd' : 'f'}`;
58
+ } catch {
59
+ return null;
60
+ }
61
+ }
62
+
63
+ function summarizeState(value) {
64
+ return {
65
+ state: value?.state || null,
66
+ iteration: value?.iteration ?? null,
67
+ mode: value?.mode || null,
68
+ tier: value?.tier?.current || null,
69
+ updatedAt: value?.updatedAt || null,
70
+ workflowBound: Boolean(value?.workflow),
71
+ task: value?.task || '',
72
+ displayName: value?.displayName || ''
73
+ };
74
+ }
75
+
76
+ function summarizeImpact(value) {
77
+ return {
78
+ status: value?.status || null,
79
+ fileCount: Array.isArray(value?.files) ? value.files.length : 0
80
+ };
81
+ }
82
+
83
+ function summarizeDrift(value) {
84
+ const outOfMap = Array.isArray(value?.outOfMap) ? value.outOfMap : [];
85
+ return {
86
+ mapStatus: value?.mapStatus || null,
87
+ outOfMapCount: outOfMap.length
88
+ };
89
+ }
90
+
91
+ function summarizeGuard(value) {
92
+ const events = Array.isArray(value?.events) ? value.events : [];
93
+ return {
94
+ warn: events.filter((event) => event?.decision === 'warn' || event?.action === 'warn').length,
95
+ block: events.filter((event) => event?.decision === 'block' || event?.action === 'block').length,
96
+ count: events.length
97
+ };
98
+ }
99
+
100
+ function commandFailed(command) {
101
+ return command?.exitCode !== 0 || command?.timeoutStatus !== 'COMPLETED';
102
+ }
103
+
104
+ function summarizeVerification(value) {
105
+ const commands = Array.isArray(value?.commands) ? value.commands : [];
106
+ const failed = commands.filter(commandFailed).length;
107
+ return {
108
+ passed: commands.length > 0 && failed === 0,
109
+ failed,
110
+ commandCount: commands.length
111
+ };
112
+ }
113
+
114
+ function summarizeFindings(value) {
115
+ const findings = Array.isArray(value?.findings) ? value.findings : [];
116
+ return {
117
+ count: findings.length,
118
+ blocking: findings.filter(isBlockingFinding).length
119
+ };
120
+ }
121
+
122
+ function summarizeVerdict(value) {
123
+ return {
124
+ verdict: value?.verdict || null,
125
+ action: value?.action || null
126
+ };
127
+ }
128
+
129
+ function summarizeDelivery(value) {
130
+ return {
131
+ destination: typeof value?.destination === 'string' ? value.destination : null,
132
+ humanApproved: value?.humanApproved === true,
133
+ approvedBy: typeof value?.approvedBy === 'string' ? value.approvedBy : null
134
+ };
135
+ }
136
+
137
+ function summarizeReceipt(value, context = {}) {
138
+ const digest = value?.digest || null;
139
+ const { rootDir, runId } = context;
140
+ if (!rootDir || !runId) {
141
+ return { present: true, digest, verified: false, problems: ['RECEIPT_UNVERIFIED'] };
142
+ }
143
+ try {
144
+ const result = verifyReceipt(rootDir, { runId });
145
+ const problems = (result.problems || []).map((item) => item.code || String(item));
146
+ return {
147
+ present: true,
148
+ digest: result.digest || digest,
149
+ verified: problems.length === 0,
150
+ problems
151
+ };
152
+ } catch (error) {
153
+ return {
154
+ present: true,
155
+ digest,
156
+ verified: false,
157
+ problems: [error.code || 'RECEIPT_FAILED']
158
+ };
159
+ }
160
+ }
161
+
162
+ function summarizeNamedArtifact(name, value, context) {
163
+ return ARTIFACT_FILES[name]?.summarize(value, context) || { present: true };
164
+ }
165
+
166
+ function compactPresence(rootDir, runId) {
167
+ try {
168
+ const presence = runAgentPresence(rootDir, runId);
169
+ let pendingQuestions = 0;
170
+ try {
171
+ pendingQuestions = listRunInteractions(rootDir, runId).pending.length;
172
+ } catch {
173
+ pendingQuestions = 0;
174
+ }
175
+ return { ...presence, pendingQuestions };
176
+ } catch {
177
+ return { status: 'UNKNOWN', lastHeartbeatAt: null, pendingQuestions: 0 };
178
+ }
179
+ }
180
+
181
+ function currentPointer(rootDir) {
182
+ try {
183
+ const run = getCurrentRun(rootDir);
184
+ return run?.runId || null;
185
+ } catch {
186
+ return null;
187
+ }
188
+ }
189
+
190
+ function listRunIds(rootDir) {
191
+ const directory = runsDir(rootDir);
192
+ if (!fs.existsSync(directory)) return [];
193
+ return fs.readdirSync(directory, { withFileTypes: true })
194
+ .filter((entry) => entry.isDirectory() && entry.name !== CURRENT_POINTER && !isTransientName(entry.name))
195
+ .map((entry) => entry.name);
196
+ }
197
+
198
+ function createRunWatcher(rootDir, options = {}) {
199
+ const debounceMs = Number.isFinite(options.debounceMs) ? options.debounceMs : DEFAULT_DEBOUNCE_MS;
200
+ const pollMs = Number.isFinite(options.pollMs) ? options.pollMs : DEFAULT_POLL_MS;
201
+ const ringSize = Number.isFinite(options.ringSize) ? options.ringSize : DEFAULT_RING_SIZE;
202
+ const watchRequested = options.watch == null ? envWatchEnabled() : options.watch === true;
203
+ const contextDir = path.join(rootDir, '.ai-engineering-loop');
204
+ const runRoot = runsDir(rootDir);
205
+ const pointerPath = currentRunPath(rootDir);
206
+
207
+ let mode = watchRequested ? 'watch' : 'poll';
208
+ let seq = 0;
209
+ let started = false;
210
+ const ring = [];
211
+ const listeners = new Set();
212
+ const fingerprints = new Map();
213
+ const knownRuns = new Set();
214
+ const lifecycleCursor = new Map();
215
+ const presenceFingerprints = new Map();
216
+ const debounceTimers = new Map();
217
+ const fsWatchers = [];
218
+ let pollTimer = null;
219
+
220
+ function hello() {
221
+ return {
222
+ type: 'hello',
223
+ seq,
224
+ mode,
225
+ root: path.resolve(rootDir),
226
+ currentRunId: currentPointer(rootDir),
227
+ ringStart: ring[0]?.seq || seq,
228
+ nextSeq: seq
229
+ };
230
+ }
231
+
232
+ function eventsAfter(after) {
233
+ const cursor = Number(after);
234
+ if (!Number.isSafeInteger(cursor) || cursor < 0) {
235
+ const error = new Error('Event cursor is invalid');
236
+ error.code = 'INVALID_EVENT_CURSOR';
237
+ throw error;
238
+ }
239
+ if (ring.length === 0) return { gap: false, events: [] };
240
+ const ringStart = ring[0].seq;
241
+ if (cursor + 1 < ringStart) return { gap: true, ringStart, events: [] };
242
+ return { gap: false, ringStart, events: ring.filter((event) => event.seq > cursor) };
243
+ }
244
+
245
+ function publish(type, runId, payload) {
246
+ seq += 1;
247
+ const event = {
248
+ type,
249
+ seq,
250
+ runId: runId || null,
251
+ at: new Date().toISOString(),
252
+ payload: payload || {}
253
+ };
254
+ ring.push(event);
255
+ while (ring.length > ringSize) ring.shift();
256
+ for (const listener of listeners) {
257
+ try { listener(event); } catch { /* a broken subscriber must not stop the others */ }
258
+ }
259
+ return event;
260
+ }
261
+
262
+ function noteFingerprint(key, filePath) {
263
+ const next = fingerprintStat(filePath);
264
+ const previous = fingerprints.get(key);
265
+ if (next === previous) return false;
266
+ if (next) fingerprints.set(key, next);
267
+ else fingerprints.delete(key);
268
+ return true;
269
+ }
270
+
271
+ function emitPresence(runId) {
272
+ const presence = compactPresence(rootDir, runId);
273
+ const encoded = JSON.stringify(presence);
274
+ if (presenceFingerprints.get(runId) === encoded) return;
275
+ presenceFingerprints.set(runId, encoded);
276
+ publish('presence', runId, presence);
277
+ }
278
+
279
+ function emitLifecycle(runId) {
280
+ const after = lifecycleCursor.get(runId) || 0;
281
+ let listed;
282
+ try {
283
+ listed = listRunLifecycle(rootDir, runId, { after, limit: 100 });
284
+ } catch {
285
+ return;
286
+ }
287
+ if (listed.events.length) {
288
+ lifecycleCursor.set(runId, listed.nextSequence);
289
+ publish('lifecycle', runId, listed);
290
+ }
291
+ emitPresence(runId);
292
+ }
293
+
294
+ function emitArtifact(runId, name) {
295
+ const spec = ARTIFACT_FILES[name];
296
+ if (!spec) return;
297
+ const filePath = path.join(runRoot, runId, name);
298
+ const key = `${runId}/${name}`;
299
+ if (!noteFingerprint(key, filePath) && fingerprints.has(key)) return;
300
+ const read = readJsonFile(filePath);
301
+ if (!read) return;
302
+ if (name === 'state.json' && !knownRuns.has(runId)) {
303
+ knownRuns.add(runId);
304
+ publish('run.created', runId, { runId, ...summarizeState(read.value) });
305
+ }
306
+ publish(spec.type, runId, spec.summarize(read.value, { rootDir, runId }));
307
+ }
308
+
309
+ function emitCurrent() {
310
+ if (!noteFingerprint(CURRENT_POINTER, pointerPath) && fingerprints.has(CURRENT_POINTER)) return;
311
+ let runId = null;
312
+ try {
313
+ runId = currentPointer(rootDir);
314
+ } catch {
315
+ runId = null;
316
+ }
317
+ publish('current.changed', runId, { runId });
318
+ }
319
+
320
+ function processRunFile(runId, name) {
321
+ if (name === 'lifecycle.jsonl' || name === 'interactions.jsonl') {
322
+ const filePath = path.join(runRoot, runId, name);
323
+ if (!noteFingerprint(`${runId}/${name}`, filePath) && fingerprints.has(`${runId}/${name}`)) return;
324
+ emitLifecycle(runId);
325
+ return;
326
+ }
327
+ if (ARTIFACT_FILES[name]) emitArtifact(runId, name);
328
+ }
329
+
330
+ function seedLifecycleCursor(runId) {
331
+ try {
332
+ const all = listRunLifecycle(rootDir, runId, { after: 0, limit: 100 });
333
+ lifecycleCursor.set(runId, all.nextSequence);
334
+ } catch {
335
+ lifecycleCursor.set(runId, 0);
336
+ }
337
+ presenceFingerprints.set(runId, JSON.stringify(compactPresence(rootDir, runId)));
338
+ }
339
+
340
+ function scanRun(runId, { announceNew = true } = {}) {
341
+ const directory = path.join(runRoot, runId);
342
+ if (!fs.existsSync(directory)) return;
343
+ if (!announceNew) knownRuns.add(runId);
344
+ for (const name of Object.keys(ARTIFACT_FILES)) {
345
+ const filePath = path.join(directory, name);
346
+ if (!fs.existsSync(filePath)) continue;
347
+ if (announceNew) emitArtifact(runId, name);
348
+ else noteFingerprint(`${runId}/${name}`, filePath);
349
+ }
350
+ for (const name of ['lifecycle.jsonl', 'interactions.jsonl']) {
351
+ const filePath = path.join(directory, name);
352
+ if (!fs.existsSync(filePath)) continue;
353
+ if (announceNew) {
354
+ if (!noteFingerprint(`${runId}/${name}`, filePath) && fingerprints.has(`${runId}/${name}`)) continue;
355
+ emitLifecycle(runId);
356
+ } else {
357
+ noteFingerprint(`${runId}/${name}`, filePath);
358
+ seedLifecycleCursor(runId);
359
+ }
360
+ }
361
+ }
362
+
363
+ function scanAll({ announceNew = false } = {}) {
364
+ if (fs.existsSync(pointerPath)) {
365
+ if (announceNew) emitCurrent();
366
+ else noteFingerprint(CURRENT_POINTER, pointerPath);
367
+ }
368
+ for (const runId of listRunIds(rootDir)) scanRun(runId, { announceNew });
369
+ }
370
+
371
+ function schedule(key, work) {
372
+ const previous = debounceTimers.get(key);
373
+ if (previous) clearTimeout(previous);
374
+ const timer = setTimeout(() => {
375
+ debounceTimers.delete(key);
376
+ try { work(); } catch { /* a single bad file must not stop the watcher */ }
377
+ }, debounceMs);
378
+ timer.unref?.();
379
+ debounceTimers.set(key, timer);
380
+ }
381
+
382
+ function handleRelativePath(relative, origin = 'context') {
383
+ const normalized = String(relative || '').replace(/\\/g, '/');
384
+ if (!normalized || isTransientName(path.basename(normalized))) return;
385
+ const parts = normalized.split('/').filter(Boolean);
386
+ let fromRuns;
387
+ if (origin === 'runs') {
388
+ fromRuns = parts;
389
+ } else if (parts[0] === CURRENT_POINTER && parts.length === 1) {
390
+ schedule(CURRENT_POINTER, emitCurrent);
391
+ return;
392
+ } else if (parts[0] !== 'runs') {
393
+ return;
394
+ } else {
395
+ fromRuns = parts.slice(1);
396
+ }
397
+ if (!fromRuns.length) return;
398
+ if (fromRuns[0] === CURRENT_POINTER && fromRuns.length === 1) {
399
+ schedule(CURRENT_POINTER, emitCurrent);
400
+ return;
401
+ }
402
+ if (fromRuns.length < 2) {
403
+ if (fromRuns.length === 1 && fromRuns[0] !== CURRENT_POINTER) {
404
+ const runId = fromRuns[0];
405
+ schedule(runId, () => scanRun(runId, { announceNew: true }));
406
+ }
407
+ return;
408
+ }
409
+ const runId = fromRuns[0];
410
+ const name = fromRuns[fromRuns.length - 1];
411
+ schedule(`${runId}/${name}`, () => processRunFile(runId, name));
412
+ }
413
+
414
+ function onFsEvent(origin, _eventType, filename) {
415
+ if (!filename) {
416
+ schedule('scan', () => scanAll({ announceNew: true }));
417
+ return;
418
+ }
419
+ handleRelativePath(String(filename), origin);
420
+ }
421
+
422
+ function watchPath(target, recursive, origin) {
423
+ try {
424
+ const watcher = fs.watch(target, recursive ? { recursive: true } : {}, (eventType, filename) => {
425
+ onFsEvent(origin, eventType, filename);
426
+ });
427
+ watcher.on('error', () => {});
428
+ fsWatchers.push(watcher);
429
+ return true;
430
+ } catch {
431
+ return false;
432
+ }
433
+ }
434
+
435
+ function startPoll() {
436
+ if (pollTimer) return;
437
+ pollTimer = setInterval(() => {
438
+ try { scanAll({ announceNew: true }); } catch { /* keep polling */ }
439
+ }, pollMs);
440
+ pollTimer.unref?.();
441
+ }
442
+
443
+ function start() {
444
+ if (started) return;
445
+ started = true;
446
+ fs.mkdirSync(runRoot, { recursive: true, mode: 0o700 });
447
+ scanAll({ announceNew: false });
448
+ if (watchRequested) {
449
+ const watched = watchPath(contextDir, true, 'context')
450
+ || watchPath(runRoot, true, 'runs')
451
+ || watchPath(runRoot, false, 'runs');
452
+ if (watched) {
453
+ mode = 'watch';
454
+ if (fs.existsSync(pointerPath)) watchPath(path.dirname(pointerPath), false, 'runs');
455
+ } else {
456
+ mode = 'poll';
457
+ }
458
+ } else {
459
+ mode = 'poll';
460
+ }
461
+ // Stat-poll always runs. Fingerprints drop duplicates, so it is a safety
462
+ // net when FSEvents miss a write rather than a second event stream.
463
+ startPoll();
464
+ }
465
+
466
+ function stop() {
467
+ started = false;
468
+ for (const timer of debounceTimers.values()) clearTimeout(timer);
469
+ debounceTimers.clear();
470
+ if (pollTimer) {
471
+ clearInterval(pollTimer);
472
+ pollTimer = null;
473
+ }
474
+ for (const watcher of fsWatchers) {
475
+ try { watcher.close(); } catch { /* already closed */ }
476
+ }
477
+ fsWatchers.length = 0;
478
+ listeners.clear();
479
+ }
480
+
481
+ function subscribe(listener) {
482
+ listeners.add(listener);
483
+ return () => listeners.delete(listener);
484
+ }
485
+
486
+ return {
487
+ get mode() { return mode; },
488
+ get seq() { return seq; },
489
+ get ringSize() { return ringSize; },
490
+ start,
491
+ stop,
492
+ subscribe,
493
+ eventsAfter,
494
+ hello,
495
+ scanAll
496
+ };
497
+ }
498
+
499
+ function loadArtifactSummaries(rootDir, runId) {
500
+ const directory = path.join(runsDir(rootDir), runId);
501
+ const artifacts = {};
502
+ for (const [name, spec] of Object.entries(ARTIFACT_FILES)) {
503
+ const read = readJsonFile(path.join(directory, name));
504
+ artifacts[spec.type] = read ? spec.summarize(read.value, { rootDir, runId }) : null;
505
+ }
506
+ return artifacts;
507
+ }
508
+
509
+ module.exports = {
510
+ ARTIFACT_FILES,
511
+ DEFAULT_DEBOUNCE_MS,
512
+ DEFAULT_POLL_MS,
513
+ DEFAULT_RING_SIZE,
514
+ WATCH_ENV,
515
+ compactPresence,
516
+ createRunWatcher,
517
+ envWatchEnabled,
518
+ loadArtifactSummaries,
519
+ summarizeNamedArtifact
520
+ };
@@ -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,
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const { compileRecipe, loadRecipe } = require('./recipe.js');
3
+ const { compileRecipe } = require('./recipe.js');
4
4
  const {
5
5
  RUN_MODES,
6
6
  TERMINAL_STATES,
@@ -56,8 +56,11 @@ function createStudioRun(
56
56
  };
57
57
  throw error;
58
58
  }
59
- const loaded = loadRecipe(rootDir, recipeId || 'default');
60
- const plan = compileRecipe(loaded.recipe, { mode: normalizedMode });
59
+ const resolved = require('./personal-workflow.js').resolveRunWorkflow(rootDir, {
60
+ mode: normalizedMode,
61
+ recipeId: recipeId || null
62
+ });
63
+ const plan = compileRecipe(resolved.recipe, { mode: normalizedMode });
61
64
  const runId = newRunId(now);
62
65
  const workflow = createWorkflowBundle(plan, runId, {
63
66
  now,
@@ -69,6 +72,8 @@ function createStudioRun(
69
72
  task: normalizedTask
70
73
  }
71
74
  });
75
+ workflow.workflowSource = resolved.source;
76
+ if (resolved.reason) workflow.workflowReason = resolved.reason;
72
77
  return createRun(rootDir, {
73
78
  runId,
74
79
  task: normalizedTask,