From 2330e5288171ce9df0d2e56bf6439a1ba4993450 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Shixiang=20Wang=20=28=E7=8E=8B=E8=AF=97=E7=BF=94=29?= Date: Tue, 24 Nov 2026 23:41:53 +0800 Subject: [PATCH] fix: abort-contract payload sweep -- 15 msuiter_abort() sites regain the mandatory i/j/c bullets (#129 sibling) The error discipline in R/classes-utils.R (ARCHITECTURE section 3.5) requires every validator violation to raise msuiter_error_ with the three-part information payload: i = what is wrong, j = where, c = how to fix. i, j and c are mandatory formals with NO defaults, so a call site that omits one does not raise the structured condition at all -- base R raises missingArgError / getvarError instead, in the locale language, with no payload for a caller to act on. A parse-tree census over R/*.R (utils::getParseData, SYMBOL_FUNCTION_CALL == "msuiter_abort", depth-0 SYMBOL_SUB collection) found 486 call sites, 15 of which passed two or three of the five names. This change restores the missing bullet(s) at each, with content appropriate to the site: R/calibrate-plot.R (2 sites) se/pass_window validation R/engines-ard.R (2 sites) counts matrix + k0 whole number R/engines-sparse.R (2 sites) counts matrix + k whole number R/fit.R (1 site) bca bootstrap interval selector R/ms-compare.R (1 site) n_draws Monte Carlo tail count R/ms-convert.R (1 site) opportunity vector shape R/viz-bootstrap.R (1 site) index / colname selector R/viz-profile.R (5 sites) index, colname and channel selectors The law is enforced going forward by a new static gate, tests/testthat/test-abort-contract.R: a firing-control unit proves the census sees a planted omission, then the gate walks the shipped R/ tree and asserts every call carries i, j and c. The file joins the sealed T0 roster at a 0 s integer weight (5 expects, witnessed nb=5 failed=0), so the pr-gate budget sum stays at its committed measurement of 625 -- a zero-weight roster row moves no seal, and the generator plus ci-selfcheck both re-price to 625. Verification: local frozen gate signature (2 WARNINGs + 2 NOTEs); test-ci-shard-map.R nb=212 failed=0; tools/ci-selfcheck.py PASS. Co-Authored-By: Claude Code --- R/calibrate-plot.R | 7 +- R/engines-ard.R | 8 +- R/engines-sparse.R | 8 +- R/fit.R | 1 + R/ms-compare.R | 1 + R/ms-convert.R | 1 + R/viz-bootstrap.R | 3 +- R/viz-profile.R | 17 ++++- tests/testthat/test-abort-contract.R | 107 +++++++++++++++++++++++++++ tools/ci/pr-gate-budget.tsv | 6 +- tools/ci/shard-manifest.tsv | 1 + tools/ci/shard-weights.tsv | 1 + 12 files changed, 149 insertions(+), 12 deletions(-) create mode 100644 tests/testthat/test-abort-contract.R diff --git a/R/calibrate-plot.R b/R/calibrate-plot.R index d5f2771b..a1429590 100644 --- a/R/calibrate-plot.R +++ b/R/calibrate-plot.R @@ -166,14 +166,17 @@ plot_calibration_curve <- function(calibration_df, window = NULL, se = TRUE) { "calibration_df$pass_window must be logical without NAs", i = "hand-built tables must pass the kernel verdict column verbatim", j = paste0("class: ", paste(class(df$pass_window), collapse = "/"), - "; NAs: ", sum(is.na(df$pass_window))) + "; NAs: ", sum(is.na(df$pass_window))), + c = "carry the logical pass_window column through unchanged" ) } if (se && anyNA(df$se)) { msuiter_abort( "input", "calibration_df$se must be non-missing when se = TRUE", - j = paste0("NAs: ", sum(is.na(df$se))) + i = "se = TRUE requests the standard-error ribbon", + j = paste0("NAs: ", sum(is.na(df$se))), + c = "populate the se column or pass se = FALSE" ) } fail <- df[!df$pass_window, , drop = FALSE] diff --git a/R/engines-ard.R b/R/engines-ard.R index ce2c7293..984b5208 100644 --- a/R/engines-ard.R +++ b/R/engines-ard.R @@ -144,7 +144,9 @@ ms_ard <- function(a0 = 10, b0 = 5, tol = 1e-5, max_iter = 2000, seed = 1) { msuiter_abort( "input", "counts must be a numeric matrix", - i = "the ARD kernel consumes the MsCatalog counts slot (transposed to t(counts) for the FFI)" + i = "the ARD kernel consumes the MsCatalog counts slot (transposed to t(counts) for the FFI)", + j = paste0("received: ", class(counts)[1L]), + c = "pass a channels x samples numeric matrix" ) } if (!is.numeric(k0) || length(k0) != 1L || is.na(k0) || k0 < 1 || @@ -152,7 +154,9 @@ ms_ard <- function(a0 = 10, b0 = 5, tol = 1e-5, max_iter = 2000, seed = 1) { msuiter_abort( "input", "`k0` must be a single positive whole number", - i = paste0("got: ", format(k0)) + i = "k0 is the number of components the ARD kernel estimates", + j = paste0("got: ", format(k0)), + c = "pass a whole number k0 >= 1" ) } ms_ard_rust( diff --git a/R/engines-sparse.R b/R/engines-sparse.R index 9620c164..f95c3ba2 100644 --- a/R/engines-sparse.R +++ b/R/engines-sparse.R @@ -163,7 +163,9 @@ ms_sparse <- function(variant = "l1", lambda = 0, mu = 0, msuiter_abort( "input", "counts must be a numeric matrix", - i = "the sparse kernel consumes the MsCatalog counts slot (transposed to t(counts) for the FFI)" + i = "the sparse kernel consumes the MsCatalog counts slot (transposed to t(counts) for the FFI)", + j = paste0("received: ", class(counts)[1L]), + c = "pass a channels x samples numeric matrix" ) } if (!is.numeric(k) || length(k) != 1L || is.na(k) || k < 1 || @@ -171,7 +173,9 @@ ms_sparse <- function(variant = "l1", lambda = 0, mu = 0, msuiter_abort( "input", "`k` must be a single positive whole number", - i = paste0("got: ", format(k)) + i = "k is the number of components the sparse kernel estimates", + j = paste0("got: ", format(k)), + c = "pass a whole number k >= 1" ) } ms_sparse_rust( diff --git a/R/fit.R b/R/fit.R index 7b40b6fa..a79b8847 100644 --- a/R/fit.R +++ b/R/fit.R @@ -921,6 +921,7 @@ S7::method(ms_fit_bootstrap, S7::class_any) <- function(catalog, signatures, msuiter_abort( "input", "bca must be a single TRUE or FALSE", + i = "bca selects the bias-corrected accelerated bootstrap interval", j = paste0("received: ", msuiter_quote_trunc(bca)), c = "leave the default FALSE for the percentile interval only" ) diff --git a/R/ms-compare.R b/R/ms-compare.R index d90ac2ee..b5c08156 100644 --- a/R/ms-compare.R +++ b/R/ms-compare.R @@ -387,6 +387,7 @@ msuiter_abort( "input", "n_draws must be a single whole number >= 200", + i = "n_draws is the Monte Carlo draw count for the null tail", j = paste0("received: ", msuiter_quote_trunc(n_draws)), c = "the add-one tail estimate needs a usable denominator" ) diff --git a/R/ms-convert.R b/R/ms-convert.R index fcb4796b..0e46f19f 100644 --- a/R/ms-convert.R +++ b/R/ms-convert.R @@ -104,6 +104,7 @@ ms_convert <- S7::new_generic( msuiter_abort( "input", sprintf("%s must be a frequency vector over the 96 channels", arg_nm), + i = "an opportunity vector is one frequency per registry channel", j = sprintf("received length: %d", length(opps)), c = "name it by the registry labels or pass a bundled table name" ) diff --git a/R/viz-bootstrap.R b/R/viz-bootstrap.R index 77ce17cb..2a753332 100644 --- a/R/viz-bootstrap.R +++ b/R/viz-bootstrap.R @@ -237,7 +237,8 @@ plot_boot_stability <- function(fit, sample = 1L) { "input", "sample must be an index or a column name", i = "one sample per CI face", - j = paste0("received: ", msuiter_quote_trunc(sample)) + j = paste0("received: ", msuiter_quote_trunc(sample)), + c = "pass an in-range index or an existing colname" ) } diff --git a/R/viz-profile.R b/R/viz-profile.R index 98c1c2ca..62db947f 100644 --- a/R/viz-profile.R +++ b/R/viz-profile.R @@ -205,7 +205,9 @@ plot_reconstruction_panel <- function(counts, signatures, exposures, msuiter_abort( "input", "counts and signatures must share the canonical channel order", - j = sprintf("rows: %d vs %d", length(pc$labels), length(ps$labels)) + i = "observed and fitted profiles must share one channel axis", + j = sprintf("rows: %d vs %d", length(pc$labels), length(ps$labels)), + c = "pass counts and signatures over the same channel table" ) } exposures <- as.matrix(exposures) @@ -213,8 +215,10 @@ plot_reconstruction_panel <- function(counts, signatures, exposures, msuiter_abort( "input", "exposures rows must match the signature columns", + i = "the fitted profile multiplies signatures by exposures", j = sprintf("exposures rows: %d; signatures: %d", - nrow(exposures), ncol(ps$matrix)) + nrow(exposures), ncol(ps$matrix)), + c = "pass exposures with one row per signature column" ) } fitted <- as.numeric(ps$matrix %*% exposures) @@ -289,6 +293,7 @@ plot_reconstruction_panel <- function(counts, signatures, exposures, msuiter_abort( "input", "profile input must be a numeric matrix (or named vector, or MsSignature)", + i = "the profile faces read one numeric m x k matrix", j = paste0("received: ", class(matrix)[1L]), c = "pass m x k with canonical registry rownames" ) @@ -355,8 +360,10 @@ plot_reconstruction_panel <- function(counts, signatures, exposures, msuiter_abort( "input", "column index out of range", + i = "column selects one profile column", j = sprintf("requested: %s; columns: %d", - msuiter_quote_trunc(idx), ncol(matrix)) + msuiter_quote_trunc(idx), ncol(matrix)), + c = "pass an index in [1, ncol]" ) } return(idx) @@ -367,7 +374,9 @@ plot_reconstruction_panel <- function(counts, signatures, exposures, msuiter_abort( "input", "requested column names are absent", - j = paste0("missing: ", msuiter_quote_trunc(missing)) + i = "columns are addressed by the matrix colnames", + j = paste0("missing: ", msuiter_quote_trunc(missing)), + c = "pass existing colnames or integer indices" ) } return(match(columns, cn)) diff --git a/tests/testthat/test-abort-contract.R b/tests/testthat/test-abort-contract.R new file mode 100644 index 00000000..5c9e34b9 --- /dev/null +++ b/tests/testthat/test-abort-contract.R @@ -0,0 +1,107 @@ +# The abort-contract law (R/classes-utils.R docs; ARCHITECTURE section 3.5): +# every `msuiter_abort()` call passes the mandatory i/j/c three-part payload, +# so a violation raises the structured `msuiter_error_` condition (the +# `msuiter_error` payload plus the three message bullets) rather than a bare +# base-R `missingArgError`/`getvarError` whose message prints in the locale's +# language and carries no payload. +# +# A missing argument parses cleanly, so no reader that only inspects present +# tokens can see it: the 2026-11-24 sweep found 15 sites in R/ that passed two +# or three of the five names (a genuine residual -- an earlier commit fixed the +# property validators but not the counts/k0/k guards). This file is the reader +# that sees the absence. + +# Census one source text: a data.frame(line, missing) over every +# msuiter_abort() call whose argument list omits any of i/j/c. getParseData +# yields byte-true line numbers and nesting, so a NESTED call's own commas are +# never read as the outer call's argument separators. +.abort_census_text <- function(text) { + empty <- data.frame(line = integer(0), missing = character(0), + stringsAsFactors = FALSE) + pd <- utils::getParseData(parse(text = text, keep.source = TRUE)) + if (is.null(pd) || nrow(pd) == 0L) return(empty) + nr <- nrow(pd) + calls <- which(pd$token == "SYMBOL_FUNCTION_CALL" & pd$text == "msuiter_abort") + if (length(calls) == 0L) return(empty) + out <- lapply(calls, function(r) { + opn <- which(pd$token == "'('" & seq_len(nr) > r)[1L] + if (is.na(opn)) return(NULL) + depth <- 0L + cls <- NA_integer_ + for (i in opn:nr) { + tok <- pd$token[i] + if (tok == "'('") depth <- depth + 1L + else if (tok == "')'") { + depth <- depth - 1L + if (depth == 0L) { cls <- i; break } + } + } + if (is.na(cls) || cls <= opn + 1L) return(NULL) + d <- 0L + subs <- character(0L) + for (i in (opn + 1L):(cls - 1L)) { + tok <- pd$token[i] + if (tok == "'('") d <- d + 1L + else if (tok == "')'") d <- d - 1L + else if (tok == "SYMBOL_SUB" && d == 0L) subs <- c(subs, pd$text[i]) + } + data.frame(line = pd$line1[r], + missing = paste(setdiff(c("i", "j", "c"), subs), collapse = ","), + stringsAsFactors = FALSE) + }) + out <- do.call(rbind, out) + if (is.null(out)) empty else out +} + +.abort_root <- function(start = testthat::test_path()) { + d <- normalizePath(start) + while (nzchar(dirname(d)) != nchar(d)) { + f <- file.path(d, "DESCRIPTION") + if (file.exists(f) && + any(grepl("^Package: msuiter", readLines(f, warn = FALSE)))) { + return(d) + } + d <- dirname(d) + } + # The simulated check layout runs the copied tests as a sibling of the + # package dir -- no DESCRIPTION ancestry there. Absent root is the + # check-context signal, spoken by the sanctioned skip, not a stop. + NA_character_ +} + +test_that("a census sees a missing i/j/c bullet (firing control)", { + bad <- "f <- function() { msuiter_abort('input', 'msg', i = 'x', j = 'y') }" + expect_identical(.abort_census_text(bad)$missing, "c") + good <- paste0("f <- function() { msuiter_abort('input', 'msg', i = 'x', ", + "j = 'y', c = 'z') }") + expect_false(any(nzchar(.abort_census_text(good)$missing))) + # A nested call's commas belong to the inner list, not the outer arg list. + nest <- paste0("f <- function() { msuiter_abort('input', 'msg', i = 'x', ", + "j = paste(c('a', 'b'), collapse = ','), c = 'z') }") + expect_false(any(nzchar(.abort_census_text(nest)$missing))) +}) + +test_that("every msuiter_abort() call carries the mandatory i/j/c payload", { + root <- .abort_root() + if (is.na(root)) { + skip(paste0("the abort census parses the R/ sources: requires the source ", + "checkout, which the check layout does not ship ", + "(R CMD check context)")) + } + files <- list.files(file.path(root, "R"), pattern = "[.]R$", + full.names = TRUE) + total <- 0L + offenders <- character() + for (f in files) { + res <- .abort_census_text(paste(readLines(f, warn = FALSE), collapse = "\n")) + total <- total + nrow(res) + bad <- res[nzchar(res$missing), , drop = FALSE] + if (nrow(bad) > 0L) { + offenders <- c(offenders, sprintf("%s:%d (missing %s)", basename(f), + bad$line, bad$missing)) + } + } + # Window liveness on the real tree: a scan reading nothing would pass blind. + expect_gt(total, 100L) + expect_identical(offenders, character()) +}) diff --git a/tools/ci/pr-gate-budget.tsv b/tools/ci/pr-gate-budget.tsv index 48fc96c8..14f08e84 100644 --- a/tools/ci/pr-gate-budget.tsv +++ b/tools/ci/pr-gate-budget.tsv @@ -81,6 +81,10 @@ # the T0 roster; witness: local run nb=265 failed=0 error=0 warning=0, measured # manifest sum 626 (generator refusal witnessed at the pre-seal re-mint # attempt, the verbatim DEFECT line naming 626 against the sealed 625). +# 2026-11-24 roster addition at constant 626: the abort-contract law row +# (test-abort-contract.R, 0 s integer weight, 5 expects) joined the T0 +# roster; witness: local run nb=5 failed=0 error=0 warning=0, elapsed 0.4 s, +# so the measured manifest sum stays 626 (a zero-weight row moves no seal). key value note -t0-measured 626 measured 2026-11-24 (integer manifest seconds, tier T0; 2026-11-24 seal 625 superseded by the CS-17 row) +t0-measured 626 measured 2026-11-24 (integer manifest seconds, tier T0; 2026-11-24 abort-contract row joins at 0 s, sum unchanged) t0-ceiling 800 t0-measured + one drift class; re-seal deliberately with a witness diff --git a/tools/ci/shard-manifest.tsv b/tools/ci/shard-manifest.tsv index 80d15205..c6601ffc 100644 --- a/tools/ci/shard-manifest.tsv +++ b/tools/ci/shard-manifest.tsv @@ -20,6 +20,7 @@ shard file seconds tier 4 test-viz-batch2.R 2 T0 4 test-viz-first-batch.R 11 T0 4 test-xval-sigminer.R 10 T0 +5 test-abort-contract.R 0 T0 5 test-calibrate-real.R 1 T0 5 test-calibrate.R 0 T0 5 test-capability-matrix-truth.R 0 T0 diff --git a/tools/ci/shard-weights.tsv b/tools/ci/shard-weights.tsv index f002790a..21230530 100644 --- a/tools/ci/shard-weights.tsv +++ b/tools/ci/shard-weights.tsv @@ -111,6 +111,7 @@ test-docs-tour-executable.R 10 28 T0 test-docs-scenarios-prose.R 1 19 T0 test-docs-extending-surface.R 1 20 T0 test-rng-hygiene.R 0 13 T0 +test-abort-contract.R 0 5 T0 test-scenario-stratify.R 1 91 T0 test-scenario-downsample.R 1 116 T0 test-scenario-reconstruction.R 1 92 T0