Split workflow modules by concern: plan, run, command

This commit is contained in:
alex committed 2026-12-01 10:55:35 +00:00
1 parent cfec14e515
commit 7114650aed
18 files changed
+149 -140

No files matched your search

+3 -2
View File
@@ -51,8 +51,9 @@ import { buildUltraGuidance, formatUltraModeStatus, parseUltraCommand, UltraMode
import { UltraKeywordTrigger } from "./ultra-keyword.ts"; import { UltraKeywordTrigger } from "./ultra-keyword.ts";
import { formatUltraReviewCard, formatUltraStart, UltraDeclinedError } from "./ultra.ts"; import { formatUltraReviewCard, formatUltraStart, UltraDeclinedError } from "./ultra.ts";
import { buildUltraNotice, ULTRA_DONE_MESSAGE_TYPE, type UltraNoticeDetails } from "./ultra-notify.ts"; import { buildUltraNotice, ULTRA_DONE_MESSAGE_TYPE, type UltraNoticeDetails } from "./ultra-notify.ts";
import { formatWorkflowStart, parseWorkflowCommand, type WorkflowStartResult } from "./workflow.ts"; import { formatWorkflowStart, parseWorkflowCommand } from "./workflow-command.ts";
import { loadWorkflow, parseInlineWorkflow } from "./workflows.ts"; import { loadWorkflow, parseInlineWorkflow } from "./workflow-plan.ts";
import type { WorkflowStartResult } from "./workflow-run.ts";
function textResult(text: string, details?: unknown, isError?: boolean): AgentToolResult<any> { function textResult(text: string, details?: unknown, isError?: boolean): AgentToolResult<any> {
return { content: [{ type: "text", text }], details, isError }; return { content: [{ type: "text", text }], details, isError };
+2 -2
View File
@@ -12,8 +12,8 @@
import { createHash } from "node:crypto"; import { createHash } from "node:crypto";
import { MAX_TASK_OUTPUT_BYTES, truncateUtf8Head } from "./output.ts"; import { MAX_TASK_OUTPUT_BYTES, truncateUtf8Head } from "./output.ts";
import type { JobSnapshot, JobStatus, TaskSnapshot, TaskStatus, UsageStats } from "./types.ts"; import type { JobSnapshot, JobStatus, TaskSnapshot, TaskStatus, UsageStats } from "./types.ts";
import type { WorkflowPlan, WorkflowStep } from "./workflows.ts"; import type { WorkflowPlan, WorkflowStep } from "./workflow-plan.ts";
import type { WorkflowRunSpec } from "./workflow.ts"; import type { WorkflowRunSpec } from "./workflow-run.ts";
/** One fanned-out item child of an each-step, as the journal records it. */ /** One fanned-out item child of an each-step, as the journal records it. */
export interface WorkflowJournalItem { export interface WorkflowJournalItem {
+1 -1
View File
@@ -21,7 +21,7 @@ import {
type SyncOutcome, type SyncOutcome,
type SyncRunOptions, type SyncRunOptions,
} from "./types.ts"; } from "./types.ts";
import type { PreparedWorkflowInvocation, WorkflowRunSpec, WorkflowStartResult } from "./workflow.ts"; import type { PreparedWorkflowInvocation, WorkflowRunSpec, WorkflowStartResult } from "./workflow-run.ts";
import { WorkflowSupervisor } from "./workflow-supervisor.ts"; import { WorkflowSupervisor } from "./workflow-supervisor.ts";
const DEFAULT_MAX_ACTIVE_JOBS = 64; const DEFAULT_MAX_ACTIVE_JOBS = 64;
+2 -2
View File
@@ -24,8 +24,8 @@ import {
validateWorkflowRunShape, validateWorkflowRunShape,
type PreparedWorkflowInvocation, type PreparedWorkflowInvocation,
type PreparedWorkflowStep, type PreparedWorkflowStep,
} from "./workflow.ts"; } from "./workflow-run.ts";
import { validateWorkflowExpansion, discoverWorkflows, type WorkflowPlan } from "./workflows.ts"; import { validateWorkflowExpansion, discoverWorkflows, type WorkflowPlan } from "./workflow-plan.ts";
const MAX_PARALLEL_TASKS = 8; const MAX_PARALLEL_TASKS = 8;
+1 -1
View File
@@ -8,7 +8,7 @@
*/ */
import type { UltraGuideline } from "./config.ts"; import type { UltraGuideline } from "./config.ts";
import { MAX_EXPANDED_CHILDREN, MAX_WORKFLOW_STEPS } from "./workflows.ts"; import { MAX_EXPANDED_CHILDREN, MAX_WORKFLOW_STEPS } from "./workflow-plan.ts";
const GUIDELINE_CHILD_TARGET: Record<UltraGuideline, number | null> = { const GUIDELINE_CHILD_TARGET: Record<UltraGuideline, number | null> = {
small: 5, small: 5,
+3 -3
View File
@@ -1,15 +1,15 @@
/** /**
* Inline-plan surface: the review card and the start result. * Inline-plan surface: the review card and the start result.
* *
* The plan itself is parsed by the file-plan parser (`workflows.ts`), so the * The plan itself is parsed by the file-plan parser (`workflow-plan.ts`), so the
* ultra tool adds policy and presentation only. Nothing here spawns a child or * ultra tool adds policy and presentation only. Nothing here spawns a child or
* touches the manager; `index.ts` owns the start path. * touches the manager; `index.ts` owns the start path.
*/ */
import type { UltraGuideline } from "./config.ts"; import type { UltraGuideline } from "./config.ts";
import { formatUltraGuidelineLine } from "./ultra-mode.ts"; import { formatUltraGuidelineLine } from "./ultra-mode.ts";
import type { PreparedWorkflowInvocation, WorkflowStartResult } from "./workflow.ts"; import { MAX_EXPANDED_CHILDREN, MAX_WORKFLOW_STEPS } from "./workflow-plan.ts";
import { MAX_EXPANDED_CHILDREN, MAX_WORKFLOW_STEPS } from "./workflows.ts"; import type { PreparedWorkflowInvocation, WorkflowStartResult } from "./workflow-run.ts";
/** Thrown when the user declines the review card for an inline plan. */ /** Thrown when the user declines the review card for an inline plan. */
export class UltraDeclinedError extends Error { export class UltraDeclinedError extends Error {
+121
View File
@@ -0,0 +1,121 @@
/**
* The `/workflow` slash command: tokenizer, argument parser, and start formatter.
*
* Parsing is purely lexical: keys are validated against the plan's declared
* args by the start path, so unknown keys are not rejected here.
*/
import type { WorkflowStartResult } from "./workflow-run.ts";
/** Thrown when a `/workflow` command line cannot be parsed. */
export class WorkflowCommandError extends Error {
constructor(message: string) {
super(message);
this.name = "WorkflowCommandError";
}
}
export interface WorkflowCommandRequest {
name: string;
args: Record<string, string>;
}
/** Shell-like tokenizer for `/workflow <name> [key=value ...]`. */
function tokenizeWorkflowCommand(input: string): string[] {
const tokens: string[] = [];
let current = "";
let tokenStarted = false;
let quote: "'" | '"' | undefined;
for (let index = 0; index < input.length; index++) {
const character = input[index];
if (quote !== undefined) {
if (quote === '"' && character === "\\" && index + 1 < input.length) {
current += input[++index];
tokenStarted = true;
continue;
}
if (character === quote) {
quote = undefined;
tokenStarted = true;
continue;
}
current += character;
tokenStarted = true;
continue;
}
if (character === "'" || character === '"') {
quote = character;
tokenStarted = true;
continue;
}
if (character === "\\" && index + 1 < input.length) {
current += input[++index];
tokenStarted = true;
continue;
}
if (/\s/.test(character)) {
if (tokenStarted) {
tokens.push(current);
current = "";
tokenStarted = false;
}
continue;
}
current += character;
tokenStarted = true;
}
if (quote !== undefined) throw new WorkflowCommandError(`Unterminated ${quote} quote in workflow command.`);
if (tokenStarted) tokens.push(current);
return tokens;
}
/**
* Parse `/workflow <name> [key=value ...]` arguments.
*
* Values may be quoted (single or double) so they can contain spaces; the
* quotes are removed and the value is kept verbatim otherwise. Unknown keys
* are not rejected here: they are validated against the plan's declared args
* by the start path, so the error names what the plan actually accepts.
*/
export function parseWorkflowCommand(input: string): WorkflowCommandRequest {
const tokens = tokenizeWorkflowCommand(input);
if (tokens.length === 0 || tokens[0].length === 0) {
throw new WorkflowCommandError("Usage: /workflow <name> [key=value ...]");
}
const [name, ...assignments] = tokens;
const args: Record<string, string> = {};
for (const assignment of assignments) {
const separator = assignment.indexOf("=");
if (separator <= 0) {
throw new WorkflowCommandError(`Invalid workflow argument "${assignment}": expected key=value.`);
}
const key = assignment.slice(0, separator);
if (Object.hasOwn(args, key)) {
throw new WorkflowCommandError(`Duplicate workflow argument "${key}".`);
}
args[key] = assignment.slice(separator + 1);
}
return { name, args };
}
/** Human-readable start result shared by the tool and the slash command. */
export function formatWorkflowStart(result: WorkflowStartResult): string {
const lines = [
`Started workflow run ${result.id} (${result.job.status}).`,
`Workflow: ${result.plan.name} — ${result.plan.description}`,
`Steps (${result.plan.steps.length}):`,
];
for (const step of result.plan.steps) {
const needs = step.needs.length > 0 ? ` needs ${step.needs.join(", ")}` : "";
lines.push(`- ${step.id} (${step.agent})${needs}`);
}
lines.push(`Use subagent_jobs { action: "get" | "wait", jobId: "${result.id}" } to retrieve it.`);
return lines.join("\n");
}
File renamed without changes.
+5 -118
View File
@@ -1,15 +1,15 @@
/** /**
* Workflow run preparation, validation, and release-time templating. * Workflow run preparation, validation, and release-time templating.
* *
* The plan module (`workflows.ts`) owns file parsing and structural validation. * The plan module (`workflow-plan.ts`) owns file parsing and structural
* This module owns the run-specific rules that need the manager's vocabulary: * validation. This module owns the run-specific rules that need the manager's
* declared arguments, step-output references, run budgets, and the expanded * vocabulary: declared arguments, step-output references, run budgets, and the
* plan returned at start. Nothing here spawns a child or mutates a job. * start plan returned at start. Nothing here spawns a child or mutates a job.
*/ */
import { truncateUtf8Head } from "./output.ts"; import { truncateUtf8Head } from "./output.ts";
import type { JobSnapshot, PreparedInvocation, PreparedTask } from "./types.ts"; import type { JobSnapshot, PreparedInvocation, PreparedTask } from "./types.ts";
import { workflowValidationError, type WorkflowPlan } from "./workflows.ts"; import { workflowValidationError, type WorkflowPlan } from "./workflow-plan.ts";
/** The design ceiling for one run, independent of the global worker pool. */ /** The design ceiling for one run, independent of the global worker pool. */
export const WORKFLOW_MAX_CONCURRENCY = 4; export const WORKFLOW_MAX_CONCURRENCY = 4;
@@ -235,116 +235,3 @@ export function interpolateWorkflowTask(template: string, context: WorkflowTempl
? appendMarker(bounded.text, `[Prompt truncated: ${bounded.bytesOmitted} bytes omitted]`) ? appendMarker(bounded.text, `[Prompt truncated: ${bounded.bytesOmitted} bytes omitted]`)
: expanded; : expanded;
} }
/** Thrown when a `/workflow` command line cannot be parsed. */
export class WorkflowCommandError extends Error {
constructor(message: string) {
super(message);
this.name = "WorkflowCommandError";
}
}
export interface WorkflowCommandRequest {
name: string;
args: Record<string, string>;
}
/** Shell-like tokenizer for `/workflow <name> [key=value ...]`. */
function tokenizeWorkflowCommand(input: string): string[] {
const tokens: string[] = [];
let current = "";
let tokenStarted = false;
let quote: "'" | '"' | undefined;
for (let index = 0; index < input.length; index++) {
const character = input[index];
if (quote !== undefined) {
if (quote === '"' && character === "\\" && index + 1 < input.length) {
current += input[++index];
tokenStarted = true;
continue;
}
if (character === quote) {
quote = undefined;
tokenStarted = true;
continue;
}
current += character;
tokenStarted = true;
continue;
}
if (character === "'" || character === '"') {
quote = character;
tokenStarted = true;
continue;
}
if (character === "\\" && index + 1 < input.length) {
current += input[++index];
tokenStarted = true;
continue;
}
if (/\s/.test(character)) {
if (tokenStarted) {
tokens.push(current);
current = "";
tokenStarted = false;
}
continue;
}
current += character;
tokenStarted = true;
}
if (quote !== undefined) throw new WorkflowCommandError(`Unterminated ${quote} quote in workflow command.`);
if (tokenStarted) tokens.push(current);
return tokens;
}
/**
* Parse `/workflow <name> [key=value ...]` arguments.
*
* Values may be quoted (single or double) so they can contain spaces; the
* quotes are removed and the value is kept verbatim otherwise. Unknown keys
* are not rejected here: they are validated against the plan's declared args
* by the start path, so the error names what the plan actually accepts.
*/
export function parseWorkflowCommand(input: string): WorkflowCommandRequest {
const tokens = tokenizeWorkflowCommand(input);
if (tokens.length === 0 || tokens[0].length === 0) {
throw new WorkflowCommandError("Usage: /workflow <name> [key=value ...]");
}
const [name, ...assignments] = tokens;
const args: Record<string, string> = {};
for (const assignment of assignments) {
const separator = assignment.indexOf("=");
if (separator <= 0) {
throw new WorkflowCommandError(`Invalid workflow argument "${assignment}": expected key=value.`);
}
const key = assignment.slice(0, separator);
if (Object.hasOwn(args, key)) {
throw new WorkflowCommandError(`Duplicate workflow argument "${key}".`);
}
args[key] = assignment.slice(separator + 1);
}
return { name, args };
}
/** Human-readable start result shared by the tool and the slash command. */
export function formatWorkflowStart(result: WorkflowStartResult): string {
const lines = [
`Started workflow run ${result.id} (${result.job.status}).`,
`Workflow: ${result.plan.name} — ${result.plan.description}`,
`Steps (${result.plan.steps.length}):`,
];
for (const step of result.plan.steps) {
const needs = step.needs.length > 0 ? ` needs ${step.needs.join(", ")}` : "";
lines.push(`- ${step.id} (${step.agent})${needs}`);
}
lines.push(`Use subagent_jobs { action: "get" | "wait", jobId: "${result.id}" } to retrieve it.`);
return lines.join("\n");
}
+2 -2
View File
@@ -29,8 +29,8 @@ import {
splitWorkflowItems, splitWorkflowItems,
workflowRunConcurrency, workflowRunConcurrency,
type WorkflowRunSpec, type WorkflowRunSpec,
} from "./workflow.ts"; } from "./workflow-run.ts";
import { validateWorkflowExpansion } from "./workflows.ts"; import { validateWorkflowExpansion } from "./workflow-plan.ts";
/** /**
* The manager capabilities a run needs. Queueing, worker accounting, job * The manager capabilities a run needs. Queueing, worker accounting, job
+1 -1
View File
@@ -10,7 +10,7 @@ import { Value } from "typebox/value";
import registerExtension from "../src/index.ts"; import registerExtension from "../src/index.ts";
import { extractJobSnapshots } from "../src/persistence.ts"; import { extractJobSnapshots } from "../src/persistence.ts";
import { WorkflowParams } from "../src/schemas.ts"; import { WorkflowParams } from "../src/schemas.ts";
import type { WorkflowStartResult } from "../src/workflow.ts"; import type { WorkflowStartResult } from "../src/workflow-run.ts";
import { FakeRunner, result as makeResult } from "./helpers.ts"; import { FakeRunner, result as makeResult } from "./helpers.ts";
type Execute = (id: string, params: any, signal?: AbortSignal, onUpdate?: unknown, ctx?: ExtensionToolContext) => Promise<AgentToolResult<any>>; type Execute = (id: string, params: any, signal?: AbortSignal, onUpdate?: unknown, ctx?: ExtensionToolContext) => Promise<AgentToolResult<any>>;
+1 -1
View File
@@ -11,7 +11,7 @@ import { MAX_TASK_OUTPUT_BYTES } from "../src/output.ts";
import { extractWorkflowJournals } from "../src/persistence.ts"; import { extractWorkflowJournals } from "../src/persistence.ts";
import { emptyUsage, type JobStatus, type TaskStatus } from "../src/types.ts"; import { emptyUsage, type JobStatus, type TaskStatus } from "../src/types.ts";
import { buildUltraNotice, ULTRA_DONE_MESSAGE_TYPE, type UltraNoticeDetails } from "../src/ultra-notify.ts"; import { buildUltraNotice, ULTRA_DONE_MESSAGE_TYPE, type UltraNoticeDetails } from "../src/ultra-notify.ts";
import type { WorkflowStartResult } from "../src/workflow.ts"; import type { WorkflowStartResult } from "../src/workflow-run.ts";
import { FakeRunner } from "./helpers.ts"; import { FakeRunner } from "./helpers.ts";
const tempRoots: string[] = []; const tempRoots: string[] = [];
+2 -2
View File
@@ -10,13 +10,13 @@ import { Value } from "typebox/value";
import registerExtension from "../src/index.ts"; import registerExtension from "../src/index.ts";
import { extractWorkflowJournals, persistedWorkflowJournal, WORKFLOW_RUN_ENTRY } from "../src/persistence.ts"; import { extractWorkflowJournals, persistedWorkflowJournal, WORKFLOW_RUN_ENTRY } from "../src/persistence.ts";
import { UltraParams } from "../src/schemas.ts"; import { UltraParams } from "../src/schemas.ts";
import type { WorkflowStartResult } from "../src/workflow.ts"; import type { WorkflowStartResult } from "../src/workflow-run.ts";
import { import {
INLINE_WORKFLOW_FILE_PATH, INLINE_WORKFLOW_FILE_PATH,
WorkflowValidationError, WorkflowValidationError,
parseInlineWorkflow, parseInlineWorkflow,
parseWorkflow, parseWorkflow,
} from "../src/workflows.ts"; } from "../src/workflow-plan.ts";
import { FakeRunner } from "./helpers.ts"; import { FakeRunner } from "./helpers.ts";
const tempRoots: string[] = []; const tempRoots: string[] = [];
+1 -1
View File
@@ -7,7 +7,7 @@ import {
interpolateWorkflowTask, interpolateWorkflowTask,
joinWorkflowItemOutputs, joinWorkflowItemOutputs,
splitWorkflowItems, splitWorkflowItems,
} from "../src/workflow.ts"; } from "../src/workflow-run.ts";
// Independent of the exported constants so a changed budget fails the test. // Independent of the exported constants so a changed budget fails the test.
const OUTPUT_BUDGET = 50 * 1024; const OUTPUT_BUDGET = 50 * 1024;
+1 -1
View File
@@ -16,7 +16,7 @@ import {
WORKFLOW_RUN_ENTRY, WORKFLOW_RUN_ENTRY,
} from "../src/persistence.ts"; } from "../src/persistence.ts";
import { prepareWorkflowInvocation, type PrepareContext } from "../src/prepare.ts"; import { prepareWorkflowInvocation, type PrepareContext } from "../src/prepare.ts";
import { parseWorkflow, type WorkflowPlan } from "../src/workflows.ts"; import { parseWorkflow, type WorkflowPlan } from "../src/workflow-plan.ts";
import { emptyUsage, type JobSnapshot, type TaskSnapshot, type TaskStatus } from "../src/types.ts"; import { emptyUsage, type JobSnapshot, type TaskSnapshot, type TaskStatus } from "../src/types.ts";
import { FakeRunner } from "./helpers.ts"; import { FakeRunner } from "./helpers.ts";
+1 -1
View File
@@ -5,7 +5,7 @@ import * as path from "node:path";
import test, { after } from "node:test"; import test, { after } from "node:test";
import { SubagentManager } from "../src/manager.ts"; import { SubagentManager } from "../src/manager.ts";
import { prepareWorkflowInvocation, type PrepareContext } from "../src/prepare.ts"; import { prepareWorkflowInvocation, type PrepareContext } from "../src/prepare.ts";
import { parseWorkflow, WorkflowValidationError, type WorkflowPlan } from "../src/workflows.ts"; import { parseWorkflow, WorkflowValidationError, type WorkflowPlan } from "../src/workflow-plan.ts";
import { FakeRunner } from "./helpers.ts"; import { FakeRunner } from "./helpers.ts";
const tempRoots: string[] = []; const tempRoots: string[] = [];
+1 -1
View File
@@ -5,7 +5,7 @@ import {
MAX_INTERPOLATED_STEP_OUTPUT_BYTES, MAX_INTERPOLATED_STEP_OUTPUT_BYTES,
interpolateWorkflowTask, interpolateWorkflowTask,
workflowRunConcurrency, workflowRunConcurrency,
} from "../src/workflow.ts"; } from "../src/workflow-run.ts";
// Independent of the exported constants so a changed budget fails the test. // Independent of the exported constants so a changed budget fails the test.
const OUTPUT_BUDGET = 50 * 1024; const OUTPUT_BUDGET = 50 * 1024;
+1 -1
View File
@@ -14,7 +14,7 @@ import {
parseWorkflow, parseWorkflow,
validateWorkflowExpansion, validateWorkflowExpansion,
type WorkflowPlan, type WorkflowPlan,
} from "../src/workflows.ts"; } from "../src/workflow-plan.ts";
const tempRoots: string[] = []; const tempRoots: string[] = [];