Add the census plugin #130

Merged
aluminum-ice merged 14 commits from feature/census-plugin into main 2026-11-01 01:04:49 +00:00
Owner

What this PR does

Adds the census plugin: an opt-in, anonymous, serverless count of active Kestrels kept in the public BitTorrent DHT (BEP 44 mutable items). Each enabled device adds a 20-byte report once an hour and reads everyone's reports back:

  • its card shows Kestrels online, active today, and "desktop won X of Y training contests this week";
  • its Census page shows releases, learning setups, remote-training states, 7- and 30-day contest results, a 30-day history, and exactly what this device reports.

It also adds tools/census-report/, a desktop script that reads the same public slots and prints the census as plain tables (optionally CSV), so you can see which releases are in the field without a device.

What leaves the device:

  • a token that changes every UTC day;
  • the hour;
  • the release version;
  • the agent's state and algorithm;
  • a remote-training code (0–4, from the remote-train plugin's remote-training service; never the host's details);
  • today's contest wins and losses;
  • a proof-of-work.

No capture data, network names or locations. tether_only is on by default, so census traffic goes out only over the phone's Bluetooth tether (the socket is bound to bnep0). The plugin pauses while the tether is down.

Plugin:

  • plugins/census.py:
    • declarations, options and the worker:
      • gates: clock synced, online, tether, data budget;
      • a daily rollover with a new token and slot, the proof-of-work found lazily, and the contest baseline carried over;
      • writes once an hour at a random minute, with a check-after-write that rewrites once if the entry is missing;
      • reads every poll_hours;
      • node-list upkeep;
      • pauses cleanly when the link drops mid-cycle;
    • totals and history, the census service, the card and the page.
  • plugins/_census_dht.py: a small read-only DHT client (bencode with caps, BEP 42, a KRPC client with rate limits, node pool, bootstrap, lookups, BEP 44 get and put).
  • plugins/_census_proto.py: the kestrel-census/1 format and the Tally that the plugin and the desktop report share.
  • Bootstrap DNS: the router names are looked up only after the gates have passed (so with tether_only, while the tether is up). Their addresses are saved and tried first on later joins, so the DNS lookup is rare.
  • State: ~pi/.local/state/kestrel-census/ (directory 0700, files 0600), written atomically. A malformed file is rebuilt, never fatal.

Docs:

  • docs/user-guide/census-setup.md;
  • docs/developer/census-protocol.md (the format);
  • a census section in GUIDE_MD;
  • updates to the README plugin table, managing-plugins.md, user-guides.md, architecture.md (versioned interfaces), remote-training.md, plugin-development.md (services table), SECURITY.md (scope), deferred.md (IPv6, a DHT plugin library, the census on the e-ink face) and CLAUDE.md (nineteen plugins, the census, the tools row, the two bootstrap routers on the allowed list).

Tests:

  • 85 tests in test_census.py;
  • 6 tests in test_census_report.py.

All of them run against an in-process fake DHT on RFC 5737 addresses with a fake clock, and none touches the network. They cover the client, the format, what this device reports, every gate, rollover, restart survival, corrupt state, the data budget, link loss mid-write, check-after-write, the DNS order, the totals, the card wording (including the zero case), page escaping, and the report end to end.

