diff --git a/src/index.ts b/src/index.ts index d5923f7..1ad17b5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,8 +51,9 @@ import { buildUltraGuidance, formatUltraModeStatus, parseUltraCommand, UltraMode import { UltraKeywordTrigger } from "./ultra-keyword.ts"; import { formatUltraReviewCard, formatUltraStart, UltraDeclinedError } from "./ultra.ts"; import { buildUltraNotice, ULTRA_DONE_MESSAGE_TYPE, type UltraNoticeDetails } from "./ultra-notify.ts"; -import { formatWorkflowStart, parseWorkflowCommand, type WorkflowStartResult } from "./workflow.ts"; -import { loadWorkflow, parseInlineWorkflow } from "./workflows.ts"; +import { formatWorkflowStart, parseWorkflowCommand } from "./workflow-command.ts"; +import { loadWorkflow, parseInlineWorkflow } from "./workflow-plan.ts"; +import type { WorkflowStartResult } from "./workflow-run.ts"; function textResult(text: string, details?: unknown, isError?: boolean): AgentToolResult { return { content: [{ type: "text", text }], details, isError }; diff --git a/src/journal.ts b/src/journal.ts index f97d936..5bbcd5c 100644 --- a/src/journal.ts +++ b/src/journal.ts @@ -12,8 +12,8 @@ import { createHash } from "node:crypto"; import { MAX_TASK_OUTPUT_BYTES, truncateUtf8Head } from "./output.ts"; import type { JobSnapshot, JobStatus, TaskSnapshot, TaskStatus, UsageStats } from "./types.ts"; -import type { WorkflowPlan, WorkflowStep } from "./workflows.ts"; -import type { WorkflowRunSpec } from "./workflow.ts"; +import type { WorkflowPlan, WorkflowStep } from "./workflow-plan.ts"; +import type { WorkflowRunSpec } from "./workflow-run.ts"; /** One fanned-out item child of an each-step, as the journal records it. */ export interface WorkflowJournalItem { diff --git a/src/manager.ts b/src/manager.ts index 2df3131..6f6ea2d 100644 --- a/src/manager.ts +++ b/src/manager.ts @@ -21,7 +21,7 @@ import { type SyncOutcome, type SyncRunOptions, } 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"; const DEFAULT_MAX_ACTIVE_JOBS = 64; diff --git a/src/prepare.ts b/src/prepare.ts index b3c0d1e..17ccbd3 100644 --- a/src/prepare.ts +++ b/src/prepare.ts @@ -24,8 +24,8 @@ import { validateWorkflowRunShape, type PreparedWorkflowInvocation, type PreparedWorkflowStep, -} from "./workflow.ts"; -import { validateWorkflowExpansion, discoverWorkflows, type WorkflowPlan } from "./workflows.ts"; +} from "./workflow-run.ts"; +import { validateWorkflowExpansion, discoverWorkflows, type WorkflowPlan } from "./workflow-plan.ts"; const MAX_PARALLEL_TASKS = 8; diff --git a/src/ultra-mode.ts b/src/ultra-mode.ts index 703c538..b5c70f6 100644 --- a/src/ultra-mode.ts +++ b/src/ultra-mode.ts @@ -8,7 +8,7 @@ */ 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 = { small: 5, diff --git a/src/ultra.ts b/src/ultra.ts index 599e87e..aa23d31 100644 --- a/src/ultra.ts +++ b/src/ultra.ts @@ -1,15 +1,15 @@ /** * 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 * touches the manager; `index.ts` owns the start path. */ import type { UltraGuideline } from "./config.ts"; import { formatUltraGuidelineLine } from "./ultra-mode.ts"; -import type { PreparedWorkflowInvocation, WorkflowStartResult } from "./workflow.ts"; -import { MAX_EXPANDED_CHILDREN, MAX_WORKFLOW_STEPS } from "./workflows.ts"; +import { MAX_EXPANDED_CHILDREN, MAX_WORKFLOW_STEPS } from "./workflow-plan.ts"; +import type { PreparedWorkflowInvocation, WorkflowStartResult } from "./workflow-run.ts"; /** Thrown when the user declines the review card for an inline plan. */ export class UltraDeclinedError extends Error { diff --git a/src/workflow-command.ts b/src/workflow-command.ts new file mode 100644 index 0000000..6830dba --- /dev/null +++ b/src/workflow-command.ts @@ -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; +} + +/** Shell-like tokenizer for `/workflow [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 [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 [key=value ...]"); + } + + const [name, ...assignments] = tokens; + const args: Record = {}; + 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"); +} diff --git a/src/workflows.ts b/src/workflow-plan.ts similarity index 100% rename from src/workflows.ts rename to src/workflow-plan.ts diff --git a/src/workflow.ts b/src/workflow-run.ts similarity index 68% rename from src/workflow.ts rename to src/workflow-run.ts index ad2f263..11f36a4 100644 --- a/src/workflow.ts +++ b/src/workflow-run.ts @@ -1,15 +1,15 @@ /** * 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. + * The plan module (`workflow-plan.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 + * start 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"; +import { workflowValidationError, type WorkflowPlan } from "./workflow-plan.ts"; /** The design ceiling for one run, independent of the global worker pool. */ 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]`) : 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; -} - -/** Shell-like tokenizer for `/workflow [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 [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 [key=value ...]"); - } - - const [name, ...assignments] = tokens; - const args: Record = {}; - 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"); -} diff --git a/src/workflow-supervisor.ts b/src/workflow-supervisor.ts index ebf3b6b..ccec88f 100644 --- a/src/workflow-supervisor.ts +++ b/src/workflow-supervisor.ts @@ -29,8 +29,8 @@ import { splitWorkflowItems, workflowRunConcurrency, type WorkflowRunSpec, -} from "./workflow.ts"; -import { validateWorkflowExpansion } from "./workflows.ts"; +} from "./workflow-run.ts"; +import { validateWorkflowExpansion } from "./workflow-plan.ts"; /** * The manager capabilities a run needs. Queueing, worker accounting, job diff --git a/test/extension.test.ts b/test/extension.test.ts index d9cec57..8e7e18b 100644 --- a/test/extension.test.ts +++ b/test/extension.test.ts @@ -10,7 +10,7 @@ import { Value } from "typebox/value"; import registerExtension from "../src/index.ts"; import { extractJobSnapshots } from "../src/persistence.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"; type Execute = (id: string, params: any, signal?: AbortSignal, onUpdate?: unknown, ctx?: ExtensionToolContext) => Promise>; diff --git a/test/ultra-notify.test.ts b/test/ultra-notify.test.ts index ca0b7e1..51bde7b 100644 --- a/test/ultra-notify.test.ts +++ b/test/ultra-notify.test.ts @@ -11,7 +11,7 @@ import { MAX_TASK_OUTPUT_BYTES } from "../src/output.ts"; import { extractWorkflowJournals } from "../src/persistence.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 type { WorkflowStartResult } from "../src/workflow.ts"; +import type { WorkflowStartResult } from "../src/workflow-run.ts"; import { FakeRunner } from "./helpers.ts"; const tempRoots: string[] = []; diff --git a/test/ultra.test.ts b/test/ultra.test.ts index da23915..2b56b85 100644 --- a/test/ultra.test.ts +++ b/test/ultra.test.ts @@ -10,13 +10,13 @@ import { Value } from "typebox/value"; import registerExtension from "../src/index.ts"; import { extractWorkflowJournals, persistedWorkflowJournal, WORKFLOW_RUN_ENTRY } from "../src/persistence.ts"; import { UltraParams } from "../src/schemas.ts"; -import type { WorkflowStartResult } from "../src/workflow.ts"; +import type { WorkflowStartResult } from "../src/workflow-run.ts"; import { INLINE_WORKFLOW_FILE_PATH, WorkflowValidationError, parseInlineWorkflow, parseWorkflow, -} from "../src/workflows.ts"; +} from "../src/workflow-plan.ts"; import { FakeRunner } from "./helpers.ts"; const tempRoots: string[] = []; diff --git a/test/workflow-items.test.ts b/test/workflow-items.test.ts index b2c6a1b..ef3d9e5 100644 --- a/test/workflow-items.test.ts +++ b/test/workflow-items.test.ts @@ -7,7 +7,7 @@ import { interpolateWorkflowTask, joinWorkflowItemOutputs, splitWorkflowItems, -} from "../src/workflow.ts"; +} from "../src/workflow-run.ts"; // Independent of the exported constants so a changed budget fails the test. const OUTPUT_BUDGET = 50 * 1024; diff --git a/test/workflow-journal.test.ts b/test/workflow-journal.test.ts index 1afe851..39631ce 100644 --- a/test/workflow-journal.test.ts +++ b/test/workflow-journal.test.ts @@ -16,7 +16,7 @@ import { WORKFLOW_RUN_ENTRY, } from "../src/persistence.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 { FakeRunner } from "./helpers.ts"; diff --git a/test/workflow-run.test.ts b/test/workflow-run.test.ts index df98c02..32b2f1b 100644 --- a/test/workflow-run.test.ts +++ b/test/workflow-run.test.ts @@ -5,7 +5,7 @@ import * as path from "node:path"; import test, { after } from "node:test"; import { SubagentManager } from "../src/manager.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"; const tempRoots: string[] = []; diff --git a/test/workflow-template.test.ts b/test/workflow-template.test.ts index eaf8787..c722000 100644 --- a/test/workflow-template.test.ts +++ b/test/workflow-template.test.ts @@ -5,7 +5,7 @@ import { MAX_INTERPOLATED_STEP_OUTPUT_BYTES, interpolateWorkflowTask, workflowRunConcurrency, -} from "../src/workflow.ts"; +} from "../src/workflow-run.ts"; // Independent of the exported constants so a changed budget fails the test. const OUTPUT_BUDGET = 50 * 1024; diff --git a/test/workflows.test.ts b/test/workflows.test.ts index 20406c7..a56da2e 100644 --- a/test/workflows.test.ts +++ b/test/workflows.test.ts @@ -14,7 +14,7 @@ import { parseWorkflow, validateWorkflowExpansion, type WorkflowPlan, -} from "../src/workflows.ts"; +} from "../src/workflow-plan.ts"; const tempRoots: string[] = [];