# tools/core/entrypoints/pipeline.entrypoint.ts

> 176 lines of code and 29 definitions.

Tree: Coordination tree
Language: typescript
Layer: runtime
Canonical: https://banes-lab.com/anatomy/coordination#file-coordination-tools-core-entrypoints-pipeline-entrypoint-ts
Source text: https://banes-lab.com/assets/sources/source.1be656d23e7eaf20d7d9590be3d9a38ea4c803213f574832d8e89427922ba1d4.generated.txt

## Definitions

- `main` (lexical_declaration, line 77)
- `REPO_ROOT` (lexical_declaration, line 32)
- `RESTATES` (lexical_declaration, line 34, exported)
- `argValue` (lexical_declaration, line 41)
- `index` (lexical_declaration, line 42)
- `argList` (lexical_declaration, line 49)
- `out` (lexical_declaration, line 50)
- `i` (lexical_declaration, line 51)
- `value` (lexical_declaration, line 55)
- `current` (lexical_declaration, line 59)
- `k` (lexical_declaration, line 60)
- `argv` (lexical_declaration, line 78)
- `stageArg` (lexical_declaration, line 80)
- `stage` (lexical_declaration, line 81)
- `asked` (lexical_declaration, line 83)
- `declaredCaller` (lexical_declaration, line 85)
- `runAgent` (lexical_declaration, line 86)
- `runAt` (lexical_declaration, line 87)
- `standing` (lexical_declaration, line 93)
- `heal` (lexical_declaration, line 98)
- `joined` (lexical_declaration, line 117)
- `result` (lexical_declaration, line 122)
- `report` (lexical_declaration, line 131)
- `superseded` (lexical_declaration, line 173)
- `target` (lexical_declaration, line 174)
- `lines` (lexical_declaration, line 176)
- `mark` (lexical_declaration, line 177)
- `detail` (lexical_declaration, line 178)
- `unidentified` (lexical_declaration, line 195)

## Uses

