188 lines
6.2 KiB
TypeScript
188 lines
6.2 KiB
TypeScript
/**
|
|
* Workflow run preparation, validation, and release-time templating.
|
|
*
|
|
* The plan module (`workflows.ts`) owns file parsing and structural validation.
|
|
* This module owns the run-specific rules that need the manager's vocabulary:
|
|
* declared arguments, step-output references, run budgets, and the expanded
|
|
* plan returned at start. Nothing here spawns a child or mutates a job.
|
|
*/
|
|
|
|
import { truncateUtf8Head } from "./output.ts";
|
|
import type { JobSnapshot, PreparedInvocation, PreparedTask } from "./types.ts";
|
|
import { WorkflowValidationError, type WorkflowPlan } from "./workflows.ts";
|
|
|
|
/** The design ceiling for one run, independent of the global worker pool. */
|
|
export const WORKFLOW_MAX_CONCURRENCY = 4;
|
|
|
|
/** Standard task-output budget: one prior output may not crowd out the rest. */
|
|
export const MAX_INTERPOLATED_STEP_OUTPUT_BYTES = 50 * 1024;
|
|
|
|
/** Expanded step prompts are bounded to keep the child brief sane. */
|
|
export const MAX_EXPANDED_STEP_PROMPT_BYTES = 100 * 1024;
|
|
|
|
export interface ExpandedWorkflowStep {
|
|
id: string;
|
|
agent: string;
|
|
needs: string[];
|
|
task: string;
|
|
}
|
|
|
|
/** The run's steps as admission units, after item fan-out is resolved. */
|
|
export interface ExpandedWorkflowPlan {
|
|
name: string;
|
|
description: string;
|
|
args: string[];
|
|
steps: ExpandedWorkflowStep[];
|
|
}
|
|
|
|
export interface PreparedWorkflowStep {
|
|
id: string;
|
|
needs: string[];
|
|
template: string;
|
|
prepared: PreparedTask;
|
|
}
|
|
|
|
export interface WorkflowRunSpec {
|
|
plan: WorkflowPlan;
|
|
args: Record<string, string>;
|
|
steps: PreparedWorkflowStep[];
|
|
expandedPlan: ExpandedWorkflowPlan;
|
|
}
|
|
|
|
export interface PreparedWorkflowInvocation extends PreparedInvocation {
|
|
mode: "workflow";
|
|
workflow: WorkflowRunSpec;
|
|
}
|
|
|
|
export interface WorkflowStartResult {
|
|
id: string;
|
|
job: JobSnapshot;
|
|
plan: ExpandedWorkflowPlan;
|
|
}
|
|
|
|
export interface WorkflowTemplateContext {
|
|
args: Readonly<Record<string, string>>;
|
|
outputs: ReadonlyMap<string, string>;
|
|
}
|
|
|
|
const PLACEHOLDER = /\{steps\.([A-Za-z_][A-Za-z0-9_-]*)\.output\}|\{args\.([A-Za-z_][A-Za-z0-9_-]*)\}/g;
|
|
const STEP_OUTPUT_REFERENCE = /\{steps\.([A-Za-z_][A-Za-z0-9_-]*)\.output\}/g;
|
|
|
|
function fail(plan: WorkflowPlan, message: string, stepId?: string): never {
|
|
throw new WorkflowValidationError(`Workflow "${plan.name}" (${plan.filePath}): ${message}`, {
|
|
filePath: plan.filePath,
|
|
stepId,
|
|
});
|
|
}
|
|
|
|
/** The per-run reservation inside the shared worker pool (ADR 0002). */
|
|
export function workflowRunConcurrency(maxWorkers: number): number {
|
|
return Math.min(maxWorkers - 1, WORKFLOW_MAX_CONCURRENCY);
|
|
}
|
|
|
|
/** The expanded plan is one admission unit per step until each-steps land. */
|
|
export function expandWorkflowPlan(plan: WorkflowPlan): ExpandedWorkflowPlan {
|
|
return {
|
|
name: plan.name,
|
|
description: plan.description,
|
|
args: [...plan.args],
|
|
steps: plan.steps.map((step) => ({
|
|
id: step.id,
|
|
agent: step.agent,
|
|
needs: [...step.needs],
|
|
task: step.task,
|
|
})),
|
|
};
|
|
}
|
|
|
|
/** Validate run arguments against the plan's declaration (all declared args are required). */
|
|
export function validateWorkflowArgs(plan: WorkflowPlan, args: Readonly<Record<string, string>>): Record<string, string> {
|
|
const declared = new Set(plan.args);
|
|
const unknown = Object.keys(args).filter((name) => !declared.has(name));
|
|
|
|
if (unknown.length > 0) {
|
|
const names = unknown.map((name) => `"${name}"`).join(", ");
|
|
fail(
|
|
plan,
|
|
`unknown argument${unknown.length === 1 ? "" : "s"} ${names}. Declared args: ${plan.args.length > 0 ? plan.args.join(", ") : "none"}.`,
|
|
);
|
|
}
|
|
|
|
const missing = plan.args.filter((name) => !Object.hasOwn(args, name));
|
|
if (missing.length > 0) {
|
|
fail(
|
|
plan,
|
|
`missing required argument${missing.length === 1 ? "" : "s"} ${missing.map((name) => `"${name}"`).join(", ")}. Provide ${missing.length === 1 ? "it" : "them"} when starting the workflow.`,
|
|
);
|
|
}
|
|
|
|
return Object.fromEntries(plan.args.map((name) => [name, args[name]]));
|
|
}
|
|
|
|
/**
|
|
* Validate the parts of the plan only a run can know: each-steps are not
|
|
* implemented yet, and every step-output reference must be a declared need so
|
|
* the value exists when the step is released.
|
|
*/
|
|
export function validateWorkflowRunShape(plan: WorkflowPlan): void {
|
|
const byId = new Map(plan.steps.map((step) => [step.id, step]));
|
|
|
|
for (const [index, step] of plan.steps.entries()) {
|
|
if (step.each !== undefined) {
|
|
fail(
|
|
plan,
|
|
`steps[${index}] ("${step.id}") declares each: {steps.${step.each}.output}; each-steps are not supported yet (line fan-out lands in ticket #8).`,
|
|
step.id,
|
|
);
|
|
}
|
|
|
|
for (const match of step.task.matchAll(STEP_OUTPUT_REFERENCE)) {
|
|
const source = match[1];
|
|
if (!byId.has(source)) {
|
|
fail(
|
|
plan,
|
|
`steps[${index}] ("${step.id}").task references unknown step "${source}". Available steps: ${plan.steps.map((candidate) => `"${candidate.id}"`).join(", ")}.`,
|
|
step.id,
|
|
);
|
|
}
|
|
if (!step.needs.includes(source)) {
|
|
fail(
|
|
plan,
|
|
`steps[${index}] ("${step.id}").task references {steps.${source}.output} but "${source}" is not in its needs (${step.needs.length > 0 ? step.needs.map((need) => `"${need}"`).join(", ") : "none"}). Add "${source}" to needs.`,
|
|
step.id,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function appendMarker(text: string, marker: string): string {
|
|
return `${text}\n\n${marker}`;
|
|
}
|
|
|
|
/**
|
|
* Expand one step brief at release time.
|
|
*
|
|
* Substitutions are single-pass, so a value that happens to contain another
|
|
* placeholder is inserted literally. Each prior output is bounded to the
|
|
* standard task-output budget, then the whole brief to the expanded-prompt
|
|
* budget; both truncations carry a visible marker.
|
|
*/
|
|
export function interpolateWorkflowTask(template: string, context: WorkflowTemplateContext): string {
|
|
const expanded = template.replace(PLACEHOLDER, (_match, stepId: string | undefined, argName: string | undefined) => {
|
|
if (stepId !== undefined) {
|
|
const output = context.outputs.get(stepId) ?? "";
|
|
const bounded = truncateUtf8Head(output, MAX_INTERPOLATED_STEP_OUTPUT_BYTES);
|
|
return bounded.truncated
|
|
? appendMarker(bounded.text, `[Output truncated: ${bounded.bytesOmitted} bytes omitted]`)
|
|
: output;
|
|
}
|
|
return context.args[argName ?? ""] ?? "";
|
|
});
|
|
|
|
const bounded = truncateUtf8Head(expanded, MAX_EXPANDED_STEP_PROMPT_BYTES);
|
|
return bounded.truncated
|
|
? appendMarker(bounded.text, `[Prompt truncated: ${bounded.bytesOmitted} bytes omitted]`)
|
|
: expanded;
|
|
}
|