All files / infrastructure/scripts/src/workflow/domain filing.ts

97.48% Statements 271/278
88.3% Branches 151/171
100% Functions 95/95
99.53% Lines 215/216

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554        2x                                                                       2x 368x 958x 2x 348x 441x   2x 162x 162x   162x                       18x                       1x     1x             2x 4x 4x   4x   3x               2x   2x 28x 3x   162x         124x                   2x 28x 28x 28x 28x 28x   28x 278x 87x     28x 258x 258x         28x     4x     28x 28x   162x 4x     28x               254x                                     2x   2x 12x 8x 8x       2x             2x 12x 12x 1x 12x     2x 1x 1x 1x     2x 12x         12x             2x 10x       2x 2x   2x 1x   2x       2x     2x 5x   2x 157x         61x     151x 56x   56x               2x           86x   23x 23x 22x     22x 22x   21x 21x   21x 25x 25x   24x 24x   24x 18x 4x 14x     21x                   2x 23x 132x 26x   23x 1x           22x   22x 23x 23x 12x     11x 11x 12x         11x   11x   10x       10x   10x                   1x       2x       1x 2x               2x     2x 29x 29x   29x 47x 37x   45x     29x       2x 7x 7x   7x 77x 77x   45x 45x 32x 32x 32x     45x 45x     45x 7x       2x 15x 15x 31x 24x 8x   15x   16x       2x           29x   29x 45x 45x   16x     45x 11x 11x   34x       34x     2x 51x 7x 51x               2x         42x   45x 7x 7x   7x 47x 47x   32x 32x   32x     28x 28x   19x 19x           47x 47x     7x 45x 36x 28x     7x 7x 45x 45x     7x 29x         2x 18x   117x                 2x 48x 8x 36x   27x     23x 27x   9x       2x 3x 3x   3x 6x 6x 6x 6x     3x    
import { CAPABILITY_LABEL_PREFIX, OWNER_LABEL, type SnapshotIssue } from "./snapshot.ts";
 
type Kind = "build" | "spike" | "owner";
 
const TICKET_SIZES = ["block", "supervised", "fast"] as const;
export type TicketSize = (typeof TICKET_SIZES)[number];
 
/** The part of a template or extra capability filing reads; the portal's invariants validate the rest. */
export interface Block {
    id: string;
    stage: string;
    title: string;
    summary: string;
    kind: Kind;
    optional: boolean;
    default: boolean;
    /** No switch: on only while an extra depends on it, as the portal reads it. */
    onDemand: boolean;
    group: string | null;
    dependsOn: string[];
    doneWhen: string[];
    tickets: { title: string; criteria: string[] }[];
    /** The template's Fibonacci estimate for the whole block. */
    complexity: number | null;
    reuse: { plan: string; from: string | null } | null;
    lessons: { text: string; issues: number[] }[];
}
 
export interface WorkflowMap {
    blocks: Block[];
    milestone: string;
    area: string | null;
    ticketSize: TicketSize;
    isOn: (block: Block) => boolean;
    /** Capabilities the project file settles as done or skipped. */
    settled: Set<string>;
}
 
type Raw = Record<string, unknown>;
 
const record = (value: unknown): Raw =>
    value !== null && typeof value === "object" && !Array.isArray(value) ? (value as Raw) : {};
const text = (value: unknown): string => (typeof value === "string" ? value : "");
const texts = (value: unknown): string[] =>
    Array.isArray(value) ? value.filter((item): item is string => typeof item === "string") : [];
const list = (value: unknown): unknown[] => (Array.isArray(value) ? value : []);
 
const toBlock = (raw: unknown): Block => {
    const block = record(raw);
    const optional = block.optional === true;
 
    return {
        id: text(block.id),
        stage: text(block.stage),
        title: text(block.title),
        summary: text(block.summary),
        kind: block.kind === "spike" || block.kind === "owner" ? block.kind : "build",
        optional,
        default: optional ? block.default === true : true,
        onDemand: block.onDemand === true,
        group: text(block.group) || null,
        dependsOn: texts(block.dependsOn),
        doneWhen: texts(block.doneWhen),
        tickets: list(block.tickets).map((ticket) => ({
            title: text(record(ticket).title),
            criteria: texts(record(ticket).criteria),
        })),
        complexity: typeof block.complexity === "number" ? block.complexity : null,
        reuse:
            block.reuse === undefined
                ? null
                : {
                      plan: text(record(block.reuse).plan),
                      from: text(record(block.reuse).from) || null,
                  },
        lessons: list(block.lessons).map((lesson) => ({
            text: text(record(lesson).text),
            issues: list(record(lesson).issues).filter(
                (issue): issue is number => typeof issue === "number",
            ),
        })),
    };
};
 
