Write an instrument
When the lab lacks a tool, your AI can write one, and so can you. An instrument is a folder with instrument.json and a program, in any language. The kernel starts the program in a fresh folder and reads back what it wrote.
The contract
- In
request.jsonholds{"verb": "run", "inputs": {...}}. File inputs are placed underinputs/.- Out
- Write
result.jsonas{"values": {...}}, or{"error": "...", "hint": "..."}in words a person can act on. Writenull, never NaN or infinity. - Files
- Anything under
out/is stored by hash. A 3D result isout/scene.glb.
The manifest
name, version, summary, command (where {dir} is the instrument's folder),
timeout_s, inputs as a JSON schema with units, outputs, selftest and
replay_rel_tol. Optionally hash_files for a compiled program, and a present block for people.
Known answers, not smoke tests
Each selftest case gives inputs and the readings expected: a value with rel_tol or abs_tol, a
min and max, or equals. The answers must come from outside your own work: closed forms,
exact solutions, published values. A case that only checks the program exits is rejected. Say where each answer comes
from and why the tolerance is what it is.
The smallest example: a pendulum
{
"name": "pendulum",
"version": "0.1.0",
"summary": "Simple pendulum at any amplitude: period by RK4 integration of theta'' = -(g/L) sin(theta). Standard library only. Readings: period, the small-angle period 2 pi sqrt(L/g), and their ratio.",
"command": ["python3", "{dir}/run.py"],
"timeout_s": 30,
"estimate_s": 1,
"inputs": {
"type": "object",
"properties": {
"length": {"type": "number", "minimum": 1e-3, "unit": "m", "default": 1.0},
"g": {"type": "number", "minimum": 0.01, "unit": "m/s^2", "default": 9.81},
"amplitude": {"type": "number", "minimum": 0.01, "maximum": 179, "unit": "deg", "default": 10}
}
},
"outputs": {"period": {"unit": "s"}, "period_small_angle": {"unit": "s"}, "ratio": {"unit": "1"}},
"selftest": [
{"name": "1 degree: matches 2 pi sqrt(L/g)", "inputs": {"amplitude": 1}, "expect": {"ratio": {"value": 1.0000190, "rel_tol": 1e-5}}},
{"name": "90 degrees: T/T0 = 1.18034 (elliptic integral)", "inputs": {"amplitude": 90}, "expect": {"ratio": {"value": 1.180341, "rel_tol": 1e-5}}}
]
}"""Invntr instrument: pendulum period at any amplitude (standard library only)."""
import json
import math
req = json.load(open("request.json"))["inputs"]
L, g, a = req["length"], req["g"], math.radians(req["amplitude"])
k = g / L
T0 = 2 * math.pi / math.sqrt(k)
dt = T0 / 20000
def acc(th):
return -k * math.sin(th)
# Release at rest from +a; the bob first passes theta = 0 at a quarter period.
th, om, t = a, 0.0, 0.0
while True:
k1t, k1o = om, acc(th)
k2t, k2o = om + dt / 2 * k1o, acc(th + dt / 2 * k1t)
k3t, k3o = om + dt / 2 * k2o, acc(th + dt / 2 * k2t)
k4t, k4o = om + dt * k3o, acc(th + dt * k3t)
nth = th + dt / 6 * (k1t + 2 * k2t + 2 * k3t + k4t)
nom = om + dt / 6 * (k1o + 2 * k2o + 2 * k3o + k4o)
if nth <= 0:
quarter = t + dt * th / (th - nth)
break
th, om, t = nth, nom, t + dt
T = 4 * quarter
json.dump({"values": {"period": T, "period_small_angle": T0, "ratio": T / T0}}, open("result.json", "w"))Copy the folder into your instruments folder and run invntr admit pendulum. To share it, publish it to the
registry.