Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,57 @@ jobs:
- name: The snapshots match SHA256SUMS
run: python -m spacevalue quotes check

# rust/: the knapsack's Rust copy, which economicspace's es_core links. A
# knapsack change made only in cargo.py fails here, not on whichever machine
# happens to have the binding built. The planted build (one ulp in every
# take) must fail all six Rust tests, or the tests could not see a defect;
# SPACEVALUE_REQUIRE_RUST=1 turns their skip-when-missing into a failure.
# The good binding is installed the way a consumer installs it, `pip
# install` of rust/cargo_py, which builds cargo_core by its relative path.
rust:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: ["3.13", "3.14"]
env:
SPACEVALUE_REQUIRE_RUST: "1"
steps:
- uses: actions/checkout@v5
- uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python-version }}
- name: Install
shell: bash
run: python -m pip install -e ".[test]" maturin==1.9.6
# No `cargo test`: cargo_core has no Rust tests (it is tested only
# against cargo.py, through the binding), and a step that passes on zero
# tests would read as coverage it is not.
- name: The planted build fails every Rust test
shell: bash
run: |
python -m maturin build --release --features planted_knapsack_defect -m rust/cargo_py/Cargo.toml -o dist-plant
python -m pip install --force-reinstall --no-deps dist-plant/spacevalue_cargo_rs-*.whl
python -c "import spacevalue_cargo_rs as r; assert r.planted_defect()"
set +e
python -m pytest -q -p no:cacheprovider tests/test_cargo_rust.py > plant.log 2>&1
rc=$?
set -e
tail -3 plant.log
[ $rc -ne 0 ] || { echo "the planted build PASSED the Rust tests"; exit 1; }
grep -Eq "^6 failed" plant.log || { echo "expected all 6 Rust tests to fail"; exit 1; }
- name: The good build, installed as a consumer installs it
shell: bash
run: |
python -m pip install --force-reinstall --no-deps ./rust/cargo_py
python -c "import spacevalue_cargo_rs as r; assert not r.planted_defect()"
- name: The Rust tests, then the whole suite with the binding installed
shell: bash
run: |
python -m pytest -q -p no:cacheprovider tests/test_cargo_rust.py
python -m pytest -q

# The editable install above reads the tree, so it cannot see a snapshot or
# a module missing from the wheel. This installs the built wheel and uses
# it from a directory where the source is not importable.
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ Thumbs.db

# Default build output
spacevalue_data/
# a library crate: its lock is regenerated by `cargo test` (cargo_py commits its own)
rust/cargo_core/Cargo.lock
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,29 @@ code.

## Package releases

### 0.7.1 - 2026-11-17

Data contract 1.21.0, unchanged, and **no number moves**: nothing under
`spacevalue/` changed except `__version__`. This adds `rust/`, a Rust copy of
`cargo.py`'s knapsack (`optimal_payload_mix`, `_hold_refining`,
`_capped_sale_value`) for economicspace's `es_core` to link, and an optional
binding of it, `spacevalue_cargo_rs`. `spacevalue` never imports either.

- `rust/cargo_core` (plain Rust) is the port; `rust/cargo_py` (PyO3) binds
it. The binding is a separate package, installed from this tag with
`pip install "spacevalue_cargo_rs @ git+https://github.com/loggger101/spacevalue@v0.7.1#subdirectory=rust/cargo_py"`
(it needs a Rust toolchain). It is not a `spacevalue` extra, which would
make every install of the package need one.
- `tests/test_cargo_rust.py` holds the binding to the Python bit for bit
(the golden replay, and fuzzed calls with exceptions compared); it skips
without the binding unless `SPACEVALUE_REQUIRE_RUST=1`.
- CI's new `rust` job, on the three hosts and Python 3.13 and 3.14: the
planted build (`planted_knapsack_defect`) failing all six Rust tests, the
good binding installed as a consumer installs it, and the whole suite with
it. A knapsack change made only in `cargo.py` now fails here. There is no
`cargo test` step: `cargo_core` has no Rust tests, only the comparison with
`cargo.py` through the binding.

### 0.7.0 - 2026-11-12

Data contract **1.21.0**, above: spacecost is pinned at `v0.17.1`, was
Expand Down
91 changes: 91 additions & 0 deletions rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# rust/: the cargo knapsack in Rust (prototype)

**Status:** shipped with spacevalue 0.7.1, tested by CI's `rust` job, and
still optional: nothing in `spacevalue` imports it. The Python in
`spacevalue/cargo.py` stays the reference and the default. Install the
binding from the tag (it needs a Rust toolchain):

