args.ts
This file
Path
scripts/lib/swarm/args.ts
250 lines · 7.3 KiB · TypeScript
Last changed by
Merge infra/drop-demo into mainoscabrielbefore adoptionc49b6bf
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251/** * A small hand decoder: the scripts' allow-list grants `effect/process` only, so no `effect/cli`. * * --stage <s> required: a stage `infra/stages.ts` declares * --project <name> default oscabriel/burrow * --scenario plain|video default plain * --dry-run print the plan; claim nothing, start nothing * --json print JSON: the plan (dry run) or the run's summary * --only <handle>… run only these contributors (repeat, or comma-separate) * --timeout-min <n> each agent's run timeout, default 20 * --view headless|herdr default headless; herdr runs each harness in a herdr pane * --url <origin> the platform, when not the stage's workers.dev origin */import { Handle, ProjectName } from "@repo/protocol/ids";import { Effect, Option, Schema } from "effect";
import { flagValue, stageOf } from "../../../infra/stages.ts";import type { Stage } from "../../../infra/stages.ts";import { roster } from "./roster.ts";import { ScenarioName } from "./scenario.ts";import { SwarmFailed } from "./workspace.ts";
export interface SwarmOptions { readonly stage: Stage; readonly project: ProjectName; readonly scenario: ScenarioName; readonly dryRun: boolean; readonly json: boolean; readonly only: readonly Handle[]; readonly timeoutMin: number; readonly view: ViewName; readonly url: Option.Option<string>;}
export const USAGE = "bun scripts/swarm.ts --stage <dev-…> [--project oscabriel/burrow] [--scenario plain|video] [--dry-run] [--json] [--only <handle>…] [--timeout-min 20] [--view headless|herdr] [--url <origin>]";
export const ViewName = Schema.Literals(["headless", "herdr"]);
export type ViewName = typeof ViewName.Type;
const decodeView = Schema.decodeUnknownOption(ViewName);
const VALUED = [ "--project", "--scenario", "--stage", "--timeout-min", "--url", "--view",] as const;
const SWITCHES = ["--dry-run", "--json"] as const;
const failed = (reason: string) => new SwarmFailed({ reason });
const onlyOf = (argv: readonly string[]) => argv.flatMap((arg, at) => { if (arg.startsWith("--only=")) { return arg.slice("--only=".length).split(","); }
if (arg !== "--only") { return []; }
const rest = argv.slice(at + 1); const end = rest.findIndex((value) => value.startsWith("--"));
return (end === -1 ? rest : rest.slice(0, end)).flatMap((value) => value.split(",") ); });
const flagsIn = (argv: readonly string[]) => argv.flatMap((arg) => arg.startsWith("--") ? [arg.split("=")[0] ?? arg] : [] );
const isValued = (flag: string): flag is (typeof VALUED)[number] => VALUED.some((valued) => valued === flag);
const straysIn = (argv: readonly string[]) => { const strays: string[] = []; let takes: "none" | "one" | "many" = "none";
for (const arg of argv) { if (arg.startsWith("--")) { const bare = !arg.includes("=");
if (bare && isValued(arg)) { takes = "one"; } else { takes = bare && arg === "--only" ? "many" : "none"; } } else if (takes === "none") { strays.push(arg); } else if (takes === "one") { takes = "none"; } }
return strays;};
const originOf = (raw: string) => Option.filter( Option.liftThrowable((url: string) => new URL(url))(raw), ({ protocol }) => protocol === "https:" || protocol === "http:" ).pipe(Option.as(raw.replace(/\/+$/u, "")));
const decodeProject = Schema.decodeUnknownOption(ProjectName);
const decodeScenario = Schema.decodeUnknownOption(ScenarioName);
export const optionsOf = ( argv: readonly string[]): Effect.Effect<SwarmOptions, SwarmFailed> => Effect.gen(function* () { const known = new Set<string>([...VALUED, ...SWITCHES, "--only"]); const strange = flagsIn(argv).filter((flag) => !known.has(flag));
if (strange.length > 0) { return yield* failed(`unknown flag ${strange.join(", ")}: ${USAGE}`); }
// A switch read only by its bare spelling: `--dry-run=yes` would // otherwise pass as a known flag and leave the run live. const switchValue = argv.find((arg) => SWITCHES.some((flag) => arg.startsWith(`${flag}=`)) );
if (switchValue !== undefined) { return yield* failed( `${switchValue.split("=")[0] ?? switchValue} takes no value: ${USAGE}` ); }
const valueless = VALUED.find( (flag) => argv.includes(flag) && Option.isNone(flagValue(argv, flag)) );
if (valueless !== undefined) { return yield* failed(`${valueless} needs a value: ${USAGE}`); }
const strays = straysIn(argv);
if (strays.length > 0) { return yield* failed( `unexpected argument ${strays.join(", ")}: ${USAGE}` ); }
const stage = yield* stageOf(argv).pipe( Effect.mapError(({ message }) => failed(`${message}. ${USAGE}`)) );
if (stage === "prod") { return yield* failed( "--stage prod: the swarm's contributors are fictional, so run it on a dev stage" ); }
const rawView = Option.getOrElse( flagValue(argv, "--view"), () => "headless" );
const view = yield* Effect.fromOption(decodeView(rawView)).pipe( Effect.mapError(() => failed(`--view ${rawView}: headless or herdr`)) );
const rawProject = Option.getOrElse( flagValue(argv, "--project"), () => "oscabriel/burrow" );
const project = yield* Effect.fromOption(decodeProject(rawProject)).pipe( Effect.mapError(() => failed(`--project ${rawProject} names no project`)) );
const rawScenario = Option.getOrElse( flagValue(argv, "--scenario"), () => "plain" );
const scenario = yield* Effect.fromOption(decodeScenario(rawScenario)).pipe( Effect.mapError(() => failed(`--scenario ${rawScenario}: plain or video`)) );
const rawTimeout = Option.getOrElse( flagValue(argv, "--timeout-min"), () => "20" );
const timeoutMin = Number(rawTimeout);
if (!Number.isInteger(timeoutMin) || timeoutMin < 1) { return yield* failed( `--timeout-min ${rawTimeout}: a whole number of minutes, 1 or more` ); }
const named = onlyOf(argv).filter((handle) => handle !== "");
const only = yield* Effect.forEach(named, (handle) => Effect.fromNullishOr( roster.find((contributor) => contributor.handle === handle)?.handle ).pipe( Effect.mapError(() => failed( `--only ${handle} is not in the roster: ${roster.map((contributor) => contributor.handle).join(", ")}` ) ) ) );
const onlyGiven = argv.some( (arg) => arg === "--only" || arg.startsWith("--only=") );
if (onlyGiven && only.length === 0) { return yield* failed("--only names no contributor"); }
const url = yield* Effect.transposeOption( Option.map(flagValue(argv, "--url"), (raw) => Effect.fromOption(originOf(raw)).pipe( Effect.mapError(() => failed( `--url ${raw}: an http(s) origin, such as https://platform.example` ) ) ) ) );
return { dryRun: argv.includes("--dry-run"), json: argv.includes("--json"), only: [...new Set(only)].map((handle) => Handle.make(handle)), project, scenario, stage, timeoutMin, url, view, }; });