Small Enough to Read in One Sitting
Delta is over nine thousand lines of Python because it has to survive a family's real network: three kinds of route, tickets, pinned TLS, a disk cache, parallel workers, bundles, stall timeouts, links, pairs and a write call. But the rules that make it trustworthy fit in a page. This lesson is that page as running code: minichannel.py, one file of standard-library Python that serves a folder on loopback and reads it back, and keeps seven of the quest's promises. Run it with no arguments and it builds a temporary share, starts its owner on a free port, and drives every rule against it: python3 minichannel.py.
It runs on Apple's /usr/bin/python3 (3.9) as well as anything newer, which is the same constraint Delta lives under.
Seven Promises, and Where Each Lives
| Promise | In minichannel.py | In Delta |
|---|---|---|
| Refusals are about places | clean_rel refuses .. and NUL, and any path with a component in REFUSED, before anything is opened | clean_rel, CREDENTIAL_*, the TCC guard, checked as asked and with links followed (Track 2) |
| Loopback is not identity | every request must carry the token, compared with secrets.compare_digest | X-Delta-Local-Token, a 0600 file only the account can read (Track 4) |
| Validators come from the owner | validator(st) is size, mtime in nanoseconds and inode; If-None-Match gets a 304 | the owner's ETag, or a child's own source tag; never a TTL (Track 5) |
| Every error has a named cause | ChannelError(cause, detail, status) crosses the wire as a JSON document with error, detail and status, and is rebuilt on the reader's side | delta/errors.py, every failure naming what, where and why (Track 1) |
| A short body is an error | the reader catches the http.client.IncompleteRead a body that ends early raises, keeps what did arrive, and compares it with Content-Length: a cut comes back as a named unavailable saying how far it got | peer.Response, because read(n) returns short without a word (Track 3) |
| Verified before recorded | each chunk is checked against the source's SHA-256, written at its offset, fsynced, and only then appended to the record | Partial: one appended line per chunk (Track 6) |
| Lands whole or not at all | the file's chunk list must equal the source's, then os.replace moves it into place | the atomic rename after the chunk list matches (Track 6) |
Two smaller rules ride along. The owner reads (drains) any request body before it answers, even with a refusal, so a refusal never strands bytes in a kept-alive connection (Track 6). And the partial's name includes the source's validator, so if the file changes between runs the next pull starts a fresh partial instead of splicing two versions together. The old one is simply abandoned, which is why Delta sweeps partials untouched for fourteen days (lesson 2 of this track).
Walking the Demo
The run prints six numbered results (the second code block shows them). Each one is a promise kept, and you should be able to say which line of code kept it:
- The first read is a
miss: bytes crossed. The second sends the validator it holds and gets a 304:revalidated, no bytes. - The first pull is cut on purpose after two chunks. The cut is an error with a cause, not a short file.
- The second pull reads its record, says how many chunks are already verified, asks only for the rest, checks the whole chunk list and renames the file into place. The demo then compares the SHA-256 of source and copy.
- A
..path, a path into a refused place and a missing file each come back as a named cause:forbidden,forbidden,not_found. - A wrong token is
unauthorized, before the path is even looked at. - A second owner,
Cutter, promises the whole file in itsContent-Lengthand closes the connection halfway. The reader does not hand back half a file: it raisesunavailable, "closed mid-body at N of M". Two experiments show what each line buys. Remove the innerexcept http.client.IncompleteReadand the cut is still named, but by the outer handler, which can only repeat the exception's text. Then narrow the outer handler toexcept OSErrorand the cut escapes as an unnamed traceback:IncompleteReadis anhttp.client.HTTPException, not anOSError.
What It Leaves Out, on Purpose
Everything below is real in Delta and absent here. Each is a good next step, roughly in the order the quest taught them:
- Routes and identity between two real Macs. Here both ends are one process on loopback. Across Macs you need Track 3's on-link choice and Track 4's roster, tickets and pinned certificates.
- A cache that survives a restart. The reader's cache is a dict. Delta keeps blobs on disk named by SHA-256, bounded by size and by the disk floor, and labels a copy
stalewhen the owner cannot be reached. - Parallel workers, bundles and stall timeouts. Chunks here move one at a time and each request has one 30-second timeout. Delta spreads 16 MiB chunks over several workers, batches small files, and times out only on a stall.
- Jobs. A pull here lives as long as your terminal. Delta runs it inside the daemon and lets you follow it.
- Two-way. A named pair with an archive, and a write call sent at most once, are Track 7.
The Checklist You Take Home
Whatever you build that moves data between machines, these are the questions the quest leaves you with:
- Is data riding a channel you own and can read the code of, while commands keep their own?
- Which single process may dial the network, and do the others ask it locally?
- Is every refusal a rule about a place, checked before the place is touched?
- Is the route chosen from facts you verified, never a name?
- Is identity proven on a channel that already authenticates, and is anything you hand out stored only as a hash?
- Does every cached answer come with the owner's validator, and does a stale one say so?
- Is every chunk verified before it is recorded, and does a re-run resume?
- Does a job end
doneonly when every planned item landed or is named? - Is a write that must not happen twice sent at most once, with did it leave? as a question you can answer?
- Does everything that accumulates have a number, and is that number in the health document?