# unread

Read coverage for code nobody read.

`unread` splits a codebase into hunks, keeps read receipts tied to each hunk's text, draws a random sample of hunks for a person to read, and turns what they find into an upper bound on how much of the unread rest could be wrong. One JavaScript file, no dependencies, Node 18+ and browsers.

```
npm test                         # 20 checks, about 2 seconds
node bin/unread.js help
```

## Why it exists

In October 2026 a developer published a Rust port of the TypeScript compiler written by an AI coding agent: about 1.3 million lines, all 181,711 ported tests passing, and a README that says the author never read a line of it. Tests say the code does what the tests check. They do not say anyone looked. Nobody is going to read 1.3 million lines either.

There is an old answer from factory inspection: you do not inspect every part, you inspect a random sample and state what that sample can promise. Read 59 randomly drawn hunks and find nothing wrong, and you can say, at 95% confidence, that at most 5% of all hunks are defective. Read 299 and the figure is 1%. `unread` does the bookkeeping: what was drawn, what was read, whether the code changed underneath the reading, and what can honestly be claimed.

## Use

```js
const unread = require("./src/unread.js");             // or <script src="unread.js"> -> window.unread

const book = unread.scan(files);                         // files: [{ path, text }]
const plan = unread.plan(book, { n: 59, seed: 2026 });   // draw before you read; plan.hash fingerprints the draw

// read each drawn hunk, then write a receipt
const receipts = plan.picks.map(k => unread.receipt(book.hunks[k.index], { verdict: "ok", reader: "me" }));

unread.judge(book, receipts, plan, { target: 0.05 })
// { read: 59, defects: 0, bound: 0.0432, verdict: "meets the target", reason: "... at most 4.32% of all 278 hunks ..." }
unread.coverage(book, receipts)                          // read share by lines and hunks, stale receipts, per file
```

## What each step does

| Step | Function | What happens |
|---|---|---|
| Split | `split(path, text)` | Cuts a file at top-level items (functions, impls, types, modules) where the brace depth is back at zero, skipping braces in strings, comments, chars and Rust lifetimes. Tiny blocks join the next one; long ones split at blank lines (60 lines at most). |
| Scan | `scan(files)` | All hunks of a codebase with line counts and a fingerprint of the whole book. |
| Risk | `risk(text, lang)` | Tags such as `unsafe`, `unwrap`, `panic`, `any`, `ignore`, `eval`, `todo`, `long`, `deep`. Useful for choosing what to read on top of the sample; they never count as findings. |
| Receipt | `receipt(hunk, {verdict, reader, note})` | One read: "ok" or "defect". Tied to the hunk's id (its file and its text), so an edit makes it stale. |
| Coverage | `coverage(book, receipts)` | Lines and hunks read, defects found, stale receipts, per file. |
| Plan | `plan(book, {n, seed})` | A reproducible random draw of hunks without replacement, hashed with the book's fingerprint so it cannot be changed after reading starts. |
| Bound | `bound(n, x, {conf, N})` | One-sided upper confidence bound on the defect rate after x defective in n read: exact hypergeometric when the number of hunks N is known, Clopper–Pearson otherwise. |
| Needed | `needed(rate, {conf, N, accept})` | The smallest sample that can show the rate is below a target. |
| Accept | `accept(n, c, p)` | The chance a plan (read n, pass with at most c defects) passes code whose true rate is p. |
| Judge | `judge(book, receipts, plan, {target, conf})` | Progress and verdict: "not started", "keep reading", "meets the target (so far)", "fails the target", "stale plan", "invalid plan". |
| Simulate | `simulate({N, n, rate, trials})` | Plants defects, draws, bounds, repeats: checks that the bound covers the truth as often as promised. |
| Demo | `demo({seed, rate})` | A simulated Rust crate in the style of a compiler port, with defects planted in a share of the functions and the truth kept aside. |

## How many hunks to read

With none of the drawn hunks defective, at 95% confidence:

| Claim: at most | 10% | 5% | 2% | 1% | 0.5% |
|---|---|---|---|---|---|
| hunks to read (large codebase) | 29 | 59 | 149 | 299 | 598 |
| hunks to read (52,000 hunks, about 1.3 million lines) | 29 | 59 | 148 | 297 | 592 |
| lines, at about 25 lines a hunk | ~725 | ~1,475 | ~3,700 | ~7,425 | ~14,800 |

`node examples/how-many.js` prints this table; `node bin/unread.js needed --rate 0.01` answers one case.

## Command line

```
node bin/unread.js demo ./crate --seed 7 --rate 0.03     # a simulated crate to practise on
node bin/unread.js scan ./crate
node bin/unread.js plan ./crate --n 59 --seed 2026        # writes ./crate/.unread/plan.json
node bin/unread.js show ./crate 1f3a9c0d2e                # print a drawn hunk
node bin/unread.js mark ./crate 1f3a9c0d2e ok --by me     # or: defect --note "..."
node bin/unread.js report ./crate --target 0.05
node bin/unread.js badge ./crate > read.svg
```

On the demo crate (seed 7, 3% of functions with a planted defect: 2,621 lines, 278 hunks, 3 of them defective), reading the 59 hunks drawn with seed 2026 finds none of the three, so the verdict is "meets the target": at most 4.32% of the 278 hunks, 12 hunks, are defective at 95% confidence. The truth is 3 hunks (1.1%), inside the bound, and also a reminder that 1.1% is not zero.

## Limits

- A bound is about the rate of defects a careful reader would catch in one hunk. Defects that only show across hunks (a wrong design, a missing feature, two functions that disagree) need other reading.
- The sample must be random and drawn before reading. Reading the risky hunks first is good practice, but those reads do not count toward the bound; keep them in separate receipts.
- "Defect" means whatever the reader decides it means. Write the rule down before reading (for example: wrong result, crash, undefined behaviour, or a security problem).
- Hunks are not all the same size. The bound is per hunk, not per line.
- Hunk ids include the file path: moving code to another file means reading it again.

## References

- Dodge, H. F. & Romig, H. G. 1929, Bell System Technical Journal 8, 613–631 — a method of sampling inspection.
- Clopper, C. J. & Pearson, E. S. 1934, Biometrika 26, 404–413 — exact confidence limits for a binomial proportion.
- Hanley, J. A. & Lippman-Hand, A. 1983, JAMA 249, 1743–1745 — "If nothing goes wrong, is everything all right?" (the rule of three).
- Fagan, M. E. 1976, IBM Systems Journal 15(3), 182–211 — design and code inspections.
- Bacchelli, A. & Bird, C. 2013, Proceedings of ICSE 2013, 712–721 — expectations, outcomes and challenges of modern code review.

MIT licence.
