{
  "blyg": "0.3",
  "id": "5b899v0wtmmaq8hd2r4gx229gz",
  "kind": "thread",
  "origin": "https://featherandwire.dev/blyg/",
  "page": "https://featherandwire.dev/articles/secret-santa-in-rust/",
  "author": {
    "name": "Christopher Chalcraft",
    "url": "https://featherandwire.dev/"
  },
  "created": "2026-11-25T00:00:00Z",
  "updated": "2026-11-25T00:00:00Z",
  "version": 1,
  "content_md": "# I let an agent rewrite Secret Santa, then made it prove it\n\nOur Secret Santa script had a bug that only shows up at Christmas. It shuffled everyone, checked the rules, and tried again, up to 1,000 times. Then it wrote the last shuffle to `pairs.csv` whether it passed or not.\n\nFor most lists that's harmless, because a valid shuffle turns up fast. One of our lists has households of two, one and two people, and under our rules it has exactly zero valid pairings. The script would grind through its thousand tries and quietly hand back a broken one. (The comments still say \"Secret Valentine\", which tells you how carefully it was adapted for December.)\n\nSo this year it got replaced. The new version is [santa](https://github.com/cchalc/secretsanta), a Rust CLI, and a coding agent wrote most of it in a day. The Python was two scripts, 137 lines between them. The Rust is 5,964 lines of source and 1,768 of tests. Nobody reads 7,700 lines of generated code before the holidays, so the question was how I'd know it's right.\n\n## What it does now\n\nThe rules haven't changed. Nobody draws themselves. Nobody draws someone from their own household. If A gives to B, B doesn't give back to A. And two people from one household never both give into the same other household.\n\nEverything around the rules is new.\n\n![The santa pipeline from roster to send, with the three pairing steps and the four sending rungs](/images/secret-santa-in-rust/santa-pipeline.svg)\n\nPairing tries the cheap thing first: shuffle, and keep the first draw that passes every rule. Throwing away the bad shuffles leaves a uniform draw over the good ones, which matters once people start comparing notes over dinner. If 200,000 shuffles all fail, a backtracking search finds a pairing, proves there isn't one, or runs out of budget and says so. A pairing from the search is valid but not guaranteed uniform, and santa tells you which method it used.\n\nWhen there isn't, santa says why without naming anyone, then bends the fewest rules it can, in a fixed order. Nobody-draws-themselves never bends. For the two-one-two list the answer is exactly one exception to the last rule: two people from one household get to give into the same other household, once. Everything else stays strict.\n\nThe result is sealed: receiver indices, a fingerprint of the roster and the policy it was drawn under, in an owner-only file. As the organizer I never see who has whom unless I run `santa reveal`. History blocks last year's pairings, and it's backed up to git encrypted with [age](https://age-encryption.org). And if a pairing ever has to be redone, the correction email swaps the Christmas trees for this guy, so nobody mistakes it for the first one:\n\n![A sheepish yeti at a keyboard, name slips floating around it and a hat knocked over nearby](/images/secret-santa-in-rust/santa-yeti.png)\n\n## Functional core, thin-ish shell\n\nThe design is a [functional core inside an imperative shell](https://www.destroyallsoftware.com/talks/boundaries). Core functions take data and return a `Result`. Randomness and email delivery are passed in, so a test can hand the core a seeded RNG and a fake mailer and get the same answer every time.\n\n![santa's modules. The shell holds main, cli, files and mailer. The core holds domain, roster, rules, solver, accommodate, seal, history, send, template, message, html and backup.](/images/secret-santa-in-rust/santa-modules.svg)\n\nDelivery is a two-method trait in `src/mailer.rs`:\n\n```rust\npub trait Mailer {\n    /// Check connectivity/credentials before anything is sent.\n    fn preflight(&self) -> Result<(), MailError> {\n        Ok(())\n    }\n    fn deliver(&self, letter: &Letter) -> Result<(), MailError>;\n}\n```\n\nThe other habit is [parse, don't validate](https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/). An `Assignment` can only be built as a real permutation, so nothing downstream has to check. `Assignment::from_receivers` in `src/domain.rs` is the only way in:\n\n```rust\n// ...after checking the length...\nreceivers\n    .iter()\n    .try_fold(vec![false; expected_len], |mut seen, &r| {\n        match seen.get(r).copied() {\n            None => Err(AssignmentError::OutOfRange(r)),\n            Some(true) => Err(AssignmentError::DuplicateReceiver(r)),\n            Some(false) => {\n                seen[r] = true;\n                Ok(seen)\n            }\n        }\n    })\n    .map(|_| Assignment(receivers))\n```\n\nThe shell is only thin-ish, though. `cli.rs` is 1,520 lines, the biggest file in the project, and some real logic lives there, like the look-ahead that stops a correction from painting next year into a corner. That's the next thing I'd move into the core.\n\n## Tests that don't trust the code\n\nAll 121 tests run in about two seconds. These are the ones I care about.\n\nThe oracle in `tests/oracle.rs` brute-forces every permutation and checks the rules with its own code. The checker shares nothing with `santa::rules`, on purpose: if the solver and its checker used the same helper, a bug in the helper would pass both.\n\n```rust\nfn oracle_valid(h: &[usize], p: &[usize]) -> bool {\n    let n = p.len();\n    let pairwise = (0..n).all(|i| {\n        p[i] != i && p[p[i]] != i && h[i] != h[p[i]]\n    });\n    let spread = (0..n).all(|i| {\n        (0..n).all(|j| i == j || h[i] != h[j] || h[p[i]] != h[p[j]])\n    });\n    pairwise && spread\n}\n```\n\n`h[i]` is person `i`'s household and `p[i]` is who they give to. [proptest](https://github.com/proptest-rs/proptest) generates random households and checks that the solver's count of valid pairings matches the oracle's, and that `solve` finds a pairing exactly when one exists.\n\nUniformity gets a chi-square test: a six-person roster with 32 valid pairings, 64,000 draws, and the statistic has to stay under 61.10, the p = 0.001 cutoff for 31 degrees of freedom. The seed is fixed, so it can't flake. This is the geophysicist in me talking: if the noise isn't flat, something upstream is lying.\n\nThen the privacy checks. The pairing and verify output, the simulation report, the inbox preview line and the encrypted backup all have tests asserting no names show up where they shouldn't. The backup check looks for email addresses too:\n\n```rust\nfn assert_no_people(text: &str) {\n    for name in NAMES {\n        assert!(!text.contains(name), \"leaks {name}:\\n{text}\");\n        assert!(\n            !text.contains(&address(name)),\n            \"leaks {name}'s email:\\n{text}\"\n        );\n    }\n}\n```\n\nEmail gets a fake SMTP server inside the test process. It records every envelope and can reject the k-th recipient on purpose, so one test kills a send halfway and checks that the resume sends nobody a second email.\n\n## Planting bugs\n\nA green suite only means something if it goes red when the code is wrong. So I planted three bugs, each in a throwaway copy, and ran everything:\n\n| Planted bug | Tests that failed |\n|---|---|\n| Nobody-draws-themselves becomes relaxable | 3 |\n| The one-household-into-another rule lets one repeat through | 9 |\n| The backtracking search forgets the reciprocal rule | 17 |\n\nThe first one is humbling. Only the three unit tests aimed at it noticed. The oracle didn't, and I think that's because the accommodation never reaches for that rule, so the pairings that come out don't change. The third was the one I worried about, since sampling stays correct and only paths that reach the search see the bug. Seventeen tests did.\n\n## Fast enough\n\nNone of this needed to be fast. Here it is anyway, from `just bench` (criterion) on an M4 Pro:\n\n| Case | Time |\n|---|---|\n| 6 people, 3 households | 2.2 µs |\n| Sampling, 50 / 200 / 1,000 people | 92 µs / 224 µs / 1.1 ms |\n| Exhaustive search, 50 people | 0.51 ms |\n| Prove the two-one-two list has no strict pairing | 4.8 µs |\n| Count every valid pairing for a family of 10 | 33 ms |\n\nThe old script spent a thousand shuffles failing to notice that list was impossible. The new one proves it in under five microseconds.\n\n## Before it emails my family\n\nThe last safeguard isn't code. Sending is a ladder:\n\n1. A dry run that prints every email with the receiver redacted.\n2. A full rehearsal into a local [Mailpit](https://mailpit.axllent.org) inbox.\n3. One real `[TEST]` email to myself through Gmail.\n4. The real `santa send`, which lists the recipients and waits until I type `send N`, where N is how many emails are pending. Anything else aborts.\n\nIf the send dies partway, a ledger means the rerun skips everyone who already has their email.\n\nThe agent wrote the code that sends. I'm the one who types the number.\n\n## Sources\n\n1. [santa on GitHub](https://github.com/cchalc/secretsanta): README, source and tests; the Python original is on the `python-legacy` branch\n2. [proptest](https://github.com/proptest-rs/proptest), property testing for Rust\n3. [Criterion.rs](https://github.com/bheisler/criterion.rs), the benchmark harness\n4. [Mailpit](https://mailpit.axllent.org), the local test inbox\n5. [age](https://age-encryption.org), which encrypts the pairing history backup\n6. Gary Bernhardt, [Boundaries](https://www.destroyallsoftware.com/talks/boundaries), on functional core, imperative shell\n7. Alexis King, [Parse, don't validate](https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/)\n",
  "content_html": "<h1>I let an agent rewrite Secret Santa, then made it prove it</h1>\n<p>Our Secret Santa script had a bug that only shows up at Christmas. It shuffled everyone, checked the rules, and tried again, up to 1,000 times. Then it wrote the last shuffle to <code>pairs.csv</code> whether it passed or not.</p>\n<p>For most lists that’s harmless, because a valid shuffle turns up fast. One of our lists has households of two, one and two people, and under our rules it has exactly zero valid pairings. The script would grind through its thousand tries and quietly hand back a broken one. (The comments still say “Secret Valentine”, which tells you how carefully it was adapted for December.)</p>\n<p>So this year it got replaced. The new version is <a href=\"https://github.com/cchalc/secretsanta\">santa</a>, a Rust CLI, and a coding agent wrote most of it in a day. The Python was two scripts, 137 lines between them. The Rust is 5,964 lines of source and 1,768 of tests. Nobody reads 7,700 lines of generated code before the holidays, so the question was how I’d know it’s right.</p>\n<h2 id=\"what-it-does-now\">What it does now</h2>\n<p>The rules haven’t changed. Nobody draws themselves. Nobody draws someone from their own household. If A gives to B, B doesn’t give back to A. And two people from one household never both give into the same other household.</p>\n<p>Everything around the rules is new.</p>\n<p><img src=\"https://featherandwire.dev/images/secret-santa-in-rust/santa-pipeline.svg\" alt=\"The santa pipeline from roster to send, with the three pairing steps and the four sending rungs\"></p>\n<p>Pairing tries the cheap thing first: shuffle, and keep the first draw that passes every rule. Throwing away the bad shuffles leaves a uniform draw over the good ones, which matters once people start comparing notes over dinner. If 200,000 shuffles all fail, a backtracking search finds a pairing, proves there isn’t one, or runs out of budget and says so. A pairing from the search is valid but not guaranteed uniform, and santa tells you which method it used.</p>\n<p>When there isn’t, santa says why without naming anyone, then bends the fewest rules it can, in a fixed order. Nobody-draws-themselves never bends. For the two-one-two list the answer is exactly one exception to the last rule: two people from one household get to give into the same other household, once. Everything else stays strict.</p>\n<p>The result is sealed: receiver indices, a fingerprint of the roster and the policy it was drawn under, in an owner-only file. As the organizer I never see who has whom unless I run <code>santa reveal</code>. History blocks last year’s pairings, and it’s backed up to git encrypted with <a href=\"https://age-encryption.org/\">age</a>. And if a pairing ever has to be redone, the correction email swaps the Christmas trees for this guy, so nobody mistakes it for the first one:</p>\n<p><img src=\"https://featherandwire.dev/images/secret-santa-in-rust/santa-yeti.png\" alt=\"A sheepish yeti at a keyboard, name slips floating around it and a hat knocked over nearby\"></p>\n<h2 id=\"functional-core-thin-ish-shell\">Functional core, thin-ish shell</h2>\n<p>The design is a <a href=\"https://www.destroyallsoftware.com/talks/boundaries\">functional core inside an imperative shell</a>. Core functions take data and return a <code>Result</code>. Randomness and email delivery are passed in, so a test can hand the core a seeded RNG and a fake mailer and get the same answer every time.</p>\n<p><img src=\"https://featherandwire.dev/images/secret-santa-in-rust/santa-modules.svg\" alt=\"santa’s modules. The shell holds main, cli, files and mailer. The core holds domain, roster, rules, solver, accommodate, seal, history, send, template, message, html and backup.\"></p>\n<p>Delivery is a two-method trait in <code>src/mailer.rs</code>:</p>\n<pre class=\"astro-code astro-code-themes gruvbox-dark-hard gruvbox-dark-medium\" style=\"background-color:#1d2021;--shiki-dark-bg:#282828;color:#ebdbb2;--shiki-dark:#ebdbb2; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;\" tabindex=\"0\" data-language=\"rust\"><code><span class=\"line\"><span style=\"color:#FB4934;--shiki-dark:#FB4934\">pub</span><span style=\"color:#FE8019;--shiki-dark:#FE8019\"> trait</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\"> Mailer</span><span style=\"color:#A89984;--shiki-dark:#A89984\"> {</span></span>\n<span class=\"line\"><span style=\"color:#928374;--shiki-light-font-style:italic;--shiki-dark:#928374;--shiki-dark-font-style:italic\">    /// Check connectivity/credentials before anything is sent.</span></span>\n<span class=\"line\"><span style=\"color:#FB4934;--shiki-dark:#FB4934\">    fn</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\"> preflight</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">&amp;</span><span style=\"color:#83A598;--shiki-dark:#83A598\">self</span><span style=\"color:#A89984;--shiki-dark:#A89984\">)</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> -&gt;</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\"> Result</span><span style=\"color:#A89984;--shiki-dark:#A89984\">&lt;(),</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\"> MailError</span><span style=\"color:#A89984;--shiki-dark:#A89984\">&gt;</span><span style=\"color:#A89984;--shiki-dark:#A89984\"> {</span></span>\n<span class=\"line\"><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">        Ok</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(())</span></span>\n<span class=\"line\"><span style=\"color:#A89984;--shiki-dark:#A89984\">    }</span></span>\n<span class=\"line\"><span style=\"color:#FB4934;--shiki-dark:#FB4934\">    fn</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\"> deliver</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">&amp;</span><span style=\"color:#83A598;--shiki-dark:#83A598\">self</span><span style=\"color:#A89984;--shiki-dark:#A89984\">,</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> letter</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">:</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> &amp;</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">Letter</span><span style=\"color:#A89984;--shiki-dark:#A89984\">)</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> -&gt;</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\"> Result</span><span style=\"color:#A89984;--shiki-dark:#A89984\">&lt;(),</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\"> MailError</span><span style=\"color:#A89984;--shiki-dark:#A89984\">&gt;;</span></span>\n<span class=\"line\"><span style=\"color:#A89984;--shiki-dark:#A89984\">}</span></span></code></pre>\n<p>The other habit is <a href=\"https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/\">parse, don’t validate</a>. An <code>Assignment</code> can only be built as a real permutation, so nothing downstream has to check. <code>Assignment::from_receivers</code> in <code>src/domain.rs</code> is the only way in:</p>\n<pre class=\"astro-code astro-code-themes gruvbox-dark-hard gruvbox-dark-medium\" style=\"background-color:#1d2021;--shiki-dark-bg:#282828;color:#ebdbb2;--shiki-dark:#ebdbb2; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;\" tabindex=\"0\" data-language=\"rust\"><code><span class=\"line\"><span style=\"color:#928374;--shiki-light-font-style:italic;--shiki-dark:#928374;--shiki-dark-font-style:italic\">// ...after checking the length...</span></span>\n<span class=\"line\"><span style=\"color:#83A598;--shiki-dark:#83A598\">receivers</span></span>\n<span class=\"line\"><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">    .</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">iter</span><span style=\"color:#A89984;--shiki-dark:#A89984\">()</span></span>\n<span class=\"line\"><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">    .</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">try_fold</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">vec!</span><span style=\"color:#A89984;--shiki-dark:#A89984\">[</span><span style=\"color:#D3869B;--shiki-dark:#D3869B\">false</span><span style=\"color:#A89984;--shiki-dark:#A89984\">;</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> expected_len</span><span style=\"color:#A89984;--shiki-dark:#A89984\">],</span><span style=\"color:#FB4934;--shiki-dark:#FB4934\"> |</span><span style=\"color:#FE8019;--shiki-dark:#FE8019\">mut</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> seen</span><span style=\"color:#A89984;--shiki-dark:#A89984\">,</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> &amp;</span><span style=\"color:#83A598;--shiki-dark:#83A598\">r</span><span style=\"color:#FB4934;--shiki-dark:#FB4934\">|</span><span style=\"color:#A89984;--shiki-dark:#A89984\"> {</span></span>\n<span class=\"line\"><span style=\"color:#FB4934;--shiki-dark:#FB4934\">        match</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> seen</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">.</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">get</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#83A598;--shiki-dark:#83A598\">r</span><span style=\"color:#A89984;--shiki-dark:#A89984\">)</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">.</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">copied</span><span style=\"color:#A89984;--shiki-dark:#A89984\">()</span><span style=\"color:#A89984;--shiki-dark:#A89984\"> {</span></span>\n<span class=\"line\"><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">            None</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> =&gt;</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\"> Err</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">AssignmentError</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">::</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">OutOfRange</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#83A598;--shiki-dark:#83A598\">r</span><span style=\"color:#A89984;--shiki-dark:#A89984\">)),</span></span>\n<span class=\"line\"><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">            Some</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#D3869B;--shiki-dark:#D3869B\">true</span><span style=\"color:#A89984;--shiki-dark:#A89984\">)</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> =&gt;</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\"> Err</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">AssignmentError</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">::</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">DuplicateReceiver</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#83A598;--shiki-dark:#83A598\">r</span><span style=\"color:#A89984;--shiki-dark:#A89984\">)),</span></span>\n<span class=\"line\"><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">            Some</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#D3869B;--shiki-dark:#D3869B\">false</span><span style=\"color:#A89984;--shiki-dark:#A89984\">)</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> =&gt;</span><span style=\"color:#A89984;--shiki-dark:#A89984\"> {</span></span>\n<span class=\"line\"><span style=\"color:#83A598;--shiki-dark:#83A598\">                seen</span><span style=\"color:#A89984;--shiki-dark:#A89984\">[</span><span style=\"color:#83A598;--shiki-dark:#83A598\">r</span><span style=\"color:#A89984;--shiki-dark:#A89984\">]</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> =</span><span style=\"color:#D3869B;--shiki-dark:#D3869B\"> true</span><span style=\"color:#A89984;--shiki-dark:#A89984\">;</span></span>\n<span class=\"line\"><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">                Ok</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#83A598;--shiki-dark:#83A598\">seen</span><span style=\"color:#A89984;--shiki-dark:#A89984\">)</span></span>\n<span class=\"line\"><span style=\"color:#A89984;--shiki-dark:#A89984\">            }</span></span>\n<span class=\"line\"><span style=\"color:#A89984;--shiki-dark:#A89984\">        }</span></span>\n<span class=\"line\"><span style=\"color:#A89984;--shiki-dark:#A89984\">    })</span></span>\n<span class=\"line\"><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">    .</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">map</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#FB4934;--shiki-dark:#FB4934\">|</span><span style=\"color:#83A598;--shiki-dark:#83A598\">_</span><span style=\"color:#FB4934;--shiki-dark:#FB4934\">|</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\"> Assignment</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#83A598;--shiki-dark:#83A598\">receivers</span><span style=\"color:#A89984;--shiki-dark:#A89984\">))</span></span></code></pre>\n<p>The shell is only thin-ish, though. <code>cli.rs</code> is 1,520 lines, the biggest file in the project, and some real logic lives there, like the look-ahead that stops a correction from painting next year into a corner. That’s the next thing I’d move into the core.</p>\n<h2 id=\"tests-that-dont-trust-the-code\">Tests that don’t trust the code</h2>\n<p>All 121 tests run in about two seconds. These are the ones I care about.</p>\n<p>The oracle in <code>tests/oracle.rs</code> brute-forces every permutation and checks the rules with its own code. The checker shares nothing with <code>santa::rules</code>, on purpose: if the solver and its checker used the same helper, a bug in the helper would pass both.</p>\n<pre class=\"astro-code astro-code-themes gruvbox-dark-hard gruvbox-dark-medium\" style=\"background-color:#1d2021;--shiki-dark-bg:#282828;color:#ebdbb2;--shiki-dark:#ebdbb2; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;\" tabindex=\"0\" data-language=\"rust\"><code><span class=\"line\"><span style=\"color:#FB4934;--shiki-dark:#FB4934\">fn</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\"> oracle_valid</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#83A598;--shiki-dark:#83A598\">h</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">:</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> &amp;</span><span style=\"color:#A89984;--shiki-dark:#A89984\">[</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">usize</span><span style=\"color:#A89984;--shiki-dark:#A89984\">],</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> p</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">:</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> &amp;</span><span style=\"color:#A89984;--shiki-dark:#A89984\">[</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">usize</span><span style=\"color:#A89984;--shiki-dark:#A89984\">])</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> -&gt;</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\"> bool</span><span style=\"color:#A89984;--shiki-dark:#A89984\"> {</span></span>\n<span class=\"line\"><span style=\"color:#FE8019;--shiki-dark:#FE8019\">    let</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> n</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> =</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> p</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">.</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">len</span><span style=\"color:#A89984;--shiki-dark:#A89984\">();</span></span>\n<span class=\"line\"><span style=\"color:#FE8019;--shiki-dark:#FE8019\">    let</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> pairwise</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> =</span><span style=\"color:#A89984;--shiki-dark:#A89984\"> (</span><span style=\"color:#D3869B;--shiki-dark:#D3869B\">0</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">..</span><span style=\"color:#83A598;--shiki-dark:#83A598\">n</span><span style=\"color:#A89984;--shiki-dark:#A89984\">)</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">.</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">all</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#FB4934;--shiki-dark:#FB4934\">|</span><span style=\"color:#83A598;--shiki-dark:#83A598\">i</span><span style=\"color:#FB4934;--shiki-dark:#FB4934\">|</span><span style=\"color:#A89984;--shiki-dark:#A89984\"> {</span></span>\n<span class=\"line\"><span style=\"color:#83A598;--shiki-dark:#83A598\">        p</span><span style=\"color:#A89984;--shiki-dark:#A89984\">[</span><span style=\"color:#83A598;--shiki-dark:#83A598\">i</span><span style=\"color:#A89984;--shiki-dark:#A89984\">]</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> !=</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> i</span><span style=\"color:#FB4934;--shiki-dark:#FB4934\"> &amp;&amp;</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> p</span><span style=\"color:#A89984;--shiki-dark:#A89984\">[</span><span style=\"color:#83A598;--shiki-dark:#83A598\">p</span><span style=\"color:#A89984;--shiki-dark:#A89984\">[</span><span style=\"color:#83A598;--shiki-dark:#83A598\">i</span><span style=\"color:#A89984;--shiki-dark:#A89984\">]]</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> !=</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> i</span><span style=\"color:#FB4934;--shiki-dark:#FB4934\"> &amp;&amp;</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> h</span><span style=\"color:#A89984;--shiki-dark:#A89984\">[</span><span style=\"color:#83A598;--shiki-dark:#83A598\">i</span><span style=\"color:#A89984;--shiki-dark:#A89984\">]</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> !=</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> h</span><span style=\"color:#A89984;--shiki-dark:#A89984\">[</span><span style=\"color:#83A598;--shiki-dark:#83A598\">p</span><span style=\"color:#A89984;--shiki-dark:#A89984\">[</span><span style=\"color:#83A598;--shiki-dark:#83A598\">i</span><span style=\"color:#A89984;--shiki-dark:#A89984\">]]</span></span>\n<span class=\"line\"><span style=\"color:#A89984;--shiki-dark:#A89984\">    });</span></span>\n<span class=\"line\"><span style=\"color:#FE8019;--shiki-dark:#FE8019\">    let</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> spread</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> =</span><span style=\"color:#A89984;--shiki-dark:#A89984\"> (</span><span style=\"color:#D3869B;--shiki-dark:#D3869B\">0</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">..</span><span style=\"color:#83A598;--shiki-dark:#83A598\">n</span><span style=\"color:#A89984;--shiki-dark:#A89984\">)</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">.</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">all</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#FB4934;--shiki-dark:#FB4934\">|</span><span style=\"color:#83A598;--shiki-dark:#83A598\">i</span><span style=\"color:#FB4934;--shiki-dark:#FB4934\">|</span><span style=\"color:#A89984;--shiki-dark:#A89984\"> {</span></span>\n<span class=\"line\"><span style=\"color:#A89984;--shiki-dark:#A89984\">        (</span><span style=\"color:#D3869B;--shiki-dark:#D3869B\">0</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">..</span><span style=\"color:#83A598;--shiki-dark:#83A598\">n</span><span style=\"color:#A89984;--shiki-dark:#A89984\">)</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">.</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">all</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#FB4934;--shiki-dark:#FB4934\">|</span><span style=\"color:#83A598;--shiki-dark:#83A598\">j</span><span style=\"color:#FB4934;--shiki-dark:#FB4934\">|</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> i</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> ==</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> j</span><span style=\"color:#FB4934;--shiki-dark:#FB4934\"> ||</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> h</span><span style=\"color:#A89984;--shiki-dark:#A89984\">[</span><span style=\"color:#83A598;--shiki-dark:#83A598\">i</span><span style=\"color:#A89984;--shiki-dark:#A89984\">]</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> !=</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> h</span><span style=\"color:#A89984;--shiki-dark:#A89984\">[</span><span style=\"color:#83A598;--shiki-dark:#83A598\">j</span><span style=\"color:#A89984;--shiki-dark:#A89984\">]</span><span style=\"color:#FB4934;--shiki-dark:#FB4934\"> ||</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> h</span><span style=\"color:#A89984;--shiki-dark:#A89984\">[</span><span style=\"color:#83A598;--shiki-dark:#83A598\">p</span><span style=\"color:#A89984;--shiki-dark:#A89984\">[</span><span style=\"color:#83A598;--shiki-dark:#83A598\">i</span><span style=\"color:#A89984;--shiki-dark:#A89984\">]]</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> !=</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> h</span><span style=\"color:#A89984;--shiki-dark:#A89984\">[</span><span style=\"color:#83A598;--shiki-dark:#83A598\">p</span><span style=\"color:#A89984;--shiki-dark:#A89984\">[</span><span style=\"color:#83A598;--shiki-dark:#83A598\">j</span><span style=\"color:#A89984;--shiki-dark:#A89984\">]])</span></span>\n<span class=\"line\"><span style=\"color:#A89984;--shiki-dark:#A89984\">    });</span></span>\n<span class=\"line\"><span style=\"color:#83A598;--shiki-dark:#83A598\">    pairwise</span><span style=\"color:#FB4934;--shiki-dark:#FB4934\"> &amp;&amp;</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> spread</span></span>\n<span class=\"line\"><span style=\"color:#A89984;--shiki-dark:#A89984\">}</span></span></code></pre>\n<p><code>h[i]</code> is person <code>i</code>’s household and <code>p[i]</code> is who they give to. <a href=\"https://github.com/proptest-rs/proptest\">proptest</a> generates random households and checks that the solver’s count of valid pairings matches the oracle’s, and that <code>solve</code> finds a pairing exactly when one exists.</p>\n<p>Uniformity gets a chi-square test: a six-person roster with 32 valid pairings, 64,000 draws, and the statistic has to stay under 61.10, the p = 0.001 cutoff for 31 degrees of freedom. The seed is fixed, so it can’t flake. This is the geophysicist in me talking: if the noise isn’t flat, something upstream is lying.</p>\n<p>Then the privacy checks. The pairing and verify output, the simulation report, the inbox preview line and the encrypted backup all have tests asserting no names show up where they shouldn’t. The backup check looks for email addresses too:</p>\n<pre class=\"astro-code astro-code-themes gruvbox-dark-hard gruvbox-dark-medium\" style=\"background-color:#1d2021;--shiki-dark-bg:#282828;color:#ebdbb2;--shiki-dark:#ebdbb2; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;\" tabindex=\"0\" data-language=\"rust\"><code><span class=\"line\"><span style=\"color:#FB4934;--shiki-dark:#FB4934\">fn</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\"> assert_no_people</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#83A598;--shiki-dark:#83A598\">text</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">:</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\"> &amp;</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">str</span><span style=\"color:#A89984;--shiki-dark:#A89984\">)</span><span style=\"color:#A89984;--shiki-dark:#A89984\"> {</span></span>\n<span class=\"line\"><span style=\"color:#FB4934;--shiki-dark:#FB4934\">    for</span><span style=\"color:#83A598;--shiki-dark:#83A598\"> name</span><span style=\"color:#FB4934;--shiki-dark:#FB4934\"> in</span><span style=\"color:#D3869B;--shiki-light-font-weight:bold;--shiki-dark:#D3869B;--shiki-dark-font-weight:bold\"> NAMES</span><span style=\"color:#A89984;--shiki-dark:#A89984\"> {</span></span>\n<span class=\"line\"><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">        assert!</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#FB4934;--shiki-dark:#FB4934\">!</span><span style=\"color:#83A598;--shiki-dark:#83A598\">text</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">.</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">contains</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#83A598;--shiki-dark:#83A598\">name</span><span style=\"color:#A89984;--shiki-dark:#A89984\">),</span><span style=\"color:#A89984;--shiki-dark:#A89984\"> \"</span><span style=\"color:#B8BB26;--shiki-dark:#B8BB26\">leaks </span><span style=\"color:#A89984;--shiki-dark:#A89984\">{</span><span style=\"color:#B8BB26;--shiki-dark:#B8BB26\">name</span><span style=\"color:#A89984;--shiki-dark:#A89984\">}</span><span style=\"color:#B8BB26;--shiki-dark:#B8BB26\">:</span><span style=\"color:#D3869B;--shiki-dark:#D3869B\">\\n</span><span style=\"color:#A89984;--shiki-dark:#A89984\">{</span><span style=\"color:#B8BB26;--shiki-dark:#B8BB26\">text</span><span style=\"color:#A89984;--shiki-dark:#A89984\">}\"</span><span style=\"color:#A89984;--shiki-dark:#A89984\">);</span></span>\n<span class=\"line\"><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">        assert!</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span></span>\n<span class=\"line\"><span style=\"color:#FB4934;--shiki-dark:#FB4934\">            !</span><span style=\"color:#83A598;--shiki-dark:#83A598\">text</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">.</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">contains</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#8EC07C;--shiki-dark:#8EC07C\">&amp;</span><span style=\"color:#FABD2F;--shiki-dark:#FABD2F\">address</span><span style=\"color:#A89984;--shiki-dark:#A89984\">(</span><span style=\"color:#83A598;--shiki-dark:#83A598\">name</span><span style=\"color:#A89984;--shiki-dark:#A89984\">)),</span></span>\n<span class=\"line\"><span style=\"color:#A89984;--shiki-dark:#A89984\">            \"</span><span style=\"color:#B8BB26;--shiki-dark:#B8BB26\">leaks </span><span style=\"color:#A89984;--shiki-dark:#A89984\">{</span><span style=\"color:#B8BB26;--shiki-dark:#B8BB26\">name</span><span style=\"color:#A89984;--shiki-dark:#A89984\">}</span><span style=\"color:#B8BB26;--shiki-dark:#B8BB26\">'s email:</span><span style=\"color:#D3869B;--shiki-dark:#D3869B\">\\n</span><span style=\"color:#A89984;--shiki-dark:#A89984\">{</span><span style=\"color:#B8BB26;--shiki-dark:#B8BB26\">text</span><span style=\"color:#A89984;--shiki-dark:#A89984\">}\"</span></span>\n<span class=\"line\"><span style=\"color:#A89984;--shiki-dark:#A89984\">        );</span></span>\n<span class=\"line\"><span style=\"color:#A89984;--shiki-dark:#A89984\">    }</span></span>\n<span class=\"line\"><span style=\"color:#A89984;--shiki-dark:#A89984\">}</span></span></code></pre>\n<p>Email gets a fake SMTP server inside the test process. It records every envelope and can reject the k-th recipient on purpose, so one test kills a send halfway and checks that the resume sends nobody a second email.</p>\n<h2 id=\"planting-bugs\">Planting bugs</h2>\n<p>A green suite only means something if it goes red when the code is wrong. So I planted three bugs, each in a throwaway copy, and ran everything:</p>\n<table>\n<thead>\n<tr>\n<th>Planted bug</th>\n<th>Tests that failed</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Nobody-draws-themselves becomes relaxable</td>\n<td>3</td>\n</tr>\n<tr>\n<td>The one-household-into-another rule lets one repeat through</td>\n<td>9</td>\n</tr>\n<tr>\n<td>The backtracking search forgets the reciprocal rule</td>\n<td>17</td>\n</tr>\n</tbody>\n</table>\n<p>The first one is humbling. Only the three unit tests aimed at it noticed. The oracle didn’t, and I think that’s because the accommodation never reaches for that rule, so the pairings that come out don’t change. The third was the one I worried about, since sampling stays correct and only paths that reach the search see the bug. Seventeen tests did.</p>\n<h2 id=\"fast-enough\">Fast enough</h2>\n<p>None of this needed to be fast. Here it is anyway, from <code>just bench</code> (criterion) on an M4 Pro:</p>\n<table>\n<thead>\n<tr>\n<th>Case</th>\n<th>Time</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>6 people, 3 households</td>\n<td>2.2 µs</td>\n</tr>\n<tr>\n<td>Sampling, 50 / 200 / 1,000 people</td>\n<td>92 µs / 224 µs / 1.1 ms</td>\n</tr>\n<tr>\n<td>Exhaustive search, 50 people</td>\n<td>0.51 ms</td>\n</tr>\n<tr>\n<td>Prove the two-one-two list has no strict pairing</td>\n<td>4.8 µs</td>\n</tr>\n<tr>\n<td>Count every valid pairing for a family of 10</td>\n<td>33 ms</td>\n</tr>\n</tbody>\n</table>\n<p>The old script spent a thousand shuffles failing to notice that list was impossible. The new one proves it in under five microseconds.</p>\n<h2 id=\"before-it-emails-my-family\">Before it emails my family</h2>\n<p>The last safeguard isn’t code. Sending is a ladder:</p>\n<ol>\n<li>A dry run that prints every email with the receiver redacted.</li>\n<li>A full rehearsal into a local <a href=\"https://mailpit.axllent.org/\">Mailpit</a> inbox.</li>\n<li>One real <code>[TEST]</code> email to myself through Gmail.</li>\n<li>The real <code>santa send</code>, which lists the recipients and waits until I type <code>send N</code>, where N is how many emails are pending. Anything else aborts.</li>\n</ol>\n<p>If the send dies partway, a ledger means the rerun skips everyone who already has their email.</p>\n<p>The agent wrote the code that sends. I’m the one who types the number.</p>\n<h2 id=\"sources\">Sources</h2>\n<ol>\n<li><a href=\"https://github.com/cchalc/secretsanta\">santa on GitHub</a>: README, source and tests; the Python original is on the <code>python-legacy</code> branch</li>\n<li><a href=\"https://github.com/proptest-rs/proptest\">proptest</a>, property testing for Rust</li>\n<li><a href=\"https://github.com/bheisler/criterion.rs\">Criterion.rs</a>, the benchmark harness</li>\n<li><a href=\"https://mailpit.axllent.org/\">Mailpit</a>, the local test inbox</li>\n<li><a href=\"https://age-encryption.org/\">age</a>, which encrypts the pairing history backup</li>\n<li>Gary Bernhardt, <a href=\"https://www.destroyallsoftware.com/talks/boundaries\">Boundaries</a>, on functional core, imperative shell</li>\n<li>Alexis King, <a href=\"https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/\">Parse, don’t validate</a></li>\n</ol>\n",
  "content_hash": "sha256:530b06ffe45b613480630d56b6a3a630d692c8d32f38440d7c05d4a6511f6c43",
  "media": [],
  "transclusions": [],
  "changelog": [
    {
      "version": 1,
      "at": "2026-11-25T00:00:00Z",
      "note": null
    }
  ]
}
