ai-engineering-loop 1.9.2 → 1.9.4
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/workflows/ai-engineering-loop.md +2 -2
- package/.claude/skills/ai-engineering-loop/SKILL.md +3 -1
- package/.gemini/skills/ai-engineering-loop/SKILL.md +3 -1
- package/.grok/skills/ai-engineering-loop/SKILL.md +3 -1
- package/bin/ai-engineering-loop.js +248 -0
- package/lib/goal-runtime.js +149 -15
- package/lib/run-lifecycle.js +164 -0
- package/lib/studio-runtime.js +51 -4
- package/lib/studio-server.js +106 -6
- package/lib/workflow-runtime.js +53 -4
- package/package.json +1 -1
- package/studio/app.js +278 -32
- package/studio/index.html +10 -0
- package/studio/styles.css +6 -4
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const { sha256 } = require('./recipe.js');
|
|
6
|
+
const { assertRunId, loadRun, runsDir } = require('./run-state.js');
|
|
7
|
+
const { redactSecrets } = require('./safe-context.js');
|
|
8
|
+
|
|
9
|
+
const MAX_EVENTS = 2000;
|
|
10
|
+
const MAX_LEDGER_BYTES = 4 * 1024 * 1024;
|
|
11
|
+
const TYPES = new Set([
|
|
12
|
+
'GOAL_DRAFTING',
|
|
13
|
+
'GOAL_DRAFT_UPDATED',
|
|
14
|
+
'GOAL_READY',
|
|
15
|
+
'AGENT_HEARTBEAT',
|
|
16
|
+
'AGENT_WAITING',
|
|
17
|
+
'NODE_STARTED',
|
|
18
|
+
'NODE_ACTIVITY',
|
|
19
|
+
'NODE_COMPLETED',
|
|
20
|
+
'LOOP_ITERATED',
|
|
21
|
+
'RUN_COMPLETED'
|
|
22
|
+
]);
|
|
23
|
+
const ID = /^[A-Za-z0-9@._-]{1,64}$/;
|
|
24
|
+
const LOCK_STALE_MS = 60_000;
|
|
25
|
+
|
|
26
|
+
function lifecycleError(message, code) {
|
|
27
|
+
const error = new Error(message);
|
|
28
|
+
error.code = code;
|
|
29
|
+
return error;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function lifecyclePath(rootDir, runId) {
|
|
33
|
+
assertRunId(runId);
|
|
34
|
+
loadRun(rootDir, runId);
|
|
35
|
+
return path.join(runsDir(rootDir), runId, 'lifecycle.jsonl');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function materialHash(event) {
|
|
39
|
+
const { hash, ...material } = event;
|
|
40
|
+
return sha256(material);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function readLifecycle(rootDir, runId) {
|
|
44
|
+
const file = lifecyclePath(rootDir, runId);
|
|
45
|
+
if (!fs.existsSync(file)) return [];
|
|
46
|
+
const stat = fs.lstatSync(file);
|
|
47
|
+
if (stat.isSymbolicLink() || !stat.isFile() || stat.nlink !== 1 || stat.size > MAX_LEDGER_BYTES) {
|
|
48
|
+
throw lifecycleError('Run lifecycle must be a bounded regular file', 'INVALID_RUN_LIFECYCLE');
|
|
49
|
+
}
|
|
50
|
+
const events = fs.readFileSync(file, 'utf8').split('\n').filter(Boolean).map((line) => {
|
|
51
|
+
try { return JSON.parse(line); } catch { return null; }
|
|
52
|
+
});
|
|
53
|
+
let previousHash = null;
|
|
54
|
+
for (const [index, event] of events.entries()) {
|
|
55
|
+
if (!event || event.runId !== runId || event.sequence !== index + 1 ||
|
|
56
|
+
event.previousHash !== previousHash || event.hash !== materialHash(event) ||
|
|
57
|
+
!TYPES.has(event.type)) {
|
|
58
|
+
throw lifecycleError(`Run lifecycle event ${index + 1} failed integrity validation`, 'INVALID_RUN_LIFECYCLE');
|
|
59
|
+
}
|
|
60
|
+
previousHash = event.hash;
|
|
61
|
+
}
|
|
62
|
+
return events;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function withLock(rootDir, runId, operation) {
|
|
66
|
+
const lock = `${lifecyclePath(rootDir, runId)}.lock`;
|
|
67
|
+
let descriptor;
|
|
68
|
+
try {
|
|
69
|
+
try {
|
|
70
|
+
descriptor = fs.openSync(lock, 'wx', 0o600);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
if (error.code !== 'EEXIST') throw error;
|
|
73
|
+
const stat = fs.lstatSync(lock);
|
|
74
|
+
if (stat.isSymbolicLink() || !stat.isFile() || Date.now() - stat.mtimeMs <= LOCK_STALE_MS) {
|
|
75
|
+
throw lifecycleError('Another lifecycle update is in progress', 'RUN_LIFECYCLE_BUSY');
|
|
76
|
+
}
|
|
77
|
+
fs.unlinkSync(lock);
|
|
78
|
+
descriptor = fs.openSync(lock, 'wx', 0o600);
|
|
79
|
+
}
|
|
80
|
+
return operation();
|
|
81
|
+
} finally {
|
|
82
|
+
if (descriptor != null) {
|
|
83
|
+
fs.closeSync(descriptor);
|
|
84
|
+
try { fs.unlinkSync(lock); } catch {}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function appendRunLifecycle(rootDir, runId, event, { now = new Date() } = {}) {
|
|
90
|
+
return withLock(rootDir, runId, () => {
|
|
91
|
+
if (!TYPES.has(event?.type)) {
|
|
92
|
+
throw lifecycleError('Lifecycle type is not allowed', 'INVALID_LIFECYCLE_TYPE');
|
|
93
|
+
}
|
|
94
|
+
const actor = String(event.actor || 'agent').trim();
|
|
95
|
+
if (!ID.test(actor)) throw lifecycleError('Lifecycle actor is invalid', 'INVALID_LIFECYCLE_ACTOR');
|
|
96
|
+
const phase = event.phase == null ? null : String(event.phase).trim();
|
|
97
|
+
const nodeId = event.nodeId == null ? null : String(event.nodeId).trim();
|
|
98
|
+
if ((phase && !ID.test(phase)) || (nodeId && !ID.test(nodeId))) {
|
|
99
|
+
throw lifecycleError('Lifecycle phase or node is invalid', 'INVALID_LIFECYCLE_SCOPE');
|
|
100
|
+
}
|
|
101
|
+
const message = redactSecrets(String(event.message || '').trim()).text;
|
|
102
|
+
if (!message || message.length > 240 ||
|
|
103
|
+
/[\u0000-\u0008\u000b\u000c\u000e-\u001f]/.test(message)) {
|
|
104
|
+
throw lifecycleError('Lifecycle message must be 1-240 printable characters', 'INVALID_LIFECYCLE_MESSAGE');
|
|
105
|
+
}
|
|
106
|
+
const events = readLifecycle(rootDir, runId);
|
|
107
|
+
if (events.length >= MAX_EVENTS) {
|
|
108
|
+
throw lifecycleError('Run lifecycle event limit reached', 'RUN_LIFECYCLE_LIMIT');
|
|
109
|
+
}
|
|
110
|
+
const material = {
|
|
111
|
+
schemaVersion: 1,
|
|
112
|
+
runId,
|
|
113
|
+
sequence: events.length + 1,
|
|
114
|
+
previousHash: events.at(-1)?.hash || null,
|
|
115
|
+
at: now.toISOString(),
|
|
116
|
+
type: event.type,
|
|
117
|
+
actor,
|
|
118
|
+
message,
|
|
119
|
+
...(phase ? { phase } : {}),
|
|
120
|
+
...(nodeId ? { nodeId } : {})
|
|
121
|
+
};
|
|
122
|
+
const result = { ...material, hash: sha256(material) };
|
|
123
|
+
fs.appendFileSync(lifecyclePath(rootDir, runId), `${JSON.stringify(result)}\n`, {
|
|
124
|
+
encoding: 'utf8',
|
|
125
|
+
mode: 0o600,
|
|
126
|
+
flag: 'a'
|
|
127
|
+
});
|
|
128
|
+
return result;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function listRunLifecycle(rootDir, runId, { after = 0, limit = 100 } = {}) {
|
|
133
|
+
const cursor = Number(after);
|
|
134
|
+
const pageSize = Number(limit);
|
|
135
|
+
if (!Number.isSafeInteger(cursor) || cursor < 0 ||
|
|
136
|
+
!Number.isSafeInteger(pageSize) || pageSize < 1 || pageSize > 100) {
|
|
137
|
+
throw lifecycleError('Lifecycle cursor or limit is invalid', 'INVALID_LIFECYCLE_CURSOR');
|
|
138
|
+
}
|
|
139
|
+
const events = readLifecycle(rootDir, runId).filter((event) => event.sequence > cursor).slice(0, pageSize);
|
|
140
|
+
return {
|
|
141
|
+
runId,
|
|
142
|
+
events,
|
|
143
|
+
nextSequence: events.at(-1)?.sequence || cursor
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function runAgentPresence(rootDir, runId, { now = new Date() } = {}) {
|
|
148
|
+
const events = readLifecycle(rootDir, runId);
|
|
149
|
+
const last = events.findLast((event) => event.type === 'AGENT_HEARTBEAT');
|
|
150
|
+
if (!last) return { status: 'UNKNOWN', lastHeartbeatAt: null };
|
|
151
|
+
const ageMs = Math.max(0, now.getTime() - Date.parse(last.at));
|
|
152
|
+
return {
|
|
153
|
+
status: ageMs < 10_000 ? 'WORKING' : ageMs <= 30_000 ? 'IDLE' : 'DISCONNECTED',
|
|
154
|
+
lastHeartbeatAt: last.at,
|
|
155
|
+
ageMs
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
module.exports = {
|
|
160
|
+
MAX_EVENTS,
|
|
161
|
+
appendRunLifecycle,
|
|
162
|
+
listRunLifecycle,
|
|
163
|
+
runAgentPresence
|
|
164
|
+
};
|
package/lib/studio-runtime.js
CHANGED
|
@@ -6,11 +6,17 @@ const {
|
|
|
6
6
|
TERMINAL_STATES,
|
|
7
7
|
createRun,
|
|
8
8
|
getCurrentRun,
|
|
9
|
+
loadRun,
|
|
9
10
|
newRunId,
|
|
10
|
-
normalizeRunMode
|
|
11
|
+
normalizeRunMode,
|
|
12
|
+
transitionRun
|
|
11
13
|
} = require('./run-state.js');
|
|
12
14
|
const { assertModeAllowed } = require('./runtime-policy.js');
|
|
13
|
-
const {
|
|
15
|
+
const {
|
|
16
|
+
cancelWorkflowRun,
|
|
17
|
+
createWorkflowBundle,
|
|
18
|
+
startWorkflowExecution
|
|
19
|
+
} = require('./workflow-runtime.js');
|
|
14
20
|
|
|
15
21
|
function studioRuntimeError(message, code) {
|
|
16
22
|
const error = new Error(message);
|
|
@@ -37,10 +43,18 @@ function createStudioRun(
|
|
|
37
43
|
assertModeAllowed(rootDir, normalizedMode);
|
|
38
44
|
const current = getCurrentRun(rootDir);
|
|
39
45
|
if (current && !TERMINAL_STATES.has(current.state)) {
|
|
40
|
-
|
|
46
|
+
const error = studioRuntimeError(
|
|
41
47
|
`Run ${current.runId} is already active; finish it before creating a sibling execution`,
|
|
42
48
|
'ACTIVE_RUN'
|
|
43
49
|
);
|
|
50
|
+
error.activeRun = {
|
|
51
|
+
runId: current.runId,
|
|
52
|
+
displayName: current.displayName,
|
|
53
|
+
task: current.task,
|
|
54
|
+
state: current.state,
|
|
55
|
+
updatedAt: current.updatedAt
|
|
56
|
+
};
|
|
57
|
+
throw error;
|
|
44
58
|
}
|
|
45
59
|
const loaded = loadRecipe(rootDir, recipeId);
|
|
46
60
|
const plan = compileRecipe(loaded.recipe, { mode: normalizedMode });
|
|
@@ -70,4 +84,37 @@ function executeStudioRun(rootDir, runId, { now = new Date() } = {}) {
|
|
|
70
84
|
return startWorkflowExecution(rootDir, { runId, now });
|
|
71
85
|
}
|
|
72
86
|
|
|
73
|
-
|
|
87
|
+
function cancelStudioRun(
|
|
88
|
+
rootDir,
|
|
89
|
+
runId,
|
|
90
|
+
{ actor = 'human', reason = 'Replaced by a new Goal', now = new Date() } = {}
|
|
91
|
+
) {
|
|
92
|
+
const run = loadRun(rootDir, runId);
|
|
93
|
+
if (TERMINAL_STATES.has(run.state)) {
|
|
94
|
+
throw studioRuntimeError(`Run ${runId} is already finished`, 'RUN_ALREADY_FINISHED');
|
|
95
|
+
}
|
|
96
|
+
const cleanActor = String(actor || '').trim();
|
|
97
|
+
const cleanReason = String(reason || '').trim();
|
|
98
|
+
if (!/^[A-Za-z0-9@._-]{1,64}$/.test(cleanActor)) {
|
|
99
|
+
throw studioRuntimeError('Cancellation actor must be a short account identifier', 'INVALID_RUN_ACTOR');
|
|
100
|
+
}
|
|
101
|
+
if (!cleanReason || cleanReason.length > 500) {
|
|
102
|
+
throw studioRuntimeError('Cancellation reason must be 1-500 characters', 'INVALID_CANCEL_REASON');
|
|
103
|
+
}
|
|
104
|
+
if (run.workflow) {
|
|
105
|
+
return cancelWorkflowRun(rootDir, {
|
|
106
|
+
runId,
|
|
107
|
+
actor: cleanActor,
|
|
108
|
+
reason: cleanReason,
|
|
109
|
+
now
|
|
110
|
+
}).run;
|
|
111
|
+
}
|
|
112
|
+
return transitionRun(rootDir, runId, 'CANCELLED', {
|
|
113
|
+
gate: 'run-cancelled',
|
|
114
|
+
reason: cleanReason,
|
|
115
|
+
historyMetadata: { actor: cleanActor },
|
|
116
|
+
now
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
module.exports = { cancelStudioRun, createStudioRun, executeStudioRun };
|
package/lib/studio-server.js
CHANGED
|
@@ -38,8 +38,9 @@ const {
|
|
|
38
38
|
appendRunQuestion,
|
|
39
39
|
listRunInteractions
|
|
40
40
|
} = require('./run-interactions.js');
|
|
41
|
-
const { saveGoalDraft, freezeGoal, unfreezeGoal } = require('./goal-runtime.js');
|
|
42
|
-
const {
|
|
41
|
+
const { saveGoalDraft, loadGoalDraft, freezeGoal, unfreezeGoal } = require('./goal-runtime.js');
|
|
42
|
+
const { appendRunLifecycle, listRunLifecycle, runAgentPresence } = require('./run-lifecycle.js');
|
|
43
|
+
const { cancelStudioRun, createStudioRun, executeStudioRun } = require('./studio-runtime.js');
|
|
43
44
|
const { createHandoff, handoffBrief, recordDecision } = require('./handoff.js');
|
|
44
45
|
const { loadStudioLayout, saveStudioLayout } = require('./studio-layout.js');
|
|
45
46
|
const {
|
|
@@ -71,6 +72,35 @@ function readBody(request) {
|
|
|
71
72
|
});
|
|
72
73
|
}
|
|
73
74
|
|
|
75
|
+
function streamRunLifecycle(rootDir, runId, after, request, response) {
|
|
76
|
+
let cursor = after;
|
|
77
|
+
response.writeHead(200, {
|
|
78
|
+
'content-type': 'text/event-stream; charset=utf-8',
|
|
79
|
+
'cache-control': 'no-store',
|
|
80
|
+
connection: 'keep-alive'
|
|
81
|
+
});
|
|
82
|
+
const publish = () => {
|
|
83
|
+
try {
|
|
84
|
+
const lifecycle = listRunLifecycle(rootDir, runId, { after: cursor, limit: 100 });
|
|
85
|
+
cursor = lifecycle.nextSequence;
|
|
86
|
+
response.write(`data: ${JSON.stringify({
|
|
87
|
+
lifecycle,
|
|
88
|
+
presence: runAgentPresence(rootDir, runId)
|
|
89
|
+
})}\n\n`);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
response.write(`event: error\ndata: ${JSON.stringify({
|
|
92
|
+
code: error.code || 'LIFECYCLE_STREAM_FAILED',
|
|
93
|
+
error: error.message
|
|
94
|
+
})}\n\n`);
|
|
95
|
+
response.end();
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
publish();
|
|
99
|
+
const timer = setInterval(publish, 750);
|
|
100
|
+
timer.unref?.();
|
|
101
|
+
request.on('close', () => clearInterval(timer));
|
|
102
|
+
}
|
|
103
|
+
|
|
74
104
|
function liveSnapshot(rootDir) {
|
|
75
105
|
const run = getCurrentRun(rootDir);
|
|
76
106
|
if (!run?.workflow) return null;
|
|
@@ -150,7 +180,10 @@ function createStudioServer(rootDir, { token = crypto.randomBytes(24).toString('
|
|
|
150
180
|
const scopedRun = url.pathname.match(/^\/api\/runs\/([^/]+)$/);
|
|
151
181
|
const scopedArtifact = url.pathname.match(/^\/api\/runs\/([^/]+)\/artifacts$/);
|
|
152
182
|
const scopedExecute = url.pathname.match(/^\/api\/runs\/([^/]+)\/execute$/);
|
|
183
|
+
const scopedCancel = url.pathname.match(/^\/api\/runs\/([^/]+)\/cancel$/);
|
|
153
184
|
const scopedGoal = url.pathname.match(/^\/api\/runs\/([^/]+)\/goal$/);
|
|
185
|
+
const scopedLifecycle = url.pathname.match(/^\/api\/runs\/([^/]+)\/lifecycle$/);
|
|
186
|
+
const scopedLifecycleStream = url.pathname.match(/^\/api\/runs\/([^/]+)\/lifecycle\/stream$/);
|
|
154
187
|
const scopedGoalFreeze = url.pathname.match(/^\/api\/runs\/([^/]+)\/goal\/freeze$/);
|
|
155
188
|
const scopedGoalUnfreeze = url.pathname.match(/^\/api\/runs\/([^/]+)\/goal\/unfreeze$/);
|
|
156
189
|
const scopedCheckout = url.pathname.match(/^\/api\/runs\/([^/]+)\/checkout$/);
|
|
@@ -247,6 +280,25 @@ function createStudioServer(rootDir, { token = crypto.randomBytes(24).toString('
|
|
|
247
280
|
ok: true,
|
|
248
281
|
interactions: listRunInteractions(rootDir, decodeURIComponent(scopedInteractions[1]))
|
|
249
282
|
});
|
|
283
|
+
} else if (request.method === 'GET' && scopedLifecycleStream) {
|
|
284
|
+
const runId = decodeURIComponent(scopedLifecycleStream[1]);
|
|
285
|
+
const after = Number(url.searchParams.get('after') || 0);
|
|
286
|
+
if (!Number.isSafeInteger(after) || after < 0) {
|
|
287
|
+
throw Object.assign(new Error('Lifecycle stream cursor is invalid'), {
|
|
288
|
+
code: 'INVALID_LIFECYCLE_CURSOR'
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
streamRunLifecycle(rootDir, runId, after, request, response);
|
|
292
|
+
} else if (request.method === 'GET' && scopedLifecycle) {
|
|
293
|
+
const runId = decodeURIComponent(scopedLifecycle[1]);
|
|
294
|
+
json(response, 200, {
|
|
295
|
+
ok: true,
|
|
296
|
+
lifecycle: listRunLifecycle(rootDir, runId, {
|
|
297
|
+
after: Number(url.searchParams.get('after') || 0),
|
|
298
|
+
limit: Number(url.searchParams.get('limit') || 100)
|
|
299
|
+
}),
|
|
300
|
+
presence: runAgentPresence(rootDir, runId)
|
|
301
|
+
});
|
|
250
302
|
} else if (request.method === 'POST' && scopedQuestions) {
|
|
251
303
|
const body = await readBody(request);
|
|
252
304
|
json(response, 201, {
|
|
@@ -277,13 +329,48 @@ function createStudioServer(rootDir, { token = crypto.randomBytes(24).toString('
|
|
|
277
329
|
nodes: result.workflow.state.nodes,
|
|
278
330
|
budget: result.budget
|
|
279
331
|
});
|
|
332
|
+
} else if (request.method === 'GET' && scopedGoal) {
|
|
333
|
+
json(response, 200, {
|
|
334
|
+
ok: true,
|
|
335
|
+
draft: loadGoalDraft(rootDir, decodeURIComponent(scopedGoal[1]))
|
|
336
|
+
});
|
|
280
337
|
} else if (request.method === 'PUT' && scopedGoal) {
|
|
281
338
|
const body = await readBody(request);
|
|
282
|
-
const
|
|
283
|
-
|
|
339
|
+
const runId = decodeURIComponent(scopedGoal[1]);
|
|
340
|
+
const actor = body.actor || 'studio-user';
|
|
341
|
+
listRunLifecycle(rootDir, runId);
|
|
342
|
+
const draft = saveGoalDraft(rootDir, runId, body.goal, {
|
|
343
|
+
actor,
|
|
344
|
+
expectedRevision: body.expectedRevision
|
|
345
|
+
});
|
|
346
|
+
appendRunLifecycle(rootDir, runId, {
|
|
347
|
+
type: 'GOAL_DRAFT_UPDATED',
|
|
348
|
+
phase: 'goal',
|
|
349
|
+
actor,
|
|
350
|
+
message: `Goal draft revision ${draft.metadata.revision} was saved in Studio`
|
|
351
|
+
});
|
|
352
|
+
appendRunLifecycle(rootDir, runId, {
|
|
353
|
+
type: 'GOAL_READY',
|
|
354
|
+
phase: 'goal',
|
|
355
|
+
actor,
|
|
356
|
+
message: `Goal draft revision ${draft.metadata.revision} is ready for review`
|
|
357
|
+
});
|
|
358
|
+
json(response, 200, { ok: true, draft, frozen: false });
|
|
284
359
|
} else if (request.method === 'POST' && scopedGoalFreeze) {
|
|
285
360
|
const body = await readBody(request);
|
|
286
|
-
const
|
|
361
|
+
const runId = decodeURIComponent(scopedGoalFreeze[1]);
|
|
362
|
+
listRunLifecycle(rootDir, runId);
|
|
363
|
+
const run = freezeGoal(rootDir, runId, {
|
|
364
|
+
actor: body.actor,
|
|
365
|
+
expectedRevision: body.expectedRevision,
|
|
366
|
+
expectedHash: body.expectedHash
|
|
367
|
+
});
|
|
368
|
+
appendRunLifecycle(rootDir, runId, {
|
|
369
|
+
type: 'NODE_COMPLETED',
|
|
370
|
+
phase: 'goal',
|
|
371
|
+
actor: body.actor || 'studio-user',
|
|
372
|
+
message: 'Goal Contract frozen in Studio'
|
|
373
|
+
});
|
|
287
374
|
json(response, 200, { ok: true, run });
|
|
288
375
|
} else if (request.method === 'POST' && scopedGoalUnfreeze) {
|
|
289
376
|
const body = await readBody(request);
|
|
@@ -307,6 +394,13 @@ function createStudioServer(rootDir, { token = crypto.randomBytes(24).toString('
|
|
|
307
394
|
const body = await readBody(request);
|
|
308
395
|
const run = createStudioRun(rootDir, body);
|
|
309
396
|
json(response, 201, { ok: true, run });
|
|
397
|
+
} else if (request.method === 'POST' && scopedCancel) {
|
|
398
|
+
const body = await readBody(request);
|
|
399
|
+
const run = cancelStudioRun(rootDir, decodeURIComponent(scopedCancel[1]), {
|
|
400
|
+
actor: body.actor || 'human',
|
|
401
|
+
reason: body.reason || 'Replaced by a new Goal'
|
|
402
|
+
});
|
|
403
|
+
json(response, 200, { ok: true, run });
|
|
310
404
|
} else if (request.method === 'PATCH' && url.pathname === '/api/run/name') {
|
|
311
405
|
const body = await readBody(request);
|
|
312
406
|
const run = updateRunDisplayName(rootDir, body.runId, body.displayName, {
|
|
@@ -470,7 +564,13 @@ function createStudioServer(rootDir, { token = crypto.randomBytes(24).toString('
|
|
|
470
564
|
response.end(fs.readFileSync(path.join(ASSET_DIR, asset)));
|
|
471
565
|
}
|
|
472
566
|
} catch (error) {
|
|
473
|
-
json(response, 400, {
|
|
567
|
+
json(response, 400, {
|
|
568
|
+
ok: false,
|
|
569
|
+
code: error.code || 'STUDIO_ERROR',
|
|
570
|
+
error: error.message,
|
|
571
|
+
details: error.details || [],
|
|
572
|
+
...(error.activeRun ? { activeRun: error.activeRun } : {})
|
|
573
|
+
});
|
|
474
574
|
}
|
|
475
575
|
});
|
|
476
576
|
return { server, token };
|
package/lib/workflow-runtime.js
CHANGED
|
@@ -857,8 +857,8 @@ function startWorkflowNode(rootDir, nodeId, { runId, now = new Date() } = {}) {
|
|
|
857
857
|
if (node.type === 'command') {
|
|
858
858
|
throw runtimeError('Command nodes are disabled in the controlled runtime', 'COMMAND_EXECUTION_DISABLED');
|
|
859
859
|
}
|
|
860
|
-
if (node.type === 'approval'
|
|
861
|
-
throw runtimeError(`Node ${nodeId} must use its dedicated approval
|
|
860
|
+
if (node.type === 'approval') {
|
|
861
|
+
throw runtimeError(`Node ${nodeId} must use its dedicated approval command`, 'WRONG_NODE_EXECUTOR');
|
|
862
862
|
}
|
|
863
863
|
const changes = nodeChanges(workflow, nodeId, NODE_STATES.RUNNING, {
|
|
864
864
|
attempts: workflow.state.nodes[nodeId].attempts + 1,
|
|
@@ -965,12 +965,13 @@ function applyWorkflowGate(rootDir, gate, { runId, now = new Date(), transitionO
|
|
|
965
965
|
let workflow = loadWorkflow(rootDir, resolved);
|
|
966
966
|
const node = workflow.plan.nodes.find((candidate) => candidate.legacyGate === gate);
|
|
967
967
|
if (!node) throw runtimeError(`Workflow has no node for gate ${gate}`, 'GATE_NOT_IN_WORKFLOW');
|
|
968
|
-
|
|
968
|
+
const before = workflow.state.nodes[node.id];
|
|
969
|
+
if (before.status !== NODE_STATES.RUNNING) assertReady(workflow, node.id);
|
|
969
970
|
const { applyGate } = require('./gates.js');
|
|
970
971
|
const run = applyGate(rootDir, gate, { runId: resolved, transitionOptions });
|
|
971
972
|
workflow.run = run;
|
|
972
973
|
let changes = nodeChanges(workflow, node.id, NODE_STATES.PASSED, {
|
|
973
|
-
attempts:
|
|
974
|
+
attempts: before.attempts + (before.status === NODE_STATES.READY ? 1 : 0),
|
|
974
975
|
reason: null,
|
|
975
976
|
activity: null
|
|
976
977
|
}, now);
|
|
@@ -1047,6 +1048,53 @@ function workflowStatus(rootDir, { runId } = {}) {
|
|
|
1047
1048
|
return loadWorkflow(rootDir, resolveRunId(rootDir, runId));
|
|
1048
1049
|
}
|
|
1049
1050
|
|
|
1051
|
+
function cancelWorkflowRun(
|
|
1052
|
+
rootDir,
|
|
1053
|
+
{ runId, actor = 'human', reason = 'Replaced by a new Goal', now = new Date() } = {}
|
|
1054
|
+
) {
|
|
1055
|
+
const resolved = resolveRunId(rootDir, runId);
|
|
1056
|
+
return withWorkflowLock(rootDir, resolved, () => {
|
|
1057
|
+
let workflow = loadWorkflow(rootDir, resolved);
|
|
1058
|
+
if (!/^[A-Za-z0-9@._-]{1,64}$/.test(String(actor))) {
|
|
1059
|
+
throw runtimeError('Cancellation actor must be a short account identifier', 'INVALID_RUN_ACTOR');
|
|
1060
|
+
}
|
|
1061
|
+
const cleanReason = redactSecrets(String(reason || '').trim()).text;
|
|
1062
|
+
if (!cleanReason || cleanReason.length > 500) {
|
|
1063
|
+
throw runtimeError('Cancellation reason must be 1-500 characters', 'INVALID_CANCEL_REASON');
|
|
1064
|
+
}
|
|
1065
|
+
workflow.run = transitionRun(rootDir, resolved, RUN_STATES.CANCELLED, {
|
|
1066
|
+
gate: 'run-cancelled',
|
|
1067
|
+
reason: cleanReason,
|
|
1068
|
+
historyMetadata: { actor: String(actor) },
|
|
1069
|
+
now
|
|
1070
|
+
});
|
|
1071
|
+
const changes = [];
|
|
1072
|
+
for (const node of workflow.plan.nodes) {
|
|
1073
|
+
const current = workflow.state.nodes[node.id];
|
|
1074
|
+
if (!SUCCESS_STATES.has(current.status)) {
|
|
1075
|
+
changes.push(...nodeChanges(workflow, node.id, NODE_STATES.BLOCKED, {
|
|
1076
|
+
reason: cleanReason,
|
|
1077
|
+
activity: null
|
|
1078
|
+
}, now));
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
const loopChanges = (workflow.plan.loopGroups || [])
|
|
1082
|
+
.filter((group) => !['EXITED', 'STOPPED'].includes(workflow.state.loopGroups[group.id]?.status))
|
|
1083
|
+
.map((group) => loopChange(workflow, group.id, { status: 'STOPPED' }, now));
|
|
1084
|
+
workflow = commitChanges(
|
|
1085
|
+
rootDir,
|
|
1086
|
+
workflow,
|
|
1087
|
+
'RUN_CANCELLED',
|
|
1088
|
+
changes,
|
|
1089
|
+
{ actor: String(actor), reason: cleanReason },
|
|
1090
|
+
now,
|
|
1091
|
+
false,
|
|
1092
|
+
loopChanges
|
|
1093
|
+
);
|
|
1094
|
+
return { run: workflow.run, workflow };
|
|
1095
|
+
});
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1050
1098
|
function unfreezeWorkflowGoal(rootDir, { runId, transitionOptions, now = new Date() } = {}) {
|
|
1051
1099
|
const resolved = resolveRunId(rootDir, runId);
|
|
1052
1100
|
return withWorkflowLock(rootDir, resolved, () => {
|
|
@@ -1123,6 +1171,7 @@ module.exports = {
|
|
|
1123
1171
|
createWorkflowBundle,
|
|
1124
1172
|
loadWorkflow,
|
|
1125
1173
|
workflowStatus,
|
|
1174
|
+
cancelWorkflowRun,
|
|
1126
1175
|
unfreezeWorkflowGoal,
|
|
1127
1176
|
startWorkflowExecution,
|
|
1128
1177
|
startWorkflowNode,
|
package/package.json
CHANGED