Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/src/pipeline/plan.ts?format=json
72 linesinfra-split 94% · rename 6%Last change rename in version 101, 3 versions changed it
1// Which pipelines an event starts, and their jobs in waves (abso issue 176,
2// under 138). A pure function of a checked pipeline file (pipeline.ts) and the
3// event: nothing runs here. A wave is every job whose needs are all in earlier
4// waves, so the jobs of one wave can run side by side. `abso pipeline check
5// --on <event>` shows it, and a runner reads the same plan.
6
7import { type Job, type Pipeline, type PipelineFile, triggerWord } from "./pipeline.ts";
8
9/** What happened, and in which space (shared, or a named draft). */
10export type PipelineEvent =
11 | { readonly kind: "publish" | "draft" | "green" | "land"; readonly space: string }
12 /** Someone starts one pipeline by name. */
13 | { readonly kind: "hand"; readonly pipeline: string; readonly space: string }
14 /** A schedule's time came: `every` as the file wrote it. Schedules run on shared. */
15 | { readonly kind: "schedule"; readonly every: string };
17export interface PlannedPipeline {
18 readonly name: string;
19 /** Job names, wave by wave. */
20 readonly waves: readonly (readonly string[])[];
21 readonly required: readonly string[];
24export interface Plan {
25 readonly start: readonly PlannedPipeline[];
26 /** The pipelines that don't start, each with why. */
27 readonly skip: readonly { readonly name: string; readonly why: string }[];
30export function plan(file: PipelineFile, event: PipelineEvent): Plan {
31 const start: PlannedPipeline[] = [];
32 const skip: { name: string; why: string }[] = [];
33 const space = event.kind === "schedule" ? "shared" : event.space;
34 for (const p of file.pipelines) {
35 const why = notStarting(p, event, space);
36 if (why) skip.push({ name: p.name, why });
37 else start.push({ name: p.name, waves: waves(p.jobs), required: p.required });
38 }
39 return { start, skip };
42/** Why `p` doesn't start on `event`, or undefined when it does. */
43function notStarting(p: Pipeline, event: PipelineEvent, space: string): string | undefined {
44 if (event.kind === "hand" && event.pipeline !== p.name) return "another pipeline was started by hand";
45 const on =
46 event.kind === "schedule"
47 ? p.on.some((t) => typeof t !== "string" && t.every === event.every)
48 : p.on.includes(event.kind);
49 if (!on)
50 return event.kind === "schedule" ? `has no schedule "${event.every}"` : `doesn't run on ${triggerWord(event.kind)}`;
51 if (p.only && !p.only.includes(space)) return `runs only for ${p.only.join(", ")}, not ${space}`;
52 // The file check refuses this already; the plan holds the line too.
53 if (event.kind === "draft" && p.jobs.some((j) => j.secrets.length > 0))
54 return "holds secrets, and a draft runs unpushed code";
55 return undefined;
58/** The jobs in waves: each wave holds every job whose needs all came in earlier waves. */
59export function waves(jobs: readonly Job[]): string[][] {
60 const done = new Set<string>();
61 const out: string[][] = [];
62 let left = [...jobs];
63 while (left.length > 0) {
64 const wave = left.filter((j) => j.needs.every((n) => done.has(n)));
65 // A checked file has no cycles; stop rather than loop if one ever got here.
66 if (wave.length === 0) throw new Error(`jobs ${left.map((j) => j.name).join(", ")} wait on each other`);
67 for (const j of wave) done.add(j.name);
68 out.push(wave.map((j) => j.name));
69 left = left.filter((j) => !done.has(j.name));
70 }
71 return out;