# Measurements, shared like benchmark results

Many people measure many units of one motor model, and robotparts turns their records into a statistical
picture of the true datasheet, the way GPU benchmark sites do for graphics cards (Zach, 2026-10-22). The record
format is SCHEMA2.md §6, and it is unchanged. This page covers what sits around it:

1. **The submission bundle**: what a contributor sends for one run on one unit, and what gets refused.
2. **`robotparts measure`**: `submit` writes a bundle as a **new** file in `parts/<id>/measured/`, and `stats` prints the picture.
3. **The statistics**: per part and key, with scope `part` and `as_mounted` kept apart.
4. **How friends and family upload**: the `/measure/` page on robotparts.dev, backed by Simple Hosted Files. Uploads
   are off until robotparts is registered with it; today the page checks a bundle and downloads it to send.

| file | what |
|---|---|
| `schemas/measurement-bundle.schema.json` | the bundle's shape |
| `python/robotparts/measure.py` | the rules (`bundle_problems`), bundle → §6 file (`measured_document`, `measured_file_name`), the statistics (`stats_of_record`, `stats`), and `python -m robotparts.measure` |
| `tools/measure.py` | the repository's `robotparts measure` command: `submit`, `check`, `stats`, `new` |
| `site/measure/` | the upload page (`index.html`, `app.js`) and the same rules in the browser (`rules.js`) |
| `tests/measure/` | the lane's tests (synthetic parts only, built in temporary directories) |

## 1. The submission bundle

One contributor, one run, one physical unit (the *specimen*). For example:

```json
{
  "format": "robotparts-measurement-bundle/1",
  "part": "aifitlab-8541438771336",
  "contributor": "Ada Example",
  "specimen": {"label": "unit A", "serial": "GIM-SN-0001", "board_id": null,
               "purchased_on": "2026-09-01", "purchased_from": "aifitlab.com", "shared_note": null},
  "rig": "Ada's bench: encoder on the rotor, camera on the output",
  "method": "Counted rotor turns per output turn over 20 output turns, both read by encoders.",
  "date": "2026-10-20",
  "versions": {"firmware": "moteus 2024-06-12"},
  "evidence": {"url": "https://github.com/ada/rig-data/blob/<40-hex commit>/runs/2026-10-20.csv#L1-L40", "sha256": null},
  "raw_data": [{"url": "https://example.org/ada/run.mp4", "sha256": "<64 hex>", "title": "video"}],
  "records": [
    {"key": "gear_ratio", "value": 10.02, "unit": ":1", "ci95": [9.98, 10.06], "scope": "part", "mounted_in": null,
     "raw": "200.4 rotor turns for 20.00 output turns"}
  ]
}
```

