Skip to content
Myr

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 file

Take it

Merge branch 'build/702-swarm-plan'oscabrielbefore adoption8496778

line 1why, one step back
1/**

Merge build/comments-ops into mainoscabrielbefore adoptione4c407e

line 2why, one step back
2 * A small hand decoder: the scripts' allow-list grants `effect/process` only, so no `effect/cli`.

Merge branch 'build/702-swarm-plan'oscabrielbefore adoption8496778

lines 3–4why, one step back
3 *
4 * --stage <s> required: a stage `infra/stages.ts` declares

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 5why, one step back
5 * --project <name> default oscabriel/burrow

Merge branch 'build/702-swarm-plan'oscabrielbefore adoption8496778

lines 6–10why, one step back
6 * --scenario plain|video default plain
7 * --dry-run print the plan; claim nothing, start nothing8 * --json print JSON: the plan (dry run) or the run's summary9 * --only <handle>… run only these contributors (repeat, or comma-separate)10 * --timeout-min <n> each agent's run timeout, default 20

Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6

line 11why, one step back
11 * --view headless|herdr default headless; herdr runs each harness in a herdr pane

Merge branch 'build/702-swarm-plan'oscabrielbefore adoption8496778

lines 12–30why, one step back
12 * --url <origin> the platform, when not the stage's workers.dev origin
13 */14import { Handle, ProjectName } from "@repo/protocol/ids";15import { Effect, Option, Schema } from "effect";1617import { flagValue, stageOf } from "../../../infra/stages.ts";18import type { Stage } from "../../../infra/stages.ts";19import { roster } from "./roster.ts";20import { ScenarioName } from "./scenario.ts";21import { SwarmFailed } from "./workspace.ts";2223export interface SwarmOptions {24 readonly stage: Stage;25 readonly project: ProjectName;26 readonly scenario: ScenarioName;27 readonly dryRun: boolean;28 readonly json: boolean;29 readonly only: readonly Handle[];30 readonly timeoutMin: number;

Merge branch 'build/717-swarm-herdr-view'oscabrielbefore adoptionb2e66c8

line 31why, one step back
31 readonly view: ViewName;

Merge branch 'build/702-swarm-plan'oscabrielbefore adoption8496778

lines 32–35why, one step back
32 readonly url: Option.Option<string>;
33}3435export const USAGE =

Merge infra/drop-demo into mainoscabrielbefore adoptionc49b6bf

line 36why, one step back
36 "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>]";

Merge branch 'build/702-swarm-plan'oscabrielbefore adoption8496778

line 37why, one step back
37

Merge branch 'build/717-swarm-herdr-view'oscabrielbefore adoptionb2e66c8

lines 38–43why, one step back
38export const ViewName = Schema.Literals(["headless", "herdr"]);
3940export type ViewName = typeof ViewName.Type;4142const decodeView = Schema.decodeUnknownOption(ViewName);43

Merge branch 'build/702-swarm-plan'oscabrielbefore adoption8496778

lines 44–52why, one step back
44const VALUED = [
45 "--project",46 "--scenario",47 "--stage",48 "--timeout-min",49 "--url",50 "--view",51] as const;52

Merge infra/drop-demo into mainoscabrielbefore adoptionc49b6bf

line 53why, one step back
53const SWITCHES = ["--dry-run", "--json"] as const;

Merge branch 'build/702-swarm-plan'oscabrielbefore adoption8496778