- [tools/core/registries/claim.registry.ts](https://banes-lab.com/source/coordination/tools/core/registries/claim.registry.ts.md)
- [tools/core/strings/pipeline.strings.ts](https://banes-lab.com/source/coordination/tools/core/strings/pipeline.strings.ts.md)

## Source

```typescript
import {
    AGGREGATE_REPORT,
    type PipelineReport,
    joinLiveRun,
    ruleReportName,
    supersedingRun,
    writePipelineReport,
} from "../reporters/rule.reporter.ts";
import {
    BYPASSED_RUN,
    JOINED,
    STREAMED,
    SUPERSEDED,
    UNATTRIBUTED,
    claimedUnwritten,
    covered,
    healingHeld,
    narrowedRun,
    notMeasured,
    repairedWhileReading,
    runContended,
    scopeUnresolved,
    wroteOutsideScope,
} from "../strings/pipeline.strings.ts";
import { GENERATED_DIR, NO_FIX_FLAG } from "../constants/path.constants.ts";
import { STAGES, type Stage } from "../types/rule.types.ts";
import { claimStanding, releaseStanding } from "../registries/claim.registry.ts";
import { projectRoot } from "../../../config/surface.config.ts";
import { runPipeline } from "../orchestrators/pipeline.orchestrator.ts";
import { toPosix } from "../iterators/file.iterator.ts";

const REPO_ROOT = projectRoot();

export const RESTATES: readonly string[] = [
    "one_entry_point_staged_pipeline",
    "healing_is_default_in_every_entrypoint",
    "a_run_that_cannot_replace_the_aggregate_streams",
    "report_is_the_state",
];

const argValue = function argValue(argv: readonly string[], flag: string): string | null {
    const index = argv.indexOf(flag);
    if (index === -1) {
        return null;
    }
    return argv[index + 1] ?? null;
};

const argList = function argList(argv: readonly string[], flag: string): string[] {
    const out: string[] = [];
    for (let i = 0; i < argv.length; i += 1) {
        if (argv[i] !== flag) {
            continue;
        }
        const value = argv[i + 1];
        if (value === undefined) {
            continue;
        }
        let current = "";
        for (let k = 0; k < value.length; k += 1) {
            if (value[k] === ",") {
                if (current.length > 0) {
                    out.push(current);
                }
                current = "";
                continue;
            }
            current += value[k];
        }
        if (current.length > 0) {
            out.push(current);
        }
    }
    return out;
};

const main = async function main(): Promise<void> {
    const argv = process.argv.slice(2);

    const stageArg = argValue(argv, "--stage");
    const stage = stageArg !== null && STAGES.includes(stageArg as Stage) ? (stageArg as Stage) : null;

    const asked = !argv.includes(NO_FIX_FLAG);

    const declaredCaller = argValue(argv, "--agent");
    const runAgent = declaredCaller ?? "an undeclared caller";
    const runAt = Date.now();

    if (declaredCaller === null) {
        process.stdout.write(UNATTRIBUTED);
    }

    const standing = claimStanding(REPO_ROOT, argValue(argv, "--scope") ?? "whole", runAt, runAgent);
    if (standing.message !== null) {
        process.stdout.write(`OTHER RUNS  ${standing.message}\n`);
    }

    const heal = asked && standing.overlapping.length === 0;

    if (asked && !heal) {
        process.stdout.write(healingHeld(standing.overlapping.length, standing.overlapping));
    }

    if (standing.decision === "yield") {
        releaseStanding(REPO_ROOT, runAgent, runAt);
        process.stdout.write(JOINED);

        const joined = await joinLiveRun(REPO_ROOT, runAt);
        process.stdout.write(joined.message);
        process.exit(joined.code);
    }

    if (standing.covering.length > 0 && !heal) {
        releaseStanding(REPO_ROOT, runAgent, runAt);
        process.stdout.write(covered(standing.covering.length, standing.covering));

        const joined = await joinLiveRun(REPO_ROOT, runAt);
        process.stdout.write(joined.message);
        process.exit(joined.code);
    }

    const result = await runPipeline({
        bypass: argList(argv, "--bypass"),
        fix: heal,
        repoRoot: REPO_ROOT,
        ruleId: argValue(argv, "--rule"),
        scope: argValue(argv, "--scope"),
        stage,
    });

    const report: PipelineReport = {
        agent: runAgent,
        at: runAt,
        authoritative: result.authoritative,
        bypassed: result.bypassedAny,
        derivations: {
            healingHeldBy: standing.overlapping,
            movedByThisRun: result.movedByThisRun,
            priorRunIncomplete: standing.incomplete,
            stagesBypassed: result.stages.filter((s) => s.bypassed).map((s) => s.rule),
            stagesWalked: result.stages.filter((s) => !s.bypassed).map((s) => s.rule),
        },
        escaped: result.escaped,
        findings: result.findings,
        mode: heal ? "healing" : "held",
        moved: result.moved,
        mutated: heal,
        registered: result.registered,
        reports: result.stages.filter((s) => !s.bypassed).map((s) => `${GENERATED_DIR}/${ruleReportName(s.rule)}`),
        scanned: result.scanned,
        scope: result.scope,
        stages: result.stages,
        tool: "govern",
        unfulfilled: result.unfulfilled,
        verdict: result.findings.length === 0 ? "pass" : "fail",
        written: result.written,
    };

    if (result.escaped.length > 0) {
        process.stdout.write(wroteOutsideScope(result.escaped));
    }

    if (result.unfulfilled.length > 0) {
        process.stdout.write(claimedUnwritten(result.unfulfilled));
    }

    if (result.unresolvedScope !== undefined) {
        releaseStanding(REPO_ROOT, runAgent, runAt);
        process.stdout.write(scopeUnresolved(result.unresolvedScope));
        process.exit(2);
    }

    const superseded = report.authoritative ? supersedingRun(REPO_ROOT, report.at) : 0;
    const target = writePipelineReport(REPO_ROOT, report);

    const lines = result.stages.map((s) => {
        const mark = s.bypassed ? "BYPASS" : (s.findings === 0 ? "  pass" : "  FAIL");
        const detail = s.bypassed ? "skipped" : `${s.findings} findings, ${s.healed} healed`;
        return `  ${mark}  ${s.stage}/${s.rule}  ${detail}`;
    });

    process.stdout.write(
        `${report.verdict.toUpperCase()}  registered=${report.registered} scanned=${report.scanned} ` +
            `findings=${report.findings.length}\n${lines.length > 0 ? `${lines.join("\n")}\n` : ""}${
                report.bypassed ? BYPASSED_RUN : ""
            }${result.movedByThisRun.length === 0 ? "" : repairedWhileReading(result.movedByThisRun)}${
                result.moved.length === 0 ? "" : runContended(result.moved.length, result.moved)
            }${report.authoritative ? "" : narrowedRun(report.scope, AGGREGATE_REPORT)}${
                result.incomparable.length === 0 ? "" : notMeasured(result.incomparable)
            }${
                superseded === 0 ? "" : SUPERSEDED
            }${target === null ? STREAMED : `report: ${toPosix(REPO_ROOT, target)}\n`}`,
    );

    const unidentified = releaseStanding(REPO_ROOT, runAgent, runAt);
    if (unidentified !== null) {
        process.stdout.write(`${unidentified}\n`);
    }
    process.exit(report.verdict === "pass" ? 0 : 1);
};

void main();
```
