Skip to content

fix: precision-correctness audit findings (F6/F11/F2/F1)#74

Merged
yilibinbin merged 3 commits into
mainfrom
fix/audit-r2-precision
Jul 2, 2026
Merged

fix: precision-correctness audit findings (F6/F11/F2/F1)#74
yilibinbin merged 3 commits into
mainfrom
fix/audit-r2-precision

Conversation

@yilibinbin

Copy link
Copy Markdown
Owner

Audit R2 — PR2/5: precision correctness (F6/F11/F2/F1)

F6 — CLI batch dropped precision

cli/main._run_calc ran the accelerator and serialized mpf outside any precision_guard, truncating high-precision results to ambient mp.dps. Now wraps compute + serialization in precision_guard(job.precision); mpf serialized via mp.nstr(value, job.precision).

F11 — workspace common/latex config captured but never restored

mpmath precision, uncertainty/display digits, scientific flag, LaTeX input digits/group size/dcolumn/output path — plus caption text and TeX engine — were saved but silently reset to defaults on reload. _restore_common_config now restores all of them.

F2 — MCMC gaussian log-probability

Extracted _gaussian_log_probability and use it for both weighted/unweighted paths.

F1 — covariance negative-variance guard

_error_from_variance maps negative/NaN/Inf variance to mp.nan and flags has_negative_variance in the covariance warning.

Test plan

  • CLI high-precision limit test (mp.dps restore leak fixed too); common/latex round-trip incl. caption+engine (RED without the restore fix); MCMC log-prob + covariance-guard tests.
  • Full suite green (3714 passed) incl. mypy --strict core-layer gate; ruff clean.

🤖 Generated with Claude Code

Aaronhaohao and others added 3 commits July 1, 2026 19:05
F6 (cli/main.py) — CLI batch `calc` serialized the extrapolation limit with bare
str(mpf) outside any precision_guard, truncating a requested high-precision (e.g.
50-digit) result to the ambient mp.dps (~15). Wrap compute + serialization in
precision_guard(job.precision) and render via mp.nstr at that precision.

F11 (app_desktop/workspace_controller.py) — workspace save persisted config.common
(mpmath_precision, uncertainty/display digits, scientific) and config.latex (input
digits, group size, dcolumn) but restore never read them back, silently resetting
compute-affecting precision to defaults on reload. Add _restore_common_config,
called from _restore_workspace_contents.

F2 (datalab_core/mcmc_refine.py) — the Gaussian log-probability divided the
already sigma-weighted residual sum (Σ rᵢ²/σᵢ²) by rmse² again, double-scaling the
likelihood and corrupting credible-interval WIDTHS for any weighted fit. Extract
_gaussian_log_probability: -0.5·residuals_sq when weighted (weights already 1/σ²),
keep the /rmse² plug-in variance only in the unweighted branch.

F1 (fitting/hp_fitter.py) — _compute_covariance took mp.sqrt of a covariance
diagonal guarded only for NaN, so a negative diagonal (finite-precision indefinite
inverse) yielded a complex mpc that crashed combine_error_components. Add
_error_from_variance (negative/NaN/Inf → NaN) and flag negative variances in the
covariance warning, so the fit degrades gracefully instead of crashing.

Each fix is TDD'd (pure-function unit tests + workspace round-trip + CLI calc
high-precision). Neighborhood regression green (218 tests).
… leak in test

Review follow-ups on the F11 precision/round-trip fix:

- workspace_controller._restore_common_config captured config.latex.caption and
  config.latex.engine at save (lines 1125-1126) but never read them back, so a
  saved caption reset to empty and the TeX engine reverted to the "tectonic"
  default on reload — the same "captured but never restored" class F11 set out
  to fix, just incomplete for these two fields. Restore both, and extend the
  round-trip test to set and assert them (it previously only covered the
  precision/digit fields, so the gap was uncaught).

- test_cli_batch set mp.dps = 15 to simulate ambient CLI precision but never
  restored it; since mp.dps is process-global that leaked into later tests.
  Wrap the mutation in try/finally.

ruff clean; 148 workspace/cli tests pass. Round-trip test is RED without the
restore fix (caption not preserved), GREEN with it.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…ct narrows

The F2 refactor hoisted the None-check into a `weighted` boolean and then indexed
likelihood_weights[index] guarded by `if weighted`. mypy --strict cannot use a
separate local flag to narrow `Sequence[Any] | None`, so it reported
"Value of type 'Sequence[Any] | None' is not indexable", failing the core-layer
strict gate (test_mypy_strict_zero_errors_on_full_core_layer) — main passed it.
Index against the direct `if likelihood_weights is not None` instead (mypy
narrows it); keep the `weighted` flag for the _gaussian_log_probability call.

Behavior is identical (weighted is True exactly when likelihood_weights is not
None). mypy strict gate + MCMC log-prob tests pass; ruff clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@yilibinbin, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 59 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: fef27c18-cb2d-40ee-914a-cad15b602c87

📥 Commits

Reviewing files that changed from the base of the PR and between 2eabd8c and 72a5057.

📒 Files selected for processing (8)
  • app_desktop/workspace_controller.py
  • cli/main.py
  • datalab_core/mcmc_refine.py
  • fitting/hp_fitter.py
  • tests/test_cli_batch.py
  • tests/test_hp_fitter_covariance_guard.py
  • tests/test_mcmc_log_probability.py
  • tests/test_workspace_controller.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/audit-r2-precision

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@yilibinbin yilibinbin merged commit baace01 into main Jul 2, 2026
6 checks passed
@yilibinbin yilibinbin deleted the fix/audit-r2-precision branch July 2, 2026 10:48
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.

2 participants