Commit baace01
fix: precision-correctness audit findings (F6/F11/F2/F1) (#74)
* fix: precision-correctness audit findings F6/F11/F2/F1
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).
* fix(workspace): restore LaTeX caption + engine on reload; stop mp.dps 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>
* fix(mcmc): index likelihood_weights via the None-check so mypy --strict 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>
---------
Co-authored-by: fanghao <fanghaoaa@gmail.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>1 parent 75af565 commit baace01
8 files changed
Lines changed: 252 additions & 12 deletions
File tree
- app_desktop
- cli
- datalab_core
- fitting
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
730 | 730 | | |
731 | 731 | | |
732 | 732 | | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
733 | 763 | | |
734 | 764 | | |
735 | 765 | | |
| |||
1910 | 1940 | | |
1911 | 1941 | | |
1912 | 1942 | | |
| 1943 | + | |
1913 | 1944 | | |
1914 | 1945 | | |
1915 | 1946 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
| 161 | + | |
| 162 | + | |
161 | 163 | | |
162 | 164 | | |
163 | 165 | | |
164 | 166 | | |
| 167 | + | |
165 | 168 | | |
166 | 169 | | |
167 | 170 | | |
168 | 171 | | |
169 | 172 | | |
170 | 173 | | |
171 | | - | |
172 | | - | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
173 | 189 | | |
174 | 190 | | |
175 | 191 | | |
176 | 192 | | |
177 | 193 | | |
178 | 194 | | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
| 195 | + | |
| 196 | + | |
184 | 197 | | |
185 | 198 | | |
186 | 199 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
87 | 106 | | |
88 | 107 | | |
89 | 108 | | |
| |||
141 | 160 | | |
142 | 161 | | |
143 | 162 | | |
| 163 | + | |
| 164 | + | |
144 | 165 | | |
145 | 166 | | |
146 | 167 | | |
| |||
153 | 174 | | |
154 | 175 | | |
155 | 176 | | |
| 177 | + | |
| 178 | + | |
156 | 179 | | |
157 | 180 | | |
158 | 181 | | |
159 | 182 | | |
160 | | - | |
| 183 | + | |
161 | 184 | | |
162 | 185 | | |
163 | 186 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
293 | 293 | | |
294 | 294 | | |
295 | 295 | | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
296 | 311 | | |
297 | 312 | | |
298 | 313 | | |
| |||
343 | 358 | | |
344 | 359 | | |
345 | 360 | | |
346 | | - | |
347 | | - | |
348 | | - | |
| 361 | + | |
349 | 362 | | |
350 | 363 | | |
351 | | - | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
352 | 372 | | |
353 | 373 | | |
354 | 374 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
60 | 101 | | |
61 | 102 | | |
62 | 103 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
206 | 206 | | |
207 | 207 | | |
208 | 208 | | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
209 | 246 | | |
210 | 247 | | |
211 | 248 | | |
| |||
0 commit comments