Repository navigation
Expand file tree
/
Copy pathengine.ts
More file actions
498 lines (462 loc) · 18.1 KB
/
Copy pathengine.ts
File metadata and controls
498 lines (462 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/**
* Execution engine.
*
* The engine is a single-writer state machine over a compiled plan:
*
* queued → running → (node …) → completed | failed | cancelled | timeout
*
* Design rules that are load-bearing:
* - the engine owns state, status, budgets, events and next-node selection;
* executors only compute one node's result;
* - nothing is persisted here: sinks (`onStepStarted`, `onStepFinished`,
* `onStatusChange`, event sinks) let a worker or an inline runner store the
* trace without the engine knowing about the database;
* - cancellation is observed *between* nodes and inside services (the abort
* signal reaches every LLM/tool call), so a cancelled run stops doing work
* instead of just being marked cancelled;
* - failures are classified once, at the top, and become a terminal status
* plus a user-safe error payload.
*/
import type { Logger } from "@/lib/logging/logger";
import type { LLMGateway } from "@/modules/llm/llm.gateway";
import type { ToolRegistry } from "@/modules/tools/tool.registry";
import { HarnessValidationError } from "@/modules/harness/harness.types";
import type { HarnessValidationIssue } from "@/modules/harness/harness.validation";
import {
CancelledError,
HarnessNotExecutableError,
InternalRuntimeError,
LimitExceededError,
MaxIterationsError,
NodeExecutionError,
RuntimeTimeoutError,
toRuntimeErrorPayload,
type RuntimeErrorPayload,
} from "../errors/runtime-error";
import { RuntimeEventEmitter, type RuntimeEventSink } from "../events/event-emitter";
import { LimitTracker } from "../policies/limits";
import { DEFAULT_RETRY_POLICY, type RetryPolicy } from "../policies/retry";
import {
appendStep,
createExecutionState,
finishExecutionState,
PERSISTED_STATUS_BY_EXECUTION_STATUS,
transitionExecutionState,
type ExecutionState,
type ExecutionStatus,
type ExecutionStep,
type PersistedRunStatus,
type RuntimeExecutionLimits,
} from "../state/execution-state";
import { UsageTracker, type UsageEntry, type UsageTotal } from "../usage/tracker";
import { getNodeExecutorRegistry } from "../nodes/registry";
import type { RuntimeAgentContext, NodeExecutionResult } from "../nodes/node-executor";
import { createRuntimeServices } from "../services/runtime-services";
import { ALLOW_ALL_TOOLS, type ToolPermissionPolicy } from "../services/permissions";
import { buildExpressionScope } from "./scope";
import { compileHarness, type CompiledNode, type ExecutionPlan } from "../compiler";
export interface ExecuteHarnessOptions {
runId: string;
harnessVersionId: string;
/** Raw DSL document (validated + compiled here) or an already-compiled plan. */
definition: unknown;
input: unknown;
agent?: RuntimeAgentContext | null;
limits?: Partial<RuntimeExecutionLimits>;
gateway: LLMGateway;
toolRegistry: ToolRegistry;
permissions?: ToolPermissionPolicy;
signal?: AbortSignal;
/** Extra cancellation channel (e.g. a Redis flag set by POST /cancel). */
shouldCancel?: () => boolean | Promise<boolean>;
eventSinks?: RuntimeEventSink[];
retryPolicy?: RetryPolicy;
llmTimeoutMs?: number;
toolTimeoutMs?: number;
/** When true, uses Redis-backed memory for session/persistent scopes. */
useRedisMemory?: boolean;
/** TTL (seconds) for session-scoped Redis memory; undefined uses the store default. */
sessionMemoryTtlSeconds?: number | null;
logger: Logger;
now?: () => Date;
onStepStarted?: (step: ExecutionStep) => void | Promise<void>;
onStepFinished?: (step: ExecutionStep) => void | Promise<void>;
/** Fired on every status change (queued→running, …, → terminal). */
onStatusChange?: (state: ExecutionState) => void | Promise<void>;
}
export interface ExecuteHarnessOutcome {
runId: string;
status: Extract<ExecutionStatus, "completed" | "failed" | "cancelled" | "timeout">;
output: unknown;
error: RuntimeErrorPayload | null;
state: ExecutionState;
usage: UsageTotal;
usageByModel: UsageEntry[];
durationMs: number;
steps: ExecutionStep[];
plan: ExecutionPlan;
/** Persisted status enum, so callers never re-derive the mapping. */
persistedStatus: PersistedRunStatus;
}
/**
* Applies a validated transition in place.
*
* `transitionExecutionState` is functional (guarded, returns a new state); the
* engine keeps one mutable state object because the per-run services hold a
* reference to it. Mutating through this helper keeps both properties: illegal
* moves still throw, and every holder sees the new status.
*/
function setStatus(state: ExecutionState, next: ExecutionStatus): void {
const transitioned = transitionExecutionState(state, next);
state.status = transitioned.status;
}
/** Applies a terminal transition in place (status, output, error, completedAt). */
function finish(state: ExecutionState, status: TerminalStatus, options: Parameters<typeof finishExecutionState>[2]): void {
const finished = finishExecutionState(state, status, options);
Object.assign(state, finished);
}
type TerminalStatus = Extract<ExecutionStatus, "completed" | "failed" | "cancelled" | "timeout">;
function assertNotCancelled(signal: AbortSignal | undefined, nodeId: string | null): void {
if (signal?.aborted === true) {
throw new CancelledError("Run was cancelled", nodeId === null ? {} : { nodeId });
}
}
/** Runs a validated harness definition to completion. Never throws for run failures. */
export async function executeHarness(options: ExecuteHarnessOptions): Promise<ExecuteHarnessOutcome> {
const now = options.now ?? ((): Date => new Date());
const registry = getNodeExecutorRegistry();
// --- compile -------------------------------------------------------------
const compiled = compileHarness(options.definition);
if (!compiled.ok) {
const issues: HarnessValidationIssue[] = compiled.issues;
const notExecutable = issues.some(
(issue) => issue.code === "NODE_NOT_EXECUTABLE" || issue.code === "NODE_CONFIG_INVALID",
);
if (notExecutable) {
throw new HarnessNotExecutableError(
`Harness version ${options.harnessVersionId} cannot be executed: ${issues
.map((issue) => issue.message)
.join("; ")}`,
{ details: { issues } },
);
}
throw new HarnessValidationError(issues);
}
const plan = compiled.plan;
// --- state, events, budgets ---------------------------------------------
const startedAt = now();
const harnessId = harnessIdFromDefinition(options.definition) ?? "";
const state = createExecutionState({
runId: options.runId,
harnessId,
harnessVersionId: options.harnessVersionId,
agentId: options.agent?.id ?? null,
agentVersion: options.agent?.version ?? null,
input: options.input,
nodeCount: plan.stats.nodeCount,
edgeCount: plan.stats.edgeCount,
nodeTypes: [...new Set(plan.nodes.map((node) => node.type))],
...(options.limits !== undefined ? { limits: options.limits } : {}),
startedAt,
});
// The plan's definition id is the harness identity; validation guaranteed it.
const emitter = new RuntimeEventEmitter(
options.runId,
options.eventSinks ?? [],
now,
options.logger,
);
const limits = new LimitTracker(state.metadata.limits, startedAt.getTime(), () => now().getTime());
const usage = new UsageTracker();
const currentState = state;
const persistStatus = async (): Promise<void> => {
await options.onStatusChange?.(currentState);
};
transitionExecutionState(currentState, "running");
currentState.status = "running";
currentState.startedAt = startedAt;
emitter.emit({ type: "RUN_STARTED", entryNode: plan.entryNode });
await persistStatus();
const { services } = createRuntimeServices({
gateway: options.gateway,
toolRegistry: options.toolRegistry,
permissions: options.permissions ?? ALLOW_ALL_TOOLS,
emitter,
limits,
usage,
logger: options.logger,
signal: options.signal ?? new AbortController().signal,
now,
state: currentState,
agent: options.agent ?? null,
retryPolicy: options.retryPolicy ?? DEFAULT_RETRY_POLICY,
...(options.llmTimeoutMs !== undefined ? { llmTimeoutMs: options.llmTimeoutMs } : {}),
...(options.toolTimeoutMs !== undefined ? { toolTimeoutMs: options.toolTimeoutMs } : {}),
...(options.useRedisMemory !== undefined ? { useRedisMemory: options.useRedisMemory } : {}),
...(options.sessionMemoryTtlSeconds !== undefined && options.sessionMemoryTtlSeconds !== null
? { sessionMemoryTtlSeconds: options.sessionMemoryTtlSeconds }
: {}),
});
// --- traversal -----------------------------------------------------------
let cursor: string | null = plan.entryNode;
let previous: { nodeId: string | null; output: unknown } = { nodeId: null, output: undefined };
let iteration = 0;
let finalOutput: unknown = null;
let termination: TerminalStatus = "completed";
let failure: RuntimeErrorPayload | null = null;
try {
while (cursor !== null) {
assertNotCancelled(options.signal, cursor);
if (options.shouldCancel !== undefined && (await options.shouldCancel())) {
throw new CancelledError("Run cancelled by request");
}
limits.assertWithinDeadline();
const node = plan.compiledNodes[cursor];
if (node === undefined) {
throw new InternalRuntimeError(`Execution plan references unknown node "${cursor}"`);
}
limits.registerNode(node.id);
const step: ExecutionStep = {
nodeId: node.id,
nodeType: node.type,
nodeLabel: node.label,
status: "running",
startedAt: now(),
completedAt: null,
durationMs: null,
input: previous.output === undefined ? options.input : previous.output,
output: null,
error: null,
metadata: { order: node.order },
};
appendStep(currentState, step);
currentState.currentNodeId = node.id;
emitter.emit({
type: "NODE_STARTED",
nodeId: node.id,
nodeType: node.type,
label: node.label,
});
await options.onStepStarted?.(step);
let result: NodeExecutionResult;
try {
const executor = registry.get(node.type);
result = await executor.execute(
{
run: {
id: currentState.runId,
harnessId: currentState.harnessId,
harnessVersionId: currentState.harnessVersionId,
iteration,
},
node: {
id: node.id,
type: node.type,
label: node.label,
...(node.description !== undefined ? { description: node.description } : {}),
config: node.config,
},
input: previous.output === undefined ? options.input : previous.output,
outgoing: node.outgoing.map((edge) => ({
edgeId: edge.id,
target: edge.target,
label: edge.label,
})),
scope: buildExpressionScope({
state: currentState,
previous,
iteration,
now: now(),
}),
services,
},
node.config,
);
} catch (error) {
const payload = toRuntimeErrorPayload(error, node.id);
step.status = "failed";
step.completedAt = now();
step.durationMs = Math.max(0, step.completedAt.getTime() - (step.startedAt?.getTime() ?? 0));
step.error = payload;
emitter.emit({ type: "NODE_FAILED", nodeId: node.id, nodeType: node.type, error: payload });
await options.onStepFinished?.(step);
throw error;
}
// Commit the result: state patch, step record, trace metadata.
if (result.state !== undefined) {
currentState.variables = { ...currentState.variables, ...result.state };
}
step.status = "succeeded";
step.output = result.output;
step.completedAt = now();
step.durationMs = Math.max(0, step.completedAt.getTime() - (step.startedAt?.getTime() ?? 0));
step.metadata = {
...step.metadata,
...(result.metadata ?? {}),
...(result.branch !== undefined ? { branch: result.branch } : {}),
...(result.warnings !== undefined && result.warnings.length > 0
? { warnings: result.warnings }
: {}),
};
if (typeof result.metadata?.["iteration"] === "number") {
iteration = result.metadata["iteration"];
}
emitter.emit({
type: "NODE_COMPLETED",
nodeId: node.id,
nodeType: node.type,
status: "succeeded",
durationMs: step.durationMs,
});
// Loop narration lives in the engine: executors count iterations, the
// engine emits the events (it owns the emitter and the sequence numbers).
if (node.type === "loop") {
const loopIteration = result.metadata?.["iteration"];
const maxIterations = result.metadata?.["maxIterations"];
if (typeof loopIteration === "number" && typeof maxIterations === "number") {
if (loopIteration === 1 && result.branch === "body") {
emitter.emit({ type: "LOOP_STARTED", nodeId: node.id, maxIterations });
}
if (result.branch === "body") {
emitter.emit({
type: "LOOP_ITERATION",
nodeId: node.id,
iteration: loopIteration,
maxIterations,
});
}
}
}
await options.onStepFinished?.(step);
previous = { nodeId: node.id, output: result.output };
currentState.variables["lastNodeId"] = node.id;
currentState.variables["lastOutput"] = result.output;
// --- next node ---------------------------------------------------------
const next = selectNextNode(plan, node, result);
if (next === null) {
if (node.type === "end") {
finalOutput = result.output;
setStatus(currentState, "completed");
finish(currentState, "completed", { output: result.output, at: now() });
emitter.emit({
type: "RUN_COMPLETED",
status: "completed",
durationMs: (currentState.completedAt?.getTime() ?? startedAt.getTime()) - startedAt.getTime(),
});
await persistStatus();
termination = "completed";
cursor = null;
break;
}
throw new NodeExecutionError(
`Node "${node.label}" has no outgoing edge on branch ${describeBranch(result.branch)}`,
{ nodeId: node.id },
);
}
cursor = next;
}
} catch (error) {
const { status, payload } = classifyTermination(error, currentState.currentNodeId);
termination = status;
failure = payload;
// A step may already be marked failed; if the error happened between nodes
// (budget, cancellation), mark the current step instead.
const runningStep = currentState.history.find((step) => step.status === "running");
if (runningStep !== undefined) {
runningStep.status = status === "cancelled" ? "skipped" : "failed";
runningStep.completedAt = now();
runningStep.durationMs = Math.max(
0,
runningStep.completedAt.getTime() - (runningStep.startedAt?.getTime() ?? 0),
);
runningStep.error = payload;
}
const at = now();
finish(currentState, status, {
error: payload,
...(currentState.output !== null ? { output: currentState.output } : {}),
at,
});
if (status === "cancelled") {
emitter.emit({ type: "RUN_CANCELLED", reason: payload.message });
} else if (status === "timeout") {
emitter.emit({ type: "RUN_TIMED_OUT", error: payload });
} else {
emitter.emit({ type: "RUN_FAILED", status: "failed", error: payload });
}
await persistStatus();
}
const completedAt = currentState.completedAt ?? now();
const outcome: ExecuteHarnessOutcome = {
runId: options.runId,
status: termination,
output: finalOutput === null ? currentState.output : finalOutput,
error: failure,
state: currentState,
usage: usage.total(),
usageByModel: usage.byModel(),
durationMs: Math.max(0, completedAt.getTime() - startedAt.getTime()),
steps: currentState.history,
plan,
persistedStatus: PERSISTED_STATUS_BY_EXECUTION_STATUS[termination],
};
return outcome;
}
/** Chooses the next node from the executor result (branch, override, or end). */
function selectNextNode(
plan: ExecutionPlan,
node: CompiledNode,
result: NodeExecutionResult,
): string | null {
if (result.nextNodeId !== undefined) {
if (plan.compiledNodes[result.nextNodeId] === undefined) {
throw new InternalRuntimeError(
`Node "${node.label}" routed to unknown node "${result.nextNodeId}"`,
);
}
return result.nextNodeId;
}
const handle = result.branch ?? "";
const edges = node.branches[handle];
if (edges === undefined || edges.length === 0) {
if (result.branch !== undefined && node.outgoing.length > 0) {
throw new NodeExecutionError(
`Node "${node.label}" selected branch "${result.branch}" but no edge is wired to it`,
{ nodeId: node.id },
);
}
return null;
}
return edges[0]?.target ?? null;
}
function describeBranch(branch: string | undefined): string {
return branch === undefined || branch === "" ? "(default)" : `"${branch}"`;
}
/** Maps a thrown error onto a terminal status + user-safe payload. */
function classifyTermination(
error: unknown,
nodeId: string | null,
): {
status: Extract<TerminalStatus, "failed" | "cancelled" | "timeout">;
payload: RuntimeErrorPayload;
} {
const payload = toRuntimeErrorPayload(error, nodeId ?? undefined);
if (error instanceof CancelledError) {
return { status: "cancelled", payload };
}
if (error instanceof RuntimeTimeoutError) {
return { status: "timeout", payload };
}
if (error instanceof MaxIterationsError || error instanceof LimitExceededError) {
return { status: "failed", payload };
}
return { status: "failed", payload };
}
/** Reads `definition.id` without trusting it (compile already validated it). */
function harnessIdFromDefinition(definition: unknown): string | null {
if (typeof definition !== "object" || definition === null) {
return null;
}
const id = (definition as Record<string, unknown>)["id"];
return typeof id === "string" && id !== "" ? id : null;
}