# ADR-0007: Runtime API and Simulation Engine Boundaries

## Status

Accepted

## Context

The simulator consists of multiple layers with distinct responsibilities:

- a host-facing API layer used by benchmarks and user code,
- a discrete-event simulation engine that executes requests,
- device components that model hardware behavior.

Without strict boundaries, orchestration logic can leak into components,
or simulation internals can become entangled with user-facing APIs.

This ADR defines clear responsibility boundaries between:

- runtime API,
- simulation engine (sim_engine),
- hardware components.

---

## Decision

### D1. Runtime API is host-facing orchestration only

The runtime API represents host/driver-level behavior and MUST:

- expose high-level operations (tensor deployment, kernel launch),
- submit requests only to endpoint components (e.g., IO_CPU),
- await completion via futures/handles,
- own and persist host-side metadata (tensor allocation maps, kernel bindings).

The runtime API MUST NOT:

- hardcode hop-by-hop routing or fan-out,
- directly invoke internal components (M_CPU, PE_CPU, engines),
- embed topology- or routing-specific assumptions.

---

### D2. Simulation engine wires components and tracks completion

The simulation engine (sim_engine) MUST:

- wire components at initialization (create port stores + start wire
  processes per the component port/wire framework — ADR-0015),
- inject requests into the compiled topology graph at entry components
  (e.g., PCIE_EP for memory operations, IO_CPU for kernel launch),
- schedule and execute events using a discrete-event model,
- manage correlation ids and completion tracking.

The simulation engine MUST NOT:

- define tensor semantics,
- define kernel execution policies,
- expose internal graph details to the runtime API,
- walk the topology path during request execution,
- call component `run()` methods directly,
- track per-hop latency or decompose fan-out (components own this).

---

### D3. Components own fan-out and aggregation

Device-side components MUST:

- fan-out requests to downstream domains
  (IO_CPU → M_CPU → PE_CPU → schedulers/engines),
- aggregate completion and failure signals,
- propagate results deterministically upstream.

Neither the runtime API nor the simulation engine may orchestrate
component-level fan-out explicitly.

---

## Consequences

- Runtime APIs remain stable as topology and routing evolve.
- Simulation internals can change without affecting user-facing code.
- Component implementations remain swappable via DI.

---

## Links

- SPEC R4, R7, R8
- ADR-0008 (Tensor deployment)
- ADR-0009 (Kernel execution)
- ADR-0015 (Component port/wire model and engine role)
- ADR-0010 (CLI surface and execution semantics — runtime API consumer)
