{"draft":null,"from":0,"to":1,"files":[{"path":"src/pipeline/plan.ts","added":71,"removed":0,"diff":"--- /dev/null\n+++ b/src/pipeline/plan.ts\n@@ -0,0 +1,71 @@\n+// Which pipelines an event starts, and their jobs in waves (abso issue 176,\n+// under 138). A pure function of a checked pipeline file (pipeline.ts) and the\n+// event: nothing runs here. A wave is every job whose needs are all in earlier\n+// waves, so the jobs of one wave can run side by side. `abso pipeline check\n+// --on <event>` shows it, and a runner reads the same plan.\n+\n+import type { Job, Pipeline, PipelineFile } from \"./pipeline.ts\";\n+\n+/** What happened, and in which space (shared, or a named draft). */\n+export type PipelineEvent =\n+\t| { readonly kind: \"publish\" | \"draft\" | \"green\" | \"land\"; readonly space: string }\n+\t/** Someone starts one pipeline by name. */\n+\t| { readonly kind: \"hand\"; readonly pipeline: string; readonly space: string }\n+\t/** A schedule's time came: `every` as the file wrote it. Schedules run on shared. */\n+\t| { readonly kind: \"schedule\"; readonly every: string };\n+\n+export interface PlannedPipeline {\n+\treadonly name: string;\n+\t/** Job names, wave by wave. */\n+\treadonly waves: readonly (readonly string[])[];\n+\treadonly required: readonly string[];\n+}\n+\n+export interface Plan {\n+\treadonly start: readonly PlannedPipeline[];\n+\t/** The pipelines that don't start, each with why. */\n+\treadonly skip: readonly { readonly name: string; readonly why: string }[];\n+}\n+\n+export function plan(file: PipelineFile, event: PipelineEvent): Plan {\n+\tconst start: PlannedPipeline[] = [];\n+\tconst skip: { name: string; why: string }[] = [];\n+\tconst space = event.kind === \"schedule\" ? \"shared\" : event.space;\n+\tfor (const p of file.pipelines) {\n+\t\tconst why = notStarting(p, event, space);\n+\t\tif (why) skip.push({ name: p.name, why });\n+\t\telse start.push({ name: p.name, waves: waves(p.jobs), required: p.required });\n+\t}\n+\treturn { start, skip };\n+}\n+\n+/** Why `p` doesn't start on `event`, or undefined when it does. */\n+function notStarting(p: Pipeline, event: PipelineEvent, space: string): string | undefined {\n+\tif (event.kind === \"hand\" && event.pipeline !== p.name) return \"another pipeline was started by hand\";\n+\tconst on =\n+\t\tevent.kind === \"schedule\"\n+\t\t\t? p.on.some((t) => typeof t !== \"string\" && t.every === event.every)\n+\t\t\t: p.on.includes(event.kind);\n+\tif (!on) return event.kind === \"schedule\" ? `has no schedule \"${event.every}\"` : `doesn't run on ${event.kind}`;\n+\tif (p.only && !p.only.includes(space)) return `runs only for ${p.only.join(\", \")}, not ${space}`;\n+\t// The file check refuses this already; the plan holds the line too.\n+\tif (event.kind === \"draft\" && p.jobs.some((j) => j.secrets.length > 0))\n+\t\treturn \"holds secrets, and a draft runs unpublished code\";\n+\treturn undefined;\n+}\n+\n+/** The jobs in waves: each wave holds every job whose needs all came in earlier waves. */\n+export function waves(jobs: readonly Job[]): string[][] {\n+\tconst done = new Set<string>();\n+\tconst out: string[][] = [];\n+\tlet left = [...jobs];\n+\twhile (left.length > 0) {\n+\t\tconst wave = left.filter((j) => j.needs.every((n) => done.has(n)));\n+\t\t// A checked file has no cycles; stop rather than loop if one ever got here.\n+\t\tif (wave.length === 0) throw new Error(`jobs ${left.map((j) => j.name).join(\", \")} wait on each other`);\n+\t\tfor (const j of wave) done.add(j.name);\n+\t\tout.push(wave.map((j) => j.name));\n+\t\tleft = left.filter((j) => !done.has(j.name));\n+\t}\n+\treturn out;\n+}"}],"unmatched":[]}