pip install "spacevalue_cargo_rs @ git+https://github.com/loggger101/spacevalue@v0.7.1#subdirectory=rust/cargo_py"

## What it is

Two crates:

| crate | is | used by |
|---|---|---|
| `cargo_core` | `optimal_payload_mix`, `_hold_refining` and `_capped_sale_value`, ported operation for operation from `cargo.py`; plain Rust, no Python | `cargo_py` here, and economicspace's `es_core`, which links it directly |
| `cargo_py` | an optional PyO3 module, `spacevalue_cargo_rs`, with the same three names and signatures | `tests/test_cargo_rust.py` |

The point of keeping the port here rather than in economicspace: the copy
lives next to the code it copies and is tested against it by the same suite,
so a change to the knapsack that is not made in both places fails here,
before a release, not later in economicspace.

## The contract

The golden test's: the same result bit for bit, and the same arguments
afterwards (`caps` is consumed in place). What a port gets wrong, and how
this one copies the Python:

- `sorted(..., key=lambda p: -p[2])` is a stable sort on the negated price.
Any stable sort agrees with timsort when no price is NaN, so a NaN price is
handed back to the Python.
- Python's `min(a, b)` keeps `a` unless `b < a`; `max(items, key=...)` keeps
the first item unless a later key is strictly greater.
- `mix_kg` is a dict: a name keeps the position of its first insertion.
- `caps` is spent on a local copy and written back only on success, so a call
handed to the Python finds the caller's dict untouched.
- Where the Python raises (an infinite payload from which nothing was taken
ends in `max()` of an empty mix), the Python runs, so the same exception
surfaces.

The binding hands a call to the Python, untouched, whenever it cannot read
the inputs exactly: a non-float where a float goes, an int past 2**53, a caps
value that is neither float nor None, a NaN in the answer. `counts()` says how
many calls each side answered.

## Measured (2026-11-15, Windows, CPython 3.14)

| check | result |
|---|---|
| golden replay, `optimal_payload_mix` / `_hold_refining` / `_capped_sale_value` | 256 / 416 / 67 recorded calls, 0 differ |
| fuzz against the Python | 60,000 + 30,000 + 30,000 calls, 0 differ (exceptions compared too) |
| plant (`planted_knapsack_defect`: one ulp in every take and in the capped sale's value) | all six tests fail |
| the whole suite with the binding installed | 189 passed |
| per call, through the Python boundary | 1.9x / 1.3x / 3.7x faster: about 1 to 3.5 us saved per call |

Called from Python, the binding is worth little: the boundary costs about as
much as the walk. The gain is in `es_core`, where the sizing loop calls
`cargo_core` with no boundary at all (economicspace's
`rust/es_core/README.md` has those numbers).

## Build and test

Build outside OneDrive (inside it, build scripts fail with "output path is
not a writable directory"):

export CARGO_TARGET_DIR="$HOME/.cargo-target/spacevalue_cargo"
cd rust/cargo_py
python -m maturin build --release -o dist
python -m pip install --force-reinstall --no-deps dist/spacevalue_cargo_rs-*.whl
cd ../..
SPACEVALUE_REQUIRE_RUST=1 python -m pytest tests/test_cargo_rust.py

Without the module, `test_cargo_rust.py` skips; `SPACEVALUE_REQUIRE_RUST=1`
turns that skip into a failure, and the gate always sets it, so the check
cannot pass by not running. The planted build, which must fail all six:

python -m maturin build --release --features planted_knapsack_defect -o dist-plant

## Open

- (Done in 0.7.1.) CI: the `rust` job in `tests.yml` builds the binding on
the three hosts and runs `test_cargo_rust.py` with
`SPACEVALUE_REQUIRE_RUST=1`, after the planted copy has failed all six.
- Decide how economicspace's `es_core` pins `cargo_core`: a git dependency at
the same tag as the spacevalue pin, held together by a check, as check 7
holds the Python pins.
- Decide whether `spacevalue.cargo` should ever dispatch to the binding
itself. The per-call numbers above say probably not.
- A change to the knapsack is then a change to both copies, and the golden
fixture re-recorded only with the calc release that moves it, as now.
14 changes: 14 additions & 0 deletions rust/cargo_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "spacevalue_cargo_core"
version = "0.1.0"
edition = "2021"
description = "PROTOTYPE: spacevalue.cargo's payload knapsack in Rust, bit-identical to the Python"
license = "MIT"
publish = false

[lib]
path = "src/lib.rs"

[features]
# Flips one ulp of every positive take: the gates must catch it.
planted_knapsack_defect = []
Loading
Loading