# Prime Grid

`scripts/prime-grid.js` generates a deterministic table of prime-related families over a bounded range.

It exists because hand-written model tables often mix correct families with small errors. The grid should be generated from predicates instead.

## Run

```powershell
cd C:\Users\issda\AetherDesk
npm run prime:grid -- --limit=1000
```

## Families

- `prime` - ordinary positive primes.
- `twin_prime_lower` / `twin_prime_upper` - primes paired at distance 2.
- `sophie_germain_prime` - prime `p` where `2p + 1` is prime.
- `safe_prime` - prime `p` where `(p - 1) / 2` is prime.
- `superprime` - prime `p_n` whose prime index `n` is also prime.
- `mersenne_prime` - prime of form `2^p - 1`.
- `fermat_prime` - one of the known Fermat primes `{3, 5, 17, 257, 65537}`.
- `semiprime` - product of exactly two primes, counted with multiplicity.
- `prime_power` - `p^k` where `p` is prime and `k >= 2`.
- `fermat_generated` - generated by powers of `2` and the known Fermat primes.
- `signed_associates` - for a positive prime `p`, the associated prime elements `[-p, p]` in `Z`.

## Fermat-65537 Coordinates

Every row includes:

```text
n = q * 65537 + r
```

with:

- `q` as the terminal-Fermat quotient.
- `r` as the local residue.
- `aligned` when `r = 0`.

This preserves the useful ruler idea without claiming that every number is Fermat-generated.

## Optical Transition Telemetry

Every classified row also includes `optical`, a deterministic local prime-gap readout:

- `depth` - optical depth from local log-gap curvature plus cold-spot / gradient signal.
- `mode` - `penetration` below `d* = 1.8`, `retention` at or above it.
- `score` - bounded optical channel score in `[0.01, 0.99]`.
- `log_r` / `log_q` - scale and curvature terms from nearby prime gaps.
- `ultra_visible` / `sub_visible` - fine recent curvature and broader historical-neighbor signals.

This is the desktop-facing bridge to the fog-of-war / optical laser work. It is telemetry, not a primality proof.

## Fog Probe Slice

For research runs that do not need the full grid payload, AetherDesk can export a probe-ready slice:

```text
GET /api/math/prime-grid/fog-slice?limit=1000
GET /api/math/prime-grid/fog-slice?limit=1000&mode=retention
GET /api/math/prime-grid/fog-slice?limit=1000&family=prime
```

The slice schema is:

```text
aetherdesk_prime_fog_probe_slice_v0
```

It contains only rows with available optical telemetry and flattens each event into the channels the fog/optical probe expects:

- `n`
- `classes`
- `fermat_65537`
- `mode`
- `depth`
- `score`
- `log_r`
- `log_q`
- `cold_spot`
- `gradient_abs`
- `ultra_visible`
- `sub_visible`
- `retention_strength`
- `retention_boost`
- `prime_window`

In the desktop, use the Export selector:

- `grid JSON` - full Prime Grid payload
- `fog slice` - filtered visible rows in probe-ready format

## AetherDesk Surface (added 2026-06-20)

- API: `GET /api/math/prime-grid?limit=N`
- API: `GET /api/math/prime-grid/fog-slice?limit=N`
- Desktop window: `primegrid` (range input, family counts, classified rows, optical depth/mode/score)
- Terminal: `primegrid`, `prime grid`, `primes`, `open primegrid`
- MCP tools (bounded):
  - `prime_grid_generate` → bounded table
  - `prime_grid_classify` → single n classification (capped range)

Verified via Playwright render + full test suite (80 passed).

This gives a practical, auditable exploration surface for the prime manifold work (families + coordinates can feed fog-of-war probes, imaginary paths, thermal/optical classifiers).
