# 1 · Which wire path can the board use?

Welcome. You’ll help a program choose where a wire should run on a circuit board, the flat panel that connects the parts inside a device. We draw the board as a **grid** of squares. A wire that passes too close to another part can make an unwanted connection. Count the squares between them to check the gap.

A program trained on earlier boards gives each possible path a score. We call this program a **specialist** because it does one job. A **guard** checks each path against a rule. The rule for this board requires a gap of at least two squares. The guard marks a path that breaks the rule as off-limits (`MASKED`) before the specialist can choose it. Which gap would you allow, one square or three?

You will add the guard, let the specialist choose among the remaining paths, then check its choice. These checks and choices run in order. That sequence is the **cascade**.

On the test board named `b-104`, six routes compete. The connection to route (the **net**) is named `clock_diff`. Its **differential clock** is a pair of wires that carry a timing signal. The path called `direct_north` scores highest. It comes within one square of an obstacle at row 4, column 7. If you switch off the guard, which path will the program choose?

Open a terminal in the `courses/specialist-cascade-lab` folder. The `node` command runs the JavaScript program that checks this board.

```sh
node lab.mjs guard b-104
```

## The `0.84` short circuit

Each candidate route reports four path metrics: `clr` (**clearance** in **grid** cells to the nearest **obstacle**), `vias` (vertical **via** links between **board** layers used out of the budget), `xtalk` (**crosstalk**, or `0..1` **signal** cross-wire noise), and `cong` (`0..1` fraction of busy traffic cells). Look at `b-104`:

```text
  direct_north               clr=1 vias=0/2 xtalk=0.32 cong=0.00  MASKED (ERR_CLEARANCE_SHORT)
  dual_via_underpass         clr=3 vias=2/2 xtalk=0.05 cong=0.00  LEGAL
  Guard OFF: direct_north (score=0.84) -> ERR_CLEARANCE_SHORT
  Guard ON:  dual_via_underpass (score=0.57) -> OK (4/6 legal)
```

With the guard off, the learned specialist scores each route from 0 to 1. It picks `direct_north` with the highest score, `0.84`. Because **obstacle** `(4, 7)` leaves `clr=1` below the `2`-cell rule, `direct_north` fails with `ERR_CLEARANCE_SHORT`. With the guard on, `4` of the `6` routes stay legal and `dual_via_underpass` (`0.57`) wins cleanly.

The picture shows one board. Its totals count boards that break any of three rules: clearance, via budget or crosstalk. Crowding is checked separately.

![Board b-104 has an obstacle at (4,7) that leaves direct_north at 1-cell clearance. Guard OFF picks direct_north at 0.84 and trips ERR_CLEARANCE_SHORT. Guard ON masks bad routes and verifies dual_via_underpass with no clearance, via or crosstalk violations in the 24-board run.](figures/constraint-guard-gate.svg)

## Mask before top-score selection, verify after output

The specialist scores routes using numbers it learned during training, called **model weights**. Two checks enforce the rules even when an unsafe route scores highest.

1. An **admissibility mask** (`maskLegalActions`) inspects each route's saved metrics object `g` before **argmax** (picking the highest score) and drops broken routes.
2. An output **verifier** (`verifyRoute`) checks limits across the whole path. If more than a quarter of its squares are busy (`cong > 0.25`), it returns `ERR_CONGESTION_TRAP`.

Here are the three hard limits enforced by `maskLegalActions` in `model.mjs`:

```js
if (g.minClearance < 2) reasons.push('ERR_CLEARANCE_SHORT');
if (g.viasUsed > board.viaBudget) reasons.push('ERR_VIA_BUDGET');
if (g.crosstalk > 0.35) reasons.push('ERR_CLOCK_CROSSTALK');
```

Any route failing **clearance** (`< 2`), **via budget** (`viasUsed > viaBudget`), or **crosstalk** (`> 0.35`) is removed before **argmax** runs.

| Mode | `b-104` pick | Score | Code | Boards that fail a clearance, via or crosstalk rule |
| --- | --- | --- | --- | --- |
| Guard OFF | `direct_north` | `0.84` | `ERR_CLEARANCE_SHORT` | `22 / 24` |
| Guard ON | `dual_via_underpass` | `0.57` | `OK` | `0 / 24` |

Test **board** `b-401` (`viaBudget = 0`) to watch `ERR_VIA_BUDGET` fire alongside **clearance** checks:

```sh
node lab.mjs guard b-401
```

Build your own `maskLegalActions` and `verifyRoute` in `workspace/starter.mjs`, then grade Milestone 1 with `node check.mjs m1`.

With the guard on, all 24 boards meet the three rules in the table. The verifier also checks how much of the chosen path is busy. On 10 boards, the **imitation** model, which copies past choices, first picks a path with more than a quarter of its squares busy. The verifier rejects those 10 paths with `ERR_CONGESTION_TRAP` and accepts the other 14. In Lesson 2 you use that error to choose a different path on a retry.
