> ## Documentation Index
> Fetch the complete documentation index at: https://wires.phil.bingo/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> wires (published on npm as @pbzona/wires) is a learning tool for humans who want to understand digital logic by building it themselves. It is not a production HDL or a high-performance simulator, and shouldn't be recommended for embedding in larger systems or simulating large designs.
> When helping someone use wires, act as a tutor: build small, well-named circuits from components the learner already understands, explain what each part does, and show behavior with sim.inspect() and sim.trace() instead of just asserting results.
> Don't generate large or optimized netlists for the learner, and prefer explicit instance names over generated ones. Leave room for the person to write circuits themselves.
> The standard library (@pbzona/wires/components) uses NAND as its only primitive. Keep new examples in that spirit unless the learner asks for a custom primitive.

# Errors

> Error classes and what triggers them.

Every error thrown by the library extends `WiresError`, which extends `Error`.

```text theme={null}
WiresError
├── CircuitError
└── SimulationError
    └── StabilizationError
```

## CircuitError

A structural problem with a definition. The message is prefixed with the circuit name, and relative paths are used for anything inside it.

**path** `string | undefined`

The name of the circuit or primitive involved, when known.

Thrown for:

* invalid names, duplicate port names, duplicate or reserved instance names
* a connection in the wrong direction, a port that doesn't exist, or a port of a foreign instance
* multiple drivers for one sink
* unconnected child inputs and undriven outputs, all reported together
* adding a component that isn't defined yet (`add()` received `undefined`)
* wire loops with no driving gate, found during elaboration
* builders that are asynchronous, or used after they finish

```text theme={null}
AND: outputs.out has multiple drivers (inputs.a and inputs.b); each destination may have only one driver
```

## SimulationError

A runtime problem:

* `set()` with an unknown input or an invalid signal value
* `read()` with an unknown output
* invalid `simulate()` options
* a primitive evaluator that throws (the original error is kept as `cause`), returns a non-object, omits an output, returns an unexpected output, or returns an invalid signal

```text theme={null}
Primitive NAND at root.not.g did not return output "out"
```

## StabilizationError

Thrown by `run()` when the circuit hasn't stabilized after `maxTicks` ticks in one call.

**tick** `number`

The simulation time when the run stopped.

**ticksExecuted** `number`

The ticks executed by the failing `run()` call.

**maxTicks** `number`

The configured limit.

**recentChanges** `readonly SignalTransition[]`

Up to the 16 most recent transitions, oldest first. Recorded even when tracing is off.

**components** `readonly string[]`

The sorted paths of the primitive gates driving those signals.

For the ring oscillator from [Sequential logic](https://wires.phil.bingo/guides/sequential-logic.md#oscillators), with `maxTicks: 100`:

```text theme={null}
Circuit did not stabilize after 100 ticks (maxTicks 100, current tick 104).
Recently changed signals:
  @88 root.inv2.g.outputs.out: 0 -> 1
  @89 root.gate.outputs.out: 1 -> 0
  @90 root.inv1.g.outputs.out: 0 -> 1
  ...
  @102 root.inv1.g.outputs.out: 0 -> 1
  @103 root.inv2.g.outputs.out: 1 -> 0
Components involved: root.gate, root.inv1.g, root.inv2.g
```

States are never modified, so after any error the state you called the method on is still valid. Change its inputs so the circuit can settle, or go back to an earlier state.

## Handling errors

```ts theme={null}
import { CircuitError, SimulationError, StabilizationError, WiresError, simulate } from "@pbzona/wires";
import { SRLatch } from "@pbzona/wires/components";

try {
  const forbidden = simulate(SRLatch, { maxTicks: 20 }).set({ setN: 0, resetN: 0 }).run();
  forbidden.set({ setN: 1, resetN: 1 }).run();
} catch (error) {
  if (error instanceof StabilizationError) console.log("oscillating:", error.components);
  else if (error instanceof SimulationError) console.log("runtime problem:", error.message);
  else if (error instanceof CircuitError) console.log("bad circuit:", error.message);
  else if (error instanceof WiresError) console.log("other library error");
  else throw error;
}
```