Checklist

  • Lint passes — make lint (ansible --syntax-check, yamllint, shellcheck --severity=error, and the real-data check). The Lint workflow runs this on the PR.
  • Tests pass — make test (the kestrel_pi pytest suite and the real-data check's tests). The Test workflow runs this on the PR.
  • Nothing real: no secrets, no personal information, no real data. This is a public repository. The diff contains no real account passwords or hashes, Wi-Fi passphrases, private keys, API tokens, .env files, real names, personal emails, device serials, and no real captured or device data: SSIDs, MAC addresses, GPS coordinates, capture files, diagnostics bundles, logs, or screenshots of a real device — placeholders only. The real-data check catches MACs, emails, keys and capture files; real SSIDs, coordinates and screenshots are on you.
  • Consent gates intact. Capture and network plugins still ship disabled, enabling them still takes the authorized-use acknowledgment (never pre-ticked or given by code), and the capture whitelist is still enforced.
  • Docs updated in the same PR, if this changes something an operator sees or configures: the README package table, the relevant page under docs/, and the on-device operator guide (GUIDE_MD in kestrel_pi/userguide.py) where it applies.
  • Commit messages are short and imperative.

Release impact

  • App-layer-safe — changes only files the in-place update engine carries: the kestrel_pi package, plugins/*.py, systemd units, the journald drop-in, a lib script already in the engine's LIB_SCRIPTS, or the CLI.
  • Reflash-class — touches a build input the engine does not carry onto a running device: apt or pip packages, boot options / config.txt / cmdline, kernel modules, the base image or partition size, builder/config.json, builder/config.yml, the Makefile, .forgejo/workflows/, a shipped config default (config.toml / default.conf), or a brand-new lib script.

No new packages are needed: Ed25519 comes from cryptography, which the image already has. There's no config.toml table and no Makefile change, because the report's tests live under kestrel_pi/tests. The rest of the diff (tools/, the docs, .gitignore) isn't shipped.

Notes for review

  • Where to start:
    1. the options and module docstring in census.py;
    2. _tick, _gate, _write and _read in the same file;
    3. then _census_proto.py.
  • The page uses short lines in cards, not tables. The shared stylesheet turns every table into a stack of cards at phone width, which made this page about 8,000 px tall. Lines in cards halve that without a core CSS change.
  • Desktop report checked live: a two-slot read joined the real DHT and returned "No reports found." (no devices yet).
  • Not tested on a device yet. That's for the beta checklist: enable on a Pi with the tether, confirm the bind to bnep0, the first write 5–30 minutes in, the page, and the pause when the tether drops.
  • The beta's check script will need item 5.5 changed from "eighteen" to "nineteen" plugins.

🤖 Generated with Claude Code

## What this PR does Adds the `census` plugin: an opt-in, anonymous, serverless count of active Kestrels kept in the public BitTorrent DHT (BEP 44 mutable items). Each enabled device adds a 20-byte report once an hour and reads everyone's reports back: - its card shows Kestrels online, active today, and "desktop won X of Y training contests this week"; - its Census page shows releases, learning setups, remote-training states, 7- and 30-day contest results, a 30-day history, and exactly what this device reports. It also adds `tools/census-report/`, a desktop script that reads the same public slots and prints the census as plain tables (optionally CSV), so you can see which releases are in the field without a device. **What leaves the device:** - a token that changes every UTC day; - the hour; - the release version; - the agent's state and algorithm; - a remote-training code (0–4, from the `remote-train` plugin's `remote-training` service; never the host's details); - today's contest wins and losses; - a proof-of-work. No capture data, network names or locations. `tether_only` is on by default, so census traffic goes out only over the phone's Bluetooth tether (the socket is bound to `bnep0`). The plugin pauses while the tether is down. **Plugin:** - `plugins/census.py`: - declarations, options and the worker: - gates: clock synced, online, tether, data budget; - a daily rollover with a new token and slot, the proof-of-work found lazily, and the contest baseline carried over; - writes once an hour at a random minute, with a check-after-write that rewrites once if the entry is missing; - reads every `poll_hours`; - node-list upkeep; - pauses cleanly when the link drops mid-cycle; - totals and history, the `census` service, the card and the page. - `plugins/_census_dht.py`: a small read-only DHT client (bencode with caps, BEP 42, a KRPC client with rate limits, node pool, bootstrap, lookups, BEP 44 get and put). - `plugins/_census_proto.py`: the `kestrel-census/1` format and the `Tally` that the plugin and the desktop report share. - **Bootstrap DNS:** the router names are looked up only after the gates have passed (so with `tether_only`, while the tether is up). Their addresses are saved and tried first on later joins, so the DNS lookup is rare. - **State:** `~pi/.local/state/kestrel-census/` (directory `0700`, files `0600`), written atomically. A malformed file is rebuilt, never fatal. **Docs:** - `docs/user-guide/census-setup.md`; - `docs/developer/census-protocol.md` (the format); - a census section in `GUIDE_MD`; - updates to the README plugin table, `managing-plugins.md`, `user-guides.md`, `architecture.md` (versioned interfaces), `remote-training.md`, `plugin-development.md` (services table), `SECURITY.md` (scope), `deferred.md` (IPv6, a DHT plugin library, the census on the e-ink face) and `CLAUDE.md` (nineteen plugins, the census, the tools row, the two bootstrap routers on the allowed list). **Tests:** - 85 tests in `test_census.py`; - 6 tests in `test_census_report.py`. All of them run against an in-process fake DHT on RFC 5737 addresses with a fake clock, and none touches the network. They cover the client, the format, what this device reports, every gate, rollover, restart survival, corrupt state, the data budget, link loss mid-write, check-after-write, the DNS order, the totals, the card wording (including the zero case), page escaping, and the report end to end. ## Checklist - [x] **Lint passes** — `make lint` (ansible `--syntax-check`, `yamllint`, `shellcheck --severity=error`, and the real-data check). The **Lint** workflow runs this on the PR. - [x] **Tests pass** — `make test` (the `kestrel_pi` pytest suite and the real-data check's tests). The **Test** workflow runs this on the PR. - [x] **Nothing real: no secrets, no personal information, no real data.** This is a public repository. The diff contains no real account passwords or hashes, Wi-Fi passphrases, private keys, API tokens, `.env` files, real names, personal emails, device serials, and no real captured or device data: SSIDs, MAC addresses, GPS coordinates, capture files, diagnostics bundles, logs, or screenshots of a real device — placeholders only. The real-data check catches MACs, emails, keys and capture files; real SSIDs, coordinates and screenshots are on you. - [x] **Consent gates intact.** Capture and network plugins still ship disabled, enabling them still takes the authorized-use acknowledgment (never pre-ticked or given by code), and the capture whitelist is still enforced. - [x] **Docs updated in the same PR**, if this changes something an operator sees or configures: the README package table, the relevant page under `docs/`, and the on-device operator guide (`GUIDE_MD` in `kestrel_pi/userguide.py`) where it applies. - [x] Commit messages are short and imperative. ## Release impact - [x] **App-layer-safe** — changes only files the in-place update engine carries: the `kestrel_pi` package, `plugins/*.py`, systemd units, the journald drop-in, a lib script already in the engine's `LIB_SCRIPTS`, or the CLI. - [ ] **Reflash-class** — touches a build input the engine does not carry onto a running device: apt or pip packages, boot options / `config.txt` / `cmdline`, kernel modules, the base image or partition size, `builder/config.json`, `builder/config.yml`, the `Makefile`, `.forgejo/workflows/`, a shipped config default (`config.toml` / `default.conf`), or a brand-new lib script. No new packages are needed: Ed25519 comes from `cryptography`, which the image already has. There's no `config.toml` table and no Makefile change, because the report's tests live under `kestrel_pi/tests`. The rest of the diff (`tools/`, the docs, `.gitignore`) isn't shipped. ## Notes for review - **Where to start:** 1. the options and module docstring in `census.py`; 2. `_tick`, `_gate`, `_write` and `_read` in the same file; 3. then `_census_proto.py`. - **The page uses short lines in cards, not tables.** The shared stylesheet turns every table into a stack of cards at phone width, which made this page about 8,000 px tall. Lines in cards halve that without a core CSS change. - **Desktop report checked live:** a two-slot read joined the real DHT and returned "No reports found." (no devices yet). - **Not tested on a device yet.** That's for the beta checklist: enable on a Pi with the tether, confirm the bind to `bnep0`, the first write 5–30 minutes in, the page, and the pause when the tether drops. - The beta's check script will need item 5.5 changed from "eighteen" to "nineteen" plugins. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add the kestrel-census/1 format: slots, entries, merge and publish
All checks were successful
Test / test (push) Successful in 28s
Lint / lint (push) Successful in 32s
Test / test (pull_request) Successful in 29s
Lint / lint (pull_request) Successful in 33s
cc2ea99869
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Document the census plugin, its format and the desktop report
All checks were successful
Test / test (push) Successful in 31s
Lint / lint (push) Successful in 32s
Test / test (pull_request) Successful in 30s
Lint / lint (pull_request) Successful in 33s
1e74db2ab9
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
aluminum-ice changed title from WIP: Add the census plugin to Add the census plugin 2026-11-01 00:58:29 +00:00
aluminum-ice deleted branch feature/census-plugin 2026-11-01 01:04:50 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
aluminum-ice/kestrel!130
No description provided.