| field | rule |
|---|---|
| `part` | a robotparts id: an atlas id (current or retired), or a part folder. A `same_as` duplicate is refused, and the message names the part it duplicates |
| `contributor` | who measured it. The site credits them |
| `specimen.label` | your name for this unit, used verbatim as every record's `specimen`. Keep it the same every time you measure that unit |
| `specimen.serial`, `board_id` | the maker's serial, or the id of a board fixed to the unit (its driver's). Optional, but they are what tells units apart across contributors |
| `specimen.purchased_on`, `purchased_from` | optional: when and where the unit was bought (batch effects) |
| `specimen.mask` | optional boolean, default `false`. `true` publishes a masked token in place of `serial` and `board_id` (see **Masking a unit's ids** below) |
| `specimen.shared_note` | required when someone else already measured this very unit (it was lent or sold): say how it is the same one |
| `rig`, `method`, `date` | on what, how, and when it was measured. A record may give its own `method` and `date` |
| `versions` | firmware, driver and analysis versions, by name. Each becomes a condition of every record (`conditions.firmware`) |
| `evidence` | `{url, sha256}`: the run's raw data, used by every record that names no `evidence_url` of its own |
| `raw_data` | more links (logs, video), listed in each record's note |
| `license` | optional: the data licence of the new file, from `registry.redistributable_licenses` |
| `records[]` | §6 values: `key`, `value`, `unit`, `ci95` (or null), `scope`, `mounted_in` (`as_mounted` only), `raw` (the evidence's own words), optional `conditions`, `note`, `evidence_url` and `evidence_sha256`, and `supersedes` plus `supersede_reason` for a correction |
| `conditions.bench` | optional, in a record's `conditions` (or in `versions`, for every record): the standard protocol the run followed, a non-empty string such as `"robotparts-bench/1"`. The statistics pool only records of the same bench (§3). robotparts does not care how the data was produced; it keeps what you state |

**Evidence must be pinned** (§6): a GitHub blob link at a full 40-hex commit with `#L` lines, a
`raw.githubusercontent.com` link at a commit, a DOI (Zenodo gives one for free), a Software Heritage id, or any
https link plus the sha256 of the bytes you read (`sha256sum run.csv`). A Simple Hosted Files read link expires,
so it is never evidence.

**Refused**, with a stable code. Each code is checked the same way in Python and in the browser
(`tests/measure/test_page_rules.py`):

| code | what is wrong |
|---|---|
| `scope_missing` | a record has no `scope`: `part` (the part alone on a rig) or `as_mounted` (with whatever it drives). §6 has no unknown scope |
| `evidence_missing`, `evidence_not_pinned` | no evidence on the record or the bundle; or a link that is not pinned |
| `unknown_part`, `duplicate_part` | the id is not a robotparts part; or it is a `same_as` duplicate |
| `unit_missing`, `unit_not_allowed` | a value without a unit (`""` only for a dimensionless key such as `gear_ratio`); or a unit the key does not allow |
| `specimen_claimed` | another contributor already measured this unit (same serial or board id, or that id named in their label, e.g. the rack's `CAN 14, moteus-c1 c3414184`) and `specimen.shared_note` is empty |
| `bench_invalid` | `conditions.bench` is not a non-empty string |
| `key_unknown`, `key_not_measurable`, `value_not_number`, `value_not_positive`, `scope_not_allowed`, `mounted_in_missing`, `mounted_in_unexpected`, `ci_invalid`, `condition_missing`, `conditions_reserved`, `conditions_conflict`, `raw_missing`, `method_missing`, `date_missing`, `date_invalid`, `supersedes_unknown`, `supersedes_mismatch`, `supersedes_twice`, `supersede_reason_missing`, `unknown_field`, … | the other §6 rules (registry units and scopes, the interval holds the value, required conditions, corrections of the same key, unit and contributor), plus typos |

**What a record becomes.** Every field of the written record comes from the bundle, so nothing is invented:

| §6 record | from the bundle |
|---|---|
| `key`, `value`, `unit`, `ci95`, `scope`, `mounted_in`, `raw` | the record |
| `method`, `date`, `evidence_url`, `evidence_sha256` | the record's own, else the bundle's |
| `rig`, `contributor` | the bundle |
| `specimen` | `specimen.label`, verbatim |
| `conditions` | the record's, then `versions`, then `specimen_serial`, `specimen_board_id` (masked tokens when `specimen.mask` is true), `specimen_purchased_on`, `specimen_purchased_from` |
| `note` | the record's note, then `Specimen: <shared_note>`, then `Raw data: …` |
| `supersedes`, `supersede_reason` | the record's, for a correction |

**Masking a unit's ids.** A contributor who would rather not publish a unit's serial or board id sets
`"mask": true` in the specimen. `tools/measure.py submit` then writes `masked:` plus the first 16 hex digits of
`HMAC-SHA256(salt, id)` as `conditions.specimen_serial` and `conditions.specimen_board_id`; the raw ids appear
nowhere in the published file. The id is lower-cased and its spacing collapsed first, as when two records are matched
to one unit, so `SN-1` and `sn-1` give one token.

- **The salt is yours.** It is a file, `~/.config/robotparts/specimen-salt` (in the directory of `$ROBOTPARTS_CONFIG`
  when that is set, as the live config is found), 32 random bytes at mode 0600. `submit` creates it the first time a
  masked bundle needs it, after every check has passed; `check` and `submit --dry-run` never write it. It is never
  published or printed, and an existing file is never replaced (a new salt would orphan every token already published).
  Back it up with your own files: lose it and later runs make new tokens for the same units.
- **What pools.** The token is the same for one salt, so your own runs of a unit stay one unit however you label them.
  It differs under another salt, so it never links to another contributor's record of the same unit, and the
  `specimen_claimed` check cannot see a unit someone else masked. That is the cost of masking.
- **The label is published as written** (every record's `specimen`). `check` warns (`mask_label_leaks_id`) when a
  masked bundle's label contains its own serial or board id, since masking the ids would then hide nothing.
- **The page cannot mask.** The salt cannot live in a browser, so `/measure/` keeps `serial` and `board_id` in the
  bundle you download or send, and says that masking happens at `tools/measure.py submit`. Whoever runs `submit` on a
  bundle sees the ids, and the tokens are made with their salt.

## 2. `robotparts measure`

```bash
python3 tools/measure.py submit bundle.json
```

`submit` takes a file, `-` for stdin, or a link: an https URL, or a contributor's Simple Hosted Files read link
(`…/a/<token>`, fetched once from its `/content`). The token is never printed. Nothing is written until every
step passes:

1. It checks the bundle against this working tree. The build runs in process, so the part's record, its
   `same_as` and its measurements are the ones `build/build.py` would publish.
2. It turns the bundle into a §6 file named `YYYY-MM-DD-<contributor>-<rig>.json`: the run's date, three words of
   the contributor and five of the rig. A second run on the same day becomes `…rack2.json`, which sorts after
   `…rack.json`. A correction always sorts after the file it corrects.
3. It checks the folder, new file included, with the build's own folder rules (the ones the deploy runs).
4. It creates the file exclusively (`open(…, "x")`), so an existing file is never opened for writing. Corrections
   are new records with `supersedes` (§6).

Then it prints the statistics of the keys it touched. The other commands:

| command | does |
|---|---|
| `tools/measure.py check <bundle or link>` | what `submit` would refuse, or the file it would write. Writes nothing |
| `tools/measure.py submit --dry-run <bundle>` | the same, plus the file's content |
| `tools/measure.py stats <id> [key] [--scope part\|as_mounted] [--json]` | the statistics of the working tree |
| `tools/measure.py new <id>` | a bundle to fill in: every required field is null until you write it |
| `python -m robotparts.measure stats\|check\|new …` | the same from the installed package, against the release it ships |

Publishing is then the usual pull request (§6): commit the new file, run `tests/acceptance_v2`, and the next
release puts it on the site.

## 3. The statistics

```python
from robotparts import measure
for s in measure.stats("aifitlab-8541438771336"):
    print(s.key, s.scope, s.n_specimens, s.median, s.interval, s.share_consistent)
```

`stats(part, key=None, scope=None)` takes an id, a `RobotPart` or a robotparts/2 record, and reads only the
published record (`measured` and `comparisons`). There is no contract change: nothing new is published under `/v2/`.

- **Groups.** One group per key and scope, `part` and `as_mounted` kept apart. A key with a required condition is
  split by its value: `torque_ripple` per `rotor_harmonic`, `twin_tracking_error` per `sim_model_sha256`.
- **Bench.** A record whose `conditions.bench` names a standard protocol (`robotparts-bench/1`) pools only with records
  of the same bench; records with no bench form a group of their own. A unit measured on two benches counts once in each.
  The bench is `KeyStats.bench` (`None` without one), is printed by `describe()` (`gear_ratio · part · bench robotparts-bench/1`)
  and appears in the Conditions column and the unit list of the part page's Measured statistics. robotparts does not
  define or check a bench's protocol: it is a name you state, and a name that differs by one character is another bench.
- **A unit counts once.** Records of one physical unit are linked when they share a label from the same
  contributor, a serial, or a board id. An id another record names inside its label counts too, and must be at
  least 4 characters. Each unit's value is the median of its active records. Superseded records are left out and
  counted (`n_superseded`).
- **Across units.** `n_specimens`, `n_contributors`, `median`, `mean`, `std` (n−1, from 2 units), `mad`, `min` and
  `max`. Values are in the key's SI unit, never rounded. `unit` and `factor` give the records' own unit when they
  all used one.
- **The interval for the median** is distribution-free, from order statistics: [x(k), x(n+1−k)], whose coverage
  is 1 − 2·P(B ≤ k−1) with B ~ Binomial(n, ½). The narrowest one at ≥ 95% is used: n = 6 to 8 gives [min, max]
  (96.9 %, 98.4 %, 99.2 %), n = 9 gives [x(2), x(8)] at 96.1 %, n = 20 gives [x(6), x(15)] at 95.9 %. With n ≤ 5
  no pair reaches 95%, so the interval is [min, max] at its own coverage (93.75 % at 5, 50 % at 2), and n = 1 has
  none. *Why not a bootstrap:* resampling a handful of values under-covers, and the same records must always give
  the same interval.
- **The datasheet.** Its value is the one §6 `comparisons` uses (`specs[canonical[key]]`, in SI). A unit is
  *consistent* when every interval it has holds that value, *outside* when none does, and *mixed* otherwise.
  `share_consistent` = consistent ÷ (consistent + outside + mixed). Units without an interval are counted as
  `no_ci`, never guessed. `as_mounted` values of a key the load can change are shown and never judged, as in §6.
  A mount-invariant key such as `gear_ratio` is judged in both scopes. The model-level check is
  `datasheet_in_interval`: does the median's interval hold the datasheet value?

Nine synthetic units of one model, from three contributors (a test fixture, never in `parts/`):

```text
gear_ratio · part
  9 specimens, 3 contributors (Ada, Bea, Cy), 9 records
  median 10 :1   mean 10.0089   std 0.12985   min 9.8   max 10.3
  interval for the median: [9.95, 10.04] :1 (coverage 96.09%, order statistics)
  datasheet 10.0 :1 (manufacturer, https://maker.example/gim)
  median vs datasheet: +0.00%; datasheet inside the median's interval
  consistent with the datasheet: 5 of 9 judged specimens (56%); outside 4, mixed 0, no interval 0
```

The real GIM4305-10 (`python3 tools/measure.py stats aifitlab-8541438771336`) has one unit per key, and says so:

```text
gear_ratio · as_mounted
  1 specimen, 1 contributor (BAM Robotics), 1 record
  median 9.615 :1   mean 9.615   std —   min 9.615   max 9.615
  interval for the median: — (one specimen)
  datasheet 10.0 :1 (aifitlab-text, https://aifitlab.com/products/steadywin-gim4305-planetary-reducer-servo-motor)
  median vs datasheet: -3.85%
  consistent with the datasheet: 0 of 1 judged specimens (0%); outside 1, mixed 0, no interval 0
  · CAN 1, moteus-c1 32bb145a, board unconfigured so its encoder reads the rotor [BAM Robotics]: 9.615 :1 (outside)
  note: one specimen: no spread across units and no interval; one unit says little about the model
```

Its friction and ripple records are `as_mounted`, so they are shown and never judged. The ripple is split into
three groups, one per harmonic (k = 14, 24, 28).

## 4. How friends and family upload

**Status (integration round 3, 2026-10-22): uploads are off.** The published `/measure/` page checks and downloads
only, and loads nothing from another site: `UPLOADS` in `site/measure/app.js` is `false` until steps 1 and 2 of
"What needs Zach" below are done. Until then a contributor downloads the bundle and sends the file (A below), and
whoever holds the repository runs `tools/measure.py submit <file>`; robotparts.dev's Submit measurements page says so.

**The server switch.** `?server=<url>` still turns sending on, which is how the journey test runs, but only for a
server on the visitor's own computer (`127.0.0.1`, `localhost` or `[::1]`) or the default server, and only that origin:
any other value is ignored, with a console warning. WHY (review V1): the page imports the accounts SDK from that server
and runs it, so a link naming another site would run that site's code on robotparts.dev. As a second fence, robotparts.dev
serves `/measure/` with a `script-src` policy (`site/_headers`) naming the same servers, so the browser refuses a
script from anywhere else whatever `app.js` does. A policy cannot name an IPv6 address, so on robotparts.dev a `[::1]`
server is refused; use `localhost` or `127.0.0.1` there. A new default server changes `DEFAULT_SERVER` in `app.js` and
that policy together (`tests/measure/test_site_page.py` pins both and drives the page on hostile values).

**Criteria** (weight): works for someone with neither git nor Python (3); no hand-rolled accounts or storage
(must); reaches robotparts with who sent it (3); checked before it is sent (2); Zach's setup (2); works today (1).

| option | no git/Python | stock accounts and storage | who sent it | checked first | Zach's setup | today |
|---|---|---|---|---|---|---|
| A. send the file (chat, email) | partly: a JSON file to edit | yes (none needed) | the sender, by chat | only with Python | none | yes |
| B. pull request on a public git remote | no | yes (GitHub) | GitHub account | in CI | a public remote plus CI | no: robotparts has no remote |
| **C. `/measure/` page + Simple Hosted Files** | **yes** | **yes** | **their sign-in** | **as they type** | **register, deploy SHF** | **local, proved** |

**Chosen: C, with A built into the same page.** The page checks a bundle as it is typed, using
`site/measure/rules.js`, the registry, and the part's `/v2/parts/<id>.json`. It downloads the file at any time.
Signed in, it keeps the bundle in the contributor's account (autosave, versions, trash) and makes a **7-day
read link** (`createAgentLink`, mode `read`) for them to send to robotparts. Then:

```bash
python3 tools/measure.py submit https://simple-hosted-files.zach-yamaoka.workers.dev/a/<token>
```

robotparts never stores anything for the page: the bundle lives in the contributor's account until they trash it.
Simple Hosted Files has no shared inbox (its v1 lists sharing as out of scope), which is why the read link travels
by chat. B becomes worth it if robotparts gets a public remote: the same bundle, in a pull request.

Proved end to end against a local Simple Hosted Files server (`tests/measure/test_upload_journey.py`): headless
Chromium opens the page, sees a template refused, a missing scope refused and a claimed unit refused, then signs in
through the stock card, saves, and gets the link. `submit <link>` writes the file into a throwaway tree, where the
frozen reference accepts the folder.

**What needs Zach**, in this order (each touches his accounts):

1. Register the app, in a Simple Hosted Files checkout: `node scripts/register-app.mjs robotparts "robotparts" https://robotparts.dev https://www.robotparts.dev --extension .measurement.json`.
   It edits `server/apps.json` in that repo only.
2. Deploy Simple Hosted Files, from its checkout: `node scripts/deploy-live.mjs`. Until then, the live
   server refuses robotparts.dev (CORS, sign-in), and the page can only check and download.
3. Google sign-in is the only production sign-in (email codes need a sender). If the Google Auth Platform app is
   still in *Testing*, only listed test users can sign in: add the friends' Google accounts, or publish the app.
4. The free plan is 3 files per person per app, which is enough when a bundle is trashed once it is submitted. A
   bigger number (`"free": {"filesPerApp": N}` in `apps.json`) is a pricing decision.
5. Cut and deploy a robotparts release, which publishes `/measure/` (`build/build.py` copies `site/measure/` and
   `schemas/registry.json`).
6. Turn uploads on: `const UPLOADS = true` in `site/measure/app.js`, with its pin in `tests/measure/test_site_page.py`
   and the "Uploads are off" lines of `site/measure/index.html` and `build/site_pages.py` (`how_to_submit`), then
   release again.