lines 54–158why, one step back
54
55const failed = (reason: string) => new SwarmFailed({ reason });5657const onlyOf = (argv: readonly string[]) =>58 argv.flatMap((arg, at) => {59 if (arg.startsWith("--only=")) {60 return arg.slice("--only=".length).split(",");61 }6263 if (arg !== "--only") {64 return [];65 }6667 const rest = argv.slice(at + 1);68 const end = rest.findIndex((value) => value.startsWith("--"));6970 return (end === -1 ? rest : rest.slice(0, end)).flatMap((value) =>71 value.split(",")72 );73 });7475const flagsIn = (argv: readonly string[]) =>76 argv.flatMap((arg) =>77 arg.startsWith("--") ? [arg.split("=")[0] ?? arg] : []78 );7980const isValued = (flag: string): flag is (typeof VALUED)[number] =>81 VALUED.some((valued) => valued === flag);8283const straysIn = (argv: readonly string[]) => {84 const strays: string[] = [];85 let takes: "none" | "one" | "many" = "none";8687 for (const arg of argv) {88 if (arg.startsWith("--")) {89 const bare = !arg.includes("=");9091 if (bare && isValued(arg)) {92 takes = "one";93 } else {94 takes = bare && arg === "--only" ? "many" : "none";95 }96 } else if (takes === "none") {97 strays.push(arg);98 } else if (takes === "one") {99 takes = "none";100 }101 }102103 return strays;104};105106const originOf = (raw: string) =>107 Option.filter(108 Option.liftThrowable((url: string) => new URL(url))(raw),109 ({ protocol }) => protocol === "https:" || protocol === "http:"110 ).pipe(Option.as(raw.replace(/\/+$/u, "")));111112const decodeProject = Schema.decodeUnknownOption(ProjectName);113114const decodeScenario = Schema.decodeUnknownOption(ScenarioName);115116export const optionsOf = (117 argv: readonly string[]118): Effect.Effect<SwarmOptions, SwarmFailed> =>119 Effect.gen(function* () {120 const known = new Set<string>([...VALUED, ...SWITCHES, "--only"]);121 const strange = flagsIn(argv).filter((flag) => !known.has(flag));122123 if (strange.length > 0) {124 return yield* failed(`unknown flag ${strange.join(", ")}: ${USAGE}`);125 }126127 // A switch read only by its bare spelling: `--dry-run=yes` would128 // otherwise pass as a known flag and leave the run live.129 const switchValue = argv.find((arg) =>130 SWITCHES.some((flag) => arg.startsWith(`${flag}=`))131 );132133 if (switchValue !== undefined) {134 return yield* failed(135 `${switchValue.split("=")[0] ?? switchValue} takes no value: ${USAGE}`136 );137 }138139 const valueless = VALUED.find(140 (flag) => argv.includes(flag) && Option.isNone(flagValue(argv, flag))141 );142143 if (valueless !== undefined) {144 return yield* failed(`${valueless} needs a value: ${USAGE}`);145 }146147 const strays = straysIn(argv);148149 if (strays.length > 0) {150 return yield* failed(151 `unexpected argument ${strays.join(", ")}: ${USAGE}`152 );153 }154155 const stage = yield* stageOf(argv).pipe(156 Effect.mapError(({ message }) => failed(`${message}. ${USAGE}`))157 );158

Add a prod stage served on the brand's own domainoscabrielbefore adoption78a5062

lines 159–160why, one step back
159 if (stage === "prod") {
160 return yield* failed(

Merge infra/drop-demo into mainoscabrielbefore adoptionc49b6bf

line 161why, one step back
161 "--stage prod: the swarm's contributors are fictional, so run it on a dev stage"

Merge branch 'build/702-swarm-plan'oscabrielbefore adoption8496778

lines 162–164why, one step back
162 );
163 }164

Merge branch 'build/717-swarm-herdr-view'oscabrielbefore adoptionb2e66c8

lines 165–168why, one step back
165 const rawView = Option.getOrElse(
166 flagValue(argv, "--view"),167 () => "headless"168 );

Merge branch 'build/702-swarm-plan'oscabrielbefore adoption8496778

line 169why, one step back
169

Merge branch 'build/717-swarm-herdr-view'oscabrielbefore adoptionb2e66c8

lines 170–172why, one step back
170 const view = yield* Effect.fromOption(decodeView(rawView)).pipe(
171 Effect.mapError(() => failed(`--view ${rawView}: headless or herdr`))172 );

Merge branch 'build/702-swarm-plan'oscabrielbefore adoption8496778

lines 173–175why, one step back
173
174 const rawProject = Option.getOrElse(175 flagValue(argv, "--project"),

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 176why, one step back
176 () => "oscabriel/burrow"

Merge branch 'build/702-swarm-plan'oscabrielbefore adoption8496778

lines 177–247why, one step back
177 );
178179 const project = yield* Effect.fromOption(decodeProject(rawProject)).pipe(180 Effect.mapError(() => failed(`--project ${rawProject} names no project`))181 );182183 const rawScenario = Option.getOrElse(184 flagValue(argv, "--scenario"),185 () => "plain"186 );187188 const scenario = yield* Effect.fromOption(decodeScenario(rawScenario)).pipe(189 Effect.mapError(() => failed(`--scenario ${rawScenario}: plain or video`))190 );191192 const rawTimeout = Option.getOrElse(193 flagValue(argv, "--timeout-min"),194 () => "20"195 );196197 const timeoutMin = Number(rawTimeout);198199 if (!Number.isInteger(timeoutMin) || timeoutMin < 1) {200 return yield* failed(201 `--timeout-min ${rawTimeout}: a whole number of minutes, 1 or more`202 );203 }204205 const named = onlyOf(argv).filter((handle) => handle !== "");206207 const only = yield* Effect.forEach(named, (handle) =>208 Effect.fromNullishOr(209 roster.find((contributor) => contributor.handle === handle)?.handle210 ).pipe(211 Effect.mapError(() =>212 failed(213 `--only ${handle} is not in the roster: ${roster.map((contributor) => contributor.handle).join(", ")}`214 )215 )216 )217 );218219 const onlyGiven = argv.some(220 (arg) => arg === "--only" || arg.startsWith("--only=")221 );222223 if (onlyGiven && only.length === 0) {224 return yield* failed("--only names no contributor");225 }226227 const url = yield* Effect.transposeOption(228 Option.map(flagValue(argv, "--url"), (raw) =>229 Effect.fromOption(originOf(raw)).pipe(230 Effect.mapError(() =>231 failed(232 `--url ${raw}: an http(s) origin, such as https://platform.example`233 )234 )235 )236 )237 );238239 return {240 dryRun: argv.includes("--dry-run"),241 json: argv.includes("--json"),242 only: [...new Set(only)].map((handle) => Handle.make(handle)),243 project,244 scenario,245 stage,246 timeoutMin,247 url,

Merge branch 'build/717-swarm-herdr-view'oscabrielbefore adoptionb2e66c8

line 248why, one step back
248 view,

Merge branch 'build/702-swarm-plan'oscabrielbefore adoption8496778

lines 249–250why, one step back
249 };
250 });