contract
// ~/projects/magellan/contract.ts
const contract = {
status: "active",
started: "2026-09-20",
tags: ["typescript", "zod", "openapi"],
repo: "https://github.com/fabbrito/magellan-cloud/tree/master/packages/contract",
};magellan-device and magellan-cloud live in separate repositories and share no code and no database.
What they share is the contract: three routes under /v1/devices/{id}/. The manifest route takes
the device’s description of its sources and metrics, the batches route takes its readings, and the
heartbeats route takes its account of itself, apart from any batch. Every answer falls into one of
three classes, committed, rejected or try again later, and the device’s whole outage handling hangs
on that class.
How a change travels
The cloud defines the contract as zod schemas, and the same code that parses every request emits an
OpenAPI document, openapi.json. The device reads that document and has its own parser in Rust. At
first I planned to generate the Rust side from openapi.json mechanically, but the contract is
small and simple enough that copying it by hand, in an agent session in the device repository, costs
less than building and keeping a generator.
Until the contract reached 1.0.0, on 2026-09-30, I designed each change on both sides at once and deployed the two together. A decision made in one repository was replicated in the other before either shipped, so the contract kept changing without the two pieces ever disagreeing in production. At the MVP stage, being able to change the contract mattered more to me than keeping old versions working.
How it got here
The first contract, on 2026-09-21, deduplicated a batch by its boot id and sequence. The same day I moved deduplication to the readings’ own key, device, source and timestamp, because two boots could share a boot id and a device behind its highest sequence would have been refused. The boot id and sequence stayed as diagnostics, so a gap still shows.
Over the next nine days the limits were lowered so a manifest fits one D1 row (09-23), a counter could declare a daily reset (09-24), and the manifest gained its time zone (09-25). On 2026-09-30 the heartbeat moved apart from the batch and the contract became 1.0.0. From then on, a breaking change means a new major version, and nothing has broken since.
Limits
Every field has a stated bound, in the spirit of TigerStyle’s “bound everything”: up to 8 sources per manifest and 128 metrics per source, up to 512 readings per batch, a batch body of at most 6 MB, a heartbeat of at most 4 KB, and a manifest small enough to fit one D1 row. The device holds a copy of every limit and refuses a reading past one before queueing it, so a reading the cloud would reject never waits in the buffer.
Decisions
- A value travels as an integer with a declared decimal exponent, so nothing is rounded between the inverter and the chart.
- Timestamps are UTC. The manifest’s time zone only decides where calendar days are cut.
- Data flows one way, from device to cloud. The cloud never sends a command or pushes configuration.