# Experiment Ledger

**A scientific system of record for R&D labs.**
`experiment-ledger.infinity-sdg.com`. An Infinity Software Development Group product built on Kalos/ADL.

Every lab has to answer two questions to be reproducible:

| Question | Where Experiment Ledger keeps the answer |
|---|---|
| **What did we plan to do?** | campaign → DBTL cycle → design → run *plan* (parameters, start time, protocol version) |
| **What actually happened?** | run *actuals* → operational events → deviations → results, with raw-data checksums |

It closes the **Design-Build-Test-Learn** loop. A cycle can't move to *Learn* until results exist for it, and it can't close until a learning is published. The next cycle's designs can come from a scientist or a model. A model-proposed design must record the model's name and version, so every result traces back to what proposed it.

## What it enforces

The rules below are enforced on the server. Users can't skip them, even by calling the API directly.

- **The plan is frozen once a run starts.** Differences are recorded as deviations, never by editing the plan.
- **A run can't complete while a critical deviation is still open.** It can still be marked failed, which is an honest outcome.
- **No run starts on an instrument that is out of calibration.** A daily schedule also takes lapsed instruments offline.
- **Results come only from runs that actually ran.** A captured value is never edited: you reject it and capture a new one. Raw-data links must carry a SHA-256 checksum.
- **Protocol versions are written once.** Each run keeps a copy of the version it followed.
- **Samples keep their lineage.** For example, powder → pellet, through a `parent` link.
- **Every change is audited** with the old and new values, who made it and when. Runs, cycles and their state changes also appear in a readable history timeline.

## Resources

`campaign` · `cycle` · `design` · `protocol` · `instrument` · `sample` · `run` · `event` · `deviation` · `result` · `learning`

Views: **planVersusActual**, **openDeviations**, **modelProposedDesigns**.
Roles: `scientist`, `lab_tech`, `ml_engineer`, `reviewer`, `admin`.

## Demo data

The demo data is a materials-discovery lab with 10 campaigns. The solid-electrolyte campaign tells the full story:
- **Cycle 1 is closed.** It has four conductivity runs and two phase-purity runs, each with a recorded deviation. One result was rejected for a bad electrode contact. A model-proposed design crossed 1 mS/cm, and the learning that points to cycle 2 is published.
- **Cycle 2 is mid-test.** One run is still going: it has a critical glovebox-oxygen deviation open, so it can't complete yet, and its first result is flagged. A second run is planned.
- **Elsewhere:** the polymer campaign is concluded, the perovskite aging run failed when its lamp power supply tripped, and one furnace's calibration has lapsed.

Regenerate the seed with `python3 seeds/gen_seed.py`.

## Run it

This repo holds the product: the ADL domain, demo seed and site config. It runs on a [Kalos](https://github.com/datajango/kalos) checkout, as one of its `*.infinity-sdg.com` sub-domain sites.

```bash
export KALOS_REPO=~/kalos              # defaults to ../kalos beside this repo
scripts/kalos.sh validate              # static checks (adlref + config + seed)
scripts/kalos.sh start                 # certs + fresh DB + kalosd on :9205
scripts/kalos.sh verify                # boot, seed, exercise over TLS, stop
```

Each command first copies this repo into `adl/v0.5/sub-domains/experiment-ledger/` inside Kalos, and registers the host in `subdomains.md` if needed. Edit files here, not in the Kalos copy.

Sign in as `admin` / `Admin123!`, or as `scientist1`, `labtech1`, `mleng1` or `reviewer1` / `Demo123!`.

## Reselling

The product is generic: no customer names in the code or data. To brand it for a customer, change `site.json` (`host`, `slug`), the port in `config.json`, and the theme of `seeds/gen_seed.py`. Then regenerate the seed and validate.