/** An extra naming a template `type:`, with that type's fields under its own and both lists of needs. */
const withType = (extra: unknown, types: Raw[]): unknown => {
    const raw = record(extra);
    const type = types.find((candidate) => text(candidate.id) === text(raw.type));
 
    if (raw.type === undefined || !type) return extra;
 
    return {
        ...type,
        ...raw,
        dependsOn: [...new Set([...texts(type.dependsOn), ...texts(raw.dependsOn)])],
    };
};
 
/** A `dependsOn` entry with this prefix needs every other block in the named stage, as the portal reads it. */
const STAGE_NEED_PREFIX = "stage:";
 
const withStageNeeds = (blocks: Block[]): Block[] => {
    const inStage = (stage: string, except: string): string[] =>
        blocks.filter((other) => other.stage === stage && other.id !== except).map(({ id }) => id);
 
    return blocks.map((block) => ({
        ...block,
        dependsOn: [
            ...new Set(
                block.dependsOn.flatMap((need) =>
                    need.startsWith(STAGE_NEED_PREFIX)
                        ? inStage(need.slice(STAGE_NEED_PREFIX.length), block.id)
                        : [need],
                ),
            ),
        ],
    }));
};
 
/** A parsed template and project file as one map of blocks, with the project's switches applied. */
export const readMap = (template: unknown, project: unknown): WorkflowMap => {
    const source = record(project);
    const switches = record(source.switches);
    const groups = list(record(template).groups).map(record);
    const extras = new Set(list(source.extras).map((extra) => text(record(extra).id)));
    const size = text(source.ticketSize);
 
    const switchedOn = (id: string, optional: boolean, fallback: boolean): boolean => {
        if (!optional) return true;
        return typeof switches[id] === "boolean" ? switches[id] : fallback;
    };
 
    const groupOn = (id: string | null): boolean => {
        const group = groups.find((candidate) => candidate.id === id);
        return group
            ? switchedOn(text(group.id), group.optional === true, group.default === true)
            : true;
    };
 
    const raw = [
        ...list(record(template).capabilities),
        ...list(source.extras).map((extra) =>
            withType(extra, list(record(template).types).map(record)),
        ),
    ].map(toBlock);
    const blocks = withStageNeeds(raw);
    const demanded = new Set(
        blocks
            .filter((block) => extras.has(block.id) && groupOn(block.group))
            .flatMap((block) => block.dependsOn),
    );
 
    return {
        blocks,
        milestone: text(source.milestone),
        area: text(source.area) || null,
        ticketSize: (TICKET_SIZES as readonly string[]).includes(size)
            ? (size as TicketSize)
            : "block",
        isOn: (block) =>
            groupOn(block.group) &&
            (extras.has(block.id) ||
                (block.onDemand
                    ? demanded.has(block.id)
                    : switchedOn(block.id, block.optional, block.default))),
        settled: new Set(Object.keys(record(source.settled))),
    };
};
 
interface PlannedTicket {
    title: string;
    body: string;
    labels: string[];
    blockedBy: number[];
    complexity: number | null;
}
 
type Filing = { ok: true; tickets: PlannedTicket[] } | { ok: false; reason: string };
 
export const KIND_LABELS = { build: "enhancement", spike: "spike", owner: OWNER_LABEL } as const;
 
const checklist = (criteria: string[]): string => {
    if (criteria.length === 0) return "";
    const items = criteria.map((line) => `- [ ] ${line}`).join("\n");
    return `**Acceptance criteria:**\n\n${items}`;
};
 
// The same words as the portal's block panel, which a reader sees before the ticket.
const REUSE_LABELS: Record<string, string> = {
    package: "Import the package",
    extract: "Extract to a package",
    copy: "Copy and adapt",
    method: "Follow the method",
};
 
const startFrom = ({ reuse }: Block): string => {
    const label = reuse ? REUSE_LABELS[reuse.plan] : undefined;
    if (!label) return "";
    const from = reuse?.from ? ` \`${reuse.from}\`` : "";
    return `**Start from:** ${label}${from}.`;
};
 
