Skip to content

feat(rust): add the anthias_native extension and its wheel pipeline - #3536

Open
vpetersson-bot wants to merge 3 commits into
masterfrom
agent/big-claude/4abc035eff50
Open

vpetersson-bot wants to merge 3 commits into
masterfrom
agent/big-claude/4abc035eff50

Conversation

@vpetersson-bot

Copy link
Copy Markdown
Contributor

Issues Fixed

Closes #3520. This is the pilot that #3521, #3522 and #3523 build on.

Description

This adds anthias_native, a Rust extension module (PyO3 0.30 + maturin, abi3 for CPython 3.13+, so one wheel per architecture), plus the pipeline that builds it for every board. Django stays the application. Each Python module stays the public interface and calls into Rust, and its Python implementation stays alongside as a fallback.

Pilot function: the backup member name checks. Those are the runtime whitelist, absolute paths, and .. or empty components that _safe_tar_member applies to every member of an uploaded backup. I picked these over ifaddrs.parse_dump because the host agent runs from a host venv rather than an image, so a pilot there wouldn't prove the image pipeline, and #3523 replaces that agent with a static binary anyway. The backup checks run in anthias-server/anthias-celery on every board, and #3521 extends them.

  • src/anthias_server/lib/backup_paths.py holds the Python implementation (moved out of backup_helper.py unchanged).
  • rust/anthias_native/src/backup.rs holds the Rust port, with cargo test tests. Bindings are in lib.rs. Names that aren't valid UTF-8 (lone surrogates from tarfile) are converted lossily, which gives the same answer because the checks only compare ASCII.
  • backup_helper uses Rust unless ANTHIAS_NATIVE_DISABLE=backup (or all) is set (anthias_common/native.py). The realpath confinement and the member-type checks stay in Python because they need the filesystem and the TarInfo.

Tests against both implementations

  • An autouse parametrised fixture runs every test in tests/test_backup_helper.py against both Rust and Python.
  • tests/test_native_backup_paths.py compares the two over about 13k generated names, covering traversal, backslashes, doubled separators, non-ASCII, surrogates and NUL. It also covers the switch and checks that the abi3 build is what gets loaded.

Images: no Rust under QEMU. A new native-builder stage (docker/native-builder.j2) runs on $BUILDPLATFORM and cross-compiles the board's wheel with maturin --zig. uv-builder syncs with --no-install-package anthias-native and installs that wheel into /venv. The stage is only added for the server and test uv groups; the viewer image is unchanged.

Local dev: uv sync --group test builds the crate from source through a uv path dependency. uv cache-keys make it rebuild when .rs or Cargo.toml files change.

CI (.github/workflows/rust.yaml)

  • Runs cargo fmt --check, clippy -D warnings, cargo test and cargo deny (licences, advisories, sources).
  • Builds the x86_64, aarch64 and armv7 wheels in parallel, cross-compiled on x86 runners.
  • Checks each wheel with rust/scripts/check_wheel.py: natively on x86_64, under QEMU in Debian trixie's Python 3.13 for the ARM wheels (only the import and the check run under QEMU; nothing is compiled there).
  • Uploads the wheels as artifacts.

Dependabot now covers cargo too.

Docs: there is a new "Rust extension" section in the developer documentation (building locally, CI checks, how it ships, the fallback switch, and how to move the next module). rust/README.md and CLAUDE.md point to it.

Measurements

These were taken on an x86_64 build host with QEMU for armv7, same as CI. Both sides were built --no-cache.

pi2 (armv7) server image, uv-builder target master this PR
Wall time 1129 s 1098 s

The difference is within noise. The native-builder stage takes about 45 s (rustup target add plus maturin), runs natively, and overlaps with uv-builder's 761 s uv sync. The only serial addition is copying and installing the wheel, about 4 s. The wheels are about 350 KB, link only against glibc (2.28 symbol floor), and import in the built armv7 venv.

check_wheel.py per-call timings. The x86_64 numbers are native; the ARM numbers come from QEMU, so they are not representative of real hardware:

Python Rust
x86_64 is_safe_member_name 0.39 µs 0.16 µs
armv7 (QEMU) 4.51 µs 2.14 µs
aarch64 (QEMU) 7.20 µs 3.12 µs

The pilot was chosen for the pipeline, not for speed; these are per-name checks on a restore.

Still open from the acceptance criteria: verifying the cross-compiled armv7 wheel on real Pi 2/3 hardware. That is being done on the device rig with the CI artifact and rust/scripts/check_wheel.py, and the results will be added here.

Local verification

  • cargo fmt --check, clippy --all-features -D warnings, cargo test --no-default-features (5 tests) and cargo deny check all pass.
  • ruff check, ruff format --check, mypy . (strict) and actionlint all pass.
  • Docker test image: 3454 unit tests passed, coverage 91% (backup_paths.py and native.py at 100%); 73 integration tests passed, 2 skipped.
  • The full x86 server image builds, and anthias_native imports from /venv.
  • Under QEMU, the armv7 and aarch64 wheels pass check_wheel.py in a debian:trixie venv.

Checklist

  • I have performed a self-review of my own code.
  • New and existing unit tests pass locally and on CI with my changes.
  • I have done an end-to-end test for Raspberry Pi devices. (Hardware run pending, see above.)
  • I have tested my changes for x86 devices.
  • I added a documentation for the changes I have made (when necessary).

🤖 Generated with Claude Code

Add a Rust extension module, anthias_native (PyO3 + maturin, abi3 for
CPython 3.13+), so parts of Anthias can move to Rust one module at a
time while Django stays the application.

The pilot is the backup member name checks (runtime whitelist, absolute
paths, '..' and empty components) that guard restoring an uploaded
archive. The Python code moves to lib/backup_paths.py and stays as the
fallback; backup_helper uses the Rust version unless
ANTHIAS_NATIVE_DISABLE=backup (or all) is set. The whole backup test
suite runs against both, plus a parity test over ~13k generated names.

Images: a native-builder stage runs on the build platform and
cross-compiles the wheel for the board with zig, so no Rust is compiled
under QEMU; uv-builder installs it into /venv. Locally, uv sync builds
the crate from rust/anthias_native.

CI: a Rust workflow runs fmt, clippy, cargo test and cargo deny, builds
the x86_64/aarch64/armv7 wheels, checks each (natively or under QEMU on
Debian trixie's Python) with rust/scripts/check_wheel.py, and uploads
them as artifacts.

Refs #3520

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
@vpetersson-bot
vpetersson-bot requested a review from a team as a code owner November 30, 2026 09:19
Copilot AI lite review requested due to automatic review settings November 30, 2026 09:19

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error: Your billing is not configured or you have Copilot licenses from multiple standalone organizations or enterprises. To use premium requests, select a billing entity via the GitHub site, under Settings > Copilot > Features.

- pyo3's build script reads python3 on PATH and refuses anything older
  than the abi3-py313 floor; the runner's own python3 is 3.12, so the
  clippy step failed before linting. Set up 3.13 in the checks job.
- Check wheels by unpacking them onto PYTHONPATH instead of installing
  them with pip/uv: same files the images get, nothing resolved.
- Apply the dual-implementation fixture through usefixtures instead of
  autouse with params.
- Name the Docker platform strings in the image builder once.

Refs #3520

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
Copilot AI lite review requested due to automatic review settings November 30, 2026 09:43

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error: Your billing is not configured or you have Copilot licenses from multiple standalone organizations or enterprises. To use premium requests, select a billing entity via the GitHub site, under Settings > Copilot > Features.

Refs #3520

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
Copilot AI lite review requested due to automatic review settings November 30, 2026 09:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error: Your billing is not configured or you have Copilot licenses from multiple standalone organizations or enterprises. To use premium requests, select a billing entity via the GitHub site, under Settings > Copilot > Features.

@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rust: add a native extension module and wheel pipeline (pilot)

2 participants