Skip to content

impl(047): one implementation of exp and ln on every target - #139

Merged
bartekus merged 1 commit into
mainfrom
impl/047-transcendentals
Oct 21, 2026
Merged

bartekus merged 1 commit into
mainfrom
impl/047-transcendentals

Conversation

@bartekus

@bartekus bartekus commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Implements spec 047 (ratified in #138, A-44): one implementation of exp and ln on every target.

  • crates/rustev-core/src/math.rs (new, public): exp and ln wrap libm::exp and libm::log. libm is a workspace dependency of core only, exact-pinned =0.2.16 with default-features = false, so it takes no architecture-specific path. Unit tests pin special values and three results' bit patterns, so a pin change that moves a bit fails here before the corpus runs.
  • The four call sites use it: softmax (kinds.rs), the log of a distribution mass (calibrate.rs), log loss (rustev-eval metrics.rs) and the temperature fit (fit.rs). Evaluation's normal dependencies are unchanged (contract, core, serde), so its dependency allowlist test passes unmodified.
  • crates/rustev-core/tests/platform_math.rs: scans the sources of contract, core, evaluation and the surface and fails on a call of any f64 platform math method (the list of 047 3.3, method and path forms) outside math.rs, comments excluded; a second test shows the scan finds each seeded form.
  • tools/rustev-release/archive-policy.json: core's new src/math.rs joins its reviewed archive list.
  • Spec 047: extends edges for the code it touches, implementation record, implementation: complete.

Every existing test and golden passes unchanged; none re-blessed.

The ratification (#138) has merged; this PR is rebased onto main.

Local verification (macOS arm64, after cargo clean -p of contract, core, eval, surface, cli and runtime): cargo test --workspace --no-fail-fast (no failure), cargo clippy --workspace --all-targets -D warnings, cargo fmt --check, make boundaries, make gate, bash scripts/check-authored-content.sh.

@github-actions

Copy link
Copy Markdown

AI review (no-findings)

The PR introduces a libm-backed math module to guarantee bit-identical exp/ln results across targets (spec 047). I reviewed all changed files.

crates/rustev-core/src/math.rs -- ln correctly calls libm::log (natural log in libm). Special-value and known-bits tests cover the pinned version. Module is pub, which is required for cross-crate use by rustev-eval.

crates/rustev-core/tests/platform_math.rs -- The scanner strips comments with .split("//").next() and uses code.contains(&format!(".{m}(")). I verified there's no false-positive from .expect( since .exp( requires the ( immediately after p, while .expect( has e next. crate::math::exp( and rustev_core::math::ln( both use :: not ., so they don't match the patterns. The ends_with("rustev-core/src/math.rs") exclusion works correctly via Path::ends_with component matching. The two self-test cases (the_scan_sees_a_direct_call) verify sensitivity and the comment-strip logic.

Call site migrations -- all four sites (calibrate.rs, kinds.rs, fit.rs, metrics.rs) are mechanically correct. The softmax and log-loss semantics are unchanged.

Cargo.toml -- libm = { version = "=0.2.16", default-features = false } is exact-pinned as required. Only rustev-core takes the dep.

tools/rustev-release/archive-policy.json -- src/math.rs correctly inserted in alphabetical order.

Spec file -- implementation: complete, extends entries correctly cover all changed units.

No bugs, security issues, or inconsistencies found.

{"head": "d72d5c79f71d63e7e9480f215b17589f164296d4", "verdict": "no-findings", "findings": []}

Subject: 913c0b6...d72d5c7, diff digest ee312c330bf2554c2450cd335c44446502a9c88764df9bfc63c29896596c98d7, 1 call(s). An AI comment is not an approval.

Base automatically changed from spec/047-transcendentals to main October 21, 2026 13:42
rustev_core::math::{exp, ln} wrap libm =0.2.16 (default features off); the softmax, calibration, log-loss and temperature-fit call sites use them, and a core test refuses a direct platform math call in the pure crates.
@bartekus
bartekus force-pushed the impl/047-transcendentals branch from d72d5c7 to 7eb2a03 Compare October 21, 2026 13:44
@github-actions

Copy link
Copy Markdown

AI review (no-findings)

The PR introduces rustev_core::math::{exp, ln} as target-independent wrappers around libm (exact-pinned at 0.2.16), replacing direct f64::exp/f64::ln calls at four sites, and adds a source scanner to enforce the policy going forward.

Observations checked:

  1. libm pin and default-features = false (Cargo.toml:38-40): Version =0.2.16 with default-features = false correctly forces the pure-Rust path on all targets. No architecture-specific code path is taken.

  2. math.rs wrapping (crates/rustev-core/src/math.rs:13-19): libm::log is the natural logarithm (ln); naming it ln in the wrapper is correct. The comment explains the rationale and spec references clearly.

  3. Call-site substitutions: All four substitutions (calibrate.rs:155, kinds.rs:111, fit.rs:226, metrics.rs:221) are mechanically correct one-for-one replacements.

  4. Scanner false-negative in offences() (tests/platform_math.rs:38-48): Comment stripping via line.split("//").next() would incorrectly strip code following // in a string literal (e.g., a URL). In practice this codebase has no such patterns in the scanned crates, so it's a theoretical gap, not a current defect.

  5. Scanner pattern specificity: .exp( vs .expensive( — the parenthesis suffix avoids identifier prefix matches. ✓

  6. ends_with on PathBuf (tests/platform_math.rs:55): Path::ends_with compares path components, so "rustev-core/src/math.rs" correctly matches the three trailing components. ✓

  7. Module is pub (lib.rs:22): Required because rustev-eval is a separate crate calling rustev_core::math::ln. Correct.

  8. known_bits test pins libm 0.2.16 bit patterns. The ln(0.5) bits 0xbfe62e42fefa39ef correspond to the well-known IEEE 754 representation of −ln(2), which is consistent.

  9. archive-policy.json: src/math.rs added to the reviewed file list alongside the existing core sources. ✓

No bugs, security issues, or spec inconsistencies found.

{"head": "7eb2a0387aa95e1d74a254254dc69fb80f3f704f", "verdict": "no-findings", "findings": []}

Subject: 29d9bed...7eb2a03, diff digest ee312c330bf2554c2450cd335c44446502a9c88764df9bfc63c29896596c98d7, 1 call(s). An AI comment is not an approval.

@bartekus
bartekus enabled auto-merge (squash) October 21, 2026 13:47
@bartekus
bartekus merged commit b2c5c6d into main Oct 21, 2026
7 checks passed
@bartekus
bartekus deleted the impl/047-transcendentals branch October 21, 2026 14:02
bartekus added a commit that referenced this pull request Oct 21, 2026
Spec 027 increment 7, first change: the native package's platform
packages (3.13.1, 3.13.2).

- `integrations/rustev-node/npm/<platform>/package.json` for the seven
platforms the main package's `optionalDependencies` name
(`linux-x64-gnu`, `linux-x64-musl`, `linux-arm64-gnu`,
`linux-arm64-musl`, `darwin-arm64`, `darwin-x64`, `win32-x64-msvc`):
`@statecrafting/rustev-<platform>` at the main package's version,
license and engines; `os`, `cpu` and, on Linux, `libc` as the name says;
`main` and `files` the addon `rustev.<platform>.node` alone; no scripts;
`private` until spec 031's release.
- `tools/rustev-conformance/js/platform-packages.mjs`:
- `check`: every optional dependency has its package and they agree as
above; no stray platform directory; no script in a platform package and
no install-time script (`preinstall`, `install`, `postinstall`,
`prepare`) in the main package;
- `stage <target>`: copies this host's release addon into its platform
package;
- `fresh <dir>`: `npm pack`s the main package and this host's platform
package and installs both into a fresh project from the tarballs
(`--omit=optional --ignore-scripts`), so nothing is fetched for another
platform, and fails if the main package's tarball carries an addon.
- `copy-addon.mjs` exports the addon naming it already had (`addonName`,
`builtAddon`) for that reuse; its command line is unchanged.
- `make bindings` runs `check`, `stage`, `fresh`, then the native
conformance suite on the fresh install, so the suite exercises the
loader's platform-package path (3.13.2), not only the local build.
- Spec 027: implementation record.

Building and smoke-testing each of the seven targets in CI is a later
change of this increment (the `bindings` job).

Local verification (macOS arm64, clean build of the changed crates):
`make bindings` passes end to end (`npm audit signatures`, WebAssembly
conformance 23/23, 27 native tests, native conformance 34/34 on the
local build and 34/34 on the fresh install); `make gate`; `bash
scripts/check-authored-content.sh`.

Review round 1 (3 findings, fixed):
1. `check` refused any `scripts` on the main package. 3.13.2 forbids
install-time code, so it now refuses only npm's install lifecycle
scripts there (`preinstall`, `install`, `postinstall`, `prepare`); a
platform package still has no script at all. Seeded locally: a `build`
script on the main package passes, a `postinstall` fails naming it.
2. `stage` and `fresh` let `addonName()`'s throw on an unsupported
platform escape as a stack trace; both now go through `hostPlatform()`,
which exits with the message.
3. npm ran with `shell: true` on Windows, so cmd.exe parsed the paths.
npm now runs without a shell everywhere: on Windows its entry point
(`node_modules/npm/bin/npm-cli.js` beside `node.exe`) runs under the
current Node, and a missing one fails by name.

Review round 2 (2 findings, fixed):
1. `fresh` deleted its target directory unconditionally. It now writes a
marker (`.rustev-fresh`) into the directory it makes and replaces only a
directory carrying it; any other existing path is refused by name and
left alone (seeded locally: a scratch directory holding a file was
refused and kept). In `make bindings` the path is under the cargo target
directory, which the target already refuses to be empty.
2. `pack` joined npm's reported file name unchecked. A response that is
not JSON, or a name that is not one plain `.tgz` file name, or a tarball
that is not there, now fails with a message naming the package.

Review round 3 (1 finding, answered in the diff, no behavior change):
`addonName()` skips `excludeNetwork` when `process.report` has no such
own property. That is deliberate and unchanged from #135's loader: only
a Node that supports leaving the network section out exposes the
property, and assigning it elsewhere would add a plain property the
report never reads. The comment at the call now says so; the report then
runs whole, slower but correct.

Rebased onto `main` after #138 and #139 merged. Local `make bindings` on
the rebased branch, after `cargo clean -p` of every crate #139 changed:
WebAssembly conformance 23/23, 27 native tests, native conformance
34/34; its `fresh` step refused a `native-fresh` directory left by a run
from before the round-2 marker existed (the guard working as intended).
With that directory removed, the fresh install's suite passed 34/34.

Review round 4 (2 findings, fixed):
1. `builtAddon()` produced a path ending in `.undefined` on an
unsupported platform; it now throws naming the platform, and `stage`
reports it cleanly.
2. `check` counted any entry under `npm/` as a platform directory, so
macOS's `.DS_Store` failed it; only non-hidden directories count now
(seeded locally with a `.DS_Store`: `check` passes).
bartekus added a commit that referenced this pull request Oct 21, 2026
Spec 027 increment 7, third change: the differential floating-point
corpus (3.11.3, 3.11.5).

After spec 047 (#138, #139) every transcendental call reachable from a
pure command is a call of `rustev_core::math`, at four sites: `exp` in
core's softmax, `ln` in core's calibration, `ln` in evaluation's log
loss, `ln` in its temperature fit.

- `tools/rustev-conformance/fp-corpus/` (new crate `rustev-fp-corpus`,
`publish = false`, excluded from the release set as tooling; an explicit
workspace member, since it sits inside the conformance suite's
directory):
- 131,072 inputs per site, drawn as each site sees them (non-positive
`exp` arguments; positive finite masses; probabilities in (0, 1] and
just above). Each set starts with boundary values: zeros, the smallest
and largest subnormals, `MIN_POSITIVE`, one ulp either side of 1, where
`exp` leaves the normal range and where it rounds to 0, and the format's
extremes. It continues with a seeded SplitMix64 using integer arithmetic
and correctly rounded basic operations only, so every target draws the
same bits.
- Per site, the SHA-256 of the input bits and of the output bits, both
committed in `expected.json`.
- A native runner (`rustev-fp-corpus check expected.json`) and a
WebAssembly build of the same crate with plain exports and no imports
(`corpus_sites`, `corpus_run`, `corpus_digest_byte`). The exports need
`#[unsafe(no_mangle)]`, so the crate relaxes `unsafe_code` to `deny` and
allows it on those three functions only; there is no `unsafe` block.
- Tests: every `rustev_core::math` call in the pure crates is a corpus
site and vice versa (a new call site cannot go unmeasured); the inputs
meet 3.11.3 (at least 100,000, no NaN, subnormal and extreme magnitudes,
distinct draws for the two evaluation sites); this target reproduces the
committed digests.
- `tools/rustev-conformance/js/fp-corpus.mjs`: the WebAssembly module in
Node and in headless Chromium against the committed digests, writing a
result document.
- `make bindings`: the corpus natively, then in Node and Chromium.
- Release set: `rustev-fp-corpus` excluded with its reason.
- Spec 027: implementation record.

Local verification (macOS arm64, before the rebase onto `main`): native,
Node 24.6.0 and Chromium 153.0.8010.12 agree on all four sites; `make
bindings` passes end to end with the corpus steps; `cargo test -p
rustev-fp-corpus` (3 tests); clippy `-D warnings`; `cargo test -p
rustev-release` with the new exclusion. Seeded check: putting the
platform's `exp` and `ln` back into `rustev_core::math` makes the native
macOS run differ from the committed digests and fail, while WebAssembly
(whose standard library already uses the pure implementation) still
matches. That is the divergence spec 047 removed. After the rebase:
`make gate`, `bash scripts/check-authored-content.sh`. The results on
every CI target come with the `bindings` job, the next PR.

Rebased onto `main` after #142 merged. One conflict, in
`tools/rustev-release/release-set.json`, resolved by hand: #142 removed
lodging's exclusion (lodging became a member), and this PR adds
`rustev-fp-corpus`'s, so the result keeps the new exclusion and not
lodging's.

Review round 1 (1 finding, fixed): `corpus_digest_byte`'s out-of-range
sentinel `u32::MAX` reaches JavaScript as -1 (a wasm `i32` result is
signed), so `b > 255` never caught it. The runner now accepts only
integers 0 to 255 and throws naming the value otherwise; the Rust doc
comment states the -1. Checked locally against the module:
`corpus_digest_byte(5, 0)` and `(0, 99)` return -1, which the new guard
refuses.

Review round 2 (2 findings, fixed):
1. `check` compared the committed file byte for byte, so a CRLF checkout
(Windows with `core.autocrlf`) would fail with correct digests; the
committed text is now compared with carriage returns removed.
2. The call-site guard stripped only `//` comments; it now scans code
alone, with line, block (nested) comments and string and character
literals blanked (`code_only`, with its own test), counts every
occurrence rather than one per line, and compares `/`-separated paths on
every host.

Local: `cargo test -p rustev-fp-corpus` 4/4, clippy `-D warnings`.

Review round 3 (1 finding, fixed): `code_only` did not blank raw string
literals. It now recognizes `r"..."`, `r#"..."#` with any number of `#`,
and the `br` forms (not when `r` ends an identifier), and the scanner
test includes a raw string and a raw byte string holding `math::exp(`
and `math::ln(`, which are not counted. Local: `cargo test -p
rustev-fp-corpus` 4/4, clippy `-D warnings`.

Rebased onto `main` after #146 (profile revision 17, spec-spine 0.31.0):
derived shards regenerated with spec-spine 0.31.0; no other conflict.
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.

1 participant