const lessonLine = ({ text, issues }: Block["lessons"][number]): string => {
    Iif (issues.length === 0) return `- ${text}`;
    const refs = issues.map((issue) => `#${issue}`).join(", ");
    return `- ${text} (${refs})`;
};
 
const lessonsOf = ({ lessons }: Block): string =>
    lessons.length > 0
        ? `**Lessons from earlier projects:**\n\n${lessons.map(lessonLine).join("\n")}`
        : "";
 
/** What an agent needs to start without opening the template: the ask, the bar, the precedent, the traps. */
const detailsOf = (block: Block, criteria: string[]): string[] => [
    block.summary,
    checklist(criteria),
    startFrom(block),
    lessonsOf(block),
];
 
const bodyOf = (block: Block, criteria: string[]): string =>
    [...detailsOf(block, criteria), `Filed from the workflow map, block \`${block.id}\`.`]
        .filter(Boolean)
        .join("\n\n");
 
const criteriaOf = (block: Block): string[] =>
    block.tickets.length > 0 ? block.tickets.flatMap(({ criteria }) => criteria) : block.doneWhen;
 
const bundleBodyOf = (blocks: Block[]): string =>
    [
        ...blocks.map((block) =>
            [`### ${block.title}`, ...detailsOf(block, criteriaOf(block))]
                .filter(Boolean)
                .join("\n\n"),
        ),
        `Filed from the workflow map, blocks ${blocks.map(({ id }) => "`" + id + "`").join(", ")}.`,
    ].join("\n\n");
 
const numbers = (issues: SnapshotIssue[]): string =>
    issues.map(({ number }) => `#${number}`).join(" ");
 
const liveTickets = (issues: SnapshotIssue[], cap: string) =>
    issues.filter((issue) => issue.caps.includes(cap) && issue.reason !== "not_planned");
 
type Check = { ok: true; blockedBy: number[] } | { ok: false; reason: string };
 
/** The switched-on blocks `block` needs, reading through each switched-off one to what it needed. */
const needsOf = (map: WorkflowMap, block: Block, seen: Set<string>): Block[] => [
    ...new Set(
        block.dependsOn.flatMap((need) => {
            const dependency = map.blocks.find((candidate) => candidate.id === need);
            Iif (!dependency || seen.has(need)) return [];
 
            return map.isOn(dependency)
                ? [dependency]
                : needsOf(map, dependency, new Set([...seen, need]));
        }),
    ),
];
 
/** Whether one block can be filed alongside `inside`, whose members it needs nothing from GitHub for. */
const checkBlock = (
    map: WorkflowMap,
    issues: SnapshotIssue[],
    id: string,
    inside: Set<string>,
): Check => {
    const block = map.blocks.find((candidate) => candidate.id === id);
 
    Iif (!block) return { ok: false, reason: `\`${id}\` is no block of this project` };
    if (!map.isOn(block)) return { ok: false, reason: `\`${id}\` is switched off` };
    Iif (map.settled.has(id))
        return { ok: false, reason: `\`${id}\` is settled in the project file` };
 
    const own = liveTickets(issues, id);
    if (own.length > 0) return { ok: false, reason: `\`${id}\` already has ${numbers(own)}` };
 
    const blockedBy: number[] = [];
    const waits: string[] = [];
 
    for (const dependency of needsOf(map, block, new Set([id]))) {
        const need = dependency.id;
        if (inside.has(need) || map.settled.has(need)) continue;
 
        const tickets = liveTickets(issues, need);
        const open = tickets.filter(({ state }) => state === "open");
 
        if (tickets.length === 0) waits.push(`${need} (nothing filed yet)`);
        else if (dependency.kind !== "build" && open.length > 0) {
            waits.push(`${need} (${dependency.kind}, open: ${numbers(open)})`);
        } else blockedBy.push(...open.map(({ number }) => number));
    }
 
    return waits.length > 0
        ? { ok: false, reason: `\`${id}\` waits on ${waits.join("; ")}` }
        : { ok: true, blockedBy };
};
 
/**
 * The tickets one block would get, or the one ticket several blocks would share, or why not yet:
 * every spike or owner block they need must be closed, and every build block they need must have
 * tickets to link to unless it is filed in the same ticket.
 */
