# tools/core/inspectors/gate.inspector.ts

> 58 lines of code and 12 definitions.

Tree: Coordination tree
Language: typescript
Layer: processing
Canonical: https://banes-lab.com/anatomy/coordination#file-coordination-tools-core-inspectors-gate-inspector-ts
Source text: https://banes-lab.com/source/coordination/tools/core/inspectors/gate.inspector.ts.txt

Listed in [tools/core/inspectors](https://banes-lab.com/api/source/coordination/tools/core/inspectors.md), after [tools/core/inspectors/fence.inspector.ts](https://banes-lab.com/source/coordination/tools/core/inspectors/fence.inspector.ts.md) and before [tools/core/inspectors/index.inspector.ts](https://banes-lab.com/source/coordination/tools/core/inspectors/index.inspector.ts.md).

## Definitions

- `rendered` (lexical_declaration, line 27)
- `judgeBranch` (lexical_declaration, line 44, exported)
- `Exercise` (interface_declaration, line 6)
- `disagreements` (lexical_declaration, line 11)
- `out` (lexical_declaration, line 12)
- `keys` (lexical_declaration, line 13)
- `seen` (lexical_declaration, line 16)
- `wanted` (lexical_declaration, line 17)
- `exercise` (lexical_declaration, line 33)
- `tree` (lexical_declaration, line 34)
- `{ error, observed }` (lexical_declaration, line 45, exported)
- `apart` (lexical_declaration, line 50, exported)

## Uses

- [tools/core/strings/gate.strings.ts](https://banes-lab.com/source/coordination/tools/core/strings/gate.strings.ts.md)

## Source

```typescript
import type { BranchFixture, BranchObservation } from "../types/fixture.types.ts";
import { branchExercised, branchThrew } from "../strings/gate.strings.ts";
import type { GateOutcome } from "../types/gate.types.ts";
import { growFixtureTree } from "../generators/fixture.generator.ts";

interface Exercise {
    readonly observed: BranchObservation | null;
    readonly error: string;
}

const disagreements = function disagreements(observed: BranchObservation, expected: BranchObservation): string[] {
    const out: string[] = [];
    const keys = new Set([...Object.keys(expected), ...Object.keys(observed)]);

    for (const key of keys) {
        const seen = observed[key];
        const wanted = expected[key];
        if (seen === wanted) {
            continue;
        }
        out.push(`${key} ${String(seen)} where ${String(wanted)} is declared`);
    }

    return out;
};

const rendered = function rendered(observation: BranchObservation): string {
    return Object.entries(observation)
        .map(([key, value]) => `${key} ${String(value)}`)
        .join(" · ");
};

const exercise = function exercise(fixture: BranchFixture): Exercise {
    const tree = growFixtureTree(fixture.seed);
    try {
        return { error: "", observed: fixture.exercise(tree.root) };
    } catch (error) {
        return { error: String(error), observed: null };
    } finally {
        tree.release();
    }
};

export const judgeBranch = function judgeBranch(fixture: BranchFixture): GateOutcome {
    const { error, observed } = exercise(fixture);
    if (observed === null) {
        return { detail: branchThrew(error), rule: `${fixture.subject} · ${fixture.branch}`, state: "noisy" };
    }

    const apart = disagreements(observed, fixture.expect);
    if (apart.length > 0) {
        return {
            detail:
                `the branch was exercised and its effect DISAGREES with what the fixture declares — ${apart.join(", ")}. ` +
                "A branch is proven by its effect on the tree rather than by the code it returns, because a message " +
                "reporting a refusal over a tree it already changed reads exactly like a refusal that changed nothing",
            rule: `${fixture.subject} · ${fixture.branch}`,
            state: "silent",
        };
    }

    return {
        detail: branchExercised(rendered(observed)),
        rule: `${fixture.subject} · ${fixture.branch}`,
        state: "proven",
    };
};
```
