---
epic: 13
story: 13.2
title: "Dependency Graph & Lazy Evaluation"
status: draft
---

## Epic 13 — Performance & Reliability
**Goal:** Instant evaluation, never lose work.

### Story 13.2: Dependency Graph & Lazy Evaluation

As a CalcPad user,
I want only the lines affected by a change to re-evaluate,
So that editing a variable on line 5 of a 500-line sheet does not re-evaluate unrelated lines.

**Acceptance Criteria:**

**Given** the engine maintains a directed acyclic graph (DAG) of variable dependencies across all lines
**When** a variable's value changes on a specific line
**Then** only lines that directly or transitively depend on that variable are marked dirty and re-evaluated
**And** lines with no dependency on the changed variable are not re-evaluated

**Given** a sheet where line 3 defines `tax = 0.08` and lines 10, 25, and 40 reference `tax`
**When** the user changes `tax` to `0.10` on line 3
**Then** only lines 3, 10, 25, 40 (and any lines that depend on those) are re-evaluated
**And** all other lines retain their cached results without recomputation

**Given** a circular dependency is introduced (e.g., `a = b + 1` and `b = a + 1`)
**When** the engine builds or updates the DAG
**Then** the circular dependency is detected before evaluation
**And** an error is displayed on the offending lines indicating a circular reference
**And** the rest of the sheet continues to evaluate normally

**Given** a line is deleted that other lines depend on
**When** the DAG is updated
**Then** all lines that referenced the deleted variable are marked dirty
**And** those lines display an "undefined variable" error on next evaluation