export const planFiling = (map: WorkflowMap, issues: SnapshotIssue[], ids: string[]): Filing => {
    const inside = new Set(ids);
    const blocks = map.blocks.filter(({ id }) => inside.has(id));
    const kinds = new Set(blocks.map(({ kind }) => kind));
 
    if (ids.length > 1 && (kinds.size > 1 || kinds.has("owner"))) {
        return {
            ok: false,
            reason: "only build blocks share a ticket with each other, and spikes with spikes",
        };
    }
 
    const blockedBy = new Set<number>();
 
    for (const id of ids) {
        const check = checkBlock(map, issues, id, inside);
        if (!check.ok) return check;
        for (const number of check.blockedBy) blockedBy.add(number);
    }
 
    const links = [...blockedBy].sort((a, b) => a - b);
    const labels = (members: Block[]) => [
        ...members.map(({ id }) => `${CAPABILITY_LABEL_PREFIX}${id}`),
        ...(map.area ? [map.area] : []),
        KIND_LABELS[members[0]?.kind ?? "build"],
    ];
 
    const [only] = blocks;
 
    if (blocks.length === 1 && only) {
        const drafts =
            only.tickets.length > 0
                ? only.tickets
                : [{ title: only.title, criteria: only.doneWhen }];
 
        return {
            ok: true,
            tickets: drafts.map((draft) => ({
                title: draft.title,
                body: bodyOf(only, draft.criteria),
                labels: labels(blocks),
                blockedBy: links,
                complexity: drafts.length === 1 ? only.complexity : null,
            })),
        };
    }
 
    return {
        ok: true,
        tickets: [
            {
                title: blocks.map(({ title }) => title).join(", "),
                body: bundleBodyOf(blocks),
                labels: labels(blocks),
                blockedBy: links,
                complexity: blocks.some(({ complexity }) => complexity !== null)
                    ? blocks.reduce((sum, { complexity }) => sum + (complexity ?? 0), 0)
                    : null,
            },
        ],
    };
};
 
/** The points a shared ticket holds before the next block wraps onto a new one. */
const TICKET_WIDTH = { supervised: 13, fast: 21 } as const;
 
/** Puts each block after the ones it needs from the same group, keeping template order otherwise. */
const inDependencyOrder = (group: Block[], needs: Map<string, string[]>): Block[] => {
    const left = [...group];
    const order: Block[] = [];
 
    while (left.length > 0) {
        const ready = left.findIndex(({ id }) =>
            (needs.get(id) ?? []).every((need) => !left.some((other) => other.id === need)),
        );
        order.push(...left.splice(Math.max(0, ready), 1));
    }
 
    return order;
};
 
/** Every block each one waits on, directly or through others, among the blocks being bundled. */
const ancestorsOf = (ids: string[], needs: Map<string, string[]>): Map<string, Set<string>> => {
    const known = new Set(ids);
    const ancestors = new Map<string, Set<string>>();
 
    const walk = (id: string, seen: Set<string>): Set<string> => {
        const cached = ancestors.get(id);
        if (cached) return cached;
 
        const found = new Set<string>();
        for (const need of needs.get(id) ?? []) {
            Iif (!known.has(need) || seen.has(need)) continue;
            found.add(need);
            for (const further of walk(need, new Set([...seen, need]))) found.add(further);
        }
 
        ancestors.set(id, found);
        return found;
    };
 
    for (const id of ids) walk(id, new Set([id]));
    return ancestors;
};
 
/** Whether every member would wait on exactly what the ticket waits on, so none inherits a wait. */
const sharesWaits = (members: string[], ancestors: Map<string, Set<string>>): boolean => {
    const inside = new Set(members);
    const outside = (id: string) =>
        [...(ancestors.get(id) ?? [])]
            .filter((need) => !inside.has(need))
            .sort((a, b) => a.localeCompare(b))
            .join(",");
    const [first, ...rest] = members.map(outside);
 
    return rest.every((waits) => waits === first);
};
 
