{
 "site": "Experiment Ledger",
 "domain": "ledger",
 "description": "Scientific system of record. Connects scientific intent (campaigns, DBTL cycles, designs, planned runs) to physical reality (what ran, on which instrument, with which parameters, what went differently, and what was measured), so every result can be traced back to the plan it answered.",
 "roles": [
  "scientist",
  "lab_tech",
  "ml_engineer",
  "reviewer",
  "admin"
 ],
 "machines": [
  {
   "name": "campaign",
   "description": "A research goal pursued through repeated DBTL cycles.",
   "field": "status",
   "machine": {
    "initial": "proposed",
    "states": {
     "proposed": {
      "description": "",
      "transitions": {
       "activate": {
        "to": "active",
        "guards": [
         "scientist",
         "admin"
        ],
        "requiredFields": [],
        "sets": {
         "startedOn": "now()"
        },
        "rules": []
       },
       "abandon": {
        "to": "abandoned",
        "guards": [
         "scientist",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       }
      }
     },
     "active": {
      "description": "",
      "transitions": {
       "conclude": {
        "to": "concluded",
        "guards": [
         "scientist",
         "admin"
        ],
        "requiredFields": [],
        "sets": {
         "concludedOn": "now()"
        },
        "rules": []
       },
       "abandon": {
        "to": "abandoned",
        "guards": [
         "scientist",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       }
      }
     },
     "concluded": {
      "description": "",
      "transitions": {}
     },
     "abandoned": {
      "description": "",
      "transitions": {}
     }
    }
   }
  },
  {
   "name": "cycle",
   "description": "One Design-Build-Test-Learn iteration. The state machine IS the loop: design \u2192 build \u2192 test \u2192 learn \u2192 closed. A cycle cannot enter `learn` until at least one result exists for it, and cannot close until a learning has been written \u2014 the two controls that keep the loop closed rather than merely documented.\n",
   "field": "status",
   "machine": {
    "initial": "design",
    "states": {
     "design": {
      "description": "",
      "transitions": {
       "start_build": {
        "to": "build",
        "guards": [
         "scientist",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       }
      }
     },
     "build": {
      "description": "",
      "transitions": {
       "start_test": {
        "to": "test",
        "guards": [
         "scientist",
         "lab_tech",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       }
      }
     },
     "test": {
      "description": "",
      "transitions": {
       "start_learn": {
        "to": "learn",
        "guards": [
         "scientist",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": [
         {
          "name": "learnNeedsResults",
          "description": "The loop cannot move to Learn with nothing measured.",
          "action": "reject",
          "message": "No results have been captured for this cycle yet."
         }
        ]
       }
      }
     },
     "learn": {
      "description": "",
      "transitions": {
       "close": {
        "to": "closed",
        "guards": [
         "scientist",
         "reviewer",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": [
         {
          "name": "closeNeedsALearning",
          "description": "A cycle closes only when what it taught has been written down.",
          "action": "reject",
          "message": "Publish a learning for this cycle before closing it."
         }
        ]
       }
      }
     },
     "closed": {
      "description": "",
      "transitions": {}
     }
    }
   }
  },
  {
   "name": "design",
   "description": "A candidate to build and test \u2014 a composition, formulation or process. Designs come from scientists or from models; a model-proposed design must name the model and its version, so a result can always be traced to what proposed it (AI provenance).\n",
   "field": "status",
   "machine": {
    "initial": "proposed",
    "states": {
     "proposed": {
      "description": "",
      "transitions": {
       "approve": {
        "to": "approved",
        "guards": [
         "scientist",
         "reviewer",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       },
       "reject": {
        "to": "rejected",
        "guards": [
         "scientist",
         "reviewer",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       }
      }
     },
     "approved": {
      "description": "",
      "transitions": {
       "mark_built": {
        "to": "built",
        "guards": [
         "lab_tech",
         "scientist",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       }
      }
     },
     "built": {
      "description": "",
      "transitions": {}
     },
     "rejected": {
      "description": "",
      "transitions": {}
     }
    }
   }
  },
  {
   "name": "protocol",
   "description": "A versioned procedure. The version string is copied onto every run that uses it, so a run always says which version it followed even after the protocol is amended.\n",
   "field": "status",
   "machine": {
    "initial": "draft",
    "states": {
     "draft": {
      "description": "",
      "transitions": {
       "release": {
        "to": "released",
        "guards": [
         "scientist",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       }
      }
     },
     "released": {
      "description": "",
      "transitions": {
       "retire": {
        "to": "retired",
        "guards": [
         "scientist",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       }
      }
     },
     "retired": {
      "description": "",
      "transitions": {}
     }
    }
   }
  },
  {
   "name": "instrument",
   "description": "An automation or analysis instrument. Calibration gates use.",
   "field": "status",
   "machine": {
    "initial": "available",
    "states": {
     "available": {
      "description": "",
      "transitions": {
       "take_down": {
        "to": "maintenance",
        "guards": [
         "lab_tech",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       }
      }
     },
     "maintenance": {
      "description": "",
      "transitions": {
       "return_to_service": {
        "to": "available",
        "guards": [
         "lab_tech",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       },
       "retire": {
        "to": "retired",
        "guards": [
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       }
      }
     },
     "retired": {
      "description": "",
      "transitions": {}
     }
    }
   }
  },
  {
   "name": "sample",
   "description": "A physical sample or asset with lineage. `parent` records what it was derived from (aliquot, transformation, prep), so any measurement can be walked back to its source material.\n",
   "field": "status",
   "machine": {
    "initial": "registered",
    "states": {
     "registered": {
      "description": "",
      "transitions": {
       "use": {
        "to": "in_use",
        "guards": [
         "lab_tech",
         "scientist",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       },
       "dispose": {
        "to": "disposed",
        "guards": [
         "lab_tech",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       }
      }
     },
     "in_use": {
      "description": "",
      "transitions": {
       "consume": {
        "to": "consumed",
        "guards": [
         "lab_tech",
         "scientist",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       },
       "dispose": {
        "to": "disposed",
        "guards": [
         "lab_tech",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       }
      }
     },
     "consumed": {
      "description": "",
      "transitions": {}
     },
     "disposed": {
      "description": "",
      "transitions": {}
     }
    }
   }
  },
  {
   "name": "run",
   "description": "The pivot of the ledger: one execution, holding the PLAN (plannedParams, plannedStart, protocol version) beside the REALITY (actualParams, startedAt, endedAt). Anything that differs is recorded as a deviation, and a run cannot complete with a critical deviation still open.\n",
   "field": "status",
   "machine": {
    "initial": "planned",
    "states": {
     "planned": {
      "description": "",
      "transitions": {
       "start": {
        "to": "running",
        "guards": [
         "lab_tech",
         "scientist",
         "admin"
        ],
        "requiredFields": [],
        "sets": {
         "startedAt": "now()"
        },
        "rules": [
         {
          "name": "noRunOnAnOutOfCalibrationInstrument",
          "description": "A run may not start on an instrument whose calibration has lapsed.",
          "action": "reject",
          "message": "The instrument is out of calibration or not available. Calibrate or choose another instrument."
         }
        ]
       },
       "cancel": {
        "to": "cancelled",
        "guards": [
         "scientist",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       }
      }
     },
     "running": {
      "description": "",
      "transitions": {
       "complete": {
        "to": "completed",
        "guards": [
         "lab_tech",
         "scientist",
         "admin"
        ],
        "requiredFields": [],
        "sets": {
         "endedAt": "now()"
        },
        "rules": [
         {
          "name": "criticalDeviationsBlockCompletion",
          "description": "A run with an open critical deviation has not finished being explained. It can still FAIL (that is a true account), but it cannot be marked complete.\n",
          "action": "reject",
          "message": "Review the open critical deviations on this run before completing it."
         }
        ]
       },
       "fail": {
        "to": "failed",
        "guards": [
         "lab_tech",
         "scientist",
         "admin"
        ],
        "requiredFields": [
         "failureReason"
        ],
        "sets": {
         "endedAt": "now()"
        },
        "rules": []
       }
      }
     },
     "completed": {
      "description": "",
      "transitions": {}
     },
     "failed": {
      "description": "",
      "transitions": {}
     },
     "cancelled": {
      "description": "",
      "transitions": {}
     }
    }
   }
  },
  {
   "name": "deviation",
   "description": "A difference between plan and reality, written down: which parameter, what was planned, what actually happened, and whether it matters. Deviations are the ledger's answer to \"what actually happened?\".\n",
   "field": "status",
   "machine": {
    "initial": "open",
    "states": {
     "open": {
      "description": "",
      "transitions": {
       "review": {
        "to": "reviewed",
        "guards": [
         "reviewer",
         "scientist",
         "admin"
        ],
        "requiredFields": [],
        "sets": {
         "reviewedBy": "$user.id"
        },
        "rules": []
       }
      }
     },
     "reviewed": {
      "description": "",
      "transitions": {
       "close": {
        "to": "closed",
        "guards": [
         "reviewer",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": [
         {
          "name": "deviationClosesWithAnExplanation",
          "description": "Closing a deviation without saying why it happened loses the record.",
          "action": "reject",
          "message": "Explain the deviation before closing it."
         }
        ]
       }
      }
     },
     "closed": {
      "description": "",
      "transitions": {}
     }
    }
   }
  },
  {
   "name": "result",
   "description": "A measurement, with provenance: the run that produced it, the sample it was taken from, where the raw data lives, and a checksum of that data. `cycle` is a copy on the flat row so the learn step can count results per cycle without traversal.\n",
   "field": "status",
   "machine": {
    "initial": "captured",
    "states": {
     "captured": {
      "description": "",
      "transitions": {
       "verify": {
        "to": "verified",
        "guards": [
         "scientist",
         "reviewer",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       },
       "reject": {
        "to": "rejected",
        "guards": [
         "scientist",
         "reviewer",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       }
      }
     },
     "verified": {
      "description": "",
      "transitions": {}
     },
     "rejected": {
      "description": "",
      "transitions": {}
     }
    }
   }
  },
  {
   "name": "learning",
   "description": "What the cycle taught \u2014 the L in DBTL \u2014 and what to design next.",
   "field": "status",
   "machine": {
    "initial": "draft",
    "states": {
     "draft": {
      "description": "",
      "transitions": {
       "publish": {
        "to": "published",
        "guards": [
         "scientist",
         "reviewer",
         "admin"
        ],
        "requiredFields": [],
        "sets": {},
        "rules": []
       }
      }
     },
     "published": {
      "description": "",
      "transitions": {}
     }
    }
   }
  }
 ],
 "resources": {
  "campaign": {
   "description": "A research goal pursued through repeated DBTL cycles.",
   "object": {
    "id": {
     "type": "uuid",
     "primaryKey": true,
     "protected": true,
     "allowUserProvided": true
    },
    "code": {
     "type": "string",
     "required": true,
     "unique": true,
     "maxLength": 24
    },
    "title": {
     "type": "string",
     "required": true,
     "maxLength": 200
    },
    "objective": {
     "type": "text",
     "required": true
    },
    "area": {
     "type": "string",
     "required": true,
     "enum": [
      "medicine",
      "materials",
      "energy",
      "chemistry",
      "other"
     ]
    },
    "lead": {
     "type": "string",
     "required": true,
     "maxLength": 100
    },
    "startedOn": {
     "type": "date"
    },
    "concludedOn": {
     "type": "date"
    },
    "status": {
     "type": "string",
     "required": true,
     "default": "proposed"
    }
   },
   "validation": {
    "concludesAfterStart": {
     "rule": "concludedOn == null || startedOn == null || concludedOn >= startedOn",
     "message": "A campaign cannot conclude before it started."
    }
   },
   "stateMachine": {
    "field": "status",
    "initial": "proposed",
    "states": {
     "proposed": {
      "transitions": {
       "activate": {
        "to": "active",
        "guards": [
         "scientist",
         "admin"
        ],
        "sets": {
         "startedOn": "now()"
        }
       },
       "abandon": {
        "to": "abandoned",
        "guards": [
         "scientist",
         "admin"
        ]
       }
      }
     },
     "active": {
      "transitions": {
       "conclude": {
        "to": "concluded",
        "guards": [
         "scientist",
         "admin"
        ],
        "sets": {
         "concludedOn": "now()"
        }
       },
       "abandon": {
        "to": "abandoned",
        "guards": [
         "scientist",
         "admin"
        ]
       }
      }
     },
     "concluded": {},
     "abandoned": {}
    }
   },
   "permissions": {
    "list": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "get": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "create": [
     "scientist",
     "admin"
    ],
    "update": [
     "scientist",
     "admin"
    ],
    "delete": [
     "admin"
    ]
   },
   "features": {
    "audit": true
   },
   "indexes": [
    {
     "fields": [
      "status"
     ]
    }
   ]
  },
  "cycle": {
   "description": "One Design-Build-Test-Learn iteration. The state machine IS the loop: design \u2192 build \u2192 test \u2192 learn \u2192 closed. A cycle cannot enter `learn` until at least one result exists for it, and cannot close until a learning has been written \u2014 the two controls that keep the loop closed rather than merely documented.\n",
   "object": {
    "id": {
     "type": "uuid",
     "primaryKey": true,
     "protected": true,
     "allowUserProvided": true
    },
    "campaign": {
     "type": "reference",
     "references": "adl.ledger.ledger.campaign",
     "required": true
    },
    "number": {
     "type": "integer",
     "required": true,
     "min": 1
    },
    "hypothesis": {
     "type": "text",
     "required": true
    },
    "status": {
     "type": "string",
     "required": true,
     "default": "design"
    }
   },
   "validation": {
    "numberIsWrittenOnce": {
     "rule": "isCreate() || !fieldChanged('number')",
     "message": "A cycle number is permanent once assigned."
    }
   },
   "stateMachine": {
    "field": "status",
    "initial": "design",
    "states": {
     "design": {
      "transitions": {
       "start_build": {
        "to": "build",
        "guards": [
         "scientist",
         "admin"
        ]
       }
      }
     },
     "build": {
      "transitions": {
       "start_test": {
        "to": "test",
        "guards": [
         "scientist",
         "lab_tech",
         "admin"
        ]
       }
      }
     },
     "test": {
      "transitions": {
       "start_learn": {
        "to": "learn",
        "guards": [
         "scientist",
         "admin"
        ]
       }
      }
     },
     "learn": {
      "transitions": {
       "close": {
        "to": "closed",
        "guards": [
         "scientist",
         "reviewer",
         "admin"
        ]
       }
      }
     },
     "closed": {}
    }
   },
   "permissions": {
    "list": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "get": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "create": [
     "scientist",
     "admin"
    ],
    "update": [
     "scientist",
     "lab_tech",
     "reviewer",
     "admin"
    ],
    "delete": [
     "admin"
    ]
   },
   "features": {
    "audit": true
   },
   "history": {
    "events": {
     "cycleBuild": {
      "trigger": "transition",
      "transition": "start_build",
      "capture": [
       "number"
      ],
      "label": "Cycle {number} moved to Build",
      "severity": "info"
     },
     "cycleTest": {
      "trigger": "transition",
      "transition": "start_test",
      "capture": [
       "number"
      ],
      "label": "Cycle {number} moved to Test",
      "severity": "info"
     },
     "cycleLearn": {
      "trigger": "transition",
      "transition": "start_learn",
      "capture": [
       "number"
      ],
      "label": "Cycle {number} moved to Learn",
      "severity": "info"
     },
     "cycleClosed": {
      "trigger": "transition",
      "transition": "close",
      "capture": [
       "number"
      ],
      "label": "Cycle {number} closed",
      "severity": "success"
     }
    }
   },
   "indexes": [
    {
     "fields": [
      "campaign"
     ]
    },
    {
     "fields": [
      "status"
     ]
    }
   ]
  },
  "design": {
   "description": "A candidate to build and test \u2014 a composition, formulation or process. Designs come from scientists or from models; a model-proposed design must name the model and its version, so a result can always be traced to what proposed it (AI provenance).\n",
   "object": {
    "id": {
     "type": "uuid",
     "primaryKey": true,
     "protected": true,
     "allowUserProvided": true
    },
    "cycle": {
     "type": "reference",
     "references": "adl.ledger.ledger.cycle",
     "required": true
    },
    "code": {
     "type": "string",
     "required": true,
     "unique": true,
     "maxLength": 40
    },
    "summary": {
     "type": "string",
     "required": true,
     "maxLength": 300
    },
    "origin": {
     "type": "string",
     "required": true,
     "enum": [
      "scientist",
      "model"
     ]
    },
    "modelName": {
     "type": "string",
     "maxLength": 100
    },
    "modelVersion": {
     "type": "string",
     "maxLength": 40
    },
    "rationale": {
     "type": "text"
    },
    "spec": {
     "type": "json"
    },
    "predictedScore": {
     "type": "decimal",
     "precision": 8,
     "scale": 3
    },
    "status": {
     "type": "string",
     "required": true,
     "default": "proposed"
    }
   },
   "validation": {
    "modelDesignsCarryProvenance": {
     "rule": "origin != 'model' || (modelName != null && modelVersion != null)",
     "message": "A model-proposed design must record the model name and version."
    },
    "specIsWrittenOnceApproved": {
     "rule": "isCreate() || status == 'proposed' || !fieldChanged('spec')",
     "message": "An approved design's spec is frozen. Propose a new design instead."
    }
   },
   "stateMachine": {
    "field": "status",
    "initial": "proposed",
    "states": {
     "proposed": {
      "transitions": {
       "approve": {
        "to": "approved",
        "guards": [
         "scientist",
         "reviewer",
         "admin"
        ]
       },
       "reject": {
        "to": "rejected",
        "guards": [
         "scientist",
         "reviewer",
         "admin"
        ]
       }
      }
     },
     "approved": {
      "transitions": {
       "mark_built": {
        "to": "built",
        "guards": [
         "lab_tech",
         "scientist",
         "admin"
        ]
       }
      }
     },
     "built": {},
     "rejected": {}
    }
   },
   "permissions": {
    "list": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "get": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "create": [
     "scientist",
     "ml_engineer",
     "admin"
    ],
    "update": [
     "scientist",
     "ml_engineer",
     "reviewer",
     "lab_tech",
     "admin"
    ],
    "delete": [
     "admin"
    ]
   },
   "features": {
    "audit": true
   },
   "indexes": [
    {
     "fields": [
      "cycle"
     ]
    },
    {
     "fields": [
      "origin"
     ]
    }
   ]
  },
  "protocol": {
   "description": "A versioned procedure. The version string is copied onto every run that uses it, so a run always says which version it followed even after the protocol is amended.\n",
   "object": {
    "id": {
     "type": "uuid",
     "primaryKey": true,
     "protected": true,
     "allowUserProvided": true
    },
    "code": {
     "type": "string",
     "required": true,
     "maxLength": 32
    },
    "title": {
     "type": "string",
     "required": true,
     "maxLength": 200
    },
    "version": {
     "type": "string",
     "required": true,
     "maxLength": 12
    },
    "steps": {
     "type": "json"
    },
    "status": {
     "type": "string",
     "required": true,
     "default": "draft"
    }
   },
   "validation": {
    "versionIsWrittenOnce": {
     "rule": "isCreate() || !fieldChanged('version')",
     "message": "A protocol version is permanent. Create the new version as a new record."
    }
   },
   "stateMachine": {
    "field": "status",
    "initial": "draft",
    "states": {
     "draft": {
      "transitions": {
       "release": {
        "to": "released",
        "guards": [
         "scientist",
         "admin"
        ]
       }
      }
     },
     "released": {
      "transitions": {
       "retire": {
        "to": "retired",
        "guards": [
         "scientist",
         "admin"
        ]
       }
      }
     },
     "retired": {}
    }
   },
   "permissions": {
    "list": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "get": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "create": [
     "scientist",
     "admin"
    ],
    "update": [
     "scientist",
     "admin"
    ],
    "delete": [
     "admin"
    ]
   },
   "features": {
    "audit": true
   },
   "indexes": [
    {
     "fields": [
      "code",
      "version"
     ],
     "unique": true
    }
   ]
  },
  "instrument": {
   "description": "An automation or analysis instrument. Calibration gates use.",
   "object": {
    "id": {
     "type": "uuid",
     "primaryKey": true,
     "protected": true,
     "allowUserProvided": true
    },
    "assetTag": {
     "type": "string",
     "required": true,
     "unique": true,
     "maxLength": 32
    },
    "name": {
     "type": "string",
     "required": true,
     "maxLength": 120
    },
    "kind": {
     "type": "string",
     "required": true,
     "enum": [
      "dispensing_robot",
      "furnace",
      "xrd",
      "impedance_analyzer",
      "tensile_tester",
      "spectrometer",
      "robot_arm",
      "other"
     ]
    },
    "location": {
     "type": "string",
     "maxLength": 120
    },
    "calibrationDueOn": {
     "type": "date",
     "required": true
    },
    "status": {
     "type": "string",
     "required": true,
     "default": "available"
    }
   },
   "stateMachine": {
    "field": "status",
    "initial": "available",
    "states": {
     "available": {
      "transitions": {
       "take_down": {
        "to": "maintenance",
        "guards": [
         "lab_tech",
         "admin"
        ]
       }
      }
     },
     "maintenance": {
      "transitions": {
       "return_to_service": {
        "to": "available",
        "guards": [
         "lab_tech",
         "admin"
        ]
       },
       "retire": {
        "to": "retired",
        "guards": [
         "admin"
        ]
       }
      }
     },
     "retired": {}
    }
   },
   "permissions": {
    "list": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "get": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "create": [
     "lab_tech",
     "admin"
    ],
    "update": [
     "lab_tech",
     "admin"
    ],
    "delete": [
     "admin"
    ]
   },
   "features": {
    "audit": true
   },
   "history": {
    "events": {
     "instrumentRetired": {
      "trigger": "transition",
      "transition": "retire",
      "capture": [
       "assetTag"
      ],
      "label": "Instrument {assetTag} retired",
      "severity": "warning"
     }
    }
   },
   "indexes": [
    {
     "fields": [
      "calibrationDueOn"
     ]
    }
   ]
  },
  "sample": {
   "description": "A physical sample or asset with lineage. `parent` records what it was derived from (aliquot, transformation, prep), so any measurement can be walked back to its source material.\n",
   "object": {
    "id": {
     "type": "uuid",
     "primaryKey": true,
     "protected": true,
     "allowUserProvided": true
    },
    "barcode": {
     "type": "string",
     "required": true,
     "unique": true,
     "maxLength": 40
    },
    "design": {
     "type": "reference",
     "references": "adl.ledger.ledger.design"
    },
    "parent": {
     "type": "reference",
     "references": "adl.ledger.ledger.sample"
    },
    "kind": {
     "type": "string",
     "required": true,
     "enum": [
      "precursor",
      "powder",
      "pellet",
      "film",
      "solution",
      "coating",
      "composite",
      "other"
     ]
    },
    "location": {
     "type": "string",
     "maxLength": 120
    },
    "quantity": {
     "type": "decimal",
     "precision": 12,
     "scale": 4,
     "min": 0
    },
    "unit": {
     "type": "string",
     "maxLength": 16
    },
    "status": {
     "type": "string",
     "required": true,
     "default": "registered"
    }
   },
   "stateMachine": {
    "field": "status",
    "initial": "registered",
    "states": {
     "registered": {
      "transitions": {
       "use": {
        "to": "in_use",
        "guards": [
         "lab_tech",
         "scientist",
         "admin"
        ]
       },
       "dispose": {
        "to": "disposed",
        "guards": [
         "lab_tech",
         "admin"
        ]
       }
      }
     },
     "in_use": {
      "transitions": {
       "consume": {
        "to": "consumed",
        "guards": [
         "lab_tech",
         "scientist",
         "admin"
        ]
       },
       "dispose": {
        "to": "disposed",
        "guards": [
         "lab_tech",
         "admin"
        ]
       }
      }
     },
     "consumed": {},
     "disposed": {}
    }
   },
   "permissions": {
    "list": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "get": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "create": [
     "lab_tech",
     "scientist",
     "admin"
    ],
    "update": [
     "lab_tech",
     "scientist",
     "admin"
    ],
    "delete": [
     "admin"
    ]
   },
   "features": {
    "audit": true
   },
   "indexes": [
    {
     "fields": [
      "design"
     ]
    },
    {
     "fields": [
      "parent"
     ]
    }
   ]
  },
  "run": {
   "description": "The pivot of the ledger: one execution, holding the PLAN (plannedParams, plannedStart, protocol version) beside the REALITY (actualParams, startedAt, endedAt). Anything that differs is recorded as a deviation, and a run cannot complete with a critical deviation still open.\n",
   "object": {
    "id": {
     "type": "uuid",
     "primaryKey": true,
     "protected": true,
     "allowUserProvided": true
    },
    "cycle": {
     "type": "reference",
     "references": "adl.ledger.ledger.cycle",
     "required": true
    },
    "design": {
     "type": "reference",
     "references": "adl.ledger.ledger.design",
     "required": true
    },
    "protocol": {
     "type": "reference",
     "references": "adl.ledger.ledger.protocol",
     "required": true
    },
    "protocolVersion": {
     "type": "string",
     "required": true,
     "maxLength": 12
    },
    "instrument": {
     "type": "reference",
     "references": "adl.ledger.ledger.instrument",
     "required": true
    },
    "operator": {
     "type": "string",
     "required": true,
     "maxLength": 100
    },
    "plannedParams": {
     "type": "json",
     "required": true
    },
    "actualParams": {
     "type": "json"
    },
    "plannedStart": {
     "type": "datetime",
     "required": true
    },
    "startedAt": {
     "type": "datetime"
    },
    "endedAt": {
     "type": "datetime"
    },
    "failureReason": {
     "type": "text"
    },
    "status": {
     "type": "string",
     "required": true,
     "default": "planned"
    }
   },
   "validation": {
    "planIsFrozenOnceStarted": {
     "rule": "isCreate() || status == 'planned' || !fieldChanged('plannedParams')",
     "message": "The plan is the record of intent. Once a run starts, record differences as deviations \u2014 don't edit the plan."
    },
    "endsAfterStart": {
     "rule": "endedAt == null || startedAt == null || endedAt >= startedAt",
     "message": "A run cannot end before it started."
    }
   },
   "stateMachine": {
    "field": "status",
    "initial": "planned",
    "states": {
     "planned": {
      "transitions": {
       "start": {
        "to": "running",
        "guards": [
         "lab_tech",
         "scientist",
         "admin"
        ],
        "sets": {
         "startedAt": "now()"
        }
       },
       "cancel": {
        "to": "cancelled",
        "guards": [
         "scientist",
         "admin"
        ]
       }
      }
     },
     "running": {
      "transitions": {
       "complete": {
        "to": "completed",
        "guards": [
         "lab_tech",
         "scientist",
         "admin"
        ],
        "sets": {
         "endedAt": "now()"
        }
       },
       "fail": {
        "to": "failed",
        "guards": [
         "lab_tech",
         "scientist",
         "admin"
        ],
        "requiredFields": [
         "failureReason"
        ],
        "sets": {
         "endedAt": "now()"
        }
       }
      }
     },
     "completed": {},
     "failed": {},
     "cancelled": {}
    }
   },
   "permissions": {
    "list": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "get": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "create": [
     "scientist",
     "lab_tech",
     "admin"
    ],
    "update": [
     "scientist",
     "lab_tech",
     "admin"
    ],
    "delete": [
     "admin"
    ]
   },
   "features": {
    "audit": true
   },
   "history": {
    "events": {
     "runStarted": {
      "trigger": "transition",
      "transition": "start",
      "label": "Run started",
      "severity": "info"
     },
     "runCancelled": {
      "trigger": "transition",
      "transition": "cancel",
      "label": "Run cancelled before it started",
      "severity": "warning"
     },
     "runCompleted": {
      "trigger": "transition",
      "transition": "complete",
      "label": "Run completed",
      "severity": "success"
     },
     "runFailed": {
      "trigger": "transition",
      "transition": "fail",
      "capture": [
       "failureReason"
      ],
      "label": "Run failed: {failureReason}",
      "severity": "error"
     }
    }
   },
   "indexes": [
    {
     "fields": [
      "cycle"
     ]
    },
    {
     "fields": [
      "design"
     ]
    },
    {
     "fields": [
      "instrument"
     ]
    },
    {
     "fields": [
      "status"
     ]
    }
   ]
  },
  "event": {
   "description": "Something that happened during operations: an error, a manual intervention, an environmental excursion.",
   "object": {
    "id": {
     "type": "uuid",
     "primaryKey": true,
     "protected": true,
     "allowUserProvided": true
    },
    "run": {
     "type": "reference",
     "references": "adl.ledger.ledger.run"
    },
    "instrument": {
     "type": "reference",
     "references": "adl.ledger.ledger.instrument"
    },
    "kind": {
     "type": "string",
     "required": true,
     "enum": [
      "instrument_error",
      "manual_intervention",
      "environment",
      "sample_issue",
      "note"
     ]
    },
    "severity": {
     "type": "string",
     "required": true,
     "enum": [
      "info",
      "warning",
      "critical"
     ],
     "default": "info"
    },
    "occurredAt": {
     "type": "datetime",
     "required": true
    },
    "detail": {
     "type": "text",
     "required": true
    },
    "reportedBy": {
     "type": "string",
     "required": true,
     "maxLength": 100
    }
   },
   "validation": {
    "eventHasAContext": {
     "rule": "run != null || instrument != null",
     "message": "An event must name the run or the instrument it happened on."
    }
   },
   "permissions": {
    "list": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "get": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "create": [
     "scientist",
     "lab_tech",
     "admin"
    ],
    "update": [
     "admin"
    ],
    "delete": [
     "admin"
    ]
   },
   "features": {
    "audit": true
   },
   "indexes": [
    {
     "fields": [
      "run"
     ]
    },
    {
     "fields": [
      "occurredAt"
     ]
    }
   ]
  },
  "deviation": {
   "description": "A difference between plan and reality, written down: which parameter, what was planned, what actually happened, and whether it matters. Deviations are the ledger's answer to \"what actually happened?\".\n",
   "object": {
    "id": {
     "type": "uuid",
     "primaryKey": true,
     "protected": true,
     "allowUserProvided": true
    },
    "run": {
     "type": "reference",
     "references": "adl.ledger.ledger.run",
     "required": true
    },
    "parameter": {
     "type": "string",
     "required": true,
     "maxLength": 80
    },
    "planned": {
     "type": "string",
     "required": true,
     "maxLength": 200
    },
    "actual": {
     "type": "string",
     "required": true,
     "maxLength": 200
    },
    "critical": {
     "type": "boolean",
     "required": true,
     "default": false
    },
    "explanation": {
     "type": "text"
    },
    "reviewedBy": {
     "type": "string",
     "maxLength": 100
    },
    "status": {
     "type": "string",
     "required": true,
     "default": "open"
    }
   },
   "stateMachine": {
    "field": "status",
    "initial": "open",
    "states": {
     "open": {
      "transitions": {
       "review": {
        "to": "reviewed",
        "guards": [
         "reviewer",
         "scientist",
         "admin"
        ],
        "sets": {
         "reviewedBy": "$user.id"
        }
       }
      }
     },
     "reviewed": {
      "transitions": {
       "close": {
        "to": "closed",
        "guards": [
         "reviewer",
         "admin"
        ]
       }
      }
     },
     "closed": {}
    }
   },
   "permissions": {
    "list": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "get": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "create": [
     "scientist",
     "lab_tech",
     "admin"
    ],
    "update": [
     "scientist",
     "lab_tech",
     "reviewer",
     "admin"
    ],
    "delete": [
     "admin"
    ]
   },
   "features": {
    "audit": true
   },
   "indexes": [
    {
     "fields": [
      "run"
     ]
    },
    {
     "fields": [
      "status"
     ]
    }
   ]
  },
  "result": {
   "description": "A measurement, with provenance: the run that produced it, the sample it was taken from, where the raw data lives, and a checksum of that data. `cycle` is a copy on the flat row so the learn step can count results per cycle without traversal.\n",
   "object": {
    "id": {
     "type": "uuid",
     "primaryKey": true,
     "protected": true,
     "allowUserProvided": true
    },
    "run": {
     "type": "reference",
     "references": "adl.ledger.ledger.run",
     "required": true
    },
    "cycle": {
     "type": "reference",
     "references": "adl.ledger.ledger.cycle",
     "required": true
    },
    "sample": {
     "type": "reference",
     "references": "adl.ledger.ledger.sample"
    },
    "measurement": {
     "type": "string",
     "required": true,
     "maxLength": 80
    },
    "value": {
     "type": "decimal",
     "required": true,
     "precision": 18,
     "scale": 6
    },
    "unit": {
     "type": "string",
     "required": true,
     "maxLength": 24
    },
    "qc": {
     "type": "string",
     "required": true,
     "enum": [
      "pass",
      "flag",
      "fail"
     ],
     "default": "pass"
    },
    "dataUri": {
     "type": "url"
    },
    "checksum": {
     "type": "string",
     "maxLength": 64,
     "pattern": "^[a-f0-9]{64}$"
    },
    "producedBy": {
     "type": "string",
     "required": true,
     "maxLength": 100
    },
    "status": {
     "type": "string",
     "required": true,
     "default": "captured"
    }
   },
   "validation": {
    "rawDataIsChecksummed": {
     "rule": "dataUri == null || checksum != null",
     "message": "Raw data must carry its SHA-256 checksum so it can be verified later."
    },
    "valueIsWrittenOnce": {
     "rule": "isCreate() || !fieldChanged('value')",
     "message": "A captured value is never edited. Reject it and capture a new one."
    }
   },
   "stateMachine": {
    "field": "status",
    "initial": "captured",
    "states": {
     "captured": {
      "transitions": {
       "verify": {
        "to": "verified",
        "guards": [
         "scientist",
         "reviewer",
         "admin"
        ]
       },
       "reject": {
        "to": "rejected",
        "guards": [
         "scientist",
         "reviewer",
         "admin"
        ]
       }
      }
     },
     "verified": {},
     "rejected": {}
    }
   },
   "permissions": {
    "list": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "get": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "create": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "admin"
    ],
    "update": [
     "scientist",
     "reviewer",
     "admin"
    ],
    "delete": [
     "admin"
    ]
   },
   "features": {
    "audit": true
   },
   "indexes": [
    {
     "fields": [
      "run"
     ]
    },
    {
     "fields": [
      "cycle"
     ]
    },
    {
     "fields": [
      "sample"
     ]
    }
   ]
  },
  "learning": {
   "description": "What the cycle taught \u2014 the L in DBTL \u2014 and what to design next.",
   "object": {
    "id": {
     "type": "uuid",
     "primaryKey": true,
     "protected": true,
     "allowUserProvided": true
    },
    "cycle": {
     "type": "reference",
     "references": "adl.ledger.ledger.cycle",
     "required": true
    },
    "conclusion": {
     "type": "text",
     "required": true
    },
    "confidence": {
     "type": "percentage"
    },
    "nextStep": {
     "type": "text"
    },
    "author": {
     "type": "string",
     "required": true,
     "maxLength": 100
    },
    "status": {
     "type": "string",
     "required": true,
     "default": "draft"
    }
   },
   "stateMachine": {
    "field": "status",
    "initial": "draft",
    "states": {
     "draft": {
      "transitions": {
       "publish": {
        "to": "published",
        "guards": [
         "scientist",
         "reviewer",
         "admin"
        ]
       }
      }
     },
     "published": {}
    }
   },
   "permissions": {
    "list": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "get": [
     "scientist",
     "lab_tech",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "create": [
     "scientist",
     "ml_engineer",
     "admin"
    ],
    "update": [
     "scientist",
     "ml_engineer",
     "reviewer",
     "admin"
    ],
    "delete": [
     "admin"
    ]
   },
   "features": {
    "audit": true
   },
   "indexes": [
    {
     "fields": [
      "cycle"
     ]
    }
   ]
  }
 },
 "rules": {
  "noRunOnAnOutOfCalibrationInstrument": {
   "description": "A run may not start on an instrument whose calibration has lapsed.",
   "trigger": {
    "resource": "run",
    "events": [
     "transition"
    ],
    "transition": "start"
   },
   "condition": "some(query('instrument', { id: $record.instrument }), i => i.calibrationDueOn < today() || i.status != 'available')",
   "action": "reject",
   "severity": "error",
   "message": "The instrument is out of calibration or not available. Calibrate or choose another instrument."
  },
  "criticalDeviationsBlockCompletion": {
   "description": "A run with an open critical deviation has not finished being explained. It can still FAIL (that is a true account), but it cannot be marked complete.\n",
   "trigger": {
    "resource": "run",
    "events": [
     "transition"
    ],
    "transition": "complete"
   },
   "condition": "len(query('deviation', { run: $record.id, status: 'open', critical: true })) > 0",
   "action": "reject",
   "severity": "error",
   "message": "Review the open critical deviations on this run before completing it."
  },
  "resultsComeFromRunsThatRan": {
   "description": "A result must come from a run that actually started.",
   "trigger": {
    "resource": "result",
    "events": [
     "create"
    ]
   },
   "condition": "some(query('run', { id: $record.run }), r => r.status == 'planned' || r.status == 'cancelled')",
   "action": "reject",
   "severity": "error",
   "message": "This run never started. Results can only be captured for runs that ran."
  },
  "learnNeedsResults": {
   "description": "The loop cannot move to Learn with nothing measured.",
   "trigger": {
    "resource": "cycle",
    "events": [
     "transition"
    ],
    "transition": "start_learn"
   },
   "condition": "len(query('result', { cycle: $record.id })) == 0",
   "action": "reject",
   "severity": "error",
   "message": "No results have been captured for this cycle yet."
  },
  "closeNeedsALearning": {
   "description": "A cycle closes only when what it taught has been written down.",
   "trigger": {
    "resource": "cycle",
    "events": [
     "transition"
    ],
    "transition": "close"
   },
   "condition": "len(query('learning', { cycle: $record.id, status: 'published' })) == 0",
   "action": "reject",
   "severity": "error",
   "message": "Publish a learning for this cycle before closing it."
  },
  "deviationClosesWithAnExplanation": {
   "description": "Closing a deviation without saying why it happened loses the record.",
   "trigger": {
    "resource": "deviation",
    "events": [
     "transition"
    ],
    "transition": "close"
   },
   "condition": "$record.explanation == null",
   "action": "reject",
   "severity": "error",
   "message": "Explain the deviation before closing it."
  },
  "criticalEventOnARun": {
   "description": "A critical event during a run is flagged so a deviation gets written.",
   "trigger": {
    "resource": "event",
    "events": [
     "create"
    ]
   },
   "condition": "$record.severity == 'critical' && $record.run != null",
   "action": "warn",
   "severity": "warning",
   "message": "Critical event on a run \u2014 record a deviation if the plan was affected."
  }
 },
 "workflows": {
  "cancelPlannedRunsOfRetiredInstrument": {
   "description": "Retiring an instrument cancels every run still planned on it: those runs can never start. Runs on it that are running or finished, and every run on another instrument, are left alone.\n",
   "trigger": {
    "resource": "instrument",
    "event": "transition",
    "transition": "instrumentRetired"
   },
   "steps": [
    {
     "name": "cancelPlannedRuns",
     "forEach": {
      "collection": "query('run', { instrument: 'adl.ledger.ledger.instrument:' + $trigger.record.id, status: 'planned' })",
      "step": {
       "name": "cancelPlannedRun",
       "action": "transition",
       "resource": "run",
       "transition": "cancel",
       "target": "$item.id"
      }
     }
    }
   ]
  }
 },
 "schedules": {
  "takeLapsedInstrumentsOffline": {
   "description": "Every morning, instruments past their calibration date go to maintenance.",
   "recurrence": "daily at 06:00",
   "source": {
    "resource": "instrument",
    "filter": "status == 'available' && calibrationDueOn < today()"
   },
   "action": "transition",
   "transition": "take_down"
  }
 }
}