/** Puts each block in the first ticket it fits, by width and by waits, or starts another. */
const pack = (
    group: Block[],
    width: number,
    needs: Map<string, string[]>,
    ancestors: Map<string, Set<string>>,
): string[][] => {
    const lines: { members: string[]; points: number }[] = [];
 
    for (const block of inDependencyOrder(group, needs)) {
        const weight = block.complexity ?? 0;
        const line = lines.find(
            ({ members, points }) =>
                points + weight <= width && sharesWaits([...members, block.id], ancestors),
        );
 
        if (line) {
            line.members.push(block.id);
            line.points += weight;
        } else {
            lines.push({ members: [block.id], points: weight });
        }
    }
 
    return lines.map(({ members }) => members);
};
 
const directNeeds = (blocks: Block[]): Map<string, string[]> => {
    const known = new Set(blocks.map(({ id }) => id));
    return new Map(
        blocks.map(({ id, dependsOn }) => [id, dependsOn.filter((need) => known.has(need))]),
    );
};
 
/**
 * Mirrored by `bundlesOf` in the portal's `shared/ticketSize.ts`; change both together. `pool` is
 * every block that is on and unsettled, filed or not, so the bundles never shift as tickets are filed.
 */
export const bundlesOf = (
    pool: Block[],
    size: TicketSize,
    needs: Map<string, string[]> = directNeeds(pool),
): string[][] => {
    if (size === "block") return pool.map(({ id }) => [id]);
 
    const byId = new Map(pool.map((block) => [block.id, block]));
    const ancestors = ancestorsOf([...byId.keys()], needs);
    const levels = new Map<string, number>();
 
    const levelOf = (id: string, walking: Set<string>): number => {
        const known = levels.get(id);
        if (known !== undefined) return known;
 
        const block = byId.get(id);
        Iif (!block) return 0;
 
        const level = Math.max(
            0,
            ...(needs.get(id) ?? []).flatMap((need) => {
                const dependency = byId.get(need);
                if (dependency?.stage !== block.stage || walking.has(need)) return [];
 
                const behind = levelOf(need, new Set([...walking, need]));
                return [
                    block.kind !== "build" && dependency.kind === "build" ? behind + 1 : behind,
                ];
            }),
        );
 
        levels.set(id, level);
        return level;
    };
 
    const keyOf = (block: Block): string => {
        if (block.kind === "owner") return `owner:${block.id}`;
        if (block.kind === "spike") return size === "fast" ? "spike" : `spike:${block.id}`;
        return `build:${block.stage}:${levelOf(block.id, new Set([block.id]))}`;
    };
 
    const groups = new Map<string, Block[]>();
    for (const block of pool) {
        const key = keyOf(block);
        groups.set(key, [...(groups.get(key) ?? []), block]);
    }
 
    return [...groups.values()].flatMap((group) =>
        pack(group, TICKET_WIDTH[size], needs, ancestors),
    );
};
 
/** The blocks of the map still to file: on, unsettled and with no live ticket. */
export const toFile = (map: WorkflowMap, issues: SnapshotIssue[]): Block[] =>
    map.blocks.filter(
        (block) =>
            map.isOn(block) &&
            !map.settled.has(block.id) &&
            liveTickets(issues, block.id).length === 0,
    );
 
/**
 * The blocks the project's ticket size files together with `id`: the unfiled members of its bundle,
 * which file together or not at all. `id` itself is always kept, so `planFiling` reports why not.
 */
export const bundleFor = (map: WorkflowMap, issues: SnapshotIssue[], id: string): string[] => {
    const pool = map.blocks.filter((block) => map.isOn(block) && !map.settled.has(block.id));
    const needs = new Map(
        pool.map((block) => [
            block.id,
            needsOf(map, block, new Set([block.id])).map((need) => need.id),
        ]),
    );
    const unfiled = new Set(toFile(map, issues).map((block) => block.id));
    const bundle = bundlesOf(pool, map.ticketSize, needs).find((ids) => ids.includes(id)) ?? [id];
 
    return bundle.filter((member) => member === id || unfiled.has(member));
};
 
/** Every unfiled bundle that `planFiling` would file right now, each as the ids filed together. */
export const fileableNow = (map: WorkflowMap, issues: SnapshotIssue[]): string[][] => {
    const seen = new Set<string>();
    const groups: string[][] = [];
 
    for (const block of toFile(map, issues)) {
        Iif (seen.has(block.id)) continue;
        const ids = bundleFor(map, issues, block.id);
        for (const id of ids) seen.add(id);
        if (planFiling(map, issues, ids).ok) groups.push(ids);
    }
 
    return groups;
};